- Categories:
Semi-structured and structured data functions (Higher-order)
REDUCE¶
Reduces an array to a single value based on the logic in a lambda expression.
The REDUCE function takes an array, an initial accumulator value, and a lambda function. It applies the lambda function to each element of the array, updating the accumulator with each result. After processing all elements, REDUCE returns the final accumulator value.
Syntax¶
Arguments¶
arrayThe array that contains the elements to be reduced. The array can be semi-structured or structured.
initThe initial accumulator value.
lambda_expressionA lambda expression that defines the reduce logic on each array element.
The lambda expression must be specified in the following syntax:
The
accargument is the accumulator, and thevalueargument is the current element being processed in the array.
Returns¶
This function can return a value of any data type.
If the input array is empty, then the function returns the initial value of the accumulator.
The function returns NULL in these cases:
The input array is NULL.
The initial value of the accumulator is NULL.
The lambda function returns NULL.
Usage notes¶
When the data type for a lambda
valueargument is explicitly specified, the array element is coerced into the specified type before lambda invocation. For information about coercion, see Data type conversion.Type checking enforces that the initial value of the accumulator, the accumulator lambda argument, and the return value of the lambda execution all have the same logical and physical types. If casting is used to meet this requirement, the largest physical type of the three is used.
The
valueargument can have intermediate NULL values. For an example, see Skip NULL values in an array.
Examples¶
The following examples use the REDUCE function.
Calculate the sum of the values in an array¶
Use the REDUCE function to return the sum of the values in an array and specify 0 for the initial
accumulator value:
This example is the same as the previous example, but it specifies a structured array of type INT:
Use the REDUCE function to return the sum of the values in an array and specify 10 for the initial
accumulator value:
Calculate the sum of the square of each value in an array¶
Use the REDUCE function to return the sum of the square of each value in the array, and specify 0
for the initial accumulator value:
Skip NULL values in an array¶
In this example, the array argument includes NULL values. When this array is passed to
the REDUCE function, the accumulator will have intermediate NULL values.
Use the REDUCE function to return the sum of the values in the array, and use the ZEROIFNULL function in the logic of the lambda expression to skip NULL values in the array. The lambda expression uses the ZEROIFNULL function to process each value in the array using the following logic:
If
valis NULL, then the result of the lambda expression isacc + 0.If
valis not NULL, then the result of the lambda expression isacc + val.
Run the query:
Generate string values¶
Use the REDUCE function to return a list of string values by concatenating each value in the array:
Use an array for the accumulator¶
Use the REDUCE function along with the ARRAY_PREPEND function in the logic of the lambda expression to return an array that reverses the order of the input array:
Use conditional logic¶
Use the REDUCE function along with the IFF function in the logic
of the lambda expression to perform an action based on conditional logic similar to an if-then
expression. This example uses the following logic in the lambda expression:
If the array value is less than seven, then square it and add it to the accumulator.
If the array value is greater than or equal to seven, then add it to the accumulator without squaring it.
Reduce an array of elements in a table to a single value¶
Assume you have a table named orders with the columns order_id, order_date, and order_detail. The
order_detail column is an array of the line items, their purchase quantity, and subtotal. The table contains
two rows of data. The following SQL statement creates this table and inserts the rows:
Use the REDUCE function to return the subtotal sum for all items in each order:
Use the REDUCE function to return a list of the items sold in each order:
Reference a table column in a lambda expression to reduce array elements in table data¶
Create a table with one column of type ARRAY and another column of type INT:
Use the REDUCE function to return the sum of the values in the array in each row by adding the value
in col2 to the accumulator value: