GQL Essentials Regular Path Expressions
PDF ↗

Paths and Regular Path Expressions

Chapter 11Regular Path Expressions

Regular path expressions (RPEs) describe constrained walks through a graph and form the declarative core of GQL’s path-matching capabilities. In this chapter, we fix a precise syntax for RPEs, use an auxiliary witness semantics that can record explicit multiset alternation, and connect this semantics to the family of selective shortest-path operators. Graph-pattern evaluation itself returns a set of reduced matches, as specified in Chapter 7.

11.1 Syntax and Denotation

Syntax. Fix a graph G = ⟨V,E,… ⁡⟩ and a finite alphabet ΣL ⊆ 𝕃 of edge-label names. RPEs are generated by:

r ::= 𝜀∣ℓ∣ℓ−1∣[ν]∣{η}∣{η}−1∣(r ⋅ r)∣(r ∪ r)∣(r ∪ msr)∣r∗∣rm..n

where ℓ ∈ΣL, m,n ∈ ℕ ∪{∞} with m ≤ n, [ν] is a node test to be satisfied at the current node, and {η} and {η}−1 are edge tests to be satisfied by the next directed edge consumed in forward and backward orientation, respectively (see below). Here ν : V →{⊤ ⁡,⊥} is a two-valued node predicate and η : E →{⊤ ⁡,⊥} is a two-valued edge predicate.

Concrete syntax for path expressions. At the concrete-syntax level, regular path expressions appear inside ⟨path pattern⟩ constructs and are written using infix alternation, concatenation by juxtaposition, and postfix quantifiers. Parenthesized subexpressions may carry an attached WHERE clause whose search condition is local to the component. The following simplified BNF captures the backbone of this concrete syntax. Here element-pattern is an abstract interface for a node or edge pattern; its role in path patterns is described in Section 7.2.

BNF
path-expr    ::= path-term
               | path-term "|" path-term ( "|" path-term )*
               | path-term "|+|" path-term ( "|+|" path-term )*
path-term    ::= path-factor+          /* concatenation */
path-factor  ::= path-primary [ quantifier ]
path-primary ::= element-pattern
               | "(" [ subpath-var "=" ] path-expr
                     [ where-clause ] ")"
quantifier   ::= "?"
               | "*"
               | "+"
               | "{" int "}"
               | "{" [ int ] "," [ int ] "}"
where-clause ::= "WHERE" search-condition
subpath-var  ::= identifier
element-pattern  ::= node-pattern | edge-pattern
node-pattern     ::= ...  /* node test and optional binding */
edge-pattern     ::= ...  /* edge step, tests, and optional binding */
search-condition ::= ...  /* boolean expressions */

Informally, path-expr corresponds to the abstract r-grammar above, with "|" realizing set alternation, the GQL multiset-alternation token "|+|" realizing ∪ms, juxtaposed path-factors realizing concatenation, and the quantifier forms mapping to r∗, rm..n, and their common shorthands. A subpath variable is a graph-pattern variable used to distinguish reduced matches for multiset alternation; it is not bound to a value and cannot be referenced.

For example, the expression OWNS⋅CHECK_OUT−1 ⋅INCLUDES accepts the library-instance path from the reader through the account and loan to either book.

For every quantified or questioned path primary, the simply contained primary shall have minimum path length greater than zero. In particular, 𝜀∗ and any other repetition of a zero-edge primary belong to the auxiliary calculus but are not well-formed concrete GQL path patterns. This condition prevents a single zero-edge witness from acquiring infinitely many derivations through different repetition counts.

Important At the level of path witnesses alone (i.e., when one forgets how variables are exposed by element patterns), the postfix quantifier "?" behaves like the bounded repetition {0, 1}: it admits either zero or one occurrence of the subexpression. However, when "?" is applied to a path-primary that contains element-patterns with bindings, GQL assigns it the status of a questioned path-pattern primary in the sense of Chapter 7. In particular, the propagation of variable exposure degrees for "?" is not in general identical to treating it as a mere abbreviation for {0, 1}: singleton-variable exposure becomes conditional due to the zero-iteration branch, while group-variable exposure is preserved as specified in Table 7.1. Thus, in this book we treat "?" as syntactic sugar for {0, 1} only in the purely language-theoretic sense of accepted path witnesses, not as an identity of the variable-exposure meta-theory.

