Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schemadiff: skip keys with expressions in Online DDL analysis #17475

Merged
merged 2 commits into from
Jan 8, 2025
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
10 changes: 10 additions & 0 deletions go/vt/schemadiff/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ func (i *IndexDefinitionEntity) IsUnique() bool {
return i.IndexDefinition.Info.IsUnique()
}

// HasExpression returns true if the index uses an expression, e.g. `KEY idx1 ((id + 1))`.
func (i *IndexDefinitionEntity) HasExpression() bool {
for _, col := range i.IndexDefinition.Columns {
if col.Expression != nil {
return true
}
}
return false
}

// HasNullable returns true if any of the columns in the index are nullable.
func (i *IndexDefinitionEntity) HasNullable() bool {
for _, col := range i.ColumnList.Entities {
Expand Down
5 changes: 5 additions & 0 deletions go/vt/schemadiff/onlineddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ func PrioritizedUniqueKeys(createTableEntity *CreateTableEntity) *IndexDefinitio
if !key.IsUnique() {
continue
}
if key.HasExpression() {
// If the key has an expression this means it unreliably covers the columns,
// we cannot trust it.
continue
}
uniqueKeys = append(uniqueKeys, key)
}
sort.SliceStable(uniqueKeys, func(i, j int) bool {
Expand Down
5 changes: 5 additions & 0 deletions go/vt/schemadiff/onlineddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,11 @@ func TestRevertible(t *testing.T) {
toSchema: `id int primary key, e1 set('a', 'b'), e2 set('a'), e3 set('a', 'b', 'c'), e4 set('a', 'x'), e5 set('a', 'x', 'b'), e6 set('b'), e7 varchar(1), e8 tinyint`,
expandedColumnNames: `e3,e4,e5,e6,e7,e8`,
},
{
name: "index with expression",
fromSchema: "id int, primary key (id), key idx1 ((id + 1))",
toSchema: "id int, primary key (id), key idx2 ((id + 2))",
},
}

var (
Expand Down
11 changes: 8 additions & 3 deletions go/vt/schemadiff/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,14 @@ func (c *CreateTableEntity) IndexDefinitionEntities() []*IndexDefinitionEntity {
keys := c.CreateTable.TableSpec.Indexes
entities := make([]*IndexDefinitionEntity, len(keys))
for i, key := range keys {
colEntities := make([]*ColumnDefinitionEntity, len(key.Columns))
for i, keyCol := range key.Columns {
colEntities[i] = colMap[keyCol.Column.Lowered()]
colEntities := []*ColumnDefinitionEntity{}
for _, keyCol := range key.Columns {
colEntity, ok := colMap[keyCol.Column.Lowered()]
if !ok {
// This can happen if the index is on an expression, e.g. `KEY idx1 ((id + 1))`.
continue
}
colEntities = append(colEntities, colEntity)
}
entities[i] = NewIndexDefinitionEntity(c.Env, key, NewColumnDefinitionEntityList(colEntities))
}
Expand Down
18 changes: 18 additions & 0 deletions go/vt/schemadiff/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,18 @@ func TestCreateTableDiff(t *testing.T) {
"+ KEY `i_idx` (`i`) INVISIBLE",
},
},
{
name: "keys with expression",
from: "create table t1 (id int, primary key (id), key idx1 ((id + 1)))",
to: "create table t1 (id int, primary key (id), key idx2 ((id + 2)))",
diff: "alter table t1 drop key idx1, add key idx2 ((id + 2))",
cdiff: "ALTER TABLE `t1` DROP KEY `idx1`, ADD KEY `idx2` ((`id` + 2))",
textdiffs: []string{
"- KEY `idx1` ((`id` + 1))",
"+ KEY `idx2` ((`id` + 2))",
},
},

// FULLTEXT keys
{
name: "add one fulltext key",
Expand Down Expand Up @@ -2564,6 +2576,12 @@ func TestValidate(t *testing.T) {
alter: "alter table t engine=innodb",
expectErr: &DuplicateKeyNameError{Table: "t", Key: "PRIMARY"},
},
{
name: "key with expression",
from: "create table t (id int, primary key (id), key idx1 ((id + 1)))",
alter: "alter table t add key idx2 ((id + 2))",
to: "create table t (id int, primary key (id), key idx1 ((id + 1)), key idx2 ((id + 2)))",
},
// partitions
{
name: "drop column used by partitions",
Expand Down
Loading