Learn techniques to manipulate and transform arrays in Firebolt.
ARRAY(<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.
visits
. The column id
is of type INTEGER
. All other columns are of type ARRAY(TEXT)
.
LENGTH
returns the number of elements in an array.
ARRAY_CONCAT
combines multiple arrays into a single array.
ARRAY_FLATTEN
converts an ARRAY of ARRAYs into a single flattened ARRAY. Note that this operation flattens only one level of the array hierarchy.
Parameter | Description |
---|---|
<LAMBDA_FUNC> | Any array function that accepts a Lambda expression as an argument. For a list, see Lambda functions. |
<arr1_var>[, <arr2_var>][, ...<arrN_var>] | A list of one or more variables that you specify. The list is specified in the same order and must be the same length as the list of array expressions (<array1>[, <array2>][, ...<arrayN>] ). At runtime, each variable contains an element of the corresponding array. The specified <operation> is performed for each variable. |
<operation> | The operation that is performed for each element of the array. This is typically a function or Boolean expression. |
<array1>[, <array2>][, ...<arrayN>] | A comma-separated list of expressions, each of which evaluates to an ARRAY data type. |
UPPER
function to each element t
in the ARRAY
-typed column tags
. This converts each element in each tags
array to upper-case.
ARRAY_FIRST
uses a Boolean expression that you specify to find the key in the key array. If the Boolean expression resolves to true, the function returns the first value in the value array that corresponds to the key’s element position. If there are duplicate keys, only the first corresponding value is returned.
The example below returns the first value in the agent_props_vals
array where the corresponding position in the agent_props_keys
array contains the key platform
.
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.
tags
column from the visits
table.
agent_props_keys
and agent_props_vals
columns to unnest.
tags
and agent_props_keys
arrays have different lengths for row 1.
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.
TEXT
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
TEXT
value '{1,2,3,4,NULL}'
.
When converting ARRAY
values containing TEXT
elements to TEXT
, some additional rules apply. Specifically, array elements are enclosed by double quotes ("
) in the following cases:
{
,[
,]
,}
), commas (,
), double quotes ("
), backslashes (\
), or white space.NULL
(case-insensitively).TEXT
value '{1,2,3,4,NULL,"","{impostor,array}","[\"impostor\",\"array\",\"back\\slash\"]"," padded and spaced ","only spaced","null"}'
.
TEXT
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
ARRAY(INTEGER)
value [1,2,3,4,NULL]
.
ARRAY
values from and to TEXT
. For example, the query
TEXT
value {NULL,{},{1,2},{3,4}}
, and the query
ARRAY(ARRAY(INTEGER))
value [NULL,[],[NULL],[1,2],[3,4]]
.