Abstract 1 Introduction 2 Related Work 3 Preliminaries 4 Aggregate Logic and CQA 5 Kernels and 𝜿-Acyclicity 6 Expressibility for 𝜿-Acyclic Queries (Theorem 4) 7 Inexpressibility for Non-𝜿-Acyclic Queries 8 Special Cases and Free Variables 9 Conclusion and Future Research References Appendix A Proof Sketch of Lemma 26

Computing Consistent Least Upper Bounds in Aggregate Logic

Aziz Amezian El Khalfioui ORCID Research fellow FNRS, University of Mons, Mons, Belgium Jef Wijsen ORCID University of Mons, Mons, Belgium
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 logic
Copyright and License:
[Uncaptioned image] © Aziz Amezian El Khalfioui and Jef Wijsen; licensed under Creative Commons License CC-BY 4.0
2012 ACM Subject Classification:
Information systems Relational database query languages
; Theory of computation Incomplete, inconsistent, and uncertain databases ; Theory of computation Logic and databases
Editors:
Balder ten Cate and Maurice Funk

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 R that agree on the primary key of R. A repair of a database instance picks exactly one tuple from each block. Given a Boolean query q, 𝖢𝖤𝖱𝖳𝖠𝖨𝖭𝖳𝖸(q) is then defined as the decision problem that takes a database instance 𝐝𝐛 as input, and asks whether q holds true in every repair of 𝐝𝐛. This problem, while commonly studied for Boolean queries, can be readily extended to queries with free variables x: a consistent answer to a query q(x) is any sequence c of constants, of length |x|, such that q(c) holds true in every repair. The computational complexity of 𝖢𝖤𝖱𝖳𝖠𝖨𝖭𝖳𝖸(q) is well understood for all queries q 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 g(), the function problems 𝖦𝖫𝖡𝖢𝖰𝖠(g()) and 𝖫𝖴𝖡𝖢𝖰𝖠(g()) take a database instance 𝐝𝐛 as input, and return, respectively, the glb and the lub of the set that contains each number returned by g() on some repair.

In the current paper, we consider numerical aggregation queries g() that take the following form in the extended datalog syntax of [10, 11]:

𝙰𝙶𝙶(r)q(u), (1)

where the body q(u) is a conjunction of atoms (a.k.a. subgoals), r is either a numeric variable occurring in u 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 θ1,θ2,,θn enumerate all homomorphisms (a.k.a. embeddings) of the body into 𝐝𝐛, then the query returns 𝙰𝙶𝙶({{θ1(r),θ2(r),,θn(r)}}). Note that the argument of 𝙰𝙶𝙶 is a multiset, because it is possible that θi(r)=θj(r) for ij. For this semantics to be well-defined, each θi(r) must necessarily belong to some numerical domain D. In this case, the aggregate query is said to be over D. In this paper, we take D to be 0.

In [24], progress has been made in studying the complexity of 𝖦𝖫𝖡𝖢𝖰𝖠(g()) for queries g() of the form (1), particularly regarding the conditions under which 𝖦𝖫𝖡𝖢𝖰𝖠(g()) can be expressed in an extension of first-order logic with aggregate operators. However, the analogous question for 𝖫𝖴𝖡𝖢𝖰𝖠(g()) 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 g() of the form (1):

Problem 𝖫𝖴𝖡𝖢𝖰𝖠(g()). Input: A database instance 𝐝𝐛 that may violate its primary key constraints. Output: max{[[g()]]𝐫)𝐫 is a repair of 𝐝𝐛}.

Here, [[g()]]𝐫 denotes the result of g() on 𝐫. The function problem 𝖦𝖫𝖡𝖢𝖰𝖠(q) is obtained by replacing max with min 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 0, 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 𝙰𝙶𝙶(r)q(u) such that u(q(u)) 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 g() in 𝖠𝖦𝖦𝖱[𝗌𝗃𝖿𝖡𝖢𝖰], determine the computational complexity of the function problem 𝖫𝖴𝖡𝖢𝖰𝖠(g()).

The restriction to self-join-free queries is due to the following reason. The computational complexity of 𝖢𝖤𝖱𝖳𝖠𝖨𝖭𝖳𝖸(q) is well understood whenever q 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 g()=𝚂𝚄𝙼(r)q(u), 𝖦𝖫𝖡𝖢𝖰𝖠(g()) agrees with 𝖢𝖤𝖱𝖳𝖠𝖨𝖭𝖳𝖸(u(q(u))) insofar as it seeks to minimize the set of embeddings, in contrast to 𝖫𝖴𝖡𝖢𝖰𝖠(g()), which maximizes the set of embeddings. This difference explains why the two problems require different techniques.

Figure 1: Database instance 𝐝𝐛𝖲𝗍𝗈𝖼𝗄. Blocks are separated by dashed lines.
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:

𝚂𝚄𝙼(y)𝐷𝑒𝑎𝑙𝑒𝑟𝑠(Smith,t),𝑆𝑡𝑜𝑐𝑘(p,t¯,y).

In Fig. 1, the repair composed of the tuples preceded by yields the answer 75 (=40+35), which is the greatest result achievable among all repairs. Notably, the smallest result among all repairs is 0, obtained by a repair that picks the tuple “Smith”,“Philadelphia” from 𝐷𝑒𝑎𝑙𝑒𝑟𝑠, thereby falsifying the body of this 𝚂𝚄𝙼-query.  

In our complexity study, we specifically aim to understand under which conditions 𝖫𝖴𝖡𝖢𝖰𝖠(g()) 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 g() in 𝖠𝖦𝖦𝖱[𝗌𝗃𝖿𝖡𝖢𝖰], and asks whether or not there is a numerical query φ() in 𝖠𝖦𝖦𝖱[𝖥𝖮𝖫] that solves 𝖫𝖴𝖡𝖢𝖰𝖠(g()); moreover, when such φ() exists, we are interested in constructing it, a task loosely referred to as “(consistent) lub rewriting of g() 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 𝚂𝚄𝙼()=0:

Theorem 2 ([24]).

Let r be a numerical variable or a positive number. For each query g():=𝚂𝚄𝙼(r)q(u) in 𝖠𝖦𝖦𝖱[𝗌𝗃𝖿𝖡𝖢𝖰] over 0, 𝖦𝖫𝖡𝖢𝖰𝖠(g()) is expressible in 𝖠𝖦𝖦𝖱[𝖥𝖮𝖫] if and only if the attack graph of u(q(u)) is acyclic.

Our results show that the right-to-left implication in Theorem 2 fails if 𝖦𝖫𝖡𝖢𝖰𝖠(g()) is replaced with 𝖫𝖴𝖡𝖢𝖰𝖠(g()), while keeping all other conditions the same. That is, acyclicity of the attack graph is not a sufficient condition for 𝖫𝖴𝖡𝖢𝖰𝖠(g()) 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 r be a numerical variable or a positive number. For each query g():=𝚂𝚄𝙼(r)q(u) in 𝖠𝖦𝖦𝖱[𝗌𝗃𝖿𝖡𝖢𝖰] over 0, 𝖫𝖴𝖡𝖢𝖰𝖠(g()) is expressible in 𝖠𝖦𝖦𝖱[𝖥𝖮𝖫] if and only if u(q(u)) is κ-acyclic.

