Conjunctive Queries with Negation and Aggregation: A Linear Time Characterization

In this paper, we study the complexity of evaluating Conjunctive Queries with negation (\cqneg). First, we present an algorithm with linear preprocessing time and constant delay enumeration for a class of CQs with negation called free-connex signed-acyclic queries. We show that no other queries admit such an algorithm subject to lower bound conjectures. Second, we extend our algorithm to Conjunctive Queries with negation and aggregation over a general semiring, which we call Functional Aggregate Queries with negation (\faqneg). Such an algorithm achieves constant delay enumeration for the same class of queries, but with a slightly increased preprocessing time which includes an inverse Ackermann function. We show that this surprising appearance of the Ackermmann function is probably unavoidable for general semirings, but can be removed when the semiring has specific structure. Finally, we show an application of our results to computing the difference of CQs.


INTRODUCTION
This paper focuses on the query evaluation problem for Conjunctive Queries with negation (CQ ¬ ), a fundamental class of relational queries.We will think of a CQ ¬ as having the following form: where the variables are of the form , ∈ [ ] = {1, 2, . . ., }, E + , E − are two sets of hyperedges that are subsets of [ ], and ⊆ [ ] are the free variables.We will call the triple H = ([ ], E + , E − ) the signed hypergraph of .We will consider only safe queries, where When E − = ∅, then is a Conjunctive Query (CQ) with the associated hypergraph ([ ], E + ).
The complexity of query evaluation for CQs is well-understood.Yannakakis [27] first showed that a Boolean CQ (i.e., = ∅) can be evaluated on a database D of size |D| in time (| | • |D|) if the hypergraph ([ ], E + ) is -acyclic (| | denotes the size of the query).Further work [3,5] generalized this result to show that if is freeconnex -acyclic then the tuples in (D) can be enumerated with constant delay (| |) after a linear-time preprocessing step.Freeconnex -acyclicity means that both ([ ], E + ) and ([ ], E + ∪ { }) are -acyclic hypergraphs.It was also shown [3] that this tractability result is tight under widely believed lower-bound conjectures.

CQs with Negation
Our first goal in this paper is to generalize the above classic result to the case where E − ≠ ∅.Prior work has looked into this problem, but without achieving a complete answer.
To explain the current progress, let us first consider the case of a Boolean CQ ¬ and attempt to solve the (seemingly harder) problem of counting the number of valuations that satisfy the body of with input a database D, which we will denote as # (D).We should note here that # is solvable in (|D|) time if is anacyclic CQ.Brault-Baron [7] had the insight that we can compute # using the inclusion-exclusion principle.Indeed, let be the Boolean CQ with hypergraph ([ ], E + ∪ ) for any ⊆ E − .Then we can write: Hence, if every is -acyclic, then # (and thus ) can be computed with data complexity (|D|).This naturally leads to the notion of signed acyclicity, introduced in [7]: a Boolean ¬ is signed-acyclic if the hypergraph ([ ], E + ∪ ) is -acyclic for every ⊆ E − .Thus, a Boolean CQ ¬ can be computed in linear time (data complexity) if it is signed-acyclic.Interestingly, if E + consists only of singleton hyperedges, then signed acyclicity is equivalent to -acyclicity of the hypergraph ([ ], E − ).However, there are two issues with applying the inclusion-exclusion approach.First, it has an exponential dependency on | | and thus does not give a polynomial-time algorithm in combined complexity.Second, it cannot be used to provide any delay guarantees for the enumeration problem in non-Boolean queries.
The second issue was partially addressed by Brault-Baron [6,7], who proposed an enumeration algorithm for any free-connex signed-acyclic CQ ¬ .However, this algorithm either achieves constant delay with a (|D| log | | |D|) preprocessing time, or achieves logarithmic delay with linear preprocessing time.The logarithmic factor is a consequence of the technique used, which translates a database instance to an instance over the Boolean domain.
Our first main result shows that the translation to the Boolean domain is not necessary and in fact we can achieve both constant delay and linear time preprocessing for free-connex signed-acyclic queries.Moreover, our algorithm has only a polynomial dependence on the size of the query.

FAQ with Negation
Our second goal is to study the evaluation of CQ ¬ in the presence of aggregation.We do this by studying a more general problem, that of computing a CQ ¬ under a general semiring, following the approach of FAQs [20].More precisely, given a commutative semiring S = ( , ⊕, ⊗, 0, 1), we define an FAQ ¬ as an expression of the form: Here, a positive factor can be viewed as a table of entries of the form x , (x ) (where the weight of tuple x is the value (x ) ∈ ), and for entries not in the table, the weight is implicitly 0. On the other hand, a negative factor is a table of entries of the form x , (x ) , and for entries not in the table, the weight is a default constant value c ≠ 0. To recover the CQ ¬ setting, we choose the Boolean semiring, and encode the values of the negative factor such that it is 0 if the tuple is in the table, otherwise 1.Our semiring formulation is though much more general: the only difference between a negative and a positive factor in our setting is whether the "default" value of a tuple not in the table is 0. This observation offers a novel angle to the semantics of negation, and is critical to our algorithms.For FAQ ¬ , we can show the following.Here, | | denotes the size of the query and ( , ) denotes the inverse Ackermann function, which grows extremely slowly as a bi-variate function of , .The appearance of this function in the runtime expression is surprising in our opinion.It occurs because the aggregation problem reduces to the well-studied problem of computing interval sums over an arbitrary semiring, called RangeSum [12,28].If the semiring structure allows for linear preprocessing and constant-time answering for its RangeSum problem, then we can drop the Ackermann factor.Examples of such semirings are the Boolean semiring ({true, false}, ∨, ∧, false, true), tropical semiring (R, min, +, +∞, 0), and semirings with additive inverse (e.g. the counting ring over integers, i.e. (Z, +, ×, 0, 1) which we use to count solutions).

Lower Bounds
Our third goal is to match our linear-time upper bounds with lower bounds.In this direction, we show that under believable conjectures, any CQ ¬ that is not free-connex signed-acyclic does not admit an algorithm that can emit the first result in linear time (hence matching the upper bound).Our conditional lower bounds are somewhat weaker than the ones obtained for CQs because the presence of negation means that we cannot use the sparse version of some problems (e.g., detecting a triangle, or Boolean matrix multiplication).For FAQ ¬ , we show stronger conditional lower bounds over the tropical semiring and counting ring based on weaker lower bound conjectures.Finally, we provide some evidence that the inverse Ackermann factor in the runtime for general semirings is unavoidable.In particular, we show that the query ( ) = ( ) ⊗ ( ) ⊗ ( , ) corresponds to a variant of the offline RangeSum problem over a general ⊕ operator.Using this observation, we can modify a construction of Chazelle [12] to show a superlinear lower bound on the number of ⊕ operations necessary to compute .

Difference of CQs
Finally, we show that our algorithm for CQ ¬ can be applied to obtain optimal algorithms with linear-time preprocessing and constant delay for the problem of computing the difference between two CQs of the form 1 − 2 , which was recently studied by Hu and Wang [18].

PRELIMINARIES
Hypergraphs.A hypergraph is a pair H = ([ ], E) where [ ] = {1, . . ., } is the set of vertices of H and E is a multiset1 of hyperedges where each ∈ E is a nonempty subset of [ ].
A signed hypergraph is a tuple H = ([ ], E + , E − ) where [ ] is the set of vertices of H , E + and E − are two multisets of hyperedges where each hyperedge ∈ E + (resp.∈ E − ) is a subset of [ ].We consider only safe signed hypergraphs, where every vertex in [ ] occurs in some hyperedge ∈ E + .
Enumeration and Complexity.In this paper, we study the enumeration problem for FAQ ¬ queries, Enum( , D), which takes as input a FAQ ¬ query and a database instance D and outputs a sequence of answers such that every answer in (D) is printed precisely once.An enumeration algorithm for Enum( , D) may consist of two phases: • (preprocessing phase) it constructs efficient data structures from and D; and • (enumeration phase) it may access the data structures built during preprocessing, and emit the answers of (D) one by one, without repetitions.We say that an enumeration algorithm enumerates with delay ( ) if the time between the emission of any two consecutive answers (and the time to emit the first answer, and the time from the last answer to the end) is bounded by ( ).In particular, we say that an enumeration algorithm is constant-delay if it enumerates with delay independent of the input database size |D|.
Model of computation.We adopt the random-access machine (RAM) as our computation model with (log )-bit words, which is standard in fine-grained complexity.The machine has read-only input registers and it contains the database and the query, read-write work memory registers, and write-only output registers.It is assumed that each register can store any tuple, and each tuple is stored in one register.The machine can perform all "standard" 3  In this paper, we consider the combined complexity of CQ ¬ and FAQ ¬ , i.e. the complexity is measured in the size of both the query and the database instance.For a FAQ ¬ query , we define | | to be the sum of the arity of all factors in .For a database D, we define |D| as sum of the size of all relations in D.

