Datalog Rewritings of Regular Path Queries using Views

We consider query answering using views on graph databases, i.e. databases structured as edge-labeled graphs. We mainly consider views and queries specified by Regular Path Queries (RPQ). These are queries selecting pairs of nodes in a graph database that are connected via a path whose sequence of edge labels belongs to some regular language. We say that a view V determines a query Q if for all graph databases D, the view image V(D) always contains enough information to answer Q on D. In other words, there is a well defined function from V(D) to Q(D). Our main result shows that when this function is monotone, there exists a rewriting of Q as a Datalog query over the view instance V(D). In particular the rewriting query can be evaluated in time polynomial in the size of V(D). Moreover this implies that it is decidable whether an RPQ query can be rewritten in Datalog using RPQ views.


Introduction
We consider the problem of answering queries using views on graph databases.Graph databases are relational databases where all relation symbols are binary.In other words a graph database can be viewed as an edge-labeled directed graph.
Graph-structured data can be found in many important scenarios.Typical examples are the semantic Web via the format RDF and social networks.Graph-structured data differs conceptually from relational databases in that the topology of the underlying graph is as important as the data it contains.Usual queries will thus test whether two nodes are connected and how they are connected [4].
In many contexts it is useful to know whether a given set of queries can be used to answer another query.A typical example is the data integration setting where data sources are described by views of a virtual global database.Queries over the global database are then rewritten as queries over the views.Another example is caching: answers to some set of queries against a data source are cached, and one wishes to know if a newly arrived query can be answered using the cached information, without accessing the source.This problem also finds application in the context of security and privacy.Suppose access to some of the information in a database is provided by a set of public views, but answers to other queries are to be kept secret.This requires verifying that the disclosed views do not provide enough information to answer the secret queries.
All these problems can be phrased in terms of views and query rewriting using views, which is a typical database problem, not specific to graph databases, that has received considerable attention (see [12,13,3] among others).When graph databases are concerned, the difference lies only in the kind of queries under consideration [6,8,7,9].
Over graph databases, typical queries have at least the expressive power of Regular Path Queries (RPQ), defined in [10] (see also the survey [4]).An RPQ selects pairs of nodes connected by a path whose sequence of edge labels satisfies a given regular expression.A view, denoted by V, is then specified using a finite set of RPQs.When evaluated over a graph database D, the view V yields a new graph database V(D) where each V i ∈ V is a new edge relation symbol.
We are interested in knowing whether the view V always provides enough information to answer another RPQ query Q, i.e. whether Q(D) can be computed from V(D) for all databases D. When this is the case we say that V determines Q, and we look for a rewriting of Q using V, i.e. a new query, in some query language, that expresses Q in terms of V. We are then interested in finding an algorithm for evaluating the rewriting, i.e. an algorithm computing Q(D) from V(D).
These two related questions, determinacy and query rewriting, have been studied for relational databases and graph databases.Over relational databases, determinacy is undecidable already if the queries and views are defined by union of conjunctive queries, and its decidability status is open for views and queries specified by conjunctive queries (CQ) [13].Over graph databases and RPQ queries and views, the decidability status of determinacy is also open [7].Determinacy has been shown to be decidable in a scenario where views and queries can only test whether there is a path of distance k between the two nodes, for some given k [3].This scenario lies at the intersection of CQ and RPQ and contains already non trivial examples.For instance the view Path 3 and Path 4 , giving respectively the pairs of nodes connected by a path of length 3 and 4, determines the query Path 5 asking for the pairs of nodes connected by a path of length 5 [3] (see also Example 1 in Section 2).
Clearly when Q can be rewritten in terms of V, the rewriting witnesses that V determines Q.On the other hand determinacy does not say that one can find a rewriting definable in a particular language, nor with particular computational properties.
It is then natural to ask which rewriting language L R is sufficiently powerful so that determinacy is equivalent to the existence of a rewriting definable in L R .This clearly depends on the language used for defining the query and the view.
Consider again the case of Path and it can be shown that there is no rewriting definable in CQ, nor in RPQ (cf.Example 1).In the case of views and queries defined by CQs it is still an open problem to know whether first-order logic is a sufficiently powerful rewriting language.Even worse, it is not even known whether there always exists a rewriting that can be evaluated in time polynomial in the size of the view instance [13], ie.polynomial data complexity.A similar situation arises over graph-databases and RPQ views and queries [7].
It can be checked that in the example above there exists no monotone rewriting of Path 5 (see again Example 1).In particular, as RPQs define only monotone queries, no rewriting is definable in RPQ.Monotone query languages such as CQ, Datalog, RPQ and their extensions are of crucial importance in many database applications.The possibility of expressing rewritings in these languages is subject to a monotonicity restriction.This is why in this paper we are considering a stronger notion of determinacy, referred to as monotone determinacy, by further requiring that the mapping from view instances to query results is monotone.
In the case when views and queries are defined by CQs, monotone determinacy can be shown to be equivalent to the existence of a rewriting in CQ [13].As this latter problem is decidable [12], monotone determinacy for CQs is decidable.
We consider here monotone determinacy for graph databases and views and queries defined by RPQs.We first observe that monotone determinacy corresponds to the notion called losslessness under the sound view assumption in [7], where it was shown to be decidable.We then concentrate on the rewriting problem.
We know that there exist cases of monotone rewritings that are not expressible in RPQ [7] (see also Example 2 in Section 5).We thus need a more powerful language in order to express all monotone rewritings.
It is not too hard to show that if V determines Q then there exists a rewriting with NP data complexity, as well as a rewriting with coNP data complexity.Our main result shows that if moreover V determines Q in a monotone way, there exists a rewriting definable in Datalog, which therefore can be evaluated in polynomial time.
Our proofs are constructive, hence the Datalog rewriting can be computed from V and Q.
As a corollary this implies that it is decidable whether a query Q has a rewriting definable in Datalog using a view V, where both V and Q are defined using RPQs.This comes from the fact that our main result implies that the existence of a rewriting in Datalog is equivalent to monotone determinacy, a decidable property as mentioned above.Related work.The work which is most closely related to ours is that of the "Four Italians".In particular, the notion of losslessness under the exact view assumption introduced in [7] corresponds to what we call determinacy; similarly the notion of losslessness under the sound view assumption corresponds to what we call monotone determinacy.Monotone determinacy is also mentioned in the thesis [14] under the name of "strong determinacy".It is shown there that it corresponds to the existence of a monotone rewriting.
A lot of attention has been devoted to the problem of computing the set of certain answers to a query w.r.t a set of views, under the sound view assumption (see the precise definition of certain answers in Section 6.1).For RPQ views and queries, the problem is shown to be equivalent to testing whether the given instance homomorphically embeds into a structure T Q,V computed from the view V and the query Q [6].In general this shows that the data complexity of computing the certain answers is coNP-complete.Building on results on Constraint Satisfaction Problems [11], it was also shown in [6] that for an RPQ view V, an RPQ query Q and for each l, k, with l ≤ k, there is a Datalog program Q l,k which is contained in the certain answers to Q given V and is, in a sense, maximally contained: i.e.Q l,k contains all Datalog programs which are contained in the certain answers and use at most l head variables and at most k variables in each rule.
If we assume that V determines Q in a monotone way, it is easy to see that the query computing the certain answers under the sound view assumption is a rewriting of Q using V (i.e the certain answers of a view instance V(D) are precisely the query result Q(D)).
However there are possibly other rewritings (they only need to agree on instances of the form V(D), but may possibly differ on instances not in the image of V.) While computing the certain answers is coNP-hard, our main result shows that there exists another rewriting which is expressible in Datalog, and has therefore polynomial time data complexity.
Nevertheless our proof makes use of the structure T Q,V mentioned above, and our Datalog rewriting turns out to be the query Q l,k associated with Q and V for some suitable values of l and k.