𝙲𝙾𝚄𝙽𝚃-queries of the form 𝙲𝙾𝚄𝙽𝚃(r)q(u) can be expressed as 𝚂𝚄𝙼(1)q(u), 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 g():=𝙰𝙶𝙶(r)q(u) be a query in 𝖠𝖦𝖦𝖱[𝗌𝗃𝖿𝖡𝖢𝖰] whose aggregate operator is monotone and associative. If u(q(u)) is κ-acyclic, then 𝖫𝖴𝖡𝖢𝖰𝖠(g()) can be expressed by a query in 𝖠𝖦𝖦𝖱[𝖥𝖮𝖫] (and such a query can be effectively constructed in polynomial time in the size of q(u)).

The converse of Theorem 4 does not hold in general. For example, 𝖫𝖴𝖡𝖢𝖰𝖠(g()) is expressible in 𝖠𝖦𝖦𝖱[𝖥𝖮𝖫] for all 𝙼𝙰𝚇-queries in 𝖠𝖦𝖦𝖱[𝗌𝗃𝖿𝖡𝖢𝖰], since the maximum over all repairs coincides with the result of the 𝙼𝙰𝚇-query on the original database.

Figure 2: Subclasses of self-join-free Boolean conjunctive queries, with example queries. The class of κ-acyclic queries is the largest class that allows lub rewriting for SUM-queries. The class of queries with an acyclic attack graph is the largest class that allows glb rewriting.

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:

SELECT D.Name, SUM(S.Qty)
FROM Dealers AS D, Stock AS S
WHERE D.Town = S.Town
GROUP BY D.Name

This query is stated as follows in the extended datalog syntax of [10]:

(x,𝚂𝚄𝙼(y))𝐷𝑒𝑎𝑙𝑒𝑟𝑠(x¯,t),𝑆𝑡𝑜𝑐𝑘(p,t¯,y).

Range semantics are obtained by replacing x 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.

This paper is largely based on [2, Chapter 7], in which the proofs are developed. Nevertheless, Section 6.2 introduces a novel approach that departs from that of [2].

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 q without aggregation, with respect to primary keys, has been studied in considerable depth. Its decision variant, which was coined 𝖢𝖤𝖱𝖳𝖠𝖨𝖭𝖳𝖸(q) in 2010 [36], asks whether a Boolean query q 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 q, 𝖢𝖤𝖱𝖳𝖠𝖨𝖭𝖳𝖸(q) is either in 𝖥𝖮, 𝖫-complete, or 𝖼𝗈𝖭𝖯-complete, and it is decidable, given q, 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 q, Fontaine [17] established interesting relationships between 𝖢𝖤𝖱𝖳𝖠𝖨𝖭𝖳𝖸(q) and Bulatov’s dichotomy theorem for conservative CSP [8]. Recently, Figueira et al. [15, 16] proposed a polynomial-time algorithm for solving 𝖢𝖤𝖱𝖳𝖠𝖨𝖭𝖳𝖸(q) for conjunctive queries q, 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 0, 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 (n,k,J) where n is the arity, {1,,k} is called the primary key, and J{1,,n} is the set of numerical positions (also called numerical columns). This relation name is full-key if n=k. Note that each relation name R is associated with exactly one key constraint, which is determined by the signature of R. For an n-tuple x=(x1,,xn), we write |x| to denote its arity n. We often blur the distinction between a sequence (x1,,xn) of distinct variables and the set {x1,,xn}, which is also denoted 𝗏𝖺𝗋𝗌(x). If x=(x1,,xn) and y=(y1,,ym), then we define their concatenation xy as the tuple (x1,,xn,y1,,ym).

Atoms, facts, and database instances.

Let R be a relation name of signature (n,k,J). An atom is an expression R(u1,,un) where each ui is either a constant or a variable, and for every jJ, uj is a numerical variable or a number in 0. 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 F is an atom, then 𝗏𝖺𝗋𝗌(F) is the set of variables that occur in F, and 𝖪𝖾𝗒(F) is the set of variables that occur in F at a position of the primary key. Further, we define 𝗇𝗈𝗍𝖪𝖾𝗒(F):=𝗏𝖺𝗋𝗌(F)𝖪𝖾𝗒(F). A fact is an atom without variables. A fact with relation name R is also called an R-fact. Two facts R1(a1¯,c1) and R2(a2¯,c2) are said to be key-equal if R1=R2 and a1=a2.

A database instance is a finite set of facts. If R is a relation name, then the R-relation of 𝐝𝐛 is the set of all R-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 R(a¯,c) in 𝐝𝐛, denoted R(a¯,), is the set of all facts in 𝐝𝐛 that are key-equal to that fact.

Valuation.

A valuation over a finite set U of variables is a total mapping θ from U to 𝐝𝐨𝐦 such that θ(r)0 for every numerical variable r. For a valuation θ over U, we write 𝖽𝗈𝗆(θ) to denote its domain U. A valuation θ over U is extended to every element u in 𝐯𝐚𝐫𝐝𝐨𝐦 by letting θ(u)=u for every uU.

Let θ be a valuation. If F is the atom R(u1,,un), then θ(F):=R(θ(u1),,θ(un)). If q is a set of atoms, then θ(q):={θ(F)Fq}. Notably, every variable in 𝗏𝖺𝗋𝗌(q)𝖽𝗈𝗆(θ) remains a variable in θ(q), where 𝗏𝖺𝗋𝗌(q)=Fq𝗏𝖺𝗋𝗌(F).

Partial valuation.

Let 𝐝𝐛 be a database instance, and φ(x) a first-order formula with free variables x. Let θ be a valuation. Then we write (𝐝𝐛,θ)φ(x) to denote that θ can be extended to a valuation θ over 𝖽𝗈𝗆(θ)𝗏𝖺𝗋𝗌(x) such that for a:=θ(x), we have 𝐝𝐛φ(a) using standard semantics (see, e.g., [33, p. 15]). Typically, but not necessarily, 𝖽𝗈𝗆(θ)𝗏𝖺𝗋𝗌(x). 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 q is a closed first-order formula u(R1(v1)Rn(vn)), where each Ri(vi) is an atom, u is a sequence containing every variable occurring in some vi, and ij implies RiRj. The conjunction R1(v1)Rn(vn), whose free variables are u, 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 q, its body, and the set {R1(v1),,Rn(vn)}. For example, if F is an atom of (the body of) q, then q{F} is the query obtained from q by deleting F from its body.

Gaifman graph.

