Vector Functions
- reference
Learn more about the vector search functions available in {sqlppea}.
Operational Insights vector functions compute a distance, or similarity, value between a stored vector field and a query vector. SQL++ for Operational Insights provides 2 kinds of vector functions:
-
K-nearest neighbor (KNN) functions compute the exact distance between a pair of vectors. A query that calls one without a matching
VTREEindex applies it to every record in the collection in turn, which scans the whole collection. Use KNN functions when no compatible index exists, or when you need an exact result rather than an approximation. -
Approximate nearest neighbor (ANN) functions use a
VTREEvector index to prune the search to a subset of clusters, trading a small amount of recall for query speed. For more information about creating aVTREEindex, see CREATE a Vector Index.
The examples in this topic use an Entertainment.Movie collection, containing movie records with an embedding field.
This example field is a 384-dimensional array of floating point numbers derived by embedding each movie’s title and overview.
cosine_similarity
Computes the exact cosine similarity between vector1 and vector2.
Unlike a distance function, a higher cosine_similarity value indicates a closer match.
Arguments
-
vector1is the first vector, expressed as an array of floating point numbers. -
vector2is the second vector, expressed as an array of floating point numbers.
Return Value
-
A floating point number between -1 and 1 representing the cosine similarity between
vector1andvector2. -
missing, if either vector argument ismissing. -
null, if either vector argument isnull. -
An empty result, with a warning, if either vector argument has the wrong (non-numeric) data type, or the 2 vectors have mismatched dimensions.
dot_product
Computes the dot product between vector1 and vector2.
Arguments
-
vector1is the first vector, expressed as an array of floating point numbers. -
vector2is the second vector, expressed as an array of floating point numbers.
Return Value
-
A floating point number representing the dot product of
vector1andvector2. -
missing, if either vector argument ismissing. -
null, if either vector argument isnull. -
An empty result, with a warning, if either vector argument has the wrong (non-numeric) data type, or the 2 vectors have mismatched dimensions.
euclidean_distance
Computes the Euclidean, or L2, distance between vector1 and vector2.
Arguments
-
vector1is the first vector, expressed as an array of floating point numbers. -
vector2is the second vector, expressed as an array of floating point numbers.
Return Value
-
A floating point number representing the Euclidean distance between
vector1andvector2. -
missing, if either vector argument ismissing. -
null, if either vector argument isnull. -
An empty result, with a warning, if either vector argument has the wrong (non-numeric) data type, or the 2 vectors have mismatched dimensions.
euclidean_squared_distance
Computes the squared Euclidean, or L2 squared, distance between vector1 and vector2.
Squared Euclidean distance omits the final square root calculation, so it’s cheaper to compute than euclidean_distance while preserving the same distance ordering.
Arguments
-
vector1is the first vector, expressed as an array of floating point numbers. -
vector2is the second vector, expressed as an array of floating point numbers.
Return Value
-
A floating point number representing the squared Euclidean distance between
vector1andvector2. -
missing, if either vector argument ismissing. -
null, if either vector argument isnull. -
An empty result, with a warning, if either vector argument has the wrong (non-numeric) data type, or the 2 vectors have mismatched dimensions.
vector_distance
Computes the exact distance between vector1 and vector2.
Use vector_distance for an exact K-nearest neighbor (KNN) result, or when no applicable VTREE index exists.
Arguments
-
vector1is the first vector, expressed as an array of floating point numbers. -
vector2is the second vector, expressed as an array of floating point numbers. -
distance_functionis a string specifying the distance metric:cosine,dot,l2oreuclidean, orl2_squaredoreuclidean_squared.
Return Value
-
A floating point number representing the exact distance between
vector1andvector2, according todistance_function. -
missing, if either vector argument ismissing. -
null, if either vector argument isnull. -
An error, if
distance_functionis invalid. -
An empty result, with a warning, if either vector argument has the wrong (non-numeric) data type, or the 2 vectors have mismatched dimensions.
Example
Find the exact 50 nearest movies to a given query vector by cosine distance, without using an index.
USE Entertainment;
LET qvec = [ /* 384-dimensional float array */ ];
FROM Movie mov
LET dist = vector_distance(mov.embedding, qvec, "cosine")
SELECT mov.id, mov.title, mov.release_date, dist
ORDER BY dist
LIMIT 50;
ann_distance
Computes the approximate distance between vector_field and query_vector when an eligible VTREE index exists and applies to the query.
Otherwise, computes the exact distance.
APPROX_VECTOR_DISTANCE is an alias for ANN_DISTANCE.
Syntax
ann_distance(vector_field, query_vector, distance_function)
ann_distance(vector_field, query_vector, distance_function, min_probe_fraction)
ann_distance(vector_field, query_vector, distance_function, min_probe_fraction, k_multiplier)
Arguments
-
vector_fieldis a reference to the vector field to search. It must match the vector field declared when the index was created. -
query_vectoris the query vector, expressed as an array of floating point numbers. -
distance_functionis a string specifying the distance metric. It must match thesimilarityparameter used when aVTREEindex was created in order for the index to be used. Otherwise, the query falls back to exact KNN evaluation. SQL++ for Operational Insights supportsdotsimilarity only for normalized embeddings in this release. -
min_probe_fractionis optional. It’s a float ranging from 0.0 to 1.0 that sets the fraction of the epsilon-narrowed shortlist of leaf-level candidates to probe. If you omit this argument, or pass0, Operational Insights uses the index’s default probe behavior.
Default:0.75 -
k_multiplieris optional. It’s an integer that sets how many candidates, expressed asktimes this multiplier, the ANN operator retrieves from each storage partition before final ranking. If you omit this argument, Operational Insights uses the default multiplier.
Default:1
Return Value
-
A floating point number representing the distance between
vector_fieldandquery_vector, according todistance_function. -
missing, if either vector argument ismissing. -
null, if either vector argument isnull. -
An error, if
distance_functionis invalid. -
An empty result, with a warning, if either vector argument has the wrong (non-numeric) data type, or the 2 vectors have mismatched dimensions.
-
If no eligible
VTREEindex exists onvector_field, or the query is not shaped to use one, SQL++ for Operational Insights evaluates the query as an exact KNN scan instead.
Example
Find the 50 movies with an embedding closest to a given query vector, using cosine distance and the index’s default probe settings.
USE Entertainment;
LET qvec = [ /* 384-dimensional float array */ ];
FROM Movie mov
LET dist = ann_distance(mov.embedding, qvec, "cosine")
SELECT mov.id, mov.title, mov.release_date, dist
ORDER BY dist
LIMIT 50;
Trade off slower performance for higher recall by probing an extra 20% of the VTREE search’s candidate cluster shortlist and doubling the number of candidates retrieved from each storage partition before performing the final ranking.
USE Entertainment;
LET qvec = [ /* 384-dimensional float array */ ];
FROM Movie mov
LET dist = ann_distance(mov.embedding, qvec, "cosine", 0.2, 2)
SELECT mov.id, mov.title, mov.release_date, dist
ORDER BY dist
LIMIT 50;
approx_vector_distance
An alias for ann_distance.
Both names accept the same arguments and return the same result.
isvector
Returns whether vec is a usable vector, meaning a non-empty array whose elements are all numeric values, of a given length when you specify dimension.
A record whose vector field satisfies isvector is eligible to be indexed with a VTREE index.
IS_VECTOR is an alias for ISVECTOR.
Arguments
-
vecis any value to test. -
dimensionis optional. It’s an integer specifying the required number of elements.
Return Value
-
true, ifvecis an array whose elements are all numeric values, and, when you specifydimension, it has exactly that many elements. -
false, ifvecis a non-null, non-missingvalue that’s not an array, is an empty array, or contains a non-numeric element.falsealso results when you specifydimensionand the element count does not match. -
missing, if either argument ismissing. -
null, if either argument isnull. -
An error, if
dimensionis not a numeric value.