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
edgeType
The edge type to traverse
STRING
attrName
The attribute whose value to retrieve
STRING

filter()

Syntax

1
v.neighbors().filter( condition )
2
v.neighborAttribute().filter( condition )
3
v.edgeAttribute().filter( condition )
Copied!

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
condition
An expression that evaluates to a boolean value
BOOL

Example

Example query
1
CREATE QUERY filterEx (SET<STRING> pIds, INT yr) FOR GRAPH workNet api("v2") {
2
3
SetAccum<vertex<company>> @recentEmplr, @allEmplr;
4
BagAccum<string> @diffCountry, @allCountry;
5
6
Start = {person.*};
7
8
L0 = SELECT v
9
FROM Start:v
10
WHERE v.id IN pIds
11
ACCUM
12
# filter using edge attribute
13
[email protected] += v.neighbors("worksFor").filter(worksFor.startYear >= yr),
14
[email protected] += v.neighbors("worksFor").filter(true),
15
16
# vertex alias attribute and neighbor type attribute
17
[email protected] += v.neighborAttribute("worksFor", "company", "id")
18
.filter(v.locationId != company.country),
19
[email protected] += v.neighborAttribute("worksFor", "company", "id")
20
;
21
23
}
Copied!
Results
1
GSQL > RUN QUERY filterEx(["person1","person2"],2016)
2
{
3
"error": false,
4
"message": "",
5
"version": {
6
"edition": "developer",
7
"schema": 0,
8
"api": "v2"
9
},
10
"results": [{
11
"L0": [
12
{
13
"v_id": "person1",
14
"attributes": {
15
"[email protected]": ["company2"],
16
"[email protected]": ["company1"],
17
"[email protected]": [ "company1", "company2" ],
18
"[email protected]": [ "company2", "company1" ]
19
},
20
"v_type": "person"
21
},
22
{
23
"v_id": "person2",
24
"attributes": {
25
"[email protected]": ["company1"],
27
"[email protected]": [ "company1", "company2" ],
28
"[email protected]": [ "company2", "company1" ]
29
},
30
"v_type": "person"
31
}
32
],
33
"yr": 2016
34
}]
35
}
Copied!

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
attrName
A vertex attribute
STRING
attrType
The type of the attribute value
STRING

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
edgeType
The edge type to traverse
STRING
targetVertexType
The target vertex type to visit
STRING
attrName
An attribute of the target vertex type
STRING

Example

For the following graph:
1
# If v is Jenny
2
v.neighborAttribute("friendship", "person", "state") -> ["ca", "ny", "ca"]
Copied!

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
edgeType
Optional. An edge type or a collections of edge types.
STRING, SET<STRING>, SetAccum<STRING>, BagAccum<STRING>, ListAccum<STRING>

Example

For the following graph:
1
# If v is Jenny
2
v.neighbors() -> ["Dan", "Amily", "Tom"]
Copied!

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
edgeType
Optional. An edge type or a collection of edge types.
STRING, SET<STRING>, SetAccum<STRING>, BagAccum<STRING>, ListAccum<STRING>
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.

setAttr()

Syntax

v.setAttr( attrName, newValue )

Description

Sets the specified attribute of a vertex to a new value.

Return type

No return value.

Parameters

Parameter
Description
Data type
attrName
The name of an attribute
STRING
newValue
The new value for the attribute
The type of the attribute.

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
tagN
A tag to add to the vertex
STRING

Example:

1
CREATE QUERY addTagsToPerson() {
2
Seed = { any };
3
# person1 ~ person5 will be tagged as public.
4
vSet = SELECT s
5
FROM Seed:s
6
WHERE s.id IN ("person1","person2","person3","person4","person5")
7
ACCUM s.addTags("public");
8
9
# person6 and person7 will be tagged as public and vip.
10
vSet = SELECT s
11
FROM Seed:s
12
WHERE s.id IN ("person6","person7")
13
ACCUM s.addTags("vip", "public");
14
15
# person8 will be tagged as vip
16
vSet = SELECT s
17
FROM Seed:s
18
WHERE s.id == "person8"
19
ACCUM s.addTags("vip");
20
}
Copied!

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
v2
A vertex
VERTEX

Example:

1
// return the difference set of tags between two vertices
2
CREATE QUERY exampleDifferencetags() {
3
SetAccum<string> @vAcc;
4
vSet = { any };
5
vSet = SELECT s
6
FROM vSet:s -(_)-> :t
7
WHERE t.type == "person"
8
ACCUM s.@vAcc += s.differenceTags(t);
9
PRINT vSet[vSet.@vAcc];
10
}
Copied!

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:

1
//print the tags of each vertices, in 2 different ways.
2
CREATE QUERY exampleGettags() {
3
SetAccum<string> @vAcc;
4
vSet = { any };
5
vSet = SELECT s
6
FROM vSet:s
7
ACCUM s.@vAcc += s.getTags();
8
PRINT vSet[vSet.@vAcc];
9
PRINT vSet[vSet.gettags()];
10
}
11
Copied!
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
tagN
A string.
STRING

