集合运算符

合并多个请求时,可以使用集合运算符,包括UNIONUNION ALLINTERSECTMINUS

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

openCypher兼容性

集合运算符仅适用于原生nGQL。

UNION、UNION DISTINCT、UNION ALL

  1. <left> UNION [DISTINCT | ALL] <right> [ UNION [DISTINCT | ALL] <right> ...]
  • 运算符UNION DISTINCT(或使用缩写UNION)返回两个集合A和B的并集,不包含重复的元素。

  • 运算符UNION ALL返回两个集合A和B的并集,包含重复的元素。

  • leftright必须有相同数量的列和数据类型。如果需要转换数据类型,请参见类型转换

示例

  1. # 返回两个查询结果的并集,不包含重复的元素。
  2. nebula> GO FROM "player102" OVER follow \
  3. UNION \
  4. GO FROM "player100" OVER follow;
  5. +-------------+
  6. | follow._dst |
  7. +-------------+
  8. | "player100" |
  9. | "player101" |
  10. | "player125" |
  11. +-------------+
  12. # 返回两个查询结果的并集,包含重复的元素。
  13. nebula> GO FROM "player102" OVER follow \
  14. UNION ALL \
  15. GO FROM "player100" OVER follow;
  16. +-------------+
  17. | follow._dst |
  18. +-------------+
  19. | "player100" |
  20. | "player101" |
  21. | "player101" |
  22. | "player125" |
  23. +-------------+
  24. # UNION也可以和YIELD语句一起使用,去重时会检查每一行的所有列,每列都相同时才会去重。
  25. nebula> GO FROM "player102" OVER follow \
  26. YIELD dst(edge) AS id, properties(edge).degree AS Degree, properties($$).age AS Age \
  27. UNION /* DISTINCT */ \
  28. GO FROM "player100" OVER follow \
  29. YIELD dst(edge) AS id, properties(edge).degree AS Degree, properties($$).age AS Age;
  30. +-------------+--------+-----+
  31. | id | Degree | Age |
  32. +-------------+--------+-----+
  33. | "player100" | 75 | 42 |
  34. | "player101" | 75 | 36 |
  35. | "player101" | 95 | 36 |
  36. | "player125" | 95 | 41 |
  37. +-------------+--------+-----+

INTERSECT

  1. <left> INTERSECT <right>
  • 运算符INTERSECT返回两个集合A和B的交集。

  • leftright必须有相同数量的列和数据类型。如果需要转换数据类型,请参见类型转换

示例

  1. nebula> GO FROM "player102" OVER follow \
  2. YIELD dst(edge) AS id, properties(edge).degree AS Degree, properties($$).age AS Age \
  3. INTERSECT \
  4. GO FROM "player100" OVER follow \
  5. YIELD dst(edge) AS id, properties(edge).degree AS Degree, properties($$).age AS Age;
  6. +----+--------+-----+
  7. | id | Degree | Age |
  8. +----+--------+-----+
  9. +----+--------+-----+

MINUS

  1. <left> MINUS <right>

运算符MINUS返回两个集合A和B的差异,即A-B。请注意leftright的顺序,A-B表示在集合A中,但是不在集合B中的元素。

示例

  1. nebula> GO FROM "player100" OVER follow \
  2. MINUS \
  3. GO FROM "player102" OVER follow;
  4. +-------------+
  5. | follow._dst |
  6. +-------------+
  7. | "player125" |
  8. +-------------+
  9. nebula> GO FROM "player102" OVER follow \
  10. MINUS \
  11. GO FROM "player100" OVER follow;
  12. +-------------+
  13. | follow._dst |
  14. +-------------+
  15. | "player100" |
  16. +-------------+

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

当查询包含集合运算符和管道符(|)时,管道符的优先级高。例如GO FROM 1 UNION GO FROM 2 | GO FROM 3相当于GO FROM 1 UNION (GO FROM 2 | GO FROM 3)

示例

  1. nebula> GO FROM "player102" OVER follow \
  2. YIELD dst(edge) AS play_dst \
  3. UNION \
  4. GO FROM "team200" OVER serve REVERSELY \
  5. YIELD src(edge) AS play_src \
  6. | GO FROM $-.play_src OVER follow YIELD dst(edge) AS play_dst;
  7. +-------------+
  8. | play_dst |
  9. +-------------+
  10. | "player100" |
  11. | "player101" |
  12. | "player117" |
  13. | "player105" |
  14. +-------------+

image

该查询会先执行红框内的语句,然后执行绿框的UNION操作。

圆括号可以修改执行的优先级,例如:

  1. nebula> (GO FROM "player102" OVER follow \
  2. YIELD dst(edge) AS play_dst \
  3. UNION \
  4. GO FROM "team200" OVER serve REVERSELY \
  5. YIELD src(edge) AS play_dst) \
  6. | GO FROM $-.play_dst OVER follow YIELD dst(edge) AS play_dst;

该查询中,圆括号包裹的部分先执行,即先执行UNION操作,再将结果结合管道符进行下一步操作。


最后更新: October 29, 2021