Set operations¶

Syntax¶

This document descriptions the set operations, including UNION, UNION ALL, INTERSECT, and MINUS. To combine multiple queries, use the set operators.

All set operators have equal precedence. If a nGQL statement contains multiple set operators, Nebula Graph evaluates them from the left to right unless parentheses explicitly specify another order.

To use the set operators, always match the return results of the GO clause with the same number and data type.

UNION, UNION DISTINCT, and UNION ALL¶

<left> UNION [DISTINCT | ALL] <right> [ UNION [DISTINCT | ALL] <right> ...]


Operator UNION DISTINCT (or by short UNION) returns the union of two sets A and B without the duplicate elements.

Operator UNION ALL returns the union of two sets A and B with duplicated elements.

The <left> and <right> must have the same number of columns and data types. Different data types are converted according to the Type Conversion.

Example¶

The following statement

nebula> GO FROM "player102" OVER follow \
UNION \
GO FROM "player100" OVER follow;
+-------------+
| follow._dst |
+-------------+
| "player101" |
+-------------+
| "player102" |
+-------------+


returns the neighbors' id of vertex "player102" and "player100 (along with edge follow) without duplication.

While

nebula> GO FROM "player102" OVER follow \
UNION ALL \
GO FROM "player100" OVER follow;
+-------------+
| follow._dst |
+-------------+
| "player101" |
+-------------+
| "player101" |
+-------------+
| "player102" |
+-------------+

returns all the neighbors of vertex "player102" and "player100, with all possible duplications.

UNION can also work with the YIELD statement. For example, let's suppose the results of the following two queries.

nebula> GO FROM "player102" OVER follow YIELD follow._dst AS id, follow.degree AS Degree, $$.player.age AS Age; -- query 1 +-------------+--------+-----+ | id | Degree | Age | +-------------+--------+-----+ | "player101" | 75 | 36 | -- line 1 +-------------+--------+-----+ nebula> GO FROM "player100" OVER follow YIELD follow._dst AS id, follow.degree AS Degree,$$.player.age AS Age;  -- query 2

+-------------+--------+-----+
| id          | Degree | Age |
+-------------+--------+-----+
| "player101" | 96     | 36  |          -- line 2
+-------------+--------+-----+
| "player102" | 90     | 33  |          -- line 3
+-------------+--------+-----+


And the following statement

nebula> GO FROM "player102" OVER follow YIELD follow._dst AS id, follow.degree AS Degree, $$.player.age AS Age \ UNION /* DISTINCT */ \ GO FROM "player100" OVER follow YIELD follow._dst AS id, follow.degree AS Degree,$$.player.age AS Age;


returns the follows:

+-------------+--------+-----+
| id          | Degree | Age |
+-------------+--------+-----+
| "player101" | 75     | 36  |        -- line 1
+-------------+--------+-----+
| "player101" | 96     | 36  |        -- line 2
+-------------+--------+-----+
| "player102" | 90     | 33  |        -- line 3
+-------------+--------+-----+


The DISTINCT check duplication by all the columns for every line. So line 1 and line 2 are different.

INTERSECT¶

<left> INTERSECT <right>


Operator INTERSECT returns the intersection of two sets A and B (denoted by A ⋂ B).

Similar to UNION, the <left> and <right> must have the same number of columns and data types. Only the INTERSECT columns of <left> and <right> are returned.

For example, the following query

nebula> GO FROM "player102" OVER follow YIELD follow._dst AS id, follow.degree AS Degree, $$.player.age AS Age \ INTERSECT \ GO FROM "player100" OVER follow YIELD follow._dst AS id, follow.degree AS Degree,$$.player.age AS Age;


returns

Empty set (time spent 5194/6264 us)


MINUS¶

<left> MINUS <right>


Operator MINUS returns the subtraction (or difference) of two sets A and B (denoted by A - B). Always pay attention to the order of the <left> and <right>. The set A - B consists of elements that are in A but not in B.

For example, the following query

nebula> GO FROM "player100" OVER follow \
MINUS \
GO FROM "player102" OVER follow;


returns

+-------------+
| follow._dst |
+-------------+
| "player102" |
+-------------+


If you reverse the MINUS order, the query

nebula> GO FROM "player102" OVER follow \
MINUS \
GO FROM "player100" OVER follow;


returns

Empty set (time spent 2243/3259 us)


Precedence of the SET Operations and Pipe¶

Please note that when a query contains pipe | and set operations, pipe takes precedence. Refer to the Pipe Doc for details. Query GO FROM 1 UNION GO FROM 2 | GO FROM 3 is the same as query GO FROM 1 UNION (GO FROM 2 | GO FROM 3).

For example:

nebula> GO FROM "player102" OVER follow YIELD follow._dst AS play_dst  \
UNION \
GO FROM "team200" OVER serve REVERSELY YIELD serve._dst AS play_dst \
| GO FROM $-.play_dst OVER follow YIELD follow._dst AS play_dst; +-------------+ | play_dst | +-------------+ | "player101" | +-------------+ | "player102" | +-------------+  The statements in the red bar are executed first. And then the statement in the green box is executed. nebula> (GO FROM "player102" OVER follow YIELD follow._dst AS play_dst \ UNION \ GO FROM "team200" OVER serve REVERSELY YIELD serve._dst AS play_dst) \ | GO FROM$-.play_dst OVER follow YIELD follow._dst AS play_dst;


In the above query, the parentheses change the execution priority, and the statements within the parentheses take the precedence.

Last update: March 17, 2021