Coverage Report

Created: 2026-11-03 19:57

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