Vector Operations

VECTOR_ENFORCE

VECTOR_ENFORCE(array, length, type) Returns the input vector if it fulfills the length and type requirements. Otherwise returns null. Input the length argument as an int and the type argument as the string, int, or float.

Use VECTOR_ENFORCE within your ingest transformations to ensure compatibility between vectors during query execution. VECTOR_ENFORCE also signals to Rockset that the ingested array should be treated as a vector for storage and access optimizations.

SELECT VECTOR_ENFORCE([1, 2, 3], 3, 'int')
[1, 2, 3]
SELECT VECTOR_ENFORCE([1, 2, 3], 3, 'float')
null
SELECT VECTOR_ENFORCE([1.5, 2.6, 3.7, 4.8], 4, 'float')
[1.5, 2.6, 3.7, 4.8]
SELECT VECTOR_ENFORCE([1.5, 2.6, 3.7, 4.8], 1, 'float')
null
SELECT VECTOR_ENFORCE('[1.5, 2.6, 3.7, 4.8]', 4, 'float')
Error: Arguments of types (string, int, string) are invalid for function `vector_enforce`
SELECT VECTOR_ENFORCE(JSON_PARSE('[1.5, 2.6, 3.7, 4.8]'), 4, 'float')
[1.5,2.6,3.7,4.8]

VECTOR_ADD

VECTOR_ADD(array, addend) Adds a scalar of type int or float to a vector. Alternatively, adds two vectors.

SELECT VECTOR_ADD([1, 2, 3, 4], 4)
[5, 6, 7, 8]
SELECT VECTOR_ADD([1, 2, 3, 4], [5, 6, 7, 8])
[6, 8, 10, 12]
SELECT VECTOR_ADD([1.5, 2.6, 3.7, 4.8], 4.4)
[5.9, 7.0, 8.1, 9.2]
SELECT VECTOR_ADD([1.1, 2.2, 3.3, 4.3], [1.5, 2.6, 3.7, 4.8])
[2.6, 4.8, 7.0, 9.1]
SELECT VECTOR_ADD([1.1, 2.2, 3.3, 4.3], ['0':1.5, '1':2.6, '2':3.7, '3':4.8])
Error: Cannot perform vector operations on datatype object.

VECTOR_SUBTRACT

VECTOR_SUBTRACT(array, subtrahend) Subtracts a scalar of type int or float from a vector. Alternatively, subtracts a vector from another vector.

SELECT VECTOR_SUBTRACT([5, 6, 7, 8], 4)
[1, 2, 3, 4]
SELECT VECTOR_SUBTRACT([5, 6, 7, 8], [1, 2, 3, 4])
[4, 4, 4, 4]
SELECT VECTOR_SUBTRACT([5.1, 6.2, 7.3, 8.4], 4.4)
[0.7, 1.8, 2.9, 4.0]
SELECT VECTOR_SUBTRACT([5.5, 6.6, 7.7, 8.8], [1.1, 2.2, 3.3, 4.4])
[4.4, 4.4, 4.4, 4.4]
SELECT VECTOR_SUBTRACT([1.1], [5.5, 6.6, 7.7, 8.8])
Error: Cannot apply operation VECTOR_SUBTRACT on vectors of different sizes 1 and 4.

VECTOR_MULTIPLY

VECTOR_MULTIPLY(array, multiplier) Multiplies a vector by a scalar of type int or float or computes the element-wise multiplication of two vectors as the Hadamard product.

SELECT VECTOR_MULTIPLY([1, 2, 3, 4], 2)
[2, 4, 6, 8]
SELECT VECTOR_MULTIPLY([1, 2, 3, 4], [1, 2, 3 , 4])
[1, 4, 9, 16]
SELECT VECTOR_MULTIPLY([5, 6, 7, 8], 1.5)
[7.5, 9.0, 10.5, 12.0]
SELECT VECTOR_MULTIPLY([5.5, 6.6, 7.7, 8.8], [5.5, 6.6, 7.7, 8.8])
[30.25, 43.56, 59.29, 77.44]
SELECT VECTOR_MULTIPLY([5.5, 6.6, 7.7, 8.8], [5.5, 6.6, 7.7, 'foo'])
Error: Cannot perform vector operations on datatype `string`.

VECTOR_DIVIDE

VECTOR_DIVIDE(array, divisor) Divides a vector by a scalar of type int or float or computes the element-wise divison of two vectors. Throws an error when division by zero occurs.

SELECT VECTOR_DIVIDE([2, 4, 6, 8], 2)
[1, 2, 3, 4]
SELECT VECTOR_DIVIDE([2, 4, 6, 8], [2, 4, 6, 8])
[1, 1, 1, 1]
SELECT VECTOR_DIVIDE([2.2, 4.4, 6.6, 8.8], 2.2)
[1.0, 2.0, 3.0, 4.0]
SELECT VECTOR_DIVIDE([2.2, 4.4, 6.6, 8.8], [2.2, 4.4, 6.6, 8.8])
[1.0, 1.0, 1.0, 1.0]
SELECT VECTOR_DIVIDE([2, 4, 6, 8], 0)
Error: The divisor in a VECTOR_DIVIDE operation was zero.
SELECT VECTOR_DIVIDE([2.2, 4.4, 6.6, 8.8], [2.2, 4.4, 0.0, 0.0])
Error: The divisor in a VECTOR_DIVIDE operation was zero.