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 <algorithm> |
23 | | |
24 | | #include "common/config.h" |
25 | | #include "common/status.h" |
26 | | #include "core/block/column_with_type_and_name.h" |
27 | | #include "core/column/column_nothing.h" |
28 | | #include "exec/common/util.hpp" |
29 | | #include "exec/operator/scan_operator.h" |
30 | | #include "exec/scan/scan_node.h" |
31 | | #include "exprs/vexpr_context.h" |
32 | | #include "exprs/vslot_ref.h" |
33 | | #include "runtime/descriptors.h" |
34 | | #include "runtime/runtime_profile.h" |
35 | | #include "util/concurrency_stats.h" |
36 | | #include "util/defer_op.h" |
37 | | |
38 | | namespace doris { |
39 | | |
40 | | Scanner::Scanner(RuntimeState* state, ScanLocalStateBase* local_state, int64_t limit, |
41 | | RuntimeProfile* profile) |
42 | 19 | : _state(state), |
43 | 19 | _local_state(local_state), |
44 | 19 | _limit(limit), |
45 | 19 | _profile(profile), |
46 | 19 | _output_tuple_desc(_local_state->output_tuple_desc()), |
47 | 19 | _output_row_descriptor(_local_state->_parent->output_row_descriptor()), |
48 | 19 | _has_prepared(false) { |
49 | 19 | _total_rf_num = cast_set<int>(_local_state->_helper.runtime_filter_nums()); |
50 | 19 | DorisMetrics::instance()->scanner_cnt->increment(1); |
51 | 19 | } |
52 | | |
53 | 4 | Status Scanner::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) { |
54 | | // All scanners share a remaining-limit counter so a LIMIT query can |
55 | | // stop once enough rows have been collected across scanners. |
56 | | // Key TopN scans have no ordinary scan LIMIT, so each scanner can |
57 | | // independently produce its full local top-N. |
58 | 4 | _shared_scan_limit = _local_state->shared_scan_limit_ptr(); |
59 | | |
60 | 4 | if (!conjuncts.empty()) { |
61 | 0 | _conjuncts.resize(conjuncts.size()); |
62 | 0 | for (size_t i = 0; i != conjuncts.size(); ++i) { |
63 | 0 | RETURN_IF_ERROR(conjuncts[i]->clone(state, _conjuncts[i])); |
64 | 0 | } |
65 | 0 | } |
66 | | |
67 | 4 | const auto& projections = _local_state->_projections; |
68 | 4 | if (!projections.empty()) { |
69 | 1 | _projections.resize(projections.size()); |
70 | 2 | for (size_t i = 0; i != projections.size(); ++i) { |
71 | 1 | RETURN_IF_ERROR(projections[i]->clone(state, _projections[i])); |
72 | 1 | } |
73 | 1 | } |
74 | | |
75 | 4 | const auto& intermediate_projections = _local_state->_intermediate_projections; |
76 | 4 | if (!intermediate_projections.empty()) { |
77 | 0 | _intermediate_projections.resize(intermediate_projections.size()); |
78 | 0 | for (int i = 0; i < intermediate_projections.size(); i++) { |
79 | 0 | _intermediate_projections[i].resize(intermediate_projections[i].size()); |
80 | 0 | for (int j = 0; j < intermediate_projections[i].size(); j++) { |
81 | 0 | RETURN_IF_ERROR(intermediate_projections[i][j]->clone( |
82 | 0 | state, _intermediate_projections[i][j])); |
83 | 0 | } |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | | // Pure slot-ref projections do not compute new values; they only reorder or forward scan |
88 | | // columns. Record the source column ids once here so the hot projection path can move the |
89 | | // existing ColumnPtr instead of executing VSlotRef and then mutating it, which would clone |
90 | | // the column when the input block still shares the same pointer. |
91 | 4 | if (!_projections.empty() && _intermediate_projections.empty() && _conjuncts.empty() && |
92 | 4 | _limit <= 0 && _shared_scan_limit == nullptr && _output_row_descriptor != nullptr && |
93 | 4 | _output_row_descriptor->num_materialized_slots() == _projections.size()) { |
94 | 0 | std::vector<int> direct_slot_ref_projection_column_ids; |
95 | 0 | direct_slot_ref_projection_column_ids.reserve(_projections.size()); |
96 | 0 | for (const auto& projection : _projections) { |
97 | 0 | auto slot_ref = std::dynamic_pointer_cast<VSlotRef>(projection->root()); |
98 | 0 | if (slot_ref == nullptr) { |
99 | 0 | direct_slot_ref_projection_column_ids.clear(); |
100 | 0 | break; |
101 | 0 | } |
102 | 0 | direct_slot_ref_projection_column_ids.push_back(slot_ref->column_id()); |
103 | 0 | } |
104 | 0 | if (!direct_slot_ref_projection_column_ids.empty()) { |
105 | 0 | _direct_slot_ref_projection_column_ids = |
106 | 0 | std::move(direct_slot_ref_projection_column_ids); |
107 | 0 | } |
108 | 0 | } |
109 | | |
110 | 4 | return Status::OK(); |
111 | 4 | } |
112 | | |
113 | 1 | Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool* eos) { |
114 | 1 | SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().vscanner_get_block); |
115 | 1 | auto& row_descriptor = _local_state->_parent->row_descriptor(); |
116 | 1 | if (_output_row_descriptor) { |
117 | 1 | _origin_block.clear_column_data(row_descriptor.num_materialized_slots()); |
118 | 1 | const auto min_batch_size = std::max(state->batch_size() / 2, 1); |
119 | 1 | const auto block_max_bytes = state->preferred_block_size_bytes(); |
120 | 2 | while (_padding_block.rows() < min_batch_size && _padding_block.bytes() < block_max_bytes && |
121 | 2 | !*eos) { |
122 | 2 | RETURN_IF_ERROR(get_block(state, &_origin_block, eos)); |
123 | 2 | if (*eos) { |
124 | | // For the final block, merge any padding directly and return eos in this call. |
125 | | // The merged tail can be larger than the target batch, but each source block is |
126 | | // already bounded by the lower scanner. |
127 | 1 | RETURN_IF_ERROR(_merge_padding_block()); |
128 | 1 | _origin_block.clear_column_data(row_descriptor.num_materialized_slots()); |
129 | 1 | break; |
130 | 1 | } |
131 | 1 | if (_origin_block.rows() >= min_batch_size) { |
132 | 0 | break; |
133 | 0 | } |
134 | | |
135 | 1 | if (_origin_block.rows() + _padding_block.rows() <= state->batch_size() && |
136 | 1 | _origin_block.bytes() + _padding_block.bytes() <= block_max_bytes) { |
137 | 1 | RETURN_IF_ERROR(_merge_padding_block()); |
138 | 1 | _origin_block.clear_column_data(row_descriptor.num_materialized_slots()); |
139 | 1 | } else { |
140 | 0 | if (_origin_block.rows() < _padding_block.rows()) { |
141 | 0 | _padding_block.swap(_origin_block); |
142 | 0 | } |
143 | 0 | break; |
144 | 0 | } |
145 | 1 | } |
146 | | |
147 | 1 | if (_origin_block.empty() && !_padding_block.empty()) { |
148 | 1 | _padding_block.swap(_origin_block); |
149 | 1 | } |
150 | 1 | return _do_projections(&_origin_block, block); |
151 | 1 | } else { |
152 | 0 | return get_block(state, block, eos); |
153 | 0 | } |
154 | 1 | } |
155 | | |
156 | 3 | Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) { |
157 | | // only empty block should be here |
158 | 3 | DCHECK(block->rows() == 0); |
159 | | |
160 | | // Stop early if other scanners have already collected enough rows |
161 | | // for the SQL LIMIT. Skipped when _shared_scan_limit is null (topn |
162 | | // path or no LIMIT). |
163 | 3 | if (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0) { |
164 | 0 | *eof = true; |
165 | 0 | return Status::OK(); |
166 | 0 | } |
167 | | |
168 | | // scanner running time |
169 | 3 | SCOPED_RAW_TIMER(&_per_scanner_timer); |
170 | 3 | int64_t rows_read_threshold = _num_rows_read + config::doris_scanner_row_num; |
171 | 3 | if (!block->mem_reuse()) { |
172 | 5 | for (auto* const slot_desc : _output_tuple_desc->slots()) { |
173 | 5 | block->insert(ColumnWithTypeAndName(slot_desc->get_empty_mutable_column(), |
174 | 5 | slot_desc->get_data_type_ptr(), |
175 | 5 | slot_desc->col_name())); |
176 | 5 | } |
177 | 3 | } |
178 | | |
179 | 3 | { |
180 | 3 | do { |
181 | | // 1. Get input block from scanner |
182 | 3 | { |
183 | | // get block time |
184 | 3 | SCOPED_TIMER(_local_state->_scan_timer); |
185 | 3 | RETURN_IF_ERROR(_get_block_impl(state, block, eof)); |
186 | 2 | if (*eof) { |
187 | 0 | DCHECK(block->rows() == 0); |
188 | 0 | break; |
189 | 0 | } |
190 | 2 | _num_rows_read += block->rows(); |
191 | 2 | _num_byte_read += block->allocated_bytes(); |
192 | 2 | } |
193 | | |
194 | | // 2. Filter the output block finally. |
195 | 0 | { |
196 | 2 | SCOPED_TIMER(_local_state->_filter_timer); |
197 | 2 | RETURN_IF_ERROR(_filter_output_block(block)); |
198 | 2 | } |
199 | | // record rows return (after filter) for _limit check |
200 | 2 | _num_rows_return += block->rows(); |
201 | | // Publish progress to the shared counter so peer scanners can |
202 | | // observe it. The counter may go negative when several scanners |
203 | | // subtract concurrently; that is harmless because the operator's |
204 | | // reached_limit() makes the final cut. |
205 | 2 | if (_shared_scan_limit && block->rows() > 0) { |
206 | 0 | _shared_scan_limit->fetch_sub(block->rows(), std::memory_order_acq_rel); |
207 | 0 | } |
208 | 2 | } while (!_should_stop && !state->is_cancelled() && block->rows() == 0 && !(*eof) && |
209 | 2 | _num_rows_read < rows_read_threshold); |
210 | 3 | } |
211 | | |
212 | 2 | if (state->is_cancelled()) { |
213 | | // TODO: Should return the specific ErrorStatus instead of just Cancelled. |
214 | 0 | return Status::Cancelled("cancelled"); |
215 | 0 | } |
216 | 2 | *eof = *eof || _should_stop; |
217 | | // set eof to true if per scanner limit is reached |
218 | | // currently for query: ORDER BY key LIMIT n |
219 | 2 | *eof = *eof || (_limit > 0 && _num_rows_return >= _limit); |
220 | 2 | *eof = *eof || (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0); |
221 | | |
222 | 2 | return Status::OK(); |
223 | 2 | } |
224 | | |
225 | 2 | Status Scanner::_filter_output_block(Block* block) { |
226 | 2 | auto old_rows = block->rows(); |
227 | 2 | Status st = VExprContext::filter_block(_conjuncts, block, block->columns()); |
228 | 2 | _counter.num_rows_unselected += old_rows - block->rows(); |
229 | 2 | return st; |
230 | 2 | } |
231 | | |
232 | 1 | Status Scanner::_do_projections(Block* origin_block, Block* output_block) { |
233 | 1 | SCOPED_RAW_TIMER(&_per_scanner_timer); |
234 | 1 | SCOPED_RAW_TIMER(&_projection_timer); |
235 | | |
236 | 1 | const size_t rows = origin_block->rows(); |
237 | 1 | if (rows == 0) { |
238 | 0 | return Status::OK(); |
239 | 0 | } |
240 | 1 | Block input_block = *origin_block; |
241 | | |
242 | 1 | std::vector<int> result_column_ids; |
243 | 1 | for (auto& projections : _intermediate_projections) { |
244 | 0 | result_column_ids.resize(projections.size()); |
245 | 0 | for (int i = 0; i < projections.size(); i++) { |
246 | 0 | RETURN_IF_ERROR(projections[i]->execute(&input_block, &result_column_ids[i])); |
247 | 0 | } |
248 | 0 | input_block.shuffle_columns(result_column_ids); |
249 | 0 | } |
250 | | |
251 | 1 | DCHECK_EQ(rows, input_block.rows()); |
252 | | // Fast path for the common scan shape: PhysicalProject only forwards slot refs and the |
253 | | // output slot type is exactly the same as the scan column type. The exact type check keeps |
254 | | // casts/nullability changes on the generic expression path; only a pure column ownership |
255 | | // transfer is handled here. |
256 | 1 | if (!_direct_slot_ref_projection_column_ids.empty() && |
257 | 1 | _direct_slot_ref_projection_column_ids.size() == _projections.size()) { |
258 | 0 | bool can_use_direct_projection = true; |
259 | 0 | auto direct_projection_columns = |
260 | 0 | VectorizedUtils::create_columns_with_type_and_name(*_output_row_descriptor); |
261 | 0 | if (direct_projection_columns.size() != _direct_slot_ref_projection_column_ids.size()) { |
262 | 0 | return Status::InternalError( |
263 | 0 | "direct slot ref projection size mismatch, output columns {}, projections {}", |
264 | 0 | direct_projection_columns.size(), |
265 | 0 | _direct_slot_ref_projection_column_ids.size()); |
266 | 0 | } |
267 | 0 | for (size_t i = 0; i < _direct_slot_ref_projection_column_ids.size(); ++i) { |
268 | 0 | auto column_id = _direct_slot_ref_projection_column_ids[i]; |
269 | 0 | if (column_id < 0 || column_id >= origin_block->columns()) { |
270 | 0 | return Status::InternalError( |
271 | 0 | "slot ref projection column id {} is out of range, origin block columns {}", |
272 | 0 | column_id, origin_block->columns()); |
273 | 0 | } |
274 | 0 | const auto& src = origin_block->get_by_position(column_id); |
275 | 0 | if (!direct_projection_columns[i].type->equals(*src.type)) { |
276 | 0 | can_use_direct_projection = false; |
277 | 0 | break; |
278 | 0 | } |
279 | 0 | } |
280 | 0 | if (can_use_direct_projection) { |
281 | 0 | return _do_direct_slot_ref_projection(origin_block, output_block, |
282 | 0 | std::move(direct_projection_columns)); |
283 | 0 | } |
284 | 0 | } |
285 | | |
286 | 1 | auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block( |
287 | 1 | output_block, *_output_row_descriptor); |
288 | 1 | auto& mutable_block = scoped_mutable_block.mutable_block(); |
289 | | |
290 | 1 | auto& mutable_columns = mutable_block.mutable_columns(); |
291 | | |
292 | 1 | DCHECK_EQ(mutable_columns.size(), _projections.size()); |
293 | | |
294 | 2 | for (int i = 0; i < mutable_columns.size(); ++i) { |
295 | 1 | ColumnPtr column_ptr; |
296 | 1 | RETURN_IF_ERROR(_projections[i]->execute(&input_block, column_ptr)); |
297 | 1 | column_ptr = column_ptr->convert_to_full_column_if_const(); |
298 | 1 | if (mutable_columns[i]->is_nullable() != column_ptr->is_nullable()) { |
299 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, "Nullable mismatch"); |
300 | 0 | } |
301 | 1 | mutable_columns[i] = IColumn::mutate(std::move(column_ptr)); |
302 | 1 | } |
303 | | |
304 | 1 | scoped_mutable_block.restore(); |
305 | | |
306 | | // origin columns was moved into output_block, so we need to set origin_block to empty columns |
307 | 1 | auto empty_columns = origin_block->clone_empty_columns(); |
308 | 1 | origin_block->set_columns(std::move(empty_columns)); |
309 | 1 | DCHECK_EQ(output_block->rows(), rows); |
310 | | |
311 | 1 | return Status::OK(); |
312 | 1 | } |
313 | | |
314 | | Status Scanner::_do_direct_slot_ref_projection(Block* origin_block, Block* output_block, |
315 | 0 | ColumnsWithTypeAndName&& output_columns) { |
316 | 0 | const size_t rows = origin_block->rows(); |
317 | |
|
318 | 0 | for (size_t i = 0; i < _direct_slot_ref_projection_column_ids.size(); ++i) { |
319 | 0 | auto column_id = _direct_slot_ref_projection_column_ids[i]; |
320 | 0 | if (column_id < 0 || column_id >= origin_block->columns()) { |
321 | 0 | return Status::InternalError( |
322 | 0 | "slot ref projection column id {} is out of range, origin block columns {}", |
323 | 0 | column_id, origin_block->columns()); |
324 | 0 | } |
325 | | |
326 | 0 | auto column = |
327 | 0 | origin_block->get_by_position(column_id).column->convert_to_full_column_if_const(); |
328 | 0 | if (column->size() != rows) { |
329 | 0 | return Status::InternalError( |
330 | 0 | "direct slot ref projection result column size {} not equal input rows {}", |
331 | 0 | column->size(), rows); |
332 | 0 | } |
333 | 0 | output_columns[i].column = std::move(column); |
334 | 0 | } |
335 | | |
336 | 0 | Block projected_block(std::move(output_columns)); |
337 | 0 | output_block->swap(projected_block); |
338 | | |
339 | | // The output block now owns the scan columns. Replace the origin block with empty columns so |
340 | | // later block reuse/destruction does not keep an extra shared reference that would make later |
341 | | // mutable paths clone the forwarded columns. |
342 | 0 | auto empty_columns = origin_block->clone_empty_columns(); |
343 | 0 | origin_block->set_columns(std::move(empty_columns)); |
344 | 0 | DCHECK_EQ(output_block->rows(), rows); |
345 | |
|
346 | 0 | return Status::OK(); |
347 | 0 | } |
348 | | |
349 | 2 | Status Scanner::try_append_late_arrival_runtime_filter() { |
350 | 2 | if (_applied_rf_num == _total_rf_num) { |
351 | 1 | return Status::OK(); |
352 | 1 | } |
353 | 2 | DCHECK(_applied_rf_num < _total_rf_num); |
354 | 1 | int arrived_rf_num = 0; |
355 | 1 | RETURN_IF_ERROR(_local_state->update_late_arrival_runtime_filter(_state, arrived_rf_num)); |
356 | | |
357 | 1 | if (arrived_rf_num == _applied_rf_num) { |
358 | | // No newly arrived runtime filters, just return; |
359 | 0 | return Status::OK(); |
360 | 0 | } |
361 | | |
362 | | // avoid conjunct destroy in used by storage layer |
363 | 1 | _conjuncts.clear(); |
364 | 1 | RETURN_IF_ERROR(_local_state->clone_conjunct_ctxs(_conjuncts)); |
365 | 1 | _applied_rf_num = arrived_rf_num; |
366 | 1 | return Status::OK(); |
367 | 1 | } |
368 | | |
369 | 17 | Status Scanner::close(RuntimeState* state) { |
370 | | #ifndef BE_TEST |
371 | | COUNTER_UPDATE(_local_state->_scanner_wait_worker_timer, _scanner_wait_worker_timer); |
372 | | #endif |
373 | 17 | return Status::OK(); |
374 | 17 | } |
375 | | |
376 | 167 | bool Scanner::_try_close() { |
377 | 167 | bool expected = false; |
378 | 167 | return _is_closed.compare_exchange_strong(expected, true); |
379 | 167 | } |
380 | | |
381 | 0 | void Scanner::_collect_profile_before_close() { |
382 | 0 | COUNTER_UPDATE(_local_state->_scan_cpu_timer, _scan_cpu_timer); |
383 | 0 | COUNTER_UPDATE(_local_state->_rows_read_counter, _num_rows_read); |
384 | | |
385 | | // Update stats for load. See _should_update_load_counters() for why this is gated. |
386 | 0 | if (_should_update_load_counters()) { |
387 | 0 | _state->update_num_rows_load_filtered(_counter.num_rows_filtered); |
388 | 0 | _state->update_num_rows_load_unselected(_counter.num_rows_unselected); |
389 | 0 | } |
390 | 0 | } |
391 | | |
392 | 0 | void Scanner::_update_scan_cpu_timer() { |
393 | 0 | int64_t cpu_time = _cpu_watch.elapsed_time(); |
394 | 0 | _scan_cpu_timer += cpu_time; |
395 | 0 | if (_state && _state->get_query_ctx()) { |
396 | 0 | _state->get_query_ctx()->resource_ctx()->cpu_context()->update_cpu_cost_ms(cpu_time); |
397 | 0 | } |
398 | 0 | } |
399 | | |
400 | | } // namespace doris |