Coverage Report

Created: 2026-07-28 01:21

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
    bool new_version_bitmap_op_count {false};
61
};
62
63
#define RETURN_REAL_TYPE_FOR_DATEV2_FUNCTION(TYPE)                                             \
64
65
    bool is_nullable = false;                                                                  \
65
65
    bool is_datev2 = false;                                                                    \
66
121
    for (auto it : arguments) {                                                                \
67
121
        is_nullable = is_nullable || it.type->is_nullable();                                   \
68
121
        is_datev2 = is_datev2 || it.type->get_primitive_type() == TYPE_DATEV2 ||               \
69
121
                    it.type->get_primitive_type() == TYPE_DATETIMEV2;                          \
70
121
    }                                                                                          \
71
65
    return is_nullable || !is_datev2                                                           \
72
65
                   ? make_nullable(                                                            \
73
62
                             std::make_shared<typename PrimitiveTypeTraits<TYPE>::DataType>()) \
74
65
                   : std::make_shared<typename PrimitiveTypeTraits<TYPE>::DataType>();
75
76
#define SET_NULLMAP_IF_FALSE(EXPR) \
77
    if (!EXPR) [[unlikely]] {      \
78
        null_map[i] = true;        \
79
    }
80
81
class Field;
82
class VExpr;
83
class ZoneMapEvalContext;
84
85
// Only use dispose the variadic argument
86
template <typename T>
87
auto has_variadic_argument_types(T&& arg) -> decltype(T::get_variadic_argument_types()) {};
88
void has_variadic_argument_types(...);
89
90
template <typename T>
91
concept HasGetVariadicArgumentTypesImpl = requires(T t) {
92
    { t.get_variadic_argument_types_impl() } -> std::same_as<DataTypes>;
93
};
94
95
bool have_null_column(const Block& block, const ColumnNumbers& args);
96
bool have_null_column(const ColumnsWithTypeAndName& args);
97
98
/// The simplest executable object.
99
/// Motivation:
100
///  * Prepare something heavy once before main execution loop instead of doing it for each block.
101
///  * Provide const interface for IFunctionBase (later).
102
class IPreparedFunction {
103
public:
104
101k
    virtual ~IPreparedFunction() = default;
105
106
    /// Get the main function name.
107
    virtual String get_name() const = 0;
108
109
    virtual Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
110
                           uint32_t result, size_t input_rows_count) const = 0;
111
};
112
113
using PreparedFunctionPtr = std::shared_ptr<IPreparedFunction>;
114
115
class PreparedFunctionImpl : public IPreparedFunction {
116
public:
117
    Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
118
                   uint32_t result, size_t input_rows_count) const final;
119
120
    /** If the function have non-zero number of arguments,
121
      *  and if all arguments are constant, that we could automatically provide default implementation:
122
      *  arguments are converted to ordinary columns with single value which is not const, then function is executed as usual,
123
      *  and then the result is converted to constant column.
124
      */
125
108k
    virtual bool use_default_implementation_for_constants() const { return true; }
126
127
    /** If use_default_implementation_for_nulls() is true, after execute the function,
128
      * whether need to replace the nested data of null data to the default value.
129
      * E.g. for binary arithmetic exprs, need return true to avoid false overflow.
130
      */
131
0
    virtual bool need_replace_null_data_to_default() const { return false; }
132
133
protected:
134
    virtual Status execute_impl(FunctionContext* context, Block& block,
135
                                const ColumnNumbers& arguments, uint32_t result,
136
                                size_t input_rows_count) const = 0;
137
138
    /** Default implementation in presence of Nullable arguments or NULL constants as arguments is the following:
139
      *  if some of arguments are NULL constants then return NULL constant,
140
      *  if some of arguments are Nullable, then execute function as usual for block,
141
      *   where Nullable columns are substituted with nested columns (they have arbitrary values in rows corresponding to NULL value)
142
      *   and wrap result in Nullable column where NULLs are in all rows where any of arguments are NULL.
143
      */
144
0
    virtual bool use_default_implementation_for_nulls() const { return true; }
145
146
0
    virtual bool skip_return_type_check() const { return false; }
147
148
    /** Some arguments could remain constant during this implementation.
149
      * Every argument required const must write here and no checks elsewhere.
150
      */
