Vertex Functions
This page lists the vertex functions that are available in the GSQL query language. The functions are divided into three categories.
-
Vertex alias methods
-
Methods available to vertex aliases
-
-
Vertex-level access control(VLAC) vertex alias methods
-
Methods available to vertex aliases that are related to vertex tags.
-
-
Vertex functions
-
Functions that return a vertex or vertex set, or functions that are closely related to certain attributes of
VERTEX
type variables.
-
Vertex alias methods
This section lists the built-in methods of vertex aliases. Methods can be accessed by the dot (.
) operator.
edgeAttribute()
Syntax
v.edgeAttribute( edgeType, attrName )
Description
From a vertex, traverse edges of a specified type and return the bag of values for a specified edge attribute.
Return type
BagAccum<attrType>
Parameters
Parameter | Description | Data type |
---|---|---|
|
The edge type to traverse |
|
|
The attribute whose value to retrieve |
|
filter()
Syntax
v.neighbors().filter( condition )
v.neighborAttribute().filter( condition )
v.edgeAttribute().filter( condition )
Description
This function is appended to neighbors()
, neighborAttribute()
, or edgeAttribute()
to filter the output set according to a filter condition. Only elements that satisfy the condition will be returned.
Return type
BagAccum
Parameters
Parameter | Description | Data type |
---|---|---|
|
An expression that evaluates to a boolean value |
|
Example
CREATE QUERY filterEx (SET<STRING> pIds, INT yr) FOR GRAPH workNet api("v2") {
SetAccum<vertex<company>> @recentEmplr, @allEmplr;
BagAccum<string> @diffCountry, @allCountry;
Start = {person.*};
L0 = SELECT v
FROM Start:v
WHERE v.id IN pIds
ACCUM
# filter using edge attribute
v.@recentEmplr += v.neighbors("worksFor").filter(worksFor.startYear >= yr),
v.@allEmplr += v.neighbors("worksFor").filter(true),
# vertex alias attribute and neighbor type attribute
v.@diffCountry += v.neighborAttribute("worksFor", "company", "id")
.filter(v.locationId != company.country),
v.@allCountry += v.neighborAttribute("worksFor", "company", "id")
;
PRINT yr, L0[L0.@recentEmplr, L0.@allEmplr, L0.@diffCountry, L0.@allCountry]; // api v2
}
GSQL > RUN QUERY filterEx(["person1","person2"],2016)
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [{
"L0": [
{
"v_id": "person1",
"attributes": {
"L0.@diffCountry": ["company2"],
"L0.@recentEmplr": ["company1"],
"L0.@allCountry": [ "company1", "company2" ],
"L0.@allEmplr": [ "company2", "company1" ]
},
"v_type": "person"
},
{
"v_id": "person2",
"attributes": {
"L0.@diffCountry": ["company1"],
"L0.@recentEmplr": [],
"L0.@allCountry": [ "company1", "company2" ],
"L0.@allEmplr": [ "company2", "company1" ]
},
"v_type": "person"
}
],
"yr": 2016
}]
}
getAttr()
Syntax
v.getAttr(attrName, attrType)
Description
Returns the value of a vertex attribute on the vertex.
Return type
attrType
Parameters
Parameter | Description | Data type |
---|---|---|
|
A vertex attribute |
|
|
The type of the attribute value |
|
neighborAttribute()
Syntax
v.neighborAttribute( edgeType, targetVertexType, attrName )
Description
From a vertex, traverses edges of a specified type to its neighbors of a specified type, and returns the set of values for a specified attribute.
Return type
BagAccum<attrType>
Parameters
Parameter | Description | Data type |
---|---|---|
|
The edge type to traverse |
|
|
The target vertex type to visit |
|
|
An attribute of the target vertex type |
|
Example
For the following graph:
# If v is Jenny
v.neighborAttribute("friendship", "person", "state") -> ["ca", "ny", "ca"]
neighbors()
Syntax
v.neighbors([edgeType])
Description
Returns the out-neighbors or undirected neighbors of the vertex. If edge types are provided, it will only return the neighbors connected by the specified edge types.
Return type
BagAccum<VERTEX>
Parameters
Parameter | Description | Data type |
---|---|---|
|
Optional. An edge type or a collections of edge types. |
|
Example
For the following graph:
# If v is Jenny
v.neighbors() -> ["Dan", "Amily", "Tom"]
outdegree()
Syntax
v.outdegree([edgeType])
Description
Returns the number of outgoing or undirected edges connected to the vertex. If edge types are provided, it will only return the number of edges of the specified types.
Return type
INT
Parameters
Parameter | Description | Data type |
---|---|---|
|
Optional. An edge type or a collection of edge types. |
|
Note on outdegree(): This function reads a metadata value stored with each vertex, to avoid traversing the graph and thus have a fast response. The snapshot transaction semantics means that outdegree() may sometimes read an old value if there are concurrent write transactions. To guarantee an accurate count, traverse the neighboring edges and count them with a SumAccum, or use a function like neighbors() and then use size() on the set.
VLAC vertex alias methods
This section covers the vertex alias methods used to access and modify tags on vertices,
addTags()
Syntax
v.addTags(STRING tag1,... STRING tagN)
Description
Adds the tags provided in the argument list to the vertex.
Return type
No return value.
Parameters
Parameter | Description | Data type |
---|---|---|
|
A tag to add to the vertex |
|
Example:
CREATE QUERY addTagsToPerson() {
Seed = { any };
# person1 ~ person5 will be tagged as public.
vSet = SELECT s
FROM Seed:s
WHERE s.id IN ("person1","person2","person3","person4","person5")
ACCUM s.addTags("public");
# person6 and person7 will be tagged as public and vip.
vSet = SELECT s
FROM Seed:s
WHERE s.id IN ("person6","person7")
ACCUM s.addTags("vip", "public");
# person8 will be tagged as vip
vSet = SELECT s
FROM Seed:s
WHERE s.id == "person8"
ACCUM s.addTags("vip");
}
differenceTags()
Syntax
v.differenceTags( v2 )
Description
Returns the difference in tags between the vertex and another vertex as a set.
Return type
SET<STRING>
Paramters
Parameter | Description | Data type |
---|---|---|
|
A vertex |
|
Example:
// return the difference set of tags between two vertices
CREATE QUERY exampleDifferencetags() {
SetAccum<string> @vAcc;
vSet = { any };
vSet = SELECT s
FROM vSet:s -(_)- :t
WHERE t.type == "person"
ACCUM s.@vAcc += s.differenceTags(t);
PRINT vSet[vSet.@vAcc];
}
getTags()
Syntax
v.getTags()
Descriptions
Returns the vertex’s tags as a set. If the vertex has no tags or is untaggable, it returns an empty set.
Return type
SET<STRING>
Parameters
None.
Example:
//print the tags of each vertices, in 2 different ways.
CREATE QUERY exampleGettags() {
SetAccum<string> @vAcc;
vSet = { any };
vSet = SELECT s
FROM vSet:s
ACCUM s.@vAcc += s.getTags();
PRINT vSet[vSet.@vAcc];
PRINT vSet[vSet.gettags()];
}
Tip: getTags()
can be used within a PRINT
statement:
-
PRINT R [R.getTags()];
-
or
PRINT R WITH TAGS
which is syntax sugar, except that it won’t print"R.getTags()": []
for non-taggable vertices.
hasTags()
Syntax
hasTags( tag1, tag2, ..., tagN )
Description
Returns true
if the vertex has every tag provided in the argument list and returns false
if it does not.
Return type
BOOL
Parameters
Parameter | Description | Data type |
---|---|---|
|
A string. |
|
Example:
USE GRAPH socialNet
CREATE QUERY findVertexWithTag(STRING tag) {
seed = { ANY };
res =
SELECT v
FROM seed:v
WHERE v.hasTags(tag)
ORDER BY v.id;
PRINT res WITH TAGS;
}
INSTALL QUERY findVertexWithTag
RUN QUERY findVertexWithTag("vip")
The output of the query would be:
{
"error": false,
"message": "",
"version": {
"schema": 2,
"edition": "enterprise",
"api": "v2"
},
"results": [{"res": [
{
"v_id": "person6",
"attributes": {
"gender": "Male",
"id": "person6",
"res.gettags()": [
"vip",
"public"
]
},
"v_type": "person"
},
{
"v_id": "person7",
"attributes": {
"gender": "Male",
"id": "person7",
"res.gettags()": [
"vip",
"public"
]
},
"v_type": "person"
},
{
"v_id": "person8",
"attributes": {
"gender": "Male",
"id": "person8",
"res.gettags()": ["vip"]
},
"v_type": "person"
}
]}]
}
isTaggable()
Syntax
v.isTaggable()
Description
Returns true if the vertex is taggable.
Return type
BOOL
Parameters
None
Example:
//count the number of taggable vertices in the graph.
CREATE QUERY countIstaggable() for graph poc_graph_tag {
SumAccum<int> @@count;
vSet = { any };
vSet = SELECT s
FROM vSet:s
WHERE s.isTaggable()
ACCUM @@count += 1;
PRINT @@count;
}
intersectTags()
Syntax
v.intersectTags( v2 )
Description
Returns the common tags between the vertex and another vertex as a set.
Return type
SET<STRING>
Example:
//return the intersect set of tags between two vertices.
CREATE QUERY exampleIntersecttags() {
SetAccum<string> @vAcc;
vSet = { any };
vSet = SELECT s
FROM vSet:s -(_)- :t
WHERE t.type == "person"
ACCUM s.@vAcc += s.intersectTags(t);
PRINT vSet[vSet.@vAcc];
}
removeAllTags()
Syntax
v.removeAllTags()
Description
Removes all tags from the vertex.
Return type
No return value.
Parameters
None
Example:
//remove all tags from all person vertices.
CREATE QUERY removealltagsFromPerson() {
vSet = { person.* };
# remove all tags from all person vertices
vSet = SELECT s
FROM vSet:s
ACCUM s.removeAllTags();
}
removeTags()
Syntax
removeTags( tag1, tag2, ..., tagN )
Description
Removes the tags provided in the argument list from the vertex.
Return type
No return value.
Parameters
Parameter | Description | Data type |
---|---|---|
|
A string value |
|
Example
//remove tag “vip” and “public” from all person vertices.
CREATE QUERY removetagsFromPerson() {
vSet = { person.* };
# remove tag vip and public from all person vertices
vSet = SELECT s
FROM vSet:s
ACCUM s.removeTags("vip", "public");
}
Vertex functions
The functions in this section either have return values of vertex or vertex set type or are closely related to vertex attributes.
getvid()
Syntax
getvid( v )
Description
Returns the internal ID number of a vertex.
The internal ID is not the primary ID which the user assigned when creating the vertex. However, there is a 1-to-1 mapping between the external ID (primary_id
) and internal ID.
The engine can access the internal ID faster than accessing the external ID, so if a query needs unique values for a large number of vertices, but doesn’t care about particular values, getvid()
can be a useful option. For example, in many community detection algorithms, we start by assigning every vertex a unique community ID. Then, as the algorithm progresses, some vertices will join the community of one of their neighbors, giving up their current community ID and copying the IDs of their neighbors.
Return type
INT
Parameters
Parameter | Description | Data type |
---|---|---|
|
A vertex alias. |
Vertex alias |
Example
- Query
-
CREATE QUERY getvid_ex () FOR GRAPH socialNet { MinAccum<int> @cc_id = 0; //each vertex's tentative component id Start = {person.*}; # Initialize: Label each vertex with its own internal ID S = SELECT x FROM Start:x POST-ACCUM x.@cc_id = getvid(x); # Community detection steps omitted PRINT Start.@cc_id; }
- Result
-
{ "error": false, "message": "", "version": { "edition": "developer", "schema": 0, "api": "v2" }, "results": [{"Start": [ { "v_id": "person7", "attributes": {"Start.@cc_id": 0}, "v_type": "person" }, { "v_id": "person5", "attributes": {"Start.@cc_id": 4194304}, "v_type": "person" }, { "v_id": "person1", "attributes": {"Start.@cc_id": 4194305}, "v_type": "person" }, { "v_id": "person4", "attributes": {"Start.@cc_id": 11534336}, "v_type": "person" }, { "v_id": "person2", "attributes": {"Start.@cc_id": 13631488}, "v_type": "person" }, { "v_id": "person3", "attributes": {"Start.@cc_id": 20971520}, "v_type": "person" }, { "v_id": "person8", "attributes": {"Start.@cc_id": 22020096}, "v_type": "person" }, { "v_id": "person6", "attributes": {"Start.@cc_id": 24117248}, "v_type": "person" } ]}] }
selectVertex()
Syntax
selectVertex( filepath, vertexIdColumn, vertexTypeColumn, seperator, header)
Description
Reads a data file that lists particular vertices of the graph and returns the corresponding vertex set.This function can only be used in a vertex set variable declaration statement as a seed set and the vertices in the data file must already be in the graph.The data file must be organized as a table with one or more columns.One column must be for vertex ID.Optionally, another column is for vertex type.
Return type
SET<VERTEX>
Parameters
Parameter | Description | Data type |
---|---|---|
|
The absolute file path of the input file to be read. A relative path is not supported. |
|
|
The vertex ID column position. The index for column positions starts at 0. Therefore, to designate the
first column as the ID column, set this parameter to |
If |
|
The vertex type column position or a specific vertex type. |
If Alternatively, a vertex type without double quotes. |
|
The column separator character. |
|
|
Whether this file has a header. |
`BOO |
Example
ID,type
Dan,person
Jenny,person
Amily,person
CREATE QUERY selectVertexEx(STRING filename) FOR GRAPH socialNet {
S1 = {SelectVertex(filename, $"c1", $1, ",", true)};
S2 = {SelectVertex(filename, $0, person, ",", true)};
PRINT S1, S2; # Both sets of inputs product the same result
}
RUN QUERY selectVertex("/home/tigergraph/mydata/selectVertex.csv")
{
"S1": [
{
"attributes": {
"age": 0,
"gender": "female",
"name": "Amily",
"state": "ca"
},
"v_id": "Amily",
"v_type": "person"
},
{
"attributes": {
"age": 1,
"gender": "male",
"name": "Dan",
"state": "ny"
},
"v_id": "Dan",
"v_type": "person"
},
{
"attributes": {
"age": 1,
"gender": "female",
"name": "Jenny",
"state": "tx"
},
"v_id": "Jenny",
"v_type": "person"
}
],
"S2": [
{
"attributes": {
"age": 0,
"gender": "female",
"name": "Amily",
"state": "ca"
},
"v_id": "Amily",
"v_type": "person"
},
{
"attributes": {
"age": 1,
"gender": "male",
"name": "Dan",
"state": "ny"
},
"v_id": "Dan",
"v_type": "person"
},
{
"attributes": {
"age": 1,
"gender": "female",
"name": "Jenny",
"state": "tx"
},
"v_id": "Jenny",
"v_type": "person"
}
]
}
]
to_vertex()
Running
|
Syntax
to_vertex( id, vertex_type )
Description
Returns a vertex from a string ID and vertex type. If a vertex with the provided ID and type does not exist, the function will throw a run-time error.
Return type
VERTEX
Parameters
Parameter | Description | Data type |
---|---|---|
|
The ID of a vertex |
|
|
The type of the vertex |
|
Example
CREATE QUERY to_vertex_test (STRING uid, STRING vtype) FOR GRAPH social {
VERTEX v;
v = to_vertex (uid, vtype); # to_vertex assigned to a vertex variable
PRINT v; # vertex variable -> only vertex id is printed
}
GSQL > RUN QUERY to_vertex_test("Dan", "person")
{
"error": false,
"message": "",
"version": {
"schema": 1,
"edition": "enterprise",
"api": "v2"
},
"results": [{"v": "Dan"}]
}
to_vertex_set()
Syntax
to_vertex_set( id_set, vertex_type)
Description
Returns a vertex set from a set or bag of string IDs and a vertex type. If there are invalid IDs in the set, those IDs will be skipped and the response will contain a warning message. If the vertex type does not exist, the function will throw a run-time error.
Return type
SET<VERTEX>
Parameters
Parameter | Description | Data type |
---|---|---|
|
A set of vertex IDs |
|
|
The type of the vertices |
|
Example
CREATE QUERY to_vertex_set_test (SET<STRING> uids, STRING vtype) FOR GRAPH social {
S2 = to_vertex_set (uids, vtype); # to_vertex_set assigned to a vertex set variable
PRINT S2; # vertex set variable-> full details printed
}
GSQL > run query to_vertex_set_test(["Dan", "Amily", "Jeff"], "person")
{
"error": false,
"message": "Runtime Warning: 1 ids are invalid person vertex ids.",
"version": {
"schema": 1,
"edition": "enterprise",
"api": "v2"
},
"results": [{"S2": [
{
"v_id": "Amily",
"attributes": {
"gender": "female",
"name": "Amily",
"state": "ca",
"age": 0
},
"v_type": "person"
},
{
"v_id": "Dan",
"attributes": {
"gender": "male",
"name": "Dan",
"state": "ny",
"age": 1
},
"v_type": "person"
}
]}]
}