Coverage Report

Created: 2026-04-09 13:36

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
859
inline bool read_as_string(PrimitiveType type) {
88
859
    return type == PrimitiveType::TYPE_STRING || type == PrimitiveType::INVALID_TYPE ||
89
859
           type == PrimitiveType::TYPE_BITMAP || type == PrimitiveType::TYPE_FIXED_LENGTH_OBJECT;
90
859
}
91
92
Status ColumnReader::create_array(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
93
                                  const io::FileReaderSPtr& file_reader,
94
67.8k
                                  std::shared_ptr<ColumnReader>* reader) {
95
67.8k
    DCHECK(meta.children_columns_size() == 2 || meta.children_columns_size() == 3);
96
97
67.8k
    std::shared_ptr<ColumnReader> item_reader;
98
67.8k
    RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(0),
99
67.8k
                                         meta.children_columns(0).num_rows(), file_reader,
100
67.8k
                                         &item_reader));
101
102
67.8k
    std::shared_ptr<ColumnReader> offset_reader;
103
67.8k
    RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(1),
104
67.8k
                                         meta.children_columns(1).num_rows(), file_reader,
105
67.8k
                                         &offset_reader));
106
107
67.8k
    std::shared_ptr<ColumnReader> null_reader;
108
67.8k
    if (meta.is_nullable()) {
109
49.1k
        RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(2),
110
49.1k
                                             meta.children_columns(2).num_rows(), file_reader,
111
49.1k
                                             &null_reader));
112
49.1k
    }
113
114
    // The num rows of the array reader equals to the num rows of the length reader.
115
67.8k
    uint64_t array_num_rows = meta.children_columns(1).num_rows();
116
67.8k
    std::shared_ptr<ColumnReader> array_reader(
117
67.8k
            new ColumnReader(opts, meta, array_num_rows, file_reader));
118
    //  array reader do not need to init
119
67.8k
    array_reader->_sub_readers.resize(meta.children_columns_size());
120
67.8k
    array_reader->_sub_readers[0] = std::move(item_reader);
121
67.8k
    array_reader->_sub_readers[1] = std::move(offset_reader);
122
67.8k
    if (meta.is_nullable()) {
123
48.9k
        array_reader->_sub_readers[2] = std::move(null_reader);
124
48.9k
    }
125
67.8k
    array_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_ARRAY;
126
67.8k
    *reader = std::move(array_reader);
127
67.8k
    return Status::OK();
128
67.8k
}
129
130
Status ColumnReader::create_map(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
131
                                const io::FileReaderSPtr& file_reader,
132
76.3k
                                std::shared_ptr<ColumnReader>* reader) {
133
    // map reader now has 3 sub readers for key, value, offsets(scalar), null(scala)
134
76.3k
    DCHECK(meta.children_columns_size() == 3 || meta.children_columns_size() == 4);
135
76.3k
    std::shared_ptr<ColumnReader> key_reader;
136
76.3k
    RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(0),
137
76.3k
                                         meta.children_columns(0).num_rows(), file_reader,
138
76.3k
                                         &key_reader));
139
76.3k
    std::shared_ptr<ColumnReader> val_reader;
140
76.3k
    RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(1),
141
76.3k
                                         meta.children_columns(1).num_rows(), file_reader,
142
76.3k
                                         &val_reader));
143
76.3k
    std::shared_ptr<ColumnReader> offset_reader;
144
76.3k
    RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(2),
145
76.3k
                                         meta.children_columns(2).num_rows(), file_reader,
146
76.3k
                                         &offset_reader));
147
76.3k
    std::shared_ptr<ColumnReader> null_reader;
148
76.3k
    if (meta.is_nullable()) {
149
15.2k
        RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(3),
150
15.2k
                                             meta.children_columns(3).num_rows(), file_reader,
151
15.2k
                                             &null_reader));
152
15.2k
    }
153
154
    // The num rows of the map reader equals to the num rows of the length reader.
155
76.3k
    uint64_t map_num_rows = meta.children_columns(2).num_rows();
156
76.3k
    std::shared_ptr<ColumnReader> map_reader(
157
76.3k
            new ColumnReader(opts, meta, map_num_rows, file_reader));
158
76.3k
    map_reader->_sub_readers.resize(meta.children_columns_size());
159
160
76.3k
    map_reader->_sub_readers[0] = std::move(key_reader);
161
76.3k
    map_reader->_sub_readers[1] = std::move(val_reader);
162
76.3k
    map_reader->_sub_readers[2] = std::move(offset_reader);
163
76.3k
    if (meta.is_nullable()) {
164
15.2k
        map_reader->_sub_readers[3] = std::move(null_reader);
165
15.2k
    }
166
76.3k
    map_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_MAP;
167
76.3k
    *reader = std::move(map_reader);
168
76.3k
    return Status::OK();
169
76.3k
}
170
171
Status ColumnReader::create_struct(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
172
                                   uint64_t num_rows, const io::FileReaderSPtr& file_reader,
173
7.73k
                                   std::shared_ptr<ColumnReader>* reader) {
174
    // not support empty struct
175
7.73k
    DCHECK(meta.children_columns_size() >= 1);
176
    // create struct column reader
177
7.73k
    std::shared_ptr<ColumnReader> struct_reader(
178
7.73k
            new ColumnReader(opts, meta, num_rows, file_reader));
179
7.73k
    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
40.8k
    for (int i = 0; i < meta.children_columns_size(); i++) {
182
33.1k
        std::shared_ptr<ColumnReader> sub_reader;
183
33.1k
        RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(i),
184
33.1k
                                             meta.children_columns(i).num_rows(), file_reader,
185
33.1k
                                             &sub_reader));
186
33.1k
        struct_reader->_sub_readers.push_back(std::move(sub_reader));
187
33.1k
    }
188
7.73k
    struct_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_STRUCT;
189
7.73k
    *reader = std::move(struct_reader);
190
7.73k
    return Status::OK();
191
7.73k
}
192
193
Status ColumnReader::create_agg_state(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
194
                                      uint64_t num_rows, const io::FileReaderSPtr& file_reader,
195
854
                                      std::shared_ptr<ColumnReader>* reader) {
196
854
    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
854
    auto data_type = DataTypeFactory::instance().create_data_type(meta);
205
854
    const auto* agg_state_type = assert_cast<const DataTypeAggState*>(data_type.get());
206
854
    agg_state_type->check_function_compatibility(opts.be_exec_version);
207
854
    auto type = agg_state_type->get_serialized_type()->get_primitive_type();
208
209
854
    if (read_as_string(type)) {
210
797
        std::shared_ptr<ColumnReader> reader_local(
211
797
                new ColumnReader(opts, meta, num_rows, file_reader));
212
797
        RETURN_IF_ERROR(reader_local->init(&meta));
213
797
        *reader = std::move(reader_local);
214
797
        return Status::OK();
215
797
    } else if (type == PrimitiveType::TYPE_MAP) {
216
48
        return create_map(opts, meta, file_reader, reader);
217
48
    } else if (type == PrimitiveType::TYPE_ARRAY) {
218
13
        return create_array(opts, meta, file_reader, reader);
219
18.4E
    } else if (type == PrimitiveType::TYPE_STRUCT) {
220
0
        return create_struct(opts, meta, num_rows, file_reader, reader);
221
0
    }
222
223
18.4E
    return Status::InternalError("Not supported type: {}, serialized type: {}",
224
18.4E
                                 agg_state_type->get_name(), int(type));
225
854
}
226
227
133k
bool ColumnReader::is_compaction_reader_type(ReaderType type) {
228
133k
    return type == ReaderType::READER_BASE_COMPACTION ||
229
133k
           type == ReaderType::READER_CUMULATIVE_COMPACTION ||
230
133k
           type == ReaderType::READER_COLD_DATA_COMPACTION ||
231
133k
           type == ReaderType::READER_SEGMENT_COMPACTION ||
232
133k
           type == ReaderType::READER_FULL_COMPACTION;
233
133k
}
234
235
Status ColumnReader::create(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
236
                            uint64_t num_rows, const io::FileReaderSPtr& file_reader,
237
20.8M
                            std::shared_ptr<ColumnReader>* reader) {
238
20.8M
    if (is_scalar_type((FieldType)meta.type())) {
239
20.7M
        std::shared_ptr<ColumnReader> reader_local(
240
20.7M
                new ColumnReader(opts, meta, num_rows, file_reader));
241
20.7M
        RETURN_IF_ERROR(reader_local->init(&meta));
242
20.7M
        *reader = std::move(reader_local);
243
20.7M
        return Status::OK();
244
20.7M
    } else {
245
177k
        auto type = (FieldType)meta.type();
246
177k
        switch (type) {
247
859
        case FieldType::OLAP_FIELD_TYPE_AGG_STATE: {
248
859
            return create_agg_state(opts, meta, num_rows, file_reader, reader);
249
0
        }
250
7.73k
        case FieldType::OLAP_FIELD_TYPE_STRUCT: {
251
7.73k
            return create_struct(opts, meta, num_rows, file_reader, reader);
252
0
        }
253
67.9k
        case FieldType::OLAP_FIELD_TYPE_ARRAY: {
254
67.9k
            return create_array(opts, meta, file_reader, reader);
255
0
        }
256
76.2k
        case FieldType::OLAP_FIELD_TYPE_MAP: {
257
76.2k
            return create_map(opts, meta, file_reader, reader);
258
0
        }
259
22.6k
        case FieldType::OLAP_FIELD_TYPE_VARIANT: {
260
            // Read variant only root data using a single ColumnReader
261
22.6k
            std::shared_ptr<ColumnReader> reader_local(
262
22.6k
                    new ColumnReader(opts, meta, num_rows, file_reader));
263
22.6k
            RETURN_IF_ERROR(reader_local->init(&meta));
264
22.6k
            *reader = std::move(reader_local);
265
22.6k
            return Status::OK();
266
22.6k
        }
267
0
        default:
268
0
            return Status::NotSupported("unsupported type for ColumnReader: {}",
269
0
                                        std::to_string(int(type)));
270
177k
        }
271
177k
    }
272
20.8M
}
273
274
22.6k
ColumnReader::ColumnReader() = default;
275
276
ColumnReader::ColumnReader(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
277
                           uint64_t num_rows, io::FileReaderSPtr file_reader)
278
20.8M
        : _use_index_page_cache(!config::disable_storage_page_cache),
279
20.8M
          _opts(opts),
280
20.8M
          _num_rows(num_rows),
281
20.8M
          _file_reader(std::move(file_reader)),