151
0
    virtual ColumnNumbers get_arguments_that_are_always_constant() const { return {}; }
152
153
private:
154
    Status default_implementation_for_nulls(FunctionContext* context, Block& block,
155
                                            const ColumnNumbers& args, uint32_t result,
156
                                            size_t input_rows_count, bool* executed) const;
157
    Status default_implementation_for_constant_arguments(FunctionContext* context, Block& block,
158
                                                         const ColumnNumbers& args, uint32_t result,
159
                                                         size_t input_rows_count,
160
                                                         bool* executed) const;
161
    Status default_execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
162
                           uint32_t result, size_t input_rows_count) const;
163
    Status _execute_skipped_constant_deal(FunctionContext* context, Block& block,
164
                                          const ColumnNumbers& args, uint32_t result,
165
                                          size_t input_rows_count) const;
166
};
167
168
/// Function with known arguments and return type.
169
class IFunctionBase {
170
public:
171
117k
    virtual ~IFunctionBase() = default;
172
173
    /// Get the main function name.
174
    virtual String get_name() const = 0;
175
176
    virtual const DataTypes& get_argument_types() const = 0;
177
    virtual const DataTypePtr& get_return_type() const = 0;
178
179
0
    virtual double execute_cost() const { return 1.0; }
180
181
    /// Do preparations and return executable.
182
    /// sample_block should contain data types of arguments and values of constants, if relevant.
183
    virtual PreparedFunctionPtr prepare(FunctionContext* context, const Block& sample_block,
184
                                        const ColumnNumbers& arguments, uint32_t result) const = 0;
185
186
    /// Override this when function need to store state in the `FunctionContext`, or do some
187
    /// preparation work according to information from `FunctionContext`.
188
70.1k
    virtual Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) {
189
70.1k
        return Status::OK();
190
70.1k
    }
191
192
    // Return aggregate argument indexes that must be constant by function semantics.
193
    // Other arguments may also be constant in a query, but they are not listed here
194
    // unless FE checks that the position is always a constant argument.
195
329
    virtual const std::vector<size_t>& get_const_argument_indexes() const {
196
329
        static const std::vector<size_t> indexes;
197
329
        return indexes;
198
329
    }
199
200
    Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
201
98.6k
                   uint32_t result, size_t input_rows_count) const {
202
        // Some function implementations may not handle the case where input_rows_count is 0
203
        // (e.g., some functions access the 0th row of input columns during execution).
204
        // Additionally, some UDF functions may hang if they write 0 rows and then try to read.
205
        // Therefore, before executing the function, we first check if input_rows_count is 0.
206
        // If it is 0, we directly return an empty result column to avoid executing the function body.
207
98.6k
        if (input_rows_count == 0) {
208
12
            block.get_by_position(result).column =
209
12
                    block.get_by_position(result).type->create_column();
210
12
            return Status::OK();
211
12
        }
212
98.6k
        try {
213
98.6k
            return prepare(context, block, arguments, result)
214
98.6k
                    ->execute(context, block, arguments, result, input_rows_count);
215
98.6k
        } catch (const Exception& e) {
216
1
            return e.to_status();
217
1
        }
218
98.6k
    }
219
220
    virtual Status evaluate_inverted_index(
221
            const ColumnsWithTypeAndName& arguments,
222
            const std::vector<IndexFieldNameAndTypePair>& data_type_with_names,
223
            std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows,
224
            const InvertedIndexAnalyzerCtx* analyzer_ctx,
225
0
            segment_v2::InvertedIndexResultBitmap& bitmap_result) const {
226
0
        return Status::OK();
227
0
    }
228
229
    /// Do cleaning work when function is finished, i.e., release state variables in the
230
    /// `FunctionContext` which are registered in `prepare` phase.
231
51.9k
    virtual Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) {
232
51.9k
        return Status::OK();
233
51.9k
    }
234
235
    virtual bool is_use_default_implementation_for_constants() const = 0;
236
237
0
    virtual bool is_udf_function() const { return false; }
238
239
0
    virtual bool can_push_down_to_index() const { return false; }
240
241
309
    virtual bool is_blockable() const { return false; }
242
243
    virtual ZoneMapFilterResult evaluate_zonemap_filter(const ZoneMapEvalContext& ctx,
244
                                                        const VExprSPtrs& function_arguments) const;
