HASH

Compute a non-cryptographic 32-bit hash of one expression.

Category: hashReturns: INTEGERDialect: Standard

Syntax

HASH(expr)

Description

## Overview Computes a non-cryptographic 32-bit hash of the input expression and returns the result as a signed INTEGER. The function is deterministic: the same input always produces the same output within a session. Use HASH for data distribution, bucketing, and change detection. HASH is not cryptographically secure. Collisions are easy to find and the output space is only 2^32, so at scale accidental collisions are expected. For security use SHA2; for better distribution at scale use XXHASH64. ## Behavior - Returns an INTEGER (signed 32-bit). - NULL inputs produce a deterministic hash, not NULL. - Output is stable across calls with the same input within an engine version. - Accepts most scalar data types and common composite types. ## Algorithm - Engine-defined non-cryptographic hash, 32-bit output. - Not compatible with HASH outputs from other SQL engines: the seed and mix function vary across implementations. ## Compatibility - The HASH name is used across many SQL engines with different underlying algorithms. Hash values are not portable across engines; do not persist HASH output expecting cross-engine consistency.

Parameters

NameTypeDescription
exprSpecifies a single expression of any supported data type to hash. Both NULL and non-NULL values participate; the NULL case produces a deterministic hash.

Examples

-- Hash an integer
SELECT HASH(42) AS h;
-- Hash a string
SELECT HASH('hello') AS h;
-- Composite key via CONCAT
SELECT HASH(CONCAT_WS('|', first_name, last_name, CAST(dob AS STRING))) AS row_hash
FROM (VALUES ('Alice', 'Smith', DATE '1990-01-15')) AS t(first_name, last_name, dob);
-- NULL produces a deterministic hash
SELECT HASH(NULL) AS h;
-- Use for modulo bucketing
SELECT val, ABS(HASH(val)) % 8 AS bucket
FROM (VALUES ('a'), ('b'), ('c'), ('d')) AS t(val);
-- Realistic: distribute a wide join key
SELECT ABS(HASH(customer_id)) % 32 AS shard, customer_id
FROM ecommerce.sales.orders;

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →