|
| 1 | +// Copyright 2024 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package queries |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/dolthub/go-mysql-server/sql" |
| 19 | + "github.com/dolthub/go-mysql-server/sql/types" |
| 20 | +) |
| 21 | + |
| 22 | +var VectorIndexQueries = []ScriptTest{ |
| 23 | + { |
| 24 | + Name: "basic vector index", |
| 25 | + SetUpScript: []string{ |
| 26 | + "create table vectors (id int primary key, v json);", |
| 27 | + `insert into vectors values (1, '[4.0,3.0]'), (2, '[0.0,0.0]'), (3, '[-1.0,1.0]'), (4, '[0.0,-2.0]');`, |
| 28 | + `create vector index v_idx on vectors(v);`, |
| 29 | + }, |
| 30 | + Assertions: []ScriptTestAssertion{ |
| 31 | + { |
| 32 | + Query: "show create table vectors", |
| 33 | + Expected: []sql.Row{ |
| 34 | + {"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"}, |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + Query: "select * from vectors order by VEC_DISTANCE('[0.0,0.0]', v) limit 4", |
| 39 | + Expected: []sql.Row{ |
| 40 | + {2, types.MustJSON(`[0.0, 0.0]`)}, |
| 41 | + {3, types.MustJSON(`[-1.0, 1.0]`)}, |
| 42 | + {4, types.MustJSON(`[0.0, -2.0]`)}, |
| 43 | + {1, types.MustJSON(`[4.0, 3.0]`)}, |
| 44 | + }, |
| 45 | + ExpectedIndexes: []string{"v_idx"}, |
| 46 | + }, |
| 47 | + { |
| 48 | + // Only queries with a limit can use a vector index. |
| 49 | + Query: "select * from vectors order by VEC_DISTANCE('[0.0,0.0]', v)", |
| 50 | + Expected: []sql.Row{ |
| 51 | + {2, types.MustJSON(`[0.0, 0.0]`)}, |
| 52 | + {3, types.MustJSON(`[-1.0, 1.0]`)}, |
| 53 | + {4, types.MustJSON(`[0.0, -2.0]`)}, |
| 54 | + {1, types.MustJSON(`[4.0, 3.0]`)}, |
| 55 | + }, |
| 56 | + ExpectedIndexes: nil, |
| 57 | + }, |
| 58 | + { |
| 59 | + Query: "select * from vectors order by VEC_DISTANCE_L2_SQUARED('[0.0,-2.0]', v) limit 4", |
| 60 | + Expected: []sql.Row{ |
| 61 | + {4, types.MustJSON(`[0.0, -2.0]`)}, |
| 62 | + {2, types.MustJSON(`[0.0, 0.0]`)}, |
| 63 | + {3, types.MustJSON(`[-1.0, 1.0]`)}, |
| 64 | + {1, types.MustJSON(`[4.0, 3.0]`)}, |
| 65 | + }, |
| 66 | + ExpectedIndexes: []string{"v_idx"}, |
| 67 | + }, |
| 68 | + { |
| 69 | + // Ensure vector index is not used for range lookups. |
| 70 | + Query: "select * from vectors order by v limit 4", |
| 71 | + Expected: []sql.Row{ |
| 72 | + {3, types.MustJSON(`[-1.0, 1.0]`)}, |
| 73 | + {4, types.MustJSON(`[0.0, -2.0]`)}, |
| 74 | + {2, types.MustJSON(`[0.0, 0.0]`)}, |
| 75 | + {1, types.MustJSON(`[4.0, 3.0]`)}, |
| 76 | + }, |
| 77 | + ExpectedIndexes: []string{}, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | +} |
0 commit comments