Coverage Report

Created: 2026-06-22 16:57

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