CoE-SQL: In-Context Learning for Multi-Turn Text-to-SQL with Chain-of-Editions

Hanchong Zhang, Ruisheng Cao, Hongshen Xu, Lu Chen, Kai Yu

Introduction

Translating natural language into SQL (Text-to-SQL) is a cornerstone of accessible data analysis. While single-turn tasks have seen significant progress, real-world interactions are often conversational. In these multi-turn scenarios, users frequently rely on context, omit previously mentioned details, or ask follow-up questions that modify earlier requests.

Existing approaches typically rely on either labor-intensive fine-tuned models or Large Language Model (LLM) based In-Context Learning (ICL). The latter often attempts to "rewrite" the entire conversation into a standalone question, a process prone to "error propagation"—where a single missed detail in the rewriting step guarantees an incorrect SQL query. CoE-SQL addresses this by treating multi-turn SQL generation as an editing problem rather than a generation-from-scratch problem.

The Chain-of-Editions (CoE) Framework

The core innovation of CoE-SQL is the Chain-of-Editions framework. Recognizing that SQL queries in a conversation are usually slight variations of the previous turn, the model is prompted to "edit" the previous query using a sequence of predefined operations.

Methodology

The reasoning process is formalized into three main steps:

  1. Unit Edit Rules: The authors defined 14 specific rules (e.g., EditSelectItem, EditWhereCondition) that serve as the vocabulary for the model's reasoning.
  2. Automatic Chain Extraction: An algorithm compares the Abstract Syntax Trees (ASTs) of consecutive queries to find the shortest path of edit rules required for the transformation.
  3. Thought-before-Edition: Inspired by the ReAct framework, the LLM first analyzes the difference between the current and previous questions before deciding on the specific edits.
Figure 1: Illustration of the AST-based editing process between turns.

The researchers also explored various prompting styles, finding that describing edits in plain Natural Language (e.g., "change * to COUNT(*)") surprisingly outperformed raw function calls or Python code. This suggests that LLMs leverage their extensive pre-training on natural language descriptions of code changes for internal reasoning.

Experimental Results

CoE-SQL was evaluated on two gold-standard multi-turn benchmarks: SParC (over 12k questions across 200 databases) and CoSQL (a conversational version of the Spider dataset). Using GPT-3.5-turbo-16k, the framework achieved state-of-the-art performance for in-context learning.

Table 1: Performance comparison on the SParC dataset.

As shown in the results, CoE-SQL significantly outperformed previous ICL methods like ACT-SQL. On the SParC dataset, it achieved an Execution Accuracy (EX) of 70.3%, compared to ACT-SQL's 63.8%. Notably, its performance is competitive with—and in some metrics surpasses—state-of-the-art fine-tuned models like RASAT+PICARD, despite not requiring expensive task-specific training.

Key Insights

  • Editing Prevents Forgetting: By forcing the model to explicitly state what is changing, the framework naturally preserves constraints from earlier turns that should remain constant, a common failure point in traditional generation.
  • Natural Language Advantage: The superiority of natural language prompts highlights the importance of aligning prompting strategies with the pre-training data of the underlying LLM.
  • Alignment with Human Cognition: The method mimics how humans track the "delta" or shift in intention during a conversation, making the reasoning process more robust.

Conclusion and Future Work

CoE-SQL demonstrates that an incremental editing approach is highly effective for complex, multi-turn reasoning tasks. While currently dependent on high-reasoning "frontier" models and requiring large context windows, the framework sets a new benchmark for zero-shot and few-shot text-to-SQL performance. Future work will explore more sophisticated exemplar retrieval strategies and testing on even larger models like GPT-4 to tackle "extra hard" query scenarios.

Summary updated

BibTeX
@inproceedings{zhang2024coe,
  author = {Hanchong Zhang and Ruisheng Cao and Hongshen Xu and Lu Chen and Kai Yu},
  title = {{CoE-SQL: In-Context Learning for Multi-Turn Text-to-SQL with Chain-of-Editions}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.naacl-long.361},
}
Figure