Coverage Report

Created: 2026-07-05 00:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/function.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/IFunction.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <fmt/format.h>
24
#include <glog/logging.h>
25
26
#include <cstddef>
27
#include <memory>
28
#include <string>
29
#include <utility>
30
31
#include "common/exception.h"
32
#include "common/logging.h"
33
#include "common/status.h"
34
#include "core/block/block.h"
35
#include "core/block/column_numbers.h"
36
#include "core/block/column_with_type_and_name.h"
37
#include "core/block/columns_with_type_and_name.h"
38
#include "core/data_type/data_type.h"
39
#include "core/data_type/data_type_array.h"
40
#include "core/data_type/data_type_map.h"
41
#include "core/data_type/data_type_nullable.h"
42
#include "core/data_type/data_type_struct.h"
43
#include "core/data_type/define_primitive_type.h"
44
#include "core/types.h"
45
#include "exprs/expr_zonemap_filter.h"
46
#include "exprs/function_context.h"
47
#include "exprs/vexpr_fwd.h"
48
#include "storage/index/inverted/inverted_index_iterator.h" // IWYU pragma: keep
49
#include "storage/index/inverted/inverted_index_parser.h"
50
#include "storage/index/zone_map/zonemap_filter_result.h"
51
52
namespace doris {
53
struct InvertedIndexAnalyzerCtx;
54
} // namespace doris
55
56
namespace doris {
57
58
struct FunctionAttr {
59
    bool new_version_unix_timestamp {false};
60
};
61
62
#define RETURN_REAL_TYPE_FOR_DATEV2_FUNCTION(TYPE)                                             \
63
65
    bool is_nullable = false;                                                                  \
64
65
    bool is_datev2 = false;                                                                    \
65
121
    for (auto it : arguments) {                                                                \
66
121
        is_nullable = is_nullable || it.type->is_nullable();                                   \
67
121
        is_datev2 = is_datev2 || it.type->get_primitive_type() == TYPE_DATEV2 ||               \
68
121
                    it.type->get_primitive_type() == TYPE_DATETIMEV2;                          \
69
121
    }                                                                                          \
70
65
    return is_nullable || !is_datev2                                                           \
71
65
                   ? make_nullable(                                                            \
72
62
                             std::make_shared<typename PrimitiveTypeTraits<TYPE>::DataType>()) \
73
65
                   : std::make_shared<typename PrimitiveTypeTraits<TYPE>::DataType>();
74
75
#define SET_NULLMAP_IF_FALSE(EXPR) \
76
    if (!EXPR) [[unlikely]] {      \
77
        null_map[i] = true;        \
78
    }
79
80
class Field;
81
class VExpr;
82
class ZoneMapEvalContext;
83
84
// Only use dispose the variadic argument
85
template <typename T>
86
auto has_variadic_argument_types(T&& arg) -> decltype(T::get_variadic_argument_types()) {};
87
void has_variadic_argument_types(...);
88
89
template <typename T>
90
concept HasGetVariadicArgumentTypesImpl = requires(T t) {
91
    { t.get_variadic_argument_types_impl() } -> std::same_as<DataTypes>;
92
};
93
94
bool have_null_column(const Block& block, const ColumnNumbers& args);
95
bool have_null_column(const ColumnsWithTypeAndName& args);
96
97
/// The simplest executable object.
98
/// Motivation:
99
///  * Prepare something heavy once before main execution loop instead of doing it for each block.
100
///  * Provide const interface for IFunctionBase (later).
101
class IPreparedFunction {
102
public:
103
101k
    virtual ~IPreparedFunction() = default;
104
105
    /// Get the main function name.
106
    virtual String get_name() const = 0;
107
108
    virtual Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
109
                           uint32_t result, size_t input_rows_count) const = 0;
110
};
111
112
using PreparedFunctionPtr = std::shared_ptr<IPreparedFunction>;
113
114
class PreparedFunctionImpl : public IPreparedFunction {
115
public:
116
    Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
117
                   uint32_t result, size_t input_rows_count) const final;
118
119
    /** If the function have non-zero number of arguments,
120
      *  and if all arguments are constant, that we could automatically provide default implementation:
121
      *  arguments are converted to ordinary columns with single value which is not const, then function is executed as usual,
122
      *  and then the result is converted to constant column.
123
      */
124
107k
    virtual bool use_default_implementation_for_constants() const { return true; }
125
126
    /** If use_default_implementation_for_nulls() is true, after execute the function,
127
      * whether need to replace the nested data of null data to the default value.
128
      * E.g. for binary arithmetic exprs, need return true to avoid false overflow.
129
      */