An element-pattern contributes a single step (possibly with tests on nodes and edges), while a parenthesized path-expr with an optional WHERE clause behaves as a grouped subpattern whose search condition is evaluated prior to any selective path-search prefix. A non-selective component may use a non-local reference of singleton degree; a selective path pattern may not reference a graph-pattern variable declared outside it.

Step predicates and step relations (for element patterns). To connect the abstract RPE syntax above with the concrete element-pattern constructs, we use a generalized notion of a step predicate.

Step predicates. A step predicate δ is a quadruple

δ = (orient,Φe,Φsrc,Φdst)

where orient ∈{→,←,↔} selects forward/backward/undirected traversal (query-time), distinct from the graph attribute dir(e) ∈{→,↔} (data-time directedness); Φe is a Boolean predicate over (e,λE(e),ρE(e)); and Φsrc, Φdst are Boolean predicates over endpoint vertices (their labels/properties). Predicates evaluate in the three-valued logic of Section 8.3; U counts as failure for step selection. Reading an absent property yields null when its declared property type is nullable; for a material property type it raises 22G03. A surrounding predicate may then evaluate to U as a consequence of a null value.

Step relation. The graph-induced ternary relation Sδ ⊆ V × E × V is defined as

(v,e,z) ∈ Sδ⇔ ([orient =→∧dir(e) =→∧src(e) = v ∧dst(e) = z]∨ [orient =←∧dir(e) =→∧src(e) = z ∧dst(e) = v]∨ [orient =↔∧dir(e) =↔∧{v,z} = {src(e),dst(e)}] ) ∧ (Φe(e) = ⊤ ⁡ )∧ (Φsrc(v) = ⊤ ⁡ )∧ (Φdst(z) = ⊤ ⁡ ).

Intuition: forward and backward steps match directed edges in the indicated orientation, while the undirected step ↔ matches an undirected edge in either traversal direction. Edge-pattern variants that admit more than one orientation use the union of the corresponding cases.

Example (one library step). For the library instance, let

δinc = (→, Φinc,⊤ ⁡,⊤ ⁡),Φinc(e) = ⊤ ⁡⇔INCLUDES ∈ λE(e).

Since einc,1 is directed from l to b1 and has this label,

(l,einc,1,b1) ∈ Sδinc.

Label and predicate atoms as special cases. The abstract atoms ℓ, ℓ−1, and {η} can be seen as notational shorthands for specific step predicates:

  • {η} corresponds to the forward step predicate δη = (→, Φe,⊤ ⁡,⊤ ⁡) with Φe(e) = ⊤ ⁡ iff η(e) = ⊤ ⁡.
  • {η}−1 corresponds to the backward step predicate δη−1 = (←, Φe,⊤ ⁡,⊤ ⁡) with the same edge test.
  • ℓ corresponds to the forward label-step predicate δℓ = (→, Φe,⊤ ⁡,⊤ ⁡) with Φe(e) = ⊤ ⁡ iff ℓ ∈ λE(e).
  • ℓ−1 corresponds to the backward label-step predicate δℓ−1 = (←, Φe,⊤ ⁡,⊤ ⁡) with the same label test.

We keep the explicit equations for these atoms below to make the endpoint orientation transparent; undirected steps are represented by the orient =↔ case of Sδ.

Ternary denotation with bag semantics. For the fixed graph G and mode mode, the denotation of r is a (possibly infinite) bag of endpoint-labeled path witnesses:

⟦r⟧Gmode ∈M(V ×Path(G) × V ).

Its support consists of triples (u,w,v) such that head(w) = u, tail(w) = v, okmode(w) = ⊤ ⁡, and w matches r by the rules below.

Important (bag meaning). The multiplicity ⟦r⟧Gmode(u,w,v) counts the number of derivations (proof trees) of the judgment “w matches r from u to v” induced by the inductive clauses (alternation may add multiplicities; concatenation multiplies and sums over intermediate endpoints; repetition sums over iteration depth). Consequently, the same triple (u,w,v) may occur with multiplicity > 1 when the expression admits multiple decompositions producing the same witness w (e.g., due to explicit multiset duplication such as r ∪msr, or due to different decompositions in iterated constructs). This derivation count is auxiliary bookkeeping. Ordinary alternation and the final graph-pattern result are set-valued; only distinctions introduced by explicit multiset alternation are intended to survive as distinct reduced matches.

