Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
Signed-off-by: Weizhen Wang <[email protected]>
  • Loading branch information
hawkingrei committed Jun 13, 2022
1 parent e7413c1 commit 1ce9c3b
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 213 deletions.
4 changes: 1 addition & 3 deletions ddl/db_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/parser/auth"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/testkit"
Expand Down Expand Up @@ -170,8 +169,7 @@ func TestAlterTableCache(t *testing.T) {
tk.MustExec("begin")
tk.MustExec("insert into t1 set a=1;")
tk2.MustExec("alter table t1 cache;")
_, err = tk.Exec("commit")
require.True(t, terror.ErrorEqual(domain.ErrInfoSchemaChanged, err))
tk.MustGetDBError("commit", domain.ErrInfoSchemaChanged)
/* Test can skip schema checker */
tk.MustExec("begin")
tk.MustExec("alter table t1 nocache")
Expand Down
63 changes: 19 additions & 44 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,26 +119,18 @@ func TestInvalidDefault(t *testing.T) {

tk.MustExec("USE test;")

_, err := tk.Exec("create table t(c1 decimal default 1.7976931348623157E308)")
require.Error(t, err)
require.Truef(t, terror.ErrorEqual(err, types.ErrInvalidDefault), "err %v", err)

_, err = tk.Exec("create table t( c1 varchar(2) default 'TiDB');")
require.Error(t, err)
require.Truef(t, terror.ErrorEqual(err, types.ErrInvalidDefault), "err %v", err)
tk.MustGetDBError("create table t(c1 decimal default 1.7976931348623157E308)", types.ErrInvalidDefault)
tk.MustGetDBError("create table t( c1 varchar(2) default 'TiDB');", types.ErrInvalidDefault)
}

// TestKeyWithoutLength for issue #13452
func TestKeyWithoutLengthCreateTable(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)

tk.MustExec("USE test")

_, err := tk.Exec("create table t_without_length (a text primary key)")
require.Error(t, err)
require.Regexp(t, ".*BLOB/TEXT column 'a' used in key specification without a key length", err.Error())
tk.MustMatchErrMsg("create table t_without_length (a text primary key)",
".*BLOB/TEXT column 'a' used in key specification without a key length")
}

// TestInvalidNameWhenCreateTable for issue #3848
Expand Down Expand Up @@ -3371,19 +3363,15 @@ func TestAvoidCreateViewOnLocalTemporaryTable(t *testing.T) {
require.True(t, core.ErrViewSelectTemporaryTable.Equal(err))
tk.MustGetErrMsg("select * from v5", "[schema:1146]Table 'test.v5' doesn't exist")

_, err = tk.Exec("create view v6 as select * from tt0 where tt0.a=(select max(tt1.b) from tt1)")
err = tk.ExecToErr("create view v6 as select * from tt0 where tt0.a=(select max(tt1.b) from tt1)")
require.True(t, core.ErrViewSelectTemporaryTable.Equal(err))
_, err = tk.Exec("select * from v6")
require.Error(t, err)
require.Equal(t, "[schema:1146]Table 'test.v6' doesn't exist", err.Error())
tk.MustGetErrMsg("select * from v6", "[schema:1146]Table 'test.v6' doesn't exist")

_, err = tk.Exec("create view v7 as select * from tt0 where tt0.b=(select max(tt1.b) from tt1 where tt0.a=tt1.a)")
err = tk.ExecToErr("create view v7 as select * from tt0 where tt0.b=(select max(tt1.b) from tt1 where tt0.a=tt1.a)")
require.True(t, core.ErrViewSelectTemporaryTable.Equal(err))
_, err = tk.Exec("select * from v7")
require.Error(t, err)
require.Equal(t, "[schema:1146]Table 'test.v7' doesn't exist", err.Error())
tk.MustGetErrMsg("select * from v7", "[schema:1146]Table 'test.v7' doesn't exist")

_, err = tk.Exec("create or replace view v0 as select * from tt1")
err = tk.ExecToErr("create or replace view v0 as select * from tt1")
require.True(t, core.ErrViewSelectTemporaryTable.Equal(err))
}