130
0
    virtual bool need_replace_null_data_to_default() const { return false; }
131
132
protected:
133
    virtual Status execute_impl(FunctionContext* context, Block& block,
134
                                const ColumnNumbers& arguments, uint32_t result,
135
                                size_t input_rows_count) const = 0;
136
137
    /** Default implementation in presence of Nullable arguments or NULL constants as arguments is the following:
138
      *  if some of arguments are NULL constants then return NULL constant,
139
      *  if some of arguments are Nullable, then execute function as usual for block,
140
      *   where Nullable columns are substituted with nested columns (they have arbitrary values in rows corresponding to NULL value)
141
      *   and wrap result in Nullable column where NULLs are in all rows where any of arguments are NULL.
142
      */
143
0
    virtual bool use_default_implementation_for_nulls() const { return true; }
144
145
0
    virtual bool skip_return_type_check() const { return false; }
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, uint32_t result,
155
                                            size_t input_rows_count, bool* executed) const;
156
    Status default_implementation_for_constant_arguments(FunctionContext* context, Block& block,
157
                                                         const ColumnNumbers& args, uint32_t result,
158
                                                         size_t input_rows_count,
159
                                                         bool* executed) const;
160
    Status default_execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
161
                           uint32_t result, size_t input_rows_count) const;
162
    Status _execute_skipped_constant_deal(FunctionContext* context, Block& block,
163
                                          const ColumnNumbers& args, uint32_t result,
164
                                          size_t input_rows_count) const;
165
};
166
167
/// Function with known arguments and return type.
168
class IFunctionBase {
169
public:
170
116k
    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
0
    virtual double execute_cost() const { return 1.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, uint32_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
70.1k
    virtual Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) {
188
70.1k
        return Status::OK();
189
70.1k
    }
190
191
    Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
192
98.5k
                   uint32_t result, size_t input_rows_count) const {
193
        // Some function implementations may not handle the case where input_rows_count is 0
194
        // (e.g., some functions access the 0th row of input columns during execution).
195
        // Additionally, some UDF functions may hang if they write 0 rows and then try to read.
196
        // Therefore, before executing the function, we first check if input_rows_count is 0.
197
        // If it is 0, we directly return an empty result column to avoid executing the function body.
198
98.5k
        if (input_rows_count == 0) {
199
12
            block.get_by_position(result).column =
200
12
                    block.get_by_position(result).type->create_column();
201
12
            return Status::OK();
202
12
        }
203
98.5k
        try {
204
98.5k
            return prepare(context, block, arguments, result)
205
98.5k
                    ->execute(context, block, arguments, result, input_rows_count);
206
98.5k
        } catch (const Exception& e) {
207
1
            return e.to_status();
208
1
        }
209
98.5k
    }
210
211
    virtual Status evaluate_inverted_index(
212
            const ColumnsWithTypeAndName& arguments,
213
            const std::vector<IndexFieldNameAndTypePair>& data_type_with_names,
214
            std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows,
215
            const InvertedIndexAnalyzerCtx* analyzer_ctx,
216
0
            segment_v2::InvertedIndexResultBitmap& bitmap_result) const {
217
0
        return Status::OK();
218
0
    }
219
220
    /// Do cleaning work when function is finished, i.e., release state variables in the
221
    /// `FunctionContext` which are registered in `prepare` phase.
222
51.8k
    virtual Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) {
223
51.8k
        return Status::OK();
224
51.8k
    }
225
226
    virtual bool is_use_default_implementation_for_constants() const = 0;
227
228
0
    virtual bool is_udf_function() const { return false; }
229
230
0
    virtual bool can_push_down_to_index() const { return false; }
231
232
177
    virtual bool is_blockable() const { return false; }
233
234
    virtual ZoneMapFilterResult evaluate_zonemap_filter(const ZoneMapEvalContext& ctx,
235
                                                        const VExprSPtrs& function_arguments) const;
236
237
22
    virtual bool can_evaluate_zonemap_filter(const VExprSPtrs& /*function_arguments*/) const {
238
22
        return false;
239
22
    }
240
241
    virtual ZoneMapFilterResult evaluate_dictionary_filter(
242
            const DictionaryEvalContext& ctx, const VExprSPtrs& function_arguments) const;
243
244
0
    virtual bool can_evaluate_dictionary_filter(const VExprSPtrs& /*function_arguments*/) const {
245
0
        return false;
246
0
    }
247
248
    virtual ZoneMapFilterResult evaluate_bloom_filter(const BloomFilterEvalContext& ctx,
249
                                                      const VExprSPtrs& function_arguments) const;