Preliminaries
Graph databases and paths.A binary schema is a finite set of relation symbols of arity 2. All the schemas used in this paper are binary.A graph database D is a finite relational structure over a (binary) schema σ.We will also say a σ-structure.Alternatively D can be viewed as a directed edge-labeled graph with labels from the alphabet σ.The elements of the domain of D are referred to as nodes.The number of elements in D is denoted by |D|.If A is a set of elements of D, we denote by D[A] the substructure of D induced by A.
Given a graph database D, a path π in D from x 0 to x m is a finite sequence π = x 0 a 0 x 1 . . .x m−1 a m−1 x m , where each x i is a node of D, each a i is in σ, and a i (x i , x i+1 ) holds in D for each i.A simple path is a path such that no node occurs twice in the sequence.The label of π, denoted by λ(π), is the word a 0 a 1 . . .a m−1 ∈ σ * .By abuse of notation, we sometimes view a path π as a graph database, which contains only the nodes and edges that occur in the sequence.
Queries and query languages.A query Q over a schema σ is a mapping associating to each graph database D over σ a finite relation Q(D) over the domain of D. We will only consider binary queries, that is queries that return binary relations, and work with the following query languages.
A Regular Path Query (often abbreviated as RPQ) over σ is given by a regular expression over the alphabet σ.If Q is an RPQ, we denote by L(Q) the language corresponding to its regular expression.On a graph database, such a query selects all the pairs (x, y) of nodes such that there exists a path π from x to y with λ(π) ∈ L(Q).For instance the query Path 3 of the introduction is an RPQ corresponding to the regular expression σσσ (also denoted σ 3 ).Another example is the RPQ (σσ) * that select pairs of nodes connected via a path of even length.
A Context-Free Path Query over σ is defined similarly but using a context-free grammar instead of a regular expression.
A Conjunctive Regular Path Query (sometimes abbreviated CRPQ) over σ is a conjunctive query whose atoms are specified using RPQs over σ.For instance the query where Q 1 = a + , Q 2 = b and Q 3 = c selects pairs of nodes (x, y) which are connected via a path labeled a + b and another path labeled a + c sharing their a + part.This cannot be expressed by an RPQ.
A Datalog query over schema σ is defined by a finite set of rules of the form where each I i is a relational symbol, either a symbol from σ, or an internal symbol.I(x) is called the head of the rule and I must be an internal symbol.The variables x are among x1 . . .xm and the variables of xi not occurring in x should be understood as existentially quantified.One of the internal symbols, referred to as the goal, is binary and is designated as being the output of the query.The evaluation of a Datalog query computes the internal relations incrementally starting from the empty ones by applying greedily the rules (see [2]).
It is easy to see that any Regular or Context-Free Path Query, and therefore any Conjunctive Regular Path Query, can be expressed in Datalog.Hence Datalog is the most expressive of the query languages presented above.It is also well known that each Datalog query can be evaluated in polynomial time, data complexity, using the procedure briefly sketched above.
We will consider restrictions of Datalog limiting the maximal arity of the internal symbols and the number of variables in each rule.This is classical in the context of Constraint Satisfaction Problems (CSP) [11] that we will use in Section 6.In the context of CSP, Datalog programs are boolean (i.e. the goal has arity 0) and Datalog l,k denotes the fragment allowing at most k variables in each rule and internal symbols of arity at most l.Here we are dealing with binary Datalog programs.In order to stay close to the notations and results coming from CSP, we generalize this definition and let Datalog l,k denote the Datalog programs having at most k + r variables in each rule and internal symbols of arity at most l + r, where r is the arity of the goal, in our case r = 2.
Views.If σ and τ are (binary) schemas, a view V from σ to τ is a set consisting of one binary query over σ for each symbol in τ .If V consists of the queries {V 1 , . . ., V n }, with a little abuse of notation, we let each V i also denote the corresponding symbol in τ .For a graph database D over σ, we denote by V(D) the graph database over τ where each binary symbol V i is instantiated as V i (D).We say that a view consisting of the queries {V 1 , . . ., V n } is an RPQ view if each V i is an RPQ.We define similarly Context-Free Path Query views and Conjunctive Regular Path Query views.
In what follows whenever we refer to a view V and a query Q, unless otherwise specified, we always assume that Q is over the schema σ and V is a view from σ to τ .A view instance E is a τ -structure such that E = V(D) for some database D. Determinacy and rewriting.The notion of determinacy specifies when a query can be answered completely from the available view.The following definitions are taken from [13].
Definition 2.1 (Determinacy).We say that a view V determines a query Q if : In other words, Q(D) only depends on the view instance V(D) and not on the particular database D yielding the view.Observe that determinacy says that there exists a function f defined on view images such that Q(D) = f (V(D)) for each database D. We call f the function induced by Q using V.
A rewriting of Q using V is a query R over the schema τ such that R(V(D)) = Q(D) for all D.
Notice that there can be possibly many rewritings, while the function induced by Q using V is unique.In fact the domain of f is defined to be the set of view images, that is, all the τ -structures E such that there exists a database D with V(D) = E. Thus, f is fully defined by the identity Q(D) = f (V(D)), and is therefore unique.On the contrary, rewritings are defined as queries over τ , which means that they are mappings defined over all τ -structures E, even those which are not of the form E = V(D).In particular, this means that the condition Q(D) = R(V(D)) is not sufficient to fully define R, as it can take arbitrary values on τ -structures that are not of the form V(D). Of course any rewriting coincides with the function f when restricted to view images.
Example 1.Consider again the view V defined by the two RPQs V 1 = σ 3 and V 2 = σ 4 testing for the existence of a path of length 3 and 4, respectively.Let Q = σ 5 be the RPQ testing for the existence of a path of length 5.
It turns out that V determines Q [3].This is not immediate to see but, as mentioned in Section 1, one can verify that a rewriting of Q using V can be expressed in first-order by the following query: As shown in Figure 1, the function induced by Q using V is not monotone.This implies that no monotone query can be a rewriting, in particular there exists no CQ nor RPQ rewriting.
Consider now the RPQ Q ′ = σ 2 .One can verify that V does not determine Q ′ .Indeed the database consisting of a single node with no edge, and the database consisting of a single path of length 2, have the same empty view but disagree on Q ′ .
Hence the function induced by Q using V is not monotone.
It is important at this point to understand the difference between determinacy and rewriting.If V determines Q then there exists a rewriting of Q using V. However there are possibly many rewritings of Q using V.Each of them agrees on the function induced by Q using V when restricted to view images, but can take arbitrary values on structures that are not in the image of the view.Consider for instance the view V and the query Q of Example 1.The query: is also a rewriting of Q using V.It is equivalent to the rewriting of Example 1 on τ -instances E such that E = V(D) for some D. Indeed whenever V 2 (x, u) holds in V(D), the database D contains a path of length 4 from x to u, hence if u ′ is the node at distance 3 from x in this path, V 1 (x, u ′ ) also holds in V(D).However the two rewritings may differ on instances which are not in the view image, such as an instance consisting of a single V 2 -labeled edge.
The determinacy problem for a query language L is the problem of deciding, given an input view V defined in L and a query Q of L, whether V determines Q.
Determinacy does not say whether there exists a rewriting definable in a particular query language, or computable with a particular data complexity.This clearly depends on the language used for specifying the views and queries.
The rewriting problem for a query language L is the problem of finding a rewriting for a query Q of L using a view V defined in L whenever V determines Q.
These two problems have been thoroughly investigated in the case that L is RPQ [7,6,8,9].However the determinacy problem for RPQ remains wide open and it is not clear what would be a good (low data complexity) rewriting language for RPQ.Note that a similar situation arises in the case that L is CQ [13,3].