Expand Down Expand Up @@ -3413,8 +3401,7 @@ func TestDropTemporaryTable(t *testing.T) {
tk.MustExec("create temporary table if not exists b_local_temp_table (id int)")
tk.MustQuery("select * from b_local_temp_table").Check(testkit.Rows())
tk.MustExec("drop table b_local_temp_table")
_, err := tk.Exec("select * from b_local_temp_table")
require.Equal(t, "[schema:1146]Table 'test.b_local_temp_table' doesn't exist", err.Error())
tk.MustGetErrMsg("select * from b_local_temp_table", "[schema:1146]Table 'test.b_local_temp_table' doesn't exist")
// TODO: test drop real data

// Check if we have a normal and local temporary table in the same db with the same name,
Expand All @@ -3427,8 +3414,7 @@ func TestDropTemporaryTable(t *testing.T) {
sequenceTable := external.GetTableByName(t, tk, "test", "b_table_local_and_normal")
require.Equal(t, model.TempTableNone, sequenceTable.Meta().TempTableType)
tk.MustExec("drop table if exists b_table_local_and_normal")
_, err = tk.Exec("select * from b_table_local_and_normal")
require.Equal(t, "[schema:1146]Table 'test.b_table_local_and_normal' doesn't exist", err.Error())
tk.MustGetErrMsg("select * from b_table_local_and_normal", "[schema:1146]Table 'test.b_table_local_and_normal' doesn't exist")

// Check dropping local temporary tables should not commit current transaction implicitly.
tk.MustExec("drop table if exists check_data_normal_table")
Expand Down Expand Up @@ -3473,21 +3459,19 @@ func TestDropTemporaryTable(t *testing.T) {
tk.MustExec("drop table if exists a_normal_table_2")
tk.MustExec("create table a_normal_table_2 (id int)")
defer tk.MustExec("drop table if exists a_normal_table_2")
_, err = tk.Exec("drop table a_local_temp_table_3, a_local_temp_table_4, a_local_temp_table_5, a_normal_table_2, a_local_temp_table_6")
require.Equal(t, "[schema:1051]Unknown table 'test.a_local_temp_table_6'", err.Error())
tk.MustGetErrMsg("drop table a_local_temp_table_3, a_local_temp_table_4, a_local_temp_table_5, a_normal_table_2, a_local_temp_table_6", "[schema:1051]Unknown table 'test.a_local_temp_table_6'")

tk.MustExec("drop table if exists check_data_normal_table_3")
tk.MustExec("create table check_data_normal_table_3 (id int)")
defer tk.MustExec("drop table if exists check_data_normal_table_3")
tk.MustExec("create temporary table a_local_temp_table_6 (id int)")
_, err = tk.Exec("drop table check_data_normal_table_3, check_data_normal_table_7, a_local_temp_table_6")
require.Equal(t, "[schema:1051]Unknown table 'test.check_data_normal_table_7'", err.Error())
tk.MustGetErrMsg("drop table check_data_normal_table_3, check_data_normal_table_7, a_local_temp_table_6", "[schema:1051]Unknown table 'test.check_data_normal_table_7'")

// Check filter out data from removed local temp tables
tk.MustExec("create temporary table a_local_temp_table_7 (id int)")
ctx := tk.Session()
require.Nil(t, sessiontxn.NewTxn(context.Background(), ctx))
_, err = ctx.Txn(true)
_, err := ctx.Txn(true)
require.NoError(t, err)
sessionVars := tk.Session().GetSessionVars()
sessVarsTempTable := sessionVars.LocalTemporaryTables
Expand All @@ -3505,8 +3489,7 @@ func TestDropTemporaryTable(t *testing.T) {
tk.MustExec("drop table if exists a_local_temp_table_7")
tk.MustExec("commit")

_, err = tk.Exec("select * from a_local_temp_table_7")
require.Equal(t, "[schema:1146]Table 'test.a_local_temp_table_7' doesn't exist", err.Error())
tk.MustGetErrMsg("select * from a_local_temp_table_7", "[schema:1146]Table 'test.a_local_temp_table_7' doesn't exist")
memData := sessionVars.TemporaryTableData
iter, err := memData.Iter(tablePrefix, endTablePrefix)
require.NoError(t, err)
Expand All @@ -3524,8 +3507,7 @@ func TestDropTemporaryTable(t *testing.T) {
// Check drop not exists table in transaction.
tk.MustExec("begin")
tk.MustExec("create temporary table a_local_temp_table_8 (id int)")
_, err = tk.Exec("drop table a_local_temp_table_8, a_local_temp_table_9_not_exist")
require.Equal(t, "[schema:1051]Unknown table 'test.a_local_temp_table_9_not_exist'", err.Error())
tk.MustGetErrMsg("drop table a_local_temp_table_8, a_local_temp_table_9_not_exist", "[schema:1051]Unknown table 'test.a_local_temp_table_9_not_exist'")
tk.MustQuery("select * from a_local_temp_table_8").Check(testkit.Rows())
}

Expand Down Expand Up @@ -3973,18 +3955,11 @@ func TestInvalidPartitionNameWhenCreateTable(t *testing.T) {
defer tk.MustExec("drop database invalidPartitionNames")
tk.MustExec("USE invalidPartitionNames")

_, err := tk.Exec("create table t(a int) partition by range (a) (partition p0 values less than (0), partition `p1 ` values less than (3))")
require.Error(t, err)
require.Truef(t, terror.ErrorEqual(err, dbterror.ErrWrongPartitionName), "err %v", err)

_, err = tk.Exec("create table t(a int) partition by range (a) (partition `` values less than (0), partition `p1` values less than (3))")
require.Error(t, err)
require.Truef(t, terror.ErrorEqual(err, dbterror.ErrWrongPartitionName), "err %v", err)
tk.MustGetDBError("create table t(a int) partition by range (a) (partition p0 values less than (0), partition `p1 ` values less than (3))", dbterror.ErrWrongPartitionName)
tk.MustGetDBError("create table t(a int) partition by range (a) (partition `` values less than (0), partition `p1` values less than (3))", dbterror.ErrWrongPartitionName)

tk.MustExec("create table t(a int) partition by range (a) (partition `p0` values less than (0), partition `p1` values less than (3))")
_, err = tk.Exec("alter table t add partition (partition `p2 ` values less than (5))")
require.Error(t, err)
require.Truef(t, terror.ErrorEqual(err, dbterror.ErrWrongPartitionName), "err %v", err)
tk.MustGetDBError("alter table t add partition (partition `p2 ` values less than (5))", dbterror.ErrWrongPartitionName)
}

func TestDDLLastInfo(t *testing.T) {
Expand Down
90 changes: 25 additions & 65 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestCreateTableWithPartition(t *testing.T) {
);`
tk.MustGetErrCode(sql4, tmysql.ErrPartitionMaxvalue)

_, err = tk.Exec(`CREATE TABLE rc (
tk.MustExec(`CREATE TABLE rc (
a INT NOT NULL,
b INT NOT NULL,
c INT NOT NULL
Expand All @@ -168,7 +168,6 @@ func TestCreateTableWithPartition(t *testing.T) {
partition p3 values less than (65,30,13),
partition p4 values less than (maxvalue,30,40)
);`)
require.NoError(t, err)

sql6 := `create table employees (
id int not null,
Expand Down Expand Up @@ -211,7 +210,7 @@ func TestCreateTableWithPartition(t *testing.T) {
);`
tk.MustGetErrCode(sql9, tmysql.ErrPartitionFunctionIsNotAllowed)

_, err = tk.Exec(`CREATE TABLE t9 (
tk.MustGetDBError(`CREATE TABLE t9 (
a INT NOT NULL,
b INT NOT NULL,
c INT NOT NULL
Expand All @@ -220,8 +219,7 @@ func TestCreateTableWithPartition(t *testing.T) {
partition p0 values less than (10),
partition p2 values less than (20),
partition p3 values less than (20)
);`)
require.True(t, dbterror.ErrRangeNotIncreasing.Equal(err))
);`, dbterror.ErrRangeNotIncreasing)

tk.MustGetErrCode(`create TABLE t10 (c1 int,c2 int) partition by range(c1 / c2 ) (partition p0 values less than (2));`, tmysql.ErrPartitionFunctionIsNotAllowed)

Expand Down Expand Up @@ -3088,11 +3086,8 @@ func TestPartitionErrorCode(t *testing.T) {
)
partition by hash(store_id)
partitions 4;`)
_, err := tk.Exec("alter table employees add partition partitions 8;")
require.True(t, dbterror.ErrUnsupportedAddPartition.Equal(err))

_, err = tk.Exec("alter table employees add partition (partition p5 values less than (42));")
require.True(t, dbterror.ErrUnsupportedAddPartition.Equal(err))
tk.MustGetDBError("alter table employees add partition partitions 8;", dbterror.ErrUnsupportedAddPartition)
tk.MustGetDBError("alter table employees add partition (partition p5 values less than (42));", dbterror.ErrUnsupportedAddPartition)

// coalesce partition
tk.MustExec(`create table clients (
Expand All @@ -3103,16 +3098,14 @@ func TestPartitionErrorCode(t *testing.T) {
)
partition by hash( month(signed) )
partitions 12;`)
_, err = tk.Exec("alter table clients coalesce partition 4;")
require.True(t, dbterror.ErrUnsupportedCoalescePartition.Equal(err))
tk.MustGetDBError("alter table clients coalesce partition 4;", dbterror.ErrUnsupportedCoalescePartition)

tk.MustExec(`create table t_part (a int key)
partition by range(a) (
partition p0 values less than (10),
partition p1 values less than (20)
);`)
_, err = tk.Exec("alter table t_part coalesce partition 4;")
require.True(t, dbterror.ErrCoalesceOnlyOnHashPartition.Equal(err))
tk.MustGetDBError("alter table t_part coalesce partition 4;", dbterror.ErrCoalesceOnlyOnHashPartition)

tk.MustGetErrCode(`alter table t_part reorganize partition p0, p1 into (
partition p0 values less than (1980));`, tmysql.ErrUnsupportedDDLOperation)
Expand All @@ -3135,9 +3128,7 @@ func TestPartitionErrorCode(t *testing.T) {
tk2 := testkit.NewTestKit(t, store)
tk2.MustExec("use test")
tk2.MustExec("alter table t truncate partition p0;")

_, err = tk1.Exec("commit")
require.NoError(t, err)
tk1.MustExec("commit")
}

func TestConstAndTimezoneDepent(t *testing.T) {
Expand Down Expand Up @@ -3337,8 +3328,7 @@ func TestCommitWhenSchemaChange(t *testing.T) {
tk.MustExec("insert into nt values (1), (3), (5);")
tk2.MustExec("alter table pt exchange partition p1 with table nt;")
tk.MustExec("insert into nt values (7), (9);")
_, err = tk.Session().Execute(context.Background(), "commit")
require.True(t, domain.ErrInfoSchemaChanged.Equal(err))
tk.MustGetDBError("commit", domain.ErrInfoSchemaChanged)

tk.MustExec("admin check table pt")
tk.MustQuery("select * from pt").Check(testkit.Rows())
Expand All @@ -3349,8 +3339,7 @@ func TestCommitWhenSchemaChange(t *testing.T) {
tk.MustExec("insert into pt values (1), (3), (5);")
tk2.MustExec("alter table pt exchange partition p1 with table nt;")
tk.MustExec("insert into pt values (7), (9);")
_, err = tk.Session().Execute(context.Background(), "commit")
require.True(t, domain.ErrInfoSchemaChanged.Equal(err))
tk.MustGetDBError("commit", domain.ErrInfoSchemaChanged)

tk.MustExec("admin check table pt")
tk.MustQuery("select * from pt").Check(testkit.Rows())
Expand All @@ -3365,34 +3354,28 @@ func TestCreatePartitionTableWithWrongType(t *testing.T) {
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
var err error
_, err = tk.Exec(`create table t(
tk.MustGetDBError(`create table t(
b int(10)
) partition by range columns (b) (
partition p0 values less than (0x10),
partition p3 values less than (0x20)
)`)
require.Error(t, err)
require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err))
)`, dbterror.ErrWrongTypeColumnValue)

_, err = tk.Exec(`create table t(
tk.MustGetDBError(`create table t(
b int(10)
) partition by range columns (b) (
partition p0 values less than ('g'),
partition p3 values less than ('k')
)`)
require.Error(t, err)
require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err))
)`, dbterror.ErrWrongTypeColumnValue)

_, err = tk.Exec(`create table t(
tk.MustGetDBError(`create table t(
b char(10)
) partition by range columns (b) (
partition p0 values less than (30),
partition p3 values less than (60)
)`)
require.Error(t, err)
require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err))
)`, dbterror.ErrWrongTypeColumnValue)

_, err = tk.Exec(`create table t(
err = tk.ExecToErr(`create table t(
b datetime
) partition by range columns (b) (
partition p0 values less than ('g'),
Expand Down Expand Up @@ -3422,35 +3405,13 @@ func TestAddPartitionForTableWithWrongType(t *testing.T) {
partition p0 values less than ('2020-09-01')
)`)

var err error

_, err = tk.Exec("alter table t_int add partition (partition p1 values less than ('g'))")
require.Error(t, err)
require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err))

_, err = tk.Exec("alter table t_int add partition (partition p1 values less than (0x20))")
require.Error(t, err)
require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err))

