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