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/05/27 11:03:54 UTC
[shardingsphere] branch master updated: add post validate for drop
table statement (#10489)
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 f7dd88e add post validate for drop table statement (#10489)
f7dd88e is described below
commit f7dd88e8bd2b66bf68db098d5f53125852b4e92f
Author: Zhengqiang Duan <st...@gmail.com>
AuthorDate: Thu May 27 19:03:10 2021 +0800
add post validate for drop table statement (#10489)
---
.../src/test/resources/sharding/alter.xml | 8 ++
.../src/test/resources/sharding/drop.xml | 140 +++++++++++++++++++++
.../ddl/ShardingDDLStatementValidator.java | 16 +++
.../impl/ShardingAlterTableStatementValidator.java | 4 +-
.../ShardingCreateTableStatementValidator.java | 4 +-
.../impl/ShardingDropTableStatementValidator.java | 4 +
.../ShardingCreateTableStatementValidatorTest.java | 86 ++++++++++++-
.../ShardingDropTableStatementValidatorTest.java | 82 +++++++++++-
8 files changed, 335 insertions(+), 9 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/alter.xml b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/alter.xml
index 404c27b..e946ee7 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/alter.xml
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/alter.xml
@@ -77,6 +77,10 @@
<input sql="ALTER TABLE t_single ADD CONSTRAINT t_single_fk FOREIGN KEY (order_id) REFERENCES t_single_extend (order_id)" />
<output sql="ALTER TABLE t_single ADD CONSTRAINT t_single_fk FOREIGN KEY (order_id) REFERENCES t_single_extend (order_id)" />
</rewrite-assertion>
+ <rewrite-assertion id="alter_table_with_single_table_with_rename_table_for_postgresql" db-type="PostgreSQL">
+ <input sql="ALTER TABLE t_single RENAME TO t_single_extend" />
+ <output sql="ALTER TABLE t_single RENAME TO t_single_extend" />
+ </rewrite-assertion>
<rewrite-assertion id="alter_table_with_single_data_node_binding_table_with_add_foreign_constraint_for_mysql" db-type="MySQL">
<input sql="ALTER TABLE t_order ADD CONSTRAINT t_order_fk FOREIGN KEY (order_id) REFERENCES t_order_item (order_id)" />
<output sql="ALTER TABLE t_order_0 ADD CONSTRAINT t_order_fk FOREIGN KEY (order_id) REFERENCES t_order_item_0 (order_id)" />
@@ -137,4 +141,8 @@
<input sql="ALTER TABLE t_single ADD CONSTRAINT t_single_fk FOREIGN KEY (order_id) REFERENCES t_single_extend (order_id)" />
<output sql="ALTER TABLE t_single ADD CONSTRAINT t_single_fk FOREIGN KEY (order_id) REFERENCES t_single_extend (order_id)" />
</rewrite-assertion>
+ <rewrite-assertion id="alter_table_with_single_table_with_rename_table_for_mysql" db-type="MySQL">
+ <input sql="ALTER TABLE t_single RENAME TO t_single_extend" />
+ <output sql="ALTER TABLE t_single RENAME TO t_single_extend" />
+ </rewrite-assertion>
</rewrite-assertions>
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/drop.xml b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/drop.xml
new file mode 100644
index 0000000..9f103f7
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/sharding/drop.xml
@@ -0,0 +1,140 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+
+<rewrite-assertions yaml-rule="yaml/sharding/sharding-rule.yaml">
+ <rewrite-assertion id="drop_table_with_single_data_node_binding_table_for_postgresql" db-type="PostgreSQL">
+ <input sql="DROP TABLE IF EXISTS t_order, t_order_item" />
+ <output sql="DROP TABLE IF EXISTS t_order_0, t_order_item_0" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_single_data_node_unbinding_table_for_postgresql" db-type="PostgreSQL">
+ <input sql="DROP TABLE IF EXISTS t_order, t_order_extend" />
+ <output sql="DROP TABLE IF EXISTS t_order_0, t_order_extend_0" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_multi_data_node_and_single_data_node_table_for_postgresql" db-type="PostgreSQL">
+ <input sql="DROP TABLE IF EXISTS t_account_detail, t_order" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_0, t_order_0" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_1, t_order_0" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_multi_data_node_binding_table_for_postgresql" db-type="PostgreSQL">
+ <input sql="DROP TABLE IF EXISTS t_account_detail, t_account_detail" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_0, t_account_detail_0" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_1, t_account_detail_1" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_single_data_node_and_broadcast_table_for_postgresql" db-type="PostgreSQL">
+ <input sql="DROP TABLE IF EXISTS t_order, t_config" />
+ <output sql="DROP TABLE IF EXISTS t_order_0, t_config" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_multi_data_node_and_broadcast_table_for_postgresql" db-type="PostgreSQL">
+ <input sql="DROP TABLE IF EXISTS t_account_detail, t_config" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_0, t_config" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_1, t_config" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_single_data_node_and_single_table_for_postgresql" db-type="PostgreSQL">
+ <input sql="DROP TABLE IF EXISTS t_order, t_single" />
+ <output sql="DROP TABLE IF EXISTS t_order_0, t_single" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_multi_data_node_and_single_table_for_postgresql" db-type="PostgreSQL">
+ <input sql="DROP TABLE IF EXISTS t_account_detail, t_single" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_0, t_single" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_1, t_single" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_broadcast_and_single_data_node_table_for_postgresql" db-type="PostgreSQL">
+ <input sql="DROP TABLE IF EXISTS t_config, t_order" />
+ <output sql="DROP TABLE IF EXISTS t_config, t_order_0" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_broadcast_table_for_postgresql" db-type="PostgreSQL">
+ <input sql="DROP TABLE IF EXISTS t_config, t_order_type" />
+ <output sql="DROP TABLE IF EXISTS t_config, t_order_type" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_broadcast_and_single_table_for_postgresql" db-type="PostgreSQL">
+ <input sql="DROP TABLE IF EXISTS t_config, t_single" />
+ <output sql="DROP TABLE IF EXISTS t_config, t_single" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_single_and_single_data_node_table_for_postgresql" db-type="PostgreSQL">
+ <input sql="DROP TABLE IF EXISTS t_single, t_order" />
+ <output sql="DROP TABLE IF EXISTS t_single, t_order_0" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_single_and_broadcast_table_for_postgresql" db-type="PostgreSQL">
+ <input sql="DROP TABLE IF EXISTS t_single, t_config" />
+ <output sql="DROP TABLE IF EXISTS t_single, t_config" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_single_table_for_postgresql" db-type="PostgreSQL">
+ <input sql="DROP TABLE IF EXISTS t_single, t_single_extend" />
+ <output sql="DROP TABLE IF EXISTS t_single, t_single_extend" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_single_data_node_binding_table_for_mysql" db-type="MySQL">
+ <input sql="DROP TABLE IF EXISTS t_order, t_order_item" />
+ <output sql="DROP TABLE IF EXISTS t_order_0, t_order_item_0" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_single_data_node_unbinding_table_for_mysql" db-type="MySQL">
+ <input sql="DROP TABLE IF EXISTS t_order, t_order_extend" />
+ <output sql="DROP TABLE IF EXISTS t_order_0, t_order_extend_0" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_multi_data_node_and_single_data_node_table_for_mysql" db-type="MySQL">
+ <input sql="DROP TABLE IF EXISTS t_account_detail, t_order" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_0, t_order_0" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_1, t_order_0" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_multi_data_node_binding_table_for_mysql" db-type="MySQL">
+ <input sql="DROP TABLE IF EXISTS t_account_detail, t_account_detail" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_0, t_account_detail_0" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_1, t_account_detail_1" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_single_data_node_and_broadcast_table_for_mysql" db-type="MySQL">
+ <input sql="DROP TABLE IF EXISTS t_order, t_config" />
+ <output sql="DROP TABLE IF EXISTS t_order_0, t_config" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_multi_data_node_and_broadcast_table_for_mysql" db-type="MySQL">
+ <input sql="DROP TABLE IF EXISTS t_account_detail, t_config" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_0, t_config" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_1, t_config" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_single_data_node_and_single_table_for_mysql" db-type="MySQL">
+ <input sql="DROP TABLE IF EXISTS t_order, t_single" />
+ <output sql="DROP TABLE IF EXISTS t_order_0, t_single" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_multi_data_node_and_single_table_for_mysql" db-type="MySQL">
+ <input sql="DROP TABLE IF EXISTS t_account_detail, t_single" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_0, t_single" />
+ <output sql="DROP TABLE IF EXISTS t_account_detail_1, t_single" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_broadcast_and_single_data_node_table_for_mysql" db-type="MySQL">
+ <input sql="DROP TABLE IF EXISTS t_config, t_order" />
+ <output sql="DROP TABLE IF EXISTS t_config, t_order_0" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_broadcast_table_for_mysql" db-type="MySQL">
+ <input sql="DROP TABLE IF EXISTS t_config, t_order_type" />
+ <output sql="DROP TABLE IF EXISTS t_config, t_order_type" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_broadcast_and_single_table_for_mysql" db-type="MySQL">
+ <input sql="DROP TABLE IF EXISTS t_config, t_single" />
+ <output sql="DROP TABLE IF EXISTS t_config, t_single" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_single_and_single_data_node_table_for_mysql" db-type="MySQL">
+ <input sql="DROP TABLE IF EXISTS t_single, t_order" />
+ <output sql="DROP TABLE IF EXISTS t_single, t_order_0" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_single_and_broadcast_table_for_mysql" db-type="MySQL">
+ <input sql="DROP TABLE IF EXISTS t_single, t_config" />
+ <output sql="DROP TABLE IF EXISTS t_single, t_config" />
+ </rewrite-assertion>
+ <rewrite-assertion id="drop_table_with_single_table_for_mysql" db-type="MySQL">
+ <input sql="DROP TABLE IF EXISTS t_single, t_single_extend" />
+ <output sql="DROP TABLE IF EXISTS t_single, t_single_extend" />
+ </rewrite-assertion>
+</rewrite-assertions>
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDDLStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDDLStatementValidator.java
index bd0bbaf..d3f3a28 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDDLStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDDLStatementValidator.java
@@ -19,9 +19,11 @@ package org.apache.shardingsphere.sharding.route.engine.validator.ddl;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.sharding.route.engine.exception.NoSuchTableException;
import org.apache.shardingsphere.sharding.route.engine.exception.TableExistsException;
import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
@@ -76,4 +78,18 @@ public abstract class ShardingDDLStatementValidator<T extends DDLStatement> impl
}
}
}
+
+ /**
+ * Judge whether route unit and primary table data node are different size or not.
+ *
+ * @param shardingRule sharding rule
+ * @param routeContext route context
+ * @param primaryTable primary table
+ * @return whether route unit and primary table data node are different size or not
+ */
+ protected boolean isRouteUnitPrimaryTableDataNodeDifferentSize(final ShardingRule shardingRule, final RouteContext routeContext, final String primaryTable) {
+ int primaryTableDataNodeSize = shardingRule.isShardingTable(primaryTable) || shardingRule.isBroadcastTable(primaryTable)
+ ? shardingRule.getTableRule(primaryTable).getActualDataNodes().size() : 1;
+ return primaryTableDataNodeSize != routeContext.getRouteUnits().size();
+ }
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidator.java
index a5f7f89..70d5f8b 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingAlterTableStatementValidator.java
@@ -58,9 +58,7 @@ public final class ShardingAlterTableStatementValidator extends ShardingDDLState
@Override
public void postValidate(final ShardingRule shardingRule, final AlterTableStatement sqlStatement, final RouteContext routeContext) {
String primaryTable = sqlStatement.getTable().getTableName().getIdentifier().getValue();
- int primaryTableDataNodeSize = shardingRule.isShardingTable(primaryTable) || shardingRule.isBroadcastTable(primaryTable)
- ? shardingRule.getTableRule(primaryTable).getActualDataNodes().size() : 1;
- if (primaryTableDataNodeSize != routeContext.getRouteUnits().size()) {
+ if (isRouteUnitPrimaryTableDataNodeDifferentSize(shardingRule, routeContext, primaryTable)) {
throw new ShardingSphereException("ALTER TABLE ... statement route unit size must be same with primary table '%s' data node size.", primaryTable);
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidator.java
index 60b8a0d..6afc0f0 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateTableStatementValidator.java
@@ -45,9 +45,7 @@ public final class ShardingCreateTableStatementValidator extends ShardingDDLStat
@Override
public void postValidate(final ShardingRule shardingRule, final CreateTableStatement sqlStatement, final RouteContext routeContext) {
String primaryTable = sqlStatement.getTable().getTableName().getIdentifier().getValue();
- int primaryTableDataNodeSize = shardingRule.isShardingTable(primaryTable) || shardingRule.isBroadcastTable(primaryTable)
- ? shardingRule.getTableRule(primaryTable).getActualDataNodes().size() : 1;
- if (primaryTableDataNodeSize != routeContext.getRouteUnits().size()) {
+ if (isRouteUnitPrimaryTableDataNodeDifferentSize(shardingRule, routeContext, primaryTable)) {
throw new ShardingSphereException("CREATE TABLE ... statement route unit size must be same with primary table '%s' data node size.", primaryTable);
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java
index bc10cec..29bdbe6 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingDropTableStatementValidator.java
@@ -50,6 +50,10 @@ public final class ShardingDropTableStatementValidator extends ShardingDDLStatem
@Override
public void postValidate(final ShardingRule shardingRule, final DropTableStatement sqlStatement, final RouteContext routeContext) {
checkTableInUsed(shardingRule, sqlStatement, routeContext);
+ String primaryTable = sqlStatement.getTables().iterator().next().getTableName().getIdentifier().getValue();
+ if (isRouteUnitPrimaryTableDataNodeDifferentSize(shardingRule, routeContext, primaryTable)) {
+ throw new ShardingSphereException("DROP TABLE ... statement route unit size must be same with primary table '%s' data node size.", primaryTable);
+ }
}
private void checkTableInUsed(final ShardingRule shardingRule, final DropTableStatement sqlStatement, final RouteContext routeContext) {
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateTableStatementValidatorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateTableStatementValidatorTest.java
index 5acd2f3..185a061 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateTableStatementValidatorTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateTableStatementValidatorTest.java
@@ -19,10 +19,15 @@ package org.apache.shardingsphere.sharding.route.engine.validator.ddl;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.statement.ddl.CreateTableStatementContext;
+import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.infra.route.context.RouteMapper;
+import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.sharding.route.engine.exception.TableExistsException;
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingCreateTableStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sharding.rule.TableRule;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateTableStatement;
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
@@ -32,18 +37,27 @@ import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sql92.ddl.SQL92CreateTableStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.ddl.SQLServerCreateTableStatement;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
+import java.util.LinkedList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+@RunWith(MockitoJUnitRunner.class)
public final class ShardingCreateTableStatementValidatorTest {
@Mock
private ShardingRule shardingRule;
+ @Mock
+ private RouteContext routeContext;
+
@Test(expected = TableExistsException.class)
public void assertValidateMySQLCreateTable() {
MySQLCreateTableStatement sqlStatement = new MySQLCreateTableStatement();
@@ -107,7 +121,77 @@ public final class ShardingCreateTableStatementValidatorTest {
private void assertValidateCreateTableIfNotExists(final CreateTableStatement sqlStatement) {
SQLStatementContext<CreateTableStatement> sqlStatementContext = new CreateTableStatementContext(sqlStatement);
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
- when(schema.containsTable("t_order")).thenReturn(true);
new ShardingCreateTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), schema);
}
+
+ @Test
+ public void assertPostValidateCreateTableWithSameRouteResultShardingTableForPostgreSQL() {
+ PostgreSQLCreateTableStatement sqlStatement = new PostgreSQLCreateTableStatement();
+ sqlStatement.setTable(new SimpleTableSegment(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"));
+ Collection<RouteUnit> routeUnits = new LinkedList<>();
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_order", "t_order_0"))));
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_1", "ds_1"), Collections.singletonList(new RouteMapper("t_order", "t_order_0"))));
+ when(routeContext.getRouteUnits()).thenReturn(routeUnits);
+ new ShardingCreateTableStatementValidator().postValidate(shardingRule, sqlStatement, routeContext);
+ }
+
+ @Test(expected = ShardingSphereException.class)
+ public void assertPostValidateCreateTableWithDifferentRouteResultShardingTableForPostgreSQL() {
+ PostgreSQLCreateTableStatement sqlStatement = new PostgreSQLCreateTableStatement();
+ sqlStatement.setTable(new SimpleTableSegment(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"));
+ Collection<RouteUnit> routeUnits = new LinkedList<>();
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_order", "t_order_0"))));
+ when(routeContext.getRouteUnits()).thenReturn(routeUnits);
+ new ShardingCreateTableStatementValidator().postValidate(shardingRule, sqlStatement, routeContext);
+ }
+
+ @Test
+ public void assertPostValidateCreateTableWithSameRouteResultBroadcastTableForPostgreSQL() {
+ PostgreSQLCreateTableStatement sqlStatement = new PostgreSQLCreateTableStatement();
+ sqlStatement.setTable(new SimpleTableSegment(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"));
+ Collection<RouteUnit> routeUnits = new LinkedList<>();
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_config", "t_config"))));
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_1", "ds_1"), Collections.singletonList(new RouteMapper("t_config", "t_config"))));
+ when(routeContext.getRouteUnits()).thenReturn(routeUnits);
+ new ShardingCreateTableStatementValidator().postValidate(shardingRule, sqlStatement, routeContext);
+ }
+
+ @Test(expected = ShardingSphereException.class)
+ public void assertPostValidateCreateTableWithDifferentRouteResultBroadcastTableForPostgreSQL() {
+ PostgreSQLCreateTableStatement sqlStatement = new PostgreSQLCreateTableStatement();
+ sqlStatement.setTable(new SimpleTableSegment(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"));
+ Collection<RouteUnit> routeUnits = new LinkedList<>();
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_config", "t_config"))));
+ when(routeContext.getRouteUnits()).thenReturn(routeUnits);
+ new ShardingCreateTableStatementValidator().postValidate(shardingRule, sqlStatement, routeContext);
+ }
+
+ @Test
+ public void assertPostValidateCreateTableWithSameRouteResultSingleTableForPostgreSQL() {
+ PostgreSQLCreateTableStatement sqlStatement = new PostgreSQLCreateTableStatement();
+ sqlStatement.setTable(new SimpleTableSegment(0, 0, new IdentifierValue("t_single")));
+ Collection<RouteUnit> routeUnits = new LinkedList<>();
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_single", "t_single"))));
+ when(routeContext.getRouteUnits()).thenReturn(routeUnits);
+ new ShardingCreateTableStatementValidator().postValidate(shardingRule, sqlStatement, routeContext);
+ }
+
+ @Test(expected = ShardingSphereException.class)
+ public void assertPostValidateCreateTableWithDifferentRouteResultSingleTableForPostgreSQL() {
+ PostgreSQLCreateTableStatement sqlStatement = new PostgreSQLCreateTableStatement();
+ sqlStatement.setTable(new SimpleTableSegment(0, 0, new IdentifierValue("t_single")));
+ Collection<RouteUnit> routeUnits = new LinkedList<>();
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_single", "t_single"))));
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_1", "ds_1"), Collections.singletonList(new RouteMapper("t_single", "t_single"))));
+ when(routeContext.getRouteUnits()).thenReturn(routeUnits);
+ new ShardingCreateTableStatementValidator().postValidate(shardingRule, sqlStatement, routeContext);
+ }
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java
index 2e28e80..063aedf 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingDropTableStatementValidatorTest.java
@@ -32,9 +32,13 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.Sim
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropTableStatement;
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLDropTableStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl.PostgreSQLDropTableStatement;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.Collection;
@@ -45,13 +49,17 @@ import java.util.List;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+@RunWith(MockitoJUnitRunner.class)
public final class ShardingDropTableStatementValidatorTest {
+ @Mock
private ShardingRule shardingRule;
+ @Mock
+ private RouteContext routeContext;
+
@Before
public void init() {
- shardingRule = mock(ShardingRule.class);
Collection<TableRule> tableRules = new LinkedList<>();
tableRules.add(generateShardingRule("t_order_item"));
tableRules.add(generateShardingRule("t_order"));
@@ -64,7 +72,6 @@ public final class ShardingDropTableStatementValidatorTest {
sqlStatement.getTables().add(new SimpleTableSegment(0, 0, new IdentifierValue("t_order_item")));
SQLStatementContext<DropTableStatement> sqlStatementContext = new CommonSQLStatementContext<>(sqlStatement);
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
- when(schema.getAllTableNames()).thenReturn(Arrays.asList("t_order", "t_order_item"));
ShardingDropTableStatementValidator validator = new ShardingDropTableStatementValidator();
validator.preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), schema);
Collection<RouteUnit> routeUnits = new LinkedList<>();
@@ -97,4 +104,75 @@ public final class ShardingDropTableStatementValidatorTest {
public void clean() {
shardingRule = mock(ShardingRule.class);
}
+
+ @Test
+ public void assertPostValidateDropTableWithSameRouteResultShardingTableForPostgreSQL() {
+ PostgreSQLDropTableStatement sqlStatement = new PostgreSQLDropTableStatement();
+ sqlStatement.getTables().add(new SimpleTableSegment(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"));
+ Collection<RouteUnit> routeUnits = new LinkedList<>();
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_order", "t_order_0"))));
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_1", "ds_1"), Collections.singletonList(new RouteMapper("t_order", "t_order_0"))));
+ when(routeContext.getRouteUnits()).thenReturn(routeUnits);
+ new ShardingDropTableStatementValidator().postValidate(shardingRule, sqlStatement, routeContext);
+ }
+
+ @Test(expected = ShardingSphereException.class)
+ public void assertPostValidateDropTableWithDifferentRouteResultShardingTableForPostgreSQL() {
+ PostgreSQLDropTableStatement sqlStatement = new PostgreSQLDropTableStatement();
+ sqlStatement.getTables().add(new SimpleTableSegment(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"));
+ Collection<RouteUnit> routeUnits = new LinkedList<>();
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_order", "t_order_0"))));
+ when(routeContext.getRouteUnits()).thenReturn(routeUnits);
+ new ShardingDropTableStatementValidator().postValidate(shardingRule, sqlStatement, routeContext);
+ }
+
+ @Test
+ public void assertPostValidateDropTableWithSameRouteResultBroadcastTableForPostgreSQL() {
+ PostgreSQLDropTableStatement sqlStatement = new PostgreSQLDropTableStatement();
+ sqlStatement.getTables().add(new SimpleTableSegment(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"));
+ Collection<RouteUnit> routeUnits = new LinkedList<>();
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_config", "t_config"))));
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_1", "ds_1"), Collections.singletonList(new RouteMapper("t_config", "t_config"))));
+ when(routeContext.getRouteUnits()).thenReturn(routeUnits);
+ new ShardingDropTableStatementValidator().postValidate(shardingRule, sqlStatement, routeContext);
+ }
+
+ @Test(expected = ShardingSphereException.class)
+ public void assertPostValidateDropTableWithDifferentRouteResultBroadcastTableForPostgreSQL() {
+ PostgreSQLDropTableStatement sqlStatement = new PostgreSQLDropTableStatement();
+ sqlStatement.getTables().add(new SimpleTableSegment(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"));
+ Collection<RouteUnit> routeUnits = new LinkedList<>();
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_config", "t_config"))));
+ when(routeContext.getRouteUnits()).thenReturn(routeUnits);
+ new ShardingDropTableStatementValidator().postValidate(shardingRule, sqlStatement, routeContext);
+ }
+
+ @Test
+ public void assertPostValidateDropTableWithSameRouteResultSingleTableForPostgreSQL() {
+ PostgreSQLDropTableStatement sqlStatement = new PostgreSQLDropTableStatement();
+ sqlStatement.getTables().add(new SimpleTableSegment(0, 0, new IdentifierValue("t_single")));
+ Collection<RouteUnit> routeUnits = new LinkedList<>();
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_single", "t_single"))));
+ when(routeContext.getRouteUnits()).thenReturn(routeUnits);
+ new ShardingDropTableStatementValidator().postValidate(shardingRule, sqlStatement, routeContext);
+ }
+
+ @Test(expected = ShardingSphereException.class)
+ public void assertPostValidateDropTableWithDifferentRouteResultSingleTableForPostgreSQL() {
+ PostgreSQLDropTableStatement sqlStatement = new PostgreSQLDropTableStatement();
+ sqlStatement.getTables().add(new SimpleTableSegment(0, 0, new IdentifierValue("t_single")));
+ Collection<RouteUnit> routeUnits = new LinkedList<>();
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_0", "ds_0"), Collections.singletonList(new RouteMapper("t_single", "t_single"))));
+ routeUnits.add(new RouteUnit(new RouteMapper("ds_1", "ds_1"), Collections.singletonList(new RouteMapper("t_single", "t_single"))));
+ when(routeContext.getRouteUnits()).thenReturn(routeUnits);
+ new ShardingDropTableStatementValidator().postValidate(shardingRule, sqlStatement, routeContext);
+ }
}