Skip to main content

vector_inner_product function

Applies to: check marked yes Databricks Runtime 18.1 and above

Computes the inner product (dot product) between two vectors.

Syntax

vector_inner_product(vector1, vector2)

Arguments

  • vector1: An ARRAY<FLOAT> expression representing the first vector.
  • vector2: An ARRAY<FLOAT> expression representing the second vector.

Returns

A FLOAT value representing the inner product of the two vectors.

Returns 0.0 for empty vectors. Returns NULL if either input is NULL or contains NULL.

Notes

  • Only ARRAY<FLOAT> is supported; other types such as ARRAY<DOUBLE> or ARRAY<DECIMAL> raise an error.
  • Both vectors must have the same dimension; otherwise the function raises VECTOR_DIMENSION_MISMATCH.
  • For normalized vectors, the inner product equals cosine similarity. Commonly used with Maximum Inner Product Search (MIPS) algorithms.

Error conditions

Examples

SQL
-- Basic inner product
> SELECT vector_inner_product(array(1.0f, 2.0f, 3.0f), array(4.0f, 5.0f, 6.0f));
32.0

-- Inner product of orthogonal unit vectors
> SELECT vector_inner_product(array(1.0f, 0.0f), array(0.0f, 1.0f));
0.0

-- Inner product with itself (squared L2 norm)
> SELECT vector_inner_product(array(3.0f, 4.0f), array(3.0f, 4.0f));
25.0