245
246
22
    virtual bool can_evaluate_zonemap_filter(const VExprSPtrs& /*function_arguments*/) const {
247
22
        return false;
248
22
    }
249
250
    virtual ZoneMapFilterResult evaluate_dictionary_filter(
251
            const DictionaryEvalContext& ctx, const VExprSPtrs& function_arguments) const;
252
253
0
    virtual bool can_evaluate_dictionary_filter(const VExprSPtrs& /*function_arguments*/) const {
254
0
        return false;
255
0
    }
256
257
    virtual ZoneMapFilterResult evaluate_bloom_filter(const BloomFilterEvalContext& ctx,
258
                                                      const VExprSPtrs& function_arguments) const;
259
260
0
    virtual bool can_evaluate_bloom_filter(const VExprSPtrs& /*function_arguments*/) const {
261
0
        return false;
262
0
    }
263
};
264
265
using FunctionBasePtr = std::shared_ptr<IFunctionBase>;
266
267
/// Creates IFunctionBase from argument types list.
268
class IFunctionBuilder {
269
public:
270
119k
    virtual ~IFunctionBuilder() = default;
271
272
    /// Get the main function name.
273
    virtual String get_name() const = 0;
274
275
    /// Override and return true if function could take different number of arguments.
276
    ///TODO: this function is not actually used now. but in check_number_of_arguments we still need it because for many
277
    /// functions we didn't set the correct number of arguments.
278
    virtual bool is_variadic() const = 0;
279
280
    /// For non-variadic functions, return number of arguments; otherwise return zero (that should be ignored).
281
    virtual size_t get_number_of_arguments() const = 0;
282
283
    /// Throw if number of arguments is incorrect. Default implementation will check only in non-variadic case.
284
    virtual void check_number_of_arguments(size_t number_of_arguments) const = 0;
285
286
    /// Check arguments and return IFunctionBase.
287
    virtual FunctionBasePtr build(const ColumnsWithTypeAndName& arguments,
288
                                  const DataTypePtr& return_type) const = 0;
289
290
    /// For higher-order functions (functions, that have lambda expression as at least one argument).
291
    /// You pass data types with empty DataTypeFunction for lambda arguments.
292
    /// This function will replace it with DataTypeFunction containing actual types.
293
    virtual DataTypes get_variadic_argument_types() const = 0;
294
295
    /// Returns indexes of arguments, that must be ColumnConst
296
    virtual ColumnNumbers get_arguments_that_are_always_constant() const = 0;
297
};
298
299
using FunctionBuilderPtr = std::shared_ptr<IFunctionBuilder>;
300
301
3
inline std::string get_types_string(const ColumnsWithTypeAndName& arguments) {
302
3
    std::string types;
303
3
    for (const auto& argument : arguments) {
304
2
        if (!types.empty()) {
305
1
            types += ", ";
306
1
        }
307
2
        types += argument.type->get_name();
308
2
    }
309
3
    return types;
310
3
}
311
312
/// used in function_factory. when we register a function, save a builder. to get a function, to get a builder.
313
/// will use DefaultFunctionBuilder as the default builder in function's registration if we didn't explicitly specify.
314
class FunctionBuilderImpl : public IFunctionBuilder {
315
public:
316
    FunctionBasePtr build(const ColumnsWithTypeAndName& arguments,
317
99.5k
                          const DataTypePtr& return_type) const final {
318
99.5k
        if (skip_return_type_check()) {
319
84.7k
            return build_impl(arguments, return_type);
320
84.7k
        }
321
14.7k
        const DataTypePtr& func_return_type = get_return_type(arguments);
322
14.7k
        if (func_return_type == nullptr) {
323
1
            throw doris::Exception(
324
1
                    ErrorCode::INTERNAL_ERROR,
325
1
                    "function return type check failed, function_name={}, "
326
1
                    "expect_return_type={}, real_return_type is nullptr, input_arguments={}",
327
1
                    get_name(), return_type->get_name(), get_types_string(arguments));
328
1
        }
329
330
        // check return types equal.
331
14.7k
        if (!(return_type->equals(*func_return_type) ||
332
              // For null constant argument, `get_return_type` would return
333
              // Nullable<DataTypeNothing> when `use_default_implementation_for_nulls` is true.
334
14.7k
              (return_type->is_nullable() && func_return_type->is_nullable() &&
335
57
               ((DataTypeNullable*)func_return_type.get())
336
32
                               ->get_nested_type()
337
32
                               ->get_primitive_type() == INVALID_TYPE) ||
338
14.7k
              is_date_or_datetime_or_decimal(return_type, func_return_type) ||
339
14.7k
              is_nested_type_date_or_datetime_or_decimal(return_type, func_return_type))) {
340
2
            throw doris::Exception(
341
2
                    ErrorCode::INTERNAL_ERROR,
342
2
                    "function return type check failed, function_name={}, "
343
2
                    "fe plan return type={},  be real return type={}, input_arguments={}",
344
2
                    get_name(), return_type->get_name(), func_return_type->get_name(),
345
2
                    get_types_string(arguments));
346
2
        }
347
14.7k
        return build_impl(arguments, return_type);
348
14.7k
    }
349
350
10.9k
    bool is_variadic() const override { return false; }
351
352
    // Default implementation. Will check only in non-variadic case.
353
    void check_number_of_arguments(size_t number_of_arguments) const override;
354
    // the return type should be same with what FE plans.
355
    // it returns: `get_return_type_impl` if `use_default_implementation_for_nulls` = false
356
    //  `get_return_type_impl` warpped in NULL if `use_default_implementation_for_nulls` = true and input has NULL
357
    DataTypePtr get_return_type(const ColumnsWithTypeAndName& arguments) const;
358
359
1.20k
    DataTypes get_variadic_argument_types() const override {
360
1.20k
        return get_variadic_argument_types_impl();
361
1.20k
    }
362
363
0
    ColumnNumbers get_arguments_that_are_always_constant() const override { return {}; }
364
365
protected:
366
    // Get the result type by argument type. If the function does not apply to these arguments, throw an exception.
367
    // the get_return_type_impl and its overrides should only return the nested type if `use_default_implementation_for_nulls` is true.
368
    // whether to wrap in nullable type will be automatically decided.
369
14.4k
    virtual DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const {
370
14.4k
        DataTypes data_types(arguments.size());
371
53.5k
        for (size_t i = 0; i < arguments.size(); ++i) {
372
39.0k
            data_types[i] = arguments[i].type;
373
39.0k
        }
374
14.4k
        return get_return_type_impl(data_types);
375
14.4k
    }
376
377
0
    virtual DataTypePtr get_return_type_impl(const DataTypes& /*arguments*/) const {
378
0
        throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
379
0
                               "get_return_type is not implemented for {}", get_name());
380
0
        return nullptr;
381
0
    }
