Coverage Report

Created: 2026-03-12 14:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/scan/scanner.cpp
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
#include "exec/scan/scanner.h"
19
20
#include <glog/logging.h>
21
22
#include "common/config.h"
23
#include "common/status.h"
24
#include "core/block/column_with_type_and_name.h"
25
#include "core/column/column_nothing.h"
26
#include "exec/operator/scan_operator.h"
27
#include "exec/scan/scan_node.h"
28
#include "exprs/vexpr_context.h"
29
#include "runtime/descriptors.h"
30
#include "runtime/runtime_profile.h"
31
#include "util/concurrency_stats.h"
32
#include "util/defer_op.h"
33
34
namespace doris {
35
36
Scanner::Scanner(RuntimeState* state, ScanLocalStateBase* local_state, int64_t limit,
37
                 RuntimeProfile* profile)
38
414k
        : _state(state),
39
414k
          _local_state(local_state),
40
414k
          _limit(limit),
41
414k
          _profile(profile),
42
414k
          _output_tuple_desc(_local_state->output_tuple_desc()),
43
414k
          _output_row_descriptor(_local_state->_parent->output_row_descriptor()),
44
414k
          _has_prepared(false) {
45
414k
    _total_rf_num = cast_set<int>(_local_state->_helper.runtime_filter_nums());
46
414k
    DorisMetrics::instance()->scanner_cnt->increment(1);
47
414k
}
48
49
414k
Status Scanner::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) {
50
414k
    if (!conjuncts.empty()) {
51
41.7k
        _conjuncts.resize(conjuncts.size());
52
100k
        for (size_t i = 0; i != conjuncts.size(); ++i) {
53
58.4k
            RETURN_IF_ERROR(conjuncts[i]->clone(state, _conjuncts[i]));
54
58.4k
        }
55
41.7k
    }
56
57
414k
    const auto& projections = _local_state->_projections;
58
414k
    if (!projections.empty()) {
59
368k
        _projections.resize(projections.size());
60
5.03M
        for (size_t i = 0; i != projections.size(); ++i) {
61
4.66M
            RETURN_IF_ERROR(projections[i]->clone(state, _projections[i]));
62
4.66M
        }
63
368k
    }
64
65
414k
    const auto& intermediate_projections = _local_state->_intermediate_projections;
66
414k
    if (!intermediate_projections.empty()) {
67
92
        _intermediate_projections.resize(intermediate_projections.size());
68
262
        for (int i = 0; i < intermediate_projections.size(); i++) {
69
170
            _intermediate_projections[i].resize(intermediate_projections[i].size());
70
1.23k
            for (int j = 0; j < intermediate_projections[i].size(); j++) {
71
1.06k
                RETURN_IF_ERROR(intermediate_projections[i][j]->clone(
72
1.06k
                        state, _intermediate_projections[i][j]));
73
1.06k
            }
74
170
        }
75
92
    }
76
77
414k
    return Status::OK();
78
414k
}
79
80
478k
Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool* eos) {
81
478k
    SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().vscanner_get_block);
82
478k
    auto& row_descriptor = _local_state->_parent->row_descriptor();
83
478k
    if (_output_row_descriptor) {
84
394k
        if (_alreay_eos) {
85
0
            *eos = true;
86
0
            _padding_block.swap(_origin_block);
87
394k
        } else {
88
394k
            _origin_block.clear_column_data(row_descriptor.num_materialized_slots());
89
394k
            const auto min_batch_size = std::max(state->batch_size() / 2, 1);
90
837k
            while (_padding_block.rows() < min_batch_size && !*eos) {
91
467k
                RETURN_IF_ERROR(get_block(state, &_origin_block, eos));
92
467k
                if (_origin_block.rows() >= min_batch_size) {
93
24.2k
                    break;
94
24.2k
                }
95
96
443k
                if (_origin_block.rows() + _padding_block.rows() <= state->batch_size()) {
97
443k
                    RETURN_IF_ERROR(_merge_padding_block());
98
443k
                    _origin_block.clear_column_data(row_descriptor.num_materialized_slots());
99
443k
                } else {
100
35
                    if (_origin_block.rows() < _padding_block.rows()) {
101
0
                        _padding_block.swap(_origin_block);
102
0
                    }
103
35
                    break;
104
35
                }
105
443k
            }
106
394k
        }
107
108
        // first output the origin block change eos = false, next time output padding block
109
        // set the eos to true
110
394k
        if (*eos && !_padding_block.empty() && !_origin_block.empty()) {
111
0
            _alreay_eos = true;
112
0
            *eos = false;
113
0
        }
114
394k
        if (_origin_block.empty() && !_padding_block.empty()) {
115
49.2k
            _padding_block.swap(_origin_block);
116
49.2k
        }
117
394k
        return _do_projections(&_origin_block, block);
118
394k
    } else {
119
84.3k
        return get_block(state, block, eos);
120
84.3k
    }
