Per Clause (Beta)

Introduction

Pattern matching produces a virtual match table, and the ACCUM clause acts like a FOREACH loop, executing the clause's statement once for each row of the match table.
Patterns are paths in the graphs, and each row in the match table is a distinct path. However, paths may share some vertices or edges. Some applications do not want to do aggregations per path. Instead, they want to execute the ACCUM clause per distinct group of vertex aliases.
For example, consider the following query which counts the number of paths in a simple 2-hop pattern:
1
SumAccum<int> @@cnt;
2
3
S = SELECT t
4
FROM S:s - (E1:edge1) - M:m -(E2:edge2) - T:t
5
ACCUM @@cnt += 1;
Copied!
Suppose the query produces the following match table.
1
(s, edge1, m , edge2, t)//match table schema
2
v1, e1, v3, e2, v2 //match 1
3
v1, e3, v4, e4, v2 //match 2
4
v5, e5, v6, e6, v7 //match 3
5
v8, e7, v9, e8, v7 //match 4
6
Copied!
By default, the ACCUM clause will execute the @@cnt += 1statement 4 times, for each row in the match table. The result will be @@cnt = 4.
For the same query, what if the user wants to
  • count the number of distinct path endings in the match table? For this case, we would want to iterate on the alias t.
  • count the number of distinct (start, end) pairs in the match table? For that case, we would want to iterate on distinct pairs of the aliases (s, t).
To provide users with this added flexibility and finer control over ACCUM iteration, TigerGraph 3.0 adds the PER clause to pattern matching (V2) syntax.

Syntax

The PER Clause is an optional clause that comes at the start of the ACCUM clause in a SELECT statement. As illustrated below, it starts with the keyword PER, and followed by a pair of parenthesis, in which user can put one or more distinct vertex aliases found in the FROM pattern.
1
selectBlock := SELECT alias
2
FROM pattern
3
[sampleClause]
4
[whereClause]
5
[[perClause] accumClause]
6
[postAccumClause]*
7
[havingClause]
8
[orderClause]
9
[limitClause]
10
11
perClause := PER (vertex_alias_1, vertex_alias_2, ...)
12
Copied!
Examples. Below are multiple examples of the PER Clause using the same FROM clause.
1
S1 = SELECT s
2
FROM S:s - (E1:edge1) - M:m - (E2:edge2) - T:t
3
PER (s)
4
ACCUM @@cnt += 1;
5
6
S2 = SELECT t
7
FROM S:s - (E1:edge1) - M:m - (E2:edge2) - T:t
8
PER (t)
9
ACCUM @@cnt += 1;
10
11
S3 = SELECT m
12
FROM S:s - (E1:edge1) - M:m - (E2:edge2) - T:t
13
PER (m)
14
ACCUM @@cnt += 1;
15
16
S4 = SELECT t
17
FROM S:s - (E1:edge1) - M:m - (E2:edge2) - T:t
18
PER (s, t)
19
ACCUM @@cnt += 1;
20
21
S5 = SELECT t
22
FROM S:s - (E1:edge1) - M:m - (E2:edge2) - T:t
23
PER (s, m, t)
24
ACCUM @@cnt += 1;
Copied!

Semantics

The PER Clause specifies a list of vertex aliases, which are used to group the rows in the match table, one group per distinct value of the alias or of the alias list. If there are N distinct groups, we will execute the ACCUM clause N times, once per distinct vertex aliases' binding. Note that the PER clause has no effect on POST-ACCUM clauses semantic, except confining the POST-ACCUM vertex alias.
Suppose s, m, and t are vertex aliases in a pattern. Below are some interpretations of the PER Clause based on the graph element bindings found in the match table.
  • PER (s) ACCUM means that per each distinct s vertex, execute the ACCUM clause once.
  • PER (s,t) ACCUM means that per each distinct (s, t) pair, execute the ACCUM clause once.
  • PER (s,m,t) ACCUM means that per each distinct (s, m, t) tuple, execute the ACCUM clause once.