Determinacy problem
We have already mentioned above that the determinacy problem for RPQs is open.For Context-Free Path Queries and for Conjunctive Regular Path Queries, determinacy is undecidable.Actually the problem is already undecidable when the query Q is an RPQ.These undecidability results are formalized in the two following propositions.Proposition 3.1.Given a Context-Free Path Query view V and a Regular Path Query Q, it is undecidable whether V determines Q.
Proof.We prove this by reduction from the universality problem for context-free languages.Let L be a context-free language over some alphabet σ.Let $ be a fresh symbol that does not appear in σ.
• Conversely, assume that L is not universal.Then there exists w ∈ σ * such that w / ∈ L. Consider the database D consisting of a simple path labeled by $ • w • $, and the empty database D ′ .Then V(D) = ∅ = V(D ′ ), but Q(D) contains the first and last node of the path, whereas Q(D ′ ) is empty.Hence, V does not determine Q. Proposition 3.2.Given a Conjunctive Regular Path Query view V and a Regular Path Query Q, it is undecidable whether V determines Q.
Proof.We prove this by reduction from the word problem for graph databases.
Problem : Word problem for graph databases Input : A list of pairs (u i , v i ) 0<i≤n , a pair (u, v), where u, v, u i , v i , for every i are words over σ, viewed as RPQs Question : Is the following statement true?
For every graph database D over σ, if ∀i, A straightforward reduction from the word problem for finite semigroups shows: Lemma 3.3.The word problem for graph databases is undecidable.
Proof.We prove this by reduction from the word problem for finite semigroups.This problem has the same input as the word problem for graph databases but asks whether for all semigroup S and all homomorphism h from σ * to S such that h We now prove that any input is accepting for the word problem for finite semigroups if and only if it is accepting for the word problem for graph databases.
(1) Assume that the input is accepting for the word problem for finite semigroups.Let D be a graph database such that for all i, u i (D) = v i (D).From D, we compute the semigroup S D and the homomorphism h : σ * → S D as follows: • The elements of S D are the set of pairs w(D) for all w ∈ σ * .As D is finite S D is finite.
• Let x and y be two elements of It is easy to check that this operation is associative and well defined (i.e.does not depend on the specific choice of u and v).• For all α ∈ σ we set h(α) = α(D).Hence for all u ∈ σ * we have h(u) = u(D).By construction we therefore have for all i, h(u i ) = h(v i ).Hence, h(u) = h(v), which implies that u(D) = v(D).
(2) Assume that the input is accepting for the word problem for graph databases.Let S be a finite semigroup, and h an homomorphism from σ * to S, such that, for all i, h(u i ) = h(v i ).From S and h, we define the graph database D h as follows: • The sets of nodes of D h is h(σ + ) ∪ {ε}.This set is finite since h(σ + ) is a subset of S.
• Let x and y be two nodes of D h .Then there is an edge α from x to y if either x = ε and y = h(α) Let (u i , v i ) 0<i≤n and (u, v) be an input for the word problem.Let σ ′ be a copy of σ using only fresh symbols.For each α ∈ σ, we use α ′ to denote the corresponding symbol in σ ′ .We define the following query and view: • For all α ∈ σ, V α is a query of the view defined by the RPQ L α = {α, α ′ }.
• For all i, V i is also a query of the view defined by the RPQ • For all α, β ∈ σ, T α,β is a query of the view defined by the CRPQ: α(x, y) ∧ ∃z, t β ′ (z, t).
• For all α, β ∈ σ, T ′ α,β is a query of the view defined by the CRPQ: α ′ (x, y) ∧ ∃z, t β(z, t).We now prove that only if the input is accepting for the word problem for graph databases.
(1) Assume that the input is accepting for the word problem for graph databases.Let D and D ′ be two graph databases such that V(D) = V(D ′ ).Consider first the case where D uses symbols from both σ and σ ′ , then T α,β and T ′ α,β reveal D entirely, which implies that D = D ′ , and thus Q(D) = Q(D ′ ).Similarly, if both D and D ′ use only symbols from σ, then V α reveals D entirely ensuring that D = D ′ .It remains to consider the case where D only uses symbols from σ and D ′ only uses symbols from σ ′ .Notice that, since V α (D) = V α (D ′ ), then D and D ′ are isomorphic (by renaming each α to α ′ ).
Let (x, y) ∈ u i (D).Hence, (x, y) ∈ V i (D), which implies that (x, y) ∈ V i (D ′ ), and finally that (x, y) ), and thus that (x, y) ∈ Q(D ′ ).A similar reasoning also gives the converse, and we can conclude that V determines Q.
(2) Assume that V determines Q.Let D be a graph database over σ that satisfies the condition for the word problem.Let D ′ be the copy of D given by renaming the symbols in σ by the corresponding symbols in σ ′ .Remark now that given by the fact that D satisfies the condition for the word problem.Finally, and since D ′ is a copy of D, this yields (x, y) ∈ v(D).A similar reasoning also gives the converse, and we can conclude that the input is accepting for the word problem for graph databases.

