Vector functions are a Turso-specific extension for working with vector embeddings directly in SQL. They 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 Types
Turso supports four vector element types with different storage sizes and precision trade-offs:
| Type | Bits per dimension | Bytes per dimension | Use case |
|---|
vector32 | 32-bit float | 4 | General purpose; best balance of precision and storage |
vector64 | 64-bit float | 8 | High precision computations |
vector8 | 8-bit integer | 1 | Storage-efficient approximate representations |
vector1bit | 1-bit binary | 0.125 (8 per byte) | Extremely compact binary representations |
Vector Creation
vector32(json_array)
Creates a 32-bit floating-point vector from a JSON array of numbers. This is the most common vector format and the recommended default.
| Parameter | Type | Description |
|---|
json_array | TEXT | A JSON array of numbers, e.g. '[1.0, 2.0, 3.0]' |
Returns: BLOB — the vector in 32-bit float binary format (4 bytes per dimension).
SELECT vector32('[1.0, 2.0, 3.0]');
-- (BLOB: 12 bytes for a 3-dimensional vector)
CREATE TABLE documents (
id INTEGER PRIMARY KEY,
content TEXT,
embedding BLOB
);
INSERT INTO documents VALUES (
1,
'Introduction to databases',
vector32('[0.1, 0.3, 0.5, 0.7]')
);
vector64(json_array)
Creates a 64-bit floating-point vector. Use when you need higher precision. Each dimension uses 8 bytes.
SELECT vector64('[1.0, 2.0, 3.0]');
-- (BLOB: 24 bytes for a 3-dimensional vector)
vector(json_array)
Creates a vector using the default format (32-bit float). Equivalent to vector32.
SELECT vector('[1.0, 2.0, 3.0]');
vector8(json_array)
Creates an 8-bit quantized vector. Each dimension is stored as a single byte (0–255). Use for storage-efficient approximate representations.
SELECT vector8('[128, 64, 255, 0]');
-- (BLOB: 4 bytes for a 4-dimensional vector)
vector1bit(json_array)
Creates a 1-bit binary vector. Eight dimensions are packed into a single byte. Use for extremely compact representations.
SELECT vector1bit('[1, 0, 1, 1, 0, 0, 1, 0]');
-- (BLOB: 1 byte for an 8-dimensional binary vector)
Distance Functions
Distance functions compute the distance between two vectors. Smaller values indicate more similar vectors. Both vectors must have the same number of dimensions and the same type.
vector_distance_cos(v1, v2)
Computes the cosine distance between two vectors, defined as 1 - cosine_similarity. A result of 0 means the vectors are identical in direction; 1 means they are orthogonal.
Cosine distance measures direction, not magnitude, making it well-suited for semantic similarity of text embeddings.
vector_distance_cos(v1, v2)
Returns: REAL
SELECT vector_distance_cos(
vector32('[1.0, 0.0, 0.0]'),
vector32('[0.0, 1.0, 0.0]')
);
-- 1.0 (orthogonal vectors)
SELECT vector_distance_cos(
vector32('[1.0, 0.0]'),
vector32('[1.0, 0.0]')
);
-- 0.0 (identical vectors)
-- Cosine distance ignores magnitude
SELECT vector_distance_cos(
vector32('[1.0, 0.0]'),
vector32('[100.0, 0.0]')
);
-- 0.0 (same direction, different magnitude)
vector_distance_l2(v1, v2)
Computes the Euclidean (L2) distance between two vectors. A result of 0 means the vectors are identical. L2 distance measures absolute position in space, so magnitude matters.
vector_distance_l2(v1, v2)
Returns: REAL
SELECT vector_distance_l2(
vector32('[1.0, 0.0, 0.0]'),
vector32('[0.0, 1.0, 0.0]')
);
-- 1.4142135623730951
SELECT vector_distance_l2(
vector32('[3.0, 4.0]'),
vector32('[0.0, 0.0]')
);
-- 5.0
-- L2 distance reflects magnitude
SELECT vector_distance_l2(
vector32('[1.0, 0.0]'),
vector32('[100.0, 0.0]')
);
-- 99.0
vector_distance_dot(v1, v2)
Computes the negative dot product between two vectors. The result is the negation of the dot product, so smaller (more negative) values indicate higher similarity. Use when vectors are pre-normalized.
vector_distance_dot(v1, v2)
Returns: REAL (negative dot product)
SELECT vector_distance_dot(
vector32('[1.0, 2.0, 3.0]'),
vector32('[4.0, 5.0, 6.0]')
);
-- -32.0 (dot product is 32; negated to -32)
vector_distance_jaccard(v1, v2)
Computes the Jaccard distance between two binary vectors. Best suited for use with vector1bit vectors.
vector_distance_jaccard(v1, v2)
Returns: REAL
SELECT vector_distance_jaccard(
vector1bit('[1, 0, 1, 1]'),
vector1bit('[1, 1, 0, 1]')
);
Utility Functions
Converts a vector BLOB back to a JSON text representation.
vector_extract(vector_blob)
Returns: TEXT — a JSON array of the vector’s components.
SELECT vector_extract(vector32('[1.5, 2.5, 3.5]'));
-- [1.500000,2.500000,3.500000]
vector_concat(v1, v2, …)
Concatenates two or more vectors into a single vector. All input vectors must be the same type.
vector_concat(v1, v2, ...)
Returns: BLOB — a new vector containing all dimensions in order.
SELECT vector_extract(
vector_concat(
vector32('[1.0, 2.0]'),
vector32('[3.0, 4.0]')
)
);
-- [1.000000,2.000000,3.000000,4.000000]
vector_slice(vector_blob, start, end)
Extracts a contiguous portion of a vector.
vector_slice(vector_blob, start, end)
| Parameter | Type | Description |
|---|
vector_blob | BLOB | The source vector |
start | INTEGER | Start index (zero-based, inclusive) |
end | INTEGER | End index (exclusive) |
Returns: BLOB — a new vector with dimensions [start, end).
SELECT vector_extract(
vector_slice(vector32('[10.0, 20.0, 30.0, 40.0, 50.0]'), 1, 4)
);
-- [20.000000,30.000000,40.000000]
Examples
Creating a Table with Vector Embeddings
CREATE TABLE articles (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
content TEXT,
embedding BLOB
);
INSERT INTO articles VALUES (
1, 'Introduction to Machine Learning',
'Machine learning is a subset of artificial intelligence...',
vector32('[0.12, -0.34, 0.56, 0.78, -0.11, 0.45, -0.23, 0.67]')
);
INSERT INTO articles VALUES (
2, 'Database Design Patterns',
'Relational databases organize data into tables...',
vector32('[0.82, 0.15, -0.44, 0.23, 0.61, -0.33, 0.09, 0.77]')
);
Similarity Search
Find the most similar articles to a query vector by sorting on distance:
SELECT
id,
title,
vector_distance_cos(
embedding,
vector32('[0.1, -0.3, 0.5, 0.8, -0.1, 0.4, -0.2, 0.6]')
) AS distance
FROM articles
ORDER BY distance ASC
LIMIT 5;
Vector indexes are not yet supported. All similarity searches perform a linear scan over the table. For large datasets, use a WHERE clause to limit the candidate set.
Choosing a Distance Metric
-- Cosine: direction only, ignores magnitude (use for text embeddings)
SELECT vector_distance_cos(
vector32('[1.0, 0.0]'),
vector32('[100.0, 0.0]')
);
-- 0.0 (same direction, cosine treats them as identical)
-- L2: absolute position (use for geospatial or image features)
SELECT vector_distance_l2(
vector32('[1.0, 0.0]'),
vector32('[100.0, 0.0]')
);
-- 99.0 (far apart in absolute terms)
-- Read back a stored embedding
SELECT vector_extract(embedding) FROM articles WHERE id = 1;
-- Concatenate embeddings from two models
SELECT vector_extract(
vector_concat(
(SELECT embedding FROM articles WHERE id = 1),
(SELECT embedding FROM articles WHERE id = 2)
)
);
-- Use only the first 4 dimensions
SELECT vector_extract(
vector_slice(embedding, 0, 4)
) FROM articles WHERE id = 1;
-- [0.120000,-0.340000,0.560000,0.780000]
See Also