text:
# Equivalences Among Aggregate Queries with Negation

## 1 Introduction

The emergence of data warehouses and of decision-support systems has highlighted the importance of efficiently processing aggregate queries. In such systems the amount of data is generally large and aggregate queries are used as a standard means of reducing the volume of the data. Aggregate queries tend to be expensive as they “touch” many items while returning few. Thus, optimization techniques for aggregate queries are a necessity. Many optimization techniques, such as query rewriting, are based on checking query equivalence. For this purpose, a coherent understanding of the equivalence problem of aggregate queries is necessary.

One of our main results in this paper is that equivalence is decidable for disjunctive queries with comparisons and negated subgoals if they contain one of the aggregation functions: max, top$`2`$, count, sum, prod, or parity.

A query that does not have negated subgoals is positive. Equivalence of positive non-aggregate queries has been studied extensively . Furthermore, in  it has been shown that equivalence is decidable for non-aggregate disjunctive queries with negation. Syntactic characterizations of equivalences among aggregate queries with the functions max, sum, and count have been given in . These results have been extended in  to queries with the functions prod and avg, for the special case of queries that contain neither constants nor comparisons. Thus, there are results on the equivalence problem for non-aggregate queries with negation as well as for aggregate queries without negation. Equivalence of aggregate queries with negated subgoals was dealt with for the first time in . This paper is a substantially revised and extended version of .111One of the proofs in  was incorrect and has been corrected in this version.

Our decidability proofs rely on abstract properties of aggregation functions. We consider functions that are defined by means of operations on abelian monoids. Our proofs work out if the monoids are either idempotent or are groups. Functions of the first kind are max and top$`2`$, functions of the second kind are count, sum, and parity.

For these functions we reduce equivalence with respect to all possible databases to equivalence over databases that have at most as many constants as there are constants and variables in the queries, a property which we call local equivalence. We do not study local equivalence immediately, but rather the more general problem of bounded equivalence. It consists of determining, given a nonnegative integer $`N`$ and two queries, whether the queries return identical results over all databases with at most $`N`$ constants. We give a complete characterization of decidability of bounded equivalence. In particular, we show that bounded equivalence is decidable for queries with the functions count, cntd, max, sum, prod, avg, top$`2`$ and parity.

Finally, we consider the special case of quasilinear queries, that is, queries where predicates that occur positively are not repeated. For quasilinear queries equivalence boils down to isomorphism, which can be decided in polynomial time.

## 2 Aggregation Functions

An aggregate query is executed in two steps. First, data is collected from a database as specified by the non-aggregate part of the query. Then the results are grouped into multisets (or bags), an aggregation function is applied to the multisets, and the aggregates are returned as answers.

The queries that we consider in this paper contain the aggregation functions count and cntd, which for a bag return the number of elements or distinct elements, respectively; parity, which returns $`0`$ or $`1`$, depending on whether the number of elements in the bag is even or odd; sum, prod and avg, which return the sum, product, or average of the elements of a bag; max, which returns the maximum among the elements of a bag; and top$`2`$, which returns a pair consisting of the two greatest different elements of a bag.

The reader will notice in the course of the paper that our results for max and top$`2`$ immediately carry over to min and bot$`2`$, which select the minimum or the two least elements out of a multiset of numbers. Moreover, our results for top$`2`$ can easily be generalized to the function top$`K`$, which selects the $`K`$ greatest different elements.

Our arguments to prove decidability of equivalence for certain classes of aggregate queries rely on the fact that the aggregation functions take values in special kinds of abelian monoids and are defined in terms of the operations of those monoids. To make this formal, we will introduce the class of monoidal aggregation functions and two of its subclasses. We will show that all of the above functions except cntd, prod and avg belong to one of these two subclasses. In general, an aggregation function maps multisets of tuples of numbers to values in some structure, which in most cases consists again of numbers. Here, we assume that the results of the aggregation are elements of some abelian monoid.

An abelian monoid is a structure $`(M,+,0)`$ consisting of a set $`M`$ with an associative and commutative binary operation, which we denote as “$`+`$”, and a neutral element, which we denote as 0. If no confusion can arise, we identify a monoid with the set on which it is defined and refer to $`(M,+,0)`$ simply as the monoid $`M`$.

An abelian monoid $`M`$ is idempotent if $`a+a=a`$ holds for all $`aM`$, and $`M`$ is a group if for every $`aM`$ there is a $`bM`$ such that $`a+b=0`$. The element $`b`$ is called the inverse of $`a`$ and is usually denoted as $`a`$. Instead of $`a+(b)`$ we will usually write $`ab`$.

###### Examples 2.1

Standard abelian monoids are the set of integers $`𝐙`$ and the set of rational numbers $`𝐐`$, with the binary operation of addition and the neutral element $`0`$. We use $`𝐐^\pm `$ to denote the set of rational numbers without the element 0. Note that $`𝐐^\pm `$, with the binary operation of multiplication and the neutral element $`1`$ is also an abelian monoid. A further example is the two-element group $`𝐙_2=\{\mathrm{\hspace{0.17em}0},1\}`$, where the addition satisfies $`1+1=0`$.

By $`𝐐_{}`$ we denote the rational numbers augmented by a new element $``$, which is less than any element in $`𝐐`$. Then $`𝐐_{}`$ is an abelian monoid if the operation is selecting the maximum of two numbers. The neutral element is $``$.

A less common example is the monoid $`𝐓_2`$, which is defined on the set of pairs

$`𝐓_2`$ $`:=`$ $`\left\{(d,e)𝐐_{}\times 𝐐_{}\right|d>e\}\left\{(,)\right\}.`$
We denote the binary operation on $`𝐓_2`$ as “$``$”. We define $`(d_1,e_1)(d_2,e_2)`$ as the pair $`(d,e)`$ that consists of the two greatest different elements among $`\{d_1,e_1,d_2,e_2\}`$, provided this set has at least two elements, and as the pair $`(d,)`$ if the set consists only of the element $`d`$. For instance, we have that $`(5,)(2,1)=(5,2)`$, that $`(5,2)(5,1)=(5,2)`$, and that $`(5,)(5,)=(5,)`$. Clearly, $`(,)`$ is the neutral element.

If $`(M,+,0)`$ is an abelian monoid, we can extend the binary operation to subsets of $`M`$ and to multisets over $`M`$ in a canonical way—because of the associativity and commutativity of “$`+`$”, the order in which we apply the operation does not matter. If $`S`$ is such a set or multiset, we denote the result of applying “$`+`$” to $`S`$ as $`_{aS}a`$.

Many common aggregation functions are computed by first mapping the elements of a multiset of tuples into an abelian monoid and then combining the values obtained through the mapping by the monoid operation.

Later on in the paper, we will assume that aggregate queries range over databases with constants from some set $``$. We call such a set a domain and assume that a linear ordering “$`<`$” is defined on its elements. For example, the integers $`𝐙`$ and the rational numbers $`𝐐`$ are such domains. For our discussion of aggregation functions the ordering on the domains is of no importance.

If $``$ is a domain and $`k`$ is a nonnegative integer, we use $`^k`$ to denote the $`k`$-fold cartesian product of $``$. Thus, $`^k`$ consists of all $`k`$-tuples where the components are elements of $``$. For the special case in which $`k=0`$, the set $`^0`$ consists of a single element, called the empty tuple. When $`k=1`$, we will often omit the superscript. Hence, we use $``$ to denote $`^1`$.

Technically, we assume that there is a domain $``$, a nonnegative integer $`k`$, a monoid $`(M,+,0)`$ and a function $`f:^kM`$. Then the aggregation function over $`^k`$ based on $`f`$ and “$`+`$”, which maps multisets $`B`$ over $`^k`$ to elements of $`M`$, is denoted as $`\alpha _f^+`$ and defined by

$`\alpha _f^+(B):={\displaystyle \underset{aB}{}}f(a),`$
for all bags $`B`$ over $`^k`$. We say that $`\alpha `$ is a monoid aggregation function if $`\alpha =\alpha _f^+`$ for some abelian monoid operation “$`+`$”. In particular, we say that $`\alpha _f^+`$ is idempotent or a group aggregation function if the underlying monoid is idempotent, or a group, respectively.

###### Examples 2.2

Obviously, sum and max are the unary aggregation functions over $`𝐙`$ or $`𝐐`$, based on the identity mapping and on addition or the binary operation “$`\mathrm{max}`$,” respectively. The functions count and parity over $`𝐙^0`$ arise from the additive groups $`𝐙`$ and $`𝐙_2`$, respectively, by choosing as $`f`$ the mapping that maps every element to the constant $`1`$. Note that count and parity are nullary aggregation functions. Therefore, the domain over which they are defined consists only of the empty tuple. We obtain the aggregate top$`2`$ over $`𝐐`$ by choosing the monoid $`𝐓_2`$ and the mapping $`f:𝐐𝐓_2`$ defined by $`f(a):=(a,)`$. Similarly, we can define top$`2`$ over the integers.

Note that sum, count and parity are group aggregation functions, while max and top$`2`$ are idempotent. The aggregation function prod is also a group aggregation function, when defined over $`𝐐^\pm `$. However, one can prove that cntd, prod (over $`𝐐`$ or $`𝐙`$) and avg are not monoid aggregation functions.

## 3 Disjunctive Aggregate and Non-Aggregate Queries

We now introduce conjunctive and disjunctive queries with negated subgoals and review their basic properties. We use standard Datalog syntax extended by aggregation functions.

### 3.1 Syntax of Non-aggregate Queries

Predicate symbols are denoted as $`p`$, $`q`$ or $`r`$. A term, denoted as $`s`$ or $`t`$, is either a variable or a constant. A relational atom has the form $`p(s_1,\mathrm{},s_k)`$, where $`p`$ is a predicate of arity $`k`$. We also use the notation $`p(\overline{s})`$, where $`\overline{s}`$ stands for a tuple of terms $`(s_1,\mathrm{},s_k)`$. Similarly, $`\overline{x}`$ stands for a tuple of variables. An ordering atom or comparison has the form $`s_1\rho s_2`$, where $`\rho `$ is one of the ordering predicates $`<`$, $``$, $`>`$, $``$, or $``$. A relational atom can be negated. A relational atom that is not negated is positive. A literal is a positive relational atom, a negated relational atom, or a comparison. A condition, denoted as $`A`$, is a conjunction of literals. A condition $`A`$ is safe  if every variable appearing in $`A`$ either appears in a positive relational atom or is equated with such a variable. Throughout this paper we will assume that all conditions are safe.

A query is a non-recursive expression of the form

$`q(\overline{x})A_1\mathrm{}A_n,`$
where each $`A_i`$ is a condition containing all the variables appearing in the tuple $`\overline{x}`$. The variables that occur in the head, i.e., in $`\overline{x}`$, are the distinguished variables of the query. Those that occur only in the body are the nondistinguished variables.

A query is conjunctive if it contains only one disjunct. A query is positive if it does not contain any negated relational atoms. By abuse of notation, we will often refer to a query by its head $`q(\overline{x})`$ or simply by the predicate of its head $`q`$.

### 3.2 Semantics of Non-aggregate Queries

Databases are sets of ground relational atoms and are denoted by the letter $`𝒟`$. The carrier of $`𝒟`$, written $`\text{carr}(𝒟)`$, is the set of constants occurring in $`𝒟`$. In this paper we assume that the constants in a database are either integers or rational numbers. We define how a query $`q`$, evaluated over a database $`𝒟`$, gives rise to a set of tuples $`q^𝒟`$.

An assignment $`\gamma `$ for a condition $`A`$ is a mapping of the variables and constants appearing in $`A`$ to constants, such that each constant is mapped to itself. Assignments are naturally extended to tuples, atoms and other complex syntactical objects. For $`\overline{s}=(s_1,\mathrm{},s_k)`$ we let $`\gamma (\overline{s})`$ denote the tuple $`(\gamma (s_1),\mathrm{},\gamma (s_k))`$. The application of an assignment to other syntactical objects is defined analogously. Satisfaction of atoms and of conjunctions of atoms by an assignment with respect to a database or with respect to a semantic structure are defined in the obvious way. Sometimes, we will refer to assignments as instantiations and to the result of applying an assignment to a syntactical object as an instantiation of that object.

For the interpretation of comparisons it makes a difference whether they range over a dense order, like the rational numbers, or a discrete order, like the integers. A conjunction of comparisons, like $`0<y<z<2`$, may be satisfiable over the rational numbers, but not over the integers.

For a given database $`𝒟`$, a query $`q(\overline{x})A_1\mathrm{}A_n`$ defines a new relation

$`q^𝒟:={\displaystyle \underset{i=1}{\overset{n}{}}}\left\{\gamma (\overline{x})\right|\gamma \text{ satisfies }A_i\text{ with respect to }𝒟\}.`$ (1)
Chaudhuri and Vardi  have introduced bag-set semantics, which records the multiplicity with which a tuple occurs as an answer to the query. The definition in (1) can be turned into one for bag-set semantics by replacing set braces by multisets and set union by multiset union. Bag semantics  differs from bag-set semantics in that both database relations and relations created by queries are multisets of tuples.

