Coverage Report

Created: 2026-07-27 20:25

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
130
    bool is_nullable = false;                                                                  \
65
130
    bool is_datev2 = false;                                                                    \
66
242
    for (auto it : arguments) {                                                                \
67
242
        is_nullable = is_nullable || it.type->is_nullable();                                   \
68
242
        is_datev2 = is_datev2 || it.type->get_primitive_type() == TYPE_DATEV2 ||               \
69
242
                    it.type->get_primitive_type() == TYPE_DATETIMEV2;                          \
70
242
    }                                                                                          \
71
130
    return is_nullable || !is_datev2                                                           \
72
130
                   ? make_nullable(                                                            \
73
124
                             std::make_shared<typename PrimitiveTypeTraits<TYPE>::DataType>()) \
74
130
                   : 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
202k
    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
216k
    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
234k
    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
140k
    virtual Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) {
189
140k
        return Status::OK();
190
140k
    }
191
192
    Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
193
197k
                   uint32_t result, size_t input_rows_count) const {
194
        // Some function implementations may not handle the case where input_rows_count is 0
195
        // (e.g., some functions access the 0th row of input columns during execution).
196
        // Additionally, some UDF functions may hang if they write 0 rows and then try to read.
197
        // Therefore, before executing the function, we first check if input_rows_count is 0.
198
        // If it is 0, we directly return an empty result column to avoid executing the function body.
199
197k
        if (input_rows_count == 0) {
200
24
            block.get_by_position(result).column =
201
24
                    block.get_by_position(result).type->create_column();
202
24
            return Status::OK();
203
24
        }
204
197k
        try {
205
197k
            return prepare(context, block, arguments, result)
206
197k
                    ->execute(context, block, arguments, result, input_rows_count);
207
197k
        } catch (const Exception& e) {
208
2
            return e.to_status();
209
2
        }
210
197k
    }
211
212
    virtual Status evaluate_inverted_index(
213
            const ColumnsWithTypeAndName& arguments,
214
            const std::vector<IndexFieldNameAndTypePair>& data_type_with_names,
215
            std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows,
216
            const InvertedIndexAnalyzerCtx* analyzer_ctx,
217
0
            segment_v2::InvertedIndexResultBitmap& bitmap_result) const {
218
0
        return Status::OK();
219
0
    }
220
221
    /// Do cleaning work when function is finished, i.e., release state variables in the
222
    /// `FunctionContext` which are registered in `prepare` phase.
223
103k
    virtual Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) {
224
103k
        return Status::OK();
225
103k
    }
226
227
    virtual bool is_use_default_implementation_for_constants() const = 0;
228
229
0
    virtual bool is_udf_function() const { return false; }
230
231
0
    virtual bool can_push_down_to_index() const { return false; }
232
233
618
    virtual bool is_blockable() const { return false; }
234
235
    virtual ZoneMapFilterResult evaluate_zonemap_filter(const ZoneMapEvalContext& ctx,
236
                                                        const VExprSPtrs& function_arguments) const;
237
238
44
    virtual bool can_evaluate_zonemap_filter(const VExprSPtrs& /*function_arguments*/) const {
239
44
        return false;
240
44
    }
241
242
    virtual ZoneMapFilterResult evaluate_dictionary_filter(
243
            const DictionaryEvalContext& ctx, const VExprSPtrs& function_arguments) const;
244
245
0
    virtual bool can_evaluate_dictionary_filter(const VExprSPtrs& /*function_arguments*/) const {
246
0
        return false;
247
0
    }
248
249
    virtual ZoneMapFilterResult evaluate_bloom_filter(const BloomFilterEvalContext& ctx,
250
                                                      const VExprSPtrs& function_arguments) const;
251
252
0
    virtual bool can_evaluate_bloom_filter(const VExprSPtrs& /*function_arguments*/) const {
253
0
        return false;
254
0
    }
