/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 | | /// Do preparations and return executable. |
175 | | /// sample_block should contain data types of arguments and values of constants, if relevant. |
176 | | virtual PreparedFunctionPtr prepare(FunctionContext* context, const Block& sample_block, |
177 | | const ColumnNumbers& arguments, uint32_t result) const = 0; |
178 | | |
179 | | /// Override this when function need to store state in the `FunctionContext`, or do some |
180 | | /// preparation work according to information from `FunctionContext`. |
181 | 70.1k | virtual Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) { |
182 | 70.1k | return Status::OK(); |
183 | 70.1k | } |
184 | | |
185 | | Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
186 | 97.4k | uint32_t result, size_t input_rows_count) const { |
187 | | // Some function implementations may not handle the case where input_rows_count is 0 |
188 | | // (e.g., some functions access the 0th row of input columns during execution). |
189 | | // Additionally, some UDF functions may hang if they write 0 rows and then try to read. |
190 | | // Therefore, before executing the function, we first check if input_rows_count is 0. |
191 | | // If it is 0, we directly return an empty result column to avoid executing the function body. |
192 | 97.4k | if (input_rows_count == 0) { |
193 | 1 | block.get_by_position(result).column = |
194 | 1 | block.get_by_position(result).type->create_column(); |
195 | 1 | return Status::OK(); |
196 | 1 | } |
197 | 97.4k | try { |
198 | 97.4k | return prepare(context, block, arguments, result) |
199 | 97.4k | ->execute(context, block, arguments, result, input_rows_count); |
200 | 97.4k | } catch (const Exception& e) { |
201 | 1 | return e.to_status(); |
202 | 1 | } |
203 | 97.4k | } |
204 | | |
205 | | virtual Status evaluate_inverted_index( |
206 | | const ColumnsWithTypeAndName& arguments, |
207 | | const std::vector<vectorized::IndexFieldNameAndTypePair>& data_type_with_names, |
208 | | std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows, |
209 | | const InvertedIndexAnalyzerCtx* analyzer_ctx, |
210 | 0 | segment_v2::InvertedIndexResultBitmap& bitmap_result) const { |
211 | 0 | return Status::OK(); |
212 | 0 | } |
213 | | |
214 | | /// Do cleaning work when function is finished, i.e., release state variables in the |
215 | | /// `FunctionContext` which are registered in `prepare` phase. |
216 | 50.9k | virtual Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) { |
217 | 50.9k | return Status::OK(); |
218 | 50.9k | } |
219 | | |
220 | | virtual bool is_use_default_implementation_for_constants() const = 0; |
221 | | |
222 | 0 | virtual bool is_udf_function() const { return false; } |
223 | | |
224 | 0 | virtual bool can_push_down_to_index() const { return false; } |
225 | | |
226 | 0 | virtual bool is_blockable() const { return false; } |
227 | | }; |
228 | | |
229 | | using FunctionBasePtr = std::shared_ptr<IFunctionBase>; |
230 | | |
231 | | /// Creates IFunctionBase from argument types list. |
232 | | class IFunctionBuilder { |
233 | | public: |
234 | 117k | virtual ~IFunctionBuilder() = default; |
235 | | |
236 | | /// Get the main function name. |
237 | | virtual String get_name() const = 0; |
238 | | |
239 | | /// Override and return true if function could take different number of arguments. |
240 | | ///TODO: this function is not actually used now. but in check_number_of_arguments we still need it because for many |
241 | | /// functions we didn't set the correct number of arguments. |
242 | | virtual bool is_variadic() const = 0; |
243 | | |
244 | | /// For non-variadic functions, return number of arguments; otherwise return zero (that should be ignored). |
245 | | virtual size_t get_number_of_arguments() const = 0; |
246 | | |
247 | | /// Throw if number of arguments is incorrect. Default implementation will check only in non-variadic case. |
248 | | virtual void check_number_of_arguments(size_t number_of_arguments) const = 0; |
249 | | |
250 | | /// Check arguments and return IFunctionBase. |
251 | | virtual FunctionBasePtr build(const ColumnsWithTypeAndName& arguments, |
252 | | const DataTypePtr& return_type) const = 0; |
253 | | |
254 | | /// For higher-order functions (functions, that have lambda expression as at least one argument). |
255 | | /// You pass data types with empty DataTypeFunction for lambda arguments. |
256 | | /// This function will replace it with DataTypeFunction containing actual types. |
257 | | virtual DataTypes get_variadic_argument_types() const = 0; |
258 | | |
259 | | /// Returns indexes of arguments, that must be ColumnConst |
260 | | virtual ColumnNumbers get_arguments_that_are_always_constant() const = 0; |
261 | | }; |
262 | | |
263 | | using FunctionBuilderPtr = std::shared_ptr<IFunctionBuilder>; |
264 | | |
265 | 3 | inline std::string get_types_string(const ColumnsWithTypeAndName& arguments) { |
266 | 3 | std::string types; |
267 | 3 | for (const auto& argument : arguments) { |
268 | 2 | if (!types.empty()) { |
269 | 1 | types += ", "; |
270 | 1 | } |
271 | 2 | types += argument.type->get_name(); |
272 | 2 | } |
273 | 3 | return types; |
274 | 3 | } |
275 | | |
276 | | /// used in function_factory. when we register a function, save a builder. to get a function, to get a builder. |
277 | | /// will use DefaultFunctionBuilder as the default builder in function's registration if we didn't explicitly specify. |
278 | | class FunctionBuilderImpl : public IFunctionBuilder { |
279 | | public: |
280 | | FunctionBasePtr build(const ColumnsWithTypeAndName& arguments, |
281 | 98.1k | const DataTypePtr& return_type) const final { |
282 | 98.1k | if (skip_return_type_check()) { |
283 | 83.8k | return build_impl(arguments, return_type); |
284 | 83.8k | } |
285 | 14.3k | const DataTypePtr& func_return_type = get_return_type(arguments); |
286 | 14.3k | if (func_return_type == nullptr) { |
287 | 1 | throw doris::Exception( |
288 | 1 | ErrorCode::INTERNAL_ERROR, |
289 | 1 | "function return type check failed, function_name={}, " |
290 | 1 | "expect_return_type={}, real_return_type is nullptr, input_arguments={}", |
291 | 1 | get_name(), return_type->get_name(), get_types_string(arguments)); |
292 | 1 | } |
293 | | |
294 | | // check return types equal. |
295 | 14.3k | if (!(return_type->equals(*func_return_type) || |
296 | | // For null constant argument, `get_return_type` would return |
297 | | // Nullable<DataTypeNothing> when `use_default_implementation_for_nulls` is true. |
298 | 14.3k | (return_type->is_nullable() && func_return_type->is_nullable() && |
299 | 36 | ((DataTypeNullable*)func_return_type.get()) |
300 | 31 | ->get_nested_type() |
301 | 31 | ->get_primitive_type() == INVALID_TYPE) || |
302 | 14.3k | is_date_or_datetime_or_decimal(return_type, func_return_type) || |
303 | 14.3k | is_nested_type_date_or_datetime_or_decimal(return_type, func_return_type))) { |
304 | 2 | throw doris::Exception( |
305 | 2 | ErrorCode::INTERNAL_ERROR, |
306 | 2 | "function return type check failed, function_name={}, " |
307 | 2 | "fe plan return type={}, be real return type={}, input_arguments={}", |
308 | 2 | get_name(), return_type->get_name(), func_return_type->get_name(), |
309 | 2 | get_types_string(arguments)); |
310 | 2 | } |
311 | 14.3k | return build_impl(arguments, return_type); |
312 | 14.3k | } |
313 | | |
314 | 10.6k | bool is_variadic() const override { return false; } |
315 | | |
316 | | // Default implementation. Will check only in non-variadic case. |
317 | | void check_number_of_arguments(size_t number_of_arguments) const override; |
318 | | // the return type should be same with what FE plans. |
319 | | // it returns: `get_return_type_impl` if `use_default_implementation_for_nulls` = false |
320 | | // `get_return_type_impl` warpped in NULL if `use_default_implementation_for_nulls` = true and input has NULL |
321 | | DataTypePtr get_return_type(const ColumnsWithTypeAndName& arguments) const; |
322 | | |
323 | 1.17k | DataTypes get_variadic_argument_types() const override { |
324 | 1.17k | return get_variadic_argument_types_impl(); |
325 | 1.17k | } |
326 | | |
327 | 0 | ColumnNumbers get_arguments_that_are_always_constant() const override { return {}; } |
328 | | |
329 | | protected: |
330 | | // Get the result type by argument type. If the function does not apply to these arguments, throw an exception. |
331 | | // the get_return_type_impl and its overrides should only return the nested type if `use_default_implementation_for_nulls` is true. |
332 | | // whether to wrap in nullable type will be automatically decided. |
333 | 14.0k | virtual DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const { |
334 | 14.0k | DataTypes data_types(arguments.size()); |
335 | 52.4k | for (size_t i = 0; i < arguments.size(); ++i) { |
336 | 38.3k | data_types[i] = arguments[i].type; |
337 | 38.3k | } |
338 | 14.0k | return get_return_type_impl(data_types); |
339 | 14.0k | } |
340 | | |
341 | 0 | virtual DataTypePtr get_return_type_impl(const DataTypes& /*arguments*/) const { |
342 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
343 | 0 | "get_return_type is not implemented for {}", get_name()); |
344 | 0 | return nullptr; |
345 | 0 | } |
346 | | |
347 | | /** If use_default_implementation_for_nulls() is true, than change arguments for get_return_type() and build_impl(): |
348 | | * if some of arguments are Nullable(Nothing) then don't call get_return_type(), call build_impl() with return_type = Nullable(Nothing), |
349 | | * if some of arguments are Nullable, then: |
350 | | * - Nullable types are substituted with nested types for get_return_type() function |
351 | | * - WRAP get_return_type() RESULT IN NULLABLE type and pass to build_impl |
352 | | * |
353 | | * Otherwise build returns build_impl(arguments, get_return_type(arguments)); |
354 | | */ |
355 | 0 | virtual bool use_default_implementation_for_nulls() const { return true; } |
356 | | |
357 | 0 | virtual bool skip_return_type_check() const { return false; } |
358 | | |
359 | 0 | virtual bool need_replace_null_data_to_default() const { return false; } |
360 | | |
361 | | /// return a real function object to execute. called in build(...). |
362 | | virtual FunctionBasePtr build_impl(const ColumnsWithTypeAndName& arguments, |
363 | | const DataTypePtr& return_type) const = 0; |
364 | | |
365 | 355 | virtual DataTypes get_variadic_argument_types_impl() const { return {}; } |
366 | | |
367 | | private: |
368 | | bool is_date_or_datetime_or_decimal(const DataTypePtr& return_type, |
369 | | const DataTypePtr& func_return_type) const; |
370 | | bool is_nested_type_date_or_datetime_or_decimal(const DataTypePtr& return_type, |
371 | | const DataTypePtr& func_return_type) const; |
372 | | }; |
373 | | |
374 | | /// Previous function interface. |
375 | | class IFunction : public std::enable_shared_from_this<IFunction>, |
376 | | public FunctionBuilderImpl, |
377 | | public IFunctionBase, |
378 | | public PreparedFunctionImpl { |
379 | | public: |
380 | | String get_name() const override = 0; |
381 | | |
382 | | /// Notice: We should not change the column in the block, because the column may be shared by multiple expressions or exec nodes. |
383 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
384 | | uint32_t result, size_t input_rows_count) const override = 0; |
385 | | |
386 | | /// Override this functions to change default implementation behavior. See details in IMyFunction. |
387 | 35.6k | bool use_default_implementation_for_nulls() const override { return true; } |
388 | | |
389 | 14.3k | bool skip_return_type_check() const override { return false; } |
390 | | |
391 | 8.74k | bool need_replace_null_data_to_default() const override { return false; } |
392 | | |
393 | | /// all constancy check should use this function to do automatically |
394 | 23.1k | ColumnNumbers get_arguments_that_are_always_constant() const override { return {}; } |
395 | | |
396 | 35 | bool is_use_default_implementation_for_constants() const override { |
397 | 35 | return use_default_implementation_for_constants(); |
398 | 35 | } |
399 | | |
400 | | using PreparedFunctionImpl::execute; |
401 | | using FunctionBuilderImpl::get_return_type_impl; |
402 | | using FunctionBuilderImpl::get_variadic_argument_types_impl; |
403 | | using FunctionBuilderImpl::get_return_type; |
404 | | |
405 | | [[noreturn]] PreparedFunctionPtr prepare(FunctionContext* context, |
406 | | const Block& /*sample_block*/, |
407 | | const ColumnNumbers& /*arguments*/, |
408 | 0 | uint32_t /*result*/) const final { |
409 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
410 | 0 | "prepare is not implemented for IFunction {}", get_name()); |
411 | 0 | __builtin_unreachable(); |
412 | 0 | } |
413 | | |
414 | 26.5k | Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
415 | 26.5k | return Status::OK(); |
416 | 26.5k | } |
417 | | |
418 | 0 | [[noreturn]] const DataTypes& get_argument_types() const final { |
419 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
420 | 0 | "get_argument_types is not implemented for IFunction {}", |
421 | 0 | get_name()); |
422 | 0 | __builtin_unreachable(); |
423 | 0 | } |
424 | | |
425 | 0 | [[noreturn]] const DataTypePtr& get_return_type() const final { |
426 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
427 | 0 | "get_return_type is not implemented for IFunction {}", get_name()); |
428 | 0 | __builtin_unreachable(); |
429 | 0 | } |
430 | | |
431 | | protected: |
432 | | FunctionBasePtr build_impl(const ColumnsWithTypeAndName& /*arguments*/, |
433 | 0 | const DataTypePtr& /*return_type*/) const final { |
434 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
435 | 0 | "build_impl is not implemented for IFunction {}", get_name()); |
436 | 0 | __builtin_unreachable(); |
437 | 0 | return {}; |
438 | 0 | } |
439 | | }; |
440 | | |
441 | | /* |
442 | | * when we register a function which didn't specify its base(i.e. inherited from IFunction), actually we use this as a wrapper. |
443 | | * it saves real implementation as `function`. |
444 | | */ |
445 | | class DefaultFunction final : public IFunctionBase { |
446 | | public: |
447 | | DefaultFunction(std::shared_ptr<IFunction> function_, DataTypes arguments_, |
448 | | DataTypePtr return_type_) |
449 | 14.3k | : function(std::move(function_)), |
450 | 14.3k | arguments(std::move(arguments_)), |
451 | 14.3k | return_type(std::move(return_type_)) {} |
452 | | |
453 | 0 | String get_name() const override { return function->get_name(); } |
454 | | |
455 | 0 | const DataTypes& get_argument_types() const override { return arguments; } |
456 | 1 | const DataTypePtr& get_return_type() const override { return return_type; } |
457 | | |
458 | | // return a default wrapper for IFunction. |
459 | | PreparedFunctionPtr prepare(FunctionContext* context, const Block& /*sample_block*/, |
460 | | const ColumnNumbers& /*arguments*/, |
461 | 14.2k | uint32_t /*result*/) const override { |
462 | 14.2k | return function; |
463 | 14.2k | } |
464 | | |
465 | 28.2k | Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
466 | 28.2k | return function->open(context, scope); |
467 | 28.2k | } |
468 | | |
469 | 28.2k | Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
470 | 28.2k | return function->close(context, scope); |
471 | 28.2k | } |
472 | | |
473 | | Status evaluate_inverted_index( |
474 | | const ColumnsWithTypeAndName& args, |
475 | | const std::vector<vectorized::IndexFieldNameAndTypePair>& data_type_with_names, |
476 | | std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows, |
477 | | const InvertedIndexAnalyzerCtx* analyzer_ctx, |
478 | 0 | segment_v2::InvertedIndexResultBitmap& bitmap_result) const override { |
479 | 0 | return function->evaluate_inverted_index(args, data_type_with_names, iterators, num_rows, |
480 | 0 | analyzer_ctx, bitmap_result); |
481 | 0 | } |
482 | | |
483 | 35 | bool is_use_default_implementation_for_constants() const override { |
484 | 35 | return function->is_use_default_implementation_for_constants(); |
485 | 35 | } |
486 | | |
487 | 0 | bool can_push_down_to_index() const override { return function->can_push_down_to_index(); } |
488 | | |
489 | 0 | bool is_blockable() const override { return function->is_blockable(); } |
490 | | |
491 | | private: |
492 | | std::shared_ptr<IFunction> function; |
493 | | DataTypes arguments; |
494 | | DataTypePtr return_type; |
495 | | }; |
496 | | |
497 | | struct simple_function_creator_without_type0 { |
498 | | template <typename AggregateFunctionTemplate, typename... TArgs> |
499 | 0 | static std::shared_ptr<IFunction> create(const DataTypePtr& result_type, TArgs&&... args) { |
500 | 0 | std::unique_ptr<IFunction> result(std::make_unique<AggregateFunctionTemplate>( |
501 | 0 | result_type, std::forward<TArgs>(args)...)); |
502 | 0 | return std::shared_ptr<IFunction>(result.release()); |
503 | 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_ |
504 | | }; |
505 | | template <template <PrimitiveType> class FunctionTemplate> |
506 | | struct SimpleFunctionCurryDirectWithResultType0 { |
507 | | template <PrimitiveType ResultType> |
508 | | using T = FunctionTemplate<ResultType>; |
509 | | }; |
510 | | template <PrimitiveType... AllowedTypes> |
511 | | struct simple_function_creator_with_result_type0 { |
512 | | template <typename Class, typename... TArgs> |
513 | | static std::shared_ptr<IFunction> create_base_with_result_type(const DataTypePtr& result_type, |
514 | 0 | TArgs&&... args) { |
515 | 0 | auto create = [&]<PrimitiveType ResultType>() { |
516 | 0 | return simple_function_creator_without_type0::create< |
517 | 0 | typename Class::template T<ResultType>>(result_type, |
518 | 0 | std::forward<TArgs>(args)...); |
519 | 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 |
520 | 0 | std::shared_ptr<IFunction> result = nullptr; |
521 | 0 | auto type = result_type->get_primitive_type(); |
522 | |
|
523 | 0 | ( |
524 | 0 | [&] { |
525 | 0 | if (type == AllowedTypes) { |
526 | 0 | static_assert(AllowedTypes == TYPE_DECIMAL128I || |
527 | 0 | AllowedTypes == TYPE_DECIMAL256); |
528 | 0 | result = create.template operator()<AllowedTypes>(); |
529 | 0 | } |
530 | 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 |
531 | 0 | ...); |
532 | |
|
533 | 0 | return result; |
534 | 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_ |
535 | | |
536 | | // Create agg function with result type from FE. |
537 | | // Currently only used for decimalv3 sum and avg. |
538 | | template <template <PrimitiveType> class FunctionTemplate> |
539 | 0 | static std::shared_ptr<IFunction> creator_with_result_type(const DataTypePtr& result_type) { |
540 | 0 | return create_base_with_result_type< |
541 | 0 | SimpleFunctionCurryDirectWithResultType0<FunctionTemplate>>(result_type); |
542 | 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 |
543 | | }; |
544 | | |
545 | | class DefaultFunctionBuilder : public FunctionBuilderImpl { |
546 | | public: |
547 | | explicit DefaultFunctionBuilder(std::shared_ptr<IFunction> function_) |
548 | 16.6k | : function(std::move(function_)) {} |
549 | | |
550 | | // template <template <PrimitiveType> class FunctionTemplate> |
551 | | explicit DefaultFunctionBuilder(DataTypePtr return_type) |
552 | 0 | : _return_type(std::move(return_type)) {} |
553 | | |
554 | | template <template <PrimitiveType> class FunctionTemplate> |
555 | 0 | static FunctionBuilderPtr create_array_agg_function_decimalv3(DataTypePtr return_type) { |
556 | 0 | auto builder = std::make_shared<DefaultFunctionBuilder>(return_type); |
557 | 0 | DataTypePtr real_return_type; |
558 | | // for array_cum_sum, the return type is array, |
559 | | // so here should check nested type |
560 | 0 | if (PrimitiveType::TYPE_ARRAY == return_type->get_primitive_type()) { |
561 | 0 | const DataTypeArray* data_type_array = |
562 | 0 | static_cast<const DataTypeArray*>(remove_nullable(return_type).get()); |
563 | 0 | real_return_type = data_type_array->get_nested_type(); |
564 | 0 | } else { |
565 | 0 | real_return_type = return_type; |
566 | 0 | } |
567 | 0 | builder->function = |
568 | 0 | simple_function_creator_with_result_type0<TYPE_DECIMAL128I, TYPE_DECIMAL256>:: |
569 | 0 | creator_with_result_type<FunctionTemplate>(real_return_type); |
570 | 0 | return builder; |
571 | 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 |
572 | | |
573 | 14.3k | void check_number_of_arguments(size_t number_of_arguments) const override { |
574 | 14.3k | function->check_number_of_arguments(number_of_arguments); |
575 | 14.3k | } |
576 | | |
577 | 500 | String get_name() const override { return function->get_name(); } |
578 | 1.13k | bool is_variadic() const override { return function->is_variadic(); } |
579 | 0 | size_t get_number_of_arguments() const override { return function->get_number_of_arguments(); } |
580 | | |
581 | 0 | ColumnNumbers get_arguments_that_are_always_constant() const override { |
582 | 0 | return function->get_arguments_that_are_always_constant(); |
583 | 0 | } |
584 | | |
585 | | protected: |
586 | 0 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
587 | 0 | return function->get_return_type_impl(arguments); |
588 | 0 | } |
589 | 14.3k | DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override { |
590 | 14.3k | return function->get_return_type_impl(arguments); |
591 | 14.3k | } |
592 | | |
593 | 14.3k | bool use_default_implementation_for_nulls() const override { |
594 | 14.3k | return function->use_default_implementation_for_nulls(); |
595 | 14.3k | } |
596 | | |
597 | 14.3k | bool skip_return_type_check() const override { return function->skip_return_type_check(); } |
598 | | |
599 | 0 | bool need_replace_null_data_to_default() const override { |
600 | 0 | return function->need_replace_null_data_to_default(); |
601 | 0 | } |
602 | | |
603 | | FunctionBasePtr build_impl(const ColumnsWithTypeAndName& arguments, |
604 | 14.3k | const DataTypePtr& return_type) const override { |
605 | 14.3k | DataTypes data_types(arguments.size()); |
606 | 53.1k | for (size_t i = 0; i < arguments.size(); ++i) { |
607 | 38.8k | data_types[i] = arguments[i].type; |
608 | 38.8k | } |
609 | 14.3k | return std::make_shared<DefaultFunction>(function, data_types, return_type); |
610 | 14.3k | } |
611 | | |
612 | 1.16k | DataTypes get_variadic_argument_types_impl() const override { |
613 | 1.16k | return function->get_variadic_argument_types_impl(); |
614 | 1.16k | } |
615 | | |
616 | | private: |
617 | | std::shared_ptr<IFunction> function; |
618 | | DataTypePtr _return_type; |
619 | | }; |
620 | | |
621 | | using FunctionPtr = std::shared_ptr<IFunction>; |
622 | | /** Return ColumnNullable of src, with null map as OR-ed null maps of args columns in blocks. |
623 | | * Or ColumnConst(ColumnNullable) if the result is always NULL or if the result is constant and always not NULL. |
624 | | */ |
625 | | ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args, |
626 | | size_t input_rows_count); |
627 | | |
628 | | } // namespace doris::vectorized |