Rewriting with Acyclic Queries: Mind Your Head

The paper studies the rewriting problem, that is, the decision problem whether, for a given conjunctive query $Q$ and a set $\mathcal{V}$ of views, there is a conjunctive query $Q'$ over $\mathcal{V}$ that is equivalent to $Q$, for cases where the query, the views, and/or the desired rewriting are acyclic or even more restricted. It shows that, if $Q$ itself is acyclic, an acyclic rewriting exists if there is any rewriting. An analogous statement also holds for free-connex acyclic, hierarchical, and q-hierarchical queries. Regarding the complexity of the rewriting problem, the paper identifies a border between tractable and (presumably) intractable variants of the rewriting problem: for schemas of bounded arity, the acyclic rewriting problem is NP-hard, even if both $Q$ and the views in $\mathcal{V}$ are acyclic or hierarchical. However, it becomes tractable if the views are free-connex acyclic (i.e., in a nutshell, their body is (i) acyclic and (ii) remains acyclic if their head is added as an additional atom).


Introduction
The query rewriting problem asks, for a query Q and a set V of views, whether there is a query Q ′ over V that is equivalent to Q, and to find such a rewriting Q ′ .
We emphasise that in the literature, various notions of rewriting are studied and that the one above is sometimes called exact [CGLV05] or equivalent [AC19].Since we are in this paper exclusively interested in exact rewritings, we use the term rewriting in the sense stated above.
Over the last decades, query rewriting has been studied in a wide range of settings with varying database models (relational, XML, graph, . . .), query languages, semantics (set, bag, . . . ) and more.A particular well-known setting is formed by conjunctive queries (CQ) under set semantics, which captures some core aspects of SQL queries.This is also the setting studied in this paper.We refer to [AC19, CY12,Hal01] for an overview of the extensive literature.
In this paper we are interested in a modified version of the query rewriting problem which does not merely ask for any rewriting but rather for rewritings that are structurally simple and allow for efficient evaluation.This is relevant in a setting where access to the database is only possible through the views, in which case the use of an efficient rewriting is obvious.It can also be interesting in a scenario where multiple queries have to be evaluated: by evaluating some of the queries and considering their results as views, some other queries might be rewritable into acyclic queries over the database extended by these views, and therefore they could possibly be evaluated more efficiently.
The forms of structural simplicity that we consider in this paper are acyclic (ACQ) and hierarchical queries (HCQ), and their slightly stronger versions free-connex acyclic (CCQ) and q-hierarchical queries (QHCQ).For a brief discussion of the origin and applications of these classes, we refer to Section 8.
We are interested in two kinds of questions.
(1) Under which circumstances is it guaranteed that a structurally simple rewriting exists, if there exists a rewriting at all? (2) What is the complexity to decide whether such a rewriting exists and to compute one?
We study these questions depending on the structure of the given views and the given query and we consider the same simplicity notions.
In particular, we study the decision problem Rewr(V, Q, R) that asks whether for a given set of views from V and a query from Q, there is a rewriting from R, for various classes V, Q and R, with an emphasis on the case where R is ACQ or a subclass.In the following we refer to this case as the acyclic rewriting problem.To the best of our knowledge, there • If the query is acyclic, the views are free-connex acyclic, and the arity of the database schema is bounded by some fixed k, then the acyclic rewriting problem is tractable, that is, for every k: Rewr k (CCQ, ACQ, ACQ) is in polynomial time (Theorem 6.2).Many results use a characterisation of rewritability, based on the notion of cover partitions, that might be interesting in its own right.Similar notions have been used in earlier work, but ours is particularly suited for the study of exact (equivalent) rewritings.
Our paper is organised as follows: we introduce general basic notions in Section 2 and notions and observations related to rewritings in Section 3. The characterisation of rewritability in terms of cover partitions is given in Section 4. The answer to Question (1) and the complexity of the acyclic rewriting problem for acyclic queries and views are studied in Section 5.The feasibility for free-connex acyclic views (and an additional class) is presented in Section 6. Section 7 shows that the answer to Question (1) is the same for hierarchical and q-hierarchical queries and views.Section 8 discusses related work and, in particular, the relationship of our characterisation of rewritability with results in the literature.Section 9 concludes.