250
251
0
    virtual bool can_evaluate_bloom_filter(const VExprSPtrs& /*function_arguments*/) const {
252
0
        return false;
253
0
    }
254
};
255
256
using FunctionBasePtr = std::shared_ptr<IFunctionBase>;
257
258
/// Creates IFunctionBase from argument types list.
259
class IFunctionBuilder {
260
public:
261
119k
    virtual ~IFunctionBuilder() = default;
262
263
    /// Get the main function name.
264
    virtual String get_name() const = 0;
265
266
    /// Override and return true if function could take different number of arguments.
267
    ///TODO: this function is not actually used now. but in check_number_of_arguments we still need it because for many
268
    /// functions we didn't set the correct number of arguments.
269
    virtual bool is_variadic() const = 0;
270
271
    /// For non-variadic functions, return number of arguments; otherwise return zero (that should be ignored).
272
    virtual size_t get_number_of_arguments() const = 0;
273
274
    /// Throw if number of arguments is incorrect. Default implementation will check only in non-variadic case.
275
    virtual void check_number_of_arguments(size_t number_of_arguments) const = 0;
276
277
    /// Check arguments and return IFunctionBase.
278
    virtual FunctionBasePtr build(const ColumnsWithTypeAndName& arguments,
279
                                  const DataTypePtr& return_type) const = 0;
280
281
    /// For higher-order functions (functions, that have lambda expression as at least one argument).
282
    /// You pass data types with empty DataTypeFunction for lambda arguments.
283
    /// This function will replace it with DataTypeFunction containing actual types.
284
    virtual DataTypes get_variadic_argument_types() const = 0;
285
286
    /// Returns indexes of arguments, that must be ColumnConst
287
    virtual ColumnNumbers get_arguments_that_are_always_constant() const = 0;
288
};
289
290
using FunctionBuilderPtr = std::shared_ptr<IFunctionBuilder>;
291
292
3
inline std::string get_types_string(const ColumnsWithTypeAndName& arguments) {
293
3
    std::string types;
294
3
    for (const auto& argument : arguments) {
295
2
        if (!types.empty()) {
296
1
            types += ", ";
297
1
        }
298
2
        types += argument.type->get_name();
299
2
    }
300
3
    return types;
301
3
}
302
303
/// used in function_factory. when we register a function, save a builder. to get a function, to get a builder.
304
/// will use DefaultFunctionBuilder as the default builder in function's registration if we didn't explicitly specify.
305
class FunctionBuilderImpl : public IFunctionBuilder {
306
public:
307
    FunctionBasePtr build(const ColumnsWithTypeAndName& arguments,
308
99.4k
                          const DataTypePtr& return_type) const final {
309
99.4k
        if (skip_return_type_check()) {
310
84.6k
            return build_impl(arguments, return_type);
311
84.6k
        }
312
14.7k
        const DataTypePtr& func_return_type = get_return_type(arguments);
313
14.7k
        if (func_return_type == nullptr) {
314
1
            throw doris::Exception(
315
1
                    ErrorCode::INTERNAL_ERROR,
316
1
                    "function return type check failed, function_name={}, "
317
1
                    "expect_return_type={}, real_return_type is nullptr, input_arguments={}",
318
1
                    get_name(), return_type->get_name(), get_types_string(arguments));
319
1
        }
320
321
        // check return types equal.
322
14.7k
        if (!(return_type->equals(*func_return_type) ||
323
              // For null constant argument, `get_return_type` would return
324
              // Nullable<DataTypeNothing> when `use_default_implementation_for_nulls` is true.
325
14.7k
              (return_type->is_nullable() && func_return_type->is_nullable() &&
326
57
               ((DataTypeNullable*)func_return_type.get())
327
32
                               ->get_nested_type()
328
32
                               ->get_primitive_type() == INVALID_TYPE) ||
329
14.7k
              is_date_or_datetime_or_decimal(return_type, func_return_type) ||
330
14.7k
              is_nested_type_date_or_datetime_or_decimal(return_type, func_return_type))) {
331
2
            throw doris::Exception(
332
2
                    ErrorCode::INTERNAL_ERROR,
333
2
                    "function return type check failed, function_name={}, "
334
2
                    "fe plan return type={},  be real return type={}, input_arguments={}",
335
2
                    get_name(), return_type->get_name(), func_return_type->get_name(),
336
2
                    get_types_string(arguments));
337
2
        }
338
14.7k
        return build_impl(arguments, return_type);
339
14.7k
    }
340
341
10.8k
    bool is_variadic() const override { return false; }
342
343
    // Default implementation. Will check only in non-variadic case.
344
    void check_number_of_arguments(size_t number_of_arguments) const override;
345
    // the return type should be same with what FE plans.
346
    // it returns: `get_return_type_impl` if `use_default_implementation_for_nulls` = false
347
    //  `get_return_type_impl` warpped in NULL if `use_default_implementation_for_nulls` = true and input has NULL
348
    DataTypePtr get_return_type(const ColumnsWithTypeAndName& arguments) const;
349
350
1.20k
    DataTypes get_variadic_argument_types() const override {
351
1.20k
        return get_variadic_argument_types_impl();
352
1.20k
    }
353
354
0
    ColumnNumbers get_arguments_that_are_always_constant() const override { return {}; }
355
356
protected:
357
    // Get the result type by argument type. If the function does not apply to these arguments, throw an exception.
358
    // the get_return_type_impl and its overrides should only return the nested type if `use_default_implementation_for_nulls` is true.
359
    // whether to wrap in nullable type will be automatically decided.
360
14.4k
    virtual DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const {
361
14.4k
        DataTypes data_types(arguments.size());
362
53.3k
        for (size_t i = 0; i < arguments.size(); ++i) {
363
38.9k
            data_types[i] = arguments[i].type;
364
38.9k
        }
365
14.4k
        return get_return_type_impl(data_types);
366
14.4k
    }
367
368
0
    virtual DataTypePtr get_return_type_impl(const DataTypes& /*arguments*/) const {
369
0
        throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
370
0
                               "get_return_type is not implemented for {}", get_name());
371
0
        return nullptr;
372
0
    }
