Operators and Expressions
An expression is a combination of fixed values, variables, operators, function calls, and groupings that specify a computation, resulting in a data value. This section of the specification describes the literals (fixed values), operators, and functions available in the GSQL query language. It covers the subset of the EBNF definitions shown below. However, more so than in other sections of the specification, syntax alone is not an adequate description. The semantics (functionality) of the particular operators and functions are an essential complement to the syntax.
EBNF for Operations, Functions, and Expressions
1
constant := numeric | stringLiteral | TRUE | FALSE | GSQL_UINT_MAX
2
| GSQL_INT_MAX | GSQL_INT_MIN | TO_DATETIME "(" stringLiteral ")"
3
4
mathOperator := "*" | "/" | "%" | "+" | "-" | "<<" | ">>" | "&" | "|"
5
6
condition := expr
7
| expr comparisonOperator expr
8
| expr [ NOT ] IN setBagExpr
9
| expr IS [ NOT ] NULL
10
| expr BETWEEN expr AND expr
11
| "(" condition ")"
12
| NOT condition
13
| condition (AND | OR) condition
14
| (TRUE | FALSE)
15
| expr [NOT] LIKE expr [ESCAPE escape_char]
16
17
comparisonOperator := "<" | "<=" | ">" | ">=" | "==" | "!="
18
19
aggregator := COUNT | MAX | MIN | AVG | SUM
20
21
expr := name
22
| globalAccumName
23
| name ".type"
24
| name "." name
25
| name "." localAccumName ["\'"]
26
| name "." name "." name "(" [argList] ")"
27
| name "." name "(" [argList] ")" [ "." FILTER "(" condition ")" ]
28
| name ["<" type ["," type]* ">"] "(" argList] ")"
29
| name "." localAccumName ("." name "(" [argList] ")")+ ["." name]
30
| globalAccumName ("."name "(" [argList] ")")+ ["."name]
31
| COALESCE "(" [argList] ")"
32
| aggregator "(" [DISTINCT] setBagExpr ")"
33
| ISEMPTY "(" setBagExpr ")"
34
| expr mathOperator expr
35
| "-" expr
36
| "(" expr ")"
37
| "(" argList "->" argList ")" // key value pair for MapAccum
38
| "[" argList "]" // a list
39
| constant
40
| setBagExpr
41
| name "(" argList ")" // function call or a tuple object
42
43
setBagExpr := name
44
| globalAccumName
45
| name "." name
46
| name "." localAccumName
47
| name "." localAccumName ("." name "(" [argList] ")")+
48
| name "." name "(" [argList] ")" [ "." FILTER "(" condition ")" ]
49
| globalAccumName ("." name "(" [argList] ")")+
50
| setBagExpr (UNION | INTERSECT | MINUS) setBagExpr
51
| "(" argList ")"
52
| "(" setBagExpr ")"
53
54
55
argList := expr ["," expr]*
Copied!

Constants

1
constant := numeric | stringLiteral | TRUE | FALSE | GSQL_UINT_MAX
2
| GSQL_INT_MAX | GSQL_INT_MIN | TO_DATETIME "(" stringLiteral ")"
Copied!
Each primitive data type supports constant values:
Data Type
Constant
Examples
Numeric types (INT, UINT, FLOAT, DOUBLE)
numeric
123 -5 45.67 2.0e-0.5
UINT
GSQL_UINT_MAX
INT
GSQL_INT_MAX GSQL_INT_MIN
boolean
TRUE FALSE
string
stringLiteral
GSL_UINT_MAX = 2 ^ 64 - 1 = 18446744073709551615
GSQL_INT_MAX = 2 ^ 63 - 1 = 9223372036854775807
GSQL_INT_MIN = -2 ^ 63 = -9223372036854775808

Operators

An operator is a keyword token that performs a specific computational function to return a resulting value, using the adjacent expressions (its operands) as input values. An operator is similar to a function in that both compute a result from inputs, but syntactically they are different. The most familiar operators are the mathematical operators for addition + and subtraction - .
Tip: The operators listed in this section are designed to behave like the operators in MySQL.

Mathematical Operators and Expressions

