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.1.tar.gz (178.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.2.1-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.1.tar.gz.

File metadata

  • Download URL: samkhya-1.2.1.tar.gz
  • Upload date:
  • Size: 178.1 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.1.tar.gz
Algorithm Hash digest
SHA256 6d1ef73dd53dfbab9be9bcfd922df19ee768c9976ffe3450ecd105bc9f930c0d
MD5 6cce5bd7710a9a4f742fdc93e8e91ad0
BLAKE2b-256 e415c582f601d4c9dba2db3cbfdbf0a568035c12396193dd42381370019a97a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for samkhya-1.2.1-cp39-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 8394c11b18799af03b5220bb1cdf877656fb9305da4a3a28483c47ef8973b869
MD5 5cec1f9c87186c7184bd0b111c8e1513
BLAKE2b-256 ed992d90fa838bd3982bb56a69fed8c6b543e98710f310b2b09c086f6690e063

See more details on using hashes here.

Release history Release notifications | RSS feed

1.2.3

2 files

1.2.2

2 files

This release

1.2.1 This release

2 files

1.2.0

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