Answering Conjunctive Queries with Inequalities

In this paper, we study the complexity of answering conjunctive queries (CQ) with inequalities). In particular, we are interested in comparing the complexity of the query with and without inequalities. The main contribution of our work is a novel combinatorial technique that enables us to use any Select-Project-Join query plan for a given CQ without inequalities in answering the CQ with inequalities, with an additional factor in running time that only depends on the query. The key idea is to define a new projection operator, which keeps a small representation (independent of the size of the database) of the set of input tuples that map to each tuple in the output of the projection; this representation is used to evaluate all the inequalities in the query. Second, we generalize a result by Papadimitriou-Yannakakis [17] and give an alternative algorithm based on the color-coding technique [4] to evaluate a CQ with inequalities by using an algorithm for the CQ without inequalities. Third, we investigate the structure of the query graph, inequality graph, and the augmented query graph with inequalities, and show that even if the query and the inequality graphs have bounded treewidth, the augmented graph not only can have an unbounded treewidth but can also be NP-hard to evaluate. Further, we illustrate classes of queries and inequalities where the augmented graphs have unbounded treewidth, but the CQ with inequalities can be evaluated in poly-time. Finally, we give necessary properties and sufficient properties that allow a class of CQs to have poly-time combined complexity with respect to any inequality pattern. We also illustrate classes of queries where our query-plan-based technique outperforms the alternative approaches discussed in the paper.


Introduction
In this paper, we study the complexity of answering conjunctive queries (CQ) with a set of inequalities of the form x i = x j between variables in the query. The complexity of answering CQs without inequalities has been extensively studied in the literature during the past three decades. Query evaluation of CQs is NP-hard in terms of combined complexity (both query and database are inputs), while the data complexity of CQs (query is fixed) is in AC 0 [1]. Yannakakis [22] showed that evaluation of acyclic CQs has polynomial-time combined complexity. This result has been generalized later to CQs with bounded treewidth, bounded querywidth, or bounded hypertreewidth: the combined complexity remains polynomial if the width of a tree or query decomposition of the query (hyper-)graph is bounded [6,10,14,9].
However, the complexity of query evaluation changes drastically once we add inequalities in the body of the query. Consider the following Boolean acyclic CQ P k which can be solved in O(k|D|) time on a database instance D: If we add the inequalities x i = x j for every i < j and evaluate it on an instance where each R , 1 ≤ ≤ k, corresponds to the edges in a graph with k + 1 vertices, query evaluation becomes equivalent to asking whether the graph contains a Hamiltonian path, and therefore is NP-hard in k. Papadimitriou and Yannakakis [17] observed this fact and showed that still the problem is fixed-parameter tractable for acyclic CQs: Theorem 1.1 ( [17]). Let q be an acyclic conjunctive query with inequalities and D be a database instance. Then, q can be evaluated in time 2 O(k log k) · |D| log 2 |D| where k is the number of variables in q that appear in some inequality.
The proof is based on the color-coding technique introduced by Alon-Yuster-Zwick in [4] that finds subgraphs in a graph. In general, answering CQs with inequalities is closely related to finding patterns in a graph, which has been extensively studied in the context of graph theory and algorithms. For example, using the idea of representative sets, Monien [15] showed the following: given a graph G(V, E) and a vertex s ∈ V , there exists a deterministic O(k! · |E|) algorithm that finds all vertices v with a length-k path from s and also reports these paths (a trivial algorithm will run in time O(|V | k )). Later, Alon et al. proposed the much simpler color-coding technique that can solve the same problem in expected time 2 O(k) |V | for undirected graphs and 2 O(k) |E| for directed graphs. These two ideas have been widely used to find other patterns in a graph, e.g., for finding cycles of even length [3,24,4].
In the context of databases, Papadimitriou and Yannakakis [17] showed that answering acyclic CQs with comparison operators between variables (<, ≤ etc.) is harder than answering acyclic CQs with inequalities ( =) since this problem is no longer fixed-parameter tractable. The query containment problem for CQs with comparisons and inequalities ( =, <, ≤), i.e., whether Q 1 ⊆ Q 2 , has been shown to be Π p 2 -complete by van der Meyden [20]; the effect of several syntactic properties of Q 1 , Q 2 on the complexity of this problem has been studied by Kolaitis et al. [14]. Durand and Grandjean [8] improved Theorem 1.1 from [17] by reducing the time complexity by a log 2 |D| factor. Answering queries with views in the presence of comparison operators has been studied by Afrati et al. [2]. Rosati [19] showed that answering CQs with inequalities is undecidable in description logic.
Our Contributions. In this paper we focus on the combined complexity of answering CQs with inequalities ( =) where we explore both the structure of the query and the inequalities. Let q be a CQ with a set of variables, I be a set of inequalities of the form x i = x j , and k be the number of variables that appear in one of the inequalities in I (k < |q|). We will use (q, I) to denote q with inequalities I, and D to denote the database instance. We will refer to the combined complexity in |D|, |q|, k by default (and not the data complexity on |D|) unless mentioned otherwise.
The main result in this paper says that any query plan for evaluating a CQ can be converted to a query plan for evaluating the same CQ with arbitrary inequalities, and the increase in running time is a factor that only depends on the query: Theorem 1.2 (Main Theorem). Let q be a CQ that can be evaluated in time T (|q|, |D|) using a Select-Project-Join (SPJ) query plan P q . Then, a query plan P q,I for (q, I) can be obtained to evaluate (q, I) in time g(q, I) · max(T (|q|, |D|), |D|) for a function g that is independent of the input database. 1 The key techniques used to prove the above theorem (Sections 3 and 4), and our other contributions in this paper (Sections 5, 6, and 7) are summarized below.
1. (Section 3, 4) Our main technical contribution is a new projection operator, called H-projection.
While the standard projection in relational algebra removes all other attributes for each tuple in the output, the new operator computes and retains a certain representation of the group of input tuples that contribute to each tuple in the output. This representation is of size independent of the database and allows the updated query plan to still correctly filter out certain tuples that do not satisfy the inequalities. In Section 3 we present the basic algorithmic components of this operator.
In Section 4, we show how to apply this operator to transform the given query plan to another query plan that incorporates the added inequalities.

(Section 5)
We generalize Theorem 1.1 to arbitrary CQs with inequalities (i.e., not necessarily acyclic) by a simple application of the color-coding technique. In particular, we show (Theorem 5.1) that any algorithm that computes a CQ q on a database D in time T (|q|, |D|) can be extended to an algorithm that can evaluate (q, I) in time f (k) · log(|D|) · T (|q|, |D|). While Theorem 1.2 and Theorem 5.1 appear similar, there are several advantages of using our algorithm over the color-coding-based technique which we also discuss in Section 5.

