You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by hu...@apache.org on 2026/03/16 04:47:19 UTC
(shardingsphere) branch master updated: Fix single table router with single logical data source (#38450)
This is an automated email from the ASF dual-hosted git repository.
hucong 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 7446ac7fffd Fix single table router with single logical data source (#38450)
7446ac7fffd is described below
commit 7446ac7fffde5f49f82482b7b074be45e3375023
Author: ZhangCheng <ch...@apache.org>
AuthorDate: Mon Mar 16 12:47:11 2026 +0800
Fix single table router with single logical data source (#38450)
* Fix single table router with single logical data source
* Fix single table router with single logical data source
* Fix single table router with single logical data source
---
.../single/route/SingleSQLRouter.java | 21 ++++++++++++-
.../single/route/SingleSQLRouterTest.java | 36 ++++++++++++++++++++++
.../shardingsphere/single/rule/SingleRuleTest.java | 25 +++++++++++++++
3 files changed, 81 insertions(+), 1 deletion(-)
diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java
index 85adff41bc0..2dd8c0ed078 100644
--- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java
+++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/route/SingleSQLRouter.java
@@ -29,11 +29,13 @@ import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.infra.route.lifecycle.DecorateSQLRouter;
import org.apache.shardingsphere.infra.route.lifecycle.EntranceSQLRouter;
+import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute;
import org.apache.shardingsphere.infra.rule.attribute.table.TableMapperRuleAttribute;
import org.apache.shardingsphere.infra.session.query.QueryContext;
import org.apache.shardingsphere.single.constant.SingleOrder;
import org.apache.shardingsphere.single.route.engine.SingleRouteEngine;
import org.apache.shardingsphere.single.rule.SingleRule;
+import org.apache.shardingsphere.single.rule.attribute.SingleDataNodeRuleAttribute;
import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.table.CreateTableStatement;
import java.util.Collection;
@@ -55,11 +57,20 @@ public final class SingleSQLRouter implements EntranceSQLRouter<SingleRule>, Dec
RouteContext routeContext = new RouteContext();
Collection<QualifiedTable> singleTables = getSingleTables(database, rule, sqlStatementContext);
if (singleTables.isEmpty()) {
- return routeContext;
+ return isAllowedToRouteWithSingleLogicalDataSource(database, rule) ? createSingleLogicalDataSourceRouteContext(rule, queryContext) : routeContext;
}
return new SingleRouteEngine(singleTables, sqlStatementContext.getSqlStatement(), queryContext.getHintValueContext()).route(routeContext, rule);
}
+ private boolean isAllowedToRouteWithSingleLogicalDataSource(final ShardingSphereDatabase database, final SingleRule rule) {
+ return 1 == rule.getDataSourceNames().size() && isOnlySingleDataNodeRuleAttribute(database);
+ }
+
+ private boolean isOnlySingleDataNodeRuleAttribute(final ShardingSphereDatabase database) {
+ Collection<DataNodeRuleAttribute> dataNodeRuleAttributes = database.getRuleMetaData().getAttributes(DataNodeRuleAttribute.class);
+ return 1 == dataNodeRuleAttributes.size() && dataNodeRuleAttributes.iterator().next() instanceof SingleDataNodeRuleAttribute;
+ }
+
@Override
public void decorateRouteContext(final RouteContext routeContext, final QueryContext queryContext, final ShardingSphereDatabase database,
final SingleRule rule, final Collection<String> tableNames, final ConfigurationProperties props) {
@@ -80,6 +91,14 @@ public final class SingleSQLRouter implements EntranceSQLRouter<SingleRule>, Dec
return result;
}
+ private RouteContext createSingleLogicalDataSourceRouteContext(final SingleRule rule, final QueryContext queryContext) {
+ String logicDataSource = rule.getDataSourceNames().iterator().next();
+ RouteContext result = new RouteContext();
+ Collection<String> tableNames = queryContext.getSqlStatementContext().getTablesContext().getTableNames();
+ result.getRouteUnits().add(new RouteUnit(new RouteMapper(logicDataSource, logicDataSource), createTableMappers(tableNames)));
+ return result;
+ }
+
private Collection<RouteMapper> createTableMappers(final Collection<String> tableNames) {
Collection<RouteMapper> result = new LinkedList<>();
for (String each : tableNames) {
diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java
index 3b76bcb3211..29bc76fdb1d 100644
--- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java
+++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/route/SingleSQLRouterTest.java
@@ -47,6 +47,7 @@ import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.single.config.SingleRuleConfiguration;
import org.apache.shardingsphere.single.rule.SingleRule;
+import org.apache.shardingsphere.single.rule.attribute.SingleDataNodeRuleAttribute;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.TableSegmentBoundInfo;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
@@ -63,6 +64,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
@@ -71,6 +73,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -302,4 +305,37 @@ class SingleSQLRouterTest {
return new QueryContext(new CommonSQLStatementContext(CreateTableStatement.builder().databaseType(databaseType).table(new SimpleTableSegment(tableNameSegment)).build()),
"CREATE TABLE", Collections.emptyList(), new HintValueContext(), connectionContext, metaData);
}
+
+ @Test
+ void assertCreateRouteContextWhenNoSingleTablesButSingleLogicalDataSource() {
+ SingleRule rule = mock(SingleRule.class);
+ when(rule.getQualifiedTables(any(), any())).thenReturn(Collections.emptyList());
+ when(rule.getSingleTables(anyCollection())).thenReturn(Collections.emptyList());
+ when(rule.getDataSourceNames()).thenReturn(Collections.singleton("logical_ds"));
+ ShardingSphereDatabase database = mockDatabaseWithMultipleResources();
+ when(database.getRuleMetaData().getAttributes(TableMapperRuleAttribute.class)).thenReturn(Collections.emptyList());
+ when(database.getRuleMetaData().getAttributes(DataNodeRuleAttribute.class))
+ .thenReturn(Collections.singletonList(new SingleDataNodeRuleAttribute(new LinkedHashMap<>())));
+ RouteContext actual = new SingleSQLRouter().createRouteContext(
+ createQueryContext(), mock(RuleMetaData.class), database, rule, Collections.singletonList("t_order"), new ConfigurationProperties(new Properties()));
+ assertThat(actual.getRouteUnits().size(), is(1));
+ RouteUnit routeUnit = actual.getRouteUnits().iterator().next();
+ assertThat(routeUnit.getDataSourceMapper().getLogicName(), is("logical_ds"));
+ assertThat(routeUnit.getDataSourceMapper().getActualName(), is("logical_ds"));
+ }
+
+ @Test
+ void assertCreateRouteContextWhenNoSingleTablesAndMultipleDataNodeParticipants() {
+ SingleRule rule = mock(SingleRule.class);
+ when(rule.getQualifiedTables(any(), any())).thenReturn(Collections.emptyList());
+ when(rule.getSingleTables(anyCollection())).thenReturn(Collections.emptyList());
+ when(rule.getDataSourceNames()).thenReturn(Collections.singleton("logical_ds"));
+ ShardingSphereDatabase database = mockDatabaseWithMultipleResources();
+ when(database.getRuleMetaData().getAttributes(TableMapperRuleAttribute.class)).thenReturn(Collections.emptyList());
+ when(database.getRuleMetaData().getAttributes(DataNodeRuleAttribute.class))
+ .thenReturn(Arrays.asList(new SingleDataNodeRuleAttribute(new LinkedHashMap<>()), mock(DataNodeRuleAttribute.class)));
+ RouteContext actual = new SingleSQLRouter().createRouteContext(
+ createQueryContext(), mock(RuleMetaData.class), database, rule, Collections.singletonList("t_order"), new ConfigurationProperties(new Properties()));
+ assertTrue(actual.getRouteUnits().isEmpty());
+ }
}
diff --git a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleRuleTest.java b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleRuleTest.java
index 564a1906827..ee404ee57a9 100644
--- a/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleRuleTest.java
+++ b/kernel/single/core/src/test/java/org/apache/shardingsphere/single/rule/SingleRuleTest.java
@@ -29,6 +29,7 @@ import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute;
import org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute;
+import org.apache.shardingsphere.infra.rule.attribute.datasource.DataSourceMapperRuleAttribute;
import org.apache.shardingsphere.infra.rule.attribute.table.TableMapperRuleAttribute;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.single.config.SingleRuleConfiguration;
@@ -175,6 +176,30 @@ class SingleRuleTest {
}
}
+ @Test
+ void assertFindSingleLogicalDataSource() {
+ ShardingSphereRule builtRule = mock(ShardingSphereRule.class);
+ DataSourceMapperRuleAttribute mapperRuleAttribute = mock(DataSourceMapperRuleAttribute.class);
+ when(mapperRuleAttribute.getDataSourceMapper()).thenReturn(Collections.singletonMap("logical_ds", Arrays.asList("foo_ds", "bar_ds")));
+ when(builtRule.getAttributes()).thenReturn(new RuleAttributes(mapperRuleAttribute));
+ SingleRule singleRule = new SingleRule(ruleConfig, "foo_db", databaseType, dataSourceMap, Collections.singleton(builtRule));
+ assertThat(singleRule.getDataSourceNames().size(), is(1));
+ assertThat(singleRule.getDataSourceNames().iterator().next(), is("logical_ds"));
+ }
+
+ @Test
+ void assertFindSingleLogicalDataSourceWhenMultipleLogicalDataSources() {
+ DataSourceMapperRuleAttribute mapperRuleAttribute = mock(DataSourceMapperRuleAttribute.class);
+ Map<String, Collection<String>> dataSourceMapper = new LinkedHashMap<>();
+ dataSourceMapper.put("logic_ds_0", Collections.singleton("foo_ds"));
+ dataSourceMapper.put("logic_ds_1", Collections.singleton("bar_ds"));
+ when(mapperRuleAttribute.getDataSourceMapper()).thenReturn(dataSourceMapper);
+ ShardingSphereRule builtRule = mock(ShardingSphereRule.class);
+ when(builtRule.getAttributes()).thenReturn(new RuleAttributes(mapperRuleAttribute));
+ SingleRule singleRule = new SingleRule(ruleConfig, "foo_db", databaseType, dataSourceMap, Collections.singleton(builtRule));
+ assertThat(singleRule.getDataSourceNames().size(), is(2));
+ }
+
@Test
void assertPut() {
ShardingSphereRule builtRule = mock(ShardingSphereRule.class, RETURNS_DEEP_STUBS);