Coverage Report

Created: 2026-05-20 10:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/segment/column_reader.h
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
#pragma once
19
20
#include <gen_cpp/Descriptors_types.h>
21
#include <gen_cpp/segment_v2.pb.h>
22
#include <glog/logging.h>
23
#include <sys/types.h>
24
25
#include <cstddef> // for size_t
26
#include <cstdint> // for uint32_t
27
#include <map>
28
#include <memory> // for unique_ptr
29
#include <string>
30
#include <utility>
31
#include <vector>
32
33
#include "common/config.h"
34
#include "common/logging.h"
35
#include "common/status.h"            // for Status
36
#include "core/column/column_array.h" // ColumnArray
37
#include "core/data_type/data_type.h"
38
#include "io/cache/cached_remote_file_reader.h"
39
#include "io/fs/file_reader_writer_fwd.h"
40
#include "io/io_common.h"
41
#include "storage/index/index_reader.h"
42
#include "storage/index/ordinal_page_index.h" // for OrdinalPageIndexIterator
43
#include "storage/index/zone_map/zone_map_index.h"
44
#include "storage/olap_common.h"
45
#include "storage/predicate/column_predicate.h"
46
#include "storage/segment/common.h"
47
#include "storage/segment/page_handle.h" // for PageHandle
48
#include "storage/segment/page_pointer.h"
49
#include "storage/segment/parsed_page.h" // for ParsedPage
50
#include "storage/segment/segment_prefetcher.h"
51
#include "storage/segment/stream_reader.h"
52
#include "storage/tablet/tablet_schema.h"
53
#include "storage/types.h"
54
#include "storage/utils.h"
55
#include "util/once.h"
56
57
namespace doris {
58
59
class BlockCompressionCodec;
60
class AndBlockColumnPredicate;
61
class ColumnPredicate;
62
class TabletIndex;
63
class StorageReadOptions;
64
65
namespace io {
66
class FileReader;
67
} // namespace io
68
struct Slice;
69
struct StringRef;
70
71
using TColumnAccessPaths = std::vector<TColumnAccessPath>;
72
73
namespace segment_v2 {
74
class EncodingInfo;
75
class ColumnIterator;
76
class BloomFilterIndexReader;
77
class InvertedIndexIterator;
78
class InvertedIndexReader;
79
class IndexFileReader;
80
class PageDecoder;
81
class RowRanges;
82
class ZoneMapIndexReader;
83
class IndexIterator;
84
class ColumnMetaAccessor;
85
86
struct ColumnReaderOptions {
87
    // whether verify checksum when read page
88
    bool verify_checksum = true;
89
    // for in memory olap table, use DURABLE CachePriority in page cache
90
    bool kept_in_memory = false;
91
92
    int be_exec_version = -1;
93
94
    TabletSchemaSPtr tablet_schema = nullptr;
95
};
96
97
struct ColumnIteratorOptions {
98
    bool use_page_cache = false;
99
    bool is_predicate_column = false;
100
    // for page cache allocation
101
    // page types are divided into DATA_PAGE & INDEX_PAGE
102
    // INDEX_PAGE including index_page, dict_page and short_key_page
103
    PageTypePB type = PageTypePB::UNKNOWN_PAGE_TYPE;
104
    io::FileReader* file_reader = nullptr; // Ref
105
    // reader statistics
106
    OlapReaderStatistics* stats = nullptr; // Ref
107
    io::IOContext io_ctx;
108
    bool only_read_offsets = false;
109
110
12.8k
    void sanity_check() const {
111
12.8k
        CHECK_NOTNULL(file_reader);
112
12.8k
        CHECK_NOTNULL(stats);
113
12.8k
    }
114
};
115
116
class ColumnIterator;
117
class OffsetFileColumnIterator;
118
class FileColumnIterator;
119
120
using ColumnIteratorUPtr = std::unique_ptr<ColumnIterator>;
121
using OffsetFileColumnIteratorUPtr = std::unique_ptr<OffsetFileColumnIterator>;
122
using FileColumnIteratorUPtr = std::unique_ptr<FileColumnIterator>;
123
using ColumnIteratorSPtr = std::shared_ptr<ColumnIterator>;
124
125
// There will be concurrent users to read the same column. So
126
// we should do our best to reduce resource usage through share
127
// same information, such as OrdinalPageIndex and Page data.
128
// This will cache data shared by all reader
129
class ColumnReader : public MetadataAdder<ColumnReader>,
130
                     public std::enable_shared_from_this<ColumnReader> {
131
public:
132
    ColumnReader();
133
    // Create an initialized ColumnReader in *reader.
134
    // This should be a lightweight operation without I/O.
135
    static Status create(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
136
                         uint64_t num_rows, const io::FileReaderSPtr& file_reader,
137
                         std::shared_ptr<ColumnReader>* reader);
138
139
    static Status create_array(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
140
                               const io::FileReaderSPtr& file_reader,
141
                               std::shared_ptr<ColumnReader>* reader);
142
    static Status create_map(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
143
                             const io::FileReaderSPtr& file_reader,
144
                             std::shared_ptr<ColumnReader>* reader);
145
    static Status create_struct(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
146
                                uint64_t num_rows, const io::FileReaderSPtr& file_reader,
147
                                std::shared_ptr<ColumnReader>* reader);
148
    static Status create_agg_state(const ColumnReaderOptions& opts, const ColumnMetaPB& meta,
149
                                   uint64_t num_rows, const io::FileReaderSPtr& file_reader,
150
                                   std::shared_ptr<ColumnReader>* reader);
151
152
    enum DictEncodingType { UNKNOWN_DICT_ENCODING, PARTIAL_DICT_ENCODING, ALL_DICT_ENCODING };
153
154
    static bool is_compaction_reader_type(ReaderType type);
155
156
    ~ColumnReader() override;
157
158
    // create a new column iterator. Client should delete returned iterator
159
    virtual Status new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* col,
160
                                const StorageReadOptions*);
161
    Status new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column);
162
    Status new_array_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column);
