Skip to content

Commit

Permalink
support-set-operators-for-cypher (#2499)
Browse files Browse the repository at this point in the history
* support-set-operators-for-cypher

* add note and define a different variable
  • Loading branch information
abby-cyber authored Jan 17, 2023
1 parent 5fa524c commit 5e23051
Showing 1 changed file with 59 additions and 2 deletions.
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 @@

所有集合运算符的优先级相同,如果一个 nGQL 语句中有多个集合运算符,NebulaGraph 会从左到右进行计算,除非用括号指定顺序。

## openCypher 兼容性
!!! caution

集合运算符仅适用于原生 nGQL
集合运算符前后的查询语句中定义的变量名及顺序必需保持一致,例如`RETURN a,b,c UNION RETURN a,b,c`中的`a,b,c`的名称及顺序需要保持一致

## UNION、UNION DISTINCT、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 n \
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" |
+---------------------+
# 返回两个查询结果的并集,包含重复的元素。
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 也可以和 YIELD 语句一起使用,去重时会检查每一行的所有列,每列都相同时才会去重。
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,8 @@ nebula> GO FROM "player102" OVER follow \
### 示例

```ngql
# 返回两个查询结果的交集。
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 +119,17 @@ 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 @@ nebula> GO FROM "player102" OVER follow \
### 示例

```ngql
# 返回在第一个查询结果中,但是不在第二个查询结果中的元素。
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 |
+---+
```

## 集合运算符和管道符的优先级
Expand Down

0 comments on commit 5e23051

Please sign in to comment.