danny0405 commented on a change in pull request #1121: [CALCITE-2936] Simplify 
EXISTS or NOT EXISTS sub-query that has "GROUP BY ()"
URL: https://github.com/apache/calcite/pull/1121#discussion_r268141917
 
 

 ##########
 File path: core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
 ##########
 @@ -1165,8 +1165,22 @@ private void substituteSubQuery(Blackboard bb, SubQuery 
subQuery) {
       if (!config.isExpand()) {
         return;
       }
-      converted = convertExists(query, RelOptUtil.SubQueryType.EXISTS,
-          subQuery.logic, true, null);
+      final SqlValidatorScope seekScope =
+          (query instanceof SqlSelect)
+              ? validator.getSelectScope((SqlSelect) query)
+              : null;
+      final Blackboard seekBb = createBlackboard(seekScope, null, false);
+      final RelNode seekRel = convertQueryOrInList(seekBb, query, null);
+      // An existential sub-query whose inner child has at least 1 tuple
+      // (e.g. an Aggregate with no grouping columns or non-empty Values node)
+      // should be simplified to a Boolean constant expression.
+      final RelMetadataQuery mq = seekRel.getCluster().getMetadataQuery();
+      if (mq.getMinRowCount(seekRel) > 0.0) {
+        subQuery.expr = rexBuilder.makeLiteral(true);
 
 Review comment:
   @hsyuan As the java doc has presented:
   >Estimates the minimum number of rows which will be returned by a
   >relational expression.
   >
   >The default implementation for this query returns 0,
   >but metadata providers can override this with their own cost models
   
   This value defaults to be 0 and is estimated, so it is not 100% accurate. I 
still think we should use statistics 
    metadata to auxiliary planning but not deterministic node rewrite.

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