This is an automated email from the ASF dual-hosted git repository.

starocean999 pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.0 by this push:
     new c8a5c2628b0 [fix](nereids)need substitute agg function using agg 
node's output if it's in order by key (#30758)
c8a5c2628b0 is described below

commit c8a5c2628b0d3942834bc2d2c78fff70dd013fbc
Author: starocean999 <40539150+starocean...@users.noreply.github.com>
AuthorDate: Fri Feb 2 17:20:48 2024 +0800

    [fix](nereids)need substitute agg function using agg node's output if it's 
in order by key (#30758)
---
 .../nereids/rules/analysis/FillUpMissingSlots.java |  7 ++++++-
 .../rules/analysis/FillUpMissingSlotsTest.java     | 24 ++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/FillUpMissingSlots.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/FillUpMissingSlots.java
index f7c04777457..1a59ba00adf 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/FillUpMissingSlots.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/FillUpMissingSlots.java
@@ -182,10 +182,14 @@ public class FillUpMissingSlots implements 
AnalysisRuleFactory {
         private final List<Expression> groupByExpressions;
         private final Map<Expression, Slot> substitution = Maps.newHashMap();
         private final List<NamedExpression> newOutputSlots = 
Lists.newArrayList();
+        private final Map<Slot, Expression> outputSubstitutionMap;
 
         Resolver(Aggregate aggregate) {
             outputExpressions = aggregate.getOutputExpressions();
             groupByExpressions = aggregate.getGroupByExpressions();
+            outputSubstitutionMap = 
outputExpressions.stream().filter(Alias.class::isInstance)
+                    .collect(Collectors.toMap(alias -> alias.toSlot(), alias 
-> alias.child(0),
+                        (k1, k2) -> k1));
         }
 
         public void resolve(Expression expression) {
@@ -272,7 +276,8 @@ public class FillUpMissingSlots implements 
AnalysisRuleFactory {
         }
 
         private void generateAliasForNewOutputSlots(Expression expression) {
-            Alias alias = new Alias(expression);
+            Expression replacedExpr = ExpressionUtils.replace(expression, 
outputSubstitutionMap);
+            Alias alias = new Alias(replacedExpr);
             newOutputSlots.add(alias);
             substitution.put(expression, alias.toSlot());
         }
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/FillUpMissingSlotsTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/FillUpMissingSlotsTest.java
index 7b0c8095ba6..d6c71ee7759 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/FillUpMissingSlotsTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/analysis/FillUpMissingSlotsTest.java
@@ -32,6 +32,7 @@ import 
org.apache.doris.nereids.trees.expressions.SlotReference;
 import org.apache.doris.nereids.trees.expressions.functions.agg.Count;
 import org.apache.doris.nereids.trees.expressions.functions.agg.Min;
 import org.apache.doris.nereids.trees.expressions.functions.agg.Sum;
+import org.apache.doris.nereids.trees.expressions.functions.scalar.Abs;
 import org.apache.doris.nereids.trees.expressions.literal.Literal;
 import org.apache.doris.nereids.trees.expressions.literal.TinyIntLiteral;
 import org.apache.doris.nereids.types.BigIntType;
@@ -513,6 +514,29 @@ public class FillUpMissingSlotsTest extends 
AnalyzeCheckTestBase implements Memo
                                                 
).when(FieldChecker.check("outputExpressions", Lists.newArrayList(a1, 
countStar))))
                                 ).when(FieldChecker.check("orderKeys", 
ImmutableList.of(new OrderKey(countStar.toSlot(), true, true))))
                         ).when(FieldChecker.check("projects", 
Lists.newArrayList(a1.toSlot()))));
+
+        sql = "SELECT abs(a1) xx, sum(a2) FROM t1 GROUP BY xx ORDER BY 
MIN(xx)";
+        a1 = new SlotReference(
+                new ExprId(1), "a1", TinyIntType.INSTANCE, true,
+                ImmutableList.of("test_resolve_aggregate_functions", "t1")
+        );
+        Alias xx = new Alias(new ExprId(3), new Abs(a1), "xx");
+        a2 = new SlotReference(
+                new ExprId(2), "a2", TinyIntType.INSTANCE, true,
+                ImmutableList.of("test_resolve_aggregate_functions", "t1")
+        );
+        sumA2 = new Alias(new ExprId(4), new Sum(a2), "sum(a2)");
+
+        Alias minXX = new Alias(new ExprId(5), new Min(xx.toSlot()), 
"min(xx)");
+        
PlanChecker.from(connectContext).analyze(sql).printlnTree().matches(logicalProject(
+                
logicalSort(logicalProject(logicalAggregate(logicalProject(logicalOlapScan())
+                        .when(FieldChecker.check("projects", 
Lists.newArrayList(xx, a2, a1))))))
+                                .when(FieldChecker.check("orderKeys",
+                                        ImmutableList
+                                                .of(new 
OrderKey(minXX.toSlot(), true, true)))))
+                                                        
.when(FieldChecker.check("projects",
+                                                                
Lists.newArrayList(xx.toSlot(),
+                                                                        
sumA2.toSlot()))));
     }
 
     @Test


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to