# Vector Type

An instance of the vector data type is an ordered collection of numeric values with a fixed number of dimensions. Vectors can represent various kinds of data, such as vector embeddings obtained from large language models (LLMs), vector embeddings representing images or faces, financial time series, spatial coordinates, velocities, colors, and other related information. Using the vector data type provides an easier way to insert, load, and query vectors.

```sql
VECTOR(<N> [ , <elementType> ])
```

* `<N>` is the number of elements (dimensions)
* `<elementType>` (optional) is the vector element type.

  * Element types supported are 8-, 16-, 32-, and 64-bit integers and 16-, 32-, and 64-bit floating-point numbers.
  * Options for `elementType` are `I8`, `I16`, `I32`, `I64`, `F16`, `F32`, and `F64`.
  * The default for `<elementType>` is 32-bit floating-point number (`F32`).
* `VECTOR_SUBVECTOR` is currently not supported with the `VECTOR` data type.
* `DOT_PRODUCT` and `EUCLIDEAN_DISTANCE` are the only functions supported on the `F16` element type. [Cast](https://docs.singlestore.com/db/v9.1/reference/sql-reference/conditional-functions/cast-or-convert.md) an `F16` vector to a `F32` vector to use other vector functions.

The input data for a `VECTOR (<N>, ...)` must have exactly `<N>` elements.

Refer to [Working with Vector Data](https://docs.singlestore.com/db/v9.1/developer-resources/functional-extensions/working-with-vector-data.md) and [How to Bulk Load Vectors](https://docs.singlestore.com/db/v9.1/developer-resources/functional-extensions/how-to-bulk-load-vectors.md) for more information on using the `VECTOR` type.

## Compare F16 and F32 Element Types

`F16` vectors offer these advantages over `F32` vectors:

* Use approximately half the storage space.
* Provide 35% faster kNN (k-Nearest Neighbor) search.
* Provide similar speed and recall for ANN (Approximate Nearest Neighbor) search.

kNN search, also called exact kNN search, searches an entire vector dataset using metrics such as [DOT\_PRODUCT](https://docs.singlestore.com/db/v9.1/reference/sql-reference/vector-functions/dot-product.md) or [EUCLIDEAN\_DISTANCE](https://docs.singlestore.com/db/v9.1/reference/sql-reference/vector-functions/euclidean-distance.md) to find the top k vectors that are most similar to a query vector. In contrast, ANN search uses a [vector index](https://docs.singlestore.com/db/v9.1/reference/sql-reference/vector-functions/vector-indexing.md) to very efficiently find a set of k near neighbors (an approximate set of near neighbors).

For nearest neighbor search, SingleStore recommends using:

* `F16` in most cases as it saves both space and execution time over the default `F32`.
* `F32` only when the highest possible [recall](https://en.wikipedia.org/wiki/Precision_and_recall) is critical.

## Remarks

> **❗ Important**: String functions, such as [CONCAT](https://docs.singlestore.com/db/v9.1/reference/sql-reference/string-functions/concat.md), [LENGTH](https://docs.singlestore.com/db/v9.1/reference/sql-reference/string-functions/length.md), [SUBSTRING](https://docs.singlestore.com/db/v9.1/reference/sql-reference/string-functions/substring.md), and [HEX](https://docs.singlestore.com/db/v9.1/reference/sql-reference/string-functions/hex.md) operate on the binary representation of a vector. To use a string function on the JSON string representation of a vector, write code as follows, where `vec` is the name of a column of type `VECTOR`.```sql
> STRINGFUNC(vec :> JSON)
> ```

## Output Format for Examples

Vectors can be output in JSON or binary format. Use JSON format for examples and for output readability. For production, use the default binary for efficiency.

Use the following command to output vectors in JSON format.

```sql
SET vector_type_project_format = JSON;
```

Use the following command to set the output format back to binary.

```sql
SET vector_type_project_format = BINARY;
```

## Examples Using Vector Data Type

## Using Vector Data Type in Tables

Create a table and insert values using the `VECTOR` data type. The `elementType` defaults to `F32`.

```sql
CREATE TABLE exp_data(col_a VECTOR(3));

INSERT INTO exp_data VALUES('[0.267261237,0.534522474,0.801783681]');
INSERT INTO exp_data VALUES('[0.371390671,0.557085991,0.742781341]');
INSERT INTO exp_data VALUES('[0.424264073,0.565685451,0.707106829]');

```

Confirm that the vectors have been loaded:

```sql
SET vector_type_project_format = JSON;  /* to make vector output readable */

SELECT col_a FROM exp_data;

```

```output

+---------------------------------------+
| col_a                                 |
+---------------------------------------+
| [0.267261237,0.534522474,0.801783681] |
| [0.424264073,0.565685451,0.707106829] |
| [0.371390671,0.557085991,0.742781341] |
+---------------------------------------+ 
```

## Using Vector Data Type with Integer Element Type

This example shows how to create a `VECTOR` with an `elementType` of 16-bit integer (`I16`) to demonstrate how to use an `elementType` other than the default.

```sql
CREATE TABLE exp_data_i16(col_a VECTOR(3, I16));

INSERT INTO exp_data_i16 VALUES('[1, 2, 3]');
INSERT INTO exp_data_i16 VALUES('[4, 5, 6]');
INSERT INTO exp_data_i16 VALUES('[1, 4, 8]');
```

Confirm that the vectors were loaded.

```sql
SET vector_type_project_format = JSON;  /* to make vector output readable */

SELECT col_a FROM exp_data_i16;

```

```output

+---------------------------------------+
| col_a                                 |
+---------------------------------------+
| [1, 2, 3]                             |
| [4, 5, 6]                             |
| [1, 4, 8]                             |
+---------------------------------------+ 
```

## A Note on Dot Product and Euclidean Distance

Two common operations on vectors are dot product and Euclidean distance. SingleStore supports [DOT\_PRODUCT](https://docs.singlestore.com/db/v9.1/reference/sql-reference/vector-functions/dot-product.md) and [EUCLIDEAN\_DISTANCE](https://docs.singlestore.com/db/v9.1/reference/sql-reference/vector-functions/euclidean-distance.md) functions and infix operators for those functions:

* `<*>` for `DOT_PRODUCT`
* `<->` for `EUCLIDEAN_DISTANCE`

The `DOT_PRODUCT` (`<*>`) and `EUCLIDEAN_DISTANCE` (`<->`) operators take as input two vector expressions and require that both operands have the same number of elements and are the same element type. In some cases, the system automatically converts between JSON strings, `BLOB`s, and `VECTOR`s. Refer to [DOT\_PRODUCT](https://docs.singlestore.com/db/v9.1/reference/sql-reference/vector-functions/dot-product.md) and [EUCLIDEAN\_DISTANCE](https://docs.singlestore.com/db/v9.1/reference/sql-reference/vector-functions/euclidean-distance.md) for more information.

## Migrate to F16 from F32

Conversions from F32 to F16 vectors are supported. To convert a table that uses F32 vectors to F16 vectors:

1. Add a new column with type `VECTOR(<N>, F16)` to the table.

2. Use an `UPDATE … SET` statement to convert the F32 values to F16 values.

3. Use `DESC` to verify the column types are correct.

4. If there is an index on the F32 column, drop the index.

5. Drop the F32 column.

6. Rename the new column to the original column name.

7. Create an index on the new column, if desired.

Given the following `CREATE TABLE` and `ADD INDEX` statement for a table with a column of F32 vectors.

```sql
CREATE TABLE t_vecs(c_orig VECTOR(1024, F32));

ALTER TABLE t_vecs ADD VECTOR INDEX i_orig (c_orig)
   INDEX_OPTIONS '...';
```

Add a new column of type F16 (`c_new`) and set the values of that column equal to the values from the original column (`c_orig`).

```sql
ALTER TABLE t_vecs ADD column c_new VECTOR(1024,F16);

UPDATE t_vecs SET c_new = c_orig;
```

Use `DESC` to confirm the data types in the new table are correct.

```sql
DESC t_vecs; 

```

```output

+--------+-------------------+------+------+---------+-------+
| Field  | Type              | Null | Key  | Default | Extra |
+--------+-------------------+------+------+---------+-------+
| c_orig | vector(1024, F32) | YES  | MUL  | NULL    |       |
| c_new  | vector(1024, F16) | YES  |      | NULL    |       |
+--------+-------------------+------+------+---------+-------+

```

Drop the index on the F32 column. Then drop the F32 column itself. Alternatively, to keep the F32 data, rename the F32 column instead of dropping it.

```sql
DROP INDEX i_orig ON t_vecs;

ALTER TABLE t_vecs DROP COLUMN c_orig;
```

Add an index to the new column, if applicable, and rename the new column to the original column name.

```sql
ALTER TABLE t_vecs ADD VECTOR INDEX i_new (c_new)
   INDEX_OPTIONS '...';

ALTER TABLE t_vecs CHANGE c_new c_orig;
```

## Transition from Blob to Vector Data Type

## Transition from Blob to Vector Data Type in a Rowstore Table

Prior to the addition of the VECTOR data type, SingleStore supported storing vectors as BLOB values. With the introduction of this data type, you may wish to update your applications to use VECTORs instead of BLOBs.

The column type in a rowstore table can be easily modified from blob to vector using the `ALTER TABLE MODIFY` statement.

To simulate an existing application that stores vectors as blobs, create a table using the BLOB data type and enter vectors using `JSON_ARRAY_PACK`.

```sql
CREATE ROWSTORE TABLE exp_data2(col_b BLOB);

INSERT INTO exp_data2 VALUES(JSON_ARRAY_PACK('[1,2,3]'));
INSERT INTO exp_data2 VALUES(JSON_ARRAY_PACK('[3,5,9]'));

```

Confirm that the vectors have been inserted into the table.

```sql
SELECT JSON_ARRAY_UNPACK(col_b) FROM exp_data2;

```

```output

+--------------------------+
| JSON_ARRAY_UNPACK(col_b) |
+--------------------------+
| [1,2,3]                  |
| [3,5,9]                  |
+--------------------------+

```

Use `ALTER TABLE` and `MODIFY` to convert the blob data type to a vector data type.

```sql
ALTER TABLE exp_data2 MODIFY col_b VECTOR(3);
```

Confirm that the column has been modified to a vector data type. Note that the element type of the vector is `F32`, the default.

```sql
SHOW COLUMNS FROM exp_data2;

```

```output

+---------+----------------+------+------+---------+-------+
| Field   | Type           | Null | Key  | Default | Extra |
+---------+----------------+------+------+---------+-------+
| col_b   | vector(3, F32) | YES  |      | NULL    |       |
+---------+----------------+------+------+---------+-------+

```

Confirm that the table reflects the vector data type.

```sql
SET vector_type_project_format = JSON;  /* to make vector output readable */

SELECT col_b FROM exp_data2;

```

```output

+--------+
|col_b   |
+--------+
|[3,5,9] |
|[1,2,3] |
+--------+

```

## Transition from Blob to Vector Data Type in a Columnstore Table

The following steps outline how to change a columnstore blob column to vector.

1. Add a new vector column to the right of the blob column.

2. Update the vector column with the data from the blob column.

3. Drop the blob column.

4. Rename the new vector column to the old blob column name. This change ensures any previous queries still work, or at least require fewer changes.

These steps are demonstrated in the following example.

## Columnstore Example

Create a table and review the table details using the `DESC` statement.

```sql
CREATE TABLE test(col_1 int, col_2 blob, col_3 int);

DESC test;

```

```output

+---------+---------+------+------+---------+-------+
| Field   | Type    | Null | Key  | Default | Extra |
+---------+---------+------+------+---------+-------+
| col_1   | int(11) | YES  |      | NULL    |       |
| col_2   | blob    | YES  |      | NULL    |       |
| col_3   | int(11) | YES  |      | NULL    |       |
+---------+---------+------+------+---------+-------+

```

Insert data into the table, add a new column to the table (`col2`), and update the new column (`col2`) with the data from the old column (`col_2`).

```sql
INSERT test VALUES(1,JSON_ARRAY_PACK("[1,2,3]"), 100);
INSERT test VALUES(2,JSON_ARRAY_PACK("[2,3,4]"), 200);

ALTER TABLE test ADD COLUMN col2 vector(3) AFTER col_2;

UPDATE test SET col2 = col_2; 
```

Check the table to confirm that the data has been moved to the new column (`col2`).

```sql
SET vector_type_project_format = JSON;  /* to make vector output readable */

SELECT col_1, JSON_ARRAY_UNPACK(col_2), col2, col_3 FROM test;

```

```output

+--------+---------------------------+---------+-------+
| col_1  | JSON_ARRAY_UNPACK(col_2)  | col2    | col_3 |
+--------+---------------------------+---------+-------+
|      1 | [1,2,3]                   | [1,2,3] |  100  |
|      2 | [2,3,4]                   | [2,3,4] |  200  |
+--------+---------------------------+---------+-------+ 
```

Drop the old column (`col_2`) and rename the new column (`col2`) using the old column name (`col_2`).

```sql
ALTER TABLE test DROP COLUMN col_2;

ALTER TABLE test CHANGE col2 col_2; 

DESC test;

```

```output

+---------+----------------+------+------+---------+-------+
| Field   | Type           | Null | Key  | Default | Extra |
+---------+----------------+------+------+---------+-------+
| col_1   | int(11)        | YES  |      | NULL    |       |
| col_2   | vector(3, F32) | YES  |      | NULL    |       |
| col_3   | int(11)        | YES  |      | NULL    |       |
+---------+----------------+------+------+---------+-------+
```

## Casting between Blob and Vector

Casting is a type of conversion that allows a value to be converted from one data type to another. The following examples show how to cast a blob data type to a vector data type and vice versa.

Continuing with the table and values from the previous example, applying the following `SELECT` statement returns a vector type value in the left column (`col_2`).

The blob produced by the `UNHEX()` function is automatically cast as a vector of the correct length, which matches the type of the `<*>` operator’s left argument. If the length is not correct, an error occurs.

```sql
SET vector_type_project_format = JSON;  /* to make vector output readable */

SELECT col_2, col_2 <*> UNHEX("0000803F0000803F0000803F") 
FROM test;


```

```output

+-----------+---------------------------------------------+
| col_2     | col_2 <*> UNHEX("0000803F0000803F0000803F") |
+-----------+---------------------------------------------+
| [1,2,3]   |                                           6 |
| [2,3,4]   |                                           9 |
+-----------+---------------------------------------------+
```

Casting can be done in either direction using the `:>` operator. For example:

```sql
SET vector_type_project_format = JSON;  /* to make vector output readable */

SELECT "[1,2,3]" :> VECTOR(3) :> BLOB :> VECTOR(3);

```

```output

+---------------------------------------------+
| "[1,2,3]" :> VECTOR(3) :> BLOB :> VECTOR(3) |
+---------------------------------------------+
| [1,2,3]                                     |
+---------------------------------------------+
```

When writing UDFs that operate on vectors of different lengths, casting can be used to allow a function to accept a blob argument instead of a fixed-length vector. Refer to the [vector normalization](https://docs.singlestore.com/db/v9.1/reference/sql-reference/vector-functions/vector-normalization.md) topic for an example of the `normalize` function, which produces the results in the following example. You may also use this function on vector-type values of a fixed length, where an automatic casting from vector to blob is performed. For example:

```sql
SET vector_type_project_format = JSON;  /* to make vector output readable */

SET @v = "[1,2,3,4]" :> VECTOR(4);

SELECT normalize(@v) :> VECTOR(4);

```

```output

+---------------------------------------------------+
| normalize(@v) :> VECTOR(4)                        |
+---------------------------------------------------+
| [0.182574183,0.365148365,0.547722578,0.730296731] |
+---------------------------------------------------+

```

Casting may be used to write helper functions, which are functions that call another function. For example, a helper function can be employed to operate on a fixed-length vector. Helper functions are useful when rewriting the logic of the calling function is impossible or impractical.

To illustrate, `normalize` treats a vector of any length as a blob due to the implicit casting of its argument from vector to blob. The following function, `norm1536`, is a thin wrapper around the `normalize` function. It normalizes a typed vector argument of length 1536 and returns a typed vector argument of length 1536.

```sql
DELIMITER //
CREATE OR REPLACE FUNCTION norm1536(v VECTOR(1536)) RETURNS VECTOR (1536) AS
BEGIN
   RETURN normalize(v);
END //
DELIMITER ; 
```

The `norm1536` function can be used to write queries with no explicit casting required. This particular function is useful when working with vectors that are always of type `VECTOR(1536)` and the desired return value is explicitly typed as `VECTOR(1536)`.

## Troubleshooting

Trying to insert vectors with incorrect element types or lengths causes an error. The following are two common errors and their causes.

To demonstrate these errors, create a table `t_invalid` with a `VECTOR` attribute `v` of length 3. The element type of `v` is 32-bit floating-point number (`F32`) as `F32` is the default element type for `VECTOR`s.

```sql
CREATE TABLE t_invalid(v VECTOR(3)); 
```

The following error occurs when attempting to insert a vector `'[8,2,4a]'`, with an element that does not match the element type of the vector attribute, `v`. Specifically, `'4a'` cannot be converted into a 32-bit floating-point number.

```sql
INSERT INTO t_invalid VALUES ('[8,2,4a]');

```

```output

ERROR 2856 (HY000): Invalid VECTOR value for column 'v',
  reason: 'Value must be a JSON array of numbers.

```

The following error occurs when attempting to insert a vector `'[1,6,7,9]'` with a number of elements that is different than the specified length for the vector column, `v`.

```sql
INSERT INTO t_invalid VALUES ('[1,6,7,9]');

```

```output

ERROR 2856 (HY000): Invalid VECTOR value for column 'v',
  reason: 'Cannot cast vector (4, F32) to a vector (3, F32). 
  Ensure the lengths are equal and the element types are the same.'

```

## Related Topics

* [JSON\_ARRAY\_PACK](https://docs.singlestore.com/db/v9.1/reference/sql-reference/vector-functions/json-array-pack.md)
* [Vector Functions](https://docs.singlestore.com/db/v9.1/reference/sql-reference/vector-functions.md)
* [Working with Vector Data](https://docs.singlestore.com/db/v9.1/developer-resources/functional-extensions/working-with-vector-data.md)
* [How to Bulk Load Vectors](https://docs.singlestore.com/db/v9.1/developer-resources/functional-extensions/how-to-bulk-load-vectors.md)
* [DOT\_PRODUCT](https://docs.singlestore.com/db/v9.1/reference/sql-reference/vector-functions/dot-product.md)
* [EUCLIDEAN\_DISTANCE](https://docs.singlestore.com/db/v9.1/reference/sql-reference/vector-functions/euclidean-distance.md)

***

Modified at: March 19, 2026

Source: [/db/v9.1/reference/sql-reference/data-types/vector-type/](https://docs.singlestore.com/db/v9.1/reference/sql-reference/data-types/vector-type/)

(An index of the documentation is available at /llms.txt)
