Copilot commented on code in PR #49589:
URL: https://github.com/apache/doris/pull/49589#discussion_r2030737453


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/NormalizeAggregate.java:
##########
@@ -389,4 +400,88 @@ private Expression 
normalizeAggFuncChildren(NormalizeToSlotContext context, Expr
             return expr;
         }
     }
+
+    private LogicalProject<Plan> 
eliminateGroupByConstant(NormalizeToSlotContext groupByExprContext,
+            ExpressionRewriteContext rewriteContext, List<Expression> 
normalizedGroupExprs,
+            List<NamedExpression> normalizedAggOutput, Set<NamedExpression> 
bottomProjects,
+            LogicalAggregate<Plan> aggregate, List<NamedExpression> 
upperProjects) {
+        // 1. Find the expressions in group by that can be folded into 
constants and build a map(slot, literal)
+        Map<Expression, NormalizeToSlotTriplet> replaceMap = 
groupByExprContext.getNormalizeToSlotMap();
+        if (replaceMap.isEmpty()) {
+            return null;
+        }
+        Map<Slot, Expression> slotToLiteral = new HashMap<>();
+        for (Map.Entry<Expression, NormalizeToSlotTriplet> entry : 
replaceMap.entrySet()) {
+            Expression foldExpression = 
FoldConstantRuleOnFE.evaluate(entry.getKey(), rewriteContext);
+            if (foldExpression.isConstant()) {
+                slotToLiteral.put(entry.getValue().remainExpr, foldExpression);
+            }
+        }
+        if (slotToLiteral.isEmpty()) {
+            return null;
+        }
+        // 2. Regenerate a group by list without constant key
+        List<Expression> newNormalizedGroupExprs = new ArrayList<>();
+        Expression lit = null;
+        for (Expression normalizedGroupExpr : normalizedGroupExprs) {
+            if (!slotToLiteral.containsKey((Slot) normalizedGroupExpr)) {
+                newNormalizedGroupExprs.add(normalizedGroupExpr);
+            } else {
+                lit = normalizedGroupExpr;

Review Comment:
   The cast to (Slot) on normalizedGroupExpr assumes it is always a Slot; 
consider adding an instanceof check to avoid potential ClassCastException.
   ```suggestion
               if (normalizedGroupExpr instanceof Slot && 
!slotToLiteral.containsKey((Slot) normalizedGroupExpr)) {
                   newNormalizedGroupExprs.add(normalizedGroupExpr);
               } else if (normalizedGroupExpr instanceof Slot) {
                   lit = normalizedGroupExpr;
               } else {
                   newNormalizedGroupExprs.add(normalizedGroupExpr);
   ```



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