Use SQL functions
This topic describes the vector functions supported by seekdb and provides usage notes.
Considerations
- The following operations will fail with the error
different vector dimensions %d and %dif the dimensions of the vectors are different. - If the result exceeds the range of a floating-point number, an error
value out of range: overflow / underflowwill be returned. - The dense vector index supports L2, inner product, and cosine distance as index distance algorithms. The in-memory sparse vector index supports only inner product as the distance algorithm. For more information, see Dense vector index and In-memory sparse vector index.
- The vector index search supports the
L2_distance,Cosine_distance, andInner_productdistance functions.
Distance functions
Distance functions calculate the distance between two vectors, and the specific calculation method varies depending on the distance algorithm.
L2_distance
The Euclidean distance reflects the distance between the coordinates of the compared vectors, essentially the straight-line distance between two vectors. It is calculated using the Pythagorean theorem applied to the vector coordinates:

The function syntax is as follows:
l2_distance(vector v1, vector v2)
The parameters are described as follows:
-
In addition to vector types, parameters can accept any other types that can be strongly cast to vector types, including single-level array types (e.g.,
[1,2,3,..]) and string types (e.g.,'[1,2,3]'). -
The dimensions of the two parameters must be the same.
-
When a single-level array type parameter is present, the elements of that parameter must not contain
NULL.
The return value is described as follows:
-
The return value is a
distance(double)value. -
If any parameter is
NULL,NULLis returned.
Example:
CREATE TABLE t1(c1 vector(3));
INSERT INTO t1 VALUES('[1,2,3]');
SELECT l2_distance(c1, [1,2,3]), l2_distance([1,2,3],[1,1,1]), l2_distance('[1,1,1]','[1,2,3]') FROM t1;
The result is as follows:
+--------------------------+------------------------------+----------------------------------+
| l2_distance(c1, [1,2,3]) | l2_distance([1,2,3],[1,1,1]) | l2_distance('[1,1,1]','[1,2,3]') |
+--------------------------+------------------------------+----------------------------------+
| 0 | 2.23606797749979 | 2.23606797749979 |
+--------------------------+------------------------------+----------------------------------+
1 row in set
L2_squared
The L2 Squared distance is the square of the Euclidean distance (L2 Distance). It omits the square root operation in the Euclidean distance formula, thereby reducing the computational cost while maintaining the relative order of distances. The calculation method is as follows:

The syntax is as follows:
l2_squared(vector v1, vector v2)
The parameters are described as follows:
-
In addition to vector types, parameters can accept any other types that can be strongly cast to vector types, including single-level array types (e.g.,
[1,2,3,..]) and string types (e.g.,'[1,2,3]'). -
The dimensions of the two parameters must be the same.
-
When a single-level array type parameter is present, the elements of that parameter must not contain
NULL.
The return value is described as follows:
-
The return value is a
distance(double)value. -
If any parameter is
NULL,NULLis returned.
Example:
CREATE TABLE t1(c1 vector(3));
INSERT INTO t1 VALUES('[1,2,3]');
SELECT l2_squared(c1, [1,2,3]), l2_squared([1,2,3],[1,1,1]), l2_squared('[1,1,1]','[1,2,3]') FROM t1;
The result is as follows:
+-------------------------+-----------------------------+---------------------------------+
| l2_squared(c1, [1,2,3]) | l2_squared([1,2,3],[1,1,1]) | l2_squared('[1,1,1]','[1,2,3]') |
+-------------------------+-----------------------------+---------------------------------+
| 0 | 5 | 5 |
+-------------------------+-----------------------------+---------------------------------+
1 row in set
L1_distance
The Manhattan distance calculates the sum of the absolute differences along the standard coordinate axes for two points. The calculation formula is as follows:

The function syntax is as follows:
l1_distance(vector v1, vector v2)
The parameters are described as follows:
-
In addition to vector types, parameters can accept any other types that can be strongly cast to vector types, including single-level array types (e.g.,
[1,2,3,..]) and string types (e.g.,'[1,2,3]'). -
The dimensions of the two parameters must be the same.
-
When a single-level array type parameter is present, the elements of that parameter must not contain
NULL.
The return value is described as follows:
-
The return value is a
distance(double)value. -
If any parameter is
NULL,NULLis returned.
Example:
CREATE TABLE t2(c1 vector(3));
INSERT INTO t2 VALUES('[1,2,3]');
INSERT INTO t2 VALUES('[1,1,1]');
SELECT l1_distance(c1, [1,2,3]) FROM t2;
The result is as follows:
+--------------------------+
| l1_distance(c1, [1,2,3]) |
+--------------------------+
| 0 |
| 3 |
+--------------------------+
2 rows in set
Cosine_distance
Cosine similarity measures the angular difference between two vectors, reflecting their directional similarity regardless of their magnitudes. The cosine similarity ranges from [-1, 1], where 1 indicates identical directions, 0 indicates orthogonality, and -1 indicates completely opposite directions.
The calculation method for cosine similarity is as follows:

Since a higher cosine similarity value indicates greater similarity, cosine distance (or cosine dissimilarity) is sometimes used as a measure of distance between vectors. Cosine distance can be calculated by subtracting the cosine similarity from 1:

The cosine distance ranges from [0, 2], where 0 indicates identical directions (no distance), and 2 indicates completely opposite directions.
The function syntax is as follows:
cosine_distance(vector v1, vector v2)
The parameters are described as follows:
-
In addition to vector types, parameters can accept any other types that can be strongly cast to vector types, including single-level array types (e.g.,
[1,2,3,..]) and string types (e.g.,'[1,2,3]'). -
The dimensions of the two parameters must be the same.
-
When a single-level array type parameter is present, the elements of that parameter must not contain
NULL.
The return value is described as follows:
-
The return value is a
distance(double)value. -
If any parameter is
NULL,NULLis returned.
Example:
CREATE TABLE t3(c1 vector(3));
INSERT INTO t3 VALUES('[1,2,3]');
INSERT INTO t3 VALUES('[1,2,1]');
SELECT cosine_distance(c1, [1,2,3]) FROM t3;
+------------------------------+
| cosine_distance(c1, [1,2,3]) |
+------------------------------+
| 0 |
| 0.12712843905603044 |
+------------------------------+
2 rows in set
Inner_product
The inner product, also known as the dot product or scalar product, represents a type of multiplication between two vectors. Geometrically, the inner product indicates the directional and magnitude relationship between two vectors. The calculation method for the inner product is as follows:

The syntax is as follows:
inner_product(vector v1, vector v2)
The parameters are described as follows:
-
In addition to vector types, other types that can be strongly converted to vector types are also accepted as parameters, including single-level array types (e.g.,
[1,2,3,..]) and string types (e.g.,'[1,2,3]'). -
The dimensions of the two parameters must be the same.
-
When a single-level array type parameter is present, the elements of this parameter must not contain
NULL. -
When using this function with sparse vectors, one parameter can be a sparse vector formatted as a string, such as
c2,'{1:2.4}'; both parameters cannot be strings.
The return value is described as follows:
-
The return value is a
distance(double)value. -
If any parameter is
NULL, the return value isNULL.
Here is an example with dense vectors:
CREATE TABLE t4(c1 vector(3));
INSERT INTO t4 VALUES('[1,2,3]');
INSERT INTO t4 VALUES('[1,2,1]');
SELECT inner_product(c1, [1,2,3]) FROM t4;
The result is as follows:
+----------------------------+
| inner_product(c1, [1,2,3]) |
+----------------------------+
| 14 |
| 8 |
+----------------------------+
2 rows in set
Here is an example with sparse vectors:
CREATE TABLE t4(c1 INT, c2 SPARSEVECTOR, c3 SPARSEVECTOR);
INSERT INTO t4 VALUES(1, '{1:1.1, 2:2.2}', '{1:2.4}');
INSERT INTO t4 VALUES(2, '{1:1.5, 3:3.6}', '{4:4.5}');
SELECT inner_product(c2,c3) FROM t4;
The result is as follows:
```shell
+----------------------+
| inner_product(c2,c3) |
+----------------------+
| 2.640000104904175 |
| 0 |
+----------------------+
2 rows in set
Negative_inner_product
The negative_inner_product function calculates the negative inner product between two vectors. The calculation method is as follows:

The syntax is as follows:
negative_inner_product(vector v1, vector v2)
The parameters are described as follows:
-
In addition to vector types, other types that can be strongly converted to vector types are also accepted as parameters, including string types (e.g.,
'[1,2,3]'). -
The dimensions of the two parameters must be the same.
-
When a single-level array type parameter is present, the elements of this parameter must not contain
NULL. -
When using this function with sparse vectors, one parameter can be a sparse vector formatted as a string, such as
c2,'{1:2.4}'; both parameters cannot be strings.
The return value is described as follows:
-
The return value is a
distance(double)value. -
If any parameter is
NULL, the return value isNULL.
Here is an example with dense vectors:
CREATE TABLE t5(c1 vector(3));
INSERT INTO t5 VALUES('[1,2,3]');
INSERT INTO t5 VALUES('[1,2,1]');
SELECT negative_inner_product(c1, [1,2,3]) FROM t5;
The result is as follows:
+-------------------------------------+
| negative_inner_product(c1, [1,2,3]) |
+-------------------------------------+
| -14 |
| -8 |
| -14 |
| -8 |
+-------------------------------------+
4 rows in set
Here is an example with sparse vectors:
CREATE TABLE t5(c1 INT, c2 SPARSEVECTOR, c3 SPARSEVECTOR);
INSERT INTO t5 VALUES(1, '{1:1.1, 2:2.2}', '{1:2.4}');
INSERT INTO t5 VALUES(2, '{1:1.5, 3:3.6}', '{4:4.5}'));
SELECT negative_inner_product(c2,c3) FROM t5;
The result is as follows:
+-------------------------------+
| negative_inner_product(c2,c3) |
+-------------------------------+
| -2.640000104904175 |
| 0 |
+-------------------------------+
2 rows in set
Vector_distance
The vector_distance function calculates the distance between two vectors and allows you to select different distance algorithms by specifying parameters.
The syntax is as follows:
vector_distance(vector v1, vector v2 [, string metric])
The vector v1/v2 parameters are described as follows:
-
In addition to vector types, other types that can be strongly converted to vector types are also accepted as parameters, including single-level array types (e.g.,
[1,2,3,..]) and string types (e.g.,'[1,2,3]'). -
The dimensions of the two parameters must be the same.
-
When a single-level array type parameter is present, the elements of this parameter must not contain
NULL.
The metric parameter specifies the distance algorithm. The options are:
-
If not specified, the default algorithm is
euclidean. -
If specified, the only valid value is:
-
euclidean: Euclidean distance, which is the same as L2_distance. -
manhattan: Manhattan distance, which is the same as L1_distance. -
cosine: Cosine distance, which is the same as Cosine_distance. -
dot: Inner product, which is the same as Inner_product.
-
The return value is described as follows:
-
The return value is a
distance(double)value. -
If any parameter is
NULL, the return value isNULL.
Here is an example:
CREATE TABLE t5(c1 vector(3));
INSERT INTO t5 VALUES('[1,2,3]');
INSERT INTO t5 VALUES('[1,2,1]');
SELECT vector_distance(c1, [1,2,3], euclidean) FROM t5;
The result is as follows:
+-----------------------------------------+
| vector_distance(c1, [1,2,3], euclidean) |
+-----------------------------------------+
| 0 |
| 2 |
+-----------------------------------------+
2 rows in set
Similarity functions
Similarity functions measure the similarity between two vectors. A higher value indicates greater similarity. They are the inverse concept of distance functions: higher similarity means a smaller distance. In vector search, you can set a similarity threshold to filter out results that meet the criteria.
inner_product_similarity
The inner_product_similarity function calculates the inner product similarity between two vectors. The calculation method is the same as the inner_product function, but the return value represents similarity instead of distance. The conversion formula between inner_product_similarity and inner_product is: inner_product_similarity(v1, v2) = (1 + inner_product(v1, v2) / (vector_norm(v1) * vector_norm(v2))) / 2. The vector_norm function calculates the Euclidean norm (magnitude) of a vector, which represents the Euclidean distance between the vector and the origin. For a vector v = [v1, v2, v3, ..., vn], the norm is calculated as: vector_norm(v) = √(v1² + v2² + v3² + ... + vn²).
The syntax is as follows:
inner_product_similarity(vector v1, vector v2)
The parameters are described as follows:
- In addition to vector types, other types that can be strongly converted to vector types are also accepted as parameters, including string types (e.g.,
'[1,2,3]'). - The dimensions of the two parameters must be the same.
- When a single-level array type parameter is present, the elements of this parameter must not contain
NULL.
The return value is described as follows:
- The return value is a double value representing the similarity between the two vectors. A higher value indicates greater similarity.
- If any parameter is
NULL, the return value isNULL.
Here is an example:
CREATE TABLE t12(c1 vector(3));
INSERT INTO t12 VALUES('[1,2,3]');
INSERT INTO t12 VALUES('[1,2,1]');
SELECT inner_product_similarity(c1, [1,2,3]) FROM t12;
The result is as follows:
+---------------------------------------+
| inner_product_similarity(c1, [1,2,3]) |
+---------------------------------------+
| 0.9999999552965164 |
| 0.9364357516169548 |
+---------------------------------------+
2 rows in set
Or more simply:
SELECT inner_product_similarity([1,0,0], [0,1,0]);
The result is as follows:
+--------------------------------------------+
| inner_product_similarity([1,0,0], [0,1,0]) |
+--------------------------------------------+
| 0.5 |
+--------------------------------------------+
1 row in set
cosine_similarity
cosine_similarity computes the cosine similarity between two vectors, reflecting their directional similarity regardless of their magnitudes. The formula is: cosine_similarity(v1, v2) = 1 - (cosine_distance(v1, v2) / 2).
Syntax:
cosine_similarity(vector v1, vector v2)
Parameters:
- In addition to vector types, parameters can accept any type that can be strongly cast to a vector type, such as strings (e.g.,
'[1,2,3]'). - The dimensions of the two parameters must be the same.
- When a parameter is of a single-level array type, it cannot contain
NULLvalues.
Return value:
- The return value is of type double, with a range of
[0, 1]. A value of1indicates that the two vectors are in the same direction, while a value of0indicates they are orthogonal. A larger value indicates greater similarity in direction. - If any parameter is
NULL, the result isNULL.
Example:
CREATE TABLE t13(c1 vector(3));
INSERT INTO t13 VALUES('[1,2,3]');
INSERT INTO t13 VALUES('[1,2,1]');
SELECT cosine_similarity(c1, [1,2,3]) FROM t13;
Result:
+--------------------------------+
| cosine_similarity(c1, [1,2,3]) |
+--------------------------------+
| 0.9999999552965164 |
| 0.9364357516169548 |
+--------------------------------+
2 rows in set
Or more simply:
SELECT cosine_similarity([1,0,0], [0,1,0]);
Result:
+-------------------------------------+
| cosine_similarity([1,0,0], [0,1,0]) |
+-------------------------------------+
| 0.5 |
+-------------------------------------+
1 row in set
l2_similarity
l2_similarity computes the L2 similarity between two vectors. L2 similarity is based on the Euclidean distance, but the returned value represents similarity rather than distance. A higher similarity value indicates greater similarity between the vectors. The conversion to l2_squared is: l2_similarity(v1, v2) = 1 / (1 + l2_squared(v1, v2)).
Syntax:
l2_similarity(vector v1, vector v2)
Parameters:
- In addition to vector types, parameters can accept any type that can be strongly cast to a vector type, such as strings (e.g.,
'[1,2,3]'). - The dimensions of the two parameters must be the same.
- When a parameter is of a single-level array type, it cannot contain
NULLvalues.
Return value:
- The return value is of type double, representing the similarity between the two vectors. A higher value indicates greater similarity.
- If any parameter is
NULL, the result isNULL.
Example:
CREATE TABLE t14(c1 vector(3));
INSERT INTO t14 VALUES('[1,2,3]');
INSERT INTO t14 VALUES('[1,2,1]');
SELECT l2_similarity(c1, [1,2,3]) FROM t14;
Result:
+----------------------------+
| l2_similarity(c1, [1,2,3]) |
+----------------------------+
| 1 |
| 0.2 |
+----------------------------+
2 rows in set
Or more simply:
SELECT l2_similarity([1,0,0], [0,1,0]);
Result:
+---------------------------------+
| l2_similarity([1,0,0], [0,1,0]) |
+---------------------------------+
| 0.3333333333333333 |
+---------------------------------+
1 row in set
Arithmetic functions
Arithmetic functions provide addition (+), subtraction (-), and multiplication (*) operations for vectors, single-level arrays, and special strings. The operations are performed element-wise. For example, addition is calculated as:

Syntax:
v1 + v2
v1 - v2
v1 * v2
Parameters:
-
In addition to vector types, parameters can accept any type that can be strongly cast to a vector type, including single-level arrays (e.g.,
[1,2,3,..]) and string types (e.g.,'[1,2,3]'). Note: Both parameters cannot be string types; at least one parameter must be a vector or single-level array. -
The dimensions of the two parameters must be the same.
-
When a parameter is of a single-level array type, it cannot contain
NULLvalues.
Return value:
-
If at least one parameter is a vector, the return value is a vector of the same type as the vector parameter.
-
If both parameters are single-level arrays, the return value is of type
array(float). -
If any parameter is
NULL, the result isNULL.
Example:
CREATE TABLE t6(c1 vector(3));
INSERT INTO t6 VALUES('[1,2,3]');
SELECT [1,2,3] + '[1.12,1000.0001, -1.2222]', c1 - [1,2,3] FROM t6;
Result:
+---------------------------------------+--------------+
| [1,2,3] + '[1.12,1000.0001, -1.2222]' | c1 - [1,2,3] |
+---------------------------------------+--------------+
| [2.12,1002,1.7778] | [0,0,0] |
+---------------------------------------+--------------+
1 row in set
Comparison functions
Comparison functions provide comparison operations (=, !=, >, <, >=, <=) for vectors, single-level arrays, and special strings. The comparisons are performed element-wise in lexicographical order.
Syntax:
v1 = v2
v1 != v2
v1 > v2
v1 < v2
v1 >= v2
v1 <= v2
Parameters:
-
In addition to vector types, parameters can accept any type that can be strongly cast to a vector type, including single-level arrays (e.g.,
[1,2,3,..]) and string types (e.g.,'[1,2,3]').tipAt least one parameter must be a vector.
-
The dimensions of the two parameters must be the same.
-
When a parameter is of a single-level array type, it cannot contain
NULLvalues.
Return value:
-
The return value is of type bool.
-
If any parameter is
NULL, the result isNULL.
Example:
CREATE TABLE t7(c1 vector(3));
INSERT INTO t7 VALUES('[1,2,3]');
SELECT c1 = '[1,2,3]' FROM t7;
Result:
+----------------+
| c1 = '[1,2,3]' |
+----------------+
| 1 |
+----------------+
1 row in set
Aggregate functions
Vector columns cannot be used as GROUP BY conditions, and DISTINCT is not supported.
Sum
The Sum function computes the sum of vectors in a table by performing element-wise accumulation to obtain the resulting vector.
Syntax:
sum(vector v1)
Parameters:
- Only vector types are supported.
Return value:
- Returns the
sum (vector)value.
Example:
CREATE TABLE t8(c1 vector(3));
INSERT INTO t8 VALUES('[1,2,3]');
SELECT sum(c1) FROM t8;
Result:
+---------+
| sum(c1) |
+---------+
| [1,2,3] |
+---------+
1 row in set
Avg
The Avg function computes the average value of vectors in a table.
Syntax:
avg(vector v1)
Parameters:
- Only vector types are supported.
Return value:
-
Returns the
avg (vector)value. -
Does not count
NULLrows in the vector column. -
If the input parameter is empty, the output is
NULL.
Example:
CREATE TABLE t9(c1 vector(3));
INSERT INTO t9 VALUES('[1,2,3]');
SELECT avg(c1) FROM t9;
Result:
+---------+
| avg(c1) |
+---------+
| [1,2,3] |
+---------+
1 row in set
Other common vector functions
Vector_norm
The Vector_norm function computes the Euclidean norm (magnitude) of a vector, representing the Euclidean distance from the vector to the origin. The calculation is as follows:

Syntax:
vector_norm(vector v1)
Parameters:
-
In addition to vector types, parameters can accept any type that can be strongly cast to a vector type, including single-level arrays (e.g.,
[1,2,3,..]) and string types (e.g.,'[1,2,3]'). -
When a parameter is of a single-level array type, it cannot contain
NULLvalues.
Return value:
-
Returns the
norm(double)magnitude. -
If the parameter is
NULL, it returnsNULL.
Here is an example:
CREATE TABLE t10(c1 vector(3));
INSERT INTO t10 VALUES('[1,2,3]');
SELECT vector_norm(c1),vector_norm([1,2,3]) FROM t10;
The result is as follows:
+--------------------+----------------------+
| vector_norm(c1) | vector_norm([1,2,3]) |
+--------------------+----------------------+
| 3.7416573867739413 | 3.7416573867739413 |
+--------------------+----------------------+
1 row in set
Vector normalization
Vector normalization (Normalization) is the process of standardizing the length (magnitude) of a vector to 1. The vector_norm function is commonly used to implement vector normalization.
For vector indexes using the l2_distance distance type, it is recommended to normalize the vectors to improve the accuracy of similarity searches. After normalization, the magnitude of the vectors is 1, and the range of L2 distances is limited to [0, 2], thus avoiding precision issues caused by excessively small similarity values.
For example, for the vector [3, 4]:
- The magnitude of the vector:
vector_norm([3, 4]) = √(3² + 4²) = 5 - After normalization:
[3, 4] / 5 = [0.6, 0.8]
The magnitude of the normalized vector is 1, which can be verified as follows:
SELECT vector_norm([0.6, 0.8]);
The result is as follows:
+-------------------------+
| vector_norm([0.6, 0.8]) |
+-------------------------+
| 1.000000029802322 |
+-------------------------+
1 row in set
Vector_dims
The vector_dims function is used to return the dimension of the vector.
The syntax is as follows:
vector_dims(vector v1)
The parameters are described as follows:
- In addition to the vector type, other types that can be strongly cast to a vector type are also supported, including single-level array types (e.g.,
[1,2,3,..]) and string types (e.g.,'[1,2,3]').
The return value is described as follows:
-
Returns the
dims(int64)value. -
If the parameter is
NULL, an error is returned.
Here is an example:
CREATE TABLE t11(c1 vector(3));
INSERT INTO t11 VALUES('[1,2,3]');
INSERT INTO t11 VALUES('[1,1,1]');
SELECT vector_dims(c1), vector_dims('[1,2,3]') FROM t11;
The result is as follows:
+-----------------+------------------------+
| vector_dims(c1) | vector_dims('[1,2,3]') |
+-----------------+------------------------+
| 3 | 3 |
| 3 | 3 |
+-----------------+------------------------+
2 rows in set
Semantic index-related functions
In addition to the general vector functions mentioned above, seekdb provides vector search functions specifically for semantic indexes (vector indexes created on text columns):
semantic_distance(column_name, 'query_text'): Performs vector search using the original text. The system automatically calls the embedding model to convert the text into a vector.semantic_vector_distance(column_name, query_vector): Performs search using the vector, supporting both index search (with theAPPROXIMATE/APPROXclause) and full-table scan (without theAPPROXIMATE/APPROXclause).
These two functions are specifically used for semantic index scenarios. For more details and examples, see Semantic index.