Abstract
While SQL is the industry standard for data manipulation, its implementation in database engines often diverges from formal semantics, particularly regarding complex features like NULL values and correlated subqueries. This paper presents DBCert, the first mechanically verified compiler that translates SQL queries into imperative JavaScript code. Developed within the Coq proof assistant, DBCert ensures that the generated imperative code preserves the mathematical semantics of the original SQL query, bridging the gap between high-level relational logic and executable software.
Introduction
SQL is foundational to modern data infrastructure, yet its specification is notoriously difficult to implement correctly. Features such as three-valued logic (for NULL values) and correlated subqueries (where inner queries reference outer scopes) frequently lead to inconsistent behavior across different database systems. While formal methods have previously defined SQL's theoretical semantics, these efforts rarely extend to the "last mile" of compilation: the generation of efficient, executable code.
DBCert addresses this challenge by providing a verified pipeline from a subset of SQL to JavaScript. By proving the compiler's correctness in Coq, the authors eliminate the possibility of translation bugs, ensuring that the resulting imperative code behaves exactly as the relational model dictates.
The DBCert Compilation Pipeline
The compiler utilizes a multi-stage architecture to manage the complexity of the formal proof. Each stage transforms the query through a series of intermediate languages, progressively lowering the abstraction level from relational algebra to imperative code.
- SQLCoq to SQLAlg: The input SQL is canonicalized and translated into a relational algebra capable of handling environments and grouping operations.
- SQLAlg to NRA$^e$: The Nested Relational Algebra with environments (NRA$^e$) makes the implicit SQL context explicit. This stage is critical for handling SQL’s three-valued logic, which is encoded using sum types to distinguish between valid results and NULL values.
- NRA$^e$ to NNRC: The algebra is converted into the Named Nested Relational Calculus (NNRC), a functional language with explicit variable bindings.
- NNRC to NNRS to Imp: This stage represents a significant paradigm shift from functional to imperative logic. The authors introduced NNRS, a language that enforces a "phase distinction"—ensuring variables are either being written to or read from, but never both simultaneously—to prevent aliasing bugs during the transition to imperative structures.
- Imp to JavaScript: The final stage translates a simplified imperative language into standard JavaScript, suitable for execution in environments like Node.js.
The entire verified core comprises approximately 51,000 lines of Coq code, with 20,000 lines dedicated to the frontend and 31,000 lines to the backend and imperative transitions.
Key Technical Insights
Managing Contextual Overhead
In standard SQL, subqueries implicitly access variables from outer scopes. In a verified environment, this "knowledge" must be explicitly managed. DBCert employs a stack-based environment where administrative steps are injected to push and pop data slices as the compiler traverses nested query structures, ensuring the context is always mathematically sound.
The Floating-Point Challenge
A notable finding during verification involved the non-associative nature of floating-point addition. While relational theory assumes that aggregating a "bag" of numbers is associative ($a + b = b + a$), floating-point arithmetic does not strictly adhere to this. To maintain the integrity of the proof, the authors isolated these unsound axioms, preventing them from compromising the broader logic of the compiler.
Safety via Phase Distinction
To safely bridge the gap between functional and imperative paradigms, DBCert utilizes a "write-then-freeze" approach for variables. This prevents pointer and aliasing errors, which are common sources of bugs in imperative code and typically make such code much harder to verify than its functional counterparts.
Evaluation and Results
DBCert was evaluated against a benchmark of 15 challenging queries designed to test edge cases in SQL engines: 4 queries focused on NULL value semantics and 11 focused on complex correlated subqueries.
Correctness
DBCert successfully passed 15/15 benchmark queries. In contrast, existing tools showed significant gaps:
- AlaSQL (a popular JavaScript SQL library) failed 7 of the 11 correlated queries and 1 of the 4 NULL queries.
- Q*cert failed all NULL-related queries as it lacks native support for three-valued logic.
Performance
Performance testing was conducted on a synthetic dataset of 58,800 entries. While DBCert prioritizes correctness, it remains performant enough for practical use, running approximately two times slower than AlaSQL. This overhead is considered acceptable given the total elimination of translation-related bugs.
Conclusion and Future Work
DBCert demonstrates that it is possible to build a verified compiler that spans the entire distance from SQL to imperative code. While the current implementation does not support the full SQL standard—omitting features like ORDER BY, DISTINCT, and recursive queries—it provides a robust, open-source foundation for future verified data tools. Future work will focus on integrating verified query optimizations to close the performance gap with unverified engines and expanding the supported SQL surface area.
Summary updated