The Gaifman graph of q, denoted 𝒢𝑎𝑖𝑓𝑚𝑎𝑛(q), is an undirected simple graph whose vertex-set is 𝗏𝖺𝗋𝗌(q). There is an edge between x and y if xy and some atom of q contains both x and y.

Guardedness.

Let q1 and q2 be two queries in 𝗌𝗃𝖿𝖡𝖢𝖰. We say that q1 is guarded by q2, denoted q1𝗀q2, if for every atom F in q1, there exists an atom G in q2 such that 𝗏𝖺𝗋𝗌(F)𝗏𝖺𝗋𝗌(G). We write q1𝗀q2 if both q1𝗀q2 and q2𝗀q1. It is straightforward that q1𝗀q2 implies 𝒢𝑎𝑖𝑓𝑚𝑎𝑛(q1)=𝒢𝑎𝑖𝑓𝑚𝑎𝑛(q2) (but the converse does not hold).

q0 q0
Figure 3: Attack graphs for two queries in 𝗌𝗃𝖿𝖡𝖢𝖰. The query q0 on the right is derived from the query on the left by replacing T(z¯,x) with the full-key atom T(z,x¯).

Attack graph.

Attack graphs for queries q 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 𝒦(q) for the set of functional dependencies that contains 𝖪𝖾𝗒(F)𝗏𝖺𝗋𝗌(F) whenever Fq. For Fq, we define F+,q:={x𝗏𝖺𝗋𝗌(q)𝒦(q{F})𝖪𝖾𝗒(F)x}, where is the standard notion of logical implication. An atom F of q is said to attack a variable x, denoted Fqx, if 𝒢𝑎𝑖𝑓𝑚𝑎𝑛(q) contains a (possibly empty) path between some variable in 𝗇𝗈𝗍𝖪𝖾𝗒(F) and x such that no variable on the path belongs to F+,q. The attack graph of q is a directed simple graph whose vertices are the atoms of q. There is a directed edge from F to G, denoted FqG, if F attacks some variable of 𝗏𝖺𝗋𝗌(G).

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, R is used as a shorthand for the atom R(x,y¯,z).

Example 5.

Consider the query q0 from 𝗌𝗃𝖿𝖡𝖢𝖰 shown on the left side of Fig. 3. The directed edges represent attacks. We have R+,q0={x,y}, S+,q0={z,x}, and T+,q0={z,x}. The sequence (z) entails Rq0S and Rq0T. The query q0 on the right side is obtained from q0 by replacing the T-atom with a full-key atom, which introduces a cycle in the attack graph.  

Let q(u) now be a self-join-free conjunction of n atoms such that the attack graph of u(q(u)) is acyclic. The following definitions are relative to a fixed topological sort (R1,,Rn) of q’s attack graph. We define the following sequences of variables for {1,,n}:

  • u contains all (and only) variables of i=1𝗏𝖺𝗋𝗌(Ri). Thus, un=u;

  • x contains the variables of 𝖪𝖾𝗒(R) that do not already occur in i=11𝗏𝖺𝗋𝗌(Ri); and

  • y contains the variables of 𝗇𝗈𝗍𝖪𝖾𝗒(R) that do not already occur in i=11𝗏𝖺𝗋𝗌(Ri).

Moreover, we define u0=(), the empty sequence. With this notation, we have that for every {1,,n}, u=(u1,x,y).

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 𝖫𝖴𝖡𝖢𝖰𝖠(g(x)), for arbitrary numerical terms g(x) in 𝖠𝖦𝖦𝖱[𝖥𝖮𝖫] with free variables x. Our study will subsequently focus on closed numerical terms g() 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 X and Y, we have:

(XY)=({{(X)}}Y), (2)

where denotes union of multisets. By letting X= in Eq. 2, we obtain that if is associative, then for all multisets Y, we have (Y)=({{()}}Y). This condition holds for 𝚂𝚄𝙼 and 𝙼𝙰𝚇 over 0 if and only if we define 𝚂𝚄𝙼():=0 and 𝙼𝙰𝚇():=0.

Example 6.

Examples of associative aggregate operators are 𝚂𝚄𝙼 and 𝙼𝙰𝚇. On the other hand, 𝙰𝚅𝙶, 𝙲𝙾𝚄𝙽𝚃, and 𝚂𝚄𝙼𝙳𝙸𝚂𝚃𝙸𝙽𝙲𝚃 are not associative. For instance, 𝙲𝙾𝚄𝙽𝚃({{5,6,7,8}})=4 and 𝙲𝙾𝚄𝙽𝚃({{𝙲𝙾𝚄𝙽𝚃({{5,6,7}}),8}})=𝙲𝙾𝚄𝙽𝚃({{3,8}})=2. Also, 𝙼𝙸𝙽 lacks associativity over 0 because any choice for 𝙼𝙸𝙽() disrupts associativity. In particular, if we define 𝙼𝙸𝙽():=0, then 𝙼𝙸𝙽({{1}})𝙼𝙸𝙽({{𝙼𝙸𝙽()}}{{1}})=0.  

An aggregate operator is monotone if for all m0 and every (possibly empty) multiset Y, we have:

({{x1,,xm}})({{x1,,xm}}Y) whenever xixi for every i. (3)

By letting m=0 in Eq. 3, we obtain that if is monotone, then for all multisets Y, we have ()(Y). Again, this condition holds for 𝚂𝚄𝙼 and 𝙼𝙰𝚇 over 0 if and only if we define 𝚂𝚄𝙼():=0 and 𝙼𝙰𝚇():=0.

Example 7.

Examples of monotone aggregate operators are 𝚂𝚄𝙼, 𝙼𝙰𝚇, and 𝙲𝙾𝚄𝙽𝚃. Note that 𝙼𝙸𝙽 is not monotone since 𝙼𝙸𝙽({{3}})>𝙼𝙸𝙽({{2,3}}). 𝙲𝙾𝚄𝙽𝚃𝙳𝙸𝚂𝚃𝙸𝙽𝙲𝚃 also lacks monotonicity: if we increase 3 to 4 in the multiset {{3,4}}, the number returned by 𝙲𝙾𝚄𝙽𝚃𝙳𝙸𝚂𝚃𝙸𝙽𝙲𝚃 drops from 2 to 1.  

Significantly, an aggregate operator that is not monotone in general may become monotone under a restriction of its underlying domain. For example, 𝙿𝚁𝙾𝙳𝚄𝙲𝚃, defined by 𝙿𝚁𝙾𝙳𝚄𝙲𝚃({{x1,,xm}}):=Πi=1mxi, is not monotone over 0 (because 𝙿𝚁𝙾𝙳𝚄𝙲𝚃({{1}})>𝙿𝚁𝙾𝙳𝚄𝙲𝚃({{1,12}})), but is monotone over 1.

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 q(x,y) be a formula in 𝖠𝖦𝖦𝖱[𝖥𝖮𝖫], where x, y are disjoint sequences that together contain each free variable of q exactly once. A primitive numerical term is either a non-negative rational number or a numerical variable in xy. For every possible aggregate operator , a primitive numerical term r, and a formula q(x,y), we have a new numerical term

