Declaring ARRAY data types in table definitions
Array types are declared usingARRAY(<type>) where <type> can be any data type that Firebolt supports. This includes the ARRAY data type, so arrays can be arbitrarily nested.
If you load an array from a CSV file, the arrays in the CSV file must be enclosed in double quotes ("").
For example, if a CSV file contains a row containing value1 , value2 , "[array_value3 , array_value4]", you can create a table using the following code to read array_value3 and array_value4 into array_values.
SELECT statement shown below is valid.
Basis for examples
All examples in this topic are based on theplayers table from the example database. The platforms column is of type ARRAY(TEXT).
Rows: 5Execution time: 14.26ms
Comparing values against arrays
You can compare a scalar value against the elements of an array using the ANY/ALL (array) quantified comparison operators.ANY returns TRUE if the comparison holds for at least one element, and ALL returns TRUE if it holds for every element.
Rows: 1Execution time: 8.31ms
Simple array functions
There are several fundamental functions that you can use to work with arrays, including ARRAY_LENGTH, ARRAY_CONCAT, and ARRAY_FLATTEN. See the respective reference for a full description. Brief examples are shown below.LENGTH example
LENGTH returns the number of elements in an array.
Rows: 5Execution time: 15.38ms
ARRAY_CONCAT example
ARRAY_CONCAT combines multiple arrays into a single array.
Rows: 3Execution time: 11.37ms
ARRAY_FLATTEN example
ARRAY_FLATTEN converts an ARRAY of ARRAYs into a single flattened ARRAY. Note that this operation flattens only one level of the array hierarchy.
Rows: 1Execution time: 7.58ms
Manipulating arrays with Lambda functions
Firebolt Lambda functions are a powerful tool that you can use on arrays to extract results. Lambda functions iteratively perform an operation on each element of one or more arrays. Arrays and the operation to perform are specified as arguments to the Lambda function.Lambda function general syntax
The general syntax pattern of a Lambda function is shown below. For detailed syntax and examples, see the reference topics for Lambda functions.
Optionally, you can enclose the variables in parentheses:
Lambda function example: single array
Consider the following TRANSFORM array function that uses a single array variable and reference in the Lambda expression. This example applies theUPPER function to each element p in the ARRAY-typed column platforms. This converts each element in each platforms array to upper-case.
Rows: 5Execution time: 11.40ms
Lambda function example: ARRAY_FIRST
ARRAY_FIRST returns the first element for which the Lambda predicate returns true, or NULL if no match is found.Rows: 5Execution time: 12.11ms
Lambda function example: multiple arrays
ARRAY_SORT sorts one array by another. One array represents the values and the other represents the sort order. The example below sorts the first array by the positions defined in the second array.Rows: 1Execution time: 5.27ms
UNNEST
You might want to transform a nested array structure to a standard tabular format.UNNEST serves this purpose.
UNNEST is a table-valued function (TVF) that transforms an input row containing an array into a set of rows.
UNNEST unfolds the elements of the array and duplicates all other columns found in the SELECT clause for each array element.
If the input array is empty, the corresponding row is eliminated.
You can use a single UNNEST command to unnest several arrays if the arrays are the same length.
Multiple UNNEST statements in a single FROM clause result in a Cartesian product. Each element in the first array has a record in the result set corresponding to each element in the second array.
Example: single UNNEST with single ARRAY-typed column
The following example unnests theplatforms column from the players table.
Rows: 8Execution time: 7.65ms
Example: single UNNEST using multiple ARRAY-typed columns
The following query specifies both theplatforms column and ARRAY_ENUMERATE(platforms) to unnest, pairing each element with its position.
Rows: 8Execution time: 6.62ms
Example: multiple UNNEST clauses resulting in a Cartesian product
The following query, while valid, creates a Cartesian product.Rows: 4Execution time: 12.33ms
Example: error on UNNEST of multiple arrays with different lengths
The following query is invalid and will result in an error as theplatforms and the literal two-element arrays have different lengths.
ARRAY input and output syntax
ARRAY values can be converted from and to TEXT. This happens, for example, when an explicit CAST is added to a query, or when ARRAY values are (de-)serialized in a COPY statement.
Converting ARRAY to TEXT
Broadly, theTEXT representation of an ARRAY value starts with an opening curly brace ({). This is followed by the TEXT representations of the individual array elements separated by commas (,).
It ends with a closing curly brace (}). NULL array elements are represented by the literal string NULL. For example, the query
Rows: 1Execution time: 5.55ms
ARRAY values containing TEXT elements to TEXT, some additional rules apply. Specifically, array elements are enclosed by double quotes (") in the following cases:
- The array element is an empty string.
- The array element contains curly or square braces (
{,[,],}), commas (,), double quotes ("), backslashes (\), or white space. - The array element matches the word
NULL(case-insensitively).
TEXT value '{1,2,3,4,NULL,"","{impostor,array}","[\"impostor\",\"array\",\"back\\slash\"]"," padded and spaced ","only spaced","null"}'.
Converting TEXT to ARRAY
When converting theTEXT representation of an array back to ARRAY, the same quoting and escaping rules as above apply. Unquoted whitespace surrounding array elements is trimmed, but whitespace
contained within array elements is preserved. The array elements themselves are converted according to the conversion rules for the requested array element type. For example, the query
ARRAY(TEXT) value [1,2,3,4,NULL,'','{impostor,array}','["impostor","array","back\slash"]',' padded and spaced ','null','unquoted padded and spaced'].
It is also possible to enclose arrays with square braces ([ and ]) instead of curly braces ({ and }) when converting from TEXT to ARRAY. For example, the query
Rows: 1Execution time: 7.62ms
Nested ARRAYs
Finally, the same procedure applies when converting nestedARRAY values from and to TEXT. For example, the query
Rows: 1Execution time: 6.04ms
TEXT to ARRAY(ARRAY(INTEGER)):
Rows: 1Execution time: 5.63ms