Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore(sql-review): implement no duplicated index in table (#10721)

boojacke7f3d038fea9
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.go
index 6c23bac647..d53b2fb2c4 100644
--- a/backend/plugin/advisor/advisor.go
+++ b/backend/plugin/advisor/advisor.go
@@ -170,6 +170,9 @@ const (
// MySQLTableDisallowTrigger is an advisor type for MySQL disallow table trigger.
MySQLTableDisallowTrigger Type = "bb.plugin.advisor.mysql.table.disallow-trigger"
+ // MySQLTableNoDuplicateIndex is an advisor type for MySQL no duplicate index.
+ MySQLTableNoDuplicateIndex Type = "bb.plugin.advisor.mysql.table.no-duplicate-index"
+
// MySQLDatabaseAllowDropIfEmpty is an advisor type for MySQL only allow drop empty database.
MySQLDatabaseAllowDropIfEmpty Type = "bb.plugin.advisor.mysql.database.drop-empty-database"
diff --git a/backend/plugin/advisor/catalog/walk_through_for_mysql.go b/backend/plugin/advisor/catalog/walk_through_for_mysql.go
index 5ceebb4fac..7ec8441d64 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_mysql.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_mysql.go
@@ -436,12 +436,12 @@ func (l *mysqlListener) EnterCreateIndex(ctx *mysql.CreateIndexContext) {
if ctx.CreateIndexTarget() == nil || ctx.CreateIndexTarget().KeyListVariants() == nil {
return
}
- columnList := mysqlparser.NormalizeKeyListVariants(ctx.CreateIndexTarget().KeyListVariants())
- if err := table.mysqlValidateKeyStringList(l.databaseState.ctx, columnList, false /* primary */, isSpatial); err != nil {
+ if err := table.mysqlValidateKeyListVariants(l.databaseState.ctx, ctx.CreateIndexTarget().KeyListVariants(), false /* primary */, isSpatial); err != nil {
l.err = err
return
}
+ columnList := mysqlparser.NormalizeKeyListVariants(ctx.CreateIndexTarget().KeyListVariants())
if err := table.mysqlCreateIndex(indexName, columnList, unique, tp, mysql.NewEmptyTableConstraintDefContext(), ctx); err != nil {
l.err = err
return
@@ -821,10 +821,10 @@ func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mys
// never reach here.
return nil
}
- keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())
- if err := t.mysqlValidateKeyStringList(ctx, keyList, true /* primary */, false /* isSpatial*/); err != nil {
+ if err := t.mysqlValidateKeyListVariants(ctx, constraintDef.KeyListVariants(), true /* primary */, false /* isSpatial*/); err != nil {
return err
}
+ keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())
if err := t.mysqlCreatePrimaryKey(keyList, mysqlGetIndexType(constraintDef)); err != nil {
return err
}
@@ -834,8 +834,7 @@ func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mys
// never reach here.
return nil
}
- keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())
- if err := t.mysqlValidateKeyStringList(ctx, keyList, false /* primary */, false /* isSpatial */); err != nil {
+ if err := t.mysqlValidateKeyListVariants(ctx, constraintDef.KeyListVariants(), false /* primary */, false /* isSpatial */); err != nil {
return err
}
@@ -843,6 +842,7 @@ func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mys
if constraintDef.IndexNameAndType() != nil && constraintDef.IndexNameAndType().IndexName() != nil {
indexName = mysqlparser.NormalizeIndexName(constraintDef.IndexNameAndType().IndexName())
}
+ keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())
if err := t.mysqlCreateIndex(indexName, keyList, false /* unique */, mysqlGetIndexType(constraintDef), constraintDef, mysql.NewEmptyCreateIndexContext()); err != nil {
return err
}
@@ -852,8 +852,7 @@ func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mys
// never reach here.
return nil
}
- keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())
- if err := t.mysqlValidateKeyStringList(ctx, keyList, false /* primary */, false /* isSpatial*/); err != nil {
+ if err := t.mysqlValidateKeyListVariants(ctx, constraintDef.KeyListVariants(), false /* primary */, false /* isSpatial*/); err != nil {
return err
}
@@ -864,6 +863,7 @@ func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mys
if constraintDef.IndexNameAndType() != nil && constraintDef.IndexNameAndType().IndexName() != nil {
indexName = mysqlparser.NormalizeIndexName(constraintDef.IndexNameAndType().IndexName())
}
+ keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())
if err := t.mysqlCreateIndex(indexName, keyList, true /* unique */, mysqlGetIndexType(constraintDef), constraintDef, mysql.NewEmptyCreateIndexContext()); err != nil {
return err
}
@@ -873,14 +873,14 @@ func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mys
// never reach here.
return nil
}
- keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())
- if err := t.mysqlValidateKeyStringList(ctx, keyList, false /* primary */, false /* isSpatial*/); err != nil {
+ if err := t.mysqlValidateKeyListVariants(ctx, constraintDef.KeyListVariants(), false /* primary */, false /* isSpatial*/); err != nil {
return err
}
indexName := ""
if constraintDef.IndexName() != nil {
indexName = mysqlparser.NormalizeIndexName(constraintDef.IndexName())
}
+ keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())
if err := t.mysqlCreateIndex(indexName, keyList, false /* unique */, mysqlGetIndexType(constraintDef), constraintDef, mysql.NewEmptyCreateIndexContext()); err != nil {
return err
}
@@ -894,8 +894,25 @@ func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mys
return nil
}
-func (t *TableState) mysqlValidateKeyStringList(ctx *FinderContext, keyList []string, primary bool, isSpatial bool) *WalkThroughError {
- for _, columnName := range keyList {
+// mysqlValidateKeyListVariants validates the key list variants.
+func (t *TableState) mysqlValidateKeyListVariants(ctx *FinderContext, keyList mysql.IKeyListVariantsContext, primary bool, isSpatial bool) *WalkThroughError {
+ if keyList.KeyList() != nil {
+ columns := mysqlparser.NormalizeKeyList(keyList.KeyList())
+ if err := t.mysqlValidateColumnList(ctx, columns, primary, isSpatial); err != nil {
+ return err
+ }
+ }
+ if keyList.KeyListWithExpression() != nil {
+ expressions := mysqlparser.NormalizeKeyListWithExpression(keyList.KeyListWithExpression())
+ if err := t.mysqlValidateExpressionList(ctx, expressions, primary, isSpatial); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func (t *TableState) mysqlValidateColumnList(ctx *FinderContext, columnList []string, primary bool, isSpatial bool) *WalkThroughError {
+ for _, columnName := range columnList {
column, exists := t.columnSet[strings.ToLower(columnName)]
if !exists {
if ctx.CheckIntegrity {
@@ -917,6 +934,30 @@ func (t *TableState) mysqlValidateKeyStringList(ctx *FinderContext, keyList []st
return nil
}
+// mysqlValidateExpressionList validates the expression list.
+// TODO: update expression validation.
+func (t *TableState) mysqlValidateExpressionList(_ *FinderContext, expressionList []string, primary bool, isSpatial bool) *WalkThroughError {
+ for _, expression := range expressionList {
+ column, exists := t.columnSet[strings.ToLower(expression)]
+ // If expression is not a column, we do not need to validate it.
+ if !exists {
+ continue
+ }
+
+ if primary {
+ column.nullable = newFalsePointer()
+ }
+ if isSpatial && column.nullable != nil && *column.nullable {
+ return &WalkThroughError{
+ Type: ErrorTypeSpatialIndexKeyNullable,
+ // The error content comes from MySQL.
+ Content: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.name),
+ }
+ }
+ }
+ return nil
+}
+
func mysqlGetIndexType(tableConstraint mysql.ITableConstraintDefContext) string {
if tableConstraint.GetType_() == nil {
return "BTREE"
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.go
index 71874404fc..8de2542c40 100644
--- a/backend/plugin/advisor/code.go
+++ b/backend/plugin/advisor/code.go
@@ -120,6 +120,7 @@ const (
DuplicateColumnInIndex Code = 812
IndexCountExceedsLimit Code = 813
CreateIndexUnconcurrently Code = 814
+ DuplicateIndexInTable Code = 815
// 1001 ~ 1099 charset error code.
DisabledCharset Code = 1001
diff --git a/backend/plugin/advisor/mysql/advisor_table_no_duplicate_index.go b/backend/plugin/advisor/mysql/advisor_table_no_duplicate_index.go
new file mode 100644
index 0000000000..c81336d248
--- /dev/null
+++ b/backend/plugin/advisor/mysql/advisor_table_no_duplicate_index.go
@@ -0,0 +1,247 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/antlr4-go/antlr/v4"
+ "github.com/pkg/errors"
+
+ mysql "github.com/bytebase/mysql-parser"
+
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+var (
+ _ advisor.Advisor = (*TableNoDuplicateIndexAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_MYSQL, advisor.MySQLTableNoDuplicateIndex, &TableNoDuplicateIndexAdvisor{})
+}
+
+// TableNoDuplicateIndexAdvisor is the advisor checking for no duplicate index in table.
+type TableNoDuplicateIndexAdvisor struct {
+}
+
+// Check checks for no duplicate index in table.
+func (*TableNoDuplicateIndexAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {
+ stmtList, ok := ctx.AST.([]*mysqlparser.ParseResult)
+ if !ok {
+ return nil, errors.Errorf("failed to convert to mysql parser result")
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ checker := &tableNoDuplicateIndexChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ indexList: []duplicateIndex{},
+ }
+ for _, stmt := range stmtList {
+ checker.baseLine = stmt.BaseLine
+ antlr.ParseTreeWalkerDefault.Walk(checker, stmt.Tree)
+ }
+
+ if len(checker.adviceList) == 0 {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return checker.adviceList, nil
+}
+
+type tableNoDuplicateIndexChecker struct {
+ *mysql.BaseMySQLParserListener
+
+ baseLine int
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ indexList []duplicateIndex
+}
+
+type duplicateIndex struct {
+ indexName string
+ // BTREE, HASH, etc.
+ indexType string
+ unique bool
+ fulltext bool
+ spatial bool
+ columns []string
+ // line is the line number of the index definition.
+ line int
+}
+
+func (checker *tableNoDuplicateIndexChecker) EnterCreateTable(ctx *mysql.CreateTableContext) {
+ if ctx.TableName() == nil {
+ return
+ }
+ if ctx.TableElementList() == nil {
+ return
+ }
+
+ // Build suspect index list.
+ for _, tableElement := range ctx.TableElementList().AllTableElement() {
+ if tableElement == nil || tableElement.TableConstraintDef() == nil {
+ continue
+ }
+ checker.handleConstraintDef(tableElement.TableConstraintDef())
+ }
+ // Check for duplicate index.
+ if index := hasDuplicateIndexes(checker.indexList); index != nil {
+ _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.DuplicateIndexInTable,
+ Title: checker.title,
+ Content: fmt.Sprintf("`%s` has duplicate index `%s`", tableName, index.indexName),
+ Line: index.line,
+ })
+ }
+}
+
+func (checker *tableNoDuplicateIndexChecker) EnterAlterTable(ctx *mysql.AlterTableContext) {
+ if ctx.TableRef() == nil {
+ return
+ }
+ if ctx.AlterTableActions() == nil || ctx.AlterTableActions().AlterCommandList() == nil || ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+ return
+ }
+
+ for _, alterListItem := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
+ if alterListItem == nil {
+ continue
+ }
+ if alterListItem.ADD_SYMBOL() != nil && alterListItem.TableConstraintDef() != nil {
+ checker.handleConstraintDef(alterListItem.TableConstraintDef())
+ }
+ }
+ if index := hasDuplicateIndexes(checker.indexList); index != nil {
+ _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.DuplicateIndexInTable,
+ Title: checker.title,
+ Content: fmt.Sprintf("`%s` has duplicate index `%s`", tableName, index.indexName),
+ Line: index.line,
+ })
+ }
+}
+
+func (checker *tableNoDuplicateIndexChecker) handleConstraintDef(ctx mysql.ITableConstraintDefContext) {
+ switch ctx.GetType_().GetTokenType() {
+ case mysql.MySQLParserINDEX_SYMBOL, mysql.MySQLParserKEY_SYMBOL, mysql.MySQLParserPRIMARY_SYMBOL, mysql.MySQLParserUNIQUE_SYMBOL, mysql.MySQLParserFULLTEXT_SYMBOL, mysql.MySQLParserSPATIAL_SYMBOL:
+ default:
+ return
+ }
+
+ index := duplicateIndex{
+ indexType: "BTREE",
+ line: checker.baseLine + ctx.GetStart().GetLine(),
+ }
+ if ctx.KeyListVariants() != nil {
+ index.columns = mysqlparser.NormalizeKeyListVariants(ctx.KeyListVariants())
+ }
+ if ctx.UNIQUE_SYMBOL() != nil {
+ index.unique = true
+ } else if ctx.FULLTEXT_SYMBOL() != nil {
+ index.fulltext = true
+ } else if ctx.SPATIAL_SYMBOL() != nil {
+ index.spatial = true
+ }
+ if ctx.IndexNameAndType() != nil {
+ if ctx.IndexNameAndType().IndexName() != nil {
+ index.indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())
+ }
+ if ctx.IndexNameAndType().IndexType() != nil {
+ index.indexType = ctx.IndexNameAndType().IndexType().GetText()
+ }
+ }
+ checker.indexList = append(checker.indexList, index)
+}
+
+func (checker *tableNoDuplicateIndexChecker) EnterCreateIndex(ctx *mysql.CreateIndexContext) {
+ if ctx.CreateIndexTarget() == nil || ctx.CreateIndexTarget().TableRef() == nil || ctx.CreateIndexTarget().KeyListVariants() == nil {
+ return
+ }
+
+ index := duplicateIndex{
+ indexType: "BTREE",
+ line: checker.baseLine + ctx.GetStart().GetLine(),
+ }
+ if ctx.UNIQUE_SYMBOL() != nil {
+ index.unique = true
+ } else if ctx.FULLTEXT_SYMBOL() != nil {
+ index.fulltext = true
+ } else if ctx.SPATIAL_SYMBOL() != nil {
+ index.spatial = true
+ }
+ if ctx.IndexName() != nil {
+ index.indexName = mysqlparser.NormalizeIndexName(ctx.IndexName())
+ }
+ if ctx.IndexNameAndType() != nil {
+ if ctx.IndexNameAndType().IndexName() != nil {
+ index.indexName = mysqlparser.NormalizeIndexName(ctx.IndexNameAndType().IndexName())
+ }
+ if ctx.IndexNameAndType().IndexType() != nil {
+ index.indexType = ctx.IndexNameAndType().IndexType().GetText()
+ }
+ }
+ if ctx.IndexTypeClause() != nil && ctx.IndexTypeClause().IndexType() != nil {
+ index.indexType = ctx.IndexTypeClause().IndexType().GetText()
+ }
+
+ index.columns = mysqlparser.NormalizeKeyListVariants(ctx.CreateIndexTarget().KeyListVariants())
+ checker.indexList = append(checker.indexList, index)
+ if index := hasDuplicateIndexes(checker.indexList); index != nil {
+ _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.CreateIndexTarget().TableRef())
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.DuplicateIndexInTable,
+ Title: checker.title,
+ Content: fmt.Sprintf("`%s` has duplicate index `%s`", tableName, index.indexName),
+ Line: index.line,
+ })
+ }
+}
+
+// hasDuplicateIndexes returns the first duplicate index if found, otherwise nil.
+func hasDuplicateIndexes(indexList []duplicateIndex) *duplicateIndex {
+ seen := make(map[string]struct{})
+ for _, index := range indexList {
+ key := indexKey(index)
+ if _, exists := seen[key]; exists {
+ return &index
+ }
+ seen[key] = struct{}{}
+ }
+ return nil
+}
+
+// indexKey returns a string key for the index with the index type and columns.
+func indexKey(index duplicateIndex) string {
+ parts := []string{}
+ if index.unique {
+ parts = append(parts, "unique")
+ }
+ if index.fulltext {
+ parts = append(parts, "fulltext")
+ }
+ if index.spatial {
+ parts = append(parts, "spatial")
+ }
+ parts = append(parts, index.indexType)
+ parts = append(parts, index.columns...)
+ return strings.Join(parts, "-")
+}
diff --git a/backend/plugin/advisor/mysql/mysql_rules_test.go b/backend/plugin/advisor/mysql/mysql_rules_test.go
index 9953296b63..2824a67fda 100644
--- a/backend/plugin/advisor/mysql/mysql_rules_test.go
+++ b/backend/plugin/advisor/mysql/mysql_rules_test.go
@@ -64,6 +64,8 @@ func TestMySQLRules(t *testing.T) {
advisor.SchemaRuleTableDisallowPartition,
// advisor.SchemaRuleTableDisallowTrigger disallow the table trigger.
advisor.SchemaRuleTableDisallowTrigger,
+ // advisor.SchemaRuleTableNoDuplicateIndex require the table no duplicate index.
+ advisor.SchemaRuleTableNoDuplicateIndex,
// advisor.SchemaRuleRequiredColumn enforce the required columns in each table.
advisor.SchemaRuleRequiredColumn,
diff --git a/backend/plugin/advisor/mysql/test/table_no_duplicate_index.yaml b/backend/plugin/advisor/mysql/test/table_no_duplicate_index.yaml
new file mode 100644
index 0000000000..521acd4fff
--- /dev/null
+++ b/backend/plugin/advisor/mysql/test/table_no_duplicate_index.yaml
@@ -0,0 +1,114 @@
+- statement: |-
+ CREATE TABLE customers (
+ id INT PRIMARY KEY,
+ first_name VARCHAR(50),
+ last_name VARCHAR(50),
+ email VARCHAR(100),
+ INDEX idx_email (email),
+ INDEX idx_name (last_name, first_name)
+ );
+ want:
+ - status: SUCCESS
+ title: OK
+- statement: |-
+ CREATE TABLE orders (
+ id INT PRIMARY KEY,
+ order_number INT,
+ customer_id INT,
+ INDEX (customer_id),
+ INDEX (order_number),
+ UNIQUE (customer_id)
+ );
+ want:
+ - status: SUCCESS
+ title: OK
+- statement: |-
+ CREATE TABLE users (
+ id INT PRIMARY KEY,
+ username VARCHAR(50),
+ email VARCHAR(100),
+ INDEX idx_username (username),
+ INDEX idx_email (email),
+ INDEX idx_username_duplicate (username)
+ );
+ want:
+ - status: WARN
+ code: 815
+ title: table.no-duplicate-index
+ content: "`users` has duplicate index `idx_username_duplicate`"
+ line: 7
+ details: ""
+- statement: |-
+ CREATE TABLE orders (
+ id INT PRIMARY KEY,
+ order_number INT,
+ customer_id INT,
+ INDEX (customer_id),
+ INDEX (order_number),
+ INDEX (customer_id)
+ );
+ want:
+ - status: WARN
+ code: 815
+ title: table.no-duplicate-index
+ content: "`orders` has duplicate index ``"
+ line: 7
+ details: ""
+- statement: |-
+ CREATE TABLE orders (
+ id INT PRIMARY KEY,
+ order_number INT,
+ customer_id INT,
+ INDEX idx_customer_id (customer_id),
+ INDEX idx_order_number (order_number),
+ FOREIGN KEY (customer_id) REFERENCES customers(id)
+ );
+ ALTER TABLE orders ADD INDEX idx_duplicate (customer_id);
+ want:
+ - status: WARN
+ code: 815
+ title: table.no-duplicate-index
+ content: "`orders` has duplicate index `idx_duplicate`"
+ line: 9
+ details: ""
+- statement: |-
+ CREATE TABLE orders (
+ id INT PRIMARY KEY,
+ order_number INT,
+ customer_id INT
+ );
+ CREATE INDEX idx_customer_id ON orders (customer_id);
+ CREATE INDEX idx_customer_id_duplicate ON orders (customer_id);
+ want:
+ - status: WARN
+ code: 815
+ title: table.no-duplicate-index
+ content: "`orders` has duplicate index `idx_customer_id_duplicate`"
+ line: 7
+ details: ""
+- statement: |-
+ CREATE TABLE orders (
+ id INT PRIMARY KEY,
+ order_number INT,
+ customer VARCHAR(50)
+ );
+ CREATE INDEX idx_customer ON orders (customer, (order_number * 2));
+ CREATE INDEX idx_customer_duplicate ON orders (customer, (order_number * 2));
+ want:
+ - status: WARN
+ code: 815
+ title: table.no-duplicate-index
+ content: "`orders` has duplicate index `idx_customer_duplicate`"
+ line: 7
+ details: ""
+- statement: |-
+ CREATE TABLE orders (
+ id INT PRIMARY KEY,
+ order_number INT,
+ customer VARCHAR(50)
+ );
+ CREATE INDEX idx_customer_btree USING BTREE ON orders (customer);
+ CREATE INDEX idx_customer_hash USING HASH ON orders (customer);
+ want:
+ - status: SUCCESS
+ title: OK
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 5f46e7fbfc..5482d49adb 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -108,6 +108,8 @@ const (
SchemaRuleTableDisallowPartition SQLReviewRuleType = "table.disallow-partition"
// SchemaRuleTableDisallowTrigger disallow the table trigger.
SchemaRuleTableDisallowTrigger SQLReviewRuleType = "table.disallow-trigger"
+ // SchemaRuleTableNoDuplicateIndex require the table no duplicate index.
+ SchemaRuleTableNoDuplicateIndex SQLReviewRuleType = "table.no-duplicate-index"
// SchemaRuleRequiredColumn enforce the required columns in each table.
SchemaRuleRequiredColumn SQLReviewRuleType = "column.required"
@@ -1297,6 +1299,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
if engine == storepb.Engine_MYSQL {
return MySQLTableDisallowTrigger, nil
}
+ case SchemaRuleTableNoDuplicateIndex:
+ if engine == storepb.Engine_MYSQL {
+ return MySQLTableNoDuplicateIndex, nil
+ }
case SchemaRuleMySQLEngine:
switch engine {
case storepb.Engine_MYSQL, storepb.Engine_MARIADB:
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index 96400d9d5e..19783090dd 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -331,6 +331,7 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType, dbType storepb.Eng
SchemaRuleTableNoFK,
SchemaRuleTableDisallowPartition,
SchemaRuleTableDisallowTrigger,
+ SchemaRuleTableNoDuplicateIndex,
SchemaRuleColumnNotNull,
SchemaRuleColumnDisallowChangeType,
SchemaRuleColumnSetDefaultForNotNull,
diff --git a/backend/plugin/parser/mysql/normalize.go b/backend/plugin/parser/mysql/normalize.go
index e1e4c3862e..d122dd2792 100644
--- a/backend/plugin/parser/mysql/normalize.go
+++ b/backend/plugin/parser/mysql/normalize.go
@@ -221,15 +221,15 @@ func NormalizeKeyList(ctx parser.IKeyListContext) []string {
return result
}
-// NormalizeMySQLKeyListWithExpression normalize the given keyListWithExpression.
+// NormalizeMySQLKeyListWithExpression normalizes the given keyListWithExpression.
func NormalizeKeyListWithExpression(ctx parser.IKeyListWithExpressionContext) []string {
var result []string
- for _, key := range ctx.AllKeyPartOrExpression() {
- if key.KeyPart() != nil {
- keyText := NormalizeMySQLIdentifier(key.KeyPart().Identifier())
+ for _, expression := range ctx.AllKeyPartOrExpression() {
+ if expression.KeyPart() != nil {
+ keyText := NormalizeMySQLIdentifier(expression.KeyPart().Identifier())
result = append(result, keyText)
- } else if key.ExprWithParentheses() != nil {
- keyText := key.GetParser().GetTokenStream().GetTextFromRuleContext(key.ExprWithParentheses())
+ } else if expression.ExprWithParentheses() != nil {
+ keyText := ctx.GetParser().GetTokenStream().GetTextFromRuleContext(expression.ExprWithParentheses().Expr())
result = append(result, keyText)
}
}