g(x):=𝖠𝗀𝗀𝗋y[r,q(x,y)].

Variables y that are free in q(x,y) become bound in 𝖠𝗀𝗀𝗋y[r,q(x,y)]; in this respect 𝖠𝗀𝗀𝗋y behaves like a sort of quantification over y. Next, we define the semantics.

Let a be a sequence of constants of length |x|. The value g(a) on a database instance 𝐝𝐛, denoted [[g(a)]]𝐝𝐛, is calculated as follows. Let {θ1,θ2,,θm} be a (possibly empty) -maximal set of valuations over xy such that θi(x)=a and (𝐝𝐛,θi)q(x,y) for 1im. Then [[g(a)]]𝐝𝐛=({{θ1(r),,θm(r)}}). Note that the argument of is in general a multiset, since θi(r) may be equal to θj(r) for ij. A numerical term without free variables is also called a numerical query, denoted g().

Example 8.

If g0(t):=𝖠𝗀𝗀𝗋𝚂𝚄𝙼(p,z)[z,𝑆𝑡𝑜𝑐𝑘(p,t,z)], then on the example database of Fig. 1, g0(“Boston”)=110, the sum of quantities in Boston. This numerical term can be used in other formulas. For example, q0(t,y):=pz(𝑆𝑡𝑜𝑐𝑘(p,t,z))y=g0(t) returns, for each town t, the total quantity y of products stored in t.  

4.3 Least Upper Bounds Across All Repairs

For each numerical term g(x):=𝖠𝗀𝗀𝗋y[r,q(x,y)], we define 𝖫𝖴𝖡𝖢𝖰𝖠(g(x)) relative to a database instance 𝐝𝐛. Let a be a sequence of constants of length |x|. Then,

[[𝖫𝖴𝖡𝖢𝖰𝖠(g(a))]]𝐝𝐛:=max{[[g(a)]]𝐫𝐫𝗋𝗌𝖾𝗍(𝐝𝐛)}.

A general research question is: under which conditions can 𝖫𝖴𝖡𝖢𝖰𝖠(g(x)) be expressed in 𝖠𝖦𝖦𝖱[𝖥𝖮𝖫]? In this paper, we study this question for the case of numerical terms g(x):=𝖠𝗀𝗀𝗋y[r,q(x,y)] where q(x,y) is a self-join-free conjunction of atoms. For readability, we often express such a numerical term g(x) using the following datalog-like syntax:

(x,𝙰𝙶𝙶(r))q(x,y),

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 x is empty, i.e., we are dealing with numerical terms g():=𝖠𝗀𝗀𝗋y[r,q(y)] without free variables, which are conveniently expressed as 𝙰𝙶𝙶(r)q(y). 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 𝖠𝗀𝗀𝗋y[r,q(y)] where q(y) is a self-join-free conjunction of atoms, and r is either a numerical variable or a non-negative number. An alternative syntax is 𝙰𝙶𝙶(r)q(y), where the aggregate symbol 𝙰𝙶𝙶 is interpreted by (which is often made explicit by writing 𝙰𝙶𝙶 instead of ). We call 𝙰𝙶𝙶(r) the head, and q(y) the body.

4.4 The Reduction Lemma

Let P1 and P2 be function problems whose output is a single number. If I is an instance of Pj, we write Pj(I) to denote the output of Pj on input I, where j{1,2}. We say that P1 is first-order reducible to P2, denoted P1𝖥𝖮P2 if there exists a first-order definable function f such that for every instance I of P1, f(I) is an instance of P2 such that P1(I)=P2(f(I)). We write P1𝗇𝗋𝖽𝖺𝗍𝖺𝗅𝗈𝗀P2 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 q1 as a shorthand for u(q1(u)) for readability.

Lemma 10 (Reduction Lemma).

Let g1():=𝙰𝙶𝙶(r)q1(u) and g2():=𝙰𝙶𝙶(r)q2(u) be queries in 𝖠𝖦𝖦𝖱[𝗌𝗃𝖿𝖡𝖢𝖰] such that 𝙰𝙶𝙶 is monotone. If q1𝗀q2 and 𝒦(q1)𝒦(q2), then 𝖫𝖴𝖡𝖢𝖰𝖠(g1())𝗇𝗋𝖽𝖺𝗍𝖺𝗅𝗈𝗀𝖫𝖴𝖡𝖢𝖰𝖠(g2()).

We illustrate Lemma 10 below and show that it does not hold for greatest lower bounds.

Example 11.

Consider the queries g1():=𝚂𝚄𝙼(1)q0(x,y,z) and g2():=𝚂𝚄𝙼(1)q0(x,y,z), where q0 and q0 are shown in Fig. 3. Their bodies are related by 𝗀 and induce the same set of functional dependencies (notably, {xyz,zx}). The Reduction Lemma implies that 𝖫𝖴𝖡𝖢𝖰𝖠(g1()) and 𝖫𝖴𝖡𝖢𝖰𝖠(g2()) are equivalent under first-order reductions. However, this equivalence does not hold for glb: indeed, by Theorem 2, 𝖦𝖫𝖡𝖢𝖰𝖠(g1()) is in 𝖠𝖦𝖦𝖱[𝖥𝖮𝖫], whereas 𝖦𝖫𝖡𝖢𝖰𝖠(g2()) 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 XA, where A 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 q be a query in 𝗌𝗃𝖿𝖡𝖢𝖰, and let q^ denote the set of atoms in q that are not full-key. We say that q is irreducible if the following two conditions hold:

  1. 1.

    for every atom F of q^, we have 𝒦(q^{F})𝒦(q^); and

  2. 2.

    the set {𝖪𝖾𝗒(F)𝗇𝗈𝗍𝖪𝖾𝗒(F)Fq^} is irreducible.

A kernel of q is an irreducible query q in 𝗌𝗃𝖿𝖡𝖢𝖰 such that 𝒦(q)𝒦(q) and q𝗀q.

Example 13.

Consider the queries q0={R(x,y¯,z),S(z¯,x),T(z¯,x)} and q0={R(x,y¯,z), S(z¯,x), T(z,x)¯} in Fig. 3. The query q0 is not irreducible, because it does not satisfy the first condition in Definition 12. The query q0 is irreducible. Since 𝒦(q0)𝒦(q0){xyz,zx} and q0𝗀q0, it follows that q0 is a kernel of q0. Notably, the attack graph of q0 is acyclic, whereas the attack graph of its kernel contains a cycle.