SIGNED ACYCLICITY
Before we introduce the definition of signed acyclicity, we first go over the notions of -and -acyclicity.
A hypergraph H = ([ ], E) is -acyclic if there is a tree T = ( (T ), (T )) and a bijective function : E → (T ) such that for every vertex ∈ [ ], the set of nodes { with respect to ⊆.It is known that every -acyclic hypergraph has an -leaf [8].
A hypergraph can be linearly ordered by ⊆.It is known that every -acyclic hypergraph has a -leaf [8,9].
We can now introduce the notion of signed acyclicity, slightly modified from [7] to take multisets into account.
The notion of a signed-leaf degenerates to an -leaf when E − = ∅ (since the -property holds trivially) and reduces to a -leaf when E + contains only singleton hyperedges (since the -property holds trivially and the pivot is a singleton set).Recall that every -acyclic hypergraph has an -leaf and every -acyclic hypergraph has aleaf.The next proposition should not be surprising.Next, we generalize the notion of an elimination sequence [20] to signed hypergraphs.Given H and a signed-leaf with a pivot , we define H, to be the hypergraph that ( ) removes from H any (positive or negative) hyperedge that contains such that ⊆ , and ( ) removes from any hyperedge that contains it.
Example 3.6.Consider the following CQ ¬ , which will function as our running example.
The vertex 4 is a signed-leaf for the hypergraph H of with pivot the hyperedge {3, 4} that corresponds to the atom .The query corresponding to the resulting hypergraph H, 4 is: In fact, is signed-acyclic with the signed-elimination sequence = (1, 2, 3, 4).
We should remark here that our notion of signed-leaf is equivalent to the notion of a leaf for signed hypergraphs as defined in [7] (we show this in Appendix A).However, for our purposes we need to define a slightly different elimination sequence, since the hypergraph after the removal of a vertex is defined differently.

ENUMERATION OF FULL CQ ¬
In this section, we present an algorithm that can enumerate the answers of a signed-acyclic full CQ ¬ (where = [ ]) with constant delay after linear preprocessing time.The correctness and runtime analysis of the algorithm is included in Appendix B.
Let be a full signed-acyclic CQ ¬ with a signed-acyclic signed hypergraph H = ([ ], E + , E − ) and D a database instance.Let = ′ • be a signed-elimination sequence of H .Our preprocessing phase recursively eliminates a variable in the signed-elimination sequence.When eliminating , the key idea is to construct in linear time a database D ′ , and reduce the problem of computing the answers of (D) to computing the answers of ′ (D ′ ), where the signed hypergraph of ′ is H ′ = H, and has a signed-elimination sequence ′ .The reduction needs to ensure that Π [ ]\{ } (D) = ′ (D ′ ). 4 To achieve constant delay enumeration, we construct (also in linear time) a data structure over the domain of the variable , that can, given an answer to ′ (D ′ ), extend it to an answer to (D) with constant delay.
Example 4.1.We will continue with the query in Example 3.6, the database D in Figure 1(a) and a signed-elimination sequence = (1, 2, 3, 4).The result (D) is depicted in Figure 1(d).After eliminating 4 , we obtain ′ and the instance D ′ in Figure 1 The preprocessing phase reduces computing (D) to ′ (D ′ ) and produces a data structure as shown in Figure 1(c), such that given any tuple a ′ ∈ ′ (D ′ ), we may use the data structure to enumerate all answers of the form (a ′ , 4 ) in (D), 4 being a value of 4 .

Preprocessing phase
Algorithm 1 describes the preprocessing phase that takes as input the signed hypergraph H , a database instance D, and a signed elimination sequence of H . Let be a pivot hyperedge of the signed-leaf in H and 0 the linear order of the negative hyperedges that contain .The preprocessing phase outputs a data structure L that maps tuples of the form Π \{ } a to a doubly linked list of elements from the domain of , with some extra tuple-labeled skipping links that we will introduce later.We explain how the algorithm eliminates the variable in two steps: -step and -step.
-step.The algorithm first performs an -step (that mimics Yannakakis' algorithm) in which we simply remove tuples from that will not contribute to a query answer of (D) by filtering with any positive (or negative) atom with ∈ and ⊆ .Then, Algorithm 7 builds a hash table L that maps each tuple a \{ } ∈ Π \{ } to a doubly linked list of the set { | (a \{ } , ) ∈ }, with a slight modification that every pointer is now parameterized/labeled with ∅ (see Algorithm 7 in the appendix).The extra label on the pointer allows us to add pointers with different labels in later steps.If contains no negative atoms, this data structure is sufficient to achieve constant delay enumeration by traversing the list from the head to the end.Finally, is replaced with its projection Π \{ } .
Example 4.2.For our running example ( 3 , 4 ) is chosen as the pivot, and the tuples highlighted in gray in Figure 1(a) denotes the tuples in that survive the semijoin step with the negative atom ¬ ( 4 ).The linked lists of the hash table correspond to the data structure in Figure 1(c) if we ignore the dotted edges. 4For a tuple a and ⊆ , we denote Π a as the projection of a on .

Algorithm 1: PreprocessFullCQ(H, D, )
Input: signed hypergraph H , instance D, signed-elimination sequence = ′ • Construct : L for every vertex in 1 if is empty then 2 terminate 3 ← a pivot hyperedge for the signed-leaf in H When negative atoms are present, for a to be an answer to (D), we also need that Π a ∉ for every ∈ [ ].Therefore, we need to augment L such that we can skip the enumeration of any value in the linked list that does not contribute to the answer.The linear order on the schemas of negative atoms allows us to construct this data structure in a dynamic fashion.If only one negative atom ¬ 1 is present (i.e., = 1), for each tuple a 1 ∈ 1 we can add a skipping link labeled with Π 1 \{ } a 1 that bypasses the node with value Π { } a 1 in the doubly linked list L [Π \{ } a 1 ].If = 2, then there is another negative atom ¬ 2 with 1 ⊆ 2 .For each tuple a 2 ∈ 2 , we only need to add a skipping link labeled with Π 2 \{ } a 2 that bypasses the node with value Π { } a 2 in the doubly linked list L [Π \{ } a 2 ] if that node has not been bypassed by a skipping link with label Π 1 \{ } a 2 yet (i.e., Π 1 a 2 ∉ 1 ).In general, for every tuple a ∈ such that Π { } a has not been bypassed by any skipping link labeled with Π \{ } a (i.e., for every 1 ≤ < ≤ , a ∉ ), we add a skipping link labeled with Π \{ } a that The list data structure L4. (D) The query result.Finally, for each negative atom ¬ , we need to keep precisely the subset of Π \{ } so that we can avoid emitting an answer to ′ (D ′ ) that cannot be extended to an answer to (D).This is done by keeping the tuple a \{ } ∈ Π \{ } only if Π \{ } a \{ } ∈ L , but the tuple cannot be extended to any answer to (D), i.e., the traversal on L using a \{ } does not lead to any value of .

Enumeration phase
The enumeration phase is shown in Algorithm 4. It follows the reverse order of = ′ • to first recursively enumerate a tuple a ′ ′ ∈ ′ (D ′ ), and then (with a slight abuse of notation), use every ∈ L .Iterate(a ′ ′ ) to obtain an answer (a ′ ′ , ) to (D).The preprocessing phase guarantees that the iterator is nonempty for every answer a ′ ′ .

ENUMERATION OF FAQ ¬
In this section, we give a high-level description of the enumeration algorithm for FAQ ¬ queries as Eq 3 that constructively proves The sequence ends up with a full signed-acyclic NestFAQ ¬ query with free variables and D such that (D) = (D ).Next, for a full signed-acyclic NestFAQ ¬ query, we show in Appendix C.1 a reduction from the enumeration problem Enum( , D ) into Enum( * , D * ), where * is a full signed-acyclic CQ ¬ and D * is a database instance of * .At this point, we simply apply the preprocessing algorithm for a full CQ ¬ .In the enumeration phase, as we emit each answer a of * (D ), we plug the emitted tuple a into to recover its weight (a ) = (a ).We now take a closer look at a signed-elimination step via a concrete running example.The formal (and rather technical) description is deferred to Appendix C.2 up to Appendix C.6.
Example 5.1.Consider the following signed-acyclic FAQ ¬ query ( 1 , 2 ), where 3 is a signed-leaf of its associated hypergraph with a pivot hyperedge {3} (corresponding to the positive factor 3 ): The signed-elimination step for the signed-leaf 3 is much more involved than the elimination step for a signed-acyclic CQ ¬ query.A naive attempt may aim for a reduction to the follwing ).However, it is not attainable by the semantics of FAQ ¬ queries over a general semiring.Indeed, we examine the weight ′ 2 ( 2 ), for some 2 = 2 such that Π 3 23 ( 2 , 3 ) ≠ ∅: This insufficiency calls for lifting FAQ ¬ queries to a more expressive class of queries NestFAQ ¬ , where we exhibit as an abstract syntax tree (AST) depicted in Figure 2 (left), in which we decompose the negative factors as where ½ ¬ is an indicator factor that maps to 0 if x ∈ and 1 otherwise.As a shorthand, we write ⊢ := ⊕ ½ ¬ ⊗ .Now we illustrate the signed-elimination step on the signed-leaf 3 (corresponding to the variable 3 ) in Example 5.1.A signed-elimination step can be further decomposed into three steps: ( ) refactor step, ( ) oracle-construction step, and ( ) aggregation step.

The Refactor Step
We first illustrate the refactor step: it reorganizes the AST of to make 3 present only in one subtree rooted at a child of the root ⊗ node.First ) .We keep recursing on the subtree for ( As we go, 3 sinks down and the linear inclusion [3] ⊇ {2, 3} ⊇ {3} surfaces along the subtrees that contain 3 .In the end, 3 absorbs the constants c 123 and c 23 .The refactor step runs in linear time with no query (or database) size blow-up, as proven in Appendix C.3.

The Oracle-construction Step
Next, we illustrate the oracle-construction step.Chazelle and Rosenberg [11] showed that a semigroup RangeSum data structure (oracle) on an array of size can be constructed in time ( ) to support a semigroup sum over any range in ( (14 , )) time, where is the inverse Ackermann function.The oracle-construction step uses it as a black-box and builds a RangeSum oracle bottom-up for each subtree containing 3 , i.e. the subtree rooted at ⊗ nodes in Figure 2 (right).We demonstrate via a simple database instance.
Example 5.2.We assume the counting ring and on the refactored tree in Figure 2 where the AST of ′′ is exactly the refactored AST in Figure 2 (right), except that 3 is being eliminated.Now the signed-elimination step is complete.We defer the formal analysis of the aggregation step to Appendix C.4 and Appendix C.6.

The Reduction to Enumeration of Full CQ ¬
Finally, we cast the enumeration of the (full) NestFAQ ¬ query ′′ into the enumeration of the full signed-acyclic CQ ¬ query * , by directly extracting factors from ′′ as relations: ), respectively.The leftmost column shows the requested range sums over [1,15] in the aggregation step.
Output does it contain a hyperclique of size + 1, i.e. a set of + 1 vertices where every subset of size forms a hyperedge There is no algorithm that solves ( + 1, )-Hyperclique in ( ) time, where is the number of edges in the input hypergraph.
Readers are referred to [5] for evidence why Conjecture 6.1 is believable.When = 2, the (3,2)-Hyperclique problem is the problem of finding a triangle in a graph.

