vector_norm function
Applies to: Databricks Runtime 18.1 and above
Computes the Lp norm of a vector using the specified degree.
Syntax
vector_norm(vector [, degree ])
Arguments
- vector: An
ARRAY<FLOAT>expression representing the vector. - degree: Optional. A
FLOATvalue specifying the norm type; defaults to 2.0 (Euclidean norm). Supported values:- 1.0 — L1 norm (Manhattan): sum of absolute values
- 2.0 — L2 norm (Euclidean): square root of sum of squares
float('inf')— L∞ (infinity norm): maximum absolute value
Returns
A FLOAT value representing the norm of the vector. The result is non-negative.
Returns 0.0 for empty vectors. Returns NULL if the input is NULL or contains NULL.
Notes
- Only
ARRAY<FLOAT>is supported; other types such asARRAY<DOUBLE>orARRAY<DECIMAL>raise an error. An unsupporteddegreevalue raises INVALID_VECTOR_NORM_DEGREE. - L2 norm is the most common in dense embedding workloads; L1 is used in sparse and probabilistic applications; L∞ is used in worst-case analysis.
Error conditions
Examples
SQL
-- L2 norm (Euclidean) - classic 3-4-5 triangle
> SELECT vector_norm(array(3.0f, 4.0f), 2.0f);
5.0
-- L1 norm (Manhattan)
> SELECT vector_norm(array(3.0f, 4.0f), 1.0f);
7.0
-- L∞ norm (infinity)
> SELECT vector_norm(array(3.0f, 4.0f), float('inf'));
4.0
-- Empty vector returns 0.0
> SELECT vector_norm(array(), 2.0);
0.0