You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2026/04/27 10:17:56 UTC

(shardingsphere) branch master updated: Add identifier schemas for TablesContext (#38653)

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 9711c475a27 Add identifier schemas for TablesContext (#38653)
9711c475a27 is described below

commit 9711c475a27f638d45fa4c892b85bae7bceccc2b
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Mon Apr 27 18:17:49 2026 +0800

    Add identifier schemas for TablesContext (#38653)
---
 .../context/segment/table/TablesContext.java       | 17 +++++++++++-
 .../context/segment/table/TablesContextTest.java   | 17 ++++++++++++
 .../refresher/util/SchemaRefreshUtils.java         | 10 ++++----
 .../refresher/util/SchemaRefreshUtilsTest.java     | 30 +++++++++++++++++++++-
 4 files changed, 67 insertions(+), 7 deletions(-)

diff --git a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java
index ff418e90d7c..6d924633478 100644
--- a/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java
+++ b/infra/binder/core/src/main/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContext.java
@@ -24,6 +24,7 @@ import lombok.ToString;
 import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.SimpleTableSegment;
 import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableNameSegment;
 import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table.TableSegment;
+import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -45,6 +46,8 @@ public final class TablesContext {
     
     private final Collection<String> databaseNames = new CaseInsensitiveSet<>();
     
+    private final Collection<IdentifierValue> identifierSchemaNames = new LinkedList<>();
+    
     public TablesContext(final SimpleTableSegment table) {
         this(null == table ? Collections.emptyList() : Collections.singletonList(table));
     }
@@ -68,7 +71,10 @@ public final class TablesContext {
         tableNames.add(tableName.getIdentifier().getValue());
         // TODO support bind with all statement contains table segment @duanzhengqiang
         tableName.getTableBoundInfo().filter(optional -> !Strings.isNullOrEmpty(optional.getOriginalSchema().getValue()))
-                .ifPresent(optional -> schemaNames.add(optional.getOriginalSchema().getValue()));
+                .ifPresent(optional -> {
+                    schemaNames.add(optional.getOriginalSchema().getValue());
+                    identifierSchemaNames.add(optional.getOriginalSchema());
+                });
         tableName.getTableBoundInfo().filter(optional -> !Strings.isNullOrEmpty(optional.getOriginalDatabase().getValue()))
                 .ifPresent(optional -> databaseNames.add(optional.getOriginalDatabase().getValue()));
     }
@@ -90,4 +96,13 @@ public final class TablesContext {
     public Optional<String> getSchemaName() {
         return schemaNames.isEmpty() ? Optional.empty() : Optional.of(schemaNames.iterator().next());
     }
+    
+    /**
+     * Get schema name.
+     *
+     * @return schema name
+     */
+    public Optional<IdentifierValue> getIdentifierSchemaName() {
+        return identifierSchemaNames.isEmpty() ? Optional.empty() : Optional.of(identifierSchemaNames.iterator().next());
+    }
 }
diff --git a/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContextTest.java b/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContextTest.java
index 32539c541a5..42bf7ddebff 100644
--- a/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContextTest.java
+++ b/infra/binder/core/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/table/TablesContextTest.java
@@ -67,6 +67,23 @@ class TablesContextTest {
         assertThat(tablesContext.getTableNames(), is(Collections.singleton("tbl")));
     }
     
+    @Test
+    void assertGetIdentifierSchemaName() {
+        IdentifierValue schemaIdentifier = new IdentifierValue("foo_schema");
+        TableNameSegment tableNameSegment = new TableNameSegment(0, 10, new IdentifierValue("tbl"));
+        tableNameSegment.setTableBoundInfo(new TableSegmentBoundInfo(new IdentifierValue("foo_db"), schemaIdentifier));
+        TablesContext tablesContext = new TablesContext(Collections.singleton(new SimpleTableSegment(tableNameSegment)));
+        assertThat(tablesContext.getIdentifierSchemaName(), is(Optional.of(schemaIdentifier)));
+    }
+    
+    @Test
+    void assertGetIdentifierSchemaNameWithoutSchema() {
+        TableNameSegment tableNameSegment = new TableNameSegment(0, 10, new IdentifierValue("tbl"));
+        tableNameSegment.setTableBoundInfo(new TableSegmentBoundInfo(new IdentifierValue("foo_db"), new IdentifierValue("")));
+        TablesContext tablesContext = new TablesContext(Collections.singleton(new SimpleTableSegment(tableNameSegment)));
+        assertThat(tablesContext.getIdentifierSchemaName(), is(Optional.empty()));
+    }
+    
     private SimpleTableSegment createTableSegment(final String tableName, final String alias, final String databaseName) {
         TableNameSegment tableNameSegment = new TableNameSegment(0, 0, new IdentifierValue(tableName));
         tableNameSegment.setTableBoundInfo(new TableSegmentBoundInfo(new IdentifierValue(databaseName), new IdentifierValue(databaseName)));
diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/util/SchemaRefreshUtils.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/util/SchemaRefreshUtils.java
index ef33a4c2ffd..34e94abe2d7 100644
--- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/util/SchemaRefreshUtils.java
+++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/refresher/util/SchemaRefreshUtils.java
@@ -48,7 +48,7 @@ public final class SchemaRefreshUtils {
      * @return schema name
      */
     public static String getSchemaName(final ShardingSphereDatabase database, final SQLStatementContext sqlStatementContext) {
-        return getRawSchemaName(database, sqlStatementContext).toLowerCase();
+        return getRawSchemaName(database, sqlStatementContext).getValue().toLowerCase();
     }
     
     /**
@@ -60,7 +60,7 @@ public final class SchemaRefreshUtils {
      * @return actual schema name
      */
     public static String getActualSchemaName(final ShardingSphereDatabase database, final SQLStatementContext sqlStatementContext, final ConfigurationProperties props) {
-        return getActualSchemaName(database, new IdentifierValue(getRawSchemaName(database, sqlStatementContext)), props);
+        return getActualSchemaName(database, getRawSchemaName(database, sqlStatementContext), props);
     }
     
     /**
@@ -99,8 +99,8 @@ public final class SchemaRefreshUtils {
         return result;
     }
     
-    private static String getRawSchemaName(final ShardingSphereDatabase database, final SQLStatementContext sqlStatementContext) {
-        return sqlStatementContext.getTablesContext().getSchemaName()
-                .orElseGet(() -> new DatabaseTypeRegistry(sqlStatementContext.getSqlStatement().getDatabaseType()).getDefaultSchemaName(database.getName()));
+    private static IdentifierValue getRawSchemaName(final ShardingSphereDatabase database, final SQLStatementContext sqlStatementContext) {
+        return sqlStatementContext.getTablesContext().getIdentifierSchemaName()
+                .orElseGet(() -> new IdentifierValue(new DatabaseTypeRegistry(sqlStatementContext.getSqlStatement().getDatabaseType()).getDefaultSchemaName(database.getName())));
     }
 }
diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/util/SchemaRefreshUtilsTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/util/SchemaRefreshUtilsTest.java
index d41bbec855d..b156550cad1 100644
--- a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/util/SchemaRefreshUtilsTest.java
+++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/util/SchemaRefreshUtilsTest.java
@@ -35,8 +35,11 @@ import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dml.De
 import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
 import org.junit.jupiter.api.Test;
 
+import java.util.Arrays;
 import java.util.Collections;
+import java.util.List;
 import java.util.Properties;
+import java.util.stream.Collectors;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
@@ -69,14 +72,39 @@ class SchemaRefreshUtilsTest {
         assertThat(SchemaRefreshUtils.getActualSchemaName(createDatabase(), new IdentifierValue("Foo_Schema"), new ConfigurationProperties(new Properties())), is("foo_schema"));
     }
     
+    @Test
+    void assertGetActualSchemaNameWithQuotedSchemaFromContext() {
+        Properties props = new Properties();
+        props.setProperty("metadata-identifier-case-sensitivity", "SENSITIVE");
+        assertThat(SchemaRefreshUtils.getActualSchemaName(createDatabaseWithSchema("Foo_Schema"), createSQLStatementContextWithSchema(new IdentifierValue("Foo_Schema", QuoteCharacter.QUOTE)),
+                new ConfigurationProperties(props)), is("Foo_Schema"));
+    }
+    
+    @Test
+    void assertGetActualSchemaNames() {
+        List<IdentifierValue> schemaIdentifiers = Arrays.asList(new IdentifierValue("Foo_Schema"), new IdentifierValue("bar_schema"), new IdentifierValue("new_schema"));
+        assertThat(SchemaRefreshUtils.getActualSchemaNames(createDatabaseWithSchema("foo_schema", "bar_schema"), schemaIdentifiers, new ConfigurationProperties(new Properties())),
+                is(Arrays.asList("foo_schema", "bar_schema", "new_schema")));
+    }
+    
     private ShardingSphereDatabase createDatabase() {
         return new ShardingSphereDatabase("FOO_DB", databaseType, new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), Collections.emptyList(),
                 new ConfigurationProperties(new Properties()));
     }
     
+    private ShardingSphereDatabase createDatabaseWithSchema(final String... schemaNames) {
+        List<ShardingSphereSchema> schemas = Arrays.stream(schemaNames).map(each -> new ShardingSphereSchema(each, databaseType)).collect(Collectors.toList());
+        return new ShardingSphereDatabase("foo_db", databaseType, new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), schemas,
+                new ConfigurationProperties(new Properties()));
+    }
+    
     private SQLStatementContext createSQLStatementContextWithSchema(final String schemaName) {
+        return createSQLStatementContextWithSchema(new IdentifierValue(schemaName));
+    }
+    
+    private SQLStatementContext createSQLStatementContextWithSchema(final IdentifierValue schemaIdentifier) {
         TableNameSegment tableNameSegment = new TableNameSegment(0, 0, new IdentifierValue("t_order"));
-        tableNameSegment.setTableBoundInfo(new TableSegmentBoundInfo(new IdentifierValue("foo_db"), new IdentifierValue(schemaName)));
+        tableNameSegment.setTableBoundInfo(new TableSegmentBoundInfo(new IdentifierValue("foo_db"), schemaIdentifier));
         TablesContext tablesContext = new TablesContext(new SimpleTableSegment(tableNameSegment));
         SQLStatement sqlStatement = DeleteStatement.builder().databaseType(databaseType).build();
         return new FixtureSQLStatementContext(sqlStatement, tablesContext);