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