arXiv:2610.41472cs.SEOct 2026

Manuscript Offprint

arXiv:2610.41472

Post-SQLFix: Property-Guided Repair of LLM-Generated SQL with Runtime Verification

Zhihao Guo, Zonghan Wu, Yilei Shao, Haizhou Du, Nan Tang, Xiao Cheng, Huan Huo

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Large Language Models (LLMs) for Text-to-SQL frequently generate queries with syntax errors, invalid identifier references, or violations of SQL scoping and typing rules. Repairing these errors is a program correctness problem, yet existing approaches rely on Execution Feedback (EF), whose symptom-level, dialect-specific error messages force the LLM to speculate on root causes. We identify an Arbitration Paradox in this paradigm: the system relies on unverified models both to propose repairs and to make the final judgment on whether those repairs satisfy the required properties. We resolve this paradox by reformulating Text-to-SQL repair as property-guided program repair. Our framework, Post-SQLFix, canonicalizes SQL into a dialect-aware Canonical Query Structure (CQS) and validates it through a hierarchical static analyzer with three stratified property layers (Syntactic, Context-Free Structural, Context-Sensitive Semantic). The analyzer produces precise, schema-aware violation diagnostics to guide the LLM's repair proposals. A Verify-Before-Apply Temporal Monitor then accepts only repairs that reduce the violation count, do not reintroduce resolved violations, and satisfy the operator contracts. These runtime invariants are bounded by the declared rule family and operator contracts, while a finite retry budget ensures halting. On three established benchmarks (BIRD, Spider, Mini-Dev) spanning over 3,000 evaluation instances, three SQL dialects (SQLite, MySQL, PostgreSQL), and six model configurations, Post-SQLFix resolves 77.8% of the execution errors that EF leaves unrepaired, while requiring less than half as many repair iterations and 30% fewer tokens. Crucially, accepted transitions under the monitor introduce no statically detectable structural regressions in our evaluation, translating checked repair fidelity into absolute accuracy improvements of up to +11.6 percentage points.

1 Introduction

Large language models (LLMs) can translate natural-language questions into executable SQL [17, 49, 53], yet the resulting queries frequently contain syntactic flaws, schema-entity type mismatches, and structural scoping errors [49, 53]. Unlike compiled languages, SQL offers no static type-checking compilation step between generation and execution: an ill-formed query is discovered only when the database engine rejects it at runtime or, worse, when it silently returns incorrect results. Repairing these errors is therefore a program correctness problem, orthogonal to the choice of generator: any LLM-based Text-to-SQL pipeline can benefit from a principled post-generation repair layer. Such a layer demands the kind of formal intermediate representations, property specifications, and bounded runtime checks that are central to the programming-languages tradition.

The dominant repair paradigm relies exclusively on the database engine for runtime verification. Execution-Feedback (EF) pipelines [6, 45, 51] run the candidate query, capture the engine's error message, and prompt the LLM to fix it. Although these runtime signals are faithful, they are also symptom-level: messages such as Syntax error near WHERE lack schema- and intent-level context, vary across dialects, and

*These authors contributed equally to this work.

†Corresponding authors: Xiao Cheng and Huan Huo.

Figure

Figure 1: Overview of Post-SQLFix. (a) Canonicalization into CQS. (b) Hierarchical Validation & Repair. (c) Deterministic Alignment for empty-result queries.

conflate multiple root causes into a single opaque trace. More fundamentally, EF provides no signal at all for queries that execute successfully yet return wrong results (e.g., scoping violations, invalid aggregations, or empty-result cases). The LLM must still diagnose root causes and synthesize repairs based on the ambiguous evidence from EF, routinely introducing new errors, a well-documented fix-and-regress cycle [15, 19].

This failure is not incidental. It reflects a structural limitation we term the Arbitration Paradox: the system relies on unverified models both to propose repairs and to make the final judgment on whether those repairs satisfy the required properties. Whether the architecture is self-correction [8, 28, 36], retrieval-augmented generation [23, 41, 46], or multi-agent coordination [1, 3, 34, 50], successful execution alone does not verify that a repair preserves previously correct query structure. If the final judgment of repair compliance is delegated to probabilistic LLM reasoning [11], the unverified mechanism proposing the repair also becomes the authority on whether it should be accepted.