282
20.8M
          _dict_encoding_type(UNKNOWN_DICT_ENCODING) {
283
20.8M
    _meta_length = meta.length();
284
20.8M
    _meta_type = (FieldType)meta.type();
285
20.8M
    if (_meta_type == FieldType::OLAP_FIELD_TYPE_ARRAY) {
286
67.9k
        _meta_children_column_type = (FieldType)meta.children_columns(0).type();
287
67.9k
    }
288
20.8M
    _data_type = DataTypeFactory::instance().create_data_type(meta);
289
20.8M
    _meta_is_nullable = meta.is_nullable();
290
20.8M
    _meta_dict_page = meta.dict_page();
291
20.8M
    _meta_compression = meta.compression();
292
20.8M
}
293
294
20.9M
ColumnReader::~ColumnReader() = default;
295
296
20.7M
int64_t ColumnReader::get_metadata_size() const {
297
20.7M
    return sizeof(ColumnReader) + (_segment_zone_map ? _segment_zone_map->ByteSizeLong() : 0);
298
20.7M
}
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
20.7M
Status ColumnReader::init(const ColumnMetaPB* meta) {
350
20.7M
    _type_info = get_type_info(meta);
351
352
20.7M
    if (meta->has_be_exec_version()) {
353
20.4M
        _be_exec_version = meta->be_exec_version();
354
20.4M
    }
355
356
20.7M
    if (_type_info == nullptr) {
357
0
        return Status::NotSupported("unsupported typeinfo, type={}", meta->type());
358
0
    }
359
20.7M
    RETURN_IF_ERROR(EncodingInfo::get(_type_info->type(), meta->encoding(), {}, &_encoding_info));
360
361
61.4M
    for (int i = 0; i < meta->indexes_size(); i++) {
362
40.7M
        const auto& index_meta = meta->indexes(i);
363
40.7M
        switch (index_meta.type()) {
364
0
        case BITMAP_INDEX:
365
0
            break;
366
20.6M
        case ORDINAL_INDEX:
367
20.6M
            _ordinal_index.reset(
368
20.6M
                    new OrdinalIndexReader(_file_reader, _num_rows, index_meta.ordinal_index()));
369
20.6M
            break;
370
20.1M
        case ZONE_MAP_INDEX:
371
20.1M
            _segment_zone_map =
372
20.1M
                    std::make_unique<ZoneMapPB>(index_meta.zone_map_index().segment_zone_map());
373
20.1M
            _zone_map_index.reset(new ZoneMapIndexReader(
374
20.1M
                    _file_reader, index_meta.zone_map_index().page_zone_maps()));
375
20.1M
            break;
376
5.08k
        case BLOOM_FILTER_INDEX:
377
5.08k
            _bloom_filter_index.reset(
378
5.08k
                    new BloomFilterIndexReader(_file_reader, index_meta.bloom_filter_index()));
379
5.08k
            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
40.7M
        }
386
40.7M
    }
387
20.6M
    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
20.6M
    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
20.6M
    return Status::OK();
397
20.6M
}
398
399
Status ColumnReader::new_index_iterator(const std::shared_ptr<IndexFileReader>& index_file_reader,
400
                                        const TabletIndex* index_meta,
401
80.4k
                                        std::unique_ptr<IndexIterator>* iterator) {
402
80.4k
    RETURN_IF_ERROR(_load_index(index_file_reader, index_meta));
403
80.4k
    {
404
80.4k
        std::shared_lock<std::shared_mutex> rlock(_load_index_lock);
405
80.4k
        auto iter = _index_readers.find(index_meta->index_id());
406
80.5k
        if (iter != _index_readers.end()) {
407
80.5k
            if (iter->second != nullptr) {
408
80.5k
                RETURN_IF_ERROR(iter->second->new_iterator(iterator));
409
80.5k
            }
410
80.5k
        }
411
80.4k
    }
412
80.4k
    return Status::OK();
413
80.4k
}
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
84.3k
        RowRanges* row_ranges, const ColumnIteratorOptions& iter_opts) {
439
84.3k
    std::vector<uint32_t> page_indexes;
440
84.3k
    RETURN_IF_ERROR(
441
84.3k
            _get_filtered_pages(col_predicates, delete_predicates, &page_indexes, iter_opts));
442
84.3k
    RETURN_IF_ERROR(_calculate_row_ranges(page_indexes, row_ranges, iter_opts));
443
84.3k
    return Status::OK();
444
84.3k
}
445
446
7.44k
Status ColumnReader::next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) const {
447
7.44k
    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
7.44k
    ZoneMap zone_map;
452
7.44k
    RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map));
453
454
7.44k
    dst->reserve(*n);
455
7.44k
    if (!zone_map.has_not_null) {
456
899
        assert_cast<ColumnNullable&>(*dst).insert_many_defaults(*n);
457
899
        return Status::OK();
458
899
    }
459
6.54k
    dst->insert(zone_map.max_value);
460
13.0k
    for (int i = 1; i < *n; ++i) {
461
6.55k
        dst->insert(zone_map.min_value);
462
6.55k
    }
463
6.54k
    return Status::OK();
464
7.44k
}
465
466
Status ColumnReader::match_condition(const AndBlockColumnPredicate* col_predicates,
467
1.76M
                                     bool* matched) const {
468
1.76M
    *matched = true;
469
1.76M
    if (_zone_map_index == nullptr) {
470
0
        return Status::OK();
471
0
    }
472
1.76M
    ZoneMap zone_map;
473
1.76M
    RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map));
474
475
1.76M
    *matched = _zone_map_match_condition(zone_map, col_predicates);
476
1.76M
    return Status::OK();
477
1.76M
}
478
479
Status ColumnReader::prune_predicates_by_zone_map(
480
        std::vector<std::shared_ptr<ColumnPredicate>>& predicates, const int column_id,
481
8.55M
        bool* pruned) const {
482
8.55M
    *pruned = false;
483
8.55M
    if (_zone_map_index == nullptr) {
484
340
        return Status::OK();
485
340
    }
486
487
8.55M
    ZoneMap zone_map;
488
8.55M
    RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map));
489
8.55M
    if (zone_map.pass_all) {
490
0
        return Status::OK();
491
0
    }
492
493
17.2M
    for (auto it = predicates.begin(); it != predicates.end();) {
494
8.71M
        auto predicate = *it;
495
8.71M
        if (predicate->column_id() == column_id && predicate->is_always_true(zone_map)) {
496
12.4k
            *pruned = true;
497
12.4k
            it = predicates.erase(it);
498
8.70M
        } else {
499
8.70M
            ++it;
500
8.70M
        }
501
8.71M
    }
502
8.55M
    return Status::OK();
503
8.55M
}
504
505
bool ColumnReader::_zone_map_match_condition(const ZoneMap& zone_map,
506
1.87M
                                             const AndBlockColumnPredicate* col_predicates) const {
507
1.87M
    if (zone_map.pass_all) {
508
0
        return true;
509
0
    }
510
511
1.87M
    return col_predicates->evaluate_and(zone_map);
512
1.87M
}
513
514
Status ColumnReader::_get_filtered_pages(
515
        const AndBlockColumnPredicate* col_predicates,
516
        const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates,
517
84.3k
        std::vector<uint32_t>* page_indexes, const ColumnIteratorOptions& iter_opts) {
518
84.3k
    RETURN_IF_ERROR(_load_zone_map_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts));
519
520
84.3k
    const std::vector<ZoneMapPB>& zone_maps = _zone_map_index->page_zone_maps();
521
84.3k
    size_t page_size = _zone_map_index->num_pages();
522
239k
    for (size_t i = 0; i < page_size; ++i) {
523
154k
        if (zone_maps[i].pass_all()) {
524
46.5k
            page_indexes->push_back(cast_set<uint32_t>(i));
525
108k
        } else {
526
108k
            segment_v2::ZoneMap zone_map;
527
108k
            RETURN_IF_ERROR(ZoneMap::from_proto(zone_maps[i], _data_type, zone_map));
528
108k
            if (_zone_map_match_condition(zone_map, col_predicates)) {
529
107k
                bool should_read = true;
530
107k
                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
107k
                if (should_read) {
541
107k
                    page_indexes->push_back(cast_set<uint32_t>(i));
542
107k
                }
543
107k
            }
544
108k
        }
545
154k
    }
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
84.3k
    return Status::OK();
551
84.3k
}
552
553
Status ColumnReader::_calculate_row_ranges(const std::vector<uint32_t>& page_indexes,
554
                                           RowRanges* row_ranges,
555
84.7k
                                           const ColumnIteratorOptions& iter_opts) {
556
84.7k
    row_ranges->clear();
557
84.7k
    RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts));
558
154k
    for (auto i : page_indexes) {
559
154k
        ordinal_t page_first_id = _ordinal_index->get_first_ordinal(i);
560
154k
        ordinal_t page_last_id = _ordinal_index->get_last_ordinal(i);
561
154k
        RowRanges page_row_ranges(RowRanges::create_single(page_first_id, page_last_id + 1));
562
154k
        RowRanges::ranges_union(*row_ranges, page_row_ranges, row_ranges);
563
154k
    }
564
84.7k
    return Status::OK();
565
84.7k
}
566
567
Status ColumnReader::get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates,
568
                                                    RowRanges* row_ranges,
569
96
                                                    const ColumnIteratorOptions& iter_opts) {
570
96
    RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts));
571
96
    RETURN_IF_ERROR(
572
96
            _load_bloom_filter_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts));
573
96
    RowRanges bf_row_ranges;
574
96
    std::unique_ptr<BloomFilterIndexIterator> bf_iter;
575
96
    RETURN_IF_ERROR(_bloom_filter_index->new_iterator(&bf_iter, iter_opts.stats));
576
96
    size_t range_size = row_ranges->range_size();
577
    // get covered page ids
578
96
    std::set<uint32_t> page_ids;
579
193
    for (int i = 0; i < range_size; ++i) {
580
97
        int64_t from = row_ranges->get_range_from(i);
581
97
        int64_t idx = from;
582
97
        int64_t to = row_ranges->get_range_to(i);
583
97
        auto iter = _ordinal_index->seek_at_or_before(from);
584
221
        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
97
    }
590
124
    for (auto& pid : page_ids) {
591
124
        std::unique_ptr<BloomFilter> bf;
592
124
        RETURN_IF_ERROR(bf_iter->read_bloom_filter(pid, &bf));
593
124
        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
124
    }
598
96
    RowRanges::ranges_intersection(*row_ranges, bf_row_ranges, row_ranges);
599
96
    return Status::OK();
600
96
}
601
602
Status ColumnReader::_load_ordinal_index(bool use_page_cache, bool kept_in_memory,
603
1.69M
                                         const ColumnIteratorOptions& iter_opts) {
604
1.69M
    if (!_ordinal_index) {
605
0
        return Status::InternalError("ordinal_index not inited");
606
0
    }
607
1.69M
    return _ordinal_index->load(use_page_cache, kept_in_memory, iter_opts.stats);
608
1.69M
}
609
610
Status ColumnReader::_load_zone_map_index(bool use_page_cache, bool kept_in_memory,
611
84.3k
                                          const ColumnIteratorOptions& iter_opts) {
612
84.4k
    if (_zone_map_index != nullptr) {
613
84.4k
        return _zone_map_index->load(use_page_cache, kept_in_memory, iter_opts.stats);
614
84.4k
    }
615
18.4E
    return Status::OK();
616
84.3k
}
617
618
Status ColumnReader::_load_index(const std::shared_ptr<IndexFileReader>& index_file_reader,
619
80.5k
                                 const TabletIndex* index_meta) {
620
80.5k
    std::unique_lock<std::shared_mutex> wlock(_load_index_lock);
621
622
80.5k
    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
80.5k
    auto it = _index_readers.find(index_meta->index_id());
628
80.5k
    if (it != _index_readers.end()) {
629
0
        return Status::OK();
630
0
    }
631
632
80.5k
    bool should_analyzer =
633
80.5k
            inverted_index::InvertedIndexAnalyzer::should_analyzer(index_meta->properties());
634
635
80.5k
    FieldType type;
636
80.5k
    if (_meta_type == FieldType::OLAP_FIELD_TYPE_ARRAY) {
637
6.38k
        type = _meta_children_column_type;
638
74.1k
    } else {
639
74.1k
        type = _type_info->type();
640
74.1k
    }
641
642
80.5k
    if (index_meta->index_type() == IndexType::ANN) {
643
183
        _index_readers[index_meta->index_id()] =
644
183
                std::make_shared<AnnIndexReader>(index_meta, index_file_reader);
645
183
        return Status::OK();
646
183
    }
647
648
80.3k
    IndexReaderPtr index_reader;
649
650
80.3k
    if (is_string_type(type)) {
651
41.5k
        if (should_analyzer) {
652
23.5k
            try {
653
23.5k
                index_reader = FullTextIndexReader::create_shared(index_meta, index_file_reader);
654
23.5k
            } catch (const CLuceneError& e) {
655
0
                return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>(
656
0
                        "create FullTextIndexReader error: {}", e.what());
657
0
            }
658
23.5k
        } else {
659
17.9k
            try {
660
17.9k
                index_reader =
661
17.9k
                        StringTypeInvertedIndexReader::create_shared(index_meta, index_file_reader);
662
17.9k
            } catch (const CLuceneError& e) {
663
0
                return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>(
664
0
                        "create StringTypeInvertedIndexReader error: {}", e.what());
665
0
            }
666
17.9k
        }
667
41.5k
    } else if (is_numeric_type(type)) {
668
38.8k
        try {
669
38.8k
            index_reader = BkdIndexReader::create_shared(index_meta, index_file_reader);
670
38.8k
        } catch (const CLuceneError& e) {
671
0
            return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>(
672
0
                    "create BkdIndexReader error: {}", e.what());
673
0
        }
674
38.8k
    } else {
675
0
        return Status::Error<ErrorCode::INVERTED_INDEX_NOT_SUPPORTED>(
676
0
                "Field type {} is not supported for inverted index", type);
677
0
    }
678
80.3k
    _index_readers[index_meta->index_id()] = index_reader;
679
80.3k
    return Status::OK();
680
80.3k
}
681
682
60.2k
bool ColumnReader::has_bloom_filter_index(bool ngram) const {
683
60.3k
    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.60M
                                       const ColumnIteratorOptions& iter_opts) {
702
1.60M
    RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts));
