メインコンテンツまでスキップ

vector_l2_distance function

Applies to: check marked yes Databricks Runtime 18.1 and above

Computes the Euclidean (L2) distance between two vectors.

Syntax

vector_l2_distance(vector1, vector2)

Arguments

  • vector1: An ARRAY<FLOAT> expression representing the first vector.
  • vector2: An ARRAY<FLOAT> expression representing the second vector.

Returns

A FLOAT value representing the Euclidean distance between the two vectors. The result is non-negative, where 0.0 indicates identical vectors.

Returns 0.0 for empty vectors. Returns NULL if either input is NULL or contains NULL.

Notes

  • Only ARRAY<FLOAT> is supported; other types such as ARRAY<DOUBLE> or ARRAY<DECIMAL> raise an error.
  • Both vectors must have the same dimension; otherwise the function raises VECTOR_DIMENSION_MISMATCH.
  • Lower values indicate greater similarity (closer vectors); also known as Euclidean distance.

Error conditions

Examples

SQL
-- Basic L2 distance
> SELECT vector_l2_distance(array(1.0f, 2.0f, 3.0f), array(4.0f, 5.0f, 6.0f));
5.196152422706632

-- Distance between identical vectors
> SELECT vector_l2_distance(array(1.0f, 2.0f), array(1.0f, 2.0f));
0.0

-- Classic 3-4-5 triangle
> SELECT vector_l2_distance(array(0.0f, 0.0f), array(3.0f, 4.0f));
5.0