Basic Pattern Concepts
Introduction
Pattern matching by nature is declarative. It enables users to focus on specifying what they want from a query without worrying about the underlying query processing.
A pattern usually appears in the FROM clause, the most fundamental part of the query structure. The pattern specifies sets of vertex types and how they are connected by edge types. A pattern can be refined further with conditions in the WHERE clause. In this tutorial, we'll focus on the linear pattern.
Currently, pattern matching may only be used in read-only queries.
1-Hop Pattern
The easiest way to understand patterns is to start with a simple 1-Hop pattern. Even a single hop has several options. After we've tackled single hops, then we'll see how to add repetition to make variable length patterns and how to connect single hops to form bigger patterns.
In classic GSQL queries, described in GSQL 101, we used the punctuation -( )->
in the FROM clause to indicate a 1-hop query, where the arrow specifies the vertex flow from left to right, and ( )
encloses the edge types.
In pattern matching, we use the punctuation -( )-
to denote a 1-hop pattern, where the edge type(s) is enclosed in the parentheses ()
and the hyphens -
symbolize connection without specifying direction. Instead, directionality is explicitly stated for each edge type.
For an undirected edge E, no added decoration: E
For a directed edge E from left to right, use a suffix: E>
For a directed edge E from right to left, use a prefix: <E
For example, in the LDBC SNB schema, there are two directed relationships between Person and Message: person LIKES message, and message HAS_CREATOR person. Despite the fact that these relationships are in opposite directions, we can include both of them in the same pattern very concisely:
Edge Type Wildcards
The underscore _
is a wildcard meaning any edge type. Arrowheads are still used to indicate direction, e.g., _>
or <_
or _
The empty parentheses ()
means any edge, directed or undirected.
Examples of 1-Hop Patterns
FROM X:x - (E1:e1) - Y:y
E1 is an undirected edge. x and y bind to the end points of E1. e1 is the alias of E1.
FROM x - (E2>:e2) - Y:y
Right directed edge, x binds to the source of E2, y binds to the target of E2.
FROM X:x - (<E3:e3) - Y:y
Left directed edge, y binds to the source of E3, x binds to the target of E3.
FROM X:x - (_:e) - Y:y
Any undirected edge between a member of X and a member of Y.
FROM X:x - (_>:e) - Y:y
Any right directed edge with source in X and target in Y.
FROM X:x - (<_:e) - Y:y
Any left directed edge with source in Y and target in X.
FROM X:x - ((<_|_):e) - Y:y
Any left directed or any undirected. "|" means OR, and parentheses enclose the group of edge descriptors. e is the alias for the edge pattern (<_|_).
FROM X:x - ((E1|E2>|<E3):e) - Y:y
Any one of the three edge patterns.
FROM X:x - () - Y:y
any edge (directed or undirected)
Same as
(<_|_>|_)
How To Enter Pattern Match Syntax Mode
To use the pattern match syntax, you need to either set a session parameter or specify it in the query. There are currently two syntax versions for queries:
"v1" is the classic syntax, traversing one hop per SELECT statement. This is the default mode.
"v2" enhances the v1 syntax with pattern matching.
syntax_version Session Parameter
You can use the SET command to assign a value to the syntax_version session parameter: v1 for classic syntax; v2 for pattern matching. If the parameter is never set, the classic v1 syntax is enabled. Once set, the selection remains valid for the duration of the GSQL client session, or until it is changed with another SET command.
Query-Level SYNTAX option
You can also select the syntax by using the new SYNTAX option in the CREATE QUERY statement: v1 for classic syntax (default); v2 for pattern matching. The Query-Level SYNTAX option overrides the syntax_version session parameter.
CHANGE ADVISORY
The punctuation used with the SYNTAX keyword was streamlined, from
CREATE QUERY <query_name><parameters> FOR GRAPH <graph_name>
SYNTAX ("v2")
# original version, TigerGraph 2.4.0
to
CREATE QUERY <query_name><parameters> FOR GRAPH <graph_name>
SYNTAX v2
# final version, since TigerGraph 2.4.1
Running Anonymous Queries Without Installing
In this tutorial, we will use the new Interpreted Mode for GSQL, also introduced in TigerGraph 2.4. Interpreted mode lets us skip the INSTALL step, and even to run a query as soon as we create it, to offer a more interactive experience. These one-step interpreted queries are unnamed (anonymous) and parameterless, just like SQL.
To send an anonymous query to the interpret engine, replace the keyword CREATE with INTERPRET. Remember, no parameters:
Recommendation: Increase the query timeout threshold.
Interpreted queries may run slower than installed queries, so we recommend increasing the query timeout threshold:
Examples of 1-Hop Fixed Length Query
Example 1. Find persons who know the person named "Viktor Akhiezer" and return the top 3 oldest such persons.
You can copy the above GSQL script to a file named example1.gsql and invoke this script file in Linux.
Example 2. Do the same as Example 1, but use a right-directed edge pattern.
You can copy the above GSQL script to a file named example2.gsql, and invoke this script file in Linux.
The output should be the same as example1's output.
Example 3. Find Viktor Akhiezer's total number of comments, total number of posts, and total number of persons he knows. A Person can reach Comments, Posts and other Persons via a directed edge.
You can copy the above GSQL script to a file named example3.gsql, and invoke this script file in Linux.
Example 4. Do the same as Example 3, but use a left-directed edge pattern.
Note below (line 10) that the Seed is now {Person.*, Comment.*, Post.* }, the three types of entities that are targets of edges from a Person.
In the current version, the vertex set on the left side of the pattern must be defined in a previous statement (e.g., a seed statement), the same requirement as in v1 syntax FROM clauses. In the example below, the current version of pattern matching would not permit FROM _:s -(<:e) - Person:tgt
You can copy the above GSQL script to a file named example4.gsql, and invoke this script file in linux command line. The output should be the same as in Example 3.
Example 5. Find the two oldest persons who either know "Viktor Akhiezer" or are known by "Vicktor Akhiezer". KNOWS is a directed relationship, so we need to include both directions in the pattern.
You can copy the above GSQL script to a file named example5.gsql, and invoke this script file in Linux:
Example 6. Find the total comments or posts created by "Viktor Akhiezer". Again, we include two types of edges, but in this case, we count them together.
You can copy the above GSQL script to a file named example6.gsql, and invoke this script file in Linux:
Last updated