|
| 1 | +// Copyright 2022 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/plan" |
| 20 | + "github.com/dolthub/go-mysql-server/sql/types" |
| 21 | +) |
| 22 | + |
| 23 | +// VectorDDLQueries tests VECTOR type creation, insertion, and querying |
| 24 | +var VectorDDLQueries = []ScriptTest{ |
| 25 | + { |
| 26 | + Name: "basic VECTOR type creation and manipulation", |
| 27 | + SetUpScript: []string{ |
| 28 | + `CREATE TABLE test_vectors ( |
| 29 | + id INT PRIMARY KEY, |
| 30 | + small_vec VECTOR(2), |
| 31 | + medium_vec VECTOR(10), |
| 32 | + large_vec VECTOR(1000) |
| 33 | + )`, |
| 34 | + `INSERT INTO test_vectors VALUES |
| 35 | + (1, STRING_TO_VECTOR('[1.0, 2.0]'), STRING_TO_VECTOR('[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]'), NULL), |
| 36 | + (2, STRING_TO_VECTOR('[3.5, 4.5]'), NULL, NULL |
| 37 | + )`, |
| 38 | + }, |
| 39 | + Assertions: []ScriptTestAssertion{ |
| 40 | + { |
| 41 | + Query: `SHOW CREATE TABLE vectors`, |
| 42 | + Expected: []sql.Row{{"vectors", "CREATE TABLE `vectors` (\n `id` int NOT NULL,\n `small_vec` VECTOR(2),\n `medium_vec` VECTOR(10),\n `large_vec` VECTOR(1000), \n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"}}, |
| 43 | + }, |
| 44 | + { |
| 45 | + Query: `SELECT id, small_vec, medium_vec FROM vectors WHERE id = 2`, |
| 46 | + Expected: []sql.Row{{2, []float32{3.5, 4.5}, nil}}, |
| 47 | + }, |
| 48 | + { |
| 49 | + Query: `SELECT id, small_vec, medium_vec FROM vectors WHERE id = 1`, |
| 50 | + Expected: []sql.Row{{1, []float32{1.0, 2.0}, []float32{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0}}}, |
| 51 | + }, |
| 52 | + { |
| 53 | + Query: `SELECT id, small_vec FROM test_vectors ORDER BY id`, |
| 54 | + Expected: []sql.Row{ |
| 55 | + {1, []float32{1.0, 2.0}}, |
| 56 | + {2, []float32{3.5, 4.5}}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + { |
| 60 | + Query: `UPDATE test_vectors SET small_vec = '[10.0, 20.0]' WHERE id = 1`, |
| 61 | + Expected: []sql.Row{{types.OkResult{RowsAffected: 1, Info: plan.UpdateInfo{Matched: 1, Updated: 1}}}}, |
| 62 | + }, |
| 63 | + { |
| 64 | + Query: `SELECT small_vec FROM test_vectors WHERE id = 1`, |
| 65 | + Expected: []sql.Row{{[]float32{10.0, 20.0}}}, |
| 66 | + }, |
| 67 | + { |
| 68 | + Query: `INSERT INTO test_vectors VALUES |
| 69 | + (3, 0x00F0803F00004040, NULL, NULL)`, |
| 70 | + Expected: []sql.Row{{types.NewOkResult(1)}}, |
| 71 | + }, |
| 72 | + { |
| 73 | + Query: `SELECT small_vec FROM test_vectors WHERE id = 3`, |
| 74 | + Expected: []sql.Row{{[]float32{1.0, 2.0}}}, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + { |
| 79 | + Name: "VECTOR type error conditions", |
| 80 | + SetUpScript: []string{ |
| 81 | + `CREATE TABLE error_vectors (id INT PRIMARY KEY, vec3 VECTOR(3))`, |
| 82 | + }, |
| 83 | + Assertions: []ScriptTestAssertion{ |
| 84 | + { |
| 85 | + Query: `INSERT INTO error_vectors VALUES (1, '[1.0, 2.0]')`, |
| 86 | + ExpectedErrStr: "", |
| 87 | + }, |
| 88 | + { |
| 89 | + Query: `INSERT INTO error_vectors VALUES (1, STRING_TO_VECTOR('[1.0, 2.0]'))`, |
| 90 | + ExpectedErrStr: "VECTOR dimension mismatch: expected 3, got 2", |
| 91 | + }, |
| 92 | + { |
| 93 | + Query: `INSERT INTO error_vectors VALUES (2, STRING_TO_VECTOR('[1.0, 2.0, 3.0, 4.0]'))`, |
| 94 | + ExpectedErrStr: "VECTOR dimension mismatch: expected 3, got 4", |
| 95 | + }, |
| 96 | + { |
| 97 | + Query: `INSERT INTO error_vectors VALUES (3, STRING_TO_VECTOR('[1.0, invalid, 3.0]'))`, |
| 98 | + ExpectedErrStr: "invalid VECTOR JSON format: invalid character 'i' looking for beginning of value", |
| 99 | + }, |
| 100 | + { |
| 101 | + Query: `INSERT INTO error_vectors VALUES (4, STRING_TO_VECTOR('invalid_json'))`, |
| 102 | + ExpectedErrStr: "VECTOR must be in JSON array format", |
| 103 | + }, |
| 104 | + { |
| 105 | + Query: `INSERT INTO error_vectors VALUES (5, STRING_TO_VECTOR('[1.0, "not an array"]'))`, |
| 106 | + ExpectedErrStr: "VECTOR must be in JSON array format", |
| 107 | + }, |
| 108 | + { |
| 109 | + Query: `INSERT INTO error_vectors VALUES (5, STRING_TO_VECTOR('"not an array"'))`, |
| 110 | + ExpectedErrStr: "VECTOR must be in JSON array format", |
| 111 | + }, |
| 112 | + { |
| 113 | + Query: `CREATE TABLE error_vectors (id INT PRIMARY KEY, vec3 VECTOR(-3))`, |
| 114 | + ExpectedErr: sql.ErrInvalidColTypeDefinition, |
| 115 | + }, |
| 116 | + { |
| 117 | + Query: `CREATE TABLE error_vectors (id INT PRIMARY KEY, vec3 VECTOR(0))`, |
| 118 | + ExpectedErr: sql.ErrInvalidColTypeDefinition, |
| 119 | + }, |
| 120 | + { |
| 121 | + Query: `CREATE TABLE error_vectors (id INT PRIMARY KEY, vec3 VECTOR(17000))`, |
| 122 | + ExpectedErr: sql.ErrInvalidColTypeDefinition, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + { |
| 127 | + Name: "VECTOR type with different data formats", |
| 128 | + SetUpScript: []string{ |
| 129 | + `CREATE TABLE format_vectors (id INT PRIMARY KEY, vec2 VECTOR(2))`, |
| 130 | + }, |
| 131 | + Assertions: []ScriptTestAssertion{ |
| 132 | + { |
| 133 | + Query: `INSERT INTO format_vectors VALUES |
| 134 | + (1, STRING_TO_VECTOR('[1.0, 2.0]')), |
| 135 | + (2, STRING_TO_VECTOR('[3, 4]')), |
| 136 | + (3, STRING_TO_VECTOR('[55e-1, 67e2]'))`, |
| 137 | + Expected: []sql.Row{{types.NewOkResult(3)}}, |
| 138 | + }, |
| 139 | + { |
| 140 | + Query: `SELECT id, vec2 FROM format_vectors ORDER BY id`, |
| 141 | + Expected: []sql.Row{ |
| 142 | + {1, []float32{1.0, 2.0}}, |
| 143 | + {2, []float32{3.0, 4.0}}, |
| 144 | + {3, []float32{5.5, 6700}}, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | +} |
0 commit comments