kgyrtkirk commented on a change in pull request #1322: [CALCITE-3198] 
ReduceExpressionsRule.FILTER_INSTANCE does not reduce 'NOT(x=a AND x=b)'
URL: https://github.com/apache/calcite/pull/1322#discussion_r307652698
 
 

 ##########
 File path: core/src/main/java/org/apache/calcite/rex/RexSimplify.java
 ##########
 @@ -1624,6 +1630,47 @@ private RexNode simplifyOrs(List<RexNode> terms, 
RexUnknownAs unknownAs) {
             continue;
           }
         }
+        break;
+      case NOT_EQUALS:
+        final Comparison notEqualsComparison =
+            Comparison.of(term, node -> RexUtil.isReferenceOrAccess(node, 
false));
+        if (notEqualsComparison != null) {
+          // We are dealing with a X<>A term, check if we saw before another 
NOT_EQUALS involving X
+          final RexNode prevNotEquals = 
notEqualsComparisonMap.get(notEqualsComparison.ref);
+          if (prevNotEquals == null) {
+            // This is the first NOT_EQUALS involving X, put it in the map
+            notEqualsComparisonMap.put(notEqualsComparison.ref, term);
+          } else {
+            // There is already in the map another NOT_EQUALS involving X:
+            //   - if it is already an IS_NOT_NULL: it was already simplified, 
ignore this term
+            //   - if it is not an IS_NOT_NULL (i.e. it is a NOT_EQUALS): 
check comparison values
+            if (prevNotEquals.getKind() != SqlKind.IS_NOT_NULL) {
+              final Comparable comparable1 = 
notEqualsComparison.literal.getValue();
+              //noinspection ConstantConditions
+              final Comparable comparable2 = Comparison.of(
+                  prevNotEquals, node -> RexUtil.isReferenceOrAccess(node, 
false))
+                  .literal.getValue();
+              //noinspection unchecked
+              if (comparable1.compareTo(comparable2) != 0) {
+                // We found: X <> A OR X <> B
+                if (unknownAs == RexUnknownAs.TRUE || 
!term.getType().isNullable()) {
 
 Review comment:
   the expression: `ref IS NOT NULL OR NULL` is the logically equvivalent 
rewrite of `ref<> 1 or ref<>2`
   instead of trying to guess in what conditions `ref IS NOT NULL OR NULL` will 
evaluate to true; run simplify on it; and if it gets back with an 
`isAlwaysTrue` ...then return true
   
   for example:  even thru ref might be nullable - we may have a predicate 
which states that `ref is not null`; so it actually will be able to simplify it 
to true with other techniques

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to