The L2_DISTANCE function calculates the L2 Euclidean distance between two vectors.
Note
L2 distance, also known as Euclidean distance, measures how far apart two vectors are in a vector space. It is the square root of the sum of the squared differences between the corresponding elements of the two vectors. A distance of zero means the two vectors are identical. A larger distance indicates that the vectors are farther apart.
Syntax
FLOAT L2_DISTANCE( <vector>, <vector> )Parameters
vector: Required. An expression of the VECTOR data type.
Return value
Returns a FLOAT representing the Euclidean distance between the two vectors.
Vector functions are optimized for performance, which can cause a minor loss of floating-point precision. The result of this function has a maximum error of 1e-4.
Examples
-- Enable the VECTOR data type.
set odps.sql.type.vector.enable=true;
set odps.sql.type.system.odps2=true;
-- Prepare data.
CREATE TABLE vectors (a VECTOR(FLOAT, 3), b VECTOR(FLOAT, 3));
INSERT INTO vectors SELECT CAST(ARRAY(1.1,2.2,3.0) AS VECTOR(FLOAT,3)), CAST(ARRAY(1.0,1.0,1.0) AS VECTOR(FLOAT,3));
INSERT INTO vectors SELECT CAST(ARRAY(1.0,2.2,3.0) AS VECTOR(FLOAT,3)), CAST(ARRAY(4.0,6.0,8.0) AS VECTOR(FLOAT,3));
-- Calculate the Euclidean distance between vectors a and b.
SELECT L2_DISTANCE(a, b) FROM vectors;
-- Result
+------------+
| _c0 |
+------------+
| 5.449999809265137 |
| 48.439998626708984 |
+------------+该文章对您有帮助吗?