Accumulators

Accumulators are special types of variables that accumulate information about the graph during its traversal and exploration. Because they are a unique and important feature of the GSQL query language, we devote a separate section for their introduction, but additional detail on their usage will be covered in other sections, the "SELECT Statement" section in particular. This section covers the following subset of the EBNF language definitions:

EBNF
accumDeclStmt := accumType "@"accumName ["=" constant][, "@"accumName ["=" constant]]*
| [STATIC] accumType "@@"accumName ["=" constant][, "@@"accumName ["=" constant]]*
accumType := "SumAccum" "<" ( INT | FLOAT | DOUBLE | STRING | STRING COMPRESS) ">"
| "MaxAccum" "<" ( INT | FLOAT | DOUBLE ) ">"
| "MinAccum" "<" ( INT | FLOAT | DOUBLE ) ">"
| "AvgAccum"
| "OrAccum"
| "AndAccum"
| "BitwiseOrAccum"
| "BitwiseAndAccum"
| "ListAccum" "<" type ">"
| "SetAccum" "<" elementType ">"
| "BagAccum" "<" elementType ">"
| "MapAccum" "<" elementType "," (baseType | accumType | tupleType) ">"
| "HeapAccum" "<" tupleType ">" "(" simpleSize "," fieleName [ASC | DESC]
["," fieldName [ASC | DESC]]* ")"
| "GroupByAccum" "<" elementType fieldName ["," elementType fieldName]* ,
accumType fieldName ["," accumType fieldName]* ">"
| "ArrayAccum" "<" accumName ">"
elementType := baseType | tupleType | STRING COMPRESS
gAccumAccumStmt := "@@"accumName "+=" expr
accumClause := ACCUM DMLSubStmtList
postAccumClause := POST-ACCUM DMLSubStmtList

There are a number of different types of accumulators, each providing specific accumulation functions. Accumulators are declared to have one of two types of association: global or vertex-attached .

More technically, accumulators are mutable mutex variables shared among all the graph computation threads exploring the graph within a given query. To improve performance, the graph processing engine employs multithreaded processing. Modification of accumulators is coordinated at run-time so the accumulation operator works correctly (i.e., mutually exclusively) across all threads. This is particularly relevant in the ACCUM clause. During traversal of the graph, the selected set of edges or vertices is partitioned among a group of threads. These threads have shared mutually exclusive access to the accumulators.

Declaration of Accumulators

All accumulator variables must be declared at the beginning of a query, immediately after any typedefs, and before any other type of statement. The scope of the accumulator variables is the entire query.

The name of a vertex-attached accumulator begins with a single "@". The name of a global accumulator begins with "@@". Additionally, a global accumulator may be declared to be static.

EBNF for Accumulator Declaration
accumDeclStmt := accumType "@"accumName ["=" constant][, "@"accumName ["=" constant]]*
| [STATIC] accumType "@@"accumName ["=" constant][, "@@"accumName ["="

Vertex-attached Accumulators

Vertex-attached accumulators are mutable state variables that are attached to each vertex in the graph for the duration of the query's lifetime. They act as run-time attributes of a vertex. They are shared, mutual exclusively, among all of the query's processes. Vertex-attached accumulators can be set to a value with the = operator. Additionally, an accumulate operator += can be used to update the state of the accumulator; the function of += depends on the accumulator type. In the example below, there are two accumulators attached to each vertex. The initial value of an accumulator of a given type is predefined, however it can be changed at declaration as in the accumulator @weight below. All vertex-attached accumulator names have a single leading at-sign "@".

Vertex-Attached Accumulators
SumAccum<int> @neighbors;
MaxAccum<float> @weight = 2.8;

If there is a graph with 10 vertices, then there is an instance of @neighbors and @weight for each vertex (hence 10 of each, and 20 total accumulator instances). These are accessed via the dot operator on a vertex variable or a vertex alias (e.g., v.@neighbor ). The accumulator operator += only impacts the accumulator for the specific vertex being referenced. A statement such as v1.@neighbors += 1will only impact v1 's @neighbors and not the @neighbors for other vertices.

Vertex-attached accumulators can only be accessed or updated (via = or +=) in an ACCUM or POST-ACCUM clause within a SELECT block. The only exception to this rule is that vertex-attached accumulators can be referenced in a PRINT statement, as the PRINT has access to all information attached to a vertex set.

Edge-attached accumulators are not supported.

Global Accumulators

A global accumulator is a single mutable accumulator that can be accessed or updated within a query. The names of global accumulators start with a double at-sign "@@".

Global Accumulators
SumAccum<int> @@totalNeighbors;
MaxAccum<float> @@entropy = 1.0;

Global accumulators can only be assigned (using the = operator) outside a SELECT block (i.e., not within an ACCUM or POST-ACCUM clause). Global accumulators can be accessed or updated via the accumulate operator += anywhere within a query, including inside a SELECT block.

It is important to note that the accumulation operation for global accumulators in an ACCUM clause executes once for each process. That is, if the FROM clause uses an edge-induced selection (introduced in Section "SELECT Statement"), the ACCUM clause executes one process for each edge in the selected edge set. If the FROM clause uses a vertex-induced selection (introduced in Section "SELECT Statement"), the ACCUM clause executes one process for each vertex in the selected vertex set. Since global accumulators are shared in a mutually exclusive manner among processes, they behave very differently than a non-accumulator variable (see Section "Variable Types" for more details) in an ACCUM clause. Take the following code example. The global accumulator@@globalRelationshipCount is accumulated for every worksFor edge traversed since it is shared among processes. Conversely, relationshipCount appears to have only been incremented once. This is because a non-accumulator variable is not shared among processes. Each process has its own separate unshared copy of relationshipCount and increments the original value by one. (E.g., each process increments relationshipCount from 0 to 1.) There is no accumulation and the final value is one.

Global Variable vs Global Accumulator
#Count the total number of employment relationships for all companies
CREATE QUERY countEmploymentRelationships() FOR GRAPH workNet {
INT localRelationshipCount;
SumAccum<INT> @@globalRelationshipCount;
start = {company.*};
companies = SELECT s FROM start:s -(worksFor)-> :t
ACCUM @@globalRelationshipCount += 1,
localRelationshipCount = localRelationshipCount + 1;
PRINT localRelationshipCount;
PRINT @@globalRelationshipCount;
}
countEmploymentRelationship.json Results
GSQL > RUN QUERY countEmploymentRelationships()
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [
{"localRelationshipCount": 1},
{"@@globalRelationshipCount": 17}
]
}

Static Global Accumulators

A static global accumulator retains its value after the execution of a query. To declare a static global accumulator, include the STATIC keyword at the beginning of the declaration statement. For example, if a static global accumulator is incremented by 1 each time a query is executed, then its value is equal to the number of times the query has been run, since the query was installed. Each static global accumulator belongs to the particular query in which it is declared; it cannot be shared among different queries. The value only persists in the context of running the same query multiple times. The value will reset to the default value when the GPE is restarted.

Static Global Accumulators example
CREATE QUERY staticAccumEx(INT x) FOR GRAPH minimalNet {
STATIC ListAccum<INT> @@testList;
@@testList += x;
PRINT @@testList;
}
staticAccumEx.json Result
GSQL > RUN QUERY staticAccumEx(3)
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [{"@@testList": [
3,
-5,
3
]}]
}
GSQL > RUN QUERY staticAccumEx(-5)
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [{"@@testList": [
3,
-5,
3,
-5
]}]
}