C 6.2 (T ).
There is no algorithm that decides whether a graph with nodes contains a triangle in ( 2 ) time.
The following is a combination of results from [5,7].T 6.3.Let be a CQ ¬ that is not signed-acyclic.Assuming Conjecture 6.1 and Conjecture 6.2, then there is no algorithm for that has linear preprocessing time and (1) delay.
To show a lower bound for queries that are not free-connex (but are signed-acyclic), we will use a weaker lower bound conjecture that implies T .

C 6.4 (BMM).
There is no algorithm that computes the product × of two × Boolean matrices and in ( 2 ) time.
Evidence for Conjecture 6.4 can be found in [22].Bagan et al. [3] reduce the BMM problem to the non-free-connex acyclic query ( , ) ← ( , ) ∧ ( , ) and apply Conjecture 6.4 to obtain a conditional lower bound.The matrix multiplication exponent is the smallest number such that for any > 0, there is an algorithm that multiplies two -by-matrices with at most ( + ) operations (assuming RAM model).The best bound known so-far on is (roughly) < 2.373 in [15,26].We note that Conjecture 6.4 does not violate the common belief that = 2, since that only implies that BMM can be computed in time 2+ (1) .For non-free-connex CQs, a weaker lower bound conjecture was used, sparse BMM (the matrices have non-zero entries and no ( ) algorithm exists).However, this conjecture cannot be applied in our case because we need to take the complement of the matrix to populate a negated atom, and that means that a sparse matrix becomes dense.T 6.5.Let be a CQ ¬ that is signed-acyclic and not freeconnex.Assuming Conjecture 6.4, there is no algorithm with linear preprocessing time and (1) delay.

Lower Bounds for FAQ ¬
We present next lower bounds for FAQ ¬ when restricted to queries with head (), which we denote as SumProd ¬ .To show these bounds, we will use weaker conjectures than the ones used in the previous section.

C 6.6 (M W C
).There is no algorithm that computes the minimum weight of a -clique in a edgeweighted graph with nodes in ( ) time.
Our reduction from Minimum-Weight -Clique [1] is an application of the clique embedding power technique introduced in [13].T 6.7.Assuming Conjecture 6.6, a SumProd ¬ over the tropical semiring can be solved in linear time iff is signed-acyclic.
Over the counting ring, we can instead show that any lower bound for counting Boolean CQs transfers immediately to SumProd ¬ .T 6.8.Suppose that no linear-time algorithm can count the solutions of a non -acyclic Boolean CQ.Then, there is no lineartime algorithm for a SumProd ¬ query over the counting ring that is not signed-acyclic.

An Unconditional Lower Bound for FAQ ¬
Finally, we show an unconditional lower bound that provides some evidence on the necessity of the inverse Ackermann factor in the runtime of Theorem 1.2.
The lower bound is based on the additive structure of the underlying semiring for FAQ ¬ , i.e. a commutative semigroup with operator ⊕.It uses the arithmetic model of computation [12,29], which charges one unit of computation for every ⊕ operation performed, while all other computation is free.Essentially, the computation can be viewed as a sequence of instructions of the form = ⊕ , where { } form an unbounded set of variables.Moreover, this sequence should be agnostic to the actual values of the semiring.The only thing we need is that the semigroup is faithful [12,29], meaning that for every 1 , 2 ⊆ {1, 2, . . ., }, and integers , ′ > 0, • cannot be an identity for all 1 , 2 , . . ., ∈ unless 1 = 2 .This is essentially saying that there is no "magical shortcut" to compute the sums.T 6.9.Under the arithmetic model of computation, any constant delay enumeration algorithm (in data complexity) for on factors , each of size and factor of size 2 must require Ω( • ( , )) preprocessing time for every ≤ .

DIFFERENCE OF CQS
As an application of our results, we consider the class of queries of the form 1 − 2 , where 1 , 2 are full CQs with the same set of variables.It is shown in a recent paper [18] is -acyclic for every in 2 .We use our main theorem to strengthen this result by providing a constant-delay enumeration guarantee (Appendix E).
where 1 , 2 are full CQs over the same set of variables.If 1 is -acyclic and 1 ∧ is -acyclic for every in 2 , then the result can be enumerated with constant delay after (|D|) preprocessing time.
As a corollary, we obtain the following generalization to differences of non-full CQs (proven in Appendix E).
where 1 , 2 are CQs with the same set of free variables.If is difference-linear (Def 2.3 in [18]), then the output of can be enumerated with constant delay after (|D|) preprocessing time.

CONCLUSION
This paper has made an initial foray into a novel way of interpreting CQ ¬ from the perspective of semiring and FAQs [20].We presented a constant-delay enumeration algorithm for the class of free-connex signed-acyclic FAQ ¬ queries, after linear preprocessing (modulo an inverse Ackermann factor), and showed lower bounds for FAQ ¬ queries out of this class.We leave as an intriguing open question the parameterized complexity of general CQ ¬ and FAQ ¬ queries (a brief discussion is in Appendix F).

A MISSING PROOFS IN SECTION 3
Let H = (V, E + , E − ) be a signed hypergraph.We define [H ] as the hypergraph obtained by iteratively removing a hyperedge ∈ E + ∪E − if there exists some hyperedge ∈ E + such that ⊆ .We say a signed hypergraph as the hypergraph obtained by removing from every hyperedge in H .

A.1 Equivalence with [7]
A vertex is said to be a bicolor-leaf in H in [7] if is a -leaf in [H ].We show that this notion of bicolor-leaf in [7] is equivalent to our definition of signed-leaf in Definition 3.2.
We show that is a pivot edge in H .

A.2 Proof of Proposition 3.3
Our proof for Proposition 3.3 is inspired from the proof of Theorem 7 in [8].Some additional definitions are required.Two vertices and are said to be non-neighbors in a signed hypergraph H = (V, E + , E − ) if there is no hyperedge ∈ E + ∪ E − such that ⊂ V and , ∈ .
We need some additional structural properties on signed-acyclicity.
L A.2. Let H = (V, E) be an -acyclic hypergraph.Then we have (1) the hypergraph H [\ ] is -acyclic for every vertex ∈ V; and (2) the hypergraph H ′ = (V, E \ { }) is -acyclic for every two distinct hyperedges and in H such that ⊆ .