121
478k
}
122
123
551k
Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) {
124
    // only empty block should be here
125
551k
    DCHECK(block->rows() == 0);
126
    // scanner running time
127
551k
    SCOPED_RAW_TIMER(&_per_scanner_timer);
128
551k
    int64_t rows_read_threshold = _num_rows_read + config::doris_scanner_row_num;
129
551k
    if (!block->mem_reuse()) {
130
5.25M
        for (auto* const slot_desc : _output_tuple_desc->slots()) {
131
5.25M
            block->insert(ColumnWithTypeAndName(slot_desc->get_empty_mutable_column(),
132
5.25M
                                                slot_desc->get_data_type_ptr(),
133
5.25M
                                                slot_desc->col_name()));
134
5.25M
        }
135
416k
    }
136
137
551k
    {
138
619k
        do {
139
            // 1. Get input block from scanner
140
619k
            {
141
                // get block time
142
619k
                SCOPED_TIMER(_local_state->_scan_timer);
143
619k
                RETURN_IF_ERROR(_get_block_impl(state, block, eof));
144
619k
                if (*eof) {
145
413k
                    DCHECK(block->rows() == 0);
146
413k
                    break;
147
413k
                }
148
206k
                _num_rows_read += block->rows();
149
206k
                _num_byte_read += block->allocated_bytes();
150
206k
            }
151
152
            // 2. Filter the output block finally.
153
0
            {
154
206k
                SCOPED_TIMER(_local_state->_filter_timer);
155
206k
                RETURN_IF_ERROR(_filter_output_block(block));
156
206k
            }
157
            // record rows return (after filter) for _limit check
158
206k
            _num_rows_return += block->rows();
159
206k
        } while (!_should_stop && !state->is_cancelled() && block->rows() == 0 && !(*eof) &&
160
206k
                 _num_rows_read < rows_read_threshold);
161
551k
    }
162
163
551k
    if (state->is_cancelled()) {
164
        // TODO: Should return the specific ErrorStatus instead of just Cancelled.
165
34
        return Status::Cancelled("cancelled");
166
34
    }
167
551k
    *eof = *eof || _should_stop;
168
    // set eof to true if per scanner limit is reached
169
    // currently for query: ORDER BY key LIMIT n
170
551k
    *eof = *eof || (_limit > 0 && _num_rows_return >= _limit);
171
172
551k
    return Status::OK();
173
551k
}
174
175
206k
Status Scanner::_filter_output_block(Block* block) {
176
206k
    auto old_rows = block->rows();
177
206k
    Status st = VExprContext::filter_block(_conjuncts, block, block->columns());
178
206k
    _counter.num_rows_unselected += old_rows - block->rows();
179
206k
    return st;
180
206k
}
181
182
394k
Status Scanner::_do_projections(Block* origin_block, Block* output_block) {
183
394k
    SCOPED_RAW_TIMER(&_per_scanner_timer);
184
394k
    SCOPED_RAW_TIMER(&_projection_timer);
185
186
394k
    const size_t rows = origin_block->rows();
187
394k
    if (rows == 0) {
188
320k
        return Status::OK();
189
320k
    }
190
73.4k
    Block input_block = *origin_block;
191
192
73.4k
    std::vector<int> result_column_ids;
193
73.4k
    for (auto& projections : _intermediate_projections) {
194
4.52k
        result_column_ids.resize(projections.size());
195
40.5k
        for (int i = 0; i < projections.size(); i++) {
196
36.0k
            RETURN_IF_ERROR(projections[i]->execute(&input_block, &result_column_ids[i]));
197
36.0k
        }
198
4.52k
        input_block.shuffle_columns(result_column_ids);
199
4.52k
    }
200
201
73.4k
    DCHECK_EQ(rows, input_block.rows());
202
73.4k
    MutableBlock mutable_block =
203
73.4k
            VectorizedUtils::build_mutable_mem_reuse_block(output_block, *_output_row_descriptor);
204
205
73.4k
    auto& mutable_columns = mutable_block.mutable_columns();
206
207
73.4k
    DCHECK_EQ(mutable_columns.size(), _projections.size());
208
209
384k
    for (int i = 0; i < mutable_columns.size(); ++i) {
210
311k
        ColumnPtr column_ptr;
211
311k
        RETURN_IF_ERROR(_projections[i]->execute(&input_block, column_ptr));
212
311k
        column_ptr = column_ptr->convert_to_full_column_if_const();
213
311k
        if (mutable_columns[i]->is_nullable() != column_ptr->is_nullable()) {
214
0
            throw Exception(ErrorCode::INTERNAL_ERROR, "Nullable mismatch");
215
0
        }
216
311k
        mutable_columns[i] = column_ptr->assume_mutable();
217
311k
    }
218
219
73.4k
    output_block->set_columns(std::move(mutable_columns));
220
221
    // origin columns was moved into output_block, so we need to set origin_block to empty columns
222
73.4k
    auto empty_columns = origin_block->clone_empty_columns();
223
73.4k
    origin_block->set_columns(std::move(empty_columns));
224
73.4k
    DCHECK_EQ(output_block->rows(), rows);
225
226
73.4k
    return Status::OK();
227
73.4k
}
228
229
419k
Status Scanner::try_append_late_arrival_runtime_filter() {
230
419k
    if (_applied_rf_num == _total_rf_num) {
231
415k
        return Status::OK();
232
415k
    }
233
419k
    DCHECK(_applied_rf_num < _total_rf_num);
234
3.81k
    int arrived_rf_num = 0;
235
3.81k
    RETURN_IF_ERROR(_local_state->update_late_arrival_runtime_filter(_state, arrived_rf_num));
236
237
3.81k
    if (arrived_rf_num == _applied_rf_num) {
238
        // No newly arrived runtime filters, just return;
239
0
        return Status::OK();
240
0
    }
241
242
    // avoid conjunct destroy in used by storage layer
243
3.81k
    _conjuncts.clear();
244
3.81k
    RETURN_IF_ERROR(_local_state->clone_conjunct_ctxs(_conjuncts));
245
3.81k
    return Status::OK();
246
3.81k
}
247
248
414k
Status Scanner::close(RuntimeState* state) {
249
414k
#ifndef BE_TEST
250
414k
    COUNTER_UPDATE(_local_state->_scanner_wait_worker_timer, _scanner_wait_worker_timer);
251
414k
#endif
252
414k
    return Status::OK();
253
414k
}
254
255
414k
bool Scanner::_try_close() {
256
414k
    bool expected = false;
257
414k
    return _is_closed.compare_exchange_strong(expected, true);
258
414k
}
259
260
413k
void Scanner::_collect_profile_before_close() {
261
413k
    COUNTER_UPDATE(_local_state->_scan_cpu_timer, _scan_cpu_timer);
262
413k
    COUNTER_UPDATE(_local_state->_rows_read_counter, _num_rows_read);
263
264
    // Update stats for load
265
413k
    _state->update_num_rows_load_filtered(_counter.num_rows_filtered);
266
413k
    _state->update_num_rows_load_unselected(_counter.num_rows_unselected);
267
413k
}
268
269
4.97k
void Scanner::update_scan_cpu_timer() {
270
4.97k
    int64_t cpu_time = _cpu_watch.elapsed_time();
271
4.97k
    _scan_cpu_timer += cpu_time;
272
4.97k
    if (_state && _state->get_query_ctx()) {
273
4.97k
        _state->get_query_ctx()->resource_ctx()->cpu_context()->update_cpu_cost_ms(cpu_time);
274
4.97k
    }
275
4.97k
}
276
277
} // namespace doris