Coverage Report

Created: 2026-03-15 22:41

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
984
inline bool read_as_string(PrimitiveType type) {
88
984
    return type == PrimitiveType::TYPE_STRING || type == PrimitiveType::INVALID_TYPE ||
89
984
           type == PrimitiveType::TYPE_BITMAP || type == PrimitiveType::TYPE_FIXED_LENGTH_OBJECT;
90
984
}
91
92
Status ColumnReader::create_array(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
93
                                  const io::FileReaderSPtr& file_reader,
94
71.1k
                                  std::shared_ptr<ColumnReader>* reader) {
95
71.1k
    DCHECK(meta.children_columns_size() == 2 || meta.children_columns_size() == 3);
96
97
71.1k
    std::shared_ptr<ColumnReader> item_reader;
98
71.1k
    RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(0),
99
71.1k
                                         meta.children_columns(0).num_rows(), file_reader,
100
71.1k
                                         &item_reader));
101
102
71.1k
    std::shared_ptr<ColumnReader> offset_reader;
103
71.1k
    RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(1),
104
71.1k
                                         meta.children_columns(1).num_rows(), file_reader,
105
71.1k
                                         &offset_reader));
106
107
71.1k
    std::shared_ptr<ColumnReader> null_reader;
108
71.1k
    if (meta.is_nullable()) {
109
50.7k
        RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(2),
110
50.7k
                                             meta.children_columns(2).num_rows(), file_reader,
111
50.7k
                                             &null_reader));
112
50.7k
    }
113
114
    // The num rows of the array reader equals to the num rows of the length reader.
115
71.1k
    uint64_t array_num_rows = meta.children_columns(1).num_rows();
116
71.1k
    std::shared_ptr<ColumnReader> array_reader(
117
71.1k
            new ColumnReader(opts, meta, array_num_rows, file_reader));
118
    //  array reader do not need to init
119
71.1k
    array_reader->_sub_readers.resize(meta.children_columns_size());
120
71.1k
    array_reader->_sub_readers[0] = std::move(item_reader);
121
71.1k
    array_reader->_sub_readers[1] = std::move(offset_reader);
122
71.1k
    if (meta.is_nullable()) {
123
50.7k
        array_reader->_sub_readers[2] = std::move(null_reader);
124
50.7k
    }
125
71.1k
    array_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_ARRAY;
126
71.1k
    *reader = std::move(array_reader);
127
71.1k
    return Status::OK();
128
71.1k
}
129
130
Status ColumnReader::create_map(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
131
                                const io::FileReaderSPtr& file_reader,
132
80.0k
                                std::shared_ptr<ColumnReader>* reader) {
133
    // map reader now has 3 sub readers for key, value, offsets(scalar), null(scala)
134
80.0k
    DCHECK(meta.children_columns_size() == 3 || meta.children_columns_size() == 4);
135
80.0k
    std::shared_ptr<ColumnReader> key_reader;
136
80.0k
    RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(0),
137
80.0k
                                         meta.children_columns(0).num_rows(), file_reader,
138
80.0k
                                         &key_reader));
139
80.0k
    std::shared_ptr<ColumnReader> val_reader;
140
80.0k
    RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(1),
141
80.0k
                                         meta.children_columns(1).num_rows(), file_reader,
142
80.0k
                                         &val_reader));
143
80.0k
    std::shared_ptr<ColumnReader> offset_reader;
144
80.0k
    RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(2),
145
80.0k
                                         meta.children_columns(2).num_rows(), file_reader,
146
80.0k
                                         &offset_reader));
147
80.0k
    std::shared_ptr<ColumnReader> null_reader;
148
80.0k
    if (meta.is_nullable()) {
149
15.4k
        RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(3),
150
15.4k
                                             meta.children_columns(3).num_rows(), file_reader,
151
15.4k
                                             &null_reader));
152
15.4k
    }
153
154
    // The num rows of the map reader equals to the num rows of the length reader.
155
80.0k
    uint64_t map_num_rows = meta.children_columns(2).num_rows();
156
80.0k
    std::shared_ptr<ColumnReader> map_reader(
157
80.0k
            new ColumnReader(opts, meta, map_num_rows, file_reader));
158
80.0k
    map_reader->_sub_readers.resize(meta.children_columns_size());
159
160
80.0k
    map_reader->_sub_readers[0] = std::move(key_reader);
161
80.0k
    map_reader->_sub_readers[1] = std::move(val_reader);
162
80.0k
    map_reader->_sub_readers[2] = std::move(offset_reader);
163
80.0k
    if (meta.is_nullable()) {
164
15.4k
        map_reader->_sub_readers[3] = std::move(null_reader);
165
15.4k
    }
166
80.0k
    map_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_MAP;
167
80.0k
    *reader = std::move(map_reader);
168
80.0k
    return Status::OK();
169
80.0k
}
170
171
Status ColumnReader::create_struct(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
172
                                   uint64_t num_rows, const io::FileReaderSPtr& file_reader,
173
8.05k
                                   std::shared_ptr<ColumnReader>* reader) {
174
    // not support empty struct
175
8.05k
    DCHECK(meta.children_columns_size() >= 1);
176
    // create struct column reader
177
8.05k
    std::shared_ptr<ColumnReader> struct_reader(
178
8.05k
            new ColumnReader(opts, meta, num_rows, file_reader));
179
8.05k
    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
42.4k
    for (int i = 0; i < meta.children_columns_size(); i++) {
182
34.3k
        std::shared_ptr<ColumnReader> sub_reader;
183
34.3k
        RETURN_IF_ERROR(ColumnReader::create(opts, meta.children_columns(i),
184
34.3k
                                             meta.children_columns(i).num_rows(), file_reader,
185
34.3k
                                             &sub_reader));
186
34.3k
        struct_reader->_sub_readers.push_back(std::move(sub_reader));
187
34.3k
    }
188
8.05k
    struct_reader->_meta_type = FieldType::OLAP_FIELD_TYPE_STRUCT;
189
8.05k
    *reader = std::move(struct_reader);
190
8.05k
    return Status::OK();
191
8.05k
}
192
193
Status ColumnReader::create_agg_state(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
194
                                      uint64_t num_rows, const io::FileReaderSPtr& file_reader,
195
983
                                      std::shared_ptr<ColumnReader>* reader) {
196
983
    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
983
    auto data_type = DataTypeFactory::instance().create_data_type(meta);
205
983
    const auto* agg_state_type = assert_cast<const DataTypeAggState*>(data_type.get());
206
983
    agg_state_type->check_function_compatibility(opts.be_exec_version);
207
983
    auto type = agg_state_type->get_serialized_type()->get_primitive_type();
208
209
983
    if (read_as_string(type)) {
210
927
        std::shared_ptr<ColumnReader> reader_local(
211
927
                new ColumnReader(opts, meta, num_rows, file_reader));
212
927
        RETURN_IF_ERROR(reader_local->init(&meta));
213
927
        *reader = std::move(reader_local);
214
927
        return Status::OK();
215
927
    } else if (type == PrimitiveType::TYPE_MAP) {
216
43
        return create_map(opts, meta, file_reader, reader);
217
43
    } 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
0
    return Status::InternalError("Not supported type: {}, serialized type: {}",
224
0
                                 agg_state_type->get_name(), int(type));
225
983
}
226
227
141k
bool ColumnReader::is_compaction_reader_type(ReaderType type) {
228
141k
    return type == ReaderType::READER_BASE_COMPACTION ||
229
141k
           type == ReaderType::READER_CUMULATIVE_COMPACTION ||
230
141k
           type == ReaderType::READER_COLD_DATA_COMPACTION ||
231
141k
           type == ReaderType::READER_SEGMENT_COMPACTION ||
232
141k
           type == ReaderType::READER_FULL_COMPACTION;
233
141k
}
234
235
Status ColumnReader::create(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
236
                            uint64_t num_rows, const io::FileReaderSPtr& file_reader,
237
30.1M
                            std::shared_ptr<ColumnReader>* reader) {
238
30.1M
    if (is_scalar_type((FieldType)meta.type())) {
239
29.9M
        std::shared_ptr<ColumnReader> reader_local(
240
29.9M
                new ColumnReader(opts, meta, num_rows, file_reader));
241
29.9M
        RETURN_IF_ERROR(reader_local->init(&meta));
242
29.9M
        *reader = std::move(reader_local);
243
29.9M
        return Status::OK();
244
29.9M
    } else {
245
175k
        auto type = (FieldType)meta.type();
246
175k
        switch (type) {
247
983
        case FieldType::OLAP_FIELD_TYPE_AGG_STATE: {
248
983
            return create_agg_state(opts, meta, num_rows, file_reader, reader);
249
0
        }
250
8.05k
        case FieldType::OLAP_FIELD_TYPE_STRUCT: {
251
8.05k
            return create_struct(opts, meta, num_rows, file_reader, reader);
252
0
        }
253
71.1k
        case FieldType::OLAP_FIELD_TYPE_ARRAY: {
254
71.1k
            return create_array(opts, meta, file_reader, reader);
255
0
        }
256
80.0k
        case FieldType::OLAP_FIELD_TYPE_MAP: {
257
80.0k
            return create_map(opts, meta, file_reader, reader);
258
0
        }
259
21.1k
        case FieldType::OLAP_FIELD_TYPE_VARIANT: {
260
            // Read variant only root data using a single ColumnReader
261
21.1k
            std::shared_ptr<ColumnReader> reader_local(
262
21.1k
                    new ColumnReader(opts, meta, num_rows, file_reader));
263
21.1k
            RETURN_IF_ERROR(reader_local->init(&meta));
264
21.1k
            *reader = std::move(reader_local);
265
21.1k
            return Status::OK();
266
21.1k
        }
267
0
        default:
268
0
            return Status::NotSupported("unsupported type for ColumnReader: {}",
269
0
                                        std::to_string(int(type)));
270
175k
        }
271
175k
    }
272
30.1M
}
273
274
21.1k
ColumnReader::ColumnReader() = default;
275
276
ColumnReader::ColumnReader(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
277
                           uint64_t num_rows, io::FileReaderSPtr file_reader)
278
30.2M
        : _use_index_page_cache(!config::disable_storage_page_cache),
279
30.2M
          _opts(opts),
280
30.2M
          _num_rows(num_rows),
281
30.2M
          _file_reader(std::move(file_reader)),
282
30.2M
          _dict_encoding_type(UNKNOWN_DICT_ENCODING) {
283
30.2M
    _meta_length = meta.length();
284
30.2M
    _meta_type = (FieldType)meta.type();
285
30.2M
    if (_meta_type == FieldType::OLAP_FIELD_TYPE_ARRAY) {
286
71.1k
        _meta_children_column_type = (FieldType)meta.children_columns(0).type();
287
71.1k
    }
288
30.2M
    _data_type = DataTypeFactory::instance().create_data_type(meta);
289
30.2M
    _meta_is_nullable = meta.is_nullable();
290
30.2M
    _meta_dict_page = meta.dict_page();
291
30.2M
    _meta_compression = meta.compression();
292
30.2M
}
293
294
30.3M
ColumnReader::~ColumnReader() = default;
295
296
29.9M
int64_t ColumnReader::get_metadata_size() const {
297
29.9M
    return sizeof(ColumnReader) + (_segment_zone_map ? _segment_zone_map->ByteSizeLong() : 0);
298
29.9M
}
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
29.9M
Status ColumnReader::init(const ColumnMetaPB* meta) {
350
29.9M
    _type_info = get_type_info(meta);
351
352
29.9M
    if (meta->has_be_exec_version()) {
353
29.6M
        _be_exec_version = meta->be_exec_version();
354
29.6M
    }
355
356
29.9M
    if (_type_info == nullptr) {
357
0
        return Status::NotSupported("unsupported typeinfo, type={}", meta->type());
358
0
    }
359
29.9M
    RETURN_IF_ERROR(EncodingInfo::get(_type_info->type(), meta->encoding(), {}, &_encoding_info));
360
361
89.0M
    for (int i = 0; i < meta->indexes_size(); i++) {
362
59.2M
        const auto& index_meta = meta->indexes(i);
363
59.2M
        switch (index_meta.type()) {
364
0
        case BITMAP_INDEX:
365
0
            break;
366
29.8M
        case ORDINAL_INDEX:
367
29.8M
            _ordinal_index.reset(
368
29.8M
                    new OrdinalIndexReader(_file_reader, _num_rows, index_meta.ordinal_index()));
369
29.8M
            break;
370
29.3M
        case ZONE_MAP_INDEX:
371
29.3M
            _segment_zone_map =
372
29.3M
                    std::make_unique<ZoneMapPB>(index_meta.zone_map_index().segment_zone_map());
373
29.3M
            _zone_map_index.reset(new ZoneMapIndexReader(
374
29.3M
                    _file_reader, index_meta.zone_map_index().page_zone_maps()));
375
29.3M
            break;
376
5.32k
        case BLOOM_FILTER_INDEX:
377
5.32k
            _bloom_filter_index.reset(
378
5.32k
                    new BloomFilterIndexReader(_file_reader, index_meta.bloom_filter_index()));
379
5.32k
            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
59.2M
        }
386
59.2M
    }
387
29.8M
    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
29.8M
    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
29.8M
    return Status::OK();
397
29.8M
}
398
399
Status ColumnReader::new_index_iterator(const std::shared_ptr<IndexFileReader>& index_file_reader,
400
                                        const TabletIndex* index_meta,
401
74.5k
                                        std::unique_ptr<IndexIterator>* iterator) {
402
74.5k
    RETURN_IF_ERROR(_load_index(index_file_reader, index_meta));
403
74.5k
    {
404
74.5k
        std::shared_lock<std::shared_mutex> rlock(_load_index_lock);
405
74.5k
        auto iter = _index_readers.find(index_meta->index_id());
406
74.7k
        if (iter != _index_readers.end()) {
407
74.7k
            if (iter->second != nullptr) {
408
74.7k
                RETURN_IF_ERROR(iter->second->new_iterator(iterator));
409
74.7k
            }
410
74.7k
        }
411
74.5k
    }
412
74.5k
    return Status::OK();
413
74.5k
}
414
415
Status ColumnReader::read_page(const ColumnIteratorOptions& iter_opts, const PagePointer& pp,
416
                               PageHandle* handle, Slice* page_body, PageFooterPB* footer,
417
2.19M
                               BlockCompressionCodec* codec, bool is_dict_page) const {
418
2.19M
    SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().column_reader_read_page);
