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