255
};
256
257
using FunctionBasePtr = std::shared_ptr<IFunctionBase>;
258
259
/// Creates IFunctionBase from argument types list.
260
class IFunctionBuilder {
261
public:
262
238k
    virtual ~IFunctionBuilder() = default;
263
264
    /// Get the main function name.
265
    virtual String get_name() const = 0;
266
267
    /// Override and return true if function could take different number of arguments.
268
    ///TODO: this function is not actually used now. but in check_number_of_arguments we still need it because for many
269
    /// functions we didn't set the correct number of arguments.
270
    virtual bool is_variadic() const = 0;
271
272
    /// For non-variadic functions, return number of arguments; otherwise return zero (that should be ignored).
273
    virtual size_t get_number_of_arguments() const = 0;
274
275
    /// Throw if number of arguments is incorrect. Default implementation will check only in non-variadic case.
276
    virtual void check_number_of_arguments(size_t number_of_arguments) const = 0;
277
278
    /// Check arguments and return IFunctionBase.
279
    virtual FunctionBasePtr build(const ColumnsWithTypeAndName& arguments,
280
                                  const DataTypePtr& return_type) const = 0;
281
282
    /// For higher-order functions (functions, that have lambda expression as at least one argument).
283
    /// You pass data types with empty DataTypeFunction for lambda arguments.
284
    /// This function will replace it with DataTypeFunction containing actual types.
285
    virtual DataTypes get_variadic_argument_types() const = 0;
286
287
    /// Returns indexes of arguments, that must be ColumnConst
288
    virtual ColumnNumbers get_arguments_that_are_always_constant() const = 0;
289
};
290
291
using FunctionBuilderPtr = std::shared_ptr<IFunctionBuilder>;
292
293
6
inline std::string get_types_string(const ColumnsWithTypeAndName& arguments) {
294
6
    std::string types;
295
6
    for (const auto& argument : arguments) {
296
4
        if (!types.empty()) {
297
2
            types += ", ";
298
2
        }
299
4
        types += argument.type->get_name();
300
4
    }
301
6
    return types;
302
6
}
303
304
/// used in function_factory. when we register a function, save a builder. to get a function, to get a builder.
305
/// will use DefaultFunctionBuilder as the default builder in function's registration if we didn't explicitly specify.
306
class FunctionBuilderImpl : public IFunctionBuilder {
307
public:
308
    FunctionBasePtr build(const ColumnsWithTypeAndName& arguments,
309
199k
                          const DataTypePtr& return_type) const final {
310
199k
        if (skip_return_type_check()) {
311
169k
            return build_impl(arguments, return_type);
312
169k
        }
313
29.5k
        const DataTypePtr& func_return_type = get_return_type(arguments);
314
29.5k
        if (func_return_type == nullptr) {
315
2
            throw doris::Exception(
316
2
                    ErrorCode::INTERNAL_ERROR,
317
2
                    "function return type check failed, function_name={}, "
318
2
                    "expect_return_type={}, real_return_type is nullptr, input_arguments={}",
319
2
                    get_name(), return_type->get_name(), get_types_string(arguments));
320
2
        }
321
322
        // check return types equal.
323
29.5k
        if (!(return_type->equals(*func_return_type) ||
324
              // For null constant argument, `get_return_type` would return
325
              // Nullable<DataTypeNothing> when `use_default_implementation_for_nulls` is true.
326
29.5k
              (return_type->is_nullable() && func_return_type->is_nullable() &&
327
114
               ((DataTypeNullable*)func_return_type.get())
328
64
                               ->get_nested_type()
329
64
                               ->get_primitive_type() == INVALID_TYPE) ||
330
29.5k
              is_date_or_datetime_or_decimal(return_type, func_return_type) ||
331
29.5k
              is_nested_type_date_or_datetime_or_decimal(return_type, func_return_type))) {
332
4
            throw doris::Exception(
333
4
                    ErrorCode::INTERNAL_ERROR,
334
4
                    "function return type check failed, function_name={}, "
335
4
                    "fe plan return type={},  be real return type={}, input_arguments={}",
336
4
                    get_name(), return_type->get_name(), func_return_type->get_name(),
337
4
                    get_types_string(arguments));
338
4
        }
339
29.5k
        return build_impl(arguments, return_type);
340
29.5k
    }
341
342
21.8k
    bool is_variadic() const override { return false; }
343
344
    // Default implementation. Will check only in non-variadic case.
345
    void check_number_of_arguments(size_t number_of_arguments) const override;
346
    // the return type should be same with what FE plans.
347
    // it returns: `get_return_type_impl` if `use_default_implementation_for_nulls` = false
348
    //  `get_return_type_impl` warpped in NULL if `use_default_implementation_for_nulls` = true and input has NULL
349
    DataTypePtr get_return_type(const ColumnsWithTypeAndName& arguments) const;
350
351
2.41k
    DataTypes get_variadic_argument_types() const override {
352
2.41k
        return get_variadic_argument_types_impl();
353
2.41k
    }
354
355
0
    ColumnNumbers get_arguments_that_are_always_constant() const override { return {}; }
356
357
protected:
358
    // Get the result type by argument type. If the function does not apply to these arguments, throw an exception.
359
    // the get_return_type_impl and its overrides should only return the nested type if `use_default_implementation_for_nulls` is true.
360
    // whether to wrap in nullable type will be automatically decided.
361
28.9k
    virtual DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const {
362
28.9k
        DataTypes data_types(arguments.size());
363
107k
        for (size_t i = 0; i < arguments.size(); ++i) {
364
78.1k
            data_types[i] = arguments[i].type;
365
78.1k
        }
366
28.9k
        return get_return_type_impl(data_types);
367
28.9k
    }
368
369
0
    virtual DataTypePtr get_return_type_impl(const DataTypes& /*arguments*/) const {
370
0
        throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
371
0
                               "get_return_type is not implemented for {}", get_name());
372
0
        return nullptr;
373
0
    }
