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

add split table ... between ... and syntax #321

Merged
merged 8 commits into from
Jun 5, 2019
113 changes: 86 additions & 27 deletions ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
_ DMLNode = &SelectStmt{}
_ DMLNode = &ShowStmt{}
_ DMLNode = &LoadDataStmt{}
_ DMLNode = &SplitIndexRegionStmt{}
_ DMLNode = &SplitRegionStmt{}

_ Node = &Assignment{}
_ Node = &ByItem{}
Expand Down Expand Up @@ -2416,60 +2416,119 @@ func (n *FrameBound) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

type SplitIndexRegionStmt struct {
type SplitRegionStmt struct {
dmlNode

Table *TableName
IndexName string
Table *TableName
IndexName model.CIStr

SplitOpt *SplitOption
}

type SplitOption struct {
Min []ExprNode
Max []ExprNode
Copy link
Member

Choose a reason for hiding this comment

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

I think lower and upper are more suitable to describe ranges

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

Num int64
ValueLists [][]ExprNode
}

func (n *SplitIndexRegionStmt) Restore(ctx *RestoreCtx) error {
func (n *SplitRegionStmt) Restore(ctx *RestoreCtx) error {
ctx.WriteKeyWord("SPLIT TABLE ")
if err := n.Table.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore SplitIndexRegionStmt.Table")
}
ctx.WriteKeyWord(" INDEX ")
ctx.WriteName(n.IndexName)
ctx.WriteKeyWord(" BY ")
for i, row := range n.ValueLists {
if i != 0 {
ctx.WritePlain(",")
}
ctx.WritePlain("(")
for j, v := range row {
if j != 0 {
ctx.WritePlain(",")
}
if err := v.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore SplitIndexRegionStmt.ValueLists[%d][%d]", i, j)
}
}
ctx.WritePlain(")")
if len(n.IndexName.L) > 0 {
ctx.WriteKeyWord(" INDEX ")
ctx.WriteName(n.IndexName.String())
}
return nil
ctx.WritePlain(" ")
err := n.SplitOpt.Restore(ctx)
return err
}

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

n = newNode.(*SplitIndexRegionStmt)
n = newNode.(*SplitRegionStmt)
node, ok := n.Table.Accept(v)
if !ok {
return n, false
}
n.Table = node.(*TableName)
for i, list := range n.ValueLists {
for i, val := range n.SplitOpt.Min {
node, ok := val.Accept(v)
if !ok {
return n, false
}
n.SplitOpt.Min[i] = node.(ExprNode)
}
for i, val := range n.SplitOpt.Max {
node, ok := val.Accept(v)
if !ok {
return n, false
}
n.SplitOpt.Max[i] = node.(ExprNode)
}

for i, list := range n.SplitOpt.ValueLists {
for j, val := range list {
node, ok := val.Accept(v)
if !ok {
return n, false
}
n.ValueLists[i][j] = node.(ExprNode)
n.SplitOpt.ValueLists[i][j] = node.(ExprNode)
}
}
return v.Leave(n)
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
}

func (n *SplitOption) Restore(ctx *RestoreCtx) error {
if len(n.ValueLists) == 0 {
ctx.WriteKeyWord("BETWEEN ")
ctx.WritePlain("(")
for j, v := range n.Min {
if j != 0 {
ctx.WritePlain(",")
}
if err := v.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore SplitOption Min")
}
}
ctx.WritePlain(")")

ctx.WriteKeyWord(" AND ")
ctx.WritePlain("(")
for j, v := range n.Max {
if j != 0 {
ctx.WritePlain(",")
}
if err := v.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore SplitOption Max")
}
}
ctx.WritePlain(")")
ctx.WriteKeyWord(" REGIONS")
ctx.WritePlainf(" %d", n.Num)
return nil
}
ctx.WriteKeyWord("BY ")
for i, row := range n.ValueLists {
if i != 0 {
ctx.WritePlain(",")
}
ctx.WritePlain("(")
for j, v := range row {
if j != 0 {
ctx.WritePlain(",")
}
if err := v.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore SplitOption.ValueLists[%d][%d]", i, j)
}
}
ctx.WritePlain(")")
}
return nil
}
1 change: 1 addition & 0 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ var tokenMap = map[string]int{
"REDUNDANT": redundant,
"REFERENCES": references,
"REGEXP": regexpKwd,
"REGIONS": regions,
"RELOAD": reload,
"RENAME": rename,
"REPEAT": repeat,
Expand Down
Loading