Skip to content

Commit

Permalink
colexec: fix LIKE operators when patterns have escape characters
Browse files Browse the repository at this point in the history
Release note (bug fix): Previously, CockroachDB could incorrectly
evaluate LIKE expressions when the pattern contained the escape
characters `\` if the expressions were executed via the vectorized
engine.
  • Loading branch information
yuzefovich committed Jul 30, 2021
1 parent fe1fb73 commit cc4ee69
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pkg/sql/colexec/colexeccmp/like_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,19 @@ func GetLikeOperatorType(pattern string, negate bool) (LikeOpType, string, error
}
return LikeAlwaysMatch, "", nil
}
if len(pattern) > 1 && !strings.ContainsAny(pattern[1:len(pattern)-1], "_%") {
// There are no wildcards in the middle of the string, so we only need to
// use a regular expression if both the first and last characters are
hasEscape := strings.Contains(pattern, `\`)
if len(pattern) > 1 && !strings.ContainsAny(pattern[1:len(pattern)-1], "_%") && !hasEscape {
// There are no wildcards in the middle of the string as well as no
// escape characters in the whole string, so we only need to use a
// regular expression if both the first and last characters are
// wildcards.
//
// The presence of the escape characters breaks the assumptions of the
// optimized versions since we no longer could just use the string for a
// direct match - we'd need to do some preprocessing here to remove the
// escape characters.
// TODO(yuzefovich): add that preprocessing (for example, `\\` needs to
// be replaced with `\`).
firstChar := pattern[0]
lastChar := pattern[len(pattern)-1]
if !isWildcard(firstChar) && !isWildcard(lastChar) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/vectorize
Original file line number Diff line number Diff line change
Expand Up @@ -1271,3 +1271,12 @@ SELECT b FROM t66706@u WHERE NOT (b = 'foo')
----
bar
bar

# Regression test for ignoring the escaping in the LIKE pattern (#68040).
statement ok
CREATE TABLE t68040 (c) AS SELECT 'string with \ backslash'

query T
SELECT c FROM t68040 WHERE c LIKE '%\\%'
----
string with \ backslash

0 comments on commit cc4ee69

Please sign in to comment.