Skip to content

Commit b8eec69

Browse files
authored
Merge pull request ClickHouse#80194 from Blargian/array_functions_part_5
Docs: array functions source code documentation - part 5
2 parents 8748a27 + 6f18762 commit b8eec69

10 files changed

+200
-40
lines changed

src/Functions/array/arrayCompact.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,18 @@ using FunctionArrayCompact = FunctionArrayMapped<ArrayCompactImpl, NameArrayComp
161161

162162
REGISTER_FUNCTION(ArrayCompact)
163163
{
164-
factory.registerFunction<FunctionArrayCompact>();
164+
FunctionDocumentation::Description description = "Removes consecutive duplicate elements from an array, including `null` values. The order of values in the resulting array is determined by the order in the source array.";
165+
FunctionDocumentation::Syntax syntax = "arrayCompact(arr)";
166+
FunctionDocumentation::Arguments arguments = {
167+
{"arr", "An array to remove duplicates from. [`Array(T)`](/sql-reference/data-types/array)"}
168+
};
169+
FunctionDocumentation::ReturnedValue returned_value = "Returns an array without duplicate values. [`Array(T)`](/sql-reference/data-types/array).";
170+
FunctionDocumentation::Examples examples = {{"Usage example", "SELECT arrayCompact([1, 1, nan, nan, 2, 3, 3, 3]);", "[1,nan,2,3]"}};
171+
FunctionDocumentation::IntroducedIn introduced_in = {20, 1};
172+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
173+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
174+
175+
factory.registerFunction<FunctionArrayCompact>(documentation);
165176
}
166177

167178
}

src/Functions/array/arrayFlatten.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,27 @@ result: Row 1: [1, 2, 3], Row2: [4]
122122

123123
REGISTER_FUNCTION(ArrayFlatten)
124124
{
125-
factory.registerFunction<ArrayFlatten>();
125+
FunctionDocumentation::Description description = R"(
126+
Converts an array of arrays to a flat array.
127+
128+
Function:
129+
130+
- Applies to any depth of nested arrays.
131+
- Does not change arrays that are already flat.
132+
133+
The flattened array contains all the elements from all source arrays.
134+
)";
135+
FunctionDocumentation::Syntax syntax = "arrayFlatten(arr)";
136+
FunctionDocumentation::Arguments arguments = {
137+
{"arr", "A multidimensional array. [`Array(T)`](/sql-reference/data-types/array)(`Array`)"}
138+
};
139+
FunctionDocumentation::ReturnedValue returned_value = "Returns a flattened array from the multidimensional array. [`Array(T)`](/sql-reference/data-types/array).";
140+
FunctionDocumentation::Examples examples = {{"Usage example", "SELECT arrayFlatten([[[1]], [[2], [3]]]);", "[1,2,3]"}};
141+
FunctionDocumentation::IntroducedIn introduced_in = {20, 1};
142+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
143+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
144+
145+
factory.registerFunction<ArrayFlatten>(documentation);
126146
factory.registerAlias("flatten", "arrayFlatten", FunctionFactory::Case::Insensitive);
127147
}
128148

src/Functions/array/arrayFold.cpp

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,56 @@ class FunctionArrayFold : public IFunction
289289

