Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(spanner/spannertest): Fix the "LIKE" clause handling for prefix a…
…nd suffix matches (#4655)

Co-authored-by: Knut Olav Løite <koloite@gmail.com>
  • Loading branch information
sryoya and olavloite committed Aug 23, 2021
1 parent cde8697 commit a2118f0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions spanner/spannertest/db_eval.go
Expand Up @@ -873,6 +873,12 @@ func evalLike(str, pat string) bool {

// Lean on regexp for simplicity.
pat = regexp.QuoteMeta(pat)
if !strings.HasPrefix(pat, "%") {
pat = "^" + pat
}
if !strings.HasSuffix(pat, "%") {
pat = pat + "$"
}
pat = strings.Replace(pat, "%", ".*", -1)
pat = strings.Replace(pat, "_", ".", -1)
match, err := regexp.MatchString(pat, str)
Expand Down
15 changes: 15 additions & 0 deletions spanner/spannertest/integration_test.go
Expand Up @@ -816,6 +816,21 @@ func TestIntegration_ReadsAndQueries(t *testing.T) {
{"Jack", 1.85},
},
},
{
`SELECT str FROM SomeStrings WHERE str LIKE "a%"`,
nil,
[][]interface{}{
{"afoo"},
{"abar"},
},
},
{
`SELECT Name FROM Staff WHERE Name LIKE "%e"`,
nil,
[][]interface{}{
{"George"},
},
},
{
`SELECT Name FROM Staff WHERE Name LIKE "J%k" OR Name LIKE "_am"`,
nil,
Expand Down

0 comments on commit a2118f0

Please sign in to comment.