(Section 6)
The multiplicative factors dependent on the query in Theorem 1.1, Theorem 5.1, and (in the worst case) Theorem 1.2 are exponential in k. In Section 6 we investigate the combined structure of the queries and inequalities that allow or forbid poly-time combined complexity. We show that, even if q and I have a simple structure, answering (q, I) can be NP-hard in k (Proposition 6.4). We also present a connection with the list coloring problem that allows us to answer certain pairings of queries with inequalities in poly-time combined complexity (Proposition 6.6).

(Section 7)
We provide a sufficient condition for CQs, bounded fractional vertex cover, that ensures poly-time combined complexity when evaluated with any set of inequalities I. Moreover, we show that families of CQs with unbounded integer vertex cover are NP-hard to evaluate in k (Theorem 7.2).

Preliminaries
We are given a CQ q, a set of inequalities I, and a database instance D. The goal is to evaluate the query with inequality, denoted by (q, I), on D. We will use vars(q) to denote the variables in the body of query q and Dom to denote the active domain of D. The set of variables in the head of q (i.e., the variables that appear in the output of q) is denoted by head(q). If head(q) = ∅, q is called a Boolean query, while if head(q) = vars(q), it is called a full query. The set I contains inequalities of the form x i = x j , where x i , x j ∈ vars(q) such that they belong to two distinct relational atoms in the query. We do not consider inequalities of the form x i = c for some constant c, or of the form x i = x j where x i , x j only belong to the same relational atoms because these can be preprocessed by scanning the database instance and filtering out the tuples that violate these inequalities in time O(|I||D|). We will use k to denote the number of variables appearing in I (k ≤ |vars(q)| < |q|).
Query Graph, Inequality Graph, and Augmented Graph.
Given a CQ q and a set of inequalities I, we define three undirected graphs on vars(q) as the set of vertices: The query incidence graph or simply the query graph, denoted by G q , of a query q contains all the variables and the relational atoms in the query as vertices; an edge exists between a variable x and an atom S if and only if x appears in S.
The inequality graph G I adds an edge between x i , x j ∈ vars(q) if the inequality x i = x j belongs to I.
The query (q, I) can be viewed as an augmentation of q with additional predicates, where for each inequality x i = x j we add a relational atom I ij (x i , x j ) to the query q, and add new relations I ij to D instantiated to tuples (a, b) ∈ Dom × Dom such that a = b. The augmented graph G q,I is the query incidence graph of this augmented query. Note that G q,I includes the edges from G q , and for every edge (x i , x j ) ∈ G I , it includes two edges (x i , I ij ), (x j , I ij ); examples can be found in Section 6.
Treewidth and Acyclicity of a Query. We briefly review the definition of the treewidth of a graph and a query.
with a set X(u) ⊆ V associated with each vertex u ∈ I of the tree, such that the following conditions are satisfied:

For all edges
3. For each v ∈ V , the set {u ∈ I : v ∈ X(u)} induces a connected subtree of T .
The width of the tree decomposition T = (I, F ) is max u∈I |X(u)| − 1. The treewidth of G is the width of the tree decomposition of G having the minimum width.
Chekuri and Rajaraman defined the treewidth of a query q as the treewidth of the query incidence graph G q [6]. A query can be viewed as a hypergraph where every hyperedge corresponds to an atom in the query and comprises the variables as vertices that belong to the relational atom. The GYO-reduction [11,23] of a query repeatedly removes ears from the query hypergraph (hyperedges having at least one variable that does not belong to any other hyperedge) until no further ears exist. A query is acyclic if its GYO-reduction is the empty hypergraph, otherwise it is cyclic. For example, the query There is another notion of width of a query called querywidth qw defined in terms of query decomposition such that the decomposition tree has relational atoms from the query instead of variables [6]; The relation between the querywidth qw and treewidth tw of a query is given by the inequality tw/a ≤ qw ≤ tw + 1, where a is the maximum arity of an atom in q. A query is acyclic if and only if its querywidth is 1; the treewidth of an acyclic query can be > 1 [6]. The notion of hypertreewidth has been defined by Gottlob et al. in [10]. A query can be evaluated in poly-time combined complexity if its treewidth, querywidth, or hypertreewidth is bounded [22,6,10,14,9].

