SQL Vector Functions and Operators
NuoDB provides functions to compute a value representing the similarity between two vectors. These functions use vector algebra functions to calculate distances or angles between the vectors.
Syntax
| Function Syntax | Description | ||
|---|---|---|---|
|
L1 distance (also called Manhattan distance) between the two vectors. Formula used:
Return type: DOUBLE Example:
|
||
|
L2 distance (also called Euclidean distance) between the two vectors. Formula used:
Return type: DOUBLE Example:
|
||
|
For normalized vectors (length 1) this is identical to the Formula used:
Return type: DOUBLE Example:
|
||
|
For normalized vectors this is the same as the Formula used: Return type: DOUBLE in the range -1 to 1. Example:
|
These functions are often used in vector similarity search applications to determine similar embeddings vectors.
For vector_l1_distance and vector_l2_distance, smaller values indicate that the vectors are closer to each other (more similar).
For vector_cosine_distance, a value of 1 means the vectors point in the same direction, 0 means they are orthogonal, and -1 means they point in opposite directions.
Example
Compare colors from the RGB color wheel and find the closest match to the orange-ish color [255,100,0] using vector similarity search.
CREATE TABLE COLORS(NAME STRING, RGB VECTOR(3,DOUBLE));
INSERT INTO COLORS VALUES
('red', '[255,0,0]'), ('green', '[0,255,0]'), ('blue', '[0,0,255]');
SELECT name, VECTOR_COSINE_DISTANCE(RGB, '[255,100,0]')
FROM COLORS ORDER BY VECTOR_COSINE_DISTANCE(RGB, '[255,100,0]') DESC LIMIT 1;
NAME [VECTOR_COSINE_DISTANCE]
----- -------------------------
red 0.9309731984870635