Documentation

CslibFrontier.Languages.SQL.QueryEquiv

Equivalence of SQL queries #

A query law says that two queries return the same result on every database instance. What "the same" means depends on what the reader of the result may observe, so equivalence comes in three levels (EquivLevel):

Query.Equiv ℓ q₁ q₂ holds when, on every conforming instance, both queries fail or both succeed with results related at level ℓ. Like expression equivalence (Expr.Equiv), it observes Except.toOption: whether evaluation succeeds and with what, not which error it raises. Ordered equivalence implies bag equivalence, which implies set equivalence (Query.Equiv.mono), and each level is an equivalence relation.

Every query constructor is a congruence at every level, unconditionally in its query arguments (Query.Equiv.select, Query.Equiv.project, Query.Equiv.join, Query.Equiv.unionAll, Query.Equiv.distinct). In its expression arguments it is a congruence for expression equivalence in the typing context of its input (Query.Equiv.select_expr, Query.Equiv.project_expr, Query.Equiv.join_expr), which needs the input to be well-typed, so that its rows conform to that context. Over WHERE c, an outer predicate can be replaced in the typing context of the inner input whatever the type of c, since c keeps some of the input's rows (Query.Equiv.select_select_expr). DISTINCT sees only sets: set-equivalent inputs give bag-equivalent DISTINCT (Query.Equiv.distinct_of_set).

It adapts and redesigns the equivalence levels of sql_equiv (revision 38030a6b) by Evelyn Mitchell, which compared results on every database, with errors identified with empty or missing results; here equivalences quantify over the instances conforming to the schema and keep a failure distinct from every result.

Main definitions #

Main results #

How much of a query's result an equivalence observes.

  • ordered : EquivLevel

    The list of rows, in order.

  • bag : EquivLevel

    The bag of rows: the list up to order.

  • set : EquivLevel

    The set of rows: which rows occur, not how often.

