At a glance
- You build
optimise, a function from queries to queries.- You prove
- that
optimise qandqhave the same answer, on every database.
| Learn to | Read |
|---|---|
| write recursive functions from queries to queries | TPIL 8.3 Structural Recursion and Induction |
| prove by induction on a query, one case for each constructor | TPIL 7.6 Tactics for Inductive Types · FPIL Interlude, Induction on Other Datatypes |
| write proofs that follow your function's cases | TPIL 8.6 Functional Induction |
| prove termination when the recursion is not structural | TPIL 8.5 Well-Founded Recursion and Induction |
Milestones
Your ticks stay in this browser.
0 of 4 done
Theory, on paperQuery trees, rewritten by the laws
An optimiser rewrites query trees by the laws
A query is a term (Definition 5F.3), which we can draw as a tree. With \(p\): \(\att{dept} = \val{CS}\) and \(q\): \(\att{topic} = \val{Lists}\):
Same answer, but only CS members and the Lists session reach the joins. Law 1 splits \(\sigma_{p \wedge q}\) into two selections, and law 6 (with law 10, to reach the right side) moves each one down to the only table it mentions. On a real database, with thousands of members, that is the difference between a fast query and a slow one.
A correct optimiser never changes an answer
Definition 8.1 (Correct optimiser)
An optimiser is a function \(\mathit{opt}\) from queries to queries. It is correct if \(\lBrack \mathit{opt}(q) \rBrack = \lBrack q \rBrack\) for every query \(q\) and every database (Definition 5F.4).
- To prove it: induction on \(q\), one case for each operator, as on lists in Levels 5T and 6.
- In each case, the rewrite is one of the laws of Level 7, and the law's side condition is exactly what \(\mathit{opt}\) must check before it rewrites.
A first optimiser: merge selections
Start small: an optimiser that only merges a selection into the selection below it, by law 1.
Definition 8.2 (Merging selections)
Fact 8.3
\(\lBrack \mathit{merge}_p(r) \rBrack = \lBrack \sel{p}(r) \rBrack\), for every query \(r\).
Proof. If \(r = \sel{q}(r')\): \(\lBrack \sel{p \wedge q}(r') \rBrack = \sel{p \wedge q}(\lBrack r' \rBrack) = \sel{p}(\sel{q}(\lBrack r' \rBrack)) = \lBrack \sel{p}(\sel{q}(r')) \rBrack\), by law 1 in the middle. Otherwise, the two sides are the same query. ∎
Theorem 8.4
\(\mathit{opt}\) is correct.
Proof. By induction on \(q\). For a name \(T\), \(\mathit{opt}(T) = T\). For \(\sel{p}(q)\):
by Fact 8.3, then the induction hypothesis. Every other operator: the induction hypotheses for its parts, and Definition 5F.4. ∎
Moving selections down, side conditions checked
The next optimiser moves a selection through a product, by law 5, whenever its side condition holds:
The check \(\mathit{attrs}(p) \subseteq \mathit{schema}(q_1)\) is computed, by the program, on the query; in the proof, it is exactly the hypothesis that law 5 needs. If the check fails, the optimiser leaves the query as it is, which is always correct.
Questions
Question 8.A The laziest optimiser
Is \(\mathit{opt}(q) = q\) a correct optimiser?
Answer
Yes: \(\lBrack q \rBrack = \lBrack q \rBrack\). It is correct, but it does nothing useful. Correctness says that the answer never changes; it says nothing about speed. Proving an optimiser faster needs a cost model: a stretch goal.
Question 8.B A rewrite without its check
What goes wrong if \(\mathit{opt}\) moves \(\sel{\att{topic} = \val{Lists}}\) onto \(\rel{Member}\) in \(\sel{\att{topic} = \val{Lists}}(\rel{Member} \times \rel{Session})\)?
Answer
\(\rel{Member}\) has no \(\att{topic}\), so \(\sel{\att{topic} = \val{Lists}}(\rel{Member})\) breaks the schema rule of selection. In a typed design it cannot be written; in the simpler design of Level 5F it keeps no row (no row has a topic to compare), and the answer becomes empty, which is wrong.
Question 8.C Run the first optimiser
Compute \(\mathit{opt}(\sel{p}(\sel{q}(\sel{r}(T))))\).
Answer
\(\mathit{opt}(\sel{r}(T)) = \mathit{merge}_r(T) = \sel{r}(T)\); then \(\mathit{opt}(\sel{q}(\sel{r}(T))) = \mathit{merge}_q(\sel{r}(T)) = \sel{q \wedge r}(T)\); and finally \(\sel{p \wedge (q \wedge r)}(T)\). All three selections are merged, because \(\mathit{opt}\) works from the bottom up.
Question 8.D Why does it terminate?
Why does \(\mathit{opt}\) always terminate?
Answer
It calls itself only on the parts of its argument (\(q\), \(q_1\), \(q_2\)), which are smaller terms, and \(\mathit{merge}_p\) does not call \(\mathit{opt}\) at all: the recursion is structural. An optimiser that rewrites and then calls itself again on the result, until nothing changes, is not structural, and needs a termination proof (TPIL 8.5 Well-Founded Recursion and Induction).
Lean, on your computerA correct optimiser
Definition 8.1 in Lean is one theorem
-- The optimiser never changes an answer
theorem optimise_correct (db : Database) (q : Query) :
eval db (optimise q) = eval db q := by sorryProve it by induction on q, using your laws of Level 7. With the simpler design of Level 5F, eval computes lists, and law 1 holds even as lists (Fact 5T.4): the first optimiser needs no finite sets.
A first optimiser, in Lean
-- Definition 8.2, for the simpler design of Level 5F
def mergeSel (c : Cond) : Query → Query
| .select d q => .select (.and c d) q
| q => .select c q
def optimise : Query → Query
| .table n => .table n
| .select c q => mergeSel c (optimise q)
| .project attrs q => .project attrs (optimise q)
| .rename a b q => .rename a b (optimise q)
| .union q r => .union (optimise q) (optimise r)
| .diff q r => .diff (optimise q) (optimise r)
| .product q r => .product (optimise q) (optimise r)The second case of mergeSel applies only when the first does not: Lean tries the cases in order.
Exercise 8.1 In Lean Run it
Write a query with three selections in a row (Question 8.C), and #eval optimise on it. Then check that eval clubDb gives the same answer before and after.
Hint
To print a Query, add deriving Repr to its definition (and to Cond).
Exercise 8.2 In Lean Fact 8.3
Prove that merging is correct:
theorem mergeSel_correct (db : Database) (c : Cond) (q : Query) :
eval db (mergeSel c q) = eval db (.select c q) := by
sorryHint
cases q. In the select case, unfold with simp [mergeSel, eval, Cond.holds], and use select_select of Level 5T. Every other case is rfl.
Exercise 8.3 In Lean Theorem 8.4
Prove optimise_correct, by induction on q, with Exercise 8.2 in the select case.
Hint
induction q with and one case per constructor. In the select case: rw [optimise, mergeSel_correct], then simp [eval, ih].
Exercise 8.4 In Lean Move selections through products
Add the rewrite of "Moving selections down" to optimise, with its check, and extend the proof with your law 5 of Level 7. Prove that the check is what makes it correct: the hypothesis of law 5 comes from the if.
Hint
In the proof, split on the if of your new case (TPIL 5.8 Split Tactic): one branch has the check as a hypothesis, the other leaves the query alone.
Exercise 8.5 Stretch Your own rewrite
Pick a law of Level 7 and turn it into a rewrite: law 7 merges projections, law 3 moves a selection into a union. Add it, and keep the proof.
Next
Done with Levels 7, 8 and 9? Take on a stretch goal, or pitch us your own challenge.
Stuck, or curious? Ask us on Discord, or come along any Thursday.