/root/doris/be/src/vec/functions/function.h
Line | Count | Source (jump to first uncovered line) |
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 | | #include <stddef.h> |
26 | | |
27 | | #include <memory> |
28 | | #include <ostream> |
29 | | #include <string> |
30 | | #include <utility> |
31 | | |
32 | | #include "common/exception.h" |
33 | | #include "common/status.h" |
34 | | #include "olap/rowset/segment_v2/inverted_index_reader.h" |
35 | | #include "udf/udf.h" |
36 | | #include "vec/core/block.h" |
37 | | #include "vec/core/column_numbers.h" |
38 | | #include "vec/core/column_with_type_and_name.h" |
39 | | #include "vec/core/columns_with_type_and_name.h" |
40 | | #include "vec/core/types.h" |
41 | | #include "vec/data_types/data_type.h" |
42 | | #include "vec/data_types/data_type_nullable.h" |
43 | | |
44 | | namespace doris::segment_v2 { |
45 | | struct FuncExprParams; |
46 | | } // namespace doris::segment_v2 |
47 | | |
48 | | namespace doris::vectorized { |
49 | | |
50 | | struct FunctionAttr { |
51 | | bool enable_decimal256 {false}; |
52 | | bool new_is_ip_address_in_range {false}; |
53 | | }; |
54 | | |
55 | | #define RETURN_REAL_TYPE_FOR_DATEV2_FUNCTION(TYPE) \ |
56 | 99 | bool is_nullable = false; \ |
57 | 99 | bool is_datev2 = false; \ |
58 | 150 | for (auto it : arguments) { \ |
59 | 150 | is_nullable = is_nullable || it.type->is_nullable(); \ |
60 | 150 | is_datev2 = is_datev2 || WhichDataType(remove_nullable(it.type)).is_date_v2() || \ |
61 | 150 | WhichDataType(remove_nullable(it.type)).is_date_time_v2(); \ |
62 | 150 | } \ |
63 | 103 | return is_nullable || !is_datev2 ? make_nullable(std::make_shared<TYPE>()) \ |
64 | 99 | : std::make_shared<TYPE>(); |
65 | | |
66 | | #define SET_NULLMAP_IF_FALSE(EXPR) \ |
67 | 0 | if (!EXPR) [[unlikely]] { \ |
68 | 0 | null_map[i] = true; \ |
69 | 0 | } |
70 | | |
71 | | class Field; |
72 | | class VExpr; |
73 | | |
74 | | // Only use dispose the variadic argument |
75 | | template <typename T> |
76 | | auto has_variadic_argument_types(T&& arg) -> decltype(T::get_variadic_argument_types()) {}; |
77 | | void has_variadic_argument_types(...); |
78 | | |
79 | | template <typename T> |
80 | | concept HasGetVariadicArgumentTypesImpl = requires(T t) { |
81 | | { t.get_variadic_argument_types_impl() } -> std::same_as<DataTypes>; |
82 | | }; |
83 | | |
84 | | bool have_null_column(const Block& block, const ColumnNumbers& args); |
85 | | bool have_null_column(const ColumnsWithTypeAndName& args); |
86 | | |
87 | | /// The simplest executable object. |
88 | | /// Motivation: |
89 | | /// * Prepare something heavy once before main execution loop instead of doing it for each block. |
90 | | /// * Provide const interface for IFunctionBase (later). |
91 | | class IPreparedFunction { |
92 | | public: |
93 | 3.36k | virtual ~IPreparedFunction() = default; |
94 | | |
95 | | /// Get the main function name. |
96 | | virtual String get_name() const = 0; |
97 | | |
98 | | virtual Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
99 | | size_t result, size_t input_rows_count, bool dry_run) const = 0; |
100 | | }; |
101 | | |
102 | | using PreparedFunctionPtr = std::shared_ptr<IPreparedFunction>; |
103 | | |
104 | | class PreparedFunctionImpl : public IPreparedFunction { |
105 | | public: |
106 | | Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
107 | | size_t result, size_t input_rows_count, bool dry_run = false) const final; |
108 | | |
109 | | /** If the function have non-zero number of arguments, |
110 | | * and if all arguments are constant, that we could automatically provide default implementation: |
111 | | * arguments are converted to ordinary columns with single value which is not const, then function is executed as usual, |
112 | | * and then the result is converted to constant column. |
113 | | */ |
114 | 1.05k | virtual bool use_default_implementation_for_constants() const { return true; } |
115 | | |
116 | | /** If use_default_implementation_for_nulls() is true, after execute the function, |
117 | | * whether need to replace the nested data of null data to the default value. |
118 | | * E.g. for binary arithmetic exprs, need return true to avoid false overflow. |
119 | | */ |
120 | 0 | virtual bool need_replace_null_data_to_default() const { return false; } |
121 | | |
122 | | protected: |
123 | | virtual Status execute_impl_dry_run(FunctionContext* context, Block& block, |
124 | | const ColumnNumbers& arguments, size_t result, |
125 | 0 | size_t input_rows_count) const { |
126 | 0 | return execute_impl(context, block, arguments, result, input_rows_count); |
127 | 0 | } |
128 | | |
129 | | virtual Status execute_impl(FunctionContext* context, Block& block, |
130 | | const ColumnNumbers& arguments, size_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 | | /** If function arguments has single low cardinality column and all other arguments are constants, call function on nested column. |
142 | | * Otherwise, convert all low cardinality columns to ordinary columns. |
143 | | * Returns ColumnLowCardinality if at least one argument is ColumnLowCardinality. |
144 | | */ |
145 | 0 | virtual bool use_default_implementation_for_low_cardinality_columns() const { return true; } |
146 | | |
147 | | /** Some arguments could remain constant during this implementation. |
148 | | * Every argument required const must write here and no checks elsewhere. |
149 | | */ |
150 | 0 | virtual ColumnNumbers get_arguments_that_are_always_constant() const { return {}; } |
151 | | |
152 | | private: |
153 | | Status default_implementation_for_nulls(FunctionContext* context, Block& block, |
154 | | const ColumnNumbers& args, size_t result, |
155 | | size_t input_rows_count, bool dry_run, |
156 | | bool* executed) const; |
157 | | Status default_implementation_for_constant_arguments(FunctionContext* context, Block& block, |
158 | | const ColumnNumbers& args, size_t result, |
159 | | size_t input_rows_count, bool dry_run, |
160 | | bool* executed) const; |
161 | | Status execute_without_low_cardinality_columns(FunctionContext* context, Block& block, |
162 | | const ColumnNumbers& arguments, size_t result, |
163 | | size_t input_rows_count, bool dry_run) const; |
164 | | Status _execute_skipped_constant_deal(FunctionContext* context, Block& block, |
165 | | const ColumnNumbers& args, size_t result, |
166 | | size_t input_rows_count, bool dry_run) const; |
167 | | }; |
168 | | |
169 | | /// Function with known arguments and return type. |
170 | | class IFunctionBase { |
171 | | public: |
172 | 3.36k | virtual ~IFunctionBase() = default; |
173 | | |
174 | | /// Get the main function name. |
175 | | virtual String get_name() const = 0; |
176 | | |
177 | | virtual const DataTypes& get_argument_types() const = 0; |
178 | | virtual const DataTypePtr& get_return_type() const = 0; |
179 | | |
180 | | /// Do preparations and return executable. |
181 | | /// sample_block should contain data types of arguments and values of constants, if relevant. |
182 | | virtual PreparedFunctionPtr prepare(FunctionContext* context, const Block& sample_block, |
183 | | const ColumnNumbers& arguments, size_t result) const = 0; |
184 | | |
185 | | /// Override this when function need to store state in the `FunctionContext`, or do some |
186 | | /// preparation work according to information from `FunctionContext`. |
187 | 36 | virtual Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) { |
188 | 36 | return Status::OK(); |
189 | 36 | } |
190 | | |
191 | | /// TODO: make const |
192 | | virtual Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
193 | 705 | size_t result, size_t input_rows_count, bool dry_run = false) const { |
194 | 705 | return prepare(context, block, arguments, result) |
195 | 705 | ->execute(context, block, arguments, result, input_rows_count, dry_run); |
196 | 705 | } |
197 | | |
198 | | virtual Status evaluate_inverted_index( |
199 | | const ColumnsWithTypeAndName& arguments, |
200 | | const std::vector<vectorized::IndexFieldNameAndTypePair>& data_type_with_names, |
201 | | std::vector<segment_v2::InvertedIndexIterator*> iterators, uint32_t num_rows, |
202 | 0 | segment_v2::InvertedIndexResultBitmap& bitmap_result) const { |
203 | 0 | return Status::OK(); |
204 | 0 | } |
205 | | |
206 | | /// Do cleaning work when function is finished, i.e., release state variables in the |
207 | | /// `FunctionContext` which are registered in `prepare` phase. |
208 | 1.12k | virtual Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) { |
209 | 1.12k | return Status::OK(); |
210 | 1.12k | } |
211 | | |
212 | | virtual bool is_use_default_implementation_for_constants() const = 0; |
213 | | |
214 | | /// The property of monotonicity for a certain range. |
215 | | struct Monotonicity { |
216 | | bool is_monotonic = false; /// Is the function monotonous (nondecreasing or nonincreasing). |
217 | | bool is_positive = |
218 | | true; /// true if the function is nondecreasing, false, if notincreasing. If is_monotonic = false, then it does not matter. |
219 | | bool is_always_monotonic = |
220 | | false; /// Is true if function is monotonic on the whole input range I |
221 | | |
222 | | Monotonicity(bool is_monotonic_ = false, bool is_positive_ = true, |
223 | | bool is_always_monotonic_ = false) |
224 | | : is_monotonic(is_monotonic_), |
225 | | is_positive(is_positive_), |
226 | 0 | is_always_monotonic(is_always_monotonic_) {} |
227 | | }; |
228 | | |
229 | | /** Get information about monotonicity on a range of values. Call only if hasInformationAboutMonotonicity. |
230 | | * NULL can be passed as one of the arguments. This means that the corresponding range is unlimited on the left or on the right. |
231 | | */ |
232 | | virtual Monotonicity get_monotonicity_for_range(const IDataType& /*type*/, |
233 | | const Field& /*left*/, |
234 | 0 | const Field& /*right*/) const { |
235 | 0 | throw doris::Exception(ErrorCode::INTERNAL_ERROR, |
236 | 0 | "Function {} has no information about its monotonicity.", |
237 | 0 | get_name()); |
238 | 0 | return Monotonicity {}; |
239 | 0 | } |
240 | | |
241 | 0 | virtual bool can_push_down_to_index() const { return false; } |
242 | | }; |
243 | | |
244 | | using FunctionBasePtr = std::shared_ptr<IFunctionBase>; |
245 | | |
246 | | /// Creates IFunctionBase from argument types list. |
247 | | class IFunctionBuilder { |
248 | | public: |
249 | 5.29k | virtual ~IFunctionBuilder() = default; |
250 | | |
251 | | /// Get the main function name. |
252 | | virtual String get_name() const = 0; |
253 | | |
254 | | /// Override and return true if function could take different number of arguments. |
255 | | virtual bool is_variadic() const = 0; |
256 | | |
257 | | /// For non-variadic functions, return number of arguments; otherwise return zero (that should be ignored). |
258 | | virtual size_t get_number_of_arguments() const = 0; |
259 | | |
260 | | /// Throw if number of arguments is incorrect. Default implementation will check only in non-variadic case. |
261 | | virtual void check_number_of_arguments(size_t number_of_arguments) const = 0; |
262 | | |
263 | | /// Check arguments and return IFunctionBase. |
264 | | virtual FunctionBasePtr build(const ColumnsWithTypeAndName& arguments, |
265 | | const DataTypePtr& return_type) const = 0; |
266 | | |
267 | | /// For higher-order functions (functions, that have lambda expression as at least one argument). |
268 | | /// You pass data types with empty DataTypeFunction for lambda arguments. |
269 | | /// This function will replace it with DataTypeFunction containing actual types. |
270 | | virtual DataTypes get_variadic_argument_types() const = 0; |
271 | | |
272 | | /// Returns indexes of arguments, that must be ColumnConst |
273 | | virtual ColumnNumbers get_arguments_that_are_always_constant() const = 0; |
274 | | }; |
275 | | |
276 | | using FunctionBuilderPtr = std::shared_ptr<IFunctionBuilder>; |
277 | | |
278 | 1 | inline std::string get_types_string(const ColumnsWithTypeAndName& arguments) { |
279 | 1 | std::string types; |
280 | 1 | for (const auto& argument : arguments) { |
281 | 0 | if (!types.empty()) { |
282 | 0 | types += ", "; |
283 | 0 | } |
284 | 0 | types += argument.type->get_name(); |
285 | 0 | } |
286 | 1 | return types; |
287 | 1 | } |
288 | | |
289 | | /// used in function_factory. when we register a function, save a builder. to get a function, to get a builder. |
290 | | /// will use DefaultFunctionBuilder as the default builder in function's registration if we didn't explicitly specify. |
291 | | class FunctionBuilderImpl : public IFunctionBuilder { |
292 | | public: |
293 | | FunctionBasePtr build(const ColumnsWithTypeAndName& arguments, |
294 | 704 | const DataTypePtr& return_type) const final { |
295 | 704 | const DataTypePtr& func_return_type = get_return_type(arguments); |
296 | 704 | if (func_return_type == nullptr) { |
297 | 1 | throw doris::Exception( |
298 | 1 | ErrorCode::INTERNAL_ERROR, |
299 | 1 | "function return type check failed, function_name={}, " |
300 | 1 | "expect_return_type={}, real_return_type is nullptr, input_arguments={}", |
301 | 1 | get_name(), return_type->get_name(), get_types_string(arguments)); |
302 | 1 | } |
303 | | |
304 | | // check return types equal. |
305 | 703 | if (!(return_type->equals(*func_return_type) || |
306 | | // For null constant argument, `get_return_type` would return |
307 | | // Nullable<DataTypeNothing> when `use_default_implementation_for_nulls` is true. |
308 | 703 | (return_type->is_nullable() && func_return_type->is_nullable() && |
309 | 2 | is_nothing(((DataTypeNullable*)func_return_type.get())->get_nested_type())) || |
310 | 703 | is_date_or_datetime_or_decimal(return_type, func_return_type) || |
311 | 703 | is_array_nested_type_date_or_datetime_or_decimal(return_type, func_return_type))) { |
312 | 0 | LOG_WARNING( |
313 | 0 | "function return type check failed, function_name={}, " |
314 | 0 | "expect_return_type={}, real_return_type={}, input_arguments={}", |
315 | 0 | get_name(), return_type->get_name(), func_return_type->get_name(), |
316 | 0 | get_types_string(arguments)); |
317 | 0 | return nullptr; |
318 | 0 | } |
319 | 703 | return build_impl(arguments, return_type); |
320 | 703 | } |
321 | | |
322 | 612 | bool is_variadic() const override { return false; } |
323 | | |
324 | | // Default implementation. Will check only in non-variadic case. |
325 | | void check_number_of_arguments(size_t number_of_arguments) const override; |
326 | | // the return type should be same with what FE plans. |
327 | | // it returns: `get_return_type_impl` if `use_default_implementation_for_nulls` = false |
328 | | // `get_return_type_impl` warpped in NULL if `use_default_implementation_for_nulls` = true and input has NULL |
329 | | DataTypePtr get_return_type(const ColumnsWithTypeAndName& arguments) const; |
330 | | |
331 | 1.03k | DataTypes get_variadic_argument_types() const override { |
332 | 1.03k | return get_variadic_argument_types_impl(); |
333 | 1.03k | } |
334 | | |
335 | 0 | ColumnNumbers get_arguments_that_are_always_constant() const override { return {}; } |
336 | | |
337 | | protected: |
338 | | // Get the result type by argument type. If the function does not apply to these arguments, throw an exception. |
339 | | // the get_return_type_impl and its overrides should only return the nested type if `use_default_implementation_for_nulls` is true. |
340 | | // whether to wrap in nullable type will be automatically decided. |
341 | 550 | virtual DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const { |
342 | 550 | DataTypes data_types(arguments.size()); |
343 | 1.54k | for (size_t i = 0; i < arguments.size(); ++i) { |
344 | 996 | data_types[i] = arguments[i].type; |
345 | 996 | } |
346 | 550 | return get_return_type_impl(data_types); |
347 | 550 | } |
348 | | |
349 | 0 | virtual DataTypePtr get_return_type_impl(const DataTypes& /*arguments*/) const { |
350 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
351 | 0 | "get_return_type is not implemented for {}", get_name()); |
352 | 0 | return nullptr; |
353 | 0 | } |
354 | | |
355 | | /** If use_default_implementation_for_nulls() is true, than change arguments for get_return_type() and build_impl(): |
356 | | * if some of arguments are Nullable(Nothing) then don't call get_return_type(), call build_impl() with return_type = Nullable(Nothing), |
357 | | * if some of arguments are Nullable, then: |
358 | | * - Nullable types are substituted with nested types for get_return_type() function |
359 | | * - WRAP get_return_type() RESULT IN NULLABLE type and pass to build_impl |
360 | | * |
361 | | * Otherwise build returns build_impl(arguments, get_return_type(arguments)); |
362 | | */ |
363 | 0 | virtual bool use_default_implementation_for_nulls() const { return true; } |
364 | | |
365 | 0 | virtual bool need_replace_null_data_to_default() const { return false; } |
366 | | |
367 | | /** If use_default_implementation_for_nulls() is true, than change arguments for get_return_type() and build_impl(). |
368 | | * If function arguments has low cardinality types, convert them to ordinary types. |
369 | | * get_return_type returns ColumnLowCardinality if at least one argument type is ColumnLowCardinality. |
370 | | */ |
371 | 0 | virtual bool use_default_implementation_for_low_cardinality_columns() const { return true; } |
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 | 306 | virtual DataTypes get_variadic_argument_types_impl() const { return DataTypes(); } |
378 | | |
379 | | private: |
380 | | DataTypePtr get_return_type_without_low_cardinality( |
381 | | const ColumnsWithTypeAndName& arguments) const; |
382 | | |
383 | | bool is_date_or_datetime_or_decimal(const DataTypePtr& return_type, |
384 | | const DataTypePtr& func_return_type) const; |
385 | | bool is_array_nested_type_date_or_datetime_or_decimal( |
386 | | const DataTypePtr& return_type, const DataTypePtr& func_return_type) const; |
387 | | }; |
388 | | |
389 | | /// Previous function interface. |
390 | | class IFunction : public std::enable_shared_from_this<IFunction>, |
391 | | public FunctionBuilderImpl, |
392 | | public IFunctionBase, |
393 | | public PreparedFunctionImpl { |
394 | | public: |
395 | | String get_name() const override = 0; |
396 | | |
397 | | /// Notice: We should not change the column in the block, because the column may be shared by multiple expressions or exec nodes. |
398 | | virtual Status execute_impl(FunctionContext* context, Block& block, |
399 | | const ColumnNumbers& arguments, size_t result, |
400 | | size_t input_rows_count) const override = 0; |
401 | | |
402 | | /// Override this functions to change default implementation behavior. See details in IMyFunction. |
403 | 1.10k | bool use_default_implementation_for_nulls() const override { return true; } |
404 | | |
405 | 359 | bool need_replace_null_data_to_default() const override { return false; } |
406 | | |
407 | 686 | bool use_default_implementation_for_low_cardinality_columns() const override { return true; } |
408 | | |
409 | | /// all constancy check should use this function to do automatically |
410 | 971 | ColumnNumbers get_arguments_that_are_always_constant() const override { return {}; } |
411 | | |
412 | 2 | bool is_use_default_implementation_for_constants() const override { |
413 | 2 | return use_default_implementation_for_constants(); |
414 | 2 | } |
415 | | |
416 | | using PreparedFunctionImpl::execute; |
417 | | using PreparedFunctionImpl::execute_impl_dry_run; |
418 | | using FunctionBuilderImpl::get_return_type_impl; |
419 | | using FunctionBuilderImpl::get_variadic_argument_types_impl; |
420 | | using FunctionBuilderImpl::get_return_type; |
421 | | |
422 | | [[noreturn]] PreparedFunctionPtr prepare(FunctionContext* context, |
423 | | const Block& /*sample_block*/, |
424 | | const ColumnNumbers& /*arguments*/, |
425 | 0 | size_t /*result*/) const final { |
426 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
427 | 0 | "prepare is not implemented for IFunction {}", get_name()); |
428 | 0 | __builtin_unreachable(); |
429 | 0 | } |
430 | | |
431 | 1.04k | Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
432 | 1.04k | return Status::OK(); |
433 | 1.04k | } |
434 | | |
435 | 0 | [[noreturn]] const DataTypes& get_argument_types() const final { |
436 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
437 | 0 | "get_argument_types is not implemented for IFunction {}", |
438 | 0 | get_name()); |
439 | 0 | __builtin_unreachable(); |
440 | 0 | } |
441 | | |
442 | 0 | [[noreturn]] const DataTypePtr& get_return_type() const final { |
443 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
444 | 0 | "get_return_type is not implemented for IFunction {}", get_name()); |
445 | 0 | __builtin_unreachable(); |
446 | 0 | } |
447 | | |
448 | | protected: |
449 | | FunctionBasePtr build_impl(const ColumnsWithTypeAndName& /*arguments*/, |
450 | 0 | const DataTypePtr& /*return_type*/) const final { |
451 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
452 | 0 | "build_impl is not implemented for IFunction {}", get_name()); |
453 | 0 | __builtin_unreachable(); |
454 | 0 | return {}; |
455 | 0 | } |
456 | | }; |
457 | | |
458 | | /// Wrappers over IFunction. If we (default)use DefaultFunction as wrapper, all function execution will go through this. |
459 | | |
460 | | class DefaultExecutable final : public PreparedFunctionImpl { |
461 | | public: |
462 | | explicit DefaultExecutable(std::shared_ptr<IFunction> function_) |
463 | 687 | : function(std::move(function_)) {} |
464 | | |
465 | 0 | String get_name() const override { return function->get_name(); } |
466 | | |
467 | | protected: |
468 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
469 | 674 | size_t result, size_t input_rows_count) const final { |
470 | 674 | return function->execute_impl(context, block, arguments, result, input_rows_count); |
471 | 674 | } |
472 | | |
473 | | Status evaluate_inverted_index( |
474 | | const ColumnsWithTypeAndName& arguments, |
475 | | const std::vector<vectorized::IndexFieldNameAndTypePair>& data_type_with_names, |
476 | | std::vector<segment_v2::InvertedIndexIterator*> iterators, uint32_t num_rows, |
477 | 0 | segment_v2::InvertedIndexResultBitmap& bitmap_result) const { |
478 | 0 | return function->evaluate_inverted_index(arguments, data_type_with_names, iterators, |
479 | 0 | num_rows, bitmap_result); |
480 | 0 | } |
481 | | |
482 | | Status execute_impl_dry_run(FunctionContext* context, Block& block, |
483 | | const ColumnNumbers& arguments, size_t result, |
484 | 0 | size_t input_rows_count) const final { |
485 | 0 | return function->execute_impl_dry_run(context, block, arguments, result, input_rows_count); |
486 | 0 | } |
487 | 1.05k | bool use_default_implementation_for_nulls() const final { |
488 | 1.05k | return function->use_default_implementation_for_nulls(); |
489 | 1.05k | } |
490 | 367 | bool need_replace_null_data_to_default() const final { |
491 | 367 | return function->need_replace_null_data_to_default(); |
492 | 367 | } |
493 | 1.05k | bool use_default_implementation_for_constants() const final { |
494 | 1.05k | return function->use_default_implementation_for_constants(); |
495 | 1.05k | } |
496 | 0 | bool use_default_implementation_for_low_cardinality_columns() const final { |
497 | 0 | return function->use_default_implementation_for_low_cardinality_columns(); |
498 | 0 | } |
499 | 1.05k | ColumnNumbers get_arguments_that_are_always_constant() const final { |
500 | 1.05k | return function->get_arguments_that_are_always_constant(); |
501 | 1.05k | } |
502 | | |
503 | | private: |
504 | | std::shared_ptr<IFunction> function; |
505 | | }; |
506 | | |
507 | | /* |
508 | | * when we register a function which didn't specify its base(i.e. inherited from IFunction), actually we use this as a wrapper. |
509 | | * it saves real implementation as `function`. |
510 | | */ |
511 | | class DefaultFunction final : public IFunctionBase { |
512 | | public: |
513 | | DefaultFunction(std::shared_ptr<IFunction> function_, DataTypes arguments_, |
514 | | DataTypePtr return_type_) |
515 | | : function(std::move(function_)), |
516 | | arguments(std::move(arguments_)), |
517 | 685 | return_type(std::move(return_type_)) {} |
518 | | |
519 | 0 | String get_name() const override { return function->get_name(); } |
520 | | |
521 | 0 | const DataTypes& get_argument_types() const override { return arguments; } |
522 | 0 | const DataTypePtr& get_return_type() const override { return return_type; } |
523 | | |
524 | | // return a default wrapper for IFunction. |
525 | | PreparedFunctionPtr prepare(FunctionContext* context, const Block& /*sample_block*/, |
526 | | const ColumnNumbers& /*arguments*/, |
527 | 687 | size_t /*result*/) const override { |
528 | 687 | return std::make_shared<DefaultExecutable>(function); |
529 | 687 | } |
530 | | |
531 | 1.30k | Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
532 | 1.30k | return function->open(context, scope); |
533 | 1.30k | } |
534 | | |
535 | 1.26k | Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
536 | 1.26k | return function->close(context, scope); |
537 | 1.26k | } |
538 | | |
539 | | Status evaluate_inverted_index( |
540 | | const ColumnsWithTypeAndName& args, |
541 | | const std::vector<vectorized::IndexFieldNameAndTypePair>& data_type_with_names, |
542 | | std::vector<segment_v2::InvertedIndexIterator*> iterators, uint32_t num_rows, |
543 | 0 | segment_v2::InvertedIndexResultBitmap& bitmap_result) const override { |
544 | 0 | return function->evaluate_inverted_index(args, data_type_with_names, iterators, num_rows, |
545 | 0 | bitmap_result); |
546 | 0 | } |
547 | | |
548 | | IFunctionBase::Monotonicity get_monotonicity_for_range(const IDataType& type, const Field& left, |
549 | 0 | const Field& right) const override { |
550 | 0 | return function->get_monotonicity_for_range(type, left, right); |
551 | 0 | } |
552 | | |
553 | 2 | bool is_use_default_implementation_for_constants() const override { |
554 | 2 | return function->is_use_default_implementation_for_constants(); |
555 | 2 | } |
556 | | |
557 | 0 | bool can_push_down_to_index() const override { return function->can_push_down_to_index(); } |
558 | | |
559 | | private: |
560 | | std::shared_ptr<IFunction> function; |
561 | | DataTypes arguments; |
562 | | DataTypePtr return_type; |
563 | | }; |
564 | | |
565 | | class DefaultFunctionBuilder : public FunctionBuilderImpl { |
566 | | public: |
567 | | explicit DefaultFunctionBuilder(std::shared_ptr<IFunction> function_) |
568 | 2.61k | : function(std::move(function_)) {} |
569 | | |
570 | 686 | void check_number_of_arguments(size_t number_of_arguments) const override { |
571 | 686 | return function->check_number_of_arguments(number_of_arguments); |
572 | 686 | } |
573 | | |
574 | 341 | String get_name() const override { return function->get_name(); } |
575 | 896 | bool is_variadic() const override { return function->is_variadic(); } |
576 | 0 | size_t get_number_of_arguments() const override { return function->get_number_of_arguments(); } |
577 | | |
578 | 0 | ColumnNumbers get_arguments_that_are_always_constant() const override { |
579 | 0 | return function->get_arguments_that_are_always_constant(); |
580 | 0 | } |
581 | | |
582 | | protected: |
583 | 0 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
584 | 0 | return function->get_return_type_impl(arguments); |
585 | 0 | } |
586 | 686 | DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override { |
587 | 686 | return function->get_return_type_impl(arguments); |
588 | 686 | } |
589 | | |
590 | 685 | bool use_default_implementation_for_nulls() const override { |
591 | 685 | return function->use_default_implementation_for_nulls(); |
592 | 685 | } |
593 | | |
594 | 0 | bool need_replace_null_data_to_default() const override { |
595 | 0 | return function->need_replace_null_data_to_default(); |
596 | 0 | } |
597 | 686 | bool use_default_implementation_for_low_cardinality_columns() const override { |
598 | 686 | return function->use_default_implementation_for_low_cardinality_columns(); |
599 | 686 | } |
600 | | |
601 | | FunctionBasePtr build_impl(const ColumnsWithTypeAndName& arguments, |
602 | 685 | const DataTypePtr& return_type) const override { |
603 | 685 | DataTypes data_types(arguments.size()); |
604 | 1.89k | for (size_t i = 0; i < arguments.size(); ++i) { |
605 | 1.21k | data_types[i] = arguments[i].type; |
606 | 1.21k | } |
607 | 685 | return std::make_shared<DefaultFunction>(function, data_types, return_type); |
608 | 685 | } |
609 | | |
610 | 1.03k | DataTypes get_variadic_argument_types_impl() const override { |
611 | 1.03k | return function->get_variadic_argument_types_impl(); |
612 | 1.03k | } |
613 | | |
614 | | private: |
615 | | std::shared_ptr<IFunction> function; |
616 | | }; |
617 | | |
618 | | using FunctionPtr = std::shared_ptr<IFunction>; |
619 | | |
620 | | /** Return ColumnNullable of src, with null map as OR-ed null maps of args columns in blocks. |
621 | | * Or ColumnConst(ColumnNullable) if the result is always NULL or if the result is constant and always not NULL. |
622 | | */ |
623 | | ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args, |
624 | | size_t result, size_t input_rows_count); |
625 | | |
626 | | #define NUMERIC_TYPE_TO_COLUMN_TYPE(M) \ |
627 | 106 | M(UInt8, ColumnUInt8) \ |
628 | 106 | M(Int8, ColumnInt8) \ |
629 | 104 | M(Int16, ColumnInt16) \ |
630 | 100 | M(Int32, ColumnInt32) \ |
631 | 96 | M(Int64, ColumnInt64) \ |
632 | 84 | M(Int128, ColumnInt128) \ |
633 | 80 | M(Float32, ColumnFloat32) \ |
634 | 78 | M(Float64, ColumnFloat64) |
635 | | |
636 | | #define DECIMAL_TYPE_TO_COLUMN_TYPE(M) \ |
637 | 78 | M(Decimal32, ColumnDecimal<Decimal32>) \ |
638 | 78 | M(Decimal64, ColumnDecimal<Decimal64>) \ |
639 | 78 | M(Decimal128V2, ColumnDecimal<Decimal128V2>) \ |
640 | 76 | M(Decimal128V3, ColumnDecimal<Decimal128V3>) \ |
641 | 74 | M(Decimal256, ColumnDecimal<Decimal256>) |
642 | | |
643 | | #define STRING_TYPE_TO_COLUMN_TYPE(M) \ |
644 | 72 | M(String, ColumnString) \ |
645 | 72 | M(JSONB, ColumnString) |
646 | | |
647 | | #define TIME_TYPE_TO_COLUMN_TYPE(M) \ |
648 | 68 | M(Date, ColumnInt64) \ |
649 | 66 | M(DateTime, ColumnInt64) \ |
650 | 61 | M(DateV2, ColumnUInt32) \ |
651 | 49 | M(DateTimeV2, ColumnUInt64) |
652 | | |
653 | | #define IP_TYPE_TO_COLUMN_TYPE(M) \ |
654 | 44 | M(IPv4, ColumnIPv4) \ |
655 | 44 | M(IPv6, ColumnIPv6) |
656 | | |
657 | | #define COMPLEX_TYPE_TO_COLUMN_TYPE(M) \ |
658 | 44 | M(Array, ColumnArray) \ |
659 | 44 | M(Map, ColumnMap) \ |
660 | 8 | M(Struct, ColumnStruct) \ |
661 | 8 | M(VARIANT, ColumnObject) \ |
662 | 8 | M(BitMap, ColumnBitmap) \ |
663 | 8 | M(HLL, ColumnHLL) \ |
664 | 4 | M(QuantileState, ColumnQuantileState) |
665 | | |
666 | | #define TYPE_TO_BASIC_COLUMN_TYPE(M) \ |
667 | 102 | NUMERIC_TYPE_TO_COLUMN_TYPE(M) \ |
668 | 76 | DECIMAL_TYPE_TO_COLUMN_TYPE(M) \ |
669 | 72 | STRING_TYPE_TO_COLUMN_TYPE(M) \ |
670 | 66 | TIME_TYPE_TO_COLUMN_TYPE(M) \ |
671 | 102 | IP_TYPE_TO_COLUMN_TYPE(M) |
672 | | |
673 | | #define TYPE_TO_COLUMN_TYPE(M) \ |
674 | 102 | TYPE_TO_BASIC_COLUMN_TYPE(M) \ |
675 | 102 | COMPLEX_TYPE_TO_COLUMN_TYPE(M) |
676 | | |
677 | | } // namespace doris::vectorized |