Coverage Report

Created: 2026-06-24 21:32

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