hash
This page explains how to use the hash function in APL.
Use the hash scalar function to transform any data type as a string of bytes into a signed integer. The result is deterministic so the value is always identical given the same input data.
Use the hash function to:
- Anonymise personally identifiable information (PII) while preserving joinability.
- Create reproducible buckets for sampling, sharding, or load-balancing.
- Build low-cardinality keys for fast aggregation and look-ups.
- You need a reversible-by-key surrogate or a quick way to distribute rows evenly.
Usage
Syntax
Parameters
| Name | Type | Description |
|---|---|---|
source | scalar | The value to hash. Accepts any scalar type, including real. Doesn’t accept dynamic arrays or property bags. |
mod | int | (Optional) A modulo to apply to the hash. If mod is greater than 0, hash returns the absolute value of the hash modulo mod. If you omit mod or set it to 0 or less, hash returns the full hash. |
Returns
If you omit mod, a signed 64-bit integer hash of source. If mod is greater than 0, an integer between 0 and mod - 1. For example, hash(id, 2) returns either 0 or 1.
Use case examples
Hash requesters to see your busiest anonymous users.
Query
Output
| anon_id | requests |
|---|---|
| -5872831405421830129 | 128 |
| 902175364502087611 | 97 |
| -354879610945237854 | 85 |
| 6423087105927348713 | 74 |
| -919087345721004317 | 69 |
The query replaces raw IDs with hashed surrogates, counts requests per surrogate, then lists the five most active requesters without exposing PII.
Hash trace IDs to see which anonymous trace has the most spans.
Query
Output
| trace_bucket | spans |
|---|---|
| 8,858,860,617,655,667,000 | 62 |
| 4,193,515,424,067,409,000 | 62 |
| 1,779,014,838,419,064,000 | 62 |
| 5,399,024,001,804,211,000 | 62 |
| -2,480,347,067,347,939,000 | 62 |
Group suspicious endpoints without leaking the exact URI.
Query
Output
| uri_hash | status | requests |
|---|---|---|
| -123640987553821047 | 404 | 230 |
| 4385902145098764321 | 403 | 145 |
| -85439034872109873 | 401 | 132 |
| 493820743209857311 | 404 | 129 |
| -90348122345872001 | 500 | 118 |
The query hides sensitive path information yet still lets you see which hashed endpoints return the most errors.