Skip to content

Commit

Permalink
ddl: Fix issue with truncate table and partitions and tiflash (#42957)
Browse files Browse the repository at this point in the history
close #42940
  • Loading branch information
mjonss authored Apr 12, 2023
1 parent 6122ca3 commit c1ce67d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
8 changes: 6 additions & 2 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6160,7 +6160,11 @@ func (d *ddl) TruncateTable(ctx sessionctx.Context, ti ast.Ident) error {
return errors.Trace(dbterror.ErrTruncateIllegalForeignKey.GenWithStackByArgs(msg))
}

genIDs, err := d.genGlobalIDs(1)
ids := 1
if tb.Meta().Partition != nil {
ids += len(tb.Meta().Partition.Definitions)
}
genIDs, err := d.genGlobalIDs(ids)
if err != nil {
return errors.Trace(err)
}
Expand All @@ -6172,7 +6176,7 @@ func (d *ddl) TruncateTable(ctx sessionctx.Context, ti ast.Ident) error {
TableName: tb.Meta().Name.L,
Type: model.ActionTruncateTable,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{newTableID, fkCheck},
Args: []interface{}{newTableID, fkCheck, genIDs[1:]},
}
if ok, _ := ctx.CheckTableLocked(tb.Meta().ID); ok && config.TableLockEnabled() {
// AddTableLock here to avoid this ddl job was executed successfully but the session was been kill before return.
Expand Down
15 changes: 10 additions & 5 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -3321,15 +3321,20 @@ func isPartExprUnsigned(tbInfo *model.TableInfo) bool {
}

// truncateTableByReassignPartitionIDs reassigns new partition ids.
func truncateTableByReassignPartitionIDs(t *meta.Meta, tblInfo *model.TableInfo) error {
newDefs := make([]model.PartitionDefinition, 0, len(tblInfo.Partition.Definitions))
for _, def := range tblInfo.Partition.Definitions {
pid, err := t.GenGlobalID()
func truncateTableByReassignPartitionIDs(t *meta.Meta, tblInfo *model.TableInfo, pids []int64) (err error) {
if len(pids) < len(tblInfo.Partition.Definitions) {
// To make it compatible with older versions when pids was not given
// and if there has been any add/reorganize partition increasing the number of partitions
morePids, err := t.GenGlobalIDs(len(tblInfo.Partition.Definitions) - len(pids))
if err != nil {
return errors.Trace(err)
}
pids = append(pids, morePids...)
}
newDefs := make([]model.PartitionDefinition, 0, len(tblInfo.Partition.Definitions))
for i, def := range tblInfo.Partition.Definitions {
newDef := def
newDef.ID = pid
newDef.ID = pids[i]
newDefs = append(newDefs, newDef)
}
tblInfo.Partition.Definitions = newDefs
Expand Down
5 changes: 3 additions & 2 deletions ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,8 @@ func onTruncateTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ erro
tableID := job.TableID
var newTableID int64
var fkCheck bool
err := job.DecodeArgs(&newTableID, &fkCheck)
var newPartitionIDs []int64
err := job.DecodeArgs(&newTableID, &fkCheck, &newPartitionIDs)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
Expand Down Expand Up @@ -733,7 +734,7 @@ func onTruncateTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ erro
if tblInfo.GetPartitionInfo() != nil {
oldPartitionIDs = getPartitionIDs(tblInfo)
// We use the new partition ID because all the old data is encoded with the old partition ID, it can not be accessed anymore.
err = truncateTableByReassignPartitionIDs(t, tblInfo)
err = truncateTableByReassignPartitionIDs(t, tblInfo, newPartitionIDs)
if err != nil {
return ver, errors.Trace(err)
}
Expand Down

0 comments on commit c1ce67d

Please sign in to comment.