163
    Status new_struct_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column);
164
    Status new_map_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column);
165
    Status new_agg_state_iterator(ColumnIteratorUPtr* iterator);
166
167
    Status new_index_iterator(const std::shared_ptr<IndexFileReader>& index_file_reader,
168
                              const TabletIndex* index_meta, const std::string& rowset_id,
169
                              uint32_t segment_id, size_t rows_of_segment,
170
                              std::unique_ptr<IndexIterator>* iterator);
171
172
    Status seek_at_or_before(ordinal_t ordinal, OrdinalPageIndexIterator* iter,
173
                             const ColumnIteratorOptions& iter_opts);
174
    Status get_ordinal_index_reader(OrdinalIndexReader*& reader,
175
                                    OlapReaderStatistics* index_load_stats);
176
177
    // read a page from file into a page handle
178
    Status read_page(const ColumnIteratorOptions& iter_opts, const PagePointer& pp,
179
                     PageHandle* handle, Slice* page_body, PageFooterPB* footer,
180
                     BlockCompressionCodec* codec, bool is_dict_page = false) const;
181
182
2.94k
    bool is_nullable() const { return _meta_is_nullable; }
183
184
25.7k
    const EncodingInfo* encoding_info() const { return _encoding_info; }
185
186
0
    bool has_zone_map() const { return _zone_map_index != nullptr; }
187
    bool has_bloom_filter_index(bool ngram) const;
188
    // Check if this column could match `cond' using segment zone map.
189
    // Since segment zone map is stored in metadata, this function is fast without I/O.
190
    // set matched to true if segment zone map is absent or `cond' could be satisfied, false otherwise.
191
    Status match_condition(const AndBlockColumnPredicate* col_predicates, bool* matched) const;
192
193
    Status next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) const;
194
195
    // get row ranges with zone map
196
    // - cond_column is user's query predicate
197
    // - delete_condition is a delete predicate of one version
198
    Status get_row_ranges_by_zone_map(
199
            const AndBlockColumnPredicate* col_predicates,
200
            const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates,
201
            RowRanges* row_ranges, const ColumnIteratorOptions& iter_opts);
202
203
    // get row ranges with bloom filter index
204
    Status get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates,
205
                                          RowRanges* row_ranges,
206
                                          const ColumnIteratorOptions& iter_opts);
207
208
2.74k
    PagePointer get_dict_page_pointer() const { return _meta_dict_page; }
209
210
8.03k
    bool is_empty() const { return _num_rows == 0; }
211
212
    Status prune_predicates_by_zone_map(std::vector<std::shared_ptr<ColumnPredicate>>& predicates,
213
                                        const int column_id, bool* pruned) const;
214
215
7.87k
    CompressionTypePB get_compression() const { return _meta_compression; }
216
217
4.28k
    uint64_t num_rows() const { return _num_rows; }
218
219
0
    void set_dict_encoding_type(DictEncodingType type) {
220
0
        static_cast<void>(_set_dict_encoding_type_once.call([&] {
221
0
            _dict_encoding_type = type;
222
0
            return Status::OK();
223
0
        }));
224
0
    }
225
226
0
    DictEncodingType get_dict_encoding_type() { return _dict_encoding_type; }
227
228
7.87k
    void disable_index_meta_cache() { _use_index_page_cache = false; }
229
230
16
    DataTypePtr get_vec_data_type() { return _data_type; }
231
232
13.8k
    virtual FieldType get_meta_type() { return _meta_type; }
233
234
    int64_t get_metadata_size() const override;
235
236
#ifdef BE_TEST
237
    void check_data_by_zone_map_for_test(const MutableColumnPtr& dst) const;
238
#endif
239
240
private:
241
    friend class VariantColumnReader;
242
    friend class FileColumnIterator;
243
    friend class SegmentPrefetcher;
244
245
    ColumnReader(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, uint64_t num_rows,
246
                 io::FileReaderSPtr file_reader);
247
    Status init(const ColumnMetaPB* meta);
248
249
    [[nodiscard]] Status _load_zone_map_index(bool use_page_cache, bool kept_in_memory,
250
                                              const ColumnIteratorOptions& iter_opts);
251
    [[nodiscard]] Status _load_ordinal_index(bool use_page_cache, bool kept_in_memory,
252
                                             const ColumnIteratorOptions& iter_opts);
