Skip to content

Commit

Permalink
修复 select xxx from tb as t force index(u_key) where t.id=x 语法错误
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyangxixian committed Apr 2, 2022
1 parent 0f77d26 commit 334745d
Showing 1 changed file with 47 additions and 13 deletions.
60 changes: 47 additions & 13 deletions parser/ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,15 @@ type TableName struct {
}

// Restore implements Node interface.
func (n *TableName) Restore(ctx *format.RestoreCtx) error {
func (n *TableName) restoreName(ctx *format.RestoreCtx) {
if n.Schema.String() != "" {
ctx.WriteName(n.Schema.String())
ctx.WritePlain(".")
}
ctx.WriteName(n.Name.String())
}

func (n *TableName) restorePartitions(ctx *format.RestoreCtx) {
if len(n.PartitionNames) > 0 {
ctx.WriteKeyWord(" PARTITION")
ctx.WritePlain("(")
Expand All @@ -206,6 +209,9 @@ func (n *TableName) Restore(ctx *format.RestoreCtx) error {
}
ctx.WritePlain(")")
}
}

func (n *TableName) restoreIndexHints(ctx *format.RestoreCtx) error {
for _, value := range n.IndexHints {
ctx.WritePlain(" ")
if err := value.Restore(ctx); err != nil {
Expand All @@ -216,6 +222,12 @@ func (n *TableName) Restore(ctx *format.RestoreCtx) error {
return nil
}

func (n *TableName) Restore(ctx *format.RestoreCtx) error {
n.restoreName(ctx)
n.restorePartitions(ctx)
return n.restoreIndexHints(ctx)
}

// IndexHintType is the type for index hint use, ignore or force.
type IndexHintType int

Expand Down Expand Up @@ -383,18 +395,40 @@ func (n *TableSource) Restore(ctx *format.RestoreCtx) error {
case *SelectStmt, *UnionStmt:
needParen = true
}
if needParen {
ctx.WritePlain("(")
}
if err := n.Source.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore TableSource.Source")
}
if needParen {
ctx.WritePlain(")")
}
if asName := n.AsName.String(); asName != "" {
ctx.WriteKeyWord(" AS ")
ctx.WriteName(asName)

if tn, tnCase := n.Source.(*TableName); tnCase {
if needParen {
ctx.WritePlain("(")
}

tn.restoreName(ctx)
tn.restorePartitions(ctx)

if asName := n.AsName.String(); asName != "" {
ctx.WriteKeyWord(" AS ")
ctx.WriteName(asName)
}
if err := tn.restoreIndexHints(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore TableSource.Source.(*TableName).IndexHints")
}

if needParen {
ctx.WritePlain(")")
}
} else {
if needParen {
ctx.WritePlain("(")
}
if err := n.Source.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore TableSource.Source")
}
if needParen {
ctx.WritePlain(")")
}
if asName := n.AsName.String(); asName != "" {
ctx.WriteKeyWord(" AS ")
ctx.WriteName(asName)
}
}

return nil
Expand Down

0 comments on commit 334745d

Please sign in to comment.