Skip to content

Commit

Permalink
expression: the quote function should treat null expr as NULL literal…
Browse files Browse the repository at this point in the history
… string instead of NULL (#11592) (#11619)
  • Loading branch information
sre-bot authored Aug 5, 2019
1 parent 25c8215 commit 4fb7f3f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
5 changes: 4 additions & 1 deletion expression/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2696,8 +2696,11 @@ func (b *builtinQuoteSig) Clone() builtinFunc {
// See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_quote
func (b *builtinQuoteSig) evalString(row chunk.Row) (string, bool, error) {
str, isNull, err := b.args[0].EvalString(b.ctx, row)
if isNull || err != nil {
if err != nil {
return "", true, err
} else if isNull {
// If the argument is NULL, the return value is the word "NULL" without enclosing single quotation marks. see ref.
return "NULL", false, err
}

runes := []rune(str)
Expand Down
2 changes: 1 addition & 1 deletion expression/builtin_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2168,7 +2168,7 @@ func (s *testEvaluatorSuite) TestQuote(c *C) {
{`萌萌哒(๑•ᴗ•๑)😊`, `'萌萌哒(๑•ᴗ•๑)😊'`},
{`㍿㌍㍑㌫`, `'㍿㌍㍑㌫'`},
{string([]byte{0, 26}), `'\0\Z'`},
{nil, nil},
{nil, "NULL"},
}

for _, t := range tbl {
Expand Down
9 changes: 8 additions & 1 deletion expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,14 @@ func (s *testIntegrationSuite) TestStringBuiltin(c *C) {
result = tk.MustQuery(`select quote("aaaa"), quote(""), quote("\"\""), quote("\n\n");`)
result.Check(testkit.Rows("'aaaa' '' '\"\"' '\n\n'"))
result = tk.MustQuery(`select quote(0121), quote(0000), quote("中文"), quote(NULL);`)
result.Check(testkit.Rows("'121' '0' '中文' <nil>"))
result.Check(testkit.Rows("'121' '0' '中文' NULL"))
tk.MustQuery(`select quote(null) is NULL;`).Check(testkit.Rows(`0`))
tk.MustQuery(`select quote(null) is NOT NULL;`).Check(testkit.Rows(`1`))
tk.MustQuery(`select length(quote(null));`).Check(testkit.Rows(`4`))
tk.MustQuery(`select quote(null) REGEXP binary 'null'`).Check(testkit.Rows(`0`))
tk.MustQuery(`select quote(null) REGEXP binary 'NULL'`).Check(testkit.Rows(`1`))
tk.MustQuery(`select quote(null) REGEXP 'NULL'`).Check(testkit.Rows(`1`))
tk.MustQuery(`select quote(null) REGEXP 'null'`).Check(testkit.Rows(`1`))

// for convert
result = tk.MustQuery(`select convert("123" using "binary"), convert("中文" using "binary"), convert("中文" using "utf8"), convert("中文" using "utf8mb4"), convert(cast("中文" as binary) using "utf8");`)
Expand Down

0 comments on commit 4fb7f3f

Please sign in to comment.