Skip to content

Commit

Permalink
parser: support create and alter placement policy (#1313)
Browse files Browse the repository at this point in the history
  • Loading branch information
AilinKid authored Aug 23, 2021
1 parent 7a79409 commit 562fed2
Show file tree
Hide file tree
Showing 5 changed files with 8,954 additions and 8,739 deletions.
70 changes: 70 additions & 0 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ import (
var (
_ DDLNode = &AlterTableStmt{}
_ DDLNode = &AlterSequenceStmt{}
_ DDLNode = &AlterPlacementPolicyStmt{}
_ DDLNode = &CreateDatabaseStmt{}
_ DDLNode = &CreateIndexStmt{}
_ DDLNode = &CreateTableStmt{}
_ DDLNode = &CreateViewStmt{}
_ DDLNode = &CreateSequenceStmt{}
_ DDLNode = &CreatePlacementPolicyStmt{}
_ DDLNode = &DropDatabaseStmt{}
_ DDLNode = &DropIndexStmt{}
_ DDLNode = &DropTableStmt{}
Expand Down Expand Up @@ -1429,6 +1431,41 @@ func (n *CreateViewStmt) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// CreatePlacementPolicyStmt is a statement to create a policy.
type CreatePlacementPolicyStmt struct {
ddlNode

IfNotExists bool
PolicyName model.CIStr
PlacementOptions []*PlacementOption
}

// Restore implements Node interface.
func (n *CreatePlacementPolicyStmt) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("CREATE PLACEMENT POLICY ")
if n.IfNotExists {
ctx.WriteKeyWord("IF NOT EXISTS ")
}
ctx.WriteName(n.PolicyName.O)
for i, option := range n.PlacementOptions {
ctx.WritePlain(" ")
if err := option.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while splicing CreatePlacementPolicy TableOption: [%v]", i)
}
}
return nil
}

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

// CreateSequenceStmt is a statement to create a Sequence.
type CreateSequenceStmt struct {
ddlNode
Expand Down Expand Up @@ -3897,6 +3934,39 @@ func (n *AttributesSpec) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// AlterPlacementPolicyStmt is a statement to alter placement policy option.
type AlterPlacementPolicyStmt struct {
ddlNode

PolicyName model.CIStr
IfExists bool
PlacementOptions []*PlacementOption
}

func (n *AlterPlacementPolicyStmt) Restore(ctx *format.RestoreCtx) error {
ctx.WriteKeyWord("ALTER PLACEMENT POLICY ")
if n.IfExists {
ctx.WriteKeyWord("IF EXISTS ")
}
ctx.WriteName(n.PolicyName.O)
for i, option := range n.PlacementOptions {
ctx.WritePlain(" ")
if err := option.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while splicing AlterPlacementPolicyStmt TableOption: [%v]", i)
}
}
return nil
}

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

// AlterSequenceStmt is a statement to alter sequence option.
type AlterSequenceStmt struct {
ddlNode
Expand Down
6 changes: 6 additions & 0 deletions model/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ const (
ActionDropIndexes ActionType = 48
ActionAlterTableAttributes ActionType = 49
ActionAlterTablePartitionAttributes ActionType = 50
ActionCreatePlacementPolicy ActionType = 51
ActionAlterPlacementPolicy ActionType = 52
ActionDropPlacementPolicy ActionType = 53
)

var actionMap = map[ActionType]string{
Expand Down Expand Up @@ -133,6 +136,9 @@ var actionMap = map[ActionType]string{
ActionDropIndexes: "drop multi-indexes",
ActionAlterTableAttributes: "alter table attributes",
ActionAlterTablePartitionAttributes: "alter table partition attributes",
ActionCreatePlacementPolicy: "create placement policy",
ActionAlterPlacementPolicy: "alter placement policy",
ActionDropPlacementPolicy: "drop placement policy",
}

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

0 comments on commit 562fed2

Please sign in to comment.