soumyakanti3578 commented on code in PR #5494:
URL: https://github.com/apache/hive/pull/5494#discussion_r1802077104


##########
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/jdbc/JDBCExpandExpressionsRule.java:
##########
@@ -159,9 +160,16 @@ public void onMatch(RelOptRuleCall call) {
 
   }
 
-  RexNode analyzeRexNode(RexBuilder rexBuilder, RexNode condition) {
-    RexTransformIntoOrAndClause transformIntoInClause = new 
RexTransformIntoOrAndClause(rexBuilder);
+  RexNode analyzeRexNode(RelOptCluster cluster, RexNode condition) {
+    RexTransformIntoOrAndClause transformIntoInClause = new 
RexTransformIntoOrAndClause(cluster.getRexBuilder());
     RexNode newCondition = transformIntoInClause.apply(condition);
+
+    if (!RexUtil.isFlat(newCondition)) {
+      newCondition = new RexSimplify(

Review Comment:
   I tried `flatten` before trying `RexSimplify` and it didn't work. To me it 
looks like there is a bug in [isFlat(List<? extends RexNode> exprs, final 
SqlOperator 
op)](https://github.com/apache/calcite/blob/calcite-1.25.0/core/src/main/java/org/apache/calcite/rex/RexUtil.java#L1429):
   ```
     private static boolean isFlat(
         List<? extends RexNode> exprs, final SqlOperator op) {
       return !isAssociative(op)
           || !exists(exprs, (Predicate1<RexNode>) expr -> isCallTo(expr, op));
   ```
   and 
[isCallTo](https://github.com/apache/calcite/blob/calcite-1.25.0/core/src/main/java/org/apache/calcite/rex/RexUtil.java#L942):
   ```
     public static boolean isCallTo(RexNode expr, SqlOperator op) {
       return (expr instanceof RexCall)
           && (((RexCall) expr).getOperator() == op);
     }
   ```
   where we just check if the List of RexNodes are flat with respect to the 
SqlOperator. In this case, for the condition
   ```
   AND(OR(=($0, 0), OR(AND(=($0, 1), =($2, 11)), AND(=($0, 2), =($2, 22)))), 
IN($0, 0, 1, 2))
   ```
   it just checks whether the outer `AND` is flat with `IN`, and with the outer 
`OR`, but it doesn't go further and check if the list of operands themselves 
are flat. In this case, as we can see, `OR(=($0, 0), OR(AND(=($0, 1), =($2, 
11)), AND(=($0, 2), =($2, 22))))` is not flat.
   
   Comparing this to the overloaded [isFlat(RexNode 
expr)](https://github.com/apache/calcite/blob/calcite-1.25.0/core/src/main/java/org/apache/calcite/rex/RexUtil.java#L1436):
   ```
     public static boolean isFlat(RexNode expr) {
       if (!(expr instanceof RexCall)) {
         return true;
       }
       final RexCall call = (RexCall) expr;
       return isFlat(call.getOperands(), call.getOperator())
           && all(call.getOperands(), RexUtil::isFlat);
     }
   ```
   we see a call to check if `all` operands are flat or not. This returns 
`false` for the `condition` shown above, and we call this method when checking 
the condition in Hive.
   
   I was able to use `flatten` elsewhere in the new commit, and for now it 
seems to resolve this because it tries to flatten bottom up, right after an 
expression is created.



-- 
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: gitbox-unsubscr...@hive.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org
For additional commands, e-mail: gitbox-h...@hive.apache.org

Reply via email to