githubEdit

Vector / Similarity Functions

Pinot provides built-in vector similarity and utility functions for working with float array columns. These functions are useful for nearest-neighbor search, recommendation systems, and any use case involving embedding vectors.

Both input vectors must have the same number of dimensions. Passing null or mismatched-length vectors will result in an error.

cosineDistance

cosineDistance(vector1, vector2)
cosineDistance(vector1, vector2, defaultValue)

Returns the cosine distance between two vectors, defined as 1 - cosine_similarity. The result ranges from 0 (identical direction) to 2 (opposite direction). If either vector has a norm of zero, the two-argument form returns NaN while the three-argument form returns the specified defaultValue.

SELECT cosineDistance(
  embedding,
  ARRAY(0.1, 0.2, 0.3),
  0.0
)
FROM products
WHERE category = 'electronics'
-- Returns 0.0 for identical vectors, up to 2.0 for opposite vectors

innerProduct

innerProduct(vector1, vector2)

Returns the inner product (sum of element-wise products) of two vectors.

l1Distance

Returns the L1 distance (Manhattan distance) between two vectors, computed as the sum of absolute differences of their components.

l2Distance

Returns the L2 distance (Euclidean distance with square root) between two vectors.

euclideanDistance

Returns the squared Euclidean distance between two vectors (the sum of squared differences without taking the square root). This is faster than l2Distance when you only need to compare relative distances.

dotProduct

Returns the dot product of two vectors. Functionally equivalent to innerProduct.

vectorDims

Returns the number of dimensions (length) of a vector.

vectorNorm

Returns the L2 norm (Euclidean length) of a vector, computed as the square root of the sum of squared components.

Last updated

Was this helpful?