Text-to-SQL is the task of translating natural language queries into executable SQL for a given database, enabling non-expert users to access structured data without writing SQL manually. Despite rapid advances driven by large language models (LLMs), existing approaches still struggle with complex queries in real-world settings, where database schemas are large and questions require multi-step reasoning over many interrelated tables. In such cases, providing the full schema often exceeds the context window, while one-shot generation frequently produces non-executable SQL due to syntax errors and incorrect schema linking. To address these challenges, we introduce AV-SQL, a framework that decomposes complex Text-to-SQL into a pipeline of specialized LLM agents. Central to AV-SQL is the concept of agentic views: agent-generated Common Table Expressions (CTEs) that encapsulate intermediate query logic and filter relevant schema elements from large schemas. AV-SQL operates in three stages: (1) a rewriter agent compresses and clarifies the input query; (2) a view generator agent processes schema chunks to produce agentic views; and (3) a planner, generator, and revisor agent collaboratively compose these views into the final SQL query. Extensive experiments show that AV-SQL achieves 70.38% execution accuracy on the challenging Spider 2.0 benchmark, outperforming state-of-the-art baselines, while remaining competitive on standard datasets with 85.59% on Spider, 72.16% on BIRD and 63.78% on KaggleDBQA. Our source code is available at https://github.com/pminhtam/AV-SQL.
翻译:文本转SQL任务旨在将自然语言查询转换为针对给定数据库的可执行SQL语句,使非专业用户无需手动编写SQL即可访问结构化数据。尽管基于大型语言模型的方案推动了该领域的快速发展,但现有方法在处理真实场景中的复杂查询时仍面临挑战——这些场景的数据库模式庞大,且问题需要跨多张关联表进行多步推理。在此情形下,提供完整模式常超出上下文窗口限制,而单次生成则因语法错误和错误模式链接导致SQL不可执行。为应对这些挑战,我们提出AV-SQL框架,该框架将复杂文本转SQL任务分解为专业化LLM智能体流水线。AV-SQL的核心是智能视图概念:由智能体生成的公共表表达式,用于封装中间查询逻辑并从大型模式中过滤相关模式元素。AV-SQL分三阶段运作:(1)重写智能体压缩并澄清输入查询;(2)视图生成智能体处理模式分块以生成智能视图;(3)规划器、生成器和修正智能体协作将这些视图组合为最终SQL查询。大量实验表明,AV-SQL在具有挑战性的Spider 2.0基准上达到70.38%的执行准确率,超越现有最优基线方法,同时在标准数据集上保持竞争力:Spider为85.59%,BIRD为72.16%,KaggleDBQA为63.78%。我们的源代码开源地址为https://github.com/pminhtam/AV-SQL。