REQ 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
REQ
19 functionsRelative-error quantile sketch. Error scales with rank rather than being fixed across the distribution — predictable accuracy on highly skewed data.
Signature
3 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
k
|
Type
INTEGER
|
Mode Positional | Description |
Argument
data
|
Type
<numeric>
10 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINT
|
Mode Positional | Description |
Return type varies by input — 10 concrete types
sketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
k
|
Type
INTEGER
|
Mode Positional | Description |
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Return type varies by input — 10 concrete types
sketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
k
|
Type
INTEGER
|
Mode Positional | Description |
Argument
data
|
Type
<numeric>
10 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINT
|
Mode Positional | Description |
Argument
hra
|
Type
BOOLEAN
|
Mode Positional | Description |
Return type varies by input — 10 concrete types
sketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
Return type varies by input — 10 concrete types
sketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
Description
Aggregate input values into a Relative Error Quantile sketch. Error scales with rank rather than being a fixed fraction of n — predictable accuracy on highly skewed distributions where a fixed-error sketch over- or under-spends precision at the extremes. An optional third argument hra (a constant, default true) picks high-rank accuracy (best for p99-style tails); pass false for low-rank accuracy. Merging starts from the first input's mode; mixing HRA and LRA sketches is an error. K must be between 4 and 1024. Returns NULL over no rows.
SELECT datasketch_req_is_hra(datasketch_req(12, value, false)) AS hra,
datasketch_req_quantile(datasketch_req(12, value, false), 0.01, true) AS p01
FROM measurements;
Related functions
- 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
- datasketch_req_cdf() — CDF over a list of split points — the cumulative rank at each split point, plus a final 1
- datasketch_req_pmf() — PMF (probability mass) over a list of split points — the fraction of the distribution falling in each bucket
- datasketch_req_is_hra() — Return true if the sketch favors accuracy at high ranks (HRA), false if it favors low ranks (LRA)
- datasketch_req_rank_lower_bound() — Return an approximate lower bound of the given normalized rank, with a confidence of num_std_dev standard deviations (1, 2 or 3)
- datasketch_req_rank_upper_bound() — Return an approximate upper bound of the given normalized rank, with a confidence of num_std_dev standard deviations (1, 2 or 3)
- datasketch_req_quantiles() — Return the quantiles (items) at a list of normalized ranks in [0, 1]
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Argument
split_points
|
Type
<numeric>[]
10 concrete typesBIGINT[]DOUBLE[]FLOAT[]INTEGER[]SMALLINT[]TINYINT[]UBIGINT[]UINTEGER[]USMALLINT[]UTINYINT[]
|
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_req_cdf(sketch, [25.0, 50.0, 75.0], true) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_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>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
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_req_describe(sketch, false, false) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_describe(sketch, false, false) |
|---|
### REQ sketch summary: K : 12 High Rank Acc : true Empty : false Estimation mode: true Sorted : false N : 100 Levels : 2 Retained items : 94 Capacity items : 144 Min item : 0 Max item : 99 ### End sketch summary |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Description
Return a boolean indicating if the sketch is empty
SELECT datasketch_req_is_empty(sketch) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_is_empty(sketch) |
|---|
| false |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Description
Return a boolean indicating if the sketch is in estimation mode
SELECT datasketch_req_is_estimation_mode(sketch) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_is_estimation_mode(sketch) |
|---|
| true |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Description
Return true if the sketch favors accuracy at high ranks (HRA), false if it favors low ranks (LRA)
SELECT datasketch_req_is_hra(sketch) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_is_hra(sketch) |
|---|
| true |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Description
Return the value of K for this sketch
SELECT datasketch_req_k(sketch) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_k(sketch) |
|---|
| 12 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Return type varies by input — 10 concrete types
BIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINT
Description
Return the maximum item in the sketch
SELECT datasketch_req_max_item(sketch) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_max_item(sketch) |
|---|
| 99.0 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Return type varies by input — 10 concrete types
BIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINT
Description
Return the minimum item in the sketch
SELECT datasketch_req_min_item(sketch) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_min_item(sketch) |
|---|
| 0.0 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Description
Return the number of items contained in the sketch
SELECT datasketch_req_n(sketch) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_n(sketch) |
|---|
| 100 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Description
Return the number of retained items in the sketch
SELECT datasketch_req_num_retained(sketch) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_num_retained(sketch) |
|---|
| 94 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Argument
split_points
|
Type
<numeric>[]
10 concrete typesBIGINT[]DOUBLE[]FLOAT[]INTEGER[]SMALLINT[]TINYINT[]UBIGINT[]UINTEGER[]USMALLINT[]UTINYINT[]
|
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_req_pmf(sketch, [25.0, 50.0, 75.0], true) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_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>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Argument
rank
|
Type
DOUBLE
|
Mode Positional | Description |
Argument
inclusive
|
Type
BOOLEAN
|
Mode Positional | Description |
Return type varies by input — 10 concrete types
BIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINT
Description
Approximate quantile at a given rank from a REQ sketch.
SELECT datasketch_req_quantile(sketch, 0.5, true) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_quantile(sketch, 0.5, true) |
|---|
| 49.0 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Argument
ranks
|
Type
DOUBLE[]
|
Mode Positional | Description |
Argument
inclusive
|
Type
BOOLEAN
|
Mode Positional | Description |
Return type varies by input — 10 concrete types
BIGINT[]DOUBLE[]FLOAT[]INTEGER[]SMALLINT[]TINYINT[]UBIGINT[]UINTEGER[]USMALLINT[]UTINYINT[]
Description
Return the quantiles (items) at a list of normalized ranks in [0, 1]. A NULL rank yields a NULL quantile
SELECT datasketch_req_quantiles(datasketch_req(12, latency_ms), [0.5, 0.9, 0.99], true) AS p50_p90_p99
FROM requests;
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Argument
item
|
Type
<numeric>
10 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINT
|
Mode Positional | Description |
Argument
inclusive
|
Type
BOOLEAN
|
Mode Positional | Description |
Description
Approximate rank of a value within the REQ sketch.
SELECT datasketch_req_rank(sketch, 50.0, true) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_rank(sketch, 50.0, true) |
|---|
| 0.51 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Argument
rank
|
Type
DOUBLE
|
Mode Positional | Description |
Argument
num_std_dev
|
Type
UTINYINT
|
Mode Positional | Description |
Description
Return an approximate lower bound of the given normalized rank, with a confidence of num_std_dev standard deviations (1, 2 or 3)
SELECT datasketch_req_rank_lower_bound(sketch, 0.5, 2) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_rank_lower_bound(sketch, 0.5, 2) |
|---|
| 0.4891133789209636 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Argument
rank
|
Type
DOUBLE
|
Mode Positional | Description |
Argument
num_std_dev
|
Type
UTINYINT
|
Mode Positional | Description |
Description
Return an approximate upper bound of the given normalized rank, with a confidence of num_std_dev standard deviations (1, 2 or 3)
WITH s AS (SELECT datasketch_req(12, value) AS sk FROM measurements)
SELECT datasketch_req_rank_lower_bound(sk, 0.99, 2) AS lo,
datasketch_req_rank_upper_bound(sk, 0.99, 2) AS hi
FROM s;
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Argument
items
|
Type
<numeric>[]
10 concrete typesBIGINT[]DOUBLE[]FLOAT[]INTEGER[]SMALLINT[]TINYINT[]UBIGINT[]UINTEGER[]USMALLINT[]UTINYINT[]
|
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_req_ranks(sketch, [10.0, 20.0, 30.0], true) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_ranks(sketch, list_value(10.0, 20.0, 30.0), true) |
|---|
| [0.1, 0.21, 0.31] |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
<sketch>
10 concrete typessketch_req_bigintsketch_req_doublesketch_req_floatsketch_req_integersketch_req_smallintsketch_req_tinyintsketch_req_ubigintsketch_req_uintegersketch_req_usmallintsketch_req_utinyint
|
Mode Positional | Description |
Return type varies by input — 10 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)[]
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_req_n(sketch) for the normalized rank. An empty sketch yields an empty list
SELECT datasketch_req_sorted_view(sketch) FROM (SELECT datasketch_req(12, i::DOUBLE) AS sketch FROM range(100) t(i)) s;
Output
| datasketch_req_sorted_view(sketch) |
|---|
| [{'item': 0.0, 'weight': 2, 'cumulative_weight': 2}, {'item': 2.0, 'weight': 2, 'cumulative_weight': 4}, {'item': 4.0, 'weight': 2, 'cumulative_weight': 6}, {'item': 6.0, 'weight': 2, 'cumulative_weight': 8}, {'item': 8.0, 'weight': 2, 'cumulative_weight': 10}, {'item': 10.0, 'weight': 2, 'cumulative_weight': 12}, {'item': 12.0, 'weight': 1, 'cumulative_weight': 13}, {'item': 13.0, 'weight': 1, 'cumulative_weight': 14}, {'item': 14.0, 'weight': 1, 'cumulative_weight': 15}, {'item': 15.0, 'weight': 1, 'cumulative_weight': 16}, {'item': 16.0, 'weight': 1, 'cumulative_weight': 17}, {'item': 17.0, 'weight': 1, 'cumulative_weight': 18}, {'item': 18.0, 'weight': 1, 'cumulative_weight': 19}, {'item': 19.0, 'weight': 1, 'cumulative_weight': 20}, {'item': 20.0, 'weight': 1, 'cumulative_weight': 21}, {'item': 21.0, 'weight': 1, 'cumulative_weight': 22}, {'item': 22.0, 'weight': 1, 'cumulative_weight': 23}, {'item': 23.0, 'weight': 1, 'cumulative_weight': 24}, {'item': 24.0, 'weight': 1, 'cumulative_weight': 25}, {'item': 25.0, 'weight': 1, 'cumulative_weight': 26}, {'item': 26.0, 'weight': 1, 'cumulative_weight': 27}, {'item': 27.0, 'weight': 1, 'cumulative_weight': 28}, {'item': 28.0, 'weight': 1, 'cumulative_weight': 29}, {'item': 29.0, 'weight': 1, 'cumulative_weight': 30}, {'item': 30.0, 'weight': 1, 'cumulative_weight': 31}, {'item': 31.0, 'weight': 1, 'cumulative_weight': 32}, {'item': 32.0, 'weight': 1, 'cumulative_weight': 33}, {'item': 33.0, 'weight': 1, 'cumulative_weight': 34}, {'item': 34.0, 'weight': 1, 'cumulative_weight': 35}, {'item': 35.0, 'weight': 1, 'cumulative_weight': 36}, {'item': 36.0, 'weight': 1, 'cumulative_weight': 37}, {'item': 37.0, 'weight': 1, 'cumulative_weight': 38}, {'item': 38.0, 'weight': 1, 'cumulative_weight': 39}, {'item': 39.0, 'weight': 1, 'cumulative_weight': 40}, {'item': 40.0, 'weight': 1, 'cumulative_weight': 41}, {'item': 41.0, 'weight': 1, 'cumulative_weight': 42}, {'item': 42.0, 'weight': 1, 'cumulative_weight': 43}, {'item': 43.0, 'weight': 1, 'cumulative_weight': 44}, {'item': 44.0, 'weight': 1, 'cumulative_weight': 45}, {'item': 45.0, 'weight': 1, 'cumulative_weight': 46}, {'item': 46.0, 'weight': 1, 'cumulative_weight': 47}, {'item': 47.0, 'weight': 1, 'cumulative_weight': 48}, {'item': 48.0, 'weight': 1, 'cumulative_weight': 49}, {'item': 49.0, 'weight': 1, 'cumulative_weight': 50}, {'item': 50.0, 'weight': 1, 'cumulative_weight': 51}, {'item': 51.0, 'weight': 1, 'cumulative_weight': 52}, {'item': 52.0, 'weight': 1, 'cumulative_weight': 53}, {'item': 53.0, 'weight': 1, 'cumulative_weight': 54}, {'item': 54.0, 'weight': 1, 'cumulative_weight': 55}, {'item': 55.0, 'weight': 1, 'cumulative_weight': 56}, {'item': 56.0, 'weight': 1, 'cumulative_weight': 57}, {'item': 57.0, 'weight': 1, 'cumulative_weight': 58}, {'item': 58.0, 'weight': 1, 'cumulative_weight': 59}, {'item': 59.0, 'weight': 1, 'cumulative_weight': 60}, {'item': 60.0, 'weight': 1, 'cumulative_weight': 61}, {'item': 61.0, 'weight': 1, 'cumulative_weight': 62}, {'item': 62.0, 'weight': 1, 'cumulative_weight': 63}, {'item': 63.0, 'weight': 1, 'cumulative_weight': 64}, {'item': 64.0, 'weight': 1, 'cumulative_weight': 65}, {'item': 65.0, 'weight': 1, 'cumulative_weight': 66}, {'item': 66.0, 'weight': 1, 'cumulative_weight': 67}, {'item': 67.0, 'weight': 1, 'cumulative_weight': 68}, {'item': 68.0, 'weight': 1, 'cumulative_weight': 69}, {'item': 69.0, 'weight': 1, 'cumulative_weight': 70}, {'item': 70.0, 'weight': 1, 'cumulative_weight': 71}, {'item': 71.0, 'weight': 1, 'cumulative_weight': 72}, {'item': 72.0, 'weight': 1, 'cumulative_weight': 73}, {'item': 73.0, 'weight': 1, 'cumulative_weight': 74}, {'item': 74.0, 'weight': 1, 'cumulative_weight': 75}, {'item': 75.0, 'weight': 1, 'cumulative_weight': 76}, {'item': 76.0, 'weight': 1, 'cumulative_weight': 77}, {'item': 77.0, 'weight': 1, 'cumulative_weight': 78}, {'item': 78.0, 'weight': 1, 'cumulative_weight': 79}, {'item': 79.0, 'weight': 1, 'cumulative_weight': 80}, {'item': 80.0, 'weight': 1, 'cumulative_weight': 81}, {'item': 81.0, 'weight': 1, 'cumulative_weight': 82}, {'item': 82.0, 'weight': 1, 'cumulative_weight': 83}, {'item': 83.0, 'weight': 1, 'cumulative_weight': 84}, {'item': 84.0, 'weight': 1, 'cumulative_weight': 85}, {'item': 85.0, 'weight': 1, 'cumulative_weight': 86}, {'item': 86.0, 'weight': 1, 'cumulative_weight': 87}, {'item': 87.0, 'weight': 1, 'cumulative_weight': 88}, {'item': 88.0, 'weight': 1, 'cumulative_weight': 89}, {'item': 89.0, 'weight': 1, 'cumulative_weight': 90}, {'item': 90.0, 'weight': 1, 'cumulative_weight': 91}, {'item': 91.0, 'weight': 1, 'cumulative_weight': 92}, {'item': 92.0, 'weight': 1, 'cumulative_weight': 93}, {'item': 93.0, 'weight': 1, 'cumulative_weight': 94}, {'item': 94.0, 'weight': 1, 'cumulative_weight': 95}, {'item': 95.0, 'weight': 1, '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}] |