Theta 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
Theta
20 functionsTheta sketch — the only distinct-count family that supports set operations beyond union. Combine cohorts with union, intersect, and a_not_b directly on sketch state, without rescanning the source data, and compare them with Jaccard similarity.
Signature
4 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_theta
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
lg_k
|
Type
INTEGER
|
Mode Positional | Description |
Argument
sketch
|
Type
sketch_theta
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
data
|
Type
UBIGINT | TINYINT | USMALLINT | …
11 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
|
Mode Positional | Description |
Argument
seed
|
Type
BIGINT
|
Mode Positional | Description |
Argument
p
|
Type
DOUBLE
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
lg_k
|
Type
INTEGER
|
Mode Positional | Description |
Argument
data
|
Type
UINTEGER | UBIGINT | VARCHAR | …
11 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTUBIGINTUINTEGERUSMALLINTUTINYINTVARCHAR
|
Mode Positional | Description |
Argument
seed
|
Type
BIGINT
|
Mode Positional | Description |
Argument
p
|
Type
DOUBLE
|
Mode Positional | Description |
Description
Aggregate input values into a Theta sketch — the distinct-count family that supports set operations (union, intersect, A-not-B) beyond simple merge. The optional leading lg_k (4–26, default 12) is base-2 log of the nominal sketch entries. Accepts signed and unsigned integers, floats and VARCHAR; passing a sketch_theta (or a BLOB of serialized sketches, e.g. read back from Parquet) merges sketches instead, growing to the largest lg_k of the inputs unless one is given. Keyword options: seed := (hash seed — sketches can only be combined with sketches built from the same seed) and p := (up-front sampling probability in (0, 1]). Results are stored in the compressed compact format (serial version 4) when it applies; versions 1–4 are all readable. Returns NULL over no rows.
SELECT cohort,
datasketch_theta(12, user_id) AS sketch
FROM events
GROUP BY cohort;
SELECT datasketch_theta_estimate(datasketch_theta(s)) AS uniques
FROM (SELECT datasketch_theta(12, user_id) AS s FROM events GROUP BY cohort);
SELECT datasketch_theta_seed_hash(datasketch_theta(user_id, seed := 42)) AS seed_hash,
datasketch_theta_get_theta(datasketch_theta(user_id, p := 0.5)) AS theta
FROM events;
Related functions
- datasketch_theta_estimate() — Read the estimated distinct count from a Theta sketch
- datasketch_theta_union() — Approximate `|A ∪ B ∪ …|` — scalar union of two or more Theta sketches
- datasketch_theta_intersect() — Approximate `|A ∩ B|` — distinct items present in both cohorts
- datasketch_theta_a_not_b() — Approximate `|A \ B|` — distinct items present in A but not in B
- datasketch_theta_jaccard() — Returns the Jaccard similarity |A ∩ B| / |A ∪ B| of two sketches as a struct of its estimate and approximate lower and upper bounds (about 2 standard deviations)
- datasketch_theta_intersect_agg() — Intersects Theta sketches across rows: the result estimates the number of distinct values present in every input sketch
- datasketch_theta_blob() — Hashes raw BLOB values (their bytes) into a Theta sketch, like datasketch_theta does for other types (datasketch_theta(BLOB) merges serialized sketches instead)
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch_a
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
sketch_b
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Approximate |A \ B| — distinct items present in A but not in B. Useful for churn (in-Jan, not-in-Feb) and new-user (in-Feb, not-in-Jan) measurements directly from sketch state.
SELECT datasketch_theta_a_not_b(sketch_a, sketch_b) FROM (SELECT datasketch_theta(i) AS sketch_a FROM range(0, 1000) t(i)) a, (SELECT datasketch_theta(i) AS sketch_b FROM range(500, 1500) t(i)) b;
Related functions
- datasketch_theta() — Aggregate input values into a Theta sketch — the distinct-count family that supports set operations (union, intersect, A-not-B) beyond simple merge
- datasketch_theta_union() — Approximate `|A ∪ B ∪ …|` — scalar union of two or more Theta sketches
- datasketch_theta_intersect() — Approximate `|A ∩ B|` — distinct items present in both cohorts
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
data
|
Type
BLOB
|
Mode Positional | Description |
Argument
seed
|
Type
BIGINT
|
Mode Positional | Description |
Argument
p
|
Type
DOUBLE
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
lg_k
|
Type
INTEGER
|
Mode Positional | Description |
Argument
data
|
Type
BLOB
|
Mode Positional | Description |
Argument
seed
|
Type
BIGINT
|
Mode Positional | Description |
Argument
p
|
Type
DOUBLE
|
Mode Positional | Description |
Description
Hashes raw BLOB values (their bytes) into a Theta sketch, like datasketch_theta does for other types (datasketch_theta(BLOB) merges serialized sketches instead). Optional keyword arguments: seed (hash seed; sketches can only be combined with sketches built with the same seed) and p (sampling probability in (0, 1], to pre-sample the input)
SELECT datasketch_theta_estimate(datasketch_theta_blob(country_code::BLOB)) AS distinct_payloads
FROM page_views;
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Returns a human-readable description of the Theta sketch
SELECT datasketch_theta_describe(sketch) FROM (SELECT datasketch_theta(i) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_theta_describe(sketch) |
|---|
### Theta sketch summary: num retained entries : 1000 seed hash : 37836 empty? : false ordered? : true estimation mode? : false theta (fraction) : 1 theta (raw 64-bit) : 9223372036854775807 estimate : 1000 lower bound 95% conf : 1000 upper bound 95% conf : 1000 ### End sketch summary |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
actual
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
expected
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
threshold
|
Type
DOUBLE
|
Mode Positional | Description |
Description
Returns true if the Jaccard similarity of the two sketches is at most the threshold, with high confidence (its upper bound is <= threshold)
SELECT datasketch_theta_dissimilarity_test(actual, expected, 0.1) FROM (SELECT datasketch_theta(i) AS actual FROM range(0, 1000) t(i)) a, (SELECT datasketch_theta(i) AS expected FROM range(100, 1100) t(i)) e;
Output
| datasketch_theta_dissimilarity_test(actual, expected, 0.1) |
|---|
| false |
Related functions
- datasketch_theta_jaccard() — Returns the Jaccard similarity |A ∩ B| / |A ∪ B| of two sketches as a struct of its estimate and approximate lower and upper bounds (about 2 standard deviations)
- datasketch_theta_similarity_test() — Returns true if the Jaccard similarity of the two sketches is at least the threshold, with high confidence (its lower bound is >= threshold)
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Read the estimated distinct count from a Theta sketch.
SELECT datasketch_theta_estimate(sketch) FROM (SELECT datasketch_theta(i) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_theta_estimate(sketch) |
|---|
| 1000.0 |
Related functions
- datasketch_theta() — Aggregate input values into a Theta sketch — the distinct-count family that supports set operations (union, intersect, A-not-B) beyond simple merge
- datasketch_theta_lower_bound() — Lower bound of the Theta distinct-count estimate
- datasketch_theta_upper_bound() — Upper bound of the Theta distinct-count estimate
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch_a
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
sketch_b
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Returns true if the two sketches retain exactly the same hashes with the same theta, i.e. they represent the same set as far as the sketches can tell
SELECT datasketch_theta_exactly_equal(sketch_a, sketch_b) FROM (SELECT datasketch_theta(i) AS sketch_a FROM range(0, 1000) t(i)) a, (SELECT datasketch_theta(i) AS sketch_b FROM range(500, 1500) t(i)) b;
Output
| datasketch_theta_exactly_equal(sketch_a, sketch_b) |
|---|
| false |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Alias of datasketch_theta_seed_hash: returns the 16-bit seed hash stored in the sketch (it always returned the hash, not the seed).
SELECT datasketch_theta_get_seed(sketch) FROM (SELECT datasketch_theta(i) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_theta_get_seed(sketch) |
|---|
| 37836 |
Related functions
- datasketch_theta() — Aggregate input values into a Theta sketch — the distinct-count family that supports set operations (union, intersect, A-not-B) beyond simple merge
- datasketch_theta_seed_hash() — Returns the 16-bit hash of the seed the sketch was built with (compact sketches store only this hash, not the seed); sketches can only be combined if their seed hashes match
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Returns the theta value of the sketch (sampling probability)
SELECT datasketch_theta_get_theta(sketch) FROM (SELECT datasketch_theta(i) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_theta_get_theta(sketch) |
|---|
| 1.0 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch_a
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
sketch_b
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
varargs
|
Type
sketch_theta
|
Mode Positional | Description Varargs |
Description
Approximate |A ∩ B| — distinct items present in both cohorts. The classic use case is funnel and retention analysis: how many users in segment A also appear in segment B, computed from the sketches alone. Accepts two or more sketches; use datasketch_theta_intersect_agg to intersect a column of sketches across rows.
WITH a AS (SELECT datasketch_theta(12, user_id) AS s FROM events_jan),
b AS (SELECT datasketch_theta(12, user_id) AS s FROM events_feb)
SELECT datasketch_theta_estimate(
datasketch_theta_intersect(a.s, b.s)
) AS retained_users
FROM a, b;
Related functions
- datasketch_theta() — Aggregate input values into a Theta sketch — the distinct-count family that supports set operations (union, intersect, A-not-B) beyond simple merge
- datasketch_theta_union() — Approximate `|A ∪ B ∪ …|` — scalar union of two or more Theta sketches
- datasketch_theta_a_not_b() — Approximate `|A \ B|` — distinct items present in A but not in B
- datasketch_theta_intersect_agg() — Intersects Theta sketches across rows: the result estimates the number of distinct values present in every input sketch
- datasketch_theta_jaccard() — Returns the Jaccard similarity |A ∩ B| / |A ∪ B| of two sketches as a struct of its estimate and approximate lower and upper bounds (about 2 standard deviations)
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Intersects Theta sketches across rows: the result estimates the number of distinct values present in every input sketch. NULL sketches are skipped; returns NULL if there are no sketches
SELECT datasketch_theta_estimate(datasketch_theta_intersect_agg(s)) AS in_every_month
FROM (SELECT datasketch_theta(12, user_id) AS s FROM events_jan
UNION ALL
SELECT datasketch_theta(12, user_id) FROM events_feb);
Related functions
- datasketch_theta_intersect() — Approximate `|A ∩ B|` — distinct items present in both cohorts
- datasketch_theta() — Aggregate input values into a Theta sketch — the distinct-count family that supports set operations (union, intersect, A-not-B) beyond simple merge
- datasketch_theta_estimate() — Read the estimated distinct count from a Theta sketch
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Returns true if the Theta sketch is empty
SELECT datasketch_theta_is_empty(sketch) FROM (SELECT datasketch_theta(i) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_theta_is_empty(sketch) |
|---|
| false |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Returns true if the sketch is in estimation mode (has exceeded exact counting capacity)
SELECT datasketch_theta_is_estimation_mode(sketch) FROM (SELECT datasketch_theta(i) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_theta_is_estimation_mode(sketch) |
|---|
| false |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch_a
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
sketch_b
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Returns the Jaccard similarity |A ∩ B| / |A ∪ B| of two sketches as a struct of its estimate and approximate lower and upper bounds (about 2 standard deviations). Two empty sketches are identical (1.0)
WITH a AS (SELECT datasketch_theta(12, user_id) AS s FROM events_jan),
b AS (SELECT datasketch_theta(12, user_id) AS s FROM events_feb)
SELECT datasketch_theta_jaccard(a.s, b.s).estimate AS jaccard,
datasketch_theta_similarity_test(a.s, b.s, 0.3) AS at_least_30pct
FROM a, b;
Related functions
- datasketch_theta_exactly_equal() — Returns true if the two sketches retain exactly the same hashes with the same theta, i
- datasketch_theta_similarity_test() — Returns true if the Jaccard similarity of the two sketches is at least the threshold, with high confidence (its lower bound is >= threshold)
- datasketch_theta_dissimilarity_test() — Returns true if the Jaccard similarity of the two sketches is at most the threshold, with high confidence (its upper bound is <= threshold)
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
num_std_devs
|
Type
INTEGER
|
Mode Positional | Description |
Description
Lower bound of the Theta distinct-count estimate. num_std_devs (1, 2, or 3 — roughly 68%, 95%, and 99.7% confidence) sets the width of the interval; any other value is an error.
SELECT datasketch_theta_lower_bound(sketch, 2) FROM (SELECT datasketch_theta(i) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_theta_lower_bound(sketch, 2) |
|---|
| 1000.0 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Returns the number of hash values retained in the sketch
SELECT datasketch_theta_num_retained(sketch) FROM (SELECT datasketch_theta(i) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_theta_num_retained(sketch) |
|---|
| 1000 |
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Returns the 16-bit hash of the seed the sketch was built with (compact sketches store only this hash, not the seed); sketches can only be combined if their seed hashes match
SELECT datasketch_theta_seed_hash(sketch) FROM (SELECT datasketch_theta(i) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_theta_seed_hash(sketch) |
|---|
| 37836 |
Related functions
- datasketch_theta() — Aggregate input values into a Theta sketch — the distinct-count family that supports set operations (union, intersect, A-not-B) beyond simple merge
- datasketch_theta_get_seed() — Alias of `datasketch_theta_seed_hash`: returns the 16-bit seed hash stored in the sketch (it always returned the hash, not the seed)
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
actual
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
expected
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
threshold
|
Type
DOUBLE
|
Mode Positional | Description |
Description
Returns true if the Jaccard similarity of the two sketches is at least the threshold, with high confidence (its lower bound is >= threshold)
SELECT datasketch_theta_similarity_test(actual, expected, 0.9) FROM (SELECT datasketch_theta(i) AS actual FROM range(0, 1000) t(i)) a, (SELECT datasketch_theta(i) AS expected FROM range(100, 1100) t(i)) e;
Output
| datasketch_theta_similarity_test(actual, expected, 0.9) |
|---|
| false |
Related functions
- datasketch_theta_jaccard() — Returns the Jaccard similarity |A ∩ B| / |A ∪ B| of two sketches as a struct of its estimate and approximate lower and upper bounds (about 2 standard deviations)
- datasketch_theta_dissimilarity_test() — Returns true if the Jaccard similarity of the two sketches is at most the threshold, with high confidence (its upper bound is <= threshold)
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch_a
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
sketch_b
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
varargs
|
Type
sketch_theta
|
Mode Positional | Description Varargs |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
lg_k
|
Type
INTEGER
|
Mode Positional | Description |
Argument
sketch_a
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
sketch_b
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
varargs
|
Type
sketch_theta
|
Mode Positional | Description Varargs |
Description
Approximate |A ∪ B ∪ …| — scalar union of two or more Theta sketches. The result keeps the largest lg_k inferred from the inputs (at least the default 12); pass a leading lg_k (datasketch_theta_union(16, a, b)) to set it explicitly. To merge a whole column of sketches across rows, use the datasketch_theta(sketch) aggregate.
WITH a AS (SELECT datasketch_theta(user_id) AS s FROM events_jan),
b AS (SELECT datasketch_theta(user_id) AS s FROM events_feb),
c AS (SELECT datasketch_theta(user_id) AS s FROM events)
SELECT datasketch_theta_estimate(datasketch_theta_union(a.s, b.s, c.s)) AS any_cohort
FROM a, b, c;
Related functions
- datasketch_theta() — Aggregate input values into a Theta sketch — the distinct-count family that supports set operations (union, intersect, A-not-B) beyond simple merge
- datasketch_theta_intersect() — Approximate `|A ∩ B|` — distinct items present in both cohorts
- datasketch_theta_a_not_b() — Approximate `|A \ B|` — distinct items present in A but not in B
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
sketch
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
num_std_devs
|
Type
INTEGER
|
Mode Positional | Description |
Description
Upper bound of the Theta distinct-count estimate. num_std_devs (1, 2, or 3 — roughly 68%, 95%, and 99.7% confidence) sets the width of the interval; any other value is an error.
SELECT datasketch_theta_upper_bound(sketch, 2) FROM (SELECT datasketch_theta(i) AS sketch FROM range(0, 1000) t(i)) s;
Output
| datasketch_theta_upper_bound(sketch, 2) |
|---|
| 1000.0 |