Abstract
In-context learning (ICL) has become the standard for Text-to-SQL tasks, yet current retrieval methods often fail by selecting demonstrations based solely on question similarity while ignoring database schemas. We introduce DCG-SQL, a novel framework that constructs a Deep Contextual Schema Link Graph to jointly represent natural language questions and database structures. By aligning retrieval with the structural logic of the database, DCG-SQL significantly bridges the "demonstration gap," yielding substantial performance gains across various benchmarks, particularly for smaller open-source large language models (LLMs).
Introduction
The effectiveness of Text-to-SQL generation relies heavily on the database schema. However, most existing ICL systems select few-shot demonstrations using flat text similarity between questions. This approach often leads to the selection of examples that, while linguistically similar to the user's query, utilize entirely different database structures, misleading the model. This issue is especially pronounced for smaller LLMs (sLLMs) like Llama 3.1-8B, which lack the reasoning capacity to overcome poor contextual examples. DCG-SQL addresses this by incorporating schema context directly into the retrieval process.
Methodology
The DCG-SQL framework operates in three distinct stages to ensure that retrieved demonstrations are structurally relevant to the target query.
1. Deep Contextual Schema Link Graph Construction
To manage complex databases, the system first performs Schema Pruning using a RoBERTa-based cross-encoder to filter irrelevant tables and columns. It then establishes Schema Linking by leveraging the model's internal attention scores. These scores create semantic "links" between question tokens (e.g., "older") and specific schema items (e.g., the age column), forming the Deep Contextual Schema Link Graph.
2. Graph-based Demonstration Retrieval
Instead of treating questions as simple text strings, DCG-SQL employs a graph encoder trained via contrastive learning. The retriever is optimized using a "likelihood" score that identifies which demonstrations actually assist an LLM in generating correct SQL for a given anchor. This allows for "hard negative mining," ensuring the retriever can distinguish between examples that are superficially similar but structurally unhelpful.
3. SQL Generation with Automated CoT
Finally, the retrieved examples are integrated into an automated Chain-of-Thought (CoT) prompt. By categorizing queries (e.g., Simple, Join, Nested), the system guides the LLM through the logical steps of SQL construction, maximizing the utility of the high-quality retrieved demonstrations.
Experimental Results
DCG-SQL was evaluated on major benchmarks including Spider (and its robustness variants DK, Realistic, and Syn) and BIRD. The framework consistently outperformed state-of-the-art baselines like DAIL-SQL and DIN-SQL across a wide range of model sizes.
Key Findings:
- The sLLM Multiplier: The most significant impact was observed in smaller models. Llama 3.1-8B achieved a 9.5% increase in Execution Accuracy (EX) on Spider, while DeepSeek-Coder-6.7B saw a massive 23.1% jump in Exact Match (EM) accuracy.
- State-of-the-Art for GPT-4: Even high-performing models improved, with GPT-4 reaching 87.5% EX on Spider, setting a new benchmark for ICL-based Text-to-SQL.
- Efficiency: The graph-based retrieval remains highly efficient, with a latency of approximately 1.3 seconds, outperforming more complex multi-step reasoning agents.
Conclusion
DCG-SQL demonstrates that schema context is a vital component of the retrieval process for Text-to-SQL. By bridging the gap between natural language and database structure through a contextual link graph, the framework enables smaller, open-source models to compete with much larger proprietary counterparts. Future work will focus on scaling this approach to enterprise-level schemas and further enhancing the reasoning capabilities of sLLMs within the ICL framework.
Summary updated