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 0c4c7199046 Add SQLStatementCompilerEngineFactoryTest (#37325)
0c4c7199046 is described below

commit 0c4c7199046f29bb4262a986bf09a0ca8fa82926
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Dec 10 15:05:57 2025 +0800

    Add SQLStatementCompilerEngineFactoryTest (#37325)
    
    * Add SQLStatementCompilerEngineFactoryTest
    
    * Add CalciteSchemaBuilderTest
---
 .../context/schema/CalciteSchemaBuilder.java       |  3 +-
 .../SQLStatementCompilerEngineFactoryTest.java     | 65 ++++++++++++++
 .../compiler/context/CalciteSchemaBuilderTest.java | 98 ++++++++++++++++++++++
 3 files changed, 164 insertions(+), 2 deletions(-)

diff --git 
a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/context/schema/CalciteSchemaBuilder.java
 
b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/context/schema/CalciteSchemaBuilder.java
index 83ebc1884d6..3f0d9678005 100644
--- 
a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/context/schema/CalciteSchemaBuilder.java
+++ 
b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/context/schema/CalciteSchemaBuilder.java
@@ -95,8 +95,7 @@ public final class CalciteSchemaBuilder {
     
     private static void registerNestedSchemaFunction(final CalciteSchema 
calciteSchema, final DatabaseType databaseType) {
         for (CalciteSchema each : calciteSchema.getSubSchemaMap().values()) {
-            Collection<CalciteSchema> subSchemas = 
each.subSchemas().getNames(LikePattern.any()).stream().map(schemaName -> 
each.subSchemas().get(schemaName)).collect(Collectors.toList());
-            registerFunction(subSchemas, databaseType);
+            
registerFunction(each.subSchemas().getNames(LikePattern.any()).stream().map(schemaName
 -> each.subSchemas().get(schemaName)).collect(Collectors.toList()), 
databaseType);
         }
     }
 }
diff --git 
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/compiler/SQLStatementCompilerEngineFactoryTest.java
 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/compiler/SQLStatementCompilerEngineFactoryTest.java
new file mode 100644
index 00000000000..e61e6a4167c
--- /dev/null
+++ 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/compiler/SQLStatementCompilerEngineFactoryTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.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.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.Optional;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.not;
+import static org.hamcrest.Matchers.sameInstance;
+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 SQLStatementCompilerEngineFactoryTest {
+    
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    @Test
+    void assertGetSQLStatementCompilerEngineCoversAllBranches() {
+        LoadingCache<ExecutionPlanCacheKey, SQLFederationExecutionPlan> 
cacheWithEviction = mock(LoadingCache.class);
+        Policy cachePolicyWithEviction = mock(Policy.class);
+        Eviction eviction = mock(Eviction.class);
+        
when(cachePolicyWithEviction.eviction()).thenReturn(Optional.of(eviction));
+        when(cacheWithEviction.policy()).thenReturn(cachePolicyWithEviction);
+        
when(ExecutionPlanCacheBuilder.build(any(SQLFederationCacheOption.class))).thenReturn(cacheWithEviction,
 mock(LoadingCache.class));
+        SQLStatementCompilerEngine actualCreatedEngine = 
SQLStatementCompilerEngineFactory.getSQLStatementCompilerEngine("factory_db", 
"factory_schema", new SQLFederationCacheOption(1, 1L));
+        SQLStatementCompilerEngine actualUpdatedEngine = 
SQLStatementCompilerEngineFactory.getSQLStatementCompilerEngine("factory_db", 
"factory_schema", new SQLFederationCacheOption(1, 2L));
+        SQLStatementCompilerEngine actualReplacedEngine = 
SQLStatementCompilerEngineFactory.getSQLStatementCompilerEngine("factory_db", 
"factory_schema", new SQLFederationCacheOption(2, 2L));
+        SQLStatementCompilerEngine actualUnchangedEngine = 
SQLStatementCompilerEngineFactory.getSQLStatementCompilerEngine("factory_db", 
"factory_schema", new SQLFederationCacheOption(2, 2L));
+        assertThat(actualUpdatedEngine, is(actualCreatedEngine));
+        assertThat(actualReplacedEngine, 
is(not(sameInstance(actualCreatedEngine))));
+        assertThat(actualUnchangedEngine, is(actualReplacedEngine));
+        verify(eviction).setMaximum(2L);
+    }
+}
diff --git 
a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/context/CalciteSchemaBuilderTest.java
 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/context/CalciteSchemaBuilderTest.java
new file mode 100644
index 00000000000..bb3b7b65bd2
--- /dev/null
+++ 
b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/context/CalciteSchemaBuilderTest.java
@@ -0,0 +1,98 @@
+/*
+ * 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.context;
+
+import org.apache.calcite.jdbc.CalciteSchema;
+import org.apache.calcite.schema.SchemaPlus;
+import 
org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import 
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import 
org.apache.shardingsphere.sqlfederation.compiler.context.schema.CalciteSchemaBuilder;
+import 
org.apache.shardingsphere.sqlfederation.compiler.sql.function.DialectSQLFederationFunctionRegister;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.MockedStatic;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Optional;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasItem;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.verify;
+
+class CalciteSchemaBuilderTest {
+    
+    @Test
+    void assertBuildWithoutSchemas() {
+        DatabaseType databaseType = 
TypedSPILoader.getService(DatabaseType.class, "MySQL");
+        ShardingSphereDatabase databaseWithoutSchemas = new 
ShardingSphereDatabase(
+                "empty_db", databaseType, mock(ResourceMetaData.class), new 
RuleMetaData(Collections.emptyList()), Collections.emptyList());
+        CalciteSchemaBuilder.build(Collections.emptyList());
+        CalciteSchema actual = 
CalciteSchemaBuilder.build(Collections.singleton(databaseWithoutSchemas));
+        assertFalse(actual.getSubSchemaMap().containsKey("empty_db"));
+    }
+    
+    @Test
+    void assertBuildWithDefaultSchemaRegistersNestedFunctions() {
+        DatabaseType databaseType = 
TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
+        Collection<ShardingSphereSchema> schemas = Arrays.asList(new 
ShardingSphereSchema("public"), new ShardingSphereSchema("other"));
+        ShardingSphereDatabase database = new ShardingSphereDatabase("pg_db", 
databaseType, mock(ResourceMetaData.class), new 
RuleMetaData(Collections.emptyList()), schemas);
+        DialectSQLFederationFunctionRegister functionRegister = 
mock(DialectSQLFederationFunctionRegister.class);
+        try (MockedStatic<DatabaseTypedSPILoader> databaseTypedSPILoader = 
mockStatic(DatabaseTypedSPILoader.class, CALLS_REAL_METHODS)) {
+            databaseTypedSPILoader.when(() -> 
DatabaseTypedSPILoader.findService(DialectSQLFederationFunctionRegister.class, 
databaseType))
+                    
.thenReturn(Optional.of(functionRegister)).thenReturn(Optional.empty());
+            CalciteSchema actual = 
CalciteSchemaBuilder.build(Collections.singletonList(database));
+            assertTrue(actual.getSubSchemaMap().containsKey("pg_db"));
+            CalciteSchema databaseSchema = actual.getSubSchema("pg_db", true);
+            assertNotNull(databaseSchema);
+            
assertTrue(databaseSchema.getSubSchemaMap().keySet().containsAll(Arrays.asList("public",
 "other")));
+            ArgumentCaptor<String> schemaNameCaptor = 
ArgumentCaptor.forClass(String.class);
+            verify(functionRegister).registerFunction(any(SchemaPlus.class), 
schemaNameCaptor.capture());
+            assertThat(Arrays.asList("public", "other"), 
hasItem(schemaNameCaptor.getValue()));
+        }
+    }
+    
+    @Test
+    void assertBuildWithoutDefaultSchemaRegistersFunctions() {
+        DatabaseType databaseType = 
TypedSPILoader.getService(DatabaseType.class, "MySQL");
+        ShardingSphereDatabase database = new 
ShardingSphereDatabase("mysql_db", databaseType, mock(ResourceMetaData.class),
+                new RuleMetaData(Collections.emptyList()), 
Collections.singletonList(new ShardingSphereSchema("foo_schema")));
+        try (MockedStatic<DatabaseTypedSPILoader> databaseTypedSPILoader = 
mockStatic(DatabaseTypedSPILoader.class, CALLS_REAL_METHODS)) {
+            DialectSQLFederationFunctionRegister functionRegister = 
mock(DialectSQLFederationFunctionRegister.class);
+            databaseTypedSPILoader.when(() -> 
DatabaseTypedSPILoader.findService(DialectSQLFederationFunctionRegister.class, 
databaseType))
+                    
.thenReturn(Optional.empty()).thenReturn(Optional.of(functionRegister));
+            CalciteSchema actual = 
CalciteSchemaBuilder.build(Collections.singletonList(database));
+            assertTrue(actual.getSubSchemaMap().containsKey("mysql_db"));
+            verify(functionRegister).registerFunction(any(SchemaPlus.class), 
eq("mysql_db"));
+        }
+    }
+}

Reply via email to