Documentation

CslibFrontier.Languages.SQL.Scalar

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:

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 #

Main results #

Why an evaluation fails.

  • typeMismatch : EvalError

    A non-null operand has the wrong type.

  • divisionByZero : EvalError

    An integer division or remainder by zero.

Instances For
    @[instance_reducible]

    Primitive operations #

    def Cslib.SQL.addInt (a b : ℤ) :

    Integer addition (unbounded).

    Instances For
      def Cslib.SQL.subInt (a b : ℤ) :

      Integer subtraction (unbounded).

      Instances For
        def Cslib.SQL.mulInt (a b : ℤ) :

        Integer multiplication (unbounded).

        Instances For

          Integer negation (unbounded).

          Instances For

            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 concatenation.

                Instances For

                  Integer comparison.

                  Instances For

                    Text comparison by code points (binary collation), lexicographically.

                    Instances For

                      Boolean comparison, with false < true.

                      Instances For

                        Compare two values of the same type; values of different types are a type error.

                        Instances For

                          Comparing in the other order swaps the outcome.

                          @[simp]

                          Two values compare equal exactly when they are equal.

                          @[simp]

                          A value compares equal to itself.

                          Two values are comparable exactly when they have the same type.

                          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

                            The truth value of an operand of AND, OR, NOT or CASE WHEN: a boolean gives its value, NULL gives unknown, and any other value is a type error.

                            Instances For

                              An operand of an integer operator: NULL or an integer, any other value is a type error.

                              Instances For

                                An operand of a text operator: NULL or a string, any other value is a type error.

                                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.

                                  A datum is an integer operand exactly when it is integer-typed.

                                  A datum is a text operand exactly when it is text-typed.

                                  An integer operand is a given integer exactly when the datum is that integer.

                                  Operators #

                                  inductive Cslib.SQL.UnOp :

                                  Unary operators.

                                  • not : UnOp

                                    NOT.

                                  • neg : UnOp

                                    Unary minus.

                                  • isNull : UnOp

                                    IS NULL.

                                  • isNotNull : UnOp

                                    IS NOT NULL.

                                  Instances For
                                    @[instance_reducible]

                                    Integer operators.

                                    Instances For
                                      @[instance_reducible]

                                      Comparison operators.

                                      Instances For
                                        @[instance_reducible]

                                        Binary operators.

                                        Instances For
                                          def Cslib.SQL.instDecidableEqBinOp.decEq (x✝ x✝¹ : BinOp) :
                                          Decidable (x✝ = x✝¹)
                                          Instances For
                                            @[instance_reducible]

                                            An integer operator on two integers.

                                            Instances For

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

                                              Instances For

                                                An integer operator fails only by dividing by zero.

                                                theorem Cslib.SQL.ArithOp.exists_eval_eq_ok {op : ArithOp} {a b : ℤ} (h : op.isDivision = true → b ≠ 0) :
                                                ∃ (z : ℤ), op.eval a b = Except.ok z

                                                An integer operator succeeds unless it divides 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
                                                      @[simp]
                                                      theorem Cslib.SQL.CmpOp.swap_swap (op : CmpOp) :
                                                      op.swap.swap = op
                                                      @[simp]
                                                      theorem Cslib.SQL.CmpOp.test_negate (op : CmpOp) (o : Ordering) :
                                                      op.negate.test o = !op.test o
                                                      @[simp]
                                                      theorem Cslib.SQL.CmpOp.test_swap (op : CmpOp) (o : Ordering) :
                                                      op.swap.test o.swap = op.test o

                                                      Three-valued comparison of datums: unknown when either side is NULL, otherwise the values must have the same type.

                                                      Instances For
                                                        theorem Cslib.SQL.CmpOp.eval_some_some (op : CmpOp) (a b : Value) :
                                                        op.eval (some a) (some b) = (fun (o : Ordering) => Truth.ofBool (op.test o)) <$> a.cmp b
                                                        theorem Cslib.SQL.CmpOp.eval_negate (op : CmpOp) (a b : Datum) :
                                                        op.negate.eval a b = Truth.not <$> op.eval a b

                                                        The negated comparison has the negated truth value.

                                                        theorem Cslib.SQL.CmpOp.eval_swap (op : CmpOp) (a b : Datum) :
                                                        op.swap.eval b a = op.eval a b

                                                        Exchanging the operands of the swapped comparison does not change it.

                                                        theorem Cslib.SQL.CmpOp.eval_eq_comm (a b : Datum) :
                                                        eq.eval a b = eq.eval b a

                                                        Equality is symmetric.

                                                        a = b is TRUE exactly when a and b are the same value.

                                                        theorem Cslib.SQL.CmpOp.exists_eval_eq_ok_iff {op : CmpOp} {a b : Datum} :
                                                        (∃ (t : Truth), op.eval a b = Except.ok t) ↔ ∃ (τ : SqlType), a.HasType τ ∧ b.HasType τ

                                                        A comparison succeeds exactly when the operands have a common type (NULL has every type).

                                                        theorem Cslib.SQL.CmpOp.exists_eval_eq_ok {op : CmpOp} {a b : Datum} {τ : SqlType} (ha : a.HasType τ) (hb : b.HasType τ) :
                                                        ∃ (t : Truth), op.eval a b = Except.ok t

                                                        A comparison of datums of a common type succeeds.

                                                        A comparison fails only with a type error.

                                                        A unary operator on a datum.

                                                        Instances For

                                                          A binary operator on two datums. Both operands are checked, left first, before NULL propagates.

                                                          Instances For

                                                            Laws of the operators #

                                                            NOT (a op b) is a (negate op) b; for instance NOT (a < b) is a >= b, also when an operand is NULL and when the comparison fails.

                                                            b (swap op) a is a op b; for instance b > a is a < b.

                                                            a AND b is b AND a, also when an operand is not boolean.

                                                            a OR b is b OR a, also when an operand is not boolean.

                                                            NOT (NOT d) is d on boolean datums.

                                                            theorem Cslib.SQL.not_evalBinOp_and (a b : Datum) :
                                                            evalBinOp BinOp.and a b >>= evalUnOp UnOp.not = do let __do_lift ← evalUnOp UnOp.not a let __do_lift_1 ← evalUnOp UnOp.not b evalBinOp BinOp.or __do_lift __do_lift_1

                                                            De Morgan: NOT (a AND b) is NOT a OR NOT b, also when an operand is not boolean.

                                                            theorem Cslib.SQL.not_evalBinOp_or (a b : Datum) :
                                                            evalBinOp BinOp.or a b >>= evalUnOp UnOp.not = do let __do_lift ← evalUnOp UnOp.not a let __do_lift_1 ← evalUnOp UnOp.not b evalBinOp BinOp.and __do_lift __do_lift_1

                                                            De Morgan: NOT (a OR b) is NOT a AND NOT b, also when an operand is not boolean.

                                                            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.

                                                            -(-d) is d 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
                                                              @[simp]

                                                              e IN () is FALSE, even for a NULL probe.

                                                              theorem Cslib.SQL.Datum.inList_cons (e v : Datum) (vs : List Datum) :
                                                              e.inList (v :: vs) = do let t ← CmpOp.eq.eval e v let u ← e.inList vs pure (max t u)
                                                              @[simp]

                                                              e IN (v) is e = v.

                                                              theorem Cslib.SQL.Datum.inList_append (e : Datum) (vs ws : List Datum) :
                                                              e.inList (vs ++ ws) = do let t ← e.inList vs let u ← e.inList ws pure (max t u)

                                                              Membership in a concatenation is the disjunction of the memberships.

                                                              theorem Cslib.SQL.Datum.exists_inList_eq_ok_iff {e : Datum} {vs : List Datum} :
                                                              (∃ (t : Truth), e.inList vs = Except.ok t) ↔ ∀ v ∈ vs, ∃ (t : Truth), CmpOp.eq.eval e v = Except.ok t

                                                              Membership succeeds exactly when every comparison does.

                                                              theorem Cslib.SQL.Datum.exists_inList_eq_ok {e : Datum} {vs : List Datum} {τ : SqlType} (he : e.HasType τ) (hvs : ∀ v ∈ vs, v.HasType τ) :
                                                              ∃ (t : Truth), e.inList vs = Except.ok t

                                                              Over a common type, membership succeeds.

                                                              theorem Cslib.SQL.Datum.inList_eq_ok_true_iff {e : Datum} {vs : List Datum} {τ : SqlType} (he : e.HasType τ) (hvs : ∀ v ∈ vs, v.HasType τ) :

                                                              Over a common type, e IN vs is TRUE exactly when e is non-null and occurs in vs.

                                                              theorem Cslib.SQL.Datum.inList_eq_ok_false_iff {e : Datum} {vs : List Datum} {τ : SqlType} (he : e.HasType τ) (hvs : ∀ v ∈ vs, v.HasType τ) :
                                                              e.inList vs = Except.ok Truth.false ↔ vs = [] ∨ e ≠ none ∧ none ∉ vs ∧ e ∉ vs

                                                              Over a common type, e IN vs is FALSE exactly when vs is empty, or e is non-null, does not occur in vs, and vs has no NULL.

                                                              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.

                                                              theorem Cslib.SQL.Datum.isTrue_not_inList_iff_iff {e : Datum} {vs : List Datum} {τ : SqlType} {t : Truth} (he : e.HasType τ) (hvs : ∀ v ∈ vs, v.HasType τ) (ht : e.inList vs = Except.ok t) :
                                                              (t.not.isTrue = true ↔ ¬∃ v ∈ vs, CmpOp.eq.eval v e = Except.ok Truth.true) ↔ vs = [] ∨ (∃ v ∈ vs, CmpOp.eq.eval v e = Except.ok Truth.true) ∨ e ≠ none ∧ none ∉ vs

                                                              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.