You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/04/24 09:41:35 UTC

[shardingsphere] branch master updated: Optimize rewrite logic to support PostgreSQL schema (#17047)

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

zhaojinchao 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 56246064247 Optimize rewrite logic to support PostgreSQL schema (#17047)
56246064247 is described below

commit 562460642470989148d6b2847cdc2f2835c3b8db
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Sun Apr 24 17:41:22 2022 +0800

    Optimize rewrite logic to support PostgreSQL schema (#17047)
    
    * Optimize rewrite logic to support PostgreSQL schema
    
    * fix unit test
    
    * optimize logic
    
    * fix build error
    
    * fix unit test
    
    * fix unit test
    
    * fix integration test
---
 .../rewrite/condition/EncryptConditionEngine.java  | 15 ++++++-----
 .../context/EncryptSQLRewriteContextDecorator.java | 13 +++++-----
 .../parameter/EncryptParameterRewriterBuilder.java |  5 ++--
 .../EncryptOrderByItemTokenGenerator.java          |  6 ++++-
 .../EncryptPredicateColumnTokenGenerator.java      |  6 ++++-
 .../generator/EncryptProjectionTokenGenerator.java |  6 ++++-
 .../shardingsphere/encrypt/rule/EncryptRule.java   | 12 +++++----
 .../fixture/CustomizedEncryptAlgorithm.java        | 12 ++++-----
 .../impl/EncryptOrderByItemTokenGeneratorTest.java | 10 ++++++++
 .../impl/EncryptProjectionTokenGeneratorTest.java  | 10 ++++++++
 .../EncryptParameterRewriterBuilderTest.java       | 14 ++++++++---
 .../encrypt/rule/EncryptRuleTest.java              | 14 ++++++++---
 .../ShardingSQLRewriteContextDecorator.java        |  2 +-
 .../ShardingParameterRewriterBuilder.java          |  5 ++--
 .../token/generator/impl/IndexTokenGenerator.java  |  7 +++++-
 .../ShardingSQLRewriteContextDecoratorTest.java    |  2 +-
 .../ShardingParameterRewriterBuilderTest.java      | 12 +++++++--
 .../rewrite/token/IndexTokenGeneratorTest.java     | 29 ++++++++++++++++------
 .../infra/database/type/DatabaseType.java          | 10 ++++++++
 .../type/dialect/OpenGaussDatabaseType.java        |  5 ++++
 .../type/dialect/PostgreSQLDatabaseType.java       |  5 ++++
 .../infra/context/kernel/KernelProcessor.java      |  2 +-
 .../infra/rewrite/SQLRewriteEntry.java             | 12 ++++-----
 .../infra/rewrite/context/SQLRewriteContext.java   | 14 ++++++-----
 .../sql/token/generator/SQLTokenGenerators.java    | 14 +++++++----
 .../token/generator/aware/SchemaMetaDataAware.java | 13 ++++++++--
 .../generator/generic/RemoveTokenGenerator.java    | 10 +++-----
 .../infra/rewrite/SQLRewriteEntryTest.java         | 12 +++++++--
 .../rewrite/context/SQLRewriteContextTest.java     | 19 ++++++++------
 .../engine/GenericSQLRewriteEngineTest.java        | 10 +++++++-
 .../rewrite/engine/RouteSQLRewriteEngineTest.java  | 20 ++++++++++-----
 .../token/generator/SQLTokenGeneratorsTest.java    | 11 ++++++--
 .../postgresql/preview_federation_select.xml       | 22 ++++++++--------
 .../SchemaBasedEncryptAlgorithmFixture.java        | 10 +++++---
 .../AbstractSQLRewriterParameterizedTest.java      |  2 +-
 .../resources/scenario/sharding/case/delete.xml    |  4 +--
 .../resources/scenario/sharding/case/select.xml    |  8 +++---
 37 files changed, 267 insertions(+), 116 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptConditionEngine.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptConditionEngine.java
index 30baebfeac7..a269cc9aed1 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptConditionEngine.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/condition/EncryptConditionEngine.java
@@ -22,7 +22,7 @@ import org.apache.shardingsphere.encrypt.rewrite.condition.impl.EncryptEqualCond
 import org.apache.shardingsphere.encrypt.rewrite.condition.impl.EncryptInCondition;
 import org.apache.shardingsphere.encrypt.rule.EncryptColumn;
 import org.apache.shardingsphere.encrypt.rule.EncryptRule;
-import org.apache.shardingsphere.infra.binder.segment.table.TablesContext;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
 import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
@@ -58,7 +58,7 @@ public final class EncryptConditionEngine {
     
     private final EncryptRule encryptRule;
     
-    private final ShardingSphereSchema schema;
+    private final Map<String, ShardingSphereSchema> schemas;
     
     static {
         LOGICAL_OPERATOR.add("AND");
@@ -79,13 +79,16 @@ public final class EncryptConditionEngine {
      *
      * @param whereSegments where segments
      * @param columnSegments column segments
-     * @param tablesContext tables context
+     * @param sqlStatementContext sql statement context
+     * @param databaseName database name
      * @return encrypt conditions
      */
-    public Collection<EncryptCondition> createEncryptConditions(final Collection<WhereSegment> whereSegments,
-                                                                final Collection<ColumnSegment> columnSegments, final TablesContext tablesContext) {
+    public Collection<EncryptCondition> createEncryptConditions(final Collection<WhereSegment> whereSegments, final Collection<ColumnSegment> columnSegments,
+                                                                final SQLStatementContext<?> sqlStatementContext, final String databaseName) {
         Collection<EncryptCondition> result = new LinkedList<>();
-        Map<String, String> expressionTableNames = tablesContext.findTableNamesByColumnSegment(columnSegments, schema);
+        String defaultSchema = sqlStatementContext.getDatabaseType().getDefaultSchema(databaseName);
+        ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName().map(schemas::get).orElse(schemas.get(defaultSchema));
+        Map<String, String> expressionTableNames = sqlStatementContext.getTablesContext().findTableNamesByColumnSegment(columnSegments, schema);
         for (WhereSegment each : whereSegments) {
             Collection<AndPredicate> andPredicates = ExpressionExtractUtil.getAndPredicates(each.getExpr());
             for (AndPredicate predicate : andPredicates) {
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/context/EncryptSQLRewriteContextDecorator.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/context/EncryptSQLRewriteContextDecorator.java
index 6b64bf9710a..c40eb8153b1 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/context/EncryptSQLRewriteContextDecorator.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/context/EncryptSQLRewriteContextDecorator.java
@@ -51,26 +51,27 @@ public final class EncryptSQLRewriteContextDecorator implements SQLRewriteContex
                 || !containsEncryptTable(encryptRule, sqlStatementContext)) {
             return;
         }
-        Collection<EncryptCondition> encryptConditions = getEncryptConditions(encryptRule, sqlRewriteContext);
-        encryptRule.setUpEncryptorSchema(sqlRewriteContext.getSchema());
+        Collection<EncryptCondition> encryptConditions = createEncryptConditions(encryptRule, sqlRewriteContext);
+        encryptRule.setUpEncryptorSchema(sqlRewriteContext.getSchemas(), sqlRewriteContext.getDatabaseName());
         if (!sqlRewriteContext.getParameters().isEmpty()) {
             Collection<ParameterRewriter> parameterRewriters = new EncryptParameterRewriterBuilder(encryptRule,
-                    sqlRewriteContext.getSchemaName(), sqlRewriteContext.getSchema(), sqlStatementContext, encryptConditions).getParameterRewriters();
+                    sqlRewriteContext.getDatabaseName(), sqlRewriteContext.getSchemas(), sqlStatementContext, encryptConditions).getParameterRewriters();
             rewriteParameters(sqlRewriteContext, parameterRewriters);
         }
         Collection<SQLTokenGenerator> sqlTokenGenerators = new EncryptTokenGenerateBuilder(encryptRule,
-                sqlStatementContext, encryptConditions, sqlRewriteContext.getSchemaName()).getSQLTokenGenerators();
+                sqlStatementContext, encryptConditions, sqlRewriteContext.getDatabaseName()).getSQLTokenGenerators();
         sqlRewriteContext.addSQLTokenGenerators(sqlTokenGenerators);
     }
     
-    private Collection<EncryptCondition> getEncryptConditions(final EncryptRule encryptRule, final SQLRewriteContext sqlRewriteContext) {
+    private Collection<EncryptCondition> createEncryptConditions(final EncryptRule encryptRule, final SQLRewriteContext sqlRewriteContext) {
         SQLStatementContext<?> sqlStatementContext = sqlRewriteContext.getSqlStatementContext();
         if (!(sqlStatementContext instanceof WhereAvailable)) {
             return Collections.emptyList();
         }
         Collection<WhereSegment> whereSegments = ((WhereAvailable) sqlStatementContext).getWhereSegments();
         Collection<ColumnSegment> columnSegments = ((WhereAvailable) sqlStatementContext).getColumnSegments();
-        return new EncryptConditionEngine(encryptRule, sqlRewriteContext.getSchema()).createEncryptConditions(whereSegments, columnSegments, sqlStatementContext.getTablesContext());
+        return new EncryptConditionEngine(encryptRule, sqlRewriteContext.getSchemas())
+                .createEncryptConditions(whereSegments, columnSegments, sqlStatementContext, sqlRewriteContext.getDatabaseName());
     }
     
     @SuppressWarnings("rawtypes")
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/EncryptParameterRewriterBuilder.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/EncryptParameterRewriterBuilder.java
index f6904a270d8..51666689bf0 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/EncryptParameterRewriterBuilder.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/EncryptParameterRewriterBuilder.java
@@ -35,6 +35,7 @@ import org.apache.shardingsphere.infra.rewrite.sql.token.generator.aware.SchemaM
 
 import java.util.Collection;
 import java.util.LinkedList;
+import java.util.Map;
 
 /**
  * Parameter rewriter builder for encrypt.
@@ -46,7 +47,7 @@ public final class EncryptParameterRewriterBuilder implements ParameterRewriterB
     
     private final String schemaName;
     
-    private final ShardingSphereSchema schema;
+    private final Map<String, ShardingSphereSchema> schemas;
     
     private final SQLStatementContext<?> sqlStatementContext;
     
@@ -73,7 +74,7 @@ public final class EncryptParameterRewriterBuilder implements ParameterRewriterB
     @SuppressWarnings("rawtypes")
     private void setUpParameterRewriter(final ParameterRewriter toBeAddedParameterRewriter) {
         if (toBeAddedParameterRewriter instanceof SchemaMetaDataAware) {
-            ((SchemaMetaDataAware) toBeAddedParameterRewriter).setSchema(schema);
+            ((SchemaMetaDataAware) toBeAddedParameterRewriter).setSchemas(schemas);
         }
         if (toBeAddedParameterRewriter instanceof EncryptRuleAware) {
             ((EncryptRuleAware) toBeAddedParameterRewriter).setEncryptRule(encryptRule);
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptOrderByItemTokenGenerator.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptOrderByItemTokenGenerator.java
index b87e7bf058e..406e6973923 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptOrderByItemTokenGenerator.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptOrderByItemTokenGenerator.java
@@ -45,7 +45,9 @@ import java.util.Optional;
 @Setter
 public final class EncryptOrderByItemTokenGenerator implements CollectionSQLTokenGenerator, SchemaMetaDataAware, EncryptRuleAware {
     
-    private ShardingSphereSchema schema;
+    private Map<String, ShardingSphereSchema> schemas;
+    
+    private String databaseName;
     
     private EncryptRule encryptRule;
     
@@ -59,6 +61,8 @@ public final class EncryptOrderByItemTokenGenerator implements CollectionSQLToke
     @Override
     public Collection<SubstitutableColumnNameToken> generateSQLTokens(final SQLStatementContext sqlStatementContext) {
         Collection<SubstitutableColumnNameToken> result = new LinkedHashSet<>();
+        String defaultSchema = sqlStatementContext.getDatabaseType().getDefaultSchema(databaseName);
+        ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName().map(schemas::get).orElse(schemas.get(defaultSchema));
         for (OrderByItem each : getOrderByItems(sqlStatementContext)) {
             if (each.getSegment() instanceof ColumnOrderByItemSegment) {
                 ColumnSegment columnSegment = ((ColumnOrderByItemSegment) each.getSegment()).getColumn();
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateColumnTokenGenerator.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateColumnTokenGenerator.java
index 552c509c951..07857279aba 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateColumnTokenGenerator.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptPredicateColumnTokenGenerator.java
@@ -42,7 +42,9 @@ import java.util.Optional;
 @Setter
 public final class EncryptPredicateColumnTokenGenerator implements CollectionSQLTokenGenerator, SchemaMetaDataAware, EncryptRuleAware {
     
-    private ShardingSphereSchema schema;
+    private Map<String, ShardingSphereSchema> schemas;
+    
+    private String databaseName;
     
     private EncryptRule encryptRule;
     
@@ -56,6 +58,8 @@ public final class EncryptPredicateColumnTokenGenerator implements CollectionSQL
     @Override
     public Collection<SubstitutableColumnNameToken> generateSQLTokens(final SQLStatementContext sqlStatementContext) {
         Collection<ColumnSegment> columnSegments = sqlStatementContext instanceof WhereAvailable ? ((WhereAvailable) sqlStatementContext).getColumnSegments() : Collections.emptyList();
+        String defaultSchema = sqlStatementContext.getDatabaseType().getDefaultSchema(databaseName);
+        ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName().map(schemas::get).orElse(schemas.get(defaultSchema));
         Map<String, String> columnExpressionTableNames = sqlStatementContext.getTablesContext().findTableNamesByColumnSegment(columnSegments, schema);
         return generateSQLTokens(columnSegments, columnExpressionTableNames);
     }
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptProjectionTokenGenerator.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptProjectionTokenGenerator.java
index 0412493f184..8e3cfe32f9e 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptProjectionTokenGenerator.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptProjectionTokenGenerator.java
@@ -54,7 +54,9 @@ public final class EncryptProjectionTokenGenerator implements CollectionSQLToken
     
     private List<SQLToken> previousSQLTokens;
     
-    private ShardingSphereSchema schema;
+    private Map<String, ShardingSphereSchema> schemas;
+    
+    private String databaseName;
     
     private EncryptRule encryptRule;
     
@@ -136,6 +138,8 @@ public final class EncryptProjectionTokenGenerator implements CollectionSQLToken
                 columns.addAll(((ShorthandProjection) projection).getActualColumns().values());
             }
         }
+        String defaultSchema = selectStatementContext.getDatabaseType().getDefaultSchema(databaseName);
+        ShardingSphereSchema schema = selectStatementContext.getTablesContext().getSchemaName().map(schemas::get).orElse(schemas.get(defaultSchema));
         return selectStatementContext.getTablesContext().findTableNamesByColumnProjection(columns, schema);
     }
     
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
index 5706fbbca68..0f602522ebb 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
@@ -20,12 +20,12 @@ package org.apache.shardingsphere.encrypt.rule;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import lombok.Getter;
-import org.apache.shardingsphere.encrypt.factory.EncryptAlgorithmFactory;
 import org.apache.shardingsphere.encrypt.algorithm.config.AlgorithmProvidedEncryptRuleConfiguration;
 import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
 import org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
 import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
 import org.apache.shardingsphere.encrypt.context.EncryptContextBuilder;
+import org.apache.shardingsphere.encrypt.factory.EncryptAlgorithmFactory;
 import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
 import org.apache.shardingsphere.encrypt.spi.QueryAssistedEncryptAlgorithm;
 import org.apache.shardingsphere.encrypt.spi.context.EncryptContext;
@@ -313,13 +313,15 @@ public final class EncryptRule implements SchemaRule, TableContainedRule {
     
     /**
      * Set up encryptor schema.
-     * 
-     * @param schema schema
+     *
+     * @param schemas schema map
+     * @param databaseName database name
      */
-    public void setUpEncryptorSchema(final ShardingSphereSchema schema) {
+    public void setUpEncryptorSchema(final Map<String, ShardingSphereSchema> schemas, final String databaseName) {
         for (EncryptAlgorithm<?, ?> each : encryptors.values()) {
             if (each instanceof SchemaMetaDataAware) {
-                ((SchemaMetaDataAware) each).setSchema(schema);
+                ((SchemaMetaDataAware) each).setSchemas(schemas);
+                ((SchemaMetaDataAware) each).setDatabaseName(databaseName);
             }
         }
     }
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/fixture/CustomizedEncryptAlgorithm.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/fixture/CustomizedEncryptAlgorithm.java
index caa9a159544..996584f5095 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/fixture/CustomizedEncryptAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/fixture/CustomizedEncryptAlgorithm.java
@@ -20,11 +20,12 @@ package org.apache.shardingsphere.encrypt.fixture;
 import lombok.Getter;
 import lombok.Setter;
 import org.apache.commons.codec.digest.DigestUtils;
-import org.apache.shardingsphere.encrypt.spi.context.EncryptContext;
 import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
+import org.apache.shardingsphere.encrypt.spi.context.EncryptContext;
 import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.rewrite.sql.token.generator.aware.SchemaMetaDataAware;
 
+import java.util.Map;
 import java.util.Properties;
 
 /**
@@ -40,7 +41,9 @@ public final class CustomizedEncryptAlgorithm implements EncryptAlgorithm<Intege
     
     private byte[] key = DigestUtils.sha256(TEST_KEY);
     
-    private ShardingSphereSchema schema;
+    private Map<String, ShardingSphereSchema> schemas;
+    
+    private String databaseName;
     
     @Override
     public void init() {
@@ -86,9 +89,4 @@ public final class CustomizedEncryptAlgorithm implements EncryptAlgorithm<Intege
         result[3] = (byte) intValue;
         return result;
     }
-    
-    @Override
-    public void setSchema(final ShardingSphereSchema schema) {
-        this.schema = schema;
-    }
 }
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptOrderByItemTokenGeneratorTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptOrderByItemTokenGeneratorTest.java
index b2ff90d34f7..a8c59cfb218 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptOrderByItemTokenGeneratorTest.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptOrderByItemTokenGeneratorTest.java
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem
 import org.apache.shardingsphere.infra.binder.segment.table.TablesContext;
 import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
+import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.generic.SubstitutableColumnNameToken;
 import org.apache.shardingsphere.sql.parser.sql.common.constant.OrderDirection;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
@@ -39,6 +40,8 @@ import org.junit.Test;
 
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Optional;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -55,6 +58,7 @@ public final class EncryptOrderByItemTokenGeneratorTest {
     public void setup() {
         generator = new EncryptOrderByItemTokenGenerator();
         generator.setEncryptRule(buildEncryptRule());
+        generator.setSchemas(mockSchemaMap());
     }
     
     @Test
@@ -89,4 +93,10 @@ public final class EncryptOrderByItemTokenGeneratorTest {
         when(result.findEncryptTable("t_encrypt")).thenReturn(Optional.of(encryptTable));
         return result;
     }
+    
+    private Map<String, ShardingSphereSchema> mockSchemaMap() {
+        Map<String, ShardingSphereSchema> result = new HashMap<>(1, 1);
+        result.put("test", mock(ShardingSphereSchema.class));
+        return result;
+    }
 }
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptProjectionTokenGeneratorTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptProjectionTokenGeneratorTest.java
index edd84060549..4fbc370f3ad 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptProjectionTokenGeneratorTest.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptProjectionTokenGeneratorTest.java
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.Col
 import org.apache.shardingsphere.infra.binder.segment.table.TablesContext;
 import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry;
+import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.generic.SubstitutableColumnNameToken;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ColumnProjectionSegment;
@@ -40,6 +41,8 @@ import org.junit.Test;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Optional;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -56,6 +59,7 @@ public final class EncryptProjectionTokenGeneratorTest {
     public void setup() {
         generator = new EncryptProjectionTokenGenerator();
         generator.setEncryptRule(buildEncryptRule());
+        generator.setSchemas(mockSchemaMap());
     }
     
     @Test
@@ -125,4 +129,10 @@ public final class EncryptProjectionTokenGeneratorTest {
         when(encryptRule.findEncryptColumn("doctor", "mobile")).thenReturn(Optional.of(column));
         return encryptRule;
     }
+    
+    private Map<String, ShardingSphereSchema> mockSchemaMap() {
+        Map<String, ShardingSphereSchema> result = new HashMap<>(1, 1);
+        result.put("test", mock(ShardingSphereSchema.class));
+        return result;
+    }
 }
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameter/EncryptParameterRewriterBuilderTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameter/EncryptParameterRewriterBuilderTest.java
index 529ec4e8f62..4ec66c35ecb 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameter/EncryptParameterRewriterBuilderTest.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameter/EncryptParameterRewriterBuilderTest.java
@@ -28,6 +28,8 @@ import org.junit.Test;
 
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
@@ -44,11 +46,10 @@ public final class EncryptParameterRewriterBuilderTest {
         EncryptRule encryptRule = mock(EncryptRule.class, RETURNS_DEEP_STUBS);
         when(encryptRule.isQueryWithCipherColumn()).thenReturn(true);
         when(encryptRule.findEncryptTable("t_order").isPresent()).thenReturn(true);
-        ShardingSphereSchema shardingSphereSchema = mock(ShardingSphereSchema.class);
         SQLStatementContext<?> sqlStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
         when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singletonList("t_order"));
         Collection<ParameterRewriter> actual = new EncryptParameterRewriterBuilder(
-                encryptRule, DefaultSchema.LOGIC_NAME, shardingSphereSchema, sqlStatementContext, Collections.emptyList()).getParameterRewriters();
+                encryptRule, DefaultSchema.LOGIC_NAME, mockSchemaMap(), sqlStatementContext, Collections.emptyList()).getParameterRewriters();
         assertThat(actual.size(), is(1));
         ParameterRewriter parameterRewriter = actual.iterator().next();
         assertThat(parameterRewriter, instanceOf(EncryptPredicateParameterRewriter.class));
@@ -59,12 +60,17 @@ public final class EncryptParameterRewriterBuilderTest {
     public void assertGetParameterRewritersWhenPredicateIsNotNeedRewrite() {
         EncryptRule encryptRule = mock(EncryptRule.class, RETURNS_DEEP_STUBS);
         when(encryptRule.isQueryWithCipherColumn()).thenReturn(true);
-        ShardingSphereSchema shardingSphereSchema = mock(ShardingSphereSchema.class);
         SelectStatementContext sqlStatementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
         when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singletonList("t_order"));
         when(sqlStatementContext.getWhereSegments()).thenReturn(Collections.emptyList());
         Collection<ParameterRewriter> actual = new EncryptParameterRewriterBuilder(
-                encryptRule, DefaultSchema.LOGIC_NAME, shardingSphereSchema, sqlStatementContext, Collections.emptyList()).getParameterRewriters();
+                encryptRule, DefaultSchema.LOGIC_NAME, mockSchemaMap(), sqlStatementContext, Collections.emptyList()).getParameterRewriters();
         assertThat(actual.size(), is(0));
     }
+    
+    private Map<String, ShardingSphereSchema> mockSchemaMap() {
+        Map<String, ShardingSphereSchema> result = new HashMap<>(1, 1);
+        result.put("test", mock(ShardingSphereSchema.class));
+        return result;
+    }
 }
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java
index d2463e38a4f..dea1ecf0a0f 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java
@@ -32,14 +32,15 @@ import org.junit.Test;
 
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Optional;
 import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
@@ -164,11 +165,18 @@ public final class EncryptRuleTest {
     @Test
     public void assertSetUpEncryptorSchema() {
         EncryptRule encryptRule = new EncryptRule(createEncryptRuleConfiguration(), Collections.emptyMap());
-        encryptRule.setUpEncryptorSchema(mock(ShardingSphereSchema.class));
+        Map<String, ShardingSphereSchema> schemas = mockSchemaMap();
+        encryptRule.setUpEncryptorSchema(schemas, "test");
         Optional<EncryptAlgorithm> actual = encryptRule.findEncryptor("t_encrypt", "name");
         assertTrue(actual.isPresent());
         assertThat(actual.get(), instanceOf(CustomizedEncryptAlgorithm.class));
-        assertNotNull(((CustomizedEncryptAlgorithm) actual.get()).getSchema());
+        assertFalse(((CustomizedEncryptAlgorithm) actual.get()).getSchemas().isEmpty());
+    }
+    
+    private Map<String, ShardingSphereSchema> mockSchemaMap() {
+        Map<String, ShardingSphereSchema> result = new HashMap<>(1, 1);
+        result.put("test", mock(ShardingSphereSchema.class));
+        return result;
     }
     
     private EncryptRuleConfiguration createEncryptRuleConfiguration() {
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java
index f79769ccfb7..aeb320ad8aa 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecorator.java
@@ -44,7 +44,7 @@ public final class ShardingSQLRewriteContextDecorator implements SQLRewriteConte
         }
         if (!sqlRewriteContext.getParameters().isEmpty()) {
             Collection<ParameterRewriter> parameterRewriters = new ShardingParameterRewriterBuilder(shardingRule,
-                    routeContext, sqlRewriteContext.getSchema(), sqlRewriteContext.getSqlStatementContext()).getParameterRewriters();
+                    routeContext, sqlRewriteContext.getSchemas(), sqlRewriteContext.getSqlStatementContext()).getParameterRewriters();
             rewriteParameters(sqlRewriteContext, parameterRewriters);
         }
         sqlRewriteContext.addSQLTokenGenerators(new ShardingTokenGenerateBuilder(shardingRule, routeContext, sqlRewriteContext.getSqlStatementContext()).getSQLTokenGenerators());
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingParameterRewriterBuilder.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingParameterRewriterBuilder.java
index 6483745eb07..3a9f453c5a1 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingParameterRewriterBuilder.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingParameterRewriterBuilder.java
@@ -32,6 +32,7 @@ import org.apache.shardingsphere.sharding.rule.aware.ShardingRuleAware;
 
 import java.util.Collection;
 import java.util.LinkedList;
+import java.util.Map;
 
 /**
  * Parameter rewriter builder for sharding.
@@ -43,7 +44,7 @@ public final class ShardingParameterRewriterBuilder implements ParameterRewriter
     
     private final RouteContext routeContext;
     
-    private final ShardingSphereSchema schema;
+    private final Map<String, ShardingSphereSchema> schemas;
     
     private final SQLStatementContext<?> sqlStatementContext;
     
@@ -59,7 +60,7 @@ public final class ShardingParameterRewriterBuilder implements ParameterRewriter
     @SuppressWarnings("rawtypes")
     private void addParameterRewriter(final Collection<ParameterRewriter> parameterRewriters, final ParameterRewriter toBeAddedParameterRewriter) {
         if (toBeAddedParameterRewriter instanceof SchemaMetaDataAware) {
-            ((SchemaMetaDataAware) toBeAddedParameterRewriter).setSchema(schema);
+            ((SchemaMetaDataAware) toBeAddedParameterRewriter).setSchemas(schemas);
         }
         if (toBeAddedParameterRewriter instanceof ShardingRuleAware) {
             ((ShardingRuleAware) toBeAddedParameterRewriter).setShardingRule(shardingRule);
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/IndexTokenGenerator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/IndexTokenGenerator.java
index f34d6022ab8..78f8568c2c8 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/IndexTokenGenerator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/IndexTokenGenerator.java
@@ -30,6 +30,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSe
 
 import java.util.Collection;
 import java.util.LinkedList;
+import java.util.Map;
 
 /**
  * Index token generator.
@@ -39,7 +40,9 @@ public final class IndexTokenGenerator implements CollectionSQLTokenGenerator<SQ
     
     private ShardingRule shardingRule;
     
-    private ShardingSphereSchema schema;
+    private Map<String, ShardingSphereSchema> schemas;
+    
+    private String databaseName;
     
     @Override
     public boolean isGenerateSQLToken(final SQLStatementContext<?> sqlStatementContext) {
@@ -49,6 +52,8 @@ public final class IndexTokenGenerator implements CollectionSQLTokenGenerator<SQ
     @Override
     public Collection<IndexToken> generateSQLTokens(final SQLStatementContext<?> sqlStatementContext) {
         Collection<IndexToken> result = new LinkedList<>();
+        String defaultSchema = sqlStatementContext.getDatabaseType().getDefaultSchema(databaseName);
+        ShardingSphereSchema schema = sqlStatementContext.getTablesContext().getSchemaName().map(schemas::get).orElse(schemas.get(defaultSchema));
         if (sqlStatementContext instanceof IndexAvailable) {
             for (IndexSegment each : ((IndexAvailable) sqlStatementContext).getIndexes()) {
                 result.add(new IndexToken(each.getStartIndex(), each.getStopIndex(), each.getIdentifier(), sqlStatementContext, shardingRule, schema));
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
index 5b95171a6c7..a2c53c53286 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/context/ShardingSQLRewriteContextDecoratorTest.java
@@ -45,7 +45,7 @@ public final class ShardingSQLRewriteContextDecoratorTest {
         RouteContext routeContext = mock(RouteContext.class);
         when(routeContext.isFederated()).thenReturn(true);
         shardingSQLRewriteContextDecorator.decorate(shardingRule, configurationProperties, sqlRewriteContext, routeContext);
-        assertTrue(Objects.isNull(sqlRewriteContext.getSchema()));
+        assertTrue(sqlRewriteContext.getSchemas().isEmpty());
         assertTrue(Objects.isNull(sqlRewriteContext.getSqlStatementContext()));
         assertThat(sqlRewriteContext.getParameters().size(), is(0));
         assertTrue(Objects.isNull(sqlRewriteContext.getParameterBuilder()));
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingParameterRewriterBuilderTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingParameterRewriterBuilderTest.java
index ebd6129205d..b839086ded0 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingParameterRewriterBuilderTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameter/ShardingParameterRewriterBuilderTest.java
@@ -26,6 +26,8 @@ import org.apache.shardingsphere.sharding.rule.ShardingRule;
 import org.junit.Test;
 
 import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
@@ -44,7 +46,7 @@ public final class ShardingParameterRewriterBuilderTest {
         when(routeContext.isSingleRouting()).thenReturn(false);
         SelectStatementContext statementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
         when(statementContext.getPaginationContext().isHasPagination()).thenReturn(true);
-        ShardingParameterRewriterBuilder shardingParameterRewriterBuilder = new ShardingParameterRewriterBuilder(shardingRule, routeContext, mock(ShardingSphereSchema.class), statementContext);
+        ShardingParameterRewriterBuilder shardingParameterRewriterBuilder = new ShardingParameterRewriterBuilder(shardingRule, routeContext, mockSchemaMap(), statementContext);
         Collection<ParameterRewriter> actual = shardingParameterRewriterBuilder.getParameterRewriters();
         assertThat(actual.size(), is(1));
         ParameterRewriter parameterRewriter = actual.iterator().next();
@@ -59,8 +61,14 @@ public final class ShardingParameterRewriterBuilderTest {
         when(routeContext.isSingleRouting()).thenReturn(true);
         SelectStatementContext statementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
         when(statementContext.getPaginationContext().isHasPagination()).thenReturn(true);
-        ShardingParameterRewriterBuilder shardingParameterRewriterBuilder = new ShardingParameterRewriterBuilder(shardingRule, routeContext, mock(ShardingSphereSchema.class), statementContext);
+        ShardingParameterRewriterBuilder shardingParameterRewriterBuilder = new ShardingParameterRewriterBuilder(shardingRule, routeContext, mockSchemaMap(), statementContext);
         Collection<ParameterRewriter> actual = shardingParameterRewriterBuilder.getParameterRewriters();
         assertThat(actual.size(), is(0));
     }
+    
+    private Map<String, ShardingSphereSchema> mockSchemaMap() {
+        Map<String, ShardingSphereSchema> result = new HashMap<>(1, 1);
+        result.put("test", mock(ShardingSphereSchema.class));
+        return result;
+    }
 }
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/IndexTokenGeneratorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/IndexTokenGeneratorTest.java
index 3c242a5fc19..bdf14b6c2ba 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/IndexTokenGeneratorTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/IndexTokenGeneratorTest.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.sharding.rewrite.token;
 
 import org.apache.shardingsphere.infra.binder.statement.ddl.AlterIndexStatementContext;
 import org.apache.shardingsphere.infra.binder.statement.ddl.CreateDatabaseStatementContext;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
 import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import org.apache.shardingsphere.sharding.rewrite.token.generator.impl.IndexTokenGenerator;
 import org.apache.shardingsphere.sharding.rewrite.token.pojo.IndexToken;
@@ -28,12 +29,16 @@ import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.Identifi
 import org.junit.Test;
 
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.LinkedList;
+import java.util.Map;
+import java.util.Optional;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -55,23 +60,31 @@ public final class IndexTokenGeneratorTest {
     @Test
     public void assertGenerateSQLTokens() {
         IndexSegment indexSegment = mock(IndexSegment.class);
-        final int testStartIndex = 1;
+        int testStartIndex = 1;
         when(indexSegment.getStartIndex()).thenReturn(testStartIndex);
-        final int testStopIndex = 3;
+        int testStopIndex = 3;
         when(indexSegment.getStopIndex()).thenReturn(testStopIndex);
         IdentifierValue identifierValue = mock(IdentifierValue.class);
         when(indexSegment.getIdentifier()).thenReturn(identifierValue);
-        Collection<IndexSegment> indexSegmentCollection = new LinkedList<>();
-        indexSegmentCollection.add(indexSegment);
-        AlterIndexStatementContext alterIndexStatementContext = mock(AlterIndexStatementContext.class);
-        when(alterIndexStatementContext.getIndexes()).thenReturn(indexSegmentCollection);
+        Collection<IndexSegment> indexSegments = new LinkedList<>();
+        indexSegments.add(indexSegment);
+        AlterIndexStatementContext alterIndexStatementContext = mock(AlterIndexStatementContext.class, RETURNS_DEEP_STUBS);
+        when(alterIndexStatementContext.getIndexes()).thenReturn(indexSegments);
+        when(alterIndexStatementContext.getDatabaseType()).thenReturn(new MySQLDatabaseType());
+        when(alterIndexStatementContext.getTablesContext().getSchemaName()).thenReturn(Optional.empty());
         IndexTokenGenerator indexTokenGenerator = new IndexTokenGenerator();
         ShardingRule shardingRule = mock(ShardingRule.class);
         indexTokenGenerator.setShardingRule(shardingRule);
-        ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
-        indexTokenGenerator.setSchema(schema);
+        indexTokenGenerator.setSchemas(mockSchemaMap());
+        indexTokenGenerator.setDatabaseName("test");
         Collection<IndexToken> result = indexTokenGenerator.generateSQLTokens(alterIndexStatementContext);
         assertThat(result.size(), is(1));
         assertThat((new LinkedList<>(result)).get(0).getStartIndex(), is(testStartIndex));
     }
+    
+    private Map<String, ShardingSphereSchema> mockSchemaMap() {
+        Map<String, ShardingSphereSchema> result = new HashMap<>(1, 1);
+        result.put("test", mock(ShardingSphereSchema.class));
+        return result;
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseType.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseType.java
index f7219c4b4ac..04a46390dac 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseType.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/DatabaseType.java
@@ -83,6 +83,16 @@ public interface DatabaseType {
      */
     Collection<String> getSystemSchemas();
     
+    /**
+     * Get default schema.
+     *
+     * @param databaseName database name
+     * @return default schema
+     */
+    default String getDefaultSchema(String databaseName) {
+        return databaseName;
+    }
+    
     /**
      * Get schema.
      *
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/OpenGaussDatabaseType.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/OpenGaussDatabaseType.java
index d12b47b2f86..a96d2880935 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/OpenGaussDatabaseType.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/OpenGaussDatabaseType.java
@@ -77,4 +77,9 @@ public final class OpenGaussDatabaseType implements DatabaseType {
     public Collection<String> getSystemSchemas() {
         return Collections.emptyList();
     }
+    
+    @Override
+    public String getDefaultSchema(final String databaseName) {
+        return "public";
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/PostgreSQLDatabaseType.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/PostgreSQLDatabaseType.java
index a0350c60e3d..f0f8a46866a 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/PostgreSQLDatabaseType.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/database/type/dialect/PostgreSQLDatabaseType.java
@@ -87,4 +87,9 @@ public final class PostgreSQLDatabaseType implements DatabaseType {
     public Collection<String> getSystemSchemas() {
         return SYSTEM_SCHEMAS;
     }
+    
+    @Override
+    public String getDefaultSchema(final String databaseName) {
+        return "public";
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java
index bf66c247fce..fcdc7861163 100644
--- a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java
+++ b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java
@@ -55,7 +55,7 @@ public final class KernelProcessor {
     }
     
     private SQLRewriteResult rewrite(final LogicSQL logicSQL, final ShardingSphereMetaData metaData, final ConfigurationProperties props, final RouteContext routeContext) {
-        SQLRewriteEntry sqlRewriteEntry = new SQLRewriteEntry(metaData.getName(), metaData.getDefaultSchema(), props, metaData.getRuleMetaData().getRules());
+        SQLRewriteEntry sqlRewriteEntry = new SQLRewriteEntry(metaData.getName(), metaData.getSchemas(), props, metaData.getRuleMetaData().getRules());
         return sqlRewriteEntry.rewrite(logicSQL.getSql(), logicSQL.getParameters(), logicSQL.getSqlStatementContext(), routeContext);
     }
     
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java b/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java
index d2fe5a26e7f..fec576d6ea7 100644
--- a/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java
@@ -44,18 +44,18 @@ public final class SQLRewriteEntry {
         ShardingSphereServiceLoader.register(SQLRewriteContextDecorator.class);
     }
     
-    private final String schemaName;
+    private final String databaseName;
     
-    private final ShardingSphereSchema schema;
+    private final Map<String, ShardingSphereSchema> schemas;
     
     private final ConfigurationProperties props;
     
     @SuppressWarnings("rawtypes")
     private final Map<ShardingSphereRule, SQLRewriteContextDecorator> decorators;
     
-    public SQLRewriteEntry(final String schemaName, final ShardingSphereSchema schema, final ConfigurationProperties props, final Collection<ShardingSphereRule> rules) {
-        this.schemaName = schemaName;
-        this.schema = schema;
+    public SQLRewriteEntry(final String databaseName, final Map<String, ShardingSphereSchema> schemas, final ConfigurationProperties props, final Collection<ShardingSphereRule> rules) {
+        this.databaseName = databaseName;
+        this.schemas = schemas;
         this.props = props;
         decorators = OrderedSPIRegistry.getRegisteredServices(SQLRewriteContextDecorator.class, rules);
     }
@@ -77,7 +77,7 @@ public final class SQLRewriteEntry {
     }
     
     private SQLRewriteContext createSQLRewriteContext(final String sql, final List<Object> parameters, final SQLStatementContext<?> sqlStatementContext, final RouteContext routeContext) {
-        SQLRewriteContext result = new SQLRewriteContext(schemaName, schema, sqlStatementContext, sql, parameters);
+        SQLRewriteContext result = new SQLRewriteContext(databaseName, schemas, sqlStatementContext, sql, parameters);
         decorate(decorators, result, routeContext);
         result.generateSQLTokens();
         return result;
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/context/SQLRewriteContext.java b/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/context/SQLRewriteContext.java
index bd0b0eaf6e9..a6a1246da6a 100644
--- a/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/context/SQLRewriteContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/context/SQLRewriteContext.java
@@ -33,6 +33,7 @@ import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken;
 import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
 
 /**
  * SQL rewrite context.
@@ -40,9 +41,9 @@ import java.util.List;
 @Getter
 public final class SQLRewriteContext {
     
-    private final String schemaName;
+    private final String databaseName;
     
-    private final ShardingSphereSchema schema;
+    private final Map<String, ShardingSphereSchema> schemas;
     
     private final SQLStatementContext<?> sqlStatementContext;
     
@@ -57,9 +58,10 @@ public final class SQLRewriteContext {
     @Getter(AccessLevel.NONE)
     private final SQLTokenGenerators sqlTokenGenerators = new SQLTokenGenerators();
     
-    public SQLRewriteContext(final String schemaName, final ShardingSphereSchema schema, final SQLStatementContext<?> sqlStatementContext, final String sql, final List<Object> parameters) {
-        this.schemaName = schemaName;
-        this.schema = schema;
+    public SQLRewriteContext(final String databaseName, final Map<String, ShardingSphereSchema> schemas,
+                             final SQLStatementContext<?> sqlStatementContext, final String sql, final List<Object> parameters) {
+        this.databaseName = databaseName;
+        this.schemas = schemas;
         this.sqlStatementContext = sqlStatementContext;
         this.sql = sql;
         this.parameters = parameters;
@@ -83,6 +85,6 @@ public final class SQLRewriteContext {
      * Generate SQL tokens.
      */
     public void generateSQLTokens() {
-        sqlTokens.addAll(sqlTokenGenerators.generateSQLTokens(sqlStatementContext, parameters, schema));
+        sqlTokens.addAll(sqlTokenGenerators.generateSQLTokens(databaseName, schemas, sqlStatementContext, parameters));
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/SQLTokenGenerators.java b/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/SQLTokenGenerators.java
index 13a02a5e282..61a2593f817 100644
--- a/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/SQLTokenGenerators.java
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/SQLTokenGenerators.java
@@ -53,16 +53,18 @@ public final class SQLTokenGenerators {
     /**
      * Generate SQL tokens.
      *
+     * @param databaseName database name
      * @param sqlStatementContext SQL statement context
      * @param parameters SQL parameters
-     * @param schema ShardingSphere schema
+     * @param schemas ShardingSphere schema map
      * @return SQL tokens
      */
     @SuppressWarnings({"rawtypes", "unchecked"})
-    public List<SQLToken> generateSQLTokens(final SQLStatementContext sqlStatementContext, final List<Object> parameters, final ShardingSphereSchema schema) {
+    public List<SQLToken> generateSQLTokens(final String databaseName, final Map<String, ShardingSphereSchema> schemas,
+                                            final SQLStatementContext sqlStatementContext, final List<Object> parameters) {
         List<SQLToken> result = new LinkedList<>();
         for (SQLTokenGenerator each : sqlTokenGenerators.values()) {
-            setUpSQLTokenGenerator(each, parameters, schema, result);
+            setUpSQLTokenGenerator(each, parameters, databaseName, schemas, result);
             if (each instanceof OptionalSQLTokenGenerator) {
                 SQLToken sqlToken = ((OptionalSQLTokenGenerator) each).generateSQLToken(sqlStatementContext);
                 if (!result.contains(sqlToken)) {
@@ -75,12 +77,14 @@ public final class SQLTokenGenerators {
         return result;
     }
     
-    private void setUpSQLTokenGenerator(final SQLTokenGenerator sqlTokenGenerator, final List<Object> parameters, final ShardingSphereSchema schema, final List<SQLToken> previousSQLTokens) {
+    private void setUpSQLTokenGenerator(final SQLTokenGenerator sqlTokenGenerator, final List<Object> parameters,
+                                        final String databaseName, final Map<String, ShardingSphereSchema> schemas, final List<SQLToken> previousSQLTokens) {
         if (sqlTokenGenerator instanceof ParametersAware) {
             ((ParametersAware) sqlTokenGenerator).setParameters(parameters);
         }
         if (sqlTokenGenerator instanceof SchemaMetaDataAware) {
-            ((SchemaMetaDataAware) sqlTokenGenerator).setSchema(schema);
+            ((SchemaMetaDataAware) sqlTokenGenerator).setSchemas(schemas);
+            ((SchemaMetaDataAware) sqlTokenGenerator).setDatabaseName(databaseName);
         }
         if (sqlTokenGenerator instanceof PreviousSQLTokensAware) {
             ((PreviousSQLTokensAware) sqlTokenGenerator).setPreviousSQLTokens(previousSQLTokens);
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/aware/SchemaMetaDataAware.java b/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/aware/SchemaMetaDataAware.java
index b21b0cdc474..bced4cac2e0 100644
--- a/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/aware/SchemaMetaDataAware.java
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/aware/SchemaMetaDataAware.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.infra.rewrite.sql.token.generator.aware;
 
 import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 
+import java.util.Map;
+
 /**
  * Schema meta data aware.
  */
@@ -27,7 +29,14 @@ public interface SchemaMetaDataAware {
     /**
      * Set schema meta data.
      * 
-     * @param schema schema meta data
+     * @param schemas schema meta data map
+     */
+    void setSchemas(Map<String, ShardingSphereSchema> schemas);
+    
+    /**
+     * Set database name.
+     * 
+     * @param databaseName database name
      */
-    void setSchema(ShardingSphereSchema schema);
+    void setDatabaseName(String databaseName);
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/generic/RemoveTokenGenerator.java b/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/generic/RemoveTokenGenerator.java
index 1a121c845f6..8627c38400f 100644
--- a/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/generic/RemoveTokenGenerator.java
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/generic/RemoveTokenGenerator.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.infra.rewrite.sql.token.generator.generic;
 
-import org.apache.shardingsphere.infra.binder.segment.table.TablesContext;
 import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
 import org.apache.shardingsphere.infra.binder.type.RemoveAvailable;
 import org.apache.shardingsphere.infra.binder.type.TableAvailable;
@@ -44,8 +43,7 @@ public final class RemoveTokenGenerator implements CollectionSQLTokenGenerator<S
         }
         boolean containsSchemaName = false;
         if (sqlStatementContext instanceof TableAvailable) {
-            TablesContext tablesContext = ((TableAvailable) sqlStatementContext).getTablesContext();
-            containsSchemaName = tablesContext.getDatabaseName().isPresent() || !tablesContext.getSchemaNames().isEmpty();
+            containsSchemaName = ((TableAvailable) sqlStatementContext).getTablesContext().getDatabaseName().isPresent();
         }
         return containsRemoveSegment || containsSchemaName;
     }
@@ -56,8 +54,7 @@ public final class RemoveTokenGenerator implements CollectionSQLTokenGenerator<S
         if (sqlStatementContext instanceof RemoveAvailable && !((RemoveAvailable) sqlStatementContext).getRemoveSegments().isEmpty()) {
             result.addAll(generateRemoveAvailableSQLTokens(((RemoveAvailable) sqlStatementContext).getRemoveSegments()));
         }
-        if (sqlStatementContext instanceof TableAvailable && (((TableAvailable) sqlStatementContext).getTablesContext().getDatabaseName().isPresent()
-                || !((TableAvailable) sqlStatementContext).getTablesContext().getSchemaNames().isEmpty())) {
+        if (sqlStatementContext instanceof TableAvailable && ((TableAvailable) sqlStatementContext).getTablesContext().getDatabaseName().isPresent()) {
             result.addAll(generateTableAvailableSQLTokens((TableAvailable) sqlStatementContext));
         }
         return result;
@@ -75,7 +72,8 @@ public final class RemoveTokenGenerator implements CollectionSQLTokenGenerator<S
             }
             OwnerSegment owner = each.getOwner().get();
             int startIndex = owner.getOwner().isPresent() ? owner.getOwner().get().getStartIndex() : owner.getStartIndex();
-            result.add(new RemoveToken(startIndex, each.getTableName().getStartIndex() - 1));
+            int stopIndex = owner.getOwner().isPresent() ? owner.getStartIndex() - 1 : each.getTableName().getStartIndex() - 1;
+            result.add(new RemoveToken(startIndex, stopIndex));
         }
         return result;
     }
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntryTest.java b/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntryTest.java
index 9a86c00788f..b9503e1d00f 100644
--- a/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntryTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntryTest.java
@@ -33,6 +33,8 @@ import org.mockito.junit.MockitoJUnitRunner;
 
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
@@ -50,7 +52,7 @@ public final class SQLRewriteEntryTest {
     
     @Test
     public void assertRewriteForGenericSQLRewriteResult() {
-        SQLRewriteEntry sqlRewriteEntry = new SQLRewriteEntry(DefaultSchema.LOGIC_NAME, schema, props, Collections.emptyList());
+        SQLRewriteEntry sqlRewriteEntry = new SQLRewriteEntry(DefaultSchema.LOGIC_NAME, mockSchemaMap(), props, Collections.emptyList());
         RouteContext routeContext = new RouteContext();
         GenericSQLRewriteResult sqlRewriteResult = (GenericSQLRewriteResult) sqlRewriteEntry.rewrite("SELECT ?", Collections.singletonList(1), mock(SQLStatementContext.class), routeContext);
         assertThat(sqlRewriteResult.getSqlRewriteUnit().getSql(), is("SELECT ?"));
@@ -59,7 +61,7 @@ public final class SQLRewriteEntryTest {
     
     @Test
     public void assertRewriteForRouteSQLRewriteResult() {
-        SQLRewriteEntry sqlRewriteEntry = new SQLRewriteEntry(DefaultSchema.LOGIC_NAME, schema, props, Collections.emptyList());
+        SQLRewriteEntry sqlRewriteEntry = new SQLRewriteEntry(DefaultSchema.LOGIC_NAME, mockSchemaMap(), props, Collections.emptyList());
         RouteContext routeContext = new RouteContext();
         RouteUnit firstRouteUnit = mock(RouteUnit.class);
         when(firstRouteUnit.getDataSourceMapper()).thenReturn(new RouteMapper("ds", "ds_0"));
@@ -69,4 +71,10 @@ public final class SQLRewriteEntryTest {
         RouteSQLRewriteResult sqlRewriteResult = (RouteSQLRewriteResult) sqlRewriteEntry.rewrite("SELECT ?", Collections.singletonList(1), mock(SQLStatementContext.class), routeContext);
         assertThat(sqlRewriteResult.getSqlRewriteUnits().size(), is(2));
     }
+    
+    private Map<String, ShardingSphereSchema> mockSchemaMap() {
+        Map<String, ShardingSphereSchema> result = new HashMap<>(1, 1);
+        result.put("test", mock(ShardingSphereSchema.class));
+        return result;
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/context/SQLRewriteContextTest.java b/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/context/SQLRewriteContextTest.java
index f740319f3b0..21b5171aa4e 100644
--- a/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/context/SQLRewriteContextTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/context/SQLRewriteContextTest.java
@@ -35,6 +35,8 @@ import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.junit.Assert.assertFalse;
@@ -49,9 +51,6 @@ public final class SQLRewriteContextTest {
     @Mock
     private SQLStatementContext sqlStatementContext;
     
-    @Mock
-    private ShardingSphereSchema schema;
-    
     @Mock
     private SQLToken sqlToken;
     
@@ -73,7 +72,7 @@ public final class SQLRewriteContextTest {
         when(((TableAvailable) statementContext).getTablesContext().getDatabaseName().isPresent()).thenReturn(false);
         when(statementContext.getInsertSelectContext()).thenReturn(null);
         SQLRewriteContext sqlRewriteContext = new SQLRewriteContext(DefaultSchema.LOGIC_NAME,
-                mock(ShardingSphereSchema.class), statementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1));
+                mockSchemaMap(), statementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1));
         assertThat(sqlRewriteContext.getParameterBuilder(), instanceOf(GroupedParameterBuilder.class));
     }
     
@@ -82,14 +81,14 @@ public final class SQLRewriteContextTest {
         SelectStatementContext statementContext = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
         when(((TableAvailable) statementContext).getTablesContext().getDatabaseName().isPresent()).thenReturn(false);
         SQLRewriteContext sqlRewriteContext = new SQLRewriteContext(DefaultSchema.LOGIC_NAME,
-                mock(ShardingSphereSchema.class), statementContext, "SELECT * FROM tbl WHERE id = ?", Collections.singletonList(1));
+                mockSchemaMap(), statementContext, "SELECT * FROM tbl WHERE id = ?", Collections.singletonList(1));
         assertThat(sqlRewriteContext.getParameterBuilder(), instanceOf(StandardParameterBuilder.class));
     }
     
     @Test
     public void assertGenerateOptionalSQLToken() {
         SQLRewriteContext sqlRewriteContext = new SQLRewriteContext(DefaultSchema.LOGIC_NAME,
-                schema, sqlStatementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1));
+                mockSchemaMap(), sqlStatementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1));
         sqlRewriteContext.addSQLTokenGenerators(Collections.singleton(optionalSQLTokenGenerator));
         sqlRewriteContext.generateSQLTokens();
         assertFalse(sqlRewriteContext.getSqlTokens().isEmpty());
@@ -99,10 +98,16 @@ public final class SQLRewriteContextTest {
     @Test
     public void assertGenerateCollectionSQLToken() {
         SQLRewriteContext sqlRewriteContext = new SQLRewriteContext(DefaultSchema.LOGIC_NAME,
-                schema, sqlStatementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1));
+                mockSchemaMap(), sqlStatementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1));
         sqlRewriteContext.addSQLTokenGenerators(Collections.singleton(collectionSQLTokenGenerator));
         sqlRewriteContext.generateSQLTokens();
         assertFalse(sqlRewriteContext.getSqlTokens().isEmpty());
         assertThat(sqlRewriteContext.getSqlTokens().get(0), instanceOf(SQLToken.class));
     }
+    
+    private Map<String, ShardingSphereSchema> mockSchemaMap() {
+        Map<String, ShardingSphereSchema> result = new HashMap<>(1, 1);
+        result.put("test", mock(ShardingSphereSchema.class));
+        return result;
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/GenericSQLRewriteEngineTest.java b/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/GenericSQLRewriteEngineTest.java
index 0f97f5f636d..2fc34eb8c2a 100644
--- a/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/GenericSQLRewriteEngineTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/GenericSQLRewriteEngineTest.java
@@ -25,6 +25,8 @@ import org.apache.shardingsphere.infra.rewrite.engine.result.GenericSQLRewriteRe
 import org.junit.Test;
 
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
@@ -36,8 +38,14 @@ public final class GenericSQLRewriteEngineTest {
     public void assertRewrite() {
         GenericSQLRewriteResult actual = new GenericSQLRewriteEngine().rewrite(
                 new SQLRewriteContext(DefaultSchema.LOGIC_NAME,
-                        mock(ShardingSphereSchema.class), mock(SQLStatementContext.class), "SELECT 1", Collections.emptyList()));
+                        mockSchemaMap(), mock(SQLStatementContext.class), "SELECT 1", Collections.emptyList()));
         assertThat(actual.getSqlRewriteUnit().getSql(), is("SELECT 1"));
         assertThat(actual.getSqlRewriteUnit().getParameters(), is(Collections.emptyList()));
     }
+    
+    private Map<String, ShardingSphereSchema> mockSchemaMap() {
+        Map<String, ShardingSphereSchema> result = new HashMap<>(1, 1);
+        result.put("test", mock(ShardingSphereSchema.class));
+        return result;
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngineTest.java b/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngineTest.java
index 731527d5c3b..e7036aad5c5 100644
--- a/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngineTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/engine/RouteSQLRewriteEngineTest.java
@@ -33,6 +33,8 @@ import org.junit.Test;
 
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
@@ -46,7 +48,7 @@ public final class RouteSQLRewriteEngineTest {
     @Test
     public void assertRewriteWithStandardParameterBuilder() {
         SQLRewriteContext sqlRewriteContext = new SQLRewriteContext(DefaultSchema.LOGIC_NAME,
-                mock(ShardingSphereSchema.class), mock(SQLStatementContext.class), "SELECT ?", Collections.singletonList(1));
+                mockSchemaMap(), mock(SQLStatementContext.class), "SELECT ?", Collections.singletonList(1));
         RouteUnit routeUnit = new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.singletonList(new RouteMapper("tbl", "tbl_0")));
         RouteContext routeContext = new RouteContext();
         routeContext.getRouteUnits().add(routeUnit);
@@ -62,7 +64,7 @@ public final class RouteSQLRewriteEngineTest {
         when(statementContext.getOrderByContext().getItems()).thenReturn(Collections.emptyList());
         when(statementContext.getPaginationContext().isHasPagination()).thenReturn(false);
         SQLRewriteContext sqlRewriteContext = new SQLRewriteContext(DefaultSchema.LOGIC_NAME,
-                mock(ShardingSphereSchema.class), statementContext, "SELECT ?", Collections.singletonList(1));
+                mockSchemaMap(), statementContext, "SELECT ?", Collections.singletonList(1));
         RouteContext routeContext = new RouteContext();
         RouteUnit firstRouteUnit = new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.singletonList(new RouteMapper("tbl", "tbl_0")));
         RouteUnit secondRouteUnit = new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.singletonList(new RouteMapper("tbl", "tbl_1")));
@@ -80,7 +82,7 @@ public final class RouteSQLRewriteEngineTest {
         when(((TableAvailable) statementContext).getTablesContext().getDatabaseName().isPresent()).thenReturn(false);
         when(statementContext.getGroupedParameters()).thenReturn(Collections.singletonList(Collections.singletonList(1)));
         SQLRewriteContext sqlRewriteContext = new SQLRewriteContext(DefaultSchema.LOGIC_NAME,
-                mock(ShardingSphereSchema.class), statementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1));
+                mockSchemaMap(), statementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1));
         RouteUnit routeUnit = new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.singletonList(new RouteMapper("tbl", "tbl_0")));
         RouteContext routeContext = new RouteContext();
         routeContext.getRouteUnits().add(routeUnit);
@@ -96,7 +98,7 @@ public final class RouteSQLRewriteEngineTest {
         when(((TableAvailable) statementContext).getTablesContext().getDatabaseName().isPresent()).thenReturn(false);
         when(statementContext.getGroupedParameters()).thenReturn(Collections.singletonList(Collections.singletonList(1)));
         SQLRewriteContext sqlRewriteContext = new SQLRewriteContext(DefaultSchema.LOGIC_NAME,
-                mock(ShardingSphereSchema.class), statementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1));
+                mockSchemaMap(), statementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1));
         RouteUnit routeUnit = new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.singletonList(new RouteMapper("tbl", "tbl_0")));
         RouteContext routeContext = new RouteContext();
         routeContext.getRouteUnits().add(routeUnit);
@@ -114,7 +116,7 @@ public final class RouteSQLRewriteEngineTest {
         when(((TableAvailable) statementContext).getTablesContext().getDatabaseName().isPresent()).thenReturn(false);
         when(statementContext.getGroupedParameters()).thenReturn(Collections.singletonList(Collections.singletonList(1)));
         SQLRewriteContext sqlRewriteContext = new SQLRewriteContext(DefaultSchema.LOGIC_NAME,
-                mock(ShardingSphereSchema.class), statementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1));
+                mockSchemaMap(), statementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1));
         RouteUnit routeUnit = new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.singletonList(new RouteMapper("tbl", "tbl_0")));
         RouteContext routeContext = new RouteContext();
         routeContext.getRouteUnits().add(routeUnit);
@@ -133,7 +135,7 @@ public final class RouteSQLRewriteEngineTest {
         when(statementContext.getGroupedParameters()).thenReturn(Collections.singletonList(Collections.singletonList(1)));
         when(statementContext.getOnDuplicateKeyUpdateParameters()).thenReturn(Collections.emptyList());
         SQLRewriteContext sqlRewriteContext = new SQLRewriteContext(DefaultSchema.LOGIC_NAME,
-                mock(ShardingSphereSchema.class), statementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1));
+                mockSchemaMap(), statementContext, "INSERT INTO tbl VALUES (?)", Collections.singletonList(1));
         RouteUnit routeUnit = new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.singletonList(new RouteMapper("tbl", "tbl_0")));
         RouteContext routeContext = new RouteContext();
         routeContext.getRouteUnits().add(routeUnit);
@@ -143,4 +145,10 @@ public final class RouteSQLRewriteEngineTest {
         assertThat(actual.getSqlRewriteUnits().get(routeUnit).getSql(), is("INSERT INTO tbl VALUES (?)"));
         assertTrue(actual.getSqlRewriteUnits().get(routeUnit).getParameters().isEmpty());
     }
+    
+    private Map<String, ShardingSphereSchema> mockSchemaMap() {
+        Map<String, ShardingSphereSchema> result = new HashMap<>(1, 1);
+        result.put("test", mock(ShardingSphereSchema.class));
+        return result;
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/SQLTokenGeneratorsTest.java b/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/SQLTokenGeneratorsTest.java
index a5e51ad1ead..55408f7f7a7 100644
--- a/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/SQLTokenGeneratorsTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/src/test/java/org/apache/shardingsphere/infra/rewrite/sql/token/generator/SQLTokenGeneratorsTest.java
@@ -30,6 +30,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -88,7 +89,7 @@ public final class SQLTokenGeneratorsTest {
         sqlTokenGenerators.addAll(Collections.singleton(optionalSQLTokenGenerator));
         SQLToken expectedToken = mock(SQLToken.class);
         when(optionalSQLTokenGenerator.generateSQLToken(any(SQLStatementContext.class))).thenReturn(expectedToken);
-        Collection<SQLToken> actualSqlTokens = sqlTokenGenerators.generateSQLTokens(mock(SQLStatementContext.class), Collections.emptyList(), mock(ShardingSphereSchema.class));
+        Collection<SQLToken> actualSqlTokens = sqlTokenGenerators.generateSQLTokens("sharding_db", mockSchemaMap(), mock(SQLStatementContext.class), Collections.emptyList());
         assertNotNull(actualSqlTokens);
         assertThat(actualSqlTokens.size(), is(1));
         assertThat(actualSqlTokens.iterator().next(), is(expectedToken));
@@ -102,7 +103,7 @@ public final class SQLTokenGeneratorsTest {
         sqlTokenGenerators.addAll(Collections.singleton(collectionSQLTokenGenerator));
         List<SQLToken> expectedCollection = Arrays.asList(mock(SQLToken.class), mock(SQLToken.class));
         doReturn(expectedCollection).when(collectionSQLTokenGenerator).generateSQLTokens(any());
-        List<SQLToken> actualSqlTokens = sqlTokenGenerators.generateSQLTokens(mock(SQLStatementContext.class), Collections.emptyList(), mock(ShardingSphereSchema.class));
+        List<SQLToken> actualSqlTokens = sqlTokenGenerators.generateSQLTokens("sharding_db", mockSchemaMap(), mock(SQLStatementContext.class), Collections.emptyList());
         assertNotNull(actualSqlTokens);
         assertThat(actualSqlTokens.size(), is(2));
         assertThat(actualSqlTokens, is(expectedCollection));
@@ -114,4 +115,10 @@ public final class SQLTokenGeneratorsTest {
         field.setAccessible(true);
         return (Map<Class<?>, SQLTokenGenerator>) field.get(sqlTokenGenerators);
     }
+    
+    private Map<String, ShardingSphereSchema> mockSchemaMap() {
+        Map<String, ShardingSphereSchema> result = new HashMap<>(1, 1);
+        result.put("test", mock(ShardingSphereSchema.class));
+        return result;
+    }
 }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/ral/dataset/empty_rules/postgresql/preview_federation_select.xml b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/ral/dataset/empty_rules/postgresql/preview_federation_select.xml
index aad09e6ef13..75418a133cc 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/ral/dataset/empty_rules/postgresql/preview_federation_select.xml
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/ral/dataset/empty_rules/postgresql/preview_federation_select.xml
@@ -20,15 +20,15 @@
         <column name="data_source_name"/>
         <column name="actual_sql"/>
     </metadata>
-    <row values="encrypt_write_ds_0| SELECT * FROM &quot;t_user_item_0&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_10&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_20&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_30&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
-    <row values="encrypt_write_ds_1| SELECT * FROM &quot;t_user_item_1&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_11&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_21&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_31&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
-    <row values="encrypt_write_ds_2| SELECT * FROM &quot;t_user_item_2&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_12&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_22&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_32&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
-    <row values="encrypt_write_ds_3| SELECT * FROM &quot;t_user_item_3&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_13&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_23&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_33&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
-    <row values="encrypt_write_ds_4| SELECT * FROM &quot;t_user_item_4&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_14&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_24&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_34&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
-    <row values="encrypt_write_ds_5| SELECT * FROM &quot;t_user_item_5&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_15&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_25&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_35&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
-    <row values="encrypt_write_ds_6| SELECT * FROM &quot;t_user_item_6&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_16&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_26&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_36&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
-    <row values="encrypt_write_ds_7| SELECT * FROM &quot;t_user_item_7&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_17&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_27&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_37&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
-    <row values="encrypt_write_ds_8| SELECT * FROM &quot;t_user_item_8&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_18&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_28&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_38&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
-    <row values="encrypt_write_ds_9| SELECT * FROM &quot;t_user_item_9&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_19&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_29&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;t_user_item_39&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
-    <row values="encrypt_read_ds_0| SELECT * FROM &quot;t_single_table&quot;"/>
+    <row values="encrypt_write_ds_0| SELECT * FROM &quot;public&quot;.&quot;t_user_item_0&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_10&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_20&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_30&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
+    <row values="encrypt_write_ds_1| SELECT * FROM &quot;public&quot;.&quot;t_user_item_1&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_11&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_21&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_31&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
+    <row values="encrypt_write_ds_2| SELECT * FROM &quot;public&quot;.&quot;t_user_item_2&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_12&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_22&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_32&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
+    <row values="encrypt_write_ds_3| SELECT * FROM &quot;public&quot;.&quot;t_user_item_3&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_13&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_23&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_33&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
+    <row values="encrypt_write_ds_4| SELECT * FROM &quot;public&quot;.&quot;t_user_item_4&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_14&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_24&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_34&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
+    <row values="encrypt_write_ds_5| SELECT * FROM &quot;public&quot;.&quot;t_user_item_5&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_15&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_25&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_35&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
+    <row values="encrypt_write_ds_6| SELECT * FROM &quot;public&quot;.&quot;t_user_item_6&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_16&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_26&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_36&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
+    <row values="encrypt_write_ds_7| SELECT * FROM &quot;public&quot;.&quot;t_user_item_7&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_17&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_27&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_37&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
+    <row values="encrypt_write_ds_8| SELECT * FROM &quot;public&quot;.&quot;t_user_item_8&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_18&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_28&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_38&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
+    <row values="encrypt_write_ds_9| SELECT * FROM &quot;public&quot;.&quot;t_user_item_9&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_19&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_29&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1 UNION ALL SELECT * FROM &quot;public&quot;.&quot;t_user_item_39&quot; WHERE CAST(&quot;user_id&quot; AS INTEGER) = 1"/>
+    <row values="encrypt_read_ds_0| SELECT * FROM &quot;public&quot;.&quot;t_single_table&quot;"/>
 </dataset>
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/fixture/SchemaBasedEncryptAlgorithmFixture.java b/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/fixture/SchemaBasedEncryptAlgorithmFixture.java
index 5c9795aae7e..5fe230119f3 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/fixture/SchemaBasedEncryptAlgorithmFixture.java
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/fixture/SchemaBasedEncryptAlgorithmFixture.java
@@ -24,10 +24,14 @@ import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
 import org.apache.shardingsphere.infra.rewrite.sql.token.generator.aware.SchemaMetaDataAware;
 
+import java.util.Map;
+
 @Setter
 public final class SchemaBasedEncryptAlgorithmFixture implements EncryptAlgorithm<Object, String>, SchemaMetaDataAware {
     
-    private ShardingSphereSchema schema;
+    private Map<String, ShardingSphereSchema> schemas;
+    
+    private String databaseName;
     
     @Override
     public void init() {
@@ -35,13 +39,13 @@ public final class SchemaBasedEncryptAlgorithmFixture implements EncryptAlgorith
     
     @Override
     public String encrypt(final Object plainValue, final EncryptContext encryptContext) {
-        TableMetaData tableMetaData = schema.get(encryptContext.getTableName());
+        TableMetaData tableMetaData = schemas.get(databaseName).get(encryptContext.getTableName());
         return "encrypt_" + plainValue + "_" + tableMetaData.getName();
     }
     
     @Override
     public Object decrypt(final String cipherValue, final EncryptContext encryptContext) {
-        TableMetaData tableMetaData = schema.get(encryptContext.getTableName());
+        TableMetaData tableMetaData = schemas.get(databaseName).get(encryptContext.getTableName());
         return cipherValue.replaceAll("encrypt_", "").replaceAll("_" + tableMetaData.getName(), "");
     }
     
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/engine/AbstractSQLRewriterParameterizedTest.java b/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/engine/AbstractSQLRewriterParameterizedTest.java
index 3c7bfbd48dd..f7bd6878ad6 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/engine/AbstractSQLRewriterParameterizedTest.java
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/engine/AbstractSQLRewriterParameterizedTest.java
@@ -122,7 +122,7 @@ public abstract class AbstractSQLRewriterParameterizedTest {
         LogicSQL logicSQL = new LogicSQL(sqlStatementContext, getTestParameters().getInputSQL(), getTestParameters().getInputParameters());
         ConfigurationProperties props = new ConfigurationProperties(rootConfig.getProps());
         RouteContext routeContext = new SQLRouteEngine(rules, props).route(logicSQL, metaData);
-        SQLRewriteEntry sqlRewriteEntry = new SQLRewriteEntry(schemaName, metaData.getDefaultSchema(), props, rules);
+        SQLRewriteEntry sqlRewriteEntry = new SQLRewriteEntry(schemaName, schemas, props, rules);
         SQLRewriteResult sqlRewriteResult = sqlRewriteEntry.rewrite(getTestParameters().getInputSQL(), getTestParameters().getInputParameters(), sqlStatementContext, routeContext);
         return sqlRewriteResult instanceof GenericSQLRewriteResult
                 ? Collections.singletonList(((GenericSQLRewriteResult) sqlRewriteResult).getSqlRewriteUnit())
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/delete.xml b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/delete.xml
index d5b78643ce1..b62e9963e37 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/delete.xml
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/delete.xml
@@ -27,13 +27,13 @@
         <output sql="DELETE FROM t_account_0 WHERE account_id = 100" />
     </rewrite-assertion>
     
-    <rewrite-assertion id="delete_without_sharding_value_for_parameters">
+    <rewrite-assertion id="delete_without_sharding_value_for_parameters" db-types="MySQL,Oracle,SQLServer,SQL92">
         <input sql="DELETE FROM sharding_db.t_account WHERE status = ?" parameters="OK" />
         <output sql="DELETE FROM t_account_0 WHERE status = ?" parameters="OK" />
         <output sql="DELETE FROM t_account_1 WHERE status = ?" parameters="OK" />
     </rewrite-assertion>
     
-    <rewrite-assertion id="delete_without_sharding_value_for_literals">
+    <rewrite-assertion id="delete_without_sharding_value_for_literals" db-types="MySQL,Oracle,SQLServer,SQL92">
         <input sql="DELETE FROM sharding_db.t_account WHERE status = 'OK'" />
         <output sql="DELETE FROM t_account_0 WHERE status = 'OK'" />
         <output sql="DELETE FROM t_account_1 WHERE status = 'OK'" />
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/select.xml b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/select.xml
index d4f0ecd9886..cca216ca31f 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/select.xml
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/select.xml
@@ -130,12 +130,12 @@
     
     <rewrite-assertion id="select_without_sharding_value_with_dollar_parameter_marker_for_parameters" db-types="PostgreSQL,openGauss">
         <input sql="SELECT * FROM public.t_account WHERE amount = $2 OR amount = $1" parameters="900, 1000" />
-        <output sql="SELECT * FROM t_account_0 WHERE amount = $2 OR amount = $1 UNION ALL SELECT * FROM t_account_1 WHERE amount = $2 OR amount = $1" parameters="900, 1000" />
+        <output sql="SELECT * FROM public.t_account_0 WHERE amount = $2 OR amount = $1 UNION ALL SELECT * FROM public.t_account_1 WHERE amount = $2 OR amount = $1" parameters="900, 1000" />
     </rewrite-assertion>
 
     <rewrite-assertion id="select_without_sharding_value_with_dollar_parameter_marker_for_literals" db-types="PostgreSQL,openGauss">
         <input sql="SELECT * FROM public.t_account WHERE amount = 1000 OR amount = 900" />
-        <output sql="SELECT * FROM t_account_0 WHERE amount = 1000 OR amount = 900 UNION ALL SELECT * FROM t_account_1 WHERE amount = 1000 OR amount = 900" />
+        <output sql="SELECT * FROM public.t_account_0 WHERE amount = 1000 OR amount = 900 UNION ALL SELECT * FROM public.t_account_1 WHERE amount = 1000 OR amount = 900" />
     </rewrite-assertion>
 
     <rewrite-assertion id="select_for_literals_with_order_by" db-types="MySQL">
@@ -585,11 +585,11 @@
 
     <rewrite-assertion id="select_with_schema_name_in_table" db-types="PostgreSQL,openGauss">
         <input sql="SELECT account_id FROM public.t_account WHERE account_id = ?" parameters="100" />
-        <output sql="SELECT account_id FROM t_account_0 WHERE account_id = ?" parameters="100" />
+        <output sql="SELECT account_id FROM public.t_account_0 WHERE account_id = ?" parameters="100" />
     </rewrite-assertion>
 
     <rewrite-assertion id="select_with_database_name_and_schema_name_in_table" db-types="PostgreSQL,openGauss">
         <input sql="SELECT account_id FROM public.public.t_account WHERE account_id = ?" parameters="100" />
-        <output sql="SELECT account_id FROM t_account_0 WHERE account_id = ?" parameters="100" />
+        <output sql="SELECT account_id FROM public.t_account_0 WHERE account_id = ?" parameters="100" />
     </rewrite-assertion>
 </rewrite-assertions>