VECTOR_ KTH_ ELEMENT
On this page
VECTOR_
is a function that returns the k-th element in a vector expression.
Syntax
VECTOR_KTH_ELEMENT(vector_expression, k)
Arguments
-
vector_
: An expression that evaluates to a vector.expression Vectors can be stored in SingleStore using the BLOB
type (BLOB Types).
-
k
: A 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
When the vector_BLOB
, then the return type will be by default a BLOB
that contains a vector encoded as 32-bit floating point numbers (F32
).BLOB
that contains a vector encoded using the type of the suffix.
See Using Suffixes for Other Element Types with BLOBs for more information on using suffixes with vectors encoded as BLOB
s.
Using VECTOR_ KTH_ ELEMENT with Vectors as BLOBs
The following examples and descriptions show the use of VECTOR_
to multiply the elements of vector stored as a BLOB
.
Example 1 - BLOB Argument
Create a table with a column of type BLOB
to store the vectors.vec
and type BLOB
, will store the vectors.BLOB
s, hence the column of type BLOB
named vec
.
Then use the JSON_
built-in function to easily insert properly formatted 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 SQL extracts the 2nd element of each of those vectors.
SELECT id, VECTOR_KTH_ELEMENT(vec, 1)FROM vectors_bORDER BY id;
+------+----------------------------+
| id | VECTOR_KTH_ELEMENT(vec, 1) |
+------+----------------------------+
| 1 | 0.800000011920929 |
| 2 | 0.550000011920929 |
+------+----------------------------+
The return type in this case will be a 32-bit floating-point number as that is the default type for vector element storage.
Using Suffixes for Other Element Types with BLOBs
The default element type for vector storage and processing is 32-bit floating point (F32
).
You can specify the datatype of the vector elements to be used in the operation by adding a suffix to the function._
.
When using a suffix, the return type will be the type of the suffix.
The following table lists the suffixes and their data type.
Suffix |
Data Type |
---|---|
|
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) |
Example 2 - BLOBs with 16-bit Integers
Below is an example of using JSON_
and VECTOR_
with 16-bit signed integers.
First create a table of vectors stored as 16-bit integers._
suffix on JSON_
.
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 SQL extracts the 2nd element of each of those vectors.
SELECT id, VECTOR_KTH_ELEMENT_I16(vec, 1)FROM vectors_b_iORDER BY id;
+------+--------------------------------+
| id | VECTOR_KTH_ELEMENT_I16(vec, 1) |
+------+--------------------------------+
| 1 | 3 |
| 2 | 4 |
+------+--------------------------------+
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_
.BLOB
s.BLOB
containing packed numbers in little-endian byte order.BLOB
s can be of any length; however, the input blob length must be divisible by the size of the packed vector elements (1, 2, 4 , or 8 bytes, depending on the vector element).
Related Topics
Last modified: May 30, 2024