Skip to content

Commit 89a6c65

Browse files
authored
Merge pull request ClickHouse#80124 from Blargian/array_functions_part_2
Docs: Array functions source code documentation - part 2
2 parents 7a36526 + 4d642b3 commit 89a6c65

16 files changed

+360
-47
lines changed

src/Functions/array/arrayCount.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ using FunctionArrayCount = FunctionArrayMapped<ArrayCountImpl, NameArrayCount>;
8181

8282
REGISTER_FUNCTION(ArrayCount)
8383
{
84+
FunctionDocumentation::Description description = R"(
85+
Returns the number of elements for which `func(arr1[i], ..., arrN[i])` returns something other than `0`.
86+
If `func` is not specified, it returns the number of non-zero elements in the array.
87+
88+
`arrayCount` is a [higher-order function](/sql-reference/functions/overview#higher-order-functions).
89+
)";
90+
FunctionDocumentation::Syntax syntax = "arrayCount([func, ] arr1, ...)";
91+
FunctionDocumentation::Arguments arguments = {
92+
{"func", "Function to apply to each element of the array(s). Optional. [Lambda function](/sql-reference/functions/overview#arrow-operator-and-lambda)"},
93+
{"arr1, ..., arrN", "N arrays. [Array(T)](/sql-reference/data-types/array)."},
94+
};
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.";
96+
FunctionDocumentation::Examples example = {{"Usage example", "SELECT arrayCount(x -> (x % 2), groupArray(number) FROM numbers(10)", "5"}};
97+
FunctionDocumentation::IntroducedIn introduced_in = {1, 1};
98+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
99+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, example, introduced_in, category};
100+
84101
factory.registerFunction<FunctionArrayCount>();
85102
}
86103

src/Functions/array/arrayDotProduct.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,33 @@ using FunctionArrayDotProduct = FunctionArrayScalarProduct<DotProduct>;
385385

386386
REGISTER_FUNCTION(ArrayDotProduct)
387387
{
388+
FunctionDocumentation::Description description = R"(
389+
Returns the dot product of two arrays.
390+
391+
:::note
392+
The sizes of the two vectors must be equal. Arrays and Tuples may also contain mixed element types.
393+
:::
394+
)";
395+
FunctionDocumentation::Syntax syntax = "arrayDotProduct(v1, v2)";
396+
FunctionDocumentation::Arguments arguments = {
397+
{"v1", "First vector. [Array(T)](/sql-reference/data-types/array) or [Tuple(T1, T2, ...)](../data-types/tuple.md) of numeric values."},
398+
{"v2", "Second vector. [Array(T)](/sql-reference/data-types/array) or [Tuple(T1, T2, ...)](../data-types/tuple.md) of numeric values."},
399+
};
400+
FunctionDocumentation::ReturnedValue returned_value = R"(
401+
The dot product of the two vectors. [Numeric](/native-protocol/columns#numeric-types).
402+
403+
:::note
404+
The return type is determined by the type of the arguments. If Arrays or Tuples contain mixed element types then the result type is the supertype.
405+
:::
406+
)";
407+
FunctionDocumentation::Examples examples = {
408+
{"Array example", "SELECT arrayDotProduct([1, 2, 3], [4, 5, 6]) AS res, toTypeName(res);", "32 UInt16"},
409+
{"Tuple example", "SELECT dotProduct((1::UInt16, 2::UInt8, 3::Float32),(4::Int16, 5::Float32, 6::UInt8)) AS res, toTypeName(res);", "32 Float64"}
410+
};
411+
FunctionDocumentation::IntroducedIn introduced_in = {23, 5};
412+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
413+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
414+
388415
factory.registerFunction<FunctionArrayDotProduct>();
389416
}
390417

src/Functions/array/arrayEnumerate.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,52 @@ class FunctionArrayEnumerate : public IFunction
7676

