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