P .
Let T be the join tree of H witnessed by the bijection mapping : E → (T ).Consider two items.(1) We have that (2) Let and be two distinct hyperedges in H such that ⊆ .Let T 1 , T 2 , . . ., T be the connected components of T − ( ), the tree obtained by removing the node ( ) and all edges incident to ( ) from T .Assume that ( ) is adjacent to ( ) in T for each ∈ [ ] and without loss of generality that ( ) is contained in T .Consider the tree T ′ obtained by removing ( ) from T and adding an edge between every ( ) to ( ) We argue that T ′ is a join tree of H ′ .Consider any vertex in H ′ and let ≠ ∅}, the indices of all subtrees T that contains some hyperedge containing .If = { } for some index ∈ [ ], then is properly contained in T , and since T is a join tree, T induces a connected component in T and thus T ′ .Otherwise, | | ≥ 2. In this case, we must have ∈ for every ∈ and thus ∈ ⊆ .Therefore, \ (T ) induces a connected component in T ′ .By construction, every ( ) is connected to ( ) in T ′ and ( ) ∈ .Note that ∩ (T ) also induces a connected component in T and ( ) ∈ ∩ (T ), induces a connected component in T ′ .L A. 3. Let H = (V, E + , E − ) be a reduced signed-acyclic signed hypergraph with at least two vertices.Then H contains two nonneighbor -leaves (and therefore signed-leaves).
Assume first that there is some hyperedge  (2) let ∈ E + ∪ E − and ∈ E + be two distinct hyperedges with ⊆ , and we have

P
. We again consider two items.( 1) Let E = E + ∪ E − and we have that   We argue that in the second step, such a must exist.For = , since ⊂ [ ] and H is signed-acyclic, by Lemma A.4, H contains two non-neighbor signed-leaves, and there must be a signed-leaf of H such that ∉ .Hence, ⊂ [ ] \ { } and remain in H , by definition.This argument can thus continue inductively.

B MISSING PROOFS IN SECTION 4
The formal definitions for the operations currNode.nextM(a) and currNode.prevM(a) are defined in Algorithm 5 and 6, respectively.
Step (1).We remove the signed leaf 4 from H 4 , which yields the following query 3 ( 1 , 2 , 3 ) whose hypergraph corresponds to the hypergraph H 3 = H 4 , 4 and emits a skipping list data structure L 4 as shown in Figure 4(e): The database is changed to D 3 as in Figure 4(b).
Step (2).We remove the signed leaf 3 from H 3 , yielding 2 ( 1 , 2 ) whose hypergraph corresponds to the hypergraph H 2 = H 3 , 3 and emits L 3 as shown in Figure 4(f): Note that in this step, the positive atom ( 3 ) and all negated atoms ¬ ( 2 , 3 ) and ¬ ( 1 , 2 , 3 ) are removed from 3 since their corresponding negative hyperedges are contained by the positive hyperedge corresponding to ( 1 , 2 , 3 ).Further, the skipping list L 3 does not contain any skipping edge.The database is changed to D 2 as in Figure 4(c).
Note that L 2 also does not contain any skipping edge.The database is changed to D 1 as in Figure 4(d).
Step (4).We remove 1 from H 1 , and this step essentially creates a linked-list L 1 (shown in Figure 4(h)) on the remaining elements in the relation in Figure 4(d).
Enumeration step.For the enumeration step, we first enumerate every element in L 1 (yielding a 1 , a 2 and a 3 ).Then we use that element enumerated in L 1 as a probing tuple in the enumeration process of every element in L 2 .For example, L 2 .nextM(a 1 ) leads to b 1 , and L 2 .nextM(a 2 ) leads to b 2 .We continue this step using the combined tuple enumerated from L 1 and L 2 (say, (a 1 , b 1 )), to enumerate the elements in L 3 .For example, L (1) if a ∈ (D), then a ′ ∈ ′ (D ′ ); and (2) if a ′ ∈ ′ (D ′ ), then L .Iterate(a ′ ) is nonempty and for every emitted by L .Iterate(a ′ ), we have (a ′ , ) ∈ (D).

P
. Consider two items.
Algorithm 7: BuildList( , ) Hence a satisfies all positive atoms of , and therefore a ′ must satisfy all positive atoms of ′ by the -step.
The proof is now complete.
We first prove the theorem for full queries.Given a signed hypergraph H = ([ ], E + , E − ), its signed-elimination sequence can be found in time (| | 3 ) using brute force: we may find a signed leaf in H in time (| | 2 ) by first iterating over every vertex and then checking whether it is a signed leaf by definition.Then we remove this signed leaf from H , and iteratively apply the previous process until the graph is empty within (| |) iterations.We remark that this step can potentially be improved.
Let H be the hypergraph of before the preprocessing step.For every vertex in H , we denote ( ) as the number of positive and negative hyperedges in H that contains .A key observation is that: for any data structure L , the procedures prevM, nextM runs in time ( ( )), since there are at most ( ) projections to check.For implementation, we also need to add create a book keeping hash table from each tuple a to the exact node in the hash table L that contains the value Π { } a for later use.Therefore, the -step runs in ( ( ) • | |) time, since for each tuple in , we need to probe at most ( ) relations to process it.For the -step, the data structure L can be initialized in (| |) time for line 11.Note that line 4 of ExtendList(L , ) only takes constant time using the book keeping hash table created in the -step.For line 15 To enumerate (D), the crux is to apply Algorithm 1 on inputs (H ′ , D, ) in the preprocessing phase, as if were a full query.Subsequently in the enumeration step, we only uses the list data structures L 1 , L 2 , . . ., L , ignoring every L where appears in the sequence ′ .This is correct, because every answer to (D) must also participate in the first positions of some full query answers, which can be recovered exactly by traversing only the first list data structures.
C MISSING DETAILS FOR SECTION 5: ENUMERATION ALGORITHMS FOR FAQ ¬ AND NestFAQ ¬ Context-free Grammar (CFG) If , , are strings of terminals and non-terminals, and ℓ ::= is a rule of the CFG, we say that ℓ yields in the CFG (written as ℓ ⇒ ).We say that derives (written as * ⇒ ) in the CFG if = or if there is a sequence 1 , . . ., for ≥ 0 such that A derivation of a string for a grammar is a sequence of grammar rule applications that transform the start symbol into the string.A derivation proves that the string belongs to the grammar's language.
For any ∈ [ ], let denote a variable, and denote a value in the discrete domain Dom( ) of the variable .Overloading notation, we also refer to [ ] as the set of variables.For any subset ⊆ [ ], we define x = ( ) ∈ and a = ( ) ∈ ∈ ∈ Dom(x ), where Dom(x ) = ∈ Dom( ).In other words, x is a tuple of variables, and a is a tuple of specific values with support .Let H = ([ ], E + , E − ) be a signed hypergraph.To each ∈ E + (and ∈ E − ), we associate a distinct function : Dom(x ) → , called a factor.We assume that all factors are represented via the listing representation: each factor is a table of all tuples of the form a , (a ) , where (a ) ∈ is the weight of the tuple a .For entries not in the table, the factor implicitly encodes their weights as 0. Under set-theoretic context, we also use to denote the set of tuples of schema x explicitly stored in the factor table and ¬ = Dom(x ) \ .Our definition of listing representation is slightly more general than [19,20] in that we allow (x ) = 0 for some x in the table (i.e.x ∈ ).A factor where ∈ E + (resp.
where ( ) is the number of hyperedges in H that contain ; • (our assumptions) in our work, we assume that each factor , , where ∈ E + , ∈ E − , shows up precisely once in (x [ ] ), i.e., a one-to-one mapping from the hyperedges of H to factors in (x [ ] ).Moreover, we assume that a NestFAQ ¬ expression (x [ ] ) is safe, that is, every ∈ [ ] appears in some ∈ E + (i.e.

∈ E +
= [ ]).Indeed, the "unsafest" case occurs when every branch takes the right child of ⊢ operator (thus more propensity to have un-guarded variables), because if takes the left child , then the right sub-tree is immediately guarded by ; otherwise, the ¬ can possibly be un-guarded.
A NestFAQ ¬ expression (x [ ] ) can be depicted pictorially as a abstract syntax tree (AST) drawn below, where • and ⊢ are the intermediate nodes, and they are show up on alternating levels, starting from the root being a node.The node takes at least one child, but the ⊢ node always takes two children.
• , (x ) and (x ), where ∈ \ {0}, ∈ E + and ∈ E − , are the leaves of the AST.The NestFAQ ¬ Queries.A NestFAQ ¬ query (with free variables ⊆ [ ]) associated with a signed hypergraph H = ([ ], E + , E − ) is defined as: where A fragement of NestFAQ ¬ queries is the FAQ ¬ queries, where the corresponding AST is flat: the right child of the ⊢ nodes are c ≠ 0, that is, the default values of .Indeed, the query can be compactly written as that coincides with the definition of FAQ ¬ queries in (3) by letting (x ) = ( (x ) ⊢ c ).
Definition C.1 (Free-connex signed-acyclicity).A NestFAQ ¬ query ( 6) is free-connex signed-acyclic if the signed hypergraph In the following sections, we will show the following main theorem for free-connex signed-acyclic NestFAQ ¬ queries, which subsumes Theorem 1.2 as a special case.Before jumping to the general case of free-connex signed-acyclicity, we dedicate the next section to a special case, where is a full signed-acyclic NestFAQ ¬ query, i.e. = [ ].

C.1 Enumeration of full NestFAQ ¬
In this section, we study the enumeration problem Enum( , D) for signed-acyclic full NestFAQ ¬ queries.First, we recall the definition ( 6) and ( 5) that: a NestFAQ ¬ query associated with a signed hypergraph ) is a safe NestFAQ ¬ expression associated with H .The enumeration algorithm can be summarized as follows.
(1) (pre-processing phase) we first reduce the Enum( , D) problem into a Enum( * , D * ) problem, where * is a signed-acyclic full CQ ¬ query that evaluates on a new database instance D * ; then, we follow the pre-processing algorithm for signed-acyclic full CQ ¬ queries presented in Section 4. (2) (enumeration phase) we enumerate the output tuples of * via the enumeration algorithm for signed-acyclic full CQ ¬ queries presented in Section 4; then we plug the tuples into to get its corresponding weight.Now we present the construction of * and D * .First, the desired * is the following full CQ ¬ query associated with H : where for every ∈ E + , we add a positive atom * (x ), and for every ∈ E − , we add a negative atom * (x ) (and place a ¬ symbol in front) into the body of * .It is easy to see that ( 7) is a full signed-acyclic full CQ ¬ query associated with H . Next, we construct the corresponding database instance D * for every atom in * defined as follows:
We then prove the "only if" direction.Suppose (a) ≠ 0. We prove that a ∈ * (D * ), or in other words, Π a ∈ * , for every ∈ E + , and Π a ∉ * , for every ∈ E − .We prove by induction on the NestFAQ ¬ subexpression (x ) following the rules of the CFG (5) in a top-down fashion, i.e. tracing the derivation from the start symbol ℓ (x [ ] ) of the CFG (5).The base case is simply (a) = (a) ≠ 0. We now argue the inductive step and suppose ℓ (x ) ( ≠ ∅) is an intermediate non-terminal along the derivation of (x [ ] ).By inductive hypothesis, we have that (Π a) ≠ 0. We then distinguish the following two cases: From the next section onwards, we study the enumeration problem of free-connex signed-acyclic NestFAQ ¬ queries.
On a high level, our preprocessing algorithm takes as input ( ) the NestFAQ ¬ expression (x [ ] ), and ( ) its database instance D, and for each = − 1, − 2, . . ., (following the signed-elimination order), it executes a signed-elimination step on , that constructs an intermediate NestFAQ ¬ expression (x [ ] ) and its database instance D , where

