VECTOR_SUM

Adds all vectors in a column and returns a vector which is the sum of those vectors.

Syntax

VECTOR_SUM(vector_expression)

Arguments

  • vector_expression: An expression that evaluates to a vector. Vectors can be stored in SingleStore using the BLOB type (BLOB Types).

Return Type

Returns the type of the argument (the vector_expression).

Remarks

When using VECTOR_SUM on a column of type BLOB, the vectors in that column may be of any length. However, all the vectors in that column must have 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 data type of the vector element).

Using VECTOR_SUM with Vectors as BLOBs

The following examples demonstrate use of VECTOR_SUM with the BLOB type.

Example 1 - Adding Two Vectors

Create a table with a column of type BLOB to store the vectors and insert data into that table. The second column in this table, with column name vec and type BLOB, will store the vectors.

CREATE TABLE vectors_b (id int, vec BLOB not null);
INSERT INTO vectors_b VALUES (1, JSON_ARRAY_PACK('[0.1, 0.8, 0.2, 0.555]')); 
INSERT INTO vectors_b VALUES (2, JSON_ARRAY_PACK('[0.45, 0.55, 0.495, 0.5]'));

The following query sums the vectors in the vec column and uses JSON_ARRAY_UNPACK to see the results in JSON format.

SELECT JSON_ARRAY_UNPACK(VECTOR_SUM(vec)) AS vector_sum
FROM vectors_b;
+---------------------------------------------------+
| vector_sum                                        |
+---------------------------------------------------+
| [0.550000012,1.35000002,0.694999993,1.05500007]   |
+---------------------------------------------------+
1 row in set (0.01 sec)

The following query uses HEX to view the results in hexadecimal format.

SELECT HEX(VECTOR_SUM(vec)) AS vector_sum
FROM vectors_b;
+---------------------------------------------------+
| vector_sum                                        |
+---------------------------------------------------+
| CDCC0C3FCDCCAC3F85EB313F3E0A873F                  |
+---------------------------------------------------+

Using Suffixes for Other Element Types with BLOBs

The default element type for vector storage and processing is 32-bit floating point (F32). However, other element types are supported.

You can specify the datatype of the vector elements to be used in the operation by adding a suffix to the function. All operations are done using the specified datatype. Omitting the suffix from the function is equivalent to suffixing it with _F32.

When using a suffix, the return type will be the type specified by the suffix.

Note

The functions with suffixes do not work with the VECTOR type.

The following table lists the suffixes and their data type.

Suffix

Data Type

_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 2 - BLOBs with 16-bit Integers

Below is an example of using JSON_ARRAY_PACK and VECTOR_SUM with 16-bit signed integers.

Create a table of vectors stored as 16-bit integers. Note the use of the _I16 suffix on JSON_ARRAY_PACK.

CREATE TABLE vectors_b_i (id int, vec BLOB not null);
INSERT INTO vectors_b_i VALUES (1, JSON_ARRAY_PACK_I16('[1, 3, 2, 5]')); 
INSERT INTO vectors_b_i VALUES(2, JSON_ARRAY_PACK_I16('[23, 4, 1, 8]'));

The following query sums the vectors in the vectors_b_i table.

SELECT JSON_ARRAY_UNPACK_I16(VECTOR_SUM_I16(vec)) AS vector_sum
FROM vectors_b_i;
+--------------+
| vector_sum   |
+--------------+
| [24,7,3,13]  |
+--------------+

Note

Be sure that the suffixes you use to pack the vector data match the suffixes you use to unpack the data and the suffixes you use on functions to process that data.

Formatting Binary Vector Data for BLOBs

When using the BLOB type for vector operations, vector data can be formatted using JSON_ARRAY_PACK. If your vector data is already in a packed binary format, you can load that data into the BLOBs. The data must be encoded as a BLOB containing packed numbers in little-endian byte order. Vectors stored as BLOBs can be of any length; however, the input blob length must be divisible by the size of the packed vector elements .

Use VECTOR_SUM with the VECTOR Data Type

The VECTOR_SUM function is not supported with the VECTOR data type in this version. The following workaround is provided.

To use VECTOR_SUM with a column of type VECTOR, cast the column to a BLOB type and then cast back to a VECTOR type as shown in the following example.

Create a table with a VECTOR data type and insert data into that table.

CREATE TABLE vector_exps(id INT, position VECTOR(4));
INSERT INTO vector_exps VALUES(1,'[1,2,3,4]'),(2,'[5,6,7,8]');

Cast to a BLOB data type and back to a VECTOR data type.

SET vector_type_project_format = JSON; /* to make vector output readable */
SELECT VECTOR_SUM(position:>BLOB):>VECTOR(4) as vector_sum
FROM vector_exps;
+-----------------+
| vector_sum      |
+-----------------+
| [6,8,10,12]     |
+-----------------+

Last modified: February 12, 2025

Was this article helpful?

Verification instructions

Note: You must install cosign to verify the authenticity of the SingleStore file.

Use the following steps to verify the authenticity of singlestoredb-server, singlestoredb-toolbox, singlestoredb-studio, and singlestore-client SingleStore files that have been downloaded.

You may perform the following steps on any computer that can run cosign, such as the main deployment host of the cluster.

  1. (Optional) Run the following command to view the associated signature files.

    curl undefined
  2. Download the signature file from the SingleStore release server.

    • Option 1: Click the Download Signature button next to the SingleStore file.

    • Option 2: Copy and paste the following URL into the address bar of your browser and save the signature file.

    • Option 3: Run the following command to download the signature file.

      curl -O undefined
  3. After the signature file has been downloaded, run the following command to verify the authenticity of the SingleStore file.

    echo -n undefined |
    cosign verify-blob --certificate-oidc-issuer https://oidc.eks.us-east-1.amazonaws.com/id/CCDCDBA1379A5596AB5B2E46DCA385BC \
    --certificate-identity https://kubernetes.io/namespaces/freya-production/serviceaccounts/job-worker \
    --bundle undefined \
    --new-bundle-format -
    Verified OK