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 <iterator> |
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/operator/scan_operator.h" |
29 | | #include "exec/scan/scan_node.h" |
30 | | #include "exprs/vexpr_context.h" |
31 | | #include "runtime/descriptors.h" |
32 | | #include "runtime/runtime_profile.h" |
33 | | #include "util/concurrency_stats.h" |
34 | | #include "util/defer_op.h" |
35 | | |
36 | | namespace doris { |
37 | | |
38 | | Scanner::Scanner(RuntimeState* state, ScanLocalStateBase* local_state, int64_t limit, |
39 | | RuntimeProfile* profile) |
40 | 20 | : _state(state), |
41 | 20 | _local_state(local_state), |
42 | 20 | _limit(limit), |
43 | 20 | _profile(profile), |
44 | 20 | _output_tuple_desc(_local_state->output_tuple_desc()), |
45 | 20 | _output_row_descriptor(_local_state->_parent->output_row_descriptor()), |
46 | 20 | _has_prepared(false) { |
47 | 20 | _total_rf_num = cast_set<int>(_local_state->_helper.runtime_filter_nums()); |
48 | 20 | DorisMetrics::instance()->scanner_cnt->increment(1); |
49 | 20 | } |
50 | | |
51 | 5 | Status Scanner::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) { |
52 | | // All scanners share a remaining-limit counter so a LIMIT query can |
53 | | // stop once enough rows have been collected across scanners. |
54 | | // Key TopN scans have no ordinary scan LIMIT, so each scanner can |
55 | | // independently produce its full local top-N. |
56 | 5 | _shared_scan_limit = _local_state->shared_scan_limit_ptr(); |
57 | | |
58 | 5 | if (!conjuncts.empty()) { |
59 | 2 | _conjuncts.resize(conjuncts.size()); |
60 | 6 | for (size_t i = 0; i != conjuncts.size(); ++i) { |
61 | 4 | RETURN_IF_ERROR(conjuncts[i]->clone(state, _conjuncts[i])); |
62 | 4 | } |
63 | 2 | } |
64 | | |
65 | 5 | const auto& projections = _local_state->_projections; |
66 | 5 | if (!projections.empty()) { |
67 | 1 | _projections.resize(projections.size()); |
68 | 2 | for (size_t i = 0; i != projections.size(); ++i) { |
69 | 1 | RETURN_IF_ERROR(projections[i]->clone(state, _projections[i])); |
70 | 1 | } |
71 | 1 | } |
72 | | |
73 | 5 | const auto& intermediate_projections = _local_state->_intermediate_projections; |
74 | 5 | if (!intermediate_projections.empty()) { |
75 | 0 | _intermediate_projections.resize(intermediate_projections.size()); |
76 | 0 | for (int i = 0; i < intermediate_projections.size(); i++) { |
77 | 0 | _intermediate_projections[i].resize(intermediate_projections[i].size()); |
78 | 0 | for (int j = 0; j < intermediate_projections[i].size(); j++) { |
79 | 0 | RETURN_IF_ERROR(intermediate_projections[i][j]->clone( |
80 | 0 | state, _intermediate_projections[i][j])); |
81 | 0 | } |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | 5 | return Status::OK(); |
86 | 5 | } |
87 | | |
88 | 1 | Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool* eos) { |
89 | 1 | SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().vscanner_get_block); |
90 | 1 | auto& row_descriptor = _local_state->_parent->row_descriptor(); |
91 | 1 | if (_output_row_descriptor) { |
92 | 1 | _origin_block.clear_column_data(row_descriptor.num_materialized_slots()); |
93 | 1 | const auto min_batch_size = std::max(state->batch_size() / 2, 1); |
94 | 1 | const auto block_max_bytes = state->preferred_block_size_bytes(); |
95 | 2 | while (_padding_block.rows() < min_batch_size && _padding_block.bytes() < block_max_bytes && |
96 | 2 | !*eos) { |
97 | 2 | RETURN_IF_ERROR(get_block(state, &_origin_block, eos)); |
98 | 2 | if (*eos) { |
99 | | // For the final block, merge any padding directly and return eos in this call. |
100 | | // The merged tail can be larger than the target batch, but each source block is |
101 | | // already bounded by the lower scanner. |
102 | 1 | RETURN_IF_ERROR(_merge_padding_block()); |
103 | 1 | _origin_block.clear_column_data(row_descriptor.num_materialized_slots()); |
104 | 1 | break; |
105 | 1 | } |
106 | 1 | if (_origin_block.rows() >= min_batch_size) { |
107 | 0 | break; |
108 | 0 | } |
109 | | |
110 | 1 | if (_origin_block.rows() + _padding_block.rows() <= state->batch_size() && |
111 | 1 | _origin_block.bytes() + _padding_block.bytes() <= block_max_bytes) { |
112 | 1 | RETURN_IF_ERROR(_merge_padding_block()); |
113 | 1 | _origin_block.clear_column_data(row_descriptor.num_materialized_slots()); |
114 | 1 | } else { |
115 | 0 | if (_origin_block.rows() < _padding_block.rows()) { |
116 | 0 | _padding_block.swap(_origin_block); |
117 | 0 | } |
118 | 0 | break; |
119 | 0 | } |
120 | 1 | } |
121 | | |
122 | 1 | if (_origin_block.empty() && !_padding_block.empty()) { |
123 | 1 | _padding_block.swap(_origin_block); |
124 | 1 | } |
125 | 1 | return _do_projections(&_origin_block, block); |
126 | 1 | } else { |
127 | 0 | return get_block(state, block, eos); |
128 | 0 | } |
129 | 1 | } |
130 | | |
131 | 3 | Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) { |
132 | | // only empty block should be here |
133 | 3 | DCHECK(block->rows() == 0); |
134 | | |
135 | | // Stop early if other scanners have already collected enough rows |
136 | | // for the SQL LIMIT. Skipped when _shared_scan_limit is null (topn |
137 | | // path or no LIMIT). |
138 | 3 | if (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0) { |
139 | 0 | *eof = true; |
140 | 0 | return Status::OK(); |
141 | 0 | } |
142 | | |
143 | | // scanner running time |
144 | 3 | SCOPED_RAW_TIMER(&_per_scanner_timer); |
145 | 3 | int64_t rows_read_threshold = _num_rows_read + config::doris_scanner_row_num; |
146 | 3 | if (!block->mem_reuse()) { |
147 | 5 | for (auto* const slot_desc : _output_tuple_desc->slots()) { |
148 | 5 | block->insert(ColumnWithTypeAndName(slot_desc->get_empty_mutable_column(), |
149 | 5 | slot_desc->get_data_type_ptr(), |
150 | 5 | slot_desc->col_name())); |
151 | 5 | } |
152 | 3 | } |
153 | | |
154 | 3 | { |
155 | 3 | do { |
156 | | // 1. Get input block from scanner |
157 | 3 | { |
158 | | // get block time |
159 | 3 | SCOPED_TIMER(_local_state->_scan_timer); |
160 | 3 | RETURN_IF_ERROR(_get_block_impl(state, block, eof)); |
161 | 2 | if (*eof) { |
162 | 0 | DCHECK(block->rows() == 0); |
163 | 0 | break; |
164 | 0 | } |
165 | | // Some scanners apply owned predicates before returning the block. Account the |
166 | | // materialized input, not only survivors, so the per-turn progress bound remains |
167 | | // effective for highly selective predicates. |
168 | 2 | _num_rows_read += _last_block_rows_read(*block); |
169 | 2 | _num_byte_read += _last_block_bytes_read(*block); |
170 | 2 | } |
171 | | |
172 | | // 2. Filter the output block finally. |
173 | 0 | { |
174 | 2 | SCOPED_TIMER(_local_state->_filter_timer); |
175 | 2 | RETURN_IF_ERROR(_filter_output_block(block)); |
176 | 2 | } |
177 | | // record rows return (after filter) for _limit check |
178 | 2 | _num_rows_return += block->rows(); |
179 | | // Publish progress to the shared counter so peer scanners can |
180 | | // observe it. The counter may go negative when several scanners |
181 | | // subtract concurrently; that is harmless because the operator's |
182 | | // reached_limit() makes the final cut. |
183 | 2 | if (_shared_scan_limit && block->rows() > 0) { |
184 | 0 | _shared_scan_limit->fetch_sub(block->rows(), std::memory_order_acq_rel); |
185 | 0 | } |
186 | 2 | } while (!_should_stop && !state->is_cancelled() && block->rows() == 0 && !(*eof) && |
187 | 2 | _num_rows_read < rows_read_threshold); |
188 | 3 | } |
189 | | |
190 | 2 | if (state->is_cancelled()) { |
191 | | // TODO: Should return the specific ErrorStatus instead of just Cancelled. |
192 | 0 | return Status::Cancelled("cancelled"); |
193 | 0 | } |
194 | 2 | *eof = *eof || _should_stop; |
195 | | // set eof to true if per scanner limit is reached |
196 | | // currently for query: ORDER BY key LIMIT n |
197 | 2 | *eof = *eof || (_limit > 0 && _num_rows_return >= _limit); |
198 | 2 | *eof = *eof || (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0); |
199 | | |
200 | 2 | return Status::OK(); |
201 | 2 | } |
202 | | |
203 | 2 | Status Scanner::_filter_output_block(Block* block) { |
204 | 2 | auto old_rows = block->rows(); |
205 | 2 | Status st = VExprContext::filter_block(_conjuncts, block, block->columns()); |
206 | 2 | _counter.num_rows_unselected += old_rows - block->rows(); |
207 | 2 | return st; |
208 | 2 | } |
209 | | |
210 | 1 | Status Scanner::_do_projections(Block* origin_block, Block* output_block) { |
211 | 1 | SCOPED_RAW_TIMER(&_per_scanner_timer); |
212 | 1 | SCOPED_RAW_TIMER(&_projection_timer); |
213 | | |
214 | 1 | const size_t rows = origin_block->rows(); |
215 | 1 | if (rows == 0) { |
216 | 0 | return Status::OK(); |
217 | 0 | } |
218 | 1 | Block input_block = *origin_block; |
219 | | |
220 | 1 | std::vector<int> result_column_ids; |
221 | 1 | for (auto& projections : _intermediate_projections) { |
222 | 0 | result_column_ids.resize(projections.size()); |
223 | 0 | for (int i = 0; i < projections.size(); i++) { |
224 | 0 | RETURN_IF_ERROR(projections[i]->execute(&input_block, &result_column_ids[i])); |
225 | 0 | } |
226 | 0 | input_block.shuffle_columns(result_column_ids); |
227 | 0 | } |
228 | | |
229 | 1 | DCHECK_EQ(rows, input_block.rows()); |
230 | 1 | auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block( |
231 | 1 | output_block, *_output_row_descriptor); |
232 | 1 | auto& mutable_block = scoped_mutable_block.mutable_block(); |
233 | | |
234 | 1 | auto& mutable_columns = mutable_block.mutable_columns(); |
235 | | |
236 | 1 | DCHECK_EQ(mutable_columns.size(), _projections.size()); |
237 | | |
238 | 2 | for (int i = 0; i < mutable_columns.size(); ++i) { |
239 | 1 | ColumnPtr column_ptr; |
240 | 1 | RETURN_IF_ERROR(_projections[i]->execute(&input_block, column_ptr)); |
241 | 1 | column_ptr = column_ptr->convert_to_full_column_if_const(); |
242 | 1 | if (mutable_columns[i]->is_nullable() != column_ptr->is_nullable()) { |
243 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, "Nullable mismatch"); |
244 | 0 | } |
245 | 1 | mutable_columns[i] = IColumn::mutate(std::move(column_ptr)); |
246 | 1 | } |
247 | | |
248 | 1 | scoped_mutable_block.restore(); |
249 | | |
250 | | // origin columns was moved into output_block, so we need to set origin_block to empty columns |
251 | 1 | auto empty_columns = origin_block->clone_empty_columns(); |
252 | 1 | origin_block->set_columns(std::move(empty_columns)); |
253 | 1 | DCHECK_EQ(output_block->rows(), rows); |
254 | | |
255 | 1 | return Status::OK(); |
256 | 1 | } |
257 | | |
258 | 3 | Status Scanner::try_append_late_arrival_runtime_filter() { |
259 | 3 | if (_applied_rf_num == _total_rf_num) { |
260 | 1 | return Status::OK(); |
261 | 1 | } |
262 | 3 | DCHECK(_applied_rf_num < _total_rf_num); |
263 | 2 | int arrived_rf_num = 0; |
264 | 2 | VExprContextSPtrs arrived_conjuncts; |
265 | 2 | RETURN_IF_ERROR(_local_state->update_late_arrival_runtime_filter( |
266 | 2 | _state, _applied_rf_num, arrived_rf_num, arrived_conjuncts)); |
267 | | |
268 | 2 | if (arrived_rf_num == _applied_rf_num) { |
269 | | // No newly arrived runtime filters, just return; |
270 | 0 | return Status::OK(); |
271 | 0 | } |
272 | | |
273 | | // avoid conjunct destroy in used by storage layer |
274 | 2 | _conjuncts.clear(); |
275 | 2 | RETURN_IF_ERROR(_local_state->clone_conjunct_ctxs(_conjuncts)); |
276 | 2 | _late_arrival_rf_conjuncts.insert(_late_arrival_rf_conjuncts.end(), |
277 | 2 | std::make_move_iterator(arrived_conjuncts.begin()), |
278 | 2 | std::make_move_iterator(arrived_conjuncts.end())); |
279 | 2 | _applied_rf_num = arrived_rf_num; |
280 | 2 | return Status::OK(); |
281 | 2 | } |
282 | | |
283 | 0 | uint64_t Scanner::_current_condition_cache_digest() const { |
284 | 0 | DORIS_CHECK(_state != nullptr); |
285 | 0 | DORIS_CHECK(_local_state != nullptr); |
286 | 0 | if (_local_state->get_condition_cache_digest() == 0) { |
287 | 0 | return 0; |
288 | 0 | } |
289 | | |
290 | | // ScanLocalState computed its digest after collecting the RFs that were ready during open(). A |
291 | | // scanner may later clone more RF conjuncts between file splits, so rebuild from its current |
292 | | // snapshot instead of reusing that stale value. For example, split 0 may use P, while split 1 |
293 | | // starts after an IN RF with payload {7, 9} arrives and must use digest(P AND RF{7, 9}). A |
294 | | // different payload {8, 10} consequently receives a different key. get_digest() returning zero |
295 | | // is the correctness fallback for an RF whose complete semantics cannot be represented. |
296 | 0 | return _build_condition_cache_digest(_state->query_options().condition_cache_digest, |
297 | 0 | _conjuncts); |
298 | 0 | } |
299 | | |
300 | 13 | uint64_t Scanner::_build_condition_cache_digest(uint64_t seed, const VExprContextSPtrs& conjuncts) { |
301 | 13 | for (const auto& conjunct : conjuncts) { |
302 | 13 | seed = conjunct->get_digest(seed); |
303 | 13 | if (seed == 0) { |
304 | 1 | return 0; |
305 | 1 | } |
306 | 13 | } |
307 | 12 | return seed; |
308 | 13 | } |
309 | | |
310 | | #ifdef BE_TEST |
311 | | uint64_t Scanner::TEST_build_condition_cache_digest(uint64_t seed, |
312 | 13 | const VExprContextSPtrs& conjuncts) { |
313 | 13 | return _build_condition_cache_digest(seed, conjuncts); |
314 | 13 | } |
315 | | #endif |
316 | | |
317 | 18 | Status Scanner::close(RuntimeState* state) { |
318 | | #ifndef BE_TEST |
319 | | COUNTER_UPDATE(_local_state->_scanner_wait_worker_timer, _scanner_wait_worker_timer); |
320 | | #endif |
321 | 18 | return Status::OK(); |
322 | 18 | } |
323 | | |
324 | 170 | bool Scanner::_try_close() { |
325 | 170 | bool expected = false; |
326 | 170 | return _is_closed.compare_exchange_strong(expected, true); |
327 | 170 | } |
328 | | |
329 | 0 | void Scanner::_collect_profile_before_close() { |
330 | 0 | COUNTER_UPDATE(_local_state->_scan_cpu_timer, _scan_cpu_timer); |
331 | 0 | COUNTER_UPDATE(_local_state->_rows_read_counter, _num_rows_read); |
332 | | |
333 | | // Update stats for load. See _should_update_load_counters() for why this is gated. |
334 | 0 | if (_should_update_load_counters()) { |
335 | 0 | _state->update_num_rows_load_filtered(_counter.num_rows_filtered); |
336 | 0 | _state->update_num_rows_load_unselected(_counter.num_rows_unselected); |
337 | 0 | } |
338 | 0 | } |
339 | | |
340 | 0 | void Scanner::_update_scan_cpu_timer() { |
341 | 0 | int64_t cpu_time = _cpu_watch.elapsed_time(); |
342 | 0 | _scan_cpu_timer += cpu_time; |
343 | 0 | if (_state && _state->get_query_ctx()) { |
344 | 0 | _state->get_query_ctx()->resource_ctx()->cpu_context()->update_cpu_cost_ms(cpu_time); |
345 | 0 | } |
346 | 0 | } |
347 | | |
348 | | } // namespace doris |