Coverage Report

Created: 2025-04-25 11:21

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