As a side remark, consider the query g():=𝚂𝚄𝙼(1)q0(x,y,z). By Theorem 2, 𝖦𝖫𝖡𝖢𝖰𝖠(g()) is expressible in 𝖠𝖦𝖦𝖱[𝖥𝖮𝖫], while, as we will see shortly, Theorem 3 implies that 𝖫𝖴𝖡𝖢𝖰𝖠(g()) is not expressible in 𝖠𝖦𝖦𝖱[𝖥𝖮𝖫]. This difference can be explained by the observation that T-blocks or S-blocks of size greater than 1 can be omitted when computing 𝖦𝖫𝖡𝖢𝖰𝖠(g()). For example, assume that 𝐝𝐛 contains S(a¯,c), T(a¯,b1), and T(a¯,b2) with b1b2. Then a repair 𝐫 can select S(a¯,c) and a fact T(a¯,bi), i{1,2}, such that bic. Consequently, no homomorphism from q0(x,y,z) to 𝐫 can map z to a. Preventing homomorphisms in this way minimizes the value of g(). On the other hand, computing 𝖫𝖴𝖡𝖢𝖰𝖠(g()) requires maximizing the number of embeddings. In that case, one would prefer selecting S(a¯,d) and T(a¯,d) if such a common d exists.  

Lemma 14.

Let q be a query in 𝗌𝗃𝖿𝖡𝖢𝖰 that is irreducible. Let G be an atom in q that is not full-key. Then, there is a variable x such that 𝗇𝗈𝗍𝖪𝖾𝗒(G)={x} and Gqx.

Proof.

By the second item in Definition 12, |𝗇𝗈𝗍𝖪𝖾𝗒(G)|=1. Hence, we can assume x such that 𝗇𝗈𝗍𝖪𝖾𝗒(G)={x}. Let q^ denote the set of atoms in q that are not full-key. By the first item in Definition 12, 𝒦(q^{G})𝒦(q^). It must obviously be the case that 𝒦(q^{G})⊧̸𝖪𝖾𝗒(G)x, which implies Gqx.

Lemma 15.

Let q be a query in 𝗌𝗃𝖿𝖡𝖢𝖰. A kernel of q can be constructed in polynomial time in the size of q.

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 q be a query in 𝗌𝗃𝖿𝖡𝖢𝖰. If some kernel of q has an acyclic attack graph, then every kernel of q has an acyclic attack graph.

The proof of Lemma 16 in [2, p. 133] also establishes the following result: if some kernel of q 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 q 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 r is either a number or a numerical variable:

P(x,r) R1(v1),,Rn(vn). (4)

Note that by the safety requirement of 𝗇𝗋𝖽𝖺𝗍𝖺𝗅𝗈𝗀, if r is a variable, it also occurs in the body of the rule. We allow such a rule to be replaced with:

P(x,𝙰𝙶𝙶(r)) R1(v1),,Rn(vn),

where 𝙰𝙶𝙶 is an aggregate symbol. The semantics on a given database 𝐝𝐛 is as follows. Let B(u) be the conjunction of all atoms in the body of the rule, where u is a shortest sequence containing every variable that occurs in the body of the rule. Let a be a sequence of constants of length |x|. Let {θ1,θ2,,θm} be a -maximal set of valuations over u such that θi(x)=a and (𝐝𝐛,θi)B(u) for 1im. If m1, then the rule derives P(a,p) with p=𝙰𝙶𝙶({{θ1(r),,θm(r)}}). Note that the argument of 𝙰𝙶𝙶 is in general a multiset, since θi(r) may be equal to θj(r) for ij. In case m=0, 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.

𝑇𝑜𝑡𝑎𝑙𝑆𝑡𝑜𝑐𝑘𝑃𝑒𝑟𝐶𝑖𝑡𝑦(t,𝚂𝚄𝙼(z)) 𝑆𝑡𝑜𝑐𝑘(p,t,z)
𝑀𝑎𝑥𝑆𝑡𝑜𝑐𝑘(𝙼𝙰𝚇(s)) 𝑇𝑜𝑡𝑎𝑙𝑆𝑡𝑜𝑐𝑘𝑃𝑒𝑟𝐶𝑖𝑡𝑦(t,s)
𝐴𝑛𝑠𝑤𝑒𝑟(t) 𝑇𝑜𝑡𝑎𝑙𝑆𝑡𝑜𝑐𝑘𝑃𝑒𝑟𝐶𝑖𝑡𝑦(t,s),𝑀𝑎𝑥𝑆𝑡𝑜𝑐𝑘(s)

Replacing 𝚂𝚄𝙼(z) and 𝙼𝙰𝚇(s) with z and s, 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 g():=𝙰𝙶𝙶(r)q(u) be a query in 𝖠𝖦𝖦𝖱[𝗌𝗃𝖿𝖡𝖢𝖰] whose aggregate operator is monotone and associative. If u(q(u)) is irreducible and has an acyclic attack graph, then an 𝖠𝖦𝖦𝖱[𝗇𝗋𝖽𝖺𝗍𝖺𝗅𝗈𝗀] program solving 𝖫𝖴𝖡𝖢𝖰𝖠(g()) can be constructed in polynomial time in the size of q(u).

We will now explain the 𝖠𝖦𝖦𝖱[𝗇𝗋𝖽𝖺𝗍𝖺𝗅𝗈𝗀] program in the statement of Lemma 20, and explain how its correctness is proved. Let u(q(u)) be a query in 𝗌𝗃𝖿𝖡𝖢𝖰 with an acyclic attack graph. Let (R1,,Rn) be a topological sort of the attack graph. We assume that r is a variable in u; our treatment can be easily specialized to the simpler case in which r is a constant. For i{1,,n}, the sequences ui, xi, and yi are defined in Section 3. Moreover, u0=(), the empty sequence. For i{0,1,,n}, we define ui as the restriction of ui to those variables that belong to {r}𝗏𝖺𝗋𝗌({Ri+1,Ri+2,,Rn}). In particular, u0=() and un=(r).

Our program contains the rules shown in Fig. 4, for {n,n1,,1}, where S is an arbitrarily chosen atom such that r𝗏𝖺𝗋𝗌(S). The IDB predicates are computed in the order En, Kn, En1, Kn1, …, E1, K1, E0. Rule (6) is easily verified to be safe, and it is instructive to note that all variables of 𝖪𝖾𝗒(R) occur in its head.

En(r,𝙰𝙶𝙶(r)) S (5)
K(u1,x,𝙼𝙰𝚇(r)) R,E(u,r) for 1n (6)
E1(u1,𝙰𝙶𝙶(m)) K(u1,x,m) for 1n (7)
Figure 4: Rules of an 𝖠𝖦𝖦𝖱[𝗇𝗋𝖽𝖺𝗍𝖺𝗅𝗈𝗀] program for computing the 𝙰𝙶𝙶 value of an AMCS, relative to a topological sort (R1,,Rn) of the attack graph of the query. S denotes an atom with r𝗏𝖺𝗋𝗌(S).
Example 21.

Let g0():=𝚂𝚄𝙼(r)R1(x¯,y),R2(y,z¯,r). Then, u1=xy, u1=y, and x2=z. We obtain a program with the following rules:

E2(r,𝚂𝚄𝙼(r)) R2(y,z¯,r)
K2(y,z,𝙼𝙰𝚇(r2)) R2(y,z¯,r),E2(r,r2)
E1(y,𝚂𝚄𝙼(m2)) K2(y,z,m2)
K1(x,𝙼𝙰𝚇(r1)) R1(x¯,y),E1(y,r1)
E0(𝚂𝚄𝙼(m1)) K1(x,m1)

Let 𝐝𝐛 be the following database instance:

Note that [[𝖫𝖴𝖡𝖢𝖰𝖠(g())]]𝐝𝐛=13, which is obtained by a repair that consists of the facts marked with . Our program computes the following facts:

E2(1,1),E2(4,4),E2(3,3),E2(5,5),E2(6,6),K2(b1,c1,4),K2(b1,c2,3),K2(b2,c3,6),E1(b1,7),E1(b2,6),K1(a1,7),K1(a2,6),E0(13).

We will show that, in general, the computed E0-fact contains the value 𝖫𝖴𝖡𝖢𝖰𝖠(g()).  

Definition 22.

If M is a set of valuations over a set U of variables that contains r, and 𝙰𝙶𝙶 is an aggregation function, then 𝙰𝙶𝙶(M)=𝙰𝙶𝙶({{θ(r)θM}}).

Definition 23 (AMCS).

Let g():=𝙰𝙶𝙶(r)q(u) be a query in 𝖠𝖦𝖦𝖱[𝗌𝗃𝖿𝖡𝖢𝖰]. Let 𝐝𝐛 be a database instance. An aggregation-maximal consistent set (AMCS) (with respect to g() and 𝐝𝐛) is a set M of valuations over 𝗏𝖺𝗋𝗌({R1,,Rn}) such that

  1. (A)

    Homomorphic: for every θM, (𝐝𝐛,θ){R1,,Rn};

  2. (B)

    Consistent: M𝒦({R1,,Rn}); and

  3. (C)

    𝙰𝙶𝙶-Maximal: among all sets of valuations over 𝗏𝖺𝗋𝗌({R1,,Rn}) satisfying the conditions in (A) and (B), M is one that maximizes 𝙰𝙶𝙶.

The following lemma has an easy proof.

Lemma 24.

Let g():=𝙰𝙶𝙶(r)q(u) be a query in 𝖠𝖦𝖦𝖱[𝗌𝗃𝖿𝖡𝖢𝖰] such that 𝙰𝙶𝙶 is monotone. Let 𝐝𝐛 be a database instance. If M is an AMCS, then 𝙰𝙶𝙶(M)=[[𝖫𝖴𝖡𝖢𝖰𝖠(g())]]𝐝𝐛.

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 g():=𝙰𝙶𝙶(r)q(u) be a query in 𝖠𝖦𝖦𝖱[𝗌𝗃𝖿𝖡𝖢𝖰] such that u(q(u)) has an acyclic attack graph, with topological sort (R1,R2,,Rn). Let 𝐝𝐛 be a database instance. Let {1,,n}, and let λ be a valuation over 𝗏𝖺𝗋𝗌(u1). An (,λ)-AMCS is a set M of valuations over 𝗏𝖺𝗋𝗌({R,,Rn}){r} such that

  1. (A)

    Extending: for every θM, θ(u1)=λ(u1);

  2. (B)

    Homomorphic: for every θM, (𝐝𝐛,θ){R,,Rn};

  3. (C)

    Consistent: M𝒦({R,,Rn}); and

  4. (D)

    𝙰𝙶𝙶-Maximal: among all sets of valuations over 𝗏𝖺𝗋𝗌({R,,Rn}){r} satisfying the conditions in (A), (B) and (C), M is one that maximizes 𝙰𝙶𝙶.

Let Q be the set of all numbers in the r-column of S used in rule (5) of Fig. 4. If λ is a valuation over 𝗏𝖺𝗋𝗌(un)={r} such that λ(r)Q, then {λ} is an (n+1,λ)-AMCS.

Clearly, a (1,)-AMCS is an AMCS. Appendix A provides a proof sketch of the following lemma.

Lemma 26.

Let g():=𝙰𝙶𝙶(r)q(u) be a query in 𝖠𝖦𝖦𝖱[𝗌𝗃𝖿𝖡𝖢𝖰] whose aggregate operator is monotone and associative. Suppose that u(q(u)) is irreducible and has an acyclic attack graph, with topological sort (R1,R2,,Rn). Let 𝐝𝐛 be a database instance, and {0,1,2,,n}. Then, the following are equivalent:

  1. (A)

    The program of Fig. 4 computes E(c,o).

  2. (B)

    There is an (+1,λ)-AMCS M with λ(u)=c such that 𝙰𝙶𝙶(M)=o.

We can now provide the proofs of Lemma 20 and Theorem 4.

Proof of Lemma 20.

By letting =0 in Lemma 26, we obtain that E0((),o) is computed if and only if there is a (1,)-AMCS M such that 𝙰𝙶𝙶(M)=o. Since a (1,)-AMCS is an AMCS, the desired result follows by Lemma 24.

Proof of Theorem 4.

Assume that u(q(u)) is κ-acyclic. By Lemma 15, it is possible to construct, in polynomial time in the size of q(u), a kernel u(q(u)) of u(q(u)). Let g():=𝙰𝙶𝙶(r)q(u). By Lemma 10, 𝖫𝖴𝖡𝖢𝖰𝖠(g())𝗇𝗋𝖽𝖺𝗍𝖺𝗅𝗈𝗀𝖫𝖴𝖡𝖢𝖰𝖠(g()). By Definition 12, u(q(u)) is irreducible. Since u(q(u)) is κ-acyclic, the attack graph of u(q(u)) is acyclic. By Lemma 20, 𝖫𝖴𝖡𝖢𝖰𝖠(g()) is expressible in 𝖠𝖦𝖦𝖱[𝗇𝗋𝖽𝖺𝗍𝖺𝗅𝗈𝗀]. Consequently, 𝖫𝖴𝖡𝖢𝖰𝖠(g()) can be solved in 𝖠𝖦𝖦𝖱[𝗇𝗋𝖽𝖺𝗍𝖺𝗅𝗈𝗀] by first reducing it to 𝖫𝖴𝖡𝖢𝖰𝖠(g()) using the 𝗇𝗋𝖽𝖺𝗍𝖺𝗅𝗈𝗀 reduction, and then solving 𝖫𝖴𝖡𝖢𝖰𝖠(g()).

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 g():=𝚂𝚄𝙼(r)q(u) in 𝖠𝖦𝖦𝖱[𝗌𝗃𝖿𝖡𝖢𝖰] over 0, with r0, if u(q(u)) is not κ-acyclic, then 𝖫𝖴𝖡𝖢𝖰𝖠(g()) 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 (2DM).

 

INSTANCE:

A set MA×B, where A and B are disjoint sets having the same number n of elements.

QUESTION:

Does M contain a matching, that is, a subset MM such that |M|=n and no two elements of M agree in any coordinate?

 

