jcamachor commented on a change in pull request #1909: [CALCITE-3914] Improve 
SubsitutionVisitor to consider RexCall of type PLUS and TIMES for 
canonicalization (Vineet Garg)
URL: https://github.com/apache/calcite/pull/1909#discussion_r406994488
 
 

 ##########
 File path: core/src/main/java/org/apache/calcite/plan/SubstitutionVisitor.java
 ##########
 @@ -345,13 +345,29 @@ private static RexNode canonizeNode(RexBuilder 
rexBuilder, RexNode condition) {
     case LESS_THAN_OR_EQUAL:
     case GREATER_THAN_OR_EQUAL: {
       RexCall call = (RexCall) condition;
-      final RexNode left = call.getOperands().get(0);
-      final RexNode right = call.getOperands().get(1);
+      RexNode left = call.getOperands().get(0);
+      left = canonizeNode(rexBuilder, left);
+      RexNode right = call.getOperands().get(1);
+      right = canonizeNode(rexBuilder, right);
+      call = (RexCall) rexBuilder.makeCall(call.getOperator(), left, right);
       if (left.toString().compareTo(right.toString()) <= 0) {
         return call;
       }
       return RexUtil.invert(rexBuilder, call);
     }
+    case PLUS:
+    case TIMES:
+      RexCall call = (RexCall) condition;
+      RexNode left = call.getOperands().get(0);
+      RexNode right = call.getOperands().get(1);
+      left = canonizeNode(rexBuilder, left);
+      right = canonizeNode(rexBuilder, right);
+
+      if (left.toString().compareTo(right.toString()) <= 0) {
+        return rexBuilder.makeCall(call.getOperator(), left, right);
+      } else {
+        return rexBuilder.makeCall(call.getOperator(), right, left);
 
 Review comment:
   We should introduce an additional check before returning the new swapped 
expression to verify that the return type of the new call is similar to the 
return type of the original one (we can do that via 
`SqlTypeUtil.equalSansNullability`); otherwise, we should not swap. For 
instance, in Calcite the type inference for + or * will be dependent on the 
order of the operands. But other systems, e.g., Hive, may have their own 
implementation and apply different rules for type inference.

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