382
383
    /** If use_default_implementation_for_nulls() is true, than change arguments for get_return_type() and build_impl():
384
      *  if some of arguments are Nullable(Nothing) then don't call get_return_type(), call build_impl() with return_type = Nullable(Nothing),
385
      *  if some of arguments are Nullable, then:
386
      *   - Nullable types are substituted with nested types for get_return_type() function
387
      *   - WRAP get_return_type() RESULT IN NULLABLE type and pass to build_impl
388
      *
389
      * Otherwise build returns build_impl(arguments, get_return_type(arguments));
390
      */
391
0
    virtual bool use_default_implementation_for_nulls() const { return true; }
392
393
0
    virtual bool skip_return_type_check() const { return false; }
394
395
0
    virtual bool need_replace_null_data_to_default() const { return false; }
396
397
    /// return a real function object to execute. called in build(...).
398
    virtual FunctionBasePtr build_impl(const ColumnsWithTypeAndName& arguments,
399
                                       const DataTypePtr& return_type) const = 0;
400
401
370
    virtual DataTypes get_variadic_argument_types_impl() const { return {}; }
402
403
private:
404
    bool is_date_or_datetime_or_decimal(const DataTypePtr& return_type,
405
                                        const DataTypePtr& func_return_type) const;
406
    bool is_nested_type_date_or_datetime_or_decimal(const DataTypePtr& return_type,
407
                                                    const DataTypePtr& func_return_type) const;
408
};
409
410
/// Previous function interface.
411
class IFunction : public std::enable_shared_from_this<IFunction>,
412
                  public FunctionBuilderImpl,
