Skip to content

Commit

Permalink
expression: change like function's behavior. (pingcap#4683)
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros authored and hanfei1991 committed Sep 29, 2017
1 parent 581f354 commit 56ed7f1
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (s *testSuite) TestShow(c *C) {
))

// For show like with escape
testSQL = `show tables like 'show\_test'`
testSQL = `show tables like 'SHOW\_test'`
result = tk.MustQuery(testSQL)
rows := result.Rows()
c.Check(rows, HasLen, 1)
Expand Down
1 change: 1 addition & 0 deletions expression/builtin_like.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type builtinLikeSig struct {

// evalInt evals a builtinLikeSig.
// See https://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html#operator_like
// NOTE: Currently tikv's like function is case sensitive, so we keep its behavior here.
func (b *builtinLikeSig) evalInt(row []types.Datum) (int64, bool, error) {
sc := b.ctx.GetSessionVars().StmtCtx
valStr, isNull, err := b.args[0].EvalString(row, sc)
Expand Down
6 changes: 3 additions & 3 deletions expression/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,9 @@ func (s *testEvaluatorSuite) TestLike(c *C) {
{"a", "", 0},
{"a", "a", 1},
{"a", "b", 0},
{"aA", "Aa", 1},
{"aAb", "Aa%", 1},
{"aAb", "Aa_", 1},
{"aA", "Aa", 0},
{"aAb", "Aa%", 0},
{"aAb", "aA_", 1},
}
for _, tt := range tests {
fc := funcs[ast.Like]
Expand Down
6 changes: 3 additions & 3 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2094,10 +2094,10 @@ func (s *testIntegrationSuite) TestBuiltin(c *C) {
likeTests := []testCase{
{"a", "a", 1},
{"a", "b", 0},
{"aA", "Aa", 1},
{"aA", "Aa", 0},
{"aA%", "aAab", 1},
{"aA_", "Aaab", 0},
{"aA_", "Aab", 1},
{"Aa_", "Aab", 1},
{"", "", 1},
{"", "a", 0},
}
Expand Down Expand Up @@ -2917,7 +2917,7 @@ func (s *testIntegrationSuite) TestSetVariables(c *C) {
_, err = tk.Exec("set @@session.sql_mode=',NO_ZERO_DATE,ANSI,ANSI_QUOTES';")
r = tk.MustQuery(`select @@session.sql_mode`)
r.Check(testkit.Rows("NO_ZERO_DATE,REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ONLY_FULL_GROUP_BY,ANSI"))
r = tk.MustQuery(`show variables like 'SQL_MODE'`)
r = tk.MustQuery(`show variables like 'sql_mode'`)
r.Check(testkit.Rows("sql_mode NO_ZERO_DATE,REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ONLY_FULL_GROUP_BY,ANSI"))

// for invalid SQL mode.
Expand Down
19 changes: 12 additions & 7 deletions util/stringutil/string_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,19 @@ func CompilePattern(pattern string, escape byte) (patChars, patTypes []byte) {

const caseDiff = 'a' - 'A'

// NOTE: Currently tikv's like function is case sensitive, so we keep its behavior here.
func matchByteCI(a, b byte) bool {
if a == b {
return true
}
if a >= 'a' && a <= 'z' && a-caseDiff == b {
return true
}
return a >= 'A' && a <= 'Z' && a+caseDiff == b
return a == b
// We may reuse below code block when like function go back to case insensitive.
/*
if a == b {
return true
}
if a >= 'a' && a <= 'z' && a-caseDiff == b {
return true
}
return a >= 'A' && a <= 'Z' && a+caseDiff == b
*/
}

// DoMatch matches the string with patChars and patTypes.
Expand Down
11 changes: 7 additions & 4 deletions util/stringutil/string_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,10 @@ func (s *testStringUtilSuite) TestPatternMatch(c *C) {
{"_", "a", '\\', true},
{"_", "ab", '\\', false},
{"__", "b", '\\', false},
{"_ab", "AAB", '\\', true},
{"%", "abcd", '\\', true},
{"%", "", '\\', true},
{"%a", "AAA", '\\', true},
{"%b", "AAA", '\\', false},
{"b%", "BBB", '\\', true},
{"%a%", "BBB", '\\', false},
{"%a%", "BAB", '\\', true},
{"a%", "BBB", '\\', false},
{`\%a`, `%a`, '\\', true},
{`\%a`, `aa`, '\\', false},
Expand All @@ -114,6 +110,13 @@ func (s *testStringUtilSuite) TestPatternMatch(c *C) {
{`\%a`, `%a`, '+', false},
{`++a`, `+a`, '+', true},
{`++_a`, `+xa`, '+', true},
// We may reopen these test when like function go back to case insensitive.
/*
{"_ab", "AAB", '\\', true},
{"%a%", "BAB", '\\', true},
{"%a", "AAA", '\\', true},
{"b%", "BBB", '\\', true},
*/
}
for _, v := range tbl {
patChars, patTypes := CompilePattern(v.pattern, v.escape)
Expand Down

0 comments on commit 56ed7f1

Please sign in to comment.