Coverage Report

Created: 2026-10-13 23:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/iterators.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 <cstddef>
21
#include <memory>
22
#include <set>
23
24
#include "common/status.h"
25
#include "core/block/block.h"
26
#include "exprs/score_runtime.h"
27
#include "exprs/vexpr.h"
28
#include "io/io_common.h"
29
#include "runtime/runtime_state.h"
30
#include "storage/index/ann/ann_topn_runtime.h"
31
#include "storage/olap_common.h"
32
#include "storage/predicate/block_column_predicate.h"
33
#include "storage/predicate/column_predicate.h"
34
#include "storage/row_cursor.h"
35
#include "storage/segment/row_ranges.h"
36
#include "storage/segment/variant/variant_compaction_paths.h"
37
#include "storage/tablet/tablet_schema.h"
38
39
namespace doris {
40
41
class ReadSchema;
42
class ColumnPredicate;
43
44
struct IteratorRowRef;
45
46
class StorageReadOptions {
47
public:
48
    // Every storage read needs a statistics sink. Deleting the default constructor keeps copied
49
    // options valid from creation; for example, SegmentIterator stores a copy and later
50
    // dereferences stats while reading pages.
51
    StorageReadOptions() = delete;
52
53
6.77M
    explicit StorageReadOptions(OlapReaderStatistics& stats_) noexcept : stats(&stats_) {}
54
55
    struct KeyRange {
56
        KeyRange()
57
                : lower_key(nullptr),
58
                  include_lower(false),
59
                  upper_key(nullptr),
60
0
                  include_upper(false) {}
61
62
        KeyRange(const RowCursor* lower_key_, bool include_lower_, const RowCursor* upper_key_,
63
                 bool include_upper_)
64
3.84M
                : lower_key(lower_key_),
65
3.84M
                  include_lower(include_lower_),
66
3.84M
                  upper_key(upper_key_),
67
3.84M
                  include_upper(include_upper_) {}
68
69
        // the lower bound of the range, nullptr if not existed
70
        const RowCursor* lower_key = nullptr;
71
        // whether `lower_key` is included in the range
72
        bool include_lower;
73
        // the upper bound of the range, nullptr if not existed
74
        const RowCursor* upper_key = nullptr;
75
        // whether `upper_key` is included in the range
76
        bool include_upper;
77
78
3.71M
        uint64_t get_digest(uint64_t seed) const {
79
3.71M
            if (lower_key != nullptr) {
80
3.71M
                auto key_str = lower_key->to_string();
81
3.71M
                seed = HashUtil::hash64(key_str.c_str(), key_str.size(), seed);
82
3.71M
                seed = HashUtil::hash64(&include_lower, sizeof(include_lower), seed);
83
3.71M
            }
84
85
3.75M
            if (upper_key != nullptr) {
86
3.75M
                auto key_str = upper_key->to_string();
87
3.75M
                seed = HashUtil::hash64(key_str.c_str(), key_str.size(), seed);
88
3.75M
                seed = HashUtil::hash64(&include_upper, sizeof(include_upper), seed);
89
3.75M
            }
90
91
3.71M
            return seed;
92
3.71M
        }
93
    };
94
95
    // reader's key ranges, empty if not existed.
96
    // used by short key index to filter row blocks
97
    std::vector<KeyRange> key_ranges;
98
99
    // For unique-key merge-on-write, the effect is similar to delete_conditions
100
    // that filters out rows that are deleted in realtime.
101
    // For a particular row, if delete_bitmap.contains(rowid) means that row is
102
    // marked deleted and invisible to user anymore.
103
    // segment_id -> roaring::Roaring*
104
    std::unordered_map<uint32_t, std::shared_ptr<roaring::Roaring>> delete_bitmap;
105
106
    std::shared_ptr<AndBlockColumnPredicate> delete_condition_predicates =
107
            AndBlockColumnPredicate::create_shared();
108
    // reader's column predicate, nullptr if not existed
109
    // used to fiter rows in row block
110
    std::vector<std::shared_ptr<ColumnPredicate>> column_predicates;
111
    std::unordered_map<int32_t, std::shared_ptr<AndBlockColumnPredicate>> col_id_to_predicates;
112
    std::unordered_map<int32_t, std::vector<std::shared_ptr<const ColumnPredicate>>>
113
            del_predicates_for_zone_map;
114
    TPushAggOp::type push_down_agg_type_opt = TPushAggOp::NONE;
115
    // Non-key columns whose predicates were proven always true by the segment zone map and then
116
    // removed from column_predicates. SegmentIterator can skip reading these non-output columns,
117
    // or COUNT_ON_INDEX columns, because the column values no longer affect filtering or counting.
118
    std::set<uint32_t> zonemap_always_true_pred_cols;
119
120
    // Non-owning and initialized to a valid object by the constructor.
121
    OlapReaderStatistics* stats;
122
    bool use_page_cache = false;
123
    uint32_t block_row_max = 4096 - 32; // see https://github.com/apache/doris/pull/11816
124
    // Effective adaptive batch size byte budget.
125
    size_t preferred_block_size_bytes = 8388608UL;
126
127
    // Whether the tablet schema this read targets materializes variant subcolumns as extracted
128
    // columns. A compaction read then takes them as flat leaves.
129
    bool tablet_has_extracted_variant_columns = false;
130
    // The compaction output schema's variant path layout; null outside a variant compaction.
131
    VariantCompactionPathsSPtr variant_compaction_paths;
132
    bool enable_unique_key_merge_on_write = false;
133
    bool record_rowids = false;
134
    std::vector<int> topn_filter_source_node_ids;
135
    // used for special optimization for query : ORDER BY key DESC LIMIT n
136
    bool read_orderby_key_reverse = false;
137
    // For rows with the same key, use ascending order (small-to-large) for tie-breakers.
138
    // For example, use lower rowset version / segment id first.
139
    bool use_insert_order_when_same = false;
140
    bool read_row_binlog = false;
141
    int binlog_tso_idx = -1;
142
    // columns for orderby keys
143
    std::vector<uint32_t>* read_orderby_key_columns = nullptr;
144
    io::IOContext io_ctx;
145
    VExprContextSPtrs common_expr_ctxs_push_down;
146
    const std::set<int32_t>* output_columns = nullptr;
147
    // Extra storage key columns that are included only to keep the scan schema
148
    // aligned with the storage key prefix. SegmentIterator can synthesize
149
    // placeholders only after proving predicates, delete conditions, and
150
    // expressions do not need their real values.
151
    std::set<ColumnId> extra_columns;
152
    // runtime state
153
    RuntimeState* runtime_state = nullptr;
154
    RowsetId rowset_id;
155
    Version version;
156
    TsoRange commit_tso;
157
    int64_t tablet_id = 0;
158
    // slots that cast may be eliminated in storage layer
159
    std::map<std::string, DataTypePtr> target_cast_type_for_variants;
160
    RowRanges row_ranges;
161
162
    // Per-segment row budget pushed down from the scanner (topn or general
163
    // limit). SegmentIterator applies it after predicate/common-expr filtering;
164
    // _can_opt_limit_reads() only decides whether the pre-filter read can also
165
    // be capped. 0 disables the optimization.
166
    size_t read_limit = 0;
167
168
    std::map<ColumnId, VExprContextSPtr> virtual_column_exprs;
169
    std::shared_ptr<segment_v2::AnnTopNRuntime> ann_topn_runtime;
170
171
    std::map<int32_t, TColumnAccessPaths> all_access_paths;
172
    std::map<int32_t, TColumnAccessPaths> predicate_access_paths;
173
174
    std::shared_ptr<ScoreRuntime> score_runtime;
175
    CollectionStatisticsPtr collection_statistics;
176
177
    uint64_t condition_cache_digest = 0;
178
};
179
180
struct CompactionSampleInfo {
181
    int64_t bytes = 0;
182
    int64_t rows = 0;
183
    int64_t group_data_size = 0;
184
    int64_t null_count = 0; // Number of NULL cells in this column group
185
};
186
187
struct BlockWithSameBit {
188
    Block* block;
189
    std::vector<bool>& same_bit;
190
191
201
    bool empty() const { return block->rows() == 0; }
192
};
193
194
class RowwiseIterator;
195
using RowwiseIteratorUPtr = std::unique_ptr<RowwiseIterator>;
196
class RowwiseIterator {
197
public:
198
4.32M
    RowwiseIterator() = default;
199
4.34M
    virtual ~RowwiseIterator() = default;
200
201
    // Initialize this iterator and make it ready to read with
202
    // input options.
203
    // Input options may contain scan range in which this scan.
204
    // Return Status::OK() if init successfully,
205
    // Return other error otherwise
206
0
    virtual Status init(const StorageReadOptions& opts) {
207
0
        return Status::InternalError("to be implemented, current class: " +
208
0
                                     demangle(typeid(*this).name()));
209
0
    }
210
211
0
    virtual Status init(const StorageReadOptions& opts, CompactionSampleInfo* sample_info) {
212
0
        return Status::InternalError("should not reach here, current class: " +
213
0
                                     demangle(typeid(*this).name()));
214
0
    }
215
216
    // If there is any valid data, this function will load data
217
    // into input batch with Status::OK() returned
218
    // If there is no data to read, will return Status::EndOfFile.
219
    // If other error happens, other error code will be returned.
220
0
    virtual Status next_batch(Block* block) {
221
0
        return Status::InternalError("should not reach here, current class: " +
222
0
                                     demangle(typeid(*this).name()));
223
0
    }
224
225
0
    virtual Status next_batch(BlockWithSameBit* block_with_same_bit) {
226
0
        return Status::InternalError("should not reach here, current class: " +
227
0
                                     demangle(typeid(*this).name()));
228
0
    }
229
230
0
    virtual Status next_batch(BlockView* block_view) {
231
0
        return Status::InternalError("should not reach here, current class: " +
232
0
                                     demangle(typeid(*this).name()));
233
0
    }
234
235
0
    virtual Status next_row(IteratorRowRef* ref) {
236
0
        return Status::InternalError("should not reach here, current class: " +
237
0
                                     demangle(typeid(*this).name()));
238
0
    }
239
0
    virtual Status unique_key_next_row(IteratorRowRef* ref) {
240
0
        return Status::InternalError("should not reach here, current class: " +
241
0
                                     demangle(typeid(*this).name()));
242
0
    }
243
244
0
    virtual bool is_merge_iterator() const { return false; }
245
246
0
    virtual Status current_block_row_locations(std::vector<RowLocation>* block_row_locations) {
247
0
        return Status::InternalError("should not reach here, current class: " +
248
0
                                     demangle(typeid(*this).name()));
249
0
    }
250
251
    // return schema for this Iterator
252
    virtual const ReadSchema& schema() const = 0;
253
254
    // Return the data id such as segment id, used for keep the insert order when do
255
    // merge sort in priority queue
256
26.2k
    virtual uint64_t data_id() const { return 0; }
257
258
5.69k
    virtual void update_profile(RuntimeProfile* profile) {}
259
    // return rows merged count by iterator
260
0
    virtual uint64_t merged_rows() const { return 0; }
261
262
    // return if it's an empty iterator
263
1.49M
    virtual bool empty() const { return false; }
264
};
265
266
} // namespace doris