/root/doris/be/src/vec/functions/function.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 | | // This file is copied from |
18 | | // https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/IFunction.h |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | |
23 | | #include <fmt/format.h> |
24 | | #include <glog/logging.h> |
25 | | |
26 | | #include <cstddef> |
27 | | #include <memory> |
28 | | #include <string> |
29 | | #include <utility> |
30 | | |
31 | | #include "common/exception.h" |
32 | | #include "common/logging.h" |
33 | | #include "common/status.h" |
34 | | #include "olap/rowset/segment_v2/inverted_index_iterator.h" // IWYU pragma: keep |
35 | | #include "runtime/define_primitive_type.h" |
36 | | #include "udf/udf.h" |
37 | | #include "vec/core/block.h" |
38 | | #include "vec/core/column_numbers.h" |
39 | | #include "vec/core/column_with_type_and_name.h" |
40 | | #include "vec/core/columns_with_type_and_name.h" |
41 | | #include "vec/core/types.h" |
42 | | #include "vec/data_types/data_type.h" |
43 | | #include "vec/data_types/data_type_array.h" |
44 | | #include "vec/data_types/data_type_map.h" |
45 | | #include "vec/data_types/data_type_nullable.h" |
46 | | #include "vec/data_types/data_type_struct.h" |
47 | | |
48 | | namespace doris::vectorized { |
49 | | |
50 | | struct FunctionAttr { |
51 | | bool new_version_unix_timestamp {false}; |
52 | | }; |
53 | | |
54 | | #define RETURN_REAL_TYPE_FOR_DATEV2_FUNCTION(TYPE) \ |
55 | 37 | bool is_nullable = false; \ |
56 | 37 | bool is_datev2 = false; \ |
57 | 65 | for (auto it : arguments) { \ |
58 | 65 | is_nullable = is_nullable || it.type->is_nullable(); \ |
59 | 65 | is_datev2 = is_datev2 || it.type->get_primitive_type() == TYPE_DATEV2 || \ |
60 | 65 | it.type->get_primitive_type() == TYPE_DATETIMEV2; \ |
61 | 65 | } \ |
62 | 37 | return is_nullable || !is_datev2 \ |
63 | 37 | ? make_nullable( \ |
64 | 31 | std::make_shared<typename PrimitiveTypeTraits<TYPE>::DataType>()) \ |
65 | 37 | : std::make_shared<typename PrimitiveTypeTraits<TYPE>::DataType>(); |
66 | | |
67 | | #define SET_NULLMAP_IF_FALSE(EXPR) \ |
68 | | if (!EXPR) [[unlikely]] { \ |
69 | | null_map[i] = true; \ |
70 | | } |
71 | | |
72 | | class Field; |
73 | | class VExpr; |
74 | | |
75 | | // Only use dispose the variadic argument |
76 | | template <typename T> |
77 | | auto has_variadic_argument_types(T&& arg) -> decltype(T::get_variadic_argument_types()) {}; |
78 | | void has_variadic_argument_types(...); |
79 | | |
80 | | template <typename T> |
81 | | concept HasGetVariadicArgumentTypesImpl = requires(T t) { |
82 | | { t.get_variadic_argument_types_impl() } -> std::same_as<DataTypes>; |
83 | | }; |
84 | | |
85 | | bool have_null_column(const Block& block, const ColumnNumbers& args); |
86 | | bool have_null_column(const ColumnsWithTypeAndName& args); |
87 | | |
88 | | /// The simplest executable object. |
89 | | /// Motivation: |
90 | | /// * Prepare something heavy once before main execution loop instead of doing it for each block. |
91 | | /// * Provide const interface for IFunctionBase (later). |
92 | | class IPreparedFunction { |
93 | | public: |
94 | 99.7k | virtual ~IPreparedFunction() = default; |
95 | | |
96 | | /// Get the main function name. |
97 | | virtual String get_name() const = 0; |
98 | | |
99 | | virtual Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
100 | | uint32_t result, size_t input_rows_count) const = 0; |
101 | | }; |
102 | | |
103 | | using PreparedFunctionPtr = std::shared_ptr<IPreparedFunction>; |
104 | | |
105 | | class PreparedFunctionImpl : public IPreparedFunction { |
106 | | public: |
107 | | Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
108 | | uint32_t result, size_t input_rows_count) const final; |
109 | | |
110 | | /** If the function have non-zero number of arguments, |
111 | | * and if all arguments are constant, that we could automatically provide default implementation: |
112 | | * arguments are converted to ordinary columns with single value which is not const, then function is executed as usual, |
113 | | * and then the result is converted to constant column. |
114 | | */ |
115 | 106k | virtual bool use_default_implementation_for_constants() const { return true; } |
116 | | |
117 | | /** If use_default_implementation_for_nulls() is true, after execute the function, |
118 | | * whether need to replace the nested data of null data to the default value. |
119 | | * E.g. for binary arithmetic exprs, need return true to avoid false overflow. |
120 | | */ |
121 | 0 | virtual bool need_replace_null_data_to_default() const { return false; } |
122 | | |
123 | | protected: |
124 | | virtual Status execute_impl(FunctionContext* context, Block& block, |
125 | | const ColumnNumbers& arguments, uint32_t result, |
126 | | size_t input_rows_count) const = 0; |
127 | | |
128 | | /** Default implementation in presence of Nullable arguments or NULL constants as arguments is the following: |
129 | | * if some of arguments are NULL constants then return NULL constant, |
130 | | * if some of arguments are Nullable, then execute function as usual for block, |
131 | | * where Nullable columns are substituted with nested columns (they have arbitrary values in rows corresponding to NULL value) |
132 | | * and wrap result in Nullable column where NULLs are in all rows where any of arguments are NULL. |
133 | | */ |
134 | 0 | virtual bool use_default_implementation_for_nulls() const { return true; } |
135 | | |
136 | 0 | virtual bool skip_return_type_check() const { return false; } |
137 | | |
138 | | /** Some arguments could remain constant during this implementation. |
139 | | * Every argument required const must write here and no checks elsewhere. |
140 | | */ |
141 | 0 | virtual ColumnNumbers get_arguments_that_are_always_constant() const { return {}; } |
142 | | |
143 | | private: |
144 | | Status default_implementation_for_nulls(FunctionContext* context, Block& block, |
145 | | const ColumnNumbers& args, uint32_t result, |
146 | | size_t input_rows_count, bool* executed) const; |
147 | | Status default_implementation_for_constant_arguments(FunctionContext* context, Block& block, |
148 | | const ColumnNumbers& args, uint32_t result, |
149 | | size_t input_rows_count, |
150 | | bool* executed) const; |
151 | | Status default_execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
152 | | uint32_t result, size_t input_rows_count) const; |
153 | | Status _execute_skipped_constant_deal(FunctionContext* context, Block& block, |
154 | | const ColumnNumbers& args, uint32_t result, |
155 | | size_t input_rows_count) const; |
156 | | }; |
157 | | |
158 | | /// Function with known arguments and return type. |
159 | | class IFunctionBase { |
160 | | public: |
161 | 114k | virtual ~IFunctionBase() = default; |
162 | | |
163 | | /// Get the main function name. |
164 | | virtual String get_name() const = 0; |
165 | | |
166 | | virtual const DataTypes& get_argument_types() const = 0; |
167 | | virtual const DataTypePtr& get_return_type() const = 0; |
168 | | |
169 | | /// Do preparations and return executable. |
170 | | /// sample_block should contain data types of arguments and values of constants, if relevant. |
171 | | virtual PreparedFunctionPtr prepare(FunctionContext* context, const Block& sample_block, |
172 | | const ColumnNumbers& arguments, uint32_t result) const = 0; |
173 | | |
174 | | /// Override this when function need to store state in the `FunctionContext`, or do some |
175 | | /// preparation work according to information from `FunctionContext`. |
176 | 70.0k | virtual Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) { |
177 | 70.0k | return Status::OK(); |
178 | 70.0k | } |
179 | | |
180 | | Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
181 | 97.4k | uint32_t result, size_t input_rows_count) const { |
182 | 97.4k | try { |
183 | 97.4k | return prepare(context, block, arguments, result) |
184 | 97.4k | ->execute(context, block, arguments, result, input_rows_count); |
185 | 97.4k | } catch (const Exception& e) { |
186 | 1 | return e.to_status(); |
187 | 1 | } |
188 | 97.4k | } |
189 | | |
190 | | virtual Status evaluate_inverted_index( |
191 | | const ColumnsWithTypeAndName& arguments, |
192 | | const std::vector<vectorized::IndexFieldNameAndTypePair>& data_type_with_names, |
193 | | std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows, |
194 | 0 | segment_v2::InvertedIndexResultBitmap& bitmap_result) const { |
195 | 0 | return Status::OK(); |
196 | 0 | } |
197 | | |
198 | | /// Do cleaning work when function is finished, i.e., release state variables in the |
199 | | /// `FunctionContext` which are registered in `prepare` phase. |
200 | 50.8k | virtual Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) { |
201 | 50.8k | return Status::OK(); |
202 | 50.8k | } |
203 | | |
204 | | virtual bool is_use_default_implementation_for_constants() const = 0; |
205 | | |
206 | 0 | virtual bool is_udf_function() const { return false; } |
207 | | |
208 | 0 | virtual bool can_push_down_to_index() const { return false; } |
209 | | |
210 | 0 | virtual bool is_blockable() const { return false; } |
211 | | }; |
212 | | |
213 | | using FunctionBasePtr = std::shared_ptr<IFunctionBase>; |
214 | | |
215 | | /// Creates IFunctionBase from argument types list. |
216 | | class IFunctionBuilder { |
217 | | public: |
218 | 116k | virtual ~IFunctionBuilder() = default; |
219 | | |
220 | | /// Get the main function name. |
221 | | virtual String get_name() const = 0; |
222 | | |
223 | | /// Override and return true if function could take different number of arguments. |
224 | | ///TODO: this function is not actually used now. but in check_number_of_arguments we still need it because for many |
225 | | /// functions we didn't set the correct number of arguments. |
226 | | virtual bool is_variadic() const = 0; |
227 | | |
228 | | /// For non-variadic functions, return number of arguments; otherwise return zero (that should be ignored). |
229 | | virtual size_t get_number_of_arguments() const = 0; |
230 | | |
231 | | /// Throw if number of arguments is incorrect. Default implementation will check only in non-variadic case. |
232 | | virtual void check_number_of_arguments(size_t number_of_arguments) const = 0; |
233 | | |
234 | | /// Check arguments and return IFunctionBase. |
235 | | virtual FunctionBasePtr build(const ColumnsWithTypeAndName& arguments, |
236 | | const DataTypePtr& return_type) const = 0; |
237 | | |
238 | | /// For higher-order functions (functions, that have lambda expression as at least one argument). |
239 | | /// You pass data types with empty DataTypeFunction for lambda arguments. |
240 | | /// This function will replace it with DataTypeFunction containing actual types. |
241 | | virtual DataTypes get_variadic_argument_types() const = 0; |
242 | | |
243 | | /// Returns indexes of arguments, that must be ColumnConst |
244 | | virtual ColumnNumbers get_arguments_that_are_always_constant() const = 0; |
245 | | }; |
246 | | |
247 | | using FunctionBuilderPtr = std::shared_ptr<IFunctionBuilder>; |
248 | | |
249 | 3 | inline std::string get_types_string(const ColumnsWithTypeAndName& arguments) { |
250 | 3 | std::string types; |
251 | 3 | for (const auto& argument : arguments) { |
252 | 2 | if (!types.empty()) { |
253 | 1 | types += ", "; |
254 | 1 | } |
255 | 2 | types += argument.type->get_name(); |
256 | 2 | } |
257 | 3 | return types; |
258 | 3 | } |
259 | | |
260 | | /// used in function_factory. when we register a function, save a builder. to get a function, to get a builder. |
261 | | /// will use DefaultFunctionBuilder as the default builder in function's registration if we didn't explicitly specify. |
262 | | class FunctionBuilderImpl : public IFunctionBuilder { |
263 | | public: |
264 | | FunctionBasePtr build(const ColumnsWithTypeAndName& arguments, |
265 | 98.1k | const DataTypePtr& return_type) const final { |
266 | 98.1k | if (skip_return_type_check()) { |
267 | 83.8k | return build_impl(arguments, return_type); |
268 | 83.8k | } |
269 | 14.3k | const DataTypePtr& func_return_type = get_return_type(arguments); |
270 | 14.3k | if (func_return_type == nullptr) { |
271 | 1 | throw doris::Exception( |
272 | 1 | ErrorCode::INTERNAL_ERROR, |
273 | 1 | "function return type check failed, function_name={}, " |
274 | 1 | "expect_return_type={}, real_return_type is nullptr, input_arguments={}", |
275 | 1 | get_name(), return_type->get_name(), get_types_string(arguments)); |
276 | 1 | } |
277 | | |
278 | | // check return types equal. |
279 | 14.3k | if (!(return_type->equals(*func_return_type) || |
280 | | // For null constant argument, `get_return_type` would return |
281 | | // Nullable<DataTypeNothing> when `use_default_implementation_for_nulls` is true. |
282 | 14.3k | (return_type->is_nullable() && func_return_type->is_nullable() && |
283 | 11 | ((DataTypeNullable*)func_return_type.get()) |
284 | 6 | ->get_nested_type() |
285 | 6 | ->get_primitive_type() == INVALID_TYPE) || |
286 | 14.3k | is_date_or_datetime_or_decimal(return_type, func_return_type) || |
287 | 14.3k | is_nested_type_date_or_datetime_or_decimal(return_type, func_return_type))) { |
288 | 2 | throw doris::Exception( |
289 | 2 | ErrorCode::INTERNAL_ERROR, |
290 | 2 | "function return type check failed, function_name={}, " |
291 | 2 | "fe plan return type={}, be real return type={}, input_arguments={}", |
292 | 2 | get_name(), return_type->get_name(), func_return_type->get_name(), |
293 | 2 | get_types_string(arguments)); |
294 | 2 | } |
295 | 14.3k | return build_impl(arguments, return_type); |
296 | 14.3k | } |
297 | | |
298 | 10.6k | bool is_variadic() const override { return false; } |
299 | | |
300 | | // Default implementation. Will check only in non-variadic case. |
301 | | void check_number_of_arguments(size_t number_of_arguments) const override; |
302 | | // the return type should be same with what FE plans. |
303 | | // it returns: `get_return_type_impl` if `use_default_implementation_for_nulls` = false |
304 | | // `get_return_type_impl` warpped in NULL if `use_default_implementation_for_nulls` = true and input has NULL |
305 | | DataTypePtr get_return_type(const ColumnsWithTypeAndName& arguments) const; |
306 | | |
307 | 1.01k | DataTypes get_variadic_argument_types() const override { |
308 | 1.01k | return get_variadic_argument_types_impl(); |
309 | 1.01k | } |
310 | | |
311 | 0 | ColumnNumbers get_arguments_that_are_always_constant() const override { return {}; } |
312 | | |
313 | | protected: |
314 | | // Get the result type by argument type. If the function does not apply to these arguments, throw an exception. |
315 | | // the get_return_type_impl and its overrides should only return the nested type if `use_default_implementation_for_nulls` is true. |
316 | | // whether to wrap in nullable type will be automatically decided. |
317 | 14.0k | virtual DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const { |
318 | 14.0k | DataTypes data_types(arguments.size()); |
319 | 52.4k | for (size_t i = 0; i < arguments.size(); ++i) { |
320 | 38.3k | data_types[i] = arguments[i].type; |
321 | 38.3k | } |
322 | 14.0k | return get_return_type_impl(data_types); |
323 | 14.0k | } |
324 | | |
325 | 0 | virtual DataTypePtr get_return_type_impl(const DataTypes& /*arguments*/) const { |
326 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
327 | 0 | "get_return_type is not implemented for {}", get_name()); |
328 | 0 | return nullptr; |
329 | 0 | } |
330 | | |
331 | | /** If use_default_implementation_for_nulls() is true, than change arguments for get_return_type() and build_impl(): |
332 | | * if some of arguments are Nullable(Nothing) then don't call get_return_type(), call build_impl() with return_type = Nullable(Nothing), |
333 | | * if some of arguments are Nullable, then: |
334 | | * - Nullable types are substituted with nested types for get_return_type() function |
335 | | * - WRAP get_return_type() RESULT IN NULLABLE type and pass to build_impl |
336 | | * |
337 | | * Otherwise build returns build_impl(arguments, get_return_type(arguments)); |
338 | | */ |
339 | 0 | virtual bool use_default_implementation_for_nulls() const { return true; } |
340 | | |
341 | 0 | virtual bool skip_return_type_check() const { return false; } |
342 | | |
343 | 0 | virtual bool need_replace_null_data_to_default() const { return false; } |
344 | | |
345 | | /// return a real function object to execute. called in build(...). |
346 | | virtual FunctionBasePtr build_impl(const ColumnsWithTypeAndName& arguments, |
347 | | const DataTypePtr& return_type) const = 0; |
348 | | |
349 | 348 | virtual DataTypes get_variadic_argument_types_impl() const { return {}; } |
350 | | |
351 | | private: |
352 | | bool is_date_or_datetime_or_decimal(const DataTypePtr& return_type, |
353 | | const DataTypePtr& func_return_type) const; |
354 | | bool is_nested_type_date_or_datetime_or_decimal(const DataTypePtr& return_type, |
355 | | const DataTypePtr& func_return_type) const; |
356 | | }; |
357 | | |
358 | | /// Previous function interface. |
359 | | class IFunction : public std::enable_shared_from_this<IFunction>, |
360 | | public FunctionBuilderImpl, |
361 | | public IFunctionBase, |
362 | | public PreparedFunctionImpl { |
363 | | public: |
364 | | String get_name() const override = 0; |
365 | | |
366 | | /// Notice: We should not change the column in the block, because the column may be shared by multiple expressions or exec nodes. |
367 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
368 | | uint32_t result, size_t input_rows_count) const override = 0; |
369 | | |
370 | | /// Override this functions to change default implementation behavior. See details in IMyFunction. |
371 | 35.6k | bool use_default_implementation_for_nulls() const override { return true; } |
372 | | |
373 | 14.3k | bool skip_return_type_check() const override { return false; } |
374 | | |
375 | 8.74k | bool need_replace_null_data_to_default() const override { return false; } |
376 | | |
377 | | /// all constancy check should use this function to do automatically |
378 | 23.1k | ColumnNumbers get_arguments_that_are_always_constant() const override { return {}; } |
379 | | |
380 | 35 | bool is_use_default_implementation_for_constants() const override { |
381 | 35 | return use_default_implementation_for_constants(); |
382 | 35 | } |
383 | | |
384 | | using PreparedFunctionImpl::execute; |
385 | | using FunctionBuilderImpl::get_return_type_impl; |
386 | | using FunctionBuilderImpl::get_variadic_argument_types_impl; |
387 | | using FunctionBuilderImpl::get_return_type; |
388 | | |
389 | | [[noreturn]] PreparedFunctionPtr prepare(FunctionContext* context, |
390 | | const Block& /*sample_block*/, |
391 | | const ColumnNumbers& /*arguments*/, |
392 | 0 | uint32_t /*result*/) const final { |
393 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
394 | 0 | "prepare is not implemented for IFunction {}", get_name()); |
395 | 0 | __builtin_unreachable(); |
396 | 0 | } |
397 | | |
398 | 26.4k | Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
399 | 26.4k | return Status::OK(); |
400 | 26.4k | } |
401 | | |
402 | 0 | [[noreturn]] const DataTypes& get_argument_types() const final { |
403 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
404 | 0 | "get_argument_types is not implemented for IFunction {}", |
405 | 0 | get_name()); |
406 | 0 | __builtin_unreachable(); |
407 | 0 | } |
408 | | |
409 | 0 | [[noreturn]] const DataTypePtr& get_return_type() const final { |
410 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
411 | 0 | "get_return_type is not implemented for IFunction {}", get_name()); |
412 | 0 | __builtin_unreachable(); |
413 | 0 | } |
414 | | |
415 | | protected: |
416 | | FunctionBasePtr build_impl(const ColumnsWithTypeAndName& /*arguments*/, |
417 | 0 | const DataTypePtr& /*return_type*/) const final { |
418 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
419 | 0 | "build_impl is not implemented for IFunction {}", get_name()); |
420 | 0 | __builtin_unreachable(); |
421 | 0 | return {}; |
422 | 0 | } |
423 | | }; |
424 | | |
425 | | /* |
426 | | * when we register a function which didn't specify its base(i.e. inherited from IFunction), actually we use this as a wrapper. |
427 | | * it saves real implementation as `function`. |
428 | | */ |
429 | | class DefaultFunction final : public IFunctionBase { |
430 | | public: |
431 | | DefaultFunction(std::shared_ptr<IFunction> function_, DataTypes arguments_, |
432 | | DataTypePtr return_type_) |
433 | 14.3k | : function(std::move(function_)), |
434 | 14.3k | arguments(std::move(arguments_)), |
435 | 14.3k | return_type(std::move(return_type_)) {} |
436 | | |
437 | 0 | String get_name() const override { return function->get_name(); } |
438 | | |
439 | 0 | const DataTypes& get_argument_types() const override { return arguments; } |
440 | 1 | const DataTypePtr& get_return_type() const override { return return_type; } |
441 | | |
442 | | // return a default wrapper for IFunction. |
443 | | PreparedFunctionPtr prepare(FunctionContext* context, const Block& /*sample_block*/, |
444 | | const ColumnNumbers& /*arguments*/, |
445 | 14.2k | uint32_t /*result*/) const override { |
446 | 14.2k | return function; |
447 | 14.2k | } |
448 | | |
449 | 28.2k | Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
450 | 28.2k | return function->open(context, scope); |
451 | 28.2k | } |
452 | | |
453 | 28.1k | Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
454 | 28.1k | return function->close(context, scope); |
455 | 28.1k | } |
456 | | |
457 | | Status evaluate_inverted_index( |
458 | | const ColumnsWithTypeAndName& args, |
459 | | const std::vector<vectorized::IndexFieldNameAndTypePair>& data_type_with_names, |
460 | | std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows, |
461 | 0 | segment_v2::InvertedIndexResultBitmap& bitmap_result) const override { |
462 | 0 | return function->evaluate_inverted_index(args, data_type_with_names, iterators, num_rows, |
463 | 0 | bitmap_result); |
464 | 0 | } |
465 | | |
466 | 35 | bool is_use_default_implementation_for_constants() const override { |
467 | 35 | return function->is_use_default_implementation_for_constants(); |
468 | 35 | } |
469 | | |
470 | 0 | bool can_push_down_to_index() const override { return function->can_push_down_to_index(); } |
471 | | |
472 | 0 | bool is_blockable() const override { return function->is_blockable(); } |
473 | | |
474 | | private: |
475 | | std::shared_ptr<IFunction> function; |
476 | | DataTypes arguments; |
477 | | DataTypePtr return_type; |
478 | | }; |
479 | | |
480 | | struct simple_function_creator_without_type0 { |
481 | | template <typename AggregateFunctionTemplate, typename... TArgs> |
482 | 0 | static std::shared_ptr<IFunction> create(const DataTypePtr& result_type, TArgs&&... args) { |
483 | 0 | std::unique_ptr<IFunction> result(std::make_unique<AggregateFunctionTemplate>( |
484 | 0 | result_type, std::forward<TArgs>(args)...)); |
485 | 0 | return std::shared_ptr<IFunction>(result.release()); |
486 | 0 | } Unexecuted instantiation: _ZN5doris10vectorized37simple_function_creator_without_type06createINS0_25FunctionArrayAggDecimalV3INS0_27ArrayAggregateImplDecimalV3ILNS0_18AggregateOperationE2ELNS_13PrimitiveTypeE30EEENS0_12NameArraySumEEEJEEESt10shared_ptrINS0_9IFunctionEERKSA_IKNS0_9IDataTypeEEDpOT0_ Unexecuted instantiation: _ZN5doris10vectorized37simple_function_creator_without_type06createINS0_25FunctionArrayAggDecimalV3INS0_27ArrayAggregateImplDecimalV3ILNS0_18AggregateOperationE2ELNS_13PrimitiveTypeE35EEENS0_12NameArraySumEEEJEEESt10shared_ptrINS0_9IFunctionEERKSA_IKNS0_9IDataTypeEEDpOT0_ Unexecuted instantiation: _ZN5doris10vectorized37simple_function_creator_without_type06createINS0_25FunctionArrayAggDecimalV3INS0_27ArrayAggregateImplDecimalV3ILNS0_18AggregateOperationE3ELNS_13PrimitiveTypeE30EEENS0_16NameArrayAverageEEEJEEESt10shared_ptrINS0_9IFunctionEERKSA_IKNS0_9IDataTypeEEDpOT0_ Unexecuted instantiation: _ZN5doris10vectorized37simple_function_creator_without_type06createINS0_25FunctionArrayAggDecimalV3INS0_27ArrayAggregateImplDecimalV3ILNS0_18AggregateOperationE3ELNS_13PrimitiveTypeE35EEENS0_16NameArrayAverageEEEJEEESt10shared_ptrINS0_9IFunctionEERKSA_IKNS0_9IDataTypeEEDpOT0_ Unexecuted instantiation: _ZN5doris10vectorized37simple_function_creator_without_type06createINS0_25FunctionArrayAggDecimalV3INS0_27ArrayAggregateImplDecimalV3ILNS0_18AggregateOperationE4ELNS_13PrimitiveTypeE30EEENS0_16NameArrayProductEEEJEEESt10shared_ptrINS0_9IFunctionEERKSA_IKNS0_9IDataTypeEEDpOT0_ Unexecuted instantiation: _ZN5doris10vectorized37simple_function_creator_without_type06createINS0_25FunctionArrayAggDecimalV3INS0_27ArrayAggregateImplDecimalV3ILNS0_18AggregateOperationE4ELNS_13PrimitiveTypeE35EEENS0_16NameArrayProductEEEJEEESt10shared_ptrINS0_9IFunctionEERKSA_IKNS0_9IDataTypeEEDpOT0_ Unexecuted instantiation: _ZN5doris10vectorized37simple_function_creator_without_type06createINS0_19FunctionArrayCumSumILNS_13PrimitiveTypeE30EEEJEEESt10shared_ptrINS0_9IFunctionEERKS6_IKNS0_9IDataTypeEEDpOT0_ Unexecuted instantiation: _ZN5doris10vectorized37simple_function_creator_without_type06createINS0_19FunctionArrayCumSumILNS_13PrimitiveTypeE35EEEJEEESt10shared_ptrINS0_9IFunctionEERKS6_IKNS0_9IDataTypeEEDpOT0_ |
487 | | }; |
488 | | template <template <PrimitiveType> class FunctionTemplate> |
489 | | struct SimpleFunctionCurryDirectWithResultType0 { |
490 | | template <PrimitiveType ResultType> |
491 | | using T = FunctionTemplate<ResultType>; |
492 | | }; |
493 | | template <PrimitiveType... AllowedTypes> |
494 | | struct simple_function_creator_with_result_type0 { |
495 | | template <typename Class, typename... TArgs> |
496 | | static std::shared_ptr<IFunction> create_base_with_result_type(const DataTypePtr& result_type, |
497 | 0 | TArgs&&... args) { |
498 | 0 | auto create = [&]<PrimitiveType ResultType>() { |
499 | 0 | return simple_function_creator_without_type0::create< |
500 | 0 | typename Class::template T<ResultType>>(result_type, |
501 | 0 | std::forward<TArgs>(args)...); |
502 | 0 | }; Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_17ArraySumDecimalV3EEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlTnS2_vE_clILS2_30EEEDav Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_17ArraySumDecimalV3EEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlTnS2_vE_clILS2_35EEEDav Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_17ArrayAvgDecimalV3EEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlTnS2_vE_clILS2_30EEEDav Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_17ArrayAvgDecimalV3EEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlTnS2_vE_clILS2_35EEEDav Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_21ArrayProductDecimalV3EEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlTnS2_vE_clILS2_30EEEDav Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_21ArrayProductDecimalV3EEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlTnS2_vE_clILS2_35EEEDav Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_19FunctionArrayCumSumEEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlTnS2_vE_clILS2_30EEEDav Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_19FunctionArrayCumSumEEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlTnS2_vE_clILS2_35EEEDav |
503 | 0 | std::shared_ptr<IFunction> result = nullptr; |
504 | 0 | auto type = result_type->get_primitive_type(); |
505 | |
|
506 | 0 | ( |
507 | 0 | [&] { |
508 | 0 | if (type == AllowedTypes) { |
509 | 0 | static_assert(AllowedTypes == TYPE_DECIMAL128I || |
510 | 0 | AllowedTypes == TYPE_DECIMAL256); |
511 | 0 | result = create.template operator()<AllowedTypes>(); |
512 | 0 | } |
513 | 0 | }(), Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_17ArraySumDecimalV3EEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlvE0_clEv Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_17ArraySumDecimalV3EEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlvE_clEv Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_17ArrayAvgDecimalV3EEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlvE0_clEv Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_17ArrayAvgDecimalV3EEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlvE_clEv Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_21ArrayProductDecimalV3EEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlvE0_clEv Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_21ArrayProductDecimalV3EEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlvE_clEv Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_19FunctionArrayCumSumEEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlvE0_clEv Unexecuted instantiation: _ZZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_19FunctionArrayCumSumEEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ENKUlvE_clEv |
514 | 0 | ...); |
515 | |
|
516 | 0 | return result; |
517 | 0 | } Unexecuted instantiation: _ZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_17ArraySumDecimalV3EEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ Unexecuted instantiation: _ZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_17ArrayAvgDecimalV3EEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ Unexecuted instantiation: _ZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_21ArrayProductDecimalV3EEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ Unexecuted instantiation: _ZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE28create_base_with_result_typeINS0_40SimpleFunctionCurryDirectWithResultType0INS0_19FunctionArrayCumSumEEEJEEESt10shared_ptrINS0_9IFunctionEERKS8_IKNS0_9IDataTypeEEDpOT0_ |
518 | | |
519 | | // Create agg function with result type from FE. |
520 | | // Currently only used for decimalv3 sum and avg. |
521 | | template <template <PrimitiveType> class FunctionTemplate> |
522 | 0 | static std::shared_ptr<IFunction> creator_with_result_type(const DataTypePtr& result_type) { |
523 | 0 | return create_base_with_result_type< |
524 | 0 | SimpleFunctionCurryDirectWithResultType0<FunctionTemplate>>(result_type); |
525 | 0 | } Unexecuted instantiation: _ZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE24creator_with_result_typeINS0_17ArraySumDecimalV3EEESt10shared_ptrINS0_9IFunctionEERKS6_IKNS0_9IDataTypeEE Unexecuted instantiation: _ZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE24creator_with_result_typeINS0_17ArrayAvgDecimalV3EEESt10shared_ptrINS0_9IFunctionEERKS6_IKNS0_9IDataTypeEE Unexecuted instantiation: _ZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE24creator_with_result_typeINS0_21ArrayProductDecimalV3EEESt10shared_ptrINS0_9IFunctionEERKS6_IKNS0_9IDataTypeEE Unexecuted instantiation: _ZN5doris10vectorized41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS2_35EEE24creator_with_result_typeINS0_19FunctionArrayCumSumEEESt10shared_ptrINS0_9IFunctionEERKS6_IKNS0_9IDataTypeEE |
526 | | }; |
527 | | |
528 | | class DefaultFunctionBuilder : public FunctionBuilderImpl { |
529 | | public: |
530 | | explicit DefaultFunctionBuilder(std::shared_ptr<IFunction> function_) |
531 | 16.3k | : function(std::move(function_)) {} |
532 | | |
533 | | // template <template <PrimitiveType> class FunctionTemplate> |
534 | | explicit DefaultFunctionBuilder(DataTypePtr return_type) |
535 | 0 | : _return_type(std::move(return_type)) {} |
536 | | |
537 | | template <template <PrimitiveType> class FunctionTemplate> |
538 | 0 | static FunctionBuilderPtr create_array_agg_function_decimalv3(DataTypePtr return_type) { |
539 | 0 | auto builder = std::make_shared<DefaultFunctionBuilder>(return_type); |
540 | 0 | DataTypePtr real_return_type; |
541 | | // for array_cum_sum, the return type is array, |
542 | | // so here should check nested type |
543 | 0 | if (PrimitiveType::TYPE_ARRAY == return_type->get_primitive_type()) { |
544 | 0 | const DataTypeArray* data_type_array = |
545 | 0 | static_cast<const DataTypeArray*>(remove_nullable(return_type).get()); |
546 | 0 | real_return_type = data_type_array->get_nested_type(); |
547 | 0 | } else { |
548 | 0 | real_return_type = return_type; |
549 | 0 | } |
550 | 0 | builder->function = |
551 | 0 | simple_function_creator_with_result_type0<TYPE_DECIMAL128I, TYPE_DECIMAL256>:: |
552 | 0 | creator_with_result_type<FunctionTemplate>(real_return_type); |
553 | 0 | return builder; |
554 | 0 | } Unexecuted instantiation: _ZN5doris10vectorized22DefaultFunctionBuilder35create_array_agg_function_decimalv3INS0_17ArraySumDecimalV3EEESt10shared_ptrINS0_16IFunctionBuilderEES4_IKNS0_9IDataTypeEE Unexecuted instantiation: _ZN5doris10vectorized22DefaultFunctionBuilder35create_array_agg_function_decimalv3INS0_17ArrayAvgDecimalV3EEESt10shared_ptrINS0_16IFunctionBuilderEES4_IKNS0_9IDataTypeEE Unexecuted instantiation: _ZN5doris10vectorized22DefaultFunctionBuilder35create_array_agg_function_decimalv3INS0_21ArrayProductDecimalV3EEESt10shared_ptrINS0_16IFunctionBuilderEES4_IKNS0_9IDataTypeEE Unexecuted instantiation: _ZN5doris10vectorized22DefaultFunctionBuilder35create_array_agg_function_decimalv3INS0_19FunctionArrayCumSumEEESt10shared_ptrINS0_16IFunctionBuilderEES4_IKNS0_9IDataTypeEE |
555 | | |
556 | 14.3k | void check_number_of_arguments(size_t number_of_arguments) const override { |
557 | 14.3k | function->check_number_of_arguments(number_of_arguments); |
558 | 14.3k | } |
559 | | |
560 | 491 | String get_name() const override { return function->get_name(); } |
561 | 982 | bool is_variadic() const override { return function->is_variadic(); } |
562 | 0 | size_t get_number_of_arguments() const override { return function->get_number_of_arguments(); } |
563 | | |
564 | 0 | ColumnNumbers get_arguments_that_are_always_constant() const override { |
565 | 0 | return function->get_arguments_that_are_always_constant(); |
566 | 0 | } |
567 | | |
568 | | protected: |
569 | 0 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
570 | 0 | return function->get_return_type_impl(arguments); |
571 | 0 | } |
572 | 14.3k | DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override { |
573 | 14.3k | return function->get_return_type_impl(arguments); |
574 | 14.3k | } |
575 | | |
576 | 14.3k | bool use_default_implementation_for_nulls() const override { |
577 | 14.3k | return function->use_default_implementation_for_nulls(); |
578 | 14.3k | } |
579 | | |
580 | 14.3k | bool skip_return_type_check() const override { return function->skip_return_type_check(); } |
581 | | |
582 | 0 | bool need_replace_null_data_to_default() const override { |
583 | 0 | return function->need_replace_null_data_to_default(); |
584 | 0 | } |
585 | | |
586 | | FunctionBasePtr build_impl(const ColumnsWithTypeAndName& arguments, |
587 | 14.3k | const DataTypePtr& return_type) const override { |
588 | 14.3k | DataTypes data_types(arguments.size()); |
589 | 53.1k | for (size_t i = 0; i < arguments.size(); ++i) { |
590 | 38.7k | data_types[i] = arguments[i].type; |
591 | 38.7k | } |
592 | 14.3k | return std::make_shared<DefaultFunction>(function, data_types, return_type); |
593 | 14.3k | } |
594 | | |
595 | 1.01k | DataTypes get_variadic_argument_types_impl() const override { |
596 | 1.01k | return function->get_variadic_argument_types_impl(); |
597 | 1.01k | } |
598 | | |
599 | | private: |
600 | | std::shared_ptr<IFunction> function; |
601 | | DataTypePtr _return_type; |
602 | | }; |
603 | | |
604 | | using FunctionPtr = std::shared_ptr<IFunction>; |
605 | | /** Return ColumnNullable of src, with null map as OR-ed null maps of args columns in blocks. |
606 | | * Or ColumnConst(ColumnNullable) if the result is always NULL or if the result is constant and always not NULL. |
607 | | */ |
608 | | ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args, |
609 | | size_t input_rows_count); |
610 | | |
611 | | } // namespace doris::vectorized |