### 3.3 Syntax of Aggregate Queries

In  we have shown that equivalence of positive disjunctive queries with several aggregate terms can be reduced to equivalence of queries with a single aggregate term. Using a similar proof it is possible to show that this still holds if the queries can contain negated subgoals. For this reason, we consider in the present paper only queries having a single aggregate term in the head. We give a formal definition of the syntax of such queries.

An aggregate term is an expression built up using variables and an aggregation function. For example count and $`\text{sum}(y)`$ are aggregate terms. We use $`\alpha (\overline{y})`$ as an abstract notation for an aggregate term. Note that $`\overline{y}`$ can be the empty tuple as in the case of the functions count or parity.

An aggregate query is a query augmented by an aggregate term in its head. Thus it has the form

$`q(\overline{x},\alpha (\overline{y}))A_1\mathrm{}A_n.`$ (2)
In addition, we require that

* no variable $`x\overline{x}`$ occurs in $`\overline{y}`$;
* each condition $`A_i`$ contains all the variables in $`\overline{x}`$ and in $`\overline{y}`$.

We call $`\overline{x}`$ the grouping variables of the query. If the aggregate term in the head of a query has the form $`\alpha (\overline{y})`$, we call the query an $`\alpha `$-query (e.g., a max-query).

### 3.4 Semantics of Aggregate Queries

Consider an aggregate query $`q`$ as in Equation (2). We define how, for a database $`𝒟`$, the query yields a new relation $`q^𝒟`$. We proceed in two steps.

We denote the set of assignments $`\gamma `$ over $`𝒟`$ that satisfy one of the disjuncts $`A_i`$ in the body of $`q`$ as $`\mathrm{\Gamma }(q,𝒟)`$. We assume that such a $`\gamma `$ is defined only for the variables that occur in $`A_i`$. Moreover, if an assignment $`\gamma `$ satisfies two or more disjuncts, we want it to be included as many times in $`\mathrm{\Gamma }(q,𝒟)`$ as there are disjuncts it satisfies. To achieve this, we assume that there are as many copies of $`\gamma `$ in $`\mathrm{\Gamma }(q,𝒟)`$ as there are disjuncts that $`\gamma `$ satisfies, and that each copy carries a label indicating which disjunct it satisfies.222We could make this more formal by defining $`\mathrm{\Gamma }(q,𝒟)`$ to consist of pairs $`(\gamma ,i)`$, where $`\gamma `$ is an (ordinary) assignment and $`i`$ the index of a condition that it satisfies. However, to avoid charging our notation with too much detail, we prefer to introduce the concept of “labeled assignments” informally.

Recall that $`\overline{x}`$ are the grouping variables of $`q`$ and $`\overline{y}`$ are the aggregation variables. For a tuple $`\overline{d}`$, let $`\mathrm{\Gamma }_{\overline{d}}(q,𝒟)`$ be the subset of $`\mathrm{\Gamma }(q,𝒟)`$ consisting of labeled assignments $`\gamma `$ with $`\gamma (\overline{x})=\overline{d}`$. In the sets $`\mathrm{\Gamma }_{\overline{d}}(q,𝒟)`$, we group those satisfying assignments that agree on $`\overline{x}`$. Therefore, we call $`\mathrm{\Gamma }_{\overline{d}}(q,𝒟)`$ the group of $`\overline{d}`$.

Let $`A`$ be a set of labeled assignments and $`\overline{y}`$ be a tuple of variables for which the elements of $`A`$ are defined. Then we define the restriction of $`A`$ to $`\overline{y}`$ as the multiset

$`A_{|\overline{y}}:=\{\{\gamma (\overline{y})\gamma A\}\}.`$
We can apply the restriction operator to $`\mathrm{\Gamma }_{\overline{d}}(q,𝒟)`$. If $`\alpha (\overline{y})`$ is an aggregate term, we can apply $`\alpha `$ to the multiset $`A_{|\overline{y}}`$, which results in the aggregate value $`\alpha (A_{|\overline{y}})`$. As an alternative notation, we define

$$\alpha (\overline{y})A:=\alpha (A_{|\overline{y}}).$$

Now we define the result of evaluating $`q(\overline{x},\alpha (\overline{y}))`$ over $`𝒟`$, denoted $`q^𝒟`$, by

$`q^𝒟:=\left\{\left(\overline{d},\alpha (\overline{y})\mathrm{\Gamma }_{\overline{d}}(q,𝒟)\right)\right|\overline{d}=\gamma (\overline{x})\text{ for some }\gamma \mathrm{\Gamma }(q,𝒟)\}.`$
Similarly as for non-aggregate queries, $`q^𝒟`$ is a set of tuples.

### 3.5 Equivalence

Two queries $`q`$ and $`q^{}`$, aggregate or non-aggregate, are equivalent, denoted $`qq^{}`$, if over every database they return identical sets of results, that is, if $`q^𝒟=q_{}^{}{}_{}{}^{𝒟}`$ for all databases $`𝒟`$. For positive non-aggregate queries, equivalence is decidable and has been characterized in terms of the existence of query homomorphisms . Levy and Sagiv have shown that equivalence is still decidable for disjunctive queries with negated atoms .

In , we have proved decidable characterizations for the equivalence of positive conjunctive and disjunctive aggregate queries with the operators max, count, and sum. Note that two non-aggregate queries $`q(\overline{x})`$ and $`q^{}(\overline{x})`$ are equivalent under bag-set semantics if and only if the count-queries $`q(\overline{x},\text{count})`$ and $`q^{}(\overline{x},\text{count})`$ are equivalent. Thus, characterizations of the equivalence of count-queries immediately yield criteria for non-aggregate queries to be equivalent under bag-set semantics.

## 4 Bounded Equivalence

Our goal is to reduce the problem of deciding equivalence of two aggregate queries over all possible databases to the problem of deciding local equivalence, that is, equivalence over databases containing no more constants than the size of the queries. In this section, we present the conditions necessary for the more general bounded equivalence problem to be decidable.

Let $`N`$ be a nonnegative integer. We say that two queries $`q`$ and $`q^{}`$ are $`N`$-equivalent, denoted $`q_Nq^{}`$, if for all databases $`𝒟`$ whose carrier has at most $`N`$ elements, we have $`q^𝒟=q_{}^{}{}_{}{}^{𝒟}`$. The bounded equivalence problem for a class of queries is to decide, given $`N>0`$ and queries $`q`$ and $`q^{}`$ from that class, whether $`q_Nq^{}`$.

Let $`A`$ be a condition. The variable size of $`A`$ is the number of variables in $`A`$. Let $`q`$ be a disjunctive query. The variable size of $`q`$ is the maximum of the variable sizes of the conditions in $`q`$. If a query contains an equality $`y=z`$, it does not matter for the proofs later on whether the variables $`y`$ and $`z`$ are counted once or twice.

The term size of a query is the total number of constants occurring in that query plus the variable size. The term size of a pair of queries $`q`$ and $`q^{}`$ is the total number of constants occurring in at least one of $`q`$ or $`q^{}`$ plus the maximum of the variable sizes of $`q`$ and $`q^{}`$. We denote the term size of $`q`$ as $`\tau (q)`$ and the term size of $`q`$ and $`q^{}`$ as $`\tau (q,q^{})`$. We say that two queries $`q`$ and $`q^{}`$ are locally equivalent if $`q^𝒟_{\tau (q,q^{})}q_{}^{}{}_{}{}^{𝒟}`$, that is, if $`q`$ and $`q^{}`$ return identical results over all databases with at most $`\tau (q,q^{})`$ constants.

Clearly, two queries are equivalent if and only if they are $`N`$-equivalent for all $`N>0`$. However, the decidability of bounded equivalence for a class of queries does not necessarily imply that equivalence is decidable. Sections 5 and 6 establish criteria for this implication to hold. Moreover, decidability of $`N`$-equivalence, for a fixed $`N`$, does not imply decidability of local equivalence, since in the latter problem the size of the databases to be tested depends on the size of the queries.

###### Proposition 4.1 (Bounded and Local Equivalence)

If the bounded equivalence problem is decidable for a class of queries, then local equivalence is decidable, too.

Proof.  Deciding local equivalence of $`q`$ and $`q^{}`$ boils down to deciding bounded equivalence of $`q`$ and $`q^{}`$ for $`N=\tau (q,q^{})`$.

In the rest of this section we study the decidability of the bounded equivalence problem for several aggregation functions. Note that $`N`$-equivalence is not necessarily a trivial property. Even if the size of databases is bounded, there are still infinitely many databases whose size is below the bound, and the aggregation results may well depend on the values of the constants in the given database.

We introduce the notion of shiftable aggregation functions and of order-decidable aggregation functions. We show that shiftable aggregation functions are a special case of order-decidable aggregation functions. Finally, we prove that bounded equivalence is decidable exactly for queries with order-decidable aggregation functions.

### 4.1 Shiftable Aggregation Functions

We introduce the notion of shiftable aggregation functions. Intuitively, the value of such a function does not depend on the specific values in a multiset, but only on the ordering of the elements.

Let $`D`$ and $`D^{}`$ be subsets of a domain $``$ and $`\varphi :DD^{}`$ be a function. We say that $`\varphi `$ is a shifting function over $``$ if for all $`d`$$`d^{}D`$ we have

$`d<d^{}\phi (d)<\phi (d^{}).`$
In other words, a shifting function over a domain is a strictly monotonic function from one subset of the domain to another subset. A shifting function is applied to bags as one would expect. Let $`\alpha `$ be an aggregation function that is defined over $`^k`$. We say that $`\alpha `$ is shiftable if for all subsets $`D`$ and $`D^{}`$ of $``$, for all shifting functions $`\phi :DD^{}`$, and for all bags $`B`$ and $`B^{}`$ with elements in $`D^k`$, we have

$`\alpha (B)=\alpha (B^{})\alpha (\phi (B))=\alpha (\phi (B^{})).`$
###### Proposition 4.2 (Shiftable Aggregation Functions)

The aggregation functions parity, cntd, count, max and top$`2`$ are shiftable.

Proof.  The results of the aggregation functions parity and count depend only on the number of elements in the bag to which they are applied. Applying a shifting function to a bag does not affect this number. Therefore, these functions are shiftable. Similarly, the result of the aggregation function cntd depends only on the number of distinct elements in the bag to which it is applied. Since shifting functions are always injective, cntd is also shiftable.

The aggregation function max chooses the greatest element in a bag. The order of the elements is preserved by a shifting function. Thus, $`\text{max}(\phi (B))=\phi (\text{max}(B))`$. By definition, $`\phi `$ is an injection. Therefore, $`\text{max}(B)=\text{max}(B^{})`$ if and only if $`\phi (\text{max}(B))=\phi (\text{max}(B^{}))`$, which is true if and only if $`\text{max}(\phi (B))=\text{max}(\phi (B^{}))`$. Hence, max is shiftable.

Using similar reasoning to that of max it is easy to see that top$`2`$ is shiftable.

Note, however, that the aggregation functions sum and prod are not shiftable. For example, consider the bags $`B=\{\{\mathrm{\hspace{0.17em}2},\mathrm{\hspace{0.17em}2}\}\}`$ and $`B^{}=\{\{\mathrm{\hspace{0.17em}4}\}\}`$ and suppose $`\phi `$ is a shifting function with $`\phi (2)=3`$ and $`\phi (4)=5`$. Then $`\text{sum}(B)=\text{sum}(B^{})=\text{prod}(B)=\text{prod}(B^{})=4`$, while neither sum nor prod agree on $`\phi (B)=\{\{\mathrm{\hspace{0.17em}3},\mathrm{\hspace{0.17em}3}\}\}`$ and $`\phi (B^{})=\{\{\mathrm{\hspace{0.17em}5}\}\}`$.

### 4.2 Order-Decidable Aggregation Functions

Before defining order-decidable aggregation functions, we present some auxiliary definitions. Given a domain $``$, a conjunction of ordering atoms $`L`$, and an ordering atom $`t\rho t^{}`$, we define in the standard way when $`L`$ entails $`t\rho t^{}`$ with respect to $``$, denoted $`L_{}t\rho t^{}`$, and when $`L`$ is satisfiable with respect to $``$.

We say that $`L`$ is a complete ordering of a set of terms $`T`$ with respect to $``$ if for every two terms $`t`$$`t^{}T`$, exactly one of the following holds:

* $`L_{}t<t^{}`$;
* $`L_{}t>t^{}`$;
* $`L_{}t=t^{}`$.

Note that by definition, complete orderings are satisfiable.

Let $`\alpha `$ be an aggregation function over $`^k`$. An ordered identity for $`\alpha `$ is a formula

