Coverage Report

Created: 2026-05-08 18:22

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