Coverage Report

Created: 2026-03-25 17:26

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
1.01M
        : _state(state),
39
1.01M
          _local_state(local_state),
40
1.01M
          _limit(limit),
41
1.01M
          _profile(profile),
42
1.01M
          _output_tuple_desc(_local_state->output_tuple_desc()),
43
1.01M
          _output_row_descriptor(_local_state->_parent->output_row_descriptor()),
44
1.01M
          _has_prepared(false),
45
1.01M
          _remote_slow_task_threshold(state->query_options().__isset.remote_slow_task_threshold
46
1.01M
                                              ? state->query_options().remote_slow_task_threshold
47
1.01M
                                              : INT64_MAX) {
48
1.01M
    _total_rf_num = cast_set<int>(_local_state->_helper.runtime_filter_nums());
49
1.01M
    DorisMetrics::instance()->scanner_cnt->increment(1);
50
1.01M
}
51
52
0
bool Scanner::is_slow_task() const {
53
0
    return _file_cache_profile_reporter &&
54
0
           _file_cache_profile_reporter->num_remote_io_total->value() > _remote_slow_task_threshold;
55
0
}
56
57
1.01M
Status Scanner::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) {
58
1.01M
    if (!conjuncts.empty()) {
59
14.3k
        _conjuncts.resize(conjuncts.size());
60
32.1k
        for (size_t i = 0; i != conjuncts.size(); ++i) {
61
17.8k
            RETURN_IF_ERROR(conjuncts[i]->clone(state, _conjuncts[i]));
62
17.8k
        }
63
14.3k
    }
64
65
1.01M
    const auto& projections = _local_state->_projections;
66
1.01M
    if (!projections.empty()) {
67
946k
        _projections.resize(projections.size());
68
8.72M
        for (size_t i = 0; i != projections.size(); ++i) {
69
7.78M
            RETURN_IF_ERROR(projections[i]->clone(state, _projections[i]));
70
7.78M
        }
71
946k
    }
72
73
1.01M
    const auto& intermediate_projections = _local_state->_intermediate_projections;
74
1.01M
    if (!intermediate_projections.empty()) {
75
10.7k
        _intermediate_projections.resize(intermediate_projections.size());
76
24.2k
        for (int i = 0; i < intermediate_projections.size(); i++) {
77
13.5k
            _intermediate_projections[i].resize(intermediate_projections[i].size());
78
81.0k
            for (int j = 0; j < intermediate_projections[i].size(); j++) {
79
67.5k
                RETURN_IF_ERROR(intermediate_projections[i][j]->clone(
80
67.5k
                        state, _intermediate_projections[i][j]));
81
67.5k
            }
82
13.5k
        }
83
10.7k
    }
84
85
1.01M
    return Status::OK();
86
1.01M
}
87
88
1.05M
Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool* eos) {
89
1.05M
    SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().vscanner_get_block);
90
1.05M
    auto& row_descriptor = _local_state->_parent->row_descriptor();
91
1.05M
    if (_output_row_descriptor) {
92
954k
        if (_alreay_eos) {
93
0
            *eos = true;
94
0
            _padding_block.swap(_origin_block);
95
954k
        } else {
96
954k
            _origin_block.clear_column_data(row_descriptor.num_materialized_slots());
97
954k
            const auto min_batch_size = std::max(state->batch_size() / 2, 1);
98
2.11M
            while (_padding_block.rows() < min_batch_size && !*eos) {
99
1.17M
                RETURN_IF_ERROR(get_block(state, &_origin_block, eos));
100
1.17M
                if (_origin_block.rows() >= min_batch_size) {
101
15.6k
                    break;
102
15.6k
                }
103
104
1.16M
                if (_origin_block.rows() + _padding_block.rows() <= state->batch_size()) {
105
1.16M
                    RETURN_IF_ERROR(_merge_padding_block());
106
1.16M
                    _origin_block.clear_column_data(row_descriptor.num_materialized_slots());
107
1.16M
                } else {
108
998
                    if (_origin_block.rows() < _padding_block.rows()) {
109
0
                        _padding_block.swap(_origin_block);
110
0
                    }
111
998
                    break;
112
998
                }
113
1.16M
            }
114
954k
        }
115
116
        // first output the origin block change eos = false, next time output padding block
117
        // set the eos to true
118
954k
        if (*eos && !_padding_block.empty() && !_origin_block.empty()) {
119
0
            _alreay_eos = true;
120
0
            *eos = false;
121
0
        }
122
954k
        if (_origin_block.empty() && !_padding_block.empty()) {
123
184k
            _padding_block.swap(_origin_block);
124
184k
        }
125
954k
        return _do_projections(&_origin_block, block);
126
954k
    } else {
127
99.7k
        return get_block(state, block, eos);
128
99.7k
    }
