Skip to content

Commit

Permalink
ddl: Check create table options for local temporary table (#27150)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcwangchao authored Aug 16, 2021
1 parent 255fa35 commit 0de541c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
3 changes: 3 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2935,6 +2935,9 @@ func (s *testIntegrationSuite3) TestCreateTemporaryTable(c *C) {
c.Assert(infoschema.ErrTableExists.Equal(err), IsTrue)
tk.MustExec("create temporary table if not exists b_local_temp_table (id int)")

// Engine type can only be 'memory' or empty for now.
tk.MustGetErrCode("create temporary table te (id int) engine = 'innodb'", errno.ErrUnsupportedDDLOperation)

// Stale read see the local temporary table but can't read on it.
tk.MustExec("START TRANSACTION READ ONLY AS OF TIMESTAMP NOW(3)")
tk.MustGetErrMsg("select * from overlap", "can not stale read temporary table")
Expand Down
28 changes: 28 additions & 0 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3390,6 +3390,7 @@ func (s *testSerialDBSuite1) TestPartitionListWithNewCollation(c *C) {
}

func (s *testSerialDBSuite1) TestAddTableWithPartition(c *C) {
// for global temporary table
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("set tidb_enable_global_temporary_table=true")
tk.MustExec("use test;")
Expand All @@ -3416,6 +3417,33 @@ func (s *testSerialDBSuite1) TestAddTableWithPartition(c *C) {
partition p3 values in (5,null)
) ON COMMIT DELETE ROWS;`, errno.ErrPartitionNoTemporary)
tk.MustExec("drop table if exists partition_list_table;")

// for local temporary table
tk.MustExec("set tidb_enable_noop_functions=1")
tk.MustExec("use test;")
tk.MustExec("drop table if exists local_partition_table;")
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;")
c.Assert(err, IsNil)
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 ) (
partition p0 values less than (10),
partition p1 values less than (20),
partition p2 values less than (30),
partition p3 values less than (MAXVALUE)
);`, errno.ErrPartitionNoTemporary)
tk.MustExec("drop table if exists local_partition_range_table;")
tk.MustExec("drop table if exists local_partition_list_table;")
tk.MustExec("set @@session.tidb_enable_list_partition = ON")
tk.MustGetErrCode(`create temporary table local_partition_list_table (id int) partition by list (id) (
partition p0 values in (1,2),
partition p1 values in (3,4),
partition p3 values in (5,null)
);`, errno.ErrPartitionNoTemporary)
tk.MustExec("drop table if exists local_partition_list_table;")
}

func (s *testSerialDBSuite1) TestTruncatePartitionMultipleTimes(c *C) {
Expand Down
38 changes: 37 additions & 1 deletion ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3329,16 +3329,22 @@ func (s *testDBSuite2) TestTemporaryTableForeignKey(c *C) {
tk.MustExec("create table t1 (a int, b int);")
tk.MustExec("drop table if exists t1_tmp;")
tk.MustExec("set tidb_enable_global_temporary_table=true")
tk.MustExec("set tidb_enable_noop_functions=1")
tk.MustExec("create global temporary table t1_tmp (a int, b int) on commit delete rows;")
tk.MustExec("create temporary table t2_tmp (a int, b int)")
// test add foreign key.
tk.MustExec("drop table if exists t2;")
tk.MustExec("create table t2 (a int, b int);")
failSQL := "alter table t1_tmp add foreign key (c) REFERENCES t2(a);"
tk.MustGetErrCode(failSQL, mysql.ErrCannotAddForeign)
failSQL = "alter table t2_tmp add foreign key (c) REFERENCES t2(a);"
tk.MustGetErrCode(failSQL, errno.ErrUnsupportedDDLOperation)
// Test drop column with foreign key.
failSQL = "create global temporary table t3 (c int,d int,foreign key (d) references t1 (b)) on commit delete rows;"
tk.MustGetErrCode(failSQL, mysql.ErrCannotAddForeign)
tk.MustExec("drop table if exists t1,t2,t3,t1_tmp;")
failSQL = "create temporary table t4(c int,d int,foreign key (d) references t1 (b));"
tk.MustGetErrCode(failSQL, mysql.ErrCannotAddForeign)
tk.MustExec("drop table if exists t1,t2,t3, t4,t1_tmp,t2_tmp;")
}

func (s *testDBSuite8) TestFKOnGeneratedColumns(c *C) {
Expand Down Expand Up @@ -3834,6 +3840,26 @@ func (s *testDBSuite3) TestVirtualColumnDDL(c *C) {
tk.MustQuery("select * from test_gv_ddl").Check(testkit.Rows("1 9 11"))
_, err = tk.Exec("commit")
c.Assert(err, IsNil)

// for local temporary table
tk.MustExec("set @@tidb_enable_noop_functions=1;")
tk.MustExec(`create temporary table test_local_gv_ddl(a int, b int as (a+8) virtual, c int as (b + 2) stored);`)
defer tk.MustExec("drop table if exists test_local_gv_ddl")
is = tk.Se.(sessionctx.Context).GetInfoSchema().(infoschema.InfoSchema)
table, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("test_local_gv_ddl"))
c.Assert(err, IsNil)
for i, column := range table.Meta().Columns {
c.Assert(column.GeneratedExprString, Equals, testCases[i].generatedExprString)
c.Assert(column.GeneratedStored, Equals, testCases[i].generatedStored)
}
result = tk.MustQuery(`DESC test_local_gv_ddl`)
result.Check(testkit.Rows(`a int(11) YES <nil> `, `b int(11) YES <nil> VIRTUAL GENERATED`, `c int(11) YES <nil> STORED GENERATED`))
tk.MustExec("begin;")
tk.MustExec("insert into test_local_gv_ddl values (1, default, default)")
tk.MustQuery("select * from test_local_gv_ddl").Check(testkit.Rows("1 9 11"))
_, err = tk.Exec("commit")
c.Assert(err, IsNil)
tk.MustQuery("select * from test_local_gv_ddl").Check(testkit.Rows("1 9 11"))
}

func (s *testDBSuite3) TestGeneratedColumnDDL(c *C) {
Expand Down Expand Up @@ -5761,6 +5787,7 @@ func (s *testSerialDBSuite) TestAlterShardRowIDBits(c *C) {
func (s *testSerialDBSuite) TestShardRowIDBitsOnTemporaryTable(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
// for global temporary table
tk.MustExec("drop table if exists shard_row_id_temporary")
tk.MustExec("set tidb_enable_global_temporary_table=true")
_, err := tk.Exec("create global temporary table shard_row_id_temporary (a int) shard_row_id_bits = 5 on commit delete rows;")
Expand All @@ -5769,6 +5796,15 @@ func (s *testSerialDBSuite) TestShardRowIDBitsOnTemporaryTable(c *C) {
defer tk.MustExec("drop table if exists shard_row_id_temporary")
_, err = tk.Exec("alter table shard_row_id_temporary shard_row_id_bits = 4;")
c.Assert(err.Error(), Equals, ddl.ErrOptOnTemporaryTable.GenWithStackByArgs("shard_row_id_bits").Error())
// for local temporary table
tk.MustExec("set tidb_enable_noop_functions=true")
tk.MustExec("drop table if exists local_shard_row_id_temporary")
_, err = tk.Exec("create temporary table local_shard_row_id_temporary (a int) shard_row_id_bits = 5;")
c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("shard_row_id_bits").Error())
tk.MustExec("create temporary table local_shard_row_id_temporary (a int);")
defer tk.MustExec("drop table if exists local_shard_row_id_temporary")
_, err = tk.Exec("alter table local_shard_row_id_temporary shard_row_id_bits = 4;")
c.Assert(err.Error(), Equals, ddl.ErrUnsupportedLocalTempTableDDL.GenWithStackByArgs("ALTER TABLE").Error())
}

// port from mysql
Expand Down
2 changes: 1 addition & 1 deletion executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5218,7 +5218,7 @@ func (s *testSplitTable) TestShowTableRegion(c *C) {
tk.MustQuery(`split table t_regions between (-10000) and (10000) regions 4;`).Check(testkit.Rows("4 1"))
re := tk.MustQuery("show table t_regions regions")

// Test show table regions and split table on temporary table.
// Test show table regions and split table on global temporary table.
tk.MustExec("drop table if exists t_regions_temporary_table")
tk.MustExec("set tidb_enable_global_temporary_table=true")
tk.MustExec("create global temporary table t_regions_temporary_table (a int key, b int, c int, index idx(b), index idx2(c)) ON COMMIT DELETE ROWS;")
Expand Down

0 comments on commit 0de541c

Please sign in to comment.