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 582b7c5d1a1 Add tests for MemoryTableStatisticsBuilder and
ExecutorBindContext (#37279)
582b7c5d1a1 is described below
commit 582b7c5d1a117346437c9b3a67ec64a1561374c5
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Dec 5 10:03:46 2025 +0800
Add tests for MemoryTableStatisticsBuilder and ExecutorBindContext (#37279)
---
.../executor/context/ExecutorBindContextTest.java | 72 +++++++++++++
.../memory/MemoryTableStatisticsBuilderTest.java | 118 +++++++++++++++++++++
2 files changed, 190 insertions(+)
diff --git
a/kernel/sql-federation/executor/src/test/java/org/apache/shardingsphere/sqlfederation/executor/context/ExecutorBindContextTest.java
b/kernel/sql-federation/executor/src/test/java/org/apache/shardingsphere/sqlfederation/executor/context/ExecutorBindContextTest.java
new file mode 100644
index 00000000000..33d224c0a85
--- /dev/null
+++
b/kernel/sql-federation/executor/src/test/java/org/apache/shardingsphere/sqlfederation/executor/context/ExecutorBindContextTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.executor.context;
+
+import org.apache.calcite.adapter.java.JavaTypeFactory;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.schema.SchemaPlus;
+import
org.apache.shardingsphere.sqlfederation.compiler.rel.converter.SQLFederationRelConverter;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class ExecutorBindContextTest {
+
+ @Test
+ void assertGetRootSchema() {
+ SchemaPlus expectedSchema = mock(SchemaPlus.class);
+ ExecutorBindContext context = new
ExecutorBindContext(createConverter(expectedSchema, null),
Collections.emptyMap());
+ assertThat(context.getRootSchema(), is(expectedSchema));
+ }
+
+ @Test
+ void assertGetTypeFactory() {
+ JavaTypeFactory typeFactory = mock(JavaTypeFactory.class,
RETURNS_DEEP_STUBS);
+ ExecutorBindContext context = new
ExecutorBindContext(createConverter(null, typeFactory), Collections.emptyMap());
+ assertThat(context.getTypeFactory(), is(typeFactory));
+ }
+
+ @Test
+ void assertGetQueryProvider() {
+ ExecutorBindContext context = new
ExecutorBindContext(createConverter(null, null), Collections.emptyMap());
+ assertNull(context.getQueryProvider());
+ }
+
+ @Test
+ void assertGet() {
+ ExecutorBindContext context = new
ExecutorBindContext(createConverter(null, null),
Collections.singletonMap("foo_param", 10));
+ assertThat(context.get("foo_param"), is(10));
+ assertThat(context.getParameters(),
is(Collections.singletonMap("foo_param", 10)));
+ }
+
+ private SQLFederationRelConverter createConverter(final SchemaPlus
schemaPlus, final JavaTypeFactory typeFactory) {
+ SQLFederationRelConverter result =
mock(SQLFederationRelConverter.class);
+ when(result.getSchemaPlus()).thenReturn(schemaPlus);
+ RelOptCluster cluster = mock(RelOptCluster.class);
+ when(result.getCluster()).thenReturn(cluster);
+ when(cluster.getTypeFactory()).thenReturn(typeFactory);
+ return result;
+ }
+}
diff --git
a/kernel/sql-federation/executor/src/test/java/org/apache/shardingsphere/sqlfederation/executor/enumerable/enumerator/memory/MemoryTableStatisticsBuilderTest.java
b/kernel/sql-federation/executor/src/test/java/org/apache/shardingsphere/sqlfederation/executor/enumerable/enumerator/memory/MemoryTableStatisticsBuilderTest.java
new file mode 100644
index 00000000000..351c33c2e0a
--- /dev/null
+++
b/kernel/sql-federation/executor/src/test/java/org/apache/shardingsphere/sqlfederation/executor/enumerable/enumerator/memory/MemoryTableStatisticsBuilderTest.java
@@ -0,0 +1,118 @@
+/*
+ * 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.executor.enumerable.enumerator.memory;
+
+import org.apache.shardingsphere.authority.rule.AuthorityRule;
+import
org.apache.shardingsphere.database.connector.core.metadata.database.metadata.option.table.DialectDriverQuerySystemCatalogOption;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
+import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
+import org.apache.shardingsphere.infra.metadata.statistics.TableStatistics;
+import org.apache.shardingsphere.infra.metadata.user.Grantee;
+import org.junit.jupiter.api.Test;
+import
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.empty;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class MemoryTableStatisticsBuilderTest {
+
+ @Test
+ void assertBuildDatabaseData() {
+ DialectDriverQuerySystemCatalogOption option =
mock(DialectDriverQuerySystemCatalogOption.class);
+ when(option.isDatabaseDataTable("pg_database")).thenReturn(true);
+ when(option.getDatCompatibility()).thenReturn("PG");
+ ShardingSphereMetaData metaData = createMetaData(
+ Arrays.asList(mockDatabase("foo_db", Collections.emptyList()),
mockDatabase("bar_db", Collections.emptyList())), new
RuleMetaData(Collections.emptyList()));
+ TableStatistics actual =
MemoryTableStatisticsBuilder.buildTableStatistics(mockTable("pg_database"),
metaData, option);
+ assertThat(actual.getRows().size(), is(2));
+ List<Object[]> rows = actual.getRows().stream().map(each ->
each.getRows().toArray(new Object[0])).collect(Collectors.toList());
+ Collection<Object> databaseNames = rows.stream().map(each ->
each[0]).collect(Collectors.toList());
+ assertThat(databaseNames, containsInAnyOrder("foo_db", "bar_db"));
+ rows.forEach(each -> assertThat(each[11], is("PG")));
+ }
+
+ @Test
+ void assertBuildTableData() {
+ DialectDriverQuerySystemCatalogOption option =
mock(DialectDriverQuerySystemCatalogOption.class);
+ when(option.isTableDataTable("pg_tables")).thenReturn(true);
+ ShardingSphereSchema schema1 = new ShardingSphereSchema("public",
Collections.singleton(mockTable("t_order")), Collections.emptyList());
+ ShardingSphereSchema schema2 = new ShardingSphereSchema("logic",
Collections.singleton(mockTable("t_user")), Collections.emptyList());
+ ShardingSphereDatabase database = mockDatabase("foo_db",
Arrays.asList(schema1, schema2));
+ ShardingSphereMetaData metaData =
createMetaData(Collections.singleton(database), new
RuleMetaData(Collections.emptyList()));
+ TableStatistics actual =
MemoryTableStatisticsBuilder.buildTableStatistics(mockTable("pg_tables"),
metaData, option);
+ assertThat(actual.getRows().size(), is(2));
+ List<Object[]> rows = actual.getRows().stream().map(each ->
each.getRows().toArray(new Object[0])).collect(Collectors.toList());
+ Collection<String> schemaNames = rows.stream().map(each -> (String)
each[0]).collect(Collectors.toList());
+ Collection<String> tableNames = rows.stream().map(each -> (String)
each[1]).collect(Collectors.toList());
+ assertThat(schemaNames, containsInAnyOrder("public", "logic"));
+ assertThat(tableNames, containsInAnyOrder("t_order", "t_user"));
+ }
+
+ @Test
+ void assertBuildRoleData() {
+ DialectDriverQuerySystemCatalogOption option =
mock(DialectDriverQuerySystemCatalogOption.class);
+ when(option.isRoleDataTable("pg_roles")).thenReturn(true);
+ AuthorityRule authorityRule = mock(AuthorityRule.class,
org.mockito.Mockito.RETURNS_DEEP_STUBS);
+ when(authorityRule.getGrantees()).thenReturn(new
LinkedHashSet<>(Arrays.asList(new Grantee("alice", ""), new Grantee("bob",
""))));
+ ShardingSphereMetaData metaData =
createMetaData(Collections.emptyList(), new
RuleMetaData(Collections.singleton(authorityRule)));
+ TableStatistics actual =
MemoryTableStatisticsBuilder.buildTableStatistics(mockTable("pg_roles"),
metaData, option);
+ assertThat(actual.getRows().size(), is(2));
+ List<Object[]> rows = actual.getRows().stream().map(each ->
each.getRows().toArray(new Object[0])).collect(Collectors.toList());
+ Collection<String> usernames = rows.stream().map(each -> (String)
each[0]).collect(Collectors.toList());
+ assertThat(usernames, containsInAnyOrder("alice", "bob"));
+ }
+
+ @Test
+ void assertBuildDefaultWhenNoMatch() {
+ DialectDriverQuerySystemCatalogOption option =
mock(DialectDriverQuerySystemCatalogOption.class);
+ TableStatistics actual =
MemoryTableStatisticsBuilder.buildTableStatistics(mockTable("other"),
createMetaData(Collections.emptyList(), new
RuleMetaData(Collections.emptyList())), option);
+ assertThat(actual.getRows(), empty());
+ assertThat(actual.getName(), is("other"));
+ }
+
+ private ShardingSphereTable mockTable(final String name) {
+ return new ShardingSphereTable(name, Collections.emptyList(),
Collections.emptyList(), Collections.emptyList());
+ }
+
+ private ShardingSphereDatabase mockDatabase(final String name, final
Collection<ShardingSphereSchema> schemas) {
+ ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
+ when(result.getName()).thenReturn(name);
+ when(result.getAllSchemas()).thenReturn(schemas);
+ return result;
+ }
+
+ private ShardingSphereMetaData createMetaData(final
Collection<ShardingSphereDatabase> databases, final RuleMetaData ruleMetaData) {
+ return new ShardingSphereMetaData(databases, new
ResourceMetaData(Collections.emptyMap()), ruleMetaData, new
ConfigurationProperties(new Properties()));
+ }
+}