Examples to show PER clause semantics.
1
//match table
2
(s, edge1, m , edge2, t)//schema
3
v1, e1, v3, e2, v2 //match 1
4
v1, e3, v4, e4, v2 //match 2
5
v5, e5, v6, e6, v7 //match 3
6
v8, e7, v9, e8, v7 //match 4
7
8
//since we have v1, v5, and v8 three distinct vertices bind to s,
9
//we execute ACCUM clause 3 times.
10
S1 = SELECT s
11
FROM S:s - (E1:edge1) - M:m -(E2:edge2) - T:t
12
PER (s)
13
ACCUM @@cnt += 1;
14
15
//since we have v2, v7 two distinct vertices bind to t,
16
//we execute ACCUM clause twice.
17
S2 = SELECT t
18
FROM S:s - (E1:edge1) - M:m -(E2:edge2) - T:t
19
PER (t)
20
ACCUM @@cnt += 1;
21
22
//since we have v3, v4, v6, v9 four distinct vertices bind to m,
23
//we execute ACCUM clause 4 times.
24
S3 = SELECT m
25
FROM S:s - (E1:edge1) - M:m -(E2:edge2) - T:t
26
PER (m)
27
ACCUM @@cnt += 1;
28
29
//since we have (v1, v2), (v5, v7) and (v8, v7) three distinct vertex pairs
30
//bind to (s,t), we execute ACCUM clause 3 times.
31
S4 = SELECT t
32
FROM S:s - (E1:edge1) - M:m -(E2:edge2) - T:t
33
PER (s, t)
34
ACCUM @@cnt += 1;
35
36
37
//since we have (v1, v3, v2), (v1, v4, v2), (v5, v6, v7) and (v8, v9, v7) four
38
//distinct vertex groups bind to (s,m,t), we execute ACCUM clause 4 times.
39
S5 = SELECT t
40
FROM S:s - (E1:edge1) - M:m -(E2:edge2) - T:t
41
PER (s, m, t)
42
ACCUM @@cnt += 1;
Copied!
If the PER Clause is used in a SELECT query block, then the vertex aliases used in the SELECT, ACCUM , and POST-ACCUM clauses must be confined to the aliases that appear in the PER clause.
Below are some illegal cases.
1
//semantic error. SELECT t, but t doesn't appear in PER clause.
2
S1 = SELECT t
3
FROM S:s - (E1:edge1) - M:m -(E2:edge2) - T:t
4
PER (s, m)
5
ACCUM @@cnt += 1;
6
7
//semantic error. ACCUM [email protected], but t doesn't appear in PER clause.
8
S2 = SELECT t
9
FROM S:s - (E1:edge1) - M:m -(E2:edge2) - T:t
10
PER (s, m)
11
ACCUM t.@cnt += 1;
12
13
//semantic error. POST-ACCUM [email protected], but t doesn't appear in PER clause.
14
S3 = SELECT s
15
FROM S:s - (E1:edge1) - M:m -(E2:edge2) - T:t
16
PER (s)
17
ACCUM s.@cnt += 1
18
POST-ACCUM t.@cnt =1;
Copied!

PER Clause Examples

