Hash functions

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

```
SELECT hex(sipHash128Reference('foo', '', 3));
```

```
┌─hex(sipHash128Reference('foo', '', 3))─┐
│ 4D1BE1A22D7F5933C0873E1698426260       │
└────────────────────────────────────────┘
```

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

Introduced in: v23.2.0

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

**Syntax**

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

**Arguments**

- `(k0, k1)` — Tuple of two 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**

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

**Examples**

**Usage example**

```
SELECT hex(sipHash128Reference('foo', '', 3));
```

```
┌─hex(sipHash128Reference('foo', '', 3))─┐
│ 4D1BE1A22D7F5933C0873E1698426260       │
└────────────────────────────────────────┘
```

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

Introduced in: v1.1.0

Produces a 64-bit [SipHash](https://en.wikipedia.org/wiki/SipHash) hash value.

This is a cryptographic hash function. It works at least three times faster than the [`MD5`](#MD5) hash function.

The function [interprets](/docs/sql-reference/functions/type-conversion-functions#reinterpretAsString) all the input parameters as strings and calculates the hash value for each of them.
It then combines the hashes using the following algorithm:

1. The first and the second hash value are concatenated to an array which is hashed.
2. The previously calculated hash value and the hash of the third input parameter are hashed in a similar way.
3. This calculation is repeated for all remaining hash values of the original input.

Note

the calculated hash values may be equal for the same input values of different argument types.
This affects for example integer types of different size, named and unnamed `Tuple` with the same data, `Map` and the corresponding `Array(Tuple(key, value))` type with the same data.

**Syntax**

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

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT sipHash64(array('e','x','a'), 'mple', 10, toDateTime('2019-06-15 23:00:00')) AS SipHash, toTypeName(SipHash) AS type;
```

```
┌──────────────SipHash─┬─type───┐
│ 11400366955626497465 │ UInt64 │
└──────────────────────┴────────┘
```

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

Introduced in: v23.2.0

Like [`sipHash64`](#sipHash64) but additionally takes an explicit key argument instead of using a fixed key.

**Syntax**

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

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT sipHash64Keyed((506097522914230528, 1084818905618843912), array('e','x','a'), 'mple', 10, toDateTime('2019-06-15 23:00:00')) AS SipHash, toTypeName(SipHash) AS type;
```

```
┌─────────────SipHash─┬─type───┐
│ 8017656310194184311 │ UInt64 │
└─────────────────────┴────────┘
```

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

Introduced in: v21.1.0

Splits a ASCII string into parts (shingles) of `shinglesize` words, calculates hash values for each word shingle and returns a tuple with these hashes.
Uses `hashnum` minimum hashes to calculate the minimum hash and `hashnum` maximum hashes to calculate the maximum hash.
It is case sensitive.

Can be used to detect semi-duplicate strings with [`tupleHammingDistance`](/docs/sql-reference/functions/tuple-functions#tupleHammingDistance).
For two strings, if the returned hashes are the same for both strings, then those strings are the same.

**Syntax**

```
wordShingleMinHash(string[, shinglesize, hashnum])
```

**Arguments**

- `string` — String for which to compute the hash. [`String`](/docs/sql-reference/data-types/string)
- `shinglesize` — Optional. The size of a word shingle, any number from `1` to `25`. The default value is `3`. [`UInt8`](/docs/sql-reference/data-types/int-uint)
- `hashnum` — Optional. The number of minimum and maximum hashes used to calculate the result, any number from `1` to `25`. The default value is `6`. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Returned value**

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

**Examples**

**Usage example**

```
SELECT wordShingleMinHash('ClickHouse® is a column-oriented database management system (DBMS) for online analytical processing of queries (OLAP).') AS Tuple;
```

```
┌─Tuple──────────────────────────────────────┐
│ (16452112859864147620,5844417301642981317) │
└────────────────────────────────────────────┘
```

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

Introduced in: v1.1.0

Splits a ASCII string into parts (shingles) of `shinglesize` words each and returns the shingles with minimum and maximum word hashes, calculated by the wordShingleMinHash function with the same input.
It is case sensitive.

**Syntax**

```
wordShingleMinHashArg(string[, shinglesize, hashnum])
```

**Arguments**

- `string` — String for which to compute the hash. [`String`](/docs/sql-reference/data-types/string)
- `shinglesize` — Optional. The size of a word shingle, any number from `1` to `25`. The default value is `3`. [`UInt8`](/docs/sql-reference/data-types/int-uint)
- `hashnum` — Optional. The number of minimum and maximum hashes used to calculate the result, any number from `1` to `25`. The default value is `6`. [`UInt8`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Returns a tuple with two tuples with `hashnum` word shingles each. [`Tuple(Tuple(String))`](/docs/sql-reference/data-types/tuple)

**Examples**

**Usage example**

```
SELECT wordShingleMinHashArg('ClickHouse® is a column-oriented database management system (DBMS) for online analytical processing of queries (OLAP).', 1, 3) AS Tuple;
```

```
┌─Tuple─────────────────────────────────────────────────────────────────┐
│ (('OLAP','database','analytical'),('online','oriented','processing')) │
└───────────────────────────────────────────────────────────────────────┘
```

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

Introduced in: v21.1.0

Splits a ASCII string into parts (shingles) of `shinglesize` words each and returns the shingles with minimum and maximum word hashes, calculated by the [`wordShingleMinHashCaseInsensitive`](#wordShingleMinHashCaseInsensitive) function with the same input.
It is case insensitive.

**Syntax**

```
wordShingleMinHashArgCaseInsensitive(string[, shinglesize, hashnum])
```