Computing Consistent Least Upper Bounds in Aggregate Logic
Abstract
We consider the problem of answering conjunctive queries with aggregation on database instances that may violate primary key constraints. In SQL, these queries follow the SELECT-FROM-WHERE-GROUP BY format, where the WHERE clause involves a conjunction of equalities, and the SELECT clause can incorporate aggregate operators like MAX, MIN, SUM, AVG, or COUNT. Repairs of a database instance are defined as inclusion-maximal subsets that satisfy all primary keys. The range-consistent answer to a numerical query over an inconsistent database is a pair [glb, lub], where glb and lub are, respectively, the smallest and the greatest results returned by the query over all possible repairs. While previous work has focused on the computation of the glb, the current paper studies the computation of the lub for a numerical domain of non-negative rational numbers. We introduce the notion of -acyclicity for self-join-free conjunctive queries. We show that if the body of a SUM-query is -acyclic, then the lub can be computed through a rewriting in first-order aggregate logic. Moreover, we show that this result extends to all aggregate operators that are monotone and associative. Importantly, we also prove the inverse: if the body of a SUM-query is not -acyclic, then the lub cannot be computed in first-order aggregate logic.
Keywords and phrases:
Consistent query answering, primary key, conjunctive query, aggregate logicCopyright and License:
2012 ACM Subject Classification:
Information systems Relational database query languages ; Theory of computation Incomplete, inconsistent, and uncertain databases ; Theory of computation Logic and databasesEditors:
Balder ten Cate and Maurice FunkSeries and Publisher:
Leibniz International Proceedings in Informatics, Schloss Dagstuhl – Leibniz-Zentrum für Informatik
1 Introduction
Consistent query answering (CQA) was introduced in [3] as a principled approach to answering queries on databases that are inconsistent with respect to a given set of integrity constraints. The only integrity constraints we consider in the current work are primary keys. A block in a database instance is a -maximal set of tuples of a same relation that agree on the primary key of . A repair of a database instance picks exactly one tuple from each block. Given a Boolean query , is then defined as the decision problem that takes a database instance as input, and asks whether holds true in every repair of . This problem, while commonly studied for Boolean queries, can be readily extended to queries with free variables : a consistent answer to a query is any sequence of constants, of length , such that holds true in every repair. The computational complexity of is well understood for all queries in , the class of self-join-free Boolean conjunctive queries [29, 32]. This understanding readily extends to queries with free variables.
It is significant to generalize CQA from Boolean queries, which return either true or false, to numerical queries, which return a numeric result. Aggregation queries constitute an important class of numerical queries. However, numerical aggregation queries are likely to return different results on different repairs, and therefore lack a single consistent answer that holds true across all repairs. For this reason, Arenas et al. [4] have proposed range semantics, which provides the greatest lower bound (glb) and the least upper bound (lub) of query answers across all repairs. Specifically, for a numerical aggregation query , the function problems and take a database instance as input, and return, respectively, the glb and the lub of the set that contains each number returned by on some repair.
In the current paper, we consider numerical aggregation queries that take the following form in the extended datalog syntax of [10, 11]:
| (1) |
where the body is a conjunction of atoms (a.k.a. subgoals), is either a numeric variable occurring in or a non-negative number, and is an aggregate symbol (like , , , , ). Such a query will be called an -query, and is interpreted as follows. Every aggregate symbol in our query language is associated with an aggregate operator, denoted , which is a function that takes a multiset of numbers, and returns a number. The semantics on a given database instance is standard: let enumerate all homomorphisms (a.k.a. embeddings) of the body into , then the query returns . Note that the argument of is a multiset, because it is possible that for . For this semantics to be well-defined, each must necessarily belong to some numerical domain . In this case, the aggregate query is said to be over . In this paper, we take to be .
In [24], progress has been made in studying the complexity of for queries of the form (1), particularly regarding the conditions under which can be expressed in an extension of first-order logic with aggregate operators. However, the analogous question for is not systematically addressed in that work and is the focus of the present paper. Thus, we study the following function problem for any -query of the form (1):
Problem . Input: A database instance that may violate its primary key constraints. Output: .
Here, denotes the result of on . The function problem is obtained by replacing with in the above problem. Note that since every database has a finite number of repairs, the glb and lub are equal to the min and max, respectively. In [24], we considered the case where an aggregate operator is undefined over the empty multiset. In contrast, in the current paper, we assume that is well-defined and equals , which, as explained in Section 4.1, is a reasonable assumption for the monotone and associative aggregate operators we consider.
We write for the class of all queries such that is a self-join-free Boolean conjunctive query (i.e., not containing two distinct atoms with the same relation name). We are interested in the following complexity classification problem: Given a query in , determine the computational complexity of the function problem .
The restriction to self-join-free queries is due to the following reason. The computational complexity of is well understood whenever is self-join-free [29] – an understanding we build upon in the current paper – but it remains largely unexplored for queries with self-joins. Only recently has it been established for self-joins of size two [35]. In particular, the concept of an attack graph, which is a key tool used in [29] and in the current paper, loses its meaning and usefulness when self-joins are present. Given the limited understanding of CQA for self-joins, we exclude self-joins in the current paper.
The following example illustrates the foregoing with a concrete case. It also demonstrates that, for queries , agrees with insofar as it seeks to minimize the set of embeddings, in contrast to , which maximizes the set of embeddings. This difference explains why the two problems require different techniques.
Example 1.
The database of Fig. 1 records the quantity of products in stock in various towns (relation ) and the town of operation for each dealer (relation ). The primary keys are underlined, and blocks are separated by dashed lines. The inconsistencies concern Smith’s town of operation, and the stock levels of Tesla X and Tesla Y in, respectively, Boston and New York. For the example database of Fig. 1, the following query returns the total quantity of cars in stock in Smith’s town of operation:
In Fig. 1, the repair composed of the tuples preceded by yields the answer (), which is the greatest result achievable among all repairs. Notably, the smallest result among all repairs is , obtained by a repair that picks the tuple from , thereby falsifying the body of this -query.
In our complexity study, we specifically aim to understand under which conditions is solvable through rewriting in an aggregate logic, denoted , which extends first-order logic with aggregate operators along the lines of [22]. So our central problem takes as input a numerical query in , and asks whether or not there is a numerical query in that solves ; moreover, when such exists, we are interested in constructing it, a task loosely referred to as “(consistent) lub rewriting of in .” A practical motivation for focusing on is that formulas in this logic are well-suited for implementation in SQL, allowing them to benefit from existing DBMS technology. Before presenting the major contributions of the current paper, we recall a notable consequence from [24] which holds true under the assumption :
Theorem 2 ([24]).
Let be a numerical variable or a positive number. For each query in over , is expressible in if and only if the attack graph of is acyclic.
Our results show that the right-to-left implication in Theorem 2 fails if is replaced with , while keeping all other conditions the same. That is, acyclicity of the attack graph is not a sufficient condition for to be expressible in . To address this, we will introduce -acyclicity, a polynomial-time decidable syntactic property of queries. Our first main result can then be stated as follows:
Theorem 3 (Separation Theorem for SUM).
Let be a numerical variable or a positive number. For each query in over , is expressible in if and only if is -acyclic.
-queries of the form can be expressed as , and are therefore also covered by Theorem 3. Our second main result is that the right-to-left implication in Theorem 3 extends from -queries to all -queries in whose aggregate operator is monotone and associative, two properties that will be precisely defined in Section 4.1.
Theorem 4.
Let be a query in whose aggregate operator is monotone and associative. If is -acyclic, then can be expressed by a query in (and such a query can be effectively constructed in polynomial time in the size of ).
The converse of Theorem 4 does not hold in general. For example, is expressible in for all -queries in , since the maximum over all repairs coincides with the result of the -query on the original database.
Our results add -acyclic queries to the query classes in Fig. 2 and prove the correctness of the inclusions shown. It was already known that lub rewriting of -queries is possible for the classes [18] and [23]. The notion of -acyclicity is new and identifies the largest class of queries that allow lub rewriting of -queries. Interestingly, Theorem 2 and our results show that there are -queries that allow glb rewriting but not lub rewriting, specifically those that are not -acyclic but have an acyclic attack graph.
As will be argued in Section 8, all our results naturally extend to queries with a GROUP BY feature. For example, the following SQL query returns, for each dealer, the total quantity of products in stock in their town of operation:
This query is stated as follows in the extended datalog syntax of [10]:
Range semantics are obtained by replacing with every possible dealer name (“Smith” and “James,” in our example), and calculating the glb and lub as before.
This paper is organized as follows. Section 2 discusses related work, and Section 3 introduces preliminaries. In Section 4, we formally define , the class of queries for which we want to compute range consistent query answers, along with the aggregate logic , which serves as the target language for our rewritings. We also introduce a key auxiliary result, the Reduction Lemma. Section 5 introduces the new notion of -acyclicity and establishes some of its properties. The expressibility result of Theorem 4 is discussed in Section 6. Section 7 establishes the inexpressibility direction of Theorem 3, showing that lub rewriting in is impossible for -queries that lack -acyclicity. Section 8 shows that all queries in [23] are -acyclic. That section also explains that, while our results are proved for closed queries, they can be readily extended to accommodate free variables. Finally, Section 9 concludes the paper.
2 Related Work
Consistent query answering (CQA) originated in 1999 with a seminal paper by Arenas, Bertossi, and Chomicki [3], which introduced the notions of repair and consistent answer. Two years later, the same authors introduced the range semantics (with lower and upper bounds) for queries with aggregation [4, 5][6, Chapter 5], which has been commonly adopted ever since.
Ariel Fuxman defined in his PhD thesis [18] a syntactic class of self-join-free conjunctive queries, called , whose extension with aggregate operators (, , , ) yields . Range semantics for queries in can be obtained by executing two first-order queries (one for lower bounds, and one for upper bounds) followed by simple aggregation steps; this technique was subsequently implemented in the ConQuer system [19] through rewriting in SQL. The class [23] is an extension of that contains all (and only) self-join-free conjunctive queries for which Fuxman’s technique applies for . In [24], Fuxman’s technique was extended to a richer rewriting in the first-order aggregate logic , but only for greatest lower bounds (see Theorem 2). The current paper extends this work to least upper bounds. Query rewriting in is different from the AggCAvSAT approach [12], which uses powerful SAT solvers for computing range semantics, and thus can solve queries that are beyond the computational power of .
CQA for queries without aggregation, with respect to primary keys, has been studied in considerable depth. Its decision variant, which was coined in 2010 [36], asks whether a Boolean query holds in every repair of a given database instance. A systematic study of its complexity for queries had started already in 2005 [20], and was eventually solved in two journal articles by Koutris and Wijsen [29, 32], as follows: for every query , is either in , -complete, or -complete, and it is decidable, given , which case applies. This complexity classification extends to non-Boolean queries by treating free variables as constants. Other extensions beyond this trichotomy deal with foreign keys [21], more than one key per relation [31], negated atoms [30], restricted self-joins [27, 28, 35], or naturally ordered positive semirings [26]. For unions of conjunctive queries , Fontaine [17] established interesting relationships between and Bulatov’s dichotomy theorem for conservative CSP [8]. Recently, Figueira et al. [15, 16] proposed a polynomial-time algorithm for solving for conjunctive queries , including those with self-joins, and showed that it can replace all polynomial-time algorithms found in [27, 29]. Overviews of two decades of theoretical research in CQA can be found in [7, 25, 38].
3 Preliminaries
We assume denumerable sets and of variables and constants respectively. The set includes , the set of non-negative rational numbers. The set of numerical variables is a subset of .
We assume denumerably many relation names. Every relation name is associated with a signature, which is a triple where is the arity, is called the primary key, and is the set of numerical positions (also called numerical columns). This relation name is full-key if . Note that each relation name is associated with exactly one key constraint, which is determined by the signature of . For an -tuple , we write to denote its arity . We often blur the distinction between a sequence of distinct variables and the set , which is also denoted . If and , then we define their concatenation as the tuple .
Atoms, facts, and database instances.
Let be a relation name of signature . An atom is an expression where each is either a constant or a variable, and for every , is a numerical variable or a number in . It is common to underline positions of the primary key. An atom is said to be full-key if its relation name is full-key. If is an atom, then is the set of variables that occur in , and is the set of variables that occur in at a position of the primary key. Further, we define . A fact is an atom without variables. A fact with relation name is also called an -fact. Two facts and are said to be key-equal if and .
A database instance is a finite set of facts. If is a relation name, then the -relation of is the set of all -facts in . A database instance is consistent if it does not contain two distinct facts that are key-equal. Primary keys need not be explicitly specified, as they are determined by the predefined relation signatures. The block of a fact in , denoted , is the set of all facts in that are key-equal to that fact.
Valuation.
A valuation over a finite set of variables is a total mapping from to such that for every numerical variable . For a valuation over , we write to denote its domain . A valuation over is extended to every element in by letting for every .
Let be a valuation. If is the atom , then . If is a set of atoms, then . Notably, every variable in remains a variable in , where .
Partial valuation.
Let be a database instance, and a first-order formula with free variables . Let be a valuation. Then we write to denote that can be extended to a valuation over such that for , we have using standard semantics (see, e.g., [33, p. 15]). Typically, but not necessarily, . If has no free variables, then we write instead of , where is the empty valuation.
Repairs and .
A repair of a database instance is a -maximal consistent subset of it. We write for the set of repairs of a database instance . For a closed formula , is the decision problem that takes a database instance as input and determines whether or not every repair of satisfies .
Boolean Conjunctive Queries.
A self-join-free Boolean conjunctive query is a closed first-order formula , where each is an atom, is a sequence containing every variable occurring in some , and implies . The conjunction , whose free variables are , is called the body of the query. We write for the set of self-join-free Boolean conjunctive queries. We often blur the distinction between the Boolean query , its body, and the set . For example, if is an atom of (the body of) , then is the query obtained from by deleting from its body.
Gaifman graph.
The Gaifman graph of , denoted , is an undirected simple graph whose vertex-set is . There is an edge between and if and some atom of contains both and .
Guardedness.
Let and be two queries in . We say that is guarded by , denoted , if for every atom in , there exists an atom in such that . We write if both and . It is straightforward that implies (but the converse does not hold).
Attack graph.
Attack graphs for queries in were first introduced in [37], later generalized in [29], and have since been used in several studies, e.g., [16, 24, 26]. We write for the set of functional dependencies that contains whenever . For , we define , where is the standard notion of logical implication. An atom of is said to attack a variable , denoted , if contains a (possibly empty) path between some variable in and such that no variable on the path belongs to . The attack graph of is a directed simple graph whose vertices are the atoms of . There is a directed edge from to , denoted , if attacks some variable of .
Whenever a query in is clear from the context, we can use a relation name as a shorthand for the unique atom with that relation name in the query. For example, in the following example, is used as a shorthand for the atom .
Example 5.
Consider the query from shown on the left side of Fig. 3. The directed edges represent attacks. We have , , and . The sequence entails and . The query on the right side is obtained from by replacing the -atom with a full-key atom, which introduces a cycle in the attack graph.
Let now be a self-join-free conjunction of atoms such that the attack graph of is acyclic. The following definitions are relative to a fixed topological sort of ’s attack graph. We define the following sequences of variables for :
-
contains all (and only) variables of . Thus, ;
-
contains the variables of that do not already occur in ; and
-
contains the variables of that do not already occur in .
Moreover, we define , the empty sequence. With this notation, we have that for every , .
4 Aggregate Logic and CQA
In this section, we first define the notion of aggregate operator and then introduce the logic which will serve as the target language for our rewritings. We formally define consistent least upper bounds, denoted , for arbitrary numerical terms in with free variables . Our study will subsequently focus on closed numerical terms in , a subclass of introduced in Definition 9. Finally, this section introduces an important auxiliary lemma, called the Reduction Lemma.
4.1 Aggregating Non-Negative Numbers
A (positive) aggregate operator is a function that maps each finite multiset of non-negative rational numbers to a non-negative rational number.
In the following definitions, all multisets are understood to be multisets of non-negative rational numbers. An aggregate operator is associative if for all multisets and , we have:
| (2) |
where denotes union of multisets. By letting in Eq. 2, we obtain that if is associative, then for all multisets , we have . This condition holds for and over if and only if we define and .
Example 6.
Examples of associative aggregate operators are and . On the other hand, , , and are not associative. For instance, and . Also, lacks associativity over because any choice for disrupts associativity. In particular, if we define , then .
An aggregate operator is monotone if for all and every (possibly empty) multiset , we have:
| whenever for every . | (3) |
By letting in Eq. 3, we obtain that if is monotone, then for all multisets , we have . Again, this condition holds for and over if and only if we define and .
Example 7.
Examples of monotone aggregate operators are , , and . Note that is not monotone since . also lacks monotonicity: if we increase to in the multiset , the number returned by drops from to .
Significantly, an aggregate operator that is not monotone in general may become monotone under a restriction of its underlying domain. For example, , defined by , is not monotone over (because ), but is monotone over .
4.2 The Logic AGGR[FOL]
Our treatment of aggregate logic follows the approach in [22, 33]. We write for the extension of predicate calculus with numerical terms introduced next.
Every formula in classical predicate calculus is also a formula in . In , terms are not restricted to variables and constants; they also include numerical terms, as defined below. Let be a formula in , where , are disjoint sequences that together contain each free variable of exactly once. A primitive numerical term is either a non-negative rational number or a numerical variable in . For every possible aggregate operator , a primitive numerical term , and a formula , we have a new numerical term
Variables that are free in become bound in ; in this respect behaves like a sort of quantification over . Next, we define the semantics.
Let be a sequence of constants of length . The value on a database instance , denoted , is calculated as follows. Let be a (possibly empty) -maximal set of valuations over such that and for . Then . Note that the argument of is in general a multiset, since may be equal to for . A numerical term without free variables is also called a numerical query, denoted .
Example 8.
If , then on the example database of Fig. 1, , the sum of quantities in Boston. This numerical term can be used in other formulas. For example, returns, for each town , the total quantity of products stored in .
4.3 Least Upper Bounds Across All Repairs
For each numerical term , we define relative to a database instance . Let be a sequence of constants of length . Then,
A general research question is: under which conditions can be expressed in ? In this paper, we study this question for the case of numerical terms where is a self-join-free conjunction of atoms. For readability, we often express such a numerical term using the following datalog-like syntax:
where the aggregate symbol is interpreted by the aggregate operator , which is often made explicit by writing . For instance, is interpreted by , and by .
In the initial technical treatment, we assume that is empty, i.e., we are dealing with numerical terms without free variables, which are conveniently expressed as . In Section 8, we treat the extension to free variables. The following definition pinpoints the class of queries we are interested in.
Definition 9.
is defined as the class of numerical queries where is a self-join-free conjunction of atoms, and is either a numerical variable or a non-negative number. An alternative syntax is , where the aggregate symbol is interpreted by (which is often made explicit by writing instead of ). We call the head, and the body.
4.4 The Reduction Lemma
Let and be function problems whose output is a single number. If is an instance of , we write to denote the output of on input , where . We say that is first-order reducible to , denoted if there exists a first-order definable function such that for every instance of , is an instance of such that . We write if the reduction can even be expressed by a nonrecursive datalog () program, as defined in [1, page 62–63].
In the statement of the following lemma, referred to as the Reduction Lemma, we use as a shorthand for for readability.
Lemma 10 (Reduction Lemma).
Let and be queries in such that is monotone. If and , then .
We illustrate Lemma 10 below and show that it does not hold for greatest lower bounds.
Example 11.
Consider the queries and , where and are shown in Fig. 3. Their bodies are related by and induce the same set of functional dependencies (notably, ). The Reduction Lemma implies that and are equivalent under first-order reductions. However, this equivalence does not hold for glb: indeed, by Theorem 2, is in , whereas is not.
5 Kernels and -Acyclicity
We assume that the reader is familiar with the notion of a minimal cover of a set of functional dependencies (FDs), which can be computed in polynomial time [34][1, p. 257]. To recall briefly, a set of FDs is a minimal cover (a.k.a. irreducible) if each functional dependency in has the form , where is an attribute, and is left-reduced and non-redundant. A set of FDs is a minimal cover of another set of FDs if is a minimal cover and . We now extend this notion of irreducibility to queries, introduce the notion of a kernel, and present some auxiliary lemmas.
Definition 12 (Irreducible query, kernel).
Let be a query in , and let denote the set of atoms in that are not full-key. We say that is irreducible if the following two conditions hold:
-
1.
for every atom of , we have ; and
-
2.
the set is irreducible.
A kernel of is an irreducible query in such that and .
Example 13.
Consider the queries and , , in Fig. 3. The query is not irreducible, because it does not satisfy the first condition in Definition 12. The query is irreducible. Since and , it follows that is a kernel of . Notably, the attack graph of is acyclic, whereas the attack graph of its kernel contains a cycle.
As a side remark, consider the query . By Theorem 2, is expressible in , while, as we will see shortly, Theorem 3 implies that is not expressible in . This difference can be explained by the observation that -blocks or -blocks of size greater than can be omitted when computing . For example, assume that contains , , and with . Then a repair can select and a fact , , such that . Consequently, no homomorphism from to can map to . Preventing homomorphisms in this way minimizes the value of . On the other hand, computing requires maximizing the number of embeddings. In that case, one would prefer selecting and if such a common exists.
Lemma 14.
Let be a query in that is irreducible. Let be an atom in that is not full-key. Then, there is a variable such that and .
Proof.
By the second item in Definition 12, . Hence, we can assume such that . Let denote the set of atoms in that are not full-key. By the first item in Definition 12, . It must obviously be the case that , which implies .
Lemma 15.
Let be a query in . A kernel of can be constructed in polynomial time in the size of .
Example 13 illustrates that an query with an acyclic attack graph can have a kernel whose attack graph contains a cycle. Informally, this means that cycles in attack graphs can emerge during the construction of a kernel but, as stated in Proposition 18, they cannot disappear. Moreover, the following lemma states that all kernels of the same query either all have an acyclic attack graph or all have a cyclic attack graph.
Lemma 16.
Let be a query in . If some kernel of has an acyclic attack graph, then every kernel of has an acyclic attack graph.
The proof of Lemma 16 in [2, p. 133] also establishes the following result: if some kernel of has an acyclic attack graph, then all kernels agree on their atoms that are not full-key, up to the choice of relation names. Lemma 16 allows the following definition.
Definition 17 (-acyclic).
A query in is -acyclic if it has a kernel with an acyclic attack graph (and hence, by Lemma 16, all of its kernels have acyclic attack graphs).
As illustrated in the Venn diagram in Fig. 2, -acyclic queries have acyclic attack graphs:
Proposition 18.
Every -acyclic query in has an acyclic attack graph.
6 Expressibility for -Acyclic Queries (Theorem 4)
We will demonstrate a slightly stronger result than Theorem 4: instead of proving expressibility in , we use a weaker logic – introduced next – that yields more readable rewritings.
6.1 The Logic AGGR[nr-datalog]
We assume that the reader is familiar with the concept of a nonrecursive datalog program ( program), whose definition can be found in [1, pp. 62–63]. We now extend such programs by incorporating aggregation. Assume that an program contains the following rule, where is either a number or a numerical variable:
| (4) |
Note that by the safety requirement of , if is a variable, it also occurs in the body of the rule. We allow such a rule to be replaced with:
where is an aggregate symbol. The semantics on a given database is as follows. Let be the conjunction of all atoms in the body of the rule, where is a shortest sequence containing every variable that occurs in the body of the rule. Let be a sequence of constants of length . Let be a -maximal set of valuations over such that and for . If , then the rule derives with . Note that the argument of is in general a multiset, since may be equal to for . In case , no such fact is derived.
We write for the language consisting of all programs obtained from programs by allowing aggregate operators in the head (but not in the body). Examples 19 and 21 show programs in .
Example 19.
Retrieve the town(s) with the largest total quantity of stored products.
Replacing and with and , respectively, in the above program yields a standard program, as required by .
6.2 Computing Consistent Least Upper Bounds in AGGR[nr-datalog]
We first show a weaker version of Theorem 4 for irreducible queries.
Lemma 20.
Let be a query in whose aggregate operator is monotone and associative. If is irreducible and has an acyclic attack graph, then an program solving can be constructed in polynomial time in the size of .
We will now explain the program in the statement of Lemma 20, and explain how its correctness is proved. Let be a query in with an acyclic attack graph. Let be a topological sort of the attack graph. We assume that is a variable in ; our treatment can be easily specialized to the simpler case in which is a constant. For , the sequences , , and are defined in Section 3. Moreover, , the empty sequence. For , we define as the restriction of to those variables that belong to . In particular, and .
Our program contains the rules shown in Fig. 4, for , where is an arbitrarily chosen atom such that . The IDB predicates are computed in the order , , , , …, , , . Rule (6) is easily verified to be safe, and it is instructive to note that all variables of occur in its head.
| (5) | |||||
| for | (6) | ||||
| for | (7) | ||||
Example 21.
Let . Then, , , and . We obtain a program with the following rules:
Let be the following database instance:
Note that , which is obtained by a repair that consists of the facts marked with . Our program computes the following facts:
We will show that, in general, the computed -fact contains the value .
Definition 22.
If is a set of valuations over a set of variables that contains , and is an aggregation function, then .
Definition 23 (AMCS).
Let be a query in . Let be a database instance. An aggregation-maximal consistent set (AMCS) (with respect to and ) is a set of valuations over such that
-
(A)
Homomorphic: for every , ;
-
(B)
Consistent: ; and
- (C)
The following lemma has an easy proof.
Lemma 24.
Let be a query in such that is monotone. Let be a database instance. If is an AMCS, then .
Using the refined notion of AMCS defined next, we will show that the program of Fig. 4 recursively computes the value of an AMCS.
Definition 25 (-AMCS).
Let be a query in such that has an acyclic attack graph, with topological sort . Let be a database instance. Let , and let be a valuation over . An -AMCS is a set of valuations over such that
-
(A)
Extending: for every , ;
-
(B)
Homomorphic: for every , ;
-
(C)
Consistent: ; and
- (D)
Let be the set of all numbers in the -column of used in rule (5) of Fig. 4. If is a valuation over such that , then is an -AMCS.
Clearly, a -AMCS is an AMCS. Appendix A provides a proof sketch of the following lemma.
Lemma 26.
Let be a query in whose aggregate operator is monotone and associative. Suppose that is irreducible and has an acyclic attack graph, with topological sort . Let be a database instance, and . Then, the following are equivalent:
-
(A)
The program of Fig. 4 computes .
-
(B)
There is an -AMCS with such that .
Proof of Lemma 20.
Proof of Theorem 4.
Assume that is -acyclic. By Lemma 15, it is possible to construct, in polynomial time in the size of , a kernel of . Let . By Lemma 10, . By Definition 12, is irreducible. Since is -acyclic, the attack graph of is acyclic. By Lemma 20, is expressible in . Consequently, can be solved in by first reducing it to using the reduction, and then solving .
7 Inexpressibility for Non--Acyclic Queries
The right-to-left implication in Theorem 3 follows from Theorem 4. We now show the left-to-right implication, whose contraposition reads as follows: for each query in over , with , if is not -acyclic, then is not expressible in . The proof relies on a reduction from the problem (a.k.a. Bipartite Perfect Matching), which we recall next.
2-DIMENSIONAL MATCHING ().
- INSTANCE:
-
A set , where and are disjoint sets having the same number of elements.
- QUESTION:
-
Does contain a matching, that is, a subset such that and no two elements of agree in any coordinate?
Lemma 27.
Let be a numerical variable or a positive number. Let be a query in over . If the attack graph of has a cycle, then .
A key crux in the following proof of Theorem 3 is a deep result by Hella et al. [22], which implies that every query in is Hanf-local.
Proof of Theorem 3.
The right-to-left implication follows from Theorem 4. We show the left-to-right implication by contraposition. Assume that is not -acyclic. Let be a kernel of , and let . Since is not -acyclic, the attack graph of contains a cycle. By Lemma 27, . By Lemma 10, . Since first-order reductions compose, . In [33, Corollary 8.26 and Exercise 8.16], it is established that every query in a logic called is Hanf-local. It is easily verified that every query in is expressible in , and therefore cannot express . It follows that cannot be expressed in .
8 Special Cases and Free Variables
Consider rule (7) in the program of Fig. 4. If , then this rule aggregates over a singleton containing the result of a prior aggregation, say . Since by associativity, the rule is redundant if . A special case arises when is empty for each , in which case no intermediate aggregation is required. In [23], the term parsimonious aggregation was introduced to refer to aggregation queries that admit glb and lub rewritings in where aggregation is applied only at the end, but not intermediately. The same work defined the query class , which captures exactly all -queries (i.e., -queries with head ) that support parsimonious counting. It was also shown that strictly includes , for which Fuxman [18] demonstrated the possibility of parsimonious counting. Since parsimonious aggregation is a special case of aggregation, our results imply that every query in must be -acyclic. A purely syntactic proof of the following result can be found in [2, p. 81].
Proposition 28.
Every query in is -acyclic.
So far, we have focused on numerical terms without free variables. We now explain how our results extend to numerical terms with free variables , where is a self-join-free conjunction of atoms. Let be a sequence of distinct constants. Let be the conjunction obtained from by replacing, for , each occurrence of each by . Our results imply the following:
-
if is not -acyclic, then is not expressible in ; and
-
if is -acyclic, then there is a program in that solves . This expressibility holds not only for , but for every aggregate operator that is monotone and associative.
Since is self-join-free, different sequences of constants yield the same program up to a renaming of the constants. Thus, we can compute the program once by treating the free variables in as distinct constants. This approach is commonly used in CQA and logic in general [33, Lemma 2.3], but it breaks down in the presence of self-joins.
9 Conclusion and Future Research
The main finding of this paper is the new notion of -acyclicity, which allowed us (Theorem 3) to exactly characterize -queries that enable the computation of consistent (with respect to primary keys) least upper bounds in aggregate logic. One direction of this characterization extends to all aggregate operators that are monotone and associative (Theorem 4): if is -acyclic, then consistent least upper bounds for can be computed in aggregate logic. However, the inverse does not generally hold as some aggregate operators, such as , allow a straightforward computation of consistent least upper bounds.
A challenge for further research is to pinpoint the complexity of for -queries when is not in . In this paper, we have only established -hardness for these problems, but we have not determined the complexity upper bounds. Note that for , is equivalent to under first-order reductions (one direction of this equivalence follows from Lemma 27, and the proof of the other direction is straightforward). This challenge is non-trivial: while is known to be -hard [9], its complexity upper bound is still under investigation [13, 14].
References
- [1] Serge Abiteboul, Richard Hull, and Victor Vianu. Foundations of Databases. Addison-Wesley, 1995.
- [2] Aziz Amezian El Khalfioui. Computing Range Consistent Answers to Conjunctive Queries with Aggregation. PhD thesis, UMONS - University of Mons [Faculté des Sciences], Mons, Belgium, 18 September 2025. URL: https://hdl.handle.net/20.500.12907/53232.
- [3] Marcelo Arenas, Leopoldo E. Bertossi, and Jan Chomicki. Consistent query answers in inconsistent databases. In PODS, pages 68–79. ACM Press, 1999. doi:10.1145/303976.303983.
- [4] Marcelo Arenas, Leopoldo E. Bertossi, and Jan Chomicki. Scalar aggregation in FD-inconsistent databases. In ICDT, volume 1973 of Lecture Notes in Computer Science, pages 39–53. Springer, 2001. doi:10.1007/3-540-44503-X_3.
- [5] Marcelo Arenas, Leopoldo E. Bertossi, Jan Chomicki, Xin He, Vijay Raghavan, and Jeremy P. Spinrad. Scalar aggregation in inconsistent databases. Theor. Comput. Sci., 296(3):405–434, 2003. doi:10.1016/S0304-3975(02)00737-5.
- [6] Leopoldo E. Bertossi. Database Repairing and Consistent Query Answering. Synthesis Lectures on Data Management. Morgan & Claypool Publishers, 2011. doi:10.2200/S00379ED1V01Y201108DTM020.
- [7] Leopoldo E. Bertossi. Database repairs and consistent query answering: Origins and further developments. In PODS, pages 48–58. ACM, 2019. doi:10.1145/3294052.3322190.
- [8] Andrei A. Bulatov. Complexity of conservative constraint satisfaction problems. ACM Trans. Comput. Log., 12(4):24:1–24:66, 2011. doi:10.1145/1970398.1970400.
- [9] Ashok K. Chandra, Larry J. Stockmeyer, and Uzi Vishkin. Constant depth reducibility. SIAM J. Comput., 13(2):423–439, 1984. doi:10.1137/0213028.
- [10] Sara Cohen, Werner Nutt, and Yehoshua Sagiv. Rewriting queries with arbitrary aggregation functions using views. ACM Trans. Database Syst., 31(2):672–715, 2006. doi:10.1145/1138394.1138400.
- [11] Sara Cohen, Werner Nutt, and Alexander Serebrenik. Algorithms for rewriting aggregate queries using views. In DMDW, volume 19 of CEUR Workshop Proceedings, page 9. CEUR-WS.org, 1999. URL: https://ceur-ws.org/Vol-19/paper9.pdf.
- [12] Akhil A. Dixit and Phokion G. Kolaitis. Consistent answers of aggregation queries via SAT. In ICDE, pages 924–937. IEEE, 2022. doi:10.1109/ICDE53745.2022.00074.
- [13] Stephen A. Fenner, Rohit Gurjar, and Thomas Thierauf. A deterministic parallel algorithm for bipartite perfect matching. Commun. ACM, 62(3):109–115, 2019. doi:10.1145/3306208.
- [14] Stephen A. Fenner, Rohit Gurjar, and Thomas Thierauf. Bipartite perfect matching is in quasi-NC. SIAM J. Comput., 50(3), 2021. doi:10.1137/16M1097870.
- [15] Diego Figueira, Anantha Padmanabha, Luc Segoufin, and Cristina Sirangelo. A simple algorithm for consistent query answering under primary keys. In ICDT, volume 255 of LIPIcs, pages 24:1–24:18. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2023. doi:10.4230/LIPIcs.ICDT.2023.24.
- [16] Diego Figueira, Anantha Padmanabha, Luc Segoufin, and Cristina Sirangelo. A simple algorithm for consistent query answering under primary keys. Logical Methods in Computer Science, Volume 21, Issue 1, February 2025. doi:10.46298/lmcs-21(1:18)2025.
- [17] Gaëlle Fontaine. Why is it hard to obtain a dichotomy for consistent query answering? ACM Trans. Comput. Log., 16(1):7:1–7:24, 2015. doi:10.1145/2699912.
- [18] Ariel Fuxman. Efficient query processing over inconsistent databases. PhD thesis, University of Toronto, 2007.
- [19] Ariel Fuxman, Elham Fazli, and Renée J. Miller. ConQuer: Efficient management of inconsistent databases. In SIGMOD Conference, pages 155–166. ACM, 2005. doi:10.1145/1066157.1066176.
- [20] Ariel Fuxman and Renée J. Miller. First-order query rewriting for inconsistent databases. In ICDT, volume 3363 of Lecture Notes in Computer Science, pages 337–351. Springer, 2005. doi:10.1007/978-3-540-30570-5_23.
- [21] Miika Hannula and Jef Wijsen. A dichotomy in consistent query answering for primary keys and unary foreign keys. In PODS, pages 437–449. ACM, 2022. doi:10.1145/3517804.3524157.
- [22] Lauri Hella, Leonid Libkin, Juha Nurmonen, and Limsoon Wong. Logics with aggregate operators. J. ACM, 48(4):880–907, 2001. doi:10.1145/502090.502100.
- [23] Aziz Amezian El Khalfioui and Jef Wijsen. Consistent query answering for primary keys and conjunctive queries with counting. In ICDT, volume 255 of LIPIcs, pages 23:1–23:19. Schloss Dagstuhl – Leibniz-Zentrum für Informatik, 2023. doi:10.4230/LIPIcs.ICDT.2023.23.
- [24] Aziz Amezian El Khalfioui and Jef Wijsen. Computing range consistent answers to aggregation queries via rewriting. Proc. ACM Manag. Data, 2(5):218:1–218:19, 2024. doi:10.1145/3695836.
- [25] Benny Kimelfeld and Phokion G. Kolaitis. A unifying framework for incompleteness, inconsistency, and uncertainty in databases. Commun. ACM, 67(3):74–83, 2024. doi:10.1145/3624717.
- [26] Phokion G. Kolaitis, Nina Pardal, Jonni Virtema, and Jef Wijsen. Rewriting consistent answers on annotated data. Proc. ACM Manag. Data, 3(2):110:1–110:26, 2025. doi:10.1145/3725247.
- [27] Paraschos Koutris, Xiating Ouyang, and Jef Wijsen. Consistent query answering for primary keys on path queries. In PODS, pages 215–232. ACM, 2021. doi:10.1145/3452021.3458334.
- [28] Paraschos Koutris, Xiating Ouyang, and Jef Wijsen. Consistent query answering for primary keys on rooted tree queries. Proc. ACM Manag. Data, 2(2):76, 2024. doi:10.1145/3651139.
- [29] Paraschos Koutris and Jef Wijsen. Consistent query answering for self-join-free conjunctive queries under primary key constraints. ACM Trans. Database Syst., 42(2):9:1–9:45, 2017. doi:10.1145/3068334.
- [30] Paraschos Koutris and Jef Wijsen. Consistent query answering for primary keys and conjunctive queries with negated atoms. In PODS, pages 209–224. ACM, 2018. doi:10.1145/3196959.3196982.
- [31] Paraschos Koutris and Jef Wijsen. First-order rewritability in consistent query answering with respect to multiple keys. In PODS, pages 113–129. ACM, 2020. doi:10.1145/3375395.3387654.
- [32] Paraschos Koutris and Jef Wijsen. Consistent query answering for primary keys in datalog. Theory Comput. Syst., 65(1):122–178, 2021. doi:10.1007/S00224-020-09985-6.
- [33] Leonid Libkin. Elements of Finite Model Theory. Texts in Theoretical Computer Science. An EATCS Series. Springer, 2004. doi:10.1007/978-3-662-07003-1.
- [34] David Maier. Minimum covers in relational database model. J. ACM, 27(4):664–674, 1980. doi:10.1145/322217.322223.
- [35] Anantha Padmanabha, Luc Segoufin, and Cristina Sirangelo. A dichotomy in the complexity of consistent query answering for two atom queries with self-join. Proc. ACM Manag. Data, 2(2):74, 2024. doi:10.1145/3651137.
- [36] Jef Wijsen. On the first-order expressibility of computing certain answers to conjunctive queries over uncertain databases. In PODS, pages 179–190. ACM, 2010. doi:10.1145/1807085.1807111.
- [37] Jef Wijsen. Certain conjunctive query answering in first-order logic. ACM Trans. Database Syst., 37(2):9:1–9:35, 2012. doi:10.1145/2188349.2188351.
- [38] Jef Wijsen. Foundations of query answering on inconsistent databases. SIGMOD Rec., 48(3):6–16, 2019. doi:10.1145/3377391.3377393.
Appendix A Proof Sketch of Lemma 26
Definition 29 (Pre-attacks).
Let be a query in , and be a linear order on the atoms of . We write if and . For every atom in , we define as the closure of with respect to .
Let be an atom of , and . We say that pre-attacks (with respect to ), denoted , if contains a path with , , and such that no belongs to .
Lemma 30 (Lemma D.3.1 in [2]).
Let be an irreducible query in with an acyclic attack graph. Let be a topological sort of ’s attack graph. For every atom in , for every variable , we have .
Definition 31.
Let and be sequences of variables and constants, respectively, of the same length. If is a valuation such that and , then we say that satisfies , also denoted . For a set of valuations, denotes that each valuation in satisfies . We write for the valuation over that satisfies .
Proof sketch of Lemma 26.
The implication B A is the easier direction. We next outline the proof of A B, which proceeds by induction on decreasing . For the induction basis, , assume is computed. If is a valuation over satisfying , then is an -AMCS. Clearly, . For the induction step from to , we assume ; the other case, where , is simpler.
We use the notations shown in the Venn diagram of Fig. 5, so that the -atom has the form . Since is shared by and , we can assume from here on that all valuations satisfy and , for fixed sequences and .
Construction of an -AMCS with , for fixed .
Consider an -block of , . For , let be the valuation over that satisfies , and let be an -AMCS; possibly .
Let be such that for all , ; if this maximizing index is not unique, break the tie by picking the index whose corresponding comes first according to a fixed linear order on . Significantly, the fact chosen from the block may depend on , as illustrated next.
Example 32.
Consider the query where is a constant. Consider the database instance . We have . Then, is a -AMCS which uses , and is a -AMCS which uses . Finally, is a -AMCS.
Let denote the set of all valuations over that extend a valuation from and satisfy , hence . Let enumerate all pairs from such that is an -block of . The crux is now to show that
| (8) |
By the induction hypothesis and our construction, for every , . We next consider valuations drawn from two distinct components of the disjoint union in (8) (hence ). We show by induction on increasing that
| (9) |
If , the desired result (9) holds because and disagree on some variable of . For , the induction hypothesis is that . If , then (9) holds vacuously. So assume from here on. Assume that and agree on every variable of . It suffices to show .
We can extend and to (arbitrary) valuations and over such that . In particular, let for every . We remark that is disjoint from , the latter containing variables on which and may disagree. Consider any path in between and some variable from , that is, and . Since by Lemma 14, . Since by Lemma 30, there is a smallest index such that , hence , and thus . Consequently, if the -fact of is , the repair of the block , which equals , is independent of values assigned to , given . Hence, we can conclude that , as desired, assuming ties are broken according to a fixed linear order on . It then follows that is an -AMCS with .
Computation of for an -AMCS with .
Rules (6) and (7) of Fig. 4 are executed in moving from to , which read as follows with the notation in the Venn diagram of Fig. 5.
Assume that is computed by the program in Fig. 4. We need to show:
| there is an -AMCS with such that . |
The fact must be derived by the following rules:
| (10) | ||||
| (11) |
Let enumerate all triples in such that, for each , is a computed -fact, hence . Each such -fact must be derived by the following partial grounding of rule (10):
For every , there exists such that is an -fact in , and is a computed -fact. If there is more than one such , the smallest is chosen according to a fixed linear order on . By the induction hypothesis, there is an -AMCS, denoted , with such that . For every , let be a valuation over satisfying , and let . As argued in the first part of the induction step, is an -AMCS with and . This concludes the proof of Lemma 26.
