KLL 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
KLL
17 functionsKLL quantile sketch — modern mergeable quantile estimator. Best balance of accuracy, speed, and size for general-purpose quantile work. Default choice unless you have a specific reason to pick TDigest or REQ.
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
k
|
Type
INTEGER
|
Mode Positional | Description |
Argument
data
|
Type
TINYINT | DOUBLE | INTEGER | …
11 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
|
Mode Positional | Description |
Return type varies by input — 11 concrete types
sketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
k
|
Type
INTEGER
|
Mode Positional | Description |
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Return type varies by input — 11 concrete types
sketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
Return type varies by input — 11 concrete types
sketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
Description
Aggregate input values into a KLL quantile sketch. Best balance of accuracy, speed, and size — the modern default for general-purpose quantile estimation. The first argument K controls accuracy: higher K → smaller error and bigger sketch. K = 200 is a sensible production starting point. Accepts every numeric type and VARCHAR (strings are ordered bytewise and give a sketch_kll_varchar). K must be between 8 and 65535. Returns NULL over no rows.
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;
Related functions
- 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
- datasketch_kll_cdf() — CDF over a list of split points — one call returns the cumulative rank at each
- datasketch_kll_pmf() — PMF (probability mass) over a list of split points — fraction of the distribution falling in each bucket
- datasketch_kll_normalized_rank_error() — Return the normalized rank error of the sketch
- datasketch_kll_quantiles() — Return the quantiles (items) at a list of normalized ranks in [0, 1]
- datasketch_kll_ranks() — Return the normalized ranks of a list of items
- datasketch_kll_sorted_view() — Return the sketch's sorted view: one entry per retained item in ascending order (equal items can appear more than once), with the item's weight and the inclusive cumulative weight (the number of input items less than or equal to it; estimated once the sketch is in estimation mode)
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Argument
split_points
|
Type
FLOAT[] | USMALLINT[] | UINTEGER[] | …
11 concrete typesBIGINT[]DOUBLE[]FLOAT[]INTEGER[]SMALLINT[]TINYINT[]UBIGINT[]UINTEGER[]USMALLINT[]UTINYINT[]VARCHAR[]
|
Mode Positional | Description |
Argument
inclusive
|
Type
BOOLEAN
|
Mode Positional | Description |
Description
CDF over a list of split points — one call returns the cumulative rank at each. Useful for histograms. The list has one more entry than there are split points (the last is always 1.0). Always returns LIST(DOUBLE) regardless of the sketch's item type.
SELECT datasketch_kll_cdf(sketch, [25.0, 50.0, 75.0], true) FROM (SELECT datasketch_kll(200, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_kll_cdf(sketch, list_value(25.0, 50.0, 75.0), true) |
|---|
| [0.26, 0.51, 0.76, 1.0] |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Argument
include_levels
|
Type
BOOLEAN
|
Mode Positional | Description |
Argument
include_items
|
Type
BOOLEAN
|
Mode Positional | Description |
Description
Return a description of this sketch
SELECT datasketch_kll_describe(sketch, false, false) FROM (SELECT datasketch_kll(200, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_kll_describe(sketch, false, false) |
|---|
### KLL sketch summary: K : 200 min K : 200 M : 8 N : 100 Epsilon : 1.33% Epsilon PMF : 1.65% Empty : false Estimation mode: false Levels : 1 Sorted : false Capacity items : 200 Retained items : 100 Min item : 0 Max item : 99 ### End sketch summary |
Related functions
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Description
Return a boolean indicating if the sketch is empty
SELECT datasketch_kll_is_empty(sketch) FROM (SELECT datasketch_kll(200, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_kll_is_empty(sketch) |
|---|
| false |
Related functions
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Description
Return a boolean indicating if the sketch is in estimation mode
SELECT datasketch_kll_is_estimation_mode(sketch) FROM (SELECT datasketch_kll(200, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_kll_is_estimation_mode(sketch) |
|---|
| false |
Related functions
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Description
Return the value of K for this sketch
SELECT datasketch_kll_k(sketch) FROM (SELECT datasketch_kll(200, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_kll_k(sketch) |
|---|
| 200 |
Related functions
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Return type varies by input — 11 concrete types
BIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
Description
Return the maximum item in the sketch
SELECT datasketch_kll_max_item(sketch) FROM (SELECT datasketch_kll(200, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_kll_max_item(sketch) |
|---|
| 99.0 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Return type varies by input — 11 concrete types
BIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
Description
Return the minimum item in the sketch
SELECT datasketch_kll_min_item(sketch) FROM (SELECT datasketch_kll(200, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_kll_min_item(sketch) |
|---|
| 0.0 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Description
Return the number of items contained in the sketch
SELECT datasketch_kll_n(sketch) FROM (SELECT datasketch_kll(200, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_kll_n(sketch) |
|---|
| 100 |
Related functions
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Argument
is_pmf
|
Type
BOOLEAN
|
Mode Positional | Description |
Description
Return the normalized rank error of the sketch
SELECT datasketch_kll_normalized_rank_error(sketch, false) FROM (SELECT datasketch_kll(200, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_kll_normalized_rank_error(sketch, false) |
|---|
| 0.013294757464848584 |
Related functions
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Description
Return the number of retained items in the sketch
SELECT datasketch_kll_num_retained(sketch) FROM (SELECT datasketch_kll(200, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_kll_num_retained(sketch) |
|---|
| 100 |
Related functions
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Argument
split_points
|
Type
FLOAT[] | USMALLINT[] | TINYINT[] | …
11 concrete typesBIGINT[]DOUBLE[]FLOAT[]INTEGER[]SMALLINT[]TINYINT[]UBIGINT[]UINTEGER[]USMALLINT[]UTINYINT[]VARCHAR[]
|
Mode Positional | Description |
Argument
inclusive
|
Type
BOOLEAN
|
Mode Positional | Description |
Description
PMF (probability mass) over a list of split points — fraction of the distribution falling in each bucket. Always returns LIST(DOUBLE) regardless of the sketch's item type.
SELECT datasketch_kll_pmf(sketch, [25.0, 50.0, 75.0], true) FROM (SELECT datasketch_kll(200, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_kll_pmf(sketch, list_value(25.0, 50.0, 75.0), true) |
|---|
| [0.26, 0.25, 0.25, 0.24] |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Argument
rank
|
Type
DOUBLE
|
Mode Positional | Description |
Argument
inclusive
|
Type
BOOLEAN
|
Mode Positional | Description |
Return type varies by input — 11 concrete types
BIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
Description
Approximate quantile at a given rank — given a sketch and r ∈ [0, 1], returns the value at that rank in the sorted distribution.
SELECT datasketch_kll_quantile(sketch, 0.5, true) FROM (SELECT datasketch_kll(200, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_kll_quantile(sketch, 0.5, true) |
|---|
| 49.0 |
Related functions
- datasketch_kll() — Aggregate input values into a KLL quantile sketch
- datasketch_kll_rank() — Inverse of `datasketch_kll_quantile` — given a value, return its approximate rank `r ∈ [0, 1]` in the sorted distribution
- datasketch_kll_cdf() — CDF over a list of split points — one call returns the cumulative rank at each
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Argument
ranks
|
Type
DOUBLE[]
|
Mode Positional | Description |
Argument
inclusive
|
Type
BOOLEAN
|
Mode Positional | Description |
Return type varies by input — 11 concrete types
BIGINT[]DOUBLE[]FLOAT[]INTEGER[]SMALLINT[]TINYINT[]UBIGINT[]UINTEGER[]USMALLINT[]UTINYINT[]VARCHAR[]
Description
Return the quantiles (items) at a list of normalized ranks in [0, 1]. A NULL rank yields a NULL quantile
SELECT datasketch_kll_quantiles(datasketch_kll(200, latency_ms), [0.5, 0.9, 0.99], true) AS p50_p90_p99
FROM requests;
Related functions
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Argument
item
|
Type
TINYINT | INTEGER | BIGINT | …
11 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
|
Mode Positional | Description |
Argument
inclusive
|
Type
BOOLEAN
|
Mode Positional | Description |
Description
Inverse of datasketch_kll_quantile — given a value, return its approximate rank r ∈ [0, 1] in the sorted distribution.
SELECT datasketch_kll_rank(sketch, 50.0, true) FROM (SELECT datasketch_kll(200, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_kll_rank(sketch, 50.0, true) |
|---|
| 0.51 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Argument
items
|
Type
USMALLINT[] | UBIGINT[] | VARCHAR[] | …
11 concrete typesBIGINT[]DOUBLE[]FLOAT[]INTEGER[]SMALLINT[]TINYINT[]UBIGINT[]UINTEGER[]USMALLINT[]UTINYINT[]VARCHAR[]
|
Mode Positional | Description |
Argument
inclusive
|
Type
BOOLEAN
|
Mode Positional | Description |
Description
Return the normalized ranks of a list of items. A NULL item yields a NULL rank
SELECT datasketch_kll_ranks(datasketch_kll(200, latency_ms), [100, 500, 900], true) AS ranks
FROM requests;
Related functions
- datasketch_kll_rank() — Inverse of `datasketch_kll_quantile` — given a value, return its approximate rank `r ∈ [0, 1]` in the sorted distribution
- datasketch_kll_quantiles() — Return the quantiles (items) at a list of normalized ranks in [0, 1]
- datasketch_kll() — Aggregate input values into a KLL quantile sketch
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_kll_bigintsketch_kll_doublesketch_kll_floatsketch_kll_integersketch_kll_smallintsketch_kll_tinyintsketch_kll_ubigintsketch_kll_uintegersketch_kll_usmallintsketch_kll_utinyintsketch_kll_varchar
|
Mode Positional | Description |
Return type varies by input — 11 concrete types
STRUCT(item BIGINT, weight UBIGINT, cumulative_weight UBIGINT)[]STRUCT(item DOUBLE, weight UBIGINT, cumulative_weight UBIGINT)[]STRUCT(item FLOAT, weight UBIGINT, cumulative_weight UBIGINT)[]STRUCT(item INTEGER, weight UBIGINT, cumulative_weight UBIGINT)[]STRUCT(item SMALLINT, weight UBIGINT, cumulative_weight UBIGINT)[]STRUCT(item TINYINT, weight UBIGINT, cumulative_weight UBIGINT)[]STRUCT(item UBIGINT, weight UBIGINT, cumulative_weight UBIGINT)[]STRUCT(item UINTEGER, weight UBIGINT, cumulative_weight UBIGINT)[]STRUCT(item USMALLINT, weight UBIGINT, cumulative_weight UBIGINT)[]STRUCT(item UTINYINT, weight UBIGINT, cumulative_weight UBIGINT)[]STRUCT(item VARCHAR, weight UBIGINT, cumulative_weight UBIGINT)[]
Description
Return the sketch's sorted view: one entry per retained item in ascending order (equal items can appear more than once), with the item's weight and the inclusive cumulative weight (the number of input items less than or equal to it; estimated once the sketch is in estimation mode). Divide cumulative_weight by datasketch_kll_n(sketch) for the normalized rank. An empty sketch yields an empty list
SELECT sv.item, sv.weight, sv.cumulative_weight
FROM (SELECT unnest(datasketch_kll_sorted_view(datasketch_kll(200, temp))) AS sv
FROM readings);