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