There is no command to deallocate a static global accumulator. If a static global accumulator is a collection accumulator and it no longer needed, it should be cleared to minimize the memory usage.

Accumulator Types

The following are the accumulator types we currently support. Each type of accumulator supports one or more data types .

EBNF for Accumulator Types
accumType := "SumAccum" "<" ( INT | FLOAT | DOUBLE | STRING | STRING COMPRESS) ">"
| "MaxAccum" "<" ( INT | FLOAT | DOUBLE ) ">"
| "MinAccum" "<" ( INT | FLOAT | DOUBLE ) ">"
| "AvgAccum"
| "OrAccum"
| "AndAccum"
| "BitwiseOrAccum"
| "BitwiseAndAccum"
| "ListAccum" "<" type ">"
| "SetAccum" "<" elementType ">"
| "BagAccum" "<" elementType ">"
| "MapAccum" "<" elementType "," (baseType | accumType | tupleType) ">"
| "HeapAccum" "<" tupleType ">" "(" simpleSize "," fieleName [ASC | DESC]
["," fieldName [ASC | DESC]]* ")"
| "GroupByAccum" "<" elementType fieldName ["," elementType fieldName]* ,
accumType fieldName ["," accumType fieldName]* ">"
| "ArrayAccum" "<" accumName ">"
elementType := baseType | tupleType | STRING COMPRESS
gAccumAccumStmt := "@@"accumName "+=" expr

The accumulators fall into two major groups :

  • Scalar Accumulators store a single value:

    • SumAccum

    • MinAccum, MaxAccum

    • AvgAccum

    • AndAccum, OrAccum

    • BitwiseAndAccum, BitwiseOrAccum

  • Collection Accumulators store a set of values:

    • ListAccum

    • SetAccum

    • BagAccum

    • MapAccum

    • ArrayAccum

    • HeapAccum

    • GroupByAccum

The details of each accumulator type are summarized in the table below. The Accumulation Operation column explains how the accumulator accumName is updated when the statement accumName += newVal is executed. Following the table are example queries for each accumulator type.

Table Ac1: Accumulator Types and Their Accumulation Behavior

Accumulator Type (Case Sensitive)

Default Initial Value

Accumulation operation (result of accumName += newVal )

SumAccum<INT>

0

accumName plus newVal

SumAccum<FLOAT or DOUBLE>

0.0

accumName plus newVal

SumAccum<STRING>

empty string

String concatenation of accumName and newVal

MaxAccum<INT>

INT_MIN

The greater of newVal and accumName

MaxAccum<FLOAT or DOUBLE>

FLOAT_MIN or DOUBLE_MIN

The greater of newVal and accumName

MaxAccum<STRING>

empty string

The greater of newVal and accumName , according to UTF-8 lexicographical ordering

MaxAccum<VERTEX>

the vertex with internal id 0

The vertex with the greater internal id , either newVal or accumName

MaxAccum<tupleTyple>

default for each field of the tuple

The greater of newVal and accumName. tupleType is a user-defined sequence of baseTypes. Ordering is hierarchical, using the leftmost field of the tuple first, then the next field, and so on.

MinAccum<INT>

INT_MAX

The lesser of newVal and accumName

MinAccum<FLOAT or DOUBLE>

FLOAT_MAX or DOUBLE_MAX

The lesser of newVal and accumName

MinAccum<STRING>

empty string

The lesser of newVal and accumName , according to UTF-8 lexicographical ordering

MinAccum<VERTEX>

unknown

The vertex with the lesser internal id, either newVal or accumName

MinAccum<tupleType>

default for each field of the tuple

The lesser of newVal and accumName. tupleType is a user-defined sequence of baseTypes. Ordering is hierarchical, using the leftmost field of the tuple first, then the next field, and so on.

AvgAccum

0.0 (double precision)

Double precision average of newVal and all previous values accumulated toaccumName

AndAccum

True

Boolean AND of newVal and accumName

OrAccum

False

Boolean OR of newVal and accumName

BitwiseAndAccum

-1 (INT) = 64-bit sequence of 1s

Bitwise AND of newVal and accumName

BitwiseOrAccum

0 (INT) = 64-bit sequence of 0s

Bitwise OR of newVal and accumName

ListAccum< typ e >

(ordered collection of elements)

empty list

List with newVal appended to end of accumName. newVal can be a single value or a list. If accumName is [ 2, 4, 6 ], then accumName += 4 produces accumName equal to [ 2, 4, 6, 4 ]

SetAccum<t ype >

(unordered collection of elements, duplicate items not allowed)

