一句话:结合代码索引图与 LLM 智能体混合上下文检索,从 Go ORM 命令式代码中还原 SQL 模板,召回率达 68%–72%。
次优 SQL 查询会显著降低云系统性能,因此需要在部署前抽取并审计 SQL 语句。但在 Go 中,ORM 框架通过分散的方法调用序列以命令式方式构造 SQL,使得静态还原最终的 SQL 模板十分困难。本文提出 AutoSQL:它首先构建 Code Index——一张以可导航边刻画函数、类型与全局变量之间结构依赖的有向图;随后从 ORM 调用点向上追溯调用链,识别与数据库交互的函数作为入口点;对每个入口点,由 LLM 智能体沿 Code Index 收集影响 SQL 生成的代码切片,当图无法解析检索目标时切换到基于模式的搜索,该策略称为混合上下文检索;收集到足够上下文后,智能体合成 SQL 模板。在来自 5 个大型 Go 仓库、包含 579 个有测试覆盖的入口点与 1186 条运行时追踪 SQL 语句的基准上,AutoSQL 取得 68.04% 至 72.18% 的召回率,分别高出静态可达性基线 11.80%–15.94% 与现有方法 8.52%–21.50%。
Suboptimal SQL queries can significantly degrade the performance of cloud systems, motivating the extraction and auditing of SQL statements before deployment. However, Go ORM frameworks construct SQL imperatively through scattered method-call sequences, making it difficult to statically recover the resulting SQL templates. We present AutoSQL, a system that reconstructs SQL templates from Go ORM code. AutoSQL constructs a Code Index, a directed graph that captures structural dependencies between functions, types, and global variables as navigable edges. It then traces upstream call chains from ORM invocation sites to identify database-interacting functions as entry points. For each entry point, an LLM agent traverses the Code Index to collect code slices that influence SQL generation, switching to pattern-based search when the graph cannot resolve a retrieval goal. We call this strategy Hybrid Context Retrieval. Once sufficient context is collected, the agent synthesizes SQL templates. Evaluation on a benchmark of 579 test-covered entry points and 1,186 runtime-traced SQL statements from five large-scale Go repositories shows that AutoSQL achieves 68.04% to 72.18% recall, exceeding the static reachability baseline by 11.80% to 15.94% and outperforming existing methods by 8.52% to 21.50%.
@inproceedings{paperbot12539,
title = {AutoSQL: Extracting SQL Templates from Imperative ORM Code in Large-Scale Repositories},
author = {Junsong Pu and Yichen Li and Zhuangbin Chen and Zhihan Jiang and Zibin Zheng},
booktitle = {ASE},
year = {2026},
doi = {10.1145/3832783.3837420}
}