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.
Signature
2 overloaded forms · click to inspectArguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
col0
|
Type
VARCHAR | FLOAT | TINYINT | …
8 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTVARCHARsketch_theta
|
Mode Positional | Description |
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
col0
|
Type
INTEGER
|
Mode Positional | Description |
Argument
col1
|
Type
sketch_theta | SMALLINT | INTEGER | …
8 concrete typesBIGINTDOUBLEFLOATINTEGERSMALLINTTINYINTVARCHARsketch_theta
|
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 leading lg_k is base-2 log of the nominal sketch entries.
SELECT cohort,
datasketch_theta(12, user_id) AS sketch
FROM events
GROUP BY cohort;
Related functions
- datasketch_theta_estimate() — Read the estimated distinct count from a Theta sketch
- datasketch_theta_union() — Approximate `|A ∪ B|` — distinct count across the union of two cohorts
- 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
col0
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
col1
|
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);
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|` — distinct count across the union of two cohorts
- datasketch_theta_intersect() — Approximate `|A ∩ B|` — distinct items present in both cohorts
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
col0
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Returns a human-readable description of the Theta sketch
SELECT datasketch_theta_describe(sketch);
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
col0
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Read the estimated distinct count from a Theta sketch.
SELECT datasketch_theta_estimate(sketch);
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() — Returns the lower bound estimate at the given number of standard deviations (1, 2, or 3)
- datasketch_theta_upper_bound() — Returns the upper bound estimate at the given number of standard deviations (1, 2, or 3)
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
col0
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Returns the seed hash used by the sketch
SELECT datasketch_theta_get_seed(sketch);
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
col0
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Returns the theta value of the sketch (sampling probability)
SELECT datasketch_theta_get_theta(sketch);
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
col0
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
col1
|
Type
sketch_theta
|
Mode Positional | Description |
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.
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|` — distinct count across the union of two 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
col0
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Returns true if the Theta sketch is empty
SELECT datasketch_theta_is_empty(sketch);
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
col0
|
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);
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
col0
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
col1
|
Type
INTEGER
|
Mode Positional | Description |
Description
Returns the lower bound estimate at the given number of standard deviations (1, 2, or 3)
SELECT datasketch_theta_lower_bound(sketch, 2);
Related functions
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
col0
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Returns the number of hash values retained in the sketch
SELECT datasketch_theta_num_retained(sketch);
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
col0
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
col1
|
Type
sketch_theta
|
Mode Positional | Description |
Description
Approximate |A ∪ B| — distinct count across the union of two cohorts. Aggregate over a sketch_theta column to merge per-day or per-shard sketches.
SELECT datasketch_theta_union(sketch1, sketch2);
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
col0
|
Type
sketch_theta
|
Mode Positional | Description |
Argument
col1
|
Type
INTEGER
|
Mode Positional | Description |
Description
Returns the upper bound estimate at the given number of standard deviations (1, 2, or 3)
SELECT datasketch_theta_upper_bound(sketch, 2);