$$L\alpha (B)=\alpha (B^{})$$
(3)
where $`L`$ is a complete ordering of some set of terms $`T`$ with respect to $``$, and $`B`$ and $`B^{}`$ are bags containing $`k`$-tuples of terms from $`T`$. We say that $`\alpha `$ is order-decidable over $``$ if the validity of ordered identities for $`\alpha `$ is decidable over $``$. Note that the validity of an ordered identity may be dependent on $``$.

Formula (3) is valid if for every assignment $`\delta `$ that maps the variables in $`L`$ to $``$ and satisfies $`L`$, we have that $`\alpha `$ yields the same values when applied to $`\delta (B)`$ and to $`\delta (B^{})`$.

###### Example 4.3

It is easy to see that the function cntd is order-decidable over any domain. Consider, for example, the bags $`B=\{\{\mathrm{\hspace{0.17em}1},\mathrm{\hspace{0.17em}2},u\}\}`$ and $`B^{}=\{\{v,v,\mathrm{\hspace{0.17em}7},\mathrm{\hspace{0.17em}8}\}\}`$ and an arbitrary complete ordering $`L`$ of $`\{\mathrm{\hspace{0.17em}1},\mathrm{\hspace{0.17em}2},u,v,\mathrm{\hspace{0.17em}7},\mathrm{\hspace{0.17em}8}\}`$. It is straightforward to decide whether the formula

$$L\text{cntd}(\{\{\mathrm{\hspace{0.17em}1},2,u\}\})=\text{cntd}(\{\{v,v,7,8\}\})$$

is valid, since for any assignment $`\delta `$ satisfying $`L`$ the number of distinct values in the bags $`\delta (B)`$ and $`\delta (B^{})`$ is not dependent on the values assigned to $`u`$ and $`v`$. In fact, the number of distinct elements that are contained in $`\delta (B)`$ and $`\delta (B^{})`$ depends entirely on the ordering $`L`$.

It is not by chance that the function cntd is order-decidable over all domains. It is actually a consequence of the fact that cntd is a shiftable aggregation function.

###### Theorem 4.4 (Shiftable Implies Order-Decidable)

Let $`\alpha `$ be a shiftable aggregation function defined over $`^k`$. Then $`\alpha `$ is order-decidable over $``$.

Proof.  Let $`L\alpha (B)=\alpha (B^{})`$ be an ordered identity as in (3). In principle, to check this identity for validity, one has to verify that for all $`\delta `$ satisfying $`L`$ the equality $`\alpha (\delta (B))=\alpha (\delta (B^{}))`$ holds. We will show that it is sufficient to verify the equality for a single $`\delta `$ if $`\alpha `$ is shiftable.

Suppose that $`\alpha `$ is a shiftable aggregation function over $`^k`$. Let $`\delta :T`$ be an assignment. Clearly, if $`\delta `$ satisfies $`L`$, then the following conditions hold:

* $`\delta `$ maps all constants to themselves;
* for all $`t`$, $`t^{}T`$ and all ordering predicates $`\rho `$ we have that

$$t\rho t^{}L_I\delta (t)\rho \delta (t^{}).$$

Consider Formula (3). Since $`L`$ is a complete ordering, $`L`$ is satisfiable with respect to $``$. Let $`\delta `$ be an assignment satisfying $`L`$. Now, let $`\delta ^{}:T`$ be a second assignment that satisfies $`L`$. We assume without loss of generality that there are no two different terms $`t_1`$, $`t_2T`$ for which $`L_{}t_1=t_2`$. (If there were such terms we could remove one of them by renaming.) Hence, $`\delta `$ and $`\delta ^{}`$ are injections. Thus, the function $`\delta ^{}\delta ^1`$ is well defined.

Since both $`\delta `$ and $`\delta ^{}`$ preserve order, $`\delta ^{}\delta ^1`$ is a shifting function. Thus, $`\alpha (\delta (B))=\alpha (\delta (B^{}))`$ implies $`\alpha (\delta ^{}(B))=\alpha (\delta ^{}(B^{}))`$, as required.

The other direction of Theorem 4.4 does not hold. An aggregation function can be order-decidable over a given domain even if it is not shiftable. For example, the aggregation functions sum and avg are order-decidable, although they are not shiftable.

###### Proposition 4.5 (Order Decidability of Sum and Average)

The aggregation functions sum and avg are order-decidable over $`𝐙`$ and over $`𝐐`$.

Proof.  For the aggregation function sum, Formula (3) can be expressed using Presburger arithmetic. Recall that Presburger arithmetic is the first-order theory of addition. Presburger showed  that Presburger integer arithmetic (i.e., where the variables range over the integers) is decidable. Similarly, Presburger rational arithmetic is also known to be decidable . Therefore, sum is order-decidable.

The order-decidability of avg follows in a straightforward fashion from the order-decidability of sum, as we now show. Let $`B`$ be a bag of size $`N`$. We use $`NB`$ to denote the bag derived from $`B`$ by increasing the multiplicity of each term in $`B`$ by a factor of $`N`$. Thus, $`NB`$ contains exactly the same terms as those in $`B`$. If a term $`t`$ appears in $`B`$ exactly $`k`$ times, then $`t`$ appears in $`NB`$ exactly $`Nk`$ times.

Consider bags of numbers $`B`$ and $`B^{}`$. Suppose that $`B`$ is of size $`N`$ and $`B^{}`$ is of size $`N^{}`$. Observe that

$`\text{avg}(B)=\text{avg}(B^{})`$ $`N^{}\text{sum}(B)=N\text{sum}(B^{})\text{sum}(N^{}B)=\text{sum}(NB^{})`$
Therefore,

$$L\text{avg}(B)=\text{avg}(B^{})$$

is valid if and only if

$$L\text{sum}(N^{}B)=\text{sum}(NB^{})$$

is valid, where $`N`$ and $`N^{}`$ are the cardinalities of $`B`$ and $`B^{}`$, respectively. Hence, since sum is order-decidable, avg is also order-decidable.

The aggregation function prod is also order-decidable. In order to show this result we first present a few necessary definitions and lemmas. These are needed when considering prod over the integers.

Let $`T`$ be a set of terms and let $`L`$ be a complete ordering of $`T`$. We say that $`T`$ is reduced with respect to $`L`$ and to a domain $``$ if

* there are no different variables $`x`$ and $`y`$ occurring in $`T`$ such that $`L_{}x=y`$;
* there is no variable $`x`$ occurring in $`T`$ and no constant $`d`$ in $``$ such that $`L_{}x=d`$.

We say that a constant $`c`$ is a possible value for a variable $`xT`$ with respect to $`L`$ and $``$ if there is an assignment for the variables in $`T`$ with constants from $``$ that satisfies $`L`$ and maps $`x`$ to the value $`c`$. Observe that if $`T`$ is reduced with respect to $`L`$ and $``$, then there are at least two different possible values for each variable in $`T`$. Also, note that $`T`$ may be reduced with respect to $`L`$ over the rational numbers, but not over the integers. For instance, $`T=\{\mathrm{\hspace{0.17em}0},x,\mathrm{\hspace{0.17em}2}\}`$ is reduced with respect to $`L=\{\mathrm{\hspace{0.17em}0}<x,x<2\}`$ over the rational numbers, but over the integers, $`L`$ entails that $`x=1`$.

###### Lemma 4.6 (Assignments for Possible Values)

Let $`L`$ be a complete ordering of the terms in $`T`$. Suppose that $`T`$ is reduced with respect to $`L`$ and to $``$. Let $`x`$ be a variable in $`T`$ and let $`c_1`$ and $`c_2`$ be possible values for $`x`$ with respect to $`L`$ and $``$. Then there are assignments $`\delta _1`$ and $`\delta _2`$ for the terms in $`T`$ that satisfy $`L`$, are equal on all terms other than $`x`$, and such that $`\delta _1(x)=c_1`$ and $`\delta _2(x)=c_2`$.

Proof.  Since $`c_1`$ and $`c_2`$ are possible values for $`x`$, there are assignments $`\delta _1`$ and $`\delta _2`$ that satisfy $`L`$ such that $`\delta _i(x)=c_i`$, for $`i=1`$$`2`$. Note that $`\delta _1`$ and $`\delta _2`$ may also differ on additional terms in $`T`$.

Let $`\delta `$ be the assignment for the terms in $`T`$ defined by

