Data Modification
Pattern Matching GSQL supports Insert, Update, and Delete operations. The syntax is identical to that in classic GSQL (v1), though the full range of data modification operations are not yet support.
In general, data modification can be at two levels in GSQL:
  1. 1.
    Top level. The statement does not need to within any other statement.
  2. 2.
    Within a SELECT query statement. The FROM-WHERE clauses define a match table, and the data modification is performed based on the vertex and edge information in the match table. The GSQL specifications calls these within-SELECT statements DML-sub statements.
  • Insert, Update, and Delete currently work in compiled mode only (e.g., you must run INSTALL QUERY before RUN QUERY.) Data Modification in interpreted mode is not yet available.
  • SELECT queries with data modification may only have one POST-ACCUM clause.

Insert vertices and edges

Pattern matching Insert is supported at both the top-level and within-SELECT levels, using the same syntax as in classic GSQL. You can insert vertices and edges.
Example 1. Create a Person vertex, whose name is Tiger Woods. Next, find Viktor's favorite 2012 posts' authors, whose last name is prefixed with S. Finally, insert KNOWS edges connecting Tiger Woods with Viktor's favorite authors.
1
USE GRAPH ldbc_snb
2
3
#find Viktor's 2012 favorite posts' authors, whose lastName prefix with S.
4
INTERPRET QUERY() SYNTAX V2 {
5
6
R = SELECT t
7
FROM Person:s -(LIKES>)- :msg -(HAS_CREATOR>)- Person:t
8
WHERE s.firstName == "Viktor" AND s.lastName == "Akhiezer"
9
AND t.lastName LIKE "S%" AND year(msg.creationDate) == 2012;
10
11
PRINT R[R.id, R.firstName, R.lastName];
12
}
13
14
#result
15
{
16
"error": false,
17
"message": "",
18
"version": {
19
"schema": 0,
20
"edition": "enterprise",
21
"api": "v2"
22
},
23
"results": [{"R": [
24
{
25
"v_id": "8796093025410",
26
"attributes": {
27
"R.id": 8796093025410,
28
"R.firstName": "Priyanka",
29
"R.lastName": "Singh"
30
},
31
"v_type": "Person"
32
},
33
{
34
"v_id": "2199023260091",
35
"attributes": {
36
"R.id": 2199023260091,
37
"R.firstName": "Janne",
38
"R.lastName": "Seppala"
39
},
40
"v_type": "Person"
41
},
42
{
43
"v_id": "15393162796846",
44
"attributes": {
45
"R.id": 15393162796846,
46
"R.firstName": "Mario",
47
"R.lastName": "Santos"
48
},
49
"v_type": "Person"
50
}
51
]}]
52
}
53
54
# create a Person node, whose name is tiger,
55
# and connect this Person with above Victor's favorite authors
56
CREATE QUERY InsertEdgeAndVertex () SYNTAX v2{
57
58
#add a celebrity person node using INSERT INTO statement.
59
INSERT INTO Person VALUES (100000000,"Tiger", "Woods", "m", _, _,_,_,_,_);
60
61
R = SELECT t
62
FROM Person:s -(LIKES>)- :msg -(HAS_CREATOR>)- Person:t
63
WHERE s.firstName == "Viktor" AND s.lastName == "Akhiezer"
64
AND t.lastName LIKE "S%" AND year(msg.creationDate) == 2012
65
PER (s, t)
66
ACCUM
67
#add edges connecting "tiger" and t with a 6/1/2020 time stamp
68
INSERT INTO KNOWS VALUES(100000000, t, to_datetime("2020-06-01"));
69
70
PRINT R [R.id, R.firstName, R.lastName];
71
}
72
73
INSTALL QUERY InsertEdgeAndVertex
74
RUN QUERY InsertEdgeAndVertex()
Copied!
You can verify the result by running a simple built-in REST endpoint.
Check the inserted vertex.
Linux Shell
1
#check the inserted vertex
2
curl -X GET "http://localhost:9000/graph/ldbc_snb/vertices/Person/100000000" | jq .
3
4
#result
5
{
6
"version": {
7
"edition": "enterprise",
8
"api": "v2",
9
"schema": 1
10
},
11
"error": false,
12
"message": "",
13
"results": [
14
{
15
"v_id": "100000000",
16
"v_type": "Person",
17
"attributes": {
18
"id": 100000000,
19
"firstName": "Tiger",
20
"lastName": "Woods",
21
"gender": "m",
22
"birthday": "1970-01-01 00:00:00",
23
"creationDate": "1970-01-01 00:00:00",
24
"locationIP": "",
25
"browserUsed": "",
26
"speaks": [],
27
"email": []
28
}
29
}
30
]
31
}
Copied!
Check the inserted edges.
Linux file
1
#check the inserted edges using tiger's id (100,000,000)
2
curl -X GET "http://localhost:9000/graph/ldbc_snb/edges/Person/100000000/KNOWS" | jq .
3
#result
4
{
5
"version": {
6
"edition": "enterprise",
7
"api": "v2",
8
"schema": 0
9
},
10
"error": false,
11
"message": "",
12
"results": [
13
{
14
"e_type": "KNOWS",
15
"directed": false,
16
"from_id": "100000000",
17
"from_type": "Person",
18
"to_id": "8796093025410",
19
"to_type": "Person",
20
"attributes": {
21
"creationDate": "2020-06-01 00:00:00"
22
}
23
},
24
{
25
"e_type": "KNOWS",
26
"directed": false,
27
"from_id": "100000000",
28
"from_type": "Person",
29
"to_id": "2199023260091",
30
"to_type": "Person",
31
"attributes": {
32
"creationDate": "2020-06-01 00:00:00"
33
}
34
},
35
{
36
"e_type": "KNOWS",
37
"directed": false,
38
"from_id": "100000000",
39
"from_type": "Person",
40
"to_id": "15393162796846",
41
"to_type": "Person",
42
"attributes": {
43
"creationDate": "2020-06-01 00:00:00"
44
}
45
}
46
]
47
}
48
#note you can use the vertex lookup API to verify the three connected authors. E.g
49
curl -X GET "http://localhost:9000/graph/ldbc_snb/vertices/Person/8796093025410" | jq .
50
Copied!

