Skip to content

Commit

Permalink
[Fix](nereids) fix bind expression compare dbname ignore cluster (#39114
Browse files Browse the repository at this point in the history
)

This pr is similar with #23008, ignoring cluster_name in binding when
compare dbname. e.g. in this sql, the "dbname" should be viewed same db
when comparing with "default_cluster:dbname"
```sql
select dbname.test_db_name_ignore_cluster.a from `default_cluster:dbname`.test_db_name_ignore_cluster; 
```
  • Loading branch information
feiniaofeiafei authored and dataroaring committed Aug 16, 2024
1 parent c6be1b9 commit 86ccbff
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -674,10 +674,10 @@ private BoundStar bindQualifiedStar(List<String> qualifierStar, List<Slot> bound
case 1: // bound slot is `table`.`column`
return false;
case 2:// bound slot is `db`.`table`.`column`
return compareDbName(qualifierStar.get(0), boundSlotQualifier.get(0))
return compareDbNameIgnoreClusterName(qualifierStar.get(0), boundSlotQualifier.get(0))
&& qualifierStar.get(1).equalsIgnoreCase(boundSlotQualifier.get(1));
case 3:// bound slot is `catalog`.`db`.`table`.`column`
return compareDbName(qualifierStar.get(0), boundSlotQualifier.get(1))
return compareDbNameIgnoreClusterName(qualifierStar.get(0), boundSlotQualifier.get(1))
&& qualifierStar.get(1).equalsIgnoreCase(boundSlotQualifier.get(2));
default:
throw new AnalysisException("Not supported qualifier: "
Expand All @@ -693,7 +693,7 @@ private BoundStar bindQualifiedStar(List<String> qualifierStar, List<Slot> bound
return false;
case 3:// bound slot is `catalog`.`db`.`table`.`column`
return qualifierStar.get(0).equalsIgnoreCase(boundSlotQualifier.get(0))
&& compareDbName(qualifierStar.get(1), boundSlotQualifier.get(1))
&& compareDbNameIgnoreClusterName(qualifierStar.get(1), boundSlotQualifier.get(1))
&& qualifierStar.get(2).equalsIgnoreCase(boundSlotQualifier.get(2));
default:
throw new AnalysisException("Not supported qualifier: "
Expand Down Expand Up @@ -861,7 +861,7 @@ private List<Slot> bindSingleSlotByDb(String db, String table, String name, Scop
List<String> boundSlotQualifier = boundSlot.getQualifier();
String boundSlotDb = boundSlotQualifier.get(boundSlotQualifier.size() - 2);
String boundSlotTable = boundSlotQualifier.get(boundSlotQualifier.size() - 1);
if (!compareDbName(boundSlotDb, db) || !sameTableName(boundSlotTable, table)) {
if (!compareDbNameIgnoreClusterName(boundSlotDb, db) || !sameTableName(boundSlotTable, table)) {
continue;
}
// set sql case as alias
Expand All @@ -882,7 +882,7 @@ private List<Slot> bindSingleSlotByCatalog(String catalog, String db, String tab
String boundSlotDb = boundSlotQualifier.get(boundSlotQualifier.size() - 2);
String boundSlotTable = boundSlotQualifier.get(boundSlotQualifier.size() - 1);
if (!boundSlotCatalog.equalsIgnoreCase(catalog)
|| !compareDbName(boundSlotDb, db)
|| !compareDbNameIgnoreClusterName(boundSlotDb, db)
|| !sameTableName(boundSlotTable, table)) {
continue;
}
Expand All @@ -891,4 +891,22 @@ private List<Slot> bindSingleSlotByCatalog(String catalog, String db, String tab
}
return usedSlots.build();
}

/**compareDbNameIgnoreClusterName.*/
public static boolean compareDbNameIgnoreClusterName(String name1, String name2) {
if (name1.equalsIgnoreCase(name2)) {
return true;
}
String ignoreClusterName1 = name1;
int idx1 = name1.indexOf(":");
if (idx1 > -1) {
ignoreClusterName1 = name1.substring(idx1 + 1);
}
String ignoreClusterName2 = name2;
int idx2 = name2.indexOf(":");
if (idx2 > -1) {
ignoreClusterName2 = name2.substring(idx2 + 1);
}
return ignoreClusterName1.equalsIgnoreCase(ignoreClusterName2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ private void checkAssignmentColumn(ConnectContext ctx, List<String> columnNamePa
}
List<String> tableQualifier = RelationUtil.getQualifierName(ctx, nameParts);
if (!ExpressionAnalyzer.sameTableName(tableAlias == null ? tableQualifier.get(2) : tableAlias, tableName)
|| (dbName != null && ExpressionAnalyzer.compareDbName(tableQualifier.get(1), dbName))) {
|| (dbName != null
&& !ExpressionAnalyzer.compareDbNameIgnoreClusterName(tableQualifier.get(1), dbName))) {
throw new AnalysisException("column in assignment list is invalid, " + String.join(".", columnNameParts));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !test_update --
2 10

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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("test_db_name_ignore_cluster") {
String db = context.config.getDbNameByFile(new File(context.file.parent))
sql 'set enable_nereids_planner=true'
sql 'set enable_nereids_distribute_planner=false'
sql "use ${db}"
sql "drop table if exists ${db}.test_db_name_ignore_cluster"
sql """create table ${db}.test_db_name_ignore_cluster(a int, b int) unique key(a) distributed by hash(a)
properties("replication_num"="1");"""
sql "select ${db}.test_db_name_ignore_cluster.a from `default_cluster:${db}`.test_db_name_ignore_cluster;"
sql "select ${db}.test_db_name_ignore_cluster.* from `default_cluster:${db}`.test_db_name_ignore_cluster;"

sql "select `default_cluster:${db}`.test_db_name_ignore_cluster.a from `${db}`.test_db_name_ignore_cluster;"
sql "select `default_cluster:${db}`.test_db_name_ignore_cluster.* from `${db}`.test_db_name_ignore_cluster;"

sql "select `default_cluster:${db}`.test_db_name_ignore_cluster.a from `default_cluster:${db}`.test_db_name_ignore_cluster;"
sql "select `default_cluster:${db}`.test_db_name_ignore_cluster.* from `default_cluster:${db}`.test_db_name_ignore_cluster;"

sql "select internal.`${db}`.test_db_name_ignore_cluster.a from internal.`default_cluster:${db}`.test_db_name_ignore_cluster;"
sql "select internal.`default_cluster:${db}`.test_db_name_ignore_cluster.* from internal.`${db}`.test_db_name_ignore_cluster;"

sql "insert into ${db}.test_db_name_ignore_cluster values(2,4)"
sql "update `default_cluster:${db}`.test_db_name_ignore_cluster set `${db}`.test_db_name_ignore_cluster.b=10;"
sql "update `${db}`.test_db_name_ignore_cluster set `${db}`.test_db_name_ignore_cluster.b=10;"
qt_test_update "select * from ${db}.test_db_name_ignore_cluster"
}

0 comments on commit 86ccbff

Please sign in to comment.