Coverage Report

Created: 2026-04-09 13:35

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