From 99e82e3e9d10a969ac7b66a0c39d9511e2d76bff Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Fri, 24 Nov 2023 10:43:12 +0800 Subject: [PATCH] ddl: fix recover table by JobID bug when JobID is set to 0 tidb-server panic (#46343) (#48085) close pingcap/tidb#46296 --- executor/ddl.go | 7 ++++--- executor/recover_test.go | 5 +++++ parser/ast/ddl.go | 8 ++++---- parser/parser_test.go | 1 + 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/executor/ddl.go b/executor/ddl.go index 9e5b7f30ae33e..485094134ad0e 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -385,10 +385,11 @@ func (e *DDLExec) executeRecoverTable(s *ast.RecoverTableStmt) error { var job *model.Job var err error var tblInfo *model.TableInfo - if s.JobID != 0 { - job, tblInfo, err = e.getRecoverTableByJobID(s, dom) - } else { + // Let check table first. Related isssue #46296. + if s.Table != nil { job, tblInfo, err = e.getRecoverTableByTableName(s.Table) + } else { + job, tblInfo, err = e.getRecoverTableByJobID(s, dom) } if err != nil { return err diff --git a/executor/recover_test.go b/executor/recover_test.go index 75204c734638f..77042895d210f 100644 --- a/executor/recover_test.go +++ b/executor/recover_test.go @@ -92,6 +92,11 @@ func TestRecoverTable(t *testing.T) { err := tk.ExecToErr(fmt.Sprintf("recover table by job %d", 10000000)) require.Error(t, err) + // recover table by zero JobID. + // related issue: https://github.com/pingcap/tidb/issues/46296 + err = tk.ExecToErr(fmt.Sprintf("recover table by job %d", 0)) + require.Error(t, err) + // Disable GC by manual first, then after recover table, the GC enable status should also be disabled. require.NoError(t, gcutil.DisableGC(tk.Session())) diff --git a/parser/ast/ddl.go b/parser/ast/ddl.go index b9b8c6d1feb23..5046898f9e85e 100644 --- a/parser/ast/ddl.go +++ b/parser/ast/ddl.go @@ -4085,16 +4085,16 @@ type RecoverTableStmt struct { // Restore implements Node interface. func (n *RecoverTableStmt) Restore(ctx *format.RestoreCtx) error { ctx.WriteKeyWord("RECOVER TABLE ") - if n.JobID != 0 { - ctx.WriteKeyWord("BY JOB ") - ctx.WritePlainf("%d", n.JobID) - } else { + if n.Table != nil { if err := n.Table.Restore(ctx); err != nil { return errors.Annotate(err, "An error occurred while splicing RecoverTableStmt Table") } if n.JobNum > 0 { ctx.WritePlainf(" %d", n.JobNum) } + } else { + ctx.WriteKeyWord("BY JOB ") + ctx.WritePlainf("%d", n.JobID) } return nil } diff --git a/parser/parser_test.go b/parser/parser_test.go index 379cd44536cad..a5fc245adecdb 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -3249,6 +3249,7 @@ func TestDDL(t *testing.T) { {"recover table by job 11", true, "RECOVER TABLE BY JOB 11"}, {"recover table by job 11,12,13", false, ""}, {"recover table by job", false, ""}, + {"recover table by job 0", true, "RECOVER TABLE BY JOB 0"}, {"recover table t1", true, "RECOVER TABLE `t1`"}, {"recover table t1,t2", false, ""}, {"recover table ", false, ""},