7777
REGISTER_FUNCTION(ArrayEnumerate)
7878
{
79-
factory.registerFunction<FunctionArrayEnumerate>();
79+
FunctionDocumentation::Description description = R"(
80+
Returns the array `[1, 2, 3, ..., length (arr)]`
81+
82+
This function is normally used with the [`ARRAY JOIN`](/sql-reference/statements/select/array-join) clause. It allows counting something just
83+
once for each array after applying `ARRAY JOIN`.
84+
This function can also be used in higher-order functions. For example, you can use it to get array indexes for elements that match a condition.
85+
)";
86+
FunctionDocumentation::Syntax syntax = "arrayEnumerate(arr)";
87+
FunctionDocumentation::Arguments arguments = {
88+
{"arr", "The array to enumerate. [`Array`](/sql-reference/data-types/array)."}
89+
};
90+
FunctionDocumentation::ReturnedValue returned_value = "Returns the array `[1, 2, 3, ..., length (arr)]`. Array(UInt32)";
91+
FunctionDocumentation::Examples examples = {{"Basic example with ARRAY JOIN", R"(
92+
CREATE TABLE test
93+
(
94+
`id` UInt8,
95+
`tag` Array(String),
96+
`version` Array(String)
97+
)
98+
ENGINE = MergeTree
99+
ORDER BY id;
100+
101+
INSERT INTO test VALUES (1, ['release-stable', 'dev', 'security'], ['2.4.0', '2.6.0-alpha', '2.4.0-sec1']);
102+
103+
SELECT
104+
id,
105+
tag,
106+
version,
107+
seq
108+
FROM test
109+
ARRAY JOIN
110+
tag,
111+
version,
112+
arrayEnumerate(tag) AS seq
113+
)", R"(
114+
┌─id─┬─tag────────────┬─version─────┬─seq─┐
115+
│ 1 │ release-stable │ 2.4.0 │ 1 │
116+
│ 1 │ dev │ 2.6.0-alpha │ 2 │
117+
│ 1 │ security │ 2.4.0-sec1 │ 3 │
118+
└────┴────────────────┴─────────────┴─────┘
119+
)"}};
120+
FunctionDocumentation::IntroducedIn introduced_in = {1, 1};
121+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
122+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
123+
124+
factory.registerFunction<FunctionArrayEnumerate>(documentation);
80125
}
81126

82127
}

src/Functions/array/arrayEnumerateUniqRanked.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,57 @@ class FunctionArrayEnumerateUniqRanked : public FunctionArrayEnumerateRankedExte
1616