703
1.60M
    *iter = _ordinal_index->seek_at_or_before(ordinal);
704
1.60M
    if (!iter->valid()) {
705
0
        return Status::NotFound("Failed to seek to ordinal {}, ", ordinal);
706
0
    }
707
1.60M
    return Status::OK();
708
1.60M
}
709
710
Status ColumnReader::get_ordinal_index_reader(OrdinalIndexReader*& reader,
711
1.17M
                                              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.17M
    RETURN_IF_ERROR(
715
1.17M
            _ordinal_index->load(_use_index_page_cache, _opts.kept_in_memory, index_load_stats));
716
1.17M
    reader = _ordinal_index.get();
717
1.17M
    return Status::OK();
718
1.17M
}
719
720
383k
Status ColumnReader::new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column) {
721
383k
    return new_iterator(iterator, tablet_column, nullptr);
722
383k
}
723
724
Status ColumnReader::new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column,
725
26.4M
                                  const StorageReadOptions* opt) {
726
26.4M
    if (is_empty()) {
727
40.5k
        *iterator = std::make_unique<EmptyFileColumnIterator>();
728
40.5k
        return Status::OK();
729
40.5k
    }
730
26.4M
    if (is_scalar_type(_meta_type)) {
731
26.3M
        if (is_string_type(_meta_type)) {
732
15.8M
            *iterator = std::make_unique<StringFileColumnIterator>(shared_from_this());
733
15.8M
        } else {
734
10.4M
            *iterator = std::make_unique<FileColumnIterator>(shared_from_this());
735
10.4M
        }
736
26.3M
        (*iterator)->set_column_name(tablet_column ? tablet_column->name() : "");
737
26.3M
        return Status::OK();
738
26.3M
    } else {
739
97.7k
        auto type = _meta_type;
740
97.7k
        switch (type) {
741
822
        case FieldType::OLAP_FIELD_TYPE_AGG_STATE: {
742
822
            return new_agg_state_iterator(iterator);
743
0
        }
744
7.82k
        case FieldType::OLAP_FIELD_TYPE_STRUCT: {
745
7.82k
            return new_struct_iterator(iterator, tablet_column);
746
0
        }
747
67.2k
        case FieldType::OLAP_FIELD_TYPE_ARRAY: {
748
67.2k
            return new_array_iterator(iterator, tablet_column);
749
0
        }
750
31.9k
        case FieldType::OLAP_FIELD_TYPE_MAP: {
751
31.9k
            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
97.7k
        }
757
97.7k
    }
758
26.4M
}
759
760
821
Status ColumnReader::new_agg_state_iterator(ColumnIteratorUPtr* iterator) {
761
821
    *iterator = std::make_unique<FileColumnIterator>(shared_from_this());
762
821
    return Status::OK();
763
821
}
764
765
Status ColumnReader::new_array_iterator(ColumnIteratorUPtr* iterator,
766
67.2k
                                        const TabletColumn* tablet_column) {
767
67.2k
    ColumnIteratorUPtr item_iterator;
768
67.2k
    RETURN_IF_ERROR(_sub_readers[0]->new_iterator(
769
67.2k
            &item_iterator, tablet_column && tablet_column->get_subtype_count() > 0
770
67.2k
                                    ? &tablet_column->get_sub_column(0)
771
67.2k
                                    : nullptr));
772
773
67.2k
    item_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(0).name() : "");
774
775
67.2k
    ColumnIteratorUPtr offset_iterator;
776
67.2k
    RETURN_IF_ERROR(_sub_readers[1]->new_iterator(&offset_iterator, nullptr));
777
67.2k
    auto* file_iter = static_cast<FileColumnIterator*>(offset_iterator.release());
778
67.2k
    OffsetFileColumnIteratorUPtr ofcIter = std::make_unique<OffsetFileColumnIterator>(
779
67.2k
            std::unique_ptr<FileColumnIterator>(file_iter));
780
781
67.2k
    ColumnIteratorUPtr null_iterator;
782
67.2k
    if (is_nullable()) {
783
48.4k
        RETURN_IF_ERROR(_sub_readers[2]->new_iterator(&null_iterator, nullptr));
784
48.4k
    }
785
67.2k
    *iterator = std::make_unique<ArrayFileColumnIterator>(shared_from_this(), std::move(ofcIter),
786
67.2k
                                                          std::move(item_iterator),
787
67.2k
                                                          std::move(null_iterator));
788
67.2k
    return Status::OK();
789
67.2k
}
790
791
Status ColumnReader::new_map_iterator(ColumnIteratorUPtr* iterator,
792
31.9k
                                      const TabletColumn* tablet_column) {
793
31.9k
    ColumnIteratorUPtr key_iterator;
794
31.9k
    RETURN_IF_ERROR(_sub_readers[0]->new_iterator(
795
31.9k
            &key_iterator, tablet_column && tablet_column->get_subtype_count() > 1
796
31.9k
                                   ? &tablet_column->get_sub_column(0)
797
31.9k
                                   : nullptr));
798
31.9k
    key_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(0).name() : "");
799
31.9k
    ColumnIteratorUPtr val_iterator;
800
31.9k
    RETURN_IF_ERROR(_sub_readers[1]->new_iterator(
801
31.9k
            &val_iterator, tablet_column && tablet_column->get_subtype_count() > 1
802
31.9k
                                   ? &tablet_column->get_sub_column(1)
803
31.9k
                                   : nullptr));
804
31.9k
    val_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(1).name() : "");
805
31.9k
    ColumnIteratorUPtr offsets_iterator;
806
31.9k
    RETURN_IF_ERROR(_sub_readers[2]->new_iterator(&offsets_iterator, nullptr));
807
31.9k
    auto* file_iter = static_cast<FileColumnIterator*>(offsets_iterator.release());
808
31.9k
    OffsetFileColumnIteratorUPtr ofcIter = std::make_unique<OffsetFileColumnIterator>(
809
31.9k
            std::unique_ptr<FileColumnIterator>(file_iter));
810
811
31.9k
    ColumnIteratorUPtr null_iterator;
812
31.9k
    if (is_nullable()) {
813
15.2k
        RETURN_IF_ERROR(_sub_readers[3]->new_iterator(&null_iterator, nullptr));
814
15.2k
    }
815
31.9k
    *iterator = std::make_unique<MapFileColumnIterator>(
816
31.9k
            shared_from_this(), std::move(null_iterator), std::move(ofcIter),
817
31.9k
            std::move(key_iterator), std::move(val_iterator));
818
31.9k
    return Status::OK();
819
31.9k
}
820
821
Status ColumnReader::new_struct_iterator(ColumnIteratorUPtr* iterator,
822
7.83k
                                         const TabletColumn* tablet_column) {
823
7.83k
    std::vector<ColumnIteratorUPtr> sub_column_iterators;
824
7.83k
    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.83k
    sub_column_iterators.reserve(child_size);
827
828
34.8k
    for (uint64_t i = 0; i < child_size; i++) {
829
27.0k
        ColumnIteratorUPtr sub_column_iterator;
830
27.0k
        RETURN_IF_ERROR(_sub_readers[i]->new_iterator(
831
27.0k
                &sub_column_iterator, tablet_column ? &tablet_column->get_sub_column(i) : nullptr));
832
27.0k
        sub_column_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(i).name()
833
27.0k
                                                           : "");
834
27.0k
        sub_column_iterators.emplace_back(std::move(sub_column_iterator));
835
27.0k
    }
836
837
    // create default_iterator for schema-change behavior which increase column
838
9.13k
    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.83k
    ColumnIteratorUPtr null_iterator;
847
7.83k
    if (is_nullable()) {
848
6.99k
        RETURN_IF_ERROR(_sub_readers[child_size]->new_iterator(&null_iterator, nullptr));
849
6.99k
    }
850
7.83k
    *iterator = std::make_unique<StructFileColumnIterator>(
851
7.83k
            shared_from_this(), std::move(null_iterator), std::move(sub_column_iterators));
852
7.83k
    return Status::OK();
853
7.83k
}
854
855
Result<TColumnAccessPaths> ColumnIterator::_get_sub_access_paths(
856
119k
        const TColumnAccessPaths& access_paths) {
857
119k
    TColumnAccessPaths sub_access_paths = access_paths;
858
183k
    for (auto it = sub_access_paths.begin(); it != sub_access_paths.end();) {
859
63.9k
        TColumnAccessPath& name_path = *it;
860
63.9k
        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
63.9k
        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
63.9k
        name_path.data_access_path.path.erase(name_path.data_access_path.path.begin());
872
63.9k
        if (!name_path.data_access_path.path.empty()) {
873
6.46k
            ++it;
874
57.4k
        } else {
875
57.4k
            set_need_to_read();
876
57.4k
            it = sub_access_paths.erase(it);
877
57.4k
        }
878
63.9k
    }
879
119k
    return sub_access_paths;
880
119k
}
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
31.9k
        : _map_reader(reader),
889
31.9k
          _offsets_iterator(std::move(offsets_iterator)),
890
31.9k
          _key_iterator(std::move(key_iterator)),
891
31.9k
          _val_iterator(std::move(val_iterator)) {
892
31.9k
    if (_map_reader->is_nullable()) {
893
15.2k
        _null_iterator = std::move(null_iterator);
894
15.2k
    }
895
31.9k
}
896
897
31.9k
Status MapFileColumnIterator::init(const ColumnIteratorOptions& opts) {
898
31.9k
    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
31.8k
    RETURN_IF_ERROR(_key_iterator->init(opts));
903
31.8k
    RETURN_IF_ERROR(_val_iterator->init(opts));
904
31.8k
    RETURN_IF_ERROR(_offsets_iterator->init(opts));
905
31.8k
    if (_map_reader->is_nullable()) {
906
15.1k
        RETURN_IF_ERROR(_null_iterator->init(opts));
907
15.1k
    }
908
31.8k
    return Status::OK();
909
31.8k
}
910
911
17.9k
Status MapFileColumnIterator::seek_to_ordinal(ordinal_t ord) {
912
17.9k
    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
17.9k
    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
17.9k
    if (_map_reader->is_nullable()) {
926
10.7k
        RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord));
927
10.7k
    }
928
17.9k
    RETURN_IF_ERROR(_offsets_iterator->seek_to_ordinal(ord));
929
17.9k
    if (read_offset_only()) {
930
        // In OFFSET_ONLY mode, key/value iterators are SKIP_READING, no need to seek them
931
468
        return Status::OK();
932
468
    }
933
    // here to use offset info
934
17.4k
    ordinal_t offset = 0;
935
17.4k
    RETURN_IF_ERROR(_offsets_iterator->_peek_one_offset(&offset));
936
17.4k
    RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(offset));
937
17.4k
    RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(offset));
938
17.4k
    return Status::OK();
939
17.4k
}
940
941
8.39k
Status MapFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) {
942
8.39k
    RETURN_IF_ERROR(_offsets_iterator->init_prefetcher(params));
943
8.39k
    if (_map_reader->is_nullable()) {
944
6.12k
        RETURN_IF_ERROR(_null_iterator->init_prefetcher(params));
945
6.12k
    }
946
8.39k
    RETURN_IF_ERROR(_key_iterator->init_prefetcher(params));
947
8.39k
    RETURN_IF_ERROR(_val_iterator->init_prefetcher(params));
948
8.39k
    return Status::OK();
949
8.39k
}
950
951
void MapFileColumnIterator::collect_prefetchers(
952
        std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
953
8.39k
        PrefetcherInitMethod init_method) {
954
8.39k
    _offsets_iterator->collect_prefetchers(prefetchers, init_method);
955
8.39k
    if (_map_reader->is_nullable()) {
956
6.11k
        _null_iterator->collect_prefetchers(prefetchers, init_method);
957
6.11k
    }
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.39k
    _key_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS);
