Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -5080,40 +5080,32 @@ Pattern compileSqlLikePattern(String sqlLikePattern) {
}
StringBuilder regex = new StringBuilder(sqlLikePattern.length() * 2);
regex.append('^');
boolean escaped = false;
for (int i = 0; i < sqlLikePattern.length(); i++) {
char c = sqlLikePattern.charAt(i);
switch (c) {
case '%':
regex.append(".*");
break;
case '_':
regex.append('.');
break;
case '\\':
case '.':
case '[':
case ']':
case '(':
case ')':
case '{':
case '}':
case '*':
case '+':
case '?':
case '^':
case '$':
case '|':
regex.append('\\').append(c);
break;
default:
regex.append(c);
break;
if (!escaped && c == '\\') {
escaped = true;
continue;
} else if (!escaped && c == '%') {
regex.append(".*");
} else if (!escaped && c == '_') {
regex.append('.');
} else {
if (isRegexMetacharacter(c)) {
regex.append('\\');
}
regex.append(c);
escaped = false;
}
}
regex.append('$');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The current implementation ignores a trailing backslash if it is the last character in the sqlLikePattern. Previously, a trailing backslash was treated as a literal character and escaped in the resulting regex. To maintain backward compatibility and handle this edge case, the code should check if the escaped flag is still true after the loop and append the escaped backslash to the regex.

    if (escaped) {
      regex.append('\\').append('\\');
    }
    regex.append('$');

return Pattern.compile(regex.toString(), Pattern.CASE_INSENSITIVE);
}

private static boolean isRegexMetacharacter(char c) {
return "\\.[]{}()*+?^$|".indexOf(c) != -1;
}

boolean needsListing(String pattern) {
return pattern == null || pattern.contains("%") || pattern.contains("_");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,17 @@ public void testCompileSqlLikePattern() {
assertNotNull(bracketPattern);
assertTrue(bracketPattern.matcher("array[0]").matches());
assertFalse(bracketPattern.matcher("array_0_").matches());

// Escaped wildcards
Pattern escapedUnderscore = dbMetadata.compileSqlLikePattern("test\\_table");
assertNotNull(escapedUnderscore);
assertTrue(escapedUnderscore.matcher("test_table").matches());
assertFalse(escapedUnderscore.matcher("test1table").matches());

Pattern escapedPercent = dbMetadata.compileSqlLikePattern("100\\%discount");
assertNotNull(escapedPercent);
assertTrue(escapedPercent.matcher("100%discount").matches());
assertFalse(escapedPercent.matcher("100PERCENTdiscount").matches());
Comment on lines +255 to +256
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Consider adding test cases for an escaped escape character (e.g., \\) and a trailing escape character to ensure they are handled correctly and to prevent future regressions. Before adding these, please verify the existing test suite to ensure these scenarios are not already covered.

    assertTrue(escapedPercent.matcher("100%discount").matches());
    assertFalse(escapedPercent.matcher("100PERCENTdiscount").matches());

    // Escaped escape character
    Pattern escapedBackslash = dbMetadata.compileSqlLikePattern("test\\\\table");
    assertTrue(escapedBackslash.matcher("test\\table").matches());

    // Trailing escape character
    Pattern trailingBackslash = dbMetadata.compileSqlLikePattern("test\\");
    assertTrue(trailingBackslash.matcher("test\\").matches());
References
  1. Before adding new test cases for input validation, verify the existing test suite to ensure these scenarios are not already covered.

}

@Test
Expand Down
Loading