Skip to main content

Module pattern

Module pattern 

Source
Expand description

SQL/PGQ pattern matching: GRAPH_TABLE (g MATCH … COLUMNS (…)) (#296).

Ursa’s parser rewrites GRAPH_TABLE ( body ) into graph_table('body'); the relation planner hands the body here. parse reads it into a Pattern; lower resolves each variable to one of the graph’s element tables and says what to join: a Lowered match, which the planner builds as a logical plan (scans of the element tables, equality conditions, the pattern’s conditions and COLUMNS). DataFusion’s optimizer turns the equalities into hash joins.

Supported:

GRAPH_TABLE ( graph
  MATCH path [, path …]
  [WHERE condition]
  COLUMNS ( expression [AS name], … ) )

path       := [var =] [selector] [mode [PATH | PATHS]] node (edge node)*
selector   := ANY SHORTEST | ALL SHORTEST | ANY [k] | SHORTEST k | ALL
mode       := WALK | TRAIL | SIMPLE | ACYCLIC
node       := ( [var] [:labels | IS labels] [WHERE condition] )
edge       := (-[ spec ]->  |  <-[ spec ]-  |  -[ spec ]-  |  ->  |  <-  |  -) [quantifier]
quantifier := * | + | ? | {n} | {m,n} | {m,} | {,n}
spec       := [var] [:labels | IS labels] [WHERE condition]
labels     := label [| label …] | %

Conditions and COLUMNS are SQL expressions over the variables (a.name); each variable is a relation of that name. A label names the element tables that carry it (a table’s name is one of its labels); a|b either label’s, % and no label every table of the variable’s kind (#412). lower keeps the combinations of tables, one per variable, in which each edge’s table connects its nodes’ tables, as its REFERENCES say, and lowers each combination on its own: the planner joins them with UNION ALL. When every variable has one table, there is one combination, lowered as a pattern always was. A pattern of more than MAX_COMBINATIONS is refused; one with none is refused too, as it can match nothing.

Each variable reads its element table’s properties: every column, or what its labels’ PROPERTIES clauses give, under their property names. An undirected edge -[e]- matches both orientations of each edge (a self-loop once): the match is a UNION ALL with one branch per orientation, so every join stays an equality join.

A quantified edge, and the edge of a path with a path variable, a selector or a mode, is not joined: it is a Segment, searched on the graph index (see super::path_search), and joined to the nodes at its ends. A path with a variable, a selector or a mode is one edge between two nodes for now, and its edge has one edge table. Label expressions with &, ! or parentheses, and parenthesized path patterns, are refused with a message.

§Through the index

A node is bound when a condition on it alone limits its key to values the query names: a.id = 7, a.id IN (1, 2) or a.id IN (SELECT …). From a bound node, the fixed-length edges of its path are expanded through the graph index too, one Segment of exactly one edge each, outward from it: each segment starts from the keys the one before it reached (Starts::After), so only the edges at the nodes reached are read, never the whole edge table. A segment is a relation of edges as the join’s is, one row per edge row, so the rows are the same. A node whose query reads only its key, at the end of a segment whose index’s nodes are that node’s table’s keys, is not joined to its table at all: its key is the segment’s column (Lowered::aliases). Edges stay joins when no node is bound, when the query reads an edge’s columns, or when Usage::indexed does not list the edge table (its index could not be kept between queries).

Structs§

Alias
A node not joined to its table: its key is a segment’s column.
Lowered
A pattern resolved against a graph: what to scan and join.
PathPattern
One path of a MATCH: [p =] [selector] [mode] (a)-[e]->…(b).
Pattern
A parsed GRAPH_TABLE body.
Quantifier
An edge’s quantifier: how many times it repeats, {min,max}.
Segment
An edge searched on the graph index rather than joined: a quantified edge, or the edge of a path with a path variable, a selector or a mode. It is a relation of paths, named var, with the columns of super::path_search: the path’s first and last node, its length, and its nodes and edges.
Source
What a variable reads: an element table (or the graph’s nodes table), all its columns or columns as (column, property name).
Spec
A node or edge: [var] [:label[|label…]] [WHERE condition].
Usage
What the query reads and binds, from its conditions and COLUMNS: what decides which edges are expanded through the graph index (see the module docs). The default plans joins, as a pattern was planned before.
Variable
A pattern variable, as a result’s field metadata describes it.

Enums§

Direction
Element
One element of a path: nodes and edges alternate, starting with a node.
Join
A condition the lowering adds.
Mode
Which walks a path may be (SQL/PGQ’s path modes).
Selector
Which of a pair of nodes’ paths a path keeps.
Starts
Where a segment’s start nodes come from.
VariableKind
What a pattern variable binds.

Constants§

ANY_LABEL
% in a label expression: any label, so any table of the element’s kind.
FUNCTION
The table function Ursa’s parser rewrites GRAPH_TABLE ( … ) into.
IS_DESTINATION
The function a IS [NOT] DESTINATION OF e is rewritten to.
IS_LABELED
The function x IS [NOT] LABELED l is rewritten to, as __ursa_is_labeled(x, 'l'); the planner replaces it with TRUE or FALSE once it knows x’s table.
IS_SOURCE
The function a IS [NOT] SOURCE OF e is rewritten to.
MAX_COMBINATIONS
The most combinations of element tables one pattern may expand into: one plan each, under a UNION ALL (#412).

Functions§

common_type
The type two columns of these types are read as together, under a UNION ALL of a variable’s tables: the same type, two numbers, or two strings; None when they do not mix (#412).
lower
Resolve pattern against graph: one Lowered match per combination of element tables its variables can be rows of (#412), which the planner joins with UNION ALL. A variable with a label is a row of the tables that carry it (a|b: either); one without, of every table of its kind. Combinations whose edge tables do not connect their nodes’ tables are left out. When every variable has one table there is one combination, planned as before. schema_of gives a table’s columns (for properties).
parse
Parse a GRAPH_TABLE body: graph MATCH … [WHERE …] COLUMNS (…).

Type Aliases§

VarColumn
A column of a variable: (variable, column).