Skip to content

Commit

Permalink
fix reassigned partition id in truncate table does not take effect (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ciscoxll authored and ngaut committed Oct 30, 2018
1 parent 56b8c4a commit 3e6f687
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
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 reassigns 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
20 changes: 20 additions & 0 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,23 @@ func isRangePartitionColUnsignedBigint(cols []*table.Column, pi *model.Partition
}
return false
}

// 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()
if err != nil {
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
}
14 changes: 5 additions & 9 deletions ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,14 @@ 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(t, tblInfo)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}
}

Expand Down

0 comments on commit 3e6f687

Please sign in to comment.