You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/01/25 09:22:25 UTC

[shardingsphere] branch master updated: Modify rewrite logic to support schema name in projection owner (#15055)

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

panjuan 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 dc27f5c  Modify rewrite logic to support schema name in projection owner (#15055)
dc27f5c is described below

commit dc27f5c3962dd4320ece373a2410ded0f28b23c3
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Tue Jan 25 17:21:26 2022 +0800

    Modify rewrite logic to support schema name in projection owner (#15055)
    
    * Modify rewrite logic to support schema name in projection owner
    
    * optimize code style
    
    * remove useless metadata in rewrite test case
---
 .../impl/EncryptProjectionTokenGenerator.java        |  5 +++--
 .../parser/sql/common/extractor/TableExtractor.java  | 12 ++++++++----
 .../engine/AbstractSQLRewriterParameterizedTest.java |  1 -
 .../encrypt/case/select_for_query_with_cipher.xml    | 10 ++++++++++
 .../encrypt/case/select_for_query_with_plain.xml     | 10 ++++++++++
 .../mix/case/select_for_query_with_cipher.xml        | 10 ++++++++++
 .../mix/case/select_for_query_with_plain.xml         | 10 ++++++++++
 .../test/resources/scenario/sharding/case/select.xml | 20 +++++++++++++++-----
 8 files changed, 66 insertions(+), 12 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptProjectionTokenGenerator.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptProjectionTokenGenerator.java
index b911cb6..01015ad 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptProjectionTokenGenerator.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptProjectionTokenGenerator.java
@@ -114,8 +114,9 @@ public final class EncryptProjectionTokenGenerator extends BaseEncryptSQLTokenGe
                 projections.add(new ColumnProjection(each.getOwner(), each.getName(), each.getAlias().orElse(null)));
             }
         }
-        previousSQLTokens.removeIf(each -> each.getStartIndex() == segment.getStartIndex());
-        return new SubstitutableColumnNameToken(segment.getStartIndex(), segment.getStopIndex(), projections, databaseType.getQuoteCharacter());
+        int startIndex = segment.getOwner().isPresent() ? segment.getOwner().get().getStartIndex() : segment.getStartIndex();
+        previousSQLTokens.removeIf(each -> each.getStartIndex() == startIndex);
+        return new SubstitutableColumnNameToken(startIndex, segment.getStopIndex(), projections, databaseType.getQuoteCharacter());
     }
 
     private ColumnProjection buildColumnProjection(final ColumnProjectionSegment segment) {
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractor.java
index 252dd40..1d374a6 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractor.java
@@ -159,19 +159,23 @@ public final class TableExtractor {
             } else if (each instanceof OwnerAvailable) {
                 if (((OwnerAvailable) each).getOwner().isPresent() && needRewrite(((OwnerAvailable) each).getOwner().get())) {
                     OwnerSegment ownerSegment = ((OwnerAvailable) each).getOwner().get();
-                    rewriteTables.add(new SimpleTableSegment(new TableNameSegment(ownerSegment.getStartIndex(), ownerSegment.getStopIndex(), ownerSegment.getIdentifier())));
+                    rewriteTables.add(createSimpleTableSegment(ownerSegment));
                 }
             } else if (each instanceof ColumnProjectionSegment) {
                 if (((ColumnProjectionSegment) each).getColumn().getOwner().isPresent() && needRewrite(((ColumnProjectionSegment) each).getColumn().getOwner().get())) {
                     OwnerSegment ownerSegment = ((ColumnProjectionSegment) each).getColumn().getOwner().get();
-                    SimpleTableSegment simpleTable = new SimpleTableSegment(new TableNameSegment(ownerSegment.getStartIndex(), ownerSegment.getStopIndex(), ownerSegment.getIdentifier()));
-                    ownerSegment.getOwner().ifPresent(simpleTable::setOwner);
-                    rewriteTables.add(simpleTable);
+                    rewriteTables.add(createSimpleTableSegment(ownerSegment));
                 }
             }
         }
     }
     
