VECTOR_SUBVECTOR

Derives a vector expression from another vector expression.

It is a scalar function.

Syntax

VECTOR_SUBVECTOR(vector_expression, start_position, length)

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.

  • start_position: A 0-indexed number to determine where to start count. A positive index counts from the beginning of the vector. A negative index counts from the end of the vector.

  • length: Length of required subvector

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

_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)

Examples

CREATE TABLE vsv_t (x BLOB);
INSERT INTO vsv_t VALUES (JSON_ARRAY_PACK('[1,2,3,4,5]'));
SELECT JSON_ARRAY_UNPACK (VECTOR_SUBVECTOR(x,2,2)) AS x from vsv_t;
+-------+
| x     |
+-------+
| [3,4] |
+-------+
SELECT JSON_ARRAY_UNPACK (VECTOR_SUBVECTOR(x,-2,2)) AS x from vsv_t;
+-------+
| x     |
+-------+
| [4,5] |
+-------+

How to Use the Vector Data Type

Currently, VECTOR_SUBVECTOR is not supported with the vector data type. A workaround is provided until support for the vector data type is implemented.

Using VECTOR_SUBVECTOR with the vector data type will result in an error.

SELECT vector_subvector(position, 1, 2) FROM vector_exps;
ERROR 1940 ER_VECTOR_FUNCTION_ERROR: Error with a vector function: 
Function `vector_subvector` is not supported with VECTOR datatype.

There is a way to use VECTOR_SUBVECTOR with a vector data type. First cast to a blob data type and then cast back to a vector data type. The example below shows how to do this:

Example

Set the position column to use vectors and the vector data type:

SET @pos = '[1,2,3,4]':>vector(4);

Cast to a blob data type:

SELECT vector_subvector(@pos:>blob, 1,2);
+------------------------------------+
| vector_subvector(@pos:>blob, 1, 2) |
+------------------------------------+
|    @@@                             |
+------------------------------------+

Cast to a vector data type:

SELECT vector_subvector(@pos:>blob, 1, 2):>vector(2);
+-----------------------------------------------+
| vector_subvector(@pos:>blob, 1, 2):>vector(2) |
+-----------------------------------------------+
| [2,3]                                         |
+-----------------------------------------------+

Last modified: January 26, 2024

Was this article helpful?