Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support like escape #360

Merged
merged 6 commits into from
Oct 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions expression/like.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type PatternLike struct {
patTypes []byte
// Not is true, the expression is "not like".
Not bool
// Escape is the special escaped character, default is \.
Escape byte
}

// Clone implements the Expression Clone interface.
Expand All @@ -57,6 +59,7 @@ func (p *PatternLike) Clone() Expression {
patChars: p.patChars,
patTypes: p.patTypes,
Not: p.Not,
Escape: p.Escape,
}
}

Expand All @@ -67,6 +70,9 @@ func (p *PatternLike) IsStatic() bool {

// String implements the Expression String interface.
func (p *PatternLike) String() string {
if p.Escape != '\\' {
return fmt.Sprintf("%s LIKE %s ESCAPE '%c'", p.Expr, p.Pattern, p.Escape)
}
return fmt.Sprintf("%s LIKE %s", p.Expr, p.Pattern)
}

Expand Down Expand Up @@ -104,7 +110,7 @@ func (p *PatternLike) Eval(ctx context.Context, args map[interface{}]interface{}
default:
return nil, errors.Errorf("Pattern should be string or []byte in LIKE: %v (Value of type %T)", pattern, pattern)
}
p.patChars, p.patTypes = compilePattern(spattern)
p.patChars, p.patTypes = compilePattern(spattern, p.Escape)
}

match := doMatch(sexpr, p.patChars, p.patTypes)
Expand All @@ -120,7 +126,7 @@ func (p *PatternLike) Accept(v Visitor) (Expression, error) {
}