$$\delta (t):=\{\begin{array}{cc}\text{min}\{\delta _1(t),\delta _2(t)\}\hfill & \text{if }L_{}tx\hfill \\ \text{max}\{\delta _1(t),\delta _2(t)\}\hfill & \text{if }L_{}t>x\text{.}\hfill \end{array}$$

We show that $`\delta `$ satisfies $`L`$. Let $`t`$ and $`t^{}`$ be terms in $`T`$. Suppose that $`L_{}t<t^{}`$. We consider two cases.

Case 1. Suppose that $`L_{}t^{}x`$. Let $`i`$ be such that $`\delta (t^{})=\delta _i(t^{})`$. Then, $`\delta (t)\delta _i(t)<\delta _i(t^{})=\delta (t^{})`$. Therefore, $`\delta `$ satisfies $`t<t^{}`$.

Case 2. Suppose that $`L_{}x<t^{}`$. Let $`i`$ be such that $`\delta (t)=\delta _i(t)`$. Then, $`\delta (t)=\delta _i(t)<\delta _i(t^{})\delta (t^{})`$. Therefore, $`\delta `$ satisfies $`t<t^{}`$.

Since $`L`$ is a complete ordering and $`t<t^{}`$ was arbitrary, it follows that $`\delta `$ satisfies $`L`$. In a similar fashion we define the assignment $`\delta ^{}`$ as

$$\delta ^{}(t):=\{\begin{array}{cc}\text{min}\{\delta _1(t),\delta _2(t)\}\hfill & \text{if }L_{}t<x\hfill \\ \text{max}\{\delta _1(t),\delta _2(t)\}\hfill & \text{if }L_{}tx\hfill \end{array}$$

We can show, as above, that $`\delta ^{}`$ satisfies $`L`$. Clearly $`\delta `$ and $`\delta ^{}`$ are equal on all terms other than $`x`$. One of the assignments $`\delta `$ or $`\delta ^{}`$ maps $`x`$ to $`c_1`$ and one maps $`x`$ to $`c_2`$. Therefore, we have found assignments as required.

###### Proposition 4.7 (Order Decidability of Product)

The aggregation function prod is order-decidable over $`𝐙`$ and over $`𝐐`$.

Proof.  Let $`T=\{t_1,\mathrm{},t_n\}`$ be a set of terms with constants from $`𝐙`$ or $`𝐐`$, and let $`L`$ be a complete ordering of $`T`$. Let $`B`$ and $`B^{}`$ be bags of terms from $`T`$. We will show that it is possible to decide whether

$`L\text{prod}(B)=\text{prod}(B^{})`$ (4)
is valid over $`𝐙`$ or $`𝐐`$, respectively. Clearly, Formula (4) is valid if $`\text{prod}(\delta (B))=\text{prod}(\delta (B^{}))`$ for all assignments $`\delta `$ that satisfy $`L`$.

There may be assignments that satisfy $`L`$ and map variables to the constant 0. It is important to be able to recognize these assignments. Let $`T^{}=T\{\mathrm{\hspace{0.17em}0}\}`$. Let $`L^{}`$ be a complete ordering of $`T^{}`$ that is a conservative extension of $`L`$. Formally, this means that for all terms $`t`$, $`t^{}T`$, the orderings $`L`$ and $`L^{}`$ imply the same relationship between $`t`$ and $`t^{}`$. Note that if $`0T`$, then $`L^{}`$ must be equivalent to $`L`$. There are only finitely many conservative extensions $`L^{}`$ of $`L`$, and an assignment satisfies $`L`$ if and only if it satisfies one of the extensions $`L^{}`$. Thus, to prove our claim, we can assume without loss of generality that $`L`$ in Formula (4) is a complete ordering of a set of terms that contains the constant 0.

Furthermore, we can assume without loss of generality, that in Formula (4) the set of terms $`T`$ is reduced with respect to $`L`$. Otherwise, whenever $`T`$ contains a variable $`y`$ and a term $`t`$ such that $`y`$ and $`t`$ are distinct, but $`L_{}y=t`$, then we replace $`y`$ with $`t`$ for every occurrence of $`y`$ in $`L`$, $`B`$ and $`B^{}`$. Eventually, we end up with a set of terms $`\overline{T}`$, a complete ordering $`\overline{L}`$ of $`\overline{T}`$, and bags $`\overline{B}`$, $`\overline{B}^{}`$ of terms from $`\overline{T}`$ such that $`\overline{T}`$ is reduced with respect to $`\overline{L}`$, and $`\overline{L}\text{prod}(\overline{B})=\text{prod}(\overline{B}^{})`$ is valid if and only if Formula (4) is valid.

Next, we rewrite the equation “$`\text{prod}(B)=\text{prod}(B^{})`$”. We note that for every assignment, $`\text{prod}(B)`$ yields the same value as the polynomial $`cu_1^{m_1}\mathrm{}u_k^{m_k}`$, where

* $`c`$ is the product of all the constants in $`B`$;
* $`u_1,\mathrm{},u_k`$ are all the variables in $`T`$;
* $`m_i`$ is the multiplicity of $`u_i`$ in $`B`$.

Similarly, $`\text{prod}(B^{})`$ yields the same value as some polynomial $`du_1^{n_1}\mathrm{}u_k^{n_k}`$. Now, deciding the validity of Formula (4) amounts to deciding whether the equation

$`c\left(\delta (u_1)\right)^{m_1}\mathrm{}\left(\delta (u_k)\right)^{m_k}=d\left(\delta (u_1)\right)^{m_1}\mathrm{}\left(\delta (u_k)\right)^{m_k}`$ (5)
holds for all assignments $`\delta `$ satisfying $`L`$.

If $`c=d=0`$, then clearly Equation (5) holds for any $`\delta `$. Similarly, Equation (5) holds for any $`\delta `$ if $`c=d`$ and $`m_i=n_i`$ for all $`i`$. We show that if neither of the above conditions holds, then there is an assignment $`\delta `$ that satisfies $`L`$ and for which Equation (5) is not true. We consider two cases.

Case 1. Suppose that $`m_i=n_i`$ for all $`i`$, however $`cd`$. Since $`L`$ is a complete ordering, it is satisfiable. Let $`\delta `$ be an assignment that satisfies $`L`$. Since the set of terms $`T`$ is reduced with respect to $`L`$, and $`0`$ is an element of $`T`$, the ordering $`L`$ imposes a strict inequality between 0 and each variable. Therefore, $`\delta `$ cannot map any variable to the constant 0. Hence, $`\delta `$ is a counterexample to the correctness of Equation (5).

Case 2. Suppose that one of $`c`$ or $`d`$ is non-zero and that there is an index $`i`$ such that $`m_in_i`$. Again, since $`T`$ is reduced with respect to $`L`$, there are at least two possible values for $`u_i`$, say $`c_1`$ and $`c_2`$. Let $`\delta _1`$ and $`\delta _2`$ be assignments that agree on all terms other than $`u_i`$ and that satisfy $`\delta _j(u_i)=c_j`$ for $`j=1`$$`2`$. Such assignments exist according to Lemma 4.6.

As before, $`\delta _1`$ and $`\delta _2`$ cannot map any variable to the constant 0. If $`c=0`$ and $`d0`$, then both $`\delta _1`$ and $`\delta _2`$ are counterexamples to the correctness of Equation (5). Similarly, they are counterexamples if $`c0`$ and $`d=0`$. Therefore, assume that $`c0`$ and $`d0`$.

Suppose, by way of contradiction, that Equation (5) holds for both $`\delta _1`$ and $`\delta _2`$. Then,

$`c\left(\delta _j(u_1)\right)^{m_1}\mathrm{}\left(\delta _j(u_k)\right)^{m_k}=d\left(\delta _j(u_1)\right)^{m_1}\mathrm{}\left(\delta _j(u_k)\right)^{m_k}`$
for $`j=1,2`$. Since the assignment $`\delta _2`$ does not map any variable to 0, we can divide the equation with $`j=1`$ by the equation with $`j=2`$. Note that $`\delta _1`$ and $`\delta _2`$ are equal on all terms other than $`u_i`$. Therefore, after simplifying, we derive

$`\left({\displaystyle \frac{\delta _1(u_i)}{\delta _2(u_i)}}\right)^{m_i}=\left({\displaystyle \frac{\delta _1(u_i)}{\delta _2(u_i)}}\right)^{n_i}.`$ (6)
Note that $`\delta _1(u_i)/\delta _2(u_i)0`$, since $`\delta _1`$ does not map any variable to the constant 0. In addition, $`\delta _1(u_i)/\delta _2(u_i)1`$, since $`\delta _1`$ and $`\delta _2`$ differ on $`u_i`$.

Finally, $`\delta _1(u_i)/\delta _2(u_i)1`$, since $`\delta _1`$ and $`\delta _2`$ must both map $`u_i`$ to positive numbers or both map $`u_i`$ to negative numbers. Therefore, Equation (6) cannot hold and either $`\delta _1`$ or $`\delta _2`$ is a counterexample to the correctness of Equation (5). This completes Case 2.

Thus, we have shown how to decide the validity of Formula (4) over both, the integers and the rational numbers. This completes the proof.

### 4.3 Decidability of Bounded Equivalence

It is possible to show that bounded equivalence can be decided for $`\alpha `$-queries containing comparisons that range over $``$ if the aggregation function $`\alpha `$ is order-decidable over $``$. Actually, bounded equivalence for $`\alpha `$-queries ranging over $``$ is decidable if and only if $`\alpha `$ is order-decidable over $``$. This gives a complete characterization of decidability of bounded equivalence of aggregate queries with negation, disjunction, constants and comparisons. In addition, we derive as a direct result that bounded equivalence is decidable for queries with a wide range of common aggregation functions.

###### Theorem 4.8 (Bounded Equivalence and Order-Decidability)

Let $`\alpha `$ be an aggregation function over $`^k`$. Then the bounded equivalence problem is decidable for disjunctive $`\alpha `$-queries with comparisons ranging over $``$ if and only if $`\alpha `$ is order-decidable over $``$.

Proof. $``$” Suppose that $`\alpha `$ is order-decidable over $``$. Consider $`\alpha `$-queries $`q`$ and $`q^{}`$. We show how to check, given some $`N>0`$, whether $`q_Nq^{}`$.

Let $`C`$ be the set of constants appearing in $`q`$ or $`q^{}`$ and let $`U`$ be a set of $`N`$ variables. We use $`T`$ to denote $`CU`$. Let $`P`$ be the set of predicates appearing either in $`q`$ or in $`q^{}`$. The set $`P`$ contains predicates that appear either positively or negatively in the queries. We use $`\text{ary}(p)`$ to denote the arity of a predicate $`pP`$. We denote by BASE the set of all atoms that can be created using the terms in $`T`$ and the predicates in $`P`$. Formally,

$`\text{BASE}:=\{p(t_1,\mathrm{},t_{\text{ary}(p)})pP\text{ and }t_1,\mathrm{},t_{\text{ary}(p)}T\}.`$
If $`\delta `$ is an assignment that maps variables in $`U`$ to elements of $``$, and if $`S`$ is a subset of BASE, then instantiating $`S`$ by $`\delta `$ results in a database $`\delta (S)`$, the carrier of which has at most $`N`$ elements. To decide whether $`q_Nq^{}`$ it is sufficient to evaluate the queries over databases of the form $`\delta (S)`$ where $`S\text{BASE}`$. Essentially, if we consider only databases of this form, we rule out databases containing predicates not appearing in $`q`$ or $`q^{}`$. Clearly, such predicates cannot affect the evaluation of $`q`$ and $`q^{}`$.

Consider now a fixed subset $`S\text{BASE}`$. We will show how to check whether $`q`$ and $`q^{}`$ return the same results over all instantiations $`\delta (S)`$. Since there exist infinitely many instantiations, we cannot check each of them separately. Instead, we divide the instantiations into finitely many equivalence classes over which we can decide the equivalence of $`q`$ and $`q^{}`$. The equivalence classes are defined by the complete orderings $`L`$ of $`T`$, that is, for each $`L`$ we simultaneously check all instantiations $`\delta (S)`$ where $`\delta `$ satisfies $`L`$.

In addition to $`S`$, consider a complete ordering $`L`$ of $`T`$. Instead of an instantiation $`\delta (S)`$, we attempt to evaluate $`q`$ and $`q^{}`$ immediately over $`S`$, based on the ordering of terms defined by $`L`$. We view the set $`S`$ equipped with this ordering as a database, denoted $`S_L`$. Obviously, given $`S`$ and $`L`$, we can compute the bags containing the tuples returned by $`q`$ and $`q^{}`$. However, it is impossible to compute the values of the aggregation function $`\alpha `$ for these bags because $`T`$, and therefore the bags, may contain variables. At this point, we make use of the fact that $`\alpha `$ is order-decidable over $``$.

Suppose that the tuple of grouping variables $`\overline{x}`$ in $`q(\overline{x},\alpha (\overline{y}))`$ and $`q(\overline{x},\alpha (\overline{y}^{}))`$ has length $`k`$. Let $`\overline{t}`$ be a $`k`$-tuple of terms in $`T`$. Note that there are only finitely many such tuples because $`T`$ is finite. Recall that $`\mathrm{\Gamma }_{\overline{t}}(q,S_L)`$ is the set of assignments $`\gamma `$ that satisfy $`q`$ over $`S_L`$ and where $`\gamma (\overline{x})=\overline{t}`$. Consider the bag $`B_{\overline{t}}`$ defined as

$$B_{\overline{t}}:=\mathrm{\Gamma }_{\overline{t}}(q,S_L)_{|\overline{y}},$$

that is, $`B_{\overline{t}}`$ consists of the restrictions of elements of $`\mathrm{\Gamma }_{\overline{t}}(q,S_L)`$ to the variables in $`\overline{y}`$. Let $`B_{\overline{t}}^{}`$ be defined analogously for $`q^{}`$.

Now, assume that there is an assignment $`\delta `$ that satisfies $`L`$ such that over the database $`\delta (S)`$ the queries $`q`$ and $`q^{}`$ do not return the same aggregate value for $`\delta (\overline{t})`$. This is the case if and only if the formula

$$L\alpha (B_{\overline{t}})=\alpha (B_{\overline{t}}^{})$$

is not valid over $``$. Since $`\alpha `$ is order-decidable over $``$, the validity of the formula can be determined.

$``$” We show that if bounded equivalence of $`\alpha `$-queries ranging over $``$ is decidable, then $`\alpha `$ is order-decidable over $``$. To simplify our notation, we assume without loss of generality that $`\alpha `$ is a unary function. Let $`T=\{t_1,\mathrm{},t_N\}`$ be a set of terms, $`L`$ be a complete ordering of $`T`$, and $`B`$ and $`B^{}`$ be bags of terms from $`T`$. We will construct $`\alpha `$-queries $`q`$ and $`q^{}`$ such that

$$L\alpha (B)=\alpha (B^{})$$
(7)
is valid over $``$ if and only if $`q_Nq^{}`$.

We assume without loss of generality that $`L`$ does not equate two different terms. Otherwise, we could remove one of them by renaming. We define the condition $`A`$ as

$$A:=p(t_1)p(t_2)\mathrm{}p(t_N).$$

Suppose that $`B=\{\{s_1,\mathrm{},s_m\}\}`$ and $`B^{}=\{\{s_1^{},\mathrm{},s_n^{}\}\}`$. We assume that $`y`$ is a new variable, i.e., $`y`$ does not appear in $`B`$ or $`B^{}`$. We define the conditions

$`A_i`$ $`:=ALy=s_i,`$ $`i`$ $`=1,\mathrm{},m`$
$`A_j^{}`$ $`:=ALy=s_j^{},`$ $`j`$ $`=1,\mathrm{},n.`$
and we define the queries $`q`$ and $`q^{}`$ by

$$q(\alpha (y))\underset{i=1}{\overset{m}{}}A_i\text{and}q^{}(\alpha (y))\underset{j=1}{\overset{n}{}}A_j^{}.$$

Suppose that $`q_Nq^{}`$. We prove this implies that Formula (7) is valid. Let $`\delta `$ be an arbitrary assignment that satisfies $`L`$. We show that $`\alpha (\delta (B))=\alpha (\delta (B^{}))`$. Consider the database $`𝒟`$ obtained by instantiating $`A`$ with $`\delta `$, i.e.,

$$𝒟:=\{p(\delta (t_1)),\mathrm{},p(\delta (t_N))\}.$$

Clearly, $`q`$ and $`q^{}`$ retrieve $`\alpha (\delta (B))`$ and $`\alpha (\delta (B^{}))`$, respectively, over $`𝒟`$. The database $`𝒟`$ contains at most $`N`$ constants. Thus, since $`q_Nq^{}`$, the two aggregates are equal.

Now suppose that Formula (7) is valid. We prove this implies that $`q_Nq^{}`$. Let $`𝒟`$ be an arbitrary database containing at most $`N`$ constants. If $`𝒟`$ contains less than $`N`$ values, then no assignment over $`𝒟`$ can satisfy $`L`$, because $`L`$ is a complete ordering that does not equate variables. Hence, $`q`$ and $`q^{}`$ will not return any value over $`𝒟`$.

Therefore, assume that $`𝒟`$ contains exactly $`N`$ values. It is easy to see that $`q`$ is satisfiable over $`𝒟`$ if and only if $`q^{}`$ is satisfiable over $`𝒟`$. In such a case, for each condition $`A_i`$ or $`A_j^{}`$ there is exactly one satisfying assignment over $`𝒟`$, say, $`\gamma _i`$ or $`\gamma _j^{}`$, respectively. Moreover, the assignments $`\gamma _i`$ and $`\gamma _j^{}`$ agree on all variables except for $`y`$. That is, there is an assignment $`\delta `$ for the variables in $`T`$ such that $`\gamma _i`$ and $`\gamma _j^{}`$ agree with $`\delta `$ on $`T`$, for all $`i`$ and $`j`$. Due to the definition of $`A_i`$ and $`A_j^{}`$, we also have $`\gamma _i(y)=\delta (s_i)`$ and $`\gamma _j^{}(y)=\delta (s_i^{})`$. Thus, $`q`$ collects over $`𝒟`$ the bag $`\{\{\gamma _1(y),\mathrm{},\gamma _m(y)\}\}=\{\{\delta (s_1),\mathrm{},\delta (s_m)\}\}=\delta (B)`$ and returns the aggregate $`\alpha (\delta (B))`$. Similarly, $`q^{}`$ returns the aggregate $`\alpha (\delta (B^{}))`$. From the assumption that Formula (7) is valid, and the fact that $`\delta `$ satisfies $`L`$, it follows that $`\alpha (\delta (B))=\alpha (\delta (B^{}))`$. Hence, $`q`$ and $`q^{}`$ return the same values over $`𝒟`$. Since $`𝒟`$ was chosen arbitrarily, this proves that $`q_Nq^{}`$.

From the proof of Theorem 4.8 we can derive an upper bound on the complexity of determining whether $`q_Nq^{}`$. In fact, the proof describes a procedure for checking $`N`$-equivalence. Suppose that there are $`C`$ constants in $`q`$ and $`q^{}`$. Let $`T:=C+N`$. As the first step of the procedure, the set BASE is created. This set contains all possible instantiations of the atoms in $`q`$ and $`q^{}`$ with the $`C`$ constants and with $`N`$ variables. Clearly, the cardinality of this set is exponential in $`T=C+N`$. Then, each subset $`S`$ of BASE is considered. The number of such subsets is, thus, double exponential in $`T`$. A subset $`S`$ is considered in conjunction with a complete ordering $`L`$ of the $`T`$ terms. Note that there are at most $`2^{T1}T!`$ complete orderings of $`T`$ terms. (This is a rough upper bound, since we can arrange the $`T`$ terms in $`T!`$ orders and then place a “$`<`$” or “$`=`$” sign between each pair.) Thus, considering all complete orderings does not affect the already double exponential order of complexity.

For each pair $`S`$, $`L`$ we evaluate $`q`$ and $`q^{}`$. Evaluating $`q`$ roughly takes time $`T^{|q|}`$, where $`|q|`$ is the size of $`q`$, since we must try all instantiations of the terms in $`q`$. This too does not affect the order of complexity, since the computation time is only exponential, while there are a double exponential number of subsets $`S`$. For each tuple $`\overline{t}`$ that instantiates the grouping variables and thus defines a group, we check the validity of the ordered identity defined in Formula (3) for $`L`$ and the bags created.

Each bag can have at most $`T^{|q|}`$ many elements. However, the number of different elements is bounded by $`T^k`$, where $`k`$ is the arity of the aggregation function $`\alpha `$. Therefore, to represent a bag we only need space polynomial in $`T`$. Thus, the size of the ordered identities to be checked for validity is polynomial in $`T`$. As long as this check takes no more than double exponential time, the overall complexity is at most double exponential. In many cases, this step is much more efficient. For example, given the aggregation function count, this step only requires checking the cardinality of the bags, and hence, is linear.

To summarize, if the validity of ordered identities of the form (3) can be checked in double exponential time, we derive a double exponential upper bound for the complexity of checking $`N`$-equivalence.

The following corollary follows directly from Theorems 4.4 and 4.8.

###### Corollary 4.9 (Bounded Equivalence and Shiftable Functions)

Let $`\alpha `$ be a shiftable aggregation function over $`^k`$. Then for disjunctive $`\alpha `$-queries with comparisons ranging over $``$ the bounded equivalence problem is decidable.

###### Corollary 4.10 (Decidable Query Classes)

For the classes of disjunctive max, sum, prod, avg, cntd, count, parity and top$`2`$ queries, bounded equivalence is decidable provided that the comparisons range over $`𝐐`$ or $`𝐙`$.

Proof.  For the classes of disjunctive max, cntd, count, parity and top$`2`$, the claim follows directly from Corollary 4.9. For disjunctive sum and avg queries, decidability follows from Proposition 4.5 and Theorem 4.8. Similarly, for disjunctive prod queries, the claim follows from Proposition 4.7 and Theorem 4.8.

In Theorem 4.8 we reduced order-decidability to bounded equivalence. A point of interest is that in our reduction we only used positive queries. Therefore, negation in queries $`q`$ and $`q^{}`$ does not affect decidability of bounded equivalence of $`q`$ and $`q^{}`$.

###### Corollary 4.11 (Bounded Equivalence of Queries Without Negation)

The bounded equivalence problem is decidable for positive disjunctive $`\alpha `$-queries with comparisons ranging over $``$ if and only if the bounded equivalence problem is decidable for disjunctive $`\alpha `$-queries with negation and comparisons ranging over $``$.

## 5 Decomposition Principles

Levy and Sagiv  have shown that two disjunctive non-aggregate queries are equivalent if they are equivalent over all databases whose carrier is not greater than the size of the queries. For non-aggregate queries this is not surprising since an answer by a query $`q`$ depends only on a single assignment satisfying $`q`$. Hence, if over some database $`𝒟`$, by means of the assignment $`\gamma `$, the query $`q`$ returns the tuple $`\overline{d}`$, but $`q^{}`$ does not return $`\overline{d}`$, then we can construct a database $`𝒟_0𝒟`$ that contains only constants occurring in $`q`$, $`q^{}`$ and $`\gamma `$ such that $`\overline{d}q^{𝒟_0}`$ and $`\overline{d}q_{}^{}{}_{}{}^{𝒟_0}`$.

For aggregate queries this argument cannot be applied since the results of a query are the amalgamation of many single results that may involve arbitrarily many constants in the database. Nevertheless, for queries with an idempotent monoid or a group aggregation function we can reduce equivalence over arbitrary databases to equivalence over small databases.

As a first step, we formulate decomposition principles for these two classes of functions. Such a principle provides a method to compute the value of an aggregation over a union of sets of assignments from aggregations over the sets themselves and possibly some of their subsets.

Note that the “$``$” in the equation below is the extension of the idempotent monoid operation. In the case of max, for instance, the right hand side of the equation becomes $`\mathrm{max}_{i=1}^k(\text{max}(y)A_i)`$.

###### Proposition 5.1 (Idempotent Decomposition Principle)

Let $`\alpha `$ be an idempotent monoid aggregation function and $`(A_i)_{i=1}^k`$ a family of sets of assignments, all defined for $`\overline{y}`$. Then

$`\alpha (\overline{y})_{i=1}^kA_i=_{i=1}^k(\alpha (\overline{y})A_i)\text{.}`$ (8)
Proof.  Let $`(A_i)_{i=1}^k`$ be a family of sets of assignments. On the left hand side of Equation (8), first the union of the $`A_i`$ is taken and $`\alpha `$ is applied afterwards. On the right hand side, $`\alpha `$ is applied first to (a projection of) each set $`A_i`$, and then the sum of the results is taken. Of course, it is possible for two different sets $`A_i`$ and $`A_j`$ to contain common elements. Note that on the left hand side, these duplicates are removed, whereas in the right side they are preserved.

The aggregation function $`\alpha `$ is associative and commutative. Therefore, the order in which $`\alpha `$ is applied to the assignments is not important. Hence, the only difference between the two sides of Equation (8) that might affect the result is that on the right side the same assignment may appear in the summation several times.

However, because of the associativity and commutativity of the monoid operation, we may first sum such assignments with themselves. Since $`\alpha `$ is idempotent, the final result is the same as if each element had occurred only once. Therefore, Equation (8) holds.

Before we treat the case of group aggregation functions, we remind the reader of the well-known Principle of Inclusion and Exclusion for computing the cardinality of a union of sets. It says that for any finite family of sets $`(A_i)_{i=1}^k`$ we have

$`\left|_{i=1}^kA_i\right|`$ $`=_{i=1}^k|A_i|_{i<j}|A_iA_j|+\mathrm{}+\text{}(1)^{k1}|_{i=1}^kA_i|.\text{}`$ (9)
For group aggregation functions, we can generalize Equation (9). In the following decomposition principle, the “$``$”-sign denotes the inverse with respect to the group operation. Note that for $`\alpha =\text{count}`$, Equation (10) simplifies to Equation (9), since $`(\text{count}A)=|A|`$ for every set of assignments $`A`$.

###### Proposition 5.2 (Group Decomposition Principle)

Suppose that $`\alpha `$ is a group aggregation function and $`(A_i)_{i=1}^k`$ is a finite family of sets of assignments, all defined for $`\overline{y}`$. Then

$`\begin{array}{cc}\hfill \alpha (\overline{y})_{i=1}^kA_i=& _{i=1}^k(\alpha (\overline{y})A_i)_{i<j}(\alpha (\overline{y})A_iA_j)+\mathrm{}+\text{}\hfill \\ & (1)^{k1}(\alpha (\overline{y})_{i=1}^kA_i).\hfill \end{array}`$ (10)
Proof.  Equation (10) can be proved in the same fashion that the Principle of Inclusion and Exclusion is proved. Note that the right hand side of the equation is well defined since $`\alpha `$ takes values in an abelian group.

Let $`\gamma `$ be an assignment. If $`\gamma `$ is not in $`A_i`$ for any $`i`$, then clearly, $`\gamma `$ does not affect the value on the left or the right hand side of Equation (10).

Suppose that $`\gamma `$ is in $`r`$ different sets $`A_i`$. The assignment $`\gamma `$ contributes $`\gamma (\overline{y})`$ once to the union of sets to which $`\alpha `$ is applied on the left hand side on the Equation. On the right hand side of the Equation, since $`\gamma `$ is in $`r`$ different sets $`A_i`$, the value $`\gamma (\overline{y})`$ is added and subtracted (possibly) several times. To prove equality of the two sides, it is sufficient to show that on the right hand side $`\gamma (\overline{y})`$ is added exactly one more time than it is subtracted.

Since $`\gamma `$ occurs in $`r`$ sets $`A_i`$, it occurs in $`\left(\genfrac{}{}{0pt}{}{r}{2}\right)`$ intersections of two sets, $`\left(\genfrac{}{}{0pt}{}{r}{3}\right)`$ intersections of three sets, etc. Thus, on the right hand side, $`\gamma (\overline{y})`$ is added $`r`$ times, then subtracted $`\left(\genfrac{}{}{0pt}{}{r}{2}\right)`$ times, then added $`\left(\genfrac{}{}{0pt}{}{r}{3}\right)`$ times, etc., In total, $`\gamma `$ contributes the tuple $`\gamma (\overline{y})`$

$$\left(\genfrac{}{}{0pt}{}{r}{1}\right)\left(\genfrac{}{}{0pt}{}{r}{2}\right)+\mathrm{}+(1)^{r1}\left(\genfrac{}{}{0pt}{}{r}{r}\right)=1$$

times to the right hand side of Equation (10), as required.

Because of the above two propositions, we say that idempotent monoid and group aggregation functions are decomposable.

## 6 Reducing Equivalence to Local Equivalence

We now show that for queries with decomposable aggregation functions, local equivalence implies equivalence. To this end we first show that, given two queries and a database, we can identify small subsets of the database, such that the satisfying assignments over the database are the union of the satisfying assignments over the subsets. Then we apply the decomposition principles to conclude that the queries return the same result over the original database from the fact that the queries return the same results over the small databases.

Let $`q_1`$ and $`q_2`$ be disjunctive queries, $`𝒟`$ be a database, and $`\overline{d}`$ be a tuple of constants. Let $`(𝒟_i)_{i=1}^k`$ be a family of databases with $`𝒟_i𝒟`$ for all $`i=1,\mathrm{},k`$. Then $`(𝒟_i)_{i=1}^k`$ is a decomposition of $`𝒟`$ with respect to $`q_1`$, $`q_2`$ and $`\overline{d}`$ if the following holds:

1. $`|\text{carr}(𝒟_i)|\tau (q_1,q_2)`$ for all $`i=1,\mathrm{},k`$;
2. $`\mathrm{\Gamma }_{\overline{d}}(q_j,𝒟)=_{i=1}^k\mathrm{\Gamma }_{\overline{d}}(q_j,𝒟_i)`$ for $`j=1`$$`2`$;
3. $`_h\mathrm{\Gamma }_{\overline{d}}(q_j,𝒟_{i_h})=\mathrm{\Gamma }_{\overline{d}}(q_j,_h𝒟_{i_h})`$ for $`j=1`$$`2`$ and for all subfamilies $`(𝒟_{i_h})_h`$ of $`(𝒟_i)_i`$.

The first condition means that, intuitively, the databases $`𝒟_i`$ are small. The second condition says that for each $`q_j`$, $`j=1`$$`2`$, we obtain exactly the satisfying assignments over $`𝒟`$ that return $`\overline{d}`$ if we evaluate $`q_j`$ over each $`𝒟_i`$ separately and select the assignments that return $`\overline{d}`$ over $`𝒟_i`$. The third condition says that for each $`q_j`$, in order to obtain the intersection of the assignment sets $`\mathrm{\Gamma }_{\overline{d}}(q_j,𝒟_{i_h})`$, it suffices to evaluate $`q_j`$ over the intersection of the databases $`𝒟_{i_h}`$.

We will prove that given a pair of queries $`q`$ and $`q^{}`$, a database $`𝒟`$ and a tuple $`\overline{d}`$, there exists a decomposition of $`𝒟`$ with respect to $`q`$, $`q^{}`$ and $`\overline{d}`$. To this end, we will first prove a series of lemmas.

We consider queries $`q`$ and $`q^{}`$ defined as

$`q(\overline{x},\alpha (\overline{y}))`$ $`{\displaystyle \underset{iI}{}}P_iN_iC_i`$
$`q^{}(\overline{x},\alpha (\overline{y}^{}))`$ $`{\displaystyle \underset{jJ}{}}P_j^{}N_j^{}C_j^{}`$
where $`P_i`$ and $`P_j^{}`$ are conjunctions of positive relational atoms, $`N_i`$ and $`N_j^{}`$ are conjunctions of negated relational atoms and $`C_i`$ and $`C_j^{}`$ are conjunctions of comparisons. We use $`A_i`$ as a shorthand for $`P_iN_iC_i`$ and we use $`A_j^{}`$ as a shorthand for $`P_j^{}N_j^{}C_j^{}`$.

Let $`𝒟`$ be a database and let $`\overline{d}`$ be a tuple. We must show that there exists a decomposition of $`𝒟`$ with respect to $`q`$, $`q^{}`$ and $`\overline{d}`$.

We create a decomposition of $`𝒟`$ with respect to $`q`$$`q^{}`$ and $`\overline{d}`$ in a two-step process. We first create databases out of the satisfying assignments of $`q`$ and of $`q^{}`$ into $`𝒟`$ that retrieve $`\overline{d}`$. Next, we extend these databases using the procedure Extend\_Database to prevent them from satisfying negated atoms that were not satisfied in $`𝒟`$.

Recall that $`\mathrm{\Gamma }_{\overline{d}}(q,𝒟)`$ is the set of satisfying assignments from $`q`$ into $`𝒟`$ that retrieve $`\overline{d}`$. We denote the disjunct of $`q`$ satisfied by $`\gamma \mathrm{\Gamma }_{\overline{d}}(q,𝒟)`$ as $`A_\gamma `$. For each $`\gamma \mathrm{\Gamma }_{\overline{d}}(q,𝒟)`$, we define a database

$$𝒟_\gamma :=\{\gamma (a)aP_\gamma \}.$$

We use this notation since we consider a database to be a set of ground positive relational atoms. Note that $`𝒟_\gamma `$ satisfies the positive atoms in $`A_\gamma `$ with respect to the assignment $`\gamma `$. However, we must extend the databases $`𝒟_\gamma `$ to ensure that $`𝒟_\gamma `$ does not satisfy negated atoms that were not satisfied in $`𝒟`$. We now create a database $`𝒟_\gamma ^{}`$ out of $`𝒟_\gamma `$ using the procedure Extend\_Database presented in Figure 1.333This process does not necessarily uniquely determine the database $`D_\gamma ^{}`$. However, this is not important for our proof.  Formally, we define $`𝒟_\gamma ^{}:=\text{Extend\_Database}(𝒟_\gamma ,q,q^{},𝒟)`$.

In a similar fashion, we create databases $`𝒟_\gamma ^{}`$ out of the satisfying assignments $`\gamma ^{}\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟)`$ of $`q^{}`$ into $`𝒟`$ that retrieve $`\overline{d}`$. As above, these databases are extended to derive databases $`𝒟_\gamma ^{}^{}`$ using the procedure Extend\_Database.

We now define

$$\mathrm{\Delta }:=\{𝒟_\gamma ^{}\gamma \mathrm{\Gamma }_{\overline{d}}(q,𝒟)\}\{𝒟_\gamma ^{}^{}\gamma ^{}\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟)\}.$$
(11)
We present a series of lemmas that will enable us to prove that $`\mathrm{\Delta }`$ is a decomposition of $`𝒟`$ w.r.t. $`q`$, $`q^{}`$ and $`\overline{d}`$. We first note that clearly for all $`𝒟^{}\mathrm{\Delta }`$, it holds that $`𝒟^{}𝒟`$.

The first lemma states that the databases in $`\mathrm{\Delta }`$ have the correct number of constants, i.e., that Property 1 of decompositions holds for $`\mathrm{\Delta }`$.

###### Lemma 6.1 (Size of Databases)

For all databases $`𝒟^{}\mathrm{\Delta }`$, it holds that $`|\text{carr}(𝒟^{})|\tau (q,q^{})`$.

Proof.  Consider a database $`𝒟_\gamma ^{}\mathrm{\Delta }`$. Clearly, $`𝒟_\gamma `$ contains at most $`\tau (q)`$ constants. Note that when an atom is added during the procedure, the constants appearing in the atom must have already appeared in the database $`𝒟_\gamma `$, or must appear in $`q`$ or $`q^{}`$. This follows since the queries are safe and all variables in negated atoms must also appear in positive atoms (or be equated to variables appearing in positive atoms). Thus, $`𝒟_\gamma ^{}`$ contains at most $`\tau (q,q^{})`$ constants.

Similarly, one can show that for $`𝒟_\gamma ^{}^{}\mathrm{\Delta }`$, it holds that $`|\text{carr}(𝒟_\gamma ^{}^{})|\tau (q,q^{})`$. Thus, it easily follows that for all $`𝒟^{}\mathrm{\Delta }`$, we have $`|\text{carr}(𝒟^{})|\tau (q,q^{})`$.

We show Property 2 of decompositions for $`\mathrm{\Delta }`$.

###### Lemma 6.2 (Assignments into $`𝒟`$ and $`\mathrm{\Delta }`$)

The following relationships hold between the assignments of $`q`$ and $`q^{}`$ into $`𝒟`$ and into databases in $`\mathrm{\Delta }`$:

1. $`\mathrm{\Gamma }_{\overline{d}}(q,𝒟)=_{𝒟^{}\mathrm{\Delta }}\mathrm{\Gamma }_{\overline{d}}(q,𝒟^{})`$;
2. $`\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟)=_{𝒟^{}\mathrm{\Delta }}\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟^{})`$.

Proof.  We only prove Part 1. Part 2 can be shown analogously. We show the set equality in Part 1 by proving two inclusions.

$``$” Suppose that $`\gamma \mathrm{\Gamma }_{\overline{d}}(q,𝒟)`$. It is enough to show that $`\gamma `$ satisfies $`q`$ over $`𝒟_\gamma ^{}`$, which entails that $`\gamma \mathrm{\Gamma }_{\overline{d}}(q,𝒟_\gamma ^{})`$.

Let $`a`$ be a positive relational atom in the conjunct $`A_\gamma `$. Then $`\gamma (a)𝒟_\gamma `$ by definition. Clearly, $`𝒟_\gamma 𝒟_\gamma ^{}`$, and thus, $`\gamma (a)𝒟_\gamma ^{}`$. If $`\neg b`$ is a negated relational atom in $`A_\gamma `$ then $`\gamma (b)𝒟`$. Otherwise, $`\gamma `$ would not be a satisfying assignment of $`q`$ in $`𝒟`$. According to the definition of $`𝒟_\gamma ^{}`$, it holds that $`𝒟_\gamma ^{}𝒟`$, and therefore, $`\gamma (b)𝒟_\gamma ^{}`$. The satisfaction of $`C_\gamma `$ (the comparisons in $`A_\gamma `$) depends only on $`\gamma `$ and not on any database. Thus, $`\gamma `$ is a satisfying assignment of $`q`$ over $`𝒟_\gamma ^{}`$.

$``$” It suffices to show that for all $`𝒟^{}\mathrm{\Delta }`$ it holds that $`\mathrm{\Gamma }_{\overline{d}}(q,𝒟)\mathrm{\Gamma }_{\overline{d}}(q,𝒟^{})`$. Suppose that $`\gamma \mathrm{\Gamma }_{\overline{d}}(q,𝒟^{})`$. We show that $`\gamma `$ is a satisfying assignment of $`q`$ over $`𝒟`$.

Suppose that $`\gamma `$ satisfies the conjunct $`A_\gamma `$ of $`q`$ in $`𝒟^{}`$. Consider a literal $`l`$ in $`A_\gamma `$. If $`l`$ is a positive relational atom then $`\gamma (l)𝒟^{}`$. We know that $`𝒟^{}𝒟`$, thus, $`\gamma (l)𝒟`$. Suppose that $`l`$ is a negated relational atom of the form $`\neg b`$, and suppose, by way of contradiction, that $`\gamma (b)𝒟`$. Then $`\gamma `$ satisfies the condition in line 6 of the procedure Extend\_Database presented above. Thus, $`\gamma (b)`$ would have been added to $`𝒟^{}`$ in contradiction to the fact that $`\gamma `$ is a satisfying assignment of $`q`$ into $`𝒟^{}`$. Finally, note that the satisfaction of comparisons depends only on the assignment, and not on the database. Thus, $`\gamma `$ is a satisfying assignment of $`q`$ over $`𝒟`$.

In Lemma 6.3, Property 3 of decompositions is proved for $`\mathrm{\Delta }`$.

###### Lemma 6.3 (Assignments into Intersections)

The following relationships hold between intersections of sets of assignments and intersections of sets of databases:

1. $`_h\mathrm{\Gamma }_{\overline{d}}(q,𝒟_h^{})=\mathrm{\Gamma }_{\overline{d}}(q,_h𝒟_h^{})`$;
2. $`_h\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟_h^{})=\mathrm{\Gamma }_{\overline{d}}(q^{},_h𝒟_h^{})`$

for all subfamilies $`𝒟_h^{}`$ of $`\mathrm{\Delta }`$.

Proof.  We only prove Part 1. Part 2 can be shown analogously. We show the set equality in Part 1 by proving two inclusions.

$``$” Let $`\gamma `$ be an assignment in $`\mathrm{\Gamma }_{\overline{d}}(q,𝒟_h^{})`$. Suppose that $`\gamma `$ satisfies $`A_\gamma `$ of $`q`$ in $`𝒟_h^{}`$. Satisfaction of $`C_\gamma `$ is dependent only on $`\gamma `$. Let $`a`$ be a positive atom in $`A_\gamma `$. The atom $`\gamma (a)`$ appears in $`𝒟_h^{}`$ and thus, $`\gamma (a)`$ appears in $`𝒟_h^{}`$ for all $`h`$. Thus, $`\gamma `$ satisfies the positive atoms of $`A_\gamma `$ in each of the $`𝒟_h^{}`$. Now, let $`l`$ be a negated atom in $`A_\gamma `$ of the form $`\neg b`$. Clearly, $`\gamma (b)𝒟_h^{}`$. Suppose, by way of contradiction, that $`\gamma (b)𝒟_h^{}`$ for some $`h`$. Then $`\gamma (b)𝒟`$, since $`𝒟_h^{}𝒟`$. However, it follows that we would have added $`\gamma (b)`$ to $`𝒟_h^{}`$ for all $`h`$, since $`\gamma `$ satisfies the condition in line 6 of Extend\_Database. Thus, $`\gamma (b)𝒟_h^{}`$ in contradiction to the assumption. This proves that $`\gamma \mathrm{\Gamma }_{\overline{d}}(q,𝒟_h^{})`$. Hence, we have shown that every $`\gamma `$ that is an element of the right hand side of Equation (1) is also an element of the left hand side.

$``$” Suppose that $`\gamma \mathrm{\Gamma }_{\overline{d}}(q,𝒟_h^{})`$. Then $`\gamma \mathrm{\Gamma }_{\overline{d}}(q,𝒟_h^{})`$ for all $`h`$. Let $`A_\gamma `$ be a conjunct such that $`\gamma `$ satisfies $`A_\gamma `$ in $`𝒟_h^{}`$ for all $`h`$. (Recall the definition of $`\mathrm{\Gamma }(q,𝒟)`$ in Section 3.4, where we have assumed that each $`\gamma `$ carries a label, recording which disjunct of $`q`$ it satisfies.) Once again, satisfaction of $`C_\gamma `$ is dependent only on $`\gamma `$. Consider a positive relational atom $`a`$ in $`A_\gamma `$. Then $`\gamma (a)𝒟_h^{}`$ for all $`h`$. Thus, $`\gamma (a)𝒟_h^{}`$. Similarly, consider a negated atom $`l`$ of the form $`\neg b`$ in $`A_\gamma `$. Then $`\gamma (b)𝒟_h^{}`$ for all $`h`$, and thus, $`\gamma (b)𝒟_h^{}`$. This proves that $`\gamma \mathrm{\Gamma }_{\overline{d}}(q,𝒟_h^{})`$. Hence, the second inclusion holds as well.

We can now prove our theorem about the existence of decompositions.

###### Theorem 6.4 (Existence of Database Decompositions)

Let $`q`$ and $`q^{}`$ be a pair of disjunctive queries, let $`𝒟`$ be a database, and let $`\overline{d}`$ be a tuple of constants from $`𝒟`$. Then there exists a decomposition of $`𝒟`$ with respect to $`q`$$`q^{}`$ and $`\overline{d}`$.

Proof.  From Lemmas 6.16.2 and 6.3 it follows that $`\mathrm{\Delta }`$ as defined in Equation (11) is a decomposition of $`𝒟`$ with respect to $`q`$, $`q^{}`$ and $`\overline{d}`$ as required.

Finally, we reduce equivalence to local equivalence.

###### Theorem 6.5 (Reduction to Local Equivalence)

Let $`\alpha `$ be a decomposable aggregation function, and let $`q`$ and $`q^{}`$ be disjunctive $`\alpha `$-queries. Then $`q`$ and $`q^{}`$ are equivalent if and only if they are locally equivalent.

Proof.  We only have to show that local equivalence implies equivalence. Suppose therefore that $`q`$ and $`q^{}`$ agree on all databases whose carrier has at most $`\tau (q,q^{})`$ elements. Let $`𝒟`$ be any database and $`\overline{d}`$ be a tuple of constants. It suffices to show that

$$\alpha (\overline{y})\mathrm{\Gamma }_{\overline{d}}(q,𝒟)=\alpha (\overline{y})\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟).$$

