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