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 2e475199fcc For #26717, fix load single table and add test (#26718)
2e475199fcc is described below

commit 2e475199fccef62750a6c79c10080586ac151481
Author: Raigor <[email protected]>
AuthorDate: Fri Jun 30 17:17:24 2023 +0800

    For #26717, fix load single table and add test (#26718)
---
 .../update/LoadSingleTableStatementUpdater.java    | 15 +---
 .../LoadSingleTableStatementUpdaterTest.java       | 96 ++++++++++++++++++++++
 2 files changed, 100 insertions(+), 11 deletions(-)

diff --git 
a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java
 
b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java
index 67416422a82..3db71ea83db 100644
--- 
a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java
+++ 
b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdater.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.single.distsql.handler.update;
 
+import 
org.apache.shardingsphere.dialect.exception.syntax.table.TableExistsException;
 import 
org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException;
 import 
org.apache.shardingsphere.distsql.handler.update.RuleDefinitionCreateUpdater;
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
@@ -33,17 +34,13 @@ import 
org.apache.shardingsphere.single.datanode.SingleTableDataNodeLoader;
 import 
org.apache.shardingsphere.single.distsql.handler.exception.MissingRequiredSingleTableException;
 import org.apache.shardingsphere.single.distsql.segment.SingleTableSegment;
 import 
org.apache.shardingsphere.single.distsql.statement.rdl.LoadSingleTableStatement;
-import 
org.apache.shardingsphere.single.exception.InvalidSingleRuleConfigurationException;
-import org.apache.shardingsphere.single.rule.SingleRule;
 import org.apache.shardingsphere.single.util.SingleTableLoadUtils;
 
 import javax.sql.DataSource;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.Map;
-import java.util.Optional;
 import java.util.stream.Collectors;
 
 /**
@@ -54,14 +51,12 @@ public final class LoadSingleTableStatementUpdater 
implements RuleDefinitionCrea
     @Override
     public void checkSQLStatement(final ShardingSphereDatabase database, final 
LoadSingleTableStatement sqlStatement, final SingleRuleConfiguration 
currentRuleConfig) {
         String defaultSchemaName = 
DatabaseTypeEngine.getDefaultSchemaName(database.getProtocolType(), 
database.getName());
-        checkTables(database, sqlStatement, defaultSchemaName);
+        checkDuplicatedTables(database, sqlStatement, defaultSchemaName);
         checkStorageUnits(database, sqlStatement);
         checkActualTableExist(database, sqlStatement, defaultSchemaName);
     }
     
-    private void checkTables(final ShardingSphereDatabase database, final 
LoadSingleTableStatement sqlStatement, final String defaultSchemaName) {
-        Optional<SingleRule> currentSingleRule = 
database.getRuleMetaData().findSingleRule(SingleRule.class);
-        Collection<String> currentSingleTables = currentSingleRule.isPresent() 
? currentSingleRule.get().getSingleTableDataNodes().keySet() : 
Collections.emptyList();
+    private void checkDuplicatedTables(final ShardingSphereDatabase database, 
final LoadSingleTableStatement sqlStatement, final String defaultSchemaName) {
         Collection<SingleTableSegment> tableSegments = 
sqlStatement.getTables();
         boolean isSchemaSupportedDatabaseType = database.getProtocolType() 
instanceof SchemaSupportedDatabaseType;
         ShardingSphereSchema schema = database.getSchema(defaultSchemaName);
@@ -70,9 +65,7 @@ public final class LoadSingleTableStatementUpdater implements 
RuleDefinitionCrea
             if (SingleTableConstants.ASTERISK.equals(each.getTableName())) {
                 continue;
             }
-            boolean isNotSingleTable = 
schema.containsTable(each.getTableName()) && 
!currentSingleTables.contains(each.getTableName());
-            ShardingSpherePreconditions.checkState(isNotSingleTable, () -> new 
InvalidSingleRuleConfigurationException(String.format("Table `%s` existed and 
is not a single table in database `%s`",
-                    each.getTableName(), database.getName())));
+            
ShardingSpherePreconditions.checkState(!schema.containsTable(each.getTableName()),
 () -> new TableExistsException(each.getTableName()));
         }
     }
     
diff --git 
a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdaterTest.java
 
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdaterTest.java
new file mode 100644
index 00000000000..dfc845c1ac2
--- /dev/null
+++ 
b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdaterTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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.
+ */
+
+package org.apache.shardingsphere.single.distsql.handler.update;
+
+import 
org.apache.shardingsphere.dialect.exception.syntax.table.TableExistsException;
+import 
org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import 
org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
+import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration;
+import org.apache.shardingsphere.single.distsql.segment.SingleTableSegment;
+import 
org.apache.shardingsphere.single.distsql.statement.rdl.LoadSingleTableStatement;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class LoadSingleTableStatementUpdaterTest {
+    
+    private ShardingSphereDatabase database;
+    
+    private ShardingSphereSchema schema;
+    
+    private final LoadSingleTableStatementUpdater updater = new 
LoadSingleTableStatementUpdater();
+    
+    @BeforeEach
+    void setUp() {
+        database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
+        
when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.emptyList());
+        
when(database.getProtocolType()).thenReturn(mock(MySQLDatabaseType.class));
+        schema = mock(ShardingSphereSchema.class);
+        when(database.getSchema("foo_db")).thenReturn(schema);
+    }
+    
+    @Test
+    void assertCheckWithDuplicatedTables() {
+        when(database.getName()).thenReturn("foo_db");
+        when(schema.containsTable("foo")).thenReturn(true);
+        LoadSingleTableStatement sqlStatement = new 
LoadSingleTableStatement(Collections.singletonList(new 
SingleTableSegment("ds_0", null, "foo")));
+        assertThrows(TableExistsException.class, () -> 
updater.checkSQLStatement(database, sqlStatement, 
mock(SingleRuleConfiguration.class)));
+    }
+    
+    @Test
+    void assertCheckWithInvalidStorageUnit() {
+        when(database.getName()).thenReturn("foo_db");
+        LoadSingleTableStatement sqlStatement = new 
LoadSingleTableStatement(Collections.singletonList(new 
SingleTableSegment("ds_0", null, "foo")));
+        assertThrows(MissingRequiredStorageUnitsException.class, () -> 
updater.checkSQLStatement(database, sqlStatement, 
mock(SingleRuleConfiguration.class)));
+    }
+    
+    @Test
+    void assertBuild() {
+        LoadSingleTableStatement sqlStatement = new 
LoadSingleTableStatement(Collections.singletonList(new 
SingleTableSegment("ds_0", null, "foo")));
+        SingleRuleConfiguration actual = 
updater.buildToBeCreatedRuleConfiguration(mock(SingleRuleConfiguration.class), 
sqlStatement);
+        assertThat(actual.getTables().iterator().next(), is("ds_0.foo"));
+    }
+    
+    @Test
+    void assertUpdate() {
+        Collection<String> currentTables = new 
LinkedList<>(Collections.singletonList("ds_0.foo"));
+        SingleRuleConfiguration currentConfig = new 
SingleRuleConfiguration(currentTables, null);
+        SingleRuleConfiguration toBeCreatedRuleConfig = new 
SingleRuleConfiguration(Collections.singletonList("ds_0.bar"), null);
+        updater.updateCurrentRuleConfiguration(currentConfig, 
toBeCreatedRuleConfig);
+        Iterator<String> iterator = currentConfig.getTables().iterator();
+        assertThat(iterator.next(), is("ds_0.foo"));
+        assertThat(iterator.next(), is("ds_0.bar"));
+    }
+}

Reply via email to