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