让数据库主动指导LLM修正错误,提升文本转SQL的准确率和效率。
SafeQL: Search-based Refinement for Safe and Efficient LLM-based Text-to-SQL

- 基于数据库反馈进行增量式修复,不重生成整个查询
- 在安全查询空间中搜索,执行成功率提升至91.3%(Bird)
- 适合需要高可靠性的智能数据库交互场景
大语言模型(LLMs)通过自然语言接口实现文本到SQL转换,无需任务特定微调。然而现有系统仍不可靠,常生成与数据库模式不符的无效查询,如引用不存在的表、属性、函数或值。这类错误持续存在,因与数据库管理系统(DBMS)的交互仅限于错误消息,使其在查询修正中处于被动地位。本文提出SafeQL,一种将DBMS重新定义为修正过程中的主动引导者的搜索式精炼范式。不同于失败后重生成整条查询,SafeQL解析DBMS反馈,仅对出错部分进行渐进式修复。每次修正步骤均在安全查询空间内进行有指导的搜索,候选查询通过DBMS执行逐步验证,从而收敛到可执行查询,并避免重复生成错误。在Bird和Spider基准上的实验表明,相比再生式方法,SafeQL显著提升了执行准确率与效率。
原文摘要 · Abstract (English)
Large language models (LLMs) have advanced Text-to-SQL by enabling natural language interfaces to databases without task-specific fine-tuning. However, existing LLM-based systems remain unreliable, often generating SQL queries that are invalid under the database schema, referencing non-existent tables, attributes, functions, or values. Such errors persist because interactions with the database management system (DBMS) are typically limited to error messages, leaving it in a largely passive role during query refinement. This paper proposes SafeQL, \textit{a search-based refinement paradigm that redefines the role of the DBMS as an active guide in the refinement process}. Instead of regenerating entire queries after execution failure, SafeQL interprets DBMS feedback to incrementally repair only the erroneous components. Each refinement step is formulated as a guided search within a \textit{safe query space}, where candidate queries are progressively validated through DBMS execution, thereby converging to an executable query and preventing repeated regeneration of errors. Experiments on the Bird and Spider benchmarks show that SafeQL significantly improves execution accuracy and efficiency compared to regeneration-based methods.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。