373
374
    /** If use_default_implementation_for_nulls() is true, than change arguments for get_return_type() and build_impl():
375
      *  if some of arguments are Nullable(Nothing) then don't call get_return_type(), call build_impl() with return_type = Nullable(Nothing),
376
      *  if some of arguments are Nullable, then:
377
      *   - Nullable types are substituted with nested types for get_return_type() function
378
      *   - WRAP get_return_type() RESULT IN NULLABLE type and pass to build_impl
379
      *
380
      * Otherwise build returns build_impl(arguments, get_return_type(arguments));
381
      */
382
0
    virtual bool use_default_implementation_for_nulls() const { return true; }
383
384
0
    virtual bool skip_return_type_check() const { return false; }
385
386
0
    virtual bool need_replace_null_data_to_default() const { return false; }
387
388
    /// return a real function object to execute. called in build(...).
389
    virtual FunctionBasePtr build_impl(const ColumnsWithTypeAndName& arguments,
390
                                       const DataTypePtr& return_type) const = 0;
391
392
366
    virtual DataTypes get_variadic_argument_types_impl() const { return {}; }
393
394
private:
395
    bool is_date_or_datetime_or_decimal(const DataTypePtr& return_type,
396
                                        const DataTypePtr& func_return_type) const;
397
    bool is_nested_type_date_or_datetime_or_decimal(const DataTypePtr& return_type,
398
                                                    const DataTypePtr& func_return_type) const;
399
};
400
401
/// Previous function interface.
402
class IFunction : public std::enable_shared_from_this<IFunction>,
403
                  public FunctionBuilderImpl,
404
                  public IFunctionBase,
405
                  public PreparedFunctionImpl {
406
public:
407
    String get_name() const override = 0;
408
409
    /// Notice: We should not change the column in the block, because the column may be shared by multiple expressions or exec nodes.
410
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
411
                        uint32_t result, size_t input_rows_count) const override = 0;
412
413
    /// Override this functions to change default implementation behavior. See details in IMyFunction.
414
36.5k
    bool use_default_implementation_for_nulls() const override { return true; }
415
416
14.7k
    bool skip_return_type_check() const override { return false; }
417
418
9.01k
    bool need_replace_null_data_to_default() const override { return false; }
419
420
    /// all constancy check should use this function to do automatically
421
23.8k
    ColumnNumbers get_arguments_that_are_always_constant() const override { return {}; }
422
423
211
    bool is_use_default_implementation_for_constants() const override {
424
211
        return use_default_implementation_for_constants();
425
211
    }
426
427
    using PreparedFunctionImpl::execute;
428
    using FunctionBuilderImpl::get_return_type_impl;
429
    using FunctionBuilderImpl::get_variadic_argument_types_impl;
430
    using FunctionBuilderImpl::get_return_type;
431
432
    [[noreturn]] PreparedFunctionPtr prepare(FunctionContext* context,
433
                                             const Block& /*sample_block*/,
434
                                             const ColumnNumbers& /*arguments*/,
435
0
                                             uint32_t /*result*/) const final {
436
0
        throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
437
0
                               "prepare is not implemented for IFunction {}", get_name());