419
2.19M
    iter_opts.sanity_check();
420
2.19M
    PageReadOptions opts(iter_opts.io_ctx);
421
2.19M
    opts.verify_checksum = _opts.verify_checksum;
422
2.19M
    opts.use_page_cache = iter_opts.use_page_cache;
423
2.19M
    opts.kept_in_memory = _opts.kept_in_memory;
424
2.19M
    opts.type = iter_opts.type;
425
2.19M
    opts.file_reader = iter_opts.file_reader;
426
2.19M
    opts.page_pointer = pp;
427
2.19M
    opts.codec = codec;
428
2.19M
    opts.stats = iter_opts.stats;
429
2.19M
    opts.encoding_info = _encoding_info;
430
2.19M
    opts.is_dict_page = is_dict_page;
431
432
2.19M
    return PageIO::read_and_decompress_page(opts, handle, page_body, footer);
433
2.19M
}
434
435
Status ColumnReader::get_row_ranges_by_zone_map(
436
        const AndBlockColumnPredicate* col_predicates,
437
        const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates,
438
80.7k
        RowRanges* row_ranges, const ColumnIteratorOptions& iter_opts) {
439
80.7k
    std::vector<uint32_t> page_indexes;
440
80.7k
    RETURN_IF_ERROR(
441
80.7k
            _get_filtered_pages(col_predicates, delete_predicates, &page_indexes, iter_opts));
442
80.7k
    RETURN_IF_ERROR(_calculate_row_ranges(page_indexes, row_ranges, iter_opts));
443
80.7k
    return Status::OK();
444
80.7k
}
445
446
5.93k
Status ColumnReader::next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) const {
447
5.93k
    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
5.93k
    ZoneMap zone_map;
452
5.93k
    RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map));
453
454
5.93k
    dst->reserve(*n);
455
5.93k
    if (!zone_map.has_not_null) {
456
275
        assert_cast<ColumnNullable&>(*dst).insert_many_defaults(*n);
457
275
        return Status::OK();
458
275
    }
459
5.66k
    dst->insert(zone_map.max_value);
460
11.4k
    for (int i = 1; i < *n; ++i) {
461
5.83k
        dst->insert(zone_map.min_value);
462
5.83k
    }
463
5.66k
    return Status::OK();
464
5.93k
}
465
466
Status ColumnReader::match_condition(const AndBlockColumnPredicate* col_predicates,
467
1.85M
                                     bool* matched) const {
468
1.85M
    *matched = true;
469
1.85M
    if (_zone_map_index == nullptr) {
470
0
        return Status::OK();
471
0
    }
472
1.85M
    ZoneMap zone_map;
473
1.85M
    RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map));
474
475
1.85M
    *matched = _zone_map_match_condition(zone_map, col_predicates);
476
1.85M
    return Status::OK();
477
1.85M
}
478
479
Status ColumnReader::prune_predicates_by_zone_map(
480
        std::vector<std::shared_ptr<ColumnPredicate>>& predicates, const int column_id,
481
1.74M
        bool* pruned) const {
482
1.74M
    *pruned = false;
483
1.74M
    if (_zone_map_index == nullptr) {
484
153
        return Status::OK();
485
153
    }
486
487
1.74M
    ZoneMap zone_map;
488
1.74M
    RETURN_IF_ERROR(ZoneMap::from_proto(*_segment_zone_map, _data_type, zone_map));
489
1.74M
    if (zone_map.pass_all) {
490
0
        return Status::OK();
491
0
    }
492
493
3.63M
    for (auto it = predicates.begin(); it != predicates.end();) {
494
1.89M
        auto predicate = *it;
495
1.89M
        if (predicate->column_id() == column_id && predicate->is_always_true(zone_map)) {
496
12.9k
            *pruned = true;
497
12.9k
            it = predicates.erase(it);
498
1.87M
        } else {
499
1.87M
            ++it;
500
1.87M
        }
501
1.89M
    }
502
1.74M
    return Status::OK();
503
1.74M
}
504
505
bool ColumnReader::_zone_map_match_condition(const ZoneMap& zone_map,
506
1.92M
                                             const AndBlockColumnPredicate* col_predicates) const {
507
1.92M
    if (zone_map.pass_all) {
508
0
        return true;
509
0
    }
510
511
1.92M
    return col_predicates->evaluate_and(zone_map);
512
1.92M
}
513
514
Status ColumnReader::_get_filtered_pages(
515
        const AndBlockColumnPredicate* col_predicates,
516
        const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates,
517
80.5k
        std::vector<uint32_t>* page_indexes, const ColumnIteratorOptions& iter_opts) {
518
80.5k
    RETURN_IF_ERROR(_load_zone_map_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts));
519
520
80.5k
    const std::vector<ZoneMapPB>& zone_maps = _zone_map_index->page_zone_maps();
521
80.5k
    size_t page_size = _zone_map_index->num_pages();
522
195k
    for (size_t i = 0; i < page_size; ++i) {
523
114k
        if (zone_maps[i].pass_all()) {
524
47.2k
            page_indexes->push_back(cast_set<uint32_t>(i));
525
67.6k
        } else {
526
67.6k
            segment_v2::ZoneMap zone_map;
527
67.6k
            RETURN_IF_ERROR(ZoneMap::from_proto(zone_maps[i], _data_type, zone_map));
528
67.6k
            if (_zone_map_match_condition(zone_map, col_predicates)) {
529
66.3k
                bool should_read = true;
530
66.3k
                if (delete_predicates != nullptr) {
531
16
                    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
16
                        if (del_pred->evaluate_del(zone_map)) {
535
1
                            should_read = false;
536
1
                            break;
537
1
                        }
538
16
                    }
539
15
                }
540
66.3k
                if (should_read) {
541
66.3k
                    page_indexes->push_back(cast_set<uint32_t>(i));
542
66.3k
                }
543
66.3k
            }
544
67.6k
        }
545
114k
    }
546
18.4E
    VLOG(1) << "total-pages: " << page_size << " not-filtered-pages: " << page_indexes->size()
547
18.4E
            << " filtered-percent:"
548
18.4E
            << 1.0 - (static_cast<double>(page_indexes->size()) /
549
18.4E
                      (static_cast<double>(page_size) * 1.0));
550
80.5k
    return Status::OK();
551
80.5k
}
552
553
Status ColumnReader::_calculate_row_ranges(const std::vector<uint32_t>& page_indexes,
554
                                           RowRanges* row_ranges,
555
81.3k
                                           const ColumnIteratorOptions& iter_opts) {
556
81.3k
    row_ranges->clear();
557
81.3k
    RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts));
558
113k
    for (auto i : page_indexes) {
559
113k
        ordinal_t page_first_id = _ordinal_index->get_first_ordinal(i);
560
113k
        ordinal_t page_last_id = _ordinal_index->get_last_ordinal(i);
561
113k
        RowRanges page_row_ranges(RowRanges::create_single(page_first_id, page_last_id + 1));
562
113k
        RowRanges::ranges_union(*row_ranges, page_row_ranges, row_ranges);
563
113k
    }
564
81.3k
    return Status::OK();
565
81.3k
}
566
567
Status ColumnReader::get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates,
568
                                                    RowRanges* row_ranges,
569
95
                                                    const ColumnIteratorOptions& iter_opts) {
570
95
    RETURN_IF_ERROR(_load_ordinal_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts));
571
95
    RETURN_IF_ERROR(
572
95
            _load_bloom_filter_index(_use_index_page_cache, _opts.kept_in_memory, iter_opts));
573
95
    RowRanges bf_row_ranges;
574
95
    std::unique_ptr<BloomFilterIndexIterator> bf_iter;
575
95
    RETURN_IF_ERROR(_bloom_filter_index->new_iterator(&bf_iter, iter_opts.stats));
576
95
    size_t range_size = row_ranges->range_size();
577
    // get covered page ids
578
95
    std::set<uint32_t> page_ids;
579
190
    for (int i = 0; i < range_size; ++i) {
580
95
        int64_t from = row_ranges->get_range_from(i);
581
95
        int64_t idx = from;
582
95
        int64_t to = row_ranges->get_range_to(i);
583
95
        auto iter = _ordinal_index->seek_at_or_before(from);
584
216
        while (idx < to && iter.valid()) {
585
121
            page_ids.insert(iter.page_index());
586
121
            idx = iter.last_ordinal() + 1;
587
121
            iter.next();
588
121
        }
589
95
    }
590
121
    for (auto& pid : page_ids) {
591
121
        std::unique_ptr<BloomFilter> bf;
592
121
        RETURN_IF_ERROR(bf_iter->read_bloom_filter(pid, &bf));
593
121
        if (col_predicates->evaluate_and(bf.get())) {
594
20
            bf_row_ranges.add(RowRange(_ordinal_index->get_first_ordinal(pid),
595
20
                                       _ordinal_index->get_last_ordinal(pid) + 1));
596
20
        }
597
121
    }
598
95
    RowRanges::ranges_intersection(*row_ranges, bf_row_ranges, row_ranges);
599
95
    return Status::OK();
600
95
}
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
80.6k
                                          const ColumnIteratorOptions& iter_opts) {
612
80.7k
    if (_zone_map_index != nullptr) {
613
80.7k
        return _zone_map_index->load(use_page_cache, kept_in_memory, iter_opts.stats);
614
80.7k
    }
615
18.4E
    return Status::OK();
616
80.6k
}
617
618
Status ColumnReader::_load_index(const std::shared_ptr<IndexFileReader>& index_file_reader,
619
74.5k
                                 const TabletIndex* index_meta) {
620
74.5k
    std::unique_lock<std::shared_mutex> wlock(_load_index_lock);
621
622
74.5k
    if (index_meta == nullptr) {
623
0
        return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>(
624
0
                "Failed to load inverted index: index metadata is null");
625
0
    }
626
627
74.5k
    auto it = _index_readers.find(index_meta->index_id());
628
74.5k
    if (it != _index_readers.end()) {
629
0
        return Status::OK();
630
0
    }
631
632
74.5k
    bool should_analyzer =
633
74.5k
            inverted_index::InvertedIndexAnalyzer::should_analyzer(index_meta->properties());
634
635
74.5k
    FieldType type;
636
74.5k
    if (_meta_type == FieldType::OLAP_FIELD_TYPE_ARRAY) {
637
6.00k
        type = _meta_children_column_type;
638
68.5k
    } else {
639
68.5k
        type = _type_info->type();
640
68.5k
    }
641
642
74.5k
    if (index_meta->index_type() == IndexType::ANN) {
643
148
        _index_readers[index_meta->index_id()] =
644
148
                std::make_shared<AnnIndexReader>(index_meta, index_file_reader);
645
148
        return Status::OK();
646
148
    }
647
648
74.4k
    IndexReaderPtr index_reader;
649
650
74.4k
    if (is_string_type(type)) {
651
37.0k
        if (should_analyzer) {
652
21.0k
            try {
653
21.0k
                index_reader = FullTextIndexReader::create_shared(index_meta, index_file_reader);
654
21.0k
            } 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.0k
        } else {
659
15.9k
            try {
660
15.9k
                index_reader =
661
15.9k
                        StringTypeInvertedIndexReader::create_shared(index_meta, index_file_reader);
662
15.9k
            } catch (const CLuceneError& e) {
663
0
                return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>(
664
0
                        "create StringTypeInvertedIndexReader error: {}", e.what());
665
0
            }
666
15.9k
        }
667
37.4k
    } else if (is_numeric_type(type)) {
668
37.4k
        try {
669
37.4k
            index_reader = BkdIndexReader::create_shared(index_meta, index_file_reader);
670
37.4k
        } catch (const CLuceneError& e) {
671
0
            return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>(
672
0
                    "create BkdIndexReader error: {}", e.what());
673
0
        }
674
18.4E
    } else {
675
18.4E
        return Status::Error<ErrorCode::INVERTED_INDEX_NOT_SUPPORTED>(
676
18.4E
                "Field type {} is not supported for inverted index", type);
677
18.4E
    }
678
74.5k
    _index_readers[index_meta->index_id()] = index_reader;
679
74.5k
    return Status::OK();
680
74.4k
}
681
682
58.1k
bool ColumnReader::has_bloom_filter_index(bool ngram) const {
683
58.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
95
                                              const ColumnIteratorOptions& iter_opts) {
694
95
    if (_bloom_filter_index != nullptr) {
695
95
        return _bloom_filter_index->load(use_page_cache, kept_in_memory, iter_opts.stats);
696
95
    }
697
0
    return Status::OK();
698
95
}
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
346k
                                              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
