Introduction
Text-to-SQL models have made significant strides in translating natural language (NL) questions into executable database queries. However, a persistent challenge remains: while the correct query is often present within the top-N results generated by a model, it frequently fails to appear as the top-ranked candidate. For instance, top-tier models often exhibit a significant performance gap (up to 7.7%) between their top-1 and top-10 accuracy. This paper introduces an automated re-ranking method that leverages Large Language Models (LLMs) to bridge this gap by generating synthetic test cases.
Methodology: Automated Test Case Generation
The core innovation lies in treating SQL selection as a software testing problem. In traditional software engineering, code is validated against test cases. In Text-to-SQL, however, the "ground truth" execution result is unknown at inference time. The authors address this by introducing a three-step framework:
- Candidate Generation: A base model produces a list of potential SQL queries.
- Test Case Synthesis: The system generates a "mini-database" and uses an LLM to predict the expected execution result for the given NL question.
- Execution-Based Re-ranking: Candidate queries are executed against the mini-database. The query that most consistently matches the LLM's predicted result is promoted to the top rank.
To ensure the LLM can accurately predict results, the authors employ two key strategies:
- Data Sampling: Using "Random Selection" from the original database ensures the data remains "natural," which improves LLM reasoning compared to random "Fuzzing."
- Complexity Reduction: By limiting the table size to a Maximum Table Size (MTS) of 5 rows and programmatically replacing large numbers with small integers (1–10), the authors reduce the cognitive load on the LLM, turning complex arithmetic into simple "mental execution."
Key Insights and Results
The study reveals that while LLMs may struggle to write complex SQL from scratch, they are remarkably adept at acting as "mental executors" when provided with small, simplified datasets. The researchers found that "naturalness" in the generated data is crucial; LLMs perform better when the data resembles real-world entries rather than synthetic noise.
Evaluation on the Spider benchmark demonstrated consistent improvements across different base models:
- DAIL-SQL saw an execution accuracy boost of 3.6%.
- RESDSQL improved by 2.0%.
Furthermore, the choice of LLM for result prediction is critical. GPT-4-turbo significantly outperformed GPT-3.5-turbo, highlighting the importance of strong reasoning capabilities in the test-case generation phase.
Limitations and Future Outlook
Despite the performance gains, the method introduces trade-offs in terms of cost and latency, requiring approximately ten additional LLM calls per question. Additionally, the accuracy of the re-ranker is bound by the accuracy of the LLM's result prediction, which currently stands at roughly 60%.
Future research may explore "active" database generation—creating specific data points designed to differentiate between subtly different SQL candidates—and methods to reduce the computational overhead of the verification process.
Summary updated