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

Support add/drop attributes for table or partition #1271

Merged
merged 9 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
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
45 changes: 45 additions & 0 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2213,6 +2213,7 @@ const (
AlterTableForce
AlterTableAddPartitions
AlterTableAlterPartition
AlterTablePartitionAttributes
AlterTableCoalescePartitions
AlterTableDropPartition
AlterTableTruncatePartition
Expand Down Expand Up @@ -2244,6 +2245,7 @@ const (
AlterTablePlacement
AlterTableAddStatistics
AlterTableDropStatistics
AlterTableAttributes
)

// LockType is the type for AlterTableSpec.
Expand Down Expand Up @@ -2343,6 +2345,7 @@ type AlterTableSpec struct {
PlacementSpecs []*PlacementSpec
Writeable bool
Statistics *StatisticsSpec
AttributesSpec *AttributesSpec
}

type TiFlashReplicaSpec struct {
Expand Down Expand Up @@ -2648,6 +2651,15 @@ func (n *AlterTableSpec) Restore(ctx *format.RestoreCtx) error {
return errors.Annotatef(err, "An error occurred while restore AlterTableSpec.PlacementSpecs[%d]", i)
}
}
case AlterTablePartitionAttributes:
ctx.WriteKeyWord("PARTITION ")
ctx.WriteName(n.PartitionNames[0].O)
ctx.WritePlain(" ")

spec := n.AttributesSpec
if err := spec.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore AlterTableSpec.AttributesSpec")
}
case AlterTableCoalescePartitions:
ctx.WriteKeyWord("COALESCE PARTITION ")
if n.NoWriteToBinlog {
Expand Down Expand Up @@ -2847,6 +2859,12 @@ func (n *AlterTableSpec) Restore(ctx *format.RestoreCtx) error {
return errors.Annotatef(err, "An error occurred while restore AlterTableSpec.PlacementSpecs[%d]", i)
}
}
case AlterTableAttributes:
spec := n.AttributesSpec
if err := spec.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore AlterTableSpec.AttributesSpec")
}

default:
// TODO: not support
ctx.WritePlainf(" /* AlterTableType(%d) is not supported */ ", n.Tp)
Expand Down Expand Up @@ -3668,6 +3686,33 @@ func (n *PlacementSpec) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

type AttributesSpec struct {
node

Attributes string
Default bool
}

func (n *AttributesSpec) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("ATTRIBUTES")
ctx.WritePlain("=")
if n.Default {
ctx.WriteKeyWord("DEFAULT")
return nil
}
ctx.WriteString(n.Attributes)
return nil
}

func (n *AttributesSpec) Accept(v Visitor) (Node, bool) {
newNode, skipChildren := v.Enter(n)
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*AttributesSpec)
return v.Leave(n)
}

// AlterSequenceStmt is a statement to alter sequence option.
type AlterSequenceStmt struct {
ddlNode
Expand Down
2 changes: 1 addition & 1 deletion ast/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (ts *testDDLSuite) TestDDLVisitorCover(c *C) {
ce := &checkExpr{}
constraint := &Constraint{Keys: []*IndexPartSpecification{{Column: &ColumnName{}}, {Column: &ColumnName{}}}, Refer: &ReferenceDef{}, Option: &IndexOption{}}

alterTableSpec := &AlterTableSpec{Constraint: constraint, Options: []*TableOption{{}}, NewTable: &TableName{}, NewColumns: []*ColumnDef{{Name: &ColumnName{}}}, OldColumnName: &ColumnName{}, Position: &ColumnPosition{RelativeColumn: &ColumnName{}}, PlacementSpecs: []*PlacementSpec{{}, {}}}
alterTableSpec := &AlterTableSpec{Constraint: constraint, Options: []*TableOption{{}}, NewTable: &TableName{}, NewColumns: []*ColumnDef{{Name: &ColumnName{}}}, OldColumnName: &ColumnName{}, Position: &ColumnPosition{RelativeColumn: &ColumnName{}}, PlacementSpecs: []*PlacementSpec{{}, {}}, AttributesSpec: &AttributesSpec{}}

stmts := []struct {
node Node
Expand Down
1 change: 1 addition & 0 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ var tokenMap = map[string]int{
"AS": as,
"ASC": asc,
"ASCII": ascii,
"ATTRIBUTES": attributes,
"AUTO_ID_CACHE": autoIdCache,
"AUTO_INCREMENT": autoIncrement,
"AUTO_RANDOM": autoRandom,
Expand Down
4 changes: 4 additions & 0 deletions model/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ const (
ActionAlterTableAlterPartition ActionType = 46
ActionRenameTables ActionType = 47
ActionDropIndexes ActionType = 48
ActionAlterTableAttributes ActionType = 49
ActionAlterTablePartitionAttributes ActionType = 50
)

var actionMap = map[ActionType]string{
Expand Down Expand Up @@ -129,6 +131,8 @@ var actionMap = map[ActionType]string{
ActionAlterCheckConstraint: "alter check constraint",
ActionAlterTableAlterPartition: "alter partition",
ActionDropIndexes: "drop multi-indexes",
ActionAlterTableAttributes: "alter table attributes",
ActionAlterTablePartitionAttributes: "alter table partition attributes",
}

// String return current ddl action in string
Expand Down
Loading