Coverage Report

Created: 2026-06-12 10:57

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