The GSQL language provides full support for vertex and edge insertion, deletion, and attribute update is provided. Therefore, the language is more than just a "query" language.
Each query is considered one transaction. Therefore, modifications to the graph data do not take effect until the entire query is completed (committed). Accordingly, any modification statement does not affect any other statements inside the same query.
Query-body DELETE Statement
The query-body DELETE statement deletes a given set of edges or vertices. This statement can only be used as a query-body statement. (Deletion at the DML-sub level is served by the DML-sub DELETE statement, described next.
The vertexSet and edgeSet terms in the FROM clause follow the same rules as those in the FROM clause in a SELECT statement. The WHERE clause can filter the items in the vertexSet or edgeSet.Below are two examples, one for deleting vertices and one for deleting edges.
DELETE statement example
# Delete all "person" vertices withlocation equal to"us"CREATE QUERY deleteEx() FOR GRAPH workNet { S = {person.*};DELETE s FROM S:sWHERE s.locationId =="us";}
DELETE statement example 2
# Delete all "worksFor" edges where the person's location is "us"CREATE QUERY deleteEx2() FOR GRAPH workNet { S = {person.*}; DELETE e FROM S:s -(worksFor:e)-> company:t WHERE s.locationId == "us";}
The following query can be used to observe the effect of the delete statements. This query counts the person vertices who work in the US ("us") and the worksFor edges for persons in the US. When the initial workNet test data loaded, there are 5 persons and 9 worksFor edges for locationId = "us". If query deleteEx2 is run, the worksAtUS query will then find the 5 persons but 0 worksFor edges. Next, if the deleteEx query is run, the worksAtUS query will then find 0 persons and 0 worksFor edges.
Query to check the results of deleteEx and deleteEx2
CREATE QUERY countAtLocation(STRING loc) FOR GRAPH workNet { SetAccum<EDGE> @@selEdge;Start= {person.*}; SV =SELECT s FROMStart:sWHERE s.locationId == loc;PRINT SV.size() AS numVertices; SE =SELECT s FROMStart:s -(worksFor:e)-> company:tWHERE s.locationId == loc ACCUM @@selEdge += e;PRINT @@selEdge.size() AS numEdges;}
For example, the following sequence of countAtLocation, deleteEx2, and deleteEx queries
DML-sub DELETE is a DML-substatement which deletes one vertex or edge each time it is called. (Deletion at the query-body level is served by the Query-body DELETE statement described above.) In practice, this statement resides within the body of a SELECT...ACCUM/POST-ACCUM clause, so it is called once for each member of a selected vertex set or edge set.
The ACCUM clause iterates over an edge set, which can encounter the same vertex multiple times. If you wish to delete a vertex, it is best practice to place the DML-sub DELETE statement in the POST-ACCUM clause rather than in the ACCUM clause.
EBNF
DMLSubDeleteStmt :=DELETE"("name")"
The following example uses and modifies the graph data for socialNet.
DELETE within ACCUM vs. POST-ACCUM
# Remove any post vertices posted by the given userCREATE QUERY deletePosts(vertex<person> seed) FOR GRAPH socialNet {start= {seed};# Best practice istodelete a vertex in a POST-ACCUM, which only# occurs once for each vertex v, guaranteeing that a vertex isnot# deleted more than once postAccumDeletedPosts =SELECT v FROMstart-(posted:e)-> post:v POST-ACCUM DELETE (v);# Possible, but not recommended as the DML-sub DELETEstatement occurs# once for each edge of the vertex v accumDeletedPosts =SELECT v FROMstart-(posted:e)-> post:v ACCUM DELETE (v);}# Need a separate query to display the results, because deletions don't take effect during the query.CREATE QUERY selectUserPosts(vertex<person> seed) FOR GRAPH socialNet { start = {seed}; userPosts = SELECT v FROM start -(posted:e)-> post:v; PRINT userPosts;}
For example, the following sequence of selectUserPosts and deletePosts queries
deletePosts.run
RUN QUERY selectUserPosts("person3")RUN QUERY deletePosts("person3")RUN QUERY selectUserPosts("person3")
will produce the following result:
Results from DeletePosts Example
# Before the deletion{"error": false,"message": "","version": {"schema": 0,"api": "v2" },"results": [{"selectedPosts": [{"v_id": "2","attributes": {"postTime": "2011-02-03 01:02:42","subject": "query languages" },"v_type": "post" }]}]}# Deletion; nooutput results requested at this point{"error": false,"message": "","version": {"schema": 0,"api": "v2" },"results": []}# After the deletion{"error": false,"message": "","version": {"schema": 0,"api": "v2" },"results": [{"selectedPosts": []}]}
INSERT INTO Statement
The INSERT INTO statement adds edges or vertices to the graph. However, if the ID value(s) for the inserted vertex/edge match those of an existing vertex/edge, then the new values will overwrite the old values. To insert an edge, its endpoint vertices must already exist, either prior to running the query or inserted earlier in that query.The INSERT INTO statement can be used as a query-body-level statement or a DML-substatement.
The formal syntax is complex because it encompasses several options, and even so, it requires additional explanation. The first name symbol is the vertex type or edge type. The user then has two options:
1) Provide a value for the ID(s) and then each attribute, in the canonical order for the vertex or edge type. This format is similar to that of a LOAD statement. In this case, it is not necessary to explicitly name the attributes, since it is assumed that every one is being referenced, in order.
2) Name the specific attributes to be set, and then provide a corresponding list of values. The attributes can be in any order, with the exception that the IDs must come first. That is, to insert a vertex, the first attribute name must be PRIMARY_ID. To insert an edge, the first two attribute names must be FROM and TO.
For each attribute value, provide either an expression expr or "_", which means the default value for that attribute type. The optional name which follows the first two (id) values is to specify the source vertex type and target vertex type, if the edge type had been defined with wildcard vertex types.
Query-Body INSERT
The query insertEx illustrates query-body level INSERT statements: insert new company vertices and worksFor examples into the workNet graph.
INSERT statement
CREATE QUERY insertEx(STRING name, STRING name2, STRING name3, STRING comp) FOR GRAPH workNet {# Vertex insertion# Adds 2'company' vertices. One is located in the USA, and a sister company in Japan.INSERT INTO company VALUES ( comp, comp, "us" );INSERT INTO company (PRIMARY_ID, country) VALUES ( comp +"_jp", "jp" );# Edge insertion# Adds a 'worksFor' edge from person 'name'to the company 'comp', filling indefault# valuesfor startYear (0), startMonth (0), and fullTime (false).INSERT INTO worksFor VALUES (name person, comp company, _, _, _);# Adds a 'worksFor' edge from person 'name2'to the company 'comp', filling indefault# valuesfor startMonth (0), but specifying valuesfor startYear and fullTime.INSERT INTO worksFor (FROM, TO, startYear, fullTime) VALUES (name2 person, comp company, 2017, true);# Adds a 'worksFor' edge from person 'name3'to the company 'comp', filling indefault# valuesfor startMonth (0), and fullTime (false) but specifying a valuefor startYear (2017).INSERT INTO worksFor (FROM, TO, startYear) VALUES (name3 person, comp company, 2000+17);}
The query whoWorksForCompany can be used to check the effect of query insertEx. Prior to running insertEx, running whoWorksForCompany("gsql") will find 0 companies called "gsql" and 0 worksFor edges for company "gsql". If we then run the query insertEx("tic", "tac", "toe", "gsql"), then insertEx("gsql") will find a company called "gsql" and another one called "gsql_jp". Moreover, it will find 3 edges, tic, tac, and toe, with different values for the startMonth, startYear, and fullTime parameters.
The following example show a DML-sub level INSERT. Because the statement applies to allCompanies, several vertices will be inserted.
DML-sub INSERT statement
# Add a child company of a given company name. The new child company isin japanCREATE QUERY addNewChildCompany(STRING name) FOR GRAPH workNet { allCompanies = {company.*}; X =SELECT sFROM allCompanies:sWHERE s.id ==name ACCUM INSERT INTO company VALUES ( name+"_jp", name+"_jp", "jp" );}# Add separate query to list the companies, beforeandafter the insertionCREATE QUERY listCompanyNames(STRING countryFilter) FOR GRAPH workNet { allCompanies = {company.*}; C =SELECT sFROM allCompanies:sWHERE s.country == countryFilter;PRINT C.size() AS numCompanies;PRINT C;}
Example: Add a child company in Japan to US-based company company3. List all the Japan-based companies before and after the insertion.
addNewChildCompany.run
RUN QUERY listCompanyNames("jp")RUN QUERY addNewChildCompany("company4")RUN QUERY listCompanyNames("jp")
The UPDATE statement updates the attribute of each vertex or edge in a vertex set or edge set, respectively, with new attribute values.
EBNF
updateStmt :=UPDATEnameFROM ( edgeSet | vertexSet ) SET DMLSubStmtList [whereClause]
The set of vertices or edges to update is described in the FROM clause, following the same rules as the FROM clause in a SELECT block. In the SET clause, the DMLSubStmtList may contain assignment statements to update the attributes of a vertex or edge. Both simple base type attributes and collection type attributes can be updated. These assignment statements use the vertex or edge aliases declared in the FROM clause. The optional WHERE clause supports boolean conditions to filter the items in the vertexSet or edgeSet.
UPDATE statement example
# Change all "person" vertices withlocation equal to"us"to"USA"CREATE QUERY updateEx() FOR GRAPH workNet { S = {person.*};UPDATE s FROM S:sSET s.locationId ="USA", # simple base type attribute s.skillList = [1,2,3] # collection-type attributeWHERE s.locationId =="us";# The update cannot become effective within this query, so PRINT S still show "us". PRINT S;}
The UPDATE statement can only be used as a query-body-level statement. However, DML-sub level updates are still possible by using other statement types. A vertex attribute's value can be updated within the POST-ACCUM clause of a SELECT block by using the assignment operator (=); An edge attribute's value can be updated within the ACCUM clause of a SELECT block by using the assignment operator. In fact, the UPDATE statement is equivalent to a SELECT statement with ACCUM and/or POST-ACCUM to update the vertex or edge attribute values. Below is an example.
Updating a vertex's attribute value in a ACCUM clause is not allowed, because the update can occur multiple times in parallel, and possibly result in an non-deterministic value. If the vertex attribute value update depends on an edge attribute value, use the vertex-attached accumulators to save the value and update the vertex attribute's value in the POST-ACCUM clause.
The query below uses the SELECT statement instead of the UPDATE statement, but is functional similar to the query above. Query updateEx2 reverses the locationId change made by updateEx (changing the location back to "us" from "USA").
UPDATE statement example 2
# The second example is equivalent to the above updateExCREATE QUERY updateEx2() FOR GRAPH workNet { S = {person.*}; X =SELECT sFROM S:sWHERE S.locationId =="USA" POST-ACCUM S.locationId ="us", S.skillList = [3,2,1];PRINT S;}
Below is an example of an edge update with two attribute changes, including an incremental change (e.startYear = e.startYear + 1):
UPDATE statement example 3
CREATE QUERY updateEx3() FOR GRAPH workNet{ S = {person.*};# update edge andtarget vertices' attribute UPDATE e FROM S:s - (worksFor:e) -> :t SET e.startYear = e.startYear + 1, e.fullTime = false WHERE s.locationId == "us"; PRINT S;}
Other Update Methods
In addition to the above UPDATE statement and SELECT statement, a simple assignment statement at the query-body level can be used to update the attribute value of a single vertex/edge, if the vertex/edge has been assigned to a variable or parameter.
update by assignment
# change the given person's new locationCREATE QUERY updateByAssignment(VERTEX<person> v, STRING newLocation) FOR GRAPH workNet{ v.locationId = newLocation;}