Documentation

CslibFrontier.Languages.SQL.QueryLaws

Proved SQL query transformations #

The rewrite laws of the select–project–join–union fragment, each at the strongest equivalence level that holds (Cslib.SQL.Query.Equiv) and with the side conditions that strict evaluation needs. Evaluation fails as soon as a predicate or projection fails on any row of its input, so a law that changes the rows on which an expression is evaluated holds only when that evaluation cannot fail. Its side condition is then the decidable safety check (Expr.IsSafePredicate, Expr.IsSafe) of that expression, in the typing context of its input, which must be well-typed (q.typeOf = some Γ). Laws that evaluate every expression on the same rows on both sides need no condition. The tests show for each condition a concrete instance on which the law fails without it.

All laws are ordered equivalences except the two commutativity laws, which hold for bags only.

It adapts and redesigns the query laws of sql_equiv (revision 38030a6b) by Evelyn Mitchell. There, a WHERE predicate that failed on a row dropped that row, so filter laws needed no conditions, and pushdown passed column names through derived tables; here a failure fails the query, each law states the safety condition it needs, and moving a predicate across a derived table or into a join is explicit substitution or renaming.

Main results #

theorem Cslib.SQL.Expr.IsSafePredicate.filterRows_eq_ok {n : ℕ} {Γ : Fin n → SqlType} {p : Expr n} (hp : IsSafePredicate Γ p) {rs : List (Row n)} (hrs : ∀ r ∈ rs, Row.Conforms Γ r) :

A safe predicate never makes WHERE fail on conforming rows: it keeps the rows on which the predicate is TRUE.

@[simp]

TRUE keeps every row.

theorem Cslib.SQL.Expr.toOption_filterRows_map {n k : ℕ} {p : Expr k} {p' : Expr n} {f : Row n → Row k} {rs : List (Row n)} (h : ∀ r ∈ rs, evalTruth (f r) p = evalTruth r p') :

WHERE on rows computed by f is WHERE on their sources with a predicate that reads through f, followed by f.

theorem Cslib.SQL.Expr.toOption_evalTruth_and {n : ℕ} (c p : Expr n) (r : Row n) :
(evalTruth r (binary BinOp.and c p)).toOption = (evalTruth r c).toOption.bind fun (s : Truth) => Option.map (fun (x : Truth) => min s x) (evalTruth r p).toOption

c AND p as a predicate: both sides are evaluated, and the truth value is their Kleene conjunction.

theorem Cslib.SQL.Expr.toOption_filterRows_append {n : ℕ} (p : Expr n) (rs ss : List (Row n)) :
(p.filterRows (rs ++ ss)).toOption = (p.filterRows rs).toOption.bind fun (a : List (Row n)) => Option.map (fun (x : List (Row n)) => a ++ x) (p.filterRows ss).toOption

WHERE on two lists one after the other is WHERE on their concatenation.

theorem Cslib.SQL.Expr.evalRow_congr {n m k : ℕ} {es : Fin k → Expr n} {es' : Fin k → Expr m} {r : Row n} {r' : Row m} (h : ∀ (i : Fin k), eval r (es i) = eval r' (es' i)) :
evalRow es r = evalRow es' r'

A SELECT list depends on the row only through the results of its expressions.

theorem Cslib.SQL.Expr.evalRow_col_comp {n k : ℕ} (ρ : Fin k → Fin n) (r : Row n) :
evalRow (fun (i : Fin k) => col (ρ i)) r = Except.ok (r ∘ ρ)

A SELECT list of column references reindexes the row.

Selections #

theorem Cslib.SQL.Query.select_select {n : ℕ} {S : DbSchema} {q : Query S n} {Γ : Fin n → SqlType} (hq : q.typeOf = some Γ) {p : Expr n} (hp : Expr.IsSafePredicate Γ p) (c : Expr n) :

Selection merge: two successive WHERE clauses are one WHERE with their conjunction, when the outer predicate is safe. The conjunction evaluates p on rows that the inner WHERE drops, so an unsafe p could make only the right side fail.

theorem Cslib.SQL.Query.select_comm {n : ℕ} {S : DbSchema} {q : Query S n} {Γ : Fin n → SqlType} (hq : q.typeOf = some Γ) {p c : Expr n} (hp : Expr.IsSafePredicate Γ p) (hc : Expr.IsSafePredicate Γ c) :

Selection commutation: two safe WHERE clauses may be applied in either order.

theorem Cslib.SQL.Query.select_project {n k : ℕ} {S : DbSchema} {q : Query S n} {Γ : Fin n → SqlType} (hq : q.typeOf = some Γ) {es : Fin k → Expr n} (hes : ∀ (i : Fin k), Expr.IsSafe Γ (es i)) (p : Expr k) :

Selection through a projection, or a WHERE over a derived table: filtering the rows of SELECT es FROM q by p is filtering the rows of q by p with the projection substituted, then projecting, when the projection is safe. The right side evaluates es only on the rows it keeps, so an unsafe projection could make only the left side fail.

theorem Cslib.SQL.Query.eval_select_of_isSafePredicate {n : ℕ} {S : DbSchema} {q : Query S n} {Γ : Fin n → SqlType} (hq : q.typeOf = some Γ) {p : Expr n} (hp : Expr.IsSafePredicate Γ p) {D : Database S} (hD : D.Conforms) :

A safe WHERE over a well-typed query is a filter of its rows.

