From a901d38b9ab4f70d6b198314b320974516e27ed3 Mon Sep 17 00:00:00 2001 From: gaoxingliang Date: Fri, 2 Aug 2019 16:38:08 +0800 Subject: [PATCH 1/3] expression: the quote func for null arg should map to "NULL" string --- expression/builtin_string.go | 7 ++++++- expression/integration_test.go | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/expression/builtin_string.go b/expression/builtin_string.go index b8882bbcf4080..bab0a31611e77 100644 --- a/expression/builtin_string.go +++ b/expression/builtin_string.go @@ -2696,10 +2696,15 @@ 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. + return "NULL", false, err } + runes := []rune(str) buffer := bytes.NewBufferString("") buffer.WriteRune('\'') diff --git a/expression/integration_test.go b/expression/integration_test.go index bf05d873abfc5..8ab9c28931527 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -4699,3 +4699,16 @@ func (s *testIntegrationSuite) TestIssue11309And11319(c *C) { tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_HOUR)`).Check(testkit.Rows("2007-03-31 00:08:28")) tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR_MONTH)`).Check(testkit.Rows("2009-05-28 22:08:28")) } + + +func (s *testIntegrationSuite) TestQuote(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + 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`)) +} From 1f8330c705f5ce73bdc2920bfebb5a2e2ba27ced Mon Sep 17 00:00:00 2001 From: gaoxingliang Date: Fri, 2 Aug 2019 16:47:02 +0800 Subject: [PATCH 2/3] expression: the quote func for null arg should map to "NULL" string (#11556) --- expression/builtin_string.go | 2 -- expression/builtin_string_test.go | 2 +- expression/integration_test.go | 22 ++++++++-------------- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/expression/builtin_string.go b/expression/builtin_string.go index bab0a31611e77..11eaefd8a9f71 100644 --- a/expression/builtin_string.go +++ b/expression/builtin_string.go @@ -2696,7 +2696,6 @@ 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 err != nil { return "", true, err } else if isNull { @@ -2704,7 +2703,6 @@ func (b *builtinQuoteSig) evalString(row chunk.Row) (string, bool, error) { return "NULL", false, err } - runes := []rune(str) buffer := bytes.NewBufferString("") buffer.WriteRune('\'') diff --git a/expression/builtin_string_test.go b/expression/builtin_string_test.go index b07ebd7d11f13..0272e2b7d8c20 100644 --- a/expression/builtin_string_test.go +++ b/expression/builtin_string_test.go @@ -2168,7 +2168,7 @@ func (s *testEvaluatorSuite) TestQuote(c *C) { {`萌萌哒(๑•ᴗ•๑)😊`, `'萌萌哒(๑•ᴗ•๑)😊'`}, {`㍿㌍㍑㌫`, `'㍿㌍㍑㌫'`}, {string([]byte{0, 26}), `'\0\Z'`}, - {nil, nil}, + {nil, "NULL"}, } for _, t := range tbl { diff --git a/expression/integration_test.go b/expression/integration_test.go index 8ab9c28931527..cbdb8d78445f6 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -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' '中文' ")) + 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");`) @@ -4699,16 +4706,3 @@ func (s *testIntegrationSuite) TestIssue11309And11319(c *C) { tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_HOUR)`).Check(testkit.Rows("2007-03-31 00:08:28")) tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR_MONTH)`).Check(testkit.Rows("2009-05-28 22:08:28")) } - - -func (s *testIntegrationSuite) TestQuote(c *C) { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - 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`)) -} From 3e458c82d67574652d0a2b73e3360b7b70093797 Mon Sep 17 00:00:00 2001 From: gaoxingliang Date: Fri, 2 Aug 2019 17:04:11 +0800 Subject: [PATCH 3/3] expression: the quote func for null arg should map to "NULL" string (#11556) --- expression/builtin_string.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/expression/builtin_string.go b/expression/builtin_string.go index 11eaefd8a9f71..5d942effe78d3 100644 --- a/expression/builtin_string.go +++ b/expression/builtin_string.go @@ -2699,7 +2699,7 @@ func (b *builtinQuoteSig) evalString(row chunk.Row) (string, bool, error) { 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. + // If the argument is NULL, the return value is the word "NULL" without enclosing single quotation marks. see ref. return "NULL", false, err }