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

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

commit e64ab13b45b102b87f7e4fb68b792f181ef6e4ab
Author: LiBinfeng <[email protected]>
AuthorDate: Mon Jun 5 16:10:54 2023 +0800

    [Fix](Nereids) Fix duplicated name in view does not throw exception (#20374)
    
    When using nereids, if we have duplicated name in output of view, we need 
to throw an exception. A check rule was added in bindExpression rule set
---
 .../nereids/rules/analysis/BindExpression.java     | 20 +++++++
 .../trees/plans/ExplainInsertCommandTest.java      |  6 --
 .../subquery/test_duplicate_name_in_view.groovy    | 69 ++++++++++++++++++++++
 3 files changed, 89 insertions(+), 6 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java
index f1bd3802c7..286dbebfc1 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java
@@ -67,6 +67,7 @@ import 
org.apache.doris.nereids.trees.plans.logical.LogicalProject;
 import org.apache.doris.nereids.trees.plans.logical.LogicalRepeat;
 import org.apache.doris.nereids.trees.plans.logical.LogicalSetOperation;
 import org.apache.doris.nereids.trees.plans.logical.LogicalSort;
+import org.apache.doris.nereids.trees.plans.logical.LogicalSubQueryAlias;
 import org.apache.doris.nereids.trees.plans.logical.LogicalTVFRelation;
 import org.apache.doris.nereids.trees.plans.logical.UsingJoin;
 import org.apache.doris.nereids.util.TypeCoercionUtils;
@@ -538,6 +539,13 @@ public class BindExpression implements AnalysisRuleFactory 
{
                     UnboundTVFRelation relation = ctx.root;
                     return bindTableValuedFunction(relation, 
ctx.statementContext);
                 })
+            ),
+            RuleType.BINDING_SUBQUERY_ALIAS_SLOT.build(
+                logicalSubQueryAlias().thenApply(ctx -> {
+                    LogicalSubQueryAlias<Plan> subQueryAlias = ctx.root;
+                    checkSameNameSlot(subQueryAlias.child(0).getOutput(), 
subQueryAlias.getAlias());
+                    return subQueryAlias;
+                })
             )
         ).stream().map(ruleCondition).collect(ImmutableList.toImmutableList());
     }
@@ -677,6 +685,18 @@ public class BindExpression implements AnalysisRuleFactory 
{
         return new LogicalTVFRelation(unboundTVFRelation.getId(), 
(TableValuedFunction) function);
     }
 
+    private void checkSameNameSlot(List<Slot> childOutputs, String 
subQueryAlias) {
+        Set<String> nameSlots = new HashSet<>();
+        for (Slot s : childOutputs) {
+            if (nameSlots.contains(s.getName())) {
+                throw new AnalysisException("Duplicated inline view column 
alias: '" + s.getName()
+                        + "'" + " in inline view: '" + subQueryAlias + "'");
+            } else {
+                nameSlots.add(s.getName());
+            }
+        }
+    }
+
     private BoundFunction bindTableGeneratingFunction(UnboundFunction 
unboundFunction,
             CascadesContext cascadesContext) {
         List<Expression> boundArguments = 
unboundFunction.getArguments().stream()
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/ExplainInsertCommandTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/ExplainInsertCommandTest.java
index 7eda6fae58..d82dd209b4 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/ExplainInsertCommandTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/ExplainInsertCommandTest.java
@@ -104,12 +104,6 @@ public class ExplainInsertCommandTest extends 
TestWithFeService {
 
     }
 
-    @Test
-    public void testInsertIntoDuplicateKeyTableWithCast() throws Exception {
-        String sql = "explain insert into t1 select * from (select cast(k1 as 
varchar), 1, 1, 1 from src) t";
-        Assertions.assertEquals(4, 
getOutputFragment(sql).getOutputExprs().size());
-    }
-
     @Test
     public void testInsertIntoSomeColumns() throws Exception {
         String sql = "explain insert into t1 (v1, v2) select v1 + 1, v2 + 4 
from src";
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
new file mode 100644
index 0000000000..fd5f093a3e
--- /dev/null
+++ 
b/regression-test/suites/nereids_p0/subquery/test_duplicate_name_in_view.groovy
@@ -0,0 +1,69 @@
+// 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("inlineview_with_project") {
+    sql "SET enable_nereids_planner=true"
+    sql "SET enable_fallback_to_original_planner=false"
+    sql """
+        drop table if exists issue_19611_t0;
+    """
+
+    sql """
+        drop table if exists issue_19611_t1;
+    """
+
+    sql """
+        create table issue_19611_t0 (c0 int)
+        ENGINE=OLAP
+        DISTRIBUTED BY HASH(c0) BUCKETS 5
+        PROPERTIES (
+        "replication_allocation" = "tag.location.default: 1",
+        "in_memory" = "false",
+        "storage_format" = "V2"
+        );
+    """
+
+    sql """
+        create table issue_19611_t1 (c0 int)
+        ENGINE=OLAP
+        DISTRIBUTED BY HASH(c0) BUCKETS 5
+        PROPERTIES (
+        "replication_allocation" = "tag.location.default: 1",
+        "in_memory" = "false",
+        "storage_format" = "V2"
+        );
+    """
+
+    test {
+        sql """
+             select * from (
+                select * from issue_19611_t0, issue_19611_t1 where 
issue_19611_t1.c0 != 0 
+                    union select * from issue_19611_t0, issue_19611_t1 where 
issue_19611_t1.c0 = 0) tmp;
+        """
+        exception "errCode = 2, detailMessage = Unexpected exception: 
Duplicated inline view column alias: 'c0' in inline view: 'tmp'"
+
+    }
+
+
+    sql """
+        drop table if exists issue_19611_t0;
+    """
+
+    sql """
+        drop table if exists issue_19611_t1;
+    """
+}


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

Reply via email to