Views and Rewriting
We have seen in the previous section that knowing whether a given view V determines a given query Q is often computationally a difficult task.In this section we assume that V determines Q and we investigate how Q can be computed from the given view instance.
A possibility is to use the following generic algorithm : As we know that V determines Q this procedure always returns the correct answers on view images.Therefore the query over τ defined by this algorithm is a rewriting of Q using V.
For all the query languages considered in this paper, computing V(D) and Q(D) can be done in time polynomial in |D|.Hence it remains to be able to test whether there exists a D such that V(D) = E and, if yes, compute such a D.
The first issue, testing whether a τ -instance is in the image of the view, is already a challenging task and will be investigated in the next section.We start with the second problem, i.e. computing a D such that V(D) = E, if it exists.4.1.Looking for a view preimage.We assume in this section that V is a view from σ to τ and that we are given a τ -structure E that is in the image of V. We are now looking for a D such that V(D) = E, knowing that one such D exists.Our first result below shows that for RPQ views, if such a D exists then there is one whose size is polynomial in |E|.It is essentially a pumping argument.Lemma 4.1.Let V be an RPQ view from σ to τ .Let E be a τ -structure.If E = V(D) for some D then E = V(D ′ ), for some D ′ of size quadratic in |E|.
Proof.Let V and E be as in the statement of the lemma.We show that if there exists D such that E = V(D) then there exists a new database D ′ of size O(|E| 2 ) such that V(D ′ ) = V(D).D ′ is obtained from D in several steps.First D is "normalized", without altering its view, so that nodes not occurring in E appear in only one path linking two nodes of E. The normalized D turns out to consist of a constant number of disjoint paths between each pair of nodes of E (where the constant only depends on the size of the view automaton).Then a Ramsey argument is used to show that these paths can be "cut" without changing the view.The resulting database D ′ thus consists of a constant number of paths of constant length between each pair of nodes of E. The size of D ′ is therefore O(|E| 2 ).We now formalize this argument.
Assume that there exists a database D such that E = V(D).We prove the lemma by constructing a new database Let A = S V , δ V , q 0 V , F V be the product automaton of all the deterministic minimal automata of all the regular expressions of the RPQs in V. Let N (V) be the number of states of A, i.e In what follows, for w ∈ σ * , δ V (•, w) denotes the function from S V to S V sending q to p such that there is a run of A on w starting in state q and arriving in state p.
We say that a path π from u to v in a database D ′ is V-minimal if u, v are elements of V(D ′ ) and no other nodes of π are in the domain of V(D ′ ).
We first build a database D 1 such that : D 1 is constructed as follows: All elements of V(D) are elements of D 1 .Moreover, for each function f : S V → S V and each pair (x, y) of elements of V(D), if there exists a V-minimal path π from x to y in D and such that f = δ V (•, λ(π)), then we add to D 1 a copy of π that uses only fresh, non-repeating nodes, except for x and y. Figure 2 illustrates the main idea of this construction.
It is now easy to check that D 1 has the desired properties.The second bullet holds by construction.Clearly the number of f : S V → S V is bounded by N (V) N (V) hence the third bullet holds.It remains to check that V(D 1 ) = V(D).There is an obvious canonical homomorphism sending D 1 to D. Hence V(D 1 ) ⊆ V(D).For the converse direction, consider a path π witnessing the fact that (u, v) ∈ V(D).Decompose π into V-minimal paths.By construction, each of these V-minimal paths can be simulated in D 1 .Hence (u, v) ∈ V(D 1 ).
From D 1 we construct the desired D ′ by replacing each V-minimal path of D 1 by another one whose length is bounded by a constant r and without affecting the view image.Altogether D ′ will have a size bounded by r Let r be the Ramsey's number that guarantees the existence of a monochromatic 3clique in an r-clique using Consider a V-minimal path π = xa 0 x 1 a 1 . . .x m a m y in D 1 such that m > r.For 1 ≤ s < t ≤ m we denote by π s→t the subpath of π that starts at position s and ends at position t, that is π s→t = x s a s x s+1 a s+1 . . .a t−1 x t .
To each pair of nodes (x i , x j ) in π with i < j, we attribute the color (f ij , ∆ ij ) where: Then, by our choice of r, we know that there exist i < j < k such that f ij = f jk = f ik and ∆ ij = ∆ jk = ∆ ik .Let π ′ be the path constructed from π by replacing the subpath π i→k by π j→k .Let D 2 be the database constructed from D 1 by replacing π by π ′ .We now prove that V(D 2 ) = V(D 1 ).As D 2 still has all the properties of D 1 listed above, by repeating this operation until all V-minimal paths have length less than r we eventually get the desired database D ′ .
Let (u, v) ∈ V(D 1 ) as witnessed by a path µ in D 1 .Then µ neither starts nor ends in an internal node of π as internal nodes do not appear in V(D 1 ).Hence either µ does not use π or it uses all of it.In the former case, µ witnesses the fact that (u, v) ∈ V(D 2 ).In the latter, notice that Suppose now that (u, v) ∈ V(D 2 ) as witnessed by a path µ in D 2 .If µ does not go through x j (i.e.x j is not an internal node of µ), it is also a path in D 1 and (u, v) ∈ V(D 1 ).If µ goes through x j but does not end between x j and x k we can also conclude that (u, v) ∈ V(D 1 ) using the fact that f ik = f jk .It remains to consider the case when µ ends with x j a j . . .a β−1 x β for some β with j < β < k (in particular v = x β ).As ∆ ij = ∆ jk there exists α with i < α < j such that δ V (•, λ(π i→α )) = δ V (•, λ(π j→β )).From this we can construct a path µ ′ in D 1 replacing in µ the segment x j a j . . .a β−1 x β by x i a i . . .a α−1 x α , witnessing the fact that (u, x α ) ∈ V(D 1 ), a contradiction as x α is not an element of V(D 1 ).Altogether we have proved that In view of Lemma 4.1, we know that if V determines Q then there exists a rewriting R with NP data complexity.Indeed R is the query computed by the following nondeterministic polynomial time algorithm: on an input τ -structure E, guess from E a database D of polynomial size, check that V(D) = E and then evaluate Q on D. There also exists a rewriting with coNP data complexity, by considering all databases D of polynomial size such that V(D) = E. Altogether we get: Corollary 4.2.Let V and Q be RPQs such that V determines Q.Then there exists a rewriting of Q using V with NP data complexity, and another one with coNP data complexity.
It is not known whether, for RPQ views and queries, determinacy implies the existence of a rewriting with polynomial time data complexity.The complexity bounds of Corollary 4.2 are the current best known bounds.We will see in the next sections that if we further assume that the function induced by Q using V is monotone then there exists a rewriting of Q using V definable in Datalog and therefore computable in polynomial time.
x 1 x 2 x 3 x 4 D : Using a more intricate pumping argument it is possible to show that for any Conjunctive Regular Path Query view V, the fact that a view instance is in the image of V can also be witnessed by a database of polynomial size.Hence Corollary 4.2 extends to Conjunctive Regular Path Queries.
However we will see that for Context-Free Path Query views there is no recursive bound on the size of a database yielding a given view instance.This will follow from Lemma 4.5 showing that, for Context-Free Path Query views, checking whether a view instance is in the image of the view is undecidable.

4.2.
Testing for view images.We now consider the following problem.We are given a view V from σ to τ and a τ -structure E and we are asking whether there exists a σ-structure Note that this problem is related to the previous one.In view of Lemma 4.1 we immediately get an NP algorithm for testing membership in the image of an RPQ view V: on input E guess a database D of size polynomial in E and check V(D) = E.We will see that testing for view images is NP-hard for Regular Path Query views and undecidable for Context-Free Path Query views.
Moreover one can show that if testing for view images can be done in PTime then, for Q and V such that V determines Q, then there exists a rewriting of Q using V with polynomial time data complexity.The polynomial time algorithm works as follows.On a view instance E, it first tests whether there exists a database D such that E = V(D).If not it rejects.If yes, consider the schema adding two new letters a and b and consider the query Q a,b asking for a path in the language a where V a and V b return all pairs of nodes linked by a and b respectively.For each pair (x, y) of nodes of E, let E ′ be E expanded with the empty relation for Q a,b , a single pair (u, x) for V a and a single pair (y, v) for V b where u and v are two new nodes.We then test whether E ′ is a view image.A simple argument shows that the test says yes iff (x, y) ∈ Q(D) and the algorithm works in time polynomial in the size of E.
Unfortunately, as already mentioned, the test for view images is NP-hard already for RPQ views.

