2022-08-28 13:53:52 +00:00
---
2022-08-28 13:58:27 +00:00
slug: /en/sql-reference/functions/distance-functions
2023-04-19 17:05:55 +00:00
sidebar_position: 55
sidebar_label: Distance
2022-08-28 13:53:52 +00:00
---
2023-04-19 17:05:55 +00:00
# Distance Functions
2022-07-13 10:29:13 +00:00
## L1Norm
Calculates the sum of absolute values of a vector.
**Syntax**
```sql
L1Norm(vector)
```
Alias: `normL1` .
**Arguments**
2024-05-24 03:54:16 +00:00
- `vector` — [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
2022-07-13 10:29:13 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- L1-norm or [taxicab geometry ](https://en.wikipedia.org/wiki/Taxicab_geometry ) distance. [UInt ](../data-types/int-uint.md ), [Float ](../data-types/float.md ) or [Decimal ](../data-types/decimal.md ).
2022-07-13 10:29:13 +00:00
**Examples**
Query:
```sql
SELECT L1Norm((1, 2));
```
Result:
```text
┌─L1Norm((1, 2))─┐
│ 3 │
└────────────────┘
```
## L2Norm
Calculates the square root of the sum of the squares of the vector values.
**Syntax**
```sql
L2Norm(vector)
```
Alias: `normL2` .
**Arguments**
2024-05-24 03:54:16 +00:00
- `vector` — [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
2022-07-13 10:29:13 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- L2-norm or [Euclidean distance ](https://en.wikipedia.org/wiki/Euclidean_distance ). [Float ](../data-types/float.md ).
2022-07-13 10:29:13 +00:00
**Example**
Query:
```sql
SELECT L2Norm((1, 2));
```
Result:
```text
┌───L2Norm((1, 2))─┐
│ 2.23606797749979 │
└──────────────────┘
```
2024-04-09 14:54:09 +00:00
## L2SquaredNorm
Calculates the square root of the sum of the squares of the vector values (the [L2Norm ](#l2norm )) squared.
**Syntax**
```sql
L2SquaredNorm(vector)
```
Alias: `normL2Squared` .
***Arguments**
2024-05-24 03:54:16 +00:00
- `vector` — [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
2024-04-09 14:54:09 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- L2-norm squared. [Float ](../data-types/float.md ).
2024-04-09 14:54:09 +00:00
**Example**
Query:
```sql
SELECT L2SquaredNorm((1, 2));
```
Result:
```text
┌─L2SquaredNorm((1, 2))─┐
│ 5 │
└───────────────────────┘
```
2022-07-13 10:29:13 +00:00
## LinfNorm
Calculates the maximum of absolute values of a vector.
**Syntax**
```sql
LinfNorm(vector)
```
Alias: `normLinf` .
**Arguments**
2024-05-24 03:54:16 +00:00
- `vector` — [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
2022-07-13 10:29:13 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- Linf-norm or the maximum absolute value. [Float ](../data-types/float.md ).
2022-07-13 10:29:13 +00:00
**Example**
Query:
```sql
SELECT LinfNorm((1, -2));
```
Result:
```text
┌─LinfNorm((1, -2))─┐
│ 2 │
└───────────────────┘
```
## LpNorm
Calculates the root of `p` -th power of the sum of the absolute values of a vector in the power of `p` .
**Syntax**
```sql
LpNorm(vector, p)
```
Alias: `normLp` .
**Arguments**
2024-05-24 03:54:16 +00:00
- `vector` — [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
- `p` — The power. Possible values: real number in `[1; inf)` . [UInt ](../data-types/int-uint.md ) or [Float ](../data-types/float.md ).
2022-07-13 10:29:13 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- [Lp-norm ](https://en.wikipedia.org/wiki/Norm_(mathematics )#p -norm). [Float ](../data-types/float.md ).
2022-07-13 10:29:13 +00:00
**Example**
Query:
```sql
SELECT LpNorm((1, -2), 2);
```
Result:
```text
┌─LpNorm((1, -2), 2)─┐
│ 2.23606797749979 │
└────────────────────┘
```
## L1Distance
Calculates the distance between two points (the values of the vectors are the coordinates) in `L1` space (1-norm ([taxicab geometry](https://en.wikipedia.org/wiki/Taxicab_geometry) distance)).
**Syntax**
```sql
L1Distance(vector1, vector2)
```
Alias: `distanceL1` .
**Arguments**
2024-05-24 03:54:16 +00:00
- `vector1` — First vector. [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
- `vector2` — Second vector. [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
2022-07-13 10:29:13 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- 1-norm distance. [Float ](../data-types/float.md ).
2022-07-13 10:29:13 +00:00
**Example**
Query:
```sql
SELECT L1Distance((1, 2), (2, 3));
```
Result:
```text
┌─L1Distance((1, 2), (2, 3))─┐
│ 2 │
└────────────────────────────┘
```
## L2Distance
Calculates the distance between two points (the values of the vectors are the coordinates) in Euclidean space ([Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance)).
**Syntax**
```sql
L2Distance(vector1, vector2)
```
Alias: `distanceL2` .
**Arguments**
2024-05-24 03:54:16 +00:00
- `vector1` — First vector. [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
- `vector2` — Second vector. [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
2022-07-13 10:29:13 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- 2-norm distance. [Float ](../data-types/float.md ).
2022-07-13 10:29:13 +00:00
**Example**
Query:
```sql
SELECT L2Distance((1, 2), (2, 3));
```
Result:
```text
┌─L2Distance((1, 2), (2, 3))─┐
│ 1.4142135623730951 │
└────────────────────────────┘
```
2023-06-23 20:05:58 +00:00
## L2SquaredDistance
2023-06-23 18:45:52 +00:00
Calculates the sum of the squares of the difference between the corresponding elements of two vectors.
**Syntax**
```sql
L2SquaredDistance(vector1, vector2)
```
Alias: `distanceL2Squared` .
**Arguments**
2024-05-24 03:54:16 +00:00
- `vector1` — First vector. [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
- `vector2` — Second vector. [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
2023-06-23 18:45:52 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- Sum of the squares of the difference between the corresponding elements of two vectors. [Float ](../data-types/float.md ).
2023-06-23 18:45:52 +00:00
**Example**
Query:
```sql
SELECT L2SquaredDistance([1, 2, 3], [0, 0, 0])
```
Result:
```response
┌─L2SquaredDistance([1, 2, 3], [0, 0, 0])─┐
│ 14 │
└─────────────────────────────────────────┘
```
2022-07-13 10:29:13 +00:00
## LinfDistance
Calculates the distance between two points (the values 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**
```sql
LinfDistance(vector1, vector2)
```
Alias: `distanceLinf` .
**Arguments**
2024-05-24 03:54:16 +00:00
- `vector1` — First vector. [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
- `vector1` — Second vector. [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
2022-07-13 10:29:13 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- Infinity-norm distance. [Float ](../data-types/float.md ).
2022-07-13 10:29:13 +00:00
**Example**
Query:
```sql
SELECT LinfDistance((1, 2), (2, 3));
```
Result:
```text
┌─LinfDistance((1, 2), (2, 3))─┐
│ 1 │
└──────────────────────────────┘
```
## LpDistance
Calculates the distance between two points (the values of the vectors are the coordinates) in `Lp` space ([p-norm distance](https://en.wikipedia.org/wiki/Norm_(mathematics)#p-norm)).
**Syntax**
```sql
LpDistance(vector1, vector2, p)
```
Alias: `distanceLp` .
**Arguments**
2024-05-24 03:54:16 +00:00
- `vector1` — First vector. [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
- `vector2` — Second vector. [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
- `p` — The power. Possible values: real number from `[1; inf)` . [UInt ](../data-types/int-uint.md ) or [Float ](../data-types/float.md ).
2022-07-13 10:29:13 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- p-norm distance. [Float ](../data-types/float.md ).
2022-07-13 10:29:13 +00:00
**Example**
Query:
```sql
SELECT LpDistance((1, 2), (2, 3), 3);
```
Result:
```text
┌─LpDistance((1, 2), (2, 3), 3)─┐
│ 1.2599210498948732 │
└───────────────────────────────┘
```
## L1Normalize
Calculates the unit vector of a given vector (the values of the tuple are the coordinates) in `L1` space ([taxicab geometry](https://en.wikipedia.org/wiki/Taxicab_geometry)).
**Syntax**
```sql
L1Normalize(tuple)
```
Alias: `normalizeL1` .
**Arguments**
2024-05-24 03:54:16 +00:00
- `tuple` — [Tuple ](../data-types/tuple.md ).
2022-07-13 10:29:13 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- Unit vector. [Tuple ](../data-types/tuple.md ) of [Float ](../data-types/float.md ).
2022-07-13 10:29:13 +00:00
**Example**
Query:
```sql
SELECT L1Normalize((1, 2));
```
Result:
```text
┌─L1Normalize((1, 2))─────────────────────┐
│ (0.3333333333333333,0.6666666666666666) │
└─────────────────────────────────────────┘
```
## L2Normalize
Calculates the unit vector of a given vector (the values of the tuple are the coordinates) in Euclidean space (using [Euclidean distance ](https://en.wikipedia.org/wiki/Euclidean_distance )).
**Syntax**
```sql
L2Normalize(tuple)
```
Alias: `normalizeL1` .
**Arguments**
2024-05-24 03:54:16 +00:00
- `tuple` — [Tuple ](../data-types/tuple.md ).
2022-07-13 10:29:13 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- Unit vector. [Tuple ](../data-types/tuple.md ) of [Float ](../data-types/float.md ).
2022-07-13 10:29:13 +00:00
**Example**
Query:
```sql
SELECT L2Normalize((3, 4));
```
Result:
```text
┌─L2Normalize((3, 4))─┐
│ (0.6,0.8) │
└─────────────────────┘
```
## LinfNormalize
Calculates the unit vector of a given vector (the values 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**
```sql
LinfNormalize(tuple)
```
Alias: `normalizeLinf ` .
**Arguments**
2024-05-24 03:54:16 +00:00
- `tuple` — [Tuple ](../data-types/tuple.md ).
2022-07-13 10:29:13 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- Unit vector. [Tuple ](../data-types/tuple.md ) of [Float ](../data-types/float.md ).
2022-07-13 10:29:13 +00:00
**Example**
Query:
```sql
SELECT LinfNormalize((3, 4));
```
Result:
```text
┌─LinfNormalize((3, 4))─┐
│ (0.75,1) │
└───────────────────────┘
```
## LpNormalize
Calculates the unit vector of a given vector (the values of the tuple are the coordinates) in `Lp` space (using [p-norm ](https://en.wikipedia.org/wiki/Norm_(mathematics )#p -norm)).
**Syntax**
```sql
LpNormalize(tuple, p)
```
Alias: `normalizeLp ` .
**Arguments**
2024-05-24 03:54:16 +00:00
- `tuple` — [Tuple ](../data-types/tuple.md ).
- `p` — The power. Possible values: any number from [1;inf). [UInt ](../data-types/int-uint.md ) or [Float ](../data-types/float.md ).
2022-07-13 10:29:13 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- Unit vector. [Tuple ](../data-types/tuple.md ) of [Float ](../data-types/float.md ).
2022-07-13 10:29:13 +00:00
**Example**
Query:
```sql
SELECT LpNormalize((3, 4),5);
```
Result:
```text
┌─LpNormalize((3, 4), 5)──────────────────┐
│ (0.7187302630182624,0.9583070173576831) │
└─────────────────────────────────────────┘
```
## cosineDistance
2024-02-19 16:30:00 +00:00
Calculates the cosine distance between two vectors (the values of the tuples are the coordinates). The smaller the returned value is, the more similar are the vectors.
2022-07-13 10:29:13 +00:00
**Syntax**
```sql
2022-10-28 17:03:35 +00:00
cosineDistance(vector1, vector2)
2022-07-13 10:29:13 +00:00
```
**Arguments**
2024-05-24 03:54:16 +00:00
- `vector1` — First tuple. [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
- `vector2` — Second tuple. [Tuple ](../data-types/tuple.md ) or [Array ](../data-types/array.md ).
2022-07-13 10:29:13 +00:00
**Returned value**
2024-05-24 03:54:16 +00:00
- Cosine of the angle between two vectors subtracted from one. [Float ](../data-types/float.md ).
2022-07-13 10:29:13 +00:00
2022-10-28 17:03:35 +00:00
**Examples**
2022-07-13 10:29:13 +00:00
Query:
```sql
SELECT cosineDistance((1, 2), (2, 3));
```
Result:
```text
┌─cosineDistance((1, 2), (2, 3))─┐
│ 0.007722123286332261 │
└────────────────────────────────┘
```