forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.cpp
More file actions
304 lines (264 loc) · 11.8 KB
/
Copy pathscanner.cpp
File metadata and controls
304 lines (264 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "exec/scan/scanner.h"
#include <glog/logging.h>
#include "common/config.h"
#include "common/status.h"
#include "core/block/column_with_type_and_name.h"
#include "core/column/column_nothing.h"
#include "exec/operator/scan_operator.h"
#include "exec/scan/scan_node.h"
#include "exprs/vexpr_context.h"
#include "runtime/descriptors.h"
#include "runtime/runtime_profile.h"
#include "util/concurrency_stats.h"
#include "util/defer_op.h"
namespace doris {
Scanner::Scanner(RuntimeState* state, ScanLocalStateBase* local_state, int64_t limit,
RuntimeProfile* profile)
: _state(state),
_local_state(local_state),
_limit(limit),
_profile(profile),
_output_tuple_desc(_local_state->output_tuple_desc()),
_output_row_descriptor(_local_state->_parent->output_row_descriptor()),
_has_prepared(false) {
_total_rf_num = cast_set<int>(_local_state->_helper.runtime_filter_nums());
DorisMetrics::instance()->scanner_cnt->increment(1);
}
Status Scanner::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) {
// All scanners share a remaining-limit counter so a LIMIT query can
// stop once enough rows have been collected across scanners.
// Key TopN scans have no ordinary scan LIMIT, so each scanner can
// independently produce its full local top-N.
_shared_scan_limit = _local_state->shared_scan_limit_ptr();
if (!conjuncts.empty()) {
_conjuncts.resize(conjuncts.size());
for (size_t i = 0; i != conjuncts.size(); ++i) {
RETURN_IF_ERROR(conjuncts[i]->clone(state, _conjuncts[i]));
}
}
const auto& projections = _local_state->_projections;
if (!projections.empty()) {
_projections.resize(projections.size());
for (size_t i = 0; i != projections.size(); ++i) {
RETURN_IF_ERROR(projections[i]->clone(state, _projections[i]));
}
}
const auto& intermediate_projections = _local_state->_intermediate_projections;
if (!intermediate_projections.empty()) {
_intermediate_projections.resize(intermediate_projections.size());
for (int i = 0; i < intermediate_projections.size(); i++) {
_intermediate_projections[i].resize(intermediate_projections[i].size());
for (int j = 0; j < intermediate_projections[i].size(); j++) {
RETURN_IF_ERROR(intermediate_projections[i][j]->clone(
state, _intermediate_projections[i][j]));
}
}
}
return Status::OK();
}
Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool* eos) {
SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().vscanner_get_block);
auto& row_descriptor = _local_state->_parent->row_descriptor();
if (_output_row_descriptor) {
_origin_block.clear_column_data(row_descriptor.num_materialized_slots());
const auto min_batch_size = std::max(state->batch_size() / 2, 1);
const auto block_max_bytes = state->preferred_block_size_bytes();
while (_padding_block.rows() < min_batch_size && _padding_block.bytes() < block_max_bytes &&
!*eos) {
RETURN_IF_ERROR(get_block(state, &_origin_block, eos));
if (*eos) {
// For the final block, merge any padding directly and return eos in this call.
// The merged tail can be larger than the target batch, but each source block is
// already bounded by the lower scanner.
RETURN_IF_ERROR(_merge_padding_block());
_origin_block.clear_column_data(row_descriptor.num_materialized_slots());
break;
}
if (_origin_block.rows() >= min_batch_size) {
break;
}
if (_origin_block.rows() + _padding_block.rows() <= state->batch_size() &&
_origin_block.bytes() + _padding_block.bytes() <= block_max_bytes) {
RETURN_IF_ERROR(_merge_padding_block());
_origin_block.clear_column_data(row_descriptor.num_materialized_slots());
} else {
if (_origin_block.rows() < _padding_block.rows()) {
_padding_block.swap(_origin_block);
}
break;
}
}
if (_origin_block.empty() && !_padding_block.empty()) {
_padding_block.swap(_origin_block);
}
return _do_projections(&_origin_block, block);
} else {
return get_block(state, block, eos);
}
}
Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) {
// only empty block should be here
DCHECK(block->rows() == 0);
// Stop early if other scanners have already collected enough rows
// for the SQL LIMIT. Skipped when _shared_scan_limit is null (topn
// path or no LIMIT).
if (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0) {
*eof = true;
return Status::OK();
}
// scanner running time
SCOPED_RAW_TIMER(&_per_scanner_timer);
int64_t rows_read_threshold = _num_rows_read + config::doris_scanner_row_num;
if (!block->mem_reuse()) {
for (auto* const slot_desc : _output_tuple_desc->slots()) {
block->insert(ColumnWithTypeAndName(slot_desc->get_empty_mutable_column(),
slot_desc->get_data_type_ptr(),
slot_desc->col_name()));
}
}
{
do {
// 1. Get input block from scanner
{
// get block time
SCOPED_TIMER(_local_state->_scan_timer);
RETURN_IF_ERROR(_get_block_impl(state, block, eof));
if (*eof) {
DCHECK(block->rows() == 0);
break;
}
_num_rows_read += block->rows();
_num_byte_read += block->allocated_bytes();
}
// 2. Filter the output block finally.
{
SCOPED_TIMER(_local_state->_filter_timer);
RETURN_IF_ERROR(_filter_output_block(block));
}
// record rows return (after filter) for _limit check
_num_rows_return += block->rows();
// Publish progress to the shared counter so peer scanners can
// observe it. The counter may go negative when several scanners
// subtract concurrently; that is harmless because the operator's
// reached_limit() makes the final cut.
if (_shared_scan_limit && block->rows() > 0) {
_shared_scan_limit->fetch_sub(block->rows(), std::memory_order_acq_rel);
}
} while (!_should_stop && !state->is_cancelled() && block->rows() == 0 && !(*eof) &&
_num_rows_read < rows_read_threshold);
}
if (state->is_cancelled()) {
// TODO: Should return the specific ErrorStatus instead of just Cancelled.
return Status::Cancelled("cancelled");
}
*eof = *eof || _should_stop;
// set eof to true if per scanner limit is reached
// currently for query: ORDER BY key LIMIT n
*eof = *eof || (_limit > 0 && _num_rows_return >= _limit);
*eof = *eof || (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0);
return Status::OK();
}
Status Scanner::_filter_output_block(Block* block) {
auto old_rows = block->rows();
Status st = VExprContext::filter_block(_conjuncts, block, block->columns());
_counter.num_rows_unselected += old_rows - block->rows();
return st;
}
Status Scanner::_do_projections(Block* origin_block, Block* output_block) {
SCOPED_RAW_TIMER(&_per_scanner_timer);
SCOPED_RAW_TIMER(&_projection_timer);
const size_t rows = origin_block->rows();
if (rows == 0) {
return Status::OK();
}
Block input_block = *origin_block;
std::vector<int> result_column_ids;
for (auto& projections : _intermediate_projections) {
result_column_ids.resize(projections.size());
for (int i = 0; i < projections.size(); i++) {
RETURN_IF_ERROR(projections[i]->execute(&input_block, &result_column_ids[i]));
}
input_block.shuffle_columns(result_column_ids);
}
DCHECK_EQ(rows, input_block.rows());
auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
output_block, *_output_row_descriptor);
auto& mutable_block = scoped_mutable_block.mutable_block();
auto& mutable_columns = mutable_block.mutable_columns();
DCHECK_EQ(mutable_columns.size(), _projections.size());
for (int i = 0; i < mutable_columns.size(); ++i) {
ColumnPtr column_ptr;
RETURN_IF_ERROR(_projections[i]->execute(&input_block, column_ptr));
column_ptr = column_ptr->convert_to_full_column_if_const();
if (mutable_columns[i]->is_nullable() != column_ptr->is_nullable()) {
throw Exception(ErrorCode::INTERNAL_ERROR, "Nullable mismatch");
}
mutable_columns[i] = IColumn::mutate(std::move(column_ptr));
}
scoped_mutable_block.restore();
// origin columns was moved into output_block, so we need to set origin_block to empty columns
auto empty_columns = origin_block->clone_empty_columns();
origin_block->set_columns(std::move(empty_columns));
DCHECK_EQ(output_block->rows(), rows);
return Status::OK();
}
Status Scanner::try_append_late_arrival_runtime_filter() {
if (_applied_rf_num == _total_rf_num) {
return Status::OK();
}
DCHECK(_applied_rf_num < _total_rf_num);
int arrived_rf_num = 0;
RETURN_IF_ERROR(_local_state->update_late_arrival_runtime_filter(_state, arrived_rf_num));
if (arrived_rf_num == _applied_rf_num) {
// No newly arrived runtime filters, just return;
return Status::OK();
}
// avoid conjunct destroy in used by storage layer
_conjuncts.clear();
RETURN_IF_ERROR(_local_state->clone_conjunct_ctxs(_conjuncts));
_applied_rf_num = arrived_rf_num;
return Status::OK();
}
Status Scanner::close(RuntimeState* state) {
#ifndef BE_TEST
COUNTER_UPDATE(_local_state->_scanner_wait_worker_timer, _scanner_wait_worker_timer);
#endif
return Status::OK();
}
bool Scanner::_try_close() {
bool expected = false;
return _is_closed.compare_exchange_strong(expected, true);
}
void Scanner::_collect_profile_before_close() {
COUNTER_UPDATE(_local_state->_scan_cpu_timer, _scan_cpu_timer);
COUNTER_UPDATE(_local_state->_rows_read_counter, _num_rows_read);
// Update stats for load. See _should_update_load_counters() for why this is gated.
if (_should_update_load_counters()) {
_state->update_num_rows_load_filtered(_counter.num_rows_filtered);
_state->update_num_rows_load_unselected(_counter.num_rows_unselected);
}
}
void Scanner::_update_scan_cpu_timer() {
int64_t cpu_time = _cpu_watch.elapsed_time();
_scan_cpu_timer += cpu_time;
if (_state && _state->get_query_ctx()) {
_state->get_query_ctx()->resource_ctx()->cpu_context()->update_cpu_cost_ms(cpu_time);
}
}
} // namespace doris