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