413
                  public IFunctionBase,
414
                  public PreparedFunctionImpl {
415
public:
416
    String get_name() const override = 0;
417
418
    /// Notice: We should not change the column in the block, because the column may be shared by multiple expressions or exec nodes.
419
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
420
                        uint32_t result, size_t input_rows_count) const override = 0;
421
422
    /// Override this functions to change default implementation behavior. See details in IMyFunction.
423
36.6k
    bool use_default_implementation_for_nulls() const override { return true; }
424
425
14.7k
    bool skip_return_type_check() const override { return false; }
426
427
9.01k
    bool need_replace_null_data_to_default() const override { return false; }
428
429
    /// all constancy check should use this function to do automatically
430
23.8k
    ColumnNumbers get_arguments_that_are_always_constant() const override { return {}; }
431
432
248
    bool is_use_default_implementation_for_constants() const override {
433
248
        return use_default_implementation_for_constants();
434
248
    }
435
436
    using PreparedFunctionImpl::execute;
437
    using FunctionBuilderImpl::get_return_type_impl;
438
    using FunctionBuilderImpl::get_variadic_argument_types_impl;
439
    using FunctionBuilderImpl::get_return_type;
440
441
    [[noreturn]] PreparedFunctionPtr prepare(FunctionContext* context,
442
                                             const Block& /*sample_block*/,
443
                                             const ColumnNumbers& /*arguments*/,
444
0
                                             uint32_t /*result*/) const final {
445
0
        throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
446
0
                               "prepare is not implemented for IFunction {}", get_name());
447
0
        __builtin_unreachable();
448
0
    }
449
450
27.3k
    Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override {
451
27.3k
        return Status::OK();
452
27.3k
    }
453
454
0
    [[noreturn]] const DataTypes& get_argument_types() const final {
455
0
        throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
456
0
                               "get_argument_types is not implemented for IFunction {}",
457
0
                               get_name());
458
0
        __builtin_unreachable();
459
0
    }
460
461
0
    [[noreturn]] const DataTypePtr& get_return_type() const final {
462
0
        throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
463
0
                               "get_return_type is not implemented for IFunction {}", get_name());
464
0
        __builtin_unreachable();
465
0
    }
466
467
protected:
468
    FunctionBasePtr build_impl(const ColumnsWithTypeAndName& /*arguments*/,
469
0
                               const DataTypePtr& /*return_type*/) const final {
470
0
        throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
471
0
                               "build_impl is not implemented for IFunction {}", get_name());
472
0
        __builtin_unreachable();
473
0
        return {};
474
0
    }
475
};
476
477
/*
478
 * when we register a function which didn't specify its base(i.e. inherited from IFunction), actually we use this as a wrapper.
479
 * it saves real implementation as `function`. 
480
*/
481
class DefaultFunction final : public IFunctionBase {
482
public:
483
    DefaultFunction(std::shared_ptr<IFunction> function_, DataTypes arguments_,
484
                    DataTypePtr return_type_)
485
14.8k
            : function(std::move(function_)),
486
14.8k
              arguments(std::move(arguments_)),
487
14.8k
              return_type(std::move(return_type_)) {}
488
489
14
    String get_name() const override { return function->get_name(); }
490
491
0
    const DataTypes& get_argument_types() const override { return arguments; }
492
6
    const DataTypePtr& get_return_type() const override { return return_type; }
493
494
    // return a default wrapper for IFunction.
495
    PreparedFunctionPtr prepare(FunctionContext* context, const Block& /*sample_block*/,
496
                                const ColumnNumbers& /*arguments*/,
497
14.6k
                                uint32_t /*result*/) const override {
498
14.6k
        return function;
499
14.6k
    }
500
501
4
    double execute_cost() const override { return function->execute_cost(); }
502
503
29.1k
    Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override {
504
29.1k
        return function->open(context, scope);
505
29.1k
    }
506
507
29.1k
    Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) override {
508
29.1k
        return function->close(context, scope);
509
29.1k
    }
510
511
329
    const std::vector<size_t>& get_const_argument_indexes() const override {
512
329
        return function->get_const_argument_indexes();
513
329
    }
