/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 | 19 | : _state(state), |
39 | 19 | _local_state(local_state), |
40 | 19 | _limit(limit), |
41 | 19 | _profile(profile), |
42 | 19 | _output_tuple_desc(_local_state->output_tuple_desc()), |
43 | 19 | _output_row_descriptor(_local_state->_parent->output_row_descriptor()), |
44 | 19 | _has_prepared(false) { |
45 | 19 | _total_rf_num = cast_set<int>(_local_state->_helper.runtime_filter_nums()); |
46 | 19 | DorisMetrics::instance()->scanner_cnt->increment(1); |
47 | 19 | } |
48 | | |
49 | 8 | Status Scanner::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) { |
50 | 8 | 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 | 8 | const auto& projections = _local_state->_projections; |
58 | 8 | if (!projections.empty()) { |
59 | 2 | _projections.resize(projections.size()); |
60 | 4 | for (size_t i = 0; i != projections.size(); ++i) { |
61 | 2 | RETURN_IF_ERROR(projections[i]->clone(state, _projections[i])); |
62 | 2 | } |
63 | 2 | } |
64 | | |
65 | 8 | const auto& intermediate_projections = _local_state->_intermediate_projections; |
66 | 8 | 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 | 8 | return Status::OK(); |
78 | 8 | } |
79 | | |
80 | 4 | Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool* eos) { |
81 | 4 | SCOPED_CONCURRENCY_COUNT(ConcurrencyStatsManager::instance().vscanner_get_block); |
82 | 4 | auto& row_descriptor = _local_state->_parent->row_descriptor(); |
83 | 4 | if (_output_row_descriptor) { |
84 | 4 | _origin_block.clear_column_data(row_descriptor.num_materialized_slots()); |
85 | 4 | if (!_can_merge_padding_blocks(_padding_block, _origin_block)) { |
86 | 3 | DORIS_CHECK(_padding_block.empty()) |
87 | 0 | << "padding policy must remain stable for one scanner"; |
88 | | // Some physical columns carry file-local state that an upper projection must consume |
89 | | // before the next split is read. Padding those blocks first would make correctness |
90 | | // depend on whether two file tails happen to share one output batch. |
91 | 3 | RETURN_IF_ERROR(get_block(state, &_origin_block, eos)); |
92 | 3 | return _do_projections(&_origin_block, block); |
93 | 3 | } |
94 | 1 | const auto min_batch_size = std::max(state->batch_size() / 2, 1); |
95 | 1 | const auto block_max_bytes = state->preferred_block_size_bytes(); |
96 | 2 | while (_padding_block.rows() < min_batch_size && _padding_block.bytes() < block_max_bytes && |
97 | 2 | !*eos) { |
98 | 2 | RETURN_IF_ERROR(get_block(state, &_origin_block, eos)); |
99 | 2 | if (*eos) { |
100 | | // For the final block, merge any padding directly and return eos in this call. |
101 | | // The merged tail can be larger than the target batch, but each source block is |
102 | | // already bounded by the lower scanner. |
103 | 1 | RETURN_IF_ERROR(_merge_padding_block()); |
104 | 1 | _origin_block.clear_column_data(row_descriptor.num_materialized_slots()); |
105 | 1 | break; |
106 | 1 | } |
107 | 1 | if (_origin_block.rows() >= min_batch_size) { |
108 | 0 | break; |
109 | 0 | } |
110 | | |
111 | 1 | if (_origin_block.rows() + _padding_block.rows() <= state->batch_size() && |
112 | 1 | _origin_block.bytes() + _padding_block.bytes() <= block_max_bytes) { |
113 | 1 | RETURN_IF_ERROR(_merge_padding_block()); |
114 | 1 | _origin_block.clear_column_data(row_descriptor.num_materialized_slots()); |
115 | 1 | } else { |
116 | 0 | if (_origin_block.rows() < _padding_block.rows()) { |
117 | 0 | _padding_block.swap(_origin_block); |
118 | 0 | } |
119 | 0 | break; |
120 | 0 | } |
121 | 1 | } |
122 | | |
123 | 1 | if (_origin_block.empty() && !_padding_block.empty()) { |
124 | 1 | _padding_block.swap(_origin_block); |
125 | 1 | } |
126 | 1 | return _do_projections(&_origin_block, block); |
127 | 1 | } else { |
128 | 0 | return get_block(state, block, eos); |
129 | 0 | } |
130 | 4 | } |
131 | | |
132 | 11 | Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) { |
133 | | // only empty block should be here |
134 | 11 | DCHECK(block->rows() == 0); |
135 | | // scanner running time |
136 | 11 | SCOPED_RAW_TIMER(&_per_scanner_timer); |
137 | 11 | int64_t rows_read_threshold = _num_rows_read + config::doris_scanner_row_num; |
138 | 11 | if (!block->mem_reuse()) { |
139 | 18 | for (auto* const slot_desc : _output_tuple_desc->slots()) { |
140 | 18 | block->insert(ColumnWithTypeAndName(slot_desc->get_empty_mutable_column(), |
141 | 18 | slot_desc->get_data_type_ptr(), |
142 | 18 | slot_desc->col_name())); |
143 | 18 | } |
144 | 8 | } |
145 | | |
146 | 11 | { |
147 | 13 | do { |
148 | | // 1. Get input block from scanner |
149 | 13 | { |
150 | | // get block time |
151 | 13 | SCOPED_TIMER(_local_state->_scan_timer); |
152 | 13 | RETURN_IF_ERROR(_get_block_impl(state, block, eof)); |
153 | 11 | if (*eof) { |
154 | 3 | DCHECK(block->rows() == 0); |
155 | 3 | break; |
156 | 3 | } |
157 | 8 | _num_rows_read += block->rows(); |
158 | 8 | _num_byte_read += block->allocated_bytes(); |
159 | 8 | } |
160 | | |
161 | | // 2. Filter the output block finally. |
162 | 0 | { |
163 | 8 | SCOPED_TIMER(_local_state->_filter_timer); |
164 | 8 | RETURN_IF_ERROR(_filter_output_block(block)); |
165 | 8 | } |
166 | | // record rows return (after filter) for _limit check |
167 | 8 | _num_rows_return += block->rows(); |
168 | 8 | } while (!_should_stop && !state->is_cancelled() && block->rows() == 0 && !(*eof) && |
169 | 8 | _num_rows_read < rows_read_threshold); |
170 | 11 | } |
171 | | |
172 | 9 | if (state->is_cancelled()) { |
173 | | // TODO: Should return the specific ErrorStatus instead of just Cancelled. |
174 | 0 | return Status::Cancelled("cancelled"); |
175 | 0 | } |
176 | 9 | *eof = *eof || _should_stop; |
177 | | // set eof to true if per scanner limit is reached |
178 | | // currently for query: ORDER BY key LIMIT n |
179 | 9 | *eof = *eof || (_limit > 0 && _num_rows_return >= _limit); |
180 | | |
181 | 9 | return Status::OK(); |
182 | 9 | } |
183 | | |
184 | 8 | Status Scanner::_filter_output_block(Block* block) { |
185 | 8 | auto old_rows = block->rows(); |
186 | 8 | Status st = VExprContext::filter_block(_conjuncts, block, block->columns()); |
187 | 8 | _counter.num_rows_unselected += old_rows - block->rows(); |
188 | 8 | return st; |
189 | 8 | } |
190 | | |
191 | 4 | Status Scanner::_do_projections(Block* origin_block, Block* output_block) { |
192 | 4 | SCOPED_RAW_TIMER(&_per_scanner_timer); |
193 | 4 | SCOPED_RAW_TIMER(&_projection_timer); |
194 | | |
195 | 4 | const size_t rows = origin_block->rows(); |
196 | 4 | if (rows == 0) { |
197 | 1 | return Status::OK(); |
198 | 1 | } |
199 | 3 | Block input_block = *origin_block; |
200 | | |
201 | 3 | std::vector<int> result_column_ids; |
202 | 3 | for (auto& projections : _intermediate_projections) { |
203 | 0 | result_column_ids.resize(projections.size()); |
204 | 0 | for (int i = 0; i < projections.size(); i++) { |
205 | 0 | RETURN_IF_ERROR(projections[i]->execute(&input_block, &result_column_ids[i])); |
206 | 0 | } |
207 | 0 | input_block.shuffle_columns(result_column_ids); |
208 | 0 | } |
209 | | |
210 | 3 | DCHECK_EQ(rows, input_block.rows()); |
211 | 3 | auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block( |
212 | 3 | output_block, *_output_row_descriptor); |
213 | 3 | auto& mutable_block = scoped_mutable_block.mutable_block(); |
214 | | |
215 | 3 | auto& mutable_columns = mutable_block.mutable_columns(); |
216 | | |
217 | 3 | DCHECK_EQ(mutable_columns.size(), _projections.size()); |
218 | | |
219 | 6 | for (int i = 0; i < mutable_columns.size(); ++i) { |
220 | 3 | ColumnPtr column_ptr; |
221 | 3 | RETURN_IF_ERROR(_projections[i]->execute(&input_block, column_ptr)); |
222 | 3 | column_ptr = column_ptr->convert_to_full_column_if_const(); |
223 | 3 | if (mutable_columns[i]->is_nullable() != column_ptr->is_nullable()) { |
224 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, "Nullable mismatch"); |
225 | 0 | } |
226 | 3 | mutable_columns[i] = IColumn::mutate(std::move(column_ptr)); |
227 | 3 | } |
228 | | |
229 | 3 | scoped_mutable_block.restore(); |
230 | | |
231 | 3 | return Status::OK(); |
232 | 3 | } |
233 | | |
234 | 2 | Status Scanner::try_append_late_arrival_runtime_filter() { |
235 | 2 | if (_applied_rf_num == _total_rf_num) { |
236 | 1 | return Status::OK(); |
237 | 1 | } |
238 | 2 | DCHECK(_applied_rf_num < _total_rf_num); |
239 | 1 | int arrived_rf_num = 0; |
240 | 1 | RETURN_IF_ERROR(_local_state->update_late_arrival_runtime_filter(_state, arrived_rf_num)); |
241 | | |
242 | 1 | if (arrived_rf_num == _applied_rf_num) { |
243 | | // No newly arrived runtime filters, just return; |
244 | 0 | return Status::OK(); |
245 | 0 | } |
246 | | |
247 | | // avoid conjunct destroy in used by storage layer |
248 | 1 | _conjuncts.clear(); |
249 | 1 | RETURN_IF_ERROR(_local_state->clone_conjunct_ctxs(_conjuncts)); |
250 | 1 | _applied_rf_num = arrived_rf_num; |
251 | 1 | return Status::OK(); |
252 | 1 | } |
253 | | |
254 | 2 | uint64_t Scanner::_current_condition_cache_digest() const { |
255 | 2 | DORIS_CHECK(_state != nullptr); |
256 | 2 | DORIS_CHECK(_local_state != nullptr); |
257 | 2 | if (_local_state->get_condition_cache_digest() == 0) { |
258 | 2 | return 0; |
259 | 2 | } |
260 | | |
261 | | // ScanLocalState computed its digest after collecting the RFs that were ready during open(). A |
262 | | // scanner may later clone more RF conjuncts between file splits, so rebuild from its current |
263 | | // snapshot instead of reusing that stale value. For example, split 0 may use P, while split 1 |
264 | | // starts after an IN RF with payload {7, 9} arrives and must use digest(P AND RF{7, 9}). A |
265 | | // different payload {8, 10} consequently receives a different key. get_digest() returning zero |
266 | | // is the correctness fallback for an RF whose complete semantics cannot be represented. |
267 | 0 | return _build_condition_cache_digest(_state->query_options().condition_cache_digest, |
268 | 0 | _conjuncts); |
269 | 2 | } |
270 | | |
271 | 13 | uint64_t Scanner::_build_condition_cache_digest(uint64_t seed, const VExprContextSPtrs& conjuncts) { |
272 | 13 | for (const auto& conjunct : conjuncts) { |
273 | 13 | seed = conjunct->get_digest(seed); |
274 | 13 | if (seed == 0) { |
275 | 1 | return 0; |
276 | 1 | } |
277 | 13 | } |
278 | 12 | return seed; |
279 | 13 | } |
280 | | |
281 | | #ifdef BE_TEST |
282 | | uint64_t Scanner::TEST_build_condition_cache_digest(uint64_t seed, |
283 | 13 | const VExprContextSPtrs& conjuncts) { |
284 | 13 | return _build_condition_cache_digest(seed, conjuncts); |
285 | 13 | } |
286 | | #endif |
287 | | |
288 | 17 | Status Scanner::close(RuntimeState* state) { |
289 | 17 | if (_is_closed) { |
290 | 0 | return Status::OK(); |
291 | 0 | } |
292 | | #ifndef BE_TEST |
293 | | COUNTER_UPDATE(_local_state->_scanner_wait_worker_timer, _scanner_wait_worker_timer); |
294 | | #endif |
295 | 17 | _is_closed = true; |
296 | 17 | return Status::OK(); |
297 | 17 | } |
298 | | |
299 | 0 | void Scanner::_collect_profile_before_close() { |
300 | 0 | COUNTER_UPDATE(_local_state->_scan_cpu_timer, _scan_cpu_timer); |
301 | 0 | COUNTER_UPDATE(_local_state->_rows_read_counter, _num_rows_read); |
302 | | |
303 | | // Update stats for load. See _should_update_load_counters() for why this is gated. |
304 | 0 | if (_should_update_load_counters()) { |
305 | 0 | _state->update_num_rows_load_filtered(_counter.num_rows_filtered); |
306 | 0 | _state->update_num_rows_load_unselected(_counter.num_rows_unselected); |
307 | 0 | } |
308 | 0 | } |
309 | | |
310 | 0 | void Scanner::update_scan_cpu_timer() { |
311 | 0 | int64_t cpu_time = _cpu_watch.elapsed_time(); |
312 | 0 | _scan_cpu_timer += cpu_time; |
313 | 0 | if (_state && _state->get_query_ctx()) { |
314 | 0 | _state->get_query_ctx()->resource_ctx()->cpu_context()->update_cpu_cost_ms(cpu_time); |
315 | 0 | } |
316 | 0 | } |
317 | | |
318 | | } // namespace doris |