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 6cf2bfc68a7 Remove unreached codes on CDCSchemaTableUtils (#37414)
6cf2bfc68a7 is described below
commit 6cf2bfc68a77d19ab8e17783b21038a1b843585e
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Dec 17 12:50:08 2025 +0800
Remove unreached codes on CDCSchemaTableUtils (#37414)
---
.../pipeline/cdc/util/CDCSchemaTableUtils.java | 5 +-
.../pipeline/cdc/util/CDCSchemaTableUtilsTest.java | 108 ++++++++++++++-------
2 files changed, 74 insertions(+), 39 deletions(-)
diff --git
a/kernel/data-pipeline/scenario/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtils.java
b/kernel/data-pipeline/scenario/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtils.java
index 17db098c62c..44aaf2d6ed3 100644
---
a/kernel/data-pipeline/scenario/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtils.java
+++
b/kernel/data-pipeline/scenario/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtils.java
@@ -99,10 +99,7 @@ public final class CDCSchemaTableUtils {
}
private static Map<String, Set<String>>
parseTableExpressionWithAllTable(final ShardingSphereDatabase database, final
SchemaTable schemaTable) {
- DialectDatabaseMetaData dialectDatabaseMetaData = new
DatabaseTypeRegistry(database.getProtocolType()).getDialectDatabaseMetaData();
- String schemaName = schemaTable.getSchema().isEmpty()
- ?
dialectDatabaseMetaData.getSchemaOption().getDefaultSchema().orElseThrow(() ->
new IllegalStateException("Default schema should exist."))
- : schemaTable.getSchema();
+ String schemaName = schemaTable.getSchema();
ShardingSphereSchema schema = database.getSchema(schemaName);
ShardingSpherePreconditions.checkNotNull(schema, () -> new
SchemaNotFoundException(schemaTable.getSchema()));
Collection<ShardingSphereTable> tables = schema.getAllTables();
diff --git
a/kernel/data-pipeline/scenario/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtilsTest.java
b/kernel/data-pipeline/scenario/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtilsTest.java
index 525b5b2a01b..9b796f59fa8 100644
---
a/kernel/data-pipeline/scenario/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtilsTest.java
+++
b/kernel/data-pipeline/scenario/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/util/CDCSchemaTableUtilsTest.java
@@ -19,12 +19,14 @@ package org.apache.shardingsphere.data.pipeline.cdc.util;
import
org.apache.shardingsphere.data.pipeline.cdc.protocol.request.StreamDataRequestBody.SchemaTable;
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import
org.apache.shardingsphere.infra.exception.kernel.metadata.TableNotFoundException;
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.spi.type.typed.TypedSPILoader;
import org.junit.jupiter.api.Test;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -36,6 +38,7 @@ import java.util.Set;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -44,50 +47,85 @@ class CDCSchemaTableUtilsTest {
private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "openGauss");
@Test
- void assertParseTableExpression() {
- ShardingSphereDatabase database = new
ShardingSphereDatabase("sharding_db", databaseType, null, null,
Arrays.asList(mockedPublicSchema(), mockedTestSchema()));
- List<SchemaTable> schemaTables =
Arrays.asList(SchemaTable.newBuilder().setSchema("public").setTable("t_order").build(),
SchemaTable.newBuilder().setSchema("test").setTable("*").build());
- Map<String, Set<String>> expected = new HashMap<>(2, 1F);
- expected.put("test", new HashSet<>(Arrays.asList("t_order_item",
"t_order_item2")));
- expected.put("public", Collections.singleton("t_order"));
- Map<String, Set<String>> actual =
CDCSchemaTableUtils.parseTableExpressionWithSchema(database, schemaTables);
- assertThat(actual, is(expected));
- schemaTables =
Collections.singletonList(SchemaTable.newBuilder().setTable("t_order").build());
- actual = CDCSchemaTableUtils.parseTableExpressionWithSchema(database,
schemaTables);
- expected = Collections.singletonMap("public",
Collections.singleton("t_order"));
- assertThat(actual, is(expected));
- schemaTables =
Collections.singletonList(SchemaTable.newBuilder().setSchema("*").setTable("t_order").build());
- actual = CDCSchemaTableUtils.parseTableExpressionWithSchema(database,
schemaTables);
- expected = Collections.singletonMap("public",
Collections.singleton("t_order"));
- assertThat(actual, is(expected));
+ void assertParseTableExpressionWithFullWildcard() {
+ ShardingSphereSchema publicSchema = mockSchema("public", "t_order",
"t_order_item");
+ ShardingSphereSchema testSchema = mockSchema("test", "t_test");
+ ShardingSphereSchema systemSchema = mockSchema("pg_catalog", "t_pg");
+ ShardingSphereDatabase database = new
ShardingSphereDatabase("sharding_db", databaseType, null, null,
Arrays.asList(publicSchema, testSchema, systemSchema));
+ List<SchemaTable> schemaTables =
Collections.singletonList(SchemaTable.newBuilder().setSchema("*").setTable("*").build());
+ Map<String, Set<String>> actualResult =
CDCSchemaTableUtils.parseTableExpressionWithSchema(database, schemaTables);
+ Map<String, Set<String>> expectedResult = new HashMap<>(2, 1F);
+ expectedResult.put("public", new HashSet<>(Arrays.asList("t_order",
"t_order_item")));
+ expectedResult.put("test", Collections.singleton("t_test"));
+ assertThat(actualResult, is(expectedResult));
}
- private ShardingSphereSchema mockedPublicSchema() {
- ShardingSphereTable table1 = mock(ShardingSphereTable.class);
- when(table1.getName()).thenReturn("t_order");
- ShardingSphereTable table2 = mock(ShardingSphereTable.class);
- when(table2.getName()).thenReturn("t_order2");
- return new ShardingSphereSchema("public", Arrays.asList(table1,
table2), Collections.emptyList());
+ @Test
+ void assertParseTableExpressionWithSchemaWildcard() {
+ ShardingSphereSchema analyticsSchema = mockSchema("analytics",
"t_shared");
+ ShardingSphereSchema auditSchema = mockSchema("audit", "t_other");
+ ShardingSphereSchema systemSchema = mockSchema("pg_catalog",
"t_shared");
+ ShardingSphereDatabase database = new
ShardingSphereDatabase("sharding_db", databaseType, null, null,
Arrays.asList(analyticsSchema, auditSchema, systemSchema));
+ List<SchemaTable> schemaTables =
Collections.singletonList(SchemaTable.newBuilder().setSchema("*").setTable("t_shared").build());
+ Map<String, Set<String>> actualResult =
CDCSchemaTableUtils.parseTableExpressionWithSchema(database, schemaTables);
+ Map<String, Set<String>> expectedResult =
Collections.singletonMap("analytics", Collections.singleton("t_shared"));
+ assertThat(actualResult, is(expectedResult));
+ }
+
+ @Test
+ void assertParseTableExpressionWithAllTablesInSchema() {
+ ShardingSphereSchema publicSchema = mockSchema("public", "t_order",
"t_order_item");
+ ShardingSphereDatabase database = new
ShardingSphereDatabase("sharding_db", databaseType, null, null,
Collections.singleton(publicSchema));
+ List<SchemaTable> schemaTables =
Collections.singletonList(SchemaTable.newBuilder().setSchema("public").setTable("*").build());
+ Map<String, Set<String>> actualResult =
CDCSchemaTableUtils.parseTableExpressionWithSchema(database, schemaTables);
+ Map<String, Set<String>> expectedResult =
Collections.singletonMap("public", new HashSet<>(Arrays.asList("t_order",
"t_order_item")));
+ assertThat(actualResult, is(expectedResult));
+ }
+
+ @Test
+ void assertParseTableExpressionFillDefaultSchema() {
+ ShardingSphereSchema publicSchema = mockSchema("public", "t_order");
+ ShardingSphereDatabase database = new
ShardingSphereDatabase("sharding_db", databaseType, null, null,
Collections.singleton(publicSchema));
+ List<SchemaTable> schemaTables =
Collections.singletonList(SchemaTable.newBuilder().setTable("t_order").build());
+ Map<String, Set<String>> actualResult =
CDCSchemaTableUtils.parseTableExpressionWithSchema(database, schemaTables);
+ Map<String, Set<String>> expectedResult =
Collections.singletonMap("public", Collections.singleton("t_order"));
+ assertThat(actualResult, is(expectedResult));
}
- private ShardingSphereSchema mockedTestSchema() {
- ShardingSphereTable table1 = mock(ShardingSphereTable.class);
- when(table1.getName()).thenReturn("t_order_item");
- ShardingSphereTable table2 = mock(ShardingSphereTable.class);
- when(table2.getName()).thenReturn("t_order_item2");
- return new ShardingSphereSchema("test", Arrays.asList(table1, table2),
Collections.emptyList());
+ @Test
+ void assertParseTableExpressionWithMissingTable() {
+ ShardingSphereSchema publicSchema = mockSchema("public", "t_exist");
+ ShardingSphereDatabase database = new
ShardingSphereDatabase("sharding_db", databaseType, null, null,
Collections.singleton(publicSchema));
+ List<SchemaTable> schemaTables =
Collections.singletonList(SchemaTable.newBuilder().setSchema("public").setTable("t_missing").build());
+ assertThrows(TableNotFoundException.class, () ->
CDCSchemaTableUtils.parseTableExpressionWithSchema(database, schemaTables));
}
@Test
void assertParseTableExpressionWithoutSchema() {
- ShardingSphereDatabase database = new ShardingSphereDatabase("public",
TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), null, null,
Collections.singleton(mockedPublicSchema()));
- List<String> schemaTables = Collections.singletonList("*");
- Collection<String> actualWildcardTable =
CDCSchemaTableUtils.parseTableExpressionWithoutSchema(database, schemaTables);
+ ShardingSphereSchema publicSchema = mockSchema("public", "t_order",
"t_order2");
+ ShardingSphereDatabase database = new ShardingSphereDatabase("public",
TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), null, null,
Collections.singleton(publicSchema));
+ List<String> tableNames = Collections.singletonList("*");
+ Collection<String> actualWildcardTable =
CDCSchemaTableUtils.parseTableExpressionWithoutSchema(database, tableNames);
Set<String> expectedWildcardTable = new
HashSet<>(Arrays.asList("t_order", "t_order2"));
assertThat(actualWildcardTable, is(expectedWildcardTable));
- schemaTables = Collections.singletonList("t_order");
- Collection<String> actualSingleTable =
CDCSchemaTableUtils.parseTableExpressionWithoutSchema(database, schemaTables);
- Set<String> expectedSingleTable = new
HashSet<>(Collections.singletonList("t_order"));
- assertThat(actualSingleTable, is(expectedSingleTable));
+ ShardingSphereDatabase databaseWithoutSchema = new
ShardingSphereDatabase("missing", TypedSPILoader.getService(DatabaseType.class,
"FIXTURE"), null, null, Collections.emptyList());
+ List<String> singleTable = Collections.singletonList("t_order");
+ Collection<String> actualTableNames =
CDCSchemaTableUtils.parseTableExpressionWithoutSchema(databaseWithoutSchema,
singleTable);
+ Set<String> expectedTableNames = new HashSet<>(singleTable);
+ assertThat(actualTableNames, is(expectedTableNames));
+ }
+
+ private ShardingSphereSchema mockSchema(final String schemaName, final
String... tableNames) {
+ Collection<ShardingSphereTable> tables = new
ArrayList<>(tableNames.length);
+ for (String each : tableNames) {
+ tables.add(mockTable(each));
+ }
+ return new ShardingSphereSchema(schemaName, tables,
Collections.emptyList());
+ }
+
+ private ShardingSphereTable mockTable(final String tableName) {
+ ShardingSphereTable result = mock(ShardingSphereTable.class);
+ when(result.getName()).thenReturn(tableName);
+ return result;
}
}