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/27 13:06:55 UTC
(shardingsphere) branch master updated: Delegate SQL support checks to providers (#39980)
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 7f1b55421a6 Delegate SQL support checks to providers (#39980)
7f1b55421a6 is described below
commit 7f1b55421a67a70ec84882fd83cb6d0a2771cf53
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Sep 27 21:06:47 2026 +0800
Delegate SQL support checks to providers (#39980)
---
.../sqlfederation/engine/SQLFederationEngine.java | 2 +-
.../sqlfederation/spi/SQLFederationProvider.java | 11 +
.../engine/SQLFederationEngineTest.java | 255 +++++++++++++++++++++
.../calcite/CalciteSQLFederationProvider.java | 8 +
....java => CalciteSQLFederationExecutorTest.java} | 231 +++++--------------
.../calcite/CalciteSQLFederationProviderTest.java | 25 ++
.../decider/SQLFederationDeciderMatchFixture.java | 47 ----
.../SQLFederationDeciderNotMatchFixture.java | 47 ----
.../rule/SQLFederationDeciderRuleMatchFixture.java | 38 ---
.../SQLFederationDeciderRuleNotMatchFixture.java | 38 ---
...ngsphere.sqlfederation.spi.SQLFederationDecider | 19 --
.../provider/none/NoneSQLFederationProvider.java | 9 +-
.../none/NoneSQLFederationProviderTest.java | 24 +-
13 files changed, 381 insertions(+), 373 deletions(-)
diff --git a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java
index b35fcc55d0e..2dcc3aab22d 100644
--- a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java
+++ b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java
@@ -164,7 +164,7 @@ public final class SQLFederationEngine implements AutoCloseable {
}
private boolean isSupportedSQLStatement(final SQLStatement sqlStatement) {
- return sqlStatement instanceof SelectStatement;
+ return sqlFederationRule.getProvider().isSupportedSQLStatement(sqlStatement);
}
/**
diff --git a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/spi/SQLFederationProvider.java b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/spi/SQLFederationProvider.java
index 0b9339de8ad..a1953d36dac 100644
--- a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/spi/SQLFederationProvider.java
+++ b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/spi/SQLFederationProvider.java
@@ -17,11 +17,13 @@
package org.apache.shardingsphere.sqlfederation.spi;
+import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
import org.apache.shardingsphere.infra.executor.sql.process.ProcessEngine;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
import java.util.Collection;
@@ -46,6 +48,15 @@ public interface SQLFederationProvider extends TypedSPI {
*/
void refresh(Collection<ShardingSphereDatabase> databases);
+ /**
+ * Check whether the SQL statement type is eligible for SQL federation.
+ *
+ * @param sqlStatement SQL statement to check
+ * @return whether the SQL statement type is eligible for SQL federation
+ */
+ @HighFrequencyInvocation
+ boolean isSupportedSQLStatement(SQLStatement sqlStatement);
+
/**
* Create SQL federation executor.
*
diff --git a/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngineTest.java b/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngineTest.java
new file mode 100644
index 00000000000..24b10b8d4b3
--- /dev/null
+++ b/kernel/sql-federation/core/src/test/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngineTest.java
@@ -0,0 +1,255 @@
+/*
+ * 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.engine;
+
+import org.apache.shardingsphere.infra.binder.context.statement.type.dal.ExplainStatementContext;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.infra.session.query.QueryContext;
+import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.ExplainStatement;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.table.CreateTableStatement;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.SelectStatement;
+import org.apache.shardingsphere.sqlfederation.config.SQLFederationCacheOption;
+import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
+import org.apache.shardingsphere.sqlfederation.context.SQLFederationContext;
+import org.apache.shardingsphere.sqlfederation.rule.SQLFederationRule;
+import org.apache.shardingsphere.sqlfederation.spi.SQLFederationDecider;
+import org.apache.shardingsphere.sqlfederation.spi.SQLFederationExecutor;
+import org.apache.shardingsphere.sqlfederation.spi.SQLFederationProvider;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.sameInstance;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class SQLFederationEngineTest {
+
+ private final SQLFederationCacheOption cacheOption = new SQLFederationCacheOption(1, 1L);
+
+ @Test
+ void assertCreatePreviewEngineWithoutJDBCExecutor() throws SQLException {
+ SQLFederationProvider provider = mock(SQLFederationProvider.class);
+ SQLFederationExecutor executor = mock(SQLFederationExecutor.class);
+ ShardingSphereStatistics statistics = mock(ShardingSphereStatistics.class);
+ when(provider.createPreviewExecutor(eq("foo_db"), eq("foo_schema"), eq(statistics), any())).thenReturn(executor);
+ ShardingSphereMetaData metaData = createMetaData(provider, new SQLFederationRuleConfiguration(true, false, cacheOption), Collections.emptyList());
+ try (SQLFederationEngine engine = new SQLFederationEngine("foo_db", "foo_schema", metaData, statistics)) {
+ assertThat(engine.getExecution(), sameInstance(executor));
+ }
+ }
+
+ private ShardingSphereMetaData createMetaData(final SQLFederationProvider provider, final SQLFederationRuleConfiguration config,
+ final Collection<ShardingSphereRule> databaseRules) {
+ SQLFederationRule rule = mock(SQLFederationRule.class);
+ when(rule.getConfiguration()).thenReturn(config);
+ when(rule.getProvider()).thenReturn(provider);
+ RuleMetaData globalRuleMetaData = mock(RuleMetaData.class);
+ when(globalRuleMetaData.getSingleRule(SQLFederationRule.class)).thenReturn(rule);
+ ShardingSphereMetaData result = mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS);
+ when(result.getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
+ when(result.getDatabase("foo_db").getRuleMetaData().getRules()).thenReturn(databaseRules);
+ return result;
+ }
+
+ @Test
+ void assertDecideWhenSQLFederationDisabled() throws SQLException {
+ SQLFederationProvider provider = mock(SQLFederationProvider.class);
+ ShardingSphereMetaData metaData = createMetaData(provider, new SQLFederationRuleConfiguration(false, false, cacheOption), Collections.emptyList());
+ try (SQLFederationEngine engine = new SQLFederationEngine("foo_db", "foo_schema", metaData, mock(), mock())) {
+ assertFalse(engine.decide(mock(QueryContext.class), mock(RuleMetaData.class)));
+ }
+ }
+
+ @Test
+ void assertDecideWhenEnableAllQueryUseSQLFederation() throws SQLException {
+ SQLFederationProvider provider = mock(SQLFederationProvider.class);
+ SelectStatement statement = mock(SelectStatement.class);
+ when(provider.isSupportedSQLStatement(statement)).thenReturn(true);
+ QueryContext queryContext = createQueryContext(statement);
+ ShardingSphereMetaData metaData = createMetaData(provider, new SQLFederationRuleConfiguration(true, true, cacheOption), Collections.emptyList());
+ try (SQLFederationEngine engine = new SQLFederationEngine("foo_db", "foo_schema", metaData, mock(), mock())) {
+ assertTrue(engine.decide(queryContext, mock(RuleMetaData.class)));
+ }
+ }
+
+ private QueryContext createQueryContext(final SQLStatement statement) {
+ QueryContext result = mock(QueryContext.class, RETURNS_DEEP_STUBS);
+ when(result.getSqlStatementContext().getSqlStatement()).thenReturn(statement);
+ return result;
+ }
+
+ @Test
+ void assertDecideWhenProviderRejectsStatement() throws SQLException {
+ SQLFederationProvider provider = mock(SQLFederationProvider.class);
+ QueryContext queryContext = createQueryContext(mock(CreateTableStatement.class));
+ ShardingSphereMetaData metaData = createMetaData(provider, new SQLFederationRuleConfiguration(true, true, cacheOption), Collections.emptyList());
+ try (SQLFederationEngine engine = new SQLFederationEngine("foo_db", "foo_schema", metaData, mock(), mock())) {
+ assertFalse(engine.decide(queryContext, mock(RuleMetaData.class)));
+ }
+ }
+
+ @Test
+ void assertDecideWithProviderSupportingNonSelectStatement() throws SQLException {
+ SQLFederationProvider provider = mock(SQLFederationProvider.class);
+ CreateTableStatement statement = mock(CreateTableStatement.class);
+ when(provider.isSupportedSQLStatement(statement)).thenReturn(true);
+ QueryContext queryContext = createQueryContext(statement);
+ ShardingSphereMetaData metaData = createMetaData(provider, new SQLFederationRuleConfiguration(true, true, cacheOption), Collections.emptyList());
+ try (SQLFederationEngine engine = new SQLFederationEngine("foo_db", "foo_schema", metaData, mock(), mock())) {
+ assertTrue(engine.decide(queryContext, mock(RuleMetaData.class)));
+ }
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ @Test
+ void assertDecideWithNotMatchedRule() throws SQLException {
+ SQLFederationProvider provider = mock(SQLFederationProvider.class);
+ SelectStatement statement = mock(SelectStatement.class);
+ when(provider.isSupportedSQLStatement(statement)).thenReturn(true);
+ QueryContext queryContext = createQueryContext(statement);
+ when(queryContext.getSqlStatementContext().getTablesContext().getDatabaseNames()).thenReturn(Collections.singleton("foo_db"));
+ ShardingSphereRule rule = mock(ShardingSphereRule.class);
+ Collection<ShardingSphereRule> databaseRules = Collections.singleton(rule);
+ SQLFederationDecider decider = mock(SQLFederationDecider.class);
+ Map<ShardingSphereRule, SQLFederationDecider> deciders = Collections.singletonMap(rule, decider);
+ ShardingSphereMetaData metaData = createMetaData(provider, new SQLFederationRuleConfiguration(true, false, cacheOption), databaseRules);
+ try (
+ MockedStatic<OrderedSPILoader> loader = mockStatic(OrderedSPILoader.class);
+ SQLFederationEngine engine = new SQLFederationEngine("foo_db", "foo_schema", metaData, mock(), mock())) {
+ loader.when(() -> OrderedSPILoader.getServices(SQLFederationDecider.class, databaseRules)).thenReturn(deciders);
+ assertFalse(engine.decide(queryContext, mock(RuleMetaData.class)));
+ verify(decider).decide(any(), any(), any(), any(), eq(rule), any());
+ }
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ @Test
+ void assertDecideWithMultipleRules() throws SQLException {
+ SQLFederationProvider provider = mock(SQLFederationProvider.class);
+ SelectStatement statement = mock(SelectStatement.class);
+ when(provider.isSupportedSQLStatement(statement)).thenReturn(true);
+ QueryContext queryContext = createQueryContext(statement);
+ when(queryContext.getSqlStatementContext().getTablesContext().getDatabaseNames()).thenReturn(Collections.singleton("foo_db"));
+ ShardingSphereRule firstRule = mock(ShardingSphereRule.class);
+ ShardingSphereRule secondRule = mock(ShardingSphereRule.class);
+ Collection<ShardingSphereRule> databaseRules = Arrays.asList(firstRule, secondRule);
+ SQLFederationDecider firstDecider = mock(SQLFederationDecider.class);
+ SQLFederationDecider secondDecider = mock(SQLFederationDecider.class);
+ when(secondDecider.decide(any(), any(), any(), any(), eq(secondRule), any())).thenReturn(true);
+ Map<ShardingSphereRule, SQLFederationDecider> deciders = new LinkedHashMap<>(2);
+ deciders.put(firstRule, firstDecider);
+ deciders.put(secondRule, secondDecider);
+ ShardingSphereMetaData metaData = createMetaData(provider, new SQLFederationRuleConfiguration(true, false, cacheOption), databaseRules);
+ try (
+ MockedStatic<OrderedSPILoader> loader = mockStatic(OrderedSPILoader.class);
+ SQLFederationEngine engine = new SQLFederationEngine("foo_db", "foo_schema", metaData, mock(), mock())) {
+ loader.when(() -> OrderedSPILoader.getServices(SQLFederationDecider.class, databaseRules)).thenReturn(deciders);
+ assertTrue(engine.decide(queryContext, mock(RuleMetaData.class)));
+ verify(firstDecider).decide(any(), any(), any(), any(), eq(firstRule), any());
+ verify(secondDecider).decide(any(), any(), any(), any(), eq(secondRule), any());
+ }
+ }
+
+ @Test
+ void assertDecideWithMultipleDatabases() throws SQLException {
+ SQLFederationProvider provider = mock(SQLFederationProvider.class);
+ SelectStatement statement = mock(SelectStatement.class);
+ when(provider.isSupportedSQLStatement(statement)).thenReturn(true);
+ QueryContext queryContext = createQueryContext(statement);
+ when(queryContext.getSqlStatementContext().getTablesContext().getDatabaseNames()).thenReturn(Arrays.asList("foo_db", "bar_db"));
+ ShardingSphereMetaData metaData = createMetaData(provider, new SQLFederationRuleConfiguration(true, false, cacheOption), Collections.emptyList());
+ try (SQLFederationEngine engine = new SQLFederationEngine("foo_db", "foo_schema", metaData, mock(), mock())) {
+ assertTrue(engine.decide(queryContext, mock(RuleMetaData.class)));
+ }
+ }
+
+ @Test
+ void assertDecideWithExplainStatement() throws SQLException {
+ SQLFederationProvider provider = mock(SQLFederationProvider.class);
+ SelectStatement selectStatement = mock(SelectStatement.class);
+ when(provider.isSupportedSQLStatement(selectStatement)).thenReturn(true);
+ ExplainStatement explainStatement = mock(ExplainStatement.class);
+ when(explainStatement.getExplainableSQLStatement()).thenReturn(selectStatement);
+ ExplainStatementContext statementContext = mock(ExplainStatementContext.class);
+ when(statementContext.getSqlStatement()).thenReturn(explainStatement);
+ QueryContext queryContext = mock(QueryContext.class);
+ when(queryContext.getSqlStatementContext()).thenReturn(statementContext);
+ ShardingSphereMetaData metaData = createMetaData(provider, new SQLFederationRuleConfiguration(true, true, cacheOption), Collections.emptyList());
+ try (SQLFederationEngine engine = new SQLFederationEngine("foo_db", "foo_schema", metaData, mock(), mock())) {
+ assertTrue(engine.decide(queryContext, mock(RuleMetaData.class)));
+ verify(provider).isSupportedSQLStatement(selectStatement);
+ }
+ }
+
+ @Test
+ void assertGetResultSetForSelectStatement() throws SQLException {
+ SQLFederationProvider provider = mock(SQLFederationProvider.class);
+ SQLFederationExecutor executor = mock(SQLFederationExecutor.class);
+ when(provider.createExecutor(anyString(), anyString(), any(), any(), any())).thenReturn(executor);
+ ResultSet expectedResultSet = mock(ResultSet.class);
+ SQLFederationContext federationContext = mock(SQLFederationContext.class);
+ QueryContext queryContext = createQueryContext(mock(SelectStatement.class));
+ when(federationContext.getQueryContext()).thenReturn(queryContext);
+ when(executor.executeQuery(any(), any(), eq(federationContext))).thenReturn(expectedResultSet);
+ ShardingSphereMetaData metaData = createMetaData(provider, new SQLFederationRuleConfiguration(true, false, cacheOption), Collections.emptyList());
+ try (SQLFederationEngine engine = new SQLFederationEngine("foo_db", "foo_schema", metaData, mock(), mock())) {
+ engine.executeQuery(mock(), mock(), federationContext);
+ assertThat(engine.getResultSet(), sameInstance(expectedResultSet));
+ }
+ }
+
+ @Test
+ void assertGetResultSetForNonSelectStatement() throws SQLException {
+ SQLFederationProvider provider = mock(SQLFederationProvider.class);
+ SQLFederationExecutor executor = mock(SQLFederationExecutor.class);
+ when(provider.createExecutor(anyString(), anyString(), any(), any(), any())).thenReturn(executor);
+ SQLFederationContext federationContext = mock(SQLFederationContext.class);
+ QueryContext queryContext = createQueryContext(mock(CreateTableStatement.class));
+ when(federationContext.getQueryContext()).thenReturn(queryContext);
+ when(executor.executeQuery(any(), any(), eq(federationContext))).thenReturn(mock(ResultSet.class));
+ ShardingSphereMetaData metaData = createMetaData(provider, new SQLFederationRuleConfiguration(true, false, cacheOption), Collections.emptyList());
+ try (SQLFederationEngine engine = new SQLFederationEngine("foo_db", "foo_schema", metaData, mock(), mock())) {
+ engine.executeQuery(mock(), mock(), federationContext);
+ assertNull(engine.getResultSet());
+ }
+ }
+
+}
diff --git a/kernel/sql-federation/provider/calcite/src/main/java/org/apache/shardingsphere/sqlfederation/provider/calcite/CalciteSQLFederationProvider.java b/kernel/sql-federation/provider/calcite/src/main/java/org/apache/shardingsphere/sqlfederation/provider/calcite/CalciteSQLFederationProvider.java
index 8a907ed944d..56dee3b3660 100644
--- a/kernel/sql-federation/provider/calcite/src/main/java/org/apache/shardingsphere/sqlfederation/provider/calcite/CalciteSQLFederationProvider.java
+++ b/kernel/sql-federation/provider/calcite/src/main/java/org/apache/shardingsphere/sqlfederation/provider/calcite/CalciteSQLFederationProvider.java
@@ -22,6 +22,8 @@ import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.J
import org.apache.shardingsphere.infra.executor.sql.process.ProcessEngine;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.SelectStatement;
import org.apache.shardingsphere.sqlfederation.compiler.context.CompilerContext;
import org.apache.shardingsphere.sqlfederation.compiler.context.CompilerContextFactory;
import org.apache.shardingsphere.sqlfederation.config.SQLFederationCacheOption;
@@ -52,6 +54,12 @@ public final class CalciteSQLFederationProvider implements SQLFederationProvider
compilerContext.set(CompilerContextFactory.create(databases));
}
+ @HighFrequencyInvocation
+ @Override
+ public boolean isSupportedSQLStatement(final SQLStatement sqlStatement) {
+ return sqlStatement instanceof SelectStatement;
+ }
+
CompilerContext getCompilerContext() {
return compilerContext.get();
}
diff --git a/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/SQLFederationEngineTest.java b/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/CalciteSQLFederationExecutorTest.java
similarity index 71%
rename from kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/SQLFederationEngineTest.java
rename to kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/CalciteSQLFederationExecutorTest.java
index a927f682fab..f8bcc470934 100644
--- a/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/SQLFederationEngineTest.java
+++ b/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/CalciteSQLFederationExecutorTest.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.sqlfederation.provider.calcite.engine;
+package org.apache.shardingsphere.sqlfederation.provider.calcite;
import org.apache.calcite.plan.RelOptUtil;
import org.apache.calcite.rel.RelNode;
@@ -28,7 +28,6 @@ import org.apache.shardingsphere.database.connector.core.type.DatabaseTypeRegist
import org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.type.dal.ExplainStatementContext;
-import org.apache.shardingsphere.infra.binder.context.statement.type.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
@@ -36,6 +35,7 @@ import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.J
import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback;
import org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult;
import org.apache.shardingsphere.infra.executor.sql.prepare.driver.DriverExecutionPrepareEngine;
+import org.apache.shardingsphere.infra.executor.sql.process.ProcessEngine;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
@@ -66,10 +66,6 @@ import org.apache.shardingsphere.sqlfederation.compiler.rel.converter.SQLFederat
import org.apache.shardingsphere.sqlfederation.config.SQLFederationCacheOption;
import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
import org.apache.shardingsphere.sqlfederation.context.SQLFederationContext;
-import org.apache.shardingsphere.sqlfederation.engine.SQLFederationEngine;
-import org.apache.shardingsphere.sqlfederation.provider.calcite.CalciteSQLFederationExecutor;
-import org.apache.shardingsphere.sqlfederation.provider.calcite.engine.fixture.rule.SQLFederationDeciderRuleMatchFixture;
-import org.apache.shardingsphere.sqlfederation.provider.calcite.engine.fixture.rule.SQLFederationDeciderRuleNotMatchFixture;
import org.apache.shardingsphere.sqlfederation.provider.calcite.engine.processor.SQLFederationProcessor;
import org.apache.shardingsphere.sqlfederation.provider.calcite.engine.processor.SQLFederationProcessorFactory;
import org.apache.shardingsphere.sqlfederation.rule.SQLFederationRule;
@@ -93,11 +89,7 @@ import java.util.concurrent.atomic.AtomicReference;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.isA;
-import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
@@ -111,108 +103,12 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-class SQLFederationEngineTest {
+class CalciteSQLFederationExecutorTest {
private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
private final SQLFederationCacheOption cacheOption = new SQLFederationCacheOption(1, 1L);
- @Test
- void assertCreatePreviewEngineWithoutJDBCExecutor() throws SQLException {
- ShardingSphereMetaData metaData = createMetaData(new Properties());
- try (SQLFederationEngine engine = new SQLFederationEngine("foo_db", "foo_schema", metaData, mock(ShardingSphereStatistics.class))) {
- assertThat(engine.getExecution(), isA(CalciteSQLFederationExecutor.class));
- }
- }
-
- @Test
- void assertDecideWhenSQLFederationDisabled() throws SQLException {
- Collection<ShardingSphereRule> globalRules = Collections.singleton(new SQLFederationRule(new SQLFederationRuleConfiguration(false, false, cacheOption), Collections.emptyList()));
- try (SQLFederationEngine engine = createSQLFederationEngine(globalRules, Collections.emptyList())) {
- assertFalse(engine.decide(mock(QueryContext.class), new RuleMetaData(globalRules)));
- }
- }
-
- @Test
- void assertDecideWhenEnableAllQueryUseSQLFederation() throws SQLException {
- Collection<ShardingSphereRule> globalRules = Collections.singleton(new SQLFederationRule(new SQLFederationRuleConfiguration(true, true, cacheOption), Collections.emptyList()));
- QueryContext queryContext = mock(QueryContext.class);
- when(queryContext.getSqlStatementContext()).thenReturn(mock(SelectStatementContext.class, RETURNS_DEEP_STUBS));
- try (SQLFederationEngine engine = createSQLFederationEngine(globalRules, Collections.emptyList())) {
- assertTrue(engine.decide(queryContext, new RuleMetaData(globalRules)));
- }
- }
-
- @Test
- void assertDecideWhenExecuteNotSelectStatement() throws SQLException {
- Collection<ShardingSphereRule> globalRules = Collections.singleton(new SQLFederationRule(new SQLFederationRuleConfiguration(true, false, cacheOption), Collections.emptyList()));
- QueryContext queryContext = mock(QueryContext.class, RETURNS_DEEP_STUBS);
- when(queryContext.getSqlStatementContext().getSqlStatement()).thenReturn(mock(CreateTableStatement.class));
- try (SQLFederationEngine engine = createSQLFederationEngine(globalRules, Collections.emptyList())) {
- assertFalse(engine.decide(queryContext, new RuleMetaData(globalRules)));
- }
- }
-
- @Test
- void assertDecideWithNotMatchedRule() throws SQLException {
- final Collection<ShardingSphereRule> globalRules = Collections.singleton(new SQLFederationRule(new SQLFederationRuleConfiguration(true, false, cacheOption), Collections.emptyList()));
- Collection<ShardingSphereRule> databaseRules = Collections.singleton(new SQLFederationDeciderRuleNotMatchFixture());
- SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
- when(selectStatementContext.getTablesContext().getDatabaseNames()).thenReturn(Collections.singleton("foo_db"));
- QueryContext queryContext = mock(QueryContext.class);
- when(queryContext.getSqlStatementContext()).thenReturn(selectStatementContext);
- ResourceMetaData resourceMetaData = mock(ResourceMetaData.class);
- ShardingSphereDatabase database =
- new ShardingSphereDatabase("foo_db", databaseType, resourceMetaData, new RuleMetaData(databaseRules), Collections.emptyList(), new ConfigurationProperties(new Properties()));
- when(queryContext.getUsedDatabase()).thenReturn(database);
- try (SQLFederationEngine engine = createSQLFederationEngine(globalRules, databaseRules)) {
- assertFalse(engine.decide(queryContext, new RuleMetaData(globalRules)));
- }
- }
-
- @Test
- void assertDecideWithMultipleRules() throws SQLException {
- final Collection<ShardingSphereRule> globalRules = Collections.singleton(new SQLFederationRule(new SQLFederationRuleConfiguration(true, false, cacheOption), Collections.emptyList()));
- Collection<ShardingSphereRule> databaseRules = Arrays.asList(new SQLFederationDeciderRuleNotMatchFixture(), new SQLFederationDeciderRuleMatchFixture());
- SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
- when(selectStatementContext.getTablesContext().getDatabaseNames()).thenReturn(Collections.singleton("foo_db"));
- QueryContext queryContext = mock(QueryContext.class);
- when(queryContext.getSqlStatementContext()).thenReturn(selectStatementContext);
- ResourceMetaData resourceMetaData = mock(ResourceMetaData.class);
- ShardingSphereDatabase database =
- new ShardingSphereDatabase("foo_db", databaseType, resourceMetaData, new RuleMetaData(databaseRules), Collections.emptyList(), new ConfigurationProperties(new Properties()));
- when(queryContext.getUsedDatabase()).thenReturn(database);
- try (SQLFederationEngine engine = createSQLFederationEngine(globalRules, databaseRules)) {
- assertTrue(engine.decide(queryContext, new RuleMetaData(globalRules)));
- }
- }
-
- @Test
- void assertDecideWithMultipleDatabases() throws SQLException {
- Collection<ShardingSphereRule> globalRules = Collections.singleton(new SQLFederationRule(new SQLFederationRuleConfiguration(true, false, cacheOption), Collections.emptyList()));
- SelectStatementContext selectStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
- when(selectStatementContext.getTablesContext().getDatabaseNames()).thenReturn(Arrays.asList("foo_db", "bar_db"));
- QueryContext queryContext = mock(QueryContext.class);
- when(queryContext.getSqlStatementContext()).thenReturn(selectStatementContext);
- try (SQLFederationEngine engine = createSQLFederationEngine(globalRules, Collections.emptyList())) {
- assertTrue(engine.decide(queryContext, new RuleMetaData(globalRules)));
- }
- }
-
- @Test
- void assertDecideWithExplainStatement() throws SQLException {
- final Collection<ShardingSphereRule> globalRules = Collections.singleton(new SQLFederationRule(new SQLFederationRuleConfiguration(true, false, cacheOption), Collections.emptyList()));
- ExplainStatementContext explainStatementContext = mock(ExplainStatementContext.class, RETURNS_DEEP_STUBS);
- ExplainStatement explainStatement = mock(ExplainStatement.class, RETURNS_DEEP_STUBS);
- when(explainStatementContext.getSqlStatement()).thenReturn(explainStatement);
- when(explainStatementContext.getTablesContext().getDatabaseNames()).thenReturn(Collections.emptyList());
- QueryContext queryContext = mock(QueryContext.class);
- when(queryContext.getSqlStatementContext()).thenReturn(explainStatementContext);
- try (SQLFederationEngine engine = createSQLFederationEngine(globalRules, Collections.emptyList())) {
- assertFalse(engine.decide(queryContext, new RuleMetaData(globalRules)));
- }
- }
-
@SuppressWarnings("unchecked")
@Test
void assertExecuteQueryWithDefaultSchemaButWithoutOwner() throws SQLException {
@@ -228,7 +124,7 @@ class SQLFederationEngineTest {
SQLFederationContext federationContext = new SQLFederationContext(false, queryContext, actualMetaData, "process_schema_path");
AtomicReference<List<String>> actualSchemaPath = new AtomicReference<>();
try (
- SQLFederationEngine engine = createSQLFederationEngine(mock(), actualMetaData);
+ CalciteSQLFederationExecutor executor = createCalciteSQLFederationExecutor(mock(), actualMetaData);
MockedConstruction<DatabaseTypeRegistry> ignoredRegistry = mockConstruction(DatabaseTypeRegistry.class, (mock, context) -> {
DialectSchemaOption schemaOption = mock(DialectSchemaOption.class);
when(schemaOption.getDefaultSchema()).thenReturn(Optional.of("public"));
@@ -244,11 +140,38 @@ class SQLFederationEngineTest {
(mock, context) -> when(mock.compile(any(ExecutionPlanCacheKey.class), eq(false))).thenReturn(mock(SQLFederationExecutionPlan.class)));
MockedStatic<RelOptUtil> relOptUtil = mockStatic(RelOptUtil.class)) {
relOptUtil.when(() -> RelOptUtil.toString(any(RelNode.class), eq(SqlExplainLevel.ALL_ATTRIBUTES))).thenReturn("plan");
- engine.executeQuery(mock(), mock(), federationContext);
+ executor.executeQuery(mock(), mock(), federationContext);
assertThat(actualSchemaPath.get(), is(Arrays.asList("foo_db", "foo_schema")));
}
}
+ private ShardingSphereMetaData createMetaData(final Properties props) {
+ ShardingSphereTable table = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
+ return createMetaData(Collections.singleton(table), props);
+ }
+
+ private ShardingSphereMetaData createMetaData(final Collection<ShardingSphereTable> tables, final Properties props) {
+ ShardingSphereSchema schema = new ShardingSphereSchema("foo_schema", databaseType, tables, Collections.emptyList());
+ ShardingSphereDatabase database = new ShardingSphereDatabase(
+ "foo_db", databaseType, new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), Collections.singleton(schema),
+ new ConfigurationProperties(new Properties()));
+ SQLFederationRuleConfiguration ruleConfig = new SQLFederationRuleConfiguration(true, false, cacheOption);
+ Collection<ShardingSphereRule> globalRules = Collections.singleton(new SQLFederationRule(ruleConfig, Collections.singleton(database)));
+ return new ShardingSphereMetaData(Collections.singleton(database), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(globalRules), new ConfigurationProperties(props));
+ }
+
+ private CalciteSQLFederationExecutor createCalciteSQLFederationExecutor(final SQLFederationProcessor processor, final ShardingSphereMetaData metaData) {
+ ShardingSphereStatistics statistics = mock(ShardingSphereStatistics.class);
+ JDBCExecutor jdbcExecutor = mock(JDBCExecutor.class);
+ try (MockedStatic<SQLFederationProcessorFactory> factoryMock = mockStatic(SQLFederationProcessorFactory.class)) {
+ SQLFederationProcessorFactory factory = mock(SQLFederationProcessorFactory.class);
+ when(factory.newInstance(statistics, jdbcExecutor)).thenReturn(processor);
+ factoryMock.when(SQLFederationProcessorFactory::getInstance).thenReturn(factory);
+ CalciteSQLFederationProvider provider = (CalciteSQLFederationProvider) metaData.getGlobalRuleMetaData().getSingleRule(SQLFederationRule.class).getProvider();
+ return new CalciteSQLFederationExecutor("foo_db", "foo_schema", statistics, jdbcExecutor, new ProcessEngine(), provider);
+ }
+ }
+
@SuppressWarnings("unchecked")
@Test
void assertExecuteQueryWithLoggingAndRelease() throws SQLException {
@@ -275,7 +198,7 @@ class SQLFederationEngineTest {
ResultSet resultSet = mock(ResultSet.class);
SQLFederationProcessor processor = mock(SQLFederationProcessor.class);
when(processor.executePlan(eq(prepareEngine), eq(callback), any(SQLFederationExecutionPlan.class), any(SQLFederationRelConverter.class), eq(federationContext), any())).thenReturn(resultSet);
- SQLFederationEngine engine = createSQLFederationEngine(processor, actualMetaData);
+ CalciteSQLFederationExecutor executor = createCalciteSQLFederationExecutor(processor, actualMetaData);
try (
MockedConstruction<SQLFederationRelConverter> converterMocked = mockConstruction(SQLFederationRelConverter.class,
(mock, context) -> when(mock.getSchemaPlus()).thenReturn(mock(SchemaPlus.class)));
@@ -283,19 +206,19 @@ class SQLFederationEngineTest {
(mock, context) -> when(mock.compile(any(ExecutionPlanCacheKey.class), eq(false))).thenReturn(mock(SQLFederationExecutionPlan.class, RETURNS_DEEP_STUBS)));
MockedStatic<RelOptUtil> relOptUtil = mockStatic(RelOptUtil.class)) {
relOptUtil.when(() -> RelOptUtil.toString(any(RelNode.class), eq(SqlExplainLevel.ALL_ATTRIBUTES))).thenReturn("plan");
- assertThat(engine.executeQuery(prepareEngine, callback, federationContext), is(resultSet));
+ assertThat(executor.executeQuery(prepareEngine, callback, federationContext), is(resultSet));
ArgumentCaptor<ExecutionPlanCacheKey> cacheKeyCaptor = ArgumentCaptor.forClass(ExecutionPlanCacheKey.class);
verify(compilerMocked.constructed().get(0)).compile(cacheKeyCaptor.capture(), eq(false));
assertThat(cacheKeyCaptor.getValue().getTableMetaDataVersions().size(), is(1));
- assertThat(engine.getResultSet(), is(resultSet));
- engine.close();
+ assertThat(executor.getResultSet(), is(resultSet));
+ executor.close();
verify(processor).release("foo_db", "foo_schema", queryContext, converterMocked.constructed().get(0).getSchemaPlus());
}
}
@SuppressWarnings("unchecked")
@Test
- void assertExecuteQueryWithParametersAndOwner() {
+ void assertExecuteQueryWithBoundTable() {
TableNameSegment tableNameSegment = new TableNameSegment(0, 0, new IdentifierValue("foo_tbl"));
tableNameSegment.setTableBoundInfo(new TableSegmentBoundInfo(new IdentifierValue("foo_db"), new IdentifierValue("foo_schema")));
SimpleTableSegment simpleTableSegment = new SimpleTableSegment(tableNameSegment);
@@ -305,7 +228,7 @@ class SQLFederationEngineTest {
SelectStatement selectStatement = mock(SelectStatement.class);
when(selectStatement.getDatabaseType()).thenReturn(databaseType);
SQLStatementContext selectStatementContext = mock(SQLStatementContext.class);
- when(selectStatementContext.getSqlStatement()).thenReturn(selectStatement, selectStatement, selectStatement, mock(CreateTableStatement.class));
+ when(selectStatementContext.getSqlStatement()).thenReturn(selectStatement);
when(selectStatementContext.getTablesContext()).thenReturn(tablesContext);
QueryContext queryContext = mock(QueryContext.class);
when(queryContext.getSqlStatementContext()).thenReturn(selectStatementContext);
@@ -313,16 +236,18 @@ class SQLFederationEngineTest {
when(queryContext.getParameters()).thenReturn(Collections.singletonList(1));
when(queryContext.getConnectionContext()).thenReturn(new ConnectionContext(Collections::emptyList, new Grantee("root", "localhost")));
ShardingSphereMetaData actualMetaData = createMetaData(PropertiesBuilder.build(new Property(ConfigurationPropertyKey.SQL_SHOW.getKey(), Boolean.TRUE.toString())));
- SQLFederationEngine engine = createSQLFederationEngine(mock(), actualMetaData);
+ CalciteSQLFederationExecutor executor = createCalciteSQLFederationExecutor(mock(), actualMetaData);
try (
MockedConstruction<SQLFederationRelConverter> ignoredConverter = mockConstruction(SQLFederationRelConverter.class,
(mock, context) -> when(mock.getSchemaPlus()).thenReturn(mock(SchemaPlus.class)));
- MockedConstruction<SQLFederationCompilerEngine> ignoredCompiler = mockConstruction(SQLFederationCompilerEngine.class,
+ MockedConstruction<SQLFederationCompilerEngine> compilerMocked = mockConstruction(SQLFederationCompilerEngine.class,
(mock, context) -> when(mock.compile(any(ExecutionPlanCacheKey.class), eq(false))).thenReturn(mock(SQLFederationExecutionPlan.class, RETURNS_DEEP_STUBS)));
MockedStatic<RelOptUtil> relOptUtil = mockStatic(RelOptUtil.class)) {
relOptUtil.when(() -> RelOptUtil.toString(any(RelNode.class), eq(SqlExplainLevel.ALL_ATTRIBUTES))).thenReturn("plan");
- engine.executeQuery(mock(), mock(), new SQLFederationContext(false, queryContext, actualMetaData, "process_2"));
- assertNull(engine.getResultSet());
+ executor.executeQuery(mock(), mock(), new SQLFederationContext(false, queryContext, actualMetaData, "process_2"));
+ ArgumentCaptor<ExecutionPlanCacheKey> cacheKeyCaptor = ArgumentCaptor.forClass(ExecutionPlanCacheKey.class);
+ verify(compilerMocked.constructed().get(0)).compile(cacheKeyCaptor.capture(), eq(false));
+ assertThat(cacheKeyCaptor.getValue().getTableMetaDataVersions().get("foo_db.foo_schema.foo_tbl"), is(0));
}
}
@@ -344,7 +269,7 @@ class SQLFederationEngineTest {
SQLFederationExecutionPlan executionPlan = mock(SQLFederationExecutionPlan.class);
AtomicReference<List<String>> actualSchemaPath = new AtomicReference<>();
try (
- SQLFederationEngine engine = createSQLFederationEngine(mock(), actualMetaData);
+ CalciteSQLFederationExecutor executor = createCalciteSQLFederationExecutor(mock(), actualMetaData);
MockedConstruction<DatabaseTypeRegistry> ignoredRegistry = mockConstruction(DatabaseTypeRegistry.class, (mock, context) -> {
DialectSchemaOption schemaOption = mock(DialectSchemaOption.class);
when(schemaOption.getDefaultSchema()).thenReturn(Optional.of("public"));
@@ -358,7 +283,7 @@ class SQLFederationEngineTest {
(mock, context) -> when(mock.compile(any(ExecutionPlanCacheKey.class), eq(false))).thenReturn(executionPlan));
MockedStatic<RelOptUtil> relOptUtil = mockStatic(RelOptUtil.class)) {
relOptUtil.when(() -> RelOptUtil.toString(any(RelNode.class), eq(SqlExplainLevel.ALL_ATTRIBUTES))).thenReturn("plan");
- engine.executeQuery(prepareEngine, callback, federationContext);
+ executor.executeQuery(prepareEngine, callback, federationContext);
}
assertThat(actualSchemaPath.get(), is(Collections.singletonList("foo_db")));
}
@@ -385,14 +310,14 @@ class SQLFederationEngineTest {
SQLFederationProcessor processor = mock(SQLFederationProcessor.class);
when(processor.executePlan(eq(prepareEngine), eq(callback), eq(executionPlan), any(SQLFederationRelConverter.class), eq(federationContext), any())).thenReturn(resultSet);
try (
- SQLFederationEngine engine = createSQLFederationEngine(processor, actualMetaData);
+ CalciteSQLFederationExecutor executor = createCalciteSQLFederationExecutor(processor, actualMetaData);
MockedConstruction<SQLFederationRelConverter> ignored = mockConstruction(SQLFederationRelConverter.class,
(mock, context) -> when(mock.getSchemaPlus()).thenReturn(mock(SchemaPlus.class)));
MockedConstruction<SQLFederationCompilerEngine> ignoredCompiler = mockConstruction(SQLFederationCompilerEngine.class,
(mock, context) -> when(mock.compile(any(ExecutionPlanCacheKey.class), eq(false))).thenReturn(executionPlan));
MockedStatic<RelOptUtil> relOptUtil = mockStatic(RelOptUtil.class)) {
relOptUtil.when(() -> RelOptUtil.toString(any(RelNode.class), eq(SqlExplainLevel.ALL_ATTRIBUTES))).thenReturn("plan");
- engine.executeQuery(prepareEngine, callback, federationContext);
+ executor.executeQuery(prepareEngine, callback, federationContext);
}
}
@@ -414,7 +339,7 @@ class SQLFederationEngineTest {
DriverExecutionPrepareEngine<JDBCExecutionUnit, Connection> prepareEngine = mock(DriverExecutionPrepareEngine.class);
JDBCExecutorCallback<? extends ExecuteResult> callback = mock(JDBCExecutorCallback.class);
SQLFederationExecutionPlan executionPlan = mock(SQLFederationExecutionPlan.class);
- SQLFederationEngine engine = createSQLFederationEngine(processor, actualMetaData);
+ CalciteSQLFederationExecutor executor = createCalciteSQLFederationExecutor(processor, actualMetaData);
try (
MockedConstruction<SQLFederationRelConverter> ignored = mockConstruction(SQLFederationRelConverter.class,
(mock, context) -> when(mock.getSchemaPlus()).thenReturn(mock(SchemaPlus.class)));
@@ -425,30 +350,16 @@ class SQLFederationEngineTest {
doAnswer(invocation -> {
throw new SQLIntegrityConstraintViolationException();
}).when(processor).executePlan(eq(prepareEngine), eq(callback), eq(executionPlan), any(SQLFederationRelConverter.class), eq(federationContext), any());
- assertThrows(SQLIntegrityConstraintViolationException.class, () -> engine.executeQuery(prepareEngine, callback, federationContext));
+ assertThrows(SQLIntegrityConstraintViolationException.class, () -> executor.executeQuery(prepareEngine, callback, federationContext));
}
}
- @Test
- void assertGetResultSetForSelectStatement() throws ReflectiveOperationException {
- ShardingSphereMetaData actualMetaData = createMetaData(Collections.emptyList(), new Properties());
- SQLStatementContext selectStatementContext = mock(SQLStatementContext.class, RETURNS_DEEP_STUBS);
- when(selectStatementContext.getSqlStatement()).thenReturn(mock(SelectStatement.class));
- QueryContext selectQueryContext = mock(QueryContext.class);
- when(selectQueryContext.getSqlStatementContext()).thenReturn(selectStatementContext);
- ResultSet expectedResultSet = mock(ResultSet.class);
- SQLFederationEngine engine = createSQLFederationEngine(mock(), actualMetaData);
- Plugins.getMemberAccessor().set(SQLFederationEngine.class.getDeclaredField("queryContext"), engine, selectQueryContext);
- Plugins.getMemberAccessor().set(SQLFederationEngine.class.getDeclaredField("resultSet"), engine, expectedResultSet);
- assertThat(engine.getResultSet(), is(expectedResultSet));
- }
-
@Test
void assertCloseWithoutSchema() throws SQLException, ReflectiveOperationException {
ShardingSphereMetaData actualMetaData = createMetaData(Collections.emptyList(), new Properties());
SQLFederationProcessor processor = mock(SQLFederationProcessor.class);
- try (SQLFederationEngine engine = createSQLFederationEngine(processor, actualMetaData)) {
- Plugins.getMemberAccessor().set(SQLFederationEngine.class.getDeclaredField("queryContext"), engine, mock(QueryContext.class));
+ try (CalciteSQLFederationExecutor executor = createCalciteSQLFederationExecutor(processor, actualMetaData)) {
+ Plugins.getMemberAccessor().set(CalciteSQLFederationExecutor.class.getDeclaredField("queryContext"), executor, mock(QueryContext.class));
verify(processor, never()).release(any(), any(), any(), any());
}
}
@@ -467,10 +378,10 @@ class SQLFederationEngineTest {
DriverExecutionPrepareEngine<JDBCExecutionUnit, Connection> prepareEngine = mock(DriverExecutionPrepareEngine.class);
JDBCExecutorCallback<? extends ExecuteResult> callback = mock(JDBCExecutorCallback.class);
SQLFederationProcessor processor = mock(SQLFederationProcessor.class);
- SQLFederationEngine engine = createSQLFederationEngine(processor, actualMetaData);
+ CalciteSQLFederationExecutor executor = createCalciteSQLFederationExecutor(processor, actualMetaData);
ResultSet closableResultSet = mock(ResultSet.class);
doThrow(SQLException.class).when(closableResultSet).close();
- Plugins.getMemberAccessor().set(SQLFederationEngine.class.getDeclaredField("resultSet"), engine, closableResultSet);
+ Plugins.getMemberAccessor().set(CalciteSQLFederationExecutor.class.getDeclaredField("resultSet"), executor, closableResultSet);
try (
MockedConstruction<SQLFederationRelConverter> ignoredConverter = mockConstruction(SQLFederationRelConverter.class,
(mock, context) -> when(mock.getSchemaPlus()).thenReturn(mock(SchemaPlus.class)));
@@ -479,40 +390,8 @@ class SQLFederationEngineTest {
MockedStatic<RelOptUtil> relOptUtil = mockStatic(RelOptUtil.class)) {
relOptUtil.when(() -> RelOptUtil.toString(any(RelNode.class), eq(SqlExplainLevel.ALL_ATTRIBUTES))).thenReturn("plan");
doThrow(RuntimeException.class).when(processor).prepare(eq(prepareEngine), eq(callback), anyString(), anyString(), eq(federationContext), any(), any(SchemaPlus.class));
- assertThrows(SQLFederationUnsupportedSQLException.class, () -> engine.executeQuery(prepareEngine, callback, federationContext));
+ assertThrows(SQLFederationUnsupportedSQLException.class, () -> executor.executeQuery(prepareEngine, callback, federationContext));
}
}
- private SQLFederationEngine createSQLFederationEngine(final Collection<ShardingSphereRule> globalRules, final Collection<ShardingSphereRule> databaseRules) {
- ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS);
- when(metaData.getGlobalRuleMetaData()).thenReturn(new RuleMetaData(globalRules));
- when(metaData.getDatabase("foo_db").getRuleMetaData().getRules()).thenReturn(databaseRules);
- return new SQLFederationEngine("foo_db", "foo_db", metaData, mock(), mock());
- }
-
- private SQLFederationEngine createSQLFederationEngine(final SQLFederationProcessor processor, final ShardingSphereMetaData metaData) {
- ShardingSphereStatistics statistics = mock(ShardingSphereStatistics.class);
- JDBCExecutor jdbcExecutor = mock(JDBCExecutor.class);
- try (MockedStatic<SQLFederationProcessorFactory> factoryMock = mockStatic(SQLFederationProcessorFactory.class)) {
- SQLFederationProcessorFactory factory = mock(SQLFederationProcessorFactory.class);
- when(factory.newInstance(statistics, jdbcExecutor)).thenReturn(processor);
- factoryMock.when(SQLFederationProcessorFactory::getInstance).thenReturn(factory);
- return new SQLFederationEngine("foo_db", "foo_schema", metaData, statistics, jdbcExecutor);
- }
- }
-
- private ShardingSphereMetaData createMetaData(final Properties props) {
- ShardingSphereTable table = new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
- return createMetaData(Collections.singleton(table), props);
- }
-
- private ShardingSphereMetaData createMetaData(final Collection<ShardingSphereTable> tables, final Properties props) {
- ShardingSphereSchema schema = new ShardingSphereSchema("foo_schema", databaseType, tables, Collections.emptyList());
- ShardingSphereDatabase database = new ShardingSphereDatabase(
- "foo_db", databaseType, new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), Collections.singleton(schema),
- new ConfigurationProperties(new Properties()));
- SQLFederationRuleConfiguration ruleConfig = new SQLFederationRuleConfiguration(true, false, cacheOption);
- Collection<ShardingSphereRule> globalRules = Collections.singleton(new SQLFederationRule(ruleConfig, Collections.singleton(database)));
- return new ShardingSphereMetaData(Collections.singleton(database), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(globalRules), new ConfigurationProperties(props));
- }
}
diff --git a/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/CalciteSQLFederationProviderTest.java b/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/CalciteSQLFederationProviderTest.java
index 77dc1fd789c..9abe3d9e41f 100644
--- a/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/CalciteSQLFederationProviderTest.java
+++ b/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/CalciteSQLFederationProviderTest.java
@@ -17,8 +17,12 @@
package org.apache.shardingsphere.sqlfederation.provider.calcite;
+import org.apache.shardingsphere.infra.executor.sql.process.ProcessEngine;
+import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
import org.apache.shardingsphere.infra.rule.scope.GlobalRule.GlobalRuleChangedType;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.table.CreateTableStatement;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.SelectStatement;
import org.apache.shardingsphere.sqlfederation.compiler.context.CompilerContext;
import org.apache.shardingsphere.sqlfederation.config.SQLFederationCacheOption;
import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
@@ -34,7 +38,10 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isA;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.sameInstance;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
class CalciteSQLFederationProviderTest {
@@ -43,6 +50,24 @@ class CalciteSQLFederationProviderTest {
assertThat(TypedSPILoader.getService(SQLFederationProvider.class, "CALCITE"), isA(CalciteSQLFederationProvider.class));
}
+ @Test
+ void assertIsSupportedSQLStatementWithSelect() {
+ SQLFederationProvider provider = TypedSPILoader.getService(SQLFederationProvider.class, "CALCITE");
+ assertTrue(provider.isSupportedSQLStatement(mock(SelectStatement.class)));
+ }
+
+ @Test
+ void assertIsSupportedSQLStatementWithNonSelect() {
+ SQLFederationProvider provider = TypedSPILoader.getService(SQLFederationProvider.class, "CALCITE");
+ assertFalse(provider.isSupportedSQLStatement(mock(CreateTableStatement.class)));
+ }
+
+ @Test
+ void assertCreatePreviewExecutor() {
+ SQLFederationProvider provider = TypedSPILoader.getService(SQLFederationProvider.class, "CALCITE");
+ assertThat(provider.createPreviewExecutor("foo_db", "foo_schema", mock(ShardingSphereStatistics.class), mock(ProcessEngine.class)), isA(CalciteSQLFederationExecutor.class));
+ }
+
@Test
void assertDefaultProviderAndSharedContextRefresh() {
SQLFederationRuleConfiguration config = new SQLFederationRuleConfiguration(true, false, new SQLFederationCacheOption(4, 64L));
diff --git a/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/fixture/decider/SQLFederationDeciderMatchFixture.java b/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/fixture/decider/SQLFederationDeciderMatchFixture.java
deleted file mode 100644
index 5662754e6c6..00000000000
--- a/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/fixture/decider/SQLFederationDeciderMatchFixture.java
+++ /dev/null
@@ -1,47 +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.provider.calcite.engine.fixture.decider;
-
-import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.datanode.DataNode;
-import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
-import org.apache.shardingsphere.sqlfederation.provider.calcite.engine.fixture.rule.SQLFederationDeciderRuleMatchFixture;
-import org.apache.shardingsphere.sqlfederation.spi.SQLFederationDecider;
-
-import java.util.Collection;
-import java.util.List;
-
-public final class SQLFederationDeciderMatchFixture implements SQLFederationDecider<SQLFederationDeciderRuleMatchFixture> {
-
- @Override
- public boolean decide(final SQLStatementContext sqlStatementContext, final List<Object> parameters, final RuleMetaData globalRuleMetaData,
- final ShardingSphereDatabase database, final SQLFederationDeciderRuleMatchFixture rule, final Collection<DataNode> includedDataNodes) {
- return true;
- }
-
- @Override
- public int getOrder() {
- return 1;
- }
-
- @Override
- public Class<SQLFederationDeciderRuleMatchFixture> getTypeClass() {
- return SQLFederationDeciderRuleMatchFixture.class;
- }
-}
diff --git a/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/fixture/decider/SQLFederationDeciderNotMatchFixture.java b/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/fixture/decider/SQLFederationDeciderNotMatchFixture.java
deleted file mode 100644
index 66df6151655..00000000000
--- a/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/fixture/decider/SQLFederationDeciderNotMatchFixture.java
+++ /dev/null
@@ -1,47 +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.provider.calcite.engine.fixture.decider;
-
-import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.datanode.DataNode;
-import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
-import org.apache.shardingsphere.sqlfederation.provider.calcite.engine.fixture.rule.SQLFederationDeciderRuleNotMatchFixture;
-import org.apache.shardingsphere.sqlfederation.spi.SQLFederationDecider;
-
-import java.util.Collection;
-import java.util.List;
-
-public final class SQLFederationDeciderNotMatchFixture implements SQLFederationDecider<SQLFederationDeciderRuleNotMatchFixture> {
-
- @Override
- public boolean decide(final SQLStatementContext sqlStatementContext, final List<Object> parameters, final RuleMetaData globalRuleMetaData,
- final ShardingSphereDatabase database, final SQLFederationDeciderRuleNotMatchFixture rule, final Collection<DataNode> includedDataNodes) {
- return false;
- }
-
- @Override
- public int getOrder() {
- return 11;
- }
-
- @Override
- public Class<SQLFederationDeciderRuleNotMatchFixture> getTypeClass() {
- return SQLFederationDeciderRuleNotMatchFixture.class;
- }
-}
diff --git a/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/fixture/rule/SQLFederationDeciderRuleMatchFixture.java b/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/fixture/rule/SQLFederationDeciderRuleMatchFixture.java
deleted file mode 100644
index c91ed5bfa36..00000000000
--- a/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/fixture/rule/SQLFederationDeciderRuleMatchFixture.java
+++ /dev/null
@@ -1,38 +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.provider.calcite.engine.fixture.rule;
-
-import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.sqlfederation.config.SQLFederationCacheOption;
-import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
-
-import static org.mockito.Mockito.mock;
-
-public final class SQLFederationDeciderRuleMatchFixture implements ShardingSphereRule {
-
- @Override
- public RuleConfiguration getConfiguration() {
- return new SQLFederationRuleConfiguration(true, false, mock(SQLFederationCacheOption.class));
- }
-
- @Override
- public int getOrder() {
- return 0;
- }
-}
diff --git a/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/fixture/rule/SQLFederationDeciderRuleNotMatchFixture.java b/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/fixture/rule/SQLFederationDeciderRuleNotMatchFixture.java
deleted file mode 100644
index ed1aa22eec0..00000000000
--- a/kernel/sql-federation/provider/calcite/src/test/java/org/apache/shardingsphere/sqlfederation/provider/calcite/engine/fixture/rule/SQLFederationDeciderRuleNotMatchFixture.java
+++ /dev/null
@@ -1,38 +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.provider.calcite.engine.fixture.rule;
-
-import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.sqlfederation.config.SQLFederationCacheOption;
-import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
-
-import static org.mockito.Mockito.mock;
-
-public final class SQLFederationDeciderRuleNotMatchFixture implements ShardingSphereRule {
-
- @Override
- public RuleConfiguration getConfiguration() {
- return new SQLFederationRuleConfiguration(false, false, mock(SQLFederationCacheOption.class));
- }
-
- @Override
- public int getOrder() {
- return 0;
- }
-}
diff --git a/kernel/sql-federation/provider/calcite/src/test/resources/META-INF/services/org.apache.shardingsphere.sqlfederation.spi.SQLFederationDecider b/kernel/sql-federation/provider/calcite/src/test/resources/META-INF/services/org.apache.shardingsphere.sqlfederation.spi.SQLFederationDecider
deleted file mode 100644
index a94819a0097..00000000000
--- a/kernel/sql-federation/provider/calcite/src/test/resources/META-INF/services/org.apache.shardingsphere.sqlfederation.spi.SQLFederationDecider
+++ /dev/null
@@ -1,19 +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.
-#
-
-org.apache.shardingsphere.sqlfederation.provider.calcite.engine.fixture.decider.SQLFederationDeciderMatchFixture
-org.apache.shardingsphere.sqlfederation.provider.calcite.engine.fixture.decider.SQLFederationDeciderNotMatchFixture
diff --git a/kernel/sql-federation/provider/none/src/main/java/org/apache/shardingsphere/sqlfederation/provider/none/NoneSQLFederationProvider.java b/kernel/sql-federation/provider/none/src/main/java/org/apache/shardingsphere/sqlfederation/provider/none/NoneSQLFederationProvider.java
index f8607e2badf..4c0bbf4cd91 100644
--- a/kernel/sql-federation/provider/none/src/main/java/org/apache/shardingsphere/sqlfederation/provider/none/NoneSQLFederationProvider.java
+++ b/kernel/sql-federation/provider/none/src/main/java/org/apache/shardingsphere/sqlfederation/provider/none/NoneSQLFederationProvider.java
@@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.J
import org.apache.shardingsphere.infra.executor.sql.process.ProcessEngine;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereStatistics;
+import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
import org.apache.shardingsphere.sqlfederation.config.SQLFederationRuleConfiguration;
import org.apache.shardingsphere.sqlfederation.spi.SQLFederationExecutor;
import org.apache.shardingsphere.sqlfederation.spi.SQLFederationProvider;
@@ -29,7 +30,7 @@ import org.apache.shardingsphere.sqlfederation.spi.SQLFederationProvider;
import java.util.Collection;
/**
- * Provider that explicitly rejects SQL federation execution.
+ * Provider that opts out of SQL federation and rejects direct federation execution.
*/
public final class NoneSQLFederationProvider implements SQLFederationProvider {
@@ -41,6 +42,12 @@ public final class NoneSQLFederationProvider implements SQLFederationProvider {
public void refresh(final Collection<ShardingSphereDatabase> databases) {
}
+ @HighFrequencyInvocation
+ @Override
+ public boolean isSupportedSQLStatement(final SQLStatement sqlStatement) {
+ return false;
+ }
+
@HighFrequencyInvocation
@Override
public SQLFederationExecutor createExecutor(final String currentDatabaseName, final String currentSchemaName, final ShardingSphereStatistics statistics,
diff --git a/kernel/sql-federation/provider/none/src/test/java/org/apache/shardingsphere/sqlfederation/provider/none/NoneSQLFederationProviderTest.java b/kernel/sql-federation/provider/none/src/test/java/org/apache/shardingsphere/sqlfederation/provider/none/NoneSQLFederationProviderTest.java
index e18d9698c36..eadb21f8c7b 100644
--- a/kernel/sql-federation/provider/none/src/test/java/org/apache/shardingsphere/sqlfederation/provider/none/NoneSQLFederationProviderTest.java
+++ b/kernel/sql-federation/provider/none/src/test/java/org/apache/shardingsphere/sqlfederation/provider/none/NoneSQLFederationProviderTest.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.sqlfederation.provider.none;
import org.apache.shardingsphere.infra.binder.context.statement.type.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
import org.apache.shardingsphere.infra.executor.sql.process.ProcessEngine;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
@@ -43,7 +44,6 @@ import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isA;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -55,6 +55,12 @@ class NoneSQLFederationProviderTest {
assertThat(TypedSPILoader.getService(SQLFederationProvider.class, "NONE"), isA(NoneSQLFederationProvider.class));
}
+ @Test
+ void assertIsSupportedSQLStatement() {
+ SQLFederationProvider provider = TypedSPILoader.getService(SQLFederationProvider.class, "NONE");
+ assertFalse(provider.isSupportedSQLStatement(mock(SelectStatement.class)));
+ }
+
@Test
void assertDefaultCalciteProviderMissingWhenOnlyNoneInstalled() {
SQLFederationRuleConfiguration config = new SQLFederationRuleConfiguration(true, false, new SQLFederationCacheOption(4, 64L));
@@ -82,7 +88,7 @@ class NoneSQLFederationProviderTest {
}
@Test
- void assertFederationExecutionFailsClearly() throws SQLException {
+ void assertAllQueryDoesNotUseFederation() throws SQLException {
SQLFederationRule rule = createRule(true);
RuleMetaData globalRuleMetaData = new RuleMetaData(Collections.singleton(rule));
ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
@@ -92,13 +98,19 @@ class NoneSQLFederationProviderTest {
QueryContext queryContext = mock(QueryContext.class);
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
try (SQLFederationEngine engine = new SQLFederationEngine("foo_db", "foo_schema", metaData, mock(ShardingSphereStatistics.class), null)) {
- assertTrue(engine.decide(queryContext, globalRuleMetaData));
- SQLFederationProviderUnsupportedException actual = assertThrows(SQLFederationProviderUnsupportedException.class,
- () -> engine.executeQuery(null, null, mock(SQLFederationContext.class)));
- assertThat(actual.getMessage(), is("SQL_FEDERATION-00003: SQL Federation provider 'NONE' does not support federation execution."));
+ assertFalse(engine.decide(queryContext, globalRuleMetaData));
}
}
+ @Test
+ void assertFederationExecutionFailsClearly() {
+ SQLFederationProvider provider = TypedSPILoader.getService(SQLFederationProvider.class, "NONE");
+ SQLFederationProviderUnsupportedException actual = assertThrows(SQLFederationProviderUnsupportedException.class,
+ () -> provider.createExecutor("foo_db", "foo_schema", mock(ShardingSphereStatistics.class), mock(JDBCExecutor.class), mock(ProcessEngine.class))
+ .executeQuery(mock(), mock(), mock(SQLFederationContext.class)));
+ assertThat(actual.getMessage(), is("SQL_FEDERATION-00003: SQL Federation provider 'NONE' does not support federation execution."));
+ }
+
@Test
void assertFederationPreviewFailsClearly() {
SQLFederationProvider provider = TypedSPILoader.getService(SQLFederationProvider.class, "NONE");