You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/11/12 05:50:53 UTC

[shardingsphere] branch master updated: fix wrong metadata when actual table node case insensitive (#13565)

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

panjuan 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 fa2b03a  fix wrong metadata when actual table node case insensitive (#13565)
fa2b03a is described below

commit fa2b03a039c407d580a50f193b4b46b9cfba06bd
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Fri Nov 12 13:49:59 2021 +0800

    fix wrong metadata when actual table node case insensitive (#13565)
---
 .../shardingsphere/sharding/rule/TableRule.java    |  5 +-
 .../singletable/rule/SingleTableRule.java          |  4 +-
 .../singletable/rule/SingleTableRuleTest.java      | 74 ++++++++++++++++++++++
 3 files changed, 80 insertions(+), 3 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/TableRule.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/TableRule.java
index 743e795..5601fcc 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/TableRule.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/TableRule.java
@@ -25,13 +25,13 @@ import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurat
 import org.apache.shardingsphere.infra.datanode.DataNode;
 import org.apache.shardingsphere.infra.datanode.DataNodeUtil;
 import org.apache.shardingsphere.infra.exception.ShardingSphereException;
-import org.apache.shardingsphere.sharding.support.InlineExpressionParser;
 import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
 import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
 import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
 import org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration;
 import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
 import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
+import org.apache.shardingsphere.sharding.support.InlineExpressionParser;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -43,6 +43,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import java.util.TreeSet;
 import java.util.stream.Collectors;
 
 /**
@@ -137,7 +138,7 @@ public final class TableRule {
     }
     
     private Set<String> getActualTables() {
-        return actualDataNodes.stream().map(DataNode::getTableName).collect(Collectors.toSet());
+        return actualDataNodes.stream().map(DataNode::getTableName).collect(Collectors.toCollection(() -> new TreeSet<>(String.CASE_INSENSITIVE_ORDER)));
     }
     
     private void addActualTable(final String datasourceName, final String tableName) {
diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java
index c3d81fb..d9fd557 100644
--- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java
+++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java
@@ -40,6 +40,7 @@ import java.util.Map.Entry;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.Set;
+import java.util.TreeSet;
 import java.util.stream.Collectors;
 
 /**
@@ -135,7 +136,8 @@ public final class SingleTableRule implements SchemaRule, DataNodeContainedRule,
     }
     
     private Collection<String> getExcludedTables(final Collection<ShardingSphereRule> rules) {
-        return rules.stream().filter(each -> each instanceof DataNodeContainedRule).flatMap(each -> ((DataNodeContainedRule) each).getAllTables().stream()).collect(Collectors.toSet());
+        return rules.stream().filter(each -> each instanceof DataNodeContainedRule).flatMap(each 
+            -> ((DataNodeContainedRule) each).getAllTables().stream()).collect(Collectors.toCollection(() -> new TreeSet<>(String.CASE_INSENSITIVE_ORDER)));
     }
     
     @Override
diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/SingleTableRuleTest.java b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/SingleTableRuleTest.java
index dced351..8b7210f 100644
--- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/SingleTableRuleTest.java
+++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/SingleTableRuleTest.java
@@ -19,20 +19,94 @@ package org.apache.shardingsphere.singletable.rule;
 
 import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
+import org.junit.Before;
 import org.junit.Test;
 
+import javax.sql.DataSource;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.Properties;
+import java.util.stream.Collectors;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public final class SingleTableRuleTest {
     
+    private static final String TABLE_TYPE = "TABLE";
+    
+    private static final String VIEW_TYPE = "VIEW";
+    
+    private static final String TABLE_NAME = "TABLE_NAME";
+    
+    private Map<String, DataSource> dataSourceMap;
+    
+    @Before
+    public void setUp() throws SQLException {
+        dataSourceMap = new LinkedHashMap<>(2, 1);
+        dataSourceMap.put("ds_0", mockDataSource("ds_0", Arrays.asList("employee", "t_order_0", "t_order_1")));
+        dataSourceMap.put("ds_1", mockDataSource("ds_1", Arrays.asList("student", "t_order_0", "t_order_1")));
+    }
+    
+    private DataSource mockDataSource(final String dataSourceName, final List<String> tableNames) throws SQLException {
+        DataSource result = mock(DataSource.class, RETURNS_DEEP_STUBS);
+        when(result.getConnection().getCatalog()).thenReturn(dataSourceName);
+        ResultSet resultSet = mockResultSet(tableNames);
+        when(result.getConnection().getMetaData().getTables(dataSourceName, null, null, new String[]{TABLE_TYPE, VIEW_TYPE})).thenReturn(resultSet);
+        return result;
+    }
+    
+    private ResultSet mockResultSet(final List<String> tableNames) throws SQLException {
+        ResultSet result = mock(ResultSet.class);
+        Collection<Boolean> nextResults = tableNames.stream().map(each -> true).collect(Collectors.toList());
+        nextResults.add(false);
+        when(result.next()).thenReturn(true, nextResults.toArray(new Boolean[tableNames.size()]));
+        String firstTableName = tableNames.get(0);
+        String[] nextTableNames = tableNames.subList(1, tableNames.size()).toArray(new String[tableNames.size() - 1]);
+        when(result.getString(TABLE_NAME)).thenReturn(firstTableName, nextTableNames);
+        return result;
+    }
+    
     @Test
     public void assertGetRuleType() {
         SingleTableRule singleTableRule = new SingleTableRule(mock(DatabaseType.class), Collections.emptyMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
         assertThat(singleTableRule.getType(), is(SingleTableRule.class.getSimpleName()));
     }
+    
+    @Test
+    public void assertGetSingleTableDataNodes() {
+        DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
+        when(dataNodeContainedRule.getAllTables()).thenReturn(Arrays.asList("t_order", "t_order_0", "t_order_1"));
+        SingleTableRule singleTableRule = new SingleTableRule(mock(DatabaseType.class), dataSourceMap, 
+                Collections.singletonList(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        Map<String, SingleTableDataNode> actual = singleTableRule.getSingleTableDataNodes();
+        assertThat(actual.size(), is(2));
+        Iterator<SingleTableDataNode> iterator = actual.values().iterator();
+        assertThat(iterator.next().getTableName(), is("employee"));
+        assertThat(iterator.next().getTableName(), is("student"));
+    }
+    
+    @Test
+    public void assertGetSingleTableDataNodesWithUpperCase() {
+        DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
+        when(dataNodeContainedRule.getAllTables()).thenReturn(Arrays.asList("T_ORDER", "T_ORDER_0", "T_ORDER_1"));
+        SingleTableRule singleTableRule = new SingleTableRule(mock(DatabaseType.class), dataSourceMap,
+                Collections.singletonList(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        Map<String, SingleTableDataNode> actual = singleTableRule.getSingleTableDataNodes();
+        assertThat(actual.size(), is(2));
+        Iterator<SingleTableDataNode> iterator = actual.values().iterator();
+        assertThat(iterator.next().getTableName(), is("employee"));
+        assertThat(iterator.next().getTableName(), is("student"));
+    }
 }