starocean999 commented on code in PR #49589: URL: https://github.com/apache/doris/pull/49589#discussion_r2041812813
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/NormalizeAggregate.java: ########## @@ -389,4 +398,93 @@ 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, LogicalAggregate<?> newAggregate) { + // 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 new LogicalProject<>(upperProjects, newAggregate); + } + 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 new LogicalProject<>(upperProjects, newAggregate); + } + // 2. Regenerate a group by list without constant key + List<Expression> newNormalizedGroupExprs = new ArrayList<>(); + for (Expression normalizedGroupExpr : normalizedGroupExprs) { + if (!slotToLiteral.containsKey((Slot) normalizedGroupExpr)) { + newNormalizedGroupExprs.add(normalizedGroupExpr); + } + } + if (newNormalizedGroupExprs.size() == normalizedGroupExprs.size()) { + return new LogicalProject<>(upperProjects, newAggregate); + } + if (newNormalizedGroupExprs.isEmpty()) { + Alias tinyInt = new Alias(new TinyIntLiteral((byte) 1)); + bottomProjects = new HashSet<>(bottomProjects); + bottomProjects.add(tinyInt); + normalizedAggOutput = new ArrayList<>(normalizedAggOutput); + Slot tinyIntSlot = tinyInt.toSlot(); + normalizedAggOutput.add(tinyIntSlot); Review Comment: please check if it's be's requirement that all group by exprs should be in agg's 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