# Lists¶

The list is a composite data type. A list is a sequence of values. Individual elements in a list can be accessed by their positions.

A list starts with a left square bracket [ and ends with a right square bracket ]. A list contains zero, one, or more expressions. List elements are separated from each other with commas (,). Whitespace around elements is ignored in the list, thus line breaks, tab stops, and blanks can be used for formatting.

## OpenCypher compatibility¶

A composite data type (i.e. set, map, and list) CANNOT be stored as properties of vertices or edges.

## List operations¶

You can use the preset list function to operate the list, or use the index to filter the elements in the list.

### Index syntax¶

[M]
[M..N]
[M..]
[..N]


The index of nGQL supports queries from front to back, starting from 0. 0 means the first element, 1 means the second element, and so on. It also supports queries from back to front, starting from -1. -1 means the last element, -2 means the penultimate element, and so on.

• [M]: represents the element whose index is M.
• [M..N]: represents the elements whose indexes are greater or equal to M but smaller than N. Return empty when N is 0.
• [M..]: represents the elements whose indexes are greater or equal to M.
• [..N]: represents the elements whose indexes are smaller than N. Return empty when N is 0.

Note

• Return empty if the index is out of bounds, while return normally if the index is within the bound.
• Return empty if MN.
• When querying a single element, if M is null, return BAD_TYPE. When conducting a range query, if M or N is null, return null.

## Examples¶

# The following query returns the list [1,2,3].
nebula> RETURN list[1, 2, 3] AS a;
+-----------+
| a         |
+-----------+
| [1, 2, 3] |
+-----------+

# The following query returns the element whose index is 3 in the list [1,2,3,4,5]. In a list, the index starts from 0, and thus the return element is 4.
nebula> RETURN range(1,5)[3];
+---------------+
| range(1,5)[3] |
+---------------+
| 4             |
+---------------+

# The following query returns the element whose index is -2 in the list [1,2,3,4,5]. The index of the last element in a list is -1, and thus the return element is 4.
nebula> RETURN range(1,5)[-2];
+------------------+
| range(1,5)[-(2)] |
+------------------+
| 4                |
+------------------+

# The following query returns the elements whose indexes are from 0 to 3 (not including 3) in the list [1,2,3,4,5].
nebula> RETURN range(1,5)[0..3];
+------------------+
| range(1,5)[0..3] |
+------------------+
| [1, 2, 3]        |
+------------------+

# The following query returns the elements whose indexes are greater than 2 in the list [1,2,3,4,5].
nebula> RETURN range(1,5)[3..] AS a;
+--------+
| a      |
+--------+
| [4, 5] |
+--------+

# The following query returns the elements whose indexes are smaller than 3.
nebula> WITH list[1, 2, 3, 4, 5] AS a \
RETURN a[..3] AS r;
+-----------+
| r         |
+-----------+
| [1, 2, 3] |
+-----------+

# The following query filters the elements whose indexes are greater than 2 in the list [1,2,3,4,5], calculate them respectively, and returns them.
nebula> RETURN [n IN range(1,5) WHERE n > 2 | n + 10] AS a;
+--------------+
| a            |
+--------------+
| [13, 14, 15] |
+--------------+

# The following query returns the elements from the first to the penultimate (inclusive) in the list [1, 2, 3].
nebula> YIELD list[1, 2, 3][0..-1] AS a;
+--------+
| a      |
+--------+
| [1, 2] |
+--------+

# The following query returns the elements from the first (exclusive) to the third backward in the list [1, 2, 3, 4, 5].
nebula> YIELD list[1, 2, 3, 4, 5][-3..-1] AS a;
+--------+
| a      |
+--------+
| [3, 4] |
+--------+

# The following query sets the variables and returns the elements whose indexes are 1 and 2.
nebula> $var = YIELD 1 AS f, 3 AS t; \ YIELD list[1, 2, 3][$var.f..$var.t] AS a; +--------+ | a | +--------+ | [2, 3] | +--------+ # The following query returns empty because the index is out of bound. It will return normally when the index is within the bound. nebula> RETURN list[1, 2, 3, 4, 5] [0..10] AS a; +-----------------+ | a | +-----------------+ | [1, 2, 3, 4, 5] | +-----------------+ nebula> RETURN list[1, 2, 3] [-5..5] AS a; +-----------+ | a | +-----------+ | [1, 2, 3] | +-----------+ # The following query returns empty because there is a [0..0]. nebula> RETURN list[1, 2, 3, 4, 5] [0..0] AS a; +----+ | a | +----+ | [] | +----+ # The following query returns empty because of M ≥ N. nebula> RETURN list[1, 2, 3, 4, 5] [3..1] AS a; +----+ | a | +----+ | [] | +----+ # When conduct a range query, if M or N is null, return null. nebula> WITH list[1,2,3] AS a \ RETURN a[0..null] as r; +----------+ | r | +----------+ | __NULL__ | +----------+ # The following query calculates the elements in the list [1,2,3,4,5] respectively and returns them without the list head. nebula> RETURN tail([n IN range(1, 5) | 2 * n - 10]) AS a; +-----------------+ | a | +-----------------+ | [-6, -4, -2, 0] | +-----------------+ # The following query takes the elements in the list [1,2,3] as true and return. nebula> RETURN [n IN range(1, 3) WHERE true | n] AS r; +-----------+ | r | +-----------+ | [1, 2, 3] | +-----------+ # The following query returns the length of the list [1,2,3]. nebula> RETURN size(list[1,2,3]); +-------------------+ | size(list[1,2,3]) | +-------------------+ | 3 | +-------------------+ # The following query calculates the elements in the list [92,90] and runs a conditional judgment in a where clause. nebula> GO FROM "player100" OVER follow WHERE properties(edge).degree NOT IN [x IN [92, 90] | x +$\$.player.age] \
YIELD dst(edge) AS id, properties(edge).degree AS degree;
+-------------+--------+
| id          | degree |
+-------------+--------+
| "player101" | 95     |
| "player102" | 90     |
+-------------+--------+

# The following query takes the query result of the MATCH statement as the elements in a list. Then it calculates and returns them.
nebula> MATCH p = (n:player{name:"Tim Duncan"})-[:follow]->(m) \
RETURN [n IN nodes(p) | n.age + 100] AS r;
+------------+
| r          |
+------------+
| [142, 136] |
| [142, 133] |
+------------+


## OpenCypher compatibility¶

• In openCypher, return null when querying a single out-of-bound element. However, in nGQL, return OUT_OF_RANGE when querying a single out-of-bound element.
nebula> RETURN range(0,5)[-12];
+-------------------+
| range(0,5)[-(12)] |
+-------------------+
| OUT_OF_RANGE      |
+-------------------+

• A composite data type (i.e., set, map, and list) CAN NOT be stored as properties for vertices or edges.

It is recommended to modify the graph modeling method. The composite data type should be modeled as an adjacent edge of a vertex, rather than its property. Each adjacent edge can be dynamically added or deleted. The rank values of the adjacent edges can be used for sequencing.

• Patterns are not supported in the list. For example, [(src)-[]->(m) | m.name].

Last update: January 14, 2022