253
254
    [[nodiscard]] Status _load_index(const std::shared_ptr<IndexFileReader>& index_file_reader,
255
                                     const TabletIndex* index_meta, const std::string& rowset_id,
256
                                     uint32_t segment_id, size_t rows_of_segment);
257
    [[nodiscard]] Status _load_bloom_filter_index(bool use_page_cache, bool kept_in_memory,
258
                                                  const ColumnIteratorOptions& iter_opts);
259
260
    bool _zone_map_match_condition(const segment_v2::ZoneMap& zone_map,
261
                                   const AndBlockColumnPredicate* col_predicates) const;
262
263
    Status _get_filtered_pages(
264
            const AndBlockColumnPredicate* col_predicates,
265
            const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates,
266
            std::vector<uint32_t>* page_indexes, const ColumnIteratorOptions& iter_opts);
267
268
    Status _calculate_row_ranges(const std::vector<uint32_t>& page_indexes, RowRanges* row_ranges,
269
                                 const ColumnIteratorOptions& iter_opts);
270
271
    int64_t _meta_length;
272
    FieldType _meta_type;
273
    FieldType _meta_children_column_type;
274
    bool _meta_is_nullable;
275
    bool _use_index_page_cache;
276
    int _be_exec_version = -1;
277
278
    PagePointer _meta_dict_page;
279
    CompressionTypePB _meta_compression;
280
281
    ColumnReaderOptions _opts;
282
    uint64_t _num_rows;
283
284
    io::FileReaderSPtr _file_reader;
285
286
    DictEncodingType _dict_encoding_type;
287
288
    DataTypePtr _data_type;
289
290
    FieldType _type =
291
            FieldType::OLAP_FIELD_TYPE_NONE; // initialized in init(), may changed by subclasses.
292
    const EncodingInfo* _encoding_info =
293
            nullptr; // initialized in init(), used for create PageDecoder
294
295
    // meta for various column indexes (null if the index is absent)
296
    std::unique_ptr<ZoneMapPB> _segment_zone_map;
297
298
    mutable std::shared_mutex _load_index_lock;
299
    std::unique_ptr<ZoneMapIndexReader> _zone_map_index;
300
    std::unique_ptr<OrdinalIndexReader> _ordinal_index;
301
    std::shared_ptr<BloomFilterIndexReader> _bloom_filter_index;
302
303
    std::unordered_map<int64_t, IndexReaderPtr> _index_readers;
304
305
    std::vector<std::shared_ptr<ColumnReader>> _sub_readers;
306
307
    DorisCallOnce<Status> _set_dict_encoding_type_once;
308
};
309
310
// Base iterator to read one column data
311
class ColumnIterator {
312
public:
313
8.90k
    ColumnIterator() = default;
314
8.90k
    virtual ~ColumnIterator() = default;
315
316
46
    virtual Status init(const ColumnIteratorOptions& opts) {
317
46
        _opts = opts;
318
46
        return Status::OK();
319
46
    }
320
321
    // Seek to the given ordinal entry in the column.
322
    // Entry 0 is the first entry written to the column.
323
    // If provided seek point is past the end of the file,
324
    // then returns false.
325
    virtual Status seek_to_ordinal(ordinal_t ord) = 0;
326
327
18.6k
    Status next_batch(size_t* n, MutableColumnPtr& dst) {
328
18.6k
        bool has_null;
329
18.6k
        return next_batch(n, dst, &has_null);
330
18.6k
    }
331
332
0
    virtual Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) {
333
0
        return Status::NotSupported("next_batch not implement");
334
0
    }
335
336
0
    virtual Status next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) {
337
0
        return Status::NotSupported("next_batch_of_zone_map not implement");
338
0
    }
339
340
    virtual Status read_by_rowids(const rowid_t* rowids, const size_t count,
341
0
                                  MutableColumnPtr& dst) {
342
0
        return Status::NotSupported("read_by_rowids not implement");
343
0
    }
344
345
    virtual ordinal_t get_current_ordinal() const = 0;
346
347
    virtual Status get_row_ranges_by_zone_map(
348
            const AndBlockColumnPredicate* col_predicates,
349
            const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates,
350
0
            RowRanges* row_ranges) {
351
0
        return Status::OK();
352
0
    }
353
354
    virtual Status get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates,
355
0
                                                  RowRanges* row_ranges) {
356
0
        return Status::OK();
357
0
    }
358
359
    virtual Status get_row_ranges_by_dict(const AndBlockColumnPredicate* col_predicates,
360
0
                                          RowRanges* row_ranges) {
361
0
        return Status::OK();
362
0
    }
363
364
0
    virtual bool is_all_dict_encoding() const { return false; }
365
366
    virtual Status set_access_paths(const TColumnAccessPaths& all_access_paths,
367
19
                                    const TColumnAccessPaths& predicate_access_paths) {
368
19
        if (!predicate_access_paths.empty()) {
369
6
            _reading_flag = ReadingFlag::READING_FOR_PREDICATE;
370
6
        }
371
19
        return Status::OK();
372
19
    }
373
374
8.29k
    void set_column_name(const std::string& column_name) { _column_name = column_name; }
375
376
94
    const std::string& column_name() const { return _column_name; }
377
378
    // Since there may be multiple paths with conflicts or overlaps,
379
    // we need to define several reading flags:
