The VECTOR_SEARCH function performs vector similarity search and supports large-scale approximate nearest neighbor search.
Limitations
The input table must be a Delta Table.
Before you run the VECTOR_SEARCH function, create a vector index on the vector column to accelerate similarity queries on large-scale vector data. For more information, see VECTOR INDEX. If no vector index is available at query time, the system automatically falls back to a brute-force search, which can result in slow queries.
Syntax
VECTOR_SEARCH(
{ TABLE base_table | (base_table_query) }, -- The table containing embedding vectors to search.
column_to_search, -- The name of the column in the base table that contains the embedding vectors.
{ TABLE query_table | (query_table_query) }, -- The table that provides the query embedding vectors.
query_column_to_search, -- The name of the column in the query table that contains the query embedding vectors.
top_k -- The number of nearest neighbors to return.
[, distance_type] -- The distance metric to use.
[, options] -- A JSON string for other configurable parameters.
)Parameters
Parameter | Description | Notes |
base_table | (base_table_query) | Required. The table containing the embedding vectors to search. |
|
query_table | (query_table_query) | Required. The table that provides the query embedding vectors. | |
column_to_search | Required. The column in the base table that contains the embedding vectors. | |
query_column_to_search | Required. The column in the query table that contains the query embedding vectors. | |
top_k | Required. The number of nearest neighbors to return. Must be a positive integer (>= 1). | |
distance_type | Optional. The metric for calculating the distance between two vectors. All supported metrics return a distance value, where a smaller value indicates greater similarity. ● ● ● Do not enclose the | |
options | Optional. A JSON-formatted string for setting other configurable parameters. The following parameter is supported:
| |
Return value
For each row in the query table, VECTOR_SEARCH returns the nearest neighbor rows from the base table. The top_k parameter specifies the number of result rows to return for each query row. The result consists of the following three parts:
Query table output columns: All columns from the query table or the columns selected in a subquery.
Base table output columns: All columns from the base table or the columns selected in a subquery.
distance: ADOUBLEthat represents the distance between the corresponding base and query table rows.
If the base and query tables share column names, use a qualified column name to access the results. For details, see the example Use qualified column names to access results.
Examples
Prepare data
-- Enable the VECTOR data type.
SET odps.sql.type.system.odps2=true;
SET odps.sql.type.vector.enable=true;
-- Base Table: A table of animal vectors.
CREATE TABLE IF NOT EXISTS animals (
name STRING,
category STRING,
vec VECTOR(FLOAT, 2)
)
TBLPROPERTIES (
"table.format.version"="2"
);
INSERT INTO animals VALUES
('dog', 'mammal', CAST(array(1.0, 2.0) AS VECTOR(FLOAT, 2))),
('wolf', 'mammal', CAST(array(2.0, 4.0) AS VECTOR(FLOAT, 2))),
('cat', 'mammal', CAST(array(1.0, 1.8) AS VECTOR(FLOAT, 2))),
('lion', 'mammal', CAST(array(2.0, -2.5) AS VECTOR(FLOAT, 2))),
('tiger', 'mammal', CAST(array(3.0, -2.0) AS VECTOR(FLOAT, 2))),
('otter', 'mammal', CAST(array(-3.0, -1.0) AS VECTOR(FLOAT, 2))),
('whale', 'mammal', CAST(array(-5.0, -1.0) AS VECTOR(FLOAT, 2))),
('snake', 'reptile', CAST(array(-2.0, 3.0) AS VECTOR(FLOAT, 2))),
('lizard','reptile', CAST(array(-1.8, 2.8) AS VECTOR(FLOAT, 2))),
('eagle', 'bird', CAST(array(5.0, -3.0) AS VECTOR(FLOAT, 2)));
-- Query Table: A table of animal vectors to query.
CREATE TABLE IF NOT EXISTS query_animals (
query_name STRING,
tag STRING,
qvec VECTOR(FLOAT, 2)
)
TBLPROPERTIES (
"table.format.version"="2"
);
INSERT INTO query_animals VALUES
('dog-like', 'mammal', CAST(array(1.0, 2.0) AS VECTOR(FLOAT, 2))),
('cat-like', 'mammal', CAST(array(1.0, -1.0) AS VECTOR(FLOAT, 2))),
('snake-like','reptile', CAST(array(-2.0, 3.0) AS VECTOR(FLOAT, 2)));For simplicity, the examples in this topic use a brute-force search. In a production environment, you should create and build a vector index on the vector column before performing a vector search to ensure optimal performance. For more information, see VECTOR INDEX. If no vector index is available, the system automatically falls back to a brute-force search, which can be slow.
Table-to-table search
Search the vec column of the animals table to find the two most similar vectors for each row in the qvec column of the query_animals table.
SELECT _base.name, _query.query_name, distance
FROM VECTOR_SEARCH(
TABLE animals, vec,
TABLE query_animals, qvec,
2, EUCLIDEAN
);
-- Result:
+--------+------------+---------------------+
| name | query_name | distance |
+--------+------------+---------------------+
| dog | dog-like | 0.0 |
| cat | dog-like | 0.04000001773238182 |
| lion | cat-like | 3.25 |
| tiger | cat-like | 5.0 |
| snake | snake-like | 0.0 |
| lizard | snake-like | 0.08000003546476364 |
+--------+------------+---------------------+Simple filter on the base table
SELECT _base.name, _query.query_name, distance
FROM VECTOR_SEARCH(
(SELECT name, vec FROM animals WHERE category = 'mammal'), vec,
TABLE query_animals, qvec,
2, EUCLIDEAN
);
-- Result:
+------+------------+------------+
| name | query_name | distance |
+------+------------+------------+
| dog | dog-like | 5.0 |
| wolf | dog-like | 5.0 |
| dog | cat-like | 2.0 |
| wolf | cat-like | 2.0 |
| dog | snake-like | 13.0 |
| wolf | snake-like | 13.0 |
+------+------------+------------+Simple subqueries for base and query tables
SELECT *
FROM VECTOR_SEARCH(
(SELECT name, vec FROM animals WHERE category IN ('mammal', 'reptile')), vec,
(SELECT query_name, qvec FROM query_animals WHERE tag = 'mammal'), qvec,
2, EUCLIDEAN
);
-- Result:
+------------+---------+------+--------+------------+
| query_name | qvec | name | vec | distance |
+------------+---------+------+--------+------------+
| dog-like | [1, 2] | dog | [1, 2] | 5.0 |
| cat-like | [1, -1] | dog | [1, 2] | 2.0 |
| dog-like | [1, 2] | wolf | [2, 4] | 5.0 |
| cat-like | [1, -1] | wolf | [2, 4] | 2.0 |
+------------+---------+------+--------+------------+Search with a single vector literal
SELECT _base.name, distance
FROM VECTOR_SEARCH(
TABLE animals, vec,
vector(1.0f, 2.0f) AS qt(qvec),
qvec,
3, COSINE
);
-- Result:
+------+-----------------------+
| name | distance |
+------+-----------------------+
| dog | 5.960464477539063e-8 |
| wolf | 5.960464477539063e-8 |
| cat | 0.0009437799453735352 |
+------+-----------------------+Use qualified column names to access results
If you do not explicitly specify an alias for the base and query tables, the system automatically generates default qualifiers: _base for the base table and _query for the query table. Use qualified column names, such as _base.id and _query.id, to access the results. Referencing a column name that exists in both tables without a qualifier is ambiguous and results in an error.
-- Not recommended: If both tables have a 'name' column, this query causes an ambiguity error.
SELECT name
FROM VECTOR_SEARCH(TABLE animals, vec, TABLE animals, vec, 10, COSINE);
-- Error:
FAILED: ODPS-0130071:[1,8] Semantic analysis exception - name is ambiguous, can be both _query.name or _base.name
-- Recommended: Use the default qualifiers.
SELECT _base.name, _query.query_name, distance
FROM VECTOR_SEARCH(
TABLE animals, vec,
TABLE query_animals, qvec,
2, COSINE
);
-- Result:
+--------+------------+------------------------+
| name | query_name | distance |
+--------+------------+------------------------+
| dog | dog-like | 5.960464477539063e-8 |
| wolf | dog-like | 5.960464477539063e-8 |
| lion | cat-like | 0.006116271018981934 |
| tiger | cat-like | 0.019419312477111816 |
| snake | snake-like | -1.1920928955078125e-7 |
| lizard | snake-like | 0.00013881921768188477 |
+--------+------------+------------------------+
-- You can also explicitly specify an alias.
SELECT b.name, q.query_name, distance
FROM VECTOR_SEARCH(
TABLE animals AS b, vec,
TABLE query_animals AS q, qvec,
2, COSINE
);
-- Result:
+--------+------------+------------------------+
| name | query_name | distance |
+--------+------------+------------------------+
| dog | dog-like | 5.960464477539063e-8 |
| wolf | dog-like | 5.960464477539063e-8 |
| lion | cat-like | 0.006116271018981934 |
| tiger | cat-like | 0.019419312477111816 |
| snake | snake-like | -1.1920928955078125e-7 |
| lizard | snake-like | 0.00013881921768188477 |
+--------+------------+------------------------+Unsupported scenarios
Base table does not support Time Travel syntax
SELECT *
FROM VECTOR_SEARCH(
(SELECT name, vec FROM animals VERSION AS OF 1), vec,
TABLE query_animals, qvec,
10, COSINE
);
-- Error:
FAILED: ODPS-0130071:[3,4] Semantic analysis exception - VECTOR_SEARCH base table does not support time travelComplex input plans not supported for base and query tables
The base and query table inputs cannot contain complex operators such as JOIN or AGGREGATE.
SELECT *
FROM VECTOR_SEARCH(
(SELECT a.name, a.vec FROM animals a JOIN query_animals b ON a.name = b.query_name), vec,
TABLE query_animals, qvec,
10, COSINE
);
-- Error:
FAILED: ODPS-0130071:[] Semantic analysis exception - VECTOR_SEARCH input must be a simple plan containing only SELECT, FILTER, and TABLE SCAN operatorsInput tables must be append-only Delta Tables
-- Create a query table that is a regular table, not a Delta Table.
CREATE Table query_table_animal AS
SELECT query_name, qvec FROM query_animals;
SELECT *
FROM VECTOR_SEARCH(
TABLE animals, vec,
TABLE query_table_animal, qvec,
10, COSINE
);
-- Error:
FAILED: ODPS-0130071:[] Semantic analysis exception - VECTOR_SEARCH input table must be an append delta tableMulti-row VALUES not supported for query table
The query side supports VALUES with 0 or 1 row, but does not support VALUES with multiple rows.
SELECT *
FROM VECTOR_SEARCH(
TABLE animals, vec,
(
SELECT query_name, qvec
FROM VALUES
('q1', vector(1.0f, 2.0f)),
('q2', vector(4.0f, 5.0f)) AS q(query_name, qvec)
),
qvec,
10, COSINE
);
-- Error:
FAILED: ODPS-0130071:[] Semantic analysis exception - VECTOR_SEARCH input table must be an append delta table