empty set

Set union of newVal and accumName . newVal can be a single value or a set/bag.If accumName is ( 2, 4, 6 ), then accumName += 4 produces accumName equal to ( 2, 4, 6)

BagAccum<t ype >

(unordered collection of elements, duplicate items allowed)

empty bag

Bag union of newVal and accumName . newVal can be a single value or a set/bag.If accumName is ( 2, 4, 6 ), then accumName += 4 would result in accumName equal to ( 2, 4, 4, 6)

MapAccum< type, type >

(unordered collection of (key,value) pairs)

empty map

Add or update a key:value pair to the accumName map. If accumName is [ ("red",3), ("green",4),("blue",2) ], then accumName += ("black"-> 5) produces accumName equal to [ ("red",3), ("green",4),("blue",2), ("black",5) ]

ArrayAccum< accumType >

empty list

See the ArrayAccum section below for details.

HeapAccum< tuple >(heapSize, sortKey [, sortKey_i]*)

(sorted collection of tuples)

empty heap

Insert newVal into the accumName heap, maintaining the heap in sorted order, according to the sortKey(s) and size limit declared for this HeapAccum

GroupByAccum< type [, type]* , accumType [, accumType]* >

empty group by map

Add or update a key:value pair in accumName . See Section "GroupByAccum" for more details.

SumAccum

The SumAccum type computes and stores the cumulative sum of numeric values or the cumulative concatenation of text values. The output of a SumAccum is a single numeric or string value. SumAccum variables operate on values of type INT , UINT, FLOAT, DOUBLE, or STRING only.

The += operator updates the accumulator's state. For INT, FLOAT, and DOUBLE types, += arg performs a numeric addition, while for the STRING value type += arg concatenates arg to the current value of the SumAccum.

SumAccum Example
# SumAccum Example
CREATE QUERY sumAccumEx() FOR GRAPH minimalNet {
SumAccum<INT> @@intAccum;
SumAccum<FLOAT> @@floatAccum;
SumAccum<DOUBLE> @@doubleAccum;
SumAccum<STRING> @@stringAccum;
@@intAccum = 1;
@@intAccum += 1;
@@floatAccum = @@intAccum;
@@floatAccum = @@floatAccum / 3;
@@doubleAccum = @@floatAccum * 8;
@@doubleAccum += -1;
@@stringAccum = "Hello ";
@@stringAccum += "World";
PRINT @@intAccum;
PRINT @@floatAccum;
PRINT @@doubleAccum;
PRINT @@stringAccum;
}
sumAccumEx.json Result
GSQL > RUN QUERY sumAccumEx()
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [
{"@@intAccum": 2},
{"@@floatAccum": 0.66667},
{"@@doubleAccum": 4.33333},
{"@@stringAccum": "Hello World"}
]
}

MinAccum / MaxAccum

The MinAccum and MaxAccum types calculate and store the cumulative minimum or the cumulative maximum of a series of values. The output of a MinAccum or a MaxAccum is a single value of the type that was passed in. MinAccum and MaxAccum variables operate on values of type INT, UINT, FLOAT, DOUBLE, STRING, TUPLE, and VERTEX (with optional specific vertex type) only.

For MinAccum, += arg checks if the current value held is less than arg and stores the smaller of the two. MaxAccum behaves the same, with the exception that it checks for and stores the greater instead of the lesser of the two.

MinAccum and MaxAccum Example
# MinAccum and MaxAccum Example
CREATE QUERY minMaxAccumEx() FOR GRAPH minimalNet {
MinAccum<INT> @@minAccum;
MaxAccum<FLOAT> @@maxAccum;
@@minAccum += 40;
@@minAccum += 20;
@@minAccum += -10;
@@maxAccum += -1.1;
@@maxAccum += 2.5;
@@maxAccum += 2.8;
PRINT @@minAccum;
PRINT @@maxAccum;
}
minMaxAccumEx.json Result
GSQL > RUN QUERY minMaxAccumEx()
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [
{"@@minAccum": -10},
{"@@maxAccum": 2.8}
]
}

String minimum and maximum values are based on their UTF-8 codes, which is a multilingual superset of the ASCII codes. Within ASCII, a < z, uppercase is less than lowercase, and digits are less than alphabetic characters.

MinAccum and MaxAccum operating on VERTEX type have a special comparison. They do not compare vertex ids, but TigerGraph internal ids, which might not be in the same order as the external ids. Comparing internal ids is much faster, so MinAccum/MaxAccum<VERTEX> provide an efficient way to compare and select vertices. This is helpful for some graph algorithms that require the vertices to be numbered and sortable . For example, the following query returns one post from each person. The returned vertex is not necessarily the vertex with alphabetically largest id.

Tuple data types are treated as hierarchical structures, where the first field used for ordering is the leftmost one.

MaxAccum<VERTEX> example
# Output one random post vertex from each person
CREATE QUERY minMaxAccumVertex() FOR GRAPH socialNet api("v2") {
MaxAccum<VERTEX> @maxVertex;
allUser = {person.*};
allUser = SELECT src
FROM allUser:src -(posted)-> post:tgt
ACCUM src.@maxVertex += tgt
ORDER BY src.id;
PRINT allUser[allUser.@maxVertex]; // api v2
}
minMaxAccuxVertex.json Result
GSQL > RUN QUERY minMaxAccumVertex()
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [{"allUser": [
{
"v_id": "person1",
"attributes": {"allUser.@maxVertex": "0"},
"v_type": "person"
},
{
"v_id": "person2",
"attributes": {"allUser.@maxVertex": "1"},
"v_type": "person"
},
{
"v_id": "person3",
"attributes": {"allUser.@maxVertex": "2"},
"v_type": "person"
},
{
"v_id": "person4",
"attributes": {"allUser.@maxVertex": "3"},
"v_type": "person"
},
{
"v_id": "person5",
"attributes": {"allUser.@maxVertex": "11"},
"v_type": "person"
},
{
"v_id": "person6",
"attributes": {"allUser.@maxVertex": "10"},
"v_type": "person"
},
{
"v_id": "person7",
"attributes": {"allUser.@maxVertex": "9"},
"v_type": "person"
},
{
"v_id": "person8",
"attributes": {"allUser.@maxVertex": "7"},
"v_type": "person"
}
]}]
}

