sketch_tuple.nx
buildroot/runtime/sketch_tuple.nx
about
sketch_tuple.nx -- Tuple sketch (Theta + per-hash auxiliary data).
Extends the Theta sketch family by attaching a SCALAR value to each
hash slot. Use cases (matching DataSketches Tuple docs):
- sum-of-revenue across distinct users (one (hash, revenue) per user)
- max-recency across distinct items
- count-of-events-per-distinct-user
Multiple inserts of the same hash combine via a CALLER-CHOSEN reducer:
NX_TUPLE_RED_SUM — add (default for revenue / counts)
NX_TUPLE_RED_MAX — keep larger (for recency / peak)
NX_TUPLE_RED_MIN — keep smaller (for first-seen / earliest)
NX_TUPLE_RED_REPLACE— overwrite (last-write-wins)
THETA + AGGREGATE ESTIMATOR:
E[sum_over_set] ≈ (sum_over_sample) * HASH_MAX / theta
where the sum_over_sample is over the values < theta currently
stored. Theta-corrected like Theta cardinality estimator.
MERGE (capability beyond DataSketches Tuple-merge spec):
- theta_out = min(theta_a, theta_b)
- hashes_out = K smallest across union, filtered to < theta_out
- value_out for each surviving hash = reducer(a.val, b.val) if both
have it, else whichever side had it.
dependencies 3 imports · 1 importers
imports: syscalls.nxmurmur3.nxsketch_types.nx
imported by: sketch_tuple_test.nx
structs
| 37 | struct TupleEntry { |
| 42 | struct TupleSketch { |
consts
| 30 | const NX_TUPLE_HASH_MAX: i64 = 4294967296 |
| 32 | const NX_TUPLE_RED_SUM: i64 = 0 |
| 33 | const NX_TUPLE_RED_MAX: i64 = 1 |
| 34 | const NX_TUPLE_RED_MIN: i64 = 2 |
| 35 | const NX_TUPLE_RED_REPLACE: i64 = 3 |
functions
| 53 | func nx_tuple_reduce(reducer: i64, a: i64, b: i64) -> i64 { |
| 68 | func nx_tuple_alloc(k: i64, seed: i64, reducer: i64) -> *TupleSketch { |
| 85 | func nx_tuple_entry_at(t: *TupleSketch, i: i64) -> *TupleEntry { |
| 91 | func nx_tuple_add(t: *TupleSketch, key: *u8, key_len: i64, value: i64) -> i64 { |
| 157 | func nx_tuple_aggregate(t: *TupleSketch) -> i64 { |
| 183 | func nx_tuple_cardinality(t: *TupleSketch) -> i64 { |
| 199 | func nx_tuple_stddev_rel_ppb(k: i64) -> i64 { |
| 209 | func nx_tuple_query_aggregate(t: *TupleSketch) -> *ApproxI64 { |
| 218 | func nx_tuple_query_cardinality(t: *TupleSketch) -> *ApproxI64 { |
| 232 | func nx_tuple_min2(a: i64, b: i64) -> i64 {
called by 1: nx_tuple_merge |
| 237 | func nx_tuple_merge(a: *TupleSketch, b: *TupleSketch) -> *TupleSketch { |
| 320 | func nx_tuple_memory_bytes(t: *TupleSketch) -> i64 { |