374
375
    /** If use_default_implementation_for_nulls() is true, than change arguments for get_return_type() and build_impl():
376
      *  if some of arguments are Nullable(Nothing) then don't call get_return_type(), call build_impl() with return_type = Nullable(Nothing),
377
      *  if some of arguments are Nullable, then:
378
      *   - Nullable types are substituted with nested types for get_return_type() function
379
      *   - WRAP get_return_type() RESULT IN NULLABLE type and pass to build_impl
380
      *
381
      * Otherwise build returns build_impl(arguments, get_return_type(arguments));
382
      */
383
0
    virtual bool use_default_implementation_for_nulls() const { return true; }
384
385
0
    virtual bool skip_return_type_check() const { return false; }
386
387
0
    virtual bool need_replace_null_data_to_default() const { return false; }
388
389
    /// return a real function object to execute. called in build(...).
390
    virtual FunctionBasePtr build_impl(const ColumnsWithTypeAndName& arguments,
391
                                       const DataTypePtr& return_type) const = 0;
392
393
740
    virtual DataTypes get_variadic_argument_types_impl() const { return {}; }
394
395
private:
396
    bool is_date_or_datetime_or_decimal(const DataTypePtr& return_type,
397
                                        const DataTypePtr& func_return_type) const;
398
    bool is_nested_type_date_or_datetime_or_decimal(const DataTypePtr& return_type,
399
                                                    const DataTypePtr& func_return_type) const;
400
};
401
402
/// Previous function interface.
403
class IFunction : public std::enable_shared_from_this<IFunction>,
404
                  public FunctionBuilderImpl,
405
                  public IFunctionBase,
406
                  public PreparedFunctionImpl {
407
public:
408
    String get_name() const override = 0;
409
410
    /// Notice: We should not change the column in the block, because the column may be shared by multiple expressions or exec nodes.
411
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
412
                        uint32_t result, size_t input_rows_count) const override = 0;
413
414
    /// Override this functions to change default implementation behavior. See details in IMyFunction.
415
73.2k
    bool use_default_implementation_for_nulls() const override { return true; }
416
417
29.5k
    bool skip_return_type_check() const override { return false; }
418
419
18.0k
    bool need_replace_null_data_to_default() const override { return false; }
