2020-04-03 13:23:32 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/functions/array-functions
2023-04-19 17:05:55 +00:00
sidebar_position: 10
2022-04-09 13:29:05 +00:00
sidebar_label: Arrays
2020-04-03 13:23:32 +00:00
---
2022-06-02 10:55:18 +00:00
# Array Functions
2017-12-28 15:13:23 +00:00
2024-01-10 13:34:55 +00:00
## empty {#empty}
2017-12-28 15:13:23 +00:00
2021-08-10 10:53:35 +00:00
Checks whether the input array is empty.
**Syntax**
``` sql
empty([x])
```
2021-08-13 10:13:09 +00:00
An array is considered empty if it does not contain any elements.
2021-08-10 10:53:35 +00:00
2023-04-03 13:43:11 +00:00
:::note
2024-05-24 03:54:16 +00:00
Can be optimized by enabling the [`optimize_functions_to_subcolumns` setting ](../../operations/settings/settings.md#optimize-functions-to-subcolumns ). With `optimize_functions_to_subcolumns = 1` the function reads only [size0 ](../data-types/array.md#array-size ) subcolumn instead of reading and processing the whole array column. The query `SELECT empty(arr) FROM TABLE;` transforms to `SELECT arr.size0 = 0 FROM TABLE;` .
2022-04-09 13:29:05 +00:00
:::
2021-08-10 10:53:35 +00:00
The function also works for [strings ](string-functions.md#empty ) or [UUID ](uuid-functions.md#empty ).
**Arguments**
2023-04-17 18:10:41 +00:00
- `[x]` — Input array. [Array ](../data-types/array.md ).
2021-08-10 10:53:35 +00:00
**Returned value**
2024-05-23 13:48:20 +00:00
- Returns `1` for an empty array or `0` for a non-empty array. [UInt8 ](../data-types/int-uint.md ).
2021-08-10 10:53:35 +00:00
**Example**
Query:
```sql
SELECT empty([]);
```
Result:
2017-12-28 15:13:23 +00:00
2021-08-10 10:53:35 +00:00
```text
┌─empty(array())─┐
│ 1 │
└────────────────┘
```
2021-06-23 00:15:11 +00:00
2024-01-10 13:34:55 +00:00
## notEmpty {#notempty}
2017-12-28 15:13:23 +00:00
2021-08-10 10:53:35 +00:00
Checks whether the input array is non-empty.
**Syntax**
``` sql
notEmpty([x])
```
2021-08-12 13:18:33 +00:00
2021-08-13 10:13:35 +00:00
An array is considered non-empty if it contains at least one element.
2021-08-10 10:53:35 +00:00
2023-04-03 13:43:11 +00:00
:::note
2024-05-24 03:54:16 +00:00
Can be optimized by enabling the [optimize_functions_to_subcolumns ](../../operations/settings/settings.md#optimize-functions-to-subcolumns ) setting. With `optimize_functions_to_subcolumns = 1` the function reads only [size0 ](../data-types/array.md#array-size ) subcolumn instead of reading and processing the whole array column. The query `SELECT notEmpty(arr) FROM table` transforms to `SELECT arr.size0 != 0 FROM TABLE` .
2022-04-09 13:29:05 +00:00
:::
2021-08-10 10:53:35 +00:00
The function also works for [strings ](string-functions.md#notempty ) or [UUID ](uuid-functions.md#notempty ).
**Arguments**
2023-04-17 18:10:41 +00:00
- `[x]` — Input array. [Array ](../data-types/array.md ).
2021-08-10 10:53:35 +00:00
**Returned value**
2024-05-23 13:48:20 +00:00
- Returns `1` for a non-empty array or `0` for an empty array. [UInt8 ](../data-types/int-uint.md ).
2021-08-10 10:53:35 +00:00
**Example**
Query:
```sql
SELECT notEmpty([1,2]);
```
Result:
2017-12-28 15:13:23 +00:00
2021-08-10 10:53:35 +00:00
```text
┌─notEmpty([1, 2])─┐
│ 1 │
└──────────────────┘
```
2021-06-23 00:15:11 +00:00
2022-06-02 10:55:18 +00:00
## length
2017-12-28 15:13:23 +00:00
Returns the number of items in the array.
The result type is UInt64.
The function also works for strings.
2024-05-24 03:54:16 +00:00
Can be optimized by enabling the [optimize_functions_to_subcolumns ](../../operations/settings/settings.md#optimize-functions-to-subcolumns ) setting. With `optimize_functions_to_subcolumns = 1` the function reads only [size0 ](../data-types/array.md#array-size ) subcolumn instead of reading and processing the whole array column. The query `SELECT length(arr) FROM table` transforms to `SELECT arr.size0 FROM TABLE` .
2021-06-23 00:15:11 +00:00
2023-07-17 02:52:05 +00:00
Alias: `OCTET_LENGTH`
2024-01-25 15:50:18 +00:00
## emptyArrayUInt8
2024-03-11 07:39:08 +00:00
Returns an empty UInt8 array.
2024-01-25 15:50:18 +00:00
**Syntax**
```sql
emptyArrayUInt8()
```
**Arguments**
None.
**Returned value**
An empty array.
**Examples**
Query:
```sql
2024-03-11 07:39:08 +00:00
SELECT emptyArrayUInt8();
2024-01-25 15:50:18 +00:00
```
Result:
```response
2024-03-11 07:39:08 +00:00
[]
2024-01-25 15:50:18 +00:00
```
## emptyArrayUInt16
2024-03-11 07:39:08 +00:00
Returns an empty UInt16 array.
2024-01-25 15:50:18 +00:00
**Syntax**
```sql
emptyArrayUInt16()
```
**Arguments**
None.
**Returned value**
An empty array.
**Examples**
Query:
```sql
2024-03-11 07:39:08 +00:00
SELECT emptyArrayUInt16();
2024-01-25 15:50:18 +00:00
```
Result:
```response
2024-03-11 07:39:08 +00:00
[]
2024-01-25 15:50:18 +00:00
```
## emptyArrayUInt32
2024-03-11 07:39:08 +00:00
Returns an empty UInt32 array.
2024-01-25 15:50:18 +00:00
**Syntax**
```sql
emptyArrayUInt32()
```
**Arguments**
None.
**Returned value**
An empty array.
**Examples**
Query:
```sql
2024-03-11 07:39:08 +00:00
SELECT emptyArrayUInt32();
2024-01-25 15:50:18 +00:00
```
Result:
```response
2024-03-11 07:39:08 +00:00
[]
2024-01-25 15:50:18 +00:00
```
## emptyArrayUInt64
2024-03-11 07:39:08 +00:00
Returns an empty UInt64 array.
2024-01-25 15:50:18 +00:00
**Syntax**
```sql
emptyArrayUInt64()
```
**Arguments**
None.
**Returned value**
An empty array.
**Examples**
Query:
```sql
2024-03-11 07:39:08 +00:00
SELECT emptyArrayUInt64();
2024-01-25 15:50:18 +00:00
```
Result:
```response
2024-03-11 07:39:08 +00:00
[]
2024-01-25 15:50:18 +00:00
```
2017-12-28 15:13:23 +00:00
2024-01-25 16:29:23 +00:00
## emptyArrayInt8
2024-03-11 07:39:08 +00:00
Returns an empty Int8 array.
2024-01-25 16:29:23 +00:00
**Syntax**
```sql
emptyArrayInt8()
```
**Arguments**
None.
**Returned value**
An empty array.
**Examples**
Query:
```sql
2024-03-11 07:39:08 +00:00
SELECT emptyArrayInt8();
2024-01-25 16:29:23 +00:00
```
Result:
```response
2024-03-11 07:39:08 +00:00
[]
2024-01-25 16:29:23 +00:00
```
## emptyArrayInt16
2024-03-11 07:39:08 +00:00
Returns an empty Int16 array.
2024-01-25 16:29:23 +00:00
**Syntax**
```sql
emptyArrayInt16()
```
**Arguments**
None.
**Returned value**
An empty array.
**Examples**
Query:
```sql
2024-03-11 07:39:08 +00:00
SELECT emptyArrayInt16();
2024-01-25 16:29:23 +00:00
```
Result:
```response
2024-03-11 07:39:08 +00:00
[]
2024-01-25 16:29:23 +00:00
```
## emptyArrayInt32
2024-03-11 07:39:08 +00:00
Returns an empty Int32 array.
2024-01-25 16:29:23 +00:00
**Syntax**
```sql
emptyArrayInt32()
```
**Arguments**
None.
**Returned value**
An empty array.
**Examples**
Query:
```sql
2024-03-11 07:39:08 +00:00
SELECT emptyArrayInt32();
2024-01-25 16:29:23 +00:00
```
Result:
```response
2024-03-11 07:39:08 +00:00
[]
2024-01-25 16:29:23 +00:00
```
## emptyArrayInt64
2024-03-11 07:39:08 +00:00
Returns an empty Int64 array.
2024-01-25 16:29:23 +00:00
**Syntax**
```sql
emptyArrayInt64()
```
**Arguments**
None.
**Returned value**
An empty array.
**Examples**
Query:
```sql
2024-03-11 07:39:08 +00:00
SELECT emptyArrayInt64();
2024-01-25 16:29:23 +00:00
```
Result:
```response
2024-03-11 07:39:08 +00:00
[]
2024-01-25 16:29:23 +00:00
```
2017-12-28 15:13:23 +00:00
2024-01-25 18:41:32 +00:00
## emptyArrayFloat32
2024-03-11 07:39:08 +00:00
Returns an empty Float32 array.
2024-01-25 18:41:32 +00:00
**Syntax**
```sql
emptyArrayFloat32()
```
**Arguments**
None.
**Returned value**
An empty array.
**Examples**
Query:
```sql
2024-03-11 07:39:08 +00:00
SELECT emptyArrayFloat32();
2024-01-25 18:41:32 +00:00
```
Result:
```response
2024-03-11 07:39:08 +00:00
[]
2024-01-25 18:41:32 +00:00
```
## emptyArrayFloat64
2024-03-11 07:39:08 +00:00
Returns an empty Float64 array.
2024-01-25 18:41:32 +00:00
**Syntax**
```sql
emptyArrayFloat64()
```
**Arguments**
None.
**Returned value**
An empty array.
**Examples**
Query:
```sql
2024-03-11 07:39:08 +00:00
SELECT emptyArrayFloat64();
2024-01-25 18:41:32 +00:00
```
Result:
```response
2024-03-11 07:39:08 +00:00
[]
2024-01-25 18:41:32 +00:00
```
2017-12-28 15:13:23 +00:00
2024-01-29 18:21:58 +00:00
## emptyArrayDate
2024-03-11 07:39:08 +00:00
Returns an empty Date array.
2024-01-29 18:21:58 +00:00
**Syntax**
```sql
emptyArrayDate()
```
**Arguments**
None.
**Returned value**
An empty array.
**Examples**
Query:
```sql
2024-03-11 07:39:08 +00:00
SELECT emptyArrayDate();
2024-01-29 18:21:58 +00:00
```
## emptyArrayDateTime
2024-03-11 07:39:08 +00:00
Returns an empty DateTime array.
2024-01-29 18:21:58 +00:00
**Syntax**
```sql
2024-03-11 07:39:08 +00:00
[]
2024-01-29 18:21:58 +00:00
```
**Arguments**
None.
**Returned value**
An empty array.
**Examples**
Query:
```sql
2024-03-11 07:39:08 +00:00
SELECT emptyArrayDateTime();
2024-01-29 18:21:58 +00:00
```
Result:
```response
2024-03-11 07:39:08 +00:00
[]
2024-01-29 18:21:58 +00:00
```
2022-06-02 10:55:18 +00:00
## emptyArrayString
2017-12-28 15:13:23 +00:00
2024-03-11 07:39:08 +00:00
Returns an empty String array.
2024-01-29 18:21:58 +00:00
**Syntax**
```sql
emptyArrayString()
```
**Arguments**
None.
**Returned value**
An empty array.
**Examples**
Query:
```sql
2024-03-11 07:39:08 +00:00
SELECT emptyArrayString();
2024-01-29 18:21:58 +00:00
```
Result:
```response
2024-03-11 07:39:08 +00:00
[]
2024-01-29 18:21:58 +00:00
```
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## emptyArrayToSingle
2017-12-28 15:13:23 +00:00
Accepts an empty array and returns a one-element array that is equal to the default value.
2022-06-02 10:55:18 +00:00
## range(end), range(\[start, \] end \[, step\])
2017-12-28 15:13:23 +00:00
2022-12-21 11:09:22 +00:00
Returns an array of numbers from `start` to `end - 1` by `step` . The supported types are [UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64 ](../data-types/int-uint.md ).
2021-06-16 23:34:59 +00:00
**Syntax**
2023-04-17 18:10:41 +00:00
2021-06-16 23:34:59 +00:00
``` sql
2021-06-18 17:53:52 +00:00
range([start, ] end [, step])
2021-06-16 23:34:59 +00:00
```
2021-06-18 17:53:52 +00:00
2021-06-16 23:34:59 +00:00
**Arguments**
2023-04-17 18:10:41 +00:00
- `start` — The first element of the array. Optional, required if `step` is used. Default value: 0.
- `end` — The number before which the array is constructed. Required.
- `step` — Determines the incremental step between each element in the array. Optional. Default value: 1.
2021-06-18 17:53:52 +00:00
2021-06-16 23:34:59 +00:00
**Returned value**
2023-04-17 18:10:41 +00:00
- Array of numbers from `start` to `end - 1` by `step` .
2021-06-18 17:53:52 +00:00
2021-06-21 10:01:23 +00:00
**Implementation details**
2021-06-21 09:48:24 +00:00
2023-06-02 13:27:56 +00:00
- All arguments `start` , `end` , `step` must be below data types: `UInt8` , `UInt16` , `UInt32` , `UInt64` ,`Int8`, `Int16` , `Int32` , `Int64` , as well as elements of the returned array, which's type is a super type of all arguments.
2023-12-20 18:26:36 +00:00
- An exception is thrown if query results in arrays with a total length of more than number of elements specified by the [function_range_max_elements_in_block ](../../operations/settings/settings.md#function_range_max_elements_in_block ) setting.
2023-07-04 10:20:48 +00:00
- Returns Null if any argument has Nullable(Nothing) type. An exception is thrown if any argument has Null value (Nullable(T) type).
2021-06-21 09:48:24 +00:00
2021-06-16 23:34:59 +00:00
**Examples**
2021-06-18 17:53:52 +00:00
Query:
2023-04-17 18:10:41 +00:00
2021-06-16 23:34:59 +00:00
``` sql
2022-12-21 11:09:22 +00:00
SELECT range(5), range(1, 5), range(1, 5, 2), range(-1, 5, 2);
2021-06-16 23:34:59 +00:00
```
2023-04-17 18:10:41 +00:00
2021-06-16 23:34:59 +00:00
Result:
2023-04-17 18:10:41 +00:00
2021-06-18 17:53:52 +00:00
```txt
2022-12-21 11:09:22 +00:00
┌─range(5)────┬─range(1, 5)─┬─range(1, 5, 2)─┬─range(-1, 5, 2)─┐
│ [0,1,2,3,4] │ [1,2,3,4] │ [1,3] │ [-1,1,3] │
└─────────────┴─────────────┴────────────────┴─────────────────┘
2021-06-16 23:34:59 +00:00
```
2017-12-28 15:13:23 +00:00
2024-05-23 11:54:45 +00:00
## array(x1, ...), operator \[x1, ...\]
2017-12-28 15:13:23 +00:00
Creates an array from the function arguments.
2020-03-20 10:10:48 +00:00
The arguments must be constants and have types that have the smallest common type. At least one argument must be passed, because otherwise it isn’ t clear which type of array to create. That is, you can’ t use this function to create an empty array (to do that, use the ‘ emptyArray\*’ function described above).
Returns an ‘ Array(T)’ type result, where ‘ T’ is the smallest common type out of the passed arguments.
2017-12-28 15:13:23 +00:00
2022-08-17 11:21:03 +00:00
## arrayWithConstant(length, elem)
Creates an array of length `length` filled with the constant `elem` .
2022-06-02 10:55:18 +00:00
## arrayConcat
2017-12-28 15:13:23 +00:00
Combines arrays passed as arguments.
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
arrayConcat(arrays)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2017-12-28 15:13:23 +00:00
2024-05-24 03:54:16 +00:00
- `arrays` – Arbitrary number of arguments of [Array ](../data-types/array.md ) type.
2020-03-20 10:10:48 +00:00
2023-08-09 20:52:09 +00:00
**Example**
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
SELECT arrayConcat([1, 2], [3, 4], [5, 6]) AS res
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +00:00
┌─res───────────┐
│ [1,2,3,4,5,6] │
└───────────────┘
```
2022-06-02 10:55:18 +00:00
## arrayElement(arr, n), operator arr\[n\]
2017-12-28 15:13:23 +00:00
2018-11-10 20:00:22 +00:00
Get the element with the index `n` from the array `arr` . `n` must be any integer type.
2017-12-28 15:13:23 +00:00
Indexes in an array begin from one.
2018-11-10 20:00:22 +00:00
Negative indexes are supported. In this case, it selects the corresponding element numbered from the end. For example, `arr[-1]` is the last item in the array.
2017-12-28 15:13:23 +00:00
2019-09-05 22:37:42 +00:00
If the index falls outside of the bounds of an array, it returns some default value (0 for numbers, an empty string for strings, etc.), except for the case with a non-constant array and a constant index 0 (in this case there will be an error `Array indices are 1-based` ).
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## has(arr, elem)
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
Checks whether the ‘ arr’ array has the ‘ elem’ element.
2020-11-05 15:23:23 +00:00
Returns 0 if the element is not in the array, or 1 if it is.
2017-12-28 15:13:23 +00:00
2018-09-04 11:18:59 +00:00
`NULL` is processed as a value.
2020-03-20 10:10:48 +00:00
``` sql
2018-09-04 11:18:59 +00:00
SELECT has([1, 2, NULL], NULL)
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─has([1, 2, NULL], NULL)─┐
│ 1 │
└─────────────────────────┘
```
2024-01-10 13:34:55 +00:00
## hasAll {#hasall}
2018-10-22 19:32:52 +00:00
Checks whether one array is a subset of another.
2020-03-20 10:10:48 +00:00
``` sql
2018-10-22 19:32:52 +00:00
hasAll(set, subset)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2018-10-22 19:32:52 +00:00
2023-04-17 18:10:41 +00:00
- `set` – Array of any type with a set of elements.
2023-06-29 17:39:39 +00:00
- `subset` – Array of any type that shares a common supertype with `set` containing elements that should be tested to be a subset of `set` .
2018-10-22 19:32:52 +00:00
**Return values**
2023-04-17 18:10:41 +00:00
- `1` , if `set` contains all of the elements from `subset` .
- `0` , otherwise.
2018-10-22 19:32:52 +00:00
2023-06-29 17:39:39 +00:00
Raises an exception `NO_COMMON_TYPE` if the set and subset elements do not share a common supertype.
2018-10-22 19:32:52 +00:00
**Peculiar properties**
2023-04-17 18:10:41 +00:00
- An empty array is a subset of any array.
- `Null` processed as a value.
- Order of values in both of arrays does not matter.
2018-10-22 19:32:52 +00:00
**Examples**
`SELECT hasAll([], [])` returns 1.
`SELECT hasAll([1, Null], [Null])` returns 1.
`SELECT hasAll([1.0, 2, 3, 4], [1, 3])` returns 1.
`SELECT hasAll(['a', 'b'], ['a'])` returns 1.
2023-06-29 17:39:39 +00:00
`SELECT hasAll([1], ['a'])` raises a `NO_COMMON_TYPE` exception.
2018-10-22 19:32:52 +00:00
`SELECT hasAll([[1, 2], [3, 4]], [[1, 2], [3, 5]])` returns 0.
2024-01-10 13:34:55 +00:00
## hasAny {#hasany}
2018-10-22 19:32:52 +00:00
Checks whether two arrays have intersection by some elements.
2020-03-20 10:10:48 +00:00
``` sql
2018-10-22 19:32:52 +00:00
hasAny(array1, array2)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2018-10-22 19:32:52 +00:00
2023-04-17 18:10:41 +00:00
- `array1` – Array of any type with a set of elements.
2023-06-29 17:39:39 +00:00
- `array2` – Array of any type that shares a common supertype with `array1` .
2018-10-22 19:32:52 +00:00
**Return values**
2023-04-17 18:10:41 +00:00
- `1` , if `array1` and `array2` have one similar element at least.
- `0` , otherwise.
2018-10-22 19:32:52 +00:00
2023-06-29 17:39:39 +00:00
Raises an exception `NO_COMMON_TYPE` if the array1 and array2 elements do not share a common supertype.
2018-10-22 19:32:52 +00:00
**Peculiar properties**
2023-04-17 18:10:41 +00:00
- `Null` processed as a value.
- Order of values in both of arrays does not matter.
2018-10-22 19:32:52 +00:00
**Examples**
`SELECT hasAny([1], [])` returns `0` .
`SELECT hasAny([Null], [Null, 1])` returns `1` .
`SELECT hasAny([-128, 1., 512], [1])` returns `1` .
2023-06-29 17:39:39 +00:00
`SELECT hasAny([[1, 2], [3, 4]], ['a', 'c'])` raises a `NO_COMMON_TYPE` exception.
2018-10-22 19:32:52 +00:00
`SELECT hasAll([[1, 2], [3, 4]], [[1, 2], [1, 2]])` returns `1` .
2022-06-02 10:55:18 +00:00
## hasSubstr
2020-06-23 14:42:19 +00:00
Checks whether all the elements of array2 appear in array1 in the same exact order. Therefore, the function will return 1, if and only if `array1 = prefix + array2 + suffix` .
``` sql
hasSubstr(array1, array2)
```
2020-07-09 15:10:35 +00:00
In other words, the functions will check whether all the elements of `array2` are contained in `array1` like
2020-06-23 14:42:19 +00:00
the `hasAll` function. In addition, it will check that the elements are observed in the same order in both `array1` and `array2` .
2020-07-09 15:10:35 +00:00
For Example:
2023-04-17 18:10:41 +00:00
2020-07-09 15:10:35 +00:00
- `hasSubstr([1,2,3,4], [2,3])` returns 1. However, `hasSubstr([1,2,3,4], [3,2])` will return `0` .
- `hasSubstr([1,2,3,4], [1,2,3])` returns 1. However, `hasSubstr([1,2,3,4], [1,2,4])` will return `0` .
2020-06-23 14:42:19 +00:00
2021-02-15 21:22:10 +00:00
**Arguments**
2020-06-23 14:42:19 +00:00
2023-04-17 18:10:41 +00:00
- `array1` – Array of any type with a set of elements.
- `array2` – Array of any type with a set of elements.
2020-06-23 14:42:19 +00:00
**Return values**
2023-04-17 18:10:41 +00:00
- `1` , if `array1` contains `array2` .
- `0` , otherwise.
2020-06-23 14:42:19 +00:00
2023-06-29 17:39:39 +00:00
Raises an exception `NO_COMMON_TYPE` if the array1 and array2 elements do not share a common supertype.
2020-06-23 14:42:19 +00:00
**Peculiar properties**
2023-04-17 18:10:41 +00:00
- The function will return `1` if `array2` is empty.
- `Null` processed as a value. In other words `hasSubstr([1, 2, NULL, 3, 4], [2,3])` will return `0` . However, `hasSubstr([1, 2, NULL, 3, 4], [2,NULL,3])` will return `1`
- Order of values in both of arrays does matter.
2020-06-23 14:42:19 +00:00
**Examples**
`SELECT hasSubstr([], [])` returns 1.
`SELECT hasSubstr([1, Null], [Null])` returns 1.
`SELECT hasSubstr([1.0, 2, 3, 4], [1, 3])` returns 0.
`SELECT hasSubstr(['a', 'b'], ['a'])` returns 1.
`SELECT hasSubstr(['a', 'b' , 'c'], ['a', 'b'])` returns 1.
`SELECT hasSubstr(['a', 'b' , 'c'], ['a', 'c'])` returns 0.
`SELECT hasSubstr([[1, 2], [3, 4], [5, 6]], [[1, 2], [3, 4]])` returns 1.
2023-06-29 17:39:39 +00:00
i
`SELECT hasSubstr([1, 2, NULL, 3, 4], ['a'])` raises a `NO_COMMON_TYPE` exception.
2020-06-23 14:42:19 +00:00
2022-06-02 10:55:18 +00:00
## indexOf(arr, x)
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
Returns the index of the first ‘ x’ element (starting from 1) if it is in the array, or 0 if it is not.
2018-09-04 11:18:59 +00:00
Example:
2020-03-20 10:10:48 +00:00
``` sql
2018-09-04 11:18:59 +00:00
SELECT indexOf([1, 3, NULL, NULL], NULL)
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─indexOf([1, 3, NULL, NULL], NULL)─┐
│ 3 │
└───────────────────────────────────┘
```
Elements set to `NULL` are handled as normal values.
2017-12-28 15:13:23 +00:00
2024-05-23 11:54:45 +00:00
## arrayCount(\[func,\] arr1, ...)
2020-09-08 15:55:00 +00:00
2024-05-23 11:54:45 +00:00
Returns the number of elements for which `func(arr1[i], ..., arrN[i])` returns something other than 0. If `func` is not specified, it returns the number of non-zero elements in the array.
2020-09-08 15:55:00 +00:00
2021-04-02 10:23:11 +00:00
Note that the `arrayCount` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You can pass a lambda function to it as the first argument.
2020-09-08 15:55:00 +00:00
2024-04-03 10:15:19 +00:00
## arrayDotProduct
2024-04-05 14:30:11 +00:00
Returns the dot product of two arrays.
2024-04-03 10:15:19 +00:00
**Syntax**
```sql
arrayDotProduct(vector1, vector2)
```
2024-04-10 15:09:01 +00:00
Alias: `scalarProduct` , `dotProduct`
2024-04-03 10:15:19 +00:00
**Parameters**
- `vector1` : First vector. [Array ](../data-types/array.md ) or [Tuple ](../data-types/tuple.md ) of numeric values.
- `vector2` : Second vector. [Array ](../data-types/array.md ) or [Tuple ](../data-types/tuple.md ) of numeric values.
2024-04-04 16:59:40 +00:00
:::note
2024-04-03 10:15:19 +00:00
The sizes of the two vectors must be equal. Arrays and Tuples may also contain mixed element types.
:::
**Returned value**
2024-05-23 13:48:20 +00:00
- The dot product of the two vectors. [Numeric ](https://clickhouse.com/docs/en/native-protocol/columns#numeric-types ).
2024-04-03 10:15:19 +00:00
2024-05-23 13:48:20 +00:00
:::note
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.
:::
2024-04-03 10:15:19 +00:00
**Examples**
Query:
```sql
SELECT arrayDotProduct([1, 2, 3], [4, 5, 6]) AS res, toTypeName(res);
```
Result:
```response
32 UInt16
```
Query:
```sql
SELECT dotProduct((1::UInt16, 2::UInt8, 3::Float32),(4::Int16, 5::Float32, 6::UInt8)) AS res, toTypeName(res);
```
Result:
```response
32 Float64
```
2022-06-02 10:55:18 +00:00
## countEqual(arr, x)
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
Returns the number of elements in the array equal to x. Equivalent to arrayCount (elem -\> elem = x, arr).
2018-09-04 11:18:59 +00:00
`NULL` elements are handled as separate values.
Example:
2020-03-20 10:10:48 +00:00
``` sql
2018-09-04 11:18:59 +00:00
SELECT countEqual([1, 2, NULL, NULL], NULL)
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─countEqual([1, 2, NULL, NULL], NULL)─┐
│ 2 │
└──────────────────────────────────────┘
```
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## arrayEnumerate(arr)
2017-12-28 15:13:23 +00:00
2024-05-23 11:54:45 +00:00
Returns the array \[1, 2, 3, ..., length (arr) \]
2017-12-28 15:13:23 +00:00
This function is normally used with ARRAY JOIN. It allows counting something just once for each array after applying ARRAY JOIN. Example:
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
SELECT
count() AS Reaches,
countIf(num = 1) AS Hits
FROM test.hits
ARRAY JOIN
GoalsReached,
arrayEnumerate(GoalsReached) AS num
WHERE CounterID = 160656
LIMIT 10
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +00:00
┌─Reaches─┬──Hits─┐
│ 95606 │ 31406 │
└─────────┴───────┘
```
In this example, Reaches is the number of conversions (the strings received after applying ARRAY JOIN), and Hits is the number of pageviews (strings before ARRAY JOIN). In this particular case, you can get the same result in an easier way:
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
SELECT
sum(length(GoalsReached)) AS Reaches,
count() AS Hits
FROM test.hits
WHERE (CounterID = 160656) AND notEmpty(GoalsReached)
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +00:00
┌─Reaches─┬──Hits─┐
│ 95606 │ 31406 │
└─────────┴───────┘
```
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.
2024-05-23 11:54:45 +00:00
## arrayEnumerateUniq(arr, ...)
2017-12-28 15:13:23 +00:00
Returns an array the same size as the source array, indicating for each element what its position is among elements with the same value.
2018-12-25 15:25:43 +00:00
For example: arrayEnumerateUniq(\[10, 20, 10, 30\]) = \[1, 1, 2, 1\].
2017-12-28 15:13:23 +00:00
This function is useful when using ARRAY JOIN and aggregation of array elements.
Example:
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
SELECT
Goals.ID AS GoalID,
sum(Sign) AS Reaches,
sumIf(Sign, num = 1) AS Visits
FROM test.visits
ARRAY JOIN
Goals,
arrayEnumerateUniq(Goals.ID) AS num
WHERE CounterID = 160656
GROUP BY GoalID
ORDER BY Reaches DESC
LIMIT 10
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +00:00
┌──GoalID─┬─Reaches─┬─Visits─┐
│ 53225 │ 3214 │ 1097 │
│ 2825062 │ 3188 │ 1097 │
│ 56600 │ 2803 │ 488 │
│ 1989037 │ 2401 │ 365 │
│ 2830064 │ 2396 │ 910 │
│ 1113562 │ 2372 │ 373 │
│ 3270895 │ 2262 │ 812 │
│ 1084657 │ 2262 │ 345 │
│ 56599 │ 2260 │ 799 │
│ 3271094 │ 2256 │ 812 │
└─────────┴─────────┴────────┘
```
In this example, each goal ID has a calculation of the number of conversions (each element in the Goals nested data structure is a goal that was reached, which we refer to as a conversion) and the number of sessions. Without ARRAY JOIN, we would have counted the number of sessions as sum(Sign). But in this particular case, the rows were multiplied by the nested Goals structure, so in order to count each session one time after this, we apply a condition to the value of the arrayEnumerateUniq(Goals.ID) function.
The arrayEnumerateUniq function can take multiple arrays of the same size as arguments. In this case, uniqueness is considered for tuples of elements in the same positions in all the arrays.
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
SELECT arrayEnumerateUniq([1, 1, 1, 2, 2, 2], [1, 1, 2, 1, 1, 2]) AS res
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +00:00
┌─res───────────┐
│ [1,2,1,1,2,1] │
└───────────────┘
```
This is necessary when using ARRAY JOIN with a nested data structure and further aggregation across multiple elements in this structure.
2024-04-04 12:13:22 +00:00
## arrayEnumerateUniqRanked
Returns an array the same size as the source array, indicating for each element what its position is among elements with the same value. It allows for enumeration of a multidimensional array with the ability to specify how deep to look inside the array.
**Syntax**
```sql
arrayEnumerateUniqRanked(clear_depth, arr, max_array_depth)
```
**Parameters**
- `clear_depth` : Enumerate elements at the specified level separately. Positive [Integer ](../data-types/int-uint.md ) less than or equal to `max_arr_depth` .
2024-04-04 17:16:53 +00:00
- `arr` : N-dimensional array to enumerate. [Array ](../data-types/array.md ).
2024-04-04 12:13:22 +00:00
- `max_array_depth` : The maximum effective depth. Positive [Integer ](../data-types/int-uint.md ) less than or equal to the depth of `arr` .
**Example**
With `clear_depth=1` and `max_array_depth=1` , the result of `arrayEnumerateUniqRanked` is identical to that which [`arrayEnumerateUniq` ](#arrayenumerateuniqarr ) would give for the same array.
Query:
``` sql
SELECT arrayEnumerateUniqRanked(1, [1,2,1], 1);
```
Result:
``` text
[1,1,2]
```
2024-04-04 17:16:53 +00:00
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.
2024-04-04 12:13:22 +00:00
Query:
``` sql
SELECT arrayEnumerateUniqRanked(1, [[1,2,3],[2,2,1],[3]], 2);
```
Result:
``` text
[[1,1,1],[2,3,2],[2]]
```
2024-04-08 17:21:44 +00:00
Changing `clear_depth=2` , results in elements being enumerated separately for each row.
2024-04-04 12:13:22 +00:00
Query:
``` sql
SELECT arrayEnumerateUniqRanked(2, [[1,2,3],[2,2,1],[3]], 2);
```
Result:
``` text
[[1,1,1],[1,2,1],[1]]
```
2022-06-02 10:55:18 +00:00
## arrayPopBack
2017-12-28 15:13:23 +00:00
Removes the last item from the array.
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
arrayPopBack(array)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2017-12-28 15:13:23 +00:00
2023-04-17 18:10:41 +00:00
- `array` – Array.
2017-12-28 15:13:23 +00:00
**Example**
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT arrayPopBack([1, 2, 3]) AS res;
2017-12-28 15:13:23 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +00:00
┌─res───┐
│ [1,2] │
└───────┘
```
2022-06-02 10:55:18 +00:00
## arrayPopFront
2017-12-28 15:13:23 +00:00
Removes the first item from the array.
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
arrayPopFront(array)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2017-12-28 15:13:23 +00:00
2023-04-17 18:10:41 +00:00
- `array` – Array.
2017-12-28 15:13:23 +00:00
**Example**
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT arrayPopFront([1, 2, 3]) AS res;
2017-12-28 15:13:23 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +00:00
┌─res───┐
│ [2,3] │
└───────┘
```
2022-06-02 10:55:18 +00:00
## arrayPushBack
2017-12-28 15:13:23 +00:00
Adds one item to the end of the array.
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
arrayPushBack(array, single_value)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2017-12-28 15:13:23 +00:00
2023-04-17 18:10:41 +00:00
- `array` – Array.
2024-05-24 03:54:16 +00:00
- `single_value` – A single value. Only numbers can be added to an array with numbers, and only strings can be added to an array of strings. When adding numbers, ClickHouse automatically sets the `single_value` type for the data type of the array. For more information about the types of data in ClickHouse, see “[Data types](../data-types/index.md#data_types)”. Can be `NULL` . The function adds a `NULL` element to an array, and the type of array elements converts to `Nullable` .
2017-12-28 15:13:23 +00:00
**Example**
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT arrayPushBack(['a'], 'b') AS res;
2017-12-28 15:13:23 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +00:00
┌─res───────┐
│ ['a','b'] │
└───────────┘
```
2022-06-02 10:55:18 +00:00
## arrayPushFront
2017-12-28 15:13:23 +00:00
Adds one element to the beginning of the array.
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
arrayPushFront(array, single_value)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2017-12-28 15:13:23 +00:00
2023-04-17 18:10:41 +00:00
- `array` – Array.
2024-05-24 03:54:16 +00:00
- `single_value` – A single value. Only numbers can be added to an array with numbers, and only strings can be added to an array of strings. When adding numbers, ClickHouse automatically sets the `single_value` type for the data type of the array. For more information about the types of data in ClickHouse, see “[Data types](../data-types/index.md#data_types)”. Can be `NULL` . The function adds a `NULL` element to an array, and the type of array elements converts to `Nullable` .
2017-12-28 15:13:23 +00:00
**Example**
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT arrayPushFront(['b'], 'a') AS res;
2017-12-28 15:13:23 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2017-12-28 15:13:23 +00:00
┌─res───────┐
│ ['a','b'] │
└───────────┘
```
2022-06-02 10:55:18 +00:00
## arrayResize
2018-09-04 11:18:59 +00:00
Changes the length of the array.
2020-03-20 10:10:48 +00:00
``` sql
2018-09-04 11:18:59 +00:00
arrayResize(array, size[, extender])
```
2021-02-15 21:22:10 +00:00
**Arguments:**
2018-09-04 11:18:59 +00:00
2023-04-17 18:10:41 +00:00
- `array` — Array.
- `size` — Required length of the array.
- If `size` is less than the original size of the array, the array is truncated from the right.
- If `size` is larger than the initial size of the array, the array is extended to the right with `extender` values or default values for the data type of the array items.
- `extender` — Value for extending an array. Can be `NULL` .
2018-09-04 11:18:59 +00:00
**Returned value:**
An array of length `size` .
**Examples of calls**
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT arrayResize([1], 3);
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─arrayResize([1], 3)─┐
│ [1,0,0] │
└─────────────────────┘
```
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT arrayResize([1], 3, NULL);
2019-09-23 15:31:46 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─arrayResize([1], 3, NULL)─┐
│ [1,NULL,NULL] │
└───────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## arraySlice
2017-12-28 15:13:23 +00:00
Returns a slice of the array.
2020-03-20 10:10:48 +00:00
``` sql
2017-12-28 15:13:23 +00:00
arraySlice(array, offset[, length])
```
2021-02-15 21:22:10 +00:00
**Arguments**
2017-12-28 15:13:23 +00:00
2022-05-07 10:48:11 +00:00
- `array` – Array of data.
- `offset` – Indent from the edge of the array. A positive value indicates an offset on the left, and a negative value is an indent on the right. Numbering of the array items begins with 1.
- `length` – The length of the required slice. If you specify a negative value, the function returns an open slice `[offset, array_length - length]` . If you omit the value, the function returns the slice `[offset, the_end_of_array]` .
2017-12-28 15:13:23 +00:00
**Example**
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT arraySlice([1, 2, NULL, 4, 5], 2, 3) AS res;
2017-12-28 15:13:23 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-04 11:18:59 +00:00
┌─res────────┐
│ [2,NULL,4] │
└────────────┘
2017-12-28 15:13:23 +00:00
```
2018-09-04 11:18:59 +00:00
Array elements set to `NULL` are handled as normal values.
2024-01-17 12:01:52 +00:00
## arrayShingles
Generates an array of "shingles", i.e. consecutive sub-arrays with specified length of the input array.
**Syntax**
``` sql
arrayShingles(array, length)
```
**Arguments**
2024-05-24 03:54:16 +00:00
- `array` — Input array [Array ](../data-types/array.md ).
2024-01-17 12:01:52 +00:00
- `length` — The length of each shingle.
**Returned value**
2024-05-24 03:54:16 +00:00
- An array of generated shingles. [Array ](../data-types/array.md ).
2024-01-17 12:01:52 +00:00
**Examples**
Query:
``` sql
SELECT arrayShingles([1,2,3,4], 3) as res;
```
Result:
``` text
┌─res───────────────┐
│ [[1,2,3],[2,3,4]] │
└───────────────────┘
```
2024-05-23 11:54:45 +00:00
## arraySort(\[func,\] arr, ...) {#sort}
2019-04-19 15:18:57 +00:00
2019-04-23 19:43:20 +00:00
Sorts the elements of the `arr` array in ascending order. If the `func` function is specified, sorting order is determined by the result of the `func` function applied to the elements of the array. If `func` accepts multiple arguments, the `arraySort` function is passed several arrays that the arguments of `func` will correspond to. Detailed examples are shown at the end of `arraySort` description.
2019-04-19 15:18:57 +00:00
2019-04-23 12:56:22 +00:00
Example of integer values sorting:
2019-04-19 15:18:57 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-04-23 12:56:22 +00:00
SELECT arraySort([1, 3, 3, 0]);
2019-04-19 15:18:57 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2019-04-19 15:18:57 +00:00
┌─arraySort([1, 3, 3, 0])─┐
│ [0,1,3,3] │
└─────────────────────────┘
```
2019-04-23 12:56:22 +00:00
Example of string values sorting:
2019-04-19 15:55:23 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-04-23 12:56:22 +00:00
SELECT arraySort(['hello', 'world', '!']);
2019-04-19 15:55:23 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2019-04-19 15:55:23 +00:00
┌─arraySort(['hello', 'world', '!'])─┐
│ ['!','hello','world'] │
└────────────────────────────────────┘
```
2019-04-23 12:56:22 +00:00
Consider the following sorting order for the `NULL` , `NaN` and `Inf` values:
2020-03-20 10:10:48 +00:00
``` sql
2019-04-23 12:56:22 +00:00
SELECT arraySort([1, nan, 2, NULL, 3, nan, -4, NULL, inf, -inf]);
```
2020-03-20 10:10:48 +00:00
``` text
2019-04-23 12:56:22 +00:00
┌─arraySort([1, nan, 2, NULL, 3, nan, -4, NULL, inf, -inf])─┐
│ [-inf,-4,1,2,3,inf,nan,nan,NULL,NULL] │
└───────────────────────────────────────────────────────────┘
```
2023-04-17 18:10:41 +00:00
- `-Inf` values are first in the array.
- `NULL` values are last in the array.
- `NaN` values are right before `NULL` .
- `Inf` values are right before `NaN` .
2019-04-23 12:56:22 +00:00
2020-09-08 15:55:00 +00:00
Note that `arraySort` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You can pass a lambda function to it as the first argument. In this case, sorting order is determined by the result of the lambda function applied to the elements of the array.
2019-04-23 19:54:32 +00:00
2020-03-20 10:10:48 +00:00
Let’ s consider the following example:
2019-04-23 12:56:22 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-04-23 19:43:20 +00:00
SELECT arraySort((x) -> -x, [1, 2, 3]) as res;
```
2020-03-20 10:10:48 +00:00
``` text
2019-04-23 19:43:20 +00:00
┌─res─────┐
│ [3,2,1] │
└─────────┘
```
2024-06-12 12:09:37 +00:00
For each element of the source array, the lambda function returns the sorting key, that is, \[1 – \> -1, 2 – \> -2, 3 – \> -3\]. Since the `arraySort` function sorts the keys in ascending order, the result is \[3, 2, 1\]. Thus, the `(x) – > -x` lambda function sets the [descending order ](#arrayreversesort ) in a sorting.
2019-04-23 19:43:20 +00:00
2019-04-24 21:08:49 +00:00
The lambda function can accept multiple arguments. In this case, you need to pass the `arraySort` function several arrays of identical length that the arguments of lambda function will correspond to. The resulting array will consist of elements from the first input array; elements from the next input array(s) specify the sorting keys. For example:
2019-04-23 19:43:20 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-04-23 19:43:20 +00:00
SELECT arraySort((x, y) -> y, ['hello', 'world'], [2, 1]) as res;
2019-04-23 12:56:22 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2019-04-23 12:56:22 +00:00
┌─res────────────────┐
│ ['world', 'hello'] │
└────────────────────┘
```
2021-05-27 19:44:11 +00:00
Here, the elements that are passed in the second array (\[2, 1\]) define a sorting key for the corresponding element from the source array (\[‘ hello’ , ‘ world’ \]), that is, \[‘ hello’ – \> 2, ‘ world’ – \> 1\]. Since the lambda function does not use `x` , actual values of the source array do not affect the order in the result. So, ‘ hello’ will be the second element in the result, and ‘ world’ will be the first.
2019-04-23 12:56:22 +00:00
2019-04-24 21:08:49 +00:00
Other examples are shown below.
2019-04-23 12:56:22 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-04-23 19:43:20 +00:00
SELECT arraySort((x, y) -> y, [0, 1, 2], ['c', 'b', 'a']) as res;
2019-04-19 15:18:57 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2019-04-23 12:56:22 +00:00
┌─res─────┐
2019-04-23 19:43:20 +00:00
│ [2,1,0] │
2019-04-23 12:56:22 +00:00
└─────────┘
2019-04-19 15:18:57 +00:00
```
2020-03-20 10:10:48 +00:00
``` sql
2019-04-23 19:54:32 +00:00
SELECT arraySort((x, y) -> -y, [0, 1, 2], [1, 2, 3]) as res;
```
2020-03-20 10:10:48 +00:00
``` text
2019-04-23 19:54:32 +00:00
┌─res─────┐
│ [2,1,0] │
└─────────┘
```
2023-04-03 13:43:11 +00:00
:::note
2022-04-09 13:29:05 +00:00
To improve sorting efficiency, the [Schwartzian transform ](https://en.wikipedia.org/wiki/Schwartzian_transform ) is used.
:::
2019-04-19 15:55:23 +00:00
2024-05-23 11:54:45 +00:00
## arrayPartialSort(\[func,\] limit, arr, ...)
2023-04-21 02:08:24 +00:00
Same as `arraySort` with additional `limit` argument allowing partial sorting. Returns an array of the same size as the original array where elements in range `[1..limit]` are sorted in ascending order. Remaining elements `(limit..N]` shall contain elements in unspecified order.
2024-06-12 12:09:37 +00:00
## arrayReverseSort
2019-04-19 15:55:23 +00:00
2019-04-23 19:43:20 +00:00
Sorts the elements of the `arr` array in descending order. If the `func` function is specified, `arr` is sorted according to the result of the `func` function applied to the elements of the array, and then the sorted array is reversed. If `func` accepts multiple arguments, the `arrayReverseSort` function is passed several arrays that the arguments of `func` will correspond to. Detailed examples are shown at the end of `arrayReverseSort` description.
2019-04-23 12:56:22 +00:00
2024-06-12 12:09:37 +00:00
**Syntax**
```sql
arrayReverseSort([func,] arr, ...)
```
2019-04-23 12:56:22 +00:00
Example of integer values sorting:
2019-04-19 15:18:57 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-04-23 12:56:22 +00:00
SELECT arrayReverseSort([1, 3, 3, 0]);
2019-04-19 15:55:23 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2019-04-19 15:55:23 +00:00
┌─arrayReverseSort([1, 3, 3, 0])─┐
│ [3,3,1,0] │
└────────────────────────────────┘
```
2019-04-23 12:56:22 +00:00
Example of string values sorting:
2019-04-19 16:04:45 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-04-23 12:56:22 +00:00
SELECT arrayReverseSort(['hello', 'world', '!']);
2019-04-19 15:55:23 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2019-04-19 15:55:23 +00:00
┌─arrayReverseSort(['hello', 'world', '!'])─┐
│ ['world','hello','!'] │
└───────────────────────────────────────────┘
```
2019-04-19 15:18:57 +00:00
2019-04-23 12:56:22 +00:00
Consider the following sorting order for the `NULL` , `NaN` and `Inf` values:
2019-06-14 12:44:33 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-04-23 12:56:22 +00:00
SELECT arrayReverseSort([1, nan, 2, NULL, 3, nan, -4, NULL, inf, -inf]) as res;
```
2020-03-20 10:10:48 +00:00
``` text
2019-04-23 12:56:22 +00:00
┌─res───────────────────────────────────┐
│ [inf,3,2,1,-4,-inf,nan,nan,NULL,NULL] │
└───────────────────────────────────────┘
2019-04-19 15:18:57 +00:00
```
2019-04-23 12:56:22 +00:00
2023-04-17 18:10:41 +00:00
- `Inf` values are first in the array.
- `NULL` values are last in the array.
- `NaN` values are right before `NULL` .
- `-Inf` values are right before `NaN` .
2019-04-23 12:56:22 +00:00
2020-09-08 15:55:00 +00:00
Note that the `arrayReverseSort` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You can pass a lambda function to it as the first argument. Example is shown below.
2019-04-23 19:43:20 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-04-23 19:43:20 +00:00
SELECT arrayReverseSort((x) -> -x, [1, 2, 3]) as res;
```
2020-03-20 10:10:48 +00:00
``` text
2019-04-23 19:43:20 +00:00
┌─res─────┐
│ [1,2,3] │
└─────────┘
```
The array is sorted in the following way:
2023-04-17 18:10:41 +00:00
1. At first, the source array (\[1, 2, 3\]) is sorted according to the result of the lambda function applied to the elements of the array. The result is an array \[3, 2, 1\].
2. Array that is obtained on the previous step, is reversed. So, the final result is \[1, 2, 3\].
2019-06-14 12:44:33 +00:00
2019-04-24 21:08:49 +00:00
The lambda function can accept multiple arguments. In this case, you need to pass the `arrayReverseSort` function several arrays of identical length that the arguments of lambda function will correspond to. The resulting array will consist of elements from the first input array; elements from the next input array(s) specify the sorting keys. For example:
2019-04-23 12:56:22 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-04-23 19:43:20 +00:00
SELECT arrayReverseSort((x, y) -> y, ['hello', 'world'], [2, 1]) as res;
2019-04-19 15:18:57 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2019-04-23 12:56:22 +00:00
┌─res───────────────┐
│ ['hello','world'] │
└───────────────────┘
2019-04-19 15:18:57 +00:00
```
2019-04-23 19:43:20 +00:00
In this example, the array is sorted in the following way:
2019-04-23 12:56:22 +00:00
2023-04-17 18:10:41 +00:00
1. At first, the source array (\[‘ hello’ , ‘ world’ \]) is sorted according to the result of the lambda function applied to the elements of the arrays. The elements that are passed in the second array (\[2, 1\]), define the sorting keys for corresponding elements from the source array. The result is an array \[‘ world’ , ‘ hello’ \].
2. Array that was sorted on the previous step, is reversed. So, the final result is \[‘ hello’ , ‘ world’ \].
2019-06-14 12:44:33 +00:00
Other examples are shown below.
2019-04-23 12:56:22 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-04-23 19:43:20 +00:00
SELECT arrayReverseSort((x, y) -> y, [4, 3, 5], ['a', 'b', 'c']) AS res;
2019-04-23 12:56:22 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2019-04-23 12:56:22 +00:00
┌─res─────┐
2019-04-23 19:43:20 +00:00
│ [5,3,4] │
2019-04-23 12:56:22 +00:00
└─────────┘
```
2020-03-20 10:10:48 +00:00
``` sql
2019-04-23 19:54:32 +00:00
SELECT arrayReverseSort((x, y) -> -y, [4, 3, 5], [1, 2, 3]) AS res;
```
2020-03-20 10:10:48 +00:00
``` text
2019-04-23 19:54:32 +00:00
┌─res─────┐
│ [4,3,5] │
└─────────┘
```
2019-04-19 15:18:57 +00:00
2024-05-23 11:54:45 +00:00
## arrayPartialReverseSort(\[func,\] limit, arr, ...)
2023-04-21 02:08:24 +00:00
Same as `arrayReverseSort` with additional `limit` argument allowing partial sorting. Returns an array of the same size as the original array where elements in range `[1..limit]` are sorted in descending order. Remaining elements `(limit..N]` shall contain elements in unspecified order.
2024-04-04 14:34:18 +00:00
## arrayShuffle
Returns an array of the same size as the original array containing the elements in shuffled order.
2024-04-04 17:16:53 +00:00
Elements are reordered in such a way that each possible permutation of those elements has equal probability of appearance.
2024-04-04 14:34:18 +00:00
**Syntax**
```sql
2024-04-10 15:44:11 +00:00
arrayShuffle(arr[, seed])
2024-04-04 14:34:18 +00:00
```
**Parameters**
2024-04-04 17:16:53 +00:00
- `arr` : The array to partially shuffle. [Array ](../data-types/array.md ).
- `seed` (optional): seed to be used with random number generation. If not provided a random one is used. [UInt or Int ](../data-types/int-uint.md ).
**Returned value**
2024-04-04 14:34:18 +00:00
- Array with elements shuffled.
**Implementation details**
:::note
This function will not materialize constants.
:::
**Examples**
2024-04-04 17:16:53 +00:00
In this example, `arrayShuffle` is used without providing a `seed` and will therefore generate one randomly itself.
2024-04-04 14:34:18 +00:00
Query:
```sql
SELECT arrayShuffle([1, 2, 3, 4]);
```
Note: when using [ClickHouse Fiddle ](https://fiddle.clickhouse.com/ ), the exact response may differ due to random nature of the function.
Result:
```response
[1,4,2,3]
```
In this example, `arrayShuffle` is provided a `seed` and will produce stable results.
Query:
```sql
SELECT arrayShuffle([1, 2, 3, 4], 41);
```
Result:
```response
[3,2,1,4]
```
2024-04-04 14:22:29 +00:00
## arrayPartialShuffle
2024-04-10 16:03:20 +00:00
Given an input array of cardinality `N` , returns an array of size N where elements in the range `[1...limit]` are shuffled and the remaining elements in the range `(limit...n]` are unshuffled.
2024-04-04 14:22:29 +00:00
**Syntax**
```sql
2024-04-10 15:44:11 +00:00
arrayPartialShuffle(arr[, limit[, seed]])
2024-04-04 14:22:29 +00:00
```
**Parameters**
2024-04-04 17:51:04 +00:00
- `arr` : The array size `N` to partially shuffle. [Array ](../data-types/array.md ).
- `limit` (optional): The number to limit element swaps to, in the range `[1..N]` . [UInt or Int ](../data-types/int-uint.md ).
- `seed` (optional): The seed value to be used with random number generation. If not provided a random one is used. [UInt or Int ](../data-types/int-uint.md )
2024-04-04 14:22:29 +00:00
**Returned value**
2024-04-04 14:34:18 +00:00
- Array with elements partially shuffled.
2024-04-04 14:22:29 +00:00
**Implementation details**
:::note
This function will not materialize constants.
2024-04-04 17:16:53 +00:00
2024-04-04 17:51:04 +00:00
The value of `limit` should be in the range `[1..N]` . Values outside of that range are equivalent to performing full [arrayShuffle ](#arrayshuffle ).
2024-04-04 14:22:29 +00:00
:::
**Examples**
2024-04-10 15:44:11 +00:00
Note: when using [ClickHouse Fiddle ](https://fiddle.clickhouse.com/ ), the exact response may differ due to random nature of the function.
2024-04-04 14:22:29 +00:00
Query:
```sql
2024-04-10 15:44:11 +00:00
SELECT arrayPartialShuffle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 1)
2024-04-04 14:22:29 +00:00
```
2024-04-10 15:44:11 +00:00
Result:
2024-04-10 16:03:20 +00:00
The order of elements is preserved (`[2,3,4,5], [7,8,9,10]`) except for the two shuffled elements `[1, 6]` . No `seed` is provided so the function selects its own randomly.
2024-04-04 14:22:29 +00:00
```response
2024-04-10 15:44:11 +00:00
[6,2,3,4,5,1,7,8,9,10]
2024-04-04 14:22:29 +00:00
```
2024-04-10 15:44:11 +00:00
In this example, the `limit` is increased to `2` and a `seed` value is provided. The order
2024-04-04 14:22:29 +00:00
Query:
```sql
2024-04-10 15:44:11 +00:00
SELECT arrayPartialShuffle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 2);
2024-04-04 14:22:29 +00:00
```
2024-04-10 16:03:20 +00:00
The order of elements is preserved (`[4, 5, 6, 7, 8], [10]`) except for the four shuffled elements `[1, 2, 3, 9]` .
2024-04-10 15:44:11 +00:00
2024-04-04 14:22:29 +00:00
Result:
```response
2024-04-10 15:44:11 +00:00
[3,9,1,4,5,6,7,8,2,10]
2024-04-04 14:22:29 +00:00
```
2024-05-23 11:54:45 +00:00
## arrayUniq(arr, ...)
2017-12-28 15:13:23 +00:00
If one argument is passed, it counts the number of different elements in the array.
If multiple arguments are passed, it counts the number of different tuples of elements at corresponding positions in multiple arrays.
2020-03-20 10:10:48 +00:00
If you want to get a list of unique items in an array, you can use arrayReduce(‘ groupUniqArray’ , arr).
2017-12-28 15:13:23 +00:00
2022-06-02 10:55:18 +00:00
## arrayJoin(arr)
2017-12-28 15:13:23 +00:00
2020-06-18 08:24:31 +00:00
A special function. See the section [“ArrayJoin function” ](../../sql-reference/functions/array-join.md#functions_arrayjoin ).
2018-10-16 10:47:17 +00:00
2022-06-02 10:55:18 +00:00
## arrayDifference
2019-01-30 10:39:46 +00:00
2023-07-04 10:20:48 +00:00
Calculates an array of differences between adjacent array elements. The first element of the result array will be 0, the second `a[1] - a[0]` , the third `a[2] - a[1]` , etc. The type of elements in the result array is determined by the type inference rules for subtraction (e.g. `UInt8` - `UInt8` = `Int16` ).
2019-09-30 22:26:30 +00:00
2020-03-20 10:10:48 +00:00
**Syntax**
2019-11-17 18:10:32 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-11-17 18:10:32 +00:00
arrayDifference(array)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2019-11-17 18:10:32 +00:00
2023-04-17 18:10:41 +00:00
- `array` – [Array ](https://clickhouse.com/docs/en/data_types/array/ ).
2019-11-17 18:10:32 +00:00
**Returned values**
2024-05-23 13:48:20 +00:00
Returns an array of differences between adjacent array elements. [UInt\* ](https://clickhouse.com/docs/en/data_types/int_uint/#uint-ranges ), [Int\* ](https://clickhouse.com/docs/en/data_types/int_uint/#int-ranges ), [Float\* ](https://clickhouse.com/docs/en/data_types/float/ ).
2019-11-17 18:10:32 +00:00
**Example**
Query:
2019-01-30 10:39:46 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT arrayDifference([1, 2, 3, 4]);
2019-01-30 10:39:46 +00:00
```
2019-11-17 18:10:32 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
2019-01-30 10:39:46 +00:00
┌─arrayDifference([1, 2, 3, 4])─┐
│ [0,1,1,1] │
└───────────────────────────────┘
```
2019-09-30 22:26:30 +00:00
Example of the overflow due to result type Int64:
2019-11-17 18:10:32 +00:00
Query:
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT arrayDifference([0, 10000000000000000000]);
2019-09-30 22:26:30 +00:00
```
2019-11-17 18:10:32 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
2019-09-30 22:26:30 +00:00
┌─arrayDifference([0, 10000000000000000000])─┐
│ [0,-8446744073709551616] │
└────────────────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## arrayDistinct
2019-01-30 10:39:46 +00:00
2019-11-17 18:10:32 +00:00
Takes an array, returns an array containing the distinct elements only.
2019-09-30 23:03:09 +00:00
2020-03-20 10:10:48 +00:00
**Syntax**
2019-11-17 18:10:32 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-11-17 18:10:32 +00:00
arrayDistinct(array)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2019-11-17 18:10:32 +00:00
2023-04-17 18:10:41 +00:00
- `array` – [Array ](https://clickhouse.com/docs/en/data_types/array/ ).
2019-11-17 18:10:32 +00:00
**Returned values**
2020-02-02 22:39:00 +00:00
Returns an array containing the distinct elements.
2019-11-17 18:10:32 +00:00
**Example**
Query:
2019-01-30 10:39:46 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT arrayDistinct([1, 2, 2, 3, 1]);
2019-01-30 10:39:46 +00:00
```
2019-11-17 18:10:32 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
2019-02-11 14:17:43 +00:00
┌─arrayDistinct([1, 2, 2, 3, 1])─┐
│ [1,2,3] │
└────────────────────────────────┘
2019-01-30 10:39:46 +00:00
```
2024-04-04 08:52:11 +00:00
## arrayEnumerateDense
2019-09-30 22:26:30 +00:00
2019-11-25 11:30:38 +00:00
Returns an array of the same size as the source array, indicating where each element first appears in the source array.
2019-09-30 22:26:30 +00:00
2024-04-04 08:52:11 +00:00
**Syntax**
```sql
arrayEnumerateDense(arr)
```
**Example**
Query:
2019-09-30 22:26:30 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-09-30 22:26:30 +00:00
SELECT arrayEnumerateDense([10, 20, 10, 30])
```
2024-04-04 08:52:11 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
2019-09-30 22:26:30 +00:00
┌─arrayEnumerateDense([10, 20, 10, 30])─┐
│ [1,2,1,3] │
└───────────────────────────────────────┘
```
2024-04-04 08:52:11 +00:00
## arrayEnumerateDenseRanked
2024-04-04 12:13:22 +00:00
Returns an array the same size as the source array, indicating where each element first appears in the source array. It allows for enumeration of a multidimensional array with the ability to specify how deep to look inside the array.
2024-04-04 08:52:11 +00:00
**Syntax**
```sql
arrayEnumerateDenseRanked(clear_depth, arr, max_array_depth)
```
**Parameters**
2024-04-04 12:13:22 +00:00
- `clear_depth` : Enumerate elements at the specified level separately. Positive [Integer ](../data-types/int-uint.md ) less than or equal to `max_arr_depth` .
2024-04-04 17:16:53 +00:00
- `arr` : N-dimensional array to enumerate. [Array ](../data-types/array.md ).
2024-04-04 08:52:11 +00:00
- `max_array_depth` : The maximum effective depth. Positive [Integer ](../data-types/int-uint.md ) less than or equal to the depth of `arr` .
**Example**
2024-04-04 17:16:53 +00:00
With `clear_depth=1` and `max_array_depth=1` , the result is identical to what [arrayEnumerateDense ](#arrayenumeratedense ) would give.
2024-04-04 08:52:11 +00:00
Query:
``` sql
SELECT arrayEnumerateDenseRanked(1,[10, 20, 10, 30],1);
```
Result:
``` text
[1,2,1,3]
```
2024-04-04 17:16:53 +00:00
In this example, `arrayEnumerateDenseRanked` 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,`[10,10,30,20]`, the corresponding first row of the result is `[1,1,2,3]` , indicating that `10` is the first number encountered in position 1 and 2, `30` the second number encountered in position 3 and `20` is the third number encountered in position 4. For the second row, `[40, 50, 10, 30]` , the corresponding second row of the result is `[4,5,1,2]` , indicating that `40` and `50` are the fourth and fifth numbers encountered in position 1 and 2 of that row, that another `10` (the first encountered number) is in position 3 and `30` (the second number encountered) is in the last position.
2024-04-04 12:13:22 +00:00
Query:
``` sql
SELECT arrayEnumerateDenseRanked(1,[[10,10,30,20],[40,50,10,30]],2);
```
Result:
``` text
[[1,1,2,3],[4,5,1,2]]
```
2024-04-08 17:21:44 +00:00
Changing `clear_depth=2` results in the enumeration occurring separately for each row anew.
2024-04-04 08:52:11 +00:00
Query:
``` sql
2024-04-04 12:13:22 +00:00
SELECT arrayEnumerateDenseRanked(2,[[10,10,30,20],[40,50,10,30]],2);
2024-04-04 08:52:11 +00:00
```
Result:
``` text
2024-04-04 12:13:22 +00:00
[[1,1,2,3],[1,2,3,4]]
2024-04-04 08:52:11 +00:00
```
2019-01-30 10:39:46 +00:00
2022-06-02 10:55:18 +00:00
## arrayIntersect(arr)
2019-01-30 10:39:46 +00:00
2021-08-17 00:08:02 +00:00
Takes multiple arrays, returns an array with elements that are present in all source arrays.
2019-01-30 10:39:46 +00:00
2019-09-30 22:26:30 +00:00
Example:
2019-01-30 10:39:46 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-01-30 10:39:46 +00:00
SELECT
arrayIntersect([1, 2], [1, 3], [2, 3]) AS no_intersect,
arrayIntersect([1, 2], [1, 3], [1, 4]) AS intersect
```
2020-03-20 10:10:48 +00:00
``` text
2019-01-30 10:39:46 +00:00
┌─no_intersect─┬─intersect─┐
│ [] │ [1] │
└──────────────┴───────────┘
```
2023-07-02 09:09:52 +00:00
## arrayJaccardIndex
2023-07-03 18:33:59 +00:00
Returns the [Jaccard index ](https://en.wikipedia.org/wiki/Jaccard_index ) of two arrays.
2023-07-02 09:09:52 +00:00
**Example**
Query:
``` sql
SELECT arrayJaccardIndex([1, 2], [2, 3]) AS res
```
Result:
``` text
┌─res────────────────┐
│ 0.3333333333333333 │
└────────────────────┘
```
2022-06-02 10:55:18 +00:00
## arrayReduce
2019-09-30 22:26:30 +00:00
Applies an aggregate function to array elements and returns its result. The name of the aggregation function is passed as a string in single quotes `'max'` , `'sum'` . When using parametric aggregate functions, the parameter is indicated after the function name in parentheses `'uniqUpTo(6)'` .
2019-01-30 10:39:46 +00:00
2020-03-11 02:56:36 +00:00
**Syntax**
2019-09-30 22:26:30 +00:00
2020-04-30 18:19:18 +00:00
``` sql
2020-03-11 02:56:36 +00:00
arrayReduce(agg_func, arr1, arr2, ..., arrN)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2020-03-11 02:56:36 +00:00
2024-05-24 03:54:16 +00:00
- `agg_func` — The name of an aggregate function which should be a constant [string ](../data-types/string.md ).
- `arr` — Any number of [array ](../data-types/array.md ) type columns as the parameters of the aggregation function.
2020-03-11 02:56:36 +00:00
**Returned value**
**Example**
2020-06-29 09:48:18 +00:00
Query:
2020-04-30 18:19:18 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT arrayReduce('max', [1, 2, 3]);
2019-09-30 22:26:30 +00:00
```
2019-01-30 10:39:46 +00:00
2020-06-29 09:48:18 +00:00
Result:
2020-04-30 18:19:18 +00:00
``` text
2019-09-30 22:26:30 +00:00
┌─arrayReduce('max', [1, 2, 3])─┐
│ 3 │
└───────────────────────────────┘
```
2019-01-30 10:39:46 +00:00
2019-09-30 23:03:09 +00:00
If an aggregate function takes multiple arguments, then this function must be applied to multiple arrays of the same size.
2019-09-30 22:26:30 +00:00
2020-06-29 09:48:18 +00:00
Query:
2020-04-30 18:19:18 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT arrayReduce('maxIf', [3, 5], [1, 0]);
2019-09-30 22:26:30 +00:00
```
2020-06-29 09:48:18 +00:00
Result:
2020-04-30 18:19:18 +00:00
``` text
2019-09-30 22:26:30 +00:00
┌─arrayReduce('maxIf', [3, 5], [1, 0])─┐
│ 3 │
└──────────────────────────────────────┘
```
Example with a parametric aggregate function:
2020-06-29 09:48:18 +00:00
Query:
2020-04-30 18:19:18 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT arrayReduce('uniqUpTo(3)', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
2019-09-30 22:26:30 +00:00
```
2020-06-29 09:48:18 +00:00
Result:
2020-04-30 18:19:18 +00:00
``` text
2019-09-30 22:26:30 +00:00
┌─arrayReduce('uniqUpTo(3)', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])─┐
│ 4 │
└─────────────────────────────────────────────────────────────┘
2019-11-17 18:37:53 +00:00
```
2023-12-14 09:52:29 +00:00
**See also**
- [arrayFold ](#arrayfold )
2022-06-02 10:55:18 +00:00
## arrayReduceInRanges
2020-03-11 02:56:36 +00:00
Applies an aggregate function to array elements in given ranges and returns an array containing the result corresponding to each range. The function will return the same result as multiple `arrayReduce(agg_func, arraySlice(arr1, index, length), ...)` .
**Syntax**
2020-04-30 18:19:18 +00:00
``` sql
2020-03-11 02:56:36 +00:00
arrayReduceInRanges(agg_func, ranges, arr1, arr2, ..., arrN)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2020-03-11 02:56:36 +00:00
2024-05-24 03:54:16 +00:00
- `agg_func` — The name of an aggregate function which should be a constant [string ](../data-types/string.md ).
- `ranges` — The ranges to aggretate which should be an [array ](../data-types/array.md ) of [tuples ](../data-types/tuple.md ) which containing the index and the length of each range.
- `arr` — Any number of [Array ](../data-types/array.md ) type columns as the parameters of the aggregation function.
2020-03-11 02:56:36 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- Array containing results of the aggregate function over specified ranges. [Array ](../data-types/array.md ).
2020-06-29 09:48:18 +00:00
2020-03-11 02:56:36 +00:00
**Example**
2020-06-29 09:48:18 +00:00
Query:
2020-04-30 18:19:18 +00:00
``` sql
2020-03-11 02:56:36 +00:00
SELECT arrayReduceInRanges(
'sum',
[(1, 5), (2, 3), (3, 4), (4, 4)],
[1000000, 200000, 30000, 4000, 500, 60, 7]
) AS res
```
2020-06-29 09:48:18 +00:00
Result:
2020-04-30 18:19:18 +00:00
``` text
2020-03-11 02:56:36 +00:00
┌─res─────────────────────────┐
│ [1234500,234000,34560,4567] │
└─────────────────────────────┘
```
2023-12-14 09:52:29 +00:00
## arrayFold
Applies a lambda function to one or more equally-sized arrays and collects the result in an accumulator.
**Syntax**
``` sql
arrayFold(lambda_function, arr1, arr2, ..., accumulator)
```
**Example**
Query:
``` sql
SELECT arrayFold( acc,x -> acc + x*2, [1, 2, 3, 4], toInt64(3)) AS res;
```
Result:
``` text
┌─res─┐
│ 23 │
└─────┘
```
**Example with the Fibonacci sequence**
```sql
SELECT arrayFold( acc,x -> (acc.2, acc.2 + acc.1), range(number), (1::Int64, 0::Int64)).1 AS fibonacci
FROM numbers(1,10);
┌─fibonacci─┐
│ 0 │
│ 1 │
│ 1 │
│ 2 │
│ 3 │
│ 5 │
│ 8 │
│ 13 │
│ 21 │
│ 34 │
└───────────┘
```
**See also**
- [arrayReduce ](#arrayreduce )
2024-06-12 12:09:37 +00:00
## arrayReverse
2019-09-30 22:26:30 +00:00
Returns an array of the same size as the original array containing the elements in reverse order.
2024-06-12 12:09:37 +00:00
**Syntax**
```sql
arrayReverse(arr)
```
2019-09-30 22:26:30 +00:00
Example:
2020-03-20 10:10:48 +00:00
``` sql
2019-09-30 22:26:30 +00:00
SELECT arrayReverse([1, 2, 3])
```
2020-03-20 10:10:48 +00:00
``` text
2019-09-30 22:26:30 +00:00
┌─arrayReverse([1, 2, 3])─┐
│ [3,2,1] │
└─────────────────────────┘
```
2019-01-30 10:39:46 +00:00
2022-06-02 10:55:18 +00:00
## reverse(arr)
2019-01-30 10:39:46 +00:00
2020-05-06 06:13:29 +00:00
Synonym for [“arrayReverse” ](#arrayreverse )
2019-01-30 10:39:46 +00:00
2022-06-02 10:55:18 +00:00
## arrayFlatten
2019-12-03 10:11:13 +00:00
2019-12-24 14:35:17 +00:00
Converts an array of arrays to a flat array.
2019-12-03 10:11:13 +00:00
Function:
2023-04-17 18:10:41 +00:00
- Applies to any depth of nested arrays.
- Does not change arrays that are already flat.
2019-12-03 10:11:13 +00:00
The flattened array contains all the elements from all source arrays.
2019-12-11 14:15:43 +00:00
**Syntax**
2019-12-03 10:11:13 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-12-03 10:11:13 +00:00
flatten(array_of_arrays)
```
Alias: `flatten` .
2024-04-03 10:15:19 +00:00
**Parameters**
2019-12-03 10:11:13 +00:00
2024-05-24 03:54:16 +00:00
- `array_of_arrays` — [Array ](../data-types/array.md ) of arrays. For example, `[[1,2,3], [4,5]]` .
2019-12-03 10:11:13 +00:00
**Examples**
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT flatten([[[1]], [[2], [3]]]);
2019-12-03 10:11:13 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2019-12-03 10:11:13 +00:00
┌─flatten(array(array([1]), array([2], [3])))─┐
│ [1,2,3] │
└─────────────────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## arrayCompact
2019-10-22 02:23:07 +00:00
2019-11-26 12:46:18 +00:00
Removes consecutive duplicate elements from an array. The order of result values is determined by the order in the source array.
2019-10-22 02:23:07 +00:00
2019-11-25 11:30:38 +00:00
**Syntax**
2019-10-22 02:23:07 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-11-25 11:47:26 +00:00
arrayCompact(arr)
2019-10-22 02:23:07 +00:00
```
2021-02-15 21:22:10 +00:00
**Arguments**
2019-11-25 11:30:38 +00:00
2024-05-24 03:54:16 +00:00
`arr` — The [array ](../data-types/array.md ) to inspect.
2019-11-25 11:30:38 +00:00
**Returned value**
2024-05-23 13:48:20 +00:00
The array without duplicate. [Array ](../data-types/array.md ).
2019-11-25 11:30:38 +00:00
**Example**
Query:
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT arrayCompact([1, 1, nan, nan, 2, 3, 3, 3]);
2019-11-25 11:30:38 +00:00
```
Result:
2020-03-20 10:10:48 +00:00
``` text
2019-11-25 11:30:38 +00:00
┌─arrayCompact([1, 1, nan, nan, 2, 3, 3, 3])─┐
│ [1,nan,nan,2,3] │
└────────────────────────────────────────────┘
2019-10-22 02:23:07 +00:00
```
2022-06-02 10:55:18 +00:00
## arrayZip
2019-12-11 14:15:43 +00:00
2020-04-15 08:03:22 +00:00
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.
2019-12-11 14:15:43 +00:00
**Syntax**
2020-03-20 10:10:48 +00:00
``` sql
2019-12-11 14:15:43 +00:00
arrayZip(arr1, arr2, ..., arrN)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2019-12-11 14:15:43 +00:00
2024-05-24 03:54:16 +00:00
- `arrN` — [Array ](../data-types/array.md ).
2020-04-15 08:03:22 +00:00
The function can take any number of arrays of different types. All the input arrays must be of equal size.
2019-12-11 14:15:43 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- Array with elements from the source arrays grouped into [tuples ](../data-types/tuple.md ). Data types in the tuple are the same as types of the input arrays and in the same order as arrays are passed. [Array ](../data-types/array.md ).
2019-12-11 14:15:43 +00:00
**Example**
Query:
2020-03-20 10:10:48 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT arrayZip(['a', 'b', 'c'], [5, 2, 1]);
2019-12-11 14:15:43 +00:00
```
2024-09-02 03:55:41 +00:00
2019-12-11 14:15:43 +00:00
Result:
2020-03-20 10:10:48 +00:00
``` text
2020-04-15 08:03:22 +00:00
┌─arrayZip(['a', 'b', 'c'], [5, 2, 1])─┐
│ [('a',5),('b',2),('c',1)] │
└──────────────────────────────────────┘
2019-12-11 14:15:43 +00:00
```
2020-03-23 04:44:49 +00:00
2024-09-02 03:55:41 +00:00
## arrayZipUnaligned
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.
**Syntax**
``` sql
arrayZipUnaligned(arr1, arr2, ..., arrN)
```
**Arguments**
- `arrN` — [Array ](../data-types/array.md ).
The function can take any number of arrays of different types.
**Returned value**
- Array with elements from the source arrays grouped into [tuples ](../data-types/tuple.md ). Data types in the tuple are the same as types of the input arrays and in the same order as arrays are passed. [Array ](../data-types/array.md ). If the arrays have different sizes, the shorter arrays will be padded with `null` values.
**Example**
Query:
``` sql
SELECT arrayZipUnaligned(['a'], [1, 2, 3]);
```
Result:
``` text
┌─arrayZipUnaligned(['a'], [1, 2, 3])─┐
│ [('a',1),(NULL,2),(NULL,3)] │
└─────────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## arrayAUC
2020-04-30 18:19:18 +00:00
2023-04-17 18:10:41 +00:00
Calculate AUC (Area Under the Curve, which is a concept in machine learning, see more details: < https: / / en . wikipedia . org / wiki / Receiver_operating_characteristic # Area_under_the_curve > ).
2020-01-17 13:52:02 +00:00
2020-02-08 06:31:03 +00:00
**Syntax**
2020-04-30 18:19:18 +00:00
``` sql
2020-02-08 06:31:03 +00:00
arrayAUC(arr_scores, arr_labels)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2021-03-13 18:18:45 +00:00
2020-04-30 18:19:18 +00:00
- `arr_scores` — scores prediction model gives.
2023-06-02 13:27:56 +00:00
- `arr_labels` — labels of samples, usually 1 for positive sample and 0 for negative sample.
2020-01-17 13:52:02 +00:00
2020-02-08 06:31:03 +00:00
**Returned value**
2021-03-13 18:18:45 +00:00
2020-03-23 04:44:49 +00:00
Returns AUC value with type Float64.
2020-01-17 13:52:02 +00:00
2020-02-08 06:31:03 +00:00
**Example**
2021-03-13 18:18:45 +00:00
2020-02-08 06:31:03 +00:00
Query:
2020-04-30 18:19:18 +00:00
``` sql
2021-03-13 18:18:45 +00:00
select arrayAUC([0.1, 0.4, 0.35, 0.8], [0, 0, 1, 1]);
2020-02-08 06:31:03 +00:00
```
Result:
2020-04-30 18:19:18 +00:00
``` text
2020-02-08 06:31:03 +00:00
┌─arrayAUC([0.1, 0.4, 0.35, 0.8], [0, 0, 1, 1])─┐
│ 0.75 │
2020-09-08 15:55:00 +00:00
└───────────────────────────────────────────────┘
```
2024-05-23 11:54:45 +00:00
## arrayMap(func, arr1, ...)
2020-09-08 15:55:00 +00:00
2024-05-23 11:54:45 +00:00
Returns an array obtained from the original arrays by application of `func(arr1[i], ..., arrN[i])` for each element. Arrays `arr1` ... `arrN` must have the same number of elements.
2020-09-08 15:55:00 +00:00
Examples:
``` sql
SELECT arrayMap(x -> (x + 2), [1, 2, 3]) as res;
```
``` text
┌─res─────┐
│ [3,4,5] │
└─────────┘
```
The following example shows how to create a tuple of elements from different arrays:
``` sql
SELECT arrayMap((x, y) -> (x, y), [1, 2, 3], [4, 5, 6]) AS res
```
``` text
┌─res─────────────────┐
│ [(1,4),(2,5),(3,6)] │
└─────────────────────┘
```
Note that the `arrayMap` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You must pass a lambda function to it as the first argument, and it can’ t be omitted.
2024-05-23 11:54:45 +00:00
## arrayFilter(func, arr1, ...)
2020-09-08 15:55:00 +00:00
2024-05-23 11:54:45 +00:00
Returns an array containing only the elements in `arr1` for which `func(arr1[i], ..., arrN[i])` returns something other than 0.
2020-09-08 15:55:00 +00:00
Examples:
``` sql
SELECT arrayFilter(x -> x LIKE '%World%', ['Hello', 'abc World']) AS res
```
``` text
┌─res───────────┐
│ ['abc World'] │
└───────────────┘
```
``` sql
SELECT
arrayFilter(
(i, x) -> x LIKE '%World%',
arrayEnumerate(arr),
['Hello', 'abc World'] AS arr)
AS res
```
``` text
┌─res─┐
│ [2] │
└─────┘
```
Note that the `arrayFilter` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You must pass a lambda function to it as the first argument, and it can’ t be omitted.
2024-05-23 11:54:45 +00:00
## arrayFill(func, arr1, ...)
2020-09-08 15:55:00 +00:00
2024-05-23 11:54:45 +00:00
Scan through `arr1` from the first element to the last element and replace `arr1[i]` by `arr1[i - 1]` if `func(arr1[i], ..., arrN[i])` returns 0. The first element of `arr1` will not be replaced.
2020-09-08 15:55:00 +00:00
Examples:
``` sql
SELECT arrayFill(x -> not isNull(x), [1, null, 3, 11, 12, null, null, 5, 6, 14, null, null]) AS res
```
``` text
┌─res──────────────────────────────┐
│ [1,1,3,11,12,12,12,5,6,14,14,14] │
└──────────────────────────────────┘
```
Note that the `arrayFill` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You must pass a lambda function to it as the first argument, and it can’ t be omitted.
2024-05-23 11:54:45 +00:00
## arrayReverseFill(func, arr1, ...)
2020-09-08 15:55:00 +00:00
2024-05-23 11:54:45 +00:00
Scan through `arr1` from the last element to the first element and replace `arr1[i]` by `arr1[i + 1]` if `func(arr1[i], ..., arrN[i])` returns 0. The last element of `arr1` will not be replaced.
2020-09-08 15:55:00 +00:00
Examples:
``` sql
SELECT arrayReverseFill(x -> not isNull(x), [1, null, 3, 11, 12, null, null, 5, 6, 14, null, null]) AS res
```
``` text
┌─res────────────────────────────────┐
│ [1,3,3,11,12,5,5,5,6,14,NULL,NULL] │
└────────────────────────────────────┘
```
2021-04-02 10:23:11 +00:00
Note that the `arrayReverseFill` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You must pass a lambda function to it as the first argument, and it can’ t be omitted.
2020-09-08 15:55:00 +00:00
2024-05-23 11:54:45 +00:00
## arraySplit(func, arr1, ...)
2020-09-08 15:55:00 +00:00
2024-05-23 11:54:45 +00:00
Split `arr1` into multiple arrays. When `func(arr1[i], ..., arrN[i])` returns something other than 0, the array will be split on the left hand side of the element. The array will not be split before the first element.
2020-09-08 15:55:00 +00:00
Examples:
``` sql
SELECT arraySplit((x, y) -> y, [1, 2, 3, 4, 5], [1, 0, 0, 1, 0]) AS res
```
``` text
┌─res─────────────┐
│ [[1,2,3],[4,5]] │
└─────────────────┘
```
Note that the `arraySplit` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You must pass a lambda function to it as the first argument, and it can’ t be omitted.
2024-05-23 11:54:45 +00:00
## arrayReverseSplit(func, arr1, ...)
2020-09-08 15:55:00 +00:00
2024-05-23 11:54:45 +00:00
Split `arr1` into multiple arrays. When `func(arr1[i], ..., arrN[i])` returns something other than 0, the array will be split on the right hand side of the element. The array will not be split after the last element.
2020-09-08 15:55:00 +00:00
Examples:
``` sql
SELECT arrayReverseSplit((x, y) -> y, [1, 2, 3, 4, 5], [1, 0, 0, 1, 0]) AS res
```
``` text
┌─res───────────────┐
│ [[1],[2,3,4],[5]] │
└───────────────────┘
```
Note that the `arrayReverseSplit` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You must pass a lambda function to it as the first argument, and it can’ t be omitted.
2024-05-23 11:54:45 +00:00
## arrayExists(\[func,\] arr1, ...)
2020-09-08 15:55:00 +00:00
2024-05-23 11:54:45 +00:00
Returns 1 if there is at least one element in `arr` for which `func(arr1[i], ..., arrN[i])` returns something other than 0. Otherwise, it returns 0.
2020-09-08 15:55:00 +00:00
Note that the `arrayExists` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You can pass a lambda function to it as the first argument.
2024-05-23 11:54:45 +00:00
## arrayAll(\[func,\] arr1, ...)
2020-09-08 15:55:00 +00:00
2024-05-23 11:54:45 +00:00
Returns 1 if `func(arr1[i], ..., arrN[i])` returns something other than 0 for all the elements in arrays. Otherwise, it returns 0.
2020-09-08 15:55:00 +00:00
Note that the `arrayAll` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You can pass a lambda function to it as the first argument.
2024-05-23 11:54:45 +00:00
## arrayFirst(func, arr1, ...)
2020-09-08 15:55:00 +00:00
2024-05-23 11:54:45 +00:00
Returns the first element in the `arr1` array for which `func(arr1[i], ..., arrN[i])` returns something other than 0.
2020-09-08 15:55:00 +00:00
2024-04-04 12:53:24 +00:00
## arrayFirstOrNull
2024-05-23 11:54:45 +00:00
Returns the first element in the `arr1` array for which `func(arr1[i], ..., arrN[i])` returns something other than 0, otherwise it returns `NULL` .
2024-04-04 12:53:24 +00:00
**Syntax**
```sql
2024-05-23 11:54:45 +00:00
arrayFirstOrNull(func, arr1, ...)
2024-04-04 12:53:24 +00:00
```
**Parameters**
2024-04-04 18:00:28 +00:00
- `func` : Lambda function. [Lambda function ](../functions/#higher-order-functions---operator-and-lambdaparams-expr-function ).
- `arr1` : Array to operate on. [Array ](../data-types/array.md ).
2024-04-04 12:53:24 +00:00
**Returned value**
- The first element in the passed array.
- Otherwise, returns `NULL`
**Implementation details**
2024-04-04 13:49:03 +00:00
Note that the `arrayFirstOrNull` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You must pass a lambda function to it as the first argument, and it can’ t be omitted.
2020-09-08 15:55:00 +00:00
2024-04-04 12:53:24 +00:00
**Example**
Query:
2024-04-04 13:49:03 +00:00
```sql
SELECT arrayFirstOrNull(x -> x >= 2, [1, 2, 3]);
```
Result:
```response
2
```
Query:
2024-04-04 12:53:24 +00:00
```sql
SELECT arrayFirstOrNull(x -> x >= 2, emptyArrayUInt8());
```
Result:
```response
\N
```
2024-04-10 15:53:31 +00:00
Query:
```sql
SELECT arrayLastOrNull((x,f) -> f, [1,2,3,NULL], [0,1,0,1]);
```
Result:
```response
\N
```
2024-05-23 11:54:45 +00:00
## arrayLast(func, arr1, ...)
2022-01-07 16:00:37 +00:00
2024-05-23 11:54:45 +00:00
Returns the last element in the `arr1` array for which `func(arr1[i], ..., arrN[i])` returns something other than 0.
2022-01-07 16:00:37 +00:00
Note that the `arrayLast` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You must pass a lambda function to it as the first argument, and it can’ t be omitted.
2024-04-04 12:53:24 +00:00
## arrayLastOrNull
2024-05-23 11:54:45 +00:00
Returns the last element in the `arr1` array for which `func(arr1[i], ..., arrN[i])` returns something other than 0, otherwise returns `NULL` .
2024-04-04 12:53:24 +00:00
2024-04-04 13:49:03 +00:00
**Syntax**
```sql
2024-05-23 11:54:45 +00:00
arrayLastOrNull(func, arr1, ...)
2024-04-04 13:49:03 +00:00
```
**Parameters**
2024-04-04 18:00:28 +00:00
- `func` : Lambda function. [Lambda function ](../functions/#higher-order-functions---operator-and-lambdaparams-expr-function ).
- `arr1` : Array to operate on. [Array ](../data-types/array.md ).
2024-04-04 13:49:03 +00:00
**Returned value**
- The last element in the passed array.
- Otherwise, returns `NULL`
**Implementation details**
Note that the `arrayLastOrNull` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You must pass a lambda function to it as the first argument, and it can’ t be omitted.
**Example**
Query:
```sql
SELECT arrayLastOrNull(x -> x >= 2, [1, 2, 3]);
```
Result:
```response
3
```
Query:
```sql
SELECT arrayLastOrNull(x -> x >= 2, emptyArrayUInt8());
```
Result:
```response
\N
```
2024-04-04 12:53:24 +00:00
2024-05-23 11:54:45 +00:00
## arrayFirstIndex(func, arr1, ...)
2020-09-08 15:55:00 +00:00
2024-05-23 11:54:45 +00:00
Returns the index of the first element in the `arr1` array for which `func(arr1[i], ..., arrN[i])` returns something other than 0.
2020-09-08 15:55:00 +00:00
Note that the `arrayFirstIndex` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You must pass a lambda function to it as the first argument, and it can’ t be omitted.
2024-05-23 11:54:45 +00:00
## arrayLastIndex(func, arr1, ...)
2022-01-08 08:39:05 +00:00
2024-05-23 11:54:45 +00:00
Returns the index of the last element in the `arr1` array for which `func(arr1[i], ..., arrN[i])` returns something other than 0.
2022-01-08 08:39:05 +00:00
Note that the `arrayLastIndex` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You must pass a lambda function to it as the first argument, and it can’ t be omitted.
2022-06-02 10:55:18 +00:00
## arrayMin
2020-12-16 15:47:30 +00:00
2021-04-02 10:23:11 +00:00
Returns the minimum of elements in the source array.
2021-01-28 22:54:21 +00:00
2021-01-29 02:08:22 +00:00
If the `func` function is specified, returns the mininum of elements converted by this function.
2020-12-16 15:47:30 +00:00
Note that the `arrayMin` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You can pass a lambda function to it as the first argument.
2021-01-28 22:54:21 +00:00
**Syntax**
2021-01-07 12:57:39 +00:00
```sql
2021-01-29 02:41:03 +00:00
arrayMin([func,] arr)
2021-01-28 22:54:21 +00:00
```
2021-02-15 21:22:10 +00:00
**Arguments**
2021-01-28 22:54:21 +00:00
2024-05-24 03:54:16 +00:00
- `func` — Function. [Expression ](../data-types/special-data-types/expression.md ).
- `arr` — Array. [Array ](../data-types/array.md ).
2021-01-28 22:54:21 +00:00
**Returned value**
2023-04-17 18:10:41 +00:00
- The minimum of function values (or the array minimum).
2021-01-28 22:54:21 +00:00
2024-05-23 13:48:20 +00:00
:::note
If `func` is specified, then the return type matches the return value type of `func` , otherwise it matches the type of the array elements.
2024-05-23 15:07:55 +00:00
:::
2021-01-28 22:54:21 +00:00
**Examples**
Query:
```sql
SELECT arrayMin([1, 2, 4]) AS res;
```
Result:
```text
2021-01-07 12:57:39 +00:00
┌─res─┐
│ 1 │
└─────┘
2021-01-28 22:54:21 +00:00
```
2021-01-07 12:57:39 +00:00
2021-01-28 22:54:21 +00:00
Query:
2021-01-07 12:57:39 +00:00
2021-01-29 07:19:01 +00:00
```sql
2021-01-28 22:54:21 +00:00
SELECT arrayMin(x -> (-x), [1, 2, 4]) AS res;
```
Result:
```text
2021-01-07 12:57:39 +00:00
┌─res─┐
│ -4 │
└─────┘
```
2022-06-02 10:55:18 +00:00
## arrayMax
2021-01-28 22:54:21 +00:00
2021-04-02 10:23:11 +00:00
Returns the maximum of elements in the source array.
2020-12-16 15:47:30 +00:00
2021-01-28 22:54:21 +00:00
If the `func` function is specified, returns the maximum of elements converted by this function.
2020-12-16 15:47:30 +00:00
Note that the `arrayMax` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You can pass a lambda function to it as the first argument.
2021-01-28 22:54:21 +00:00
**Syntax**
```sql
arrayMax([func,] arr)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2021-01-28 22:54:21 +00:00
2024-05-24 03:54:16 +00:00
- `func` — Function. [Expression ](../data-types/special-data-types/expression.md ).
- `arr` — Array. [Array ](../data-types/array.md ).
2021-01-28 22:54:21 +00:00
**Returned value**
2023-04-17 18:10:41 +00:00
- The maximum of function values (or the array maximum).
2021-01-28 22:54:21 +00:00
2024-05-23 13:48:20 +00:00
:::note
if `func` is specified then the return type matches the return value type of `func` , otherwise it matches the type of the array elements.
:::
2021-01-28 22:54:21 +00:00
**Examples**
Query:
2021-01-07 12:57:39 +00:00
```sql
2021-01-28 22:54:21 +00:00
SELECT arrayMax([1, 2, 4]) AS res;
```
2021-01-29 02:08:22 +00:00
2021-01-28 22:54:21 +00:00
Result:
```text
2021-01-07 12:57:39 +00:00
┌─res─┐
│ 4 │
└─────┘
2021-01-28 22:54:21 +00:00
```
2021-01-07 12:57:39 +00:00
2021-01-28 22:54:21 +00:00
Query:
2021-01-07 12:57:39 +00:00
2021-01-28 22:54:21 +00:00
```sql
SELECT arrayMax(x -> (-x), [1, 2, 4]) AS res;
```
Result:
```text
2021-01-07 12:57:39 +00:00
┌─res─┐
│ -1 │
└─────┘
```
2022-06-02 10:55:18 +00:00
## arraySum
2021-01-28 22:54:21 +00:00
2021-04-02 10:23:11 +00:00
Returns the sum of elements in the source array.
2020-09-08 15:55:00 +00:00
2021-01-28 22:54:21 +00:00
If the `func` function is specified, returns the sum of elements converted by this function.
2020-09-08 15:55:00 +00:00
Note that the `arraySum` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You can pass a lambda function to it as the first argument.
2021-01-28 22:54:21 +00:00
**Syntax**
2021-01-07 12:57:39 +00:00
```sql
2021-01-29 02:41:03 +00:00
arraySum([func,] arr)
2021-01-28 22:54:21 +00:00
```
2021-02-15 21:22:10 +00:00
**Arguments**
2021-01-28 22:54:21 +00:00
2024-05-24 03:54:16 +00:00
- `func` — Function. [Expression ](../data-types/special-data-types/expression.md ).
- `arr` — Array. [Array ](../data-types/array.md ).
2021-01-28 22:54:21 +00:00
**Returned value**
2023-04-17 18:10:41 +00:00
- The sum of the function values (or the array sum).
2021-01-28 22:54:21 +00:00
2024-05-23 13:48:20 +00:00
:::note
Return type:
2024-05-24 03:54:16 +00:00
- For decimal numbers in the source array (or for converted values, if `func` is specified) — [Decimal128 ](../data-types/decimal.md ).
- For floating point numbers — [Float64 ](../data-types/float.md ).
- For numeric unsigned — [UInt64 ](../data-types/int-uint.md ).
- For numeric signed — [Int64 ](../data-types/int-uint.md ).
2024-05-23 13:48:20 +00:00
:::
2021-01-28 22:54:21 +00:00
**Examples**
Query:
```sql
2021-01-29 02:41:03 +00:00
SELECT arraySum([2, 3]) AS res;
2021-01-28 22:54:21 +00:00
```
Result:
```text
2021-01-07 12:57:39 +00:00
┌─res─┐
│ 5 │
└─────┘
2021-01-28 22:54:21 +00:00
```
2021-01-07 12:57:39 +00:00
2021-01-28 22:54:21 +00:00
Query:
```sql
SELECT arraySum(x -> x*x, [2, 3]) AS res;
```
2021-01-07 12:57:39 +00:00
2021-01-28 22:54:21 +00:00
Result:
```text
2021-01-07 12:57:39 +00:00
┌─res─┐
│ 13 │
└─────┘
```
2022-06-02 10:55:18 +00:00
## arrayAvg
2021-01-07 12:57:39 +00:00
2021-04-02 10:23:11 +00:00
Returns the average of elements in the source array.
2020-12-16 15:47:30 +00:00
2021-01-28 22:54:21 +00:00
If the `func` function is specified, returns the average of elements converted by this function.
2020-12-16 15:47:30 +00:00
Note that the `arrayAvg` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You can pass a lambda function to it as the first argument.
2021-01-28 22:54:21 +00:00
**Syntax**
```sql
arrayAvg([func,] arr)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2021-01-28 22:54:21 +00:00
2024-05-24 03:54:16 +00:00
- `func` — Function. [Expression ](../data-types/special-data-types/expression.md ).
- `arr` — Array. [Array ](../data-types/array.md ).
2021-01-28 22:54:21 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- The average of function values (or the array average). [Float64 ](../data-types/float.md ).
2021-01-28 22:54:21 +00:00
**Examples**
Query:
```sql
SELECT arrayAvg([1, 2, 4]) AS res;
```
Result:
```text
┌────────────────res─┐
│ 2.3333333333333335 │
└────────────────────┘
```
Query:
```sql
SELECT arrayAvg(x -> (x * x), [2, 4]) AS res;
```
Result:
```text
┌─res─┐
│ 10 │
└─────┘
```
2024-05-23 11:54:45 +00:00
## arrayCumSum(\[func,\] arr1, ...)
2020-09-08 15:55:00 +00:00
2024-05-23 11:54:45 +00:00
Returns an array of the partial (running) sums of the elements in the source array `arr1` . If `func` is specified, then the sum is computed from applying `func` to `arr1` , `arr2` , ..., `arrN` , i.e. `func(arr1[i], ..., arrN[i])` .
2020-09-08 15:55:00 +00:00
2023-04-17 18:10:41 +00:00
**Syntax**
``` sql
arrayCumSum(arr)
```
**Arguments**
2024-05-24 03:54:16 +00:00
- `arr` — [Array ](../data-types/array.md ) of numeric values.
2023-04-17 18:10:41 +00:00
**Returned value**
2024-05-23 13:48:20 +00:00
- Returns an array of the partial sums of the elements in the source array. [UInt\* ](https://clickhouse.com/docs/en/data_types/int_uint/#uint-ranges ), [Int\* ](https://clickhouse.com/docs/en/data_types/int_uint/#int-ranges ), [Float\* ](https://clickhouse.com/docs/en/data_types/float/ ).
2020-09-08 15:55:00 +00:00
Example:
``` sql
SELECT arrayCumSum([1, 1, 1, 1]) AS res
```
``` text
┌─res──────────┐
│ [1, 2, 3, 4] │
└──────────────┘
```
Note that the `arrayCumSum` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You can pass a lambda function to it as the first argument.
2024-05-23 11:54:45 +00:00
## arrayCumSumNonNegative(\[func,\] arr1, ...)
2020-09-08 15:55:00 +00:00
2024-05-23 11:54:45 +00:00
Same as `arrayCumSum` , returns an array of the partial (running) sums of the elements in the source array. If `func` is specified, then the sum is computed from applying `func` to `arr1` , `arr2` , ..., `arrN` , i.e. `func(arr1[i], ..., arrN[i])` . Unlike `arrayCumSum` , if the current running sum is smaller than `0` , it is replaced by `0` .
2020-09-08 15:55:00 +00:00
2023-04-17 18:10:41 +00:00
**Syntax**
``` sql
arrayCumSumNonNegative(arr)
```
**Arguments**
2024-05-24 03:54:16 +00:00
- `arr` — [Array ](../data-types/array.md ) of numeric values.
2023-04-17 18:10:41 +00:00
**Returned value**
2024-05-23 13:48:20 +00:00
- Returns an array of non-negative partial sums of elements in the source array. [UInt\* ](https://clickhouse.com/docs/en/data_types/int_uint/#uint-ranges ), [Int\* ](https://clickhouse.com/docs/en/data_types/int_uint/#int-ranges ), [Float\* ](https://clickhouse.com/docs/en/data_types/float/ ).
2020-09-08 15:55:00 +00:00
``` sql
SELECT arrayCumSumNonNegative([1, 1, -4, 1]) AS res
```
``` text
┌─res───────┐
│ [1,2,0,1] │
└───────────┘
2020-02-08 06:31:03 +00:00
```
2023-04-17 18:10:41 +00:00
2020-09-08 15:55:00 +00:00
Note that the `arraySumNonNegative` is a [higher-order function ](../../sql-reference/functions/index.md#higher-order-functions ). You can pass a lambda function to it as the first argument.
2019-12-11 14:15:43 +00:00
2022-06-02 10:55:18 +00:00
## arrayProduct
2021-05-09 23:25:54 +00:00
2024-05-24 03:54:16 +00:00
Multiplies elements of an [array ](../data-types/array.md ).
2021-05-09 23:25:54 +00:00
**Syntax**
``` sql
arrayProduct(arr)
```
**Arguments**
2024-05-24 03:54:16 +00:00
- `arr` — [Array ](../data-types/array.md ) of numeric values.
2021-05-09 23:25:54 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- A product of array's elements. [Float64 ](../data-types/float.md ).
2021-05-09 23:25:54 +00:00
**Examples**
Query:
``` sql
SELECT arrayProduct([1,2,3,4,5,6]) as res;
```
Result:
``` text
┌─res───┐
│ 720 │
└───────┘
```
Query:
``` sql
2021-05-11 14:56:57 +00:00
SELECT arrayProduct([toDecimal64(1,8), toDecimal64(2,8), toDecimal64(3,8)]) as res, toTypeName(res);
2021-05-09 23:25:54 +00:00
```
2024-05-24 03:54:16 +00:00
Return value type is always [Float64 ](../data-types/float.md ). Result:
2021-05-09 23:25:54 +00:00
``` text
2021-05-11 14:56:57 +00:00
┌─res─┬─toTypeName(arrayProduct(array(toDecimal64(1, 8), toDecimal64(2, 8), toDecimal64(3, 8))))─┐
│ 6 │ Float64 │
└─────┴──────────────────────────────────────────────────────────────────────────────────────────┘
2021-05-10 16:13:16 +00:00
```
2022-07-13 10:29:13 +00:00
2023-08-16 19:54:54 +00:00
## arrayRotateLeft
2024-05-24 03:54:16 +00:00
Rotates an [array ](../data-types/array.md ) to the left by the specified number of elements.
2023-08-16 19:54:54 +00:00
If the number of elements is negative, the array is rotated to the right.
**Syntax**
``` sql
arrayRotateLeft(arr, n)
```
**Arguments**
2024-05-24 03:54:16 +00:00
- `arr` — [Array ](../data-types/array.md ).
2023-08-16 19:54:54 +00:00
- `n` — Number of elements to rotate.
**Returned value**
2024-05-24 03:54:16 +00:00
- An array rotated to the left by the specified number of elements. [Array ](../data-types/array.md ).
2023-08-16 19:54:54 +00:00
**Examples**
Query:
``` sql
SELECT arrayRotateLeft([1,2,3,4,5,6], 2) as res;
```
Result:
``` text
┌─res───────────┐
│ [3,4,5,6,1,2] │
└───────────────┘
```
Query:
``` sql
SELECT arrayRotateLeft([1,2,3,4,5,6], -2) as res;
```
Result:
``` text
┌─res───────────┐
│ [5,6,1,2,3,4] │
└───────────────┘
```
Query:
``` sql
SELECT arrayRotateLeft(['a','b','c','d','e'], 3) as res;
```
Result:
``` text
┌─res───────────────────┐
│ ['d','e','a','b','c'] │
└───────────────────────┘
```
## arrayRotateRight
2024-05-24 03:54:16 +00:00
Rotates an [array ](../data-types/array.md ) to the right by the specified number of elements.
2023-08-16 19:54:54 +00:00
If the number of elements is negative, the array is rotated to the left.
**Syntax**
``` sql
arrayRotateRight(arr, n)
```
**Arguments**
2024-05-24 03:54:16 +00:00
- `arr` — [Array ](../data-types/array.md ).
2023-08-16 19:54:54 +00:00
- `n` — Number of elements to rotate.
**Returned value**
2024-05-24 03:54:16 +00:00
- An array rotated to the right by the specified number of elements. [Array ](../data-types/array.md ).
2023-08-16 19:54:54 +00:00
**Examples**
Query:
``` sql
SELECT arrayRotateRight([1,2,3,4,5,6], 2) as res;
```
Result:
``` text
┌─res───────────┐
│ [5,6,1,2,3,4] │
└───────────────┘
```
Query:
``` sql
SELECT arrayRotateRight([1,2,3,4,5,6], -2) as res;
```
Result:
``` text
┌─res───────────┐
│ [3,4,5,6,1,2] │
└───────────────┘
```
Query:
``` sql
SELECT arrayRotateRight(['a','b','c','d','e'], 3) as res;
```
Result:
``` text
┌─res───────────────────┐
│ ['c','d','e','a','b'] │
└───────────────────────┘
```
## arrayShiftLeft
2024-05-24 03:54:16 +00:00
Shifts an [array ](../data-types/array.md ) to the left by the specified number of elements.
2023-08-16 19:54:54 +00:00
New elements are filled with the provided argument or the default value of the array element type.
If the number of elements is negative, the array is shifted to the right.
**Syntax**
``` sql
arrayShiftLeft(arr, n[, default])
```
**Arguments**
2024-05-24 03:54:16 +00:00
- `arr` — [Array ](../data-types/array.md ).
2023-08-16 19:54:54 +00:00
- `n` — Number of elements to shift.
- `default` — Optional. Default value for new elements.
**Returned value**
2024-05-24 03:54:16 +00:00
- An array shifted to the left by the specified number of elements. [Array ](../data-types/array.md ).
2023-08-16 19:54:54 +00:00
**Examples**
Query:
``` sql
SELECT arrayShiftLeft([1,2,3,4,5,6], 2) as res;
```
Result:
``` text
┌─res───────────┐
│ [3,4,5,6,0,0] │
└───────────────┘
```
Query:
``` sql
SELECT arrayShiftLeft([1,2,3,4,5,6], -2) as res;
```
Result:
``` text
┌─res───────────┐
│ [0,0,1,2,3,4] │
└───────────────┘
```
Query:
``` sql
SELECT arrayShiftLeft([1,2,3,4,5,6], 2, 42) as res;
```
Result:
``` text
┌─res─────────────┐
│ [3,4,5,6,42,42] │
└─────────────────┘
```
Query:
``` sql
SELECT arrayShiftLeft(['a','b','c','d','e','f'], 3, 'foo') as res;
```
Result:
``` text
┌─res─────────────────────────────┐
│ ['d','e','f','foo','foo','foo'] │
└─────────────────────────────────┘
```
Query:
``` sql
SELECT arrayShiftLeft([1,2,3,4,5,6] :: Array(UInt16), 2, 4242) as res;
```
Result:
``` text
┌─res─────────────────┐
│ [3,4,5,6,4242,4242] │
└─────────────────────┘
```
## arrayShiftRight
2024-05-24 03:54:16 +00:00
Shifts an [array ](../data-types/array.md ) to the right by the specified number of elements.
2023-08-16 19:54:54 +00:00
New elements are filled with the provided argument or the default value of the array element type.
If the number of elements is negative, the array is shifted to the left.
**Syntax**
``` sql
arrayShiftRight(arr, n[, default])
```
**Arguments**
2024-05-24 03:54:16 +00:00
- `arr` — [Array ](../data-types/array.md ).
2023-08-16 19:54:54 +00:00
- `n` — Number of elements to shift.
- `default` — Optional. Default value for new elements.
**Returned value**
2024-05-24 03:54:16 +00:00
- An array shifted to the right by the specified number of elements. [Array ](../data-types/array.md ).
2023-08-16 19:54:54 +00:00
**Examples**
Query:
``` sql
SELECT arrayShiftRight([1,2,3,4,5,6], 2) as res;
```
Result:
``` text
┌─res───────────┐
│ [0,0,1,2,3,4] │
└───────────────┘
```
Query:
``` sql
SELECT arrayShiftRight([1,2,3,4,5,6], -2) as res;
```
Result:
``` text
┌─res───────────┐
│ [3,4,5,6,0,0] │
└───────────────┘
```
Query:
``` sql
SELECT arrayShiftRight([1,2,3,4,5,6], 2, 42) as res;
```
Result:
``` text
┌─res─────────────┐
│ [42,42,1,2,3,4] │
└─────────────────┘
```
Query:
``` sql
SELECT arrayShiftRight(['a','b','c','d','e','f'], 3, 'foo') as res;
```
Result:
``` text
┌─res─────────────────────────────┐
│ ['foo','foo','foo','a','b','c'] │
└─────────────────────────────────┘
```
Query:
``` sql
SELECT arrayShiftRight([1,2,3,4,5,6] :: Array(UInt16), 2, 4242) as res;
```
Result:
``` text
┌─res─────────────────┐
│ [4242,4242,1,2,3,4] │
└─────────────────────┘
```
2023-11-07 10:16:13 +00:00
## arrayRandomSample
2023-11-07 10:25:27 +00:00
Function `arrayRandomSample` returns a subset with `samples` -many random elements of an input array. If `samples` exceeds the size of the input array, the sample size is limited to the size of the array, i.e. all array elements are returned but their order is not guaranteed. The function can handle both flat arrays and nested arrays.
2023-11-07 10:16:13 +00:00
**Syntax**
```sql
arrayRandomSample(arr, samples)
```
**Arguments**
2023-11-07 10:25:27 +00:00
- `arr` — The input array from which to sample elements. ([Array(T)](../data-types/array.md))
- `samples` — The number of elements to include in the random sample ([UInt*](../data-types/int-uint.md))
2023-11-07 10:16:13 +00:00
**Returned Value**
2024-05-23 13:48:20 +00:00
- An array containing a random sample of elements from the input array. [Array ](../data-types/array.md ).
2023-11-07 10:25:27 +00:00
2023-11-07 10:16:13 +00:00
**Examples**
Query:
```sql
SELECT arrayRandomSample(['apple', 'banana', 'cherry', 'date'], 2) as res;
```
Result:
2023-11-07 10:25:27 +00:00
2023-11-07 10:16:13 +00:00
```
┌─res────────────────┐
2023-11-07 10:25:27 +00:00
│ ['cherry','apple'] │
2023-11-07 10:16:13 +00:00
└────────────────────┘
```
Query:
```sql
SELECT arrayRandomSample([[1, 2], [3, 4], [5, 6]], 2) as res;
```
Result:
2023-11-07 10:25:27 +00:00
2023-11-07 10:16:13 +00:00
```
┌─res───────────┐
│ [[3,4],[5,6]] │
└───────────────┘
```
Query:
```sql
SELECT arrayRandomSample([1, 2, 3], 5) as res;
```
Result:
2023-11-07 10:25:27 +00:00
2023-11-07 10:16:13 +00:00
```
┌─res─────┐
│ [3,1,2] │
└─────────┘
```
2022-07-13 10:29:13 +00:00
## Distance functions
2024-07-12 09:28:41 +00:00
All supported functions are described in [distance functions documentation ](../../sql-reference/functions/distance-functions.md ).