+    private SimpleTableSegment createSimpleTableSegment(final OwnerSegment ownerSegment) {
+        SimpleTableSegment result = new SimpleTableSegment(new TableNameSegment(ownerSegment.getStartIndex(), ownerSegment.getStopIndex(), ownerSegment.getIdentifier()));
+        ownerSegment.getOwner().ifPresent(result::setOwner);
+        return result;
+    }
+    
     private void extractTablesFromOrderByItems(final Collection<OrderByItemSegment> orderByItems) {
         for (OrderByItemSegment each : orderByItems) {
             if (each instanceof ColumnOrderByItemSegment) {
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 ed1f179..b89982d 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
@@ -99,7 +99,6 @@ public abstract class AbstractSQLRewriterParameterizedTest {
         ShardingSphereMetaData metaData = new ShardingSphereMetaData("sharding_db", mock(ShardingSphereResource.class), new ShardingSphereRuleMetaData(Collections.emptyList(), rules), schema);
         Map<String, ShardingSphereMetaData> metaDataMap = new HashMap<>(2, 1);
         metaDataMap.put(DefaultSchema.LOGIC_NAME, metaData);
-        metaDataMap.put("sharding_db", metaData);
         SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(metaDataMap, getTestParameters().getInputParameters(),
                 sqlStatementParserEngine.parse(getTestParameters().getInputSQL(), false), DefaultSchema.LOGIC_NAME);
         LogicSQL logicSQL = new LogicSQL(sqlStatementContext, getTestParameters().getInputSQL(), getTestParameters().getInputParameters());
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/select_for_query_with_cipher.xml b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/select_for_query_with_cipher.xml
index d897073..db868b2 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/select_for_query_with_cipher.xml
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/select_for_query_with_cipher.xml
@@ -208,4 +208,14 @@
         <input sql="SELECT COUNT(1) AS cnt FROM (SELECT a.amount FROM t_account a ORDER BY a.amount DESC ) AS tmp" />
         <output sql="SELECT COUNT(1) AS cnt FROM (SELECT a.cipher_amount FROM t_account a ORDER BY a.cipher_amount DESC ) AS tmp" />
     </rewrite-assertion>
+
+    <rewrite-assertion id="select_with_schema_name_in_shorthand_projection" db-types="MySQL">
+        <input sql="SELECT logic_db.t_account.* FROM t_account WHERE account_id = ?" parameters="100" />
+        <output sql="SELECT `t_account`.`account_id`, `t_account`.`cipher_certificate_number` AS `certificate_number`, `t_account`.`cipher_password` AS `password`, `t_account`.`cipher_amount` AS `amount`, `t_account`.`status` FROM t_account WHERE account_id = ?" parameters="100" />
+    </rewrite-assertion>
+
+    <rewrite-assertion id="select_with_schema_name_in_column_projection" db-types="MySQL">
+        <input sql="SELECT logic_db.t_account.account_id FROM t_account WHERE account_id = ?" parameters="100" />
+        <output sql="SELECT t_account.account_id FROM t_account WHERE account_id = ?" parameters="100" />
+    </rewrite-assertion>
 </rewrite-assertions>
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/select_for_query_with_plain.xml b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/select_for_query_with_plain.xml
index 9fd97db..283416b 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/select_for_query_with_plain.xml
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/encrypt/case/select_for_query_with_plain.xml
@@ -61,4 +61,14 @@
         <input sql="SELECT a.account_id, a.password, a.amount AS a, a.status AS s FROM t_account_detail AS a WHERE a.account_id = 1 AND a.password = 'aaa' AND a.amount = 1000 AND a.status = 'OK'" />
         <output sql="SELECT a.account_id, a.cipher_password AS password, a.cipher_amount AS a, a.status AS s FROM t_account_detail AS a WHERE a.account_id = 1 AND a.assisted_query_password = 'assisted_query_aaa' AND a.cipher_amount = 'encrypt_1000' AND a.status = 'OK'" />
     </rewrite-assertion>
+
+    <rewrite-assertion id="select_with_schema_name_in_shorthand_projection" db-types="MySQL">
+        <input sql="SELECT logic_db.t_account.* FROM t_account WHERE account_id = ?" parameters="100" />
+        <output sql="SELECT `t_account`.`account_id`, `t_account`.`cipher_certificate_number` AS `certificate_number`, `t_account`.`cipher_password` AS `password`, `t_account`.`cipher_amount` AS `amount`, `t_account`.`status` FROM t_account WHERE account_id = ?" parameters="100" />
+    </rewrite-assertion>
+
+    <rewrite-assertion id="select_with_schema_name_in_column_projection" db-types="MySQL">
+        <input sql="SELECT logic_db.t_account.account_id FROM t_account WHERE account_id = ?" parameters="100" />
+        <output sql="SELECT t_account.account_id FROM t_account WHERE account_id = ?" parameters="100" />
+    </rewrite-assertion>
 </rewrite-assertions>
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/mix/case/select_for_query_with_cipher.xml b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/mix/case/select_for_query_with_cipher.xml
index cbf9b98d..3786cb1 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/mix/case/select_for_query_with_cipher.xml
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/mix/case/select_for_query_with_cipher.xml
@@ -111,4 +111,14 @@
         <input sql="SELECT t_account.* FROM t_account" />
         <output sql="SELECT `t_account_0`.`account_id`, `t_account_0`.`cipher_password` AS `password`, `t_account_0`.`cipher_amount` AS `amount`, `t_account_0`.`status` FROM t_account_0 UNION ALL SELECT `t_account_1`.`account_id`, `t_account_1`.`cipher_password` AS `password`, `t_account_1`.`cipher_amount` AS `amount`, `t_account_1`.`status` FROM t_account_1" />
     </rewrite-assertion>
+
+    <rewrite-assertion id="select_with_schema_name_in_shorthand_projection" db-types="MySQL">
+        <input sql="SELECT logic_db.t_account.* FROM t_account WHERE account_id = ?" parameters="100" />
+        <output sql="SELECT `t_account_0`.`account_id`, `t_account_0`.`cipher_password` AS `password`, `t_account_0`.`cipher_amount` AS `amount`, `t_account_0`.`status` FROM t_account_0 WHERE account_id = ?" parameters="100" />
+    </rewrite-assertion>
+
+    <rewrite-assertion id="select_with_schema_name_in_column_projection" db-types="MySQL">
+        <input sql="SELECT logic_db.t_account.account_id FROM t_account WHERE account_id = ?" parameters="100" />
+        <output sql="SELECT t_account_0.account_id FROM t_account_0 WHERE account_id = ?" parameters="100" />
+    </rewrite-assertion>
 </rewrite-assertions>
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/mix/case/select_for_query_with_plain.xml b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/mix/case/select_for_query_with_plain.xml
index 671f790..68aece2 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/mix/case/select_for_query_with_plain.xml
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/mix/case/select_for_query_with_plain.xml
@@ -46,4 +46,14 @@
         <input sql="SELECT t_account_bak.* FROM t_account_bak" />
         <output sql="SELECT `t_account_bak_0`.`account_id`, `t_account_bak_0`.`plain_password` AS `password`, `t_account_bak_0`.`plain_amount` AS `amount`, `t_account_bak_0`.`status` FROM t_account_bak_0 UNION ALL SELECT `t_account_bak_1`.`account_id`, `t_account_bak_1`.`plain_password` AS `password`, `t_account_bak_1`.`plain_amount` AS `amount`, `t_account_bak_1`.`status` FROM t_account_bak_1" />
     </rewrite-assertion>
+
+    <rewrite-assertion id="select_with_schema_name_in_shorthand_projection" db-types="MySQL">
+        <input sql="SELECT logic_db.t_account.* FROM t_account WHERE account_id = ?" parameters="100" />
+        <output sql="SELECT `t_account_0`.`account_id`, `t_account_0`.`cipher_password` AS `password`, `t_account_0`.`cipher_amount` AS `amount`, `t_account_0`.`status` FROM t_account_0 WHERE account_id = ?" parameters="100" />
+    </rewrite-assertion>
+
+    <rewrite-assertion id="select_with_schema_name_in_column_projection" db-types="MySQL">
+        <input sql="SELECT logic_db.t_account.account_id FROM t_account WHERE account_id = ?" parameters="100" />
+        <output sql="SELECT t_account_0.account_id FROM t_account_0 WHERE account_id = ?" parameters="100" />
+    </rewrite-assertion>
 </rewrite-assertions>
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 fcc1182..e3db081 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
@@ -79,7 +79,7 @@
     </rewrite-assertion>
 
     <rewrite-assertion id="select_with_schema">
-        <input sql="SELECT * FROM sharding_db.t_account" />
+        <input sql="SELECT * FROM logic_db.t_account" />
         <output sql="SELECT * FROM t_account_0 UNION ALL SELECT * FROM t_account_1"/>
     </rewrite-assertion>
 
@@ -119,22 +119,22 @@
     </rewrite-assertion>
     
     <rewrite-assertion id="select_without_sharding_value_for_parameters">
-        <input sql="SELECT * FROM sharding_db.t_account WHERE amount = ?" parameters="1000" />
+        <input sql="SELECT * FROM logic_db.t_account WHERE amount = ?" parameters="1000" />
         <output sql="SELECT * FROM t_account_0 WHERE amount = ? UNION ALL SELECT * FROM t_account_1 WHERE amount = ?" parameters="1000, 1000" />
     </rewrite-assertion>
     
     <rewrite-assertion id="select_without_sharding_value_for_literals">
-        <input sql="SELECT * FROM sharding_db.t_account WHERE amount = 1000" />
+        <input sql="SELECT * FROM logic_db.t_account WHERE amount = 1000" />
         <output sql="SELECT * FROM t_account_0 WHERE amount = 1000 UNION ALL SELECT * FROM t_account_1 WHERE amount = 1000" />
     </rewrite-assertion>
     
     <rewrite-assertion id="select_without_sharding_value_with_dollar_parameter_marker_for_parameters" db-types="PostgreSQL,openGauss">
-        <input sql="SELECT * FROM sharding_db.t_account WHERE amount = $2 OR amount = $1" parameters="900, 1000" />
+        <input sql="SELECT * FROM logic_db.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" />
     </rewrite-assertion>
 
     <rewrite-assertion id="select_without_sharding_value_with_dollar_parameter_marker_for_literals" db-types="PostgreSQL,openGauss">
-        <input sql="SELECT * FROM sharding_db.t_account WHERE amount = 1000 OR amount = 900" />
+        <input sql="SELECT * FROM logic_db.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" />
     </rewrite-assertion>
 
@@ -572,4 +572,14 @@
         <input sql="SELECT * FROM t_account WHERE account_id = BINARY 100" />
         <output sql="SELECT * FROM t_account_0 WHERE account_id = BINARY 100" />
     </rewrite-assertion>
+
+    <rewrite-assertion id="select_with_schema_name_in_shorthand_projection" db-types="MySQL">
+        <input sql="SELECT logic_db.t_account.* FROM t_account WHERE account_id = ?" parameters="100" />
+        <output sql="SELECT t_account_0.* FROM t_account_0 WHERE account_id = ?" parameters="100" />
+    </rewrite-assertion>
+
+    <rewrite-assertion id="select_with_schema_name_in_column_projection" db-types="MySQL">
+        <input sql="SELECT logic_db.t_account.account_id FROM t_account WHERE account_id = ?" parameters="100" />
+        <output sql="SELECT t_account_0.account_id FROM t_account_0 WHERE account_id = ?" parameters="100" />
+    </rewrite-assertion>
 </rewrite-assertions>