Variable manipulation¶
You can use the SET statement to perform assignments, aggregations, and function calls on variables.
The following table summarizes the supported operations and their operands:
| Operation | Operand |
|---|---|
| Assignment | Primitive value variables, aggregator value variables, and active set variables |
| Aggregation | Aggregator value variables |
| Function call | Member functions of aggregator value variables and table variables |
Syntax¶
- assignment_statement
- aggregation_statement
- function_call_statement
assignment_statement ::=
'SET' ( primitive_value_variable | aggregator_value_variable | active_set_variable ) '=' value_expression
aggregation_statement ::=
'SET' aggregator_value_variable '+=' value_expression
function_call_statement ::=
'SET' (aggregator_value_variable | table_variable) '.' member_function '(' ( argument (',' argument)* )? ')'
Parameters¶
primitive_value_variable¶
The primitive value variable to which the value is assigned. The variable must be defined before it is used. For how to define primitive value variables, see Primitive value variables.
aggregator_value_variable¶
The aggregator value variable to which the value is assigned or aggregated. The variable must be defined before it is used. For how to define aggregator value variables, see Aggregator value variables.
When used in aggregation statements, node-attached aggregator value variables can be referenced in the following two syntax forms:
node_variable.@aggregator_value_variable_name, wherenode_variableis a predefined variable that represents a node in the temporary graph.NODE(primitive_value_variable).@aggregator_value_variable_name, whereprimitive_value_variableis a primitive value variable of typeINTthat holds the system ID of a node in the temporary graph.
When used in other statements, node-attached aggregator value variables only support the first syntax form.
active_set_variable¶
The active set variable to which the value is assigned. The variable must be defined before it is used. For how to define active set variables, see Active set variables.
value_expression¶
A value expression that evaluates to a value or constant, which is to be assigned or aggregated to the variable. For more information, see Value expressions.
The data type of the value or constant must be compatible with that of the value held by the variable.
table_variable¶
The table variable to which the value is set. The variable must be defined before it is used. For how to define table variables, see Table variables.
member_function and argument¶
The member function to be called on the aggregator value variable and table variable, and their arguments.
The following table lists the member functions supported by different types of value variables:
| Value variable type | Supported member functions |
|---|---|
| ListAgg | size() and clear() |
| SetAgg | size(), clear(), and contains_key() |
| MapAgg | size(), clear(), contains_key(), and get() |
| TopKAgg | size(), clear(), and min() |
| Table variable | clear() |
For more information about the member functions and their arguments of aggregator value variables, see the corresponding aggregator value variable document.
Examples¶
The following examples are based on the temporary graph #movie_temporary_graph created in CREATE TEMPORARY GRAPH.
Assignment examples¶
Assign values to primitive value variables¶
'''
USE #movie_temporary_graph {
VALUE a INT = 1
VALUE b INT = 1
VALUE c INT = 1
SET a = a + 1
SET b = a + 2
SET c = b + 3
RETURN a, b, c
}
'''
Return:
+---+---+---+
| a | b | c |
+---+---+---+
| 2 | 4 | 7 |
+---+---+---+
Assign values to global aggregator value variables¶
'''
USE #movie_temporary_graph {
VALUE a SumAgg<INT> = 0
VALUE b MaxAgg<DOUBLE> = 0.0
VALUE c OrAgg = false
SET @a = 10
SET @b = 20.5
SET @c = true
RETURN @a, @b, @c
}
'''
Return:
+----+------+------+
| @a | @b | @c |
+----+------+------+
| 10 | 20.5 | true |
+----+------+------+
Assign values to node-attached aggregator value variables¶
'''
USE #movie_temporary_graph {
NODE VALUE a ListAgg<INT>
MATCH (v@Actor) WHERE v.id > 271940
PER NODE (v) {
SET v.@a = LIST[v.id]
LOG_INFO("ID_List: ", v.@a)
}
}
'''
Return:
Log:
2025-08-06T07:02:23.215245 INFO - ID_List: [271947]
2025-08-06T07:02:23.215245 INFO - ID_List: [271944]
2025-08-06T07:02:23.215245 INFO - ID_List: [282659]
2025-08-06T07:02:23.215245 INFO - ID_List: [271943]
2025-08-06T07:02:23.215245 INFO - ID_List: [271946]
2025-08-06T07:02:23.215354 INFO - ID_List: [271945]
2025-08-06T07:02:23.215354 INFO - ID_List: [271942]
2025-08-06T07:02:23.215392 INFO - ID_List: [271941]
Assign values to active set variables¶
'''
USE #movie_temporary_graph {
VALUE active_set ACTIVE_SET
VALUE list_id ListAgg<INT64>
MATCH (a@Actor) WHERE a.id IN [31, 8252]
PER NODE (a) {
// Accumulate the unique identifiers (UIDs) of matched nodes into a list
SET @list_id += element_id(a)
}
// Assign the list of node UIDs to the active set variable
SET active_set = @list_id
MATCH (a)
WHERE a IN active_set
PER NODE (a) {
LOG_INFO("Actor Name: ", a.name)
}
}
'''
Return:
Log:
2025-09-16T09:34:40.697808 INFO - Actor Name: Tom Hanks
2025-09-16T09:34:40.697808 INFO - Actor Name: William Holden
Aggregation examples¶
Aggregate values into global aggregator value variables¶
'''
USE #movie_temporary_graph {
VALUE global_avg AvgAgg<INT>
SET @global_avg += 3 // Average: 3.0 (Total: 3, Count: 1)
SET @global_avg += 7 // Average: 5.0 (Total: 10, Count: 2)
SET @global_avg += 5 // Average: 5.0 (Total: 15, Count: 3)
LOG_INFO("@global_avg: ",@global_avg)
}
'''
Return:
Log:
2025-09-16T09:36:54.696143 INFO - @global_avg: 5
Aggregate values into node-attached aggregator value variables¶
'''
USE #movie_temporary_graph {
NODE VALUE node_avg AvgAgg<DOUBLE>
MATCH (a@User WHERE a.id IN [1,3,4])-[w@Watch]->(b@Movie)
PER PATH {
// Accumulate user ratings for movies watched and calculate the average
SET a.@node_avg += w.rate
}
PER NODE (a) {
LOG_INFO("a.id: ",a.id, " a.@node_avg: ",a.@node_avg)
}
}
'''
Return:
Log:
2025-09-16T09:37:42.145639 INFO - a.id: 3 a.@node_avg: 3.75
2025-09-16T09:37:42.145639 INFO - a.id: 1 a.@node_avg: 4.77778
2025-09-16T09:37:42.145639 INFO - a.id: 4 a.@node_avg: 2.66667
Function call examples¶
Call clear()¶
'''
USE #movie_temporary_graph {
VALUE global_set_1 SetAgg<INT>
TABLE result_table { id, name } = (10, "new1"), (20, "new2")
SET @global_set_1 += 3 // set: {3}
SET @global_set_1 += 7 // set: {3, 7}
SET @global_set_1 += 3 // set: {3, 7} (duplicate ignored)
SET @global_set_1 += 5 // set: {3, 7, 5}
LOG_INFO("@global_set_1: ", @global_set_1)
SET @global_set_1.clear() // set: {}
LOG_INFO("@global_set_1: ", @global_set_1)
SET result_table.clear() // clear the table
FOR rec IN result_table
RETURN rec.id, rec.name
}
'''
Return:
+--------+----------+
| rec.id | rec.name |
+--------+----------+
+--------+----------+
Log:
2026-01-16T06:53:14.956605 INFO - @global_set_1: [5,7,3]
2026-01-16T06:53:14.957146 INFO - @global_set_1: []