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

[shardingsphere] branch master updated: Fix #9576 (#9601)

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

menghaoran 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 a3bd7b6  Fix #9576 (#9601)
a3bd7b6 is described below

commit a3bd7b60787d3ea104251002f582aabdf876fbc7
Author: Liang Zhang <te...@163.com>
AuthorDate: Sat Mar 6 13:03:39 2021 +0800

    Fix #9576 (#9601)
    
    * For xml format
    
    * Fix #9576
---
 .../impl/EncryptProjectionTokenGenerator.java       |  6 +++++-
 .../select/projection/engine/ProjectionEngine.java  | 18 +++++++++++++++---
 .../select/projection/impl/ShorthandProjection.java |  2 ++
 ...ection.java => UnexpandShorthandProjection.java} | 21 ++++-----------------
 .../select/projection/ProjectionsContextTest.java   |  6 +++---
 .../projection/impl/ShorthandProjectionTest.java    |  8 ++++----
 .../cases/dql/dql-integration-test-cases.xml        | 11 ++++++-----
 7 files changed, 39 insertions(+), 33 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 0eee6de..4863695 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
@@ -26,6 +26,7 @@ import org.apache.shardingsphere.infra.binder.segment.select.projection.Projecti
 import org.apache.shardingsphere.infra.binder.segment.select.projection.ProjectionsContext;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ColumnProjection;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ShorthandProjection;
+import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.UnexpandShorthandProjection;
 import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
 import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
@@ -77,7 +78,7 @@ public final class EncryptProjectionTokenGenerator extends BaseEncryptSQLTokenGe
             }
             if (isToGeneratedSQLToken(each, selectStatementContext, tableName)) {
                 ShorthandProjection shorthandProjection = getShorthandProjection((ShorthandProjectionSegment) each, selectStatementContext.getProjectionsContext());
-                if (!shorthandProjection.getActualColumns().isEmpty()) {
+                if (!shorthandProjection.getActualColumns().isEmpty() || !shorthandProjection.getUnexpandShorthandProjections().isEmpty()) {
                     result.add(generateSQLToken((ShorthandProjectionSegment) each, shorthandProjection, tableName, encryptTable, selectStatementContext.getDatabaseType()));
                 }
             }
@@ -114,6 +115,9 @@ public final class EncryptProjectionTokenGenerator extends BaseEncryptSQLTokenGe
                 shorthandExtensionProjections.add(null == each.getOwner() ? quoteCharacter.wrap(each.getName()) : quoteCharacter.wrap(each.getOwner()) + "." + quoteCharacter.wrap(each.getName()));
             }
         }
+        for (UnexpandShorthandProjection each : shorthandProjection.getUnexpandShorthandProjections()) {
+            shorthandExtensionProjections.add(each.getColumnLabel());
+        }
         return new SubstitutableColumnNameToken(segment.getStartIndex(), segment.getStopIndex(), Joiner.on(", ").join(shorthandExtensionProjections));
     }
     
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngine.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngine.java
index 94834a8..4afbf80 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngine.java
@@ -18,7 +18,6 @@
 package org.apache.shardingsphere.infra.binder.segment.select.projection.engine;
 
 import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.DerivedColumn;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.Projection;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationDistinctProjection;
@@ -26,6 +25,8 @@ import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.Agg
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ColumnProjection;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ExpressionProjection;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ShorthandProjection;
+import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.UnexpandShorthandProjection;
+import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
 import org.apache.shardingsphere.sql.parser.sql.common.constant.AggregationType;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationDistinctProjectionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment;
