Coverage Report

Created: 2026-05-13 20:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/segment/column_reader.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "storage/segment/column_reader.h"
19
20
#include <assert.h>
21
#include <gen_cpp/Descriptors_types.h>
22
#include <gen_cpp/segment_v2.pb.h>
23
#include <glog/logging.h>
24
25
#include <algorithm>
26
#include <memory>
27
#include <ostream>
28
#include <set>
29
#include <utility>
30
31
#include "common/compiler_util.h" // IWYU pragma: keep
32
#include "common/status.h"
33
#include "core/assert_cast.h"
34
#include "core/binary_cast.hpp"
35
#include "core/column/column.h"
36
#include "core/column/column_array.h"
37
#include "core/column/column_map.h"
38
#include "core/column/column_nullable.h"
39
#include "core/column/column_struct.h"
40
#include "core/column/column_vector.h"
41
#include "core/data_type/data_type_agg_state.h"
42
#include "core/data_type/data_type_factory.hpp"
43
#include "core/data_type/data_type_nullable.h"
44
#include "core/data_type/define_primitive_type.h"
45
#include "core/decimal12.h"
46
#include "core/string_ref.h"
47
#include "core/types.h"
48
#include "core/value/decimalv2_value.h"
49
#include "core/value/vdatetime_value.h" //for VecDateTime
50
#include "io/fs/file_reader.h"
51
#include "storage/index/ann/ann_index_reader.h"
52
#include "storage/index/bloom_filter/bloom_filter.h"
53
#include "storage/index/bloom_filter/bloom_filter_index_reader.h"
54
#include "storage/index/index_file_reader.h"
55
#include "storage/index/index_reader.h"
56
#include "storage/index/inverted/analyzer/analyzer.h"
57
#include "storage/index/inverted/inverted_index_reader.h"
58
#include "storage/index/zone_map/zone_map_index.h"
59
#include "storage/iterators.h"
60
#include "storage/olap_common.h"
61
#include "storage/predicate/block_column_predicate.h"
62
#include "storage/predicate/column_predicate.h"
63
#include "storage/segment/binary_dict_page.h" // for BinaryDictPageDecoder
64
#include "storage/segment/binary_plain_page.h"
65
#include "storage/segment/column_meta_accessor.h"
66
#include "storage/segment/encoding_info.h" // for EncodingInfo
67
#include "storage/segment/page_decoder.h"
68
#include "storage/segment/page_handle.h" // for PageHandle
69
#include "storage/segment/page_io.h"
70
#include "storage/segment/page_pointer.h" // for PagePointer
71
#include "storage/segment/row_ranges.h"
72
#include "storage/segment/segment.h"
73
#include "storage/segment/segment_prefetcher.h"
74
#include "storage/segment/variant/variant_column_reader.h"
75
#include "storage/tablet/tablet_schema.h"
76
#include "storage/types.h" // for TypeInfo
77
#include "util/bitmap.h"
78
#include "util/block_compression.h"
79
#include "util/concurrency_stats.h"
80
#include "util/rle_encoding.h" // for RleDecoder
81
#include "util/slice.h"
82
83
namespace doris::segment_v2 {
84
#include "storage/segment/column_reader.h"
85
86
631
inline bool read_as_string(PrimitiveType type) {
87
631
    return type == PrimitiveType::TYPE_STRING || type == PrimitiveType::INVALID_TYPE ||
88
631
           type == PrimitiveType::TYPE_BITMAP || type == PrimitiveType::TYPE_FIXED_LENGTH_OBJECT;
89
631
}
90
91
Status ColumnReader::create_array(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
92
                                  const io::FileReaderSPtr& file_reader,
93
58.6k
                                  std::shared_ptr<ColumnReader>* reader) {
94
58.6k
    DCHECK(meta.children_columns_size() == 2 || meta.children_columns_size() == 3);
95
96
58.6k
    std::shared_ptr<ColumnReader> item_reader;
97
58.6k
    RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(0),
98
58.6k
                                         meta.children_columns(0).num_rows(), file_reader,
99
58.6k
                                         &item_reader));
100
101
58.6k
    std::shared_ptr<ColumnReader> offset_reader;
102
58.6k
    RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(1),
103
58.6k
                                         meta.children_columns(1).num_rows(), file_reader,
104
58.6k
                                         &offset_reader));
105
106
58.6k
    std::shared_ptr<ColumnReader> null_reader;
107
58.6k
    if (meta.is_nullable()) {
108
41.4k
        RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(2),
109
41.4k
                                             meta.children_columns(2).num_rows(), file_reader,
110
41.4k
                                             &null_reader));
111
41.4k
    }
112
113
    // The num rows of the array reader equals to the num rows of the length reader.
114
58.6k
    uint64_t array_num_rows = meta.children_columns(1).num_rows();
115
58.6k
    std::shared_ptr<ColumnReader> array_reader(
116
58.6k
            new ColumnReader(opts, meta, array_num_rows, file_reader));
117
    //  array reader do not need to init
118
58.6k
    array_reader->_sub_readers.resize(meta.children_columns_size());
119
58.6k
    array_reader->_sub_readers[0] = std::move(item_reader);
120
58.6k
    array_reader->_sub_readers[1] = std::move(offset_reader);
121
58.6k
    if (meta.is_nullable()) {
122
41.2k
        array_reader->_sub_readers[2] = std::move(null_reader);
123
41.2k
    }
124
58.6k
    array_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_ARRAY;
125
58.6k
    *reader = std::move(array_reader);
126
58.6k
    return Status::OK();
127
58.6k
}
128
129
Status ColumnReader::create_map(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
130
                                const io::FileReaderSPtr& file_reader,
131
34.1k
                                std::shared_ptr<ColumnReader>* reader) {
132
    // map reader now has 3 sub readers for key, value, offsets(scalar), null(scala)
133
34.1k
    DCHECK(meta.children_columns_size() == 3 || meta.children_columns_size() == 4);
134
34.1k
    std::shared_ptr<ColumnReader> key_reader;
135
34.1k
    RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(0),
136
34.1k
                                         meta.children_columns(0).num_rows(), file_reader,
137
34.1k
                                         &key_reader));
138
34.1k
    std::shared_ptr<ColumnReader> val_reader;
139
34.1k
    RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(1),
140
34.1k
                                         meta.children_columns(1).num_rows(), file_reader,
141
34.1k
                                         &val_reader));
142
34.1k
    std::shared_ptr<ColumnReader> offset_reader;
143
34.1k
    RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(2),
144
34.1k
                                         meta.children_columns(2).num_rows(), file_reader,
145
34.1k
                                         &offset_reader));
146
34.1k
    std::shared_ptr<ColumnReader> null_reader;
147
34.1k
    if (meta.is_nullable()) {
148
13.3k
        RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(3),
149
13.3k
                                             meta.children_columns(3).num_rows(), file_reader,
150
13.3k
                                             &null_reader));
151
13.3k
    }
152
153
    // The num rows of the map reader equals to the num rows of the length reader.
154
34.1k
    uint64_t map_num_rows = meta.children_columns(2).num_rows();
155
34.1k
    std::shared_ptr<ColumnReader> map_reader(
156
34.1k
            new ColumnReader(opts, meta, map_num_rows, file_reader));
157
34.1k
    map_reader->_sub_readers.resize(meta.children_columns_size());
158
159
34.1k
    map_reader->_sub_readers[0] = std::move(key_reader);
160
34.1k
    map_reader->_sub_readers[1] = std::move(val_reader);
161
34.1k
    map_reader->_sub_readers[2] = std::move(offset_reader);
162
34.1k
    if (meta.is_nullable()) {
163
13.3k
        map_reader->_sub_readers[3] = std::move(null_reader);
164
13.3k
    }
165
34.1k
    map_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_MAP;
166
34.1k
    *reader = std::move(map_reader);
167
34.1k
    return Status::OK();
168
34.1k
}
169
170
Status ColumnReader::create_struct(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
171
                                   uint64_t num_rows, const io::FileReaderSPtr& file_reader,
172
6.69k
                                   std::shared_ptr<ColumnReader>* reader) {
173
    // not support empty struct
174
6.69k
    DCHECK(meta.children_columns_size() >= 1);
175
    // create struct column reader
176
6.69k
    std::shared_ptr<ColumnReader> struct_reader(
177
6.69k
            new ColumnReader(opts, meta, num_rows, file_reader));
178
6.69k
    struct_reader->_sub_readers.reserve(meta.children_columns_size());
179
    // now we support struct column can add the children columns according to the schema-change behavior
180
36.3k
    for (int i = 0; i < meta.children_columns_size(); i++) {
181
29.6k
        std::shared_ptr<ColumnReader> sub_reader;
182
29.6k
        RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(i),
183
29.6k
                                             meta.children_columns(i).num_rows(), file_reader,
184
29.6k
                                             &sub_reader));
185
29.6k
        struct_reader->_sub_readers.push_back(std::move(sub_reader));
186
29.6k
    }
187
6.69k
    struct_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_STRUCT;
188
6.69k
    *reader = std::move(struct_reader);
189
6.69k
    return Status::OK();
190
6.69k
}
191
192
Status ColumnReader::create_agg_state(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
193
                                      uint64_t num_rows, const io::FileReaderSPtr& file_reader,
194
632
                                      std::shared_ptr<ColumnReader>* reader) {
195
632
    if (!meta.has_function_name()) { // meet old version ColumnMetaPB
196
0
        std::shared_ptr<ColumnReader> reader_local(
197
0
                new ColumnReader(opts, meta, num_rows, file_reader));
198
0
        RETURN_IF_ERROR(reader_local->init(&meta));
199
0
        *reader = std::move(reader_local);
200
0
        return Status::OK();
201
0
    }
202
203
632
    auto data_type = DataTypeFactory::instance().create_data_type(meta);
204
632
    const auto* agg_state_type = assert_cast<const DataTypeAggState*>(data_type.get());
205
632
    agg_state_type->check_function_compatibility(opts.be_exec_version);
206
632
    auto type = agg_state_type->get_serialized_type()->get_primitive_type();
207
208
632
    if (read_as_string(type)) {
209
588
        std::shared_ptr<ColumnReader> reader_local(
210
588
                new ColumnReader(opts, meta, num_rows, file_reader));
211
588
        RETURN_IF_ERROR(reader_local->init(&meta));
212
588
        *reader = std::move(reader_local);
213
588
        return Status::OK();
214
588
    } else if (type == PrimitiveType::TYPE_MAP) {
215
34
        return create_map(opts, meta, file_reader, reader);
216
34
    } else if (type == PrimitiveType::TYPE_ARRAY) {
217
10
        return create_array(opts, meta, file_reader, reader);
218
10
    } else if (type == PrimitiveType::TYPE_STRUCT) {
219
0
        return create_struct(opts, meta, num_rows, file_reader, reader);
220
0
    }
221
222
0
    return Status::InternalError("Not supported type: {}, serialized type: {}",
223
0
                                 agg_state_type->get_name(), int(type));
224
632
}
225
226
109k
bool ColumnReader::is_compaction_reader_type(ReaderType type) {
227
109k
    return type == ReaderType::READER_BASE_COMPACTION ||
228
109k
           type == ReaderType::READER_CUMULATIVE_COMPACTION ||
229
109k
           type == ReaderType::READER_COLD_DATA_COMPACTION ||
230
109k
           type == ReaderType::READER_SEGMENT_COMPACTION ||
231
109k
           type == ReaderType::READER_FULL_COMPACTION;
232
109k
}
233
234
Status ColumnReader::create(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
235
                            uint64_t num_rows, const io::FileReaderSPtr& file_reader,
236
6.48M
                            std::shared_ptr<ColumnReader>* reader) {
237
6.48M
    if (is_scalar_type((FieldType)meta.type())) {
238
6.38M
        std::shared_ptr<ColumnReader> reader_local(
239
6.38M
                new ColumnReader(opts, meta, num_rows, file_reader));
240
6.38M
        RETURN_IF_ERROR(reader_local->init(&meta));
241
6.38M
        *reader = std::move(reader_local);
242
6.38M
        return Status::OK();
243
6.38M
    } else {
244
108k
        auto type = (FieldType)meta.type();
245
108k
        switch (type) {
246
633
        case FieldType::OLAP_FIELD_TYPE_AGG_STATE: {
247
633
            return create_agg_state(opts, meta, num_rows, file_reader, reader);
248
0
        }
249
6.69k
        case FieldType::OLAP_FIELD_TYPE_STRUCT: {
250
6.69k
            return create_struct(opts, meta, num_rows, file_reader, reader);
251
0
        }
252
58.7k
        case FieldType::OLAP_FIELD_TYPE_ARRAY: {
253
58.7k
            return create_array(opts, meta, file_reader, reader);
254
0
        }
255
34.0k
        case FieldType::OLAP_FIELD_TYPE_MAP: {
256
34.0k
            return create_map(opts, meta, file_reader, reader);
257
0
        }
258
7.92k
        case FieldType::OLAP_FIELD_TYPE_VARIANT: {
259
            // Read variant only root data using a single ColumnReader
260
7.92k
            std::shared_ptr<ColumnReader> reader_local(
261
7.92k
                    new ColumnReader(opts, meta, num_rows, file_reader));
262
7.92k
            RETURN_IF_ERROR(reader_local->init(&meta));
263
7.92k
            *reader = std::move(reader_local);
264
7.92k
            return Status::OK();
265
7.92k
        }
266
0
        default:
267
0
            return Status::NotSupported("unsupported type for ColumnReader: {}",
268
0
                                        std::to_string(int(type)));
269
108k
        }
270
108k
    }
271
6.48M
}
272
273
7.96k
ColumnReader::ColumnReader() = default;
274
275
ColumnReader::ColumnReader(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
276
                           uint64_t num_rows, io::FileReaderSPtr file_reader)
277
6.49M
        : _use_index_page_cache(!config::disable_storage_page_cache),
278
6.49M
          _opts(opts),
279
6.49M
          _num_rows(num_rows),
280
6.49M
          _file_reader(std::move(file_reader)),
281
6.49M
          _dict_encoding_type(UNKNOWN_DICT_ENCODING) {
282
6.49M
    _meta_length = meta.length();
283
6.49M
    _meta_type = (FieldType)meta.type();
284
6.49M
    if (_meta_type == FieldType::OLAP_FIELD_TYPE_ARRAY) {
285
58.7k
        _meta_children_column_type = (FieldType)meta.children_columns(0).type();
286
58.7k
    }
287
6.49M
    _data_type = DataTypeFactory::instance().create_data_type(meta);
288
6.49M
    _meta_is_nullable = meta.is_nullable();
289
6.49M
    _meta_dict_page = meta.dict_page();
290
6.49M
    _meta_compression = meta.compression();
291
6.49M
}
292
293
6.35M
ColumnReader::~ColumnReader() = default;
294
295
6.39M
int64_t ColumnReader::get_metadata_size() const {
296
6.39M
    return sizeof(ColumnReader) + (_segment_zone_map ? _segment_zone_map->ByteSizeLong() : 0);
297
6.39M
}
298
299
#ifdef BE_TEST
300
/// This function is only used in UT to verify the correctness of data read from zone map
301
/// See UT case 'SegCompactionMoWTest.SegCompactionInterleaveWithBig_ooooOOoOooooooooO'
302
/// be/test/olap/segcompaction_mow_test.cpp
303
void ColumnReader::check_data_by_zone_map_for_test(const MutableColumnPtr& dst) const {
304
    if (!_segment_zone_map) {
305
        return;
306
    }
307
308
    const auto rows = dst->size();
309
    if (rows == 0) {
310
        return;
311
    }
312
313
    FieldType type = _type;
314
315
    if (type != FieldType::OLAP_FIELD_TYPE_INT) {
316
        return;
317
    }
318
319
    auto* non_nullable_column =
320
            dst->is_nullable()
321
                    ? assert_cast<ColumnNullable*>(dst.get())->get_nested_column_ptr().get()
322
                    : dst.get();
323
324
    /// `PredicateColumnType<TYPE_INT>` does not support `void get(size_t n, Field& res)`,
325
    /// So here only check `CoumnVector<TYPE_INT>`
326
    if (check_and_get_column<ColumnVector<TYPE_INT>>(non_nullable_column) == nullptr) {
327
        return;
328
    }
329
330
    ZoneMap zone_map;
331
    THROW_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map));
332
333
    if (zone_map.has_null) {
334
        return;
335
    }
336
337
    for (size_t i = 0; i != rows; ++i) {
338
        Field field;
339
        dst->get(i, field);
340
        DCHECK(!field.is_null());
341
        const auto v = field.get<TYPE_INT>();
342
        DCHECK_GE(v, zone_map.min_value.get<TYPE_INT>());
343
        DCHECK_LE(v, zone_map.max_value.get<TYPE_INT>());
344
    }
