This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 cc6d758b669 Refactor SimpleTableSegmentBinder to use Optional for 
schema handling (#37458)
cc6d758b669 is described below

commit cc6d758b669f48774f31c86a1e1fb2e6b5a825b1
Author: Cong Hu <[email protected]>
AuthorDate: Mon Dec 22 19:00:56 2025 +0800

    Refactor SimpleTableSegmentBinder to use Optional for schema handling 
(#37458)
---
 .../dml/from/type/SimpleTableSegmentBinder.java    | 72 ++++++++++++----------
 1 file changed, 40 insertions(+), 32 deletions(-)

diff --git 
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/dml/from/type/SimpleTableSegmentBinder.java
 
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/dml/from/type/SimpleTableSegmentBinder.java
index 18d55945553..0709ee965fb 100644
--- 
a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/dml/from/type/SimpleTableSegmentBinder.java
+++ 
b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/engine/segment/dml/from/type/SimpleTableSegmentBinder.java
@@ -88,15 +88,15 @@ public final class SimpleTableSegmentBinder {
                                           final 
Multimap<CaseInsensitiveString, TableSegmentBinderContext> tableBinderContexts) 
{
         
binderContext.getPivotColumnNames().addAll(segment.getPivot().map(PivotSegment::getPivotColumnNames).orElse(Collections.emptyList()));
         IdentifierValue databaseName = getDatabaseName(segment, binderContext);
-        IdentifierValue schemaName = getSchemaName(segment, binderContext, 
databaseName);
+        Optional<IdentifierValue> schemaName = getSchemaName(segment, 
binderContext, databaseName);
         IdentifierValue tableName = segment.getTableName().getIdentifier();
-        ShardingSphereSchema schema = 
binderContext.getMetaData().getDatabase(databaseName.getValue()).getSchema(schemaName.getValue());
-        checkTableExists(binderContext, schema, schemaName.getValue(), 
tableName.getValue());
-        checkTableMetadata(binderContext, schema, schemaName.getValue(), 
tableName.getValue());
+        Optional<ShardingSphereSchema> schema = schemaName.map(identifierValue 
-> 
binderContext.getMetaData().getDatabase(databaseName.getValue()).getSchema(identifierValue.getValue()));
+        checkTableExists(binderContext, schema, tableName.getValue());
+        checkTableMetadata(binderContext, schema.orElse(null), 
schemaName.map(IdentifierValue::getValue).orElse(null), tableName.getValue());
         tableBinderContexts.put(new 
CaseInsensitiveString(segment.getAliasName().orElseGet(tableName::getValue)),
                 createSimpleTableBinderContext(segment, schema, databaseName, 
schemaName, binderContext));
         TableNameSegment tableNameSegment = new 
TableNameSegment(segment.getTableName().getStartIndex(), 
segment.getTableName().getStopIndex(), tableName);
-        tableNameSegment.setTableBoundInfo(new 
TableSegmentBoundInfo(databaseName, schemaName));
+        tableNameSegment.setTableBoundInfo(new 
TableSegmentBoundInfo(databaseName, schemaName.orElse(null)));
         SimpleTableSegment result = new SimpleTableSegment(tableNameSegment);
         segment.getOwner().ifPresent(result::setOwner);
         segment.getAliasSegment().ifPresent(result::setAlias);
@@ -112,62 +112,70 @@ public final class SimpleTableSegmentBinder {
         return result;
     }
     
-    private static IdentifierValue getSchemaName(final SimpleTableSegment 
segment, final SQLStatementBinderContext binderContext, final IdentifierValue 
databaseName) {
-        IdentifierValue result = getSchemaName(segment, binderContext);
-        
ShardingSpherePreconditions.checkState(binderContext.getMetaData().getDatabase(databaseName.getValue()).containsSchema(result.getValue()),
-                () -> new SchemaNotFoundException(result.getValue()));
+    private static Optional<IdentifierValue> getSchemaName(final 
SimpleTableSegment segment, final SQLStatementBinderContext binderContext, 
final IdentifierValue databaseName) {
+        Optional<IdentifierValue> result = getSchemaName(segment, 
binderContext);
+        result.ifPresent(identifierValue -> 
ShardingSpherePreconditions.checkState(binderContext.getMetaData().getDatabase(databaseName.getValue()).containsSchema(identifierValue.getValue()),
+                () -> new 
SchemaNotFoundException(identifierValue.getValue())));
         return result;
     }
     
-    private static IdentifierValue getSchemaName(final SimpleTableSegment 
segment, final SQLStatementBinderContext binderContext) {
+    private static Optional<IdentifierValue> getSchemaName(final 
SimpleTableSegment segment, final SQLStatementBinderContext binderContext) {
         if (segment.getOwner().isPresent()) {
-            return segment.getOwner().get().getIdentifier();
+            return 
Optional.ofNullable(segment.getOwner().get().getIdentifier());
         }
-        // TODO getSchemaName according to search path
         DatabaseType databaseType = 
binderContext.getSqlStatement().getDatabaseType();
         DatabaseTypeRegistry databaseTypeRegistry = new 
DatabaseTypeRegistry(databaseType);
-        Optional<String> defaultSystemSchema = 
databaseTypeRegistry.getDialectDatabaseMetaData().getSchemaOption().getDefaultSystemSchema();
-        return defaultSystemSchema.isPresent() && 
SystemSchemaManager.isSystemTable(databaseType.getType(), 
defaultSystemSchema.get(), segment.getTableName().getIdentifier().getValue())
-                ? new IdentifierValue(defaultSystemSchema.get())
-                : new 
IdentifierValue(databaseTypeRegistry.getDefaultSchemaName(binderContext.getCurrentDatabaseName()));
+        DialectDatabaseMetaData dialectDatabaseMetaData = 
databaseTypeRegistry.getDialectDatabaseMetaData();
+        Optional<String> defaultSystemSchema = 
dialectDatabaseMetaData.getSchemaOption().getDefaultSystemSchema();
+        if (defaultSystemSchema.isPresent() && 
SystemSchemaManager.isSystemTable(databaseType.getType(), 
defaultSystemSchema.get(), segment.getTableName().getIdentifier().getValue())) {
+            return Optional.of(new IdentifierValue(defaultSystemSchema.get()));
+        }
+        return Optional.of(new 
IdentifierValue(databaseTypeRegistry.getDefaultSchemaName(binderContext.getCurrentDatabaseName())));
     }
     
-    private static void checkTableExists(final SQLStatementBinderContext 
binderContext, final ShardingSphereSchema schema, final String schemaName, 
final String tableName) {
+    private static void checkTableExists(final SQLStatementBinderContext 
binderContext, final Optional<ShardingSphereSchema> schema, final String 
tableName) {
         // TODO refactor table exists check with spi @duanzhengqiang
         if (binderContext.getSqlStatement() instanceof CreateTableStatement && 
isCreateTable(((CreateTableStatement) 
binderContext.getSqlStatement()).getTable(), tableName)) {
             
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate()
-                    || ((CreateTableStatement) 
binderContext.getSqlStatement()).isIfNotExists() || 
!schema.containsTable(tableName), () -> new TableExistsException(tableName));
+                    || ((CreateTableStatement) 
binderContext.getSqlStatement()).isIfNotExists() || !schema.isPresent() || 
!schema.get().containsTable(tableName),
+                    () -> new TableExistsException(tableName));
             return;
         }
         if (binderContext.getSqlStatement() instanceof AlterTableStatement && 
isRenameTable((AlterTableStatement) binderContext.getSqlStatement(), 
tableName)) {
-            
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate()
 || !schema.containsTable(tableName), () -> new 
TableExistsException(tableName));
+            
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate()
 || !schema.isPresent() || !schema.get().containsTable(tableName),
+                    () -> new TableExistsException(tableName));
             return;
         }
         if (binderContext.getSqlStatement() instanceof DropTableStatement) {
-            ShardingSpherePreconditions.checkState(((DropTableStatement) 
binderContext.getSqlStatement()).isIfExists() || 
schema.containsTable(tableName), () -> new TableNotFoundException(tableName));
+            ShardingSpherePreconditions.checkState(((DropTableStatement) 
binderContext.getSqlStatement()).isIfExists() || schema.isPresent() && 
schema.get().containsTable(tableName),
+                    () -> new TableNotFoundException(tableName));
             return;
         }
         if (binderContext.getSqlStatement() instanceof RenameTableStatement && 
isRenameTable((RenameTableStatement) binderContext.getSqlStatement(), 
tableName)) {
-            
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate()
 || !schema.containsTable(tableName), () -> new 
TableExistsException(tableName));
+            
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate()
 || !schema.isPresent() || !schema.get().containsTable(tableName),
+                    () -> new TableExistsException(tableName));
             return;
         }
         if (binderContext.getSqlStatement() instanceof CreateViewStatement && 
isCreateTable(((CreateViewStatement) 
binderContext.getSqlStatement()).getView(), tableName)) {
             
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate()
-                    || ((CreateViewStatement) 
binderContext.getSqlStatement()).isReplaceView() || 
!schema.containsTable(tableName), () -> new TableExistsException(tableName));
+                    || ((CreateViewStatement) 
binderContext.getSqlStatement()).isReplaceView() || !schema.isPresent() || 
!schema.get().containsTable(tableName),
+                    () -> new TableExistsException(tableName));
             return;
         }
         if (binderContext.getSqlStatement() instanceof AlterViewStatement && 
isRenameView((AlterViewStatement) binderContext.getSqlStatement(), tableName)) {
-            
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate()
 || !schema.containsTable(tableName), () -> new 
TableExistsException(tableName));
+            
ShardingSpherePreconditions.checkState(binderContext.getHintValueContext().isSkipMetadataValidate()
 || !schema.isPresent() || !schema.get().containsTable(tableName),
+                    () -> new TableExistsException(tableName));
             return;
         }
         if (binderContext.getSqlStatement() instanceof DropViewStatement) {
-            ShardingSpherePreconditions.checkState(((DropViewStatement) 
binderContext.getSqlStatement()).isIfExists() || 
schema.containsTable(tableName), () -> new TableNotFoundException(tableName));
+            ShardingSpherePreconditions.checkState(((DropViewStatement) 
binderContext.getSqlStatement()).isIfExists() || schema.isPresent() && 
schema.get().containsTable(tableName),
+                    () -> new TableNotFoundException(tableName));
             return;
         }
         if ("DUAL".equalsIgnoreCase(tableName)) {
             return;
         }