Main Techniques
In this section, we present the main techniques used to prove Theorem 1.2 with the help of a simple query q 2 that computes the cross product of two relations and projects onto the empty set. In particular, we consider the query (q 2 , I) with an arbitrary set of inequalities I, where q 2 ( ) = R(x 1 , . . . , x m ), S(y 1 , . . . , y ). A naïve way to evaluate the query (q 2 , I) is to iterate over all pairs of tuples from R and S, and check if any such pair satisfies the inequalities in I. This algorithm runs in time O(m |R||S|). We will show instead how to evaluate (q 2 , I) in time f (q 2 , I)(|R| + |S|) for some function f that is independent of the relations R and S.
The key idea is to compress the information that we need from R to evaluate the inequalities by computing a representation R of R of such that the size of R only depends on I and not on R. Further, we must be able to compute R in time O(f (I)|R|). Then, instead of iterating over the pairs of tuples from R, S, we can iterate over the pairs from R and S, which can be done in time f (q 2 , I)|S|. The challenge is to show that such a representation R exists and that we can compute it efficiently.
We now formalize the above intuition. Let X = {x 1 , · · · , x m }, Y = {y 1 , · · · , y }. Let H = G I denote the inequality graph; since q 2 has only two relations, H is a bipartite graph on X and Y . If a tuple t from S satisfies the inequalities in I when paired with at least one tuple in R, we say that t is H-accepted by R, and it contributes to the answer of (q 2 , I). For a variable x i and a tuple t, let t[x i ] denotes the value of the attribute of t that corresponds to variable x i . Definition 3.1 (H-accepted Tuples). Let H = (X, Y, E) be a bipartite graph. We say that a tuple t over Y is H-accepted by a relation R if there exists some tuple t R ∈ R such that for every Notice that (q 2 , I) is true if and only if there exists a tuple t S ∈ S that is H-accepted by R. Figure 1(a)) and consider the instance for R as depicted in Figure 1(b). This setting will be used as our running example.
Observe that the tuple t = (2, 1, 3) is H 0 -accepted by R. Indeed consider the tuple t = (3, 2) in R: it is easy to check that all inequalities are satisfied by t, t . In contrast, the tuple (2, 1, 2) is not H 0 -accepted by R. H-equivalent relations form an equivalence class comprising instances of the same arity m. The main result in this section shows that for a given R, an H-equivalent instance R ⊆ R of size independent of R can be efficiently constructed.
Theorem 3.4. Let H = (X, Y, E) be a bipartite graph (|Y | = ) and R be a relation of arity There exists an instance R ⊆ R such that: To describe how the algorithm that constructs R works, we need to introduce another notion that describes the tuples of arity that are not H-accepted by R. Let ⊥ be a value that does not appear in the active domain Dom.  , where x can be any value, are H 0 -forbidden for R. Furthermore, notice that the tuple (1, 2, ⊥) is also H 0 -forbidden (in our construction (1, 2, ⊥) being H 0 -forbidden implies that any tuple of the form (1, 2, x) is H 0 -forbidden).
Next we formalize the intuition of the above example. We say that a tuple t 1 defined over Y subsumes another tuple t 2 defined over Y if for any y j ∈ Y , either t 1 [y j ] = ⊥ or t 1 [y j ] = t 2 [y j ]. Observe that if t 1 subsumes t 2 and t 1 is H-forbidden, t 2 must be H-forbidden as well. A tuple is minimally H-forbidden if it is H-forbidden and is not subsumed by any other H-forbidden tuple. In our example, (1, 2, 1) is subsumed by (1, 2, ⊥), so it is not minimally H 0 -forbidden, but the tuple (1, 2, ⊥) is. Lemma 3.7 stated below will be used to prove Theorem 3.4: To prove the above lemma, we present an algorithm that encodes all the minimally H-forbidden tuples of R in a rooted tree T H (R). The tree has labels for both the nodes and the edges. More precisely, the label L(v) of some node v is either a tuple in R or a special symbol ⊥ * (only the leaves can have label ⊥ * ), while the label of an edge of the tree is a pair of the form (y j , a), where y j ∈ Y and a ∈ Dom. The labels of the edges are used to construct a set of H-forbidden tuples that includes the set of all minimally H-forbidden tuples as follows: For each leaf node v with label L(v) = ⊥ * , let (y j 1 , a j 1 ), . . . , (y jm , a jm ) be the edge labels in the order they appear from the root to the leaf. Then, the tuple tup(v) defined on Y as follows is an H-forbidden tuple (but not necessarily minimally H-forbidden): (1, 2) y 3 : 1 y 1 : 1 y 2 : 1 y 2 : 2 Construction of T H (R). We construct T H (R) inductively by scanning through the tuples of R in an arbitrary order. As we read the next tuple t from R, we need to ensure that the H-forbidden tuples that have been so far encoded by the tree are not H-accepted by t: we achieve this by expanding some of the leaves and adding new edges and nodes to the tree. Therefore, after the algorithm has consumed a subset R ⊆ R, the partially constructed tree will be T H (R ). For the base of the induction, where R = ∅, we define T H (∅) as a tree that contains a single node (the root r) with label L(r) = ⊥ * .
For the inductive step, let T H (R ) be the current tree and let t ∈ R be the next scanned tuple. The algorithm processes (in arbitrary order) all the leaf nodes v of the tree with L(v) = ⊥ * . Let (y j 1 , a j 1 ), . . . , (y jp , a jp ) be the edge labels in the order they appear on the path from root r to v. We distinguish two cases (for tuple t and a fixed leaf node v): 1. There exists j ∈ {j 1 , . . . , j p } and edge (x i , y j ) ∈ E such that t[x i ] = a j . In this case, tup(v) will be H-forbidden in R ∪ {t}; therefore, nothing needs to be done for this v.
2. Otherwise (i.e., there is no such j), tup(v) is not a H-forbidden tuple for R ∪ {t}. We set L(v) = t (therefore, we never reassign the label of a node that has already been assigned to some tuple in R). There are two cases: (a) If p = , we cannot expand further from v (and will not expand in the future because now L(v) = ⊥ * ), since all y j -s have been already set.
The algorithm stops when either (a) all the tuples from R are scanned or (b) there exists no leaf node with label ⊥ * . Example 3.8 (Continued). We now illustrate the steps of the algorithm through the running example. After reading the first tuple, t 1 = (1, 1), the algorithm expands the root node r to three children (for y 1 , y 2 , y 3 ), labels L(r) = (1, 1) and labels the new edges as (y 1 , 1), (y 2 , 1), (y 3 , 1) and the new three leaves as ⊥ * .
Suppose the second tuple t 2 = (1, 2) is read next. First consider the leaf node with label ⊥ * that is reached from the root through the edge (y 1 , 1). At this point, the node represents the tuple (1, ⊥, ⊥). Observe that are in case (1) of the algorithm, and so the node is not expanded (t 2 [x 1 ] = 1 and m = 1 < 3 = ). Consider now the third leaf node with label ⊥ * , reached through the edge (y 3 , 1). We are now in case (2), and we have to expand the node. The available edges (since we have already assigned a value to y 3 ) are (x 1 , y 1 ), (x 1 , y 2 ), (x 2 , y 2 ). Hence, the node is labeled (1,2), and expands into three children, one for each of the above edges. These edges are labeled by (y 1 , 1), (y 2 , 1), (y 2 , 2) respectively; then the algorithm continues and at the end the tree in Figure 2 is obtained.
The H-forbidden tuples encoded by the tree are not necessarily minimally H-forbidden. However, for every minimally H-forbidden tuple there exists a node in the tree that encodes it. In the running example, we find only two minimally H 0 -forbidden tuples for R: (1, 2, ⊥) and (2, 1, 2). Furthermore, the constructed tree is not unique for R and depends on the order in which the tuples in R are scanned. The following lemma sums up the properties of the tree construction, and directly implies Lemma 3.7.
Lemma 3.9. T H (R) satisfies the following properties: 2. Every leaf of T H (R) with label ⊥ * encodes a H-forbidden tuple.
3. Every minimally H-forbidden tuple is encoded by some leaf of the tree with label ⊥ * . Proof. We start by showing item (1). The first observation is that the depth of the tree is at most . Indeed, consider any path from the root to a leaf, and let (y j 1 , a j 1 ), . . . , (y jm , a jm ) be the labels of the edges. By the construction in step (2), all j a are pairwise disjoint, and so we can have at most such labels in the path. Notice additionally that each such path visits a subset of the nodes in Y in some order, and maps each node maps it to one of its neighbors in X. This implies that the number of leaves in T H (R) can be at most Item (2) is straightforward and follows by the fact that only the expansion step (2) of the algorithm can assign the label ⊥ * to a node.
Finally, we prove item (3). Let t be a minimally H-forbidden tuple. We will show that the algorithm will produce t at some leaf of the tree. Our argument will trace t along a path from the root of T H (R) to the appropriate leaf.
Consider the tuples of R in the order visited by the algorithm: t 1 , t 2 , . . . . We will show the following inductive statement: for each tuple t a , there exists a leaf node v a in the tree with label ⊥ * such that tup(v a ) is H-forbidden for {t 1 , . . . , t a } and subsumes t. This statement suffices to prove (3), since at the point where a = |R| = m (i.e., all the tuples in R have been scanned), tup(v a ) must equal t (otherwise t is not minimal), and also L(v a ) = ⊥ * .
The statement vacuously holds before no tuples from R have been scanned for the root node that encodes (⊥, · · · , ⊥), and forms the basis of the induction. Now, suppose that we are at some tuple t a and node v a where the inductive statement holds. Let t a+1 be the next tuple in the order. If the algorithm falls into case (1), then v a+1 = v a and tup(v a+1 ) = tup(v a ). Since tup(v a ) is H-forbidden for {t 1 , . . . , t a } and subsumes t, it will be H-forbidden for {t 1 , . . . , t a+1 } as well, and still subsume t. Further, the label of v a+1 = v a remains ⊥ * . Now suppose we fall into case (2) and t a+1 is read. Let y j 1 , . . . , y jp be the variables set so far in tup(v a ) where v a is labeled ⊥ * . First note that we cannot fall into case (2a), i.e. p < . Indeed, if p = and t a+1 satisfies all inequalities with tup(v a ), then tup(v a ) is not H-forbidden. Since all the positions of tup(v a ) have been set and tup(v a ) subsumes t, it must hold that tup(v a ) = t. It follows that t is not H-forbidden, which contradicts the fact that t remains H-forbidden after all tuples in R are read.
Therefore, we are in case (2b), and for all s ∈ [p], there exists some By construction, the algorithm will choose (x i , y j ) at step (2) to expand v a and create a child v a+1 that connects with an edge (y j , t[y j ]). Note that, v(t a+1 ) still subsumes t, is H-forbidden for the tuples t 1 , · · · , t a+1 , and has label ⊥ * , which proves the induction hypothesis for t a+1 .
For our running example, φ(H 0 ) = 3! · (1 · 2 · 1) = 12, whereas the tree T H 0 (R) has only 10 leaves. We should note here that the bound φ(H) is tight, i.e. there exists an instance for which the number of minimally H-forbidden tuples is exactly φ(H). 2 We now discuss how we can use the tree T H (R) to find a small H-equivalent relation to R. It turns out that the connection is immediate: it suffices to collect the labels of all the nodes (not only leaves) of the tree T H (R) that are not ⊥ * . More formally: We can now show the following result, which completes the proof of Theorem 3.4: Proof. The proof of H-equivalence is based on the observation that if T H (R) = T H (R ), then R, R must be H-equivalent. Indeed, both trees will have the same minimally H-forbidden tuples, and therefore the set of tuples that are H-accepted will be same. To see that T H (R) = T H (E H (R)), consider R and suppose that we remove some tuple t that does not appear at any label of the tree (and therefore the resulting instance equals E H (R)). If we keep the same order of scanned tuples when constructing both trees, the exact same tree will be produced (since t will not expand any node or add any label).
To prove the size bound, we have to give a bound on the number of nodes in the tree, |V (T H (R))|. For every possible mapping of nodes y j to one of its neighbors in H (there are j∈[ ] d H (y j ) such mappings), consider the subtree of T H (R) that contains only the paths from root to leaves where all the edges agree with the mapping (remember that each node creates a child corresponding to an edge (x i , y j ) of H); we will first count the nodes of such a subtree. This is because the root node can have at most children corresponding to ≤ edges in the mapping. Each child of root can have at most − 1 children as one of the edges in the mapping has been used in the first level. Therefore, this subtree will be of size at most Since the union of these subtrees will cover all the nodes of T H (R), we obtain that the e · φ(H) is an upper bound for the size of the tree. Although the set of minimally H-forbidden tuples is the same irrespective of the order by which the algorithm scans the tuples, the relation E H (R) depends on this order. It is an open problem to find the smallest possible H-equivalent relation for R.

