Understanding, Detecting, and Repairing Real-World In-Context-Learning-Based Text-to-SQL Errors

Jiawei Shen, Chengcheng Wan, Ruoyi Qiao, Jiazhen Zou, Hang Xu, Yuchen Shao, Yueling Zhang, Weikai Miao, Geguang Pu

TL;DR

This paper presents the first comprehensive study of text-to-SQL errors generated by Large Language Models (LLMs) using In-Context Learning (ICL). The authors uncover 27 distinct error types across 7 categories and demonstrate that existing LLM self-correction methods often introduce new bugs or suffer from massive computational overhead. To address this, they introduce MapleDoctor, a hybrid framework that prioritizes deterministic rule-based fixes before falling back to constrained LLM prompts, boosting query correctness while slashing repair latency by 67.4%.

What Problem Does This Paper Address?

Text-to-SQL systems allow non-expert users to query relational databases using natural language. With the rise of LLMs, state-of-the-art performance is typically achieved via In-Context Learning (ICL)—prompting the model with a few examples without updating its underlying parameters.

However, LLMs are prone to hallucinations and non-deterministic behavior, frequently generating invalid or semantically incorrect SQL queries.

Fig. 1. An incorrect SQL query generated by DIN- SQL [1].

As shown in Figure 1, an LLM might look for an un-capitalized string value ('legal') in an enumerated column that only accepts capitalized values ('Legal'), or perform an aggregate function on a column containing NULL values without proper filtering.

Existing repair strategies fall into two flawed camps: they either blindly ask the LLM to "fix" every query (which is slow, expensive, and often introduces new errors), or they rely on simplistic compiler-error checks that miss silent logic and semantic bugs. This paper aims to systematically categorize these errors and build a faster, more reliable automated doctor for SQL queries.

Methods

The authors manually analyzed 4,602 incorrect SQL queries to construct a symptom-driven error taxonomy focused on the "edit path to correctness."

Fig. 2. Text-to-SQL error taxonomy for ICL-based techniques.

As illustrated in Figure 2, the taxonomy breaks errors down into categories like Syntax, Schema, Logic, Convention, and Semantics.

Building on these insights, they developed MapleDoctor, an automated error detection and repair framework that uses a hybrid, "rule-first" strategy:

  1. Rule-Based Detection: MapleDoctor uses static analysis, Abstract Syntax Tree (AST) checks, and lightweight database execution probes to identify specific error symptoms without needing to understand the user's natural language intent.
  2. Deterministic Repair: When a high-confidence fix exists (e.g., adding an IS NOT NULL clause or casting an integer to a float during division), MapleDoctor applies a deterministic code rewrite.
  3. Constrained LLM Fallback: If the error is purely semantic or the rule-based fix is ambiguous, MapleDoctor invokes an LLM. Crucially, instead of a generic "please fix this" prompt, it provides a highly constrained prompt containing precise schema metadata, execution error logs, error locations, and explicit repair guidelines.

Datasets

The framework was evaluated using three prominent text-to-SQL benchmarks:

  • Spider: A widely used cross-domain benchmark containing 20 databases and 1,034 text-SQL pairs in its dev split.
  • BIRD: A significantly larger and more complex benchmark (11 databases, 1,534 pairs) featuring large-scale databases and external knowledge evidence.
  • ScienceBenchmark: A challenging, domain-specific benchmark consisting of 599 text-to-SQL tasks used to test the generalizability of the framework.

Key Insights and Ideas

  • LLM Self-Correction is Risky: Blindly relying on LLMs to fix their own code without external constraints often leads to "mis-repairs," where a simple syntax or schema error is mutated into a complex semantic error that is even harder to detect.
  • Intent-Independent Errors are Prevalent: Roughly 26% of all text-to-SQL errors are completely independent of the user's natural language question. These include basic syntax violations, schema mismatches, and clear logical anti-patterns that can be resolved purely through database schema constraints.
  • Execution Symptoms as Proxies: While semantic errors (like an improper WHERE clause condition) are hard to catch via static analysis, they frequently manifest as "suspicious symptoms" during execution—namely, returning an empty result set or unexpected NULL values.
  • Benchmark Imperfections: A surprising 15.4% of flagged errors were categorized as "Not an Error," driven primarily by incorrect ground-truth labels ("gold errors") within the benchmarks themselves.

Key Results

MapleDoctor was evaluated against top-performing text-to-SQL repair pipelines (including MAC-SQL, DIN-SQL, and CHESS) across multiple LLM backends (GPT-3.5, GPT-4o, Qwen3, Mistral, LLaMA, and DeepSeekCoder).

  • Higher Correctness: MapleDoctor successfully repaired 13.8% more queries than the next-best baseline framework.
  • Fewer Mis-repairs: It reduced the number of broken queries introduced during the repair phase by 84.9%.
  • Massive Speedup: By resolving simple errors programmatically and avoiding unnecessary LLM calls, MapleDoctor achieved a 67.4% reduction in repair latency, averaging just 1.2 seconds per query compared to the 3.5 to 8.0 seconds required by purely LLM-based approaches.

Limitations and Future Work

  • Unclassifiable Errors: About a third of the analyzed errors were so severely broken that they lacked a clear incremental "edit path" to correctness, requiring a complete query rewrite. MapleDoctor currently terminates when encountering these.
  • Heuristic False Positives: The framework assumes that correct queries rarely return empty result sets. In real-world applications where an empty result is a valid answer, this heuristic could trigger unnecessary verification.
  • Future Directions: The authors suggest exploring structural rewriting techniques for completely garbled queries and integrating MapleDoctor's taxonomy directly into the pre-generation phase to prevent errors from being generated in the first place.

Summary updated

Abstract

Large language models (LLMs) have been adopted for text-to-SQL tasks, utilizing their in-context learning (ICL) capability to translate natural language questions into SQL queries. However, such a technique faces correctness problems. In this paper, we conduct the first comprehensive study of text-to-SQL errors of ICL-based techniques. Our study covers four representative ICL-based techniques, five basic repairing methods, two benchmarks, and two LLM settings. We find that text-to-SQL errors are widespread and summarize 27 error types of 7 categories. We also find that existing repairing attempts have limited correctness improvement while having high computational overhead and many mis-repairs. Based on these findings, we propose MapleDoctor, a novel text-to-SQL error detection and repairing framework. The evaluation demonstrates that MapleDoctor outperforms existing solutions by repairing 13.8% more queries with a negligible number of mis-repairs and reducing 67.4% repair latency. The artifact is publicly available at GitHub.
BibTeX
@article{shen2026understanding,
  author = {Jiawei Shen and Chengcheng Wan and Ruoyi Qiao and Jiazhen Zou and Hang Xu and Yuchen Shao and Yueling Zhang and Weikai Miao and Geguang Pu},
  title = {{Understanding, Detecting, and Repairing Real-World In-Context-Learning-Based Text-to-SQL Errors}},
  journal = {Proc. ACM Softw. Eng.},
  year = {2026},
  doi = {10.1145/3808171},
}

Loading related content...

Figure