vineetgarg02 commented on a change in pull request #693: HIVE-21921: Support 
for correlated quantified predicates
URL: https://github.com/apache/hive/pull/693#discussion_r299683240
 
 

 ##########
 File path: 
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveSubQueryRemoveRule.java
 ##########
 @@ -230,33 +228,59 @@ private RexNode rewriteScalar(RelMetadataQuery mq, 
RexSubQuery e, Set<Correlatio
   private RexNode rewriteSomeAll(RexSubQuery e, Set<CorrelationId> 
variablesSet,
       HiveSubQRemoveRelBuilder builder) {
     final SqlQuantifyOperator op = (SqlQuantifyOperator) e.op;
-    assert(op == SqlStdOperatorTable.SOME_GE
-        || op == SqlStdOperatorTable.SOME_LE
-        || op == SqlStdOperatorTable.SOME_LT
-        || op == SqlStdOperatorTable.SOME_GT);
-    builder.push(e.rel)
-        .aggregate(builder.groupKey(),
-            op.comparisonKind == SqlKind.GREATER_THAN
-                || op.comparisonKind == SqlKind.GREATER_THAN_OR_EQUAL
-                ? builder.min("m", builder.field(0))
-                : builder.max("m", builder.field(0)),
-            builder.count(false, "c"),
-            builder.count(false, "d", builder.field(0)))
-        .as("q")
-        .join(JoinRelType.INNER);
-    return builder.call(SqlStdOperatorTable.CASE,
-        builder.call(SqlStdOperatorTable.EQUALS,
-            builder.field("q", "c"), builder.literal(0)),
-        builder.literal(false),
-        builder.call(SqlStdOperatorTable.IS_TRUE,
-            builder.call(RelOptUtil.op(op.comparisonKind, null),
-                e.operands.get(0), builder.field("q", "m"))),
-        builder.literal(true),
-        builder.call(SqlStdOperatorTable.GREATER_THAN,
-            builder.field("q", "c"), builder.field("q", "d")),
-        
e.rel.getCluster().getRexBuilder().makeNullLiteral(SqlTypeName.BOOLEAN),
-        builder.call(RelOptUtil.op(op.comparisonKind, null),
-            e.operands.get(0), builder.field("q", "m")));
+    assert (op == SqlStdOperatorTable.SOME_GE || op == 
SqlStdOperatorTable.SOME_LE
+        || op == SqlStdOperatorTable.SOME_LT || op == 
SqlStdOperatorTable.SOME_GT);
+    if (variablesSet.isEmpty()) {
+      builder.push(e.rel)
+          .aggregate(builder.groupKey(),
+              op.comparisonKind == SqlKind.GREATER_THAN
+                  || op.comparisonKind == SqlKind.GREATER_THAN_OR_EQUAL
+                  ? builder.min("m", builder.field(0))
+                  : builder.max("m", builder.field(0)),
+              builder.count(false, "c"),
+              builder.count(false, "d", builder.field(0)))
+          .as("q")
+          .join(JoinRelType.INNER);
+      return builder.call(SqlStdOperatorTable.CASE,
+          builder.call(SqlStdOperatorTable.EQUALS,
+              builder.field("q", "c"), builder.literal(0)),
 
 Review comment:
   In this case it should be safe to rely on aliases since rewrite will produce 
only `c`, `d`, and `m`. 
   With correlated case plan can have an extra `group by <key col>` but planner 
appropriately rename the column.
   e.g. 
   
   `explain cbo select * from t45 where i > ANY(select i from t45 t where t.c = 
t45.c);` 
   
   generates
   
   ```
   HiveProject(i=[$0], c=[$1])
     HiveFilter(condition=[OR(AND(>($0, $2), IS NOT TRUE(OR(IS NULL($5), =($3, 
0)))), AND(>($0, $2), IS NOT TRUE(OR(IS NULL($5), =($3, 0))), IS NOT TRUE(>($0, 
$2)), IS NOT TRUE(>($3, $4))))])
       HiveJoin(condition=[=($6, $1)], joinType=[left], algorithm=[none], 
cost=[{2.0 rows, 0.0 cpu, 0.0 io}])
         HiveProject(i=[$0], c=[$1])
           HiveTableScan(table=[[default, t45]], table:alias=[t45])
         HiveProject(m=[$1], c=[$2], d=[$3], alwaysTrue2115=[true], c0=[$0])
           HiveAggregate(group=[{1}], m=[MIN($0)], c=[COUNT()], d=[COUNT($0)])
             HiveFilter(condition=[IS NOT NULL($1)])
               HiveTableScan(table=[[default, t45]], table:alias=[t])
   ```

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

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to