420
421
    /// all constancy check should use this function to do automatically
422
47.6k
    ColumnNumbers get_arguments_that_are_always_constant() const override { return {}; }
423
424
496
    bool is_use_default_implementation_for_constants() const override {
425
496
        return use_default_implementation_for_constants();
426
496
    }
427
428
    using PreparedFunctionImpl::execute;
429
    using FunctionBuilderImpl::get_return_type_impl;
430
    using FunctionBuilderImpl::get_variadic_argument_types_impl;
431
    using FunctionBuilderImpl::get_return_type;
432
433
    [[noreturn]] PreparedFunctionPtr prepare(FunctionContext* context,
434
                                             const Block& /*sample_block*/,
435
                                             const ColumnNumbers& /*arguments*/,
436
0
                                             uint32_t /*result*/) const final {
437
0
        throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
438
0
                               "prepare is not implemented for IFunction {}", get_name());
439
0
        __builtin_unreachable();
440
0
    }
441
442
54.6k
    Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override {
443
54.6k
        return Status::OK();
444
54.6k
    }
445
446
0
    [[noreturn]] const DataTypes& get_argument_types() const final {
447
0
        throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
448
0
                               "get_argument_types is not implemented for IFunction {}",
449
0
                               get_name());
450
0
        __builtin_unreachable();
451
0
    }
452
453
0
    [[noreturn]] const DataTypePtr& get_return_type() const final {
454
0
        throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
455
0
                               "get_return_type is not implemented for IFunction {}", get_name());
456
0
        __builtin_unreachable();
457
0
    }
458
459
protected:
460
    FunctionBasePtr build_impl(const ColumnsWithTypeAndName& /*arguments*/,
461
0
                               const DataTypePtr& /*return_type*/) const final {
462
0
        throw doris::Exception(ErrorCode::NOT_IMPLEMENTED_ERROR,
463
0
                               "build_impl is not implemented for IFunction {}", get_name());
464
0
        __builtin_unreachable();
465
0
        return {};
466
0
    }
467
};
468
469
/*
470
 * when we register a function which didn't specify its base(i.e. inherited from IFunction), actually we use this as a wrapper.
471
 * it saves real implementation as `function`. 
472
*/
473
class DefaultFunction final : public IFunctionBase {
474
public:
475
    DefaultFunction(std::shared_ptr<IFunction> function_, DataTypes arguments_,
476
                    DataTypePtr return_type_)
477
29.6k
            : function(std::move(function_)),
478
29.6k
              arguments(std::move(arguments_)),
479
29.6k
              return_type(std::move(return_type_)) {}
480
481
28
    String get_name() const override { return function->get_name(); }
482
483
0
    const DataTypes& get_argument_types() const override { return arguments; }
484
12
    const DataTypePtr& get_return_type() const override { return return_type; }
485
486
    // return a default wrapper for IFunction.
487
    PreparedFunctionPtr prepare(FunctionContext* context, const Block& /*sample_block*/,
488
                                const ColumnNumbers& /*arguments*/,
489
29.3k
                                uint32_t /*result*/) const override {
490
29.3k
        return function;
491
29.3k
    }
492
493
8
    double execute_cost() const override { return function->execute_cost(); }
494
495
58.3k
    Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override {
496
58.3k
        return function->open(context, scope);
497
58.3k
    }
498
499
58.3k
    Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) override {
500
58.3k
        return function->close(context, scope);
501
58.3k
    }
502
503
    Status evaluate_inverted_index(
504
            const ColumnsWithTypeAndName& args,
505
            const std::vector<IndexFieldNameAndTypePair>& data_type_with_names,
506
            std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows,
507
            const InvertedIndexAnalyzerCtx* analyzer_ctx,
508
28
            segment_v2::InvertedIndexResultBitmap& bitmap_result) const override {
509
28
        return function->evaluate_inverted_index(args, data_type_with_names, iterators, num_rows,
510
28
                                                 analyzer_ctx, bitmap_result);
511
28
    }
