pyspark-xxhash64
A pure-Python, dependency-free reimplementation of PySpark's
pyspark.sql.functions.xxhash64(*cols), so you can compute the exact same
row hashes Spark would produce without a JVM, without pyspark installed,
and (eventually) from Postgres.
Why this exists
pyspark.sql.functions.xxhash64 is not just the standard xxHash64 algorithm
run over your bytes. It's two layers:
- The XXH64 primitive (Cyan4973/xxHash) -- this part is generic, and
the
xxhashPyPI package (C bindings to the real xxHash library) already computes it correctly. Nothing Spark-specific here. - Spark's Catalyst type serialization -- how each SQL type gets turned
into bytes/ints before hashing, and how multiple columns and nested
values (arrays/maps/structs) get chained into one hash by feeding each
value's hash back in as the seed for the next. This part is
Spark-specific and isn't provided by any existing generic hash library
(checked:
xxhash,pyhash, ClickHouse'ssparkXxHash64only covers strings). This package implements it, ported directly fromorg.apache.spark.sql.catalyst.expressions.{HashExpression,XxHash64,XXH64}inapache/spark.
Install
pip install pyspark-xxhash64
Or, for local development:
pip install -e .
Usage
from pyspark_xxhash64 import xxhash64, types as T
# Equivalent to: df.select(F.xxhash64(F.col("name"), F.col("age")))
xxhash64(("Alice", T.StringType()), (30, T.IntegerType()))
# Custom seed, equivalent to F.xxhash64(..., F.lit(seed)) is NOT how Spark's
# seed works -- Spark's seed is fixed per-expression (default 42), not a
# literal column. Pass it as a keyword instead:
xxhash64(("Alice", T.StringType()), seed=42)
Supported types (mirroring pyspark.sql.types, see types.py): NullType,
BooleanType, ByteType, ShortType, IntegerType, LongType,
FloatType, DoubleType, DecimalType(precision, scale), StringType,
BinaryType, DateType (int days since epoch), TimestampType /
TimestampNTZType (int microseconds since epoch), DayTimeIntervalType,
YearMonthIntervalType, ArrayType, MapType, StructType.
You can pass a real pyspark.sql.types instance too -- dispatch is by class
name, not isinstance, so no hard dependency on pyspark is needed.
Not implemented
- Legacy
CalendarIntervalType(the pre-3.2, no-ANSI interval type stored as a months/days/microseconds triple). Rare in modern Spark; PRs welcome. - Collation-aware hashing for non-
UTF8_BINARYcollations (ICU sort keys). PlainStringType(UTF8_BINARY, Spark's default) is fully supported and verified against Spark's own test vectors. TimestampNanosVal(nanosecond-precision timestamps, Spark 4.x preview).
Verification
Correctness was established three ways, each covering a different layer:
- The XXH64 core (
core.py) was checked against the actual C reference implementation from Cyan4973/xxHash (compiled locally with gcc, not reimplemented from memory) across 41 vectors spanning every branch of the algorithm: 0-length input, the 32-byte block loop, 8/4/1-byte remainders, and negative/out-of-int32seeds. Seetests/test_core_vectors.py. - The Spark type layer (
hasher.py) was checked against hardcoded expected values copied verbatim out of Spark's ownHashExpressionsSuite.scalatest file (apache/spark,sql/catalyst): 4 string vectors (seed 42, e.g.xxhash64("AAA") == 3965631622972380050) and theSPARK-35113day-time/year-month interval vectors (seed 10). Seetests/test_hasher.py. - A real local Spark session. A portable JDK 17 +
pysparkwere installed and every type was compared directly againstdf.select(F.xxhash64("col"))on Spark 4.2.0: float/double including NaN and-0.0, byte/short/int/long/bool, string (incl. non-ASCII), binary, date, timestamp, decimal (both the precision<=18 long-encoding and the precision>18BigInteger-byte-array encoding, including the exact boundary at precision 18 vs 19), array (incl. null elements and empty arrays), map, struct, nested array-of-struct, multi-column hashing, and top-level null. All matched exactly. Seetests/test_spark_crosscheck.py(skipped by default -- needs a JVM +pysparkto run).
Two non-obvious things that verification run surfaced, both now documented in the test file:
- Maps: modern Spark (
spark.sql.legacy.allowHashOnMapType) refuses to hashMapTypecolumns by default with an analysis error -- it's not that the hash is undefined, Spark just won't compute it unless you opt back into the legacy behavior. This library will hash a Python dict as a map regardless; make sure that's actually what you want. - Type inference matters, exactly.
spark.createDataFrame([("Alice", 30)], [...])without an explicit schema infers a bare Python int asLongType, notIntegerType. SinceIntegerTypeandLongTypehash via different byte lengths (4 vs 8 bytes), picking the wrong one silently produces a different, still-plausible-looking hash. Always match the actual Spark column type, not the Python value's type.
Fast path: Arrow, native Rust
The pure-Python xxhash64() above processes one value at a time -- fine for
small data, a bottleneck at scale. For a whole Arrow column hashed natively
(no per-row Python overhead, ~175x faster on strings), see rust/.
No pyarrow dependency -- both directions go through the standardized
Arrow PyCapsule Interface,
so nanoarrow (much lighter than pyarrow) works just as well:
import nanoarrow as na
from pyspark_xxhash64 import arrow as fast
result = fast.xxhash64_array(na.Array(["hello", "world"], schema=na.string()))
na.Array(result).to_pylist()
(pyarrow.Array works identically if that's what you're already using --
fast.xxhash64_array doesn't care which Arrow implementation produced its
input, and its return value works with any of them too.)
Requires building the spark-xxhash64-arrow extension with maturin (see
rust/README.md); it's an optional extra, not a dependency of the base
package.
Fast path: DuckDB extension
rust/crates/spark-xxhash64-duckdb is a DuckDB loadable extension exposing
spark_xxhash64(VARCHAR) -> BIGINT in SQL, built against DuckDB's new
stable C Extension API (not the older C++ extension template):
LOAD 'spark_xxhash64.duckdb_extension';
SELECT spark_xxhash64('hello'); -- -4367754540140381902
See rust/crates/spark-xxhash64-duckdb/README.md for build steps and
scope (currently VARCHAR only).
Pre-built binaries are published to a self-hosted DuckDB custom extension
repository
on every push to main (linux/macOS/windows, amd64+arm64 where available --
see .github/workflows/duckdb-extension.yml), so no local build is required:
SET allow_unsigned_extensions = true; -- unsigned, self-hosted build
SET custom_extension_repository = 'https://bmsuisse.github.io/pyspark-xxhash64';
INSTALL spark_xxhash64;
LOAD spark_xxhash64;
Fast path: Postgres extension
rust/crates/spark-xxhash64-postgres is a Postgres extension (built with
pgrx) exposing
spark_xxhash64(text) -> bigint:
CREATE EXTENSION spark_xxhash64_postgres;
SELECT spark_xxhash64('hello'); -- -4367754540140381902
See rust/crates/spark-xxhash64-postgres/README.md for build/install
steps -- including how this was done against a real Postgres cluster
without OS root access (extracting build dependencies from .debs and
loading the extension via Postgres 18's extension_control_path/
dynamic_library_path GUCs).
Running tests
uv pip install -e '.[test]'
pytest
Metadata
Release files for pyspark-xxhash64 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyspark_xxhash64-0.1.0.tar.gz | 57.2 kB | With provenance | Details for pyspark_xxhash64-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyspark_xxhash64-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pyspark_xxhash64-0.1.0-py3-none-any.whl |
Total release size: 68.5 kB
Release files / pyspark_xxhash64-0.1.0.tar.gz
| Download URL | pyspark_xxhash64-0.1.0.tar.gz |
|---|---|
| Size | 57.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c013e180c5f27b27300f6152c2653a490788b8a77b0d2c5f4589d5ca32a4cace
|
|
BLAKE2b-256 checksum How to use checksums |
03255cbf004d7073a2dba0aeef7be7a2a5b6b83e18e209d4fb5227ed89468d21
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 1, 2026.
Transparency logRelease files / pyspark_xxhash64-0.1.0-py3-none-any.whl
| Download URL | pyspark_xxhash64-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7fa6a9a349df8a2edddeba3f7c1086a26465531d085f36f21734f618d3372f77
|
|
BLAKE2b-256 checksum How to use checksums |
684fdcc7f6cafa08b8aae24d740d8244e8a39cd3454bbfdb7074a87c1528a612
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 1, 2026.
Transparency logSecurity for pyspark-xxhash64 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for pyspark_xxhash64-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 1, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.