Vector Functions and Operators
This document lists the functions and operators available for Vector data types.
Vector functions
The following functions are designed specifically for Vector data types.
Vector distance functions:
Other vector functions:
Extended built-in functions and operators
The following built-in functions and operators are extended to support operations on Vector data types.
Arithmetic operators:
For more information about how vector arithmetic works, see Vector Data Type | Arithmetic.
Aggregate (GROUP BY) functions:
Comparison functions and operators:
For more information about how vectors are compared, see Vector Data Type | Comparison.
Control flow functions:
Cast functions:
For more information about how to use CAST(), see Vector Data Type | Cast.
Full references
VEC_L2_DISTANCE
VEC_L2_DISTANCE(vector1, vector2)
Calculates the L2 distance (Euclidean distance) between two vectors using the following formula:
The two vectors must have the same number of dimensions. Otherwise, an error is returned.
Example:
SELECT VEC_L2_DISTANCE('[0, 3]', '[4, 0]');
+-------------------------------------+
| VEC_L2_DISTANCE('[0, 3]', '[4, 0]') |
+-------------------------------------+
| 5 |
+-------------------------------------+
VEC_COSINE_DISTANCE
VEC_COSINE_DISTANCE(vector1, vector2)
Calculates the cosine distance between two vectors using the following formula:
The two vectors must have the same number of dimensions. Otherwise, an error is returned.
For embeddings from OpenAI, it is recommended that you use this function.
Example:
SELECT VEC_COSINE_DISTANCE('[1, 1]', '[-1, -1]');
+-------------------------------------------+
| VEC_COSINE_DISTANCE('[1, 1]', '[-1, -1]') |
+-------------------------------------------+
| 2 |
+-------------------------------------------+
VEC_NEGATIVE_INNER_PRODUCT
VEC_NEGATIVE_INNER_PRODUCT(vector1, vector2)
Calculates the distance by using the negative of the inner product between two vectors, using the following formula:
The two vectors must have the same number of dimensions. Otherwise, an error is returned.
Example:
SELECT VEC_NEGATIVE_INNER_PRODUCT('[1, 2]', '[3, 4]');
+------------------------------------------------+
| VEC_NEGATIVE_INNER_PRODUCT('[1, 2]', '[3, 4]') |
+------------------------------------------------+
| -11 |
+------------------------------------------------+
VEC_L1_DISTANCE
VEC_L1_DISTANCE(vector1, vector2)
Calculates the L1 distance (Manhattan distance) between two vectors using the following formula:
The two vectors must have the same number of dimensions. Otherwise, an error is returned.
Example:
SELECT VEC_L1_DISTANCE('[0, 0]', '[3, 4]');
+-------------------------------------+
| VEC_L1_DISTANCE('[0, 0]', '[3, 4]') |
+-------------------------------------+
| 7 |
+-------------------------------------+
VEC_DIMS
VEC_DIMS(vector)
Returns the dimension of a vector.
Examples:
SELECT VEC_DIMS('[1, 2, 3]');
+-----------------------+
| VEC_DIMS('[1, 2, 3]') |
+-----------------------+
| 3 |
+-----------------------+
SELECT VEC_DIMS('[]');
+----------------+
| VEC_DIMS('[]') |
+----------------+
| 0 |
+----------------+
VEC_L2_NORM
VEC_L2_NORM(vector)
Calculates the L2 norm (Euclidean norm) of a vector using the following formula:
Example:
SELECT VEC_L2_NORM('[3, 4]');
+-----------------------+
| VEC_L2_NORM('[3, 4]') |
+-----------------------+
| 5 |
+-----------------------+
VEC_FROM_TEXT
VEC_FROM_TEXT(string)
Converts a string into a vector. In many cases, this conversion is done implicitly, for example when inserting data into a column of the VECTOR data type. However, in expressions where implicit conversion is not supported (such as arithmetic operations on vectors), you need to explicitly call this function.
Example:
SELECT VEC_FROM_TEXT('[1, 2]') + VEC_FROM_TEXT('[3, 4]');
+-------------------------------------------------+
| VEC_FROM_TEXT('[1,2]') + VEC_FROM_TEXT('[3,4]') |
+-------------------------------------------------+
| [4,6] |
+-------------------------------------------------+
VEC_AS_TEXT
VEC_AS_TEXT(vector)
Converts a vector into a string.
Example:
SELECT VEC_AS_TEXT('[1.000, 2.5]');
+-----------------------------+
| VEC_AS_TEXT('[1.000, 2.5]') |
+-----------------------------+
| [1,2.5] |
+-----------------------------+
MySQL compatibility
The vector functions and the extended usage of built-in functions and operators over vector data types are TiDB specific, and are not supported in MySQL.