diff --git a/go/test/endtoend/vreplication/helper_test.go b/go/test/endtoend/vreplication/helper_test.go index 09785aafe6c..cff23e45ede 100644 --- a/go/test/endtoend/vreplication/helper_test.go +++ b/go/test/endtoend/vreplication/helper_test.go @@ -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++ } } diff --git a/go/vt/schemadiff/diff_test.go b/go/vt/schemadiff/diff_test.go index 2f8d913f042..291049a22ad 100644 --- a/go/vt/schemadiff/diff_test.go +++ b/go/vt/schemadiff/diff_test.go @@ -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)))", }, }, { @@ -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)", }, }, { diff --git a/go/vt/schemadiff/table.go b/go/vt/schemadiff/table.go index dbc01ec315c..fee70c96e88 100644 --- a/go/vt/schemadiff/table.go +++ b/go/vt/schemadiff/table.go @@ -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) @@ -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}}, } @@ -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 @@ -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 }, } @@ -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 @@ -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) @@ -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 } } @@ -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 @@ -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 diff --git a/go/vt/schemadiff/table_test.go b/go/vt/schemadiff/table_test.go index 633fdc9a5d6..bb4ad0571da 100644 --- a/go/vt/schemadiff/table_test.go +++ b/go/vt/schemadiff/table_test.go @@ -168,8 +168,8 @@ func TestCreateTableDiff(t *testing.T) { from: "create table t1 (id int primary key, i1 int not null, c char(3) default '')", to: "create table t2 (id int primary key, i2 int not null, c char(3) default '', key i2_idx(i2))", colrename: ColumnRenameHeuristicStatement, - diff: "alter table t1 rename column i1 to i2, add key i2_idx (i2)", - cdiff: "ALTER TABLE `t1` RENAME COLUMN `i1` TO `i2`, ADD KEY `i2_idx` (`i2`)", + diff: "alter table t1 rename column i1 to i2, add index i2_idx (i2)", + cdiff: "ALTER TABLE `t1` RENAME COLUMN `i1` TO `i2`, ADD INDEX `i2_idx` (`i2`)", }, { // in a future iteration, this will generate a RENAME for both column, like in the previous test. Until then, we do not RENAME two successive columns @@ -305,36 +305,36 @@ func TestCreateTableDiff(t *testing.T) { name: "added key", from: "create table t1 (`id` int primary key, i int)", to: "create table t2 (id int primary key, `i` int, key `i_idx` (i))", - diff: "alter table t1 add key i_idx (i)", - cdiff: "ALTER TABLE `t1` ADD KEY `i_idx` (`i`)", + diff: "alter table t1 add index i_idx (i)", + cdiff: "ALTER TABLE `t1` ADD INDEX `i_idx` (`i`)", }, { name: "added key without name", from: "create table t1 (`id` int primary key, i int)", to: "create table t2 (id int primary key, `i` int, key (i))", - diff: "alter table t1 add key i (i)", - cdiff: "ALTER TABLE `t1` ADD KEY `i` (`i`)", + diff: "alter table t1 add index i (i)", + cdiff: "ALTER TABLE `t1` ADD INDEX `i` (`i`)", }, { name: "added key without name, conflicting name", from: "create table t1 (`id` int primary key, i int, key i(i))", to: "create table t2 (id int primary key, `i` int, key i(i), key (i))", - diff: "alter table t1 add key i_2 (i)", - cdiff: "ALTER TABLE `t1` ADD KEY `i_2` (`i`)", + diff: "alter table t1 add index i_2 (i)", + cdiff: "ALTER TABLE `t1` ADD INDEX `i_2` (`i`)", }, { name: "added key without name, conflicting name 2", from: "create table t1 (`id` int primary key, i int, key i(i), key i_2(i))", to: "create table t2 (id int primary key, `i` int, key i(i), key i_2(i), key (i))", - diff: "alter table t1 add key i_3 (i)", - cdiff: "ALTER TABLE `t1` ADD KEY `i_3` (`i`)", + diff: "alter table t1 add index i_3 (i)", + cdiff: "ALTER TABLE `t1` ADD INDEX `i_3` (`i`)", }, { name: "added column and key", from: "create table t1 (`id` int primary key)", to: "create table t2 (id int primary key, `i` int, key `i_idx` (i))", - diff: "alter table t1 add column i int, add key i_idx (i)", - cdiff: "ALTER TABLE `t1` ADD COLUMN `i` int, ADD KEY `i_idx` (`i`)", + diff: "alter table t1 add column i int, add index i_idx (i)", + cdiff: "ALTER TABLE `t1` ADD COLUMN `i` int, ADD INDEX `i_idx` (`i`)", }, { name: "modify column primary key", @@ -375,8 +375,8 @@ func TestCreateTableDiff(t *testing.T) { name: "modified key", from: "create table t1 (`id` int primary key, i int, key i_idx(i))", to: "create table t2 (`id` int primary key, i int, key i_idx(i, id))", - diff: "alter table t1 drop key i_idx, add key i_idx (i, id)", - cdiff: "ALTER TABLE `t1` DROP KEY `i_idx`, ADD KEY `i_idx` (`i`, `id`)", + diff: "alter table t1 drop key i_idx, add index i_idx (i, id)", + cdiff: "ALTER TABLE `t1` DROP KEY `i_idx`, ADD INDEX `i_idx` (`i`, `id`)", }, { name: "modified primary key", @@ -420,11 +420,11 @@ func TestCreateTableDiff(t *testing.T) { to: "CREATE TABLE `pets` (`id` int, `name` VARCHAR(255), `login` VARCHAR(255), PRIMARY KEY (`id`), KEY (`name`), KEY (`login`), KEY login (login, name) )", }, { - name: "reordered key, add key", + name: "reordered key, add index", from: "create table t1 (`id` int primary key, i int, key i_idx(i), key i2_idx(i, `id`))", to: "create table t2 (`id` int primary key, i int, key i2_idx (`i`, id), key i_idx3(id), key i_idx ( i ) )", - diff: "alter table t1 add key i_idx3 (id)", - cdiff: "ALTER TABLE `t1` ADD KEY `i_idx3` (`id`)", + diff: "alter table t1 add index i_idx3 (id)", + cdiff: "ALTER TABLE `t1` ADD INDEX `i_idx3` (`id`)", }, { name: "key made visible", @@ -452,37 +452,37 @@ func TestCreateTableDiff(t *testing.T) { name: "add one fulltext key", from: "create table t1 (id int primary key, name tinytext not null)", to: "create table t1 (id int primary key, name tinytext not null, fulltext key name_ft(name))", - diff: "alter table t1 add fulltext key name_ft (`name`)", - cdiff: "ALTER TABLE `t1` ADD FULLTEXT KEY `name_ft` (`name`)", + diff: "alter table t1 add fulltext index name_ft (`name`)", + cdiff: "ALTER TABLE `t1` ADD FULLTEXT INDEX `name_ft` (`name`)", }, { name: "add one fulltext key with explicit parser", from: "create table t1 (id int primary key, name tinytext not null)", to: "create table t1 (id int primary key, name tinytext not null, fulltext key name_ft(name) with parser ngram)", - diff: "alter table t1 add fulltext key name_ft (`name`) with parser ngram", - cdiff: "ALTER TABLE `t1` ADD FULLTEXT KEY `name_ft` (`name`) WITH PARSER ngram", + diff: "alter table t1 add fulltext index name_ft (`name`) with parser ngram", + cdiff: "ALTER TABLE `t1` ADD FULLTEXT INDEX `name_ft` (`name`) WITH PARSER ngram", }, { name: "add one fulltext key and one normal key", from: "create table t1 (id int primary key, name tinytext not null)", to: "create table t1 (id int primary key, name tinytext not null, key name_idx(name(32)), fulltext key name_ft(name))", - diff: "alter table t1 add key name_idx (`name`(32)), add fulltext key name_ft (`name`)", - cdiff: "ALTER TABLE `t1` ADD KEY `name_idx` (`name`(32)), ADD FULLTEXT KEY `name_ft` (`name`)", + diff: "alter table t1 add index name_idx (`name`(32)), add fulltext index name_ft (`name`)", + cdiff: "ALTER TABLE `t1` ADD INDEX `name_idx` (`name`(32)), ADD FULLTEXT INDEX `name_ft` (`name`)", }, { name: "add two fulltext keys, distinct statements", from: "create table t1 (id int primary key, name1 tinytext not null, name2 tinytext not null)", to: "create table t1 (id int primary key, name1 tinytext not null, name2 tinytext not null, fulltext key name1_ft(name1), fulltext key name2_ft(name2))", - diffs: []string{"alter table t1 add fulltext key name1_ft (name1)", "alter table t1 add fulltext key name2_ft (name2)"}, - cdiffs: []string{"ALTER TABLE `t1` ADD FULLTEXT KEY `name1_ft` (`name1`)", "ALTER TABLE `t1` ADD FULLTEXT KEY `name2_ft` (`name2`)"}, + diffs: []string{"alter table t1 add fulltext index name1_ft (name1)", "alter table t1 add fulltext index name2_ft (name2)"}, + cdiffs: []string{"ALTER TABLE `t1` ADD FULLTEXT INDEX `name1_ft` (`name1`)", "ALTER TABLE `t1` ADD FULLTEXT INDEX `name2_ft` (`name2`)"}, }, { name: "add two fulltext keys, unify statements", from: "create table t1 (id int primary key, name1 tinytext not null, name2 tinytext not null)", to: "create table t1 (id int primary key, name1 tinytext not null, name2 tinytext not null, fulltext key name1_ft(name1), fulltext key name2_ft(name2))", fulltext: FullTextKeyUnifyStatements, - diff: "alter table t1 add fulltext key name1_ft (name1), add fulltext key name2_ft (name2)", - cdiff: "ALTER TABLE `t1` ADD FULLTEXT KEY `name1_ft` (`name1`), ADD FULLTEXT KEY `name2_ft` (`name2`)", + diff: "alter table t1 add fulltext index name1_ft (name1), add fulltext index name2_ft (name2)", + cdiff: "ALTER TABLE `t1` ADD FULLTEXT INDEX `name1_ft` (`name1`), ADD FULLTEXT INDEX `name2_ft` (`name2`)", }, { name: "no fulltext diff", @@ -650,8 +650,8 @@ func TestCreateTableDiff(t *testing.T) { name: "add foreign key and index", from: "create table t1 (id int primary key, i int)", to: "create table t2 (id int primary key, i int, key ix(i), constraint f foreign key (i) references parent(id))", - diff: "alter table t1 add key ix (i), add constraint f foreign key (i) references parent (id)", - cdiff: "ALTER TABLE `t1` ADD KEY `ix` (`i`), ADD CONSTRAINT `f` FOREIGN KEY (`i`) REFERENCES `parent` (`id`)", + diff: "alter table t1 add index ix (i), add constraint f foreign key (i) references parent (id)", + cdiff: "ALTER TABLE `t1` ADD INDEX `ix` (`i`), ADD CONSTRAINT `f` FOREIGN KEY (`i`) REFERENCES `parent` (`id`)", }, { name: "identical foreign key", @@ -1168,8 +1168,8 @@ func TestCreateTableDiff(t *testing.T) { properties json NOT NULL, KEY index_on_company_id ((cast(json_unquote(json_extract(properties,_utf8mb4'$.company_id')) as signed))) )`, - diff: "alter table t4 add key index_on_company_id ((cast(json_unquote(json_extract(properties, _utf8mb4 '$.company_id')) as signed)))", - cdiff: "ALTER TABLE `t4` ADD KEY `index_on_company_id` ((CAST(JSON_UNQUOTE(JSON_EXTRACT(`properties`, _utf8mb4 '$.company_id')) AS signed)))", + diff: "alter table t4 add index index_on_company_id ((cast(json_unquote(json_extract(properties, _utf8mb4 '$.company_id')) as signed)))", + cdiff: "ALTER TABLE `t4` ADD INDEX `index_on_company_id` ((CAST(JSON_UNQUOTE(JSON_EXTRACT(`properties`, _utf8mb4 '$.company_id')) AS signed)))", }, { // validates that CanonicalString prints 'interval 30 minute' and not ' INTERVAL 30 MINUTE', as MySQL's `SHOW CREATE TABLE` outputs lower case 'interval 30 minute' @@ -1342,9 +1342,9 @@ func TestValidate(t *testing.T) { }, // keys { - name: "add key", + name: "add index", from: "create table t (id int primary key, i int)", - alter: "alter table t add key i_idx(i)", + alter: "alter table t add index i_idx(i)", to: "create table t (id int primary key, i int, key i_idx(i))", }, { @@ -1408,27 +1408,27 @@ func TestValidate(t *testing.T) { expectErr: &DuplicateKeyNameError{Table: "t", Key: "PRIMARY"}, }, { - name: "add key, column case", + name: "add index, column case", from: "create table t (id int primary key, i int)", - alter: "alter table t add key i_idx(I)", + alter: "alter table t add index i_idx(I)", to: "create table t (id int primary key, i int, key i_idx(I))", }, { name: "add column and key", from: "create table t (id int primary key)", - alter: "alter table t add column i int, add key i_idx(i)", + alter: "alter table t add column i int, add index i_idx(i)", to: "create table t (id int primary key, i int, key i_idx(i))", }, { - name: "add key, missing column", + name: "add index, missing column", from: "create table t (id int primary key, i int)", - alter: "alter table t add key j_idx(j)", + alter: "alter table t add index j_idx(j)", expectErr: &InvalidColumnInKeyError{Table: "t", Column: "j", Key: "j_idx"}, }, { - name: "add key, missing column 2", + name: "add index, missing column 2", from: "create table t (id int primary key, i int)", - alter: "alter table t add key j_idx(j, i)", + alter: "alter table t add index j_idx(j, i)", expectErr: &InvalidColumnInKeyError{Table: "t", Column: "j", Key: "j_idx"}, }, { @@ -1488,13 +1488,13 @@ func TestValidate(t *testing.T) { { name: "add multiple keys, multi columns, ok", from: "create table t (id int primary key, i1 int, i2 int, i3 int)", - alter: "alter table t add key i12_idx(i1, i2), add key i32_idx(i3, i2), add key i21_idx(i2, i1)", + alter: "alter table t add index i12_idx(i1, i2), add index i32_idx(i3, i2), add index i21_idx(i2, i1)", to: "create table t (id int primary key, i1 int, i2 int, i3 int, key i12_idx(i1, i2), key i32_idx(i3, i2), key i21_idx(i2, i1))", }, { name: "add multiple keys, multi columns, missing column", from: "create table t (id int primary key, i1 int, i2 int, i4 int)", - alter: "alter table t add key i12_idx(i1, i2), add key i32_idx(i3, i2), add key i21_idx(i2, i1)", + alter: "alter table t add index i12_idx(i1, i2), add index i32_idx(i3, i2), add index i21_idx(i2, i1)", expectErr: &InvalidColumnInKeyError{Table: "t", Column: "i3", Key: "i32_idx"}, }, { @@ -1543,7 +1543,7 @@ func TestValidate(t *testing.T) { { name: "add multiple keys, multi columns, missing column", from: "create table t (id int primary key, i1 int, i2 int, i4 int)", - alter: "alter table t add key i12_idx(i1, i2), add key i32_idx((IF(i3 IS NULL, i2, i3)), i2), add key i21_idx(i2, i1)", + alter: "alter table t add index i12_idx(i1, i2), add index i32_idx((IF(i3 IS NULL, i2, i3)), i2), add index i21_idx(i2, i1)", expectErr: &InvalidColumnInKeyError{Table: "t", Column: "i3", Key: "i32_idx"}, }, // data types @@ -1764,13 +1764,13 @@ func TestValidate(t *testing.T) { { name: "add foreign key and index, no implicit index", from: "create table t (id int primary key, i int)", - alter: "alter table t add key i_idx (i), add constraint f foreign key (i) references parent(id)", + alter: "alter table t add index i_idx (i), add constraint f foreign key (i) references parent(id)", to: "create table t (id int primary key, i int, key i_idx (i), constraint f foreign key (i) references parent(id))", }, { name: "add foreign key and extended index, no implicit index", from: "create table t (id int primary key, i int)", - alter: "alter table t add key i_id_idx (i, id), add constraint f foreign key (i) references parent(id)", + alter: "alter table t add index i_id_idx (i, id), add constraint f foreign key (i) references parent(id)", to: "create table t (id int primary key, i int, key i_id_idx (i, id), constraint f foreign key (i) references parent(id))", }, { @@ -2035,7 +2035,7 @@ func TestNormalize(t *testing.T) { { name: "generates a name for a key with proper casing", from: "create table t (id int, I int, index i (i), index(I))", - to: "CREATE TABLE `t` (\n\t`id` int,\n\t`I` int,\n\tKEY `i` (`i`),\n\tKEY `I_2` (`I`)\n)", + to: "CREATE TABLE `t` (\n\t`id` int,\n\t`I` int,\n\tINDEX `i` (`i`),\n\tINDEX `I_2` (`I`)\n)", }, { name: "generates a name for checks", @@ -2050,47 +2050,47 @@ func TestNormalize(t *testing.T) { { name: "generates a name for foreign key constraints", from: "create table t1 (id int primary key, i int, key i_idx (i), foreign key (i) references parent(id))", - to: "CREATE TABLE `t1` (\n\t`id` int,\n\t`i` int,\n\tPRIMARY KEY (`id`),\n\tKEY `i_idx` (`i`),\n\tCONSTRAINT `t1_ibfk_1` FOREIGN KEY (`i`) REFERENCES `parent` (`id`)\n)", + to: "CREATE TABLE `t1` (\n\t`id` int,\n\t`i` int,\n\tPRIMARY KEY (`id`),\n\tINDEX `i_idx` (`i`),\n\tCONSTRAINT `t1_ibfk_1` FOREIGN KEY (`i`) REFERENCES `parent` (`id`)\n)", }, { name: "creates an index for foreign key constraints", from: "create table t1 (id int primary key, i int, constraint f foreign key (i) references parent(id))", - to: "CREATE TABLE `t1` (\n\t`id` int,\n\t`i` int,\n\tPRIMARY KEY (`id`),\n\tKEY `f` (`i`),\n\tCONSTRAINT `f` FOREIGN KEY (`i`) REFERENCES `parent` (`id`)\n)", + to: "CREATE TABLE `t1` (\n\t`id` int,\n\t`i` int,\n\tPRIMARY KEY (`id`),\n\tINDEX `f` (`i`),\n\tCONSTRAINT `f` FOREIGN KEY (`i`) REFERENCES `parent` (`id`)\n)", }, { name: "creates an index for unnamed foreign key constraints", from: "create table t1 (id int primary key, i int, foreign key (i) references parent(id))", - to: "CREATE TABLE `t1` (\n\t`id` int,\n\t`i` int,\n\tPRIMARY KEY (`id`),\n\tKEY `i` (`i`),\n\tCONSTRAINT `t1_ibfk_1` FOREIGN KEY (`i`) REFERENCES `parent` (`id`)\n)", + to: "CREATE TABLE `t1` (\n\t`id` int,\n\t`i` int,\n\tPRIMARY KEY (`id`),\n\tINDEX `i` (`i`),\n\tCONSTRAINT `t1_ibfk_1` FOREIGN KEY (`i`) REFERENCES `parent` (`id`)\n)", }, { name: "does not add index since one already defined for foreign key constraint", from: "create table t1 (id int primary key, i int, key i_idx (i), foreign key (i) references parent(id))", - to: "CREATE TABLE `t1` (\n\t`id` int,\n\t`i` int,\n\tPRIMARY KEY (`id`),\n\tKEY `i_idx` (`i`),\n\tCONSTRAINT `t1_ibfk_1` FOREIGN KEY (`i`) REFERENCES `parent` (`id`)\n)", + to: "CREATE TABLE `t1` (\n\t`id` int,\n\t`i` int,\n\tPRIMARY KEY (`id`),\n\tINDEX `i_idx` (`i`),\n\tCONSTRAINT `t1_ibfk_1` FOREIGN KEY (`i`) REFERENCES `parent` (`id`)\n)", }, { name: "uses KEY for indexes", from: "create table t (id int primary key, i1 int, index i1_idx(i1))", - to: "CREATE TABLE `t` (\n\t`id` int,\n\t`i1` int,\n\tPRIMARY KEY (`id`),\n\tKEY `i1_idx` (`i1`)\n)", + to: "CREATE TABLE `t` (\n\t`id` int,\n\t`i1` int,\n\tPRIMARY KEY (`id`),\n\tINDEX `i1_idx` (`i1`)\n)", }, { name: "drops default index type", from: "create table t (id int primary key, i1 int, key i1_idx(i1) using btree)", - to: "CREATE TABLE `t` (\n\t`id` int,\n\t`i1` int,\n\tPRIMARY KEY (`id`),\n\tKEY `i1_idx` (`i1`)\n)", + to: "CREATE TABLE `t` (\n\t`id` int,\n\t`i1` int,\n\tPRIMARY KEY (`id`),\n\tINDEX `i1_idx` (`i1`)\n)", }, { name: "does not drop non-default index type", from: "create table t (id int primary key, i1 int, key i1_idx(i1) using hash)", - to: "CREATE TABLE `t` (\n\t`id` int,\n\t`i1` int,\n\tPRIMARY KEY (`id`),\n\tKEY `i1_idx` (`i1`) USING hash\n)", + to: "CREATE TABLE `t` (\n\t`id` int,\n\t`i1` int,\n\tPRIMARY KEY (`id`),\n\tINDEX `i1_idx` (`i1`) USING hash\n)", }, { name: "drops default index visibility", from: "create table t (id int primary key, i1 int, key i1_idx(i1) visible)", - to: "CREATE TABLE `t` (\n\t`id` int,\n\t`i1` int,\n\tPRIMARY KEY (`id`),\n\tKEY `i1_idx` (`i1`)\n)", + to: "CREATE TABLE `t` (\n\t`id` int,\n\t`i1` int,\n\tPRIMARY KEY (`id`),\n\tINDEX `i1_idx` (`i1`)\n)", }, { name: "drops non-default index visibility", from: "create table t (id int primary key, i1 int, key i1_idx(i1) invisible)", - to: "CREATE TABLE `t` (\n\t`id` int,\n\t`i1` int,\n\tPRIMARY KEY (`id`),\n\tKEY `i1_idx` (`i1`) INVISIBLE\n)", + to: "CREATE TABLE `t` (\n\t`id` int,\n\t`i1` int,\n\tPRIMARY KEY (`id`),\n\tINDEX `i1_idx` (`i1`) INVISIBLE\n)", }, { name: "drops default column visibility", diff --git a/go/vt/sqlparser/ast.go b/go/vt/sqlparser/ast.go index f57943cda18..46172e4de63 100644 --- a/go/vt/sqlparser/ast.go +++ b/go/vt/sqlparser/ast.go @@ -706,6 +706,9 @@ type ( Type KillType ProcesslistID uint64 } + + // IndexType is the type of index in a DDL statement + IndexType int8 ) func (*Union) iStatement() {} @@ -1885,13 +1888,18 @@ type IndexDefinition struct { // IndexInfo describes the name and type of an index in a CREATE TABLE statement type IndexInfo struct { - Type string + Type IndexType Name IdentifierCI ConstraintName IdentifierCI - Primary bool - Spatial bool - Fulltext bool - Unique bool +} + +func (ii *IndexInfo) IsUnique() bool { + switch ii.Type { + case IndexTypePrimary, IndexTypeUnique: + return true + default: + return false + } } // VindexSpec defines a vindex for a CREATE VINDEX or DROP VINDEX statement diff --git a/go/vt/sqlparser/ast_equals.go b/go/vt/sqlparser/ast_equals.go index 953947ba765..7c39f2c7a83 100644 --- a/go/vt/sqlparser/ast_equals.go +++ b/go/vt/sqlparser/ast_equals.go @@ -2854,10 +2854,6 @@ func (cmp *Comparator) RefOfIndexInfo(a, b *IndexInfo) bool { return false } return a.Type == b.Type && - a.Primary == b.Primary && - a.Spatial == b.Spatial && - a.Fulltext == b.Fulltext && - a.Unique == b.Unique && cmp.IdentifierCI(a.Name, b.Name) && cmp.IdentifierCI(a.ConstraintName, b.ConstraintName) } diff --git a/go/vt/sqlparser/ast_format.go b/go/vt/sqlparser/ast_format.go index 02dd037985f..b594efd8eaf 100644 --- a/go/vt/sqlparser/ast_format.go +++ b/go/vt/sqlparser/ast_format.go @@ -831,13 +831,21 @@ func (ii *IndexInfo) Format(buf *TrackedBuffer) { if !ii.ConstraintName.IsEmpty() { buf.astPrintf(ii, "constraint %v ", ii.ConstraintName) } - if ii.Primary { - buf.astPrintf(ii, "%s", ii.Type) - } else { - buf.astPrintf(ii, "%s", ii.Type) - if !ii.Name.IsEmpty() { - buf.astPrintf(ii, " %v", ii.Name) - } + switch ii.Type { + case IndexTypePrimary: + buf.astPrintf(ii, "%s %s", keywordStrings[PRIMARY], keywordStrings[KEY]) + return + case IndexTypeDefault: + buf.astPrintf(ii, "%s", keywordStrings[INDEX]) + case IndexTypeUnique: + buf.astPrintf(ii, "%s %s", keywordStrings[UNIQUE], keywordStrings[INDEX]) + case IndexTypeSpatial: + buf.astPrintf(ii, "%s %s", keywordStrings[SPATIAL], keywordStrings[INDEX]) + case IndexTypeFullText: + buf.astPrintf(ii, "%s %s", keywordStrings[FULLTEXT], keywordStrings[INDEX]) + } + if !ii.Name.IsEmpty() { + buf.astPrintf(ii, " %v", ii.Name) } } diff --git a/go/vt/sqlparser/ast_format_fast.go b/go/vt/sqlparser/ast_format_fast.go index a3fcc81f937..c400580a259 100644 --- a/go/vt/sqlparser/ast_format_fast.go +++ b/go/vt/sqlparser/ast_format_fast.go @@ -1130,14 +1130,30 @@ func (ii *IndexInfo) formatFast(buf *TrackedBuffer) { ii.ConstraintName.formatFast(buf) buf.WriteByte(' ') } - if ii.Primary { - buf.WriteString(ii.Type) - } else { - buf.WriteString(ii.Type) - if !ii.Name.IsEmpty() { - buf.WriteByte(' ') - ii.Name.formatFast(buf) - } + switch ii.Type { + case IndexTypePrimary: + buf.WriteString(keywordStrings[PRIMARY]) + buf.WriteByte(' ') + buf.WriteString(keywordStrings[KEY]) + return + case IndexTypeDefault: + buf.WriteString(keywordStrings[INDEX]) + case IndexTypeUnique: + buf.WriteString(keywordStrings[UNIQUE]) + buf.WriteByte(' ') + buf.WriteString(keywordStrings[INDEX]) + case IndexTypeSpatial: + buf.WriteString(keywordStrings[SPATIAL]) + buf.WriteByte(' ') + buf.WriteString(keywordStrings[INDEX]) + case IndexTypeFullText: + buf.WriteString(keywordStrings[FULLTEXT]) + buf.WriteByte(' ') + buf.WriteString(keywordStrings[INDEX]) + } + if !ii.Name.IsEmpty() { + buf.WriteByte(' ') + ii.Name.formatFast(buf) } } diff --git a/go/vt/sqlparser/cached_size.go b/go/vt/sqlparser/cached_size.go index 1f416ae0896..a38ac22854d 100644 --- a/go/vt/sqlparser/cached_size.go +++ b/go/vt/sqlparser/cached_size.go @@ -1776,10 +1776,8 @@ func (cached *IndexInfo) CachedSize(alloc bool) int64 { } size := int64(0) if alloc { - size += int64(96) + size += int64(80) } - // field Type string - size += hack.RuntimeAllocSize(int64(len(cached.Type))) // field Name vitess.io/vitess/go/vt/sqlparser.IdentifierCI size += cached.Name.CachedSize(false) // field ConstraintName vitess.io/vitess/go/vt/sqlparser.IdentifierCI diff --git a/go/vt/sqlparser/constants.go b/go/vt/sqlparser/constants.go index 1be3124aa24..3848c53f3e0 100644 --- a/go/vt/sqlparser/constants.go +++ b/go/vt/sqlparser/constants.go @@ -1062,3 +1062,11 @@ const ( ConnectionType KillType = iota QueryType ) + +const ( + IndexTypeDefault IndexType = iota + IndexTypePrimary + IndexTypeUnique + IndexTypeSpatial + IndexTypeFullText +) diff --git a/go/vt/sqlparser/parse_test.go b/go/vt/sqlparser/parse_test.go index d48f8c84140..e2d19d6c3e0 100644 --- a/go/vt/sqlparser/parse_test.go +++ b/go/vt/sqlparser/parse_test.go @@ -1527,13 +1527,17 @@ var ( }, { input: "alter table a alter index x visible, alter index x2 invisible", }, { - input: "alter table a add spatial key foo (column1)", + input: "alter table a add spatial key foo (column1)", + output: "alter table a add spatial index foo (column1)", }, { - input: "alter table a add fulltext key foo (column1), order by a, b, c", + input: "alter table a add fulltext key foo (column1), order by a, b, c", + output: "alter table a add fulltext index foo (column1), order by a, b, c", }, { - input: "alter table a add unique key foo (column1)", + input: "alter table a add unique key foo (column1)", + output: "alter table a add unique index foo (column1)", }, { - input: "alter /*vt+ strategy=online */ table a add unique key foo (column1)", + input: "alter /*vt+ strategy=online */ table a add unique key foo (column1)", + output: "alter /*vt+ strategy=online */ table a add unique index foo (column1)", }, { input: "alter table a change column s foo int default 1 after x", }, { @@ -1673,7 +1677,8 @@ var ( }, { input: "alter table a add constraint b primary key (id)", }, { - input: "alter table a add constraint b unique key (id)", + input: "alter table a add constraint b unique key (id)", + output: "alter table a add constraint b unique index (id)", }, { input: "alter table t add column iii int signed not null", output: "alter table t add column iii int not null", @@ -1681,7 +1686,7 @@ var ( input: "alter table t add column iii int unsigned not null", }, { input: "alter table a add constraint b unique c (id)", - output: "alter table a add constraint b unique key c (id)", + output: "alter table a add constraint b unique index c (id)", }, { input: "alter table a add constraint check (id)", output: "alter table a add check (id)", @@ -1828,7 +1833,7 @@ var ( output: "create table a (\n\tb1 bool not null primary key,\n\tb2 boolean not null\n)", }, { input: "create table a (b1 bool NOT NULL PRIMARY KEY, b2 boolean not null references b (a) on delete restrict, KEY b2_idx(b))", - output: "create table a (\n\tb1 bool not null primary key,\n\tb2 boolean not null references b (a) on delete restrict,\n\tKEY b2_idx (b)\n)", + output: "create table a (\n\tb1 bool not null primary key,\n\tb2 boolean not null references b (a) on delete restrict,\n\tindex b2_idx (b)\n)", }, { input: "create temporary table a (\n\tid bigint\n)", }, { @@ -4657,6 +4662,22 @@ func TestCreateTable(t *testing.T) { unique index by_username3 (username), index by_status (status_nonkeyword), key by_full_name (full_name) +)`, + output: `create table t ( + id int auto_increment, + username varchar, + email varchar, + full_name varchar, + geom point not null, + status_nonkeyword varchar, + primary key (id), + spatial index geom (geom), + fulltext index fts (full_name), + unique index by_username (username), + unique index by_username2 (username), + unique index by_username3 (username), + index by_status (status_nonkeyword), + index by_full_name (full_name) )`, }, // test defining index visibility @@ -4667,6 +4688,13 @@ func TestCreateTable(t *testing.T) { unique key by_username (username) visible, unique key by_username2 (username) invisible, unique index by_username3 (username) +)`, + output: `create table t ( + id int auto_increment, + username varchar, + unique index by_username (username) visible, + unique index by_username2 (username) invisible, + unique index by_username3 (username) )`, }, // test adding engine attributes @@ -4676,6 +4704,12 @@ func TestCreateTable(t *testing.T) { username varchar, unique key by_username (username) engine_attribute '{}' secondary_engine_attribute '{}', unique index by_username3 (username) +)`, + output: `create table t ( + id int auto_increment, + username varchar, + unique index by_username (username) engine_attribute '{}' secondary_engine_attribute '{}', + unique index by_username3 (username) )`, }, // test defining SRID @@ -4716,11 +4750,11 @@ func TestCreateTable(t *testing.T) { full_name varchar, status_nonkeyword varchar, primary key (id) using BTREE, - unique key by_username (username) using HASH, - unique key by_username2 (username) using OTHER, + unique index by_username (username) using HASH, + unique index by_username2 (username) using OTHER, unique index by_username3 (username) using XYZ, index by_status (status_nonkeyword) using PDQ, - key by_full_name (full_name) using OTHER + index by_full_name (full_name) using OTHER )`, }, // test other index options @@ -4730,7 +4764,7 @@ func TestCreateTable(t *testing.T) { username varchar, email varchar, primary key (id) comment 'hi', - unique key by_username (username) key_block_size 8, + unique index by_username (username) key_block_size 8, unique index by_username4 (username) comment 'hi' using BTREE, unique index by_username4 (username) using BTREE key_block_size 4 comment 'hi' )`, @@ -4756,7 +4790,8 @@ func TestCreateTable(t *testing.T) { )`, }, { - input: "create table t2 (\n\tid int not null,\n\textra tinyint(1) as (id = 1) stored,\n\tPRIMARY KEY (id)\n)", + input: "create table t2 (\n\tid int not null,\n\textra tinyint(1) as (id = 1) stored,\n\tPRIMARY KEY (id)\n)", + output: "create table t2 (\n\tid int not null,\n\textra tinyint(1) as (id = 1) stored,\n\tprimary key (id)\n)", }, // multi-column indexes { @@ -4772,6 +4807,19 @@ func TestCreateTable(t *testing.T) { unique key by_abc (a, b, c), unique key (a, b, c), key by_email (email(10), username) +)`, + output: `create table t ( + id int auto_increment, + username varchar, + email varchar, + full_name varchar, + a int, + b int, + c int, + primary key (id, username), + unique index by_abc (a, b, c), + unique index (a, b, c), + index by_email (email(10), username) )`, }, // geometrycollection & geomcollection alias @@ -4843,7 +4891,7 @@ func TestCreateTable(t *testing.T) { newCol int references t2 (a) on update no action, newCol int references t2 (a) on update cascade, primary key (id, username), - key by_email (email(10), username), + index by_email (email(10), username), constraint second_ibfk_1 foreign key (k, j) references t2 (a, b), constraint second_ibfk_1 foreign key (k, j) references t2 (a, b) on delete restrict, constraint second_ibfk_1 foreign key (k, j) references t2 (a, b) on delete no action, @@ -4866,7 +4914,7 @@ func TestCreateTable(t *testing.T) { id int(11) not null auto_increment, user_id int(11) not null, primary key (id), - unique key post_user_unique (user_id), + unique index post_user_unique (user_id), constraint ` + "`" + `Post With Space_ibfk_1` + "`" + ` foreign key (user_id) references ` + "`" + `User` + "`" + ` (id) ) ENGINE Innodb`, }, @@ -4926,9 +4974,9 @@ func TestCreateTable(t *testing.T) { output: `create table t ( id int auto_increment, username varchar, - unique key by_username (username) key_block_size 8, - unique key by_username2 (username) key_block_size 8, - unique key by_username3 (username) key_block_size 4 + unique index by_username (username) key_block_size 8, + unique index by_username2 (username) key_block_size 8, + unique index by_username3 (username) key_block_size 4 )`, }, { // test defaults @@ -5545,6 +5593,14 @@ partition by list (val) email varchar(64), primary key (id), key email_idx (email, (if(username = '', nickname, username))) +)`, + output: `create table t ( + id int auto_increment, + username varchar(64), + nickname varchar(64), + email varchar(64), + primary key (id), + index email_idx (email, (if(username = '', nickname, username))) )`, }, { @@ -5557,10 +5613,10 @@ partition by list (val) labels json default null, spec json default null, salaryInfo json default null, - PRIMARY KEY (namespace, uid), - UNIQUE KEY namespaced_name (namespace, place), - UNIQUE KEY unique_uid (uid), - KEY entries_spec_updatedAt ((json_value(spec, _utf8mb4 '$.updatedAt'))) + primary key (namespace, uid), + unique index namespaced_name (namespace, place), + unique index unique_uid (uid), + index entries_spec_updatedAt ((json_value(spec, _utf8mb4 '$.updatedAt'))) ) ENGINE InnoDB, CHARSET utf8mb4, COLLATE utf8mb4_bin`, @@ -5572,7 +5628,7 @@ partition by list (val) )`, output: `create table t1 ( j JSON, - INDEX i1 ((json_value(j, '$.id' returning UNSIGNED))) + index i1 ((json_value(j, '$.id' returning UNSIGNED))) )`, }, { input: `CREATE TABLE entries ( @@ -5598,10 +5654,10 @@ partition by list (val) labels json default null, spec json default null, salaryInfo json default null, - PRIMARY KEY (namespace, uid), - UNIQUE KEY namespaced_employee (namespace, employee), - UNIQUE KEY unique_uid (uid), - KEY entries_spec_updatedAt ((json_value(spec, _utf8mb4 '$.updatedAt' returning datetime))) + primary key (namespace, uid), + unique index namespaced_employee (namespace, employee), + unique index unique_uid (uid), + index entries_spec_updatedAt ((json_value(spec, _utf8mb4 '$.updatedAt' returning datetime))) ) ENGINE InnoDB, CHARSET utf8mb4, COLLATE utf8mb4_bin`, @@ -5668,7 +5724,7 @@ partition by range (YEAR(purchased)) subpartition by hash (TO_DAYS(purchased)) }, { input: "create table t (id int, info JSON, INDEX zips((CAST(info->'$.field' AS unsigned ARRAY))))", - output: "create table t (\n\tid int,\n\tinfo JSON,\n\tINDEX zips ((cast(info -> '$.field' as unsigned array)))\n)", + output: "create table t (\n\tid int,\n\tinfo JSON,\n\tindex zips ((cast(info -> '$.field' as unsigned array)))\n)", }, } for _, test := range createTableQueries { diff --git a/go/vt/sqlparser/sql.go b/go/vt/sqlparser/sql.go index ace47c392bc..da54c6cc34d 100644 --- a/go/vt/sqlparser/sql.go +++ b/go/vt/sqlparser/sql.go @@ -11063,7 +11063,7 @@ yydefault: var yyLOCAL *AlterTable //line sql.y:1250 { - yyLOCAL = &AlterTable{Table: yyDollar[7].tableName, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition: &IndexDefinition{Info: &IndexInfo{Name: yyDollar[4].identifierCI, Type: string(yyDollar[3].str)}, Options: yyDollar[5].indexOptionsUnion()}}}} + yyLOCAL = &AlterTable{Table: yyDollar[7].tableName, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition: &IndexDefinition{Info: &IndexInfo{Name: yyDollar[4].identifierCI}, Options: yyDollar[5].indexOptionsUnion()}}}} setDDL(yylex, yyLOCAL) } yyVAL.union = yyLOCAL @@ -11072,7 +11072,7 @@ yydefault: var yyLOCAL *AlterTable //line sql.y:1255 { - yyLOCAL = &AlterTable{Table: yyDollar[8].tableName, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition: &IndexDefinition{Info: &IndexInfo{Name: yyDollar[5].identifierCI, Type: string(yyDollar[3].str) + " " + string(yyDollar[4].str), Fulltext: true}, Options: yyDollar[6].indexOptionsUnion()}}}} + yyLOCAL = &AlterTable{Table: yyDollar[8].tableName, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition: &IndexDefinition{Info: &IndexInfo{Name: yyDollar[5].identifierCI, Type: IndexTypeFullText}, Options: yyDollar[6].indexOptionsUnion()}}}} setDDL(yylex, yyLOCAL) } yyVAL.union = yyLOCAL @@ -11081,7 +11081,7 @@ yydefault: var yyLOCAL *AlterTable //line sql.y:1260 { - yyLOCAL = &AlterTable{Table: yyDollar[8].tableName, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition: &IndexDefinition{Info: &IndexInfo{Name: yyDollar[5].identifierCI, Type: string(yyDollar[3].str) + " " + string(yyDollar[4].str), Spatial: true}, Options: yyDollar[6].indexOptionsUnion()}}}} + yyLOCAL = &AlterTable{Table: yyDollar[8].tableName, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition: &IndexDefinition{Info: &IndexInfo{Name: yyDollar[5].identifierCI, Type: IndexTypeSpatial}, Options: yyDollar[6].indexOptionsUnion()}}}} setDDL(yylex, yyLOCAL) } yyVAL.union = yyLOCAL @@ -11090,7 +11090,7 @@ yydefault: var yyLOCAL *AlterTable //line sql.y:1265 { - yyLOCAL = &AlterTable{Table: yyDollar[8].tableName, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition: &IndexDefinition{Info: &IndexInfo{Name: yyDollar[5].identifierCI, Type: string(yyDollar[3].str) + " " + string(yyDollar[4].str), Unique: true}, Options: yyDollar[6].indexOptionsUnion()}}}} + yyLOCAL = &AlterTable{Table: yyDollar[8].tableName, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition: &IndexDefinition{Info: &IndexInfo{Name: yyDollar[5].identifierCI, Type: IndexTypeUnique}, Options: yyDollar[6].indexOptionsUnion()}}}} setDDL(yylex, yyLOCAL) } yyVAL.union = yyLOCAL @@ -12819,7 +12819,7 @@ yydefault: var yyLOCAL *IndexInfo //line sql.y:2419 { - yyLOCAL = &IndexInfo{Type: string(yyDollar[2].str) + " " + string(yyDollar[3].str), ConstraintName: NewIdentifierCI(yyDollar[1].str), Name: NewIdentifierCI("PRIMARY"), Primary: true, Unique: true} + yyLOCAL = &IndexInfo{Type: IndexTypePrimary, ConstraintName: NewIdentifierCI(yyDollar[1].str), Name: NewIdentifierCI("PRIMARY")} } yyVAL.union = yyLOCAL case 404: @@ -12827,7 +12827,7 @@ yydefault: var yyLOCAL *IndexInfo //line sql.y:2423 { - yyLOCAL = &IndexInfo{Type: string(yyDollar[1].str) + " " + string(yyDollar[2].str), Name: NewIdentifierCI(yyDollar[3].str), Spatial: true, Unique: false} + yyLOCAL = &IndexInfo{Type: IndexTypeSpatial, Name: NewIdentifierCI(yyDollar[3].str)} } yyVAL.union = yyLOCAL case 405: @@ -12835,7 +12835,7 @@ yydefault: var yyLOCAL *IndexInfo //line sql.y:2427 { - yyLOCAL = &IndexInfo{Type: string(yyDollar[1].str) + " " + string(yyDollar[2].str), Name: NewIdentifierCI(yyDollar[3].str), Fulltext: true, Unique: false} + yyLOCAL = &IndexInfo{Type: IndexTypeFullText, Name: NewIdentifierCI(yyDollar[3].str)} } yyVAL.union = yyLOCAL case 406: @@ -12843,7 +12843,7 @@ yydefault: var yyLOCAL *IndexInfo //line sql.y:2431 { - yyLOCAL = &IndexInfo{Type: string(yyDollar[2].str) + " " + string(yyDollar[3].str), ConstraintName: NewIdentifierCI(yyDollar[1].str), Name: NewIdentifierCI(yyDollar[4].str), Unique: true} + yyLOCAL = &IndexInfo{Type: IndexTypeUnique, ConstraintName: NewIdentifierCI(yyDollar[1].str), Name: NewIdentifierCI(yyDollar[4].str)} } yyVAL.union = yyLOCAL case 407: @@ -12851,7 +12851,7 @@ yydefault: var yyLOCAL *IndexInfo //line sql.y:2435 { - yyLOCAL = &IndexInfo{Type: string(yyDollar[1].str), Name: NewIdentifierCI(yyDollar[2].str), Unique: false} + yyLOCAL = &IndexInfo{Type: IndexTypeDefault, Name: NewIdentifierCI(yyDollar[2].str)} } yyVAL.union = yyLOCAL case 408: @@ -12900,7 +12900,7 @@ yydefault: yyDollar = yyS[yypt-0 : yypt+1] //line sql.y:2473 { - yyVAL.str = "key" + yyVAL.str = "" } case 416: yyDollar = yyS[yypt-1 : yypt+1] diff --git a/go/vt/sqlparser/sql.y b/go/vt/sqlparser/sql.y index 48993358229..0cba64b26e0 100644 --- a/go/vt/sqlparser/sql.y +++ b/go/vt/sqlparser/sql.y @@ -1248,22 +1248,22 @@ alter_table_prefix: create_index_prefix: CREATE comment_opt INDEX ci_identifier using_opt ON table_name { - $$ = &AlterTable{Table: $7, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition:&IndexDefinition{Info: &IndexInfo{Name:$4, Type:string($3)}, Options:$5}}}} + $$ = &AlterTable{Table: $7, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition:&IndexDefinition{Info: &IndexInfo{Name:$4}, Options:$5}}}} setDDL(yylex, $$) } | CREATE comment_opt FULLTEXT INDEX ci_identifier using_opt ON table_name { - $$ = &AlterTable{Table: $8, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition:&IndexDefinition{Info: &IndexInfo{Name:$5, Type:string($3)+" "+string($4), Fulltext:true}, Options:$6}}}} + $$ = &AlterTable{Table: $8, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition:&IndexDefinition{Info: &IndexInfo{Name:$5, Type: IndexTypeFullText}, Options:$6}}}} setDDL(yylex, $$) } | CREATE comment_opt SPATIAL INDEX ci_identifier using_opt ON table_name { - $$ = &AlterTable{Table: $8, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition:&IndexDefinition{Info: &IndexInfo{Name:$5, Type:string($3)+" "+string($4), Spatial:true}, Options:$6}}}} + $$ = &AlterTable{Table: $8, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition:&IndexDefinition{Info: &IndexInfo{Name:$5, Type: IndexTypeSpatial}, Options:$6}}}} setDDL(yylex, $$) } | CREATE comment_opt UNIQUE INDEX ci_identifier using_opt ON table_name { - $$ = &AlterTable{Table: $8, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition:&IndexDefinition{Info: &IndexInfo{Name:$5, Type:string($3)+" "+string($4), Unique:true}, Options:$6}}}} + $$ = &AlterTable{Table: $8, AlterOptions: []AlterOption{&AddIndexDefinition{IndexDefinition:&IndexDefinition{Info: &IndexInfo{Name:$5, Type: IndexTypeUnique}, Options:$6}}}} setDDL(yylex, $$) } @@ -2417,23 +2417,23 @@ equal_opt: index_info: constraint_name_opt PRIMARY KEY name_opt { - $$ = &IndexInfo{Type: string($2) + " " + string($3), ConstraintName: NewIdentifierCI($1), Name: NewIdentifierCI("PRIMARY"), Primary: true, Unique: true} + $$ = &IndexInfo{Type: IndexTypePrimary, ConstraintName: NewIdentifierCI($1), Name: NewIdentifierCI("PRIMARY")} } | SPATIAL index_or_key_opt name_opt { - $$ = &IndexInfo{Type: string($1) + " " + string($2), Name: NewIdentifierCI($3), Spatial: true, Unique: false} + $$ = &IndexInfo{Type: IndexTypeSpatial, Name: NewIdentifierCI($3)} } | FULLTEXT index_or_key_opt name_opt { - $$ = &IndexInfo{Type: string($1) + " " + string($2), Name: NewIdentifierCI($3), Fulltext: true, Unique: false} + $$ = &IndexInfo{Type: IndexTypeFullText, Name: NewIdentifierCI($3)} } | constraint_name_opt UNIQUE index_or_key_opt name_opt { - $$ = &IndexInfo{Type: string($2) + " " + string($3), ConstraintName: NewIdentifierCI($1), Name: NewIdentifierCI($4), Unique: true} + $$ = &IndexInfo{Type: IndexTypeUnique, ConstraintName: NewIdentifierCI($1), Name: NewIdentifierCI($4)} } | index_or_key name_opt { - $$ = &IndexInfo{Type: string($1), Name: NewIdentifierCI($2), Unique: false} + $$ = &IndexInfo{Type: IndexTypeDefault, Name: NewIdentifierCI($2)} } constraint_name_opt: @@ -2471,7 +2471,7 @@ from_or_in: index_or_key_opt: { - $$ = "key" + $$ = "" } | index_or_key { diff --git a/go/vt/sqlparser/tracked_buffer_test.go b/go/vt/sqlparser/tracked_buffer_test.go index 6924bf11911..e928cdfeddf 100644 --- a/go/vt/sqlparser/tracked_buffer_test.go +++ b/go/vt/sqlparser/tracked_buffer_test.go @@ -86,7 +86,7 @@ func TestCanonicalOutput(t *testing.T) { }, { "create table a (id int not null auto_increment, v varchar(32) default null, v2 varchar(62) charset utf8mb4 collate utf8mb4_0900_ai_ci, key v_idx(v(16)))", - "CREATE TABLE `a` (\n\t`id` int NOT NULL AUTO_INCREMENT,\n\t`v` varchar(32) DEFAULT NULL,\n\t`v2` varchar(62) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,\n\tKEY `v_idx` (`v`(16))\n)", + "CREATE TABLE `a` (\n\t`id` int NOT NULL AUTO_INCREMENT,\n\t`v` varchar(32) DEFAULT NULL,\n\t`v2` varchar(62) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,\n\tINDEX `v_idx` (`v`(16))\n)", }, { "create table a (id int not null primary key, dt datetime default current_timestamp)", @@ -178,7 +178,7 @@ func TestCanonicalOutput(t *testing.T) { }, { "create table entries (uid varchar(53) not null, namespace varchar(254) not null, spec json default null, primary key (namespace, uid), key entries_spec_updatedAt ((json_value(spec, _utf8mb4 '$.updatedAt'))))", - "CREATE TABLE `entries` (\n\t`uid` varchar(53) NOT NULL,\n\t`namespace` varchar(254) NOT NULL,\n\t`spec` json DEFAULT NULL,\n\tPRIMARY KEY (`namespace`, `uid`),\n\tKEY `entries_spec_updatedAt` ((JSON_VALUE(`spec`, _utf8mb4 '$.updatedAt')))\n)", + "CREATE TABLE `entries` (\n\t`uid` varchar(53) NOT NULL,\n\t`namespace` varchar(254) NOT NULL,\n\t`spec` json DEFAULT NULL,\n\tPRIMARY KEY (`namespace`, `uid`),\n\tINDEX `entries_spec_updatedAt` ((JSON_VALUE(`spec`, _utf8mb4 '$.updatedAt')))\n)", }, { "create table identifiers (id binary(16) not null default (uuid_to_bin(uuid(),true)))", @@ -234,7 +234,7 @@ func TestCanonicalOutput(t *testing.T) { }, { "create table t1 (id int primary key, name tinytext not null, fulltext key name_ft(name) with parser ngram)", - "CREATE TABLE `t1` (\n\t`id` int PRIMARY KEY,\n\t`name` tinytext NOT NULL,\n\tFULLTEXT KEY `name_ft` (`name`) WITH PARSER ngram\n)", + "CREATE TABLE `t1` (\n\t`id` int PRIMARY KEY,\n\t`name` tinytext NOT NULL,\n\tFULLTEXT INDEX `name_ft` (`name`) WITH PARSER ngram\n)", }, { "select convert('abc' using utf8mb4)", diff --git a/go/vt/vtctl/workflow/materializer_test.go b/go/vt/vtctl/workflow/materializer_test.go index 29cb7b085c2..413368087a7 100644 --- a/go/vt/vtctl/workflow/materializer_test.go +++ b/go/vt/vtctl/workflow/materializer_test.go @@ -96,8 +96,8 @@ func TestStripForeignKeys(t *testing.T) { newDDL: "create table table1 (\n" + "\tid int(11) not null auto_increment,\n" + "\tforeign_id int(11),\n" + - "\tPRIMARY KEY (id),\n" + - "\tKEY fk_table1_ref_foreign_id (foreign_id),\n" + + "\tprimary key (id),\n" + + "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + "\tcheck (foreign_id > 10)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", @@ -119,9 +119,9 @@ func TestStripForeignKeys(t *testing.T) { "\tid int(11) not null auto_increment,\n" + "\tforeign_id int(11) not null,\n" + "\tuser_id int(11) not null,\n" + - "\tPRIMARY KEY (id),\n" + - "\tKEY fk_table1_ref_foreign_id (foreign_id),\n" + - "\tKEY fk_table1_ref_user_id (user_id),\n" + + "\tprimary key (id),\n" + + "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + + "\tindex fk_table1_ref_user_id (user_id),\n" + "\tcheck (foreign_id > 10)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", @@ -165,9 +165,9 @@ func TestStripConstraints(t *testing.T) { "\tid int(11) not null auto_increment,\n" + "\tforeign_id int(11) not null,\n" + "\tuser_id int(11) not null,\n" + - "\tPRIMARY KEY (id),\n" + - "\tKEY fk_table1_ref_foreign_id (foreign_id),\n" + - "\tKEY fk_table1_ref_user_id (user_id)\n" + + "\tprimary key (id),\n" + + "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + + "\tindex fk_table1_ref_user_id (user_id)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", @@ -188,9 +188,9 @@ func TestStripConstraints(t *testing.T) { "\tid int(11) not null auto_increment,\n" + "\tforeign_id int(11) not null,\n" + "\tuser_id int(11) not null,\n" + - "\tPRIMARY KEY (id),\n" + - "\tKEY fk_table1_ref_foreign_id (foreign_id),\n" + - "\tKEY fk_table1_ref_user_id (user_id)\n" + + "\tprimary key (id),\n" + + "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + + "\tindex fk_table1_ref_user_id (user_id)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", }, diff --git a/go/vt/vtexplain/vtexplain_vttablet.go b/go/vt/vtexplain/vtexplain_vttablet.go index f902eca8b07..1f8ebd969d7 100644 --- a/go/vt/vtexplain/vtexplain_vttablet.go +++ b/go/vt/vtexplain/vtexplain_vttablet.go @@ -463,7 +463,7 @@ func newTabletEnvironment(ddls []sqlparser.DDLStatement, opts *Options) (*tablet continue } for _, idx := range ddl.GetTableSpec().Indexes { - if !idx.Info.Primary { + if idx.Info.Type != sqlparser.IndexTypePrimary { continue } for _, col := range idx.Columns { diff --git a/go/vt/vttablet/onlineddl/executor.go b/go/vt/vttablet/onlineddl/executor.go index d95d4afc41f..32664b15424 100644 --- a/go/vt/vttablet/onlineddl/executor.go +++ b/go/vt/vttablet/onlineddl/executor.go @@ -1225,7 +1225,7 @@ func (e *Executor) validateAndEditAlterTableStatement(ctx context.Context, onlin // we do not pass ALGORITHM. We choose our own ALGORITHM. continue case *sqlparser.AddIndexDefinition: - if opt.IndexDefinition.Info.Fulltext { + if opt.IndexDefinition.Info.Type == sqlparser.IndexTypeFullText { countAddFullTextStatements++ if countAddFullTextStatements > 1 { // We've already got one ADD FULLTEXT KEY. We can't have another diff --git a/go/vt/vttablet/onlineddl/executor_test.go b/go/vt/vttablet/onlineddl/executor_test.go index 08f363fc892..b642921d831 100644 --- a/go/vt/vttablet/onlineddl/executor_test.go +++ b/go/vt/vttablet/onlineddl/executor_test.go @@ -166,15 +166,15 @@ func TestValidateAndEditAlterTableStatement(t *testing.T) { }, { alter: "alter table t add column i int, add fulltext key name1_ft (name1)", - expect: []string{"alter table t add column i int, add fulltext key name1_ft (name1), algorithm = copy"}, + expect: []string{"alter table t add column i int, add fulltext index name1_ft (name1), algorithm = copy"}, }, { alter: "alter table t add column i int, add fulltext key name1_ft (name1), add fulltext key name2_ft (name2)", - expect: []string{"alter table t add column i int, add fulltext key name1_ft (name1), algorithm = copy", "alter table t add fulltext key name2_ft (name2), algorithm = copy"}, + expect: []string{"alter table t add column i int, add fulltext index name1_ft (name1), algorithm = copy", "alter table t add fulltext index name2_ft (name2), algorithm = copy"}, }, { alter: "alter table t add fulltext key name0_ft (name0), add column i int, add fulltext key name1_ft (name1), add fulltext key name2_ft (name2)", - expect: []string{"alter table t add fulltext key name0_ft (name0), add column i int, algorithm = copy", "alter table t add fulltext key name1_ft (name1), algorithm = copy", "alter table t add fulltext key name2_ft (name2), algorithm = copy"}, + expect: []string{"alter table t add fulltext index name0_ft (name0), add column i int, algorithm = copy", "alter table t add fulltext index name1_ft (name1), algorithm = copy", "alter table t add fulltext index name2_ft (name2), algorithm = copy"}, }, { alter: "alter table t add constraint check (id != 1)", diff --git a/go/vt/vttablet/tabletmanager/vreplication/vreplicator.go b/go/vt/vttablet/tabletmanager/vreplication/vreplicator.go index addf8de2019..e148151934e 100644 --- a/go/vt/vttablet/tabletmanager/vreplication/vreplicator.go +++ b/go/vt/vttablet/tabletmanager/vreplication/vreplicator.go @@ -657,7 +657,7 @@ func (vr *vreplicator) stashSecondaryKeys(ctx context.Context, tableName string) // to each record. // - You can not add/remove multiple fulltext keys // in a single ALTER statement. - if secondaryKey.Info.Primary || secondaryKey.Info.Fulltext { + if secondaryKey.Info.Type == sqlparser.IndexTypePrimary || secondaryKey.Info.Type == sqlparser.IndexTypeFullText { continue } alterDrop.AlterOptions = append(alterDrop.AlterOptions, @@ -740,7 +740,7 @@ func (vr *vreplicator) getTableSecondaryKeys(ctx context.Context, tableName stri } for _, index := range createTable.GetTableSpec().Indexes { - if !index.Info.Primary { + if index.Info.Type != sqlparser.IndexTypePrimary { secondaryKeys = append(secondaryKeys, index) } } diff --git a/go/vt/vttablet/tabletmanager/vreplication/vreplicator_test.go b/go/vt/vttablet/tabletmanager/vreplication/vreplicator_test.go index dd9649645c0..7dbcff1d6ff 100644 --- a/go/vt/vttablet/tabletmanager/vreplication/vreplicator_test.go +++ b/go/vt/vttablet/tabletmanager/vreplication/vreplicator_test.go @@ -286,7 +286,7 @@ func TestDeferSecondaryKeys(t *testing.T) { tableName: "t1", initialDDL: "create table t1 (id int not null, c1 int default null, primary key (id), key c1 (c1))", strippedDDL: "create table t1 (id int not null, c1 int default null, primary key (id))", - actionDDL: "alter table %s.t1 add key c1 (c1)", + actionDDL: "alter table %s.t1 add index c1 (c1)", WorkflowType: int32(binlogdatapb.VReplicationWorkflowType_Reshard), }, { @@ -294,7 +294,7 @@ func TestDeferSecondaryKeys(t *testing.T) { tableName: "t1", initialDDL: "create table t1 (id int not null, c1 int default null, c2 int default null, primary key (id), key c1 (c1), key c2 (c2))", strippedDDL: "create table t1 (id int not null, c1 int default null, c2 int default null, primary key (id))", - actionDDL: "alter table %s.t1 add key c1 (c1), add key c2 (c2)", + actionDDL: "alter table %s.t1 add index c1 (c1), add index c2 (c2)", WorkflowType: int32(binlogdatapb.VReplicationWorkflowType_MoveTables), }, { @@ -302,7 +302,7 @@ func TestDeferSecondaryKeys(t *testing.T) { tableName: "t1", initialDDL: "create table t1 (id int not null, c1 varchar(10) default null, c2 varchar(10) default null, primary key (id), key c1_c2 (c1,c2), key c2 (c2))", strippedDDL: "create table t1 (id int not null, c1 varchar(10) default null, c2 varchar(10) default null, primary key (id))", - actionDDL: "alter table %s.t1 add key c1_c2 (c1, c2), add key c2 (c2)", + actionDDL: "alter table %s.t1 add index c1_c2 (c1, c2), add index c2 (c2)", WorkflowType: int32(binlogdatapb.VReplicationWorkflowType_MoveTables), }, { @@ -310,7 +310,7 @@ func TestDeferSecondaryKeys(t *testing.T) { tableName: "t1", initialDDL: "create table t1 (id int not null, c1 varchar(10) not null, c2 varchar(10) default null, primary key (id,c1), key c1_c2 (c1,c2), key c2 (c2))", strippedDDL: "create table t1 (id int not null, c1 varchar(10) not null, c2 varchar(10) default null, primary key (id,c1))", - actionDDL: "alter table %s.t1 add key c1_c2 (c1, c2), add key c2 (c2)", + actionDDL: "alter table %s.t1 add index c1_c2 (c1, c2), add index c2 (c2)", WorkflowType: int32(binlogdatapb.VReplicationWorkflowType_MoveTables), }, { @@ -318,7 +318,7 @@ func TestDeferSecondaryKeys(t *testing.T) { tableName: "t1", initialDDL: "create table t1 (id int not null, c1 varchar(10) not null, c2 varchar(10) not null, primary key (id,c1,c2), key c2 (c2))", strippedDDL: "create table t1 (id int not null, c1 varchar(10) not null, c2 varchar(10) not null, primary key (id,c1,c2))", - actionDDL: "alter table %s.t1 add key c2 (c2)", + actionDDL: "alter table %s.t1 add index c2 (c2)", WorkflowType: int32(binlogdatapb.VReplicationWorkflowType_Reshard), }, { @@ -326,7 +326,7 @@ func TestDeferSecondaryKeys(t *testing.T) { tableName: "t1", initialDDL: "create table t1 (id int not null, c1 varchar(10) not null, c2 varchar(10) not null, primary key (id,c1,c2), key c2 (c2))", strippedDDL: "create table t1 (id int not null, c1 varchar(10) not null, c2 varchar(10) not null, primary key (id,c1,c2))", - actionDDL: "alter table %s.t1 add key c2 (c2)", + actionDDL: "alter table %s.t1 add index c2 (c2)", postStashHook: func() error { myid := id + 1000 // Insert second vreplication record to simulate a second controller/vreplicator @@ -343,7 +343,7 @@ func TestDeferSecondaryKeys(t *testing.T) { // when this is called there's no secondary keys to stash anymore. addlAction, err := json.Marshal(PostCopyAction{ Type: PostCopyActionSQL, - Task: fmt.Sprintf("alter table %s.t1 add key c2 (c2)", dbName), + Task: fmt.Sprintf("alter table %s.t1 add index c2 (c2)", dbName), }) if err != nil { return err @@ -366,7 +366,7 @@ func TestDeferSecondaryKeys(t *testing.T) { tableName: "t1", initialDDL: "create table t1 (id int not null, c1 varchar(10) default null, c2 varchar(10) default null, key c1_c2 (c1,c2), key c2 (c2))", strippedDDL: "create table t1 (id int not null, c1 varchar(10) default null, c2 varchar(10) default null)", - actionDDL: "alter table %s.t1 add key c1_c2 (c1, c2), add key c2 (c2)", + actionDDL: "alter table %s.t1 add index c1_c2 (c1, c2), add index c2 (c2)", WorkflowType: int32(binlogdatapb.VReplicationWorkflowType_MoveTables), }, { @@ -374,8 +374,8 @@ func TestDeferSecondaryKeys(t *testing.T) { tableName: "t1", initialDDL: "create table t1 (id int not null, c1 int default null, c2 int default null, primary key (id), key c1 (c1), key c2 (c2))", strippedDDL: "create table t1 (id int not null, c1 int default null, c2 int default null, primary key (id))", - intermediateDDL: "alter table %s.t1 add key c1 (c1), add key c2 (c2)", - actionDDL: "alter table %s.t1 add key c1 (c1), add key c2 (c2)", + intermediateDDL: "alter table %s.t1 add index c1 (c1), add index c2 (c2)", + actionDDL: "alter table %s.t1 add index c1 (c1), add index c2 (c2)", WorkflowType: int32(binlogdatapb.VReplicationWorkflowType_MoveTables), }, { @@ -383,8 +383,8 @@ func TestDeferSecondaryKeys(t *testing.T) { tableName: "t1", initialDDL: "create table t1 (id int not null, c1 int default null, c2 int default null, primary key (id), key c1 (c1), key c2 (c2))", strippedDDL: "create table t1 (id int not null, c1 int default null, c2 int default null, primary key (id))", - intermediateDDL: "alter table %s.t1 add key c2 (c2), add key c1 (c1)", - actionDDL: "alter table %s.t1 add key c1 (c1), add key c2 (c2)", + intermediateDDL: "alter table %s.t1 add index c2 (c2), add index c1 (c1)", + actionDDL: "alter table %s.t1 add index c1 (c1), add index c2 (c2)", WorkflowType: int32(binlogdatapb.VReplicationWorkflowType_MoveTables), }, { @@ -392,8 +392,8 @@ func TestDeferSecondaryKeys(t *testing.T) { tableName: "t1", initialDDL: "create table t1 (id int not null, c1 int default null, c2 int default null, primary key (id), key c1 (c1), key c2 (c2))", strippedDDL: "create table t1 (id int not null, c1 int default null, c2 int default null, primary key (id))", - intermediateDDL: "alter table %s.t1 add unique key c1_c2 (c1,c2), add key c2 (c2), add key c1 (c1)", - actionDDL: "alter table %s.t1 add key c1 (c1), add key c2 (c2)", + intermediateDDL: "alter table %s.t1 add unique index c1_c2 (c1,c2), add index c2 (c2), add index c1 (c1)", + actionDDL: "alter table %s.t1 add index c1 (c1), add index c2 (c2)", WorkflowType: int32(binlogdatapb.VReplicationWorkflowType_MoveTables), expectFinalSchemaDiff: true, }, @@ -402,8 +402,8 @@ func TestDeferSecondaryKeys(t *testing.T) { tableName: "t1", initialDDL: "create table t1 (id int not null, c1 int default null, c2 int default null, primary key (id), key c1 (c1), key c2 (c2))", strippedDDL: "create table t1 (id int not null, c1 int default null, c2 int default null, primary key (id))", - intermediateDDL: "alter table %s.t1 add key c2 (c2)", - actionDDL: "alter table %s.t1 add key c1 (c1), add key c2 (c2)", + intermediateDDL: "alter table %s.t1 add index c2 (c2)", + actionDDL: "alter table %s.t1 add index c1 (c1), add index c2 (c2)", WorkflowType: int32(binlogdatapb.VReplicationWorkflowType_MoveTables), wantExecErr: "Duplicate key name 'c2' (errno 1061) (sqlstate 42000)", }, @@ -412,8 +412,8 @@ func TestDeferSecondaryKeys(t *testing.T) { tableName: "t1", initialDDL: "create table t1 (id int not null, c1 int default null, c2 int default null, primary key (id), key c1 (c1), key c2 (c2))", strippedDDL: "create table t1 (id int not null, c1 int default null, c2 int default null, primary key (id))", - intermediateDDL: "alter table %s.t1 add key c1 (c1)", - actionDDL: "alter table %s.t1 add key c1 (c1), add key c2 (c2)", + intermediateDDL: "alter table %s.t1 add index c1 (c1)", + actionDDL: "alter table %s.t1 add index c1 (c1), add index c2 (c2)", WorkflowType: int32(binlogdatapb.VReplicationWorkflowType_MoveTables), wantExecErr: "Duplicate key name 'c1' (errno 1061) (sqlstate 42000)", }, @@ -578,9 +578,9 @@ func TestCancelledDeferSecondaryKeys(t *testing.T) { getActionsSQLf := "select action from _vt.post_copy_action where vrepl_id=%d and table_name='%s'" tableName := "t1" - ddl := fmt.Sprintf("create table %s.t1 (id int not null, c1 int default null, c2 int default null, primary key(id), key c1 (c1), key c2 (c2))", dbName) + ddl := fmt.Sprintf("create table %s.t1 (id int not null, c1 int default null, c2 int default null, primary key(id), index c1 (c1), index c2 (c2))", dbName) withoutPKs := "create table t1 (id int not null, c1 int default null, c2 int default null, primary key(id))" - alter := fmt.Sprintf("alter table %s.t1 add key c1 (c1), add key c2 (c2)", dbName) + alter := fmt.Sprintf("alter table %s.t1 add index c1 (c1), add index c2 (c2)", dbName) // Create the table. _, err = dbClient.ExecuteFetch(ddl, 1) diff --git a/go/vt/wrangler/materializer_test.go b/go/vt/wrangler/materializer_test.go index 9126d8540c1..3984641fcf8 100644 --- a/go/vt/wrangler/materializer_test.go +++ b/go/vt/wrangler/materializer_test.go @@ -2834,8 +2834,8 @@ func TestStripForeignKeys(t *testing.T) { newDDL: "create table table1 (\n" + "\tid int(11) not null auto_increment,\n" + "\tforeign_id int(11),\n" + - "\tPRIMARY KEY (id),\n" + - "\tKEY fk_table1_ref_foreign_id (foreign_id),\n" + + "\tprimary key (id),\n" + + "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + "\tcheck (foreign_id > 10)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", @@ -2857,9 +2857,9 @@ func TestStripForeignKeys(t *testing.T) { "\tid int(11) not null auto_increment,\n" + "\tforeign_id int(11) not null,\n" + "\tuser_id int(11) not null,\n" + - "\tPRIMARY KEY (id),\n" + - "\tKEY fk_table1_ref_foreign_id (foreign_id),\n" + - "\tKEY fk_table1_ref_user_id (user_id),\n" + + "\tprimary key (id),\n" + + "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + + "\tindex fk_table1_ref_user_id (user_id),\n" + "\tcheck (foreign_id > 10)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", @@ -2903,9 +2903,9 @@ func TestStripConstraints(t *testing.T) { "\tid int(11) not null auto_increment,\n" + "\tforeign_id int(11) not null,\n" + "\tuser_id int(11) not null,\n" + - "\tPRIMARY KEY (id),\n" + - "\tKEY fk_table1_ref_foreign_id (foreign_id),\n" + - "\tKEY fk_table1_ref_user_id (user_id)\n" + + "\tprimary key (id),\n" + + "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + + "\tindex fk_table1_ref_user_id (user_id)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", @@ -2926,9 +2926,9 @@ func TestStripConstraints(t *testing.T) { "\tid int(11) not null auto_increment,\n" + "\tforeign_id int(11) not null,\n" + "\tuser_id int(11) not null,\n" + - "\tPRIMARY KEY (id),\n" + - "\tKEY fk_table1_ref_foreign_id (foreign_id),\n" + - "\tKEY fk_table1_ref_user_id (user_id)\n" + + "\tprimary key (id),\n" + + "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + + "\tindex fk_table1_ref_user_id (user_id)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", },