Coverage Report

Created: 2026-05-25 13:29

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