Preliminaries
In this section, we fix some notation and recall the basic concepts from relational databases that are relevant for this paper.Let N 0 denote the set of non-negative integers.Let dom and var be countably infinite sets of data values (also called constants) and variables, respectively.We use the natural extensions of mappings of variables onto tuples and sets without notational distinction.That is, for a mapping f : X → Y , we write f (X ′ ) for {f (x) | Databases.Databases and queries are formulated over database schemas.A database schema S is a finite set of relation schemas, each represented by a symbol R and associated with a fixed arity ar(R) ∈ N 0 . 1 A fact or R-fact R(a 1 , . . ., a r ) comprises a relation symbol R with some arity r and data values a 1 , . . ., a r .A database D over schema S is a finite set of R-facts for R ∈ S.
Queries.In general, queries map databases to relations.In this paper, we consider conjunctive queries and restrictions of them.Conjunctive queries are queries that can be expressed syntactically as conjunctions of relation atoms as follows.
An atom is of the form R(x 1 , . . ., x r ) with a relation symbol R with arity r, and variable set {x 1 , . . ., x r }.We denote the variable set of an atom A by vars(A).Analogously to facts, an atom with relation symbol R is called an R-atom if we want to stress the associated relation (symbol).More generally, S-atoms are R-atoms for some symbol R in schema S.
We represent a conjunctive query (CQ for short) over schema S as a rule of the form A ← A 1 , . . ., A m whose body {A 1 , . . ., A m } consists of a positive number of atoms and whose head is a single atom A such that the following two conditions are satisfied.First, atoms A 1 , . . ., A m refer to relation symbols from S and atom A, on the contrary, does not.Second, the query is safe, that is, every variable in the head occurs in at least one atom of the body.Let head(Q) and body(Q) denote the head and body of a query Q, respectively.Variables that occur in the head of a query are called head variables; all other variables are called quantified variables.A query without quantified variables is called a full query.Queries with two or more body atoms that refer to the same relation are said to have self-joins.The arity of a query Q is the arity of its head atom.Furthermore, a query Q is a Boolean query if the arity of head(Q) is 0.
Like for sets and tuples, we also use the natural extension of mappings of variables on atoms without difference in notation: for a mapping f : var → Y , let f (A) denote R(f (x 1 ), . . ., f (x r )) for an atom A = R(x 1 , . . ., x r ).
A valuation is a mapping ϑ : var ⇀ dom.A database D satisfies a set A of atoms under a valuation ϑ, if ϑ(A) ⊆ D, that is for every atom R(x 1 , . . ., x r ) in A the fact R(ϑ(x 1 ), . . ., ϑ(x r )) is contained in D. The result of query Q on database D is defined as Relationships between queries.Queries over the same schema can be compared with respect to the results they define.Let Q 1 and Q 2 be queries.We say that It is well-known that a conjunctive query Q 1 is contained in a conjunctive query Q 2 if and only if there is a homomorphism from the latter query into the first [CM77].Such a homomorphism is a mapping h : vars(Q 2 ) → vars(Q 1 ) such that (1) h(body(Q 2 )) ⊆ body(Q 1 ) and (2) h(head(Q 2 )) = head(Q 1 ) hold.We call h a body homomorphism if it fulfils Condition (1).
Structurally simple queries.Despite their simplicity and restricted expressibility, several interesting problems are intractable for conjunctive queries in general.Therefore, different fragments have been studied in the literature.In this paper, we are particularly concerned with "acyclic" queries, which allow, for instance, evaluation in polynomial time.We also consider three subclasses of acyclic queries, namely, free-connex acyclic, hierarchical, and q-hierarchical queries.
Acyclic and free-connex acyclic queries.A join tree for a query Q is a tree whose vertices are the atoms in the query's body that satisfies the following path property: for two atoms A, A ′ ∈ body(Q) with a common variable x, all atoms on the path from A to A ′ contain x.A query Q is acyclic if it has a join tree.It is free-connex acyclic if Q is acyclic and the Boolean query whose body is body(Q) ∪ {head(Q)} is acyclic as well [BDG07,BB13].
Hierarchical queries.For a fixed query Q and some variable x in Q, let atoms(x) denote the set of atoms in body(Q) in which x appears.
for all variables x, y in Q, one of the following conditions is satisfied.
A conjunctive query Q is q-hierarchical if it is hierarchical and for all variables x, y ∈ vars(Q) the following is satisfied: if atoms(x) ⊊ atoms(y) holds and x is in the head of Q, then y is also in the head of Q.
For brevity, we denote by CQ, ACQ, CCQ, HCQ, and QHCQ the classes of conjunctive queries in general and those conjunctive queries that are acyclic, free-connex acyclic, hierarchical or q-hierarchical, respectively.We note that each q-hierarchical query is free-connex acyclic [IUV17, Proposition 4.25] and each hierarchical query is acyclic.2Views and rewritings.A view V over a schema S is just a query over schema S.
In this paper, we consider only views that are conjunctive queries.We furthermore assume that every view defines its own relation in the derived database.We denote the set of relation schemas induced by the heads of the views in V by S V .Definition 3.1.Let V, Q, and R be classes of conjunctive queries.The rewriting problem for V, Q, and R, denoted Rewr(V, Q, R) asks, upon input of a query Q ∈ Q and a (finite) set V ⊆ V of views, whether there is a V-rewriting of Q in the class R. We write Rewr k (V, Q, R) for the restriction, where the arity of the database schema is bounded by k.
In general, the rewriting problem for conjunctive queries is NP-complete.
There is a straightforward, albeit in general inefficient, way of finding a rewriting for a conjunctive query and views.In fact, it can be shown that, for given Q and V, a rewriting Q ′ over V exists, if and only if a certain canonical query canon(Q, V) exists and is a rewriting.This canonical candidate canon(Q, V) can be obtained by evaluating V on the canonical database canon(Q), which in turn is defined as the set of body atoms of Q viewed as facts, in which the variables are considered as fresh constants.The canonical candidate has the same head as Q and its body is just V(canon(Q)). 4If the canonical candidate turns out to be a rewriting, then we often call it canonical rewriting.
An important detail should be mentioned here: the canonical candidate does not always exist, e.g., if V(canon(Q)) does not contain all head variables of Q or it is outright empty.In that case, there is no rewriting.For conjunctive queries without self-joins this had been shown in [CR00, Lemma 7].
Example 3.4.Let us consider the query Q defined by and the views Evaluating the views V 1 and V 2 on the canonical database canon(Q) of Q yields the result {V 1 (x, y, z), V 2 (x, y, z, x)}.Thus, the query Q ′ defined by is the canonical candidate, which happens to be an actual V-rewriting.
In this example it is easy to see that the canonical candidate is a V-rewriting, without considering any (let alone all) databases.In fact, in this particular case, this can be established with the help of an expansion of the rewriting.Before we define expansions, we need one more definition.Definition 3.5 (View Application).An application of a view V is a substitution α : vars(V ) → var that does not unify any quantified variable of V with another variable of V .This definition reflects the fact that a rewriting can access a view only through its head and therefore cannot unify any quantified variables.
We say that a collection α 1 , . . ., α m of applications for views V 1 , . . ., V m fulfils quantified variable disjointness, if for all i, j ∈ {1, . . ., m}, each quantified variable x of V i , and each variable y of V j with i ̸ = j, it holds α i (x) ̸ = α j (y).That is, beyond what is already required by the definition of an application, a view application never maps a quantified variable of a view to a variable in the range of any other view application.This ensures that, for all i, j ∈ {1, . . ., m} with i ̸ = j, the bodies α i (body(V i )) and α j (body(V j )) only share variables from vars(α i (head(V i ))) ∩ vars(α j (head(V j ))).
An expansion of a V-rewriting Q ′ is, intuitively, obtained by inlining the bodies of the views from V in Q ′ .Definition 3.6 (Expansion).Let V be a set of views over a schema S and let Q ′ be a query with body(Q ′ ) = {A ′ 1 , . . ., A ′ m } over the schema S V .Furthermore, let, for each i ∈ {1, . . ., m}, V i be the view in V and α i be an application for V i such that A ′ i = α i (head(V i )), such that α 1 , . . ., α m fulfil quantified variable disjointness.
The expansion of Q ′ w.r.t.V and α 1 , . . ., α m is the query that has the same head as Q ′ and body m i=1 α i (body(V i )).Since the applications α 1 , . . ., α m in Definition 3.6 are uniquely determined up to renaming of quantified variables, we usually do not mention them explicitly and just speak of an expansion of a query Q ′ w.r.t.V.
Furthermore, because an expansion Q ′′ of a V-rewriting Q ′ is, intuitively, obtained by inlining the bodies of the views, the expansion Q ′′ is "equivalent" to Q ′ in the following sense.
Lemma 3.7.Let V be a set of views, Q ′ be a query over the schema S V , and Q ′′ be an expansion of Q ′ w.r.t.V. Then Q ′′ (D) = Q ′ (V(D)) holds for every database D. Lemma 3.7 has been proved, e.g., as part of the proof of [AC19, Theorem 3.5].For the sake of convenience, we provide a short proof using our notation in the following.
Proof of Lemma 3.7.Following Definition 3.6, let {A ′ 1 , . . ., A ′ m } be the body of Q ′ , and let α 1 , . . ., α m be applications fulfilling quantified variable disjointness, and V 1 , . . ., V m ∈ V views such that A ′ i = α i (head(V i )) holds for all i ∈ {1, . . ., m} and body We first show the inclusion Let D be a database and ϑ be a valuation such that D satisfies ϑ(body(Q ′′ )) = ϑ( m i=1 α i (body(V i ))) under ϑ.By the definition of an expansion, ϑ(head(Q ′′ )) = ϑ(head(Q ′ )) holds.Thus, it suffices to show that ϑ(body Since ϑ(body(Q ′′ ) ⊆ D, it holds ϑ(α i (body(V i ))) ⊆ D, for all i ∈ {1, . . ., m}.Interpreting ϑ • α i as a valuation, it follows that ϑ(α i (head i for all i ∈ {1, . . ., m} and, therefore, we can conclude that ϑ(body For the inclusion holds, for all i ∈ {1, . . ., m}.The latter implies that there are valuations µ i such that ϑ(α i (head That is, the µ i map body(V i ) into D and agree with ϑ • α i on all head variables of V i .Moreover, since the application α i does not unify any quantified variables with other variables, the valuation ϑ can be extended to a valuation ϑ + i such that ϑ + i (α i (body(V i ))) ⊆ D. Lastly, thanks to the quantified variable disjointness of the α i , the extended mappings ϑ + i can be combined into a valuation ϑ + which maps all α i (body(V i )) into D.
We further note that the expansion of a rewriting Q ′ can be directly compared with a query Q since it is over the same schema.In fact, we will frequently use the following result which follows readily from Lemma 3.7.Proposition 3.8 [AC19, Theorem 3.5].Let Q be a query over a schema S, V be a set of views over S, Q ′ be a query over the schema S V , and Q ′′ be an expansion of In Example 3.4, the query Q ′ is a V-rewriting of Q, because the views are full queries, and, thus, there is only one (unique) expansion of Q ′ w.r.t.V which even coincides with Q.
If there is no a priori bound on the arity of views, then the canonical candidate can be of exponential size in |Q| + |V|.However, if there is a rewriting, then there always is one of polynomial size.In fact, at most n view applications are needed if the body of query Q contains n atoms [LMSS95, Lemma 3.5].
Evaluating the views on the canonical database canon(Q) yields the result This result, and thus, the body of the canonical candidate, has exponential size.There is, however, a simple {V 1 , V 2 }-rewriting which has as many view atoms as Q has atoms, namely, H() ← V 1 (x, y), V 2 (x, . . ., x), V 2 (y, . . ., y).
Therefore, an NP-algorithm for Rewr(CQ, CQ, CQ) can "guess" a rewriting of polynomial size and test whether it yields an expansion that is equivalent to the given query.Since equivalence of CQ can be tested in NP, this indeed shows that Rewr(CQ, CQ, CQ) is in NP.
Due to their potentially exponential size, canonical candidates are, in general, not useful as a starting point for polynomial time algorithms.However, in some cases their size is only polynomial, in particular, if the views have bounded arity.In such cases, the above characterisation can sometimes be used to obtain efficient algorithms.In particular, this holds for acyclic views of bounded arity and acyclic queries.For a class V of views, we write V k for the restriction to views of arity at most k.
Proof.We first observe that, if it exists, the canonical candidate always obeys Q ⊑ canon(Q, V) • V. Indeed, a homomorphism from an expansion of canon(Q, V) to Q can be obtained by combining the valuations that yield V(canon(Q)) from canon(Q) as follows.
To improve readability and avoid technical clutter we identify the variables in Q with their corresponding constants in canon(Q) in the following.Notably, this means that we identify canon(Q) with body(Q), i.e. we write canon(Q) = body(Q).
Let A ′ 1 , . . ., A ′ m be the atoms of canon(Q, V) and, for each i ∈ {1, ..., m}, let α i be an application and V i be a view such that A ′ i = α i (head(V i )), such that α 1 , . . ., α m fulfil quantified variables disjointness.Furthermore, since the A ′ i are also contained in V(canon(Q)) by definition of canon(Q, V), there are valuations ϑ i such that A ′ i = ϑ i (head(V i )) and ϑ i (body(V i )) ⊆ canon(Q) holds.In particular, we have that ϑ i (head(V i )) = α i (head(V i )) for all i ∈ {1, . . ., m}.
Let y be a variable occuring in α i (body(V i )) and x such that y = α i (x) for some i ∈ {1, . . ., m}.Then the desired homomorphism h maps y to ϑ i (x).Note that h is the identity on variables occuring in α i (head and, thus, y = α i (x) = ϑ i (x) = h(y).Hence, h is well-defined, since, if a variable y occurs in α i (body(V i )) and α j (body(V j )) with i ̸ = j then it also occurs in α i (head(V i )) and α j (head(V j )) thanks to quantified variable disjointness.
It is easy to see that h is a homomorphism from the expansion of canon holds for all i ∈ {1, . . ., m} and h is the identity on all variables occuring in the head of canon(Q, V).Therefore, to test for rewritability, it suffices to check whether canon Since the views are acyclic and their arity is bounded by a constant, the polynomial-sized canonical candidate canon(Q, V) can be computed in polynomial time. 5It then suffices to test whether a homomorphism from Q to an expansion of canon(Q, V) exists.This test can be done in polynomial time because the query Q is acyclic.
Thus, overall, it can be tested efficiently whether canon(Q, V) • V ⊑ Q holds if the query and views are acyclic and the arity of the views is bounded by a constant.
Hence, the statement follows due to Proposition 3.3.
Chekuri and Rajaraman have shown that the rewriting problem is in P for acyclic queries without self-joins and arbitrary views using a similar idea [CR00, Theorem 5].
However, even for Boolean views and databases over a small fixed schema, it does not suffice to restrict only the query or only the views to acyclic queries.
Proposition 3.11.Rewr k (CQ 0 , ACQ, CQ) and Rewr k (ACQ 0 , CQ, CQ) are NP-complete, for every k ≥ 3.This even holds for Boolean queries, view sets with only one Boolean view and a schema with two relations of maximum arity 3.
Proof.The upper bound holds thanks to Theorem 3.2.For the lower bound, we first show, by a reduction from the three-colouring problem, that it is NP-hard to test, for an acyclic Boolean conjunctive query Q 1 and a Boolean conjunctive query Q 2 , whether To this end, let G be an undirected graph without isolated vertices and self-loops (i.e.edges of the form (v, v)).With each vertex v of G we associate a variable x v .We define Q 2 to be the Boolean conjunctive query with head H() that consists of all atoms E(x v , x w ) such that (v, w) is an edge in G.The Boolean query Q 1 is defined by the rule where b, r, y are variables representing the colours blue, red, and yellow.Observe that Q 1 is acyclic, because all variables are contained in the "cover-atom" C(b, r, y).
Towards the correctness of the reduction, if Q 1 ⊑ Q 2 holds, there is a homomorphism h from Q 2 to Q 1 .This homomorphism represents a valid colouring, since two variables x v and x w cannot be mapped to the same variable in Q 1 if there is an edge (v, w) in G. Conversely, every valid colouring of G gives rise to a homomorphism from Q 2 to Q 1 and therefore witnesses Finally, we reduce the equivalence problem with acyclic Q 1 to the rewriting problem.Let thus Q 1 and Q 2 be Boolean conjunctive queries.The input instance (Q 1 , Q 2 ) for the equivalence problem is mapped to an input instance for the rewriting problem by assigning Q 1 the role of the query and Q 2 the role of (the sole) view.
The canonical rewriting, if it exists, consists only of the single atom This reduction establishes that Rewr 3 (CQ 0 , ACQ, CQ) is NP-hard.For the NP-hardness of Rewr 3 (ACQ 0 , CQ, CQ), the roles of Q 1 and Q 2 are simply swapped in the last reduction.
By composing the three reductions, the proposition is established.

A Characterisation
In this section we give a characterisation of rewritability of a query Q with respect to a set V of views.It is in terms of a partition of the atoms of body(Q), where each set of the partition can be matched with a view in a specific way.We refer to such partitions as cover partitions and the matches as cover descriptions.The characterisation is very similar to other such characterisations in the literature [GKC06, AC19], in particular to "MiniCon Descriptions" [PH01].However, in its specific form and notation it is tailored for our needs in the subsequent sections.We will discuss the relationship of our characterisation with others further below in Section 8.
Next we define the notions of cover descriptions and cover partitions.Let Q be a conjunctive query.For a set A ⊆ body(Q) we define bvars Q (A) as the set of bridge variables of A, that occur in A as well as in the head of Q or in some atom of Q not in A. More formally, bvars Example 4.1.Consider the query Q defined by H(x, y, z) ← R(x, u), S(u, y, w), T (y, w, z).For the set A = {R(x, u), S(u, y, w)} of atoms from the body of Q, we have bvars Q (A) = {x, y, w} because x and y are head variables of Q and because w, and also y, occurs in the atom T (y, w, z) that does not belong to A. The variable u in A is not a bridge variable since it is quantified and does not occur in any atom outside of A.

Definition 4.2 (Cover Description
(3) ψ is a body homomorphism from α(V ) to Q, and (4) ψ is the identity on vars(A).
Intuitively, the conditions of Definition 4.2 testify that the atoms of Q in A can be "represented" or "covered" by a V -atom in a rewriting of Q-giving rise to the term cover description: Condition (1) means that every atom in A has a matching atom in V .Condition (3) ensures that every database which satisfies the query also satisfies the view (under application α).Conditions (2) and (4) establish an "interface" to combine multiple cover descriptions in a straightforward and compatible manner such that, overall, a rewriting is described.
Example 4.3.Consider the query Q given by the rule and the following views.
Although ψ 1 could be chosen as id (by adapting α 1 accordingly), we will see in Example 4.5, that this is not always desirable.Now we can simply characterise rewritability of a query Q by the existence of a partition of body(Q) whose subsets have cover descriptions with views from V. Definition 4.4.Let Q be a query and V be a set of views.A collection We call a cover partition consistent if variables of any α j (V j ) are in the range of any other α i only if they also appear in bvars Q (A j ).We note that, since each α i is a view application, in a consistent cover partition, the applications obey quantified variable disjointness. 7xample 4.5 (Continuation of Example 4.3).Let C 1 = (A 1 , V 1 , α 1 , ψ 1 ) be the cover description defined in Example 4.3.In addition, we consider the cover descriptions C 2 and C 3 with and It is, however, not consistent, since v is in the range of α 1 and α 2 , but not in bvars(A 1 ).Replacing α 2 and ψ 2 by mappings α2 and ψ2 with α2 (v 2 ) = v and ψ2 (v) = v that agree with α 2 and ψ 2 on all other variables, respectively, yields a consistent cover partition.Note that there is no consistent cover partition with a cover description of the form (A 1 , V 1 , α ′ 1 , id) because necessarily α ′ 1 (w 1 ) = w would hold and thus w would be in the range of α ′ 1 and α 3 .A consistent cover partition C induces a query Q C and an expansion Q ′ C as follows.We note first that each variable in head(Q) occurs in at least one of the sets A i and thus in some set bvars Q (A i ).Therefore, Condition (2) of Definition 4.2 guarantees that each head variable of Q occurs in some set α i (head(V i )) and thus a cover partition

Likewise, and thanks to quantified variable disjointness, it induces an expansion
Next, we show that the existence of a cover partition indeed characterises rewritability.
Theorem 4.6.Let Q be a minimal conjunctive query and V be a set of views.The following three statements are equivalent.
(a) Q is V-rewritable.
(b) There is a cover partition C for Q over V.
(c) There is a consistent cover partition C for Q over V.
then there is a consistent cover partition with the same partition of body(Q) as C.
Proof.We first show that (c) implies (a).To this end, let C = C 1 , . . ., C m be a consistent cover partition for Q over V, where, for each i, We prove that the query Q C induced by C is a V-rewriting of Q: first, since A 1 , . . ., A m partition body(Q) and thanks to Condition (1), id is a homomorphism from Q into the expansion Q ′ C .Therefore, it suffices to show that the union of the mappings ψ 1 , . . ., ψ m is a homomorphism h ′ from Q ′ C into Q.We first show that h ′ is well-defined: let us assume that a variable z occurs in α i (V i ) and α j (V j ) with i ̸ = j.Thanks to consistency, z is in bvars(A i ) and bvars(A j ) and therefore ψ i (z) = ψ j (z) thanks to Condition (4).
That h ′ is a homomorphism follows easily, because each ψ i is a body homomorphism by Condition (3) and h ′ is the identity on head(Q) by Condition (4).
Next, we show that (a) implies (b).Let us assume that Q has a V-rewriting Q R with an expansion Q E and let the equivalence of Q and Q E be witnessed by homomorphisms Since Q is minimal, we can assume, thanks to Lemma 4.7, that h is injective and h ′ is the inverse of h on the atoms of h(body(Q)).Since h is injective, we can further assume, without loss of generality, that h is the identity mapping on the variables in Q and that h ′ (x) = x for every such variable. 8hat is, we have head where the applications fulfil the quantified variable disjointness property.These applications induce a partition A 1 , . . ., A m of body(Q E ) in the following way: For every i ∈ {1, . . ., m} we define , where h ′ i is the restriction of h ′ to the variables in α i (V i ), for every i ∈ {1, . . ., m}.To show that C 1 , . . ., C m yield a cover partition, it only remains to show that each C i is a cover description.
To this end, we show that all four conditions in Definition 4.2 are satisfied for C i , for every i ∈ {1, . . ., m}.Condition (1) holds by the definition of A i .Condition (3) is true because each h ′ i is a restriction of the body homomorphism h ′ .Condition (4) follows since h ′ is the identity on all variables in body(Q) and thus also on all variables of A i ⊆ body(Q).
Hence, it only remains to show that Condition (2) is satisfied.To this end, let x be an arbitrary bridge variable in some A i .If x occurs in a subset A j where j ̸ = i, then it is a head variable in both α i (V i ) and α j (V j ) because of the quantified variable disjointness of α 1 , . . ., α m .Otherwise, A i is the only subset containing variable x, which thus has to be a head variable of Q in order to be a bridge variable in it is then also a head variable of Q R .This, in turn, implies that x is a head variable of α i (V i ) because the quantified variables of α i (V i ) do not occur in Q R .
Finally, we show that (b) implies (c).Let thus C = C 1 , . . ., C m be a cover partition for Q over V, where, for each i, C i = (A i , V i , α i , ψ i ).Let z be a variable that occurs in some α i (V i ), but not in bvars(A i ), and also in some α j (V j ) with j ̸ = i.Since z ̸ ∈ bvars(A i ) we have z ̸ ∈ vars(A j ).We define α ′ j like α j but, for some fresh variable z ′ , we set α ′ j (x) = z ′ whenever α j (x) = z.Accordingly, we define ψ ′ j like ψ j but with ψ ′ j (z ′ ) = ψ j (z).It is easy to verify that C ′ j = (A j , V j , α ′ j , ψ ′ j ) is a cover description, as well.By repeating this process, a consistent cover partition for Q over V can be obtained.
The previous proof used the following lemma.It is similarly stated in the proof of Lemma 2 in [CGLP20a], see also the proof of Lemma 9 in the full version [CGLP20b] of [CGLP20a] for the proof idea.We give its short proof to keep the paper self-contained.
Lemma 4.7.Let Q 1 be a minimal conjunctive query, Q 2 a conjunctive query equivalent to Q 1 and h 1 : Remark 4.8.We note that the requirement in Theorem 4.6 for Q to be minimal is only seemingly a restriction in our setting, since we apply it only to acyclic queries.If Q is acyclic but not minimal, an equivalent minimal query Q ′ can be computed in polynomial time by iteratively removing atoms from its body [CM77,CR00].Moreover, it is guaranteed that the minimal query Q ′ is also acyclic (we believe this to be folklore, it follows readily from more general results, cf. for instance [BPR17]).
The same is true for free-connex acyclic queries: every homomorphism from Q to Q ′ is also a homomorphism from body(Q) ∪ {head(Q)} to body(Q ′ ) ∪ {head(Q ′ )} and vice versa, since the relation symbol of head(Q) = head(Q ′ ) does not occur in body(Q ′ ).Thus, Q ′ is minimal if and only if the Boolean query whose body is body For hierarchical and q-hierarchical queries the same holds: it is easy to see that removing atoms does not change the conditions in their respective definitions.

Towards Acyclic Rewritings
In this section, we turn our focus to the main topic of this paper: acyclic rewritings and the decision problem that asks whether such a rewriting exists.We study the complexity of Rewr(V, Q, ACQ) for the case that V and Q are the class of conjunctive queries and for various subclasses.It will be helpful to analyse the case that Q = ACQ and V = CQ first.The following example illustrates that, even if an acyclic rewriting exists, the canonical rewriting need not be acyclic.Furthermore, it may be that each "sub-rewriting" of the canonical candidate is cyclic or not a rewriting, and thus none of them is an acyclic rewriting.
Example 5.1.Consider the query Q given by the rule and the following views Each query whose body is a proper subset of the body of Q R is not a V-rewriting for Q.However, an acyclic V-rewriting for Q exists.One such rewriting is the query Even though Example 5.1 suggests that general and acyclic rewritings can be seemingly unrelated, it turns out that there is a close connection between them.In fact, we will show next that an acyclic V-rewriting exists if and only if an arbitrary V-rewriting exists.Furthermore, from an arbitrary rewriting an acyclic rewriting can always be constructed.
Towards a proof, we study the relationship between a query Q and the view applications that can occur in any rewriting of Q more closely, to determine the circumstances under which a decomposition of view atoms is possible.
Example 5.2 (Continuation of Example 5.1).We first have a closer look at the V 3 -atoms in the rewritings of the previous example.The two V 3 -atoms in Q ′ R can be understood as a (sub-)query with body {V 3 (z, y ′ ), V 3 (z ′ , y)} and head variables z, y, whereas the V 3 -atom in Q R can be understood as a (sub-)query with body {V (z, y)} and head variables z, y.The expansions of these two (sub-)queries are equivalent.That is, the acyclic rewriting can be obtained from the canonical rewriting by replacing an atom by a set of atoms that is equivalent with respect to view expansions. 10 In Example 5.1 the acyclic rewriting was obtained from the canonical rewriting by replacing a view atom by a set that contained one view atom for each connected component of (the hypergraph induced by) V 3 .The following example shows that the required modifications can be more involved.
Example 5.3.Consider the query Q given by the rule and the following views.
In contrast to Example 5.1, all view bodies are connected.Therefore, replacing a view atom by a set of representatives of connected components of some views does not yield an acyclic rewriting.However, there is an acyclic Now we turn to the main result of this section.
Theorem 5.4.Let Q be a conjunctive query and V be a set of views.(a) If Q is acyclic and V-rewritable, then it has is an acyclic V-rewriting.(b) If Q is free-connex acyclic and V-rewritable, then it has a free-connex acyclic V-rewriting.
Proof.Since Q is acyclic, we can assume that it is minimal (cf.Remark 4.8).Moreover, it has a join tree J Q , and, thanks to Theorem 4.6, since Q is V-rewritable, there is a consistent cover partition We show first that Q C is acyclic if each set A i is connected in J Q .Afterwards we show that a consistent cover partition with that property can always be constructed from C.
To this end, we show how to construct a join tree J for Q C : we first cluster, for each j, the nodes for A j in J Q together into one node that is labelled by α j (head(V j )).Since the A j are connected in J Q , the resulting graph J is a tree.
To verify that J is a join tree, let us consider two nodes u, v of J labelled by α j (head(V j )) and α k (head(V k )), respectively, and x be a variable that appears in α j (head(V j )) and α k (head(V k )).Thanks to consistency of C, variable x appears in bvars Q (A j ) and in bvars Q (A k ).It follows that x appears in two atoms A ∈ A j and A ′ ∈ A k .Moreover, since J Q is a join tree, x appears in every node on the (shortest) path from A to A ′ in J Q .Again thanks to consistency, x is in bvars Q (A ℓ ) for every α ℓ (head(V ℓ )) along the corresponding contracted path in J from u to v.In particular, x appears in all sets α ℓ (head(V ℓ )) on the path from u to v. Thus, u and v are x-connected.We conclude that J is a join tree for If Q is also free-connex acyclic, there is a join tree J + Q for body(Q) ∪ {head(Q)}.As we show later, we can assume that the sets A i of the cover partition C are also connected in J + Q .Clustering the nodes 11 of J + Q analogously as for J Q yields a join tree for body(Q C ) ∪ {head(Q C )} since head(Q C ) = head(Q) and all head variables in a set A i also occur in the new label α i (head(V i )) thanks to Condition (2) of Definition 4.2.
We now show how, from a consistent cover partition C, we can construct a consistent cover partition C ′ = C ′ 1 , . . ., C ′ m such that each set A ′ i is connected.To this end, let us assume that some set A j is not connected in J Q .Let B j ⊆ A j be a maximal connected subset and let A ′ j = A j \ B j .We observe that each variable x that appears in B j and A ′ j , is also in bvars Q (A j ), since x has to occur in at least one other atom in J Q (otherwise, B j and A ′ j would be connected in J Q ).Thus, bvars Q (B j ) ⊆ bvars Q (A j ).We conclude that C B j = (B j , V j , α j , ψ j ) is a cover description.Likewise, C ′ j = (A ′ j , V j , α j , ψ j ) is a cover description.By repeated application of this modification step, we eventually obtain a cover partition then C ′ can be further refined as described above but w.r.t.J + Q instead of J Q .Refining the cover partition iteratively w.r.t.J Q and J + Q yields a cover partition The number of iterations is bounded by the number of atoms of Q.
Thanks to Theorem 4.6, C ′′ can be turned into a consistent cover partition (and the required renamings of variables do not affect the connectedness).Theorem 5.4 delivers good news as well as bad news.The good news is that, since the proofs of Theorem 5.4 and Theorem 4.6 are constructive, we altogether have a procedure to construct an acyclic rewriting from an arbitrary rewriting Q R if the query Q is acyclic.Furthermore, if a homomorphism from an expansion of Q R to Q can be computed in polynomial time, the overall procedure can be performed in polynomial time.In particular, we get the following collary.
Corollary 5.5.For every k, Rewr(ACQ k , ACQ, ACQ) is in polynomial time, and an acyclic rewriting can be computed in polynomial time (if it exists).
That the decision problem is in polynomial time follows immediately from Proposition 3.10 and Theorem 5.4.Moreover, the canonical candidate for an acyclic query Q and a set V of acyclic views with bounded arity can be computed in polynomial time, since the size of V(canon(Q)) is bounded by a polynomial and query evaluation for acyclic queries is in polynomial time [Yan81,AHV95].In case the canonical candidate is a rewriting, valuations witnessing V(canon(Q)) can be computed in polynomial time [KS06] and combined into a homomorphism from an expansion of the rewriting to Q. Thus, an acyclic rewriting can then be efficiently constructed by the above procedure.
In the next part of this section, we resolve the complexity of Rewr(ACQ, ACQ, CQ) and Rewr(ACQ, ACQ, ACQ) and their restrictions to schemas of bounded arity.5.2.The Complexity of Acyclic Rewritability for Acyclic Queries.It may be tempting to assume that, since acyclic queries are so well-behaved in general, it should be tractable to decide whether for an acyclic query and a set of acyclic views there exists an acyclic rewriting.However, as we show next, this is (probably) not the case, and this surprising finding even holds for the even better behaved hierarchical queries as well.
Of course, Theorem 5.7 immediately implies NP-hardness of Rewr k (V, Q, ACQ), for all pairs V, Q of classes with ACQ ⊆ V ⊆ CQ and ACQ ⊆ Q ⊆ CQ.
We will see in Section 7 that Theorem 5.4 has an analogue for hierarchical queries from which it can be concluded that deciding the existence of a hierarchical rewriting, given a hierarchical query and hierarchical views, is still NP-complete (cf.Corollary 7.5).17:19 Form(w 0 , w 1 , u) of sets of atoms, where the first two sets {Form(w 0 , w 1 , u)} and C 0 are the same as above.
The bodies of query Q and view V thus differ in the sets C 1 and N 1 , . . ., N n of atoms on the one hand as well as X 1 , . . ., X n and X + 1 , . . ., X + n and X − 1 , . . ., X − n on the other hand.Sets N 1 , . . ., N n and X 1 , . . ., X n are intended to guide the assignment of propositions, while sets X + 1 , . . ., X + n and X − 1 , . . ., X − n represent the occurrences of literals in the clauses of formula f , and set C 1 represents the aim to satisfy all of them.The new sets are defined as follows.
• For each proposition x i , set X i contains atoms Neg i (x i , xi , u) and Neg i (x i , x i , u), intended to enforce the mapping of (x i , xi ) to either (w 0 , w 1 ) or (w 1 , w 0 ), that is, to "complementary truth values".• For each positive literal x i and each clause f j that contains literal x i , set X + i contains an atom C j (x i , xi , u).
• For each negated literal xi and each clause f j that contains literal ¬x i , set X − i contains an atom C j (x i , x i , u).
Thus defined, the view is also hierarchical as shown in the following.Every atom in every set contains variable u.Therefore, it follows that atoms(x) ⊆ atoms(u) for all x ∈ vars(V ).Furthermore, all atoms in {Form(w 0 , w 1 , u)} and C 0 additionally contain both variables w 0 and w 1 and they are the only atoms to contain these variables.Thus, we have that atoms(w 0 ) = atoms(w 1 ) and atoms(y) ∩ atoms(w 0 ) = ∅ and atoms(y) ∩ atoms(w 1 ) = ∅ for all y ∈ vars(V ) \ {u, w 0 , w 1 }.Similarly, for each i ∈ {1, . . ., n}, all atoms in X i ∪ X + i ∪ X − i contain both variables x i and xi , which do not occur in any other atom.It follows that atoms(x i ) = atoms(x i ) and atoms(x i ) ∩ atoms(y) = ∅ and atoms(x i ) ∩ atoms(y) = ∅ for all y ∈ vars(V ) \ {x i , xi , u} and i ∈ {1, . . ., n}.Therefore, we obtain that each pair of variables in V satisfies at least one of the conditions in Definition 2.1 and thus V is hierarchical-and acyclic in particular.For the correctness argument, however, the structure of the join tree of view V , depicted in Figure 1(b) is probably more helpful.We close the description of the construction by remarking that the query and view can be computed in polynomial time for any given propositional formula in 3CNF.
Correctness.We prove now that formula f is satisfiable if and only if there are mappings α and ψ such that (body(Q), V, α, ψ) is a cover description for Q.
First, we show that if formula f is satisfiable, then there are mappings α and ψ such that (body(Q), V, α, ψ) is a cover description for Q.
To this end, let us assume that f is satisfiable and that this is witnessed by a satisfying truth assignment β.From this assignment for propositions x 1 , . . ., x n , we derive the application α of V .Let α be the mapping that is the identity on variables w 0 , w 1 and u and behaves as follows on the literal variables, for every i ∈ {1, . . ., n}.
We choose mapping ψ to be the identity on the variables of α(V ).Then (body(Q), V, α, ψ) is a cover description for Q because application α establishes the following relationships between the atoms of view V (on the left-hand side) and the atoms of query Q (on the right-hand side).
is rather obvious since the application α behaves like the identity on the variables w 0 , w 1 and u in C 0 .Next, let us consider the set X i = {Neg i (w 0 , w 1 , u), Neg i (w 1 , w 0 , u)} for an arbitrary i ∈ {1, . . ., n}.By definition, application α maps (x i , xi ) either to (w 0 , w 1 ) or (w 1 , w 0 ).In each case, we get Hence, Property (ii) holds too.Again by definition of α, the atoms in X + 1 ∪ • • • ∪ X + n , which are of the form C j (x i , xi , u), are mapped to C j (w 0 , w 1 , u) or C j (w 1 , w 0 , u), contained in C 0 ∪ C 1 .An analogous argument holds for the atoms in Thus, Property (iii) is also satisfied.Finally, we show that Property (iv) holds.To this end, let C j (w 1 , w 0 , u) be an arbitrary atom in C 1 .By our assumption, clause f j in formula f is satisfied by truth assignment β.Hence, there is a literal ℓ j,h in clause f j such that β |= ℓ j,h .Let us assume ℓ j,h = x i for some i ∈ {1, . . ., n}; the argument for a negated literal is analogous.This assumption implies α(x i ) = w 1 by the definition of α, which depends on β.Since literal x i is a literal in clause f j , we have C j (x i , xi , u) ∈ X + i and thus α(X + i ) ∋ C j (w 1 , w 0 , u).Since Properties (i) -(iv) consider all body atoms of query Q and view V , they imply that the bodies are identical: body(Q) = α(body(V )).For ψ = id, this trivially implies body(Q) ⊆ α(body(V )) and ψ(α(body(V ))) ⊆ body(Q), namely Conditions (1) and (3) in Definition 4.2.Furthermore, since query Q is Boolean, set A = body(Q) has no bridge variables.Thus, Conditions (4) and (2) are trivially satisfied.
Therefore, (A, V, α, ψ) is a cover description for Q.Now, we show that formula f is satisfiable if there are mappings α and ψ such that (body(Q), V, α, ψ) is a cover description for Q.
Consider the set B = α(body(V )) of atoms.From Definition 4.2, we know that body(Q) ⊆ B and ψ(B) ⊆ body(Q) hold.Since both sets body(Q) and B contain only one Form-atom B = Form(w 0 , w 1 , u), we can conlude α(B) = B by Condition (1) and ψ(α(B)) = ψ(B) = B by Condition (3).Therefore, each mapping α and ψ is the identity on variables w 0 , w 1 and u.Hence, application α induces unambiguously a truth assignment β that maps proposition x i → 1 if α(x i ) = w 1 ; and maps x i → 0 otherwise.
It suffices to show that β satisfies formula f .Let f j be an arbitrary clause of the formula.From body(Q) ⊆ α(body(V )), we know that atom C j (w 1 , w 0 , u) is in α(body(V )) because C j (w 1 , w 0 , u) is in C 1 and, therefore, in body(Q).Since body(V ) does not contain atoms from C 1 , it has to contain a different C j -atom which is mapped to C j (w 1 , w 0 , u) by α.This atom cannot be C j (w 0 , w 1 , u) from C 0 because α is the identity on w 0 , w 1 , and u.Hence, there has to be an atom C j (x i , xi , u) ∈ X + i or C j (x i , x i , u) ∈ X − i for some i ∈ {1, . . ., n} in body(V ) that is mapped to C j (w 1 , w 0 , u) by α.In the case that an atom C j (x i , xi , u) ∈ X + i is mapped to C j (w 1 , w 0 , u), it follows by construction that the literal x i occurs in clause f j .Furthermore, from α(C j (x i , xi , u)) = C j (w 1 , w 0 , u), we can infer α(x i ) = w 1 .Hence, truth assignment β satisfies literal Thus, truth assignment β satisfies every clause of formula f and formula f is satisfiable if a rewriting of query Q in terms of view V exists.
This concludes our proof.
The proof of Theorem 5.7 can now be stated easily.
Proof of Theorem 5.7.The upper bound follows from Theorem 3.2.
For the lower bound, we show that the reduction of Theorem 5.9 can be adapted to a reduction from 3SAT to Rewr k (HCQ, HCQ, CQ).To this end, we show that the query Q constructed in the proof of Theorem 5.9 has a {V }-rewriting if the formula f is satisfiable.
If the formula f is satisfiable then there are mappings α and ψ such that C = (body(Q), V, α, ψ) is a cover description.Due to Theorem 4.6 it follows that Q is {V }rewritable, since C trivially constitutes a cover partition.
If Q is {V }-rewritable then there is a cover partition C for Q over {V } due to Theorem 4.6.We will show that C consists of a single cover description C. Since C then necessarily has the shape (body(Q), V, α ′ , ψ ′ ), this implies that the formula f is satisfiable.
Suppose for the sake of contradiction that C consists of at least two cover descriptions.Let C 1 be a cover description from C. Since V is the only view and C 1 is not the only cover description in C, C 1 has the shape C 1 = (A 1 , V, α 1 , ψ 1 ) with A 1 ⊊ body(Q).We observe that the variable u is a bridge variable of A 1 since it occurs in every atom of Q, and, thus, in A 1 and outside A 1 .Since u is always the last variable in every atom in Q and V , it follows from Definition 4.2(1) that α 1 (u) = u.But then bvars Q (A 1 ) ⊈ α 1 (vars(head(V ))), since u does not occur in the head of V and α 1 does not unify quantified variables.This contradicts Definition 4.2(2), and, thus, C 1 being a cover description.Therefore, C consists of a single cover description.
To conclude, we have established that Q is {V }-rewritable if and only of formula f is satisfiable.Hence, the NP-hardness shown for CovDesc(HCQ, HCQ) also holds for Rewr k (HCQ, HCQ, CQ).It trivially transfers to Rewr k (ACQ, ACQ, CQ) and by Theorem 5.4 also to Rewr k (ACQ, ACQ, ACQ).
The fact that, for Theorem 5.7, a single view application is sufficient if there is any rewriting, allows to draw yet another conclusion for a related problem, defined next.Definition 5.10.Given a query class Q ⊆ CQ, the select-full-project equivalence problem for Q, denoted SelProjEquiv(Q), asks, upon input of a Boolean query Q ∈ Q and a query Q ′ ∈ Q whether there is a Boolean query Q ′′ which is equivalent to Q and whose body can be obtained from the body of Q ′ by unifying head variables of Q ′ in its body. 14xample 5.11.Consider the Boolean query Q defined by H() ← R(x), S(y, x, y), T (y, z), T (z, x) and the query Q ′ defined by H ′ (y, z, u, v) ← R(x), S(y, x, v), S(y, x, w), T (y, z), T (u, x).
The body of the Boolean query Q ′′ given by H() ← R(x), S(y, x, y), S(y, x, w), T (y, z), T (z, x) can be obtained by unifying the head variables y and v as well as z and u in body(Q ′ ).The query Interpreting the query Q ′ in Definition 5.10 as a view, the unification of head variables can be realised by an application α which does not unnecessarily rename variables.Whether the desired Boolean query Q ′′ exists then boils down to whether there is a {Q ′ }-rewriting for Q whose body consists of a single view atom, i.e. α(head(Q ′ )).
Example 5.12 (Continuation of Example 5.11).Recall that the body of the Boolean query Q ′′ in Example 5.11 is obtained from body(Q ′ ) by unifying the head variables y and v as well as z and u.Let α be the application which maps v to y, u to z, and is the identity on every other variable.The Boolean query with body {α(head Due to the relationship between the select-full-project equivalence problem and rewritability explained above and Theorem 5.9 the following holds. Corollary 5.13.SelProjEquiv(ACQ) and SelProjEquiv(HCQ) are NP-hard.
Proof.The proof is essentially the same as for Theorem 5.7.
Let Q and V be the Boolean query and the view constructed in the proof for Theorem 5.7, respectively.If (body(Q), V, α, ψ), for some mappings α and ψ, is a cover description for Q, then the Boolean query with body α(head(V )) is a rewriting of Q since the cover description constitutes a consistent cover partition for Q (a cover partition consisting of a single cover description is consistent by definition).
Thus, Q is equivalent to the Boolean query Q ′′ whose body is obtained from body(V ) by unifying exactly the (head) variables in body(V ) the application α unifies.In other words, Q ′′ equals α(V ) up to renaming variables.
Conversely, if Q is equivalent to a Boolean query Q ′′ whose body can be obtained from body(V ) by unifying head variables of V in body(V ), then there is an application α with α(body(V )) = body(Q ′′ ) and, thus, Q ′′ is the expansion of a V -rewriting, namely the Boolean query with body α(head(V )), for Q.
Hence, by choosing the second input query to be Q ′ = V , Corollary 5.13 follows because Q and Q ′ are hierarchical.
We note that the restriction of the select-full-project equivalence problem to Boolean queries Q ′ is just the equivalence problem for Boolean queries which is in P for acyclic queries [CR00].
But, surprisingly, the select-full-project equivalence problem is NP-hard not only for hierarchical but even for q-hierarchical queries-while the rewriting problem for q-hierarchical views is in P (Corollary 7.4).

If
x occurs in only one atom of body(R) then there are two cases: (i) if in the only atom in which x occurs, y occurs as well, then atoms R (x) ⊆ atoms R (y), and (ii) otherwise, atoms R (x) ∩ atoms R (y) = ∅ holds.Analogously, if y occurs in only one atom of body(R) then either atoms R (y) ⊆ atoms R (x) or atoms R (x) ∩ atoms R (y) = ∅ holds.
Let us finally assume that both x and y occur in at least two atoms of body(R) each.Thanks to C being a consistent cover partition and Condition (2) of Definition 4.2, x and y are bridge variables, and occur in atom sets of at least two different cover descriptions of C each.
Since Q is hierarchical we have the following three cases.
Case 1: atoms Q (x) ⊆ atoms Q (y).Let (A, V, α, ψ) in C be a cover description which fulfils x ∈ α(head(V )).In particular, x occurs in an atom A ∈ A. Therefore, y also occurs in A, since atoms Q (x) ⊆ atoms Q (y).Since y is a bridge variable by assumption, it follows that y ∈ α(head(V )) thanks to Condition (2) of Definition 4.2.We conclude that atoms R (x) ⊆ atoms R (y) holds.Case 2: atoms Q (y) ⊆ atoms Q (x).This case is analogous to the first case.Case 3: atoms Q (x) ∩ atoms Q (y) = ∅.If there is no cover description in C, in which x and y occur together, then atoms R (x) ∩ atoms R (y) = ∅ holds.Let us thus assume that there is a cover description (A, V, α, ψ) in C in which both x and y occur.Thanks to atoms Q (x) ∩ atoms Q (y) = ∅, set A contains at least two atoms.Thanks to Lemma 7.3, there is a variable u / ∈ vars(head(α(V ))) that appears in all atoms of A. Since Q is hierarchical, we must have atoms Q (x) ⊊ atoms Q (u).However, this yields a contradiction, because x occurs in at least two cover descriptions, and therefore outside A, whereas u does not.This concludes the proof of Statement (a).
Towards (b), let us assume that Q is q-hierarchical.Our goal is to show that for all variables x, y ∈ vars(R), if atoms R (x) ⊊ atoms R (y) holds and x is in the head of R, then y is also in the head of R. Note that x and y are both bridge variables because x occurs in the head of Q which is the same as the head of R, and y occurs in at least two atoms of body(R) due to atoms R (x) ⊊ atoms R (y).In particular, x and y both occur in Q.Since the heads of Q and R are the same, whenever atoms Q (x) ⊊ atoms Q (y) holds, we can conclude that if x is in the head of R, then y is also in the head of R.
In the remainder we assume, for the sake of a contradiction, that atoms Q (x) ⊊ atoms Q (y) does not hold.Since Q is hierarchical this means that either atoms Q (x) ⊇ atoms Q (y) or atoms Q (x) ∩ atoms Q (y) = ∅ holds. 18 If x also occurs in at least two atoms of body(R), then the preconditions for Case 2 and Case 3 in the proof for Statement (a) above are met.Thus, atoms Q (x) ⊇ atoms Q (y) and atoms Q (x) ∩ atoms Q (y) = ∅ imply atoms R (x) ⊇ atoms R (y) and atoms R (x) ∩ atoms R (y) = ∅, respectively.But this is a contradiction to atoms R (x) ⊊ atoms R (y).
In the remainder, consider the case that x occurs in exactly one atom B of body(R).Variable y occurs in B and outside B. Since x and y are bridge variables, it follows that x occurs in exactly one atom set A of C and y occurs in and outside A. Hence, atoms Q (x) ∩ atoms Q (y) = ∅ holds because atoms Q (x) ⊇ atoms Q (y) cannot.This implies that A consists of at least two atoms, since x and y co-occur in A. Therefore, there is a non-bridge variable u that occurs in all atoms of A thanks to Lemma 7.3(B).But then atoms Q (x) ⊊ atoms Q (u) holds, and, since u is a non-head variable, x is a non-head variable as well, because Q is q-hierarchical.This is a contradiction to x being a head variable.
All in all, we can conclude that atoms Q (x) ⊊ atoms Q (y) holds, and therefore, that y is a head variable.Thus, R is q-hierarchical.
Similarly to Theorem 5.4, Theorem 7.2 delivers good news as well as bad news.The good news is that, since QHCQ ⊆ CCQ and QHCQ ⊆ HCQ ⊆ ACQ hold, the rewriting problem for q-hierarchical views and hierarchical queries over a fixed schema is tractable thanks to Theorem 6.2 and Theorem 7.2.The bad news is that Theorem 5.7 and Theorem 7.2 imply NP-completeness for hierarchical queries and views.Of course, Corollary 7.5 implies NP-hardness of Rewr k (V, Q, ACQ), for all pairs V, Q of classes with HCQ ⊆ V ⊆ CQ and HCQ ⊆ Q ⊆ CQ.

Related Work
We already mentioned that our notion of cover partitions is similar to various notions from the literature.We mention three of them here.
In [AC19] algorithms for finding exact rewritings with a minimal number of atoms and (maximally) contained rewritings are presented.For this purpose triples (S, S ′ , h) are considered which are comparable to cover descriptions [AC19, Definition 3.12].Namely, S ′ corresponds to the set A in a cover description, S = body(Q), and h is a homomorphism from S ′ into a view V .In our characterisation we can assume h = id (and, thus, omit it in the specification of a cover description) thanks to the view application α.We note that h can be assumed to be one-to-one for (equivalent) V-rewritings [AC19, Theorem 3.15].Furthermore, for (equivalent) rewritings, only candidates whose body is a proper subset of the canonical rewriting's body are considered (cf.[AC19, Section 3.2.3]).Note that the existence of body homomorphisms ψ i mapping the α i (V i ) into Q is guaranteed for such candidates and ψ i determines the application α i (in particular, the unification of variables in the head of a view).This is, however, too restrictive for our purposes (cf., for instance, Example 5.1) which is why α and ψ are part of a cover description.We emphasise that Condition (2) is equivalent to the shared-variable property [AC19, Definition 3.12].An analogue of the implication (a) ⇒ (b) of Theorem 4.6 is proven [AC19, Theorem 3.15] and, based on that, an algorithm CoreCover for finding (equivalent) rewritings is derived.This algorithm, however, considers only triples with maximal sets S ′ (called tuple cores) and they are allowed to overlap, i.e. they do not have to form a partition.In contrast, we consider non-maximal sets, for instance in the proof of Theorem 5.4 which depends upon the possibility to split these sets.For (maximally) contained rewritings, an analogue of (b) ⇒ (a) is implied by [AC19, Theorem 4.19].Interestingly, the proof of this result (and the associated algorithm) exploits triples with minimal sets S ′ (that still satisfy the shared-variable property) and a partition property.We discuss the relation of this minimality constraint with our results below in more detail.An analogue of (b) ⇒ (a) for (equivalent) V-rewritings, and, therefore, a characterisation for V-rewritability, is not stated (nor implied).
In [GKC06] a characterisation for V-rewritability is employed to find rewritings efficiently.It is in terms of tuple coverages and a partition condition corresponding to cover descriptions and partitions, respectively [GKC06, Theorem 5, Theorem 6].A tuple coverage, denoted s(t V , Q), is a (non-empty) set G, where t V corresponds to α(V ) and G to the set A of a cover description.Similar to [AC19] the most crucial differences in comparison with our characterisation are that only rewritings whose body is contained in the canonical rewriting, i.e. vars(α(V )) ⊆ vars(Q) holds for all tuple coverages, are considered.Also the mappings ψ and α and their associated conditions are not denoted explicitly, instead it is required that G is isomorphic to a subset of α(V ) and Condition (2) holds.This is equivalent to our conditions if restricted to the rewritings considered in [GKC06].
Lastly, in [PH01] MiniCon descriptions (MCDs) are used to compute (maximally) contained rewritings (which are unions of conjunctive queries, in general).A MCD is a tuple of the form (h, V, φ, G) which relates to a cover description (A, V, α, ψ) as follows: h is called a head homomorphism and is basically the restriction of an application α to the head variables of the view V , G corresponds to the set A, and φ is mapping embedding G into h(V ).The component φ has no counterpart in a cover description because, thanks to α being able to rename any variable in V , we can assume φ = id.We note that the applications α i of a consistent cover partition C also allow us to conveniently denote the expansion of the associated rewriting Q C .On the other hand, there is no counterpart for the body-homomorphism ψ in a MCD (since the ψ i in a consistent cover partition ensure that the query is contained in the associated expansion of the rewriting, there is also no need for them if contained rewritings are considered).Condition (2) is stated as [PH01, Property 1] and the idea of a cover partition in [PH01, Property 2].
Minimal cover descriptions.In [AC19], "minimal" triples are used to construct maximally contained rewritings.In terms of cover descriptions, an analogous definition of minimality is as follows.

Definition 8.1 (Minimal Cover Descriptions). A cover description (
It seems obvious to exploit such minimal cover description for the constructions in Theorem 5.4 and Theorem 7.2, instead of partitioning the set A of a cover description "manually".However, in contrast to our constructions, this would (possibly) not result in efficient algorithms to compute an acyclic or hierarchical rewriting from an arbitrary one, since it turns out that deciding whether a cover description is minimal is coNP-hard.Proposition 8.2.Let Q be a hierarchical conjunctive query.It is coNP-hard to decide whether a cover description (A, Q, α, ψ) is minimal.
Proof.We reduce the problem Rewr(HCQ, HCQ, CQ), which is NP-hard due to Theorem 5.7, to the complement of the minimisation problem.For a hierarchical query Q and a set of hierarchical views V, we describe how we can derive a cover description C Q V and a query Q ′ such that C Q V is not minimal for Q ′ if and only if there is a V-rewriting for Q.For convenience, we introduce some notation.For an atom A = R(x 1 , . . ., x r ) and a variable u we denote by A u the atom R(u, x 1 , . . ., x r ) resulting from extending A by the variable u.We lift this notation to sets A of atoms in the natural way, i.e.
If a set of atoms or a query consists of atoms extended by (possibly different) variables as described above, we often signify this with a "+"-symbol in the superscript, e.g.we write A + .
Let V 1 , . . ., V n be the views in V. We assume that the views in V and the query Q refer to distinct variables, which is no restriction since the variables can be renamed accordingly in polynomial time.
Construction.We start with the construction of the cover description C Q V .To define a view and a query for the cover description, we consider views V + 1 , . . ., V + n that are copies of the input views V 1 , . . ., V n where each atom will be expanded by a special variable, that only appears in the associated view, in the first component of each body atom and of the head, i.e. for every i ∈ {1, . . ., n} we define a view V + i with head(V + i ) = head(V i ) v i and body(V + i ) = body(V i ) v i where v 1 , . . ., v n are distinct variables that do not occur in the query or in any of the views in V.In the same fashion, we define a set of atoms for the atoms in the query, but we add a new atom whose relation symbol does not occur in Q or in any of the views in V.For a new variable u and a new relation symbol S, we define Let us now turn to the definition of the view W that is used in the cover description.The view W contains the atoms in A + Q , the atoms in body(V + i ) for all i ∈ {1, . . ., n}, and a special atom S(v n+1 ) with a new variable v n+1 in the body.The head of W contains the variables in the head of Q and variables in the heads of the views V i for all i ∈ {1, . . ., n} as well as the variables v 1 , . . ., v n+1 , but not u.Let us emphasize that, for each view V + i , the set body(V + i ) is contained in body(W ).The query Q ′ is defined by the rule head(Q) ← body(W ), that is Q ′ has the head variables of Q and the body of Q ′ has the same atoms as the body of W .
The cover description C Q V is defined as (A + Q , W, id, id).Note that the bridge variables of A + Q with respect to Q ′ are contained in the head variables in W . Hence, C Q V is a cover description for Q ′ .
Note that the views V + i are hierarchical since for the new variables v i we have that atoms y) for all y ∈ vars(V i ) and the input views are hierarchical.The same is true for the set A + Q (viewed as a Boolean query here).Moreover, W and Q ′ are also hierarchical since the views and the query, and, hence, the V + i and A + Q do not share any variable.
Correctness.In the following, we show that Q has a V-rewriting if and only if For the only if-direction assume that Q has a V-rewriting.Then there is a consistent cover partition C for Q.Note that by construction each cover description (A, V j , α, ψ) in C can be turned into a cover description (A u , V + j , α ∪ {v j → u}, ψ ∪ {u → u}) for Q ′ and, furthermore into a cover description (A u , W, α ′ , ψ ′ ) for Q ′ where α ′ and ψ ′ coincide with α ∪ {v j → u} and ψ ∪ {u → u} on their domain, respectively, and are the identity on all other variables.Let C ′ be the collection of cover descriptions we obtain by transforming every cover description in C to a cover description as described above and the additional cover description ({S(u)}, W, {v n+1 → u}, id) for Q ′ .
Since C is a cover partition for Q and Q does not contain an S-atom, the atom sets of the cover descriptions in C ′ form a partition of A + Q = body(Q) u ∪ {S(u)}.But then (A + Q , W, id, id) is not minimal for Q ′ because C ′ consists of at least two cover descriptions.For the if-direction assume that C Q V is not minimal for Q ′ .Let C ′ = (A + 1 , W, α 1 , ψ 1 ), . . ., (A + k , W, α k , ψ k ) be a collection of cover descriptions witnessing that C Q V = (A + Q , W, id, id) is not minimal for Q ′ .From C ′ we will derive a cover partition C for Q witnessing that Q is indeed V-rewritable.For this purpose, we first analyse to which atoms in α i (W ) the atoms of a set A + i are mapped to and then associate views V + j with (subsets of) the sets A + i .We can assume that the α i fulfil the quantified variable disjointness.Note, that k ≥ 2 since C Q V is not minimal, and u is a bridge variable of each set A + i since it occurs in every atom in A + Q .Therefore, no atom in A + i can be mapped into the copy of α i (A + Q ) in α i (W ) because the variable u is not a head variable of W . Hence, A + i is a subset of α i (body(V 1 ) v 1 ) ∪ . . .∪ α i (body(V n ) vn ) ∪ {α i (S(v n+1 ))}.
Since the sets α i (body(V j ) v j ) do not share any variable that is not in the head(α i (W )), each cover description in C ′ can be partitioned into cover descriptions (B + i,1 , W, α i , ψ i ), . . ., (B + i,n , W, α i , ψ i ) for Q ′ where B + i,j = A + i ∩ α i (body(V j ) v j ) and, in case S(u) ∈ A + i , a cover description ({S(u)}, W, α i , ψ i ).For the sake of readability we assume that B + i,j ̸ = ∅ holds for all i, j.If not, the respective cover descriptions can just be removed from the sequence.
Note that the view W in a cover description (B + i,j , W, α i , ψ i ) can be replaced by V + j , because B + i,j ⊆ α i (body(V + j )) by definition.Hence, each cover description (B + i,j , W, α i , ψ i ) can be transformed into a cover description (B + i,j , V + j , α i,j , ψ i,j ) where α i,j and ψ i,j are the restriction of α i and ψ i resp.on vars(V + j ).A cover description (B + i,j , V + j , α i,j , ψ i,j ) can, in turn, be transformed into a cover description (B i,j , V j , α i,j , ψ i,j ) for Q where B i,j is the atom set with B v j i,j = B + i,j , and α i,j and ψ i,j are the restriction of α i,j and ψ i,j on vars(V j ).Let C be the collection of cover descriptions we obtain by applying the transformations described above to all cover descriptions in C ′ and removing cover descriptions with atom set {S(u)}.Note, that by construction, the atom sets in C ′ form a partition of body(Q).Thus C ′ is a cover partition and hence, Q is V-rewritable.
Applications of structurally simple queries.It is well known that many problems are tractable for acyclic conjunctive queries but (presumably) not for conjunctive queries in general.Notably, the evaluation, minimisation, and the containment problem are tractable for acyclic queries [Yan81, CR00, GLS01] but NP-complete for the class of conjunctive queries [CM77].
The class of free-connex conjunctive queries plays a central role in the enumeration complexity of conjunctive queries.In [BDG07], Bagan, Durand and Grandjean showed that the result of a free-connex acyclic conjunctive query can be enumerated with constant delay after a linear time preprocessing phase.Moreover, they also showed that the result of an acyclic conjunctive query without self-joins that is not free-connex cannot be enumerated with constant delay after a linear time preprocessing, unless n × n matrices can be multiplied in time O(n 2 ).
Hierarchical queries play a central role in different contexts.On the one hand, Dalvi and Suciu [DS07] showed that the class of hierarchical Boolean conjunctive queries without self-joins characterises precisely the Boolean CQs without self-joins that can be answered in polynomial time on probabilistic databases.This has been extended by Fink and Olteanu [FO14] to the notion of non-Boolean queries and queries with negation.On the other hand, Koutris and Suciu [KS11] studied hierarchical join queries in the context of query evaluation on massively parallel architectures.We refer to [FO16] for further applications of hierarchical queries.
The notion of q-hierarchical queries has played a central role in the evaluation of conjunctive queries under single tuple updates [BKS17].In [BKS17] it is shown that the result of a q-hierarchical conjunctive query can be evaluated (by answering a Boolean CQ in constant time, enumerating the result of a non-Boolean CQ with constant delay, or outputting the number of results in constant time) with constant update time after a linear time preprocessing.Moreover, they showed that the result of a conjunctive query without self-joins that is not q-hierarchical cannot be evaluated with constant update time after a linear time preprocessing, unless some algorithmic conjectures on Online Matrix-Vector multiplication (see [HKNS15] for more information about the conjecture) do not hold.The notion of q-hierarchical queries is also related to factorised databases [Kep20].The notion of factorised databases has already been considered in various contexts [OZ15,BKOZ13,SOC16].A further recent source of information on structurally simple queries is [KNOZ20].

Conclusion
We studied rewritability by acyclic queries or queries from CCQ, HCQ, or QHCQ.Based on a new characterisation of (exact) rewritability, we showed that acyclic queries have acyclic rewritings, if they have any CQ rewriting.The same holds for the other three query classes.
We showed that for acyclic queries and views the decision problem, whether an acyclic rewriting exists, is intractable, even for schemas with bounded arity, but becomes tractable if views have a bounded arity (even with unbounded schema arity) or are free-connex acyclic.
We leave the case of free-connex acyclic views and unbounded schemas open.Another interesting open question is the complexity of rewriting problems Rewr(V, Q, R) with R ⊊ Q, e.g.Rewr(ACQ, ACQ, QHCQ).So far we have only NP-hardness results for problems Rewr(V, Q, ACQ) and Rewr(V, Q, HCQ) where V and Q encompass all acyclic or all hierarchical queries, respectively.
Finally, it would be interesting to study whether our results can be extended to other classes of queries that can be evaluated efficiently like conjunctive queries of bounded treewidth.
A Characterisation of Acyclic Rewritability for Acyclic Queries.

Figure 1 .
Figure 1.Structure of the query Q and the view V , induced by formula f .
Proposition 3.3 [NSV10, Proposition 5.1].Let Q be a conjunctive query and V a set of views.If there is a V-rewriting of Q, then the canonical candidate canon(Q, V) is such a rewriting.