Let $`(𝒟_i)_{i=1}^k`$ be a decomposition of $`𝒟`$ with respect to $`q`$, $`q^{}`$ and to $`\overline{d}`$. If $`\alpha `$ is an idempotent monoid function, we apply Proposition 5.1, which yields

$`\alpha (\overline{y})\mathrm{\Gamma }_{\overline{d}}(q,𝒟)`$ $`=\alpha (\overline{y})_{i=1}^k\mathrm{\Gamma }_{\overline{d}}(q,𝒟_i)\text{}`$ (12a)
$`=_{i=1}^k(\alpha (\overline{y})\mathrm{\Gamma }_{\overline{d}}(q,𝒟_i))\text{}`$ (12b)
$`=_{i=1}^k(\alpha (\overline{y})\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟_i))\text{}`$ (12c)
$`=\alpha (\overline{y})_{i=1}^k\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟_i)\text{}`$ (12d)
$`=\alpha (\overline{y})\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟),\text{}`$ (12e)
where Equations (12a) and (12e) hold because of Property 2 of decompositions, Equations (12b) and (12d) hold because of Proposition 5.1, and Equation (12c) holds because $`q`$ and $`q^{}`$ are locally equivalent and the databases $`𝒟_i`$ contain at most $`\tau (q,q^{})`$ constants.

