Conjunctive Pattern Matching (Beta)

What is Conjunctive Pattern Matching?

So far, we have described pattern matching as one path pattern in a FROM clause. In this section, we introduce GSQL's capability to match multiple patterns in one FROM clause. This extension is called Conjunctive Pattern Matching (CPM), because the query asks for the conjunction (logical AND) of the patterns. To get a match, all of the patterns must be satisfied, and the patterns can interrelate. Visually, you can think of patterns formed by a set of intersecting line segments. This feature, introduced as a Beta feature in TigerGraph 3.0, enables you to express complex patterns concisely in a single query block.
In general, a CPM query block consists of multiple patterns in the FROM clause. It has a structure illustrated below.
# Conjunctive Pattern Matching Syntax
1
SelectBlock := SELECT alias
2
FROM pattern
3
[sampleClause]
4
[whereClause]
5
[ [perClause] accumClause]
6
[postAccumClause]*
7
...
8
9
pattern := vertexPattern | edgePattern | (pathPattern ["," pathPattern])
10
# vertexPattern and edgePattern are from classic GSQL
Copied!
We elaborate on each of the clause.

SELECT Clause

The SELECT clause selects only one vertex alias from all the patterns in the FROM clause.

FROM Clause - Conjunctive Matching

This is where the conjunctive matching is expressed. The FROM clause consists of a list of path patterns, which are separated by commas. Evaluating each pattern against the underlying graph data produces a match table. If two patterns share a vertex alias, then we form the natural join of the two match tables.
For example, consider this CPM:
1
FROM X:x - (E1:e1) - Y:y - (E2>:e2) - Z:z,
2
Z:z - (E3:e3) - U:u - (E4>:e4) - V:v
Copied!
The first pattern's variables are x, e1, y, e2, and z; the second pattern's variables are z, e3, u, e4, and v. Considering the two patterns independently would yield the follwing match table schemas:
1
#first match table
2
(x, e1, y, e2, z)
3
#second match table
4
(z, e3, u, e4, v)
Copied!

Natural Join of Match Tables

Natural joining two match tables compares all the shared vertex aliases between the two tables, and the resulting joined table contains all non-shared variables plus one copy of each of the shared vertex variables. Here is the match table for the CPM above:
1
#natural join result; the shared vertex variable z appears once.
2
(x, e1, y, e2, z, e3, u, e4, v)
Copied!

Valid Conjunctive Patterns

The match table of the conjunctive pattern match is the natural join of all the patterns' match tables. By design, a row in the CPM match table must simultaneously satisfy all the match tables.
If the match tables of the patterns in a FROM clause can be naturally joined into one match table, then the FROM clause has a valid CPM input. Otherwise, the FROM clause has an invalid pattern input list.
For example, below we show two valid CPM inputs and one invalid CPM input.
1
# a valid CPM, since the two patterns natrually join on :tgt
2
SELECT
3
FROM Person:p - (KNOWS) - :tgt,
4
Post:s -(<LIKES) - :tgt
5
6
# a valid CPM, since the two patterns naturally join on :f
7
SELECT
8
FROM Person:p - (KNOWS) - :f - (LIKES>) - Post:tgt,
9
:f - (LIKES>) - Comment:c
10
11
# an invalid CPM, since the two patterns do not share any vertex variables.
12
# they cannot be naturally joined.
13
# One pattern has (p, tgt); the other has (s, t).
14
SELECT
15
FROM Person:p - (KNOWS) - :tgt,
16
Post:s - (<LIKES) - Person:t
Copied!

WHERE Clause

The predicates in the WHERE clause can use any of the vertex or edge aliases in any of the patterns, including predicates which combine variables from different constituent paths. CPM queries do not have any special restrictions on the WHERE predicate. Distance matters, however, for performance. Conditions that are local, measured both cross-path and within-path, can be resolved earlier and therefore are faster.
In the example below, x2.age > x4.age is a cross-pattern predicate, e1.timestamp < e3.timestamp is a cross-pattern predicate, and x1.gender == x4.gender is a local predicate of the second pattern.
1
FROM X1:x1-(E1:e1)-X2:x2-(E2:e2)-X3:x3,
2
X1:x1-(E3:e3)-X4:x4
3
WHERE x2.age > x4.age AND e1.timestamp < e3.timestamp AND x1.gender == x4.gender
Copied!

