Kikyou1997 commented on code in PR #12274:
URL: https://github.com/apache/doris/pull/12274#discussion_r962339380


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PruneAggChildColumns.java:
##########
@@ -54,15 +59,65 @@ public class PruneAggChildColumns extends 
OneRewriteRuleFactory {
     @Override
     public Rule build() {
         return 
RuleType.COLUMN_PRUNE_AGGREGATION_CHILD.build(logicalAggregate().then(agg -> {
+            Slot slot = handleAggregateConstant(agg);
+            List<Slot> childOutput = agg.child().getOutput();
+            if (slot != null) {
+                if (childOutput.size() == 1 && 
childOutput.get(0).equals(slot)) {
+                    return agg;
+                }
+                return agg.withChildren(ImmutableList.of(new 
LogicalProject<>(ImmutableList.of(slot), agg.child())));
+            }
             List<Expression> slots = Lists.newArrayList();
             slots.addAll(agg.getExpressions());
             Set<Slot> outputs = SlotExtractor.extractSlot(slots);
-            List<NamedExpression> prunedOutputs = 
agg.child().getOutput().stream().filter(outputs::contains)
+            List<NamedExpression> prunedOutputs = 
childOutput.stream().filter(outputs::contains)
                     .collect(Collectors.toList());
             if (prunedOutputs.size() == agg.child().getOutput().size()) {
                 return agg;
             }
             return agg.withChildren(ImmutableList.of(new 
LogicalProject<>(prunedOutputs, agg.child())));
         }));
     }
+
+    /**
+     * For these aggregate function with constant param. Such as:
+     *  count(*), count(1), sum(1)..etc.
+     * @return null, if there exists an aggregation function that its 
parameters contains non-constant expr.
+     *               else return a slot with min data type.
+     */
+    private Slot handleAggregateConstant(LogicalAggregate<GroupPlan> agg) {
+        List<NamedExpression> outputExpressions = agg.getOutputExpressions();
+        for (NamedExpression namedExpression : outputExpressions) {
+            if (!(namedExpression instanceof Alias)) {
+                return null;
+            }
+            Expression childOfAlias = ((Alias) namedExpression).child();
+            if (!(childOfAlias instanceof AggregateFunction)) {
+                return null;
+            }
+            if (childOfAlias instanceof Count) {
+                Count countFunc = (Count) childOfAlias;
+                if (!countFunc.isStar()) {
+                    return null;
+                }
+            } else if (childOfAlias.children().stream().anyMatch(e -> 
!e.isConstant())) {

Review Comment:
   Just can't get it...could you give a sample case pls ? We could only pass a 
scalar function or a function that returns a constant value like `NOW()` as the 
params of agg function, for the former, this rule is useless , for the latter, 
it should be cast to literal before this rule, so what's the meaning of 
`generate random output`?



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

Reply via email to