If $`\alpha `$ is a group aggregation function, we apply Proposition 5.2, which yields the equations

$`\alpha (\overline{y})\mathrm{\Gamma }_{\overline{d}}(q,𝒟)`$ $`=\alpha (\overline{y})_{i=1}^k\mathrm{\Gamma }_{\overline{d}}(q,𝒟_i)\text{}`$ (13a)
$`=_{i=1}^k(\alpha (\overline{y})\mathrm{\Gamma }_{\overline{d}}(q,𝒟_i))\mathrm{}+(1)^{k1}(\alpha (\overline{y})_{i=1}^k\mathrm{\Gamma }_{\overline{d}}(q,𝒟_i))\text{}`$ (13b)
$`=_{i=1}^k(\alpha (\overline{y})\mathrm{\Gamma }_{\overline{d}}(q,𝒟_i))\mathrm{}+(1)^{k1}(\alpha (\overline{y})\mathrm{\Gamma }_{\overline{d}}(q,_{i=1}^k𝒟_i))\text{}`$ (13c)
$`=_{i=1}^k(\alpha (\overline{y})\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟_i))\mathrm{}+(1)^{k1}(\alpha (\overline{y})\mathrm{\Gamma }_{\overline{d}}(q^{},_{i=1}^k𝒟_i))\text{}`$ (13d)
$`=_{i=1}^k(\alpha (\overline{y})\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟_i))\mathrm{}+(1)^{k1}(\alpha (\overline{y})_{i=1}^k\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟_i))\text{}`$ (13e)
$`=\alpha (\overline{y})_{i=1}^k\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟_i)\text{}`$ (13f)
$`=\alpha (\overline{y})\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟)\text{}`$ (13g)
where Equations (13a) and (13g) hold because of Property 2 of decompositions, Equations (13b) and (13f) hold because of Proposition 5.2, Equations (13c) and (13e) hold because of Property 3 of decompositions, and Equation (13d) holds because $`q`$ and $`q^{}`$ are locally equivalent and the databases $`𝒟_i`$ contain at most $`\tau (q,q^{})`$ constants.

