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

duanzhengqiang 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 24c1f4de48c add validate for drop table cascade (#18662)
24c1f4de48c is described below

commit 24c1f4de48c67d6bad81c60b464dcb203e1636b2
Author: Chuxin Chen <[email protected]>
AuthorDate: Tue Jun 28 19:30:11 2022 +0800

    add validate for drop table cascade (#18662)
---
 .../algorithm/DatabaseDiscoveryEngine.java         |  4 +--
 .../impl/ShardingDropTableStatementValidator.java  |  3 ++
 .../ShardingDropTableStatementValidatorTest.java   |  8 ++---
 .../SingleTableMetadataValidatorFactory.java       | 10 +++++-
 .../ddl/SingleTableDropTableValidator.java         | 39 ++++++++++++++++++++++
 .../impl/OpenGaussDDLStatementSQLVisitor.java      |  3 +-
 .../impl/PostgreSQLDDLStatementSQLVisitor.java     |  3 +-
 .../handler/ddl/DropTableStatementHandler.java     | 16 +++++++++
 .../opengauss/ddl/OpenGaussDropTableStatement.java |  2 ++
 .../ddl/PostgreSQLDropTableStatement.java          |  2 ++
 .../handler/ddl/DropTableStatementHandlerTest.java |  4 +--
 11 files changed, 83 insertions(+), 11 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java
index a999ea23d6d..a5f498a44d1 100644
--- 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java
@@ -98,9 +98,9 @@ public final class DatabaseDiscoveryEngine {
         }
         return result;
     }
-
+    
     private void postReplicaDataSourceDisabledEvent(final String databaseName, 
final String groupName, final String primaryDataSourceName,
-            final Map<String, DataSource> dataSourceMap, final 
Collection<String> disabledDataSourceNames) {
+                                                    final Map<String, 
DataSource> dataSourceMap, final Collection<String> disabledDataSourceNames) {
         int enabledReplicasCount = dataSourceMap.size() - 
disabledDataSourceNames.size() - 1;
         for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
             if (!entry.getKey().equals(primaryDataSourceName)) {
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java
index a9e976b75c1..8bc3c2cf785 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java
@@ -52,6 +52,9 @@ public final class ShardingDropTableStatementValidator 
extends ShardingDDLStatem
                     .map(optional -> 
database.getSchemas().get(optional)).orElseGet(() -> 
database.getSchemas().get(defaultSchemaName));
             validateTableExist(schema, 
sqlStatementContext.getTablesContext().getTables());
         }
+        if 
(DropTableStatementHandler.containsCascade(sqlStatementContext.getSqlStatement()))
 {
+            throw new ShardingSphereException("DROP TABLE ... CASCADE 
statement is not supported yet.");
+        }
     }
     
     @Override
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java
index 8463164b47b..4059bd25964 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java
@@ -113,7 +113,7 @@ public final class ShardingDropTableStatementValidatorTest {
     
     @Test
     public void 
assertPostValidateDropTableWithSameRouteResultShardingTableForPostgreSQL() {
-        PostgreSQLDropTableStatement sqlStatement = new 
PostgreSQLDropTableStatement(false);
+        PostgreSQLDropTableStatement sqlStatement = new 
PostgreSQLDropTableStatement(false, false);
         sqlStatement.getTables().add(new SimpleTableSegment(new 
TableNameSegment(0, 0, new IdentifierValue("t_order"))));
         when(shardingRule.isShardingTable("t_order")).thenReturn(true);
         when(shardingRule.getTableRule("t_order")).thenReturn(new 
TableRule(Arrays.asList("ds_0", "ds_1"), "t_order"));
@@ -127,7 +127,7 @@ public final class ShardingDropTableStatementValidatorTest {
     
     @Test(expected = ShardingSphereException.class)
     public void 
assertPostValidateDropTableWithDifferentRouteResultShardingTableForPostgreSQL() 
{
-        PostgreSQLDropTableStatement sqlStatement = new 
PostgreSQLDropTableStatement(false);
+        PostgreSQLDropTableStatement sqlStatement = new 
PostgreSQLDropTableStatement(false, false);
         sqlStatement.getTables().add(new SimpleTableSegment(new 
TableNameSegment(0, 0, new IdentifierValue("t_order"))));
         when(shardingRule.isShardingTable("t_order")).thenReturn(true);
         when(shardingRule.getTableRule("t_order")).thenReturn(new 
TableRule(Arrays.asList("ds_0", "ds_1"), "t_order"));
@@ -140,7 +140,7 @@ public final class ShardingDropTableStatementValidatorTest {
     
     @Test
     public void 
assertPostValidateDropTableWithSameRouteResultBroadcastTableForPostgreSQL() {
-        PostgreSQLDropTableStatement sqlStatement = new 
PostgreSQLDropTableStatement(false);
+        PostgreSQLDropTableStatement sqlStatement = new 
PostgreSQLDropTableStatement(false, false);
         sqlStatement.getTables().add(new SimpleTableSegment(new 
TableNameSegment(0, 0, new IdentifierValue("t_config"))));
         when(shardingRule.isBroadcastTable("t_config")).thenReturn(true);
         when(shardingRule.getTableRule("t_config")).thenReturn(new 
TableRule(Arrays.asList("ds_0", "ds_1"), "t_config"));
@@ -154,7 +154,7 @@ public final class ShardingDropTableStatementValidatorTest {
     
     @Test(expected = ShardingSphereException.class)
     public void 
assertPostValidateDropTableWithDifferentRouteResultBroadcastTableForPostgreSQL()
 {
-        PostgreSQLDropTableStatement sqlStatement = new 
PostgreSQLDropTableStatement(false);
+        PostgreSQLDropTableStatement sqlStatement = new 
PostgreSQLDropTableStatement(false, false);
         sqlStatement.getTables().add(new SimpleTableSegment(new 
TableNameSegment(0, 0, new IdentifierValue("t_config"))));
         when(shardingRule.isBroadcastTable("t_config")).thenReturn(true);
         when(shardingRule.getTableRule("t_config")).thenReturn(new 
TableRule(Arrays.asList("ds_0", "ds_1"), "t_config"));
diff --git 
a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/route/validator/SingleTableMetadataValidatorFactory.java
 
b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/route/validator/SingleTableMetadataValidatorFactory.java
index a03de28feeb..391988d910d 100644
--- 
a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/route/validator/SingleTableMetadataValidatorFactory.java
+++ 
b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/route/validator/SingleTableMetadataValidatorFactory.java
@@ -20,8 +20,10 @@ package 
org.apache.shardingsphere.singletable.route.validator;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import 
org.apache.shardingsphere.singletable.route.validator.ddl.SingleTableDropSchemaMetadataValidator;
+import 
org.apache.shardingsphere.singletable.route.validator.ddl.SingleTableDropTableValidator;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropSchemaStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropTableStatement;
 
 import java.util.Optional;
 
@@ -39,6 +41,12 @@ public final class SingleTableMetadataValidatorFactory {
      */
     @SuppressWarnings("rawtypes")
     public static Optional<SingleTableMetadataValidator> newInstance(final 
SQLStatement sqlStatement) {
-        return sqlStatement instanceof DropSchemaStatement ? Optional.of(new 
SingleTableDropSchemaMetadataValidator()) : Optional.empty();
+        if (sqlStatement instanceof DropSchemaStatement) {
+            return Optional.of(new SingleTableDropSchemaMetadataValidator());
+        }
+        if (sqlStatement instanceof DropTableStatement) {
+            return Optional.of(new SingleTableDropTableValidator());
+        }
+        return Optional.empty();
     }
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/route/validator/ddl/SingleTableDropTableValidator.java
 
b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/route/validator/ddl/SingleTableDropTableValidator.java
new file mode 100644
index 00000000000..1adb9467f98
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/route/validator/ddl/SingleTableDropTableValidator.java
@@ -0,0 +1,39 @@
+/*
+ * 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.singletable.route.validator.ddl;
+
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.exception.ShardingSphereException;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import 
org.apache.shardingsphere.singletable.route.validator.SingleTableMetadataValidator;
+import org.apache.shardingsphere.singletable.rule.SingleTableRule;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropTableStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.dialect.handler.ddl.DropTableStatementHandler;
+
+/**
+ * Single table drop table validator.
+ */
+public final class SingleTableDropTableValidator implements 
SingleTableMetadataValidator<DropTableStatement> {
+    
+    @Override
+    public void validate(final SingleTableRule rule, final 
SQLStatementContext<DropTableStatement> sqlStatementContext, final 
ShardingSphereDatabase database) {
+        if 
(DropTableStatementHandler.containsCascade(sqlStatementContext.getSqlStatement()))
 {
+            throw new ShardingSphereException("DROP TABLE ... CASCADE 
statement is not supported yet.");
+        }
+    }
+}
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussDDLStatementSQLVisitor.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussDDLStatementSQLVisitor.java
index 0295f816de5..44271d67d2c 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussDDLStatementSQLVisitor.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-opengauss/src/main/java/org/apache/shardingsphere/sql/parser/opengauss/visitor/statement/impl/OpenGaussDDLStatementSQLVisitor.java
@@ -490,7 +490,8 @@ public final class OpenGaussDDLStatementSQLVisitor extends 
OpenGaussStatementSQL
     @SuppressWarnings("unchecked")
     @Override
     public ASTNode visitDropTable(final DropTableContext ctx) {
-        OpenGaussDropTableStatement result = new 
OpenGaussDropTableStatement(null != ctx.ifExists());
+        boolean containsCascade = null != ctx.dropTableOpt() && null != 
ctx.dropTableOpt().CASCADE();
+        OpenGaussDropTableStatement result = new 
OpenGaussDropTableStatement(null != ctx.ifExists(), containsCascade);
         result.getTables().addAll(((CollectionValue<SimpleTableSegment>) 
visit(ctx.tableNames())).getValue());
         return result;
     }
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDDLStatementSQLVisitor.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDDLStatementSQLVisitor.java
index 0d04cd45d8e..25e4fc9c745 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDDLStatementSQLVisitor.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDDLStatementSQLVisitor.java
@@ -556,7 +556,8 @@ public final class PostgreSQLDDLStatementSQLVisitor extends 
PostgreSQLStatementS
     @SuppressWarnings("unchecked")
     @Override
     public ASTNode visitDropTable(final DropTableContext ctx) {
-        PostgreSQLDropTableStatement result = new 
PostgreSQLDropTableStatement(null != ctx.ifExists());
+        boolean containsCascade = null != ctx.dropTableOpt() && null != 
ctx.dropTableOpt().CASCADE();
+        PostgreSQLDropTableStatement result = new 
PostgreSQLDropTableStatement(null != ctx.ifExists(), containsCascade);
         result.getTables().addAll(((CollectionValue<SimpleTableSegment>) 
visit(ctx.tableNames())).getValue());
         return result;
     }
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/DropTableStatementHandler.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/DropTableStatementHandler.java
index a9ece13ee0d..f60fad5e8d2 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/DropTableStatementHandler.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/DropTableStatementHandler.java
@@ -57,4 +57,20 @@ public final class DropTableStatementHandler implements 
SQLStatementHandler {
         }
         return false;
     }
+    
+    /**
+     * Judge whether contains cascade.
+     *
+     * @param dropTableStatement drop table statement
+     * @return contains cascade or not
+     */
+    public static boolean containsCascade(final DropTableStatement 
dropTableStatement) {
+        if (dropTableStatement instanceof PostgreSQLStatement) {
+            return ((PostgreSQLDropTableStatement) 
dropTableStatement).isContainsCascade();
+        }
+        if (dropTableStatement instanceof OpenGaussStatement) {
+            return ((OpenGaussDropTableStatement) 
dropTableStatement).isContainsCascade();
+        }
+        return false;
+    }
 }
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/opengauss/ddl/OpenGaussDropTableStatement.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/opengauss/ddl/OpenGaussDropTableStatement.java
index d6ae916d3c9..ca1d7017543 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/opengauss/ddl/OpenGaussDropTableStatement.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/opengauss/ddl/OpenGaussDropTableStatement.java
@@ -32,4 +32,6 @@ import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.Open
 public final class OpenGaussDropTableStatement extends DropTableStatement 
implements OpenGaussStatement {
     
     private final boolean ifExists;
+    
+    private final boolean containsCascade;
 }
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/ddl/PostgreSQLDropTableStatement.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/ddl/PostgreSQLDropTableStatement.java
index 70eb05bfda5..61920dd6554 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/ddl/PostgreSQLDropTableStatement.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/ddl/PostgreSQLDropTableStatement.java
@@ -32,4 +32,6 @@ import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.Pos
 public final class PostgreSQLDropTableStatement extends DropTableStatement 
implements PostgreSQLStatement {
     
     private final boolean ifExists;
+    
+    private final boolean containsCascade;
 }
diff --git 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/DropTableStatementHandlerTest.java
 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/DropTableStatementHandlerTest.java
index ee371d31c57..e62e5f03bfa 100644
--- 
a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/DropTableStatementHandlerTest.java
+++ 
b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/DropTableStatementHandlerTest.java
@@ -36,7 +36,7 @@ public final class DropTableStatementHandlerTest {
     
     @Test
     public void assertContainsIfExistsForPostgreSQL() {
-        assertTrue(DropTableStatementHandler.ifExists(new 
PostgreSQLDropTableStatement(true)));
+        assertTrue(DropTableStatementHandler.ifExists(new 
PostgreSQLDropTableStatement(true, false)));
     }
     
     @Test
@@ -56,7 +56,7 @@ public final class DropTableStatementHandlerTest {
     
     @Test
     public void assertNotContainsIfExistsForPostgreSQL() {
-        assertFalse(DropTableStatementHandler.ifExists(new 
PostgreSQLDropTableStatement(false)));
+        assertFalse(DropTableStatementHandler.ifExists(new 
PostgreSQLDropTableStatement(false, false)));
     }
     
     @Test

Reply via email to