LLM-SQL-Solver: Can LLMs Determine SQL Equivalence?
Fuheng Zhao, Jiayue Chen, Lawrence Lim, Ishtiyaque Ahmad, Divyakant Agrawal, Amr El Abbadi
TL;DR
The paper investigates whether large language models can determine SQL equivalence under semantic and pragmatic notions. It introduces two prompting strategies, Miniature & Mull for semantic tests via simple database counterexamples, and Explain & Compare for pragmatic tests through explanation and comparison, applying them to GPT-3.5 Turbo and GPT-4o across Cosette, Zhong 2020, and Spider datasets. Results show LLMs can effectively help identify semantically equivalent and non-equivalent queries and align well with human judgments for pragmatic similarity, though execution-based metrics remain fallible. The work argues for adopting LLM-SQL-Solver as a privacy-preserving, scalable evaluation metric for NL2SQL and SQL rewriting tasks, while outlining limitations and future directions to reduce hallucinations and broaden datasets.
Abstract
Judging the equivalence between two SQL queries is a fundamental problem with many practical applications in data management and SQL generation (i.e., evaluating the quality of generated SQL queries in text-to-SQL task). While the research community has reasoned about SQL equivalence for decades, it poses considerable difficulties and no complete solutions exist. Recently, Large Language Models (LLMs) have shown strong reasoning capability in conversation, question answering and solving mathematics challenges. In this paper, we study if LLMs can be used to determine the equivalence between SQL queries under two notions of SQL equivalence (semantic equivalence and relaxed equivalence). To assist LLMs in generating high quality responses, we present two prompting techniques: Miniature & Mull and Explain & Compare. The former technique is used to evaluate the semantic equivalence in which it asks LLMs to execute a query on a simple database instance and then explore if a counterexample exists by modifying the database. The latter technique is used to evaluate the relaxed equivalence in which it asks LLMs to explain the queries and then compare if they contain significant logical differences. Our experiments demonstrate using our techniques, LLMs is a promising tool to help data engineers in writing semantically equivalent SQL queries, however challenges still persist, and is a better metric for evaluating SQL generation than the popular execution accuracy.