We support the following standard mathematical operators and meanings. The latter four ("<<" | ">>" | "&" | "|") are for bitwise operations. See the section below: "Bit Operators".
1
mathOperator := "*" | "/" | "%" | "+" | "-" | "<<" | ">>" | "&" | "|"
Copied!
Operator precedences are shown in the following list, from the highest precedence to the lowest. Operators that are shown together on a line have the same precedence:
Operator Precedence, highest to lowest
1
*, /, %
2
-, +
3
<<, >>
4
&
5
|
6
==, >=, >, <=, <, !=
Copied!
Example 1. Math Operators + - * /
1
CREATE QUERY mathOperators() FOR GRAPH minimalNet api("v2")
2
{
3
int x,y;
4
int z1,z2,z3,z4,z5;
5
float f1,f2,f3,f4;
6
7
x = 7;
8
y = 3;
9
10
z1 = x * y; # z = 21
11
z2 = x - y; # z = 4
12
z3 = x + y; # z = 10
13
z4 = x / y; # z = 2
14
z5 = x / 4.0; # z = 1
15
f1 = x / y; # v = 2
16
f2 = x / 4.0; # v = 1.75
17
f3 = x % 3; # v = 1
18
f4 = x % y; # z = 1
19
20
PRINT x,y;
21
PRINT z1 AS xTIMESy, z2 AS xMINUSy, z3 AS xPLUSy, z4 AS xDIVy, z5 AS xDIV4f;
22
PRINT f1 AS xDIVy, f2 AS xDIV4f, f3 AS xMOD3, f4 AS xMODy;
23
}
Copied!
mathOperators.json Results
1
GSQL > RUN QUERY mathOperators()
2
{
3
"error": false,
4
"message": "",
5
"version": {
6
"edition": "developer",
7
"schema": 0,
8
"api": "v2"
9
},
10
"results": [
11
{
12
"x": 7,
13
"y": 3
14
},
15
{
16
"xTIMESy": 21,
17
"xPLUSy": 10,
18
"xMINUSy": 4,
19
"xDIVy": 2,
20
"xDIV4f": 1
21
},
22
{
23
"xMODy": 1,
24
"xMOD3": 1,
25
"xDIVy": 2,
26
"xDIV4f": 1.75
27
}
28
]
29
}
Copied!

Boolean Operators

We support the standard Boolean operators and standard order of precedence: AND, OR, NOT

Bit Operators

Bit operators (<<, >>, &, and |) operate on integers and return an integer.
Bit Operators
1
CREATE QUERY bitOperationTest() FOR GRAPH minimalNet{
2
PRINT 80 >> 2; # 20
3
PRINT 80 << 2; # 320
4
PRINT 2 + 80 >> 4; # 5
5
PRINT 2 | 3 ; # 3
6
PRINT 2 & 3 ; # 2
7
PRINT 2 | 3 + 2; # 7
8
PRINT 2 & 3 - 2; # 0
9
}
Copied!

String Operators

Operator + can be used for concatenating strings.

Tuple Fields

The fields of the tuple can be accessed using the dot operator.

Comparison Operators and Conditions

A condition is an expression that evaluates to a boolean value of either true or false. One type of condition uses the familiar comparison operators. A comparison operator compares two numeric or string values.
1
comparisonOperator := "<" | "<=" | ">" | ">=" | "==" | "!="
2
3
condition := expr
4
| expr comparisonOperator expr
5
| expr [ NOT ] IN setBagExpr
6
| expr IS [ NOT ] NULL
7
| expr BETWEEN expr AND expr
8
| "(" condition ")"
9
| NOT condition
10
| condition (AND | OR) condition
11
| (TRUE | FALSE)
12
| expr [NOT] LIKE expr [ESCAPE escape_char]
Copied!
Strings are compared based on standard lexicographical ordering: (space) < (digit) < (uppercase_letter) < (lowercase_letter).
The comparison operators treat the STRING COMPRESS type as though it is STRING type.

BETWEEN expr AND expr

