Coverage Report

Created: 2026-07-17 18:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/exec/scan/scanner.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 <stdint.h>
21
22
#include <algorithm>
23
#include <vector>
24
25
#include "common/status.h"
26
#include "core/block/block.h"
27
#include "runtime/exec_env.h"
28
#include "runtime/runtime_state.h"
29
#include "storage/tablet/tablet.h"
30
#include "util/stopwatch.hpp"
31
32
namespace doris {
33
class RuntimeProfile;
34
class TupleDescriptor;
35
36
class VExprContext;
37
38
class ScanLocalStateBase;
39
} // namespace doris
40
41
namespace doris {
42
43
// Counter for load
44
struct ScannerCounter {
45
41
    ScannerCounter() : num_rows_filtered(0), num_rows_unselected(0) {}
46
47
    int64_t num_rows_filtered;   // unqualified rows (unmatched the dest schema, or no partition)
48
    int64_t num_rows_unselected; // rows filtered by predicates
49
};
50
51
class Scanner {
52
public:
53
    Scanner(RuntimeState* state, ScanLocalStateBase* local_state, int64_t limit,
54
            RuntimeProfile* profile);
55
56
    //only used for FileScanner read one line.
57
    Scanner(RuntimeState* state, RuntimeProfile* profile)
58
18
            : _state(state), _limit(1), _profile(profile), _total_rf_num(0), _has_prepared(false) {
59
18
        DorisMetrics::instance()->scanner_cnt->increment(1);
60
18
    };
61
62
36
    virtual ~Scanner() {
63
36
        SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_state->query_mem_tracker());
64
36
        _input_block.clear();
65
36
        _conjuncts.clear();
66
36
        _projections.clear();
67
36
        _origin_block.clear();
68
36
        _common_expr_ctxs_push_down.clear();
69
36
        DorisMetrics::instance()->scanner_cnt->increment(-1);
70
36
    }
71
72
    virtual Status init(RuntimeState* state, const VExprContextSPtrs& conjuncts);
73
0
    virtual Status prepare() {
74
0
        _has_prepared = true;
75
0
        return Status::OK();
76
0
    }
77
78
0
    Status open(RuntimeState* state) {
79
0
        SCOPED_RAW_TIMER(&_per_scanner_timer);
80
0
        return _open_impl(state);
81
0
    }
82
83
    Status get_block(RuntimeState* state, Block* block, bool* eos);
84
    Status get_block_after_projects(RuntimeState* state, Block* block, bool* eos);
85
86
    virtual Status close(RuntimeState* state);
87
88
    // Try to stop scanner, and all running readers.
89
0
    virtual void try_stop() { _should_stop = true; };
90
91
0
    virtual std::string get_name() { return ""; }
92
93
    // return the readable name of current scan range.
94
    // eg, for file scanner, return the current file path.
95
0
    virtual std::string get_current_scan_range_name() { return "not implemented"; }
96
97
#ifdef BE_TEST
98
    static uint64_t TEST_build_condition_cache_digest(uint64_t seed,
99
                                                      const VExprContextSPtrs& conjuncts);
100
#endif
101
102
protected:
103
    // Rebuild the condition-cache digest from the scanner's current conjunct snapshot. The local
104
    // state's digest is used only as a safety gate: zero means condition cache was disabled during
105
    // scan-node open (for example by TopN or an expression without a reliable digest).
106
    uint64_t _current_condition_cache_digest() const;
107
    static uint64_t _build_condition_cache_digest(uint64_t seed,
108
                                                  const VExprContextSPtrs& conjuncts);
109
110
0
    virtual Status _open_impl(RuntimeState* state) {
111
0
        _block_avg_bytes = state->batch_size() * 8;
112
0
        return Status::OK();
113
0
    }
114
115
    // Subclass should implement this to return data.
116
    virtual Status _get_block_impl(RuntimeState* state, Block* block, bool* eof) = 0;
117
118
2
    Status _merge_padding_block() {
119
2
        if (_padding_block.empty()) {
120
1
            _padding_block.swap(_origin_block);
121
1
        } else if (_origin_block.rows()) {
122
1
            ScopedMutableBlock scoped_mutable_block(&_padding_block);
123
1
            auto& mutable_block = scoped_mutable_block.mutable_block();
124
1
            RETURN_IF_ERROR(mutable_block.merge(_origin_block));
125
1
        }
126
2
        return Status::OK();
127
2
    }
128
129
    // Update the counters before closing this scanner
130
    virtual void _collect_profile_before_close();
131
132
    // Whether rows filtered/unselected by this scanner should be reported to the load
133
    // counters in RuntimeState. Only the scanner reading the load source data should
134
    // report, otherwise rows filtered by query predicates (e.g. in INSERT INTO ... SELECT
135
    // or DELETE FROM ... WHERE) would be mixed into load counters and make
136
    // num_rows_load_success() negative.
137
0
    virtual bool _should_update_load_counters() const { return _is_load; }
138
139
    // Filter the output block finally.
140
    Status _filter_output_block(Block* block);
141
142
    Status _do_projections(Block* origin_block, Block* output_block);
143
144
public:
145
0
    int64_t get_time_cost_ns() const { return _per_scanner_timer; }
146
147
0
    int64_t projection_time() const { return _projection_timer; }
148
0
    int64_t get_rows_read() const { return _num_rows_read; }
149
150
0
    bool has_prepared() const { return _has_prepared; }
151
152
    Status try_append_late_arrival_runtime_filter();
153
154
    // Call start_wait_worker_timer() when submit the scanner to the thread pool.
155
    // And call update_wait_worker_timer() when it is actually being executed.
156
0
    void start_wait_worker_timer() {
157
0
        _watch.reset();
158
0
        _watch.start();
159
0
    }
160
161
0
    void start_scan_cpu_timer() {
162
0
        _cpu_watch.reset();
163
0
        _cpu_watch.start();
164
0
    }
165
166
0
    void update_wait_worker_timer() { _scanner_wait_worker_timer += _watch.elapsed_time(); }
167
168
0
    int64_t get_scanner_wait_worker_timer() const { return _scanner_wait_worker_timer; }
169
170
    void update_scan_cpu_timer();
171
172
    // Some counters need to be updated realtime, for example, workload group policy need
173
    // scan bytes to cancel the query exceed limit.
174
0
    virtual void update_realtime_counters() {}
175
176
290
    RuntimeState* runtime_state() { return _state; }
177
178
0
    bool is_open() const { return _is_open; }
179
0
    void set_opened() { _is_open = true; }
180
181
0
    virtual doris::TabletStorageType get_storage_type() {
182
0
        return doris::TabletStorageType::STORAGE_TYPE_REMOTE;
183
0
    }
184
185
0
    bool need_to_close() const { return _need_to_close; }
186
187
0
    void mark_to_need_to_close() {
188
        // If the scanner is failed during init or open, then not need update counters
189
        // because the query is fail and the counter is useless. And it may core during
190
        // update counters. For example, update counters depend on scanner's tablet, but
191
        // the tablet == null when init failed.
192
0
        if (_is_open) {
193
0
            _collect_profile_before_close();
194
0
        }
195
0
        _need_to_close = true;
196
0
    }
197
198
0
    void set_status_on_failure(const Status& st) { _status = st; }
199
200
0
    int64_t limit() const { return _limit; }
201
202
0
    auto get_block_avg_bytes() const { return _block_avg_bytes; }
203
204
0
    void update_block_avg_bytes(size_t block_avg_bytes) { _block_avg_bytes = block_avg_bytes; }
205
206
protected:
207
    RuntimeState* _state = nullptr;
208
    ScanLocalStateBase* _local_state = nullptr;
209
210
    // Set if scan node has sort limit info
211
    int64_t _limit = -1;
212
213
    RuntimeProfile* _profile = nullptr;
214
215
    const TupleDescriptor* _output_tuple_desc = nullptr;
216
    const RowDescriptor* _output_row_descriptor = nullptr;
217
218
    // If _input_tuple_desc is set, the scanner will read data into
219
    // this _input_block first, then convert to the output block.
220
    Block _input_block;
221
222
    bool _is_open = false;
223
    bool _is_closed = false;
224
    bool _need_to_close = false;
225
    Status _status;
226
227
    // If _applied_rf_num == _total_rf_num
228
    // means all runtime filters are arrived and applied.
229
    int _applied_rf_num = 0;
230
    int _total_rf_num = 0;
231
    // Cloned from _conjuncts of scan node.
232
    // It includes predicate in SQL and runtime filters.
233
    VExprContextSPtrs _conjuncts;
234
    VExprContextSPtrs _projections;
235
    // Used in common subexpression elimination to compute intermediate results.
236
    std::vector<VExprContextSPtrs> _intermediate_projections;
237
    Block _origin_block;
238
    Block _padding_block;
239
240
    VExprContextSPtrs _common_expr_ctxs_push_down;
241
242
    // num of rows read from scanner
243
    int64_t _num_rows_read = 0;
244
245
    int64_t _num_byte_read = 0;
246
247
    // num of rows return from scanner, after filter block
248
    int64_t _num_rows_return = 0;
249
250
    size_t _block_avg_bytes = 0;
251
252
    // Set true after counter is updated finally
253
    bool _has_updated_counter = false;
254
255
    // watch to count the time wait for scanner thread
256
    MonotonicStopWatch _watch;
257
    // Do not use ScopedTimer. There is no guarantee that, the counter
258
    ThreadCpuStopWatch _cpu_watch;
259
    int64_t _scanner_wait_worker_timer = 0;
260
    int64_t _scan_cpu_timer = 0;
261
262
    bool _is_load = false;
263
264
    bool _has_prepared = false;
265
266
    ScannerCounter _counter;
267
    int64_t _per_scanner_timer = 0;
268
    int64_t _projection_timer = 0;
269
270
    bool _should_stop = false;
271
};
272
273
using ScannerSPtr = std::shared_ptr<Scanner>;
274
275
} // namespace doris