Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API
Computes the cosine similarity between two vectors, measuring directional alignment.
Syntax
cosine_similarity(<array1>, <array2>)
Arguments
Required
<array1>: The first vector (array(real)orarray(double-precision)).<array2>: The second vector (array(real)orarray(double-precision)).
Returns
Returns a double-precision value between -1 and 1, where 1 indicates identical direction, 0 indicates orthogonal, and -1 indicates opposite direction.
Considerations
- Both input arrays must be of the same element type and length.
- If any element is NULL, the result is NULL.
- Zero vectors cause division by zero.
- For optimal performance, use array definitions with non-nullable elements.
Examples
Calculate Cosine Similarity
Compute the cosine similarity between two vectors.
SELECT cosine_similarity(array[1, 2, 3], array[-1, 2, -3]) AS result;
Returns approximately -0.42857, indicating the vectors are somewhat opposed in direction.