1717
REGISTER_FUNCTION(ArrayEnumerateUniqRanked)
1818
{
19-
factory.registerFunction<FunctionArrayEnumerateUniqRanked>();
19+
FunctionDocumentation::Description description = R"(
20+
Returns an array (or multi-dimensional array) with the same dimensions as the source array,
21+
indicating for each element what it's position is among elements with the same value.
22+
It allows for enumeration of a multi-dimensional array with the ability to specify how deep to look inside the array.
23+
)";
24+
FunctionDocumentation::Syntax syntax = "arrayEnumerateUniqRanked(clear_depth, arr, max_array_depth)";
25+
FunctionDocumentation::Arguments arguments = {
26+
{"clear_depth", "Enumerate elements at the specified level separately. Positive [Integer](../data-types/int-uint.md) less than or equal to `max_arr_depth`."},
27+
{"arr", "N-dimensional array to enumerate. [Array](/sql-reference/data-types/array)."},
28+
{"max_array_depth", "The maximum effective depth. Positive [Integer](../data-types/int-uint.md) less than or equal to the depth of `arr`."}
29+
};
30+
FunctionDocumentation::ReturnedValue returned_value = "Returns an N-dimensional array the same size as `arr` with each element showing the position of that element in relation to other elements of the same value.";
31+
FunctionDocumentation::Examples examples = {
32+
{"Example 1", R"(
33+
With `clear_depth=1` and `max_array_depth=1`, the result of `arrayEnumerateUniqRanked` is identical to that which [`arrayEnumerateUniq`](#arrayenumerateuniq) would give for the same array.
34+
35+
```sql
36+
SELECT arrayEnumerateUniqRanked(1, [1,2,1], 1);
37+
```
38+
)", "[1,1,2]"},
39+
{"Example 2", R"(
40+
41+
With `clear_depth=1` and `max_array_depth=1`, the result of `arrayEnumerateUniqRanked` is identical to that which [`arrayEnumerateUniq`](#arrayenumerateuniq) would give for the same array.
42+
43+
```sql
44+
SELECT arrayEnumerateUniqRanked(1, [[1,2,3],[2,2,1],[3]], 2);", "[[1,1,1],[2,3,2],[2]]
45+
```
46+
)", "[1,1,2]"},
47+
{"Example 3", R"(
48+
49+
In this example, `arrayEnumerateUniqRanked` is used to obtain an array indicating, for each element of the multidimensional array, what its position is among elements of the same value. For the first row of the passed array,`[1,2,3]`, the corresponding result is `[1,1,1]`, indicating that this is the first time `1`,`2` and `3` are encountered. For the second row of the provided array,`[2,2,1]`, the corresponding result is `[2,3,3]`, indicating that `2` is encountered for a second and third time, and `1` is encountered for the second time. Likewise, for the third row of the provided array `[3]` the corresponding result is `[2]` indicating that `3` is encountered for the second time.
50+
51+
```sql
52+
SELECT arrayEnumerateUniqRanked(1, [[1,2,3],[2,2,1],[3]], 2);
53+
```
54+
)", "[[1,1,1],[2,3,2],[2]]"
55+
},
56+
{"Example 4", R"(
57+
58+
Changing `clear_depth=2`, results in elements being enumerated separately for each row.
59+
60+
```sql
61+
SELECT arrayEnumerateUniqRanked(2, [[1,2,3],[2,2,1],[3]], 2);
62+
```
63+
)", "[[1,1,1],[1,2,1],[1]]"},
64+
};
65+
FunctionDocumentation::IntroducedIn introduced_in = {20, 1};
66+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
67+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
68+
69+
factory.registerFunction<FunctionArrayEnumerateUniqRanked>(documentation);
2070
}
2171

2272
}

src/Functions/array/arrayPopBack.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ class FunctionArrayPopBack : public FunctionArrayPop
1515

1616
REGISTER_FUNCTION(ArrayPopBack)
1717
{
18-
factory.registerFunction<FunctionArrayPopBack>();
18+
FunctionDocumentation::Description description = "Removes the last element from the array.";
19+
FunctionDocumentation::Syntax syntax = "arrayPopBack(arr)";
20+
FunctionDocumentation::Arguments arguments = {{"arr", "The array for which to remove the last element from. [`Array(T)`](/sql-reference/data-types/array)."}};
21+
FunctionDocumentation::ReturnedValue returned_value = "Returns an array identical to `arr` but without the last element of `arr`. [`Array(T)`](/sql-reference/data-types/array).";
22+
FunctionDocumentation::Examples examples = {{"Usage example", "SELECT arrayPopBack([1, 2, 3]) AS res;", "[1,2]"}};
23+
FunctionDocumentation::IntroducedIn introduced_in = {1, 1};
24+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
25+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
26+
27+
factory.registerFunction<FunctionArrayPopBack>(documentation);
1928
}
2029

2130
}

src/Functions/array/arrayPopFront.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ class FunctionArrayPopFront : public FunctionArrayPop
1515

1616
REGISTER_FUNCTION(ArrayPopFront)
1717
{
18-
factory.registerFunction<FunctionArrayPopFront>();
18+
FunctionDocumentation::Description description = "Removes the first item from the array.";
19+
FunctionDocumentation::Syntax syntax = "arrayPopFront(arr)";
20+
FunctionDocumentation::Arguments arguments = {{"arr", "The array for which to remove the first element from. [`Array(T)`](/sql-reference/data-types/array)."}};
21+
FunctionDocumentation::ReturnedValue returned_value = "Returns an array identical to `arr` but without the first element of `arr`. [`Array(T)`](/sql-reference/data-types/array).";
22+
FunctionDocumentation::Examples examples = {{"Usage example", "SELECT arrayPopFront([1, 2, 3]) AS res;", "[2,3]"}};
23+
FunctionDocumentation::IntroducedIn introduced_in = {1, 1};
24+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
25+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
26+
27+
factory.registerFunction<FunctionArrayPopFront>(documentation);
1928
}
2029

