PURPLE: Making a Large Language Model a Better SQL Writer

Tonghui Ren, Yuankai Fan, Zhenying He, Ren Huang, Jiaqi Dai, Can Huang, Yinan Jing, Kai Zhang, Yifan Yang, X. Sean Wang

Abstract

While Large Language Models (LLMs) have demonstrated remarkable proficiency in natural language understanding, translating complex user intent into semantically correct SQL remains a significant challenge. Current methods often rely on keyword-based retrieval for few-shot prompting, which fails to capture the underlying logical composition required for complex queries. This paper introduces PURPLE (Pre-trained models Utilized to Retrieve Prompts for Logical Enhancement), a framework that prioritizes logical structure over lexical similarity. By utilizing a multi-level automaton for demonstration selection and a Steiner Tree algorithm for schema pruning, PURPLE achieves a new state-of-the-art Exact-Set Match accuracy of 80.5% on the Spider benchmark while maintaining high token efficiency.

Introduction

The task of Natural Language to SQL (NL2SQL) is critical for democratizing data access. Despite the success of models like GPT-4, a performance gap persists in handling complex logical operators, such as nested subqueries, joins, and set operations. A primary reason for this is the reliance on "Execution Match" (EX) as a success metric. EX can be misleading, as different SQL queries may produce the same result by chance, leading to false positives. In contrast, "Exact-Set Match" (EM) requires the SQL structure to be semantically and logically correct.

Existing few-shot prompting techniques typically select demonstrations based on keyword overlap between the natural language query and the database schema. However, for complex SQL, the logical "skeleton" of the query—how clauses and operators are composed—is more important than the specific entities involved. PURPLE addresses this by focusing on logical retrieval and efficient schema representation.

The PURPLE Framework

PURPLE introduces a four-stage pipeline designed to provide LLMs with the precise logical context needed to generate complex SQL.

1. Schema Pruning via Steiner Tree

Feeding an entire database schema into an LLM is often counterproductive, as it introduces noise and consumes excessive tokens. PURPLE employs a schema pruning module that uses a classifier to identify relevant tables and columns. To ensure the selected components can actually form a valid query, the framework applies a Steiner Tree algorithm. This algorithm identifies the smallest connected subgraph in the database schema that links all required elements, preventing the LLM from hallucinating impossible joins.

2. Skeleton Prediction

To bridge the gap between natural language and SQL logic, PURPLE uses a smaller, fine-tuned model (T5-3B) to predict a "SQL Skeleton." This skeleton replaces specific table and column names with placeholders (e.g., SELECT _ FROM _ WHERE _ = _). This abstraction allows the system to focus on the structural requirements of the query without being distracted by domain-specific terminology.

3. Automaton-based Demonstration Selection

The core innovation of PURPLE is its method for retrieving few-shot demonstrations. Rather than matching keywords, PURPLE models SQL logic using a four-level automaton abstraction:

  • Detail-Level: Requires an exact structural match.
  • Keywords-Level: Focuses on the specific SQL keywords used.
  • Structure-Level: Generalizes operators (e.g., grouping all comparison operators like > or <= into a single <CMP> tag).
  • Clause-Level: Provides a high-level abstraction of the main SQL clauses (e.g., SELECT, FROM, GROUP BY).

By traversing these levels, PURPLE can find demonstrations in its library that share the same logical "DNA" as the target query. If an exact structural match is not found, the system "zooms out" to a more abstract level to find the most relevant logical analogue.

4. Database Adaption

Following SQL generation by the LLM, a post-processing module ensures the output is compatible with the target database. This includes fixing table-column mismatches and adapting SQL functions to the specific dialect (such as SQLite), further reducing errors caused by model hallucinations.

Evaluation and Results

PURPLE was evaluated on the Spider benchmark and its variants (Spider-DK, Spider-SYN, and Spider-Realistic).

Performance

Using GPT-4, PURPLE achieved a new state-of-the-art 80.5% Exact-Set Match (EM) and 87.8% Execution Match (EX) on the Spider validation set. This represents an 11.8% improvement in EM accuracy over previous leading few-shot methods like DAIL-SQL. Notably, when paired with GPT-3.5, PURPLE still outperformed several GPT-4 based baselines, demonstrating that superior prompt retrieval can effectively compensate for smaller model parameters.

Robustness and Efficiency

The framework maintained high performance across all Spider variants, proving its resilience against domain-specific knowledge requirements and synonym replacements. Furthermore, PURPLE is highly token-efficient. It achieves its SOTA results using approximately 1,250 tokens per query, compared to the ~10,000 tokens required by other high-performing methods like DIN-SQL. This makes PURPLE significantly more cost-effective for production environments.

Conclusion

PURPLE demonstrates that for the task of NL2SQL, the logical composition of a query is a more effective signal for prompt retrieval than lexical similarity. By abstracting SQL into a multi-level automaton and ensuring schema connectivity through Steiner Trees, PURPLE enables LLMs to navigate the complexities of relational databases with unprecedented accuracy. Future work will explore generative prompting, where models synthesize custom demonstrations tailored to specific logical requirements, further reducing the dependency on fixed demonstration libraries.

Summary updated

BibTeX
@inproceedings{ren2024purple,
  author = {Tonghui Ren and Yuankai Fan and Zhenying He and Ren Huang and Jiaqi Dai and Can Huang and Yinan Jing and Kai Zhang and Yifan Yang and X. Sean Wang},
  title = {{PURPLE: Making a Large Language Model a Better SQL Writer}},
  booktitle = {ICDE},
  year = {2024},
  doi = {10.1109/icde60146.2024.00009},
}
Figure