Skip to content

Commit

Permalink
[parser] parser: fix alter_stmt order by syntax (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
zuoRambo authored and ti-chi-bot committed Oct 9, 2021
1 parent 05b744d commit 1c514a6
Show file tree
Hide file tree
Showing 5 changed files with 5,660 additions and 5,548 deletions.
2 changes: 1 addition & 1 deletion parser/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bin/goyacc: goyacc/main.go

fmt:
@echo "gofmt (simplify)"
@ gofmt -s -l -w . 2>&1 | awk '{print} END{if(NR>0) {exit 1}}'
@gofmt -s -l -w . 2>&1 | awk '{print} END{if(NR>0) {exit 1}}'

clean:
go clean -i ./...
Expand Down
30 changes: 30 additions & 0 deletions parser/ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1920,6 +1920,7 @@ const (
AlterTableDiscardTablespace
AlterTableIndexInvisible
// TODO: Add more actions
AlterTableOrderByColumns
)

// LockType is the type for AlterTableSpec.
Expand Down Expand Up @@ -1996,6 +1997,7 @@ type AlterTableSpec struct {
Name string
Constraint *Constraint
Options []*TableOption
OrderByList []*AlterOrderItem
NewTable *TableName
NewColumns []*ColumnDef
OldColumnName *ColumnName
Expand All @@ -2014,6 +2016,24 @@ type AlterTableSpec struct {
Visibility IndexVisibility
}

// AlterOrderItem represents an item in order by at alter table stmt.
type AlterOrderItem struct {
node
Column *ColumnName
Desc bool
}

// Restore implements Node interface.
func (n *AlterOrderItem) Restore(ctx *RestoreCtx) error {
if err := n.Column.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore AlterOrderItem.Column")
}
if n.Desc {
ctx.WriteKeyWord(" DESC")
}
return nil
}

// Restore implements Node interface.
func (n *AlterTableSpec) Restore(ctx *RestoreCtx) error {
switch n.Tp {
Expand Down Expand Up @@ -2163,6 +2183,16 @@ func (n *AlterTableSpec) Restore(ctx *RestoreCtx) error {
ctx.WriteKeyWord("LOCK ")
ctx.WritePlain("= ")
ctx.WriteKeyWord(n.LockType.String())
case AlterTableOrderByColumns:
ctx.WriteKeyWord("ORDER BY ")
for i, alterOrderItem := range n.OrderByList {
if i != 0 {
ctx.WritePlain(",")
}
if err := alterOrderItem.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore AlterTableSpec.OrderByList[%d]", i)
}
}
case AlterTableAlgorithm:
ctx.WriteKeyWord("ALGORITHM ")
ctx.WritePlain("= ")
Expand Down
Loading

0 comments on commit 1c514a6

Please sign in to comment.