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 #
Cslib.SQL.Expr: resolved scalar expressions overncolumns.Cslib.SQL.Expr.eval: strict evaluation on a row;Cslib.SQL.Expr.evalTruthfor predicates.Cslib.SQL.Expr.rename,Cslib.SQL.Expr.subst: renaming and substitution of columns.Cslib.SQL.Expr.asLiteral,Cslib.SQL.Expr.ofDatum: literals and their datums.Cslib.SQL.Expr.lower,Cslib.SQL.Expr.lowerLeft,Cslib.SQL.Expr.lowerRight: renaming along a partial map, and moving an expression over a joined row to one input.
Main results #
Cslib.SQL.Expr.eval_rename: renaming is evaluation on the reindexed row.Cslib.SQL.Expr.eval_subst: substitution is evaluation on the row of substituted results.Cslib.SQL.Expr.eval_rename_castAdd,Cslib.SQL.Expr.eval_rename_natAdd: an expression moved into a join reads its own side of the joined row.Cslib.SQL.Expr.toOption_eval_subst_congr: substitutions with the same observations give expressions with the same observations.Cslib.SQL.Expr.rename_castAdd_of_lowerLeft_eq_some,Cslib.SQL.Expr.rename_natAdd_of_lowerRight_eq_some: a lowered expression renames back to the original.
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
NULLliteral 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;NULLmay occur in the list. - caseWhen {n : ℕ} (c a b : Expr n) : Expr n
CASE WHEN c THEN a ELSE b END.
Instances For
Literals #
Renaming and substitution #
An expression over the left input of a join reads the left part of the joined row.
An expression over the right input of a join reads the right part of the joined row.
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 #
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
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
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).