Skip to main content

samkhya — Python bindings

Portable, feedback-driven cardinality correction for embedded analytical engines (DuckDB, Polars, DataFusion, gpudb).

This wheel exposes samkhya-core's classical sketches (HyperLogLog, Bloom, Count-Min, equi-depth histogram) and its LpBound ceiling helpers to Python, with no Rust toolchain required at install time.

Built on top of PyO3 with a stable-ABI (abi3-py39) wheel — one wheel per platform serves every CPython 3.9+ interpreter.

Install

pip install samkhya

For a from-source build of this directory:

pip install maturin
maturin develop --release            # editable install into the current venv
maturin build --release              # produce a redistributable wheel

Quickstart — count 1000 distinct items, get back ~42 for a small set

import samkhya

# Precision 14 gives 2^14 = 16384 registers; relative error ~ 0.8%.
hll = samkhya.HllSketch(14)
for i in range(1000):
    hll.add(str(i).encode("utf-8"))

print(f"~1000 → {hll.estimate():.0f}")

# A second sketch over the first 42 distinct items returns ~42.
small = samkhya.HllSketch(14)
for i in range(42):
    small.add(str(i).encode("utf-8"))
print(f"~42 → {small.estimate():.0f}")

# Sketches are mergeable and serialisable for transport (e.g. Iceberg Puffin).
hll.merge(small)
payload: bytes = hll.to_bytes()
restored = samkhya.HllSketch.from_bytes(payload)
assert restored.estimate() == hll.estimate()

The same API style applies to BloomFilter, CountMinSketch, and EquiDepthHistogram — see the type stubs in python/samkhya/__init__.pyi for full signatures.

LpBound — keep corrected estimates honest

Every corrected cardinality estimate samkhya emits is clamped from above by a provable pessimistic ceiling derived from the AGM / fractional-edge-cover bound (Atserias–Grohe–Marx; extended to ℓp-norms by Zhang et al., SIGMOD 2025 Best Paper). The Python wheel exposes two ceiling helpers that operate on plain row-count and selectivity inputs:

import samkhya

# Cartesian-product safety floor for three relations.
print(samkhya.product_bound([1_000, 2_000, 3_000]))   # 6_000_000_000.0

# Selectivity-weighted AGM ceiling for an equi-join graph.
# joins: list of (left_idx, right_idx, predicate_selectivity)
rows = [1_000_000, 1_000_000]
joins = [(0, 1, 1e-5)]
print(samkhya.agm_bound(joins, rows))                 # ~ 10_000.0

product_bound is the trivial worst case; agm_bound collapses the ceiling using the supplied predicate selectivities. Cold-start plans are always either the native estimate or the ceiling — whichever is tighter — and never degrade below baseline.

Errors

Recoverable errors from the core (out-of-range sketch parameters, malformed serialised payloads, etc.) surface as samkhya.SamkhyaError, a subclass of the built-in Exception:

try:
    samkhya.HllSketch(3)               # precision must be in [4, 18]
except samkhya.SamkhyaError as exc:
    print("rejected:", exc)

License

Apache-2.0. See the workspace README for the broader samkhya project layout and the Rust crate documentation.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

samkhya-1.1.0.tar.gz (157.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

samkhya-1.1.0-cp39-abi3-manylinux_2_34_x86_64.whl (278.1 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.34+ x86-64

File details

Details for the file samkhya-1.1.0.tar.gz.

File metadata

  • Download URL: samkhya-1.1.0.tar.gz
  • Upload date:
  • Size: 157.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.14.1

File hashes

Hashes for samkhya-1.1.0.tar.gz
Algorithm Hash digest
SHA256 1501659e9b67c1394076ad0cee2e515f3828856b006437fe6f5ae8f720f5997a
MD5 bde7beb84029d2d2668790fac48a91f3
BLAKE2b-256 b6978d5d023ae945509463cc04d4e654b12072a70d6b9230404e3b96ae1c5744

See more details on using hashes here.

File details

Details for the file samkhya-1.1.0-cp39-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for samkhya-1.1.0-cp39-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 18b780f1065b16274ba272cb72566d72bb1201b168917b50d080112154486250
MD5 c8ee66724076848f595c484d3c218ce7
BLAKE2b-256 307c42b10333c9c97f7532cf0ca56a5af0e89827724b0ea17adb643a8f96348b

See more details on using hashes here.

Release history Release notifications | RSS feed

1.2.3

2 files

1.2.2

2 files

1.2.1

2 files

1.2.0

2 files

This release

1.1.0 This release

2 files

1.0.0

1 file

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page