REDUCE
functionsupportedCore SQL functionsReduces 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
REDUCE( <array> , <init> , <lambda_expression> )
Parameters
| Name | Required | Description |
|---|---|---|
array | required | The array that contains the elements to be reduced. The array can be semi-structured or structured. |
init | required | The initial accumulator value. |
lambda_expression | required | A lambda expression that defines the reduce logic on each array element. The lambda expression must be specified in the following syntax:Copy code <acc> [ <datatype> ] , <value> [ <datatype> ] -> <expr> The acc argument is the accumulator, and the value argument 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 value argument 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 value argument can have intermediate NULL values. For an example, see Skip NULL values in an array.