345
}
346
#endif
347
348
6.38M
Status ColumnReader::init(const ColumnMetaPB* meta) {
349
6.38M
    _type = (FieldType)meta->type();
350
351
6.38M
    if (meta->has_be_exec_version()) {
352
6.16M
        _be_exec_version = meta->be_exec_version();
353
6.16M
    }
354
355
6.39M
    if (_type == FieldType::OLAP_FIELD_TYPE_NONE || _type == FieldType::OLAP_FIELD_TYPE_UNKNOWN) {
356
0
        return Status::NotSupported("unsupported typeinfo, type={}", meta->type());
357
0
    }
358
6.38M
    RETURN_IF_ERROR(EncodingInfo::get(_type, meta->encoding(), {}, &_encoding_info));
359
360
18.7M
    for (int i = 0; i < meta->indexes_size(); i++) {
361
12.3M
        const auto& index_meta = meta->indexes(i);
362
12.3M
        switch (index_meta.type()) {
363
0
        case BITMAP_INDEX:
364
0
            break;
365
6.35M
        case ORDINAL_INDEX:
366
6.35M
            _ordinal_index.reset(
367
6.35M
                    new OrdinalIndexReader(_file_reader, _num_rows, index_meta.ordinal_index()));
368
6.35M
            break;
369
6.03M
        case ZONE_MAP_INDEX:
370
6.03M
            _segment_zone_map =
371
6.03M
                    std::make_unique<ZoneMapPB>(index_meta.zone_map_index().segment_zone_map());
372
6.03M
            _zone_map_index.reset(new ZoneMapIndexReader(
373
6.03M
                    _file_reader, index_meta.zone_map_index().page_zone_maps()));
374
6.03M
            break;
375
4.71k
        case BLOOM_FILTER_INDEX:
376
4.71k
            _bloom_filter_index.reset(
377
4.71k
                    new BloomFilterIndexReader(_file_reader, index_meta.bloom_filter_index()));
378
4.71k
            break;
379
0
        case NESTED_OFFSETS_INDEX:
380
0
            break;
381
0
        default:
382
0
            return Status::Corruption("Bad file {}: invalid column index type {}",
383
0
                                      _file_reader->path().native(), index_meta.type());
384
12.3M
        }
385
12.3M
    }
386
6.38M
    update_metadata_size();
387
388
    // ArrayColumnWriter writes a single empty array and flushes. In this scenario,
389
    // the item writer doesn't write any data and the corresponding ordinal index is empty.
390
6.38M
    if (_ordinal_index == nullptr && !is_empty()) {
391
0
        return Status::Corruption("Bad file {}: missing ordinal index for column {}",
392
0
                                  _file_reader->path().native(), meta->column_id());
393
0
    }
394
395
6.38M
    return Status::OK();
396
6.38M
}
397
398
Status ColumnReader::new_index_iterator(const std::shared_ptr<IndexFileReader>& index_file_reader,
399
                                        const TabletIndex* index_meta,
400
79.7k
                                        std::unique_ptr<IndexIterator>* iterator) {
401
79.7k
    RETURN_IF_ERROR(_load_index(index_file_reader, index_meta));
402
79.7k
    {
403
79.7k
        std::shared_lock<std::shared_mutex> rlock(_load_index_lock);
404
79.7k
        auto iter = _index_readers.find(index_meta->index_id());
405
79.9k
        if (iter != _index_readers.end()) {
406
79.9k
            if (iter->second != nullptr) {
407
79.9k
                RETURN_IF_ERROR(iter->second->new_iterator(iterator));
408
79.9k
            }
409
79.9k
        }
410
79.7k
    }
411
79.7k
    return Status::OK();
412
79.7k
}
413
414
Status ColumnReader::read_page(const ColumnIteratorOptions& iter_opts, const PagePointer& pp,
415
                               PageHandle* handle, Slice* page_body, PageFooterPB* footer,
416
1.84M
                               BlockCompressionCodec* codec, bool is_dict_page) const {
417
1.84M
    SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().column_reader_read_page);
418
1.84M
    iter_opts.sanity_check();
419
1.84M
    PageReadOptions opts(iter_opts.io_ctx);
420
1.84M
    opts.verify_checksum = _opts.verify_checksum;
421
1.84M
    opts.use_page_cache = iter_opts.use_page_cache;
422
1.84M
    opts.kept_in_memory = _opts.kept_in_memory;
423
1.84M
    opts.type = iter_opts.type;
424
1.84M
    opts.file_reader = iter_opts.file_reader;
425
1.84M
    opts.page_pointer = pp;
426
1.84M
    opts.codec = codec;
427
1.84M
    opts.stats = iter_opts.stats;
428
1.84M
    opts.encoding_info = _encoding_info;
429
1.84M
    opts.is_dict_page = is_dict_page;
430
431
1.84M
    return PageIO::read_and_decompress_page(opts, handle, page_body, footer);
432
1.84M
}
433
434
Status ColumnReader::get_row_ranges_by_zone_map(
435
        const AndBlockColumnPredicate* col_predicates,
436
        const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates,
437
81.9k
        RowRanges* row_ranges, const ColumnIteratorOptions& iter_opts) {
438
81.9k
    std::vector<uint32_t> page_indexes;
439
81.9k
    RETURN_IF_ERROR(
440
81.9k
            _get_filtered_pages(col_predicates, delete_predicates, &page_indexes, iter_opts));
441
81.9k
    RETURN_IF_ERROR(_calculate_row_ranges(page_indexes, row_ranges, iter_opts));
442
81.9k
    return Status::OK();
443
81.9k
}
444
445
7.95k
Status ColumnReader::next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) const {
446
7.95k
    if (_segment_zone_map == nullptr) {
447
0
        return Status::InternalError("segment zonemap not exist");
448
0
    }
449
    // TODO: this work to get min/max value seems should only do once
450
7.95k
    ZoneMap zone_map;
451
7.95k
    RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map));
452
453
7.95k
    dst->reserve(*n);
454
7.95k
    if (!zone_map.has_not_null) {
455
1.02k
        assert_cast<ColumnNullable&>(*dst).insert_many_defaults(*n);
456
1.02k
        return Status::OK();
457
1.02k
    }
458
6.92k
    dst->insert(zone_map.max_value);
459
13.8k
    for (int i = 1; i < *n; ++i) {
460
6.93k
        dst->insert(zone_map.min_value);
461
6.93k
    }
462
6.92k
    return Status::OK();
463
7.95k
}
464
465
Status ColumnReader::match_condition(const AndBlockColumnPredicate* col_predicates,
466
1.80M
                                     bool* matched) const {
467
1.80M
    *matched = true;
468
1.80M
    if (_zone_map_index == nullptr) {
469
0
        return Status::OK();
470
0
    }
471
1.80M
    ZoneMap zone_map;
472
1.80M
    RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map));
473
474
1.80M
    *matched = _zone_map_match_condition(zone_map, col_predicates);
475
1.80M
    return Status::OK();
476
1.80M
}
477
478
Status ColumnReader::prune_predicates_by_zone_map(
479
        std::vector<std::shared_ptr<ColumnPredicate>>& predicates, const int column_id,
480
23.9M
        bool* pruned) const {
481
23.9M
    *pruned = false;
482
23.9M
    if (_zone_map_index == nullptr) {
483
23.3k
        return Status::OK();
484
23.3k
    }
485
486
23.8M
    ZoneMap zone_map;
487
23.8M
    RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map));
488
23.8M
    if (zone_map.pass_all) {
489
0
        return Status::OK();
490
0
    }
491
492
48.0M
    for (auto it = predicates.begin(); it != predicates.end();) {
493
24.1M
        auto predicate = *it;
494
24.1M
        if (predicate->column_id() == column_id && predicate->is_always_true(zone_map)) {
495
13.7k
            *pruned = true;
496
13.7k
            it = predicates.erase(it);
497
24.1M
        } else {
498
24.1M
            ++it;
499
24.1M
        }
500
24.1M
    }
501
23.8M
    return Status::OK();
502
23.8M
}
503
504
bool ColumnReader::_zone_map_match_condition(const ZoneMap& zone_map,
505
1.89M
                                             const AndBlockColumnPredicate* col_predicates) const {
506
1.89M
    if (zone_map.pass_all) {
507
0
        return true;
508
0
    }
509
510
1.89M
    return col_predicates->evaluate_and(zone_map);
511
1.89M
}
512
513
Status ColumnReader::_get_filtered_pages(
514
        const AndBlockColumnPredicate* col_predicates,
515
        const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates,
516
82.3k
        std::vector<uint32_t>* page_indexes, const ColumnIteratorOptions& iter_opts) {
517
82.3k
    RETURN_IF_ERROR(_load_zone_map_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts));
518
519
82.3k
    const std::vector<ZoneMapPB>& zone_maps = _zone_map_index->page_zone_maps();
520
82.3k
    size_t page_size = _zone_map_index->num_pages();
521
215k
    for (size_t i = 0; i < page_size; ++i) {
522
132k
        if (zone_maps[i].pass_all()) {
523
47.5k
            page_indexes->push_back(cast_set<uint32_t>(i));
524
85.2k
        } else {
525
85.2k
            segment_v2::ZoneMap zone_map;
526
85.2k
            RETURN_IF_ERROR(ZoneMap::from_proto(zone_maps[i], _data_type, zone_map));
527
85.3k
            if (_zone_map_match_condition(zone_map, col_predicates)) {
528
85.3k
                bool should_read = true;
529
85.3k
                if (delete_predicates != nullptr) {
530
4
                    for (auto del_pred : *delete_predicates) {
531
                        // TODO: Both `min_value` and `max_value` should be 0 or neither should be 0.
532
                        //  So nullable only need to judge once.
533
4
                        if (del_pred->evaluate_del(zone_map)) {
534
1
                            should_read = false;
535
1
                            break;
536
1
                        }
537
4
                    }
538
3
                }
539
85.3k
                if (should_read) {
540
85.3k
                    page_indexes->push_back(cast_set<uint32_t>(i));
541
85.3k
                }
542
85.3k
            }
543
85.2k
        }
544
132k
    }
545
18.4E
    VLOG(1) << "total-pages: " << page_size << " not-filtered-pages: " << page_indexes->size()
546
18.4E
            << " filtered-percent:"
547
18.4E
            << 1.0 - (static_cast<double>(page_indexes->size()) /
548
18.4E
                      (static_cast<double>(page_size) * 1.0));
549
82.3k
    return Status::OK();
550
82.3k
}
551
552
Status ColumnReader::_calculate_row_ranges(const std::vector<uint32_t>& page_indexes,
553
                                           RowRanges* row_ranges,
554
81.8k
                                           const ColumnIteratorOptions& iter_opts) {
555
81.8k
    row_ranges->clear();
556
81.8k
    RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts));
557
133k
    for (auto i : page_indexes) {
558
133k
        ordinal_t page_first_id = _ordinal_index->get_first_ordinal(i);
559
133k
        ordinal_t page_last_id = _ordinal_index->get_last_ordinal(i);
560
133k
        RowRanges page_row_ranges(RowRanges::create_single(page_first_id, page_last_id + 1));
561
133k
        RowRanges::ranges_union(*row_ranges, page_row_ranges, row_ranges);
562
133k
    }
563
81.8k
    return Status::OK();
564
81.8k
}
565
566
Status ColumnReader::get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates,
567
                                                    RowRanges* row_ranges,
568
97
                                                    const ColumnIteratorOptions& iter_opts) {
569
97
    RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts));
570
97
    RETURN_IF_ERROR(
571
97
            _load_bloom_filter_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts));
572
97
    RowRanges bf_row_ranges;
573
97
    std::unique_ptr<BloomFilterIndexIterator> bf_iter;
574
97
    RETURN_IF_ERROR(_bloom_filter_index->new_iterator(&bf_iter, iter_opts.stats));
575
97
    size_t range_size = row_ranges->range_size();
576
    // get covered page ids
577
97
    std::set<uint32_t> page_ids;
578
194
    for (int i = 0; i < range_size; ++i) {
579
97
        int64_t from = row_ranges->get_range_from(i);
580
97
        int64_t idx = from;
581
97
        int64_t to = row_ranges->get_range_to(i);
582
97
        auto iter = _ordinal_index->seek_at_or_before(from);
583
221
        while (idx < to && iter.valid()) {
584
124
            page_ids.insert(iter.page_index());
585
124
            idx = iter.last_ordinal() + 1;
586
124
            iter.next();
587
124
        }
588
97
    }
589
124
    for (auto& pid : page_ids) {
590
124
        std::unique_ptr<BloomFilter> bf;
591
124
        RETURN_IF_ERROR(bf_iter->read_bloom_filter(pid, &bf));
592
124
        if (col_predicates->evaluate_and(bf.get())) {
593
21
            bf_row_ranges.add(RowRange(_ordinal_index->get_first_ordinal(pid),
594
21
                                       _ordinal_index->get_last_ordinal(pid) + 1));
595
21
        }
596
124
    }
597
97
    RowRanges::ranges_intersection(*row_ranges, bf_row_ranges, row_ranges);
598
97
    return Status::OK();
599
97
}
600
601
Status ColumnReader::_load_ordinal_index(bool use_page_cache, bool kept_in_memory,
602
1.51M
                                         const ColumnIteratorOptions& iter_opts) {
603
1.51M
    if (!_ordinal_index) {
604
0
        return Status::InternalError("ordinal_index not inited");
605
0
    }
606
1.51M
    return _ordinal_index->load(use_page_cache, kept_in_memory, iter_opts.stats);
607
1.51M
}
608
609
Status ColumnReader::_load_zone_map_index(bool use_page_cache, bool kept_in_memory,
610
82.3k
                                          const ColumnIteratorOptions& iter_opts) {
611
82.3k
    if (_zone_map_index != nullptr) {
612
82.3k
        return _zone_map_index->load(use_page_cache, kept_in_memory, iter_opts.stats);
613
82.3k
    }
614
18.4E
    return Status::OK();
615
82.3k
}
616
617
Status ColumnReader::_load_index(const std::shared_ptr<IndexFileReader>& index_file_reader,
618
79.8k
                                 const TabletIndex* index_meta) {
619
79.8k
    std::unique_lock<std::shared_mutex> wlock(_load_index_lock);
620
621
79.8k
    if (index_meta == nullptr) {
622
0
        return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>(
623
0
                "Failed to load inverted index: index metadata is null");
624
0
    }
625
626
79.8k
    auto it = _index_readers.find(index_meta->index_id());
627
79.8k
    if (it != _index_readers.end()) {
628
15.7k
        return Status::OK();
629
15.7k
    }
630
631
64.1k
    bool should_analyzer =
632
64.1k
            inverted_index::InvertedIndexAnalyzer::should_analyzer(index_meta->properties());
633
634
64.1k
    FieldType type;
635
64.1k
    if (_meta_type == FieldType::OLAP_FIELD_TYPE_ARRAY) {
636
3.91k
        type = _meta_children_column_type;
637
60.2k
    } else {
638
60.2k
        type = _type;
639
60.2k
    }
640
641
64.1k
    if (index_meta->index_type() == IndexType::ANN) {
642
70
        _index_readers[index_meta->index_id()] =
643
70
                std::make_shared<AnnIndexReader>(index_meta, index_file_reader);
644
70
        return Status::OK();
645
70
    }
646
647
64.0k
    IndexReaderPtr index_reader;
648
649
64.0k
    if (is_string_type(type)) {
650
33.4k
        if (should_analyzer) {
651
20.1k
            try {
652
20.1k
                index_reader = FullTextIndexReader::create_shared(index_meta, index_file_reader);
653
20.1k
            } catch (const CLuceneError& e) {
654
0
                return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>(
655
0
                        "create FullTextIndexReader error: {}", e.what());
656
0
            }
657
20.1k
        } else {
658
13.2k
            try {
659
13.2k
                index_reader =
660
13.2k
                        StringTypeInvertedIndexReader::create_shared(index_meta, index_file_reader);
661
13.2k
            } catch (const CLuceneError& e) {
662
0
                return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>(
663
0
                        "create StringTypeInvertedIndexReader error: {}", e.what());
664
0
            }
665
13.2k
        }
666
33.4k
    } else if (is_numeric_type(type)) {
667
30.5k
        try {
668
30.5k
            index_reader = BkdIndexReader::create_shared(index_meta, index_file_reader);
669
30.5k
        } catch (const CLuceneError& e) {
670
0
            return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>(
671
0
                    "create BkdIndexReader error: {}", e.what());
672
0
        }
673
30.5k
    } else {
674
18
        return Status::Error<ErrorCode::INVERTED_INDEX_NOT_SUPPORTED>(
675
18
                "Field type {} is not supported for inverted index", type);
676
18
    }
677
64.0k
    _index_readers[index_meta->index_id()] = index_reader;
678
64.0k
    return Status::OK();
679
64.0k
}
680
681
59.9k
bool ColumnReader::has_bloom_filter_index(bool ngram) const {
682
59.9k
    if (_bloom_filter_index == nullptr) return false;
683
684
18.4E
    if (ngram) {
685
77
        return _bloom_filter_index->algorithm() == BloomFilterAlgorithmPB::NGRAM_BLOOM_FILTER;
686
18.4E
    } else {
687
18.4E
        return _bloom_filter_index->algorithm() != BloomFilterAlgorithmPB::NGRAM_BLOOM_FILTER;
688
18.4E
    }
689
18.4E
}
690
691
Status ColumnReader::_load_bloom_filter_index(bool use_page_cache, bool kept_in_memory,
692
97
                                              const ColumnIteratorOptions& iter_opts) {
693
97
    if (_bloom_filter_index != nullptr) {
694
97
        return _bloom_filter_index->load(use_page_cache, kept_in_memory, iter_opts.stats);
695
97
    }
696
0
    return Status::OK();
697
97
}
698
699
Status ColumnReader::seek_at_or_before(ordinal_t ordinal, OrdinalPageIndexIterator* iter,
700
1.43M
                                       const ColumnIteratorOptions& iter_opts) {
701
1.43M
    RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts));