Example:

1
USE GRAPH socialNet
2
3
CREATE QUERY findVertexWithTag(STRING tag) {
4
seed = { ANY };
5
res =
6
SELECT v
7
FROM seed:v
8
WHERE v.hasTags(tag)
9
ORDER BY v.id;
10
PRINT res WITH TAGS;
11
}
12
13
INSTALL QUERY findVertexWithTag
14
15
RUN QUERY findVertexWithTag("vip")
Copied!
The output of the query would be:
1
{
2
"error": false,
3
"message": "",
4
"version": {
5
"schema": 2,
6
"edition": "enterprise",
7
"api": "v2"
8
},
9
"results": [{"res": [
10
{
11
"v_id": "person6",
12
"attributes": {
13
"gender": "Male",
14
"id": "person6",
15
"res.gettags()": [
16
"vip",
17
"public"
18
]
19
},
20
"v_type": "person"
21
},
22
{
23
"v_id": "person7",
24
"attributes": {
25
"gender": "Male",
26
"id": "person7",
27
"res.gettags()": [
28
"vip",
29
"public"
30
]
31
},
32
"v_type": "person"
33
},
34
{
35
"v_id": "person8",
36
"attributes": {
37
"gender": "Male",
38
"id": "person8",
39
"res.gettags()": ["vip"]
40
},
41
"v_type": "person"
42
}
43
]}]
44
}
Copied!

isTaggable()

Syntax

v.isTaggable()

Description

Returns true if the vertex is taggable.

Return type

BOOL

Parameters

None

Example:

1
//count the number of taggable vertices in the graph.
2
CREATE QUERY countIstaggable() for graph poc_graph_tag {
3
SumAccum<int> @@count;
4
vSet = { any };
5
vSet = SELECT s
6
FROM vSet:s
7
WHERE s.isTaggable()
8
ACCUM @@count += 1;
9
PRINT @@count;
10
}
Copied!

intersectTags()

Syntax

v.intersectTags( v2 )

Description

Returns the common tags between the vertex and another vertex as a set.

Return type

SET<STRING>

Example:

1
//return the intersect set of tags between two vertices.
2
CREATE QUERY exampleIntersecttags() {
3
SetAccum<string> @vAcc;
4
vSet = { any };
5
vSet = SELECT s
6
FROM vSet:s -(_)-> :t
7
WHERE t.type == "person"
8
ACCUM s.@vAcc += s.intersectTags(t);
9
PRINT vSet[vSet.@vAcc];
10
}
Copied!

removeAllTags()

Syntax

v.removeAllTags()

Description

Removes all tags from the vertex.

Return type

No return value.

Parameters

None

Example:

1
//remove all tags from all person vertices.
2
CREATE QUERY removealltagsFromPerson() {
3
vSet = { person.* };
4
# remove all tags from all person vertices
5
vSet = SELECT s
6
FROM vSet:s
7
ACCUM s.removeAllTags();
8
}
Copied!

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
tagN
A string value
STRING

Example

1
//remove tagvipandpublicfrom all person vertices.
2
CREATE QUERY removetagsFromPerson() {
3
vSet = { person.* };
4
# remove tag vip and public from all person vertices
5
vSet = SELECT s
6
FROM vSet:s
7
ACCUM s.removeTags("vip", "public");
8
}
Copied!

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
v
A vertex alias.
Vertex alias

Example

