Skip to content

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

chore: handle index expression key length (#10456)

h3n4lc1267c42a582
diff --git a/backend/plugin/schema/mysql/get_design_schema.go b/backend/plugin/schema/mysql/get_design_schema.go
index 5e1984ad64..0028a202cc 100644
--- a/backend/plugin/schema/mysql/get_design_schema.go
+++ b/backend/plugin/schema/mysql/get_design_schema.go
@@ -402,8 +402,8 @@ func (g *mysqlDesignSchemaGenerator) EnterTableConstraintDef(ctx *mysql.TableCon
}
}
- keys := extractKeyListVariants(ctx.KeyListVariants())
- if equalKeys(keys, g.currentTable.indexes["PRIMARY"].keys) {
+ keys, keyLengths := extractKeyListVariants(ctx.KeyListVariants())
+ if equalKeys(keys, g.currentTable.indexes["PRIMARY"].keys) && equalKeyLengths(keyLengths, g.currentTable.indexes["PRIMARY"].lengths) {
if _, err := g.tableConstraints.WriteString(ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)); err != nil {
g.err = err
return
@@ -435,7 +435,7 @@ func (g *mysqlDesignSchemaGenerator) EnterTableConstraintDef(ctx *mysql.TableCon
fk := g.currentTable.foreignKeys[name]
- columns := extractKeyList(ctx.KeyList())
+ columns, _ := extractKeyList(ctx.KeyList())
referencedTable, referencedColumns := extractReference(ctx.References())
equal := equalKeys(columns, fk.columns) && referencedTable == fk.referencedTable && equalKeys(referencedColumns, fk.referencedColumns)
if equal {
@@ -470,8 +470,8 @@ func (g *mysqlDesignSchemaGenerator) EnterTableConstraintDef(ctx *mysql.TableCon
idx := g.currentTable.indexes[name]
- keys := extractKeyListVariants(ctx.KeyListVariants())
- equal := equalKeys(keys, idx.keys)
+ keys, keyLengths := extractKeyListVariants(ctx.KeyListVariants())
+ equal := equalKeys(keys, idx.keys) && equalKeyLengths(keyLengths, idx.lengths)
var comment string
for _, v := range ctx.AllIndexOption() {
@@ -537,8 +537,8 @@ func (g *mysqlDesignSchemaGenerator) EnterTableConstraintDef(ctx *mysql.TableCon
}
idx := g.currentTable.indexes[name]
- keys := extractKeyListVariants(ctx.KeyListVariants())
- equal := equalKeys(keys, idx.keys)
+ keys, keyLengths := extractKeyListVariants(ctx.KeyListVariants())
+ equal := equalKeys(keys, idx.keys) && equalKeyLengths(keyLengths, idx.lengths)
equal = equal && (!idx.primary) && (idx.unique) && (idx.comment == comment)
if equal {
@@ -583,8 +583,8 @@ func (g *mysqlDesignSchemaGenerator) EnterTableConstraintDef(ctx *mysql.TableCon
}
idx := g.currentTable.indexes[name]
- keys := extractKeyListVariants(ctx.KeyListVariants())
- equal := equalKeys(keys, idx.keys)
+ keys, keyLengths := extractKeyListVariants(ctx.KeyListVariants())
+ equal := equalKeys(keys, idx.keys) && equalKeyLengths(keyLengths, idx.lengths)
equal = equal && (!idx.primary) && (!idx.unique) && (idx.comment == comment)
if equal {
@@ -628,8 +628,8 @@ func (g *mysqlDesignSchemaGenerator) EnterTableConstraintDef(ctx *mysql.TableCon
}
idx := g.currentTable.indexes[name]
- keys := extractKeyListVariants(ctx.KeyListVariants())
- equal := equalKeys(keys, idx.keys)
+ keys, keyLengths := extractKeyListVariants(ctx.KeyListVariants())
+ equal := equalKeys(keys, idx.keys) && equalKeyLengths(keyLengths, idx.lengths)
equal = equal && (!idx.primary) && (!idx.unique) && (idx.comment == comment)
if equal {
diff --git a/backend/plugin/schema/mysql/parse_to_metadata.go b/backend/plugin/schema/mysql/parse_to_metadata.go
index 8dcc3f99eb..b139bd65ba 100644
--- a/backend/plugin/schema/mysql/parse_to_metadata.go
+++ b/backend/plugin/schema/mysql/parse_to_metadata.go
@@ -195,12 +195,13 @@ func (t *mysqlTransformer) EnterTableConstraintDef(ctx *mysql.TableConstraintDef
symbol := strings.ToUpper(ctx.GetType_().GetText())
switch symbol {
case "PRIMARY":
- list := extractKeyListVariants(ctx.KeyListVariants())
+ keys, keyLengths := extractKeyListVariants(ctx.KeyListVariants())
table := t.state.schemas[""].tables[t.currentTable]
table.indexes["PRIMARY"] = &indexState{
id: len(table.indexes),
name: "PRIMARY",
- keys: list,
+ keys: keys,
+ lengths: keyLengths,
primary: true,
unique: true,
}
@@ -211,7 +212,7 @@ func (t *mysqlTransformer) EnterTableConstraintDef(ctx *mysql.TableConstraintDef
} else if ctx.IndexName() != nil {
name = mysqlparser.NormalizeMySQLIdentifier(ctx.IndexName().Identifier())
}
- keys := extractKeyList(ctx.KeyList())
+ keys, _ := extractKeyList(ctx.KeyList())
table := t.state.schemas[""].tables[t.currentTable]
if table.foreignKeys[name] != nil {
t.err = errors.New("multiple foreign keys found: " + name)
@@ -231,7 +232,7 @@ func (t *mysqlTransformer) EnterTableConstraintDef(ctx *mysql.TableConstraintDef
if ctx.IndexName() != nil {
name = mysqlparser.NormalizeMySQLIdentifier(ctx.IndexName().Identifier())
}
- list := extractKeyListVariants(ctx.KeyListVariants())
+ keys, keyLengths := extractKeyListVariants(ctx.KeyListVariants())
table := t.state.schemas[""].tables[t.currentTable]
if table.indexes[name] != nil {
t.err = errors.New("multiple indexes found: " + name)
@@ -240,7 +241,8 @@ func (t *mysqlTransformer) EnterTableConstraintDef(ctx *mysql.TableConstraintDef
idx := &indexState{
id: len(table.indexes),
name: name,
- keys: list,
+ keys: keys,
+ lengths: keyLengths,
primary: false,
unique: false,
tp: symbol,
@@ -251,7 +253,7 @@ func (t *mysqlTransformer) EnterTableConstraintDef(ctx *mysql.TableConstraintDef
if ctx.IndexName() != nil {
name = mysqlparser.NormalizeMySQLIdentifier(ctx.IndexName().Identifier())
}
- list := extractKeyListVariants(ctx.KeyListVariants())
+ keys, keyLengths := extractKeyListVariants(ctx.KeyListVariants())
table := t.state.schemas[""].tables[t.currentTable]
if table.indexes[name] != nil {
t.err = errors.New("multiple indexes found: " + name)
@@ -260,7 +262,8 @@ func (t *mysqlTransformer) EnterTableConstraintDef(ctx *mysql.TableConstraintDef
idx := &indexState{
id: len(table.indexes),
name: name,
- keys: list,
+ keys: keys,
+ lengths: keyLengths,
primary: false,
unique: false,
tp: symbol,
@@ -273,7 +276,7 @@ func (t *mysqlTransformer) EnterTableConstraintDef(ctx *mysql.TableConstraintDef
} else {
t.err = errors.New("index name not found")
}
- list := extractKeyListVariants(ctx.KeyListVariants())
+ keys, keyLengths := extractKeyListVariants(ctx.KeyListVariants())
table := t.state.schemas[""].tables[t.currentTable]
if table.indexes[name] != nil {
t.err = errors.New("multiple indexes found: " + name)
@@ -286,7 +289,8 @@ func (t *mysqlTransformer) EnterTableConstraintDef(ctx *mysql.TableConstraintDef
idx := &indexState{
id: len(table.indexes),
name: name,
- keys: list,
+ keys: keys,
+ lengths: keyLengths,
primary: false,
unique: symbol == "UNIQUE",
tp: tp,
diff --git a/backend/plugin/schema/mysql/state.go b/backend/plugin/schema/mysql/state.go
index 6858c3e218..8adf8e9f13 100644
--- a/backend/plugin/schema/mysql/state.go
+++ b/backend/plugin/schema/mysql/state.go
@@ -363,6 +363,7 @@ type indexState struct {
id int
name string
keys []string
+ lengths []int64
primary bool
unique bool
tp string
@@ -376,6 +377,7 @@ func (i *indexState) convertToIndexMetadata() *storepb.IndexMetadata {
Primary: i.primary,
Unique: i.unique,
Comment: i.comment,
+ KeyLength: i.lengths,
// Unsupported, for tests only.
Visible: true,
Type: i.tp,
@@ -391,6 +393,7 @@ func convertToIndexState(id int, index *storepb.IndexMetadata) *indexState {
unique: index.Unique,
tp: index.Type,
comment: index.Comment,
+ lengths: index.KeyLength,
}
}
@@ -399,8 +402,8 @@ func (i *indexState) toString(buf io.StringWriter) error {
if _, err := buf.WriteString("PRIMARY KEY ("); err != nil {
return err
}
- for i, key := range i.keys {
- if i > 0 {
+ for j, key := range i.keys {
+ if j > 0 {
if _, err := buf.WriteString(", "); err != nil {
return err
}
@@ -408,6 +411,11 @@ func (i *indexState) toString(buf io.StringWriter) error {
if _, err := buf.WriteString(fmt.Sprintf("`%s`", key)); err != nil {
return err
}
+ if j < len(i.lengths) && i.lengths[j] > 0 {
+ if _, err := buf.WriteString(fmt.Sprintf("(%d)", i.lengths[j])); err != nil {
+ return err
+ }
+ }
}
if _, err := buf.WriteString(")"); err != nil {
return err
@@ -449,6 +457,11 @@ func (i *indexState) toString(buf io.StringWriter) error {
if _, err := buf.WriteString(fmt.Sprintf("`%s`", key)); err != nil {
return err
}
+ if j < len(i.lengths) && i.lengths[j] > 0 {
+ if _, err := buf.WriteString(fmt.Sprintf("(%d)", i.lengths[j])); err != nil {
+ return err
+ }
+ }
}
}
if _, err := buf.WriteString(")"); err != nil {
diff --git a/backend/plugin/schema/mysql/testdata/get_design_schema.yaml b/backend/plugin/schema/mysql/testdata/get_design_schema.yaml
index 659b82883d..9cb2cccda7 100644
--- a/backend/plugin/schema/mysql/testdata/get_design_schema.yaml
+++ b/backend/plugin/schema/mysql/testdata/get_design_schema.yaml
@@ -341,6 +341,15 @@
"primary": true,
"visible": true,
"comment": "primary key of multiple key fields"
+ },
+ {
+ "name": "idx_b_5",
+ "expressions": [
+ "b"
+ ],
+ "keyLength": [
+ 5
+ ]
}
],
"foreignKeys": [
@@ -502,7 +511,8 @@
`e` int ,
`b` char(10) NOT NULL,
PRIMARY KEY (`a`, `b`, `c`) COMMENT 'primary key of multiple key fields',
- CONSTRAINT `fk1` FOREIGN KEY (`b`) REFERENCES `t2` (`b`)
+ CONSTRAINT `fk1` FOREIGN KEY (`b`) REFERENCES `t2` (`b`),
+ KEY `idx_b_5` (`b`(5))
);
--
diff --git a/backend/plugin/schema/mysql/testdata/parse_to_metadata.yaml b/backend/plugin/schema/mysql/testdata/parse_to_metadata.yaml
index 47fea3045e..d01b20dfc4 100644
--- a/backend/plugin/schema/mysql/testdata/parse_to_metadata.yaml
+++ b/backend/plugin/schema/mysql/testdata/parse_to_metadata.yaml
@@ -5,12 +5,14 @@
`b` VARCHAR(20) NOT NULL,
`c` int NOT NULL,
`d` text NULL,
+ `e` varchar(20),
PRIMARY KEY (`a`),
UNIQUE KEY `t_c_uniq_idx` (`c` DESC),
UNIQUE KEY `t_c_plus_5_uniq_idx` (((`c` + 5)) DESC),
UNIQUE KEY `t_c_plus_5_t_c_plus_3_uniq_idx` (((`c` + 5)) DESC,((`c` + 3))),
KEY `idx_c` (`c`),
KEY `idx_d` (`d`(10)),
+ KEY `idx_e` (`e`(10)),
FULLTEXT KEY `t_d_fulltext_idx` (`d`)
) ENGINE=InnoDB COLLATE=utf8mb4_0900_ai_ci COMMENT 'this is comment with '' escaped';
metadata: |-
@@ -42,6 +44,12 @@
"position": 4,
"nullable": true,
"type": "text"
+ },
+ {
+ "name": "e",
+ "position": 5,
+ "nullable": true,
+ "type": "varchar(20)"
}
],
"indexes": [
@@ -50,6 +58,9 @@
"expressions": [
"a"
],
+ "keyLength": [
+ "-1"
+ ],
"unique": true,
"primary": true,
"visible": true
@@ -59,6 +70,9 @@
"expressions": [
"c"
],
+ "keyLength": [
+ "-1"
+ ],
"type": "BTREE",
"unique": true,
"visible": true
@@ -68,6 +82,9 @@
"expressions": [
"((`c` + 5))"
],
+ "keyLength": [
+ "-1"
+ ],
"type": "BTREE",
"unique": true,
"visible": true
@@ -78,6 +95,10 @@
"((`c` + 5))",
"((`c` + 3))"
],
+ "keyLength": [
+ "-1",
+ "-1"
+ ],
"type": "BTREE",
"unique": true,
"visible": true
@@ -87,6 +108,9 @@
"expressions": [
"c"
],
+ "keyLength": [
+ "-1"
+ ],
"type": "BTREE",
"visible": true
},
@@ -95,6 +119,20 @@
"expressions": [
"d"
],
+ "keyLength": [
+ "10"
+ ],
+ "type": "BTREE",
+ "visible": true
+ },
+ {
+ "name": "idx_e",
+ "expressions": [
+ "e"
+ ],
+ "keyLength": [
+ "10"
+ ],
"type": "BTREE",
"visible": true
},
@@ -103,6 +141,9 @@
"expressions": [
"d"
],
+ "keyLength": [
+ "-1"
+ ],
"type": "FULLTEXT",
"visible": true
}
@@ -163,6 +204,10 @@
"a",
"b"
],
+ "keyLength": [
+ "-1",
+ "-1"
+ ],
"unique": true,
"primary": true,
"visible": true
@@ -198,6 +243,9 @@
"expressions": [
"b"
],
+ "keyLength": [
+ "-1"
+ ],
"unique": true,
"primary": true,
"visible": true
diff --git a/backend/plugin/schema/mysql/utils.go b/backend/plugin/schema/mysql/utils.go
index 9a2735a124..4c6d073e1a 100644
--- a/backend/plugin/schema/mysql/utils.go
+++ b/backend/plugin/schema/mysql/utils.go
@@ -1,6 +1,7 @@
package mysql
import (
+ "strconv"
"strings"
"github.com/antlr4-go/antlr/v4"
@@ -66,40 +67,59 @@ func extractIdentifierList(ctx mysql.IIdentifierListContext) []string {
return result
}
-func extractKeyListVariants(ctx mysql.IKeyListVariantsContext) []string {
+func extractKeyListVariants(ctx mysql.IKeyListVariantsContext) ([]string, []int64) {
if ctx.KeyList() != nil {
return extractKeyList(ctx.KeyList())
}
if ctx.KeyListWithExpression() != nil {
return extractKeyListWithExpression(ctx.KeyListWithExpression())
}
- return nil
+ return nil, nil
}
-func extractKeyListWithExpression(ctx mysql.IKeyListWithExpressionContext) []string {
+func extractKeyListWithExpression(ctx mysql.IKeyListWithExpressionContext) ([]string, []int64) {
var result []string
+ var keyLengths []int64
for _, key := range ctx.AllKeyPartOrExpression() {
if key.KeyPart() != nil {
- keyText := mysqlparser.NormalizeMySQLIdentifier(key.KeyPart().Identifier())
+ keyText, keyLength := getKeyExpressionAndLengthFromKeyPart(key.KeyPart())
result = append(result, keyText)
+ keyLengths = append(keyLengths, keyLength)
} else if key.ExprWithParentheses() != nil {
keyText := key.GetParser().GetTokenStream().GetTextFromRuleContext(key.ExprWithParentheses())
result = append(result, keyText)
+ keyLengths = append(keyLengths, -1)
}
}
- return result
+ return result, keyLengths
}
-func extractKeyList(ctx mysql.IKeyListContext) []string {
+func extractKeyList(ctx mysql.IKeyListContext) ([]string, []int64) {
var result []string
+ var keyLengths []int64
for _, key := range ctx.AllKeyPart() {
- keyText := mysqlparser.NormalizeMySQLIdentifier(key.Identifier())
- if key.FieldLength() != nil || key.Direction() != nil {
- keyText = key.GetParser().GetTokenStream().GetTextFromRuleContext(key)
- }
+ keyText, keyLength := getKeyExpressionAndLengthFromKeyPart(key)
result = append(result, keyText)
+ keyLengths = append(keyLengths, keyLength)
}
- return result
+ return result, keyLengths
+}
+
+func getKeyExpressionAndLengthFromKeyPart(ctx mysql.IKeyPartContext) (string, int64) {
+ keyText := mysqlparser.NormalizeMySQLIdentifier(ctx.Identifier())
+ keyLength := int64(-1)
+ if ctx.FieldLength() != nil {
+ l := ctx.FieldLength().GetText()
+ if len(l) > 2 && l[0] == '(' && l[len(l)-1] == ')' {
+ l = l[1 : len(l)-1]
+ }
+ length, err := strconv.ParseInt(l, 10, 64)
+ if err != nil {
+ length = -1
+ }
+ keyLength = length
+ }
+ return keyText, keyLength
}
type columnAttr struct {
@@ -211,6 +231,18 @@ func equalKeys(a, b []string) bool {
return true
}
+func equalKeyLengths(a, b []int64) bool {
+ if len(a) != len(b) {
+ return false
+ }
+ for i, length := range a {
+ if length != b[i] {
+ return false
+ }
+ }
+ return true
+}
+
func nextDefaultChannelTokenIndex(tokens antlr.TokenStream, currentIndex int) int {
for i := currentIndex + 1; i < tokens.Size(); i++ {
if tokens.Get(i).GetChannel() == antlr.TokenDefaultChannel {