The Proofs and Programs Club @ RHUL

After the tracks meet · leans on FPIL

Level 9 Fast Joins

After Level 6 · Next: Level 7

At a glance

You build
a hash join and a sort-merge join, on arrays or lists.
You prove
that each computes exactly the join of Level 4F.
What to learn first, and where
Learn toRead
program with Array, and show Lean that loops terminateFPIL 3.4 Arrays and Indexing · FPIL 8.3 Arrays and Termination
prove a fast function equal to a simple oneFPIL 8.1 Tail Recursion · FPIL 8.2 Proving Equivalence
use Std.HashMap, with Hashable keysFPIL 3.5.4 Hashing
prove equality up to order and duplicatesMIL 6.1 Finsets and Fintypes · Mathlib docs, Mathlib.Data.Multiset.Defs

Milestones

Your ticks stay in this browser.

0 of 4 done

Theory, on paperFast joins, and when they are correct

Fast joins must compute the same table

With a thousand rows on each side, that is a million steps against a few thousand. Every database implements several join algorithms, and picks one for each query (Abiteboul, Hull, Vianu, Foundations of Databases (1995), ch. 6: Static Analysis and Optimization, section 6.1).

"Future users of large data banks must be protected from having to know how the data is organized in the machine." E. F. Codd, 1970 (A Relational Model of Data for Large Shared Data Banks).

A correct join algorithm represents the join

Definition 9.1 (Correct join algorithm)

A function \(J\) from two lists to a list is a correct join if, whenever \(l\) represents \(R\) and \(m\) represents \(S\) (Definition 1.2), \(J(l, m)\) represents \(R \join_{k_1 = k_2} S\) (Definition 4F.5).

Hash join, on paper

Write \(\mathit{index}(k)\) for the rows \(s\) of \(m\) with \(k_2(s) = k\): what the hash map returns for the key \(k\). Then

\[\mathit{hashJoin}(l, m) = [\,(t, s) \mid t \text{ in } l,\ s \text{ in } \mathit{index}(k_1(t))\,].\]

Theorem 9.2

If \(\mathit{index}(k)\) represents \(\{s \in \mathit{table}(m) \mid k_2(s) = k\}\) for every key \(k\), then \(\mathit{hashJoin}\) is a correct join.

Proof. \((t, s)\) is in \(\mathit{hashJoin}(l, m)\) \(\iff\) \(t\) is in \(l\) and \(s\) is in \(\mathit{index}(k_1(t))\) \(\iff\) \(t\) is in \(l\), \(s\) is in \(m\) and \(k_2(s) = k_1(t)\) \(\iff\) \((t, s) \in \mathit{table}(l) \join_{k_1 = k_2} \mathit{table}(m)\). ∎

So the whole proof rests on one fact about the hash map: a lookup finds exactly the rows with that key. That is a theorem about Std.HashMap, and it needs the key's == and hash to agree.

Questions

Question 9.A Count the steps

How many pairs of rows does joinOn compare on \(\rel{Attended}\) and \(\rel{Session}\)? How many lookups does a hash join make?

Answer

\(4 \cdot 4 = 16\) comparisons. A hash join inserts the 4 sessions, then makes 4 lookups, one for each attendance.

Question 9.B Why not the same list?

Why does Definition 9.1 ask for the same table, not the same list as joinOn?

Answer

A hash map does not keep rows in the order they were inserted, so a hash join may find the pairs in another order. Asking for the same list would rule out every fast join; asking for the same table is what a user can see.

Question 9.C A broken hash

What goes wrong if two equal keys can have different hashes?

Answer

A lookup searches only the bucket of the key's hash, so it can miss rows whose equal key was stored under another hash. The join then loses pairs, and is no longer correct. In Lean, the class LawfulHashable rules this out.

Lean, on your computerA correct hash join

A hash join, in Lean

Leanhashjoin.lean
import Std.Data.HashMap
open Std

-- The rows of s, grouped by key
def buildIndex {β κ : Type} [BEq κ] [Hashable κ] (k₂ : β → κ)
    (s : List β) : HashMap κ (List β) :=
  s.foldl (fun m b => m.insert (k₂ b) (b :: m.getD (k₂ b) [])) {}

def hashJoin {α β κ : Type} [BEq κ] [Hashable κ] (k₁ : α → κ) (k₂ : β → κ)
    (r : List α) (s : List β) : List (α × β) :=
  let index := buildIndex k₂ s
  r.flatMap (fun a => (index.getD (k₁ a) []).map (fun b => (a, b)))

Exercise 9.1 In Lean Run it

#eval hashJoin (fun a => a.2) (fun s => s.1) attended session, and compare with joinOn of Level 4F: the same four pairs, maybe in another order.

Definition 9.1 in Lean is one theorem

Leangoal.leanSketch
-- The hash join computes the join
theorem hashJoin_correct {α β κ : Type} [BEq κ] [LawfulBEq κ] [Hashable κ]
    [LawfulHashable κ] (k₁ : α → κ) (k₂ : β → κ) (r : List α) (s : List β)
    (x : α × β) :
    x ∈ hashJoin k₁ k₂ r s ↔ x ∈ joinOn k₁ k₂ r s := by sorry

The statement compares the tables the two lists represent, element by element, as toTable did in Level 6. Start from your joinOn of Level 4F, proved correct first.

Exercise 9.2 In Lean joinOn is the join

Assuming [LawfulBEq κ], prove that joinOn computes the join: x ∈ joinOn k₁ k₂ r s ↔ x.1 ∈ r ∧ x.2 ∈ s ∧ k₁ x.1 = k₂ x.2, with your theorems of Level 6 for select and product.

Exercise 9.3 In Lean The hash join is correct

Prove hashJoin_correct. First prove the property of Theorem 9.2 for buildIndex: b ∈ (buildIndex k₂ s).getD k [] ↔ b ∈ s ∧ k₂ b = k.

Hint

buildIndex is a foldl, so prove a more general statement, about foldl from any starting map, by induction on s. The lemmas you need about getD after insert are in Std.HashMap: find them with Loogle.

Exercise 9.4 Stretch Sort-merge join

Write a sort-merge join on Array, and prove it correct the same way. You will need to show Lean that the merge loop terminates (FPIL 8.3 Arrays and Termination), and that sorting keeps the same rows (FPIL 8.6 Insertion Sort and Array Mutation).

Beyond Level 9Stretch goals

What relational algebra can and cannot express

Read E. F. Codd, Relational Completeness of Data Base Sublanguages (1972); A. K. Chandra and P. M. Merlin, Optimal Implementation of Conjunctive Queries in Relational Data Bases (STOC 1977); A. V. Aho and J. D. Ullman, The Universality of Data Retrieval Languages (POPL 1979); and Abiteboul, Hull, Vianu, Foundations of Databases (1995), ch. 6: Static Analysis and Optimization.

Beyond Level 9: bags, NULL, aggregation, SQL

Each of these is a challenge on its own. Pick one, and tell us at the club.

Others did this in Coq: read them after trying

Verified relational databases, in Coq (now Rocq):

For the theory of databases: S. Abiteboul, R. Hull, V. Vianu. Foundations of Databases. Addison-Wesley, 1995.

Next

  1. Level 7 Laws for every query

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.