From e5d591031acb26a8263bf5cf404ef7ed972bbf55 Mon Sep 17 00:00:00 2001 From: Dirkjan Bussink Date: Tue, 31 Oct 2023 17:57:14 +0100 Subject: [PATCH] sqlparser: Use KEY instead of INDEX for normalized form This matches how MySQL behaves for example in `SHOW CREATE TABLE`. This is the kind of output format we want to match if we reasonably can, also to simplify and `schemadiff` more friendly to use. Signed-off-by: Dirkjan Bussink --- go/vt/schema/online_ddl_test.go | 10 +- go/vt/schemadiff/diff_test.go | 12 +- go/vt/schemadiff/table_test.go | 110 ++++++++-------- go/vt/sqlparser/ast_format.go | 8 +- go/vt/sqlparser/ast_format_fast.go | 8 +- go/vt/sqlparser/parse_test.go | 120 +++++++++--------- go/vt/sqlparser/tracked_buffer_test.go | 8 +- go/vt/vtctl/workflow/materializer_test.go | 14 +- .../planbuilder/testdata/ddl_cases.json | 4 +- .../ddl_cases_no_default_keyspace.json | 2 +- .../testdata/unsupported_cases.json | 2 +- go/vt/vttablet/onlineddl/executor_test.go | 6 +- .../vreplication/vreplicator_test.go | 38 +++--- .../planbuilder/testdata/exec_cases.txt | 2 +- .../tabletserver/query_executor_test.go | 6 +- go/vt/wrangler/materializer_test.go | 14 +- 16 files changed, 182 insertions(+), 182 deletions(-) diff --git a/go/vt/schema/online_ddl_test.go b/go/vt/schema/online_ddl_test.go index dbcad5454dc..c616d64a698 100644 --- a/go/vt/schema/online_ddl_test.go +++ b/go/vt/schema/online_ddl_test.go @@ -264,11 +264,11 @@ func TestNewOnlineDDLs(t *testing.T) { "drop table if exists t": {sqls: []string{"drop table if exists t"}}, "drop table t1, t2, t3": {sqls: []string{"drop table t1", "drop table t2", "drop table t3"}}, "drop table if exists t1, t2, t3": {sqls: []string{"drop table if exists t1", "drop table if exists t2", "drop table if exists t3"}}, - "create index i_idx on t(id)": {sqls: []string{"alter table t add index i_idx (id)"}}, - "create index i_idx on t(name(12))": {sqls: []string{"alter table t add index i_idx (`name`(12))"}}, - "create index i_idx on t(id, `ts`, name(12))": {sqls: []string{"alter table t add index i_idx (id, ts, `name`(12))"}}, - "create unique index i_idx on t(id)": {sqls: []string{"alter table t add unique index i_idx (id)"}}, - "create index i_idx using btree on t(id)": {sqls: []string{"alter table t add index i_idx (id) using btree"}}, + "create index i_idx on t(id)": {sqls: []string{"alter table t add key i_idx (id)"}}, + "create index i_idx on t(name(12))": {sqls: []string{"alter table t add key i_idx (`name`(12))"}}, + "create index i_idx on t(id, `ts`, name(12))": {sqls: []string{"alter table t add key i_idx (id, ts, `name`(12))"}}, + "create unique index i_idx on t(id)": {sqls: []string{"alter table t add unique key i_idx (id)"}}, + "create index i_idx using btree on t(id)": {sqls: []string{"alter table t add key i_idx (id) using btree"}}, "create view v as select * from t": {sqls: []string{"create view v as select * from t"}, isView: true}, "alter view v as select * from t": {sqls: []string{"alter view v as select * from t"}, isView: true}, "drop view v": {sqls: []string{"drop view v"}, isView: true}, diff --git a/go/vt/schemadiff/diff_test.go b/go/vt/schemadiff/diff_test.go index 8676e1bab29..d2a170f4752 100644 --- a/go/vt/schemadiff/diff_test.go +++ b/go/vt/schemadiff/diff_test.go @@ -479,10 +479,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 index deleted_check (id, (if(deleted_at is not null, 0, null)))", + "alter table t1 drop key deleted_check, add unique key deleted_check (id, (if(deleted_at is not null, 0, null)))", }, cdiffs: []string{ - "ALTER TABLE `t1` DROP KEY `deleted_check`, ADD UNIQUE INDEX `deleted_check` (`id`, (if(`deleted_at` IS NOT NULL, 0, NULL)))", + "ALTER TABLE `t1` DROP KEY `deleted_check`, ADD UNIQUE KEY `deleted_check` (`id`, (if(`deleted_at` IS NOT NULL, 0, NULL)))", }, }, { @@ -658,13 +658,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\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)", + "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)", }, 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\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)", + "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)", }, }, { diff --git a/go/vt/schemadiff/table_test.go b/go/vt/schemadiff/table_test.go index d3df8274fee..4d41d9584c0 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 index i2_idx (i2)", - cdiff: "ALTER TABLE `t1` RENAME COLUMN `i1` TO `i2`, ADD INDEX `i2_idx` (`i2`)", + 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`)", }, { // 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 index i_idx (i)", - cdiff: "ALTER TABLE `t1` ADD INDEX `i_idx` (`i`)", + diff: "alter table t1 add key i_idx (i)", + cdiff: "ALTER TABLE `t1` ADD KEY `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 index i (i)", - cdiff: "ALTER TABLE `t1` ADD INDEX `i` (`i`)", + diff: "alter table t1 add key i (i)", + cdiff: "ALTER TABLE `t1` ADD KEY `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 index i_2 (i)", - cdiff: "ALTER TABLE `t1` ADD INDEX `i_2` (`i`)", + diff: "alter table t1 add key i_2 (i)", + cdiff: "ALTER TABLE `t1` ADD KEY `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 index i_3 (i)", - cdiff: "ALTER TABLE `t1` ADD INDEX `i_3` (`i`)", + diff: "alter table t1 add key i_3 (i)", + cdiff: "ALTER TABLE `t1` ADD KEY `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 index i_idx (i)", - cdiff: "ALTER TABLE `t1` ADD COLUMN `i` int, ADD INDEX `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`)", }, { 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 index i_idx (i, id)", - cdiff: "ALTER TABLE `t1` DROP KEY `i_idx`, ADD INDEX `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`)", }, { 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 index", + name: "reordered key, add key", 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 index i_idx3 (id)", - cdiff: "ALTER TABLE `t1` ADD INDEX `i_idx3` (`id`)", + diff: "alter table t1 add key i_idx3 (id)", + cdiff: "ALTER TABLE `t1` ADD KEY `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 index name_ft (`name`)", - cdiff: "ALTER TABLE `t1` ADD FULLTEXT INDEX `name_ft` (`name`)", + diff: "alter table t1 add fulltext key name_ft (`name`)", + cdiff: "ALTER TABLE `t1` ADD FULLTEXT KEY `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 index name_ft (`name`) with parser ngram", - cdiff: "ALTER TABLE `t1` ADD FULLTEXT INDEX `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", }, { 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 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`)", + 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`)", }, { 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 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`)"}, + 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`)"}, }, { 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 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`)", + 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`)", }, { name: "no fulltext diff", @@ -658,8 +658,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 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`)", + 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`)", }, { name: "identical foreign key", @@ -1207,8 +1207,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 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)))", + 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)))", }, { // validates that CanonicalString prints 'interval 30 minute' and not ' INTERVAL 30 MINUTE', as MySQL's `SHOW CREATE TABLE` outputs lower case 'interval 30 minute' @@ -1381,9 +1381,9 @@ func TestValidate(t *testing.T) { }, // keys { - name: "add index", + name: "add key", from: "create table t (id int primary key, i int)", - alter: "alter table t add index i_idx(i)", + alter: "alter table t add key i_idx(i)", to: "create table t (id int primary key, i int, key i_idx(i))", }, { @@ -1447,27 +1447,27 @@ func TestValidate(t *testing.T) { expectErr: &DuplicateKeyNameError{Table: "t", Key: "PRIMARY"}, }, { - name: "add index, column case", + name: "add key, column case", from: "create table t (id int primary key, i int)", - alter: "alter table t add index i_idx(I)", + alter: "alter table t add key 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 index i_idx(i)", + alter: "alter table t add column i int, add key i_idx(i)", to: "create table t (id int primary key, i int, key i_idx(i))", }, { - name: "add index, missing column", + name: "add key, missing column", from: "create table t (id int primary key, i int)", - alter: "alter table t add index j_idx(j)", + alter: "alter table t add key j_idx(j)", expectErr: &InvalidColumnInKeyError{Table: "t", Column: "j", Key: "j_idx"}, }, { - name: "add index, missing column 2", + name: "add key, missing column 2", from: "create table t (id int primary key, i int)", - alter: "alter table t add index j_idx(j, i)", + alter: "alter table t add key j_idx(j, i)", expectErr: &InvalidColumnInKeyError{Table: "t", Column: "j", Key: "j_idx"}, }, { @@ -1527,13 +1527,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 index i12_idx(i1, i2), add index i32_idx(i3, i2), add index i21_idx(i2, i1)", + alter: "alter table t add key i12_idx(i1, i2), add key i32_idx(i3, i2), add key 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 index i12_idx(i1, i2), add index i32_idx(i3, i2), add index i21_idx(i2, i1)", + alter: "alter table t add key i12_idx(i1, i2), add key i32_idx(i3, i2), add key i21_idx(i2, i1)", expectErr: &InvalidColumnInKeyError{Table: "t", Column: "i3", Key: "i32_idx"}, }, { @@ -1582,7 +1582,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 index i12_idx(i1, i2), add index i32_idx((IF(i3 IS NULL, i2, i3)), i2), add index i21_idx(i2, i1)", + 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)", expectErr: &InvalidColumnInKeyError{Table: "t", Column: "i3", Key: "i32_idx"}, }, // data types @@ -1803,13 +1803,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 index i_idx (i), add constraint f foreign key (i) references parent(id)", + alter: "alter table t add key 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 index i_id_idx (i, id), add constraint f foreign key (i) references parent(id)", + alter: "alter table t add key 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))", }, { @@ -2074,7 +2074,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\tINDEX `i` (`i`),\n\tINDEX `I_2` (`I`)\n)", + to: "CREATE TABLE `t` (\n\t`id` int,\n\t`I` int,\n\tKEY `i` (`i`),\n\tKEY `I_2` (`I`)\n)", }, { name: "generates a name for checks", @@ -2089,47 +2089,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\tINDEX `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\tKEY `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\tINDEX `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\tKEY `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\tINDEX `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\tKEY `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\tINDEX `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\tKEY `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\tINDEX `i1_idx` (`i1`)\n)", + to: "CREATE TABLE `t` (\n\t`id` int,\n\t`i1` int,\n\tPRIMARY KEY (`id`),\n\tKEY `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\tINDEX `i1_idx` (`i1`)\n)", + to: "CREATE TABLE `t` (\n\t`id` int,\n\t`i1` int,\n\tPRIMARY KEY (`id`),\n\tKEY `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\tINDEX `i1_idx` (`i1`) USING hash\n)", + to: "CREATE TABLE `t` (\n\t`id` int,\n\t`i1` int,\n\tPRIMARY KEY (`id`),\n\tKEY `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\tINDEX `i1_idx` (`i1`)\n)", + to: "CREATE TABLE `t` (\n\t`id` int,\n\t`i1` int,\n\tPRIMARY KEY (`id`),\n\tKEY `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\tINDEX `i1_idx` (`i1`) INVISIBLE\n)", + to: "CREATE TABLE `t` (\n\t`id` int,\n\t`i1` int,\n\tPRIMARY KEY (`id`),\n\tKEY `i1_idx` (`i1`) INVISIBLE\n)", }, { name: "drops default column visibility", diff --git a/go/vt/sqlparser/ast_format.go b/go/vt/sqlparser/ast_format.go index 3b854181341..3176ea2c12e 100644 --- a/go/vt/sqlparser/ast_format.go +++ b/go/vt/sqlparser/ast_format.go @@ -839,13 +839,13 @@ func (ii *IndexInfo) Format(buf *TrackedBuffer) { buf.astPrintf(ii, "%s %s", keywordStrings[PRIMARY], keywordStrings[KEY]) return case IndexTypeDefault: - buf.astPrintf(ii, "%s", keywordStrings[INDEX]) + buf.astPrintf(ii, "%s", keywordStrings[KEY]) case IndexTypeUnique: - buf.astPrintf(ii, "%s %s", keywordStrings[UNIQUE], keywordStrings[INDEX]) + buf.astPrintf(ii, "%s %s", keywordStrings[UNIQUE], keywordStrings[KEY]) case IndexTypeSpatial: - buf.astPrintf(ii, "%s %s", keywordStrings[SPATIAL], keywordStrings[INDEX]) + buf.astPrintf(ii, "%s %s", keywordStrings[SPATIAL], keywordStrings[KEY]) case IndexTypeFullText: - buf.astPrintf(ii, "%s %s", keywordStrings[FULLTEXT], keywordStrings[INDEX]) + buf.astPrintf(ii, "%s %s", keywordStrings[FULLTEXT], keywordStrings[KEY]) } 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 07fedfe7943..b99c96c87ab 100644 --- a/go/vt/sqlparser/ast_format_fast.go +++ b/go/vt/sqlparser/ast_format_fast.go @@ -1140,19 +1140,19 @@ func (ii *IndexInfo) FormatFast(buf *TrackedBuffer) { buf.WriteString(keywordStrings[KEY]) return case IndexTypeDefault: - buf.WriteString(keywordStrings[INDEX]) + buf.WriteString(keywordStrings[KEY]) case IndexTypeUnique: buf.WriteString(keywordStrings[UNIQUE]) buf.WriteByte(' ') - buf.WriteString(keywordStrings[INDEX]) + buf.WriteString(keywordStrings[KEY]) case IndexTypeSpatial: buf.WriteString(keywordStrings[SPATIAL]) buf.WriteByte(' ') - buf.WriteString(keywordStrings[INDEX]) + buf.WriteString(keywordStrings[KEY]) case IndexTypeFullText: buf.WriteString(keywordStrings[FULLTEXT]) buf.WriteByte(' ') - buf.WriteString(keywordStrings[INDEX]) + buf.WriteString(keywordStrings[KEY]) } if !ii.Name.IsEmpty() { buf.WriteByte(' ') diff --git a/go/vt/sqlparser/parse_test.go b/go/vt/sqlparser/parse_test.go index 28fa6fc4a0f..1837a104e4c 100644 --- a/go/vt/sqlparser/parse_test.go +++ b/go/vt/sqlparser/parse_test.go @@ -1529,16 +1529,16 @@ var ( input: "alter table a alter index x visible, alter index x2 invisible", }, { input: "alter table a add spatial key foo (column1)", - output: "alter table a add spatial index foo (column1)", + output: "alter table a add spatial key foo (column1)", }, { 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", + output: "alter table a add fulltext key foo (column1), order by a, b, c", }, { input: "alter table a add unique key foo (column1)", - output: "alter table a add unique index foo (column1)", + output: "alter 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)", + output: "alter /*vt+ strategy=online */ table a add unique key foo (column1)", }, { input: "alter table a change column s foo int default 1 after x", }, { @@ -1660,13 +1660,13 @@ var ( }, { input: "alter table a add column (id int, id2 char(23))", }, { - input: "alter table a add index idx (id)", + input: "alter table a add key idx (id)", }, { - input: "alter table a add fulltext index idx (id)", + input: "alter table a add fulltext key idx (id)", }, { - input: "alter table a add spatial index idx (id)", + input: "alter table a add spatial key idx (id)", }, { - input: "alter table a add fulltext index idx (id)", + input: "alter table a add fulltext key idx (id)", }, { input: "alter table a add foreign key (id) references f (id)", }, { @@ -1679,7 +1679,7 @@ var ( input: "alter table a add constraint b primary key (id)", }, { input: "alter table a add constraint b unique key (id)", - output: "alter table a add constraint b unique index (id)", + output: "alter table a add constraint b unique key (id)", }, { input: "alter table t add column iii int signed not null", output: "alter table t add column iii int not null", @@ -1687,7 +1687,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 index c (id)", + output: "alter table a add constraint b unique key c (id)", }, { input: "alter table a add constraint check (id)", output: "alter table a add check (id)", @@ -1834,7 +1834,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\tindex 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\tkey b2_idx (b)\n)", }, { input: "create temporary table a (\n\tid bigint\n)", }, { @@ -2006,28 +2006,28 @@ var ( ignoreNormalizerTest: true, }, { input: "create index a on b (col1)", - output: "alter table b add index a (col1)", + output: "alter table b add key a (col1)", }, { input: "create unique index a on b (col1)", - output: "alter table b add unique index a (col1)", + output: "alter table b add unique key a (col1)", }, { input: "create unique index a using foo on b (col1 desc)", - output: "alter table b add unique index a (col1 desc) using foo", + output: "alter table b add unique key a (col1 desc) using foo", }, { input: "create fulltext index a on b (col1) with parser a", - output: "alter table b add fulltext index a (col1) with parser a", + output: "alter table b add fulltext key a (col1) with parser a", }, { input: "create spatial index a on b (col1)", - output: "alter table b add spatial index a (col1)", + output: "alter table b add spatial key a (col1)", }, { input: "create fulltext index a on b (col1) key_block_size=12 with parser a comment 'string' algorithm inplace lock none", - output: "alter table b add fulltext index a (col1) key_block_size 12 with parser a comment 'string', algorithm = inplace, lock none", + output: "alter table b add fulltext key a (col1) key_block_size 12 with parser a comment 'string', algorithm = inplace, lock none", }, { input: "create index a on b ((col1 + col2), (col1*col2))", - output: "alter table b add index a ((col1 + col2), (col1 * col2))", + output: "alter table b add key a ((col1 + col2), (col1 * col2))", }, { input: "create fulltext index b using btree on A (col1 desc, col2) algorithm = inplace lock = none", - output: "alter table A add fulltext index b (col1 desc, col2) using btree, algorithm = inplace, lock none", + output: "alter table A add fulltext key b (col1 desc, col2) using btree, algorithm = inplace, lock none", }, { input: "create algorithm = merge sql security definer view a as select * from e", }, { @@ -4085,7 +4085,7 @@ func TestCaseSensitivity(t *testing.T) { output: "create table A (\n\tB int\n)", }, { input: "create index b on A (col1 desc)", - output: "alter table A add index b (col1 desc)", + output: "alter table A add key b (col1 desc)", }, { input: "alter table A foo", output: "alter table A", @@ -4664,7 +4664,7 @@ func TestCreateTable(t *testing.T) { fulltext key fts (full_name), unique key by_username (username), unique key by_username2 (username), - unique index by_username3 (username), + unique key by_username3 (username), index by_status (status_nonkeyword), key by_full_name (full_name) )`, @@ -4676,13 +4676,13 @@ func TestCreateTable(t *testing.T) { 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) + spatial key geom (geom), + fulltext key fts (full_name), + unique key by_username (username), + unique key by_username2 (username), + unique key by_username3 (username), + key by_status (status_nonkeyword), + key by_full_name (full_name) )`, }, // test defining index visibility @@ -4692,14 +4692,14 @@ func TestCreateTable(t *testing.T) { username varchar, unique key by_username (username) visible, unique key by_username2 (username) invisible, - unique index by_username3 (username) + unique key 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) + unique key by_username (username) visible, + unique key by_username2 (username) invisible, + unique key by_username3 (username) )`, }, // test adding engine attributes @@ -4708,13 +4708,13 @@ func TestCreateTable(t *testing.T) { id int auto_increment, username varchar, unique key by_username (username) engine_attribute '{}' secondary_engine_attribute '{}', - unique index by_username3 (username) + unique key 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) + unique key by_username (username) engine_attribute '{}' secondary_engine_attribute '{}', + unique key by_username3 (username) )`, }, // test defining SRID @@ -4755,11 +4755,11 @@ func TestCreateTable(t *testing.T) { full_name varchar, status_nonkeyword varchar, primary key (id) using BTREE, - 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, - index by_full_name (full_name) using OTHER + unique key by_username (username) using HASH, + unique key by_username2 (username) using OTHER, + unique key by_username3 (username) using XYZ, + key by_status (status_nonkeyword) using PDQ, + key by_full_name (full_name) using OTHER )`, }, // test other index options @@ -4769,9 +4769,9 @@ func TestCreateTable(t *testing.T) { username varchar, email varchar, primary key (id) comment 'hi', - 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' + unique key by_username (username) key_block_size 8, + unique key by_username4 (username) comment 'hi' using BTREE, + unique key by_username4 (username) using BTREE key_block_size 4 comment 'hi' )`, }, { @@ -4822,9 +4822,9 @@ func TestCreateTable(t *testing.T) { 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) + unique key by_abc (a, b, c), + unique key (a, b, c), + key by_email (email(10), username) )`, }, // geometrycollection & geomcollection alias @@ -4896,7 +4896,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), - index by_email (email(10), username), + key 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, @@ -4919,7 +4919,7 @@ func TestCreateTable(t *testing.T) { id int(11) not null auto_increment, user_id int(11) not null, primary key (id), - unique index post_user_unique (user_id), + unique key post_user_unique (user_id), constraint ` + "`" + `Post With Space_ibfk_1` + "`" + ` foreign key (user_id) references ` + "`" + `User` + "`" + ` (id) ) ENGINE Innodb`, }, @@ -4979,9 +4979,9 @@ func TestCreateTable(t *testing.T) { output: `create table t ( id int auto_increment, username varchar, - 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 + 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 )`, }, { // test defaults @@ -5605,7 +5605,7 @@ partition by list (val) nickname varchar(64), email varchar(64), primary key (id), - index email_idx (email, (if(username = '', nickname, username))) + key email_idx (email, (if(username = '', nickname, username))) )`, }, { @@ -5619,9 +5619,9 @@ partition by list (val) spec json default null, salaryInfo json default null, primary key (namespace, uid), - unique index namespaced_name (namespace, place), - unique index unique_uid (uid), - index entries_spec_updatedAt ((json_value(spec, _utf8mb4 '$.updatedAt'))) + unique key namespaced_name (namespace, place), + unique key unique_uid (uid), + key entries_spec_updatedAt ((json_value(spec, _utf8mb4 '$.updatedAt'))) ) ENGINE InnoDB, CHARSET utf8mb4, COLLATE utf8mb4_bin`, @@ -5633,7 +5633,7 @@ partition by list (val) )`, output: `create table t1 ( j JSON, - index i1 ((json_value(j, '$.id' returning UNSIGNED))) + key i1 ((json_value(j, '$.id' returning UNSIGNED))) )`, }, { input: `CREATE TABLE entries ( @@ -5660,9 +5660,9 @@ partition by list (val) spec json default null, salaryInfo json default null, 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))) + unique key namespaced_employee (namespace, employee), + unique key unique_uid (uid), + key entries_spec_updatedAt ((json_value(spec, _utf8mb4 '$.updatedAt' returning datetime))) ) ENGINE InnoDB, CHARSET utf8mb4, COLLATE utf8mb4_bin`, @@ -5729,7 +5729,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\tkey zips ((cast(info -> '$.field' as unsigned array)))\n)", }, } for _, test := range createTableQueries { diff --git a/go/vt/sqlparser/tracked_buffer_test.go b/go/vt/sqlparser/tracked_buffer_test.go index e928cdfeddf..2375441b34e 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\tINDEX `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\tKEY `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\tINDEX `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\tKEY `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 INDEX `name_ft` (`name`) WITH PARSER ngram\n)", + "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)", }, { "select convert('abc' using utf8mb4)", @@ -270,7 +270,7 @@ func TestCanonicalOutput(t *testing.T) { }, { "create table t (id int, info JSON, INDEX zips((CAST(info->'$.field' AS unsigned array))))", - "CREATE TABLE `t` (\n\t`id` int,\n\t`info` JSON,\n\tINDEX `zips` ((CAST(`info` -> '$.field' AS unsigned array)))\n)", + "CREATE TABLE `t` (\n\t`id` int,\n\t`info` JSON,\n\tKEY `zips` ((CAST(`info` -> '$.field' AS unsigned array)))\n)", }, { "select 1 from t1 into outfile 'test/t1.txt'", diff --git a/go/vt/vtctl/workflow/materializer_test.go b/go/vt/vtctl/workflow/materializer_test.go index 9b5c7c5c1cd..fc39bb4d30b 100644 --- a/go/vt/vtctl/workflow/materializer_test.go +++ b/go/vt/vtctl/workflow/materializer_test.go @@ -102,7 +102,7 @@ func TestStripForeignKeys(t *testing.T) { "\tid int(11) not null auto_increment,\n" + "\tforeign_id int(11),\n" + "\tprimary key (id),\n" + - "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + + "\tkey fk_table1_ref_foreign_id (foreign_id),\n" + "\tcheck (foreign_id > 10)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", @@ -125,8 +125,8 @@ func TestStripForeignKeys(t *testing.T) { "\tforeign_id int(11) not null,\n" + "\tuser_id int(11) not null,\n" + "\tprimary key (id),\n" + - "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + - "\tindex fk_table1_ref_user_id (user_id),\n" + + "\tkey fk_table1_ref_foreign_id (foreign_id),\n" + + "\tkey fk_table1_ref_user_id (user_id),\n" + "\tcheck (foreign_id > 10)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", @@ -171,8 +171,8 @@ func TestStripConstraints(t *testing.T) { "\tforeign_id int(11) not null,\n" + "\tuser_id int(11) not null,\n" + "\tprimary key (id),\n" + - "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + - "\tindex fk_table1_ref_user_id (user_id)\n" + + "\tkey fk_table1_ref_foreign_id (foreign_id),\n" + + "\tkey fk_table1_ref_user_id (user_id)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", @@ -194,8 +194,8 @@ func TestStripConstraints(t *testing.T) { "\tforeign_id int(11) not null,\n" + "\tuser_id int(11) not null,\n" + "\tprimary key (id),\n" + - "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + - "\tindex fk_table1_ref_user_id (user_id)\n" + + "\tkey fk_table1_ref_foreign_id (foreign_id),\n" + + "\tkey fk_table1_ref_user_id (user_id)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", }, diff --git a/go/vt/vtgate/planbuilder/testdata/ddl_cases.json b/go/vt/vtgate/planbuilder/testdata/ddl_cases.json index c6dad1ab946..e31cc3e29e1 100644 --- a/go/vt/vtgate/planbuilder/testdata/ddl_cases.json +++ b/go/vt/vtgate/planbuilder/testdata/ddl_cases.json @@ -222,7 +222,7 @@ "Name": "user", "Sharded": true }, - "Query": "alter table `user` add index a (id)" + "Query": "alter table `user` add key a (id)" }, "TablesUsed": [ "user.user" @@ -241,7 +241,7 @@ "Name": "main", "Sharded": false }, - "Query": "alter table unknown add index a (id)" + "Query": "alter table unknown add key a (id)" }, "TablesUsed": [ "main.unknown" diff --git a/go/vt/vtgate/planbuilder/testdata/ddl_cases_no_default_keyspace.json b/go/vt/vtgate/planbuilder/testdata/ddl_cases_no_default_keyspace.json index e813b8bdbdd..c2e1b1ed94e 100644 --- a/go/vt/vtgate/planbuilder/testdata/ddl_cases_no_default_keyspace.json +++ b/go/vt/vtgate/planbuilder/testdata/ddl_cases_no_default_keyspace.json @@ -567,7 +567,7 @@ "Name": "user", "Sharded": true }, - "Query": "alter table `user` add index a (id)" + "Query": "alter table `user` add key a (id)" }, "TablesUsed": [ "user.user" diff --git a/go/vt/vtgate/planbuilder/testdata/unsupported_cases.json b/go/vt/vtgate/planbuilder/testdata/unsupported_cases.json index 6070ede27b9..923e7804782 100644 --- a/go/vt/vtgate/planbuilder/testdata/unsupported_cases.json +++ b/go/vt/vtgate/planbuilder/testdata/unsupported_cases.json @@ -389,4 +389,4 @@ "query": "WITH user AS (SELECT col FROM user) SELECT * FROM user", "plan": "VT12001: unsupported: do not support CTE that use the CTE alias inside the CTE query" } -] \ No newline at end of file +] diff --git a/go/vt/vttablet/onlineddl/executor_test.go b/go/vt/vttablet/onlineddl/executor_test.go index 0da2b5b802e..560f2ebe132 100644 --- a/go/vt/vttablet/onlineddl/executor_test.go +++ b/go/vt/vttablet/onlineddl/executor_test.go @@ -197,15 +197,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 index name1_ft (name1), algorithm = copy"}, + expect: []string{"alter table t add column i int, add fulltext key 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 index name1_ft (name1), algorithm = copy", "alter table t add fulltext index name2_ft (name2), algorithm = copy"}, + 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"}, }, { 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 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"}, + 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"}, }, { alter: "alter table t add constraint check (id != 1)", diff --git a/go/vt/vttablet/tabletmanager/vreplication/vreplicator_test.go b/go/vt/vttablet/tabletmanager/vreplication/vreplicator_test.go index 7dbcff1d6ff..128d41d4bc2 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 index c1 (c1)", + actionDDL: "alter table %s.t1 add key 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 index c1 (c1), add index c2 (c2)", + actionDDL: "alter table %s.t1 add key c1 (c1), add key 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 index c1_c2 (c1, c2), add index c2 (c2)", + actionDDL: "alter table %s.t1 add key c1_c2 (c1, c2), add key 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 index c1_c2 (c1, c2), add index c2 (c2)", + actionDDL: "alter table %s.t1 add key c1_c2 (c1, c2), add key 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 index c2 (c2)", + actionDDL: "alter table %s.t1 add key 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 index c2 (c2)", + actionDDL: "alter table %s.t1 add key 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 index c2 (c2)", dbName), + Task: fmt.Sprintf("alter table %s.t1 add key 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 index c1_c2 (c1, c2), add index c2 (c2)", + actionDDL: "alter table %s.t1 add key c1_c2 (c1, c2), add key 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 index c1 (c1), add index c2 (c2)", - actionDDL: "alter table %s.t1 add index c1 (c1), add index c2 (c2)", + 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)", 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 index c2 (c2), add index c1 (c1)", - actionDDL: "alter table %s.t1 add index c1 (c1), add index c2 (c2)", + 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)", 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 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)", + intermediateDDL: "alter table %s.t1 add unique index 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)", 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 index c2 (c2)", - actionDDL: "alter table %s.t1 add index c1 (c1), add index c2 (c2)", + intermediateDDL: "alter table %s.t1 add key c2 (c2)", + actionDDL: "alter table %s.t1 add key c1 (c1), add key 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 index c1 (c1)", - actionDDL: "alter table %s.t1 add index c1 (c1), add index c2 (c2)", + intermediateDDL: "alter table %s.t1 add key c1 (c1)", + actionDDL: "alter table %s.t1 add key c1 (c1), add key c2 (c2)", WorkflowType: int32(binlogdatapb.VReplicationWorkflowType_MoveTables), wantExecErr: "Duplicate key name 'c1' (errno 1061) (sqlstate 42000)", }, @@ -580,7 +580,7 @@ func TestCancelledDeferSecondaryKeys(t *testing.T) { tableName := "t1" 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 index c1 (c1), add index c2 (c2)", dbName) + alter := fmt.Sprintf("alter table %s.t1 add key c1 (c1), add key c2 (c2)", dbName) // Create the table. _, err = dbClient.ExecuteFetch(ddl, 1) diff --git a/go/vt/vttablet/tabletserver/planbuilder/testdata/exec_cases.txt b/go/vt/vttablet/tabletserver/planbuilder/testdata/exec_cases.txt index aa08bcfc4a3..5565f405bc7 100644 --- a/go/vt/vttablet/tabletserver/planbuilder/testdata/exec_cases.txt +++ b/go/vt/vttablet/tabletserver/planbuilder/testdata/exec_cases.txt @@ -621,7 +621,7 @@ options:PassthroughDMLs "Role": 2 } ], - "FullQuery":"alter table b add index a (id)" + "FullQuery":"alter table b add key a (id)" } # create view diff --git a/go/vt/vttablet/tabletserver/query_executor_test.go b/go/vt/vttablet/tabletserver/query_executor_test.go index 10114624fb2..d4058df8ad2 100644 --- a/go/vt/vttablet/tabletserver/query_executor_test.go +++ b/go/vt/vttablet/tabletserver/query_executor_test.go @@ -208,13 +208,13 @@ func TestQueryExecutorPlans(t *testing.T) { }, { input: "create index a on user(id)", dbResponses: []dbResponse{{ - query: "alter table `user` add index a (id)", + query: "alter table `user` add key a (id)", result: emptyResult, }}, resultWant: emptyResult, planWant: "DDL", - logWant: "alter table `user` add index a (id)", - inTxWant: "alter table `user` add index a (id)", + logWant: "alter table `user` add key a (id)", + inTxWant: "alter table `user` add key a (id)", }, { input: "create index a on user(id1 + id2)", dbResponses: []dbResponse{{ diff --git a/go/vt/wrangler/materializer_test.go b/go/vt/wrangler/materializer_test.go index 91dfff80d11..242bca31e49 100644 --- a/go/vt/wrangler/materializer_test.go +++ b/go/vt/wrangler/materializer_test.go @@ -2838,7 +2838,7 @@ func TestStripForeignKeys(t *testing.T) { "\tid int(11) not null auto_increment,\n" + "\tforeign_id int(11),\n" + "\tprimary key (id),\n" + - "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + + "\tkey fk_table1_ref_foreign_id (foreign_id),\n" + "\tcheck (foreign_id > 10)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", @@ -2861,8 +2861,8 @@ func TestStripForeignKeys(t *testing.T) { "\tforeign_id int(11) not null,\n" + "\tuser_id int(11) not null,\n" + "\tprimary key (id),\n" + - "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + - "\tindex fk_table1_ref_user_id (user_id),\n" + + "\tkey fk_table1_ref_foreign_id (foreign_id),\n" + + "\tkey fk_table1_ref_user_id (user_id),\n" + "\tcheck (foreign_id > 10)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", @@ -2907,8 +2907,8 @@ func TestStripConstraints(t *testing.T) { "\tforeign_id int(11) not null,\n" + "\tuser_id int(11) not null,\n" + "\tprimary key (id),\n" + - "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + - "\tindex fk_table1_ref_user_id (user_id)\n" + + "\tkey fk_table1_ref_foreign_id (foreign_id),\n" + + "\tkey fk_table1_ref_user_id (user_id)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", @@ -2930,8 +2930,8 @@ func TestStripConstraints(t *testing.T) { "\tforeign_id int(11) not null,\n" + "\tuser_id int(11) not null,\n" + "\tprimary key (id),\n" + - "\tindex fk_table1_ref_foreign_id (foreign_id),\n" + - "\tindex fk_table1_ref_user_id (user_id)\n" + + "\tkey fk_table1_ref_foreign_id (foreign_id),\n" + + "\tkey fk_table1_ref_user_id (user_id)\n" + ") ENGINE InnoDB,\n" + " CHARSET latin1", },