Abstract
Translating natural language into SQL (Text-to-SQL) is a critical component of modern business intelligence, yet it remains challenging due to massive database schemas and the difficulty of selecting structurally relevant few-shot examples. This paper introduces ASTReS, a retrieval-augmented generation (RAG) framework that utilizes Abstract Syntax Trees (ASTs) to rank and select few-shot examples based on structural logic rather than linguistic similarity. By combining this with a lightweight, parallelized approximator for dynamic schema pruning, ASTReS achieves state-of-the-art performance on the Spider benchmark while significantly reducing computational overhead.
1. Introduction
Large Language Models (LLMs) have demonstrated impressive capabilities in Text-to-SQL tasks, particularly when provided with few-shot examples. However, two major hurdles persist in production environments: Schema Bloat and Poor Example Selection. Real-world databases often contain hundreds of tables, exceeding the context window of most LLMs. Furthermore, current retrieval methods often rely on simple question similarity or "SQL skeletons," which can be misleading.
As shown in Figure 1, two queries with vastly different logic can share the same skeleton, causing the LLM to follow an incorrect structural pattern. ASTReS addresses these issues by focusing on the hierarchical logic captured in SQL ASTs.
2. Methodology
The ASTReS framework employs a multi-stage pipeline to optimize the prompt sent to the LLM:
- AST-based Ranking: The system uses a small "approximator" model to generate an initial draft of the SQL query. It then retrieves few-shot examples whose SQL Abstract Syntax Trees (ASTs) are most similar to this draft. ASTs capture nested joins and specific filters more accurately than flat text.
- Dynamic Schema Pruning: To manage large databases, ASTReS identifies necessary tables and columns based on the approximator's draft and a BM25 search. This creates a concise "sub-schema," filtering out irrelevant noise.
- FastRAText Approximator: The core of the retrieval process is a specialized, decoder-free model (<500M parameters). A novel "schema splitting" strategy allows this model to process large schemas in parallel, ensuring high efficiency.
3. Experimental Evaluation
The framework was evaluated on the SPIDER benchmark and its robustness variants: SPIDER-DK (domain knowledge), SPIDER-REAL (realistic questions), SPIDER-SYN (synonyms), and CSPIDER (Chinese-language).
4. Results and Discussion
ASTReS establishes a new state-of-the-art for prompt-based Text-to-SQL. On the Spider benchmark, ASTReS paired with GPT-4 achieved an Execution Accuracy (EX) of 86.6%, surpassing the previous benchmark of 83.6% set by DAIL-SQL.
Key insights from the evaluation include:
- Efficiency: The FastRAText approximator is over 600 times faster than larger alternatives like Graphix-T5 while maintaining comparable accuracy.
- Schema Reduction: The pruning strategy successfully excludes over 70% of irrelevant schema elements, directly reducing API costs and inference latency.
- Structural Logic: Normalizing ASTs to resolve aliases and sort JOIN nodes allows the system to find examples that demonstrate the true logic required for the target query.
5. Conclusion
ASTReS demonstrates that structural similarity is a more effective teacher for LLMs than linguistic similarity in Text-to-SQL tasks. By using small models as efficient "scouts" to prune search spaces and select high-quality examples, the framework achieves superior accuracy and efficiency. Future work will focus on improving approximator reliability and exploring diverse example selection to further enhance generalization.
Summary updated