ACCUM Clause

You can ACCUM to any vertex variable in a CPM block.
The ACCUM clause by default will execute as many times as the row (match) count of the CPM match table; each execution uses one row from the match table.
ACCUM To The Three Vertex Variables of A CPM Pattern
1
#accum to x1, x2 and x4.
2
FROM X1:x1-(E1:e1)-X2:x2-(E2:e2)-X3:x3,
3
X1:x1-(E3:e3)-X4:x4
4
ACCUM x1.@cnt +=1, x2.@cnt += x3.quantity, x4.@cnt += x3.quantity
Copied!

POST-ACCUM Clause

POST-ACCUM for CPM behaves the same as POST-ACCUM for single path patterns. That is, each POST-ACCUM clause can refer to one vertex alias, and the clause executes iteratively over that vertex set (e.g. one vertex column in the matching table). Its statements can access the aggregated accumulator result computed in the ACCUM clause. The query can have multiple POST-ACCUM clauses, one for each vertex alias you wish to work on. The multiple POST-ACCUM clauses are processed in parallel; it doesn't matter in what order you write them. (For each binding, the statements within a clause are executed in order.)
For example, below we have three POST-ACCUM clauses. The first one iterates throughx1, and for each x1, we do @@cnt += [email protected]. The second and third POST-ACCUMs iterate through x2 and x3 respectively, and accumulates their @cnt accumulator value into @@cnt.
POST-ACCUM to a global accumulator @@cnt, using three CPM Vertex Variables
1
2
FROM X1:x1-(E1:e1)-X2:x2-(E2:e2)-X3:x3,
3
X1:x1-(E3:e3)-X4:x4
4
ACCUM x1.@cnt +=1, x2.@cnt += x3.quantity, x4.@cnt += x3.quantity
5
POST-ACCUM @@cnt += x1.@cnt
6
POST-ACCUM @@cnt += x2.@cnt
7
POST-ACCUm @@cnt += x3.@cnt;
Copied!

Examples

