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

fix reassigned partition id in truncate table does not take effect #8103

Merged
merged 5 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3008,6 +3008,32 @@ func (s *testDBSuite) TestTruncatePartitionAndDropTable(c *C) {
hasOldPartitionData = checkPartitionDelRangeDone(c, s, partitionPrefix)
c.Assert(hasOldPartitionData, IsFalse)
s.testErrorCode(c, "select * from t4;", tmysql.ErrNoSuchTable)

// Test truncate table partition reassign a new partitionIDs.
s.tk.MustExec("drop table if exists t5;")
s.tk.MustExec("set @@session.tidb_enable_table_partition=1;")
s.tk.MustExec(`create table t5(
id int, name varchar(50),
purchased date
)
partition by range( year(purchased) ) (
partition p0 values less than (1990),
partition p1 values less than (1995),
partition p2 values less than (2000),
partition p3 values less than (2005),
partition p4 values less than (2010),
partition p5 values less than (2015)
);`)
is = domain.GetDomain(ctx).InfoSchema()
oldTblInfo, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t5"))
c.Assert(err, IsNil)
oldPID = oldTblInfo.Meta().Partition.Definitions[0].ID
s.tk.MustExec("truncate table t5;")
is = domain.GetDomain(ctx).InfoSchema()
c.Assert(err, IsNil)
newTblInfo, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t5"))
newPID := newTblInfo.Meta().Partition.Definitions[0].ID
c.Assert(oldPID != newPID, IsTrue)
}

func (s *testDBSuite) TestPartitionUniqueKeyNeedAllFieldsInPf(c *C) {
Expand Down
21 changes: 21 additions & 0 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,24 @@ func isRangePartitionColUnsignedBigint(cols []*table.Column, pi *model.Partition
}
return false
}

// truncateTableByReassignPartitionIDs reassign a new partition ids.
func truncateTableByReassignPartitionIDs(job *model.Job, 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()
if err != nil {
job.State = model.JobStateCancelled
return errors.Trace(err)
}
newDef := model.PartitionDefinition{
ID: pid,
Name: def.Name,
LessThan: def.LessThan,
Comment: def.Comment,
}
newDefs = append(newDefs, newDef)
}
tblInfo.Partition.Definitions = newDefs
return nil
}
13 changes: 4 additions & 9 deletions ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,13 @@ func onTruncateTable(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ erro
return ver, errors.Trace(err)
}

// We use the new partition ID because all the old data is encoded with the old partition ID, it can not be accessed anymore.
var oldPartitionIDs []int64
if tblInfo.GetPartitionInfo() != nil {
oldPartitionIDs = getPartitionIDs(tblInfo)
for _, def := range tblInfo.Partition.Definitions {
var pid int64
pid, err = t.GenGlobalID()
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}
def.ID = pid
// 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(job, t, tblInfo)
if err != nil {
return ver, errors.Trace(err)
}
}

Expand Down