Quantiles 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
Quantiles
17 functionsClassic mergeable quantile sketch from the original DataSketches paper. Solid general-purpose choice supporting all numeric types; KLL is now preferred for new projects.
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
k
|
Type
INTEGER
|
Mode Positional | Description |
Argument
data
|
Type
INTEGER | DOUBLE | UINTEGER | …
11 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
|
Mode Positional | Description |
Return type varies by input — 11 concrete types
sketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
k
|
Type
INTEGER
|
Mode Positional | Description |
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
|
Mode Positional | Description |
Return type varies by input — 11 concrete types
sketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
Return type varies by input — 11 concrete types
sketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
Description
Aggregate input values into the classic mergeable quantiles sketch from the original DataSketches paper. Solid general-purpose choice supporting all numeric input types; for new projects prefer datasketch_kll, which gives the same merge semantics with better accuracy/size trade-off. Also accepts VARCHAR items (ordered bytewise, sketch_quantiles_varchar). K must be a power of 2 from 2 to 32768. Returns NULL over no rows.
SELECT datasketch_quantiles(16, i::DOUBLE) FROM range(100) t(i);
Related functions
- 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
- datasketch_quantiles_cdf() — CDF over a list of split points — the cumulative rank at each split point, plus a final 1
- datasketch_quantiles_pmf() — PMF (probability mass) over a list of split points — the fraction of the distribution falling in each bucket
- datasketch_quantiles_quantiles() — Return the quantiles (items) at a list of normalized ranks in [0, 1]
- datasketch_quantiles_ranks() — Return the normalized ranks of a list of items
- datasketch_quantiles_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_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
|
Mode Positional | Description |
Argument
split_points
|
Type
UTINYINT[] | INTEGER[] | USMALLINT[] | …
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 — the cumulative rank at each split point, plus a final 1.0. Always returns LIST(DOUBLE) regardless of the sketch's item type.
SELECT datasketch_quantiles_cdf(sketch, [25.0, 50.0, 75.0], true) FROM (SELECT datasketch_quantiles(16, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_quantiles_cdf(sketch, list_value(25.0, 50.0, 75.0), true) |
|---|
| [0.28, 0.52, 0.76, 1.0] |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_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_quantiles_describe(sketch, false, false) FROM (SELECT datasketch_quantiles(16, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_quantiles_describe(sketch, false, false) |
|---|
### Quantiles Sketch summary: K : 16 N : 100 Epsilon : 10.6% Epsilon PMF : 12.7% Empty : false Estimation mode: true Levels (w/o BB): 2 Used Levels : 2 Retained items : 36 Min item : 0 Max item : 99 ### End sketch summary |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
|
Mode Positional | Description |
Description
Return a boolean indicating if the sketch is empty
SELECT datasketch_quantiles_is_empty(sketch) FROM (SELECT datasketch_quantiles(16, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_quantiles_is_empty(sketch) |
|---|
| false |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
|
Mode Positional | Description |
Description
Return a boolean indicating if the sketch is in estimation mode
SELECT datasketch_quantiles_is_estimation_mode(sketch) FROM (SELECT datasketch_quantiles(16, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_quantiles_is_estimation_mode(sketch) |
|---|
| true |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
|
Mode Positional | Description |
Description
Return the value of K for this sketch
SELECT datasketch_quantiles_k(sketch) FROM (SELECT datasketch_quantiles(16, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_quantiles_k(sketch) |
|---|
| 16 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
|
Mode Positional | Description |
Return type varies by input — 11 concrete types
BIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
Description
Return the maximum item in the sketch
SELECT datasketch_quantiles_max_item(sketch) FROM (SELECT datasketch_quantiles(16, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_quantiles_max_item(sketch) |
|---|
| 99.0 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
|
Mode Positional | Description |
Return type varies by input — 11 concrete types
BIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
Description
Return the minimum item in the sketch
SELECT datasketch_quantiles_min_item(sketch) FROM (SELECT datasketch_quantiles(16, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_quantiles_min_item(sketch) |
|---|
| 0.0 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
|
Mode Positional | Description |
Description
Return the number of items contained in the sketch
SELECT datasketch_quantiles_n(sketch) FROM (SELECT datasketch_quantiles(16, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_quantiles_n(sketch) |
|---|
| 100 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
|
Mode Positional | Description |
Argument
is_pmf
|
Type
BOOLEAN
|
Mode Positional | Description |
Description
Return the normalized rank error of the sketch
SELECT datasketch_quantiles_normalized_rank_error(sketch, false) FROM (SELECT datasketch_quantiles(16, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_quantiles_normalized_rank_error(sketch, false) |
|---|
| 0.10627451165926699 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
|
Mode Positional | Description |
Description
Return the number of retained items in the sketch
SELECT datasketch_quantiles_num_retained(sketch) FROM (SELECT datasketch_quantiles(16, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_quantiles_num_retained(sketch) |
|---|
| 36 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
|
Mode Positional | Description |
Argument
split_points
|
Type
DOUBLE[] | UBIGINT[] | 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 — the fraction of the distribution falling in each bucket. Always returns LIST(DOUBLE) regardless of the sketch's item type.
SELECT datasketch_quantiles_pmf(sketch, [25.0, 50.0, 75.0], true) FROM (SELECT datasketch_quantiles(16, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_quantiles_pmf(sketch, list_value(25.0, 50.0, 75.0), true) |
|---|
| [0.28, 0.24, 0.24, 0.24] |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_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 from a classic Quantiles sketch.
SELECT datasketch_quantiles_quantile(sketch, 0.5, true) FROM (SELECT datasketch_quantiles(16, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_quantiles_quantile(sketch, 0.5, true) |
|---|
| 51.0 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_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_quantiles_quantiles(datasketch_quantiles(128, latency_ms), [0.5, 0.9, 0.99], true) AS p50_p90_p99
FROM requests;
Related functions
- datasketch_quantiles_quantile() — Approximate quantile at a given rank from a classic Quantiles sketch
- datasketch_quantiles_ranks() — Return the normalized ranks of a list of items
- datasketch_quantiles() — Aggregate input values into the classic mergeable quantiles sketch from the original DataSketches paper
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
|
Mode Positional | Description |
Argument
item
|
Type
DOUBLE | INTEGER | USMALLINT | …
11 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
|
Mode Positional | Description |
Argument
inclusive
|
Type
BOOLEAN
|
Mode Positional | Description |
Description
Approximate rank of a value within the classic Quantiles sketch.
SELECT datasketch_quantiles_rank(sketch, 50.0, true) FROM (SELECT datasketch_quantiles(16, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_quantiles_rank(sketch, 50.0, true) |
|---|
| 0.52 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_varchar
|
Mode Positional | Description |
Argument
items
|
Type
FLOAT[] | BIGINT[] | SMALLINT[] | …
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_quantiles_ranks(sketch, [10.0, 20.0, 30.0], true) FROM (SELECT datasketch_quantiles(16, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_quantiles_ranks(sketch, list_value(10.0, 20.0, 30.0), true) |
|---|
| [0.12, 0.24, 0.32] |
Related functions
- datasketch_quantiles_rank() — Approximate rank of a value within the classic Quantiles sketch
- datasketch_quantiles_quantiles() — Return the quantiles (items) at a list of normalized ranks in [0, 1]
- datasketch_quantiles() — Aggregate input values into the classic mergeable quantiles sketch from the original DataSketches paper
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
11 concrete typessketch_quantiles_bigintsketch_quantiles_doublesketch_quantiles_floatsketch_quantiles_integersketch_quantiles_smallintsketch_quantiles_tinyintsketch_quantiles_ubigintsketch_quantiles_uintegersketch_quantiles_usmallintsketch_quantiles_utinyintsketch_quantiles_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_quantiles_n(sketch) for the normalized rank. An empty sketch yields an empty list
SELECT datasketch_quantiles_sorted_view(sketch) FROM (SELECT datasketch_quantiles(16, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_quantiles_sorted_view(sketch) |
|---|
| [{'item': 1.0, 'weight': 4, 'cumulative_weight': 4}, {'item': 5.0, 'weight': 4, 'cumulative_weight': 8}, {'item': 9.0, 'weight': 4, 'cumulative_weight': 12}, {'item': 13.0, 'weight': 4, 'cumulative_weight': 16}, {'item': 17.0, 'weight': 4, 'cumulative_weight': 20}, {'item': 21.0, 'weight': 4, 'cumulative_weight': 24}, {'item': 25.0, 'weight': 4, 'cumulative_weight': 28}, {'item': 29.0, 'weight': 4, 'cumulative_weight': 32}, {'item': 33.0, 'weight': 4, 'cumulative_weight': 36}, {'item': 37.0, 'weight': 4, 'cumulative_weight': 40}, {'item': 41.0, 'weight': 4, 'cumulative_weight': 44}, {'item': 45.0, 'weight': 4, 'cumulative_weight': 48}, {'item': 49.0, 'weight': 4, 'cumulative_weight': 52}, {'item': 53.0, 'weight': 4, 'cumulative_weight': 56}, {'item': 57.0, 'weight': 4, 'cumulative_weight': 60}, {'item': 61.0, 'weight': 4, 'cumulative_weight': 64}, {'item': 65.0, 'weight': 2, 'cumulative_weight': 66}, {'item': 67.0, 'weight': 2, 'cumulative_weight': 68}, {'item': 69.0, 'weight': 2, 'cumulative_weight': 70}, {'item': 71.0, 'weight': 2, 'cumulative_weight': 72}, {'item': 73.0, 'weight': 2, 'cumulative_weight': 74}, {'item': 75.0, 'weight': 2, 'cumulative_weight': 76}, {'item': 77.0, 'weight': 2, 'cumulative_weight': 78}, {'item': 79.0, 'weight': 2, 'cumulative_weight': 80}, {'item': 81.0, 'weight': 2, 'cumulative_weight': 82}, {'item': 83.0, 'weight': 2, 'cumulative_weight': 84}, {'item': 85.0, 'weight': 2, 'cumulative_weight': 86}, {'item': 87.0, 'weight': 2, 'cumulative_weight': 88}, {'item': 89.0, 'weight': 2, 'cumulative_weight': 90}, {'item': 91.0, 'weight': 2, 'cumulative_weight': 92}, {'item': 93.0, 'weight': 2, 'cumulative_weight': 94}, {'item': 95.0, 'weight': 2, 'cumulative_weight': 96}, {'item': 96.0, 'weight': 1, 'cumulative_weight': 97}, {'item': 97.0, 'weight': 1, 'cumulative_weight': 98}, {'item': 98.0, 'weight': 1, 'cumulative_weight': 99}, {'item': 99.0, 'weight': 1, 'cumulative_weight': 100}] |
Related functions
- datasketch_quantiles_cdf() — CDF over a list of split points — the cumulative rank at each split point, plus a final 1
- datasketch_quantiles_n() — Return the number of items contained in the sketch
- datasketch_quantiles() — Aggregate input values into the classic mergeable quantiles sketch from the original DataSketches paper