ARRAY_SUM
functionsupportedCore SQL functionsAggregates a column of structured ARRAY values by summing them element by element. The value at each position in the result is the sum of the values at that position across all of the input arrays. Unlike SUM, which adds up scalar values, this function adds up arrays position by position and returns an array. Unlike VECTOR_SUM, which operates on 32-bit VECTOR values, this function operates on structured arrays of 64-bit floating-point elements.
Syntax
ARRAY_SUM( <array> )
Parameters
| Name | Required | Description |
|---|---|---|
array | required | A column of structured ARRAY values whose element type is floating-point: ARRAY(FLOAT), ARRAY(DOUBLE), or ARRAY(REAL). |
Returns
Returns a structured ARRAY with the same element type as the input. Each element in the result is the sum of the elements at the corresponding position across all of the input arrays. The function returns NULL if the group contains no rows or if every value in the group is NULL.
Usage notes
- All non-NULL arrays in a group must have the same length. If two arrays have different lengths, the function returns the following error: Input to ARRAY_SUM has mismatched lengths: expected <length> but got <length>
- A whole-row NULL is ignored, as it is with SUM. A row whose array value is NULL doesn’t contribute to the result.
- A NULL element contributes nothing to its position. If a position is NULL in every array in the group, the result is NULL at that position.
- The input must be a structured floating-point array. Untyped ARRAY values, and arrays with integer, NUMBER, or DECFLOAT elements, aren’t supported.
- Floating-point summation uses the same compensated accumulation as SUM, so results match scalar SUM over the same values. If the sum at a position exceeds the range of the element type, that position overflows to infinity.