380
    //
381
    // NORMAL_READING — Default value, indicating that the column should be read.
382
    // SKIP_READING — The column should not be read.
383
    // NEED_TO_READ — The column must be read.
384
    // READING_FOR_PREDICATE — The column is required for predicate evaluation.
385
    //
386
    // For example, suppose there are two paths:
387
    // - Path 1 specifies that column A needs to be read, so it is marked as NEED_TO_READ.
388
    // - Path 2 specifies that the column should not be read, but since it is already marked as NEED_TO_READ,
389
    //   it should not be changed to SKIP_READING.
390
    enum class ReadingFlag : int {
391
        NORMAL_READING,
392
        SKIP_READING,
393
        NEED_TO_READ,
394
        READING_FOR_PREDICATE
395
    };
396
252
    void set_reading_flag(ReadingFlag flag) {
397
252
        if (static_cast<int>(flag) > static_cast<int>(_reading_flag)) {
398
187
            _reading_flag = flag;
399
187
        }
400
252
    }
401
402
50
    ReadingFlag reading_flag() const { return _reading_flag; }
403
404
31
    virtual void set_need_to_read() { set_reading_flag(ReadingFlag::NEED_TO_READ); }
405
406
2
    virtual void remove_pruned_sub_iterators() {};
407
408
0
    virtual Status init_prefetcher(const SegmentPrefetchParams& params) { return Status::OK(); }
409
410
    virtual void collect_prefetchers(
411
            std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
412
0
            PrefetcherInitMethod init_method) {}
413
414
    static constexpr const char* ACCESS_OFFSET = "OFFSET";
415
    static constexpr const char* ACCESS_ALL = "*";
416
    static constexpr const char* ACCESS_MAP_KEYS = "KEYS";
417
    static constexpr const char* ACCESS_MAP_VALUES = "VALUES";
418
    static constexpr const char* ACCESS_NULL = "NULL";
419
420
    // Meta-only read modes:
421
    // - OFFSET_ONLY: only read offset information (e.g., for array_size/map_size/string_length)
422
    // - NULL_MAP_ONLY: only read null map (e.g., for IS NULL / IS NOT NULL predicates)
423
    // When these modes are enabled, actual content data is skipped.
424
    enum class ReadMode : int { DEFAULT, OFFSET_ONLY, NULL_MAP_ONLY };
425
426
2.99k
    bool read_offset_only() const { return _read_mode == ReadMode::OFFSET_ONLY; }
427
23.2k
    bool read_null_map_only() const { return _read_mode == ReadMode::NULL_MAP_ONLY; }
428
429
    enum class ReadingMode : int {
430
        NORMAL, // default mode
431
        PREDICATE,
432
        LAZY
433
    };
434
435
54.4k
    virtual void set_reading_mode(ReadingMode mode) {
436
54.4k
        _reading_mode = mode;
437
54.4k
        if (mode == ReadingMode::PREDICATE) {
438
33
            _has_place_holder_column = false;
439
33
        }
440
54.4k
    }
441
442
46.3k
    virtual bool need_to_read() const {
443
46.3k
        switch (_reading_mode) {
444
46.3k
        case ReadingMode::NORMAL:
445
46.3k
            return _reading_flag != ReadingFlag::SKIP_READING;
446
5
        case ReadingMode::PREDICATE:
447
5
            return _reading_flag == ReadingFlag::READING_FOR_PREDICATE;
448
5
        case ReadingMode::LAZY:
449
5
            return _reading_flag == ReadingFlag::NEED_TO_READ;
450
0
        default:
451
0
            return false;
452
46.3k
        }
453
46.3k
    }
454
455
    // Whether need to read meta columns, such as null map column, offset column.
456
2.33k
    bool need_to_read_meta_columns() const {
457
2.33k
        if (_reading_flag == ReadingFlag::SKIP_READING) {
458
1
            return false;
459
1
        }
460
2.33k
        switch (_reading_mode) {
461
2.33k
        case ReadingMode::NORMAL:
462
2.33k
        case ReadingMode::PREDICATE:
463
2.33k
            return true;
464
2
        case ReadingMode::LAZY:
465
2
            return _reading_flag != ReadingFlag::READING_FOR_PREDICATE;
466
2.33k
        }
467
0
        return false;
468
2.33k
    }
469
470
4
    virtual void finalize_lazy_mode(MutableColumnPtr& dst) {
471
4
        _recovery_from_place_holder_column(dst);
472
4
    }
473
474
12
    virtual void set_reading_flag_recursively(ReadingFlag flag) { set_reading_flag(flag); }
475
476
    // Whether this iterator or any nested iterator has data that must be materialized
477
    // in lazy mode. Predicate-only and meta-only branches are read before filtering and
478
    // must not be re-read in the lazy phase.
479
62
    virtual bool has_lazy_read_target() const { return _reading_flag == ReadingFlag::NEED_TO_READ; }
480
481
8
    bool is_pruned() const { return _pruned; }
482
483
protected:
484
    void _convert_to_place_holder_column(MutableColumnPtr& dst, size_t count);
485
486
    void _recovery_from_place_holder_column(MutableColumnPtr& dst);
487
488
    // Checks sub access paths for OFFSET or NULL meta-only modes and
