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 2020/06/18 08:34:22 UTC
[shardingsphere] branch master updated: Fix encrypt + sharding
rewriting (#6112)
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 310d200 Fix encrypt + sharding rewriting (#6112)
310d200 is described below
commit 310d20030ed773d61deade4660fbfd3b91d77611
Author: Juan Pan(Trista) <pa...@apache.org>
AuthorDate: Thu Jun 18 16:33:37 2020 +0800
Fix encrypt + sharding rewriting (#6112)
* Fix encrypt + sharding rewriting
* remove invalid test
---
.../generator/impl/EncryptProjectionTokenGenerator.java | 11 ++++++++++-
.../test/resources/encrypt/select_for_query_with_cipher.xml | 2 +-
.../src/test/resources/mix/select_for_query_with_cipher.xml | 12 ++++++++++++
.../sql/parser/binder/segment/table/TablesContext.java | 7 ++++++-
4 files changed, 29 insertions(+), 3 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptProjectionTokenGenerator.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptProjectionTokenGenerator.java
index 6c1eb72..c1968fe 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptProjectionTokenGenerator.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptProjectionTokenGenerator.java
@@ -34,6 +34,7 @@ import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ProjectionsSegm
import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ShorthandProjectionSegment;
import org.apache.shardingsphere.infra.rewrite.sql.token.generator.CollectionSQLTokenGenerator;
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.generic.SubstitutableColumnNameToken;
+import org.apache.shardingsphere.sql.parser.sql.segment.generic.OwnerSegment;
import java.util.Collection;
import java.util.Collections;
@@ -72,13 +73,21 @@ public final class EncryptProjectionTokenGenerator extends BaseEncryptSQLTokenGe
result.add(generateSQLToken((ColumnProjectionSegment) each, tableName));
}
}
- if (each instanceof ShorthandProjectionSegment) {
+ if (isToGeneratedSQLToken(each, selectStatementContext, tableName)) {
result.add(generateSQLToken((ShorthandProjectionSegment) each, selectStatementContext.getProjectionsContext(), tableName, encryptTable));
}
}
return result;
}
+ private boolean isToGeneratedSQLToken(final ProjectionSegment projectionSegment, final SelectStatementContext selectStatementContext, final String tableName) {
+ if (!(projectionSegment instanceof ShorthandProjectionSegment)) {
+ return false;
+ }
+ Optional<OwnerSegment> ownerSegment = ((ShorthandProjectionSegment) projectionSegment).getOwner();
+ return ownerSegment.map(segment -> selectStatementContext.getTablesContext().findTableNameFromSQL(segment.getIdentifier().getValue()).equalsIgnoreCase(tableName)).orElse(true);
+ }
+
private SubstitutableColumnNameToken generateSQLToken(final ColumnProjectionSegment segment, final String tableName) {
String encryptColumnName = getEncryptColumnName(tableName, segment.getColumn().getIdentifier().getValue());
if (!segment.getAlias().isPresent()) {
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/resources/encrypt/select_for_query_with_cipher.xml b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/resources/encrypt/select_for_query_with_cipher.xml
index 864c945..ade46e3 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/resources/encrypt/select_for_query_with_cipher.xml
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/resources/encrypt/select_for_query_with_cipher.xml
@@ -68,7 +68,7 @@
<input sql="SELECT a.* FROM t_account a" />
<output sql="SELECT a.account_id, a.cipher_certificate_number AS certificate_number, a.cipher_password AS password, a.cipher_amount AS amount, a.status FROM t_account a" />
</rewrite-assertion>
-
+
<rewrite-assertion id="select_with_mix_qualified_shorthand_and_other_projection">
<input sql="SELECT a.*, account_id, 1+1 FROM t_account a" />
<output sql="SELECT a.account_id, a.cipher_certificate_number AS certificate_number, a.cipher_password AS password, a.cipher_amount AS amount, a.status, account_id, 1+1 FROM t_account a" />
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/mix/select_for_query_with_cipher.xml b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/mix/select_for_query_with_cipher.xml
index a38c4cc..775d03a 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/mix/select_for_query_with_cipher.xml
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/resources/mix/select_for_query_with_cipher.xml
@@ -53,6 +53,18 @@
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS amount, a.status FROM t_account_0 a" />
<output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS amount, a.status FROM t_account_1 a" />
</rewrite-assertion>
+
+ <rewrite-assertion id="select_with_sharding_qualified_shorthand_join_table">
+ <input sql="SELECT b.* FROM t_account a, t_account_detail b where a.password = b.password" />
+ <output sql="SELECT b.* FROM t_account_0 a, t_account_detail_0 b where a.assisted_query_password = b.password" />
+ <output sql="SELECT b.* FROM t_account_1 a, t_account_detail_1 b where a.assisted_query_password = b.password" />
+ </rewrite-assertion>
+
+ <rewrite-assertion id="select_with_encrypt_qualified_shorthand_join_table">
+ <input sql="SELECT a.* FROM t_account a, t_account_detail b where a.password = b.password" />
+ <output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS amount, a.status FROM t_account_0 a, t_account_detail_0 b where a.assisted_query_password = b.password" />
+ <output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS amount, a.status FROM t_account_1 a, t_account_detail_1 b where a.assisted_query_password = b.password" />
+ </rewrite-assertion>
<rewrite-assertion id="select_with_mix_qualified_shorthand_and_other_projection">
<input sql="SELECT a.*, account_id, 1+1 FROM t_account a" />
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/segment/table/TablesContext.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/segment/table/TablesContext.java
index aa47a11..2054fb9 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/segment/table/TablesContext.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/segment/table/TablesContext.java
@@ -89,7 +89,12 @@ public final class TablesContext {
return findTableNameFromMetaData(column.getName(), schemaMetaData);
}
- private String findTableNameFromSQL(final String tableNameOrAlias) {
+ /**
+ * Find table name from SQL.
+ * @param tableNameOrAlias table name or alias
+ * @return table name
+ */
+ public String findTableNameFromSQL(final String tableNameOrAlias) {
for (SimpleTableSegment each : tables) {
if (tableNameOrAlias.equalsIgnoreCase(each.getTableName().getIdentifier().getValue()) || tableNameOrAlias.equals(each.getAlias().orElse(null))) {
return each.getTableName().getIdentifier().getValue();