Distance functions

## 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**

Returns the cosine distance (one minus the cosine similarity). 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 cosineDistance((1, 2), (2, 3));
```

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

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

Introduced in: v26.1.0

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

**Syntax**

```
cosineDistanceTransposed(vector1, vector2, p)
```

**Aliases**: `distanceCosineTransposed`

**Arguments**

- `vectors` — Vectors. [`QBit(T, UInt64)`](/docs/sql-reference/data-types/qbit)
- `reference` — Reference vector. [`Array(T)`](/docs/sql-reference/data-types/array)
- `p` — Number of bits from each vector element to use in the distance calculation (1 to element bit-width). The quantization level controls the precision-speed trade-off. Using fewer bits results in faster I/O and calculations with reduced accuracy, while using more bits increases accuracy at the cost of performance. [`UInt`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns the approximate cosine distance (one minus the cosine similarity). Always returns Float64. [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Basic usage**

```
CREATE TABLE qbit (id UInt32, vec QBit(Float64, 2)) ENGINE = Memory;
INSERT INTO qbit VALUES (1, [0, 1]);
SELECT cosineDistanceTransposed(vec, array(1, 2), 16) FROM qbit;
```

```
┌─cosineDistanceTransposed([0, 1], [1, 2], 16)─┐
│                          0.10557281085638826 │
└──────────────────────────────────────────────┘
```

- [L1Distance](#L1Distance)- [L1Norm](#L1Norm)- [L1Normalize](#L1Normalize)- [L2Distance](#L2Distance)- [L2DistanceTransposed](#L2DistanceTransposed)- [L2Norm](#L2Norm)- [L2Normalize](#L2Normalize)- [L2SquaredDistance](#L2SquaredDistance)- [L2SquaredNorm](#L2SquaredNorm)- [LinfDistance](#LinfDistance)- [LinfNorm](#LinfNorm)- [LinfNormalize](#LinfNormalize)- [LpDistance](#LpDistance)- [LpNorm](#LpNorm)- [LpNormalize](#LpNormalize)- [cosineDistance](#cosineDistance)- [cosineDistanceTransposed](#cosineDistanceTransposed)

Was this page helpful?