The expression expr1 BETWEEN expr2 AND expr3 is true if the value expr1 is in the range from expr2 to expr3, including the endpoint values. Each expression must be numeric.
" expr1 BETWEEN expr2 AND expr3 " is equivalent to " expr1 <= expr3 AND expr1 >= expr2".
BETWEEN AND example
1
CREATE QUERY mathOperatorBetween() FOR GRAPH minimalNet
2
{
3
int x;
4
bool b;
5
x = 1;
6
b = (x BETWEEN 0 AND 100); PRINT b; # True
7
b = (x BETWEEN 1 AND 2); PRINT b; # True
8
b = (x BETWEEN 0 AND 1); PRINT b; # True
9
}
Copied!

IS NULL, IS NOT NULL

IS NULL and IS NOT NULL can be used for checking whether an optional parameter is given any value.
IS NULL example
1
CREATE QUERY parameterIsNULL (INT p) FOR GRAPH minimalNet {
2
IF p IS NULL THEN
3
PRINT "p is null";
4
ELSE
5
PRINT "p is not null";
6
END;
7
}
Copied!
parameterIsNULL.json Results
1
GSQL > RUN QUERY parameterIsNULL(_)
2
{
3
"error": false,
4
"message": "",
5
"version": {
6
"edition": "developer",
7
"schema": 0,
8
"api": "v2"
9
},
10
"results": [{"p is null": "p is null"}]
11
}
12
GSQL > RUN QUERY parameterIsNULL(3)
13
{
14
"error": false,
15
"message": "",
16
"version": {
17
"edition": "developer",
18
"schema": 0,
19
"api": "v2"
20
},
21
"results": [{"p is not null": "p is not null"}]
22
}
Copied!
Every attribute value stored in GSQL is a valid value, so IS NULL and IS NOT NULL is only effective for query parameters.

LIKE

1
expr [NOT] LIKE expr [ESCAPE escape_char]
Copied!
The LIKE operator is used for string pattern matching and can only be used in WHERE clauses. The expression string1 LIKE string_patternevaluates to boolean true if string1 matches the pattern in string_pattern; otherwise, it is false.
Both operands must be strings. Additionally, while string1 can be a function call (e.g. lower(string_variable), string_pattern must be a string literal or a parameter. A string_pattern can contain characters as well as the following wildcard and other special symbols, in order to express a pattern (<char_list>indicates a placeholder):
Character or syntax
Description
Example
%
Matches zero or more characters.
%abc% matches any string which contains the sequence "abc".
_(underscore)
Matches any single character.
_abc_ematches any 6-character string where the 2nd to 4th characters are "abc" and the last character is "e".
[<char_list>]
Matches any character in a char list. A char list is a concatenated character set, with no separators.
[Tiger]matches either T, i, g, e, or r.
[^<char_list>]
Matches any character NOT in a char list.
[^qxz]matches any character other than q, x, or z.
[!<char_list>]
Matches any character NOT in a char list.
α-β
(Special syntax within a char list) matches a character in the range from α to β. A char list can have multiple ranges.
[a-mA-M0-3]matches a letter from a to m, upper or lower case, or a digit from 0 to 3.
\\
(Special syntax within a char list) matches the character \
\\]
(Special syntax within a char list) matches the character ] No special treatment is needed for [ inside a char list.
%[\\]!]matches any string which ends with either ] or !

ESCAPE escape_char

The optional ESCAPE escape_char clause is used to define an escape character. When escape_char occurs in string_pattern, then the next character is interpreted literally, instead of as a pattern matching operator. For example, if we want to specify the pattern "any string ending with the '%' character", we could use "%\%" ESCAPE "\"
The first "%" has its usual pattern-matching meaning "zero or more characters". "\%" means a literal percentage character, because it starts with the escape character "\".

Example

Example query using LIKE operator
1
CREATE QUERY printAPosts(/* Parameters here */) FOR GRAPH socialNet {
2
/* Write query logic here */
3
posts = {post.*};
4
aPosts = SELECT v FROM posts:v
5
WHERE v.subject LIKE "%a%"; // Return all posts that has the
6
// character "a" in its subject
7
PRINT aPosts;
8
}
Copied!
1
Results
2
{
3
"error": false,
4
"message": "",
5
"version": {
6
"schema": 0,
7
"edition": "enterprise",
8
"api": "v2"
9
},
10
"results": [{"aPosts": [
11
{
12
"v_id": "2",
13
"attributes": {
14
"postTime": "2011-02-03 01:02:42",
15
"subject": "query languages"
16
},
17
"v_type": "post"
18
},
19
{
20
"v_id": "8",
21
"attributes": {
22
"postTime": "2011-02-03 17:05:52",
23
"subject": "cats"
24
},
25
"v_type": "post"
26
},
27
{
28
"v_id": "0",
29
"attributes": {
30
"postTime": "2010-01-12 11:22:05",
31
"subject": "Graphs"
32
},
33
"v_type": "post"
34
},
35
{
36
"v_id": "1",
37
"attributes": {
38
"postTime": "2011-03-03 23:02:00",
39
"subject": "tigergraph"
40
},
41
"v_type": "post"
42
}
43
]}]
44
}
Copied!