• (x [ ]
) is associated with the signed hypergraph H = H +1 , + 1 , i.e. the signed hypergraph after eliminating , − 1, . . ., ).In other words, for any ) is evaluated on D +1 and (x [ ] ) is evaluated on D .The last NestFAQ ¬ expression (x [ ] ), after the sequence of signed-elimination steps on − 1, − 2, . . ., , becomes a full NestFAQ ¬ query whose associated signed hypergraph H accepts a signed-elimination sequence (1, 2, . . ., ) and Thus, we degenerate to the case where is a full signed-acyclic NestFAQ ¬ query and our algorithm simply follows our discussion in the last section (Appendix C.1): reduce Enum( (x [ ] ), D ) into Enum( * (x [ ] ), D * ), where * is a full signed-acyclic CQ ¬ and apply the preprocessing algorithm for full CQ ¬ .At the enumeration phase, we emit each answer a of * (D ) (applying the enumeration algorithm for full CQ ¬ ) and plug the emitted tuple a into to get recover its weight (a ) = (a ).
signed-elimination step.W.L.O.G, we let be the given signed-leaf of H and appoint to be a pivot hyperedge, breaking ties arbitrarily.We call the factor (x ) corresponding to the pivot factor.A signed-elimination step runs the following 3 algorithms consecutively: (1) the refactoring algorithm (Appendix C.3) The refactoring algorithm takes the given NestFAQ ¬ expression (x [ ] ) and database instance D as input and returns ( (x [ ] ), D ) as output where is a new NestFAQ ¬ expression recognized by the following (more restrictive) CFG associated with the signed hypergraph • D is the corresponding database instance of (x [ ] ) such that (x [ ] ) = (x [ ] ).We observe that CFG is more restrictive than CFG (5) in that it has strictly less productions for the non-terminals ℓ (x ) where ∈ .The refactoring algorithm uses the pivot factor to "absorb" the factors and where ∈ ⊆ , ∈ ⊆ .By the properties of a signed-leaf , the only hyperedges containing left in H are and 1 , . . ., ∈ E − for some such that , where The grammar CFG −1 is defined as follows: As a recap, a signed-elimination step on aggregates out from the NestFAQ ¬ expression (x [ ] ) and outputs a NestFAQ ¬ expression −1 (x [ −1] ) associated with H −1 = H, and its database instance . Now, we can keep applying the signed-elimination step on the next signed-leaf − 1 and so on, untill all variables in [ ] \ have been removed.

C.3 The refactoring algorithm
In this section, we present the refactoring algorithm that refactors (x [ ] ) into (x [ ] ), a NestFAQ ¬ expression associated with H .

ℓ (x [ ] ) * ⇒ (x [ ] ).
Along the way, we will also update the database instance D accordingly, leading to D at the end.In particular, we let where Refactor ℓ (x ) * ⇒ (x ), ℓ (x ) * ⇒ (x ) , for some , ⊆ [ ], is an invokation of a recursive algorithm Refactor that takes the following inputs: (1) a stashed derivation of a NestFAQ ¬ subexpression (x ), i.e. ℓ (x ) * ⇒ (x ), such that its first yield step will be maintained to be one of the followings: • if ∉ , then it follows that = ∅ and (x ) ⇒ 1; or • otherwise ∈ , then it applies the production of CFG that uses the largest hyperedge among all productions that can be used as the next yield step, if there is any (an arbitrary choice otherwise) (2) a derivation of a NestFAQ ¬ subexpression (x ), i.e. ℓ (x ) * ⇒ (x ), for some ⊆ [ ]; (3) a database instance D of (x [ ] ) as a global input, and returns a NestFAQ ¬ subexpression (x ∪ ) recognized by CFG and a database instance D corresponding to (x [ ] ), such that The refactoring algorithm Refactor is illustrated in Algorithm 8 and formally, we prove the following lemma.

