Coverage Report

Created: 2026-06-01 21:29

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