VECTOR_KTH_ELEMENT

Returns the k-th element in a vector expression where k is 0-indexed.

It is a scalar function.

Syntax

VECTOR_KTH_ELEMENT(vector_expression, k)

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.

  • k: An 0-indexed number to determine which element in the vector to be returned. A positive index counts from the beginning of the vector. A negative index counts from the end of the vector.

Return Type

An element at the K-th position of the given vector. An error that might be returned for out-of-bounds access: "ERROR 2609 (HY000): Index ... is out of range for an array/subarray of size ..."

Remarks

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.

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. Omitting the suffix from the function is equivalent to suffixing it with _F32. All operations are done using the specified data 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)

Examples

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

Last modified: December 18, 2023

Was this article helpful?