Skip to content

Commit b28b58a

Browse files
authored
Merge pull request ClickHouse#80345 from Blargian/array_functions_part_7
Docs: array functions source code documentation. Part 7
2 parents 56b84ad + aeeb45c commit b28b58a

File tree

12 files changed

+318
-55
lines changed

12 files changed

+318
-55
lines changed

docs/en/sql-reference/functions/array-functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2624,7 +2624,7 @@ Note that the `arrayLastIndex` is a [higher-order function](/sql-reference/funct
26242624

26252625
Returns the minimum of elements in the source array.
26262626

2627-
If the `func` function is specified, returns the mininum of elements converted by this function.
2627+
If the `func` function is specified, returns the minimum of elements converted by this function.
26282628

26292629
Note that the `arrayMin` is a [higher-order function](/sql-reference/functions/overview#higher-order-functions). You can pass a lambda function to it as the first argument.
26302630

src/Functions/array/arrayAggregation.cpp

Lines changed: 109 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,115 @@ using FunctionArrayProduct = FunctionArrayMapped<ArrayAggregateImpl<AggregateOpe
446446

447447
REGISTER_FUNCTION(ArrayAggregation)
448448
{
449-
factory.registerFunction<FunctionArrayMin>();
450-
factory.registerFunction<FunctionArrayMax>();
451-
factory.registerFunction<FunctionArraySum>();
452-
factory.registerFunction<FunctionArrayAverage>();
453-
factory.registerFunction<FunctionArrayProduct>();
449+
FunctionDocumentation::Description description_min = R"(
450+
Returns the minimum element in the source array.
451+
452+
If a lambda function `func` is specified, returns the minimum element of the lambda results.
453+
)";
454+
FunctionDocumentation::Syntax syntax_min = "arrayMin([func(x[, y1, ..., yN])], source_arr[, cond1_arr, ... , condN_arr])";
455+
FunctionDocumentation::Arguments arguments_min = {
456+
{"func(x[, y1, ..., yN])", "Optional. A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [Lambda function](/sql-reference/functions/overview#arrow-operator-and-lambda)."},
457+
{"source_arr", "The source array to process [`Array(T)`](/sql-reference/data-types/array)."},
458+
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/sql-reference/data-types/array)."},
459+
};
460+
FunctionDocumentation::ReturnedValue returned_value_min = "Returns the minimum element in the source array, or the minimum element of the lambda results if provided.";
461+
FunctionDocumentation::Examples examples_min = {
462+
{"Basic example", "SELECT arrayMin([5, 3, 2, 7]);", "2"},
463+
{"Usage with lambda function", "SELECT arrayMin(x, y -> x/y, [4, 8, 12, 16], [1, 2, 1, 2]);", "4"},
464+
};
465+
FunctionDocumentation::IntroducedIn introduced_in_min = {21, 1};
466+
FunctionDocumentation::Category category_min = FunctionDocumentation::Category::Array;
467+
FunctionDocumentation documentation_min = {description_min, syntax_min, arguments_min, returned_value_min, examples_min, introduced_in_min, category_min};
468+
469+
factory.registerFunction<FunctionArrayMin>(documentation_min);
470+
471+
FunctionDocumentation::Description description_max = R"(
472+
Returns the maximum element in the source array.
473+
474+
If a lambda function `func` is specified, returns the maximum element of the lambda results.
475+
)";
476+
FunctionDocumentation::Syntax syntax_max = "arrayMax([func(x[, y1, ..., yN])], source_arr[, cond1_arr, ... , condN_arr])";
477+
FunctionDocumentation::Arguments arguments_max = {
478+
{"func(x[, y1, ..., yN])", "Optional. A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [Lambda function](/sql-reference/functions/overview#arrow-operator-and-lambda)."},
479+
{"source_arr", "The source array to process. [`Array(T)`](/sql-reference/data-types/array)."},
480+
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/sql-reference/data-types/array)."},
481+
};
482+
FunctionDocumentation::ReturnedValue returned_value_max = "Returns the maximum element in the source array, or the minimum element of the lambda results if provided.";
483+
FunctionDocumentation::Examples examples_max = {
484+
{"Basic example", "SELECT arrayMax([5, 3, 2, 7]);", "7"},
485+
{"Usage with lambda function", "SELECT arrayMax(x, y -> x/y, [4, 8, 12, 16], [1, 2, 1, 2]);", "12"},
486+
};
487+
FunctionDocumentation::IntroducedIn introduced_in_max = {21, 1};
488+
FunctionDocumentation::Category category_max = FunctionDocumentation::Category::Array;
489+
FunctionDocumentation documentation_max = {description_max, syntax_max, arguments_max, returned_value_max, examples_max, introduced_in_max, category_max};
490+
491+
factory.registerFunction<FunctionArrayMax>(documentation_max);
492+
493+
FunctionDocumentation::Description description_sum = R"(
494+
Returns the sum of elements in the source array.
495+
496+
If a lambda function `func` is specified, returns the sum of elements of the lambda results.
497+
)";
498+
FunctionDocumentation::Syntax syntax_sum = "arrayMax([func(x[, y1, ..., yN])], source_arr[, cond1_arr, ... , condN_arr])";
499+
FunctionDocumentation::Arguments arguments_sum = {
500+
{"func(x[, y1, ..., yN])", "Optional. A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [Lambda function](/sql-reference/functions/overview#arrow-operator-and-lambda)."},
501+
{"source_arr", "The source array to process. [`Array(T)`](/sql-reference/data-types/array)."},
502+
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/sql-reference/data-types/array)."},
503+
};
504+
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.";
505+
FunctionDocumentation::Examples examples_sum = {
506+
{"Basic example", "SELECT arraySum([1, 2, 3, 4]);", "10"},
507+
{"Usage with lambda function", "SELECT arraySum(x, y -> x+y, [1, 1, 1, 1], [1, 1, 1, 1]);", "8"},
508+
};
509+
FunctionDocumentation::IntroducedIn introduced_in_sum = {21, 1};
510+
FunctionDocumentation::Category category_sum = FunctionDocumentation::Category::Array;
511+
FunctionDocumentation documentation_sum = {description_sum, syntax_sum, arguments_sum, returned_value_sum, examples_sum, introduced_in_sum, category_sum};
512+
513+
factory.registerFunction<FunctionArraySum>(documentation_sum);
514+
515+
FunctionDocumentation::Description description_avg = R"(
516+
Returns the average of elements in the source array.
517+
518+
If a lambda function `func` is specified, returns the average of elements of the lambda results.
519+
)";
520+
FunctionDocumentation::Syntax syntax_avg = "arrayAvg([func(x[, y1, ..., yN])], source_arr[, cond1_arr, ... , condN_arr])";
521+
FunctionDocumentation::Arguments arguments_avg = {
522+
{"func(x[, y1, ..., yN])", "Optional. A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [Lambda function](/sql-reference/functions/overview#arrow-operator-and-lambda)."},
523+
{"source_arr", "The source array to process. [`Array(T)`](/sql-reference/data-types/array)."},
524+
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/sql-reference/data-types/array)."},
525+
};
526+
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`](/sql-reference/data-types/float).";
527+
FunctionDocumentation::Examples examples_avg = {
528+
{"Basic example", "SELECT arrayAvg([1, 2, 3, 4]);", "2.5"},
529+
{"Usage with lambda function", "SELECT arrayAvg(x, y -> x*y, [2, 3], [2, 3]) AS res;", "6.5"},
530+
};
531+
FunctionDocumentation::IntroducedIn introduced_in_avg = {21, 1};
532+
FunctionDocumentation::Category category_avg = FunctionDocumentation::Category::Array;
533+
FunctionDocumentation documentation_avg = {description_avg, syntax_avg, arguments_avg, returned_value_avg, examples_avg, introduced_in_avg, category_avg};
534+
535+
factory.registerFunction<FunctionArrayAverage>(documentation_avg);
536+
537+
FunctionDocumentation::Description description_prod = R"(
538+
Returns the product of elements in the source array.
539+
540+
If a lambda function `func` is specified, returns the product of elements of the lambda results.
541+
)";
542+
FunctionDocumentation::Syntax syntax_prod = "arrayProduct([func(x[, y1, ..., yN])], source_arr[, cond1_arr, ... , condN_arr])";
543+
FunctionDocumentation::Arguments arguments_prod = {
544+
{"func(x[, y1, ..., yN])", "Optional. A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [Lambda function](/sql-reference/functions/overview#arrow-operator-and-lambda)."},
545+
{"source_arr", "The source array to process [`Array(T)`](/sql-reference/data-types/array)."},
546+
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/sql-reference/data-types/array)."},
547+
};
548+
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`](/sql-reference/data-types/float).";
549+
FunctionDocumentation::Examples examples_prod = {
550+
{"Basic example", "SELECT arrayProduct([1, 2, 3, 4]);", "24"},
551+
{"Usage with lambda function", "SELECT arrayProduct(x, y -> x+y, [2, 2], [2, 2]) AS res;", "16"},
552+
};
553+
FunctionDocumentation::IntroducedIn introduced_in_prod = {21, 1};
554+
FunctionDocumentation::Category category_prod = FunctionDocumentation::Category::Array;
555+
FunctionDocumentation documentation_prod = {description_prod, syntax_prod, arguments_prod, returned_value_prod, examples_prod, introduced_in_prod, category_prod};
556+
557+
factory.registerFunction<FunctionArrayProduct>(documentation_prod);
454558
}
455559

