BigQuery · Utility function
BigQuery FARM_FINGERPRINT
FARM_FINGERPRINT(value) hashes a STRING or BYTES value to a signed INT64 that never changes between runs. Use it for repeatable sampling, A/B assignment, sharding and compact surrogate keys, usually as ABS(MOD(FARM_FINGERPRINT(id), n)). It is fast but not cryptographic.
FARM_FINGERPRINT computes FarmHash Fingerprint64, an open-source 64-bit hash, and returns it as a signed INT64 spread over the whole range, negative values included. The same input always gives the same output, across runs and query engine versions, which makes it the standard tool for deterministic bucketing: ABS(MOD(FARM_FINGERPRINT(user_id), 100)) < 10 selects the same 10% of users every time.
Two details trip people up. The result can be negative, so MOD(FARM_FINGERPRINT(x), 10) ranges from -9 to 9 and needs ABS. And ABS belongs outside MOD: ABS(FARM_FINGERPRINT(x)) raises an integer overflow error if the hash is ever the smallest INT64. The input must be STRING or BYTES, so cast numbers first, and remember that CONCAT returns NULL when any part is NULL, which makes the fingerprint NULL.
Use FARM_FINGERPRINT for sharding, sampling, experiment assignment and INT64 surrogate keys. It isn't a cryptographic hash, so don't use it for security checks or to protect personal data; use SHA256 there. With 64 bits, the GoogleSQL reference notes that collision probability rises once you hash more than about four billion (2^32) distinct values.
Syntax
FARM_FINGERPRINT(value)Arguments and return value
| Argument | Description |
|---|---|
value | A STRING or BYTES value. Cast numbers and dates to STRING first. |
Returns: INT64, spread uniformly over the full signed range. NULL for NULL input.
Examples
Fingerprints are signed INT64 values
The same strings return the same numbers in every project and on every run. About half of all fingerprints are negative.
SELECT
FARM_FINGERPRINT('user_1001') AS fp_user_1001,
FARM_FINGERPRINT('user_1002') AS fp_user_1002,
FARM_FINGERPRINT(CAST(1001 AS STRING)) AS fp_from_int64;| fp_user_1001 | fp_user_1002 | fp_from_int64 |
|---|---|---|
| 3030719907069016545 | -6807041869636952309 | -4304066633937632911 |
A repeatable 10% sample
Hash bucket 0 of 10 holds 93 of these 1,000 users. The membership never changes between runs, unlike RAND() < 0.1.
WITH users AS (
SELECT CONCAT('user_', CAST(n AS STRING)) AS user_id
FROM UNNEST(GENERATE_ARRAY(1, 1000)) AS n
)
SELECT COUNT(*) AS sampled_users
FROM users
WHERE ABS(MOD(FARM_FINGERPRINT(user_id), 10)) = 0;| sampled_users |
|---|
| 93 |
Stable A/B assignment with a salt
Prefixing the experiment name gives each experiment its own independent split. A user keeps the same variant for as long as the prefix stays the same.
WITH users AS (
SELECT user_id FROM UNNEST(['u-101', 'u-102', 'u-103', 'u-104', 'u-105']) AS user_id
)
SELECT
user_id,
IF(ABS(MOD(FARM_FINGERPRINT(CONCAT('checkout_v2:', user_id)), 2)) = 0, 'control', 'treatment') AS variant
FROM users
ORDER BY user_id;| user_id | variant |
|---|---|
| u-101 | treatment |
| u-102 | control |
| u-103 | treatment |
| u-104 | control |
| u-105 | control |
Composite keys and NULL parts
CONCAT returns NULL if any part is NULL, so the naive key is NULL for line 2. IFNULL keeps the key defined, and the | separator stops different parts from forming the same string.
WITH order_lines AS (
SELECT 'A-1' AS order_id, 1 AS line_no, 'SKU-9' AS sku UNION ALL
SELECT 'A-1', 2, NULL
)
SELECT
order_id,
line_no,
FARM_FINGERPRINT(CONCAT(order_id, '|', CAST(line_no AS STRING), '|', sku)) AS naive_key,
FARM_FINGERPRINT(CONCAT(order_id, '|', CAST(line_no AS STRING), '|', IFNULL(sku, ''))) AS line_key
FROM order_lines
ORDER BY line_no;| order_id | line_no | naive_key | line_key |
|---|---|---|---|
| A-1 | 1 | 223149423443819341 | 223149423443819341 |
| A-1 | 2 | NULL | -7042985435527517335 |
Common mistakes
- Taking ABS before MOD.
MOD(ABS(FARM_FINGERPRINT(id)), n)can fail with an integer overflow on the INT64 minimum; useABS(MOD(FARM_FINGERPRINT(id), n)). - Forgetting negative values. Without ABS,
MOD(FARM_FINGERPRINT(id), 100) < 10keeps about 55% of rows instead of 10%, because every negative remainder is below 10. - Hashing numbers directly.
FARM_FINGERPRINT(123)fails with No matching signature for function FARM_FINGERPRINT; useFARM_FINGERPRINT(CAST(123 AS STRING)). - Building keys with CONCAT over nullable columns. Any NULL part makes the key NULL; wrap parts in IFNULL and put a separator between them.
- Using it to pseudonymize emails or other personal data. It isn't a cryptographic hash; use SHA256 with a secret salt, or encryption.
Cost and performance
- FARM_FINGERPRINT is cheaper to compute than SHA256, and an INT64 key takes 8 logical bytes, against 34 for a SHA256 digest stored as BYTES or 66 as a hex STRING.
- Hash-based sampling reduces rows, not bytes: BigQuery still reads the full columns. To cut on-demand cost, write the sample to a table once and reuse it, or use TABLESAMPLE.
FAQ
How do I hash a column in BigQuery?
Why does FARM_FINGERPRINT return negative numbers?
It returns a signed INT64 covering the full range, so about half of all values are negative. Use ABS(MOD(FARM_FINGERPRINT(x), n)) to get buckets from 0 to n - 1.
How do I get a repeatable random sample in BigQuery?
Filter on ABS(MOD(FARM_FINGERPRINT(CAST(id AS STRING)), 100)) < 10 for 10%. Unlike RAND, the same rows come back every time, and the same IDs are selected in every table you apply it to.
Can two different values have the same FARM_FINGERPRINT?
Yes, as with any 64-bit hash. Collisions are rare for millions of keys, but the GoogleSQL reference warns that the probability rises past about four billion distinct values; use SHA256 when that matters.
Related
Errors you may hit
More hash and utility functions
All BigQuery hash and utility functions · All functions · BigQuery error messages