961
8.39k
    _val_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS);
962
8.39k
}
963
964
17.9k
Status MapFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
965
17.9k
    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
17.9k
    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
17.9k
    auto& column_map = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>(
996
17.9k
            dst->is_nullable() ? static_cast<ColumnNullable&>(*dst).get_nested_column() : *dst);
997
17.9k
    auto column_offsets_ptr = column_map.get_offsets_column().assume_mutable();
998
17.9k
    bool offsets_has_null = false;
999
17.9k
    ssize_t start = column_offsets_ptr->size();
1000
17.9k
    RETURN_IF_ERROR(_offsets_iterator->next_batch(n, column_offsets_ptr, &offsets_has_null));
1001
17.9k
    if (*n == 0) {
1002
0
        return Status::OK();
1003
0
    }
1004
17.9k
    auto& column_offsets = static_cast<ColumnArray::ColumnOffsets&>(*column_offsets_ptr);
1005
17.9k
    RETURN_IF_ERROR(_offsets_iterator->_calculate_offsets(start, column_offsets));
1006
17.9k
    DCHECK(column_offsets.get_data().back() >= column_offsets.get_data()[start - 1]);
1007
17.9k
    size_t num_items =
1008
17.9k
            column_offsets.get_data().back() - column_offsets.get_data()[start - 1]; // -1 is valid
1009
17.9k
    auto key_ptr = column_map.get_keys().assume_mutable();
1010
17.9k
    auto val_ptr = column_map.get_values().assume_mutable();
1011
1012
17.9k
    if (num_items > 0) {
1013
15.6k
        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
15.2k
        } else {
1018
15.2k
            size_t num_read = num_items;
1019
15.2k
            bool key_has_null = false;
1020
15.2k
            bool val_has_null = false;
1021
15.2k
            RETURN_IF_ERROR(_key_iterator->next_batch(&num_read, key_ptr, &key_has_null));
1022
15.2k
            RETURN_IF_ERROR(_val_iterator->next_batch(&num_read, val_ptr, &val_has_null));
1023
15.2k
            DCHECK(num_read == num_items);
1024
15.2k
        }
1025
1026
15.6k
        column_map.get_keys_ptr() = std::move(key_ptr);
1027
15.6k
        column_map.get_values_ptr() = std::move(val_ptr);
1028
15.6k
    }
1029
1030
17.9k
    if (dst->is_nullable()) {
1031
10.7k
        size_t num_read = *n;
1032
10.7k
        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
10.7k
        if (_null_iterator) {
1038
10.7k
            bool null_signs_has_null = false;
1039
10.7k
            RETURN_IF_ERROR(
1040
10.7k
                    _null_iterator->next_batch(&num_read, null_map_ptr, &null_signs_has_null));
1041
10.7k
        } else {
1042
1
            auto& null_map = assert_cast<ColumnUInt8&, TypeCheckOnRelease::DISABLE>(*null_map_ptr);
1043
1
            null_map.insert_many_vals(0, num_read);
1044
1
        }
1045
10.7k
        DCHECK(num_read == *n);
1046
10.7k
    }
1047
17.9k
    return Status::OK();
1048
17.9k
}
1049
1050
Status MapFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count,
1051
15.8k
                                             MutableColumnPtr& dst) {
1052
15.8k
    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
15.8k
    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
15.8k
    if (count == 0) {
1079
0
        return Status::OK();
1080
0
    }
1081
    // resolve ColumnMap and nullable wrapper
1082
15.8k
    const auto* column_map = check_and_get_column<ColumnMap>(
1083
15.8k
            dst->is_nullable() ? static_cast<ColumnNullable&>(*dst).get_nested_column() : *dst);
1084
15.8k
    auto offsets_ptr = column_map->get_offsets_column().assume_mutable();
1085
15.8k
    auto& offsets = static_cast<ColumnArray::ColumnOffsets&>(*offsets_ptr);
1086
15.8k
    size_t base = offsets.get_data().empty() ? 0 : offsets.get_data().back();
1087
1088
    // 1. bulk read null-map if nullable
1089
15.8k
    std::vector<uint8_t> null_mask; // 0: not null, 1: null
1090
15.8k
    if (_map_reader->is_nullable()) {
1091
        // For nullable map columns, the destination column must also be nullable.
1092
3.14k
        if (UNLIKELY(!dst->is_nullable())) {
1093
0
            return Status::InternalError(
1094
0
                    "unexpected non-nullable destination column for nullable map reader");
1095
0
        }
1096
3.14k
        auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr();
1097
3.14k
        size_t null_before = null_map_ptr->size();
1098
3.14k
        RETURN_IF_ERROR(_null_iterator->read_by_rowids(rowids, count, null_map_ptr));
1099
        // extract a light-weight view to decide element reads
1100
3.14k
        auto& null_map_col = assert_cast<ColumnUInt8&>(*null_map_ptr);
1101
3.14k
        null_mask.reserve(count);
1102
54.7k
        for (size_t i = 0; i < count; ++i) {
1103
51.5k
            null_mask.push_back(null_map_col.get_element(null_before + i));
1104
51.5k
        }
1105
12.6k
    } 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
15.8k
    MutableColumnPtr starts_col = ColumnOffset64::create();
1117
15.8k
    starts_col->reserve(count);
1118
15.8k
    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
15.8k
    std::vector<rowid_t> next_rowids(count);
1122
2.12M
    for (size_t i = 0; i < count; ++i) {
1123
2.10M
        uint64_t nr = rowids[i] + 1;
1124
2.10M
        next_rowids[i] = nr < _map_reader->num_rows() ? static_cast<rowid_t>(nr)
1125
2.10M
                                                      : static_cast<rowid_t>(0); // placeholder
1126
2.10M
    }
1127
15.8k
    MutableColumnPtr next_starts_col = ColumnOffset64::create();
1128
15.8k
    next_starts_col->reserve(count);
1129
    // read for all; we'll fix out-of-bound cases below
1130
15.8k
    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
2.12M
    for (size_t i = 0; i < count; ++i) {
1134
2.10M
        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.6k
            RETURN_IF_ERROR(_offsets_iterator->seek_to_ordinal(rowids[i]));
1138
12.6k
            size_t one = 1;
1139
12.6k
            bool has_null_unused = false;
1140
12.6k
            MutableColumnPtr tmp = ColumnOffset64::create();
1141
12.6k
            RETURN_IF_ERROR(_offsets_iterator->next_batch(&one, tmp, &has_null_unused));
1142
12.6k
            ordinal_t ns = 0;
1143
12.6k
            RETURN_IF_ERROR(_offsets_iterator->_peek_one_offset(&ns));
1144
            // overwrite with sentinel
1145
12.6k
            assert_cast<ColumnOffset64&, TypeCheckOnRelease::DISABLE>(*next_starts_col)
1146
12.6k
                    .get_data()[i] = ns;
1147
12.6k
        }
1148
2.10M
    }
1149
1150
    // 5. compute sizes and append offsets prefix-sum
1151
15.8k
    auto& starts_data = assert_cast<ColumnOffset64&>(*starts_col).get_data();
1152
15.8k
    auto& next_starts_data = assert_cast<ColumnOffset64&>(*next_starts_col).get_data();
1153
15.8k
    std::vector<size_t> sizes(count, 0);
1154
15.8k
    size_t acc = base;
1155
15.8k
    const auto original_size = offsets.get_data().back();
1156
15.8k
    offsets.get_data().reserve(offsets.get_data().size() + count);
1157
2.11M
    for (size_t i = 0; i < count; ++i) {
1158
2.10M
        size_t sz = static_cast<size_t>(next_starts_data[i] - starts_data[i]);
1159
2.10M
        if (_map_reader->is_nullable() && !null_mask.empty() && null_mask[i]) {
1160
736
            sz = 0; // null rows do not consume elements
1161
736
        }
1162
2.10M
        sizes[i] = sz;
1163
2.10M
        acc += sz;
1164
2.10M
        offsets.get_data().push_back(acc);
1165
2.10M
    }
1166
1167
    // 6. read key/value elements for non-empty sizes
1168
15.8k
    auto keys_ptr = column_map->get_keys().assume_mutable();
1169
15.8k
    auto vals_ptr = column_map->get_values().assume_mutable();
1170
1171
15.8k
    size_t this_run = sizes[0];
1172
15.8k
    auto start_idx = starts_data[0];
1173
15.8k
    auto last_idx = starts_data[0] + this_run;
1174
2.10M
    for (size_t i = 1; i < count; ++i) {
1175
2.08M
        size_t sz = sizes[i];
1176
2.08M
        if (sz == 0) {
1177
3.49k
            continue;
1178
3.49k
        }
1179
2.08M
        auto start = static_cast<ordinal_t>(starts_data[i]);
1180
2.08M
        if (start != last_idx) {
1181
449k
            size_t n = this_run;
1182
449k
            bool dummy_has_null = false;
1183
1184
449k
            if (this_run != 0) {
1185
449k
                if (_key_iterator->reading_flag() != ReadingFlag::SKIP_READING) {
1186
449k
                    RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(start_idx));
1187
449k
                    RETURN_IF_ERROR(_key_iterator->next_batch(&n, keys_ptr, &dummy_has_null));
1188
449k
                    DCHECK(n == this_run);
1189
449k
                }
1190
1191
449k
                if (_val_iterator->reading_flag() != ReadingFlag::SKIP_READING) {
1192
449k
                    n = this_run;
1193
449k
                    RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(start_idx));
1194
449k
                    RETURN_IF_ERROR(_val_iterator->next_batch(&n, vals_ptr, &dummy_has_null));
1195
449k
                    DCHECK(n == this_run);
1196
449k
                }
1197
449k
            }
1198
449k
            start_idx = start;
1199
449k
            this_run = sz;
1200
449k
            last_idx = start + sz;
1201
449k
            continue;
1202
449k
        }
1203
1204
1.63M
        this_run += sz;
1205
1.63M
        last_idx += sz;
1206
1.63M
    }
1207
1208
15.8k
    size_t n = this_run;
1209
15.8k
    const size_t total_count = offsets.get_data().back() - original_size;
1210
15.8k
    bool dummy_has_null = false;
1211
15.8k
    if (_key_iterator->reading_flag() != ReadingFlag::SKIP_READING) {
1212
15.7k
        if (this_run != 0) {
1213
7.05k
            RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(start_idx));
1214
7.05k
            RETURN_IF_ERROR(_key_iterator->next_batch(&n, keys_ptr, &dummy_has_null));
1215
7.05k
            DCHECK(n == this_run);
1216
7.05k
        }
1217
15.7k
    } else {
1218
9
        keys_ptr->insert_many_defaults(total_count);
1219
9
    }
1220
1221
15.8k
    if (_val_iterator->reading_flag() != ReadingFlag::SKIP_READING) {
1222
15.7k
        if (this_run != 0) {
1223
7.04k
            n = this_run;
1224
7.04k
            RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(start_idx));
1225
7.04k
            RETURN_IF_ERROR(_val_iterator->next_batch(&n, vals_ptr, &dummy_has_null));
1226
7.04k
            DCHECK(n == this_run);
1227
7.04k
        }
1228
15.7k
    } else {
1229
19
        vals_ptr->insert_many_defaults(total_count);
1230
19
    }
1231
1232
15.8k
    return Status::OK();
