/root/doris/be/src/exprs/vcompound_pred.h
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 | | #pragma once |
19 | | #include <gen_cpp/Opcodes_types.h> |
20 | | |
21 | | #include <algorithm> |
22 | | #include <cstdint> |
23 | | #include <memory> |
24 | | |
25 | | #include "common/logging.h" |
26 | | #include "common/status.h" |
27 | | #include "core/assert_cast.h" |
28 | | #include "core/column/column.h" |
29 | | #include "core/column/column_nullable.h" |
30 | | #include "exprs/vectorized_fn_call.h" |
31 | | #include "exprs/vexpr_context.h" |
32 | | #include "exprs/vexpr_fwd.h" |
33 | | #include "storage/compaction/collection_similarity.h" |
34 | | #include "storage/index/zone_map/zonemap_eval_context.h" |
35 | | #include "util/defer_op.h" |
36 | | #include "util/simd/bits.h" |
37 | | |
38 | | namespace doris { |
39 | | #include "common/compile_check_begin.h" |
40 | | |
41 | 86 | inline std::string compound_operator_to_string(TExprOpcode::type op) { |
42 | 86 | if (op == TExprOpcode::COMPOUND_AND) { |
43 | 38 | return "and"; |
44 | 48 | } else if (op == TExprOpcode::COMPOUND_OR) { |
45 | 29 | return "or"; |
46 | 29 | } else { |
47 | 19 | return "not"; |
48 | 19 | } |
49 | 86 | } |
50 | | |
51 | | class VCompoundPred : public VectorizedFnCall { |
52 | | ENABLE_FACTORY_CREATOR(VCompoundPred); |
53 | | |
54 | | public: |
55 | 86 | VCompoundPred(const TExprNode& node) : VectorizedFnCall(node) { |
56 | 86 | _op = node.opcode; |
57 | 86 | _fn.name.function_name = compound_operator_to_string(_op); |
58 | 86 | _expr_name = fmt::format("VCompoundPredicate[{}](arguments={},return={})", |
59 | 86 | _fn.name.function_name, get_child_names(), _data_type->get_name()); |
60 | 86 | } |
61 | | |
62 | | #ifdef BE_TEST |
63 | 2 | VCompoundPred() = default; |
64 | | #endif |
65 | | |
66 | 42 | const std::string& expr_name() const override { return _expr_name; } |
67 | 0 | Status clone_node(VExprSPtr* cloned_expr) const override { |
68 | 0 | DORIS_CHECK(cloned_expr != nullptr); |
69 | 0 | *cloned_expr = VCompoundPred::create_shared(clone_texpr_node()); |
70 | 0 | return Status::OK(); |
71 | 0 | } |
72 | | |
73 | | bool can_execute_on_raw_fixed_values(const DataTypePtr& data_type, |
74 | 24 | int column_id) const override { |
75 | 24 | return !_children.empty() && |
76 | 24 | (_op == TExprOpcode::COMPOUND_AND || _op == TExprOpcode::COMPOUND_OR) && |
77 | 27 | std::ranges::all_of(_children, [&](const VExprSPtr& child) { |
78 | 27 | return child->can_execute_on_raw_fixed_values(data_type, column_id); |
79 | 27 | }); |
80 | 24 | } |
81 | | |
82 | | Status execute_on_raw_fixed_values(const uint8_t* values, size_t num_values, size_t value_width, |
83 | | const DataTypePtr& data_type, int column_id, |
84 | 2 | uint8_t* matches) const override { |
85 | 2 | if (!can_execute_on_raw_fixed_values(data_type, column_id)) { |
86 | 0 | return Status::NotSupported("Compound predicate cannot evaluate raw fixed values"); |
87 | 0 | } |
88 | 2 | return _execute_raw_compound( |
89 | 4 | num_values, matches, [&](const VExprSPtr& child, uint8_t* child_matches) { |
90 | 4 | return child->execute_on_raw_fixed_values(values, num_values, value_width, |
91 | 4 | data_type, column_id, child_matches); |
92 | 4 | }); |
93 | 2 | } |
94 | | |
95 | | bool can_execute_on_raw_binary_values(const DataTypePtr& data_type, |
96 | 40 | int column_id) const override { |
97 | 40 | return !_children.empty() && |
98 | 40 | (_op == TExprOpcode::COMPOUND_AND || _op == TExprOpcode::COMPOUND_OR) && |
99 | 80 | std::ranges::all_of(_children, [&](const VExprSPtr& child) { |
100 | 80 | return child->can_execute_on_raw_binary_values(data_type, column_id); |
101 | 80 | }); |
102 | 40 | } |
103 | | |
104 | | Status execute_on_raw_binary_values(const StringRef* values, size_t num_values, |
105 | | const DataTypePtr& data_type, int column_id, |
106 | 8 | uint8_t* matches) const override { |
107 | 8 | if (!can_execute_on_raw_binary_values(data_type, column_id)) { |
108 | 0 | return Status::NotSupported("Compound predicate cannot evaluate raw binary values"); |
109 | 0 | } |
110 | 8 | return _execute_raw_compound( |
111 | 16 | num_values, matches, [&](const VExprSPtr& child, uint8_t* child_matches) { |
112 | 16 | return child->execute_on_raw_binary_values(values, num_values, data_type, |
113 | 16 | column_id, child_matches); |
114 | 16 | }); |
115 | 8 | } |
116 | | |
117 | 12 | bool raw_predicate_result_for_null() const override { |
118 | 12 | if (_op != TExprOpcode::COMPOUND_AND && _op != TExprOpcode::COMPOUND_OR) { |
119 | | // A Boolean keep bit cannot distinguish FALSE from UNKNOWN, so negating a child's |
120 | | // collapsed result is not SQL-correct. NOT remains residual and rejects NULL here. |
121 | 0 | return false; |
122 | 0 | } |
123 | 12 | if (_op == TExprOpcode::COMPOUND_AND) { |
124 | 3 | return std::ranges::all_of(_children, [](const VExprSPtr& child) { |
125 | 3 | return child->raw_predicate_result_for_null(); |
126 | 3 | }); |
127 | 3 | } |
128 | 18 | return std::ranges::any_of(_children, [](const VExprSPtr& child) { |
129 | 18 | return child->raw_predicate_result_for_null(); |
130 | 18 | }); |
131 | 12 | } |
132 | | |
133 | 70 | bool can_evaluate_zonemap_filter() const override { |
134 | 70 | switch (_op) { |
135 | 36 | case TExprOpcode::COMPOUND_AND: |
136 | 56 | return std::ranges::any_of(_children, [](const VExprSPtr& child) { |
137 | 56 | return child->can_evaluate_zonemap_filter(); |
138 | 56 | }); |
139 | 34 | case TExprOpcode::COMPOUND_OR: |
140 | 68 | return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) { |
141 | 68 | return child->can_evaluate_zonemap_filter(); |
142 | 68 | }); |
143 | 0 | case TExprOpcode::COMPOUND_NOT: |
144 | 0 | return false; |
145 | 0 | default: |
146 | 0 | return false; |
147 | 70 | } |
148 | 70 | } |
149 | | |
150 | 7 | ZoneMapFilterResult evaluate_zonemap_filter(const ZoneMapEvalContext& ctx) const override { |
151 | 7 | switch (_op) { |
152 | 2 | case TExprOpcode::COMPOUND_AND: { |
153 | 4 | for (const auto& child : _children) { |
154 | 4 | if (!child->can_evaluate_zonemap_filter()) { |
155 | 1 | continue; |
156 | 1 | } |
157 | 3 | if (child->evaluate_zonemap_filter(ctx) == ZoneMapFilterResult::kNoMatch) { |
158 | 2 | return ZoneMapFilterResult::kNoMatch; |
159 | 2 | } |
160 | 3 | } |
161 | 0 | return ZoneMapFilterResult::kMayMatch; |
162 | 2 | } |
163 | 4 | case TExprOpcode::COMPOUND_OR: { |
164 | 6 | for (const auto& child : _children) { |
165 | 6 | DORIS_CHECK(child->can_evaluate_zonemap_filter()); |
166 | 6 | if (child->evaluate_zonemap_filter(ctx) != ZoneMapFilterResult::kNoMatch) { |
167 | 3 | return ZoneMapFilterResult::kMayMatch; |
168 | 3 | } |
169 | 6 | } |
170 | 1 | return ZoneMapFilterResult::kNoMatch; |
171 | 4 | } |
172 | 1 | case TExprOpcode::COMPOUND_NOT: |
173 | 1 | return unsupported_zonemap_filter(ctx); |
174 | 0 | default: |
175 | 0 | return unsupported_zonemap_filter(ctx); |
176 | 7 | } |
177 | 7 | } |
178 | | |
179 | 33 | bool can_evaluate_dictionary_filter() const override { |
180 | 33 | switch (_op) { |
181 | 30 | case TExprOpcode::COMPOUND_AND: |
182 | 33 | return std::ranges::any_of(_children, [](const VExprSPtr& child) { |
183 | 33 | return child->can_evaluate_dictionary_filter(); |
184 | 33 | }); |
185 | 3 | case TExprOpcode::COMPOUND_OR: |
186 | 6 | return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) { |
187 | 6 | return child->can_evaluate_dictionary_filter(); |
188 | 6 | }); |
189 | 0 | default: |
190 | 0 | return false; |
191 | 33 | } |
192 | 33 | } |
193 | | |
194 | 9 | bool is_safe_to_execute_on_selected_rows() const override { |
195 | | // Boolean composition introduces no data-dependent failure of its own. Reuse the generic |
196 | | // child walk so AND/OR remain eligible only when every nested expression is independently |
197 | | // safe; applying VectorizedFnCall's scalar-function allowlist to this structural node would |
198 | | // incorrectly disable selected-row execution for otherwise safe predicates. |
199 | 9 | return VExpr::is_safe_to_execute_on_selected_rows(); |
200 | 9 | } |
201 | | |
202 | | ZoneMapFilterResult evaluate_dictionary_filter( |
203 | 21 | const DictionaryEvalContext& ctx) const override { |
204 | 21 | switch (_op) { |
205 | 21 | case TExprOpcode::COMPOUND_AND: |
206 | 29 | for (const auto& child : _children) { |
207 | 29 | if (!child->can_evaluate_dictionary_filter()) { |
208 | 8 | continue; |
209 | 8 | } |
210 | 21 | if (child->evaluate_dictionary_filter(ctx) == ZoneMapFilterResult::kNoMatch) { |
211 | 13 | return ZoneMapFilterResult::kNoMatch; |
212 | 13 | } |
213 | 21 | } |
214 | 8 | return ZoneMapFilterResult::kMayMatch; |
215 | 0 | case TExprOpcode::COMPOUND_OR: |
216 | 0 | for (const auto& child : _children) { |
217 | 0 | DORIS_CHECK(child->can_evaluate_dictionary_filter()); |
218 | 0 | if (child->evaluate_dictionary_filter(ctx) != ZoneMapFilterResult::kNoMatch) { |
219 | 0 | return ZoneMapFilterResult::kMayMatch; |
220 | 0 | } |
221 | 0 | } |
222 | 0 | return ZoneMapFilterResult::kNoMatch; |
223 | 0 | default: |
224 | 0 | return ZoneMapFilterResult::kUnsupported; |
225 | 21 | } |
226 | 21 | } |
227 | | |
228 | 14 | bool can_evaluate_bloom_filter() const override { |
229 | 14 | switch (_op) { |
230 | 9 | case TExprOpcode::COMPOUND_AND: |
231 | 15 | return std::ranges::any_of(_children, [](const VExprSPtr& child) { |
232 | 15 | return child->can_evaluate_bloom_filter(); |
233 | 15 | }); |
234 | 5 | case TExprOpcode::COMPOUND_OR: |
235 | 8 | return !_children.empty() && std::ranges::all_of(_children, [](const VExprSPtr& child) { |
236 | 8 | return child->can_evaluate_bloom_filter(); |
237 | 8 | }); |
238 | 0 | default: |
239 | 0 | return false; |
240 | 14 | } |
241 | 14 | } |
242 | | |
243 | 0 | ZoneMapFilterResult evaluate_bloom_filter(const BloomFilterEvalContext& ctx) const override { |
244 | 0 | switch (_op) { |
245 | 0 | case TExprOpcode::COMPOUND_AND: |
246 | 0 | for (const auto& child : _children) { |
247 | 0 | if (!child->can_evaluate_bloom_filter()) { |
248 | 0 | continue; |
249 | 0 | } |
250 | 0 | if (child->evaluate_bloom_filter(ctx) == ZoneMapFilterResult::kNoMatch) { |
251 | 0 | return ZoneMapFilterResult::kNoMatch; |
252 | 0 | } |
253 | 0 | } |
254 | 0 | return ZoneMapFilterResult::kMayMatch; |
255 | 0 | case TExprOpcode::COMPOUND_OR: |
256 | 0 | for (const auto& child : _children) { |
257 | 0 | DORIS_CHECK(child->can_evaluate_bloom_filter()); |
258 | 0 | if (child->evaluate_bloom_filter(ctx) != ZoneMapFilterResult::kNoMatch) { |
259 | 0 | return ZoneMapFilterResult::kMayMatch; |
260 | 0 | } |
261 | 0 | } |
262 | 0 | return ZoneMapFilterResult::kNoMatch; |
263 | 0 | default: |
264 | 0 | return ZoneMapFilterResult::kUnsupported; |
265 | 0 | } |
266 | 0 | } |
267 | | |
268 | | // NOLINTNEXTLINE(readability-function-size,readability-function-cognitive-complexity): Keep each operator's bitmap and score updates together. |
269 | 38 | Status evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) override { |
270 | 38 | segment_v2::InvertedIndexResultBitmap res; |
271 | 38 | bool all_pass = true; |
272 | 38 | auto index_context = context->get_index_context(); |
273 | 38 | auto query_context = index_context->get_index_query_context(); |
274 | 38 | auto parent_similarity = |
275 | 38 | query_context != nullptr ? query_context->collection_similarity : nullptr; |
276 | 38 | CollectionSimilarityPtr branch_similarity; |
277 | 38 | if (parent_similarity != nullptr) { |
278 | 38 | branch_similarity = std::make_shared<CollectionSimilarity>(); |
279 | 38 | query_context->collection_similarity = branch_similarity; |
280 | 38 | } |
281 | 38 | Defer restore_similarity {[&]() { |
282 | 38 | if (branch_similarity != nullptr) { |
283 | 38 | query_context->collection_similarity = parent_similarity; |
284 | 38 | } |
285 | 38 | }}; |
286 | | |
287 | 38 | switch (_op) { |
288 | 6 | case TExprOpcode::COMPOUND_OR: { |
289 | 10 | for (const auto& child : _children) { |
290 | 10 | if (Status st = child->evaluate_inverted_index(context, segment_num_rows); |
291 | 10 | !st.ok()) { |
292 | 4 | LOG(ERROR) << "expr:" << child->expr_name() |
293 | 4 | << " evaluate_inverted_index error:" << st.to_string(); |
294 | | // A scored child may have discarded its map before reporting this error. |
295 | | // Preserve the failure even when no sibling contributes scores. |
296 | 4 | if (branch_similarity != nullptr && |
297 | 4 | st.code() == ErrorCode::INVERTED_INDEX_NOT_SUPPORTED) { |
298 | 4 | return st; |
299 | 4 | } |
300 | 0 | all_pass = false; |
301 | 0 | continue; |
302 | 4 | } |
303 | 6 | auto inverted_index_context = context->get_index_context(); |
304 | 6 | if (inverted_index_context->has_index_result_for_expr(child.get())) { |
305 | 5 | const auto* index_result = |
306 | 5 | inverted_index_context->get_index_result_for_expr(child.get()); |
307 | 5 | if (res.is_empty()) { |
308 | 3 | res = *index_result; |
309 | 3 | } else { |
310 | 2 | res |= *index_result; |
311 | 2 | } |
312 | 5 | if (branch_similarity == nullptr) { |
313 | 0 | if (res.get_data_bitmap()->cardinality() == segment_num_rows) { |
314 | 0 | break; // Early exit if result is full |
315 | 0 | } |
316 | 0 | } |
317 | 5 | } else { |
318 | 1 | all_pass = false; |
319 | 1 | } |
320 | 6 | } |
321 | 2 | break; |
322 | 6 | } |
323 | 15 | case TExprOpcode::COMPOUND_AND: { |
324 | 28 | for (const auto& child : _children) { |
325 | 28 | if (Status st = child->evaluate_inverted_index(context, segment_num_rows); |
326 | 28 | !st.ok()) { |
327 | 4 | LOG(ERROR) << "expr:" << child->expr_name() |
328 | 4 | << " evaluate_inverted_index error:" << st.to_string(); |
329 | | // A scored child may have discarded its map before reporting this error. |
330 | | // Preserve the failure even when no sibling contributes scores. |
331 | 4 | if (branch_similarity != nullptr && |
332 | 4 | st.code() == ErrorCode::INVERTED_INDEX_NOT_SUPPORTED) { |
333 | 4 | return st; |
334 | 4 | } |
335 | 0 | all_pass = false; |
336 | 0 | continue; |
337 | 4 | } |
338 | 24 | if (context->get_index_context()->has_index_result_for_expr(child.get())) { |
339 | 14 | const auto* index_result = |
340 | 14 | context->get_index_context()->get_index_result_for_expr(child.get()); |
341 | 14 | if (res.is_empty()) { |
342 | 12 | res = *index_result; |
343 | 12 | } else { |
344 | 2 | res &= *index_result; |
345 | 2 | } |
346 | | |
347 | 14 | if (res.get_data_bitmap()->isEmpty()) { |
348 | 0 | break; // Early exit if result is empty |
349 | 0 | } |
350 | 14 | } else { |
351 | 10 | all_pass = false; |
352 | 10 | } |
353 | 24 | } |
354 | 11 | break; |
355 | 15 | } |
356 | 17 | case TExprOpcode::COMPOUND_NOT: { |
357 | 17 | const auto& child = _children[0]; |
358 | 17 | Status st = child->evaluate_inverted_index(context, segment_num_rows); |
359 | 17 | if (!st.ok()) { |
360 | 16 | LOG(ERROR) << "expr:" << child->expr_name() |
361 | 16 | << " evaluate_inverted_index error:" << st.to_string(); |
362 | 16 | return st; |
363 | 16 | } |
364 | | |
365 | 1 | if (context->get_index_context()->has_index_result_for_expr(child.get())) { |
366 | 1 | const auto* index_result = |
367 | 1 | context->get_index_context()->get_index_result_for_expr(child.get()); |
368 | 1 | roaring::Roaring full_result; |
369 | 1 | full_result.addRange(0, segment_num_rows); |
370 | 1 | res = index_result->op_not(&full_result); |
371 | 1 | } else { |
372 | 0 | all_pass = false; |
373 | 0 | } |
374 | 1 | break; |
375 | 17 | } |
376 | 0 | default: |
377 | 0 | return Status::NotSupported( |
378 | 0 | "Compound operator must be AND, OR, or NOT to execute with inverted index."); |
379 | 38 | } |
380 | | |
381 | 14 | if (branch_similarity != nullptr && _op != TExprOpcode::COMPOUND_NOT) { |
382 | 13 | auto scores = branch_similarity->release_scores(); |
383 | 13 | if (!scores.empty() && (!all_pass || res.is_empty())) { |
384 | 9 | return Status::Error<ErrorCode::INVERTED_INDEX_NOT_SUPPORTED>( |
385 | 9 | "Cannot score a compound predicate with incomplete index evaluation"); |
386 | 9 | } |
387 | 4 | if (all_pass && !res.is_empty()) { |
388 | 4 | DORIS_CHECK(res.get_data_bitmap() != nullptr); |
389 | 4 | parent_similarity->merge_scores(scores, *res.get_data_bitmap()); |
390 | 4 | } |
391 | 4 | } |
392 | 5 | if (all_pass && !res.is_empty()) { |
393 | 5 | index_context->set_index_result_for_expr(this, res); |
394 | 5 | } |
395 | 5 | return Status::OK(); |
396 | 14 | } |
397 | | |
398 | | Status execute_column(VExprContext* context, const Block* block, Selector* selector, |
399 | 4 | size_t count, ColumnPtr& result_column) const override { |
400 | 4 | if (fast_execute(context, selector, count, result_column)) { |
401 | 0 | return Status::OK(); |
402 | 0 | } |
403 | 4 | if (get_num_children() == 1 || _has_const_child()) { |
404 | 0 | return VectorizedFnCall::execute_column(context, block, selector, count, result_column); |
405 | 0 | } |
406 | | |
407 | 4 | ColumnPtr lhs_column; |
408 | 4 | RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, lhs_column)); |
409 | 4 | lhs_column = lhs_column->convert_to_full_column_if_const(); |
410 | 4 | size_t size = lhs_column->size(); |
411 | | |
412 | 4 | bool lhs_is_nullable = lhs_column->is_nullable(); |
413 | 4 | auto [lhs_data_column, lhs_null_map] = |
414 | 4 | _get_raw_data_and_null_map(lhs_column, lhs_is_nullable); |
415 | 4 | size_t filted = simd::count_zero_num((int8_t*)lhs_data_column, size); |
416 | 4 | bool lhs_all_true = (filted == 0); |
417 | 4 | bool lhs_all_false = (filted == size); |
418 | | |
419 | 4 | bool lhs_all_is_not_null = false; |
420 | 4 | if (lhs_is_nullable) { |
421 | 4 | filted = simd::count_zero_num((int8_t*)lhs_null_map, size); |
422 | 4 | lhs_all_is_not_null = (filted == size); |
423 | 4 | } |
424 | | |
425 | 4 | ColumnPtr rhs_column = nullptr; |
426 | 4 | const uint8_t* __restrict rhs_data_column = nullptr; |
427 | 4 | const uint8_t* __restrict rhs_null_map = nullptr; |
428 | 4 | bool rhs_is_nullable = false; |
429 | 4 | bool rhs_all_true = false; |
430 | 4 | bool rhs_all_false = false; |
431 | 4 | bool rhs_all_is_not_null = false; |
432 | 4 | bool result_is_nullable = _data_type->is_nullable(); |
433 | | |
434 | 4 | auto get_rhs_colum = [&]() { |
435 | 4 | if (!rhs_column) { |
436 | 4 | RETURN_IF_ERROR( |
437 | 4 | _children[1]->execute_column(context, block, selector, count, rhs_column)); |
438 | 3 | rhs_column = rhs_column->convert_to_full_column_if_const(); |
439 | 3 | rhs_is_nullable = rhs_column->is_nullable(); |
440 | 3 | auto rhs_nullable_column = _get_raw_data_and_null_map(rhs_column, rhs_is_nullable); |
441 | 3 | rhs_data_column = rhs_nullable_column.first; |
442 | 3 | rhs_null_map = rhs_nullable_column.second; |
443 | 3 | size_t filted = simd::count_zero_num((int8_t*)rhs_data_column, size); |
444 | 3 | rhs_all_true = (filted == 0); |
445 | 3 | rhs_all_false = (filted == size); |
446 | 3 | if (rhs_is_nullable) { |
447 | 3 | filted = simd::count_zero_num((int8_t*)rhs_null_map, size); |
448 | 3 | rhs_all_is_not_null = (filted == size); |
449 | 3 | } |
450 | 3 | } |
451 | 3 | return Status::OK(); |
452 | 4 | }; |
453 | | |
454 | 4 | auto return_result_column_id = [&](ColumnPtr& arg_column) { |
455 | 2 | result_column = std::move(*arg_column).mutate(); |
456 | 2 | if (result_is_nullable && !result_column->is_nullable()) { |
457 | 0 | result_column = make_nullable(result_column); |
458 | 0 | } |
459 | 2 | }; |
460 | | |
461 | 4 | auto create_null_map_column = [&](ColumnPtr& null_map_column, |
462 | 4 | const uint8_t* __restrict null_map_data) { |
463 | 2 | if (null_map_data == nullptr) { |
464 | 0 | null_map_column = ColumnUInt8::create(size, 0); |
465 | 0 | null_map_data = |
466 | 0 | assert_cast<const ColumnUInt8*>(null_map_column.get())->get_data().data(); |
467 | 0 | } |
468 | 2 | return null_map_data; |
469 | 2 | }; |
470 | | |
471 | 4 | auto vector_vector = [&]<bool is_and_op>() { |
472 | 0 | MutableColumnPtr mutable_result_column; |
473 | 0 | uint8_t* __restrict result_data_column = nullptr; |
474 | 0 | const uint8_t* __restrict other_data_column = rhs_data_column; |
475 | 0 | if (lhs_column->use_count() == 1) { |
476 | 0 | mutable_result_column = IColumn::mutate(std::move(lhs_column)); |
477 | 0 | result_data_column = |
478 | 0 | assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data(); |
479 | 0 | } else if (rhs_column->use_count() == 1) { |
480 | 0 | mutable_result_column = IColumn::mutate(std::move(rhs_column)); |
481 | 0 | result_data_column = |
482 | 0 | assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data(); |
483 | 0 | other_data_column = lhs_data_column; |
484 | 0 | } else { |
485 | 0 | mutable_result_column = lhs_column->clone_resized(size); |
486 | 0 | result_data_column = |
487 | 0 | assert_cast<ColumnUInt8*>(mutable_result_column.get())->get_data().data(); |
488 | 0 | } |
489 | |
|
490 | 0 | do_not_null_pred<is_and_op>(result_data_column, other_data_column, size); |
491 | 0 | result_column = std::move(mutable_result_column); |
492 | 0 | }; Unexecuted instantiation: _ZZNK5doris13VCompoundPred14execute_columnEPNS_12VExprContextEPKNS_5BlockEPNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISD_EEENKUlTnbvE_clILb1EEEDav Unexecuted instantiation: _ZZNK5doris13VCompoundPred14execute_columnEPNS_12VExprContextEPKNS_5BlockEPNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISD_EEENKUlTnbvE_clILb0EEEDav |
493 | 4 | auto vector_vector_null = [&]<bool is_and_op>() { |
494 | 1 | auto col_res = ColumnUInt8::create(size); |
495 | 1 | auto col_nulls = ColumnUInt8::create(size); |
496 | | |
497 | 1 | auto* __restrict res_datas = col_res->get_data().data(); |
498 | 1 | auto* __restrict res_nulls = col_nulls->get_data().data(); |
499 | 1 | ColumnPtr temp_null_map = nullptr; |
500 | | // maybe both children are nullable / or one of children is nullable |
501 | 1 | auto* __restrict lhs_null_map_tmp = create_null_map_column(temp_null_map, lhs_null_map); |
502 | 1 | auto* __restrict rhs_null_map_tmp = create_null_map_column(temp_null_map, rhs_null_map); |
503 | 1 | auto* __restrict lhs_data_column_tmp = lhs_data_column; |
504 | 1 | auto* __restrict rhs_data_column_tmp = rhs_data_column; |
505 | | |
506 | 1 | do_null_pred<is_and_op>(lhs_data_column_tmp, lhs_null_map_tmp, rhs_data_column_tmp, |
507 | 1 | rhs_null_map_tmp, res_datas, res_nulls, size); |
508 | | |
509 | 1 | result_column = ColumnNullable::create(std::move(col_res), std::move(col_nulls)); |
510 | 1 | }; Unexecuted instantiation: _ZZNK5doris13VCompoundPred14execute_columnEPNS_12VExprContextEPKNS_5BlockEPNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISD_EEENKUlTnbvE0_clILb1EEEDav _ZZNK5doris13VCompoundPred14execute_columnEPNS_12VExprContextEPKNS_5BlockEPNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISD_EEENKUlTnbvE0_clILb0EEEDav Line | Count | Source | 493 | 1 | auto vector_vector_null = [&]<bool is_and_op>() { | 494 | 1 | auto col_res = ColumnUInt8::create(size); | 495 | 1 | auto col_nulls = ColumnUInt8::create(size); | 496 | | | 497 | 1 | auto* __restrict res_datas = col_res->get_data().data(); | 498 | 1 | auto* __restrict res_nulls = col_nulls->get_data().data(); | 499 | 1 | ColumnPtr temp_null_map = nullptr; | 500 | | // maybe both children are nullable / or one of children is nullable | 501 | 1 | auto* __restrict lhs_null_map_tmp = create_null_map_column(temp_null_map, lhs_null_map); | 502 | 1 | auto* __restrict rhs_null_map_tmp = create_null_map_column(temp_null_map, rhs_null_map); | 503 | 1 | auto* __restrict lhs_data_column_tmp = lhs_data_column; | 504 | 1 | auto* __restrict rhs_data_column_tmp = rhs_data_column; | 505 | | | 506 | 1 | do_null_pred<is_and_op>(lhs_data_column_tmp, lhs_null_map_tmp, rhs_data_column_tmp, | 507 | 1 | rhs_null_map_tmp, res_datas, res_nulls, size); | 508 | | | 509 | 1 | result_column = ColumnNullable::create(std::move(col_res), std::move(col_nulls)); | 510 | 1 | }; |
|
511 | | |
512 | | // false and NULL ----> 0 |
513 | | // true and NULL ----> NULL |
514 | 4 | if (_op == TExprOpcode::COMPOUND_AND) { |
515 | | //1. not null column: all data is false |
516 | | //2. nullable column: null map all is not null |
517 | 1 | if ((lhs_all_false && !lhs_is_nullable) || (lhs_all_false && lhs_all_is_not_null)) { |
518 | | // false and any = false, return lhs |
519 | 0 | return_result_column_id(lhs_column); |
520 | 1 | } else { |
521 | 1 | RETURN_IF_ERROR(get_rhs_colum()); |
522 | | |
523 | 0 | if ((lhs_all_true && !lhs_is_nullable) || //not null column |
524 | 0 | (lhs_all_true && lhs_all_is_not_null)) { //nullable column |
525 | | // true and any = any, return rhs |
526 | |
|
527 | 0 | return_result_column_id(rhs_column); |
528 | 0 | } else if ((rhs_all_false && !rhs_is_nullable) || |
529 | 0 | (rhs_all_false && rhs_all_is_not_null)) { |
530 | | // any and false = false, return rhs |
531 | 0 | return_result_column_id(rhs_column); |
532 | 0 | } else if ((rhs_all_true && !rhs_is_nullable) || |
533 | 0 | (rhs_all_true && rhs_all_is_not_null)) { |
534 | | // any and true = any, return lhs |
535 | 0 | return_result_column_id(lhs_column); |
536 | 0 | } else { |
537 | 0 | if (!result_is_nullable) { |
538 | 0 | vector_vector.template operator()<true>(); |
539 | 0 | } else { |
540 | 0 | vector_vector_null.template operator()<true>(); |
541 | 0 | } |
542 | 0 | } |
543 | 0 | } |
544 | 3 | } else if (_op == TExprOpcode::COMPOUND_OR) { |
545 | | // true or NULL ----> 1 |
546 | | // false or NULL ----> NULL |
547 | 3 | if ((lhs_all_true && !lhs_is_nullable) || (lhs_all_true && lhs_all_is_not_null)) { |
548 | | // true or any = true, return lhs |
549 | 0 | return_result_column_id(lhs_column); |
550 | 3 | } else { |
551 | 3 | RETURN_IF_ERROR(get_rhs_colum()); |
552 | 3 | if ((lhs_all_false && !lhs_is_nullable) || (lhs_all_false && lhs_all_is_not_null)) { |
553 | | // false or any = any, return rhs |
554 | 1 | return_result_column_id(rhs_column); |
555 | 2 | } else if ((rhs_all_true && !rhs_is_nullable) || |
556 | 2 | (rhs_all_true && rhs_all_is_not_null)) { |
557 | | // any or true = true, return rhs |
558 | 0 | return_result_column_id(rhs_column); |
559 | 2 | } else if ((rhs_all_false && !rhs_is_nullable) || |
560 | 2 | (rhs_all_false && rhs_all_is_not_null)) { |
561 | | // any or false = any, return lhs |
562 | 1 | return_result_column_id(lhs_column); |
563 | 1 | } else { |
564 | 1 | if (!result_is_nullable) { |
565 | 0 | vector_vector.template operator()<false>(); |
566 | 1 | } else { |
567 | 1 | vector_vector_null.template operator()<false>(); |
568 | 1 | } |
569 | 1 | } |
570 | 3 | } |
571 | 3 | } else { |
572 | 0 | return Status::InternalError("Compound operator must be AND or OR."); |
573 | 0 | } |
574 | | |
575 | 4 | DCHECK_EQ(result_column->size(), count); |
576 | 3 | return Status::OK(); |
577 | 4 | } |
578 | | |
579 | 0 | double execute_cost() const override { |
580 | 0 | double cost = 0.3; |
581 | 0 | for (const auto& child : _children) { |
582 | 0 | cost += child->execute_cost(); |
583 | 0 | } |
584 | 0 | return cost; |
585 | 0 | } |
586 | | |
587 | | private: |
588 | | template <typename ExecuteChild> |
589 | | Status _execute_raw_compound(size_t num_values, uint8_t* matches, |
590 | 10 | ExecuteChild&& execute_child) const { |
591 | 10 | if (_op == TExprOpcode::COMPOUND_AND) { |
592 | 2 | for (const auto& child : _children) { |
593 | 2 | RETURN_IF_ERROR(execute_child(child, matches)); |
594 | 2 | } |
595 | 1 | return Status::OK(); |
596 | 1 | } |
597 | | |
598 | | // Each execution context owns its expression tree. Retaining masks on the OR node avoids |
599 | | // N+1 row-sized allocations for every decoder fragment, while nested OR nodes keep |
600 | | // independent buffers and therefore cannot overwrite their parent's in-flight state. |
601 | 9 | _raw_combined_scratch.resize(num_values); |
602 | 9 | std::ranges::fill(_raw_combined_scratch, 0); |
603 | 18 | for (const auto& child : _children) { |
604 | | // resize_fill() initializes only newly appended bytes; explicitly reset a reused mask |
605 | | // so matches from an earlier page fragment cannot leak into this OR evaluation. |
606 | 18 | _raw_child_scratch.resize(num_values); |
607 | 18 | std::ranges::fill(_raw_child_scratch, 1); |
608 | 18 | RETURN_IF_ERROR(execute_child(child, _raw_child_scratch.data())); |
609 | 154 | for (size_t row = 0; row < num_values; ++row) { |
610 | 136 | _raw_combined_scratch[row] |= _raw_child_scratch[row]; |
611 | 136 | } |
612 | 18 | } |
613 | | // Raw kernels receive an existing selection mask, so composition must preserve rows that |
614 | | // an earlier conjunct already rejected instead of replacing the caller's mask. |
615 | 77 | for (size_t row = 0; row < num_values; ++row) { |
616 | 68 | matches[row] &= _raw_combined_scratch[row]; |
617 | 68 | } |
618 | 9 | constexpr size_t MAX_RETAINED_RAW_MASK_BYTES = 1UL << 20; |
619 | 9 | if (_raw_combined_scratch.capacity() > MAX_RETAINED_RAW_MASK_BYTES) { |
620 | 0 | IColumn::Filter().swap(_raw_combined_scratch); |
621 | 0 | } |
622 | 9 | if (_raw_child_scratch.capacity() > MAX_RETAINED_RAW_MASK_BYTES) { |
623 | 0 | IColumn::Filter().swap(_raw_child_scratch); |
624 | 0 | } |
625 | 9 | return Status::OK(); |
626 | 9 | } _ZNK5doris13VCompoundPred21_execute_raw_compoundIZNKS0_27execute_on_raw_fixed_valuesEPKhmmRKSt10shared_ptrIKNS_9IDataTypeEEiPhEUlRKS4_INS_5VExprEESA_E_EENS_6StatusEmSA_OT_ Line | Count | Source | 590 | 2 | ExecuteChild&& execute_child) const { | 591 | 2 | if (_op == TExprOpcode::COMPOUND_AND) { | 592 | 2 | for (const auto& child : _children) { | 593 | 2 | RETURN_IF_ERROR(execute_child(child, matches)); | 594 | 2 | } | 595 | 1 | return Status::OK(); | 596 | 1 | } | 597 | | | 598 | | // Each execution context owns its expression tree. Retaining masks on the OR node avoids | 599 | | // N+1 row-sized allocations for every decoder fragment, while nested OR nodes keep | 600 | | // independent buffers and therefore cannot overwrite their parent's in-flight state. | 601 | 1 | _raw_combined_scratch.resize(num_values); | 602 | 1 | std::ranges::fill(_raw_combined_scratch, 0); | 603 | 2 | for (const auto& child : _children) { | 604 | | // resize_fill() initializes only newly appended bytes; explicitly reset a reused mask | 605 | | // so matches from an earlier page fragment cannot leak into this OR evaluation. | 606 | 2 | _raw_child_scratch.resize(num_values); | 607 | 2 | std::ranges::fill(_raw_child_scratch, 1); | 608 | 2 | RETURN_IF_ERROR(execute_child(child, _raw_child_scratch.data())); | 609 | 10 | for (size_t row = 0; row < num_values; ++row) { | 610 | 8 | _raw_combined_scratch[row] |= _raw_child_scratch[row]; | 611 | 8 | } | 612 | 2 | } | 613 | | // Raw kernels receive an existing selection mask, so composition must preserve rows that | 614 | | // an earlier conjunct already rejected instead of replacing the caller's mask. | 615 | 5 | for (size_t row = 0; row < num_values; ++row) { | 616 | 4 | matches[row] &= _raw_combined_scratch[row]; | 617 | 4 | } | 618 | 1 | constexpr size_t MAX_RETAINED_RAW_MASK_BYTES = 1UL << 20; | 619 | 1 | if (_raw_combined_scratch.capacity() > MAX_RETAINED_RAW_MASK_BYTES) { | 620 | 0 | IColumn::Filter().swap(_raw_combined_scratch); | 621 | 0 | } | 622 | 1 | if (_raw_child_scratch.capacity() > MAX_RETAINED_RAW_MASK_BYTES) { | 623 | 0 | IColumn::Filter().swap(_raw_child_scratch); | 624 | 0 | } | 625 | 1 | return Status::OK(); | 626 | 1 | } |
_ZNK5doris13VCompoundPred21_execute_raw_compoundIZNKS0_28execute_on_raw_binary_valuesEPKNS_9StringRefEmRKSt10shared_ptrIKNS_9IDataTypeEEiPhEUlRKS5_INS_5VExprEESB_E_EENS_6StatusEmSB_OT_ Line | Count | Source | 590 | 8 | ExecuteChild&& execute_child) const { | 591 | 8 | if (_op == TExprOpcode::COMPOUND_AND) { | 592 | 0 | for (const auto& child : _children) { | 593 | 0 | RETURN_IF_ERROR(execute_child(child, matches)); | 594 | 0 | } | 595 | 0 | return Status::OK(); | 596 | 0 | } | 597 | | | 598 | | // Each execution context owns its expression tree. Retaining masks on the OR node avoids | 599 | | // N+1 row-sized allocations for every decoder fragment, while nested OR nodes keep | 600 | | // independent buffers and therefore cannot overwrite their parent's in-flight state. | 601 | 8 | _raw_combined_scratch.resize(num_values); | 602 | 8 | std::ranges::fill(_raw_combined_scratch, 0); | 603 | 16 | for (const auto& child : _children) { | 604 | | // resize_fill() initializes only newly appended bytes; explicitly reset a reused mask | 605 | | // so matches from an earlier page fragment cannot leak into this OR evaluation. | 606 | 16 | _raw_child_scratch.resize(num_values); | 607 | 16 | std::ranges::fill(_raw_child_scratch, 1); | 608 | 16 | RETURN_IF_ERROR(execute_child(child, _raw_child_scratch.data())); | 609 | 144 | for (size_t row = 0; row < num_values; ++row) { | 610 | 128 | _raw_combined_scratch[row] |= _raw_child_scratch[row]; | 611 | 128 | } | 612 | 16 | } | 613 | | // Raw kernels receive an existing selection mask, so composition must preserve rows that | 614 | | // an earlier conjunct already rejected instead of replacing the caller's mask. | 615 | 72 | for (size_t row = 0; row < num_values; ++row) { | 616 | 64 | matches[row] &= _raw_combined_scratch[row]; | 617 | 64 | } | 618 | 8 | constexpr size_t MAX_RETAINED_RAW_MASK_BYTES = 1UL << 20; | 619 | 8 | if (_raw_combined_scratch.capacity() > MAX_RETAINED_RAW_MASK_BYTES) { | 620 | 0 | IColumn::Filter().swap(_raw_combined_scratch); | 621 | 0 | } | 622 | 8 | if (_raw_child_scratch.capacity() > MAX_RETAINED_RAW_MASK_BYTES) { | 623 | 0 | IColumn::Filter().swap(_raw_child_scratch); | 624 | 0 | } | 625 | 8 | return Status::OK(); | 626 | 8 | } |
|
627 | | |
628 | | mutable IColumn::Filter _raw_combined_scratch; |
629 | | mutable IColumn::Filter _raw_child_scratch; |
630 | | |
631 | 0 | static inline constexpr uint8_t apply_and_null(UInt8 a, UInt8 l_null, UInt8 b, UInt8 r_null) { |
632 | | // (<> && false) is false, (true && NULL) is NULL |
633 | 0 | return (l_null & r_null) | (r_null & (l_null ^ a)) | (l_null & (r_null ^ b)); |
634 | 0 | } |
635 | 5 | static inline constexpr uint8_t apply_or_null(UInt8 a, UInt8 l_null, UInt8 b, UInt8 r_null) { |
636 | | // (<> || true) is true, (false || NULL) is NULL |
637 | 5 | return (l_null & r_null) | (r_null & (r_null ^ a)) | (l_null & (l_null ^ b)); |
638 | 5 | } |
639 | | |
640 | | template <bool is_and> |
641 | | void static do_not_null_pred(uint8_t* __restrict lhs, const uint8_t* __restrict rhs, |
642 | 0 | size_t size) { |
643 | | #ifdef NDEBUG |
644 | | #if defined(__clang__) |
645 | | #pragma clang loop vectorize(enable) |
646 | | #elif defined(__GNUC__) && (__GNUC__ >= 5) |
647 | | #pragma GCC ivdep |
648 | | #endif |
649 | | #endif |
650 | 0 | for (size_t i = 0; i < size; ++i) { |
651 | 0 | if constexpr (is_and) { |
652 | 0 | lhs[i] &= rhs[i]; |
653 | 0 | } else { |
654 | | // Logical OR must produce a canonical Boolean instead of preserving input bits. |
655 | 0 | lhs[i] = (lhs[i] | rhs[i]) != 0; |
656 | 0 | } |
657 | 0 | } |
658 | 0 | } Unexecuted instantiation: _ZN5doris13VCompoundPred16do_not_null_predILb1EEEvPhPKhm Unexecuted instantiation: _ZN5doris13VCompoundPred16do_not_null_predILb0EEEvPhPKhm |
659 | | |
660 | | template <bool is_and> |
661 | | void static do_null_pred(const uint8_t* __restrict lhs_data, const uint8_t* __restrict lhs_null, |
662 | | const uint8_t* __restrict rhs_data, const uint8_t* __restrict rhs_null, |
663 | | uint8_t* __restrict res_data, uint8_t* __restrict res_null, |
664 | 1 | size_t size) { |
665 | | #ifdef NDEBUG |
666 | | #if defined(__clang__) |
667 | | #pragma clang loop vectorize(enable) |
668 | | #elif defined(__GNUC__) && (__GNUC__ >= 5) |
669 | | #pragma GCC ivdep |
670 | | #endif |
671 | | #endif |
672 | 6 | for (size_t i = 0; i < size; ++i) { |
673 | 5 | if constexpr (is_and) { |
674 | 0 | res_null[i] = apply_and_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]); |
675 | 0 | res_data[i] = lhs_data[i] & rhs_data[i]; |
676 | 5 | } else { |
677 | 5 | res_null[i] = apply_or_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]); |
678 | | // A NULL row may carry an arbitrary nested byte. If the result remains NULL the |
679 | | // byte is ignored; otherwise normalization prevents it from becoming visible. |
680 | 5 | res_data[i] = (lhs_data[i] | rhs_data[i]) != 0; |
681 | 5 | } |
682 | 5 | } |
683 | 1 | } Unexecuted instantiation: _ZN5doris13VCompoundPred12do_null_predILb1EEEvPKhS3_S3_S3_PhS4_m _ZN5doris13VCompoundPred12do_null_predILb0EEEvPKhS3_S3_S3_PhS4_m Line | Count | Source | 664 | 1 | size_t size) { | 665 | | #ifdef NDEBUG | 666 | | #if defined(__clang__) | 667 | | #pragma clang loop vectorize(enable) | 668 | | #elif defined(__GNUC__) && (__GNUC__ >= 5) | 669 | | #pragma GCC ivdep | 670 | | #endif | 671 | | #endif | 672 | 6 | for (size_t i = 0; i < size; ++i) { | 673 | | if constexpr (is_and) { | 674 | | res_null[i] = apply_and_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]); | 675 | | res_data[i] = lhs_data[i] & rhs_data[i]; | 676 | 5 | } else { | 677 | 5 | res_null[i] = apply_or_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]); | 678 | | // A NULL row may carry an arbitrary nested byte. If the result remains NULL the | 679 | | // byte is ignored; otherwise normalization prevents it from becoming visible. | 680 | 5 | res_data[i] = (lhs_data[i] | rhs_data[i]) != 0; | 681 | 5 | } | 682 | 5 | } | 683 | 1 | } |
|
684 | | |
685 | 4 | bool _has_const_child() const { |
686 | 4 | return std::ranges::any_of(_children, |
687 | 8 | [](const VExprSPtr& arg) -> bool { return arg->is_constant(); }); |
688 | 4 | } |
689 | | |
690 | | std::pair<const uint8_t*, const uint8_t*> _get_raw_data_and_null_map( |
691 | 7 | const ColumnPtr& column, bool has_nullable_column) const { |
692 | 7 | if (has_nullable_column) { |
693 | 7 | const auto* nullable_column = assert_cast<const ColumnNullable*>(column.get()); |
694 | 7 | auto* data_column = |
695 | 7 | assert_cast<const ColumnUInt8*>(nullable_column->get_nested_column_ptr().get()) |
696 | 7 | ->get_data() |
697 | 7 | .data(); |
698 | 7 | auto* null_map = nullable_column->get_null_map_column_ptr()->get_data().data(); |
699 | 7 | return std::make_pair(data_column, null_map); |
700 | 7 | } else { |
701 | 0 | auto* data_column = assert_cast<const ColumnUInt8*>(column.get())->get_data().data(); |
702 | 0 | return std::make_pair(data_column, nullptr); |
703 | 0 | } |
704 | 7 | } |
705 | | |
706 | | TExprOpcode::type _op; |
707 | | }; |
708 | | |
709 | | #include "common/compile_check_end.h" |
710 | | } // namespace doris |