morrySnow commented on code in PR #27471: URL: https://github.com/apache/doris/pull/27471#discussion_r1405645714
########## fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Expression.java: ########## @@ -223,6 +224,10 @@ public Expression withChildren(List<Expression> children) { * Whether the expression is a constant. */ public boolean isConstant() { + if (this instanceof AggregateFunction) { Review Comment: do we need add window expression and tvf? ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/CountLiteralRewrite.java: ########## @@ -21,31 +21,59 @@ import org.apache.doris.nereids.rules.RuleType; import org.apache.doris.nereids.trees.expressions.Expression; import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator; import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction; import org.apache.doris.nereids.trees.expressions.functions.agg.Count; +import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalOneRowRelation; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; import com.google.common.collect.Lists; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; /** * count(1) ==> count(*) + * count(null) ==> 0 */ -public class CountLiteralToCountStar extends OneRewriteRuleFactory { +public class CountLiteralRewrite extends OneRewriteRuleFactory { @Override public Rule build() { return logicalAggregate().then( agg -> { List<NamedExpression> newExprs = Lists.newArrayListWithCapacity(agg.getOutputExpressions().size()); - if (rewriteCountLiteral(agg.getOutputExpressions(), newExprs)) { - return agg.withAggOutput(newExprs); + if (!rewriteCountLiteral(agg.getOutputExpressions(), newExprs)) { + // no need to rewrite + return agg; + } + + Map<Boolean, List<NamedExpression>> projectsAndAggFunc = newExprs.stream() + .collect(Collectors.partitioningBy(Expression::isConstant)); + + if (projectsAndAggFunc.get(false).isEmpty()) { + // if there is no group by keys and other agg func, just return the one row Relations, such as + // select count(null) from t Review Comment: it is not right when base table is empty, think about ```sql select 'abc' from t group by 'abc' ``` -- 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: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org