Skip to content

Commit

Permalink
parser: make HNSW as vector index default type (#56429)
Browse files Browse the repository at this point in the history
close #56428
  • Loading branch information
zimulala authored Oct 8, 2024
1 parent 7252cd4 commit f7f573b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
15 changes: 12 additions & 3 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15478,7 +15478,11 @@ yynewstate:
}

keyType := yyS[yypt-11].item.(ast.IndexKeyType)
if (keyType == ast.IndexKeyTypeVector && indexOption.Tp != model.IndexTypeHNSW) || (keyType != ast.IndexKeyTypeVector && indexOption.Tp == model.IndexTypeHNSW) {
isVectorIndex := keyType == ast.IndexKeyTypeVector
if isVectorIndex && indexOption.Tp == model.IndexTypeInvalid {
indexOption.Tp = model.IndexTypeHNSW
}
if (isVectorIndex && indexOption.Tp != model.IndexTypeHNSW) || (!isVectorIndex && indexOption.Tp == model.IndexTypeHNSW) {
yylex.AppendError(ErrSyntax)
return 1
}
Expand Down Expand Up @@ -21952,17 +21956,22 @@ yynewstate:
Name: yyS[yypt-4].item.([]interface{})[0].(*ast.NullString).String,
IsEmptyIndex: yyS[yypt-4].item.([]interface{})[0].(*ast.NullString).Empty,
}

if yyS[yypt-0].item != nil {
c.Option = yyS[yypt-0].item.(*ast.IndexOption)
}

