SQL queries with bag semantics #
Query S n is a query over the database schema S whose result rows have n columns: a table,
WHERE (Query.select), a SELECT list (Query.project), an inner join ON θ
(Query.join; a cross join is ON TRUE), UNION ALL (Query.unionAll) and DISTINCT
(Query.distinct). The output arity is the type index, so a projection list or a join predicate
cannot refer to a column its input lacks.
Query.eval D q evaluates q on the instance D and returns a list of rows or an error. The
list is a bag: projection, join and UNION ALL keep duplicates, and only DISTINCT removes them,
treating rows with NULL in the same places as duplicates, as SQL's DISTINCT does (Mathlib's
List.dedup, which keeps the last copy). The list order is a property of this representation,
not of SQL, which promises no order without ORDER BY: here a table keeps its list order,
WHERE keeps the order of its input, a join enumerates pairs with the left row outermost, and
UNION ALL lists its left input first. The equivalences in Cslib.SQL.Query.Equiv decide how
much of the order a law may rely on.
Evaluation is strict. A WHERE or ON predicate is evaluated on every input row or pair, and a
projection on every input row, from left to right; the first error fails the whole query, even
when an engine might never have reached the failing row. On an empty input nothing is evaluated,
so the same query succeeds (Expr.filterRows, Expr.evalRow).
Query.typeOf q is the typing context of the result, if q is well-typed: predicates are
boolean in the context of their input, projections are well-typed, and UNION ALL combines two
inputs of the same types. Type soundness (Query.eval_ok_or_divisionByZero): on a conforming
instance, a well-typed query returns rows conforming to its type, or fails with
divisionByZero, which requires a division that is not divSafe; it never raises
typeMismatch. The decidable check Query.IsSafe (well-typed, every division by a nonzero
literal) guarantees that evaluation succeeds on every conforming instance.
It adapts and redesigns the queries of sql_equiv (revision 38030a6b) by Evelyn Mitchell. There,
queries were untyped syntax whose column names were looked up in the rows at run time, and the
evaluator for the richer fragment (aggregates, subqueries, set operations) was partial. Here the
fragment is the select–project–join–union core with DISTINCT, columns are positions checked by
the type index, evaluation is structural and total, and errors are results rather than rows
silently dropped.
Main definitions #
Cslib.SQL.Query: queries over a schema, indexed by their output arity.Cslib.SQL.Query.eval: strict evaluation on a database instance.Cslib.SQL.Expr.filterRows,Cslib.SQL.Expr.evalRow:WHEREandSELECTlists on a list of rows and on one row.Cslib.SQL.Row.product: the rows of a cross join.Cslib.SQL.Expr.eqCols: the equijoin predicateq₁.i = q₂.jover a joined row.Cslib.SQL.Query.typeOf: the typing context of a query's result.Cslib.SQL.Query.divSafe,Cslib.SQL.Query.IsSafe: the decidable safety check.
Main results #
Cslib.SQL.Expr.toOption_filterRows,Cslib.SQL.Expr.evalRow_eq_ok_iff: whatWHEREand aSELECTlist compute when they succeed, and when they do.Cslib.SQL.Expr.mem_of_filterRows_eq_ok:WHEREkeeps some of its input rows.Cslib.SQL.Query.eval_join_eq_ok_of_forall: a join whose predicate succeeds on every pair returns the joined pairs on which it isTRUE.Cslib.SQL.Expr.evalTruth_eqCols_eq_ok_true_iff,Cslib.SQL.Expr.isSafePredicate_eqCols_iff: the equijoin predicate keeps the pairs with the same non-null key, and is safe exactly when the key columns have the same type.Cslib.SQL.Query.eval_ok_or_divisionByZero,Cslib.SQL.Query.eval_spec: type soundness, also as a postcondition.Cslib.SQL.Query.eval_ne_typeMismatch,Cslib.SQL.Query.conforms_of_eval_eq_ok: a well-typed query never raises a type error, and its rows have its types.Cslib.SQL.Query.IsSafe.exists_eval_eq_ok: a safe query succeeds on every conforming instance.
The rows of a cross join of conforming rows conform to the joined typing context.
SELECT lists and WHERE on rows #
When a SELECT list fails, one of its expressions fails with that error.
WHERE succeeds exactly when the predicate succeeds on every row, and then keeps the rows on
which it is TRUE, in order.
Type soundness for predicates: on a conforming row, a boolean predicate has a truth value, or
divides by zero, which requires a division that is not divSafe.
Equijoin predicates #
The equijoin predicate q₁.i = q₂.j over the joined row of an n-column left input and an
m-column right input: column i of the left input (Fin.castAdd m i) equals column j of the
right input (Fin.natAdd n j).
Instances For
Queries #
A query over the schema S whose result rows have n columns.
- table {S : DbSchema} (t : S.Table) : Query S (S.arity t)
The rows of a table, in the instance's list order.
- select {S : DbSchema} {n : ℕ} (p : Expr n) (q : Query S n) : Query S n
SELECT * FROM q WHERE p: the rows ofqon whichpisTRUE. - project {S : DbSchema} {n k : ℕ} (es : Fin k → Expr n) (q : Query S n) : Query S k
SELECT es₀, …, esₖ₋₁ FROM q: one row of results per row ofq; duplicates are kept. - join {S : DbSchema} {n m : ℕ} (θ : Expr (n + m)) (q₁ : Query S n) (q₂ : Query S m) : Query S (n + m)
q₁ JOIN q₂ ON θ: the joined rowsFin.append r sof the pairs on whichθisTRUE. The left columns come first; a cross join isON TRUE. - unionAll {S : DbSchema} {n : ℕ} (q₁ q₂ : Query S n) : Query S n
q₁ UNION ALL q₂: the rows of both, with duplicates. - distinct {S : DbSchema} {n : ℕ} (q : Query S n) : Query S n
SELECT DISTINCT * FROM q: one copy of each row ofq. Rows withNULLin the same places and equal values elsewhere are duplicates.
Instances For
When the join predicate θ succeeds on every pair of input rows, the join succeeds and
returns the joined rows of the pairs on which θ is TRUE, in the order of rs ×ˢ ss.
Typing and safety #
The typing context of the query's result rows, if the query is well-typed: WHERE and ON
predicates are boolean over their input, the expressions of a SELECT list are well-typed, and
the inputs of UNION ALL have the same types.
Instances For
The query is well-typed and every division in it is by a nonzero literal; then it succeeds
on every conforming instance (Query.IsSafe.exists_eval_eq_ok).
Instances For
Type soundness as a postcondition: on a conforming instance, a well-typed query returns rows of
its type, or divides by zero, which requires a division that is not divSafe.
Type soundness: on a conforming instance, a well-typed query returns rows of its type, or
divides by zero, which requires a division that is not divSafe.
The rows of a well-typed query on a conforming instance conform to its type.