The aggregation function prod is not a decomposable aggregation function over $`𝐐`$. However, prod is decomposable over $`𝐐^\pm `$, i.e., the rational numbers without the element 0. It turns out that this is sufficient in order to reduce equivalence to local equivalence for prod, defined over the rational numbers.

###### Theorem 6.6 (Reduction to Local Equivalence for Product)

Suppose $`q(\overline{x},\text{prod}(y))`$ and $`q^{}(\overline{x},\text{prod}(y))`$ are disjunctive prod-queries, defined over $`𝐐`$. Then $`q`$ and $`q^{}`$ are equivalent if and only if they are locally equivalent.

Proof.  As before, we only have to show that local equivalence implies equivalence. Suppose therefore that $`q`$ and $`q^{}`$ agree on all databases whose carrier has at most $`\tau (q,q^{})`$ elements. Let $`𝒟`$ be any database and $`\overline{d}`$ be a tuple of constants. It suffices to show that

$$\text{prod}(y)\mathrm{\Gamma }_{\overline{d}}(q,𝒟)=\text{prod}(y)\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟).$$
(14)
Let $`(𝒟_i)_{i=1}^k`$ be a decomposition of $`𝒟`$ with respect to $`q`$, $`q^{}`$ and to $`\overline{d}`$. We distinguish between three cases.

Case 1. Suppose that there is an assignment $`\gamma \mathrm{\Gamma }_{\overline{d}}(q,𝒟)`$ that maps $`y`$ to 0. Then, $`q`$ retrieves the aggregate value 0 for $`\overline{d}`$ over $`𝒟`$, i.e., $`\text{prod}(y)\mathrm{\Gamma }_{\overline{d}}(q,𝒟)=0`$. By Property 2 of decompositions, there is a database $`𝒟_\gamma (𝒟_i)_{i=1}^k`$ such that $`\gamma \mathrm{\Gamma }_{\overline{d}}(q,𝒟_\gamma )`$. Note that $`q`$ returns the aggregate value 0 for $`\overline{d}`$ over $`𝒟_\gamma `$. By Property 1 of decompositions, $`𝒟_\gamma `$ has at most $`\tau (q,q^{})`$ elements. By our assumption, $`q`$ and $`q^{}`$ are locally equivalent. Therefore, $`q^{}`$ must return the aggregation value 0 for $`\overline{d}`$ over $`𝒟_\gamma `$. Hence, by applying Property 2 of decompositions once more, we derive that $`q^{}`$ retrieves the aggregate value 0 for $`\overline{d}`$ over $`𝒟`$.

Case 2. Suppose that there is an assignment $`\gamma \mathrm{\Gamma }_{\overline{d}}(q^{},𝒟)`$ that maps $`y`$ to 0. By analogous arguments to the previous case, we can show that both $`q`$ and $`q^{}`$ retrieve the aggregate value 0 for $`\overline{d}`$ over $`𝒟`$.

Case 3. Suppose that there is no assignment in $`\mathrm{\Gamma }_{\overline{d}}(q,𝒟)`$ that maps $`y`$ to 0. Similarly, suppose that there is no assignment in $`\mathrm{\Gamma }_{\overline{d}}(q^{},𝒟)`$ that maps $`y`$ to 0. Then, the aggregation function prod could just as well have been defined over $`𝐐^\pm `$. In this case, prod is a decomposable aggregation function and the arguments used in Equations (13a) through (13g) in the proof of Theorem 6.5 apply. Therefore, $`q`$ and $`q^{}`$ return the same aggregate value for $`\overline{d}`$ over $`𝒟`$ as required.

Thus, we have proved that Equation (14) holds in all possible cases.

The following result follows directly from Theorem 6.5.

###### Corollary 6.7 (Local Equivalence and Equivalence)

Suppose $`\alpha `$ is a decomposable aggregation function. If local equivalence is decidable for disjunctive $`\alpha `$-queries, then equivalence is also decidable.

In Section 4, we have noted that bounded equivalence of $`\alpha `$-queries can be checked in double exponential time if ordered identities of the form defined in Formula (3) can be decided in double exponential time. Hence, if $`\alpha `$ is also decomposable, we derive a double exponential upper bound on checking for equivalence of $`\alpha `$-queries.

From Theorems 6.5 and 6.6 and Corollary 4.10 we derive the following result.

###### Corollary 6.8 (Decidable Query Classes)

Equivalence of disjunctive aggregate queries is decidable for the aggregation functions max, top$`2`$, count, parity, and sum over both the integers and the rational numbers. In addition, equivalence of disjunctive prod-queries is decidable over the rational numbers.

## 7 Equivalence of Conjunctive Quasilinear Queries

A positive conjunctive query $`q`$ is linear if no predicate occurs more than once in $`q`$ . We generalize this by defining that a conjunctive query is quasilinear if no predicate that occurs in a positive literal, occurs more than once. Thus, in a quasilinear query, no predicate occurs in both a positive and a negated literal and no predicate occurs more than once in a positive literal. In this section we show that for a wide range of quasilinear queries, equivalence is isomorphism.

In Section 4.2. we defined reduced sets of terms with respect to a complete ordering. In a similar spirit, we now introduce reduced conjunctions of comparisons. A conjunction of comparisons $`C`$ is reduced with respect to a domain $``$ if

* there are no variables $`x`$ and $`y`$ occurring in $`C`$ such that $`C_{}x=y`$;
* there is no variable $`x`$ occurring in $`C`$ such that $`C_{}x=d`$ for a constant $`d`$.

We say that a conjunctive query is reduced with respect to $``$ if its comparisons are reduced with respect to $``$. If the domain is clear from the context, we will simply say that a query is reduced, without specifying the domain.

We have shown in  that for any positive conjunctive query, one can compute in polynomial time an equivalent reduced conjunctive query. This still holds when the query contains negated atoms. Note that the head of the equivalent reduced query may contain constants, even if the head of the original non-reduced query does not.

Let $`q(\overline{s},\alpha (\overline{t}))PNC`$ and $`q^{}(\overline{s}^{},\alpha (\overline{t}^{}))P^{}N^{}C^{}`$ be conjunctive aggregate queries with comparisons, ranging over the domain $``$. We use $`P`$ and $`P^{}`$ to denote the positive atoms, $`N`$ and $`N^{}`$ to denote the negated atoms, and $`C`$ and $`C^{}`$ to denote the comparisons. A homomorphism from $`q^{}`$ to $`q`$ is a substitution $`\theta `$ of the variables in $`q^{}`$ by terms in $`q`$ such that

1. $`\theta (\overline{s}^{})=\overline{s}`$ and $`\theta (\overline{t}^{})=\overline{t}`$;
2. $`\theta (a^{})`$ is in $`P`$ for every positive relational atom $`a^{}`$ of $`P^{}`$;
3. $`\theta (a^{})`$ is in $`N`$ for every negated relational atom $`a^{}`$ of $`N^{}`$;
4. $`C_{}\theta (s^{})\rho \theta (t^{})`$ for every comparison $`s^{}\rho t^{}`$ in $`C^{}`$.

A homomorphism is an isomorphism if it is bijective and if its inverse is also a homomorphism. The queries $`q^{}`$ and $`q`$ are isomorphic if there is an isomorphism from $`q^{}`$ to $`q`$. In  we have also shown that reduced linear max, count and sum queries are equivalent if and only if they are isomorphic. For queries with negated literals, we can generalize this result to quasilinear queries.

