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
constant := numeric | stringLiteral | TRUE | FALSE | GSQL_UINT_MAX | GSQL_INT_MAX | GSQL_INT_MIN | TO_DATETIME "(" stringLiteral ")"mathOperator :="*" | "/" | "%" | "+" | "-" | "<<" | ">>" | "&" | "|"condition := expr | expr comparisonOperator expr | expr [ NOT ] IN setBagExpr | expr IS [ NOT ] NULL | expr BETWEEN expr AND expr | "(" condition ")" | NOT condition | condition (AND | OR) condition | (TRUE | FALSE) | expr [NOT] LIKE expr [ESCAPE escape_char] comparisonOperator :="<" | "<=" | ">" | ">=" | "==" | "!="aggregator := COUNT | MAX | MIN | AVG | SUMexpr := name | globalAccumName | name ".type" | name "." name | name "." localAccumName ["\'"] | name "." name "." name "(" [argList] ")" | name "." name "(" [argList] ")" [ "." FILTER "(" condition ")" ] | name ["<" type ["," type]*">"] "(" argList] ")" | name "."localAccumName ("." name "(" [argList] ")")+ ["." name] | globalAccumName ("."name "(" [argList] ")")+ ["."name] | COALESCE "(" [argList] ")" | aggregator "(" [DISTINCT] setBagExpr ")" | ISEMPTY "(" setBagExpr ")" | expr mathOperator expr | "-" expr | "(" expr ")" | "(" argList "->" argList ")"// key value pair for MapAccum | "[" argList "]"// a list | constant | setBagExpr | name "(" argList ")"// function call or a tuple object setBagExpr := name | globalAccumName | name "." name | name "." localAccumName | name "."localAccumName ("." name "(" [argList] ")")+ | name "." name "(" [argList] ")" [ "." FILTER "(" condition ")" ] | globalAccumName ("." name "(" [argList] ")")+ | setBagExpr (UNION | INTERSECT | MINUS) setBagExpr | "(" argList ")" | "(" setBagExpr ")"argList := expr ["," expr]*
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_MAXGSQL_INT_MIN
boolean
TRUEFALSE
string
stringLiteral
"atoz@com" "0.25"
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".
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
*, /, %-, +<<, >>&|==, >=, >, <=, <, !=
Example 1. Math Operators + - * /
CREATE QUERY mathOperators() FOR GRAPH minimalNet api("v2"){int x,y;int z1,z2,z3,z4,z5;float f1,f2,f3,f4; x =7; y =3; z1 = x * y; # z =21 z2 = x - y; # z =4 z3 = x + y; # z =10 z4 = x / y; # z =2 z5 = x /4.0; # z =1 f1 = x / y; # v =2 f2 = x /4.0; # v =1.75 f3 = x % 3; # v =1 f4 = x % y; # z =1PRINT x,y;PRINT z1 AS xTIMESy, z2 AS xMINUSy, z3 AS xPLUSy, z4 AS xDIVy, z5 AS xDIV4f;PRINT f1 AS xDIVy, f2 AS xDIV4f, f3 AS xMOD3, f4 AS xMODy;}
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.
comparisonOperator :="<" | "<=" | ">" | ">=" | "==" | "!="condition := expr | expr comparisonOperator expr | expr [ NOT ] IN setBagExpr | expr IS [ NOT ] NULL | expr BETWEEN expr AND expr | "(" condition ")" | NOT condition | condition (AND | OR) condition | (TRUE | FALSE) | expr [NOT] LIKE expr [ESCAPE escape_char]
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
CREATE QUERY mathOperatorBetween() FOR GRAPH minimalNet{int x; bool b; x =1; b = (x BETWEEN0AND100); PRINT b; # True b = (x BETWEEN1AND2); PRINT b; # True b = (x BETWEEN0AND1); PRINT b; # True}
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
CREATE QUERY parameterIsNULL (INT p) FOR GRAPH minimalNet {IF p ISNULLTHENPRINT"p is null";ELSEPRINT"p is not null";END;}
parameterIsNULL.json Results
GSQL > RUN QUERY parameterIsNULL(_){"error": false,"message": "","version": {"edition": "developer","schema": 0,"api": "v2" },"results": [{"p is null": "p is null"}]}GSQL > RUN QUERY parameterIsNULL(3){"error": false,"message": "","version": {"edition": "developer","schema": 0,"api": "v2" },"results": [{"p is not null": "p is not null"}]}
Every attribute value stored in GSQL is a valid value, so IS NULL and IS NOT NULL is only effective for query parameters.
LIKE
expr [NOT] LIKE expr [ESCAPE escape_char]
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 string1matches 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_patterncan 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
CREATE QUERY printAPosts(/* Parameters here */) FOR GRAPH socialNet { /* Write query logic here */ posts = {post.*}; aPosts =SELECT v FROM posts:v WHERE v.subject LIKE"%a%"; //Return all posts that has the //character"a"in its subjectPRINT aPosts;}
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.
name".type"//read only. Returns vertexType or edgeType of namename"." attrName //read/write. Accesses attribute called attrName
DYNAMIC Query Support
The name of the attribute can be parameterized using the getAttr and setAttrvertex 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
CREATE QUERY coffeeRelatedPosts() FOR GRAPH socialNet{ allVertices = {ANY}; results =SELECT v FROM allVertices:s -(:e)-> post:v WHERE v.subject =="coffee";PRINT results; results =SELECT v FROM allVertices:s -(:e)-> :v WHERE v.type =="post"AND v.subject =="coffee";PRINT results;}
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:
In the last line, we compute @@max_diff as the absolute value of the difference between the post-ACCUM score (s.@score) and the pre-ACCUM score (s.@score').
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.
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.
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
(setBagExpr_A INTERSECT (setBagExpr_B UNION setBagExpr_C) ) MINUS setBagExpr_D
Set/Bag Expression Membership Operators
For example , suppose setBagExpr_A is ("a", "b", "c")
"a" IN setBagExpr_A => true
"d" IN setBagExpr_A => false
"a" NOT IN setBagExpr_A => false
"d" NOT IN setBagExpr_A => true
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
CREATE QUERY friendsNotInblockedlist (VERTEX<person> seed, SET<VERTEX<person>> blockedList) FOR GRAPH socialNet `{ Start = {seed}; Result = SELECT v FROM Start:s-(friend:e)-person:v WHERE v NOT IN blockedList; PRINT Result;}
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
CREATE QUERY <query_name>() FOR GRAPH <graph_name>// Parameters are optionalRETURNS (INT) /* A subquery has a RETURNS header specifying its return type */{// ...// Query body goes here// ...RETURN<return_value>/* The return statement of a subquery. Return value must be the same type as specified in the RETURNS header */}
Parameter types
A subquery parameter can only be one of the following 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.
CREATE QUERY subFindFriendsInDistance(SET<VERTEX> seeds, INT distance)FOR GRAPH friendNet RETURNS (SET<VERTEX>){IF distance <=0THEN// Base case//When distance is0, return the seed vertices themselvesRETURN seeds;ELSE seed_vs = seeds; // Initialize starting vertices//Select1-hop neighbors from the starting points next_vs =SELECT v FROM seed_vs -(friendship:e)- :v;// Find the (distance-1)-hop neighbors of the 1-hop neighbors//andreturn the union of the starting vertices and neighborsRETURN seeds UNION subFindFriendsInDistance(next_vs, distance -1);END;}CREATE QUERY findFriendsInDistance(Vertex<person> p, INT distance) FOR GRAPH friendNet { seed = {p};//PRINT All Persons;PRINT subFindFriendsInDistance(seed, distance) AS friends;}
Test cases: Starting from person1, search to a distance of 1 and a distance of 2.