if indexType := yyS[yypt-4].item.([]interface{})[1]; indexType != nil {
if c.Option == nil {
c.Option = &ast.IndexOption{}
}
c.Option.Tp = indexType.(model.IndexType)
}
if c.Option == nil || c.Option.Tp != model.IndexTypeHNSW {
if c.Option == nil {
c.Option = &ast.IndexOption{Tp: model.IndexTypeHNSW}
} else if c.Option.Tp == model.IndexTypeInvalid {
c.Option.Tp = model.IndexTypeHNSW
}
if c.Option.Tp != model.IndexTypeHNSW {
yylex.AppendError(ErrSyntax)
return 1
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -4236,7 +4236,11 @@ CreateIndexStmt:
}

keyType := $2.(ast.IndexKeyType)
if (keyType == ast.IndexKeyTypeVector && indexOption.Tp != model.IndexTypeHNSW) || (keyType != ast.IndexKeyTypeVector && indexOption.Tp == model.IndexTypeHNSW) {
isVectorIndex := keyType == ast.IndexKeyTypeVector
if isVectorIndex && indexOption.Tp == model.IndexTypeInvalid {
indexOption.Tp = model.IndexTypeHNSW
}
if (isVectorIndex && indexOption.Tp != model.IndexTypeHNSW) || (!isVectorIndex && indexOption.Tp == model.IndexTypeHNSW) {
yylex.AppendError(ErrSyntax)
return 1
}
Expand Down Expand Up @@ -12376,17 +12380,22 @@ ConstraintVectorIndex:
Name: $4.([]interface{})[0].(*ast.NullString).String,
IsEmptyIndex: $4.([]interface{})[0].(*ast.NullString).Empty,
}

if $8 != nil {
c.Option = $8.(*ast.IndexOption)
}

if indexType := $4.([]interface{})[1]; indexType != nil {
if c.Option == nil {
c.Option = &ast.IndexOption{}
}
c.Option.Tp = indexType.(model.IndexType)
}
if c.Option == nil || c.Option.Tp != model.IndexTypeHNSW {
if c.Option == nil {
c.Option = &ast.IndexOption{Tp: model.IndexTypeHNSW}
} else if c.Option.Tp == model.IndexTypeInvalid {
c.Option.Tp = model.IndexTypeHNSW
}
if c.Option.Tp != model.IndexTypeHNSW {
yylex.AppendError(ErrSyntax)
return 1
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3189,6 +3189,8 @@ func TestDDL(t *testing.T) {
{"ALTER TABLE t ADD VECTOR INDEX ((lower(a))) USING HNSW COMMENT 'a'", true, "ALTER TABLE `t` ADD VECTOR INDEX((LOWER(`a`))) USING HNSW COMMENT 'a'"},
{"ALTER TABLE t ADD VECTOR INDEX ((VEC_COSINE_DISTANCE(a), a)) USING HNSW COMMENT 'a'", false, ""},
{"ALTER TABLE t ADD VECTOR INDEX (a, (VEC_COSINE_DISTANCE(a))) USING HNSW COMMENT 'a'", false, ""},
{"ALTER TABLE t ADD VECTOR INDEX ((VEC_COSINE_DISTANCE(a))) USING HYPO COMMENT 'a'", false, ""},
{"ALTER TABLE t ADD VECTOR INDEX ((VEC_COSINE_DISTANCE(a))) COMMENT 'a'", true, "ALTER TABLE `t` ADD VECTOR INDEX((VEC_COSINE_DISTANCE(`a`))) USING HNSW COMMENT 'a'"},
{"ALTER TABLE t ADD VECTOR INDEX ((VEC_COSINE_DISTANCE(a))) USING HNSW COMMENT 'a'", true, "ALTER TABLE `t` ADD VECTOR INDEX((VEC_COSINE_DISTANCE(`a`))) USING HNSW COMMENT 'a'"},
{"ALTER TABLE t ADD VECTOR INDEX IF NOT EXISTS ((VEC_COSINE_DISTANCE(a))) USING HNSW COMMENT 'a'", true, "ALTER TABLE `t` ADD VECTOR INDEX IF NOT EXISTS((VEC_COSINE_DISTANCE(`a`))) USING HNSW COMMENT 'a'"},
{"ALTER TABLE t ADD CONSTRAINT fk_t2_id FOREIGN KEY (t2_id) REFERENCES t(id)", true, "ALTER TABLE `t` ADD CONSTRAINT `fk_t2_id` FOREIGN KEY (`t2_id`) REFERENCES `t`(`id`)"},
Expand Down Expand Up @@ -3366,7 +3368,7 @@ func TestDDL(t *testing.T) {
// For create vector index statement
{"CREATE VECTOR INDEX idx ON t (a) USING HNSW ", false, ""},
{"CREATE VECTOR INDEX idx ON t (a, b) USING HNSW ", false, ""},
{"CREATE VECTOR INDEX idx ON t ((VEC_COSINE_DISTANCE(a)))", false, ""},
{"CREATE VECTOR INDEX idx ON t ((VEC_COSINE_DISTANCE(a)))", true, "CREATE VECTOR INDEX `idx` ON `t` ((VEC_COSINE_DISTANCE(`a`))) USING HNSW"},
{"CREATE VECTOR INDEX idx ON t ((VEC_COSINE_DISTANCE(a))) TYPE BTREE", false, ""},
{"CREATE VECTOR INDEX idx ON t USING HNSW ((VEC_COSINE_DISTANCE(a)))", false, ""},
{"CREATE VECTOR idx ON t ((VEC_COSINE_DISTANCE(a))) USING HNSW", false, ""},
Expand Down Expand Up @@ -3840,7 +3842,8 @@ func TestDDL(t *testing.T) {
// for create table with vector index
{"create table t(a int, b vector(3), vector index(b) USING HNSW);", false, ""},
{"create table t(a int, b vector(3), vector index(a, b) USING HNSW);", false, ""},
{"create table t(a int, b vector(3), vector index((VEC_COSINE_DISTANCE(b))));", false, ""},
{"create table t(a int, b vector(3), vector index((VEC_COSINE_DISTANCE(b))));", true, "CREATE TABLE `t` (`a` INT,`b` VECTOR(3),VECTOR INDEX((VEC_COSINE_DISTANCE(`b`))) USING HNSW)"},
{"create table t(a int, b vector(3), vector index((VEC_COSINE_DISTANCE(b))) USING HASH);", false, ""},
{"create table t(a int, b vector(3), vector index(a, (VEC_COSINE_DISTANCE(b))) USING HNSW);", false, ""},
{"create table t(a int, b vector(3), vector index((VEC_COSINE_DISTANCE(b)), a) USING HNSW);", false, ""},
{"create table t(a int, b vector(3), vector index(VEC_COSINE_DISTANCE(b)) USING HNSW);", false, ""},
Expand Down

0 comments on commit f7f573b

Please sign in to comment.