Example 1. Count the number of Countries that has a City which has a resident that likes a post.
1
//Example 1.
2
USE GRAPH ldbc_snb
3
4
INTERPRET QUERY () SYNTAX v2 {
5
SumAccum<int> @@cnt;
6
7
R = SELECT c
8
FROM Country:c -(<IS_PART_OF.<IS_LOCATED_IN.LIKES>)- Post:p
9
PER (c)
10
ACCUM @@cnt +=1;
11
12
PRINT @@cnt;
13
}
14
15
//results
16
Using graph 'ldbc_snb'
17
The query AA is dropped.
18
{
19
"error": false,
20
"message": "",
21
"version": {
22
"schema": 0,
23
"edition": "enterprise",
24
"api": "v2"
25
},
26
"results": [{"@@cnt": 111}]
27
}
Copied!
Example 2. Count the number of posts liked by a person who is located in a city that belongs to a country. (All cities are in a country, but humor us. We are reusing the same FROM pattern in several examples.)
1
//Example 2.
2
USE GRAPH ldbc_snb
3
4
INTERPRET QUERY () SYNTAX v2 {
5
SumAccum<int> @@cnt;
6
7
R = SELECT p
8
FROM Country:c -(<IS_PART_OF.<IS_LOCATED_IN.LIKES>)- Post:p
9
PER (p)
10
ACCUM @@cnt +=1;
11
12
PRINT @@cnt;
13
14
//result
15
Using graph 'ldbc_snb'
16
{
17
"error": false,
18
"message": "",
19
"version": {
20
"schema": 0,
21
"edition": "enterprise",
22
"api": "v2"
23
},
24
"results": [{"@@cnt": 70668}]
25
}
Copied!
Example 3. Find for each country in ("Dominican_Republic","Angola", "Cambodia") the number of posts that is liked by a person living in that country.
1
//Exmaple 3
2
USE GRAPH ldbc_snb
3
4
INTERPRET QUERY () SYNTAX v2{
5
6
MapAccum<string, SumAccum<int>> @@postPerCountry;
7
8
R = SELECT p
9
FROM Country:c -(<IS_PART_OF.<IS_LOCATED_IN.LIKES>)- Post:p
10
WHERE c.name in ("Dominican_Republic","Angola", "Cambodia")
11
PER (c, p)
12
ACCUM @@postPerCountry += (c.name -> 1);
13
14
PRINT @@postPerCountry;
15
}
16
17
//results
18
Using graph 'ldbc_snb'
19
{
20
"error": false,
21
"message": "",
22
"version": {
23
"schema": 0,
24
"edition": "enterprise",
25
"api": "v2"
26
},
27
"results": [{"@@postPerCountry": {
28
"Dominican_Republic": 395,
29
"Angola": 12,
30
"Cambodia": 4002
31
}}]
32
}
Copied!
Example 4. Find for each country in ("Dominican_Republic","Angola", "Cambodia") the number of posts that is liked by a person living in that country. Use local accumulators this time.
1
USE GRAPH ldbc_snb
2
3
INTERPRET QUERY () SYNTAX v2{
4
5
SumAccum<int> @postCnt;
6
7
R = SELECT c
8
FROM Country:c -(<IS_PART_OF.<IS_LOCATED_IN.LIKES>)- Post:p
9
WHERE c.name in ("Dominican_Republic","Angola", "Cambodia")
10
PER (c, p) //per (country, post), add 1 to [email protected]
11
ACCUM c.@postCnt += 1;
12
13
PRINT R;
14
}
15
16
//results
17
Using graph 'ldbc_snb'
18
{
19
"error": false,
20
"message": "",
21
"version": {
22
"schema": 0,
23
"edition": "enterprise",
24
"api": "v2"
25
},
26
"results": [{"R": [
27
{
28
"v_id": "2",
29
"attributes": {
30
"@postCnt": 12,
31
"name": "Angola",
32
"id": 2,
33
"url": "http://dbpedia.org/resource/Angola"
34
},
35
"v_type": "Country"
36
},
37
{
38
"v_id": "67",
39
"attributes": {
40
"@postCnt": 4002,
41
"name": "Cambodia",
42
"id": 67,
43
"url": "http://dbpedia.org/resource/Cambodia"
44
},
45
"v_type": "Country"
46
},
47
{
48
"v_id": "11",
49
"attributes": {
50
"@postCnt": 395,
51
"name": "Dominican_Republic",
52
"id": 11,
53
"url": "http://dbpedia.org/resource/Dominican_Republic"
54
},
55
"v_type": "Country"
56
}
57
]}]
58
}
Copied!

Performance and Best Practices

The PER Clause not only helps users to control the semantics of the ACCUM clause, it also boosts the performance of the pattern match query, as it uses the PER clause to optimize the query execution.
To get the best performance, we recommend three guidelines for writing efficient queries.

Use PER (target) If Possible

Per target is in general faster than Per source. In the example below, query q2 is faster than q1. The only difference between these two queries is q2's FROM pattern is the flip of q1's FROM pattern.
1
USE GRAPH ldbc_snb
2
3
# not recommended, since it does per (src).
4
CREATE QUERY q1 () SYNTAX v2 {
5
6
SumAccum<int> @@cnt ;
7
8
T = SELECT c
9
FROM Comment:c - (<LIKES) - Person:ps - (IS_LOCATED_IN>) - City:city
10
WHERE year(c.creationDate) >= 2006
11
PER (c)
12
ACCUM @@cnt += 1;
13
14
PRINT @@cnt;
15
}
16
17
# recommended, since it does per (tgt)
18
CREATE QUERY q2 () SYNTAX v2 {
19
20
SumAccum<int> @@cnt ;
21
22
T = SELECT c
23
FROM City:city - (<IS_LOCATED_IN) - Person:ps - (LIKES>) - Comment:c
24
WHERE year(c.creationDate) >= 2006
25
PER (c)
26
ACCUM @@cnt += 1;
27
28
PRINT @@cnt;
29
}
Copied!

Write Patterns with Smallest Expected Vertex Set on the Left

The match table is built by traversing the pattern from left to right. Follow the basic principle of pruning early rather than late by orienting the query the smaller cardinality set on the left. This practice will result in producing the least number of candidate matches during the query computation. For example, if there are fewer distinct tags than persons, then query q4 is faster than q3.
1
USE GRAPH ldbc_snb
2
3
# not recommended, since the pattern starts from a large cardinality vertex type
4
# (Person), and ends at a small cardinality vertex type (Tag).
5
CREATE QUERY q3 () SYNTAX v2 {
6
7
SumAccum<int> @cnt;
8
9
V = SELECT s
10
FROM Person:s- (LIKES>)-Post:p - (<CONTAINER_OF)-:f - (HAS_TAG>) - :t
11
PER (s)
12
ACCUM s.@cnt += 1;
13
14
PRINT V.size();
15
}
16
17
# recommended, start from small cardinality end (Tag), and use per tgt
18
CREATE QUERY q4 () SYNTAX v2 {
19
20
SumAccum<int> @cnt;
21
22
V = SELECT s
23
FROM Tag:t-(<HAS_TAG)-Forum:f -(CONTAINER_OF>)-Post:p - (<LIKES)- Person:s
24
PER (s)
25
ACCUM s.@cnt += 1;
26
27
PRINT V.size();
28
}
Copied!

Specify Complete Type Information

Specifying complete type information improves performance. For example, query q6 is faster than q5 even though they are known to be logically identical. Forum is the CONTAINER_OF Post, so it does not need to be specified in q5, but explicitly saying Forum in q6 speeds up performance.
1
USE GRAPH ldbc_snb
2
3
#we do not put Forum befoe :f, even if we know it.
4
CREATE QUERY q5 () SYNTAX v2 {
5
6
SumAccum<int> @@person_cnt;
7
8
V = SELECT s
9
FROM Person:s- (LIKES>)-Post:p - (<CONTAINER_OF)-:f - (HAS_TAG>) - :t
10
PER (s)
11
ACCUM @@person_cnt += 1;
12
13
PRINT @@person_cnt;
14
}
15
16
17
#recommended: we put Forum as the type info.
18
CREATE QUERY q6 () SYNTAX v2 {
19
20
SumAccum<int> @@person_cnt;
21
22
V = SELECT s
23
FROM Person:s- (LIKES>)-Post:p - (<CONTAINER_OF)-Forum:f - (HAS_TAG>) - :t
24
PER (s)
25
ACCUM @@person_cnt += 1;
26
27
PRINT @@person_cnt;
28
}
Copied!

LDBC Benchmark Queries

Using the PER clause and linear regular path pattern, we have translated all of the LDBC-SNB queries. You can find them on github at https://github.com/tigergraph/ecosys/tree/ldbc/ldbc_benchmark/tigergraph/queries_linear/queries. Most of the queries are installed as functions. You can find sample parameter(s) of the functions from https://github.com/tigergraph/ecosys/tree/ldbc/ldbc_benchmark/tigergraph/queries/seeds.
Last modified 1yr ago