Skip to main content

Vector Functions

Turso Extension: Vector functions are a Turso-specific feature for working with vector embeddings directly in SQL. These functions are not part of the SQLite standard.
Turso provides built-in functions for storing and querying vector embeddings. Vectors are stored as BLOBs in a compact binary format and can be compared using distance functions for similarity search.

Vector Creation

vector32

Creates a 32-bit floating-point vector from a JSON array of numbers.
Returns: BLOB — the vector in 32-bit float binary format. This is the most common vector format and offers a good balance of precision and storage efficiency. Each dimension uses 4 bytes.

vector64

Creates a 64-bit floating-point vector from a JSON array of numbers.
Returns: BLOB — the vector in 64-bit float binary format. Use this format when you need higher precision. Each dimension uses 8 bytes.

vector

Creates a vector using the default format (32-bit float).
Returns: BLOB — the vector in default (32-bit float) binary format.

vector8

Creates an 8-bit quantized vector from a JSON array of numbers.
Returns: BLOB — the vector in 8-bit integer binary format. Use this format for storage-efficient approximate representations. Each dimension uses 1 byte.

vector1bit

Creates a 1-bit binary vector from a JSON array.
Returns: BLOB — the vector in 1-bit binary format. Use this format for extremely compact representations. Eight dimensions are packed into a single byte.

vector32_sparse

Creates a 32-bit floating-point sparse vector from a JSON array. Only non-zero components are stored, which is efficient for high-dimensional vectors that are mostly zero.
Returns: BLOB — the vector in 32-bit float sparse binary format.
Sparse vectors can be indexed with the experimental sparse vector index method (enable --experimental-index-method). See CREATE INDEX.

Distance Functions

Distance functions compute the distance between two vectors. Smaller values indicate more similar vectors.

vector_distance_cos

Computes the cosine distance between two vectors, defined as 1 - cosine_similarity. A result of 0 means the vectors are identical in direction; a result of 1 means they are orthogonal.
Returns: REAL — the cosine distance between the two vectors.

vector_distance_l2

Computes the Euclidean (L2) distance between two vectors. A result of 0 means the vectors are identical.
Returns: REAL — the Euclidean distance between the two vectors.

vector_distance_dot

Computes the negative dot product distance between two vectors. The result is the negation of the dot product, so smaller (more negative) values indicate higher similarity.
Returns: REAL — the negative dot product of the two vectors.

vector_distance_jaccard

Computes the Jaccard distance between two binary vectors. Best suited for use with vector1bit vectors.
Returns: REAL — the Jaccard distance between the two vectors.

Utility Functions

vector_extract

Converts a vector BLOB back to a JSON text representation.
Returns: TEXT — a JSON array string of the vector’s components.

vector_concat

Concatenates two or more vectors into a single vector.
Returns: BLOB — a new vector containing the dimensions of all input vectors in order.

vector_slice

Extracts a contiguous portion of a vector.
Returns: BLOB — a new vector containing dimensions from start to end - 1.

Examples

Creating a table with vector embeddings

Similarity search with ORDER BY distance

Cosine distance vs. L2 distance

Cosine distance measures the angle between two vectors, ignoring magnitude. L2 distance measures the absolute distance in space.
Use cosine distance when you care about the direction of the vector (e.g., semantic similarity of text embeddings). Use L2 distance when absolute position matters (e.g., geospatial coordinates or image feature vectors).

Working with vector data

By default, similarity searches use a linear scan over the table. An experimental sparse vector index method is available behind the --experimental-index-method flag; without it, consider limiting search to a subset of rows with a WHERE clause for large datasets.

See Also