COSINE_SIMILARITY()

All functions > ARRAY > COSINE_SIMILARITY()

Calculates the cosine similarity between two arrays.

Signatures

Returns: Cosine similarity between -1 and 1

COSINE_SIMILARITY(array1: ARRAY<DOUBLE>, array2: ARRAY<DOUBLE>) → DOUBLE
sql
ParameterTypeRequiredDescription
array1ARRAY<DOUBLE>YesFirst vector (array of doubles)
array2ARRAY<DOUBLE>YesSecond vector (array of doubles)

Notes

  • Measures angle between two vectors
  • Formula: dot(x,y) / (||x|| * ||y||)
  • Returns value between -1 (opposite) and 1 (same direction)
  • Arrays must be same length
  • Used in text similarity and recommendation systems

See also

Examples

FeatureQL
SELECT
    f1 := COSINE_SIMILARITY(ARRAY(1.0E0, 0.0E0), ARRAY(1.0E0, 0.0E0)), -- Identical vectors have similarity 1
    f2 := COSINE_SIMILARITY(ARRAY(1.0E0, 0.0E0), ARRAY(0.0E0, 1.0E0)), -- Orthogonal vectors have similarity 0
    f3 := COSINE_SIMILARITY(ARRAY(1.0E0, 0.0E0), ARRAY(-1.0E0, 0.0E0)) -- Opposite vectors have similarity -1
;
Result
f1 VARCHARf2 VARCHARf3 VARCHAR
1.00.0-1.0

Last update at: 2026/05/26 17:22:09