// handle escapes and wild cards convert pattern characters and pattern types,
func compilePattern(pattern string) (patChars, patTypes []byte) {
func compilePattern(pattern string, escape byte) (patChars, patTypes []byte) {
var lastAny bool
patChars = make([]byte, len(pattern))
patTypes = make([]byte, len(pattern))
Expand All @@ -129,18 +135,23 @@ func compilePattern(pattern string) (patChars, patTypes []byte) {
var tp byte
var c = pattern[i]
switch c {
case '\\':
case escape:
lastAny = false
tp = patMatch
if i < len(pattern)-1 {
i++
c = pattern[i]
if c == '\\' || c == '_' || c == '%' {
if c == escape || c == '_' || c == '%' {
// valid escape.
} else {
// invalid escape, fall back to literal back slash
// invalid escape, fall back to escape byte
// mysql will treat escape character as the origin value even
// the escape sequence is invalid in Go or C.
// e.g, \m is invalid in Go, but in MySQL we will get "m" for select '\m'.
// Following case is correct just for escape \, not for others like +.
// TODO: add more checks for other escapes.
i--
c = '\\'
c = escape
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about i==len(pattern)-1 ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just use the old code here, which just seems using the origin escape character directly.
I have added a comment that we will add more tests later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the escape is the last character in the pattern, the match type would be exact match.

case '_':
Expand Down
58 changes: 34 additions & 24 deletions expression/like_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,40 @@ func (*testLikeSuite) TestLike(c *C) {
tbl := []struct {
pattern string
input string
escape byte
match bool
}{
{"", "a", false},
{"a", "a", true},
{"a", "b", false},
{"aA", "aA", true},
{"_", "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},
{`\_a`, `_a`, true},
{`\_a`, `aa`, false},
{`\\_a`, `\xa`, true},
{`\a\b`, `\a\b`, true},
{"%%_", `abc`, true},
{"", "a", '\\', false},
{"a", "a", '\\', true},
{"a", "b", '\\', false},
{"aA", "aA", '\\', true},
{"_", "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},
{`\_a`, `_a`, '\\', true},
{`\_a`, `aa`, '\\', false},
{`\\_a`, `\xa`, '\\', true},
{`\a\b`, `\a\b`, '\\', true},
{"%%_", `abc`, '\\', true},
{`+_a`, `_a`, '+', true},
{`+%a`, `%a`, '+', true},
{`\%a`, `%a`, '+', false},
{`++a`, `+a`, '+', true},
{`++_a`, `+xa`, '+', true},
}
for _, v := range tbl {
patChars, patTypes := compilePattern(v.pattern)
patChars, patTypes := compilePattern(v.pattern, v.escape)
match := doMatch(v.input, patChars, patTypes)
c.Assert(match, Equals, v.match, Commentf("%v", v))
}
Expand All @@ -68,6 +74,8 @@ func (*testLikeSuite) TestEval(c *C) {
Pattern: &Value{
Val: "aA",
},

Escape: '\\',
}
cloned := pattern.Clone()
pattern = cloned.(*PatternLike)
Expand Down Expand Up @@ -107,13 +115,15 @@ func (*testLikeSuite) TestEval(c *C) {
pattern = &PatternLike{
Expr: mockExpr{isStatic: true, val: "slien"},
Pattern: mockExpr{isStatic: true, val: []byte("%E%")},
Escape: '\\',
}
v, err := pattern.Eval(nil, nil)
c.Assert(err, IsNil)
c.Assert(v, IsTrue)
pattern = &PatternLike{
Expr: mockExpr{isStatic: true, val: "slin"},
Pattern: mockExpr{isStatic: true, val: []byte("%E%")},
Escape: '\\',
}
v, err = pattern.Eval(nil, nil)
c.Assert(err, IsNil)
Expand Down
31 changes: 28 additions & 3 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ import (
engines "ENGINES"
enum "ENUM"
eq "="
escape "ESCAPE"
execute "EXECUTE"
exists "EXISTS"
explain "EXPLAIN"
Expand Down Expand Up @@ -428,6 +429,7 @@ import (
JoinTable "join table"
JoinType "join type"
KeyOrIndex "{KEY|INDEX}"
LikeEscapeOpt "like escape option"
LimitClause "LIMIT clause"
Literal "literal value"
logAnd "logical and operator"
Expand Down Expand Up @@ -590,6 +592,8 @@ import (
%precedence '('
%precedence lowerThanQuick
%precedence quick
%precedence lowerThanEscape
%precedence escape
%precedence lowerThanComma
%precedence ','

Expand Down Expand Up @@ -1510,9 +1514,20 @@ Factor1:
return 1
}
}
| PrimaryFactor NotOpt "LIKE" PrimaryExpression
| PrimaryFactor NotOpt "LIKE" PrimaryExpression LikeEscapeOpt
{
$$ = &expression.PatternLike{Expr: $1.(expression.Expression), Pattern: $4.(expression.Expression), Not: $2.(bool)}
escape := $5.(string)
if len(escape) > 1 {
yylex.(*lexer).errf("Incorrect arguments %s to ESCAPE", escape)
return 1
} else if len(escape) == 0 {
escape = "\\"
}
$$ = &expression.PatternLike{
Expr: $1.(expression.Expression),
Pattern: $4.(expression.Expression),
Not: $2.(bool),
Escape: escape[0]}
}
| PrimaryFactor NotOpt RegexpSym PrimaryExpression
{
Expand All @@ -1524,6 +1539,16 @@ RegexpSym:
"REGEXP"
| "RLIKE"

LikeEscapeOpt:
%prec lowerThanEscape
{
$$ = "\\"
}
| "ESCAPE" stringLit
{
$$ = $2
}

NotOpt:
{
$$ = false
Expand Down Expand Up @@ -1666,7 +1691,7 @@ UnReservedKeyword:
| "START" | "GLOBAL" | "TABLES"| "TEXT" | "TIME" | "TIMESTAMP" | "TRANSACTION" | "TRUNCATE" | "UNKNOWN"
| "VALUE" | "WARNINGS" | "YEAR" | "MODE" | "WEEK" | "ANY" | "SOME" | "USER" | "IDENTIFIED" | "COLLATION"
| "COMMENT" | "AVG_ROW_LENGTH" | "CONNECTION" | "CHECKSUM" | "COMPRESSION" | "KEY_BLOCK_SIZE" | "MAX_ROWS" | "MIN_ROWS"
| "NATIONAL" | "ROW" | "QUARTER"
| "NATIONAL" | "ROW" | "QUARTER" | "ESCAPE"

NotKeywordToken:
"ABS" | "COALESCE" | "CONCAT" | "CONCAT_WS" | "COUNT" | "DAY" | "DAYOFMONTH" | "DAYOFWEEK" | "DAYOFYEAR" | "FOUND_ROWS" | "GROUP_CONCAT"
Expand Down
14 changes: 13 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *testParserSuite) TestSimple(c *C) {
"start", "global", "tables", "text", "time", "timestamp", "transaction", "truncate", "unknown",
"value", "warnings", "year", "now", "substring", "mode", "any", "some", "user", "identified",
"collation", "comment", "avg_row_length", "checksum", "compression", "connection", "key_block_size",
"max_rows", "min_rows", "national", "row", "quarter",
"max_rows", "min_rows", "national", "row", "quarter", "escape",
}
for _, kw := range unreservedKws {
src := fmt.Sprintf("SELECT %s FROM tbl;", kw)
Expand Down Expand Up @@ -578,3 +578,15 @@ func (s *testParserSuite) TestUnion(c *C) {
}
s.RunTest(c, table)
}

func (s *testParserSuite) TestLikeEscape(c *C) {
table := []testCase{
// For like escape
{`select "abc_" like "abc\\_" escape ''`, true},
{`select "abc_" like "abc\\_" escape '\\'`, true},
{`select "abc_" like "abc\\_" escape '||'`, false},
{`select "abc" like "escape" escape '+'`, true},
}

s.RunTest(c, table)
}
3 changes: 3 additions & 0 deletions parser/scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ else {e}{l}{s}{e}
end {e}{n}{d}
engine {e}{n}{g}{i}{n}{e}
engines {e}{n}{g}{i}{n}{e}{s}
escape {e}{s}{c}{a}{p}{e}
execute {e}{x}{e}{c}{u}{t}{e}
exists {e}{x}{i}{s}{t}{s}
explain {e}{x}{p}{l}{a}{i}{n}
Expand Down Expand Up @@ -667,6 +668,8 @@ year_month {y}{e}{a}{r}_{m}{o}{n}{t}{h}
{execute} lval.item = string(l.val)
return execute
{enum} return enum
{escape} lval.item = string(l.val)
return escape
{exists} return exists
{explain} return explain
{extract} lval.item = string(l.val)
Expand Down