Lemma 4.3.
There is an RPQ view V from σ to τ such that given a τ -structure E it is NP-hard to test whether there exists a σ-structure D such that V(D) = E.
Proof.We reduce 3-Colorability to our problem.The proof is a simple variation of the reduction found in [5] to prove that computing certain answers under the sound view assumption is coNP-hard in data complexity.
Let σ = {rg, gr, bg, gb, rb, br} and τ = {V 1 , V 2 }.By abuse of notation, we will refer to an element of σ as αβ, with α and β two symbols in {r, g, b}, and α = β.Let V be the following view from σ to τ : Let G = (U, W ) be a connected graph.From G we define a τ -structure E G , in which the interpretation of V 1 is: and the interpretation of V 2 is the empty relation.
We show that G is 3-colorable iff there exists D such that V(D) = E G .Intuitively, the idea is that σ describes the colors of the edges of G, that is the color of the two end points of each edge.For instance, if x and y are linked by rg, then it should be understood that x is red and y is green.V 1 checks that each pair of nodes that are connected in G are colored with (at least) two different colors, and V 2 checks if there is any error, that is, if a node is required to have more than one color.Since V 2 is empty, any graph database D such that V(D) = E cannot have any such error, and would thus be 3-colorable.
More precisely, assume that G is 3-colorable.Then there exists a coloring function c : U → {r, g, b} such that c(x) = c(y) for all (x, y) ∈ W .We define D as the σ-structure such that, for each αβ ∈ σ, the interpretation of αβ in D is: It is then easy to check that V(D) = E G .Indeed, for all x, y, z ∈ D, if α 1 β 1 (x, y) and α 2 β 2 (y, z) hold in D, then , so V 2 (D) is empty.Conversely, assume that there exists a graph database D such that V(D) = E G .Consider the coloring function c : U → {r, g, b} defined as: c(x) = α if there exists y such that αβ(x, y) holds in D. Since V 2 (D) is empty, it is immediate to check that c(x) is uniquely defined and that c is a proper 3-coloring of G.
If we go from regular languages to context-free ones, then the problem becomes undecidable.
Lemma 4.4.Let V be a Context-Free Path Query view from σ to τ .Let E be a τ -instance.Then it is undecidable whether there exists a σ-structure D such that V(D) = E.
Proof.We prove this by reduction from the universality problem for context-free languages.Let L be a context-free language over some alphabet σ.Let $ be a fresh symbol that does not appear in σ.
Finally, let E be the view instance that contains a single pair (x, y) in V 2 and no pair in V 1 .Then there exists D such that V(D) = E if and only if L is not universal over σ.
• Assume that there exists a database D such that V(D) = E. Then there exists a path π from x to y such that λ(π) ∈ L(V 2 ).Hence there exists w ∈ σ * such that λ(π) = $ • w • $.However, λ(π) / ∈ L(V 1 ).Hence w / ∈ L and L is not universal.• Conversely, assume that L is not universal.Then there exists w ∈ σ * such that w / ∈ L. Then it is easy to check that the database D consisting of a simple path labeled by $ A more intricate argument shows that undecidability already holds for a fixed view definition V. Lemma 4.5.There exists a fixed Context-Free Path Query view V from σ to τ such that, given a τ -structure E, it is undecidable whether there exists a σ-structure D such that V(D) = E.
Proof.Let σ = {(, ; , ), a, b, $, 1}.Let σ be a copy of σ with fresh symbols.For α ∈ σ, we denote by α the corresponding symbol in σ.For w a word, w denote the word corresponding to w read from right to left.V consists of views that reveal each symbol in σ, that is, for all α ∈ σ, V contains a view V α defined by L(V α ) = {α}.Additionally, V contains the queries V u , V v , V ′ u , V ′ v , V g and V c defined by the following equations:  where ϕ is the function that maps each symbol in σ to the corresponding symbol in σ.
One can check that all these languages are actually context-free languages.We now prove that, given a view instance E for this specific view V, it is undecidable whether there exists a database D such that V(D) = E.We prove this by reduction from the Post Correspondence Problem (PCP).Let (u i , v i , i) 0<i≤n be an instance of PCP over {a, b}, where the third argument explicitly gives the index of each pair.We build the following view instance E:

PCP encoding solution encoding
We now show that there exists D such that V(D) = E if and only if the PCP instance is satisfiable.Intuitively, E consists of two parts.The first part, from x 0 to x n+1 is the encoding of the PCP instance.It uses letters from σ that are all revealed by the view.All tuples are simply enumerated in the natural order, where the ith tuple is encoded between x i and x i+1 .The dashed arrows V u i and V v i represent the correct succession of V a and V b that naturally encode u i and v i , whereas the V i 1 part is the unary encoding of i, the index of the tuple.The second part of the instance states the existence of a solution for this instance, and uses "hidden" letters from σ. V u and V v states that there exists a solution, and the fact that all other views are empty checks that this solution is correct.
• Assume that there exists a database D such that V(D) = E. Then there exists a path π from x n+1 to x end such that λ(π) ∈ L(V u ).Hence, this path is of the form $ is not the mirror of some tuple encoded in the first half of the instance.Remark that |i i | ≤ n.Otherwise, there exists a path whose label is in L(V g ), which leads to a contradiction.Hence, either Both cases lead to the existence of a path whose label is in L(V c ), and thus to a contradiction.
• Assume that there exists a solution i 1 . . .i m to the PCP instance.Then the database D that consists of the following simple path is such that V(D) = E: where u i and v i simply represent the corresponding u i and v i written using a and b instead of a and b.
Note that in the proof of Lemma 4.5 the view instance is a coding of a PCP instance and the corresponding database a coding of a solution.As there is no recursive bound on the size of a solution of a PCP instance, for Context-Free Path Query views, there are no recursive bound on the size of a database that yields a given view instance.This is to be compared with the polynomial bound for RPQ views shown in Lemma 4.1.