-        if (SystemSchemaManager.isSystemTable(schemaName, tableName)) {
+        if (schema.isPresent() && 
SystemSchemaManager.isSystemTable(schema.get().getName(), tableName)) {
             return;
         }
         if (binderContext.getExternalTableBinderContexts().containsKey(new 
CaseInsensitiveString(tableName))) {
@@ -176,7 +184,7 @@ public final class SimpleTableSegmentBinder {
         if 
(binderContext.getCommonTableExpressionsSegmentsUniqueAliases().contains(tableName))
 {
             return;
         }
-        
ShardingSpherePreconditions.checkState(schema.containsTable(tableName), () -> 
new TableNotFoundException(tableName));
+        ShardingSpherePreconditions.checkState(schema.isPresent() && 
schema.get().containsTable(tableName), () -> new 
TableNotFoundException(tableName));
     }
     
     private static boolean isCreateTable(final SimpleTableSegment 
simpleTableSegment, final String tableName) {
@@ -201,7 +209,7 @@ public final class SimpleTableSegmentBinder {
     }
     
     private static void checkTableMetadata(final SQLStatementBinderContext 
binderContext, final ShardingSphereSchema schema, final String schemaName, 
final String tableName) {
-        if (binderContext.getHintValueContext().isSkipMetadataValidate()) {
+        if (binderContext.getHintValueContext().isSkipMetadataValidate() || 
null == schema) {
             return;
         }
         ShardingSphereTable shardingSphereTable = schema.getTable(tableName);
@@ -228,14 +236,14 @@ public final class SimpleTableSegmentBinder {
         }
     }
     
-    private static SimpleTableSegmentBinderContext 
createSimpleTableBinderContext(final SimpleTableSegment segment, final 
ShardingSphereSchema schema, final IdentifierValue databaseName,
-                                                                               
   final IdentifierValue schemaName, final SQLStatementBinderContext 
binderContext) {
+    private static SimpleTableSegmentBinderContext 
createSimpleTableBinderContext(final SimpleTableSegment segment, final 
Optional<ShardingSphereSchema> schema, final IdentifierValue databaseName,
+                                                                               
   final Optional<IdentifierValue> schemaName, final SQLStatementBinderContext 
binderContext) {
         IdentifierValue tableName = segment.getTableName().getIdentifier();
-        if 
(binderContext.getMetaData().getDatabase(databaseName.getValue()).getSchema(schemaName.getValue()).containsTable(tableName.getValue()))
 {
-            return createSimpleTableSegmentBinderContextWithMetaData(segment, 
schema, databaseName, schemaName, binderContext, tableName);
+        if (schema.isPresent() && 
schema.get().containsTable(tableName.getValue())) {
+            return createSimpleTableSegmentBinderContextWithMetaData(segment, 
schema.get(), databaseName, schemaName.get(), binderContext, tableName);
         }
         if (binderContext.getSqlStatement() instanceof CreateTableStatement) {
-            Collection<ProjectionSegment> projectionSegments = 
createProjectionSegments((CreateTableStatement) 
binderContext.getSqlStatement(), databaseName, schemaName, tableName);
+            Collection<ProjectionSegment> projectionSegments = 
createProjectionSegments((CreateTableStatement) 
binderContext.getSqlStatement(), databaseName, schemaName.orElse(null), 
tableName);
             return new SimpleTableSegmentBinderContext(projectionSegments, 
TableSourceType.PHYSICAL_TABLE);
         }
         CaseInsensitiveString caseInsensitiveTableName = new 
CaseInsensitiveString(tableName.getValue());

Reply via email to