Coverage Report

Created: 2026-05-17 03:41

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