Inferring SQL Queries Using Program Synthesis

Alvin Cheung, Armando Solar-Lezama, Samuel MaddenarXiv 2012

Abstract

Modern application development often suffers from an "impedance mismatch" between imperative programming languages and relational databases. Developers frequently implement data processing logic—such as joins and aggregations—within application code rather than delegating it to the database engine. This paper introduces QBS (Query By Synthesis), a system that automatically transforms imperative Java code into efficient SQL queries using program synthesis and formal verification. By inferring code invariants and postconditions, QBS bridges the performance gap while allowing developers to maintain intuitive coding practices.

Introduction

Object-Relational Mapping (ORM) frameworks like Hibernate simplify database interactions by treating records as native objects. However, this abstraction often encourages inefficient patterns where developers fetch large datasets into memory to perform filtering or joining in the application layer. These "application-side joins" bypass database optimizations and create significant performance bottlenecks. QBS addresses this by treating query extraction as a program synthesis problem rather than a simple pattern-matching task.

Methodology

The QBS workflow transforms imperative code into declarative SQL through a multi-stage process involving language conversion, formal modeling, and automated reasoning.

Kernel Language and Ordered Relations

The system first converts Java code into a simplified "kernel language" that isolates data manipulation logic. To accurately represent Java's List structures, the authors developed a formal Theory of Ordered Relations. Unlike standard relational algebra, which operates on unordered sets, this theory preserves record ordering, ensuring that the synthesized SQL matches the original program's semantics.

Formal definitions of operations within the Theory of Ordered Relations, including ordered joins and aggregations.

Synthesis via Sketching

QBS utilizes the Sketch synthesis engine to find a relational expression (a postcondition) equivalent to the imperative code. Instead of exhaustive search, it uses templates of SQL-like operations. The core insight is that the desired SQL query is effectively the postcondition of the imperative block—a description of the final state of the data.

Verification and SQL Generation

Once a candidate query is synthesized, QBS employs the Z3 theorem prover to formally verify its correctness across all possible inputs. This involves generating verification conditions for loop invariants and postconditions to prove semantic equivalence.

Verification conditions for loop invariants used to prove the correctness of synthesized queries.

Finally, the verified postcondition is translated into standard SQL and reintegrated into the application.

Evaluation

The researchers evaluated QBS on 49 code fragments from two large open-source projects: Wilos (a project management app) and itracker (an issue management system). These benchmarks represent real-world Hibernate usage, including complex data processing patterns like nested loops and conditional aggregations.

Results and Discussion

QBS demonstrated significant improvements over prior state-of-the-art techniques:

  • Superior Coverage: QBS successfully transformed 40 out of 49 benchmarks, whereas previous access-path analysis methods handled only 14.
  • Advanced Idioms: It is the first tool to automatically extract joins and aggregates (e.g., SUM, MAX) from general imperative loops.
  • Performance: While synthesis can take time (up to 40 minutes for complex joins), it is a one-time compile-time cost. The resulting SQL queries often provide orders-of-magnitude speedups during runtime by reducing data transfer and leveraging database-side indexing.

Conclusion

QBS provides a robust solution to the impedance mismatch by leveraging program synthesis to infer SQL queries from imperative code. By incorporating order into its formal theory and using automated verification, it ensures both performance and correctness. Future work aims to extend QBS to support data modification operations (INSERT, UPDATE) and more complex language features like polymorphism.

Summary updated

BibTeX
@misc{cheung2012inferring,
  author = {Alvin Cheung and Armando Solar-Lezama and Samuel Madden},
  title = {{Inferring SQL Queries Using Program Synthesis}},
  year = {2012},
  eprint = {1208.2013},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1208.2013},
}
Figure