GQL Essentials Path Semantics
PDF ↗

Paths and Regular Path Expressions

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

G = ⟨V,E,λV ,ρV ,λE,ρE,src,dst,dir⟩,

a path in G is a finite alternating sequence

w = ⟨v0,e1,v1,… ⁡,en,vn⟩∈Path(G)(n ≥ 0)

such that for every i ∈{1,… ⁡,n}, the edge ei connects vi−1 and vi. More explicitly, if ei is directed, either (src(ei),dst(ei)) = (vi−1,vi) or (src(ei),dst(ei)) = (vi,vi−1); its orientation in the path is respectively left-to-right or right-to-left. If ei is undirected, its endpoints are vi−1 and vi, without a directed orientation. Which of these possibilities satisfies a particular edge pattern is determined by that pattern’s directionality constraint. We write head(w) = v0, tail(w) = vn, |w| = n (number of edges), Edges(w) = ⟨e1,… ⁡,en⟩ as a list, and Nodes(w) = ⟨v0,… ⁡,vn⟩ as a list.

Walk, trail, simple, acyclic.

  • w is a walk always.
  • w is a trail iff all ei are pairwise distinct.
  • w is simple iff the node sequence has no repetitions except possibly that the first and last node may coincide; i.e., vi≠vj for all 0 ≤ i < j ≤ n with the single allowed case i = 0,j = n (so v0 = vn is permitted).
  • w is acyclic iff the node sequence has no repetitions at all (in particular v0≠vn unless n = 0).

For M ∈{WALK,TRAIL,SIMPLE,ACYCLIC}, define the mode predicate okM : Path(G) →{⊤ ⁡,⊥} by okM(w) = ⊤ ⁡ iff w obeys the restriction of M. Default: unless explicitly qualified otherwise, the path mode is WALK.

For example, the library-instance path ⟨r,eowns,a,echeck,l,einc,1,b1⟩, where echeck is the CHECK_OUT edge directed from l to a, 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

w = ⟨v0,e1,… ⁡,en,vn⟩and w′ = ⟨v 0′,e 1′,… ⁡,e m′,v m′⟩

with vn = v0′, their walk concatenation is the path

w ⋅ w′ = ⟨v 0,e1,… ⁡,en,vn = v0′︸ glued,e1′,… ⁡,e m′,v m′⟩.

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 X, let P(X) denote its power set and Pfin(X) its finite subsets.

For a path mode M ∈{WALK,TRAIL,SIMPLE,ACYCLIC} and a regular path expression R, we use the preview notation

PMR(u,v) ∈P(Path(G))

for the path projection, at endpoints (u,v), of the RPE witness denotation defined in Section 11.1. Thus its members are exactly the paths that satisfy R under direction tests and the mode predicate M. Selective path search prefixes (shortest, allshortest, any, etc.) are maps

Sel : P(Path(G)) →Pfin(Path(G))

applied independently per (u,v)-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 p ∈PathVal ∪{null}. We write PEL(p) for the path element list and len(p) for the path length. Formally,

PEL(p) := ELEMENTS(p)andlen(p) := PATH_LENGTH(p).

Both accessors are null-propagating: if p = null then PEL(p) = null and len(p) = null.

For a material path value p = PATH[⟨n0,e1,n1,… ⁡,ek,nk⟩], the element list is exactly that alternating list and the length is the number of edges:

PEL(p) = ⟨n0,e1,n1,… ⁡,ek,nk⟩andlen(p) = k.

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 null).

From PEL(p) we derive the node and edge projections as lists:

nodes(p) := ⟨x ∈PEL(p)∣xis a node reference⟩, edges(p) := ⟨x ∈PEL(p)∣xis an edge reference⟩.

When p is material, the following cardinality equalities hold (including the case len(p) = 0):

#nodes(p) = len(p) + 1, #edges(p) = len(p), #PEL(p) = 2 ⋅len(p) + 1.

We abbreviate the endpoints by

head(p) := the first element of nodes(p), tail(p) := the last element of nodes(p),

with the understanding that if p = null (or len(p) = null) then head(p) = tail(p) = null.

Path value construction and concatenation. For node/edge references ni,ei forming a path element list ⟨n0,e1,n1,… ⁡,ek,nk⟩,

PATH [⟨n0,e1,n1,… ⁡,ek,nk⟩ ]

denotes the corresponding (material) path value.

List helpers. For any finite list L = ⟨x1,… ⁡,xm⟩ with m ≥ 1, let

dropLast(L) := ⟨x1,… ⁡,xm−1⟩anddropFirst(L) := ⟨x2,… ⁡,xm⟩,

and let L ++ L′ denote list concatenation.

Given path values p and q, their value-level concatenation p ∘ q 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 null, then p ∘ q = null. Otherwise assume

PEL(p) = ⟨n0,e1,n1,… ⁡,ek,nk⟩andPEL(q) = ⟨n0′,e 1′,n 1′,… ⁡,e ℓ′,n ℓ′⟩,

with k,ℓ ≥ 0. If nk and n0′ 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

card ⁡(dropLast(PEL(p))) +card ⁡(PEL(q))

exceeds the implementation-defined maximum path-element-list cardinality (IL015), data exception 22G10 is raised. Otherwise:

PEL(p ∘ q) := dropLast(PEL(p)) ++ PEL(q).

Equivalently (expanding the list and showing the glued node once):

PEL(p ∘ q) = ⟨n0,e1,n1,… ⁡,ek,n0′︸ same referent as nk,e1′,n 1′,… ⁡,e ℓ′,n ℓ′⟩.

In particular, whenever both p and q are material and p ∘ q is defined,

len(p ∘ q) = len(p) + len(q).

When both p and q are material and correspond to walks w,w′, the operation p ∘ q mirrors the walk concatenation w ⋅ w′ 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 X as follows:

  • If X is a node variable of singleton degree and is bound to a node v, the projected field value is the node reference value for v. If X is conditional singleton and unbound, the value is null. If X has group degree, the projected value is the list of node reference values in the left-to-right binding order (duplicates retained).
  • If X is an edge variable, the cases are analogous with edge reference values.
  • If X is a path variable declared by some path pattern of the graph pattern, the projected field value is the path value p whose PEL(p) 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 null 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 null in the output binding table for unmatched input rows.

Result typing. Let type(X) denote the declared value type of the projected field for variable X in the current query: Element variables. Let X be an element variable. At singleton degree, type(X) is an implementation-determined supertype. It contains the corresponding node/edge reference value type. At conditional singleton degree, the type additionally contains null. At group degree, type(X) is a group list value type of node (resp. edge) reference values.

Path variables. If X is a path variable and is unconditionally exposed in the output, then type(X) is the (material) path value type. If X may be null due to statement-level optionalization, then type(X) additionally contains null (i.e., it is an Opt(Path)-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.

GQL Essentials · Dominik Tomaszuk

Search chapters, definitions and examples.