From 11019d6f346e65d326da51ea1334b782678956ef Mon Sep 17 00:00:00 2001 From: Howie Date: Mon, 31 May 2021 15:47:36 +0800 Subject: [PATCH] ddl: add admin_checksum_table compatibility for temporary table (#24867) --- ddl/serial_test.go | 7 +++++++ executor/admin_test.go | 14 +++++++++++++- planner/core/planbuilder.go | 3 +++ planner/core/preprocess.go | 8 ++++++-- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/ddl/serial_test.go b/ddl/serial_test.go index f78ff3d16577b..8d9217e1df8e3 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -534,6 +534,13 @@ func (s *testSerialSuite) TestCreateTableWithLike(c *C) { _, err = tk.Exec("create table temporary_table_t1 like temporary_table") c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("create table like").Error()) tk.MustExec("drop table if exists temporary_table;") + + tk.MustExec("drop table if exists temporary_table_like;") + tk.MustExec("create table temporary_table (a int, b int,index(a))") + tk.MustExec("drop table if exists temporary_table_like_t1;") + _, err = tk.Exec("create global temporary table temporary_table_like_t1 like temporary_table on commit delete rows;") + c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("create table like").Error()) + tk.MustExec("drop table if exists temporary_table_like;") } // TestCancelAddIndex1 tests canceling ddl job when the add index worker is not started. diff --git a/executor/admin_test.go b/executor/admin_test.go index 35e3d08345d63..2e1d24bd32bc6 100644 --- a/executor/admin_test.go +++ b/executor/admin_test.go @@ -24,6 +24,7 @@ import ( mysql "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/executor" "github.com/pingcap/tidb/kv" + "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/sessionctx/variable" "github.com/pingcap/tidb/table" "github.com/pingcap/tidb/table/tables" @@ -82,7 +83,8 @@ func (s *testSuite5) TestAdminCheckIndexInTemporaryMode(c *C) { tk.MustExec("drop table if exists temporary_admin_test;") tk.MustExec("create global temporary table temporary_admin_test (c1 int, c2 int, c3 int default 1, primary key (c1), index (c1), unique key(c2)) ON COMMIT DELETE ROWS;") tk.MustExec("insert temporary_admin_test (c1, c2) values (1, 1), (2, 2), (3, 3);") - tk.MustGetErrCode("admin check table temporary_admin_test;", mysql.ErrAdminCheckTable) + _, err := tk.Exec("admin check table temporary_admin_test;") + c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("admin check table").Error()) tk.MustExec("drop table if exists temporary_admin_test;") tk.MustExec("drop table if exists non_temporary_admin_test;") @@ -90,6 +92,16 @@ func (s *testSuite5) TestAdminCheckIndexInTemporaryMode(c *C) { tk.MustExec("insert non_temporary_admin_test (c1, c2) values (1, 1), (2, 2), (3, 3);") tk.MustExec("admin check table non_temporary_admin_test;") tk.MustExec("drop table if exists non_temporary_admin_test;") + + tk.MustExec("drop table if exists temporary_admin_checksum_table_with_index_test;") + tk.MustExec("drop table if exists temporary_admin_checksum_table_without_index_test;") + tk.MustExec("create global temporary table temporary_admin_checksum_table_with_index_test (id int, count int, PRIMARY KEY(id), KEY(count)) ON COMMIT DELETE ROWS;") + tk.MustExec("create global temporary table temporary_admin_checksum_table_without_index_test (id int, count int, PRIMARY KEY(id)) ON COMMIT DELETE ROWS;") + _, err = tk.Exec("admin checksum table temporary_admin_checksum_table_with_index_test;") + c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("admin checksum table").Error()) + _, err = tk.Exec("admin checksum table temporary_admin_checksum_table_without_index_test;") + c.Assert(err.Error(), Equals, core.ErrOptOnTemporaryTable.GenWithStackByArgs("admin checksum table").Error()) + tk.MustExec("drop table if exists temporary_admin_checksum_table_with_index_test,temporary_admin_checksum_table_without_index_test;") } func (s *testSuite5) TestAdminRecoverIndex(c *C) { diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index 6640af5785b81..2189f645e32b1 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -3471,6 +3471,9 @@ func (b *PlanBuilder) buildDDL(ctx context.Context, node ast.DDLNode) (Plan, err b.visitInfo = appendVisitInfo(b.visitInfo, mysql.IndexPriv, v.Table.Schema.L, v.Table.Name.L, "", authErr) case *ast.CreateTableStmt: + if v.TemporaryKeyword != ast.TemporaryNone && v.ReferTable != nil { + return nil, ErrOptOnTemporaryTable.GenWithStackByArgs("create table like") + } if b.ctx.GetSessionVars().User != nil { authErr = ErrTableaccessDenied.GenWithStackByArgs("CREATE", b.ctx.GetSessionVars().User.AuthUsername, b.ctx.GetSessionVars().User.AuthHostname, v.Table.Name.L) diff --git a/planner/core/preprocess.go b/planner/core/preprocess.go index c23d4c2df170e..29ad5c848dbeb 100644 --- a/planner/core/preprocess.go +++ b/planner/core/preprocess.go @@ -608,8 +608,12 @@ func (p *preprocessor) checkAdminCheckTableGrammar(stmt *ast.AdminStmt) { return } tempTableType := tableInfo.Meta().TempTableType - if stmt.Tp == ast.AdminCheckTable && tempTableType != model.TempTableNone { - p.err = infoschema.ErrAdminCheckTable + if (stmt.Tp == ast.AdminCheckTable || stmt.Tp == ast.AdminChecksumTable) && tempTableType != model.TempTableNone { + if stmt.Tp == ast.AdminChecksumTable { + p.err = ErrOptOnTemporaryTable.GenWithStackByArgs("admin checksum table") + } else { + p.err = ErrOptOnTemporaryTable.GenWithStackByArgs("admin check table") + } return } }