702
1.43M
    *iter = _ordinal_index->seek_at_or_before(ordinal);
703
1.43M
    if (!iter->valid()) {
704
0
        return Status::NotFound("Failed to seek to ordinal {}, ", ordinal);
705
0
    }
706
1.43M
    return Status::OK();
707
1.43M
}
708
709
Status ColumnReader::get_ordinal_index_reader(OrdinalIndexReader*& reader,
710
293k
                                              OlapReaderStatistics* index_load_stats) {
711
18.4E
    CHECK(_ordinal_index) << fmt::format("ordinal index is null for column reader of type {}",
712
18.4E
                                         std::to_string(int(_meta_type)));
713
293k
    RETURN_IF_ERROR(
714
293k
            _ordinal_index->load(_use_index_page_cache, _opts.kept_in_memory, index_load_stats));
715
293k
    reader = _ordinal_index.get();
716
293k
    return Status::OK();
717
293k
}
718
719
388k
Status ColumnReader::new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column) {
720
388k
    return new_iterator(iterator, tablet_column, nullptr);
721
388k
}
722
723
Status ColumnReader::new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column,
724
27.2M
                                  const StorageReadOptions* opt) {
725
27.2M
    if (is_empty()) {
726
44.1k
        *iterator = std::make_unique<EmptyFileColumnIterator>();
727
44.1k
        return Status::OK();
728
44.1k
    }
729
27.2M
    if (is_scalar_type(_meta_type)) {
730
27.1M
        if (is_string_type(_meta_type)) {
731
16.3M
            *iterator = std::make_unique<StringFileColumnIterator>(shared_from_this());
732
16.3M
        } else {
733
10.7M
            *iterator = std::make_unique<FileColumnIterator>(shared_from_this());
734
10.7M
        }
735
27.1M
        (*iterator)->set_column_name(tablet_column ? tablet_column->name() : "");
736
27.1M
        return Status::OK();
737
27.1M
    } else {
738
93.6k
        auto type = _meta_type;
739
93.6k
        switch (type) {
740
821
        case FieldType::OLAP_FIELD_TYPE_AGG_STATE: {
741
821
            return new_agg_state_iterator(iterator);
742
0
        }
743
7.87k
        case FieldType::OLAP_FIELD_TYPE_STRUCT: {
744
7.87k
            return new_struct_iterator(iterator, tablet_column);
745
0
        }
746
63.5k
        case FieldType::OLAP_FIELD_TYPE_ARRAY: {
747
63.5k
            return new_array_iterator(iterator, tablet_column);
748
0
        }
749
36.1k
        case FieldType::OLAP_FIELD_TYPE_MAP: {
750
36.1k
            return new_map_iterator(iterator, tablet_column);
751
0
        }
752
0
        default:
753
0
            return Status::NotSupported("unsupported type to create iterator: {}",
754
0
                                        std::to_string(int(type)));
755
93.6k
        }
756
93.6k
    }
757
27.2M
}
758
759
816
Status ColumnReader::new_agg_state_iterator(ColumnIteratorUPtr* iterator) {
760
816
    *iterator = std::make_unique<FileColumnIterator>(shared_from_this());
761
816
    return Status::OK();
762
816
}
763
764
Status ColumnReader::new_array_iterator(ColumnIteratorUPtr* iterator,
765
63.3k
                                        const TabletColumn* tablet_column) {
766
63.3k
    ColumnIteratorUPtr item_iterator;
767
63.3k
    RETURN_IF_ERROR(_sub_readers[0]->new_iterator(
768
63.3k
            &item_iterator, tablet_column && tablet_column->get_subtype_count() > 0
769
63.3k
                                    ? &tablet_column->get_sub_column(0)
770
63.3k
                                    : nullptr));
771
772
63.3k
    item_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(0).name() : "");
773
774
63.3k
    ColumnIteratorUPtr offset_iterator;
775
63.3k
    RETURN_IF_ERROR(_sub_readers[1]->new_iterator(&offset_iterator, nullptr));
776
63.3k
    auto* file_iter = static_cast<FileColumnIterator*>(offset_iterator.release());
777
63.3k
    OffsetFileColumnIteratorUPtr ofcIter = std::make_unique<OffsetFileColumnIterator>(
778
63.3k
            std::unique_ptr<FileColumnIterator>(file_iter));
779
780
63.3k
    ColumnIteratorUPtr null_iterator;
781
63.3k
    if (is_nullable()) {
782
45.5k
        RETURN_IF_ERROR(_sub_readers[2]->new_iterator(&null_iterator, nullptr));
783
45.5k
    }
784
63.3k
    *iterator = std::make_unique<ArrayFileColumnIterator>(shared_from_this(), std::move(ofcIter),
785
63.3k
                                                          std::move(item_iterator),
786
63.3k
                                                          std::move(null_iterator));
787
63.3k
    return Status::OK();
788
63.3k
}
789
790
Status ColumnReader::new_map_iterator(ColumnIteratorUPtr* iterator,
791
36.1k
                                      const TabletColumn* tablet_column) {
792
36.1k
    ColumnIteratorUPtr key_iterator;
793
36.1k
    RETURN_IF_ERROR(_sub_readers[0]->new_iterator(
794
36.1k
            &key_iterator, tablet_column && tablet_column->get_subtype_count() > 1
795
36.1k
                                   ? &tablet_column->get_sub_column(0)
796
36.1k
                                   : nullptr));
797
36.1k
    key_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(0).name() : "");
798
36.1k
    ColumnIteratorUPtr val_iterator;
799
36.1k
    RETURN_IF_ERROR(_sub_readers[1]->new_iterator(
800
36.1k
            &val_iterator, tablet_column && tablet_column->get_subtype_count() > 1
801
36.1k
                                   ? &tablet_column->get_sub_column(1)
802
36.1k
                                   : nullptr));
803
36.1k
    val_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(1).name() : "");
804
36.1k
    ColumnIteratorUPtr offsets_iterator;
805
36.1k
    RETURN_IF_ERROR(_sub_readers[2]->new_iterator(&offsets_iterator, nullptr));
806
36.1k
    auto* file_iter = static_cast<FileColumnIterator*>(offsets_iterator.release());
807
36.1k
    OffsetFileColumnIteratorUPtr ofcIter = std::make_unique<OffsetFileColumnIterator>(
808
36.1k
            std::unique_ptr<FileColumnIterator>(file_iter));
809
810
36.1k
    ColumnIteratorUPtr null_iterator;
811
36.1k
    if (is_nullable()) {
812
15.2k
        RETURN_IF_ERROR(_sub_readers[3]->new_iterator(&null_iterator, nullptr));
813
15.2k
    }
814
36.1k
    *iterator = std::make_unique<MapFileColumnIterator>(
815
36.1k
            shared_from_this(), std::move(null_iterator), std::move(ofcIter),
816
36.1k
            std::move(key_iterator), std::move(val_iterator));
817
36.1k
    return Status::OK();
818
36.1k
}
819
820
Status ColumnReader::new_struct_iterator(ColumnIteratorUPtr* iterator,
821
7.86k
                                         const TabletColumn* tablet_column) {
822
7.86k
    std::vector<ColumnIteratorUPtr> sub_column_iterators;
823
7.86k
    size_t child_size = is_nullable() ? _sub_readers.size() - 1 : _sub_readers.size();
824
18.4E
    size_t tablet_column_size = tablet_column ? tablet_column->get_sub_columns().size() : 0;
825
7.86k
    sub_column_iterators.reserve(child_size);
826
827
34.9k
    for (uint64_t i = 0; i < child_size; i++) {
828
27.1k
        ColumnIteratorUPtr sub_column_iterator;
829
27.1k
        RETURN_IF_ERROR(_sub_readers[i]->new_iterator(
830
27.1k
                &sub_column_iterator, tablet_column ? &tablet_column->get_sub_column(i) : nullptr));
831
27.1k
        sub_column_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(i).name()
832
27.1k
                                                           : "");
833
27.1k
        sub_column_iterators.emplace_back(std::move(sub_column_iterator));
834
27.1k
    }
835
836
    // create default_iterator for schema-change behavior which increase column
837
9.17k
    for (size_t i = child_size; i < tablet_column_size; i++) {
838
1.30k
        TabletColumn column = tablet_column->get_sub_column(i);
839
1.30k
        ColumnIteratorUPtr it;
840
1.30k
        RETURN_IF_ERROR(Segment::new_default_iterator(column, &it));
841
1.30k
        it->set_column_name(column.name());
842
1.30k
        sub_column_iterators.emplace_back(std::move(it));
843
1.30k
    }
844
845
7.86k
    ColumnIteratorUPtr null_iterator;
846
7.86k
    if (is_nullable()) {
847
7.03k
        RETURN_IF_ERROR(_sub_readers[child_size]->new_iterator(&null_iterator, nullptr));
848
7.03k
    }
849
7.86k
    *iterator = std::make_unique<StructFileColumnIterator>(
850
7.86k
            shared_from_this(), std::move(null_iterator), std::move(sub_column_iterators));
851
7.86k
    return Status::OK();
852
7.86k
}
853
854
Result<TColumnAccessPaths> ColumnIterator::_get_sub_access_paths(
855
116k
        const TColumnAccessPaths& access_paths) {
856
116k
    TColumnAccessPaths sub_access_paths = access_paths;
857
179k
    for (auto it = sub_access_paths.begin(); it != sub_access_paths.end();) {
858
63.2k
        TColumnAccessPath& name_path = *it;
859
63.2k
        if (name_path.data_access_path.path.empty()) {
860
1
            return ResultError(
861
1
                    Status::InternalError("Invalid access path for struct column: path is empty"));
862
1
        }
863
864
63.2k
        if (!StringCaseEqual()(name_path.data_access_path.path[0], _column_name)) {
865
1
            return ResultError(Status::InternalError(
866
1
                    R"(Invalid access path for column: expected name "{}", got "{}")", _column_name,
867
1
                    name_path.data_access_path.path[0]));
868
1
        }
869
870
63.2k
        name_path.data_access_path.path.erase(name_path.data_access_path.path.begin());
871
63.2k
        if (!name_path.data_access_path.path.empty()) {
872
7.01k
            ++it;
873
56.2k
        } else {
874
56.2k
            set_need_to_read();
875
56.2k
            it = sub_access_paths.erase(it);
876
56.2k
        }
877
63.2k
    }
878
116k
    return sub_access_paths;
879
116k
}
880
881
///====================== MapFileColumnIterator ============================////
882
MapFileColumnIterator::MapFileColumnIterator(std::shared_ptr<ColumnReader> reader,
883
                                             ColumnIteratorUPtr null_iterator,
884
                                             OffsetFileColumnIteratorUPtr offsets_iterator,
885
                                             ColumnIteratorUPtr key_iterator,
886
                                             ColumnIteratorUPtr val_iterator)
887
36.1k
        : _map_reader(reader),
888
36.1k
          _offsets_iterator(std::move(offsets_iterator)),
889
36.1k
          _key_iterator(std::move(key_iterator)),
890
36.1k
          _val_iterator(std::move(val_iterator)) {
891
36.1k
    if (_map_reader->is_nullable()) {
892
15.2k
        _null_iterator = std::move(null_iterator);
893
15.2k
    }
894
36.1k
}
895
896
36.0k
Status MapFileColumnIterator::init(const ColumnIteratorOptions& opts) {
897
36.0k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
898
32
        DLOG(INFO) << "Map column iterator column " << _column_name << " skip reading.";
899
32
        return Status::OK();
900
32
    }
901
36.0k
    RETURN_IF_ERROR(_key_iterator->init(opts));
902
36.0k
    RETURN_IF_ERROR(_val_iterator->init(opts));
903
36.0k
    RETURN_IF_ERROR(_offsets_iterator->init(opts));
904
36.0k
    if (_map_reader->is_nullable()) {
905
15.1k
        RETURN_IF_ERROR(_null_iterator->init(opts));
906
15.1k
    }
907
36.0k
    return Status::OK();
908
36.0k
}
909
910
19.9k
Status MapFileColumnIterator::seek_to_ordinal(ordinal_t ord) {
911
19.9k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
912
0
        DLOG(INFO) << "Map column iterator column " << _column_name << " skip reading.";
913
0
        return Status::OK();
914
0
    }
915
916
19.9k
    if (read_null_map_only()) {
917
        // In NULL_MAP_ONLY mode, only seek the null iterator; skip offset/key/val iterators
918
5
        if (_map_reader->is_nullable() && _null_iterator) {
919
5
            RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord));
920
5
        }
921
5
        return Status::OK();
922
5
    }
923
924
19.9k
    if (_map_reader->is_nullable()) {
925
10.8k
        RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord));
926
10.8k
    }
927
19.9k
    RETURN_IF_ERROR(_offsets_iterator->seek_to_ordinal(ord));
928
19.9k
    if (read_offset_only()) {
929
        // In OFFSET_ONLY mode, key/value iterators are SKIP_READING, no need to seek them
930
468
        return Status::OK();
931
468
    }
932
    // here to use offset info
933
19.4k
    ordinal_t offset = 0;
934
19.4k
    RETURN_IF_ERROR(_offsets_iterator->_peek_one_offset(&offset));
935
19.4k
    RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(offset));
936
19.4k
    RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(offset));
937
19.4k
    return Status::OK();
938
19.4k
}
939
940
1.34k
Status MapFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) {
941
1.34k
    RETURN_IF_ERROR(_offsets_iterator->init_prefetcher(params));
942
1.34k
    if (_map_reader->is_nullable()) {
943
1.32k
        RETURN_IF_ERROR(_null_iterator->init_prefetcher(params));
944
1.32k
    }
945
1.34k
    RETURN_IF_ERROR(_key_iterator->init_prefetcher(params));
946
1.34k
    RETURN_IF_ERROR(_val_iterator->init_prefetcher(params));
947
1.34k
    return Status::OK();
948
1.34k
}
949
950
void MapFileColumnIterator::collect_prefetchers(
951
        std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
952
1.34k
        PrefetcherInitMethod init_method) {
953
1.34k
    _offsets_iterator->collect_prefetchers(prefetchers, init_method);
954
1.34k
    if (_map_reader->is_nullable()) {
955
1.32k
        _null_iterator->collect_prefetchers(prefetchers, init_method);
956
1.32k
    }
957
    // the actual data pages to read of key/value column depends on the read result of offset column,
958
    // so we can't init prefetch blocks according to rowids, just prefetch all data blocks here.
959
1.34k
    _key_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS);
960
1.34k
    _val_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS);
961
1.34k
}
962
963
19.9k
Status MapFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
964
19.9k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
965
0
        DLOG(INFO) << "Map column iterator column " << _column_name << " skip reading.";
966
0
        dst->insert_many_defaults(*n);
967
0
        return Status::OK();
968
0
    }
969
970
19.9k
    if (read_null_map_only()) {
971
        // NULL_MAP_ONLY mode: read null map, fill nested ColumnMap with empty defaults
972
5
        DORIS_CHECK(dst->is_nullable());
973
5
        auto& nullable_col = assert_cast<ColumnNullable&>(*dst);
974
5
        auto null_map_ptr = nullable_col.get_null_map_column_ptr();
975
5
        size_t num_read = *n;
976
5
        if (_null_iterator) {
977
5
            bool null_signs_has_null = false;
978
5
            RETURN_IF_ERROR(
979
5
                    _null_iterator->next_batch(&num_read, null_map_ptr, &null_signs_has_null));
980
5
        } else {
981
            // schema-change: column became nullable but old segment has no null data
982
0
            auto& null_map = assert_cast<ColumnUInt8&, TypeCheckOnRelease::DISABLE>(*null_map_ptr);
983
0
            null_map.insert_many_vals(0, num_read);
984
0
        }
985
5
        DCHECK(num_read == *n);
986
        // fill nested ColumnMap with empty (zero-element) maps
987
5
        auto& column_map = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>(
988
5
                nullable_col.get_nested_column());
989
5
        column_map.insert_many_defaults(num_read);
990
5
        *has_null = true;
991
5
        return Status::OK();
992
5
    }
993
994
19.9k
    auto& column_map = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>(
995
19.9k
            dst->is_nullable() ? static_cast<ColumnNullable&>(*dst).get_nested_column() : *dst);
996
19.9k
    auto column_offsets_ptr = column_map.get_offsets_column().assume_mutable();
997
19.9k
    bool offsets_has_null = false;
998
19.9k
    ssize_t start = column_offsets_ptr->size();
999
19.9k
    RETURN_IF_ERROR(_offsets_iterator->next_batch(n, column_offsets_ptr, &offsets_has_null));
