A proof-producing SQL simplifier #
Expr.simplify Γ e simplifies an expression over the typing context Γ, and
Query.simplify q simplifies a query. Both are ordinary functions that the kernel evaluates, and
each comes with its certificate: Expr.equiv_simplify says that e and e.simplify Γ are
equivalent on every row conforming to Γ (Expr.Equiv), and Query.equiv_simplify that q and
q.simplify are ordered-equivalent on every conforming instance (Query.Equiv .ordered), so a
fact about the result of one transfers to the other at every level. Equivalence observes failure
under strict evaluation, so no rewrite turns a failing expression or query into a succeeding one,
or the reverse.
Every rewrite is one of the laws of Cslib.SQL.Expr.Equiv or Cslib.SQL.Query. A rewrite whose
law has a side condition is guarded by the decidable check of that condition (a type from
Expr.typeOf, safety from Expr.IsSafe or Expr.IsSafePredicate, in the typing context of the
input from Query.typeOf), and when the check fails the node is left as it is.
Expressions #
Expr.simplify works bottom up: it simplifies the operands of a node, then rewrites the node once
(Expr.simpUnary, Expr.simpBinary, Expr.simpInList, Expr.simpCaseWhen):
- an operator or
INwhose operands are literals is evaluated, and replaced by the literal of its result when evaluation succeeds; an error is never folded, so1 / 0stays; NOT (a op b)becomesa (negate op) b,NOT (e IS NULL)becomese IS NOT NULLand the reverse, andNOT (NOT p)and-(-x)becomepandxwhenpis boolean andxan integer;p AND TRUE,TRUE AND p,p OR FALSEandFALSE OR pbecomepwhenpis boolean, whilep AND FALSE,FALSE AND p,p OR TRUEandTRUE OR pbecome the constant only whenpis a safe predicate, since an erroringpmust still make the whole expression fail;x + 0,0 + x,x - 0,x * 1and1 * xbecomexwhenxis an integer;x * 0is not rewritten, since it isNULLwhenxis;e IN ()becomesFALSEwheneis safe, ande IN (v)becomese = v;CASE WHEN c THEN a ELSE b ENDwith a literal condition becomesaforTRUEandbforFALSEandNULL.
A NULL produced by folding gets the type of the node it replaces, or BOOLEAN when that node is
ill-typed. Folding and CASE elimination can therefore turn an ill-typed expression into a
well-typed one: NULL::text + 0 becomes a boolean NULL, and
CASE WHEN TRUE THEN 1 ELSE 'a' END becomes 1. The certificate covers such inputs too, since it
compares evaluations on conforming rows, but typing does not transfer back from the result: check
Expr.typeOf, Query.typeOf or Query.IsSafe on the input when its typing matters. (A result
that passes Query.IsSafe still shows, through the certificate, that the input succeeds on every
conforming instance.)
Queries #
Query.simplify also works bottom up, in one pass. At each node it simplifies the expressions of
the node in the typing context of the node's input, when that input is well-typed, and then:
WHERE pover anotherWHERE cmerges intoWHERE c AND pwhenp, after simplification, is safe (Query.select_select); over a projection it moves below it with the projection substituted when the projection is safe (Query.select_project); over a join it moves into the left or right input when, after simplification, it mentions only that input's columns and both it and the join predicate are safe (Query.select_join_left,Query.select_join_right, with the decidableExpr.lowerLeftandExpr.lowerRightrecognizing such a predicate); over a cross join that it cannot enter it becomes the join predicate (Query.select_join_true); overUNION ALLit is copied into both inputs (Query.select_unionAll), and it moves belowDISTINCT(Query.select_distinct). A moved predicate keeps moving down in the same way, and where it stops it is simplified in the context it reached;WHERE TRUEis dropped (Query.select_true). This isQuery.pushSelect.- A projection over a projection fuses by substitution when the inner one is safe
(
Query.project_project), and a projection that becomesSELECT *is dropped (Query.project_col). This isQuery.fuseProject. The simplifiedSELECTlist is built byFinVec.map, so the simplification of a literal![…]list is again a literal list, which callers can compare byrfl. - A join predicate is simplified in the context of the joined rows (
Query.simpJoin).
Each of these recursions follows the structure of the query, so the simplifier terminates by structural recursion. Merged conjunctions are not split again, so a conjunction that mentions both inputs of a join stays above it even if one of its conjuncts could move.
It adapts and redesigns the certified rewriter of sql_equiv (revision 38030a6b) by Evelyn
Mitchell, which returned each rewritten expression with its equivalence proof and guarded
conditional rules by syntactic type checks. Here the guards are the typing and safety checks of a
typing context, the guarantee covers every conforming row and every conforming instance under
strict errors (so the absorbing rules p AND FALSE and p OR TRUE need a safe p), and the
simplifier also rewrites queries.
Main definitions #
Cslib.SQL.Expr.simplify: the expression simplifier.Cslib.SQL.Expr.simpUnary,Cslib.SQL.Expr.simpBinary,Cslib.SQL.Expr.simpInList,Cslib.SQL.Expr.simpCaseWhen: one rewrite at the root.Cslib.SQL.Query.simplify: the query simplifier.Cslib.SQL.Query.pushSelect,Cslib.SQL.Query.fuseProject: movingWHEREdown and fusing projections.Cslib.SQL.Query.simpSelect,Cslib.SQL.Query.simpProject,Cslib.SQL.Query.simpJoin: simplifying the expressions of a node in place.
Main results #
Cslib.SQL.Expr.equiv_simplify: an expression is equivalent to its simplification.Cslib.SQL.Query.equiv_simplify: a query is ordered-equivalent to its simplification.
Expressions #
op e rewritten at the root, for an operand e that is already simplified: folded when e
is a literal and the operator succeeds on it, otherwise rewritten by the laws for NOT over a
comparison, a null test or a negation, and for double negation.
Instances For
e₁ op e₂ rewritten at the root, for operands that are already simplified: folded when both
are literals and the operator succeeds on them, otherwise rewritten by the identity and absorption
laws of AND and OR and the identity laws of +, - and *, each under its guard.
Instances For
e IN vs rewritten at the root, for an operand e that is already simplified: folded when
e is a literal and the membership succeeds, otherwise e IN () is FALSE when e is safe and
e IN (v) is e = v.
Instances For
CASE WHEN c THEN a ELSE b END with a literal condition is the branch it takes.
Instances For
The expression simplifier: simplify the operands of each node, then rewrite the node once.
Expr.equiv_simplify certifies it on the rows conforming to Γ.
Instances For
Queries #
SELECT es FROM q, simplified in place: the expressions are simplified in the typing context
of q when q is well-typed, and the projection is dropped when it becomes SELECT *. The
simplified list is built by FinVec.map, so that of a literal ![…] list is a literal list. Over
an ill-typed input, the list is returned as written.
Instances For
WHERE p over q, moved as far down as the guarded laws allow: simplified and merged into a
WHERE below it when it simplifies to a safe predicate, moved below a safe projection with the
projection substituted, into the input of a join that it reads when it and the join predicate are
safe, into both inputs of a UNION ALL, or below a DISTINCT, and made the predicate of a cross
join that it cannot enter. Where it stops, it is simplified in place (Query.simpSelect).
Instances For
SELECT es FROM q with projections fused: over a safe projection, the two fuse by
substitution, and the result fuses further down. Where it stops, it is simplified in place
(Query.simpProject).
Instances For
The query simplifier: simplify the inputs of each node, then move its WHERE down, fuse its
projections or simplify its join predicate. Query.equiv_simplify certifies it on every
conforming instance.
Instances For
The query simplifier is sound: on every conforming instance, a query and its simplification both fail or both succeed with the same list of rows.