Analyzing the Effectiveness of Large Language Models on Text-to-SQL Synthesis

Ashutosh Trivedi, Richard Roberson, Gowtham Kaki, Ashutosh Trivedi 0001arXiv 2024 · DOI

Introduction

The task of Text-to-SQL synthesis—translating natural language questions into executable SQL queries—is a critical component in democratizing data access for non-technical users. While traditional approaches relied on specialized sequence-to-sequence architectures, the advent of Large Language Models (LLMs) has fundamentally shifted the paradigm. This paper investigates the performance of both open-source and closed-source LLMs on this task, proposing a multi-stage pipeline to maximize accuracy and identifying persistent challenges in the field.

Methodology

The research explores two primary avenues for model optimization:

  1. Open-Source Fine-Tuning: The authors utilized Quantized Low-Rank Adaptation (QLoRA) to fine-tune the WizardCoder-15B model. This approach, resulting in the "Spider Skeleton Wizard Coder," allows for efficient parameter updates while maintaining high performance.
  2. Closed-Source Pipeline: A more sophisticated pipeline was developed using OpenAI’s gpt-3.5-turbo-16k. This system incorporates a two-stage automated error correction loop. First, an Example Driven Correction stage compares the query's output against a "gold" result table. If discrepancies exist, the model is re-prompted. Second, an Error Driven Correction stage employs GPT-4-turbo to analyze and fix queries that fail to execute.

Evaluation was conducted using the Spider benchmark, a rigorous cross-domain dataset that tests a model's ability to generalize to unseen database schemas.

Performance Evaluation

The study demonstrates a clear progression in performance as pipeline complexity increases. While a fine-tuned WizardCoder achieved 61.0% accuracy, the full pipeline utilizing GPT-3.5 and GPT-4 correction reached a state-of-the-art 82.1% execution accuracy.

Table 1: Execution accuracy of the final pipeline across different difficulty levels on the Spider benchmark.

As shown in the table above, performance is highly dependent on query difficulty. The model achieves near-perfect results on "Easy" queries (94.0%) but sees a significant drop-off for "Extra Hard" queries (56.6%), which typically involve complex joins and multiple nested subqueries.

Qualitative Analysis and Limitations

Beyond raw metrics, the research uncovered several key insights into LLM behavior in Text-to-SQL tasks:

  • The "Skeleton" Strategy: Prompting models to generate a structural SQL outline (a "skeleton") before the full query significantly improves logical consistency.
  • Value Prediction Challenges: LLMs often struggle when queries require specific data values not present in the schema. This "Value Blind Spot" occurs because models are typically trained on schemas rather than full database contents.
  • Over-Engineering: A notable trend is the generation of overly complex queries. LLMs frequently resort to nested subqueries even when a simpler join would suffice.
Figure 1: An example of a generated SQL query utilizing a nested subquery.

The example above illustrates a query that, while functional, uses a subquery to calculate an average for comparison. Such structures can become difficult to maintain as complexity grows.

Furthermore, the authors identified significant inconsistencies within the Spider benchmark itself. In several instances, the LLM-generated query was logically sound or even superior to the "ground truth" provided by the dataset, suggesting that current evaluation metrics may underrepresent true model capability.

Conclusion

While LLMs have made remarkable strides in Text-to-SQL synthesis, reaching an 82.1% accuracy on the Spider benchmark, challenges remain. Future work should focus on improving "contextual awareness" of actual database values and refining evaluation scripts to better handle semantic equivalence rather than strict syntactic matching. Reducing the tendency for over-engineering will also be crucial for deploying these models in production environments.

Summary updated

Abstract

This study investigates various approaches to using Large Language Models (LLMs) for Text-to-SQL program synthesis, focusing on the outcomes and insights derived. Employing the popular Text-to-SQL dataset, spider, the goal was to input a natural language question along with the database schema and output the correct SQL SELECT query. The initial approach was to fine-tune a local and open-source model to generate the SELECT query. After QLoRa fine-tuning WizardLM's WizardCoder-15B model on the spider dataset, the execution accuracy for generated queries rose to a high of 61%. With the second approach, using the fine-tuned gpt-3.5-turbo-16k (Few-shot) + gpt-4-turbo (Zero-shot error correction), the execution accuracy reached a high of 82.1%. Of all the incorrect queries, most can be categorized into a seven different categories of what went wrong: selecting the wrong columns or wrong order of columns, grouping by the wrong column, predicting the wrong values in conditionals, using different aggregates than the ground truth, extra or too few JOIN clauses, inconsistencies in the Spider dataset, and lastly completely incorrect query structure. Most if not all of the queries fall into these categories and it is insightful to understanding where the faults still lie with LLM program synthesis and where they can be improved.

Empirical ResultsBrowse all benchmarks →

