Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operators #559

Merged
merged 5 commits into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 82 additions & 49 deletions docs-2.0/3.ngql-guide/5.operators/1.comparison.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,53 @@
# Comparison operators

| Name | Description |
Nebula Graph supports the following comparison operators.

| Name | Description |
|:----|:----:|
| `=` | Assign a value |
| `/` | Division operator |
| `==` | Equal operator |
| `!=`, `<>` | Not equal operator |
| `<` | Less than operator |
| `<=` | Less than or equal operator |
| `-` | Minus operator |
| `%` | Modulo operator |
| `+` | Addition operator |
| `*` | Multiplication operator |
| `-` | Change the sign of the argument |
| `IS NULL` | NULL check|
| `IS NOT NULL` | not NULL check |

Comparison operations result in a value of _true_ and _false_.
| `=` | Assigns a value |
| `+` | Addition operator |
| `-` | Minus operator |
| `*` | Multiplication operator |
| `/` | Division operator |
| `==` | Equal operator |
| `!=`, `<>` | Not equal operator |
| `>` | Greater than operator |
| `>=` | Greater than or equal operator |
| `<` | Less than operator |
| `<=` | Less than or equal operator |
| `%` | Modulo operator |
| `-` | Changes the sign of the argument |
| `IS NULL` | NULL check |
| `IS NOT NULL` | Not NULL check |
| `IS EMPTY` | EMPTY check |
| `IS NOT EMPTY` | Not EMPTY check |

The result of the comparison operation is `true` or `false`.

!!! note

Comparability between values of different types is often undefined. The result could be NULL or others.
- Comparability between values of different types is often undefined. The result could be `NULL` or others.

- `EMPTY` is currently used only for checking,and does not support functions or operations. It includes but is not limited to `GROUP BY`, `count()`, `sum()`, `max()`, `hash()`, ` collect()`, `+`, `*`.
randomJoe211 marked this conversation as resolved.
Show resolved Hide resolved

## OpenCypher compatibility

!!! note "OpenCypher compatibility"
- The comparison operation of `NULL` is different from openCypher. The behavior may also change. `IS [NOT] NULL` is often used with `OPTIONAL MATCH` in openCypher. But `OPTIONAL MATCH` is not supported in nGQL.

Comparing with NULL is different from openCypher. The behavior may change. `IS [NOT] NULL` is often used with `OPTIONAL MATCH`. But `OPTIONAL MATCH` is not support in nGQL.
- openCypher does not have `EMPTY`. Thus `EMPTY` is not supported in MATCH statements.

* `==`
## Examples

Equal. String comparisons are case-sensitive. Values of different types are not equal.
### `==`

String comparisons are case-sensitive. Values of different types are not equal.

!!! note

The equality operator is `==` in nGQL and is `=` in openCypher.
The equal operator is `==` in nGQL, while in openCypher it is `=`.

```ngql
nebula> RETURN 'A' == 'a', toUpper('A') == toUpper('a'), toLower('A') == toLower('a')
nebula> RETURN 'A' == 'a', toUpper('A') == toUpper('a'), toLower('A') == toLower('a');
+------------+------------------------------+------------------------------+
| ("A"=="a") | (toUpper("A")==toUpper("a")) | (toLower("A")==toLower("a")) |
+------------+------------------------------+------------------------------+
Expand All @@ -50,9 +62,7 @@ nebula> RETURN '2' == 2, toInteger('2') == 2;
+----------+---------------------+
```

* `>`

Greater than:
### `>`

```ngql
nebula> RETURN 3 > 2;
Expand All @@ -62,30 +72,27 @@ nebula> RETURN 3 > 2;
| true |
+-------+

nebula> WITH 4 AS one, 3 AS two RETURN one > two AS result;
nebula> WITH 4 AS one, 3 AS two \
RETURN one > two AS result;
+--------+
| result |
+--------+
| true |
+--------+
```

* `>=`

Greater than or equal to:
### `>=`

```ngql
nebula> RETURN 2 >= "2", 2 >= 2
nebula> RETURN 2 >= "2", 2 >= 2;
+----------+--------+
| (2>="2") | (2>=2) |
+----------+--------+
| __NULL__ | true |
+----------+--------+
```

* `<`

Less than:
### `<`

```ngql
nebula> YIELD 2.0 < 1.9;
Expand All @@ -96,9 +103,7 @@ nebula> YIELD 2.0 < 1.9;
+---------+
```

* `<=`

Less than or equal to:
### `<=`

