zabetak commented on code in PR #4505:
URL: https://github.com/apache/calcite/pull/4505#discussion_r2487071007


##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -488,9 +493,13 @@ private RexNode simplifyMultiply(RexCall e) {
   }
 
   private RexNode simplifyDivide(RexCall e) {
-    final int oneIndex = findLiteralIndex(e.operands, BigDecimal.ONE);
-    if (oneIndex == 1) {
-      RexNode leftOperand = e.getOperands().get(0);
+    RexNode leftOperand = e.getOperands().get(0);
+    RexNode rightOperand = e.getOperands().get(1);
+    if ((isSafeExpression(leftOperand) && STRONG.isNull(leftOperand))

Review Comment:
   OK, I checked the code in `RexSimplify#simplify(RexNode, RexUnknownAs)` and 
I realized that we handle arithmetic with nulls there. However, I feel that 
`NULL / y` to `NULL` is generally a "missed" simplification and not really 
relevant to the denominator being zero or not.
   
   I am a bit puzzled with the changes here cause there is a bit of overlap 
with the code in 
[SafeRexVisitor#visitCall](https://github.com/apache/calcite/blob/67e5bfcdc0151aa1926fa369382d12fc672ca5bf/core/src/main/java/org/apache/calcite/rex/RexSimplify.java#L1559).
 It seems that the case `X / NULL` is handled via the `SafeRexVisitor` while 
`NULL / X` is handled here. Shouldn't we handle both cases with the same code? 
Which is the best place to do it?



##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -1189,6 +1102,23 @@ private RexNode simplifyIs(RexCall call, RexUnknownAs 
unknownAs) {
         }
       }
       return RexUtil.composeDisjunction(rexBuilder, operands, false);
+    case CUSTOM:
+      if (a.getKind() == SqlKind.DIVIDE) {
+        RexNode op0 = ((RexCall) a).getOperands().get(0);
+        RexNode op1 = ((RexCall) a).getOperands().get(1);
+        if (RexUtil.isNull(op0) || RexUtil.isNull(op1)) {
+          return rexBuilder.makeLiteral(true);
+        }
+        if (!op1.isA(SqlKind.LITERAL)) {
+          return rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, 
simplifyGenericNode((RexCall) a));
+        }
+        if (checkLiteralValue(op1, BigDecimal.ZERO)) {
+          return rexBuilder.makeCall(SqlStdOperatorTable.IS_NULL, 
simplifyGenericNode((RexCall) a));
+        }
+        // op1 is a non-null and non-zero literal, so simplify op0
+        return simplifyIsNull(op0);
+      }
+      return null;

Review Comment:
   I was mainly referring to the new code that is added but if we want to do 
something more ambitious indeed we can post-pone to CALCITE-7263.



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