The following table shows supported vector functions. These are not supported with dictionary-encoded columns. These were introduced in MySQL 9.0.0.
Table 2.13 Vector Functions
Name | Description |
---|---|
DISTANCE() |
Calculates the distance between two vectors with the specified method |
VECTOR_TO_STRING() |
Get the string representation of a VECTOR column, given its value as a binary string |
FROM_VECTOR() |
A synonym for VECTOR_TO_STRING()
|
TO_VECTOR() |
A synonym for STRING_TO_VECTOR()
|
VECTOR_DIM() |
Get the length of a vector, that is, the number of entries it contains |
VECTOR_DISTANCE |
A synonym for DISTANCE()
|
VECTOR_TO_STRING() |
Get the string representation of a VECTOR column, given its value as a binary string |
-
DISTANCE(
vector
,vector
,string
)Calculates the distance between two vectors per the specified calculation method. It accepts the following arguments:
A column of
VECTOR
data type.An input query of
VECTOR
data type.A string that specifies the distance metric. The supported values are
COSINE
,DOT
, andEUCLIDEAN
. The argument is a string, and requires quotation marks.
Examples:
mysql> SELECT DISTANCE(STRING_TO_VECTOR("[1.01231, 2.0123123, 3.0123123, 4.01231231]"), STRING_TO_VECTOR("[1, 2, 3, 4]"), "COSINE"); +-----------------------------------------------------------------------------------------------------------------------+ | DISTANCE(STRING_TO_VECTOR("[1.01231, 2.0123123, 3.0123123, 4.01231231]"), STRING_TO_VECTOR("[1, 2, 3, 4]"), "COSINE") | +-----------------------------------------------------------------------------------------------------------------------+ | 0.0000016689300537109375 | +-----------------------------------------------------------------------------------------------------------------------+