You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by tu...@apache.org on 2022/11/23 02:27:29 UTC

[shardingsphere] branch master updated: Revise pr 21768 (#22348)

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

tuichenchuxin 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 77c234b198a Revise pr 21768 (#22348)
77c234b198a is described below

commit 77c234b198ae3bae382f6ecf4d99b734567e754d
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Wed Nov 23 10:27:22 2022 +0800

    Revise pr 21768 (#22348)
    
    * Revise pr 21768
    
    * optimize code style
---
 .../generator/EncryptProjectionTokenGenerator.java | 14 ++++----
 .../select/projection/ProjectionsContext.java      |  6 ++--
 .../select/projection/engine/ProjectionEngine.java |  9 +++--
 .../engine/ProjectionsContextEngine.java           |  2 +-
 .../projection/impl/ShorthandProjection.java       | 26 +++++++-------
 .../projection/engine/ProjectionEngineTest.java    | 42 ++++++++++++----------
 6 files changed, 52 insertions(+), 47 deletions(-)

diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptProjectionTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptProjectionTokenGenerator.java
index a99f7d3f358..887533bac60 100644
--- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptProjectionTokenGenerator.java
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptProjectionTokenGenerator.java
@@ -19,8 +19,8 @@ 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.rewrite.aware.EncryptRuleAware;
+import org.apache.shardingsphere.encrypt.rule.EncryptRule;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.Projection;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.ProjectionsContext;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ColumnProjection;
@@ -92,9 +92,9 @@ public final class EncryptProjectionTokenGenerator implements CollectionSQLToken
             }
             if (projection instanceof ShorthandProjectionSegment) {
                 ShorthandProjectionSegment shorthandSegment = (ShorthandProjectionSegment) projection;
-                Collection<ColumnProjection> actualColumns = getShorthandProjection(shorthandSegment, selectStatementContext.getProjectionsContext()).getActualColumns().values();
-                if (!actualColumns.isEmpty()) {
-                    result.add(generateSQLToken(shorthandSegment, actualColumns, selectStatementContext.getDatabaseType(), subqueryType, columnTableNames));
+                Collection<ColumnProjection> columnProjections = getShorthandProjection(shorthandSegment, selectStatementContext.getProjectionsContext()).getColumnProjections();
+                if (!columnProjections.isEmpty()) {
+                    result.add(generateSQLToken(shorthandSegment, columnProjections, selectStatementContext.getDatabaseType(), subqueryType, columnTableNames));
                 }
             }
         }
@@ -108,10 +108,10 @@ public final class EncryptProjectionTokenGenerator implements CollectionSQLToken
         return new SubstitutableColumnNameToken(startIndex, stopIndex, projections);
     }
     
-    private SubstitutableColumnNameToken generateSQLToken(final ShorthandProjectionSegment segment, final Collection<ColumnProjection> actualColumns,
+    private SubstitutableColumnNameToken generateSQLToken(final ShorthandProjectionSegment segment, final Collection<ColumnProjection> columnProjections,
                                                           final DatabaseType databaseType, final SubqueryType subqueryType, final Map<String, String> columnTableNames) {
         List<ColumnProjection> projections = new LinkedList<>();
-        for (ColumnProjection each : actualColumns) {
+        for (ColumnProjection each : columnProjections) {
             String tableName = columnTableNames.get(each.getExpression());
             if (null == tableName || !encryptRule.findEncryptor(tableName, each.getName()).isPresent()) {
                 projections.add(new ColumnProjection(each.getOwner(), each.getName(), each.getAlias().orElse(null)));
@@ -136,7 +136,7 @@ public final class EncryptProjectionTokenGenerator implements CollectionSQLToken
                 columns.add((ColumnProjection) projection);
             }
             if (projection instanceof ShorthandProjection) {
-                columns.addAll(((ShorthandProjection) projection).getActualColumns().values());
+                columns.addAll(((ShorthandProjection) projection).getColumnProjections());
             }
         }
         String defaultSchema = DatabaseTypeEngine.getDefaultSchemaName(selectStatementContext.getDatabaseType(), databaseName);
diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/ProjectionsContext.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/ProjectionsContext.java
index a5cc6c6cb4f..97784c192e6 100644
--- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/ProjectionsContext.java
+++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/ProjectionsContext.java
@@ -78,7 +78,7 @@ public final class ProjectionsContext {
         List<Projection> result = new ArrayList<>();
         for (Projection each : projections) {
             if (each instanceof ShorthandProjection) {
-                result.addAll(((ShorthandProjection) each).getResultSetColumns().values());
+                result.addAll(((ShorthandProjection) each).getActualColumns().values());
             } else if (!(each instanceof DerivedProjection)) {
                 result.add(each);
             }
@@ -107,8 +107,8 @@ public final class ProjectionsContext {
      */
     public Optional<String> findAlias(final String projectionName) {
         for (Projection each : projections) {
-            if (each instanceof ShorthandProjection && ((ShorthandProjection) each).getResultSetColumns().containsKey(projectionName.toLowerCase())) {
-                return ((ShorthandProjection) each).getResultSetColumns().get(projectionName.toLowerCase()).getAlias();
+            if (each instanceof ShorthandProjection && ((ShorthandProjection) each).getActualColumns().containsKey(projectionName.toLowerCase())) {
+                return ((ShorthandProjection) each).getActualColumns().get(projectionName.toLowerCase()).getAlias();
             }
             if (projectionName.equalsIgnoreCase(SQLUtil.getExactlyValue(each.getExpression()))) {
                 return each.getAlias();
diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngine.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngine.java
index 3709a34a869..0b153d31721 100644
--- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngine.java
+++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngine.java
@@ -101,7 +101,6 @@ public final class ProjectionEngine {
         if (projectionSegment instanceof ParameterMarkerExpressionSegment) {
             return Optional.of(createProjection((ParameterMarkerExpressionSegment) projectionSegment));
         }
-        // TODO subquery
         return Optional.empty();
     }
     
@@ -178,7 +177,7 @@ public final class ProjectionEngine {
         SelectStatement subSelectStatement = ((SubqueryTableSegment) table).getSubquery().getSelect();
         Collection<Projection> projections = subSelectStatement.getProjections().getProjections().stream().map(each -> createProjection(subSelectStatement.getFrom(), each).orElse(null))
                 .filter(Objects::nonNull).collect(Collectors.toList());
-        return getResultSetProjections(projections);
+        return getProjections(projections);
     }
     
     private Collection<Projection> getShorthandColumnsFromJoinTableSegment(final TableSegment table, final ProjectionSegment projectionSegment) {
@@ -188,10 +187,10 @@ public final class ProjectionEngine {
         Collection<Projection> projections = new LinkedList<>();
         createProjection(((JoinTableSegment) table).getLeft(), projectionSegment).ifPresent(projections::add);
         createProjection(((JoinTableSegment) table).getRight(), projectionSegment).ifPresent(projections::add);
-        return getResultSetProjections(projections);
+        return getProjections(projections);
     }
     
-    private Collection<Projection> getResultSetProjections(final Collection<Projection> projections) {
+    private Collection<Projection> getProjections(final Collection<Projection> projections) {
         Collection<Projection> result = new LinkedList<>();
         for (Projection each : projections) {
             if (each instanceof ColumnProjection) {
@@ -199,7 +198,7 @@ public final class ProjectionEngine {
             } else if (each instanceof ExpressionProjection) {
                 result.add(each);
             } else if (each instanceof ShorthandProjection) {
-                result.addAll(((ShorthandProjection) each).getResultSetColumns().values());
+                result.addAll(((ShorthandProjection) each).getActualColumns().values());
             }
         }
         return result;
diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java
index cd3df174e4a..c1f801f0c7e 100644
--- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java
+++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java
@@ -134,7 +134,7 @@ public final class ProjectionsContextEngine {
             result.add((ColumnProjection) projection);
         }
         if (projection instanceof ShorthandProjection) {
-            result.addAll(((ShorthandProjection) projection).getActualColumns().values());
+            result.addAll(((ShorthandProjection) projection).getColumnProjections());
         }
         return result;
     }
diff --git a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ShorthandProjection.java b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ShorthandProjection.java
index 44f70ca4a13..9b3f69564be 100644
--- a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ShorthandProjection.java
+++ b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/impl/ShorthandProjection.java
@@ -25,7 +25,9 @@ import org.apache.shardingsphere.infra.binder.segment.select.projection.Projecti
 
 import java.util.Collection;
 import java.util.LinkedHashMap;
+import java.util.LinkedList;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Optional;
 
 /**
@@ -37,12 +39,12 @@ import java.util.Optional;
 public final class ShorthandProjection implements Projection {
     
     private final String owner;
-
-    private final Map<String, Projection> resultSetColumns = new LinkedHashMap<>();
-
+    
+    private final Map<String, Projection> actualColumns = new LinkedHashMap<>();
+    
     public ShorthandProjection(final String owner, final Collection<Projection> projections) {
         this.owner = owner;
-        projections.forEach(each -> resultSetColumns.put(each.getExpression().toLowerCase(), each));
+        projections.forEach(each -> actualColumns.put(each.getExpression().toLowerCase(), each));
     }
     
     @Override
@@ -68,19 +70,19 @@ public final class ShorthandProjection implements Projection {
     public Optional<String> getOwner() {
         return Optional.ofNullable(owner);
     }
-
+    
     /**
-     * get actualColumns, exclude ExpressionProjection.
+     * Get column projections.
      *
-     * @return actualColumns
+     * @return column projections
      */
-    public Map<String, ColumnProjection> getActualColumns() {
-        Map<String, ColumnProjection> actualColumns = new LinkedHashMap<>();
-        for (Map.Entry<String, Projection> entry : resultSetColumns.entrySet()) {
+    public Collection<ColumnProjection> getColumnProjections() {
+        Collection<ColumnProjection> result = new LinkedList<>();
+        for (Entry<String, Projection> entry : actualColumns.entrySet()) {
             if (entry.getValue() instanceof ColumnProjection) {
-                actualColumns.put(entry.getKey(), (ColumnProjection) entry.getValue());
+                result.add((ColumnProjection) entry.getValue());
             }
         }
-        return actualColumns;
+        return result;
     }
 }
diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngineTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngineTest.java
index 8493ce69abe..526b77d8491 100644
--- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngineTest.java
+++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngineTest.java
@@ -54,8 +54,11 @@ import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
+import java.util.Iterator;
 import java.util.LinkedHashMap;
+import java.util.LinkedList;
 import java.util.Map;
 import java.util.Optional;
 
@@ -101,11 +104,11 @@ public final class ProjectionEngineTest {
                 Collections.singletonMap(DefaultDatabase.LOGIC_NAME, schema), databaseType).createProjection(table, new ShorthandProjectionSegment(0, 0));
         assertTrue(actual.isPresent());
         assertThat(actual.get(), instanceOf(ShorthandProjection.class));
-        assertThat(((ShorthandProjection) actual.get()).getActualColumns().size(), is(2));
-        Map<String, ColumnProjection> actualColumns = new LinkedHashMap<>();
-        actualColumns.put("t_order.order_id", new ColumnProjection("t_order", "order_id", null));
-        actualColumns.put("t_order.content", new ColumnProjection("t_order", "content", null));
-        assertThat(((ShorthandProjection) actual.get()).getActualColumns(), is(actualColumns));
+        assertThat(((ShorthandProjection) actual.get()).getColumnProjections().size(), is(2));
+        Collection<ColumnProjection> columnProjections = new LinkedList<>();
+        columnProjections.add(new ColumnProjection("t_order", "order_id", null));
+        columnProjections.add(new ColumnProjection("t_order", "content", null));
+        assertThat(((ShorthandProjection) actual.get()).getColumnProjections(), is(columnProjections));
     }
     
     @Test
@@ -189,11 +192,12 @@ public final class ProjectionEngineTest {
                 DefaultDatabase.LOGIC_NAME, Collections.singletonMap(DefaultDatabase.LOGIC_NAME, schema), databaseType).createProjection(table, shorthandProjectionSegment);
         assertTrue(actual.isPresent());
         assertThat(actual.get(), instanceOf(ShorthandProjection.class));
-        Map<String, ColumnProjection> actualColumns = ((ShorthandProjection) actual.get()).getActualColumns();
-        assertThat(actualColumns.size(), is(3));
-        assertThat(actualColumns.get("t_order.order_id"), is(new ColumnProjection("t_order", "order_id", null)));
-        assertThat(actualColumns.get("t_order.customer_id"), is(new ColumnProjection("t_order", "customer_id", null)));
-        assertThat(actualColumns.get("t_customer.customer_id"), is(new ColumnProjection("t_customer", "customer_id", null)));
+        Collection<ColumnProjection> columnProjections = ((ShorthandProjection) actual.get()).getColumnProjections();
+        assertThat(columnProjections.size(), is(3));
+        Iterator<ColumnProjection> iterator = columnProjections.iterator();
+        assertThat(iterator.next(), is(new ColumnProjection("t_order", "order_id", null)));
+        assertThat(iterator.next(), is(new ColumnProjection("t_order", "customer_id", null)));
+        assertThat(iterator.next(), is(new ColumnProjection("t_customer", "customer_id", null)));
     }
     
     @Test(expected = SchemaNotFoundException.class)
@@ -205,9 +209,9 @@ public final class ProjectionEngineTest {
         ShorthandProjectionSegment shorthandProjectionSegment = new ShorthandProjectionSegment(0, 0);
         new ProjectionEngine(DefaultDatabase.LOGIC_NAME, Collections.singletonMap(DefaultDatabase.LOGIC_NAME, schema), databaseType).createProjection(tableSegment, shorthandProjectionSegment);
     }
-
+    
     @Test
-    public void assertResultSetColumnsWithColumnProjectionAndExpressionProjectionOfShorthandProjection() {
+    public void assertGetActualColumnsWhenShorthandProjectionContainsColumnProjectionAndExpressionProjection() {
         ProjectionsSegment subQuerySegment = new ProjectionsSegment(0, 0);
         ColumnSegment columnSegment = new ColumnSegment(0, 0, new IdentifierValue("name"));
         subQuerySegment.getProjections().add(new ColumnProjectionSegment(columnSegment));
@@ -223,14 +227,14 @@ public final class ProjectionEngineTest {
                 .createProjection(subqueryTableSegment, shorthandProjectionSegment);
         assertTrue(actual.isPresent());
         assertThat(actual.get(), instanceOf(ShorthandProjection.class));
-        assertThat(((ShorthandProjection) actual.get()).getActualColumns().size(), is(1));
-        assertThat(((ShorthandProjection) actual.get()).getResultSetColumns().size(), is(2));
-        Map<String, ColumnProjection> actualColumns = new LinkedHashMap<>();
+        assertThat(((ShorthandProjection) actual.get()).getColumnProjections().size(), is(1));
+        assertThat(((ShorthandProjection) actual.get()).getActualColumns().size(), is(2));
+        Collection<ColumnProjection> columnProjections = new LinkedList<>();
+        columnProjections.add(new ColumnProjection(null, "name", null));
+        assertThat(((ShorthandProjection) actual.get()).getColumnProjections(), is(columnProjections));
+        Map<String, Projection> actualColumns = new LinkedHashMap<>();
         actualColumns.put("name", new ColumnProjection(null, "name", null));
+        actualColumns.put("nvl(leave_date, '20991231')", new ExpressionProjection("nvl(leave_date, '20991231')", "leave_date"));
         assertThat(((ShorthandProjection) actual.get()).getActualColumns(), is(actualColumns));
-        Map<String, Projection> resultSetColumns = new LinkedHashMap<>();
-        resultSetColumns.put("name", new ColumnProjection(null, "name", null));
-        resultSetColumns.put("nvl(leave_date, '20991231')", new ExpressionProjection("nvl(leave_date, '20991231')", "leave_date"));
-        assertThat(((ShorthandProjection) actual.get()).getResultSetColumns(), is(resultSetColumns));
     }
 }