Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support-set-operators-for-cypher #1887

Merged
merged 2 commits into from
Jan 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions docs-2.0/3.ngql-guide/5.operators/6.set.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ This topic will describe the set operators, including `UNION`, `UNION ALL`, `INT

All set operators have equal precedence. If a nGQL statement contains multiple set operators, NebulaGraph will evaluate them from left to right unless parentheses explicitly specify another order.

## OpenCypher compatibility
!!! caution

Set operators apply to native nGQL only.
The names and order of the variables defined in the query statements before and after the set operator must be consistent. For example, the names and order of `a,b,c` in `RETURN a,b,c UNION RETURN a,b,c` need to be consistent.

## UNION, UNION DISTINCT, and UNION ALL

Expand Down Expand Up @@ -35,6 +35,21 @@ nebula> GO FROM "player102" OVER follow YIELD dst(edge) \
| "player125" |
+-------------+

nebula> MATCH (v:player) \
WITH v.player.name AS v \
RETURN n ORDER BY n LIMIT 3 \
UNION \
UNWIND ["Tony Parker", "Ben Simmons"] AS n \
RETURN n;
+---------------------+
| n |
+---------------------+
| "Amar'e Stoudemire" |
| "Aron Baynes" |
| "Ben Simmons" |
| "Tony Parker" |
+---------------------+

# The following statement returns the union of two query results with duplicated elements.
nebula> GO FROM "player102" OVER follow YIELD dst(edge) \
UNION ALL \
Expand All @@ -48,6 +63,22 @@ nebula> GO FROM "player102" OVER follow YIELD dst(edge) \
| "player125" |
+-------------+

nebula> MATCH (v:player) \
WITH v.player.name AS n \
RETURN n ORDER BY n LIMIT 3 \
UNION ALL \
UNWIND ["Tony Parker", "Ben Simmons"] AS n \
RETURN n;
+---------------------+
| n |
+---------------------+
| "Amar'e Stoudemire" |
| "Aron Baynes" |
| "Ben Simmons" |
| "Tony Parker" |
| "Ben Simmons" |
+---------------------+

# 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 dst(edge) AS id, properties(edge).degree AS Degree, properties($$).age AS Age \
Expand Down Expand Up @@ -77,6 +108,7 @@ nebula> GO FROM "player102" OVER follow \
### Example

```ngql
# The following statement returns the intersection of two query results.
nebula> GO FROM "player102" OVER follow \
YIELD dst(edge) AS id, properties(edge).degree AS Degree, properties($$).age AS Age \
INTERSECT \
Expand All @@ -86,6 +118,18 @@ nebula> GO FROM "player102" OVER follow \
| id | Degree | Age |
+----+--------+-----+
+----+--------+-----+

nebula> UNWIND [1,2] AS a RETURN a \
INTERSECT \
UNWIND [1,2,3,4] AS a \
RETURN a;
+---+
| a |
+---+
| 1 |
| 2 |
+---+

```

## MINUS
Expand All @@ -99,6 +143,7 @@ Operator `MINUS` returns the subtraction (or difference) of two sets A and B (de
### Example

```ngql
# The following statement returns the elements in the first query result but not in the second query result.
nebula> GO FROM "player100" OVER follow YIELD dst(edge) \
MINUS \
GO FROM "player102" OVER follow YIELD dst(edge);
Expand All @@ -116,6 +161,18 @@ nebula> GO FROM "player102" OVER follow YIELD dst(edge) \
+-------------+
| "player100" |
+-------------+

nebula> UNWIND [1,2,3] AS a RETURN a \
MINUS \
WITH 4 AS a \
RETURN a;
+---+
| a |
+---+
| 1 |
| 2 |
| 3 |
+---+
```

## Precedence of the set operators and pipe operators
Expand Down