arXiv:2609.09978cs.DBSep 2026

Manuscript Offprint

arXiv:2609.09978

Few Rows Tell Them Apart: Equivalence of Queries Mixing Set and Bag Semantics

Sara Cohen

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

Abstract

Bounded SQL equivalence checkers search for a counter-example database of bounded size, and a search that comes back empty proves nothing. We supply missing theory: computable bounds BB such that agreement on all databases with at most BB tuples per relation implies equivalence. We work in the combined-semantics framework, which captures SQL's mix of duplicate-eliminating (DISTINCT) and duplicate-preserving computation over set-valued relations. For conjunctive queries we prove a bound linear in the query size for fixed multiset width: inequivalent queries already disagree on a database with at most 2w∣Q∣2^w|Q| tuples, where the width ww counts only the columns the queries actually read, independently of the total number of multiset variables. Declared keys shrink the bound to 2kw∣Q∣2^{kw}|Q| for the smaller key-width kwkw, acyclic foreign keys leave it unchanged, and the result extends to several classes of queries with comparisons, for which equivalence had not previously been characterized. For these fragments, bounded search becomes a terminating, complete decision procedure.

2012 ACM Subject Classification Theory of computation → Database query languages (principles); Theory of computation → Complexity theory and logic; Information systems → Query optimization

Keywords and phrases query equivalence, combined semantics, bounded equivalence, counterexample bounds, conjunctive queries, integrity constraints

Funding The authors were partially supported by the ISF (Israel Science Foundation, Grant 359/21).

1 Introduction

Query equivalence is a central problem of database theory. Two queries are equivalent if they return the same result on every database. Five decades of research on the problem have powered query optimization, rewriting over materialized views, and data integration [3, 4]; it has recently gained new urgency. Large language models now rewrite SQL at scale, and err at scale: in a recent industrial study, roughly a third of over 3,100 LLM-proposed rewrites of production queries returned results different from the original [17], and text-to-SQL benchmarks' equivalence verdicts determine published model rankings [14].

This renewed demand has produced a wave of practical SQL equivalence checkers, split into two one-sided families. Symbolic provers [22, 12, 20] certify equivalence, but each only on a restricted syntactic fragment, and none certifies a refutation. Bounded refuters [8, 13, 21, 14] embrace the most expressive queries by testing bounded equivalence: a solver searches for a counter-example—a database on which the two queries disagree—among all databases with at most nn tuples per relation, growing nn until timeout or fixing it in advance. A counter-example refutes equivalence; an exhausted search proves nothing, yet its verdicts are trusted. A fifth of the pairs one suite of VeriEQL's evaluation reports as “checked” are in fact inequivalent, two needing counter-examples of over a thousand tuples [13], and SpotIt publishes what survives its n=5n=5-capped search as benchmark accuracy [14].

What is missing is a completeness threshold: a computable bound BB such that any two inequivalent queries already disagree on some database with at most BB tuples per relation. With such a bound the search stops at BB and reports equivalent with certainty—and a per-relation version caps each relation separately, shrinking the search space further. This paper establishes such bounds for the conjunctive core of the problem.

Equivalence of Queries Mixing Set and Bag Semantics

We work in the framework of combined semantics [9, 10], which captures a core aspect of how real SQL queries evaluate. SQL mixes two modes of computation. A DISTINCT block or an EXISTS subquery is a set computation, blind to duplicates, while projection without DISTINCT preserves multiplicities. Combined semantics expresses this mix in one language by declaring, per variable, whether different values contribute new copies to the answer (multiset variables) or not (set variables). Following common practice, we take stored relations to be sets, as is the case whenever every table declares a key.

For the two pure extremes, small counter-examples have long been known. Inequivalent set queries (pure DISTINCT) disagree on a database with at most one tuple per query atom [3], and the same holds for pure bag-set queries [4]. In both cases the bound is the self-join size: the maximum number of atoms sharing a predicate. For queries that genuinely mix the two modes, the property fails, and it fails already for the simplest possible queries.

Example 1. The following queries return the names of vip customers. The first returns each name once. The second returns each name once per distinct customer bearing it.

QA: SELECT DISTINCT name FROM Customer WHERE type='vip' QB: SELECT name FROM (SELECT DISTINCT cid, name FROM Customer WHERE type='vip') D

Each query uses the Customer table once, so the classical bounds promise a one-tuple counter-example. None exists: the queries agree on every one-tuple database, and differ only once two vip customers share a name—which takes two tuples. The classical bound is wrong for mixed queries, and how far it must grow is precisely the question.

The example is benign, but the general phenomenon is not: it is not even clear that a computable bound on counter-example size exists at all. Our main result is that it does, and it is linear in the query size.

Contributions. Section 3 proves the central result, a counter-example bound for relational combined-semantics queries that is linear in the query: inequivalent queries disagree on a database with at most 2w∣Q∣2^w|Q| tuples, where ∣Q∣|Q| is the number of atoms and the multiset width ww counts only the multiset columns the queries actually read. Section 4 admits integrity constraints. The bound refines per relation, drops to 2kw∣Q∣2^{kw}|Q| for the smaller key-width kwkw under declared keys, covering in particular queries whose joins follow keys acyclically, and is unchanged under acyclic foreign keys. The same argument characterizes equivalence there, as multiset-homomorphism of the key-chased reducts, and so places the problem in NP. These are the first results on equivalence of multiset and combined queries in the presence of integrity constraints. Section 5 extends the bound to several classes of queries with comparisons against constants, for which no equivalence characterization, and hence no decision procedure, was previously known.

Taken together, the results turn heuristic bounded search into a terminating, complete decision procedure for the conjunctive fragment studied here, with a threshold that is small whenever self-joins are few and the multiset columns are narrow. All proofs, and some constructions, are deferred to the appendix.