Documentation

CslibFrontier.Languages.SQL.Expr

Resolved SQL expressions #

Expr n is a scalar SQL expression over a row of n columns: literals, typed NULL, column references by position (Expr.col i with i : Fin n, so a reference to a missing column cannot be written), the operators of Cslib.SQL.Scalar, e IN (v₁, …, vₖ) over a literal list, and CASE WHEN c THEN a ELSE b END.

Expr.eval r e evaluates e on the row r. It is strict and goes from left to right: every operand is evaluated, and the first error is the result. The one exception is CASE, which evaluates only the branch it takes, so an error in the other branch is not raised. A WHERE or ON predicate is read through Expr.evalTruth, which also rejects a non-boolean result.

Moving an expression across a scope boundary is explicit. Expr.rename ρ reindexes the columns, and Expr.subst σ replaces column i by an expression σ i: evaluating a substituted expression is evaluating the original on the row of results of σ, provided every σ i succeeds (Expr.eval_subst). For joins, whose rows are Fin.append, Expr.eval_rename_castAdd and Expr.eval_rename_natAdd evaluate an expression moved to the left or right side. Conversely, Expr.lowerLeft and Expr.lowerRight decide whether an expression over a joined row reads only one side, and return it as an expression over that side.

It adapts and redesigns the expressions of sql_equiv (revision 38030a6b) by Evelyn Mitchell. There, column references were names looked up in rows at evaluation time, NULL literals carried optional types, and the expression type held aggregates, subqueries and windows evaluated by partial functions. Here references are resolved positions, the fragment is scalar, and evaluation is structural and total.

Main definitions #

Main results #

inductive Cslib.SQL.Expr (n : ℕ) :

A resolved scalar SQL expression over rows of n columns.

  • lit {n : ℕ} (v : Value) : Expr n

    A non-null literal.

  • null {n : ℕ} (τ : SqlType) : Expr n

    A NULL literal of type τ; the type matters only for typing.

  • col {n : ℕ} (i : Fin n) : Expr n

    The i-th column of the row.

  • unary {n : ℕ} (op : UnOp) (e : Expr n) : Expr n

    A unary operator.

  • binary {n : ℕ} (op : BinOp) (e₁ e₂ : Expr n) : Expr n

    A binary operator.

  • inList {n : ℕ} (e : Expr n) (vs : List Datum) : Expr n

    e IN (v₁, …, vₖ) over a list of literals; NULL may occur in the list.

  • caseWhen {n : ℕ} (c a b : Expr n) : Expr n

    CASE WHEN c THEN a ELSE b END.

