Coverage Report

Created: 2026-05-12 20:22

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