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