Frequent Items functions in the Apache DataSketches DuckDB extension
Sample data for the examples
Each browser example starts with these sample tables; sample files are shared across examples on this page. Run this setup first when trying the examples in your own DuckDB session.
SET VARIABLE example_today = (SELECT CURRENT_DATE);
-- Fixtures for the datasketches extension cookbook / technical-details /
-- functions.json examples. Provides the source tables that the snippets
-- reference (events, requests, page_views, measurements, …) so they can
-- actually execute under the validator instead of skipping with
-- "table fixture missing".
--
-- The validator's parse phase EXPLAINs each statement in a snippet
-- individually, so a snippet of the shape `CREATE TABLE foo …; SELECT
-- … FROM foo;` will fail to parse the SELECT (foo doesn't exist yet
-- during EXPLAIN). For the affected tables (daily_uniques, readings)
-- we pre-create them with the same column names + types the snippets
-- expect, so EXPLAIN succeeds. The snippets themselves use CREATE OR
-- REPLACE, so their own CREATE then rebuilds the table on execute.
----------------------------------------------------------------------
-- events: the catch-all source table referenced by HLL/CPC/Theta
-- examples. Carries enough columns to satisfy every reference:
-- user_id → distinct-count examples
-- ts → date_trunc('day', ts) for per-day rollups
-- country_code → frequent-items examples
-- cohort → datasketch_theta GROUP BY cohort example
----------------------------------------------------------------------
CREATE OR REPLACE TABLE events AS
SELECT
(i % 30) AS user_id,
(getvariable('example_today')::DATE + INTERVAL '-14 days')::TIMESTAMP
+ INTERVAL (i) HOUR AS ts,
(['US','GB','DE','FR','NO','SE','JP','CA','BR','AU'])[1 + (i % 10)]
AS country_code,
(['signup','active','dormant'])[1 + (i % 3)]
AS cohort
FROM range(0, 200) t(i);
----------------------------------------------------------------------
-- events_jan / events_feb: the Theta set-algebra (funnel/retention/
-- churn) example partitions users across two months. Overlapping but
-- not identical user-id ranges make the intersect / a-not-b results
-- non-trivial.
----------------------------------------------------------------------
CREATE OR REPLACE TABLE events_jan AS
SELECT (i % 50) AS user_id
FROM range(0, 200) t(i);
CREATE OR REPLACE TABLE events_feb AS
SELECT (20 + (i % 50)) AS user_id -- overlaps users 20..49 with jan
FROM range(0, 200) t(i);
----------------------------------------------------------------------
-- requests: latency_ms column for KLL / TDigest quantile examples.
-- Static deterministic values (no randomness) so sketch outputs are
-- reproducible across runs.
----------------------------------------------------------------------
CREATE OR REPLACE TABLE requests AS
SELECT (i * 7 % 1000)::INTEGER AS latency_ms
FROM range(0, 500) t(i);
----------------------------------------------------------------------
-- page_views: country_code column for the Frequent Items example.
----------------------------------------------------------------------
CREATE OR REPLACE TABLE page_views AS
SELECT
(['US','US','US','US','GB','GB','DE','FR','NO','SE','JP','CA','BR','AU','IT'])[1 + (i % 15)]
AS country_code
FROM range(0, 300) t(i);
----------------------------------------------------------------------
-- measurements: `value` column for the REQ skew-aware quantile
-- example. Mildly skewed via i*i to exercise the rank-error scaling.
----------------------------------------------------------------------
CREATE OR REPLACE TABLE measurements AS
SELECT (i * i)::DOUBLE AS value
FROM range(0, 100) t(i);
----------------------------------------------------------------------
-- daily_uniques: per-day HLL/Theta rollup table referenced by the
-- "Per-day HLL sketches" cookbook recipe, the technical-details
-- "billion-row distinct count" example, the datasketch_hll_union
-- example, and the persist-to-Parquet snippet. Schema matches what
-- the cookbook + functions.json snippets expect: (day DATE, hll
-- sketch_hll). The technical-details snippet builds its own (day,
-- sketch) shape with CREATE OR REPLACE TABLE … AS SELECT.
----------------------------------------------------------------------
CREATE OR REPLACE TABLE daily_uniques AS
SELECT
date_trunc('day', ts) AS day,
datasketch_hll(12, user_id) AS hll
FROM events
GROUP BY 1;
----------------------------------------------------------------------
-- readings: temp DOUBLE column for the TDigest cookbook examples.
-- Pre-seeded so the SELECT inside the same snippet binds at EXPLAIN
-- time. The snippet then re-creates it (CREATE OR REPLACE) with the
-- same shape on execute.
----------------------------------------------------------------------
CREATE OR REPLACE TABLE readings AS
SELECT i::DOUBLE AS temp
FROM range(1, 11) t(i);
----------------------------------------------------------------------
-- orders: per-order rows for the Tuple / array-of-doubles examples
-- (distinct customers carrying a per-customer revenue or metric vector).
----------------------------------------------------------------------
CREATE OR REPLACE TABLE orders AS
SELECT
(i % 40) AS customer_id,
(['north','south'])[1 + (i % 2)] AS region,
((i * 13) % 97 + 1)::DOUBLE AS amount,
(1 + i % 3)::DOUBLE AS items
FROM range(0, 400) t(i);
Function category
Frequent Items
10 functionsHeavy-hitter sketch that identifies the most-frequent items in a stream along with confidence-bounded frequency estimates. Use for top-N analysis on high-cardinality streams where exact GROUP BY count() is too expensive.
Signature
4 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
data
|
Type
FLOAT | UINTEGER | SMALLINT | …
11 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_frequent_items
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
lg_max_k
|
Type
INTEGER
|
Mode Positional | Description |
Argument
data
|
Type
TINYINT | VARCHAR | SMALLINT | …
11 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
lg_max_k
|
Type
INTEGER
|
Mode Positional | Description |
Argument
sketch
|
Type
sketch_frequent_items
|
Mode Positional | Description |
Description
Aggregate input values into a Frequent Items (heavy-hitter) sketch. The optional first argument is lg_max_k (default 10) — log₂ of the maximum number of tracked items, so 8 → up to 256 candidates, 12 → up to 4096. Bigger values track more candidates and tighten the per-item error bound. Valid values are 3–30 (anything else is rejected at bind time); typical values run 4–12. FLOAT and DOUBLE items are keyed by their shortest round-trip spelling (the same text as CAST(x AS VARCHAR)), so nearby values such as 0.1234567 and 0.1234568 stay distinct. Returns NULL over no rows.
SELECT datasketch_frequent_items_get_frequent(
datasketch_frequent_items(8, country_code),
'NO_FALSE_POSITIVES'
) AS heavy_hitters
FROM page_views;
Related functions
- datasketch_frequent_items_get_frequent() — Return the heavy-hitter candidates with per-item estimate, lower bound, and upper bound
- datasketch_frequent_items_estimate() — Estimated frequency for a specific item
- datasketch_frequent_items_lower_bound() — Returns the lower bound frequency estimate for a specific item
- datasketch_frequent_items_upper_bound() — Returns the upper bound frequency estimate for a specific item
- datasketch_frequent_items_weighted() — Like `datasketch_frequent_items`, but each row adds a weight (`BIGINT` or `UBIGINT`; negative weights are rejected) instead of a count of one — approximate top-K by a metric such as revenue or bytes
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_frequent_items
|
Mode Positional | Description |
Description
Returns the epsilon value (relative error) of the sketch
SELECT datasketch_frequent_items_epsilon(sketch) FROM (SELECT datasketch_frequent_items(10, (i % 10)::VARCHAR) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_frequent_items_epsilon(sketch) |
|---|
| 0.00341796875 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_frequent_items
|
Mode Positional | Description |
Argument
item
|
Type
UBIGINT | BIGINT | SMALLINT | …
11 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
|
Mode Positional | Description |
Description
Estimated frequency for a specific item. FLOAT/DOUBLE probes are formatted like CAST(x AS VARCHAR); sketches of FLOAT/DOUBLE items built by older extension versions used six-decimal keys and should be rebuilt from source data.
SELECT datasketch_frequent_items_estimate(sketch, '3') FROM (SELECT datasketch_frequent_items(10, (i % 10)::VARCHAR) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_frequent_items_estimate(sketch, '3') |
|---|
| 100 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_frequent_items
|
Mode Positional | Description |
Argument
error_type
|
Type
VARCHAR
|
Mode Positional | Description |
Description
Return the heavy-hitter candidates with per-item estimate, lower bound, and upper bound. The second argument selects the error mode: 'NO_FALSE_POSITIVES' returns only items that are definitely heavy (every returned item really is frequent); 'NO_FALSE_NEGATIVES' returns the union of all candidates that might be — every truly-frequent item is included, plus some that may not be. The error type is case-insensitive; any other value is an error.
SELECT datasketch_frequent_items_get_frequent(sketch, 'NO_FALSE_POSITIVES') FROM (SELECT datasketch_frequent_items(10, (i % 10)::VARCHAR) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_frequent_items_get_frequent(sketch, 'NO_FALSE_POSITIVES') |
|---|
| [{'item': '7', 'estimate': 100, 'lower_bound': 100, 'upper_bound': 100}, {'item': '8', 'estimate': 100, 'lower_bound': 100, 'upper_bound': 100}, {'item': '0', 'estimate': 100, 'lower_bound': 100, 'upper_bound': 100}, {'item': '9', 'estimate': 100, 'lower_bound': 100, 'upper_bound': 100}, {'item': '6', 'estimate': 100, 'lower_bound': 100, 'upper_bound': 100}, {'item': '2', 'estimate': 100, 'lower_bound': 100, 'upper_bound': 100}, {'item': '1', 'estimate': 100, 'lower_bound': 100, 'upper_bound': 100}, {'item': '3', 'estimate': 100, 'lower_bound': 100, 'upper_bound': 100}, {'item': '4', 'estimate': 100, 'lower_bound': 100, 'upper_bound': 100}, {'item': '5', 'estimate': 100, 'lower_bound': 100, 'upper_bound': 100}] |
Related functions
- datasketch_frequent_items() — Aggregate input values into a Frequent Items (heavy-hitter) sketch
- datasketch_frequent_items_estimate() — Estimated frequency for a specific item
- datasketch_frequent_items_lower_bound() — Returns the lower bound frequency estimate for a specific item
- datasketch_frequent_items_upper_bound() — Returns the upper bound frequency estimate for a specific item
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_frequent_items
|
Mode Positional | Description |
Description
Returns true if the sketch is empty
SELECT datasketch_frequent_items_is_empty(sketch) FROM (SELECT datasketch_frequent_items(10, (i % 10)::VARCHAR) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_frequent_items_is_empty(sketch) |
|---|
| false |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_frequent_items
|
Mode Positional | Description |
Argument
item
|
Type
TINYINT | FLOAT | UTINYINT | …
11 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
|
Mode Positional | Description |
Description
Returns the lower bound frequency estimate for a specific item
SELECT datasketch_frequent_items_lower_bound(sketch, '3') FROM (SELECT datasketch_frequent_items(10, (i % 10)::VARCHAR) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_frequent_items_lower_bound(sketch, '3') |
|---|
| 100 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_frequent_items
|
Mode Positional | Description |
Description
Returns the number of active items currently tracked by the sketch
SELECT datasketch_frequent_items_num_active(sketch) FROM (SELECT datasketch_frequent_items(10, (i % 10)::VARCHAR) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_frequent_items_num_active(sketch) |
|---|
| 10 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_frequent_items
|
Mode Positional | Description |
Description
Returns the total weight (sum of all item counts) processed by the sketch
SELECT datasketch_frequent_items_total_weight(sketch) FROM (SELECT datasketch_frequent_items(10, (i % 10)::VARCHAR) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_frequent_items_total_weight(sketch) |
|---|
| 1000 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_frequent_items
|
Mode Positional | Description |
Argument
item
|
Type
UTINYINT | SMALLINT | INTEGER | …
11 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
|
Mode Positional | Description |
Description
Returns the upper bound frequency estimate for a specific item
SELECT datasketch_frequent_items_upper_bound(sketch, '3') FROM (SELECT datasketch_frequent_items(10, (i % 10)::VARCHAR) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_frequent_items_upper_bound(sketch, '3') |
|---|
| 100 |
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
data
|
Type
UTINYINT | USMALLINT | UBIGINT | …
11 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
|
Mode Positional | Description |
Argument
weight
|
Type
<numeric>
2 concrete typesBIGINTUBIGINT
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
lg_max_k
|
Type
INTEGER
|
Mode Positional | Description |
Argument
data
|
Type
INTEGER | BIGINT | TINYINT | …
11 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
|
Mode Positional | Description |
Argument
weight
|
Type
<numeric>
2 concrete typesBIGINTUBIGINT
|
Mode Positional | Description |
Description
Like datasketch_frequent_items, but each row adds a weight (BIGINT or UBIGINT; negative weights are rejected) instead of a count of one — approximate top-K by a metric such as revenue or bytes. The optional leading lg_max_k (log₂ of the maximum number of tracked items, default 10, valid range 3–30) works as in datasketch_frequent_items. Returns NULL over no rows.
SELECT datasketch_frequent_items_get_frequent(
datasketch_frequent_items_weighted(8, region, amount::BIGINT),
'NO_FALSE_POSITIVES'
) AS by_revenue
FROM orders;