Array Operations
An array type can be created by applying the ARRAY suffix to another
type. For example INT ARRAY is an array of integers. Array indexes
start from 1. Array sizes are limited to 2^31 elements. Array values
can be nullable types, e.g., INT ARRAY NULL. Multidimensional arrays
are possible, e.g. VARCHAR ARRAY ARRAY is a two-dimensional array.
Many array functions take an index or a length represented as a 32-bit signed integer, which limits the practical size of an array. An argument outside the 32-bit range generally causes a runtime error.
In CREATE TABLE and CREATE TYPE declarations there is no way to
specify the nullability of the elements of an ARRAY. The compiler
will always assume that array elements are nullable:
CREATE TABLE t(a INT ARRAY);
Table T will have a single column a whose values are nullable
arrays; the array elements will be nullable INT values.
Array literals
Array literals have the syntax ARRAY[expr [,expr]*]. An example
creating a nested array is: ARRAY[ARRAY[1, 2], ARRAY[3, 4]].
The UNNEST SQL Operator
The UNNEST operator takes an ARRAY and returns a table with a
row for each element in the ARRAY: UNNEST(ARRAY) [WITH ORDINALITY]. If the input is an array with 5 elements, the output
is a table with 5 rows, each row holding one element of the array.
The additional keyword WITH ORDINALITY creates an output table
with two columns, where the second column is the index of the element
within the array, with numbering starting at 1. If the array contains
duplicated values, the resulting table will be a multiset.
The UNNEST operator can be used in self-joins as follows:
CREATE TABLE data(cities VARCHAR ARRAY, country VARCHAR);
CREATE VIEW v AS SELECT city, country
FROM data, UNNEST(cities) AS t (city);
The previous query is a shortcut for a CROSS-JOIN query:
CREATE VIEW v AS SELECT city, data.country
FROM data CROSS JOIN UNNEST(data.cities) AS city;
CROSS APPLY is another spelling for the same query:
CREATE VIEW v AS SELECT city, data.country
FROM data CROSS apply UNNEST(data.cities) AS city;
UNNEST applied to a NULL value returns an empty table. As a
consequence, the queries above drop the rows of data where cities
is NULL or empty. LEFT JOIN UNNEST(...) ON TRUE and OUTER APPLY UNNEST(...) keep such rows: each one produces a single row where all
the columns produced by UNNEST, including the ORDINALITY column,
are NULL:
-- A row with a NULL or empty cities array produces one row with a NULL city
CREATE VIEW v AS SELECT city, data.country
FROM data LEFT JOIN UNNEST(data.cities) AS city ON TRUE;
CREATE VIEW w AS SELECT city, data.country
FROM data OUTER APPLY UNNEST(data.cities) AS city;
The join condition of LEFT JOIN UNNEST must be ON TRUE; see
unsupported operations.
Note that applying UNNEST to an ARRAY of structure-typed objects
will produce a collection whose columns are the fields of the
structure, instead of a collection with a single structure-typed
column.
Comparison Operations on Arrays
Comparison operations (=, <>, !=, >, <, >=, <=) in arrays occur lexicographically, from left to right. The elements are compared one by one until a difference is found. Arrays can be compared even if they are different in size.
A NULL array element is smaller than any other element,
whatever the NULLS FIRST or NULLS LAST clause of an enclosing ORDER BY says.
On arrays, = is equivalent to IS NOT DISTINCT FROM and <> is
equivalent to IS DISTINCT FROM: two NULL elements compare as
equal, so ARRAY[1, NULL] = ARRAY[1, NULL] is true. >, <,
>=, and <= are rejected on arrays that contain ROW values. See
comparing complex values.
Examples:
[45] > [22][23, 56, 16] > [23, NULL][NULL] < [1]