We say that a class of queries $`𝒬`$ is proper if for satisfiable queries in $`𝒬`$ equivalence implies isomorphism, that is, if for any two satisfiable reduced queries $`q`$$`q^{}𝒬`$ it is the case that $`q`$ and $`q^{}`$ are only equivalent if they are isomorphic. For every aggregation function we denote by $`(\alpha )`$ the class of linear $`\alpha `$-queries. Similarly, we denote by $`𝒬(\alpha )`$ the class of quasilinear $`\alpha `$-queries.

###### Theorem 7.1 (Quasilinear and Linear Queries)

Let $`\alpha `$ be an aggregation function. Suppose that the class $`(\alpha )`$ is proper. Then, the class $`𝒬(\alpha )`$ is also proper.

Proof.  Consider the satisfiable reduced queries

$`q(\overline{s},\alpha (\overline{t}))`$ $`PNC`$
$`q^{}(\overline{s},\alpha (\overline{t}))`$ $`P^{}N^{}C^{}`$
where

* $`P`$ and $`P^{}`$ are conjunctions of positive relational atoms;
* $`N`$ and $`N^{}`$ are conjunctions of negated relational atoms;
* $`C`$ and $`C^{}`$ are conjunctions of comparisons.

Suppose that $`(\alpha )`$ is proper. Suppose that $`q`$ is not isomorphic to $`q^{}`$. We show that $`q`$ is not equivalent to $`q^{}`$. Note that we can assume that $`q`$ and $`q^{}`$ have the same heads since otherwise they are obviously not equivalent.

We introduce the positive parts of $`q`$ and $`q^{}`$ as the queries $`q_+`$ and $`q_+^{}`$, defined as

$`q_+(\overline{s},\alpha (\overline{t}))`$ $`PC`$
$`q_+^{}(\overline{s},\alpha (\overline{t}))`$ $`P^{}C^{}.`$
We consider two cases.

Case 1. Suppose that $`q_+`$ is not isomorphic to $`q_+^{}`$. Hence, $`q_+`$ and $`q_+^{}`$ are not equivalent. Let $`𝒟`$ be a database for which $`q_+`$ and $`q_+^{}`$ return different values. We may assume, without loss of generality, that $`𝒟`$ only contains atoms with predicates appearing in $`P`$ or in $`P^{}`$.

If there is a predicate $`p`$ that appears in an atom $`P`$, but not in $`P^{}`$, then clearly $`q`$ and $`q^{}`$ cannot be equivalent, since we could create a database that satisfies $`q^{}`$ and does not contain any atom with predicate $`p`$. Similarly, if there is a predicate that appears in $`P^{}`$, but not in $`P`$, then $`q`$ and $`q^{}`$ cannot be equivalent. Hence, we may assume that the set of predicates of atoms in $`P`$ is identical to the set of predicates of atoms in $`P^{}`$. Thus, there is no atom in $`𝒟`$ containing a predicate appearing in $`N`$ or $`N^{}`$.

Thus, $`q_{+}^{}{}_{}{}^{𝒟}=q^𝒟`$ and $`q_{+}^{}{}_{}{}^{𝒟}=q_{}^{}{}_{}{}^{𝒟}`$. We conclude that $`𝒟`$ is a counterexample for the equivalence of $`q`$ and $`q^{}`$.

Case 2. Suppose that $`q_+`$ is isomorphic to $`q_+^{}`$. Since $`q_+`$ and $`q_+^{}`$ are linear, there exists only one isomorphism between them, say $`\theta `$. Note that $`\theta `$ is defined on all the variables in $`q`$, since $`q`$ is a safe query. By our assumption, $`q`$ is not isomorphic to $`q^{}`$. Thus, $`\theta (N)N^{}`$. Suppose, without loss of generality, that $`\neg a`$ appears in $`N`$ and $`\theta (\neg a)`$ does not appear in $`N^{}`$. Let $`\gamma `$ be a mapping of the variables in $`q`$ to constants, such that $`\gamma `$ is consistent with the comparisons in $`q`$. We define a database $`𝒟:=\{\gamma (b)bP\}\{\gamma (a)\}`$. Clearly, $`q`$ does not return a grouping value for $`\gamma (\overline{s})`$ over $`𝒟`$ whereas $`q^{}`$ does return a grouping value for $`\gamma (\overline{s})`$. Thus, $`q`$ and $`q^{}`$ are not equivalent.

This completes the proof.

Now, it follows from our results in  that for quasilinear queries with the aggregate functions max, sum and count, equivalence boils down to isomorphism. In a similar fashion to the proofs there, we can extend our results to additional aggregate functions.

A bag $`B`$ is a singleton if it contains exactly one value. We say that an aggregation function $`\alpha `$ is a singleton-determining aggregation function, if for all singleton bags $`B`$ and $`B^{}`$ we have that

$$\alpha (B)=\alpha (B^{})B=B^{}.$$

Clearly max, top$`2`$, sum, prod and avg are singleton-determining aggregation functions. Note that count and parity are nullary aggregate functions. Thus, they are defined over a domain that contains only a single value, the empty tuple. Hence, count and parity are also singleton-determining aggregation functions. However cntd is not singleton-determining aggregation functions.

###### Theorem 7.2 (Equivalence of Quasilinear Queries)

Let $`\alpha `$ be an aggregation function. Then, the following conditions are equivalent:

1. $`\alpha `$ is singleton-determining;
2. $`(\alpha )`$ is proper;
3. $`𝒬(\alpha )`$ is proper.

Proof.  The direction “(2) $``$ (3)” holds by Theorem 7.1. Clearly, “(3) $``$ (2)” holds since $`(\alpha )𝒬(\alpha )`$. Thus, we need only show that “(1) $``$ (2)” and “(2) $``$ (1)”.

“(1) $``$ (2)” Suppose that $`\alpha `$ is a singleton-determining aggregation function. We show that $`(\alpha )`$ is proper. To this end, let $`q(\overline{s},\alpha (\overline{t}))A`$ and $`q^{}(\overline{s},\alpha (\overline{t}))A^{}`$ be satisfiable reduced linear $`\alpha `$-queries. Suppose that $`qq^{}`$. We will show that $`q`$ and $`q^{}`$ are isomorphic.

In  it has been shown that positive linear non-aggregate queries without comparisons are set-equivalent if and only if they are isomorphic. This still holds even if the queries have comparisons.444We are not aware that this result has been published, but it appears in the extended version of . We associate with $`q`$ a non-aggregate query $`\widehat{q}`$, called the non-aggregate projection of $`q`$, which is derived from $`q`$ by simply removing the aggregate term from the head of $`q`$. Thus, $`\widehat{q}`$ has the form

$`\widehat{q}(\overline{s})A.`$
Since $`qq^{}`$, they return values for the same grouping tuples. Thus, $`\widehat{q}`$ is set-equivalent to $`\widehat{q}^{}`$. Hence, $`\widehat{q}`$ is isomorphic to $`\widehat{q}^{}`$. Let $`\theta `$ be the isomorphism from $`\widehat{q}^{}`$ to $`\widehat{q}`$. If $`\alpha `$ is a nullary aggregation function, then $`\theta `$ is an isomorphism from $`q^{}`$ to $`q`$. Suppose that $`\alpha `$ is not a nullary aggregation function.

Let $`\gamma `$ be an instantiation of the terms in $`q`$ that satisfies the comparisons in $`q`$ and maps each term to a different value. We construct a database $`𝒟`$ out of $`q`$ by applying $`\gamma `$ to the relational part of $`q`$.

Clearly, the only satisfying assignment of $`q`$ to the constants in $`𝒟`$ is exactly $`\gamma `$. Thus, $`q`$ retrieves $`(\gamma (\overline{s}),\alpha (\gamma (\overline{t})))`$. The only satisfying assignment of $`q^{}`$ is $`\gamma \theta `$. Therefore, $`q^{}`$ returns $`(\gamma \theta (\overline{s}),\alpha (\gamma \theta (\overline{t})))`$. Note that since $`\theta `$ is an isomorphism from $`q^{}`$ to $`q`$, it holds that $`\gamma \theta (\overline{s})=\gamma (\overline{s})`$.

Recall that $`\alpha `$ is a singleton-determining aggregation function. Therefore, $`\alpha (\gamma \theta (\overline{t})))=\alpha (\gamma (\overline{t})))`$ if and only if $`\gamma \theta (\overline{t})=\gamma (\overline{t})`$. The instantiation $`\gamma `$ is an injection, thus $`\gamma \theta (\overline{t})=\gamma (\overline{t})`$ if and only if $`\theta (\overline{t})=\overline{t}`$. This must hold since $`qq^{}`$. Therefore, $`\theta `$ is an isomorphism from $`q`$ to $`q^{}`$.

“(2) $``$ (1)” Suppose that $`\alpha `$ is not a singleton-determining aggregation function. We show that $`(\alpha )`$ is not proper. To this end, we create linear $`\alpha `$-queries $`q`$ and $`q^{}`$ such that $`qq^{}`$, but $`q`$ and $`q^{}`$ are not isomorphic.

Since $`\alpha `$ is not a singleton-determining aggregation function, there are singleton bags $`B=\{\{d\}\}`$, and $`B^{}=\{\{d^{}\}\}`$ such that $`dd^{}`$ and $`\alpha (B)=\alpha (B^{})`$. We define the queries

$`q(\alpha (d))`$ $`p(d)p(d^{})`$
$`q^{}(\alpha (d^{}))`$ $`p(d)p(d^{}).`$
Clearly $`q`$ and $`q^{}`$ are not isomorphic, but they are equivalent.

###### Corollary 7.3 (Equivalence and Isomorphism)

The classes of quasilinear max, top$`2`$, count, sum, prod, parity and avg queries are proper.

Proof.  This result follows from the fact that all the aggregation functions above are singleton-determining and from Theorem 7.2.

For cntd a similar result can be shown for common cases.

###### Theorem 7.4 (Equivalence of Quasilinear Count-Distinct Queries)

Let $`q`$ and $`q^{}`$ be satisfiable reduced quasilinear cntd-queries. Moreover, suppose that

* the comparisons in $`q`$ and $`q^{}`$ use only $``$, $``$ and
* $`q`$ and $`q^{}`$ either range over the rational numbers or do not have constants.

Then $`q`$ and $`q^{}`$ are equivalent if and only if they are isomorphic.

Proof.  This follows directly from the fact that such queries, when positive, are equivalent if and only if they are isomorphic  and from Theorem 7.1.

Since isomorphism of quasilinear queries can be checked in polynomial time, we derive the following complexity result.

###### Corollary 7.5 (Polynomiality)

The equivalence problem for the class of quasilinear $`\alpha `$-queries is decidable in polynomial time if $`\alpha `$ is one of the aggregation functions max, top$`2`$, count, sum, prod, parity, or avg and for common cntd-queries.

## 8 Conclusion

Necessary and complete conditions for the decidability of bounded equivalence of disjunctive aggregate queries with negation have been presented. This problem has been shown to be decidable for a wide class of aggregation functions. Equivalence of aggregate queries with negation has been reduced to a special case of bounded equivalence, called local equivalence, for decomposable aggregation functions. We have also shown that equivalence can be decided in polynomial time for the common case of quasilinear queries.

Novel proof techniques have been presented. One example is the application of the Principle of Inclusion and Exclusion to the case of group aggregation functions. Our results are couched in terms of abstract characterizations of aggregation functions. Thus, the results presented are easily extendible to additional aggregation functions. In Table 1 we summarize the properties that hold for each of the aggregation functions considered in this paper. Table 2 shows our decidability results for these aggregation functions.

Bag-set semantics has been introduced in  to give a formal account of the way in which SQL queries are executed, which do not return a set of tuples but a multiset. It is easy to see that two non-aggregate queries are equivalent under bag-set semantics if and only if the aggregate queries obtained by adding the function count are equivalent. Thus, our results on count-queries directly carry over to non-aggregate queries that are evaluated under bag-set semantics. This is a significant contribution to the understanding of SQL queries. Moreover, these results can easily be extended to non-aggregate queries evaluated under bag semantics , thereby, solving an additional open problem.

Concepts seemingly similar to the ones introduced in the present paper have been investigated in . In particular, the authors considered aggregation functions defined in terms of commutative monoids. However, the purpose of that research was to study the expressivity of logics that extend first-order logic by aggregation. In  it is shown that formulas in those extended logics are Hanf-local and Gaifman-local. Intuitively, this means that whether or not a formula is true for a tuple $`\overline{d}`$ in a structure, depends only on that part of the structure that is “close” to $`\overline{d}`$. A class of formulas that is Hanf- or Gaifman-local need not be decidable. In addition, the authors only considered monoids over the rational numbers, which excludes functions such as top$`K`$ and parity.

We leave for future research the problem of deciding equivalence among avg and cntd queries as well as equivalence of aggregate queries with a HAVING clause. Finding tight upper and lower bounds for equivalence, as well as the adaptation of our results to the view usability problem are other important open problems.