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

fix(spanner/spannertest): Fix the "LIKE" clause handling for forward and backward matches #4655

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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