_, err = tk.Exec("alter table t_char add partition (partition p1 values less than (0x20))")
require.Error(t, err)
require.True(t, dbterror.ErrRangeNotIncreasing.Equal(err))

_, err = tk.Exec("alter table t_char add partition (partition p1 values less than (10))")
require.Error(t, err)
require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err))

_, err = tk.Exec("alter table t_date add partition (partition p1 values less than ('m'))")
require.Error(t, err)
require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err))

_, err = tk.Exec("alter table t_date add partition (partition p1 values less than (0x20))")
require.Error(t, err)
require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err))

_, err = tk.Exec("alter table t_date add partition (partition p1 values less than (20))")
require.Error(t, err)
require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err))
tk.MustGetDBError("alter table t_int add partition (partition p1 values less than ('g'))", dbterror.ErrWrongTypeColumnValue)
tk.MustGetDBError("alter table t_int add partition (partition p1 values less than (0x20))", dbterror.ErrWrongTypeColumnValue)
tk.MustGetDBError("alter table t_char add partition (partition p1 values less than (0x20))", dbterror.ErrRangeNotIncreasing)
tk.MustGetDBError("alter table t_char add partition (partition p1 values less than (10))", dbterror.ErrWrongTypeColumnValue)
tk.MustGetDBError("alter table t_date add partition (partition p1 values less than ('m'))", dbterror.ErrWrongTypeColumnValue)
tk.MustGetDBError("alter table t_date add partition (partition p1 values less than (0x20))", dbterror.ErrWrongTypeColumnValue)
tk.MustGetDBError("alter table t_date add partition (partition p1 values less than (20))", dbterror.ErrWrongTypeColumnValue)
}

func TestPartitionListWithTimeType(t *testing.T) {
Expand Down Expand Up @@ -3517,8 +3478,7 @@ func TestAddTableWithPartition(t *testing.T) {
tk.MustGetErrCode("create temporary table local_partition_table (a int, b int) partition by hash(a) partitions 3;", errno.ErrPartitionNoTemporary)
tk.MustExec("drop table if exists local_partition_table;")
tk.MustExec("drop table if exists partition_table;")
_, err = tk.Exec("create table partition_table (a int, b int) partition by hash(a) partitions 3;")
require.NoError(t, err)
tk.MustExec("create table partition_table (a int, b int) partition by hash(a) partitions 3;")
tk.MustExec("drop table if exists partition_table;")
tk.MustExec("drop table if exists local_partition_range_table;")
tk.MustGetErrCode(`create temporary table local_partition_range_table (c1 smallint(6) not null, c2 char(5) default null) partition by range ( c1 ) (
Expand Down
Loading

0 comments on commit 1ce9c3b

Please sign in to comment.