1233
15.8k
}
1234
1235
5.73k
void MapFileColumnIterator::set_need_to_read() {
1236
5.73k
    set_reading_flag(ReadingFlag::NEED_TO_READ);
1237
5.73k
    _key_iterator->set_need_to_read();
1238
5.73k
    _val_iterator->set_need_to_read();
1239
5.73k
}
1240
1241
6.90k
void MapFileColumnIterator::remove_pruned_sub_iterators() {
1242
6.90k
    _key_iterator->remove_pruned_sub_iterators();
1243
6.90k
    _val_iterator->remove_pruned_sub_iterators();
1244
6.90k
}
1245
1246
Status MapFileColumnIterator::set_access_paths(const TColumnAccessPaths& all_access_paths,
1247
6.65k
                                               const TColumnAccessPaths& predicate_access_paths) {
1248
6.65k
    if (all_access_paths.empty()) {
1249
1.51k
        return Status::OK();
1250
1.51k
    }
1251
1252
5.14k
    if (!predicate_access_paths.empty()) {
1253
41
        set_reading_flag(ReadingFlag::READING_FOR_PREDICATE);
1254
41
        DLOG(INFO) << "Map column iterator set sub-column " << _column_name
1255
41
                   << " to READING_FOR_PREDICATE";
1256
41
    }
1257
1258
5.14k
    auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths));
1259
5.14k
    auto sub_predicate_access_paths = DORIS_TRY(_get_sub_access_paths(predicate_access_paths));
1260
1261
5.14k
    if (sub_all_access_paths.empty()) {
1262
3.99k
        return Status::OK();
1263
3.99k
    }
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
467
        _key_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1269
467
        _val_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1270
467
        DLOG(INFO) << "Map column iterator set column " << _column_name
1271
467
                   << " to OFFSET_ONLY reading mode, key/value columns set to SKIP_READING";
1272
467
        return Status::OK();
1273
467
    }
1274
678
    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
678
    TColumnAccessPaths key_all_access_paths;
1283
678
    TColumnAccessPaths val_all_access_paths;
1284
678
    TColumnAccessPaths key_predicate_access_paths;
1285
678
    TColumnAccessPaths val_predicate_access_paths;
1286
1287
723
    for (auto paths : sub_all_access_paths) {
1288
723
        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
185
            TColumnAccessPath key_path;
1294
185
            key_path.__set_type(paths.type);
1295
185
            TDataAccessPath key_data_path;
1296
185
            key_data_path.__set_path({_key_iterator->column_name()});
1297
185
            key_path.__set_data_access_path(key_data_path);
1298
185
            key_all_access_paths.emplace_back(std::move(key_path));
1299
            // For value: pass the full sub-path so qualifiers like OFFSET propagate.
1300
185
            paths.data_access_path.path[0] = _val_iterator->column_name();
1301
185
            val_all_access_paths.emplace_back(paths);
1302
538
        } else if (paths.data_access_path.path[0] == ACCESS_MAP_KEYS) {
1303
274
            paths.data_access_path.path[0] = _key_iterator->column_name();
1304
274
            key_all_access_paths.emplace_back(paths);
1305
274
        } else if (paths.data_access_path.path[0] == ACCESS_MAP_VALUES) {
1306
264
            paths.data_access_path.path[0] = _val_iterator->column_name();
1307
264
            val_all_access_paths.emplace_back(paths);
1308
264
        }
1309
723
    }
1310
678
    const auto need_read_keys = !key_all_access_paths.empty();
1311
678
    const auto need_read_values = !val_all_access_paths.empty();
1312
1313
678
    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
678
    if (need_read_keys) {
1334
455
        _key_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ);
1335
455
        RETURN_IF_ERROR(
1336
455
                _key_iterator->set_access_paths(key_all_access_paths, key_predicate_access_paths));
1337
455
    } else {
1338
223
        _key_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1339
223
        DLOG(INFO) << "Map column iterator set key column to SKIP_READING";
1340
223
    }
1341
1342
678
    if (need_read_values) {
1343
449
        _val_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ);
1344
449
        RETURN_IF_ERROR(
1345
449
                _val_iterator->set_access_paths(val_all_access_paths, val_predicate_access_paths));
1346
449
    } else {
1347
229
        _val_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1348
229
        DLOG(INFO) << "Map column iterator set value column to SKIP_READING";
1349
229
    }
1350
678
    return Status::OK();
1351
678
}
1352
1353
////////////////////////////////////////////////////////////////////////////////
1354
1355
StructFileColumnIterator::StructFileColumnIterator(
1356
        std::shared_ptr<ColumnReader> reader, ColumnIteratorUPtr null_iterator,
1357
        std::vector<ColumnIteratorUPtr>&& sub_column_iterators)
1358
7.83k
        : _struct_reader(reader), _sub_column_iterators(std::move(sub_column_iterators)) {
1359
7.83k
    if (_struct_reader->is_nullable()) {
1360
6.99k
        _null_iterator = std::move(null_iterator);
1361
6.99k
    }
1362
7.83k
}
1363
1364
7.79k
Status StructFileColumnIterator::init(const ColumnIteratorOptions& opts) {
1365
7.79k
    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
27.4k
    for (auto& column_iterator : _sub_column_iterators) {
1371
27.4k
        RETURN_IF_ERROR(column_iterator->init(opts));
1372
27.4k
    }
1373
7.79k
    if (_struct_reader->is_nullable()) {
1374
6.98k
        RETURN_IF_ERROR(_null_iterator->init(opts));
1375
6.98k
    }
1376
7.79k
    return Status::OK();
1377
7.79k
}
1378
1379
4.52k
Status StructFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
1380
4.52k
    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.52k
    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.52k
    auto& column_struct = assert_cast<ColumnStruct&, TypeCheckOnRelease::DISABLE>(
1411
4.52k
            dst->is_nullable() ? static_cast<ColumnNullable&>(*dst).get_nested_column() : *dst);
1412
19.0k
    for (size_t i = 0; i < column_struct.tuple_size(); i++) {
1413
14.5k
        size_t num_read = *n;
1414
14.5k
        auto sub_column_ptr = column_struct.get_column(i).assume_mutable();
1415
14.5k
        bool column_has_null = false;
1416
14.5k
        RETURN_IF_ERROR(
1417
14.5k
                _sub_column_iterators[i]->next_batch(&num_read, sub_column_ptr, &column_has_null));
1418
14.5k
        DCHECK(num_read == *n);
1419
14.5k
        column_struct.get_column_ptr(i) = std::move(sub_column_ptr);
1420
14.5k
    }
1421
1422
4.52k
    if (dst->is_nullable()) {
1423
4.21k
        size_t num_read = *n;
1424
4.21k
        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
4.21k
        if (_null_iterator) {
1430
4.12k
            bool null_signs_has_null = false;
1431
4.12k
            RETURN_IF_ERROR(
1432
4.12k
                    _null_iterator->next_batch(&num_read, null_map_ptr, &null_signs_has_null));
1433
4.12k
        } else {
1434
90
            auto& null_map = assert_cast<ColumnUInt8&, TypeCheckOnRelease::DISABLE>(*null_map_ptr);
1435
90
            null_map.insert_many_vals(0, num_read);
1436
90
        }
1437
4.21k
        DCHECK(num_read == *n);
1438
4.21k
    }
1439
1440
4.52k
    return Status::OK();
1441
4.52k
}
1442
1443
4.53k
Status StructFileColumnIterator::seek_to_ordinal(ordinal_t ord) {
1444
4.53k
    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.53k
    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
14.5k
    for (auto& column_iterator : _sub_column_iterators) {
1458
14.5k
        RETURN_IF_ERROR(column_iterator->seek_to_ordinal(ord));
1459
14.5k
    }
1460
4.53k
    if (_struct_reader->is_nullable()) {
1461
4.12k
        RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord));
1462
4.12k
    }
1463
4.53k
    return Status::OK();
1464
4.53k
}
1465
1466
3.78k
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.78k
    if (_struct_reader->is_nullable()) {
1471
3.41k
        RETURN_IF_ERROR(_null_iterator->init_prefetcher(params));
1472
3.41k
    }
1473
3.78k
    return Status::OK();
1474
3.78k
}
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.41k
        _null_iterator->collect_prefetchers(prefetchers, init_method);
1484
3.41k
    }
1485
3.78k
}
1486
1487
Status StructFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count,
1488
2.65k
                                                MutableColumnPtr& dst) {
1489
2.65k
    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.65k
    if (count == 0) {
1496
0
        return Status::OK();
1497
0
    }
1498
1499
2.65k
    size_t this_run = 1;
1500
2.65k
    auto start_idx = rowids[0];
1501
2.65k
    auto last_idx = rowids[0];
1502
2.74k
    for (size_t i = 1; i < count; ++i) {
1503
90
        if (last_idx == rowids[i] - 1) {
1504
86
            last_idx = rowids[i];
1505
86
            this_run++;
1506
86
            continue;
1507
86
        }
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.65k
    RETURN_IF_ERROR(seek_to_ordinal(start_idx));
1519
2.65k
    size_t num_read = this_run;
1520
2.65k
    RETURN_IF_ERROR(next_batch(&num_read, dst));
1521
2.65k
    DCHECK_EQ(num_read, this_run);
1522
2.65k
    return Status::OK();
1523
2.65k
}
1524
1525
6.90k
void StructFileColumnIterator::set_need_to_read() {
1526
6.90k
    set_reading_flag(ReadingFlag::NEED_TO_READ);
1527
24.9k
    for (auto& sub_iterator : _sub_column_iterators) {
1528
24.9k
        sub_iterator->set_need_to_read();
1529
24.9k
    }
1530
6.90k
}
1531
1532
7.00k
void StructFileColumnIterator::remove_pruned_sub_iterators() {
1533
32.9k
    for (auto it = _sub_column_iterators.begin(); it != _sub_column_iterators.end();) {
1534
25.9k
        auto& sub_iterator = *it;
1535
25.9k
        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
25.1k
        } else {
1540
25.1k
            sub_iterator->remove_pruned_sub_iterators();
1541
25.1k
            ++it;
1542
25.1k
        }
1543
25.9k
    }
1544
7.00k
}
1545
1546
Status StructFileColumnIterator::set_access_paths(
1547
        const TColumnAccessPaths& all_access_paths,
1548
6.45k
        const TColumnAccessPaths& predicate_access_paths) {
1549
6.45k
    if (all_access_paths.empty()) {
1550
1.86k
        return Status::OK();
1551
1.86k
    }
1552
1553
4.58k
    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.58k
    auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths));
1559
4.58k
    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.58k
    _check_and_set_meta_read_mode(sub_all_access_paths);
1563
4.58k
    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.58k
    const auto no_sub_column_to_skip = sub_all_access_paths.empty();
1573
4.58k
    const auto no_predicate_sub_column = sub_predicate_access_paths.empty();
1574
1575
20.5k
    for (auto& sub_iterator : _sub_column_iterators) {
1576
20.5k
        const auto name = sub_iterator->column_name();
1577
20.5k
        bool need_to_read = no_sub_column_to_skip;
1578
20.5k
        TColumnAccessPaths sub_all_access_paths_of_this;
1579
20.5k
        if (!need_to_read) {
1580
1.31k
            for (const auto& paths : sub_all_access_paths) {
1581
1.31k
                if (paths.data_access_path.path[0] == name) {
1582
198
                    sub_all_access_paths_of_this.emplace_back(paths);
1583
198
                }
1584
1.31k
            }
1585
1.01k
            need_to_read = !sub_all_access_paths_of_this.empty();
1586
1.01k
        }
1587
1588
20.5k
        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
19.6k
        set_reading_flag(ReadingFlag::NEED_TO_READ);
1595
19.6k
        sub_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ);
1596
1597
19.6k
        TColumnAccessPaths sub_predicate_access_paths_of_this;
1598
1599
19.6k
        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
19.6k
        RETURN_IF_ERROR(sub_iterator->set_access_paths(sub_all_access_paths_of_this,
1608
19.6k
                                                       sub_predicate_access_paths_of_this));
1609
19.6k
    }
1610
4.58k
    return Status::OK();
