diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java index 2fe58db9ecfa05..2cc192b9d11670 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/SelectStmt.java @@ -1062,6 +1062,9 @@ private void analyzeAggregation(Analyzer analyzer) throws AnalysisException { countAllMap = ExprSubstitutionMap.compose(multiCountOrSumDistinctMap, countAllMap, analyzer); List substitutedAggs = Expr.substituteList(aggExprs, countAllMap, analyzer, false); + // the resultExprs must substitute in the same way as aggExprs + // then resultExprs can be substitute correctly using combinedSmap + resultExprs = Expr.substituteList(resultExprs, countAllMap, analyzer, false); aggExprs.clear(); TreeNode.collect(substitutedAggs, Expr.isAggregatePredicate(), aggExprs); @@ -1395,7 +1398,11 @@ public void rewriteExprs(ExprRewriter rewriter) throws AnalysisException { // we must make sure the expr is analyzed before rewrite try { for (Expr expr : oriGroupingExprs) { - expr.analyze(analyzer); + if (!(expr instanceof SlotRef)) { + // if group expr is not a slotRef, it should be analyzed in the same way as result expr + // otherwise, the group expr is either a simple column or an alias, no need to analyze + expr.analyze(analyzer); + } } } catch (AnalysisException ex) { //ignore any exception @@ -1403,7 +1410,9 @@ public void rewriteExprs(ExprRewriter rewriter) throws AnalysisException { rewriter.rewriteList(oriGroupingExprs, analyzer); // after rewrite, need reset the analyze status for later re-analyze for (Expr expr : oriGroupingExprs) { - expr.reset(); + if (!(expr instanceof SlotRef)) { + expr.reset(); + } } } } @@ -1411,13 +1420,20 @@ public void rewriteExprs(ExprRewriter rewriter) throws AnalysisException { for (OrderByElement orderByElem : orderByElements) { // we must make sure the expr is analyzed before rewrite try { - orderByElem.getExpr().analyze(analyzer); + if (!(orderByElem.getExpr() instanceof SlotRef)) { + // if sort expr is not a slotRef, it should be analyzed in the same way as result expr + // otherwise, the sort expr is either a simple column or an alias, no need to analyze + orderByElem.getExpr().analyze(analyzer); + } } catch (AnalysisException ex) { //ignore any exception } orderByElem.setExpr(rewriter.rewrite(orderByElem.getExpr(), analyzer)); // after rewrite, need reset the analyze status for later re-analyze orderByElem.getExpr().reset(); + if (!(orderByElem.getExpr() instanceof SlotRef)) { + orderByElem.getExpr().reset(); + } } } }