Introduction
Large Language Models (LLMs) have significantly advanced the field of text-to-SQL, enabling natural language interfaces for complex databases. However, these models frequently produce "silent" semantic errors—queries that are syntactically correct and execute without error but return incorrect data due to logical flaws. SQLENS is an end-to-end framework designed to detect and rectify these insidious errors by combining database-native signals with LLM reasoning, aggregated through weak supervision.
The Challenge of Semantic Errors
Unlike syntax errors, which cause immediate failure, semantic errors are difficult to identify because the system provides no warning. Common issues include joining the wrong tables, misinterpreting filters, or creating redundant logic. Existing "self-reflection" techniques, where an LLM checks its own work, often fail due to self-preference bias—the model's tendency to trust its initial output. Furthermore, these systems often lack the fine-grained, explainable feedback necessary for human debugging in real-world scenarios where ground truth is unavailable.
The SQLens Framework
SQLENS addresses these challenges through a three-phase pipeline that grounds LLM reasoning in the physical reality of the database.
1. Multi-Signal Error Detection
SQLENS extracts "noisy signals" from two primary sources:
- Database-based signals: These utilize structural heuristics, such as identifying suboptimal join trees via Steiner Tree algorithms, detecting empty predicates (filters returning zero rows), or flagging abnormal results (e.g., columns dominated by NULLs).
- LLM-based signals: Targeted prompts check for evidence violations (contradictions of the user's prompt) or column ambiguity.
2. Weak Supervision Aggregation
To handle the inherent noise in individual signals, SQLENS employs a weak supervision framework. By treating each signal as a labeling function, the system estimates the true probability of an error without requiring a labeled "gold" dataset, making it highly applicable to production environments.
3. Iterative Correction Loop
Once errors are detected, SQLENS enters an iterative refinement cycle:
- Error Selector: Prioritizes and ranks detected errors by criticality.
- Error Fixer: Guides the LLM to resolve errors sequentially rather than all at once, preventing the model from becoming overwhelmed.
- SQL Auditor: Acts as a final high-precision guardrail, determining if the revised query is genuinely superior to the original.
Experimental Evaluation
The framework was evaluated on the BIRD and Spider benchmarks using Claude 3.5 Sonnet as the backbone. SQLENS was integrated into several state-of-the-art systems, including DIN-SQL, MAC-SQL, and CHESS.
Key Results
- Detection Performance: SQLENS achieved a 25.78% higher F1-score in error detection compared to standard LLM self-evaluation on the BIRD benchmark.
- Accuracy Gains: The framework improved execution accuracy across all tested systems, notably providing a 20.5% relative improvement for DIN-SQL on BIRD.
- Signal Precision: Specific signals like "Abnormal Result" and "Incorrect Join Predicate" demonstrated near 100% precision, serving as reliable indicators of logic failure.
Conclusion and Future Work
SQLENS demonstrates that the most effective way to mitigate LLM hallucinations in SQL generation is to ground the model in database-native structural signals. While the iterative process introduces some latency—averaging 30 seconds per query—it provides a robust solution for offline query refinement and asynchronous debugging. Future research will focus on reducing latency and utilizing reinforcement learning to further optimize error ranking and selection.
Summary updated