Array functions

**Returned value**

Returns an array from the lambda results [`Array(T)`](/docs/sql-reference/data-types/array)

**Examples**

**Usage example**

```
SELECT arrayMap(x -> (x + 2), [1, 2, 3]) as res;
```

```
[3, 4, 5]
```

**Creating a tuple of elements from different arrays**

```
SELECT arrayMap((x, y) -> (x, y), [1, 2, 3], [4, 5, 6]) AS res
```

```
[(1, 4),(2, 5),(3, 6)]
```

## arrayMax[​](#arrayMax "Direct link to arrayMax")

Introduced in: v21.1.0

Returns the maximum element in the source array.

If a lambda function `func` is specified, returns the maximum element of the lambda results.

**Syntax**

```
arrayMax([func(x[, y1, ..., yN])], source_arr[, cond1_arr, ... , condN_arr])
```

**Arguments**

- `func(x[, y1, ..., yN])` — Optional. A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [`Lambda function`](/docs/sql-reference/functions/overview#arrow-operator-and-lambda)
- `source_arr` — The source array to process. [`Array(T)`](/docs/sql-reference/data-types/array)
- `[, cond1_arr, ... , condN_arr]` — Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/docs/sql-reference/data-types/array)

**Returned value**

Returns the maximum element in the source array, or the maximum element of the lambda results if provided.

**Examples**

**Basic example**

```
SELECT arrayMax([5, 3, 2, 7]);
```

```
7
```

**Usage with lambda function**

```
SELECT arrayMax(x, y -> x/y, [4, 8, 12, 16], [1, 2, 1, 2]);
```

```
12
```

## arrayMin[​](#arrayMin "Direct link to arrayMin")

Introduced in: v21.1.0

Returns the minimum element in the source array.

If a lambda function `func` is specified, returns the minimum element of the lambda results.

**Syntax**

```
arrayMin([func(x[, y1, ..., yN])], source_arr[, cond1_arr, ... , condN_arr])
```

**Arguments**

