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