From a5188a8370ab209b31e01458beee3f7a892f2174 Mon Sep 17 00:00:00 2001 From: winkyao Date: Thu, 21 Feb 2019 14:59:50 +0800 Subject: [PATCH] ddl: return warnings when the alter algorithm is COPY, return errors when the algorithm is wrong (#9367) --- ddl/db_integration_test.go | 208 ++++++++++++++++++------------------- ddl/db_partition_test.go | 70 ++++++------- ddl/db_test.go | 120 ++++++++++----------- ddl/ddl_api.go | 10 +- 4 files changed, 204 insertions(+), 204 deletions(-) diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 786401ff773b0..6f69f2d9bc820 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -102,11 +102,11 @@ func (s *testIntegrationSuite) TestNoZeroDateMode(c *C) { tk.MustExec("use test;") tk.MustExec("set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION';") - s.testErrorCode(c, tk, "create table test_zero_date(agent_start_time date NOT NULL DEFAULT '0000-00-00')", mysql.ErrInvalidDefault) - s.testErrorCode(c, tk, "create table test_zero_date(agent_start_time datetime NOT NULL DEFAULT '0000-00-00 00:00:00')", mysql.ErrInvalidDefault) - s.testErrorCode(c, tk, "create table test_zero_date(agent_start_time timestamp NOT NULL DEFAULT '0000-00-00 00:00:00')", mysql.ErrInvalidDefault) - s.testErrorCode(c, tk, "create table test_zero_date(a timestamp default '0000-00-00 00');", mysql.ErrInvalidDefault) - s.testErrorCode(c, tk, "create table test_zero_date(a timestamp default 0);", mysql.ErrInvalidDefault) + assertErrorCode(c, tk, "create table test_zero_date(agent_start_time date NOT NULL DEFAULT '0000-00-00')", mysql.ErrInvalidDefault) + assertErrorCode(c, tk, "create table test_zero_date(agent_start_time datetime NOT NULL DEFAULT '0000-00-00 00:00:00')", mysql.ErrInvalidDefault) + assertErrorCode(c, tk, "create table test_zero_date(agent_start_time timestamp NOT NULL DEFAULT '0000-00-00 00:00:00')", mysql.ErrInvalidDefault) + assertErrorCode(c, tk, "create table test_zero_date(a timestamp default '0000-00-00 00');", mysql.ErrInvalidDefault) + assertErrorCode(c, tk, "create table test_zero_date(a timestamp default 0);", mysql.ErrInvalidDefault) } func (s *testIntegrationSuite) TestInvalidDefault(c *C) { @@ -194,15 +194,6 @@ func (s *testIntegrationSuite) TestEndIncluded(c *C) { tk.MustExec("admin check table t") } -func (s *testIntegrationSuite) testErrorCode(c *C, tk *testkit.TestKit, sql string, errCode int) { - _, err := tk.Exec(sql) - c.Assert(err, NotNil) - originErr := errors.Cause(err) - tErr, ok := originErr.(*terror.Error) - c.Assert(ok, IsTrue, Commentf("err: %T", originErr)) - c.Assert(tErr.ToSQLError().Code, DeepEquals, uint16(errCode), Commentf("MySQL code:%v", tErr.ToSQLError())) -} - // TestModifyColumnAfterAddIndex Issue 5134 func (s *testIntegrationSuite) TestModifyColumnAfterAddIndex(c *C) { tk := testkit.NewTestKit(c, s.store) @@ -217,7 +208,7 @@ func (s *testIntegrationSuite) TestIssue2293(c *C) { tk.MustExec("use test") tk.MustExec("create table t_issue_2293 (a int)") sql := "alter table t_issue_2293 add b int not null default 'a'" - s.testErrorCode(c, tk, sql, tmysql.ErrInvalidDefault) + assertErrorCode(c, tk, sql, tmysql.ErrInvalidDefault) tk.MustExec("insert into t_issue_2293 value(1)") tk.MustQuery("select * from t_issue_2293").Check(testkit.Rows("1")) } @@ -242,9 +233,9 @@ func (s *testIntegrationSuite) TestIssue3833(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("create table issue3833 (b char(0))") - s.testErrorCode(c, tk, "create index idx on issue3833 (b)", tmysql.ErrWrongKeyColumn) - s.testErrorCode(c, tk, "alter table issue3833 add index idx (b)", tmysql.ErrWrongKeyColumn) - s.testErrorCode(c, tk, "create table issue3833_2 (b char(0), index (b))", tmysql.ErrWrongKeyColumn) + assertErrorCode(c, tk, "create index idx on issue3833 (b)", tmysql.ErrWrongKeyColumn) + assertErrorCode(c, tk, "alter table issue3833 add index idx (b)", tmysql.ErrWrongKeyColumn) + assertErrorCode(c, tk, "create table issue3833_2 (b char(0), index (b))", tmysql.ErrWrongKeyColumn) } func (s *testIntegrationSuite) TestIssue2858And2717(c *C) { @@ -295,129 +286,129 @@ func (s *testIntegrationSuite) TestMySQLErrorCode(c *C) { // create database sql := "create database aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - s.testErrorCode(c, tk, sql, tmysql.ErrTooLongIdent) + assertErrorCode(c, tk, sql, tmysql.ErrTooLongIdent) sql = "create database test" - s.testErrorCode(c, tk, sql, tmysql.ErrDBCreateExists) + assertErrorCode(c, tk, sql, tmysql.ErrDBCreateExists) sql = "create database test1 character set uft8;" - s.testErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet) + assertErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet) sql = "create database test2 character set gkb;" - s.testErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet) + assertErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet) sql = "create database test3 character set laitn1;" - s.testErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet) + assertErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet) // drop database sql = "drop database db_not_exist" - s.testErrorCode(c, tk, sql, tmysql.ErrDBDropExists) + assertErrorCode(c, tk, sql, tmysql.ErrDBDropExists) // create table tk.MustExec("create table test_error_code_succ (c1 int, c2 int, c3 int, primary key(c3))") sql = "create table test_error_code_succ (c1 int, c2 int, c3 int)" - s.testErrorCode(c, tk, sql, tmysql.ErrTableExists) + assertErrorCode(c, tk, sql, tmysql.ErrTableExists) sql = "create table test_error_code1 (c1 int, c2 int, c2 int)" - s.testErrorCode(c, tk, sql, tmysql.ErrDupFieldName) + assertErrorCode(c, tk, sql, tmysql.ErrDupFieldName) sql = "create table test_error_code1 (c1 int, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa int)" - s.testErrorCode(c, tk, sql, tmysql.ErrTooLongIdent) + assertErrorCode(c, tk, sql, tmysql.ErrTooLongIdent) sql = "create table aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(a int)" - s.testErrorCode(c, tk, sql, tmysql.ErrTooLongIdent) + assertErrorCode(c, tk, sql, tmysql.ErrTooLongIdent) sql = "create table test_error_code1 (c1 int, c2 int, key aa (c1, c2), key aa (c1))" - s.testErrorCode(c, tk, sql, tmysql.ErrDupKeyName) + assertErrorCode(c, tk, sql, tmysql.ErrDupKeyName) sql = "create table test_error_code1 (c1 int, c2 int, c3 int, key(c_not_exist))" - s.testErrorCode(c, tk, sql, tmysql.ErrKeyColumnDoesNotExits) + assertErrorCode(c, tk, sql, tmysql.ErrKeyColumnDoesNotExits) sql = "create table test_error_code1 (c1 int, c2 int, c3 int, primary key(c_not_exist))" - s.testErrorCode(c, tk, sql, tmysql.ErrKeyColumnDoesNotExits) + assertErrorCode(c, tk, sql, tmysql.ErrKeyColumnDoesNotExits) sql = "create table test_error_code1 (c1 int not null default '')" - s.testErrorCode(c, tk, sql, tmysql.ErrInvalidDefault) + assertErrorCode(c, tk, sql, tmysql.ErrInvalidDefault) sql = "CREATE TABLE `t` (`a` double DEFAULT 1.0 DEFAULT 2.0 DEFAULT now());" - s.testErrorCode(c, tk, sql, tmysql.ErrInvalidDefault) + assertErrorCode(c, tk, sql, tmysql.ErrInvalidDefault) sql = "CREATE TABLE `t` (`a` double DEFAULT now());" - s.testErrorCode(c, tk, sql, tmysql.ErrInvalidDefault) + assertErrorCode(c, tk, sql, tmysql.ErrInvalidDefault) sql = "create table t1(a int) character set uft8;" - s.testErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet) + assertErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet) sql = "create table t1(a int) character set gkb;" - s.testErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet) + assertErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet) sql = "create table t1(a int) character set laitn1;" - s.testErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet) + assertErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet) sql = "create table test_error_code (a int not null ,b int not null,c int not null, d int not null, foreign key (b, c) references product(id));" - s.testErrorCode(c, tk, sql, tmysql.ErrWrongFkDef) + assertErrorCode(c, tk, sql, tmysql.ErrWrongFkDef) sql = "create table test_error_code_2;" - s.testErrorCode(c, tk, sql, tmysql.ErrTableMustHaveColumns) + assertErrorCode(c, tk, sql, tmysql.ErrTableMustHaveColumns) sql = "create table test_error_code_2 (unique(c1));" - s.testErrorCode(c, tk, sql, tmysql.ErrTableMustHaveColumns) + assertErrorCode(c, tk, sql, tmysql.ErrTableMustHaveColumns) sql = "create table test_error_code_2(c1 int, c2 int, c3 int, primary key(c1), primary key(c2));" - s.testErrorCode(c, tk, sql, tmysql.ErrMultiplePriKey) + assertErrorCode(c, tk, sql, tmysql.ErrMultiplePriKey) sql = "create table test_error_code_3(pt blob ,primary key (pt));" - s.testErrorCode(c, tk, sql, tmysql.ErrBlobKeyWithoutLength) + assertErrorCode(c, tk, sql, tmysql.ErrBlobKeyWithoutLength) sql = "create table test_error_code_3(a text, unique (a(3073)));" - s.testErrorCode(c, tk, sql, tmysql.ErrTooLongKey) + assertErrorCode(c, tk, sql, tmysql.ErrTooLongKey) sql = "create table test_error_code_3(`id` int, key `primary`(`id`));" - s.testErrorCode(c, tk, sql, tmysql.ErrWrongNameForIndex) + assertErrorCode(c, tk, sql, tmysql.ErrWrongNameForIndex) sql = "create table t2(c1.c2 blob default null);" - s.testErrorCode(c, tk, sql, tmysql.ErrWrongTableName) + assertErrorCode(c, tk, sql, tmysql.ErrWrongTableName) sql = "create table t2 (id int default null primary key , age int);" - s.testErrorCode(c, tk, sql, tmysql.ErrInvalidDefault) + assertErrorCode(c, tk, sql, tmysql.ErrInvalidDefault) sql = "create table t2 (id int null primary key , age int);" - s.testErrorCode(c, tk, sql, tmysql.ErrPrimaryCantHaveNull) + assertErrorCode(c, tk, sql, tmysql.ErrPrimaryCantHaveNull) sql = "create table t2 (id int default null, age int, primary key(id));" - s.testErrorCode(c, tk, sql, tmysql.ErrPrimaryCantHaveNull) + assertErrorCode(c, tk, sql, tmysql.ErrPrimaryCantHaveNull) sql = "create table t2 (id int null, age int, primary key(id));" - s.testErrorCode(c, tk, sql, tmysql.ErrPrimaryCantHaveNull) + assertErrorCode(c, tk, sql, tmysql.ErrPrimaryCantHaveNull) sql = "create table t2 (id int primary key , age int);" tk.MustExec(sql) // add column sql = "alter table test_error_code_succ add column c1 int" - s.testErrorCode(c, tk, sql, tmysql.ErrDupFieldName) + assertErrorCode(c, tk, sql, tmysql.ErrDupFieldName) sql = "alter table test_error_code_succ add column aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa int" - s.testErrorCode(c, tk, sql, tmysql.ErrTooLongIdent) + assertErrorCode(c, tk, sql, tmysql.ErrTooLongIdent) sql = "alter table test_comment comment 'test comment'" - s.testErrorCode(c, tk, sql, tmysql.ErrNoSuchTable) + assertErrorCode(c, tk, sql, tmysql.ErrNoSuchTable) sql = "alter table test_error_code_succ add column `a ` int ;" - s.testErrorCode(c, tk, sql, tmysql.ErrWrongColumnName) + assertErrorCode(c, tk, sql, tmysql.ErrWrongColumnName) tk.MustExec("create table test_on_update (c1 int, c2 int);") sql = "alter table test_on_update add column c3 int on update current_timestamp;" - s.testErrorCode(c, tk, sql, tmysql.ErrInvalidOnUpdate) + assertErrorCode(c, tk, sql, tmysql.ErrInvalidOnUpdate) sql = "create table test_on_update_2(c int on update current_timestamp);" - s.testErrorCode(c, tk, sql, tmysql.ErrInvalidOnUpdate) + assertErrorCode(c, tk, sql, tmysql.ErrInvalidOnUpdate) // drop column sql = "alter table test_error_code_succ drop c_not_exist" - s.testErrorCode(c, tk, sql, tmysql.ErrCantDropFieldOrKey) + assertErrorCode(c, tk, sql, tmysql.ErrCantDropFieldOrKey) tk.MustExec("create table test_drop_column (c1 int );") sql = "alter table test_drop_column drop column c1;" - s.testErrorCode(c, tk, sql, tmysql.ErrCantRemoveAllFields) + assertErrorCode(c, tk, sql, tmysql.ErrCantRemoveAllFields) // add index sql = "alter table test_error_code_succ add index idx (c_not_exist)" - s.testErrorCode(c, tk, sql, tmysql.ErrKeyColumnDoesNotExits) + assertErrorCode(c, tk, sql, tmysql.ErrKeyColumnDoesNotExits) tk.MustExec("alter table test_error_code_succ add index idx (c1)") sql = "alter table test_error_code_succ add index idx (c1)" - s.testErrorCode(c, tk, sql, tmysql.ErrDupKeyName) + assertErrorCode(c, tk, sql, tmysql.ErrDupKeyName) // drop index sql = "alter table test_error_code_succ drop index idx_not_exist" - s.testErrorCode(c, tk, sql, tmysql.ErrCantDropFieldOrKey) + assertErrorCode(c, tk, sql, tmysql.ErrCantDropFieldOrKey) sql = "alter table test_error_code_succ drop column c3" - s.testErrorCode(c, tk, sql, int(tmysql.ErrUnknown)) + assertErrorCode(c, tk, sql, int(tmysql.ErrUnknown)) // modify column sql = "alter table test_error_code_succ modify testx.test_error_code_succ.c1 bigint" - s.testErrorCode(c, tk, sql, tmysql.ErrWrongDBName) + assertErrorCode(c, tk, sql, tmysql.ErrWrongDBName) sql = "alter table test_error_code_succ modify t.c1 bigint" - s.testErrorCode(c, tk, sql, tmysql.ErrWrongTableName) + assertErrorCode(c, tk, sql, tmysql.ErrWrongTableName) // insert value tk.MustExec("create table test_error_code_null(c1 char(100) not null);") sql = "insert into test_error_code_null (c1) values(null);" - s.testErrorCode(c, tk, sql, tmysql.ErrBadNull) + assertErrorCode(c, tk, sql, tmysql.ErrBadNull) } func (s *testIntegrationSuite) TestTableDDLWithFloatType(c *C) { s.tk = testkit.NewTestKit(c, s.store) s.tk.MustExec("use test") s.tk.MustExec("drop table if exists t") - s.testErrorCode(c, s.tk, "create table t (a decimal(1, 2))", tmysql.ErrMBiggerThanD) - s.testErrorCode(c, s.tk, "create table t (a float(1, 2))", tmysql.ErrMBiggerThanD) - s.testErrorCode(c, s.tk, "create table t (a double(1, 2))", tmysql.ErrMBiggerThanD) + assertErrorCode(c, s.tk, "create table t (a decimal(1, 2))", tmysql.ErrMBiggerThanD) + assertErrorCode(c, s.tk, "create table t (a float(1, 2))", tmysql.ErrMBiggerThanD) + assertErrorCode(c, s.tk, "create table t (a double(1, 2))", tmysql.ErrMBiggerThanD) s.tk.MustExec("create table t (a double(1, 1))") - s.testErrorCode(c, s.tk, "alter table t add column b decimal(1, 2)", tmysql.ErrMBiggerThanD) + assertErrorCode(c, s.tk, "alter table t add column b decimal(1, 2)", tmysql.ErrMBiggerThanD) // add multi columns now not support, so no case. - s.testErrorCode(c, s.tk, "alter table t modify column a float(1, 4)", tmysql.ErrMBiggerThanD) - s.testErrorCode(c, s.tk, "alter table t change column a aa float(1, 4)", tmysql.ErrMBiggerThanD) + assertErrorCode(c, s.tk, "alter table t modify column a float(1, 4)", tmysql.ErrMBiggerThanD) + assertErrorCode(c, s.tk, "alter table t change column a aa float(1, 4)", tmysql.ErrMBiggerThanD) s.tk.MustExec("drop table t") } @@ -425,21 +416,21 @@ func (s *testIntegrationSuite) TestTableDDLWithTimeType(c *C) { s.tk = testkit.NewTestKit(c, s.store) s.tk.MustExec("use test") s.tk.MustExec("drop table if exists t") - s.testErrorCode(c, s.tk, "create table t (a time(7))", tmysql.ErrTooBigPrecision) - s.testErrorCode(c, s.tk, "create table t (a datetime(7))", tmysql.ErrTooBigPrecision) - s.testErrorCode(c, s.tk, "create table t (a timestamp(7))", tmysql.ErrTooBigPrecision) + assertErrorCode(c, s.tk, "create table t (a time(7))", tmysql.ErrTooBigPrecision) + assertErrorCode(c, s.tk, "create table t (a datetime(7))", tmysql.ErrTooBigPrecision) + assertErrorCode(c, s.tk, "create table t (a timestamp(7))", tmysql.ErrTooBigPrecision) _, err := s.tk.Exec("create table t (a time(-1))") c.Assert(err, NotNil) s.tk.MustExec("create table t (a datetime)") - s.testErrorCode(c, s.tk, "alter table t add column b time(7)", tmysql.ErrTooBigPrecision) - s.testErrorCode(c, s.tk, "alter table t add column b datetime(7)", tmysql.ErrTooBigPrecision) - s.testErrorCode(c, s.tk, "alter table t add column b timestamp(7)", tmysql.ErrTooBigPrecision) - s.testErrorCode(c, s.tk, "alter table t modify column a time(7)", tmysql.ErrTooBigPrecision) - s.testErrorCode(c, s.tk, "alter table t modify column a datetime(7)", tmysql.ErrTooBigPrecision) - s.testErrorCode(c, s.tk, "alter table t modify column a timestamp(7)", tmysql.ErrTooBigPrecision) - s.testErrorCode(c, s.tk, "alter table t change column a aa time(7)", tmysql.ErrTooBigPrecision) - s.testErrorCode(c, s.tk, "alter table t change column a aa datetime(7)", tmysql.ErrTooBigPrecision) - s.testErrorCode(c, s.tk, "alter table t change column a aa timestamp(7)", tmysql.ErrTooBigPrecision) + assertErrorCode(c, s.tk, "alter table t add column b time(7)", tmysql.ErrTooBigPrecision) + assertErrorCode(c, s.tk, "alter table t add column b datetime(7)", tmysql.ErrTooBigPrecision) + assertErrorCode(c, s.tk, "alter table t add column b timestamp(7)", tmysql.ErrTooBigPrecision) + assertErrorCode(c, s.tk, "alter table t modify column a time(7)", tmysql.ErrTooBigPrecision) + assertErrorCode(c, s.tk, "alter table t modify column a datetime(7)", tmysql.ErrTooBigPrecision) + assertErrorCode(c, s.tk, "alter table t modify column a timestamp(7)", tmysql.ErrTooBigPrecision) + assertErrorCode(c, s.tk, "alter table t change column a aa time(7)", tmysql.ErrTooBigPrecision) + assertErrorCode(c, s.tk, "alter table t change column a aa datetime(7)", tmysql.ErrTooBigPrecision) + assertErrorCode(c, s.tk, "alter table t change column a aa timestamp(7)", tmysql.ErrTooBigPrecision) s.tk.MustExec("alter table t change column a aa datetime(0)") s.tk.MustExec("drop table t") } @@ -941,7 +932,7 @@ func (s *testIntegrationSuite) TestCreateTableTooLarge(c *C) { } } sql += ");" - s.testErrorCode(c, s.tk, sql, tmysql.ErrTooManyFields) + assertErrorCode(c, s.tk, sql, tmysql.ErrTooManyFields) originLimit := atomic.LoadUint32(&ddl.TableColumnCountLimit) atomic.StoreUint32(&ddl.TableColumnCountLimit, uint32(cnt*4)) @@ -971,7 +962,7 @@ func (s *testIntegrationSuite) TestChangeColumnPosition(c *C) { s.tk.MustQuery("select * from position1").Check(testkit.Rows("TiDB 2 3.14 1")) s.tk.MustExec("alter table position1 modify column c double first") s.tk.MustQuery("select * from position1").Check(testkit.Rows("3.14 TiDB 2 1")) - s.testErrorCode(c, s.tk, "alter table position1 modify column b int after b", tmysql.ErrBadField) + assertErrorCode(c, s.tk, "alter table position1 modify column b int after b", tmysql.ErrBadField) s.tk.MustExec("create table position2 (a int, b int)") s.tk.MustExec("alter table position2 add index t(a, b)") @@ -980,7 +971,7 @@ func (s *testIntegrationSuite) TestChangeColumnPosition(c *C) { s.tk.MustQuery("select a from position2 where a = 3").Check(testkit.Rows()) s.tk.MustExec("alter table position2 change column b c int first") s.tk.MustQuery("select * from position2 where c = 3").Check(testkit.Rows("3 5")) - s.testErrorCode(c, s.tk, "alter table position2 change column c b int after c", tmysql.ErrBadField) + assertErrorCode(c, s.tk, "alter table position2 change column c b int after c", tmysql.ErrBadField) s.tk.MustExec("create table position3 (a int default 2)") s.tk.MustExec("alter table position3 modify column a int default 5 first") @@ -1009,9 +1000,9 @@ func (s *testIntegrationSuite) TestAddIndexAfterAddColumn(c *C) { s.tk.MustExec("insert into test_add_index_after_add_col values(1, 2),(2,2)") s.tk.MustExec("alter table test_add_index_after_add_col add column c int not null default '0'") sql := "alter table test_add_index_after_add_col add unique index cc(c) " - s.testErrorCode(c, s.tk, sql, tmysql.ErrDupEntry) + assertErrorCode(c, s.tk, sql, tmysql.ErrDupEntry) sql = "alter table test_add_index_after_add_col add index idx_test(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17);" - s.testErrorCode(c, s.tk, sql, tmysql.ErrTooManyKeyParts) + assertErrorCode(c, s.tk, sql, tmysql.ErrTooManyKeyParts) } func (s *testIntegrationSuite) TestResolveCharset(c *C) { @@ -1106,7 +1097,7 @@ func (s *testIntegrationSuite) TestAddColumnTooMany(c *C) { s.tk.MustExec(createSQL) s.tk.MustExec("alter table t_column_too_many add column a_512 int") alterSQL := "alter table t_column_too_many add column a_513 int" - s.testErrorCode(c, s.tk, alterSQL, tmysql.ErrTooManyFields) + assertErrorCode(c, s.tk, alterSQL, tmysql.ErrTooManyFields) } func (s *testIntegrationSuite) TestAlterColumn(c *C) { @@ -1162,13 +1153,13 @@ func (s *testIntegrationSuite) TestAlterColumn(c *C) { // for failing tests sql := "alter table db_not_exist.test_alter_column alter column b set default 'c'" - s.testErrorCode(c, s.tk, sql, tmysql.ErrNoSuchTable) + assertErrorCode(c, s.tk, sql, tmysql.ErrNoSuchTable) sql = "alter table test_not_exist alter column b set default 'c'" - s.testErrorCode(c, s.tk, sql, tmysql.ErrNoSuchTable) + assertErrorCode(c, s.tk, sql, tmysql.ErrNoSuchTable) sql = "alter table test_alter_column alter column col_not_exist set default 'c'" - s.testErrorCode(c, s.tk, sql, tmysql.ErrBadField) + assertErrorCode(c, s.tk, sql, tmysql.ErrBadField) sql = "alter table test_alter_column alter column c set default null" - s.testErrorCode(c, s.tk, sql, tmysql.ErrInvalidDefault) + assertErrorCode(c, s.tk, sql, tmysql.ErrInvalidDefault) // The followings tests whether adding constraints via change / modify column // is forbidden as expected. @@ -1223,6 +1214,10 @@ func (s *testIntegrationSuite) assertAlterWarnExec(c *C, sql string) { s.assertWarningExec(c, sql, ddl.ErrAlterOperationNotSupported) } +func (s *testIntegrationSuite) assertAlterErrorExec(c *C, sql string) { + assertErrorCode(c, s.tk, sql, mysql.ErrAlterOperationNotSupportedReason) +} + func (s *testIntegrationSuite) TestAlterAlgorithm(c *C) { s.tk = testkit.NewTestKit(c, s.store) s.tk.MustExec("use test") @@ -1240,49 +1235,50 @@ func (s *testIntegrationSuite) TestAlterAlgorithm(c *C) { PARTITION p2 VALUES LESS THAN (16), PARTITION p3 VALUES LESS THAN (21) )`) - s.assertAlterWarnExec(c, "alter table t modify column a bigint, ALGORITHM=INPLACE;") + s.assertAlterErrorExec(c, "alter table t modify column a bigint, ALGORITHM=INPLACE;") s.tk.MustExec("alter table t modify column a bigint, ALGORITHM=INPLACE, ALGORITHM=INSTANT;") s.tk.MustExec("alter table t modify column a bigint, ALGORITHM=DEFAULT;") // Test add/drop index - s.assertAlterWarnExec(c, "alter table t add index idx_b(b), ALGORITHM=INSTANT") + s.assertAlterErrorExec(c, "alter table t add index idx_b(b), ALGORITHM=INSTANT") s.assertAlterWarnExec(c, "alter table t add index idx_b1(b), ALGORITHM=COPY") s.tk.MustExec("alter table t add index idx_b2(b), ALGORITHM=INPLACE") - s.assertAlterWarnExec(c, "alter table t drop index idx_b, ALGORITHM=INPLACE") + s.assertAlterErrorExec(c, "alter table t drop index idx_b, ALGORITHM=INPLACE") s.assertAlterWarnExec(c, "alter table t drop index idx_b1, ALGORITHM=COPY") s.tk.MustExec("alter table t drop index idx_b2, ALGORITHM=INSTANT") // Test rename s.assertAlterWarnExec(c, "alter table t rename to t1, ALGORITHM=COPY") - s.assertAlterWarnExec(c, "alter table t1 rename to t, ALGORITHM=INPLACE") - s.tk.MustExec("alter table t rename to t1, ALGORITHM=INSTANT") - s.tk.MustExec("alter table t1 rename to t, ALGORITHM=DEFAULT") + s.assertAlterErrorExec(c, "alter table t1 rename to t, ALGORITHM=INPLACE") + s.tk.MustExec("alter table t1 rename to t, ALGORITHM=INSTANT") + s.tk.MustExec("alter table t rename to t1, ALGORITHM=DEFAULT") + s.tk.MustExec("alter table t1 rename to t") // Test rename index s.assertAlterWarnExec(c, "alter table t rename index idx_c to idx_c1, ALGORITHM=COPY") - s.assertAlterWarnExec(c, "alter table t rename index idx_c1 to idx_c, ALGORITHM=INPLACE") - s.tk.MustExec("alter table t rename index idx_c to idx_c1, ALGORITHM=INSTANT") - s.tk.MustExec("alter table t rename index idx_c1 to idx_c, ALGORITHM=DEFAULT") + s.assertAlterErrorExec(c, "alter table t rename index idx_c1 to idx_c, ALGORITHM=INPLACE") + s.tk.MustExec("alter table t rename index idx_c1 to idx_c, ALGORITHM=INSTANT") + s.tk.MustExec("alter table t rename index idx_c to idx_c1, ALGORITHM=DEFAULT") // partition. s.assertAlterWarnExec(c, "alter table t truncate partition p1, ALGORITHM=COPY") - s.assertAlterWarnExec(c, "alter table t truncate partition p2, ALGORITHM=INPLACE") + s.assertAlterErrorExec(c, "alter table t truncate partition p2, ALGORITHM=INPLACE") s.tk.MustExec("alter table t truncate partition p3, ALGORITHM=INSTANT") s.assertAlterWarnExec(c, "alter table t add partition (partition p4 values less than (2002)), ALGORITHM=COPY") - s.assertAlterWarnExec(c, "alter table t add partition (partition p5 values less than (3002)), ALGORITHM=INPLACE") + s.assertAlterErrorExec(c, "alter table t add partition (partition p5 values less than (3002)), ALGORITHM=INPLACE") s.tk.MustExec("alter table t add partition (partition p6 values less than (4002)), ALGORITHM=INSTANT") s.assertAlterWarnExec(c, "alter table t drop partition p4, ALGORITHM=COPY") - s.assertAlterWarnExec(c, "alter table t drop partition p5, ALGORITHM=INPLACE") + s.assertAlterErrorExec(c, "alter table t drop partition p5, ALGORITHM=INPLACE") s.tk.MustExec("alter table t drop partition p6, ALGORITHM=INSTANT") // Table options s.assertAlterWarnExec(c, "alter table t comment = 'test', ALGORITHM=COPY") - s.assertAlterWarnExec(c, "alter table t comment = 'test', ALGORITHM=INPLACE") + s.assertAlterErrorExec(c, "alter table t comment = 'test', ALGORITHM=INPLACE") s.tk.MustExec("alter table t comment = 'test', ALGORITHM=INSTANT") s.assertAlterWarnExec(c, "alter table t default charset = utf8mb4, ALGORITHM=COPY") - s.assertAlterWarnExec(c, "alter table t default charset = utf8mb4, ALGORITHM=INPLACE") + s.assertAlterErrorExec(c, "alter table t default charset = utf8mb4, ALGORITHM=INPLACE") s.tk.MustExec("alter table t default charset = utf8mb4, ALGORITHM=INSTANT") } diff --git a/ddl/db_partition_test.go b/ddl/db_partition_test.go index 8d2a377b20515..8f44ce6606e5b 100644 --- a/ddl/db_partition_test.go +++ b/ddl/db_partition_test.go @@ -79,7 +79,7 @@ func (s *testIntegrationSuite) TestCreateTableWithPartition(c *C) { partition p2 values less than (1996), partition p2 values less than (2001) );` - s.testErrorCode(c, tk, sql1, tmysql.ErrSameNamePartition) + assertErrorCode(c, tk, sql1, tmysql.ErrSameNamePartition) sql2 := `create table employees ( id int not null, @@ -90,7 +90,7 @@ func (s *testIntegrationSuite) TestCreateTableWithPartition(c *C) { partition p2 values less than (1996), partition p3 values less than (2001) );` - s.testErrorCode(c, tk, sql2, tmysql.ErrRangeNotIncreasing) + assertErrorCode(c, tk, sql2, tmysql.ErrRangeNotIncreasing) sql3 := `create table employees ( id int not null, @@ -101,7 +101,7 @@ func (s *testIntegrationSuite) TestCreateTableWithPartition(c *C) { partition p2 values less than maxvalue, partition p3 values less than (2001) );` - s.testErrorCode(c, tk, sql3, tmysql.ErrPartitionMaxvalue) + assertErrorCode(c, tk, sql3, tmysql.ErrPartitionMaxvalue) sql4 := `create table t4 ( a int not null, @@ -112,7 +112,7 @@ func (s *testIntegrationSuite) TestCreateTableWithPartition(c *C) { partition p2 values less than (1991), partition p3 values less than (1995) );` - s.testErrorCode(c, tk, sql4, tmysql.ErrPartitionMaxvalue) + assertErrorCode(c, tk, sql4, tmysql.ErrPartitionMaxvalue) _, err = tk.Exec(`CREATE TABLE rc ( a INT NOT NULL, @@ -134,7 +134,7 @@ func (s *testIntegrationSuite) TestCreateTableWithPartition(c *C) { partition by range( hired ) ( partition p0 values less than (6 , 10) );` - s.testErrorCode(c, tk, sql6, tmysql.ErrTooManyValues) + assertErrorCode(c, tk, sql6, tmysql.ErrTooManyValues) sql7 := `create table t7 ( a int not null, @@ -147,7 +147,7 @@ func (s *testIntegrationSuite) TestCreateTableWithPartition(c *C) { partition p4 values less than (1995), partition p5 values less than maxvalue );` - s.testErrorCode(c, tk, sql7, tmysql.ErrPartitionMaxvalue) + assertErrorCode(c, tk, sql7, tmysql.ErrPartitionMaxvalue) _, err = tk.Exec(`create table t8 ( a int not null, @@ -166,9 +166,9 @@ func (s *testIntegrationSuite) TestCreateTableWithPartition(c *C) { partition p0 values less than (2), partition p1 values less than (6) );` - s.testErrorCode(c, tk, sql9, tmysql.ErrPartitionFunctionIsNotAllowed) + assertErrorCode(c, tk, sql9, tmysql.ErrPartitionFunctionIsNotAllowed) - s.testErrorCode(c, tk, `create TABLE t10 (c1 int,c2 int) partition by range(c1 / c2 ) (partition p0 values less than (2));`, tmysql.ErrPartitionFunctionIsNotAllowed) + assertErrorCode(c, tk, `create TABLE t10 (c1 int,c2 int) partition by range(c1 / c2 ) (partition p0 values less than (2));`, tmysql.ErrPartitionFunctionIsNotAllowed) tk.MustExec(`create TABLE t11 (c1 int,c2 int) partition by range(c1 div c2 ) (partition p0 values less than (2));`) tk.MustExec(`create TABLE t12 (c1 int,c2 int) partition by range(c1 + c2 ) (partition p0 values less than (2));`) @@ -177,17 +177,17 @@ func (s *testIntegrationSuite) TestCreateTableWithPartition(c *C) { tk.MustExec(`create TABLE t15 (c1 int,c2 int) partition by range( abs(c1) ) (partition p0 values less than (2));`) tk.MustExec(`create TABLE t16 (c1 int) partition by range( c1) (partition p0 values less than (10));`) - s.testErrorCode(c, tk, `create TABLE t17 (c1 int,c2 float) partition by range(c1 + c2 ) (partition p0 values less than (2));`, tmysql.ErrPartitionFuncNotAllowed) - s.testErrorCode(c, tk, `create TABLE t18 (c1 int,c2 float) partition by range( floor(c2) ) (partition p0 values less than (2));`, tmysql.ErrPartitionFuncNotAllowed) + assertErrorCode(c, tk, `create TABLE t17 (c1 int,c2 float) partition by range(c1 + c2 ) (partition p0 values less than (2));`, tmysql.ErrPartitionFuncNotAllowed) + assertErrorCode(c, tk, `create TABLE t18 (c1 int,c2 float) partition by range( floor(c2) ) (partition p0 values less than (2));`, tmysql.ErrPartitionFuncNotAllowed) tk.MustExec(`create TABLE t19 (c1 int,c2 float) partition by range( floor(c1) ) (partition p0 values less than (2));`) tk.MustExec(`create TABLE t20 (c1 int,c2 bit(10)) partition by range(c2) (partition p0 values less than (10));`) tk.MustExec(`create TABLE t21 (c1 int,c2 year) partition by range( c2 ) (partition p0 values less than (2000));`) - s.testErrorCode(c, tk, `create TABLE t24 (c1 float) partition by range( c1 ) (partition p0 values less than (2000));`, tmysql.ErrFieldTypeNotAllowedAsPartitionField) + assertErrorCode(c, tk, `create TABLE t24 (c1 float) partition by range( c1 ) (partition p0 values less than (2000));`, tmysql.ErrFieldTypeNotAllowedAsPartitionField) // test check order. The sql below have 2 problem: 1. ErrFieldTypeNotAllowedAsPartitionField 2. ErrPartitionMaxvalue , mysql will return ErrPartitionMaxvalue. - s.testErrorCode(c, tk, `create TABLE t25 (c1 float) partition by range( c1 ) (partition p1 values less than maxvalue,partition p0 values less than (2000));`, tmysql.ErrPartitionMaxvalue) + assertErrorCode(c, tk, `create TABLE t25 (c1 float) partition by range( c1 ) (partition p1 values less than maxvalue,partition p0 values less than (2000));`, tmysql.ErrPartitionMaxvalue) // Fix issue 7362. tk.MustExec("create table test_partition(id bigint, name varchar(255), primary key(id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 PARTITION BY RANGE COLUMNS(id) (PARTITION p1 VALUES LESS THAN (10) ENGINE = InnoDB);") @@ -358,7 +358,7 @@ func (s *testIntegrationSuite) TestAlterTableAddPartition(c *C) { partition p1 values less than (2010), partition p2 values less than maxvalue );` - s.testErrorCode(c, tk, sql1, tmysql.ErrPartitionMgmtOnNonpartitioned) + assertErrorCode(c, tk, sql1, tmysql.ErrPartitionMgmtOnNonpartitioned) tk.MustExec(`create table table_MustBeDefined ( id int not null, hired date not null @@ -369,7 +369,7 @@ func (s *testIntegrationSuite) TestAlterTableAddPartition(c *C) { partition p3 values less than (2001) );`) sql2 := "alter table table_MustBeDefined add partition" - s.testErrorCode(c, tk, sql2, tmysql.ErrPartitionsMustBeDefined) + assertErrorCode(c, tk, sql2, tmysql.ErrPartitionsMustBeDefined) tk.MustExec("drop table if exists table2;") tk.MustExec(`create table table2 ( @@ -384,7 +384,7 @@ func (s *testIntegrationSuite) TestAlterTableAddPartition(c *C) { sql3 := `alter table table2 add partition ( partition p3 values less than (2010) );` - s.testErrorCode(c, tk, sql3, tmysql.ErrPartitionMaxvalue) + assertErrorCode(c, tk, sql3, tmysql.ErrPartitionMaxvalue) tk.MustExec("drop table if exists table3;") tk.MustExec(`create table table3 ( @@ -399,25 +399,25 @@ func (s *testIntegrationSuite) TestAlterTableAddPartition(c *C) { sql4 := `alter table table3 add partition ( partition p3 values less than (1993) );` - s.testErrorCode(c, tk, sql4, tmysql.ErrRangeNotIncreasing) + assertErrorCode(c, tk, sql4, tmysql.ErrRangeNotIncreasing) sql5 := `alter table table3 add partition ( partition p1 values less than (1993) );` - s.testErrorCode(c, tk, sql5, tmysql.ErrSameNamePartition) + assertErrorCode(c, tk, sql5, tmysql.ErrSameNamePartition) sql6 := `alter table table3 add partition ( partition p1 values less than (1993), partition p1 values less than (1995) );` - s.testErrorCode(c, tk, sql6, tmysql.ErrSameNamePartition) + assertErrorCode(c, tk, sql6, tmysql.ErrSameNamePartition) sql7 := `alter table table3 add partition ( partition p4 values less than (1993), partition p1 values less than (1995), partition p5 values less than maxvalue );` - s.testErrorCode(c, tk, sql7, tmysql.ErrSameNamePartition) + assertErrorCode(c, tk, sql7, tmysql.ErrSameNamePartition) } func (s *testIntegrationSuite) TestAlterTableDropPartition(c *C) { @@ -452,7 +452,7 @@ func (s *testIntegrationSuite) TestAlterTableDropPartition(c *C) { tk.MustExec("drop table if exists table1;") tk.MustExec("create table table1 (a int);") sql1 := "alter table table1 drop partition p10;" - s.testErrorCode(c, tk, sql1, tmysql.ErrPartitionMgmtOnNonpartitioned) + assertErrorCode(c, tk, sql1, tmysql.ErrPartitionMgmtOnNonpartitioned) tk.MustExec("drop table if exists table2;") tk.MustExec(`create table table2 ( @@ -465,7 +465,7 @@ func (s *testIntegrationSuite) TestAlterTableDropPartition(c *C) { partition p3 values less than (2001) );`) sql2 := "alter table table2 drop partition p10;" - s.testErrorCode(c, tk, sql2, tmysql.ErrDropPartitionNonExistent) + assertErrorCode(c, tk, sql2, tmysql.ErrDropPartitionNonExistent) tk.MustExec("drop table if exists table3;") tk.MustExec(`create table table3 ( @@ -475,7 +475,7 @@ func (s *testIntegrationSuite) TestAlterTableDropPartition(c *C) { partition p1 values less than (1991) );`) sql3 := "alter table table3 drop partition p1;" - s.testErrorCode(c, tk, sql3, tmysql.ErrDropLastPartition) + assertErrorCode(c, tk, sql3, tmysql.ErrDropLastPartition) tk.MustExec("drop table if exists table4;") tk.MustExec(`create table table4 ( @@ -554,7 +554,7 @@ func (s *testIntegrationSuite) TestAlterTableDropPartition(c *C) { tk.MustExec("alter table table4 drop partition Par2;") tk.MustExec("alter table table4 drop partition PAR5;") sql4 := "alter table table4 drop partition PAR0;" - s.testErrorCode(c, tk, sql4, tmysql.ErrDropPartitionNonExistent) + assertErrorCode(c, tk, sql4, tmysql.ErrDropPartitionNonExistent) } func (s *testIntegrationSuite) TestAddPartitionTooManyPartitions(c *C) { @@ -570,7 +570,7 @@ func (s *testIntegrationSuite) TestAddPartitionTooManyPartitions(c *C) { sql1 += fmt.Sprintf("partition p%d values less than (%d),", i, i) } sql1 += "partition p1025 values less than (1025) );" - s.testErrorCode(c, tk, sql1, tmysql.ErrTooManyPartitions) + assertErrorCode(c, tk, sql1, tmysql.ErrTooManyPartitions) tk.MustExec("drop table if exists p2;") sql2 := `create table p2 ( @@ -586,7 +586,7 @@ func (s *testIntegrationSuite) TestAddPartitionTooManyPartitions(c *C) { sql3 := `alter table p2 add partition ( partition p1025 values less than (1025) );` - s.testErrorCode(c, tk, sql3, tmysql.ErrTooManyPartitions) + assertErrorCode(c, tk, sql3, tmysql.ErrTooManyPartitions) } func checkPartitionDelRangeDone(c *C, s *testIntegrationSuite, partitionPrefix kv.Key) bool { @@ -638,7 +638,7 @@ func (s *testIntegrationSuite) TestTruncatePartitionAndDropTable(c *C) { result = tk.MustQuery("select count(*) from t2;") result.Check(testkit.Rows("100")) tk.MustExec("drop table t2;") - s.testErrorCode(c, tk, "select * from t2;", tmysql.ErrNoSuchTable) + assertErrorCode(c, tk, "select * from t2;", tmysql.ErrNoSuchTable) // Test truncate table partition. tk.MustExec("drop table if exists t3;") @@ -714,7 +714,7 @@ func (s *testIntegrationSuite) TestTruncatePartitionAndDropTable(c *C) { partitionPrefix = tablecodec.EncodeTablePrefix(oldPID) hasOldPartitionData = checkPartitionDelRangeDone(c, s, partitionPrefix) c.Assert(hasOldPartitionData, IsFalse) - s.testErrorCode(c, tk, "select * from t4;", tmysql.ErrNoSuchTable) + assertErrorCode(c, tk, "select * from t4;", tmysql.ErrNoSuchTable) // Test truncate table partition reassigns new partitionIDs. tk.MustExec("drop table if exists t5;") @@ -852,7 +852,7 @@ func (s *testIntegrationSuite) TestPartitionUniqueKeyNeedAllFieldsInPf(c *C) { partition p1 values less than (11), partition p2 values less than (15) );` - s.testErrorCode(c, tk, sql1, tmysql.ErrUniqueKeyNeedAllFieldsInPf) + assertErrorCode(c, tk, sql1, tmysql.ErrUniqueKeyNeedAllFieldsInPf) tk.MustExec("drop table if exists Part1;") sql2 := `create table Part1 ( @@ -867,7 +867,7 @@ func (s *testIntegrationSuite) TestPartitionUniqueKeyNeedAllFieldsInPf(c *C) { partition p1 values less than (11), partition p2 values less than (15) );` - s.testErrorCode(c, tk, sql2, tmysql.ErrUniqueKeyNeedAllFieldsInPf) + assertErrorCode(c, tk, sql2, tmysql.ErrUniqueKeyNeedAllFieldsInPf) tk.MustExec("drop table if exists Part1;") sql3 := `create table Part1 ( @@ -882,7 +882,7 @@ func (s *testIntegrationSuite) TestPartitionUniqueKeyNeedAllFieldsInPf(c *C) { partition p1 values less than (11), partition p2 values less than (15) );` - s.testErrorCode(c, tk, sql3, tmysql.ErrUniqueKeyNeedAllFieldsInPf) + assertErrorCode(c, tk, sql3, tmysql.ErrUniqueKeyNeedAllFieldsInPf) tk.MustExec("drop table if exists Part1;") sql4 := `create table Part1 ( @@ -897,7 +897,7 @@ func (s *testIntegrationSuite) TestPartitionUniqueKeyNeedAllFieldsInPf(c *C) { partition p1 values less than (11), partition p2 values less than (15) );` - s.testErrorCode(c, tk, sql4, tmysql.ErrUniqueKeyNeedAllFieldsInPf) + assertErrorCode(c, tk, sql4, tmysql.ErrUniqueKeyNeedAllFieldsInPf) tk.MustExec("drop table if exists Part1;") sql5 := `create table Part1 ( @@ -911,7 +911,7 @@ func (s *testIntegrationSuite) TestPartitionUniqueKeyNeedAllFieldsInPf(c *C) { partition p1 values less than (11), partition p2 values less than (15) );` - s.testErrorCode(c, tk, sql5, tmysql.ErrUniqueKeyNeedAllFieldsInPf) + assertErrorCode(c, tk, sql5, tmysql.ErrUniqueKeyNeedAllFieldsInPf) tk.MustExec("drop table if exists Part1;") sql6 := `create table Part1 ( @@ -926,7 +926,7 @@ func (s *testIntegrationSuite) TestPartitionUniqueKeyNeedAllFieldsInPf(c *C) { partition p1 values less than (11), partition p2 values less than (15) );` - s.testErrorCode(c, tk, sql6, tmysql.ErrUniqueKeyNeedAllFieldsInPf) + assertErrorCode(c, tk, sql6, tmysql.ErrUniqueKeyNeedAllFieldsInPf) tk.MustExec("drop table if exists Part1;") sql7 := `create table Part1 ( @@ -941,7 +941,7 @@ func (s *testIntegrationSuite) TestPartitionUniqueKeyNeedAllFieldsInPf(c *C) { partition p1 values less than (11), partition p2 values less than (15) );` - s.testErrorCode(c, tk, sql7, tmysql.ErrUniqueKeyNeedAllFieldsInPf) + assertErrorCode(c, tk, sql7, tmysql.ErrUniqueKeyNeedAllFieldsInPf) tk.MustExec("drop table if exists part6;") sql8 := `create table part6 ( @@ -957,7 +957,7 @@ func (s *testIntegrationSuite) TestPartitionUniqueKeyNeedAllFieldsInPf(c *C) { partition p1 values less than (11), partition p2 values less than (15) );` - s.testErrorCode(c, tk, sql8, tmysql.ErrUniqueKeyNeedAllFieldsInPf) + assertErrorCode(c, tk, sql8, tmysql.ErrUniqueKeyNeedAllFieldsInPf) } func (s *testIntegrationSuite) TestPartitionDropIndex(c *C) { diff --git a/ddl/db_test.go b/ddl/db_test.go index 7e954a3169359..b41685e397c62 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -112,8 +112,8 @@ func (s *testDBSuite) TearDownSuite(c *C) { s.store.Close() } -func (s *testDBSuite) testErrorCode(c *C, sql string, errCode int) { - _, err := s.tk.Exec(sql) +func assertErrorCode(c *C, tk *testkit.TestKit, sql string, errCode int) { + _, err := tk.Exec(sql) c.Assert(err, NotNil) originErr := errors.Cause(err) tErr, ok := originErr.(*terror.Error) @@ -158,13 +158,13 @@ func (s *testDBSuite) TestRenameIndex(c *C) { s.tk.MustExec("admin check index t k3") // Test rename on non-exists keys - s.testErrorCode(c, "alter table t rename index x to x", mysql.ErrKeyDoesNotExist) + assertErrorCode(c, s.tk, "alter table t rename index x to x", mysql.ErrKeyDoesNotExist) // Test rename on already-exists keys - s.testErrorCode(c, "alter table t rename index k3 to k2", mysql.ErrDupKeyName) + assertErrorCode(c, s.tk, "alter table t rename index k3 to k2", mysql.ErrDupKeyName) s.tk.MustExec("alter table t rename index k2 to K2") - s.testErrorCode(c, "alter table t rename key k3 to K2", mysql.ErrDupKeyName) + assertErrorCode(c, s.tk, "alter table t rename key k3 to K2", mysql.ErrDupKeyName) } func testGetTableByName(c *C, ctx sessionctx.Context, db, table string) table.Table { @@ -1224,7 +1224,7 @@ func (s *testDBSuite) TestAddColumnTooMany(c *C) { s.tk.MustExec(createSQL) s.tk.MustExec("alter table t_column_too_many add column a_512 int") alterSQL := "alter table t_column_too_many add column a_513 int" - s.testErrorCode(c, alterSQL, tmysql.ErrTooManyFields) + assertErrorCode(c, s.tk, alterSQL, tmysql.ErrTooManyFields) } func sessionExec(c *C, s kv.Storage, sql string) { @@ -1514,23 +1514,23 @@ func (s *testDBSuite) TestChangeColumn(c *C) { // for failing tests sql := "alter table t3 change aa a bigint default ''" - s.testErrorCode(c, sql, tmysql.ErrInvalidDefault) + assertErrorCode(c, s.tk, sql, tmysql.ErrInvalidDefault) sql = "alter table t3 change a testx.t3.aa bigint" - s.testErrorCode(c, sql, tmysql.ErrWrongDBName) + assertErrorCode(c, s.tk, sql, tmysql.ErrWrongDBName) sql = "alter table t3 change t.a aa bigint" - s.testErrorCode(c, sql, tmysql.ErrWrongTableName) + assertErrorCode(c, s.tk, sql, tmysql.ErrWrongTableName) s.mustExec(c, "create table t4 (c1 int, c2 int, c3 int default 1, index (c1));") s.tk.MustExec("insert into t4(c2) values (null);") sql = "alter table t4 change c1 a1 int not null;" - s.testErrorCode(c, sql, tmysql.ErrInvalidUseOfNull) + assertErrorCode(c, s.tk, sql, tmysql.ErrInvalidUseOfNull) sql = "alter table t4 change c2 a bigint not null;" - s.testErrorCode(c, sql, tmysql.WarnDataTruncated) + assertErrorCode(c, s.tk, sql, tmysql.WarnDataTruncated) sql = "alter table t3 modify en enum('a', 'z', 'b', 'c') not null default 'a'" - s.testErrorCode(c, sql, tmysql.ErrUnknown) + assertErrorCode(c, s.tk, sql, tmysql.ErrUnknown) // Rename to an existing column. s.mustExec(c, "alter table t3 add column a bigint") sql = "alter table t3 change aa a bigint" - s.testErrorCode(c, sql, tmysql.ErrDupFieldName) + assertErrorCode(c, s.tk, sql, tmysql.ErrDupFieldName) s.tk.MustExec("drop table t3") } @@ -1605,15 +1605,15 @@ func (s *testDBSuite) TestCreateTableWithLike(c *C) { // for failure cases failSQL := fmt.Sprintf("create table t1 like test_not_exist.t") - s.testErrorCode(c, failSQL, tmysql.ErrNoSuchTable) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrNoSuchTable) failSQL = fmt.Sprintf("create table t1 like test.t_not_exist") - s.testErrorCode(c, failSQL, tmysql.ErrNoSuchTable) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrNoSuchTable) failSQL = fmt.Sprintf("create table t1 (like test_not_exist.t)") - s.testErrorCode(c, failSQL, tmysql.ErrNoSuchTable) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrNoSuchTable) failSQL = fmt.Sprintf("create table test_not_exis.t1 like ctwl_db.t") - s.testErrorCode(c, failSQL, tmysql.ErrBadDB) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrBadDB) failSQL = fmt.Sprintf("create table t1 like ctwl_db.t") - s.testErrorCode(c, failSQL, tmysql.ErrTableExists) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrTableExists) s.tk.MustExec("drop database ctwl_db") s.tk.MustExec("drop database ctwl_db1") @@ -1643,18 +1643,18 @@ func (s *testDBSuite) TestCreateTable(c *C) { // test for enum column failSQL := "create table t_enum (a enum('e','e'));" - s.testErrorCode(c, failSQL, tmysql.ErrDuplicatedValueInType) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrDuplicatedValueInType) failSQL = "create table t_enum (a enum('e','E'));" - s.testErrorCode(c, failSQL, tmysql.ErrDuplicatedValueInType) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrDuplicatedValueInType) failSQL = "create table t_enum (a enum('abc','Abc'));" - s.testErrorCode(c, failSQL, tmysql.ErrDuplicatedValueInType) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrDuplicatedValueInType) // test for set column failSQL = "create table t_enum (a set('e','e'));" - s.testErrorCode(c, failSQL, tmysql.ErrDuplicatedValueInType) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrDuplicatedValueInType) failSQL = "create table t_enum (a set('e','E'));" - s.testErrorCode(c, failSQL, tmysql.ErrDuplicatedValueInType) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrDuplicatedValueInType) failSQL = "create table t_enum (a set('abc','Abc'));" - s.testErrorCode(c, failSQL, tmysql.ErrDuplicatedValueInType) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrDuplicatedValueInType) _, err = s.tk.Exec("create table t_enum (a enum('B','b'));") c.Assert(err.Error(), Equals, "[types:1291]Column 'a' has duplicated value 'B' in ENUM") } @@ -1665,11 +1665,11 @@ func (s *testDBSuite) TestTableForeignKey(c *C) { s.tk.MustExec("create table t1 (a int, b int);") // test create table with foreign key. failSQL := "create table t2 (c int, foreign key (a) references t1(a));" - s.testErrorCode(c, failSQL, tmysql.ErrKeyColumnDoesNotExits) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrKeyColumnDoesNotExits) // test add foreign key. s.tk.MustExec("create table t3 (a int, b int);") failSQL = "alter table t1 add foreign key (c) REFERENCES t3(a);" - s.testErrorCode(c, failSQL, tmysql.ErrKeyColumnDoesNotExits) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrKeyColumnDoesNotExits) s.tk.MustExec("drop table if exists t1,t2,t3;") } @@ -1770,46 +1770,46 @@ func (s *testDBSuite) testRenameTable(c *C, sql string, isAlterTable bool) { // for failure case failSQL := fmt.Sprintf(sql, "test_not_exist.t", "test_not_exist.t") if isAlterTable { - s.testErrorCode(c, failSQL, tmysql.ErrNoSuchTable) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrNoSuchTable) } else { - s.testErrorCode(c, failSQL, tmysql.ErrFileNotFound) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrFileNotFound) } failSQL = fmt.Sprintf(sql, "test.test_not_exist", "test.test_not_exist") if isAlterTable { - s.testErrorCode(c, failSQL, tmysql.ErrNoSuchTable) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrNoSuchTable) } else { - s.testErrorCode(c, failSQL, tmysql.ErrFileNotFound) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrFileNotFound) } failSQL = fmt.Sprintf(sql, "test.t_not_exist", "test_not_exist.t") if isAlterTable { - s.testErrorCode(c, failSQL, tmysql.ErrNoSuchTable) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrNoSuchTable) } else { - s.testErrorCode(c, failSQL, tmysql.ErrFileNotFound) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrFileNotFound) } failSQL = fmt.Sprintf(sql, "test1.t2", "test_not_exist.t") - s.testErrorCode(c, failSQL, tmysql.ErrErrorOnRename) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrErrorOnRename) s.tk.MustExec("use test1") s.tk.MustExec("create table if not exists t_exist (c1 int, c2 int)") failSQL = fmt.Sprintf(sql, "test1.t2", "test1.t_exist") - s.testErrorCode(c, failSQL, tmysql.ErrTableExists) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrTableExists) failSQL = fmt.Sprintf(sql, "test.t_not_exist", "test1.t_exist") if isAlterTable { - s.testErrorCode(c, failSQL, tmysql.ErrNoSuchTable) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrNoSuchTable) } else { - s.testErrorCode(c, failSQL, tmysql.ErrTableExists) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrTableExists) } failSQL = fmt.Sprintf(sql, "test_not_exist.t", "test1.t_exist") if isAlterTable { - s.testErrorCode(c, failSQL, tmysql.ErrNoSuchTable) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrNoSuchTable) } else { - s.testErrorCode(c, failSQL, tmysql.ErrTableExists) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrTableExists) } failSQL = fmt.Sprintf(sql, "test_not_exist.t", "test1.t_not_exist") if isAlterTable { - s.testErrorCode(c, failSQL, tmysql.ErrNoSuchTable) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrNoSuchTable) } else { - s.testErrorCode(c, failSQL, tmysql.ErrFileNotFound) + assertErrorCode(c, s.tk, failSQL, tmysql.ErrFileNotFound) } // for the same table name @@ -1820,8 +1820,8 @@ func (s *testDBSuite) testRenameTable(c *C, sql string, isAlterTable bool) { s.tk.MustExec(fmt.Sprintf(sql, "test1.t", "t")) s.tk.MustExec(fmt.Sprintf(sql, "test1.t1", "test1.T1")) } else { - s.testErrorCode(c, fmt.Sprintf(sql, "test1.t", "t"), tmysql.ErrTableExists) - s.testErrorCode(c, fmt.Sprintf(sql, "test1.t1", "test1.T1"), tmysql.ErrTableExists) + assertErrorCode(c, s.tk, fmt.Sprintf(sql, "test1.t", "t"), tmysql.ErrTableExists) + assertErrorCode(c, s.tk, fmt.Sprintf(sql, "test1.t1", "test1.T1"), tmysql.ErrTableExists) } s.tk.MustExec("drop database test1") @@ -1926,7 +1926,7 @@ func (s *testDBSuite) TestGeneratedColumnDDL(c *C) { {`create table test_gv_ddl_bad (a int, b int, c int as (a+b), primary key(a, c))`, mysql.ErrUnsupportedOnGeneratedColumn}, } for _, tt := range genExprTests { - s.testErrorCode(c, tt.stmt, tt.err) + assertErrorCode(c, s.tk, tt.stmt, tt.err) } // Check alter table modify/change generated column. @@ -1955,9 +1955,9 @@ func (s *testDBSuite) TestComment(c *C) { s.tk.MustExec("create index i on ct (d) comment '" + validComment + "'") s.tk.MustExec("alter table ct add key (e) comment '" + validComment + "'") - s.testErrorCode(c, "create table ct1 (c int, key (c) comment '"+invalidComment+"')", tmysql.ErrTooLongIndexComment) - s.testErrorCode(c, "create index i1 on ct (d) comment '"+invalidComment+"b"+"'", tmysql.ErrTooLongIndexComment) - s.testErrorCode(c, "alter table ct add key (e) comment '"+invalidComment+"'", tmysql.ErrTooLongIndexComment) + assertErrorCode(c, s.tk, "create table ct1 (c int, key (c) comment '"+invalidComment+"')", tmysql.ErrTooLongIndexComment) + assertErrorCode(c, s.tk, "create index i1 on ct (d) comment '"+invalidComment+"b"+"'", tmysql.ErrTooLongIndexComment) + assertErrorCode(c, s.tk, "alter table ct add key (e) comment '"+invalidComment+"'", tmysql.ErrTooLongIndexComment) s.tk.MustExec("set @@sql_mode=''") s.tk.MustExec("create table ct1 (c int, d int, e int, key (c) comment '" + invalidComment + "')") @@ -1998,23 +1998,23 @@ func (s *testDBSuite) TestRebaseAutoID(c *C) { s.tk.MustQuery("select * from tidb.test").Check(testkit.Rows("1 1", "6000 1", "11000 1", "16000 1")) s.tk.MustExec("create table tidb.test2 (a int);") - s.testErrorCode(c, "alter table tidb.test2 add column b int auto_increment key, auto_increment=10;", tmysql.ErrUnknown) + assertErrorCode(c, s.tk, "alter table tidb.test2 add column b int auto_increment key, auto_increment=10;", tmysql.ErrUnknown) } func (s *testDBSuite) TestCheckColumnDefaultValue(c *C) { s.tk = testkit.NewTestKit(c, s.store) s.tk.MustExec("use test;") s.tk.MustExec("drop table if exists text_default_text;") - s.testErrorCode(c, "create table text_default_text(c1 text not null default '');", tmysql.ErrBlobCantHaveDefault) - s.testErrorCode(c, "create table text_default_text(c1 text not null default 'scds');", tmysql.ErrBlobCantHaveDefault) + assertErrorCode(c, s.tk, "create table text_default_text(c1 text not null default '');", tmysql.ErrBlobCantHaveDefault) + assertErrorCode(c, s.tk, "create table text_default_text(c1 text not null default 'scds');", tmysql.ErrBlobCantHaveDefault) s.tk.MustExec("drop table if exists text_default_json;") - s.testErrorCode(c, "create table text_default_json(c1 json not null default '');", tmysql.ErrBlobCantHaveDefault) - s.testErrorCode(c, "create table text_default_json(c1 json not null default 'dfew555');", tmysql.ErrBlobCantHaveDefault) + assertErrorCode(c, s.tk, "create table text_default_json(c1 json not null default '');", tmysql.ErrBlobCantHaveDefault) + assertErrorCode(c, s.tk, "create table text_default_json(c1 json not null default 'dfew555');", tmysql.ErrBlobCantHaveDefault) s.tk.MustExec("drop table if exists text_default_blob;") - s.testErrorCode(c, "create table text_default_blob(c1 blob not null default '');", tmysql.ErrBlobCantHaveDefault) - s.testErrorCode(c, "create table text_default_blob(c1 blob not null default 'scds54');", tmysql.ErrBlobCantHaveDefault) + assertErrorCode(c, s.tk, "create table text_default_blob(c1 blob not null default '');", tmysql.ErrBlobCantHaveDefault) + assertErrorCode(c, s.tk, "create table text_default_blob(c1 blob not null default 'scds54');", tmysql.ErrBlobCantHaveDefault) s.tk.MustExec("set sql_mode='';") s.tk.MustExec("create table text_default_text(c1 text not null default '');") @@ -2126,8 +2126,8 @@ func (s *testDBSuite) TestColumnModifyingDefinition(c *C) { s.tk.MustExec("drop table if exists test2;") s.tk.MustExec("create table test2 (c1 int, c2 int, c3 int default 1, index (c1));") s.tk.MustExec("insert into test2(c2) values (null);") - s.testErrorCode(c, "alter table test2 change c2 a int not null", tmysql.ErrInvalidUseOfNull) - s.testErrorCode(c, "alter table test2 change c1 a1 bigint not null;", tmysql.WarnDataTruncated) + assertErrorCode(c, s.tk, "alter table test2 change c2 a int not null", tmysql.ErrInvalidUseOfNull) + assertErrorCode(c, s.tk, "alter table test2 change c1 a1 bigint not null;", tmysql.WarnDataTruncated) } func (s *testDBSuite) TestCheckTooBigFieldLength(c *C) { @@ -2144,10 +2144,10 @@ func (s *testDBSuite) TestCheckTooBigFieldLength(c *C) { s.tk.MustExec("drop table if exists tr_04;") s.tk.MustExec("create table tr_04 (a varchar(20000)) default charset utf8;") - s.testErrorCode(c, "alter table tr_04 convert to character set utf8mb4;", tmysql.ErrTooBigFieldlength) - s.testErrorCode(c, "create table tr (id int, name varchar(30000), purchased date ) default charset=utf8 collate=utf8_bin;", tmysql.ErrTooBigFieldlength) - s.testErrorCode(c, "create table tr (id int, name varchar(20000) charset utf8mb4, purchased date ) default charset=utf8 collate=utf8;", tmysql.ErrTooBigFieldlength) - s.testErrorCode(c, "create table tr (id int, name varchar(65536), purchased date ) default charset=latin1;", tmysql.ErrTooBigFieldlength) + assertErrorCode(c, s.tk, "alter table tr_04 convert to character set utf8mb4;", tmysql.ErrTooBigFieldlength) + assertErrorCode(c, s.tk, "create table tr (id int, name varchar(30000), purchased date ) default charset=utf8 collate=utf8_bin;", tmysql.ErrTooBigFieldlength) + assertErrorCode(c, s.tk, "create table tr (id int, name varchar(20000) charset utf8mb4, purchased date ) default charset=utf8 collate=utf8;", tmysql.ErrTooBigFieldlength) + assertErrorCode(c, s.tk, "create table tr (id int, name varchar(65536), purchased date ) default charset=latin1;", tmysql.ErrTooBigFieldlength) s.tk.MustExec("drop table if exists tr_05;") s.tk.MustExec("create table tr_05 (a varchar(16000) charset utf8);") @@ -2196,7 +2196,7 @@ func (s *testDBSuite) TestModifyColumnRollBack(c *C) { } } if mysql.HasPreventNullInsertFlag(c2.Flag) { - s.testErrorCode(c, "insert into t1(c2) values (null);", tmysql.ErrBadNull) + assertErrorCode(c, s.tk, "insert into t1(c2) values (null);", tmysql.ErrBadNull) } hookCtx := mock.NewContext() diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 6da8c7f60c754..569ef1d967d4f 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -1461,14 +1461,18 @@ func resolveAlterTableSpec(ctx sessionctx.Context, specs []*ast.AlterTableSpec) // Verify whether the algorithm is supported. for _, spec := range validSpecs { - algorithm, err := ResolveAlterAlgorithm(spec, algorithm) + resolvedAlgorithm, err := ResolveAlterAlgorithm(spec, algorithm) if err != nil { - // For the compatibility, we return warning instead of error. + if algorithm != ast.AlterAlgorithmCopy { + return nil, errors.Trace(err) + } + // For the compatibility, we return warning instead of error when the algorithm is COPY, + // because the COPY ALGORITHM is not supported in TiDB. ctx.GetSessionVars().StmtCtx.AppendError(err) err = nil } - spec.Algorithm = algorithm + spec.Algorithm = resolvedAlgorithm } // Only handle valid specs.