346k
    RETURN_IF_ERROR(
715
346k
            _ordinal_index->load(_use_index_page_cache, _opts.kept_in_memory, index_load_stats));
716
346k
    reader = _ordinal_index.get();
717
346k
    return Status::OK();
718
346k
}
719
720
416k
Status ColumnReader::new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column) {
721
416k
    return new_iterator(iterator, tablet_column, nullptr);
722
416k
}
723
724
Status ColumnReader::new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column,
725
28.0M
                                  const StorageReadOptions* opt) {
726
28.0M
    if (is_empty()) {
727
46.6k
        *iterator = std::make_unique<EmptyFileColumnIterator>();
728
46.6k
        return Status::OK();
729
46.6k
    }
730
28.0M
    if (is_scalar_type(_meta_type)) {
731
27.9M
        *iterator = std::make_unique<FileColumnIterator>(shared_from_this());
732
27.9M
        (*iterator)->set_column_name(tablet_column ? tablet_column->name() : "");
733
27.9M
        return Status::OK();
734
27.9M
    } else {
735
77.9k
        auto type = _meta_type;
736
77.9k
        switch (type) {
737
927
        case FieldType::OLAP_FIELD_TYPE_AGG_STATE: {
738
927
            return new_agg_state_iterator(iterator);
739
0
        }
740
7.98k
        case FieldType::OLAP_FIELD_TYPE_STRUCT: {
741
7.98k
            return new_struct_iterator(iterator, tablet_column);
742
0
        }
743
67.1k
        case FieldType::OLAP_FIELD_TYPE_ARRAY: {
744
67.1k
            return new_array_iterator(iterator, tablet_column);
745
0
        }
746
35.6k
        case FieldType::OLAP_FIELD_TYPE_MAP: {
747
35.6k
            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
77.9k
        }
753
77.9k
    }
754
28.0M
}
755
756
924
Status ColumnReader::new_agg_state_iterator(ColumnIteratorUPtr* iterator) {
757
924
    *iterator = std::make_unique<FileColumnIterator>(shared_from_this());
758
924
    return Status::OK();
759
924
}
760
761
Status ColumnReader::new_array_iterator(ColumnIteratorUPtr* iterator,
762
67.1k
                                        const TabletColumn* tablet_column) {
763
67.1k
    ColumnIteratorUPtr item_iterator;
764
67.1k
    RETURN_IF_ERROR(_sub_readers[0]->new_iterator(
765
67.1k
            &item_iterator, tablet_column && tablet_column->get_subtype_count() > 0
766
67.1k
                                    ? &tablet_column->get_sub_column(0)
767
67.1k
                                    : nullptr));
768
769
67.1k
    item_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(0).name() : "");
770
771
67.1k
    ColumnIteratorUPtr offset_iterator;
772
67.1k
    RETURN_IF_ERROR(_sub_readers[1]->new_iterator(&offset_iterator, nullptr));
773
67.1k
    auto* file_iter = static_cast<FileColumnIterator*>(offset_iterator.release());
774
67.1k
    OffsetFileColumnIteratorUPtr ofcIter = std::make_unique<OffsetFileColumnIterator>(
775
67.1k
            std::unique_ptr<FileColumnIterator>(file_iter));
776
777
67.1k
    ColumnIteratorUPtr null_iterator;
778
67.1k
    if (is_nullable()) {
779
47.4k
        RETURN_IF_ERROR(_sub_readers[2]->new_iterator(&null_iterator, nullptr));
780
47.4k
    }
781
67.1k
    *iterator = std::make_unique<ArrayFileColumnIterator>(shared_from_this(), std::move(ofcIter),
782
67.1k
                                                          std::move(item_iterator),
783
67.1k
                                                          std::move(null_iterator));
784
67.1k
    return Status::OK();
785
67.1k
}
786
787
Status ColumnReader::new_map_iterator(ColumnIteratorUPtr* iterator,
788
35.6k
                                      const TabletColumn* tablet_column) {
789
35.6k
    ColumnIteratorUPtr key_iterator;
790
35.6k
    RETURN_IF_ERROR(_sub_readers[0]->new_iterator(
791
35.6k
            &key_iterator, tablet_column && tablet_column->get_subtype_count() > 1
792
35.6k
                                   ? &tablet_column->get_sub_column(0)
793
35.6k
                                   : nullptr));
794
35.6k
    key_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(0).name() : "");
795
35.6k
    ColumnIteratorUPtr val_iterator;
796
35.6k
    RETURN_IF_ERROR(_sub_readers[1]->new_iterator(
797
35.6k
            &val_iterator, tablet_column && tablet_column->get_subtype_count() > 1
798
35.6k
                                   ? &tablet_column->get_sub_column(1)
799
35.6k
                                   : nullptr));
800
35.6k
    val_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(1).name() : "");
801
35.6k
    ColumnIteratorUPtr offsets_iterator;
802
35.6k
    RETURN_IF_ERROR(_sub_readers[2]->new_iterator(&offsets_iterator, nullptr));
803
35.6k
    auto* file_iter = static_cast<FileColumnIterator*>(offsets_iterator.release());
804
35.6k
    OffsetFileColumnIteratorUPtr ofcIter = std::make_unique<OffsetFileColumnIterator>(
805
35.6k
            std::unique_ptr<FileColumnIterator>(file_iter));
806
807
35.6k
    ColumnIteratorUPtr null_iterator;
808
35.6k
    if (is_nullable()) {
809
15.2k
        RETURN_IF_ERROR(_sub_readers[3]->new_iterator(&null_iterator, nullptr));
810
15.2k
    }
811
35.6k
    *iterator = std::make_unique<MapFileColumnIterator>(
812
35.6k
            shared_from_this(), std::move(null_iterator), std::move(ofcIter),
813
35.6k
            std::move(key_iterator), std::move(val_iterator));
814
35.6k
    return Status::OK();
815
35.6k
}
816
817
Status ColumnReader::new_struct_iterator(ColumnIteratorUPtr* iterator,
818
7.97k
                                         const TabletColumn* tablet_column) {
819
7.97k
    std::vector<ColumnIteratorUPtr> sub_column_iterators;
820
7.97k
    size_t child_size = is_nullable() ? _sub_readers.size() - 1 : _sub_readers.size();
821
18.4E
    size_t tablet_column_size = tablet_column ? tablet_column->get_sub_columns().size() : 0;
822
7.97k
    sub_column_iterators.reserve(child_size);
823
824
34.7k
    for (uint64_t i = 0; i < child_size; i++) {
825
26.7k
        ColumnIteratorUPtr sub_column_iterator;
826
26.7k
        RETURN_IF_ERROR(_sub_readers[i]->new_iterator(
827
26.7k
                &sub_column_iterator, tablet_column ? &tablet_column->get_sub_column(i) : nullptr));
828
26.7k
        sub_column_iterator->set_column_name(tablet_column ? tablet_column->get_sub_column(i).name()
829
26.7k
                                                           : "");
830
26.7k
        sub_column_iterators.emplace_back(std::move(sub_column_iterator));
831
26.7k
    }
832
833
    // create default_iterator for schema-change behavior which increase column
834
9.27k
    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.97k
    ColumnIteratorUPtr null_iterator;
843
7.97k
    if (is_nullable()) {
844
7.12k
        RETURN_IF_ERROR(_sub_readers[child_size]->new_iterator(&null_iterator, nullptr));
845
7.12k
    }
846
7.97k
    *iterator = std::make_unique<StructFileColumnIterator>(
847
7.97k
            shared_from_this(), std::move(null_iterator), std::move(sub_column_iterators));
848
7.97k
    return Status::OK();
849
7.97k
}
850
851
Result<TColumnAccessPaths> ColumnIterator::_get_sub_access_paths(
852
116k
        const TColumnAccessPaths& access_paths) {
853
116k
    TColumnAccessPaths sub_access_paths = access_paths;
854
177k
    for (auto it = sub_access_paths.begin(); it != sub_access_paths.end();) {
855
61.2k
        TColumnAccessPath& name_path = *it;
856
61.2k
        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
61.2k
        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
61.2k
        name_path.data_access_path.path.erase(name_path.data_access_path.path.begin());
868
61.2k
        if (!name_path.data_access_path.path.empty()) {
869
4.14k
            ++it;
870
57.1k
        } else {
871
57.1k
            set_need_to_read();
872
57.1k
            it = sub_access_paths.erase(it);
873
57.1k
        }
874
61.2k
    }
875
116k
    return sub_access_paths;
876
116k
}
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
35.6k
        : _map_reader(reader),
885
35.6k
          _offsets_iterator(std::move(offsets_iterator)),
886
35.6k
          _key_iterator(std::move(key_iterator)),
887
35.6k
          _val_iterator(std::move(val_iterator)) {
888
35.6k
    if (_map_reader->is_nullable()) {
889
15.3k
        _null_iterator = std::move(null_iterator);
890
15.3k
    }
891
35.6k
}
892
893
35.4k
Status MapFileColumnIterator::init(const ColumnIteratorOptions& opts) {
894
35.4k
    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
35.4k
    RETURN_IF_ERROR(_key_iterator->init(opts));
899
35.4k
    RETURN_IF_ERROR(_val_iterator->init(opts));
900
35.4k
    RETURN_IF_ERROR(_offsets_iterator->init(opts));
901
35.4k
    if (_map_reader->is_nullable()) {
902
15.2k
        RETURN_IF_ERROR(_null_iterator->init(opts));
903
15.2k
    }
904
35.4k
    return Status::OK();
905
35.4k
}
906
907
18.9k
Status MapFileColumnIterator::seek_to_ordinal(ordinal_t ord) {
908
18.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
18.9k
    if (_map_reader->is_nullable()) {
914
10.7k
        RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord));
915
10.7k
    }
916
18.9k
    RETURN_IF_ERROR(_offsets_iterator->seek_to_ordinal(ord));
917
    // here to use offset info
918
18.9k
    ordinal_t offset = 0;
919
18.9k
    RETURN_IF_ERROR(_offsets_iterator->_peek_one_offset(&offset));
920
18.9k
    RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(offset));
921
18.9k
    RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(offset));
922
18.9k
    return Status::OK();
923
18.9k
}
924
925
1.74k
Status MapFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) {
926
1.74k
    RETURN_IF_ERROR(_offsets_iterator->init_prefetcher(params));
927
1.74k
    if (_map_reader->is_nullable()) {
928
1.71k
        RETURN_IF_ERROR(_null_iterator->init_prefetcher(params));
929
1.71k
    }
930
1.74k
    RETURN_IF_ERROR(_key_iterator->init_prefetcher(params));
931
1.74k
    RETURN_IF_ERROR(_val_iterator->init_prefetcher(params));
932
1.74k
    return Status::OK();
933
1.74k
}
934
935
void MapFileColumnIterator::collect_prefetchers(
936
        std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
937
1.74k
        PrefetcherInitMethod init_method) {
938
1.74k
    _offsets_iterator->collect_prefetchers(prefetchers, init_method);
939
1.74k
    if (_map_reader->is_nullable()) {
940
1.71k
        _null_iterator->collect_prefetchers(prefetchers, init_method);
941
1.71k
    }
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
1.74k
    _key_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS);
945
1.74k
    _val_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS);
946
1.74k
}
947
948
18.9k
Status MapFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
949
18.9k
    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
18.9k
    auto& column_map = assert_cast<ColumnMap&, TypeCheckOnRelease::DISABLE>(
956
18.9k
            dst->is_nullable() ? static_cast<ColumnNullable&>(*dst).get_nested_column() : *dst);
957
18.9k
    auto column_offsets_ptr = column_map.get_offsets_column().assume_mutable();
958
18.9k
    bool offsets_has_null = false;
959
18.9k
    ssize_t start = column_offsets_ptr->size();