P
. We prove the lemma by induction on | | ≥ 2 for every if-else branch in Refactor.We first examine the case where the next yield step of ℓ (x ) is ℓ (x ∅ ) ⇒ (x ∅ ) = 1, so = ∅.We divide into 2 main cases (case ( 1) and ( 2)), one for each next yield step of ℓ (x ): (1) (line 2) the base case is when = ∅ and ℓ (x ) ⇒ (x ∅ ) = 1, in which case ( = = ∅, | | = | | = 1) we simply return 1 and the lemma is trivially true.Indeed, it uses the production ℓ (x ∅ ) ::= 1 in CFG .(2) otherwise, ( ∈ ), recall = ∅ now, so there must be one largest hyperedge in (x ), by the -property of the signed-leaf and we have the following cases: (2.1) (line 8) let the next yield step of ℓ (x ) be where ∈ E − , − ⊆ and − ⊆ such that = ∪ − .As is the largest hyperedge used among the next yield steps of ℓ (x ), we have − ⊆ .Then we have Output: an expression (x ∪ ) recognized by CFG such that ℓ (x ∪ ) * ⇒ (x ∪ ) = (x ) ⊗ (x ).where ) is an updated negative factor that can be computed in (| |) time.Recall that we return in this case Now, we observe that the first yield step in the stashed derivation ℓ Thus, we get and ℓ (x ∪ ) indeed derives this subexpression in CFG because (now = ∅ and = , so ∪ = ) where the first yield step here uses the production ℓ (x ) ::= (x ) ⊢ ℓ (x − ), where = and (2.2) (line 6) if there is no ⊇ (only ⊂ lower), then in the children of the root, there must be , because can no longer hide lower in any subtrees of the AST, thus let the next yield step of ℓ (x ) be where − ⊆ .As is the largest hyperedge used among the next yield steps of ℓ (x ), we have − ⊆ .Then we have where ) is an updated positive factor that can be computed in (| |) time.Recall that we simply return in this case (x ) and the lemma is trivially true.Indeed, the next production of CFG to be used is ℓ (x ) ::= (x ).
Next, we examine the case where the next yield step of ℓ (x ) is not ℓ (x ) ⇒ 1.Now, Refactor prioritizes processing the next yield steps of ℓ (x ) over those of ℓ (x ).We distinguish the following two cases: one for not being contained in the hyperedge used in the next yield step, and the other for the opposite.
(3) First, we examine the case where is not contained in the hyperedge in the next yield step of ℓ (x ).There are 3 subcases, one for each next yield step of ℓ (x ): ) is trimmed off in the recursive call).In the CFG, we have (recall that we return where the first yield step here uses the production ℓ (x ) ::= where (i) the first yield step here uses the production ℓ (x ∪ ) :: ) in CFG as ∉ − and (iii) the last derivation follows from the induction hypothesis. where Recall that we return in this case where the first yield step here uses the production ℓ (x ∅ ) ::= ∈ \ {0} in CFG .(4) otherwise, is contained in the hyperedge in the next yield step of ℓ (x ).In this case, we apply a 'stash' step that temporarily 'stashes' this yield step into ℓ (x ) and then keep processing the next yield step of ℓ (x ).
We insert a new yield step for into the stashed derivation ℓ (x ) * ⇒ (x ) and keep track of the largest hyperedge to be used as the next yield step of ℓ (x ) in the augmented derivation.W.L.O.G, the agumented derivation becomes and then we return Refactor ℓ and then we return Refactor ℓ Case ( 4) is the only if-else branch where | | stays the same in the recursive call, because this step essentially 'stashes' the next yield step of ℓ (x ) into the derivation for ℓ (x ).Therefore, the length of the derivation for (x ) decrements by 1 but that of (x ) increments by 1 in the recursive call.However, in the worst-case, this if-else branch can be visited at most (| |) times, each visit incurs an (| |) time overhead to place the yield step of into the stashed derivation ℓ (x ∪ ) * ⇒ (x ∪ ) so that the largest hyperedge can be directly accessed upon request.Eventually, Refactor will recurse back to one of the previous cases and | | will decrement by 1 in that subsequent recursive call.As a result, the correctness of the lemma in this case simply follows through from the correctness of Refactor ℓ (x ∪ ) * ⇒ (x ∪ ), ℓ (x − ) * ⇒ (x − ) .
Lastly, we justify that Refactor runs in time (| | 2 + ( ) • |D|).The Refactor algorithm recursively visits each yield step in both derivations ℓ (x ) * ⇒ (x ) and ℓ (x ) * ⇒ (x ) at most twice (accounting for "stashes").For each visit, if it does not fall into the last stash case (4), | | decrements by 1, and Refactor takes (|D|) time to update the global database instance D. Furthermore, every such update (on the database instance of D) leverages the or properties of the signed-leaf to guarantee that |D | ≤ |D| and the update can only happen once for each hyperedge containing (since that corresponding factor does not appear in the subsequent recursive call).Thus, all updates leading to D take ( ( ) • |D|) time in total.
For the last case (4), as discussed in paragraph of case (4), Refactor can fall into it for at most (| |) times, each time with a (| |) time overhead before the next recursive call (for the 'stashes').Therefore, the total time complexity of Refactor is The following theorem is a direct consequence of Lemma C.5.

T C.6.
There is an algorithm that takes as input an NestFAQ ¬ expression (x [ ] ) associated with H , thus recognized by CFG (5), and a database instance D, and then returns a NestFAQ ¬ expression (

P
. We simply call and trace the recursive call steps of Refactor to construct the derivation ℓ (x [ ] ) * ⇒ (x [ ] ) using production rules of CFG (9).As Refactor (Algorithm 8) only recurses on ℓ (x ) where ∈ , and if not, it just follows from the original derivations in CFG (those derivations can be attached to the end of the derivation for every NestFAQ ¬ subexpression ℓ (x ) * ⇒ (x ) where ∉ ).We therefore close the proof by invoking Lemma C.5.

C.4 The aggregation algorithm
We assume in this section that we have obtained ( (x [ ] ), D ) from the call of Refactor as in Theorem C.6.We now shift our focus directly to the aggregation algorithm that eliminates , where is the signed-leaf of the signed hypergraph H .We defer the oracle-construction algorithm to Appendix C.5 for now because the aggregation algorithm will provide an intuitive motivation that necessitates the oracles to be constructed.
The aggregation algorithm takes as input ( The main upside of turning the NestFAQ ¬ expression ( ) where every * ⇒ (a derive step) in ( 16) applies the productions ( 11), ( 13) and (15) to factor out all the terms ( , (x ) or (x ) ⊢ ℓ (x − )) that does not contain and wrap them in some (x ) such that ℓ (x ) * ⇒ (x ), where ∈ [ ] and ∉ .Then it is followed by ⇒ (a yield step) that applies the production ( 14) that produces the negative factors ), except for the last yield step where we use the production (12) to produce the pivot factor (x ) from ℓ (x ).On a high level, our aggregation algorithm will follow the derivation ( 16) of (x [ ] ) line-by-line and "push-in" the aggregation operator ∈Dom( ) into the more and more nested subexpressions.For succinctness, we use as an abbreviation of ∈Dom( ) in the following.This "push-in" step for every derive step is immediate: we can simply push the aggregation operator by factoring out the term (x ) that does not contain .That is, for = 0, 1, . . ., , Here, in the last yield step (applying the production (12)), (x ) is straightforward: we can scan the list representation of the pivot factor once and get a new factor \{ } (x \{ } ) in time (| |).Now we turn to the tricky yield steps in the derivation ( 16) that applies the production (14) of the CFG .What we want here is to "pushin" the aggregation operator after the ⊢ operator so that the aggregation algorithm can proceed with the subexpression (x − ).To that end, first we recall the semantics of the ⊢ operator: We distinguish the following two disjoint cases: (1) if a \{ } ∉ Π \{ } , then the aggreation simply becomes (x − ), because ( , a \{ } ) = 0 and ½ ¬ ( , a \{ } ) = 1, for all ∈ Dom( ).Thus, for such tuples a \{ } , the push-in step is trivial.(2) otherwise, we have a \{ } ∈ Π \{ } (x ).In this case, we define a new factor \{ } as follows: It is easy to see that the list representation of \{ } is of size (| |).
Therefore, if we can obtain (the list representation of) this new factor \{ } efficiently, the "push-in" of the aggregation operator becomes immediate because Back to our big picture, the aggregation step "pushes-in" the aggregation operator one line at a time following the derivation ( 16): . . .
As one can see from the comparison with ( 16), the derivation of −1 (x [ −1] ) exactly follows the derivation of (x [ ] ) except that has been peeled off from the set of variables and every NestFAQ ¬ subexpression show up in (x [ ] ).So, −1 (x [ −1] ) is a NestFAQ ¬ expression recognized by the following intermediate context-free grammar, obtained by directly removing from every non-terminal of the grammar CFG : for all ⊆ [ − 1], ℓ −1 (x ∅ ) ::= where = ∅ and ∈ \ {0} In spite of its discrepencies to CFG −1 (10), every production of the above grammar can be derived from CFG −1 .Indeed, recall that CFG −1 is associated with the signed hypergraph ) is also recognized by CFG −1 at the end of the elimination step for the signed-leaf and the signed-elimination step for now completes.
The only missing (and tricky) piece is how to compute the list representation of the new factor \{ } efficiently.In the next section, we will introduce an efficient algorithm for this task using RangeSum data structures and it then to the following main theorem.Its proof is deferred to Appendix C.6, after the introduction of RangeSum data structures.The inverse Ackermann factor (14|D |, |D |) in the aggregation algorithm is inherited from the algorithm supporting RangeSum queries.Thus, the guarantee of the RangeSum problem carries over to our aggregation algorithm: for semigroups that accepts additive inverse, or for the semigroup with minimum (or maximum) as the operation (RMQ as in [14]), the aggregation algorithm runs in (| | • |D |) time, without the additional inverse Ackermann overhead.

C.5 The oracle-construction algorithm
RangeSum Chazelle and Rosenberg [11] studied the range query problem RangeSum in the semigroup model defined as follows.Preprocess a array of elements from a semigroup S = ( , ⊕), and then support the following query: given a query range [ − , + ], return the range sum In particular, they proved the following theorem.

