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_r267804795
##########
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:
Should we introduce statistics directly during sqlNode to RelNode converting
? Some how we can not make sure the statistics is definitely right, this may
causes wrong results. I think it's a little radical promotion.
----------------------------------------------------------------
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