AvgAccum

The AvgAccum type calculates and stores the cumulative mean of a series of numeric values. Internally, its state information includes the sum value of all inputs and a count of how many input values it has accumulated. The output is the mean value; the sum and the count values are not accessible to the user. The data type of a AvgAccum variable is not declared; all AvgAccum accumulators accept inputs of type INT, UINT, FLOAT, and DOUBLE. The output is always DOUBLE type.

The += arg operation updates the AvgAccum variable's state to be the mean of all the previous arguments along with the current argument; The = arg operation clears all the previously accumulated state and sets the new state to be arg with a count of one.

AvgAccum Example
# AvgAccum Example
CREATE QUERY avgAccumEx() FOR GRAPH minimalNet {
AvgAccum @@averageAccum;
@@averageAccum += 10;
@@averageAccum += 5.5; # avg = (10+5.5) / 2.0
@@averageAccum += -1; # avg = (10+5.5-1) / 3.0
PRINT @@averageAccum; # 4.8333...
@@averageAccum = 99; # reset
@@averageAccum += 101; # avg = (99 + 101) / 2
PRINT @@averageAccum; # 100
}
avgAccumEx.json Result
GSQL > RUN QUERY avgAccumEx()
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [
{"@@averageAccum": 4.83333},
{"@@averageAccum": 100}
]
}

AndAccum / OrAccum

The AndAccum and OrAccum types calculate and store the cumulative result of a series of boolean operations. The output of an AndAccum or an OrAccum is a single boolean value (True or False). AndAccum and OrAccum variables operate on boolean values only. The data type does not need to be declared.

For AndAccum, += arg updates the state to be the logical AND between the current boolean state and arg . OrAccum behaves the same, with the exception that it stores the result of a logical OR operation.

AndAccum and OrAccum Example
# AndAccum and OrAccum Example
CREATE QUERY andOrAccumEx() FOR GRAPH minimalNet {
# T = True
# F = False
AndAccum @@andAccumVar; # (default value = T)
OrAccum @@orAccumVar; # (default value = F)
@@andAccumVar += True; # T and T = T
@@andAccumVar += False; # T and F = F
@@andAccumVar += True; # F and T = F
PRINT @@andAccumVar;
@@orAccumVar += False; # F or F == F
@@orAccumVar += True; # F or T == T
@@orAccumVar += False; # T or F == T
PRINT @@orAccumVar;
}
andOrAccumEx.json Result
GSQL > RUN QUERY andOrAccumEx()
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [
{"@@andAccumVar": false},
{"@@orAccumVar": true}
]
}

BitwiseAndAccum / BitwiseOrAccum

The BitwiseAndAccum and BitwiseOrAccum types calculate and store the cumulative result of a series of bitwise boolean operations and store the resulting bit sequences. BitwiseAndAccum and BitwiseOrAccum operator on INT only. The data type does not need to be declared.

Fundamental for understanding and using bitwise operations is the knowledge that integers are stored in base-2 representation as a 64-bit sequence of 1s and 0s. "Bitwise" means that each bit is treated as a separate boolean value, with 1 representing true and 0 representing false. Hence, an integer is equivalent to a sequence of boolean values. Computing the Bitwise AND of two numbers A and B means to compute the bit sequence C where the j th bit of C, denoted C j , is equal to (A j AND B j ).

For BitwiseAndAccum, += arg updates the accumulator's state to be the Bitwise AND of the current state and arg . BitwiseOrAccum behaves the same, with the exception that it computes a Bitwise OR.

Bitwise Operations and Negative Integers

Most computer systems represent negative integers using "2's complement" format, where the uppermost bit has special significance. Operations which affect the uppermost bit are crossing the boundary between positive and negative numbers, and vice versa.

BitwiseAndAccum and BitwiseOrAccum Example
# BitwiseAndAccum and BitwiseOrAccum Example
CREATE QUERY bitwiseAccumEx() FOR GRAPH minimalNet {
BitwiseAndAccum @@bwAndAccumVar; # default value = 64-bits of 1 = -1 (INT)
BitwiseOrAccum @@bwOrAccumVar; # default value = 64-bits of 0 = 0 (INT))
# 11110000 = 240
# 00001111 = 15
# 10101010 = 170
# 01010101 = 85
# BitwiseAndAccum
@@bwAndAccumVar += 170; # 11111111 & 10101010 -> 10101010
@@bwAndAccumVar += 85; # 10101010 & 01010101 -> 00000000
PRINT @@bwAndAccumVar; # 0
@@bwAndAccumVar = 15; # reset to 00001111
@@bwAndAccumVar += 85; # 00001111 & 01010101 -> 00000101
PRINT @@bwAndAccumVar; # 5
# BitwiseOrAccum
@@bwOrAccumVar += 170; # 00000000 | 10101010 -> 10101010
@@bwOrAccumVar += 85; # 10101010 | 01010101 -> 11111111 = 255
PRINT @@bwOrAccumVar; # 255
@@bwOrAccumVar = 15; # reset to 00001111
@@bwOrAccumVar += 85; # 00001111 | 01010101 -> 01011111 = 95
PRINT @@bwOrAccumVar; # 95
}
bitwiseAccumEx.json Result
GSQL > RUN QUERY bitwiseAccumEx()
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [
{"@@bwAndAccumVar": 0},
{"@@bwAndAccumVar": 5},
{"@@bwOrAccumVar": 255},
{"@@bwOrAccumVar": 95}
]
}

ListAccum

The ListAccum type maintains a sequential collection of elements. The output of a ListAccum is a list of values in the order the elements were added. The element type can be any base type, tuple, or STRING COMPRESS. Additionally, a ListAccum can contain a nested collection of type ListAccum. Nesting of ListAccums is limited to a depth of three.