514
515
    Status evaluate_inverted_index(
516
            const ColumnsWithTypeAndName& args,
517
            const std::vector<IndexFieldNameAndTypePair>& data_type_with_names,
518
            std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows,
519
            const InvertedIndexAnalyzerCtx* analyzer_ctx,
520
14
            segment_v2::InvertedIndexResultBitmap& bitmap_result) const override {
521
14
        return function->evaluate_inverted_index(args, data_type_with_names, iterators, num_rows,
522
14
                                                 analyzer_ctx, bitmap_result);
523
14
    }
524
525
248
    bool is_use_default_implementation_for_constants() const override {
526
248
        return function->is_use_default_implementation_for_constants();
527
248
    }
528
529
0
    bool can_push_down_to_index() const override { return function->can_push_down_to_index(); }
530
531
309
    bool is_blockable() const override { return function->is_blockable(); }
532
533
    ZoneMapFilterResult evaluate_zonemap_filter(
534
53
            const ZoneMapEvalContext& ctx, const VExprSPtrs& function_arguments) const override {
535
53
        return function->evaluate_zonemap_filter(ctx, function_arguments);
536
53
    }
537
538
221
    bool can_evaluate_zonemap_filter(const VExprSPtrs& function_arguments) const override {
539
221
        return function->can_evaluate_zonemap_filter(function_arguments);
540
221
    }
541
542
    ZoneMapFilterResult evaluate_dictionary_filter(
543
4
            const DictionaryEvalContext& ctx, const VExprSPtrs& function_arguments) const override {
544
4
        return function->evaluate_dictionary_filter(ctx, function_arguments);
545
4
    }
546
547
67
    bool can_evaluate_dictionary_filter(const VExprSPtrs& function_arguments) const override {
548
67
        return function->can_evaluate_dictionary_filter(function_arguments);
549
67
    }
550
551
    ZoneMapFilterResult evaluate_bloom_filter(const BloomFilterEvalContext& ctx,
552
2
                                              const VExprSPtrs& function_arguments) const override {
553
2
        return function->evaluate_bloom_filter(ctx, function_arguments);
554
2
    }
555
556
29
    bool can_evaluate_bloom_filter(const VExprSPtrs& function_arguments) const override {
557
29
        return function->can_evaluate_bloom_filter(function_arguments);
558
29
    }
559
560
private:
561
    std::shared_ptr<IFunction> function;
562
    DataTypes arguments;
563
    DataTypePtr return_type;
