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 e786a9c8fde Use SQLFederationDeciderEngine and remove useless sql
federation route logic (#19943)
e786a9c8fde is described below
commit e786a9c8fde7ec3dfeb618e2ac9df6054665ef5d
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Sun Aug 7 20:12:00 2022 +0800
Use SQLFederationDeciderEngine and remove useless sql federation route
logic (#19943)
---
.../decider/ShardingSQLFederationDecider.java | 3 +
.../ShardingSQLRewriteContextDecorator.java | 3 -
.../engine/type/ShardingRouteEngineFactory.java | 25 ----
.../federated/ShardingFederatedRoutingEngine.java | 57 --------
.../ShardingSQLRewriteContextDecoratorTest.java | 24 +---
.../type/ShardingRouteEngineFactoryTest.java | 18 ---
.../ShardingFederatedRoutingEngineTest.java | 160 ---------------------
.../decider/engine/SQLFederationDeciderEngine.java | 15 +-
.../engine/SQLFederationDeciderEngineTest.java | 21 ++-
.../infra/route/context/RouteContext.java | 4 -
.../statement/ShardingSpherePreparedStatement.java | 30 ++--
.../core/statement/ShardingSphereStatement.java | 33 +++--
.../decider/SingleTableSQLFederationDecider.java | 3 +
.../engine/SingleTableStandardRouteEngine.java | 15 --
.../route/SingleTableSQLRouterTest.java | 4 -
.../engine/SingleTableStandardRouteEngineTest.java | 34 +----
.../jdbc/JDBCDatabaseCommunicationEngine.java | 21 +--
.../vertx/VertxDatabaseCommunicationEngine.java | 15 +-
.../handler/distsql/rul/sql/PreviewHandler.java | 22 ++-
.../jdbc/JDBCDatabaseCommunicationEngineTest.java | 18 +--
.../scenario/sharding/case/dml/select.xml | 40 ------
21 files changed, 138 insertions(+), 427 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/decider/ShardingSQLFederationDecider.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/decider/ShardingSQLFederationDecider.java
index 805e08e0285..d4313d07df1 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/decider/ShardingSQLFederationDecider.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/decider/ShardingSQLFederationDecider.java
@@ -42,6 +42,9 @@ public final class ShardingSQLFederationDecider implements
SQLFederationDecider<
public void decide(final SQLFederationDeciderContext deciderContext, final
LogicSQL logicSQL, final ShardingSphereDatabase database, final ShardingRule
rule, final ConfigurationProperties props) {
SelectStatementContext select = (SelectStatementContext)
logicSQL.getSqlStatementContext();
Collection<String> tableNames =
rule.getShardingLogicTableNames(select.getTablesContext().getTableNames());
+ if (tableNames.isEmpty()) {
+ return;
+ }
addTableDataNodes(deciderContext, rule, tableNames);
ShardingConditions shardingConditions =
createShardingConditions(logicSQL, database, rule);
if (select.getPaginationContext().isHasPagination() ||
(shardingConditions.isNeedMerge() &&
shardingConditions.isSameShardingCondition())) {
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java
index aeb320ad8aa..69a9a3f936f 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java
@@ -39,9 +39,6 @@ public final class ShardingSQLRewriteContextDecorator
implements SQLRewriteConte
@SuppressWarnings("rawtypes")
@Override
public void decorate(final ShardingRule shardingRule, final
ConfigurationProperties props, final SQLRewriteContext sqlRewriteContext, final
RouteContext routeContext) {
- if (routeContext.isFederated()) {
- return;
- }
if (!sqlRewriteContext.getParameters().isEmpty()) {
Collection<ParameterRewriter> parameterRewriters = new
ShardingParameterRewriterBuilder(shardingRule,
routeContext, sqlRewriteContext.getSchemas(),
sqlRewriteContext.getSqlStatementContext()).getParameterRewriters();
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java
index cadc34b7872..f6058d2a78c 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java
@@ -21,11 +21,9 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import
org.apache.shardingsphere.infra.binder.statement.ddl.CloseStatementContext;
-import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.binder.type.CursorAvailable;
import org.apache.shardingsphere.infra.binder.type.TableAvailable;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.sharding.route.engine.condition.ShardingCondition;
import
org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditions;
@@ -35,7 +33,6 @@ import
org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingDa
import
org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingInstanceBroadcastRoutingEngine;
import
org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingTableBroadcastRoutingEngine;
import
org.apache.shardingsphere.sharding.route.engine.type.complex.ShardingComplexRoutingEngine;
-import
org.apache.shardingsphere.sharding.route.engine.type.federated.ShardingFederatedRoutingEngine;
import
org.apache.shardingsphere.sharding.route.engine.type.ignore.ShardingIgnoreRoutingEngine;
import
org.apache.shardingsphere.sharding.route.engine.type.standard.ShardingStandardRoutingEngine;
import
org.apache.shardingsphere.sharding.route.engine.type.unicast.ShardingUnicastRoutingEngine;
@@ -219,9 +216,6 @@ public final class ShardingRouteEngineFactory {
final
SQLStatementContext<?> sqlStatementContext, final ShardingConditions
shardingConditions,
final
ConfigurationProperties props, final Collection<String> tableNames) {
boolean allBindingTables = tableNames.size() > 1 &&
shardingRule.isAllBindingTables(database, sqlStatementContext, tableNames);
- if (isShardingFederatedQuery(shardingRule, sqlStatementContext,
shardingConditions, props, tableNames, allBindingTables)) {
- return new ShardingFederatedRoutingEngine(tableNames);
- }
if (isShardingStandardQuery(shardingRule, tableNames,
allBindingTables)) {
return new
ShardingStandardRoutingEngine(getLogicTableName(shardingConditions,
tableNames), shardingConditions, props);
}
@@ -241,23 +235,4 @@ public final class ShardingRouteEngineFactory {
private static boolean isShardingStandardQuery(final ShardingRule
shardingRule, final Collection<String> tableNames, final boolean
allBindingTables) {
return 1 == tableNames.size() &&
shardingRule.isAllShardingTables(tableNames) || allBindingTables;
}
-
- private static boolean isShardingFederatedQuery(final ShardingRule
shardingRule, final SQLStatementContext<?> sqlStatementContext, final
ShardingConditions shardingConditions,
- final
ConfigurationProperties props, final Collection<String> tableNames, final
boolean allBindingTables) {
- boolean sqlFederationEnabled =
props.getValue(ConfigurationPropertyKey.SQL_FEDERATION_ENABLED);
- if (!sqlFederationEnabled || !(sqlStatementContext instanceof
SelectStatementContext)) {
- return false;
- }
- SelectStatementContext select = (SelectStatementContext)
sqlStatementContext;
- if (select.getPaginationContext().isHasPagination() ||
(shardingConditions.isNeedMerge() &&
shardingConditions.isSameShardingCondition())) {
- return false;
- }
- if (select.isContainsSubquery() || select.isContainsHaving() ||
select.isContainsCombine() || select.isContainsPartialDistinctAggregation()) {
- return true;
- }
- if (!select.isContainsJoinQuery() ||
shardingRule.isAllTablesInSameDataSource(tableNames)) {
- return false;
- }
- return tableNames.size() > 1 && !allBindingTables;
- }
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/federated/ShardingFederatedRoutingEngine.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/federated/ShardingFederatedRoutingEngine.java
deleted file mode 100644
index 8869089a484..00000000000
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/federated/ShardingFederatedRoutingEngine.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * 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.sharding.route.engine.type.federated;
-
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.datanode.DataNode;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.route.context.RouteMapper;
-import
org.apache.shardingsphere.sharding.route.engine.type.ShardingRouteEngine;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sharding.rule.TableRule;
-
-import java.util.Collection;
-import java.util.Collections;
-
-/**
- * Sharding federated routing engine.
- */
-@RequiredArgsConstructor
-public final class ShardingFederatedRoutingEngine implements
ShardingRouteEngine {
-
- private final Collection<String> logicTables;
-
- @Override
- public RouteContext route(final ShardingRule shardingRule) {
- RouteContext result = new RouteContext();
- for (String each : logicTables) {
- fillRouteContext(result, shardingRule, each);
- }
- result.setFederated(true);
- return result;
- }
-
- private void fillRouteContext(final RouteContext routeContext, final
ShardingRule shardingRule, final String logicTableName) {
- TableRule tableRule = shardingRule.getTableRule(logicTableName);
- for (DataNode each : tableRule.getActualDataNodes()) {
- RouteMapper dataSource = new RouteMapper(each.getDataSourceName(),
each.getDataSourceName());
- RouteMapper table = new RouteMapper(logicTableName,
each.getTableName());
- routeContext.putRouteUnit(dataSource,
Collections.singletonList(table));
- }
- }
-}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
index 2275b4f864e..30908a5ad82 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
@@ -26,7 +26,6 @@ import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
-import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -34,28 +33,13 @@ import static org.mockito.Mockito.when;
public final class ShardingSQLRewriteContextDecoratorTest {
@Test
- public void assertDecorateForRouteContextWhenIsFederated() {
+ public void assertDecorate() {
+ List<Object> parameters = new ArrayList<>();
+ parameters.add(new Object());
ShardingSQLRewriteContextDecorator decorator = new
ShardingSQLRewriteContextDecorator();
SQLRewriteContext sqlRewriteContext = mock(SQLRewriteContext.class);
+ when(sqlRewriteContext.getParameters()).thenReturn(parameters);
RouteContext routeContext = mock(RouteContext.class);
- when(routeContext.isFederated()).thenReturn(true);
- decorator.decorate(mock(ShardingRule.class),
mock(ConfigurationProperties.class), sqlRewriteContext, routeContext);
- assertTrue(sqlRewriteContext.getSchemas().isEmpty());
- assertNull(sqlRewriteContext.getSqlStatementContext());
- assertTrue(sqlRewriteContext.getParameters().isEmpty());
- assertNull(sqlRewriteContext.getParameterBuilder());
- assertTrue(sqlRewriteContext.getSqlTokens().isEmpty());
- }
-
- @Test
- public void assertDecorateForRouteContextWhenNotFederated() {
- List<Object> dummy = new ArrayList<>();
- dummy.add(new Object());
- ShardingSQLRewriteContextDecorator decorator = new
ShardingSQLRewriteContextDecorator();
- SQLRewriteContext sqlRewriteContext = mock(SQLRewriteContext.class);
- when(sqlRewriteContext.getParameters()).thenReturn(dummy);
- RouteContext routeContext = mock(RouteContext.class);
- when(routeContext.isFederated()).thenReturn(false);
decorator.decorate(mock(ShardingRule.class),
mock(ConfigurationProperties.class), sqlRewriteContext, routeContext);
assertTrue(sqlRewriteContext.getSqlTokens().isEmpty());
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactoryTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactoryTest.java
index dff72f7a751..4a861f54d82 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactoryTest.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactoryTest.java
@@ -34,7 +34,6 @@ import
org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingDa
import
org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingInstanceBroadcastRoutingEngine;
import
org.apache.shardingsphere.sharding.route.engine.type.broadcast.ShardingTableBroadcastRoutingEngine;
import
org.apache.shardingsphere.sharding.route.engine.type.complex.ShardingComplexRoutingEngine;
-import
org.apache.shardingsphere.sharding.route.engine.type.federated.ShardingFederatedRoutingEngine;
import
org.apache.shardingsphere.sharding.route.engine.type.ignore.ShardingIgnoreRoutingEngine;
import
org.apache.shardingsphere.sharding.route.engine.type.standard.ShardingStandardRoutingEngine;
import
org.apache.shardingsphere.sharding.route.engine.type.unicast.ShardingUnicastRoutingEngine;
@@ -300,21 +299,6 @@ public final class ShardingRouteEngineFactoryTest {
assertThat(actual, instanceOf(ShardingUnicastRoutingEngine.class));
}
- @Test
- public void assertNewInstanceForSubqueryWithDifferentConditions() {
- SelectStatementContext sqlStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
- tableNames.add("t_order");
-
when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(tableNames);
- when(sqlStatementContext.isContainsSubquery()).thenReturn(true);
- ShardingRule shardingRule = mock(ShardingRule.class,
RETURNS_DEEP_STUBS);
-
when(shardingRule.getShardingRuleTableNames(tableNames)).thenReturn(tableNames);
-
when(shardingRule.getTableRule("t_order").getActualDataSourceNames()).thenReturn(Arrays.asList("ds_0",
"ds_1"));
- when(shardingConditions.isNeedMerge()).thenReturn(true);
- when(shardingConditions.isSameShardingCondition()).thenReturn(false);
- ShardingRouteEngine actual =
ShardingRouteEngineFactory.newInstance(shardingRule, database,
sqlStatementContext, shardingConditions,
createFederationConfigurationProperties());
- assertThat(actual, instanceOf(ShardingFederatedRoutingEngine.class));
- }
-
private ConfigurationProperties createFederationConfigurationProperties() {
Properties props = new Properties();
props.setProperty(ConfigurationPropertyKey.SQL_FEDERATION_ENABLED.getKey(),
String.valueOf(Boolean.TRUE));
@@ -330,8 +314,6 @@ public final class ShardingRouteEngineFactoryTest {
when(shardingRule.getShardingLogicTableNames(tableNames)).thenReturn(tableNames);
when(shardingRule.getTableRule("t_order").getActualDataSourceNames()).thenReturn(Arrays.asList("ds_0",
"ds_1"));
when(shardingRule.isAllShardingTables(Collections.singletonList("t_order"))).thenReturn(true);
- when(shardingConditions.isNeedMerge()).thenReturn(true);
- when(shardingConditions.isSameShardingCondition()).thenReturn(true);
ShardingRouteEngine actual =
ShardingRouteEngineFactory.newInstance(shardingRule, database,
sqlStatementContext, shardingConditions,
createFederationConfigurationProperties());
assertThat(actual, instanceOf(ShardingStandardRoutingEngine.class));
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/federated/ShardingFederatedRoutingEngineTest.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/federated/ShardingFederatedRoutingEngineTest.java
deleted file mode 100644
index 257223a7a17..00000000000
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/federated/ShardingFederatedRoutingEngineTest.java
+++ /dev/null
@@ -1,160 +0,0 @@
-/*
- * 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.sharding.route.engine.type.federated;
-
-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.fixture.AbstractRoutingEngineTest;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.junit.Test;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.mock;
-
-public final class ShardingFederatedRoutingEngineTest extends
AbstractRoutingEngineTest {
-
- @Test
- public void assertRouteByNonConditions() {
- ShardingFederatedRoutingEngine federatedRoutingEngine =
createShardingFederatedRoutingEngine(Collections.singletonList("t_order"));
- RouteContext actual =
federatedRoutingEngine.route(createBasedShardingRule());
- List<RouteUnit> routeUnits = new ArrayList<>(actual.getRouteUnits());
- assertThat(actual.getRouteUnits().size(), is(2));
- RouteUnit routeUnit1 = routeUnits.get(0);
- assertThat(routeUnit1.getDataSourceMapper().getActualName(),
is("ds_0"));
- assertThat(routeUnit1.getTableMappers().size(), is(2));
- Collection<RouteMapper> tableMappers1 = routeUnit1.getTableMappers();
- Iterator<RouteMapper> iterator1 = tableMappers1.iterator();
- RouteMapper tableMapper1 = iterator1.next();
- assertThat(tableMapper1.getActualName(), is("t_order_0"));
- assertThat(tableMapper1.getLogicName(), is("t_order"));
- RouteMapper tableMapper2 = iterator1.next();
- assertThat(tableMapper2.getActualName(), is("t_order_1"));
- assertThat(tableMapper2.getLogicName(), is("t_order"));
- RouteUnit routeUnit2 = routeUnits.get(1);
- assertThat(routeUnit2.getDataSourceMapper().getActualName(),
is("ds_1"));
- assertThat(routeUnit2.getTableMappers().size(), is(2));
- Collection<RouteMapper> tableMappers2 = routeUnit2.getTableMappers();
- Iterator<RouteMapper> iterator2 = tableMappers2.iterator();
- RouteMapper tableMapper3 = iterator2.next();
- assertThat(tableMapper3.getActualName(), is("t_order_0"));
- assertThat(tableMapper3.getLogicName(), is("t_order"));
- RouteMapper tableMapper4 = iterator2.next();
- assertThat(tableMapper4.getActualName(), is("t_order_1"));
- assertThat(tableMapper4.getLogicName(), is("t_order"));
- }
-
- @Test
- public void assertRouteByShardingConditions() {
- ShardingFederatedRoutingEngine federatedRoutingEngine =
createShardingFederatedRoutingEngine(Collections.singletonList("t_order"));
- RouteContext actual =
federatedRoutingEngine.route(createBasedShardingRule());
- assertThat(actual.getRouteUnits().size(), is(2));
- List<RouteUnit> routeUnits = new ArrayList<>(actual.getRouteUnits());
- assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(),
is("ds_0"));
- assertThat(routeUnits.get(0).getDataSourceMapper().getLogicName(),
is("ds_0"));
- assertThat(routeUnits.get(0).getTableMappers().size(), is(2));
- List<RouteMapper> firstRouteMappers = new
ArrayList<>(routeUnits.get(0).getTableMappers());
- assertThat(firstRouteMappers.get(0).getActualName(), is("t_order_0"));
- assertThat(firstRouteMappers.get(0).getLogicName(), is("t_order"));
- assertThat(firstRouteMappers.get(1).getActualName(), is("t_order_1"));
- assertThat(firstRouteMappers.get(1).getLogicName(), is("t_order"));
- assertThat(routeUnits.get(1).getDataSourceMapper().getActualName(),
is("ds_1"));
- assertThat(routeUnits.get(1).getDataSourceMapper().getLogicName(),
is("ds_1"));
- assertThat(routeUnits.get(1).getTableMappers().size(), is(2));
- List<RouteMapper> secondRouteMappers = new
ArrayList<>(routeUnits.get(1).getTableMappers());
- assertThat(secondRouteMappers.get(0).getActualName(), is("t_order_0"));
- assertThat(secondRouteMappers.get(0).getLogicName(), is("t_order"));
- assertThat(secondRouteMappers.get(1).getActualName(), is("t_order_1"));
- assertThat(secondRouteMappers.get(1).getLogicName(), is("t_order"));
- }
-
- @Test
- public void assertRoutingForBindingTables() {
- ShardingFederatedRoutingEngine federatedRoutingEngine =
createShardingFederatedRoutingEngine(Arrays.asList("t_order", "t_order_item"));
- RouteContext actual =
federatedRoutingEngine.route(createBindingShardingRule());
- assertThat(actual.getRouteUnits().size(), is(2));
- List<RouteUnit> routeUnits = new ArrayList<>(actual.getRouteUnits());
- assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(),
is("ds_0"));
- assertThat(routeUnits.get(0).getDataSourceMapper().getLogicName(),
is("ds_0"));
- assertThat(routeUnits.get(0).getTableMappers().size(), is(4));
- List<RouteMapper> firstRouteMappers = new
ArrayList<>(routeUnits.get(0).getTableMappers());
- assertThat(firstRouteMappers.get(0).getActualName(), is("t_order_0"));
- assertThat(firstRouteMappers.get(0).getLogicName(), is("t_order"));
- assertThat(firstRouteMappers.get(1).getActualName(), is("t_order_1"));
- assertThat(firstRouteMappers.get(1).getLogicName(), is("t_order"));
- assertThat(firstRouteMappers.get(2).getActualName(),
is("t_order_item_0"));
- assertThat(firstRouteMappers.get(2).getLogicName(),
is("t_order_item"));
- assertThat(firstRouteMappers.get(3).getActualName(),
is("t_order_item_1"));
- assertThat(firstRouteMappers.get(3).getLogicName(),
is("t_order_item"));
- assertThat(routeUnits.get(1).getDataSourceMapper().getActualName(),
is("ds_1"));
- assertThat(routeUnits.get(1).getDataSourceMapper().getLogicName(),
is("ds_1"));
- assertThat(routeUnits.get(1).getTableMappers().size(), is(4));
- List<RouteMapper> secondRouteMappers = new
ArrayList<>(routeUnits.get(1).getTableMappers());
- assertThat(secondRouteMappers.get(0).getActualName(), is("t_order_0"));
- assertThat(secondRouteMappers.get(0).getLogicName(), is("t_order"));
- assertThat(secondRouteMappers.get(1).getActualName(), is("t_order_1"));
- assertThat(secondRouteMappers.get(1).getLogicName(), is("t_order"));
- assertThat(secondRouteMappers.get(2).getActualName(),
is("t_order_item_0"));
- assertThat(secondRouteMappers.get(2).getLogicName(),
is("t_order_item"));
- assertThat(secondRouteMappers.get(3).getActualName(),
is("t_order_item_1"));
- assertThat(secondRouteMappers.get(3).getLogicName(),
is("t_order_item"));
- }
-
- @Test
- public void assertRoutingForNonLogicTable() {
- ShardingFederatedRoutingEngine complexRoutingEngine =
createShardingFederatedRoutingEngine(Collections.emptyList());
- RouteContext actual =
complexRoutingEngine.route(mock(ShardingRule.class));
- assertTrue(actual.getOriginalDataNodes().isEmpty());
- assertTrue(actual.getRouteUnits().isEmpty());
- assertTrue(actual.getRouteStageContexts().isEmpty());
- assertTrue(actual.isFederated());
- }
-
- @Test
- public void assertRoutingForShardingTableJoinBroadcastTable() {
- ShardingFederatedRoutingEngine federatedRoutingEngine =
createShardingFederatedRoutingEngine(Collections.singletonList("t_config"));
- RouteContext actual =
federatedRoutingEngine.route(createBroadcastShardingRule());
- List<RouteUnit> routeUnits = new ArrayList<>(actual.getRouteUnits());
- assertThat(actual.getRouteUnits().size(), is(2));
- assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(),
is("ds_0"));
- assertThat(routeUnits.get(0).getDataSourceMapper().getLogicName(),
is("ds_0"));
- assertThat(routeUnits.get(0).getTableMappers().size(), is(1));
- List<RouteMapper> firstRouteMappers = new
ArrayList<>(routeUnits.get(0).getTableMappers());
- assertThat(firstRouteMappers.get(0).getActualName(), is("t_config"));
- assertThat(firstRouteMappers.get(0).getLogicName(), is("t_config"));
- assertThat(routeUnits.get(1).getDataSourceMapper().getActualName(),
is("ds_1"));
- assertThat(routeUnits.get(1).getDataSourceMapper().getLogicName(),
is("ds_1"));
- assertThat(routeUnits.get(1).getTableMappers().size(), is(1));
- List<RouteMapper> secondRouteMappers = new
ArrayList<>(routeUnits.get(0).getTableMappers());
- assertThat(secondRouteMappers.get(0).getActualName(), is("t_config"));
- assertThat(secondRouteMappers.get(0).getLogicName(), is("t_config"));
- }
-
- private ShardingFederatedRoutingEngine
createShardingFederatedRoutingEngine(final Collection<String> logicTables) {
- return new ShardingFederatedRoutingEngine(logicTables);
- }
-}
diff --git
a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/decider/engine/SQLFederationDeciderEngine.java
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/decider/engine/SQLFederationDeciderEngine.java
index 09b7fe40e1f..3f02e35f592 100644
---
a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/decider/engine/SQLFederationDeciderEngine.java
+++
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/decider/engine/SQLFederationDeciderEngine.java
@@ -21,10 +21,12 @@ import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.decider.SQLFederationDecider;
import
org.apache.shardingsphere.infra.binder.decider.SQLFederationDeciderFactory;
import
org.apache.shardingsphere.infra.binder.decider.context.SQLFederationDeciderContext;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import
org.apache.shardingsphere.infra.metadata.database.schema.util.SystemSchemaUtil;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import java.util.Collection;
@@ -56,8 +58,14 @@ public final class SQLFederationDeciderEngine {
@SuppressWarnings({"unchecked", "rawtypes"})
public SQLFederationDeciderContext decide(final LogicSQL logicSQL, final
ShardingSphereDatabase database) {
SQLFederationDeciderContext result = new SQLFederationDeciderContext();
+ SQLStatementContext<?> sqlStatementContext =
logicSQL.getSqlStatementContext();
+ // TODO move this logic to SQLFederationDecider implement class when
we remove sqlFederationEnabled
+ if (isSelectStatementContainsSystemSchema(sqlStatementContext,
database)) {
+ result.setUseSQLFederation(true);
+ return result;
+ }
boolean sqlFederationEnabled =
props.getValue(ConfigurationPropertyKey.SQL_FEDERATION_ENABLED);
- if (!sqlFederationEnabled || !(logicSQL.getSqlStatementContext()
instanceof SelectStatementContext)) {
+ if (!sqlFederationEnabled || !(sqlStatementContext instanceof
SelectStatementContext)) {
return result;
}
for (Entry<ShardingSphereRule, SQLFederationDecider> entry :
deciders.entrySet()) {
@@ -67,4 +75,9 @@ public final class SQLFederationDeciderEngine {
}
return result;
}
+
+ private boolean isSelectStatementContainsSystemSchema(final
SQLStatementContext<?> sqlStatementContext, final ShardingSphereDatabase
database) {
+ return sqlStatementContext instanceof SelectStatementContext
+ &&
SystemSchemaUtil.containsSystemSchema(sqlStatementContext.getDatabaseType(),
sqlStatementContext.getTablesContext().getSchemaNames(), database);
+ }
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/decider/engine/SQLFederationDeciderEngineTest.java
b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/decider/engine/SQLFederationDeciderEngineTest.java
index 112ac915164..99d603a6cd4 100644
---
a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/decider/engine/SQLFederationDeciderEngineTest.java
+++
b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/decider/engine/SQLFederationDeciderEngineTest.java
@@ -27,6 +27,7 @@ import
org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.database.DefaultDatabase;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.metadata.database.resource.ShardingSphereResource;
import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
@@ -42,9 +43,23 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public final class SQLFederationDeciderEngineTest {
+ @Test
+ public void assertDecideWhenSelectStatementContainsSystemSchema() {
+ SQLFederationDeciderEngine deciderEngine = new
SQLFederationDeciderEngine(Collections.emptyList(), new
ConfigurationProperties(new Properties()));
+ SelectStatementContext sqlStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
+ when(sqlStatementContext.getDatabaseType()).thenReturn(new
MySQLDatabaseType());
+
when(sqlStatementContext.getTablesContext().getSchemaNames()).thenReturn(Collections.singletonList("information_schema"));
+ LogicSQL logicSQL = new LogicSQL(sqlStatementContext, "",
Collections.emptyList());
+ ShardingSphereDatabase database = new
ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME,
+ mock(DatabaseType.class), mock(ShardingSphereResource.class,
RETURNS_DEEP_STUBS), mock(ShardingSphereRuleMetaData.class),
Collections.emptyMap());
+ SQLFederationDeciderContext actual = deciderEngine.decide(logicSQL,
database);
+ assertTrue(actual.isUseSQLFederation());
+ }
+
@Test
public void assertDecideWhenNotConfigSqlFederationEnabled() {
Collection<ShardingSphereRule> rules = Collections.singletonList(new
SQLFederationDeciderRuleMatchFixture());
@@ -75,7 +90,7 @@ public final class SQLFederationDeciderEngineTest {
Properties props = new Properties();
props.put(ConfigurationPropertyKey.SQL_FEDERATION_ENABLED.getKey(),
true);
SQLFederationDeciderEngine deciderEngine = new
SQLFederationDeciderEngine(rules, new ConfigurationProperties(props));
- LogicSQL logicSQL = new LogicSQL(mock(SelectStatementContext.class),
"", Collections.emptyList());
+ LogicSQL logicSQL = new LogicSQL(mock(SelectStatementContext.class,
RETURNS_DEEP_STUBS), "", Collections.emptyList());
ShardingSphereDatabase database = new
ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME,
mock(DatabaseType.class), mock(ShardingSphereResource.class,
RETURNS_DEEP_STUBS), new ShardingSphereRuleMetaData(rules),
Collections.emptyMap());
SQLFederationDeciderContext actual = deciderEngine.decide(logicSQL,
database);
@@ -88,7 +103,7 @@ public final class SQLFederationDeciderEngineTest {
Properties props = new Properties();
props.put(ConfigurationPropertyKey.SQL_FEDERATION_ENABLED.getKey(),
true);
SQLFederationDeciderEngine deciderEngine = new
SQLFederationDeciderEngine(rules, new ConfigurationProperties(props));
- LogicSQL logicSQL = new LogicSQL(mock(SelectStatementContext.class),
"", Collections.emptyList());
+ LogicSQL logicSQL = new LogicSQL(mock(SelectStatementContext.class,
RETURNS_DEEP_STUBS), "", Collections.emptyList());
ShardingSphereDatabase database = new
ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME,
mock(DatabaseType.class), mock(ShardingSphereResource.class,
RETURNS_DEEP_STUBS), new ShardingSphereRuleMetaData(rules),
Collections.emptyMap());
SQLFederationDeciderContext actual = deciderEngine.decide(logicSQL,
database);
@@ -101,7 +116,7 @@ public final class SQLFederationDeciderEngineTest {
Properties props = new Properties();
props.put(ConfigurationPropertyKey.SQL_FEDERATION_ENABLED.getKey(),
true);
SQLFederationDeciderEngine deciderEngine = new
SQLFederationDeciderEngine(rules, new ConfigurationProperties(props));
- LogicSQL logicSQL = new LogicSQL(mock(SelectStatementContext.class),
"", Collections.emptyList());
+ LogicSQL logicSQL = new LogicSQL(mock(SelectStatementContext.class,
RETURNS_DEEP_STUBS), "", Collections.emptyList());
ShardingSphereDatabase database = new
ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME,
mock(DatabaseType.class), mock(ShardingSphereResource.class,
RETURNS_DEEP_STUBS), new ShardingSphereRuleMetaData(rules),
Collections.emptyMap());
SQLFederationDeciderContext actual = deciderEngine.decide(logicSQL,
database);
diff --git
a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/context/RouteContext.java
b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/context/RouteContext.java
index bede50f331e..ce6dc622989 100644
---
a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/context/RouteContext.java
+++
b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/context/RouteContext.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.infra.route.context;
import lombok.Getter;
-import lombok.Setter;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
@@ -46,9 +45,6 @@ public final class RouteContext {
private final Map<Class<? extends ShardingSphereRule>, RouteStageContext>
routeStageContexts = new LinkedHashMap<>();
- @Setter
- private boolean isFederated;
-
/**
* Judge is route for single database and table only or not.
*
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
index 0e9ca26ccf8..72616e5542b 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSpherePreparedStatement.java
@@ -33,14 +33,16 @@ import
org.apache.shardingsphere.driver.jdbc.exception.SQLExceptionErrorCode;
import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory;
import org.apache.shardingsphere.infra.binder.aware.ParameterAware;
+import
org.apache.shardingsphere.infra.binder.decider.context.SQLFederationDeciderContext;
+import
org.apache.shardingsphere.infra.binder.decider.engine.SQLFederationDeciderEngine;
import
org.apache.shardingsphere.infra.binder.segment.insert.keygen.GeneratedKeyContext;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.context.kernel.KernelProcessor;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
-import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.executor.check.SQLCheckEngine;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroup;
@@ -68,12 +70,14 @@ import org.apache.shardingsphere.infra.hint.HintManager;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.merge.MergeEngine;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import
org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.RawExecutionRule;
import
org.apache.shardingsphere.infra.rule.identifier.type.StorageConnectorReusableRule;
+import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
@@ -146,6 +150,8 @@ public final class ShardingSpherePreparedStatement extends
AbstractPreparedState
private TrafficContext trafficContext;
+ private SQLFederationDeciderContext deciderContext;
+
public ShardingSpherePreparedStatement(final ShardingSphereConnection
connection, final String sql) throws SQLException {
this(connection, sql, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT, false);
}
@@ -210,11 +216,11 @@ public final class ShardingSpherePreparedStatement
extends AbstractPreparedState
JDBCExecutionUnit executionUnit =
createTrafficExecutionUnit(trafficContext, logicSQL);
return executor.getTrafficExecutor().execute(executionUnit,
(statement, sql) -> ((PreparedStatement) statement).executeQuery());
}
- // TODO move federation route logic to binder
- executionContext = createExecutionContext(logicSQL);
- if (executionContext.getRouteContext().isFederated()) {
+ deciderContext = decide(logicSQL,
metaDataContexts.getMetaData().getProps(),
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()));
+ if (deciderContext.isUseSQLFederation()) {
return executeFederationQuery(logicSQL);
}
+ executionContext = createExecutionContext(logicSQL);
List<QueryResult> queryResults = executeQuery0();
MergedResult mergedResult = mergeQuery(queryResults);
result = new ShardingSphereResultSet(getShardingSphereResultSet(),
mergedResult, this, executionContext);
@@ -228,6 +234,11 @@ public final class ShardingSpherePreparedStatement extends
AbstractPreparedState
return result;
}
+ private static SQLFederationDeciderContext decide(final LogicSQL logicSQL,
final ConfigurationProperties props, final ShardingSphereDatabase database) {
+ SQLFederationDeciderEngine deciderEngine = new
SQLFederationDeciderEngine(database.getRuleMetaData().getRules(), props);
+ return deciderEngine.decide(logicSQL, database);
+ }
+
private JDBCExecutionUnit createTrafficExecutionUnit(final TrafficContext
trafficContext, final LogicSQL logicSQL) throws SQLException {
DriverExecutionPrepareEngine<JDBCExecutionUnit, Connection>
prepareEngine = createDriverExecutionPrepareEngine();
ExecutionUnit executionUnit = new
ExecutionUnit(trafficContext.getInstanceId(), new SQLUnit(logicSQL.getSql(),
logicSQL.getParameters()));
@@ -365,6 +376,11 @@ public final class ShardingSpherePreparedStatement extends
AbstractPreparedState
JDBCExecutionUnit executionUnit =
createTrafficExecutionUnit(trafficContext, logicSQL);
return executor.getTrafficExecutor().execute(executionUnit,
(statement, sql) -> ((PreparedStatement) statement).execute());
}
+ deciderContext = decide(logicSQL,
metaDataContexts.getMetaData().getProps(),
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()));
+ if (deciderContext.isUseSQLFederation()) {
+ ResultSet resultSet = executeFederationQuery(logicSQL);
+ return null != resultSet;
+ }
executionContext = createExecutionContext(logicSQL);
if (hasRawExecutionRule()) {
// TODO process getStatement
@@ -372,10 +388,6 @@ public final class ShardingSpherePreparedStatement extends
AbstractPreparedState
new RawSQLExecutorCallback(eventBusContext));
return executeResults.iterator().next() instanceof QueryResult;
}
- if (executionContext.getRouteContext().isFederated()) {
- ResultSet resultSet = executeFederationQuery(logicSQL);
- return null != resultSet;
- }
ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext =
createExecutionGroupContext();
cacheStatements(executionGroupContext.getInputGroups());
return executor.getRegularExecutor().execute(executionGroupContext,
@@ -433,7 +445,7 @@ public final class ShardingSpherePreparedStatement extends
AbstractPreparedState
if (trafficContext.isMatchTraffic()) {
return executor.getTrafficExecutor().getResultSet();
}
- if (executionContext.getRouteContext().isFederated()) {
+ if (null != deciderContext && deciderContext.isUseSQLFederation()) {
return executor.getFederationExecutor().getResultSet();
}
if (executionContext.getSqlStatementContext() instanceof
SelectStatementContext ||
executionContext.getSqlStatementContext().getSqlStatement() instanceof
DALStatement) {
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
index a0796cfe311..4d67fc92fa7 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/statement/ShardingSphereStatement.java
@@ -31,14 +31,16 @@ import
org.apache.shardingsphere.driver.jdbc.core.resultset.ShardingSphereResult
import org.apache.shardingsphere.driver.jdbc.exception.SQLExceptionErrorCode;
import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory;
+import
org.apache.shardingsphere.infra.binder.decider.context.SQLFederationDeciderContext;
+import
org.apache.shardingsphere.infra.binder.decider.engine.SQLFederationDeciderEngine;
import
org.apache.shardingsphere.infra.binder.segment.insert.keygen.GeneratedKeyContext;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import
org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.context.kernel.KernelProcessor;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
-import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.executor.check.SQLCheckEngine;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroup;
@@ -64,9 +66,11 @@ import
org.apache.shardingsphere.infra.federation.executor.FederationContext;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.merge.MergeEngine;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import
org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.RawExecutionRule;
+import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
@@ -122,6 +126,8 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
private TrafficContext trafficContext;
+ private SQLFederationDeciderContext deciderContext;
+
public ShardingSphereStatement(final ShardingSphereConnection connection) {
this(connection, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
}
@@ -156,11 +162,11 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
JDBCExecutionUnit executionUnit =
createTrafficExecutionUnit(trafficContext, logicSQL);
return executor.getTrafficExecutor().execute(executionUnit,
Statement::executeQuery);
}
- executionContext = createExecutionContext(logicSQL);
- // TODO move federation route logic to binder
- if (executionContext.getRouteContext().isFederated()) {
+ deciderContext = decide(logicSQL,
metaDataContexts.getMetaData().getProps(),
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()));
+ if (deciderContext.isUseSQLFederation()) {
return executeFederationQuery(logicSQL);
}
+ executionContext = createExecutionContext(logicSQL);
List<QueryResult> queryResults = executeQuery0();
MergedResult mergedResult = mergeQuery(queryResults);
result = new
ShardingSphereResultSet(getShardingSphereResultSets(), mergedResult, this,
executionContext);
@@ -174,6 +180,11 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
return result;
}
+ private static SQLFederationDeciderContext decide(final LogicSQL logicSQL,
final ConfigurationProperties props, final ShardingSphereDatabase database) {
+ SQLFederationDeciderEngine deciderEngine = new
SQLFederationDeciderEngine(database.getRuleMetaData().getRules(), props);
+ return deciderEngine.decide(logicSQL, database);
+ }
+
private TrafficContext getTrafficContext(final LogicSQL logicSQL) {
TrafficContext result = TrafficContextHolder.get().orElseGet(() ->
createTrafficContext(logicSQL));
if (connection.isHoldTransaction()) {
@@ -207,7 +218,7 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
private ResultSet executeFederationQuery(final LogicSQL logicSQL) throws
SQLException {
StatementExecuteQueryCallback callback = new
StatementExecuteQueryCallback(metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getResource().getDatabaseType(),
- executionContext.getSqlStatementContext().getSqlStatement(),
SQLExecutorExceptionHandler.isExceptionThrown(), eventBusContext);
+ logicSQL.getSqlStatementContext().getSqlStatement(),
SQLExecutorExceptionHandler.isExceptionThrown(), eventBusContext);
FederationContext context = new FederationContext(false, logicSQL,
metaDataContexts.getMetaData().getDatabases());
return
executor.getFederationExecutor().executeQuery(createDriverExecutionPrepareEngine(),
callback, context);
}
@@ -423,17 +434,17 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
JDBCExecutionUnit executionUnit =
createTrafficExecutionUnit(trafficContext, logicSQL);
return executor.getTrafficExecutor().execute(executionUnit,
(statement, actualSQL) -> callback.execute(actualSQL, statement));
}
+ deciderContext = decide(logicSQL,
metaDataContexts.getMetaData().getProps(),
metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()));
+ if (deciderContext.isUseSQLFederation()) {
+ ResultSet resultSet = executeFederationQuery(logicSQL);
+ return null != resultSet;
+ }
executionContext = createExecutionContext(logicSQL);
if
(metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName()).getRuleMetaData().getRules().stream().anyMatch(each
-> each instanceof RawExecutionRule)) {
// TODO process getStatement
Collection<ExecuteResult> results =
executor.getRawExecutor().execute(createRawExecutionContext(),
executionContext.getLogicSQL(), new RawSQLExecutorCallback(eventBusContext));
return results.iterator().next() instanceof QueryResult;
}
- // TODO move federation route logic to binder
- if (executionContext.getRouteContext().isFederated()) {
- ResultSet resultSet = executeFederationQuery(logicSQL);
- return null != resultSet;
- }
ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext =
createExecutionContext();
cacheStatements(executionGroupContext.getInputGroups());
return execute(executionGroupContext, callback,
executionContext.getSqlStatementContext().getSqlStatement(),
executionContext.getRouteContext().getRouteUnits());
@@ -505,7 +516,7 @@ public final class ShardingSphereStatement extends
AbstractStatementAdapter {
if (trafficContext.isMatchTraffic()) {
return executor.getTrafficExecutor().getResultSet();
}
- if (executionContext.getRouteContext().isFederated()) {
+ if (null != deciderContext && deciderContext.isUseSQLFederation()) {
return executor.getFederationExecutor().getResultSet();
}
if (executionContext.getSqlStatementContext() instanceof
SelectStatementContext ||
executionContext.getSqlStatementContext().getSqlStatement() instanceof
DALStatement) {
diff --git
a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/decider/SingleTableSQLFederationDecider.java
b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/decider/SingleTableSQLFederationDecider.java
index f32c42b98dd..c4413183228 100644
---
a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/decider/SingleTableSQLFederationDecider.java
+++
b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/decider/SingleTableSQLFederationDecider.java
@@ -48,6 +48,9 @@ public final class SingleTableSQLFederationDecider implements
SQLFederationDecid
final ShardingSphereDatabase database, final
SingleTableRule rule, final ConfigurationProperties props) {
SelectStatementContext select = (SelectStatementContext)
logicSQL.getSqlStatementContext();
Collection<QualifiedTable> singleTableNames =
getSingleTableNames(select, database, rule);
+ if (singleTableNames.isEmpty()) {
+ return;
+ }
addTableDataNodes(deciderContext, rule, singleTableNames);
deciderContext.setUseSQLFederation(!isAllTablesInSameDataSource(deciderContext,
rule, singleTableNames));
}
diff --git
a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/route/engine/SingleTableStandardRouteEngine.java
b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/route/engine/SingleTableStandardRouteEngine.java
index 02c0a143381..58cc22268d1 100644
---
a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/route/engine/SingleTableStandardRouteEngine.java
+++
b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/route/engine/SingleTableStandardRouteEngine.java
@@ -84,9 +84,6 @@ public final class SingleTableStandardRouteEngine implements
SingleTableRouteEng
routeContext.getRouteUnits().add(new RouteUnit(new
RouteMapper(dataSourceName, dataSourceName), Collections.singleton(new
RouteMapper(tableName, tableName))));
} else if (sqlStatement instanceof AlterTableStatement || sqlStatement
instanceof DropTableStatement || rule.isAllTablesInSameDataSource(routeContext,
singleTableNames)) {
fillRouteContext(rule, routeContext,
rule.getSingleTableNames(singleTableNames));
- } else {
- decorateFederationRouteContext(routeContext);
- fillRouteContext(rule, routeContext, singleTableNames);
}
}
@@ -101,16 +98,4 @@ public final class SingleTableStandardRouteEngine
implements SingleTableRouteEng
routeContext.putRouteUnit(new RouteMapper(dataSource, dataSource),
Collections.singletonList(new RouteMapper(tableName, tableName)));
}
}
-
- private void decorateFederationRouteContext(final RouteContext
routeContext) {
- RouteContext newRouteContext = new RouteContext();
- for (RouteUnit each : routeContext.getRouteUnits()) {
- newRouteContext.putRouteUnit(each.getDataSourceMapper(),
each.getTableMappers());
- }
- routeContext.setFederated(true);
- routeContext.getRouteUnits().clear();
- routeContext.getOriginalDataNodes().clear();
- routeContext.getRouteUnits().addAll(newRouteContext.getRouteUnits());
-
routeContext.getOriginalDataNodes().addAll(newRouteContext.getOriginalDataNodes());
- }
}
diff --git
a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java
b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java
index 4ae462564eb..061e5d2ff43 100644
---
a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java
+++
b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java
@@ -53,7 +53,6 @@ import java.util.Map;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
@@ -69,7 +68,6 @@ public final class SingleTableSQLRouterTest {
rule.getSingleTableDataNodes().put("t_order",
Collections.singletonList(createDataNode("foo_ds")));
ShardingSphereDatabase database = mockSingleDatabase();
RouteContext actual = new
SingleTableSQLRouter().createRouteContext(createLogicSQL(), database, rule, new
ConfigurationProperties(new Properties()));
- assertFalse(actual.isFederated());
assertThat(actual.getRouteUnits().size(), is(1));
RouteUnit routeUnit = actual.getRouteUnits().iterator().next();
assertThat(routeUnit.getDataSourceMapper().getLogicName(),
is("foo_ds"));
@@ -90,7 +88,6 @@ public final class SingleTableSQLRouterTest {
rule.getSingleTableDataNodes().put("t_order",
Collections.singletonList(createDataNode("write_ds")));
ShardingSphereDatabase database = mockReadwriteSplittingDatabase();
RouteContext actual = new
SingleTableSQLRouter().createRouteContext(createLogicSQL(), database, rule, new
ConfigurationProperties(new Properties()));
- assertFalse(actual.isFederated());
assertThat(actual.getRouteUnits().size(), is(1));
RouteUnit routeUnit = actual.getRouteUnits().iterator().next();
assertThat(routeUnit.getDataSourceMapper().getLogicName(),
is("readwrite_ds"));
@@ -118,7 +115,6 @@ public final class SingleTableSQLRouterTest {
RouteMapper tableMapper =
routeUnits.get(0).getTableMappers().iterator().next();
assertThat(tableMapper.getActualName(), is("t_order"));
assertThat(tableMapper.getLogicName(), is("t_order"));
- assertFalse(actual.isFederated());
}
private Map<String, DataSource> createMultiDataSourceMap() throws
SQLException {
diff --git
a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/engine/SingleTableStandardRouteEngineTest.java
b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/engine/SingleTableStandardRouteEngineTest.java
index 06c56970155..73e395c542c 100644
---
a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/engine/SingleTableStandardRouteEngineTest.java
+++
b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/engine/SingleTableStandardRouteEngineTest.java
@@ -42,9 +42,7 @@ import java.util.List;
import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -55,8 +53,8 @@ public final class SingleTableStandardRouteEngineTest {
public void assertRouteInSameDataSource() throws SQLException {
SingleTableStandardRouteEngine engine = new
SingleTableStandardRouteEngine(mockQualifiedTables(), null);
SingleTableRule singleTableRule = new SingleTableRule(new
SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME,
createDataSourceMap(), Collections.emptyList());
- singleTableRule.getSingleTableDataNodes().put("t_order",
Collections.singletonList(mockDataNode("ds_0", "t_order")));
- singleTableRule.getSingleTableDataNodes().put("t_order_item",
Collections.singletonList(mockDataNode("ds_0", "t_order_item")));
+ singleTableRule.getSingleTableDataNodes().put("t_order",
Collections.singletonList(mockDataNode("t_order")));
+ singleTableRule.getSingleTableDataNodes().put("t_order_item",
Collections.singletonList(mockDataNode("t_order_item")));
RouteContext routeContext = new RouteContext();
engine.route(routeContext, singleTableRule);
List<RouteUnit> routeUnits = new
ArrayList<>(routeContext.getRouteUnits());
@@ -70,11 +68,10 @@ public final class SingleTableStandardRouteEngineTest {
RouteMapper tableMapper1 = tableMappers.next();
assertThat(tableMapper1.getActualName(), is("t_order_item"));
assertThat(tableMapper1.getLogicName(), is("t_order_item"));
- assertFalse(routeContext.isFederated());
}
- private DataNode mockDataNode(final String dataSourceName, final String
tableName) {
- DataNode result = new DataNode(dataSourceName, tableName);
+ private DataNode mockDataNode(final String tableName) {
+ DataNode result = new DataNode("ds_0", tableName);
result.setSchemaName(DefaultDatabase.LOGIC_NAME);
return result;
}
@@ -83,29 +80,6 @@ public final class SingleTableStandardRouteEngineTest {
return Arrays.asList(new QualifiedTable(DefaultDatabase.LOGIC_NAME,
"t_order"), new QualifiedTable(DefaultDatabase.LOGIC_NAME, "t_order_item"));
}
- @Test
- public void assertRouteInDifferentDataSource() throws SQLException {
- SingleTableStandardRouteEngine engine = new
SingleTableStandardRouteEngine(mockQualifiedTables(), null);
- SingleTableRule singleTableRule = new SingleTableRule(new
SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME,
createDataSourceMap(), Collections.emptyList());
- singleTableRule.getSingleTableDataNodes().put("t_order",
Collections.singletonList(mockDataNode("ds_0", "t_order")));
- singleTableRule.getSingleTableDataNodes().put("t_order_item",
Collections.singletonList(mockDataNode("ds_1", "t_order_item")));
- RouteContext routeContext = new RouteContext();
- engine.route(routeContext, singleTableRule);
- List<RouteUnit> routeUnits = new
ArrayList<>(routeContext.getRouteUnits());
- assertThat(routeContext.getRouteUnits().size(), is(2));
- assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(),
is("ds_0"));
- assertThat(routeUnits.get(0).getTableMappers().size(), is(1));
- RouteMapper tableMapper0 =
routeUnits.get(0).getTableMappers().iterator().next();
- assertThat(tableMapper0.getActualName(), is("t_order"));
- assertThat(tableMapper0.getLogicName(), is("t_order"));
- assertThat(routeUnits.get(1).getDataSourceMapper().getActualName(),
is("ds_1"));
- assertThat(routeUnits.get(1).getTableMappers().size(), is(1));
- RouteMapper tableMapper1 =
routeUnits.get(1).getTableMappers().iterator().next();
- assertThat(tableMapper1.getActualName(), is("t_order_item"));
- assertThat(tableMapper1.getLogicName(), is("t_order_item"));
- assertTrue(routeContext.isFederated());
- }
-
@Test
public void assertRouteWithoutSingleTableRule() throws SQLException {
SingleTableStandardRouteEngine engine = new
SingleTableStandardRouteEngine(mockQualifiedTables(), new
MySQLCreateTableStatement(false));
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
index 8d9020f7710..16ec024d1c3 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
@@ -18,8 +18,9 @@
package org.apache.shardingsphere.proxy.backend.communication.jdbc;
import org.apache.shardingsphere.infra.binder.LogicSQL;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
-import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
+import
org.apache.shardingsphere.infra.binder.decider.context.SQLFederationDeciderContext;
+import
org.apache.shardingsphere.infra.binder.decider.engine.SQLFederationDeciderEngine;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
@@ -38,7 +39,6 @@ import
org.apache.shardingsphere.infra.federation.executor.FederationExecutorFac
import
org.apache.shardingsphere.infra.federation.optimizer.context.OptimizerContext;
import
org.apache.shardingsphere.infra.federation.optimizer.context.OptimizerContextFactory;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import
org.apache.shardingsphere.infra.metadata.database.schema.util.SystemSchemaUtil;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import
org.apache.shardingsphere.proxy.backend.communication.DatabaseCommunicationEngine;
import org.apache.shardingsphere.proxy.backend.communication.ProxySQLExecutor;
@@ -116,17 +116,15 @@ public final class JDBCDatabaseCommunicationEngine
extends DatabaseCommunication
public ResponseHeader execute() throws SQLException {
LogicSQL logicSQL = getLogicSQL();
MetaDataContexts metaDataContexts =
ProxyContext.getInstance().getContextManager().getMetaDataContexts();
- ExecutionContext executionContext =
getKernelProcessor().generateExecutionContext(
- logicSQL, getDatabase(),
metaDataContexts.getMetaData().getGlobalRuleMetaData(),
metaDataContexts.getMetaData().getProps());
- // TODO move federation route logic to binder
- SQLStatementContext<?> sqlStatementContext =
logicSQL.getSqlStatementContext();
ShardingSphereDatabase database =
metaDataContexts.getMetaData().getDatabase(backendConnection.getConnectionSession().getDatabaseName());
- if (executionContext.getRouteContext().isFederated() ||
(sqlStatementContext instanceof SelectStatementContext
- &&
SystemSchemaUtil.containsSystemSchema(sqlStatementContext.getDatabaseType(),
sqlStatementContext.getTablesContext().getSchemaNames(), database))) {
+ SQLFederationDeciderContext deciderContext = decide(logicSQL,
metaDataContexts.getMetaData().getProps(), database);
+ if (deciderContext.isUseSQLFederation()) {
prepareFederationExecutor();
ResultSet resultSet = doExecuteFederation(logicSQL,
metaDataContexts);
return processExecuteFederation(resultSet, metaDataContexts);
}
+ ExecutionContext executionContext =
getKernelProcessor().generateExecutionContext(
+ logicSQL, getDatabase(),
metaDataContexts.getMetaData().getGlobalRuleMetaData(),
metaDataContexts.getMetaData().getProps());
if (executionContext.getExecutionUnits().isEmpty()) {
return new
UpdateResponseHeader(executionContext.getSqlStatementContext().getSqlStatement());
}
@@ -140,6 +138,11 @@ public final class JDBCDatabaseCommunicationEngine extends
DatabaseCommunication
: processExecuteUpdate(executionContext, result);
}
+ private static SQLFederationDeciderContext decide(final LogicSQL logicSQL,
final ConfigurationProperties props, final ShardingSphereDatabase database) {
+ SQLFederationDeciderEngine deciderEngine = new
SQLFederationDeciderEngine(database.getRuleMetaData().getRules(), props);
+ return deciderEngine.decide(logicSQL, database);
+ }
+
private void prepareFederationExecutor() {
MetaDataContexts metaDataContexts =
ProxyContext.getInstance().getContextManager().getMetaDataContexts();
String databaseName =
backendConnection.getConnectionSession().getDatabaseName();
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxDatabaseCommunicationEngine.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxDatabaseCommunicationEngine.java
index 971da7427be..3d038457883 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxDatabaseCommunicationEngine.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/vertx/VertxDatabaseCommunicationEngine.java
@@ -19,6 +19,9 @@ package
org.apache.shardingsphere.proxy.backend.communication.vertx;
import io.vertx.core.Future;
import org.apache.shardingsphere.infra.binder.LogicSQL;
+import
org.apache.shardingsphere.infra.binder.decider.context.SQLFederationDeciderContext;
+import
org.apache.shardingsphere.infra.binder.decider.engine.SQLFederationDeciderEngine;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import
org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult;
import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
@@ -55,11 +58,12 @@ public final class VertxDatabaseCommunicationEngine extends
DatabaseCommunicatio
public Future<ResponseHeader> executeFuture() {
try {
ShardingSphereMetaData metaData =
ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData();
- ExecutionContext executionContext = getKernelProcessor()
- .generateExecutionContext(getLogicSQL(), getDatabase(),
metaData.getGlobalRuleMetaData(), metaData.getProps());
- if (executionContext.getRouteContext().isFederated()) {
+ SQLFederationDeciderContext deciderContext = decide(getLogicSQL(),
metaData.getProps(), getDatabase());
+ if (deciderContext.isUseSQLFederation()) {
return Future.failedFuture(new
UnsupportedOperationException("Executing federated query by Vert.x is not
supported yet."));
}
+ ExecutionContext executionContext = getKernelProcessor()
+ .generateExecutionContext(getLogicSQL(), getDatabase(),
metaData.getGlobalRuleMetaData(), metaData.getProps());
if (executionContext.getExecutionUnits().isEmpty()) {
return Future.succeededFuture(new
UpdateResponseHeader(executionContext.getSqlStatementContext().getSqlStatement()));
}
@@ -83,6 +87,11 @@ public final class VertxDatabaseCommunicationEngine extends
DatabaseCommunicatio
}
}
+ private static SQLFederationDeciderContext decide(final LogicSQL logicSQL,
final ConfigurationProperties props, final ShardingSphereDatabase database) {
+ SQLFederationDeciderEngine deciderEngine = new
SQLFederationDeciderEngine(database.getRuleMetaData().getRules(), props);
+ return deciderEngine.decide(logicSQL, database);
+ }
+
@Override
public ResponseHeader execute() throws SQLException {
throw new UnsupportedOperationException();
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/PreviewHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/PreviewHandler.java
index 8ab2f54d496..194f17d1aa6 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/PreviewHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rul/sql/PreviewHandler.java
@@ -23,16 +23,18 @@ import
org.apache.shardingsphere.distsql.parser.statement.rul.sql.PreviewStateme
import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory;
import org.apache.shardingsphere.infra.binder.aware.CursorDefinitionAware;
+import
org.apache.shardingsphere.infra.binder.decider.context.SQLFederationDeciderContext;
+import
org.apache.shardingsphere.infra.binder.decider.engine.SQLFederationDeciderEngine;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import
org.apache.shardingsphere.infra.binder.statement.ddl.CursorStatementContext;
import org.apache.shardingsphere.infra.binder.type.CursorAvailable;
import org.apache.shardingsphere.infra.binder.type.TableAvailable;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.context.kernel.KernelProcessor;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
import org.apache.shardingsphere.infra.exception.DatabaseNotExistedException;
-import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;
import
org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;
import
org.apache.shardingsphere.infra.executor.sql.execute.engine.SQLExecutorExceptionHandler;
@@ -51,6 +53,7 @@ import
org.apache.shardingsphere.infra.federation.optimizer.context.OptimizerCon
import
org.apache.shardingsphere.infra.federation.optimizer.context.OptimizerContextFactory;
import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
@@ -96,7 +99,8 @@ public final class PreviewHandler extends
SQLRULBackendHandler<PreviewStatement>
MetaDataContexts metaDataContexts =
ProxyContext.getInstance().getContextManager().getMetaDataContexts();
String databaseName = getDatabaseName();
String databaseType =
DatabaseTypeEngine.getTrunkDatabaseTypeName(metaDataContexts.getMetaData().getDatabase(databaseName).getProtocolType());
- SQLParserRule sqlParserRule =
metaDataContexts.getMetaData().getGlobalRuleMetaData().getSingleRule(SQLParserRule.class);
+ ShardingSphereRuleMetaData globalRuleMetaData =
metaDataContexts.getMetaData().getGlobalRuleMetaData();
+ SQLParserRule sqlParserRule =
globalRuleMetaData.getSingleRule(SQLParserRule.class);
SQLStatement previewedStatement =
sqlParserRule.getSQLParserEngine(databaseType).parse(getSqlStatement().getSql(),
false);
SQLStatementContext<?> sqlStatementContext =
SQLStatementContextFactory.newInstance(metaDataContexts.getMetaData().getDatabases(),
previewedStatement, databaseName);
// TODO optimize SQLStatementDatabaseHolder
@@ -111,14 +115,18 @@ public final class PreviewHandler extends
SQLRULBackendHandler<PreviewStatement>
throw new RuleNotExistedException();
}
LogicSQL logicSQL = new LogicSQL(sqlStatementContext,
getSqlStatement().getSql(), Collections.emptyList());
- ExecutionContext executionContext =
kernelProcessor.generateExecutionContext(
- logicSQL, database,
metaDataContexts.getMetaData().getGlobalRuleMetaData(),
metaDataContexts.getMetaData().getProps());
- Collection<ExecutionUnit> executionUnits =
executionContext.getRouteContext().isFederated()
- ? getFederationExecutionUnits(logicSQL, databaseName,
metaDataContexts)
- : executionContext.getExecutionUnits();
+ ConfigurationProperties props =
metaDataContexts.getMetaData().getProps();
+ SQLFederationDeciderContext deciderContext = decide(logicSQL, props,
metaDataContexts.getMetaData().getDatabase(getConnectionSession().getDatabaseName()));
+ Collection<ExecutionUnit> executionUnits =
deciderContext.isUseSQLFederation() ? getFederationExecutionUnits(logicSQL,
databaseName, metaDataContexts)
+ : kernelProcessor.generateExecutionContext(logicSQL, database,
globalRuleMetaData, props).getExecutionUnits();
return
executionUnits.stream().map(this::buildRow).collect(Collectors.toList());
}
+ private static SQLFederationDeciderContext decide(final LogicSQL logicSQL,
final ConfigurationProperties props, final ShardingSphereDatabase database) {
+ SQLFederationDeciderEngine deciderEngine = new
SQLFederationDeciderEngine(database.getRuleMetaData().getRules(), props);
+ return deciderEngine.decide(logicSQL, database);
+ }
+
private void setUpCursorDefinition(final SQLStatementContext<?>
sqlStatementContext) {
if (!((CursorAvailable)
sqlStatementContext).getCursorName().isPresent()) {
return;
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngineTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngineTest.java
index 30b5739cccd..6796ccee610 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngineTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngineTest.java
@@ -20,12 +20,13 @@ package
org.apache.shardingsphere.proxy.backend.communication.jdbc;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.context.kernel.KernelProcessor;
import org.apache.shardingsphere.infra.database.DefaultDatabase;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.database.type.dialect.H2DatabaseType;
import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
-import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import
org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
@@ -43,6 +44,7 @@ import
org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.
import
org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereIndex;
import
org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereSchema;
import
org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereTable;
+import
org.apache.shardingsphere.infra.metadata.database.schema.util.SystemSchemaUtil;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
@@ -133,7 +135,8 @@ public final class JDBCDatabaseCommunicationEngineTest
extends ProxyContextResto
@Test
public void assertExecuteFederationAndClose() throws SQLException,
NoSuchFieldException, IllegalAccessException {
- SQLStatementContext<?> sqlStatementContext =
mock(SQLStatementContext.class, RETURNS_DEEP_STUBS);
+ SQLStatementContext<?> sqlStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
+ when(sqlStatementContext.getDatabaseType()).thenReturn(new
MySQLDatabaseType());
JDBCDatabaseCommunicationEngine engine =
DatabaseCommunicationEngineFactory.getInstance().newDatabaseCommunicationEngine(new
LogicSQL(sqlStatementContext, "schemaName", Collections.emptyList()),
backendConnection, true);
Field kernelProcessorField =
DatabaseCommunicationEngine.class.getDeclaredField("kernelProcessor");
@@ -141,20 +144,19 @@ public final class JDBCDatabaseCommunicationEngineTest
extends ProxyContextResto
KernelProcessor kernelProcessor = mock(KernelProcessor.class);
kernelProcessorField.set(engine, kernelProcessor);
RouteContext routeContext = new RouteContext();
- routeContext.setFederated(true);
- ExecutionContext executionContext = new
ExecutionContext(mock(LogicSQL.class), Collections.emptyList(), routeContext);
- when(kernelProcessor.generateExecutionContext(any(LogicSQL.class),
any(ShardingSphereDatabase.class), any(ShardingSphereRuleMetaData.class),
any(ConfigurationProperties.class)))
- .thenReturn(executionContext);
when(backendConnection.getConnectionSession().getStatementManager()).thenReturn(new
JDBCBackendStatement());
FederationExecutor federationExecutor = mock(FederationExecutor.class);
- try (MockedStatic<FederationExecutorFactory> mockedStatic =
mockStatic(FederationExecutorFactory.class)) {
+ try (
+ MockedStatic<FederationExecutorFactory>
federationExecutorFactory = mockStatic(FederationExecutorFactory.class);
+ MockedStatic<SystemSchemaUtil> systemSchemaUtil =
mockStatic(SystemSchemaUtil.class)) {
when(federationExecutor.executeQuery(any(DriverExecutionPrepareEngine.class),
any(ProxyJDBCExecutorCallback.class),
any(FederationContext.class))).thenReturn(resultSet);
when(resultSet.getMetaData().getColumnCount()).thenReturn(1);
when(resultSet.getMetaData().getColumnType(1)).thenReturn(Types.INTEGER);
when(resultSet.next()).thenReturn(true, false);
when(resultSet.getObject(1)).thenReturn(Integer.MAX_VALUE);
- mockedStatic.when(() ->
FederationExecutorFactory.newInstance(anyString(), nullable(String.class),
any(OptimizerContext.class), any(ShardingSphereRuleMetaData.class),
+ federationExecutorFactory.when(() ->
FederationExecutorFactory.newInstance(anyString(), nullable(String.class),
any(OptimizerContext.class), any(ShardingSphereRuleMetaData.class),
any(ConfigurationProperties.class),
any(JDBCExecutor.class),
any(EventBusContext.class))).thenReturn(federationExecutor);
+ systemSchemaUtil.when(() ->
SystemSchemaUtil.containsSystemSchema(any(DatabaseType.class), any(),
any(ShardingSphereDatabase.class))).thenReturn(true);
engine.execute();
}
assertTrue(engine.next());
diff --git
a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/dml/select.xml
b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/dml/select.xml
index 22af4a5479c..68f29005a22 100644
---
a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/dml/select.xml
+++
b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/dml/select.xml
@@ -47,16 +47,6 @@
<output sql="SELECT * FROM t_account_0 WHERE 100 = account_id" />
</rewrite-assertion>
- <rewrite-assertion id="select_with_not_exists" db-types="MySQL">
- <input sql="SELECT * FROM t_account a WHERE not exists (select * from
t_account_detail where a.account_id=account_id and account_id=1000) and
account_id = 100" />
- <output sql="SELECT * FROM t_account a WHERE not exists (select * from
t_account_detail where a.account_id=account_id and account_id=1000) and
account_id = 100" />
- </rewrite-assertion>
-
- <rewrite-assertion id="select_with_sum_fun">
- <input sql="SELECT SUM(DISTINCT account_id), SUM(account_id) FROM
t_account WHERE account_id = 100" />
- <output sql="SELECT SUM(DISTINCT account_id), SUM(account_id) FROM
t_account WHERE account_id = 100" />
- </rewrite-assertion>
-
<rewrite-assertion id="select_with_avg_fun">
<input sql="SELECT AVG(DISTINCT t.account_id), account_id FROM
t_account t WHERE account_id = 100" />
<output sql="SELECT AVG(DISTINCT t.account_id), account_id FROM
t_account_0 t WHERE account_id = 100" />
@@ -68,16 +58,6 @@
<output sql="SELECT DISTINCT amount AS AGGREGATION_DISTINCT_DERIVED_0,
account_id FROM t_account_1 ORDER BY account_id ASC " />
</rewrite-assertion>
- <rewrite-assertion id="select_with_having"
db-types="MySQL,PostgreSQL,openGauss,SQLServer,SQL92">
- <input sql="SELECT COUNT(account_id) as count_alias FROM t_account
GROUP BY amount HAVING count_alias > ?" parameters="1"/>
- <output sql="SELECT COUNT(account_id) as count_alias FROM t_account
GROUP BY amount HAVING count_alias > ?" parameters="1"/>
- </rewrite-assertion>
-
- <rewrite-assertion id="select_with_having_and_window"
db-types="MySQL,PostgreSQL,openGauss">
- <input sql="SELECT COUNT(account_id) as count_alias FROM t_account
GROUP BY amount HAVING count_alias > ? WINDOW w AS (PARTITION BY account_id)"
parameters="1"/>
- <output sql="SELECT COUNT(account_id) as count_alias FROM t_account
GROUP BY amount HAVING count_alias > ? WINDOW w AS (PARTITION BY account_id)"
parameters="1"/>
- </rewrite-assertion>
-
<rewrite-assertion id="select_with_schema"
db-types="MySQL,Oracle,SQLServer,SQL92">
<input sql="SELECT * FROM logic_db.t_account" />
<output sql="SELECT * FROM t_account_0 UNION ALL SELECT * FROM
t_account_1"/>
@@ -88,26 +68,6 @@
<output sql="SELECT * FROM (select t_account_0.account_id from
t_account_0 where t_account_0.account_id=?) a WHERE account_id = 100"
parameters="100" />
</rewrite-assertion>
- <rewrite-assertion
id="select_with_subquery_and_different_sharding_conditions" db-types="MySQL">
- <input sql="SELECT * FROM (select t_account.account_id from t_account
where t_account.account_id=?) a WHERE account_id = 101" parameters="100" />
- <output sql="SELECT * FROM (select t_account.account_id from t_account
where t_account.account_id=?) a WHERE account_id = 101" parameters="100" />
- </rewrite-assertion>
-
- <rewrite-assertion id="select_with_subquery_with_subquery"
db-types="MySQL">
- <input sql="SELECT * FROM (select b.account_id from (select
t_account.account_id from t_account) b where b.account_id=?) a WHERE account_id
= 100" parameters="100" />
- <output sql="SELECT * FROM (select b.account_id from (select
t_account.account_id from t_account) b where b.account_id=?) a WHERE account_id
= 100" parameters="100" />
- </rewrite-assertion>
-
- <rewrite-assertion id="select_with_subquery_in_projection_and_where"
db-types="MySQL">
- <input sql="SELECT (select id from t_account limit 1) as myid FROM
(select b.account_id from (select t_account.account_id from t_account) b where
b.account_id=?) a WHERE account_id >= (select account_id from t_account limit
1)" parameters="100"/>
- <output sql="SELECT (select id from t_account limit 1) as myid FROM
(select b.account_id from (select t_account.account_id from t_account) b where
b.account_id=?) a WHERE account_id >= (select account_id from t_account limit
1)" parameters="100"/>
- </rewrite-assertion>
-
- <rewrite-assertion id="select_with_subquery_only_in_projection"
db-types="MySQL">
- <input sql="SELECT (select id from t_account)"/>
- <output sql="SELECT (select id from t_account)"/>
- </rewrite-assertion>
-
<rewrite-assertion id="select_with_subquery_for_where_in_predicate"
db-types="MySQL">
<input sql="SELECT * FROM t_account WHERE account_id = ? AND amount IN
(SELECT amount FROM t_account WHERE account_id = ?)" parameters="100, 100"/>
<output sql="SELECT * FROM t_account_0 WHERE account_id = ? AND amount
IN (SELECT amount FROM t_account_0 WHERE account_id = ?)" parameters="100,
100"/>