theorem Cslib.SQL.Query.eval_join_of_isSafePredicate {n m : ℕ} {S : DbSchema} {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.IsSafePredicate (Fin.append Γ Δ) θ) {D : Database S} (hD : D.Conforms) :
eval D (join θ q₁ q₂) = do let rs ← eval D q₁ let ss ← eval D q₂ pure (List.map (fun (x : Row n × Row m) => match x with | (r, s) => Fin.append r s) (List.filter (fun (x : Row n × Row m) => match x with | (r, s) => decide ((Expr.evalTruth (Fin.append r s) θ).toOption = some Truth.true)) (rs ×ˢ ss)))

A join with a safe predicate over well-typed inputs filters the pairs of input rows, as Query.eval_join_eq_ok_of_forall says, whenever both inputs succeed.

theorem Cslib.SQL.Query.select_join_left {n m : ℕ} {S : DbSchema} {q₁ : Query S n} {q₂ : Query S m} {Γ : Fin n → SqlType} {Δ : Fin m → SqlType} (h₁ : q₁.typeOf = some Γ) (h₂ : q₂.typeOf = some Δ) {p : Expr n} (hp : Expr.IsSafePredicate Γ p) {θ : Expr (n + m)} (hθ : Expr.IsSafePredicate (Fin.append Γ Δ) θ) :
Equiv EquivLevel.ordered (select (Expr.rename (Fin.castAdd m) p) (join θ q₁ q₂)) (join θ (select p q₁) q₂)

Predicate pushdown into the left input of a join: a safe predicate on the left columns may be applied before the join, when the join predicate is safe too. Without safety the sides evaluate the predicates on different rows: with an empty right input, the left side evaluates nothing while the right side evaluates p on every left row.

theorem Cslib.SQL.Query.select_join_right {n m : ℕ} {S : DbSchema} {q₁ : Query S n} {q₂ : Query S m} {Γ : Fin n → SqlType} {Δ : Fin m → SqlType} (h₁ : q₁.typeOf = some Γ) (h₂ : q₂.typeOf = some Δ) {p : Expr m} (hp : Expr.IsSafePredicate Δ p) {θ : Expr (n + m)} (hθ : Expr.IsSafePredicate (Fin.append Γ Δ) θ) :
Equiv EquivLevel.ordered (select (Expr.rename (Fin.natAdd n) p) (join θ q₁ q₂)) (join θ q₁ (select p q₂))

Predicate pushdown into the right input of a join, under the same conditions as Query.select_join_left.

theorem Cslib.SQL.Query.select_join_true {n m : ℕ} {S : DbSchema} (θ : Expr (n + m)) (q₁ : Query S n) (q₂ : Query S m) :
Equiv EquivLevel.ordered (select θ (join (Expr.lit (Value.bool true)) q₁ q₂)) (join θ q₁ q₂)

A WHERE over a cross join is an inner join ON the same predicate. Both evaluate the predicate on every pair, so no condition is needed.

WHERE TRUE keeps every row.

Projections #

theorem Cslib.SQL.Query.project_project {n k l : ℕ} {S : DbSchema} {q : Query S n} {Γ : Fin n → SqlType} (hq : q.typeOf = some Γ) {g : Fin k → Expr n} (hg : ∀ (i : Fin k), Expr.IsSafe Γ (g i)) (f : Fin l → Expr k) :
Equiv EquivLevel.ordered (project f (project g q)) (project (fun (j : Fin l) => Expr.subst g (f j)) q)

Projection fusion: projecting twice is projecting once with the inner projection substituted, when the inner projection is safe. The fused projection evaluates only the inner expressions that the outer one uses, so an unsafe inner projection could make only the left side fail.

SELECT *: projecting on every column in order is the identity.

UNION ALL #

theorem Cslib.SQL.Query.select_unionAll {n : ℕ} {S : DbSchema} (p : Expr n) (q₁ q₂ : Query S n) :
Equiv EquivLevel.ordered (select p (q₁.unionAll q₂)) ((select p q₁).unionAll (select p q₂))

WHERE distributes over UNION ALL.

theorem Cslib.SQL.Query.project_unionAll {n k : ℕ} {S : DbSchema} (es : Fin k → Expr n) (q₁ q₂ : Query S n) :
Equiv EquivLevel.ordered (project es (q₁.unionAll q₂)) ((project es q₁).unionAll (project es q₂))

A SELECT list distributes over UNION ALL.

theorem Cslib.SQL.Query.unionAll_assoc {n : ℕ} {S : DbSchema} (q₁ q₂ q₃ : Query S n) :
Equiv EquivLevel.ordered ((q₁.unionAll q₂).unionAll q₃) (q₁.unionAll (q₂.unionAll q₃))

UNION ALL is associative.

theorem Cslib.SQL.Query.unionAll_comm {n : ℕ} {S : DbSchema} (q₁ q₂ : Query S n) :
Equiv EquivLevel.bag (q₁.unionAll q₂) (q₂.unionAll q₁)

UNION ALL is commutative as a bag. It is not as a list: the left input comes first.

Joins #

theorem Cslib.SQL.Query.join_comm {n m : ℕ} {S : DbSchema} (θ : Expr (n + m)) (q₁ : Query S n) (q₂ : Query S m) :
Equiv EquivLevel.bag (join θ q₁ q₂) (project (fun (i : Fin (n + m)) => Expr.col (finAddFlip i)) (join (Expr.rename (⇑finAddFlip) θ) q₂ q₁))

Join commutativity: exchanging the inputs of a join, and the two blocks of columns of its predicate and its result, gives the same bag of rows. The pairs are enumerated in another order, so it is not an equality of lists.

DISTINCT #

DISTINCT ignores the duplicates that UNION ALL of a query with itself creates.

WHERE commutes with DISTINCT. The left side evaluates p on one copy of each row and the right side on every copy, but the copies are equal rows, so p has the same result, or fails, on all of them, and no condition is needed. Both sides keep the last copy of each kept row.