Skip to content

Commit

Permalink
parser: fix test_driver with RestoreStringWithoutCharset and RestoreS…
Browse files Browse the repository at this point in the history
…tringWithoutDefaultCharset flag (#37248)

close #37175
  • Loading branch information
Defined2014 authored Aug 22, 2022
1 parent e0da196 commit 3977a0a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
49 changes: 49 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6563,6 +6563,55 @@ func TestHelp(t *testing.T) {
RunTest(t, table, false)
}

func TestWithoutCharsetFlags(t *testing.T) {
type testCaseWithFlag struct {
src string
ok bool
restore string
flag RestoreFlags
}

flag := RestoreStringSingleQuotes | RestoreSpacesAroundBinaryOperation | RestoreBracketAroundBinaryOperation | RestoreNameBackQuotes
cases := []testCaseWithFlag{
{"select 'a'", true, "SELECT 'a'", flag | RestoreStringWithoutCharset},
{"select _utf8'a'", true, "SELECT 'a'", flag | RestoreStringWithoutCharset},
{"select _utf8mb4'a'", true, "SELECT 'a'", flag | RestoreStringWithoutCharset},
{"select _utf8 X'D0B1'", true, "SELECT x'd0b1'", flag | RestoreStringWithoutCharset},

{"select _utf8mb4'a'", true, "SELECT 'a'", flag | RestoreStringWithoutDefaultCharset},
{"select _utf8'a'", true, "SELECT _utf8'a'", flag | RestoreStringWithoutDefaultCharset},
{"select _utf8'a'", true, "SELECT _utf8'a'", flag | RestoreStringWithoutDefaultCharset},
{"select _utf8 X'D0B1'", true, "SELECT _utf8 x'd0b1'", flag | RestoreStringWithoutDefaultCharset},
}

p := parser.New()
p.EnableWindowFunc(false)
for _, tbl := range cases {
stmts, _, err := p.Parse(tbl.src, "", "")
if !tbl.ok {
require.Error(t, err)
continue
}
require.NoError(t, err)
// restore correctness test
var sb strings.Builder
restoreSQLs := ""
for _, stmt := range stmts {
sb.Reset()
ctx := NewRestoreCtx(tbl.flag, &sb)
ctx.DefaultDB = "test"
err = stmt.Restore(ctx)
require.NoError(t, err)
restoreSQL := sb.String()
if restoreSQLs != "" {
restoreSQLs += "; "
}
restoreSQLs += restoreSQL
}
require.Equal(t, tbl.restore, restoreSQLs)
}
}

func TestRestoreBinOpWithBrackets(t *testing.T) {
cases := []testCase{
{"select mod(a+b, 4)+1", true, "SELECT (((`a` + `b`) % 4) + 1)"},
Expand Down
5 changes: 4 additions & 1 deletion parser/test_driver/test_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ func (n *ValueExpr) Restore(ctx *format.RestoreCtx) error {
case KindFloat64:
ctx.WritePlain(strconv.FormatFloat(n.GetFloat64(), 'e', -1, 64))
case KindString:
if n.Type.GetCharset() != "" {
if n.Type.GetCharset() != "" &&
!ctx.Flags.HasStringWithoutCharset() &&
(!ctx.Flags.HasStringWithoutDefaultCharset() || n.Type.GetCharset() != mysql.DefaultCharset) {
ctx.WritePlain("_")
ctx.WriteKeyWord(n.Type.GetCharset())
}
Expand All @@ -90,6 +92,7 @@ func (n *ValueExpr) Restore(ctx *format.RestoreCtx) error {
ctx.WritePlain(n.GetMysqlDecimal().String())
case KindBinaryLiteral:
if n.Type.GetCharset() != "" && n.Type.GetCharset() != mysql.DefaultCharset &&
!ctx.Flags.HasStringWithoutCharset() &&
n.Type.GetCharset() != charset.CharsetBin {
ctx.WritePlain("_")
ctx.WriteKeyWord(n.Type.GetCharset() + " ")
Expand Down

0 comments on commit 3977a0a

Please sign in to comment.