489
    // updates _read_mode accordingly. Use the accessor helpers
490
    // read_offset_only() / read_null_map_only() to query the current mode.
491
    void _check_and_set_meta_read_mode(const TColumnAccessPaths& sub_all_access_paths);
492
493
    Result<TColumnAccessPaths> _get_sub_access_paths(const TColumnAccessPaths& access_paths);
494
    Result<TColumnAccessPaths> _process_sub_access_paths(const TColumnAccessPaths& access_paths,
495
                                                         const bool is_predicate);
496
    ColumnIteratorOptions _opts;
497
498
    ReadingFlag _reading_flag {ReadingFlag::NORMAL_READING};
499
    ReadMode _read_mode = ReadMode::DEFAULT;
500
    ReadingMode _reading_mode {ReadingMode::NORMAL};
501
    std::string _column_name;
502
    bool _pruned {false};
503
504
    bool _has_place_holder_column {false};
505
};
506
507
// This iterator is used to read column data from file
508
// for scalar type
509
class FileColumnIterator : public ColumnIterator {
510
public:
511
    explicit FileColumnIterator(std::shared_ptr<ColumnReader> reader);
512
    ~FileColumnIterator() override;
513
514
    Status init(const ColumnIteratorOptions& opts) override;
515
516
    Status seek_to_ordinal(ordinal_t ord) override;
517
518
    Status seek_to_page_start();
519
520
    Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override;
521
522
    Status next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) override;
523
524
    Status read_by_rowids(const rowid_t* rowids, const size_t count,
525
                          MutableColumnPtr& dst) override;
526
527
2
    ordinal_t get_current_ordinal() const override { return _current_ordinal; }
528
529
    // get row ranges by zone map
530
    // - cond_column is user's query predicate
531
    // - delete_condition is delete predicate of one version
532
    Status get_row_ranges_by_zone_map(
533
            const AndBlockColumnPredicate* col_predicates,
534
            const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates,
535
            RowRanges* row_ranges) override;
536
537
    Status get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates,
538
                                          RowRanges* row_ranges) override;
539
540
    Status get_row_ranges_by_dict(const AndBlockColumnPredicate* col_predicates,
541
                                  RowRanges* row_ranges) override;
542
543
704
    ParsedPage* get_current_page() { return &_page; }
544
545
0
    bool is_nullable() { return _reader->is_nullable(); }
546
547
0
    bool is_all_dict_encoding() const override { return _is_all_dict_encoding; }
548
549
    Status init_prefetcher(const SegmentPrefetchParams& params) override;
550
    void collect_prefetchers(
551
            std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
552
            PrefetcherInitMethod init_method) override;
553
554
protected:
555
    // Exposed to derived iterators (e.g. StringFileColumnIterator) so they can
556
    // query column metadata such as the storage field type.
557
2
    const std::shared_ptr<ColumnReader>& get_reader() const { return _reader; }
558
559
private:
560
    Status _seek_to_pos_in_page(ParsedPage* page, ordinal_t offset_in_page) const;
561
    Status _load_next_page(bool* eos);
562
    Status _read_data_page(const OrdinalPageIndexIterator& iter);
563
    Status _read_dict_data();
564
    void _trigger_prefetch_if_eligible(ordinal_t ord);
565
566
    std::shared_ptr<ColumnReader> _reader = nullptr;
567
568
    BlockCompressionCodec* _compress_codec = nullptr;
569
570
    // 1. The _page represents current page.
571
    // 2. We define an operation is one seek and following read,
572
    //    If new seek is issued, the _page will be reset.
573
    ParsedPage _page;
574
575
    // keep dict page decoder
576
    std::unique_ptr<PageDecoder> _dict_decoder;
577
578
    // keep dict page handle to avoid released
579
    PageHandle _dict_page_handle;
580
581
    // page iterator used to get next page when current page is finished.
582
    // This value will be reset when a new seek is issued
583
    OrdinalPageIndexIterator _page_iter;
584
585
    // current value ordinal
586
    ordinal_t _current_ordinal = 0;
587
588
    bool _is_all_dict_encoding = false;
589
590
    std::unique_ptr<StringRef[]> _dict_word_info;
591
592
    bool _enable_prefetch {false};
593
    std::unique_ptr<SegmentPrefetcher> _prefetcher;
594
    std::shared_ptr<io::CachedRemoteFileReader> _cached_remote_file_reader {nullptr};