1611
4.58k
}
1612
1613
////////////////////////////////////////////////////////////////////////////////
1614
98.9k
Status OffsetFileColumnIterator::init(const ColumnIteratorOptions& opts) {
1615
98.9k
    RETURN_IF_ERROR(_offset_iterator->init(opts));
1616
    // allocate peek tmp column once
1617
98.9k
    _peek_tmp_col = ColumnOffset64::create();
1618
98.9k
    return Status::OK();
1619
98.9k
}
1620
1621
318k
Status OffsetFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
1622
318k
    RETURN_IF_ERROR(_offset_iterator->next_batch(n, dst, has_null));
1623
318k
    return Status::OK();
1624
318k
}
1625
1626
621k
Status OffsetFileColumnIterator::_peek_one_offset(ordinal_t* offset) {
1627
621k
    if (_offset_iterator->get_current_page()->has_remaining()) {
1628
535k
        PageDecoder* offset_page_decoder = _offset_iterator->get_current_page()->data_decoder.get();
1629
535k
        size_t n = 1;
1630
535k
        _peek_tmp_col->clear();
1631
535k
        RETURN_IF_ERROR(offset_page_decoder->peek_next_batch(&n, _peek_tmp_col)); // not null
1632
535k
        DCHECK(_peek_tmp_col->size() == 1);
1633
535k
        *offset =
1634
535k
                assert_cast<const ColumnOffset64*, TypeCheckOnRelease::DISABLE>(_peek_tmp_col.get())
1635
535k
                        ->get_element(0);
1636
535k
    } else {
1637
86.4k
        *offset = _offset_iterator->get_current_page()->next_array_item_ordinal;
1638
86.4k
    }
1639
621k
    return Status::OK();
1640
621k
}
1641
1642
61.9k
Status OffsetFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) {
1643
61.9k
    return _offset_iterator->init_prefetcher(params);
1644
61.9k
}
1645
1646
void OffsetFileColumnIterator::collect_prefetchers(
1647
        std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
1648
61.6k
        PrefetcherInitMethod init_method) {
1649
61.6k
    _offset_iterator->collect_prefetchers(prefetchers, init_method);
1650
61.6k
}
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
305k
                                                    ColumnArray::ColumnOffsets& column_offsets) {
1666
305k
    ordinal_t next_storage_offset = 0;
1667
305k
    RETURN_IF_ERROR(_peek_one_offset(&next_storage_offset));
1668
1669
    // calculate real offsets
1670
305k
    auto& offsets_data = column_offsets.get_data();
1671
305k
    ordinal_t first_column_offset = offsets_data[start - 1]; // -1 is valid
1672
305k
    ordinal_t first_storage_offset = offsets_data[start];
1673
305k
    DCHECK(next_storage_offset >= first_storage_offset);
1674
16.0M
    for (ssize_t i = start; i < offsets_data.size() - 1; ++i) {
1675
15.7M
        offsets_data[i] = first_column_offset + (offsets_data[i + 1] - first_storage_offset);
1676
15.7M
    }
1677
    // last offset
1678
305k
    offsets_data[offsets_data.size() - 1] =
1679
305k
            first_column_offset + (next_storage_offset - first_storage_offset);
1680
305k
    return Status::OK();
1681
305k
}
1682
1683
////////////////////////////////////////////////////////////////////////////////
1684
ArrayFileColumnIterator::ArrayFileColumnIterator(std::shared_ptr<ColumnReader> reader,
1685
                                                 OffsetFileColumnIteratorUPtr offset_reader,
1686
                                                 ColumnIteratorUPtr item_iterator,
1687
                                                 ColumnIteratorUPtr null_iterator)
1688
67.1k
        : _array_reader(reader),
1689
67.1k
          _offset_iterator(std::move(offset_reader)),
1690
67.1k
          _item_iterator(std::move(item_iterator)) {
1691
67.1k
    if (_array_reader->is_nullable()) {
1692
48.4k
        _null_iterator = std::move(null_iterator);
1693
48.4k
    }
1694
67.1k
}
1695
1696
67.1k
Status ArrayFileColumnIterator::init(const ColumnIteratorOptions& opts) {
1697
67.1k
    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
67.0k
    RETURN_IF_ERROR(_offset_iterator->init(opts));
1703
67.0k
    RETURN_IF_ERROR(_item_iterator->init(opts));
1704
67.0k
    if (_array_reader->is_nullable()) {
1705
48.3k
        RETURN_IF_ERROR(_null_iterator->init(opts));
1706
48.3k
    }
1707
67.0k
    return Status::OK();
1708
67.0k
}
1709
1710
287k
Status ArrayFileColumnIterator::_seek_by_offsets(ordinal_t ord) {
1711
287k
    if (read_offset_only()) {
1712
        // In OFFSET_ONLY mode, item iterator is SKIP_READING, no need to seek it
1713
1.35k
        return Status::OK();
1714
1.35k
    }
1715
    // using offsets info
1716
286k
    ordinal_t offset = 0;
1717
286k
    RETURN_IF_ERROR(_offset_iterator->_peek_one_offset(&offset));
1718
286k
    RETURN_IF_ERROR(_item_iterator->seek_to_ordinal(offset));
1719
286k
    return Status::OK();
1720
286k
}
1721
1722
287k
Status ArrayFileColumnIterator::seek_to_ordinal(ordinal_t ord) {
1723
287k
    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
287k
    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
287k
    RETURN_IF_ERROR(_offset_iterator->seek_to_ordinal(ord));
1737
287k
    if (_array_reader->is_nullable()) {
1738
266k
        RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord));
1739
266k
    }
1740
287k
    return _seek_by_offsets(ord);
1741
287k
}
1742
1743
287k
Status ArrayFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
1744
287k
    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
287k
    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
287k
    const auto* column_array = check_and_get_column<ColumnArray>(
1775
287k
            dst->is_nullable() ? static_cast<ColumnNullable&>(*dst).get_nested_column() : *dst);
1776
1777
287k
    bool offsets_has_null = false;
1778
287k
    auto column_offsets_ptr = column_array->get_offsets_column().assume_mutable();
1779
287k
    ssize_t start = column_offsets_ptr->size();
1780
287k
    RETURN_IF_ERROR(_offset_iterator->next_batch(n, column_offsets_ptr, &offsets_has_null));
1781
287k
    if (*n == 0) {
1782
0
        return Status::OK();
1783
0
    }
1784
287k
    auto& column_offsets = static_cast<ColumnArray::ColumnOffsets&>(*column_offsets_ptr);
1785
287k
    RETURN_IF_ERROR(_offset_iterator->_calculate_offsets(start, column_offsets));
1786
287k
    size_t num_items =
1787
287k
            column_offsets.get_data().back() - column_offsets.get_data()[start - 1]; // -1 is valid
1788
287k
    auto column_items_ptr = column_array->get_data().assume_mutable();
1789
287k
    if (num_items > 0) {
1790
82.5k
        if (read_offset_only()) {
1791
            // OFFSET_ONLY mode: skip reading actual item data, fill with defaults
1792
1.03k
            column_items_ptr->insert_many_defaults(num_items);
1793
81.4k
        } else {
1794
81.4k
            size_t num_read = num_items;
1795
81.4k
            bool items_has_null = false;
1796
81.4k
            RETURN_IF_ERROR(
1797
81.4k
                    _item_iterator->next_batch(&num_read, column_items_ptr, &items_has_null));
1798
81.4k
            DCHECK(num_read == num_items);
1799
81.4k
        }
1800
82.5k
    }
1801
1802
287k
    if (dst->is_nullable()) {
1803
266k
        auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr();
1804
266k
        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
266k
        if (_null_iterator) {
1810
266k
            bool null_signs_has_null = false;
1811
266k
            RETURN_IF_ERROR(
1812
266k
                    _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
266k
        DCHECK(num_read == *n);
1818
266k
    }
1819
1820
287k
    return Status::OK();
1821
287k
}
1822
1823
53.5k
Status ArrayFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) {
1824
53.5k
    RETURN_IF_ERROR(_offset_iterator->init_prefetcher(params));
1825
53.5k
    RETURN_IF_ERROR(_item_iterator->init_prefetcher(params));
1826
53.5k
    if (_array_reader->is_nullable()) {
1827
35.3k
        RETURN_IF_ERROR(_null_iterator->init_prefetcher(params));
1828
35.3k
    }
1829
53.5k
    return Status::OK();
1830
53.5k
}
1831
1832
void ArrayFileColumnIterator::collect_prefetchers(
1833
        std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
1834
53.4k
        PrefetcherInitMethod init_method) {
1835
53.4k
    _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
53.4k
    _item_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS);
1839
53.4k
    if (_array_reader->is_nullable()) {
1840
35.2k
        _null_iterator->collect_prefetchers(prefetchers, init_method);
1841
35.2k
    }
1842
53.4k
}
1843
1844
Status ArrayFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count,
1845
95.3k
                                               MutableColumnPtr& dst) {
1846
95.3k
    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
355k
    for (size_t i = 0; i < count; ++i) {
1853
        // TODO(cambyszju): now read array one by one, need optimize later
1854
259k
        RETURN_IF_ERROR(seek_to_ordinal(rowids[i]));
1855
259k
        size_t num_read = 1;
1856
259k
        RETURN_IF_ERROR(next_batch(&num_read, dst));
1857
259k
    }
1858
95.3k
    return Status::OK();
1859
95.3k
}
1860
1861
50.3k
void ArrayFileColumnIterator::set_need_to_read() {
1862
50.3k
    set_reading_flag(ReadingFlag::NEED_TO_READ);
1863
50.3k
    _item_iterator->set_need_to_read();
1864
50.3k
}
1865
1866
52.0k
void ArrayFileColumnIterator::remove_pruned_sub_iterators() {
1867
52.0k
    _item_iterator->remove_pruned_sub_iterators();
1868
52.0k
}
1869
1870
Status ArrayFileColumnIterator::set_access_paths(const TColumnAccessPaths& all_access_paths,
1871
51.4k
                                                 const TColumnAccessPaths& predicate_access_paths) {
1872
51.4k
    if (all_access_paths.empty()) {
1873
1.75k
        return Status::OK();
1874
1.75k
    }
1875
1876
49.6k
    if (!predicate_access_paths.empty()) {
1877
3.08k
        set_reading_flag(ReadingFlag::READING_FOR_PREDICATE);
1878
3.08k
        DLOG(INFO) << "Array column iterator set sub-column " << _column_name
1879
3.08k
                   << " to READING_FOR_PREDICATE";
1880
3.08k
    }
1881
1882
49.6k
    auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths));
1883
49.6k
    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
49.6k
    _check_and_set_meta_read_mode(sub_all_access_paths);
1887
49.6k
    if (read_offset_only()) {
1888
1.33k
        _item_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1889
1.33k
        DLOG(INFO) << "Array column iterator set column " << _column_name
1890
1.33k
                   << " to OFFSET_ONLY reading mode, item column set to SKIP_READING";
1891
1.33k
        return Status::OK();
1892
1.33k
    }
1893
48.3k
    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
48.3k
    const auto no_sub_column_to_skip = sub_all_access_paths.empty();
1901
48.3k
    const auto no_predicate_sub_column = sub_predicate_access_paths.empty();
1902
1903
48.3k
    if (!no_sub_column_to_skip) {
1904
3.31k
        for (auto& path : sub_all_access_paths) {
1905
3.31k
            if (path.data_access_path.path[0] == ACCESS_ALL) {
1906
3.30k
                path.data_access_path.path[0] = _item_iterator->column_name();
1907
3.30k
            }
1908
3.31k
        }
1909
3.29k
    }
1910
1911
48.3k
    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
48.3k
    if (!no_sub_column_to_skip || !no_predicate_sub_column) {
1920
3.33k
        _item_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ);
1921
3.33k
        RETURN_IF_ERROR(
1922
3.33k
                _item_iterator->set_access_paths(sub_all_access_paths, sub_predicate_access_paths));
1923
3.33k
    }
1924
48.3k
    return Status::OK();
1925
48.3k
}
1926
1927
////////////////////////////////////////////////////////////////////////////////
1928
// StringFileColumnIterator implementation
1929
////////////////////////////////////////////////////////////////////////////////
1930
1931
StringFileColumnIterator::StringFileColumnIterator(std::shared_ptr<ColumnReader> reader)
1932
15.9M
        : FileColumnIterator(std::move(reader)) {}