960
18.9k
    RETURN_IF_ERROR(_offsets_iterator->next_batch(n, column_offsets_ptr, &offsets_has_null));
961
18.9k
    if (*n == 0) {
962
0
        return Status::OK();
963
0
    }
964
18.9k
    auto& column_offsets = static_cast<ColumnArray::ColumnOffsets&>(*column_offsets_ptr);
965
18.9k
    RETURN_IF_ERROR(_offsets_iterator->_calculate_offsets(start, column_offsets));
966
18.9k
    DCHECK(column_offsets.get_data().back() >= column_offsets.get_data()[start - 1]);
967
18.9k
    size_t num_items =
968
18.9k
            column_offsets.get_data().back() - column_offsets.get_data()[start - 1]; // -1 is valid
969
18.9k
    auto key_ptr = column_map.get_keys().assume_mutable();
970
18.9k
    auto val_ptr = column_map.get_values().assume_mutable();
971
972
18.9k
    if (num_items > 0) {
973
15.0k
        size_t num_read = num_items;
974
15.0k
        bool key_has_null = false;
975
15.0k
        bool val_has_null = false;
976
15.0k
        RETURN_IF_ERROR(_key_iterator->next_batch(&num_read, key_ptr, &key_has_null));
977
15.0k
        RETURN_IF_ERROR(_val_iterator->next_batch(&num_read, val_ptr, &val_has_null));
978
15.0k
        DCHECK(num_read == num_items);
979
980
15.0k
        column_map.get_keys_ptr() = std::move(key_ptr);
981
15.0k
        column_map.get_values_ptr() = std::move(val_ptr);
982
15.0k
    }
983
984
18.9k
    if (dst->is_nullable()) {
985
10.7k
        size_t num_read = *n;
986
10.7k
        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
10.7k
        if (_null_iterator) {
992
10.7k
            bool null_signs_has_null = false;
993
10.7k
            RETURN_IF_ERROR(
994
10.7k
                    _null_iterator->next_batch(&num_read, null_map_ptr, &null_signs_has_null));
995
10.7k
        } 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
10.7k
        DCHECK(num_read == *n);
1000
10.7k
    }
1001
18.9k
    return Status::OK();
1002
18.9k
}
1003
1004
Status MapFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count,
1005
17.5k
                                             MutableColumnPtr& dst) {
1006
17.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
17.5k
    if (count == 0) {
1012
0
        return Status::OK();
1013
0
    }
1014
    // resolve ColumnMap and nullable wrapper
1015
17.5k
    const auto* column_map = check_and_get_column<ColumnMap>(
1016
17.5k
            dst->is_nullable() ? static_cast<ColumnNullable&>(*dst).get_nested_column() : *dst);
1017
17.5k
    auto offsets_ptr = column_map->get_offsets_column().assume_mutable();
1018
17.5k
    auto& offsets = static_cast<ColumnArray::ColumnOffsets&>(*offsets_ptr);
1019
17.5k
    size_t base = offsets.get_data().empty() ? 0 : offsets.get_data().back();
1020
1021
    // 1. bulk read null-map if nullable
1022
17.5k
    std::vector<uint8_t> null_mask; // 0: not null, 1: null
1023
17.5k
    if (_map_reader->is_nullable()) {
1024
        // For nullable map columns, the destination column must also be nullable.
1025
3.20k
        if (UNLIKELY(!dst->is_nullable())) {
1026
0
            return Status::InternalError(
1027
0
                    "unexpected non-nullable destination column for nullable map reader");
1028
0
        }
1029
3.20k
        auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr();
1030
3.20k
        size_t null_before = null_map_ptr->size();
1031
3.20k
        RETURN_IF_ERROR(_null_iterator->read_by_rowids(rowids, count, null_map_ptr));
1032
        // extract a light-weight view to decide element reads
1033
3.20k
        auto& null_map_col = assert_cast<ColumnUInt8&>(*null_map_ptr);
1034
3.20k
        null_mask.reserve(count);
1035
31.1k
        for (size_t i = 0; i < count; ++i) {
1036
27.9k
            null_mask.push_back(null_map_col.get_element(null_before + i));
1037
27.9k
        }
1038
14.3k
    } 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
17.5k
    MutableColumnPtr starts_col = ColumnOffset64::create();
1050
17.5k
    starts_col->reserve(count);
1051
17.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
17.5k
    std::vector<rowid_t> next_rowids(count);
1055
1.64M
    for (size_t i = 0; i < count; ++i) {
1056
1.62M
        uint64_t nr = rowids[i] + 1;
1057
1.62M
        next_rowids[i] = nr < _map_reader->num_rows() ? static_cast<rowid_t>(nr)
1058
1.62M
                                                      : static_cast<rowid_t>(0); // placeholder
1059
1.62M
    }
1060
17.5k
    MutableColumnPtr next_starts_col = ColumnOffset64::create();
1061
17.5k
    next_starts_col->reserve(count);
1062
    // read for all; we'll fix out-of-bound cases below
1063
17.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
1.64M
    for (size_t i = 0; i < count; ++i) {
1067
1.62M
        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.1k
            RETURN_IF_ERROR(_offsets_iterator->seek_to_ordinal(rowids[i]));
1071
14.1k
            size_t one = 1;
1072
14.1k
            bool has_null_unused = false;
1073
14.1k
            MutableColumnPtr tmp = ColumnOffset64::create();
1074
14.1k
            RETURN_IF_ERROR(_offsets_iterator->next_batch(&one, tmp, &has_null_unused));
1075
14.1k
            ordinal_t ns = 0;
1076
14.1k
            RETURN_IF_ERROR(_offsets_iterator->_peek_one_offset(&ns));
1077
            // overwrite with sentinel
1078
14.1k
            assert_cast<ColumnOffset64&, TypeCheckOnRelease::DISABLE>(*next_starts_col)
1079
14.1k
                    .get_data()[i] = ns;
1080
14.1k
        }
1081
1.62M
    }
1082
1083
    // 5. compute sizes and append offsets prefix-sum
1084
17.5k
    auto& starts_data = assert_cast<ColumnOffset64&>(*starts_col).get_data();
1085
17.5k
    auto& next_starts_data = assert_cast<ColumnOffset64&>(*next_starts_col).get_data();
1086
17.5k
    std::vector<size_t> sizes(count, 0);
1087
17.5k
    size_t acc = base;
1088
17.5k
    const auto original_size = offsets.get_data().back();
1089
17.5k
    offsets.get_data().reserve(offsets.get_data().size() + count);
1090
1.61M
    for (size_t i = 0; i < count; ++i) {
1091
1.60M
        size_t sz = static_cast<size_t>(next_starts_data[i] - starts_data[i]);
1092
1.60M
        if (_map_reader->is_nullable() && !null_mask.empty() && null_mask[i]) {
1093
758
            sz = 0; // null rows do not consume elements
1094
758
        }
1095
1.60M
        sizes[i] = sz;
1096
1.60M
        acc += sz;
1097
1.60M
        offsets.get_data().push_back(acc);
1098
1.60M
    }
1099
1100
    // 6. read key/value elements for non-empty sizes
1101
17.5k
    auto keys_ptr = column_map->get_keys().assume_mutable();
1102
17.5k
    auto vals_ptr = column_map->get_values().assume_mutable();
1103
1104
17.5k
    size_t this_run = sizes[0];
1105
17.5k
    auto start_idx = starts_data[0];
1106
17.5k
    auto last_idx = starts_data[0] + this_run;
1107
1.61M
    for (size_t i = 1; i < count; ++i) {
1108
1.59M
        size_t sz = sizes[i];
1109
1.59M
        if (sz == 0) {
1110
43.6k
            continue;
1111
43.6k
        }
1112
1.55M
        auto start = static_cast<ordinal_t>(starts_data[i]);
1113
1.55M
        if (start != last_idx) {
1114
301k
            size_t n = this_run;
1115
301k
            bool dummy_has_null = false;
1116
1117
301k
            if (this_run != 0) {
1118
301k
                if (_key_iterator->reading_flag() != ReadingFlag::SKIP_READING) {
1119
301k
                    RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(start_idx));
1120
301k
                    RETURN_IF_ERROR(_key_iterator->next_batch(&n, keys_ptr, &dummy_has_null));
1121
301k
                    DCHECK(n == this_run);
1122
301k
                }
1123
1124
301k
                if (_val_iterator->reading_flag() != ReadingFlag::SKIP_READING) {
1125
300k
                    n = this_run;
1126
300k
                    RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(start_idx));
1127
300k
                    RETURN_IF_ERROR(_val_iterator->next_batch(&n, vals_ptr, &dummy_has_null));
1128
300k
                    DCHECK(n == this_run);
1129
300k
                }
1130
301k
            }
1131
301k
            start_idx = start;
1132
301k
            this_run = sz;
1133
301k
            last_idx = start + sz;
1134
301k
            continue;
1135
301k
        }
1136
1137
1.24M
        this_run += sz;
1138
1.24M
        last_idx += sz;
1139
1.24M
    }
1140
1141
17.5k
    size_t n = this_run;
1142
17.5k
    const size_t total_count = offsets.get_data().back() - original_size;
1143
17.5k
    bool dummy_has_null = false;
1144
17.5k
    if (_key_iterator->reading_flag() != ReadingFlag::SKIP_READING) {
1145
17.5k
        if (this_run != 0) {
1146
7.41k
            RETURN_IF_ERROR(_key_iterator->seek_to_ordinal(start_idx));
1147
7.41k
            RETURN_IF_ERROR(_key_iterator->next_batch(&n, keys_ptr, &dummy_has_null));
1148
7.41k
            DCHECK(n == this_run);
1149
7.41k
        }
1150
17.5k
    } else {
1151
7
        keys_ptr->insert_many_defaults(total_count);
1152
7
    }
1153
1154
17.5k
    if (_val_iterator->reading_flag() != ReadingFlag::SKIP_READING) {
1155
17.5k
        if (this_run != 0) {
1156
7.41k
            n = this_run;
1157
7.41k
            RETURN_IF_ERROR(_val_iterator->seek_to_ordinal(start_idx));
1158
7.41k
            RETURN_IF_ERROR(_val_iterator->next_batch(&n, vals_ptr, &dummy_has_null));
1159
7.41k
            DCHECK(n == this_run);
1160
7.41k
        }
1161
17.5k
    } else {
1162
9
        vals_ptr->insert_many_defaults(total_count);
1163
9
    }
1164
1165
17.5k
    return Status::OK();
1166
17.5k
}
1167
1168
6.18k
void MapFileColumnIterator::set_need_to_read() {
1169
6.18k
    set_reading_flag(ReadingFlag::NEED_TO_READ);
1170
6.18k
    _key_iterator->set_need_to_read();
1171
6.18k
    _val_iterator->set_need_to_read();
1172
6.18k
}
1173
1174
6.82k
void MapFileColumnIterator::remove_pruned_sub_iterators() {
1175
6.82k
    _key_iterator->remove_pruned_sub_iterators();
1176
6.82k
    _val_iterator->remove_pruned_sub_iterators();
1177
6.82k
}
1178
1179
Status MapFileColumnIterator::set_access_paths(const TColumnAccessPaths& all_access_paths,
1180
6.57k
                                               const TColumnAccessPaths& predicate_access_paths) {
1181
6.57k
    if (all_access_paths.empty()) {
1182
1.51k
        return Status::OK();
1183
1.51k
    }
1184
1185
5.05k
    if (!predicate_access_paths.empty()) {
1186
51
        set_reading_flag(ReadingFlag::READING_FOR_PREDICATE);
1187
51
        DLOG(INFO) << "Map column iterator set sub-column " << _column_name
1188
51
                   << " to READING_FOR_PREDICATE";
1189
51
    }
1190
1191
5.05k
    auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths));
1192
5.05k
    auto sub_predicate_access_paths = DORIS_TRY(_get_sub_access_paths(predicate_access_paths));
1193
1194
5.05k
    if (sub_all_access_paths.empty()) {
1195
4.37k
        return Status::OK();
1196
4.37k
    }
1197
1198
682
    TColumnAccessPaths key_all_access_paths;
1199
682
    TColumnAccessPaths val_all_access_paths;
1200
682
    TColumnAccessPaths key_predicate_access_paths;
1201
682
    TColumnAccessPaths val_predicate_access_paths;
1202
1203
721
    for (auto paths : sub_all_access_paths) {
1204
721
        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
551
        } else if (paths.data_access_path.path[0] == "KEYS") {
1210
267
            paths.data_access_path.path[0] = _key_iterator->column_name();
1211
267
            key_all_access_paths.emplace_back(paths);
1212
284
        } else if (paths.data_access_path.path[0] == "VALUES") {
1213
284
            paths.data_access_path.path[0] = _val_iterator->column_name();
1214
284
            val_all_access_paths.emplace_back(paths);
1215
284
        }
1216
721
    }
1217
682
    const auto need_read_keys = !key_all_access_paths.empty();
