Skip to content

Commit

Permalink
fix recover table by JobID panic bug when JobID is set to 0
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyfhust committed Oct 27, 2023
1 parent 30288c7 commit e203803
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
7 changes: 4 additions & 3 deletions pkg/executor/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,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
Expand Down
5 changes: 5 additions & 0 deletions pkg/executor/recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))

Expand Down
8 changes: 4 additions & 4 deletions pkg/parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4381,16 +4381,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
}
Expand Down
1 change: 1 addition & 0 deletions pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3373,6 +3373,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, ""},
Expand Down

0 comments on commit e203803

Please sign in to comment.