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

yiguolei pushed a commit to branch 2.1-tmp
in repository https://gitbox.apache.org/repos/asf/doris.git

commit edd170196334672a0263dd5177ad82305753676b
Author: morrySnow <101034200+morrys...@users.noreply.github.com>
AuthorDate: Wed Apr 3 16:24:36 2024 +0800

    [fix](Nereids) convert agg state type failed in some cases (#33208)
---
 .../expression/rules/ConvertAggStateCast.java      | 38 +++++++++++++---------
 1 file changed, 22 insertions(+), 16 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/ConvertAggStateCast.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/ConvertAggStateCast.java
index 23900701553..6aa4529ddd4 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/ConvertAggStateCast.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/ConvertAggStateCast.java
@@ -19,6 +19,7 @@ package org.apache.doris.nereids.rules.expression.rules;
 
 import org.apache.doris.nereids.rules.expression.ExpressionPatternMatcher;
 import org.apache.doris.nereids.rules.expression.ExpressionPatternRuleFactory;
+import org.apache.doris.nereids.trees.expressions.Alias;
 import org.apache.doris.nereids.trees.expressions.Cast;
 import org.apache.doris.nereids.trees.expressions.Expression;
 import 
org.apache.doris.nereids.trees.expressions.functions.combinator.StateCombinator;
@@ -42,7 +43,7 @@ public class ConvertAggStateCast implements 
ExpressionPatternRuleFactory {
     @Override
     public List<ExpressionPatternMatcher<? extends Expression>> buildRules() {
         return ImmutableList.of(
-                matchesTopType(Cast.class).then(ConvertAggStateCast::convert)
+                matchesType(Cast.class).then(ConvertAggStateCast::convert)
         );
     }
 
@@ -50,24 +51,29 @@ public class ConvertAggStateCast implements 
ExpressionPatternRuleFactory {
         Expression child = cast.child();
         DataType originalType = child.getDataType();
         DataType targetType = cast.getDataType();
-        if (originalType instanceof AggStateType
-                && targetType instanceof AggStateType
-                && child instanceof StateCombinator) {
-            AggStateType target = (AggStateType) targetType;
-            ImmutableList.Builder<Expression> newChildren = 
ImmutableList.builderWithExpectedSize(child.arity());
-            for (int i = 0; i < child.arity(); i++) {
-                Expression newChild = 
TypeCoercionUtils.castIfNotSameType(child.child(i), 
target.getSubTypes().get(i));
-                if (newChild.nullable() != 
target.getSubTypeNullables().get(i)) {
-                    if (newChild.nullable()) {
-                        newChild = new NonNullable(newChild);
-                    } else {
-                        newChild = new Nullable(newChild);
+        if (originalType instanceof AggStateType && targetType instanceof 
AggStateType) {
+            // TODO remve it after we refactor mv rewriter to avoid generate 
Alias in expression
+            while (child instanceof Alias) {
+                child = ((Alias) child).child();
+            }
+            if (child instanceof StateCombinator) {
+                AggStateType target = (AggStateType) targetType;
+                ImmutableList.Builder<Expression> newChildren = 
ImmutableList.builderWithExpectedSize(child.arity());
+                for (int i = 0; i < child.arity(); i++) {
+                    Expression newChild = 
TypeCoercionUtils.castIfNotSameTypeStrict(
+                            child.child(i), target.getSubTypes().get(i));
+                    if (newChild.nullable() != 
target.getSubTypeNullables().get(i)) {
+                        if (newChild.nullable()) {
+                            newChild = new NonNullable(newChild);
+                        } else {
+                            newChild = new Nullable(newChild);
+                        }
                     }
+                    newChildren.add(newChild);
                 }
-                newChildren.add(newChild);
+                child = child.withChildren(newChildren.build());
+                return cast.withChildren(ImmutableList.of(child));
             }
-            child = child.withChildren(newChildren.build());
-            return cast.withChildren(ImmutableList.of(child));
         }
         return cast;
     }


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

Reply via email to