Skip to content

Theta functions in the Apache DataSketches DuckDB extension

Function category

Theta

13 functions

[Theta sketch](https://datasketches.apache.org/docs/Theta/ThetaSketches.html) — 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.

datasketch_theta

Aggregate function Theta
Signature
2 overloaded forms · click to inspect
Arguments (Positional)
Argument col0 Type VARCHAR | FLOAT | TINYINT | …
8 concrete types
BIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTVARCHARsketch_theta
Mode Positional Description
Description
1 Build per-cohort sketches
SELECT cohort,
       datasketch_theta(12, user_id) AS sketch
FROM events
GROUP BY cohort;

datasketch_theta_a_not_b

Scalar function Theta
Signature
datasketch_theta_a_not_b(col0: sketch_theta, col1: sketch_theta) sketch_theta
Arguments (Positional)
Argument col0 Type sketch_theta Mode Positional Description
Argument col1 Type sketch_theta Mode Positional Description
Description
1 Example 1
SELECT datasketch_theta_a_not_b(sketch_a, sketch_b);

datasketch_theta_describe

Scalar function Theta
Signature
datasketch_theta_describe(col0: sketch_theta) VARCHAR
Arguments (Positional)
Argument col0 Type sketch_theta Mode Positional Description
Description
1 Example 1
SELECT datasketch_theta_describe(sketch);

datasketch_theta_estimate

Scalar function Theta
Signature
datasketch_theta_estimate(col0: sketch_theta) DOUBLE
Arguments (Positional)
Argument col0 Type sketch_theta Mode Positional Description
Description
1 Example 1
SELECT datasketch_theta_estimate(sketch);

datasketch_theta_get_seed

Scalar function Theta
Signature
datasketch_theta_get_seed(col0: sketch_theta) BIGINT
Arguments (Positional)
Argument col0 Type sketch_theta Mode Positional Description
Description
1 Example 1
SELECT datasketch_theta_get_seed(sketch);

datasketch_theta_get_theta

Scalar function Theta
Signature
datasketch_theta_get_theta(col0: sketch_theta) DOUBLE
Arguments (Positional)
Argument col0 Type sketch_theta Mode Positional Description
Description
1 Example 1
SELECT datasketch_theta_get_theta(sketch);

datasketch_theta_intersect

Scalar function Theta
Signature
datasketch_theta_intersect(col0: sketch_theta, col1: sketch_theta) sketch_theta
Arguments (Positional)
Argument col0 Type sketch_theta Mode Positional Description
Argument col1 Type sketch_theta Mode Positional Description
Description
1 Retention — users active in Jan AND Feb
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;

datasketch_theta_is_empty

Scalar function Theta
Signature
datasketch_theta_is_empty(col0: sketch_theta) BOOLEAN
Arguments (Positional)
Argument col0 Type sketch_theta Mode Positional Description
Description
1 Example 1
SELECT datasketch_theta_is_empty(sketch);

datasketch_theta_is_estimation_mode

Scalar function Theta
Signature
datasketch_theta_is_estimation_mode(col0: sketch_theta) BOOLEAN
Arguments (Positional)
Argument col0 Type sketch_theta Mode Positional Description
Description
1 Example 1
SELECT datasketch_theta_is_estimation_mode(sketch);

datasketch_theta_lower_bound

Scalar function Theta
Signature
datasketch_theta_lower_bound(col0: sketch_theta, col1: INTEGER) DOUBLE
Arguments (Positional)
Argument col0 Type sketch_theta Mode Positional Description
Argument col1 Type INTEGER Mode Positional Description
Description
1 Example 1
SELECT datasketch_theta_lower_bound(sketch, 2);

datasketch_theta_num_retained

Scalar function Theta
Signature
datasketch_theta_num_retained(col0: sketch_theta) BIGINT
Arguments (Positional)
Argument col0 Type sketch_theta Mode Positional Description
Description
1 Example 1
SELECT datasketch_theta_num_retained(sketch);

datasketch_theta_union

Scalar function Theta
Signature
datasketch_theta_union(col0: sketch_theta, col1: sketch_theta) sketch_theta
Arguments (Positional)
Argument col0 Type sketch_theta Mode Positional Description
Argument col1 Type sketch_theta Mode Positional Description
Description
1 Example 1
SELECT datasketch_theta_union(sketch1, sketch2);

datasketch_theta_upper_bound

Scalar function Theta
Signature
datasketch_theta_upper_bound(col0: sketch_theta, col1: INTEGER) DOUBLE
Arguments (Positional)
Argument col0 Type sketch_theta Mode Positional Description
Argument col1 Type INTEGER Mode Positional Description
Description
1 Example 1
SELECT datasketch_theta_upper_bound(sketch, 2);