Query
Result
1
CREATE QUERY getvid_ex () FOR GRAPH socialNet {
2
MinAccum<int> @cc_id = 0; //each vertex's tentative component id
3
4
Start = {person.*};
5
# Initialize: Label each vertex with its own internal ID
6
S = SELECT x FROM Start:x
7
POST-ACCUM
8
x.@cc_id = getvid(x);
9
10
# Community detection steps omitted
11
PRINT Start.@cc_id;
12
}
Copied!
1
{
2
"error": false,
3
"message": "",
4
"version": {
5
"edition": "developer",
6
"schema": 0,
7
"api": "v2"
8
},
9
"results": [{"Start": [
10
{
11
"v_id": "person7",
12
"attributes": {"[email protected]_id": 0},
13
"v_type": "person"
14
},
15
{
16
"v_id": "person5",
17
"attributes": {"[email protected]_id": 4194304},
18
"v_type": "person"
19
},
20
{
21
"v_id": "person1",
22
"attributes": {"[email protected]_id": 4194305},
23
"v_type": "person"
24
},
25
{
26
"v_id": "person4",
27
"attributes": {"[email protected]_id": 11534336},
28
"v_type": "person"
29
},
30
{
31
"v_id": "person2",
32
"attributes": {"[email protected]_id": 13631488},
33
"v_type": "person"
34
},
35
{
36
"v_id": "person3",
37
"attributes": {"[email protected]_id": 20971520},
38
"v_type": "person"
39
},
40
{
41
"v_id": "person8",
42
"attributes": {"[email protected]_id": 22020096},
43
"v_type": "person"
44
},
45
{
46
"v_id": "person6",
47
"attributes": {"[email protected]_id": 24117248},
48
"v_type": "person"
49
}
50
]}]
51
}
Copied!

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
filePath
The absolute file path of the input file to be read. A relative path is not supported.
STRING
vertexIdColumn
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 $0.
$ num.
If header is set to true,$ "column_name" is also acceptable.
vertexTypeColumn
The vertex type column position or a specific vertex type.
$ num.
If header is set to true,$ "column_name" is also acceptable.
Alternatively, a vertex type without double quotes.
separator
The column separator character.
STRING
header
Whether this file has a header.
`BOO

Example

selectVertex.csv
1
ID,type
2
Dan,person
3
Jenny,person
4
Amily,person
Copied!
Example query
1
CREATE QUERY selectVertexEx(STRING filename) FOR GRAPH socialNet {
2
S1 = {SelectVertex(filename, $"c1", $1, ",", true)};
3
S2 = {SelectVertex(filename, $0, person, ",", true)};
4
PRINT S1, S2; # Both sets of inputs product the same result
5
}
Copied!
Results
1
RUN QUERY selectVertex("/home/tigergraph/mydata/selectVertex.csv")
2
{
3
"S1": [
4
{
5
"attributes": {
6
"age": 0,
7
"gender": "female",
8
"name": "Amily",
9
"state": "ca"
10
},
11
"v_id": "Amily",
12
"v_type": "person"
13
},
14
{
15
"attributes": {
16
"age": 1,
17
"gender": "male",
18
"name": "Dan",
19
"state": "ny"
20
},
21
"v_id": "Dan",
22
"v_type": "person"
23
},
24
{
25
"attributes": {
26
"age": 1,
27
"gender": "female",
28
"name": "Jenny",
29
"state": "tx"
30
},
31
"v_id": "Jenny",
32
"v_type": "person"
33
}
34
],
35
"S2": [
36
{
37
"attributes": {
38
"age": 0,
39
"gender": "female",
40
"name": "Amily",
41
"state": "ca"
42
},
43
"v_id": "Amily",
44
"v_type": "person"
45
},
46
{
47
"attributes": {
48
"age": 1,
49
"gender": "male",
50
"name": "Dan",
51
"state": "ny"
52
},
53
"v_id": "Dan",
54
"v_type": "person"
55
},
56
{
57
"attributes": {
58
"age": 1,
59
"gender": "female",
60
"name": "Jenny",
61
"state": "tx"
62
},
63
"v_id": "Jenny",
64
"v_type": "person"
65
}
66
]
67
}
68
]
Copied!

to_vertex()

Running to_vertex() and to_vertex_set() requires real-time conversion of an external ID to a GSQL internal ID, which is a relatively slow process. Therefore,
  • If the user can always know the id before running the query, define the query with VERTEX or SET<VERTEX> parameters instead of STRING or SET<STRING> parameters, and avoid calling to_vertex() or to_vertex_set().
  • Calling to_vertex_set() one time is much faster than calling to_vertex() multiple times. Use to_vertex_set() instead of to_vertex() as much as possible.

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
id
The ID of a vertex
STRING
vertex_type
The type of the vertex
STRING

Example

Example query using to_vertex()
1
CREATE QUERY to_vertex_test (STRING uid, STRING vtype) FOR GRAPH social {
2
VERTEX v;
3
4
v = to_vertex (uid, vtype); # to_vertex assigned to a vertex variable
5
PRINT v; # vertex variable -> only vertex id is printed
6
}
Copied!
Query result
1
GSQL > RUN QUERY to_vertex_test("Dan", "person")
2
{
3
"error": false,
4
"message": "",
5
"version": {
6
"schema": 1,
7
"edition": "enterprise",
8
"api": "v2"
9
},
10
"results": [{"v": "Dan"}]
11
}
Copied!

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
id_set
A set of vertex IDs
SET<STRING>, BAG<STRING>
vertex_type
The type of the vertices
STRING

Example

1
CREATE QUERY to_vertex_set_test (SET<STRING> uids, STRING vtype) FOR GRAPH social {
2
3
S2 = to_vertex_set (uids, vtype); # to_vertex_set assigned to a vertex set variable
4
PRINT S2; # vertex set variable-> full details printed
5
}
Copied!
1
GSQL > run query to_vertex_set_test(["Dan", "Amily", "Jeff"], "person")
2
{
3
"error": false,
4
"message": "Runtime Warning: 1 ids are invalid person vertex ids.",
5
"version": {
6
"schema": 1,
7
"edition": "enterprise",
8
"api": "v2"
9
},
10
"results": [{"S2": [
11
{
12
"v_id": "Amily",
13
"attributes": {
14
"gender": "female",
15
"name": "Amily",
16
"state": "ca",
17
"age": 0
18
},
19
"v_type": "person"
20
},
21
{
22
"v_id": "Dan",
23
"attributes": {
24
"gender": "male",
25
"name": "Dan",
26
"state": "ny",
27
"age": 1
28
},
29
"v_type": "person"
30
}
31
]}]
32
}
Copied!
Last modified 18d ago