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:
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.
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 "@".
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 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 companiesCREATE QUERY countEmploymentRelationships() FOR GRAPH workNet {INT localRelationshipCount; SumAccum<INT> @@globalRelationshipCount;start= {company.*}; companies =SELECT s FROMstart:s -(worksFor)-> :t ACCUM @@globalRelationshipCount +=1, localRelationshipCount = localRelationshipCount +1;PRINT localRelationshipCount;PRINT @@globalRelationshipCount;}
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.
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 .
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
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.
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 numeric value. MinAccum and MaxAccum variables operate on values of type INT, UINT, FLOAT, and DOUBLE, 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 operating on VERTEX type have a special comparison. They do not compare vertex ids, but TigerGraph internal ids, which might n ot be in t he same order as the external ids. Comparing internal ids is much faster, so MinAccum/ MaxAccum<VERTEX> provide an efficient way to compar e 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.
MaxAccum<VERTEX> example
# Output one random post vertex from each personCREATE QUERY minMaxAccumVertex() FOR GRAPH socialNet api("v2") { MaxAccum<VERTEX> @maxVertex; allUser = {person.*}; allUser =SELECT src FROM allUser:src -(posted)-> post:tgt ACCUM src.@maxVertex += tgtORDER BY src.id;PRINT allUser[allUser.@maxVertex]; // api v2}
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.
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 ExampleCREATE QUERY andOrAccumEx() FOR GRAPH minimalNet {# T = True# F = False AndAccum @@andAccumVar; # (defaultvalue= T) OrAccum @@orAccumVar; # (defaultvalue= F) @@andAccumVar += True; # T and T = T @@andAccumVar += False; # T and F = F @@andAccumVar += True; # F and T = FPRINT @@andAccumVar; @@orAccumVar += False; # F or F == F @@orAccumVar += True; # F or T == T @@orAccumVar += False; # T or F == TPRINT @@orAccumVar;}
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.
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.
ListAccum Example
# ListAccum ExampleCREATE 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: defaultvalue of int: 0#Other built-in functionsPRINT @@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);}
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, andthen udpates the last# intwith-99and updates the last string with"fizz". ListAccum<INT> @intList; ListAccum<STRING COMPRESS> @stringList; ListAccum<STRING> @@intFails, @@strFails; S0 (person) = seed; S1 =SELECT sFROM S0:s ACCUM s.@intList = s.skillList, s.@stringList = s.interestList POST-ACCUMINT len = s.@intList.size(),IFNOT s.@intList.update(len-1,-99) THEN @@intFails += s.id END,INT len2 = s.@stringList.size(),IFNOT s.@stringList.update(len2-1,"fizz") THEN @@strFails += s.id END ;PRINT S1[S1.skillList, S1.interestList, S1.@intList, S1.@stringList]; // api v2PRINT @@intFails, @@strFails;}
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.
SetAccum Example
# SetAccum ExampleCREATE 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 createsimplesets this wayPRINT @@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();}
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.
BagAccum Example
# BagAccum ExampleCREATE 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 elementsPRINT @@stringBagAccum;}
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.
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, sizeto 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:
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.
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.
CREATE QUERY arrayAccumLocal() FOR GRAPH socialNet api("v2") {# Count each person's edges by type # friend/liked/posted edges are type 0/1/2, respectively ArrayAccum<SumAccum<INT>> @edgesByType[3]; Persons = {person.*}; Persons = SELECT s FROM Persons:s -(:e)-> :t ACCUM CASE e.type WHEN "friend" THEN s.@edgesByType[0] += 1 WHEN "liked" THEN s.@edgesByType[1] += 1 WHEN "posted" THEN s.@edgesByType[2] += 1 END ORDER BY s.id; #PRINT Persons.@edgesByType; // api v1 PRINT Persons[Persons.@edgesByType]; // api v2}
The HeapAccum type maintains a sorted collection of tuples and enforces a maximum number of tuples in the collection. The output of a HeapAccum is a sorted collection of tuple elements. The += arg operation adds a tuple to the collection in sorted order. If the HeapAccum is already at maximum capacity when the += operator is applied, then the tuple which is last in the sorted order is dropped from the HeapAccum. Sorting of tuples is performed on one or more defined tuple fields ordered either ascending or descending. Sorting precedence is performed based on defined tuple fields from left to right.
The declaration of a HeapAccum is more complex than for most other accumulators, because the user must define a custom tuple type, set the maximum capacity of the HeapAccum, and specify how the HeapAccum should be sorted. The declaration syntax is outlined in the figure below:
First, the HeapAccum declaration must be preceded by a TYPEDEF statement which defines the tuple type. At least one of the fields (field_1, ..., field_n) must be of a data type that can be sorted.
In the declaration of the HeapAccum itself, the keyword "HeapAccum" is followed by the tuple type in angle brackets < >. This is followed by a parenthesized list of two or more parameters. The first parameter is the maximum number of tuples that the HeapAccum may store. This parameter must be a positive integer. The subsequent parameters are a subset of the tuple's field, which are used as sort keys. The sort key hierarchy is from left to right, with the leftmost key being the primary sort key. The keywords ASC and DESC indicate Ascending (lowest value first) or Descending (highest value first) sort order. Ascending order is the default.
HeapAccum also supports the following class functions.
Functions which modify the HeapAccum (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.
HeapAccum Example
#HeapAccum ExampleCREATE QUERY heapAccumEx() FOR GRAPH minimalNet { TYPEDEF tuple<STRING firstName, STRING lastName, INT score> testResults;#Heap with max size of 4 sorted decending by score then ascending lastname HeapAccum<testResults>(4, score DESC, lastName ASC) @@topTestResults;PRINT @@topTestResults.top(); @@topTestResults += testResults("Bruce", "Wayne", 80); @@topTestResults += testResults("Peter", "Parker", 80); @@topTestResults += testResults("Tony", "Stark", 100); @@topTestResults += testResults("Bruce", "Banner", 95); @@topTestResults += testResults("Jean", "Summers", 95); @@topTestResults += testResults("Clark", "Kent", 80);#Show element with the highest sorted positionPRINT @@topTestResults.top();PRINT @@topTestResults.top().firstName, @@topTestResults.top().lastName, @@topTestResults.top().score;PRINT @@topTestResults;#Increase the size of the heap toadd more elements @@topTestResults.resize(5);#Find the size of the current heapPRINT @@topTestResults.size(); @@topTestResults += testResults("Bruce", "Wayne", 80); @@topTestResults += testResults("Peter", "Parker", 80); PRINT @@topTestResults; #Resizing smaller WILL REMOVE excess elementsfrom the HeapAccum @@topTestResults.resize(3);PRINT @@topTestResults;#Increasing capacity will notrestore dropped elements @@topTestResults.resize(5);PRINT @@topTestResults;#Removes all elementsfrom the HeapAccum @@topTestResults.clear(); PRINT @@topTestResults.size();}
The GroupByAccum is compound accumulator, an accumulator of accumulators. At the top level, it is a MapAccum where both the key and the value can have multiple fields. Moreover, each of the value fields is an accumulator type.
In the EBNF above, the type terms form the key set, and the accumType terms form the map's value. Since they are accumulators, they perform a grouping. Like a MapAccum, if we try to store a (key->value) whose key has already been used, then the new value will accumulate to the data which is already stored. In this case, each field of the multiple-field value has its own accumulation function. One way to think about GroupByAccum is that each unique key is a group ID.
In GroupByAccum, the key types can be base type, tuple, or STRING COMPRESS. The accumulators are used for aggregating group values. Each accumulator type can be any type except HeapAccum. Each base type and each accumulator type must be followed an alias. Below is an example declaration.
GroupByAccum<INT a, STRING b, MaxAccum<INT> maxa, ListAccum<ListAccum<INT>> lists> @@group;
To add new data to this GroupByAccum, the data should be formatted as (key1, key2 -> value1, value2) .
GroupByAccum also supports the following class functions.
Functions which modify the GroupByAccum (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.
GroupByAccum Example
#GroupByAccum ExampleCREATE QUERY groupByAccumEx () FOR GRAPH socialNet {## declaration, first two primitive type are group by keys; the rest accumulator type are aggregates GroupByAccum<INT a, STRING b, MaxAccum<INT> maxa, ListAccum<ListAccum<INT>> lists> @@group; GroupByAccum<STRING gender, MapAccum<VERTEX<person>, DATETIME> m> @@group2;# nested GroupByAccum GroupByAccum<INT a, MaxAccum<INT> maxa, GroupByAccum<INT a, MaxAccum<INT> maxa> heap> @@group3;Start= { person.* }; ## usage of global GroupByAccum @@group += (1, "a"->1, [1]); @@group += (1, "a"->2, [2]); @@group += (2, "b"->1, [4]); @@group3 += (2->1, (2->0) ); @@group3 += (2->1, (2->5) ); @@group3 += (2->5, (3->3) );PRINT @@group, @@group.get(1, "a"), @@group.get(1, "a").lists, @@group.containsKey(1, "c"), @@group3;## two kinds of foreach FOREACH g IN @@group DOPRINT g.a, g.b, g.maxa, g.lists;END; FOREACH (g1,g2,g3,g4) IN @@group DOPRINT g1,g2,g3,g4;END; S =SELECT v FROMStart:v - (liked:e) - post:t ACCUM @@group2 += (v.gender -> (v -> e.actionTime));PRINT @@group2, @@group2.get("Male").m, @@group2.get("Female").m; }
Certain collection accumulators may be nested. That is, an accumulator may contain a collection of elements where the elements themselves are accumulators. For example:
ListAccum<ListAccum<INT>> @@matrix; # a 2-dimensional jagged array of integers. Each inner list has its own unique size.
Only ListAccum, ArrayAccum, MapAccum, and GroupByAccum can contain other accumulators. However, not all combinations of collection accumulators are allowed. The following constraints apply:
ListAccum: ListAccum is the only accumulator type which can be nested within ListAccum, up to a depth of 3:
3. GroupByAccum: All accumulator types, except for HeapAccum, can be nested within GroupByAccum as the accumulator type. For example:
GroupByAccum<INT a, STRING b, MaxAccum<INT> maxs, ListAccum<ListAccum<INT>> lists>
4. ArrayAccum: Unlike the other accumulators in this list, where nesting is optional, nesting is mandatory for ArrayAccum. See the ArrayAccum section above.
It is legal to define nested ListAccums to form a multi-dimensional array. Note the declaration statements and the nested [ bracket ] notation in the example below:
CREATE QUERY nestedAccumEx() FOR GRAPH minimalNet {
ListAccum<ListAccum<INT>> @@_2d_list;
ListAccum<ListAccum<ListAccum<INT>>> @@_3d_list;
ListAccum<INT> @@_1d_list;
SumAccum <INT> @@sum = 4;
@@_1d_list += 1;
@@_1d_list += 2;
// add 1D-list to 2D-list as element
@@_2d_list += @@_1d_list;
// add 1D-enum-list to 2D-list as element
@@_2d_list += [@@sum, 5, 6];
// combine 2D-enum-list and 2d-list
@@_2d_list += [[7, 8, 9], [10, 11], [12]];
// add an empty 1D-list
@@_1d_list.clear();
@@_2d_list += @@_1d_list;
// combine two 2D-list
@@_2d_list += @@_2d_list;
PRINT @@_2d_list;
// test 3D-list
@@_3d_list += @@_2d_list;
@@_3d_list += [[7, 8, 9], [10, 11], [12]];
PRINT @@_3d_list;
}
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<VERTEX>
the vertex with internal id 0
The vertex with the greater internal id , either newVal or accumName
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<VERTEX>
unknown
The vertex with the lesser internal id, either newVal or accumName
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) ]
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.
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 .
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.
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.
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.
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
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.
function
return type
Accessor / Mutator
description
size()
INT
Accessor
Returns the number of elements in the heap.
top()
tupleType
Accessor
Returns the top tuple. If this heap is empty, returns a tuple with each element equal to the default value.
pop()
tupleType
Mutator
Returns the top tuple and removes it from the heap. If this heap is empty, returns a tuple with each element equal to the default value.
resize( INT )
VOID
Mutator
Changes the maximum capacity of the heap.
clear()
VOID
Mutator
Clears the heap so it becomes empty with size 0.
function (KEY1..KEYn are the key types)
return type
Accessor / Mutator
description
size()
INT
Accessor
Returns the number of elements in the heap.
get( KEY1 key_value1 , KEY2 key_value2 ... )
element type(s) of the accumulator(s)
Accessor
Returns the values from each accumulator in the group associating with the given key(s). If the key(s) doesn't exist, return the default value(s) of the accumulator type(s).