The += arg operation appends arg to the end of the list. In this case, arg may be either a single element or another ListAccum.

ListAccum supports two additional operations:

  • @list1 + @list2 creates a new ListAccum, which contains the elements of @list1 followed by the elements of @list2. The two ListAccums must have identical data types.

Change in "+" definition

The pre-v2.0 definition of the ListAccum "+" operator ( @list + arg : Add arg to each member of @list) is no longer supported.

  • @list1 * @list2 (STRING data only) generates a new list of strings consisting of all permutations of an element of the first list followed by an element of the second list.

ListAccum also supports the following class functions.

Functions which modify the ListAccum (mutator functions) can be used only under the following conditions:

  • Mutator functions of global accumulators may only be used at the query-body level.

  • Mutator functions of vertex-attached accumulators may only be used in a POST-ACCUM clause.

function (T is the element type)

return type

Accessor / Mutator

description

size()

INT

Accessor

Returns the number of elements in the list.

contains( T val )

BOOL

Accessor

Returns true/false if the list does/doesn't contain the value .

get( INT idx )

T

Accessor

Returns the value at the given index position in the list. The index begins at 0. If the index is out of bound (including any negative value), the default value of the element type is returned.

clear()

VOID

Mutator

Clears the list so it becomes empty with size 0.

update (INT index, T value )

VOID

Mutator

Assigns value to the list element at position index .

ListAccum Example
# ListAccum Example
CREATE QUERY listAccumEx() FOR GRAPH minimalNet {
ListAccum<INT> @@intListAccum;
ListAccum<STRING> @@stringListAccum;
ListAccum<STRING> @@stringMultiplyListAccum;
ListAccum<STRING> @@stringAdditionAccum;
ListAccum<STRING> @@letterListAccum;
ListAccum<ListAccum<STRING>> @@nestedListAccum;
@@intListAccum = [1,3,5];
@@intListAccum += [7,9];
@@intListAccum += 11;
@@intListAccum += 13;
@@intListAccum += 15;
PRINT @@intListAccum;
PRINT @@intListAccum.get(0), @@intListAccum.get(1);
PRINT @@intListAccum.get(8); # Out of bound: default value of int: 0
#Other built-in functions
PRINT @@intListAccum.size();
PRINT @@intListAccum.contains(2);
PRINT @@intListAccum.contains(3);
@@stringListAccum += "Hello";
@@stringListAccum += "World";
PRINT @@stringListAccum; // ["Hello","World"]
@@letterListAccum += "a";
@@letterListAccum += "b";
# ListA + ListB produces a new list equivalent to ListB appended to ListA.
# Ex: [a,b,c] + [d,e,f] => [a,b,c,d,e,f]
@@stringAdditionAccum = @@stringListAccum + @@letterListAccum;
PRINT @@stringAdditionAccum;
#Multiplication produces a list of all list-to-list element combinations (STRING TYPE ONLY)
# Ex: [a,b] * [c,d] = [ac, ad, bc, bd]
@@stringMultiplyListAccum = @@stringListAccum * @@letterListAccum;
PRINT @@stringMultiplyListAccum;
#Two dimensional list (3 dimensions is possible as well)
@@nestedListAccum += [["foo", "bar"], ["Big", "Bang", "Theory"], ["String", "Theory"]];
PRINT @@nestedListAccum;
PRINT @@nestedListAccum.get(0);
PRINT @@nestedListAccum.get(0).get(1);
}
listAccumEx.json Result
GSQL > RUN QUERY listAccumEx()
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [ {"@@intListAccum": [ 1, 3, 5, 7, 9, 11, 13, 15 ]},
{
"@@intListAccum.get(0)": 1,
"@@intListAccum.get(1)": 3
},
{"@@intListAccum.get(8)": 0},
{"@@intListAccum.size()": 8},
{"@@intListAccum.contains(2)": false},
{"@@intListAccum.contains(3)": true},
{"@@stringListAccum": [ "Hello", "World" ]},
{"@@stringAdditionAccum": [ "Hello", "World", "a", "b"]},
{"@@stringMultiplyListAccum": [ "Helloa", "Worlda", "Hellob", "Worldb" ]},
{"@@nestedListAccum": [
[ "foo", "bar" ],
[ "Big", "Bang", "Theory" ],
[ "String", "Theory" ]
]},
{"@@nestedListAccum.get(0)": [ "foo", "bar" ]},
{"@@nestedListAccum.get(0).get(1)": "bar"}
]
}
Example for update function on a global ListAccum
CREATE QUERY listAccumUpdateEx() FOR GRAPH workNet {
# Global ListAccum
ListAccum<INT> @@intListAccum;
ListAccum<STRING> @@stringListAccum;
ListAccum<BOOL> @@passFail;
@@intListAccum += [0,2,4,6,8];
@@stringListAccum += ["apple","banana","carrot","daikon"];
# Global update at Query-Body Level
@@passFail += @@intListAccum.update(1,-99);
@@passFail += @@intListAccum.update(@@intListAccum.size()-1,40); // last element
@@passFail += @@stringListAccum.update(0,"zero"); // first element
@@passFail += @@stringListAccum.update(4,"four"); // FAIL: out-of-range
PRINT @@intListAccum, @@stringListAccum, @@passFail;
}
Results in listAcccumUpdateEx.json
GSQL > RUN QUERY listAccumUpdateEx()
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [{
"@@passFail": [ true, true, true, false ],
"@@intListAccum": [ 0, -99, 4, 6, 40 ],
"@@stringListAccum": [ "zero", "banana", "carrot", "daikon" ]
}]
}
Example for update function on a vertex-attached ListAccum
CREATE QUERY listAccumUpdateEx2(SET<VERTEX<person>> seed) FOR GRAPH workNet api("v2") {
# Each person has an LIST<INT> of skills and a LIST<STRING COMPRESS> of interests.
# This function copies their lists into ListAccums, and then udpates the last
# int with -99 and updates the last string with "fizz".
ListAccum<INT> @intList;
ListAccum<STRING COMPRESS> @stringList;
ListAccum<STRING> @@intFails, @@strFails;
S0 (person) = seed;
S1 = SELECT s
FROM S0:s
ACCUM
s.@intList = s.skillList,
s.@stringList = s.interestList
POST-ACCUM
INT len = s.@intList.size(),
IF NOT s.@intList.update(len-1,-99) THEN
@@intFails += s.id END,
INT len2 = s.@stringList.size(),
IF NOT s.@stringList.update(len2-1,"fizz") THEN
@@strFails += s.id END
;
PRINT S1[S1.skillList, S1.interestList, S1.@intList, S1.@stringList]; // api v2
PRINT @@intFails, @@strFails;
}
Results for listAccumUpdateEx2
GSQL > RUN QUERY listAccumUpdateEx2(["person1","person5"])
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [
{"S1": [
{
"v_id": "person1",
"attributes": {
"S1.@stringList": [ "management","fizz" ],
"S1.interestList": [ "management", "financial"],
"S1.skillList": [ 1, 2, 3 ],
"S1.@intList": [ 1, 2, -99 ]
},
"v_type": "person"
},
{
"v_id": "person5",
"attributes": {
"S1.@stringList": [ "sport", "financial", "fizz" ],
"S1.interestList": [ "sport", "financial", "engineering" ],
"S1.skillList": [ 8, 2, 5 ],
"S1.@intList": [ 8, 2, -99 ]
},
"v_type": "person"
}
]},
{
"@@strFails": [],
"@@intFails": []
}
]
}