1218
682
    const auto need_read_values = !val_all_access_paths.empty();
1219
1220
682
    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
682
    if (need_read_keys) {
1236
436
        _key_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ);
1237
436
        RETURN_IF_ERROR(
1238
436
                _key_iterator->set_access_paths(key_all_access_paths, key_predicate_access_paths));
1239
436
    } else {
1240
246
        _key_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1241
246
        DLOG(INFO) << "Map column iterator set key column to SKIP_READING";
1242
246
    }
1243
1244
682
    if (need_read_values) {
1245
453
        _val_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ);
1246
453
        RETURN_IF_ERROR(
1247
453
                _val_iterator->set_access_paths(val_all_access_paths, val_predicate_access_paths));
1248
453
    } else {
1249
229
        _val_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1250
229
        DLOG(INFO) << "Map column iterator set value column to SKIP_READING";
1251
229
    }
1252
682
    return Status::OK();
1253
682
}
1254
1255
////////////////////////////////////////////////////////////////////////////////
1256
1257
StructFileColumnIterator::StructFileColumnIterator(
1258
        std::shared_ptr<ColumnReader> reader, ColumnIteratorUPtr null_iterator,
1259
        std::vector<ColumnIteratorUPtr>&& sub_column_iterators)
1260
7.98k
        : _struct_reader(reader), _sub_column_iterators(std::move(sub_column_iterators)) {
1261
7.98k
    if (_struct_reader->is_nullable()) {
1262
7.12k
        _null_iterator = std::move(null_iterator);
1263
7.12k
    }
1264
7.98k
}
1265
1266
7.94k
Status StructFileColumnIterator::init(const ColumnIteratorOptions& opts) {
1267
7.94k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1268
0
        DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading.";
1269
0
        return Status::OK();
1270
0
    }
1271
1272
27.9k
    for (auto& column_iterator : _sub_column_iterators) {
1273
27.9k
        RETURN_IF_ERROR(column_iterator->init(opts));
1274
27.9k
    }
1275
7.94k
    if (_struct_reader->is_nullable()) {
1276
7.11k
        RETURN_IF_ERROR(_null_iterator->init(opts));
1277
7.11k
    }
1278
7.94k
    return Status::OK();
1279
7.94k
}
1280
1281
4.67k
Status StructFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
1282
4.67k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1283
0
        DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading.";
1284
0
        dst->insert_many_defaults(*n);
1285
0
        return Status::OK();
1286
0
    }
1287
1288
4.67k
    auto& column_struct = assert_cast<ColumnStruct&, TypeCheckOnRelease::DISABLE>(
1289
4.67k
            dst->is_nullable() ? static_cast<ColumnNullable&>(*dst).get_nested_column() : *dst);
1290
19.7k
    for (size_t i = 0; i < column_struct.tuple_size(); i++) {
1291
15.0k
        size_t num_read = *n;
1292
15.0k
        auto sub_column_ptr = column_struct.get_column(i).assume_mutable();
1293
15.0k
        bool column_has_null = false;
1294
15.0k
        RETURN_IF_ERROR(
1295
15.0k
                _sub_column_iterators[i]->next_batch(&num_read, sub_column_ptr, &column_has_null));
1296
15.0k
        DCHECK(num_read == *n);
1297
15.0k
        column_struct.get_column_ptr(i) = std::move(sub_column_ptr);
1298
15.0k
    }
1299
1300
4.67k
    if (dst->is_nullable()) {
1301
4.34k
        size_t num_read = *n;
1302
4.34k
        auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr();
1303
        // in not-null to null linked-schemachange mode,
1304
        // actually we do not change dat data include meta in footer,
1305
        // so may dst from changed meta which is nullable but old data is not nullable,
1306
        // if so, we should set null_map to all null by default
1307
4.34k
        if (_null_iterator) {
1308
4.25k
            bool null_signs_has_null = false;
1309
4.25k
            RETURN_IF_ERROR(
1310
4.25k
                    _null_iterator->next_batch(&num_read, null_map_ptr, &null_signs_has_null));
1311
4.25k
        } else {
1312
88
            auto& null_map = assert_cast<ColumnUInt8&, TypeCheckOnRelease::DISABLE>(*null_map_ptr);
1313
88
            null_map.insert_many_vals(0, num_read);
1314
88
        }
1315
4.34k
        DCHECK(num_read == *n);
1316
4.34k
    }
1317
1318
4.67k
    return Status::OK();
1319
4.67k
}
1320
1321
4.67k
Status StructFileColumnIterator::seek_to_ordinal(ordinal_t ord) {
1322
4.67k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1323
0
        DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading.";
1324
0
        return Status::OK();
1325
0
    }
1326
1327
15.0k
    for (auto& column_iterator : _sub_column_iterators) {
1328
15.0k
        RETURN_IF_ERROR(column_iterator->seek_to_ordinal(ord));
1329
15.0k
    }
1330
4.67k
    if (_struct_reader->is_nullable()) {
1331
4.25k
        RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord));
1332
4.25k
    }
1333
4.67k
    return Status::OK();
1334
4.67k
}
1335
1336
690
Status StructFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) {
1337
1.73k
    for (auto& column_iterator : _sub_column_iterators) {
1338
1.73k
        RETURN_IF_ERROR(column_iterator->init_prefetcher(params));
1339
1.73k
    }
1340
690
    if (_struct_reader->is_nullable()) {
1341
661
        RETURN_IF_ERROR(_null_iterator->init_prefetcher(params));
1342
661
    }
1343
690
    return Status::OK();
1344
690
}
1345
1346
void StructFileColumnIterator::collect_prefetchers(
1347
        std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
1348
690
        PrefetcherInitMethod init_method) {
1349
1.73k
    for (auto& column_iterator : _sub_column_iterators) {
1350
1.73k
        column_iterator->collect_prefetchers(prefetchers, init_method);
1351
1.73k
    }
1352
690
    if (_struct_reader->is_nullable()) {
1353
661
        _null_iterator->collect_prefetchers(prefetchers, init_method);
1354
661
    }
1355
690
}
1356
1357
Status StructFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count,
1358
2.70k
                                                MutableColumnPtr& dst) {
1359
2.70k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1360
0
        DLOG(INFO) << "Struct column iterator column " << _column_name << " skip reading.";
1361
0
        dst->insert_many_defaults(count);
1362
0
        return Status::OK();
1363
0
    }
1364
1365
2.70k
    if (count == 0) {
1366
0
        return Status::OK();
1367
0
    }
1368
1369
2.70k
    size_t this_run = 1;
1370
2.70k
    auto start_idx = rowids[0];
1371
2.70k
    auto last_idx = rowids[0];
1372
2.78k
    for (size_t i = 1; i < count; ++i) {
1373
87
        if (last_idx == rowids[i] - 1) {
1374
83
            last_idx = rowids[i];
1375
83
            this_run++;
1376
83
            continue;
1377
83
        }
1378
4
        RETURN_IF_ERROR(seek_to_ordinal(start_idx));
1379
4
        size_t num_read = this_run;
1380
4
        RETURN_IF_ERROR(next_batch(&num_read, dst, nullptr));
1381
4
        DCHECK_EQ(num_read, this_run);
1382
1383
4
        start_idx = rowids[i];
1384
4
        last_idx = rowids[i];
1385
4
        this_run = 1;
1386
4
    }
1387
1388
2.70k
    RETURN_IF_ERROR(seek_to_ordinal(start_idx));
1389
2.70k
    size_t num_read = this_run;
1390
2.70k
    RETURN_IF_ERROR(next_batch(&num_read, dst, nullptr));
1391
2.70k
    DCHECK_EQ(num_read, this_run);
1392
2.70k
    return Status::OK();
1393
2.70k
}
1394
1395
6.85k
void StructFileColumnIterator::set_need_to_read() {
1396
6.85k
    set_reading_flag(ReadingFlag::NEED_TO_READ);
1397
25.2k
    for (auto& sub_iterator : _sub_column_iterators) {
1398
25.2k
        sub_iterator->set_need_to_read();
1399
25.2k
    }
1400
6.85k
}
1401
1402
6.89k
void StructFileColumnIterator::remove_pruned_sub_iterators() {
1403
32.2k
    for (auto it = _sub_column_iterators.begin(); it != _sub_column_iterators.end();) {
1404
25.3k
        auto& sub_iterator = *it;
1405
25.3k
        if (sub_iterator->reading_flag() == ReadingFlag::SKIP_READING) {
1406
33
            DLOG(INFO) << "Struct column iterator remove pruned sub-column "
1407
33
                       << sub_iterator->column_name();
1408
33
            it = _sub_column_iterators.erase(it);
1409
25.2k
        } else {
1410
25.2k
            sub_iterator->remove_pruned_sub_iterators();
1411
25.2k
            ++it;
1412
25.2k
        }
1413
25.3k
    }
1414
6.89k
}
1415
1416
Status StructFileColumnIterator::set_access_paths(
1417
        const TColumnAccessPaths& all_access_paths,
1418
6.34k
        const TColumnAccessPaths& predicate_access_paths) {
1419
6.34k
    if (all_access_paths.empty()) {
1420
1.86k
        return Status::OK();
1421
1.86k
    }
1422
1423
4.47k
    if (!predicate_access_paths.empty()) {
1424
50
        set_reading_flag(ReadingFlag::READING_FOR_PREDICATE);
1425
50
        DLOG(INFO) << "Struct column iterator set sub-column " << _column_name
1426
50
                   << " to READING_FOR_PREDICATE";
1427
50
    }
1428
4.47k
    auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths));
1429
4.47k
    auto sub_predicate_access_paths = DORIS_TRY(_get_sub_access_paths(predicate_access_paths));
1430
1431
4.47k
    const auto no_sub_column_to_skip = sub_all_access_paths.empty();
1432
4.47k
    const auto no_predicate_sub_column = sub_predicate_access_paths.empty();
1433
1434
19.8k
    for (auto& sub_iterator : _sub_column_iterators) {
1435
19.8k
        const auto name = sub_iterator->column_name();
1436
19.8k
        bool need_to_read = no_sub_column_to_skip;
1437
19.8k
        TColumnAccessPaths sub_all_access_paths_of_this;
1438
19.8k
        if (!need_to_read) {
1439
239
            for (const auto& paths : sub_all_access_paths) {
1440
239
                if (paths.data_access_path.path[0] == name) {
1441
82
                    sub_all_access_paths_of_this.emplace_back(paths);
1442
82
                }
1443
239
            }
1444
117
            need_to_read = !sub_all_access_paths_of_this.empty();
1445
117
        }
1446
1447
19.8k
        if (!need_to_read) {
1448
36
            set_reading_flag(ReadingFlag::SKIP_READING);
1449
36
            sub_iterator->set_reading_flag(ReadingFlag::SKIP_READING);
1450
36
            DLOG(INFO) << "Struct column iterator set sub-column " << name << " to SKIP_READING";
1451
36
            continue;
1452
36
        }
1453
19.8k
        set_reading_flag(ReadingFlag::NEED_TO_READ);
1454
19.8k
        sub_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ);
1455
1456
19.8k
        TColumnAccessPaths sub_predicate_access_paths_of_this;
1457
1458
19.8k
        if (!no_predicate_sub_column) {
1459
638
            for (const auto& paths : sub_predicate_access_paths) {
1460
638
                if (StringCaseEqual()(paths.data_access_path.path[0], name)) {
1461
48
                    sub_predicate_access_paths_of_this.emplace_back(paths);
1462
48
                }
1463
638
            }
1464
638
        }
1465
1466
19.8k
        RETURN_IF_ERROR(sub_iterator->set_access_paths(sub_all_access_paths_of_this,
1467
19.8k
                                                       sub_predicate_access_paths_of_this));
1468
19.8k
    }
1469
4.47k
    return Status::OK();
1470
4.47k
}
1471
1472
////////////////////////////////////////////////////////////////////////////////
1473
102k
Status OffsetFileColumnIterator::init(const ColumnIteratorOptions& opts) {
1474
102k
    RETURN_IF_ERROR(_offset_iterator->init(opts));
1475
    // allocate peek tmp column once
1476
102k
    _peek_tmp_col = ColumnOffset64::create();
1477
102k
    return Status::OK();
1478
102k
}
1479
1480
362k
Status OffsetFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
1481
362k
    RETURN_IF_ERROR(_offset_iterator->next_batch(n, dst, has_null));
1482
362k
    return Status::OK();