BenchmarkMethodMetricScoreProtocol / Evidence
Spider · development set (1,034 entriesSpider Skeleton Wizard Coder (QLoRA fine-tuned WizardCoder-15B)execution accuracy61%+3.4 vs baselinesplit: val · fine-tuned · params=15B · backbone=WizardCoder-15B · fine-tuning=QLoRA
Source quote
L15: For our first set of experiments, we chose to fine-tune WizardLM's WizardCoder, a 15B parameter model renowned for its proficiency in programming tasks \cite{luo2023WizardCoder}. This decision was driven by the need for a coding model, especially pertinent to the spider dataset in Text-to-SQL program synthesis. The fine-tuned model, named Spider Skeleton Wizard Coder, attained an execution accuracy of 61% On the spider development set comprising 1,034 entries. The model is publicly available on the HuggingFace Transformers Library\footnote{https://huggingface.co/richardr1126/spider-skeleton-wizard-coder-merged}.
Spider · split not statedgpt-3.5-turbo-16k (zero-shot with prompt alignment and database schema format)execution accuracy68.2%+10.6 vs baseline0-shot · model=gpt-3.5-turbo-16k · prompting=C3-based prompt alignment and schema format
Source quote
L32: In preliminary trials, conducted without a clear database context and prompt alignment, and conducted mainly for comparison with Spider Skeleton Wizard Coder, the execution accuracy stood at 57.6%. The transition from open-source to closed-source methodologies marked the adoption of more robust strategies for OpenAI's models akin to those in C3 \cite{dong2023c3}. This approach, within a zero-shot framework, achieved an execution accuracy of 68.2%.
Spider · split not statedFine-tuned gpt-3.5-turbo-16kexecution accuracy73.4%+15.8 vs baselinefine-tuned · model=gpt-3.5-turbo-16k · evaluation=zero-shot environment
Source quote
L38: The subsequent phase involved fine-tuning OpenAI's gpt-3.5-turbo-16k on the spider dataset, incorporating database context. The fine-tuning process was facilitated using OpenAI's API. Even in a zero-shot environment, the fine-tuned model demonstrated a substantial improvement, achieving a 73.4% execution accuracy in generating SQL queries, highlighting the effectiveness of fine-tuning models.
Spider · split not statedFine-tuned gpt-3.5-turbo-16k (few-shot) + gpt-4-turbo (zero-shot error correction)execution accuracy82.1%+24.5 vs baselinefew-shot · error-correction=zero-shot · generation-model=gpt-3.5-turbo-16k · error-correction-model=gpt-4-turbo
Source quote
L4: This study investigates various approaches to using Large Language Models (LLMs) for Text-to-SQL program synthesis, focusing on the outcomes and insights derived. Employing the popular Text-to-SQL dataset, spider, the goal was to input a natural language question along with the database schema and output the correct SQL SELECT query. The initial approach was to fine-tune a local and open-source model to generate the SELECT query. After QLoRa fine-tuning WizardLM's WizardCoder-15B model on the spider dataset, the execution accuracy for generated queries rose to a high of 61%. With the second approach, using the fine-tuned gpt-3.5-turbo-16k (Few-shot) + gpt-4-turbo (Zero-shot error correction), the execution accuracy reached a high of 82.1%. Of all the incorrect queries, most can be categorized into a seven different categories of what went wrong: selecting the wrong columns or wrong order of columns, grouping by the wrong column, predicting the wrong values in conditionals, using different aggregates than the ground truth, extra or too few JOIN clauses, inconsistencies in the Spider dataset, and lastly completely incorrect query structure. Most if not all of the queries fall into these categories and it is insightful to understanding where the faults still lie with LLM program synthesis and where they can be improved.
Reported baseline / comparator measurements (1)
BenchmarkMethodMetricScoreProtocol / Evidence
Spider · split not statedgpt-3.5-turbo-16k (zero-shot, without clear database context or prompt alignment) baselineexecution accuracy57.6%0-shot · model=gpt-3.5-turbo-16k · prompting=no clear database context or prompt alignment
Source quote
L32: In preliminary trials, conducted without a clear database context and prompt alignment, and conducted mainly for comparison with Spider Skeleton Wizard Coder, the execution accuracy stood at 57.6%. The transition from open-source to closed-source methodologies marked the adoption of more robust strategies for OpenAI's models akin to those in C3 \cite{dong2023c3}. This approach, within a zero-shot framework, achieved an execution accuracy of 68.2%.
BibTeX
@misc{trivedi2024analyzing,
  author = {Ashutosh Trivedi and Richard Roberson and Gowtham Kaki and Ashutosh Trivedi},
  title = {{Analyzing the Effectiveness of Large Language Models on Text-to-SQL Synthesis}},
  year = {2024},
  eprint = {2401.12379},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2401.12379},
}
Figure