456560
}

src/Functions/array/arrayAll.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,25 @@ ColumnPtr ArrayAllImpl::execute(const ColumnArray & array, ColumnPtr mapped)
6363

6464
REGISTER_FUNCTION(ArrayAll)
6565
{
66-
factory.registerFunction<FunctionArrayAll>();
66+
FunctionDocumentation::Description description = R"(
67+
Returns `1` if lambda `func(x [, y1, y2, ... yN])` returns true for all elements. Otherwise, it returns `0`.
68+
)";
69+
FunctionDocumentation::Syntax syntax = "arrayAll(func(x[, y1, ..., yN]), source_arr[, cond1_arr, ... , condN_arr])";
70+
FunctionDocumentation::Arguments arguments = {
71+
{"func(x[, y1, ..., yN])", "A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [Lambda function](/sql-reference/functions/overview#arrow-operator-and-lambda)."},
72+
{"source_arr", "The source array to process. [`Array(T)`](/sql-reference/data-types/array)."},
73+
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/sql-reference/data-types/array)."}
74+
};
75+
FunctionDocumentation::ReturnedValue returned_value = "Returns `1` if the lambda function returns true for all elements, `0` otherwise. [`UInt8`](/sql-reference/data-types/int-uint).";
76+
FunctionDocumentation::Examples examples = {
77+
{"All elements match", "SELECT arrayAll(x, y -> x=y, [1, 2, 3], [1, 2, 3])", "1"},
78+
{"Not all elements match", "SELECT arrayAll(x, y -> x=y, [1, 2, 3], [1, 1, 1])", "0"}
79+
};
80+
FunctionDocumentation::IntroducedIn introduced_in = {1, 1};
81+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
82+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
83+
84+
factory.registerFunction<FunctionArrayAll>(documentation);
6785
}
6886