290290
REGISTER_FUNCTION(ArrayFold)
291291
{
292-
factory.registerFunction<FunctionArrayFold>(FunctionDocumentation{.description=R"(
293-
Function arrayFold(acc,a1,...,aN->expr, arr1, ..., arrN, acc_initial) applies a lambda function to each element
294-
in each (equally-sized) array and collects the result in an accumulator.
295-
)", .examples{{"sum", "SELECT arrayFold(acc,x->acc+x, [1,2,3,4], toInt64(1));", "11"}}, .category = FunctionDocumentation::Category::Array});
292+
FunctionDocumentation::Description description = "Applies a lambda function to one or more equally-sized arrays and collects the result in an accumulator.";
293+
FunctionDocumentation::Syntax syntax = "arrayFold(λ(acc, x1 [, x2, x3, ... xN]), arr1 [, arr2, arr3, ... arrN], acc)";
294+
FunctionDocumentation::Arguments arguments = {
295+
{"λ(x, x1 [, x2, x3, ... xN])", "A lambda function `λ(acc, x1 [, x2, x3, ... xN]) → F(acc, x1 [, x2, x3, ... xN])` where `F` is an operation applied to `acc` and array values from `x` with the result of `acc` re-used. [Lambda function](/sql-reference/functions/overview#arrow-operator-and-lambda)."},
296+
{"arr1 [, arr2, arr3, ... arrN]", "N arrays over which to operate. [`Array(T)`](/sql-reference/data-types/array)"},
297+
{"acc", "Accumulator value with the same type as the return type of the Lambda function."}
298+
};
299+
FunctionDocumentation::ReturnedValue returned_value = "Returns the final `acc` value.";
300+
FunctionDocumentation::Examples examples = {
301+
{
302+
"Usage example",
303+
"SELECT arrayFold(acc,x -> acc + x*2, [1, 2, 3, 4], 3::Int64) AS res;",
304+
"23"
305+
},
306+
{
307+
"Fibonacci sequence",
308+
R"(
309+
SELECT arrayFold(acc, x -> (acc.2, acc.2 + acc.1),range(number),(1::Int64, 0::Int64)).1 AS fibonacci FROM numbers(1,10);)",
310+
R"(
311+
┌─fibonacci─┐
312+
│ 0 │
313+
│ 1 │
314+
│ 1 │
315+
│ 2 │
316+
│ 3 │
317+
│ 5 │
318+
│ 8 │
319+
│ 13 │
320+
│ 21 │
321+
│ 34 │
322+
└───────────┘
323+
)"
324+
},
325+
{
326+
"Example using multiple arrays",
327+
R"(
328+
SELECT arrayFold(
329+
(acc, x, y) -> acc + (x * y),
330+
[1, 2, 3, 4],
331+
[10, 20, 30, 40],
332+
0::Int64
333+
) AS res;
334+
)",
335+
"300"
336+
}
337+
};
338+
FunctionDocumentation::IntroducedIn introduced_in = {23, 10};
339+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
340+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
341+
342+
factory.registerFunction<FunctionArrayFold>(documentation);
296343
}
297344
}

src/Functions/array/arrayJaccardIndex.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,19 @@ class FunctionArrayJaccardIndex : public IFunction
151151

152152
REGISTER_FUNCTION(ArrayJaccardIndex)
153153
{
154-
factory.registerFunction<FunctionArrayJaccardIndex>();
154+
FunctionDocumentation::Description description = "Returns the [Jaccard index](https://en.wikipedia.org/wiki/Jaccard_index) of two arrays.";
155+
FunctionDocumentation::Syntax syntax = "arrayJaccardIndex(arr_x, arr_y)";
156+
FunctionDocumentation::Arguments arguments = {
157+
{"arr_x", "First array. [`Array(T)`](/sql-reference/data-types/array)."},
158+
{"arr_y", "Second array. [`Array(T)`](/sql-reference/data-types/array)."},
159+
};
160+
FunctionDocumentation::ReturnedValue returned_value = "Returns the Jaccard index of `arr_x` and `arr_y`.[Float64](/sql-reference/data-types/float)";
161+
FunctionDocumentation::Examples examples = {{"Usage example", "SELECT arrayJaccardIndex([1, 2], [2, 3]) AS res", "0.3333333333333333"}};
162+
FunctionDocumentation::IntroducedIn introduced_in = {23, 7};
163+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
164+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
165+
166+
factory.registerFunction<FunctionArrayJaccardIndex>(documentation);
155167
}
156168

157169
}