595
};
596
597
class EmptyFileColumnIterator final : public ColumnIterator {
598
public:
599
46
    Status seek_to_ordinal(ordinal_t ord) override { return Status::OK(); }
600
0
    ordinal_t get_current_ordinal() const override { return 0; }
601
};
602
603
// StringFileColumnIterator extends FileColumnIterator with meta-only reading
604
// support for string/binary column types. When the OFFSET path is detected in
605
// set_access_paths, it sets only_read_offsets on the ColumnIteratorOptions so
606
// that the BinaryPlainPageDecoder skips chars memcpy and only fills offsets.
607
class StringFileColumnIterator final : public FileColumnIterator {
608
public:
609
    explicit StringFileColumnIterator(std::shared_ptr<ColumnReader> reader);
610
2.64k
    ~StringFileColumnIterator() override = default;
611
612
    Status init(const ColumnIteratorOptions& opts) override;
613
614
    Status set_access_paths(const TColumnAccessPaths& all_access_paths,
615
                            const TColumnAccessPaths& predicate_access_paths) override;
616
};
617
618
// This iterator make offset operation write once for
619
class OffsetFileColumnIterator final : public ColumnIterator {
620
public:
621
228
    explicit OffsetFileColumnIterator(FileColumnIteratorUPtr offset_reader) {
622
228
        _offset_iterator = std::move(offset_reader);
623
228
    }
624
625
228
    ~OffsetFileColumnIterator() override = default;
626
627
    Status init(const ColumnIteratorOptions& opts) override;
628
629
    Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override;
630
631
0
    Status next_batch(size_t* n, MutableColumnPtr& dst) {
632
0
        bool has_null;
633
0
        return next_batch(n, dst, &has_null);
634
0
    }
635
636
0
    ordinal_t get_current_ordinal() const override {
637
0
        return _offset_iterator->get_current_ordinal();
638
0
    }
639
177
    Status seek_to_ordinal(ordinal_t ord) override {
640
177
        RETURN_IF_ERROR(_offset_iterator->seek_to_ordinal(ord));
641
177
        return Status::OK();
642
177
    }
643
644
    Status _peek_one_offset(ordinal_t* offset);
645
646
    Status _calculate_offsets(ssize_t start, ColumnArray::ColumnOffsets& column_offsets);
647
648
    Status read_by_rowids(const rowid_t* rowids, const size_t count,
649
6
                          MutableColumnPtr& dst) override {
650
6
        return _offset_iterator->read_by_rowids(rowids, count, dst);
651
6
    }
652
653
    Status init_prefetcher(const SegmentPrefetchParams& params) override;
654
    void collect_prefetchers(
655
            std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
656
            PrefetcherInitMethod init_method) override;
657
658
private:
659
    std::unique_ptr<FileColumnIterator> _offset_iterator;
660
    // reuse a tiny column for peek to avoid frequent allocations
661
    MutableColumnPtr _peek_tmp_col;
662
};
663
664
// This iterator is used to read map value column
665
class MapFileColumnIterator final : public ColumnIterator {
666
public:
667
    explicit MapFileColumnIterator(std::shared_ptr<ColumnReader> reader,
668
                                   ColumnIteratorUPtr null_iterator,
669
                                   OffsetFileColumnIteratorUPtr offsets_iterator,
670
                                   ColumnIteratorUPtr key_iterator,
671
                                   ColumnIteratorUPtr val_iterator);
672
673
191
    ~MapFileColumnIterator() override = default;
674
675
    Status init(const ColumnIteratorOptions& opts) override;
676
677
    Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override;
678
679
    Status read_by_rowids(const rowid_t* rowids, const size_t count,
680
                          MutableColumnPtr& dst) override;
681
682
    Status seek_to_ordinal(ordinal_t ord) override;
683
684
0
    ordinal_t get_current_ordinal() const override {
685
0
        if (read_null_map_only() && _null_iterator) {
686
0
            return _null_iterator->get_current_ordinal();
687
0
        }
688
0
        return _offsets_iterator->get_current_ordinal();
689
0
    }
690
    Status init_prefetcher(const SegmentPrefetchParams& params) override;
691
    void collect_prefetchers(
692
            std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
693
            PrefetcherInitMethod init_method) override;
694
695
    Status set_access_paths(const TColumnAccessPaths& all_access_paths,
696
                            const TColumnAccessPaths& predicate_access_paths) override;
697
698
    void set_need_to_read() override;
699
700
    void remove_pruned_sub_iterators() override;
701
702
    void set_reading_mode(ReadingMode mode) override;
703
704
338
    bool need_to_read() const override {
705
338
        switch (_reading_mode) {
706
330
        case ReadingMode::NORMAL:
707
330
            return _reading_flag != ReadingFlag::SKIP_READING;
708
3
        case ReadingMode::PREDICATE:
709
3
            return _reading_flag == ReadingFlag::READING_FOR_PREDICATE;
710
5
        case ReadingMode::LAZY:
711
            // In lazy mode, read this map only when at least one key/value branch still
712
            // has non-predicate data to materialize.
713
5
            return has_lazy_read_target();
714
0
        default:
715
0
            return false;
716
338
        }
717
338
    }
718
719
    void finalize_lazy_mode(MutableColumnPtr& dst) override;
720
721
    void set_reading_flag_recursively(ReadingFlag flag) override;
722
723
    bool has_lazy_read_target() const override;
724
725
private:
726
    std::shared_ptr<ColumnReader> _map_reader = nullptr;
727
    ColumnIteratorUPtr _null_iterator;
728
    OffsetFileColumnIteratorUPtr _offsets_iterator; //OffsetFileIterator
729
    ColumnIteratorUPtr _key_iterator;
730
    ColumnIteratorUPtr _val_iterator;
731
};
732
733
class StructFileColumnIterator final : public ColumnIterator {
734
public:
735
    explicit StructFileColumnIterator(std::shared_ptr<ColumnReader> reader,
736
                                      ColumnIteratorUPtr null_iterator,
737
                                      std::vector<ColumnIteratorUPtr>&& sub_column_iterators);
738
739
48
    ~StructFileColumnIterator() override = default;
740
741
    Status init(const ColumnIteratorOptions& opts) override;
742
743
    Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override;
744
745
0
    Status next_batch(size_t* n, MutableColumnPtr& dst) {
746
0
        bool has_null;
747
0
        return next_batch(n, dst, &has_null);
748
0
    }
749
750
    Status read_by_rowids(const rowid_t* rowids, const size_t count,
751
                          MutableColumnPtr& dst) override;
752
753
    Status seek_to_ordinal(ordinal_t ord) override;
754
755
0
    ordinal_t get_current_ordinal() const override {
756
0
        if (read_null_map_only() && _null_iterator) {
757
0
            return _null_iterator->get_current_ordinal();
758
0
        }
759
0
        return _sub_column_iterators[0]->get_current_ordinal();
760
0
    }
761
762
    Status set_access_paths(const TColumnAccessPaths& all_access_paths,
763
                            const TColumnAccessPaths& predicate_access_paths) override;
764
765
    void set_need_to_read() override;
766
767
    void remove_pruned_sub_iterators() override;
768
769
    Status init_prefetcher(const SegmentPrefetchParams& params) override;
770
    void collect_prefetchers(
771
            std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
772
            PrefetcherInitMethod init_method) override;
773
774
    void set_reading_mode(ReadingMode mode) override;
775
776
29
    bool need_to_read() const override {
777
29
        switch (_reading_mode) {
778
9
        case ReadingMode::NORMAL:
779
9
            return _reading_flag != ReadingFlag::SKIP_READING;
780
9
        case ReadingMode::PREDICATE:
781
9
            return _reading_flag == ReadingFlag::READING_FOR_PREDICATE;
782
11
        case ReadingMode::LAZY:
783
            // In lazy mode, read this struct only when at least one nested branch still
784
            // has non-predicate data to materialize.
785
11
            return has_lazy_read_target();
786
0
        default:
787
0
            return false;
788
29
        }
789
29
    }
790
791
    void finalize_lazy_mode(MutableColumnPtr& dst) override;
792
    void set_reading_flag_recursively(ReadingFlag flag) override;
793
    bool has_lazy_read_target() const override;
794
795
private:
796
    std::shared_ptr<ColumnReader> _struct_reader = nullptr;
797
    ColumnIteratorUPtr _null_iterator;
798
    std::vector<ColumnIteratorUPtr> _sub_column_iterators;
799
};
800
801
class ArrayFileColumnIterator final : public ColumnIterator {
802
public:
803
    explicit ArrayFileColumnIterator(std::shared_ptr<ColumnReader> reader,
804
                                     OffsetFileColumnIteratorUPtr offset_reader,
805
                                     ColumnIteratorUPtr item_iterator,
806
                                     ColumnIteratorUPtr null_iterator);
807
808
35
    ~ArrayFileColumnIterator() override = default;
809
810
    Status init(const ColumnIteratorOptions& opts) override;
811
812
    Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override;
813
814
0
    Status next_batch(size_t* n, MutableColumnPtr& dst) {
815
0
        bool has_null;
816
0
        return next_batch(n, dst, &has_null);
817
0
    }
818
819
    Status read_by_rowids(const rowid_t* rowids, const size_t count,
820
                          MutableColumnPtr& dst) override;
821
822
    Status seek_to_ordinal(ordinal_t ord) override;
823
824
0
    ordinal_t get_current_ordinal() const override {
825
0
        if (read_null_map_only() && _null_iterator) {
826
0
            return _null_iterator->get_current_ordinal();
827
0
        }
828
0
        return _offset_iterator->get_current_ordinal();
829
0
    }
830
831
    Status set_access_paths(const TColumnAccessPaths& all_access_paths,
832
                            const TColumnAccessPaths& predicate_access_paths) override;
833
    void set_need_to_read() override;
834
835
    void remove_pruned_sub_iterators() override;
836
837
    Status init_prefetcher(const SegmentPrefetchParams& params) override;
838
    void collect_prefetchers(
839
            std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers,
840
            PrefetcherInitMethod init_method) override;
841
842
    void set_reading_mode(ReadingMode mode) override;
843
844
35
    bool need_to_read() const override {
845
35
        switch (_reading_mode) {
846
27
        case ReadingMode::NORMAL:
847
27
            return _reading_flag != ReadingFlag::SKIP_READING;
848
3
        case ReadingMode::PREDICATE:
849
3
            return _reading_flag == ReadingFlag::READING_FOR_PREDICATE;
850
5
        case ReadingMode::LAZY:
851
            // In lazy mode, read this array only when its item branch still has
852
            // non-predicate data to materialize.
853
5
            return has_lazy_read_target();
854
0
        default:
855
0
            return false;
856
35
        }
857
35
    }
858
859
    void finalize_lazy_mode(MutableColumnPtr& dst) override;
860
861
    void set_reading_flag_recursively(ReadingFlag flag) override;
862
863
    bool has_lazy_read_target() const override;
864
865
private:
866
    std::shared_ptr<ColumnReader> _array_reader = nullptr;
867
    std::unique_ptr<OffsetFileColumnIterator> _offset_iterator;
868
    std::unique_ptr<ColumnIterator> _null_iterator;
869
    std::unique_ptr<ColumnIterator> _item_iterator;
870
871
    Status _seek_by_offsets(ordinal_t ord);
872
};
873
874
class RowIdColumnIterator : public ColumnIterator {
875
public:
876
    RowIdColumnIterator() = delete;
877
    RowIdColumnIterator(int64_t tid, RowsetId rid, int32_t segid)
878
0
            : _tablet_id(tid), _rowset_id(rid), _segment_id(segid) {}
879
880
0
    Status seek_to_ordinal(ordinal_t ord_idx) override {
881
0
        _current_rowid = cast_set<uint32_t>(ord_idx);
882
0
        return Status::OK();
883
0
    }
884
885
0
    Status next_batch(size_t* n, MutableColumnPtr& dst) {
886
0
        bool has_null;
887
0
        return next_batch(n, dst, &has_null);
888
0
    }
889
890
0
    Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override {
891
0
        for (size_t i = 0; i < *n; ++i) {
892
0
            const auto row_id = cast_set<uint32_t>(_current_rowid + i);
893
0
            GlobalRowLoacation location(_tablet_id, _rowset_id, _segment_id, row_id);
894
0
            dst->insert_data(reinterpret_cast<const char*>(&location), sizeof(GlobalRowLoacation));
895
0
        }
896
0
        _current_rowid += *n;
897
0
        return Status::OK();
898
0
    }
899
900
    Status read_by_rowids(const rowid_t* rowids, const size_t count,
901
0
                          MutableColumnPtr& dst) override {
902
0
        for (size_t i = 0; i < count; ++i) {
903
0
            rowid_t row_id = rowids[i];
904
0
            GlobalRowLoacation location(_tablet_id, _rowset_id, _segment_id, row_id);
905
0
            dst->insert_data(reinterpret_cast<const char*>(&location), sizeof(GlobalRowLoacation));
906
0
        }
907
0
        return Status::OK();
908
0
    }
909
910
0
    ordinal_t get_current_ordinal() const override { return _current_rowid; }
911
912
private:
913
    rowid_t _current_rowid = 0;
914
    int64_t _tablet_id = 0;
915
    RowsetId _rowset_id;
916
    int32_t _segment_id = 0;
917
};
918
919
// Add new RowIdColumnIteratorV2
920
class RowIdColumnIteratorV2 : public ColumnIterator {
921
public:
922
    RowIdColumnIteratorV2(uint8_t version, int64_t backend_id, uint32_t file_id)
923
16
            : _version(version), _backend_id(backend_id), _file_id(file_id) {}
924
925
0
    Status seek_to_ordinal(ordinal_t ord_idx) override {
926
0
        _current_rowid = cast_set<uint32_t>(ord_idx);
927
0
        return Status::OK();
928
0
    }
929
930
0
    Status next_batch(size_t* n, MutableColumnPtr& dst) {
931
0
        bool has_null;
932
0
        return next_batch(n, dst, &has_null);
933
0
    }
934
935
    Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override;
936
937
    Status read_by_rowids(const rowid_t* rowids, const size_t count,
938
                          MutableColumnPtr& dst) override;
939
940
0
    ordinal_t get_current_ordinal() const override { return _current_rowid; }
941
942
private:
943
    uint32_t _current_rowid = 0;
944
    uint8_t _version;
945
    int64_t _backend_id;
946
    uint32_t _file_id;
947
};
948
949
// This iterator is used to read default value column
950
class DefaultValueColumnIterator : public ColumnIterator {
951
public:
952
    DefaultValueColumnIterator(bool has_default_value, std::string default_value, bool is_nullable,
953
                               FieldType type, int precision, int scale, int len)
954
4
            : _has_default_value(has_default_value),
955
4
              _default_value(std::move(default_value)),
956
4
              _is_nullable(is_nullable),
957
4
              _type(type),
958
4
              _precision(precision),
959
4
              _scale(scale),
960
4
              _len(len) {}
961
962
    Status init(const ColumnIteratorOptions& opts) override;
963
964
1
    Status seek_to_ordinal(ordinal_t ord_idx) override {
965
1
        _current_rowid = ord_idx;
966
1
        return Status::OK();
967
1
    }
968
969
0
    Status next_batch(size_t* n, MutableColumnPtr& dst) {
970
0
        bool has_null;
971
0
        return next_batch(n, dst, &has_null);
972
0
    }
973
974
    Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override;
975
976
0
    Status next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) override {
977
0
        return next_batch(n, dst);
978
0
    }
979
980
    Status read_by_rowids(const rowid_t* rowids, const size_t count,
981
                          MutableColumnPtr& dst) override;
982
983
0
    ordinal_t get_current_ordinal() const override { return _current_rowid; }
984
985
private:
986
    void _insert_many_default(MutableColumnPtr& dst, size_t n);
987
988
    bool _has_default_value;
989
    std::string _default_value;
990
    bool _is_nullable;
991
    FieldType _type;
992
    int _precision;
993
    int _scale;
994
    const int _len;
995
    Field _default_value_field;
996
997
    // current rowid
998
    ordinal_t _current_rowid = 0;
999
};
1000
1001
} // namespace segment_v2
1002
} // namespace doris