diff --git a/docs-2.0/3.ngql-guide/5.operators/6.set.md b/docs-2.0/3.ngql-guide/5.operators/6.set.md index d2b4fd38826..5a6c85ce2b0 100644 --- a/docs-2.0/3.ngql-guide/5.operators/6.set.md +++ b/docs-2.0/3.ngql-guide/5.operators/6.set.md @@ -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 @@ -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 \ @@ -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 \ @@ -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 \ @@ -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 @@ -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); @@ -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