438
0
        __builtin_unreachable();
439
0
    }
440
441
27.2k
    Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override {
442
27.2k
        return Status::OK();
443
27.2k
    }
444
445
0
    [[noreturn]] const DataTypes& get_argument_types() const final {
446
0
        throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
447
0
                               "get_argument_types is not implemented for IFunction {}",
448
0
                               get_name());
449
0
        __builtin_unreachable();
450
0
    }
451
452
0
    [[noreturn]] const DataTypePtr& get_return_type() const final {
453
0
        throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
454
0
                               "get_return_type is not implemented for IFunction {}", get_name());
455
0
        __builtin_unreachable();
456
0
    }
457
458
protected:
459
    FunctionBasePtr build_impl(const ColumnsWithTypeAndName& /*arguments*/,
460
0
                               const DataTypePtr& /*return_type*/) const final {
461
0
        throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
462
0
                               "build_impl is not implemented for IFunction {}", get_name());
463
0
        __builtin_unreachable();
464
0
        return {};
465
0
    }
466
};
467
468
/*
469
 * when we register a function which didn't specify its base(i.e. inherited from IFunction), actually we use this as a wrapper.
470
 * it saves real implementation as `function`. 
471
*/
472
class DefaultFunction final : public IFunctionBase {
473
public:
474
    DefaultFunction(std::shared_ptr<IFunction> function_, DataTypes arguments_,
475
                    DataTypePtr return_type_)
476
14.7k
            : function(std::move(function_)),
477
14.7k
              arguments(std::move(arguments_)),
478
14.7k
              return_type(std::move(return_type_)) {}
479
480
10
    String get_name() const override { return function->get_name(); }
481
482
0
    const DataTypes& get_argument_types() const override { return arguments; }
483
2
    const DataTypePtr& get_return_type() const override { return return_type; }
484
485
    // return a default wrapper for IFunction.
486
    PreparedFunctionPtr prepare(FunctionContext* context, const Block& /*sample_block*/,
487
                                const ColumnNumbers& /*arguments*/,
488
14.6k
                                uint32_t /*result*/) const override {
489
14.6k
        return function;
490
14.6k
    }
491
492
4
    double execute_cost() const override { return function->execute_cost(); }
493
494
29.0k
    Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override {
495
29.0k
        return function->open(context, scope);
496
29.0k
    }
497
498
29.0k
    Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) override {
499
29.0k
        return function->close(context, scope);
500
29.0k
    }
501
502
    Status evaluate_inverted_index(
503
            const ColumnsWithTypeAndName& args,
504
            const std::vector<IndexFieldNameAndTypePair>& data_type_with_names,
505
            std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows,
506
            const InvertedIndexAnalyzerCtx* analyzer_ctx,
507
14
            segment_v2::InvertedIndexResultBitmap& bitmap_result) const override {
508
14
        return function->evaluate_inverted_index(args, data_type_with_names, iterators, num_rows,
509
14
                                                 analyzer_ctx, bitmap_result);
510
14
    }
511
512
211
    bool is_use_default_implementation_for_constants() const override {
513
211
        return function->is_use_default_implementation_for_constants();
514
211
    }
515
516
0
    bool can_push_down_to_index() const override { return function->can_push_down_to_index(); }
517
518
177
    bool is_blockable() const override { return function->is_blockable(); }
519
520
    ZoneMapFilterResult evaluate_zonemap_filter(
521
48
            const ZoneMapEvalContext& ctx, const VExprSPtrs& function_arguments) const override {
522
48
        return function->evaluate_zonemap_filter(ctx, function_arguments);
523
48
    }
524
525
155
    bool can_evaluate_zonemap_filter(const VExprSPtrs& function_arguments) const override {
526
155
        return function->can_evaluate_zonemap_filter(function_arguments);
527
155
    }
528
529
    ZoneMapFilterResult evaluate_dictionary_filter(
530
2
            const DictionaryEvalContext& ctx, const VExprSPtrs& function_arguments) const override {
531
2
        return function->evaluate_dictionary_filter(ctx, function_arguments);
532
2
    }
533
534
15
    bool can_evaluate_dictionary_filter(const VExprSPtrs& function_arguments) const override {
535
15
        return function->can_evaluate_dictionary_filter(function_arguments);
536
15
    }