T C.8 ([11]
).There is a data structure of size ( ) that works in the word RAM, and support a RangeSum query in ( (14 , )) time, where is the inverse Ackermann function, after spending ( ) preprocessing time.
It is the absence of additive inverse for general semirings that results in the hardness of RangeSum and an unconditional hard instance can be recursively constructed that mimics the definition of the inverse Ackermann function (•, •) as in [11].It is easy to see that RangeSum queries can be answered in (1) time after ( ) preprocessing, if the underlying semigroup S allows for additive inverse: one candidate algorithm can precompute the partial sums The RangeSum problem with minimum as the semigroup operation, is studied intensively and typically known as RangeMinimum ery (RMQ).In their influential paper [14], Gabow, Bentley and Tarjan shown that there is an algorithm that works in the word RAM and supports a RMQ query in (1) time, after spending ( ) preprocessing time and space.They recognized the Cartesian tree as the instrumental data structure that was introduced by Vuillemin [25] in the context of average time analysis of searching.
In this section, we introduce a key oracle (called RangeSumOrcale) that supports efficient computation of the new negative factors \{ } in the aggregation algorithm introduced in (17) of the last section.In particular, RangeSumOrcale uses the RangeSum data structures as a black-box, thus inherits the inverse Ackermann factor of Theorem C.8 under general semirings.We first introduce the following definition.
RangeSumOracle Let be an an array of size over a semigroup S = ( , ⊕), where we implicitly assume that An array decomposition of is an array of pairs as follows: and 0, ⊥, are called the break points of the array decomposition) and each pair contains a range and a semigroup sum over that range.For convenience, we add a dummy pair ([ + 1, ⊥], 0) indicating the end of the array decomposition.A trivial array decomposition of is the array of pairs whose set of break points is {1, 2, . . ., }.
An RangeSumOracle of the array is a RangeSum data structure built on an array decomposition of such that: given a query range [ + 1, ], where < are two break points of the array decomposition (so that the query range aligns with the smaller range sums stored in the array decomposition), it returns the range sum over the query range It is easy to see from Theorem C.8 that there is a data structure of size ( ) and support a RangeSumOracle query in ( (14 , )) time, where (•, •) is the inverse Ackermann function, after spending ( ) preprocessing time.Indeed, for a trivial array decomposition, the RangeSumOracle is essentially a RangeSum data structure of .

Π
Observe that the break points constructed in T (a \{ } ) is a subset of those of T −1 (Π −1 \{ } a \{ } ), thus we are free from the risk of asking the oracle T −1 (Π −1 \{ } a \{ } ) with mis-aligned intervals, when constructing T (a \{ } ).The number of break points to be constructed in the array decomposition T (a \{ } ) is at most ≤ ≤ deg (a \{ } ), where deg (a \{ } ) denotes the degree of a \{ } in , i.e., the number of tuples in that coincide with a \{ } on the variables x \{ } .Therefore, the total number of break points in the oracle T (x \{ } ) is at most input a tuple a \{ } ∈ ≤ ≤ Π \{ } (i.e. a query key to identity the array decomposition T (a \{ } ) to be queried) and a query range [ − + 1, + ], where 0 ≤ − ≤ + are two break points of the array decomposition T (a \{ } ).In particular, the break points of the array decomposition T (a \{ } ) are and − 1, where a \{ } , (or 0 if − = + ).To conclude this section, we present the following theorem, whose proof is immediate from the prior discussion in this section.Here, = ( ), where we recall that ( ) is number of hyperedges containing the signed-leaf in H . ≤ ≤ Π \{ } to identity the array decomposition T (a \{ } ) to be queried, and a query range [ − + 1, + ], where 0 ≤ − ≤ + are two break points of the array decomposition T (a \{ } ).In particular, the break points of the array decomposition T (a \{ } ) are and − 1, where C.6 The signed-elimination step: a summary We finally prove the main theorem Theorem C.2 for free-connex signed-acyclic NestFAQ ¬ queries.
Recall that for a signed-leaf, say , we run the following algorithms in order: From now on, we follow the enumeration algorithm for full NestFAQ ¬ queries proposed in Appendix C.1 and by Appendix C.4, thus we close the proof.

D MISSING PROOFS IN SECTION 6 D.1 Some Notations and Results
We apply a characterization for -acyclic hypergraphs by Beeri, Fagin, Maier and Yannakakis [4].The Gaifman graph of a hypergraph H is the clique graph of H , i.e., we replace every hyperedge of size with a clique of size over the variables in the hyperedge.

Definition D.1 (Conformal Hypergraph).
A hypergraph H is called conformal if for every clique in its Gaifman graph, there exists an atom covering the clique.

Definition D.2 (Chordal Hypergraph).
A hypergraph H is called chordal if every cycle of length at least 4 in its Gaifman graph has a chord.

