Skip to content

Commit

Permalink
expression: fix behavior for builtin 'LTrim', 'RTrim' and 'Trim' (#7291)
Browse files Browse the repository at this point in the history
  • Loading branch information
spongedu authored and coocood committed Aug 7, 2018
1 parent ae2e3ac commit 6e33d2e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion expression/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ func (b *builtinUnHexSig) evalString(row chunk.Row) (string, bool, error) {
return string(bs), false, nil
}

const spaceChars = "\n\t\r "
const spaceChars = " "

type trimFunctionClass struct {
baseFunctionClass
Expand Down
17 changes: 17 additions & 0 deletions expression/builtin_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,9 @@ func (s *testEvaluatorSuite) TestTrim(c *C) {
res string
}{
{[]interface{}{" bar "}, false, false, "bar"},
{[]interface{}{"\t bar \n"}, false, false, "\t bar \n"},
{[]interface{}{"\r bar \t"}, false, false, "\r bar \t"},
{[]interface{}{" \tbar\n "}, false, false, "\tbar\n"},
{[]interface{}{""}, false, false, ""},
{[]interface{}{nil}, true, false, ""},
{[]interface{}{"xxxbarxxx", "x"}, false, false, "bar"},
Expand Down Expand Up @@ -919,6 +922,14 @@ func (s *testEvaluatorSuite) TestLTrim(c *C) {
res string
}{
{" bar ", false, false, "bar "},
{"\t bar ", false, false, "\t bar "},
{" \tbar ", false, false, "\tbar "},
{"\t bar ", false, false, "\t bar "},
{" \tbar ", false, false, "\tbar "},
{"\r bar ", false, false, "\r bar "},
{" \rbar ", false, false, "\rbar "},
{"\n bar ", false, false, "\n bar "},
{" \nbar ", false, false, "\nbar "},
{"bar", false, false, "bar"},
{"", false, false, ""},
{nil, true, false, ""},
Expand Down Expand Up @@ -954,6 +965,12 @@ func (s *testEvaluatorSuite) TestRTrim(c *C) {
}{
{" bar ", false, false, " bar"},
{"bar", false, false, "bar"},
{"bar \n", false, false, "bar \n"},
{"bar\n ", false, false, "bar\n"},
{"bar \r", false, false, "bar \r"},
{"bar\r ", false, false, "bar\r"},
{"bar \t", false, false, "bar \t"},
{"bar\t ", false, false, "bar\t"},
{"", false, false, ""},
{nil, true, false, ""},
{errors.New("must error"), false, true, ""},
Expand Down
6 changes: 6 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,10 @@ func (s *testIntegrationSuite) TestStringBuiltin(c *C) {
result.Check(testutil.RowsWithSep(",", "bar ,bar,,<nil>"))
result = tk.MustQuery(`select rtrim(' bar '), rtrim('bar'), rtrim(''), rtrim(null)`)
result.Check(testutil.RowsWithSep(",", " bar,bar,,<nil>"))
result = tk.MustQuery(`select ltrim("\t bar "), ltrim(" \tbar"), ltrim("\n bar"), ltrim("\r bar")`)
result.Check(testutil.RowsWithSep(",", "\t bar ,\tbar,\n bar,\r bar"))
result = tk.MustQuery(`select rtrim(" bar \t"), rtrim("bar\t "), rtrim("bar \n"), rtrim("bar \r")`)
result.Check(testutil.RowsWithSep(",", " bar \t,bar\t,bar \n,bar \r"))

// for reverse
tk.MustExec(`DROP TABLE IF EXISTS t;`)
Expand All @@ -776,6 +780,8 @@ func (s *testIntegrationSuite) TestStringBuiltin(c *C) {
// for trim
result = tk.MustQuery(`select trim(' bar '), trim(leading 'x' from 'xxxbarxxx'), trim(trailing 'xyz' from 'barxxyz'), trim(both 'x' from 'xxxbarxxx')`)
result.Check(testkit.Rows("bar barxxx barx bar"))
result = tk.MustQuery(`select trim('\t bar\n '), trim(' \rbar \t')`)
result.Check(testutil.RowsWithSep(",", "\t bar\n,\rbar \t"))
result = tk.MustQuery(`select trim(leading from ' bar'), trim('x' from 'xxxbarxxx'), trim('x' from 'bar'), trim('' from ' bar ')`)
result.Check(testutil.RowsWithSep(",", "bar,bar,bar, bar "))
result = tk.MustQuery(`select trim(''), trim('x' from '')`)
Expand Down

0 comments on commit 6e33d2e

Please sign in to comment.