Coverage Report

Created: 2026-05-26 16:41

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