512
513
496
    bool is_use_default_implementation_for_constants() const override {
514
496
        return function->is_use_default_implementation_for_constants();
515
496
    }
516
517
0
    bool can_push_down_to_index() const override { return function->can_push_down_to_index(); }
518
519
618
    bool is_blockable() const override { return function->is_blockable(); }
520
521
    ZoneMapFilterResult evaluate_zonemap_filter(
522
106
            const ZoneMapEvalContext& ctx, const VExprSPtrs& function_arguments) const override {
523
106
        return function->evaluate_zonemap_filter(ctx, function_arguments);
524
106
    }
525
526
442
    bool can_evaluate_zonemap_filter(const VExprSPtrs& function_arguments) const override {
527
442
        return function->can_evaluate_zonemap_filter(function_arguments);
528
442
    }
529
530
    ZoneMapFilterResult evaluate_dictionary_filter(
531
8
            const DictionaryEvalContext& ctx, const VExprSPtrs& function_arguments) const override {
532
8
        return function->evaluate_dictionary_filter(ctx, function_arguments);
533
8
    }
534
535
134
    bool can_evaluate_dictionary_filter(const VExprSPtrs& function_arguments) const override {
536
134
        return function->can_evaluate_dictionary_filter(function_arguments);
537
134
    }
538
539
    ZoneMapFilterResult evaluate_bloom_filter(const BloomFilterEvalContext& ctx,
540
4
                                              const VExprSPtrs& function_arguments) const override {
541
4
        return function->evaluate_bloom_filter(ctx, function_arguments);
542
4
    }
543
544
58
    bool can_evaluate_bloom_filter(const VExprSPtrs& function_arguments) const override {
545
58
        return function->can_evaluate_bloom_filter(function_arguments);
546
58
    }
547
548
private:
549
    std::shared_ptr<IFunction> function;
550
    DataTypes arguments;
551
    DataTypePtr return_type;
