Coverage Report

Created: 2026-05-14 15:22

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