Equivalence of SQL queries #
A query law says that two queries return the same result on every database instance. What "the
same" means depends on what the reader of the result may observe, so equivalence comes in three
levels (EquivLevel):
ordered: the same list of rows; nothing in this fragment orders rows, so this is a property of the list representation, used for laws that hold for it exactly;bag: the same rows with the same multiplicities, in any order (List.Perm), which is SQL's meaning of a query withoutORDER BYorDISTINCT;set: the same rows, ignoring multiplicities, which is whatDISTINCTorEXISTSsees.
Query.Equiv ℓ q₁ q₂ holds when, on every conforming instance, both queries fail or both
succeed with results related at level ℓ. Like expression equivalence (Expr.Equiv), it
observes Except.toOption: whether evaluation succeeds and with what, not which error it raises.
Ordered equivalence implies bag equivalence, which implies set equivalence (Query.Equiv.mono),
and each level is an equivalence relation.
Every query constructor is a congruence at every level, unconditionally in its query arguments
(Query.Equiv.select, Query.Equiv.project, Query.Equiv.join, Query.Equiv.unionAll,
Query.Equiv.distinct). In its expression arguments it is a congruence for expression
equivalence in the typing context of its input (Query.Equiv.select_expr,
Query.Equiv.project_expr, Query.Equiv.join_expr), which needs the input to be well-typed, so
that its rows conform to that context. Over WHERE c, an outer predicate can be replaced in the
typing context of the inner input whatever the type of c, since c keeps some of the input's
rows (Query.Equiv.select_select_expr). DISTINCT sees only sets: set-equivalent inputs give
bag-equivalent DISTINCT (Query.Equiv.distinct_of_set).
It adapts and redesigns the equivalence levels of sql_equiv (revision 38030a6b) by Evelyn
Mitchell, which compared results on every database, with errors identified with empty or missing
results; here equivalences quantify over the instances conforming to the schema and keep a
failure distinct from every result.
Main definitions #
Cslib.SQL.EquivLevel: the ordered, bag and set levels, ordered by how much they observe.Cslib.SQL.EquivLevel.Rel: lists related at a level.Cslib.SQL.Query.Equiv: equivalence of queries at a level, over conforming instances.
Main results #
Cslib.SQL.Query.Equiv.mono: ordered ⇒ bag ⇒ set.Cslib.SQL.Query.equivSetoid: each level is an equivalence relation.Cslib.SQL.Query.Equiv.select,Cslib.SQL.Query.Equiv.project,Cslib.SQL.Query.Equiv.join,Cslib.SQL.Query.Equiv.unionAll,Cslib.SQL.Query.Equiv.distinct: congruence in the query arguments.Cslib.SQL.Query.Equiv.select_expr,Cslib.SQL.Query.Equiv.project_expr,Cslib.SQL.Query.Equiv.join_expr: congruence in the expression arguments.Cslib.SQL.Query.Equiv.select_select_expr: congruence in the outer predicate ofWHEREoverWHERE, in the typing context of the inner input.Cslib.SQL.Query.Equiv.distinct_of_set: set-equivalent inputs give bag-equivalentDISTINCT.
How much of a query's result an equivalence observes.
- ordered : EquivLevel
The list of rows, in order.
- bag : EquivLevel
The bag of rows: the list up to order.
- set : EquivLevel
The set of rows: which rows occur, not how often.
Instances For
Distinct levels have distinct ranks.
Two lists are related at the level: equal; permutations of each other; or with the same members.
Instances For
Lists related at a level have the same members.
A relation at a finer level holds at every coarser one.
A list operation that respects permutations, and whose members depend only on the members of its input, relates related lists at every level.
Lists with the same members, related at any level, have permuted deduplications.
WHERE with predicates that agree on the members of related inputs gives related results,
or fails on both.
SELECT lists that agree on the members of related inputs give related results, or fail on
both.
Queries q₁ and q₂ are equivalent at level ℓ: on every instance conforming to the
schema, both fail, or both succeed with results related at ℓ.
Instances For
Equivalence at a finer level implies equivalence at every coarser one: ordered ⇒ bag ⇒ set.
Ordered equivalence implies equivalence at every level.
Congruence in the query arguments #
DISTINCT observes only the set of rows of its input: set-equivalent inputs give
bag-equivalent results.
Congruence in the expression arguments #
Replacing a WHERE predicate by an equivalent one in the typing context of the input gives an
equivalent query.
Replacing the outer predicate of WHERE over WHERE c by an equivalent one in the typing
context of the inner input gives an equivalent query, whatever the type of c: the rows that c
keeps are rows of the input, so they conform to that context. Query.Equiv.select_expr would need
WHERE c itself to be well-typed.
Replacing the expressions of a SELECT list by equivalent ones in the typing context of the
input gives an equivalent query.
Replacing a join predicate by an equivalent one in the typing context of the joined rows gives an equivalent query.
Equivalence at the level ℓ, as a setoid.