The tug-of-war join signature for join sizes #
The join size of two relations with frequency vectors f and g on a common join attribute is
the inner product |F ⋈ G| = ∑ₐ fₐ gₐ; for streams u and v of attribute values it is
Cslib.Streaming.joinSize u v = ∑ₐ count a u · count a v, the number of tuples in the equijoin
(Cslib.Streaming.length_filter_product_eq_joinSize). Its self-join joinSize u u is the
second frequency moment F₂ u (Cslib.Streaming.joinSize_self), and joinSize u v ^ 2 ≤ F₂ u F₂ v
(Cslib.Streaming.joinSize_sq_le). These are in CslibFrontier.Computability.Streaming.Frequency,
which needs no probability.
Alon, Gibbons, Matias and Szegedy [AlonGibbonsMatiasSzegedy1999, Section 4.3] estimate join sizes
with tug-of-war join signatures: each relation keeps a tug-of-war counter
S(F) = ∑ₐ fₐ sgn (h θ a) of the AMS sketch, with the seed θ shared by the two relations, and
S(F) S(G) estimates |F ⋈ G| with variance at most 2 F₂ F₂
(Cslib.Probability.IsKWiseIndependentHash.expect_sum_mul_sign_mul_sum_mul_sign,
Cslib.Probability.IsKWiseIndependentHash.expect_sq_sum_mul_sign_mul_sum_mul_sign_sub_le).
The join sketch (Cslib.Streaming.joinSketch) reads one stream of tagged items α ⊕ α: an item
Sum.inl a adds a tuple with attribute value a to the first relation and Sum.inr a to the
second, in any interleaving, so the relations are s.filterMap Sum.getLeft? and
s.filterMap Sum.getRight?. It keeps 2 r groups of k seeds, each with one counter per
relation, and answers the median over the groups of the average product of the two counters
(Cslib.Streaming.run_joinSketch). Each relation's counters depend only on its own items
(Cslib.Streaming.joinSketch_state_left, Cslib.Streaming.joinSketch_state_right): they are the
AMS sketch of that relation with the shared seeds, so the two signatures could equally be maintained
separately and combined at query time, as in the source.
With a four-wise independent family of signs of mean zero and 6 ≤ k η², the answer is within
η √(F₂(F) F₂(G)) of the join size except for an (8 / 9) ^ r fraction of the seeds
(Cslib.Streaming.dens_joinSketch_error_le, Cslib.Streaming.solves_joinSketchRandom). This is
[AlonGibbonsMatiasSzegedy1999, Theorem 4.5] with the median trick added: given a lower bound
B₁ ≤ |F ⋈ G| and upper bounds F₂(F), F₂(G) ≤ B₂, taking 6 B₂² ≤ k ε² B₁² gives relative error
ε (Cslib.Streaming.solves_joinSketchRandom_relative), so k = O(B₂² / (ε² B₁²)) copies suffice
as in the source, and r = O(log δ⁻¹) groups reduce the failure probability to δ.
Memory. On streams of at most m items every counter lies in [-m, m]. With the seeds not
charged (the public-coin convention of Cslib.Streaming.Randomized), every sketch drawn uses b
bits once (2 m + 1) ^ (4 r k) ≤ 2 ^ b (Cslib.Streaming.spaceLE_joinSketchRandom); with the
seeds stored, once (|ι| (2 m + 1) ^ 2) ^ (2 r k) ≤ 2 ^ b
(Cslib.Streaming.exists_injOn_iUnion_reachable_joinSketch). With the four-wise independent
polynomial hash families on a domain of t attribute values (log₂ |ι| = O(log t)), this is
O(r k (log t + log m)) bits. In the source, n is the number of tuples in each relation, not
the domain size, and the hash functions are not charged, so its k log n bits per relation
count only the k counters, matching the public-coin bound above.
Scope. The relations are insert-only: the source also handles deletions by subtracting the
sign, which would need turnstile stream items, not formalized here. The answer is the median of
means rather than the source's plain mean, which gives the (8 / 9) ^ r failure bound in place of
Chebyshev's constant failure probability.
Main definitions #
Cslib.Streaming.JoinState: the memory of the join sketch.Cslib.Streaming.joinSketch: the join sketch with given seeds.Cslib.Streaming.joinSketchRandom: the randomized join sketch with uniform seeds.
Main results #
Cslib.Streaming.joinSketch_state_left,Cslib.Streaming.joinSketch_state_right,Cslib.Streaming.run_joinSketch: the counters are the AMS sketches of the two relations, and the answer is the median of means of their products.Cslib.Streaming.dens_joinSketch_error_le,Cslib.Streaming.solves_joinSketchRandom,Cslib.Streaming.solves_joinSketchRandom_relative: the join sketch estimates the join size withinη √(F₂(F) F₂(G)), or within relative errorεunder a priori bounds, except with probability(8 / 9) ^ r.Cslib.Streaming.spaceLE_joinSketchRandom,Cslib.Streaming.exists_injOn_iUnion_reachable_joinSketch: its memory without and with the seeds.
References #
The join sketch #
The memory of the join sketch with 2 r groups of k seeds: the seeds, and for each seed a
tug-of-war counter of each relation.
The seed of each counter pair.
The tug-of-war counters of the first relation.
The tug-of-war counters of the second relation.
Instances For
The join sketch [AlonGibbonsMatiasSzegedy1999, Section 4.3] with seeds ω, reading a
stream of tagged items: Sum.inl a adds the sign sgn (h θ a) to the first relation's counter with
seed θ, and Sum.inr a to the second relation's. The answer is the median over the 2 r groups
of the average product of the two counters. The seeds are written in the initial memory and read
back by the update.
Instances For
After the stream s, the counters with seed θ hold ∑_{a ∈ F} sgn (h θ a) and
∑_{a ∈ G} sgn (h θ a), where F and G are the items tagged Sum.inl and Sum.inr.
The first relation's counters are the AMS sketch of its items, with the shared seeds.
The second relation's counters are the AMS sketch of its items, with the shared seeds.
The answer of the join sketch is the median of means of the products of the tug-of-war sums of the two relations.
The join sketch estimates join sizes [AlonGibbonsMatiasSzegedy1999, Lemma 4.4 and
Theorem 4.5]: with a four-wise independent family of signs of mean zero and 6 ≤ k η², the join
sketch with 2 r groups of k counter pairs misses the join size of the two relations by more
than η √(F₂(F) F₂(G)) for at most an (8 / 9) ^ r fraction of the seeds.
The error of the randomized join sketch is the fraction of bad seeds.
The join sketch solves join size estimation [AlonGibbonsMatiasSzegedy1999, Theorem 4.5]:
with a four-wise independent family of signs of mean zero, 6 ≤ k η² and (8 / 9) ^ r ≤ δ, the
randomized join sketch estimates the join size of the two relations within η √(F₂(F) F₂(G)) with
error at most δ on every stream.
Relative error under a priori bounds [AlonGibbonsMatiasSzegedy1999, Theorem 4.5]: if the
join size is known to be at least B₁ > 0 and the self-join sizes at most B₂, then
k ≥ 6 B₂² / (ε² B₁²) counter pairs per group give relative error ε except with probability
(8 / 9) ^ r.
On streams of at most m items, every counter of the join sketch lies in [-m, m].
The memory of the randomized join sketch [AlonGibbonsMatiasSzegedy1999, Section 4.3]:
every sketch it draws uses at most b bits of memory on streams of at most m items when
(2 m + 1) ^ (4 r k) ≤ 2 ^ b. Under the public-coin convention of Cslib.Streaming.Randomized
only the 4 r k counters are charged; the seeds are not. For the count with the seeds stored see
Cslib.Streaming.exists_injOn_iUnion_reachable_joinSketch.
The memory of the join sketch with its seeds stored [AlonGibbonsMatiasSzegedy1999,
Section 4.3]: on streams of at most m items, the states reachable from all seeds together have a
single injective encoding by b bits as soon as (|ι| (2 m + 1) ^ 2) ^ (2 r k) ≤ 2 ^ b.