This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 c5d428f56cc Add SQLFederationCompilerEngineTest (#37324)
c5d428f56cc is described below
commit c5d428f56cc8e955ad90909b09f6edce72e2694d
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Dec 10 13:22:59 2025 +0800
Add SQLFederationCompilerEngineTest (#37324)
* Remove MySQLSQLFederationConnectionConfigBuilder.isDefault()
* Add SQLFederationCompilerEngineTest
* Add SQLStatementCompilerEngineTest
---
kernel/sql-federation/compiler/pom.xml | 7 ++
.../compiler/compiler/SQLStatementCompiler.java | 10 +-
.../compiler/SQLFederationCompilerEngineTest.java | 59 +++++++++++
.../compiler/SQLStatementCompilerEngineTest.java | 108 +++++++++++++++++++++
4 files changed, 177 insertions(+), 7 deletions(-)
diff --git a/kernel/sql-federation/compiler/pom.xml
b/kernel/sql-federation/compiler/pom.xml
index 538ece50d03..0230c47c161 100644
--- a/kernel/sql-federation/compiler/pom.xml
+++ b/kernel/sql-federation/compiler/pom.xml
@@ -80,6 +80,13 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-test-infra-framework</artifactId>
+ <version>${project.version}</version>
+ <scope>test</scope>
+ </dependency>
+
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
diff --git
a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/compiler/SQLStatementCompiler.java
b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/compiler/SQLStatementCompiler.java
index d9f2ea77860..52be6860f53 100644
---
a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/compiler/SQLStatementCompiler.java
+++
b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/compiler/SQLStatementCompiler.java
@@ -43,11 +43,11 @@ public final class SQLStatementCompiler {
private final Convention convention;
/**
- * Compile sql statement to execution plan.
+ * Compile SQL statement to execution plan.
*
* @param sqlStatement SQL statement
* @param databaseType database type
- * @return sql federation execution plan
+ * @return SQL federation execution plan
*/
public SQLFederationExecutionPlan compile(final SQLStatement sqlStatement,
final String databaseType) {
RelMetadataQueryBase.THREAD_PROVIDERS.set(JaninoRelMetadataProvider.DEFAULT);
@@ -70,11 +70,7 @@ public final class SQLStatementCompiler {
private RelNode optimize(final RelNode logicalPlan, final
SQLFederationRelConverter converter, final String databaseType) {
RelNode rewrittenPlan = rewriteTableScan(logicalPlan, databaseType);
RelOptPlanner planner = converter.getCluster().getPlanner();
- if (rewrittenPlan.getTraitSet().contains(convention)) {
- planner.setRoot(rewrittenPlan);
- } else {
- planner.setRoot(planner.changeTraits(rewrittenPlan,
converter.getCluster().traitSet().replace(convention)));
- }
+ planner.setRoot(rewrittenPlan.getTraitSet().contains(convention) ?
rewrittenPlan : planner.changeTraits(rewrittenPlan,
converter.getCluster().traitSet().replace(convention)));
return planner.findBestExp();
}
diff --git
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/SQLFederationCompilerEngineTest.java
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/SQLFederationCompilerEngineTest.java
new file mode 100644
index 00000000000..06c26604670
--- /dev/null
+++
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/SQLFederationCompilerEngineTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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.compiler;
+
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
+import
org.apache.shardingsphere.sqlfederation.compiler.compiler.SQLStatementCompiler;
+import
org.apache.shardingsphere.sqlfederation.compiler.compiler.SQLStatementCompilerEngine;
+import
org.apache.shardingsphere.sqlfederation.compiler.compiler.SQLStatementCompilerEngineFactory;
+import
org.apache.shardingsphere.sqlfederation.compiler.planner.cache.ExecutionPlanCacheKey;
+import org.apache.shardingsphere.sqlfederation.config.SQLFederationCacheOption;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(SQLStatementCompilerEngineFactory.class)
+class SQLFederationCompilerEngineTest {
+
+ @Test
+ void assertCompileDelegatesForCacheFlags() {
+ SQLFederationCacheOption cacheOption = new SQLFederationCacheOption(1,
1L);
+ ExecutionPlanCacheKey cacheKey = new ExecutionPlanCacheKey("select 1",
mock(SQLStatement.class), mock(SQLStatementCompiler.class));
+ SQLStatementCompilerEngine statementCompilerEngine =
mock(SQLStatementCompilerEngine.class);
+ SQLFederationExecutionPlan expectedPlanWithCache =
mock(SQLFederationExecutionPlan.class);
+ SQLFederationExecutionPlan expectedPlanWithoutCache =
mock(SQLFederationExecutionPlan.class);
+
when(SQLStatementCompilerEngineFactory.getSQLStatementCompilerEngine("foo_db",
"foo_schema", cacheOption)).thenReturn(statementCompilerEngine);
+ when(statementCompilerEngine.compile(cacheKey,
true)).thenReturn(expectedPlanWithCache);
+ when(statementCompilerEngine.compile(cacheKey,
false)).thenReturn(expectedPlanWithoutCache);
+ SQLFederationCompilerEngine compilerEngine = new
SQLFederationCompilerEngine("foo_db", "foo_schema", cacheOption);
+ SQLFederationExecutionPlan actualPlanWithCache =
compilerEngine.compile(cacheKey, true);
+ SQLFederationExecutionPlan actualPlanWithoutCache =
compilerEngine.compile(cacheKey, false);
+ assertThat(actualPlanWithCache, is(expectedPlanWithCache));
+ assertThat(actualPlanWithoutCache, is(expectedPlanWithoutCache));
+ verify(statementCompilerEngine).compile(cacheKey, true);
+ verify(statementCompilerEngine).compile(cacheKey, false);
+ }
+}
diff --git
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/compiler/SQLStatementCompilerEngineTest.java
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/compiler/SQLStatementCompilerEngineTest.java
new file mode 100644
index 00000000000..839f6c2a189
--- /dev/null
+++
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/compiler/SQLStatementCompilerEngineTest.java
@@ -0,0 +1,108 @@
+/*
+ * 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.compiler.compiler;
+
+import com.github.benmanes.caffeine.cache.LoadingCache;
+import com.github.benmanes.caffeine.cache.Policy;
+import com.github.benmanes.caffeine.cache.Policy.Eviction;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
+import
org.apache.shardingsphere.sqlfederation.compiler.SQLFederationExecutionPlan;
+import
org.apache.shardingsphere.sqlfederation.compiler.planner.cache.ExecutionPlanCacheBuilder;
+import
org.apache.shardingsphere.sqlfederation.compiler.planner.cache.ExecutionPlanCacheKey;
+import org.apache.shardingsphere.sqlfederation.config.SQLFederationCacheOption;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.StaticMockSettings;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+
+import java.util.Optional;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(ExecutionPlanCacheBuilder.class)
+class SQLStatementCompilerEngineTest {
+
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+
+ @Mock
+ private SQLStatementCompiler sqlStatementCompiler;
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private SQLStatement sqlStatement;
+
+ private ExecutionPlanCacheKey cacheKey;
+
+ @BeforeEach
+ void setUp() {
+ when(sqlStatement.getDatabaseType()).thenReturn(databaseType);
+ cacheKey = new ExecutionPlanCacheKey("select 1", sqlStatement,
sqlStatementCompiler);
+ }
+
+ @Test
+ void assertCompileWithoutCache() {
+ SQLFederationExecutionPlan expectedPlan =
mock(SQLFederationExecutionPlan.class);
+ when(sqlStatementCompiler.compile(sqlStatement,
"FIXTURE")).thenReturn(expectedPlan);
+ SQLStatementCompilerEngine engine = new SQLStatementCompilerEngine(new
SQLFederationCacheOption(1, 1L));
+ assertThat(engine.compile(cacheKey, false), is(expectedPlan));
+ verify(sqlStatementCompiler).compile(sqlStatement, "FIXTURE");
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ void assertCompileWithCache() {
+ LoadingCache<ExecutionPlanCacheKey, SQLFederationExecutionPlan> cache
= mock(LoadingCache.class);
+ SQLFederationExecutionPlan expectedCachedPlan =
mock(SQLFederationExecutionPlan.class);
+ when(cache.get(cacheKey)).thenReturn(null, expectedCachedPlan,
expectedCachedPlan, expectedCachedPlan);
+
when(ExecutionPlanCacheBuilder.build(any(SQLFederationCacheOption.class))).thenReturn(cache);
+ SQLStatementCompilerEngine engine = new SQLStatementCompilerEngine(new
SQLFederationCacheOption(1, 1L));
+ assertThat(engine.compile(cacheKey, true), is(expectedCachedPlan));
+ assertThat(engine.compile(cacheKey, true), is(expectedCachedPlan));
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ @Test
+ void assertUpdateCacheOption() {
+ LoadingCache<ExecutionPlanCacheKey, SQLFederationExecutionPlan>
cacheWithEviction = mock(LoadingCache.class);
+ Policy cachePolicyWithEviction = mock(Policy.class);
+ Policy.Eviction eviction = mock(Eviction.class);
+
when(cachePolicyWithEviction.eviction()).thenReturn(Optional.of(eviction));
+ when(cacheWithEviction.policy()).thenReturn(cachePolicyWithEviction);
+ LoadingCache<ExecutionPlanCacheKey, SQLFederationExecutionPlan>
cacheWithoutEviction = mock(LoadingCache.class);
+ Policy cachePolicyWithoutEviction = mock(Policy.class);
+
when(cachePolicyWithoutEviction.eviction()).thenReturn(Optional.empty());
+
when(cacheWithoutEviction.policy()).thenReturn(cachePolicyWithoutEviction);
+
when(ExecutionPlanCacheBuilder.build(any(SQLFederationCacheOption.class))).thenReturn(cacheWithEviction,
cacheWithoutEviction);
+ SQLStatementCompilerEngine engineWithEviction = new
SQLStatementCompilerEngine(new SQLFederationCacheOption(1, 1L));
+ engineWithEviction.updateCacheOption(new SQLFederationCacheOption(2,
2L));
+ SQLStatementCompilerEngine engineWithoutEviction = new
SQLStatementCompilerEngine(new SQLFederationCacheOption(1, 1L));
+ engineWithoutEviction.updateCacheOption(new
SQLFederationCacheOption(3, 3L));
+ verify(eviction).setMaximum(2L);
+ verify(cachePolicyWithoutEviction).eviction();
+ }
+}