arXiv:2610.30100cs.DBOct 2026

Manuscript Offprint

arXiv:2610.30100

Building SQL Equivalence Proofs from Query Rewrite Rules

Chunyu Chen, Xinyi Huang, Zhengjie Miao

ABSTRACT

Query rewriting and semantic evaluation of text-to-SQL systems both depend on deciding whether two SQL queries return the same result on every database satisfying the schema constraints. Decades of equivalence-preserving optimizer rules provide a natural basis for constructive proofs: two queries are equivalent when admitted rewrites transform them to a common logical expression. Building on this observation, EquiTrace organizes query-rewriting knowledge as a standalone proof-producing system. It separates rule validation from proof discovery, explicitly aligns alternative query representations, and returns the resulting rewrite derivation. Across three corpus classes, EquiTrace proves 648/1,106 expected-equivalent text-to-SQL pairs (58.6%), 229/266 curated pairs (86.1%), and 97/240 LLM-generated rewrites whose results agree under benchmark execution (40.4%). In a pinned comparison on the query rewrite corpus, EquiTrace proves 97 pairs, compared with 35 for QED and 38 for SQLSolver. An analysis of 1,242 recorded proofs shows that coverage depends on a small number of transformations, while many frequently used rules are individually substitutable. The resulting derivations make each proof replayable and expose the specific transformations on which it depends.

1 INTRODUCTION

Query equivalence is foundational in database systems, with wide applications in query optimization [2]. Recent advances in large language models (LLMs) have renewed interest in determining whether syntactically different queries produced by query-rewriting and text-to-SQL systems return the same result on every database satisfying the schema constraints.

A long line of work approaches equivalence checking through symbolic reasoning: SPES [15], SQLSolver [7], QED [12], and VeriEQL [8] translate SQL into algebraic or SMT-friendly representations and reason about their equivalence. While principled, supporting the full diversity of SQL requires ever-richer models of bag semantics, NULLs, subqueries, and integrity constraints — leading to unsatisfying empirical coverage of practical queries, in the literature and in our experiments (Section 4). That raises a complementary question: must every query pair be proved by reconstructing its full semantics inside a solver, or can we reuse the equivalence knowledge database people have accumulated over decades?

Query optimizers already encode a substantial body of equivalence knowledge. A logical transformation rule states a reusable semantic identity: whenever its applicability conditions hold, replacing one logical expression with another preserves the query result. Optimization composes such transformations to explore alternative plans and select an efficient one. The same transformations can instead serve as proof steps. If admitted rewrites transform both Q1Q_1 and Q2Q_2 to a common logical expression NN, the two resulting derivations constitute a constructive proof that Q1≡Q2Q_1 \equiv Q_2 (Figure 1).

(a) Query optimization: pick the most cost-efficient member of the class

Figure

(b) Equivalence proving: connect two queries inside the class

Figure

Figure 1: The same rewrite vocabulary supports two objectives: optimization explores an equivalence class to select an efficient plan, whereas equivalence proving constructs derivations from Q1Q_{1} and Q2Q_{2} to a common expression N.

Narasayya and Chaudhuri recently explored this observation in QO-Verify [11]: rather than building a separate semantic solver, it extends the SQL Server optimizer and searches the logical-expression spaces generated for the two queries; a common expression establishes equivalence, because each optimizer transformation is assumed to preserve semantics. A production optimizer, their results show, already contains enough equivalence knowledge to verify a substantial fraction of LLM-generated rewrites.

This raises a broader question: can query-rewriting knowledge form the basis of a general, standalone SQL equivalence proof system? Existing optimizers provide useful infrastructure for equivalence checking, including internal query representations, transformation libraries, and search strategies. However, our experience adapting Calcite for equivalence proving reveals three mismatches between optimization and proof construction. First, useful equalities may be oriented in only one direction; for example, Calcite includes guarded join-to-semi-join rules, but not the uniqueness-guarded reverse transformations required by some proofs. Second, some equivalence proofs require transformations that operate beyond a local rewrite. Pairwise join rules can enumerate alternative join trees, whereas canonicalization may require inspecting and rebuilding an entire commutative join region. Third, equivalent expressions may remain syntactically distinct, such as a=b and b=a or differently ordered conjunctions, and therefore require explicit normalization before their derivations can converge.

A standalone prover can combine inference rules from optimizer transformations, verified rewrite libraries, integrity-constraint reasoning, and semantic canonicalizations whose purpose is to expose equivalence rather than produce an efficient plan. Unlike a prover embedded in a proprietary optimizer, such a system can also expose the derivation, making explicit both the transformations that justify the result and the assumptions on which it depends.

We present EquiTrace, a standalone rewrite-based SQL equivalence prover built on three principles: it separates rule validity from proof discovery (transformation rules are inputs, assumed sound; proving searches for their compositions); it makes representation alignment first-class, bridging structurally different encodings of the same query; and it treats the derivation as the output – every proof records a replayable sequence of transformations reaching a common logical expression. The implementation uses Apache Calcite [1], but the principles do not depend on Calcite or on any single optimizer's inventory; users may supply their own rule sets.

Our evaluation measures the coverage of this proof model and identifies its remaining limitations. EquiTrace proves 648/1 106 expected-equivalent text-to-SQL-derived pairs (58.6%), 229/266 curated pairs (86.1%), including 210/232 Calcite rule tests, and 97/240 LLM-generated rewrites whose queries agree under benchmark execution (40.4%). On the last corpus, a common subset yields 97 proofs for EquiTrace, 35 for QED, and 38 for SQLSolver, with 114 pairs proved by their union. Analysis shows that the remaining LLM-rewrite failures arise either from insufficient search capacity in large join regions or from missing representations, particularly for union branches, outer joins, and window functions.

Contributions. Our contributions are summarized below:

  • We propose a proof model for SQL equivalence based on compositions of independently validated rewrite rules, with the resulting derivation as a first-class artifact (Section 2–Section 3).

  • We develop EquiTrace, a standalone and extensible proof-building framework that explicitly separates rule validation, representation alignment, and proof search, and serializes successful rule applications as replayable certificates (Section 3).

  • We evaluate EquiTrace across three workload classes, including a comparison with prior formal verification-based provers with analyses of both transformation-level necessity and the remaining proof gaps (Section 4–Section 5).