Skip to content

Apache DataSketches

Bring Apache DataSketches into DuckDB.

14,842,446
extension loads ยท last 90 days
On this page

Technical Overview

Billion-row aggregates on a bounded memory budget

How sketches work

  • โ€ข Bounded memory, single pass: A sketch's footprint is fixed up front โ€” it does not grow with the number of input rows. A billion-row distinct count that would otherwise need a sort plus a large hash table becomes a constant-memory aggregate computed in one scan, with mathematically grounded error bounds rather than ad-hoc approximation.
  • โ€ข The size โ†” accuracy knob: Each family takes a sizing parameter โ€” lg_k for the cardinality sketches (HLL/CPC/Theta), K for the quantile sketches (KLL/TDigest/REQ/classic), lg_max_map_size for Frequent Items. Larger means tighter error and a bigger sketch; smaller means a cheaper sketch and looser bounds. For example, an HLL at lg_k = 12 is roughly 4 KB at about ยฑ1.6% standard error. You pick the point on that curve that matches your accuracy budget.
  • โ€ข State is mergeable without rescanning: The serialized sketch is the durable, composable unit. Build one sketch per partition or per day, persist it as a typed BLOB column, then union those sketches into any rolling window (per-day โ†’ 7d/30d, per-shard โ†’ global) without ever touching the original rows again. Theta sketches go further, supporting approximate union, intersection, and A-not-B set algebra over sketch state alone โ€” the basis for funnel, retention, and churn analysis.
  • โ€ข Portable across systems: The serialization format matches the Java reference implementation โ€” the same format consumed by Druid, Pinot, BigQuery, and Spark integrations. A sketch built in DuckDB and written to Parquet can be merged inside another DataSketches-aware system, and vice versa, with no re-aggregation across the boundary.

When to reach for sketches vs. exact aggregates

Deep Dive

Technical Details

Install

INSTALL datasketches FROM community;
LOAD datasketches;

Quick Start

Approximate distinct users with HLL (lg_k = 12 โ†’ ~4 KB sketch, ~1.6% error)

SELECT datasketch_hll_estimate(datasketch_hll(12, user_id)) AS distinct_users
FROM events;

p50 / p95 / p99 latency from a KLL quantile sketch

WITH agg AS (
  SELECT datasketch_kll(200, latency_ms) AS sketch FROM requests
)
SELECT
  datasketch_kll_quantile(sketch, 0.50, true) AS p50,
  datasketch_kll_quantile(sketch, 0.95, true) AS p95,
  datasketch_kll_quantile(sketch, 0.99, true) AS p99
FROM agg;

Reference

Extension Contents

Quick reference to all available functions and settings organized by category.

