morrySnow commented on code in PR #63690:
URL: https://github.com/apache/doris/pull/63690#discussion_r3387569788


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/eageraggregation/EagerAggRewriter.java:
##########
@@ -534,21 +656,382 @@ public Plan visitLogicalRelation(LogicalRelation 
relation, PushDownAggContext co
     }
 
     private Plan genAggregate(Plan child, PushDownAggContext context) {
-        if (context.isValid() && checkStats(child, context)) {
+        if (isPushDisabledByVariable(context)) {
+            context.getBilateralState().registerNoCountSlot(child);
+            return child;
+        }
+        if (checkStats(child, context) || isPushEnabledByVariable(context)) {
             List<NamedExpression> aggOutputExpressions = new ArrayList<>();
             for (AggregateFunction func : context.getAggFunctions()) {
                 aggOutputExpressions.add(context.getAliasMap().get(func));
             }
+            Alias countStarAlias = null;
+            boolean countStarAlreadyProjected = false;
+            Count countStar = new Count();
+            if (context.getAliasMap().containsKey(countStar)) {
+                countStarAlias = context.getAliasMap().get(countStar);
+                countStarAlreadyProjected = true;
+            } else {
+                countStarAlias = new Alias(countStar,
+                        "cnt" + 
context.getCascadesContext().getStatementContext().generateColumnName());
+            }
             aggOutputExpressions.addAll(context.getGroupKeys());
+            if (countStarAlias != null && !countStarAlreadyProjected) {
+                aggOutputExpressions.add(countStarAlias);
+            }
             LogicalAggregate genAgg = new 
LogicalAggregate(context.getGroupKeys(), aggOutputExpressions, child);
             NormalizeAggregate normalizeAggregate = new NormalizeAggregate();
-            return normalizeAggregate.normalizeAgg(genAgg, Optional.empty(),
+            Plan normalized = normalizeAggregate.normalizeAgg(genAgg, 
Optional.empty(),
                     context.getCascadesContext());
+
+            for (AggregateFunction func : context.getAggFunctions()) {
+                Alias a = context.getAliasMap().get(func);
+                
context.getBilateralState().registerPushedAggFuncSlot(a.getExprId(), 
a.toSlot());
+            }
+
+            if (countStarAlias != null) {
+                context.getBilateralState().registerCountSlot(normalized, 
countStarAlias.toSlot());
+            } else {
+                context.getBilateralState().registerNoCountSlot(normalized);
+            }
+            return normalized;
         } else {
+            context.getBilateralState().registerNoCountSlot(child);
             return child;
         }
     }
 
+    // Build the canonical project above a rewritten join after 
eager-aggregation pushdown.
+    // Responsibilities:
+    // 1. Restore the outputs expected by the parent rollup. If a join side 
has a childContext, materialize
+    //    that side's aggregate current values and group keys; otherwise 
forward the original join outputs.
+    // 2. For inner joins, recover join multiplicity by multiplying 
non-MIN/MAX aggregate current values by
+    //    the opposite side's count slot when that side contributes rows to 
the parent aggregate.
+    // 3. Append and register a synthetic join-count slot `cnt` (logical jcnt) 
for upper-level rollup.
+    //
+    // The examples below are schematic. The real project may keep extra 
forwarded slots such as join keys.
+    //
+    // Inner join + sum, single-side rewrite:
+    //   Before:
+    //     agg(sum(t1.a), sum(t2.a), gby t2.k)
+    //       -> inner join(k = k)
+    //            -> scan(t1)
+    //            -> scan(t2)
+    //   After:
+    //     agg(sum(s1), sum(s2), gby t2.k)
+    //       -> project(s1, t2.a * cnt1 as s2, t2.k, cnt1 as cnt)

Review Comment:
   What is the purpose of using `cnt1 as cnt` in the project?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to