SetAccum

The SetAccum type maintains a collection of unique elements. The output of a SetAccum is a list of elements in arbitrary order. A SetAccum instance can contain values of one type. The element type can be any base type, tuple, or STRING COMPRESS.

For SetAccum, the += arg operation adds a non-duplicate element or set of elements to the set. If an element is already represented in the set, then the SetAccum state does not change.

SetAccum also can be used with the three canonical set operators: UNION, INTERSECT, and MINUS (see Section "Set/Bag Expression and Operators" for more details).

SetAccum also supports the following class functions.

Functions which modify the SetAccum (mutator functions) can be used only under the following conditions:

  • Mutator functions of global accumulators may only be used at the query-body level.

  • Mutator functions of vertex-attached accumulators may only be used in a POST-ACCUM clause.

function (T is the element type)

return type

Accessor / Mutator

description

size()

INT

Accessor

Returns the number of elements in the set.

contains( T value )

BOOL

Accessor

Returns true/false if the set does/doesn't contain the value .

remove( T value )

VOID

Mutator

Removes value from the set.

clear()

VOID

Mutator

Clears the set so it becomes empty with size 0.

SetAccum Example
# SetAccum Example
CREATE QUERY setAccumEx() FOR GRAPH minimalNet {
SetAccum<INT> @@intSetAccum;
SetAccum<STRING> @@stringSetAccum;
@@intSetAccum += 5;
@@intSetAccum.clear();
@@intSetAccum += 4;
@@intSetAccum += 11;
@@intSetAccum += 1;
@@intSetAccum += 11; # Sets do not store duplicates
@@intSetAccum += (1,2,3,4); # Can create simple sets this way
PRINT @@intSetAccum;
@@intSetAccum.remove(2);
PRINT @@intSetAccum AS RemovedVal2; # Demostrate remove.
PRINT @@intSetAccum.contains(3);
@@stringSetAccum += "Hello";
@@stringSetAccum += "Hello";
@@stringSetAccum += "There";
@@stringSetAccum += "World";
PRINT @@stringSetAccum;
PRINT @@stringSetAccum.contains("Hello");
PRINT @@stringSetAccum.size();
}
setAccumEx.json Result
GSQL > RUN QUERY setAccumEx()
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [ {"@@intSetAccum": [ 3, 2, 1, 11, 4 ]},
{"@@intSetAccum.contains(3)": true},
{"@@stringSetAccum": [ "World", "There", "Hello" ]},
{"@@stringSetAccum.contains(Hello)": true},
{"@@stringSetAccum.size()": 3}
]
}

BagAccum

The BagAccum type maintains a collection of elements with duplicated elements allowed. The output of a BagAccum is a list of elements in arbitrary order. A BagAccum instance can contain values of one type. The element type can be any base type, tuple, or STRING COMPRESS.

For BagAccum, the += arg operation adds an element or bag of elements to the bag.

BagAccum also supports the + operator:

  • @bag1 + @bag2 creates a new BagAccum, which contains the elements of @bag1 and the elements of @bag2. The two BagAccums must have identical data types.

BagAccum also supports the following class functions.

Functions which modify the BagAccum (mutator functions) can be used only under the following conditions:

  • Mutator functions of global accumulators may only be used at the query-body level.

  • Mutator functions of vertex-attached accumulators may only be used in a POST-ACCUM clause.

function (T is the element type)

return type

Accessor / Mutator

description

size()

INT

Accessor

Returns the number of elements in the bag.

contains( T value )

BOOL

Accessor

Returns true/false if the bag does/doesn't contain the value .

clear()

VOID

Mutator

Clears the bag so it becomes empty with size 0.

remove( T value )

VOID

Mutator

Removes one instance of value from the bag.

removeAll( T value )

VOID

Mutator

Removes all instances of the given value from the bag.

