Hash functions

## SHA512\_256[​](#SHA512_256 "Direct link to SHA512_256")

Introduced in: v1.1.0

Calculates the SHA512\_256 hash of the given string.

**Syntax**

```
SHA512_256(s)
```

**Arguments**

- `s` — The input string to hash. [`String`](/docs/sql-reference/data-types/string)

**Returned value**

Returns the SHA512\_256 hash of the given input string as a fixed-length string. [`FixedString(32)`](/docs/sql-reference/data-types/fixedstring)

**Examples**

**Usage example**

```
SELECT HEX(SHA512_256('abc'));
```

```
┌─hex(SHA512_256('abc'))───────────────────────────────────────────┐
│ 53048E2681941EF99B2E29B76B4C7DABE4C2D0C634FC6D46E0E2F13107E7AF23 │
└──────────────────────────────────────────────────────────────────┘
```

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

Introduced in: v1.1.0

A fast, decent-quality non-cryptographic hash function for a string obtained from a URL using some type of normalization.

This hash function has two modes:

|  |  |  |  |  |  |
| --- | --- | --- | --- | --- | --- |
| Mode Description|  |  |  |  | | --- | --- | --- | --- | | `URLHash(url)` Calculates a hash from a string without one of the trailing symbols `/`,`?` or `#` at the end, if present.| `URLHash(url, N)` Calculates a hash from a string up to the N level in the URL hierarchy, without one of the trailing symbols `/`,`?` or `#` at the end, if present. Levels are the same as in `URLHierarchy`. | | | | | |

**Syntax**

```
URLHash(url[, N])
```

**Arguments**

- `url` — URL string to hash. [`String`](/docs/sql-reference/data-types/string)
- `N` — Optional. Level in the URL hierarchy. [`(U)Int*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

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

**Examples**

**Usage example**

```
SELECT URLHash('https://www.clickhouse.com')
```

```
┌─URLHash('htt⋯house.com')─┐
│     13614512636072854701 │
└──────────────────────────┘
```

**Hash of url with specified level**

```
SELECT URLHash('https://www.clickhouse.com/docs', 0);
SELECT URLHash('https://www.clickhouse.com/docs', 1);
```

```
-- hash of https://www.clickhouse.com
┌─URLHash('htt⋯m/docs', 0)─┐
│     13614512636072854701 │
└──────────────────────────┘
-- hash of https://www.clickhouse.com/docs
┌─URLHash('htt⋯m/docs', 1)─┐
│     13167253331440520598 │
└──────────────────────────┘
```

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

Introduced in: v1.1.0

Produces a 64-bit [CityHash](https://github.com/google/cityhash) hash value.

This is a fast non-cryptographic hash function.
It uses the CityHash algorithm for string parameters and implementation-specific fast non-cryptographic hash function for parameters with other data types.
The function uses the CityHash combinator to get the final results.

References

Google changed the algorithm of CityHash after it was added to ClickHouse.
In other words, ClickHouse's cityHash64 and Google's upstream CityHash now produce different results.
ClickHouse cityHash64 corresponds to CityHash v1.0.2.

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

```
cityHash64(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 hash of the input arguments. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Call example**

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

```
┌─────────────CityHash─┬─type───┐
│ 12072650598913549138 │ UInt64 │
└──────────────────────┴────────┘
```

**Computing the checksum of the entire table with accuracy up to the row order**

```
CREATE TABLE users (
    id UInt32,
    name String,
    age UInt8,
    city String
)
ENGINE = MergeTree
ORDER BY tuple();

INSERT INTO users VALUES
(1, 'Alice', 25, 'New York'),
(2, 'Bob', 30, 'London'),
(3, 'Charlie', 35, 'Tokyo');

SELECT groupBitXor(cityHash64(*)) FROM users;
```

```
┌─groupBitXor(⋯age, city))─┐
│     11639977218258521182 │
└──────────────────────────┘
```

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

Introduced in: v20.12.0

Produces a 64-bit [FarmHash](https://github.com/google/farmhash) value using the `Fingerprint64` method.

Tip

`farmFingerprint64` is preferred for a stable and portable value over [`farmHash64`](#farmHash64).

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

```
farmFingerprint64(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 hash value of the input arguments. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

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

```
┌─────FarmFingerprint─┬─type───┐
│ 5752020380710916328 │ UInt64 │
└─────────────────────┴────────┘
```

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

Introduced in: v1.1.0

Produces a 64-bit [FarmHash](https://github.com/google/farmhash) using the `Hash64` method.

Tip

[`farmFingerprint64`](#farmFingerprint64) is preferred for a stable and portable value.

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

```
farmHash64(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 hash value of the input arguments. [`UInt64`](/docs/sql-reference/data-types/int-uint)

**Examples**

**Usage example**

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

```
┌─────────────FarmHash─┬─type───┐
│ 18125596431186471178 │ UInt64 │
└──────────────────────┴────────┘
```

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

Introduced in: v20.1.0

Computes the 64-bit [MurmurHash2](https://github.com/aappleby/smhasher) hash of the input value using the same seed as used by [GCC](https://github.com/gcc-mirror/gcc/blob/41d6b10e96a1de98e90a7c0378437c3255814b16/libstdc%2B%2B-v3/include/bits/functional_hash.h#L191).

It is portable between Clang and GCC builds.

**Syntax**

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

**Arguments**

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

**Returned value**

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

**Examples**

**Usage example**

```
SELECT
    gccMurmurHash(1, 2, 3) AS res1,
    gccMurmurHash(('a', [1, 2, 3], 4, (4, ['foo', 'bar'], 1, (1, 2)))) AS res2
```

```
┌─────────────────res1─┬────────────────res2─┐
│ 12384823029245979431 │ 1188926775431157506 │
└──────────────────────┴─────────────────────┘
```

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

Introduced in: v1.1.0