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