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