Documentation

CslibFrontier.Languages.SQL.Simplify

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):

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:

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 #

Main results #

Expressions #

def Cslib.SQL.Expr.simpUnary {n : ℕ} (Γ : Fin n → SqlType) (op : UnOp) (e : Expr n) :

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
    def Cslib.SQL.Expr.simpBinary {n : ℕ} (Γ : Fin n → SqlType) (op : BinOp) (e₁ e₂ : Expr n) :

    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
      def Cslib.SQL.Expr.simpInList {n : ℕ} (Γ : Fin n → SqlType) (e : Expr n) (vs : List Datum) :

      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
        def Cslib.SQL.Expr.simpCaseWhen {n : ℕ} (c a b : Expr n) :

        CASE WHEN c THEN a ELSE b END with a literal condition is the branch it takes.

        Instances For
          def Cslib.SQL.Expr.simplify {n : ℕ} (Γ : Fin n → SqlType) :
          Expr n → Expr n

          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
            theorem Cslib.SQL.Expr.equiv_simpUnary {n : ℕ} (Γ : Fin n → SqlType) (op : UnOp) (e : Expr n) :
            Equiv Γ (unary op e) (simpUnary Γ op e)
            theorem Cslib.SQL.Expr.equiv_simpBinary {n : ℕ} (Γ : Fin n → SqlType) (op : BinOp) (e₁ e₂ : Expr n) :
            Equiv Γ (binary op e₁ e₂) (simpBinary Γ op e₁ e₂)
            theorem Cslib.SQL.Expr.equiv_simpInList {n : ℕ} (Γ : Fin n → SqlType) (e : Expr n) (vs : List Datum) :
            Equiv Γ (e.inList vs) (simpInList Γ e vs)
            theorem Cslib.SQL.Expr.equiv_simpCaseWhen {n : ℕ} {Γ : Fin n → SqlType} (c a b : Expr n) :
            Equiv Γ (c.caseWhen a b) (c.simpCaseWhen a b)
            theorem Cslib.SQL.Expr.equiv_simplify {n : ℕ} (Γ : Fin n → SqlType) (e : Expr n) :
            Equiv Γ e (simplify Γ e)

            The expression simplifier is sound: on every row conforming to Γ, an expression and its simplification both fail or both succeed with the same datum.

            Queries #

            def Cslib.SQL.Query.simpSelect {n : ℕ} {S : DbSchema} (p : Expr n) (q : Query S n) :
            Query S n

            WHERE p over q, simplified in place: p is simplified in the typing context of q when q is well-typed, and dropped when it becomes TRUE.

            Instances For
              def Cslib.SQL.Query.simpProject {n k : ℕ} {S : DbSchema} (es : Fin k → Expr n) (q : Query S n) :
              Query S k

              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
                def Cslib.SQL.Query.simpJoin {n m : ℕ} {S : DbSchema} (θ : Expr (n + m)) (q₁ : Query S n) (q₂ : Query S m) :
                Query S (n + m)

                q₁ JOIN q₂ ON θ with θ simplified in the typing context of the joined rows, when both inputs are well-typed.

                Instances For
                  def Cslib.SQL.Query.pushSelect {S : DbSchema} {n : ℕ} :
                  Expr n → Query S n → Query S n

                  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
                    def Cslib.SQL.Query.fuseProject {S : DbSchema} {n k : ℕ} :
                    (Fin k → Expr n) → Query S n → Query S k

                    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
                      def Cslib.SQL.Query.simplify {S : DbSchema} {n : ℕ} :
                      Query S n → Query S n

                      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
                        theorem Cslib.SQL.Query.equiv_simpProject {n k : ℕ} {S : DbSchema} (es : Fin k → Expr n) (q : Query S n) :
                        theorem Cslib.SQL.Query.equiv_simpJoin {n m : ℕ} {S : DbSchema} (θ : Expr (n + m)) (q₁ : Query S n) (q₂ : Query S m) :
                        Equiv EquivLevel.ordered (join θ q₁ q₂) (simpJoin θ q₁ q₂)
                        theorem Cslib.SQL.Query.equiv_fuseProject {n k : ℕ} {S : DbSchema} (es : Fin k → Expr n) (q : Query S n) :

                        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.