1483
362k
}
1484
1485
711k
Status OffsetFileColumnIterator::_peek_one_offset(ordinal_t* offset) {
1486
711k
    if (_offset_iterator->get_current_page()->has_remaining()) {
1487
621k
        PageDecoder* offset_page_decoder = _offset_iterator->get_current_page()->data_decoder.get();
1488
621k
        size_t n = 1;
1489
621k
        _peek_tmp_col->clear();
1490
621k
        RETURN_IF_ERROR(offset_page_decoder->peek_next_batch(&n, _peek_tmp_col)); // not null
1491
621k
        DCHECK(_peek_tmp_col->size() == 1);
1492
621k
        *offset =
1493
621k
                assert_cast<const ColumnOffset64*, TypeCheckOnRelease::DISABLE>(_peek_tmp_col.get())
1494
621k
                        ->get_element(0);
1495
621k
    } else {
1496
89.6k
        *offset = _offset_iterator->get_current_page()->next_array_item_ordinal;
1497
89.6k
    }
1498
711k
    return Status::OK();
1499
711k
}
1500
1501
9.45k
Status OffsetFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) {
1502
9.45k
    return _offset_iterator->init_prefetcher(params);
1503
9.45k
}
1504
1505
void OffsetFileColumnIterator::collect_prefetchers(
1506
        std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
1507
9.45k
        PrefetcherInitMethod init_method) {
1508
9.45k
    _offset_iterator->collect_prefetchers(prefetchers, init_method);
1509
9.45k
}
1510
1511
/**
1512
 *  first_storage_offset read from page should smaller than next_storage_offset which here call _peek_one_offset from page,
1513
    and first_column_offset is keep in memory data which is different dimension with (first_storage_offset and next_storage_offset)
1514
     eg. step1. read page: first_storage_offset = 16382
1515
         step2. read page below with _peek_one_offset(&last_offset): last_offset = 16387
1516
         step3. first_offset = 126 which is calculate in column offsets
1517
         for loop column offsets element in size
1518
            we can calculate from first_storage_offset to next_storage_offset one by one to fill with offsets_data in memory column offsets
1519
 * @param start
1520
 * @param column_offsets
1521
 * @return
1522
 */
1523
Status OffsetFileColumnIterator::_calculate_offsets(ssize_t start,
1524
348k
                                                    ColumnArray::ColumnOffsets& column_offsets) {
1525
348k
    ordinal_t next_storage_offset = 0;
1526
348k
    RETURN_IF_ERROR(_peek_one_offset(&next_storage_offset));
1527
1528
    // calculate real offsets
1529
348k
    auto& offsets_data = column_offsets.get_data();
1530
348k
    ordinal_t first_column_offset = offsets_data[start - 1]; // -1 is valid
1531
348k
    ordinal_t first_storage_offset = offsets_data[start];
1532
348k
    DCHECK(next_storage_offset >= first_storage_offset);
1533
11.8M
    for (ssize_t i = start; i < offsets_data.size() - 1; ++i) {
1534
11.4M
        offsets_data[i] = first_column_offset + (offsets_data[i + 1] - first_storage_offset);
1535
11.4M
    }
1536
    // last offset
1537
348k
    offsets_data[offsets_data.size() - 1] =
1538
348k
            first_column_offset + (next_storage_offset - first_storage_offset);
1539
348k
    return Status::OK();
1540
348k
}
1541
1542
////////////////////////////////////////////////////////////////////////////////
1543
ArrayFileColumnIterator::ArrayFileColumnIterator(std::shared_ptr<ColumnReader> reader,
1544
                                                 OffsetFileColumnIteratorUPtr offset_reader,
1545
                                                 ColumnIteratorUPtr item_iterator,
1546
                                                 ColumnIteratorUPtr null_iterator)
1547
67.0k
        : _array_reader(reader),
1548
67.0k
          _offset_iterator(std::move(offset_reader)),
1549
67.0k
          _item_iterator(std::move(item_iterator)) {
1550
67.0k
    if (_array_reader->is_nullable()) {
1551
47.4k
        _null_iterator = std::move(null_iterator);
1552
47.4k
    }
1553
67.0k
}
1554
1555
66.9k
Status ArrayFileColumnIterator::init(const ColumnIteratorOptions& opts) {
1556
66.9k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1557
0
        DLOG(INFO) << "Array column iterator column " << _column_name << " skip readking.";
1558
0
        return Status::OK();
1559
0
    }
1560
1561
66.9k
    RETURN_IF_ERROR(_offset_iterator->init(opts));
1562
66.9k
    RETURN_IF_ERROR(_item_iterator->init(opts));
1563
66.9k
    if (_array_reader->is_nullable()) {
1564
47.4k
        RETURN_IF_ERROR(_null_iterator->init(opts));
1565
47.4k
    }
1566
66.9k
    return Status::OK();
1567
66.9k
}
1568
1569
329k
Status ArrayFileColumnIterator::_seek_by_offsets(ordinal_t ord) {
1570
    // using offsets info
1571
329k
    ordinal_t offset = 0;
1572
329k
    RETURN_IF_ERROR(_offset_iterator->_peek_one_offset(&offset));
1573
329k
    RETURN_IF_ERROR(_item_iterator->seek_to_ordinal(offset));
1574
329k
    return Status::OK();
1575
329k
}
1576
1577
329k
Status ArrayFileColumnIterator::seek_to_ordinal(ordinal_t ord) {
1578
329k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1579
0
        DLOG(INFO) << "Array column iterator column " << _column_name << " skip reading.";
1580
0
        return Status::OK();
1581
0
    }
1582
1583
329k
    RETURN_IF_ERROR(_offset_iterator->seek_to_ordinal(ord));
1584
329k
    if (_array_reader->is_nullable()) {
1585
303k
        RETURN_IF_ERROR(_null_iterator->seek_to_ordinal(ord));
1586
303k
    }
1587
329k
    return _seek_by_offsets(ord);
1588
329k
}
1589
1590
329k
Status ArrayFileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
1591
329k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1592
0
        DLOG(INFO) << "Array column iterator column " << _column_name << " skip reading.";
1593
0
        dst->insert_many_defaults(*n);
1594
0
        return Status::OK();
1595
0
    }
1596
1597
329k
    const auto* column_array = check_and_get_column<ColumnArray>(
1598
329k
            dst->is_nullable() ? static_cast<ColumnNullable&>(*dst).get_nested_column() : *dst);
1599
1600
329k
    bool offsets_has_null = false;
1601
329k
    auto column_offsets_ptr = column_array->get_offsets_column().assume_mutable();
1602
329k
    ssize_t start = column_offsets_ptr->size();
1603
329k
    RETURN_IF_ERROR(_offset_iterator->next_batch(n, column_offsets_ptr, &offsets_has_null));
1604
329k
    if (*n == 0) {
1605
0
        return Status::OK();
1606
0
    }
1607
329k
    auto& column_offsets = static_cast<ColumnArray::ColumnOffsets&>(*column_offsets_ptr);
1608
329k
    RETURN_IF_ERROR(_offset_iterator->_calculate_offsets(start, column_offsets));
1609
329k
    size_t num_items =
1610
329k
            column_offsets.get_data().back() - column_offsets.get_data()[start - 1]; // -1 is valid
1611
329k
    auto column_items_ptr = column_array->get_data().assume_mutable();
1612
329k
    if (num_items > 0) {
1613
110k
        size_t num_read = num_items;
1614
110k
        bool items_has_null = false;
1615
110k
        RETURN_IF_ERROR(_item_iterator->next_batch(&num_read, column_items_ptr, &items_has_null));
1616
110k
        DCHECK(num_read == num_items);
1617
110k
    }
1618
1619
329k
    if (dst->is_nullable()) {
1620
303k
        auto null_map_ptr = static_cast<ColumnNullable&>(*dst).get_null_map_column_ptr();
1621
303k
        size_t num_read = *n;
1622
        // in not-null to null linked-schemachange mode,
1623
        // actually we do not change dat data include meta in footer,
1624
        // so may dst from changed meta which is nullable but old data is not nullable,
1625
        // if so, we should set null_map to all null by default
1626
303k
        if (_null_iterator) {
1627
303k
            bool null_signs_has_null = false;
1628
303k
            RETURN_IF_ERROR(
1629
303k
                    _null_iterator->next_batch(&num_read, null_map_ptr, &null_signs_has_null));
1630
18.4E
        } else {
1631
18.4E
            auto& null_map = assert_cast<ColumnUInt8&, TypeCheckOnRelease::DISABLE>(*null_map_ptr);
1632
18.4E
            null_map.insert_many_vals(0, num_read);
1633
18.4E
        }
1634
303k
        DCHECK(num_read == *n);
1635
303k
    }
1636
1637
329k
    return Status::OK();
1638
329k
}
1639
1640
7.71k
Status ArrayFileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) {
1641
7.71k
    RETURN_IF_ERROR(_offset_iterator->init_prefetcher(params));
1642
7.71k
    RETURN_IF_ERROR(_item_iterator->init_prefetcher(params));
1643
7.71k
    if (_array_reader->is_nullable()) {
1644
6.00k
        RETURN_IF_ERROR(_null_iterator->init_prefetcher(params));
1645
6.00k
    }
1646
7.71k
    return Status::OK();
1647
7.71k
}
1648
1649
void ArrayFileColumnIterator::collect_prefetchers(
1650
        std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
1651
7.71k
        PrefetcherInitMethod init_method) {
1652
7.71k
    _offset_iterator->collect_prefetchers(prefetchers, init_method);
1653
    // the actual data pages to read of item column depends on the read result of offset column,
1654
    // so we can't init prefetch blocks according to rowids, just prefetch all data blocks here.
1655
7.71k
    _item_iterator->collect_prefetchers(prefetchers, PrefetcherInitMethod::ALL_DATA_BLOCKS);
1656
7.71k
    if (_array_reader->is_nullable()) {
1657
6.00k
        _null_iterator->collect_prefetchers(prefetchers, init_method);
1658
6.00k
    }
1659
7.71k
}
1660
1661
Status ArrayFileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count,
1662
95.7k
                                               MutableColumnPtr& dst) {
1663
95.7k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1664
0
        DLOG(INFO) << "Array column iterator column " << _column_name << " skip reading.";
1665
0
        dst->insert_many_defaults(count);
1666
0
        return Status::OK();
1667
0
    }
1668
1669
397k
    for (size_t i = 0; i < count; ++i) {
1670
        // TODO(cambyzju): now read array one by one, need optimize later
1671
302k
        RETURN_IF_ERROR(seek_to_ordinal(rowids[i]));
1672
302k
        size_t num_read = 1;
1673
302k
        RETURN_IF_ERROR(next_batch(&num_read, dst, nullptr));
1674
302k
        DCHECK(num_read == 1);
1675
302k
    }
1676
95.7k
    return Status::OK();
1677
95.7k
}
1678
1679
50.5k
void ArrayFileColumnIterator::set_need_to_read() {
1680
50.5k
    set_reading_flag(ReadingFlag::NEED_TO_READ);
1681
50.5k
    _item_iterator->set_need_to_read();
1682
50.5k
}
1683
1684
50.9k
void ArrayFileColumnIterator::remove_pruned_sub_iterators() {
1685
50.9k
    _item_iterator->remove_pruned_sub_iterators();
1686
50.9k
}
1687
1688
Status ArrayFileColumnIterator::set_access_paths(const TColumnAccessPaths& all_access_paths,
1689
50.2k
                                                 const TColumnAccessPaths& predicate_access_paths) {
1690
50.2k
    if (all_access_paths.empty()) {
1691
1.75k
        return Status::OK();
1692
1.75k
    }
1693
1694
48.5k
    if (!predicate_access_paths.empty()) {
1695
2.72k
        set_reading_flag(ReadingFlag::READING_FOR_PREDICATE);
1696
2.72k
        DLOG(INFO) << "Array column iterator set sub-column " << _column_name
1697
2.72k
                   << " to READING_FOR_PREDICATE";
1698
2.72k
    }
1699
1700
48.5k
    auto sub_all_access_paths = DORIS_TRY(_get_sub_access_paths(all_access_paths));
1701
48.5k
    auto sub_predicate_access_paths = DORIS_TRY(_get_sub_access_paths(predicate_access_paths));
1702
1703
48.5k
    const auto no_sub_column_to_skip = sub_all_access_paths.empty();
1704
48.5k
    const auto no_predicate_sub_column = sub_predicate_access_paths.empty();
1705
1706
48.5k
    if (!no_sub_column_to_skip) {
1707
3.08k
        for (auto& path : sub_all_access_paths) {
1708
3.08k
            if (path.data_access_path.path[0] == "*") {
1709
3.07k
                path.data_access_path.path[0] = _item_iterator->column_name();
1710
3.07k
            }
1711
3.08k
        }
1712
3.08k
    }
1713
1714
48.5k
    if (!no_predicate_sub_column) {
1715
192
        for (auto& path : sub_predicate_access_paths) {
1716
192
            if (path.data_access_path.path[0] == "*") {
1717
192
                path.data_access_path.path[0] = _item_iterator->column_name();
1718
192
            }
1719
192
        }
1720
192
    }
1721
1722
48.5k
    if (!no_sub_column_to_skip || !no_predicate_sub_column) {
1723
3.09k
        _item_iterator->set_reading_flag(ReadingFlag::NEED_TO_READ);
1724
3.09k
        RETURN_IF_ERROR(
1725
3.09k
                _item_iterator->set_access_paths(sub_all_access_paths, sub_predicate_access_paths));
1726
3.09k
    }
1727
48.5k
    return Status::OK();
1728
48.5k
}
1729
1730
////////////////////////////////////////////////////////////////////////////////
1731
1732
28.0M
FileColumnIterator::FileColumnIterator(std::shared_ptr<ColumnReader> reader) : _reader(reader) {}
1733
1734
27.9M
Status FileColumnIterator::init(const ColumnIteratorOptions& opts) {
1735
27.9M
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1736
486
        DLOG(INFO) << "File column iterator column " << _column_name << " skip reading.";
1737
486
        return Status::OK();
1738
486
    }