Instances For
    @[instance_reducible]

    The position of a level in ordered < bag < set, from finest to coarsest.

    Instances For

      Distinct levels have distinct ranks.

      @[instance_reducible]

      The order ordered < bag < set, transported along rank: ℓ ≤ ℓ' when ℓ observes at least as much as ℓ'.

      theorem Cslib.SQL.EquivLevel.le_iff_rank_le {ℓ ℓ' : EquivLevel} :
      ℓ ≤ ℓ' ↔ ℓ.rank ≤ ℓ'.rank
      def Cslib.SQL.EquivLevel.Rel {α : Type u_1} :
      EquivLevel → List α → List α → Prop

      Two lists are related at the level: equal; permutations of each other; or with the same members.

      Instances For
        @[simp]
        theorem Cslib.SQL.EquivLevel.rel_ordered {α : Type u_1} {l₁ l₂ : List α} :
        ordered.Rel l₁ l₂ ↔ l₁ = l₂
        @[simp]
        theorem Cslib.SQL.EquivLevel.rel_bag {α : Type u_1} {l₁ l₂ : List α} :
        bag.Rel l₁ l₂ ↔ l₁.Perm l₂
        @[simp]
        theorem Cslib.SQL.EquivLevel.rel_set {α : Type u_1} {l₁ l₂ : List α} :
        set.Rel l₁ l₂ ↔ ∀ (a : α), a ∈ l₁ ↔ a ∈ l₂
        theorem Cslib.SQL.EquivLevel.Rel.refl {α : Type u_1} (ℓ : EquivLevel) (l : List α) :
        ℓ.Rel l l
        theorem Cslib.SQL.EquivLevel.Rel.symm {α : Type u_1} {ℓ : EquivLevel} {l₁ l₂ : List α} (h : ℓ.Rel l₁ l₂) :
        ℓ.Rel l₂ l₁
        theorem Cslib.SQL.EquivLevel.Rel.trans {α : Type u_1} {ℓ : EquivLevel} {l₁ l₂ l₃ : List α} (h₁₂ : ℓ.Rel l₁ l₂) (h₂₃ : ℓ.Rel l₂ l₃) :
        ℓ.Rel l₁ l₃
        theorem Cslib.SQL.EquivLevel.Rel.mem_iff {α : Type u_1} {ℓ : EquivLevel} {l₁ l₂ : List α} (h : ℓ.Rel l₁ l₂) (a : α) :
        a ∈ l₁ ↔ a ∈ l₂

        Lists related at a level have the same members.

        theorem Cslib.SQL.EquivLevel.Rel.mono {α : Type u_1} {ℓ ℓ' : EquivLevel} {l₁ l₂ : List α} (hℓ : ℓ ≤ ℓ') (h : ℓ.Rel l₁ l₂) :
        ℓ'.Rel l₁ l₂

        A relation at a finer level holds at every coarser one.

        theorem Cslib.SQL.EquivLevel.Rel.append {α : Type u_1} {ℓ : EquivLevel} {l₁ l₂ l₃ l₄ : List α} (h₁ : ℓ.Rel l₁ l₂) (h₂ : ℓ.Rel l₃ l₄) :
        ℓ.Rel (l₁ ++ l₃) (l₂ ++ l₄)
        theorem Cslib.SQL.EquivLevel.Rel.lift {α : Type u_1} {β : Type u_2} {ℓ : EquivLevel} {l₁ l₂ : List α} {F : List α → List β} (hperm : ∀ {l₁ l₂ : List α}, l₁.Perm l₂ → (F l₁).Perm (F l₂)) (hmem : ∀ {l₁ l₂ : List α}, (∀ (a : α), a ∈ l₁ ↔ a ∈ l₂) → ∀ (b : β), b ∈ F l₁ ↔ b ∈ F l₂) (h : ℓ.Rel l₁ l₂) :
        ℓ.Rel (F l₁) (F l₂)

        A list operation that respects permutations, and whose members depend only on the members of its input, relates related lists at every level.

        theorem Cslib.SQL.EquivLevel.Rel.filter {α : Type u_1} {ℓ : EquivLevel} {l₁ l₂ : List α} (p : α → Bool) (h : ℓ.Rel l₁ l₂) :
        ℓ.Rel (List.filter p l₁) (List.filter p l₂)
        theorem Cslib.SQL.EquivLevel.Rel.filterMap {α : Type u_1} {β : Type u_2} {ℓ : EquivLevel} {l₁ l₂ : List α} (f : α → Option β) (h : ℓ.Rel l₁ l₂) :
        ℓ.Rel (List.filterMap f l₁) (List.filterMap f l₂)
        theorem Cslib.SQL.EquivLevel.Rel.map {α : Type u_1} {β : Type u_2} {ℓ : EquivLevel} {l₁ l₂ : List α} (f : α → β) (h : ℓ.Rel l₁ l₂) :
        ℓ.Rel (List.map f l₁) (List.map f l₂)
        theorem Cslib.SQL.EquivLevel.Rel.product {α : Type u_1} {β : Type u_2} {ℓ : EquivLevel} {l₁ l₂ : List α} {t₁ t₂ : List β} (h : ℓ.Rel l₁ l₂) (h' : ℓ.Rel t₁ t₂) :
        ℓ.Rel (l₁ ×ˢ t₁) (l₂ ×ˢ t₂)
        theorem Cslib.SQL.EquivLevel.Rel.dedup {α : Type u_1} {ℓ : EquivLevel} {l₁ l₂ : List α} [DecidableEq α] (h : ℓ.Rel l₁ l₂) :
        ℓ.Rel l₁.dedup l₂.dedup
        theorem Cslib.SQL.EquivLevel.Rel.perm_dedup {α : Type u_1} {ℓ : EquivLevel} {l₁ l₂ : List α} [DecidableEq α] (h : ℓ.Rel l₁ l₂) :
        l₁.dedup.Perm l₂.dedup

        Lists with the same members, related at any level, have permuted deduplications.

        theorem Cslib.SQL.EquivLevel.Rel.rowProduct {ℓ : EquivLevel} {n m : ℕ} {rs rs' : List (Row n)} {ss ss' : List (Row m)} (h : ℓ.Rel rs rs') (h' : ℓ.Rel ss ss') :
        ℓ.Rel (Row.product rs ss) (Row.product rs' ss')
        theorem Cslib.SQL.EquivLevel.Rel.filterRows {ℓ : EquivLevel} {n : ℕ} {rs rs' : List (Row n)} (h : ℓ.Rel rs rs') {p p' : Expr n} (hp : ∀ r ∈ rs, (Expr.evalTruth r p).toOption = (Expr.evalTruth r p').toOption) :

        WHERE with predicates that agree on the members of related inputs gives related results, or fails on both.

        theorem Cslib.SQL.EquivLevel.Rel.mapM_evalRow {ℓ : EquivLevel} {n k : ℕ} {rs rs' : List (Row n)} (h : ℓ.Rel rs rs') {es es' : Fin k → Expr n} (hes : ∀ r ∈ rs, (Expr.evalRow es r).toOption = (Expr.evalRow es' r).toOption) :

        SELECT lists that agree on the members of related inputs give related results, or fail on both.

        def Cslib.SQL.Query.Equiv {S : DbSchema} {n : ℕ} (ℓ : EquivLevel) (q₁ q₂ : Query S n) :

        Queries q₁ and q₂ are equivalent at level ℓ: on every instance conforming to the schema, both fail, or both succeed with results related at ℓ.

        Instances For
          theorem Cslib.SQL.Query.equiv_ordered_iff {S : DbSchema} {n : ℕ} {q₁ q₂ : Query S n} :
          Equiv EquivLevel.ordered q₁ q₂ ↔ ∀ (D : Database S), D.Conforms → (eval D q₁).toOption = (eval D q₂).toOption
          theorem Cslib.SQL.Query.Equiv.of_eval_eq {S : DbSchema} {n : ℕ} {ℓ : EquivLevel} {q₁ q₂ : Query S n} (h : ∀ (D : Database S), D.Conforms → eval D q₁ = eval D q₂) :
          Equiv ℓ q₁ q₂

          Queries with the same evaluation on every conforming instance are equivalent at every level.

          theorem Cslib.SQL.Query.Equiv.refl {S : DbSchema} {n : ℕ} {ℓ : EquivLevel} (q : Query S n) :
          Equiv ℓ q q
          theorem Cslib.SQL.Query.Equiv.symm {S : DbSchema} {n : ℕ} {ℓ : EquivLevel} {q₁ q₂ : Query S n} (h : Equiv ℓ q₁ q₂) :
          Equiv ℓ q₂ q₁
          theorem Cslib.SQL.Query.Equiv.trans {S : DbSchema} {n : ℕ} {ℓ : EquivLevel} {q₁ q₂ q₃ : Query S n} (h₁₂ : Equiv ℓ q₁ q₂) (h₂₃ : Equiv ℓ q₂ q₃) :
          Equiv ℓ q₁ q₃
          @[instance_reducible]
          instance Cslib.SQL.Query.Equiv.instTrans {S : DbSchema} {n : ℕ} {ℓ : EquivLevel} :
          Trans (Equiv ℓ) (Equiv ℓ) (Equiv ℓ)
          theorem Cslib.SQL.Query.Equiv.mono {S : DbSchema} {n : ℕ} {ℓ ℓ' : EquivLevel} (hℓ : ℓ ≤ ℓ') {q₁ q₂ : Query S n} (h : Equiv ℓ q₁ q₂) :
          Equiv ℓ' q₁ q₂

          Equivalence at a finer level implies equivalence at every coarser one: ordered ⇒ bag ⇒ set.

          theorem Cslib.SQL.Query.Equiv.of_ordered {S : DbSchema} {n : ℕ} {ℓ : EquivLevel} {q₁ q₂ : Query S n} (h : Equiv EquivLevel.ordered q₁ q₂) :
          Equiv ℓ q₁ q₂

          Ordered equivalence implies equivalence at every level.

          Congruence in the query arguments #

          theorem Cslib.SQL.Query.Equiv.select {S : DbSchema} {n : ℕ} {ℓ : EquivLevel} (p : Expr n) {q q' : Query S n} (h : Equiv ℓ q q') :
          theorem Cslib.SQL.Query.Equiv.project {S : DbSchema} {n k : ℕ} {ℓ : EquivLevel} (es : Fin k → Expr n) {q q' : Query S n} (h : Equiv ℓ q q') :
          Equiv ℓ (Query.project es q) (Query.project es q')
          theorem Cslib.SQL.Query.Equiv.join {S : DbSchema} {n m : ℕ} {ℓ : EquivLevel} (θ : Expr (n + m)) {q₁ q₁' : Query S n} {q₂ q₂' : Query S m} (h₁ : Equiv ℓ q₁ q₁') (h₂ : Equiv ℓ q₂ q₂') :
          Equiv ℓ (Query.join θ q₁ q₂) (Query.join θ q₁' q₂')
          theorem Cslib.SQL.Query.Equiv.unionAll {S : DbSchema} {n : ℕ} {ℓ : EquivLevel} {q₁ q₁' q₂ q₂' : Query S n} (h₁ : Equiv ℓ q₁ q₁') (h₂ : Equiv ℓ q₂ q₂') :
          Equiv ℓ (q₁.unionAll q₂) (q₁'.unionAll q₂')
          theorem Cslib.SQL.Query.Equiv.distinct {S : DbSchema} {n : ℕ} {ℓ : EquivLevel} {q q' : Query S n} (h : Equiv ℓ q q') :

          DISTINCT observes only the set of rows of its input: set-equivalent inputs give bag-equivalent results.

          Congruence in the expression arguments #

          theorem Cslib.SQL.Query.Equiv.select_expr {S : DbSchema} {n : ℕ} {ℓ : EquivLevel} {q : Query S n} {Γ : Fin n → SqlType} (hq : q.typeOf = some Γ) {p p' : Expr n} (hp : Expr.Equiv Γ p p') :

          Replacing a WHERE predicate by an equivalent one in the typing context of the input gives an equivalent query.

          theorem Cslib.SQL.Query.Equiv.select_select_expr {S : DbSchema} {n : ℕ} {ℓ : EquivLevel} {q : Query S n} {Γ : Fin n → SqlType} (hq : q.typeOf = some Γ) {p p' : Expr n} (hp : Expr.Equiv Γ p p') (c : Expr n) :

          Replacing the outer predicate of WHERE over WHERE c by an equivalent one in the typing context of the inner input gives an equivalent query, whatever the type of c: the rows that c keeps are rows of the input, so they conform to that context. Query.Equiv.select_expr would need WHERE c itself to be well-typed.

          theorem Cslib.SQL.Query.Equiv.project_expr {S : DbSchema} {n k : ℕ} {ℓ : EquivLevel} {q : Query S n} {Γ : Fin n → SqlType} (hq : q.typeOf = some Γ) {es es' : Fin k → Expr n} (hes : ∀ (i : Fin k), Expr.Equiv Γ (es i) (es' i)) :
          Equiv ℓ (Query.project es q) (Query.project es' q)

          Replacing the expressions of a SELECT list by equivalent ones in the typing context of the input gives an equivalent query.

          theorem Cslib.SQL.Query.Equiv.join_expr {S : DbSchema} {n m : ℕ} {ℓ : EquivLevel} {q₁ : Query S n} {q₂ : Query S m} {Γ : Fin n → SqlType} {Δ : Fin m → SqlType} (h₁ : q₁.typeOf = some Γ) (h₂ : q₂.typeOf = some Δ) {θ θ' : Expr (n + m)} (hθ : Expr.Equiv (Fin.append Γ Δ) θ θ') :
          Equiv ℓ (Query.join θ q₁ q₂) (Query.join θ' q₁ q₂)

          Replacing a join predicate by an equivalent one in the typing context of the joined rows gives an equivalent query.

          Equivalence at the level ℓ, as a setoid.

          Instances For