Example 1. Find Viktor Akhiezer's liked messages (100+ days after their creation) whose author's last name begin with letter S. Output the message's forum.
1
USE GRAPH ldbc_snb
2
3
INTERPRET QUERY () SYNTAX v2 {
4
5
SumAccum<int> @@cnt;
6
7
F = SELECT f
8
FROM Person:s - (LIKES>:e1) - :msg - (HAS_CREATOR>) - Person:t,
9
Forum:f - (CONTAINER_OF>:e2) - :msg
10
WHERE s.firstName == "Viktor" AND s.lastName == "Akhiezer"
11
AND t.lastName LIKE "S%"
12
AND e1.creationDate >DATETIME_ADD(msg.creationDate, INTERVAL 100 DAY);
13
14
PRINT F;
15
}
16
17
#result
18
{
19
"error": false,
20
"message": "",
21
"version": {
22
"schema": 0,
23
"edition": "enterprise",
24
"api": "v2"
25
},
26
"results": [{"F": [{
27
"v_id": "962072688797",
28
"attributes": {
29
"id": 962072688797,
30
"title": "Album 12 of Mario Santos",
31
"creationDate": "2011-04-12 09:36:50"
32
},
33
"v_type": "Forum"
34
}]}]
35
}
Copied!
Example 2. Find any authors who wrote posts that Viktor Akhiezer's liked and whose last name begins with S. Find the country for each of these authors and report on the countries.
1
USE GRAPH ldbc_snb
2
3
INTERPRET QUERY () SYNTAX v2 {
4
5
SumAccum<int> @@cnt;
6
7
C = SELECT ctry
8
FROM Person:s - (LIKES>:e1) - Post:msg - (HAS_CREATOR>) - Person:t,
9
:t - (WORK_AT>:e2) - Company:c,
10
:c - (IS_LOCATED_IN>) - Country:ctry
11
WHERE s.firstName == "Viktor" AND s.lastName == "Akhiezer"
12
AND t.lastName LIKE "S%" ;
13
14
PRINT C;
15
}
16
17
#result
18
{
19
"error": false,
20
"message": "",
21
"version": {
22
"schema": 0,
23
"edition": "enterprise",
24
"api": "v2"
25
},
26
"results": [{"C": [{
27
"v_id": "93",
28
"attributes": {
29
"name": "Portugal",
30
"id": 93,
31
"url": "http://dbpedia.org/resource/Portugal"
32
},
33
"v_type": "Country"
34
}]}]
35
}
Copied!
Example 3. Given a TagClass and a Country, find all the Forums created in the given Country, containing at least one Post with Tags belonging directly to the given TagClass. The location of a Forum is identified by the location of the Forum’s moderator.
1
USE GRAPH ldbc_snb
2
3
DROP QUERY bi_4
4
5
CREATE QUERY bi_4(string tcName, string cName) for graph ldbc_snb syntax v2 {
6
SetAccum<vertex<Post>> @postSet;
7
SumAccum<int> @personId, @postCount;
8
9
ForumSet =
10
SELECT f
11
FROM Forum:f -(HAS_MODERATOR>)- Person:a -(IS_LOCATED_IN>.IS_PART_OF>)- Country:c,
12
:f -(CONTAINER_OF>)- Post:p -(HAS_TAG>.HAS_TYPE>)- TagClass:tc
13
WHERE c.name == cName and tc.name == tcName
14
ACCUM f.@personId = a.id, f.@postSet += p
15
POST-ACCUM f.@postCount = f.@postSet.size(), f.@postSet.clear()
16
ORDER BY f.@postCount DESC, f.id ASC
17
LIMIT 3;
18
19
PRINT ForumSet[ForumSet.id, ForumSet.title, ForumSet.creationDate,
20
ForumSet.@personId, ForumSet.@postCount];
21
}
22
23
24
Copied!
1
INSTALL QUERY bi_4
2
3
RUN QUERY bi_4("MusicalArtist", "Burma")
4
5
#result
6
{
7
"error": false,
8
"message": "",
9
"version": {
10
"schema": 0,
11
"edition": "enterprise",
12
"api": "v2"
13
},
14
"results": [{"ForumSet": [
15
{
16
"v_id": "81903",
17
"attributes": {
18
"ForumSet.title": "Wall of Donald Steele-Perkins",
19
20
"ForumSet.id": 81903,
22
"ForumSet.creationDate": "2010-02-15 06:48:04"
23
},
24
"v_type": "Forum"
25
},
26
{
27
"v_id": "137438953686",
28
"attributes": {
29
"ForumSet.title": "Wall of Eric Law-Yone",
30
"[email protected]": 2199023262994,
31
"ForumSet.id": 137438953686,
33
"ForumSet.creationDate": "2010-04-25 22:10:32"
34
},
35
"v_type": "Forum"
36
},
37
{
38
"v_id": "687194810508",
39
"attributes": {
40
"ForumSet.title": "Wall of Hector Hugh Michie",
41
"[email protected]": 10995116283784,
42
"ForumSet.id": 687194810508,
44
"ForumSet.creationDate": "2010-12-19 15:33:30"
45
},
46
"v_type": "Forum"
47
}
48
]}]
49
}
Copied!
Example 4. For a given country, count all the distinct triples of Persons such that:
  • a is a friend of b.
  • b is a friend of c
  • c is a friend of a.