Monotone determinacy and rewriting
As Example 1 shows, there is an RPQ view V and an RPQ query Q such that V determines Q but the function induced by Q using V is not monotone, therefore having no RPQ rewriting.It is natural to wonder whether the monotonicity of the function induced by the query is the only limit for the existence of an RPQ rewriting.Recall from the introduction that if V and Q are defined using CQs and V determines Q, then the function induced by Q using V is monotone iff there exists a CQ rewriting.In the case of RPQ views and queries the analog does not hold.We will see that, even if we assume monotonicity, an RPQ rewriting need not exist; however in the next section we will show that a rewriting definable in Datalog always exists.We start by formalizing the notion of monotone determinacy.
Definition 5.1 (Monotone determinacy).We say that a view V determines a query Q in a monotone way if V determines Q and the function induced by Q using V is monotone.
It is rather immediate to see that monotone determinacy is equivalent to the following property for V and Q: This turns out to coincide with the notion of losslessness under the sound view assumption defined in [7], that was shown to be decidable, actually ExpSpace-complete, for RPQs.
Note that in the proof of Proposition 3.1, the rewriting is always monotone when the view determines the query.Therefore, for Context-Free Path Query views and RPQ queries, monotone determinacy is undecidable.
Recall from Example 1 that there exist a view and a query such that the view determines the query but not in a monotone way.We now assume given an RPQ view V and an RPQ query Q such that V determines Q in a monotone way.It was observed in [7] that even in this case there might be no rewriting definable in RPQ.
In fact, given V and Q defined using RPQs, it is decidable whether an RPQ rewriting exists and the problem is 2ExpSpace-complete [8].As testing monotone determinacy is ExpSpace-complete, a simple complexity argument shows that an RPQ rewriting is not guaranteed to exist under monotone determinacy.
Here is a concrete example witnessing this fact. 1xample 2. Let σ = {a, b, c}.Let Q and V be defined as follows: One can verify that V determines Q as witnessed by the following rewriting R(x, y): That R is a rewriting is illustrated in Figure 3. Consider the database D of Figure 3 which is a typical database such that (x, y) ∈ Q(D).The choice of z witnessing (x, y) ∈ R(V(D)) is then immediate.Conversely, consider the database D ′ of Figure 3.It is a typical database such that (x, y) ∈ R(V(D)).The top path shows that (x, y) ∈ Q(D).Since R is monotone, V determines Q in a monotone way.It can also be shown (for instance using the decision procedure provided in [8]) that no RPQ rewriting exists.
In the previous example we have exhibited a Conjunctive Regular Path Query rewriting.However the following example suggests that Conjunctive Regular Path Query is not expressive enough as a rewriting language.
Example 3. Let σ = {a}.Let V and Q be defined as follows: (words of length 1 or 2 modulo 6) (words of length 2 or 3) It can be verified that V determines Q in a monotone way as witnessed by the following rewriting R(x, y): ∃z V 1 (x, z) ∧ T * (z, y) where T (x, y) is defined as: The query T is such that if T (x, y) holds in V(D), then in D the nodes x and y are either linked by a path of length 6 or by both a path of length 5 and a path of length 7.This fact can be checked by a simple case analysis.One such case is illustrated in Figure 4.In this case there is no path of length 6 in D, but the top path has length 5, and the path starting with the bottom segment and then the last two top segments has length 7.
From this, a simple induction shows that if T * (x, y) holds in V(D), then in D the nodes x and y are either linked by a path of length 0 modulo 6, or by both a path of length 1 modulo 6 and a path of length 5 modulo 6.
Assume now that R(x, y) holds in V(D).Then in D there exists a z such that x is at distance 1 or 2 from z, and such that T * (z, y) holds in V(D).Assume first that z and y are at distance 0 modulo 6 in D. In this case, regardless of the distance between x and z, Q(x, y) holds in D. Otherwise, in D there exist both a path of length 1 modulo 6 and a path of length 5 modulo 6 from z to y.Therefore, if x and z are at distance 1, the first path from z to y yields a path of length 2 modulo 6 and, if x and z are at distance 2, the second path from z to y yields a path of length 1 modulo 6, see Figure 5.
Conversely, it is easy to check that R(x, y) holds in V(D) whenever Q(x, y) holds in D. This follows from the fact that T (x, y) holds in V(D) for all x and y that are at distance 6 in D.
Notice that R is monotone.A tedious combinatorial argument can show that R cannot be expressed as a Conjunctive Regular Path Query. .Each arrow of the form V i : w from a node u to a node v should be understood as a path from u to v whose label is w which witnesses (u, v) ∈ V i (D).Remark 5.3.The careful reader has probably noticed that in both examples above a rewriting can be expressed in MSO.As we will see later, it easily follows from the results of [6] that this is always true in general: if V and Q are defined by RPQs and V determines Q in a monotone way, then there exists a rewriting of Q using V definable in MSO (actually universal MSO).