537
538
    ZoneMapFilterResult evaluate_bloom_filter(const BloomFilterEvalContext& ctx,
539
2
                                              const VExprSPtrs& function_arguments) const override {
540
2
        return function->evaluate_bloom_filter(ctx, function_arguments);
541
2
    }
542
543
15
    bool can_evaluate_bloom_filter(const VExprSPtrs& function_arguments) const override {
544
15
        return function->can_evaluate_bloom_filter(function_arguments);
545
15
    }
546
547
private:
548
    std::shared_ptr<IFunction> function;
549
    DataTypes arguments;
550
    DataTypePtr return_type;
551
};
552
553
struct simple_function_creator_without_type0 {
554
    template <typename AggregateFunctionTemplate, typename... TArgs>
555
0
    static std::shared_ptr<IFunction> create(const DataTypePtr& result_type, TArgs&&... args) {
556
0
        std::unique_ptr<IFunction> result(std::make_unique<AggregateFunctionTemplate>(
557
0
                result_type, std::forward<TArgs>(args)...));
558
0
        return std::shared_ptr<IFunction>(result.release());
559
0
    }
Unexecuted instantiation: _ZN5doris37simple_function_creator_without_type06createINS_25FunctionArrayAggDecimalV3INS_27ArrayAggregateImplDecimalV3ILNS_18AggregateOperationE2ELNS_13PrimitiveTypeE30EEENS_12NameArraySumEEEJEEESt10shared_ptrINS_9IFunctionEERKS9_IKNS_9IDataTypeEEDpOT0_
Unexecuted instantiation: _ZN5doris37simple_function_creator_without_type06createINS_25FunctionArrayAggDecimalV3INS_27ArrayAggregateImplDecimalV3ILNS_18AggregateOperationE2ELNS_13PrimitiveTypeE35EEENS_12NameArraySumEEEJEEESt10shared_ptrINS_9IFunctionEERKS9_IKNS_9IDataTypeEEDpOT0_
Unexecuted instantiation: _ZN5doris37simple_function_creator_without_type06createINS_25FunctionArrayAggDecimalV3INS_27ArrayAggregateImplDecimalV3ILNS_18AggregateOperationE3ELNS_13PrimitiveTypeE30EEENS_16NameArrayAverageEEEJEEESt10shared_ptrINS_9IFunctionEERKS9_IKNS_9IDataTypeEEDpOT0_
Unexecuted instantiation: _ZN5doris37simple_function_creator_without_type06createINS_25FunctionArrayAggDecimalV3INS_27ArrayAggregateImplDecimalV3ILNS_18AggregateOperationE3ELNS_13PrimitiveTypeE35EEENS_16NameArrayAverageEEEJEEESt10shared_ptrINS_9IFunctionEERKS9_IKNS_9IDataTypeEEDpOT0_
Unexecuted instantiation: _ZN5doris37simple_function_creator_without_type06createINS_25FunctionArrayAggDecimalV3INS_27ArrayAggregateImplDecimalV3ILNS_18AggregateOperationE4ELNS_13PrimitiveTypeE30EEENS_16NameArrayProductEEEJEEESt10shared_ptrINS_9IFunctionEERKS9_IKNS_9IDataTypeEEDpOT0_
Unexecuted instantiation: _ZN5doris37simple_function_creator_without_type06createINS_25FunctionArrayAggDecimalV3INS_27ArrayAggregateImplDecimalV3ILNS_18AggregateOperationE4ELNS_13PrimitiveTypeE35EEENS_16NameArrayProductEEEJEEESt10shared_ptrINS_9IFunctionEERKS9_IKNS_9IDataTypeEEDpOT0_
Unexecuted instantiation: _ZN5doris37simple_function_creator_without_type06createINS_19FunctionArrayCumSumILNS_13PrimitiveTypeE30EEEJEEESt10shared_ptrINS_9IFunctionEERKS5_IKNS_9IDataTypeEEDpOT0_
Unexecuted instantiation: _ZN5doris37simple_function_creator_without_type06createINS_19FunctionArrayCumSumILNS_13PrimitiveTypeE35EEEJEEESt10shared_ptrINS_9IFunctionEERKS5_IKNS_9IDataTypeEEDpOT0_
560
};
561
template <template <PrimitiveType> class FunctionTemplate>
562
struct SimpleFunctionCurryDirectWithResultType0 {
563
    template <PrimitiveType ResultType>
564
    using T = FunctionTemplate<ResultType>;
565
};
566
template <PrimitiveType... AllowedTypes>
567
struct simple_function_creator_with_result_type0 {
568
    template <typename Class, typename... TArgs>
569
    static std::shared_ptr<IFunction> create_base_with_result_type(const DataTypePtr& result_type,
570
0
                                                                   TArgs&&... args) {
571
0
        auto create = [&]<PrimitiveType ResultType>() {
572
0
            return simple_function_creator_without_type0::create<
573
0
                    typename Class::template T<ResultType>>(result_type,
574
0
                                                            std::forward<TArgs>(args)...);
575
0
        };
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_17ArraySumDecimalV3EEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlTnS1_vE_clILS1_30EEEDav
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_17ArraySumDecimalV3EEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlTnS1_vE_clILS1_35EEEDav
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_17ArrayAvgDecimalV3EEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlTnS1_vE_clILS1_30EEEDav
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_17ArrayAvgDecimalV3EEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlTnS1_vE_clILS1_35EEEDav
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_21ArrayProductDecimalV3EEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlTnS1_vE_clILS1_30EEEDav
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_21ArrayProductDecimalV3EEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlTnS1_vE_clILS1_35EEEDav
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_19FunctionArrayCumSumEEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlTnS1_vE_clILS1_30EEEDav
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_19FunctionArrayCumSumEEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlTnS1_vE_clILS1_35EEEDav
576
0
        std::shared_ptr<IFunction> result = nullptr;
577
0
        auto type = result_type->get_primitive_type();
578
579
0
        (
580
0
                [&] {
581
0
                    if (type == AllowedTypes) {
582
0
                        static_assert(AllowedTypes == TYPE_DECIMAL128I ||
583
0
                                      AllowedTypes == TYPE_DECIMAL256);
584
0
                        result = create.template operator()<AllowedTypes>();
585
0
                    }
586
0
                }(),
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_17ArraySumDecimalV3EEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlvE0_clEv
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_17ArraySumDecimalV3EEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlvE_clEv
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_17ArrayAvgDecimalV3EEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlvE0_clEv
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_17ArrayAvgDecimalV3EEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlvE_clEv
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_21ArrayProductDecimalV3EEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlvE0_clEv
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_21ArrayProductDecimalV3EEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlvE_clEv
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_19FunctionArrayCumSumEEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlvE0_clEv
Unexecuted instantiation: _ZZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_19FunctionArrayCumSumEEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_ENKUlvE_clEv
587
0
                ...);
588
589
0
        return result;
590
0
    }
Unexecuted instantiation: _ZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_17ArraySumDecimalV3EEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_
Unexecuted instantiation: _ZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_17ArrayAvgDecimalV3EEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_
Unexecuted instantiation: _ZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_21ArrayProductDecimalV3EEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_
Unexecuted instantiation: _ZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE28create_base_with_result_typeINS_40SimpleFunctionCurryDirectWithResultType0INS_19FunctionArrayCumSumEEEJEEESt10shared_ptrINS_9IFunctionEERKS7_IKNS_9IDataTypeEEDpOT0_
591
592
    // Create agg function with result type from FE.
593
    // Currently only used for decimalv3 sum and avg.
594
    template <template <PrimitiveType> class FunctionTemplate>
595
0
    static std::shared_ptr<IFunction> creator_with_result_type(const DataTypePtr& result_type) {
596
0
        return create_base_with_result_type<
597
0
                SimpleFunctionCurryDirectWithResultType0<FunctionTemplate>>(result_type);
598
0
    }
Unexecuted instantiation: _ZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE24creator_with_result_typeINS_17ArraySumDecimalV3EEESt10shared_ptrINS_9IFunctionEERKS5_IKNS_9IDataTypeEE
Unexecuted instantiation: _ZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE24creator_with_result_typeINS_17ArrayAvgDecimalV3EEESt10shared_ptrINS_9IFunctionEERKS5_IKNS_9IDataTypeEE
Unexecuted instantiation: _ZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE24creator_with_result_typeINS_21ArrayProductDecimalV3EEESt10shared_ptrINS_9IFunctionEERKS5_IKNS_9IDataTypeEE
Unexecuted instantiation: _ZN5doris41simple_function_creator_with_result_type0IJLNS_13PrimitiveTypeE30ELS1_35EEE24creator_with_result_typeINS_19FunctionArrayCumSumEEESt10shared_ptrINS_9IFunctionEERKS5_IKNS_9IDataTypeEE
599
};
600
601
class DefaultFunctionBuilder : public FunctionBuilderImpl {
602
public:
603
    explicit DefaultFunctionBuilder(std::shared_ptr<IFunction> function_)
604
17.1k
            : function(std::move(function_)) {}
605
606
    // template <template <PrimitiveType> class FunctionTemplate>
607
    explicit DefaultFunctionBuilder(DataTypePtr return_type)
608
0
            : _return_type(std::move(return_type)) {}
609
610
    template <template <PrimitiveType> class FunctionTemplate>
611
0
    static FunctionBuilderPtr create_array_agg_function_decimalv3(DataTypePtr return_type) {
612
0
        auto builder = std::make_shared<DefaultFunctionBuilder>(return_type);
613
0
        DataTypePtr real_return_type;
614
        // for array_cum_sum, the return type is array,
615
        // so here should check nested type
616
0
        if (PrimitiveType::TYPE_ARRAY == return_type->get_primitive_type()) {
617
0
            const DataTypeArray* data_type_array =
618
0
                    static_cast<const DataTypeArray*>(remove_nullable(return_type).get());
619
0
            real_return_type = data_type_array->get_nested_type();
620
0
        } else {
621
0
            real_return_type = return_type;
622
0
        }
623
0
        builder->function =
624
0
                simple_function_creator_with_result_type0<TYPE_DECIMAL128I, TYPE_DECIMAL256>::
625
0
                        creator_with_result_type<FunctionTemplate>(real_return_type);
626
0
        return builder;
627
0
    }
Unexecuted instantiation: _ZN5doris22DefaultFunctionBuilder35create_array_agg_function_decimalv3INS_17ArraySumDecimalV3EEESt10shared_ptrINS_16IFunctionBuilderEES3_IKNS_9IDataTypeEE
Unexecuted instantiation: _ZN5doris22DefaultFunctionBuilder35create_array_agg_function_decimalv3INS_17ArrayAvgDecimalV3EEESt10shared_ptrINS_16IFunctionBuilderEES3_IKNS_9IDataTypeEE
Unexecuted instantiation: _ZN5doris22DefaultFunctionBuilder35create_array_agg_function_decimalv3INS_21ArrayProductDecimalV3EEESt10shared_ptrINS_16IFunctionBuilderEES3_IKNS_9IDataTypeEE
Unexecuted instantiation: _ZN5doris22DefaultFunctionBuilder35create_array_agg_function_decimalv3INS_19FunctionArrayCumSumEEESt10shared_ptrINS_16IFunctionBuilderEES3_IKNS_9IDataTypeEE
628
629
14.7k
    void check_number_of_arguments(size_t number_of_arguments) const override {
630
14.7k
        function->check_number_of_arguments(number_of_arguments);
631
14.7k
    }
632
633
508
    String get_name() const override { return function->get_name(); }
634
1.16k
    bool is_variadic() const override { return function->is_variadic(); }
635
0
    size_t get_number_of_arguments() const override { return function->get_number_of_arguments(); }
636
637
0
    ColumnNumbers get_arguments_that_are_always_constant() const override {
638
0
        return function->get_arguments_that_are_always_constant();
639
0
    }
640
641
protected:
642
0
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
643
0
        return function->get_return_type_impl(arguments);
644
0
    }