T D.3 (B ., 83').
A hypergraph H is -acyclic if and only if it is conformal and chordal.
We also reprove a lemma which strengthens Theorem D.3 for signed hypergraphs [7].The result originally appeared in French [7].Given a signed hypergraph H = ([ ], E + , E − ), for some ⊆ [ ] we denote by H [ ] the induced signed hypergraph on the vertex set .P .Take a minimal set of vertices such that H [ ] is not -acyclic.Take the minimal set E ′ ⊆ E − such that the hypergraph with edges E + [ ] ∪ E ′ is not -acyclic.By Theorem D.3, there exists a set of vertices ′ such that H ′ [ ′ ] is a cycle or a clique.In both cases, the minimality of imposes that = ′ .If H [ ′ ] were a cycle, then we are done.Otherwise, H [ ′ ] is a clique.If E ′ = ∅, then we are also done.Otherwise, let ∈ E ′ .By the minimality of E ′ , we know that (1) ∉ E + and (2) participates as an edge in the clique H ′ [ ].But then note that (E + [ ] ∪ E ′ \ { })[ \ ], for the unique where ∈ but ∉ , is a clique of smaller size.This contradicts the minimality of .The proof is now complete.
It is easy to see that all relations can be constructed in ( 2 ) time and also have size ( 2 ).We prove that the output of on this relational instance (projecting on , ), denoted as , encodes the resulting matrix of the Boolean matrix multiplication.

D.3 Missing proofs in Section 6.2
We begin with the definitions of -clique embedding and its weak edge depth.Definition D.5 ( -Clique Embedding).Let be the -clique and H be a hypergraph.A -clique embedding to H , denoted as ↦ → H , maps every vertex in to a non-empty subset ( ) ⊆ (H ) such that the followings hold: (1) ( ) induces a connected subhypergraph; (2) for any , in , either ( ) ∩ ( ) ≠ ∅ or there exists a hyperedge ∈ (H ) such that ∩ ( ) ≠ ∅ and ∩ ( ) ≠ ∅.
In this paper, we care about the weak edge depth of a -clique embedding .Given a -clique embedding , we define ∀ ∈ (H ) the weak edge depth of as ( ) := |{ ∈ ( ) | ( ) ∩ ≠ ∅}|.Then for a -clique embedding, its weak edge depth is defined to be wed( ) := max ( ).Finally, we define wed( ) to be the minimum weak edge depth of any -clique embedding to H .We are now ready to define the signed clique embedding power.where H is the set of all possible hypergraphs with hyperedges E + ∪ E ′ where E ′ ⊆ E − .
We first show that the singed clique embedding power for non-signed-acyclic queries is strictly greater than 1.
T D.7.If H is a non-signed-acyclic signed hypergraph, then its signed clique embedding power is strictly greater than 1.P T D.7.Since H is not signed-acyclic, there exists a subset E ′ ⊆ E − such that the hypergraph H ′ = ([ ], E + ∪E ′ ) ∈ H is not -acyclic.By Theorem D.3, either H ′ is non-conformal or H ′ contains an induced ℓ-cycle for some ℓ ≥ 4.
If H ′ is non-conformal, take a -clique, ≥ 3 in its Gaifman graph such that no hyperedge in ′ covers the -clique.Now we define the embedding from to H ′ to be any one-to-one mapping between the vertices in and the -clique in H ′ .The weak edge depth of this embedding must be strictly smaller than , by the construction.Therefore, H ′ has signed clique embedding power strictly greater than 1, and so does H .
Otherwise, there exists an induced ℓ-cycle for some ℓ ≥ 4. We take the same embedding from [13].We name the variables of the cycle query as 1 , . . ., ℓ .If ℓ is odd, let = (ℓ + 1)/2 and we construct the ℓ-clique embedding as follows: Observe that the weak edge depth of the embedding is strictly smaller than ℓ.Therefore in this case H also has signed clique embedding power strictly greater than 1.
We now show that the signed clique embedding power provides a conditional lower bound for the running time of a SumProd ¬ query over the tropical semiring, assuming Conjecture 6.6.

T 1 . 3 .
Let be a free-connex signed-acyclic FAQ ¬ query over a commutative semiring S with additive inverse.Then there is an algorithm that can enumerate with (| | 3 +| |•|D|) preprocessing time and (| |) delay.

Figure 1 :
Figure 1: Given the query in Example 4.1, the database instance in (a) and the signed-elimination sequence = (1, 2, 3, 4) as inputs, Algorithm 1 first produces the data structure L 4 in (c), and then constructs a new query ′ in Example 4.1, a new database as in (b), and the sequence ′ = (1, 2, 3) as inputs to the recursive call.

Example 4 . 3 .
Algorithm 3 takes as input the basic linked list data structure and all tuples in the negative atoms ¬ ( 2 , 3 , 4 ) and ¬ ( 1 , 2 , 3 , 4 ) in Figure 1(a) to produce the data structure in Figure 1(c).Note that the skipping link bypassing d 4 with label (a 4 , b 3 , c 4 ) also needs to bypass both skipping links labeled by (b 3 , c 3 ) and (b 3 , c 3 ) that bypass d 2 and d 5 respectively.

A. 3 5 C A. 5 .
Proof of Proposition 3.Let H = (V, E + , E − ) be a signed-acyclic signed hypergraph.Then we have (1) the hypergraph H [\ ] is signed-acyclic, for every vertex ∈ V; and is also -acyclic by Lemma A.2, as desired.

Algorithm 5 :
currNode.nextM(a ) Input: a list node currNode from L , a tuple a with ⊇ \ { } Global variables of L

Figure 4 :
Figure 4: Intermediate databases and list data structures produced for Example B.1.
is a safe NestFAQ ¬ expression recognized by the CFG in (5).Sometimes we call (x [ ] ) a full NestFAQ ¬ query (i.e.= [ ]). • = [ ] ⊆ [ ] is the set of free variables for some integer 0 ≤ ≤ .• (query input and output) The input to a NestFAQ ¬ query is a database instance D that contains the constants ∈ \ {0} and a list representation for every factor in (x [ ] ).The size of a database instance |D| is defined as the total number of rows to store the constants and the list representations.An answer (or output) of is a tuple a , (a ) ∈ Dom(x ) such that a ∈ Dom(x ) and (a ) ≠ 0. The set of all answers of is denoted by (D).• (complexity measure) In this work, we measure the combined (and data) complexity of evaluating a NestFAQ ¬ query on a database instance D by the query size | | = | | = ∈ [ ] ( ) and the database size |D|.

T C. 3 .
Thus, D * is of size (|D|) and can be constructed in (|D|) time by scanning over the list representation of every factor in D. We reason about the such a construction of Enum( * , D * ) through the following theorem: Let be a signed-acyclic full NestFAQ ¬ query associated with a signed hypergraph H = ([ ], E + , E − ) over a commutative semiring S, and D be a corresponding database instance.Then, a tuple a [ ] is an answer of (i.e.(a [ ] ) ≠ 0) if and only if a [ ] is an answer of * (D * ) (i.e. a [ ] ∈ * (D * ), or * (a [ ] ) = true), where * and D * are defined in (7) and (8), respectively.
If a signed hypergraph H = ([ ], E + , E − ) is not -acyclic, then we can find a set of vertices ⊆ [ ], with | | ≥ 3, such that: -the Gaifman graph of the positive hyperedges in H [ ] is a clique of size | |; -there exists a subset E ′ of the negative hyperedges in H [ ] such that the Gaifman graph of the union of E ′ with the positive atoms in H [ ] is a | |-cycle.

Definition D. 6 (
Signed Clique Embedding Power).Given a signed hypergraph H , define the signed clique embedding power of H as semb(H ) := max
, it replaces 123 with a new factor ′ 123 := 123 ⊗ ( 23 ⊢ c 23 ) ⊗ 3 , the entry table of which is exactly that of 123 , except for multiplying the extra weight ( 23 ⊢ c 23 ) ⊗ 3 to each table entry.Then, we get a new AST in Figure 2 (middle) since ), 18 into the new factor ′′ 2 ( 2 ), where 18 is yet another [1, 15] query on the oracle of ′ 23 (a 3 , 3 ).The second equality holds because for all values of 2 not encoded in the table of ′′ 2 , we have Figure 3: The oracle-construction steps on the refactored AST.The oracle of ′ 23 (a 1 , 3), ′ 23 (a 2 , 3 ) and ′ 23 (a 3 , 3 ) are built atop the oracle of ′ 3 ( 3 ); the oracle of ′ 123 (b 1 , a 1 , 3 ) and ′ 123 (b 2 , a 2 , 3 ) are built atop the oracle of ′ 23 (a 1 , 3 ) and ′ 23 (a 2 , 3 ′ 123 (b 1 , a 1 , 3 ) } that contains .Then by definition of [H ], we must have that ⊆ , and therefore is not present in [H ].Therefore, , 1 , 2 , . . ., are all hyperedges in [H ] that contain .Hence is a -leaf of [H ]. ⇐= Assume that is a -leaf in [H ].Let 1 , 2 , . . ., be all hyperedges in [H ] that contain , and assume that and ⊆ +1 for ∈ [ − 1].We argue that { 1 } = E + ∩ E ′ + , i.e. 1 ∈ E ′ + and for every ∈ {2, 3, . . ., − 1}, ∈ E ′ − .Let be the smallest index such that ∈ E ′ + .Such an must exist, since otherwise otherwise we have ∈ since H is reduced, we must have H = (V, { }, ∅), and the lemma follows since pair of vertices in H are non-neighbor -leaves.If ∈ E − , consider H ′ = (V, E + , E − \{ }).Note that H ′ is also signed-acyclic, reduced and of smaller size.Thus by the inductive hypothesis, H ′ contains two non-neighbor -leaves and .Then and are also -leaves in H , since ⊆ V = for every hyperedge in H .The vertices and are nonneighbors, because if not, there exists some hyperedge in H such that , ∈ and ⊂ , and thus is in H ′ , a contradiction to that and are non-neighbors in H ′ In what follows, we assume that H contains no hyperedge with = V and has at least 3 vertices (by the inductive step).Consider the signed hypergraph H ′ = [H [\ ]], i.e., H ′ is obtained by first removing from H and then taking its reduced hypergraph.By Proposition A.6, H ′ is signed-acyclic with size less than .By the inductive hypothesis, H We argue that there is no hyperedge in H that can contain both vertices and .Indeed, if not, assume that contains both and in H .By definition of , we have that ⊆ , and we would have ∈ , a contradiction to our choice of .Let 1 , By the inductive hypothesis, is a -leaf in H ′ = [H [\ ]], and thus is a -leaf in H since the chain 1 , 2 , . . ., in H ′ remains in H . Since H is signed-acyclic, H is -acyclic and thus contains an -leaf .By Claim A.1, there is a -leaf in H that is non-neighbor with .Then is also an -leaf in H , and by Claim A.1 again, there is a -leaf in H that is non-neighbor with .Hence and are non-neighbor -leaves in H , as desired.This concludes the proof.-leaves and .By Lemma A.1, and are both signed-leaves of H . Let H be a signed-acyclic signed hypergraph.If H contains only one vertex , then it is only possible that H = ({ }, {{ }}, E − ) where E − ⊆ {{ }}, and in all cases is a signed-leaf of H .If H contains at least two vertices, the claim follows by Lemma A.4.
CA.1.If is a -leaf in a reduced signed hypergraph H , then there exists a -leaf in H such that and are non-neighbors in H .P. Assume that is an -leaf in H . Then there exists a hyperedge ∈ E + ∪ E − such that for any hyperedge ∈ E + ∪ E − that contains , ⊆ .Note that ≠ V. ′ contains two non-neighbor -leaves and .Since and are non-neighbors and ≠ V, either or is not contained in \ { } and we assume that ∉ \ { }. 2 , . . ., be all hyperedges in H such that ∈ for each ∈ [ ].We argue that every also appear in H ′ = [H [\ ]].First, every appears in H ′ = H [\ ] since we argued that there is no hyperedge in H that contains both and .Suppose for contradiction that some hyperedge is not contained in [H [\ ]].Then there exists some hyperedge ∈ E + such that ⊆ \ { } ⊆ , a contradiction that H is reduced.L A.4.Every signed-acyclic signed hypergraph with at least two-vertices have two non-neighbor signed-leaves.P .When H contains at least two vertices, by Lemma A.2 and A.3, [H ] is reduced, signed-acyclic and contains two non-neighbor by Lemma A.2, H ′ is -acyclic, as desired.
x [ ] ) (recognized by CFG) into (x [ ] ) (recognized by CFG ) is that the productions of CFG are layed out in a principled way:is only contained in one terminal (or non-terminal).From an AST standpoint, (we let 0 = for convenience), gifted by the -property of the signed-leaf .A convenient way to derive (x [ ] ) in CFG is as follows (let