Giter VIP home page Giter VIP logo

Comments (4)

ianzapolsky avatar ianzapolsky commented on July 20, 2024

You can see this bug arises from the fact that a TableSource calls n.Source.Restore(), then writes the AsName if it exists.

// Restore implements Node interface.
func (n *TableSource) Restore(ctx *RestoreCtx) error {
    needParen := false
    switch n.Source.(type) {
    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)
    }

    return nil
}

TableName, which is the underlying type of Source in my case, is responsible for printing Index Hints:

// Restore implements Node interface.
func (n *TableName) Restore(ctx *RestoreCtx) error {
    if n.Schema.String() != "" {
        ctx.WriteName(n.Schema.String())
        ctx.WritePlain(".")
    }
    ctx.WriteName(n.Name.String())
    if len(n.PartitionNames) > 0 {
        ctx.WriteKeyWord(" PARTITION")
        ctx.WritePlain("(")
        for i, v := range n.PartitionNames {
            if i != 0 {
                ctx.WritePlain(", ")
            }
            ctx.WriteName(v.String())
        }
        ctx.WritePlain(")")
    }
    for _, value := range n.IndexHints {
        ctx.WritePlain(" ")
        if err := value.Restore(ctx); err != nil {
            return errors.Annotate(err, "An error occurred while splicing IndexHints")
        }
    }

    return nil
}

from parser.

ianzapolsky avatar ianzapolsky commented on July 20, 2024

A naive fix could be:

diff --git a/vendor/github.com/pingcap/parser/ast/dml.go b/vendor/github.com/pingcap/parser/ast/dml.go
index 483018f..95cf7d2 100644
--- a/vendor/github.com/pingcap/parser/ast/dml.go
+++ b/vendor/github.com/pingcap/parser/ast/dml.go
@@ -205,13 +205,6 @@ func (n *TableName) Restore(ctx *RestoreCtx) error {
                }
                ctx.WritePlain(")")
        }
-       for _, value := range n.IndexHints {
-               ctx.WritePlain(" ")
-               if err := value.Restore(ctx); err != nil {
-                       return errors.Annotate(err, "An error occurred while splicing IndexHints")
-               }
-       }
-
        return nil
 }

@@ -395,6 +388,14 @@ func (n *TableSource) Restore(ctx *RestoreCtx) error {
                ctx.WriteKeyWord(" AS ")
                ctx.WriteName(asName)
        }
+       if tn, ok := n.Source.(*TableName); ok {
+               for _, value := range tn.IndexHints {
+                       ctx.WritePlain(" ")
+                       if err := value.Restore(ctx); err != nil {
+                               return errors.Annotate(err, "An error occurred while splicing IndexHints")
+                       }
+               }
+       }

        return nil
 }

from parser.

zz-jason avatar zz-jason commented on July 20, 2024

@eurekaka @foreyes PTAL

from parser.

foreyes avatar foreyes commented on July 20, 2024

The same issue as #538, already fixed in #539.

from parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.