Coverage Report

Created: 2025-04-15 14:42

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