Query Plans for Inequalities
In this section, we use the techniques presented in the previous section as building blocks and prove Theorem 1.2. A Select-Project-Join (SPJ) query plan refers to a relational algebra expression that uses only selection (σ), projection (Π), and join ( ) operators. Let P q be any SPJ query plan that computes a CQ q (without inequalities) on a database instance D in time T (|q|, |D|). We will show how to transform P q into a plan P q,I that computes (q, I) in time g(q, I) · max(T (|q|, |D|). Without loss of generality, we assume that all the relation names and attributes in the base and derived relations (at intermediate steps in the plan) are distinct. Our running example for this section is given below: The query plan P q 0 is depicted in Figure 3.
Clearly, this plan by itself does not work for (q 0 , I) as it is losing information that is essential to evaluate the inequlities, e.g., B(= B ) is being projected out and it is used later in the inequality x = w with the attribute C of T . To overcome this problem while keeping the same structure of the plan, we define a new projection operator that allows us to perform valid algebraic transformations, even in the presence of inequalities. Let att(R) be the set of attributes that appear in a base or derived relation R; a query plan or sub-plan P is a derived relation with attributes att(P). If X ⊆ att(R), letX R = att(R) \ X.  H-Projection). Let R be a base or a derived relation in P. Let X ⊆ att(R) and H = (X R , att(P) \ att(R), E) be a bipartite graph. Then, the H-projection of R on X, denoted Π H X (R), is defined as where E H denotes an H-equivalent subrelation as defined and constructed in equation (1).
Intuitively, H contains the inequalities between the attributes inX R (that are being projected out) and the attributes of the rest of the query plan. The operator Π H X first groups the tuples from R according to the values of the X-attributes, but then instead of projecting out the values of the attributes inX R for each such group, it computes a small H-equivalent subrelation according to the graph H.
First step. To construct the plan P q,I from P q , we first create an equivalent query plan P q, by pulling all the projections in P q to the top of the plan. The equivalence of P q and P q, is maintained by the following standard algebraic rules regarding projections: (Rule-2) Distribution: If X 1 ⊆ att(R 1 ) and (Rule-3) Commutativity with Selection: If the selection condition θ is over a subset of X, then σ θ (Π X (R)) = Π X (σ θ (R)). Figure 3 depicts how each rule is applied in our running example to transform the initial query plan P q 0 to P q 0 , , where the only projection occurs in the top of the query plan. Observe that to distribute a projection over a join R 1 A 1 =A 2 R 2 (and not a cartesian product), we can write it as σ A 1 =A 2 (R 1 × R 2 ), use both (Rule-2) and (Rule-3) to push the projection, and then write it back in the form as The plan P q, will be of the form P q, = Π X (P 0 ), where P 0 is a query plan that contains only selections and joins. Notice that the plan Π X (σ I (P 0 )) correctly computes (q, I), since it applies the inequalities before projecting out any attributes. 3 However, the running time is not comparable with that of P q since the structures of the plans P q and Π X (σ I (P 0 )) are very different. To achieve comparable running time, we modify Π X (σ I (P 0 )) by applying the corresponding rules of (Rule-1), (Rule-2), (Rule-3) for H-projection in the reverse order.
Second step. To convert projections to H-projections, first, we replace Π X with Π H 0 X , where H 0 = (att(P 0 ) \ X, ∅, ∅). Notice that Π H 0 X is essentially like Π X , but instead of removing the attributes that are not in X, the operator keeps an arbitrary witness. Thus, if we compute Π H 0 X (σ I (P 0 )), we not only get all tuples t in (q, I), but for every such tuple we obtain a tuple t from (q f , I) such that t = t [X]. For our running example, X = {D}, and therefore, H 0 = ({A, B, B , C, C , E}, ∅, ∅) (see the rightmost plan in Figure 4).
Third step. We next present the rules for H-projections to convert Π H 0 X (σ I (P 0 )) to the desired plan P q,I . To show that the rules are algebraically correct, we need a weaker version of plan equivalence.
In other words, we do not need to have the same values of the attributes that are being projected out by Π X in the small sub-relations E H . We write I[X 1 , X 2 ] ⊆ I to denote the inequalities between attributes in subsets X 1 and X 2 . For convenience, we also write I[X, X] = I[X]. We use E[X 1 , X 2 ] in a similar fashion, where E is the set of edges in a bipartite graph. Let A = att(P 0 ). We apply the transformation rules for a sub-plan that is of the form Π H X (σ I (S)), where I is defined on att(S) and H = (X S , A, E). 4 The rules are: (Rule-2').
This rule adds new edges to the bipartite graph (which is initially empty) from the set of inequalities I. In the running example, we have In the running example, we move the selection operator σ E='a' before the projection operator Π H 2 C,E as the last step of the transformation. (Rule-1'). Denote S = σ I (S). It suffices to show that for every tuple α, E 1 = E H (σ X=α (S )) and E 2 = E H (σ X=α (Π H Y (S ))) are H-equivalent. Fix some X = α. The one direction is based on the observation that Π H Y (S ) ⊆ S . Hence, σ X=α (Π H Y (S )) ⊆ σ X=α (S ), which implies that if a tuple is H-accepted by E 2 , it is accepted by E 1 as well.
For the other direction, suppose that t is H-accepted by E 1 . Then, there exists some s ∈ σ X=α (S ) such that E |= s • t. 5 SinceȲ S ⊆X S , E[Ȳ S , A] |= s • t and t must be H -accepted by σ Y =s[Y ] (σ X=α (S )), and consequently by E H (σ Y =s[Y ] (σ X=α (S ))) as well. Then, there exists some , we must also have that E |= t • s . Since s ∈ Π H Y (σ X=α (S )), we conclude that t is H-accepted by E 2 .
(Rule-2'). Denote R 1 = σ I[Z 1 ] (R 1 ) and I 1 = I \ I[Z 1 ]. It suffices to show that for every tuple α, the following are H-equivalent: The one direction of the equivalence is based on the fact that Π H 1 X 1 (R 1 ) ⊆ R 1 . The other direction is more involved.
Suppose that t is H-accepted by E 1 . Then, there exists some s ∈ R 1 × R 2 such that E, I |= s • t and s[X] = α. Now, consider the tuple t • s[Z 2 ]. The crucial observation is that t • s[Z 2 ] is H 1 -accepted by σ X 1 =α[X 1 ] (R 1 ), and thus by E H 1 (σ X 1 =α[X 1 ] (R 1 )) as well. Then, there exists some = α, and also satisfies I . This implies that t is H-accepted by E 2 .
(Rule-3'). This is immediate, since the selection θ is applied only on the attributes in X, which are not projected out.
After applying the above transformations in the reverse order, the following lemma holds: Lemma 4.6. Let P q be an SPJ plan for q. For a set of inequalities I, the transformed plan P q,I has the following properties: 1. If P q, = Π X (P 0 ), the plan Π X (P q,I ) computes (q, I) (after projecting out the attributes that served as witness from P q,I ).
2. For every Π X operator in P q , there exists a corresponding Π H X operator in P q,I for some appropriately constructed H. Theorem 1.2 directly follows from the above lemma. To prove the bound on the running time, we use the fact that each operator (selection, projection or join) can be implemented in at most quadratic time in the size of the input (i.e., T (M N ) ≤ cM 2 T (N )). Additionally, notice that, if k is the vertex size of the inequality graph, then max H {φ(H)} ≤ k!k k . Hence, the running time can increase at most by a factor of 2 O(k log k) when inequalities are added to the query. In our running example, φ(H 0 ) = 1, φ(H 0 ) = 1 and φ(H 2 ) = 2, hence the resulting intermediate relations in will be at most 2e times larger than the ones in P q 0 .

Every intermediate relation
The following query with inequalities is an example where our algorithm gives much better running time than the color-coding-based or treewidth-based techniques described in the subsequent sections.
Let P be the SPJ plan that computes this acyclic query in time O(k|D|) by performing joins from left to right and projecting out the attributes as soon as they join. Then, the plan P I that is constructed has constant max H {φ(H)}; thus, (P k , I) can be evaluated in time O(k|D|) as well.
Remark 4.8. In this section we compared the running time of queries with inequalities with SPJ plans that compute the query without the inequalities. However, optimal algorithms that compute CQs may not use SPJ plans, as the recent worst-case optimal algorithms in [16,21] show. These algorithms apply to conjunctive queries without projections, where any inequality can be applied at the end without affecting the asymptotic running time. However, there are cases where nonstandard algorithms for Boolean CQs run faster than SPJ algorithms, e.g. q() = R(x 1 , x 2 ), R(x 2 , x 3 ), . . . , R(x 2k , x 1 ), can be computed in time O(N 2−1/k ), where N = |R|. We show in Appendix B that our techniques can be applied in this case as well. However, it is an open whether we can use them for any black-box algorithm.

Color-coding Technique and Generalization of Theorem 1.1
In this section, we will review the color-coding technique from [4] and use it to generalize Theorem 1.1 for arbitrary CQs with inequalities (i.e., not necessarily acyclic queries) 6 . First, we state the original randomized color-coding technique to describe the intuition: randomly color each value of the active domain by using a hash function h, use these colors to check the inequality constraints, and use the actual values to check the equality constraints.
For a CQ q, let q f denote the full query (without inequalities), where every variable in the body appears in the head of the query q. For a variable x i and a tuple t, t[x i ] (or simply t[i] where it is clear from the context) denotes the value of the attribute of t that corresponds to variable x i . Let t ∈ q f (D). We say that t satisfies the inequalities I, denoted by t |= I, if for each We say that t satisfies the inequalities I with respect to the hash function h, denoted by t |= h I, if for each such inequaity h(t[ Recall that k is the number of variables that appear in I. Let h be a perfectly random hash function h : Dom → [p] (where p ≥ k). For any t ∈ q f (D) if t satisfies I, then with high probability it also satisfies I with respect to h, i.e., Pr where we used the fact that 1 − x ≥ e −2x for x ≤ 1 2 . Therefore, by repeating the experiment 2 O(k) times we can evaluate a Boolean query with constant probability.
This process can be derandomized leading to a deterministic algorithm (for evaluating any CQ, not necessarily Boolean) by selecting h from a family F of k-perfect hash functions. A k-perfect family guarantees that for every tuple of arity at most k (with values from the domain Dom), there will be some h ∈ F such that for all i, ) (and thus if t |= I, then t |= h I) It is known (see [4]) that we can construct a k-perfect family of size |F| = 2 O(k) log(|Dom|) = 2 O(k) log |D|. 7 A coloring c of the vertices of the inequality graph G I with k colors is called a valid k-coloring, if for each x i = x j we have that c i = c j where c i denotes the color of variable Proof of Theorem 5.1. Suppose Then the union h∈F q (h) (D) produces the result of the query (this is because for any tuple t ∈ q f (D), there exists a hash function h ∈ F that satisfies all the inequalities in I). In the rest of this subsection, we will show how to compute q (h) (D) for a fixed hash function h : Dom → [p], p ≥ k, using the coloring technique in time bounded by 2 O(k log k) T (|q|, |D|). Let C be a valid p-coloring of the vertices of the inequality graph G I , such that whenever x i = x j , we have that c i = c j where c i denotes the color of variable x i under c. For each such coloring, we can define a subinstance D[C, h] ⊆ D such that for each relation R, In other words, the subinstance D[C, h] picks only the tuples that under the hash function h agree with the coloring C of the inequality graph. Proof. Let t ∈ q f (D) |= h I. Let C be the coloring such that for every x i ∈ V (G I ), we set c i = h(t[x i ]). We will show that C is a valid coloring of G I . Indeed, if x i = x j ∈ I, it must be that h(t[x i ]) = h([t j ]) (Since t |= h I) and hence c i = c j . Thus, we have that t[head(q)] ∈ q(D[C, h]).
For the other direction, let t ∈ q f (D[C, h]) for a valid coloring C. For any inequality x i = x j , we will have h(t[x i ]) = c i = c j = h(t[x j ]), and hence t |= h I.
The algorithm now iterates over all hash functions h ∈ F, and all valid colorings of G I with p colors, and for each combination computes q (D[c], h). The output result is: The running time is O(|F| · |C(G I )| · T (q, |D|)). As we discussed before |F| ≤ 2 O(p) log |D| and |C(G I )| ≤ k p . Theorem 5.1 follows by choosing p = k = |V (G I )| (the smallest possible value of p). the randomized color-coding technique is simple and elegant, but is unsuitable to implement in a database system that typically aims to find deterministic answers. On the other hand, apart from the additional log(|D|) factor, the derandomized color-coding technique demands the construction of a new k-perfect hash family for every database instance and query, and therefore may not be efficient for practical purposes. Our algorithm requires no preprocessing and can be applied in a database system by maintaining the same query plan and using a more sophisticated projection operation. More importantly, the color coding technique is oblivious of the combined structure of the query and the inequalities. As an example, consider the path query P k , together with the inequalities I 1 = {x i = x i+2 : i ∈ [k − 1]}. The color-coding-based algorithm has a running time of 2 O(k log k) |D| log |D|. However, as discussed in Section 4, we can compute this query in time O(k|D|), thus the exponential dependence on k is eliminated.

CQs and Inequalities with Polynomial Combined Complexity
In this section, we investigate classes of queries and inequalities that entail a poly-time combined complexity for (q, I) in terms of the treewidths of query graph G q , inequality graph G I , and augmented graph G q,I . If the augmented graph G q,I has bounded treewidth, then (q, I) can be answered in poly-time combined complexity [22,6]. We give examples of such q and I below: Example 6.1. Consider the path query: x 3 ), . . . , R k (x k , x k+1 ), which is acyclic, and consider the following inequality patterns (see Figure 5): } has treewidth 2. A tree-decomposition with treewidth 2 (i.e., maximum node in the tree has size 3) is ]} has treewidth 3 (k is odd): The incidence graph without the edge (x k 2 , x k 2 + 1) has the structure of a k 2 × 2 grid and therefore has treewidth 2 (see Figure 5(b)). We can simply add the node x k 2 to all nodes in this tree-decomposition to have a decomposition with treewidth 3.

(P
2 ]} has treewidth 2 (k is odd): A tree-decomposition with treewidth 3 can be obtained by going back and forth along the inequality (dotted) edges: For example, in Figure 5(c) the decomposition can be {x 2 , However, for certain inputs our algorithm in Section 4 can outperform the treewidth-based techniques since it considers the inequality structure more carefully. For instance, even though the augmented graph of (P k , I 1 ) has treewidth 2 (see Figure 5 (a)), the techniques of [22] will give an algorithm with running time O(poly(k)|D| 2 ), whereas the algorithm in Section 4 gives a running time of O(k|D|).
Indeed, the treewidth of G q,I is at least as large as the treewidth of G q and G I . As mentioned earlier, when G I is the complete graph on k + 1 variables (with treewidth = k + 1), answering (P k , I) is as hard as finding if a graph on k + 1 vertices has a Hamiltonian path, and therefore is NP-hard in k. Interestingly, even when both G q and G I have bounded treewidths, G q,I may have unbounded treewidth as illustrated by the following example: Example 6.2. Consider (P k , I 4 ) (see Figure 5(d)), where k + 1 = p 2 for some p. Algebraically, we can write I 4 as: The edges for P k are depicted in the figure as an alternating path on the grid with solid edges, whereas the remaining edges are dotted and correspond to the inequalities. Here both G P k and G I 4 have treewidth 1, but G P k ,I 4 has treewidth Θ( √ k).
However, this does not show that evaluation of the query (P k , I 4 ) is NP-hard in k, which we prove below by a reduction from the list coloring problem: The list coloring problem generalizes the coloring problem, and therefore is NP-hard. List coloring is NP-hard even on grid graphs with 4 colors and where 2 ≤ |L(v)| ≤ 3 for each vertex v [7]; we show NP-hardness for (P k , I 4 ) by a reduction from list coloring on grids.
Proposition 6.4. The combined complexity of evaluating (P k , I 4 ) is NP-hard, where both the query P k and the inequality graph G are acyclic (have treewidth 1).
Proof. We reduce from list coloring on grid graphs, which is known to be NP-complete with c = 4 colors and where 2 ≤ |L(v)| ≤ 3 for each vertex v [7].
Given an instance of the list coloring problem where the graph G is a p × p grid-graph, we create an instance of P k , I 4 as shown in Figure 5(d), where k + 1 = p 2 . We denote by x i both the vertices in G as welll as the variables in P k . For each i ∈ [k], we create an instance The inequalities I 4 are as shown in the figure: x i = x j . Note that each vertex v in the grid graph G appears in one of the relations so its domain in the query is bounded by L(v).
Suppose the list coloring instance has a valid coloring, i.e., every vertex v in G can be colored . This gives an yes-instance to the query (P k , I 4 ). Similarly, if the query has a yes instance, that corresponds to a yes-instance of the list coloring problem.
In fact, the above proposition can be generalized as follows: if the graph G q,I is NP-hard for list coloring for a query q where each relation has arity 2, then evaluation of the query (q, I) is also NP-hard in the size of the query.
On the contrary, (q, I) may not be hard in terms of combined complexity if the treewidth of G q,I is unbounded, which we also show with the help of the list coloring problem. Consider the queries F k ( ) = R 1 (x 1 ), R 2 (x 2 ), . . . , R k (x k ). Given inequalities I, the evaluation of (F k , I) is equivalent to the list coloring problem on the graph G I when the available colors for each vertex x i are the tuples in R i (x i ). Since list coloring is NP-hard: Proposition 6.5. The evaluation of (F k , I) is NP-hard in k for arbitrary inequalities I.
Therefore, answering (F k , I) becomes NP-hard in k even for this simple class of queries if we allow arbitrary set of inequalities I (this also follows from Theorem 7.2). However, list coloring can be solved in polynomial time for certain graphs G I : • Trees (the problem can be solved in time O(|V |) independent of the available colors [13]), and in general graphs of constant treewidth. • Complete graphs (by a reduction to bipartite matching). 8 In general, if the connected components of G are either complete graphs or have constant treewidth, list coloring can be solved in polynomial time. Therefore, on such graphs as G I , the query (F k , I) can be computed in poly-time in k and |D|. Here we point out that none of the other algorithms given in this paper can give a poly-time algorithm in k, |D| for (F k , I) when G I is the complete graph (and therefore has treewidth k). The following proposition generalizes this property: Proposition 6.6. Let q be a Boolean CQ, where each relational atom has arity at most 2. If q has a vertex cover (a set of variables that can cover all relations in q) of constant size and the list coloring problem on G I can be solved in poly-time, then (q, I) can be answered in poly-time combined complexity.
Proof. Let X = {x i 1 , . . . , x ic } be the vertices of the vertex cover. Consider each possible instantiation of these variables from the domain Dom; the number of such instantiation is |Dom| c . For each such instantiation α consider the updated query q α . Since X is a vertex cover and each relation has arity ≤ 2, in q α each relation has at most one free variable. Relations with single variable that has been instantiated to a unique constant from α or relations where both the variables have been instantiated can be evaluated by a linear scan of the instance and removed thereafter. Similarly, relations with arity 2 where exactly one of the two variables has been instantiated to a constant can be evaluated by removing tuples from the instance that are not consistent with this constant. These steps can be done in poly-time in combined complexity. In the reduced query, each relation has exactly one free variable and therefore is equivalent to F n for some n (n = the number of relations in the query where exactly one variable belongs to X). Hence if list coloring can be solved in poly-time on G I , (q α , I) for each instantiation α, and therefore (q, I) can be solved in poly-time in combined complexity.
To see an example, consider the star query Z n ( ) = R 1 (y, x 1 ), . . . , R n (y, x n ) which has a vertex cover {y} of size 1. We iterate over all possible values of y: for each such value α ∈ Dom, the query R 1 (α, x 1 ), . . . , R n (α, x n ) is equivalent to F n , and therefore (Z n , I) can be evaluated in poly-time in combined complexity when G I is an easy instance of list coloring.

CQs with Polynomial Combined Complexity for All Inequalities
This section aims to find CQs q such that computing (q, I) has poly-time combined complexity, no matter what the choice of I is. Here we present a sufficient condition for this, and a stronger necessary condition.
A fractional edge cover of a CQ q assigns a number v R to each relation R ∈ q such that for each variable x, R:x∈vars(R) v R ≥ 1. A fractional vertex packing (or, independent set) of q assigns a number u x to each variable x, such that x∈vars(R) u x ≤ 1 for every relation R ∈ q. By duality, the minimum fractional edge cover is equal to the maximum fractional vertex packing. When each v R ∈ {0, 1} we get an integer edge cover, and when each u x ∈ {0, 1} we get an integer vertex packing.
Definition 7.1. A family Q of Boolean CQs has unbounded fractional (resp. integer) vertex packing if there exists a function T (n) such that for every integer n > 0 it can output in time poly(n) a query q ∈ Q that has a fractional (resp. integer) vertex packing of size at least n (counting relational atoms as well as variables).
A family Q of Boolean CQs has bounded fractional (resp. integer) vertex packing if there exists a constant b > 0 such that for any q ∈ Q, the size of any fractional (resp. integer) vertex packing is ≤ b.
The main theorem of this section is stated below: Theorem 7.2. The following hold: 1. If a family of Boolean CQs Q has unbounded integer vertex packing, the combined complexity of (q, I) for q ∈ Q is NP-hard.
2. If a family of CQs Q has bounded fractional vertex packing, then for each q ∈ Q, (q, I) can be evaluated in poly-time combined complexity for any I.
The NP-hardness in this theorem follows by a reduction from 3-Coloring, whereas the polytime algorithm uses the bound given by Atserias-Grohe-Marx [12,5] in terms of the size of minimum fractional edge cover of the query, and the duality between minimum fractional edge cover and maximum fractional vertex packing.
Proof. 1. NP-hardness. We do a reduction from 3-Coloring. Let G = (V, E) be an undirected graph, where n = |V |. The goal is to color G with 3 colors such that no two adjacent vertices have the same color. Given the family Q with unbounded integer vertex packing, we construct a query with inequality (q, I) where q ∈ Q, and an instance D such that G admits a 3-coloring if and only if (q, I) is true on the instance D.
Fine in polynomial time the query q ∈ Q such that q has an integer vertex packing X of size n. Let X = {x 1 , . . . , x n } be the variables in the vertex packing. We create an instance D as follows. Note that each relation R contains at most one variable x i from X. If R contains no such variable, then R D = {(0, 0, . . . , 0)} (a single tuple with value 0 for all variables). Otherwise, let x i ∈ vars(R) and without loss of generality (wlog.), let x i be at the first position of R; then, R D = {(c, 0, 0, . . . ) | c = 1, 2, 3}. Observe that the size of the instance D is at most 3 · |q| and it is constructed in poly-time.
By construction the answer to the full query q f of q is q f (D) = {1, 2, 3} n × {(0, 0, . . . )}, where wlog. all x 1 , . . . , x n appear at the first n positions of the head of q f . Therefore, each variable x i , i = 1, . . . , n can obtain each color independent of the other variables. Finally, we construct a one-to-one mapping from each vertex v ∈ V to a unique variable x v ∈ {x 1 , . . . , x n }, and define Now it is easy to verify that G has a valid 3-coloring if and only if (q, I) is true on D.
2. Algorithm for queries with bounded fractional vertex packing. Since the maximum fractional vertex packing of any q ∈ Q is ≤ b, the minimum fractional edge cover is also ≤ b by duality. Thus from [12,5], q f (D) can be evaluated in poly-time in combined complexity (in time O(|q| 2 |D| b+1 )). Further, |q f (D)| ≤ |D| b [12,5]. We first compute q f (D), then for each tuple in q f (D) we check whether it satisfies the inequalities, and finally apply the projection to get the answers to (q, I) in poly-time in combined complexity.
In this paper, we illustrate the properties with examples. Consider the family S k ( ) = R(x 1 , . . . , x k ) for k ≥ 1: this has vertex packing of size = 1 and therefore can be answered trivially in poly-time in combined complexity for any inequality pattern I. On the other hand, the class of path queries P k mentioned earlier has unbounded vertex packing (has a vertex packing of size ≈ k 2 ), and therefore for certain set of inequalities (e.g., when G I is a complete graph), the query evaluation of (P k , I) is NP-hard in k. Similarly, the class F k ( ) = R 1 (x 1 ), R 2 (x 2 ), . . . , R k (x k ) mentioned earlier has unbounded vertex packing, and is NP-hard in k with certain inequality patterns (see Proposition 6.5).
Theorem 7.2 is not a dichotomy or a characterization of easy CQs w.r.t. inequalities, since there is a gap between the maximum fractional and integer vertex packing. 9

Conclusion
We studied the complexity of CQs with inequalities and compared the complexity of query answering with and without the inequality constraints. Several questions remain open: Is there a property that gives a dichotomy of query evaluation with inequalities both for the class of CQs, and for the class of CQs along with the inequality graphs? What can be said about unions of conjunctive queries (UCQ) and recursive datalog programs? Can our techniques be used as a black-box to extend any algorithm for CQs, i.e., not necessarily based on SPJ query plans, to evaluate CQs with inequalities?

A Comparison of Our Techniques in Section 3 with Other Related Work
Monien in [15] defines the notion of q-representatives for families of sets. Given a family of sets F , where each set has p elements,F ⊆ F is a q-representative if for every set T of size q, there exists some set U ∈ F with U ∩ T = ∅ if and only if there exists a setÛ ∈F such thatÛ ∩ T = ∅.
Observe that a q-representative is a special case of an H-equivalent relation: indeed, we can model the family F as a relation R F of arity p (where we do not care about the order of the attributes), and define H as the full bipartite graph with edge set [p] × [q]. Then, if we write E H (R F ) back to a family of sets, it is a q-representative of F . Our techniques also generalize the notion of minimum samples presented in [8], which corresponds to H-forbidden tuples of a relation in the case where H = (X, Y, E) has |X| = |Y | and E(H) forms a perfect matching between X and Y . Several of the definitions and algorithmic ideas were inspired by both [8,15].

B Computing Cycles with non-SPJ plans
Let R be a binary relation, and define for any k ≥ 1 the query: One can think intuitively that R represents the edges of a directed graph G. Proof. Let δ be some threshold parameter. We say that a value a is a heavy hitter if the degree |σ X=a R(X, Y )| ≥ δ, otherwise it is light. The algorithm distinguishes two cases.
First, we compute all the 2k-cycles that contain some heavy hitter value. We have at most N/δ such values. For each such value, we can compute C (a) 2k () = R(a, x 2 ), R(x 2 , x 3 ), . . . , R(x 2k , a) Observe that this is an acyclic query now that a is a fixed value, so we can compute this query in time O(kN ). Hence, to compute all possible cycles in this case we need O(kN 2 /δ) time.
Second, we compute whether there exists a cycle C 2k that uses only light values. To do this, let R be the subset of R that contains only the light values. The maximum degree is δ, so the queries q 1 (x 1 , x k ) = R(x 1 , x 2 ), . . . , R(x k−1 , x k ) q 2 (x k , x 1 ) = R(x k , x k+1 ), . . . , R(x 2k , x 1 ) each contain at most N δ k−1 answers, which we can compute in time O(kN δ k−1 ) by performing consecutive joins. However, |q 1 |, |q 2 | have size at most N , and we can compute their intersection in time O (N log N ). So the total running time for this case is O(N δ k−1 ).
To balance the two cases, we must have N 2 /δ = N δ k−1 or δ = N 1/k .
We can combine the above algorithm with our technique as follows. Suppose the query now is (C 2k , I) for some set of inequalities I. Observe that the first case is easy to handle, since we know how to compute C (a) 2k in time O(kN × max H (φ(H)) 2 ), where φ(H) depends only on the inequality structure. For the second case, instead of computing q 1 , q 2 , we consider the full queries q f 1 (x 1 , x 2 , . . . , x k ) = R(x 1 , x 2 ), . . . , R(x k−1 , x k ) q f 2 (x k , . . . , x 2k , x 1 ) = R(x k , x k+1 ), . . . , R(x 2k , x 1 ) and compute (q f 1 , I 1 ), (q f 2 , I 2 ), where I 1 are the inequalities defined only between the variables of x 1 (and similarly for I 2 ). Since these queries have size at most N δ k−1 , we can compute the full answers and apply the inequalities at the end. To compute the intersection between q 1 , q 2 , let I 12 = I \ (I 1 ∪ I 2 ). We then compute Π H 1 x 1 ,x k (q f 1 ), where H 1 = ({x 2 , . . . , x k−1 }, {x k+1 , . . . , x 2k }, I 12 ), and similarly Π H 2 x 1 ,x k (q f 2 ) with a symmetrically defined H 2 . The resulting projections have size at most N · φ(H i ) for i = 1, 2, so we can then compute their intersection in time O(N log N ) and then apply the inequalities in I 12 .