Selective Demonstrations for Cross-domain Text-to-SQL

Shuaichen Chang, Eric Fosler-Lussier

Introduction

Translating natural language questions (NLQ) into SQL queries is a critical task for democratizing data access. The primary challenge in this field is cross-domain text-to-SQL, where models must generalize to databases they have never encountered during training. While Large Language Models (LLMs) like GPT-4 have shown promise, their performance is significantly enhanced by "in-domain" examples—NLQ-SQL pairs from the specific database being queried.

However, creating these examples typically requires manual annotation by SQL experts for every new database, a process that is costly and difficult to scale. Researchers from The Ohio State University have introduced ODIS (Out-of-domain Demonstrations and In-domain Synthetic data), a framework designed to bridge this gap by providing the benefits of in-domain demonstrations without the need for human labor.

The ODIS Framework

ODIS utilizes a two-pronged strategy to construct optimal prompts for LLMs, combining retrieved out-of-domain (OOD) examples with automatically generated in-domain (ID) synthetic data.

1. Out-of-Domain Retrieval (SimSQL)

To provide the model with a general understanding of SQL logic and formatting, ODIS retrieves examples from external databases. Rather than selecting random examples, it employs a strategy called SimSQL. The framework first prompts the LLM to generate a "zero-shot" guess of the SQL for the target question. It then retrieves OOD examples that share a similar SQL structure with this initial guess, ensuring the demonstrations are relevant to the logic required for the current task.

2. In-Domain Synthetic Generation

To familiarize the model with the specific schema and quirks of the target database, ODIS generates synthetic data. The process involves:

  • Template Sampling: Sampling SQL templates and filling them with the target database's schema.
  • Question Generation: Using an LLM to write natural language questions corresponding to the generated SQL.
  • Back-Translation Filtering: To ensure quality, the LLM attempts to translate the synthetic question back into the original SQL. If the translation does not match, the example is discarded as unreliable.

3. Maximum Coverage Selection (CovSQL)

From the pool of validated synthetic data, ODIS selects the most informative examples using a greedy algorithm called CovSQL. Instead of looking for examples similar to the user's question, CovSQL prioritizes diversity, selecting examples that cover the widest possible range of SQL keywords and database columns. This "primes" the model on the entire database schema.

Experimental Results

The researchers evaluated ODIS against industry-standard benchmarks, including Spider (200 databases), KaggleDBQA (complex, real-world databases), and Dr. Spider (a robustness benchmark).

Performance Highlights

  • KaggleDBQA: ODIS achieved a massive 11.8-point accuracy improvement over the previous state-of-the-art (SKILL-KNN), reaching 54.8% accuracy. This demonstrates the framework's effectiveness on complex, poorly documented real-world data.
  • Spider: Using Codex, ODIS reached 85.2% execution accuracy, surpassing the previous best (Self-Debugging) by 1.1 points.
  • Robustness: On Dr. Spider, the framework maintained high performance despite perturbed questions or synonyms, indicating that the model learns structural logic rather than simple template matching.
  • Model Agnostic: While results were strongest on Codex, ODIS also improved performance for ChatGPT and open-source models like CodeLlama.

Key Insights

The study yielded several critical insights into how LLMs process SQL tasks:

  • SQL Structure is Paramount: Ablation studies revealed that the most important element of an in-domain example is the SQL structure rather than the natural language question. Providing the model with valid SQL patterns for a specific schema is the primary driver of performance gains.
  • Synthetic Data Sufficiency: Even if synthetic questions are slightly robotic or imperfect, they provide enough structural context for the LLM to generalize effectively to human-written questions.
  • Diversity Over Similarity: For synthetic demonstrations, covering the breadth of the schema (Maximum Coverage) is more effective than trying to find a perfect match for the target question.

Limitations and Future Work

Despite its success, ODIS has limitations. Its retrieval accuracy depends on the quality of the initial "zero-shot" guess; if the first guess is fundamentally flawed, the retrieved examples may be less helpful. Additionally, generating and filtering synthetic data for a new database requires an offline computational overhead before the system is ready for real-time use.

Future research directions include developing a unified retrieval strategy that treats OOD and synthetic data as a single hybrid pool and exploring the combination of ODIS with parameter-efficient fine-tuning (PEFT) to further refine model performance.

Summary updated

BibTeX
@inproceedings{chang2023selective,
  author = {Shuaichen Chang and Eric Fosler-Lussier},
  title = {{Selective Demonstrations for Cross-domain Text-to-SQL}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  doi = {10.18653/v1/2023.findings-emnlp.944},
}
Figure