You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2020/12/13 14:18:29 UTC
[shardingsphere] branch master updated: Modify prevalidate and post
validate method. (#8512)
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 9e1d1d1 Modify prevalidate and post validate method. (#8512)
9e1d1d1 is described below
commit 9e1d1d1361d014d3777e7fc51f2e2bd78c436d8b
Author: coco <co...@gmail.com>
AuthorDate: Sun Dec 13 22:18:00 2020 +0800
Modify prevalidate and post validate method. (#8512)
* test: add unit test.
* refactor: modify prevalidate and postvalidate method.
* refactor: modify the variable name.
* refactor: modify the check conditions.
---
.../validator/dml/ShardingDMLStatementValidator.java | 11 +++++------
.../dml/impl/ShardingInsertStatementValidator.java | 9 ++++++++-
.../dml/impl/ShardingSelectStatementValidator.java | 9 ++++++++-
.../route/engine/type/standard/SubqueryRouteTest.java | 16 ++++++++++++----
4 files changed, 33 insertions(+), 12 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDMLStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDMLStatementValidator.java
index 567260e..890987d 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDMLStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/ShardingDMLStatementValidator.java
@@ -40,8 +40,6 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatemen
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.sql.common.util.SafeNumberOperationUtils;
-import com.google.common.base.Preconditions;
-
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
@@ -75,19 +73,20 @@ public abstract class ShardingDMLStatementValidator<T extends SQLStatement> impl
return allTableNames.isEmpty();
}
- protected void checkSubqueryShardingValues(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext,
+ protected boolean checkSubqueryShardingValues(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext,
final List<Object> parameters, final ShardingSphereSchema schema) {
for (String each : sqlStatementContext.getTablesContext().getTableNames()) {
Optional<TableRule> tableRule = shardingRule.findTableRule(each);
if (tableRule.isPresent() && isRoutingByHint(shardingRule, tableRule.get())
&& !HintManager.getDatabaseShardingValues(each).isEmpty() && !HintManager.getTableShardingValues(each).isEmpty()) {
- return;
+ return false;
}
}
ShardingConditions shardingConditions = createShardingConditions(sqlStatementContext, parameters, schema, shardingRule);
- if (shardingConditions.getConditions().size() > 1) {
- Preconditions.checkState(isSameShardingCondition(shardingRule, shardingConditions), "Sharding value must same with subquery.");
+ if (shardingConditions.getConditions().size() > 1 && !isSameShardingCondition(shardingRule, shardingConditions)) {
+ return true;
}
+ return false;
}
private boolean isRoutingByHint(final ShardingRule shardingRule, final TableRule tableRule) {
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidator.java
index 4876206..5b9ee86 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingInsertStatementValidator.java
@@ -32,6 +32,8 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.InsertStatementHandler;
+import com.google.common.base.Preconditions;
+
import java.util.Collection;
import java.util.List;
import java.util.Optional;
@@ -40,6 +42,8 @@ import java.util.Optional;
* Sharding insert statement validator.
*/
public final class ShardingInsertStatementValidator extends ShardingDMLStatementValidator<InsertStatement> {
+
+ private boolean needCheckDatabaseInstance;
@Override
public void preValidate(final ShardingRule shardingRule, final SQLStatementContext<InsertStatement> sqlStatementContext,
@@ -63,7 +67,7 @@ public final class ShardingInsertStatementValidator extends ShardingDMLStatement
throw new ShardingSphereException("The table inserted and the table selected must be the same or bind tables.");
}
if (insertSelectSegment.isPresent() && isNeedMergeShardingValues(sqlStatementContext, shardingRule)) {
- checkSubqueryShardingValues(shardingRule, sqlStatementContext, parameters, schema);
+ needCheckDatabaseInstance = checkSubqueryShardingValues(shardingRule, sqlStatementContext, parameters, schema);
}
}
@@ -90,5 +94,8 @@ public final class ShardingInsertStatementValidator extends ShardingDMLStatement
@Override
public void postValidate(final InsertStatement sqlStatement, final RouteContext routeContext) {
+ if (needCheckDatabaseInstance) {
+ Preconditions.checkState(routeContext.isSingleRouting(), "Sharding value must same with subquery.");
+ }
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingSelectStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingSelectStatementValidator.java
index 4cb7ee0..5dcec54 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingSelectStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/dml/impl/ShardingSelectStatementValidator.java
@@ -24,6 +24,8 @@ import org.apache.shardingsphere.sharding.route.engine.validator.dml.ShardingDML
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
+import com.google.common.base.Preconditions;
+
import java.util.List;
/**
@@ -31,15 +33,20 @@ import java.util.List;
*/
public final class ShardingSelectStatementValidator extends ShardingDMLStatementValidator<SelectStatement> {
+ private boolean needCheckDatabaseInstance;
+
@Override
public void preValidate(final ShardingRule shardingRule, final SQLStatementContext<SelectStatement> sqlStatementContext,
final List<Object> parameters, final ShardingSphereSchema schema) {
if (isNeedMergeShardingValues(sqlStatementContext, shardingRule)) {
- checkSubqueryShardingValues(shardingRule, sqlStatementContext, parameters, schema);
+ needCheckDatabaseInstance = checkSubqueryShardingValues(shardingRule, sqlStatementContext, parameters, schema);
}
}
@Override
public void postValidate(final SelectStatement sqlStatement, final RouteContext routeContext) {
+ if (needCheckDatabaseInstance) {
+ Preconditions.checkState(routeContext.isSingleRouting(), "Sharding value must same with subquery.");
+ }
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/SubqueryRouteTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/SubqueryRouteTest.java
index 5d03abc..14714a9 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/SubqueryRouteTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/SubqueryRouteTest.java
@@ -25,7 +25,7 @@ import java.util.List;
public final class SubqueryRouteTest extends AbstractSQLRouteTest {
- @Test(expected = IllegalStateException.class)
+ @Test
public void assertOneTableError() {
String sql = "select (select max(id) from t_order b where b.user_id =? ) from t_order a where user_id = ? ";
List<Object> parameters = new LinkedList<>();
@@ -61,7 +61,7 @@ public final class SubqueryRouteTest extends AbstractSQLRouteTest {
assertRoute(sql, parameters);
}
- @Test(expected = IllegalStateException.class)
+ @Test
public void assertBindingTableWithDifferentValue() {
String sql = "select (select max(id) from t_order_item b where b.user_id = ? ) from t_order a where user_id = ? ";
List<Object> parameters = new LinkedList<>();
@@ -91,7 +91,7 @@ public final class SubqueryRouteTest extends AbstractSQLRouteTest {
assertRoute(sql, parameters);
}
- @Test(expected = IllegalStateException.class)
+ @Test
public void assertSubqueryInSubqueryError() {
List<Object> parameters = new LinkedList<>();
parameters.add(11);
@@ -115,7 +115,7 @@ public final class SubqueryRouteTest extends AbstractSQLRouteTest {
assertRoute(sql, parameters);
}
- @Test(expected = IllegalStateException.class)
+ @Test
public void assertSubqueryInFromError() {
String sql = "select status from t_order b join (select user_id,status from t_order b where b.user_id =?) c on b.user_id = c.user_id where b.user_id =? ";
List<Object> parameters = new LinkedList<>();
@@ -162,4 +162,12 @@ public final class SubqueryRouteTest extends AbstractSQLRouteTest {
assertRoute(sql, parameters);
hintManager.close();
}
+
+ @Test
+ public void assertSubqueryWithOneInstance() {
+ String sql = "select count(*) from t_order where user_id =?";
+ List<Object> parameters = new LinkedList<>();
+ parameters.add(1);
+ assertRoute(sql, parameters);
+ }
}