From f7f573b6e19eec74015f161d1d2e9529c54593bc Mon Sep 17 00:00:00 2001 From: Lynn Date: Tue, 8 Oct 2024 11:23:30 +0800 Subject: [PATCH] parser: make HNSW as vector index default type (#56429) close pingcap/tidb#56428 --- pkg/parser/parser.go | 15 ++++++++++++--- pkg/parser/parser.y | 15 ++++++++++++--- pkg/parser/parser_test.go | 7 +++++-- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/pkg/parser/parser.go b/pkg/parser/parser.go index 804d4661972e2..b6f3f59767e4c 100644 --- a/pkg/parser/parser.go +++ b/pkg/parser/parser.go @@ -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 } @@ -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 } diff --git a/pkg/parser/parser.y b/pkg/parser/parser.y index 724462e9d373e..a23a70eac1a95 100644 --- a/pkg/parser/parser.y +++ b/pkg/parser/parser.y @@ -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 } @@ -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 } diff --git a/pkg/parser/parser_test.go b/pkg/parser/parser_test.go index dbb76732865d3..f7142918b3224 100644 --- a/pkg/parser/parser_test.go +++ b/pkg/parser/parser_test.go @@ -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`)"}, @@ -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, ""}, @@ -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, ""},