SQL-Exchange: Recycling Query Logic Across the Database Universe
TL;DR
SQL-Exchange is a novel framework that uses Large Language Models (LLMs) to "translate" SQL queries from one database schema to another while preserving their underlying logical structure. By turning source queries into abstract templates and grounding them with target-side data, it enables high-quality data augmentation and in-context learning for text-to-SQL systems in low-resource domains.
What Problem Does This Paper Address?
Building a text-to-SQL system (which lets users ask questions in plain English to get data) usually requires thousands of hand-labeled pairs of natural language questions and their corresponding SQL queries. While massive benchmarks like BIRD and SPIDER exist, they don't help much when you're working with a brand-new, niche database that has zero training data.
The authors observed that while the content of databases varies (e.g., one is about Formula 1 and another is about toxicology), the structure of the questions we ask is often identical. However, simply asking an LLM to "rewrite this query for a different database" (zero-shot translation) fails miserably. The models suffer from structural drift (simplifying complex joins into basic queries) and schema leakage (accidentally copying table or column names from the old database into the new one).
Methods
SQL-Exchange introduces a multi-step pipeline to solve these issues:
- Template-Guided Query Transfer: Instead of direct translation, the framework first converts the source SQL into a "schema-agnostic template." For example,
SELECT T1.name FROM students AS T1becomesSELECT T1.column FROM table1 AS T1. This forces the model to focus on the relational logic rather than specific names. - Structured Prompting with CoT: The system uses Chain-of-Thought (CoT) reasoning, requiring the LLM to explain its mapping plan before generating the final SQL and natural language question.
- Sample-Grounded Substitution: To ensure the new query actually works and makes sense, the prompt includes real sample rows from the target database. This helps the LLM pick valid constants (like a specific "District Name") rather than hallucinating values.
- Semantic & Execution Filtering: The framework validates the generated pairs by checking if the SQL actually executes on the target database and if the natural language question is meaningful.
Datasets
The authors evaluated SQL-Exchange using the two most prominent text-to-SQL benchmarks:
- BIRD: A large-scale benchmark focusing on massive, real-world databases with complex values.
- SPIDER: A cross-domain benchmark known for its structural complexity.
They used these to create a new corpus of over 100,000 schema-aligned SQL-NL pairs, demonstrating how logic can be transferred from these "source" benchmarks to "target" development databases.
Key Insights and Ideas
- Logic is Portable, Names are Not: The "skeleton" of a query (the joins, filters, and aggregations) is a universal language. SQL-Exchange treats the source query as a structural guide rather than a literal script.
- The "Deletions" Trend: When LLMs fail at mapping, they tend to simplify. The authors found that zero-shot models frequently delete
JOINandWHEREclauses, losing the nuance of the original query. SQL-Exchange's template approach successfully "locks in" these structural elements. - In-Context Learning Power: One of the most effective ways to use these mapped queries is as "few-shot" examples. By showing a model a few examples of complex queries specifically tailored to the target schema, performance on the actual task sky-rockets compared to using generic examples.
Key Results
- Structural Fidelity: On the BIRD benchmark, structural alignment improved from a measly 13.1% (zero-shot) to 66.6% using SQL-Exchange with Gemini-1.5-flash.
- Execution Success: Mapped queries achieved execution validity rates as high as 93.5%.
- Downstream Gains: Using SQL-Exchange examples for in-context learning boosted the execution accuracy of smaller models (like Llama-3B) by up to 19 percentage points on the SPIDER benchmark.
- Fine-Tuning Superiority: Fine-tuning a model on 1.3K SQL-Exchange generated pairs was significantly more effective than fine-tuning on the original source data, nearly doubling accuracy in some low-resource scenarios (e.g., from 8.28% to 22.07% on the toxicology dataset).
Limitations and Future Work
- Schema Incompatibility: If the target schema is fundamentally simpler than the source (e.g., it lacks the necessary foreign keys to support a complex join), the mapping is impossible. The model currently handles this by returning a null output, but more graceful "partial" mappings could be explored.
- Semantic Nuance: While the SQL might be structurally perfect, the resulting natural language question can sometimes feel "robotic" or slightly awkward in the new domain.
- Validation Overhead: The framework relies on having a working database instance to verify execution validity, which might not always be available during the early stages of database design.
Future work could involve "schema-aware" selection, where the system automatically finds the best source database to "borrow" logic from based on the target's specific constraints.
Summary updated