Translating Canonical SQL to Imperative Code in Coq

Véronique Benzaken, Évelyne Contejean, Mohammed Houssem Hachmaoui, Chantal Keller, Louis Mandel, Avraham Shinnar, Jérôme Siméon

TL;DR

DBCert is the first mechanically verified compiler that translates SQL queries into executable imperative code (specifically JavaScript). By using the Coq proof assistant, the authors ensure that the compiler correctly handles notoriously tricky SQL features like NULL values and correlated subqueries, where popular unverified libraries often fail.

What Problem Does This Paper Address?

SQL is the bedrock of modern data management, yet its semantics are surprisingly subtle. Two areas in particular—NULL values (which use three-valued logic) and correlated subqueries (where an inner query refers to variables in an outer query)—are frequent sources of bugs. Even popular libraries like AlaSQL can produce incorrect results for complex nested queries because they rely on host language comparisons (like JavaScript's !=) that don't match SQL's formal specification.

While researchers have previously formalized SQL semantics, there has been a significant gap between these formal models and actual, optimizable execution engines. This paper bridges that gap, moving from a high-level declarative query to low-level, verified imperative code.

Methods

The authors built DBCert, a compiler pipeline verified in the Coq proof assistant. The compilation process involves several intermediate languages to break down the complexity of the translation:

  1. SQL to $SQL_{Alg}$: The query is first put into a canonical form ($SQL_{Coq}$) and then translated into an extended relational algebra that handles grouping and environments.
  2. $SQL_{Alg}$ to $NRA^e$: This is a key step where the implicit SQL environment (the context for correlated queries) is made explicit. NULL values are encoded using "sum types" (the either type), allowing the compiler to formally reason about unknown logic states.
  3. $NRA^e$ to Imp: The compiler transitions from a functional, algebraic model to Imp, a small imperative language with mutable variables and loops. This transition is managed through several sub-steps (NNRC, NNRS, NNRSimp) to isolate paradigm shifts like variable shadowing and side effects.
  4. Imp to JavaScript: Finally, the verified Imp code is pretty-printed into standard JavaScript to run in environments like Node.js.

The architecture of this pipeline is shown below:

The DBCert compilation pipeline, moving from SQL through various intermediate representations to JavaScript.

Datasets

The authors evaluated DBCert using a benchmark of 15 challenging queries:

  • 4 queries specifically designed to test NULL value semantics (adapted from Guagliardo and Libkin).
  • 11 queries focused on correlated subqueries (adapted from Benzaken and Contejean).

For performance testing, they used a database containing 58,800 entries in JSON format to compare execution speeds against unverified engines.

Key Insights and Ideas

  • Environment "Slicing": To handle correlated queries correctly, the compiler maintains a stack of "slices." Each slice represents a nesting level of the query. This ensures that when an inner query asks for a variable, it looks through the stack to find the correct context from the outer query.
  • The NULL Logic Trap: The paper highlights how easy it is for unverified engines to fail. For example, in SQL, 1 != NULL is unknown, not true. DBCert’s use of sum types in the intermediate algebra forces the compiler to handle the unknown case explicitly.
  • The Floating-Point Conflict: A fascinating "aha moment" in the paper is the realization that floating-point arithmetic is technically incompatible with SQL's bag semantics. Because floating-point addition isn't truly associative, the result of a SUM() could change depending on the internal processing order. The authors had to use specific axioms to isolate these floating-point properties during verification.
  • Verification via Pipeline: By using many intermediate languages, the authors could prove small, manageable properties at each step rather than trying to prove the entire jump from SQL to JavaScript in one go.

Key Results

  • Correctness: DBCert achieved 100% accuracy (15/15) on the correctness benchmarks. In contrast, AlaSQL failed 4 out of 11 correlated query tests and 1 out of 4 NULL tests.
  • Handling Complexity: The paper demonstrates how subtle changes in variable references in nested queries (e.g., referring to b1 vs b2) can lead to vastly different results, a challenge DBCert handles correctly as shown in the table below:
Comparison of results for two similar correlated queries (Q1 and Q2) showing how different environment handling leads to different outputs.
  • Performance: The generated JavaScript code is roughly 2x slower than AlaSQL. While there is a performance penalty for verification, it remains within a practical range for many applications.

Limitations and Future Work

  • Missing Features: The current version of DBCert does not yet support DISTINCT, ORDER BY, recursive queries, or certain silent type coercions common in SQL.
  • Optimization: While $NRA^e$ allows for algebraic optimizations, the authors note that more work is needed to eliminate redundant data packing/unpacking introduced by the many intermediate translation steps.
  • Unverified Ends: The very beginning (the SQL parser) and the very end (the JavaScript pretty-printer) are not yet formally verified, though the core logic of the compiler is.

Summary updated

BibTeX
@inproceedings{benzaken2022translating,
  author = {Véronique Benzaken and Évelyne Contejean and Mohammed Houssem Hachmaoui and Chantal Keller and Louis Mandel and Avraham Shinnar and Jérôme Siméon},
  title = {{Translating Canonical SQL to Imperative Code in Coq}},
  booktitle = {Proc. ACM Program. Lang},
  year = {2022},
  url = {https://openreview.net/forum?id=vYbwLYIP0Jc},
}
Figure