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

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


The following commit(s) were added to refs/heads/branch-2.1 by this push:
     new f4e73378964 branch-2.1: [fix](Nereids) let anonymous alias same as 
user input #47093 (#47770)
f4e73378964 is described below

commit f4e73378964c9b1b778b92e877f25234e2ec03c0
Author: morrySnow <zhangwen...@selectdb.com>
AuthorDate: Fri Feb 14 16:53:57 2025 +0800

    branch-2.1: [fix](Nereids) let anonymous alias same as user input #47093 
(#47770)
    
    pick from master #47093
---
 .../doris/nereids/analyzer/UnboundAlias.java       |  22 ++++++++++++++++-----
 .../doris/nereids/parser/LogicalPlanBuilder.java   |  11 ++++++++++-
 .../nereids/rules/analysis/ExpressionAnalyzer.java |   2 +-
 .../doris/nereids/trees/expressions/Alias.java     |   6 +++++-
 .../trees/plans/commands/insert/InsertUtils.java   |   9 +++++++--
 .../rules/analysis/FillUpMissingSlotsTest.java     |   8 ++++----
 .../data/nereids_hint_tpcds_p0/shape/query54.out   | Bin 7315 -> 7307 bytes
 .../push_down_count_through_join.out               | Bin 29572 -> 29578 bytes
 .../push_down_count_through_join_one_side.out      | Bin 31747 -> 31753 bytes
 .../eager_aggregate/push_down_min_through_join.out | Bin 16427 -> 16433 bytes
 .../eager_aggregate/push_down_sum_through_join.out | Bin 16193 -> 16199 bytes
 .../push_down_sum_through_join_one_side.out        | Bin 17453 -> 17459 bytes
 .../filter_push_down/push_filter_through.out       | Bin 10142 -> 10141 bytes
 .../bs_downgrade_shape/query54.out                 | Bin 7149 -> 7141 bytes
 .../shape/query54.out                              | Bin 7149 -> 7141 bytes
 .../noStatsRfPrune/query54.out                     | Bin 6151 -> 6143 bytes
 .../no_stats_shape/query54.out                     | Bin 6420 -> 6412 bytes
 .../rf_prune/query54.out                           | Bin 7259 -> 7251 bytes
 .../nereids_tpcds_shape_sf100_p0/shape/query54.out | Bin 7306 -> 7298 bytes
 .../subquery/test_duplicate_name_in_view.groovy    |   2 +-
 .../suites/nereids_syntax_p0/select_const.groovy   |   2 ++
 21 files changed, 47 insertions(+), 15 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundAlias.java 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundAlias.java
index 25d40dd5981..043542eaac6 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundAlias.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/analyzer/UnboundAlias.java
@@ -36,21 +36,29 @@ import java.util.Optional;
  */
 public class UnboundAlias extends NamedExpression implements UnaryExpression, 
Unbound, PropagateNullable {
 
-    private Optional<String> alias;
+    private final Optional<String> alias;
+    private final boolean nameFromChild;
 
     public UnboundAlias(Expression child) {
-        super(ImmutableList.of(child));
-        this.alias = Optional.empty();
+        this(ImmutableList.of(child), Optional.empty());
     }
 
     public UnboundAlias(Expression child, String alias) {
-        super(ImmutableList.of(child));
-        this.alias = Optional.of(alias);
+        this(ImmutableList.of(child), Optional.of(alias));
+    }
+
+    public UnboundAlias(Expression child, String alias, boolean nameFromChild) 
{
+        this(ImmutableList.of(child), Optional.of(alias), nameFromChild);
     }
 
     private UnboundAlias(List<Expression> children, Optional<String> alias) {
+        this(children, alias, false);
+    }
+
+    private UnboundAlias(List<Expression> children, Optional<String> alias, 
boolean nameFromChild) {
         super(children);
         this.alias = alias;
+        this.nameFromChild = nameFromChild;
     }
 
     @Override
@@ -89,4 +97,8 @@ public class UnboundAlias extends NamedExpression implements 
UnaryExpression, Un
     public Optional<String> getAlias() {
         return alias;
     }
+
+    public boolean isNameFromChild() {
+        return nameFromChild;
+    }
 }
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
index 50e15f8791c..0ae1a209f55 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java
@@ -230,6 +230,7 @@ import 
org.apache.doris.nereids.properties.SelectHintUseCboRule;
 import org.apache.doris.nereids.properties.SelectHintUseMv;
 import org.apache.doris.nereids.trees.TableSample;
 import org.apache.doris.nereids.trees.expressions.Add;
+import org.apache.doris.nereids.trees.expressions.Alias;
 import org.apache.doris.nereids.trees.expressions.And;
 import org.apache.doris.nereids.trees.expressions.BitAnd;
 import org.apache.doris.nereids.trees.expressions.BitNot;
@@ -1557,7 +1558,15 @@ public class LogicalPlanBuilder extends 
DorisParserBaseVisitor<Object> {
                 if (expression instanceof NamedExpression) {
                     return (NamedExpression) expression;
                 } else {
-                    return new UnboundAlias(expression);
+                    int start = ctx.expression().start.getStartIndex();
+                    int stop = ctx.expression().stop.getStopIndex();
+                    String alias = ctx.start.getInputStream()
+                            .getText(new 
org.antlr.v4.runtime.misc.Interval(start, stop));
+                    if (expression instanceof Literal) {
+                        return new Alias(expression, alias, true);
+                    } else {
+                        return new UnboundAlias(expression, alias, true);
+                    }
                 }
             }
             String alias = visitIdentifierOrText(ctx.identifierOrText());
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzer.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzer.java
index b6f436f1560..624b3248056 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzer.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ExpressionAnalyzer.java
@@ -262,7 +262,7 @@ public class ExpressionAnalyzer extends 
SubExprAnalyzer<ExpressionRewriteContext
     public Expression visitUnboundAlias(UnboundAlias unboundAlias, 
ExpressionRewriteContext context) {
         Expression child = unboundAlias.child().accept(this, context);
         if (unboundAlias.getAlias().isPresent()) {
-            return new Alias(child, unboundAlias.getAlias().get());
+            return new Alias(child, unboundAlias.getAlias().get(), 
unboundAlias.isNameFromChild());
             // TODO: the variant bind element_at(slot, 'name') will return a 
slot, and we should
             //       assign an Alias to this function, this is trick and 
should refactor it
         } else if (!(unboundAlias.child() instanceof ElementAt) && child 
instanceof NamedExpression) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java
index 9ff90b5ac96..48a98eb3ee1 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java
@@ -48,7 +48,11 @@ public class Alias extends NamedExpression implements 
UnaryExpression {
      * @param name alias name
      */
     public Alias(Expression child, String name) {
-        this(StatementScopeIdGenerator.newExprId(), child, name, false);
+        this(child, name, false);
+    }
+
+    public Alias(Expression child, String name, boolean nameFromChild) {
+        this(StatementScopeIdGenerator.newExprId(), child, name, 
nameFromChild);
     }
 
     public Alias(Expression child) {
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java
index c77789bc0c8..a0858e7571d 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java
@@ -382,8 +382,13 @@ public class InsertUtils {
     }
 
     private static Expression castValue(Expression value, DataType targetType) 
{
-        if (value instanceof UnboundAlias) {
-            return 
value.withChildren(TypeCoercionUtils.castUnbound(((UnboundAlias) 
value).child(), targetType));
+        if (value instanceof Alias) {
+            Expression oldChild = value.child(0);
+            Expression newChild = TypeCoercionUtils.castUnbound(oldChild, 
targetType);
+            return oldChild == newChild ? value : value.withChildren(newChild);
+        } else if (value instanceof UnboundAlias) {
+            UnboundAlias unboundAlias = (UnboundAlias) value;
+            return new 
Alias(TypeCoercionUtils.castUnbound(unboundAlias.child(), targetType));
         } else {
             return TypeCoercionUtils.castUnbound(value, targetType);
         }
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 02f3caffa80..cb01fc17e30 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
@@ -230,7 +230,7 @@ public class FillUpMissingSlotsTest extends 
AnalyzeCheckTestBase implements Memo
                         ).when(FieldChecker.check("projects", 
Lists.newArrayList(a1.toSlot(), sumA2.toSlot()))));
 
         sql = "SELECT a1, sum(a1 + a2) FROM t1 GROUP BY a1 HAVING sum(a1 + a2) 
> 0";
-        Alias sumA1A2 = new Alias(new ExprId(3), new Sum(new Add(a1, a2)), 
"sum((a1 + a2))");
+        Alias sumA1A2 = new Alias(new ExprId(3), new Sum(new Add(a1, a2)), 
"sum(a1 + a2)");
         PlanChecker.from(connectContext).analyze(sql)
                 .matches(
                         logicalProject(
@@ -353,7 +353,7 @@ public class FillUpMissingSlotsTest extends 
AnalyzeCheckTestBase implements Memo
         Alias sumA1 = new Alias(new ExprId(9), new Sum(a1), "sum(a1)");
         Alias countA1 = new Alias(new ExprId(13), new Count(a1), "count(a1)");
         Alias countA11 = new Alias(new ExprId(10), new Add(countA1.toSlot(), 
Literal.of((byte) 1)), "(count(a1) + 1)");
-        Alias sumA1A2 = new Alias(new ExprId(11), new Sum(new Add(a1, a2)), 
"sum((a1 + a2))");
+        Alias sumA1A2 = new Alias(new ExprId(11), new Sum(new Add(a1, a2)), 
"sum(a1 + a2)");
         Alias v1 = new Alias(new ExprId(12), new Count(a2), "v1");
         PlanChecker.from(connectContext).analyze(sql)
                 .matches(
@@ -466,7 +466,7 @@ public class FillUpMissingSlotsTest extends 
AnalyzeCheckTestBase implements Memo
                         ).when(FieldChecker.check("projects", 
Lists.newArrayList(a1.toSlot(), sumA2.toSlot()))));
 
         sql = "SELECT a1, sum(a1 + a2) FROM t1 GROUP BY a1 ORDER BY sum(a1 + 
a2)";
-        Alias sumA1A2 = new Alias(new ExprId(3), new Sum(new Add(a1, a2)), 
"sum((a1 + a2))");
+        Alias sumA1A2 = new Alias(new ExprId(3), new Sum(new Add(a1, a2)), 
"sum(a1 + a2)");
         PlanChecker.from(connectContext).analyze(sql)
                 .matches(
                         logicalSort(
@@ -552,7 +552,7 @@ public class FillUpMissingSlotsTest extends 
AnalyzeCheckTestBase implements Memo
         Alias sumA1 = new Alias(new ExprId(9), new Sum(a1), "sum(a1)");
         Alias countA1 = new Alias(new ExprId(13), new Count(a1), "count(a1)");
         Alias countA11 = new Alias(new ExprId(10), new Add(new Count(a1), 
Literal.of((byte) 1)), "(count(a1) + 1)");
-        Alias sumA1A2 = new Alias(new ExprId(11), new Sum(new Add(a1, a2)), 
"sum((a1 + a2))");
+        Alias sumA1A2 = new Alias(new ExprId(11), new Sum(new Add(a1, a2)), 
"sum(a1 + a2)");
         Alias v1 = new Alias(new ExprId(12), new Count(a2), "v1");
         PlanChecker.from(connectContext).analyze(sql)
                 
.matches(logicalProject(logicalSort(logicalProject(logicalAggregate(logicalProject(
diff --git a/regression-test/data/nereids_hint_tpcds_p0/shape/query54.out 
b/regression-test/data/nereids_hint_tpcds_p0/shape/query54.out
index 0a4c09b07d0..6171fe33693 100644
Binary files a/regression-test/data/nereids_hint_tpcds_p0/shape/query54.out and 
b/regression-test/data/nereids_hint_tpcds_p0/shape/query54.out differ
diff --git 
a/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_count_through_join.out
 
b/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_count_through_join.out
index ca56c802a9b..cc5d07424b2 100644
Binary files 
a/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_count_through_join.out
 and 
b/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_count_through_join.out
 differ
diff --git 
a/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_count_through_join_one_side.out
 
b/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_count_through_join_one_side.out
index 49f21c8db90..1f3c859c57a 100644
Binary files 
a/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_count_through_join_one_side.out
 and 
b/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_count_through_join_one_side.out
 differ
diff --git 
a/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_min_through_join.out
 
b/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_min_through_join.out
index 03e52839ecf..707c24b3754 100644
Binary files 
a/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_min_through_join.out
 and 
b/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_min_through_join.out
 differ
diff --git 
a/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_sum_through_join.out
 
b/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_sum_through_join.out
index 62930e78a09..d1f77bfb819 100644
Binary files 
a/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_sum_through_join.out
 and 
b/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_sum_through_join.out
 differ
diff --git 
a/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_sum_through_join_one_side.out
 
b/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_sum_through_join_one_side.out
index 7b55ac7b08b..363d0153813 100644
Binary files 
a/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_sum_through_join_one_side.out
 and 
b/regression-test/data/nereids_rules_p0/eager_aggregate/push_down_sum_through_join_one_side.out
 differ
diff --git 
a/regression-test/data/nereids_rules_p0/filter_push_down/push_filter_through.out
 
b/regression-test/data/nereids_rules_p0/filter_push_down/push_filter_through.out
index cd1b295f525..27ad895837c 100644
Binary files 
a/regression-test/data/nereids_rules_p0/filter_push_down/push_filter_through.out
 and 
b/regression-test/data/nereids_rules_p0/filter_push_down/push_filter_through.out
 differ
diff --git 
a/regression-test/data/nereids_tpcds_shape_sf1000_p0/bs_downgrade_shape/query54.out
 
b/regression-test/data/nereids_tpcds_shape_sf1000_p0/bs_downgrade_shape/query54.out
index 2d25f4d29ff..3e77fcc2aa7 100644
Binary files 
a/regression-test/data/nereids_tpcds_shape_sf1000_p0/bs_downgrade_shape/query54.out
 and 
b/regression-test/data/nereids_tpcds_shape_sf1000_p0/bs_downgrade_shape/query54.out
 differ
diff --git 
a/regression-test/data/nereids_tpcds_shape_sf1000_p0/shape/query54.out 
b/regression-test/data/nereids_tpcds_shape_sf1000_p0/shape/query54.out
index 2d25f4d29ff..3e77fcc2aa7 100644
Binary files 
a/regression-test/data/nereids_tpcds_shape_sf1000_p0/shape/query54.out and 
b/regression-test/data/nereids_tpcds_shape_sf1000_p0/shape/query54.out differ
diff --git 
a/regression-test/data/nereids_tpcds_shape_sf100_p0/noStatsRfPrune/query54.out 
b/regression-test/data/nereids_tpcds_shape_sf100_p0/noStatsRfPrune/query54.out
index 58aaeff3829..e98895a4cb7 100644
Binary files 
a/regression-test/data/nereids_tpcds_shape_sf100_p0/noStatsRfPrune/query54.out 
and 
b/regression-test/data/nereids_tpcds_shape_sf100_p0/noStatsRfPrune/query54.out 
differ
diff --git 
a/regression-test/data/nereids_tpcds_shape_sf100_p0/no_stats_shape/query54.out 
b/regression-test/data/nereids_tpcds_shape_sf100_p0/no_stats_shape/query54.out
index 9db6017459c..aea5944d30e 100644
Binary files 
a/regression-test/data/nereids_tpcds_shape_sf100_p0/no_stats_shape/query54.out 
and 
b/regression-test/data/nereids_tpcds_shape_sf100_p0/no_stats_shape/query54.out 
differ
diff --git 
a/regression-test/data/nereids_tpcds_shape_sf100_p0/rf_prune/query54.out 
b/regression-test/data/nereids_tpcds_shape_sf100_p0/rf_prune/query54.out
index b4baf3b35d9..6d90d6ea753 100644
Binary files 
a/regression-test/data/nereids_tpcds_shape_sf100_p0/rf_prune/query54.out and 
b/regression-test/data/nereids_tpcds_shape_sf100_p0/rf_prune/query54.out differ
diff --git 
a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query54.out 
b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query54.out
index ec260fc303a..61c8aaee058 100644
Binary files 
a/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query54.out and 
b/regression-test/data/nereids_tpcds_shape_sf100_p0/shape/query54.out differ
diff --git 
a/regression-test/suites/nereids_p0/subquery/test_duplicate_name_in_view.groovy 
b/regression-test/suites/nereids_p0/subquery/test_duplicate_name_in_view.groovy
index c5d2c219527..775636aba17 100644
--- 
a/regression-test/suites/nereids_p0/subquery/test_duplicate_name_in_view.groovy
+++ 
b/regression-test/suites/nereids_p0/subquery/test_duplicate_name_in_view.groovy
@@ -77,7 +77,7 @@ suite("test_duplicate_name_in_view") {
                         issue_19611_t1.c0 + 1
                     FROM issue_19611_t1 ) tmp;
         """
-        exception "Duplicated inline view column alias: '(c0 + 1)' in inline 
view: 'tmp'"
+        exception "Duplicated inline view column alias: 'issue_19611_t1.c0 + 
1' in inline view: 'tmp'"
     }
 
     test {
diff --git a/regression-test/suites/nereids_syntax_p0/select_const.groovy 
b/regression-test/suites/nereids_syntax_p0/select_const.groovy
index e802fbcca78..d65e9cb6ac2 100644
--- a/regression-test/suites/nereids_syntax_p0/select_const.groovy
+++ b/regression-test/suites/nereids_syntax_p0/select_const.groovy
@@ -49,4 +49,6 @@ suite("select_with_const") {
     """
 
     sql "select all 1"
+
+    sql "select `1  + 2` from (select 1  + 2) t"
 }


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

Reply via email to