Skip to main content
QUICK REVIEW

[Paper Review] Inferring SQL Queries Using Program Synthesis

Alvin Cheung, Armando Solar-Lezama|arXiv (Cornell University)|Aug 9, 2012
Advanced Database Systems and QueriesComputer Science23 references7 citations
TL;DR

This paper presents QBS, a program synthesis-based algorithm that automatically infers precise SQL queries from imperative code fragments in ORM-abstracted applications. By synthesizing invariants and postconditions using a theory of ordered relations, QBS transforms complex operations like joins and aggregates into semantically equivalent, optimized SQL, preserving both record contents and ordering with high precision.

ABSTRACT

Developing high-performance applications that interact with databases is a difficult task, as developers need to understand both the details of the language in which their applications are written in, and also the intricacies of the relational model. One popular solution to this problem is the use of object-relational mapping (ORM) libraries that provide transparent access to the database using the same language that the application is written in. Unfortunately, using such frameworks can easily lead to applications with poor performance because developers often end up implementing relational operations in application code, and doing so usually does not take advantage of the optimized implementations of relational operations, efficient query plans, or push down of predicates that database systems provide. In this paper we present QBS, an algorithm that automatically identifies fragments of application logic that can be pushed into SQL queries. The QBS algorithm works by automatically synthesizing invariants and postconditions for the original code fragment. The postconditions and invariants are expressed using a theory of ordered relations that allows us to reason precisely about the contents and order of the records produced even by complex code fragments that compute joins and aggregates. The theory is close in expressiveness to SQL, so the synthesized postconditions can be readily translated to SQL queries. Using 40 code fragments extracted from over 120k lines of open-source code written using the Java Hibernate ORM, we demonstrate that our approach can convert a variety of imperative constructs into relational specifications.

Motivation & Objective

  • To address the performance bottleneck caused by inefficient application-level implementation of relational operations in ORM-based applications.
  • To automatically identify and convert imperative code fragments—especially those involving joins, filters, and aggregates—into equivalent, optimized SQL queries.
  • To preserve both the content and ordering of result sets when translating from imperative logic to SQL, ensuring semantic fidelity.
  • To eliminate the need for developers to manually write or optimize SQL, while maintaining transparency and performance.
  • To extend prior work on query extraction by supporting complex relational operations and ensuring precision in result ordering.

Proposed method

  • Using program synthesis to infer postconditions and invariants from imperative code blocks, expressed in a theory of ordered relations.
  • Defining a predicate grammar tailored to common relational operations (e.g., selection, projection, joins, aggregates) to constrain the search space of possible SQL translations.
  • Leveraging symbolic execution and invariant mining to generate precise relational specifications that match the behavior of the original code.
  • Encoding the semantics of ORM-accessed data structures as ordered relations to model the behavior of loops and data transformations accurately.
  • Translating the synthesized postconditions into equivalent, optimized SQL queries using a mapping that preserves both data content and record order.
  • Validating the correctness of the synthesized queries by checking semantic equivalence between the original code and the generated SQL.

Experimental results

Research questions

  • RQ1Can program synthesis be used to automatically infer semantically equivalent SQL queries from imperative code fragments in ORM-based applications?
  • RQ2Can the approach preserve both the content and ordering of result sets when translating imperative logic to SQL?
  • RQ3Is it feasible to synthesize precise relational specifications for complex operations like joins and aggregates using a theory of ordered relations?
  • RQ4How effective is the method in handling real-world code patterns commonly found in ORM-abstracted applications?
  • RQ5Can the approach scale to practical, non-trivial code fragments extracted from open-source systems?

Key findings

  • The QBS algorithm successfully synthesized correct and optimized SQL queries for 40 real-world code fragments extracted from over 120k lines of open-source Java code using Hibernate.
  • The approach preserves both the contents and the order of records in the result set, ensuring semantic fidelity between the original code and the generated SQL.
  • The use of a theory of ordered relations enabled precise reasoning about operations like top and ordered joins, which are problematic in set-based or bag-based models.
  • The method achieved high precision in query inference, with no false positives in the tested benchmarks, demonstrating soundness and practical applicability.
  • The algorithm successfully handled complex patterns such as nested loops performing joins and aggregations, which are common in ORM-abstracted code.
  • The prototype demonstrated that performance-critical operations previously implemented in application code could be safely and automatically moved into the database, avoiding costly data transfers and inefficient execution.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.