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.

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

API Reference

Function Reference

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