Coverage Report

Created: 2026-08-18 22:22

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
45
    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
19
            : _state(state), _limit(1), _profile(profile), _total_rf_num(0), _has_prepared(false) {
59
19
        DorisMetrics::instance()->scanner_cnt->increment(1);
60
19
    };
61
62
39
    virtual ~Scanner() {
63
39
        SCOPED_SWITCH_THREAD_MEM_TRACKER_LIMITER(_state->query_mem_tracker());
64
39
        _input_block.clear();
65
39
        _conjuncts.clear();
66
39
        _projections.clear();
67
39
        _origin_block.clear();
68
39
        _common_expr_ctxs_push_down.clear();
69
39
        DorisMetrics::instance()->scanner_cnt->increment(-1);
70
39
    }
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
0
    virtual bool _can_merge_padding_blocks(const Block& /*left*/, const Block& /*right*/) const {
119
0
        return true;
120
0
    }
121
122
2
    Status _merge_padding_block() {
123
2
        if (_padding_block.empty()) {
124
1
            _padding_block.swap(_origin_block);
125
1
        } else if (_origin_block.rows()) {
126
1
            ScopedMutableBlock scoped_mutable_block(&_padding_block);
127
1
            auto& mutable_block = scoped_mutable_block.mutable_block();
128
1
            RETURN_IF_ERROR(mutable_block.merge(_origin_block));
129
1
        }
130
2
        return Status::OK();
131
2
    }
132
133
    // Update the counters before closing this scanner
134
    virtual void _collect_profile_before_close();
135
136
    // Whether rows filtered/unselected by this scanner should be reported to the load
137
    // counters in RuntimeState. Only the scanner reading the load source data should
138
    // report, otherwise rows filtered by query predicates (e.g. in INSERT INTO ... SELECT
139
    // or DELETE FROM ... WHERE) would be mixed into load counters and make
140
    // num_rows_load_success() negative.
141
0
    virtual bool _should_update_load_counters() const { return _is_load; }
142
143
    // Filter the output block finally.
144
    virtual Status _filter_output_block(Block* block);
145
146
    Status _do_projections(Block* origin_block, Block* output_block);
147
148
public:
149
0
    int64_t get_time_cost_ns() const { return _per_scanner_timer; }
150
151
0
    int64_t projection_time() const { return _projection_timer; }
152
0
    int64_t get_rows_read() const { return _num_rows_read; }
153
154
0
    bool has_prepared() const { return _has_prepared; }
155
156
    Status try_append_late_arrival_runtime_filter();
157
158
    // Call start_wait_worker_timer() when submit the scanner to the thread pool.
159
    // And call update_wait_worker_timer() when it is actually being executed.
160
0
    void start_wait_worker_timer() {
161
0
        _watch.reset();
162
0
        _watch.start();
163
0
    }
164
165
0
    void start_scan_cpu_timer() {
166
0
        _cpu_watch.reset();
167
0
        _cpu_watch.start();
168
0
    }
169
170
0
    void update_wait_worker_timer() { _scanner_wait_worker_timer += _watch.elapsed_time(); }
171
172
0
    int64_t get_scanner_wait_worker_timer() const { return _scanner_wait_worker_timer; }
173
174
    void update_scan_cpu_timer();
175
176
    // Some counters need to be updated realtime, for example, workload group policy need
177
    // scan bytes to cancel the query exceed limit.
178
0
    virtual void update_realtime_counters() {}
179
180
292
    RuntimeState* runtime_state() { return _state; }
181
182
0
    bool is_open() const { return _is_open; }
183
0
    void set_opened() { _is_open = true; }
184
185
0
    virtual doris::TabletStorageType get_storage_type() {
186
0
        return doris::TabletStorageType::STORAGE_TYPE_REMOTE;
187
0
    }
188
189
0
    bool need_to_close() const { return _need_to_close; }
190
191
0
    void mark_to_need_to_close() {
192
        // If the scanner is failed during init or open, then not need update counters
193
        // because the query is fail and the counter is useless. And it may core during
194
        // update counters. For example, update counters depend on scanner's tablet, but
195
        // the tablet == null when init failed.
196
0
        if (_is_open) {
197
0
            _collect_profile_before_close();
198
0
        }
199
0
        _need_to_close = true;
200
0
    }
201
202
0
    void set_status_on_failure(const Status& st) { _status = st; }
203
204
0
    int64_t limit() const { return _limit; }
205
206
0
    auto get_block_avg_bytes() const { return _block_avg_bytes; }
207
208
0
    void update_block_avg_bytes(size_t block_avg_bytes) { _block_avg_bytes = block_avg_bytes; }
209
210
protected:
211
    RuntimeState* _state = nullptr;
212
    ScanLocalStateBase* _local_state = nullptr;
213
214
    // Set if scan node has sort limit info
215
    int64_t _limit = -1;
216
217
    RuntimeProfile* _profile = nullptr;
218
219
    const TupleDescriptor* _output_tuple_desc = nullptr;
220
    const RowDescriptor* _output_row_descriptor = nullptr;
221
222
    // If _input_tuple_desc is set, the scanner will read data into
223
    // this _input_block first, then convert to the output block.
224
    Block _input_block;
225
226
    bool _is_open = false;
227
    bool _is_closed = false;
228
    bool _need_to_close = false;
229
    Status _status;
230
231
    // If _applied_rf_num == _total_rf_num
232
    // means all runtime filters are arrived and applied.
233
    int _applied_rf_num = 0;
234
    int _total_rf_num = 0;
235
    // Cloned from _conjuncts of scan node.
236
    // It includes predicate in SQL and runtime filters.
237
    VExprContextSPtrs _conjuncts;
238
    VExprContextSPtrs _projections;
239
    // Used in common subexpression elimination to compute intermediate results.
240
    std::vector<VExprContextSPtrs> _intermediate_projections;
241
    Block _origin_block;
242
    Block _padding_block;
243
244
    VExprContextSPtrs _common_expr_ctxs_push_down;
245
246
    // num of rows read from scanner
247
    int64_t _num_rows_read = 0;
248
249
    int64_t _num_byte_read = 0;
250
251
    // num of rows return from scanner, after filter block
252
    int64_t _num_rows_return = 0;
253
254
    size_t _block_avg_bytes = 0;
255
256
    // Set true after counter is updated finally
257
    bool _has_updated_counter = false;
258
259
    // watch to count the time wait for scanner thread
260
    MonotonicStopWatch _watch;
261
    // Do not use ScopedTimer. There is no guarantee that, the counter
262
    ThreadCpuStopWatch _cpu_watch;
263
    int64_t _scanner_wait_worker_timer = 0;
264
    int64_t _scan_cpu_timer = 0;
265
266
    bool _is_load = false;
267
268
    bool _has_prepared = false;
269
270
    ScannerCounter _counter;
271
    int64_t _per_scanner_timer = 0;
272
    int64_t _projection_timer = 0;
273
274
    bool _should_stop = false;
275
};
276
277
using ScannerSPtr = std::shared_ptr<Scanner>;
278
279
} // namespace doris