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 and a finite alphabet of edge-label names. RPEs are generated by:
where , with , is a node test to be satisfied at the current node, and and are edge tests to be satisfied by the next directed edge consumed in forward and backward orientation, respectively (see below). Here is a two-valued node predicate and 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.
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 -grammar above, with "|" realizing set alternation, the GQL multiset-alternation token "|+|" realizing , juxtaposed path-factors realizing concatenation, and the quantifier forms mapping to , , 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 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 : 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 : 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 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
where selects forward/backward/undirected traversal (query-time), distinct from the graph attribute (data-time directedness); is a Boolean predicate over ; and are Boolean predicates over endpoint vertices (their labels/properties). Predicates evaluate in the three-valued logic of Section 8.3; counts as failure for step selection. Reading an absent property yields when its declared property type is nullable; for a material property type it raises 22G03. A surrounding predicate may then evaluate to as a consequence of a null value.
Step relation. The graph-induced ternary relation is defined as
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
Since is directed from to and has this label,
Label and predicate atoms as special cases. The abstract atoms , , and can be seen as notational shorthands for specific step predicates:
- corresponds to the forward step predicate with iff .
- corresponds to the backward step predicate with the same edge test.
- corresponds to the forward label-step predicate with iff .
- corresponds to the backward label-step predicate 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 case of .
Ternary denotation with bag semantics. For the fixed graph and mode , the denotation of is a (possibly infinite) bag of endpoint-labeled path witnesses:
Its support consists of triples such that , , , and matches by the rules below.
Important (bag meaning). The multiplicity counts the number of derivations (proof trees) of the judgment “ matches from to ” 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 may occur with multiplicity when the expression admits multiple decompositions producing the same witness (e.g., due to explicit multiset duplication such as , 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 denotes set alternation; is used only for the explicit multiset-alternation form.
For a bag , define its setification by
If one needs set semantics (“distinct path witnesses”) for the overall result of an RPE, it can be obtained either by setification or, equivalently, by taking the support as a post-processing step.
Base cases. (All bags below assign multiplicity to each listed triple.)
Inductive cases. Here and in the sequel, denotes the iterated bag union of bags , 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:
Formally, for bags , define
We also use the mode filter, which rejects witnesses not admissible under the chosen path mode:
Alternation. We distinguish explicitly:
Unless stated otherwise, we interpret the abstract constructor as , matching concrete "|". Only the distinct multiset-alternation token is interpreted as .
Concatenation and repetition. Concatenation composes the witnesses produced by its two operands. A witness of ending at some node may be combined with a witness of starting at the same node . 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:
To define repetition, we first define the -fold concatenation of an expression . The zeroth power denotes the empty path at each node and is therefore identified with the semantics of :
For , the -st power is obtained by concatenating one additional witness of with a witness of its -th power. As in ordinary concatenation, the resulting path is filtered according to the selected path mode:
Consequently, contains the path witnesses formed from exactly successive occurrences of , with their bag multiplicities determined by all admissible decompositions into component witnesses.
For a well-formed concrete GQL quantified primary, every occurrence of consumes at least one edge. Hence a witness of length can arise only from repetition counts , 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:
Thus, the result includes the empty-path witnesses contributed by , as well as witnesses formed by one or more occurrences of . 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 and with , bounded repetition retains only the powers whose repetition counts lie in the specified interval:
When , the expression denotes lower-bounded repetition; when , it denotes exactly repetitions. In particular, coincides with .
Reversal of expressions. Define the reversal recursively by
This witnesses symmetry of undirected matching and is used in bidirectional expansions.
Guards over intermediates. Let be a Boolean predicate on paths, with type
It is built from node/edge predicates with quantifiers over position indices. For , define
and define a guarded expression by
Intent: 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 and a path mode such that
For a regular path expression , let be the set of candidate path matches admitted by the auxiliary denotation and the chosen path mode. More precisely, let be the set of successful derivation trees. For , write for its endpoint/path triple and for its trace of subpath markers introduced by explicit multiset alternation. Define
For a candidate class , define
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 , write the endpoint partition
Below, abbreviates this partition for the fixed and . 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 and , and let
If is the candidate represented by the successful derivation whose endpoint/path triple is , then
Length and groups. Let denote the number of edge-steps in . For a fixed partition , define its length-groups by
Groups are ordered by the key in increasing order. We write
when .
Selective path-search operators. A path-search prefix in this abstraction is one of:
where . Except for , these are selective prefixes. Their denotation is a partition-wise selection operator over , defined as follows.
All paths (non-selective).
Any paths per endpoints. For each , if , keep ; otherwise choose an implementation-dependent subset of cardinality .
Counted shortest paths per endpoints. Within each , order candidates by nondecreasing path length; ties may be resolved in an implementation-dependent manner. Return up to candidates from the front of this order.
where and .
Counted shortest groups per endpoints. Within each , take the groups in increasing . Return the set union of the first nonempty groups (or all of them if fewer exist):
Named shorthands and defaults. The following equivalences and defaults hold:
Well-formedness and finiteness. If a selective prefix declares it is an error. Every quantified or questioned path-pattern primary has positive minimum path length. For each unbounded quantifier, a restrictive mode , , or , 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.