1000
19.9k
    if (*n == 0) {
1001
0
        return Status::OK();
1002
0
    }
1003
19.9k
    auto& column_offsets = static_cast<ColumnArray::ColumnOffsets&>(*column_offsets_ptr);
1004
19.9k
    RETURN_IF_ERROR(_offsets_iterator->_calculate_offsets(start, column_offsets));
1005
19.9k
    DCHECK(column_offsets.get_data().back() >= column_offsets.get_data()[start - 1]);
1006
19.9k
    size_t num_items =
1007
19.9k
            column_offsets.get_data().back() - column_offsets.get_data()[start - 1]; // -1 is valid
1008
19.9k
    auto key_ptr = column_map.get_keys().assume_mutable();
1009
19.9k
    auto val_ptr = column_map.get_values().assume_mutable();
1010
1011
19.9k
    if (num_items > 0) {
1012
15.4k
        if (read_offset_only()) {
1013
            // OFFSET_ONLY mode: skip reading actual key/value data, fill with defaults
1014
468
            key_ptr->insert_many_defaults(num_items);
1015
468
            val_ptr->insert_many_defaults(num_items);
1016
14.9k
        } else {
1017
14.9k
            size_t num_read = num_items;
1018
14.9k
            bool key_has_null = false;
1019
14.9k
            bool val_has_null = false;
1020
14.9k
            RETURN_IF_ERROR(_key_iterator->next_batch(&num_read, key_ptr, &key_has_null));
1021
14.9k
            RETURN_IF_ERROR(_val_iterator->next_batch(&num_read, val_ptr, &val_has_null));
1022
14.9k
            DCHECK(num_read == num_items);
1023
14.9k
        }
1024
1025
15.4k
        column_map.get_keys_ptr() = std::move(key_ptr);
1026
15.4k
        column_map.get_values_ptr() = std::move(val_ptr);
1027
15.4k
    }
1028
1029
19.9k
    if (dst->is_nullable()) {
1030
10.8k
        size_t num_read = *n;
1031
10.8k
        auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr();
1032
        // in not-null to null linked-schemachange mode,
1033
        // actually we do not change dat data include meta in footer,
1034
        // so may dst from changed meta which is nullable but old data is not nullable,
1035
        // if so, we should set null_map to all null by default
1036
10.8k
        if (_null_iterator) {
1037
10.8k
            bool null_signs_has_null = false;
1038
10.8k
            RETURN_IF_ERROR(
1039
10.8k
                    _null_iterator->next_batch(&num_read, null_map_ptr, &null_signs_has_null));
1040
18.4E
        } else {
1041
18.4E
            auto& null_map = assert_cast<ColumnUInt8&, TypeCheckOnRelease::DISABLE>(*null_map_ptr);
1042
18.4E
            null_map.insert_many_vals(0, num_read);
1043
18.4E
        }
1044
10.8k
        DCHECK(num_read == *n);
1045
10.8k
    }
1046
19.9k
    return Status::OK();
1047
19.9k
}
1048
1049
Status MapFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count,
1050
15.9k
                                             MutableColumnPtr& dst) {
1051
15.9k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1052
1
        DLOG(INFO) << "File column iterator column " << _column_name << " skip reading.";
1053
1
        dst->insert_many_defaults(count);
1054
1
        return Status::OK();
1055
1
    }
1056
1057
15.9k
    if (read_null_map_only()) {
1058
        // NULL_MAP_ONLY mode: read null map by rowids, fill nested ColumnMap with empty defaults
1059
7
        DORIS_CHECK(dst->is_nullable());
1060
7
        auto& nullable_col = assert_cast<ColumnNullable&>(*dst);
1061
7
        if (_null_iterator) {
1062
7
            auto null_map_ptr = nullable_col.get_null_map_column_ptr();
1063
7
            RETURN_IF_ERROR(_null_iterator->read_by_rowids(rowids, count, null_map_ptr));
1064
7
        } else {
1065
            // schema-change: column became nullable but old segment has no null data
1066
0
            auto null_map_ptr = nullable_col.get_null_map_column_ptr();
1067
0
            auto& null_map = assert_cast<ColumnUInt8&, TypeCheckOnRelease::DISABLE>(*null_map_ptr);
1068
0
            null_map.insert_many_vals(0, count);
1069
0
        }
1070
        // fill nested ColumnMap with empty (zero-element) maps
1071
7
        auto& column_map = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>(
1072
7
                nullable_col.get_nested_column());
1073
7
        column_map.insert_many_defaults(count);
1074
7
        return Status::OK();
1075
7
    }
1076
1077
15.8k
    if (count == 0) {
1078
0
        return Status::OK();
1079
0
    }
1080
    // resolve ColumnMap and nullable wrapper
1081
15.8k
    const auto* column_map = check_and_get_column<ColumnMap>(
1082
15.8k
            dst->is_nullable() ? static_cast<ColumnNullable&>(*dst).get_nested_column() : *dst);
1083
15.8k
    auto offsets_ptr = column_map->get_offsets_column().assume_mutable();
1084
15.8k
    auto& offsets = static_cast<ColumnArray::ColumnOffsets&>(*offsets_ptr);
1085
15.8k
    size_t base = offsets.get_data().empty() ? 0 : offsets.get_data().back();
1086
1087
    // 1. bulk read null-map if nullable
1088
15.8k
    std::vector<uint8_t> null_mask; // 0: not null, 1: null
1089
15.8k
    if (_map_reader->is_nullable()) {
1090
        // For nullable map columns, the destination column must also be nullable.
1091
3.10k
        if (UNLIKELY(!dst->is_nullable())) {
1092
0
            return Status::InternalError(
1093
0
                    "unexpected non-nullable destination column for nullable map reader");
1094
0
        }
1095
3.10k
        auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr();
1096
3.10k
        size_t null_before = null_map_ptr->size();
1097
3.10k
        RETURN_IF_ERROR(_null_iterator->read_by_rowids(rowids, count, null_map_ptr));
1098
        // extract a light-weight view to decide element reads
1099
3.10k
        auto& null_map_col = assert_cast<ColumnUInt8&>(*null_map_ptr);
1100
3.10k
        null_mask.reserve(count);
1101
56.4k
        for (size_t i = 0; i < count; ++i) {
1102
53.3k
            null_mask.push_back(null_map_col.get_element(null_before + i));
1103
53.3k
        }
1104
12.7k
    } else if (dst->is_nullable()) {
1105
        // in not-null to null linked-schemachange mode,
1106
        // actually we do not change dat data include meta in footer,
1107
        // so may dst from changed meta which is nullable but old data is not nullable,
1108
        // if so, we should set null_map to all null by default
1109
1
        auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr();
1110
1
        auto& null_map = assert_cast<ColumnUInt8&>(*null_map_ptr);
1111
1
        null_map.insert_many_vals(0, count);
1112
1
    }
1113
1114
    // 2. bulk read start ordinals for requested rows
1115
15.8k
    MutableColumnPtr starts_col = ColumnOffset64::create();
1116
15.8k
    starts_col->reserve(count);
1117
15.8k
    RETURN_IF_ERROR(_offsets_iterator->read_by_rowids(rowids, count, starts_col));
1118
1119
    // 3. bulk read next-start ordinals for rowid+1 (within bounds)
1120
15.8k
    std::vector<rowid_t> next_rowids(count);
1121
2.32M
    for (size_t i = 0; i < count; ++i) {
1122
2.30M
        uint64_t nr = rowids[i] + 1;
1123
2.30M
        next_rowids[i] = nr < _map_reader->num_rows() ? static_cast<rowid_t>(nr)
1124
2.30M
                                                      : static_cast<rowid_t>(0); // placeholder
1125
2.30M
    }
1126
15.8k
    MutableColumnPtr next_starts_col = ColumnOffset64::create();
1127
15.8k
    next_starts_col->reserve(count);
1128
    // read for all; we'll fix out-of-bound cases below
1129
15.8k
    RETURN_IF_ERROR(_offsets_iterator->read_by_rowids(next_rowids.data(), count, next_starts_col));
1130
1131
    // 4. fix next_start for rows whose next_rowid is out-of-bound (rowid == num_rows-1)
1132
2.32M
    for (size_t i = 0; i < count; ++i) {
1133
2.30M
        if (rowids[i] + 1 >= _map_reader->num_rows()) {
1134
            // seek to the last row and consume one to move decoder to end-of-page,
1135
            // then peek page-tail sentinel next_array_item_ordinal as next_start
1136
13.9k
            RETURN_IF_ERROR(_offsets_iterator->seek_to_ordinal(rowids[i]));
1137
13.9k
            size_t one = 1;
1138
13.9k
            bool has_null_unused = false;
1139
13.9k
            MutableColumnPtr tmp = ColumnOffset64::create();
1140
13.9k
            RETURN_IF_ERROR(_offsets_iterator->next_batch(&one, tmp, &has_null_unused));
1141
13.9k
            ordinal_t ns = 0;
1142
13.9k
            RETURN_IF_ERROR(_offsets_iterator->_peek_one_offset(&ns));
1143
            // overwrite with sentinel
1144
13.9k
            assert_cast<ColumnOffset64&, TypeCheckOnRelease::DISABLE>(*next_starts_col)
1145
13.9k
                    .get_data()[i] = ns;
1146
13.9k
        }
1147
2.30M
    }
1148
1149
    // 5. compute sizes and append offsets prefix-sum
1150
15.8k
    auto& starts_data = assert_cast<ColumnOffset64&>(*starts_col).get_data();
1151
15.8k
    auto& next_starts_data = assert_cast<ColumnOffset64&>(*next_starts_col).get_data();
1152
15.8k
    std::vector<size_t> sizes(count, 0);
1153
15.8k
    size_t acc = base;
1154
15.8k
    const auto original_size = offsets.get_data().back();
1155
15.8k
    offsets.get_data().reserve(offsets.get_data().size() + count);
1156
2.30M
    for (size_t i = 0; i < count; ++i) {
1157
2.28M
        size_t sz = static_cast<size_t>(next_starts_data[i] - starts_data[i]);
1158
2.28M
        if (_map_reader->is_nullable() && !null_mask.empty() && null_mask[i]) {
1159
726
            sz = 0; // null rows do not consume elements
1160
726
        }
1161
2.28M
        sizes[i] = sz;
1162
2.28M
        acc += sz;
1163
2.28M
        offsets.get_data().push_back(acc);
1164
2.28M
    }
1165
1166
    // 6. read key/value elements for non-empty sizes
1167
15.8k
    auto keys_ptr = column_map->get_keys().assume_mutable();
1168
15.8k
    auto vals_ptr = column_map->get_values().assume_mutable();
1169
1170
15.8k
    size_t this_run = sizes[0];
1171
15.8k
    auto start_idx = starts_data[0];
1172
15.8k
    auto last_idx = starts_data[0] + this_run;
1173
2.29M
    for (size_t i = 1; i < count; ++i) {
1174
2.27M
        size_t sz = sizes[i];
1175
2.27M
        if (sz == 0) {
1176
15.4k
            continue;
1177
15.4k
        }
1178
2.25M
        auto start = static_cast<ordinal_t>(starts_data[i]);
1179
2.25M
        if (start != last_idx) {
1180
457k
            size_t n = this_run;
1181
457k
            bool dummy_has_null = false;
1182
1183
457k
            if (this_run != 0) {
1184
458k
                if (_key_iterator->reading_flag() != ReadingFlag::SKIP_READING) {
1185
458k
                    RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(start_idx));
1186
458k
                    RETURN_IF_ERROR(_key_iterator->next_batch(&n, keys_ptr, &dummy_has_null));
1187
458k
                    DCHECK(n == this_run);
1188
458k
                }
1189
1190
458k
                if (_val_iterator->reading_flag() != ReadingFlag::SKIP_READING) {
1191
458k
                    n = this_run;
1192
458k
                    RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(start_idx));
1193
458k
                    RETURN_IF_ERROR(_val_iterator->next_batch(&n, vals_ptr, &dummy_has_null));
1194
458k
                    DCHECK(n == this_run);
1195
458k
                }
1196
457k
            }
1197
457k
            start_idx = start;
1198
457k
            this_run = sz;
1199
457k
            last_idx = start + sz;
1200
457k
            continue;
1201
457k
        }
1202
1203
1.80M
        this_run += sz;
1204
1.80M
        last_idx += sz;
1205
1.80M
    }
1206
1207
15.8k
    size_t n = this_run;
1208
15.8k
    const size_t total_count = offsets.get_data().back() - original_size;
1209
15.8k
    bool dummy_has_null = false;
1210
15.8k
    if (_key_iterator->reading_flag() != ReadingFlag::SKIP_READING) {
1211
15.8k
        if (this_run != 0) {
1212
6.85k
            RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(start_idx));
1213
6.85k
            RETURN_IF_ERROR(_key_iterator->next_batch(&n, keys_ptr, &dummy_has_null));
1214
6.85k
            DCHECK(n == this_run);
1215
6.85k
        }
1216
15.8k
    } else {
1217
6
        keys_ptr->insert_many_defaults(total_count);
1218
6
    }
1219
1220
15.8k
    if (_val_iterator->reading_flag() != ReadingFlag::SKIP_READING) {
1221
15.8k
        if (this_run != 0) {
1222
6.84k
            n = this_run;
1223
6.84k
            RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(start_idx));
1224
6.84k
            RETURN_IF_ERROR(_val_iterator->next_batch(&n, vals_ptr, &dummy_has_null));
1225
6.84k
            DCHECK(n == this_run);
1226
6.84k
        }
1227
15.8k
    } else {
1228
16
        vals_ptr->insert_many_defaults(total_count);
1229
16
    }
1230
1231
15.8k
    return Status::OK();
1232
15.8k
}
1233
1234
5.70k
void MapFileColumnIterator::set_need_to_read() {
1235
5.70k
    set_reading_flag(ReadingFlag::NEED_TO_READ);
1236
5.70k
    _key_iterator->set_need_to_read();
1237
5.70k
    _val_iterator->set_need_to_read();
1238
5.70k
}
1239
1240
6.88k
void MapFileColumnIterator::remove_pruned_sub_iterators() {
1241
6.88k
    _key_iterator->remove_pruned_sub_iterators();
1242
6.88k
    _val_iterator->remove_pruned_sub_iterators();
1243
6.88k
}
1244
1245
Status MapFileColumnIterator::set_access_paths(const TColumnAccessPaths& all_access_paths,
1246
6.65k
                                               const TColumnAccessPaths& predicate_access_paths) {
1247
6.65k
    if (all_access_paths.empty()) {
1248
1.51k
        return Status::OK();
1249
1.51k
    }
1250
1251
5.14k
    if (!predicate_access_paths.empty()) {
1252
52
        set_reading_flag(ReadingFlag::READING_FOR_PREDICATE);
1253
52
        DLOG(INFO) << "Map column iterator set sub-column " << _column_name
1254
52
                   << " to READING_FOR_PREDICATE";
1255
52
    }
1256
1257
5.14k
    auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths));
1258
5.14k
    auto sub_predicate_access_paths = DORIS_TRY(_get_sub_access_paths(predicate_access_paths));
1259
1260
5.14k
    if (sub_all_access_paths.empty()) {
1261
3.96k
        return Status::OK();
1262
3.96k
    }
1263
1264
    // Check for meta-only modes (OFFSET_ONLY or NULL_MAP_ONLY)
1265
1.17k
    _check_and_set_meta_read_mode(sub_all_access_paths);
1266
1.17k
    if (read_offset_only()) {
1267
477
        _key_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1268
477
        _val_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1269
477
        DLOG(INFO) << "Map column iterator set column " << _column_name
1270
477
                   << " to OFFSET_ONLY reading mode, key/value columns set to SKIP_READING";
1271
477
        return Status::OK();
1272
477
    }
1273
701
    if (read_null_map_only()) {
1274
12
        _key_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1275
12
        _val_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1276
12
        DLOG(INFO) << "Map column iterator set column " << _column_name
1277
12
                   << " to NULL_MAP_ONLY reading mode, key/value columns set to SKIP_READING";
1278
12
        return Status::OK();
1279
12
    }
1280
1281
689
    TColumnAccessPaths key_all_access_paths;
1282
689
    TColumnAccessPaths val_all_access_paths;
1283
689
    TColumnAccessPaths key_predicate_access_paths;
1284
689
    TColumnAccessPaths val_predicate_access_paths;
1285
1286
714
    for (auto paths : sub_all_access_paths) {
1287
714
        if (paths.data_access_path.path[0] == ACCESS_ALL) {
1288
            // ACCESS_ALL means element_at(map, key) style access: the key column must be
1289
            // fully read so that the runtime can match the requested key, while any sub-path
1290
            // qualifiers (e.g. OFFSET) apply only to the value column.
1291
            // For key: create a path with just the column name (= full data access).
1292
189
            TColumnAccessPath key_path;
1293
189
            key_path.__set_type(paths.type);
1294
189
            TDataAccessPath key_data_path;
1295
189
            key_data_path.__set_path({_key_iterator->column_name()});
1296
189
            key_path.__set_data_access_path(key_data_path);
1297
189
            key_all_access_paths.emplace_back(std::move(key_path));
1298
            // For value: pass the full sub-path so qualifiers like OFFSET propagate.
1299
189
            paths.data_access_path.path[0] = _val_iterator->column_name();
1300
189
            val_all_access_paths.emplace_back(paths);
1301
525
        } else if (paths.data_access_path.path[0] == ACCESS_MAP_KEYS) {
1302
268
            paths.data_access_path.path[0] = _key_iterator->column_name();
1303
268
            key_all_access_paths.emplace_back(paths);
1304
268
        } else if (paths.data_access_path.path[0] == ACCESS_MAP_VALUES) {
1305
258
            paths.data_access_path.path[0] = _val_iterator->column_name();
1306
258
            val_all_access_paths.emplace_back(paths);
1307
258
        }
1308
714
    }