1933
1934
15.9M
Status StringFileColumnIterator::init(const ColumnIteratorOptions& opts) {
1935
15.9M
    if (read_offset_only()) {
1936
        // Propagate only_read_offsets to the FileColumnIterator's options
1937
115
        auto modified_opts = opts;
1938
115
        modified_opts.only_read_offsets = true;
1939
115
        return FileColumnIterator::init(modified_opts);
1940
115
    }
1941
15.9M
    return FileColumnIterator::init(opts);
1942
15.9M
}
1943
1944
Status StringFileColumnIterator::set_access_paths(
1945
        const TColumnAccessPaths& all_access_paths,
1946
4.66k
        const TColumnAccessPaths& predicate_access_paths) {
1947
4.66k
    if (all_access_paths.empty()) {
1948
3.76k
        return Status::OK();
1949
3.76k
    }
1950
1951
902
    if (!predicate_access_paths.empty()) {
1952
134
        set_reading_flag(ReadingFlag::READING_FOR_PREDICATE);
1953
134
    }
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
902
    auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths));
1958
902
    _check_and_set_meta_read_mode(sub_all_access_paths);
1959
902
    if (read_offset_only()) {
1960
116
        DLOG(INFO) << "String column iterator set column " << _column_name
1961
116
                   << " to OFFSET_ONLY reading mode";
1962
786
    } 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
902
    return Status::OK();
1968
902
}
1969
1970
////////////////////////////////////////////////////////////////////////////////
1971
1972
26.3M
FileColumnIterator::FileColumnIterator(std::shared_ptr<ColumnReader> reader) : _reader(reader) {}
1973
1974
56.2k
void ColumnIterator::_check_and_set_meta_read_mode(const TColumnAccessPaths& sub_all_access_paths) {
1975
56.2k
    for (const auto& path : sub_all_access_paths) {
1976
6.08k
        if (!path.data_access_path.path.empty()) {
1977
6.08k
            if (StringCaseEqual()(path.data_access_path.path[0], ACCESS_OFFSET)) {
1978
1.90k
                _read_mode = ReadMode::OFFSET_ONLY;
1979
1.90k
                return;
1980
4.17k
            } 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
6.08k
        }
1985
6.06k
    }
1986
54.3k
    _read_mode = ReadMode::DEFAULT;
1987
54.3k
}
1988
1989
26.3M
Status FileColumnIterator::init(const ColumnIteratorOptions& opts) {
1990
26.3M
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1991
2.35k
        DLOG(INFO) << "File column iterator column " << _column_name << " skip reading.";
1992
2.35k
        return Status::OK();
1993
2.35k
    }
1994
1995
26.3M
    _opts = opts;
1996
26.3M
    if (!_opts.use_page_cache) {
1997
26.3M
        _reader->disable_index_meta_cache();
1998
26.3M
    }
1999
26.3M
    RETURN_IF_ERROR(get_block_compression_codec(_reader->get_compression(), &_compress_codec));
2000
26.3M
    if (config::enable_low_cardinality_optimize &&
2001
26.3M
        opts.io_ctx.reader_type == ReaderType::READER_QUERY &&
2002
26.3M
        _reader->encoding_info()->encoding() == DICT_ENCODING) {
2003
15.7M
        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
15.7M
        if (dict_encoding_type == ColumnReader::UNKNOWN_DICT_ENCODING && opts.is_predicate_column) {
2011
15.2k
            RETURN_IF_ERROR(seek_to_ordinal(_reader->num_rows() - 1));
2012
15.2k
            _is_all_dict_encoding = _page.is_dict_encoding;
2013
15.2k
            _reader->set_dict_encoding_type(_is_all_dict_encoding
2014
15.2k
                                                    ? ColumnReader::ALL_DICT_ENCODING
2015
15.2k
                                                    : ColumnReader::PARTIAL_DICT_ENCODING);
2016
15.7M
        } else {
2017
15.7M
            _is_all_dict_encoding = dict_encoding_type == ColumnReader::ALL_DICT_ENCODING;
2018
15.7M
        }
2019
15.7M
    }
2020
26.3M
    return Status::OK();
2021
26.3M
}
2022
2023
26.4M
FileColumnIterator::~FileColumnIterator() = default;
2024
2025
1.83M
void FileColumnIterator::_trigger_prefetch_if_eligible(ordinal_t ord) {
2026
1.83M
    std::vector<BlockRange> ranges;
2027
1.83M
    if (_prefetcher->need_prefetch(cast_set<uint32_t>(ord), &ranges)) {
2028
1.14M
        for (const auto& range : ranges) {
2029
1.14M
            _cached_remote_file_reader->prefetch_range(range.offset, range.size, &_opts.io_ctx);
2030
1.14M
        }
2031
1.14M
    }
2032
1.83M
}
2033
2034
4.46M
Status FileColumnIterator::seek_to_ordinal(ordinal_t ord) {
2035
4.46M
    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
4.46M
    if (_enable_prefetch) {
2044
1.83M
        _trigger_prefetch_if_eligible(ord);
2045
1.83M
    }
2046
2047
    // if current page contains this row, we don't need to seek
2048
4.46M
    if (!_page || !_page.contains(ord) || !_page_iter.valid()) {
2049
1.60M
        RETURN_IF_ERROR(_reader->seek_at_or_before(ord, &_page_iter, _opts));
2050
1.60M
        RETURN_IF_ERROR(_read_data_page(_page_iter));
2051
1.60M
    }
2052
4.46M
    RETURN_IF_ERROR(_seek_to_pos_in_page(&_page, ord - _page.first_ordinal));
2053
4.46M
    _current_ordinal = ord;
2054
4.46M
    return Status::OK();
2055
4.46M
}
2056
2057
0
Status FileColumnIterator::seek_to_page_start() {
2058
0
    return seek_to_ordinal(_page.first_ordinal);
2059
0
}
2060
2061
4.52M
Status FileColumnIterator::_seek_to_pos_in_page(ParsedPage* page, ordinal_t offset_in_page) const {
2062
4.52M
    if (page->offset_in_page == offset_in_page) {
2063
        // fast path, do nothing
2064
2.11M
        return Status::OK();
2065
2.11M
    }
2066
2067
2.40M
    ordinal_t pos_in_data = offset_in_page;
2068
2.40M
    if (_page.has_null) {
2069
226k
        ordinal_t offset_in_data = 0;
2070
226k
        ordinal_t skips = offset_in_page;
2071
2072
226k
        if (offset_in_page > page->offset_in_page) {
2073
            // forward, reuse null bitmap
2074
200k
            skips = offset_in_page - page->offset_in_page;
2075
200k
            offset_in_data = page->data_decoder->current_index();
2076
200k
        } else {
2077
            // rewind null bitmap, and
2078
25.8k
            page->null_decoder = RleDecoder<bool>((const uint8_t*)page->null_bitmap.data,
2079
25.8k
                                                  cast_set<int>(page->null_bitmap.size), 1);
2080
25.8k
        }
2081
2082
226k
        auto skip_nulls = page->null_decoder.Skip(skips);
2083
226k
        pos_in_data = offset_in_data + skips - skip_nulls;
2084
226k
    }
2085
2086
2.40M
    RETURN_IF_ERROR(page->data_decoder->seek_to_position_in_page(pos_in_data));
2087
2.40M
    page->offset_in_page = offset_in_page;
2088
2.40M
    return Status::OK();
2089
2.40M
}
2090
2091
7.44k
Status FileColumnIterator::next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) {
2092
7.44k
    return _reader->next_batch_of_zone_map(n, dst);
2093
7.44k
}
2094
2095
3.52M
Status FileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
2096
3.52M
    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
3.52M
    if (_reading_flag == ReadingFlag::SKIP_READING) {
2145
448
        DLOG(INFO) << "File column iterator column " << _column_name << " skip reading.";
2146
448
        dst->insert_many_defaults(*n);
2147
448
        return Status::OK();
2148
448
    }
2149
2150
3.52M
    size_t curr_size = dst->byte_size();
2151
3.52M
    dst->reserve(*n);
2152
3.52M
    size_t remaining = *n;
2153
3.52M
    *has_null = false;
