Proved SQL query transformations #
The rewrite laws of the select–project–join–union fragment, each at the strongest equivalence level
that holds (Cslib.SQL.Query.Equiv) and with the side conditions that strict evaluation needs.
Evaluation fails as soon as a predicate or projection fails on any row of its input, so a law that
changes the rows on which an expression is evaluated holds only when that evaluation cannot fail.
Its side condition is then the decidable safety check (Expr.IsSafePredicate, Expr.IsSafe) of
that expression, in the typing context of its input, which must be well-typed (q.typeOf = some Γ).
Laws that evaluate every expression on the same rows on both sides need no condition. The tests
show for each condition a concrete instance on which the law fails without it.
- Selections: merge into a conjunction (
Query.select_select, the outer predicate safe), commute (Query.select_comm, both safe), move through a projection, as when aWHEREover a derived table moves into it with the projection substituted (Query.select_project, the projection safe), and push into either input of a join (Query.select_join_left,Query.select_join_right, the predicate and the join predicate safe); aWHEREover a cross join is an inner join (Query.select_join_true), andWHERE TRUEis the identity (Query.select_true). - Projections: fusion by substitution (
Query.project_project, the inner projection safe) andSELECT *(Query.project_col). UNION ALL:WHEREandSELECTlists distribute over it (Query.select_unionAll,Query.project_unionAll), it is associative (Query.unionAll_assoc), and it is commutative as a bag only (Query.unionAll_comm).- Joins are commutative as bags, exchanging the blocks of columns of the predicate and the result
through
finAddFlip(Query.join_comm). DISTINCTis idempotent, absorbs aUNION ALLof a query with itself, and commutes withWHERE(Query.distinct_distinct,Query.distinct_unionAll_self,Query.select_distinct).
All laws are ordered equivalences except the two commutativity laws, which hold for bags only.
It adapts and redesigns the query laws of sql_equiv (revision 38030a6b) by Evelyn Mitchell.
There, a WHERE predicate that failed on a row dropped that row, so filter laws needed no
conditions, and pushdown passed column names through derived tables; here a failure fails the
query, each law states the safety condition it needs, and moving a predicate across a derived
table or into a join is explicit substitution or renaming.
Main results #
Cslib.SQL.Query.select_select,Cslib.SQL.Query.select_comm: selection merge and commutation.Cslib.SQL.Query.select_project: selection through a projection.Cslib.SQL.Query.select_join_left,Cslib.SQL.Query.select_join_right,Cslib.SQL.Query.select_join_true: predicate pushdown into joins.Cslib.SQL.Query.select_true:WHERE TRUEkeeps every row.Cslib.SQL.Query.project_project,Cslib.SQL.Query.project_col: projection fusion andSELECT *.Cslib.SQL.Query.select_unionAll,Cslib.SQL.Query.project_unionAll,Cslib.SQL.Query.unionAll_assoc,Cslib.SQL.Query.unionAll_comm:UNION ALLlaws.Cslib.SQL.Query.join_comm: join commutativity as bags.Cslib.SQL.Query.distinct_distinct,Cslib.SQL.Query.distinct_unionAll_self,Cslib.SQL.Query.select_distinct:DISTINCTlaws.Cslib.SQL.Query.eval_select_of_isSafePredicate,Cslib.SQL.Query.eval_join_of_isSafePredicate: a safeWHEREor join predicate is a filter.Cslib.SQL.Expr.IsSafePredicate.filterRows_eq_ok,Cslib.SQL.Expr.toOption_filterRows_map,Cslib.SQL.Expr.toOption_evalTruth_and: what the laws need aboutWHEREon lists of rows.
A safe predicate never makes WHERE fail on conforming rows: it keeps the rows on which the
predicate is TRUE.
c AND p as a predicate: both sides are evaluated, and the truth value is their Kleene
conjunction.
WHERE on two lists one after the other is WHERE on their concatenation.
Selections #
Selection merge: two successive WHERE clauses are one WHERE with their conjunction, when
the outer predicate is safe. The conjunction evaluates p on rows that the inner WHERE drops,
so an unsafe p could make only the right side fail.
Selection commutation: two safe WHERE clauses may be applied in either order.
Selection through a projection, or a WHERE over a derived table: filtering the rows of
SELECT es FROM q by p is filtering the rows of q by p with the projection substituted,
then projecting, when the projection is safe. The right side evaluates es only on the rows it
keeps, so an unsafe projection could make only the left side fail.
A safe WHERE over a well-typed query is a filter of its rows.
A join with a safe predicate over well-typed inputs filters the pairs of input rows, as
Query.eval_join_eq_ok_of_forall says, whenever both inputs succeed.
Predicate pushdown into the left input of a join: a safe predicate on the left columns may be
applied before the join, when the join predicate is safe too. Without safety the sides evaluate
the predicates on different rows: with an empty right input, the left side evaluates nothing
while the right side evaluates p on every left row.
Predicate pushdown into the right input of a join, under the same conditions as
Query.select_join_left.
A WHERE over a cross join is an inner join ON the same predicate. Both evaluate the
predicate on every pair, so no condition is needed.
WHERE TRUE keeps every row.
Projections #
Projection fusion: projecting twice is projecting once with the inner projection substituted, when the inner projection is safe. The fused projection evaluates only the inner expressions that the outer one uses, so an unsafe inner projection could make only the left side fail.
SELECT *: projecting on every column in order is the identity.
UNION ALL #
UNION ALL is commutative as a bag. It is not as a list: the left input comes first.
Joins #
Join commutativity: exchanging the inputs of a join, and the two blocks of columns of its predicate and its result, gives the same bag of rows. The pairs are enumerated in another order, so it is not an equality of lists.
DISTINCT #
DISTINCT is idempotent.
DISTINCT ignores the duplicates that UNION ALL of a query with itself creates.
WHERE commutes with DISTINCT. The left side evaluates p on one copy of each row and the
right side on every copy, but the copies are equal rows, so p has the same result, or fails, on
all of them, and no condition is needed. Both sides keep the last copy of each kept row.