You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/08/12 11:27:22 UTC
(shardingsphere) branch master updated: Fix order-dependent cartesian route intersection (#39407)
This is an automated email from the ASF dual-hosted git repository.
terrymanu 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 3b2f569f4d4 Fix order-dependent cartesian route intersection (#39407)
3b2f569f4d4 is described below
commit 3b2f569f4d487a61206077cbcd552769168416dc
Author: Gimin Kim <13...@users.noreply.github.com>
AuthorDate: Wed Aug 12 20:27:15 2026 +0900
Fix order-dependent cartesian route intersection (#39407)
Co-authored-by: Gimin Kim <Gi...@users.noreply.github.com>
---
.../type/complex/ShardingCartesianRouteEngine.java | 7 +-
.../complex/ShardingCartesianRouteEngineTest.java | 81 ++++++++++++++++++++++
2 files changed, 86 insertions(+), 2 deletions(-)
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingCartesianRouteEngine.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingCartesianRouteEngine.java
index f36f70d3fe7..802f5a2dd4a 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingCartesianRouteEngine.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingCartesianRouteEngine.java
@@ -74,12 +74,15 @@ public final class ShardingCartesianRouteEngine implements ShardingRouteEngine {
private Collection<String> getIntersectionDataSources() {
Collection<String> result = new HashSet<>();
+ boolean initialized = false;
for (RouteContext each : routeContexts) {
Collection<String> actualDataSourceNames = each.getActualDataSourceNames();
- if (result.isEmpty()) {
+ if (!initialized) {
result.addAll(actualDataSourceNames);
+ initialized = true;
+ } else {
+ result.retainAll(actualDataSourceNames);
}
- result.retainAll(actualDataSourceNames);
}
return result;
}
diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingCartesianRouteEngineTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingCartesianRouteEngineTest.java
new file mode 100644
index 00000000000..f7d4fcfbe2c
--- /dev/null
+++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/complex/ShardingCartesianRouteEngineTest.java
@@ -0,0 +1,81 @@
+/*
+ * 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.complex;
+
+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.exception.algorithm.NoShardingTableRouteFactorException;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Optional;
+import java.util.stream.Stream;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.anyCollection;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class ShardingCartesianRouteEngineTest {
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("routeArguments")
+ void assertRoute(final String name, final Collection<RouteContext> routeContexts, final Collection<RouteUnit> expectedRouteUnits) {
+ ShardingCartesianRouteEngine routeEngine = new ShardingCartesianRouteEngine(routeContexts);
+ RouteContext actual = routeEngine.route(mock(ShardingRule.class));
+ assertThat(actual.getRouteUnits(), is(expectedRouteUnits));
+ }
+
+ private static Stream<Arguments> routeArguments() {
+ return Stream.of(
+ Arguments.of("Common data source", Arrays.asList(
+ createRouteContext("foo_ds", "foo_order", "foo_order_0"), createRouteContext("foo_ds", "foo_order_item", "foo_order_item_0")),
+ Collections.singleton(new RouteUnit(new RouteMapper("foo_ds", "foo_ds"),
+ Arrays.asList(new RouteMapper("foo_order", "foo_order_0"), new RouteMapper("foo_order_item", "foo_order_item_0"))))),
+ Arguments.of("Disjoint data sources", Arrays.asList(
+ createRouteContext("foo_ds_0", "foo_order", "foo_order_0"), createRouteContext("foo_ds_1", "foo_order_item", "foo_order_item_0"),
+ createRouteContext("foo_ds_0", "foo_user", "foo_user_0")), Collections.emptySet()),
+ Arguments.of("Empty route contexts", Collections.emptyList(), Collections.emptySet()));
+ }
+
+ @Test
+ void assertRouteWithMissingTableMapper() {
+ RouteContext routeContext = mock(RouteContext.class);
+ when(routeContext.getActualDataSourceNames()).thenReturn(Collections.singleton("foo_ds"));
+ when(routeContext.getDataSourceLogicTablesMap(anyCollection())).thenReturn(Collections.singletonMap("foo_ds", Collections.singleton("foo_table")));
+ when(routeContext.getActualTableNameGroups("foo_ds", Collections.singleton("foo_table"))).thenReturn(Collections.singletonList(Collections.singleton("foo_table_0")));
+ when(routeContext.findTableMapper("foo_ds", "foo_table_0")).thenReturn(Optional.empty());
+ ShardingCartesianRouteEngine routeEngine = new ShardingCartesianRouteEngine(Collections.singleton(routeContext));
+ assertThrows(NoShardingTableRouteFactorException.class, () -> routeEngine.route(mock(ShardingRule.class)));
+ }
+
+ private static RouteContext createRouteContext(final String dataSourceName, final String logicTableName, final String actualTableName) {
+ RouteContext result = new RouteContext();
+ result.getRouteUnits().add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), Collections.singletonList(new RouteMapper(logicTableName, actualTableName))));
+ return result;
+ }
+}