Set operators¶
This topic will describe the set operators, including UNION
, UNION ALL
, INTERSECT
, and MINUS
. To combine multiple queries, use these set operators.
All set operators have equal precedence. If a nGQL statement contains multiple set operators, Nebula Graph will evaluate them from left to right unless parentheses explicitly specify another order.
OpenCypher compatibility¶
Set operators apply to native nGQL only.
UNION, UNION DISTINCT, and UNION ALL¶
<left> UNION [DISTINCT | ALL] <right> [ UNION [DISTINCT | ALL] <right> ...]
- Operator
UNION DISTINCT
(or by shortUNION
) returns the union of two sets A and B without duplicated 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.
Examples¶
# The following statement returns the union of two query results without duplicated elements.
nebula> GO FROM "player102" OVER follow \
UNION \
GO FROM "player100" OVER follow;
+-------------+
| follow._dst |
+-------------+
| "player101" |
+-------------+
| "player102" |
+-------------+
# The following statement returns the union of two query results with duplicated elements.
nebula> GO FROM "player102" OVER follow \
UNION ALL \
GO FROM "player100" OVER follow;
+-------------+
| follow._dst |
+-------------+
| "player101" |
+-------------+
| "player101" |
+-------------+
| "player102" |
+-------------+
# UNION can also work with the YIELD statement. The DISTINCT keyword will check duplication by all the columns for every line, and remove duplicated lines if every column is the same.
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;
+-------------+--------+-----+
| id | Degree | Age |
+-------------+--------+-----+
| "player101" | 75 | 36 |
+-------------+--------+-----+
| "player101" | 96 | 36 |
+-------------+--------+-----+
| "player102" | 90 | 33 |
+-------------+--------+-----+
INTERSECT¶
<left> INTERSECT <right>
- Operator
INTERSECT
returns the intersection of two sets A and B (denoted by A ⋂ B).
- Similar to
UNION
, theleft
andright
must have the same number of columns and data types. Different data types are converted according to the Type Conversion.
Example¶
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;
Empty set (time spent 2990/3511 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 left
and right
. The set A-B
consists of elements that are in A but not in B.
Example¶
nebula> GO FROM "player100" OVER follow \
MINUS \
GO FROM "player102" OVER follow;
+-------------+
| follow._dst |
+-------------+
| "player102" |
+-------------+
nebula> GO FROM "player102" OVER follow \
MINUS \
GO FROM "player100" OVER follow;
Empty set (time spent 2243/3259 us)
Precedence of the set operators and pipe operators¶
Please note that when a query contains a pipe |
and a set operator, the pipe takes precedence. Refer to Pipe for details. The query GO FROM 1 UNION GO FROM 2 | GO FROM 3
is the same as the query GO FROM 1 UNION (GO FROM 2 | GO FROM 3)
.
Examples¶
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 above query executes the statements in the red bar first and then executes the statement in the green box.
The parentheses can change the execution priority. 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;
In the above query, the statements within the parentheses take precedence. That is, the UNION
operation will be executed first, and its output will be executed as the input of the next operation with pipes.