This is an automated email from the ASF dual-hosted git repository.
menghaoranss 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 9fdc12bd541 Use database default schema in sharding drop index checker
(#39513)
9fdc12bd541 is described below
commit 9fdc12bd541c4d23210a7bbba82ed92c8537faf3
Author: Haoran Meng <[email protected]>
AuthorDate: Wed Aug 19 15:44:52 2026 +0800
Use database default schema in sharding drop index checker (#39513)
* Use database default schema in sharding drop index checker
* Fix checkstyle
---
.../ddl/ShardingDropIndexRouteContextChecker.java | 9 +++--
.../ShardingDropIndexRouteContextCheckerTest.java | 38 ++++++++++++++++++----
2 files changed, 38 insertions(+), 9 deletions(-)
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/checker/ddl/ShardingDropIndexRouteContextChecker.java
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/checker/ddl/ShardingDropIndexRouteContextChecker.java
index 5934b6d7b82..da045bc2cb2 100644
---
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/checker/ddl/ShardingDropIndexRouteContextChecker.java
+++
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/route/engine/checker/ddl/ShardingDropIndexRouteContextChecker.java
@@ -17,8 +17,9 @@
package org.apache.shardingsphere.sharding.route.engine.checker.ddl;
-import
org.apache.shardingsphere.database.connector.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
+import
org.apache.shardingsphere.infra.exception.kernel.metadata.SchemaNotFoundException;
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;
@@ -48,9 +49,11 @@ public final class ShardingDropIndexRouteContextChecker
implements ShardingRoute
if (logicTableName.isPresent()) {
validateDropIndexRouteUnit(shardingRule, routeContext,
indexSegments, logicTableName.get());
} else {
- String defaultSchemaName = new
DatabaseTypeRegistry(queryContext.getSqlStatementContext().getSqlStatement().getDatabaseType()).getDefaultSchemaName(database.getName());
+ ShardingSphereSchema defaultSchema =
database.findDefaultSchema().orElse(null);
for (IndexSegment each : indexSegments) {
- ShardingSphereSchema schema = each.getOwner().map(optional ->
optional.getIdentifier().getValue()).map(database::getSchema).orElseGet(() ->
database.getSchema(defaultSchemaName));
+ Optional<String> schemaName = each.getOwner().map(optional ->
optional.getIdentifier().getValue());
+ ShardingSphereSchema schema = schemaName.isPresent() ?
database.getSchema(schemaName.get()) : defaultSchema;
+ ShardingSpherePreconditions.checkNotNull(schema, () -> new
SchemaNotFoundException(schemaName.orElseGet(database::getDefaultSchemaName)));
logicTableName = schema.getAllTables().stream().filter(table
->
table.containsIndex(each.getIndexName().getIdentifier().getValue())).findFirst().map(ShardingSphereTable::getName);
logicTableName.ifPresent(optional ->
validateDropIndexRouteUnit(shardingRule, routeContext, indexSegments,
optional));
}
diff --git
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/checker/ddl/ShardingDropIndexRouteContextCheckerTest.java
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/checker/ddl/ShardingDropIndexRouteContextCheckerTest.java
index c97ae4c7a16..9f9796a7dd9 100644
---
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/checker/ddl/ShardingDropIndexRouteContextCheckerTest.java
+++
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/checker/ddl/ShardingDropIndexRouteContextCheckerTest.java
@@ -20,7 +20,9 @@ package
org.apache.shardingsphere.sharding.route.engine.checker.ddl;
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import
org.apache.shardingsphere.infra.binder.context.statement.type.CommonSQLStatementContext;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import
org.apache.shardingsphere.infra.exception.kernel.metadata.SchemaNotFoundException;
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.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
@@ -32,11 +34,11 @@ import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.ShardingTable;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexNameSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.OwnerSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.type.ddl.index.DropIndexStatement;
import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@@ -44,6 +46,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
+import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -58,7 +61,7 @@ class ShardingDropIndexRouteContextCheckerTest {
@Mock
private ShardingRule shardingRule;
- @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ @Mock
private ShardingSphereDatabase database;
@Mock
@@ -69,10 +72,11 @@ class ShardingDropIndexRouteContextCheckerTest {
@Test
void assertCheckWithSameRouteResultShardingTableIndexForPostgreSQL() {
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
ShardingSphereTable table = mock(ShardingSphereTable.class);
when(table.getName()).thenReturn("t_order");
-
when(database.getSchema("public").getAllTables()).thenReturn(Collections.singleton(table));
-
when(database.getSchema("public").getTable("t_order")).thenReturn(table);
+ when(database.findDefaultSchema()).thenReturn(Optional.of(schema));
+ when(schema.getAllTables()).thenReturn(Collections.singleton(table));
when(table.containsIndex("t_order_index")).thenReturn(true);
when(shardingRule.isShardingTable("t_order")).thenReturn(true);
when(shardingRule.getShardingTable("t_order")).thenReturn(new
ShardingTable(Arrays.asList("ds_0", "ds_1"), "t_order"));
@@ -90,10 +94,11 @@ class ShardingDropIndexRouteContextCheckerTest {
@Test
void assertCheckWithDifferentRouteResultShardingTableIndexForPostgreSQL() {
+ ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
ShardingSphereTable table = mock(ShardingSphereTable.class);
when(table.getName()).thenReturn("t_order");
-
when(database.getSchema("public").getAllTables()).thenReturn(Collections.singleton(table));
-
when(database.getSchema("public").getTable("t_order")).thenReturn(table);
+ when(database.findDefaultSchema()).thenReturn(Optional.of(schema));
+ when(schema.getAllTables()).thenReturn(Collections.singleton(table));
when(table.containsIndex("t_order_index")).thenReturn(true);
when(shardingRule.isShardingTable("t_order")).thenReturn(true);
when(shardingRule.getShardingTable("t_order")).thenReturn(new
ShardingTable(Arrays.asList("ds_0", "ds_1"), "t_order"));
@@ -107,4 +112,25 @@ class ShardingDropIndexRouteContextCheckerTest {
when(queryContext.getSqlStatementContext()).thenReturn(new
CommonSQLStatementContext(sqlStatement));
assertThrows(ShardingDDLRouteException.class, () -> new
ShardingDropIndexRouteContextChecker().check(shardingRule, queryContext,
database, mock(ConfigurationProperties.class), routeContext));
}
+
+ @Test
+ void assertCheckWithoutDefaultSchema() {
+ DropIndexStatement sqlStatement =
DropIndexStatement.builder().databaseType(databaseType)
+ .indexes(Collections.singleton(new IndexSegment(0, 0, new
IndexNameSegment(0, 0, new IdentifierValue("foo_index"))))).build();
+ when(queryContext.getSqlStatementContext()).thenReturn(new
CommonSQLStatementContext(sqlStatement));
+ when(database.findDefaultSchema()).thenReturn(Optional.empty());
+ when(database.getDefaultSchemaName()).thenReturn("foo_default_schema");
+ assertThrows(SchemaNotFoundException.class,
+ () -> new
ShardingDropIndexRouteContextChecker().check(shardingRule, queryContext,
database, mock(ConfigurationProperties.class), routeContext));
+ }
+
+ @Test
+ void assertCheckWithMissingOwnerSchema() {
+ IndexSegment indexSegment = new IndexSegment(0, 0, new
IndexNameSegment(0, 0, new IdentifierValue("foo_index")));
+ indexSegment.setOwner(new OwnerSegment(0, 0, new
IdentifierValue("foo_schema")));
+ DropIndexStatement sqlStatement =
DropIndexStatement.builder().databaseType(databaseType).indexes(Collections.singleton(indexSegment)).build();
+ when(queryContext.getSqlStatementContext()).thenReturn(new
CommonSQLStatementContext(sqlStatement));
+ assertThrows(SchemaNotFoundException.class,
+ () -> new
ShardingDropIndexRouteContextChecker().check(shardingRule, queryContext,
database, mock(ConfigurationProperties.class), routeContext));
+ }
}