Coverage Report

Created: 2026-03-12 17:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/tablet/tablet_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/PaloInternalService_types.h>
22
#include <gen_cpp/PlanNodes_types.h>
23
#include <stddef.h>
24
#include <stdint.h>
25
26
#include <memory>
27
#include <set>
28
#include <string>
29
#include <unordered_set>
30
#include <utility>
31
#include <vector>
32
33
#include "agent/be_exec_version_manager.h"
34
#include "common/status.h"
35
#include "exprs/function_filter.h"
36
#include "io/io_common.h"
37
#include "storage/delete/delete_handler.h"
38
#include "storage/iterators.h"
39
#include "storage/olap_common.h"
40
#include "storage/olap_tuple.h"
41
#include "storage/predicate/filter_olap_param.h"
42
#include "storage/row_cursor.h"
43
#include "storage/rowid_conversion.h"
44
#include "storage/rowset/rowset.h"
45
#include "storage/rowset/rowset_meta.h"
46
#include "storage/rowset/rowset_reader.h"
47
#include "storage/rowset/rowset_reader_context.h"
48
#include "storage/tablet/base_tablet.h"
49
#include "storage/tablet/tablet_fwd.h"
50
51
namespace doris {
52
53
class RuntimeState;
54
class BitmapFilterFuncBase;
55
class BloomFilterFuncBase;
56
class ColumnPredicate;
57
class DeleteBitmap;
58
class HybridSetBase;
59
class RuntimeProfile;
60
61
class VCollectIterator;
62
class Block;
63
class VExpr;
64
class Arena;
65
class VExprContext;
66
67
// Used to compare row with input scan key. Scan key only contains key columns,
68
// row contains all key columns, which is superset of key columns.
69
// So we should compare the common prefix columns of lhs and rhs.
70
//
71
// NOTE: if you are not sure if you can use it, please don't use this function.
72
1.80M
inline int compare_row_key(const RowCursor& lhs, const RowCursor& rhs) {
73
1.80M
    auto cmp_cids = std::min(lhs.schema()->num_column_ids(), rhs.schema()->num_column_ids());
74
4.02M
    for (uint32_t cid = 0; cid < cmp_cids; ++cid) {
75
3.97M
        auto res = lhs.schema()->column(cid)->compare_cell(lhs.cell(cid), rhs.cell(cid));
76
3.97M
        if (res != 0) {
77
1.76M
            return res;
78
1.76M
        }
79
3.97M
    }
80
43.3k
    return 0;
81
1.80M
}
82
83
class TabletReader {
84
    struct KeysParam {
85
        std::vector<RowCursor> start_keys;
86
        std::vector<RowCursor> end_keys;
87
        bool start_key_include = false;
88
        bool end_key_include = false;
89
    };
90
91
public:
92
    // Params for Reader,
93
    // mainly include tablet, data version and fetch range.
94
    struct ReaderParams {
95
1.31M
        bool has_single_version() const {
96
1.31M
            return (rs_splits.size() == 1 &&
97
1.31M
                    rs_splits[0].rs_reader->rowset()->start_version() == 0 &&
98
1.31M
                    !rs_splits[0].rs_reader->rowset()->rowset_meta()->is_segments_overlapping()) ||
99
1.31M
                   (rs_splits.size() == 2 &&
100
1.31M
                    rs_splits[0].rs_reader->rowset()->rowset_meta()->num_rows() == 0 &&
101
1.31M
                    rs_splits[1].rs_reader->rowset()->start_version() == 2 &&
102
1.31M
                    !rs_splits[1].rs_reader->rowset()->rowset_meta()->is_segments_overlapping());
103
1.31M
        }
104
105
14.3k
        int get_be_exec_version() const {
106
14.3k
            if (runtime_state) {
107
10.3k
                return runtime_state->be_exec_version();
108
10.3k
            }
109
4.03k
            return BeExecVersionManager::get_newest_version();
110
14.3k
        }
111
112
1.35M
        void set_read_source(TabletReadSource read_source, bool skip_delete_bitmap = false) {
113
1.35M
            rs_splits = std::move(read_source.rs_splits);
114
1.35M
            delete_predicates = std::move(read_source.delete_predicates);
115
1.35M
#ifndef BE_TEST
116
1.35M
            if (tablet->enable_unique_key_merge_on_write() && !skip_delete_bitmap) {
117
796k
                delete_bitmap = std::move(read_source.delete_bitmap);
118
796k
            }
119
1.35M
#endif
120
1.35M
        }
121
122
        BaseTabletSPtr tablet;
123
        TabletSchemaSPtr tablet_schema;
124
        ReaderType reader_type = ReaderType::READER_QUERY;
125
        bool direct_mode = false;
126
        bool aggregation = false;
127
        // for compaction, schema_change, check_sum: we don't use page cache
128
        // for query and config::disable_storage_page_cache is false, we use page cache
129
        bool use_page_cache = false;
130
        Version version = Version(-1, 0);
131
132
        std::vector<OlapTuple> start_key;
133
        std::vector<OlapTuple> end_key;
134
        bool start_key_include = false;
135
        bool end_key_include = false;
136
137
        std::vector<std::shared_ptr<ColumnPredicate>> predicates;
138
        std::vector<FunctionFilter> function_filters;
139
        std::vector<RowsetMetaSharedPtr> delete_predicates;
140
        // slots that cast may be eliminated in storage layer
141
        std::map<std::string, DataTypePtr> target_cast_type_for_variants;
142
143
        std::map<int32_t, TColumnAccessPaths> all_access_paths;
144
        std::map<int32_t, TColumnAccessPaths> predicate_access_paths;
145
146
        std::vector<RowSetSplits> rs_splits;
147
        // For unique key table with merge-on-write
148
        DeleteBitmapPtr delete_bitmap = nullptr;
149
150
        // return_columns is init from query schema
151
        std::vector<ColumnId> return_columns;
152
        // output_columns only contain columns in OrderByExprs and outputExprs
153
        std::set<int32_t> output_columns;
154
        RuntimeProfile* profile = nullptr;
155
        RuntimeState* runtime_state = nullptr;
156
157
        // use only in vec exec engine
158
        std::vector<ColumnId>* origin_return_columns = nullptr;
159
        std::unordered_set<uint32_t>* tablet_columns_convert_to_null_set = nullptr;
160
        TPushAggOp::type push_down_agg_type_opt = TPushAggOp::NONE;
161
        std::vector<VExprSPtr> remaining_conjunct_roots;
162
        VExprContextSPtrs common_expr_ctxs_push_down;
163
164
        // used for compaction to record row ids
165
        bool record_rowids = false;
166
        RowIdConversion* rowid_conversion = nullptr;
167
        std::vector<int> topn_filter_source_node_ids;
168
        int topn_filter_target_node_id = -1;
169
        // used for special optimization for query : ORDER BY key LIMIT n
170
        bool read_orderby_key = false;
171
        // used for special optimization for query : ORDER BY key DESC LIMIT n
172
        bool read_orderby_key_reverse = false;
173
        // num of columns for orderby key
174
        size_t read_orderby_key_num_prefix_columns = 0;
175
        // limit of rows for read_orderby_key
176
        size_t read_orderby_key_limit = 0;
177
        // filter_block arguments
178
        VExprContextSPtrs filter_block_conjuncts;
179
180
        // for vertical compaction
181
        bool is_key_column_group = false;
182
        std::vector<uint32_t> key_group_cluster_key_idxes;
183
184
        // For sparse column compaction optimization
185
        // When true, use optimized path for sparse wide tables
186
        bool enable_sparse_optimization = false;
187
188
        bool is_segcompaction = false;
189
190
        std::vector<RowwiseIteratorUPtr>* segment_iters_ptr = nullptr;
191
192
        void check_validation() const;
193
194
        std::string to_string() const;
195
196
        int64_t batch_size = -1;
197
198
        std::map<ColumnId, VExprContextSPtr> virtual_column_exprs;
199
        std::map<ColumnId, size_t> vir_cid_to_idx_in_block;
200
        std::map<size_t, DataTypePtr> vir_col_idx_to_type;
201
202
        std::shared_ptr<ScoreRuntime> score_runtime;
203
        CollectionStatisticsPtr collection_statistics;
204
        std::shared_ptr<segment_v2::AnnTopNRuntime> ann_topn_runtime;
205
206
        uint64_t condition_cache_digest = 0;
207
    };
208
209
1.34M
    TabletReader() = default;
210
211
1.34M
    virtual ~TabletReader() = default;
212
213
    TabletReader(const TabletReader&) = delete;
214
    void operator=(const TabletReader&) = delete;
215
216
    // Initialize TabletReader with tablet, data version and fetch range.
217
    virtual Status init(const ReaderParams& read_params);
218
219
    // Read next block with aggregation.
220
    // Return OK and set `*eof` to false when next block is read
221
    // Return OK and set `*eof` to true when no more rows can be read.
222
    // Return others when unexpected error happens.
223
0
    virtual Status next_block_with_aggregation(Block* block, bool* eof) {
224
0
        return Status::Error<ErrorCode::READER_INITIALIZE_ERROR>(
225
0
                "TabletReader not support next_block_with_aggregation");
226
0
    }
227
228
1.39k
    virtual uint64_t merged_rows() const { return _merged_rows; }
229
230
12.5k
    uint64_t filtered_rows() const {
231
12.5k
        return _stats.rows_del_filtered + _stats.rows_del_by_bitmap +
232
12.5k
               _stats.rows_conditions_filtered + _stats.rows_vec_del_cond_filtered +
233
12.5k
               _stats.rows_vec_cond_filtered + _stats.rows_short_circuit_cond_filtered;
234
12.5k
    }
235
236
1.31M
    void set_batch_size(int batch_size) { _reader_context.batch_size = batch_size; }
237
238
334
    int batch_size() const { return _reader_context.batch_size; }
239
240
1.48M
    const OlapReaderStatistics& stats() const { return _stats; }
241
8.96k
    OlapReaderStatistics* mutable_stats() { return &_stats; }
242
243
0
    virtual void update_profile(RuntimeProfile* profile) {}
244
    static Status init_reader_params_and_create_block(
245
            TabletSharedPtr tablet, ReaderType reader_type,
246
            const std::vector<RowsetSharedPtr>& input_rowsets,
247
            TabletReader::ReaderParams* reader_params, Block* block);
248
249
protected:
250
    friend class VCollectIterator;
251
    friend class DeleteHandler;
252
253
    Status _init_params(const ReaderParams& read_params);
254
255
    Status _capture_rs_readers(const ReaderParams& read_params);
256
257
    Status _init_keys_param(const ReaderParams& read_params);
258
259
    Status _init_orderby_keys_param(const ReaderParams& read_params);
260
261
    Status _init_conditions_param(const ReaderParams& read_params);
262
263
    virtual std::shared_ptr<ColumnPredicate> _parse_to_predicate(
264
            const FunctionFilter& function_filter);
265
266
    Status _init_delete_condition(const ReaderParams& read_params);
267
268
    Status _init_return_columns(const ReaderParams& read_params);
269
270
2.74M
    const BaseTabletSPtr& tablet() { return _tablet; }
271
    // If original column is a variant type column, and it's predicate is normalized
272
    // so in order to get the real type of column predicate, we need to reset type
273
    // according to the related type in `target_cast_type_for_variants`.Since variant is not
274
    // an predicate applicable type.Otherwise return the original tablet column.
275
    // Eg. `where cast(v:a as bigint) > 1` will elimate cast, and materialize this variant column
276
    // to type bigint
277
    TabletColumn materialize_column(const TabletColumn& orig);
278
279
4.68M
    const TabletSchema& tablet_schema() { return *_tablet_schema; }
280
281
    Arena _predicate_arena;
282
    std::vector<ColumnId> _return_columns;
283
284
    // used for special optimization for query : ORDER BY key [ASC|DESC] LIMIT n
285
    // columns for orderby keys
286
    std::vector<uint32_t> _orderby_key_columns;
287
    // only use in outer join which change the column nullable which must keep same in
288
    // vec query engine
289
    std::unordered_set<uint32_t>* _tablet_columns_convert_to_null_set = nullptr;
290
291
    BaseTabletSPtr _tablet;
292
    RowsetReaderContext _reader_context;
293
    TabletSchemaSPtr _tablet_schema;
294
    KeysParam _keys_param;
295
    std::vector<bool> _is_lower_keys_included;
296
    std::vector<bool> _is_upper_keys_included;
297
    std::vector<std::shared_ptr<ColumnPredicate>> _col_predicates;
298
    std::vector<std::shared_ptr<ColumnPredicate>> _value_col_predicates;
299
    DeleteHandler _delete_handler;
300
301
    // Indicates whether the tablets has do a aggregation in storage engine.
302
    bool _aggregation = false;
303
    // for agg query, we don't need to finalize when scan agg object data
304
    ReaderType _reader_type = ReaderType::READER_QUERY;
305
    bool _next_delete_flag = false;
306
    bool _delete_sign_available = false;
307
    bool _filter_delete = false;
308
    int32_t _sequence_col_idx = -1;
309
    bool _direct_mode = false;
310
311
    std::vector<uint32_t> _key_cids;
312
    std::vector<uint32_t> _value_cids;
313
314
    uint64_t _merged_rows = 0;
315
    OlapReaderStatistics _stats;
316
};
317
318
} // namespace doris