1309
689
    const auto need_read_keys = !key_all_access_paths.empty();
1310
689
    const auto need_read_values = !val_all_access_paths.empty();
1311
1312
689
    for (auto paths : sub_predicate_access_paths) {
1313
21
        if (paths.data_access_path.path[0] == ACCESS_ALL) {
1314
            // Same logic as above: key needs full data, value gets the sub-path.
1315
19
            TColumnAccessPath key_path;
1316
19
            key_path.__set_type(paths.type);
1317
19
            TDataAccessPath key_data_path;
1318
19
            key_data_path.__set_path({_key_iterator->column_name()});
1319
19
            key_path.__set_data_access_path(key_data_path);
1320
19
            key_predicate_access_paths.emplace_back(std::move(key_path));
1321
19
            paths.data_access_path.path[0] = _val_iterator->column_name();
1322
19
            val_predicate_access_paths.emplace_back(paths);
1323
19
        } else if (paths.data_access_path.path[0] == ACCESS_MAP_KEYS) {
1324
0
            paths.data_access_path.path[0] = _key_iterator->column_name();
1325
0
            key_predicate_access_paths.emplace_back(paths);
1326
2
        } else if (paths.data_access_path.path[0] == ACCESS_MAP_VALUES) {
1327
0
            paths.data_access_path.path[0] = _val_iterator->column_name();
1328
0
            val_predicate_access_paths.emplace_back(paths);
1329
0
        }
1330
21
    }
1331
1332
689
    if (need_read_keys) {
1333
455
        _key_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ);
1334
455
        RETURN_IF_ERROR(
1335
455
                _key_iterator->set_access_paths(key_all_access_paths, key_predicate_access_paths));
1336
455
    } else {
1337
234
        _key_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1338
234
        DLOG(INFO) << "Map column iterator set key column to SKIP_READING";
1339
234
    }
1340
1341
689
    if (need_read_values) {
1342
443
        _val_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ);
1343
443
        RETURN_IF_ERROR(
1344
443
                _val_iterator->set_access_paths(val_all_access_paths, val_predicate_access_paths));
1345
443
    } else {
1346
246
        _val_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1347
246
        DLOG(INFO) << "Map column iterator set value column to SKIP_READING";
1348
246
    }
1349
689
    return Status::OK();
1350
689
}
1351
1352
////////////////////////////////////////////////////////////////////////////////
1353
1354
StructFileColumnIterator::StructFileColumnIterator(
1355
        std::shared_ptr<ColumnReader> reader, ColumnIteratorUPtr null_iterator,
1356
        std::vector<ColumnIteratorUPtr>&& sub_column_iterators)
1357
7.86k
        : _struct_reader(reader), _sub_column_iterators(std::move(sub_column_iterators)) {
1358
7.86k
    if (_struct_reader->is_nullable()) {
1359
7.02k
        _null_iterator = std::move(null_iterator);
1360
7.02k
    }
1361
7.86k
}
1362
1363
7.84k
Status StructFileColumnIterator::init(const ColumnIteratorOptions& opts) {
1364
7.84k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1365
0
        DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading.";
1366
0
        return Status::OK();
1367
0
    }
1368
1369
27.4k
    for (auto& column_iterator : _sub_column_iterators) {
1370
27.4k
        RETURN_IF_ERROR(column_iterator->init(opts));
1371
27.4k
    }
1372
7.84k
    if (_struct_reader->is_nullable()) {
1373
7.01k
        RETURN_IF_ERROR(_null_iterator->init(opts));
1374
7.01k
    }
1375
7.84k
    return Status::OK();
1376
7.84k
}
1377
1378
4.55k
Status StructFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
1379
4.55k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1380
0
        DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading.";
1381
0
        dst->insert_many_defaults(*n);
1382
0
        return Status::OK();
1383
0
    }
1384
1385
4.55k
    if (read_null_map_only()) {
1386
        // NULL_MAP_ONLY mode: read null map, fill nested ColumnStruct with empty defaults
1387
2
        DORIS_CHECK(dst->is_nullable());
1388
2
        auto& nullable_col = assert_cast<ColumnNullable&>(*dst);
1389
2
        auto null_map_ptr = nullable_col.get_null_map_column_ptr();
1390
2
        size_t num_read = *n;
1391
2
        if (_null_iterator) {
1392
2
            bool null_signs_has_null = false;
1393
2
            RETURN_IF_ERROR(
1394
2
                    _null_iterator->next_batch(&num_read, null_map_ptr, &null_signs_has_null));
1395
2
        } else {
1396
            // schema-change: column became nullable but old segment has no null data
1397
0
            auto& null_map = assert_cast<ColumnUInt8&, TypeCheckOnRelease::DISABLE>(*null_map_ptr);
1398
0
            null_map.insert_many_vals(0, num_read);
1399
0
        }
1400
2
        DCHECK(num_read == *n);
1401
        // fill nested ColumnStruct with defaults to maintain consistent column sizes
1402
2
        auto& column_struct = assert_cast<ColumnStruct&, TypeCheckOnRelease::DISABLE>(
1403
2
                nullable_col.get_nested_column());
1404
2
        column_struct.insert_many_defaults(num_read);
1405
2
        *has_null = true;
1406
2
        return Status::OK();
1407
2
    }
1408
1409
4.54k
    auto& column_struct = assert_cast<ColumnStruct&, TypeCheckOnRelease::DISABLE>(
1410
4.54k
            dst->is_nullable() ? static_cast<ColumnNullable&>(*dst).get_nested_column() : *dst);
1411
19.1k
    for (size_t i = 0; i < column_struct.tuple_size(); i++) {
1412
14.5k
        size_t num_read = *n;
1413
14.5k
        auto sub_column_ptr = column_struct.get_column(i).assume_mutable();
1414
14.5k
        bool column_has_null = false;
1415
14.5k
        RETURN_IF_ERROR(
1416
14.5k
                _sub_column_iterators[i]->next_batch(&num_read, sub_column_ptr, &column_has_null));
1417
14.5k
        DCHECK(num_read == *n);
1418
14.5k
        column_struct.get_column_ptr(i) = std::move(sub_column_ptr);
1419
14.5k
    }
1420
1421
4.54k
    if (dst->is_nullable()) {
1422
4.23k
        size_t num_read = *n;
1423
4.23k
        auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr();
1424
        // in not-null to null linked-schemachange mode,
1425
        // actually we do not change dat data include meta in footer,
1426
        // so may dst from changed meta which is nullable but old data is not nullable,
1427
        // if so, we should set null_map to all null by default
1428
4.23k
        if (_null_iterator) {
1429
4.14k
            bool null_signs_has_null = false;
1430
4.14k
            RETURN_IF_ERROR(
1431
4.14k
                    _null_iterator->next_batch(&num_read, null_map_ptr, &null_signs_has_null));
1432
4.14k
        } else {
1433
89
            auto& null_map = assert_cast<ColumnUInt8&, TypeCheckOnRelease::DISABLE>(*null_map_ptr);
1434
89
            null_map.insert_many_vals(0, num_read);
1435
89
        }
1436
4.23k
        DCHECK(num_read == *n);
1437
4.23k
    }
1438
1439
4.54k
    return Status::OK();
1440
4.54k
}
1441
1442
4.55k
Status StructFileColumnIterator::seek_to_ordinal(ordinal_t ord) {
1443
4.55k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1444
0
        DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading.";
1445
0
        return Status::OK();
1446
0
    }
1447
1448
4.55k
    if (read_null_map_only()) {
1449
        // In NULL_MAP_ONLY mode, only seek the null iterator; skip all sub-column iterators
1450
2
        if (_struct_reader->is_nullable() && _null_iterator) {
1451
2
            RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord));
1452
2
        }
1453
2
        return Status::OK();
1454
2
    }
1455
1456
14.5k
    for (auto& column_iterator : _sub_column_iterators) {
1457
14.5k
        RETURN_IF_ERROR(column_iterator->seek_to_ordinal(ord));
1458
14.5k
    }
1459
4.55k
    if (_struct_reader->is_nullable()) {
1460
4.14k
        RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord));
1461
4.14k
    }
1462
4.55k
    return Status::OK();
1463
4.55k
}
1464
1465
409
Status StructFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) {
1466
1.27k
    for (auto& column_iterator : _sub_column_iterators) {
1467
1.27k
        RETURN_IF_ERROR(column_iterator->init_prefetcher(params));
1468
1.27k
    }
1469
409
    if (_struct_reader->is_nullable()) {
1470
403
        RETURN_IF_ERROR(_null_iterator->init_prefetcher(params));
1471
403
    }
1472
409
    return Status::OK();
1473
409
}
1474
1475
void StructFileColumnIterator::collect_prefetchers(
1476
        std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
1477
409
        PrefetcherInitMethod init_method) {
1478
1.27k
    for (auto& column_iterator : _sub_column_iterators) {
1479
1.27k
        column_iterator->collect_prefetchers(prefetchers, init_method);
1480
1.27k
    }
1481
409
    if (_struct_reader->is_nullable()) {
1482
403
        _null_iterator->collect_prefetchers(prefetchers, init_method);
1483
403
    }
1484
409
}
1485
1486
Status StructFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count,
1487
2.67k
                                                MutableColumnPtr& dst) {
1488
2.67k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1489
0
        DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading.";
1490
0
        dst->insert_many_defaults(count);
1491
0
        return Status::OK();
1492
0
    }
1493
1494
2.67k
    if (count == 0) {
1495
0
        return Status::OK();
1496
0
    }
1497
1498
2.67k
    size_t this_run = 1;
1499
2.67k
    auto start_idx = rowids[0];
1500
2.67k
    auto last_idx = rowids[0];
1501
2.76k
    for (size_t i = 1; i < count; ++i) {
1502
87
        if (last_idx == rowids[i] - 1) {
1503
83
            last_idx = rowids[i];
1504
83
            this_run++;
1505
83
            continue;
1506
83
        }
1507
4
        RETURN_IF_ERROR(seek_to_ordinal(start_idx));
1508
4
        size_t num_read = this_run;
1509
4
        RETURN_IF_ERROR(next_batch(&num_read, dst));
1510
4
        DCHECK_EQ(num_read, this_run);
1511
1512
4
        start_idx = rowids[i];
1513
4
        last_idx = rowids[i];
1514
4
        this_run = 1;
1515
4
    }
1516
1517
2.67k
    RETURN_IF_ERROR(seek_to_ordinal(start_idx));
1518
2.67k
    size_t num_read = this_run;
1519
2.67k
    RETURN_IF_ERROR(next_batch(&num_read, dst));
1520
2.67k
    DCHECK_EQ(num_read, this_run);
1521
2.67k
    return Status::OK();
1522
2.67k
}
1523
1524
6.91k
void StructFileColumnIterator::set_need_to_read() {
1525
6.91k
    set_reading_flag(ReadingFlag::NEED_TO_READ);
1526
24.9k
    for (auto& sub_iterator : _sub_column_iterators) {
1527
24.9k
        sub_iterator->set_need_to_read();
1528
24.9k
    }
1529
6.91k
}
1530
1531
7.03k
void StructFileColumnIterator::remove_pruned_sub_iterators() {
1532
33.0k
    for (auto it = _sub_column_iterators.begin(); it != _sub_column_iterators.end();) {
1533
26.0k
        auto& sub_iterator = *it;
1534
26.0k
        if (sub_iterator->reading_flag() == ReadingFlag::SKIP_READING) {
1535
836
            DLOG(INFO) << "Struct column iterator remove pruned sub-column "
1536
836
                       << sub_iterator->column_name();
1537
836
            it = _sub_column_iterators.erase(it);
1538
25.1k
        } else {
1539
25.1k
            sub_iterator->remove_pruned_sub_iterators();
1540
25.1k
            ++it;
1541
25.1k
        }
1542
26.0k
    }
1543
7.03k
}
1544
1545
Status StructFileColumnIterator::set_access_paths(
1546
        const TColumnAccessPaths& all_access_paths,
1547
6.48k
        const TColumnAccessPaths& predicate_access_paths) {
1548
6.48k
    if (all_access_paths.empty()) {
1549
1.87k
        return Status::OK();
1550
1.87k
    }
1551
1552
4.61k
    if (!predicate_access_paths.empty()) {
1553
64
        set_reading_flag(ReadingFlag::READING_FOR_PREDICATE);
1554
64
        DLOG(INFO) << "Struct column iterator set sub-column " << _column_name
1555
64
                   << " to READING_FOR_PREDICATE";
1556
64
    }
1557
4.61k
    auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths));
1558
4.61k
    auto sub_predicate_access_paths = DORIS_TRY(_get_sub_access_paths(predicate_access_paths));
1559
1560
    // Check for NULL_MAP_ONLY mode: only read null map, skip all sub-columns
1561
4.61k
    _check_and_set_meta_read_mode(sub_all_access_paths);
1562
4.61k
    if (read_null_map_only()) {
1563
6
        for (auto& sub_iterator : _sub_column_iterators) {
1564
6
            sub_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1565
6
        }
1566
3
        DLOG(INFO) << "Struct column iterator set column " << _column_name
1567
3
                   << " to NULL_MAP_ONLY reading mode, all sub-columns set to SKIP_READING";
1568
3
        return Status::OK();
1569
3
    }
1570
1571
4.60k
    const auto no_sub_column_to_skip = sub_all_access_paths.empty();
1572
4.60k
    const auto no_predicate_sub_column = sub_predicate_access_paths.empty();
1573
1574
20.5k
    for (auto& sub_iterator : _sub_column_iterators) {
1575
20.5k
        const auto name = sub_iterator->column_name();
1576
20.5k
        bool need_to_read = no_sub_column_to_skip;
1577
20.5k
        TColumnAccessPaths sub_all_access_paths_of_this;
1578
20.5k
        if (!need_to_read) {
1579
1.35k
            for (const auto& paths : sub_all_access_paths) {
1580
1.35k
                if (paths.data_access_path.path[0] == name) {
1581
219
                    sub_all_access_paths_of_this.emplace_back(paths);
1582
219
                }
1583
1.35k
            }
1584
1.05k
            need_to_read = !sub_all_access_paths_of_this.empty();
1585
1.05k
        }
1586
1587
20.5k
        if (!need_to_read) {
1588
834
            set_reading_flag(ReadingFlag::SKIP_READING);
1589
834
            sub_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1590
834
            DLOG(INFO) << "Struct column iterator set sub-column " << name << " to SKIP_READING";
1591
834
            continue;
1592
834
        }
1593
19.7k
        set_reading_flag(ReadingFlag::NEED_TO_READ);
1594
19.7k
        sub_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ);
1595
1596
19.7k
        TColumnAccessPaths sub_predicate_access_paths_of_this;
1597
1598
19.7k
        if (!no_predicate_sub_column) {
1599
65
            for (const auto& paths : sub_predicate_access_paths) {
1600
65
                if (StringCaseEqual()(paths.data_access_path.path[0], name)) {
1601
57
                    sub_predicate_access_paths_of_this.emplace_back(paths);
1602
57
                }
1603
65
            }
1604
63
        }
1605
1606
19.7k
        RETURN_IF_ERROR(sub_iterator->set_access_paths(sub_all_access_paths_of_this,
1607
19.7k
                                                       sub_predicate_access_paths_of_this));
1608
19.7k
    }
1609
4.60k
    return Status::OK();
1610
4.60k
}
1611
1612
////////////////////////////////////////////////////////////////////////////////
1613
98.8k
Status OffsetFileColumnIterator::init(const ColumnIteratorOptions& opts) {
1614
98.8k
    RETURN_IF_ERROR(_offset_iterator->init(opts));
1615
    // allocate peek tmp column once
1616
98.8k
    _peek_tmp_col = ColumnOffset64::create();
1617
98.8k
    return Status::OK();
1618
98.8k
}
1619
1620
134k
Status OffsetFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
1621
134k
    RETURN_IF_ERROR(_offset_iterator->next_batch(n, dst, has_null));
1622
134k
    return Status::OK();
