At a glance
- You build
- tables as finite sets, so that row order and repeated rows no longer matter.
- You prove
- the laws, for every query, each side condition a hypothesis.
| Learn to | Read |
|---|---|
| write conditions as terms, and functions on them by recursion | TPIL 7.5 Other Recursive Data Types · TPIL 8.3 Structural Recursion and Induction |
turn a decidable proposition into a Bool | TPIL 10.8 Decidable Propositions |
| prove two sets equal, element by element | MIL 4.1 Sets · MIL 6.1 Finsets and Fintypes |
Milestones
Your ticks stay in this browser.
0 of 5 done
Theory, on paperWhy databases need laws, and the laws for queries
Databases rewrite your queries using laws
SELECT o.id
FROM orders o JOIN users u ON o.user_id = u.id
WHERE u.country = 'UK';
-- the optimiser filters users before joining:
-- filter(join(orders, users)) = join(orders, filter(users))- The rewrite is faster, because the join meets only the users from the UK. It is correct only under a side condition: the filter mentions only
users. - Get it wrong, and the database silently answers wrongly. Testing tools such as SQLancer have found hundreds of such bugs in mature databases (M. Rigger, Z. Su, Detecting Optimization Bugs in Database Engines via Non-Optimizing Reference Engine Construction, ESEC/FSE 2020).
Each rewrite is a law, and a law can be proved, together with its side condition.
Selections move down, under side conditions
| Law | Holds when | |
|---|---|---|
| 1 | \(\sel{p}(\sel{q}(R)) = \sel{p \wedge q}(R)\) | always (Level 2T) |
| 2 | \(\sel{p}(\sel{q}(R)) = \sel{q}(\sel{p}(R))\) | always (Level 2T) |
| 3 | \(\sel{p}(R \cup S) = \sel{p}(R) \cup \sel{p}(S)\) | always (Level 2T) |
| 4 | \(\sel{p}(R - S) = \sel{p}(R) - S\) | always (Level 3T) |
| 5 | \(\sel{p}(R \times S) = \sel{p}(R) \times S\) | \(p\) mentions only attributes of \(R\) (Level 4T) |
| 6 | \(\sel{p}(R \join S) = \sel{p}(R) \join S\) | \(p\) mentions only attributes of \(R\) |
Levels 2T to 4T proved laws 1 to 5 for two-column tables. Now they are laws about queries of any schema (Level 5F), and their side conditions are about attributes.
Projections and joins have laws, and traps
| Law | Holds when | |
|---|---|---|
| 7 | \(\proj{A}(\proj{B}(R)) = \proj{A}(R)\) | \(A \subseteq B\) |
| 8 | \(\proj{A}(R \cup S) = \proj{A}(R) \cup \proj{A}(S)\) | always |
| 9 | \(\proj{A}(\sel{p}(R)) = \sel{p}(\proj{A}(R))\) | \(p\) mentions only attributes in \(A\) |
| 10 | \(R \join S = S \join R\) | always |
| 11 | \((R \join S) \join T = R \join (S \join T)\) | always |
Not a law: \(\proj{A}(R - S) = \proj{A}(R) - \proj{A}(S)\). Try: find two small tables that break it, before you open Question 7.A. Read more in Abiteboul, Hull, Vianu, Foundations of Databases (1995), ch. 6: Static Analysis and Optimization (section 6.1).
A side condition is about attributes
Definition 7.1 (Attributes of a condition)
Conditions are terms: \(a = v\), \(a = b\), \(\neg c\) and \(c \wedge c'\), for attributes \(a\), \(b\) and values \(v\). The attributes of \(c\), \(\mathit{attrs}(c)\), are those it mentions:
Fact 7.2
If rows \(t\) and \(t'\) agree on \(\mathit{attrs}(c)\), then \(c(t) = c(t')\).
Proof. By induction on \(c\). For \(a = v\) and \(a = b\): \(c\) looks only at \(t(a)\) (and \(t(b)\)), which equal \(t'(a)\) (and \(t'(b)\)). For \(\neg c\) and \(c \wedge c'\): the induction hypotheses, since \(t\) and \(t'\) agree on the attributes of each part. ∎
So "mentions only attributes of \(R\)" is now a fact you can check, by computing: \(\mathit{attrs}(p) \subseteq U\).
Law 5
If \(R : U\), \(S : V\) and \(\mathit{attrs}(p) \subseteq U\), then \(\sel{p}(R \times S) = \sel{p}(R) \times S\).
Proof. The rows \(t \cup s\) and \(t\) agree on \(U\), so \(p(t \cup s) = p(t)\) by Fact 7.2. The rest unfolds the definitions, as in Level 4T. ∎
Lists, bags or sets: which equality?
Definition 7.3
Two lists are equal as bags if one is a reordering of the other, and equal as sets if they represent the same table (Definition 1.2).
| as lists | as bags | as sets | |
|---|---|---|---|
| \([a, b]\) and \([b, a]\) | no | yes | yes |
| \([a, b, a]\) and \([a, b]\) | no | no | yes |
Relational algebra uses set equality. Law 1 holds even for lists (Fact 5T.4); law 10 only for bags or sets (Question 5T.D); \(R \cup R = R\) only for sets.
Questions
Question 7.A Break the non-law
Find \(A\), \(R\) and \(S\) with \(\proj{A}(R - S) \neq \proj{A}(R) - \proj{A}(S)\).
Answer
\(R = \{(a, 1)\}\), \(S = \{(a, 2)\}\) over \(\{\att{name}, \att{week}\}\), and \(A = \{\att{name}\}\). Then \(R - S = R\), so \(\proj{A}(R - S) = \{a\}\); but \(\proj{A}(R) - \proj{A}(S) = \{a\} - \{a\} = \emptyset\). Projection forgets the column that made the rows different.
Question 7.B Why law 9 needs its side condition
What goes wrong with law 9 for \(\proj{\att{name}}(\sel{\att{week} = 1}(\rel{Attended}))\)?
Answer
The right side, \(\sel{\att{week} = 1}(\proj{\att{name}}(\rel{Attended}))\), makes no sense: after the projection there is no \(\att{week}\) to test. The side condition, \(\mathit{attrs}(p) \subseteq A\), is what makes the right side meaningful.
Question 7.C Prove law 7
Prove \(\proj{A}(\proj{B}(R)) = \proj{A}(R)\) when \(A \subseteq B\).
Answer
For every row \(t\), \((t[B])[A] = t[A]\), since \(A \subseteq B\): restricting to \(B\) and then to \(A\) is restricting to \(A\). So \(\proj{A}(\proj{B}(R)) = \{(t[B])[A] \mid t \in R\} = \{t[A] \mid t \in R\} = \proj{A}(R)\).
Question 7.D Prove law 10
Prove \(R \join S = S \join R\), with Definition 5F.2.
Answer
A row of \(R \join S\) is \(t \cup s\) for \(t \in R\) and \(s \in S\) that agree. Agreement is symmetric, and \(t \cup s = s \cup t\), since both give each attribute the value of whichever row has it. So the rows of \(R \join S\) are exactly those of \(S \join R\). With named columns, there is no order of columns to swap: compare Question 3F.D.
Lean, on your computerTables as finite sets, and theorems about queries
In Lean, tables become finite sets
| Equal as | In Lean | From |
|---|---|---|
| lists | List | Lean |
| bags | Multiset | Mathlib |
| sets | Finset | Mathlib |
- Use your Mathlib project now (Level 1, Before you start): start your file with
import Mathlib. l.toFinsetturns a list into the table it represents.- Move your operators from lists to
Finset, or prove them equal as sets:l.toFinset = m.toFinset. - On
Finset, selection isFinset.filter, union∪, difference\, intersection∩, imageFinset.image, and products ×ˢ t. Their lemmas are named after them:Finset.mem_filter,Finset.mem_union,Finset.mem_sdiff,Finset.mem_image,Finset.mem_product.
Read MIL 6.1 Finsets and Fintypes, then browse Mathlib docs, Mathlib.Data.Finset.Defs and Mathlib docs, Mathlib.Data.Multiset.Defs.
Warm-up: laws for finite sets
import Mathlib
-- Law 3, for finite sets: prove two finite sets equal, element by element
theorem law3_finset {α : Type} [DecidableEq α] (p : α → Prop)
[DecidablePred p] (R S : Finset α) :
(R ∪ S).filter p = R.filter p ∪ S.filter p := by
ext t
simp only [Finset.mem_filter, Finset.mem_union]
sorry
-- Law 1, for finite sets
theorem law1_finset {α : Type} (p q : α → Prop) [DecidablePred p]
[DecidablePred q] (R : Finset α) :
(R.filter q).filter p = R.filter (fun t => p t ∧ q t) := by
sorryext treduces an equation between finite sets to "t ∈ … ↔ t ∈ …", for everyt: the form of every proof of the proofs track.[DecidablePred p]: to filter a finite set, Lean must be able to decidepon each element (TPIL 10.8 Decidable Propositions).
Exercise 7.1 In Lean Law 3 for finite sets
Finish law3_finset. After simp only, what is left is the logic of law 3 (Level 2T).
Exercise 7.2 In Lean Law 1 for finite sets
Prove law1_finset the same way. Mathlib proves both laws already: find them, after you have proved them yourself.
Exercise 7.3 In Lean From lists to finite sets
Prove that your select represents Finset.filter, with your theorem toTable_select of Level 6:
theorem select_toFinset {α : Type} [DecidableEq α] (p : α → Bool)
(l : List α) :
(select p l).toFinset = l.toFinset.filter (fun t => p t = true) := by
sorryHint
ext t, then simp only [List.mem_toFinset, Finset.mem_filter]. What is left is toTable_select, unfolded: toTable l t is t ∈ l.
Conditions as terms, and the side condition
Following Definition 7.1, conditions become a type you can inspect. With the simpler design of Level 5F:
def Cond.attrs : Cond → List String
| .eqConst a _ => [a]
| .eqAttr a b => [a, b]
| .not c => c.attrs
| .and c d => c.attrs ++ d.attrsExercise 7.4 In Lean The schema of a query
Define Query.schema, the schema of a query from the schema rules of Level 5F. It needs the schemas of the tables of the database, as an argument.
Exercise 7.5 In Lean Fact 7.2
State and prove Fact 7.2 for your conditions, by induction on c: if two rows give the same value to every attribute in c.attrs, then c.holds gives the same answer on both.
Exercise 7.6 In Lean Law 5 for every query
Prove law 5 for every query. Your theorem will look like this one, for your own types:
-- Law 5, with its side condition as the hypothesis h
theorem select_product (db : Database) (p : Cond) (q₁ q₂ : Query)
(h : p.attrs ⊆ q₁.schema) :
eval db (.select p (.product q₁ q₂)) =
eval db (.product (.select p q₁) q₂) := by sorryp.attrs ⊆ q₁.schema is Definition 7.1. Level 4T proved law 5 for two-column tables: start again from there, and use Fact 7.2 where it used the hypothesis h t s.
Exercise 7.7 In Lean One theorem for each law
State and prove laws 1 to 11 for your queries, each side condition a hypothesis. Then state the non-law of Question 7.A, and check with #eval that your counterexample breaks it.
Next
Stuck, or curious? Ask us on Discord, or come along any Thursday.