SCALAR_ VECTOR_ MUL
On this page
The SCALAR_
function multiples each element in a vector with a scalar value.
Syntax
SCALAR_VECTOR_MUL(scalar_value, vector_expression)
Arguments
-
scalar_
: A scalar value.value
-
vector_
: An expression that evaluates to a vector.expression Vectors can be stored in SingleStore using the native VECTOR
type (Vector Type) or theBLOB
type (BLOB Types).SingleStore recommends using the VECTOR
type when possible.
Return Type
The function returns the type of the second argument.
Output Format for Examples
Vectors may be output in JSON or binary format.
To get JSON output which will match the examples, use the following command to output vectors in JSON.
SET vector_type_project_format = JSON;
Use the following command to set the output format back to binary.
SET vector_type_project_format = BINARY;
Using SCALAR_ VECTOR_ MUL with the VECTOR Data Type
Example 1 - Vector with F32 Element Type
First create a table of vectors of length 4 using the VECTOR
data type and insert data into that table.
CREATE TABLE vectors (id int, vec VECTOR(4) not null);INSERT INTO vectors VALUES (1, '[0.45, 0.55, 0.495, 0.5]');INSERT INTO vectors VALUES (2, '[0.1, 0.8, 0.2, 0.555]');INSERT INTO vectors VALUES (3, '[-0.5, -0.03, -0.1, 0.86]');INSERT INTO vectors VALUES (4, '[0.5, 0.3, 0.807, 0.1]');
Note
The default element type for Vector Type is 32-bit floating point (F32
).
The following SQL multiplies each of those vectors by the scalar number 10.
SET vector_type_project_format = JSON; /* to make vector output readable */SELECT id, SCALAR_VECTOR_MUL(10, vec)FROM vectorsORDER BY id;
+------+---------------------------------+
| id | SCALAR_VECTOR_MUL(10, vec) |
+------+---------------------------------+
| 1 | [4.5,5.5,4.94999981,5] |
| 2 | [1,8,2,5.55000019] |
| 3 | [-5,-0.299999982,-1,8.60000038] |
| 4 | [5,3,8.06999969,1] |
+------+---------------------------------+
Example 2 - Vector with I16 Element Type
This example shows how to use SCALAR_
with a VECTOR
with an element type of 16-bit integer (I16
).
CREATE TABLE vectors_i16(id INT, vec VECTOR(3, I16));INSERT INTO vectors_i16 VALUES(1, '[1, 2, 3]');INSERT INTO vectors_i16 VALUES(2, '[4, 5, 6]');INSERT INTO vectors_i16 VALUES(3, '[1, 4, 8]');
The following SQL multiplies each of the vectors in that table by the scalar number 10.
SET vector_type_project_format = JSON; /* to make vector output readable */SELECT id, SCALAR_VECTOR_MUL(10, vec)FROM vectors_i16ORDER BY id;
+------+----------------------------+
| id | SCALAR_VECTOR_MUL(10, vec) |
+------+----------------------------+
| 1 | [10,20,30] |
| 2 | [40,50,60] |
| 3 | [10,40,80] |
+------+----------------------------+
Using SCALAR_ VECTOR_ MUL with Vectors as BLOBs
The following examples and descriptions show the use of SCALAR_
to multiply the a vector by a scalar when the elements of the vector are 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 query multiples the vectors in the vectors_
table by 10 and uses JSON_
to see the results in JSON format.
SELECT id, JSON_ARRAY_UNPACK(SCALAR_VECTOR_MUL(10, vec)) as scalar_mulFROM vectors_bORDER BY id;
+------+------------------------+
| id | scalar_mul |
+------+------------------------+
| 1 | [1,8,2,5.55000019] |
| 2 | [4.5,5.5,4.94999981,5] |
+------+------------------------+
JSON_
was used here to output the vectors in readable format because VECTOR_
returns a BLOB
when its second argument is a BLOB
.
The following query multiples the vectors in the vectors_
table by 10 and uses HEX
to view the results in hexadecimal format.
SELECT id, HEX(SCALAR_VECTOR_MUL(10, vec)) as scalar_mulFROM vectors_bORDER BY id;
+------+----------------------------------+
| id | scalar_mul |
+------+----------------------------------+
| 1 | 0000803F00000041000000409A99B140 |
| 2 | 000090400000B04066669E400000A040 |
+------+----------------------------------+
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.
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 |
---|---|
|
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 SCALAR_
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]'));
This following query multiplies the vectors in the vectors_
table by the scalar 10.
SELECT id, JSON_ARRAY_UNPACK_I16(SCALAR_VECTOR_MUL_I16(10, vec)) AS scalar_mulFROM vectors_b_iORDER BY id;
+------+-----------------+
| id | scalar_mul |
+------+-----------------+
| 1 | [10,30,20,50] |
| 2 | [230,40,10,80] |
+------+-----------------+
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).
Troubleshooting
Should you receive an unexpected NULL
value or values in the results, check the order of the arguments.SCALAR_
function returns NULL
if the order of arguments is reversed.
Using the vectors
table created above, the following query reverses the order of the arguments, putting the VECTOR
first and the scalar value (10
) second.NULL
values as is expected when the order of arguments is reversed.
SELECT id, SCALAR_VECTOR_MUL(vec, 10)FROM vectorsORDER BY id;
+------+----------------------------+
| id | SCALAR_VECTOR_MUL(vec, 10) |
+------+----------------------------+
| 1 | NULL |
| 2 | NULL |
| 3 | NULL |
| 4 | NULL |
+------+----------------------------+
Related Topics
Last modified: June 6, 2024