Coverage Report

Created: 2026-03-18 18:05

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