src/Functions/array/arrayReduce.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,42 @@ ColumnPtr FunctionArrayReduce::executeImpl(const ColumnsWithTypeAndName & argume
216216

217217
REGISTER_FUNCTION(ArrayReduce)
218218
{
219-
factory.registerFunction<FunctionArrayReduce>();
219+
FunctionDocumentation::Description description = R"(
220+
Applies an aggregate function to array elements and returns its result.
221+
The name of the aggregation function is passed as a string in single quotes `'max'`, `'sum'`.
222+
When using parametric aggregate functions, the parameter is indicated after the function name in parentheses `'uniqUpTo(6)'`.
223+
)";
224+
FunctionDocumentation::Syntax syntax = "arrayReduce(agg_f, arr1 [, arr2, ... , arrN)]";
225+
FunctionDocumentation::Arguments arguments = {
226+
{"agg_f", "The name of an aggregate function which should be a constant [String](/sql-reference/data-types/string)."},
227+
{"arr1 [, arr2, ... , arrN)]", "N arrays corresponding to the arguments of `agg_f`. [`Array(T)`](/sql-reference/data-types/array)."},
228+
};
229+
FunctionDocumentation::ReturnedValue returned_value = "Returns the result of the aggregate function";
230+
FunctionDocumentation::Examples examples = {{"Usage example", "SELECT arrayReduce('max', [1, 2, 3]);", R"(
231+
┌─arrayReduce('max', [1, 2, 3])─┐
232+
│ 3 │
233+
└───────────────────────────────┘
234+
)"},{"Example with aggregate function using multiple arguments", R"(If an aggregate function takes multiple arguments, then this function must be applied to multiple arrays of the same size.
235+
236+
```sql
237+
SELECT arrayReduce('maxIf', [3, 5], [1, 0]);
238+
```
239+
)", R"(
240+
┌─arrayReduce('maxIf', [3, 5], [1, 0])─┐
241+
│ 3 │
242+
└──────────────────────────────────────┘
243+
)"},{"Example with a parametric aggregate function", R"(
244+
SELECT arrayReduce('uniqUpTo(3)', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
245+
)", R"(
246+
┌─arrayReduce('uniqUpTo(3)', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])─┐
247+
│ 4 │
248+
└─────────────────────────────────────────────────────────────┘
249+
)"}};
250+
FunctionDocumentation::IntroducedIn introduced_in = {1, 1};
251+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
252+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
253+
254+
factory.registerFunction<FunctionArrayReduce>(documentation);
220255
}
221256

222257
}

src/Functions/array/arrayReduceInRanges.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,33 @@ ColumnPtr FunctionArrayReduceInRanges::executeImpl(
387387

388388
REGISTER_FUNCTION(ArrayReduceInRanges)
389389
{
390-
factory.registerFunction<FunctionArrayReduceInRanges>();
390+
FunctionDocumentation::Description description = R"(
391+
Applies an aggregate function to array elements in the given ranges and returns an array containing the result corresponding to each range.
392+
The function will return the same result as multiple `arrayReduce(agg_func, arraySlice(arr1, index, length), ...)`.
393+
)";
394+
FunctionDocumentation::Syntax syntax = "arrayReduceInRanges(agg_f, ranges, arr1 [, arr2, ... ,arrN)]";
395+
FunctionDocumentation::Arguments arguments = {
396+
{"agg_f", "The name of the aggregate function to use. [String](/sql-reference/data-types/string)"},
397+
{"ranges", "The range over which to aggregate. An array of tuples, `(i, r)` containing the index `i` from which to begin from and the range `r` over which to aggregate [`Array(T)`](/sql-reference/data-types/array)([`Tuple(T1, T2, ...)`](/sql-reference/data-types/tuple))"},
398+
{"arr1 [, arr2, ... ,arrN)]", "N arrays as arguments to the aggregate function. [`Array(T)`](/sql-reference/data-types/array)."},
399+
};
400+
FunctionDocumentation::ReturnedValue returned_value = "Returns an array containing results of the aggregate function over the specified ranges. [`Array(T)`](/sql-reference/data-types/array).";
401+
FunctionDocumentation::Examples examples = {{"Usage example", R"(
402+
SELECT arrayReduceInRanges(
403+
'sum',
404+
[(1, 5), (2, 3), (3, 4), (4, 4)],
405+
[1000000, 200000, 30000, 4000, 500, 60, 7]
406+
) AS res)", R"(
407+
┌─res─────────────────────────┐
408+
│ [1234500,234000,34560,4567] │
409+
└─────────────────────────────┘
410+
)"}
411+
};
412+
FunctionDocumentation::IntroducedIn introduced_in = {20, 4};
413+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
414+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
415+
416+
factory.registerFunction<FunctionArrayReduceInRanges>(documentation);
391417
}
392418

