Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: Fix issue with truncate table and partitions and tiflash #42957

Merged
merged 3 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
mjonss marked this conversation as resolved.
Show resolved Hide resolved
// 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