2130
}

src/Functions/array/arrayPushBack.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,29 @@ class FunctionArrayPushBack : public FunctionArrayPush
1515

1616
REGISTER_FUNCTION(ArrayPushBack)
1717
{
18-
factory.registerFunction<FunctionArrayPushBack>();
18+
FunctionDocumentation::Description description = "Adds one item to the end of the array.";
19+
FunctionDocumentation::Syntax syntax = "arrayPushBack(arr, x)";
20+
FunctionDocumentation::Arguments arguments = {
21+
{"arr", "The array for which to add value `x` to the end of. [`Array(T)`](/sql-reference/data-types/array)."},
22+
{"x", R"(
23+
- Single value to add to the end of the array. [`Array(T)`](/sql-reference/data-types/array).
24+
25+
:::note
26+
- Only numbers can be added to an array with numbers, and only strings can be added to an array of strings.
27+
- When adding numbers, ClickHouse automatically sets the type of `x` for the data type of the array.
28+
- Can be `NULL`. The function adds a `NULL` element to an array, and the type of array elements converts to `Nullable`.
29+
30+
For more information about the types of data in ClickHouse, see [Data types](/sql-reference/data-types).
31+
:::
32+
)"},
33+
};
34+
FunctionDocumentation::ReturnedValue returned_value = "Returns an array identical to `arr` but with an additional value `x` at the end of the array. [`Array(T)`](/sql-reference/data-types/array).";
35+
FunctionDocumentation::Examples examples = {{"Usage example", "SELECT arrayPushBack(['a'], 'b') AS res;", "['a','b']"}};
36+
FunctionDocumentation::IntroducedIn introduced_in = {1, 1};
37+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
38+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
39+
40+
factory.registerFunction<FunctionArrayPushBack>(documentation);
1941
}
2042

2143
}

src/Functions/array/arrayPushFront.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,29 @@ class FunctionArrayPushFront : public FunctionArrayPush
1717

1818
REGISTER_FUNCTION(ArrayPushFront)
1919
{
20-
factory.registerFunction<FunctionArrayPushFront>();
20+
FunctionDocumentation::Description description = "Adds one element to the beginning of the array.";
21+
FunctionDocumentation::Syntax syntax = "arrayPushFront(arr, x)";
22+
FunctionDocumentation::Arguments arguments = {
23+
{"arr", "The array for which to add value `x` to the end of. [`Array(T)`](/sql-reference/data-types/array)."},
24+
{"x", R"(
25+
- Single value to add to the start of the array. [`Array(T)`](/sql-reference/data-types/array).
26+
27+
:::note
28+
- Only numbers can be added to an array with numbers, and only strings can be added to an array of strings.
29+
- When adding numbers, ClickHouse automatically sets the type of `x` for the data type of the array.
30+
- Can be `NULL`. The function adds a `NULL` element to an array, and the type of array elements converts to `Nullable`.
31+
32+
For more information about the types of data in ClickHouse, see [Data types](/sql-reference/data-types).
33+
:::
34+
)"},
35+
};
36+
FunctionDocumentation::ReturnedValue returned_value = "Returns an array identical to `arr` but with an additional value `x` at the beginning of the array. [`Array(T)`](/sql-reference/data-types/array).";
37+
FunctionDocumentation::Examples examples = {{"Usage example", "SELECT arrayPushFront(['b'], 'a') AS res;", "['a','b']"}};
38+
FunctionDocumentation::IntroducedIn introduced_in = {1, 1};
39+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
40+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
41+
42+
factory.registerFunction<FunctionArrayPushFront>(documentation);
2143
}
2244

