集合操作(UNIONINTERSECTMINUS

您可以使用集合运算符 UNIONUNION ALLINTERSECTMINUS 进行多个组合查询。所有集合运算符具有相同的优先级。如果 nGQL 语句包含多个集合运算符,则 Nebula Graph 将对其进行从左到右处理,除非使用括号明确指定了顺序。

复合查询中,GO 语句返回的结果必须列数相同,且数据类型相同(比如均为数字类型或字符类型)。

UNION,UNION DISTINCT,UNION ALL

UNION DISTINCT(简称 UNION)返回数据集 A 和 B 的并集(不包含重复元素)。

UNION ALL 返回数据集 A 和 B 的并集(包含重复元素)。UNION 语法为

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

<left><right> 必须列数相同,且数据类型相同。如果数据类型不同,将按照类型转换进行转换。

示例

  1. nebula> GO FROM 1 OVER e1 \
  2. UNION \
  3. GO FROM 2 OVER e1;

以上语句返回点 12(沿边 e1)关联的唯一的点。

  1. nebula> GO FROM 1 OVER e1 \
  2. UNION ALL\
  3. GO FROM 2 OVER e1;

以上语句返回点 12 关联的所有点,其中存在重复点。

UNION 亦可与 YIELD 同时使用,例如以下语句:

  1. nebula> GO FROM 1 OVER e1 YIELD e1._dst AS id, e1.prop1 AS col_1, $$.tag.prop2 AS col_2; -- query 1
  2. =======================
  3. | id | col_1| col_2 |
  4. =======================
  5. | 104 | 1 | 2 | -- line 1
  6. -----------------------
  7. | 215 | 4 | 3 | -- line 3
  8. -----------------------
  9. nebula> GO FROM 2,3 OVER e1 YIELD e1._dst AS id, e1.prop1 AS col_1, $$.tag.prop2 AS col_2; -- query 2
  10. ======================
  11. | id | col_1| col_2 |
  12. ======================
  13. | 104 | 1 | 2 | -- line 1
  14. ----------------------
  15. | 104 | 2 | 2 | -- line 2
  16. ----------------------
  1. nebula> GO FROM 1 OVER e1 YIELD e1._dst AS id, e1.prop1 AS col_1, $$.tag.prop2 AS col_2 \
  2. UNION /* DISTINCT */ \
  3. GO FROM 2,3 OVER e1 YIELD e1._dst AS id, e1.prop1 AS col_1, $$.tag.prop2 AS col_2;

以上语句返回

  1. =======================
  2. | id | col_1| col_2 | -- UNION or UNION DISTINCT. The column names come from query 1
  3. =======================
  4. | 104 | 1 | 2 | -- line 1
  5. -----------------------
  6. | 104 | 2 | 2 | -- line 2
  7. -----------------------
  8. | 215 | 4 | 3 | -- line 3
  9. -----------------------

请注意第一行与第二行返回相同 id 的点,但是返回的值不同。DISTINCT 检查返回结果中的重复值。所以第一行与第二行的返回结果不同。 UNION ALL 返回结果为

  1. nebula> GO FROM 1 OVER e1 YIELD e1._dst AS id, e1.prop1 AS col_1, $$.tag.prop2 AS col_2 \
  2. UNION ALL \
  3. GO FROM 2,3 OVER e1 YIELD e1._dst AS id, e1.prop1 AS col_1, $$.tag.prop2 AS col_2;
  4. ======================
  5. | id | col_1| col_2 | -- UNION ALL
  6. ======================
  7. | 104 | 1 | 2 | -- line 1
  8. ----------------------
  9. | 104 | 1 | 2 | -- line 1
  10. ----------------------
  11. | 104 | 2 | 2 | -- line 2
  12. ----------------------
  13. | 215 | 4 | 3 | -- line 3
  14. ----------------------

INTERSECT

INTERSECT 返回集合 A 和 B 的交集(A ⋂ B)。

  1. <left> INTERSECT <right>

UNION 类似, <left><right> 必须列数相同,且数据类型相同。 此外,只返回 <left><right> 相同的行。例如:

  1. nebula> GO FROM 1 OVER e1 YIELD e1._dst AS id, e1.prop1 AS col_1, $$.tag.prop2 AS col_2
  2. INTERSECT
  3. GO FROM 2,3 OVER e1 YIELD e1._dst AS id, e1.prop1 AS col_1, $$.tag.prop2 AS col_2;

返回

  1. =======================
  2. | id | col_1 | col_2 |
  3. =======================
  4. | 104 | 1 | 2 | -- line 1
  5. -----------------------

MINUS

返回 A - B 数据的差集,此处请注意运算顺序。例如:

  1. nebula> GO FROM 1 OVER e1 YIELD e1._dst AS id, e1.prop1 AS col_1, $$.tag.prop2 AS col_2
  2. MINUS
  3. GO FROM 2,3 OVER e1 YIELD e1._dst AS id, e1.prop1 AS col_1, $$.tag.prop2 AS col_2;

返回

  1. ========================
  2. | id | col_1 | col_2 |
  3. ========================
  4. | 215 | 4 | 3 | -- line 3
  5. ------------------------

如果更改 MINUS 顺序

  1. nebula> GO FROM 2,3 OVER e1 YIELD e1._dst AS id, e1.prop1 AS col_1, $$.tag.prop2 AS col_2
  2. MINUS
  3. GO FROM 1 OVER e1 YIELD e1._dst AS id, e1.prop1 AS col_1, $$.tag.prop2 AS col_2;

则返回

  1. =======================
  2. | id | col_1 | col_2 | -- column named from query 2
  3. =======================
  4. | 104 | 2 | 2 | -- line 2
  5. -----------------------

集合操作和管道的优先级

请注意当一条查询同时包含管道 | 和集合操作时,管道的优先级高于集合操作。管道用法请参考管道文档。语句 GO FROM 1 UNION GO FROM 2 | GO FROM 3 等价于语句 GO FROM 1 UNION (GO FROM 2 | GO FROM 3)

例如:

  1. nebula> GO FROM 100 OVER follow YIELD follow._dst AS play_dst \
  2. UNION \
  3. GO FROM 200 OVER serve REVERSELY YIELD serve._dst AS play_dst \
  4. | GO FROM $-.play_dst OVER follow YIELD follow._dst AS play_dst;

image

红色框内的语句先执行,然后再执行绿色框内的语句。

  1. nebula> (GO FROM 100 OVER follow YIELD follow._dst AS play_dst \
  2. UNION \
  3. GO FROM 200 OVER serve REVERSELY YIELD serve._dst AS play_dst) \
  4. | GO FROM $-.play_dst OVER follow YIELD follow._dst AS play_dst;

以上语句中,括号改变了执行的优先级,括号内的语句先执行。