Coverage Report

Created: 2025-11-28 11:20

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