6987
}

src/Functions/array/arrayCount.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ using FunctionArrayCount = FunctionArrayMapped<ArrayCountImpl, NameArrayCount>;
8282
REGISTER_FUNCTION(ArrayCount)
8383
{
8484
FunctionDocumentation::Description description = R"(
85-
Returns the number of elements for which `func(arr1[i], ..., arrN[i])` returns something other than `0`.
85+
Returns the number of elements for which `func(arr1[i], ..., arrN[i])` returns true.
8686
If `func` is not specified, it returns the number of non-zero elements in the array.
8787
8888
`arrayCount` is a [higher-order function](/sql-reference/functions/overview#higher-order-functions).
@@ -92,7 +92,7 @@ If `func` is not specified, it returns the number of non-zero elements in the ar
9292
{"func", "Function to apply to each element of the array(s). Optional. [Lambda function](/sql-reference/functions/overview#arrow-operator-and-lambda)"},
9393
{"arr1, ..., arrN", "N arrays. [Array(T)](/sql-reference/data-types/array)."},
9494
};
95-
FunctionDocumentation::ReturnedValue returned_value = "Returns the number of elements for which `func` returns something other than `0`. Otherwise, returns the number of non-zero elements in the array.";
95+
FunctionDocumentation::ReturnedValue returned_value = "Returns the number of elements for which `func` returns true. Otherwise, returns the number of non-zero elements in the array.";
9696
FunctionDocumentation::Examples example = {{"Usage example", "SELECT arrayCount(x -> (x % 2), groupArray(number) FROM numbers(10)", "5"}};
9797
FunctionDocumentation::IntroducedIn introduced_in = {1, 1};
9898
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;

src/Functions/array/arrayExists.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,22 @@ ColumnPtr ArrayExistsImpl::execute(const ColumnArray & array, ColumnPtr mapped)
6464

6565
REGISTER_FUNCTION(ArrayExists)
6666
{
67-
factory.registerFunction<FunctionArrayExists>();
67+
FunctionDocumentation::Description description = R"(
68+
Returns `1` if there is at least one element in a source array for which `func(x[, y1, y2, ... yN])` returns true. Otherwise, it returns `0`.
69+
)";
70+
FunctionDocumentation::Syntax syntax = "arrayExists(func(x[, y1, ..., yN]), source_arr[, cond1_arr, ... , condN_arr])";
71+
FunctionDocumentation::Arguments arguments = {
72+
{"func(x[, y1, ..., yN])", "A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [Lambda function](/sql-reference/functions/overview#arrow-operator-and-lambda)."},
73+
{"source_arr", "The source array to process. [`Array(T)`](/sql-reference/data-types/array)."},
74+
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/sql-reference/data-types/array)."}
75+
};
76+
FunctionDocumentation::ReturnedValue returned_value = "Returns `1` if the lambda function returns true for at least one element, `0` otherwise. [`UInt8`](/sql-reference/data-types/int-uint).";
77+
FunctionDocumentation::Examples examples = {{"Usage example", "SELECT arrayExists(x, y -> x=y, [1, 2, 3], [0, 0, 0])", "0"}};
78+
FunctionDocumentation::IntroducedIn introduced_in = {1, 1};
79+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
80+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
81+
82+
factory.registerFunction<FunctionArrayExists>(documentation);
6883
}
6984

