Skip to content

Commit

Permalink
ddl: add check table compatibility for temporary table (#24501)
Browse files Browse the repository at this point in the history
  • Loading branch information
Howie59 authored May 12, 2021
1 parent 6a69ee7 commit 89c0e6c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
5 changes: 5 additions & 0 deletions errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,11 @@ error = '''
Unknown SEQUENCE: '%-.300s'
'''

["schema:8003"]
error = '''
TiDB admin check table failed.
'''

["schema:8020"]
error = '''
Table '%s' was locked in %s by %v
Expand Down
16 changes: 16 additions & 0 deletions executor/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ func (s *testSuite5) TestAdminCheckIndex(c *C) {
check()
}

func (s *testSuite5) TestAdminCheckIndexInTemporaryMode(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
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)
tk.MustExec("drop table if exists temporary_admin_test;")

tk.MustExec("drop table if exists non_temporary_admin_test;")
tk.MustExec("create table non_temporary_admin_test (c1 int, c2 int, c3 int default 1, primary key (c1), index (c1), unique key(c2));")
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;")
}

func (s *testSuite5) TestAdminRecoverIndex(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
8 changes: 6 additions & 2 deletions executor/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,12 @@ func (e *DDLExec) dropTableObject(objects []*ast.TableName, obt objectType, ifEx
if isSystemTable(tn.Schema.L, tn.Name.L) {
return errors.Errorf("Drop tidb system table '%s.%s' is forbidden", tn.Schema.L, tn.Name.L)
}

if obt == tableObject && config.CheckTableBeforeDrop {
tableInfo, err := e.is.TableByName(tn.Schema, tn.Name)
if err != nil {
return err
}
tempTableType := tableInfo.Meta().TempTableType
if obt == tableObject && config.CheckTableBeforeDrop && tempTableType == model.TempTableNone {
logutil.BgLogger().Warn("admin check table before drop",
zap.String("database", fullti.Schema.O),
zap.String("table", fullti.Name.O),
Expand Down
2 changes: 2 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9161,8 +9161,10 @@ func (s *testIntegrationSuite) TestIssue24429(c *C) {

tk.MustExec("set @@sql_mode = ANSI_QUOTES;")
tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a int);")
tk.MustQuery(`select t."a"=10 from t;`).Check(testkit.Rows())
tk.MustExec("drop table if exists t;")
}

func (s *testIntegrationSuite) TestVitessHash(c *C) {
Expand Down
2 changes: 2 additions & 0 deletions infoschema/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,6 @@ var (
ErrTableLocked = dbterror.ClassSchema.NewStd(mysql.ErrTableLocked)
// ErrWrongObject returns when the table/view/sequence is not the expected object.
ErrWrongObject = dbterror.ClassSchema.NewStd(mysql.ErrWrongObject)
// ErrAdminCheckTable returns when the check table in temporary mode.
ErrAdminCheckTable = dbterror.ClassSchema.NewStd(mysql.ErrAdminCheckTable)
)
27 changes: 27 additions & 0 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ type preprocessor struct {

func (p *preprocessor) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
switch node := in.(type) {
case *ast.AdminStmt:
p.checkAdminCheckTableGrammar(node)
case *ast.DeleteStmt:
p.stmtTp = TypeDelete
case *ast.SelectStmt:
Expand Down Expand Up @@ -557,6 +559,31 @@ func (p *preprocessor) checkDropDatabaseGrammar(stmt *ast.DropDatabaseStmt) {
}
}

func (p *preprocessor) checkAdminCheckTableGrammar(stmt *ast.AdminStmt) {
for _, table := range stmt.Tables {
currentDB := p.ctx.GetSessionVars().CurrentDB
if table.Schema.String() != "" {
currentDB = table.Schema.L
}
if currentDB == "" {
p.err = errors.Trace(ErrNoDB)
return
}
sName := model.NewCIStr(currentDB)
tName := table.Name
tableInfo, err := p.is.TableByName(sName, tName)
if err != nil {
p.err = err
return
}
tempTableType := tableInfo.Meta().TempTableType
if stmt.Tp == ast.AdminCheckTable && tempTableType != model.TempTableNone {
p.err = infoschema.ErrAdminCheckTable
return
}
}
}

func (p *preprocessor) checkCreateTableGrammar(stmt *ast.CreateTableStmt) {
tName := stmt.Table.Name.String()
if isIncorrectName(tName) {
Expand Down

0 comments on commit 89c0e6c

Please sign in to comment.