924060929 commented on code in PR #38104:
URL: https://github.com/apache/doris/pull/38104#discussion_r1683728418


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java:
##########
@@ -507,6 +508,31 @@ private LogicalJoin<Plan, Plan> 
bindJoin(MatchingContext<LogicalJoin<Plan, Plan>
         LogicalJoin<Plan, Plan> join = ctx.root;
         CascadesContext cascadesContext = ctx.cascadesContext;
 
+        Set<String> tableNames = new HashSet<>();
+        List<Plan> children = join.children();
+        for (Plan child : children) {
+            // get alias name
+            if (child instanceof LogicalSubQueryAlias) {
+                LogicalSubQueryAlias subQueryAlias = (LogicalSubQueryAlias) 
child;
+                String alias = subQueryAlias.getAlias();
+
+                if (!tableNames.add(alias)) {
+                    throw new AnalysisException("Not unique table/alias: '" + 
alias + "'");
+                }
+                // get table name
+            } else if (child instanceof LogicalFilter) {
+                Plan grandChild = child.children().get(0);
+                if (grandChild instanceof LogicalOlapScan) {

Review Comment:
   you should support other table type, for example, `LogicalFileScan` for hive 
table, so `LogicalRelation` is more suitable



##########
regression-test/suites/nereids_syntax_p0/alias_conflict.groovy:
##########
@@ -0,0 +1,107 @@
+// 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("alias_conflict") {
+
+    sql """ DROP TABLE IF EXISTS `test1` """
+    sql """ DROP TABLE IF EXISTS `test2` """
+    sql """ DROP TABLE IF EXISTS `test3` """
+
+    sql """
+        CREATE TABLE `test1` (

Review Comment:
   `test1` table conflict to `nereids_syntax_p0/test_limit.groovy`, please 
rename to other table name, e.g. `test_alias_tbl1`, `test_alias_tbl2`



##########
regression-test/suites/nereids_syntax_p0/alias_conflict.groovy:
##########
@@ -0,0 +1,107 @@
+// 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("alias_conflict") {
+
+    sql """ DROP TABLE IF EXISTS `test1` """
+    sql """ DROP TABLE IF EXISTS `test2` """
+    sql """ DROP TABLE IF EXISTS `test3` """
+
+    sql """
+        CREATE TABLE `test1` (
+        `id` varchar(64) NULL,
+        `name` varchar(64) NULL,
+        `age` int NULL
+        ) ENGINE=OLAP
+        DUPLICATE KEY(`id`,`name`)
+        COMMENT 'OLAP'
+        DISTRIBUTED BY HASH(`id`,`name`) BUCKETS 4
+        PROPERTIES (
+        "replication_allocation" = "tag.location.default: 1",
+        "in_memory" = "false",
+        "storage_format" = "V2",
+        "disable_auto_compaction" = "false"
+        );
+    """
+
+    sql """
+        CREATE TABLE `test2` (
+        `id` varchar(64) NULL,
+        `name` varchar(64) NULL,
+        `age` int NULL
+        ) ENGINE=OLAP
+        DUPLICATE KEY(`id`,`name`)
+        COMMENT 'OLAP'
+        DISTRIBUTED BY HASH(`id`,`name`) BUCKETS 5
+        PROPERTIES (
+        "replication_allocation" = "tag.location.default: 1",
+        "in_memory" = "false",
+        "storage_format" = "V2",
+        "disable_auto_compaction" = "false"
+        );
+    """
+
+    sql """
+        CREATE TABLE `test3` (
+        `id` varchar(64) NULL,
+        `name` varchar(64) NULL,
+        `age` int NULL
+        ) ENGINE=OLAP
+        DUPLICATE KEY(`id`,`name`)
+        COMMENT 'OLAP'
+        DISTRIBUTED BY HASH(`id`,`name`) BUCKETS 6
+        PROPERTIES (
+        "replication_allocation" = "tag.location.default: 1",
+        "in_memory" = "false",
+        "storage_format" = "V2",
+        "disable_auto_compaction" = "false"
+        );
+    """
+
+    sql """insert into test1 values('1','a',12);"""
+    sql """insert into test2 values('1','a',12);"""
+    sql """insert into test3 values('1','a',12);"""
+
+    // Valid query
+    qt_select_normal """select t3.id from test1 t1 inner join test2 t2 on true 
inner join test3 t3 on t3.id = t2.id;"""
+
+    // Test for alias conflict
+    test {
+        sql "select * from test1 t, test1 t;"
+        exception "Not unique table/alias: 't'"
+    }
+
+    // Test for table name conflict
+    test {
+        sql "select * from test1 t1, test2 t1;"
+        exception "Not unique table/alias: 't1'"
+    }
+
+    // Test for no conflict
+    qt_select_no_conflict """select * from test1 t1, test2 t2 where t1.id = 
t2.id;"""
+
+    // Complex query with alias conflict
+    test {
+        sql "select * from (select * from test1) a, (select * from test1) a;"
+        exception "Not unique table/alias: 'a'"
+    }
+

Review Comment:
   Add more cases
   
   ```groovy
   multi_sql """
       DROP TABLE IF EXISTS test_alias_tbl1;
       DROP TABLE IF EXISTS test_alias_tbl2;
       CREATE TABLE IF NOT EXISTS `test_alias_tbl1 ` (
         `id` varchar(64) NULL
       )
       DISTRIBUTED BY HASH(`id`)
       PROPERTIES (
         "replication_num"="1"
       );
       CREATE TABLE IF NOT EXISTS `test_alias_tbl2 ` (
         `id` varchar(64) NULL
       )
       DISTRIBUTED BY HASH(`id`)
       PROPERTIES (
         "replication_num"="1"
       );
       """
   
   test {
       sql "select * from test_alias_tbl1, test_alias_tbl1 b, test_alias_tbl1 
c, test_alias_tbl1"
       exception "Not unique table/alias: 'test_alias_tbl1'"
   }
   ```



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java:
##########
@@ -507,6 +508,31 @@ private LogicalJoin<Plan, Plan> 
bindJoin(MatchingContext<LogicalJoin<Plan, Plan>
         LogicalJoin<Plan, Plan> join = ctx.root;
         CascadesContext cascadesContext = ctx.cascadesContext;
 
+        Set<String> tableNames = new HashSet<>();
+        List<Plan> children = join.children();
+        for (Plan child : children) {

Review Comment:
   this seems only check one level alias, can you support check conflict names 
between multiple level?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


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

Reply via email to