- `func(x[, y1, ..., yN])` — Optional. A lambda function which operates on elements of the source array (`x`) and condition arrays (`y`). [`Lambda function`](/docs/sql-reference/functions/overview#arrow-operator-and-lambda)
- `source_arr` — The source array to process. [`Array(T)`](/docs/sql-reference/data-types/array)
- `cond1_arr, ...` — Optional. N condition arrays providing additional arguments to the lambda function. [`Array(T)`](/docs/sql-reference/data-types/array)

**Returned value**

Returns the minimum element in the source array, or the minimum element of the lambda results if provided.

**Examples**

**Basic example**

```
SELECT arrayMin([5, 3, 2, 7]);
```

```
2
```

**Usage with lambda function**

```
SELECT arrayMin(x, y -> x/y, [4, 8, 12, 16], [1, 2, 1, 2]);
```

```
4
```

## arrayNormalizedGini[​](#arrayNormalizedGini "Direct link to arrayNormalizedGini")

Introduced in: v25.1.0

Calculates the normalized Gini coefficient.

**Syntax**

```
arrayNormalizedGini(predicted, label)
```

**Arguments**

- `predicted` — The predicted value. [`Array(T)`](/docs/sql-reference/data-types/array)
- `label` — The actual value. [`Array(T)`](/docs/sql-reference/data-types/array)

**Returned value**

A tuple containing the Gini coefficients of the predicted values, the Gini coefficient of the normalized values, and the normalized Gini coefficient (= the ratio of the former two Gini coefficients) [`Tuple(Float64, Float64, Float64)`](/docs/sql-reference/data-types/tuple)

**Examples**

**Usage example**

```
SELECT arrayNormalizedGini([0.9, 0.3, 0.8, 0.7],[6, 1, 0, 2]);
```

```
(0.18055555555555558, 0.2638888888888889, 0.6842105263157896)
```

## arrayPartialReverseSort[​](#arrayPartialReverseSort "Direct link to arrayPartialReverseSort")

Introduced in: v23.2.0

This function is the same as `arrayReverseSort` but with an additional `limit` argument allowing partial sorting.

Tip

To retain only the sorted elements use `arrayResize`.

**Syntax**

```
arrayPartialReverseSort([f,] arr [, arr1, ... ,arrN], limit)
```

**Arguments**

- `f(arr[, arr1, ... ,arrN])` — The lambda function to apply to elements of array `x`. [`Lambda function`](/docs/sql-reference/functions/overview#arrow-operator-and-lambda)
- `arr` — Array to be sorted. [`Array(T)`](/docs/sql-reference/data-types/array)
- `arr1, ... ,arrN` — N additional arrays, in the case when `f` accepts multiple arguments. [`Array(T)`](/docs/sql-reference/data-types/array)
- `limit` — Index value up until which sorting will occur. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns an array of the same size as the original array where elements in the range `[1..limit]` are sorted
in descending order. The remaining elements `(limit..N]` are in an unspecified order.

**Examples**

**simple\_int**

```
SELECT arrayPartialReverseSort(2, [5, 9, 1, 3])
```

```
[9, 5, 1, 3]
```

**simple\_string**

```
SELECT arrayPartialReverseSort(2, ['expenses','lasso','embolism','gladly'])
```

```
['lasso','gladly','expenses','embolism']
```

**retain\_sorted**

```
SELECT arrayResize(arrayPartialReverseSort(2, [5, 9, 1, 3]), 2)
```

```
[9, 5]
```

**lambda\_simple**

```
SELECT arrayPartialReverseSort((x) -> -x, 2, [5, 9, 1, 3])
```

```
[1, 3, 5, 9]
```

**lambda\_complex**

```
SELECT arrayPartialReverseSort((x, y) -> -y, 1, [0, 1, 2], [1, 2, 3]) as res
```

```
[0, 1, 2]
```

## arrayPartialShuffle[​](#arrayPartialShuffle "Direct link to arrayPartialShuffle")

Introduced in: v23.2.0

Returns an array of the same size as the original array where elements in range `[1..limit]` are a random
subset of the original array. Remaining `(limit..n]` shall contain the elements not in `[1..limit]` range in undefined order.
Value of limit shall be in range `[1..n]`. Values outside of that range are equivalent to performing full `arrayShuffle`:

Note

This function will not materialize constants.

The value of `limit` should be in the range `[1..N]`. Values outside of that range are equivalent to performing full [`arrayShuffle`](#arrayShuffle).

**Syntax**

```
arrayPartialShuffle(arr [, limit[, seed]])
```

**Arguments**

- `arr` — The array to shuffle. [`Array(T)`](/docs/sql-reference/data-types/array)
- `seed` — Optional. The seed to be used with random number generation. If not provided, a random one is used. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)
- `limit` — Optional. The number to limit element swaps to, in the range `[1..N]`. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Array with elements partially shuffled. [`Array(T)`](/docs/sql-reference/data-types/array)

**Examples**

**no\_limit1**

```
SELECT arrayPartialShuffle([1, 2, 3, 4], 0)
```

```
[2, 4, 3, 1]
```

**no\_limit2**

```
SELECT arrayPartialShuffle([1, 2, 3, 4])
```

```
[4, 1, 3, 2]
```

**random\_seed**

```
SELECT arrayPartialShuffle([1, 2, 3, 4], 2)
```

```
[3, 4, 1, 2]
```

**explicit\_seed**

```
SELECT arrayPartialShuffle([1, 2, 3, 4], 2, 41)
```

```
[3, 2, 1, 4]
```

**materialize**

```
SELECT arrayPartialShuffle(materialize([1, 2, 3, 4]), 2, 42), arrayPartialShuffle([1, 2, 3], 2, 42) FROM numbers(10)
```

```
┌─arrayPartial⋯4]), 2, 42)─┬─arrayPartial⋯ 3], 2, 42)─┐
│ [3,2,1,4]                │ [3,2,1]                  │
│ [3,2,1,4]                │ [3,2,1]                  │
│ [4,3,2,1]                │ [3,2,1]                  │
│ [1,4,3,2]                │ [3,2,1]                  │
│ [3,4,1,2]                │ [3,2,1]                  │
│ [1,2,3,4]                │ [3,2,1]                  │
│ [1,4,3,2]                │ [3,2,1]                  │
│ [1,4,3,2]                │ [3,2,1]                  │
│ [3,1,2,4]                │ [3,2,1]                  │
│ [1,3,2,4]                │ [3,2,1]                  │
└──────────────────────────┴──────────────────────────┘
```

## arrayPartialSort[​](#arrayPartialSort "Direct link to arrayPartialSort")

Introduced in: v23.2.0

This function is the same as `arraySort` but with an additional `limit` argument allowing partial sorting.

Tip

To retain only the sorted elements use `arrayResize`.

**Syntax**

```
arrayPartialSort([f,] arr [, arr1, ... ,arrN], limit)
```

**Arguments**