Chapter 10Path Semantics
This chapter formalizes paths as first-class values in the GQL data model and explains how different path modes constrain the shape of walks through a graph. It describes the interaction between path modes, selective search prefixes, and accessors, and clarifies how path values are constructed, combined, and projected back into binding tables.
10.1 Modes: Walk, Trail, Simple, Acyclic
Paths in a graph. Given a graph
a path in is a finite alternating sequence
such that for every , the edge connects and . More explicitly, if is directed, either or ; its orientation in the path is respectively left-to-right or right-to-left. If is undirected, its endpoints are and , without a directed orientation. Which of these possibilities satisfies a particular edge pattern is determined by that pattern’s directionality constraint. We write , , (number of edges), as a list, and as a list.
- is a walk always.
- is a trail iff all are pairwise distinct.
- is simple iff the node sequence has no repetitions except possibly that the first and last node may coincide; i.e., for all with the single allowed case (so is permitted).
- is acyclic iff the node sequence has no repetitions at all (in particular unless ).
For , define the mode predicate by iff obeys the restriction of . Default: unless explicitly qualified otherwise, the path mode is .
For example, the library-instance path , where is the CHECK_OUT edge directed from to , is traversed from the reader through the account and loan to the first book. This path has no repeated node or edge. It is therefore a walk, trail, simple path, and acyclic path.
Concatenation of walks. For two paths
with , their walk concatenation is the path
This is a purely combinatorial operation on the walk domain and will be used when defining the semantics of regular path expressions.
10.2 Finiteness and Selection
Notation. For a set , let denote its power set and its finite subsets.
For a path mode and a regular path expression , we use the preview notation
for the path projection, at endpoints , of the RPE witness denotation defined in Section 11.1. Thus its members are exactly the paths that satisfy under direction tests and the mode predicate . Selective path search prefixes (, , , etc.) are maps
applied independently per -partition. The well-formedness rules require every unbounded quantifier to be bounded in effect by a restrictive path mode, a selective path-search prefix, or the DIFFERENT EDGES match mode; accordingly, graph-pattern evaluation returns a finite set of reduced matches.
10.3 Path Accessors and Results
Accessors on path values. Let . We write for the path element list and for the path length. Formally,
Both accessors are null-propagating: if then and .
For a material path value , the element list is exactly that alternating list and the length is the number of edges:
Malformed representations (non-alternating lists or lists not starting/ending in a node reference) are outside the well-formed path domain; if encountered at runtime, they raise a malformed path data exception (rather than yielding ).
From we derive the node and edge projections as lists:
When is material, the following cardinality equalities hold (including the case ):
We abbreviate the endpoints by
with the understanding that if (or ) then .
Path value construction and concatenation. For node/edge references forming a path element list ,
denotes the corresponding (material) path value.
List helpers. For any finite list with , let
and let denote list concatenation.
Given path values and , their value-level concatenation is defined when the last and first elements, respectively, are node reference values whose referent is the same node, and yields the path value obtained by gluing these endpoints so that the shared node occurs once. Otherwise, a malformed path exception is raised.
Formally, if either operand is , then . Otherwise assume
with . If and are not node reference values whose referent is the same node, a malformed-path exception is raised. If their referent is the same node but
exceeds the implementation-defined maximum path-element-list cardinality (IL015), data exception 22G10 is raised. Otherwise:
Equivalently (expanding the list and showing the glued node once):
In particular, whenever both and are material and is defined,
When both and are material and correspond to walks , the operation mirrors the walk concatenation defined above.
Result projection (from matches to values). Let a graph pattern be evaluated to a finite set of reduced matches (multi-path bindings). Projection of binding variables into a working record proceeds per variable as follows:
- If is a node variable of singleton degree and is bound to a node , the projected field value is the node reference value for . If is conditional singleton and unbound, the value is . If has group degree, the projected value is the list of node reference values in the left-to-right binding order (duplicates retained).
- If is an edge variable, the cases are analogous with edge reference values.
- If is a path variable declared by some path pattern of the graph pattern, the projected field value is the path value whose is the left-to-right list of graph element references in the extracted path of that binding. If the surrounding statement semantics preserves rows in which that declaring component contributes no match (e.g., OPTIONAL MATCH), then the path variable is projected as for those rows, analogously to other newly introduced binding variables.
A reference to a path variable always has singleton reference degree. Within reduced matches produced by its declaring path pattern component it is unconditionally bound; however, statement-level optionalization may yield in the output binding table for unmatched input rows.
Result typing. Let denote the declared value type of the projected field for variable in the current query: Element variables. Let be an element variable. At singleton degree, is an implementation-determined supertype. It contains the corresponding node/edge reference value type. At conditional singleton degree, the type additionally contains . At group degree, is a group list value type of node (resp. edge) reference values.
Path variables. If is a path variable and is unconditionally exposed in the output, then is the (material) path value type. If may be due to statement-level optionalization, then additionally contains (i.e., it is an -like type in the sense of Chapter 6).
Interaction with selection and modes. For each selective path pattern, selection is applied to the candidate path bindings after evaluating any contained WHERE conditions and enforcing restrictive path modes (e.g., TRAIL, SIMPLE, ACYCLIC). Variants such as ANY SHORTEST, ALL SHORTEST, SHORTEST N PATHS, and SHORTEST N GROUPS operate on the candidates as specified and yield a finite set of retained bindings; projection then proceeds as above.