/root/doris/be/src/exprs/function/if.cpp
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 | | // This file is copied from |
18 | | // https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/If.cpp |
19 | | // and modified by Doris |
20 | | |
21 | | #include "exprs/function/if.h" |
22 | | |
23 | | #include <glog/logging.h> |
24 | | #include <stddef.h> |
25 | | |
26 | | #include <algorithm> |
27 | | #include <boost/iterator/iterator_facade.hpp> |
28 | | #include <memory> |
29 | | #include <type_traits> |
30 | | #include <utility> |
31 | | |
32 | | #include "common/status.h" |
33 | | #include "core/assert_cast.h" |
34 | | #include "core/block/block.h" |
35 | | #include "core/block/column_numbers.h" |
36 | | #include "core/block/column_with_type_and_name.h" |
37 | | #include "core/call_on_type_index.h" |
38 | | #include "core/column/column.h" |
39 | | #include "core/column/column_const.h" |
40 | | #include "core/column/column_nullable.h" |
41 | | #include "core/column/column_vector.h" |
42 | | #include "core/data_type/data_type.h" |
43 | | #include "core/data_type/data_type_date_or_datetime_v2.h" |
44 | | #include "core/data_type/data_type_decimal.h" |
45 | | #include "core/data_type/data_type_ipv4.h" |
46 | | #include "core/data_type/data_type_ipv6.h" |
47 | | #include "core/data_type/data_type_nullable.h" |
48 | | #include "core/data_type/data_type_number.h" |
49 | | #include "core/data_type/data_type_time.h" |
50 | | #include "core/data_type/define_primitive_type.h" |
51 | | #include "core/data_type/primitive_type.h" |
52 | | #include "core/typeid_cast.h" |
53 | | #include "core/types.h" |
54 | | #include "exprs/aggregate/aggregate_function.h" |
55 | | #include "exprs/function/cast_type_to_either.h" |
56 | | #include "exprs/function/function.h" |
57 | | #include "exprs/function/function_helpers.h" |
58 | | #include "exprs/function/simple_function_factory.h" |
59 | | #include "util/simd/bits.h" |
60 | | namespace doris { |
61 | | class FunctionContext; |
62 | | |
63 | | namespace NumberTraits { |
64 | | struct Error; |
65 | | } // namespace NumberTraits |
66 | | } // namespace doris |
67 | | |
68 | | namespace doris { |
69 | | |
70 | 3 | size_t count_true_with_notnull(const ColumnPtr& col) { |
71 | 3 | if (col->only_null()) { |
72 | 0 | return 0; |
73 | 0 | } |
74 | | |
75 | 3 | if (const auto* const_col = check_and_get_column_const<ColumnUInt8>(col.get())) { |
76 | 0 | bool is_true = const_col->get_bool(0); |
77 | 0 | return is_true ? col->size() : 0; |
78 | 0 | } |
79 | | |
80 | 3 | auto count = col->size(); |
81 | 3 | if (col->is_nullable()) { |
82 | 3 | const auto* nullable = assert_cast<const ColumnNullable*>(col.get()); |
83 | 3 | const auto* __restrict null_data = nullable->get_null_map_data().data(); |
84 | 3 | const auto* __restrict bool_data = |
85 | 3 | ((const ColumnUInt8&)(nullable->get_nested_column())).get_data().data(); |
86 | | |
87 | 3 | size_t null_count = count - simd::count_zero_num((const int8_t*)null_data, count); |
88 | | |
89 | 3 | if (null_count == count) { |
90 | 0 | return 0; |
91 | 3 | } else if (null_count == 0) { |
92 | 0 | size_t true_count = count - simd::count_zero_num((const int8_t*)bool_data, count); |
93 | 0 | return true_count; |
94 | 3 | } else { |
95 | | // In fact, the null_count maybe is different with true_count, but it's no impact |
96 | 3 | return null_count; |
97 | 3 | } |
98 | 3 | } else { |
99 | 0 | const auto* bool_col = assert_cast<const ColumnUInt8*>(col.get()); |
100 | 0 | const auto* __restrict bool_data = bool_col->get_data().data(); |
101 | 0 | return count - simd::count_zero_num((const int8_t*)bool_data, count); |
102 | 0 | } |
103 | 3 | } |
104 | | // todo(wb) support llvm codegen |
105 | | class FunctionIf : public IFunction { |
106 | | public: |
107 | | static constexpr auto name = "if"; |
108 | | |
109 | 5 | static FunctionPtr create() { return std::make_shared<FunctionIf>(); } |
110 | 1 | String get_name() const override { return name; } |
111 | | |
112 | 3 | size_t get_number_of_arguments() const override { return 3; } |
113 | 6 | bool use_default_implementation_for_nulls() const override { return false; } |
114 | 3 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
115 | | // if return type is custom, one of nullable return type will be nullable |
116 | 3 | bool nullable = arguments[1]->is_nullable() || arguments[2]->is_nullable(); |
117 | 3 | if (nullable) { |
118 | 3 | return make_nullable(arguments[1]); |
119 | 3 | } else { |
120 | 0 | return arguments[1]; |
121 | 0 | } |
122 | 3 | } |
123 | | |
124 | 9 | static ColumnPtr materialize_column_if_const(const ColumnPtr& column) { |
125 | 9 | return column->convert_to_full_column_if_const(); |
126 | 9 | } |
127 | | |
128 | 0 | static ColumnPtr make_nullable_column_if_not(const ColumnPtr& column) { |
129 | 0 | if (is_column_nullable(*column)) return column; |
130 | | |
131 | 0 | return ColumnNullable::create(materialize_column_if_const(column), |
132 | 0 | ColumnUInt8::create(column->size(), 0)); |
133 | 0 | } |
134 | | |
135 | 0 | static ColumnPtr get_nested_column(const ColumnPtr& column) { |
136 | 0 | if (auto* nullable = check_and_get_column<ColumnNullable>(*column)) |
137 | 0 | return nullable->get_nested_column_ptr(); |
138 | 0 | else if (const auto* column_const = check_and_get_column<ColumnConst>(*column)) |
139 | 0 | return ColumnConst::create(get_nested_column(column_const->get_data_column_ptr()), |
140 | 0 | column->size()); |
141 | | |
142 | 0 | return column; |
143 | 0 | } |
144 | | |
145 | | Status execute_generic(Block& block, const ColumnUInt8* cond_col, |
146 | | const ColumnWithTypeAndName& then_col_type_name, |
147 | | const ColumnWithTypeAndName& else_col_type_name, uint32_t result, |
148 | 0 | size_t input_row_count) const { |
149 | 0 | MutableColumnPtr result_column = block.get_by_position(result).type->create_column(); |
150 | 0 | result_column->reserve(input_row_count); |
151 | |
|
152 | 0 | const IColumn& then_col = *then_col_type_name.column; |
153 | 0 | const IColumn& else_col = *else_col_type_name.column; |
154 | 0 | bool then_is_const = is_column_const(then_col); |
155 | 0 | bool else_is_const = is_column_const(else_col); |
156 | |
|
157 | 0 | const auto& cond_array = cond_col->get_data(); |
158 | |
|
159 | 0 | if (then_is_const && else_is_const) { |
160 | 0 | const IColumn& then_nested_column = |
161 | 0 | assert_cast<const ColumnConst&>(then_col).get_data_column(); |
162 | 0 | const IColumn& else_nested_column = |
163 | 0 | assert_cast<const ColumnConst&>(else_col).get_data_column(); |
164 | 0 | for (size_t i = 0; i < input_row_count; i++) { |
165 | 0 | if (cond_array[i]) |
166 | 0 | result_column->insert_from(then_nested_column, 0); |
167 | 0 | else |
168 | 0 | result_column->insert_from(else_nested_column, 0); |
169 | 0 | } |
170 | 0 | } else if (then_is_const) { |
171 | 0 | const IColumn& then_nested_column = |
172 | 0 | assert_cast<const ColumnConst&>(then_col).get_data_column(); |
173 | |
|
174 | 0 | for (size_t i = 0; i < input_row_count; i++) { |
175 | 0 | if (cond_array[i]) |
176 | 0 | result_column->insert_from(then_nested_column, 0); |
177 | 0 | else |
178 | 0 | result_column->insert_from(else_col, i); |
179 | 0 | } |
180 | 0 | } else if (else_is_const) { |
181 | 0 | const IColumn& else_nested_column = |
182 | 0 | assert_cast<const ColumnConst&>(else_col).get_data_column(); |
183 | |
|
184 | 0 | for (size_t i = 0; i < input_row_count; i++) { |
185 | 0 | if (cond_array[i]) |
186 | 0 | result_column->insert_from(then_col, i); |
187 | 0 | else |
188 | 0 | result_column->insert_from(else_nested_column, 0); |
189 | 0 | } |
190 | 0 | } else { |
191 | 0 | for (size_t i = 0; i < input_row_count; i++) { |
192 | 0 | result_column->insert_from(cond_array[i] ? then_col : else_col, i); |
193 | 0 | } |
194 | 0 | } |
195 | 0 | block.replace_by_position(result, std::move(result_column)); |
196 | 0 | return Status::OK(); |
197 | 0 | } |
198 | | |
199 | | template <PrimitiveType PType> |
200 | | Status execute_basic_type(Block& block, const ColumnUInt8* cond_col, |
201 | | const ColumnWithTypeAndName& then_col, |
202 | | const ColumnWithTypeAndName& else_col, uint32_t result, |
203 | 0 | Status& status) const { |
204 | 0 | if (then_col.type->get_primitive_type() != else_col.type->get_primitive_type()) { |
205 | 0 | return Status::InternalError( |
206 | 0 | "then and else column type must be same for function {} , but got {} , {}", |
207 | 0 | get_name(), then_col.type->get_name(), else_col.type->get_name()); |
208 | 0 | } |
209 | | |
210 | 0 | auto res_column = |
211 | 0 | NumIfImpl<PType>::execute_if(cond_col->get_data(), then_col.column, else_col.column, |
212 | 0 | block.get_by_position(result).type->get_scale()); |
213 | 0 | if (!res_column) { |
214 | 0 | return Status::InternalError("unexpected args column {} , {} , of function {}", |
215 | 0 | then_col.column->get_name(), else_col.column->get_name(), |
216 | 0 | get_name()); |
217 | 0 | } |
218 | 0 | block.replace_by_position(result, std::move(res_column)); |
219 | 0 | return Status::OK(); |
220 | 0 | } Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE2EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE3EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE4EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE5EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE6EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE7EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE8EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE9EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE28EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE29EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE20EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE30EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE35EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE11EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE25EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE26EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE12EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE27EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE42EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE36EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ Unexecuted instantiation: _ZNK5doris10FunctionIf18execute_basic_typeILNS_13PrimitiveTypeE37EEENS_6StatusERNS_5BlockEPKNS_12ColumnVectorILS2_2EEERKNS_21ColumnWithTypeAndNameESC_jRS3_ |
221 | | |
222 | | Status execute_for_null_then_else(FunctionContext* context, Block& block, |
223 | | const ColumnWithTypeAndName& arg_cond, |
224 | | const ColumnWithTypeAndName& arg_then, |
225 | | const ColumnWithTypeAndName& arg_else, uint32_t result, |
226 | 3 | size_t input_rows_count, bool& handled) const { |
227 | 3 | bool then_is_null = arg_then.column->only_null(); |
228 | 3 | bool else_is_null = arg_else.column->only_null(); |
229 | | |
230 | 3 | handled = false; |
231 | 3 | if (!then_is_null && !else_is_null) { |
232 | 0 | return Status::OK(); |
233 | 0 | } |
234 | | |
235 | 3 | if (then_is_null && else_is_null) { |
236 | 0 | block.get_by_position(result).column = |
237 | 0 | block.get_by_position(result).type->create_column_const_with_default_value( |
238 | 0 | input_rows_count); |
239 | 0 | handled = true; |
240 | 0 | return Status::OK(); |
241 | 0 | } |
242 | | |
243 | 3 | const auto* cond_col = typeid_cast<const ColumnUInt8*>(arg_cond.column.get()); |
244 | 3 | const ColumnConst* cond_const_col = |
245 | 3 | check_and_get_column_const<ColumnUInt8>(arg_cond.column.get()); |
246 | | |
247 | | /// If then is NULL, we create Nullable column with null mask OR-ed with condition. |
248 | 3 | if (then_is_null) { |
249 | 3 | if (cond_col) { |
250 | 3 | if (is_column_nullable(*arg_else.column)) { // if(cond, null, nullable) |
251 | 3 | auto arg_else_column = arg_else.column; |
252 | 3 | auto result_column = (*std::move(arg_else_column)).mutate(); |
253 | 3 | assert_cast<ColumnNullable&>(*result_column) |
254 | 3 | .apply_null_map(assert_cast<const ColumnUInt8&>(*arg_cond.column)); |
255 | 3 | block.replace_by_position(result, std::move(result_column)); |
256 | 3 | } else { // if(cond, null, not_nullable) |
257 | 0 | block.replace_by_position( |
258 | 0 | result, |
259 | 0 | ColumnNullable::create(materialize_column_if_const(arg_else.column), |
260 | 0 | arg_cond.column)); |
261 | 0 | } |
262 | 3 | } else if (cond_const_col) { |
263 | 0 | if (cond_const_col->get_value<TYPE_BOOLEAN>()) { // if(true, null, else) |
264 | 0 | block.get_by_position(result).column = |
265 | 0 | block.get_by_position(result).type->create_column()->clone_resized( |
266 | 0 | input_rows_count); |
267 | 0 | } else { // if(false, null, else) |
268 | 0 | block.get_by_position(result).column = |
269 | 0 | make_nullable_column_if_not(arg_else.column); |
270 | 0 | } |
271 | 0 | } else { |
272 | 0 | return Status::InternalError( |
273 | 0 | "Illegal column {} of first argument of function {}. Must be ColumnUInt8 " |
274 | 0 | "or ColumnConstUInt8.", |
275 | 0 | arg_cond.column->get_name(), get_name()); |
276 | 0 | } |
277 | 3 | } else { /// If else is NULL, we create Nullable column with null mask OR-ed with negated condition. |
278 | 0 | if (cond_col) { |
279 | 0 | size_t size = input_rows_count; |
280 | |
|
281 | 0 | if (is_column_nullable(*arg_then.column)) { // if(cond, nullable, NULL) |
282 | 0 | auto arg_then_column = arg_then.column; |
283 | 0 | auto result_column = (*std::move(arg_then_column)).mutate(); |
284 | 0 | assert_cast<ColumnNullable&>(*result_column) |
285 | 0 | .apply_negated_null_map( |
286 | 0 | assert_cast<const ColumnUInt8&>(*arg_cond.column)); |
287 | 0 | block.replace_by_position(result, std::move(result_column)); |
288 | 0 | } else { // if(cond, not_nullable, NULL) |
289 | 0 | const auto& null_map_data = cond_col->get_data(); |
290 | 0 | auto negated_null_map = ColumnUInt8::create(); |
291 | 0 | auto& negated_null_map_data = negated_null_map->get_data(); |
292 | 0 | negated_null_map_data.resize(size); |
293 | |
|
294 | 0 | for (size_t i = 0; i < size; ++i) { |
295 | 0 | negated_null_map_data[i] = !null_map_data[i]; |
296 | 0 | } |
297 | |
|
298 | 0 | block.replace_by_position( |
299 | 0 | result, |
300 | 0 | ColumnNullable::create(materialize_column_if_const(arg_then.column), |
301 | 0 | std::move(negated_null_map))); |
302 | 0 | } |
303 | 0 | } else if (cond_const_col) { |
304 | 0 | if (cond_const_col->get_value<TYPE_BOOLEAN>()) { // if(true, then, NULL) |
305 | 0 | block.get_by_position(result).column = |
306 | 0 | make_nullable_column_if_not(arg_then.column); |
307 | 0 | } else { // if(false, then, NULL) |
308 | 0 | block.get_by_position(result).column = |
309 | 0 | block.get_by_position(result).type->create_column()->clone_resized( |
310 | 0 | input_rows_count); |
311 | 0 | } |
312 | 0 | } else { |
313 | 0 | return Status::InternalError( |
314 | 0 | "Illegal column {} of first argument of function {}. Must be ColumnUInt8 " |
315 | 0 | "or ColumnConstUInt8.", |
316 | 0 | arg_cond.column->get_name(), get_name()); |
317 | 0 | } |
318 | 0 | } |
319 | 3 | handled = true; |
320 | 3 | return Status::OK(); |
321 | 3 | } |
322 | | |
323 | | Status execute_for_nullable_then_else(FunctionContext* context, Block& block, |
324 | | const ColumnWithTypeAndName& arg_cond, |
325 | | const ColumnWithTypeAndName& arg_then, |
326 | | const ColumnWithTypeAndName& arg_else, uint32_t result, |
327 | 0 | size_t input_rows_count, bool& handled) const { |
328 | 0 | auto then_type_is_nullable = arg_then.type->is_nullable(); |
329 | 0 | auto else_type_is_nullable = arg_else.type->is_nullable(); |
330 | 0 | handled = false; |
331 | 0 | if (!then_type_is_nullable && !else_type_is_nullable) { |
332 | 0 | return Status::OK(); |
333 | 0 | } |
334 | | |
335 | 0 | auto* then_is_nullable = check_and_get_column<ColumnNullable>(*arg_then.column); |
336 | 0 | auto* else_is_nullable = check_and_get_column<ColumnNullable>(*arg_else.column); |
337 | 0 | bool then_column_is_const_nullable = false; |
338 | 0 | bool else_column_is_const_nullable = false; |
339 | 0 | if (then_type_is_nullable && then_is_nullable == nullptr) { |
340 | | //this case is a const(nullable column) |
341 | 0 | auto& const_column = assert_cast<const ColumnConst&>(*arg_then.column); |
342 | 0 | then_is_nullable = |
343 | 0 | assert_cast<const ColumnNullable*>(const_column.get_data_column_ptr().get()); |
344 | 0 | then_column_is_const_nullable = true; |
345 | 0 | } |
346 | |
|
347 | 0 | if (else_type_is_nullable && else_is_nullable == nullptr) { |
348 | | //this case is a const(nullable column) |
349 | 0 | auto& const_column = assert_cast<const ColumnConst&>(*arg_else.column); |
350 | 0 | else_is_nullable = |
351 | 0 | assert_cast<const ColumnNullable*>(const_column.get_data_column_ptr().get()); |
352 | 0 | else_column_is_const_nullable = true; |
353 | 0 | } |
354 | | |
355 | | /** Calculate null mask of result and nested column separately. |
356 | | */ |
357 | 0 | ColumnPtr result_null_mask; |
358 | 0 | { |
359 | | // get null map from column: |
360 | | // a. get_null_map_column_ptr() : it's a real nullable column, so could get it from nullable column |
361 | | // b. create a const_nullmap_column: it's a not nullable column or a const nullable column, contain a const value |
362 | 0 | Block temporary_block; |
363 | 0 | temporary_block.insert(arg_cond); |
364 | 0 | auto then_nested_null_map = |
365 | 0 | (then_type_is_nullable && !then_column_is_const_nullable) |
366 | 0 | ? then_is_nullable->get_null_map_column_ptr() |
367 | 0 | : DataTypeUInt8().create_column_const_with_default_value( |
368 | 0 | input_rows_count); |
369 | 0 | temporary_block.insert({then_nested_null_map, std::make_shared<DataTypeUInt8>(), |
370 | 0 | "then_column_null_map"}); |
371 | |
|
372 | 0 | auto else_nested_null_map = |
373 | 0 | (else_type_is_nullable && !else_column_is_const_nullable) |
374 | 0 | ? else_is_nullable->get_null_map_column_ptr() |
375 | 0 | : DataTypeUInt8().create_column_const_with_default_value( |
376 | 0 | input_rows_count); |
377 | 0 | temporary_block.insert({else_nested_null_map, std::make_shared<DataTypeUInt8>(), |
378 | 0 | "else_column_null_map"}); |
379 | 0 | temporary_block.insert( |
380 | 0 | {nullptr, std::make_shared<DataTypeUInt8>(), "result_column_null_map"}); |
381 | |
|
382 | 0 | RETURN_IF_ERROR(_execute_impl_internal(context, temporary_block, {0, 1, 2}, 3, |
383 | 0 | temporary_block.rows())); |
384 | | |
385 | 0 | result_null_mask = temporary_block.get_by_position(3).column; |
386 | 0 | } |
387 | | |
388 | 0 | ColumnPtr result_nested_column; |
389 | |
|
390 | 0 | { |
391 | 0 | Block temporary_block( |
392 | 0 | {arg_cond, |
393 | 0 | {get_nested_column(arg_then.column), remove_nullable(arg_then.type), ""}, |
394 | 0 | {get_nested_column(arg_else.column), remove_nullable(arg_else.type), ""}, |
395 | 0 | {nullptr, remove_nullable(block.get_by_position(result).type), ""}}); |
396 | |
|
397 | 0 | RETURN_IF_ERROR(_execute_impl_internal(context, temporary_block, {0, 1, 2}, 3, |
398 | 0 | temporary_block.rows())); |
399 | | |
400 | 0 | result_nested_column = temporary_block.get_by_position(3).column; |
401 | 0 | } |
402 | | |
403 | 0 | auto column = ColumnNullable::create(materialize_column_if_const(result_nested_column), |
404 | 0 | materialize_column_if_const(result_null_mask)); |
405 | 0 | block.replace_by_position(result, std::move(column)); |
406 | 0 | handled = true; |
407 | 0 | return Status::OK(); |
408 | 0 | } |
409 | | |
410 | | Status execute_for_null_condition(FunctionContext* context, Block& block, |
411 | | const ColumnNumbers& arguments, |
412 | | const ColumnWithTypeAndName& arg_cond, |
413 | | const ColumnWithTypeAndName& arg_then, |
414 | | const ColumnWithTypeAndName& arg_else, uint32_t result, |
415 | 6 | bool& handled) const { |
416 | 6 | bool cond_is_null = arg_cond.column->only_null(); |
417 | 6 | handled = false; |
418 | | |
419 | 6 | if (cond_is_null) { |
420 | 0 | block.replace_by_position(result, |
421 | 0 | arg_else.column->clone_resized(arg_cond.column->size())); |
422 | 0 | handled = true; |
423 | 0 | return Status::OK(); |
424 | 0 | } |
425 | | |
426 | 6 | if (const auto* nullable = check_and_get_column<ColumnNullable>(*arg_cond.column)) { |
427 | 3 | DCHECK(remove_nullable(arg_cond.type)->get_primitive_type() == |
428 | 3 | PrimitiveType::TYPE_BOOLEAN); |
429 | | |
430 | | // update nested column by null map |
431 | 3 | const auto* __restrict null_map = nullable->get_null_map_data().data(); |
432 | 3 | auto* __restrict nested_bool_data = |
433 | 3 | ((ColumnUInt8&)(nullable->get_nested_column())).get_data().data(); |
434 | 3 | auto rows = nullable->size(); |
435 | 12 | for (size_t i = 0; i < rows; i++) { |
436 | 9 | nested_bool_data[i] &= !null_map[i]; |
437 | 9 | } |
438 | 3 | auto column_size = block.columns(); |
439 | 3 | block.insert({nullable->get_nested_column_ptr(), remove_nullable(arg_cond.type), |
440 | 3 | arg_cond.name}); |
441 | | |
442 | 3 | handled = true; |
443 | 3 | return _execute_impl_internal(context, block, {column_size, arguments[1], arguments[2]}, |
444 | 3 | result, rows); |
445 | 3 | } |
446 | 3 | return Status::OK(); |
447 | 6 | } |
448 | | |
449 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
450 | 3 | uint32_t result, size_t input_rows_count) const override { |
451 | 3 | const ColumnWithTypeAndName& arg_then = block.get_by_position(arguments[1]); |
452 | 3 | const ColumnWithTypeAndName& arg_else = block.get_by_position(arguments[2]); |
453 | | |
454 | | /// A case for identical then and else (pointers are the same). |
455 | 3 | if (arg_then.column.get() == arg_else.column.get()) { |
456 | | /// Just point result to them. |
457 | 0 | block.replace_by_position(result, arg_then.column); |
458 | 0 | return Status::OK(); |
459 | 0 | } |
460 | | |
461 | 3 | ColumnWithTypeAndName& cond_column = block.get_by_position(arguments[0]); |
462 | 3 | cond_column.column = materialize_column_if_const(cond_column.column); |
463 | 3 | const ColumnWithTypeAndName& arg_cond = block.get_by_position(arguments[0]); |
464 | | |
465 | 3 | auto true_count = count_true_with_notnull(arg_cond.column); |
466 | 3 | auto item_count = arg_cond.column->size(); |
467 | 3 | if (true_count == item_count || true_count == 0) { |
468 | 0 | bool result_nullable = block.get_by_position(result).type->is_nullable(); |
469 | 0 | if (true_count == item_count) { |
470 | 0 | block.replace_by_position( |
471 | 0 | result, |
472 | 0 | result_nullable |
473 | 0 | ? make_nullable(arg_then.column->clone_resized(input_rows_count)) |
474 | 0 | : arg_then.column->clone_resized(input_rows_count)); |
475 | 0 | } else { |
476 | 0 | block.replace_by_position( |
477 | 0 | result, |
478 | 0 | result_nullable |
479 | 0 | ? make_nullable(arg_else.column->clone_resized(input_rows_count)) |
480 | 0 | : arg_else.column->clone_resized(input_rows_count)); |
481 | 0 | } |
482 | 0 | return Status::OK(); |
483 | 0 | } |
484 | | |
485 | 3 | return _execute_impl_internal(context, block, arguments, result, input_rows_count); |
486 | 3 | } |
487 | | |
488 | | Status _execute_impl_internal(FunctionContext* context, Block& block, |
489 | | const ColumnNumbers& arguments, uint32_t result, |
490 | 6 | size_t input_rows_count) const { |
491 | 6 | const ColumnWithTypeAndName& arg_then = block.get_by_position(arguments[1]); |
492 | 6 | const ColumnWithTypeAndName& arg_else = block.get_by_position(arguments[2]); |
493 | 6 | ColumnWithTypeAndName& cond_column = block.get_by_position(arguments[0]); |
494 | 6 | cond_column.column = materialize_column_if_const(cond_column.column); |
495 | 6 | const ColumnWithTypeAndName& arg_cond = block.get_by_position(arguments[0]); |
496 | | |
497 | 6 | Status ret = Status::OK(); |
498 | 6 | bool handled = false; |
499 | 6 | RETURN_IF_ERROR(execute_for_null_condition(context, block, arguments, arg_cond, arg_then, |
500 | 6 | arg_else, result, handled)); |
501 | | |
502 | 6 | if (!handled) { |
503 | 3 | RETURN_IF_ERROR(execute_for_null_then_else(context, block, arg_cond, arg_then, arg_else, |
504 | 3 | result, input_rows_count, handled)); |
505 | 3 | } |
506 | | |
507 | 6 | if (!handled) { |
508 | 0 | RETURN_IF_ERROR(execute_for_nullable_then_else(context, block, arg_cond, arg_then, |
509 | 0 | arg_else, result, input_rows_count, |
510 | 0 | handled)); |
511 | 0 | } |
512 | | |
513 | 6 | if (handled) { |
514 | 6 | return Status::OK(); |
515 | 6 | } |
516 | | |
517 | 0 | const auto* cond_col = assert_cast<const ColumnUInt8*>(arg_cond.column.get()); |
518 | 0 | const ColumnConst* cond_const_col = |
519 | 0 | check_and_get_column_const<ColumnUInt8>(arg_cond.column.get()); |
520 | |
|
521 | 0 | if (cond_const_col) { |
522 | 0 | block.get_by_position(result).column = |
523 | 0 | cond_const_col->get_value<TYPE_BOOLEAN>() ? arg_then.column : arg_else.column; |
524 | 0 | return Status::OK(); |
525 | 0 | } |
526 | | |
527 | 0 | Status vec_exec; |
528 | |
|
529 | 0 | auto call = [&](const auto& type) -> bool { |
530 | 0 | using DataType = std::decay_t<decltype(type)>; |
531 | 0 | vec_exec = execute_basic_type<DataType::PType>(block, cond_col, arg_then, arg_else, |
532 | 0 | result, vec_exec); |
533 | 0 | return true; |
534 | 0 | }; Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE2EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE3EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE4EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE5EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE6EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE7EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE8EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE9EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE28EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE29EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE20EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE30EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE35EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE11EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE25EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE26EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE12EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE27EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE42EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE36EEEEEbSC_ Unexecuted instantiation: _ZZNK5doris10FunctionIf22_execute_impl_internalEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmENKUlRKT_E_clINS_16DispatchDataTypeILNS_13PrimitiveTypeE37EEEEEbSC_ |
535 | |
|
536 | 0 | auto can_use_vec_exec = dispatch_switch_scalar(arg_then.type->get_primitive_type(), call); |
537 | 0 | if (can_use_vec_exec) { |
538 | 0 | return vec_exec; |
539 | 0 | } else { |
540 | 0 | return execute_generic(block, cond_col, arg_then, arg_else, result, input_rows_count); |
541 | 0 | } |
542 | 0 | } |
543 | | }; |
544 | | |
545 | 1 | void register_function_if(SimpleFunctionFactory& factory) { |
546 | 1 | factory.register_function<FunctionIf>(); |
547 | 1 | } |
548 | | |
549 | | } // namespace doris |