Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 69 additions & 44 deletions enginetest/queries/index_query_plans.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions enginetest/queries/vector_index_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var VectorIndexQueries = []ScriptTest{
{
Name: "basic JSON vector index",
SetUpScript: []string{
"create table vectors (id int primary key, v json);",
"create table vectors (id int primary key, v json not null);",
`insert into vectors values (1, '[4.0,3.0]'), (2, '[0.0,0.0]'), (3, '[-1.0,1.0]'), (4, '[0.0,-2.0]');`,
`create vector index v_idx on vectors(v);`,
`set @query_vec = '[0.0,0.0]';`,
Expand All @@ -32,7 +32,7 @@ var VectorIndexQueries = []ScriptTest{
{
Query: "show create table vectors",
Expected: []sql.Row{
{"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"},
{"vectors", "CREATE TABLE `vectors` (\n `id` int NOT NULL,\n `v` json NOT NULL,\n PRIMARY KEY (`id`),\n VECTOR KEY `v_idx` (`v`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"},
},
},
{
Expand Down Expand Up @@ -119,7 +119,7 @@ var VectorIndexQueries = []ScriptTest{
{
Name: "basic VECTOR vector index",
SetUpScript: []string{
"create table vectors (id int primary key, v vector(2));",
"create table vectors (id int primary key, v vector(2) not null);",
`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]'));`,
`create vector index v_idx on vectors(v);`,
`set @query_vec = '[0.0,0.0]';`,
Expand All @@ -128,7 +128,7 @@ var VectorIndexQueries = []ScriptTest{
{
Query: "show create table vectors",
Expected: []sql.Row{
{"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"},
{"vectors", "CREATE TABLE `vectors` (\n `id` int NOT NULL,\n `v` VECTOR(2) NOT NULL,\n PRIMARY KEY (`id`),\n VECTOR KEY `v_idx` (`v`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin"},
},
},
{
Expand Down Expand Up @@ -233,4 +233,20 @@ var VectorIndexQueries = []ScriptTest{
},
},
},
{
Name: "vector index on nullable column errors",
SetUpScript: []string{
"create table vectors_nullable (id int primary key, v vector(2) null);",
},
Assertions: []ScriptTestAssertion{
{
Query: `create vector index v_idx on vectors_nullable(v);`,
ExpectedErr: sql.ErrNullableVectorIdx,
},
{
Query: `create table bad_vector_idx (id int primary key, v vector(2), vector index (v));`,
ExpectedErr: sql.ErrNullableVectorIdx,
},
},
},
}
10 changes: 7 additions & 3 deletions enginetest/scriptgen/setup/scripts/comp_index_tables
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ create table pref_index_t4 (i int primary key, v1 varchar(10), v2 varchar(10), u
----

exec
CREATE TABLE comp_vector_index_t0 (pk BIGINT PRIMARY KEY, v1 BIGINT, v2 JSON);
CREATE TABLE comp_vector_index_t0 (pk BIGINT PRIMARY KEY, v1 BIGINT, json_column JSON NOT NULL, vector_column VECTOR(2) NOT NULL GENERATED ALWAYS AS (STRING_TO_VECTOR(json_column)) STORED);
----

exec
INSERT INTO comp_vector_index_t0 VALUES (0,0,"[3,16]"),(1,2,"[65,9]"),(2,3,"[38,37]"),(3,3,"[99,99]"),(4,5,"[17,42]"),(5,6,"[6,76]"),(6,6,"[81,33]"),
INSERT INTO comp_vector_index_t0 (pk, v1, json_column) VALUES (0,0,"[3,16]"),(1,2,"[65,9]"),(2,3,"[38,37]"),(3,3,"[99,99]"),(4,5,"[17,42]"),(5,6,"[6,76]"),(6,6,"[81,33]"),
(7,7,"[33,51]"),(8,7,"[37,42]"),(9,8,"[9,21]"),(10,8,"[37,90]"),(11,9,"[39,20]"),(12,9,"[71,82]"),(13,10,"[16,21]"),(14,10,"[32,46]"),(15,10,"[47,36]"),
(16,12,"[44,84]"),(17,12,"[66,40]"),(18,13,"[47,30]"),(19,13,"[56,41]"),(20,14,"[38,24]"),(21,14,"[91,1]"),(22,15,"[2,69]"),(23,16,"[40,36]"),
(24,20,"[29,93]"),(25,21,"[9,89]"),(26,21,"[42,76]"),(27,23,"[13,53]"),(28,23,"[28,68]"),(29,23,"[28,90]"),(30,23,"[30,44]"),(31,24,"[20,8]"),
Expand All @@ -118,7 +118,11 @@ INSERT INTO comp_vector_index_t0 VALUES (0,0,"[3,16]"),(1,2,"[65,9]"),(2,3,"[38,
----

exec
create VECTOR INDEX v_idx on comp_vector_index_t0 (v2)
create VECTOR INDEX v_idx_json on comp_vector_index_t0 (json_column)
----

exec
create VECTOR INDEX v_idx_vec on comp_vector_index_t0 (vector_column)
----

exec
Expand Down
7 changes: 4 additions & 3 deletions enginetest/scriptgen/setup/setup_data.sg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions sql/analyzer/validate_create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package analyzer

import (
"fmt"
"strings"

"github.com/dolthub/go-mysql-server/sql"
Expand Down Expand Up @@ -888,6 +889,16 @@ func validateIndex(ctx *sql.Context, colMap map[string]*sql.Column, idxDef *sql.
}
}

if idxDef.IsVector() {
if len(idxDef.Columns) != 1 {
return fmt.Errorf("a vector index must have exactly one column")
}
schCol, _ := colMap[strings.ToLower(idxDef.Columns[0].Name)]
if schCol.Nullable {
return sql.ErrNullableVectorIdx.New()
}
}

if idxDef.IsSpatial() {
if len(idxDef.Columns) != 1 {
return sql.ErrTooManyKeyParts.New(1)
Expand Down
3 changes: 3 additions & 0 deletions sql/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ var (
// ErrNullableSpatialIdx is thrown when creating a SPATIAL index with a nullable column
ErrNullableSpatialIdx = errors.NewKind("All parts of a SPATIAL index must be NOT NULL")

// ErrNullableVectorIdx is thrown when creating a VECTOR index with a nullable column
ErrNullableVectorIdx = errors.NewKind("All parts of a VECTOR index must be NOT NULL")

// ErrBadSpatialIdxCol is thrown when attempting to define a SPATIAL index over a non-geometry column
ErrBadSpatialIdxCol = errors.NewKind("a SPATIAL index may only contain a geometrical type column")

Expand Down
3 changes: 3 additions & 0 deletions sql/rowexec/ddl_iters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2163,6 +2163,9 @@ func (b *BaseBuilder) executeAlterIndex(ctx *sql.Context, n *plan.AlterIndex) er
if !types.IsVectorConvertable(tblCol.Type) {
return sql.ErrVectorInvalidColumnType.New()
}
if tblCol.Nullable {
return sql.ErrNullableVectorIdx.New()
}
break
}
}
Expand Down