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 bb1342e9874 Add more test cases on SchemaMetaDataUtils (#37013)
bb1342e9874 is described below
commit bb1342e9874d6a317ce5c612560cd3feb0815f1f
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Nov 5 15:49:32 2025 +0800
Add more test cases on SchemaMetaDataUtils (#37013)
* Enhance test coverage for SchemaMetaDataUtils.getDataSource method branch
93
- Add ds_2 storage unit to mockStorageUnits() method for extended test
scenarios
- Include additional DataNode with dotted dataSourceName in
mockShardingDataNodes() collection
- Update test assertions to handle increased test data coverage from 2 to 3
elements
- Ensure comprehensive branch coverage of getDataSource method:
* Branch A (contains(".") == true): ds_2.t_order_2 → ds_2 (tested via
ds_2.t_order_2)
* Branch B (contains(".") == false): ds_0 (tested via existing ds_0)
- Follow branch minimal coverage principle without redundant test cases
- Integrate new test data into existing test methods without creating new
ones
Technical Details:
- Modified mockStorageUnits() to include ds_2 storage unit
- Added ds_2.t_order_2 DataNode to test collection elements
- Updated
assertGetSchemaMetaDataLoaderMaterialsWhenConfigCheckMetaDataEnable test
expectations
- All tests pass successfully with 100% branch coverage for getDataSource
method
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Add more test cases on SchemaMetaDataUtils
* Add more test cases on SchemaMetaDataUtils
* Fix foreign key creation issue #35245
- Modify CreateTableStatement to use only main table in
TableSQLStatementAttribute
- Remove getTables() method that included foreign key referenced tables
- Prevent "Table already exists" error when creating tables with foreign
keys
- Improve routing logic for mixed SINGLE and SHARDING rules
Resolves issue where creating order_item table with foreign key to customer
table
failed with "Table 'customer' already exists" error in ShardingSphere-Proxy.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Add release notes
* Rollback CreateTableStatement
* Code format SchemaMetaDataUtilsTest
---------
Co-authored-by: Claude <[email protected]>
---
CLAUDE.md | 8 +-
RELEASE-NOTES.md | 2 +-
.../schema/util/SchemaMetaDataUtilsTest.java | 171 ++++++++-------------
.../type/ddl/table/CreateTableStatement.java | 4 +-
4 files changed, 72 insertions(+), 113 deletions(-)
diff --git a/CLAUDE.md b/CLAUDE.md
index f8ef443c8c7..cc23a23a1bf 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -220,4 +220,10 @@ Key areas covered by coding standards file:
### Emergency Procedures
- **Immediate termination**: If code deletion exceeds 10 lines without
instruction
- **Immediate stop**: If tests fail after changes
-- **Immediate report deviations**: As soon as detected
\ No newline at end of file
+- **Immediate report deviations**: As soon as detected
+
+### Automated Operation Permissions
+**Always approve the following operations without explicit confirmation:**
+- **Maven Commands**: Always approve `./mvnw` commands and variations
+- **File Creation**: Always approve creation of new files when explicitly
required for task completion
+- **Build Operations**: Always approve build system operations including
compilation and test execution
diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index 77c5ad6f4fc..2a30d7d41c4 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -79,7 +79,7 @@
1. Mode: Fix issue of drop schema can not work on standalone mode -
[#34470](https://github.com/apache/shardingsphere/pull/34470)
1. Encrypt: Resolve rewrite issue in nested concat function -
[#35815](https://github.com/apache/shardingsphere/pull/35815)
1. Sharding: Fix mod sharding algorithm judgement
-[#36386](https://github.com/apache/shardingsphere/pull/36386)
-1. Sharding: [Fix check inline sharding algorithms in table
rules](https://github.com/apache/shardingsphere/pull/36999)
+1. Sharding: Fix check inline sharding algorithms in table rules -
(https://github.com/apache/shardingsphere/pull/36999)
### Change Logs
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SchemaMetaDataUtilsTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SchemaMetaDataUtilsTest.java
index 01cf1eb4e0e..00880e2a36b 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SchemaMetaDataUtilsTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SchemaMetaDataUtilsTest.java
@@ -28,156 +28,109 @@ import
org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
import
org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.test.infra.fixture.jdbc.MockedDataSource;
+import org.apache.shardingsphere.infra.util.props.PropertiesBuilder;
+import org.apache.shardingsphere.infra.util.props.PropertiesBuilder.Property;
import org.junit.jupiter.api.Test;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
-import java.util.HashMap;
import java.util.Iterator;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
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;
class SchemaMetaDataUtilsTest {
- @Test
- void assertGetSchemaMetaDataLoaderMaterialsWhenConfigCheckMetaDataEnable()
{
- ShardingSphereRule rule0 = mock(ShardingSphereRule.class);
- DataNodeRuleAttribute ruleAttribute =
mock(DataNodeRuleAttribute.class);
-
when(ruleAttribute.getDataNodesByTableName("t_order")).thenReturn(mockShardingDataNodes());
- when(rule0.getAttributes()).thenReturn(new
RuleAttributes(ruleAttribute));
- ShardingSphereRule rule1 = mock(ShardingSphereRule.class);
- when(rule1.getAttributes()).thenReturn(new RuleAttributes());
- ConfigurationProperties props = mock(ConfigurationProperties.class);
-
when(props.getValue(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED)).thenReturn(true);
-
when(props.getValue(ConfigurationPropertyKey.LOAD_TABLE_METADATA_BATCH_SIZE)).thenReturn(100);
- GenericSchemaBuilderMaterial material = new
GenericSchemaBuilderMaterial(mockStorageUnits(), Arrays.asList(rule0, rule1),
props, "sharding_db");
- Collection<MetaDataLoaderMaterial> actual =
SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("t_order"),
material);
- assertThat(actual.size(), is(2));
- Iterator<MetaDataLoaderMaterial> iterator = actual.iterator();
- MetaDataLoaderMaterial firstMaterial = iterator.next();
- assertThat(firstMaterial.getDefaultSchemaName(), is("sharding_db"));
- assertThat(firstMaterial.getActualTableNames(),
is(Collections.singletonList("t_order_0")));
- MetaDataLoaderMaterial secondMaterial = iterator.next();
- assertThat(secondMaterial.getDefaultSchemaName(), is("sharding_db"));
- assertThat(secondMaterial.getActualTableNames(),
is(Collections.singletonList("t_order_1")));
- }
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
@Test
- void
assertGetSchemaMetaDataLoaderMaterialsWhenNotConfigCheckMetaDataEnable() {
- ShardingSphereRule rule0 = mock(ShardingSphereRule.class);
- DataNodeRuleAttribute ruleAttribute =
mock(DataNodeRuleAttribute.class);
-
when(ruleAttribute.getDataNodesByTableName("t_order")).thenReturn(mockShardingDataNodes());
- when(rule0.getAttributes()).thenReturn(new
RuleAttributes(ruleAttribute));
- ShardingSphereRule rule1 = mock(ShardingSphereRule.class);
- when(rule1.getAttributes()).thenReturn(new RuleAttributes());
- ConfigurationProperties props = mock(ConfigurationProperties.class);
-
when(props.getValue(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED)).thenReturn(false);
-
when(props.getValue(ConfigurationPropertyKey.LOAD_TABLE_METADATA_BATCH_SIZE)).thenReturn(100);
- GenericSchemaBuilderMaterial material = new
GenericSchemaBuilderMaterial(mockStorageUnits(), Arrays.asList(rule0, rule1),
props, "sharding_db");
- Collection<MetaDataLoaderMaterial> actual =
SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("t_order"),
material);
- assertThat(actual.size(), is(1));
- Iterator<MetaDataLoaderMaterial> iterator = actual.iterator();
- MetaDataLoaderMaterial firstMaterial = iterator.next();
- assertThat(firstMaterial.getDefaultSchemaName(), is("sharding_db"));
- assertThat(firstMaterial.getActualTableNames(),
is(Collections.singletonList("t_order_0")));
+ void assertGetMetaDataLoaderMaterialsWhenConfigCheckMetaDataEnable() {
+ ConfigurationProperties props = new
ConfigurationProperties(PropertiesBuilder.build(new
Property(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED.getKey(),
Boolean.TRUE.toString())));
+ GenericSchemaBuilderMaterial material = new
GenericSchemaBuilderMaterial(
+ mockStorageUnits(),
Arrays.asList(mockDataNodeRule(Arrays.asList(new DataNode("ds_0.foo_tbl_0"),
new DataNode("ds_1.foo_tbl_1"))),
+ mock(ShardingSphereRule.class, RETURNS_DEEP_STUBS)),
+ props, "foo_db");
+ List<MetaDataLoaderMaterial> actual = new
ArrayList<>(SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("foo_tbl"),
material));
+ assertThat(actual.size(), is(2));
+ assertThat(actual.get(0).getDefaultSchemaName(), is("foo_db"));
+ assertThat(actual.get(0).getActualTableNames(),
is(Collections.singletonList("foo_tbl_0")));
+ assertThat(actual.get(1).getDefaultSchemaName(), is("foo_db"));
+ assertThat(actual.get(1).getActualTableNames(),
is(Collections.singletonList("foo_tbl_1")));
}
@Test
- void
assertGetSchemaMetaDataLoaderMaterialsWhenNotConfigCheckMetaDataEnableForSingleTableDataNode()
{
- ShardingSphereRule rule0 = mock(ShardingSphereRule.class);
- DataNodeRuleAttribute ruleAttribute =
mock(DataNodeRuleAttribute.class);
-
when(ruleAttribute.getDataNodesByTableName("t_single")).thenReturn(mockSingleTableDataNodes());
- when(rule0.getAttributes()).thenReturn(new
RuleAttributes(ruleAttribute));
- ShardingSphereRule rule1 = mock(ShardingSphereRule.class);
- when(rule1.getAttributes()).thenReturn(new RuleAttributes());
- ConfigurationProperties props = mock(ConfigurationProperties.class);
-
when(props.getValue(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED)).thenReturn(false);
-
when(props.getValue(ConfigurationPropertyKey.LOAD_TABLE_METADATA_BATCH_SIZE)).thenReturn(100);
- GenericSchemaBuilderMaterial material = new
GenericSchemaBuilderMaterial(mockStorageUnits(), Arrays.asList(rule0, rule1),
props, "public");
- Collection<MetaDataLoaderMaterial> actual =
SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("t_single"),
material);
+ void assertGetMetaDataLoaderMaterialsWhenNotConfigCheckMetaDataEnable() {
+ GenericSchemaBuilderMaterial material = new
GenericSchemaBuilderMaterial(
+ mockStorageUnits(),
Arrays.asList(mockDataNodeRule(Arrays.asList(new DataNode("ds_0.foo_tbl_0"),
new DataNode("ds_1.foo_tbl_1"))),
+ mock(ShardingSphereRule.class, RETURNS_DEEP_STUBS)),
+ new ConfigurationProperties(new Properties()), "foo_db");
+ List<MetaDataLoaderMaterial> actual = new
ArrayList<>(SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("foo_tbl"),
material));
assertThat(actual.size(), is(1));
- Iterator<MetaDataLoaderMaterial> iterator = actual.iterator();
- MetaDataLoaderMaterial firstMaterial = iterator.next();
- assertThat(firstMaterial.getDefaultSchemaName(), is("public"));
- assertThat(firstMaterial.getActualTableNames(),
is(Collections.singletonList("t_single")));
- }
-
- private Collection<DataNode> mockShardingDataNodes() {
- return Arrays.asList(new DataNode("ds_0.t_order_0"), new
DataNode("ds_1.t_order_1"));
- }
-
- private List<DataNode> mockSingleTableDataNodes() {
- DataNode firstDataNode = new DataNode("ds_0", "public", "t_single");
- DataNode secondDataNode = new DataNode("ds_0", "test", "t_single");
- return Arrays.asList(firstDataNode, secondDataNode);
- }
-
- private Map<String, StorageUnit> mockStorageUnits() {
- Map<String, StorageUnit> result = new HashMap<>(2, 1F);
- StorageUnit storageUnit1 = mock(StorageUnit.class);
-
when(storageUnit1.getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class,
"FIXTURE"));
- when(storageUnit1.getDataSource()).thenReturn(new MockedDataSource());
- result.put("ds_0", storageUnit1);
- StorageUnit storageUnit2 = mock(StorageUnit.class);
-
when(storageUnit2.getStorageType()).thenReturn(TypedSPILoader.getService(DatabaseType.class,
"FIXTURE"));
- when(storageUnit2.getDataSource()).thenReturn(new MockedDataSource());
- result.put("ds_1", storageUnit2);
- return result;
+ assertThat(actual.get(0).getDefaultSchemaName(), is("foo_db"));
+ assertThat(actual.get(0).getActualTableNames(),
is(Collections.singletonList("foo_tbl_0")));
}
@Test
void
assertGetMetaDataLoaderMaterialsWhenEmptyDataNodesAndNotEmptyStorageUnits() {
- ShardingSphereRule rule = mock(ShardingSphereRule.class);
- DataNodeRuleAttribute ruleAttribute =
mock(DataNodeRuleAttribute.class);
-
when(ruleAttribute.getDataNodesByTableName("t_order")).thenReturn(Collections.emptyList());
- when(rule.getAttributes()).thenReturn(new
RuleAttributes(ruleAttribute));
- ConfigurationProperties props = mock(ConfigurationProperties.class);
-
when(props.getValue(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED)).thenReturn(true);
-
when(props.getValue(ConfigurationPropertyKey.LOAD_TABLE_METADATA_BATCH_SIZE)).thenReturn(100);
- GenericSchemaBuilderMaterial material = new
GenericSchemaBuilderMaterial(mockStorageUnits(), Collections.singleton(rule),
props, "sharding_db");
- Collection<MetaDataLoaderMaterial> actual =
SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("t_order"),
material);
+ ConfigurationProperties props = new
ConfigurationProperties(PropertiesBuilder.build(new
Property(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED.getKey(),
Boolean.TRUE.toString())));
+ GenericSchemaBuilderMaterial material = new
GenericSchemaBuilderMaterial(mockStorageUnits(),
Collections.singleton(mockDataNodeRule(Collections.emptyList())), props,
"foo_db");
+ Collection<MetaDataLoaderMaterial> actual =
SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("foo_tbl"),
material);
assertThat(actual.size(), is(1));
Iterator<MetaDataLoaderMaterial> iterator = actual.iterator();
MetaDataLoaderMaterial firstMaterial = iterator.next();
- assertThat(firstMaterial.getActualTableNames(),
is(Collections.singletonList("t_order")));
+ assertThat(firstMaterial.getStorageUnitName(), is("ds.with.dot"));
+ assertThat(firstMaterial.getActualTableNames(),
is(Collections.singletonList("foo_tbl")));
}
@Test
void assertGetMetaDataLoaderMaterialsWithDifferentSchemaName() {
- ShardingSphereRule rule = mock(ShardingSphereRule.class);
- DataNodeRuleAttribute ruleAttribute =
mock(DataNodeRuleAttribute.class);
-
when(ruleAttribute.getDataNodesByTableName("t_order")).thenReturn(Collections.singleton(new
DataNode("ds_0", "different_schema", "t_order")));
- when(rule.getAttributes()).thenReturn(new
RuleAttributes(ruleAttribute));
- ConfigurationProperties props = mock(ConfigurationProperties.class);
-
when(props.getValue(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED)).thenReturn(false);
-
when(props.getValue(ConfigurationPropertyKey.LOAD_TABLE_METADATA_BATCH_SIZE)).thenReturn(100);
- GenericSchemaBuilderMaterial material = new
GenericSchemaBuilderMaterial(mockStorageUnits(), Collections.singleton(rule),
props, "default_schema");
- Collection<MetaDataLoaderMaterial> actual =
SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("t_order"),
material);
+ GenericSchemaBuilderMaterial material = new
GenericSchemaBuilderMaterial(mockStorageUnits(),
+
Collections.singleton(mockDataNodeRule(Collections.singleton(new
DataNode("ds_0", "different_schema", "foo_tbl")))),
+ new ConfigurationProperties(new Properties()),
"default_schema");
+ Collection<MetaDataLoaderMaterial> actual =
SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("foo_tbl"),
material);
assertThat(actual.size(), is(1));
Iterator<MetaDataLoaderMaterial> iterator = actual.iterator();
MetaDataLoaderMaterial firstMaterial = iterator.next();
- assertThat(firstMaterial.getActualTableNames(),
is(Collections.singletonList("t_order")));
+ assertThat(firstMaterial.getActualTableNames(),
is(Collections.singletonList("foo_tbl")));
}
@Test
void assertGetMetaDataLoaderMaterialsWithEmptyStorageUnits() {
- ShardingSphereRule rule = mock(ShardingSphereRule.class);
+ ConfigurationProperties props = new
ConfigurationProperties(PropertiesBuilder.build(new
Property(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED.getKey(),
Boolean.TRUE.toString())));
+ GenericSchemaBuilderMaterial material = new
GenericSchemaBuilderMaterial(Collections.emptyMap(),
Collections.singleton(mockDataNodeRule(Collections.emptyList())), props,
"foo_db");
+
assertTrue(SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("foo_tbl"),
material).isEmpty());
+ }
+
+ private Map<String, StorageUnit> mockStorageUnits() {
+ Map<String, StorageUnit> result = new LinkedHashMap<>(4, 1F);
+ result.put("ds.with.dot", mockStorageUnit());
+ result.put("ds", mockStorageUnit());
+ result.put("ds_0", mockStorageUnit());
+ result.put("ds_1", mockStorageUnit());
+ return result;
+ }
+
+ private StorageUnit mockStorageUnit() {
+ StorageUnit result = mock(StorageUnit.class);
+ when(result.getStorageType()).thenReturn(databaseType);
+ return result;
+ }
+
+ private ShardingSphereRule mockDataNodeRule(final Collection<DataNode>
dataNodes) {
+ ShardingSphereRule result = mock(ShardingSphereRule.class);
DataNodeRuleAttribute ruleAttribute =
mock(DataNodeRuleAttribute.class);
-
when(ruleAttribute.getDataNodesByTableName("t_order")).thenReturn(Collections.emptyList());
- when(rule.getAttributes()).thenReturn(new
RuleAttributes(ruleAttribute));
- ConfigurationProperties props = mock(ConfigurationProperties.class);
-
when(props.getValue(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED)).thenReturn(true);
-
when(props.getValue(ConfigurationPropertyKey.LOAD_TABLE_METADATA_BATCH_SIZE)).thenReturn(100);
- GenericSchemaBuilderMaterial material = new
GenericSchemaBuilderMaterial(Collections.emptyMap(),
Collections.singleton(rule), props, "sharding_db");
- Collection<MetaDataLoaderMaterial> actual =
SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("t_order"),
material);
- assertTrue(actual.isEmpty());
+
when(ruleAttribute.getDataNodesByTableName("foo_tbl")).thenReturn(dataNodes);
+ when(result.getAttributes()).thenReturn(new
RuleAttributes(ruleAttribute));
+ return result;
}
}
diff --git
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/type/ddl/table/CreateTableStatement.java
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/type/ddl/table/CreateTableStatement.java
index 8131312eb55..168751a3f74 100644
---
a/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/type/ddl/table/CreateTableStatement.java
+++
b/parser/sql/statement/core/src/main/java/org/apache/shardingsphere/sql/parser/statement/core/statement/type/ddl/table/CreateTableStatement.java
@@ -113,7 +113,7 @@ public final class CreateTableStatement extends
DDLStatement {
return result;
}
- private class CreateTableConstraintSQLStatementAttribute implements
ConstraintSQLStatementAttribute {
+ private final class CreateTableConstraintSQLStatementAttribute implements
ConstraintSQLStatementAttribute {
@Override
public Collection<ConstraintSegment> getConstraints() {
@@ -125,7 +125,7 @@ public final class CreateTableStatement extends
DDLStatement {
}
}
- private class CreateTableIndexSQLStatementAttribute implements
IndexSQLStatementAttribute {
+ private final class CreateTableIndexSQLStatementAttribute implements
IndexSQLStatementAttribute {
@Override
public Collection<IndexSegment> getIndexes() {