1623
134k
}
1624
1625
254k
Status OffsetFileColumnIterator::_peek_one_offset(ordinal_t* offset) {
1626
254k
    if (_offset_iterator->get_current_page()->has_remaining()) {
1627
166k
        PageDecoder* offset_page_decoder = _offset_iterator->get_current_page()->data_decoder.get();
1628
166k
        size_t n = 1;
1629
166k
        _peek_tmp_col->clear();
1630
166k
        RETURN_IF_ERROR(offset_page_decoder->peek_next_batch(&n, _peek_tmp_col)); // not null
1631
166k
        DCHECK(_peek_tmp_col->size() == 1);
1632
166k
        *offset =
1633
166k
                assert_cast<const ColumnOffset64*, TypeCheckOnRelease::DISABLE>(_peek_tmp_col.get())
1634
166k
                        ->get_element(0);
1635
166k
    } else {
1636
87.1k
        *offset = _offset_iterator->get_current_page()->next_array_item_ordinal;
1637
87.1k
    }
1638
254k
    return Status::OK();
1639
254k
}
1640
1641
6.61k
Status OffsetFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) {
1642
6.61k
    return _offset_iterator->init_prefetcher(params);
1643
6.61k
}
1644
1645
void OffsetFileColumnIterator::collect_prefetchers(
1646
        std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
1647
6.61k
        PrefetcherInitMethod init_method) {
1648
6.61k
    _offset_iterator->collect_prefetchers(prefetchers, init_method);
1649
6.61k
}
1650
1651
/**
1652
 *  first_storage_offset read from page should smaller than next_storage_offset which here call _peek_one_offset from page,
1653
    and first_column_offset is keep in memory data which is different dimension with (first_storage_offset and next_storage_offset)
1654
     eg. step1. read page: first_storage_offset = 16382
1655
         step2. read page below with _peek_one_offset(&last_offset): last_offset = 16387
1656
         step3. first_offset = 126 which is calculate in column offsets
1657
         for loop column offsets element in size
1658
            we can calculate from first_storage_offset to next_storage_offset one by one to fill with offsets_data in memory column offsets
1659
 * @param start
1660
 * @param column_offsets
1661
 * @return
1662
 */
1663
Status OffsetFileColumnIterator::_calculate_offsets(ssize_t start,
1664
120k
                                                    ColumnArray::ColumnOffsets& column_offsets) {
1665
120k
    ordinal_t next_storage_offset = 0;
1666
120k
    RETURN_IF_ERROR(_peek_one_offset(&next_storage_offset));
1667
1668
    // calculate real offsets
1669
120k
    auto& offsets_data = column_offsets.get_data();
1670
120k
    ordinal_t first_column_offset = offsets_data[start - 1]; // -1 is valid
1671
120k
    ordinal_t first_storage_offset = offsets_data[start];
1672
120k
    DCHECK(next_storage_offset >= first_storage_offset);
1673
9.62M
    for (ssize_t i = start; i < offsets_data.size() - 1; ++i) {
1674
9.50M
        offsets_data[i] = first_column_offset + (offsets_data[i + 1] - first_storage_offset);
1675
9.50M
    }
1676
    // last offset
1677
120k
    offsets_data[offsets_data.size() - 1] =
1678
120k
            first_column_offset + (next_storage_offset - first_storage_offset);
1679
120k
    return Status::OK();
1680
120k
}
1681
1682
////////////////////////////////////////////////////////////////////////////////
1683
ArrayFileColumnIterator::ArrayFileColumnIterator(std::shared_ptr<ColumnReader> reader,
1684
                                                 OffsetFileColumnIteratorUPtr offset_reader,
1685
                                                 ColumnIteratorUPtr item_iterator,
1686
                                                 ColumnIteratorUPtr null_iterator)
1687
63.2k
        : _array_reader(reader),
1688
63.2k
          _offset_iterator(std::move(offset_reader)),
1689
63.2k
          _item_iterator(std::move(item_iterator)) {
1690
63.2k
    if (_array_reader->is_nullable()) {
1691
45.4k
        _null_iterator = std::move(null_iterator);
1692
45.4k
    }
1693
63.2k
}
1694
1695
63.0k
Status ArrayFileColumnIterator::init(const ColumnIteratorOptions& opts) {
1696
63.0k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1697
56
        DLOG(INFO) << "Array column iterator column " << _column_name << " skip readking.";
1698
56
        return Status::OK();
1699
56
    }
1700
1701
62.9k
    RETURN_IF_ERROR(_offset_iterator->init(opts));
1702
62.9k
    RETURN_IF_ERROR(_item_iterator->init(opts));
1703
62.9k
    if (_array_reader->is_nullable()) {
1704
45.2k
        RETURN_IF_ERROR(_null_iterator->init(opts));
1705
45.2k
    }
1706
62.9k
    return Status::OK();
1707
62.9k
}
1708
1709
100k
Status ArrayFileColumnIterator::_seek_by_offsets(ordinal_t ord) {
1710
100k
    if (read_offset_only()) {
1711
        // In OFFSET_ONLY mode, item iterator is SKIP_READING, no need to seek it
1712
1.24k
        return Status::OK();
1713
1.24k
    }
1714
    // using offsets info
1715
99.7k
    ordinal_t offset = 0;
1716
99.7k
    RETURN_IF_ERROR(_offset_iterator->_peek_one_offset(&offset));
1717
99.7k
    RETURN_IF_ERROR(_item_iterator->seek_to_ordinal(offset));
1718
99.7k
    return Status::OK();
1719
99.7k
}
1720
1721
101k
Status ArrayFileColumnIterator::seek_to_ordinal(ordinal_t ord) {
1722
101k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1723
0
        DLOG(INFO) << "Array column iterator column " << _column_name << " skip reading.";
1724
0
        return Status::OK();
1725
0
    }
1726
1727
101k
    if (read_null_map_only()) {
1728
        // In NULL_MAP_ONLY mode, only seek the null iterator; skip offset and item iterators
1729
2
        if (_array_reader->is_nullable() && _null_iterator) {
1730
2
            RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord));
1731
2
        }
1732
2
        return Status::OK();
1733
2
    }
1734
1735
101k
    RETURN_IF_ERROR(_offset_iterator->seek_to_ordinal(ord));
1736
101k
    if (_array_reader->is_nullable()) {
1737
82.2k
        RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord));
1738
82.2k
    }
1739
101k
    return _seek_by_offsets(ord);
1740
101k
}
1741
1742
101k
Status ArrayFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
1743
101k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1744
0
        DLOG(INFO) << "Array column iterator column " << _column_name << " skip reading.";
1745
0
        dst->insert_many_defaults(*n);
1746
0
        return Status::OK();
1747
0
    }
1748
1749
101k
    if (read_null_map_only()) {
1750
        // NULL_MAP_ONLY mode: read null map, fill nested ColumnArray with empty defaults
1751
2
        DORIS_CHECK(dst->is_nullable());
1752
2
        auto& nullable_col = assert_cast<ColumnNullable&>(*dst);
1753
2
        auto null_map_ptr = nullable_col.get_null_map_column_ptr();
1754
2
        size_t num_read = *n;
1755
2
        if (_null_iterator) {
1756
2
            bool null_signs_has_null = false;
1757
2
            RETURN_IF_ERROR(
1758
2
                    _null_iterator->next_batch(&num_read, null_map_ptr, &null_signs_has_null));
1759
2
        } else {
1760
            // schema-change: column became nullable but old segment has no null data
1761
0
            auto& null_map = assert_cast<ColumnUInt8&, TypeCheckOnRelease::DISABLE>(*null_map_ptr);
1762
0
            null_map.insert_many_vals(0, num_read);
1763
0
        }
1764
2
        DCHECK(num_read == *n);
1765
        // fill nested ColumnArray with empty (zero-length) arrays
1766
2
        auto& column_array = assert_cast<ColumnArray&, TypeCheckOnRelease::DISABLE>(
1767
2
                nullable_col.get_nested_column());
1768
2
        column_array.insert_many_defaults(num_read);
1769
2
        *has_null = true;
1770
2
        return Status::OK();
1771
2
    }
1772
1773
101k
    const auto* column_array = check_and_get_column<ColumnArray>(
1774
101k
            dst->is_nullable() ? static_cast<ColumnNullable&>(*dst).get_nested_column() : *dst);
1775
1776
101k
    bool offsets_has_null = false;
1777
101k
    auto column_offsets_ptr = column_array->get_offsets_column().assume_mutable();
1778
101k
    ssize_t start = column_offsets_ptr->size();
1779
101k
    RETURN_IF_ERROR(_offset_iterator->next_batch(n, column_offsets_ptr, &offsets_has_null));
1780
101k
    if (*n == 0) {
1781
0
        return Status::OK();
1782
0
    }
1783
101k
    auto& column_offsets = static_cast<ColumnArray::ColumnOffsets&>(*column_offsets_ptr);
1784
101k
    RETURN_IF_ERROR(_offset_iterator->_calculate_offsets(start, column_offsets));
1785
101k
    size_t num_items =
1786
101k
            column_offsets.get_data().back() - column_offsets.get_data()[start - 1]; // -1 is valid
1787
101k
    auto column_items_ptr = column_array->get_data().assume_mutable();
1788
101k
    if (num_items > 0) {
1789
75.0k
        if (read_offset_only()) {
1790
            // OFFSET_ONLY mode: skip reading actual item data, fill with defaults
1791
1.03k
            column_items_ptr->insert_many_defaults(num_items);
1792
73.9k
        } else {
1793
73.9k
            size_t num_read = num_items;
1794
73.9k
            bool items_has_null = false;
1795
73.9k
            RETURN_IF_ERROR(
1796
73.9k
                    _item_iterator->next_batch(&num_read, column_items_ptr, &items_has_null));
1797
73.9k
            DCHECK(num_read == num_items);
1798
73.9k
        }
1799
75.0k
    }
1800
1801
101k
    if (dst->is_nullable()) {
1802
82.3k
        auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr();
1803
82.3k
        size_t num_read = *n;
1804
        // in not-null to null linked-schemachange mode,
1805
        // actually we do not change dat data include meta in footer,
1806
        // so may dst from changed meta which is nullable but old data is not nullable,
1807
        // if so, we should set null_map to all null by default
1808
82.3k
        if (_null_iterator) {
1809
82.3k
            bool null_signs_has_null = false;
1810
82.3k
            RETURN_IF_ERROR(
1811
82.3k
                    _null_iterator->next_batch(&num_read, null_map_ptr, &null_signs_has_null));
1812
18.4E
        } else {
1813
18.4E
            auto& null_map = assert_cast<ColumnUInt8&, TypeCheckOnRelease::DISABLE>(*null_map_ptr);
1814
18.4E
            null_map.insert_many_vals(0, num_read);
1815
18.4E
        }
1816
82.3k
        DCHECK(num_read == *n);
1817
82.3k
    }
1818
1819
101k
    return Status::OK();
1820
101k
}
1821
1822
5.27k
Status ArrayFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) {
1823
5.27k
    RETURN_IF_ERROR(_offset_iterator->init_prefetcher(params));
1824
5.27k
    RETURN_IF_ERROR(_item_iterator->init_prefetcher(params));
1825
5.27k
    if (_array_reader->is_nullable()) {
1826
3.99k
        RETURN_IF_ERROR(_null_iterator->init_prefetcher(params));
1827
3.99k
    }
1828
5.27k
    return Status::OK();
1829
5.27k
}
1830
1831
void ArrayFileColumnIterator::collect_prefetchers(
1832
        std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
1833
5.27k
        PrefetcherInitMethod init_method) {
1834
5.27k
    _offset_iterator->collect_prefetchers(prefetchers, init_method);
1835
    // the actual data pages to read of item column depends on the read result of offset column,
1836
    // so we can't init prefetch blocks according to rowids, just prefetch all data blocks here.
1837
5.27k
    _item_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS);
1838
5.27k
    if (_array_reader->is_nullable()) {
1839
3.99k
        _null_iterator->collect_prefetchers(prefetchers, init_method);
1840
3.99k
    }
1841
5.27k
}
1842
1843
Status ArrayFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count,
1844
32.6k
                                               MutableColumnPtr& dst) {
1845
32.6k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1846
0
        DLOG(INFO) << "Array column iterator column " << _column_name << " skip reading.";
1847
0
        dst->insert_many_defaults(count);
1848
0
        return Status::OK();
1849
0
    }
1850
1851
107k
    for (size_t i = 0; i < count; ++i) {
1852
        // TODO(cambyszju): now read array one by one, need optimize later
1853
74.6k
        RETURN_IF_ERROR(seek_to_ordinal(rowids[i]));
1854
74.6k
        size_t num_read = 1;
1855
74.6k
        RETURN_IF_ERROR(next_batch(&num_read, dst));
1856
74.6k
    }
1857
32.6k
    return Status::OK();
1858
32.6k
}
1859
1860
49.1k
void ArrayFileColumnIterator::set_need_to_read() {
1861
49.1k
    set_reading_flag(ReadingFlag::NEED_TO_READ);
1862
49.1k
    _item_iterator->set_need_to_read();
1863
49.1k
}
1864
1865
50.6k
void ArrayFileColumnIterator::remove_pruned_sub_iterators() {
1866
50.6k
    _item_iterator->remove_pruned_sub_iterators();
1867
50.6k
}
1868
1869
Status ArrayFileColumnIterator::set_access_paths(const TColumnAccessPaths& all_access_paths,
1870
49.7k
                                                 const TColumnAccessPaths& predicate_access_paths) {
1871
49.7k
    if (all_access_paths.empty()) {
1872
1.75k
        return Status::OK();
1873
1.75k
    }
1874
1875
47.9k
    if (!predicate_access_paths.empty()) {
1876
3.23k
        set_reading_flag(ReadingFlag::READING_FOR_PREDICATE);
1877
3.23k
        DLOG(INFO) << "Array column iterator set sub-column " << _column_name
1878
3.23k
                   << " to READING_FOR_PREDICATE";
1879
3.23k
    }
1880
1881
47.9k
    auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths));
1882
47.9k
    auto sub_predicate_access_paths = DORIS_TRY(_get_sub_access_paths(predicate_access_paths));
1883
1884
    // Check for meta-only modes (OFFSET_ONLY or NULL_MAP_ONLY)
1885
47.9k
    _check_and_set_meta_read_mode(sub_all_access_paths);
1886
47.9k
    if (read_offset_only()) {
1887
1.16k
        _item_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1888
1.16k
        DLOG(INFO) << "Array column iterator set column " << _column_name
1889
1.16k
                   << " to OFFSET_ONLY reading mode, item column set to SKIP_READING";
1890
1.16k
        return Status::OK();
1891
1.16k
    }
1892
46.7k
    if (read_null_map_only()) {
1893
12
        _item_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1894
12
        DLOG(INFO) << "Array column iterator set column " << _column_name
1895
12
                   << " to NULL_MAP_ONLY reading mode, item column set to SKIP_READING";
1896
12
        return Status::OK();
1897
12
    }
1898
1899
46.7k
    const auto no_sub_column_to_skip = sub_all_access_paths.empty();
1900
46.7k
    const auto no_predicate_sub_column = sub_predicate_access_paths.empty();
1901
1902
46.7k
    if (!no_sub_column_to_skip) {
1903
3.63k
        for (auto& path : sub_all_access_paths) {
1904
3.64k
            if (path.data_access_path.path[0] == ACCESS_ALL) {
1905
3.64k
                path.data_access_path.path[0] = _item_iterator->column_name();
1906
3.64k
            }
1907
3.63k
        }
1908
3.61k
    }
1909
1910
46.7k
    if (!no_predicate_sub_column) {
1911
279
        for (auto& path : sub_predicate_access_paths) {
1912
279
            if (path.data_access_path.path[0] == ACCESS_ALL) {
1913
218
                path.data_access_path.path[0] = _item_iterator->column_name();
1914
218
            }
1915
279
        }
1916
279
    }
1917
1918
46.7k
    if (!no_sub_column_to_skip || !no_predicate_sub_column) {
1919
3.72k
        _item_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ);
1920
3.72k
        RETURN_IF_ERROR(
1921
3.72k
                _item_iterator->set_access_paths(sub_all_access_paths, sub_predicate_access_paths));
1922
3.72k
    }
1923
46.7k
    return Status::OK();
1924
46.7k
}
1925
1926
////////////////////////////////////////////////////////////////////////////////
1927
// StringFileColumnIterator implementation
1928
////////////////////////////////////////////////////////////////////////////////
1929
1930
StringFileColumnIterator::StringFileColumnIterator(std::shared_ptr<ColumnReader> reader)
1931
16.3M
        : FileColumnIterator(std::move(reader)) {}
1932
1933
16.3M
Status StringFileColumnIterator::init(const ColumnIteratorOptions& opts) {
1934
16.3M
    if (read_offset_only()) {
1935
        // Propagate only_read_offsets to the FileColumnIterator's options
1936
143
        auto modified_opts = opts;
1937
143
        modified_opts.only_read_offsets = true;
1938
143
        return FileColumnIterator::init(modified_opts);
1939
143
    }
1940
16.3M
    return FileColumnIterator::init(opts);
1941
16.3M
}
1942
1943
Status StringFileColumnIterator::set_access_paths(
1944
        const TColumnAccessPaths& all_access_paths,
1945
4.81k
        const TColumnAccessPaths& predicate_access_paths) {
1946
4.81k
    if (all_access_paths.empty()) {
1947
3.80k
        return Status::OK();
1948
3.80k
    }
1949
1950
1.01k
    if (!predicate_access_paths.empty()) {
1951
192
        set_reading_flag(ReadingFlag::READING_FOR_PREDICATE);
1952
192
    }
1953
1954
    // Strip the column name from path[0] before checking for meta-only modes.
1955
    // Raw paths look like ["col_name", "OFFSET"] or ["col_name", "NULL"].
1956
1.01k
    auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths));
