All Products
Search
Document Center

MaxCompute:ARRAY_REDUCE

Last Updated:Sep 06, 2023

Aggregates the elements in Array a.

Syntax

R array_reduce(array<T> <a>, buf <init>, function<buf, T, buf> <merge>, function<buf, R> <final>)

Parameters

  • a: required. This parameter specifies an array. T in array<T> specifies the data type of the elements in the array. The elements can be of any data type.

  • init: required. The initial value of the intermediate result that is used to aggregate elements.

  • merge: required. A built-in function, user-defined function, or expression that is used to perform an operation on each element in Array a and the intermediate result. This function or expression uses the elements of Array a and the init parameter as input parameters.

  • final: required. A built-in function, user-defined function, or expression that is used to convert the intermediate result into the final result. This function or expression uses the result of merge as the input parameter. R specifies the data type of the output.

Return value

The data type of the return value is the same as the data type that is specified for final.

Examples

-- The return value is 6. 
select array_reduce(array(1, 2, 3), 0, (buf, e)->buf + e, buf->buf);
-- The return value is 2.5. 
select array_reduce(array(1, 2, 3, 4), named_struct('sum', 0, 'count', 0), (buf, e)->named_struct('sum', buf.sum + e, 'count', buf.count + 1), buf -> buf.sum / buf.count);

Related functions

  • ARRAY_REDUCE is a complex type function. For more information about functions related to the processing of data of complex data types, such as ARRAY, MAP, STRUCT, and JSON, see Complex type functions.

  • In the preceding examples, the combination of a hyphen and a closing angle bracket (->) is used. For more information about how to use the combination of a hyphen and a closing angle bracket (->) in Lambda functions, see Lambda functions.