Update data

To update vertex attributes, use assignment statements in a POST-ACCUM clause. To update edge attributes, use assignment statements in an ACCUM clause. In addition, data updates can only be performed if the FROM statement only contains a single-hop and fix-length pattern.
Query-body level UPDATE statements are not yet supported in syntax v2.
Example 2. For all KNOWS edges that connect Viktor Akhiezer and his friends whose lastName begins with "S", update the edge creationDate to "2020-10-01". Also, for the Person vertex (Tiger Woods) update the vertex's creationDate and language he speaks.
1
USE GRAPH ldbc_snb
2
3
DROP QUERY UpdateKnowsTS
4
5
CREATE QUERY UpdateKnowsTS () SYNTAX v2 {
6
7
# update the vertex tiger's attributes
8
# creationDate and languages spoken in POST-ACCUM
9
R = SELECT p
10
FROM Person:p
11
WHERE p.firstName == "Tiger" AND p.lastName == "Woods"
12
POST-ACCUM
13
# update simple base type attribute
14
p.creationDate = to_datetime("2020-6-1"),
15
# update collection-type attribute
16
p.speaks = ("english", "golf");
17
18
#DML-sub level, update KNOWS edge attribute "creationDate" in ACCUM
19
R = SELECT t
20
FROM Person:s-(KNOWS:e) -:t
21
WHERE s.firstName == "Tiger" and s.lastName == "Woods"
22
#update the KNOWS edge time stamp
23
ACCUM e.creationDate = to_datetime("2020-10-01");
24
}
25
26
INSTALL QUERY UpdateKnowsTS
27
RUN QUERY UpdateKnowsTS()
Copied!
To verify the update, we can use REST calls.
Check Tiger Woods' creationDate and language he speaks.
Linux Shell
1
curl -X GET "http://localhost:9000/graph/ldbc_snb/vertices/Person/100000000" | jq .
2
#result
3
{
4
"version": {
5
"edition": "enterprise",
6
"api": "v2",
7
"schema": 0
8
},
9
"error": false,
10
"message": "",
11
"results": [
12
{
13
"v_id": "100000000",
14
"v_type": "Person",
15
"attributes": {
16
"id": 100000000,
17
"firstName": "Tiger",
18
"lastName": "Woods",
19
"gender": "m",
20
"birthday": "1970-01-01 00:00:00",
21
"creationDate": "2020-06-01 00:00:00",
22
"locationIP": "",
23
"browserUsed": "",
24
"speaks": [
25
"english",
26
"golf"
27
],
28
"email": []
29
}
30
}
31
]
32
}
Copied!
Check KNOWS edges whose source is tiger woods.
Linux Shell
1
curl -X GET "http://localhost:9000/graph/ldbc_snb/edges/Person/100000000/KNOWS" | jq .
2
3
#result
4
{
5
"version": {
6
"edition": "enterprise",
7
"api": "v2",
8
"schema": 0
9
},
10
"error": false,
11
"message": "",
12
"results": [
13
{
14
"e_type": "KNOWS",
15
"directed": false,
16
"from_id": "100000000",
17
"from_type": "Person",
18
"to_id": "8796093025410",
19
"to_type": "Person",
20
"attributes": {
21
"creationDate": "2020-10-01 00:00:00"
22
}
23
},
24
{
25
"e_type": "KNOWS",
26
"directed": false,
27
"from_id": "100000000",
28
"from_type": "Person",
29
"to_id": "2199023260091",
30
"to_type": "Person",
31
"attributes": {
32
"creationDate": "2020-10-01 00:00:00"
33
}
34
},
35
{
36
"e_type": "KNOWS",
37
"directed": false,
38
"from_id": "100000000",
39
"from_type": "Person",
40
"to_id": "15393162796846",
41
"to_type": "Person",
42
"attributes": {
43
"creationDate": "2020-10-01 00:00:00"
44
}
45
}
46
]
47
}
Copied!