564
};
565
566
struct simple_function_creator_without_type0 {
567
    template <typename AggregateFunctionTemplate, typename... TArgs>
568
0
    static std::shared_ptr<IFunction> create(const DataTypePtr& result_type, TArgs&&... args) {
569
0
        std::unique_ptr<IFunction> result(std::make_unique<AggregateFunctionTemplate>(
570
0
                result_type, std::forward<TArgs>(args)...));
571
0
        return std::shared_ptr<IFunction>(result.release());
572
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_
573
};
574
template <template <PrimitiveType> class FunctionTemplate>
575
struct SimpleFunctionCurryDirectWithResultType0 {
576
    template <PrimitiveType ResultType>
577
    using T = FunctionTemplate<ResultType>;
578
};
579
template <PrimitiveType... AllowedTypes>
580
struct simple_function_creator_with_result_type0 {
581
    template <typename Class, typename... TArgs>
582
    static std::shared_ptr<IFunction> create_base_with_result_type(const DataTypePtr& result_type,
583
0
                                                                   TArgs&&... args) {
584
0
        auto create = [&]<PrimitiveType ResultType>() {
585
0
            return simple_function_creator_without_type0::create<
586
0
                    typename Class::template T<ResultType>>(result_type,
587
0
                                                            std::forward<TArgs>(args)...);
588
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
589
0
        std::shared_ptr<IFunction> result = nullptr;
590
0
        auto type = result_type->get_primitive_type();
591
592
0
        (
593
0
                [&] {
594
0
                    if (type == AllowedTypes) {
595
0
                        static_assert(AllowedTypes == TYPE_DECIMAL128I ||
596
0
                                      AllowedTypes == TYPE_DECIMAL256);
597
0
                        result = create.template operator()<AllowedTypes>();
598
0
                    }
599
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
600
0
                ...);
601
602
0
        return result;
603
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_
604
605
    // Create agg function with result type from FE.
606
    // Currently only used for decimalv3 sum and avg.
607
    template <template <PrimitiveType> class FunctionTemplate>
608
0
    static std::shared_ptr<IFunction> creator_with_result_type(const DataTypePtr& result_type) {
609
0
        return create_base_with_result_type<
610
0
                SimpleFunctionCurryDirectWithResultType0<FunctionTemplate>>(result_type);
611
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
612
};
613
614
class DefaultFunctionBuilder : public FunctionBuilderImpl {
615
public:
616
    explicit DefaultFunctionBuilder(std::shared_ptr<IFunction> function_)
617
17.1k
            : function(std::move(function_)) {}
618
619
    // template <template <PrimitiveType> class FunctionTemplate>
620
    explicit DefaultFunctionBuilder(DataTypePtr return_type)
621
0
            : _return_type(std::move(return_type)) {}
622
623
    template <template <PrimitiveType> class FunctionTemplate>
624
0
    static FunctionBuilderPtr create_array_agg_function_decimalv3(DataTypePtr return_type) {
625
0
        auto builder = std::make_shared<DefaultFunctionBuilder>(return_type);
626
0
        DataTypePtr real_return_type;
627
        // for array_cum_sum, the return type is array,
628
        // so here should check nested type
629
0
        if (PrimitiveType::TYPE_ARRAY == return_type->get_primitive_type()) {
630
0
            const DataTypeArray* data_type_array =
631
0
                    static_cast<const DataTypeArray*>(remove_nullable(return_type).get());
632
0
            real_return_type = data_type_array->get_nested_type();
633
0
        } else {
634
0
            real_return_type = return_type;
635
0
        }
636
0
        builder->function =
637
0
                simple_function_creator_with_result_type0<TYPE_DECIMAL128I, TYPE_DECIMAL256>::
638
0
                        creator_with_result_type<FunctionTemplate>(real_return_type);
639
0
        return builder;
640
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
641
642
14.8k
    void check_number_of_arguments(size_t number_of_arguments) const override {
643
14.8k
        function->check_number_of_arguments(number_of_arguments);
644
14.8k
    }
645
646
509
    String get_name() const override { return function->get_name(); }
647
1.17k
    bool is_variadic() const override { return function->is_variadic(); }
648
0
    size_t get_number_of_arguments() const override { return function->get_number_of_arguments(); }
649
650
0
    ColumnNumbers get_arguments_that_are_always_constant() const override {
651
0
        return function->get_arguments_that_are_always_constant();
652
0
    }
653
654
protected:
655
0
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
656
0
        return function->get_return_type_impl(arguments);
657
0
    }
658
14.8k
    DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override {
659
14.8k
        return function->get_return_type_impl(arguments);
660
14.8k
    }
661
662
14.7k
    bool use_default_implementation_for_nulls() const override {
663
14.7k
        return function->use_default_implementation_for_nulls();
664
14.7k
    }
665
666
14.8k
    bool skip_return_type_check() const override { return function->skip_return_type_check(); }
667
668
0
    bool need_replace_null_data_to_default() const override {
669
0
        return function->need_replace_null_data_to_default();
670
0
    }
671
672
    FunctionBasePtr build_impl(const ColumnsWithTypeAndName& arguments,
673
14.7k
                               const DataTypePtr& return_type) const override {
674
14.7k
        DataTypes data_types(arguments.size());
675
54.4k
        for (size_t i = 0; i < arguments.size(); ++i) {
676
39.6k
            data_types[i] = arguments[i].type;
677
39.6k
        }
678
14.7k
        return std::make_shared<DefaultFunction>(function, data_types, return_type);
679
14.7k
    }
680
681
1.20k
    DataTypes get_variadic_argument_types_impl() const override {
682
1.20k
        return function->get_variadic_argument_types_impl();
683
1.20k
    }
684
685
private:
686
    std::shared_ptr<IFunction> function;
687
    DataTypePtr _return_type;
688
};
689
690
using FunctionPtr = std::shared_ptr<IFunction>;
691
/** Return ColumnNullable of src, with null map as OR-ed null maps of args columns in blocks.
692
  * Or ColumnConst(ColumnNullable) if the result is always NULL or if the result is constant and always not NULL.
693
  */
694
ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args,
695
                           size_t input_rows_count);
696
697
} // namespace doris