645
14.7k
    DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override {
646
14.7k
        return function->get_return_type_impl(arguments);
647
14.7k
    }
648
649
14.7k
    bool use_default_implementation_for_nulls() const override {
650
14.7k
        return function->use_default_implementation_for_nulls();
651
14.7k
    }
652
653
14.7k
    bool skip_return_type_check() const override { return function->skip_return_type_check(); }
654
655
0
    bool need_replace_null_data_to_default() const override {
656
0
        return function->need_replace_null_data_to_default();
657
0
    }
658
659
    FunctionBasePtr build_impl(const ColumnsWithTypeAndName& arguments,
660
14.7k
                               const DataTypePtr& return_type) const override {
661
14.7k
        DataTypes data_types(arguments.size());
662
54.3k
        for (size_t i = 0; i < arguments.size(); ++i) {
663
39.5k
            data_types[i] = arguments[i].type;
664
39.5k
        }
665
14.7k
        return std::make_shared<DefaultFunction>(function, data_types, return_type);
666
14.7k
    }
667
668
1.19k
    DataTypes get_variadic_argument_types_impl() const override {
669
1.19k
        return function->get_variadic_argument_types_impl();
670
1.19k
    }
671
672
private:
673
    std::shared_ptr<IFunction> function;
674
    DataTypePtr _return_type;
675
};
676
677
using FunctionPtr = std::shared_ptr<IFunction>;
678
/** Return ColumnNullable of src, with null map as OR-ed null maps of args columns in blocks.
679
  * Or ColumnConst(ColumnNullable) if the result is always NULL or if the result is constant and always not NULL.
680
  */
681
ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args,
682
                           size_t input_rows_count);
683
684
} // namespace doris