Skip to content

Commit

Permalink
plan: add the exact match in building like range
Browse files Browse the repository at this point in the history
  • Loading branch information
zimulala committed Jul 1, 2016
1 parent a6ed1e7 commit 4b3d15a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 10 additions & 2 deletions plan/new_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,17 +429,25 @@ func (s *testPlanSuite) TestNewRangeBuilder(c *C) {
exprStr: "a LIKE 'abc_'",
resultStr: "[(abc abd)]",
},
{
exprStr: "a LIKE 'abc'",
resultStr: "[[abc abc]]",
},
{
exprStr: `a LIKE "ab\_c"`,
resultStr: "[[ab_c ab_c]]",
},
{
exprStr: "a LIKE '%'",
resultStr: "[[-inf +inf]]",
},
{
exprStr: `a LIKE '\%a'`,
resultStr: `[[%a %b)]`,
resultStr: `[[%a %a]]`,
},
{
exprStr: `a LIKE "\\"`,
resultStr: `[[\ ])]`,
resultStr: `[[\ \]]`,
},
{
exprStr: `a LIKE "\\\\a%"`,
Expand Down
7 changes: 7 additions & 0 deletions plan/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ func (r *rangeBuilder) newBuildFromPatternLike(expr *expression.ScalarFunction)
lowValue := make([]byte, 0, len(pattern))
escape := byte(expr.Args[2].(*expression.Constant).Value.GetInt64())
var exclude bool
isExactMatch := true
for i := 0; i < len(pattern); i++ {
if pattern[i] == escape {
i++
Expand All @@ -322,19 +323,25 @@ func (r *rangeBuilder) newBuildFromPatternLike(expr *expression.ScalarFunction)
}
if pattern[i] == '%' {
// Get the prefix.
isExactMatch = false
break
} else if pattern[i] == '_' {
// Get the prefix, but exclude the prefix.
// e.g., "abc_x", the start point exclude "abc",
// because the string length is more than 3.
exclude = true
isExactMatch = false
break
}
lowValue = append(lowValue, pattern[i])
}
if len(lowValue) == 0 {
return []rangePoint{{value: types.MinNotNullDatum(), start: true}, {value: types.MaxValueDatum()}}
}
if isExactMatch {
val := types.NewStringDatum(string(lowValue))
return []rangePoint{{value: val, start: true}, {value: val}}
}
startPoint := rangePoint{start: true, excl: exclude}
startPoint.value.SetBytesAsString(lowValue)
highValue := make([]byte, len(lowValue))
Expand Down

0 comments on commit 4b3d15a

Please sign in to comment.