Datalog rewriting
In this section we prove our main result, namely: Theorem 6.1.If V and Q are RPQs and V determines Q in a monotone way then there exists a Datalog rewriting of Q using V. Theorem 6.1 also implies that the monotone determinacy problem for RPQs coincides with the problem of the existence of a Datalog rewriting.The latter is therefore decidable by Corollary 5.2: Corollary 6.2.Let V and Q be RPQs.It is decidable, ExpSpace-complete, whether there exists a Datalog rewriting of Q using V.
Our proof being constructive, the Datalog rewriting can be computed from V and Q.
Main idea and sketch of the proof.The starting point is the relationship between rewriting and certain answers under monotone determinacy.One can easily show that if the view determines the query in a monotone way then the certain answers query is a rewriting.However certain answers for RPQ views and queries are coNP-hard to compute [5].
Here we show that there exists another rewriting (which of course coincides with certain answers on view images) that is expressible in Datalog.This other rewriting is suggested by the relationship between certain answers and Constraint Satisfaction Problems (CSP).Following [6] we adopt here the homomorphism point of view for CSPs: Each CSP is defined by a structure, called the template, and its solutions are all the structures mapping homomorphically into the template.
Indeed [6] showed that, for RPQs V and Q, certain answers can be expressed as a CSP whose template depends only on V and Q.It is known from [11] that for every l and k with l ≤ k, and every template, there exists a Datalog l,k query approximating the CSP defined by this template.Even if its Datalog l,k "approximation" does not compute precisely the CSP associated to V and Q, if it is exact on view images, then it is a rewriting.We show that if the view determines the query in a monotone way then there is an l and a k, depending only on V and Q, such that the Datalog l,k approximation is exact on view images.This proves the existence of a Datalog rewriting.This is done in two steps.We first show that there exists a Datalog approximation which is exact on view images of simple path databases.Then we show how to lift this result on all view images.The first step is proved by a careful analysis of the properties of view images of simple path databases.The second steps exploits monotonicity.
We now provide more details.
6.1.Monotone rewritings, certain answers and CSP.Let V be a view from σ to τ and Q be a query on σ-structures.The certain answers of Q on a τ -structure E w.r.t.V are defined as This notion is usually referred to as certain answers under the sound view assumption or open world assumption in the literature [1,9].It is straightforward to check that if V determines Q in a monotone way, the query cert Q,V is a rewriting of Q using V, i.e. cert Q,V (V(D)) = Q(D) for each σ-structure D.
Therefore any language known to express certain answers is a suitable rewriting language under monotone determinacy.
The following proposition, proved in [6], shows that, for RPQ views and queries, certain answers (and therefore rewritings) can be expressed as (the negation of) a CSP.Proposition 6.3 ([6]).Let V be an RPQ view from σ to τ and Q be an RPQ query over σ.There exists a τ -structure T Q,V having a set of distinguished source nodes and a set of distinguished target nodes such that, if V determines Q in a monotone way, the following are equivalent, for each σ-structure D and each pair of nodes u, v of D: (3) V(D) has no homomorphism to T Q,V sending u to a source node and v to a target node. 2  In the sequel, by ¬CSP(T Q,V ) (resp.CSP(T Q,V )) we refer to the set of all triplets (E, u, v) such that E is a τ -structure, u, v are nodes of E and, there is no homomorphism (resp.there is a homomorphism) from E to T Q,V sending u to a source node and v to a target node 3 .In view of Proposition 6.3, if V determines Q in a monotone way, ( Observe that ¬CSP(T Q,V ) naturally defines a binary query associating with each τstructure E the set of all pairs (u, v) of nodes of E such that (E, u, v) ∈ ¬CSP(T Q,V ).By abuse of notation, when clear from the context, we will let ¬CSP(T Q,V ) also denote this binary query.Remark 6.4.The structure T Q,V of Proposition 6.3 can be effectively computed from Q and V. Moreover observe that CSP(T Q,V ) can be expressed in existential MSO.This shows, as mentioned in Remark 5.3, that if V and Q are RPQs and V determines Q in monotone way, then there always exists a rewriting of Q using V definable in (universal) MSO; moreover this rewriting can be effectively computed from Q and V.
It is well known that the certain answers query is a rewriting that can be computed in coNP (this follows for instance from Proposition 6.3).Assuming coNP is not PTime, cert Q,V cannot always be computed in polynomial time, not even under the assumption that V determines Q in a monotone way.Indeed it has been shown [5] that there exists Q and V defined by RPQs such that cert Q,V has coNP-hard data complexity.An easy reduction from this problem shows that the lower bound remains valid if we further assume that V determines Q in a monotone way: Proposition 6.5.There exist an RPQ view V and an RPQ query Q such that V determines Q in a monotone way and it is coNP-hard to decide -given a τ -structure E and nodes We show in the next section that when V determines Q in a monotone way there is another rewriting expressible in Datalog, hence computable in polynomial time.Before we do this we remark that the coNP complexity of cert Q,V can be extended to Context-Free Path Query views and RPQ queries. 2 More precisely [6] further proved that 2. and 3. are equivalent not only for V(D) but for all τ -structures, and even without the assumption that V determines Q in a monotone way.
3 CSP are usually defined as boolean problems, i.e. without the nodes u, v.As RPQ queries are binary, these parameters are necessary for our presentation.The problem CSP(TQ,V), as defined here, can be viewed as a classical CSP problem by extending the signature with two unary predicates, interpreted as the source and the target nodes, as done in [6].
Proposition 6.6.Let V be a Context-Free Path Query view and Q be a RPQ.Then cert Q,V can be evaluated with coNP data complexity.
Proof.Let V be a Context-Free Path Query view, and Q be a RPQ over some schema σ.
We prove that cert Q,V can be evaluated with coNP data complexity by reducing it to the case of regular path views.Let A = S, δ, q 0 , F be a deterministic minimal automaton for L(Q).In what follows, δ(., w) denotes the function from S to S associating to a state p the state reached by A when reading w starting from p.For all V ∈ V, we define the language L V as: We claim that L V is a regular language.To see this recall that for each function f from S to S the language L f defined as L f = {w ∈ σ * | δ(•, w) = f } is regular and notice that L V is a union of such languages.We remark here for later that L V is constructible as soon that it is decidable whether L(V ) ∩ L f is non empty.This is in particular the case when L(V ) is context-free.
We now define a new view Ṽ defined as the RPQ view: Let E be a view instance for V.We define Ẽ as a copy of E where each V relation is replaced by Ṽ .Hence, Ẽ is a view instance for Ṽ.We now show that: and thus cert Q,V (E) can be evaluated in coNP in the size of Ẽ, which is also the size of E.
• Assume that (u, v) ∈ cert Q, Ṽ( Ẽ).Hence, for all D such that Ṽ(D) ⊇ Ẽ, there exists a path π from u to v such that λ(π) ∈ L(Q).Let D be a database such that V(D) ⊇ E. Remark that, for all V ∈ V, L(V ) ⊆ L( Ṽ ).Hence, Ṽ(D) ⊇ Ẽ.Hence, there exists a path π in D from u to v such that λ(π) ∈ L(Q), which means that (u, v) ∈ cert Q,V (E).• Conversely, assume that (u, v) / ∈ cert Q, Ṽ( Ẽ).Hence, there exists a database D such that Ṽ(D) ⊇ Ẽ, but no path from u to v in D satisfies Q.From D, we build a database D ′ as follows: -Start with D ′ as a copy of D.
-For all V ∈ V, for all (x, y) ∈ E, if (x, y) ∈ V , then (x, y) ∈ Ṽ in Ẽ.We pick a path π in D ′ from x to y of label w ′ such that w ′ ∈ L( Ṽ ).Hence, there exists w ∈ L(V ) such that δ(•, w ′ ) = δ(•, w).Then, we add in D ′ a simple path from x to y using only fresh nodes of label w.Hence (x, y) ∈ where each π i is a path that was originally in D and each µ i is a new path using only fresh nodes.Then, for each µ i , there exists a path ρ i in D with the same starting and ending nodes and such that δ(•, λ(µ i )) = δ(•, λ(ρ i )).Hence, we can define a path π of D as π The proposition has the following consequence: Corollary 6.7.Let Q be a RPQ and V be a Context-Free Path Query view such that V determines Q in a monotone way.Then there exists a rewriting of Q using V that can be evaluated with coNP data complexity.
Notice that the proof of Proposition 6.6 and therefore also Corollary 6.7 do not assume that the language defining the views are context-free and work with any language.However, in order to effectively construct the rewriting, it is necessary that the formalism used to define the views has a decidable emptiness test for the intersection with a regular language.6.2.Existence of a Datalog rewriting.We now show that for each RPQ query Q and each RPQ view V such that V determines Q in a monotone way, there exists a Datalog rewriting.
The existence of such a rewriting stems from links between CSPs and Datalog.Recall from Proposition 6.3 that if V determines Q in a monotone way, ¬CSP(T Q,V ), viewed as a binary query, is a rewriting of Q using V.It is known that to each CSP problem (i.e.arbitrary template), one can associate a canonical Datalog l,k program, for each l, k, with l ≤ k.This program can equivalently be described in terms of a two-player game, and can be thought of as a maximal "approximation" of the complement of a CSP problem, in a precise sense (the interested reader is referred to [11] for more details).Our main contribution consists in proving that, for some explicit values of l and k (depending on Q and V), this Datalog l,k approximation is "exact" when restricted to view images (i.e.computes precisely ¬CSP(T Q,V )), and is therefore a rewriting over such instances.
We now present the (l, k)-two-player game of [11], and its correspondence with Datalog.Definition 6.8 ((l, k)-two-player game).Let l, k be two integers, with l ≤ k, let E be a τ -structure and u, v be two nodes of E. The (l, k)-game on (E, T Q,V , u, v) is played by two players as follows: • The game begins with A 0 = ∅ and h 0 being the empty function over A 0 .For i ≥ 0, round i + 1 is defined as follows: • Player 2 responds by giving a homomorphism h i+1 : E[A i+1 ] → T Q,V that coincides with h i on A i ∩ A i+1 and such that h i+1 (u) is a source node and h i+1 (v) is a target node whenever u or v are in A i+1 .Player 1 wins if at any point Player 2 has no possible move.Player 2 wins if she can play forever.
The existence of a winning strategy for Player 1 is expressible in Datalog: Lemma 6.9 ( [11,6]).Let l, k be two integers, with l ≤ k, and Q and V be an RPQ query and an RPQ view.Then there exists a program Q l,k (x, y) in Datalog l,k such that for every graph database E, Q l,k (E) is the set of pairs (u, v) such that Player 1 has a winning strategy for the (l, k)-two-player game on (E, T Q,V , u, v).
Moreover the program in the above lemma can be effectively constructed from T Q,V , and therefore from Q and V.It will be simply denoted by Q l,k when Q and V are clear from the context.
We are now ready to state the main technical result of our paper.
Proposition 6.10.Let V and Q be an RPQ view and an RPQ query such that V determines Q in a monotone way.There exists l such that Q l,l+1 is a rewriting of Q using V.
Theorem 6.1 is an immediate consequence of this proposition.The rest of this section is devoted to proving Proposition 6.10.This is done in two steps.We first prove that there exists l such that Q l,l+1 is a rewriting of Q using V, when restricted to view images of simple path graph databases.We then show that this suffices for Q l,l+1 to be a rewriting of Q using V.
Observe that if there is a homomorphism from a τ -structure E to T Q,V sending u to a source node and v to a target node, then Player 2 has a winning strategy for the (l, k)two-player game on (E, T Q,V , u, v).This strategy consists in always playing the restriction of the homomorphism on the set selected by Player 1.In this sense the program Q l,k is a Datalog l,k under-approximation of the ¬CSP(T We will refer to this property by saying that Q l,k is always sound. The converse inclusion does not necessarily hold.If (u, v) / ∈ Q l,k (E) then Player 2 has a winning strategy, but this only means that she can always exhibit partial homomorphisms from E to T Q,V (sometimes called local consistency checking); this is in general not sufficient to guarantee the existence of a suitable global homomorphism.
However here we are not interested in arbitrary τ -structures, but only structures of the form V(D) for some simple path graph database D. We now show that, thanks to the particular properties of these structures, local consistency checking is sufficient to obtain a global homomorphism, for some suitable l and k = l + 1.In other words, the program Q l,l+1 computes precisely ¬CSP(T Q,V ) on views of simple path graph databases.
The case of simple path graph databases.Proposition 6.11.Let V and Q be an RPQ view and an RPQ query.There exists l such that for every simple path database D from u to v, In particular if V determines Q in a monotone way, (u, v) ∈ Q l,l+1 (V(D)) iff (u, v) ∈ Q(D).
Proof.Let V and Q be an RPQ view and an RPQ query, and let D be a graph database consisting of a simple path from node u to node v. Assume u, v ∈ V(D).
We will show, in Lemma 6.12 below, that for large enough l, if Player 2 has a winning strategy on the game on (V(D), T Q,V , u, v) then we can exhibit a homomorphism witnessing the fact that (V(D), u, v) ∈ CSP(T Q,V ).Before that we prove crucial properties of V(D) which will be exploited in the sequel.For that we need the following simple definitions and claims.
Let D consist of the simple path π = x 0 a 1 x 1 . . .x m−1 a m x m , with x 0 = u and x m = v.Moreover let E = V(D) and let A = S V , δ V , q 0 V , F V be the product automaton of all the deterministic minimal automata of all the regular expressions of the RPQs in V. Let N (V) be the number of states of A, i.e. |S V |.
In what follows, for q ∈ S V and w ∈ σ * , δ V (q, w) denotes the state p ∈ S V such that there is a run of A on w starting in state q and arriving in state p.
For every k ≤ m + 1, and every i, j ≤ k, we say that x i ∼ k x j in V(D) if, for all V ∈ V, for all r ≥ k, (x i , x r ) ∈ V (D) ⇔ (x j , x r ) ∈ V (D) For all k, the relation ∼ k is an equivalence relation over {x i | i ≤ k}.We now prove the main property of V(D), namely that the index of all ∼ k is bounded by the size of V.
• On round l + 1, Player 1 plays A l+1 = {s 1 , . . ., s l+1 }.Player 2 has to respond with a partial homomorphism h l+1 , which she can do, since she has a winning strategy.• Assume that, on round i, A i is of size l + 1 and its element of biggest index is s i (as it is the case on round l + 1).Given the choice of l, the set A i is sufficiently "big", that is by Claim 4, there exist two elements s j , s k ∈ A i such that s j ∼ i s k , and h i (s j ) = h i (s k ).On round i + 1, Player 1 picks A i+1 = (A i − {s j }) ∪ {s i+1 }.This choice maintains that A i+1 is of size l + 1 and that its element of biggest index is s i+1 .Once again, Player 2 has to respond with a partial homomorphism h i+1 , which she can do.• Following this play, on round r, A r contains s r , the element of biggest index in E. From now on, we no longer care about Player 1's move, that is, we arbitrarily set A i = ∅ for all i > r.We can now define h as follows : Observe that, by definition, the mapping h sends u to a source node and v to a target node (since so do all the h i 's used in the game).It remains to prove that h is an homomorphism from E to T Q,V .We prove by induction on i ≥ l + 1 that : (H 1 ) h is a homomorphism from E[{s 1 , . . ., s i }] to T Q,V .(H 2 ) h coincides with h i on A i .(H 3 ) for all j ≤ i, there exists s ∈ A i such that s j ∼ i s and h(s j ) = h(s).
Inductive case : Assume that there exists i with l + 1 ≤ i < r such that (H 1 ),(H 2 ) and (H 3 ) holds for i; we prove them for i + 1. (H 2 ) Let s ∈ A i+1 .If s = s i+1 , then, by definition, h(s i+1 ) = h i+1 (s i+1 ).Otherwise, s ∈ A i ∩ A i+1 .(H 2 ) for i implies that h(s) = h i (s), and the definition of h i+1 thus yields h i+1 (s) = h i (s) = h(s).Hence, (H 2 ) holds for i + 1. (H 3 ) Let j ≤ i + 1.If j = i + 1, then s j ∈ A i+1 , and the result is obvious.Otherwise, (H 3 ) for i implies that there exists s ∈ A i such that s j ∼ i s and h(s j ) = h(s).From Claim 5, we deduce that s j ∼ i+1 s.If s ∈ A i+1 , there is nothing more to prove.Otherwise, it means that s is exactly the element that was removed from A i on round i + 1, which means that there exists another element s ′ ∈ A i ∩ A i+1 such that s ∼ i s ′ and h i (s) = h i (s ′ ).Then Claim 5 and (H 2 ) imply that s j ∼ i+1 s ′ and h(s j ) = h(s ′ ).
Hence (H 3 ) holds for i + 1. (H 1 ) By definition, h already preserves any self-loop.Moreover, (H 1 ) for i implies that h is a homomorphism from E[{s 1 , . . ., s i }] to T Q,V .Hence, any edge between two elements of {s 1 , . . ., s i } in E is already preserved by h.Let s j ∈ {s 1 , . . ., s i }.Remark that, since π is a simple path, there are no edges from s i+1 to s j in E. Thus, we just have to prove that all edges from s j to s i+1 are preserved by h.(H 3 ) for i + 1 implies that there exists an element s ∈ A i+1 such that s j ∼ i+1 s and h(s j ) = h(s).Since h i+1 is a homomorphism on E[A i+1 ], it preserves all edges from s to s i+1 .Moreover, (H 2 ) for i + 1 implies that h and h i+1 coincide on A i+1 , which means that h preserves all edges from s to s i+1 .Finally, the definition of ∼ i+1 implies that s j and s have the same edges to s i+1 .Hence, h preserves all edges from s j to s i+1 .traversing an edge of label a.This query language has been studied in [9].In particular [9] gives an extension of Corollary 5.2 and of Proposition 6.3 for 2-way-RPQ.Building from these two results it is possible to extend the results of Section 6 to 2-way-RPQs.The details are more complicated and omitted here, but the general idea is the same.
We may wonder whether a simpler query language than Datalog could suffice to express monotone rewritings of RPQ queries using RPQ views.For instance all examples we are aware of use only the transitive closure of binary Conjunctive Regular Path Queries.It is then natural to ask whether linear Datalog (where at most one internal predicate may occur in the body of each rule), using internal predicates of arity at most 2, can express all monotone rewritings.We leave this interesting question for future work.
Finally we conclude by mentioning that we don't know yet whether the monotone determinacy problem for Conjunctive Regular Path Query is decidable.Likewise, deciding whether an RPQ view determines an RPQ query, without the monotonicity assumption, is still an open problem.

Figure 2 :
Figure 2: Illustration of the transformation from D to D 1 in Lemma 4.1.Nodes are colored white or black depending on whether they appear in V(D) or not.

Figure 3 :
Figure 3: Databases D and D ′ for Example 2.

Figure 4 :
Figure 4: Example 3: An arbitrary database D whose view satisfies T (x, y).Each arrow ofthe form V i : w from a node u to a node v should be understood as a path from u to v whose label is w which witnesses (u, v) ∈ V i (D).

Figure 5 :
Figure 5: The three cases of Example 3. The parts that are not used for Q are shaded out.