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/09/30 05:41:28 UTC
(shardingsphere) branch master updated: Remove SQL Federation-specific provider validation (#40018)
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 e7ad668bd88 Remove SQL Federation-specific provider validation (#40018)
e7ad668bd88 is described below
commit e7ad668bd88680791b57870f1ea914c8bcece1d3
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Sep 30 13:41:20 2026 +0800
Remove SQL Federation-specific provider validation (#40018)
Resolve providers directly through TypedSPILoader in the rule and
ALTER handler. Remove the redundant factory, duplicate-provider
exception, and obsolete tests.
---
.../SQLFederationProviderDuplicatedException.java | 32 -------
.../rule/SQLFederationProviderFactory.java | 51 -----------
.../sqlfederation/rule/SQLFederationRule.java | 3 +-
.../rule/SQLFederationProviderFactoryTest.java | 98 ----------------------
.../sqlfederation/rule/SQLFederationRuleTest.java | 9 +-
.../update/AlterSQLFederationRuleExecutor.java | 5 +-
.../update/AlterSQLFederationRuleExecutorTest.java | 12 ---
7 files changed, 10 insertions(+), 200 deletions(-)
diff --git a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/exception/SQLFederationProviderDuplicatedException.java b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/exception/SQLFederationProviderDuplicatedException.java
deleted file mode 100644
index b723783f9d8..00000000000
--- a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/exception/SQLFederationProviderDuplicatedException.java
+++ /dev/null
@@ -1,32 +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.sqlfederation.exception;
-
-import org.apache.shardingsphere.infra.exception.external.server.ShardingSphereServerException;
-
-/**
- * SQL federation provider type duplicated exception.
- */
-public final class SQLFederationProviderDuplicatedException extends ShardingSphereServerException {
-
- private static final long serialVersionUID = 3499814950380744743L;
-
- public SQLFederationProviderDuplicatedException(final String providerType) {
- super("SQL_FEDERATION", 2, String.format("Multiple SQL Federation providers have type '%s'.", providerType));
- }
-}
diff --git a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationProviderFactory.java b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationProviderFactory.java
deleted file mode 100644
index 4080d3bba24..00000000000
--- a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationProviderFactory.java
+++ /dev/null
@@ -1,51 +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.sqlfederation.rule;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
-import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.sqlfederation.exception.SQLFederationProviderDuplicatedException;
-import org.apache.shardingsphere.sqlfederation.spi.SQLFederationProvider;
-
-import java.util.HashSet;
-import java.util.Locale;
-import java.util.Set;
-
-/**
- * SQL federation provider factory.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class SQLFederationProviderFactory {
-
- /**
- * Get SQL federation provider without calling SQLFederationProvider.initialize.
- *
- * @param providerType provider type, or null to select the default provider
- * @return selected provider
- */
- public static SQLFederationProvider getProvider(final String providerType) {
- Set<String> types = new HashSet<>();
- for (SQLFederationProvider each : ShardingSphereServiceLoader.getServiceInstances(SQLFederationProvider.class)) {
- ShardingSpherePreconditions.checkState(types.add(each.getType().toUpperCase(Locale.ROOT)), () -> new SQLFederationProviderDuplicatedException(each.getType()));
- }
- return TypedSPILoader.getService(SQLFederationProvider.class, providerType);
- }
-}
diff --git a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRule.java b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRule.java
index acbd229a808..dcc46360f93 100644
--- a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRule.java
+++ b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRule.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.sqlfederation.rule;
import lombok.Getter;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
import org.apache.shardingsphere.sqlfederation.constant.SQLFederationOrder;
import org.apache.shardingsphere.sqlfederation.spi.SQLFederationProvider;
@@ -42,7 +43,7 @@ public final class SQLFederationRule implements GlobalRule {
}
private SQLFederationProvider createProvider(final Collection<ShardingSphereDatabase> databases) {
- SQLFederationProvider result = SQLFederationProviderFactory.getProvider(configuration.getProviderType());
+ SQLFederationProvider result = TypedSPILoader.getService(SQLFederationProvider.class, configuration.getProviderType());
result.initialize(configuration, databases);
return result;
}
diff --git a/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationProviderFactoryTest.java b/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationProviderFactoryTest.java
deleted file mode 100644
index af9991673df..00000000000
--- a/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationProviderFactoryTest.java
+++ /dev/null
@@ -1,98 +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.sqlfederation.rule;
-
-import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException;
-import org.apache.shardingsphere.sqlfederation.exception.SQLFederationProviderDuplicatedException;
-import org.apache.shardingsphere.sqlfederation.spi.SQLFederationProvider;
-import org.junit.jupiter.api.Test;
-import org.mockito.MockedStatic;
-
-import java.util.Arrays;
-import java.util.Collections;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.sameInstance;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.mockStatic;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-class SQLFederationProviderFactoryTest {
-
- @Test
- void assertMissingProviderFallsBackToDefault() {
- SQLFederationProvider expected = mock(SQLFederationProvider.class);
- when(expected.getType()).thenReturn("CALCITE");
- when(expected.isDefault()).thenReturn(true);
- try (MockedStatic<ShardingSphereServiceLoader> serviceLoader = mockStatic(ShardingSphereServiceLoader.class)) {
- serviceLoader.when(() -> ShardingSphereServiceLoader.getServiceInstances(SQLFederationProvider.class)).thenReturn(Collections.singleton(expected));
- assertThat(SQLFederationProviderFactory.getProvider("UNKNOWN"), sameInstance(expected));
- }
- }
-
- @Test
- void assertDefaultProviderMissing() {
- try (MockedStatic<ShardingSphereServiceLoader> serviceLoader = mockStatic(ShardingSphereServiceLoader.class)) {
- serviceLoader.when(() -> ShardingSphereServiceLoader.getServiceInstances(SQLFederationProvider.class)).thenReturn(Collections.emptyList());
- ServiceProviderNotFoundException actual = assertThrows(ServiceProviderNotFoundException.class, () -> SQLFederationProviderFactory.getProvider(null));
- assertThat(actual.getMessage(), is("SPI-00001: No implementation class load from SPI 'org.apache.shardingsphere.sqlfederation.spi.SQLFederationProvider' with type 'null'."));
- }
- }
-
- @Test
- void assertDefaultNoneProvider() {
- SQLFederationProvider expected = mock(SQLFederationProvider.class);
- when(expected.getType()).thenReturn("NONE");
- when(expected.isDefault()).thenReturn(true);
- try (MockedStatic<ShardingSphereServiceLoader> serviceLoader = mockStatic(ShardingSphereServiceLoader.class)) {
- serviceLoader.when(() -> ShardingSphereServiceLoader.getServiceInstances(SQLFederationProvider.class)).thenReturn(Collections.singleton(expected));
- assertThat(SQLFederationProviderFactory.getProvider(null), sameInstance(expected));
- }
- }
-
- @Test
- void assertDuplicateProviderType() {
- SQLFederationProvider first = mock(SQLFederationProvider.class);
- SQLFederationProvider second = mock(SQLFederationProvider.class);
- when(first.getType()).thenReturn("CALCITE");
- when(second.getType()).thenReturn("calcite");
- try (MockedStatic<ShardingSphereServiceLoader> serviceLoader = mockStatic(ShardingSphereServiceLoader.class)) {
- serviceLoader.when(() -> ShardingSphereServiceLoader.getServiceInstances(SQLFederationProvider.class)).thenReturn(Arrays.asList(first, second));
- SQLFederationProviderDuplicatedException actual = assertThrows(SQLFederationProviderDuplicatedException.class, () -> SQLFederationProviderFactory.getProvider(null));
- assertThat(actual.getMessage(), is("SQL_FEDERATION-00002: Multiple SQL Federation providers have type 'calcite'."));
- }
- }
-
- @Test
- void assertValidProviderWithoutRuleInitialization() {
- SQLFederationProvider expected = mock(SQLFederationProvider.class);
- when(expected.getType()).thenReturn("CALCITE");
- try (MockedStatic<ShardingSphereServiceLoader> serviceLoader = mockStatic(ShardingSphereServiceLoader.class)) {
- serviceLoader.when(() -> ShardingSphereServiceLoader.getServiceInstances(SQLFederationProvider.class)).thenReturn(Collections.singleton(expected));
- SQLFederationProvider actual = SQLFederationProviderFactory.getProvider("calcite");
- assertThat(actual, sameInstance(expected));
- verify(expected, never()).initialize(any(), any());
- }
- }
-}
diff --git a/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRuleTest.java b/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRuleTest.java
index 9facbafe088..f6a3660c71e 100644
--- a/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRuleTest.java
+++ b/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/rule/SQLFederationRuleTest.java
@@ -23,7 +23,6 @@ import org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundExce
import org.apache.shardingsphere.sqlfederation.config.SQLFederationCacheOption;
import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
import org.apache.shardingsphere.sqlfederation.constant.SQLFederationOrder;
-import org.apache.shardingsphere.sqlfederation.exception.SQLFederationProviderDuplicatedException;
import org.apache.shardingsphere.sqlfederation.spi.SQLFederationProvider;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
@@ -88,15 +87,17 @@ class SQLFederationRuleTest {
}
@Test
- void assertDuplicateProviderTypeFails() {
+ void assertConstructWithDuplicateProviderTypes() {
SQLFederationProvider first = mock(SQLFederationProvider.class);
SQLFederationProvider second = mock(SQLFederationProvider.class);
when(first.getType()).thenReturn("CALCITE");
when(second.getType()).thenReturn("calcite");
try (MockedStatic<ShardingSphereServiceLoader> serviceLoader = mockStatic(ShardingSphereServiceLoader.class)) {
serviceLoader.when(() -> ShardingSphereServiceLoader.getServiceInstances(SQLFederationProvider.class)).thenReturn(Arrays.asList(first, second));
- SQLFederationRuleConfiguration config = new SQLFederationRuleConfiguration(true, false, new SQLFederationCacheOption(4, 64L));
- assertThrows(SQLFederationProviderDuplicatedException.class, () -> new SQLFederationRule(config, Collections.emptyList()));
+ SQLFederationRuleConfiguration config = new SQLFederationRuleConfiguration(true, false, new SQLFederationCacheOption(4, 64L), "CALCITE");
+ SQLFederationRule actual = new SQLFederationRule(config, Collections.emptyList());
+ assertThat(actual.getProvider(), sameInstance(first));
+ verify(first).initialize(config, Collections.emptyList());
}
}
}
diff --git a/kernel/sql-federation/distsql/handler/src/main/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutor.java b/kernel/sql-federation/distsql/handler/src/main/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutor.java
index 90a61a4af10..f5b25b0764a 100644
--- a/kernel/sql-federation/distsql/handler/src/main/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutor.java
+++ b/kernel/sql-federation/distsql/handler/src/main/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutor.java
@@ -19,12 +19,13 @@ package org.apache.shardingsphere.sqlfederation.distsql.handler.update;
import lombok.Setter;
import org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.global.GlobalRuleDefinitionExecutor;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sqlfederation.config.SQLFederationCacheOption;
import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
import org.apache.shardingsphere.sqlfederation.distsql.segment.CacheOptionSegment;
import org.apache.shardingsphere.sqlfederation.distsql.statement.updatable.AlterSQLFederationRuleStatement;
-import org.apache.shardingsphere.sqlfederation.rule.SQLFederationProviderFactory;
import org.apache.shardingsphere.sqlfederation.rule.SQLFederationRule;
+import org.apache.shardingsphere.sqlfederation.spi.SQLFederationProvider;
/**
* Alter SQL federation rule executor.
@@ -44,7 +45,7 @@ public final class AlterSQLFederationRuleExecutor implements GlobalRuleDefinitio
String providerType = null == sqlStatement.getProviderType() ? rule.getConfiguration().getProviderType() : sqlStatement.getProviderType();
SQLFederationRuleConfiguration result = new SQLFederationRuleConfiguration(sqlFederationEnabled, allQueryUseSQLFederation, executionPlanCache, providerType);
if (result.isSqlFederationEnabled()) {
- SQLFederationProviderFactory.getProvider(result.getProviderType());
+ TypedSPILoader.getService(SQLFederationProvider.class, result.getProviderType());
}
return result;
}
diff --git a/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutorTest.java b/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutorTest.java
index 3904c0bbec6..1f89762fdad 100644
--- a/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutorTest.java
+++ b/kernel/sql-federation/distsql/handler/src/test/java/org/apache/shardingsphere/sqlfederation/distsql/handler/update/AlterSQLFederationRuleExecutorTest.java
@@ -28,7 +28,6 @@ import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistServ
import org.apache.shardingsphere.sqlfederation.config.SQLFederationCacheOption;
import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
import org.apache.shardingsphere.sqlfederation.distsql.statement.updatable.AlterSQLFederationRuleStatement;
-import org.apache.shardingsphere.sqlfederation.exception.SQLFederationProviderDuplicatedException;
import org.apache.shardingsphere.sqlfederation.rule.SQLFederationRule;
import org.apache.shardingsphere.sqlfederation.spi.SQLFederationProvider;
import org.apache.shardingsphere.test.it.distsql.handler.engine.update.DistSQLGlobalRuleDefinitionExecutorAssert;
@@ -40,7 +39,6 @@ import org.junit.jupiter.params.provider.ArgumentsSource;
import org.mockito.MockedStatic;
import java.sql.SQLException;
-import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -87,16 +85,6 @@ class AlterSQLFederationRuleExecutorTest {
"SPI-00001: No implementation class load from SPI 'org.apache.shardingsphere.sqlfederation.spi.SQLFederationProvider' with type 'null'.");
}
- @Test
- void assertDuplicateProviderBeforePersistence() {
- SQLFederationProvider first = mock(SQLFederationProvider.class);
- SQLFederationProvider second = mock(SQLFederationProvider.class);
- when(first.getType()).thenReturn("CALCITE");
- when(second.getType()).thenReturn("calcite");
- assertRejectedBeforePersistence(new AlterSQLFederationRuleStatement(true, null, null), Arrays.asList(first, second), SQLFederationProviderDuplicatedException.class,
- "SQL_FEDERATION-00002: Multiple SQL Federation providers have type 'calcite'.");
- }
-
@Test
void assertDisabledUnknownProviderPersists() {
SQLFederationCacheOption cacheOption = new SQLFederationCacheOption(4, 64L);