```ngql
nebula> YIELD 0.11 <= 0.11;
Expand All @@ -109,9 +114,7 @@ nebula> YIELD 0.11 <= 0.11;
+--------------+
```

* `!=`

Not equal:
### `!=`

```ngql
nebula> YIELD 1 != '1';
Expand All @@ -122,40 +125,42 @@ nebula> YIELD 1 != '1';
+--------+
```

* `IS [NOT] NULL`
### `IS [NOT] NULL`

```ngql
nebula> RETURN null IS NULL AS value1, null == null AS value2, null != null AS value3
nebula> RETURN null IS NULL AS value1, null == null AS value2, null != null AS value3;
+--------+----------+----------+
| value1 | value2 | value3 |
+--------+----------+----------+
| true | __NULL__ | __NULL__ |
+--------+----------+----------+

nebula> RETURN length(NULL), size(NULL), count(NULL), NULL IS NULL, NULL IS NOT NULL, sin(NULL), NULL + NULL, [1, NULL] IS NULL
nebula> RETURN length(NULL), size(NULL), count(NULL), NULL IS NULL, NULL IS NOT NULL, sin(NULL), NULL + NULL, [1, NULL] IS NULL;
+--------------+------------+-------------+--------------+------------------+-----------+-------------+------------------+
| length(NULL) | size(NULL) | COUNT(NULL) | NULL IS NULL | NULL IS NOT NULL | sin(NULL) | (NULL+NULL) | [1,NULL] IS NULL |
| length(NULL) | size(NULL) | count(NULL) | NULL IS NULL | NULL IS NOT NULL | sin(NULL) | (NULL+NULL) | [1,NULL] IS NULL |
+--------------+------------+-------------+--------------+------------------+-----------+-------------+------------------+
| BAD_TYPE | __NULL__ | 0 | true | false | BAD_TYPE | __NULL__ | false |
| __NULL__ | __NULL__ | 0 | true | false | __NULL__ | __NULL__ | false |
+--------------+------------+-------------+--------------+------------------+-----------+-------------+------------------+

nebula> WITH {name: null} AS map RETURN map.name IS NOT NULL
nebula> WITH {name: null} AS map \
RETURN map.name IS NOT NULL;
+----------------------+
| map.name IS NOT NULL |
+----------------------+
| false |
+----------------------+

nebula> WITH {name: 'Mats', name2: 'Pontus'} AS map1, \
{name: null} AS map2, {notName: 0, notName2: null } AS map3 \
RETURN map1.name IS NULL, map2.name IS NOT NULL, map3.name IS NULL
{name: null} AS map2, {notName: 0, notName2: null } AS map3 \
RETURN map1.name IS NULL, map2.name IS NOT NULL, map3.name IS NULL;
+-------------------+-----------------------+-------------------+
| map1.name IS NULL | map2.name IS NOT NULL | map3.name IS NULL |
+-------------------+-----------------------+-------------------+
| false | false | true |
+-------------------+-----------------------+-------------------+

nebula> MATCH (n:player) RETURN n.age IS NULL, n.name IS NOT NULL, n.empty IS NULL
nebula> MATCH (n:player) \
RETURN n.age IS NULL, n.name IS NOT NULL, n.empty IS NULL;
+---------------+--------------------+-----------------+
| n.age IS NULL | n.name IS NOT NULL | n.empty IS NULL |
+---------------+--------------------+-----------------+
Expand All @@ -167,3 +172,31 @@ nebula> MATCH (n:player) RETURN n.age IS NULL, n.name IS NOT NULL, n.empty IS NU
+---------------+--------------------+-----------------+
...
```

### `IS [NOT] EMPTY`

```ngql
nebula> RETURN null IS EMPTY;
+---------------+
| NULL IS EMPTY |
+---------------+
| false |
+---------------+

nebula> RETURN "a" IS NOT EMPTY;
+------------------+
| "a" IS NOT EMPTY |
+------------------+
| true |
+------------------+

nebula> GO FROM "player100" OVER * WHERE $$.player.name IS NOT EMPTY YIELD follow._dst;
+-------------+
| follow._dst |
+-------------+
| "player125" |
+-------------+
| "player101" |
+-------------+

```
9 changes: 5 additions & 4 deletions docs-2.0/3.ngql-guide/5.operators/2.boolean.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Boolean operators

| **Name** | **Description** |
Nebula Graph supports the following boolean operators.

| Name | Description |
| :------- | :-------------: |
| AND | Logical AND |
| NOT | Logical NOT |
Expand All @@ -9,9 +11,8 @@

For the precedence of the operators, refer to [Operator Precedence](9.precedence.md).

