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