- Categories:
Aggregate functions (General Aggregation) , Semi-structured and structured data functions (Array/Object)
ARRAY_ SUM¶
Aggregates 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.
- See also:
Syntax¶
Arguments¶
arrayA column of structured ARRAY values whose element type is floating-point:
ARRAY(FLOAT),ARRAY(DOUBLE), orARRAY(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:
-
A whole-row
NULLis ignored, as it is with SUM. A row whose array value isNULLdoesn’t contribute to the result. -
A
NULLelement contributes nothing to its position. If a position isNULLin every array in the group, the result isNULLat that position. -
The input must be a structured floating-point array. Untyped ARRAY values, and arrays with integer,
NUMBER, orDECFLOATelements, aren’t supported. -
Floating-point summation uses the same compensated accumulation as SUM, so results match scalar
SUMover the same values. If the sum at a position exceeds the range of the element type, that position overflows to infinity.
Examples¶
Store the values to add in an ARRAY(DOUBLE) column, then call ARRAY_SUM with that column. Use GROUP BY when you
want one result array for each group. For example, the following query sums the daily channel totals for each store:
A row whose array value is NULL is ignored:
A NULL element contributes nothing to its position, while the other positions still accumulate. A position is NULL in
the result only when every input array has NULL at that position:
If the arrays in a group have different lengths, the function returns an error: