VECTOR_ADD
Adds two input vectors and returns a single vector blob.
It is a scalar function.
Syntax
VECTOR_ADD(vector_expression, vector_expression)
Arguments
vector_expression
: An expression that evaluates to a vector. The vector must be encoded as a blob containing packed 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 the input blob length must be divisible by the packed vector element size (1, 2, 4 or 8 bytes, depending on 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 |
---|---|
| 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) |
Examples
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 adds two vector blobs using the VECTOR_ADD()
function:
SELECT JSON_ARRAY_UNPACK(VECTOR_ADD(x,y)) "x+y" FROM v_t; **** +-------------------------+ | x+y | +-------------------------+ | [20.7000008,15.6000004] | | [9.79999924,19] | +-------------------------+
The vector blobs are added as 32-bit floating point numbers because no suffix is specified with the VECTOR_ADD()
function. You can also view the output vector blob in hexadecimal format using the HEX()
function.
SELECT HEX(VECTOR_ADD(x,y)) "x+y" FROM v_t; **** +------------------+ | x+y | +------------------+ | 9A99A5419A997941 | | CCCC1C4100009841 | +------------------+
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_ADD()
function. For example,
SELECT JSON_ARRAY_UNPACK_I16(VECTOR_ADD_I16(JSON_ARRAY_PACK_I16('[6,2]'),JSON_ARRAY_PACK_I16('[8,4]'))) AS "Result"; **** +--------+ | Result | +--------+ | [14,6] | +--------+