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

table partition: enhance auto id for exchange partition with table #36663

Closed
wants to merge 11 commits into from
1 change: 1 addition & 0 deletions build/nogo_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@
},
"only_files": {
"planner/core/rule_partition_eliminate.go": "planner/core/rule_partition_eliminate code",
"infoschema/builder.go": "infoschema/builder.go code",
"distsql/": "ignore distsql code",
"dumpling/export": "dumpling/export code",
"lock/": "lock file",
Expand Down
70 changes: 67 additions & 3 deletions infoschema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/util/domainutil"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/mathutil"
"github.com/pingcap/tidb/util/sqlexec"
"go.uber.org/zap"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -74,7 +75,7 @@ func (b *bundleInfoBuilder) SetDeltaUpdateBundles() {
b.deltaUpdate = true
}

func (b *bundleInfoBuilder) deleteBundle(is *infoSchema, tblID int64) {
func (_ *bundleInfoBuilder) deleteBundle(is *infoSchema, tblID int64) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

func (*bundleInfoBuilder)

delete(is.ruleBundleMap, tblID)
}

Expand Down Expand Up @@ -215,6 +216,8 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro
return b.applyRecoverTable(m, diff)
case model.ActionCreateTables:
return b.applyCreateTables(m, diff)
case model.ActionExchangeTablePartition:
return b.applyExchangePartitionWithTable(m, diff)
default:
return b.applyDefaultAction(m, diff)
}
Expand Down Expand Up @@ -242,6 +245,67 @@ func (b *Builder) applyCreateTables(m *meta.Meta, diff *model.SchemaDiff) ([]int
return tblIDs, nil
}

func (b *Builder) applyExchangePartitionWithTable(m *meta.Meta, diff *model.SchemaDiff) ([]int64, error) {
tblIDs, err := b.applyTableUpdate(m, diff)
mjonss marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, errors.Trace(err)
}

for _, opt := range diff.AffectedOpts {
affectedDiff := &model.SchemaDiff{
Version: diff.Version,
Type: diff.Type,
SchemaID: opt.SchemaID,
TableID: opt.TableID,
OldSchemaID: opt.OldSchemaID,
OldTableID: opt.OldTableID,
}
affectedIDs, err := b.ApplyDiff(m, affectedDiff)
if err != nil {
return nil, errors.Trace(err)
}
tblIDs = append(tblIDs, affectedIDs...)

// handle partition table and table AutoID
err = updateAutoIDForExchangePartition(m, affectedDiff.SchemaID, affectedDiff.TableID, diff.SchemaID, diff.OldTableID)
if err != nil {
return nil, errors.Trace(err)
}
}

return tblIDs, nil
}

func updateAutoIDForExchangePartition(m *meta.Meta, ptSchemaID, ptID, ntSchemaID, ntID int64) error {
// partition table auto IDs.
ptAutoIDs, err := m.GetAutoIDAccessors(ptSchemaID, ptID).Get()
if err != nil {
return err
}
// non-partition table auto IDs.
ntAutoIDs, err := m.GetAutoIDAccessors(ntSchemaID, ntID).Get()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen in this case?

CREATE TABLE pt (a int unsigned auto_increment, b varchar(255)) PARTITION BY RANGE (a) (PARTITION p0 VALUES LESS THAN (1000000), partition p1M values less than (2000000));
create table t (a int unsigned auto_increment, b varchar(255));
insert into pt values (1, "1");
insert into t values (2, "2");
insert into t values (4000000, "4M");
delete from t where a = 4000000;
alter table pt exchange partition p0 with table t;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it to the test,RowID is equal to 4005000.

if err != nil {
return err
}

// Set both tables to the maximum auto IDs between normal table and partitioned table.
newAutoIDs := meta.AutoIDGroup{
RowID: mathutil.Max(ptAutoIDs.RowID, ntAutoIDs.RowID),
IncrementID: mathutil.Max(ptAutoIDs.IncrementID, ntAutoIDs.IncrementID),
RandomID: mathutil.Max(ptAutoIDs.RandomID, ntAutoIDs.RandomID),
}
err = m.GetAutoIDAccessors(ptSchemaID, ptID).Put(newAutoIDs)
if err != nil {
return err
}
err = m.GetAutoIDAccessors(ntSchemaID, ntID).Put(newAutoIDs)
if err != nil {
return err
}

return nil
}

func (b *Builder) applyTruncateTableOrPartition(m *meta.Meta, diff *model.SchemaDiff) ([]int64, error) {
tblIDs, err := b.applyTableUpdate(m, diff)
if err != nil {
Expand Down Expand Up @@ -529,8 +593,8 @@ func (b *Builder) applyModifySchemaDefaultPlacement(m *meta.Meta, diff *model.Sc
return nil
}

func (b *Builder) applyDropPolicy(PolicyID int64) []int64 {
po, ok := b.is.PolicyByID(PolicyID)
func (b *Builder) applyDropPolicy(policyID int64) []int64 {
po, ok := b.is.PolicyByID(policyID)
if !ok {
return nil
}
Expand Down