@@ -82,8 +83,9 @@ public final class ProjectionEngine {
     
     private ShorthandProjection createProjection(final Collection<SimpleTableSegment> tableSegments, final ShorthandProjectionSegment projectionSegment) {
         String owner = projectionSegment.getOwner().map(ownerSegment -> ownerSegment.getIdentifier().getValue()).orElse(null);
-        Collection<ColumnProjection> columns = getShorthandColumns(tableSegments, owner);
-        return new ShorthandProjection(owner, columns);
+        Collection<ColumnProjection> shorthandColumns = getShorthandColumns(tableSegments, owner);
+        Collection<UnexpandShorthandProjection> unexpandShorthandProjections = getUnexpandShorthandColumns(projectionSegment, tableSegments);
+        return new ShorthandProjection(owner, shorthandColumns, unexpandShorthandProjections);
     }
     
     private ColumnProjection createProjection(final ColumnProjectionSegment projectionSegment) {
@@ -140,6 +142,16 @@ public final class ProjectionEngine {
         return Collections.emptyList();
     }
     
+    private Collection<UnexpandShorthandProjection> getUnexpandShorthandColumns(final ShorthandProjectionSegment projectionSegment, final Collection<SimpleTableSegment> tables) {
+        return tables.stream().filter(each -> isMatch(projectionSegment, each) && schema.getAllColumnNames(each.getTableName().getIdentifier().getValue()).isEmpty())
+            .map(each -> new UnexpandShorthandProjection(each.getAlias().orElse(each.getTableName().getIdentifier().getValue()))).collect(Collectors.toList());
+    }
+    
+    private boolean isMatch(final ShorthandProjectionSegment projectionSegment, final SimpleTableSegment tableSegment) {
+        return !projectionSegment.getOwner().isPresent()
+                || tableSegment.getAlias().orElse(tableSegment.getTableName().getIdentifier().getValue()).equals(projectionSegment.getOwner().get().getIdentifier().getValue());
+    }
+    
     private void appendAverageDistinctDerivedProjection(final AggregationDistinctProjection averageDistinctProjection) {
         String innerExpression = averageDistinctProjection.getInnerExpression();
         String distinctInnerExpression = averageDistinctProjection.getDistinctInnerExpression();
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ShorthandProjection.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ShorthandProjection.java
index 91246e0..a72ec9c 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ShorthandProjection.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ShorthandProjection.java
@@ -40,6 +40,8 @@ public final class ShorthandProjection implements Projection {
     
     private final Collection<ColumnProjection> actualColumns;
     
+    private final Collection<UnexpandShorthandProjection> unexpandShorthandProjections;
+    
     @Override
     public String getExpression() {
         return Strings.isNullOrEmpty(owner) ? "*" : owner + ".*";
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ShorthandProjection.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/UnexpandShorthandProjection.java
similarity index 75%
copy from shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ShorthandProjection.java
copy to shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/UnexpandShorthandProjection.java
index 91246e0..691a600 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ShorthandProjection.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/UnexpandShorthandProjection.java
@@ -17,32 +17,28 @@
 
 package org.apache.shardingsphere.infra.binder.segment.select.projection.impl;
 
-import com.google.common.base.Strings;
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 import lombok.ToString;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.Projection;
 
-import java.util.Collection;
 import java.util.Optional;
 
 /**
- * Shorthand projection.
+ * Unexpand shorthand projection.
  */
 @RequiredArgsConstructor
 @Getter
 @EqualsAndHashCode
 @ToString
-public final class ShorthandProjection implements Projection {
+public final class UnexpandShorthandProjection implements Projection {
     
     private final String owner;
     
-    private final Collection<ColumnProjection> actualColumns;
-    
     @Override
     public String getExpression() {
-        return Strings.isNullOrEmpty(owner) ? "*" : owner + ".*";
+        return owner + ".*";
     }
     
     @Override
@@ -52,15 +48,6 @@ public final class ShorthandProjection implements Projection {
     
     @Override
     public String getColumnLabel() {
-        return "*";
-    }
-    
-    /**
-     * Get owner.
-     * 
-     * @return owner
-     */
-    public Optional<String> getOwner() {
-        return Optional.ofNullable(owner);
+        return getExpression();
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/ProjectionsContextTest.java b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/ProjectionsContextTest.java
index 669fa59..023b9ee 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/ProjectionsContextTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/ProjectionsContextTest.java
@@ -57,7 +57,7 @@ public final class ProjectionsContextTest {
     
     @Test
     public void assertUnqualifiedShorthandProjection() {
-        Projection projection = new ShorthandProjection(null, Collections.emptyList());
+        Projection projection = new ShorthandProjection(null, Collections.emptyList(), Collections.emptyList());
         ProjectionsContext projectionsContext = new ProjectionsContext(0, 0, true, Collections.singleton(projection));
         assertTrue(projectionsContext.isUnqualifiedShorthandProjection());
     }
@@ -109,7 +109,7 @@ public final class ProjectionsContextTest {
     }
     
     private ShorthandProjection getShorthandProjection() {
-        return new ShorthandProjection("table", Collections.emptyList());
+        return new ShorthandProjection("table", Collections.emptyList(), Collections.emptyList());
     }
     
     private ColumnProjection getColumnProjection() {
@@ -134,7 +134,7 @@ public final class ProjectionsContextTest {
         ColumnProjection columnProjection2 = new ColumnProjection(null, "col2", null);
         ColumnProjection columnProjection3 = new ColumnProjection(null, "col3", null);
         DerivedProjection derivedProjection = new DerivedProjection("col3", "a3", null);
-        ShorthandProjection shorthandProjection = new ShorthandProjection(null, Arrays.asList(columnProjection2, columnProjection3));
+        ShorthandProjection shorthandProjection = new ShorthandProjection(null, Arrays.asList(columnProjection2, columnProjection3), Collections.emptyList());
         ProjectionsContext actual = new ProjectionsContext(0, 0, false, Arrays.asList(columnProjection1, shorthandProjection, derivedProjection));
         assertThat(actual.getExpandProjections().size(), is(3));
         assertThat(actual.getExpandProjections().get(0), is(columnProjection1));
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ShorthandProjectionTest.java b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ShorthandProjectionTest.java
index 966c78a..e0897d2 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ShorthandProjectionTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ShorthandProjectionTest.java
@@ -30,21 +30,21 @@ public final class ShorthandProjectionTest {
     
     @Test
     public void assertGetExpression() {
-        assertThat(new ShorthandProjection("owner", Collections.emptyList()).getExpression(), is("owner.*"));
+        assertThat(new ShorthandProjection("owner", Collections.emptyList(), Collections.emptyList()).getExpression(), is("owner.*"));
     }
     
     @Test
     public void assertGetAliasWhenAbsent() {
-        assertFalse(new ShorthandProjection("owner", Collections.emptyList()).getAlias().isPresent());
+        assertFalse(new ShorthandProjection("owner", Collections.emptyList(), Collections.emptyList()).getAlias().isPresent());
     }
     
     @Test
     public void assertGetColumnLabel() {
-        assertTrue(new ShorthandProjection("owner", Collections.emptyList()).getColumnLabel().contains("*"));
+        assertTrue(new ShorthandProjection("owner", Collections.emptyList(), Collections.emptyList()).getColumnLabel().contains("*"));
     }
     
     @Test
     public void assertContains() {
-        assertTrue(new ShorthandProjection("owner", Collections.emptyList()).getOwner().isPresent());
+        assertTrue(new ShorthandProjection("owner", Collections.emptyList(), Collections.emptyList()).getOwner().isPresent());
     }
 }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/dql/dql-integration-test-cases.xml b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/dql/dql-integration-test-cases.xml
index 61dd382..8dd564c 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/dql/dql-integration-test-cases.xml
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/cases/dql/dql-integration-test-cases.xml
@@ -490,23 +490,24 @@
     <test-case sql="SELECT * FROM t_user" scenario-types="encrypt">
         <assertion expected-data-file="select_encrypt.xml"/>
     </test-case>
-
+    
     <test-case sql="SELECT user_id, pwd FROM t_user WHERE pwd = 'abc'" scenario-types="encrypt">
         <assertion expected-data-file="select_by_encrypt.xml"/>
     </test-case>
-
+    
     <test-case sql="SELECT user_id FROM t_user WHERE pwd in ('abc', 'def')" scenario-types="encrypt">
         <assertion expected-data-file="select_in_encrypt.xml"/>
     </test-case>
-
+    
     <test-case sql="SELECT user_id FROM t_user WHERE pwd in ('abc', 'def') and user_id = 10" scenario-types="encrypt">
         <assertion expected-data-file="select_in_encrypt_without_pwd.xml"/>
     </test-case>
-
+    
     <test-case sql="SELECT * FROM t_user WHERE pwd in ('abc', 'def') OR user_id = 10" scenario-types="encrypt">
         <assertion expected-data-file="select_in_or_encrypt.xml"/>
     </test-case>
-    <!--TODO fix me! about issue #9576 -->
+    
+    <!-- TODO dataset assertion mismatch -->
 <!--    <test-case sql="SELECT * FROM t_user u INNER JOIN t_user_item m ON u.user_id=m.user_id" scenario-types="encrypt">-->
 <!--        <assertion expected-data-file="select_join_encrypt.xml"/>-->
 <!--    </test-case>-->