Skip to content

Commit 5b37fa1

Browse files
committed
Add test cases for vector indexes on vectors.
1 parent bf05239 commit 5b37fa1

File tree

1 file changed

+106
-6
lines changed

1 file changed

+106
-6
lines changed

enginetest/queries/vector_index_queries.go

Lines changed: 106 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121

2222
var VectorIndexQueries = []ScriptTest{
2323
{
24-
Name: "basic vector index",
24+
Name: "basic JSON vector index",
2525
SetUpScript: []string{
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]');`,
@@ -117,19 +117,119 @@ var VectorIndexQueries = []ScriptTest{
117117
},
118118
},
119119
{
120-
Name: "vector index errors",
120+
Name: "basic VECTOR vector index",
121121
SetUpScript: []string{
122-
"create table vectors (id int primary key, v json);",
123-
`insert into vectors values (1, '[4.0,3.0]'), (2, '[0.0,0.0]'), (3, '[-1.0,1.0]'), (4, '[0.0,-2.0]');`,
122+
"create table vectors (id int primary key, v vector(2));",
123+
`insert into vectors values (1, STRING_TO_VECTOR('[4.0,3.0]')), (2, STRING_TO_VECTOR('[0.0,0.0]')), (3, STRING_TO_VECTOR('[-1.0,1.0]')), (4, STRING_TO_VECTOR('[0.0,-2.0]'));`,
124124
`create vector index v_idx on vectors(v);`,
125125
`set @query_vec = '[0.0,0.0]';`,
126126
},
127127
Assertions: []ScriptTestAssertion{
128128
{
129-
Query: `create vector index v_idx2 on vectors(id);`,
129+
Query: "show create table vectors",
130130
Expected: []sql.Row{
131-
{"vectors", "CREATE TABLE `vectors` (\n `id` int NOT NULL,\n `v` json,\n PRIMARY KEY (`id`),\n VECTOR KEY `v_idx` (`v`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"},
131+
{"vectors", "CREATE TABLE `vectors` (\n `id` int NOT NULL,\n `v` VECTOR(2),\n PRIMARY KEY (`id`),\n VECTOR KEY `v_idx` (`v`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"},
132+
},
133+
},
134+
{
135+
Query: "select * from vectors order by VEC_DISTANCE('[0.0,0.0]', v) limit 4",
136+
Expected: []sql.Row{
137+
{2, floatsToBytes(0.0, 0.0)},
138+
{3, floatsToBytes(-1.0, 1.0)},
139+
{4, floatsToBytes(0.0, -2.0)},
140+
{1, floatsToBytes(4.0, 3.0)},
141+
},
142+
ExpectedIndexes: []string{"v_idx"},
143+
},
144+
{
145+
// Queries against a user var can be optimized.
146+
Query: "select * from vectors order by VEC_DISTANCE(@query_vec, v) limit 4",
147+
Expected: []sql.Row{
148+
{2, floatsToBytes(0.0, 0.0)},
149+
{3, floatsToBytes(-1.0, 1.0)},
150+
{4, floatsToBytes(0.0, -2.0)},
151+
{1, floatsToBytes(4.0, 3.0)},
152+
},
153+
ExpectedIndexes: []string{"v_idx"},
154+
},
155+
{
156+
// Use the index even when there's a projection involved.
157+
Query: "select `id`+1 from vectors order by VEC_DISTANCE('[0.0,0.0]', v) limit 4",
158+
Expected: []sql.Row{
159+
{3},
160+
{4},
161+
{5},
162+
{2},
163+
},
164+
ExpectedIndexes: []string{"v_idx"},
165+
},
166+
{
167+
// Only queries with a limit can use a vector index.
168+
Query: "select * from vectors order by VEC_DISTANCE('[0.0,0.0]', v)",
169+
Expected: []sql.Row{
170+
{2, floatsToBytes(0.0, 0.0)},
171+
{3, floatsToBytes(-1.0, 1.0)},
172+
{4, floatsToBytes(0.0, -2.0)},
173+
{1, floatsToBytes(4.0, 3.0)},
132174
},
175+
ExpectedIndexes: nil,
176+
},
177+
{
178+
Query: "select * from vectors order by VEC_DISTANCE_L2_SQUARED('[0.0,-2.0]', v) limit 4",
179+
Expected: []sql.Row{
180+
{4, floatsToBytes(0.0, -2.0)},
181+
{2, floatsToBytes(0.0, 0.0)},
182+
{3, floatsToBytes(-1.0, 1.0)},
183+
{1, floatsToBytes(4.0, 3.0)},
184+
},
185+
ExpectedIndexes: []string{"v_idx"},
186+
},
187+
{
188+
// Ensure vector index is not used for range lookups.
189+
Query: "select * from vectors order by v limit 4",
190+
Expected: []sql.Row{
191+
{2, floatsToBytes(0.0, 0.0)},
192+
{4, floatsToBytes(0.0, -2.0)},
193+
{1, floatsToBytes(4.0, 3.0)},
194+
{3, floatsToBytes(-1.0, 1.0)},
195+
},
196+
ExpectedIndexes: []string{},
197+
},
198+
{
199+
// Modify the index after creation.
200+
Query: "insert into vectors values (5, STRING_TO_VECTOR('[1.0,0.0]'))",
201+
},
202+
{
203+
Query: "select * from vectors order by VEC_DISTANCE('[0.0,0.0]', v)",
204+
Expected: []sql.Row{
205+
{2, floatsToBytes(0.0, 0.0)},
206+
{5, floatsToBytes(1.0, 0.0)},
207+
{3, floatsToBytes(-1.0, 1.0)},
208+
{4, floatsToBytes(0.0, -2.0)},
209+
{1, floatsToBytes(4.0, 3.0)},
210+
},
211+
ExpectedIndexes: []string{},
212+
},
213+
},
214+
},
215+
{
216+
Name: "vector index errors",
217+
SetUpScript: []string{
218+
"create table vectors (id int primary key, j json, v vector(2));",
219+
`insert into vectors values
220+
(1, '[4.0,3.0]', STRING_TO_VECTOR('[4.0,3.0]')),
221+
(2, '[0.0,0.0]', STRING_TO_VECTOR('[0.0,0.0]')),
222+
(3, '[-1.0,1.0]', STRING_TO_VECTOR('[-1.0,1.0]')),
223+
(4, '[0.0,-2.0]', STRING_TO_VECTOR('[0.0,-2.0]'));`,
224+
},
225+
Assertions: []ScriptTestAssertion{
226+
{
227+
Query: `create vector index v_idx2 on vectors(j, v);`,
228+
ExpectedErrStr: "a vector index must have exactly one column",
229+
},
230+
{
231+
Query: `create vector index v_idx2 on vectors(id);`,
232+
ExpectedErrStr: "a vector index colum must be a vector or JSON",
133233
},
134234
},
135235
},

0 commit comments

Comments
 (0)