cslib-frontier guide

6.3. Equijoins and Join Sizes🔗

The equijoin emp JOIN dept ON emp.dept = dept.id uses the predicate Cslib.SQL.Expr.eqCols, which compares a column of the left input with a column of the right input. Each of the three employees in department 1 meets both departments with id 1. The NULL keys match nothing, not even each other, because NULL = NULL is unknown.

abbrev empDept : Query S 4 := .join (Expr.eqCols (1 : Fin 2) (0 : Fin 2)) emp dept example : (empDept.eval D).toOption.map List.length = some 6 := ⊢ Option.map List.length (eval D empDept).toOption = some 6 All goals completed! 🐙

The streaming library's Cslib.Streaming.joinSize u v is ∑ₐ count a u · count a v. Cslib.SQL.Query.eval_join_table_eqCols_length_eq_joinSize_filterMap says that, on every conforming instance, the equijoin of two tables returns the join size of their non-null keys. The key columns must be declared with the same type, which rfl checks here.

example (D' : Database S) (h : D'.Conforms) : ∃ ts, empDept.eval D' = .ok ts ∧ ts.length = Cslib.Streaming.joinSize ((D' Tbl.emp).filterMap (· 1)) ((D' Tbl.dept).filterMap (· 0)) := eval_join_table_eqCols_length_eq_joinSize_filterMap 1 0 h rfl example : Cslib.Streaming.joinSize ((D Tbl.emp).filterMap (· 1)) ((D Tbl.dept).filterMap (· 0)) = 6 := ⊢ Cslib.Streaming.joinSize (List.filterMap (fun x => x 1) (D Tbl.emp)) (List.filterMap (fun x => x 0) (D Tbl.dept)) = 6 All goals completed! 🐙

Counting with NULL equal to NULL would pair cy with the tmp department. That count is correct when one input has no NULL key (Cslib.SQL.Query.eval_join_eqCols_length_eq_joinSize); here both inputs have one.