Dwrite commented on code in PR #5131:
URL: https://github.com/apache/calcite/pull/5131#discussion_r3691013081
##########
mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoAdapterTest.java:
##########
@@ -1178,4 +1178,153 @@ private static Consumer<List> mongoChecker(final
String... expected) {
"CITY_SUBSTRING=RA",
"CITY_SUBSTRING=UT");
}
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7673">[CALCITE-7673]
+ * MongoDB Adapter can not support LIKE operator</a>. */
+ @Test void testLikePrefix() {
+ // Test LIKE on state field - matches states starting with 'A'
+ assertModel(MODEL)
+ .query("select state, city from zips where state like 'A%' order by
state")
+ .returnsUnordered(
+ "STATE=AK; CITY=ANCHORAGE",
+ "STATE=AK; CITY=FAIRBANKS",
+ "STATE=AK; CITY=JUNEAU",
+ "STATE=AL; CITY=CENTER POINT",
+ "STATE=AL; CITY=TUSCALOOSA",
+ "STATE=AL; CITY=SOUTHSIDE",
+ "STATE=AR; CITY=CONWAY",
+ "STATE=AR; CITY=GRAVEL RIDGE",
+ "STATE=AR; CITY=JONESBORO",
+ "STATE=AZ; CITY=MESA",
+ "STATE=AZ; CITY=PHOENIX",
+ "STATE=AZ; CITY=YUMA");
+ }
+
+ /** Test case for LIKE operator with suffix pattern (ends with). */
+ @Test void testLikeSuffix() {
+ assertModel(MODEL)
+ .query("select state, city from zips where city like '%TON' order by
state, city")
+ .limit(5)
+ .returnsOrdered(
+ "STATE=DC; CITY=WASHINGTON",
+ "STATE=KY; CITY=HATTON",
+ "STATE=MA; CITY=BROCKTON",
+ "STATE=ME; CITY=LEWISTON",
+ "STATE=MN; CITY=NEW BRIGHTON");
+ }
+
+ /** Test case for LIKE operator with contains pattern. */
+ @Test void testLikeContains() {
+ assertModel(MODEL)
+ .query("select state, city from zips where city like '%ING%' order by
state")
+ .limit(5)
+ .returnsOrdered(
+ "STATE=DC; CITY=WASHINGTON",
+ "STATE=MA; CITY=FRAMINGHAM",
+ "STATE=MO; CITY=JENNINGS",
+ "STATE=MT; CITY=BILLINGS",
+ "STATE=NC; CITY=LEXINGTON");
+ }
+
+ /** Test case for LIKE operator with single character wildcard. */
+ @Test void testLikeSingleChar() {
+ // Pattern 'NEW ______' matches cities starting with 'NEW ' followed by
exactly 6 characters
+ // NEW IBERIA: "NEW " + "IBERIA" (6 chars) = matches
+ // NEW ORLEANS: "NEW " + "ORLEANS" (7 chars) = does not match
+ // NEW YORK: "NEW " + "YORK" (4 chars) = does not match
+ assertModel(MODEL)
+ .query("select city, state from zips where city like 'NEW ______'
order by city")
+ .returnsOrdered(
+ "CITY=NEW IBERIA; STATE=LA");
+ }
+
+ /** Test case for LIKE operator combined with other filters. */
+ @Test void testLikeCombinedWithOtherFilters() {
+ assertModel(MODEL)
+ .query("select city, state from zips where city like 'L%' and state =
'CA' order by city")
+ .returnsOrdered(
+ "CITY=LOS ANGELES; STATE=CA");
+ }
+
+ /** Test case for LIKE operator verifying the generated MongoDB regex. */
+ @Test void testLikeGeneratedRegex() {
+ assertModel(MODEL)
+ .query("select state from zips where city like 'A%'")
+ .queryContains(
+ mongoChecker(
+ "{$match: {city: {$regex: '^A.*$'}}}",
+ "{$project: {STATE: '$state'}}"))
+ .returnsUnordered(
+ "STATE=AK",
+ "STATE=IA",
+ "STATE=SC",
+ "STATE=SD",
+ "STATE=TX");
+ }
+
+ /** Test case for LIKE operator with escape character on underscore. */
+ @Test void testLikeEscapeUnderscore() {
+ // Without escape, '_' matches a single character.
+ assertModel(MODEL)
+ .query("select city from zips where city like 'BROOKLY_'")
+ .returnsUnordered("CITY=BROOKLYN");
+ // With escape, '_' is a literal character and does not match BROOKLYN.
+ assertModel(MODEL)
+ .query("select city from zips where city like 'BROOKLY\\_' ESCAPE
'\\'")
+ .returnsUnordered();
+ }
+
+ /** Test case for LIKE operator with escape character on percent. */
+ @Test void testLikeEscapePercent() {
Review Comment:
Should you add some NOT LIKE test cases? The switch statement in the caller
only handles LIKE, not NOT_LIKE — if NOT LIKE isn't rewritten to NOT(LIKE(...))
before reaching this code, it may fall into the default branch and throw an
AssertionError. Worth adding a test like city not like 'A%' to confirm the
behavior.
##########
mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoAdapterTest.java:
##########
@@ -1178,4 +1178,153 @@ private static Consumer<List> mongoChecker(final
String... expected) {
"CITY_SUBSTRING=RA",
"CITY_SUBSTRING=UT");
}
+
+ /** Test case for
+ * <a
href="https://issues.apache.org/jira/browse/CALCITE-7673">[CALCITE-7673]
+ * MongoDB Adapter can not support LIKE operator</a>. */
+ @Test void testLikePrefix() {
+ // Test LIKE on state field - matches states starting with 'A'
+ assertModel(MODEL)
+ .query("select state, city from zips where state like 'A%' order by
state")
+ .returnsUnordered(
+ "STATE=AK; CITY=ANCHORAGE",
+ "STATE=AK; CITY=FAIRBANKS",
+ "STATE=AK; CITY=JUNEAU",
+ "STATE=AL; CITY=CENTER POINT",
+ "STATE=AL; CITY=TUSCALOOSA",
+ "STATE=AL; CITY=SOUTHSIDE",
+ "STATE=AR; CITY=CONWAY",
+ "STATE=AR; CITY=GRAVEL RIDGE",
+ "STATE=AR; CITY=JONESBORO",
+ "STATE=AZ; CITY=MESA",
+ "STATE=AZ; CITY=PHOENIX",
+ "STATE=AZ; CITY=YUMA");
+ }
+
+ /** Test case for LIKE operator with suffix pattern (ends with). */
+ @Test void testLikeSuffix() {
+ assertModel(MODEL)
+ .query("select state, city from zips where city like '%TON' order by
state, city")
+ .limit(5)
+ .returnsOrdered(
+ "STATE=DC; CITY=WASHINGTON",
+ "STATE=KY; CITY=HATTON",
+ "STATE=MA; CITY=BROCKTON",
+ "STATE=ME; CITY=LEWISTON",
+ "STATE=MN; CITY=NEW BRIGHTON");
+ }
+
+ /** Test case for LIKE operator with contains pattern. */
+ @Test void testLikeContains() {
+ assertModel(MODEL)
+ .query("select state, city from zips where city like '%ING%' order by
state")
+ .limit(5)
+ .returnsOrdered(
+ "STATE=DC; CITY=WASHINGTON",
+ "STATE=MA; CITY=FRAMINGHAM",
+ "STATE=MO; CITY=JENNINGS",
+ "STATE=MT; CITY=BILLINGS",
+ "STATE=NC; CITY=LEXINGTON");
+ }
+
+ /** Test case for LIKE operator with single character wildcard. */
+ @Test void testLikeSingleChar() {
+ // Pattern 'NEW ______' matches cities starting with 'NEW ' followed by
exactly 6 characters
+ // NEW IBERIA: "NEW " + "IBERIA" (6 chars) = matches
+ // NEW ORLEANS: "NEW " + "ORLEANS" (7 chars) = does not match
+ // NEW YORK: "NEW " + "YORK" (4 chars) = does not match
+ assertModel(MODEL)
+ .query("select city, state from zips where city like 'NEW ______'
order by city")
+ .returnsOrdered(
+ "CITY=NEW IBERIA; STATE=LA");
+ }
+
+ /** Test case for LIKE operator combined with other filters. */
+ @Test void testLikeCombinedWithOtherFilters() {
+ assertModel(MODEL)
+ .query("select city, state from zips where city like 'L%' and state =
'CA' order by city")
+ .returnsOrdered(
+ "CITY=LOS ANGELES; STATE=CA");
+ }
+
+ /** Test case for LIKE operator verifying the generated MongoDB regex. */
+ @Test void testLikeGeneratedRegex() {
+ assertModel(MODEL)
+ .query("select state from zips where city like 'A%'")
+ .queryContains(
+ mongoChecker(
+ "{$match: {city: {$regex: '^A.*$'}}}",
+ "{$project: {STATE: '$state'}}"))
+ .returnsUnordered(
+ "STATE=AK",
+ "STATE=IA",
+ "STATE=SC",
+ "STATE=SD",
+ "STATE=TX");
+ }
+
+ /** Test case for LIKE operator with escape character on underscore. */
+ @Test void testLikeEscapeUnderscore() {
+ // Without escape, '_' matches a single character.
+ assertModel(MODEL)
+ .query("select city from zips where city like 'BROOKLY_'")
+ .returnsUnordered("CITY=BROOKLYN");
+ // With escape, '_' is a literal character and does not match BROOKLYN.
+ assertModel(MODEL)
+ .query("select city from zips where city like 'BROOKLY\\_' ESCAPE
'\\'")
+ .returnsUnordered();
+ }
+
+ /** Test case for LIKE operator with escape character on percent. */
+ @Test void testLikeEscapePercent() {
+ // Without escape, '%' matches zero or more characters.
+ assertModel(MODEL)
+ .query("select city from zips where city like 'BROOKLYN%'")
+ .returnsUnordered("CITY=BROOKLYN");
+ // With escape, '%' is a literal character and does not match BROOKLYN.
+ assertModel(MODEL)
+ .query("select city from zips where city like 'BROOKLYN\\%' ESCAPE
'\\'")
+ .returnsUnordered();
+ }
+
+ /** Test case for LIKE operator without a default escape character. */
+ @Test void testLikeNoDefaultEscape() {
Review Comment:
TranslateLike explicitly handles case ITEM: (nested field / map access), but
all 12 current tests exercise only the INPUT_REF path. Since this branch is
dedicated code, it's currently completely unverified. Suggest adding at least
one case like _MAP['city'] like 'A%' to exercise 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]