-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(bigquery-jdbc): Add escape character support for pattern matching #13259
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5080,40 +5080,60 @@ 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 '|': | ||
| if (escaped) { | ||
| if (isRegexMetacharacter(c)) { | ||
| regex.append('\\').append(c); | ||
| break; | ||
| default: | ||
| } else { | ||
| regex.append(c); | ||
| break; | ||
| } | ||
| escaped = false; | ||
| } else if (c == '\\') { | ||
| escaped = true; | ||
| } else { | ||
| switch (c) { | ||
| case '%': | ||
| regex.append(".*"); | ||
| break; | ||
| case '_': | ||
| regex.append('.'); | ||
| break; | ||
| default: | ||
| if (isRegexMetacharacter(c)) { | ||
| regex.append('\\').append(c); | ||
| } else { | ||
| regex.append(c); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| if (escaped) { | ||
| regex.append('\\').append('\\'); | ||
| } | ||
| regex.append('$'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation ignores a trailing backslash if it is the last character in the if (escaped) {
regex.append('\\').append('\\');
}
regex.append('$'); |
||
| return Pattern.compile(regex.toString(), Pattern.CASE_INSENSITIVE); | ||
| } | ||
|
|
||
| private boolean isRegexMetacharacter(char c) { | ||
| return c == '\\' | ||
| || c == '.' | ||
| || c == '[' | ||
| || c == ']' | ||
| || c == '(' | ||
| || c == ')' | ||
| || c == '{' | ||
| || c == '}' | ||
| || c == '*' | ||
| || c == '+' | ||
| || c == '?' | ||
| || c == '^' | ||
| || c == '$' | ||
| || c == '|'; | ||
| } | ||
|
logachev marked this conversation as resolved.
Outdated
|
||
|
|
||
| boolean needsListing(String pattern) { | ||
| return pattern == null || pattern.contains("%") || pattern.contains("_"); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding test cases for an escaped escape character (e.g., 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
|
||
| } | ||
|
|
||
| @Test | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.