Skip to content
Merged
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
13 changes: 12 additions & 1 deletion core/src/main/java/org/apache/calcite/rex/RexSimplify.java
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ private RexNode simplifyLike(RexCall e, RexUnknownAs unknownAs) {
// string with even escapes 'AA\\\\%%__%%AA' simplify to 'AA\\__%AA'
// string with odd escapes 'AA\\\\\\%%__%%AA' simplify to 'AA\\\\\\%__%AA'
private String simplifyMixedWildcards(String str, char escape) {
Pattern pattern = Pattern.compile("[_%]+");
Pattern pattern = getWildCardPattern(escape);
Matcher matcher = pattern.matcher(str);
StringBuilder builder = new StringBuilder();
int from = 0;
Expand All @@ -570,6 +570,17 @@ && consecutiveSameCharCountBefore(str, start - 1, escape) % 2 == 1) {
return builder.toString();
}

private static Pattern getWildCardPattern(char escape) {
switch (escape) {
case '%':
return Pattern.compile("_+");
Comment thread
snuyanzin marked this conversation as resolved.
case '_':
return Pattern.compile("%+");
default:
return Pattern.compile("[_%]+");
}
}

// Tool method: count the number of consecutive identical characters before index
private int consecutiveSameCharCountBefore(String str, int index, char escape) {
int count = 0;
Expand Down
10 changes: 10 additions & 0 deletions core/src/test/java/org/apache/calcite/rex/RexProgramTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4360,6 +4360,8 @@ private void checkSarg(String message, Sarg sarg,
* Mixed wildcards of _ and % need to be simplified in LIKE operator</a>.
* <a href="https://issues.apache.org/jira/browse/CALCITE-7578">[CALCITE-7578]
* LIKE with empty ESCAPE might fail with StringIndexOutOfBoundsException</a>.
* <a href="https://issues.apache.org/jira/browse/CALCITE-7588">[CALCITE-7588]
* LIKE with ESCAPE symbols containing wildcards fails</a>.
* */
@Test void testSimplifyLike() {
final RexNode ref = input(tVarchar(true, 10), 0);
Expand Down Expand Up @@ -4423,6 +4425,14 @@ private void checkSarg(String message, Sarg sarg,
"LIKE($0, '###%%#%#%A#%%#%A%###%%', '#')");
checkSimplifyUnchanged(like(ref, literal("A"), literal("#")));
checkSimplifyUnchanged(like(ref, literal("%A"), literal("#")));
checkSimplifyUnchanged(like(ref, literal("TE%_ST"), literal("%")));
checkSimplifyUnchanged(like(ref, literal("TE%%ST"), literal("%")));
checkSimplifyUnchanged(like(ref, literal("a%_b%%c"), literal("%")));
checkSimplifyUnchanged(like(ref, literal("%_%%A%_"), literal("%")));
// escape char equal to the '_' wildcard. '%E__S%' ESCAPE '_' is a literal '_'.
checkSimplifyUnchanged(like(ref, literal("%E__S%"), literal("_")));
checkSimplifyUnchanged(like(ref, literal("TE_%ST"), literal("_")));
checkSimplifyUnchanged(like(ref, literal("a_%b__c"), literal("_")));

// As above, but ref is NOT NULL
final RexNode refMandatory = vVarcharNotNull(0);
Expand Down
Loading