rubenada commented on code in PR #5375:
URL: https://github.com/apache/calcite/pull/5375#discussion_r4423069031
##########
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:
'_' support included
--
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]