BagAccum Example
# BagAccum Example
CREATE QUERY bagAccumEx() FOR GRAPH minimalNet {
#Unordered collection
BagAccum<INT> @@intBagAccum;
BagAccum<STRING> @@stringBagAccum;
@@intBagAccum += 5;
@@intBagAccum.clear();
@@intBagAccum += 4;
@@intBagAccum += 11;
@@intBagAccum += 1;
@@intBagAccum += 11; #Bag accums can store duplicates
@@intBagAccum += (1,2,3,4);
PRINT @@intBagAccum;
PRINT @@intBagAccum.size();
PRINT @@intBagAccum.contains(4);
@@stringBagAccum += "Hello";
@@stringBagAccum += "Hello";
@@stringBagAccum += "There";
@@stringBagAccum += "World";
PRINT @@stringBagAccum.contains("Hello");
@@stringBagAccum.remove("Hello"); #Remove one matching element
@@stringBagAccum.removeAll("There"); #Remove all matching elements
PRINT @@stringBagAccum;
}
bagAccumEx.json Result
GSQL > RUN QUERY bagAccumEx()
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [ {"@@intBagAccum": [ 2, 3, 1, 1, 11, 11, 4, 4 ]},
{"@@intBagAccum.size()": 8},
{"@@intBagAccum.contains(4)": true},
{"@@stringBagAccum.contains(Hello)": true},
{"@@stringBagAccum": [ "World", "Hello" ]}
]
}

MapAccum

The MapAccum type maintains a collection of (key -> value) pairs. The output of a MapAccum is a set of key and value pairs in which the keys are unique.

The key type of a MapAccum can be all base types or tuples. If the key type is VERTEX, then only the vertex's id is stored and displayed.

The value type of a MapAccum can be all base types, tuples, or any type of accumulator, except for HeapAccum.

For MapAccum, the += (key->val) operation adds a key-value element to the collection if key is not yet used in the MapAccum. If the MapAccum already contains key , then val is accumulated to the current value, where the accumulation operation depends on the data type of val . (Strings would get concatenated, lists would be appended, numerical values would be added, etc.)

MapAccum also supports the + operator:

  • @map1 + @map2 creates a new MapAccum, which contains the (key,value) pairs of @map2 added to the (key,value) pairs of @map1. The two MapAccums must have identical data types.

MapAccum also supports the following class functions.

Functions which modify the MapAccum (mutator functions) can be used only under the following conditions:

  • Mutator functions of global accumulators may only be used at the query-body level.

  • Mutator functions of vertex-attached accumulators may only be used in a POST-ACCUM clause.

function (KEY is the key type)

return type

Accessor / Mutator

description

size()

INT

Accessor

Returns the number of elements in the map.

containsKey( KEY key )

BOOL

Accessor

Returns true/false if the map does/doesn't contain key .

get( KEY key )

value type

Accessor

Returns the value which the map associates with key . If the map doesn't containkey , then the return value is undefined.

clear()

VOID

Mutator

Clears the map so it becomes empty with size 0.

MapAccum Example
#MapAccum Example
CREATE QUERY mapAccumEx() FOR GRAPH minimalNet {
#Map(Key, Value)
# Keys can be INT or STRING only
MapAccum<STRING, INT> @@intMapAccum;
MapAccum<INT, STRING> @@stringMapAccum;
MapAccum<INT, MapAccum<STRING, STRING>> @@nestedMapAccum;
@@intMapAccum += ("foo" -> 1);
@@intMapAccum.clear();
@@intMapAccum += ("foo" -> 3);
@@intMapAccum += ("bar" -> 2);
@@intMapAccum += ("baz" -> 2);
@@intMapAccum += ("baz" -> 1); #add 1 to existing value
PRINT @@intMapAccum.containsKey("baz");
PRINT @@intMapAccum.get("bar");
PRINT @@intMapAccum.get("root");
@@stringMapAccum += (1 -> "apple");
@@stringMapAccum += (2 -> "pear");
@@stringMapAccum += (3 -> "banana");
@@stringMapAccum += (4 -> "a");
@@stringMapAccum += (4 -> "b"); #append "b" to existing value
@@stringMapAccum += (4 -> "c"); #append "c" to existing value
PRINT @@intMapAccum;
PRINT @@stringMapAccum;
#Checking and getting keys
if @@stringMapAccum.containsKey(1) THEN
PRINT @@stringMapAccum.get(1);
END;
#Map nesting
@@nestedMapAccum += ( 1 -> ("foo" -> "bar") );
@@nestedMapAccum += ( 1 -> ("flip" -> "top") );
@@nestedMapAccum += ( 2 -> ("fizz" -> "pop") );
@@nestedMapAccum += ( 1 -> ("foo" -> "s") );
PRINT @@nestedMapAccum;
if @@nestedMapAccum.containsKey(1) THEN
if @@nestedMapAccum.get(1).containsKey("foo") THEN
PRINT @@nestedMapAccum.get(1).get("foo");
END;
END;
}
mapAccumEx.json Result
GSQL > RUN QUERY mapAccumEx()
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [
{"@@intMapAccum.containsKey(baz)": true},
{"@@intMapAccum.get(bar)": 2},
{"@@intMapAccum.get(root)": 0},
{"@@intMapAccum": {
"bar": 2,
"foo": 3,
"baz": 3
}},
{"@@stringMapAccum": {
"1": "apple",
"2": "pear",
"3": "banana",
"4": "abc"
}},
{"@@stringMapAccum.get(1)": "apple"},
{"@@nestedMapAccum": {
"1": {
"foo": "bars",
"flip": "top"
},
"2": {"fizz": "pop"}
}},
{"@@nestedMapAccum.get(1).get(foo)": "bars"}
]
}

ArrayAccum

The ArrayAccum type maintains an array of accumulators. An array is a fixed-length sequence of elements, with direct access to elements by position. The ArrayAccum has these particular characteristics:

  • The elements are accumulators, not primitive or base data types. All accumulators, except HeapAccum, MapAccum, and GroupByAccum, can be used.

  • An ArrayAccum instance can be multidimensional. There is no limit to the number of dimensions.

  • The size can be set at run-time (dynamically).

  • There are operators which update the entire array efficiently.

When an ArrayAccum is declared, the instance name should be followed by a pair of brackets for each dimension. The brackets may either contain an integer constant to set the size of the array, or they may be empty. In that case, the size must be set with the reallocate function before the ArrayAccum can be used.

ArrayAccum declaration example
ArrayAccum<SetAccum<STRING>> @@names[10];
ArrayAccum<SetAccum<INT>> @@ids[][]; // 2-dimensional, size to be determined

