Source: https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/variable-definition/agg/set_agg/ Documentation version: v5.3.2 # SetAgg A SetAgg aggregator value variable (`SetAgg`) aggregates all inputs into a set that contains only unique values, where `T` represents the data type of the input values. To aggregate a list of input values with duplicates, see [ListAgg](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/variable-definition/agg/list_agg/). ## Declaration syntax diagramsource set_agg_declaration ```` set_agg_declaration ::= 'NODE'? 'VALUE' aggregator_value_variable 'SetAgg' '<' ('INT8'|'INT16'|'INT32'|'INT64'|'FLOAT'|'DOUBLE'|'STRING') '>' ```` For information about the parameters in the declaration syntax, see [Parameters](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/variable-definition/agg/#parameters). ## Initial value As indicated in the declaration syntax, an initial value must not be specified during declaration. ## Input type and supported operations The input type must be `T`, which is specified during declaration. Both [assignment and aggregation](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/set/) operations are supported. ## Return type ```` SetAgg -> LIST ```` The return type is `LIST`, with unique elements of the same type as the type `T` specified during declaration. ## Member functions This section introduces the member functions supported by `SetAgg`. ### size() Returns the number of unique elements in the set held by a SetAgg aggregator value variable. #### Function signature ```` size() -> UINT64 ```` ### clear() Removes all elements from the set held by a SetAgg aggregator value variable. #### Function signature ```` clear() -> VOID ```` ### contains_key() Returns true if the specified value exists in the set held by a SetAgg aggregator value variable; otherwise, it returns false. #### Function signature ```` contains_key(value: T) -> BOOL ```` ## Examples The following examples are based on the temporary graph `#movie_temporary_graph` created in [CREATE TEMPORARY GRAPH](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/ddl/graph-projection/create/). ### Global SetAgg aggregator value variables ```` ''' USE #movie_temporary_graph { VALUE global_set_1 SetAgg VALUE global_set_2 SetAgg 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) // Assignment from empty LIST SET @global_set_1 = [] // Clear the set LOG_INFO("Set from empty LIST:", @global_set_1) // output: [] // Assignment from a LIST SET @global_set_1 = [1, 2, 2, 3, 1, 4] // set: {1, 2, 3, 4} LOG_INFO("Set from LIST assignment: ", @global_set_1) SET @global_set_2 += 3 // global_set_2: {3} SET @global_set_2 += 4 // global_set_2: {3,4} LOG_INFO("@global_set_2: ", @global_set_2) // Assignment from another SetAgg SET @global_set_1 = @global_set_2 // global_set_1: {3,4} LOG_INFO("After assignment from set_2: ", @global_set_1) // Aggregation from another SetAgg SET @global_set_1 += @global_set_2 // global_set_1: {3,4} LOG_INFO("After aggregation from set_2: ", @global_set_1) // Member functions SET @global_set_2.clear() // Clear the set LOG_INFO("Clear the set: ", @global_set_2) // Output: [] LOG_INFO("Size: ", @global_set_1.size()) // Output: 4 LOG_INFO("Contains 4: ", @global_set_1.contains_key(4)) // Output: true LOG_INFO("Contains 100: ", @global_set_1.contains_key(100)) // Output: false } ''' ```` Return: ```` Log: 2025-08-21T02:18:53.175811 INFO - @global_set_1: [5,7,3] 2025-08-21T02:18:53.176426 INFO - Set from empty LIST:[] 2025-08-21T02:18:53.176812 INFO - Set from LIST assignment: [4,3,2,1] 2025-08-21T02:18:53.177273 INFO - @global_set_2: [4,3] 2025-08-21T02:18:53.177556 INFO - After assignment from set_2: [3,4] 2025-08-21T02:18:53.177808 INFO - After aggregation from set_2: [3,4] 2025-08-21T02:18:53.178056 INFO - Clear the set: [] 2025-08-21T02:18:53.178199 INFO - Size: 2 2025-08-21T02:18:53.178400 INFO - Contains 4: true 2025-08-21T02:18:53.178600 INFO - Contains 100: false ```` ### Node-attached SetAgg aggregator value variables ```` ''' USE #movie_temporary_graph { NODE VALUE node_set SetAgg MATCH (a@User WHERE a.id IN [1,3,4])-[w@Watch]->(b@Movie) PER PATH { // For each matched path, add the movie ID to the corresponding user's set SET a.@node_set += b.id } PER NODE (a) { LOG_INFO("a.id: ", a.id, " a.@node_set: ", a.@node_set) } } ''' ```` In the preceding example, a node-attached SetAgg aggregator value variable `node_set` of type `SetAgg` is declared to hold a set of unique movie IDs for each user. Return: ```` Log: 2025-08-12T09:05:22.315257 INFO - a.id: 1 a.@node_set: [10,104,11,497,252,601,100,651,473] 2025-08-12T09:05:22.315257 INFO - a.id: 3 a.@node_set: [152,473] 2025-08-12T09:05:22.315257 INFO - a.id: 4 a.@node_set: [346,99,648,144,637,146,671,10,104,11,252,473] ````