From b5659eded88e951f37632527256a5b720ed902d7 Mon Sep 17 00:00:00 2001 From: lihaowei Date: Sat, 22 May 2021 15:34:49 +0800 Subject: [PATCH] check create table like Signed-off-by: lihaowei --- ddl/error.go | 1 + ddl/serial_test.go | 10 ++++++++++ planner/core/preprocess.go | 15 +++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/ddl/error.go b/ddl/error.go index 86727c95e2ef5..d7e109a0c5341 100644 --- a/ddl/error.go +++ b/ddl/error.go @@ -278,6 +278,7 @@ var ( // ErrPartitionNoTemporary returns when partition at temporary mode ErrPartitionNoTemporary = dbterror.ClassDDL.NewStd(mysql.ErrPartitionNoTemporary) + // ErrOptOnTemporaryTable returns when exec unsupported opt at temporary mode ErrOptOnTemporaryTable = dbterror.ClassDDL.NewStd(mysql.ErrOptOnTemporaryTable) errUnsupportedOnCommitPreserve = dbterror.ClassDDL.NewStdErr(mysql.ErrUnsupportedDDLOperation, parser_mysql.Message("TiDB doesn't support ON COMMIT PRESERVE ROWS for now", nil)) diff --git a/ddl/serial_test.go b/ddl/serial_test.go index b891cce567392..f78ff3d16577b 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -37,6 +37,7 @@ import ( "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/meta/autoid" + "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/sessionctx/variable" @@ -524,6 +525,15 @@ func (s *testSerialSuite) TestCreateTableWithLike(c *C) { tk.MustExec("drop database ctwl_db") tk.MustExec("drop database ctwl_db1") + + // Test create table like at temporary mode. + tk.MustExec("use test") + tk.MustExec("drop table if exists temporary_table;") + tk.MustExec("create global temporary table temporary_table (a int, b int,index(a)) on commit delete rows") + tk.MustExec("drop table if exists temporary_table_t1;") + _, 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;") } // TestCancelAddIndex1 tests canceling ddl job when the add index worker is not started. diff --git a/planner/core/preprocess.go b/planner/core/preprocess.go index b5caf55e8de03..ca436189c0d41 100644 --- a/planner/core/preprocess.go +++ b/planner/core/preprocess.go @@ -585,6 +585,21 @@ func (p *preprocessor) checkAdminCheckTableGrammar(stmt *ast.AdminStmt) { } func (p *preprocessor) checkCreateTableGrammar(stmt *ast.CreateTableStmt) { + if stmt.ReferTable != nil { + schema := model.NewCIStr(p.ctx.GetSessionVars().CurrentDB) + if stmt.ReferTable.Schema.String() != "" { + schema = stmt.ReferTable.Schema + } + tableInfo, err := p.is.TableByName(schema, stmt.ReferTable.Name) + if err != nil { + p.err = err + return + } + if tableInfo.Meta().TempTableType != model.TempTableNone { + p.err = ErrOptOnTemporaryTable.GenWithStackByArgs("create table like") + return + } + } tName := stmt.Table.Name.String() if isIncorrectName(tName) { p.err = ddl.ErrWrongTableName.GenWithStackByArgs(tName)