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 #
Cslib.SQL.Query.length_eq_joinSize_filterMap_of_eval_join_eqCols_eq_ok: whenever an equijoin succeeds, it returns as many rows as the join size of the inputs' non-null keys.Cslib.SQL.Query.eval_join_eqCols_length_eq_joinSize_filterMap: an equijoin on keys of one type succeeds and returns as many rows as the join size of the inputs' non-null keys.Cslib.SQL.Query.eval_join_eqCols_length_eq_joinSize: the same count withNULLtreated as a value, when one input has noNULLkey.Cslib.SQL.Query.eval_join_eqCols_length_eq_joinSize_filterMap_of_typeOf,Cslib.SQL.Query.eval_join_table_eqCols_length_eq_joinSize_filterMap: on a conforming instance, for well-typed inputs and for tables, with key columns of the same declared type.
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.
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.
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.
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.
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.