Documentation

CslibFrontier.Languages.SQL.JoinSize

Equijoin row counts are join sizes #

An equijoin q₁ JOIN q₂ ON q₁.i = q₂.j pairs the rows of its inputs whose key columns are equal. Its predicate is Expr.eqCols i j (in Cslib.SQL.Query), which compares column i of the left input with column j of the right one in the joined row. This module connects SQL evaluation of such a join to the join size of the streaming library, Cslib.Streaming.joinSize u v = ∑ₐ count a u · count a v: the number of rows an equijoin returns is the join size of the lists of key values of its inputs.

The key domain is the non-null values of one SQL type τ, on which = is TRUE exactly for equal values (Expr.evalTruth_eqCols_eq_ok_true_iff). A NULL key matches nothing, not even another NULL, because NULL = NULL is unknown and ON keeps only TRUE pairs. The faithful count therefore drops the NULL keys first: joinSize (rs.filterMap (· i)) (ss.filterMap (· j)), where (· i) : Row n → Option Value reads the key and filterMap keeps only the non-null ones (Query.eval_join_eqCols_length_eq_joinSize_filterMap). Counting with structural equality of datums, joinSize (rs.map (· i)) (ss.map (· j)), would also pair NULL with NULL; it gives the same count when one input has no NULL key (Query.eval_join_eqCols_length_eq_joinSize). On a conforming instance, a well-typed query, and in particular a table, supplies the typing hypothesis by itself (Query.eval_join_eqCols_length_eq_joinSize_filterMap_of_typeOf, Query.eval_join_table_eqCols_length_eq_joinSize_filterMap).

The count itself needs no typing hypothesis: whenever the equijoin succeeds, its rows number the join size of the non-null keys (Query.length_eq_joinSize_filterMap_of_eval_join_eqCols_eq_ok), which applies Cslib.Streaming.length_filter_product_eq_joinSize_filterMap once. The typing hypothesis of the other forms guarantees success (Query.eval_join_eq_ok_of_forall); without it the join may fail, for instance on an integer key compared with a text key.

It adapts and redesigns material from sql_equiv (revision 38030a6b) by Evelyn Mitchell, whose joins compared name-addressed columns at run time; the connection to join sizes is new.

Main results #

theorem Cslib.SQL.Query.length_eq_joinSize_filterMap_of_eval_join_eqCols_eq_ok {n m : ℕ} {S : DbSchema} {q₁ : Query S n} {q₂ : Query S m} {D : Database S} {rs : List (Row n)} {ss : List (Row m)} {i : Fin n} {j : Fin m} {ts : List (Row (n + m))} (h₁ : eval D q₁ = Except.ok rs) (h₂ : eval D q₂ = Except.ok ss) (h : eval D (join (Expr.eqCols i j) q₁ q₂) = Except.ok ts) :
ts.length = Streaming.joinSize (List.filterMap (fun (x : Row n) => x i) rs) (List.filterMap (fun (x : Row m) => x j) ss)

Equijoin row counts are join sizes, whenever the join succeeds. If q₁ and q₂ evaluate to rs and ss and the equijoin q₁ JOIN q₂ ON q₁.i = q₂.j succeeds with ts, then ts has joinSize (rs.filterMap (· i)) (ss.filterMap (· j)) rows: the join size of the non-null keys of both inputs. No typing hypothesis is needed, since a key comparison that fails fails the join. A NULL key matches nothing, since SQL's = is unknown on NULL; on non-null values = is TRUE exactly on equal values.

theorem Cslib.SQL.Query.eval_join_eqCols_length_eq_joinSize_filterMap {n m : ℕ} {S : DbSchema} {q₁ : Query S n} {q₂ : Query S m} {D : Database S} {rs : List (Row n)} {ss : List (Row m)} (i : Fin n) (j : Fin m) (h₁ : eval D q₁ = Except.ok rs) (h₂ : eval D q₂ = Except.ok ss) {τ : SqlType} (hrs : ∀ r ∈ rs, (r i).HasType τ) (hss : ∀ s ∈ ss, (s j).HasType τ) :
∃ (ts : List (Row (n + m))), eval D (join (Expr.eqCols i j) q₁ q₂) = Except.ok ts ∧ ts.length = Streaming.joinSize (List.filterMap (fun (x : Row n) => x i) rs) (List.filterMap (fun (x : Row m) => x j) ss)

