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

jianglongtao 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 7d92ddd  `Drop sharding binding table rules` syntax ignore the effect 
of sharding table order on drop binding rule (#15735)
7d92ddd is described below

commit 7d92ddd8531f28576d2546a6361702d8e4f22efd
Author: lanchengx <[email protected]>
AuthorDate: Wed Mar 2 14:54:41 2022 +0800

    `Drop sharding binding table rules` syntax ignore the effect of sharding 
table order on drop binding rule (#15735)
    
    * `Drop sharding binding table rules` supports dropping specified rules 
when the order of sharding tables is inconsistent
    
    * Rename  method.
---
 ...opShardingBindingTableRuleStatementUpdater.java | 52 +++++++++++++++++-----
 ...ardingBindingTableRuleStatementUpdaterTest.java | 19 +++++++-
 2 files changed, 59 insertions(+), 12 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingBindingTableRuleStatementUpdater.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingBindingTableRuleStatementUpdater.java
index dd69122..143552f 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingBindingTableRuleStatementUpdater.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingBindingTableRuleStatementUpdater.java
@@ -17,47 +17,79 @@
 
 package org.apache.shardingsphere.sharding.distsql.handler.update;
 
+import com.google.common.base.Splitter;
+import com.google.common.base.Strings;
 import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
 import 
org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
 import 
org.apache.shardingsphere.infra.distsql.update.RuleDefinitionDropUpdater;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import 
org.apache.shardingsphere.sharding.distsql.parser.segment.BindingTableRuleSegment;
 import 
org.apache.shardingsphere.sharding.distsql.parser.statement.DropShardingBindingTableRulesStatement;
 
+import java.util.Arrays;
 import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
 import java.util.LinkedList;
-import java.util.stream.Collectors;
+import java.util.Map;
+import java.util.Optional;
 
 /**
  * Drop sharding binding table rule statement updater.
  */
 public final class DropShardingBindingTableRuleStatementUpdater implements 
RuleDefinitionDropUpdater<DropShardingBindingTableRulesStatement, 
ShardingRuleConfiguration> {
     
+    private Map<String, String> bindingTableRules;
+    
     @Override
     public void checkSQLStatement(final ShardingSphereMetaData 
shardingSphereMetaData, final DropShardingBindingTableRulesStatement 
sqlStatement,
                                   final ShardingRuleConfiguration 
currentRuleConfig) throws DistSQLException {
         String schemaName = shardingSphereMetaData.getName();
         checkCurrentRuleConfiguration(schemaName, currentRuleConfig);
-        checkBindingTableRuleExist(schemaName, sqlStatement, 
currentRuleConfig);
+        bindingTableRules = buildBindingTableRule(currentRuleConfig);
+        checkBindingTableRuleExist(schemaName, sqlStatement, 
bindingTableRules);
     }
     
     private void checkCurrentRuleConfiguration(final String schemaName, final 
ShardingRuleConfiguration currentRuleConfig) throws DistSQLException {
         DistSQLException.predictionThrow(null != currentRuleConfig && 
!currentRuleConfig.getBindingTableGroups().isEmpty(), new 
RequiredRuleMissedException("Binding", schemaName));
     }
     
+    private Map<String, String> buildBindingTableRule(final 
ShardingRuleConfiguration configuration) {
+        Map<String, String> result = new LinkedHashMap<>();
+        configuration.getBindingTableGroups().forEach(each -> 
Arrays.stream(each.split(",")).forEach(each1 -> result.put(each1, each)));
+        return result;
+    }
+    
     private void checkBindingTableRuleExist(final String schemaName, final 
DropShardingBindingTableRulesStatement sqlStatement,
-                                            final ShardingRuleConfiguration 
currentRuleConfig) throws DistSQLException {
-        Collection<String> bindingTableGroups = 
currentRuleConfig.getBindingTableGroups();
-        Collection<String> notExistBindingGroup = 
sqlStatement.getBindingGroups().stream().filter(each -> 
!bindingTableGroups.contains(each)).collect(Collectors.toCollection(LinkedList::new));
-        DistSQLException.predictionThrow(notExistBindingGroup.isEmpty(), new 
RequiredRuleMissedException("Binding", schemaName, notExistBindingGroup));
+                                            final Map<String, String> 
bindingRelationship) throws DistSQLException {
+        Collection<String> notExistBindingGroups = new LinkedList<>();
+        for (BindingTableRuleSegment each : sqlStatement.getRules()) {
+            if (!isToBeDroppedRuleExists(each, bindingRelationship)) {
+                notExistBindingGroups.add(each.getTableGroups());
+            }
+        }
+        DistSQLException.predictionThrow(notExistBindingGroups.isEmpty(), new 
RequiredRuleMissedException("Binding", schemaName, notExistBindingGroups));
+    }
+    
+    private boolean isToBeDroppedRuleExists(final BindingTableRuleSegment 
bindingRule, final Map<String, String> bindingRelationship) {
+        Optional<String> anyTableInToBeAlteredRule = 
bindingRule.getBindingTables().stream().findAny();
+        if (anyTableInToBeAlteredRule.isPresent()) {
+            String currentBindingRule = 
bindingRelationship.get(anyTableInToBeAlteredRule.get());
+            if (!Strings.isNullOrEmpty(currentBindingRule)) {
+                Collection<String> currentBindingTables = 
Splitter.on(",").trimResults().splitToList(currentBindingRule);
+                return 
bindingRule.getBindingTables().containsAll(currentBindingTables);
+            }
+        }
+        return false;
     }
     
     @Override
     public boolean updateCurrentRuleConfiguration(final 
DropShardingBindingTableRulesStatement sqlStatement, final 
ShardingRuleConfiguration currentRuleConfig) {
-        if (sqlStatement.getRules().isEmpty()) {
-            currentRuleConfig.getBindingTableGroups().clear();
-        } else {
-            
currentRuleConfig.getBindingTableGroups().removeIf(sqlStatement.getBindingGroups()::contains);
+        currentRuleConfig.getBindingTableGroups().clear();
+        if (!sqlStatement.getRules().isEmpty()) {
+            sqlStatement.getRules().forEach(each -> 
each.getBindingTables().forEach(each1 -> bindingTableRules.remove(each1)));
+            currentRuleConfig.getBindingTableGroups().addAll(new 
LinkedHashSet<>(bindingTableRules.values()));
         }
         return false;
     }
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingBindingTableRuleStatementUpdaterTest.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingBindingTableRuleStatementUpdaterTest.java
index 75a7aac..79ac8b9 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingBindingTableRuleStatementUpdaterTest.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingBindingTableRuleStatementUpdaterTest.java
@@ -36,6 +36,7 @@ import java.util.LinkedList;
 import java.util.stream.Collectors;
 
 import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
@@ -71,14 +72,28 @@ public final class 
DropShardingBindingTableRuleStatementUpdaterTest {
     }
     
     @Test
-    public void assertDropSpecifiedCurrentRuleConfiguration() {
+    public void assertDropSpecifiedCurrentRuleConfiguration() throws 
DistSQLException {
         ShardingRuleConfiguration currentRuleConfig = 
createCurrentRuleConfiguration();
         currentRuleConfig.getBindingTableGroups().add("t_1,t_2");
-        updater.updateCurrentRuleConfiguration(createSQLStatement("t_1,t_2"), 
currentRuleConfig);
+        DropShardingBindingTableRulesStatement sqlStatement = 
createSQLStatement("t_1,t_2");
+        updater.checkSQLStatement(shardingSphereMetaData, sqlStatement, 
currentRuleConfig);
+        updater.updateCurrentRuleConfiguration(sqlStatement, 
currentRuleConfig);
         assertThat(currentRuleConfig.getBindingTableGroups().size(), is(1));
         
assertTrue(currentRuleConfig.getBindingTableGroups().contains("t_order,t_order_item"));
     }
     
+    @Test
+    public void assertDropWrongOrderRulesCurrentRuleConfiguration() throws 
DistSQLException {
+        ShardingRuleConfiguration currentRuleConfig = 
createCurrentRuleConfiguration();
+        currentRuleConfig.getBindingTableGroups().add("t_1,t_2,t_3");
+        DropShardingBindingTableRulesStatement sqlStatement = 
createSQLStatement("t_3,t_2,t_1");
+        updater.checkSQLStatement(shardingSphereMetaData, sqlStatement, 
currentRuleConfig);
+        updater.updateCurrentRuleConfiguration(sqlStatement, 
currentRuleConfig);
+        assertThat(currentRuleConfig.getBindingTableGroups().size(), is(1));
+        
assertTrue(currentRuleConfig.getBindingTableGroups().contains("t_order,t_order_item"));
+        
assertFalse(currentRuleConfig.getBindingTableGroups().contains("t_1,t_2,t_3"));
+    }
+    
     private DropShardingBindingTableRulesStatement createSQLStatement(final 
String... group) {
         LinkedList<BindingTableRuleSegment> segments = 
Arrays.stream(group).map(BindingTableRuleSegment::new).collect(Collectors.toCollection(LinkedList::new));
         return new DropShardingBindingTableRulesStatement(segments);

Reply via email to