Skip to content

Commit

Permalink
support gql list/set/map (#1366)
Browse files Browse the repository at this point in the history
  • Loading branch information
cooper-lzy authored Jan 7, 2022
1 parent 51fd34a commit 1b931fe
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ INT8
INTERSECT
IS
LIMIT
LIST
LOOKUP
MAP
MATCH
MINUS
NO
Expand Down
34 changes: 19 additions & 15 deletions docs-2.0/3.ngql-guide/3.data-types/6.list.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

列表用左方括号([)和右方括号(])包裹多个元素,各个元素之间用英文逗号(,)隔开。元素前后的空格在列表中被忽略,因此可以使用换行符、制表符和空格调整格式。

## OpenCypher 兼容性

复合数据类型(例如 List、Set、Map)**不能**存储为点或边的属性。

## 列表操作

对列表进行操作可以使用预设的[列表函数](../6.functions-and-expressions/6.list.md),也可以使用下标表达式过滤列表内的元素。
Expand Down Expand Up @@ -34,9 +38,9 @@ nGQL 的下标支持从前往后查询,从 0 开始,0 表示第一个元素

```ngql
# 返回列表 [1,2,3]
nebula> RETURN [1, 2, 3] AS List;
nebula> RETURN list[1, 2, 3] AS a;
+-----------+
| List |
| a |
+-----------+
| [1, 2, 3] |
+-----------+
Expand Down Expand Up @@ -74,8 +78,8 @@ nebula> RETURN range(1,5)[3..] AS a;
+--------+
# 返回列表内下标小于 3 的元素。
nebula> WITH [1, 2, 3, 4, 5] AS list \
RETURN list[..3] AS r;
nebula> WITH list[1, 2, 3, 4, 5] AS a \
RETURN a[..3] AS r;
+-----------+
| r |
+-----------+
Expand All @@ -91,15 +95,15 @@ nebula> RETURN [n IN range(1,5) WHERE n > 2 | n + 10] AS a;
+--------------+
# 返回列表内第一个至倒数第二个(包括)的元素。
nebula> YIELD [1, 2, 3][0..-1] AS a;
nebula> YIELD list[1, 2, 3][0..-1] AS a;
+--------+
| a |
+--------+
| [1, 2] |
+--------+
# 返回列表内倒数第三个至倒数第一个(不包括)的元素。
nebula> YIELD [1, 2, 3, 4, 5][-3..-1] AS a;
nebula> YIELD list[1, 2, 3, 4, 5][-3..-1] AS a;
+--------+
| a |
+--------+
Expand All @@ -108,49 +112,49 @@ nebula> YIELD [1, 2, 3, 4, 5][-3..-1] AS a;
# 设置变量,返回列表内下标为 1、2 的元素。
nebula> $var = YIELD 1 AS f, 3 AS t; \
YIELD [1, 2, 3][$var.f..$var.t] AS a;
YIELD list[1, 2, 3][$var.f..$var.t] AS a;
+--------+
| a |
+--------+
| [2, 3] |
+--------+
# 越界的下标返回为空,未越界的可以正常返回。
nebula> RETURN [1, 2, 3, 4, 5] [0..10] AS a;
nebula> RETURN list[1, 2, 3, 4, 5] [0..10] AS a;
+-----------------+
| a |
+-----------------+
| [1, 2, 3, 4, 5] |
+-----------------+
nebula> RETURN [1, 2, 3] [-5..5] AS a;
nebula> RETURN list[1, 2, 3] [-5..5] AS a;
+-----------+
| a |
+-----------+
| [1, 2, 3] |
+-----------+
# [0..0] 时返回为空。
nebula> RETURN [1, 2, 3, 4, 5] [0..0] AS a;
nebula> RETURN list[1, 2, 3, 4, 5] [0..0] AS a;
+----+
| a |
+----+
| [] |
+----+
# M ≥ N 时,返回为空。
nebula> RETURN [1, 2, 3, 4, 5] [3..1] AS a;
nebula> RETURN list[1, 2, 3, 4, 5] [3..1] AS a;
+----+
| a |
+----+
| [] |
+----+
# 范围查询时,下标有 null 时,返回为 null。
nebula> WITH [1,2,3] AS list \
RETURN list[0..null] as a;
nebula> WITH list[1,2,3] AS a \
RETURN a[0..null] as r;
+----------+
| a |
| r |
+----------+
| __NULL__ |
+----------+
Expand All @@ -172,7 +176,7 @@ nebula> RETURN [n IN range(1, 3) WHERE true | n] AS r;
+-----------+
# 返回列表 [1,2,3] 的长度。
nebula> RETURN size([1,2,3]);
nebula> RETURN size(list[1,2,3]);
+---------------+
| size([1,2,3]) |
+---------------+
Expand Down
54 changes: 52 additions & 2 deletions docs-2.0/3.ngql-guide/3.data-types/7.set.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
# 集合

集合(Set)是复合数据类型。
集合(Set)是复合数据类型,集合中是一组元素,与列表(List)不同的是,集合中的元素是无序的,且不允许重复。

集合用左花括号({)和右花括号(})包裹多个元素,各个元素之间用英文逗号(,)隔开。元素前后的空格在集合中被忽略,因此可以使用换行符、制表符和空格调整格式。

## OpenCypher 兼容性

在 OpenCypher 中,集合不是一个数据类型,而在 nGQL 中,集合仍在设计阶段。
- 复合数据类型(例如 List、Set、Map)**不能**存储为点或边的属性。

- 在 OpenCypher 中,集合不是一个数据类型,而在 nGQL 中,用户可以使用集合。

## 示例

```ngql
# 返回集合 {1,2,3}。
nebula> RETURN set{1, 2, 3} AS a;
+-----------+
| a |
+-----------+
| {3, 2, 1} |
+-----------+
# 返回集合 {1,2,1},因为集合不允许重复元素,会返回 {1,2},且顺序是无序的。
nebula> RETURN set{1, 2, 1} AS a;
+--------+
| a |
+--------+
| {2, 1} |
+--------+
# 判断集合中是否有指定元素 1。
nebula> RETURN 1 IN set{1, 2} AS a;
+------+
| a |
+------+
| true |
+------+
# 计算集合中的元素数量。
nebula> YIELD size(set{1, 2, 1}) AS a;
+---+
| a |
+---+
| 2 |
+---+
# 返回目标点属性值组成的集合。
nebula> GO FROM "player100" OVER follow \
YIELD set{properties($$).name,properties($$).age} as a;
+-----------------------+
| a |
+-----------------------+
| {36, "Tony Parker"} |
| {41, "Manu Ginobili"} |
+-----------------------+
```
59 changes: 48 additions & 11 deletions docs-2.0/3.ngql-guide/3.data-types/8.map.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,56 @@

映射(Map)是复合数据类型。一个映射是一组键值对(Key-Value)的无序集合。在映射中,Key 是字符串类型,Value 可以是任何数据类型。用户可以通过`map['<key>']`的方法获取映射中的元素。

## 字面值映射

```ngql
nebula> YIELD {key: 'Value', listKey: [{inner: 'Map1'}, {inner: 'Map2'}]};
+-------------------------------------------------------------+
| {key:Value,listKey:[{inner:Map1},{inner:Map2}]} |
+-------------------------------------------------------------+
| {key: "Value", listKey: [{inner: "Map1"}, {inner: "Map2"}]} |
+-------------------------------------------------------------+
```
映射用左花括号({)和右花括号(})包裹多个键值对,各个键值对之间用英文逗号(,)隔开。键值对前后的空格在映射中被忽略,因此可以使用换行符、制表符和空格调整格式。

## OpenCypher 兼容性

- 复合数据类型(例如 set、map、list**不能**存储为点或边的属性。
- 复合数据类型(例如 List、Set、Map**不能**存储为点或边的属性。

- 不支持映射投影(map projection)。

## 示例

```ngql
# 返回简单的映射。
nebula> YIELD map{key1: 'Value1', Key2: 'Value2'} as a;
+----------------------------------+
| a |
+----------------------------------+
| {Key2: "Value2", key1: "Value1"} |
+----------------------------------+
# 返回列表类型的映射。
nebula> YIELD map{listKey: [{inner: 'Map1'}, {inner: 'Map2'}]} as a;
+-----------------------------------------------+
| a |
+-----------------------------------------------+
| {listKey: [{inner: "Map1"}, {inner: "Map2"}]} |
+-----------------------------------------------+
# 返回混合类型的映射。
nebula> RETURN map{a: LIST[1,2], b: SET{1,2,1}, c: "hee"} as a;
+----------------------------------+
| a |
+----------------------------------+
| {a: [1, 2], b: {2, 1}, c: "hee"} |
+----------------------------------+
# 返回映射中的指定元素。
nebula> RETURN map{a: LIST[1,2], b: SET{1,2,1}, c: "hee"}["b"] AS b;
+--------+
| b |
+--------+
| {2, 1} |
+--------+
# 判断映射中是否有指定key,暂不支持判断value。
nebula> RETURN "a" IN MAP{a:1, b:2} AS a;
+------+
| a |
+------+
| true |
+------+
```


0 comments on commit 1b931fe

Please sign in to comment.