Name Description
CPC
datasketch_cpc() Aggregate input values into a Compressed Probability Counting sketch โ€” a distinct-count sketch that's roughly 40% smaller than HLL at the same accuracy, traded against slower serialization.
datasketch_cpc_describe() Return a string representation of the sketch
datasketch_cpc_estimate() Read the estimated distinct count from a CPC sketch.
datasketch_cpc_is_empty() Return a boolean indicating if the sketch is empty
datasketch_cpc_lower_bound() Return the lower bound of the number of distinct items seen by the sketch
datasketch_cpc_union() Merge a column of sketch_cpc BLOBs into one rollup sketch โ€” the standard per-partition / per-day rollup pattern.
datasketch_cpc_upper_bound() Return the upper bound of the number of distinct items seen by the sketch
Frequent Items
datasketch_frequent_items() Aggregate input values into a Frequent Items (heavy-hitter) sketch.
datasketch_frequent_items_epsilon() Returns the epsilon value (relative error) of the sketch
datasketch_frequent_items_estimate() Estimated frequency for a specific item.
datasketch_frequent_items_get_frequent() Return the heavy-hitter candidates with per-item estimate, lower bound, and upper bound.
datasketch_frequent_items_is_empty() Returns true if the sketch is empty
datasketch_frequent_items_lower_bound() Returns the lower bound frequency estimate for a specific item
datasketch_frequent_items_num_active() Returns the number of active items currently tracked by the sketch
datasketch_frequent_items_total_weight() Returns the total weight (sum of all item counts) processed by the sketch
datasketch_frequent_items_upper_bound() Returns the upper bound frequency estimate for a specific item
HLL
datasketch_hll() Aggregate input values into a HyperLogLog sketch for distinct counting.
datasketch_hll_describe() Return a string representation of the sketch
datasketch_hll_estimate() Read the estimated distinct count from an HLL sketch.
datasketch_hll_is_compact() Return whether the sketch is in compact form
datasketch_hll_is_empty() Return a boolean indicating if the sketch is empty
datasketch_hll_lg_config_k() Return the value of log base 2 K for this sketch
datasketch_hll_lower_bound() Lower bound of the HLL distinct-count estimate at a given number of standard deviations.
datasketch_hll_union() Merge multiple HLL sketches into one.
datasketch_hll_upper_bound() Upper bound of the HLL distinct-count estimate at a given number of standard deviations.
KLL
datasketch_kll() Aggregate input values into a KLL quantile sketch.
datasketch_kll_cdf() CDF over a list of split points โ€” one call returns the cumulative rank at each.
datasketch_kll_describe() Return a description of this sketch
datasketch_kll_is_empty() Return a boolean indicating if the sketch is empty
datasketch_kll_is_estimation_mode() Return a boolean indicating if the sketch is in estimation mode
datasketch_kll_k() Return the value of K for this sketch
datasketch_kll_max_item() Return the maxium item in the sketch
datasketch_kll_min_item() Return the minimum item in the sketch
datasketch_kll_n() Return the number of items contained in the sketch
datasketch_kll_normalized_rank_error() Return the normalized rank error of the sketch
datasketch_kll_num_retained() Return the number of retained items in the sketch
datasketch_kll_pmf() PMF (probability mass) over a list of split points โ€” fraction of the distribution falling in each bucket.
datasketch_kll_quantile() Approximate quantile at a given rank โ€” given a sketch and r โˆˆ [0, 1], returns the value at that rank in the sorted distribution.
datasketch_kll_rank() Inverse of datasketch_kll_quantile โ€” given a value, return its approximate rank r โˆˆ [0, 1] in the sorted distribution.
Quantiles
datasketch_quantiles() Aggregate input values into the classic mergeable quantiles sketch from the original DataSketches paper.
datasketch_quantiles_cdf() Return the Cumulative Distribution Function (CDF) of the sketch for a series of points
datasketch_quantiles_describe() Return a description of this sketch
datasketch_quantiles_is_empty() Return a boolean indicating if the sketch is empty
datasketch_quantiles_is_estimation_mode() Return a boolean indicating if the sketch is in estimation mode
datasketch_quantiles_k() Return the value of K for this sketch
datasketch_quantiles_max_item() Return the maxium item in the sketch
datasketch_quantiles_min_item() Return the minimum item in the sketch
datasketch_quantiles_n() Return the number of items contained in the sketch
datasketch_quantiles_normalized_rank_error() Return the normalized rank error of the sketch
datasketch_quantiles_num_retained() Return the number of retained items in the sketch
datasketch_quantiles_pmf() Return the Probability Mass Function (PMF) of the sketch for a series of points
datasketch_quantiles_quantile() Approximate quantile at a given rank from a classic Quantiles sketch.
datasketch_quantiles_rank() Approximate rank of a value within the classic Quantiles sketch.
REQ
datasketch_req() Aggregate input values into a Relative Error Quantile sketch.
datasketch_req_cdf() Return the Cumulative Distribution Function (CDF) of the sketch for a series of points
datasketch_req_describe() Return a description of this sketch
datasketch_req_is_empty() Return a boolean indicating if the sketch is empty
datasketch_req_is_estimation_mode() Return a boolean indicating if the sketch is in estimation mode
datasketch_req_k() Return the value of K for this sketch
datasketch_req_max_item() Return the maxium item in the sketch
datasketch_req_min_item() Return the minimum item in the sketch
datasketch_req_n() Return the number of items contained in the sketch
datasketch_req_num_retained() Return the number of retained items in the sketch
datasketch_req_pmf() Return the Probability Mass Function (PMF) of the sketch for a series of points
datasketch_req_quantile() Approximate quantile at a given rank from a REQ sketch.
datasketch_req_rank() Approximate rank of a value within the REQ sketch.
TDigest
datasketch_tdigest() Aggregate input values into a t-digest quantile sketch โ€” most accurate at the tails (p99, p999), exactly where SLOs live.
datasketch_tdigest_cdf() Return the Cumulative Distribution Function (CDF) of the sketch for a series of points
datasketch_tdigest_describe() Return a description of this sketch
datasketch_tdigest_is_empty() Return a boolean indicating if the sketch is empty
datasketch_tdigest_k() Return the value of K for this sketch
datasketch_tdigest_pmf() Return the Probability Mass Function (PMF) of the sketch for a series of points
datasketch_tdigest_quantile() Approximate quantile at a given rank from a t-digest sketch.
datasketch_tdigest_rank() Approximate rank of a value within the t-digest sketch.
datasketch_tdigest_total_weight() Return the total weight of this sketch
Theta
datasketch_theta() Aggregate input values into a Theta sketch โ€” the distinct-count family that supports set operations (union, intersect, A-not-B) beyond simple merge.
datasketch_theta_a_not_b() Approximate |A \ B| โ€” distinct items present in A but not in B.
datasketch_theta_describe() Returns a human-readable description of the Theta sketch
datasketch_theta_estimate() Read the estimated distinct count from a Theta sketch.
datasketch_theta_get_seed() Returns the seed hash used by the sketch
datasketch_theta_get_theta() Returns the theta value of the sketch (sampling probability)
datasketch_theta_intersect() Approximate |A โˆฉ B| โ€” distinct items present in both cohorts.
datasketch_theta_is_empty() Returns true if the Theta sketch is empty
datasketch_theta_is_estimation_mode() Returns true if the sketch is in estimation mode (has exceeded exact counting capacity)
datasketch_theta_lower_bound() Returns the lower bound estimate at the given number of standard deviations (1, 2, or 3)
datasketch_theta_num_retained() Returns the number of hash values retained in the sketch
datasketch_theta_union() Approximate |A โˆช B| โ€” distinct count across the union of two cohorts.
datasketch_theta_upper_bound() Returns the upper bound estimate at the given number of standard deviations (1, 2, or 3)
Types
sketch_cpc Logical type registered by this extension.
sketch_frequent_items Logical type registered by this extension.
sketch_hll Logical type registered by this extension.
sketch_kll_bigint Logical type registered by this extension.
sketch_kll_double Logical type registered by this extension.
sketch_kll_float Logical type registered by this extension.
sketch_kll_integer Logical type registered by this extension.
sketch_kll_smallint Logical type registered by this extension.
sketch_kll_tinyint Logical type registered by this extension.
sketch_kll_ubigint Logical type registered by this extension.
sketch_kll_uinteger Logical type registered by this extension.
sketch_kll_usmallint Logical type registered by this extension.
sketch_kll_utinyint Logical type registered by this extension.
sketch_quantiles_bigint Logical type registered by this extension.
sketch_quantiles_double Logical type registered by this extension.
sketch_quantiles_float Logical type registered by this extension.
sketch_quantiles_integer Logical type registered by this extension.
sketch_quantiles_smallint Logical type registered by this extension.
sketch_quantiles_tinyint Logical type registered by this extension.
sketch_quantiles_ubigint Logical type registered by this extension.
sketch_quantiles_uinteger Logical type registered by this extension.
sketch_quantiles_usmallint Logical type registered by this extension.
sketch_quantiles_utinyint Logical type registered by this extension.
sketch_req_bigint Logical type registered by this extension.
sketch_req_double Logical type registered by this extension.
sketch_req_float Logical type registered by this extension.
sketch_req_integer Logical type registered by this extension.
sketch_req_smallint Logical type registered by this extension.
sketch_req_tinyint Logical type registered by this extension.
sketch_req_ubigint Logical type registered by this extension.
sketch_req_uinteger Logical type registered by this extension.
sketch_req_usmallint Logical type registered by this extension.
sketch_req_utinyint Logical type registered by this extension.
sketch_tdigest_double Logical type registered by this extension.
sketch_tdigest_float Logical type registered by this extension.
sketch_theta Logical type registered by this extension.

