Skip to content

Commit dc94195

Browse files
committed
sql: verify correct query vector length when building a vector search
Previously the vector search operator was missing a check to ensure that the query vector had the right number of dimensions to search a given table. This patch adds a verification of query vector size when building a search node. Fixes: #146693 Release note (bug fix): Fixed an issue where searching a vector with a query vector that doesn't match the dimensions of the vector column in the table would cause a node to crash.
1 parent 8589b8a commit dc94195

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

pkg/sql/logictest/testdata/logic_test/vector_index

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ CREATE TABLE exec_test (
456456
FAMILY (a, b, vec1)
457457
)
458458

459+
query error pgcode 22000 different vector dimensions 1 and 3
460+
SELECT a FROM exec_test ORDER BY vec1 <-> '[1]' LIMIT 1;
461+
459462
statement ok
460463
INSERT INTO exec_test (a, b, vec1) VALUES
461464
(1, 1, '[1, 2, 3]'),
@@ -468,6 +471,12 @@ INSERT INTO exec_test (a, b, vec1) VALUES
468471
(8, NULL, NULL),
469472
(9, 3, NULL);
470473

474+
statement error pgcode 22000 pq: expected 3 dimensions, not 1
475+
INSERT INTO exec_test (a, b, vec1) VALUES (10, 1, '[1]');
476+
477+
query error pgcode 22000 different vector dimensions 1 and 3
478+
SELECT a FROM exec_test ORDER BY vec1 <-> '[1]' LIMIT 1;
479+
471480
# Get all rows that do not have NULL vector values.
472481
query IT rowsort
473482
SELECT a, vec1 FROM exec_test@idx1 ORDER BY vec1 <-> '[1, 1, 2]' LIMIT 10;

pkg/sql/opt/exec/execbuilder/relational.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3975,6 +3975,18 @@ func (b *Builder) buildVectorSearch(
39753975
}
39763976
targetNeighborCount := uint64(search.TargetNeighborCount)
39773977

3978+
// Verify that the query vector and vector column have the same dimensions.
3979+
resolvedQueryVector, ok := queryVector.(*tree.DPGVector)
3980+
if !ok {
3981+
return execPlan{}, colOrdMap{}, errors.AssertionFailedf("expected vector type, got %T", queryVector)
3982+
}
3983+
queryVectorLen := int32(len(resolvedQueryVector.T))
3984+
vectorColumnType := index.VectorColumn().DatumType()
3985+
if queryVectorLen != vectorColumnType.Width() {
3986+
return execPlan{}, colOrdMap{}, pgerror.Newf(pgcode.DataException,
3987+
"different vector dimensions %d and %d", queryVectorLen, vectorColumnType.Width())
3988+
}
3989+
39783990
var res execPlan
39793991
res.root, err = b.factory.ConstructVectorSearch(
39803992
table, index, outColOrds, search.PrefixConstraint, queryVector, targetNeighborCount,

0 commit comments

Comments
 (0)