2154
7.12M
    while (remaining > 0) {
2155
3.58M
        if (!_page.has_remaining()) {
2156
63.1k
            bool eos = false;
2157
63.1k
            RETURN_IF_ERROR(_load_next_page(&eos));
2158
63.1k
            if (eos) {
2159
0
                break;
2160
0
            }
2161
63.1k
        }
2162
2163
        // number of rows to be read from this page
2164
3.58M
        size_t nrows_in_page = std::min(remaining, _page.remaining());
2165
3.58M
        size_t nrows_to_read = nrows_in_page;
2166
3.58M
        if (_page.has_null) {
2167
1.11M
            while (nrows_to_read > 0) {
2168
933k
                bool is_null = false;
2169
933k
                size_t this_run = _page.null_decoder.GetNextRun(&is_null, nrows_to_read);
2170
                // we use num_rows only for CHECK
2171
933k
                size_t num_rows = this_run;
2172
933k
                if (!is_null) {
2173
469k
                    RETURN_IF_ERROR(_page.data_decoder->next_batch(&num_rows, dst));
2174
469k
                    DCHECK_EQ(this_run, num_rows);
2175
469k
                } else {
2176
463k
                    *has_null = true;
2177
463k
                    auto* null_col = check_and_get_column<ColumnNullable>(dst.get());
2178
478k
                    if (null_col != nullptr) {
2179
478k
                        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
463k
                }
2184
2185
948k
                nrows_to_read -= this_run;
2186
948k
                _page.offset_in_page += this_run;
2187
948k
                _current_ordinal += this_run;
2188
948k
            }
2189
3.41M
        } else {
2190
3.41M
            RETURN_IF_ERROR(_page.data_decoder->next_batch(&nrows_to_read, dst));
2191
3.41M
            DCHECK_EQ(nrows_to_read, nrows_in_page);
2192
2193
3.41M
            _page.offset_in_page += nrows_to_read;
2194
3.41M
            _current_ordinal += nrows_to_read;
2195
3.41M
        }
2196
3.60M
        remaining -= nrows_in_page;
2197
3.60M
    }
2198
3.53M
    *n -= remaining;
2199
3.53M
    _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
3.53M
    return Status::OK();
2205
3.52M
}
2206
2207
Status FileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count,
2208
709k
                                          MutableColumnPtr& dst) {
2209
709k
    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
709k
    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
709k
    size_t remaining = count;
2281
709k
    size_t total_read_count = 0;
2282
709k
    size_t nrows_to_read = 0;
2283
1.44M
    while (remaining > 0) {
2284
735k
        RETURN_IF_ERROR(seek_to_ordinal(rowids[total_read_count]));
2285
2286
        // number of rows to be read from this page
2287
735k
        nrows_to_read = std::min(remaining, _page.remaining());
2288
2289
735k
        if (_page.has_null) {
2290
65.0k
            size_t already_read = 0;
2291
433k
            while ((nrows_to_read - already_read) > 0) {
2292
368k
                bool is_null = false;
2293
368k
                size_t this_run = std::min(nrows_to_read - already_read, _page.remaining());
2294
368k
                if (UNLIKELY(this_run == 0)) {
2295
61
                    break;
2296
61
                }
2297
368k
                this_run = _page.null_decoder.GetNextRun(&is_null, this_run);
2298
368k
                size_t offset = total_read_count + already_read;
2299
368k
                size_t this_read_count = 0;
2300
368k
                rowid_t current_ordinal_in_page =
2301
368k
                        cast_set<uint32_t>(_page.offset_in_page + _page.first_ordinal);
2302
3.70M
                for (size_t i = 0; i < this_run; ++i) {
2303
3.61M
                    if (rowids[offset + i] - current_ordinal_in_page >= this_run) {
2304
276k
                        break;
2305
276k
                    }
2306
3.34M
                    this_read_count++;
2307
3.34M
                }
2308
2309
368k
                auto origin_index = _page.data_decoder->current_index();
2310
368k
                if (this_read_count > 0) {
2311
97.6k
                    if (is_null) {
2312
66.3k
                        auto* null_col = check_and_get_column<ColumnNullable>(dst.get());
2313
66.3k
                        if (UNLIKELY(null_col == nullptr)) {
2314
0
                            return Status::InternalError("unexpected column type in column reader");
2315
0
                        }
2316
2317
66.3k
                        null_col->insert_many_defaults(this_read_count);
2318
66.3k
                    } else {
2319
31.2k
                        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
31.2k
                        size_t page_start_off_in_decoder =
2324
31.2k
                                _page.first_ordinal + _page.offset_in_page - origin_index;
2325
31.2k
                        RETURN_IF_ERROR(_page.data_decoder->read_by_rowids(
2326
31.2k
                                &rowids[offset], page_start_off_in_decoder, &read_count, dst));
2327
31.2k
                        DCHECK_EQ(read_count, this_read_count);
2328
31.2k
                    }
2329
97.6k
                }
2330
2331
368k
                if (!is_null) {
2332
300k
                    RETURN_IF_ERROR(
2333
300k
                            _page.data_decoder->seek_to_position_in_page(origin_index + this_run));
2334
300k
                }
2335
2336
368k
                already_read += this_read_count;
2337
368k
                _page.offset_in_page += this_run;
2338
368k
                DCHECK(_page.offset_in_page <= _page.num_rows);
2339
368k
            }
2340
2341
65.0k
            nrows_to_read = already_read;
2342
65.0k
            total_read_count += nrows_to_read;
2343
65.0k
            remaining -= nrows_to_read;
2344
670k
        } else {
2345
670k
            RETURN_IF_ERROR(_page.data_decoder->read_by_rowids(
2346
670k
                    &rowids[total_read_count], _page.first_ordinal, &nrows_to_read, dst));
2347
670k
            total_read_count += nrows_to_read;
2348
670k
            remaining -= nrows_to_read;
2349
670k
        }
2350
735k
    }
2351
709k
    return Status::OK();
2352
709k
}
2353
2354
63.1k
Status FileColumnIterator::_load_next_page(bool* eos) {
2355
63.1k
    _page_iter.next();
2356
63.1k
    if (!_page_iter.valid()) {
2357
0
        *eos = true;
2358
0
        return Status::OK();
2359
0
    }
2360
2361
63.1k
    RETURN_IF_ERROR(_read_data_page(_page_iter));
2362
63.1k
    RETURN_IF_ERROR(_seek_to_pos_in_page(&_page, 0));
2363
63.1k
    *eos = false;
2364
63.1k
    return Status::OK();
2365
63.1k
}
2366
2367
1.66M
Status FileColumnIterator::_read_data_page(const OrdinalPageIndexIterator& iter) {
2368
1.66M
    PageHandle handle;
2369
1.66M
    Slice page_body;
2370
1.66M
    PageFooterPB footer;
2371
1.66M
    _opts.type = DATA_PAGE;
2372
1.66M
    PageDecoderOptions decoder_opts;
2373
1.66M
    decoder_opts.only_read_offsets = _opts.only_read_offsets;
2374
1.66M
    RETURN_IF_ERROR(
2375
1.66M
            _reader->read_page(_opts, iter.page(), &handle, &page_body, &footer, _compress_codec));
2376
    // parse data page
2377
1.66M
    auto st = ParsedPage::create(std::move(handle), page_body, footer.data_page_footer(),
2378
1.66M
                                 _reader->encoding_info(), iter.page(), iter.page_index(), &_page,
2379
1.66M
                                 decoder_opts);
2380
1.66M
    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.66M
    if (_reader->encoding_info()->encoding() == DICT_ENCODING) {
2393
515k
        auto dict_page_decoder = reinterpret_cast<BinaryDictPageDecoder*>(_page.data_decoder.get());
2394
515k
        if (dict_page_decoder->is_dict_encoding()) {
2395
482k
            if (_dict_decoder == nullptr) {
2396
378k
                RETURN_IF_ERROR(_read_dict_data());
2397
378k
                CHECK_NOTNULL(_dict_decoder);
2398
378k
            }
2399
2400
482k
            dict_page_decoder->set_dict_decoder(cast_set<uint32_t>(_dict_decoder->count()),
2401
482k
                                                _dict_word_info.get());
2402
482k
        }
2403
515k
    }
2404
1.66M
    return Status::OK();
2405
1.66M
}
2406
2407
378k
Status FileColumnIterator::_read_dict_data() {
2408
378k
    CHECK_EQ(_reader->encoding_info()->encoding(), DICT_ENCODING);
2409
    // read dictionary page
2410
378k
    Slice dict_data;
2411
378k
    PageFooterPB dict_footer;
2412
378k
    _opts.type = INDEX_PAGE;
2413
2414
378k
    RETURN_IF_ERROR(_reader->read_page(_opts, _reader->get_dict_page_pointer(), &_dict_page_handle,
2415
378k
                                       &dict_data, &dict_footer, _compress_codec, true));
2416
378k
    const EncodingInfo* encoding_info;
2417
378k
    RETURN_IF_ERROR(EncodingInfo::get(FieldType::OLAP_FIELD_TYPE_VARCHAR,
2418
378k
                                      dict_footer.dict_page_footer().encoding(), {},
2419
378k
                                      &encoding_info));
2420
378k
    RETURN_IF_ERROR(encoding_info->create_page_decoder(dict_data, {}, _dict_decoder));
2421
378k
    RETURN_IF_ERROR(_dict_decoder->init());
2422
2423
378k
    _dict_word_info.reset(new StringRef[_dict_decoder->count()]);
2424
378k
    RETURN_IF_ERROR(_dict_decoder->get_dict_word_info(_dict_word_info.get()));
2425
378k
    return Status::OK();
2426
378k
}
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
84.4k
        RowRanges* row_ranges) {
2432
84.4k
    if (_reader->has_zone_map()) {
2433
84.4k
        RETURN_IF_ERROR(_reader->get_row_ranges_by_zone_map(col_predicates, delete_predicates,
2434
84.4k
                                                            row_ranges, _opts));
2435
84.4k
    }
2436
84.4k
    return Status::OK();
2437
84.4k
}
2438
2439
Status FileColumnIterator::get_row_ranges_by_bloom_filter(
2440
84.7k
        const AndBlockColumnPredicate* col_predicates, RowRanges* row_ranges) {
2441
84.7k
    if ((col_predicates->can_do_bloom_filter(false) && _reader->has_bloom_filter_index(false)) ||
2442
84.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
84.7k
    return Status::OK();
2446
84.7k
}
2447
2448
Status FileColumnIterator::get_row_ranges_by_dict(const AndBlockColumnPredicate* col_predicates,
2449
86.9k
                                                  RowRanges* row_ranges) {
2450
86.9k
    if (!_is_all_dict_encoding) {
2451
75.7k
        return Status::OK();
2452
75.7k
    }
2453
2454
11.1k
    if (!_dict_decoder) {
2455
0
        RETURN_IF_ERROR(_read_dict_data());
2456
0
        CHECK_NOTNULL(_dict_decoder);
2457
0
    }
2458
2459
11.1k
    if (!col_predicates->evaluate_and(_dict_word_info.get(), _dict_decoder->count())) {
2460
1.25k
        row_ranges->clear();
2461
1.25k
    }
2462
11.1k
    return Status::OK();
2463
11.1k
}
2464
2465
1.17M
Status FileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) {
2466
1.17M
    if (_cached_remote_file_reader =
2467
1.17M
                std::dynamic_pointer_cast<io::CachedRemoteFileReader>(_reader->_file_reader);
2468
1.17M
        !_cached_remote_file_reader) {
2469
0
        return Status::OK();
2470
0
    }
2471
1.17M
    _enable_prefetch = true;
2472
1.17M
    _prefetcher = std::make_unique<SegmentPrefetcher>(params.config);
2473
1.17M
    RETURN_IF_ERROR(_prefetcher->init(_reader, params.read_options));
2474
1.17M
    return Status::OK();
2475
1.17M
}
2476
2477
void FileColumnIterator::collect_prefetchers(
2478
        std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
2479
1.17M
        PrefetcherInitMethod init_method) {
2480
1.17M
    if (_prefetcher) {
2481
1.17M
        prefetchers[init_method].emplace_back(_prefetcher.get());
2482
1.17M
    }
2483
1.17M
}
2484
2485
47.6k
Status DefaultValueColumnIterator::init(const ColumnIteratorOptions& opts) {
2486
47.6k
    _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
47.6k
    if (_has_default_value) {
2491
1.06k
        if (_default_value == "NULL") {
2492
227
            _default_value_field = Field::create_field<TYPE_NULL>(Null {});
2493
838
        } else {
2494
838
            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
830
            } else if (_type_info->type() == FieldType::OLAP_FIELD_TYPE_STRUCT) {
2502
0
                return Status::NotSupported("STRUCT default type is unsupported");
2503
830
            } else if (_type_info->type() == FieldType::OLAP_FIELD_TYPE_MAP) {
2504
0
                return Status::NotSupported("MAP default type is unsupported");
2505
0
            }
2506
830
            const auto t = _type_info->type();
2507
830
            const auto serde = DataTypeFactory::instance()
2508
830
                                       .create_data_type(t, _precision, _scale, _len)
2509
830
                                       ->get_serde();
2510
830
            RETURN_IF_ERROR(serde->from_fe_string(_default_value, _default_value_field));
2511
830
        }
2512
46.6k
    } else if (_is_nullable) {
2513
46.6k
        _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
47.6k
    return Status::OK();
2519
47.6k
}
2520
2521
2.58k
Status DefaultValueColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
2522
2.58k
    *has_null = _default_value_field.is_null();
2523
2.58k
    _insert_many_default(dst, *n);
2524
2.58k
    return Status::OK();
2525
2.58k
}
2526
2527
Status DefaultValueColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count,
2528
19.0k
                                                  MutableColumnPtr& dst) {
2529
19.0k
    _insert_many_default(dst, count);
2530
19.0k
    return Status::OK();
2531
19.0k
}
2532
2533
21.6k
void DefaultValueColumnIterator::_insert_many_default(MutableColumnPtr& dst, size_t n) {
2534
21.6k
    if (_default_value_field.is_null()) {
2535
21.1k
        dst->insert_many_defaults(n);
2536
21.1k
    } else {
2537
446
        dst = dst->convert_to_predicate_column_if_dictionary();
2538
446
        dst->insert_duplicate_fields(_default_value_field, n);
2539
446
    }
2540
21.6k
}
2541
2542
10.2k
Status RowIdColumnIteratorV2::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
2543
10.2k
    auto* string_column = assert_cast<ColumnString*, TypeCheckOnRelease::DISABLE>(dst.get());
2544
2545
27.4M
    for (uint32_t i = 0; i < *n; ++i) {
2546
27.4M
        uint32_t row_id = _current_rowid + i;
2547
27.4M
        GlobalRowLoacationV2 location(_version, _backend_id, _file_id, row_id);
2548
27.4M
        string_column->insert_data(reinterpret_cast<const char*>(&location),
2549
27.4M
                                   sizeof(GlobalRowLoacationV2));
2550
27.4M
    }
2551
10.2k
    _current_rowid += *n;
2552
10.2k
    return Status::OK();
2553
10.2k
}
2554
2555
Status RowIdColumnIteratorV2::read_by_rowids(const rowid_t* rowids, const size_t count,
2556
8.72k
                                             MutableColumnPtr& dst) {
2557
8.72k
    auto* string_column = assert_cast<ColumnString*>(dst.get());
2558
2559
18.0M
    for (size_t i = 0; i < count; ++i) {
2560
18.0M
        uint32_t row_id = rowids[i];
2561
18.0M
        GlobalRowLoacationV2 location(_version, _backend_id, _file_id, row_id);
2562
18.0M
        string_column->insert_data(reinterpret_cast<const char*>(&location),
2563
18.0M
                                   sizeof(GlobalRowLoacationV2));
2564
18.0M
    }
2565
8.72k
    return Status::OK();
2566
8.72k
}
2567
#include "common/compile_check_end.h"
2568
2569
} // namespace doris::segment_v2