Skip to content

Commit

Permalink
[release-21.0] schemadiff: skip keys with expressions in Online DDL a…
Browse files Browse the repository at this point in the history
…nalysis (#17475) (#17480)

Signed-off-by: Shlomi Noach <[email protected]>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
  • Loading branch information
vitess-bot[bot] authored Jan 8, 2025
1 parent 47883f1 commit f527ed7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
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 @@ -873,6 +873,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 @@ -2546,6 +2558,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

0 comments on commit f527ed7

Please sign in to comment.