Coverage Report

Created: 2026-06-03 17:31

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