ReFSQL: A Retrieval-Augmentation Framework for Text-to-SQL Generation

Kun Zhang, Xiexiong Lin, Yuanzhuo Wang, Xin Zhang, Fei Sun, Jianhe Cen, Hexiang Tan, Xuhui Jiang, Huawei ShenEMNLP 2023

Abstract

Text-to-SQL is the task that aims at translating natural language questions into SQL queries. Existing methods directly align the natural language with SQL Language and train one encoder-decoder-based model to fit all questions. However, they underestimate the inherent structural characteristics of SQL, as well as the gap between specific structure knowledge and general knowledge. This leads to structure errors in the generated SQL. To address the above challenges, we propose a retrieval-argument framework, namely ReFSQL. It contains two parts, structure-enhanced retriever and the generator. Structure-enhanced retriever is designed to identify samples with comparable specific knowledge in an unsupervised way. Subsequently, we incorporate the retrieved samples’ SQL into the input, enabling the model to acquire prior knowledge of similar SQL grammar. To further bridge the gap between specific and general knowledge, we present a mahalanobis contrastive learning method, which facilitates the transfer of the sample toward the specific knowledge distribution constructed by the retrieved samples. Experimental results on five datasets verify the effectiveness of our approach in improving the accuracy and robustness of Text-to-SQL generation. Our framework has achieved improved performance when combined with many other backbone models (including the 11B flan-T5) and also achieved state-of-the-art performance when compared to existing methods that employ the fine-tuning approach.

Introduction

Translating a natural language question (e.g., "Which singers are from the USA?") into a SQL query is notoriously difficult. While large language models (LLMs) are proficient at general reasoning, they often struggle with the structural constraints of SQL. Common errors include missing JOIN operations, selecting incorrect columns, or failing to follow the specific logic of a complex database schema. The authors of ReFSQL argue that standard models underestimate the "structural gap" between fluid natural language and the rigid, schema-dependent structure of SQL. Existing "one-size-fits-all" models often fail to adapt to the specific "local" knowledge required for a particular, unseen database.

Methodology

ReFSQL addresses these challenges using a two-part architecture: a Structure-enhanced Retriever and a Generator.

The ReFSQL architecture featuring the Structure-enhanced Retriever and the Generator with Mahalanobis Contrastive Learning.

Structure-enhanced Retriever

Instead of searching for semantic similarity alone, this retriever identifies structural parallels:

  • SQLSE (SQL-Structure-enhanced Question Retriever): Utilizes Tree-Edit-Distance (TED) on the SQL queries of training samples to find questions that result in similar query "shapes."
  • LSES (Linking-Structure-based Schema Retriever): Constructs an "Interaction Graph" using a Relational Graph Convolutional Network (R-GCN) to model how question tokens link to specific tables and columns.

The Generator and Mahalanobis Contrastive Learning

The retrieved SQL queries are appended to the input as a prompt, providing the model with a structural "blueprint" of the target grammar. To further bridge the knowledge gap, the framework employs Mahalanobis Contrastive Learning (MCL). Unlike standard contrastive learning that pulls individual points together in vector space, MCL treats retrieved similar samples as a distribution. It uses the Mahalanobis distance to "pull" the current sample's representation toward the distribution of correct structural knowledge, making the model more robust to schema variations.

Experimental Results

ReFSQL was evaluated on several high-stakes benchmarks, including Spider (the gold standard for cross-domain, multi-table Text-to-SQL) and WikiSQL (large-scale single-table queries). To test real-world robustness, the authors also utilized Spider-DK, Spider-Syn, and Spider-Realistic.

Performance comparison of ReFSQL against other state-of-the-art models on the Spider and WikiSQL benchmarks.

Key Findings

  • State-of-the-Art Performance: On the Spider Dev set, ReFSQL achieved 86.6% Exact Match (EM) and 88.1% Execution Accuracy (EX) using a Flan-T5 backbone, significantly outperforming previous SOTA models like RESDSQL.
  • Superiority over General LLMs: ReFSQL's 88.1% EX on Spider significantly outperformed ChatGPT (70.1%), demonstrating that specialized fine-tuning with retrieval remains superior to general-purpose zero-shot LLMs for structured tasks.
  • Scalability: The framework provides massive gains even for smaller models; for instance, T5-small saw substantial improvements when augmented with ReFSQL.
  • Robustness: On the "Spider-Realistic" dataset, ReFSQL improved the baseline by nearly 3%, proving it is less susceptible to confusion from ambiguous user questions.

Conclusion

ReFSQL demonstrates that providing models with structural priors via retrieval is more effective than simply increasing parameter counts. By treating structural similarity as a distribution through Mahalanobis Contrastive Learning, the framework effectively separates general language understanding from specific structural execution. Future work will focus on reducing the computational overhead of the retrieval process and expanding the framework to support diverse linguistic structures beyond English.

Summary updated

BibTeX
@inproceedings{zhang2023refsql,
  author = {Kun Zhang and Xiexiong Lin and Yuanzhuo Wang and Xin Zhang and Fei Sun and Jianhe Cen and Hexiang Tan and Xuhui Jiang and Huawei Shen},
  title = {{ReFSQL: A Retrieval-Augmentation Framework for Text-to-SQL Generation}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  url = {https://openreview.net/forum?id=t0n1s07E2Z},
}
Figure