diff --git a/parser/ast/expressions.go b/parser/ast/expressions.go index 67b7383e57c04..b1bda01a5e88e 100755 --- a/parser/ast/expressions.go +++ b/parser/ast/expressions.go @@ -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 ") diff --git a/parser/ast/format_test.go b/parser/ast/format_test.go index 43d7b2703477b..aca579740fdf8 100644 --- a/parser/ast/format_test.go +++ b/parser/ast/format_test.go @@ -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"`}, }