Coverage Report

Created: 2026-05-28 11:44

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