1739
1740
27.9M
    _opts = opts;
1741
27.9M
    if (!_opts.use_page_cache) {
1742
27.1M
        _reader->disable_index_meta_cache();
1743
27.1M
    }
1744
27.9M
    RETURN_IF_ERROR(get_block_compression_codec(_reader->get_compression(), &_compress_codec));
1745
27.9M
    if (config::enable_low_cardinality_optimize &&
1746
27.9M
        opts.io_ctx.reader_type == ReaderType::READER_QUERY &&
1747
27.9M
        _reader->encoding_info()->encoding() == DICT_ENCODING) {
1748
16.7M
        auto dict_encoding_type = _reader->get_dict_encoding_type();
1749
        // Only if the column is a predicate column, then we need check the all dict encoding flag
1750
        // because we could rewrite the predciate to accelarate query speed. But if it is not a
1751
        // predicate column, then it is useless. And it has a bad impact on cold read(first time read)
1752
        // because it will load the column's ordinal index and zonemap index and maybe other indices.
1753
        // it has bad impact on primary key query. For example, select * from table where pk = 1, and
1754
        // the table has 2000 columns.
1755
16.7M
        if (dict_encoding_type == ColumnReader::UNKNOWN_DICT_ENCODING && opts.is_predicate_column) {
1756
17.7k
            RETURN_IF_ERROR(seek_to_ordinal(_reader->num_rows() - 1));
1757
17.7k
            _is_all_dict_encoding = _page.is_dict_encoding;
1758
17.7k
            _reader->set_dict_encoding_type(_is_all_dict_encoding
1759
17.7k
                                                    ? ColumnReader::ALL_DICT_ENCODING
1760
17.7k
                                                    : ColumnReader::PARTIAL_DICT_ENCODING);
1761
16.7M
        } else {
1762
16.7M
            _is_all_dict_encoding = dict_encoding_type == ColumnReader::ALL_DICT_ENCODING;
1763
16.7M
        }
1764
16.7M
    }
1765
27.9M
    return Status::OK();
1766
27.9M
}
1767
1768
28.0M
FileColumnIterator::~FileColumnIterator() = default;
1769
1770
546k
void FileColumnIterator::_trigger_prefetch_if_eligible(ordinal_t ord) {
1771
546k
    std::vector<BlockRange> ranges;
1772
546k
    if (_prefetcher->need_prefetch(cast_set<uint32_t>(ord), &ranges)) {
1773
346k
        for (const auto& range : ranges) {
1774
346k
            _cached_remote_file_reader->prefetch_range(range.offset, range.size, &_opts.io_ctx);
1775
346k
        }
1776
346k
    }
1777
546k
}
1778
1779
4.40M
Status FileColumnIterator::seek_to_ordinal(ordinal_t ord) {
1780
4.40M
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1781
468
        DLOG(INFO) << "File column iterator column " << _column_name << " skip reading.";
1782
468
        return Status::OK();
1783
468
    }
1784
1785
18.4E
    LOG_IF(INFO, config::enable_segment_prefetch_verbose_log) << fmt::format(
1786
18.4E
            "[verbose] FileColumnIterator::seek_to_ordinal seek to ordinal {}, enable_prefetch={}",
1787
18.4E
            ord, _enable_prefetch);
1788
4.40M
    if (_enable_prefetch) {
1789
546k
        _trigger_prefetch_if_eligible(ord);
1790
546k
    }
1791
1792
    // if current page contains this row, we don't need to seek
1793
4.40M
    if (!_page || !_page.contains(ord) || !_page_iter.valid()) {
1794
1.71M
        RETURN_IF_ERROR(_reader->seek_at_or_before(ord, &_page_iter, _opts));
1795
1.71M
        RETURN_IF_ERROR(_read_data_page(_page_iter));
1796
1.71M
    }
1797
4.40M
    RETURN_IF_ERROR(_seek_to_pos_in_page(&_page, ord - _page.first_ordinal));
1798
4.40M
    _current_ordinal = ord;
1799
4.40M
    return Status::OK();
1800
4.40M
}
1801
1802
0
Status FileColumnIterator::seek_to_page_start() {
1803
0
    return seek_to_ordinal(_page.first_ordinal);
1804
0
}
1805
1806
4.47M
Status FileColumnIterator::_seek_to_pos_in_page(ParsedPage* page, ordinal_t offset_in_page) const {
1807
4.47M
    if (page->offset_in_page == offset_in_page) {
1808
        // fast path, do nothing
1809
2.36M
        return Status::OK();
1810
2.36M
    }
1811
1812
2.11M
    ordinal_t pos_in_data = offset_in_page;
1813
2.11M
    if (_page.has_null) {
1814
289k
        ordinal_t offset_in_data = 0;
1815
289k
        ordinal_t skips = offset_in_page;
1816
1817
289k
        if (offset_in_page > page->offset_in_page) {
1818
            // forward, reuse null bitmap
1819
244k
            skips = offset_in_page - page->offset_in_page;
1820
244k
            offset_in_data = page->data_decoder->current_index();
1821
244k
        } else {
1822
            // rewind null bitmap, and
1823
44.5k
            page->null_decoder = RleDecoder<bool>((const uint8_t*)page->null_bitmap.data,
1824
44.5k
                                                  cast_set<int>(page->null_bitmap.size), 1);
1825
44.5k
        }
1826
1827
289k
        auto skip_nulls = page->null_decoder.Skip(skips);
1828
289k
        pos_in_data = offset_in_data + skips - skip_nulls;
1829
289k
    }
1830
1831
2.11M
    RETURN_IF_ERROR(page->data_decoder->seek_to_position_in_page(pos_in_data));
1832
2.11M
    page->offset_in_page = offset_in_page;
1833
2.11M
    return Status::OK();
1834
2.11M
}
1835
1836
5.95k
Status FileColumnIterator::next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) {
1837
5.95k
    return _reader->next_batch_of_zone_map(n, dst);
1838
5.95k
}
1839
1840
3.41M
Status FileColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
1841
3.41M
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1842
468
        DLOG(INFO) << "File column iterator column " << _column_name << " skip reading.";
1843
468
        dst->insert_many_defaults(*n);
1844
468
        return Status::OK();
1845
468
    }
1846
1847
3.41M
    size_t curr_size = dst->byte_size();
1848
3.41M
    dst->reserve(*n);
1849
3.41M
    size_t remaining = *n;
1850
3.41M
    *has_null = false;
1851
6.91M
    while (remaining > 0) {
1852
3.48M
        if (!_page.has_remaining()) {
1853
73.3k
            bool eos = false;
1854
73.3k
            RETURN_IF_ERROR(_load_next_page(&eos));
1855
73.3k
            if (eos) {
1856
0
                break;
1857
0
            }
1858
73.3k
        }
1859
1860
        // number of rows to be read from this page
1861
3.48M
        size_t nrows_in_page = std::min(remaining, _page.remaining());
1862
3.48M
        size_t nrows_to_read = nrows_in_page;
1863
3.48M
        if (_page.has_null) {
1864
1.54M
            while (nrows_to_read > 0) {
1865
1.27M
                bool is_null = false;
1866
1.27M
                size_t this_run = _page.null_decoder.GetNextRun(&is_null, nrows_to_read);
1867
                // we use num_rows only for CHECK
1868
1.27M
                size_t num_rows = this_run;
1869
1.27M
                if (!is_null) {
1870
656k
                    RETURN_IF_ERROR(_page.data_decoder->next_batch(&num_rows, dst));
1871
656k
                    DCHECK_EQ(this_run, num_rows);
1872
656k
                } else {
1873
613k
                    *has_null = true;
1874
613k
                    auto* null_col = check_and_get_column<ColumnNullable>(dst.get());
1875
626k
                    if (null_col != nullptr) {
1876
626k
                        null_col->insert_many_defaults(this_run);
1877
18.4E
                    } else {
1878
18.4E
                        return Status::InternalError("unexpected column type in column reader");
1879
18.4E
                    }
1880
613k
                }
1881
1882
1.28M
                nrows_to_read -= this_run;
1883
1.28M
                _page.offset_in_page += this_run;
1884
1.28M
                _current_ordinal += this_run;
1885
1.28M
            }
1886
3.22M
        } else {
1887
3.22M
            RETURN_IF_ERROR(_page.data_decoder->next_batch(&nrows_to_read, dst));
1888
3.22M
            DCHECK_EQ(nrows_to_read, nrows_in_page);
1889
1890
3.22M
            _page.offset_in_page += nrows_to_read;
1891
3.22M
            _current_ordinal += nrows_to_read;
1892
3.22M
        }
1893
3.50M
        remaining -= nrows_in_page;
1894
3.50M
    }
1895
3.42M
    *n -= remaining;
1896
3.42M
    _opts.stats->bytes_read += (dst->byte_size() - curr_size) + BitmapSize(*n);
1897
1898
#ifdef BE_TEST
1899
    _reader->check_data_by_zone_map_for_test(dst);
1900
#endif
1901
3.42M
    return Status::OK();