1957
1.01k
    _check_and_set_meta_read_mode(sub_all_access_paths);
1958
    // OFFSET_ONLY mode is fundamentally incompatible with CHAR columns:
1959
    // CHAR is stored padded to its declared length (see
1960
    // OlapColumnDataConvertorChar::clone_and_padding), so the per-row length
1961
    // recorded in dict word info / page headers is always the padded length
1962
    // (e.g. 25 for CHAR(25)) — never the logical length expected by length().
1963
    // Recovering the logical length requires scanning the chars buffer with
1964
    // strnlen() (shrink_padding_chars), which OFFSET_ONLY by definition skips.
1965
    // There is no partial-benefit path: any optimization that still produces
1966
    // the correct length() result must read the chars buffer in full.
1967
    //
1968
    // FE (NestedColumnPruning) already filters CHAR slots out of the
1969
    // OFFSET-only access plan, so reaching this branch means an FE/BE
1970
    // contract violation. Fail loudly instead of silently falling back.
1971
1.01k
    if (read_offset_only() && get_reader() != nullptr &&
1972
1.01k
        get_reader()->get_meta_type() == FieldType::OLAP_FIELD_TYPE_CHAR) {
1973
0
        return Status::InternalError(
1974
0
                "OFFSET_ONLY access path is not supported on CHAR column '{}': CHAR is stored "
1975
0
                "padded so the per-row length information available without reading the chars "
1976
0
                "buffer is always the padded length, not the logical length. The FE planner "
1977
0
                "must not emit an OFFSET access path for CHAR columns.",
1978
0
                _column_name);
1979
0
    }
1980
1.01k
    if (read_offset_only()) {
1981
144
        DLOG(INFO) << "String column iterator set column " << _column_name
1982
144
                   << " to OFFSET_ONLY reading mode";
1983
866
    } else if (read_null_map_only()) {
1984
207
        DLOG(INFO) << "String column iterator set column " << _column_name
1985
207
                   << " to NULL_MAP_ONLY reading mode";
1986
207
    }
1987
1988
1.01k
    return Status::OK();
1989
1.01k
}
1990
1991
////////////////////////////////////////////////////////////////////////////////
1992
1993
27.1M
FileColumnIterator::FileColumnIterator(std::shared_ptr<ColumnReader> reader) : _reader(reader) {}
1994
1995
55.0k
void ColumnIterator::_check_and_set_meta_read_mode(const TColumnAccessPaths& sub_all_access_paths) {
1996
55.0k
    for (const auto& path : sub_all_access_paths) {
1997
6.55k
        if (!path.data_access_path.path.empty()) {
1998
6.55k
            if (StringCaseEqual()(path.data_access_path.path[0], ACCESS_OFFSET)) {
1999
1.78k
                _read_mode = ReadMode::OFFSET_ONLY;
2000
1.78k
                return;
2001
4.77k
            } else if (StringCaseEqual()(path.data_access_path.path[0], ACCESS_NULL)) {
2002
235
                _read_mode = ReadMode::NULL_MAP_ONLY;
2003
235
                return;
2004
235
            }
2005
6.55k
        }
2006
6.53k
    }
2007
53.0k
    _read_mode = ReadMode::DEFAULT;
2008
53.0k
}
2009
2010
27.0M
Status FileColumnIterator::init(const ColumnIteratorOptions& opts) {
2011
27.0M
    if (_reading_flag == ReadingFlag::SKIP_READING) {
2012
2.39k
        DLOG(INFO) << "File column iterator column " << _column_name << " skip reading.";
2013
2.39k
        return Status::OK();
2014
2.39k
    }
2015
2016
27.0M
    _opts = opts;
2017
27.0M
    if (!_opts.use_page_cache) {
2018
26.2M
        _reader->disable_index_meta_cache();
2019
26.2M
    }
2020
27.0M
    RETURN_IF_ERROR(get_block_compression_codec(_reader->get_compression(), &_compress_codec));
2021
27.0M
    if (config::enable_low_cardinality_optimize &&
2022
27.0M
        opts.io_ctx.reader_type == ReaderType::READER_QUERY &&
2023
27.0M
        _reader->encoding_info()->encoding() == DICT_ENCODING) {
2024
16.2M
        auto dict_encoding_type = _reader->get_dict_encoding_type();
2025
        // Only if the column is a predicate column, then we need check the all dict encoding flag
2026
        // because we could rewrite the predciate to accelarate query speed. But if it is not a
2027
        // predicate column, then it is useless. And it has a bad impact on cold read(first time read)
2028
        // because it will load the column's ordinal index and zonemap index and maybe other indices.
2029
        // it has bad impact on primary key query. For example, select * from table where pk = 1, and
2030
        // the table has 2000 columns.
2031
16.2M
        if (dict_encoding_type == ColumnReader::UNKNOWN_DICT_ENCODING && opts.is_predicate_column) {
2032
7.96k
            RETURN_IF_ERROR(seek_to_ordinal(_reader->num_rows() - 1));
2033
7.96k
            _is_all_dict_encoding = _page.is_dict_encoding;
2034
7.96k
            _reader->set_dict_encoding_type(_is_all_dict_encoding
2035
7.96k
                                                    ? ColumnReader::ALL_DICT_ENCODING
2036
7.96k
                                                    : ColumnReader::PARTIAL_DICT_ENCODING);
2037
16.2M
        } else {
2038
16.2M
            _is_all_dict_encoding = dict_encoding_type == ColumnReader::ALL_DICT_ENCODING;
2039
16.2M
        }
2040
16.2M
    }
2041
27.0M
    return Status::OK();
2042
27.0M
}
2043
2044
27.1M
FileColumnIterator::~FileColumnIterator() = default;
2045
2046
327k
void FileColumnIterator::_trigger_prefetch_if_eligible(ordinal_t ord) {
2047
327k
    std::vector<BlockRange> ranges;
2048
327k
    if (_prefetcher->need_prefetch(cast_set<uint32_t>(ord), &ranges)) {
2049
293k
        for (const auto& range : ranges) {
2050
293k
            _cached_remote_file_reader->prefetch_range(range.offset, range.size, &_opts.io_ctx);
2051
293k
        }
2052
293k
    }
2053
327k
}
2054
2055
4.40M
Status FileColumnIterator::seek_to_ordinal(ordinal_t ord) {
2056
4.40M
    if (_reading_flag == ReadingFlag::SKIP_READING) {
2057
449
        DLOG(INFO) << "File column iterator column " << _column_name << " skip reading.";
2058
449
        return Status::OK();
2059
449
    }
2060
2061
18.4E
    LOG_IF(INFO, config::enable_segment_prefetch_verbose_log) << fmt::format(
2062
18.4E
            "[verbose] FileColumnIterator::seek_to_ordinal seek to ordinal {}, enable_prefetch={}",
2063
18.4E
            ord, _enable_prefetch);
2064
4.39M
    if (_enable_prefetch) {
2065
327k
        _trigger_prefetch_if_eligible(ord);
2066
327k
    }
2067
2068
    // if current page contains this row, we don't need to seek
2069
4.39M
    if (!_page || !_page.contains(ord) || !_page_iter.valid()) {
2070
1.43M
        RETURN_IF_ERROR(_reader->seek_at_or_before(ord, &_page_iter, _opts));
2071
1.43M
        RETURN_IF_ERROR(_read_data_page(_page_iter));
2072
1.43M
    }
2073
4.39M
    RETURN_IF_ERROR(_seek_to_pos_in_page(&_page, ord - _page.first_ordinal));
2074
4.39M
    _current_ordinal = ord;
2075
4.39M
    return Status::OK();
2076
4.39M
}
2077
2078
0
Status FileColumnIterator::seek_to_page_start() {
2079
0
    return seek_to_ordinal(_page.first_ordinal);
2080
0
}
2081
2082
4.45M
Status FileColumnIterator::_seek_to_pos_in_page(ParsedPage* page, ordinal_t offset_in_page) const {
2083
4.45M
    if (page->offset_in_page == offset_in_page) {
2084
        // fast path, do nothing
2085
1.73M
        return Status::OK();
2086
1.73M
    }
2087
2088
2.71M
    ordinal_t pos_in_data = offset_in_page;
2089
2.71M
    if (_page.has_null) {
2090
110k
        ordinal_t offset_in_data = 0;
2091
110k
        ordinal_t skips = offset_in_page;
2092
2093
110k
        if (offset_in_page > page->offset_in_page) {
2094
            // forward, reuse null bitmap
2095
62.7k
            skips = offset_in_page - page->offset_in_page;
2096
62.7k
            offset_in_data = page->data_decoder->current_index();
2097
62.7k
        } else {
2098
            // rewind null bitmap, and
2099
47.8k
            page->null_decoder = RleDecoder<bool>((const uint8_t*)page->null_bitmap.data,
2100
47.8k
                                                  cast_set<int>(page->null_bitmap.size), 1);
2101
47.8k
        }
2102
2103
110k
        auto skip_nulls = page->null_decoder.Skip(skips);
2104
110k
        pos_in_data = offset_in_data + skips - skip_nulls;
2105
110k
    }
2106
2107
2.71M
    RETURN_IF_ERROR(page->data_decoder->seek_to_position_in_page(pos_in_data));
2108
2.71M
    page->offset_in_page = offset_in_page;
2109
2.71M
    return Status::OK();
2110
2.71M
}
2111
2112
7.95k
Status FileColumnIterator::next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) {
2113
7.95k
    return _reader->next_batch_of_zone_map(n, dst);
2114
7.95k
}
2115
2116
3.64M
Status FileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
2117
3.64M
    if (read_null_map_only()) {
2118
2.95k
        DLOG(INFO) << "File column iterator column " << _column_name
2119
2.95k
                   << " in NULL_MAP_ONLY mode, reading only null map.";
2120
2.95k
        DORIS_CHECK(dst->is_nullable());
2121
2.95k
        auto& nullable_col = assert_cast<ColumnNullable&>(*dst);
2122
2.95k
        auto& null_map_data = nullable_col.get_null_map_data();
2123
2124
2.95k
        size_t remaining = *n;
2125
2.95k
        *has_null = false;
2126
5.96k
        while (remaining > 0) {
2127
3.00k
            if (!_page.has_remaining()) {
2128
51
                bool eos = false;
2129
51
                RETURN_IF_ERROR(_load_next_page(&eos));
2130
51
                if (eos) {
2131
0
                    break;
2132
0
                }
2133
51
            }
2134
2135
3.00k
            size_t nrows_in_page = std::min(remaining, _page.remaining());
2136
3.00k
            size_t nrows_to_read = nrows_in_page;
2137
3.00k
            if (_page.has_null) {
2138
211k
                while (nrows_to_read > 0) {
2139
209k
                    bool is_null = false;
2140
209k
                    size_t this_run = _page.null_decoder.GetNextRun(&is_null, nrows_to_read);
2141
209k
                    const size_t cur_size = null_map_data.size();
2142
209k
                    null_map_data.resize(cur_size + this_run);
2143
209k
                    memset(null_map_data.data() + cur_size, is_null ? 1 : 0, this_run);
2144
209k
                    if (is_null) {
2145
105k
                        *has_null = true;
2146
105k
                    }
2147
209k
                    nrows_to_read -= this_run;
2148
209k
                    _page.offset_in_page += this_run;
2149
209k
                    _current_ordinal += this_run;
2150
209k
                }
2151
2.93k
            } else {
2152
76
                const size_t cur_size = null_map_data.size();
2153
76
                null_map_data.resize(cur_size + nrows_to_read);
2154
76
                memset(null_map_data.data() + cur_size, 0, nrows_to_read);
2155
76
                _page.offset_in_page += nrows_to_read;
2156
76
                _current_ordinal += nrows_to_read;
2157
76
            }
2158
3.00k
            remaining -= nrows_in_page;
2159
3.00k
        }
2160
2.95k
        *n -= remaining;
2161
2.95k
        nullable_col.get_nested_column().insert_many_defaults(*n);
2162
2.95k
        return Status::OK();
2163
2.95k
    }
2164
2165
3.64M
    if (_reading_flag == ReadingFlag::SKIP_READING) {
2166
449
        DLOG(INFO) << "File column iterator column " << _column_name << " skip reading.";
2167
449
        dst->insert_many_defaults(*n);
2168
449
        return Status::OK();
2169
449
    }
2170
2171
3.64M
    size_t curr_size = dst->byte_size();
2172
3.64M
    dst->reserve(*n);
2173
3.64M
    size_t remaining = *n;
2174
3.64M
    *has_null = false;
2175
7.35M
    while (remaining > 0) {
2176
3.70M
        if (!_page.has_remaining()) {
2177
54.4k
            bool eos = false;
2178
54.4k
            RETURN_IF_ERROR(_load_next_page(&eos));
2179
54.4k
            if (eos) {
2180
0
                break;
2181
0
            }
2182
54.4k
        }
2183
2184
        // number of rows to be read from this page
2185
3.70M
        size_t nrows_in_page = std::min(remaining, _page.remaining());
2186
3.70M
        size_t nrows_to_read = nrows_in_page;
2187
3.70M
        if (_page.has_null) {
2188
1.15M
            while (nrows_to_read > 0) {
2189
917k
                bool is_null = false;
2190
917k
                size_t this_run = _page.null_decoder.GetNextRun(&is_null, nrows_to_read);
2191
                // we use num_rows only for CHECK
2192
917k
                size_t num_rows = this_run;
2193
917k
                if (!is_null) {
2194
475k
                    RETURN_IF_ERROR(_page.data_decoder->next_batch(&num_rows, dst));
2195
475k
                    DCHECK_EQ(this_run, num_rows);
2196
475k
                } else {
2197
441k
                    *has_null = true;
2198
441k
                    auto* null_col = check_and_get_column<ColumnNullable>(dst.get());
2199
442k
                    if (null_col != nullptr) {
2200
442k
                        null_col->insert_many_defaults(this_run);
2201
18.4E
                    } else {
2202
18.4E
                        return Status::InternalError("unexpected column type in column reader");
2203
18.4E
                    }
2204
441k
                }
2205
2206
917k
                nrows_to_read -= this_run;
2207
917k
                _page.offset_in_page += this_run;
2208
917k
                _current_ordinal += this_run;
2209
917k
            }
2210
3.47M
        } else {
2211
3.47M
            RETURN_IF_ERROR(_page.data_decoder->next_batch(&nrows_to_read, dst));
2212
3.47M
            DCHECK_EQ(nrows_to_read, nrows_in_page);
2213
2214
3.47M
            _page.offset_in_page += nrows_to_read;
2215
3.47M
            _current_ordinal += nrows_to_read;
2216
3.47M
        }
2217
3.70M
        remaining -= nrows_in_page;
2218
3.70M
    }
2219
3.64M
    *n -= remaining;
2220
3.64M
    _opts.stats->bytes_read += (dst->byte_size() - curr_size) + BitmapSize(*n);
2221
2222
#ifdef BE_TEST
2223
    _reader->check_data_by_zone_map_for_test(dst);
2224
#endif
2225
3.64M
    return Status::OK();
2226
3.64M
}
2227
2228
Status FileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count,
2229
703k
                                          MutableColumnPtr& dst) {
2230
703k
    if (read_null_map_only()) {
2231
16
        DLOG(INFO) << "File column iterator column " << _column_name
2232
16
                   << " in NULL_MAP_ONLY mode, reading only null map by rowids.";
2233
2234
16
        DORIS_CHECK(dst->is_nullable());
2235
16
        auto& nullable_col = assert_cast<ColumnNullable&>(*dst);
2236
16
        auto& null_map_data = nullable_col.get_null_map_data();
2237
16
        const size_t base_size = null_map_data.size();
2238
16
        null_map_data.resize(base_size + count);
2239
2240
16
        size_t remaining = count;
2241
16
        size_t total_read_count = 0;
2242
16
        size_t nrows_to_read = 0;
2243
32
        while (remaining > 0) {
2244
16
            RETURN_IF_ERROR(seek_to_ordinal(rowids[total_read_count]));
2245
2246
16
            nrows_to_read = std::min(remaining, _page.remaining());
2247
2248
16
            if (_page.has_null) {
2249
2
                size_t already_read = 0;
2250
4
                while ((nrows_to_read - already_read) > 0) {
2251
2
                    bool is_null = false;
2252
2
                    size_t this_run = std::min(nrows_to_read - already_read, _page.remaining());
2253
2
                    if (UNLIKELY(this_run == 0)) {
2254
0
                        break;
2255
0
                    }
2256
2
                    this_run = _page.null_decoder.GetNextRun(&is_null, this_run);
2257
2258
2
                    size_t offset = total_read_count + already_read;
2259
2
                    size_t this_read_count = 0;
2260
2
                    rowid_t current_ordinal_in_page =
2261
2
                            cast_set<uint32_t>(_page.offset_in_page + _page.first_ordinal);
2262
4
                    for (size_t i = 0; i < this_run; ++i) {
2263
2
                        if (rowids[offset + i] - current_ordinal_in_page >= this_run) {
2264
0
                            break;
2265
0
                        }
2266
2
                        this_read_count++;
2267
2
                    }
2268
2269
2
                    if (this_read_count > 0) {
2270
2
                        memset(null_map_data.data() + base_size + offset, is_null ? 1 : 0,
2271
2
                               this_read_count);
2272
2
                    }
2273
2274
2
                    already_read += this_read_count;
2275
2
                    _page.offset_in_page += this_run;
2276
2
                }
2277
2278
2
                nrows_to_read = already_read;
2279
2
                total_read_count += nrows_to_read;
2280
2
                remaining -= nrows_to_read;
2281
14
            } else {
2282
14
                memset(null_map_data.data() + base_size + total_read_count, 0, nrows_to_read);
2283
14
                total_read_count += nrows_to_read;
2284
14
                remaining -= nrows_to_read;
2285
14
            }
2286
16
        }
2287
2288
16
        null_map_data.resize(base_size + total_read_count);
2289
16
        nullable_col.get_nested_column().insert_many_defaults(total_read_count);
2290
16
        return Status::OK();
2291
16
    }
2292
2293
703k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
2294
0
        DLOG(INFO) << "File column iterator column " << _column_name << " skip reading.";
2295
0
        dst->insert_many_defaults(count);
2296
0
        return Status::OK();
2297
0
    }
