Documentation

CslibFrontier.Computability.Streaming.JoinSize

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 #

Main results #

References #

The join sketch #

structure Cslib.Streaming.JoinState (ι : Type u_4) (r k : ℕ) :
Type u_4

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.

  • seeds : Fin (2 * r) → Fin k → ι

    The seed of each counter pair.

  • left : Fin (2 * r) → Fin k → ℤ

    The tug-of-war counters of the first relation.

  • right : Fin (2 * r) → Fin k → ℤ

    The tug-of-war counters of the second relation.

Instances For
    noncomputable def Cslib.Streaming.joinSketch {ι : Type u_1} {α : Type u_2} {Y : Type u_3} (h : ι → α → Y) (sgn : Y → ℤ) (r k : ℕ) (ω : Fin (2 * r) → Fin k → ι) :
    Algorithm (JoinState ι r k) (α ⊕ α) ℝ

    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
      noncomputable def Cslib.Streaming.joinSketchRandom {ι : Type u_1} {α : Type u_2} {Y : Type u_3} [Fintype ι] [Nonempty ι] (h : ι → α → Y) (sgn : Y → ℤ) (r k : ℕ) :
      Randomized (JoinState ι r k) (α ⊕ α) ℝ

      The randomized join sketch: the join sketch with independent uniform seeds.

      Instances For
        theorem Cslib.Streaming.state_joinSketch {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} (ω : Fin (2 * r) → Fin k → ι) (s : List (α ⊕ α)) :
        (joinSketch h sgn r k ω).state s = { seeds := ω, left := fun (i : Fin (2 * r)) (l : Fin k) => (List.map (fun (a : α) => sgn (h (ω i l) a)) (List.filterMap Sum.getLeft? s)).sum, right := fun (i : Fin (2 * r)) (l : Fin k) => (List.map (fun (a : α) => sgn (h (ω i l) a)) (List.filterMap Sum.getRight? s)).sum }

        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.

        theorem Cslib.Streaming.joinSketch_state_left {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} (ω : Fin (2 * r) → Fin k → ι) (s : List (α ⊕ α)) :
        ((joinSketch h sgn r k ω).state s).left = ((amsSketch h sgn r k ω).state (List.filterMap Sum.getLeft? s)).2

        The first relation's counters are the AMS sketch of its items, with the shared seeds.

        theorem Cslib.Streaming.joinSketch_state_right {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} (ω : Fin (2 * r) → Fin k → ι) (s : List (α ⊕ α)) :
        ((joinSketch h sgn r k ω).state s).right = ((amsSketch h sgn r k ω).state (List.filterMap Sum.getRight? s)).2

        The second relation's counters are the AMS sketch of its items, with the shared seeds.

        theorem Cslib.Streaming.run_joinSketch {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} [DecidableEq α] (ω : Fin (2 * r) → Fin k → ι) (s : List (α ⊕ α)) :
        (joinSketch h sgn r k ω).run s = Probability.medianOfMeans (fun (θ : ι) => (∑ a ∈ (List.filterMap Sum.getLeft? s).toFinset, ↑(List.count a (List.filterMap Sum.getLeft? s)) * ↑(sgn (h θ a))) * ∑ a ∈ (List.filterMap Sum.getRight? s).toFinset, ↑(List.count a (List.filterMap Sum.getRight? s)) * ↑(sgn (h θ a))) ω

        The answer of the join sketch is the median of means of the products of the tug-of-war sums of the two relations.

        theorem Cslib.Streaming.dens_joinSketch_error_le {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} [Fintype ι] [DecidableEq α] [Fintype Y] [DecidableEq Y] (hh : Probability.IsKWiseIndependentHash 4 h) (hsq : ∀ (y : Y), sgn y ^ 2 = 1) (hsum : ∑ y : Y, sgn y = 0) {η : ℝ} (hη : 0 < η) (hk : 6 ≤ ↑k * η ^ 2) (s : List (α ⊕ α)) :
        ↑{ω : Fin (2 * r) → Fin k → ι | η * √(↑(freqMoment 2 (List.filterMap Sum.getLeft? s)) * ↑(freqMoment 2 (List.filterMap Sum.getRight? s))) < |(joinSketch h sgn r k ω).run s - ↑(joinSize (List.filterMap Sum.getLeft? s) (List.filterMap Sum.getRight? s))|}.dens ≤ (8 / 9) ^ r

        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.

        theorem Cslib.Streaming.errorProb_joinSketchRandom {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} [Fintype ι] [Nonempty ι] (R : List (α ⊕ α) → ℝ → Prop) (s : List (α ⊕ α)) [DecidablePred fun (ω : Fin (2 * r) → Fin k → ι) => ¬R s ((joinSketch h sgn r k ω).run s)] :
        (joinSketchRandom h sgn r k).errorProb R s = ↑{ω : Fin (2 * r) → Fin k → ι | ¬R s ((joinSketch h sgn r k ω).run s)}.dens

        The error of the randomized join sketch is the fraction of bad seeds.

        theorem Cslib.Streaming.solves_joinSketchRandom {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} [Fintype ι] [Nonempty ι] [DecidableEq α] [Fintype Y] [DecidableEq Y] (hh : Probability.IsKWiseIndependentHash 4 h) (hsq : ∀ (y : Y), sgn y ^ 2 = 1) (hsum : ∑ y : Y, sgn y = 0) {η δ : ℝ} (hη : 0 < η) (hk : 6 ≤ ↑k * η ^ 2) (hδ : (8 / 9) ^ r ≤ δ) (m : ℕ) :

        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.

        theorem Cslib.Streaming.solves_joinSketchRandom_relative {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} [Fintype ι] [Nonempty ι] [DecidableEq α] [Fintype Y] [DecidableEq Y] (hh : Probability.IsKWiseIndependentHash 4 h) (hsq : ∀ (y : Y), sgn y ^ 2 = 1) (hsum : ∑ y : Y, sgn y = 0) {ε B₁ B₂ δ : ℝ} (hε : 0 < ε) (hB₁ : 0 < B₁) (hB₂ : 0 < B₂) (hk : 6 * B₂ ^ 2 ≤ ↑k * ε ^ 2 * B₁ ^ 2) (hδ : (8 / 9) ^ r ≤ δ) (m : ℕ) :

        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.

        theorem Cslib.Streaming.mem_Icc_joinSketch_state {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} (hsgn : ∀ (y : Y), |sgn y| ≤ 1) (ω : Fin (2 * r) → Fin k → ι) {m : ℕ} {s : List (α ⊕ α)} (hs : s.length ≤ m) (i : Fin (2 * r)) (l : Fin k) :
        ((joinSketch h sgn r k ω).state s).left i l ∈ Finset.Icc (-↑m) ↑m ∧ ((joinSketch h sgn r k ω).state s).right i l ∈ Finset.Icc (-↑m) ↑m

        On streams of at most m items, every counter of the join sketch lies in [-m, m].

        theorem Cslib.Streaming.spaceLE_joinSketchRandom {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} [Fintype ι] [Nonempty ι] (hsgn : ∀ (y : Y), |sgn y| ≤ 1) {m b : ℕ} (hb : (2 * m + 1) ^ (4 * r * k) ≤ 2 ^ b) :
        (joinSketchRandom h sgn r k).SpaceLE m b

        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.

        theorem Cslib.Streaming.exists_injOn_iUnion_reachable_joinSketch {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} [Fintype ι] (hsgn : ∀ (y : Y), |sgn y| ≤ 1) {m b : ℕ} (hb : (Fintype.card ι * (2 * m + 1) ^ 2) ^ (2 * r * k) ≤ 2 ^ b) :
        ∃ (enc : JoinState ι r k → BitString b), Set.InjOn enc (⋃ (ω : Fin (2 * r) → Fin k → ι), (joinSketch h sgn r k ω).reachable m)

        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.