Documentation

CslibFrontier.Languages.SQL.Typing

Typing and safety of SQL expressions #

UnOp.typeOf and BinOp.typeOf give the result type of an operator on operands of given types, and Expr.typeOf Γ e the type of an expression over a typing context Γ : Fin n → SqlType: a literal has its value's type, NULL its declared type, col i the type Γ i, e IN vs is boolean when every list element has e's type, and CASE needs a boolean condition and branches of one type.

Type soundness (Expr.eval_ok_or_divisionByZero): on a row conforming to Γ, a well-typed expression of type τ never raises typeMismatch; it either returns NULL or a value of type τ, or divides by zero. Division by zero is excluded syntactically by Expr.divSafe: / and % only by a nonzero integer literal. An expression is safe (Expr.IsSafe, decidable) when it is well-typed and divSafe, and a safe expression succeeds on every conforming row (Expr.IsSafe.exists_eval_eq_ok). A safe predicate (Expr.IsSafePredicate, also boolean-typed) never makes a WHERE or ON clause fail. Laws that move, duplicate or drop the evaluation of an expression use safety as their side condition. A divisor that is a column or a computed expression is not safe even when it is never zero on the data: safety is a syntactic check.

Typing and safety are preserved by renaming and by substitution of safe expressions of the right types (Expr.typeOf_rename, Expr.typeOf_subst, Expr.IsSafe.rename, Expr.IsSafePredicate.rename, Expr.IsSafe.subst).

It adapts and redesigns the typing of sql_equiv (revision 38030a6b) by Evelyn Mitchell, whose type checks came from the data at evaluation time; here typing is static, relative to a typing context.

Main definitions #

Main results #

Operators #

The result type of a unary operator on an operand of type τ, if the application is well-typed.