Instances For
    def Cslib.SQL.instDecidableEqExpr.decEq {n✝ : ℕ} (x✝ x✝¹ : Expr n✝) :
    Decidable (x✝ = x✝¹)
    Instances For
      @[instance_reducible]
      @[instance_reducible]
      instance Cslib.SQL.instReprExpr {n✝ : ℕ} :
      Repr (Expr n✝)
      Instances For

        Evaluate an expression on a row: strict and left to right, except that CASE evaluates only the branch it takes.

        Instances For

          Evaluate a predicate on a row, as WHERE and ON do: a non-boolean result is a type error, and the row is kept exactly when the result is Truth.true.

          Instances For
            @[simp]
            theorem Cslib.SQL.Expr.eval_lit {n : ℕ} (r : Row n) (v : Value) :
            eval r (lit v) = Except.ok (some v)
            @[simp]
            theorem Cslib.SQL.Expr.eval_null {n : ℕ} (r : Row n) (τ : SqlType) :
            @[simp]
            theorem Cslib.SQL.Expr.eval_col {n : ℕ} (r : Row n) (i : Fin n) :
            eval r (col i) = Except.ok (r i)
            theorem Cslib.SQL.Expr.eval_unary {n : ℕ} (r : Row n) (op : UnOp) (e : Expr n) :
            eval r (unary op e) = do let __do_lift ← eval r e evalUnOp op __do_lift
            theorem Cslib.SQL.Expr.eval_binary {n : ℕ} (r : Row n) (op : BinOp) (e₁ e₂ : Expr n) :
            eval r (binary op e₁ e₂) = do let __do_lift ← eval r e₁ let __do_lift_1 ← eval r e₂ evalBinOp op __do_lift __do_lift_1
            theorem Cslib.SQL.Expr.eval_inList {n : ℕ} (r : Row n) (e : Expr n) (vs : List Datum) :
            eval r (e.inList vs) = do let __do_lift ← eval r e let __do_lift ← __do_lift.inList vs pure __do_lift.toDatum
            theorem Cslib.SQL.Expr.eval_caseWhen {n : ℕ} (r : Row n) (c a b : Expr n) :
            eval r (c.caseWhen a b) = do let __do_lift ← eval r c let __do_lift ← __do_lift.toTruth if __do_lift.isTrue = true then eval r a else eval r b
            theorem Cslib.SQL.Expr.eval_caseWhen_of_true {n : ℕ} (r : Row n) {c : Expr n} (hc : evalTruth r c = Except.ok Truth.true) (a b : Expr n) :
            eval r (c.caseWhen a b) = eval r a

            CASE takes the THEN branch when the condition is TRUE, without evaluating ELSE.

            theorem Cslib.SQL.Expr.eval_caseWhen_of_ne_true {n : ℕ} (r : Row n) {c : Expr n} {t : Truth} (hc : evalTruth r c = Except.ok t) (ht : t ≠ Truth.true) (a b : Expr n) :
            eval r (c.caseWhen a b) = eval r b

            CASE takes the ELSE branch when the condition is FALSE or unknown, without evaluating THEN.

            Literals #

            The datum of a literal: lit v is v and null τ is NULL. Other expressions are not literals.

            Instances For
              def Cslib.SQL.Expr.ofDatum {n : ℕ} (τ : SqlType) :
              Datum → Expr n

              The literal of a datum, with NULL typed τ.

              Instances For
                theorem Cslib.SQL.Expr.eval_of_asLiteral_eq_some {n : ℕ} {e : Expr n} {d : Datum} (h : e.asLiteral = some d) (r : Row n) :

                A literal evaluates to its datum on every row.

                @[simp]
                theorem Cslib.SQL.Expr.eval_ofDatum {n : ℕ} (τ : SqlType) (d : Datum) (r : Row n) :
                eval r (ofDatum τ d) = Except.ok d
                @[simp]

                Renaming and substitution #

                def Cslib.SQL.Expr.rename {n m : ℕ} (ρ : Fin n → Fin m) :
                Expr n → Expr m

                Reindex the columns of an expression along ρ.

                Instances For
                  def Cslib.SQL.Expr.subst {n m : ℕ} (σ : Fin n → Expr m) :
                  Expr n → Expr m

                  Replace every column i of an expression by the expression σ i.

                  Instances For
                    theorem Cslib.SQL.Expr.rename_eq_subst {n m : ℕ} (ρ : Fin n → Fin m) (e : Expr n) :
                    rename ρ e = subst (col ∘ ρ) e

                    Renaming is substitution by column references.

                    @[simp]
                    theorem Cslib.SQL.Expr.rename_id {n : ℕ} (e : Expr n) :
                    rename id e = e
                    theorem Cslib.SQL.Expr.rename_rename {n m k : ℕ} (ρ : Fin n → Fin m) (ρ' : Fin m → Fin k) (e : Expr n) :
                    rename ρ' (rename ρ e) = rename (ρ' ∘ ρ) e
                    @[simp]
                    theorem Cslib.SQL.Expr.subst_col {n : ℕ} (e : Expr n) :
                    subst col e = e
                    theorem Cslib.SQL.Expr.subst_subst {n m k : ℕ} (σ : Fin n → Expr m) (σ' : Fin m → Expr k) (e : Expr n) :
                    subst σ' (subst σ e) = subst (fun (i : Fin n) => subst σ' (σ i)) e
                    theorem Cslib.SQL.Expr.eval_rename {n m : ℕ} (ρ : Fin n → Fin m) (r : Row m) (e : Expr n) :
                    eval r (rename ρ e) = eval (r ∘ ρ) e

                    Evaluating a renamed expression is evaluating the original on the reindexed row.

                    theorem Cslib.SQL.Expr.eval_subst {n m : ℕ} {σ : Fin n → Expr m} {r : Row m} {d : Row n} (h : ∀ (i : Fin n), eval r (σ i) = Except.ok (d i)) (e : Expr n) :
                    eval r (subst σ e) = eval d e

                    Evaluating a substituted expression is evaluating the original on the row d of the substituted results, when every σ i succeeds with d i.

                    theorem Cslib.SQL.Expr.eval_rename_castAdd {n m : ℕ} (e : Expr n) (r : Row n) (s : Row m) :

                    An expression over the left input of a join reads the left part of the joined row.

                    theorem Cslib.SQL.Expr.eval_rename_natAdd {n m : ℕ} (e : Expr m) (r : Row n) (s : Row m) :

                    An expression over the right input of a join reads the right part of the joined row.

                    theorem Cslib.SQL.Expr.toOption_eval_subst_congr {n m : ℕ} {σ σ' : Fin n → Expr m} {r : Row m} (h : ∀ (i : Fin n), (eval r (σ i)).toOption = (eval r (σ' i)).toOption) (e : Expr n) :
                    (eval r (subst σ e)).toOption = (eval r (subst σ' e)).toOption

                    Evaluation depends on the substituted expressions only through their observations: if σ i and σ' i succeed together with the same result, so do e.subst σ and e.subst σ'.

                    Moving an expression into a join input #

                    def Cslib.SQL.Expr.lower {n m : ℕ} (f : Fin n → Option (Fin m)) :
                    Expr n → Option (Expr m)

                    Rename the columns of an expression along the partial map f, failing when the expression mentions a column outside the domain of f. When f inverts a renaming ρ, a result e' is an expression with e'.rename ρ = e (Expr.rename_of_lower_eq_some).

                    Instances For
                      theorem Cslib.SQL.Expr.rename_of_lower_eq_some {n m : ℕ} {f : Fin n → Option (Fin m)} {ρ : Fin m → Fin n} (hf : ∀ (i : Fin n) (j : Fin m), f i = some j → ρ j = i) {e : Expr n} {e' : Expr m} (h : lower f e = some e') :
                      rename ρ e' = e

                      Lowering along a partial inverse of ρ undoes the renaming by ρ.

                      def Cslib.SQL.Expr.lowerLeft {n m : ℕ} (e : Expr (n + m)) :

                      The expression over the left input of a join that e reads, if e mentions only left columns; then e is its renaming by Fin.castAdd m (Expr.rename_castAdd_of_lowerLeft_eq_some).

                      Instances For
                        def Cslib.SQL.Expr.lowerRight {n m : ℕ} (e : Expr (n + m)) :

                        The expression over the right input of a join that e reads, if e mentions only right columns; then e is its renaming by Fin.natAdd n (Expr.rename_natAdd_of_lowerRight_eq_some).

                        Instances For
                          theorem Cslib.SQL.Expr.rename_castAdd_of_lowerLeft_eq_some {n m : ℕ} {e : Expr (n + m)} {e' : Expr n} (h : e.lowerLeft = some e') :
                          theorem Cslib.SQL.Expr.rename_natAdd_of_lowerRight_eq_some {n m : ℕ} {e : Expr (n + m)} {e' : Expr m} (h : e.lowerRight = some e') :