/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/rowset/segment_v2/inverted_index_iterator.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 | 49 | bool is_nullable = false; \ |
56 | 49 | bool is_datev2 = false; \ |
57 | 160 | for (auto it : arguments) { \ |
58 | 160 | is_nullable = is_nullable || it.type->is_nullable(); \ |
59 | 160 | is_datev2 = is_datev2 || it.type->get_primitive_type() == TYPE_DATEV2 || \ |
60 | 160 | it.type->get_primitive_type() == TYPE_DATETIMEV2; \ |
61 | 160 | } \ |
62 | 106 | return is_nullable || !is_datev2 \ |
63 | 104 | ? make_nullable( \ |
64 | 104 | std::make_shared<typename PrimitiveTypeTraits<TYPE>::DataType>()) \ |
65 | 49 | : std::make_shared<typename PrimitiveTypeTraits<TYPE>::DataType>(); |
66 | | |
67 | | #define SET_NULLMAP_IF_FALSE(EXPR) \ |
68 | 0 | if (!EXPR) [[unlikely]] { \ |
69 | 0 | null_map[i] = true; \ |
70 | 0 | } |
71 | | |
72 | | class Field; |
73 | | class VExpr; |
74 | | |
75 | | // Only use dispose the variadic argument |
76 | | template <typename T> |
77 | | auto has_variadic_argument_types(T&& arg) -> decltype(T::get_variadic_argument_types()) {}; |
78 | | void has_variadic_argument_types(...); |
79 | | |
80 | | template <typename T> |
81 | | concept HasGetVariadicArgumentTypesImpl = requires(T t) { |
82 | | { t.get_variadic_argument_types_impl() } -> std::same_as<DataTypes>; |
83 | | }; |
84 | | |
85 | | bool have_null_column(const Block& block, const ColumnNumbers& args); |
86 | | bool have_null_column(const ColumnsWithTypeAndName& args); |
87 | | |
88 | | /// The simplest executable object. |
89 | | /// Motivation: |
90 | | /// * Prepare something heavy once before main execution loop instead of doing it for each block. |
91 | | /// * Provide const interface for IFunctionBase (later). |
92 | | class IPreparedFunction { |
93 | | public: |
94 | 279k | virtual ~IPreparedFunction() = default; |
95 | | |
96 | | /// Get the main function name. |
97 | | virtual String get_name() const = 0; |
98 | | |
99 | | virtual Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
100 | | uint32_t result, size_t input_rows_count, bool dry_run) const = 0; |
101 | | }; |
102 | | |
103 | | using PreparedFunctionPtr = std::shared_ptr<IPreparedFunction>; |
104 | | |
105 | | class PreparedFunctionImpl : public IPreparedFunction { |
106 | | public: |
107 | | Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
108 | | uint32_t result, size_t input_rows_count, bool dry_run = false) const final; |
109 | | |
110 | | /** If the function have non-zero number of arguments, |
111 | | * and if all arguments are constant, that we could automatically provide default implementation: |
112 | | * arguments are converted to ordinary columns with single value which is not const, then function is executed as usual, |
113 | | * and then the result is converted to constant column. |
114 | | */ |
115 | 270k | virtual bool use_default_implementation_for_constants() const { return true; } |
116 | | |
117 | | /** If use_default_implementation_for_nulls() is true, after execute the function, |
118 | | * whether need to replace the nested data of null data to the default value. |
119 | | * E.g. for binary arithmetic exprs, need return true to avoid false overflow. |
120 | | */ |
121 | 0 | virtual bool need_replace_null_data_to_default() const { return false; } |
122 | | |
123 | | protected: |
124 | | virtual Status execute_impl_dry_run(FunctionContext* context, Block& block, |
125 | | const ColumnNumbers& arguments, uint32_t result, |
126 | 0 | size_t input_rows_count) const { |
127 | 0 | return execute_impl(context, block, arguments, result, input_rows_count); |
128 | 0 | } |
129 | | |
130 | | virtual Status execute_impl(FunctionContext* context, Block& block, |
131 | | const ColumnNumbers& arguments, uint32_t result, |
132 | | size_t input_rows_count) const = 0; |
133 | | |
134 | | /** Default implementation in presence of Nullable arguments or NULL constants as arguments is the following: |
135 | | * if some of arguments are NULL constants then return NULL constant, |
136 | | * if some of arguments are Nullable, then execute function as usual for block, |
137 | | * where Nullable columns are substituted with nested columns (they have arbitrary values in rows corresponding to NULL value) |
138 | | * and wrap result in Nullable column where NULLs are in all rows where any of arguments are NULL. |
139 | | */ |
140 | 0 | virtual bool use_default_implementation_for_nulls() const { return true; } |
141 | | |
142 | 0 | virtual bool skip_return_type_check() const { return false; } |
143 | | |
144 | | /** Some arguments could remain constant during this implementation. |
145 | | * Every argument required const must write here and no checks elsewhere. |
146 | | */ |
147 | 0 | virtual ColumnNumbers get_arguments_that_are_always_constant() const { return {}; } |
148 | | |
149 | | private: |
150 | | Status default_implementation_for_nulls(FunctionContext* context, Block& block, |
151 | | const ColumnNumbers& args, uint32_t result, |
152 | | size_t input_rows_count, bool dry_run, |
153 | | bool* executed) const; |
154 | | Status default_implementation_for_constant_arguments(FunctionContext* context, Block& block, |
155 | | const ColumnNumbers& args, uint32_t result, |
156 | | size_t input_rows_count, bool dry_run, |
157 | | bool* executed) const; |
158 | | Status execute_without_low_cardinality_columns(FunctionContext* context, Block& block, |
159 | | const ColumnNumbers& arguments, uint32_t result, |
160 | | size_t input_rows_count, bool dry_run) const; |
161 | | Status _execute_skipped_constant_deal(FunctionContext* context, Block& block, |
162 | | const ColumnNumbers& args, uint32_t result, |
163 | | size_t input_rows_count, bool dry_run) const; |
164 | | }; |
165 | | |
166 | | /// Function with known arguments and return type. |
167 | | class IFunctionBase { |
168 | | public: |
169 | 279k | virtual ~IFunctionBase() = default; |
170 | | |
171 | | /// Get the main function name. |
172 | | virtual String get_name() const = 0; |
173 | | |
174 | | virtual const DataTypes& get_argument_types() const = 0; |
175 | | virtual const DataTypePtr& get_return_type() const = 0; |
176 | | |
177 | | /// Do preparations and return executable. |
178 | | /// sample_block should contain data types of arguments and values of constants, if relevant. |
179 | | virtual PreparedFunctionPtr prepare(FunctionContext* context, const Block& sample_block, |
180 | | const ColumnNumbers& arguments, uint32_t result) const = 0; |
181 | | |
182 | | /// Override this when function need to store state in the `FunctionContext`, or do some |
183 | | /// preparation work according to information from `FunctionContext`. |
184 | 496k | virtual Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) { |
185 | 496k | return Status::OK(); |
186 | 496k | } |
187 | | |
188 | | Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
189 | 263k | uint32_t result, size_t input_rows_count, bool dry_run = false) const { |
190 | 263k | try { |
191 | 263k | return prepare(context, block, arguments, result) |
192 | 263k | ->execute(context, block, arguments, result, input_rows_count, dry_run); |
193 | 263k | } catch (const Exception& e) { |
194 | 4.46k | return e.to_status(); |
195 | 4.46k | } |
196 | 263k | } |
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::IndexIterator*> 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 | 464k | virtual Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) { |
209 | 464k | return Status::OK(); |
210 | 464k | } |
211 | | |
212 | | virtual bool is_use_default_implementation_for_constants() const = 0; |
213 | | |
214 | 0 | virtual bool is_udf_function() const { return false; } |
215 | | |
216 | 0 | virtual bool can_push_down_to_index() 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 | 281k | 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 | | virtual bool is_variadic() const = 0; |
231 | | |
232 | | /// For non-variadic functions, return number of arguments; otherwise return zero (that should be ignored). |
233 | | virtual size_t get_number_of_arguments() const = 0; |
234 | | |
235 | | /// Throw if number of arguments is incorrect. Default implementation will check only in non-variadic case. |
236 | | virtual void check_number_of_arguments(size_t number_of_arguments) const = 0; |
237 | | |
238 | | /// Check arguments and return IFunctionBase. |
239 | | virtual FunctionBasePtr build(const ColumnsWithTypeAndName& arguments, |
240 | | const DataTypePtr& return_type) const = 0; |
241 | | |
242 | | /// For higher-order functions (functions, that have lambda expression as at least one argument). |
243 | | /// You pass data types with empty DataTypeFunction for lambda arguments. |
244 | | /// This function will replace it with DataTypeFunction containing actual types. |
245 | | virtual DataTypes get_variadic_argument_types() const = 0; |
246 | | |
247 | | /// Returns indexes of arguments, that must be ColumnConst |
248 | | virtual ColumnNumbers get_arguments_that_are_always_constant() const = 0; |
249 | | }; |
250 | | |
251 | | using FunctionBuilderPtr = std::shared_ptr<IFunctionBuilder>; |
252 | | |
253 | 1 | inline std::string get_types_string(const ColumnsWithTypeAndName& arguments) { |
254 | 1 | std::string types; |
255 | 1 | for (const auto& argument : arguments) { |
256 | 0 | if (!types.empty()) { |
257 | 0 | types += ", "; |
258 | 0 | } |
259 | 0 | types += argument.type->get_name(); |
260 | 0 | } |
261 | 1 | return types; |
262 | 1 | } |
263 | | |
264 | | /// used in function_factory. when we register a function, save a builder. to get a function, to get a builder. |
265 | | /// will use DefaultFunctionBuilder as the default builder in function's registration if we didn't explicitly specify. |
266 | | class FunctionBuilderImpl : public IFunctionBuilder { |
267 | | public: |
268 | | FunctionBasePtr build(const ColumnsWithTypeAndName& arguments, |
269 | 263k | const DataTypePtr& return_type) const final { |
270 | 263k | if (skip_return_type_check()) { |
271 | 248k | return build_impl(arguments, return_type); |
272 | 248k | } |
273 | 14.5k | const DataTypePtr& func_return_type = get_return_type(arguments); |
274 | 14.5k | if (func_return_type == nullptr) { |
275 | 1 | throw doris::Exception( |
276 | 1 | ErrorCode::INTERNAL_ERROR, |
277 | 1 | "function return type check failed, function_name={}, " |
278 | 1 | "expect_return_type={}, real_return_type is nullptr, input_arguments={}", |
279 | 1 | get_name(), return_type->get_name(), get_types_string(arguments)); |
280 | 1 | } |
281 | | |
282 | | // check return types equal. |
283 | 14.5k | if (!(return_type->equals(*func_return_type) || |
284 | | // For null constant argument, `get_return_type` would return |
285 | | // Nullable<DataTypeNothing> when `use_default_implementation_for_nulls` is true. |
286 | 14.5k | (return_type->is_nullable() && func_return_type->is_nullable() && |
287 | 3 | ((DataTypeNullable*)func_return_type.get()) |
288 | 2 | ->get_nested_type() |
289 | 2 | ->get_primitive_type() == INVALID_TYPE) || |
290 | 14.5k | is_date_or_datetime_or_decimal(return_type, func_return_type) || |
291 | 14.5k | is_array_nested_type_date_or_datetime_or_decimal(return_type, func_return_type))) { |
292 | 0 | LOG_WARNING( |
293 | 0 | "function return type check failed, function_name={}, " |
294 | 0 | "expect_return_type={}, real_return_type={}, input_arguments={}", |
295 | 0 | get_name(), return_type->get_name(), func_return_type->get_name(), |
296 | 0 | get_types_string(arguments)); |
297 | 0 | return nullptr; |
298 | 0 | } |
299 | 14.5k | return build_impl(arguments, return_type); |
300 | 14.5k | } |
301 | | |
302 | 10.3k | bool is_variadic() const override { return false; } |
303 | | |
304 | | // Default implementation. Will check only in non-variadic case. |
305 | | void check_number_of_arguments(size_t number_of_arguments) const override; |
306 | | // the return type should be same with what FE plans. |
307 | | // it returns: `get_return_type_impl` if `use_default_implementation_for_nulls` = false |
308 | | // `get_return_type_impl` warpped in NULL if `use_default_implementation_for_nulls` = true and input has NULL |
309 | | DataTypePtr get_return_type(const ColumnsWithTypeAndName& arguments) const; |
310 | | |
311 | 1.03k | DataTypes get_variadic_argument_types() const override { |
312 | 1.03k | return get_variadic_argument_types_impl(); |
313 | 1.03k | } |
314 | | |
315 | 0 | ColumnNumbers get_arguments_that_are_always_constant() const override { return {}; } |
316 | | |
317 | | protected: |
318 | | // Get the result type by argument type. If the function does not apply to these arguments, throw an exception. |
319 | | // the get_return_type_impl and its overrides should only return the nested type if `use_default_implementation_for_nulls` is true. |
320 | | // whether to wrap in nullable type will be automatically decided. |
321 | 14.3k | virtual DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const { |
322 | 14.3k | DataTypes data_types(arguments.size()); |
323 | 48.3k | for (size_t i = 0; i < arguments.size(); ++i) { |
324 | 33.9k | data_types[i] = arguments[i].type; |
325 | 33.9k | } |
326 | 14.3k | return get_return_type_impl(data_types); |
327 | 14.3k | } |
328 | | |
329 | 0 | virtual DataTypePtr get_return_type_impl(const DataTypes& /*arguments*/) const { |
330 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
331 | 0 | "get_return_type is not implemented for {}", get_name()); |
332 | 0 | return nullptr; |
333 | 0 | } |
334 | | |
335 | | /** If use_default_implementation_for_nulls() is true, than change arguments for get_return_type() and build_impl(): |
336 | | * if some of arguments are Nullable(Nothing) then don't call get_return_type(), call build_impl() with return_type = Nullable(Nothing), |
337 | | * if some of arguments are Nullable, then: |
338 | | * - Nullable types are substituted with nested types for get_return_type() function |
339 | | * - WRAP get_return_type() RESULT IN NULLABLE type and pass to build_impl |
340 | | * |
341 | | * Otherwise build returns build_impl(arguments, get_return_type(arguments)); |
342 | | */ |
343 | 0 | virtual bool use_default_implementation_for_nulls() const { return true; } |
344 | | |
345 | 0 | virtual bool skip_return_type_check() const { return false; } |
346 | | |
347 | 0 | virtual bool need_replace_null_data_to_default() const { return false; } |
348 | | |
349 | | /// return a real function object to execute. called in build(...). |
350 | | virtual FunctionBasePtr build_impl(const ColumnsWithTypeAndName& arguments, |
351 | | const DataTypePtr& return_type) const = 0; |
352 | | |
353 | 321 | virtual DataTypes get_variadic_argument_types_impl() const { return {}; } |
354 | | |
355 | | private: |
356 | | bool is_date_or_datetime_or_decimal(const DataTypePtr& return_type, |
357 | | const DataTypePtr& func_return_type) const; |
358 | | bool is_array_nested_type_date_or_datetime_or_decimal( |
359 | | const DataTypePtr& return_type, const DataTypePtr& func_return_type) const; |
360 | | }; |
361 | | |
362 | | /// Previous function interface. |
363 | | class IFunction : public std::enable_shared_from_this<IFunction>, |
364 | | public FunctionBuilderImpl, |
365 | | public IFunctionBase, |
366 | | public PreparedFunctionImpl { |
367 | | public: |
368 | | String get_name() const override = 0; |
369 | | |
370 | | /// Notice: We should not change the column in the block, because the column may be shared by multiple expressions or exec nodes. |
371 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
372 | | uint32_t result, size_t input_rows_count) const override = 0; |
373 | | |
374 | | /// Override this functions to change default implementation behavior. See details in IMyFunction. |
375 | 33.6k | bool use_default_implementation_for_nulls() const override { return true; } |
376 | | |
377 | 14.5k | bool skip_return_type_check() const override { return false; } |
378 | | |
379 | 6.81k | bool need_replace_null_data_to_default() const override { return false; } |
380 | | |
381 | | /// all constancy check should use this function to do automatically |
382 | 21.4k | ColumnNumbers get_arguments_that_are_always_constant() const override { return {}; } |
383 | | |
384 | 30 | bool is_use_default_implementation_for_constants() const override { |
385 | 30 | return use_default_implementation_for_constants(); |
386 | 30 | } |
387 | | |
388 | | using PreparedFunctionImpl::execute; |
389 | | using PreparedFunctionImpl::execute_impl_dry_run; |
390 | | using FunctionBuilderImpl::get_return_type_impl; |
391 | | using FunctionBuilderImpl::get_variadic_argument_types_impl; |
392 | | using FunctionBuilderImpl::get_return_type; |
393 | | |
394 | | [[noreturn]] PreparedFunctionPtr prepare(FunctionContext* context, |
395 | | const Block& /*sample_block*/, |
396 | | const ColumnNumbers& /*arguments*/, |
397 | 0 | uint32_t /*result*/) const final { |
398 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
399 | 0 | "prepare is not implemented for IFunction {}", get_name()); |
400 | 0 | __builtin_unreachable(); |
401 | 0 | } |
402 | | |
403 | 21.9k | Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
404 | 21.9k | return Status::OK(); |
405 | 21.9k | } |
406 | | |
407 | 0 | [[noreturn]] const DataTypes& get_argument_types() const final { |
408 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
409 | 0 | "get_argument_types is not implemented for IFunction {}", |
410 | 0 | get_name()); |
411 | 0 | __builtin_unreachable(); |
412 | 0 | } |
413 | | |
414 | 0 | [[noreturn]] const DataTypePtr& get_return_type() const final { |
415 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
416 | 0 | "get_return_type is not implemented for IFunction {}", get_name()); |
417 | 0 | __builtin_unreachable(); |
418 | 0 | } |
419 | | |
420 | | protected: |
421 | | FunctionBasePtr build_impl(const ColumnsWithTypeAndName& /*arguments*/, |
422 | 0 | const DataTypePtr& /*return_type*/) const final { |
423 | 0 | throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR, |
424 | 0 | "build_impl is not implemented for IFunction {}", get_name()); |
425 | 0 | __builtin_unreachable(); |
426 | 0 | return {}; |
427 | 0 | } |
428 | | }; |
429 | | |
430 | | /// Wrappers over IFunction. If we (default)use DefaultFunction as wrapper, all function execution will go through this. |
431 | | |
432 | | class DefaultExecutable final : public PreparedFunctionImpl { |
433 | | public: |
434 | | explicit DefaultExecutable(std::shared_ptr<IFunction> function_) |
435 | 14.6k | : function(std::move(function_)) {} |
436 | | |
437 | 0 | String get_name() const override { return function->get_name(); } |
438 | | |
439 | | protected: |
440 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
441 | 11.3k | uint32_t result, size_t input_rows_count) const final { |
442 | 11.3k | return function->execute_impl(context, block, arguments, result, input_rows_count); |
443 | 11.3k | } |
444 | | |
445 | | Status evaluate_inverted_index( |
446 | | const ColumnsWithTypeAndName& arguments, |
447 | | const std::vector<vectorized::IndexFieldNameAndTypePair>& data_type_with_names, |
448 | | std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows, |
449 | 0 | segment_v2::InvertedIndexResultBitmap& bitmap_result) const { |
450 | 0 | return function->evaluate_inverted_index(arguments, data_type_with_names, iterators, |
451 | 0 | num_rows, bitmap_result); |
452 | 0 | } |
453 | | |
454 | | Status execute_impl_dry_run(FunctionContext* context, Block& block, |
455 | | const ColumnNumbers& arguments, uint32_t result, |
456 | 0 | size_t input_rows_count) const final { |
457 | 0 | return function->execute_impl_dry_run(context, block, arguments, result, input_rows_count); |
458 | 0 | } |
459 | 21.6k | bool use_default_implementation_for_nulls() const final { |
460 | 21.6k | return function->use_default_implementation_for_nulls(); |
461 | 21.6k | } |
462 | | |
463 | 0 | bool skip_return_type_check() const final { return function->skip_return_type_check(); } |
464 | 6.99k | bool need_replace_null_data_to_default() const final { |
465 | 6.99k | return function->need_replace_null_data_to_default(); |
466 | 6.99k | } |
467 | 21.6k | bool use_default_implementation_for_constants() const final { |
468 | 21.6k | return function->use_default_implementation_for_constants(); |
469 | 21.6k | } |
470 | 21.6k | ColumnNumbers get_arguments_that_are_always_constant() const final { |
471 | 21.6k | return function->get_arguments_that_are_always_constant(); |
472 | 21.6k | } |
473 | | |
474 | | private: |
475 | | std::shared_ptr<IFunction> function; |
476 | | }; |
477 | | |
478 | | /* |
479 | | * when we register a function which didn't specify its base(i.e. inherited from IFunction), actually we use this as a wrapper. |
480 | | * it saves real implementation as `function`. |
481 | | */ |
482 | | class DefaultFunction final : public IFunctionBase { |
483 | | public: |
484 | | DefaultFunction(std::shared_ptr<IFunction> function_, DataTypes arguments_, |
485 | | DataTypePtr return_type_) |
486 | 14.5k | : function(std::move(function_)), |
487 | 14.5k | arguments(std::move(arguments_)), |
488 | 14.5k | return_type(std::move(return_type_)) {} |
489 | | |
490 | 0 | String get_name() const override { return function->get_name(); } |
491 | | |
492 | 0 | const DataTypes& get_argument_types() const override { return arguments; } |
493 | 1 | const DataTypePtr& get_return_type() const override { return return_type; } |
494 | | |
495 | | // return a default wrapper for IFunction. |
496 | | PreparedFunctionPtr prepare(FunctionContext* context, const Block& /*sample_block*/, |
497 | | const ColumnNumbers& /*arguments*/, |
498 | 14.6k | uint32_t /*result*/) const override { |
499 | 14.6k | return std::make_shared<DefaultExecutable>(function); |
500 | 14.6k | } |
501 | | |
502 | 22.8k | Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
503 | 22.8k | return function->open(context, scope); |
504 | 22.8k | } |
505 | | |
506 | 22.7k | Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
507 | 22.7k | return function->close(context, scope); |
508 | 22.7k | } |
509 | | |
510 | | Status evaluate_inverted_index( |
511 | | const ColumnsWithTypeAndName& args, |
512 | | const std::vector<vectorized::IndexFieldNameAndTypePair>& data_type_with_names, |
513 | | std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows, |
514 | 0 | segment_v2::InvertedIndexResultBitmap& bitmap_result) const override { |
515 | 0 | return function->evaluate_inverted_index(args, data_type_with_names, iterators, num_rows, |
516 | 0 | bitmap_result); |
517 | 0 | } |
518 | | |
519 | 30 | bool is_use_default_implementation_for_constants() const override { |
520 | 30 | return function->is_use_default_implementation_for_constants(); |
521 | 30 | } |
522 | | |
523 | 0 | bool can_push_down_to_index() const override { return function->can_push_down_to_index(); } |
524 | | |
525 | | private: |
526 | | std::shared_ptr<IFunction> function; |
527 | | DataTypes arguments; |
528 | | DataTypePtr return_type; |
529 | | }; |
530 | | |
531 | | class DefaultFunctionBuilder : public FunctionBuilderImpl { |
532 | | public: |
533 | | explicit DefaultFunctionBuilder(std::shared_ptr<IFunction> function_) |
534 | 16.6k | : function(std::move(function_)) {} |
535 | | |
536 | 14.5k | void check_number_of_arguments(size_t number_of_arguments) const override { |
537 | 14.5k | return function->check_number_of_arguments(number_of_arguments); |
538 | 14.5k | } |
539 | | |
540 | 456 | String get_name() const override { return function->get_name(); } |
541 | 1.00k | bool is_variadic() const override { return function->is_variadic(); } |
542 | 0 | size_t get_number_of_arguments() const override { return function->get_number_of_arguments(); } |
543 | | |
544 | 0 | ColumnNumbers get_arguments_that_are_always_constant() const override { |
545 | 0 | return function->get_arguments_that_are_always_constant(); |
546 | 0 | } |
547 | | |
548 | | protected: |
549 | 0 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
550 | 0 | return function->get_return_type_impl(arguments); |
551 | 0 | } |
552 | 14.5k | DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override { |
553 | 14.5k | return function->get_return_type_impl(arguments); |
554 | 14.5k | } |
555 | | |
556 | 14.5k | bool use_default_implementation_for_nulls() const override { |
557 | 14.5k | return function->use_default_implementation_for_nulls(); |
558 | 14.5k | } |
559 | | |
560 | 14.5k | bool skip_return_type_check() const override { return function->skip_return_type_check(); } |
561 | | |
562 | 0 | bool need_replace_null_data_to_default() const override { |
563 | 0 | return function->need_replace_null_data_to_default(); |
564 | 0 | } |
565 | | |
566 | | FunctionBasePtr build_impl(const ColumnsWithTypeAndName& arguments, |
567 | 14.5k | const DataTypePtr& return_type) const override { |
568 | 14.5k | DataTypes data_types(arguments.size()); |
569 | 48.9k | for (size_t i = 0; i < arguments.size(); ++i) { |
570 | 34.4k | data_types[i] = arguments[i].type; |
571 | 34.4k | } |
572 | 14.5k | return std::make_shared<DefaultFunction>(function, data_types, return_type); |
573 | 14.5k | } |
574 | | |
575 | 1.03k | DataTypes get_variadic_argument_types_impl() const override { |
576 | 1.03k | return function->get_variadic_argument_types_impl(); |
577 | 1.03k | } |
578 | | |
579 | | private: |
580 | | std::shared_ptr<IFunction> function; |
581 | | }; |
582 | | |
583 | | using FunctionPtr = std::shared_ptr<IFunction>; |
584 | | |
585 | | /** Return ColumnNullable of src, with null map as OR-ed null maps of args columns in blocks. |
586 | | * Or ColumnConst(ColumnNullable) if the result is always NULL or if the result is constant and always not NULL. |
587 | | */ |
588 | | ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args, |
589 | | uint32_t result, size_t input_rows_count); |
590 | | |
591 | | } // namespace doris::vectorized |