[论文解读] Inferring SQL Queries Using Program Synthesis
本文提出 QBS,一种基于程序综合的算法,可自动从 ORM 抽象应用中的命令式代码片段推断出精确的 SQL 查询。通过使用有序关系理论综合不变式和后置条件,QBS 将复杂的操作(如连接和聚合)转换为语义等价且经过优化的 SQL,以高精度保留记录内容和排序。
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.
研究动机与目标
- 解决 ORM 基础应用中由于关系操作在应用层实现效率低下而引起的性能瓶颈。
- 自动识别并转换涉及连接、过滤和聚合等操作的命令式代码片段,生成等价且优化的 SQL 查询。
- 在将命令式逻辑转换为 SQL 时,保留结果集的内容和排序,确保语义一致性。
- 消除开发者手动编写或优化 SQL 的需求,同时保持透明性和性能。
- 在先前查询提取工作的基础上,扩展支持复杂关系操作,并确保结果排序的精确性。
提出的方法
- 使用程序综合从命令式代码块中推断后置条件和不变式,表达在有序关系理论中。
- 定义一种针对常见关系操作(如选择、投影、连接、聚合)的谓词语法,以约束可能的 SQL 翻译搜索空间。
- 利用符号执行和不变式挖掘,生成与原始代码行为匹配的精确关系规范。
- 将 ORM 访问的数据结构语义编码为有序关系,以准确建模循环和数据转换的行为。
- 使用保留数据内容和记录顺序的映射,将合成的后置条件转换为等价且优化的 SQL 查询。
- 通过检查原始代码与生成 SQL 之间的语义等价性,验证合成查询的正确性。
实验结果
研究问题
- RQ1能否使用程序综合从 ORM 基础应用中的命令式代码片段自动推断出语义等价的 SQL 查询?
- RQ2该方法在将命令式逻辑转换为 SQL 时,能否同时保留结果集的内容和排序?
- RQ3能否利用有序关系理论,为连接和聚合等复杂操作合成精确的关系规范?
- RQ4该方法在处理 ORM 抽象应用中常见的实际代码模式时,效果如何?
- RQ5该方法能否扩展到从开源系统中提取的实际非平凡代码片段?
主要发现
- QBS 算法成功为从超过 12 万行开源 Java 代码(使用 Hibernate)中提取的 40 个真实世界代码片段,生成了正确且优化的 SQL 查询。
- 该方法在结果集中同时保留了记录内容和排序,确保了原始代码与生成 SQL 之间的语义一致性。
- 使用有序关系理论使得对 top 操作和有序连接等操作的精确推理成为可能,而这些操作在基于集合或多重集的模型中存在困难。
- 该方法在查询推断中实现了高精度,在测试基准中未出现误报,证明了其可靠性和实际适用性。
- 该算法成功处理了嵌套循环执行连接和聚合等复杂模式,这些模式在 ORM 抽象代码中非常常见。
- 原型验证表明,原本在应用代码中实现的性能关键操作可被安全且自动地移至数据库执行,避免了昂贵的数据传输和低效执行。
更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。