393419
}

src/Functions/array/arrayReverse.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,25 @@ bool FunctionArrayReverse::executeString(const IColumn & src_data, const ColumnA
246246

247247
REGISTER_FUNCTION(ArrayReverse)
248248
{
249-
factory.registerFunction<FunctionArrayReverse>();
249+
FunctionDocumentation::Description description = R"(
250+
Reverses the order of elements of a given array.
251+
252+
:::note
253+
Function `reverse(arr)` performs the same functionality but works on other data-types
254+
in addition to Arrays.
255+
:::
256+
)";
257+
FunctionDocumentation::Syntax syntax = "arrayReverse(arr)";
258+
FunctionDocumentation::Arguments arguments = {
259+
{"arr", "The array to reverse. [`Array(T)`](/sql-reference/data-types/array)."}
260+
};
261+
FunctionDocumentation::ReturnedValue returned_value = "Returns an array of the same size as the original array containing the elements in reverse order. [`Array(T)`](/sql-reference/data-types/array).";
262+
FunctionDocumentation::Examples examples = {{"Usage example", "SELECT arrayReverse([1, 2, 3])", "[3,2,1]"}};
263+
FunctionDocumentation::IntroducedIn introduced_in = {1, 1};
264+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
265+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
266+
267+
factory.registerFunction<FunctionArrayReverse>(documentation);
250268
}
251269

252270
}

src/Functions/array/arrayZip.cpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,27 @@ class FunctionArrayZip : public IFunction
171171

172172
REGISTER_FUNCTION(ArrayZip)
173173
{
174-
factory.registerFunction<FunctionArrayZip<false>>(
175-
{.description = R"(
176-
Combines multiple arrays into a single array. The resulting array contains the corresponding elements of the source arrays grouped into tuples in the listed order of arguments.
177-
)",
178-
.category = FunctionDocumentation::Category::Array});
179-
180-
factory.registerFunction<FunctionArrayZip<true>>(
181-
{.description = R"(
182-
Combines multiple arrays into a single array, allowing for unaligned arrays. The resulting array contains the corresponding elements of the source arrays grouped into tuples in the listed order of arguments.
183-
184-
If the arrays have different sizes, the shorter arrays will be padded with `null` values.
185-
)",
186-
.category = FunctionDocumentation::Category::Array}
187-
188-
);
174+
FunctionDocumentation::Description description = "Combines multiple arrays into a single array. The resulting array contains the corresponding elements of the source arrays grouped into tuples in the listed order of arguments.";
175+
FunctionDocumentation::Syntax syntax = "arrayZip(arr1, arr2, ... , arrN)";
176+
FunctionDocumentation::Arguments argument = {{"arr1, arr2, ... , arrN", "N arrays to combine into a single array. [`Array(T)`](/sql-reference/data-types/array)"}};
177+
FunctionDocumentation::ReturnedValue returned_value = "Returns an array with elements from the source arrays grouped in tuples. Data types in the tuple are the same as types of the input arrays and in the same order as arrays are passed. [`Array(T)`](/sql-reference/data-types/array)([`Tuple`](/sql-reference/data-types/tuple)).";
178+
FunctionDocumentation::Examples example = {{"Usage example", "SELECT arrayZip(['a', 'b', 'c'], [5, 2, 1]);", "[('a', 5), ('b', 2), ('c', 1)]"}};
179+
FunctionDocumentation::IntroducedIn introduced_in = {20, 1};
180+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
181+
FunctionDocumentation documentation = {description, syntax, argument, returned_value, example, introduced_in, category};
182+
183+
factory.registerFunction<FunctionArrayZip<false>>(documentation);
184+
185+
FunctionDocumentation::Description description_unaligned = "Combines multiple arrays into a single array, allowing for unaligned arrays (arrays of differing lengths). The resulting array contains the corresponding elements of the source arrays grouped into tuples in the listed order of arguments.";
186+
FunctionDocumentation::Syntax syntax_unaligned = "arrayZipUnaligned(arr1, arr2, ..., arrN)";
187+
FunctionDocumentation::Arguments argument_unaligned = {{"arr1, arr2, ..., arrN", "N arrays to combine into a single array. [`Array(T)`](/sql-reference/data-types/array)."}};
188+
FunctionDocumentation::ReturnedValue returned_value_unaligned = "Returns an array with elements from the source arrays grouped in tuples. Data types in the tuple are the same as types of the input arrays and in the same order as arrays are passed. [`Array(T)`](/sql-reference/data-types/array)([`Tuple(T1, T2, ...)`](/sql-reference/data-types/tuple)).";
189+
FunctionDocumentation::Examples example_unaligned = {{"Usage example", "SELECT arrayZipUnaligned(['a'], [1, 2, 3]);", "[('a', 1),(NULL, 2),(NULL, 3)]"}};
190+
FunctionDocumentation::IntroducedIn introduced_in_unaligned = {20, 1};
191+
FunctionDocumentation::Category category_unaligned = FunctionDocumentation::Category::Array;
192+
FunctionDocumentation documentation_unaligned = {description_unaligned, syntax_unaligned, argument_unaligned, returned_value_unaligned, example_unaligned, introduced_in_unaligned, category_unaligned};
193+
194+
factory.registerFunction<FunctionArrayZip<true>>(documentation_unaligned);
189195
}
190196

