From 45bf86b5174cab36858ad00b833cda6147d89f36 Mon Sep 17 00:00:00 2001 From: ciscoxll Date: Tue, 16 Oct 2018 20:30:52 +0800 Subject: [PATCH 1/4] fix reassigned partition id in truncate table does not take effect --- ddl/db_test.go | 27 +++++++++++++++++++++++++++ ddl/partition.go | 21 +++++++++++++++++++++ ddl/table.go | 13 ++++--------- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/ddl/db_test.go b/ddl/db_test.go index e7a4f3a687fa7..72e8f3f9a4729 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -3008,6 +3008,33 @@ 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) { diff --git a/ddl/partition.go b/ddl/partition.go index b1110209c3e3b..0e375eafbd929 100644 --- a/ddl/partition.go +++ b/ddl/partition.go @@ -417,3 +417,24 @@ func isRangePartitionColUnsignedBigint(cols []*table.Column, pi *model.Partition } return false } + +// truncateTableFromReassignPartitionIDs reassign a new partition ids. +func truncateTableFromReassignPartitionIDs(job *model.Job, t *meta.Meta, tblInfo *model.TableInfo) (ver int64, _ 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 ver, 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 ver, nil +} diff --git a/ddl/table.go b/ddl/table.go index 930f871ff43d2..1b419382a43e7 100644 --- a/ddl/table.go +++ b/ddl/table.go @@ -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. + ver, err = truncateTableFromReassignPartitionIDs(job, t, tblInfo) + if err != nil { + return ver, errors.Trace(err) } } From 2fa6b5dc581df382281614549769d1451f42e8c8 Mon Sep 17 00:00:00 2001 From: ciscoxll Date: Wed, 17 Oct 2018 15:20:07 +0800 Subject: [PATCH 2/4] Address comment --- ddl/partition.go | 6 +++--- ddl/table.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ddl/partition.go b/ddl/partition.go index 0e375eafbd929..6ec9d1d3af7c3 100644 --- a/ddl/partition.go +++ b/ddl/partition.go @@ -419,13 +419,13 @@ func isRangePartitionColUnsignedBigint(cols []*table.Column, pi *model.Partition } // truncateTableFromReassignPartitionIDs reassign a new partition ids. -func truncateTableFromReassignPartitionIDs(job *model.Job, t *meta.Meta, tblInfo *model.TableInfo) (ver int64, _ error) { +func truncateTableFromReassignPartitionIDs(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 ver, errors.Trace(err) + return errors.Trace(err) } newDef := model.PartitionDefinition{ ID: pid, @@ -436,5 +436,5 @@ func truncateTableFromReassignPartitionIDs(job *model.Job, t *meta.Meta, tblInfo newDefs = append(newDefs, newDef) } tblInfo.Partition.Definitions = newDefs - return ver, nil + return nil } diff --git a/ddl/table.go b/ddl/table.go index 1b419382a43e7..6199b2c7d0873 100644 --- a/ddl/table.go +++ b/ddl/table.go @@ -213,7 +213,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. - ver, err = truncateTableFromReassignPartitionIDs(job, t, tblInfo) + err = truncateTableFromReassignPartitionIDs(job, t, tblInfo) if err != nil { return ver, errors.Trace(err) } From 09b6b23ca317e31701b4c9da7e1419455f414760 Mon Sep 17 00:00:00 2001 From: ciscoxll Date: Thu, 18 Oct 2018 21:57:50 +0800 Subject: [PATCH 3/4] Address comment --- ddl/partition.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/ddl/partition.go b/ddl/partition.go index 6ec9d1d3af7c3..82a5c4be4d6e0 100644 --- a/ddl/partition.go +++ b/ddl/partition.go @@ -420,21 +420,13 @@ func isRangePartitionColUnsignedBigint(cols []*table.Column, pi *model.Partition // truncateTableFromReassignPartitionIDs reassign a new partition ids. func truncateTableFromReassignPartitionIDs(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 { + for i := 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[i].ID = pid } - tblInfo.Partition.Definitions = newDefs return nil } From ff5da6bc446e1a4ada323c826594772dfd6071e3 Mon Sep 17 00:00:00 2001 From: ciscoxll Date: Tue, 23 Oct 2018 17:24:34 +0800 Subject: [PATCH 4/4] Address comment --- ddl/partition.go | 16 ++++++++++++---- ddl/table.go | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/ddl/partition.go b/ddl/partition.go index 82a5c4be4d6e0..16933b9bf6351 100644 --- a/ddl/partition.go +++ b/ddl/partition.go @@ -418,15 +418,23 @@ func isRangePartitionColUnsignedBigint(cols []*table.Column, pi *model.Partition return false } -// truncateTableFromReassignPartitionIDs reassign a new partition ids. -func truncateTableFromReassignPartitionIDs(job *model.Job, t *meta.Meta, tblInfo *model.TableInfo) error { - for i := range tblInfo.Partition.Definitions { +// 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) } - tblInfo.Partition.Definitions[i].ID = pid + newDef := model.PartitionDefinition{ + ID: pid, + Name: def.Name, + LessThan: def.LessThan, + Comment: def.Comment, + } + newDefs = append(newDefs, newDef) } + tblInfo.Partition.Definitions = newDefs return nil } diff --git a/ddl/table.go b/ddl/table.go index 6199b2c7d0873..45a5a2372193f 100644 --- a/ddl/table.go +++ b/ddl/table.go @@ -213,7 +213,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 = truncateTableFromReassignPartitionIDs(job, t, tblInfo) + err = truncateTableByReassignPartitionIDs(job, t, tblInfo) if err != nil { return ver, errors.Trace(err) }