Skip to content

Commit

Permalink
[parser] ast: fix case expression format bug (pingcap#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored and xhebox committed Oct 8, 2021
1 parent 9b524d4 commit f2b8239
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
5 changes: 3 additions & 2 deletions parser/ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,14 @@ func (n *CaseExpr) Restore(ctx *RestoreCtx) error {

// Format the ExprNode into a Writer.
func (n *CaseExpr) Format(w io.Writer) {
fmt.Fprint(w, "CASE ")
fmt.Fprint(w, "CASE")
// Because the presence of `case when` syntax, `Value` could be nil and we need check this.
if n.Value != nil {
n.Value.Format(w)
fmt.Fprint(w, " ")
n.Value.Format(w)
}
for _, clause := range n.WhenClauses {
fmt.Fprint(w, " ")
fmt.Fprint(w, "WHEN ")
clause.Expr.Format(w)
fmt.Fprint(w, " THEN ")
Expand Down
1 change: 1 addition & 0 deletions parser/ast/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (ts *testAstFormatSuite) TestAstFormat(c *C) {
{` cast( a as decimal ) `, "CAST(`a` AS DECIMAL(11))"},
{` cast( a as decimal (3) ) `, "CAST(`a` AS DECIMAL(3))"},
{` cast( a as decimal (3,3) ) `, "CAST(`a` AS DECIMAL(3, 3))"},
{` ((case when (c0 = 0) then 0 when (c0 > 0) then (c1 / c0) end)) `, "((CASE WHEN (`c0` = 0) THEN 0 WHEN (`c0` > 0) THEN (`c1` / `c0`) END))"},
{` convert (a, signed) `, "CONVERT(`a`, SIGNED)"},
{` binary "hello"`, `BINARY "hello"`},
}
Expand Down

0 comments on commit f2b8239

Please sign in to comment.