VECTOR_SUM

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_expression: An expression that evaluates to a vector. 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

A vector can be of any length, but all input vectors must be the same length and the blob lengths must be divisible by the packed vector element size (1, 2, 4 or 8 bytes depending on the datatype of the vector element).

You can specify the datatype of the vector elements in which this operation is performed on the vector by adding a suffix to the function. Omitting the suffix from the function is equivalent to suffixing it with _F32. All operations are done using the specified datatype. The following table lists the suffixes and their datatype.

Suffix

Datatype

_I8

8-bit signed integer

_I16

16-bit signed integer

_I32

32-bit signed integer

_I64

64-bit signed integer

_F32

32-bit floating-point number (IEEE standard format)

_F64

64-bit floating-point number (IEEE standard format)

Example

The following examples use the JSON_ARRAY_UNPACK() 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_SUM() 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 Datatype 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_SUM() function.

-- _i8 suffix
SELECT 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 suffix
SELECT 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 suffix
SELECT 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 suffix
SELECT 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 suffix
SELECT 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)

Last modified: February 28, 2023

Was this article helpful?