Coverage Report

Created: 2026-06-09 21:04

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