VECTOR_ SUM
On this page
Returns a vector, which is a sum of all vectors in a column.
It is an aggregate function.
Syntax
VECTOR_SUM(vector_expression)
Arguments
-
vector_
: An expression that evaluates to a vector.expression The vector must be encoded as a blob containing packed 8, 16 or 32 bit integer numbers; 32 or 64 bit floating-point numbers; single-precision; or double-precision floating-point numbers in little-endian byte order.
Return Type
A blob containing packed single-precision floating-point numbers in little-endian byte order.
Remarks
You can specify the data type of the vector elements in which this operation is performed on the vector by adding a suffix to the function._
.
Suffix |
Data Type |
---|---|
|
8-bit signed integer |
|
16-bit signed integer |
|
32-bit signed integer |
|
64-bit signed integer |
|
32-bit floating-point number (IEEE standard format) |
|
64-bit floating-point number (IEEE standard format) |
Example
The following examples use the JSON_
function to view the output in JSON format.
Adding Two Vectors
The following example uses the t
table:
CREATE TABLE v_t (x BLOB, y BLOB);INSERT INTO v_t VALUES (JSON_ARRAY_PACK('[12.1, 6.4]'), JSON_ARRAY_PACK('[8.6,9.2]'));INSERT INTO v_t VALUES (JSON_ARRAY_PACK('[5.2, 11.7]'), JSON_ARRAY_PACK('[4.6,7.3]'));
This example sums the vector blobs for each column using the VECTOR_
aggregate function:
SELECT JSON_ARRAY_UNPACK(VECTOR_SUM(x)), JSON_ARRAY_UNPACK(VECTOR_SUM(y)) FROM v_t;
+----------------------------------+----------------------------------+
| JSON_ARRAY_UNPACK(VECTOR_SUM(x)) | JSON_ARRAY_UNPACK(VECTOR_SUM(y)) |
+----------------------------------+----------------------------------+
| [17.2999992,18.1000004] | [13.2000008,16.5] |
+----------------------------------+----------------------------------+
1 row in set (0.01 sec)
Typecasting the Data type Using Suffixes
To perform this operation on vectors specified in a format other than the default 32-bit floating point number, specify a suffix with the VECTOR_
function.
-- _i8 suffixSELECT JSON_ARRAY_UNPACK(VECTOR_SUM_i8(x)), JSON_ARRAY_UNPACK(VECTOR_SUM_i8(y)) FROM v_t;
+-------------------------------------+-------------------------------------+
| JSON_ARRAY_UNPACK(VECTOR_SUM_i8(x)) | JSON_ARRAY_UNPACK(VECTOR_SUM_i8(y)) |
+-------------------------------------+-------------------------------------+
| [-8.52219055e-38,-2.49785375e-38] | [-5.75992243e-38,-9.28640548e-38] |
+-------------------------------------+-------------------------------------+
1 row in set (0.01 sec)
-- _i16 suffixSELECT JSON_ARRAY_UNPACK(VECTOR_SUM_i16(x)), JSON_ARRAY_UNPACK(VECTOR_SUM_i16(y)) FROM v_t;
+--------------------------------------+--------------------------------------+
| JSON_ARRAY_UNPACK(VECTOR_SUM_i16(x)) | JSON_ARRAY_UNPACK(VECTOR_SUM_i16(y)) |
+--------------------------------------+--------------------------------------+
| [-8.48559984e-38,-9.91823359e-38] | [-5.75992243e-38,-9.28640548e-38] |
+--------------------------------------+--------------------------------------+
1 row in set (0.03 sec)
-- _i32 suffixSELECT JSON_ARRAY_UNPACK(VECTOR_SUM_i32(x)), JSON_ARRAY_UNPACK(VECTOR_SUM_i32(y)) FROM v_t;
+--------------------------------------+--------------------------------------+
| JSON_ARRAY_UNPACK(VECTOR_SUM_i32(x)) | JSON_ARRAY_UNPACK(VECTOR_SUM_i32(y)) |
+--------------------------------------+--------------------------------------+
| [-8.52233404e-38,-9.99170198e-38] | [-5.75992243e-38,-9.28640548e-38] |
+--------------------------------------+--------------------------------------+
1 row in set (0.02 sec)
-- _i64 suffixSELECT JSON_ARRAY_UNPACK(VECTOR_SUM_i64(x)), JSON_ARRAY_UNPACK(VECTOR_SUM_i64(y)) FROM v_t;
+--------------------------------------+--------------------------------------+
| JSON_ARRAY_UNPACK(VECTOR_SUM_i64(x)) | JSON_ARRAY_UNPACK(VECTOR_SUM_i64(y)) |
+--------------------------------------+--------------------------------------+
| [-8.52233404e-38,-9.99170198e-38] | [-5.75992243e-38,-9.28640548e-38] |
+--------------------------------------+--------------------------------------+
1 row in set (0.03 sec)
-- _f64 suffixSELECT JSON_ARRAY_UNPACK(VECTOR_SUM_f64(x)), JSON_ARRAY_UNPACK(VECTOR_SUM_f64(y)) FROM v_t;
+--------------------------------------+--------------------------------------+
| JSON_ARRAY_UNPACK(VECTOR_SUM_f64(x)) | JSON_ARRAY_UNPACK(VECTOR_SUM_f64(y)) |
+--------------------------------------+--------------------------------------+
| [-4.75446394e+16,11.7140617] | [-1.8777817e-33,9.39999962] |
+--------------------------------------+--------------------------------------+
1 row in set (0.03 sec)
How to Use the Vector Data Type
Currently, VECTOR_
is not supported with the vector data type.
Using VECTOR_
with the vector data type will result in an error.
SELECT vector_sum(position) FROM vector_exps;
ERROR 1706 (HY000): Feature 'VECTOR_SUM aggregate function with VECTOR datatype'
is not supported by SingleStore.
There is a way to use VECTOR_
with a vector data type.
Example
Create a table with a vector data type:
CREATE TABLE vector_exps(id INT, position vector(4));
Insert some vector values:
INSERT INTO vector_exps VALUES(1,'[1,2,3,4]'),(2,'[5,6,7,8]');
Cast to a blob data type:
SELECT vector_sum(position:>blob) FROM vector_exps;
+----------------------------+
| vector_sum(position:>blob) |
+----------------------------+
| ?A B B B |
+----------------------------+
Cast to a vector data type:
SELECT vector_sum(position:>blob):>vector(4) FROM vector_exps;
+---------------------------------------+
| vector_sum(position:>blob):>vector(4) |
+---------------------------------------+
| [6,8,10,12] |
+---------------------------------------+
Last modified: March 8, 2024