Instances For

    The result type of a binary operator on operands of types τ₁ and τ₂, if the application is well-typed: comparisons need equal types.

    Instances For

      Whether the operator fails on a zero divisor: / and %.

      Instances For
        theorem Cslib.SQL.evalUnOp_ok_of_typeOf {op : UnOp} {τ τ' : SqlType} (h : op.typeOf τ = some τ') {d : Datum} (hd : d.HasType τ) :
        ∃ (d' : Datum), evalUnOp op d = Except.ok d' ∧ d'.HasType τ'

        A well-typed unary operator succeeds, with a result of its type.

        theorem Cslib.SQL.evalBinOp_ok_or_divisionByZero {op : BinOp} {τ₁ τ₂ τ : SqlType} (h : op.typeOf τ₁ τ₂ = some τ) {a b : Datum} (ha : a.HasType τ₁) (hb : b.HasType τ₂) :

        A well-typed binary operator succeeds with a result of its type, unless it divides by zero, in which case the divisor is the integer 0.

        Expressions #

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

        The type of an expression over the typing context Γ, if it is well-typed.

        Instances For

          The expression is an integer literal other than 0.

          Instances For

            Every division and remainder in the expression is by a nonzero integer literal.

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

              The expression is well-typed in Γ and every division is by a nonzero literal; then it succeeds on every row conforming to Γ (Expr.IsSafe.exists_eval_eq_ok).

              Instances For
                @[instance_reducible]
                instance Cslib.SQL.Expr.instDecidableIsSafe {n : ℕ} (Γ : Fin n → SqlType) (e : Expr n) :
                def Cslib.SQL.Expr.IsSafePredicate {n : ℕ} (Γ : Fin n → SqlType) (e : Expr n) :

                The expression is a safe boolean predicate in Γ; then a WHERE or ON clause using it never fails on a row conforming to Γ (Expr.IsSafePredicate.exists_evalTruth_eq_ok).

                Instances For
                  @[instance_reducible]
                  theorem Cslib.SQL.Expr.eval_spec {n : ℕ} {Γ : Fin n → SqlType} {r : Row n} (hr : Row.Conforms Γ r) {e : Expr n} {τ : SqlType} (he : typeOf Γ e = some τ) :
                  Except.Spec (fun (x : Datum) => x.HasType τ) (fun (err : EvalError) => err = EvalError.divisionByZero ∧ e.divSafe = false) (eval r e)

                  Type soundness as a postcondition: on a row conforming to Γ, an expression of type τ returns NULL or a value of type τ, or divides by zero, which requires a division that is not divSafe.

                  theorem Cslib.SQL.Expr.eval_ok_or_divisionByZero {n : ℕ} {Γ : Fin n → SqlType} {r : Row n} (hr : Row.Conforms Γ r) {e : Expr n} {τ : SqlType} (he : typeOf Γ e = some τ) :

                  Type soundness: on a row conforming to Γ, an expression of type τ returns NULL or a value of type τ, or divides by zero, which requires a division that is not divSafe.

                  theorem Cslib.SQL.Expr.eval_ne_typeMismatch {n : ℕ} {Γ : Fin n → SqlType} {r : Row n} (hr : Row.Conforms Γ r) {e : Expr n} {τ : SqlType} (he : typeOf Γ e = some τ) :

                  A well-typed expression never raises a type error on a conforming row.

                  theorem Cslib.SQL.Expr.hasType_of_eval_eq_ok {n : ℕ} {Γ : Fin n → SqlType} {r : Row n} (hr : Row.Conforms Γ r) {e : Expr n} {τ : SqlType} (he : typeOf Γ e = some τ) {d : Datum} (h : eval r e = Except.ok d) :
                  d.HasType τ

                  The result of a well-typed expression on a conforming row has the expression's type.

                  theorem Cslib.SQL.Expr.exists_eval_eq_ok_of_divSafe {n : ℕ} {Γ : Fin n → SqlType} {r : Row n} (hr : Row.Conforms Γ r) {e : Expr n} {τ : SqlType} (he : typeOf Γ e = some τ) (hs : e.divSafe = true) :
                  ∃ (d : Datum), eval r e = Except.ok d ∧ d.HasType τ

                  A well-typed divSafe expression succeeds on a conforming row, with a result of its type.

                  theorem Cslib.SQL.Expr.IsSafe.exists_eval_eq_ok {n : ℕ} {Γ : Fin n → SqlType} {e : Expr n} (hs : IsSafe Γ e) {r : Row n} (hr : Row.Conforms Γ r) :
                  ∃ (d : Datum), eval r e = Except.ok d

                  A safe expression succeeds on every conforming row.

                  theorem Cslib.SQL.Expr.IsSafePredicate.isSafe {n : ℕ} {Γ : Fin n → SqlType} {e : Expr n} (h : IsSafePredicate Γ e) :
                  IsSafe Γ e
                  theorem Cslib.SQL.Expr.IsSafePredicate.exists_evalTruth_eq_ok {n : ℕ} {Γ : Fin n → SqlType} {e : Expr n} (h : IsSafePredicate Γ e) {r : Row n} (hr : Row.Conforms Γ r) :
                  ∃ (t : Truth), evalTruth r e = Except.ok t

                  A safe predicate never fails on a conforming row, so a WHERE or ON clause using it is total.

                  Renaming and substitution #

                  theorem Cslib.SQL.Expr.typeOf_rename {n m : ℕ} (ρ : Fin n → Fin m) (Γ : Fin m → SqlType) (e : Expr n) :
                  typeOf Γ (rename ρ e) = typeOf (Γ ∘ ρ) e
                  theorem Cslib.SQL.Expr.typeOf_subst {n m : ℕ} {σ : Fin n → Expr m} {Γ : Fin n → SqlType} {Δ : Fin m → SqlType} (hσ : ∀ (i : Fin n), typeOf Δ (σ i) = some (Γ i)) (e : Expr n) :
                  typeOf Δ (subst σ e) = typeOf Γ e

                  Substituting expressions of the column types preserves the type.

                  @[simp]
                  theorem Cslib.SQL.Expr.divSafe_rename {n m : ℕ} (ρ : Fin n → Fin m) (e : Expr n) :
                  theorem Cslib.SQL.Expr.divSafe_subst {n m : ℕ} {σ : Fin n → Expr m} (hσ : ∀ (i : Fin n), (σ i).divSafe = true) {e : Expr n} (he : e.divSafe = true) :

                  Substituting divSafe expressions into a divSafe expression keeps it divSafe.

                  theorem Cslib.SQL.Expr.IsSafe.rename {n m : ℕ} {Γ : Fin m → SqlType} {e : Expr n} (ρ : Fin n → Fin m) (h : IsSafe (Γ ∘ ρ) e) :
                  theorem Cslib.SQL.Expr.IsSafePredicate.rename {n m : ℕ} {Γ : Fin m → SqlType} {e : Expr n} (ρ : Fin n → Fin m) (h : IsSafePredicate (Γ ∘ ρ) e) :
                  theorem Cslib.SQL.Expr.IsSafe.subst {n m : ℕ} {σ : Fin n → Expr m} {Γ : Fin n → SqlType} {Δ : Fin m → SqlType} (hσ : ∀ (i : Fin n), typeOf Δ (σ i) = some (Γ i)) (hσs : ∀ (i : Fin n), (σ i).divSafe = true) {e : Expr n} (h : IsSafe Γ e) :
                  IsSafe Δ (Expr.subst σ e)

                  Substituting safe expressions of the column types into a safe expression keeps it safe.