Lemma 27.

Let r be a numerical variable or a positive number. Let g():=𝚂𝚄𝙼(r)q(u) be a query in 𝖠𝖦𝖦𝖱[𝗌𝗃𝖿𝖡𝖢𝖰] over 0. If the attack graph of u(q(u)) has a cycle, then 𝟤𝖣𝖬𝖥𝖮𝖫𝖴𝖡𝖢𝖰𝖠(g()).

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 u(q(u)) is not κ-acyclic. Let u(q(u)) be a kernel of u(q(u)), and let g():=𝚂𝚄𝙼(r)q(u). Since u(q(u)) is not κ-acyclic, the attack graph of u(q(u)) contains a cycle. By Lemma 27, 𝟤𝖣𝖬𝖥𝖮𝖫𝖴𝖡𝖢𝖰𝖠(g()). By Lemma 10, 𝖫𝖴𝖡𝖢𝖰𝖠(g())𝖥𝖮𝖫𝖴𝖡𝖢𝖰𝖠(g()). Since first-order reductions compose, 𝟤𝖣𝖬𝖥𝖮𝖫𝖴𝖡𝖢𝖰𝖠(g()). In [33, Corollary 8.26 and Exercise 8.16], it is established that every query in a logic called aggr is Hanf-local. It is easily verified that every query in 𝖠𝖦𝖦𝖱[𝖥𝖮𝖫] is expressible in aggr, and therefore cannot express 𝟤𝖣𝖬. It follows that 𝖫𝖴𝖡𝖢𝖰𝖠(g()) cannot be expressed in 𝖠𝖦𝖦𝖱[𝖥𝖮𝖫].

8 Special Cases and Free Variables

Consider rule (7) in the 𝖠𝖦𝖦𝖱[𝗇𝗋𝖽𝖺𝗍𝖺𝗅𝗈𝗀] program of Fig. 4. If x=(), then this rule aggregates over a singleton containing the result m of a prior aggregation, say m=𝙰𝙶𝙶(X). Since 𝙰𝙶𝙶({{𝙰𝙶𝙶(X)}})=𝙰𝙶𝙶(X) by associativity, the rule is redundant if x=(). A special case arises when x 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 𝚂𝚄𝙼(1)) 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 g() without free variables. We now explain how our results extend to numerical terms g(x)=𝖠𝗀𝗀𝗋𝚂𝚄𝙼y[r,q(x,y)] with free variables x:=(x1,,xk), where q(x,y) is a self-join-free conjunction of atoms. Let c=(c1,,ck) be a sequence of distinct constants. Let qc(y) be the conjunction obtained from q(x,y) by replacing, for i{1,,k}, each occurrence of each xi by ci. Our results imply the following:

  • if y(qc(y)) is not κ-acyclic, then 𝖫𝖴𝖡𝖢𝖰𝖠(g(c)) is not expressible in 𝖠𝖦𝖦𝖱[𝖥𝖮𝖫]; and

  • if y(qc(y)) is κ-acyclic, then there is a program φc in 𝖠𝖦𝖦𝖱[𝗇𝗋𝖽𝖺𝗍𝖺𝗅𝗈𝗀] that solves 𝖫𝖴𝖡𝖢𝖰𝖠(g(c)). This expressibility holds not only for 𝚂𝚄𝙼, but for every aggregate operator that is monotone and associative.

Since q(x,y) is self-join-free, different sequences c of constants yield the same program φc up to a renaming of the constants. Thus, we can compute the program once by treating the free variables in x 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 u(q(u)) is κ-acyclic, then consistent least upper bounds for 𝙰𝙶𝙶(r)q(u) 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 𝖫𝖴𝖡𝖢𝖰𝖠(g()) for 𝚂𝚄𝙼-queries g() when 𝖫𝖴𝖡𝖢𝖰𝖠(g()) 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 g0():=𝚂𝚄𝙼(1)R(x¯,y),S(y¯,x), 𝖦𝖫𝖡𝖢𝖰𝖠(g0) 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 q be a query in 𝗌𝗃𝖿𝖡𝖢𝖰, and be a linear order on the atoms of q. We write F<G if FG and FG. For every atom G in q, we define G+,(q,<) as the closure of 𝖪𝖾𝗒(G) with respect to {𝖪𝖾𝗒(F)𝗏𝖺𝗋𝗌(F)F<G}.

Let G be an atom of q, and u𝗏𝖺𝗋𝗌(q). We say that G pre-attacks u (with respect to (q,)), denoted G(q,)u, if 𝒢𝑎𝑖𝑓𝑚𝑎𝑛(q) contains a path (v0,v1,,vn) with n0, v0𝗇𝗈𝗍𝖪𝖾𝗒(G), and vn=u such that no vi belongs to G+,(q,<).

Lemma 30 (Lemma D.3.1 in [2]).

Let q be an irreducible query in 𝗌𝗃𝖿𝖡𝖢𝖰 with an acyclic attack graph. Let be a topological sort of q’s attack graph. For every atom G in q, for every variable uF<G𝗏𝖺𝗋𝗌(F), we have G↬̸(q,)u.

Definition 31.

Let x and a be sequences of variables and constants, respectively, of the same length. If θ is a valuation such that 𝗏𝖺𝗋𝗌(x)𝖽𝗈𝗆(θ) and θ(x)=a, then we say that θ satisfies x=a, also denoted θx=a. For a set M of valuations, Mx=a denotes that each valuation in M satisfies x=a. We write xa for the valuation over 𝗏𝖺𝗋𝗌(x) that satisfies x=a.

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 =n,n1,,0. For the induction basis, =n, assume En(o,𝙰𝙶𝙶(o)) is computed. If λ is a valuation over {r} satisfying r=o, then {λ} is an (n+1,λ)-AMCS. Clearly, 𝙰𝙶𝙶({λ})=𝙰𝙶𝙶({λ(r)})=𝙰𝙶𝙶(o). For the induction step from to 1, we assume 𝗇𝗈𝗍𝖪𝖾𝗒(R)={y}𝗏𝖺𝗋𝗌(u); the other case, where 𝗇𝗈𝗍𝖪𝖾𝗒(R)𝗏𝖺𝗋𝗌(u), is simpler.

Figure 5: Notations.

We use the notations shown in the Venn diagram of Fig. 5, so that the R-atom has the form R(x,x+,x ,x¯,y). Since zx+ is shared by u and u1, we can assume from here on that all valuations satisfy z=d and x+=a+, for fixed sequences d and a+.

The remainder of the proof consists of two parts. First, we show how an (,λ)-AMCS can be constructed from (+1,λi)-AMCSs. Second, we argue that rules (6) and (7) of the 𝖠𝖦𝖦𝖱[𝗇𝗋𝖽𝖺𝗍𝖺𝗅𝗈𝗀] program in Fig. 4 capture this construction.

Construction of an (,𝝀)-AMCS with 𝝀𝒛𝒙𝒙+=𝒅𝒂𝒂+, for fixed 𝒂.