191197
}

tests/queries/0_stateless/02415_all_new_functions_must_be_documented.reference

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ array
9292
arrayAUCPR
9393
arrayAll
9494
arrayAvg
95-
arrayCompact
9695
arrayConcat
9796
arrayCount
9897
arrayCumSum
@@ -108,8 +107,6 @@ arrayFilter
108107
arrayFirst
109108
arrayFirstIndex
110109
arrayFirstOrNull
111-
arrayFlatten
112-
arrayJaccardIndex
113110
arrayLast
114111
arrayLastIndex
115112
arrayLastOrNull
@@ -119,9 +116,6 @@ arrayMin
119116
arrayProduct
120117
arrayROCAUC
121118
arrayRandomSample
122-
arrayReduce
123-
arrayReduceInRanges
124-
arrayReverse
125119
arrayReverseFill
126120
arrayReverseSplit
127121
arraySplit

tests/queries/0_stateless/02415_all_new_functions_must_have_version_information.reference

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ array
108108
arrayAUCPR
109109
arrayAll
110110
arrayAvg
111-
arrayCompact
112111
arrayConcat
113112
arrayCount
114113
arrayCumSum
@@ -126,9 +125,6 @@ arrayFilter
126125
arrayFirst
127126
arrayFirstIndex
128127
arrayFirstOrNull
129-
arrayFlatten
130-
arrayFold
131-
arrayJaccardIndex
132128
arrayLast
133129
arrayLastIndex
134130
arrayLastOrNull
@@ -140,9 +136,6 @@ arrayMin
140136
arrayProduct
141137
arrayROCAUC
142138
arrayRandomSample
143-
arrayReduce
144-
arrayReduceInRanges
145-
arrayReverse
146139
arrayReverseFill
147140
arrayReverseSplit
148141
arrayRotateLeft
@@ -154,8 +147,6 @@ arraySplit
154147
arrayStringConcat
155148
arraySum
156149
arrayWithConstant
157-
arrayZip
158-
arrayZipUnaligned
159150
ascii
160151
asin
161152
asinh

0 commit comments

Comments
 (0)