1902
3.41M
}
1903
1904
Status FileColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count,
1905
739k
                                          MutableColumnPtr& dst) {
1906
739k
    if (_reading_flag == ReadingFlag::SKIP_READING) {
1907
0
        DLOG(INFO) << "File column iterator column " << _column_name << " skip reading.";
1908
0
        dst->insert_many_defaults(count);
1909
0
        return Status::OK();
1910
0
    }
1911
1912
739k
    size_t remaining = count;
1913
739k
    size_t total_read_count = 0;
1914
739k
    size_t nrows_to_read = 0;
1915
1.50M
    while (remaining > 0) {
1916
767k
        RETURN_IF_ERROR(seek_to_ordinal(rowids[total_read_count]));
1917
1918
        // number of rows to be read from this page
1919
767k
        nrows_to_read = std::min(remaining, _page.remaining());
1920
1921
767k
        if (_page.has_null) {
1922
67.7k
            size_t already_read = 0;
1923
373k
            while ((nrows_to_read - already_read) > 0) {
1924
306k
                bool is_null = false;
1925
306k
                size_t this_run = std::min(nrows_to_read - already_read, _page.remaining());
1926
306k
                if (UNLIKELY(this_run == 0)) {
1927
45
                    break;
1928
45
                }
1929
306k
                this_run = _page.null_decoder.GetNextRun(&is_null, this_run);
1930
306k
                size_t offset = total_read_count + already_read;
1931
306k
                size_t this_read_count = 0;
1932
306k
                rowid_t current_ordinal_in_page =
1933
306k
                        cast_set<uint32_t>(_page.offset_in_page + _page.first_ordinal);
1934
3.68M
                for (size_t i = 0; i < this_run; ++i) {
1935
3.59M
                    if (rowids[offset + i] - current_ordinal_in_page >= this_run) {
1936
214k
                        break;
1937
214k
                    }
1938
3.38M
                    this_read_count++;
1939
3.38M
                }
1940
1941
306k
                auto origin_index = _page.data_decoder->current_index();
1942
306k
                if (this_read_count > 0) {
1943
95.9k
                    if (is_null) {
1944
67.6k
                        auto* null_col = check_and_get_column<ColumnNullable>(dst.get());
1945
67.6k
                        if (UNLIKELY(null_col == nullptr)) {
1946
0
                            return Status::InternalError("unexpected column type in column reader");
1947
0
                        }
1948
1949
67.6k
                        null_col->insert_many_defaults(this_read_count);
1950
67.6k
                    } else {
1951
28.2k
                        size_t read_count = this_read_count;
1952
1953
                        // ordinal in nullable columns' data buffer maybe be not continuously(the data doesn't contain null value),
1954
                        // so we need use `page_start_off_in_decoder` to calculate the actual offset in `data_decoder`
1955
28.2k
                        size_t page_start_off_in_decoder =
1956
28.2k
                                _page.first_ordinal + _page.offset_in_page - origin_index;
1957
28.2k
                        RETURN_IF_ERROR(_page.data_decoder->read_by_rowids(
1958
28.2k
                                &rowids[offset], page_start_off_in_decoder, &read_count, dst));
1959
28.2k
                        DCHECK_EQ(read_count, this_read_count);
1960
28.2k
                    }
1961
95.9k
                }
1962
1963
306k
                if (!is_null) {
1964
237k
                    RETURN_IF_ERROR(
1965
237k
                            _page.data_decoder->seek_to_position_in_page(origin_index + this_run));
1966
237k
                }
1967
1968
306k
                already_read += this_read_count;
1969
306k
                _page.offset_in_page += this_run;
1970
306k
                DCHECK(_page.offset_in_page <= _page.num_rows);
1971
306k
            }
1972
1973
67.7k
            nrows_to_read = already_read;
1974
67.7k
            total_read_count += nrows_to_read;
1975
67.7k
            remaining -= nrows_to_read;
1976
699k
        } else {
1977
699k
            RETURN_IF_ERROR(_page.data_decoder->read_by_rowids(
1978
699k
                    &rowids[total_read_count], _page.first_ordinal, &nrows_to_read, dst));
1979
699k
            total_read_count += nrows_to_read;
1980
699k
            remaining -= nrows_to_read;
1981
699k
        }
1982
767k
    }
1983
739k
    return Status::OK();
1984
739k
}
1985
1986
73.3k
Status FileColumnIterator::_load_next_page(bool* eos) {
1987
73.3k
    _page_iter.next();
1988
73.3k
    if (!_page_iter.valid()) {
1989
0
        *eos = true;
1990
0
        return Status::OK();
1991
0
    }
1992
1993
73.3k
    RETURN_IF_ERROR(_read_data_page(_page_iter));
1994
73.3k
    RETURN_IF_ERROR(_seek_to_pos_in_page(&_page, 0));
1995
73.3k
    *eos = false;
1996
73.3k
    return Status::OK();
1997
73.3k
}
1998
1999
1.78M
Status FileColumnIterator::_read_data_page(const OrdinalPageIndexIterator& iter) {
2000
1.78M
    PageHandle handle;
2001
1.78M
    Slice page_body;
2002
1.78M
    PageFooterPB footer;
2003
1.78M
    _opts.type = DATA_PAGE;
2004
1.78M
    RETURN_IF_ERROR(
2005
1.78M
            _reader->read_page(_opts, iter.page(), &handle, &page_body, &footer, _compress_codec));
2006
    // parse data page
2007
1.78M
    auto st = ParsedPage::create(std::move(handle), page_body, footer.data_page_footer(),
2008
1.78M
                                 _reader->encoding_info(), iter.page(), iter.page_index(), &_page);
2009
1.78M
    if (!st.ok()) {
2010
0
        LOG(WARNING) << "failed to create ParsedPage, file=" << _opts.file_reader->path().native()
2011
0
                     << ", page_offset=" << iter.page().offset << ", page_size=" << iter.page().size
2012
0
                     << ", page_index=" << iter.page_index() << ", error=" << st;
2013
0
        return st;
2014
0
    }
2015
2016
    // dictionary page is read when the first data page that uses it is read,
2017
    // this is to optimize the memory usage: when there is no query on one column, we could
2018
    // release the memory of dictionary page.
2019
    // note that concurrent iterators for the same column won't repeatedly read dictionary page
2020
    // because of page cache.
2021
1.78M
    if (_reader->encoding_info()->encoding() == DICT_ENCODING) {
2022
565k
        auto dict_page_decoder = reinterpret_cast<BinaryDictPageDecoder*>(_page.data_decoder.get());
2023
565k
        if (dict_page_decoder->is_dict_encoding()) {
2024
512k
            if (_dict_decoder == nullptr) {
2025
406k
                RETURN_IF_ERROR(_read_dict_data());
2026
406k
                CHECK_NOTNULL(_dict_decoder);
2027
406k
            }
2028
2029
512k
            dict_page_decoder->set_dict_decoder(cast_set<uint32_t>(_dict_decoder->count()),
2030
512k
                                                _dict_word_info.get());
2031
512k
        }
2032
565k
    }
2033
1.78M
    return Status::OK();
2034
1.78M
}
2035
2036
406k
Status FileColumnIterator::_read_dict_data() {
2037
406k
    CHECK_EQ(_reader->encoding_info()->encoding(), DICT_ENCODING);
2038
    // read dictionary page
2039
406k
    Slice dict_data;
2040
406k
    PageFooterPB dict_footer;
2041
406k
    _opts.type = INDEX_PAGE;
2042
2043
406k
    RETURN_IF_ERROR(_reader->read_page(_opts, _reader->get_dict_page_pointer(), &_dict_page_handle,
2044
406k
                                       &dict_data, &dict_footer, _compress_codec, true));
2045
406k
    const EncodingInfo* encoding_info;
2046
406k
    RETURN_IF_ERROR(EncodingInfo::get(FieldType::OLAP_FIELD_TYPE_VARCHAR,
2047
406k
                                      dict_footer.dict_page_footer().encoding(), {},
2048
406k
                                      &encoding_info));
2049
406k
    RETURN_IF_ERROR(encoding_info->create_page_decoder(dict_data, {}, _dict_decoder));
2050
406k
    RETURN_IF_ERROR(_dict_decoder->init());
2051
2052
406k
    _dict_word_info.reset(new StringRef[_dict_decoder->count()]);
2053
406k
    RETURN_IF_ERROR(_dict_decoder->get_dict_word_info(_dict_word_info.get()));
2054
406k
    return Status::OK();
2055
406k
}
2056
2057
Status FileColumnIterator::get_row_ranges_by_zone_map(
2058
        const AndBlockColumnPredicate* col_predicates,
2059
        const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates,
2060
80.7k
        RowRanges* row_ranges) {
2061
80.7k
    if (_reader->has_zone_map()) {
2062
80.7k
        RETURN_IF_ERROR(_reader->get_row_ranges_by_zone_map(col_predicates, delete_predicates,
2063
80.7k
                                                            row_ranges, _opts));
2064
80.7k
    }
2065
80.7k
    return Status::OK();
2066
80.7k
}
2067
2068
Status FileColumnIterator::get_row_ranges_by_bloom_filter(
2069
81.0k
        const AndBlockColumnPredicate* col_predicates, RowRanges* row_ranges) {
2070
81.0k
    if ((col_predicates->can_do_bloom_filter(false) && _reader->has_bloom_filter_index(false)) ||
2071
81.0k
        (col_predicates->can_do_bloom_filter(true) && _reader->has_bloom_filter_index(true))) {
2072
95
        RETURN_IF_ERROR(_reader->get_row_ranges_by_bloom_filter(col_predicates, row_ranges, _opts));
2073
95
    }
2074
81.0k
    return Status::OK();
2075
81.0k
}
2076
2077
Status FileColumnIterator::get_row_ranges_by_dict(const AndBlockColumnPredicate* col_predicates,
2078
84.3k
                                                  RowRanges* row_ranges) {
2079
84.3k
    if (!_is_all_dict_encoding) {
2080
71.7k
        return Status::OK();
2081
71.7k
    }
2082
2083
12.5k
    if (!_dict_decoder) {
2084
0
        RETURN_IF_ERROR(_read_dict_data());
2085
0
        CHECK_NOTNULL(_dict_decoder);
2086
0
    }
2087
2088
12.5k
    if (!col_predicates->evaluate_and(_dict_word_info.get(), _dict_decoder->count())) {
2089
1.75k
        row_ranges->clear();
2090
1.75k
    }
2091
12.5k
    return Status::OK();
2092
12.5k
}
2093
2094
346k
Status FileColumnIterator::init_prefetcher(const SegmentPrefetchParams& params) {
2095
346k
    if (_cached_remote_file_reader =
2096
346k
                std::dynamic_pointer_cast<io::CachedRemoteFileReader>(_reader->_file_reader);
2097
346k
        !_cached_remote_file_reader) {
2098
0
        return Status::OK();
2099
0
    }
2100
346k
    _enable_prefetch = true;
2101
346k
    _prefetcher = std::make_unique<SegmentPrefetcher>(params.config);
2102
346k
    RETURN_IF_ERROR(_prefetcher->init(_reader, params.read_options));
2103
346k
    return Status::OK();
2104
346k
}
2105
2106
void FileColumnIterator::collect_prefetchers(
2107
        std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
2108
346k
        PrefetcherInitMethod init_method) {
2109
346k
    if (_prefetcher) {
2110
346k
        prefetchers[init_method].emplace_back(_prefetcher.get());
2111
346k
    }
2112
346k
}
2113
2114
24.3k
Status DefaultValueColumnIterator::init(const ColumnIteratorOptions& opts) {
2115
24.3k
    _opts = opts;
2116
    // be consistent with segment v1
2117
    // if _has_default_value, we should create default column iterator for this column, and
2118
    // "NULL" is a special default value which means the default value is null.
2119
24.3k
    if (_has_default_value) {
2120
2.05k
        if (_default_value == "NULL") {
2121
1.21k
            _default_value_field = Field::create_field<TYPE_NULL>(Null {});
2122
1.21k
        } else {
2123
836
            if (_type_info->type() == FieldType::OLAP_FIELD_TYPE_ARRAY) {
2124
8
                if (_default_value != "[]") {
2125
0
                    return Status::NotSupported("Array default {} is unsupported", _default_value);
2126
8
                } else {
2127
8
                    _default_value_field = Field::create_field<TYPE_ARRAY>(Array {});
2128
8
                    return Status::OK();
2129
8
                }
2130
828
            } else if (_type_info->type() == FieldType::OLAP_FIELD_TYPE_STRUCT) {
2131
0
                return Status::NotSupported("STRUCT default type is unsupported");
2132
828
            } else if (_type_info->type() == FieldType::OLAP_FIELD_TYPE_MAP) {
2133
0
                return Status::NotSupported("MAP default type is unsupported");
2134
0
            }
2135
828
            const auto t = _type_info->type();
2136
828
            const auto serde = DataTypeFactory::instance()
2137
828
                                       .create_data_type(t, _precision, _scale, _len)
2138
828
                                       ->get_serde();
2139
828
            DataTypeSerDe::FormatOptions opt;
2140
828
            RETURN_IF_ERROR(serde->from_olap_string(_default_value, _default_value_field, opt));
2141
828
        }
2142
22.3k
    } else if (_is_nullable) {
2143
22.3k
        _default_value_field = Field::create_field<TYPE_NULL>(Null {});
2144
18.4E
    } else {
2145
18.4E
        return Status::InternalError(
2146
18.4E
                "invalid default value column for no default value and not nullable");
2147
18.4E
    }
2148
24.4k
    return Status::OK();
2149
24.3k
}
2150
2151
2.74k
Status DefaultValueColumnIterator::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
2152
2.74k
    *has_null = _default_value_field.is_null();
2153
2.74k
    _insert_many_default(dst, *n);
2154
2.74k
    return Status::OK();
2155
2.74k
}
2156
2157
Status DefaultValueColumnIterator::read_by_rowids(const rowid_t* rowids, const size_t count,
2158
7.19k
                                                  MutableColumnPtr& dst) {
2159
7.19k
    _insert_many_default(dst, count);
2160
7.19k
    return Status::OK();
2161
7.19k
}
2162
2163
9.93k
void DefaultValueColumnIterator::_insert_many_default(MutableColumnPtr& dst, size_t n) {
2164
9.93k
    if (_default_value_field.is_null()) {
2165
9.50k
        dst->insert_many_defaults(n);
2166
9.50k
    } else {
2167
427
        dst = dst->convert_to_predicate_column_if_dictionary();
2168
427
        dst->insert_duplicate_fields(_default_value_field, n);
2169
427
    }
2170
9.93k
}
2171
2172
12.6k
Status RowIdColumnIteratorV2::next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
2173
12.6k
    auto* string_column = assert_cast<ColumnString*, TypeCheckOnRelease::DISABLE>(dst.get());
2174
2175
27.9M
    for (uint32_t i = 0; i < *n; ++i) {
2176
27.9M
        uint32_t row_id = _current_rowid + i;
2177
27.9M
        GlobalRowLoacationV2 location(_version, _backend_id, _file_id, row_id);
2178
27.9M
        string_column->insert_data(reinterpret_cast<const char*>(&location),
2179
27.9M
                                   sizeof(GlobalRowLoacationV2));
2180
27.9M
    }
2181
12.6k
    _current_rowid += *n;
2182
12.6k
    return Status::OK();
2183
12.6k
}
2184
2185
Status RowIdColumnIteratorV2::read_by_rowids(const rowid_t* rowids, const size_t count,
2186
9.66k
                                             MutableColumnPtr& dst) {
2187
9.66k
    auto* string_column = assert_cast<ColumnString*>(dst.get());
2188
2189
24.5M
    for (size_t i = 0; i < count; ++i) {
2190
24.5M
        uint32_t row_id = rowids[i];
2191
24.5M
        GlobalRowLoacationV2 location(_version, _backend_id, _file_id, row_id);
2192
24.5M
        string_column->insert_data(reinterpret_cast<const char*>(&location),
2193
24.5M
                                   sizeof(GlobalRowLoacationV2));
2194
24.5M
    }
2195
9.66k
    return Status::OK();
2196
9.66k
}
2197
#include "common/compile_check_end.h"
2198
2199
} // namespace doris::segment_v2