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

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


The following commit(s) were added to refs/heads/master by this push:
     new 735cd15a3d [fix](nereids) PushdownAliasThroughJoin should handle same 
column with different alias in project list (#18470)
735cd15a3d is described below

commit 735cd15a3d45af9b8ffeae1b6a00d5d3c4a7b66f
Author: starocean999 <[email protected]>
AuthorDate: Mon Apr 10 11:50:37 2023 +0800

    [fix](nereids) PushdownAliasThroughJoin should handle same column with 
different alias in project list (#18470)
---
 .../rewrite/logical/PushdownAliasThroughJoin.java  | 49 +++++++++++--------
 .../data/nereids_syntax_p0/join_with_alias.out     |  4 ++
 .../nereids_syntax_p0/join_with_alias.groovy       | 55 ++++++++++++++++++++++
 3 files changed, 89 insertions(+), 19 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownAliasThroughJoin.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownAliasThroughJoin.java
index e35493797e..04811beeca 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownAliasThroughJoin.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownAliasThroughJoin.java
@@ -20,7 +20,6 @@ package org.apache.doris.nereids.rules.rewrite.logical;
 import org.apache.doris.nereids.rules.Rule;
 import org.apache.doris.nereids.rules.RuleType;
 import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory;
-import org.apache.doris.nereids.trees.UnaryNode;
 import org.apache.doris.nereids.trees.expressions.Alias;
 import org.apache.doris.nereids.trees.expressions.ExprId;
 import org.apache.doris.nereids.trees.expressions.Expression;
@@ -31,6 +30,8 @@ import 
org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
 import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
 
 import java.util.List;
 import java.util.Map;
@@ -58,10 +59,18 @@ public class PushdownAliasThroughJoin extends 
OneRewriteRuleFactory {
             .when(this::isAllSlotOrAliasSlot)
             .then(project -> {
                 LogicalJoin<? extends Plan, ? extends Plan> join = 
project.child();
-                // aliasMap { Slot -> Alias<Slot> }
-                Map<Expression, NamedExpression> aliasMap = 
project.getProjects().stream()
+                // aliasMap { Slot -> List<Alias<Slot>> }
+                Map<Expression, List<NamedExpression>> aliasMap = 
Maps.newHashMap();
+                project.getProjects().stream()
                         .filter(expr -> expr instanceof Alias && ((Alias) 
expr).child() instanceof Slot)
-                        .map(expr -> (Alias) 
expr).collect(Collectors.toMap(UnaryNode::child, expr -> expr));
+                        .forEach(expr -> {
+                            List<NamedExpression> aliases = 
aliasMap.get(((Alias) expr).child());
+                            if (aliases == null) {
+                                aliases = Lists.newArrayList();
+                                aliasMap.put(((Alias) expr).child(), aliases);
+                            }
+                            aliases.add(expr);
+                        });
                 if (aliasMap.isEmpty()) {
                     return null;
                 }
@@ -69,21 +78,9 @@ public class PushdownAliasThroughJoin extends 
OneRewriteRuleFactory {
                         .collect(Collectors.toList());
 
                 List<Slot> leftOutput = join.left().getOutput();
-                List<NamedExpression> leftProjects = 
leftOutput.stream().map(slot -> {
-                    NamedExpression alias = aliasMap.get(slot);
-                    if (alias != null) {
-                        return alias;
-                    }
-                    return slot;
-                }).collect(Collectors.toList());
+                List<NamedExpression> leftProjects = 
createNewOutput(leftOutput, aliasMap);
                 List<Slot> rightOutput = join.right().getOutput();
-                List<NamedExpression> rightProjects = 
rightOutput.stream().map(slot -> {
-                    NamedExpression alias = aliasMap.get(slot);
-                    if (alias != null) {
-                        return alias;
-                    }
-                    return slot;
-                }).collect(Collectors.toList());
+                List<NamedExpression> rightProjects = 
createNewOutput(rightOutput, aliasMap);
 
                 Plan left;
                 Plan right;
@@ -103,7 +100,7 @@ public class PushdownAliasThroughJoin extends 
OneRewriteRuleFactory {
                 // join aid = b.id -- project a.id as aid
                 Map<ExprId, Slot> replaceMap = 
aliasMap.entrySet().stream().collect(
                         Collectors.toMap(entry -> ((Slot) 
entry.getKey()).getExprId(),
-                                entry -> entry.getValue().toSlot()));
+                                entry -> entry.getValue().get(0).toSlot()));
 
                 List<Expression> newHash = 
replaceJoinConjuncts(join.getHashJoinConjuncts(), replaceMap);
                 List<Expression> newOther = 
replaceJoinConjuncts(join.getOtherJoinConjuncts(), replaceMap);
@@ -122,4 +119,18 @@ public class PushdownAliasThroughJoin extends 
OneRewriteRuleFactory {
             }
         })).collect(ImmutableList.toImmutableList());
     }
+
+    private List<NamedExpression> createNewOutput(List<Slot> oldOutput,
+            Map<Expression, List<NamedExpression>> aliasMap) {
+        List<NamedExpression> output = Lists.newArrayList();
+        oldOutput.stream().forEach(slot -> {
+            List<NamedExpression> alias = aliasMap.get(slot);
+            if (alias != null) {
+                output.addAll(alias);
+            } else {
+                output.add(slot);
+            }
+        });
+        return output;
+    }
 }
diff --git a/regression-test/data/nereids_syntax_p0/join_with_alias.out 
b/regression-test/data/nereids_syntax_p0/join_with_alias.out
new file mode 100644
index 0000000000..a4cca003b9
--- /dev/null
+++ b/regression-test/data/nereids_syntax_p0/join_with_alias.out
@@ -0,0 +1,4 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !sql --
+1      1
+
diff --git a/regression-test/suites/nereids_syntax_p0/join_with_alias.groovy 
b/regression-test/suites/nereids_syntax_p0/join_with_alias.groovy
new file mode 100644
index 0000000000..408da58d21
--- /dev/null
+++ b/regression-test/suites/nereids_syntax_p0/join_with_alias.groovy
@@ -0,0 +1,55 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+suite("join_with_alias") {
+    sql 'set enable_nereids_planner=true'
+    sql 'set enable_fallback_to_original_planner=false'
+
+    sql """ drop table if exists table_A_alias;"""
+    sql """
+        create table table_A_alias ( a bigint not null )
+        ENGINE=OLAP
+        DISTRIBUTED BY HASH(a) BUCKETS 1
+        PROPERTIES (
+        "replication_allocation" = "tag.location.default: 1",
+        "in_memory" = "false",
+        "storage_format" = "V2"
+        );
+    """
+    sql """ drop table if exists table_B_alias;"""
+    sql """
+        create table table_B_alias ( b int not null )
+        ENGINE=OLAP
+        DISTRIBUTED BY HASH(b) BUCKETS 1
+        PROPERTIES (
+        "replication_allocation" = "tag.location.default: 1",
+        "in_memory" = "false",
+        "storage_format" = "V2"
+        );
+    """
+
+    sql """insert into table_A_alias values( 1 );"""
+    sql """insert into table_B_alias values( 1 );"""
+
+    qt_sql"""
+            select a1, a2 from ( select a as a1, a as a2 from table_A_alias 
join table_B_alias on a = b ) t;
+        """
+
+    sql """ drop table if exists table_A_alias;"""
+
+    sql """ drop table if exists table_B_alias;"""
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to