/root/doris/be/src/vec/functions/function.cpp
Line | Count | Source (jump to first uncovered line) |
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/IFunction.cpp |
19 | | // and modified by Doris |
20 | | |
21 | | #include "vec/functions/function.h" |
22 | | |
23 | | #include <algorithm> |
24 | | #include <memory> |
25 | | #include <numeric> |
26 | | #include <vector> |
27 | | |
28 | | #include "vec/aggregate_functions/aggregate_function.h" |
29 | | #include "vec/columns/column.h" |
30 | | #include "vec/columns/column_const.h" |
31 | | #include "vec/columns/column_nullable.h" |
32 | | #include "vec/columns/column_vector.h" |
33 | | #include "vec/columns/columns_number.h" |
34 | | #include "vec/common/assert_cast.h" |
35 | | #include "vec/core/field.h" |
36 | | #include "vec/data_types/data_type_array.h" |
37 | | #include "vec/data_types/data_type_nothing.h" |
38 | | #include "vec/data_types/data_type_nullable.h" |
39 | | #include "vec/functions/function_helpers.h" |
40 | | #include "vec/utils/util.hpp" |
41 | | |
42 | | namespace doris::vectorized { |
43 | | |
44 | | ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args, |
45 | 323 | size_t result, size_t input_rows_count) { |
46 | 323 | ColumnPtr result_null_map_column; |
47 | | /// If result is already nullable. |
48 | 323 | ColumnPtr src_not_nullable = src; |
49 | 323 | MutableColumnPtr mutable_result_null_map_column; |
50 | | |
51 | 323 | if (const auto* nullable = check_and_get_column<ColumnNullable>(*src)) { |
52 | 171 | src_not_nullable = nullable->get_nested_column_ptr(); |
53 | 171 | result_null_map_column = nullable->get_null_map_column_ptr(); |
54 | 171 | } |
55 | | |
56 | 500 | for (const auto& arg : args) { |
57 | 500 | const ColumnWithTypeAndName& elem = block.get_by_position(arg); |
58 | 500 | if (!elem.type->is_nullable() || is_column_const(*elem.column)) { |
59 | 64 | continue; |
60 | 64 | } |
61 | | |
62 | 436 | if (const auto* nullable = assert_cast<const ColumnNullable*>(elem.column.get()); |
63 | 436 | nullable->has_null()) { |
64 | 145 | const ColumnPtr& null_map_column = nullable->get_null_map_column_ptr(); |
65 | 145 | if (!result_null_map_column) { // NOLINT(bugprone-use-after-move) |
66 | 72 | result_null_map_column = null_map_column->clone_resized(input_rows_count); |
67 | 72 | continue; |
68 | 72 | } |
69 | | |
70 | 73 | if (!mutable_result_null_map_column) { |
71 | 60 | mutable_result_null_map_column = |
72 | 60 | std::move(result_null_map_column)->assume_mutable(); |
73 | 60 | } |
74 | | |
75 | 73 | NullMap& result_null_map = |
76 | 73 | assert_cast<ColumnUInt8&>(*mutable_result_null_map_column).get_data(); |
77 | 73 | const NullMap& src_null_map = |
78 | 73 | assert_cast<const ColumnUInt8&>(*null_map_column).get_data(); |
79 | | |
80 | 73 | VectorizedUtils::update_null_map(result_null_map, src_null_map); |
81 | 73 | } |
82 | 436 | } |
83 | | |
84 | 323 | if (!result_null_map_column) { |
85 | 80 | if (is_column_const(*src)) { |
86 | 0 | return ColumnConst::create( |
87 | 0 | make_nullable(assert_cast<const ColumnConst&>(*src).get_data_column_ptr(), |
88 | 0 | false), |
89 | 0 | input_rows_count); |
90 | 0 | } |
91 | 80 | return ColumnNullable::create(src, ColumnUInt8::create(input_rows_count, 0)); |
92 | 80 | } |
93 | | |
94 | 243 | return ColumnNullable::create(src_not_nullable, result_null_map_column); |
95 | 323 | } |
96 | | |
97 | 635 | bool have_null_column(const Block& block, const ColumnNumbers& args) { |
98 | 807 | return std::ranges::any_of(args, [&block](const auto& elem) { |
99 | 807 | return block.get_by_position(elem).type->is_nullable(); |
100 | 807 | }); |
101 | 635 | } |
102 | | |
103 | 334 | bool have_null_column(const ColumnsWithTypeAndName& args) { |
104 | 339 | return std::ranges::any_of(args, [](const auto& elem) { return elem.type->is_nullable(); }); |
105 | 334 | } |
106 | | |
107 | | inline Status PreparedFunctionImpl::_execute_skipped_constant_deal( |
108 | | FunctionContext* context, Block& block, const ColumnNumbers& args, size_t result, |
109 | 1.01k | size_t input_rows_count, bool dry_run) const { |
110 | 1.01k | bool executed = false; |
111 | 1.01k | RETURN_IF_ERROR(default_implementation_for_nulls(context, block, args, result, input_rows_count, |
112 | 1.01k | dry_run, &executed)); |
113 | 1.01k | if (executed) { |
114 | 325 | return Status::OK(); |
115 | 325 | } |
116 | | |
117 | 688 | if (dry_run) { |
118 | 0 | return execute_impl_dry_run(context, block, args, result, input_rows_count); |
119 | 688 | } else { |
120 | 688 | return execute_impl(context, block, args, result, input_rows_count); |
121 | 688 | } |
122 | 688 | } |
123 | | |
124 | | Status PreparedFunctionImpl::default_implementation_for_constant_arguments( |
125 | | FunctionContext* context, Block& block, const ColumnNumbers& args, size_t result, |
126 | 1.01k | size_t input_rows_count, bool dry_run, bool* executed) const { |
127 | 1.01k | *executed = false; |
128 | 1.01k | ColumnNumbers args_expect_const = get_arguments_that_are_always_constant(); |
129 | | |
130 | | // Check that these arguments are really constant. |
131 | 1.01k | for (auto arg_num : args_expect_const) { |
132 | 100 | if (arg_num < args.size() && |
133 | 100 | !is_column_const(*block.get_by_position(args[arg_num]).column)) { |
134 | 0 | return Status::InvalidArgument("Argument at index {} for function {} must be constant", |
135 | 0 | arg_num, get_name()); |
136 | 0 | } |
137 | 100 | } |
138 | | |
139 | 1.01k | if (args.empty() || !use_default_implementation_for_constants() || |
140 | 1.01k | !VectorizedUtils::all_arguments_are_constant(block, args)) { |
141 | 1.01k | return Status::OK(); |
142 | 1.01k | } |
143 | | |
144 | | // now all columns are const. |
145 | 1 | Block temporary_block; |
146 | | |
147 | 1 | size_t arguments_size = args.size(); |
148 | 3 | for (size_t arg_num = 0; arg_num < arguments_size; ++arg_num) { |
149 | 2 | const ColumnWithTypeAndName& column = block.get_by_position(args[arg_num]); |
150 | | // Columns in const_list --> column_const, others --> nested_column |
151 | | // that's because some functions supposes some specific columns always constant. |
152 | | // If we unpack it, there will be unnecessary cost of virtual judge. |
153 | 2 | if (args_expect_const.end() != |
154 | 2 | std::find(args_expect_const.begin(), args_expect_const.end(), arg_num)) { |
155 | 0 | temporary_block.insert({column.column, column.type, column.name}); |
156 | 2 | } else { |
157 | 2 | temporary_block.insert( |
158 | 2 | {assert_cast<const ColumnConst*>(column.column.get())->get_data_column_ptr(), |
159 | 2 | column.type, column.name}); |
160 | 2 | } |
161 | 2 | } |
162 | | |
163 | 1 | temporary_block.insert(block.get_by_position(result)); |
164 | | |
165 | 1 | ColumnNumbers temporary_argument_numbers(arguments_size); |
166 | 3 | for (size_t i = 0; i < arguments_size; ++i) { |
167 | 2 | temporary_argument_numbers[i] = i; |
168 | 2 | } |
169 | | |
170 | 1 | RETURN_IF_ERROR(_execute_skipped_constant_deal(context, temporary_block, |
171 | 1 | temporary_argument_numbers, arguments_size, |
172 | 1 | temporary_block.rows(), dry_run)); |
173 | | |
174 | 1 | ColumnPtr result_column; |
175 | | /// extremely rare case, when we have function with completely const arguments |
176 | | /// but some of them produced by non is_deterministic function |
177 | 1 | if (temporary_block.get_by_position(arguments_size).column->size() > 1) { |
178 | 0 | result_column = temporary_block.get_by_position(arguments_size).column->clone_resized(1); |
179 | 1 | } else { |
180 | 1 | result_column = temporary_block.get_by_position(arguments_size).column; |
181 | 1 | } |
182 | | // We shuold handle the case where the result column is also a ColumnConst. |
183 | 1 | block.get_by_position(result).column = ColumnConst::create(result_column, input_rows_count); |
184 | 1 | *executed = true; |
185 | 1 | return Status::OK(); |
186 | 1 | } |
187 | | |
188 | | Status PreparedFunctionImpl::default_implementation_for_nulls( |
189 | | FunctionContext* context, Block& block, const ColumnNumbers& args, size_t result, |
190 | 1.01k | size_t input_rows_count, bool dry_run, bool* executed) const { |
191 | 1.01k | *executed = false; |
192 | 1.01k | if (args.empty() || !use_default_implementation_for_nulls()) { |
193 | 372 | return Status::OK(); |
194 | 372 | } |
195 | | |
196 | 984 | if (std::ranges::any_of(args, [&block](const auto& elem) { |
197 | 984 | return block.get_by_position(elem).column->only_null(); |
198 | 984 | })) { |
199 | 13 | block.get_by_position(result).column = |
200 | 13 | block.get_by_position(result).type->create_column_const(input_rows_count, Null()); |
201 | 13 | *executed = true; |
202 | 13 | return Status::OK(); |
203 | 13 | } |
204 | | |
205 | 629 | if (have_null_column(block, args)) { |
206 | 313 | bool need_to_default = need_replace_null_data_to_default(); |
207 | 313 | if (context) { |
208 | 310 | need_to_default &= context->check_overflow_for_decimal(); |
209 | 310 | } |
210 | | // extract nested column from nulls |
211 | 313 | ColumnNumbers new_args; |
212 | 480 | for (auto arg : args) { |
213 | 480 | new_args.push_back(block.columns()); |
214 | 480 | block.insert(block.get_by_position(arg).get_nested(need_to_default)); |
215 | 480 | DCHECK(!block.get_by_position(new_args.back()).column->is_nullable()); |
216 | 480 | } |
217 | 313 | RETURN_IF_ERROR(execute_without_low_cardinality_columns(context, block, new_args, result, |
218 | 313 | block.rows(), dry_run)); |
219 | | // after run with nested, wrap them in null. |
220 | 312 | block.get_by_position(result).column = wrap_in_nullable( |
221 | 312 | block.get_by_position(result).column, block, args, result, input_rows_count); |
222 | | |
223 | 790 | while (!new_args.empty()) { |
224 | 478 | block.erase(new_args.back()); |
225 | 478 | new_args.pop_back(); |
226 | 478 | } |
227 | 312 | *executed = true; |
228 | 312 | return Status::OK(); |
229 | 313 | } |
230 | 316 | return Status::OK(); |
231 | 629 | } |
232 | | |
233 | | Status PreparedFunctionImpl::execute_without_low_cardinality_columns( |
234 | | FunctionContext* context, Block& block, const ColumnNumbers& args, size_t result, |
235 | 1.01k | size_t input_rows_count, bool dry_run) const { |
236 | 1.01k | bool executed = false; |
237 | | |
238 | 1.01k | RETURN_IF_ERROR(default_implementation_for_constant_arguments( |
239 | 1.01k | context, block, args, result, input_rows_count, dry_run, &executed)); |
240 | 1.01k | if (executed) { |
241 | 1 | return Status::OK(); |
242 | 1 | } |
243 | | |
244 | 1.01k | return _execute_skipped_constant_deal(context, block, args, result, input_rows_count, dry_run); |
245 | 1.01k | } |
246 | | |
247 | | Status PreparedFunctionImpl::execute(FunctionContext* context, Block& block, |
248 | | const ColumnNumbers& args, size_t result, |
249 | 701 | size_t input_rows_count, bool dry_run) const { |
250 | 701 | return execute_without_low_cardinality_columns(context, block, args, result, input_rows_count, |
251 | 701 | dry_run); |
252 | 701 | } |
253 | | |
254 | 699 | void FunctionBuilderImpl::check_number_of_arguments(size_t number_of_arguments) const { |
255 | 699 | if (is_variadic()) { |
256 | 237 | return; |
257 | 237 | } |
258 | | |
259 | 462 | size_t expected_number_of_arguments = get_number_of_arguments(); |
260 | | |
261 | 462 | CHECK_EQ(number_of_arguments, expected_number_of_arguments) << fmt::format( |
262 | 0 | "Number of arguments for function {} doesn't match: passed {} , should be {}", |
263 | 0 | get_name(), number_of_arguments, expected_number_of_arguments); |
264 | 462 | } |
265 | | |
266 | | DataTypePtr FunctionBuilderImpl::get_return_type_without_low_cardinality( |
267 | 699 | const ColumnsWithTypeAndName& arguments) const { |
268 | 699 | check_number_of_arguments(arguments.size()); |
269 | | |
270 | 699 | if (!arguments.empty() && use_default_implementation_for_nulls()) { |
271 | 331 | if (have_null_column(arguments)) { |
272 | 328 | ColumnNumbers numbers(arguments.size()); |
273 | 328 | std::iota(numbers.begin(), numbers.end(), 0); |
274 | 328 | auto [nested_block, _] = |
275 | 328 | create_block_with_nested_columns(Block(arguments), numbers, false); |
276 | 328 | auto return_type = get_return_type_impl( |
277 | 328 | ColumnsWithTypeAndName(nested_block.begin(), nested_block.end())); |
278 | 328 | return make_nullable(return_type); |
279 | 328 | } |
280 | 331 | } |
281 | | |
282 | 371 | return get_return_type_impl(arguments); |
283 | 699 | } |
284 | | |
285 | 699 | DataTypePtr FunctionBuilderImpl::get_return_type(const ColumnsWithTypeAndName& arguments) const { |
286 | 699 | if (use_default_implementation_for_low_cardinality_columns()) { |
287 | 681 | ColumnsWithTypeAndName args_without_low_cardinality(arguments); |
288 | | |
289 | 1.20k | for (ColumnWithTypeAndName& arg : args_without_low_cardinality) { |
290 | 1.20k | bool is_const = arg.column && is_column_const(*arg.column); |
291 | 1.20k | if (is_const) { |
292 | 191 | arg.column = assert_cast<const ColumnConst&>(*arg.column).remove_low_cardinality(); |
293 | 191 | } |
294 | 1.20k | } |
295 | | |
296 | 681 | auto type_without_low_cardinality = |
297 | 681 | get_return_type_without_low_cardinality(args_without_low_cardinality); |
298 | | |
299 | 681 | return type_without_low_cardinality; |
300 | 681 | } |
301 | | |
302 | 18 | return get_return_type_without_low_cardinality(arguments); |
303 | 699 | } |
304 | | |
305 | | bool FunctionBuilderImpl::is_date_or_datetime_or_decimal( |
306 | 2 | const DataTypePtr& return_type, const DataTypePtr& func_return_type) const { |
307 | 2 | return (is_date_or_datetime(return_type->is_nullable() |
308 | 2 | ? ((DataTypeNullable*)return_type.get())->get_nested_type() |
309 | 2 | : return_type) && |
310 | 2 | is_date_or_datetime( |
311 | 2 | func_return_type->is_nullable() |
312 | 2 | ? ((DataTypeNullable*)func_return_type.get())->get_nested_type() |
313 | 2 | : func_return_type)) || |
314 | 2 | (is_date_v2_or_datetime_v2( |
315 | 0 | return_type->is_nullable() |
316 | 0 | ? ((DataTypeNullable*)return_type.get())->get_nested_type() |
317 | 0 | : return_type) && |
318 | 0 | is_date_v2_or_datetime_v2( |
319 | 0 | func_return_type->is_nullable() |
320 | 0 | ? ((DataTypeNullable*)func_return_type.get())->get_nested_type() |
321 | 0 | : func_return_type)) || |
322 | | // For some date functions such as str_to_date(string, string), return_type will |
323 | | // be datetimev2 if users enable datev2 but get_return_type(arguments) will still |
324 | | // return datetime. We need keep backward compatibility here. |
325 | 2 | (is_date_v2_or_datetime_v2( |
326 | 0 | return_type->is_nullable() |
327 | 0 | ? ((DataTypeNullable*)return_type.get())->get_nested_type() |
328 | 0 | : return_type) && |
329 | 0 | is_date_or_datetime( |
330 | 0 | func_return_type->is_nullable() |
331 | 0 | ? ((DataTypeNullable*)func_return_type.get())->get_nested_type() |
332 | 0 | : func_return_type)) || |
333 | 2 | (is_date_or_datetime(return_type->is_nullable() |
334 | 0 | ? ((DataTypeNullable*)return_type.get())->get_nested_type() |
335 | 0 | : return_type) && |
336 | 0 | is_date_v2_or_datetime_v2( |
337 | 0 | func_return_type->is_nullable() |
338 | 0 | ? ((DataTypeNullable*)func_return_type.get())->get_nested_type() |
339 | 0 | : func_return_type)) || |
340 | 2 | (is_decimal(return_type->is_nullable() |
341 | 0 | ? ((DataTypeNullable*)return_type.get())->get_nested_type() |
342 | 0 | : return_type) && |
343 | 0 | is_decimal(func_return_type->is_nullable() |
344 | 0 | ? ((DataTypeNullable*)func_return_type.get())->get_nested_type() |
345 | 0 | : func_return_type)); |
346 | 2 | } |
347 | | |
348 | | bool FunctionBuilderImpl::is_array_nested_type_date_or_datetime_or_decimal( |
349 | 0 | const DataTypePtr& return_type, const DataTypePtr& func_return_type) const { |
350 | 0 | auto return_type_ptr = return_type->is_nullable() |
351 | 0 | ? ((DataTypeNullable*)return_type.get())->get_nested_type() |
352 | 0 | : return_type; |
353 | 0 | auto func_return_type_ptr = |
354 | 0 | func_return_type->is_nullable() |
355 | 0 | ? ((DataTypeNullable*)func_return_type.get())->get_nested_type() |
356 | 0 | : func_return_type; |
357 | 0 | if (!(is_array(return_type_ptr) && is_array(func_return_type_ptr))) { |
358 | 0 | return false; |
359 | 0 | } |
360 | 0 | auto nested_nullable_return_type_ptr = |
361 | 0 | (assert_cast<const DataTypeArray*>(return_type_ptr.get()))->get_nested_type(); |
362 | 0 | auto nested_nullable_func_return_type = |
363 | 0 | (assert_cast<const DataTypeArray*>(func_return_type_ptr.get()))->get_nested_type(); |
364 | | // There must be nullable inside array type. |
365 | 0 | if (nested_nullable_return_type_ptr->is_nullable() && |
366 | 0 | nested_nullable_func_return_type->is_nullable()) { |
367 | 0 | auto nested_return_type_ptr = |
368 | 0 | ((DataTypeNullable*)(nested_nullable_return_type_ptr.get()))->get_nested_type(); |
369 | 0 | auto nested_func_return_type_ptr = |
370 | 0 | ((DataTypeNullable*)(nested_nullable_func_return_type.get()))->get_nested_type(); |
371 | 0 | return is_date_or_datetime_or_decimal(nested_return_type_ptr, nested_func_return_type_ptr); |
372 | 0 | } |
373 | 0 | return false; |
374 | 0 | } |
375 | | } // namespace doris::vectorized |