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

sqlparser: normalize IndexInfo #14177

Merged
merged 4 commits into from
Oct 5, 2023
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
2 changes: 1 addition & 1 deletion go/test/endtoend/vreplication/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func confirmTablesHaveSecondaryKeys(t *testing.T, tablets []*cluster.VttabletPro
require.NotNil(t, createTable)
require.NotNil(t, createTable.GetTableSpec())
for _, index := range createTable.GetTableSpec().Indexes {
if !index.Info.Primary {
if index.Info.Type != sqlparser.IndexTypePrimary {
secondaryKeys++
}
}
Expand Down
12 changes: 6 additions & 6 deletions go/vt/schemadiff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,10 +477,10 @@ func TestDiffSchemas(t *testing.T) {
from: "create table t1 (id mediumint unsigned NOT NULL, deleted_at timestamp, primary key (id), unique key deleted_check (id, (if((deleted_at is null),0,NULL))))",
to: "create table t1 (id mediumint unsigned NOT NULL, deleted_at timestamp, primary key (id), unique key deleted_check (id, (if((deleted_at is not null),0,NULL))))",
diffs: []string{
"alter table t1 drop key deleted_check, add unique key deleted_check (id, (if(deleted_at is not null, 0, null)))",
"alter table t1 drop key deleted_check, add unique index deleted_check (id, (if(deleted_at is not null, 0, null)))",
},
cdiffs: []string{
"ALTER TABLE `t1` DROP KEY `deleted_check`, ADD UNIQUE KEY `deleted_check` (`id`, (if(`deleted_at` IS NOT NULL, 0, NULL)))",
"ALTER TABLE `t1` DROP KEY `deleted_check`, ADD UNIQUE INDEX `deleted_check` (`id`, (if(`deleted_at` IS NOT NULL, 0, NULL)))",
},
},
{
Expand Down Expand Up @@ -656,13 +656,13 @@ func TestDiffSchemas(t *testing.T) {
to: "create table t7(id int primary key); create table t5 (id int primary key, i int, constraint f5 foreign key (i) references t7(id)); create table t4 (id int primary key, i int, constraint f4 foreign key (i) references t7(id));",
diffs: []string{
"create table t7 (\n\tid int,\n\tprimary key (id)\n)",
"create table t4 (\n\tid int,\n\ti int,\n\tprimary key (id),\n\tkey f4 (i),\n\tconstraint f4 foreign key (i) references t7 (id)\n)",
"create table t5 (\n\tid int,\n\ti int,\n\tprimary key (id),\n\tkey f5 (i),\n\tconstraint f5 foreign key (i) references t7 (id)\n)",
"create table t4 (\n\tid int,\n\ti int,\n\tprimary key (id),\n\tindex f4 (i),\n\tconstraint f4 foreign key (i) references t7 (id)\n)",
"create table t5 (\n\tid int,\n\ti int,\n\tprimary key (id),\n\tindex f5 (i),\n\tconstraint f5 foreign key (i) references t7 (id)\n)",
},
cdiffs: []string{
"CREATE TABLE `t7` (\n\t`id` int,\n\tPRIMARY KEY (`id`)\n)",
"CREATE TABLE `t4` (\n\t`id` int,\n\t`i` int,\n\tPRIMARY KEY (`id`),\n\tKEY `f4` (`i`),\n\tCONSTRAINT `f4` FOREIGN KEY (`i`) REFERENCES `t7` (`id`)\n)",
"CREATE TABLE `t5` (\n\t`id` int,\n\t`i` int,\n\tPRIMARY KEY (`id`),\n\tKEY `f5` (`i`),\n\tCONSTRAINT `f5` FOREIGN KEY (`i`) REFERENCES `t7` (`id`)\n)",
"CREATE TABLE `t4` (\n\t`id` int,\n\t`i` int,\n\tPRIMARY KEY (`id`),\n\tINDEX `f4` (`i`),\n\tCONSTRAINT `f4` FOREIGN KEY (`i`) REFERENCES `t7` (`id`)\n)",
"CREATE TABLE `t5` (\n\t`id` int,\n\t`i` int,\n\tPRIMARY KEY (`id`),\n\tINDEX `f5` (`i`),\n\tCONSTRAINT `f5` FOREIGN KEY (`i`) REFERENCES `t7` (`id`)\n)",
},
},
{
Expand Down
24 changes: 7 additions & 17 deletions go/vt/schemadiff/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,6 @@ func (c *CreateTableEntity) normalizeColumnOptions() {

func (c *CreateTableEntity) normalizeIndexOptions() {
for _, idx := range c.CreateTable.TableSpec.Indexes {
// This name is taking straight from the input string
// so we want to normalize this to always lowercase.
idx.Info.Type = strings.ToLower(idx.Info.Type)
for _, opt := range idx.Options {
opt.Name = strings.ToLower(opt.Name)
opt.String = strings.ToLower(opt.String)
Expand Down Expand Up @@ -617,10 +614,8 @@ func (c *CreateTableEntity) normalizePartitionOptions() {
func newPrimaryKeyIndexDefinitionSingleColumn(name sqlparser.IdentifierCI) *sqlparser.IndexDefinition {
index := &sqlparser.IndexDefinition{
Info: &sqlparser.IndexInfo{
Name: sqlparser.NewIdentifierCI("PRIMARY"),
Type: "PRIMARY KEY",
Primary: true,
Unique: true,
Name: sqlparser.NewIdentifierCI("PRIMARY"),
Type: sqlparser.IndexTypePrimary,
},
Columns: []*sqlparser.IndexColumn{{Column: name}},
}
Expand Down Expand Up @@ -653,10 +648,6 @@ func (c *CreateTableEntity) normalizeKeys() {
}
}
for _, key := range c.CreateTable.TableSpec.Indexes {
// Normalize to KEY which matches MySQL behavior for the type.
if key.Info.Type == sqlparser.KeywordString(sqlparser.INDEX) {
key.Info.Type = sqlparser.KeywordString(sqlparser.KEY)
}
// now, let's look at keys that do not have names, and assign them new names
if name := key.Info.Name.String(); name == "" {
// we know there must be at least one column covered by this key
Expand Down Expand Up @@ -747,7 +738,6 @@ func (c *CreateTableEntity) normalizeForeignKeyIndexes() {
// - or, a standard auto-generated index name, if the constraint name is not provided
indexDefinition := &sqlparser.IndexDefinition{
Info: &sqlparser.IndexInfo{
Type: "key",
Name: constraint.Name, // if name is empty, then the name is later auto populated
},
}
Expand Down Expand Up @@ -1366,7 +1356,7 @@ func (c *CreateTableEntity) diffKeys(alterTable *sqlparser.AlterTable,

dropKeyStatement := func(info *sqlparser.IndexInfo) *sqlparser.DropKey {
dropKey := &sqlparser.DropKey{}
if strings.EqualFold(info.Type, sqlparser.PrimaryKeyTypeStr) {
if info.Type == sqlparser.IndexTypePrimary {
dropKey.Type = sqlparser.PrimaryKeyType
} else {
dropKey.Type = sqlparser.NormalKeyType
Expand Down Expand Up @@ -1417,7 +1407,7 @@ func (c *CreateTableEntity) diffKeys(alterTable *sqlparser.AlterTable,
IndexDefinition: t2Key,
}
addedAsSuperfluousStatement := false
if t2Key.Info.Fulltext {
if t2Key.Info.Type == sqlparser.IndexTypeFullText {
if addedFulltextKeys > 0 && hints.FullTextKeyStrategy == FullTextKeyDistinctStatements {
// Special case: MySQL does not support multiple ADD FULLTEXT KEY statements in a single ALTER
superfluousFulltextKeys = append(superfluousFulltextKeys, addKey)
Expand Down Expand Up @@ -1703,7 +1693,7 @@ func heuristicallyDetectColumnRenames(
// a PRIMARY KEY
func (c *CreateTableEntity) primaryKeyColumns() []*sqlparser.IndexColumn {
for _, existingIndex := range c.CreateTable.TableSpec.Indexes {
if existingIndex.Info.Primary {
if existingIndex.Info.Type == sqlparser.IndexTypePrimary {
return existingIndex.Columns
}
}
Expand Down Expand Up @@ -1874,7 +1864,7 @@ func (c *CreateTableEntity) apply(diff *AlterTableEntityDiff) error {
switch opt.Type {
case sqlparser.PrimaryKeyType:
for i, idx := range c.TableSpec.Indexes {
if strings.EqualFold(idx.Info.Type, sqlparser.PrimaryKeyTypeStr) {
if idx.Info.Type == sqlparser.IndexTypePrimary {
found = true
c.TableSpec.Indexes = append(c.TableSpec.Indexes[0:i], c.TableSpec.Indexes[i+1:]...)
break
Expand Down Expand Up @@ -2435,7 +2425,7 @@ func (c *CreateTableEntity) validate() error {

// Validate all unique keys include this column:
for _, key := range c.CreateTable.TableSpec.Indexes {
if !key.Info.Unique {
if !key.Info.IsUnique() {
continue
}
colFound := false
Expand Down
Loading
Loading