Vector math functions
Use these functions to treat lists or fixed-size arrays of numbers as vectors, and to compute the distance, similarity, inner product, or cross product of two vectors. Most functions whose names start with array_ need fixed-size ARRAY arguments, and the functions whose names start with list_ take lists.
array_cosine_distance function
Returns 1 minus the cosine similarity of two vectors that you pass as ARRAY values of the same size. The result ranges from 0 for vectors that point in the same direction, through 1 for perpendicular vectors, to 2 for vectors that point in opposite directions. Only the directions of the vectors matter, so multiplying a vector by a positive number does not change the result. Smaller values mean more similar directions, so you can sort by the result in ascending order to put the closest vectors first.
If either argument is NULL, the result is NULL. If every element of either vector is 0, the result is NaN (not a number).
returns the same result as array1 <=> array2array_cosine_distance(. The array1, array2)<=> operator is an alias of list_cosine_distance, which computes the cosine distance between two lists. To get the cosine similarity, use the array_cosine_similarity function.
Syntax
array_cosine_distance(array1, array2)
Arguments
The array_cosine_distance function takes the following arguments:
array1-
The first vector, as an
ARRAYofFLOATorDOUBLEvalues, such asDOUBLE[3]. The elements cannot beNULL. array2-
The second vector, as an
ARRAYofFLOATorDOUBLEvalues with the same size asarray1. The elements cannot beNULL.
Return type
FLOAT when both arguments are FLOAT arrays, and DOUBLE otherwise.
Examples
The following SQL query builds two FLOAT arrays of size 3 with array_value and returns their cosine distance.
SELECT array_cosine_distance( array_value(1.0::FLOAT, 2.0::FLOAT, 3.0::FLOAT), array_value(2.0::FLOAT, 3.0::FLOAT, 4.0::FLOAT) ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query casts lists to DOUBLE[2] arrays and returns the cosine distance for vectors that point in the same direction, in perpendicular directions, and in opposite directions.
SELECT array_cosine_distance( [2.0, 0.0]::DOUBLE[2], [5.0, 0.0]::DOUBLE[2] ) AS same_direction, array_cosine_distance( [1.0, 0.0]::DOUBLE[2], [0.0, 1.0]::DOUBLE[2] ) AS perpendicular, array_cosine_distance( [1.0, 0.0]::DOUBLE[2], [-1.0, 0.0]::DOUBLE[2] ) AS opposite
The query returns the following result.
| same_direction | perpendicular | opposite |
|---|---|---|
|
|
|
The following SQL query sorts three vectors by their cosine distance from the vector [1.0, 0.0], so the vector with the closest direction comes first.
SELECT label, array_cosine_distance( v::DOUBLE[2], [1.0, 0.0]::DOUBLE[2] ) AS distance FROM (VALUES ('a', [0.0, 3.0]), ('b', [2.0, 0.0]), ('c', [-1.0, 0.0]) ) AS t(label, v) ORDER BY distance
The query returns the following results.
| label | distance |
|---|---|
|
|
|
|
|
|
The following SQL query shows that the result is NaN (not a number) when every element of one vector is 0.
SELECT isnan( array_cosine_distance( [0.0, 0.0]::DOUBLE[2], [1.0, 0.0]::DOUBLE[2] ) ) AS is_nan
The query returns the following result.
| is_nan |
|---|
|
Considerations
The arguments must be ARRAY values. If you pass a list, such as [1.0, 2.0], the query fails, because DuckDB does not convert it to an ARRAY for you. Cast the list to an array type with its number of elements, as in CAST(, or use the list_cosine_distance function. The cast fails for any list that has a different number of elements.list AS DOUBLE[3])
The elements must be FLOAT or DOUBLE values. An ARRAY of INTEGER or DECIMAL values makes the query fail, so cast it to a DOUBLE array, such as DOUBLE[3].
If the arrays have different sizes, or if either array contains a NULL element, the query fails.
array_cosine_similarity function
Returns the cosine of the angle between two vectors that you pass as ARRAY values of the same size. The result ranges from -1 to 1. A result of 1 means that the vectors point in the same direction, 0 means that they are perpendicular, and -1 means that they point in opposite directions. Only the directions of the vectors matter, so multiplying a vector by a positive number does not change the result.
If either argument is NULL, the result is NULL. If every element of either vector is 0, the result is NaN (not a number).
To compute the cosine similarity of two lists, use the list_cosine_similarity function. To get 1 minus the similarity, use the array_cosine_distance function.
Syntax
array_cosine_similarity(array1, array2)
Arguments
The array_cosine_similarity function takes the following arguments:
array1-
The first vector, as an
ARRAYofFLOATorDOUBLEvalues, such asDOUBLE[3]. The elements cannot beNULL. array2-
The second vector, as an
ARRAYofFLOATorDOUBLEvalues with the same size asarray1. The elements cannot beNULL.
Return type
FLOAT when both arguments are FLOAT arrays, and DOUBLE otherwise.
Examples
The following SQL query builds two FLOAT arrays of size 3 with array_value and returns their cosine similarity.
SELECT array_cosine_similarity( array_value(1.0::FLOAT, 2.0::FLOAT, 3.0::FLOAT), array_value(2.0::FLOAT, 3.0::FLOAT, 4.0::FLOAT) ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query casts two lists to DOUBLE[2] arrays and returns their cosine similarity.
SELECT array_cosine_similarity( [3.0, 4.0]::DOUBLE[2], [4.0, 3.0]::DOUBLE[2] ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the cosine similarity for vectors that point in the same direction, in perpendicular directions, and in opposite directions.
SELECT array_cosine_similarity( [2.0, 0.0]::DOUBLE[2], [5.0, 0.0]::DOUBLE[2] ) AS same_direction, array_cosine_similarity( [1.0, 0.0]::DOUBLE[2], [0.0, 1.0]::DOUBLE[2] ) AS perpendicular, array_cosine_similarity( [1.0, 0.0]::DOUBLE[2], [-1.0, 0.0]::DOUBLE[2] ) AS opposite
The query returns the following result.
| same_direction | perpendicular | opposite |
|---|---|---|
|
|
|
The following SQL query shows that the result is NaN (not a number) when every element of one vector is 0.
SELECT isnan( array_cosine_similarity( [0.0, 0.0]::DOUBLE[2], [1.0, 0.0]::DOUBLE[2] ) ) AS is_nan
The query returns the following result.
| is_nan |
|---|
|
Considerations
The arguments must be ARRAY values. If you pass a list, such as [1.0, 2.0], the query fails, because DuckDB does not convert it to an ARRAY for you. Cast the list to an array type with its number of elements, as in CAST(, or use the list_cosine_similarity function. The cast fails for any list that has a different number of elements.list AS DOUBLE[3])
The elements must be FLOAT or DOUBLE values. An ARRAY of INTEGER or DECIMAL values makes the query fail, so cast it to a DOUBLE array, such as DOUBLE[3].
If the arrays have different sizes, or if either array contains a NULL element, the query fails.
Differences from Athena engine version 3
Athena engine version 3 has no function for the cosine similarity of two arrays: its cosine_similarity takes two maps. DuckDB has no cosine_similarity function. Use array_cosine_similarity for ARRAY values, or list_cosine_similarity for lists.
array_cross_product function
Returns the cross product of two vectors in three-dimensional space, which you pass as ARRAY values of size 3. The result is an ARRAY of size 3 that is perpendicular to both arguments. If you swap the arguments, each element of the result changes sign.
If either argument is NULL, the result is NULL.
Syntax
array_cross_product(array1, array2)
Arguments
The array_cross_product function takes the following arguments:
array1-
The first vector, as an
ARRAYof threeFLOATorDOUBLEvalues, such asDOUBLE[3]. You can also pass anINTEGERarray, such asINTEGER[3], or a list of three numbers, and the function converts it. The elements cannot beNULL. array2-
The second vector, in any of the forms that
array1accepts. The elements cannot beNULL.
Return type
FLOAT[3] or DOUBLE[3], depending on the arguments. The result is FLOAT[3] when you pass two FLOAT arrays, or a FLOAT array and a list of integers. It is DOUBLE[3] when you pass two DOUBLE arrays, two INTEGER arrays, or two lists of integers.
Examples
The following SQL query builds two FLOAT arrays of size 3 with array_value and returns their cross product.
SELECT array_cross_product( array_value(1.0::FLOAT, 2.0::FLOAT, 3.0::FLOAT), array_value(2.0::FLOAT, 3.0::FLOAT, 4.0::FLOAT) ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the cross product of the X and Y unit vectors in both orders. Swapping the arguments changes the sign of the result.
SELECT array_cross_product( [1.0, 0.0, 0.0]::DOUBLE[3], [0.0, 1.0, 0.0]::DOUBLE[3] ) AS x_cross_y, array_cross_product( [0.0, 1.0, 0.0]::DOUBLE[3], [1.0, 0.0, 0.0]::DOUBLE[3] ) AS y_cross_x
The query returns the following result.
| x_cross_y | y_cross_x |
|---|---|
|
|
The following SQL query passes two list literals of three integers. array_cross_product converts them to DOUBLE[3] arrays, so you do not need a cast.
SELECT array_cross_product([1, 0, 0], [0, 1, 0]) AS result
The query returns the following result.
| result |
|---|
|
Considerations
Both arrays must have exactly three elements. If either array contains a NULL element, the query fails.
Unlike the other vector functions in this category, array_cross_product also accepts lists of three numbers and converts them for you. You can pass list literals such as [1, 0, 0] without a cast. Two lists of integers are converted to DOUBLE[3], and a list of integers that you pass with a FLOAT array is converted to FLOAT[3]. A list that does not have exactly three elements makes the query fail.
array_cross_product also converts INTEGER arrays. When you pass two INTEGER[3] arrays, the result is DOUBLE[3].
array_distance function
Returns the straight-line (Euclidean) distance between two points that you pass as ARRAY values of the same size. The result is the square root of the sum of the squared differences between the elements at the same positions. It is 0 when the arrays are equal, and it is never negative.
If either argument is NULL, the result is NULL.
returns the same result as array1 <-> array2array_distance(. The array1, array2)<-> operator is an alias of list_distance, which computes the distance between two lists.
Syntax
array_distance(array1, array2)
Arguments
The array_distance function takes the following arguments:
array1-
The first point, as an
ARRAYofFLOATorDOUBLEvalues, such asDOUBLE[3]. The elements cannot beNULL. array2-
The second point, as an
ARRAYofFLOATorDOUBLEvalues with the same size asarray1. The elements cannot beNULL.
Return type
FLOAT when both arguments are FLOAT arrays, and DOUBLE otherwise.
Examples
The following SQL query builds two FLOAT arrays of size 3 with array_value and returns the distance between them.
SELECT array_distance( array_value(1.0::FLOAT, 2.0::FLOAT, 3.0::FLOAT), array_value(2.0::FLOAT, 3.0::FLOAT, 4.0::FLOAT) ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query casts two lists to DOUBLE[2] arrays and returns the distance between the points (0, 0) and (3, 4).
SELECT array_distance( [0.0, 0.0]::DOUBLE[2], [3.0, 4.0]::DOUBLE[2] ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query sorts three points by their distance from the point (1, 1), so the nearest point comes first.
SELECT label, array_distance( v::DOUBLE[2], [1.0, 1.0]::DOUBLE[2] ) AS distance FROM (VALUES ('a', [4.0, 5.0]), ('b', [1.0, 2.0]), ('c', [7.0, 9.0]) ) AS t(label, v) ORDER BY distance
The query returns the following results.
| label | distance |
|---|---|
|
|
|
|
|
|
Considerations
The arguments must be ARRAY values. If you pass a list, such as [1.0, 2.0], the query fails, because DuckDB does not convert it to an ARRAY for you. Cast the list to an array type with its number of elements, as in CAST(, or use the list_distance function. The cast fails for any list that has a different number of elements.list AS DOUBLE[3])
The elements must be FLOAT or DOUBLE values. An ARRAY of INTEGER or DECIMAL values makes the query fail, so cast it to a DOUBLE array, such as DOUBLE[3].
If the arrays have different sizes, or if either array contains a NULL element, the query fails.
array_inner_product function
Returns the sum of the products of the elements at the same positions in two ARRAY values of the same size. For example, the inner product of [1, 2, 3] and [4, 5, 6] is 1*4 + 2*5 + 3*6, which is 32. The result is 0 for perpendicular vectors.
If either argument is NULL, the result is NULL.
array_dot_product is an alias of array_inner_product. To compute the inner product of two lists, use the list_inner_product function.
Syntax
array_inner_product(array1, array2)
array_dot_product(array1, array2)
Arguments
The array_inner_product function takes the following arguments:
array1-
The first vector, as an
ARRAYofFLOATorDOUBLEvalues, such asDOUBLE[3]. The elements cannot beNULL. array2-
The second vector, as an
ARRAYofFLOATorDOUBLEvalues with the same size asarray1. The elements cannot beNULL.
Return type
FLOAT when both arguments are FLOAT arrays, and DOUBLE otherwise.
Examples
The following SQL query builds two FLOAT arrays of size 3 with array_value and returns their inner product.
SELECT array_inner_product( array_value(1.0::FLOAT, 2.0::FLOAT, 3.0::FLOAT), array_value(2.0::FLOAT, 3.0::FLOAT, 4.0::FLOAT) ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query casts two lists to DOUBLE[3] arrays and returns their inner product, 1*4 + 2*5 + 3*6.
SELECT array_inner_product( [1.0, 2.0, 3.0]::DOUBLE[3], [4.0, 5.0, 6.0]::DOUBLE[3] ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the array_dot_product alias to return the inner product 1*4 + 2*5 + 3*6.
SELECT array_dot_product( [1.0, 2.0, 3.0]::DOUBLE[3], [4.0, 5.0, 6.0]::DOUBLE[3] ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The arguments must be ARRAY values. If you pass a list, such as [1.0, 2.0], the query fails, because DuckDB does not convert it to an ARRAY for you. Cast the list to an array type with its number of elements, as in CAST(, or use the list_inner_product function. The cast fails for any list that has a different number of elements.list AS DOUBLE[3])
The elements must be FLOAT or DOUBLE values. An ARRAY of INTEGER or DECIMAL values makes the query fail, so cast it to a DOUBLE array, such as DOUBLE[3].
If the arrays have different sizes, or if either array contains a NULL element, the query fails.
array_negative_inner_product function
Returns the inner product of two ARRAY values of the same size, multiplied by -1. The result equals -array_inner_product(. A larger inner product gives a smaller result. To put the vectors with the largest inner product first, sort by this value in ascending order, as you would with a distance.array1, array2)
If either argument is NULL, the result is NULL.
array_negative_dot_product is an alias of array_negative_inner_product. To compute the same value for two lists, use the list_negative_inner_product function.
Syntax
array_negative_inner_product(array1, array2)
array_negative_dot_product(array1, array2)
Arguments
The array_negative_inner_product function takes the following arguments:
array1-
The first vector, as an
ARRAYofFLOATorDOUBLEvalues, such asDOUBLE[3]. The elements cannot beNULL. array2-
The second vector, as an
ARRAYofFLOATorDOUBLEvalues with the same size asarray1. The elements cannot beNULL.
Return type
FLOAT when both arguments are FLOAT arrays, and DOUBLE otherwise.
Examples
The following SQL query builds two FLOAT arrays of size 3 with array_value and returns the negative of their inner product.
SELECT array_negative_inner_product( array_value(1.0::FLOAT, 2.0::FLOAT, 3.0::FLOAT), array_value(2.0::FLOAT, 3.0::FLOAT, 4.0::FLOAT) ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the array_negative_dot_product alias to return the negative of the inner product 1*4 + 2*5 + 3*6.
SELECT array_negative_dot_product( [1.0, 2.0, 3.0]::DOUBLE[3], [4.0, 5.0, 6.0]::DOUBLE[3] ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query sorts three vectors by their negative inner product with the vector [1.0, 0.0], so the vector with the largest inner product comes first.
SELECT label, array_negative_inner_product( v::DOUBLE[2], [1.0, 0.0]::DOUBLE[2] ) AS score FROM (VALUES ('a', [2.0, 1.0]), ('b', [5.0, 0.0]), ('c', [-1.0, 3.0]) ) AS t(label, v) ORDER BY score
The query returns the following results.
| label | score |
|---|---|
|
|
|
|
|
|
Considerations
The arguments must be ARRAY values. If you pass a list, such as [1.0, 2.0], the query fails, because DuckDB does not convert it to an ARRAY for you. Cast the list to an array type with its number of elements, as in CAST(, or use the list_negative_inner_product function. The cast fails for any list that has a different number of elements.list AS DOUBLE[3])
The elements must be FLOAT or DOUBLE values. An ARRAY of INTEGER or DECIMAL values makes the query fail, so cast it to a DOUBLE array, such as DOUBLE[3].
If the arrays have different sizes, or if either array contains a NULL element, the query fails.
list_cosine_distance function
Treats list1 and list2 as vectors and returns 1 minus their cosine similarity. The result is 0 for two lists that point in the same direction, 1 for perpendicular lists, and 2 for lists that point in opposite directions. Only the direction of each vector matters, not its length, so [1, 2] and [2, 4] have a distance of 0. If either list is NULL, the result is NULL.
The <=> operator is an alias of list_cosine_distance: returns the same result as list1 <=> list2list_cosine_distance(. To get the cosine similarity itself, use the list_cosine_similarity function.list1, list2)
Syntax
list_cosine_distance(list1, list2)
list1 <=> list2
Arguments
The list_cosine_distance function takes the following arguments:
list1-
The first vector, as a list of numbers, such as a
DOUBLE[]orFLOAT[]value. A list of another numeric type, such asINTEGER[], is converted toDOUBLE[]. list2-
The second vector, as a list of numbers with the same number of elements as
list1.
Return type
DOUBLE. When both lists are FLOAT[] values, the result is a FLOAT.
Examples
The following SQL query returns the cosine distance between two equal lists.
SELECT list_cosine_distance([1, 2, 3], [1, 2, 3]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the <=> operator to compare pairs of lists that point in the same direction, in perpendicular directions, and in opposite directions.
SELECT [1, 2] <=> [2, 4] AS same_direction, [1, 0] <=> [0, 1] AS perpendicular, [1, 0] <=> [-1, 0] AS opposite
The query returns the following result.
| same_direction | perpendicular | opposite |
|---|---|---|
|
|
|
The following SQL query shows that a list of zeros returns NaN.
SELECT isnan( list_cosine_distance([0, 0], [1, 2]) ) AS is_nan
The query returns the following result.
| is_nan |
|---|
|
Considerations
Both lists must have the same number of elements, and neither list can contain a NULL element. Otherwise, the query fails.
If either list holds only zeros, the cosine is not defined, and the result is NaN.
In some other SQL engines, <=> is a null-safe equality operator. In DuckDB, it is the cosine distance operator. It accepts only lists of numbers, so a comparison such as 1 <=> 1 causes an error. To compare two values and treat two NULL values as equal, use IS NOT DISTINCT FROM.
list_cosine_similarity function
Treats list1 and list2 as vectors and returns the cosine of the angle between them, which is their dot product divided by the product of their lengths. The result is 1 for lists that point in the same direction, 0 for perpendicular lists, and -1 for lists that point in opposite directions. If either list is NULL, the result is NULL.
To get the cosine distance, which is 1 minus this value, use the list_cosine_distance function or the <=> operator.
Syntax
list_cosine_similarity(list1, list2)
Arguments
The list_cosine_similarity function takes the following arguments:
list1-
The first vector, as a list of numbers, such as a
DOUBLE[]orFLOAT[]value. A list of another numeric type, such asINTEGER[], is converted toDOUBLE[]. list2-
The second vector, as a list of numbers with the same number of elements as
list1.
Return type
DOUBLE. When both lists are FLOAT[] values, the result is a FLOAT.
Examples
The following SQL query returns the cosine similarity of two equal lists.
SELECT list_cosine_similarity( [1, 2, 3], [1, 2, 3] ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares pairs of lists that point in similar, perpendicular, and opposite directions.
SELECT list_cosine_similarity([3, 4], [4, 3]) AS similar, list_cosine_similarity( [1, 0], [0, 1] ) AS perpendicular, list_cosine_similarity([1, 0], [-1, 0]) AS opposite
The query returns the following result.
| similar | perpendicular | opposite |
|---|---|---|
|
|
|
The following SQL query ranks rows by how similar their vectors are to [1.0, 1.0] and returns the two rows with the highest scores. Rows 1 and 3 have the same score, so the ORDER BY clause breaks the tie by id and returns row 1. The score is rounded to four decimal places.
SELECT id, round( list_cosine_similarity(embedding, [1.0, 1.0]), 4 ) AS score FROM (VALUES (1, [1.0, 0.0]), (2, [0.6, 0.8]), (3, [0.0, 1.0]) ) AS t(id, embedding) ORDER BY score DESC, id LIMIT 2
The query returns the following results.
| id | score |
|---|---|
|
|
|
|
Considerations
Both lists must have the same number of elements, and neither list can contain a NULL element. Otherwise, the query fails.
If either list holds only zeros, the cosine is not defined, and the result is NaN.
Differences from Athena engine version 3
Athena engine version 3 has cosine_similarity, which takes two maps from names to DOUBLE values and matches their entries by key. DuckDB has no cosine_similarity function. Put the values in two lists in the same order and use list_cosine_similarity, which matches elements by position.
list_distance function
Treats list1 and list2 as the coordinates of two points and returns the straight-line (Euclidean) distance between them. The distance is the square root of the sum of the squared differences between the elements at each position. If either list is NULL, the result is NULL.
The <-> operator is an alias of list_distance: returns the same result as list1 <-> list2list_distance(. Use it, for example, in an list1, list2)ORDER BY clause to find the rows nearest to a point.
Syntax
list_distance(list1, list2)
list1 <-> list2
Arguments
The list_distance function takes the following arguments:
list1-
The first point, as a list of numbers, such as a
DOUBLE[]orFLOAT[]value. A list of another numeric type, such asINTEGER[], is converted toDOUBLE[]. list2-
The second point, as a list of numbers with the same number of elements as
list1.
Return type
DOUBLE. When both lists are FLOAT[] values, the result is a FLOAT.
Examples
The following SQL query returns the distance between two points that differ only in their third coordinate.
SELECT list_distance([1, 2, 3], [1, 2, 5]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the <-> operator to return the distance between the points (0, 0) and (3, 4).
SELECT [0, 0] <-> [3, 4] AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the two rows whose points are nearest to [1.0, 1.0].
SELECT id, v <-> [1.0, 1.0] AS distance FROM (VALUES (1, [1.0, 1.0]), (2, [4.0, 5.0]), (3, [7.0, 9.0]) ) AS t(id, v) ORDER BY distance, id LIMIT 2
The query returns the following results.
| id | distance |
|---|---|
|
|
|
|
Considerations
Both lists must have the same number of elements, and neither list can contain a NULL element. Otherwise, the query fails.
list_inner_product function
Multiplies the elements of list1 and list2 that are at the same position, and returns the sum of the products. For example, the inner product of [1, 2, 3] with itself is 1 * 1 + 2 * 2 + 3 * 3, which is 14. The inner product of two empty lists is 0. If either list is NULL, the result is NULL.
list_dot_product is an alias of list_inner_product.
Syntax
list_inner_product(list1, list2)
Arguments
The list_inner_product function takes the following arguments:
list1-
The first list of numbers, as a
DOUBLE[]orFLOAT[]list. Lists of other numeric types, such asINTEGERorDECIMAL, are converted toFLOAT[]when the other list isFLOAT[], and toDOUBLE[]otherwise. list2-
The second list of numbers, with the same length as
list1.
Return type
FLOAT when at least one list is FLOAT[] and neither list is DOUBLE[], and DOUBLE otherwise.
Examples
The following SQL query returns the inner product of a list with itself.
SELECT list_inner_product([1, 2, 3], [1, 2, 3]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the list_dot_product alias with decimal values. The result is 1.5 * 2.0 + 2.0 * 4.0, which is 11.0.
SELECT list_dot_product([1.5, 2.0], [2.0, 4.0]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes two FLOAT[] lists, so the result is a FLOAT.
SELECT list_inner_product( CAST([1.5, 2.0] AS FLOAT[]), CAST([2.0, 4.0] AS FLOAT[]) ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The two lists must have the same length, and neither list can have a NULL element. Otherwise the query fails.
list_negative_inner_product function
Treats list1 and list2 as vectors, multiplies their elements position by position, adds the products, and returns the negative of that sum. The result equals -list_inner_product(. A larger inner product gives a smaller negative inner product. To put the vectors with the largest inner product first, sort by this value in ascending order.list1, list2)
If either list is NULL, the result is NULL. list_negative_dot_product is an alias of list_negative_inner_product.
Syntax
list_negative_inner_product(list1, list2)
Arguments
The list_negative_inner_product function takes the following arguments:
list1-
The first list of numbers, as
DOUBLE[]orFLOAT[]. Lists of other numeric types are converted toDOUBLE[]. The list cannot containNULLelements. list2-
The second list of numbers, with the same number of elements as
list1. The list cannot containNULLelements.
Return type
DOUBLE, or FLOAT when the lists are FLOAT[].
Examples
The following SQL query computes the negative inner product of [1, 2, 3] with itself. The inner product is 1 * 1 + 2 * 2 + 3 * 3, which is 14, so the result is -14.0.
SELECT list_negative_inner_product( [1, 2, 3], [1, 2, 3] ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the list_negative_dot_product alias. The inner product is 1.5 * 2.0 + 2.0 * -1.0, which is 1.0, so the result is -1.0.
SELECT list_negative_dot_product( [1.5, 2.0], [2.0, -1.0] ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes two FLOAT[] lists, so the result is a FLOAT.
SELECT list_negative_inner_product( CAST([1.0, 2.0] AS FLOAT[]), CAST([3.0, 4.0] AS FLOAT[]) ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query ranks three vectors by their inner product with [1.0, 2.0]. Sorting by the negative inner product in ascending order puts the vector with the largest inner product first.
SELECT id AS id, list_negative_inner_product(v, [1.0, 2.0]) AS score FROM (VALUES (1, [1.0, 0.0]), (2, [0.0, 1.0]), (3, [0.5, 0.5]) ) AS t(id, v) ORDER BY score, id
The query returns the following results.
| id | score |
|---|---|
|
|
|
|
|
|
Considerations
If the two lists have different lengths, or if either list contains a NULL element, the query fails.