Conformance note. GQL distinguishes the ordinary vertical-bar alternation, which has set semantics, from a multiset-sensitive alternation form. Accordingly, the unsubscripted abstract constructor r1 ∪ r2 denotes set alternation; ∪ms is used only for the explicit multiset-alternation form.

For a bag B, define its setification by

setify(B)(x) := { 1if B(x) > 0, 0 otherwise.

If one needs set semantics (“distinct path witnesses”) for the overall result of an RPE, it can be obtained either by setification setify or, equivalently, by taking the support supp(⟦r⟧Gmode) as a post-processing step.

Base cases. (All bags below assign multiplicity 1 to each listed triple.)

⟦𝜀⟧Gmode = {(v,⟨v⟩,v)∣v ∈ V } ⟦[ν]⟧Gmode = {(v,⟨v⟩,v)∣ν(v) = ⊤ ⁡} ⟦ℓ⟧Gmode = {(u,⟨u,e,v⟩,v)|e∈E,ℓ∈λE(e),dir(e)=→, src(e)=u,dst(e)=v, okmode(⟨u,e,v⟩)=⊤ ⁡ } ⟦ℓ−1⟧ Gmode = {(v,⟨v,e,u⟩,u)|e∈E,ℓ∈λE(e),dir(e)=→, src(e)=u,dst(e)=v, okmode(⟨v,e,u⟩)=⊤ ⁡ } ⟦{η}⟧Gmode = {(u,⟨u,e,v⟩,v)|e∈E,η(e)=⊤ ⁡,dir(e)=→, src(e)=u,dst(e)=v, okmode(⟨u,e,v⟩)=⊤ ⁡ } ⟦{η}−1⟧ Gmode = {(v,⟨v,e,u⟩,u)|e∈E,η(e)=⊤ ⁡,dir(e)=→, src(e)=u,dst(e)=v, okmode(⟨v,e,u⟩)=⊤ ⁡ }.

Inductive cases. Here and in the sequel, ⊎ ⁡ kBk denotes the iterated bag union of bags Bk, obtained by pointwise addition of multiplicities.

We write bag-join on the middle endpoint with path concatenation as ⋊⋉ ⋈, where ⋈ combines two compatible triples by concatenating their path witnesses:

(u,w1,x)⋈(x,w2,v) = (u,w1 ⋅ w2,v).

Formally, for bags B1,B2 ∈M(V ×Path(G) × V ), define

(B1 ⋊⋉ ⋈B2)(u,w,v) := ∑ x∈V ∑ w1,w2 w=w1⋅w2 B1(u,w1,x)⋅B2(x,w2,v).

We also use the mode filter, which rejects witnesses not admissible under the chosen path mode:

filtermode(B)(u,w,v) := { B(u,w,v)if okmode(w) = ⊤ ⁡, 0 otherwise,

Alternation. We distinguish explicitly:

⟦r1 ∪msr2⟧Gmode := ⟦r 1⟧Gmode ⊎⟦r 2⟧Gmode(multiset alternation), ⟦r1 ∪setr2⟧Gmode := setify (⟦r 1⟧Gmode ⊎⟦r 2⟧Gmode )(set alternation).

Unless stated otherwise, we interpret the abstract constructor (r1 ∪ r2) as ∪set, matching concrete "|". Only the distinct multiset-alternation token is interpreted as ∪ms.

Concatenation and repetition. Concatenation composes the witnesses produced by its two operands. A witness of r1 ending at some node x may be combined with a witness of r2 starting at the same node x. The resulting witness is obtained by concatenating the two paths, while its multiplicity is the product of the multiplicities of the component witnesses. Contributions obtained through different intermediate nodes or different decompositions of the same path are then added by the bag join defined above.

Although each operand has already been evaluated under the selected path mode, their concatenation need not itself satisfy that mode. For example, two individually admissible path witnesses may, after concatenation, repeat an edge or a node forbidden by the selected mode. The mode filter is therefore applied again to the composed witnesses:

⟦r1 ⋅ r2⟧Gmode = filter mode (⟦r1⟧Gmode ⋊⋉ ⋈⟦r2⟧Gmode) .

To define repetition, we first define the k-fold concatenation of an expression r. The zeroth power denotes the empty path at each node and is therefore identified with the semantics of 𝜀:

⟦r⟧Gmode,0 = ⟦𝜀⟧ Gmode.

For k ≥ 0, the (k + 1)-st power is obtained by concatenating one additional witness of r with a witness of its k-th power. As in ordinary concatenation, the resulting path is filtered according to the selected path mode:

⟦r⟧Gmode,k+1 = filter mode (⟦r⟧Gmode ⋊⋉ ⋈⟦r⟧Gmode,k) .

Consequently, ⟦r⟧Gmode,k contains the path witnesses formed from exactly k successive occurrences of r, with their bag multiplicities determined by all admissible decompositions into k component witnesses.

For a well-formed concrete GQL quantified primary, every occurrence of r consumes at least one edge. Hence a witness of length L can arise only from repetition counts k ≤ L, and its multiplicity cannot become infinite by summing over repetition depths. Expressions that violate the positive minimum-path-length condition, such as 𝜀∗, are excluded from the concrete language.

The Kleene-star expression collects the witnesses obtained for every finite number of repetitions:

⟦r∗⟧ Gmode = ⊎ k=0∞⟦r⟧ Gmode,k.

Thus, the result includes the empty-path witnesses contributed by k = 0, as well as witnesses formed by one or more occurrences of r. Because the outer operation is bag union, the multiplicity of a witness records the total number of derivations of that witness across all repetition counts and all admissible decompositions.

Finally, for bounds m ∈ ℕ and n ∈ ℕ ∪{∞} with m ≤ n, bounded repetition retains only the powers whose repetition counts lie in the specified interval:

⟦rm..n⟧ Gmode = ⊎ k=mn⟦r⟧ Gmode,k,(n = ∞allowed).

When n = ∞, the expression denotes lower-bounded repetition; when m = n, it denotes exactly m repetitions. In particular, r0..∞ coincides with r∗.

Reversal of expressions. Define the reversal r⊤ ⁡ recursively by

𝜀⊤ ⁡ = 𝜀, [ν]⊤ ⁡ = [ν], {η}⊤ ⁡ = {η}−1, ({η}−1)⊤ ⁡ = {η}, ℓ⊤ ⁡ = ℓ−1, (ℓ−1)⊤ ⁡ = ℓ, (r ⋅ s)⊤ ⁡ = s⊤ ⁡ ⋅ r⊤ ⁡, (r ∪ s)⊤ ⁡ = r⊤ ⁡ ∪ s⊤ ⁡, (r ∪mss)⊤ ⁡ = r⊤ ⁡ ∪ mss⊤ ⁡, (r∗)⊤ ⁡ = (r⊤ ⁡)∗, (rm..n)⊤ ⁡ = (r⊤ ⁡)m..n.

This witnesses symmetry of undirected matching and is used in bidirectional expansions.

Guards over intermediates. Let ψ be a Boolean predicate on paths, with type

ψ : Path(G) →{⊤ ⁡,⊥}.

It is built from node/edge predicates with quantifiers ∀ ⁡,∃ ⁡ over position indices. For B ∈M(V ×Path(G) × V ), define

filterψ(B)(u,w,v) := { B(u,w,v)if ψ(w) = ⊤ ⁡, 0 otherwise.

and define a guarded expression r&ψ by

⟦r&ψ⟧Gmode = filter ψ (⟦r⟧Gmode ).

Intent: r&ψ is a semantic shorthand for a parenthesized path pattern with an attached WHERE clause that uses only the component’s local variables.

11.2 Shortest-Path Semantics

Candidates and endpoint partitions. Fix a graph G and a path mode pm such that

pm ∈{WALK,TRAIL,SIMPLE,ACYCLIC}.

For a regular path expression R, let Cand(R,pm) be the set of candidate path matches admitted by the auxiliary denotation and the chosen path mode. More precisely, let Der(R,pm) be the set of successful derivation trees. For d ∈Der(R,pm), write (ud,wd,vd) for its endpoint/path triple and χsp(d) for its trace of subpath markers introduced by explicit multiset alternation. Define

d ≡spd′ ⇔(u d,wd,vd) = (ud′,wd′,vd′) ∧χsp(d) = χsp(d′), Cand(R,pm) := Der(R,pm)∕ ≡sp.

For a candidate class c = [d]≡sp, define

path(c) := wd.

This is well-defined because equivalent derivations have the same endpoint/path triple. Thus ordinary derivational duplicates are identified, while branch distinctions introduced by explicit multiset alternation remain distinct, as represented in the standard by subpath markers. For u,v ∈ V , write the endpoint partition

Πu,v(Cand(R,pm)) := {c ∈Cand(R,pm)∣head(path(c)) = u∧tail(path(c)) = v}.

Below, Πu,v abbreviates this partition for the fixed R and pm. Every path-search selector acts endpoint-locally on these sets and returns the set union of its per-partition results.

Example (one endpoint partition). For the library instance, take Rinc = INCLUDES and pm = WALK, and let

winc,1 = ⟨l,einc,1,b1⟩.

If cinc,1 is the candidate represented by the successful derivation whose endpoint/path triple is (l,winc,1,b1), then

cinc,1 ∈Πl,b1(Cand(Rinc,WALK)).

Length and groups. Let len(w) denote the number of edge-steps in w. For a fixed partition Πu,v, define its length-groups by

Gu,v(k) := {c ∈Πu,v∣len(path(c)) = k},(k ∈ ℕ).

Groups are ordered by the key k in increasing order. We write

minlen(u,v) = min ⁡ {len(path(c))∣c ∈Πu,v}

when Πu,v≠∅.

Selective path-search operators. A path-search prefix in this abstraction is one of:

ALLPATHS, ANYNPATHS, SHORTESTNPATHS, SHORTESTNGROUPS,

where N ∈ ℤ>0. Except for ALLPATHS, these are selective prefixes. Their denotation is a partition-wise selection operator SPSPs(⋅) over Cand(R,pm), defined as follows.

All paths (non-selective).

SPSPALLPATHS(Cand(R,pm)) = Cand(R,pm).

Any N paths per endpoints. For each (u,v), if |Πu,v|≤ N, keep Πu,v; otherwise choose an implementation-dependent subset pickN(Πu,v) ⊆Πu,v of cardinality N.

SPSPANYNPATHS(Cand(R,pm)) = ⋃ u,v { Πu,v if |Πu,v|≤ N, pickN(Πu,v)otherwise.

Counted shortest N paths per endpoints. Within each Πu,v, order candidates by nondecreasing path length; ties may be resolved in an implementation-dependent manner. Return up to N candidates from the front of this order.

SPSPSHORTESTNPATHS(Cand(R,pm)) = ⋃ u,vtakeShortestN(Πu,v),

where takeShortestN(Πu,v) ⊆Πu,v and |takeShortestN(Πu,v)| = min ⁡ {N,|Πu,v|}.

Counted shortest N groups per endpoints. Within each Πu,v, take the groups Gu,v(k) in increasing k. Return the set union of the first N nonempty groups (or all of them if fewer exist):

SPSPSHORTESTNGROUPS(Cand(R,pm)) = ⋃ u,v ⋃ kamong the first Nnonempty groups Gu,v(k).

Named shorthands and defaults. The following equivalences and defaults hold:

ANYSHORTESTPATH ≡SHORTEST1PATHS, ALLSHORTESTPATHS ≡SHORTEST1GROUPS, if N is omitted then N := 1,if pm is omitted then pm := WALK.

Well-formedness and finiteness. If a selective prefix declares N ≤ 0 it is an error. Every quantified or questioned path-pattern primary has positive minimum path length. For each unbounded quantifier, a restrictive mode TRAIL, ACYCLIC, or SIMPLE, a selective prefix, or the graph pattern’s DIFFERENT EDGES match mode is required. A restrictive mode or DIFFERENT EDGES makes the admissible match set finite; a selective prefix may start from an infinite candidate set but returns a finite selected set.

GQL Essentials · Dominik Tomaszuk

Search chapters, definitions and examples.