TL;DR
While large language models (LLMs) excel at translating natural language into SQL, current evaluation metrics only check if the query runs correctly, masking massive structural instability. This paper introduces SQLStructEval, a framework that analyzes the structural reliability of LLM-generated SQL using canonicalized Abstract Syntax Trees (ASTs). The authors reveal that even when execution results are correct, LLMs frequently produce wildly different query structures in response to minor, surface-level prompt changes—a vulnerability that can be mitigated by a novel "compile-style" generation pipeline.
What Problem Does This Paper Address?
When evaluating Text-to-SQL systems, researchers typically rely on execution accuracy—does the generated query return the correct rows from the database? However, this metric creates a significant blind spot. Multiple structurally distinct queries can yield identical results, often by introducing redundant joins, unnecessary subqueries, or alternative formulations.
This structural variance matters because it directly impacts code interpretability, maintainability, and downstream reasoning. If an LLM changes its underlying query logic every time it is asked the same question with slightly different wording, it cannot be considered reliable for production environments. This paper exposes and quantifies this hidden structural instability.
Methods
The authors propose SQLStructEval, a structure-aware evaluation framework that abstracts away superficial text differences (like formatting or alias naming) to focus entirely on program architecture.
The framework operates via two main components:
- AST Parsing & Canonicalization: Queries are converted into Abstract Syntax Trees (ASTs) using the
sqlglotlibrary. The framework normalizes table aliases (e.g., mapping them sequentially tot1,t2) and flattens/sorts logical operators (like nestedANDblocks) so that logically identical structures map to the exact same canonical representation. - Structural Metrics: The authors introduce metrics to quantify model behavior over multiple sampled outputs, including Structural Consistency (the frequency of the most common structure), Structural Diversity (the number of distinct ASTs generated), and Perturbation Sensitivity (how often the dominant structure changes under input variations).
Additionally, the paper evaluates a Compile-Style Generation paradigm. Instead of forcing the LLM to write raw SQL directly, the model is prompted to generate a structured JSON object representing the query's logical components. A deterministic compiler then transforms this JSON into executable SQL, decoupling structural planning from surface-level code synthesis.
Datasets
The framework was evaluated using the Spider benchmark, a widely recognized cross-domain Text-to-SQL dataset. Specifically, the authors utilized the Spider development set, which contains 1,034 questions across 138 distinct databases. To test robustness, they also generated semantically invariant perturbations (paraphrases and reordered schemas) for a subset of 200 questions.
Key Insights and Ideas
- Execution correctness masks structural chaos: LLMs can be functionally right but structurally erratic. A model might achieve high execution accuracy while simultaneously alternating between entirely different query plans across multiple iterations of the same prompt.
- LLMs are incredibly sensitive to linguistic surface forms: Minor changes in phrasing or the order of tables in a database schema frequently trigger completely different query decomposition strategies or join patterns, indicating a lack of stable internal representations.
- Structured intermediate spaces anchor the model: Forcing an LLM to think in a structured intermediate space (like JSON/AST) before generating code acts as a regularizer, significantly boosting both execution accuracy and structural stability.
Key Results
The authors evaluated several leading LLMs, including variants of GPT, Claude, Gemini, and DeepSeek.
- High Structural Variance: As shown in Table 1, even advanced models like GPT-5-mini generate an average of 1.913 distinct AST structures per question, with the dominant structure appearing only 65% of the time.
- Instability Among "Correct" Queries: Even when filtering strictly for execution-correct queries, substantial variance remains. For example, GPT-5-mini averages 1.378 distinct structures per correct answer, yielding an AST similarity score of just 0.552 (Table 2).
- Extreme Sensitivity to Paraphrasing: When tested against paraphrased questions, GPT-5-mini's cross-paraphrase AST similarity plummeted to 0.328, with a staggering 90% of questions experiencing a change in their majority query structure (Table 4).
- The Power of Compilation: Transitioning to a compile-style pipeline yielded impressive gains. For GPT-5-mini, execution accuracy jumped from 0.742 to 0.785, while the structural similarity among correct queries improved from 0.552 to 0.632 (Table 3).
Limitations and Future Work
The study focuses primarily on the Spider benchmark, which features relatively small and controlled database environments; real-world enterprise databases with massive schemas might exhibit different structural behaviors. Furthermore, while canonical ASTs catch structural differences, they do not fully account for semantic equivalence across radically different query formulations (e.g., achieving the same result via a complex JOIN versus a nested window function).
The authors suggest that future work should extend structural evaluation to broader code generation tasks (like general Python synthesis or API calling) and explore ways to incorporate structural objectives directly into LLM training losses.
Summary updated