From 4357d7193abd005bec60f93b801181c9ddf001f4 Mon Sep 17 00:00:00 2001 From: starocean999 <12095047@qq.com> Date: Fri, 28 Oct 2022 15:23:44 +0800 Subject: [PATCH 1/2] [fix](plan)result exprs should be substituted in the same way as agg exprs --- .../org/apache/doris/analysis/SelectStmt.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) 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..b6e0eaea4d38d1 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 @@ -1404,6 +1411,9 @@ public void rewriteExprs(ExprRewriter rewriter) throws AnalysisException { // 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 +1421,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(); + } } } } From f0203c1b1bbe4479cb628ed41c1c14e51dbf7ea1 Mon Sep 17 00:00:00 2001 From: starocean999 <12095047@qq.com> Date: Fri, 28 Oct 2022 15:29:43 +0800 Subject: [PATCH 2/2] remove unused code --- .../src/main/java/org/apache/doris/analysis/SelectStmt.java | 1 - 1 file changed, 1 deletion(-) 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 b6e0eaea4d38d1..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 @@ -1410,7 +1410,6 @@ 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(); }