SQL scalar operators #
This module gives the meaning of SQL's operators on datums. An operator either returns a datum,
possibly NULL, or fails with an EvalError: typeMismatch when a non-null operand has the wrong
type, and divisionByZero. The rules are:
- A non-null operand of the wrong type is a type error, even when the other operand is
NULL. Otherwise aNULLoperand givesNULL, soNULL / 0isNULLwhile1 / 0fails. - Integers are unbounded.
/truncates toward zero (Int.tdiv) and%takes the sign of the dividend (Int.tmod). = <> < <= > >=compare two values of the same type: integers numerically, text by code points (binary collation), and booleans withfalse < true. Their result is a truth value, as a datum:TRUE,FALSE, orNULLforunknown.AND,ORandNOTare Kleene's connectives⊓,⊔andTruth.noton the operands' truth values (Datum.toTruth): a boolean gives its value,NULLgivesunknown.IS NULLandIS NOT NULLaccept any operand and are neverNULL.e IN (v₁, …, vₖ)(Datum.inList) is the Kleene disjunction ofe = vᵢ, starting fromFALSE.
Every primitive operation (addInt, divInt, compareText, concatText, …) is a named
function, so that bounded integers or other collations can replace one later without touching the
operator rules.
All operators are strict: both operands of a binary operator are evaluated, and the left operand's
error is reported first. Since every type error is typeMismatch, the laws a rewriter needs hold
as equations of results: NOT (a < b) is a >= b (evalBinOp_cmp_negate), a < b is b > a
(evalBinOp_cmp_swap), AND and OR commute and satisfy De Morgan's laws, and arithmetic
identities hold on integer-typed datums. Some familiar identities fail because of NULL:
x * 0 is NULL when x is.
Three-valued membership is where NULL matters most. Over datums of a common type, e IN vs is
FALSE for an empty list (even for a NULL probe), TRUE when e is non-null and occurs in
vs, FALSE when moreover nothing is NULL, and unknown otherwise (inList_eq_ok). So
NOT IN agrees with NOT EXISTS (… WHERE v = e) as a filter exactly when vs is empty, some
element matches, or the probe is non-null and the list has no NULL
(isTrue_not_inList_iff_iff). Subqueries, a later development, will reuse this membership
function.
It adapts and redesigns the operator evaluators of sql_equiv (revision 38030a6b) by Evelyn
Mitchell. There, an untyped NULL result, an evaluation error and a type error shared
representations, IN lost unknown, and comparisons promoted between types; here errors are a
separate type, membership is three-valued, and comparison requires one type.
Main definitions #
Cslib.SQL.EvalError: why an evaluation fails.Cslib.SQL.Value.cmp: comparison of two values of the same type.Cslib.SQL.Datum.toTruth,Cslib.SQL.Truth.toDatum: truth values as datums and back.Cslib.SQL.UnOp,Cslib.SQL.ArithOp,Cslib.SQL.CmpOp,Cslib.SQL.BinOp: the operators.Cslib.SQL.CmpOp.eval: three-valued comparison of datums.Cslib.SQL.evalUnOp,Cslib.SQL.evalBinOp: operators on datums.Cslib.SQL.Datum.inList: three-valuede IN (v₁, …, vₖ).
Main results #
Cslib.SQL.CmpOp.eval_eq_ok_true_iff:a = bisTRUEexactly when both are the same value.Cslib.SQL.evalBinOp_cmp_negate,Cslib.SQL.evalBinOp_cmp_swap: negating and flipping a comparison.Cslib.SQL.evalBinOp_and_comm,Cslib.SQL.evalBinOp_or_comm,Cslib.SQL.not_evalBinOp_and,Cslib.SQL.not_evalBinOp_or: commutativity and De Morgan's laws on datums.Cslib.SQL.evalBinOp_add_comm,Cslib.SQL.evalBinOp_add_zero,Cslib.SQL.evalBinOp_arith_null_left: arithmetic identities andNULLpropagation.Cslib.SQL.evalBinOp_and_true,Cslib.SQL.evalBinOp_and_false,Cslib.SQL.evalBinOp_or_false,Cslib.SQL.evalBinOp_or_true:TRUEandFALSEas operands ofANDandOR, on boolean datums.Cslib.SQL.Datum.inList_eq_ok: the closed form of three-valued membership.Cslib.SQL.Datum.isTrue_not_inList_iff_iff: whenNOT INagrees withNOT EXISTS.
Primitive operations #
Integer division, truncating toward zero; a zero divisor is divisionByZero.
Instances For
Integer remainder, with the sign of the dividend; a zero divisor is divisionByZero.
Instances For
Text comparison by code points (binary collation), lexicographically.
Instances For
Boolean comparison, with false < true.
Instances For
Comparing in the other order swaps the outcome.
Two values compare equal exactly when they are equal.
A value compares equal to itself.
Comparison fails only with a type error.
Truth values as datums #
A truth value as a datum: TRUE, FALSE, or NULL for unknown.
Instances For
A datum has a truth value exactly when it is boolean-typed.
A boolean-typed datum is a truth value: TRUE, FALSE, or NULL for unknown.
Operators #
Whether the operator fails on a zero divisor: / and %.
Instances For
An integer operator fails only by dividing by zero.
Whether an outcome of compare satisfies the comparison.
Instances For
The comparison that holds exactly when this one fails: NOT (a < b) is a >= b.
Instances For
The comparison with its operands exchanged: a < b is b > a.
Instances For
A comparison fails only with a type error.
Laws of the operators #
De Morgan: NOT (a AND b) is NOT a OR NOT b, also when an operand is not boolean.
De Morgan: NOT (a OR b) is NOT a AND NOT b, also when an operand is not boolean.
NOT (d IS NULL) is d IS NOT NULL.
NOT (d IS NOT NULL) is d IS NULL.
a AND TRUE is a on boolean datums; on any other value it is a type error.
a AND FALSE is FALSE on boolean datums, also when a is NULL.
a OR FALSE is a on boolean datums.
a OR TRUE is TRUE on boolean datums, also when a is NULL.
NULL op a is NULL for an integer operator and an integer-typed a.
a op NULL is NULL for an integer operator and an integer-typed a; in particular
NULL / 0 and 1 / NULL are NULL.
a + b is b + a, also when an operand is not an integer.
a * b is b * a, also when an operand is not an integer.
a + 0 is a on integer-typed datums; on a text or boolean a it is a type error.
a - 0 is a on integer-typed datums.
a * 1 is a on integer-typed datums.
Three-valued membership #
Three-valued e IN (v₁, …, vₖ): the Kleene disjunction of the comparisons e = vᵢ, starting
from FALSE. It is strict: the comparisons are made from left to right and the first failure is
the result. e NOT IN (v₁, …, vₖ) is its negation Truth.not.
Instances For
e IN () is FALSE, even for a NULL probe.
The closed form of three-valued membership over a common type: FALSE for an empty list (even
for a NULL probe), TRUE when the non-null probe occurs, FALSE when moreover nothing is NULL,
and unknown otherwise.
NOT IN and NOT EXISTS. Read as filters, e NOT IN vs keeps a row when the negated
membership is TRUE, and NOT EXISTS (SELECT … FROM vs WHERE v = e) keeps it when no comparison
v = e is TRUE. Over a common type they agree exactly when vs is empty, some element matches,
or e is non-null and vs has no NULL. Without a common type both fail under strict
evaluation, since a comparison raises a type error.