The paradox exposes two concrete gaps. First, a diagnostic gap: no deterministic mechanism exists to lift the ambiguous, engine-specific runtime symptoms of EF pipelines into precise, schema-aware property violations; the LLM must map an opaque error trace, or for silently wrong queries no trace at all, to a structured root cause, and does so unreliably. Second, a repair gap: even given an accurate diagnosis, no principled mechanism constrains the repair search space or enforces monotonic progress; existing pipelines check neither that a proposed fix preserves previously correct structure (soundness) nor that the process converges within bounded iterations (termination), and the LLM may freely introduce new violations while patching old ones.

A natural response is to turn to existing SQL static analysis. However, industry linters such as SQLFluff [44] enforce only syntactic formatting rules without schema-aware semantic validation. Prior work on statically analyzing dynamically generated SQL [10] targets human-written application code and does not account for the distinctive error distribution of LLM generators (e.g., hallucinated table names, foreign-key-agnostic joins). Formal equivalence checkers [13] require a ground-truth reference query, which is unavailable at inference time. All of these tools are passive validators: they may detect errors but provide no mechanism to synthesize repairs or to check that a proposed fix does not introduce new violations. To bridge this gap, we draw inspiration from the broader programming languages community, where property-guided program repair [2, 43] has proven highly successful in bypassing ambiguous runtime errors to synthesize checked fixes. The efficacy of these formal methods in traditional software development suggests that a similar paradigm is highly viable for the Text-to-SQL problem.

Our Solution. We resolve the Arbitration Paradox by reformulating Text-to-SQL repair as a property-guided program repair problem [2, 43], replacing speculative execution feedback with verifiable static analysis and formally bounded synthesis. Our framework, Post-SQLFix, is a generator-agnostic post-processing layer: it takes the SQL output of any existing Text-to-SQL pipeline and strengthens it through formal analysis and repair. The direct comparison target is the EF repair paradigm that such pipelines currently rely on.

To close the repair gap, we formalize the repair process as a state-transition system governed by a verify-before-apply temporal monitor. For each detected violation, the monitor first computes a bounded candidate subspace from the schema and dialect configuration, constraining the LLM to propose patches within that subspace. A candidate transition is accepted only if the violation set strictly decreases; otherwise it is rejected and the LLM is re-prompted. This monotonicity invariant directly checks bounded soundness (no previously resolved violation recurs) and bounded halting (the loop terminates under the finite retry budget, returning either a zero-violation CQS or the last accepted state).

Figure 1 illustrates the end-to-end pipeline. (a) The raw SQL is parsed into a dialect-aware Abstract Syntax Tree and lifted into a Canonical Query Structure (CQS), a normalized intermediate representation that decouples relational semantics from dialect-specific surface syntax. (b) The hierarchical validation and repair stage combines the static analyzer with a checked repair loop. The analyzer reports violations of the declared α/β/γ\alpha/\beta/\gamma rules, closing the diagnostic gap. These violations guide candidate construction, LLM repair proposals, and validation before updating the CQS. (c) Queries that pass all static layers but return empty results are routed to a data-driven alignment stage that conservatively adjusts filter predicates using empirical evidence from the database. We illustrate this paradox and our workflow through a concrete query repair in § 2. Post-SQLFix is the first framework to provide formally specified runtime invariant checks for LLM-generated SQL repair. Our contributions are summarized as follows:

  • A property-guided formulation of Text-to-SQL repair, grounded in a dialect-aware intermediate representation (CQS) and a stratified static analyzer that replaces ambiguous execution feedback with precise, schema-aware property violations (§3).
  • A verify-before-apply temporal monitor with runtime-enforced monotonicity: every accepted repair transition strictly decreases the violation count and satisfies the history and preservation checks (Runtime Invariant 1). A finite retry budget also bounds rejected proposals (§4).
  • An empirical evaluation across three SQL dialects (SQLite, MySQL, PostgreSQL) and six models showing up to +11.6 percentage points of execution accuracy improvement, over 58% fewer repair iterations, and a 100% interception rate of LLM-induced regressions (§5).

References (48)