Because each element of an ArrayAccum itself is an accumulator, the operators =, +=, and + can be used in two contexts: accumulator-level and element-level.

Element-level operations

If @A is an ArrayAccum of length 6, then @A[0] and @A[5] refer to its first and last elements, respectively. Referring to an ArrayAccum element is like referring to an accumulator of that type. For example, given the following definitions:

ArrayAccum<SumAccum<INT>> @@Sums[3];
ArrayAccum<ListAccum<STRING>> @@Lists[2];

then @@Sums[0], @@Sums[1], and @@Sums[2] each refer to an individual SumAccum<INT>, and @@Lists[0] and @@Lists[1] each refer to a ListAccum<STRING>, supporting all the operations for those accumulator and data types.

@@Sums[1] = 1;
@@Sums[1] += 2; // value is now 3
@@Lists[0] = "cat";
@@Lists[0] += "egory"; // value is now "category"

Accumulator-level operations

The operators =, +=, and + have special meanings when applied to an ArrayAccum as a whole. There operations efficiently update an entire ArrayAccum. All of the ArrayAccums must have the same element type.

Operator

Description

Example

=

sets the ArrayAccum on the left equal to the ArrayAccum on the right. The two ArrayAccums must have the same element type, but the left-side ArrayAccum will change its size and dimensions to match the one on the right-side.

@A = @B;

+

performs element-by-element addition of two ArrayAccums of the same type and size. The result is a new ArrayAccum of the same size.

@C = @A + @B; // @A and @B must be the same size

+=

performs element-by-element accumulation (+=) from the right-side ArrayAccum to the left-side ArrayAccum. They must be the same type and size.

@A += @B; // @A and @B must be the same size

ArrayAccum also supports the following class functions.

Functions which modify the ArrayAccum (mutator functions) can be used only under the following conditions:

  • Mutator functions of global accumulators may only be used at the query-body level.

  • Mutator functions of vertex-attached accumulators may only be used in a POST-ACCUM clause.

function

return type

Accessor / Mutator

description

size()

INT

Accessor

Returns the total number of elements in the (multi-dimensional) array. For example, the size of an ArrayAccum declared as @A[3][4] is 12.

reallocate( INT, ... )

VOID

Mutator

Discards the previous ArrayAccum instance and creates a new ArrayAccum, with the size(s) given. An N-dimensional ArrayAccum requires N integer parameters. The reallocate function cannot be used to change the number of dimensions.

Example of ArrayAccum Element-level Operations
CREATE QUERY ArrayAccumElem() FOR GRAPH minimalNet {
ArrayAccum<SumAccum<DOUBLE>> @@aaSumD[2][2]; # 2D Sum Double
ArrayAccum<SumAccum<STRING>> @@aaSumS[2][2]; # 2D Sum String
ArrayAccum<MaxAccum<INT>> @@aaMax[2];
ArrayAccum<MinAccum<UINT>> @@aaMin[2];
ArrayAccum<AvgAccum> @@aaAvg[2];
ArrayAccum<AndAccum<BOOL>> @@aaAnd[2];
ArrayAccum<OrAccum<BOOL>> @@aaOr[2];
ArrayAccum<BitwiseAndAccum> @@aaBitAnd[2];
ArrayAccum<BitwiseOrAccum> @@aaBitOr[2];
ArrayAccum<ListAccum<INT>> @@aaList[2][2]; # 2D List
ArrayAccum<SetAccum<FLOAT>> @@aaSetF[2];
ArrayAccum<BagAccum<DATETIME>> @@aaBagT[2];
## for test data
ListAccum<STRING> @@words;
BOOL toggle = false;
@@words += "1st"; @@words += "2nd"; @@words += "3rd"; @@words += "4th";
# Int: a[0] += 1, 2; a[1] += 3, 4
# Bool: alternate true/false
# Float: a[0] += 1.111, 2.222; a[1] += 3.333, 4.444
# 2D Doub: a[0][0] += 1.111, 2.222; a[0][1] += 5.555, 6.666;
# a[1][0] += 3.333, 4.444; a[0][1] += 7.777, 8.888;
FOREACH i IN RANGE [0,1] DO
FOREACH n IN RANGE [1, 2] DO
toggle = NOT toggle;
@@aaMax[i] += i*2 + n;
@@aaMin[i] += i*2 + n;
@@aaAvg[i] += i*2 + n;
@@aaAnd[i] += toggle;
@@aaOr[i] += toggle;
@@aaBitAnd[i] += i*2 + n;
@@aaBitOr[i] += i*2 + n;
@@aaSetF[i] += (i*2 + n)/0.9;
@@aaBagT[i] += epoch_to_datetime(i*2 + n);
FOREACH j IN RANGE [0,1] DO
@@aaSumD[i][j] += (j*4 + i*2 + n)/0.9;
@@aaSumS[i][j] += @@words.get((j*2 + i + n)%4);
@@aaList[i][j] += j*4 +i*2 + n ;
END;
END;
END;
PRINT @@aaSumD; PRINT @@aaSumS;
PRINT @@aaMax; PRINT @@aaMin; PRINT @@aaAvg;
PRINT @@aaAnd; PRINT @@aaOr;
PRINT @@aaBitAnd; PRINT @@aaBitOr;
PRINT @@aaList; PRINT @@aaSetF; PRINT @@aaBagT;
}
ArrayAccumElem.json Results
GSQL > RUN QUERY ArrayAccumElem()
{
"error": false,
"message": "",
"version": {
"edition": "developer",
"schema": 0,
"api": "v2"
},
"results": [
{"@@aaSumD": [
[ 3.33333, 12.22222 ],
[ 7.77778, 16.66667 ]
]},
{"@@aaSumS": [
[ "2nd3rd", "4th1st" ],
[ "3rd4th", "1st2nd" ]
]},
{"@@aaMax": [ 2, 4 ]},
{"@@aaMin": [ 1, 3 ]},
{"@@aaAvg": [ 1.5, 3.5 ]},
{"@@aaAnd": [ false, false