-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[CALCITE-7693] Move MongoDB LIKE-to-regex conversion into runtime.Like for consistency #5158
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
Open
xuzifu666
wants to merge
2
commits into
apache:main
Choose a base branch
from
xuzifu666:calcite-7693
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+123
−72
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
core/src/test/java/org/apache/calcite/runtime/LikeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to you under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.calcite.runtime; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
|
|
||
| /** Unit tests for {@link Like}. */ | ||
| class LikeTest { | ||
|
|
||
| /** Test case for | ||
| * <a href="https://issues.apache.org/jira/browse/CALCITE-7693">[CALCITE-7693] | ||
| * Move MongoDB LIKE-to-regex conversion into runtime.Like for consistency</a>. */ | ||
| @Test void testSqlToRegexMongo() { | ||
| assertThat(Like.sqlToRegexMongo("", null), is("^$")); | ||
| assertThat(Like.sqlToRegexMongo("abc", null), is("^abc$")); | ||
| assertThat(Like.sqlToRegexMongo("A%", null), is("^A.*$")); | ||
| assertThat(Like.sqlToRegexMongo("A_", null), is("^A.$")); | ||
| assertThat(Like.sqlToRegexMongo("%abc%", null), is("^.*abc.*$")); | ||
| // '.' is an ordinary SQL LIKE character; it must be escaped so that it is | ||
| // literal in the generated regex. | ||
| assertThat(Like.sqlToRegexMongo("A.B%", null), is("^A\\.B.*$")); | ||
| } | ||
|
|
||
| @Test void testSqlToRegexMongoWithEscape() { | ||
| // '\' escapes the wildcards, making them literal. | ||
| assertThat(Like.sqlToRegexMongo("A\\_B\\%C%", "\\"), is("^A_B%C.*$")); | ||
| assertThat(Like.sqlToRegexMongo("BROOKLYN\\%", "\\"), is("^BROOKLYN%$")); | ||
| // A custom escape character. | ||
| assertThat(Like.sqlToRegexMongo("BROOKLYN!%", "!"), is("^BROOKLYN%$")); | ||
| // The escape character followed by itself is a literal escape character. | ||
| assertThat(Like.sqlToRegexMongo("A\\\\B", "\\"), is("^A\\\\B$")); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| import org.apache.calcite.rex.RexLiteral; | ||
| import org.apache.calcite.rex.RexNode; | ||
| import org.apache.calcite.rex.RexUtil; | ||
| import org.apache.calcite.runtime.Like; | ||
| import org.apache.calcite.sql.SqlKind; | ||
| import org.apache.calcite.sql.type.SqlTypeName; | ||
| import org.apache.calcite.util.JsonBuilder; | ||
|
|
@@ -321,8 +322,8 @@ private Void translateLike(RexCall call, | |
| final RexLiteral patternLiteral = (RexLiteral) right; | ||
| final String sqlPattern = patternLiteral.getValue2().toString(); | ||
|
|
||
| final @Nullable Character escapeChar = escapeChar(call); | ||
| final String finalRegex = sqlLikeToMongoRegex(sqlPattern, escapeChar); | ||
| final @Nullable String escapeChar = escapeChar(call); | ||
| final String finalRegex = Like.sqlToRegexMongo(sqlPattern, escapeChar); | ||
|
|
||
| switch (left.getKind()) { | ||
| case INPUT_REF: | ||
|
|
@@ -364,8 +365,8 @@ private Void translateNotLike(RexCall call, List<Map<String, Object>> orMapList) | |
| final RexLiteral patternLiteral = (RexLiteral) right; | ||
| final String sqlPattern = patternLiteral.getValue2().toString(); | ||
|
|
||
| final @Nullable Character escapeChar = escapeChar(call); | ||
| final String finalRegex = sqlLikeToMongoRegex(sqlPattern, escapeChar); | ||
| final @Nullable String escapeChar = escapeChar(call); | ||
| final String finalRegex = Like.sqlToRegexMongo(sqlPattern, escapeChar); | ||
|
|
||
| final String name; | ||
| switch (left.getKind()) { | ||
|
|
@@ -404,8 +405,8 @@ private static RexNode stripCast(RexNode node) { | |
| return node; | ||
| } | ||
|
|
||
| /** Returns the escape character declared in a LIKE expression, or null. */ | ||
| private static @Nullable Character escapeChar(RexCall call) { | ||
|
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. Should we rename this to escapeStr to match the parameter name in Like.sqlToRegexMongo? (escapeString also works but reads a bit ambiguous — could be misread as "a string that's been escaped" rather than "the escape string from the LIKE clause".) |
||
| /** Returns the escape string declared in a LIKE expression, or null. */ | ||
| private static @Nullable String escapeChar(RexCall call) { | ||
| if (call.operands.size() != 3) { | ||
| return null; | ||
| } | ||
|
|
@@ -417,72 +418,7 @@ private static RexNode stripCast(RexNode node) { | |
| if (escape.length() != 1) { | ||
| throw new AssertionError("cannot translate LIKE with multi-character escape: " + call); | ||
| } | ||
| return escape.charAt(0); | ||
| } | ||
|
|
||
| /** | ||
| * Converts SQL LIKE pattern to MongoDB regex pattern. | ||
| * | ||
| * <p>SQL: {@code %} matches zero or more characters, {@code _} matches a single | ||
| * character. MongoDB: {@code .*} matches zero or more characters, {@code .} | ||
| * matches a single character. | ||
| * | ||
| * <p>We add {@code ^} and {@code $} anchors so that the entire string matches | ||
| * the pattern, just as SQL LIKE does. | ||
| */ | ||
| private static String sqlLikeToMongoRegex(String sqlPattern, @Nullable Character escapeChar) { | ||
| final StringBuilder regex = new StringBuilder(sqlPattern.length() * 2); | ||
| regex.append("^"); | ||
| for (int i = 0; i < sqlPattern.length(); i++) { | ||
| char c = sqlPattern.charAt(i); | ||
| if (escapeChar != null && c == escapeChar) { | ||
| if (i == sqlPattern.length() - 1) { | ||
| throw new AssertionError("Invalid escape sequence at end of LIKE pattern: " | ||
| + sqlPattern); | ||
| } | ||
| final char nextChar = sqlPattern.charAt(i + 1); | ||
| if (nextChar == '%' || nextChar == '_' || nextChar == escapeChar) { | ||
| regex.append(escapeRegexChar(nextChar)); | ||
| i++; | ||
| } else { | ||
| throw new AssertionError("Invalid escape sequence in LIKE pattern: " + sqlPattern); | ||
| } | ||
| } else if (c == '%') { | ||
| regex.append(".*"); | ||
| } else if (c == '_') { | ||
| regex.append('.'); | ||
| } else { | ||
| regex.append(escapeRegexChar(c)); | ||
| } | ||
| } | ||
| regex.append("$"); | ||
| return regex.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Escapes a character for use in a MongoDB regex if it's a special regex character. | ||
| */ | ||
| private static String escapeRegexChar(char c) { | ||
| // MongoDB regex special characters that need escaping | ||
| switch (c) { | ||
| case '\\': | ||
| case '^': | ||
| case '$': | ||
| case '.': | ||
| case '|': | ||
| case '?': | ||
| case '*': | ||
| case '+': | ||
| case '(': | ||
| case ')': | ||
| case '[': | ||
| case ']': | ||
| case '{': | ||
| case '}': | ||
| return "\\" + c; | ||
| default: | ||
| return String.valueOf(c); | ||
| } | ||
| return escape; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is now public static in core, but its name (sqlToRegexMongo) makes it look Mongo-specific even though the logic is generic (anchored regex translation). Worth either noting in the javadoc that it's not actually Mongo-exclusive, or using a more neutral name — not blocking, just a naming nit.