Delete vertices and edges

You can use delete () function to delete edges and vertices in ACCUM and POST-ACCUM clauses.
  • Top-levels DELETE statements are not yet supported in SYNTAX v2.
  • Edges can only be deleted in the ACCUM clause.
  • For best performance, vertices should be deleted in the POST-ACCUM clause.
  • To perform within-SELECT deletes, the FROM pattern can only be a single hop, fixed length pattern.
Example 3. Delete vertex Tiger Woods and its KNOWS edges.
1
USE GRAPH ldbc_snb
2
3
DROP QUERY DeleteEdgeAndVertex
4
5
CREATE QUERY DeleteEdgeAndVertex () SYNTAX v2{
6
7
R = SELECT t
8
FROM Person:s -(KNOWS:e)- Person:t
9
WHERE s.firstName == "Tiger" AND s.lastName == "Woods"
10
ACCUM
11
//delete edges
12
DELETE(e)
13
POST-ACCUM DELETE(s); //delete src vertex
14
15
16
PRINT R [R.id, R.firstName, R.lastName];
17
}
18
19
INSTALL QUERY DeleteEdgeAndVertex
20
RUN QUERY DeleteEdgeAndVertex()
Copied!
To verify the result, you can use built-in REST calls.
1
curl -X GET "http://localhost:9000/graph/ldbc_snb/vertices/Person/100000000" | jq .
2
#vertexresults
3
{
4
"version": {
5
"edition": "enterprise",
6
"api": "v2",
7
"schema": 0
8
},
9
"error": true,
10
"message": "The input vertex id '100000000' is not a valid vertex id for vertex type = Person.",
11
"code": "601"
12
}
13
14
curl -X GET "http://localhost:9000/graph/ldbc_snb/edges/Person/100000000/KNOWS" | jq .
15
#edge results
16
{
17
"version": {
18
"edition": "enterprise",
19
"api": "v2",
20
"schema": 0
21
},
22
"error": true,
23
"message": "The input source_vertex_id '100000000' is not a valid vertex id for vertex type = Person.",
24
"code": "601"
25
}
Copied!
Last modified 1mo ago