Full text
This work is licensed under a Creative Commons “Attribution-NonCommercial-NoDerivatives 4.0 International” license. The Semantics of GQL-like Path Queries ⋆ Fernando Orejas1and Elvira Pino1 Universitat Polit`ecnica de Catalunya, Barcelona, Spain {orejas,pino}@cs.upc.edu Abstract. The success of graph databases (GDBs) has caused the development of several dozens of GDB products by companies like Neo4j, Oracle, Amazon, IBM or SAP. In parallel, several graph query languages have been designed for querying these GDBs, like Cypher for Neo4j, PGQL for Oracle, or GSQL for TigerGraph, which actually are quite similar. This situation has led to propose the creation of a unifying standard for graph query languages. This standard is currently under development, and is called GQL. In a previous paper we defined a logical and an operational semantics for a core language similar to Cypher, showing that the operational semantics is sound and complete with respect to the logical one. However, GQL has gone beyond the expressive power of the above mentioned languages in several senses. Therefore, in the current work, our aim is to redefine the semantics that we gave for Cypher-like languages to a core subset of GQL. Keywords: Graph Databases ·Query Languages ·Paths 1 Introduction The success of graph databases (GDBs) has caused the development of several dozens of GDB products by companies like Neo4j, Oracle, Amazon, IBM or SAP. In parallel, several graph query languages have been designed for querying these GDBs, like Cypher [7] for Neo4j, PGQL [10] for Oracle, or GSQL for TigerGraph [4], which actually are quite similar. This situation has led to propose the creation a unifying standard for graph query languages. This standard is currently under development, and is called GQL [6,5]. However, GQL has gone beyond the expressive power of the above mentioned languages in several senses. In particular, in Cypher and the rest of languages, queries are defined using patterns of the form p: (x1 α =⇒x2), meaning that we are looking for a path p, between nodes x1and x2, whose shape is specified by a regular expression α over the set of edge types. For instance, we would write p: (“Barcelona”flight+ =⇒ “LosAngeles”) to ask for connections, consisting of a sequence of flights, that would take me from Barcelona to Los Angeles. This kind of patterns are called Regular Path Patterns (RPQs). However, in GQL we can define patterns that are ⋆This work is partially supported by MCIN/ AEI /10.13039/501100011033 under grant PID2020-112581GB-C21.
This work is licensed under a Creative Commons “Attribution-NonCommercial-NoDerivatives 4.0 International” license. 2 F. Orejas and E. Pino not RPQs, that is, we can use patterns p:(x1 α =⇒x2), where αis not a regular expression. Actually, it may define a non-regular language. The reason is that, in addition to the possibility of defining a path pattern of the form p:(x1 α =⇒x2), we can also define new patterns by means of path expressions, using operations of concatenation, union, and iteration. For instance, using concatenation, we can define the pattern p′=p·p. Notice that, in this case, the language of edge types specified by p′, i.e. {a1. . . aka1. . . ak|a1. . . ak∈ L(p)}, is not regular. In [9], we addressed the definition of a logical and an operational semantics for a core query language similar to Cypher, i.e., based on the use of RPQs, showing that the operational semantics was sound and complete with respect to the logical semantics. The aim was to show that, defining an adequate logical semantics, we may be able to verify the implementation of a query language. Now, our aim is to redefine these semantics for the case of the kind of path patterns in GQL. In particular, the redefinition of the operational semantics is not obvious, since the semantics defined in [9] was defined in terms of an unfolding operation, which is tailored to paths described by regular languages, and that, in principle, would not work for context-free languages. The paper is organized as follows. After this introduction, we briefly introduce our query language and we present a running example that we will use to introduce our graph model and our language of queries. Then, in Section 3, we present some basic concepts related to the data model that we use. In Section 4 we define the logical semantics of queries, in Section 5, their operational semantics, and in Section 6 we show their equivalence. Finally, in Section 7, we describe related work and draw some conclusions. 2 The language of queries In this section we first describe our query language, including path patterns defined by expressions over simpler path patterns. Then we present a running example that we will use along the paper. The main ingredients of our queries are: – Variables: A query, may have node, edge and path variables to be matched to specific nodes, edges and paths in the database. Node and edge variables are denoted x,x′,x1,x2,. . . ,y,y′,y1,y2,. . . , while path variables are denoted p,p′,p1,p2,. . . . In general, nodes and edge variables are typed, e.g., x1:Airport or y:F light. In particular, node types will be denoted nt, nt1,nt2,. . . ; and edge types et,et1,. . . . However, in some occasions we may not know or we may not care about the type of some element x, in this case we will write x:⊥(or just x), where ⊥is considered as a wildcard that may match any type in the graph. – Patterns: Queries include patterns to specify what are we searching in the database. If ΦPis a set of constraints on the values of the properties of nodes or edges, used to restrict the search space, like x.city =“Barcelona”, y.airline =“IB”, a pattern Pmay be of the form:
This work is licensed under a Creative Commons “Attribution-NonCommercial-NoDerivatives 4.0 International” license. The Semantics of GQL-like Path Queries 3 •node pattern P= (x:nt)□ΦP. •edge pattern P= (x1:nt1 y:et −→ x2:nt2)□ΦP. •atomic path pattern P=p: (x1:nt1 α =⇒x2:nt2)□ΦP, where αis a regular expression over the alphabet of edge types, e.g., Flight+. •path pattern P=p:p1·p2, where p1, p2are path variables. For instance, (x:Airport)□x.city =“Los Angeles” specifies that we are looking for airports of Los Angeles. Similarly, (x1:Airport y:F light −→ x2: Airport)□y.airline =“IB” specifies that we are looking for edges yof type Flight from node x1to node x2, both of type Airport, such that the flights must be operated by the company “IB”. Then, p: (x1:Airport F light+ =⇒x2: Airport)□x1.city =“Los Angeles”, x2.city =“Barcelona” is a path pattern, stating that we are looking for sequences of flights going from airport of Los Angeles to an airport of Barcelona. Finally, p:p1·p2specifies that we are looking for a path that starts by the path bound to p1and continues by the path bound to p2; and p:p∗ 1states that we are looking for a path consisting of zero or more repetitions of the path bound to p1. – Queries: A query qhas the form P1,...,Pn,n≥0, i.e., a conjunction of patterns specifying what are we looking in the database. 2.1 A running example Our running example is a graph database that describes the flight connections between a number of airports, as in Fig. 1. (x1:Airport)□x1.city = “Los Angeles” p1: (x1:Airport F light+ =⇒x2:Airport)□x2.code = “BCN” p2: (x2:Airport F light+ =⇒x1:Airport) p:p1·p2 The first pattern in the query is a node pattern, and just says that we want to find airports in Los Angeles. The second and third ones are atomic path patterns and ask for a path p1, consisting of a sequence of flights from Los Angeles airports to the BCN airport nearby Barcelona1, and for a return path p2. Finally, the last pattern is a path pattern defining pas the concatenation of p1and p2. 3 Preliminaries Our data model in this paper is the standard notion of property graph. In [9], we defined a data model of typed property graph, that we considered more adequate 1Notice that in the GDB there is also the airport with code GRO associated to the city of Barcelona. The number added to the airport code identifies the terminal (for instance, BCN1 and BCN2).
This work is licensed under a Creative Commons “Attribution-NonCommercial-NoDerivatives 4.0 International” license. 4 F. Orejas and E. Pino Fig. 1. A fragment of the graph database used as a running example than the standard model, in particular including a notion of graph schema. However, since we are just currently interested on upgrading our semantics to the case of GQL patterns, we prefer to work with a data model that is technically simpler. We assume that we have a given family of data domains D={Dvt}vt∈V T , indexed by a set of value types, V T , including all data values that can be stored in the given database as properties of nodes or edges. For instance, V T probably includes the types int, bool and string, but perhaps other more complex data values like list of int. So that, e.g. Dint is the set of integers and Dbool ={true,false}. Moreover, we assume that we have several pairwise disjoint countable sets of identifiers: Nfor nodes, Efor edges, Kfor properties and Tfor types. Let us first define the notion of property graph which is the kind of base model that we consider for graph databases: Definition 1. Aproperty graph Gover Dis a tuple G= (N, E, P, srcG, tgtG, ρG, τ), where: –N⊂ N is a finite set of identifiers for nodes; –E⊂ E is a finite set of identifiers for edges; –P⊂ K is a finite set of identifiers for properties; –src, tgt :E→Nare functions that map each edge to its source and target nodes, respectively. –ρ: (N∪E)→2(P×D)is a function that maps every node or edge to a set of pairs consisting of a property and a value.
This work is licensed under a Creative Commons “Attribution-NonCommercial-NoDerivatives 4.0 International” license. The Semantics of GQL-like Path Queries 5 –τ:N∪E→2Tis a function that maps each node and edge in Gto their set of type identifiers. Apath in a graph is an alternating sequence of nodes and edges n1e1. . . nkeknk+1, such that for every i(0 < i ≤k) we have src(ei) = niand tgt(ei) = ni+1. Given two paths s=n1e1. . . nkeknk+1 and s′=n′ 1e′ 1. . . n′ je′ jn′ j+1, we say that they are composable if nk+1 =n′ 1. Then, their composition is s·s′=n1e1. . . nkeknk+1 e′ 1. . . n′ je′ jn′ j+1. In what follows Paths will denote the set of paths in a given graph G. 3.1 Disjunctive decomposition of regular expressions and path unfolding We assume that the reader is familiar with regular expressions over an alphabet A. In particular, R(A) denotes the set of all regular expressions over Aand L(α) denotes the language defined by α∈ R(A), i.e. the set of sequences a1· · · an∈ A∗ that match α. Then, given expressions αand α′,α′is more general than α, denoted α≤α′, if L(α)⊆ L(α′) and they are equivalent if α≤α′and α′≤α. In particular, we denote by ⊤=A+the most general regular expression (excluding the empty sequence). Disjunctive decomposition is an operation on regular expressions allowing us to decompose an expression αinto an equivalent set of expressions, that will be used below to define the unfolding of a path pattern. This property says that for every α∈ R(A), there is a set S⊆ A, and a remainder function rem :S→ R(A), such that it is satisfied that L(α) = {λ} ∪ Sb∈Sb· L(rem(b)), if λ∈ L(α); otherwise, L(α) = Sb∈Sb· L(rem(b)); where λdenotes the empty sequence and b· L(rem(b)) denotes the language obtained as concatenation of b and every sequence in L(rem(b)). For example, given α=a|ab+|ac+|c, we have S={a, c},rem(a) = λ|b+|c+, and rem(c) = λ. Hence: L(a|ab+|ac+|c) = a·L(λ|b+|c+)∪C·{λ}=a·L(λ|b+|c+)∪ {c} So, by iterating this decomposition, we may obtain all sequences in L(α) of length 0, 1, 2, etc. Disjunctive decompositions are finite, provided that Ais finite, and unique up to equivalence of remainders. That is, if (S, rem) and (S′, rem′) are two decompositions of α, then S=S′, and for each a∈S:rem(a)≡rem′(a). Given an atomic path pattern P=p: (x:nt α =⇒x′:nt′)□ΦPwhere αis a regular expression over the alphabet of edge types; and, given a disjunctive decomposition of α, (S, rem), we say that uis an unfolding of Pif either: 1. u=λ, provided that λ∈ L(α), or 2. u=⟨P1,P2⟩, where P1= (x:nt y:et −→ x′′ :⊥)□ΦP,P2=p′: (x′′ :⊥rem(et) =⇒ x′:nt′)□ΦP,et ∈S,y, x′′, p′are fresh new variables2. 2They are different from any variable used in the given context.
This work is licensed under a Creative Commons “Attribution-NonCommercial-NoDerivatives 4.0 International” license. 6 F. Orejas and E. Pino U(P) denotes the set of all unfoldings of P. For instance, a possible unfolding of the pattern p1:(x1:Airport F light+ =⇒x2: Airport)□x1.city = “Los Angeles”, x2.code = “BCN” in the running example, might be u=⟨P1,P2⟩, where P1= (x1:Airport y:F light −→ x:⊥)□x1.city = “Los Angeles”, P2=p′:(x:⊥rem(F light) =⇒x2:Airport)□x2.code=“BCN”. 4 Logical semantics of queries In this section we define the semantics of queries by means of a logic whose formulas are queries and whose satisfaction relation tells us if an answer to a query is correct in the given database, where the answer to a query is a matching, i.e. a function that maps node variables to nodes, edge variables to edges, and path variables to paths in the given database. This logical semantics will allow us to check if our operational semantics (or, in general, any implementation) is sound, i.e., if every computed answer is correct, and complete, i.e., if all correct answers are found by the given search procedure. Definition 2 (Matching). A matching mof a query qis a function m: V ar(q)→N∪E∪P aths, where V ar(q)denotes the set of variables in q, and N, E, Paths denote, respectively, the sets of nodes, edges and paths of the given database, GDB, satisfying the following conditions: –For each node variable x:nt ∈V ar(q),nt ∈τ(m(x)). –For each edge variable y:et ∈V ar(q),et ∈τ(m(y)). –For each edge pattern P= (x1:t1 y:t −→ x2:t2)□ΦP∈q,src(m(y)) = m(x1) and tgt(m(y)) = m(x2). –For each path pattern P=p: (x1:nt1 α =⇒x2:nt2)□ΦP∈q, if m(p) = (n1e1· · · nkeknk+1), we have τ(e1)· · · τ(ek)∈ L(α),n1=m(x1)and nk+1 =m(x2). –For each path pattern P=p:p1·p2, we have that m(p1)and m(p2)are composable. For instance, the graph at the left of Fig. 2 is one of the possible matchings of the pattern p1:(x1:Airport F light+ =⇒x2:Airport)□x2.code=“BCN” into the GDB in Fig. 1. However, the graph at the right of Fig. 2 (the strikethrough one) can not be considered a correct matching since the constraint x2.code = “BCN” is not satisfied. In what follows, for simplicity, we will denote a matching mas a function from V ar(q) to GDB. If we consider a query qas a formula of a graph logic, like GNL [8], a matching mis correct with respect to qif the database is a model for qwith respect to the substitution defined by m, denoted GDB, m |=q, or equivalently, if all constraints are satisfied by m. More precisely:
This work is licensed under a Creative Commons “Attribution-NonCommercial-NoDerivatives 4.0 International” license. The Semantics of GQL-like Path Queries 7 Fig. 2. A correct and a wrong matching. Definition 3 (Satisfaction. Correct Answer). Given a graph database GDB, a query qand a matching m:V ar(q)→GDB we define the satisfaction relation as follows: –For every node, edge, or atomic path pattern P,GDB, m |=Pif m(ΦP)is satisfiable. –For every pattern P=p:p1·p2, we have that GDB, m |=Pif m(p) = m(p1)·m(p2). –For every query q=P1,...,Pk, we have that GDB, m |=qif for every i (1≤i≤k), GDB, m|V ar(Pi)|=Pi, where m|V ar(Pi)denotes the function m restricted to the variables occurring in Pi. Finally, we say that mis a correct answer for qif GDB, m |=q. In Fig. 3 we can see a correct answer to the query in Subsec. 2.1 containing the matching at the top right in Fig. 2. Fig. 3. A correct answer to the query in Subsec. 2.1. 5 Operational semantics Our operational semantics is defined in terms of five derivation rules that define how to compute query answers. Roughly, to obtain these answers, starting from
This work is licensed under a Creative Commons “Attribution-NonCommercial-NoDerivatives 4.0 International” license. 8 F. Orejas and E. Pino an initial state, we successively apply these rules until we reach a final state, which in some sense includes the answer. We may notice that our semantics is different to the semantics and implementation of other query languages (see, e.g., [7]) where, using relational operators, we obtain at once all results of a query, while we find the results in a stepwise manner. That is, to find all results we would need a search procedure using backtracking, like in the case of logic programming. The main reason is that we believe that integrating our techniques for computing path properties in the context of these relational operators would be more difficult to explain and understand. Nevertheless, we also believe that it would not be difficult to implement the main ideas of our construction, in this other kind of semantics, which we leave for future work. A state includes two kinds of information: what remains to be done, i.e., which patterns have yet to be solved to compute the answer to the given query, and what information we have gathered in the process of reaching that state, including part of the matching that will provide the answer to the query. More precisely: Definition 4 (States, initial and final states). A state σis a four-tuple (q, Ψ, M, Π), where: (a) qis the current query to be solved, that is, the sequence of patterns not yet solved. (b) Ψis the set of constraints from the already solved patterns. (c) Mis the matching that we have already computed for node and edge variables. At the end, Mwill be part of the answer to the given query. More precisely, Mis a set of pairs x7→a, where xis a node or edge variable and ais a node or edge in GDB, respectively. (d) Πis the matching that we have partially computed for path variables. In particular, Πis a set of triples p7→ (s, p′), where pis the name of the path variable, sis a path in GDB, and p′is a path variable or the symbol ε. The idea is that the path assigned to pwill be the composition of sand the path that will be assigned to p′, if p′=ε; otherwise it will be s. The initial state is σ0= (q0,∅,∅,∅), where q0is the given query. Then, a final state is a state σ= (∅, Ψ, M, Π). The computation of the next state, after a given state σ= (q, Ψ, M, Π) consists roughly of three steps: selecting a pattern from q; finding a matching for the pattern; and applying a derivation rule on that pattern. This means that we will not try to solve the patterns following their order in the query, i.e., we consider that qis a set of patterns, rather than a sequence. More precisely, we consider that the selection of the pattern is nondeterministic, with the restriction that, if the pattern is of the form p:p1·p2, then Πshould include triples of the form p17→(s1, ε) and p27→(s2, ε). When finding a matching for a given pattern, it may happen that some of its variables have already been bound to elements in GDB in some previous derivation step, so we must be sure that our matching agrees with our current set M.
This work is licensed under a Creative Commons “Attribution-NonCommercial-NoDerivatives 4.0 International” license. The Semantics of GQL-like Path Queries 9 Definition 5. A matching m:P →GDB agrees with a state σ= (q, Ψ, M, Π)if Ψ∪m(ΦP)is satisfiable and, for every variable x∈V ar(P),x7→v∈ M implies m(x) = v. The concrete definition of our rules is shown below. Definition 6 (Derivation rules). We assume that we are in a state σ= (q, Ψ, M, Π), where q={P} ∪ q′and that Pis the selected pattern for the application of the rule: (R1) If P= (x:nt)□ΦPor P= (x1:nt1 y:et →x2:nt2)□ΦP, and there is a matching m:V ar(P)→GDB that agrees with σand such that m(ΦP)is satisfiable, then: (({P} ∪ q′), Ψ, M, Π)⇝GDB (q′, Ψ′,M′, Π) where Ψ′=m(Ψ∪ΦP)and M′=M∪{x7→m(x)|x∈V ar(P)} (R2) If P=p:p1·p2, there is no triple p7→ (s, p′)∈Π, there are triples pi7→ (si, ϵ)∈Π, for i= 1,2, and s1and s2are composable, then (({P} ∪ q′), Ψ, M, Π)⇝GDB (q′, Ψ, M, Π′) where Π′=Π∪ {p7→(s1·s2, ϵ)} (R3) If P=p: (x1:nt1 α =⇒x2:nt2)□ΦPor P=p:p1·p2, and there is a triple p7→(s, p′)∈Π, then (({P} ∪ q′), Ψ, M, Π)⇝GDB (q′, Ψ, M, Π) (U1) If P=p: (x1:nt1 α =⇒x2:nt2)□ΦP, there is no triple p7→ (s, p′)∈Π, λ∈ L(α), and λis the chosen unfolding, then (({P} ∪ q′), Ψ, M, Π)⇝GDB (q′, Ψ, M, Π′) where Π′=Π∪ {p7→(λ, ϵ)} (U2) If P=p:(x1:nt1 α =⇒x2:nt2)□ΦP, and if (a) there is no triple p7→(s, p′)∈Π, (b) u=⟨P1,P2⟩is the chosen unfolding, where P1= (x1:nt1 y:et −→ x3)□ΦP, P2=p′:(x3 α′ =⇒x2:nt2)□ΦP, and y, x3, p′are fresh new variables, (c) there is a matching m:V ar(P1)→GDB that agrees with σand such that m(Ψ∪ΦP)is satisfiable, then (({P} ∪ q′), Ψ, M, Π)⇝GDB ({P2} ∪ q′, Ψ′,M′, Π′) where M′=M∪{x7→m(x)|x∈V ar(P1)},Ψ′=m(Ψ∪ΦP)and, if there is a triple p′′ 7→(s, p)∈Π, Π′={p17→(s, p2)∈Π|p2=p}∪{p′′ 7→(s·(m(x1)m(y)m(x3)), p′)} Otherwise, Π′=Π∪ {p7→(m(x1)m(y)m(x3), p′)}.