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/status.h" |
25 | | #include "core/assert_cast.h" |
26 | | #include "core/column/column.h" |
27 | | #include "core/column/column_nullable.h" |
28 | | #include "exprs/vectorized_fn_call.h" |
29 | | #include "exprs/vexpr_context.h" |
30 | | #include "exprs/vexpr_fwd.h" |
31 | | #include "util/simd/bits.h" |
32 | | |
33 | | namespace doris { |
34 | | |
35 | 17 | inline std::string compound_operator_to_string(TExprOpcode::type op) { |
36 | 17 | if (op == TExprOpcode::COMPOUND_AND) { |
37 | 8 | return "and"; |
38 | 9 | } else if (op == TExprOpcode::COMPOUND_OR) { |
39 | 9 | return "or"; |
40 | 9 | } else { |
41 | 0 | return "not"; |
42 | 0 | } |
43 | 17 | } |
44 | | |
45 | | class VCompoundPred : public VectorizedFnCall { |
46 | | ENABLE_FACTORY_CREATOR(VCompoundPred); |
47 | | |
48 | | public: |
49 | 17 | VCompoundPred(const TExprNode& node) : VectorizedFnCall(node) { |
50 | 17 | _op = node.opcode; |
51 | 17 | _fn.name.function_name = compound_operator_to_string(_op); |
52 | 17 | _expr_name = fmt::format("VCompoundPredicate[{}](arguments={},return={})", |
53 | 17 | _fn.name.function_name, get_child_names(), _data_type->get_name()); |
54 | 17 | } |
55 | | |
56 | | #ifdef BE_TEST |
57 | 2 | VCompoundPred() = default; |
58 | | #endif |
59 | | |
60 | 18 | const std::string& expr_name() const override { return _expr_name; } |
61 | | |
62 | 0 | Status evaluate_inverted_index(VExprContext* context, uint32_t segment_num_rows) override { |
63 | 0 | segment_v2::InvertedIndexResultBitmap res; |
64 | 0 | bool all_pass = true; |
65 | |
|
66 | 0 | switch (_op) { |
67 | 0 | case TExprOpcode::COMPOUND_OR: { |
68 | 0 | for (const auto& child : _children) { |
69 | 0 | if (Status st = child->evaluate_inverted_index(context, segment_num_rows); |
70 | 0 | !st.ok()) { |
71 | 0 | LOG(ERROR) << "expr:" << child->expr_name() |
72 | 0 | << " evaluate_inverted_index error:" << st.to_string(); |
73 | 0 | all_pass = false; |
74 | 0 | continue; |
75 | 0 | } |
76 | 0 | auto inverted_index_context = context->get_index_context(); |
77 | 0 | if (inverted_index_context->has_index_result_for_expr(child.get())) { |
78 | 0 | const auto* index_result = |
79 | 0 | inverted_index_context->get_index_result_for_expr(child.get()); |
80 | 0 | if (res.is_empty()) { |
81 | 0 | res = *index_result; |
82 | 0 | } else { |
83 | 0 | res |= *index_result; |
84 | 0 | } |
85 | 0 | if (inverted_index_context->get_score_runtime() == nullptr) { |
86 | 0 | if (res.get_data_bitmap()->cardinality() == segment_num_rows) { |
87 | 0 | break; // Early exit if result is full |
88 | 0 | } |
89 | 0 | } |
90 | 0 | } else { |
91 | 0 | all_pass = false; |
92 | 0 | } |
93 | 0 | } |
94 | 0 | break; |
95 | 0 | } |
96 | 0 | case TExprOpcode::COMPOUND_AND: { |
97 | 0 | for (const auto& child : _children) { |
98 | 0 | if (Status st = child->evaluate_inverted_index(context, segment_num_rows); |
99 | 0 | !st.ok()) { |
100 | 0 | LOG(ERROR) << "expr:" << child->expr_name() |
101 | 0 | << " evaluate_inverted_index error:" << st.to_string(); |
102 | 0 | all_pass = false; |
103 | 0 | continue; |
104 | 0 | } |
105 | 0 | if (context->get_index_context()->has_index_result_for_expr(child.get())) { |
106 | 0 | const auto* index_result = |
107 | 0 | context->get_index_context()->get_index_result_for_expr(child.get()); |
108 | 0 | if (res.is_empty()) { |
109 | 0 | res = *index_result; |
110 | 0 | } else { |
111 | 0 | res &= *index_result; |
112 | 0 | } |
113 | |
|
114 | 0 | if (res.get_data_bitmap()->isEmpty()) { |
115 | 0 | break; // Early exit if result is empty |
116 | 0 | } |
117 | 0 | } else { |
118 | 0 | all_pass = false; |
119 | 0 | } |
120 | 0 | } |
121 | 0 | break; |
122 | 0 | } |
123 | 0 | case TExprOpcode::COMPOUND_NOT: { |
124 | 0 | const auto& child = _children[0]; |
125 | 0 | Status st = child->evaluate_inverted_index(context, segment_num_rows); |
126 | 0 | if (!st.ok()) { |
127 | 0 | LOG(ERROR) << "expr:" << child->expr_name() |
128 | 0 | << " evaluate_inverted_index error:" << st.to_string(); |
129 | 0 | return st; |
130 | 0 | } |
131 | | |
132 | 0 | if (context->get_index_context()->has_index_result_for_expr(child.get())) { |
133 | 0 | const auto* index_result = |
134 | 0 | context->get_index_context()->get_index_result_for_expr(child.get()); |
135 | 0 | roaring::Roaring full_result; |
136 | 0 | full_result.addRange(0, segment_num_rows); |
137 | 0 | res = index_result->op_not(&full_result); |
138 | 0 | } else { |
139 | 0 | all_pass = false; |
140 | 0 | } |
141 | 0 | break; |
142 | 0 | } |
143 | 0 | default: |
144 | 0 | return Status::NotSupported( |
145 | 0 | "Compound operator must be AND, OR, or NOT to execute with inverted index."); |
146 | 0 | } |
147 | | |
148 | 0 | if (all_pass && !res.is_empty()) { |
149 | 0 | context->get_index_context()->set_index_result_for_expr(this, res); |
150 | 0 | } |
151 | 0 | return Status::OK(); |
152 | 0 | } |
153 | | |
154 | | Status execute_column(VExprContext* context, const Block* block, Selector* selector, |
155 | 0 | size_t count, ColumnPtr& result_column) const override { |
156 | 0 | if (fast_execute(context, selector, count, result_column)) { |
157 | 0 | return Status::OK(); |
158 | 0 | } |
159 | 0 | if (get_num_children() == 1 || _has_const_child()) { |
160 | 0 | return VectorizedFnCall::execute_column(context, block, selector, count, result_column); |
161 | 0 | } |
162 | | |
163 | 0 | ColumnPtr lhs_column; |
164 | 0 | RETURN_IF_ERROR(_children[0]->execute_column(context, block, selector, count, lhs_column)); |
165 | 0 | lhs_column = lhs_column->convert_to_full_column_if_const(); |
166 | 0 | size_t size = lhs_column->size(); |
167 | |
|
168 | 0 | bool lhs_is_nullable = lhs_column->is_nullable(); |
169 | 0 | auto [lhs_data_column, lhs_null_map] = |
170 | 0 | _get_raw_data_and_null_map(lhs_column, lhs_is_nullable); |
171 | 0 | size_t filted = simd::count_zero_num((int8_t*)lhs_data_column, size); |
172 | 0 | bool lhs_all_true = (filted == 0); |
173 | 0 | bool lhs_all_false = (filted == size); |
174 | |
|
175 | 0 | bool lhs_all_is_not_null = false; |
176 | 0 | if (lhs_is_nullable) { |
177 | 0 | filted = simd::count_zero_num((int8_t*)lhs_null_map, size); |
178 | 0 | lhs_all_is_not_null = (filted == size); |
179 | 0 | } |
180 | |
|
181 | 0 | ColumnPtr rhs_column = nullptr; |
182 | 0 | uint8_t* __restrict rhs_data_column = nullptr; |
183 | 0 | uint8_t* __restrict rhs_null_map = nullptr; |
184 | 0 | bool rhs_is_nullable = false; |
185 | 0 | bool rhs_all_true = false; |
186 | 0 | bool rhs_all_false = false; |
187 | 0 | bool rhs_all_is_not_null = false; |
188 | 0 | bool result_is_nullable = _data_type->is_nullable(); |
189 | |
|
190 | 0 | auto get_rhs_colum = [&]() { |
191 | 0 | if (!rhs_column) { |
192 | 0 | RETURN_IF_ERROR( |
193 | 0 | _children[1]->execute_column(context, block, selector, count, rhs_column)); |
194 | 0 | rhs_column = rhs_column->convert_to_full_column_if_const(); |
195 | 0 | rhs_is_nullable = rhs_column->is_nullable(); |
196 | 0 | auto rhs_nullable_column = _get_raw_data_and_null_map(rhs_column, rhs_is_nullable); |
197 | 0 | rhs_data_column = rhs_nullable_column.first; |
198 | 0 | rhs_null_map = rhs_nullable_column.second; |
199 | 0 | size_t filted = simd::count_zero_num((int8_t*)rhs_data_column, size); |
200 | 0 | rhs_all_true = (filted == 0); |
201 | 0 | rhs_all_false = (filted == size); |
202 | 0 | if (rhs_is_nullable) { |
203 | 0 | filted = simd::count_zero_num((int8_t*)rhs_null_map, size); |
204 | 0 | rhs_all_is_not_null = (filted == size); |
205 | 0 | } |
206 | 0 | } |
207 | 0 | return Status::OK(); |
208 | 0 | }; |
209 | |
|
210 | 0 | auto return_result_column_id = [&](ColumnPtr& arg_column) { |
211 | 0 | result_column = std::move(*arg_column).mutate(); |
212 | 0 | if (result_is_nullable && !result_column->is_nullable()) { |
213 | 0 | result_column = make_nullable(result_column); |
214 | 0 | } |
215 | 0 | }; |
216 | |
|
217 | 0 | auto create_null_map_column = [&](ColumnPtr& null_map_column, |
218 | 0 | uint8_t* __restrict null_map_data) { |
219 | 0 | if (null_map_data == nullptr) { |
220 | 0 | null_map_column = ColumnUInt8::create(size, 0); |
221 | 0 | null_map_data = assert_cast<ColumnUInt8*>(null_map_column->assume_mutable().get()) |
222 | 0 | ->get_data() |
223 | 0 | .data(); |
224 | 0 | } |
225 | 0 | return null_map_data; |
226 | 0 | }; |
227 | |
|
228 | 0 | auto vector_vector = [&]<bool is_and_op>() { |
229 | 0 | if (lhs_column->use_count() == 1) { |
230 | 0 | result_column = lhs_column; |
231 | 0 | } else if (rhs_column->use_count() == 1) { |
232 | 0 | result_column = rhs_column; |
233 | 0 | auto tmp_column = rhs_data_column; |
234 | 0 | rhs_data_column = lhs_data_column; |
235 | 0 | lhs_data_column = tmp_column; |
236 | 0 | } else { |
237 | 0 | auto col_res = lhs_column->clone_resized(size); |
238 | 0 | lhs_data_column = assert_cast<ColumnUInt8*>(col_res.get())->get_data().data(); |
239 | 0 | result_column = std::move(col_res); |
240 | 0 | } |
241 | |
|
242 | 0 | do_not_null_pred<is_and_op>(lhs_data_column, rhs_data_column, size); |
243 | 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 |
244 | 0 | auto vector_vector_null = [&]<bool is_and_op>() { |
245 | 0 | auto col_res = ColumnUInt8::create(size); |
246 | 0 | auto col_nulls = ColumnUInt8::create(size); |
247 | |
|
248 | 0 | auto* __restrict res_datas = |
249 | 0 | assert_cast<ColumnUInt8*>(col_res.get())->get_data().data(); |
250 | 0 | auto* __restrict res_nulls = |
251 | 0 | assert_cast<ColumnUInt8*>(col_nulls.get())->get_data().data(); |
252 | 0 | ColumnPtr temp_null_map = nullptr; |
253 | | // maybe both children are nullable / or one of children is nullable |
254 | 0 | auto* __restrict lhs_null_map_tmp = create_null_map_column(temp_null_map, lhs_null_map); |
255 | 0 | auto* __restrict rhs_null_map_tmp = create_null_map_column(temp_null_map, rhs_null_map); |
256 | 0 | auto* __restrict lhs_data_column_tmp = lhs_data_column; |
257 | 0 | auto* __restrict rhs_data_column_tmp = rhs_data_column; |
258 | |
|
259 | 0 | do_null_pred<is_and_op>(lhs_data_column_tmp, lhs_null_map_tmp, rhs_data_column_tmp, |
260 | 0 | rhs_null_map_tmp, res_datas, res_nulls, size); |
261 | |
|
262 | 0 | result_column = ColumnNullable::create(std::move(col_res), std::move(col_nulls)); |
263 | 0 | }; Unexecuted instantiation: _ZZNK5doris13VCompoundPred14execute_columnEPNS_12VExprContextEPKNS_5BlockEPNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISD_EEENKUlTnbvE0_clILb1EEEDav Unexecuted instantiation: _ZZNK5doris13VCompoundPred14execute_columnEPNS_12VExprContextEPKNS_5BlockEPNS_8PODArrayIjLm4096ENS_9AllocatorILb0ELb0ELb0ENS_22DefaultMemoryAllocatorELb1EEELm16ELm15EEEmRNS_3COWINS_7IColumnEE13immutable_ptrISD_EEENKUlTnbvE0_clILb0EEEDav |
264 | | |
265 | | // false and NULL ----> 0 |
266 | | // true and NULL ----> NULL |
267 | 0 | if (_op == TExprOpcode::COMPOUND_AND) { |
268 | | //1. not null column: all data is false |
269 | | //2. nullable column: null map all is not null |
270 | 0 | if ((lhs_all_false && !lhs_is_nullable) || (lhs_all_false && lhs_all_is_not_null)) { |
271 | | // false and any = false, return lhs |
272 | 0 | return_result_column_id(lhs_column); |
273 | 0 | } else { |
274 | 0 | RETURN_IF_ERROR(get_rhs_colum()); |
275 | | |
276 | 0 | if ((lhs_all_true && !lhs_is_nullable) || //not null column |
277 | 0 | (lhs_all_true && lhs_all_is_not_null)) { //nullable column |
278 | | // true and any = any, return rhs |
279 | |
|
280 | 0 | return_result_column_id(rhs_column); |
281 | 0 | } else if ((rhs_all_false && !rhs_is_nullable) || |
282 | 0 | (rhs_all_false && rhs_all_is_not_null)) { |
283 | | // any and false = false, return rhs |
284 | 0 | return_result_column_id(rhs_column); |
285 | 0 | } else if ((rhs_all_true && !rhs_is_nullable) || |
286 | 0 | (rhs_all_true && rhs_all_is_not_null)) { |
287 | | // any and true = any, return lhs |
288 | 0 | return_result_column_id(lhs_column); |
289 | 0 | } else { |
290 | 0 | if (!result_is_nullable) { |
291 | 0 | vector_vector.template operator()<true>(); |
292 | 0 | } else { |
293 | 0 | vector_vector_null.template operator()<true>(); |
294 | 0 | } |
295 | 0 | } |
296 | 0 | } |
297 | 0 | } else if (_op == TExprOpcode::COMPOUND_OR) { |
298 | | // true or NULL ----> 1 |
299 | | // false or NULL ----> NULL |
300 | 0 | if ((lhs_all_true && !lhs_is_nullable) || (lhs_all_true && lhs_all_is_not_null)) { |
301 | | // true or any = true, return lhs |
302 | 0 | return_result_column_id(lhs_column); |
303 | 0 | } else { |
304 | 0 | RETURN_IF_ERROR(get_rhs_colum()); |
305 | 0 | if ((lhs_all_false && !lhs_is_nullable) || (lhs_all_false && lhs_all_is_not_null)) { |
306 | | // false or any = any, return rhs |
307 | 0 | return_result_column_id(rhs_column); |
308 | 0 | } else if ((rhs_all_true && !rhs_is_nullable) || |
309 | 0 | (rhs_all_true && rhs_all_is_not_null)) { |
310 | | // any or true = true, return rhs |
311 | 0 | return_result_column_id(rhs_column); |
312 | 0 | } else if ((rhs_all_false && !rhs_is_nullable) || |
313 | 0 | (rhs_all_false && rhs_all_is_not_null)) { |
314 | | // any or false = any, return lhs |
315 | 0 | return_result_column_id(lhs_column); |
316 | 0 | } else { |
317 | 0 | if (!result_is_nullable) { |
318 | 0 | vector_vector.template operator()<false>(); |
319 | 0 | } else { |
320 | 0 | vector_vector_null.template operator()<false>(); |
321 | 0 | } |
322 | 0 | } |
323 | 0 | } |
324 | 0 | } else { |
325 | 0 | return Status::InternalError("Compound operator must be AND or OR."); |
326 | 0 | } |
327 | | |
328 | 0 | DCHECK_EQ(result_column->size(), count); |
329 | 0 | return Status::OK(); |
330 | 0 | } |
331 | | |
332 | 0 | double execute_cost() const override { |
333 | 0 | double cost = 0.3; |
334 | 0 | for (const auto& child : _children) { |
335 | 0 | cost += child->execute_cost(); |
336 | 0 | } |
337 | 0 | return cost; |
338 | 0 | } |
339 | | |
340 | | private: |
341 | 0 | static inline constexpr uint8_t apply_and_null(UInt8 a, UInt8 l_null, UInt8 b, UInt8 r_null) { |
342 | | // (<> && false) is false, (true && NULL) is NULL |
343 | 0 | return (l_null & r_null) | (r_null & (l_null ^ a)) | (l_null & (r_null ^ b)); |
344 | 0 | } |
345 | 0 | static inline constexpr uint8_t apply_or_null(UInt8 a, UInt8 l_null, UInt8 b, UInt8 r_null) { |
346 | | // (<> || true) is true, (false || NULL) is NULL |
347 | 0 | return (l_null & r_null) | (r_null & (r_null ^ a)) | (l_null & (l_null ^ b)); |
348 | 0 | } |
349 | | |
350 | | template <bool is_and> |
351 | 0 | void static do_not_null_pred(uint8_t* __restrict lhs, uint8_t* __restrict rhs, size_t size) { |
352 | | #ifdef NDEBUG |
353 | | #if defined(__clang__) |
354 | | #pragma clang loop vectorize(enable) |
355 | | #elif defined(__GNUC__) && (__GNUC__ >= 5) |
356 | | #pragma GCC ivdep |
357 | | #endif |
358 | | #endif |
359 | 0 | for (size_t i = 0; i < size; ++i) { |
360 | 0 | if constexpr (is_and) { |
361 | 0 | lhs[i] &= rhs[i]; |
362 | 0 | } else { |
363 | 0 | lhs[i] |= rhs[i]; |
364 | 0 | } |
365 | 0 | } |
366 | 0 | } Unexecuted instantiation: _ZN5doris13VCompoundPred16do_not_null_predILb1EEEvPhS2_m Unexecuted instantiation: _ZN5doris13VCompoundPred16do_not_null_predILb0EEEvPhS2_m |
367 | | |
368 | | template <bool is_and> |
369 | | void static do_null_pred(uint8_t* __restrict lhs_data, uint8_t* __restrict lhs_null, |
370 | | uint8_t* __restrict rhs_data, uint8_t* __restrict rhs_null, |
371 | | uint8_t* __restrict res_data, uint8_t* __restrict res_null, |
372 | 0 | size_t size) { |
373 | | #ifdef NDEBUG |
374 | | #if defined(__clang__) |
375 | | #pragma clang loop vectorize(enable) |
376 | | #elif defined(__GNUC__) && (__GNUC__ >= 5) |
377 | | #pragma GCC ivdep |
378 | | #endif |
379 | | #endif |
380 | 0 | for (size_t i = 0; i < size; ++i) { |
381 | 0 | if constexpr (is_and) { |
382 | 0 | res_null[i] = apply_and_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]); |
383 | 0 | res_data[i] = lhs_data[i] & rhs_data[i]; |
384 | 0 | } else { |
385 | 0 | res_null[i] = apply_or_null(lhs_data[i], lhs_null[i], rhs_data[i], rhs_null[i]); |
386 | 0 | res_data[i] = lhs_data[i] | rhs_data[i]; |
387 | 0 | } |
388 | 0 | } |
389 | 0 | } Unexecuted instantiation: _ZN5doris13VCompoundPred12do_null_predILb1EEEvPhS2_S2_S2_S2_S2_m Unexecuted instantiation: _ZN5doris13VCompoundPred12do_null_predILb0EEEvPhS2_S2_S2_S2_S2_m |
390 | | |
391 | 0 | bool _has_const_child() const { |
392 | 0 | return std::ranges::any_of(_children, |
393 | 0 | [](const VExprSPtr& arg) -> bool { return arg->is_constant(); }); |
394 | 0 | } |
395 | | |
396 | | std::pair<uint8_t*, uint8_t*> _get_raw_data_and_null_map(ColumnPtr column, |
397 | 0 | bool has_nullable_column) const { |
398 | 0 | if (has_nullable_column) { |
399 | 0 | auto* nullable_column = assert_cast<ColumnNullable*>(column->assume_mutable().get()); |
400 | 0 | auto* data_column = |
401 | 0 | assert_cast<ColumnUInt8*>(nullable_column->get_nested_column_ptr().get()) |
402 | 0 | ->get_data() |
403 | 0 | .data(); |
404 | 0 | auto* null_map = |
405 | 0 | assert_cast<ColumnUInt8*>(nullable_column->get_null_map_column_ptr().get()) |
406 | 0 | ->get_data() |
407 | 0 | .data(); |
408 | 0 | return std::make_pair(data_column, null_map); |
409 | 0 | } else { |
410 | 0 | auto* data_column = |
411 | 0 | assert_cast<ColumnUInt8*>(column->assume_mutable().get())->get_data().data(); |
412 | 0 | return std::make_pair(data_column, nullptr); |
413 | 0 | } |
414 | 0 | } |
415 | | |
416 | | TExprOpcode::type _op; |
417 | | }; |
418 | | |
419 | | } // namespace doris |