552
};
553
554
struct simple_function_creator_without_type0 {
555
    template <typename AggregateFunctionTemplate, typename... TArgs>
556
0
    static std::shared_ptr<IFunction> create(const DataTypePtr& result_type, TArgs&&... args) {
557
0
        std::unique_ptr<IFunction> result(std::make_unique<AggregateFunctionTemplate>(
558
0
                result_type, std::forward<TArgs>(args)...));
559
0
        return std::shared_ptr<IFunction>(result.release());
560
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_
561
};
562
template <template <PrimitiveType> class FunctionTemplate>
563
struct SimpleFunctionCurryDirectWithResultType0 {
564
    template <PrimitiveType ResultType>
565
    using T = FunctionTemplate<ResultType>;
566
};
567
template <PrimitiveType... AllowedTypes>
568
struct simple_function_creator_with_result_type0 {
569
    template <typename Class, typename... TArgs>
570
    static std::shared_ptr<IFunction> create_base_with_result_type(const DataTypePtr& result_type,
571
0
                                                                   TArgs&&... args) {
572
0
        auto create = [&]<PrimitiveType ResultType>() {
573
0
            return simple_function_creator_without_type0::create<
574
0
                    typename Class::template T<ResultType>>(result_type,
575
0
                                                            std::forward<TArgs>(args)...);
576
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
577
0
        std::shared_ptr<IFunction> result = nullptr;
578
0
        auto type = result_type->get_primitive_type();
579
580
0
        (
581
0
                [&] {
582
0
                    if (type == AllowedTypes) {
583
0
                        static_assert(AllowedTypes == TYPE_DECIMAL128I ||
584
0
                                      AllowedTypes == TYPE_DECIMAL256);
585
0
                        result = create.template operator()<AllowedTypes>();
586
0
                    }
587
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
588
0
                ...);
589
590
0
        return result;
591
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_
592
593
    // Create agg function with result type from FE.
594
    // Currently only used for decimalv3 sum and avg.
595
    template <template <PrimitiveType> class FunctionTemplate>
596
0
    static std::shared_ptr<IFunction> creator_with_result_type(const DataTypePtr& result_type) {
597
0
        return create_base_with_result_type<
598
0
                SimpleFunctionCurryDirectWithResultType0<FunctionTemplate>>(result_type);
599
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
600
};
601
602
class DefaultFunctionBuilder : public FunctionBuilderImpl {
603
public:
604
    explicit DefaultFunctionBuilder(std::shared_ptr<IFunction> function_)
605
34.3k
            : function(std::move(function_)) {}
606
607
    // template <template <PrimitiveType> class FunctionTemplate>
608
    explicit DefaultFunctionBuilder(DataTypePtr return_type)
609
0
            : _return_type(std::move(return_type)) {}
610
611
    template <template <PrimitiveType> class FunctionTemplate>
612
0
    static FunctionBuilderPtr create_array_agg_function_decimalv3(DataTypePtr return_type) {
613
0
        auto builder = std::make_shared<DefaultFunctionBuilder>(return_type);
614
0
        DataTypePtr real_return_type;
615
        // for array_cum_sum, the return type is array,
616
        // so here should check nested type
617
0
        if (PrimitiveType::TYPE_ARRAY == return_type->get_primitive_type()) {
618
0
            const DataTypeArray* data_type_array =
619
0
                    static_cast<const DataTypeArray*>(remove_nullable(return_type).get());
620
0
            real_return_type = data_type_array->get_nested_type();
621
0
        } else {
622
0
            real_return_type = return_type;
623
0
        }
624
0
        builder->function =
625
0
                simple_function_creator_with_result_type0<TYPE_DECIMAL128I, TYPE_DECIMAL256>::
626
0
                        creator_with_result_type<FunctionTemplate>(real_return_type);
627
0
        return builder;
628
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
629
630
29.6k
    void check_number_of_arguments(size_t number_of_arguments) const override {
631
29.6k
        function->check_number_of_arguments(number_of_arguments);
632
29.6k
    }
633
634
1.01k
    String get_name() const override { return function->get_name(); }
635
2.34k
    bool is_variadic() const override { return function->is_variadic(); }
636
0
    size_t get_number_of_arguments() const override { return function->get_number_of_arguments(); }
637
638
0
    ColumnNumbers get_arguments_that_are_always_constant() const override {
639
0
        return function->get_arguments_that_are_always_constant();
640
0
    }
641
642
protected:
643
0
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
644
0
        return function->get_return_type_impl(arguments);
645
0
    }
646
29.6k
    DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override {
647
29.6k
        return function->get_return_type_impl(arguments);
648
29.6k
    }
649
650
29.5k
    bool use_default_implementation_for_nulls() const override {
651
29.5k
        return function->use_default_implementation_for_nulls();
652
29.5k
    }
653
654
29.6k
    bool skip_return_type_check() const override { return function->skip_return_type_check(); }
655
656
0
    bool need_replace_null_data_to_default() const override {
657
0
        return function->need_replace_null_data_to_default();
658
0
    }
659
660
    FunctionBasePtr build_impl(const ColumnsWithTypeAndName& arguments,
661
29.5k
                               const DataTypePtr& return_type) const override {
662
29.5k
        DataTypes data_types(arguments.size());
663
108k
        for (size_t i = 0; i < arguments.size(); ++i) {
664
79.3k
            data_types[i] = arguments[i].type;
665
79.3k
        }
666
29.5k
        return std::make_shared<DefaultFunction>(function, data_types, return_type);
667
29.5k
    }
668
669
2.40k
    DataTypes get_variadic_argument_types_impl() const override {
670
2.40k
        return function->get_variadic_argument_types_impl();
671
2.40k
    }
672
673
private:
674
    std::shared_ptr<IFunction> function;
675
    DataTypePtr _return_type;
676
};
677
678
using FunctionPtr = std::shared_ptr<IFunction>;
679
/** Return ColumnNullable of src, with null map as OR-ed null maps of args columns in blocks.
680
  * Or ColumnConst(ColumnNullable) if the result is always NULL or if the result is constant and always not NULL.
681
  */
682
ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args,
683
                           size_t input_rows_count);
684
685
} // namespace doris