Equijoin row counts are join sizes. If q₁ and q₂ evaluate to rs and ss, and the key columns i and j hold NULL or values of one type τ, then the equijoin q₁ JOIN q₂ ON q₁.i = q₂.j succeeds and returns joinSize (rs.filterMap (· i)) (ss.filterMap (· j)) rows: the join size of the non-null keys of both inputs. The typing hypothesis gives success; Query.length_eq_joinSize_filterMap_of_eval_join_eqCols_eq_ok gives the count.

theorem Cslib.SQL.Query.eval_join_eqCols_length_eq_joinSize {n m : ℕ} {S : DbSchema} {q₁ : Query S n} {q₂ : Query S m} {D : Database S} {rs : List (Row n)} {ss : List (Row m)} (i : Fin n) (j : Fin m) (h₁ : eval D q₁ = Except.ok rs) (h₂ : eval D q₂ = Except.ok ss) {τ : SqlType} (hrs : ∀ r ∈ rs, (r i).HasType τ) (hss : ∀ s ∈ ss, (s j).HasType τ) (hnull : (∀ r ∈ rs, r i ≠ none) ∨ ∀ s ∈ ss, s j ≠ none) :
∃ (ts : List (Row (n + m))), eval D (join (Expr.eqCols i j) q₁ q₂) = Except.ok ts ∧ ts.length = Streaming.joinSize (List.map (fun (x : Row n) => x i) rs) (List.map (fun (x : Row m) => x j) ss)

Equijoin row counts with NULL as a value. Under the hypotheses of Query.eval_join_eqCols_length_eq_joinSize_filterMap, if one of the inputs has no NULL key, the equijoin returns joinSize (rs.map (· i)) (ss.map (· j)) rows: comparing the keys as datums, with NULL equal to NULL, counts the same pairs. When both inputs have a NULL key, it also counts the pairs of NULL keys, which the join does not return.

theorem Cslib.SQL.Query.eval_join_eqCols_length_eq_joinSize_filterMap_of_typeOf {n m : ℕ} {S : DbSchema} {q₁ : Query S n} {q₂ : Query S m} {D : Database S} {rs : List (Row n)} {ss : List (Row m)} (i : Fin n) (j : Fin m) (hD : D.Conforms) {Γ : Fin n → SqlType} {Δ : Fin m → SqlType} (hq₁ : q₁.typeOf = some Γ) (hq₂ : q₂.typeOf = some Δ) (hτ : Γ i = Δ j) (h₁ : eval D q₁ = Except.ok rs) (h₂ : eval D q₂ = Except.ok ss) :
∃ (ts : List (Row (n + m))), eval D (join (Expr.eqCols i j) q₁ q₂) = Except.ok ts ∧ ts.length = Streaming.joinSize (List.filterMap (fun (x : Row n) => x i) rs) (List.filterMap (fun (x : Row m) => x j) ss)

Equijoin row counts for well-typed inputs. On a conforming instance, if q₁ and q₂ are well-typed, their key columns i and j have the same type, and both evaluate successfully, the equijoin returns as many rows as the join size of their non-null keys.

theorem Cslib.SQL.Query.eval_join_table_eqCols_length_eq_joinSize_filterMap {S : DbSchema} {D : Database S} {t₁ t₂ : S.Table} (i : Fin (S.arity t₁)) (j : Fin (S.arity t₂)) (hD : D.Conforms) (hτ : S.ctx t₁ i = S.ctx t₂ j) :
∃ (ts : List (Row (S.arity t₁ + S.arity t₂))), eval D (join (Expr.eqCols i j) (table t₁) (table t₂)) = Except.ok ts ∧ ts.length = Streaming.joinSize (List.filterMap (fun (x : Row (S.arity t₁)) => x i) (D t₁)) (List.filterMap (fun (x : Row (S.arity t₂)) => x j) (D t₂))

Equijoin row counts for tables. On a conforming instance D, the equijoin of tables t₁ and t₂ on key columns i and j declared with the same type returns joinSize ((D t₁).filterMap (· i)) ((D t₂).filterMap (· j)) rows.