TL;DR
Database query optimizers rely on rewrite rules to speed up queries, but bugs in these rules can cause databases to silently return incorrect data. This paper introduces HoTTSQL, a novel formal semantics for SQL based on Homotopy Type Theory that makes it remarkably easy to mathematically prove these rewrite rules correct using the Coq proof assistant.
What Problem Does This Paper Address?
Every relational database uses a query optimizer to transform a user's SQL query into a faster, semantically equivalent execution plan. This is done using "rewrite rules" (e.g., pushing a WHERE filter down before a JOIN). If a rewrite rule is unsound, the database returns the wrong results—a severe bug that can go unnoticed for years.
Formally verifying these rules is notoriously difficult because SQL is a messy language that freely mixes "set" semantics (no duplicates) and "bag" semantics (allows duplicates). Previous attempts to mechanize SQL proofs treated tables as lists of rows, which required nightmarish inductive proofs to handle row permutations and duplicate elimination. Another mathematical approach, "K-relations," models tables as functions mapping rows to integer multiplicities, but requires proving that every intermediate table is finite ("finite support"). This is easy on paper but incredibly tedious in automated theorem provers.
Methods
The authors propose HoTTSQL, a new denotational semantics for SQL that generalizes K-relations to make mechanized proving easy. They do this via two massive mathematical shifts:
- Dropping the "finite support" requirement: They allow relations to be infinite and tuples to have infinite multiplicities. While real databases aren't infinite, removing this constraint frees the theorem prover from constantly having to prove that intermediate query steps yield finite results.
- Homotopy Type Theory (HoTT): Instead of using integers to represent how many times a row appears, they use univalent types. A relation is modeled as a mathematical function mapping a tuple to a univalent type (representing its multiplicity).
Under this framework, SQL operations translate beautifully into type-theoretic operations:
UNION ALL(bag union) becomes a disjoint union (+).JOINbecomes a Cartesian product (×).DISTINCTbecomes a squash type (a HoTT concept that truncates a type's cardinality to 0 or 1, perfectly mimicking the conversion of a bag to a set).
The authors implemented this semantics in a Coq-based system called DOPCERT (Database OPtimizations CERTified) to mechanically verify SQL rewrite rules.
Datasets
Because this is a formal verification and programming languages paper, it does not use traditional machine learning datasets. Instead, the authors evaluated DOPCERT by attempting to formally prove 23 complex query rewrite rules pulled from database literature, real-world optimizers, and the TPC-H benchmark.
Key Insights and Ideas
- Squash Types are perfect for SQL: The "aha moment" of the paper is mapping SQL's
DISTINCTkeyword to HoTT's squash types. Proving that two sets are equal usually requires complex logic, but in HoTT, proving the equivalence of two squash types only requires proving a simple bi-implication (if A is true then B is true, and vice versa). - Infinity makes finite proofs easier: By generalizing the math to allow infinite databases, the authors paradoxically made it much easier to prove rules for finite databases. The proof assistant no longer gets bogged down checking boundary conditions for finiteness.
- Unnamed schemas prevent naming collisions: The authors model database schemas as binary trees of types rather than named lists (e.g.,
Left.Rightinstead ofTable.Column). This allows the Coq proof assistant to easily decide if two schemas are structurally equivalent without worrying about arbitrary column names.
Key Results
- Concise Proofs: DOPCERT successfully verified 23 rewrite rules with incredibly short proofs. On average, a proof required only 25.2 lines of Coq code.
- First-of-their-kind Verifications: The system successfully proved highly complex rules that had never been formally verified before, including "Magic Set" rewrites (which involve correlated subqueries and aggregations) and index-based rewrites.
- Full Automation for Conjunctive Queries: For a specific, highly common subset of SQL known as conjunctive queries (queries with
SELECT,FROM,WHERE, andAND), the authors built a fully automated decision procedure. Proving equivalence for these queries now requires exactly 1 line of Coq code.
Limitations and Future Work
- No
NULLSupport (Yet): The current system does not supportNULLvalues and SQL's notorious three-valued logic (True, False, Unknown). The authors note this can be added by modifying the external operators, but plan to build native support in future versions to keep proofs simple. - Missing Native SQL Features: Outer joins and window functions are not natively supported, though the authors note they can be expressed in HoTTSQL at the cost of added complexity.
- The Infinity Quirk: Because the semantics allow infinite relations, the system theoretically cannot prove the equivalence of two queries if they only yield the same result on finite databases but differ on infinite ones. However, the authors note that no practical query optimizer rules rely on this distinction.
Summary updated