Returns an array whose elements are normalized based on the specified p norm.
Syntax
array_normalize(array, p)This function is equivalent to TRANSFORM(array, v -> v / REDUCE(array, 0, (a, v) -> a + POW(ABS(v), p), a -> POW(a, 1 / p)), but calls REDUCE only once regardless of array length.
Parameters
| Parameter | Description | Constraints |
|---|---|---|
array | The input array to normalize. | Elements must be FLOAT or DOUBLE. |
p | The p norm of the array. | Must be >= 0. If p = 0, the original array is returned. If p < 0, an error is returned. |
Return value
An array whose elements are normalized based on the specified p norm.
Returns null if
arrayis null or contains any null element.Returns the original array if
p = 0.Returns an error if
p < 0.
Examples
L1 normalization
SELECT array_normalize(array(10.0, 20.0, 50.0), 1.0);Result:
[0.125, 0.25, 0.625]The absolute values sum to 1: 0.125 + 0.25 + 0.625 = 1.0.
Null input
SELECT array_normalize(array(1.0, null, 3.0), 2.0);Result:
nullRelated functions
ARRAY_NORMALIZE is a complex type function. For more information about functions that process ARRAY, MAP, STRUCT, and JSON data types, see Complex type functions.