Coverage Report

Created: 2026-06-24 18:16

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