Working with Arrays
An Array is an ordered collection of zero or more VARCHAR values.
Actions
Casting
Cast values to array type:
Example:
Create
Array Literal
Create an array using literal notation:
Example:
Split String
Create an array by splitting a string:
Example:
Reading
Subscript Access
Access individual elements by index:
Example:
Get Length
Get the number of elements in an array:
Example:
Get Minimum/Maximum
Get the smallest or largest value in an array:
Example:
Comparing
Equality
Compare arrays for equality:
ANY (Comparison)
The ANY function is used to apply a filter to each item in an array, and returns True if any item in the array matches the condition.
Example:
Supported operators: =, !=, >, <, >=, <=
ANY (Similarity)
The ANY modifier is used to perform a similarity search against all of the items in an array, and returns True if any item in the array matches the pattern.
Example:
Supported operators: LIKE, NOT LIKE, ILIKE, NOT ILIKE
ALL
The ALL function is used to apply a filter to each item in an array, and returns True if all items in the array match the condition.
Example:
Note
ALL currently supports a subset of operators: =, !=
Containment Testing
ARRAY_CONTAINS
Test if an array contains a specific value:
Example:
ARRAY_CONTAINS_ANY
Test if an array contains any of the specified values:
Or using the @> operator:
The @> operator also supports JSON Path expressions for more complex queries on nested structures.
Example:
ARRAY_CONTAINS_ALL
Test if an array contains all of the specified values:
Or using the @>> operator:
The @>> operator also supports JSON Path expressions for more complex queries on nested structures.
Example:
IN Operator
Test if a value is in a list of values:
Example:
IN UNNEST
Test if a value exists in an array column:
Example:
Transforms
Sort Array
Sort an array in ascending order:
Example:
Converting Lists to Relations
Using UNNEST
UNNEST allows you to create a single column relation either as a list of literals, or from a column of LIST type in a dataset.
Limitations
Lists have the following limitations
- Statements cannot
ORDER BYa list column - Lists cannot be used in comparisons
Note
Some restrictions may be resolved by the query optimizer, for example, Projection Pushdown may remove list columns as part of optimization. However, you should not rely on the optimizer to take any particular action.