xiedeyantu commented on code in PR #4452:
URL: https://github.com/apache/calcite/pull/4452#discussion_r2206976027


##########
mongodb/src/main/java/org/apache/calcite/adapter/mongodb/MongoFilter.java:
##########
@@ -267,5 +292,15 @@ private void translateOp2(String op, String name, 
RexLiteral right,
         multimap.put(name, Pair.of(op, right));
       }
     }
+
+    /** Translates is null/is not null to $exists. */
+    private Void translateUnary(RexCall call,
+        Multimap<String, Pair<String, RexLiteral>> multimap, Map<String, 
RexLiteral> eqMap) {
+      final RexNode left = call.operands.get(0);
+      final RexNode right = 
rexBuilder.makeNullLiteral(rexBuilder.getTypeFactory().createSqlType(SqlTypeName.NULL));
+      String op = call.getKind() == SqlKind.IS_NOT_NULL ? "$ne" : "$eq";

Review Comment:
   I suggest not to judge IS NUL or IS NOT NULL here, and modify this method 
according to the style above. Such as `translateBinary("$gte", "$lte", 
(RexCall) node, multimap, eqMap)`. And I want to add a IS NOT NULL test.



##########
mongodb/src/test/java/org/apache/calcite/adapter/mongodb/MongoAdapterTest.java:
##########
@@ -919,4 +919,72 @@ private static Consumer<List> mongoChecker(final String... 
expected) {
                 "{$sort: {STATE: 1}}"))
         .returns("STATE=ME; CITY=LEWISTON\nSTATE=VT; CITY=BRATTLEBORO\n");
   }
+
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-7079";>[CALCITE-7079]
+   * Mongo adapter: unable to translate  (A <> 'a' and A <> 'b') conditional 
case  </a>. */
+  @Test void testMultiNeFilterContition() {
+    assertModel(MODEL)
+        .query("select city, state from zips where city <> 'ABERDEEN' and city 
<> 'AIKEN'  "
+            + "order by city")
+        .limit(3)
+        .queryContains(
+            mongoChecker(
+                "{$match: {city: {$nin: [\"ABERDEEN\", \"AIKEN\"]}}}",
+                "{$project: {CITY: '$city', STATE: '$state'}}",
+                "{$sort: {CITY: 1}}"))
+        .returnsOrdered("CITY=ALTON; STATE=TX",
+            "CITY=AMES; STATE=IA",
+            "CITY=ANCHORAGE; STATE=AK");
+
+    assertModel(MODEL)
+        .query("select city, state from zips where city <> 'ABERDEEN' and city 
<> 'AIKEN'  "
+            + "or (state <> 'IA' and state <> 'TX') order by city")
+        .limit(3)
+        .queryContains(
+            mongoChecker(
+                "{$match: {$or: [{city: {$nin: [\"ABERDEEN\", \"AIKEN\"]}}, 
{state: {$nin: [\"IA\", \"TX\"]}}]}}",
+                "{$project: {CITY: '$city', STATE: '$state'}}",
+                "{$sort: {CITY: 1}}"))
+        .returnsOrdered("CITY=ABERDEEN; STATE=SD",
+            "CITY=AIKEN; STATE=SC",
+            "CITY=ALTON; STATE=TX");
+
+    assertModel(MODEL)
+        .query("select city, state from zips where city <> 'ABERDEEN' and city 
<> 'AIKEN'  "
+            + "and state <> 'IA' order by city")
+        .limit(3)
+        .queryContains(
+            mongoChecker(
+                "{$match: {city: {$nin: [\"ABERDEEN\", \"AIKEN\"]}, state: 
{$ne: \"IA\"}}}",
+                "{$project: {CITY: '$city', STATE: '$state'}}",
+                "{$sort: {CITY: 1}}"))
+        .returnsOrdered("CITY=ALTON; STATE=TX",
+            "CITY=ANCHORAGE; STATE=AK",
+            "CITY=BALTIMORE; STATE=MD");
+
+    assertModel(MODEL)
+        .query("select city, state from zips where city <> 'ABERDEEN' and city 
<> 'AIKEN'  "
+            + "and (state <> 'IA' or state <> 'TX') order by city")
+        .limit(3)
+        .queryContains(
+            mongoChecker(
+                "{$match: {city: {$nin: [\"ABERDEEN\", \"AIKEN\"]}, state: 
{$ne: null}}}",
+                "{$project: {CITY: '$city', STATE: '$state'}}",
+                "{$sort: {CITY: 1}}"))
+        .returnsOrdered("CITY=ALTON; STATE=TX",
+            "CITY=AMES; STATE=IA",
+            "CITY=ANCHORAGE; STATE=AK");
+
+    assertModel(MODEL)
+        .query("select city, state from zips where city <> 'ABERDEEN' and city 
<> 'AIKEN'  "
+            + "and state IS NULL order by city")

Review Comment:
   I suggest that there is no need to add this case. The above case can be 
simplified to `state: {$ne: null}`. If you want to clarify this problem, you 
can add a comment to the case.



-- 
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]

Reply via email to