Consider an R-block 𝐛 of 𝐝𝐛, 𝐛={R(a,a+,a ,a¯,bi)}i=1|𝐛|. For i{1,,|𝐛|}, let λi be the valuation over u that satisfies zx+xy=da+abi, and let Mi be an (+1,λi)-AMCS; possibly Mi=.

Let i{1,,|𝐛|} be such that for all j{1,,|𝐛|}, 𝙰𝙶𝙶(Mi)𝙰𝙶𝙶(Mj); if this maximizing index is not unique, break the tie by picking the index i whose corresponding bi 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 𝚂𝚄𝙼(r)R1(c¯,r),R2(c¯,y),R3(y,r¯) where c is a constant. Consider the database instance 𝐝𝐛={R1(c¯,5),R1(c¯,7),R2(c¯,a),R2(c¯,b),R3(a,5¯),R3(b,7¯)}. We have u1=r. Then, {yra5} is a (2,r5)-AMCS which uses R2(c¯,a), and {yrb7} is a (2,r7)-AMCS which uses R2(c¯,b). Finally, {yrb7} is a (1,)-AMCS.  

Let Ma adaa+ denote the set of all valuations over 𝗏𝖺𝗋𝗌({R,,Rn}){r} that extend a valuation from Mi and satisfy xx =aa , hence Ma adaa+zxx+x x=daa+a a. Let (a 1,a1),,(a k,ak) enumerate all pairs (a ,a) from 𝐝𝐨𝐦|x |×𝐝𝐨𝐦|x| such that R(a,a+,a ,a¯,) is an R-block of 𝐝𝐛. The crux is now to show that

j=1kMa jajdaa+𝒦({R,,Rn}). (8)

By the induction hypothesis and our construction, for every j{1,,k}, Ma jajdaa+𝒦({R,,Rn}). We next consider valuations θ1,θ2 drawn from two distinct components of the disjoint union in (8) (hence k2). We show by induction on increasing h=,+1,,n that

{θ1,θ2}𝖪𝖾𝗒(Rh)𝗇𝗈𝗍𝖪𝖾𝗒(Rh). (9)

If h=, the desired result (9) holds because θ1 and θ2 disagree on some variable of 𝖪𝖾𝗒(R). For h>, the induction hypothesis is that {θ1,θ2}𝒦({R,,Rh1}). If 𝗇𝗈𝗍𝖪𝖾𝗒(Rh)=, then (9) holds vacuously. So assume 𝗇𝗈𝗍𝖪𝖾𝗒(Rh)={yh} from here on. Assume that θ1 and θ2 agree on every variable of 𝖪𝖾𝗒(Rh). It suffices to show θ1(yh)=θ2(yh).

We can extend θ1 and θ2 to (arbitrary) valuations θ1+ and θ2+ over 𝗏𝖺𝗋𝗌(q) such that {θ1+,θ2+}𝒦({R1,,R1}). In particular, let θ1+(v)=θ2+(v) for every v𝗏𝖺𝗋𝗌(u1)𝗏𝖺𝗋𝗌(u1). We remark that 𝗏𝖺𝗋𝗌(u1) is disjoint from 𝗏𝖺𝗋𝗌(x x), the latter containing variables on which θ1 and θ2 may disagree. Consider any path (v0,v1,,vm) in 𝒢𝑎𝑖𝑓𝑚𝑎𝑛(q) between yh and some variable from x x, that is, v0=yh and vm𝗏𝖺𝗋𝗌(x x). Since Rhqyh by Lemma 14, Rh(q,)yh. Since Rh↬̸(q,)vm by Lemma 30, there is a smallest index i{1,,m} such that Rh↬̸(q,)vi, hence 𝒦({R1,,Rh1})𝖪𝖾𝗒(Rh)vi, and thus θ1+(vi)=θ2+(vi). Consequently, if the Rh-fact of q(u) is Rh(s¯,yh), the repair of the block Rh(θ1(s¯),), which equals Rh(θ2(s¯),), is independent of values assigned to x x, given zxx+=daa+. Hence, we can conclude that θ1(yh)=θ2(yh), as desired, assuming ties are broken according to a fixed linear order on 𝐝𝐨𝐦. It then follows that j=1kMa jajdaa+ is an (,λ)-AMCS with λzxx+=daa+.

Computation of 𝓕𝙰𝙶𝙶(𝑴) for an (,𝝀)-AMCS 𝑴 with 𝝀𝒛𝒙𝒙+=𝒅𝒂𝒂+.

Rules (6) and (7) of Fig. 4 are executed in moving from to 1, which read as follows with the notation in the Venn diagram of Fig. 5.

K(z,x,x+,x ,x,𝙼𝙰𝚇(r)) R(x,x+,x ,x¯,y),E(z,x+,x,y,r)
E1(z,x,x+,𝙰𝙶𝙶(m)) K(z,x,x+,x ,x,m)

Assume that E1(d,a,a+,p) is computed by the program in Fig. 4. We need to show:

there is an (,λ)-AMCS M with λzx+x=da+a such that 𝙰𝙶𝙶(M)=p.

The fact E1(d,a,a+,p) must be derived by the following rules:

K(d,a,a+,x ,x,𝙼𝙰𝚇(r)) R(a,a+,x ,x¯,y),E(d,a+,x,y,r) (10)
E1(d,a,a+,𝙰𝙶𝙶(m)) K(d,a,a+,x ,x,m) (11)

Let (a 1,a1,p1),,(a g,ag,pg) enumerate all triples in 𝐝𝐨𝐦|x |×𝐝𝐨𝐦|x|×0 such that, for each j{1,,g}, K(d,a,a+,a j,aj,pj) is a computed K-fact, hence p=𝙰𝙶𝙶({{p1,,pg}}). Each such K-fact must be derived by the following partial grounding of rule (10):

K(d,a,a+,a j,aj,𝙼𝙰𝚇(r)) R(a,a+,a j,aj¯,y),E(d,a+,aj,y,r).

For every j{1,,g}, there exists bj𝐝𝐨𝐦 such that R(a,a+,a j,aj¯,bj) is an R-fact in 𝐝𝐛, and E(d,a+,aj,bj,pj) is a computed E-fact. If there is more than one such bj, the smallest is chosen according to a fixed linear order on 𝐝𝐨𝐦. By the induction hypothesis, there is an (+1,λj)-AMCS, denoted Majda+, with λjzx+xy=da+ajbj such that 𝙰𝙶𝙶(Majda+)=pj. For every j{1,,g}, let μj be a valuation over 𝗏𝖺𝗋𝗌(xx ) satisfying xx =aa j, and let Ma jajdaa+={μjθθMajda+}. As argued in the first part of the induction step, M:=j=1gMa jajdaa+ is an (,λ)-AMCS with λzxx=daa+ and 𝙰𝙶𝙶(M)=p. This concludes the proof of Lemma 26.