2345
}

src/Functions/array/countEqual.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ using FunctionCountEqual = FunctionArrayIndex<CountEqualAction, NameCountEqual>;
99

1010
REGISTER_FUNCTION(CountEqual)
1111
{
12-
factory.registerFunction<FunctionCountEqual>();
12+
FunctionDocumentation::Description description = R"(
13+
Returns the number of elements in the array equal to `x`. Equivalent to `arrayCount(elem -> elem = x, arr)`.
14+
15+
`NULL` elements are handled as separate values.
16+
)";
17+
FunctionDocumentation::Syntax syntax = "countEqual(arr, x)";
18+
FunctionDocumentation::Arguments arguments = {
19+
{"arr", "Array to search. [`Array(T)`](/sql-reference/data-types/array)."},
20+
{"x", "Value in the array to count. Any type."}
21+
};
22+
FunctionDocumentation::ReturnedValue returned_value = "Returns the number of elements in the array equal to `x`. [UInt64](/sql-reference/data-types/int-uint).";
23+
FunctionDocumentation::Examples example = {{"Usage example", "SELECT countEqual([1, 2, NULL, NULL], NULL)", "2"}};
24+
FunctionDocumentation::IntroducedIn introduced_in = {1, 1};
25+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
26+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, example, introduced_in, category};
27+
28+
factory.registerFunction<FunctionCountEqual>(documentation);
1329
}
1430
}

src/Functions/array/hasAll.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,37 @@ class FunctionArrayHasAll : public FunctionArrayHasAllAny
1616

1717
REGISTER_FUNCTION(HasAll)
1818
{
19-
factory.registerFunction<FunctionArrayHasAll>();
19+
FunctionDocumentation::Description description = R"(
20+
Checks whether one array is a subset of another.
21+
22+
- An empty array is a subset of any array.
23+
- `Null` is processed as a value.
24+
- The order of values in both the arrays does not matter.
25+
)";
26+
FunctionDocumentation::Syntax syntax = "hasAll(set, subset)";
27+
FunctionDocumentation::Arguments arguments = {
28+
{"set", "Array of any type with a set of elements. [`Array`](/sql-reference/data-types/array)."},
29+
{"subset", "Array of any type that shares a common supertype with `set` containing elements that should be tested to be a subset of `set`. [`Array`](/sql-reference/data-types/array)."},
30+
};
31+
FunctionDocumentation::ReturnedValue returned_value = R"(
32+
- `1`, if `set` contains all of the elements from `subset`.
33+
- `0`, otherwise.
34+
35+
Raises a `NO_COMMON_TYPE` exception if the set and subset elements do not share a common supertype.
36+
)";
37+
FunctionDocumentation::Examples examples = {
38+
{"Empty arrays", "SELECT hasAll([], [])", "1"},
39+
{"Arrays containing NULL values", "SELECT hasAll([1, Null], [Null])", "1"},
40+
{"Arrays containing values of a different type", "SELECT hasAll([1.0, 2, 3, 4], [1, 3])", "1"},
41+
{"Arrays containing String values", "SELECT hasAll(['a', 'b'], ['a'])", "1"},
42+
{"Arrays without a common type", "SELECT hasAll([1], ['a'])", "Raises a NO_COMMON_TYPE exception"},
43+
{"Array of arrays", "SELECT hasAll([[1, 2], [3, 4]], [[1, 2], [3, 5]])", "0"},
44+
};
45+
FunctionDocumentation::IntroducedIn introduced_in = {1, 1};
46+
FunctionDocumentation::Category category = FunctionDocumentation::Category::Array;
47+
FunctionDocumentation documentation = {description, syntax, arguments, returned_value, examples, introduced_in, category};
48+
49+
factory.registerFunction<FunctionArrayHasAll>(documentation);
2050
}
2151

2252
}

0 commit comments

Comments
 (0)