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.2.0.tar.gz (178.0 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.2.0-cp39-abi3-manylinux_2_34_x86_64.whl (296.4 kB view details)

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

File details

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

File metadata

  • Download URL: samkhya-1.2.0.tar.gz
  • Upload date:
  • Size: 178.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for samkhya-1.2.0.tar.gz
Algorithm Hash digest
SHA256 d776fb160391644c8d7e20bbd4743a6adcbf6c555de2983932f135f48aadb11e
MD5 2a7d11b5f2b06c635111022bd7f73f8b
BLAKE2b-256 e991ad7a20f7dd501553b35697634bb8c8c60c9c406e658a8467a34529675b2c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for samkhya-1.2.0-cp39-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 474941cf3f5002bedcd4fb1655efa45b50e9d6aab90e2a8c93aed04601b7f78e
MD5 9d152ae1ff73abe471a717b8cefbd197
BLAKE2b-256 6e322caa027dd7b2c1da7db3aab8e77a5c5af12537d653a4031b1d940f96ff2c

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

This release

1.2.0 This release

2 files

1.1.0

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