TL;DR
Determining if two SQL queries are truly equivalent is a notoriously difficult problem that traditional tools often struggle to solve. This paper introduces LLM-SQL-Solver, a framework that leverages Large Language Models (LLMs) and novel prompting techniques—Miniature & Mull and Explain & Compare—to identify both strict semantic equivalence and a more relaxed "pragmatic" equivalence, significantly outperforming standard execution-based metrics in aligning with human judgment.
What Problem Does This Paper Address?
In the world of databases, knowing if two queries are the same is vital for query optimization and for evaluating AI-generated code (text-to-SQL). However, SQL equivalence is theoretically undecidable in the general case. Existing formal provers are often unmaintained or fail to support common operations like EXISTS or IN.
Currently, researchers rely on two main metrics:
- Exact Match: Checking if the SQL strings are identical (too strict, as many different strings produce the same result).
- Execution Match: Running both queries on a database and comparing outputs.
The authors point out a major flaw in Execution Match: it often yields "false positives." If a test database is small or lacks specific edge cases, two logically different queries might return the same empty result, leading a system to incorrectly label them as equivalent. A classic example is the "COUNT bug," where subtle differences in how subqueries handle nulls or empty sets are easily missed by standard tests.
Methods
The authors propose two distinct notions of equivalence and specific prompting strategies to help LLMs (like GPT-4o) identify them:
- Semantic Equivalence: The strict definition—two queries are equivalent if they return the same output for every possible database instance. To tackle this, the authors developed Miniature & Mull (M&M). The LLM is asked to:
- Miniature: Create a tiny, simple database instance and execute both queries.
- Mull: Actively try to modify that database (e.g., adding rows or changing values) to find a "counterexample" where the queries produce different results.
- Pragmatic Equivalence: A relaxed definition—do the queries convey the same meaningful information to a human user, even if the table structure or column names differ? For this, they use Explain & Compare, where the LLM first explains the high-level intent of each query before comparing their underlying logic.
Datasets
The study evaluated LLMs across three main data sources:
- Calcite test suites: 232 pairs of expertly verified equivalent queries used in the Apache Calcite optimizer.
- Semantically Inequivalent Queries: 180 pairs from the Spider benchmark where execution outputs were known to differ (providing clear counterexamples).
- Pragmatic Equivalence Queries: A new set of 70 query pairs manually labeled by three SQL experts to capture human preference and intent.
- Spider Benchmark: Used to compare the LLM-SQL-Solver against state-of-the-art text-to-SQL models like DAIL-SQL and DIN-SQL.
Key Insights and Ideas
- LLMs as "Counterexample Hunters": The "Mull" step is crucial. By forcing the model to think like a debugger trying to break the equivalence, it catches subtle logic errors that surface-level reasoning misses.
- Execution Match is a Poor Proxy for Intent: The authors found that in cases where LLMs and Execution Match disagreed, the LLM's judgment aligned with human experts 78.6% of the time, while Execution Match only aligned 40% of the time.
- Model Scaling Matters: GPT-4o is significantly better at following strict instructions (like case-sensitivity) and simulating query execution than GPT-3.5-Turbo, which frequently hallucinated results or ignored prompt constraints.
- Pragmatic Equivalence is Subjective: Even experts sometimes disagree on whether two queries are "close enough." This suggests that future evaluation metrics might need to be customizable to specific user needs.
Key Results
The Miniature & Mull technique provided a massive boost to accuracy, especially for weaker models. As shown in the table below, M&M improved GPT-3.5's accuracy on semantic equivalence by over 30% compared to standard Chain-of-Thought (CoT) prompting.
For GPT-4o, the M&M approach achieved roughly 90% accuracy in determining semantic equivalence. Furthermore, when evaluating text-to-SQL systems, the LLM-based "pragmatic" metric proved to be a much more reliable indicator of quality than the industry-standard execution accuracy.
Limitations and Future Work
- Hallucination: LLMs still sometimes "hallucinate" the results of their simulated executions. The authors suggest that giving LLMs access to external tools (like a Python/Pandas sandbox) could mitigate this.
- Dataset Size: The human-labeled dataset for pragmatic equivalence is relatively small (70 pairs) because expert annotation is extremely time-consuming (approx. 10 minutes per pair).
- Customization: Since pragmatic equivalence is subjective, the authors propose developing personalized evaluation metrics that can be tuned to a specific user's domain or preferences via in-context learning.
Summary updated