跳转至

集合运算符Graph

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

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

openCypher兼容性Graph

集合运算符仅适用于nGQL扩展。

UNION、UNION DISTINCT、UNION ALLGraph

<left> UNION [DISTINCT | ALL] <right> [ UNION [DISTINCT | ALL] <right> ...]
  • 运算符UNION DISTINCT(或使用缩写UNION)返回两个集合A和B的并集,不包含重复的元素。
  • 运算符UNION ALL返回两个集合A和B的并集,包含重复的元素。
  • leftright必须有相同数量的列和数据类型。如果需要转换数据类型,请参见Graph。

示例Graph

# 返回两个查询结果的并集,不包含重复的元素。
nebula> GO FROM "player102" OVER follow \
    UNION \
    GO FROM "player100" OVER follow;
+-------------+
| follow._dst |
+-------------+
| "player101" |
+-------------+
| "player102" |
+-------------+

# 返回两个查询结果的并集,包含重复的元素。
nebula> GO FROM "player102" OVER follow \
    UNION ALL \
    GO FROM "player100" OVER follow;
+-------------+
| follow._dst |
+-------------+
| "player101" |
+-------------+
| "player101" |
+-------------+
| "player102" |
+-------------+

# UNION也可以和YIELD语句一起使用,去重时会检查每一行的所有列,每列都相同时才会去重。
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  |     
+-------------+--------+-----+

INTERSECTGraph

<left> INTERSECT <right>
  • 运算符INTERSECT返回两个集合A和B的交集。
  • leftright必须有相同数量的列和数据类型。如果需要转换数据类型,请参见Graph。

示例Graph

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)

MINUSGraph

<left> MINUS <right>

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

示例Graph

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)

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

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

示例Graph

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" |
+-------------+

image

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

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

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;

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


最后更新: March 24, 2021
Back to top