Skip to content

Commit 1354b88

Browse files
committed
Add comment and test for user var vector index query.
1 parent bbd3a63 commit 1354b88

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

enginetest/queries/vector_index_queries.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var VectorIndexQueries = []ScriptTest{
2626
"create table vectors (id int primary key, v json);",
2727
`insert into vectors values (1, '[4.0,3.0]'), (2, '[0.0,0.0]'), (3, '[-1.0,1.0]'), (4, '[0.0,-2.0]');`,
2828
`create vector index v_idx on vectors(v);`,
29+
`set @query_vec = '[0.0,0.0]';`,
2930
},
3031
Assertions: []ScriptTestAssertion{
3132
{
@@ -44,6 +45,17 @@ var VectorIndexQueries = []ScriptTest{
4445
},
4546
ExpectedIndexes: []string{"v_idx"},
4647
},
48+
{
49+
// Queries against a user var can be optimized.
50+
Query: "select * from vectors order by VEC_DISTANCE(@query_vec, v) limit 4",
51+
Expected: []sql.Row{
52+
{2, types.MustJSON(`[0.0, 0.0]`)},
53+
{3, types.MustJSON(`[-1.0, 1.0]`)},
54+
{4, types.MustJSON(`[0.0, -2.0]`)},
55+
{1, types.MustJSON(`[4.0, 3.0]`)},
56+
},
57+
ExpectedIndexes: []string{"v_idx"},
58+
},
4759
{
4860
// Only queries with a limit can use a vector index.
4961
Query: "select * from vectors order by VEC_DISTANCE('[0.0,0.0]', v)",

sql/analyzer/replace_order_by_distance.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ func replaceIdxOrderByDistanceHelper(ctx *sql.Context, scope *plan.Scope, node s
6060
return n, transform.SameTree, nil
6161
}
6262

63+
// We currently require that the query vector to the distance function is a constant value that does not
64+
// depend on the row. Right now that can be a Literal or a UserVar.
6365
isLiteral := func(expr sql.Expression) bool {
6466
switch expr.(type) {
6567
case *expression.Literal, *expression.UserVar:

0 commit comments

Comments
 (0)