129
1.05M
}
130
131
1.27M
Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) {
132
    // only empty block should be here
133
1.27M
    DCHECK(block->rows() == 0);
134
    // scanner running time
135
1.27M
    SCOPED_RAW_TIMER(&_per_scanner_timer);
136
1.27M
    int64_t rows_read_threshold = _num_rows_read + config::doris_scanner_row_num;
137
1.27M
    if (!block->mem_reuse()) {
138
8.73M
        for (auto* const slot_desc : _output_tuple_desc->slots()) {
139
8.73M
            block->insert(ColumnWithTypeAndName(slot_desc->get_empty_mutable_column(),
140
8.73M
                                                slot_desc->get_data_type_ptr(),
141
8.73M
                                                slot_desc->col_name()));
142
8.73M
        }
143
1.12M
    }
144
145
1.27M
    {
146
1.28M
        do {
147
            // 1. Get input block from scanner
148
1.28M
            {
149
                // get block time
150
1.28M
                SCOPED_TIMER(_local_state->_scan_timer);
151
1.28M
                RETURN_IF_ERROR(_get_block_impl(state, block, eof));
152
1.28M
                if (*eof) {
153
1.00M
                    DCHECK(block->rows() == 0);
154
1.00M
                    break;
155
1.00M
                }
156
279k
                _num_rows_read += block->rows();
157
279k
                _num_byte_read += block->allocated_bytes();
158
279k
            }
159
160
            // 2. Filter the output block finally.
161
0
            {
162
279k
                SCOPED_TIMER(_local_state->_filter_timer);
163
279k
                RETURN_IF_ERROR(_filter_output_block(block));
164
279k
            }
165
            // record rows return (after filter) for _limit check
166
279k
            _num_rows_return += block->rows();
167
279k
        } while (!_should_stop && !state->is_cancelled() && block->rows() == 0 && !(*eof) &&
168
279k
                 _num_rows_read < rows_read_threshold);
169
1.27M
    }
170
171
1.27M
    if (state->is_cancelled()) {
172
        // TODO: Should return the specific ErrorStatus instead of just Cancelled.
173
10
        return Status::Cancelled("cancelled");
174
10
    }
175
1.27M
    *eof = *eof || _should_stop;
176
    // set eof to true if per scanner limit is reached
177
    // currently for query: ORDER BY key LIMIT n
178
1.27M
    *eof = *eof || (_limit > 0 && _num_rows_return >= _limit);
179
180
1.27M
    return Status::OK();
181
1.27M
}
182
183
278k
Status Scanner::_filter_output_block(Block* block) {
184
278k
    auto old_rows = block->rows();
185
278k
    Status st = VExprContext::filter_block(_conjuncts, block, block->columns());
186
278k
    _counter.num_rows_unselected += old_rows - block->rows();
187
278k
    return st;
188
278k
}
189
190
956k
Status Scanner::_do_projections(Block* origin_block, Block* output_block) {
191
956k
    SCOPED_RAW_TIMER(&_per_scanner_timer);
192
956k
    SCOPED_RAW_TIMER(&_projection_timer);
193
194
956k
    const size_t rows = origin_block->rows();
195
956k
    if (rows == 0) {
196
756k
        return Status::OK();
197
756k
    }
198
199k
    Block input_block = *origin_block;
199
200
199k
    std::vector<int> result_column_ids;
201
199k
    for (auto& projections : _intermediate_projections) {
202
14.2k
        result_column_ids.resize(projections.size());
203
90.2k
        for (int i = 0; i < projections.size(); i++) {
204
75.9k
            RETURN_IF_ERROR(projections[i]->execute(&input_block, &result_column_ids[i]));
205
75.9k
        }
206
14.2k
        input_block.shuffle_columns(result_column_ids);
207
14.2k
    }
208
209
199k
    DCHECK_EQ(rows, input_block.rows());
210
199k
    MutableBlock mutable_block =
211
199k
            VectorizedUtils::build_mutable_mem_reuse_block(output_block, *_output_row_descriptor);
212
213
199k
    auto& mutable_columns = mutable_block.mutable_columns();
214
215
199k
    DCHECK_EQ(mutable_columns.size(), _projections.size());
216
217
738k
    for (int i = 0; i < mutable_columns.size(); ++i) {
218
540k
        ColumnPtr column_ptr;
219
540k
        RETURN_IF_ERROR(_projections[i]->execute(&input_block, column_ptr));
220
539k
        column_ptr = column_ptr->convert_to_full_column_if_const();
221
539k
        if (mutable_columns[i]->is_nullable() != column_ptr->is_nullable()) {
222
0
            throw Exception(ErrorCode::INTERNAL_ERROR, "Nullable mismatch");
223
0
        }
224
539k
        mutable_columns[i] = column_ptr->assume_mutable();
225
539k
    }
226
227
198k
    output_block->set_columns(std::move(mutable_columns));
228
229
    // origin columns was moved into output_block, so we need to set origin_block to empty columns
230
198k
    auto empty_columns = origin_block->clone_empty_columns();
231
198k
    origin_block->set_columns(std::move(empty_columns));
232
198k
    DCHECK_EQ(output_block->rows(), rows);
233
234
198k
    return Status::OK();
235
199k
}
236
237
1.01M
Status Scanner::try_append_late_arrival_runtime_filter() {
238
1.01M
    if (_applied_rf_num == _total_rf_num) {
239
961k
        return Status::OK();
240
961k
    }
241
1.01M
    DCHECK(_applied_rf_num < _total_rf_num);
242
51.9k
    int arrived_rf_num = 0;
243
51.9k
    RETURN_IF_ERROR(_local_state->update_late_arrival_runtime_filter(_state, arrived_rf_num));
244
245
51.9k
    if (arrived_rf_num == _applied_rf_num) {
246
        // No newly arrived runtime filters, just return;
247
1.08k
        return Status::OK();
248
1.08k
    }
249
250
    // avoid conjunct destroy in used by storage layer
251
50.8k
    _conjuncts.clear();
252
50.8k
    RETURN_IF_ERROR(_local_state->clone_conjunct_ctxs(_conjuncts));
253
50.8k
    return Status::OK();
254
50.8k
}
255
256
1.01M
Status Scanner::close(RuntimeState* state) {
257
1.01M
#ifndef BE_TEST
258
1.01M
    COUNTER_UPDATE(_local_state->_scanner_wait_worker_timer, _scanner_wait_worker_timer);
259
1.01M
#endif
260
1.01M
    return Status::OK();
261
1.01M
}
262
263
1.01M
bool Scanner::_try_close() {
264
1.01M
    bool expected = false;
265
1.01M
    return _is_closed.compare_exchange_strong(expected, true);
266
1.01M
}
267
268
1.00M
void Scanner::_collect_profile_before_close() {
269
1.00M
    COUNTER_UPDATE(_local_state->_scan_cpu_timer, _scan_cpu_timer);
270
1.00M
    COUNTER_UPDATE(_local_state->_rows_read_counter, _num_rows_read);
271
272
    // Update stats for load
273
1.00M
    _state->update_num_rows_load_filtered(_counter.num_rows_filtered);
274
1.00M
    _state->update_num_rows_load_unselected(_counter.num_rows_unselected);
275
1.00M
}
276
277
1.00M
void Scanner::update_scan_cpu_timer() {
278
1.00M
    int64_t cpu_time = _cpu_watch.elapsed_time();
279
1.00M
    _scan_cpu_timer += cpu_time;
280
1.00M
    if (_state && _state->get_query_ctx()) {
281
1.00M
        _state->get_query_ctx()->resource_ctx()->cpu_context()->update_cpu_cost_ms(cpu_time);
282
1.00M
    }
283
1.00M
}
284
285
} // namespace doris