2298
2299
703k
    size_t remaining = count;
2300
703k
    size_t total_read_count = 0;
2301
703k
    size_t nrows_to_read = 0;
2302
1.43M
    while (remaining > 0) {
2303
731k
        RETURN_IF_ERROR(seek_to_ordinal(rowids[total_read_count]));
2304
2305
        // number of rows to be read from this page
2306
731k
        nrows_to_read = std::min(remaining, _page.remaining());
2307
2308
731k
        if (_page.has_null) {
2309
52.3k
            size_t already_read = 0;
2310
456k
            while ((nrows_to_read - already_read) > 0) {
2311
404k
                bool is_null = false;
2312
404k
                size_t this_run = std::min(nrows_to_read - already_read, _page.remaining());
2313
404k
                if (UNLIKELY(this_run == 0)) {
2314
98
                    break;
2315
98
                }
2316
404k
                this_run = _page.null_decoder.GetNextRun(&is_null, this_run);
2317
404k
                size_t offset = total_read_count + already_read;
2318
404k
                size_t this_read_count = 0;
2319
404k
                rowid_t current_ordinal_in_page =
2320
404k
                        cast_set<uint32_t>(_page.offset_in_page + _page.first_ordinal);
2321
3.32M
                for (size_t i = 0; i < this_run; ++i) {
2322
3.25M
                    if (rowids[offset + i] - current_ordinal_in_page >= this_run) {
2323
337k
                        break;
2324
337k
                    }
2325
2.91M
                    this_read_count++;
2326
2.91M
                }
2327
2328
404k
                auto origin_index = _page.data_decoder->current_index();
2329
404k
                if (this_read_count > 0) {
2330
71.8k
                    if (is_null) {
2331
52.9k
                        auto* null_col = check_and_get_column<ColumnNullable>(dst.get());
2332
52.9k
                        if (UNLIKELY(null_col == nullptr)) {
2333
0
                            return Status::InternalError("unexpected column type in column reader");
2334
0
                        }
2335
2336
52.9k
                        null_col->insert_many_defaults(this_read_count);
2337
52.9k
                    } else {
2338
18.9k
                        size_t read_count = this_read_count;
2339
2340
                        // ordinal in nullable columns' data buffer maybe be not continuously(the data doesn't contain null value),
2341
                        // so we need use `page_start_off_in_decoder` to calculate the actual offset in `data_decoder`
2342
18.9k
                        size_t page_start_off_in_decoder =
2343
18.9k
                                _page.first_ordinal + _page.offset_in_page - origin_index;
2344
18.9k
                        RETURN_IF_ERROR(_page.data_decoder->read_by_rowids(
2345
18.9k
                                &rowids[offset], page_start_off_in_decoder, &read_count, dst));
2346
18.9k
                        DCHECK_EQ(read_count, this_read_count);
2347
18.9k
                    }
2348
71.8k
                }
2349
2350
404k
                if (!is_null) {
2351
350k
                    RETURN_IF_ERROR(
2352
350k
                            _page.data_decoder->seek_to_position_in_page(origin_index + this_run));
2353
350k
                }
2354
2355
404k
                already_read += this_read_count;
2356
404k
                _page.offset_in_page += this_run;
2357
404k
                DCHECK(_page.offset_in_page <= _page.num_rows);
2358
404k
            }
2359
2360
52.3k
            nrows_to_read = already_read;
2361
52.3k
            total_read_count += nrows_to_read;
2362
52.3k
            remaining -= nrows_to_read;
2363
679k
        } else {
2364
679k
            RETURN_IF_ERROR(_page.data_decoder->read_by_rowids(
2365
679k
                    &rowids[total_read_count], _page.first_ordinal, &nrows_to_read, dst));
2366
679k
            total_read_count += nrows_to_read;
2367
679k
            remaining -= nrows_to_read;
2368
679k
        }
2369
731k
    }
2370
703k
    return Status::OK();
2371
703k
}
2372
2373
54.4k
Status FileColumnIterator::_load_next_page(bool* eos) {
2374
54.4k
    _page_iter.next();
2375
54.4k
    if (!_page_iter.valid()) {
2376
0
        *eos = true;
2377
0
        return Status::OK();
2378
0
    }
2379
2380
54.4k
    RETURN_IF_ERROR(_read_data_page(_page_iter));
2381
54.4k
    RETURN_IF_ERROR(_seek_to_pos_in_page(&_page, 0));
2382
54.4k
    *eos = false;
2383
54.4k
    return Status::OK();
2384
54.4k
}
2385
2386
1.48M
Status FileColumnIterator::_read_data_page(const OrdinalPageIndexIterator& iter) {
2387
1.48M
    PageHandle handle;
2388
1.48M
    Slice page_body;
2389
1.48M
    PageFooterPB footer;
2390
1.48M
    _opts.type = DATA_PAGE;
2391
1.48M
    PageDecoderOptions decoder_opts;
2392
1.48M
    decoder_opts.only_read_offsets = _opts.only_read_offsets;
2393
1.48M
    RETURN_IF_ERROR(
2394
1.48M
            _reader->read_page(_opts, iter.page(), &handle, &page_body, &footer, _compress_codec));
2395
    // parse data page
2396
1.48M
    auto st = ParsedPage::create(std::move(handle), page_body, footer.data_page_footer(),
2397
1.48M
                                 _reader->encoding_info(), iter.page(), iter.page_index(), &_page,
2398
1.48M
                                 decoder_opts);
2399
1.48M
    if (!st.ok()) {
2400
0
        LOG(WARNING) << "failed to create ParsedPage, file=" << _opts.file_reader->path().native()
2401
0
                     << ", page_offset=" << iter.page().offset << ", page_size=" << iter.page().size
2402
0
                     << ", page_index=" << iter.page_index() << ", error=" << st;
2403
0
        return st;
2404
0
    }
2405
2406
    // dictionary page is read when the first data page that uses it is read,
2407
    // this is to optimize the memory usage: when there is no query on one column, we could
2408
    // release the memory of dictionary page.
2409
    // note that concurrent iterators for the same column won't repeatedly read dictionary page
2410
    // because of page cache.
2411
1.48M
    if (_reader->encoding_info()->encoding() == DICT_ENCODING) {
2412
399k
        auto dict_page_decoder = reinterpret_cast<BinaryDictPageDecoder*>(_page.data_decoder.get());
2413
399k
        if (dict_page_decoder->is_dict_encoding()) {
2414
369k
            if (_dict_decoder == nullptr) {
2415
358k
                RETURN_IF_ERROR(_read_dict_data());
2416
358k
                CHECK_NOTNULL(_dict_decoder);
2417
358k
            }
2418
2419
369k
            dict_page_decoder->set_dict_decoder(cast_set<uint32_t>(_dict_decoder->count()),
2420
369k
                                                _dict_word_info.get());
2421
369k
        }
2422
399k
    }
2423
1.48M
    return Status::OK();
2424
1.48M
}
2425
2426
362k
Status FileColumnIterator::_read_dict_data() {
2427
362k
    CHECK_EQ(_reader->encoding_info()->encoding(), DICT_ENCODING);
2428
    // read dictionary page
2429
362k
    Slice dict_data;
2430
362k
    PageFooterPB dict_footer;
2431
362k
    _opts.type = INDEX_PAGE;
2432
2433
362k
    RETURN_IF_ERROR(_reader->read_page(_opts, _reader->get_dict_page_pointer(), &_dict_page_handle,
2434
362k
                                       &dict_data, &dict_footer, _compress_codec, true));
2435
362k
    const EncodingInfo* encoding_info;
2436
362k
    RETURN_IF_ERROR(EncodingInfo::get(FieldType::OLAP_FIELD_TYPE_VARCHAR,
2437
362k
                                      dict_footer.dict_page_footer().encoding(), {},
2438
362k
                                      &encoding_info));
2439
362k
    RETURN_IF_ERROR(encoding_info->create_page_decoder(dict_data, {}, _dict_decoder));
2440
362k
    RETURN_IF_ERROR(_dict_decoder->init());
2441
2442
362k
    _dict_word_info.reset(new StringRef[_dict_decoder->count()]);
2443
362k
    RETURN_IF_ERROR(_dict_decoder->get_dict_word_info(_dict_word_info.get()));
2444
362k
    return Status::OK();
2445
362k
}
2446
2447
Status FileColumnIterator::get_row_ranges_by_zone_map(
2448
        const AndBlockColumnPredicate* col_predicates,
2449
        const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates,
2450
82.2k
        RowRanges* row_ranges) {
2451
82.2k
    if (_reader->has_zone_map()) {
2452
82.2k
        RETURN_IF_ERROR(_reader->get_row_ranges_by_zone_map(col_predicates, delete_predicates,
2453
82.2k
                                                            row_ranges, _opts));
2454
82.2k
    }
2455
82.2k
    return Status::OK();
2456
82.2k
}
2457
2458
Status FileColumnIterator::get_row_ranges_by_bloom_filter(
2459
82.7k
        const AndBlockColumnPredicate* col_predicates, RowRanges* row_ranges) {
2460
82.7k
    if ((col_predicates->can_do_bloom_filter(false) && _reader->has_bloom_filter_index(false)) ||
2461
82.7k
        (col_predicates->can_do_bloom_filter(true) && _reader->has_bloom_filter_index(true))) {
2462
97
        RETURN_IF_ERROR(_reader->get_row_ranges_by_bloom_filter(col_predicates, row_ranges, _opts));
2463
97
    }
2464
82.7k
    return Status::OK();
2465
82.7k
}
2466
2467
Status FileColumnIterator::get_row_ranges_by_dict(const AndBlockColumnPredicate* col_predicates,
2468
84.8k
                                                  RowRanges* row_ranges) {
2469
84.8k
    if (!_is_all_dict_encoding) {
2470
74.1k
        return Status::OK();
2471
74.1k
    }
2472
2473
10.6k
    if (!_dict_decoder) {
2474
4.20k
        RETURN_IF_ERROR(_read_dict_data());
2475
4.20k
        CHECK_NOTNULL(_dict_decoder);
2476
4.20k
    }
2477
2478
10.6k
    if (!col_predicates->evaluate_and(_dict_word_info.get(), _dict_decoder->count())) {
2479
1.13k
        row_ranges->clear();
2480
1.13k
    }
2481
10.6k
    return Status::OK();
2482
10.6k
}
2483
2484
293k
Status FileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) {
2485
293k
    if (_cached_remote_file_reader =
2486
293k
                std::dynamic_pointer_cast<io::CachedRemoteFileReader>(_reader->_file_reader);
2487
293k
        !_cached_remote_file_reader) {
2488
0
        return Status::OK();
2489
0
    }
2490
293k
    _enable_prefetch = true;
2491
293k
    _prefetcher = std::make_unique<SegmentPrefetcher>(params.config);
2492
293k
    RETURN_IF_ERROR(_prefetcher->init(_reader, params.read_options));
2493
293k
    return Status::OK();
2494
293k
}
2495
2496
void FileColumnIterator::collect_prefetchers(
2497
        std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
2498
293k
        PrefetcherInitMethod init_method) {
2499
293k
    if (_prefetcher) {
2500
293k
        prefetchers[init_method].emplace_back(_prefetcher.get());
2501
293k
    }
2502
293k
}
2503
2504
20.6k
Status DefaultValueColumnIterator::init(const ColumnIteratorOptions& opts) {
2505
20.6k
    _opts = opts;
2506
    // be consistent with segment v1
2507
    // if _has_default_value, we should create default column iterator for this column, and
2508
    // "NULL" is a special default value which means the default value is null.
2509
20.6k
    if (_has_default_value) {
2510
1.77k
        if (_default_value == "NULL") {
2511
923
            _default_value_field = Field::create_field<TYPE_NULL>(Null {});
2512
923
        } else {
2513
855
            if (_type == FieldType::OLAP_FIELD_TYPE_ARRAY) {
2514
8
                if (_default_value != "[]") {
2515
0
                    return Status::NotSupported("Array default {} is unsupported", _default_value);
2516
8
                } else {
2517
8
                    _default_value_field = Field::create_field<TYPE_ARRAY>(Array {});
2518
8
                    return Status::OK();
2519
8
                }
2520
847
            } else if (_type == FieldType::OLAP_FIELD_TYPE_STRUCT) {
2521
0
                return Status::NotSupported("STRUCT default type is unsupported");
2522
847
            } else if (_type == FieldType::OLAP_FIELD_TYPE_MAP) {
2523
0
                return Status::NotSupported("MAP default type is unsupported");
2524
0
            }
2525
847
            const auto t = _type;
2526
847
            const auto serde = DataTypeFactory::instance()
2527
847
                                       .create_data_type(t, _precision, _scale, _len)
2528
847
                                       ->get_serde();
2529
847
            RETURN_IF_ERROR(serde->from_fe_string(_default_value, _default_value_field));
2530
847
        }
2531
18.8k
    } else if (_is_nullable) {
2532
18.8k
        _default_value_field = Field::create_field<TYPE_NULL>(Null {});
2533
18.4E
    } else {
2534
18.4E
        return Status::InternalError(
2535
18.4E
                "invalid default value column for no default value and not nullable");
2536
18.4E
    }
2537
20.6k
    return Status::OK();
2538
20.6k
}
2539
2540
2.13k
Status DefaultValueColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
2541
2.13k
    *has_null = _default_value_field.is_null();
2542
2.13k
    _insert_many_default(dst, *n);
2543
2.13k
    return Status::OK();
2544
2.13k
}
2545
2546
Status DefaultValueColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count,
2547
5.79k
                                                  MutableColumnPtr& dst) {
2548
5.79k
    _insert_many_default(dst, count);
2549
5.79k
    return Status::OK();
2550
5.79k
}
2551
2552
7.92k
void DefaultValueColumnIterator::_insert_many_default(MutableColumnPtr& dst, size_t n) {
2553
7.92k
    if (_default_value_field.is_null()) {
2554
7.46k
        dst->insert_many_defaults(n);
2555
7.46k
    } else {
2556
463
        dst = dst->convert_to_predicate_column_if_dictionary();
2557
463
        dst->insert_duplicate_fields(_default_value_field, n);
2558
463
    }
2559
7.92k
}
2560
2561
3.38k
Status RowIdColumnIteratorV2::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
2562
3.38k
    auto* string_column = assert_cast<ColumnString*, TypeCheckOnRelease::DISABLE>(dst.get());
2563
2564
11.1M
    for (uint32_t i = 0; i < *n; ++i) {
2565
11.1M
        uint32_t row_id = _current_rowid + i;
2566
11.1M
        GlobalRowLoacationV2 location(_version, _backend_id, _file_id, row_id);
2567
11.1M
        string_column->insert_data(reinterpret_cast<const char*>(&location),
2568
11.1M
                                   sizeof(GlobalRowLoacationV2));
2569
11.1M
    }
2570
3.38k
    _current_rowid += *n;
2571
3.38k
    return Status::OK();
2572
3.38k
}
2573
2574
Status RowIdColumnIteratorV2::read_by_rowids(const rowid_t* rowids, const size_t count,
2575
42.3k
                                             MutableColumnPtr& dst) {
2576
42.3k
    auto* string_column = assert_cast<ColumnString*>(dst.get());
2577
2578
31.9M
    for (size_t i = 0; i < count; ++i) {
2579
31.9M
        uint32_t row_id = rowids[i];
2580
31.9M
        GlobalRowLoacationV2 location(_version, _backend_id, _file_id, row_id);
2581
31.9M
        string_column->insert_data(reinterpret_cast<const char*>(&location),
2582
31.9M
                                   sizeof(GlobalRowLoacationV2));
2583
31.9M
    }
2584
42.3k
    return Status::OK();
2585
42.3k
}
2586
2587
} // namespace doris::segment_v2