Coverage Report

Created: 2026-04-10 06:20

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