-
Notifications
You must be signed in to change notification settings - Fork 9k
Expand file tree
/
Copy patharrayAggregation.cpp
More file actions
668 lines (564 loc) · 30 KB
/
Copy patharrayAggregation.cpp
File metadata and controls
668 lines (564 loc) · 30 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
#include <base/defines.h>
#include <Columns/IColumn.h>
#include <Columns/ColumnConst.h>
#include <Columns/ColumnArray.h>
#include <Columns/ColumnDecimal.h>
#include <Core/callOnTypeIndex.h>
#include <DataTypes/DataTypeDate.h>
#include <DataTypes/DataTypesDecimal.h>
#include <DataTypes/DataTypesNumber.h>
#include <Functions/FunctionFactory.h>
#include <Functions/array/FunctionArrayMapped.h>
#include <Common/NaNUtils.h>
#include <Common/findExtreme.h>
namespace DB
{
namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int ILLEGAL_COLUMN;
extern const int DECIMAL_OVERFLOW;
extern const int ARGUMENT_OUT_OF_BOUND;
}
enum class AggregateOperation : uint8_t
{
min,
max,
sum,
average,
product
};
/**
* During array aggregation we derive result type from operation.
* For array min or array max we use array element as result type.
* For array average we use Float64.
* For array sum for big integers, we use same type representation, decimal numbers up to 128-bit will use Decimal128, then Decimal256.
* for floating point numbers Float64, for numeric unsigned Int64, and for numeric signed UInt64.
*/
template <typename ArrayElement, AggregateOperation operation>
struct ArrayAggregateResultImpl;
template <typename ArrayElement>
struct ArrayAggregateResultImpl<ArrayElement, AggregateOperation::min>
{
using Result = ArrayElement;
};
template <typename ArrayElement>
struct ArrayAggregateResultImpl<ArrayElement, AggregateOperation::max>
{
using Result = ArrayElement;
};
template <typename ArrayElement>
struct ArrayAggregateResultImpl<ArrayElement, AggregateOperation::average>
{
using Result = Float64;
};
template <typename ArrayElement>
struct ArrayAggregateResultImpl<ArrayElement, AggregateOperation::product>
{
using Result = Float64;
};
template <typename ArrayElement>
struct ArrayAggregateResultImpl<ArrayElement, AggregateOperation::sum>
{
using Result =
std::conditional_t<std::is_same_v<ArrayElement, Int128>, Int128,
std::conditional_t<std::is_same_v<ArrayElement, UInt128>, UInt128,
std::conditional_t<std::is_same_v<ArrayElement, Int256>, Int256,
std::conditional_t<std::is_same_v<ArrayElement, UInt256>, UInt256,
std::conditional_t<std::is_same_v<ArrayElement, Decimal32>, Decimal128,
std::conditional_t<std::is_same_v<ArrayElement, Decimal64>, Decimal128,
std::conditional_t<std::is_same_v<ArrayElement, Decimal128>, Decimal128,
std::conditional_t<std::is_same_v<ArrayElement, Decimal256>, Decimal256,
std::conditional_t<std::is_same_v<ArrayElement, DateTime64>, Decimal128,
std::conditional_t<is_floating_point<ArrayElement>, Float64,
std::conditional_t<std::is_signed_v<ArrayElement>, Int64,
UInt64>>>>>>>>>>>;
};
template <typename ArrayElement, AggregateOperation operation>
using ArrayAggregateResult = typename ArrayAggregateResultImpl<ArrayElement, operation>::Result;
template<AggregateOperation aggregate_operation>
struct ArrayAggregateImpl
{
static bool needBoolean() { return false; }
static bool needExpression() { return false; }
static bool needOneArray() { return false; }
static DataTypePtr getReturnType(const DataTypePtr & expression_return, const DataTypePtr & /*array_element*/)
{
if constexpr (aggregate_operation == AggregateOperation::max || aggregate_operation == AggregateOperation::min)
{
return expression_return;
}
DataTypePtr result;
auto call = [&](const auto & types)
{
using Types = std::decay_t<decltype(types)>;
using DataType = typename Types::LeftType;
if constexpr (!IsDataTypeDateOrDateTimeOrTime<DataType>)
{
if constexpr (aggregate_operation == AggregateOperation::average || aggregate_operation == AggregateOperation::product)
{
result = std::make_shared<DataTypeFloat64>();
return true;
}
else if constexpr (IsDataTypeNumber<DataType>)
{
using NumberReturnType = ArrayAggregateResult<typename DataType::FieldType, aggregate_operation>;
result = std::make_shared<DataTypeNumber<NumberReturnType>>();
return true;
}
else if constexpr (IsDataTypeDecimal<DataType>)
{
using DecimalReturnType = ArrayAggregateResult<typename DataType::FieldType, aggregate_operation>;
UInt32 scale = getDecimalScale(*expression_return);
result = std::make_shared<DataTypeDecimal<DecimalReturnType>>(DecimalUtils::max_precision<DecimalReturnType>, scale);
return true;
}
}
return false;
};
if (!callOnIndexAndDataType<void>(expression_return->getTypeId(), call))
{
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "array aggregation function cannot be performed on type {}",
expression_return->getName());
}
return result;
}
/// Vectorized fast path for plain numeric arrays: reduce each array slice with findExtremeMin/Max
/// (branchless SIMD horizontal min/max) instead of a per-element compareAt.
/// The result is bitwise-identical to the generic path: the extreme value is unique up to representation,
/// and the only value classes with multiple representations (NaN payloads and 0.0/-0.0) are fixed up below
/// to return the first occurrence, which is what compareAt-based selection returns.
template <typename Element>
requires(has_find_extreme_implementation<Element>)
static bool executeMinOrMaxNumeric(const ColumnPtr & mapped, const ColumnArray::Offsets & offsets, ColumnPtr & res_ptr)
{
const ColumnVector<Element> * column = checkAndGetColumn<ColumnVector<Element>>(&*mapped);
if (!column)
return false;
const Element * data = column->getData().data();
auto res_column = ColumnVector<Element>::create(offsets.size());
typename ColumnVector<Element>::Container & res = res_column->getData();
size_t pos = 0;
for (size_t i = 0; i < offsets.size(); ++i)
{
const size_t end_of_array = offsets[i];
/// Array is empty
if (pos == end_of_array)
{
res[i] = Element{};
continue;
}
std::optional<Element> result;
if constexpr (aggregate_operation == AggregateOperation::min)
result = findExtremeMin(data, pos, end_of_array);
else
result = findExtremeMax(data, pos, end_of_array);
chassert(result.has_value());
if constexpr (is_floating_point<Element>)
{
/// findExtreme* returns NaN only if all elements are NaN; the generic path returns the first of them.
if (isNaN(*result))
result = data[pos];
/// A zero result may be either 0.0 or -0.0 depending on reduction order; take the first zero in the array.
else if (*result == Element{})
{
for (size_t j = pos; j < end_of_array; ++j)
{
if (data[j] == Element{})
{
result = data[j];
break;
}
}
}
}
res[i] = *result;
pos = end_of_array;
}
res_ptr = std::move(res_column);
return true;
}
template <AggregateOperation op = aggregate_operation>
requires(op == AggregateOperation::min || op == AggregateOperation::max)
static void executeMinOrMax(const ColumnPtr & mapped, const ColumnArray::Offsets & offsets, ColumnPtr & res_ptr)
{
const ColumnConst * const_column = checkAndGetColumn<ColumnConst>(&*mapped);
if (const_column)
{
MutableColumnPtr res_column = const_column->getDataColumn().cloneEmpty();
const Field field = const_column->getField();
UInt64 pos = 0;
size_t first_non_empty = 0;
for (size_t i = 0; i < offsets.size(); ++i)
{
const auto end_of_array = offsets[i];
if (pos == end_of_array)
{
if (first_non_empty < i)
res_column->insertMany(field, i - first_non_empty);
res_column->insertDefault();
first_non_empty = i + 1;
}
pos = end_of_array;
}
if (first_non_empty < offsets.size())
res_column->insertMany(field, offsets.size() - first_non_empty);
res_ptr = std::move(res_column);
return;
}
if (executeMinOrMaxNumeric<UInt8>(mapped, offsets, res_ptr)
|| executeMinOrMaxNumeric<UInt16>(mapped, offsets, res_ptr)
|| executeMinOrMaxNumeric<UInt32>(mapped, offsets, res_ptr)
|| executeMinOrMaxNumeric<UInt64>(mapped, offsets, res_ptr)
|| executeMinOrMaxNumeric<Int8>(mapped, offsets, res_ptr)
|| executeMinOrMaxNumeric<Int16>(mapped, offsets, res_ptr)
|| executeMinOrMaxNumeric<Int32>(mapped, offsets, res_ptr)
|| executeMinOrMaxNumeric<Int64>(mapped, offsets, res_ptr)
|| executeMinOrMaxNumeric<UInt128>(mapped, offsets, res_ptr)
|| executeMinOrMaxNumeric<UInt256>(mapped, offsets, res_ptr)
|| executeMinOrMaxNumeric<Int128>(mapped, offsets, res_ptr)
|| executeMinOrMaxNumeric<Int256>(mapped, offsets, res_ptr)
|| executeMinOrMaxNumeric<Float32>(mapped, offsets, res_ptr)
|| executeMinOrMaxNumeric<Float64>(mapped, offsets, res_ptr))
return;
MutableColumnPtr res_column = mapped->cloneEmpty();
static constexpr int nan_null_direction_hint = aggregate_operation == AggregateOperation::min ? 1 : -1;
/// TODO: Introduce row_begin and row_end to getPermutation or an equivalent function to use that instead
/// (same use case as SingleValueDataBase::getSmallestIndex)
UInt64 start_of_array = 0;
for (auto end_of_array : offsets)
{
/// Array is empty
if (start_of_array == end_of_array)
{
res_column->insertDefault();
continue;
}
UInt64 index = start_of_array;
for (UInt64 i = index + 1; i < end_of_array; i++)
{
if constexpr (aggregate_operation == AggregateOperation::min)
{
if ((mapped->compareAt(i, index, *mapped, nan_null_direction_hint) < 0))
index = i;
}
else
{
if ((mapped->compareAt(i, index, *mapped, nan_null_direction_hint) > 0))
index = i;
}
}
res_column->insertFrom(*mapped, index);
start_of_array = end_of_array;
}
chassert(res_column->size() == offsets.size());
res_ptr = std::move(res_column);
}
template <typename Element>
static NO_SANITIZE_UNDEFINED bool executeType(const ColumnPtr & mapped, const ColumnArray::Offsets & offsets, ColumnPtr & res_ptr)
{
/// Min and Max are implemented in a different function
static_assert(aggregate_operation != AggregateOperation::min && aggregate_operation != AggregateOperation::max);
using ResultType = ArrayAggregateResult<Element, aggregate_operation>;
using ColVecType = ColumnVectorOrDecimal<Element>;
using ColVecResultType = ColumnVectorOrDecimal<ResultType>;
/// For average and product of array we return Float64 as result, but we want to keep precision
/// so we convert to Float64 as last step, but intermediate value is represented as result of sum operation
static constexpr bool is_average_or_product_operation = aggregate_operation == AggregateOperation::average ||
aggregate_operation == AggregateOperation::product;
using SummAggregationType = ArrayAggregateResult<Element, AggregateOperation::sum>;
using AggregationType = std::conditional_t<is_average_or_product_operation, SummAggregationType, ResultType>;
const ColVecType * column = checkAndGetColumn<ColVecType>(&*mapped);
/// Constant case.
if (!column)
{
const ColumnConst * column_const = checkAndGetColumnConst<ColVecType>(&*mapped);
if (!column_const)
return false;
const AggregationType x = static_cast<AggregationType>(column_const->template getValue<Element>()); // NOLINT
const ColVecType * column_typed = checkAndGetColumn<ColVecType>(&column_const->getDataColumn());
typename ColVecResultType::MutablePtr res_column;
if constexpr (is_decimal<Element>)
res_column = ColVecResultType::create(offsets.size(), column_typed->getScale());
else
res_column = ColVecResultType::create(offsets.size());
auto & res = res_column->getData();
size_t pos = 0;
for (size_t i = 0; i < offsets.size(); ++i)
{
const size_t array_size = offsets[i] - pos;
if (array_size == 0)
{
res[i] = {};
continue;
}
if constexpr (aggregate_operation == AggregateOperation::sum)
{
/// Just multiply the value by array size.
res[i] = x * static_cast<ResultType>(array_size);
}
else if constexpr (aggregate_operation == AggregateOperation::average)
{
if constexpr (is_decimal<Element>)
{
res[i] = DecimalUtils::convertTo<ResultType>(x, column_typed->getScale());
}
else
{
res[i] = static_cast<ResultType>(x);
}
}
else if constexpr (aggregate_operation == AggregateOperation::product)
{
AggregationType product = x;
if constexpr (is_decimal<Element>)
{
using T = decltype(x.value);
T x_val = x.value;
for (size_t array_index = 1; array_index < array_size; ++array_index)
{
T product_val = product.value;
if (common::mulOverflow(x_val, product_val, product.value))
throw Exception(ErrorCodes::DECIMAL_OVERFLOW, "Decimal math overflow");
}
auto result_scale = column_typed->getScale() * array_size;
if (unlikely(result_scale > DecimalUtils::max_precision<AggregationType>))
throw Exception(ErrorCodes::ARGUMENT_OUT_OF_BOUND, "Scale {} is out of bounds (max scale: {})",
result_scale, DecimalUtils::max_precision<AggregationType>);
res[i] = DecimalUtils::convertTo<ResultType>(product, static_cast<UInt32>(result_scale));
}
else
{
for (size_t array_index = 1; array_index < array_size; ++array_index)
product = product * x;
res[i] = static_cast<ResultType>(product);
}
}
pos = offsets[i];
}
res_ptr = std::move(res_column);
return true;
}
const auto & data = column->getData();
typename ColVecResultType::MutablePtr res_column;
if constexpr (is_decimal<Element>)
res_column = ColVecResultType::create(offsets.size(), column->getScale());
else
res_column = ColVecResultType::create(offsets.size());
typename ColVecResultType::Container & res = res_column->getData();
size_t pos = 0;
for (size_t i = 0; i < offsets.size(); ++i)
{
AggregationType aggregate_value{};
/// Array is empty
if (offsets[i] == pos)
{
if constexpr (is_decimal<AggregationType>)
res[i] = aggregate_value.value;
else
res[i] = static_cast<ResultType>(aggregate_value);
continue;
}
size_t count = 1;
aggregate_value = static_cast<AggregationType>(data[pos]); // NOLINT
++pos;
for (; pos < offsets[i]; ++pos)
{
auto element = data[pos];
if constexpr (aggregate_operation == AggregateOperation::sum ||
aggregate_operation == AggregateOperation::average)
{
aggregate_value += static_cast<AggregationType>(element);
}
else if constexpr (aggregate_operation == AggregateOperation::product)
{
if constexpr (is_decimal<Element>)
{
using AggregateValueDecimalUnderlyingValue = decltype(aggregate_value.value);
AggregateValueDecimalUnderlyingValue current_aggregate_value = aggregate_value.value;
AggregateValueDecimalUnderlyingValue element_value = static_cast<AggregateValueDecimalUnderlyingValue>(element.value);
if (common::mulOverflow(current_aggregate_value, element_value, aggregate_value.value))
throw Exception(ErrorCodes::DECIMAL_OVERFLOW, "Decimal math overflow");
}
else
{
aggregate_value *= static_cast<AggregationType>(element);
}
}
++count;
}
if constexpr (aggregate_operation == AggregateOperation::average)
{
if constexpr (is_decimal<Element>)
{
aggregate_value = aggregate_value / AggregationType(count);
res[i] = DecimalUtils::convertTo<ResultType>(aggregate_value, column->getScale());
}
else
{
res[i] = static_cast<ResultType>(aggregate_value) / static_cast<ResultType>(count);
}
}
else if constexpr (aggregate_operation == AggregateOperation::product && is_decimal<Element>)
{
auto result_scale = column->getScale() * count;
if (unlikely(result_scale > DecimalUtils::max_precision<AggregationType>))
throw Exception(ErrorCodes::ARGUMENT_OUT_OF_BOUND, "Scale {} is out of bounds (max scale: {})",
result_scale, DecimalUtils::max_precision<AggregationType>);
res[i] = DecimalUtils::convertTo<ResultType>(aggregate_value, static_cast<UInt32>(result_scale));
}
else
{
res[i] = static_cast<ResultType>(aggregate_value);
}
}
res_ptr = std::move(res_column);
return true;
}
static ColumnPtr execute(const ColumnArray & array, ColumnPtr mapped)
{
const IColumn::Offsets & offsets = array.getOffsets();
ColumnPtr res;
if constexpr (aggregate_operation == AggregateOperation::min || aggregate_operation == AggregateOperation::max)
{
executeMinOrMax(mapped, offsets, res);
return res;
}
else
{
if (executeType<UInt8>(mapped, offsets, res) ||
executeType<UInt16>(mapped, offsets, res) ||
executeType<UInt32>(mapped, offsets, res) ||
executeType<UInt64>(mapped, offsets, res) ||
executeType<UInt128>(mapped, offsets, res) ||
executeType<UInt256>(mapped, offsets, res) ||
executeType<Int8>(mapped, offsets, res) ||
executeType<Int16>(mapped, offsets, res) ||
executeType<Int32>(mapped, offsets, res) ||
executeType<Int64>(mapped, offsets, res) ||
executeType<Int128>(mapped, offsets, res) ||
executeType<Int256>(mapped, offsets, res) ||
executeType<BFloat16>(mapped, offsets, res) ||
executeType<Float32>(mapped, offsets, res) ||
executeType<Float64>(mapped, offsets, res) ||
executeType<Decimal32>(mapped, offsets, res) ||
executeType<Decimal64>(mapped, offsets, res) ||
executeType<Decimal128>(mapped, offsets, res) ||
executeType<Decimal256>(mapped, offsets, res) ||
executeType<DateTime64>(mapped, offsets, res))
{
return res;
}
}
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Unexpected column for arraySum: {}", mapped->getName());
}
};
struct NameArrayMin { static constexpr auto name = "arrayMin"; };
using FunctionArrayMin = FunctionArrayMapped<ArrayAggregateImpl<AggregateOperation::min>, NameArrayMin>;
struct NameArrayMax { static constexpr auto name = "arrayMax"; };
using FunctionArrayMax = FunctionArrayMapped<ArrayAggregateImpl<AggregateOperation::max>, NameArrayMax>;
struct NameArraySum { static constexpr auto name = "arraySum"; };
using FunctionArraySum = FunctionArrayMapped<ArrayAggregateImpl<AggregateOperation::sum>, NameArraySum>;
struct NameArrayAverage { static constexpr auto name = "arrayAvg"; };
using FunctionArrayAverage = FunctionArrayMapped<ArrayAggregateImpl<AggregateOperation::average>, NameArrayAverage>;
struct NameArrayProduct { static constexpr auto name = "arrayProduct"; };
using FunctionArrayProduct = FunctionArrayMapped<ArrayAggregateImpl<AggregateOperation::product>, NameArrayProduct>;
REGISTER_FUNCTION(ArrayAggregation)
{
FunctionDocumentation::Description description_min = R"(
Returns the minimum element in the source array.
If a lambda function `func` is specified, returns the minimum element of the lambda results.
)";
FunctionDocumentation::Syntax syntax_min = "arrayMin([func(x[, y1, ..., yN])], source_arr[, cond1_arr, ... , condN_arr])";
FunctionDocumentation::Arguments arguments_min = {
{"func(x[, y1, ..., yN])", "Optional. A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`).", {"Lambda function"}},
{"source_arr", "The source array to process.", {"Array(T)"}},
{"cond1_arr, ...", "Optional. N condition arrays providing additional arguments to the lambda function.", {"Array(T)"}}
};
FunctionDocumentation::ReturnedValue returned_value_min = {"Returns the minimum element in the source array, or the minimum element of the lambda results if provided."};
FunctionDocumentation::Examples examples_min = {
{"Basic example", "SELECT arrayMin([5, 3, 2, 7]);", "2"},
{"Usage with lambda function", "SELECT arrayMin(x, y -> x/y, [4, 8, 12, 16], [1, 2, 1, 2]);", "4"},
};
FunctionDocumentation::IntroducedIn introduced_in_min = {21, 1};
FunctionDocumentation::Category category_min = FunctionDocumentation::Category::Array;
FunctionDocumentation documentation_min = {description_min, syntax_min, arguments_min, {}, returned_value_min, examples_min, introduced_in_min, category_min};
factory.registerFunction<FunctionArrayMin>(documentation_min);
FunctionDocumentation::Description description_max = R"(
Returns the maximum element in the source array.
If a lambda function `func` is specified, returns the maximum element of the lambda results.
)";
FunctionDocumentation::Syntax syntax_max = "arrayMax([func(x[, y1, ..., yN])], source_arr[, cond1_arr, ... , condN_arr])";
FunctionDocumentation::Arguments arguments_max = {
{"func(x[, y1, ..., yN])", "Optional. A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`).", {"Lambda function"}},
{"source_arr", "The source array to process.", {"Array(T)"}},
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function.", {"Array(T)"}}
};
FunctionDocumentation::ReturnedValue returned_value_max = {"Returns the maximum element in the source array, or the maximum element of the lambda results if provided."};
FunctionDocumentation::Examples examples_max = {
{"Basic example", "SELECT arrayMax([5, 3, 2, 7]);", "7"},
{"Usage with lambda function", "SELECT arrayMax(x, y -> x/y, [4, 8, 12, 16], [1, 2, 1, 2]);", "12"},
};
FunctionDocumentation::IntroducedIn introduced_in_max = {21, 1};
FunctionDocumentation::Category category_max = FunctionDocumentation::Category::Array;
FunctionDocumentation documentation_max = {description_max, syntax_max, arguments_max, {}, returned_value_max, examples_max, introduced_in_max, category_max};
factory.registerFunction<FunctionArrayMax>(documentation_max);
FunctionDocumentation::Description description_sum = R"(
Returns the sum of elements in the source array.
If a lambda function `func` is specified, returns the sum of elements of the lambda results.
)";
FunctionDocumentation::Syntax syntax_sum = "arraySum([func(x[, y1, ..., yN])], source_arr[, cond1_arr, ... , condN_arr])";
FunctionDocumentation::Arguments arguments_sum = {
{"func(x[, y1, ..., yN])", "Optional. A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`).", {"Lambda function"}},
{"source_arr", "The source array to process.", {"Array(T)"}},
{", cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function.", {"Array(T)"}}
};
FunctionDocumentation::ReturnedValue returned_value_sum = {"Returns the sum of elements in the source array, or the sum of elements of the lambda results if provided."};
FunctionDocumentation::Examples examples_sum = {
{"Basic example", "SELECT arraySum([1, 2, 3, 4]);", "10"},
{"Usage with lambda function", "SELECT arraySum(x, y -> x+y, [1, 1, 1, 1], [1, 1, 1, 1]);", "8"},
};
FunctionDocumentation::IntroducedIn introduced_in_sum = {21, 1};
FunctionDocumentation::Category category_sum = FunctionDocumentation::Category::Array;
FunctionDocumentation documentation_sum = {description_sum, syntax_sum, arguments_sum, {}, returned_value_sum, examples_sum, introduced_in_sum, category_sum};
factory.registerFunction<FunctionArraySum>(documentation_sum);
FunctionDocumentation::Description description_avg = R"(
Returns the average of elements in the source array.
If a lambda function `func` is specified, returns the average of elements of the lambda results.
)";
FunctionDocumentation::Syntax syntax_avg = "arrayAvg([func(x[, y1, ..., yN])], source_arr[, cond1_arr, ... , condN_arr])";
FunctionDocumentation::Arguments arguments_avg = {
{"func(x[, y1, ..., yN])", "Optional. A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`).", {"Lambda function"}},
{"source_arr", "The source array to process.", {"Array(T)"}},
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function.", {"Array(T)"}}
};
FunctionDocumentation::ReturnedValue returned_value_avg = {"Returns the average of elements in the source array, or the average of elements of the lambda results if provided.", {"Float64"}};
FunctionDocumentation::Examples examples_avg = {
{"Basic example", "SELECT arrayAvg([1, 2, 3, 4]);", "2.5"},
{"Usage with lambda function", "SELECT arrayAvg(x, y -> x*y, [2, 3], [2, 3]) AS res;", "6.5"},
};
FunctionDocumentation::IntroducedIn introduced_in_avg = {21, 1};
FunctionDocumentation::Category category_avg = FunctionDocumentation::Category::Array;
FunctionDocumentation documentation_avg = {description_avg, syntax_avg, arguments_avg, {}, returned_value_avg, examples_avg, introduced_in_avg, category_avg};
factory.registerFunction<FunctionArrayAverage>(documentation_avg);
FunctionDocumentation::Description description_prod = R"(
Returns the product of elements in the source array.
If a lambda function `func` is specified, returns the product of elements of the lambda results.
)";
FunctionDocumentation::Syntax syntax_prod = "arrayProduct([func(x[, y1, ..., yN])], source_arr[, cond1_arr, ... , condN_arr])";
FunctionDocumentation::Arguments arguments_prod = {
{"func(x[, y1, ..., yN])", "Optional. A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`).", {"Lambda function"}},
{"source_arr", "The source array to process.", {"Array(T)"}},
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function.", {"Array(T)"}}
};
FunctionDocumentation::ReturnedValue returned_value_prod = {"Returns the product of elements in the source array, or the product of elements of the lambda results if provided.", {"Float64"}};
FunctionDocumentation::Examples examples_prod = {
{"Basic example", "SELECT arrayProduct([1, 2, 3, 4]);", "24"},
{"Usage with lambda function", "SELECT arrayProduct(x, y -> x+y, [2, 2], [2, 2]) AS res;", "16"},
};
FunctionDocumentation::IntroducedIn introduced_in_prod = {21, 1};
FunctionDocumentation::Category category_prod = FunctionDocumentation::Category::Array;
FunctionDocumentation documentation_prod = {description_prod, syntax_prod, arguments_prod, {}, returned_value_prod, examples_prod, introduced_in_prod, category_prod};
factory.registerFunction<FunctionArrayProduct>(documentation_prod);
}
}