be/src/exprs/vexpr_context.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 "exprs/vexpr_context.h" |
19 | | |
20 | | #include <algorithm> |
21 | | #include <cstdint> |
22 | | #include <string> |
23 | | |
24 | | #include "common/compiler_util.h" // IWYU pragma: keep |
25 | | #include "common/exception.h" |
26 | | #include "common/logging.h" |
27 | | #include "common/status.h" |
28 | | #include "core/block/column_numbers.h" |
29 | | #include "core/block/column_with_type_and_name.h" |
30 | | #include "core/block/columns_with_type_and_name.h" |
31 | | #include "core/column/column.h" |
32 | | #include "core/column/column_const.h" |
33 | | #include "exec/common/util.hpp" |
34 | | #include "exprs/function_context.h" |
35 | | #include "exprs/vexpr.h" |
36 | | #include "runtime/runtime_state.h" |
37 | | #include "runtime/thread_context.h" |
38 | | #include "storage/olap_common.h" |
39 | | #include "storage/segment/column_reader.h" |
40 | | #include "util/simd/bits.h" |
41 | | |
42 | | namespace doris { |
43 | | class RowDescriptor; |
44 | | } // namespace doris |
45 | | |
46 | | namespace doris { |
47 | | |
48 | 18.9M | VExprContext::~VExprContext() { |
49 | | // In runtime filter, only create expr context to get expr root, will not call |
50 | | // prepare or open, so that it is not need to call close. And call close may core |
51 | | // because the function context in expr is not set. |
52 | 18.9M | if (!_prepared || !_opened) { |
53 | 251k | return; |
54 | 251k | } |
55 | 18.6M | try { |
56 | 18.6M | close(); |
57 | 18.6M | } catch (const Exception& e) { |
58 | 0 | LOG(WARNING) << "Exception occurs when expr context deconstruct: " << e.to_string(); |
59 | 0 | } |
60 | 18.6M | } |
61 | | |
62 | 1.13M | Status VExprContext::execute(Block* block, int* result_column_id) { |
63 | 1.13M | Status st; |
64 | 1.13M | RETURN_IF_CATCH_EXCEPTION({ |
65 | 1.13M | st = _root->execute(this, block, result_column_id); |
66 | 1.13M | _last_result_column_id = *result_column_id; |
67 | | // We should first check the status, as some expressions might incorrectly set result_column_id, even if the st is not ok. |
68 | 1.13M | if (st.ok() && _last_result_column_id != -1) { |
69 | 1.13M | block->get_by_position(*result_column_id).column->sanity_check(); |
70 | 1.13M | RETURN_IF_ERROR( |
71 | 1.13M | block->get_by_position(*result_column_id).check_type_and_column_match()); |
72 | 1.13M | } |
73 | 1.13M | }); |
74 | 1.13M | return st; |
75 | 1.13M | } |
76 | | |
77 | 1.97M | Status VExprContext::execute(const Block* block, ColumnPtr& result_column) { |
78 | 1.97M | Status st; |
79 | 1.97M | RETURN_IF_CATCH_EXCEPTION( |
80 | 1.97M | { st = _root->execute_column(this, block, nullptr, block->rows(), result_column); }); |
81 | 1.97M | return st; |
82 | 1.97M | } |
83 | | |
84 | 35.2k | Status VExprContext::execute(const Block* block, ColumnWithTypeAndName& result_data) { |
85 | 35.2k | Status st; |
86 | 35.2k | ColumnPtr result_column; |
87 | 35.2k | RETURN_IF_CATCH_EXCEPTION( |
88 | 35.2k | { st = _root->execute_column(this, block, nullptr, block->rows(), result_column); }); |
89 | 35.3k | RETURN_IF_ERROR(st); |
90 | 35.3k | result_data.column = result_column; |
91 | 35.3k | result_data.type = execute_type(block); |
92 | 35.3k | result_data.name = _root->expr_name(); |
93 | 35.3k | return Status::OK(); |
94 | 35.3k | } |
95 | | |
96 | 809k | DataTypePtr VExprContext::execute_type(const Block* block) { |
97 | 809k | return _root->execute_type(block); |
98 | 809k | } |
99 | | |
100 | 1.03M | Status VExprContext::execute_const_expr(ColumnWithTypeAndName& result) { |
101 | 1.03M | Status st; |
102 | 1.03M | RETURN_IF_CATCH_EXCEPTION( |
103 | 1.03M | { st = _root->execute_column(this, nullptr, nullptr, 1, result.column); }); |
104 | 1.03M | RETURN_IF_ERROR(st); |
105 | 1.03M | result.type = _root->execute_type(nullptr); |
106 | 1.03M | result.name = _root->expr_name(); |
107 | 1.03M | return Status::OK(); |
108 | 1.03M | } |
109 | | |
110 | 773k | [[nodiscard]] const std::string& VExprContext::expr_name() const { |
111 | 773k | return _root->expr_name(); |
112 | 773k | } |
113 | | |
114 | 0 | bool VExprContext::is_blockable() const { |
115 | 0 | return _root->is_blockable(); |
116 | 0 | } |
117 | | |
118 | 4.99M | Status VExprContext::prepare(RuntimeState* state, const RowDescriptor& row_desc) { |
119 | 4.99M | _prepared = true; |
120 | 4.99M | Status st; |
121 | 4.99M | RETURN_IF_CATCH_EXCEPTION({ st = _root->prepare(state, row_desc, this); }); |
122 | 4.99M | return st; |
123 | 4.99M | } |
124 | | |
125 | 4.99M | Status VExprContext::open(RuntimeState* state) { |
126 | 4.99M | DCHECK(_prepared); |
127 | 4.99M | if (_opened) { |
128 | 47 | return Status::OK(); |
129 | 47 | } |
130 | 4.99M | _opened = true; |
131 | | // Fragment-local state is only initialized for original contexts. Clones inherit the |
132 | | // original's fragment state and only need to have thread-local state initialized. |
133 | 4.99M | FunctionContext::FunctionStateScope scope = |
134 | 4.99M | _is_clone ? FunctionContext::THREAD_LOCAL : FunctionContext::FRAGMENT_LOCAL; |
135 | 4.99M | Status st; |
136 | 4.99M | RETURN_IF_CATCH_EXCEPTION({ st = _root->open(state, this, scope); }); |
137 | 4.99M | return st; |
138 | 4.99M | } |
139 | | |
140 | 18.6M | void VExprContext::close() { |
141 | | // Sometimes expr context may not have a root, then it need not call close |
142 | 18.6M | if (_root == nullptr) { |
143 | 0 | return; |
144 | 0 | } |
145 | 18.6M | FunctionContext::FunctionStateScope scope = |
146 | 18.6M | _is_clone ? FunctionContext::THREAD_LOCAL : FunctionContext::FRAGMENT_LOCAL; |
147 | 18.6M | _root->close(this, scope); |
148 | 18.6M | } |
149 | | |
150 | 13.5M | Status VExprContext::clone(RuntimeState* state, VExprContextSPtr& new_ctx) { |
151 | 18.4E | DCHECK(_prepared) << "expr context not prepared"; |
152 | 13.5M | DCHECK(_opened); |
153 | 13.5M | DCHECK(new_ctx.get() == nullptr); |
154 | | |
155 | 13.5M | new_ctx = std::make_shared<VExprContext>(_root); |
156 | 13.5M | for (auto& _fn_context : _fn_contexts) { |
157 | 1.21M | new_ctx->_fn_contexts.push_back(_fn_context->clone()); |
158 | 1.21M | } |
159 | | |
160 | 13.5M | new_ctx->_is_clone = true; |
161 | 13.5M | new_ctx->_prepared = true; |
162 | 13.5M | new_ctx->_opened = true; |
163 | | // segment_v2::AnnRangeSearchRuntime should be cloned as well. |
164 | | // The object of segment_v2::AnnRangeSearchRuntime is not shared by threads. |
165 | 13.5M | new_ctx->_ann_range_search_runtime = this->_ann_range_search_runtime; |
166 | | |
167 | 13.5M | return _root->open(state, new_ctx.get(), FunctionContext::THREAD_LOCAL); |
168 | 13.5M | } |
169 | | |
170 | 0 | void VExprContext::clone_fn_contexts(VExprContext* other) { |
171 | 0 | for (auto& _fn_context : _fn_contexts) { |
172 | 0 | other->_fn_contexts.push_back(_fn_context->clone()); |
173 | 0 | } |
174 | 0 | } |
175 | | |
176 | | int VExprContext::register_function_context(RuntimeState* state, const DataTypePtr& return_type, |
177 | 610k | const std::vector<DataTypePtr>& arg_types) { |
178 | 610k | _fn_contexts.push_back(FunctionContext::create_context(state, return_type, arg_types)); |
179 | 610k | _fn_contexts.back()->set_check_overflow_for_decimal(state->check_overflow_for_decimal()); |
180 | 610k | _fn_contexts.back()->set_enable_strict_mode(state->enable_strict_mode()); |
181 | 610k | return static_cast<int>(_fn_contexts.size()) - 1; |
182 | 610k | } |
183 | | |
184 | 19.0k | Status VExprContext::evaluate_inverted_index(uint32_t segment_num_rows) { |
185 | 19.0k | Status st; |
186 | 19.0k | RETURN_IF_CATCH_EXCEPTION({ st = _root->evaluate_inverted_index(this, segment_num_rows); }); |
187 | 18.9k | return st; |
188 | 19.0k | } |
189 | | |
190 | | ZoneMapFilterResult VExprContext::evaluate_zonemap_filter(const VExprContextSPtrs& conjuncts, |
191 | 18.6k | const ZoneMapEvalContext& ctx) { |
192 | 21.1k | for (const auto& conjunct : conjuncts) { |
193 | 21.1k | DORIS_CHECK(conjunct != nullptr); |
194 | 21.1k | const auto& root = conjunct->root(); |
195 | 21.1k | DORIS_CHECK(root != nullptr); |
196 | 21.1k | if (!root->can_evaluate_zonemap_filter()) { |
197 | 18.0k | continue; |
198 | 18.0k | } |
199 | 3.15k | if (root->evaluate_zonemap_filter(ctx) == ZoneMapFilterResult::kNoMatch) { |
200 | 459 | return ZoneMapFilterResult::kNoMatch; |
201 | 459 | } |
202 | 3.15k | } |
203 | 18.1k | return ZoneMapFilterResult::kMayMatch; |
204 | 18.6k | } |
205 | | |
206 | | ZoneMapFilterResult VExprContext::evaluate_dictionary_filter(const VExprContextSPtrs& conjuncts, |
207 | 29 | const DictionaryEvalContext& ctx) { |
208 | 29 | for (const auto& conjunct : conjuncts) { |
209 | 29 | DORIS_CHECK(conjunct != nullptr); |
210 | 29 | const auto& root = conjunct->root(); |
211 | 29 | DORIS_CHECK(root != nullptr); |
212 | 29 | if (!root->can_evaluate_dictionary_filter()) { |
213 | 0 | continue; |
214 | 0 | } |
215 | 29 | if (root->evaluate_dictionary_filter(ctx) == ZoneMapFilterResult::kNoMatch) { |
216 | 20 | return ZoneMapFilterResult::kNoMatch; |
217 | 20 | } |
218 | 29 | } |
219 | 9 | return ZoneMapFilterResult::kMayMatch; |
220 | 29 | } |
221 | | |
222 | | ZoneMapFilterResult VExprContext::evaluate_bloom_filter(const VExprContextSPtrs& conjuncts, |
223 | 15 | const BloomFilterEvalContext& ctx) { |
224 | 15 | for (const auto& conjunct : conjuncts) { |
225 | 15 | DORIS_CHECK(conjunct != nullptr); |
226 | 15 | const auto& root = conjunct->root(); |
227 | 15 | DORIS_CHECK(root != nullptr); |
228 | 15 | if (!root->can_evaluate_bloom_filter()) { |
229 | 0 | continue; |
230 | 0 | } |
231 | 15 | if (root->evaluate_bloom_filter(ctx) == ZoneMapFilterResult::kNoMatch) { |
232 | 7 | return ZoneMapFilterResult::kNoMatch; |
233 | 7 | } |
234 | 15 | } |
235 | 8 | return ZoneMapFilterResult::kMayMatch; |
236 | 15 | } |
237 | | |
238 | 18.6k | bool VExprContext::all_expr_inverted_index_evaluated() { |
239 | 18.6k | return _index_context->has_index_result_for_expr(_root.get()); |
240 | 18.6k | } |
241 | | |
242 | 50 | Status VExprContext::filter_block(VExprContext* vexpr_ctx, Block* block) { |
243 | 50 | if (vexpr_ctx == nullptr || block->rows() == 0) { |
244 | 0 | return Status::OK(); |
245 | 0 | } |
246 | 50 | ColumnPtr filter_column; |
247 | 50 | RETURN_IF_ERROR(vexpr_ctx->execute(block, filter_column)); |
248 | 50 | size_t filter_column_id = block->columns(); |
249 | 50 | block->insert({filter_column, vexpr_ctx->execute_type(block), "filter_column"}); |
250 | 50 | vexpr_ctx->_memory_usage = filter_column->allocated_bytes(); |
251 | 50 | return Block::filter_block(block, filter_column_id, filter_column_id); |
252 | 50 | } |
253 | | |
254 | | Status VExprContext::filter_block(const VExprContextSPtrs& expr_contexts, Block* block, |
255 | 1.30M | size_t column_to_keep) { |
256 | 1.30M | if (expr_contexts.empty() || block->rows() == 0) { |
257 | 1.28M | return Status::OK(); |
258 | 1.28M | } |
259 | | |
260 | 20.5k | ColumnNumbers columns_to_filter(column_to_keep); |
261 | 20.5k | std::iota(columns_to_filter.begin(), columns_to_filter.end(), 0); |
262 | | |
263 | 20.5k | return execute_conjuncts_and_filter_block(expr_contexts, block, columns_to_filter, |
264 | 20.5k | static_cast<int>(column_to_keep)); |
265 | 1.30M | } |
266 | | |
267 | | Status VExprContext::execute_conjuncts(const VExprContextSPtrs& ctxs, |
268 | | const std::vector<IColumn::Filter*>* filters, Block* block, |
269 | 3.00k | IColumn::Filter* result_filter, bool* can_filter_all) { |
270 | 3.00k | return execute_conjuncts(ctxs, filters, false, block, result_filter, can_filter_all); |
271 | 3.00k | } |
272 | | |
273 | | Status VExprContext::execute_filter(const Block* block, uint8_t* __restrict result_filter_data, |
274 | 118k | size_t rows, bool accept_null, bool* can_filter_all) { |
275 | 118k | return _root->execute_filter(this, block, result_filter_data, rows, accept_null, |
276 | 118k | can_filter_all); |
277 | 118k | } |
278 | | |
279 | | Status VExprContext::execute_conjuncts(const VExprContextSPtrs& ctxs, |
280 | | const std::vector<IColumn::Filter*>* filters, |
281 | | bool accept_null, const Block* block, |
282 | 280k | IColumn::Filter* result_filter, bool* can_filter_all) { |
283 | 280k | size_t rows = block->rows(); |
284 | 280k | DCHECK_EQ(result_filter->size(), rows); |
285 | 280k | *can_filter_all = false; |
286 | 280k | auto* __restrict result_filter_data = result_filter->data(); |
287 | 280k | for (const auto& ctx : ctxs) { |
288 | 102k | RETURN_IF_ERROR( |
289 | 102k | ctx->execute_filter(block, result_filter_data, rows, accept_null, can_filter_all)); |
290 | 102k | if (*can_filter_all) { |
291 | 28.2k | return Status::OK(); |
292 | 28.2k | } |
293 | 102k | } |
294 | 251k | if (filters != nullptr) { |
295 | 28 | for (auto* filter : *filters) { |
296 | 0 | auto* __restrict filter_data = filter->data(); |
297 | 0 | const size_t size = filter->size(); |
298 | 0 | for (size_t i = 0; i < size; ++i) { |
299 | 0 | result_filter_data[i] &= filter_data[i]; |
300 | 0 | } |
301 | 0 | if (memchr(result_filter_data, 0x1, size) == nullptr) { |
302 | 0 | *can_filter_all = true; |
303 | 0 | return Status::OK(); |
304 | 0 | } |
305 | 0 | } |
306 | 28 | } |
307 | 251k | return Status::OK(); |
308 | 251k | } |
309 | | |
310 | | Status VExprContext::execute_conjuncts(const VExprContextSPtrs& conjuncts, const Block* block, |
311 | 375 | ColumnUInt8& null_map, IColumn::Filter& filter) { |
312 | 375 | const auto& rows = block->rows(); |
313 | 375 | if (rows == 0) { |
314 | 0 | return Status::OK(); |
315 | 0 | } |
316 | 375 | if (null_map.size() != rows) { |
317 | 0 | return Status::InternalError("null_map.size()!=rows, null_map.size()={}, rows={}", |
318 | 0 | null_map.size(), rows); |
319 | 0 | } |
320 | | |
321 | 375 | auto* final_null_map = null_map.get_data().data(); |
322 | 375 | auto* final_filter_ptr = filter.data(); |
323 | | |
324 | 375 | for (const auto& conjunct : conjuncts) { |
325 | 76 | ColumnPtr result_column; |
326 | 76 | RETURN_IF_ERROR(conjunct->execute(block, result_column)); |
327 | 76 | auto [filter_column, is_const] = unpack_if_const(result_column); |
328 | 76 | const auto* nullable_column = assert_cast<const ColumnNullable*>(filter_column.get()); |
329 | 76 | if (!is_const) { |
330 | 64 | const ColumnPtr& nested_column = nullable_column->get_nested_column_ptr(); |
331 | 64 | const IColumn::Filter& result = |
332 | 64 | assert_cast<const ColumnUInt8&>(*nested_column).get_data(); |
333 | 64 | const auto* __restrict filter_data = result.data(); |
334 | 64 | const auto* __restrict null_map_data = nullable_column->get_null_map_data().data(); |
335 | 64 | DCHECK_EQ(rows, nullable_column->size()); |
336 | | |
337 | 783 | for (size_t i = 0; i != rows; ++i) { |
338 | | // null and null => null |
339 | | // null and true => null |
340 | | // null and false => false |
341 | 719 | final_null_map[i] = (final_null_map[i] & (null_map_data[i] | filter_data[i])) | |
342 | 719 | (null_map_data[i] & (final_null_map[i] | final_filter_ptr[i])); |
343 | 719 | final_filter_ptr[i] = final_filter_ptr[i] & filter_data[i]; |
344 | 719 | } |
345 | 64 | } else { |
346 | 12 | bool filter_data = nullable_column->get_bool(0); |
347 | 12 | bool null_map_data = nullable_column->is_null_at(0); |
348 | 68 | for (size_t i = 0; i != rows; ++i) { |
349 | | // null and null => null |
350 | | // null and true => null |
351 | | // null and false => false |
352 | 56 | final_null_map[i] = (final_null_map[i] & (null_map_data | filter_data)) | |
353 | 56 | (null_map_data & (final_null_map[i] | final_filter_ptr[i])); |
354 | 56 | final_filter_ptr[i] = final_filter_ptr[i] & filter_data; |
355 | 56 | } |
356 | 12 | } |
357 | 76 | } |
358 | 375 | return Status::OK(); |
359 | 375 | } |
360 | | |
361 | | // TODO Performance Optimization |
362 | | // need exception safety |
363 | | Status VExprContext::execute_conjuncts_and_filter_block(const VExprContextSPtrs& ctxs, Block* block, |
364 | | std::vector<uint32_t>& columns_to_filter, |
365 | 21.8k | int column_to_keep) { |
366 | 21.8k | IColumn::Filter result_filter(block->rows(), 1); |
367 | 21.8k | bool can_filter_all; |
368 | | |
369 | 21.8k | _reset_memory_usage(ctxs); |
370 | | |
371 | 21.8k | RETURN_IF_ERROR( |
372 | 21.8k | execute_conjuncts(ctxs, nullptr, false, block, &result_filter, &can_filter_all)); |
373 | | |
374 | | // Accumulate the usage of `result_filter` into the first context. |
375 | 21.8k | if (!ctxs.empty()) { |
376 | 21.8k | ctxs[0]->_memory_usage += result_filter.allocated_bytes(); |
377 | 21.8k | } |
378 | 21.8k | if (can_filter_all) { |
379 | 39.5k | for (auto& col : columns_to_filter) { |
380 | 39.5k | auto& column = block->get_by_position(col).column; |
381 | 39.5k | if (column->is_exclusive()) { |
382 | 33.8k | column->assert_mutable()->clear(); |
383 | 33.8k | } else { |
384 | 5.70k | column = column->clone_empty(); |
385 | 5.70k | } |
386 | 39.5k | } |
387 | 11.6k | } else { |
388 | 11.6k | try { |
389 | 11.6k | Block::filter_block_internal(block, columns_to_filter, result_filter); |
390 | 11.6k | } catch (const Exception& e) { |
391 | 0 | std::string str; |
392 | 0 | for (auto ctx : ctxs) { |
393 | 0 | if (str.length()) { |
394 | 0 | str += ","; |
395 | 0 | } |
396 | 0 | str += ctx->root()->debug_string(); |
397 | 0 | } |
398 | |
|
399 | 0 | return Status::InternalError( |
400 | 0 | "filter_block_internal meet exception, exprs=[{}], exception={}", str, |
401 | 0 | e.what()); |
402 | 0 | } |
403 | 11.6k | } |
404 | 21.8k | Block::erase_useless_column(block, column_to_keep); |
405 | 21.8k | return Status::OK(); |
406 | 21.8k | } |
407 | | |
408 | | Status VExprContext::execute_conjuncts_and_filter_block(const VExprContextSPtrs& ctxs, Block* block, |
409 | | std::vector<uint32_t>& columns_to_filter, |
410 | | int column_to_keep, |
411 | 106 | IColumn::Filter& filter) { |
412 | 106 | _reset_memory_usage(ctxs); |
413 | 106 | filter.resize_fill(block->rows(), 1); |
414 | 106 | bool can_filter_all; |
415 | 106 | RETURN_IF_ERROR(execute_conjuncts(ctxs, nullptr, false, block, &filter, &can_filter_all)); |
416 | | |
417 | | // Accumulate the usage of `result_filter` into the first context. |
418 | 106 | if (!ctxs.empty()) { |
419 | 106 | ctxs[0]->_memory_usage += filter.allocated_bytes(); |
420 | 106 | } |
421 | 106 | if (can_filter_all) { |
422 | 106 | for (auto& col : columns_to_filter) { |
423 | 106 | auto& column = block->get_by_position(col).column; |
424 | 106 | if (column->is_exclusive()) { |
425 | 106 | column->assert_mutable()->clear(); |
426 | 106 | } else { |
427 | 0 | column = column->clone_empty(); |
428 | 0 | } |
429 | 106 | } |
430 | 75 | } else { |
431 | 75 | RETURN_IF_CATCH_EXCEPTION(Block::filter_block_internal(block, columns_to_filter, filter)); |
432 | 75 | } |
433 | | |
434 | 106 | Block::erase_useless_column(block, column_to_keep); |
435 | 106 | return Status::OK(); |
436 | 106 | } |
437 | | |
438 | | // do_projection: for some query(e.g. in MultiCastDataStreamerSourceOperator::get_block()), |
439 | | // output_vexpr_ctxs will output the same column more than once, and if the output_block |
440 | | // is mem-reused later, it will trigger DCHECK_EQ(d.column->use_count(), 1) failure when |
441 | | // doing Block::clear_column_data, set do_projection to true to copy the column data to |
442 | | // avoid this problem. |
443 | | Status VExprContext::get_output_block_after_execute_exprs( |
444 | | const VExprContextSPtrs& output_vexpr_ctxs, const Block& input_block, Block* output_block, |
445 | 174k | bool do_projection) { |
446 | 174k | auto rows = input_block.rows(); |
447 | 174k | ColumnsWithTypeAndName result_columns; |
448 | 174k | _reset_memory_usage(output_vexpr_ctxs); |
449 | | |
450 | 773k | for (const auto& vexpr_ctx : output_vexpr_ctxs) { |
451 | 773k | ColumnPtr result_column; |
452 | 773k | RETURN_IF_ERROR(vexpr_ctx->execute(&input_block, result_column)); |
453 | | |
454 | 773k | auto type = vexpr_ctx->execute_type(&input_block); |
455 | 773k | const auto& name = vexpr_ctx->expr_name(); |
456 | | |
457 | 773k | vexpr_ctx->_memory_usage += result_column->allocated_bytes(); |
458 | 773k | if (do_projection) { |
459 | 12.3k | result_columns.emplace_back(result_column->clone_resized(rows), type, name); |
460 | | |
461 | 761k | } else { |
462 | 761k | result_columns.emplace_back(result_column, type, name); |
463 | 761k | } |
464 | 773k | } |
465 | 174k | *output_block = {result_columns}; |
466 | 174k | return Status::OK(); |
467 | 174k | } |
468 | | |
469 | 196k | void VExprContext::_reset_memory_usage(const VExprContextSPtrs& contexts) { |
470 | 196k | std::for_each(contexts.begin(), contexts.end(), |
471 | 801k | [](auto&& context) { context->_memory_usage = 0; }); |
472 | 196k | } |
473 | | |
474 | 22.8k | void VExprContext::prepare_ann_range_search(const doris::VectorSearchUserParams& params) { |
475 | 22.8k | if (_root == nullptr) { |
476 | 0 | return; |
477 | 0 | } |
478 | | |
479 | 22.8k | _root->prepare_ann_range_search(params, _ann_range_search_runtime, _suitable_for_ann_index); |
480 | 18.4E | VLOG_DEBUG << fmt::format("Prepare ann range search result {}, _suitable_for_ann_index {}", |
481 | 18.4E | this->_ann_range_search_runtime.to_string(), |
482 | 18.4E | this->_suitable_for_ann_index); |
483 | 22.8k | return; |
484 | 22.8k | } |
485 | | |
486 | | Status VExprContext::evaluate_ann_range_search( |
487 | | const std::vector<std::unique_ptr<segment_v2::IndexIterator>>& cid_to_index_iterators, |
488 | | const std::vector<ColumnId>& idx_to_cid, |
489 | | const std::vector<std::unique_ptr<segment_v2::ColumnIterator>>& column_iterators, |
490 | | const std::unordered_map<VExprContext*, std::unordered_map<ColumnId, VExpr*>>& |
491 | | common_expr_to_slotref_map, |
492 | | size_t rows_of_segment, roaring::Roaring& row_bitmap, |
493 | | segment_v2::AnnIndexStats& ann_index_stats, bool enable_result_cache, |
494 | 18.7k | bool* ann_range_search_executed) { |
495 | 18.7k | if (ann_range_search_executed != nullptr) { |
496 | 18.7k | *ann_range_search_executed = false; |
497 | 18.7k | } |
498 | 18.7k | if (_root == nullptr) { |
499 | 0 | return Status::OK(); |
500 | 0 | } |
501 | | |
502 | 18.7k | AnnRangeSearchEvaluationResult evaluation_result; |
503 | 18.7k | RETURN_IF_ERROR(_root->evaluate_ann_range_search( |
504 | 18.7k | _ann_range_search_runtime, cid_to_index_iterators, idx_to_cid, column_iterators, |
505 | 18.7k | rows_of_segment, row_bitmap, ann_index_stats, enable_result_cache, evaluation_result)); |
506 | | |
507 | 18.7k | if (!evaluation_result.executed) { |
508 | 18.7k | return Status::OK(); |
509 | 18.7k | } |
510 | 28 | if (ann_range_search_executed != nullptr) { |
511 | 28 | *ann_range_search_executed = true; |
512 | 28 | } |
513 | | |
514 | 25 | DCHECK(_index_context != nullptr); |
515 | 25 | _index_context->set_index_result_for_expr( |
516 | 25 | _root.get(), |
517 | 25 | segment_v2::InvertedIndexResultBitmap(std::make_shared<roaring::Roaring>(row_bitmap), |
518 | 25 | std::make_shared<roaring::Roaring>())); |
519 | | |
520 | 25 | if (!evaluation_result.dist_fulfilled) { |
521 | | // Do not perform index scan in this case. |
522 | 2 | return Status::OK(); |
523 | 2 | } |
524 | | |
525 | 25 | DCHECK_LT(_ann_range_search_runtime.src_col_idx, idx_to_cid.size()); |
526 | 23 | const auto src_col_idx = cast_set<int>(_ann_range_search_runtime.src_col_idx); |
527 | 23 | const auto src_col_key = cast_set<ColumnId>(_ann_range_search_runtime.src_col_idx); |
528 | 23 | auto slot_ref_map_it = common_expr_to_slotref_map.find(this); |
529 | 23 | if (slot_ref_map_it == common_expr_to_slotref_map.end()) { |
530 | 1 | return Status::OK(); |
531 | 1 | } |
532 | 22 | auto& slot_ref_map = slot_ref_map_it->second; |
533 | 22 | auto slot_ref_it = slot_ref_map.find(src_col_key); |
534 | 22 | if (slot_ref_it == slot_ref_map.end()) { |
535 | 0 | return Status::OK(); |
536 | 0 | } |
537 | 22 | const VExpr* slot_ref_expr_addr = slot_ref_it->second; |
538 | 22 | _index_context->set_true_for_index_status(slot_ref_expr_addr, src_col_idx); |
539 | | |
540 | 18.4E | VLOG_DEBUG << fmt::format( |
541 | 18.4E | "Evaluate ann range search for expr {}, src_col_idx {}, cid {}, row_bitmap " |
542 | 18.4E | "cardinality {}", |
543 | 18.4E | _root->debug_string(), src_col_idx, idx_to_cid[_ann_range_search_runtime.src_col_idx], |
544 | 18.4E | row_bitmap.cardinality()); |
545 | 22 | return Status::OK(); |
546 | 22 | } |
547 | | |
548 | 290k | uint64_t VExprContext::get_digest(uint64_t seed) const { |
549 | 290k | return _root->get_digest(seed); |
550 | 290k | } |
551 | | |
552 | 651k | double VExprContext::execute_cost() const { |
553 | 651k | if (_root == nullptr) { |
554 | | // When there is no expression root, treat the cost as a base value. |
555 | | // This avoids null dereferences while keeping a deterministic cost. |
556 | 0 | return 0.0; |
557 | 0 | } |
558 | 651k | return _root->execute_cost(); |
559 | 651k | } |
560 | | |
561 | | } // namespace doris |