Coverage Report

Created: 2026-04-03 05:44

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