API Reference

Function Documentation

Data model

Registered Types

Logical types this extension adds to DuckDB. Functions in the reference may accept or return these names directly.

  • sketch_cpc

    Logical type registered by this extension.

  • sketch_frequent_items

    Logical type registered by this extension.

  • sketch_hll

    Logical type registered by this extension.

  • sketch_kll_bigint

    Logical type registered by this extension.

  • sketch_kll_double

    Logical type registered by this extension.

  • sketch_kll_float

    Logical type registered by this extension.

  • sketch_kll_integer

    Logical type registered by this extension.

  • sketch_kll_smallint

    Logical type registered by this extension.

  • sketch_kll_tinyint

    Logical type registered by this extension.

  • sketch_kll_ubigint

    Logical type registered by this extension.

  • sketch_kll_uinteger

    Logical type registered by this extension.

  • sketch_kll_usmallint

    Logical type registered by this extension.

  • sketch_kll_utinyint

    Logical type registered by this extension.

  • sketch_quantiles_bigint

    Logical type registered by this extension.

  • sketch_quantiles_double

    Logical type registered by this extension.

  • sketch_quantiles_float

    Logical type registered by this extension.

  • sketch_quantiles_integer

    Logical type registered by this extension.

  • sketch_quantiles_smallint

    Logical type registered by this extension.

  • sketch_quantiles_tinyint

    Logical type registered by this extension.

  • sketch_quantiles_ubigint

    Logical type registered by this extension.

  • sketch_quantiles_uinteger

    Logical type registered by this extension.

  • sketch_quantiles_usmallint

    Logical type registered by this extension.

  • sketch_quantiles_utinyint

    Logical type registered by this extension.

  • sketch_req_bigint

    Logical type registered by this extension.

  • sketch_req_double

    Logical type registered by this extension.

  • sketch_req_float

    Logical type registered by this extension.

  • sketch_req_integer

    Logical type registered by this extension.

  • sketch_req_smallint

    Logical type registered by this extension.

  • sketch_req_tinyint

    Logical type registered by this extension.

  • sketch_req_ubigint

    Logical type registered by this extension.

  • sketch_req_uinteger

    Logical type registered by this extension.

  • sketch_req_usmallint

    Logical type registered by this extension.

  • sketch_req_utinyint

    Logical type registered by this extension.

  • sketch_tdigest_double

    Logical type registered by this extension.

  • sketch_tdigest_float

    Logical type registered by this extension.

  • sketch_theta

    Logical type registered by this extension.

Practical Examples

Cookbook

Real-world recipes and patterns for common use cases.

Platform Support

Compatibility

Extension availability may vary by platform and DuckDB version. Check below to ensure this extension supports your environment before installation.

Quick Facts

Release status Stable
Software License Apache-2.0
Pricing Free
Written In C++
Source Available Yes
View on GitHub
Usage
14,842,446
loads ยท last 90 days

Platforms

  • Linux x86_64 aarch64
  • Linux (musl) Not available
  • macOS Intel Apple Silicon
  • Windows x86_64
  • WASM eh mvp threads
Compiled binary sizes
Platform Architecture Size
Linux x86_64 4.46 MB
Linux aarch64 3.93 MB
macOS Intel 3.26 MB
macOS Apple Silicon 2.88 MB
Windows x86_64 8.21 MB
WASM eh 612.6 KB
WASM mvp 556.0 KB
WASM threads 611.7 KB

Compressed download size from the Haybarn extension repository.

DuckDB & Haybarn

Release calendar