Coverage Report

Created: 2026-04-14 07:58

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