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 #
Cslib.SQL.UnOp.typeOf,Cslib.SQL.BinOp.typeOf: result types of the operators.Cslib.SQL.Expr.typeOf: the type of an expression in a typing context.Cslib.SQL.Expr.divSafe: every division or remainder is by a nonzero integer literal.Cslib.SQL.Expr.IsSafe,Cslib.SQL.Expr.IsSafePredicate: the decidable safety checks.
Main results #
Cslib.SQL.evalUnOp_ok_of_typeOf,Cslib.SQL.evalBinOp_ok_or_divisionByZero: operator soundness.Cslib.SQL.Expr.eval_ok_or_divisionByZero,Cslib.SQL.Expr.eval_spec: type soundness of expressions, also as a postcondition that composes along evaluation.Cslib.SQL.Expr.eval_ne_typeMismatch: a well-typed expression never raises a type error.Cslib.SQL.Expr.IsSafe.exists_eval_eq_ok: a safe expression succeeds on conforming rows.Cslib.SQL.Expr.IsSafePredicate.exists_evalTruth_eq_ok: a safe predicate never fails.
Operators #
Whether the operator fails on a zero divisor: / and %.
Instances For
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 #
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
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
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
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.
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.
The result of a well-typed expression on a conforming row has the expression's type.
A safe predicate never fails on a conforming row, so a WHERE or ON clause using it is
total.
Renaming and substitution #
Substituting safe expressions of the column types into a safe expression keeps it safe.