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 09e3362  fix wrong sharding table validate logic (#14547)
09e3362 is described below

commit 09e33620ef7b479651e1dcc88636c8e17eec32a6
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Wed Jan 5 19:48:50 2022 +0800

    fix wrong sharding table validate logic (#14547)
---
 .../engine/validator/ddl/ShardingDDLStatementValidator.java    |  6 +++---
 .../ddl/impl/ShardingAlterViewStatementValidator.java          |  8 ++++----
 .../ddl/impl/ShardingCreateFunctionStatementValidator.java     | 10 +++++-----
 .../ddl/impl/ShardingCreateProcedureStatementValidator.java    | 10 +++++-----
 .../ddl/impl/ShardingCreateViewStatementValidator.java         |  8 ++++----
 .../validator/ddl/ShardingAlterViewStatementValidatorTest.java |  9 ++++++---
 .../ddl/ShardingCreateFunctionStatementValidatorTest.java      |  3 +++
 .../ddl/ShardingCreateProcedureStatementValidatorTest.java     |  4 ++++
 .../ddl/ShardingCreateViewStatementValidatorTest.java          |  8 ++++++--
 9 files changed, 40 insertions(+), 26 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDDLStatementValidator.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDDLStatementValidator.java
index 7367a48..c04fd3a 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDDLStatementValidator.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDDLStatementValidator.java
@@ -38,13 +38,13 @@ public abstract class ShardingDDLStatementValidator<T 
extends DDLStatement> impl
     /**
      * Validate sharding table.
      *
-     * @param schema ShardingSphere schema
+     * @param shardingRule sharding rule
      * @param tables tables
      */
-    protected void validateShardingTable(final ShardingSphereSchema schema, 
final Collection<SimpleTableSegment> tables) {
+    protected void validateShardingTable(final ShardingRule shardingRule, 
final Collection<SimpleTableSegment> tables) {
         for (SimpleTableSegment each : tables) {
             String tableName = each.getTableName().getIdentifier().getValue();
-            if (schema.getAllTableNames().contains(tableName)) {
+            if (shardingRule.isShardingTable(tableName)) {
                 throw new ShardingSphereException("Can not support sharding 
table '%s'.", tableName);
             }
         }
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidator.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidator.java
index ded4138..6e7f594 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidator.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterViewStatementValidator.java
@@ -40,11 +40,11 @@ public final class ShardingAlterViewStatementValidator 
extends ShardingDDLStatem
     public void preValidate(final ShardingRule shardingRule, final 
SQLStatementContext<AlterViewStatement> sqlStatementContext, 
                             final List<Object> parameters, final 
ShardingSphereSchema schema) {
         Optional<SelectStatement> selectStatement = 
AlterViewStatementHandler.getSelectStatement(sqlStatementContext.getSqlStatement());
-        selectStatement.ifPresent(select -> {
+        if (selectStatement.isPresent()) {
             TableExtractor extractor = new TableExtractor();
-            extractor.extractTablesFromSelect(select);
-            validateShardingTable(schema, extractor.getRewriteTables());
-        });
+            extractor.extractTablesFromSelect(selectStatement.get());
+            validateShardingTable(shardingRule, extractor.getRewriteTables());
+        }
     }
     
     @Override
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateFunctionStatementValidator.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateFunctionStatementValidator.java
index 776f6ba..3c62f02 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateFunctionStatementValidator.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateFunctionStatementValidator.java
@@ -42,13 +42,13 @@ public final class ShardingCreateFunctionStatementValidator 
extends ShardingDDLS
     public void preValidate(final ShardingRule shardingRule, final 
SQLStatementContext<CreateFunctionStatement> sqlStatementContext, 
                             final List<Object> parameters, final 
ShardingSphereSchema schema) {
         Optional<RoutineBodySegment> routineBodySegment = 
CreateFunctionStatementHandler.getRoutineBodySegment(sqlStatementContext.getSqlStatement());
-        routineBodySegment.ifPresent(routineBody -> {
+        if (routineBodySegment.isPresent()) {
             TableExtractor extractor = new TableExtractor();
-            Collection<SimpleTableSegment> existTables = 
extractor.extractExistTableFromRoutineBody(routineBody);
-            validateShardingTable(schema, existTables);
+            Collection<SimpleTableSegment> existTables = 
extractor.extractExistTableFromRoutineBody(routineBodySegment.get());
+            validateShardingTable(shardingRule, existTables);
             validateTableExist(schema, existTables);
-            validateTableNotExist(schema, 
extractor.extractNotExistTableFromRoutineBody(routineBody));
-        });
+            validateTableNotExist(schema, 
extractor.extractNotExistTableFromRoutineBody(routineBodySegment.get()));
+        }
     }
     
     @Override
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateProcedureStatementValidator.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateProcedureStatementValidator.java
index b16fe11..faf07b6 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateProcedureStatementValidator.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateProcedureStatementValidator.java
@@ -42,13 +42,13 @@ public final class 
ShardingCreateProcedureStatementValidator extends ShardingDDL
     public void preValidate(final ShardingRule shardingRule, final 
SQLStatementContext<CreateProcedureStatement> sqlStatementContext, 
                             final List<Object> parameters, final 
ShardingSphereSchema schema) {
         Optional<RoutineBodySegment> routineBodySegment = 
CreateProcedureStatementHandler.getRoutineBodySegment(sqlStatementContext.getSqlStatement());
-        routineBodySegment.ifPresent(routineBody -> {
+        if (routineBodySegment.isPresent()) {
             TableExtractor extractor = new TableExtractor();
-            validateTableNotExist(schema, 
extractor.extractNotExistTableFromRoutineBody(routineBody));
-            Collection<SimpleTableSegment> existTables = 
extractor.extractExistTableFromRoutineBody(routineBody);
-            validateShardingTable(schema, existTables);
+            validateTableNotExist(schema, 
extractor.extractNotExistTableFromRoutineBody(routineBodySegment.get()));
+            Collection<SimpleTableSegment> existTables = 
extractor.extractExistTableFromRoutineBody(routineBodySegment.get());
+            validateShardingTable(shardingRule, existTables);
             validateTableExist(schema, existTables);
-        });
+        }
     }
     
     @Override
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java
index 2a56089..11e9aca 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateViewStatementValidator.java
@@ -40,11 +40,11 @@ public final class ShardingCreateViewStatementValidator 
extends ShardingDDLState
     public void preValidate(final ShardingRule shardingRule, final 
SQLStatementContext<CreateViewStatement> sqlStatementContext, 
                             final List<Object> parameters, final 
ShardingSphereSchema schema) {
         Optional<SelectStatement> selectStatement = 
CreateViewStatementHandler.getSelectStatement(sqlStatementContext.getSqlStatement());
-        selectStatement.ifPresent(select -> {
+        if (selectStatement.isPresent()) {
             TableExtractor extractor = new TableExtractor();
-            extractor.extractTablesFromSelect(select);
-            validateShardingTable(schema, extractor.getRewriteTables());
-        });
+            extractor.extractTablesFromSelect(selectStatement.get());
+            validateShardingTable(shardingRule, extractor.getRewriteTables());
+        }
     }
     
     @Override
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java
index 5a8d8cc..ba6cb09 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingAlterViewStatementValidatorTest.java
@@ -30,13 +30,16 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.Identifi
 import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLAlterViewStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
 
 import java.util.Collections;
 
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+@RunWith(MockitoJUnitRunner.class)
 public final class ShardingAlterViewStatementValidatorTest {
     
     @Mock
@@ -45,12 +48,12 @@ public final class ShardingAlterViewStatementValidatorTest {
     @Test
     public void assertPreValidateAlterViewForMySQL() {
         MySQLSelectStatement selectStatement = new MySQLSelectStatement();
-        selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 
0, new IdentifierValue("t_order_item"))));
+        selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 
0, new IdentifierValue("t_order"))));
         MySQLAlterViewStatement sqlStatement = new MySQLAlterViewStatement();
         sqlStatement.setSelect(selectStatement);
         SQLStatementContext<AlterViewStatement> sqlStatementContext = new 
CommonSQLStatementContext<>(sqlStatement);
         ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
-        
when(schema.getAllTableNames()).thenReturn(Collections.singletonList("t_order"));
+        when(shardingRule.isShardingTable("t_order")).thenReturn(false);
         new ShardingAlterViewStatementValidator().preValidate(shardingRule, 
sqlStatementContext, Collections.emptyList(), schema);
     }
     
@@ -61,8 +64,8 @@ public final class ShardingAlterViewStatementValidatorTest {
         MySQLAlterViewStatement sqlStatement = new MySQLAlterViewStatement();
         sqlStatement.setSelect(selectStatement);
         ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
-        
when(schema.getAllTableNames()).thenReturn(Collections.singleton("t_order"));
         SQLStatementContext<AlterViewStatement> sqlStatementContext = new 
CommonSQLStatementContext<>(sqlStatement);
+        when(shardingRule.isShardingTable("t_order")).thenReturn(true);
         new ShardingAlterViewStatementValidator().preValidate(shardingRule, 
sqlStatementContext, Collections.emptyList(), schema);
     }
 }
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateFunctionStatementValidatorTest.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateFunctionStatementValidatorTest.java
index a20562d..73568b7 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateFunctionStatementValidatorTest.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateFunctionStatementValidatorTest.java
@@ -35,7 +35,9 @@ import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQ
 import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLCreateTableStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
 
 import java.util.Collections;
 
@@ -43,6 +45,7 @@ import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+@RunWith(MockitoJUnitRunner.class)
 public final class ShardingCreateFunctionStatementValidatorTest {
     
     @Mock
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateProcedureStatementValidatorTest.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateProcedureStatementValidatorTest.java
index 2110ccb..eb2a498 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateProcedureStatementValidatorTest.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateProcedureStatementValidatorTest.java
@@ -35,7 +35,9 @@ import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQ
 import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLCreateTableStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
 
 import java.util.Collections;
 
@@ -43,6 +45,7 @@ import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+@RunWith(MockitoJUnitRunner.class)
 public final class ShardingCreateProcedureStatementValidatorTest {
     
     @Mock
@@ -66,6 +69,7 @@ public final class 
ShardingCreateProcedureStatementValidatorTest {
         SQLStatementContext<CreateProcedureStatement> sqlStatementContext = 
new CommonSQLStatementContext<>(sqlStatement);
         ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
         when(schema.containsTable("t_order_item")).thenReturn(true);
+        when(shardingRule.isShardingTable("t_order_item")).thenReturn(false);
         new 
ShardingCreateProcedureStatementValidator().preValidate(shardingRule, 
sqlStatementContext, Collections.emptyList(), schema);
     }
     
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java
index 4ad0619..aa8534f 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateViewStatementValidatorTest.java
@@ -30,13 +30,16 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.Identifi
 import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLCreateViewStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement;
 import org.junit.Test;
+import org.junit.runner.RunWith;
 import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
 
 import java.util.Collections;
 
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+@RunWith(MockitoJUnitRunner.class)
 public final class ShardingCreateViewStatementValidatorTest {
     
     @Mock
@@ -45,10 +48,11 @@ public final class ShardingCreateViewStatementValidatorTest 
{
     @Test
     public void assertPreValidateCreateViewForMySQL() {
         MySQLSelectStatement selectStatement = new MySQLSelectStatement();
-        selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 
0, new IdentifierValue("t_order_item"))));
+        selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 
0, new IdentifierValue("t_order"))));
         MySQLCreateViewStatement sqlStatement = new MySQLCreateViewStatement();
         sqlStatement.setSelect(selectStatement);
         SQLStatementContext<CreateViewStatement> sqlStatementContext = new 
CommonSQLStatementContext<>(sqlStatement);
+        when(shardingRule.isShardingTable("t_order")).thenReturn(false);
         new ShardingCreateViewStatementValidator().preValidate(shardingRule, 
sqlStatementContext, Collections.emptyList(), mock(ShardingSphereSchema.class));
     }
     
@@ -59,8 +63,8 @@ public final class ShardingCreateViewStatementValidatorTest {
         MySQLCreateViewStatement sqlStatement = new MySQLCreateViewStatement();
         sqlStatement.setSelect(selectStatement);
         ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
-        
when(schema.getAllTableNames()).thenReturn(Collections.singleton("t_order"));
         SQLStatementContext<CreateViewStatement> sqlStatementContext = new 
CommonSQLStatementContext<>(sqlStatement);
+        when(shardingRule.isShardingTable("t_order")).thenReturn(true);
         new ShardingCreateViewStatementValidator().preValidate(shardingRule, 
sqlStatementContext, Collections.emptyList(), schema);
     }
 }

Reply via email to