Coverage Report

Created: 2026-08-25 21:30

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