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/06/16 09:28:02 UTC

[shardingsphere] branch master updated: Support create table with index statement rewrite when config encrypt (#18388)

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

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 05b6b042a4e Support create table with index statement rewrite when config encrypt (#18388)
05b6b042a4e is described below

commit 05b6b042a4ea50d0b1582285681ea979569ab4bf
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Thu Jun 16 17:27:56 2022 +0800

    Support create table with index statement rewrite when config encrypt (#18388)
---
 .../rewrite/token/EncryptTokenGenerateBuilder.java |  4 +-
 ....java => EncryptIndexColumnTokenGenerator.java} | 44 +++++++++++++---------
 .../statement/ddl/AlterIndexStatementContext.java  |  6 +++
 .../statement/ddl/AlterTableStatementContext.java  | 13 ++++++-
 .../statement/ddl/CreateIndexStatementContext.java |  6 +++
 .../statement/ddl/CreateTableStatementContext.java | 10 +++++
 .../statement/ddl/DropIndexStatementContext.java   |  6 +++
 .../infra/binder/type/IndexAvailable.java          |  8 ++++
 .../query-with-cipher/ddl/alter/alter-table.xml    | 27 +++++++------
 .../query-with-cipher/ddl/create/create-table.xml  |  9 ++++-
 .../query-with-plain/ddl/alter/alter-table.xml     | 39 ++++++++++---------
 .../query-with-plain/ddl/create/create-table.xml   |  9 ++++-
 12 files changed, 127 insertions(+), 54 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilder.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilder.java
index e04fba885a6..4de03038d6a 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilder.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptTokenGenerateBuilder.java
@@ -24,7 +24,7 @@ import org.apache.shardingsphere.encrypt.rewrite.condition.EncryptCondition;
 import org.apache.shardingsphere.encrypt.rewrite.token.generator.AssistQueryAndPlainInsertColumnsTokenGenerator;
 import org.apache.shardingsphere.encrypt.rewrite.token.generator.EncryptAlterTableTokenGenerator;
 import org.apache.shardingsphere.encrypt.rewrite.token.generator.EncryptAssignmentTokenGenerator;
-import org.apache.shardingsphere.encrypt.rewrite.token.generator.EncryptCreateIndexTokenGenerator;
+import org.apache.shardingsphere.encrypt.rewrite.token.generator.EncryptIndexColumnTokenGenerator;
 import org.apache.shardingsphere.encrypt.rewrite.token.generator.EncryptCreateTableTokenGenerator;
 import org.apache.shardingsphere.encrypt.rewrite.token.generator.EncryptForUseDefaultInsertColumnsTokenGenerator;
 import org.apache.shardingsphere.encrypt.rewrite.token.generator.EncryptInsertOnUpdateTokenGenerator;
@@ -72,7 +72,7 @@ public final class EncryptTokenGenerateBuilder implements SQLTokenGeneratorBuild
         addSQLTokenGenerator(result, new EncryptCreateTableTokenGenerator());
         addSQLTokenGenerator(result, new EncryptAlterTableTokenGenerator());
         addSQLTokenGenerator(result, new EncryptOrderByItemTokenGenerator());
-        addSQLTokenGenerator(result, new EncryptCreateIndexTokenGenerator());
+        addSQLTokenGenerator(result, new EncryptIndexColumnTokenGenerator());
         return result;
     }
     
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptCreateIndexTokenGenerator.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptIndexColumnTokenGenerator.java
similarity index 56%
rename from shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptCreateIndexTokenGenerator.java
rename to shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptIndexColumnTokenGenerator.java
index d71a85ec082..aed057b3d79 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptCreateIndexTokenGenerator.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptIndexColumnTokenGenerator.java
@@ -17,15 +17,17 @@
 
 package org.apache.shardingsphere.encrypt.rewrite.token.generator;
 
+import com.google.common.base.Preconditions;
 import lombok.Setter;
 import org.apache.shardingsphere.encrypt.rule.EncryptRule;
 import org.apache.shardingsphere.encrypt.rule.aware.EncryptRuleAware;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ColumnProjection;
 import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.binder.statement.ddl.CreateIndexStatementContext;
+import org.apache.shardingsphere.infra.binder.type.IndexAvailable;
 import org.apache.shardingsphere.infra.rewrite.sql.token.generator.CollectionSQLTokenGenerator;
 import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.SQLToken;
 import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.generic.SubstitutableColumnNameToken;
+import org.apache.shardingsphere.sql.parser.sql.common.constant.QuoteCharacter;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 
 import java.util.Collection;
@@ -34,24 +36,27 @@ import java.util.LinkedList;
 import java.util.Optional;
 
 /**
- * Create index token generator for encrypt.
+ * Index column token generator for encrypt.
  */
 @Setter
-public final class EncryptCreateIndexTokenGenerator implements CollectionSQLTokenGenerator<CreateIndexStatementContext>, EncryptRuleAware {
+public final class EncryptIndexColumnTokenGenerator implements CollectionSQLTokenGenerator<SQLStatementContext<?>>, EncryptRuleAware {
     
     private EncryptRule encryptRule;
     
     @Override
     public boolean isGenerateSQLToken(final SQLStatementContext<?> sqlStatementContext) {
-        return sqlStatementContext instanceof CreateIndexStatementContext && !(((CreateIndexStatementContext) sqlStatementContext).getSqlStatement()).getColumns().isEmpty();
+        return sqlStatementContext instanceof IndexAvailable;
     }
     
     @Override
-    public Collection<SQLToken> generateSQLTokens(final CreateIndexStatementContext createIndexStatementContext) {
+    public Collection<SQLToken> generateSQLTokens(final SQLStatementContext<?> sqlStatementContext) {
+        Preconditions.checkArgument(sqlStatementContext instanceof IndexAvailable, "SQLStatementContext must implementation IndexAvailable interface.");
+        if (sqlStatementContext.getTablesContext().getTableNames().isEmpty()) {
+            return Collections.emptyList();
+        }
         Collection<SQLToken> result = new LinkedList<>();
-        String tableName = createIndexStatementContext.getSqlStatement().getTable().getTableName().getIdentifier().getValue();
-        Collection<ColumnSegment> columns = createIndexStatementContext.getSqlStatement().getColumns();
-        for (ColumnSegment each : columns) {
+        String tableName = sqlStatementContext.getTablesContext().getTableNames().iterator().next();
+        for (ColumnSegment each : ((IndexAvailable) sqlStatementContext).getIndexColumns()) {
             encryptRule.findEncryptor(tableName, each.getIdentifier().getValue()).flatMap(optional -> getColumnToken(tableName, each)).ifPresent(result::add);
         }
         return result;
@@ -59,28 +64,31 @@ public final class EncryptCreateIndexTokenGenerator implements CollectionSQLToke
     
     private Optional<SQLToken> getColumnToken(final String tableName, final ColumnSegment columnSegment) {
         String columnName = columnSegment.getIdentifier().getValue();
+        QuoteCharacter quoteCharacter = columnSegment.getIdentifier().getQuoteCharacter();
+        int startIndex = columnSegment.getStartIndex();
+        int stopIndex = columnSegment.getStopIndex();
         boolean queryWithCipherColumn = encryptRule.isQueryWithCipherColumn(tableName, columnName);
         if (queryWithCipherColumn) {
-            Optional<String> assistedQueryColumn = encryptRule.findAssistedQueryColumn(tableName, columnName);
-            return assistedQueryColumn.map(optional -> getAssistedQueryColumnToken(columnSegment, optional)).orElseGet(() -> getCipherColumnToken(tableName, columnSegment, columnName));
+            return encryptRule.findAssistedQueryColumn(tableName, columnName).map(optional -> getAssistedQueryColumnToken(startIndex, stopIndex, optional, quoteCharacter))
+                    .orElseGet(() -> getCipherColumnToken(tableName, startIndex, stopIndex, columnName, quoteCharacter));
         }
-        return getPlainColumnToken(tableName, columnSegment, columnName);
+        return getPlainColumnToken(tableName, startIndex, stopIndex, columnName, quoteCharacter);
     }
     
-    private Optional<SQLToken> getAssistedQueryColumnToken(final ColumnSegment columnSegment, final String columnName) {
+    private Optional<SQLToken> getAssistedQueryColumnToken(final int startIndex, final int stopIndex, final String columnName, final QuoteCharacter quoteCharacter) {
         Collection<ColumnProjection> columnProjections = getColumnProjections(columnName);
-        return Optional.of(new SubstitutableColumnNameToken(columnSegment.getStartIndex(), columnSegment.getStopIndex(), columnProjections));
+        return Optional.of(new SubstitutableColumnNameToken(startIndex, stopIndex, columnProjections, quoteCharacter));
     }
     
-    private Optional<SQLToken> getCipherColumnToken(final String tableName, final ColumnSegment columnSegment, final String columnName) {
+    private Optional<SQLToken> getCipherColumnToken(final String tableName, final int startIndex, final int stopIndex, final String columnName, final QuoteCharacter quoteCharacter) {
         String cipherColumn = encryptRule.getCipherColumn(tableName, columnName);
         Collection<ColumnProjection> columnProjections = getColumnProjections(cipherColumn);
-        return Optional.of(new SubstitutableColumnNameToken(columnSegment.getStartIndex(), columnSegment.getStopIndex(), columnProjections));
+        return Optional.of(new SubstitutableColumnNameToken(startIndex, stopIndex, columnProjections, quoteCharacter));
     }
     
-    private Optional<SQLToken> getPlainColumnToken(final String tableName, final ColumnSegment columnSegment, final String columnName) {
-        Optional<String> plainColumn = encryptRule.findPlainColumn(tableName, columnName);
-        return plainColumn.map(optional -> new SubstitutableColumnNameToken(columnSegment.getStartIndex(), columnSegment.getStopIndex(), getColumnProjections(optional)));
+    private Optional<SQLToken> getPlainColumnToken(final String tableName, final int startIndex, final int stopIndex, final String columnName, final QuoteCharacter quoteCharacter) {
+        return encryptRule.findPlainColumn(tableName, columnName)
+                .map(optional -> new SubstitutableColumnNameToken(startIndex, stopIndex, getColumnProjections(optional), quoteCharacter));
     }
     
     private Collection<ColumnProjection> getColumnProjections(final String columnName) {
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/AlterIndexStatementContext.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/AlterIndexStatementContext.java
index 2b7c0286f8c..9f87c743514 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/AlterIndexStatementContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/AlterIndexStatementContext.java
@@ -23,6 +23,7 @@ import org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContex
 import org.apache.shardingsphere.infra.binder.type.IndexAvailable;
 import org.apache.shardingsphere.infra.binder.type.TableAvailable;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.AlterIndexStatement;
 import org.apache.shardingsphere.sql.parser.sql.dialect.handler.ddl.AlterIndexStatementHandler;
@@ -61,4 +62,9 @@ public final class AlterIndexStatementContext extends CommonSQLStatementContext<
         AlterIndexStatementHandler.getRenameIndexSegment(getSqlStatement()).ifPresent(result::add);
         return result;
     }
+    
+    @Override
+    public Collection<ColumnSegment> getIndexColumns() {
+        return Collections.emptyList();
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/AlterTableStatementContext.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/AlterTableStatementContext.java
index ae417f7947b..eeb9d23395b 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/AlterTableStatementContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/AlterTableStatementContext.java
@@ -32,12 +32,12 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.al
 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.alter.ValidateConstraintDefinitionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.DropIndexDefinitionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.AlterTableStatement;
 
 import java.util.Collection;
 import java.util.LinkedList;
-import java.util.List;
 
 /**
  * Alter table statement context.
@@ -85,7 +85,7 @@ public final class AlterTableStatementContext extends CommonSQLStatementContext<
     
     @Override
     public Collection<ConstraintSegment> getConstraints() {
-        List<ConstraintSegment> result = new LinkedList<>();
+        Collection<ConstraintSegment> result = new LinkedList<>();
         for (AddConstraintDefinitionSegment each : getSqlStatement().getAddConstraintDefinitions()) {
             each.getConstraintDefinition().getConstraintName().ifPresent(result::add);
         }
@@ -93,4 +93,13 @@ public final class AlterTableStatementContext extends CommonSQLStatementContext<
         getSqlStatement().getDropConstraintDefinitions().stream().map(DropConstraintDefinitionSegment::getConstraintName).forEach(result::add);
         return result;
     }
+    
+    @Override
+    public Collection<ColumnSegment> getIndexColumns() {
+        Collection<ColumnSegment> result = new LinkedList<>();
+        for (AddConstraintDefinitionSegment each : getSqlStatement().getAddConstraintDefinitions()) {
+            result.addAll(each.getConstraintDefinition().getIndexColumns());
+        }
+        return result;
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateIndexStatementContext.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateIndexStatementContext.java
index c4ae931bd4f..947026ef26f 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateIndexStatementContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateIndexStatementContext.java
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.infra.binder.type.TableAvailable;
 import org.apache.shardingsphere.infra.metadata.database.schema.util.IndexMetaDataUtil;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexNameSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateIndexStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
@@ -62,4 +63,9 @@ public final class CreateIndexStatementContext extends CommonSQLStatementContext
         return CreateIndexStatementHandler.getGeneratedIndexStartIndex(getSqlStatement()).map(each -> Collections.singletonList(new IndexSegment(each, each,
                 new IndexNameSegment(each, each, new IdentifierValue(IndexMetaDataUtil.getGeneratedLogicIndexName(getSqlStatement().getColumns())))))).orElseGet(Collections::emptyList);
     }
+    
+    @Override
+    public Collection<ColumnSegment> getIndexColumns() {
+        return getSqlStatement().getColumns();
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateTableStatementContext.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateTableStatementContext.java
index b3a2f93a4bb..1bed476b692 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateTableStatementContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateTableStatementContext.java
@@ -27,6 +27,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.column.Column
 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintDefinitionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateTableStatement;
 
@@ -78,4 +79,13 @@ public final class CreateTableStatementContext extends CommonSQLStatementContext
         }
         return result;
     }
+    
+    @Override
+    public Collection<ColumnSegment> getIndexColumns() {
+        Collection<ColumnSegment> result = new LinkedList<>();
+        for (ConstraintDefinitionSegment each : getSqlStatement().getConstraintDefinitions()) {
+            result.addAll(each.getIndexColumns());
+        }
+        return result;
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/DropIndexStatementContext.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/DropIndexStatementContext.java
index 8495a2efe76..0904b24c054 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/DropIndexStatementContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/DropIndexStatementContext.java
@@ -23,6 +23,7 @@ import org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContex
 import org.apache.shardingsphere.infra.binder.type.IndexAvailable;
 import org.apache.shardingsphere.infra.binder.type.TableAvailable;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropIndexStatement;
 import org.apache.shardingsphere.sql.parser.sql.dialect.handler.ddl.DropIndexStatementHandler;
@@ -55,4 +56,9 @@ public final class DropIndexStatementContext extends CommonSQLStatementContext<D
     public Collection<IndexSegment> getIndexes() {
         return getSqlStatement().getIndexes();
     }
+    
+    @Override
+    public Collection<ColumnSegment> getIndexColumns() {
+        return Collections.emptyList();
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/type/IndexAvailable.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/type/IndexAvailable.java
index 1043af07356..6320583aa06 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/type/IndexAvailable.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/type/IndexAvailable.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.infra.binder.type;
 
 import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 
 import java.util.Collection;
 
@@ -32,4 +33,11 @@ public interface IndexAvailable {
      * @return index segments
      */
     Collection<IndexSegment> getIndexes();
+    
+    /**
+     * Get index columns.
+     *
+     * @return index columns
+     */
+    Collection<ColumnSegment> getIndexColumns();
 }
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-cipher/ddl/alter/alter-table.xml b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-cipher/ddl/alter/alter-table.xml
index 335e864e0d8..e4c609378a3 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-cipher/ddl/alter/alter-table.xml
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-cipher/ddl/alter/alter-table.xml
@@ -17,58 +17,63 @@
   -->
 
 <rewrite-assertions yaml-rule="scenario/encrypt/config/query-with-cipher.yaml">
-    <rewrite-assertion id="modify_column_amount_for_cipher" db-types="MySQL">
+    <rewrite-assertion id="alter_table_modify_column_amount_for_cipher" db-types="MySQL">
         <input sql="ALTER TABLE t_account MODIFY COLUMN amount text"/>
         <output sql="ALTER TABLE t_account MODIFY COLUMN cipher_amount text"/>
     </rewrite-assertion>
 
-    <rewrite-assertion id="change_column_amount_for_cipher" db-types="MySQL">
+    <rewrite-assertion id="alter_table_change_column_amount_for_cipher" db-types="MySQL">
         <input sql="ALTER TABLE t_account CHANGE COLUMN amount amount_new text"/>
         <output sql="ALTER TABLE t_account CHANGE COLUMN cipher_amount amount_new_cipher text"/>
     </rewrite-assertion>
 
-    <rewrite-assertion id="add_column_with_position_cipher" db-types="MySQL">
+    <rewrite-assertion id="alter_table_add_column_with_position_cipher" db-types="MySQL">
         <input sql="ALTER TABLE t_account ADD COLUMN status text AFTER amount"/>
         <output sql="ALTER TABLE t_account ADD COLUMN status text AFTER cipher_amount"/>
     </rewrite-assertion>
 
-    <rewrite-assertion id="modify_column_with_position_cipher" db-types="MySQL">
+    <rewrite-assertion id="alter_table_modify_column_with_position_cipher" db-types="MySQL">
         <input sql="ALTER TABLE t_account MODIFY COLUMN status text AFTER amount"/>
         <output sql="ALTER TABLE t_account MODIFY COLUMN status text AFTER cipher_amount"/>
     </rewrite-assertion>
 
-    <rewrite-assertion id="add_column_with_cipher" db-types="MySQL">
+    <rewrite-assertion id="alter_table_add_column_with_cipher" db-types="MySQL">
         <input sql="ALTER TABLE t_account ADD COLUMN amount INT"/>
         <output sql="ALTER TABLE t_account ADD COLUMN cipher_amount INT"/>
     </rewrite-assertion>
     
-    <rewrite-assertion id="add_column_for_cipher" db-types="MySQL">
+    <rewrite-assertion id="alter_table_add_column_for_cipher" db-types="MySQL">
         <input sql="ALTER TABLE t_account_bak ADD COLUMN id int not null, ADD COLUMN password varchar(255) not null default ''" />
         <output sql="ALTER TABLE t_account_bak ADD COLUMN id int not null, ADD COLUMN cipher_password varchar(255) not null default '', ADD COLUMN assisted_query_password varchar(255) not null default '', ADD COLUMN plain_password varchar(255) not null default ''" />
     </rewrite-assertion>
 
-    <rewrite-assertion id="modify_column_for_cipher" db-types="MySQL">
+    <rewrite-assertion id="alter_table_modify_column_for_cipher" db-types="MySQL">
         <input sql="ALTER TABLE t_account_bak MODIFY COLUMN password varchar(255) not null default ''" />
         <output sql="ALTER TABLE t_account_bak MODIFY COLUMN cipher_password varchar(255) not null default '', MODIFY COLUMN assisted_query_password varchar(255) not null default '', MODIFY COLUMN plain_password varchar(255) not null default ''" />
     </rewrite-assertion>
 
-    <rewrite-assertion id="change_column_for_cipher" db-types="MySQL">
+    <rewrite-assertion id="alter_table_change_column_for_cipher" db-types="MySQL">
         <input sql="ALTER TABLE t_account_bak CHANGE COLUMN password password_new varchar(255) not null default ''" />
         <output sql="ALTER TABLE t_account_bak CHANGE COLUMN cipher_password password_new_cipher varchar(255) not null default '', CHANGE COLUMN assisted_query_password password_new_assisted varchar(255) not null default '', CHANGE COLUMN plain_password password_new_plain varchar(255) not null default ''" />
     </rewrite-assertion>
 
-    <rewrite-assertion id="drop_column_for_cipher" db-types="MySQL">
+    <rewrite-assertion id="alter_table_drop_column_for_cipher" db-types="MySQL">
         <input sql="ALTER TABLE t_account_bak DROP COLUMN password" />
         <output sql="ALTER TABLE t_account_bak DROP COLUMN cipher_password, DROP COLUMN assisted_query_password, DROP COLUMN plain_password" />
     </rewrite-assertion>
 
-    <rewrite-assertion id="drop_columns_for_cipher" db-types="MySQL">
+    <rewrite-assertion id="alter_table_drop_columns_for_cipher" db-types="MySQL">
         <input sql="ALTER TABLE t_account_bak DROP COLUMN password, DROP COLUMN amount" />
         <output sql="ALTER TABLE t_account_bak DROP COLUMN cipher_password, DROP COLUMN assisted_query_password, DROP COLUMN plain_password, DROP COLUMN cipher_amount, DROP COLUMN plain_amount" />
     </rewrite-assertion>
 
-    <rewrite-assertion id="drop_mix_columns_for_cipher" db-types="MySQL">
+    <rewrite-assertion id="alter_table_drop_mix_columns_for_cipher" db-types="MySQL">
         <input sql="ALTER TABLE t_account_bak DROP COLUMN password, DROP COLUMN id" />
         <output sql="ALTER TABLE t_account_bak DROP COLUMN cipher_password, DROP COLUMN assisted_query_password, DROP COLUMN plain_password, DROP COLUMN id" />
     </rewrite-assertion>
+
+    <rewrite-assertion id="alter_table_add_index_for_cipher" db-types="MySQL">
+        <input sql="ALTER TABLE t_account_bak ADD INDEX t_account_bak_amount(`amount`)"/>
+        <output sql="ALTER TABLE t_account_bak ADD INDEX t_account_bak_amount(`cipher_amount`)"/>
+    </rewrite-assertion>
 </rewrite-assertions>
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-cipher/ddl/create/create-table.xml b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-cipher/ddl/create/create-table.xml
index 25c601921e5..86f49751c3c 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-cipher/ddl/create/create-table.xml
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-cipher/ddl/create/create-table.xml
@@ -18,7 +18,12 @@
 
 <rewrite-assertions yaml-rule="scenario/encrypt/config/query-with-cipher.yaml">
     <rewrite-assertion id="create_table_for_cipher" db-types="MySQL">
-        <input sql="CREATE TABLE t_account_bak (id int not null, name varchar(100) not null default '', password varchar(255) not null default '', primary key (`id`))" />
-        <output sql="CREATE TABLE t_account_bak (id int not null, name varchar(100) not null default '', cipher_password varchar(255) not null default '', assisted_query_password varchar(255) not null default '', plain_password varchar(255) not null default '', primary key (`id`))" />
+        <input sql="CREATE TABLE t_account_bak (id INT NOT NULL, name VARCHAR(100) NOT NULL DEFAULT '', password VARCHAR(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`))" />
+        <output sql="CREATE TABLE t_account_bak (id INT NOT NULL, name VARCHAR(100) NOT NULL DEFAULT '', cipher_password VARCHAR(255) NOT NULL DEFAULT '', assisted_query_password VARCHAR(255) NOT NULL DEFAULT '', plain_password VARCHAR(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`))" />
+    </rewrite-assertion>
+
+    <rewrite-assertion id="create_table_with_index_for_cipher" db-types="MySQL">
+        <input sql="CREATE TABLE t_account_bak (id INT NOT NULL, name VARCHAR(100) NOT NULL DEFAULT '', password VARCHAR(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`), KEY `t_account_bak_amount_idx` (`amount`))" />
+        <output sql="CREATE TABLE t_account_bak (id INT NOT NULL, name VARCHAR(100) NOT NULL DEFAULT '', cipher_password VARCHAR(255) NOT NULL DEFAULT '', assisted_query_password VARCHAR(255) NOT NULL DEFAULT '', plain_password VARCHAR(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`), KEY `t_account_bak_amount_idx` (`cipher_amount`))" />
     </rewrite-assertion>
 </rewrite-assertions>
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-plain/ddl/alter/alter-table.xml b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-plain/ddl/alter/alter-table.xml
index d8664c943bb..0a12dd4d661 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-plain/ddl/alter/alter-table.xml
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-plain/ddl/alter/alter-table.xml
@@ -17,57 +17,62 @@
   -->
 
 <rewrite-assertions yaml-rule="scenario/encrypt/config/query-with-plain.yaml">
-    <rewrite-assertion id="add_column_for_plain" db-types="MySQL">
-        <input sql="ALTER TABLE t_account_bak ADD COLUMN id int not null, ADD COLUMN password varchar(255) not null default ''" />
-        <output sql="ALTER TABLE t_account_bak ADD COLUMN id int not null, ADD COLUMN cipher_password varchar(255) not null default '', ADD COLUMN assisted_query_password varchar(255) not null default '', ADD COLUMN plain_password varchar(255) not null default ''" />
+    <rewrite-assertion id="alter_table_add_column_for_plain" db-types="MySQL">
+        <input sql="ALTER TABLE t_account_bak ADD COLUMN id INT NOT NULL, ADD COLUMN password VARCHAR(255) NOT NULL DEFAULT ''" />
+        <output sql="ALTER TABLE t_account_bak ADD COLUMN id INT NOT NULL, ADD COLUMN cipher_password VARCHAR(255) NOT NULL DEFAULT '', ADD COLUMN assisted_query_password VARCHAR(255) NOT NULL DEFAULT '', ADD COLUMN plain_password VARCHAR(255) NOT NULL DEFAULT ''" />
     </rewrite-assertion>
     
-    <rewrite-assertion id="modify_column_for_plain" db-types="MySQL">
-        <input sql="ALTER TABLE t_account_bak MODIFY COLUMN password varchar(255) not null default ''" />
-        <output sql="ALTER TABLE t_account_bak MODIFY COLUMN cipher_password varchar(255) not null default '', MODIFY COLUMN assisted_query_password varchar(255) not null default '', MODIFY COLUMN plain_password varchar(255) not null default ''" />
+    <rewrite-assertion id="alter_table_modify_column_for_plain" db-types="MySQL">
+        <input sql="ALTER TABLE t_account_bak MODIFY COLUMN password VARCHAR(255) NOT NULL DEFAULT ''" />
+        <output sql="ALTER TABLE t_account_bak MODIFY COLUMN cipher_password VARCHAR(255) NOT NULL DEFAULT '', MODIFY COLUMN assisted_query_password VARCHAR(255) NOT NULL DEFAULT '', MODIFY COLUMN plain_password VARCHAR(255) NOT NULL DEFAULT ''" />
     </rewrite-assertion>
     
-    <rewrite-assertion id="change_column_for_plain" db-types="MySQL">
-        <input sql="ALTER TABLE t_account_bak CHANGE COLUMN password password_new varchar(255) not null default ''" />
-        <output sql="ALTER TABLE t_account_bak CHANGE COLUMN cipher_password password_new_cipher varchar(255) not null default '', CHANGE COLUMN assisted_query_password password_new_assisted varchar(255) not null default '', CHANGE COLUMN plain_password password_new_plain varchar(255) not null default ''" />
+    <rewrite-assertion id="alter_table_change_column_for_plain" db-types="MySQL">
+        <input sql="ALTER TABLE t_account_bak CHANGE COLUMN password password_new VARCHAR(255) NOT NULL DEFAULT ''" />
+        <output sql="ALTER TABLE t_account_bak CHANGE COLUMN cipher_password password_new_cipher VARCHAR(255) NOT NULL DEFAULT '', CHANGE COLUMN assisted_query_password password_new_assisted VARCHAR(255) NOT NULL DEFAULT '', CHANGE COLUMN plain_password password_new_plain VARCHAR(255) NOT NULL DEFAULT ''" />
     </rewrite-assertion>
     
-    <rewrite-assertion id="drop_column_for_plain" db-types="MySQL">
+    <rewrite-assertion id="alter_table_drop_column_for_plain" db-types="MySQL">
         <input sql="ALTER TABLE t_account_bak DROP COLUMN password" />
         <output sql="ALTER TABLE t_account_bak DROP COLUMN cipher_password, DROP COLUMN assisted_query_password, DROP COLUMN plain_password" />
     </rewrite-assertion>
     
-    <rewrite-assertion id="drop_columns_for_plain" db-types="MySQL">
+    <rewrite-assertion id="alter_table_drop_columns_for_plain" db-types="MySQL">
         <input sql="ALTER TABLE t_account_bak DROP COLUMN password, DROP COLUMN amount" />
         <output sql="ALTER TABLE t_account_bak DROP COLUMN cipher_password, DROP COLUMN assisted_query_password, DROP COLUMN plain_password, DROP COLUMN cipher_amount, DROP COLUMN plain_amount" />
     </rewrite-assertion>
     
-    <rewrite-assertion id="drop_mix_columns_for_plain" db-types="MySQL">
+    <rewrite-assertion id="alter_table_drop_mix_columns_for_plain" db-types="MySQL">
         <input sql="ALTER TABLE t_account_bak DROP COLUMN password, DROP COLUMN id" />
         <output sql="ALTER TABLE t_account_bak DROP COLUMN cipher_password, DROP COLUMN assisted_query_password, DROP COLUMN plain_password, DROP COLUMN id" />
     </rewrite-assertion>
 
-    <rewrite-assertion id="modify_column_amount_for_plain" db-types="MySQL">
+    <rewrite-assertion id="alter_table_add_index_for_plain" db-types="MySQL">
+        <input sql="ALTER TABLE t_account_bak ADD INDEX t_account_bak_amount(`amount`)"/>
+        <output sql="ALTER TABLE t_account_bak ADD INDEX t_account_bak_amount(`plain_amount`)"/>
+    </rewrite-assertion>
+
+    <rewrite-assertion id="alter_table_modify_column_amount_for_plain" db-types="MySQL">
         <input sql="ALTER TABLE t_account MODIFY COLUMN amount text"/>
         <output sql="ALTER TABLE t_account MODIFY COLUMN cipher_amount text"/>
     </rewrite-assertion>
 
-    <rewrite-assertion id="change_column_amount_for_plain" db-types="MySQL">
+    <rewrite-assertion id="alter_table_change_column_amount_for_plain" db-types="MySQL">
         <input sql="ALTER TABLE t_account CHANGE COLUMN amount amount_new text"/>
         <output sql="ALTER TABLE t_account CHANGE COLUMN cipher_amount amount_new_cipher text"/>
     </rewrite-assertion>
 
-    <rewrite-assertion id="add_column_with_position_cipher" db-types="MySQL">
+    <rewrite-assertion id="alter_table_add_column_with_position_cipher" db-types="MySQL">
         <input sql="ALTER TABLE t_account ADD COLUMN status text AFTER amount"/>
         <output sql="ALTER TABLE t_account ADD COLUMN status text AFTER cipher_amount"/>
     </rewrite-assertion>
 
-    <rewrite-assertion id="modify_column_with_position_cipher" db-types="MySQL">
+    <rewrite-assertion id="alter_table_modify_column_with_position_cipher" db-types="MySQL">
         <input sql="ALTER TABLE t_account MODIFY COLUMN status text AFTER amount"/>
         <output sql="ALTER TABLE t_account MODIFY COLUMN status text AFTER cipher_amount"/>
     </rewrite-assertion>
 
-    <rewrite-assertion id="add_column_with_cipher" db-types="MySQL">
+    <rewrite-assertion id="alter_table_add_column_with_cipher" db-types="MySQL">
         <input sql="ALTER TABLE t_account ADD COLUMN amount INT"/>
         <output sql="ALTER TABLE t_account ADD COLUMN cipher_amount INT"/>
     </rewrite-assertion>
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-plain/ddl/create/create-table.xml b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-plain/ddl/create/create-table.xml
index db7a8543fbc..b0b2840134b 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-plain/ddl/create/create-table.xml
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/query-with-plain/ddl/create/create-table.xml
@@ -18,7 +18,12 @@
 
 <rewrite-assertions yaml-rule="scenario/encrypt/config/query-with-plain.yaml">
     <rewrite-assertion id="create_table_for_plain" db-types="MySQL">
-        <input sql="CREATE TABLE t_account_bak (id int not null, name varchar(100) not null default '', password varchar(255) not null default '', primary key (`id`))" />
-        <output sql="CREATE TABLE t_account_bak (id int not null, name varchar(100) not null default '', cipher_password varchar(255) not null default '', assisted_query_password varchar(255) not null default '', plain_password varchar(255) not null default '', primary key (`id`))" />
+        <input sql="CREATE TABLE t_account_bak (id INT NOT NULL, name VARCHAR(100) NOT NULL DEFAULT '', password VARCHAR(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`))" />
+        <output sql="CREATE TABLE t_account_bak (id INT NOT NULL, name VARCHAR(100) NOT NULL DEFAULT '', cipher_password VARCHAR(255) NOT NULL DEFAULT '', assisted_query_password VARCHAR(255) NOT NULL DEFAULT '', plain_password VARCHAR(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`))" />
+    </rewrite-assertion>
+
+    <rewrite-assertion id="create_table_with_index_for_plain" db-types="MySQL">
+        <input sql="CREATE TABLE t_account_bak (id INT NOT NULL, name VARCHAR(100) NOT NULL DEFAULT '', password VARCHAR(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`), KEY `t_account_bak_amount_idx` (`amount`))" />
+        <output sql="CREATE TABLE t_account_bak (id INT NOT NULL, name VARCHAR(100) NOT NULL DEFAULT '', cipher_password VARCHAR(255) NOT NULL DEFAULT '', assisted_query_password VARCHAR(255) NOT NULL DEFAULT '', plain_password VARCHAR(255) NOT NULL DEFAULT '', PRIMARY KEY (`id`), KEY `t_account_bak_amount_idx` (`plain_amount`))" />
     </rewrite-assertion>
 </rewrite-assertions>