You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2024/01/22 11:01:24 UTC

(shardingsphere) branch master updated: Fix drop sharding table exception when table name is uppercase (#29803)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 92dc6d92d2d Fix drop sharding table exception when table name is uppercase (#29803)
92dc6d92d2d is described below

commit 92dc6d92d2d0aaf349b3c8a64c63eaa3b630ec38
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Mon Jan 22 19:01:14 2024 +0800

    Fix drop sharding table exception when table name is uppercase (#29803)
---
 .../impl/ShardingDropTableStatementValidator.java  | 22 +++++++++++++---------
 .../context/segment/table/TablesContext.java       |  1 -
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java
index daddee3f9fa..b0823bb795a 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java
@@ -17,10 +17,12 @@
 
 package org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl;
 
+import com.cedarsoftware.util.CaseInsensitiveSet;
 import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
 import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
 import org.apache.shardingsphere.infra.hint.HintValueContext;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
@@ -66,7 +68,7 @@ public final class ShardingDropTableStatementValidator extends ShardingDDLStatem
     public void postValidate(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final HintValueContext hintValueContext, final List<Object> params,
                              final ShardingSphereDatabase database, final ConfigurationProperties props, final RouteContext routeContext) {
         DropTableStatement dropTableStatement = (DropTableStatement) sqlStatementContext.getSqlStatement();
-        checkTableInUsed(shardingRule, dropTableStatement, routeContext);
+        checkTableInUsed(shardingRule, sqlStatementContext, routeContext);
         for (SimpleTableSegment each : dropTableStatement.getTables()) {
             if (isRouteUnitDataNodeDifferentSize(shardingRule, routeContext, each.getTableName().getIdentifier().getValue())) {
                 throw new ShardingDDLRouteException("DROP", "TABLE", sqlStatementContext.getTablesContext().getTableNames());
@@ -74,19 +76,21 @@ public final class ShardingDropTableStatementValidator extends ShardingDDLStatem
         }
     }
     
-    private void checkTableInUsed(final ShardingRule shardingRule, final DropTableStatement sqlStatement, final RouteContext routeContext) {
+    private void checkTableInUsed(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final RouteContext routeContext) {
         Collection<String> inUsedTables = new LinkedList<>();
-        Set<String> dropTables = sqlStatement.getTables().stream().map(each -> each.getTableName().getIdentifier().getValue()).collect(Collectors.toSet());
+        Collection<String> dropTables = sqlStatementContext.getTablesContext().getTableNames();
         Set<String> actualTables = routeContext.getRouteUnits().stream().flatMap(each -> each.getTableMappers().stream().map(RouteMapper::getActualName)).collect(Collectors.toSet());
-        Collection<String> tableMeta = shardingRule.getTableRules().values().stream().filter(each -> !dropTables.contains(each.getLogicTable()))
-                .flatMap(each -> each.getActualDataNodes().stream().map(DataNode::getTableName)).collect(Collectors.toSet());
+        // TODO check actual tables not be used in multi rules, and remove this check logic
+        Collection<String> otherRuleActualTables = shardingRule.getTableRules().values().stream().filter(each -> !dropTables.contains(each.getLogicTable()))
+                .flatMap(each -> each.getActualDataNodes().stream().map(DataNode::getTableName)).collect(Collectors.toCollection(CaseInsensitiveSet::new));
+        if (otherRuleActualTables.isEmpty()) {
+            return;
+        }
         for (String each : actualTables) {
-            if (tableMeta.contains(each)) {
+            if (otherRuleActualTables.contains(each)) {
                 inUsedTables.add(each);
             }
         }
-        if (!inUsedTables.isEmpty()) {
-            throw new DropInUsedTablesException(inUsedTables);
-        }
+        ShardingSpherePreconditions.checkState(inUsedTables.isEmpty(), () -> new DropInUsedTablesException(inUsedTables));
     }
 }
diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java
index 7f93fb85372..20102712678 100644
--- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java
+++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java
@@ -56,7 +56,6 @@ public final class TablesContext {
     
     private final Collection<SimpleTableSegment> simpleTableSegments = new LinkedList<>();
     
-    @Getter
     private final Collection<String> tableNames = new CaseInsensitiveSet<>();
     
     private final Collection<String> schemaNames = new CaseInsensitiveSet<>();