For the logical operations with NULL, refer to [NULL](../3.data-types/5.null.md).
For the logical operations with `NULL`, refer to [NULL](../3.data-types/5.null.md).

## Legacy version compatibility

* In Nebula Graph 1.0, non-zero numbers are evaluated to _true_ like c-language.
* In Nebula Graph 2.0, non-zero numbers can't be converted to boolean values.
* In Nebula Graph 2.0, non-zero numbers cannot be converted to boolean values.
33 changes: 28 additions & 5 deletions docs-2.0/3.ngql-guide/5.operators/4.pipe.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
# Pipe operator
# Pipe operators

Multiple queries can be combined using pipe operators in nGQL.

## OpenCypher compatibility

This page applies to native nGQL only.
Pipe operators apply to native nGQL only.

## Syntax

One major difference between nGQL and SQL is how sub-queries are composed.

In SQL, to form a statement, sub-queries are nested (embedded).
In nGQL the shell style `PIPE (|)` is introduced.
- In SQL, sub-queries are nested in the query statements.

- In nGQL, the shell style `PIPE (|)` is introduced into the sub-queries.

## Examples

```ngql
nebula> GO FROM "player100" OVER follow \
YIELD follow._dst AS dstid, $$.player.name AS Name |\
YIELD follow._dst AS dstid, $$.player.name AS Name | \
GO FROM $-.dstid OVER follow;

+-------------+
Expand All @@ -28,3 +31,23 @@ GO FROM $-.dstid OVER follow;
If there is no `YIELD` clause to define the output, the destination vertex ID is returned by default. If a YIELD clause is applied, the output is defined by the YIELD clause.

You must define aliases in the `YIELD` clause for the reference operator `$-` to use, just like `$-.dstid` in the preceding example.

## Performance tips

In Nebula Graph {{ nebula.release }}, pipes will affect the performance. Take `A | B` as an example, the effects are as follows:

1. Pipe operators operate synchronously. That is, the data can enter the pipe clause as a whole after the execution of clause `A` before the pipe operator is completed.

2. Pipe operators need to be serialized and deserialized, which is executed in a single thread.

3. If `A` sends a large amount of data to `|`, the entire query request may be very slow. You can try to split this statement.

1. Send `A` on the application,
izhuxiaoqing marked this conversation as resolved.
Show resolved Hide resolved

2. Split the return results on the application,

3. Send multiple graphd processes concurrently,
izhuxiaoqing marked this conversation as resolved.
Show resolved Hide resolved

4. Every graphd process executes part of B.

This is usually much faster than executing a complete `A | B` with a single graphd process.
17 changes: 6 additions & 11 deletions docs-2.0/3.ngql-guide/5.operators/5.property-reference.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
# Reference operators

NGQL provides reference operators to represent a property in a `WHERE` or `YIELD` clause, or the output of the statement before the pipe symbol in a composite query.
NGQL provides reference operators to represent a property in a `WHERE` or `YIELD` clause, or the output of the statement before the pipe operator in a composite query.

## OpenCypher compatibility

This page applies to native nGQL only.
Reference operators apply to native nGQL only.

## Reference operator List

|Reference operator|Description|
|-|-|
|`$^`|Refers to a source vertex property. For more information, see [Property reference](../4.variable-and-composite-queries/3.property-reference.md).|
|`$$`|Refers to a destination vertex property. For more information, see [Property reference](../4.variable-and-composite-queries/3.property-reference.md).|
|`$-`|Refers to the output of the statement before the pipe symbol in a composite query. For more information, see [Pipe](4.pipe.md).|
|`$-`|Refers to the output of the statement before the pipe operator in a composite query. For more information, see [Pipe](4.pipe.md).|

## Examples

The following example returns the age of the source vertex and the destination vertex.

```ngql
nebula> GO FROM "player100" OVER follow \
YIELD $^.player.age AS SrcAge, $$.player.age AS DestAge;
# The following example returns the age of the source vertex and the destination vertex.
nebula> GO FROM "player100" OVER follow YIELD $^.player.age AS SrcAge, $$.player.age AS DestAge;
+--------+---------+
| SrcAge | DestAge |
+--------+---------+
| 42 | 36 |
+--------+---------+
| 42 | 41 |
+--------+---------+
```

The following example returns the name and team of the players that "player100" follows.

```ngql
# The following example returns the name and team of the players that player100 follows.
nebula> GO FROM "player100" OVER follow \
YIELD follow._dst AS id | \
GO FROM $-.id OVER serve \
Expand Down
Loading