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.26M | : _state(state), |
39 | 1.26M | _local_state(local_state), |
40 | 1.26M | _limit(limit), |
41 | 1.26M | _profile(profile), |
42 | 1.26M | _output_tuple_desc(_local_state->output_tuple_desc()), |
43 | 1.26M | _output_row_descriptor(_local_state->_parent->output_row_descriptor()), |
44 | 1.26M | _has_prepared(false) { |
45 | 1.26M | _total_rf_num = cast_set<int>(_local_state->_helper.runtime_filter_nums()); |
46 | 1.26M | DorisMetrics::instance()->scanner_cnt->increment(1); |
47 | 1.26M | } |
48 | | |
49 | 1.26M | Status Scanner::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) { |
50 | | // All scanners share a remaining-limit counter so a LIMIT query can |
51 | | // stop once enough rows have been collected across scanners. |
52 | | // Key TopN scans have no ordinary scan LIMIT, so each scanner can |
53 | | // independently produce its full local top-N. |
54 | 1.26M | _shared_scan_limit = _local_state->shared_scan_limit_ptr(); |
55 | | |
56 | 1.26M | if (!conjuncts.empty()) { |
57 | 42.2k | _conjuncts.resize(conjuncts.size()); |
58 | 99.3k | for (size_t i = 0; i != conjuncts.size(); ++i) { |
59 | 57.0k | RETURN_IF_ERROR(conjuncts[i]->clone(state, _conjuncts[i])); |
60 | 57.0k | } |
61 | 42.2k | } |
62 | | |
63 | 1.26M | const auto& projections = _local_state->_projections; |
64 | 1.26M | if (!projections.empty()) { |
65 | 1.16M | _projections.resize(projections.size()); |
66 | 12.7M | for (size_t i = 0; i != projections.size(); ++i) { |
67 | 11.6M | RETURN_IF_ERROR(projections[i]->clone(state, _projections[i])); |
68 | 11.6M | } |
69 | 1.16M | } |
70 | | |
71 | 1.26M | const auto& intermediate_projections = _local_state->_intermediate_projections; |
72 | 1.26M | if (!intermediate_projections.empty()) { |
73 | 12.2k | _intermediate_projections.resize(intermediate_projections.size()); |
74 | 33.0k | for (int i = 0; i < intermediate_projections.size(); i++) { |
75 | 20.8k | _intermediate_projections[i].resize(intermediate_projections[i].size()); |
76 | 125k | for (int j = 0; j < intermediate_projections[i].size(); j++) { |
77 | 104k | RETURN_IF_ERROR(intermediate_projections[i][j]->clone( |
78 | 104k | state, _intermediate_projections[i][j])); |
79 | 104k | } |
80 | 20.8k | } |
81 | 12.2k | } |
82 | | |
83 | 1.26M | return Status::OK(); |
84 | 1.26M | } |
85 | | |
86 | 1.36M | Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool* eos) { |
87 | 1.36M | SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().vscanner_get_block); |
88 | 1.36M | auto& row_descriptor = _local_state->_parent->row_descriptor(); |
89 | 1.36M | if (_output_row_descriptor) { |
90 | 1.19M | if (_alreay_eos) { |
91 | 0 | *eos = true; |
92 | 0 | _padding_block.swap(_origin_block); |
93 | 1.19M | } else { |
94 | 1.19M | _origin_block.clear_column_data(row_descriptor.num_materialized_slots()); |
95 | 1.19M | const auto min_batch_size = std::max(state->batch_size() / 2, 1); |
96 | 1.19M | const auto block_max_bytes = state->preferred_block_size_bytes(); |
97 | 2.62M | while (_padding_block.rows() < min_batch_size && |
98 | 2.62M | _padding_block.bytes() < block_max_bytes && !*eos) { |
99 | 1.46M | RETURN_IF_ERROR(get_block(state, &_origin_block, eos)); |
100 | 1.46M | if (_origin_block.rows() >= min_batch_size) { |
101 | 28.9k | break; |
102 | 28.9k | } |
103 | | |
104 | 1.43M | if (_origin_block.rows() + _padding_block.rows() <= state->batch_size() && |
105 | 1.43M | _origin_block.bytes() + _padding_block.bytes() <= block_max_bytes) { |
106 | 1.43M | RETURN_IF_ERROR(_merge_padding_block()); |
107 | 1.43M | _origin_block.clear_column_data(row_descriptor.num_materialized_slots()); |
108 | 1.43M | } else { |
109 | 535 | if (_origin_block.rows() < _padding_block.rows()) { |
110 | 0 | _padding_block.swap(_origin_block); |
111 | 0 | } |
112 | 535 | break; |
113 | 535 | } |
114 | 1.43M | } |
115 | 1.19M | } |
116 | | |
117 | | // first output the origin block change eos = false, next time output padding block |
118 | | // set the eos to true |
119 | 1.19M | if (*eos && !_padding_block.empty() && !_origin_block.empty()) { |
120 | 0 | _alreay_eos = true; |
121 | 0 | *eos = false; |
122 | 0 | } |
123 | 1.19M | if (_origin_block.empty() && !_padding_block.empty()) { |
124 | 212k | _padding_block.swap(_origin_block); |
125 | 212k | } |
126 | 1.19M | return _do_projections(&_origin_block, block); |
127 | 1.19M | } else { |
128 | 169k | return get_block(state, block, eos); |
129 | 169k | } |
130 | 1.36M | } |
131 | | |
132 | 1.63M | Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) { |
133 | | // only empty block should be here |
134 | 1.63M | DCHECK(block->rows() == 0); |
135 | | |
136 | | // Stop early if other scanners have already collected enough rows |
137 | | // for the SQL LIMIT. Skipped when _shared_scan_limit is null (topn |
138 | | // path or no LIMIT). |
139 | 1.63M | if (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0) { |
140 | 16 | *eof = true; |
141 | 16 | return Status::OK(); |
142 | 16 | } |
143 | | |
144 | | // scanner running time |
145 | 1.63M | SCOPED_RAW_TIMER(&_per_scanner_timer); |
146 | 1.63M | int64_t rows_read_threshold = _num_rows_read + config::doris_scanner_row_num; |
147 | 1.63M | if (!block->mem_reuse()) { |
148 | 12.9M | for (auto* const slot_desc : _output_tuple_desc->slots()) { |
149 | 12.9M | block->insert(ColumnWithTypeAndName(slot_desc->get_empty_mutable_column(), |
150 | 12.9M | slot_desc->get_data_type_ptr(), |
151 | 12.9M | slot_desc->col_name())); |
152 | 12.9M | } |
153 | 1.37M | } |
154 | | |
155 | 1.63M | { |
156 | 1.74M | do { |
157 | | // 1. Get input block from scanner |
158 | 1.74M | { |
159 | | // get block time |
160 | 1.74M | SCOPED_TIMER(_local_state->_scan_timer); |
161 | 1.74M | RETURN_IF_ERROR(_get_block_impl(state, block, eof)); |
162 | 1.74M | if (*eof) { |
163 | 1.26M | DCHECK(block->rows() == 0); |
164 | 1.26M | break; |
165 | 1.26M | } |
166 | 484k | _num_rows_read += block->rows(); |
167 | 484k | _num_byte_read += block->allocated_bytes(); |
168 | 484k | } |
169 | | |
170 | | // 2. Filter the output block finally. |
171 | 0 | { |
172 | 484k | SCOPED_TIMER(_local_state->_filter_timer); |
173 | 484k | RETURN_IF_ERROR(_filter_output_block(block)); |
174 | 484k | } |
175 | | // record rows return (after filter) for _limit check |
176 | 484k | _num_rows_return += block->rows(); |
177 | | // Publish progress to the shared counter so peer scanners can |
178 | | // observe it. The counter may go negative when several scanners |
179 | | // subtract concurrently; that is harmless because the operator's |
180 | | // reached_limit() makes the final cut. |
181 | 484k | if (_shared_scan_limit && block->rows() > 0) { |
182 | 2.77k | _shared_scan_limit->fetch_sub(block->rows(), std::memory_order_acq_rel); |
183 | 2.77k | } |
184 | 484k | } while (!_should_stop && !state->is_cancelled() && block->rows() == 0 && !(*eof) && |
185 | 484k | _num_rows_read < rows_read_threshold); |
186 | 1.63M | } |
187 | | |
188 | 1.63M | if (state->is_cancelled()) { |
189 | | // TODO: Should return the specific ErrorStatus instead of just Cancelled. |
190 | 15 | return Status::Cancelled("cancelled"); |
191 | 15 | } |
192 | 1.63M | *eof = *eof || _should_stop; |
193 | | // set eof to true if per scanner limit is reached |
194 | | // currently for query: ORDER BY key LIMIT n |
195 | 1.63M | *eof = *eof || (_limit > 0 && _num_rows_return >= _limit); |
196 | 1.63M | *eof = *eof || (_shared_scan_limit && _shared_scan_limit->load(std::memory_order_acquire) <= 0); |
197 | | |
198 | 1.63M | return Status::OK(); |
199 | 1.63M | } |
200 | | |
201 | 483k | Status Scanner::_filter_output_block(Block* block) { |
202 | 483k | auto old_rows = block->rows(); |
203 | 483k | Status st = VExprContext::filter_block(_conjuncts, block, block->columns()); |
204 | 483k | _counter.num_rows_unselected += old_rows - block->rows(); |
205 | 483k | return st; |
206 | 483k | } |
207 | | |
208 | 1.19M | Status Scanner::_do_projections(Block* origin_block, Block* output_block) { |
209 | 1.19M | SCOPED_RAW_TIMER(&_per_scanner_timer); |
210 | 1.19M | SCOPED_RAW_TIMER(&_projection_timer); |
211 | | |
212 | 1.19M | const size_t rows = origin_block->rows(); |
213 | 1.19M | if (rows == 0) { |
214 | 951k | return Status::OK(); |
215 | 951k | } |
216 | 241k | Block input_block = *origin_block; |
217 | | |
218 | 241k | std::vector<int> result_column_ids; |
219 | 241k | for (auto& projections : _intermediate_projections) { |
220 | 30.9k | result_column_ids.resize(projections.size()); |
221 | 203k | for (int i = 0; i < projections.size(); i++) { |
222 | 172k | RETURN_IF_ERROR(projections[i]->execute(&input_block, &result_column_ids[i])); |
223 | 172k | } |
224 | 30.9k | input_block.shuffle_columns(result_column_ids); |
225 | 30.9k | } |
226 | | |
227 | 241k | DCHECK_EQ(rows, input_block.rows()); |
228 | 241k | MutableBlock mutable_block = |
229 | 241k | VectorizedUtils::build_mutable_mem_reuse_block(output_block, *_output_row_descriptor); |
230 | | |
231 | 241k | auto& mutable_columns = mutable_block.mutable_columns(); |
232 | | |
233 | 241k | DCHECK_EQ(mutable_columns.size(), _projections.size()); |
234 | | |
235 | 883k | for (int i = 0; i < mutable_columns.size(); ++i) { |
236 | 643k | ColumnPtr column_ptr; |
237 | 643k | RETURN_IF_ERROR(_projections[i]->execute(&input_block, column_ptr)); |
238 | 642k | column_ptr = column_ptr->convert_to_full_column_if_const(); |
239 | 642k | if (mutable_columns[i]->is_nullable() != column_ptr->is_nullable()) { |
240 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, "Nullable mismatch"); |
241 | 0 | } |
242 | 642k | mutable_columns[i] = column_ptr->assume_mutable(); |
243 | 642k | } |
244 | | |
245 | 240k | output_block->set_columns(std::move(mutable_columns)); |
246 | | |
247 | | // origin columns was moved into output_block, so we need to set origin_block to empty columns |
248 | 240k | auto empty_columns = origin_block->clone_empty_columns(); |
249 | 240k | origin_block->set_columns(std::move(empty_columns)); |
250 | 240k | DCHECK_EQ(output_block->rows(), rows); |
251 | | |
252 | 240k | return Status::OK(); |
253 | 241k | } |
254 | | |
255 | 1.36M | Status Scanner::try_append_late_arrival_runtime_filter() { |
256 | 1.36M | if (_applied_rf_num == _total_rf_num) { |
257 | 1.32M | return Status::OK(); |
258 | 1.32M | } |
259 | 1.36M | DCHECK(_applied_rf_num < _total_rf_num); |
260 | 38.9k | int arrived_rf_num = 0; |
261 | 38.9k | RETURN_IF_ERROR(_local_state->update_late_arrival_runtime_filter(_state, arrived_rf_num)); |
262 | | |
263 | 38.9k | if (arrived_rf_num == _applied_rf_num) { |
264 | | // No newly arrived runtime filters, just return; |
265 | 5.20k | return Status::OK(); |
266 | 5.20k | } |
267 | | |
268 | | // avoid conjunct destroy in used by storage layer |
269 | 33.7k | _conjuncts.clear(); |
270 | 33.7k | RETURN_IF_ERROR(_local_state->clone_conjunct_ctxs(_conjuncts)); |
271 | 33.7k | _applied_rf_num = arrived_rf_num; |
272 | 33.7k | return Status::OK(); |
273 | 33.7k | } |
274 | | |
275 | 1.27M | Status Scanner::close(RuntimeState* state) { |
276 | 1.27M | #ifndef BE_TEST |
277 | 1.27M | COUNTER_UPDATE(_local_state->_scanner_wait_worker_timer, _scanner_wait_worker_timer); |
278 | 1.27M | #endif |
279 | 1.27M | return Status::OK(); |
280 | 1.27M | } |
281 | | |
282 | 1.27M | bool Scanner::_try_close() { |
283 | 1.27M | bool expected = false; |
284 | 1.27M | return _is_closed.compare_exchange_strong(expected, true); |
285 | 1.27M | } |
286 | | |
287 | 1.26M | void Scanner::_collect_profile_before_close() { |
288 | 1.26M | COUNTER_UPDATE(_local_state->_scan_cpu_timer, _scan_cpu_timer); |
289 | 1.26M | COUNTER_UPDATE(_local_state->_rows_read_counter, _num_rows_read); |
290 | | |
291 | | // Update stats for load |
292 | 1.26M | _state->update_num_rows_load_filtered(_counter.num_rows_filtered); |
293 | 1.26M | _state->update_num_rows_load_unselected(_counter.num_rows_unselected); |
294 | 1.26M | } |
295 | | |
296 | 1.36M | void Scanner::_update_scan_cpu_timer() { |
297 | 1.36M | int64_t cpu_time = _cpu_watch.elapsed_time(); |
298 | 1.36M | _scan_cpu_timer += cpu_time; |
299 | 1.36M | if (_state && _state->get_query_ctx()) { |
300 | 1.36M | _state->get_query_ctx()->resource_ctx()->cpu_context()->update_cpu_cost_ms(cpu_time); |
301 | 1.36M | } |
302 | 1.36M | } |
303 | | |
304 | | } // namespace doris |