Distinct means that if a certain 3 vertices appear once in the results, it will not be repeated: it will appear only once. KNOWS is an undirected relationship, so it doesn't matter in what order we list the 3 vertices.
1
2
USE GRAPH ldbc_snb
3
4
CREATE QUERY bi_17(string cName) FOR GRAPH ldbc_snb SYNTAX v2 {
5
TYPEDEF TUPLE <uint a, uint b, uint c> triplet;
6
SetAccum<triplet> @@tripletSet;
7
SumAccum<int> @@tripletCount;
8
9
C =
10
SELECT c
11
FROM Country:c -(<IS_PART_OF.<IS_LOCATED_IN)- Person:p1,
12
:c -(<IS_PART_OF.<IS_LOCATED_IN)- Person:p2,
13
:c -(<IS_PART_OF.<IS_LOCATED_IN)- Person:p3,
14
:p1 -(KNOWS)- :p2 -(KNOWS)- :p3 -(KNOWS)- :p1
15
WHERE c.name == cName AND p1.id < p2.id AND p2.id < p3.id
16
ACCUM @@tripletSet += triplet(p1.id, p2.id, p3.id);
17
18
@@tripletCount = @@tripletSet.size();
19
@@tripletSet.clear();
20
PRINT @@tripletCount;
21
}
22
23
24
INSTALL QUERY bi_17
25
26
RUN QUERY bi_17("Spain")
27
28
#result
29
{
30
"error": false,
31
"message": "",
32
"version": {
33
"schema": 0,
34
"edition": "enterprise",
35
"api": "v2"
36
},
37
"results": [{"@@tripletCount": 242}]
38
}
Copied!
More Examples. We translated LDBC-SNB BI and IC queries using CPM, and shared the translation in github. Please refer to the query translation here. Most of the queries are installed as functions, you can find sample parameter(s) of the functions from here.

Source Vertex Set Flexibility

As mentioned when we first described pattern matching, in One-hop patterns, the source (leftmost) vertex set can be a vertex type, an alternation of types, or even omitted.
Example 1. Find Viktor Akhiezer's favorite messages' creators whose last name begins with letter S. Count them.
1
2
USE GRAPH ldbc_snb
3
4
#start from a vertex type "Person"
5
INTERPRET QUERY () SYNTAX v2 {
6
7
SumAccum<int> @@cnt;
8
9
F = SELECT t
10
FROM Person:s -(LIKES>:e1)- :msg -(HAS_CREATOR>)- Person:t
11
WHERE s.firstName == "Viktor" AND s.lastName == "Akhiezer"
12
AND t.lastName LIKE "S%"
13
POST-ACCUM @@cnt+=1;
14
15
PRINT @@cnt;
16
17
}
18
#result
19
{
20
"error": false,
21
"message": "",
22
"version": {
23
"schema": 0,
24
"edition": "enterprise",
25
"api": "v2"
26
},
27
"results": [{"@@cnt": 8}]
28
}
Copied!
Example 2. Same query as example 1, but without beginning with vertex types. GSQL compiler can infer the types of :s.
1
USE GRAPH ldbc_snb
2
3
#both end points of the pattern do not have vertex types.
4
INTERPRET QUERY () SYNTAX v2 {
5
6
SumAccum<int> @@cnt;
7
8
F = SELECT t
9
FROM :s -(LIKES>:e1)- :msg -(HAS_CREATOR>)- :t
10
WHERE s.firstName == "Viktor" AND s.lastName == "Akhiezer" AND t.lastName LIKE "S%"
11
POST-ACCUM @@cnt+=1;
12
13
PRINT @@cnt;
14
15
}
16
#result
17
{
18
"error": false,
19
"message": "",
20
"version": {
21
"schema": 0,
22
"edition": "enterprise",
23
"api": "v2"
24
},
25
"results": [{"@@cnt": 8}]
26
}
Copied!
Example 3. Count the LIKES edge.
1
USE GRAPH ldbc_snb
2
3
# a pattern starts without any information.
4
INTERPRET QUERY () SYNTAX v2 {
5
6
SumAccum<int> @@cnt;
7
8
F = SELECT msg
9
FROM -(LIKES>:e1)- :msg
10
ACCUM @@cnt+=1;
11
12
PRINT @@cnt;
13
14
}
15
#result
16
{
17
"error": false,
18
"message": "",
19
"version": {
20
"schema": 0,
21
"edition": "enterprise",
22
"api": "v2"
23
},
24
"results": [{"@@cnt": 2190095}]
25
}
Copied!
Last modified 1yr ago