Define an array
To define an array in a schema, append[]
to the data type of the column when you define the schema. For example, you can use trip_id VARCHAR[]
to create an array that stores trip IDs.
You can also define a temporary array in an SQL statement in this syntax:
Examples
The following statement defines a temporary array and retrieves the columns in it.x
that has an array of arrays.
taxi
that contains an array trip_id
.
Add values to an array
To add values to an array, in the SQL statement, use ARRAY to indicate that this is an array, and then enclose the data in the array with[]
. For example, ARRAY ['ABCD1234', 'ABCD1235', 'ABCD1236', 'ABCD1237']
.
Examples
Add values to tablex
:
taxi
:
Retrieve data in an array
To retrieve data in an array, use theARRAY_COLUMN[RELATIVE_POSITION]
syntax. Relative positions start from 1. For example, to access ABCD1234
, the first object in the trip_id
array, we can specify trip_id[1]
.
Examples
Retrieve the second element in arraya
from the x
table.
trip_id
from the taxi
table.
Retrieve a slice of an array
To retrieve data in an array, use theARRAY_COLUMN[n:m]
syntax, where n
and m
are integers representing indices and are both inclusive. Either n
, m
, or both can be omitted. Relative positions start from 1. In multidimensional arrays, arrays with unmatching dimensions are allowed.
Examples
Retrieve the entire array withn
omitted.
Differences from PostgreSQL
In RisingWave, assumearr
is of type T[ ][ ][ ]:
- arr[x] is of type T[ ][ ]
- arr[x][y] is interpreted as
[arr[x]](y)
, and of type T[ ] - arr[x0:x1] is of type T[ ][ ][ ]
- arr[x0:x1][y0:y1] is interpreted as
[arr[x0:x1]](y0:y1)
, and of type T[ ][ ][ ] - arr[x0:x1][y] is interpreted as
[arr[x0:x1]](y)
, and of type T[ ][ ]
arr
is still of type T[ ]:
- arr[x] or arr[x][y] is of type T but due to an insufficient number of indices is of
NULL
value - arr[x][y][z] is of type T
- arr[x0:x1][y0:y1][z0:z1] is of type T[ ] and 3-dimensional
- arr[x0:x1] is interpreted as arr[x0:x1][:][:], and of type T[ ] and 3-dimensional
- arr[x0:x1][y] is interpreted as arr[x0:x1][1:y][:], and of type T[ ] and 3-dimensional
Unnest data from an array
You can use theunnest()
function to spread values in an array into separate rows.