Distance functions

**Returned value**

Returns the sum of the squares of the differences between the corresponding elements of two vectors. For `Array` inputs, returns `Float32` if the least common supertype of the element types is `Float32` or `BFloat16`, otherwise `Float64`. For `Tuple` inputs, the return type follows the arithmetic result type of the element-wise operations (integer types are preserved). [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)

**Examples**

**Basic usage**

```
SELECT L2SquaredDistance([1, 2, 3], [0, 0, 0])
```

```
┌─L2SquaredDis⋯ [0, 0, 0])─┐
│                       14 │
└──────────────────────────┘
```

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

Introduced in: v22.7.0

Calculates the square root of the sum of the squares of the vector elements (the [`L2Norm`](#L2Norm)) squared.

**Syntax**

```
L2SquaredNorm(vector)
```

**Aliases**: `normL2Squared`

**Arguments**

- `vector` — Vector or tuple of numeric values. [`Array(T)`](/docs/sql-reference/data-types/array) or [`Tuple(T)`](/docs/sql-reference/data-types/tuple)

**Returned value**

Returns the L2-norm squared. [`UInt*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal`](/docs/sql-reference/data-types/decimal)

**Examples**

**Basic usage**

```
SELECT L2SquaredNorm((1, 2))
```

```
┌─L2SquaredNorm((1, 2))─┐
│                     5 │
└───────────────────────┘
```

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

Introduced in: v21.11.0

Calculates the distance between two points (the elements of the vectors are the coordinates) in `L_{inf}` space ([maximum norm](https://en.wikipedia.org/wiki/Norm_(mathematics)#Maximum_norm_(special_case_of:_infinity_norm,_uniform_norm,_or_supremum_norm))).

**Syntax**

```
LinfDistance(vector1, vector2)
```

**Aliases**: `distanceLinf`

**Arguments**

- `vector1` — First vector. [`Tuple(T)`](/docs/sql-reference/data-types/tuple) or [`Array(T)`](/docs/sql-reference/data-types/array)
- `vector2` — Second vector. [`Tuple(T)`](/docs/sql-reference/data-types/tuple) or [`Array(T)`](/docs/sql-reference/data-types/array)

**Returned value**

Returns the infinity-norm distance. For `Array` inputs, returns `Float32` if the least common supertype of the element types is `Float32` or `BFloat16`, otherwise `Float64`. For `Tuple` inputs, always returns `Float64`. [`Float*`](/docs/sql-reference/data-types/float)

**Examples**

**Basic usage**

```
SELECT LinfDistance((1, 2), (2, 3))
```

```
┌─LinfDistance((1, 2), (2, 3))─┐
│                            1 │
└──────────────────────────────┘
```

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

Introduced in: v21.11.0

Calculates the maximum of absolute elements of a vector.

**Syntax**

```
LinfNorm(vector)
```

**Aliases**: `normLinf`

**Arguments**

- `vector` — Vector or tuple of numeric values. [`Array(T)`](/docs/sql-reference/data-types/array) or [`Tuple(T)`](/docs/sql-reference/data-types/tuple)

**Returned value**

Returns the Linf-norm or the maximum absolute value. [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Basic usage**

```
SELECT LinfNorm((1, -2))
```

```
┌─LinfNorm((1, -2))─┐
│                 2 │
└───────────────────┘
```

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

Introduced in: v21.11.0

Calculates the unit vector of a given vector (the elements of the tuple are the coordinates) in `L_{inf}` space (using [maximum norm](https://en.wikipedia.org/wiki/Norm_(mathematics)#Maximum_norm_(special_case_of:_infinity_norm,_uniform_norm,_or_supremum_norm))).

**Syntax**

```
LinfNormalize(tuple)
```

**Aliases**: `normalizeLinf`

**Arguments**

- `tuple` — A tuple of numeric values. [`Tuple(T)`](/docs/sql-reference/data-types/tuple)

**Returned value**

Returns the unit vector. [`Tuple(Float64)`](/docs/sql-reference/data-types/tuple)

**Examples**

**Basic usage**

```
SELECT LinfNormalize((3, 4))
```

```
┌─LinfNormalize((3, 4))─┐
│ (0.75,1)              │
└───────────────────────┘
```

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

Introduced in: v21.11.0

Calculates the distance between two points (the elements of the vectors are the coordinates) in `Lp` space ([p-norm distance](https://en.wikipedia.org/wiki/Norm_(mathematics)#p-norm)).

**Syntax**

```
LpDistance(vector1, vector2, p)
```

**Aliases**: `distanceLp`

**Arguments**

- `vector1` — First vector. [`Tuple(T)`](/docs/sql-reference/data-types/tuple) or [`Array(T)`](/docs/sql-reference/data-types/array)
- `vector2` — Second vector. [`Tuple(T)`](/docs/sql-reference/data-types/tuple) or [`Array(T)`](/docs/sql-reference/data-types/array)
- `p` — The power. Possible values: real number from `[1; inf)`. [`UInt*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)

**Returned value**

Returns the p-norm distance. For `Array` inputs, returns `Float32` if the least common supertype of the element types is `Float32` or `BFloat16`, otherwise `Float64`. For `Tuple` inputs, always returns `Float64`. [`Float*`](/docs/sql-reference/data-types/float)

**Examples**

**Basic usage**

```
SELECT LpDistance((1, 2), (2, 3), 3)
```

```
┌─LpDistance((1, 2), (2, 3), 3)─┐
│            1.2599210498948732 │
└───────────────────────────────┘
```

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

Introduced in: v21.11.0

Calculates the p-norm of a vector, which is the p-th root of the sum of the p-th powers of the absolute elements of its elements.

Special cases:

- When p=1, it's equivalent to L1Norm (Manhattan distance).
- When p=2, it's equivalent to L2Norm (Euclidean distance).
- When p=∞, it's equivalent to LinfNorm (maximum norm).

**Syntax**

```
LpNorm(vector, p)
```

**Aliases**: `normLp`

**Arguments**

- `vector` — Vector or tuple of numeric values. [`Tuple(T)`](/docs/sql-reference/data-types/tuple) or [`Array(T)`](/docs/sql-reference/data-types/array)
- `p` — The power. Possible values are real numbers in the range `[1; inf)`. [`UInt*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)

**Returned value**

Returns the [Lp-norm](https://en.wikipedia.org/wiki/Norm_(mathematics)#p-norm). [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Basic usage**

```
SELECT LpNorm((1, -2), 2)
```

```
┌─LpNorm((1, -2), 2)─┐
│   2.23606797749979 │
└────────────────────┘
```

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

Introduced in: v21.11.0

Calculates the unit vector of a given vector (the elements of the tuple are the coordinates) in `Lp` space (using [p-norm](https://en.wikipedia.org/wiki/Norm_(mathematics)#p-norm)).

**Syntax**

```
LpNormalize(tuple, p)
```

**Aliases**: `normalizeLp`

**Arguments**

- `tuple` — A tuple of numeric values. [`Tuple(T)`](/docs/sql-reference/data-types/tuple)
- `p` — The power. Possible values are any number in the range range from `[1; inf)`. [`UInt*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float)

**Returned value**

Returns the unit vector. [`Tuple(Float64)`](/docs/sql-reference/data-types/tuple)

**Examples**

**Usage example**

```
SELECT LpNormalize((3, 4), 5)
```

```
┌─LpNormalize((3, 4), 5)──────────────────┐
│ (0.7187302630182624,0.9583070173576831) │
└─────────────────────────────────────────┘
```

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

Introduced in: v21.11.0

Calculates the [cosine distance](https://en.wikipedia.org/wiki/Cosine_similarity#Cosine_distance) between two vectors (the elements of the tuples are the coordinates). The smaller the returned value is, the more similar are the vectors.

**Syntax**

```
cosineDistance(vector1, vector2)
```

**Aliases**: `distanceCosine`

**Arguments**

- `vector1` — First tuple. [`Tuple(T)`](/docs/sql-reference/data-types/tuple) or [`Array(T)`](/docs/sql-reference/data-types/array)
- `vector2` — Second tuple. [`Tuple(T)`](/docs/sql-reference/data-types/tuple) or [`Array(T)`](/docs/sql-reference/data-types/array)

**Returned value**