rubenada commented on code in PR #5375:
URL: https://github.com/apache/calcite/pull/5375#discussion_r4422530366
##########
core/src/main/java/org/apache/calcite/schema/lookup/LikePattern.java:
##########
@@ -56,43 +60,56 @@ public static Predicate1<String> matcher(String
likePattern) {
if (likePattern == null || likePattern.equals(ANY)) {
return v1 -> true;
}
- final Pattern regex = likeToRegex(likePattern);
- return v1 -> regex.matcher(v1).matches();
+ final List<String> segments = segments(likePattern);
+ return v1 -> matches(segments, v1);
}
- /**
- * Converts a LIKE-style pattern (where '%' represents a wild-card, escaped
- * using '\') to a Java regex. It's always case sensitive.
- */
- public static Pattern likeToRegex(String pattern) {
- StringBuilder buf = new StringBuilder("^");
- char[] charArray = pattern.toCharArray();
- int slash = -2;
- for (int i = 0; i < charArray.length; i++) {
- char c = charArray[i];
- if (slash == i - 1) {
- buf.append('[').append(c).append(']');
- } else {
- switch (c) {
- case '\\':
- slash = i;
- break;
- case '%':
- buf.append(".*");
- break;
- case '[':
- buf.append("\\[");
- break;
- case ']':
- buf.append("\\]");
- break;
- default:
- buf.append('[').append(c).append(']');
+ /** Splits a LIKE-style pattern into the literal segments between its
+ * '%' wild-cards, resolving '\' escapes. A pattern with n wild-cards has
+ * n + 1 segments, some of which may be empty. */
+ private static List<String> segments(String pattern) {
+ final List<String> segments = new ArrayList<>();
+ final StringBuilder buf = new StringBuilder();
+ for (int i = 0; i < pattern.length(); i++) {
+ final char c = pattern.charAt(i);
+ if (c == '\\') {
+ if (++i < pattern.length()) {
+ buf.append(pattern.charAt(i));
}
+ } else if (c == '%') {
Review Comment:
however, looking back into it... this pattern comes as an Avatica's
`Meta#Pat` , whose javadoc says "Wrapper to remind API calls that a parameter
is a pattern (allows '%' and '_' wildcards, per the JDBC spec) rather than a
string to be matched exactly"; so probably it'd make sense to fix this and
document the breaking change on the RN.... I'll look into it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]