7085
}

src/Functions/array/arrayFill.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ position i, the function replaces that element with the element at position i-1
136136
from the current state of the array. The first element is always preserved
137137
regardless of any condition.
138138
)";
139-
FunctionDocumentation::Syntax syntax = "arrayFill(func(x [, y1, ..., yN]), source [, cond1, ... , condN])";
139+
FunctionDocumentation::Syntax syntax = "arrayFill(func(x [, y1, ..., yN]), source_arr[, cond1_arr, ... , condN_arr])";
140140
FunctionDocumentation::Arguments arguments = {
141141
{"func(x [, y1, ..., yN])", "A lambda function `func(x [, y1, y2, ... yN]) → F(x [, y1, y2, ... yN])` which operates on elements of the source array (`x`) and condition arrays (`y`). [Lambda function](/sql-reference/functions/overview#arrow-operator-and-lambda)."},
142-
{"source", "The source array to process [`Array(T)`](/sql-reference/data-types/array)."},
143-
{"[, cond1, ... , condN]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/sql-reference/data-types/array)."},
142+
{"source_arr", "The source array to process [`Array(T)`](/sql-reference/data-types/array)."},
143+
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/sql-reference/data-types/array)."},
144144
};
145145
FunctionDocumentation::ReturnedValue returned_value = "Returns an array. [`Array(T)`](/sql-reference/data-types/array).";
146146
FunctionDocumentation::Examples examples = {
@@ -161,11 +161,11 @@ position i, the function replaces that element with the element at position i+1
161161
from the current state of the array. The last element is always preserved
162162
regardless of any condition.
163163
)";
164-
FunctionDocumentation::Syntax syntax_reverse = "arrayReverseFill(func(x[, y1, ..., yN]), source[, cond1, ... , condN])";
164+
FunctionDocumentation::Syntax syntax_reverse = "arrayReverseFill(func(x[, y1, ..., yN]), source_arr[, cond1_arr, ... , condN_arr])";
165165
FunctionDocumentation::Arguments arguments_reverse = {
166166
{"func(x[, y1, ..., yN])", "A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [Lambda function](/sql-reference/functions/overview#arrow-operator-and-lambda)."},
167-
{"source", "The source array to process [`Array(T)`](/sql-reference/data-types/array)."},
168-
{"[, cond1, ... , condN]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/sql-reference/data-types/array)."},
167+
{"source_arr", "The source array to process [`Array(T)`](/sql-reference/data-types/array)."},
168+
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/sql-reference/data-types/array)."},
169169
};
170170
FunctionDocumentation::ReturnedValue returned_value_reverse = "Returns an array with elements of the source array replaced by the results of the lambda. [`Array(T)`](/sql-reference/data-types/array).";
171171
FunctionDocumentation::Examples examples_reverse = {

src/Functions/array/arrayFilter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ ColumnPtr ArrayFilterImpl::execute(const ColumnArray & array, ColumnPtr mapped)
4949

5050
REGISTER_FUNCTION(ArrayFilter)
5151
{
52-
FunctionDocumentation::Description description = "Returns an array containing only the elements in the source array for which a lambda function returns something other than `0`.";
53-
FunctionDocumentation::Syntax syntax = "arrayFilter(func(x[, y1, ..., yN]), source[, cond1, ... , condN])]";
52+
FunctionDocumentation::Description description = "Returns an array containing only the elements in the source array for which a lambda function returns true.";
53+
FunctionDocumentation::Syntax syntax = "arrayFilter(func(x[, y1, ..., yN]), source_arr[, cond1_arr, ... , condN_arr])]";
5454
FunctionDocumentation::Arguments arguments = {
5555
{"func(x[, y1, ..., yN])", "A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [Lambda function](/sql-reference/functions/overview#arrow-operator-and-lambda)."},
56-
{"source", "The source array to process [`Array(T)`](/sql-reference/data-types/array)."},
57-
{"[, cond1, ... , condN]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/sql-reference/data-types/array)."},
56+
{"source_arr", "The source array to process [`Array(T)`](/sql-reference/data-types/array)."},
57+
{"[, cond1_arr, ... , condN_arr]", "Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/sql-reference/data-types/array)."},
5858
};
5959
FunctionDocumentation::ReturnedValue returned_value = "Returns a subset of the source array. [`Array(T)`](/sql-reference/data-types/array).";
6060
FunctionDocumentation::Examples examples = {

0 commit comments

Comments
 (0)