/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/defer_op.h" |
32 | | |
33 | | namespace doris { |
34 | | |
35 | | Scanner::Scanner(RuntimeState* state, ScanLocalStateBase* local_state, int64_t limit, |
36 | | RuntimeProfile* profile) |
37 | 15 | : _state(state), |
38 | 15 | _local_state(local_state), |
39 | 15 | _limit(limit), |
40 | 15 | _profile(profile), |
41 | 15 | _output_tuple_desc(_local_state->output_tuple_desc()), |
42 | 15 | _output_row_descriptor(_local_state->_parent->output_row_descriptor()), |
43 | 15 | _has_prepared(false) { |
44 | 15 | _total_rf_num = cast_set<int>(_local_state->_helper.runtime_filter_nums()); |
45 | 15 | DorisMetrics::instance()->scanner_cnt->increment(1); |
46 | 15 | } |
47 | | |
48 | 4 | Status Scanner::init(RuntimeState* state, const VExprContextSPtrs& conjuncts) { |
49 | 4 | if (!conjuncts.empty()) { |
50 | 0 | _conjuncts.resize(conjuncts.size()); |
51 | 0 | for (size_t i = 0; i != conjuncts.size(); ++i) { |
52 | 0 | RETURN_IF_ERROR(conjuncts[i]->clone(state, _conjuncts[i])); |
53 | 0 | } |
54 | 0 | } |
55 | | |
56 | 4 | const auto& projections = _local_state->_projections; |
57 | 4 | if (!projections.empty()) { |
58 | 0 | _projections.resize(projections.size()); |
59 | 0 | for (size_t i = 0; i != projections.size(); ++i) { |
60 | 0 | RETURN_IF_ERROR(projections[i]->clone(state, _projections[i])); |
61 | 0 | } |
62 | 0 | } |
63 | | |
64 | 4 | const auto& intermediate_projections = _local_state->_intermediate_projections; |
65 | 4 | if (!intermediate_projections.empty()) { |
66 | 0 | _intermediate_projections.resize(intermediate_projections.size()); |
67 | 0 | for (int i = 0; i < intermediate_projections.size(); i++) { |
68 | 0 | _intermediate_projections[i].resize(intermediate_projections[i].size()); |
69 | 0 | for (int j = 0; j < intermediate_projections[i].size(); j++) { |
70 | 0 | RETURN_IF_ERROR(intermediate_projections[i][j]->clone( |
71 | 0 | state, _intermediate_projections[i][j])); |
72 | 0 | } |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | 4 | return Status::OK(); |
77 | 4 | } |
78 | | |
79 | 0 | Status Scanner::get_block_after_projects(RuntimeState* state, Block* block, bool* eos) { |
80 | 0 | auto& row_descriptor = _local_state->_parent->row_descriptor(); |
81 | 0 | if (_output_row_descriptor) { |
82 | 0 | if (_alreay_eos) { |
83 | 0 | *eos = true; |
84 | 0 | _padding_block.swap(_origin_block); |
85 | 0 | } else { |
86 | 0 | _origin_block.clear_column_data(row_descriptor.num_materialized_slots()); |
87 | 0 | const auto min_batch_size = std::max(state->batch_size() / 2, 1); |
88 | 0 | while (_padding_block.rows() < min_batch_size && !*eos) { |
89 | 0 | RETURN_IF_ERROR(get_block(state, &_origin_block, eos)); |
90 | 0 | if (_origin_block.rows() >= min_batch_size) { |
91 | 0 | break; |
92 | 0 | } |
93 | | |
94 | 0 | if (_origin_block.rows() + _padding_block.rows() <= state->batch_size()) { |
95 | 0 | RETURN_IF_ERROR(_merge_padding_block()); |
96 | 0 | _origin_block.clear_column_data(row_descriptor.num_materialized_slots()); |
97 | 0 | } else { |
98 | 0 | if (_origin_block.rows() < _padding_block.rows()) { |
99 | 0 | _padding_block.swap(_origin_block); |
100 | 0 | } |
101 | 0 | break; |
102 | 0 | } |
103 | 0 | } |
104 | 0 | } |
105 | | |
106 | | // first output the origin block change eos = false, next time output padding block |
107 | | // set the eos to true |
108 | 0 | if (*eos && !_padding_block.empty() && !_origin_block.empty()) { |
109 | 0 | _alreay_eos = true; |
110 | 0 | *eos = false; |
111 | 0 | } |
112 | 0 | if (_origin_block.empty() && !_padding_block.empty()) { |
113 | 0 | _padding_block.swap(_origin_block); |
114 | 0 | } |
115 | 0 | return _do_projections(&_origin_block, block); |
116 | 0 | } else { |
117 | 0 | return get_block(state, block, eos); |
118 | 0 | } |
119 | 0 | } |
120 | | |
121 | 6 | Status Scanner::get_block(RuntimeState* state, Block* block, bool* eof) { |
122 | | // only empty block should be here |
123 | 6 | DCHECK(block->rows() == 0); |
124 | | // scanner running time |
125 | 6 | SCOPED_RAW_TIMER(&_per_scanner_timer); |
126 | 6 | int64_t rows_read_threshold = _num_rows_read + config::doris_scanner_row_num; |
127 | 6 | if (!block->mem_reuse()) { |
128 | 15 | for (auto* const slot_desc : _output_tuple_desc->slots()) { |
129 | 15 | block->insert(ColumnWithTypeAndName(slot_desc->get_empty_mutable_column(), |
130 | 15 | slot_desc->get_data_type_ptr(), |
131 | 15 | slot_desc->col_name())); |
132 | 15 | } |
133 | 5 | } |
134 | | |
135 | 6 | { |
136 | 8 | do { |
137 | | // 1. Get input block from scanner |
138 | 8 | { |
139 | | // get block time |
140 | 8 | SCOPED_TIMER(_local_state->_scan_timer); |
141 | 8 | RETURN_IF_ERROR(_get_block_impl(state, block, eof)); |
142 | 6 | if (*eof) { |
143 | 2 | DCHECK(block->rows() == 0); |
144 | 2 | break; |
145 | 2 | } |
146 | 4 | _num_rows_read += block->rows(); |
147 | 4 | _num_byte_read += block->allocated_bytes(); |
148 | 4 | } |
149 | | |
150 | | // 2. Filter the output block finally. |
151 | 0 | { |
152 | 4 | SCOPED_TIMER(_local_state->_filter_timer); |
153 | 4 | RETURN_IF_ERROR(_filter_output_block(block)); |
154 | 4 | } |
155 | | // record rows return (after filter) for _limit check |
156 | 4 | _num_rows_return += block->rows(); |
157 | 4 | } while (!_should_stop && !state->is_cancelled() && block->rows() == 0 && !(*eof) && |
158 | 4 | _num_rows_read < rows_read_threshold); |
159 | 6 | } |
160 | | |
161 | 4 | if (state->is_cancelled()) { |
162 | | // TODO: Should return the specific ErrorStatus instead of just Cancelled. |
163 | 0 | return Status::Cancelled("cancelled"); |
164 | 0 | } |
165 | 4 | *eof = *eof || _should_stop; |
166 | | // set eof to true if per scanner limit is reached |
167 | | // currently for query: ORDER BY key LIMIT n |
168 | 4 | *eof = *eof || (_limit > 0 && _num_rows_return >= _limit); |
169 | | |
170 | 4 | return Status::OK(); |
171 | 4 | } |
172 | | |
173 | 4 | Status Scanner::_filter_output_block(Block* block) { |
174 | 4 | auto old_rows = block->rows(); |
175 | 4 | Status st = VExprContext::filter_block(_conjuncts, block, block->columns()); |
176 | 4 | _counter.num_rows_unselected += old_rows - block->rows(); |
177 | 4 | return st; |
178 | 4 | } |
179 | | |
180 | 0 | Status Scanner::_do_projections(Block* origin_block, Block* output_block) { |
181 | 0 | SCOPED_RAW_TIMER(&_per_scanner_timer); |
182 | 0 | SCOPED_RAW_TIMER(&_projection_timer); |
183 | |
|
184 | 0 | const size_t rows = origin_block->rows(); |
185 | 0 | if (rows == 0) { |
186 | 0 | return Status::OK(); |
187 | 0 | } |
188 | 0 | Block input_block = *origin_block; |
189 | |
|
190 | 0 | std::vector<int> result_column_ids; |
191 | 0 | for (auto& projections : _intermediate_projections) { |
192 | 0 | result_column_ids.resize(projections.size()); |
193 | 0 | for (int i = 0; i < projections.size(); i++) { |
194 | 0 | RETURN_IF_ERROR(projections[i]->execute(&input_block, &result_column_ids[i])); |
195 | 0 | } |
196 | 0 | input_block.shuffle_columns(result_column_ids); |
197 | 0 | } |
198 | | |
199 | 0 | DCHECK_EQ(rows, input_block.rows()); |
200 | 0 | MutableBlock mutable_block = |
201 | 0 | VectorizedUtils::build_mutable_mem_reuse_block(output_block, *_output_row_descriptor); |
202 | |
|
203 | 0 | auto& mutable_columns = mutable_block.mutable_columns(); |
204 | |
|
205 | 0 | DCHECK_EQ(mutable_columns.size(), _projections.size()); |
206 | |
|
207 | 0 | for (int i = 0; i < mutable_columns.size(); ++i) { |
208 | 0 | ColumnPtr column_ptr; |
209 | 0 | RETURN_IF_ERROR(_projections[i]->execute(&input_block, column_ptr)); |
210 | 0 | column_ptr = column_ptr->convert_to_full_column_if_const(); |
211 | 0 | if (mutable_columns[i]->is_nullable() != column_ptr->is_nullable()) { |
212 | 0 | throw Exception(ErrorCode::INTERNAL_ERROR, "Nullable mismatch"); |
213 | 0 | } |
214 | 0 | mutable_columns[i]->insert_range_from(*column_ptr, 0, rows); |
215 | 0 | } |
216 | 0 | DCHECK(mutable_block.rows() == rows); |
217 | 0 | output_block->set_columns(std::move(mutable_columns)); |
218 | |
|
219 | 0 | return Status::OK(); |
220 | 0 | } |
221 | | |
222 | 0 | Status Scanner::try_append_late_arrival_runtime_filter() { |
223 | 0 | if (_applied_rf_num == _total_rf_num) { |
224 | 0 | return Status::OK(); |
225 | 0 | } |
226 | 0 | DCHECK(_applied_rf_num < _total_rf_num); |
227 | 0 | int arrived_rf_num = 0; |
228 | 0 | RETURN_IF_ERROR(_local_state->update_late_arrival_runtime_filter(_state, arrived_rf_num)); |
229 | | |
230 | 0 | if (arrived_rf_num == _applied_rf_num) { |
231 | | // No newly arrived runtime filters, just return; |
232 | 0 | return Status::OK(); |
233 | 0 | } |
234 | | |
235 | | // avoid conjunct destroy in used by storage layer |
236 | 0 | _conjuncts.clear(); |
237 | 0 | RETURN_IF_ERROR(_local_state->clone_conjunct_ctxs(_conjuncts)); |
238 | 0 | return Status::OK(); |
239 | 0 | } |
240 | | |
241 | 15 | Status Scanner::close(RuntimeState* state) { |
242 | 15 | if (_is_closed) { |
243 | 0 | return Status::OK(); |
244 | 0 | } |
245 | | #ifndef BE_TEST |
246 | | COUNTER_UPDATE(_local_state->_scanner_wait_worker_timer, _scanner_wait_worker_timer); |
247 | | #endif |
248 | 15 | _is_closed = true; |
249 | 15 | return Status::OK(); |
250 | 15 | } |
251 | | |
252 | 0 | void Scanner::_collect_profile_before_close() { |
253 | 0 | COUNTER_UPDATE(_local_state->_scan_cpu_timer, _scan_cpu_timer); |
254 | 0 | COUNTER_UPDATE(_local_state->_rows_read_counter, _num_rows_read); |
255 | | |
256 | | // Update stats for load |
257 | 0 | _state->update_num_rows_load_filtered(_counter.num_rows_filtered); |
258 | 0 | _state->update_num_rows_load_unselected(_counter.num_rows_unselected); |
259 | 0 | } |
260 | | |
261 | 0 | void Scanner::update_scan_cpu_timer() { |
262 | 0 | int64_t cpu_time = _cpu_watch.elapsed_time(); |
263 | 0 | _scan_cpu_timer += cpu_time; |
264 | 0 | if (_state && _state->get_query_ctx()) { |
265 | 0 | _state->get_query_ctx()->resource_ctx()->cpu_context()->update_cpu_cost_ms(cpu_time); |
266 | 0 | } |
267 | 0 | } |
268 | | |
269 | | } // namespace doris |