Hash functions

**Returned value**

Returns a tuple with two hashes — the minimum and the maximum. [`Tuple`](/docs/sql-reference/data-types/tuple)

**Examples**

**Usage example**

```
SELECT ngramMinHashUTF8('ClickHouse') AS Tuple;
```

```
┌─Tuple──────────────────────────────────────┐
│ (18333312859352735453,6742163577938632877) │
└────────────────────────────────────────────┘
```

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

Introduced in: v21.1.0

Splits a ASCII string into n-grams of `ngramsize` symbols and returns the n-gram `simhash`.

Can be used for detection of semi-duplicate strings with [`bitHammingDistance`](/docs/sql-reference/functions/bit-functions#bitHammingDistance).
The smaller the [Hamming distance](https://en.wikipedia.org/wiki/Hamming_distance) of the calculated `simhashes` of two strings, the more likely these strings are the same.

**Syntax**

```
ngramSimHash(string[, ngramsize])
```

**Arguments**

- `string` — String for which to compute the case sensitive `simhash`. [`String`](/docs/sql-reference/data-types/string)
- `ngramsize` — Optional. The size of an n-gram, any number from `1` to `25`. The default value is`3`. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns the computed hash of the input string. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT ngramSimHash('ClickHouse') AS Hash;
```

```
┌───────Hash─┐
│ 1627567969 │
└────────────┘
```

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

Introduced in: v21.1.0

Splits a ASCII string into n-grams of `ngramsize` symbols and returns the n-gram `simhash`.
It is case insensitive.

Can be used for detection of semi-duplicate strings with [`bitHammingDistance`](/docs/sql-reference/functions/bit-functions#bitHammingDistance).
The smaller the [Hamming distance](https://en.wikipedia.org/wiki/Hamming_distance) of the calculated `simhashes` of two strings, the more likely these strings are the same.

**Syntax**

```
ngramSimHashCaseInsensitive(string[, ngramsize])
```

**Arguments**

- `string` — String for which to compute the case insensitive `simhash`. [`String`](/docs/sql-reference/data-types/string)
- `ngramsize` — Optional. The size of an n-gram, any number from `1` to `25`. The default value is `3`. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Hash value. [UInt64](/docs/sql-reference/data-types/int-uint). [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT ngramSimHashCaseInsensitive('ClickHouse') AS Hash;
```

```
┌──────Hash─┐
│ 562180645 │
└───────────┘
```

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

Introduced in: v21.1.0

Splits a UTF-8 string into n-grams of `ngramsize` symbols and returns the n-gram `simhash`.
It is case insensitive.

Can be used for detection of semi-duplicate strings with [bitHammingDistance](/docs/sql-reference/functions/bit-functions#bitHammingDistance). The smaller is the [Hamming Distance](https://en.wikipedia.org/wiki/Hamming_distance) of the calculated `simhashes` of two strings, the more likely these strings are the same.

**Syntax**

```
ngramSimHashCaseInsensitiveUTF8(string[, ngramsize])
```

**Arguments**

- `string` — String for which to compute the hash. [`String`](/docs/sql-reference/data-types/string)
- `ngramsize` — Optional. The size of an n-gram, any number from `1` to `25`. The default value is `3`. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns the computed hash value. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT ngramSimHashCaseInsensitiveUTF8('ClickHouse') AS Hash;
```

```
┌───────Hash─┐
│ 1636742693 │
└────────────┘
```

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

Introduced in: v21.1.0

Splits a UTF-8 encoded string into n-grams of `ngramsize` symbols and returns the n-gram `simhash`.
It is case sensitive.

Can be used for detection of semi-duplicate strings with [`bitHammingDistance`](/docs/sql-reference/functions/bit-functions#bitHammingDistance).
The smaller the [Hamming distance](https://en.wikipedia.org/wiki/Hamming_distance) of the calculated `simhashes` of two strings, the more likely these strings are the same.

**Syntax**

```
ngramSimHashUTF8(string[, ngramsize])
```

**Arguments**

- `string` — String for which to compute the hash. [`String`](/docs/sql-reference/data-types/string)
- `ngramsize` — Optional. The size of an n-gram, any number from `1` to `25`. The default value is `3`. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns the computed hash value. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

```
SELECT ngramSimHashUTF8('ClickHouse') AS Hash;
```

```
┌───────Hash─┐
│ 1628157797 │
└────────────┘
```

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

Introduced in: v1.1.0

Like [`sipHash64`](#sipHash64) but produces a 128-bit hash value, i.e. the final xor-folding state is done up to 128 bits.

use sipHash128Reference for new projects

This 128-bit variant differs from the reference implementation and is weaker.
This version exists because, when it was written, there was no official 128-bit extension for SipHash.
New projects are advised to use [`sipHash128Reference`](#sipHash128Reference).

**Syntax**

```
sipHash128(arg1[, arg2, ...])
```

**Arguments**

- `arg1[, arg2, ...]` — A variable number of input arguments for which to compute the hash. [`Any`](/docs/sql-reference/data-types)

**Returned value**

Returns a 128-bit `SipHash` hash value. [`FixedString(16)`](/docs/sql-reference/data-types/fixedstring)

**Examples**

**Usage example**

```
SELECT hex(sipHash128('foo', '\x01', 3));
```

```
┌─hex(sipHash128('foo', '', 3))────┐
│ 9DE516A64A414D4B1B609415E4523F24 │
└──────────────────────────────────┘
```

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

Introduced in: v23.2.0

Same as [`sipHash128`](#sipHash128) but additionally takes an explicit key argument instead of using a fixed key.

use sipHash128ReferenceKeyed for new projects

This 128-bit variant differs from the reference implementation and it's weaker.
This version exists because, when it was written, there was no official 128-bit extension for SipHash.
New projects should probably use [`sipHash128ReferenceKeyed`](#sipHash128ReferenceKeyed).

**Syntax**

```
sipHash128Keyed((k0, k1), [arg1, arg2, ...])
```

**Arguments**

- `(k0, k1)` — A tuple of two UInt64 values representing the key. [`Tuple(UInt64, UInt64)`](/docs/sql-reference/data-types/tuple)
- `arg1[, arg2, ...]` — A variable number of input arguments for which to compute the hash. [`Any`](/docs/sql-reference/data-types)

**Returned value**

A 128-bit `SipHash` hash value of type [FixedString(16)](/docs/sql-reference/data-types/fixedstring). [`FixedString(16)`](/docs/sql-reference/data-types/fixedstring)

**Examples**

**Usage example**

```
SELECT hex(sipHash128Keyed((506097522914230528, 1084818905618843912),'foo', '\x01', 3));
```

```
┌─hex(sipHash128Keyed((506097522914230528, 1084818905618843912), 'foo', '', 3))─┐
│ B8467F65C8B4CFD9A5F8BD733917D9BF                                              │
└───────────────────────────────────────────────────────────────────────────────┘
```

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

Introduced in: v23.2.0

Like [`sipHash128`](/docs/sql-reference/functions/hash-functions#sipHash128) but implements the 128-bit algorithm from the original authors of SipHash.

**Syntax**

```
sipHash128Reference(arg1[, arg2, ...])
```

**Arguments**

- `arg1[, arg2, ...]` — A variable number of input arguments for which to compute the hash. [`Any`](/docs/sql-reference/data-types)

**Returned value**

Returns the computed 128-bit `SipHash` hash value of the input arguments. [`FixedString(16)`](/docs/sql-reference/data-types/fixedstring)

**Examples**

**Usage example**