Skip to content

Commit

Permalink
*: Remove ALTER TABLE ... ALTER PARTITION ... (#29556)
Browse files Browse the repository at this point in the history
  • Loading branch information
lcwangchao authored Nov 8, 2021
1 parent 5d9eb5b commit 49d995d
Show file tree
Hide file tree
Showing 10 changed files with 5,652 additions and 6,299 deletions.
63 changes: 0 additions & 63 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/ddl/label"
"github.com/pingcap/tidb/ddl/placement"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
Expand Down Expand Up @@ -2765,12 +2764,6 @@ func (d *ddl) AlterTable(ctx context.Context, sctx sessionctx.Context, ident ast
newIdent := ast.Ident{Schema: spec.NewTable.Schema, Name: spec.NewTable.Name}
isAlterTable := true
err = d.RenameTable(sctx, ident, newIdent, isAlterTable)
case ast.AlterTableAlterPartition:
if sctx.GetSessionVars().EnableAlterPlacement {
err = d.AlterTableAlterPartition(sctx, ident, spec)
} else {
err = errors.New("alter partition alter placement is experimental and it is switched off by tidb_enable_alter_placement")
}
case ast.AlterTablePartition:
// Prevent silent succeed if user executes ALTER TABLE x PARTITION BY ...
err = errors.New("alter table partition is unsupported")
Expand Down Expand Up @@ -6305,62 +6298,6 @@ func (d *ddl) AlterIndexVisibility(ctx sessionctx.Context, ident ast.Ident, inde
return errors.Trace(err)
}

func (d *ddl) AlterTableAlterPartition(ctx sessionctx.Context, ident ast.Ident, spec *ast.AlterTableSpec) (err error) {
schema, tb, err := d.getSchemaAndTableByIdent(ctx, ident)
if err != nil {
return errors.Trace(err)
}

meta := tb.Meta()
if meta.Partition == nil {
return errors.Trace(ErrPartitionMgmtOnNonpartitioned)
}

partitionID, err := tables.FindPartitionByName(meta, spec.PartitionNames[0].L)
if err != nil {
return errors.Trace(err)
}

// TODO: the old placement rules should be migrated to new format. use the bundle from meta directly.
bundle := infoschema.GetBundle(d.infoCache.GetLatest(), []int64{partitionID, meta.ID, schema.ID})

err = bundle.ApplyPlacementSpec(spec.PlacementSpecs)
if err != nil {
var sb strings.Builder
sb.Reset()

restoreCtx := format.NewRestoreCtx(format.RestoreStringSingleQuotes|format.RestoreKeyWordLowercase|format.RestoreNameBackQuotes, &sb)

if e := spec.Restore(restoreCtx); e != nil {
return ErrInvalidPlacementSpec.GenWithStackByArgs("", err)
}
return ErrInvalidPlacementSpec.GenWithStackByArgs(sb.String(), err)
}

err = bundle.Tidy()
if err != nil {
return errors.Trace(err)
}
bundle.Reset(placement.RuleIndexPartition, []int64{partitionID})

job := &model.Job{
SchemaID: schema.ID,
TableID: meta.ID,
SchemaName: schema.Name.L,
Type: model.ActionAlterTableAlterPartition,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{partitionID, bundle},
}

err = d.doDDLJob(ctx, job)
if err != nil {
return errors.Trace(err)
}

err = d.callHookOnChanged(err)
return errors.Trace(err)
}

func (d *ddl) AlterTableAttributes(ctx sessionctx.Context, ident ast.Ident, spec *ast.AlterTableSpec) error {
schema, tb, err := d.getSchemaAndTableByIdent(ctx, ident)
if err != nil {
Expand Down
14 changes: 0 additions & 14 deletions ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,6 @@ func skipWriteBinlog(job *model.Job) bool {
// it's used to update table's TiFlash replica available status.
case model.ActionUpdateTiFlashReplicaStatus:
return true
// It is done without modifying table info, bin log is not needed
case model.ActionAlterTableAlterPartition:
return true
}

return false
Expand Down Expand Up @@ -818,8 +815,6 @@ func (w *worker) runDDLJob(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64,
ver, err = onCreateSequence(d, t, job)
case model.ActionAlterIndexVisibility:
ver, err = onAlterIndexVisibility(t, job)
case model.ActionAlterTableAlterPartition:
ver, err = onAlterTableAlterPartition(t, job)
case model.ActionAlterSequence:
ver, err = onAlterSequence(t, job)
case model.ActionRenameTables:
Expand Down Expand Up @@ -1060,15 +1055,6 @@ func updateSchemaVersion(t *meta.Meta, job *model.Job) (int64, error) {
diff.AffectedOpts = buildPlacementAffects(oldIDs, oldIDs)
}
}
case model.ActionAlterTableAlterPartition:
diff.TableID = job.TableID
if len(job.CtxVars) > 0 {
diff.AffectedOpts = []*model.AffectedOption{
{
TableID: job.CtxVars[0].(int64),
},
}
}
default:
diff.TableID = job.TableID
}
Expand Down
47 changes: 0 additions & 47 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1853,53 +1853,6 @@ func truncateTableByReassignPartitionIDs(t *meta.Meta, tblInfo *model.TableInfo)
return nil
}

func onAlterTableAlterPartition(t *meta.Meta, job *model.Job) (ver int64, err error) {
var partitionID int64
bundle := &placement.Bundle{}
err = job.DecodeArgs(&partitionID, bundle)
if err != nil {
job.State = model.JobStateCancelled
return 0, errors.Trace(err)
}

tblInfo, err := getTableInfoAndCancelFaultJob(t, job, job.SchemaID)
if err != nil {
return 0, err
}

ptInfo := tblInfo.GetPartitionInfo()
if ptInfo.GetNameByID(partitionID) == "" {
job.State = model.JobStateCancelled
return 0, errors.Trace(table.ErrUnknownPartition.GenWithStackByArgs("drop?", tblInfo.Name.O))
}

pstate := ptInfo.GetStateByID(partitionID)
switch pstate {
case model.StatePublic:
ptInfo.SetStateByID(partitionID, model.StateGlobalTxnOnly)
ver, err = updateVersionAndTableInfo(t, job, tblInfo, true)
if err != nil {
return ver, errors.Trace(err)
}
job.SchemaState = model.StateGlobalTxnOnly
case model.StateGlobalTxnOnly:
err = infosync.PutRuleBundles(context.TODO(), []*placement.Bundle{bundle})
if err != nil {
job.State = model.JobStateCancelled
return 0, errors.Wrapf(err, "failed to notify PD the placement rules")
}
ptInfo.SetStateByID(partitionID, model.StatePublic)
// used by ApplyDiff in updateSchemaVersion
job.CtxVars = []interface{}{partitionID}
ver, err = updateVersionAndTableInfo(t, job, tblInfo, true)
if err != nil {
return ver, errors.Trace(err)
}
job.FinishTableJob(model.JobStateDone, model.StatePublic, ver, tblInfo)
}
return ver, nil
}

type partitionExprProcessor func(sessionctx.Context, *model.TableInfo, ast.ExprNode) error

type partitionExprChecker struct {
Expand Down
Loading

0 comments on commit 49d995d

Please sign in to comment.