Vertex, Edge, and Accumulator Attributes

Accessing attributes

Attributes on vertices or edges are defined in the graph schema. Additionally, each vertex and edge has a built-in STRING attribute called type which represents the user-defined type of that edge or vertex. These attributes, including type, can be accessed for a particular edge or vertex with the dot operator:
Accessing attributes with a known name.
1
name ".type" // read only. Returns vertexType or edgeType of name
2
name "." attrName // read/write. Accesses attribute called attrName
Copied!
DYNAMIC Query Support
The name of the attribute can be parameterized using the getAttr and setAttr vertex functions, described later in this section. This allows you to write dynamic query procedures where the attribute names are specified when you run the query.
For example, the following code snippet shows two different SELECT statements which produce equivalent results. The first uses the dot operator on the vertex variable v to access the "subject" attribute, which is defined in the graph schema. The FROM clause in the first SELECT statement necessitates that any target vertices will be of type "post" (also defined in the graph schema). The second SELECT schema checks that the vertex variable v's type is a "post" vertex by using the dot operator to access the built-in type attribute.
Accessing vertex variable attributes
1
CREATE QUERY coffeeRelatedPosts() FOR GRAPH socialNet
2
{
3
allVertices = {ANY};
4
results = SELECT v FROM allVertices:s -(:e)-> post:v WHERE v.subject == "coffee";
5
PRINT results;
6
results = SELECT v FROM allVertices:s -(:e)-> :v WHERE v.type == "post" AND v.subject == "coffee";
7
PRINT results;
8
}
Copied!
Results for Query coffeeRelatedPosts
1
GSQL > RUN QUERY coffeeRelatedPosts()
2
{
3
"error": false,
4
"message": "",
5
"version": {
6
"edition": "developer",
7
"schema": 0,
8
"api": "v2"
9
},
10
"results": [
11
{"results": [{
12
"v_id": "4",
13
"attributes": {
14
"postTime": "2011-02-07 05:02:51",
15
"subject": "coffee"
16
},
17
"v_type": "post"
18
}]},
19
{"results": [{
20
"v_id": "4",
21
"attributes": {
22
"postTime": "2011-02-07 05:02:51",
23
"subject": "coffee"
24
},
25
"v_type": "post"
26
}]}
27
]
28
}
Copied!

Accumulator Functions

This section describes functions that apply to all or most accumulators. Other accumulator functions for each accumulator type are illustrated in the "Accumulator Type" section.

Previous value of accumulator

