VECTOR_DISTANCE scalar function

Returns the distance between two vectors.

The VECTOR_DISTANCE function takes two vectors as input parameters, and returns the distance between them based on the specified distance metric.

This function is designed for similarity search use cases and allows users to compute the distance between two vectors.

Read syntax diagramSkip visual syntax diagramVECTOR_DISTANCE(vector1, vector2, metric)

The schema is SYSIBM.

vector1
A VECTOR value that represents the first vector used to compute the distance. The vector1 vector must have the same coordinate-type and dimension as vector2.
vector2
A VECTOR value that represents the second vector used to compute the distance. The vector2 vector must have the same coordinate-type and dimension as vector1.
metric
Identifies the metric used to compute the distance between vector1 and vector2. The following metrics are supported:
  • COSINE
  • EUCLIDEAN
  • EUCLIDEAN_SQUARED
  • DOT
  • HAMMING
  • MANHATTAN

Result

The result of the function is a DOUBLE value representing the computed distance between vector1 and vector2, based on the specified metric. The result can be null if either VECTOR argument can be null or if the dft_sqlmathwarn database configuration parameter is set to YES; The result is the null value when either VECTOR argument is NULL, or when the dft_sqlmathwarn database configuration parameter is set to YES and a division-by-zero error occurs.

Examples

The following example shows the command syntax for computing the distance between a pair of identical VECTOR values.
SELECT VECTOR_DISTANCE(VECTOR('[-0.0762727, 0.0559632]', 2, REAL),
                       VECTOR('[-0.0762727, 0.0559632]', 2, REAL), COSINE) as DISTANCE
FROM SYSIBM.SYSDUMMY1

DISTANCE
------------------------
  +0.00000000000000E+000

  1 record(s) selected.