The tick operator ( ' ) can be used to read the value of an accumulator as it was at the start an ACCUM clause, before any changes that took place within the ACCUM clause. It can only be used in the POST-ACCUM clause. A typical use is to compare the value of the accumulator before and after the ACCUM clause. The PageRank algorithm provides a good example:
1
v = SELECT s
2
FROM start:s - (e_type:e) -> :t
3
ACCUM t.@received_score += s.@score/(s.outdegree(e_type))
4
POST-ACCUM s.@score = (1.0 - damping) + damping * s.@received_score,
5
s.@received_score = 0,
6
@@max_diff += abs(s.@score - s.@score');
Copied!
In the last line, we compute @@max_diff as the absolute value of the difference between the post-ACCUM score ([email protected]) and the pre-ACCUM score ([email protected]').

Set/Bag Expression and Operators

SELECT blocks take an input vertex set and perform various selection and filtering operations to produce an output set. Therefore, set/bag expressions and their operators are a useful and powerful part of the GSQL query language. A set/bag expression can use either SetAccum or BagAccum.
EBNF
1
setBagExpr := name
2
| globalAccumName
3
| name "." name
4
| name "." localAccumName
5
| name "." localAccumName ("." name "(" [argList] ")")+
6
| name "." name "(" [argList] ")" [ "." FILTER "(" condition ")" ]
7
| globalAccumName ("." name "(" [argList] ")")+
8
| setBagExpr (UNION | INTERSECT | MINUS) setBagExpr
9
| "(" argList ")"
10
| "(" setBagExpr ")"
Copied!

Set/Bag Expression Operators - UNION, INTERSECT, MINUS

The operators are straightforward, when two operands are both sets, the result expression is a set. When at least one operand is a bag, the result expression is a bag. If one operand is a bag and the other is a set, the operator treats the set operant as a bag containing one of each value.
Set/Bag Operator Examples
1
# Demonstrate Set & Bag operators
2
CREATE QUERY setOperatorsEx() FOR GRAPH minimalNet {
3
SetAccum<INT> @@setA, @@setB, @@AunionB, @@AintsctB, @@AminusB;
4
BagAccum<INT> @@bagD, @@bagE, @@DunionE, @@DintsctE, @@DminusE;
5
BagAccum<INT> @@DminusA, @@DunionA, @@AunionBbag;
6
7
BOOL x;
8
9
@@setA = (1,2,3,4); PRINT @@setA;
10
@@setB = (2,4,6,8); PRINT @@setB;
11
12
@@AunionB = @@setA UNION @@setB ; PRINT @@AunionB; // (1, 2, 3, 4, 6, 8)
13
@@AintsctB = @@setA INTERSECT @@setB; PRINT @@AintsctB; // (2, 4)
14
@@AminusB = @@setA MINUS @@setB ; PRINT @@AminusB; // C = (1, 3)
15
16
@@bagD = (1,2,2,3); PRINT @@bagD;
17
@@bagE = (2,3,5,7); PRINT @@bagE;
18
19
@@DunionE = @@bagD UNION @@bagE; PRINT @@DunionE; // (1, 2, 2, 2, 3, 3, 5, 7)
20
@@DintsctE = @@bagD INTERSECT @@bagE; PRINT @@DintsctE; // (2, 3)
21
@@DminusE = @@bagD MINUS @@bagE; PRINT @@DminusE; // (1, 2)
22
@@DminusA = @@bagD MINUS @@setA; PRINT @@DminusA; // (2)
23
@@DunionA = @@bagD UNION @@setA; PRINT @@DunionA; // (1, 1, 2, 2, 2, 3, 3, 4)
24
// because bag UNION set is a bag
25
@@AunionBbag = @@setA UNION @@setB; PRINT @@AunionBbag; // (1, 2, 3, 4, 6, 8)
26
// because set UNION set is a set
27
}
Copied!
setOperatorsEx Query Results
1
GSQL > RUN QUERY setOperatorsEx()
2
{
3
"error": false,
4
"message": "",
5
"version": {
6
"edition": "developer",
7
"schema": 0,
8
"api": "v2"
9
},
10
"results": [ {"@@setA": [ 4, 3, 2, 1 ]},
11
{"@@setB": [ 8, 6, 4, 2 ]},
12
{"@@AunionB": [ 4, 3, 2, 1, 8, 6 ]},
13
{"@@AintsctB": [ 4, 2 ]},
14
{"@@AminusB": [ 3, 1 ]},
15
{"@@bagD": [ 1, 2, 2, 3 ]},
16
{"@@bagE": [ 2, 7, 3, 5 ]},
17
{"@@DunionE": [ 1, 2, 2, 2, 3, 3, 7, 5 ]},
18
{"@@DintsctE": [ 2, 3 ]},
19
{"@@DminusE": [ 1, 2 ]},
20
{"@@DminusA": [2]},
21
{"@@DunionA": [ 1, 1, 2, 2, 2, 3, 3, 4 ]},
22
{"@@AunionBbag": [ 6, 8, 1, 2, 3, 4 ]}
23
]
24
}
Copied!
The result of these operators is another set or bag, so these operations can be nested and chained to form more complex expressions, such as
1
(setBagExpr_A INTERSECT (setBagExpr_B UNION setBagExpr_C) ) MINUS setBagExpr_D
Copied!

Set/Bag Expression Membership Operators

For example , suppose setBagExpr_A is ("a", "b", "c")
1
"a" IN setBagExpr_A => true
2
"d" IN setBagExpr_A => false
3
"a" NOT IN setBagExpr_A => false
4
"d" NOT IN setBagExpr_A => true
Copied!
The IN and NOT IN operators support all base types on the left-hand side, and any set/bag expression on the right-hand side. The base type must be the same as the accumulator's element type. IN and NOT IN return a BOOL value.
The following example uses NOT IN to exclude neighbors that are on a blocked list.
Set Membership example
1
CREATE QUERY friendsNotInblockedlist (VERTEX<person> seed, SET<VERTEX<person>> blockedList) FOR GRAPH socialNet `{
2
Start = {seed};
3
Result = SELECT v
4
FROM Start:s-(friend:e)-person:v
5
WHERE v NOT IN blockedList;
6
PRINT Result;
7
}
Copied!
Results for Query friendsNotInblockedlist
1
GSQL > RUN QUERY friendsNotInblockedlist("person1", ["person2"])
2
{
3
"error": false,
4
"message": "",
5
"version": {
6
"edition": "developer",
7
"schema": 0,
8
"api": "v2"
9
},
10
"results": [{"Result": [{
11
"v_id": "person8",
12
"attributes": {
13
"gender": "Male",
14
"id": "person8"
15
},
16
"v_type": "person"
17
}]}]
18
}
Copied!

Subqueries

A query defined with a RETURNS header following its CREATE statement is called a subquery. A subquery acts as a callable function in GSQL. They take parameters, perform a set of actions, and return a value at the end. A subquery must end with a return statement to pass its output value to a query. Exactly one type is allowed in the RETURNS header, and thus the RETURN statement can only return one expression.
A subquery must be created before the query that calls the subquery. A subquery must be installed either before or in the same INSTALL QUERY command with the query that calls the subquery.
Main Components of a Subquery
1
CREATE QUERY <query_name>() FOR GRAPH <graph_name> // Parameters are optional
2
RETURNS (INT) /* A subquery has a RETURNS header specifying
3
its return type */
4
{
5
// ...
6
// Query body goes here
7
// ...
8
RETURN <return_value> /* The return statement of a subquery. Return
9
value must be the same type as specified in
10
the RETURNS header */
11
}
Copied!

Parameter types

A subquery parameter can only be one of the following types:
  • Primitives: INT, UINT, FLOAT, DOUBLE, STRING, BOOL
  • VERTEX
  • A set or bag of primitive or VERTEX elements

Return types

A subquery's return value can be any base type or accumulator type with the following exceptions.
  • If the return type is a user-defined tuple type, a HeapAccum type, or a GroupByAccum type, the user-defined types must be defined at the catalog level.
  • If the return type is a BagAccum. SetAccum, or ListAccum with a tuple as its element, the tuple does not need to be defined at the catalog level and can be anonymous.

Recursive subqueries

Recursion is supported for subqueries and a subquery can call itself. Here is an example of a recursive subquery: The following subquery takes a set of persons as starting points, and returns all the friends within a given distance.
While recursive subqueries may look simpler in writing, they are usually not as efficient as iterative subqueries in GSQL.
Recursive Query Example
1
CREATE QUERY subFindFriendsInDistance(SET<VERTEX> seeds, INT distance)
2
FOR GRAPH friendNet RETURNS (SET<VERTEX>)
3
{
4
IF distance <= 0 THEN // Base case
5
// When distance is 0, return the seed vertices themselves
6
RETURN seeds;
7
ELSE
8
seed_vs = seeds; // Initialize starting vertices
9
// Select 1-hop neighbors from the starting points
10
next_vs = SELECT v FROM seed_vs -(friendship:e)- :v;
11
// Find the (distance-1)-hop neighbors of the 1-hop neighbors
12
// and return the union of the starting vertices and neighbors
13
RETURN seeds UNION subFindFriendsInDistance(next_vs, distance - 1);
14
END;
15
}
16
17
CREATE QUERY findFriendsInDistance(Vertex<person> p, INT distance) FOR GRAPH friendNet {
18
19
seed = {p};
20
//PRINT All Persons;
21
PRINT subFindFriendsInDistance(seed, distance) AS friends;
22
}
Copied!
Test cases: Starting from person1, search to a distance of 1 and a distance of 2.
1
GSQL> RUN QUERY findFriendsInDistance("person1", 1)
2
[
3
{
4
"friends": [
5
"person4",
6
"person3",
7
"person2",
8
"person1"
9
]
10
}
11
]
12
GSQL> RUN QUERY findFriendsInDistance("person1", 2)
13
[
14
{
15
"friends": [
16
"person4",
17
"person9",
18
"person3",
19
"person2",
20
"person6",
21
"person8",
22
"person1"
23
]
24
}
25
]
Copied!

Examples of Expressions

Below is a list of examples of expressions. Note that ( argList ) is a set/bag expression, while [ argList ] is a list expression.
Expression Examples
1
#Show various types of expressions
2
CREATE QUERY expressionEx() FOR GRAPH workNet {
3
TYPEDEF tuple<STRING countryName, STRING companyName> companyInfo;
4
5
ListAccum<STRING> @companyNames;
6
SumAccum<INT> @companyCount;
7
SumAccum<INT> @numberOfRelationships;
8
ListAccum<companyInfo> @info;
9
MapAccum< STRING,ListAccum<STRING> > @@companyEmployeeRelationships;
10
SumAccum<INT> @@totalRelationshipCount;
11
12
ListAccum<INT> @@valueList;
13
SetAccum<INT> @@valueSet;
14
15
SumAccum<INT> @@a;
16
SumAccum<INT> @@b;
17
18
#expr := constant
19
@@a = 10;
20
21
#expr := ["@@"] name
22
@@b = @@a;
23
24
#expr := expr mathOperator expr
25
@@b = @@a + 5;
26
27
#expr := "(" expr ")"
28
@@b = (@@a + 5);
29
30
#expr := "-" expr
31
@@b = -(@@a + 5);
32
33
PRINT @@a, @@b;
34
35
#expr := "[" argList "]" // a list
36
@@valueList = [1,2,3,4,5];
37
@@valueList += [24,80];
38
39
#expr := "(" argList ")" // setBagExpr
40
@@valueSet += (1,2,3,4,5);
41
42
#expr := ( COUNT | ISEMPTY | MAX | MIN | AVG | SUM ) "(" setBagExpr ")"
43
PRINT MAX(@@valueList);
44
PRINT AVG(@@valueList);
45
46
seed = {ANY};
47
48
company1 = SELECT t FROM seed:s -(worksFor)-> :t WHERE (s.id == "company1");
49
company2 = SELECT t FROM seed:s -(worksFor)-> :t WHERE (s.id == "company2");
50
51
#expr := setBagExpr
52
worksForBoth = company1 INTERSECT company2;
53
PRINT worksForBoth;
54
55
#expr := name "." "type"
56
employees = SELECT s FROM seed:s WHERE (s.type == "person");
57
58
employees = SELECT s FROM employees:s -(worksFor)-> :t
59
60
ACCUM
61
#expr := name "." ["@"] name
62
s.@companyNames += t.id,
63
64
#expr := name "."name "(" [argList] ")" [ "."FILTER "(" condition ")" ]
65
s.@numberOfRelationships += s.outdegree(),
66
67
#expr := name ["<" type ["," type"]* ">"] "(" [argList] ")"
68
s.@info += companyInfo(t.country, t.id)
69
70
POST-ACCUM
71
#expr := name "."localAccumName ("."name "(" [argList] ")")+ ["."name]
72
s.@companyCount += s.@companyNames.size(),
73
74
#expr := name "."localAccumName ["\'"]
75
@@totalRelationshipCount += s.@companyCount,
76
77
FOREACH comp IN s.@companyNames DO
78
#expr := "(" argList "->" argList ")"
79
@@companyEmployeeRelationships += (s.id -> comp)
80
END;
81
82
PRINT employees;
83
PRINT @@totalRelationshipCount;
84
PRINT @@companyEmployeeRelationships;
85
86
#expr := globalAccumName ("."name "(" [argList] ")")+ ["."name]
87
PRINT @@companyEmployeeRelationships.size();
88
}
Copied!
expressionEx.json Results
1
GSQL > RUN QUERY expressionEx()
2
{
3
"error": false,
4
"message": "",
5
"version": {
6
"edition": "developer",
7
"schema": 0,
8
"api": "v2"
9
},
10
"results": [
11
{
12
"@@a": 10,
13
"@@b": -15
14
},
15
{"max(@@valueList)": 80},
16
{"avg(@@valueList)": 17},
17
{"worksForBoth": [
18
{
19
"v_id": "person2",
20
"attributes": {
21
"interestList": ["engineering"],
22
"@companyCount": 0,
23
"@numberOfRelationships": 0,
24
"skillSet": [ 6, 5, 3, 2 ],
25
"skillList": [ 2, 3, 5, 6 ],
26
"locationId": "chn",
27
"interestSet": ["engineering"],
28
"@info": [],
29
"id": "person2",
30
"@companyNames": []
31
},
32
"v_type": "person"
33
},
34
{
35
"v_id": "person1",
36
"attributes": {
37
"interestList": [ "management", "financial" ],
38
"@companyCount": 0,
39
"@numberOfRelationships": 0,
40
"skillSet": [ 3, 2, 1 ],
41
"skillList": [ 1, 2, 3 ],
42
"locationId": "us",
43
"interestSet": [ "financial", "management" ],
44
"@info": [],
45
"id": "person1",
46
"@companyNames": []
47
},
48
"v_type": "person"
49
}
50
]},
51
{"employees": [
52
{
53
"v_id": "person4",
54
"attributes": {
55
"interestList": ["football"],
56
"@companyCount": 1,
57
"@numberOfRelationships": 1,
58
"skillSet": [ 10, 1, 4 ],
59
"skillList": [ 4, 1, 10 ],
60
"locationId": "us",
61
"interestSet": ["football"],
62
"@info": [{ "companyName": "company2", "countryName": "chn" }],
63
"id": "person4",
64
"@companyNames": ["company2"]
65
},
66
"v_type": "person"
67
},
68
{
69
"v_id": "person12",
70
"attributes": {
71
"interestList": [
72
"music",
73
"engineering",
74
"teaching",
75
"teaching",
76
"teaching"
77
],
78
"@companyCount": 1,
79
"@numberOfRelationships": 1,
80
"skillSet": [ 2, 5, 1 ],
81
"skillList": [ 1, 5, 2, 2, 2 ],
82
"locationId": "jp",
83
"interestSet": [ "teaching", "engineering", "music" ],
84
"@info": [{ "companyName": "company4", "countryName": "us" }],
85
"id": "person12",
86
"@companyNames": ["company4"]
87
},
88
"v_type": "person"
89
},
90
{
91
"v_id": "person3",
92
"attributes": {
93
"interestList": ["teaching"],
94
"@companyCount": 1,
95
"@numberOfRelationships": 1,
96
"skillSet": [ 6, 1, 4 ],
97
"skillList": [ 4, 1, 6 ],
98
"locationId": "jp",
99
"interestSet": ["teaching"],
100
"@info": [{ "companyName": "company1", "countryName": "us" }],
101
"id": "person3",
102
"@companyNames": ["company1"]
103
},
104
"v_type": "person"
105
},
106
{
107
"v_id": "person9",
108
"attributes": {
109
"interestList": [ "financial", "teaching" ],
110
"@companyCount": 2,
111
"@numberOfRelationships": 4,
112
"skillSet": [ 2, 7, 4 ],
113
"skillList": [ 4, 7, 2 ],
114
"locationId": "us",
115
"interestSet": [ "teaching", "financial" ],
116
"@info": [
117
{
118
"companyName": "company3",
119
"countryName": "jp"
120
},
121
{
122
"companyName": "company2",
123
"countryName": "chn"
124
}
125
],
126
"id": "person9",
127
"@companyNames": [ "company3", "company2" ]
128
},
129
"v_type": "person"
130
},
131
{
132
"v_id": "person11",
133
"attributes": {
134
"interestList": [ "sport", "football" ],
135
"@companyCount": 1,
136
"@numberOfRelationships": 1,
137
"skillSet": [10],