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 aded799  Fixes #8896 (#8950)
aded799 is described below

commit aded799e8e61c7b2eefa330e431f2a59dfe0dbbb
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Jan 8 15:49:04 2021 +0800

    Fixes #8896 (#8950)
    
    * Refactor TableExtractor
    
    * Fix #8896
    
    * Add javadoc
---
 .../impl/EncryptProjectionTokenGenerator.java      |  4 +-
 .../dql/groupby/GroupByMemoryMergedResult.java     |  2 +-
 .../generator/impl/OrderByTokenGenerator.java      |  2 +-
 .../statement/dml/InsertStatementContext.java      |  2 +-
 .../statement/dml/SelectStatementContext.java      | 22 ++++++++---
 .../engine/ProjectionsContextEngineTest.java       | 14 +++----
 .../sql/common/extractor/TableExtractor.java       | 46 ++++++++++++++--------
 7 files changed, 57 insertions(+), 35 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 ed82743..a2706f3 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
@@ -56,14 +56,14 @@ public final class EncryptProjectionTokenGenerator extends 
BaseEncryptSQLTokenGe
     
     @Override
     protected boolean isGenerateSQLTokenForEncrypt(final SQLStatementContext 
sqlStatementContext) {
-        return sqlStatementContext instanceof SelectStatementContext && 
!((SelectStatementContext) 
sqlStatementContext).getSimpleTableSegments().isEmpty();
+        return sqlStatementContext instanceof SelectStatementContext && 
!((SelectStatementContext) 
sqlStatementContext).getAllSimpleTableSegments().isEmpty();
     }
     
     @Override
     public Collection<SubstitutableColumnNameToken> generateSQLTokens(final 
SelectStatementContext selectStatementContext) {
         ProjectionsSegment projectionsSegment = 
selectStatementContext.getSqlStatement().getProjections();
         // TODO process multiple tables
-        String tableName = 
selectStatementContext.getSimpleTableSegments().iterator().next().getTableName().getIdentifier().getValue();
+        String tableName = 
selectStatementContext.getAllSimpleTableSegments().iterator().next().getTableName().getIdentifier().getValue();
         return getEncryptRule().findEncryptTable(tableName).map(
             encryptTable -> generateSQLTokens(projectionsSegment, tableName, 
selectStatementContext, encryptTable)).orElseGet(Collections::emptyList);
     }
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java
index 7fe4ba1..821b9b5 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-merge/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java
@@ -126,7 +126,7 @@ public final class GroupByMemoryMergedResult extends 
MemoryMergedResult<Sharding
     
     private boolean getValueCaseSensitiveFromTables(final QueryResult 
queryResult, 
                                                     final 
SelectStatementContext selectStatementContext, final ShardingSphereSchema 
schema, final int columnIndex) throws SQLException {
-        for (SimpleTableSegment each : 
selectStatementContext.getSimpleTableSegments()) {
+        for (SimpleTableSegment each : 
selectStatementContext.getAllSimpleTableSegments()) {
             String tableName = each.getTableName().getIdentifier().getValue();
             TableMetaData tableMetaData = schema.get(tableName);
             Map<String, ColumnMetaData> columns = tableMetaData.getColumns();
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/OrderByTokenGenerator.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/OrderByTokenGenerator.java
index 546b895..b9444ac 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/OrderByTokenGenerator.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/OrderByTokenGenerator.java
@@ -65,7 +65,7 @@ public final class OrderByTokenGenerator implements 
OptionalSQLTokenGenerator<Se
         if (selectStatement.getWhere().isPresent()) {
             return selectStatement.getWhere().get().getStopIndex() + 1;
         } else {
-            return 
selectStatementContext.getSimpleTableSegments().stream().mapToInt(SimpleTableSegment::getStopIndex).max().getAsInt()
 + 1;
+            return 
selectStatementContext.getAllSimpleTableSegments().stream().mapToInt(SimpleTableSegment::getStopIndex).max().getAsInt()
 + 1;
         }
     }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/InsertStatementContext.java
 
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/InsertStatementContext.java
index e13e66b..07d3de8 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/InsertStatementContext.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/InsertStatementContext.java
@@ -82,7 +82,7 @@ public final class InsertStatementContext extends 
CommonSQLStatementContext<Inse
         List<SimpleTableSegment> result = new LinkedList<>();
         result.add(sqlStatement.getTable());
         if (sqlStatement.getInsertSelect().isPresent()) {
-            
result.addAll(insertSelectContext.getSelectStatementContext().getSimpleTableSegments());
+            
result.addAll(insertSelectContext.getSelectStatementContext().getAllSimpleTableSegments());
         }
         return new TablesContext(result);
     }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/SelectStatementContext.java
 
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/SelectStatementContext.java
index 851c852..e71e101 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/SelectStatementContext.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/SelectStatementContext.java
@@ -73,7 +73,7 @@ public final class SelectStatementContext extends 
CommonSQLStatementContext<Sele
     public SelectStatementContext(final SelectStatement sqlStatement, final 
GroupByContext groupByContext,
                                   final OrderByContext orderByContext, final 
ProjectionsContext projectionsContext, final PaginationContext 
paginationContext) {
         super(sqlStatement);
-        tablesContext = new TablesContext(getSimpleTableSegments());
+        tablesContext = new TablesContext(getAllSimpleTableSegments());
         this.groupByContext = groupByContext;
         this.orderByContext = orderByContext;
         this.projectionsContext = projectionsContext;
@@ -83,10 +83,10 @@ public final class SelectStatementContext extends 
CommonSQLStatementContext<Sele
     
     public SelectStatementContext(final ShardingSphereSchema schema, final 
List<Object> parameters, final SelectStatement sqlStatement) {
         super(sqlStatement);
-        tablesContext = new TablesContext(getSimpleTableSegments());
+        tablesContext = new TablesContext(getAllSimpleTableSegments());
         groupByContext = new 
GroupByContextEngine().createGroupByContext(sqlStatement);
         orderByContext = new 
OrderByContextEngine().createOrderBy(sqlStatement, groupByContext);
-        projectionsContext = new 
ProjectionsContextEngine(schema).createProjectionsContext(getSimpleTableSegments(),
 getSqlStatement().getProjections(), groupByContext, orderByContext);
+        projectionsContext = new 
ProjectionsContextEngine(schema).createProjectionsContext(getFromSimpleTableSegments(),
 getSqlStatement().getProjections(), groupByContext, orderByContext);
         paginationContext = new 
PaginationContextEngine().createPaginationContext(sqlStatement, 
projectionsContext, parameters);
         containsSubquery = containsSubquery();
     }
@@ -187,12 +187,22 @@ public final class SelectStatementContext extends 
CommonSQLStatementContext<Sele
     }
     
     /**
-     * get tables.
-     * @return tables.
+     * Get all tables.
+     * 
+     * @return all tables
      */
-    public Collection<SimpleTableSegment> getSimpleTableSegments() {
+    public Collection<SimpleTableSegment> getAllSimpleTableSegments() {
         TableExtractor tableExtractor = new TableExtractor();
         tableExtractor.extractTablesFromSelect(getSqlStatement());
         return tableExtractor.getRewriteTables();
     }
+    
+    /**
+     * Get tables with from clause.
+     *
+     * @return tables with from clause
+     */
+    public Collection<SimpleTableSegment> getFromSimpleTableSegments() {
+        return new 
TableExtractor().extractTablesWithFromClause(getSqlStatement());
+    }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngineTest.java
 
b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngineTest.java
index b0ab9e2..226a5b6 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngineTest.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngineTest.java
@@ -82,7 +82,7 @@ public final class ProjectionsContextEngineTest {
         ProjectionsContextEngine projectionsContextEngine = new 
ProjectionsContextEngine(null);
         selectStatement.setProjections(new ProjectionsSegment(0, 0));
         SelectStatementContext selectStatementContext = new 
SelectStatementContext(schema, new LinkedList<>(), selectStatement);
-        Collection<SimpleTableSegment> tables = 
selectStatementContext.getSimpleTableSegments();
+        Collection<SimpleTableSegment> tables = 
selectStatementContext.getAllSimpleTableSegments();
         ProjectionsSegment projectionsSegment = 
selectStatement.getProjections();
         ProjectionsContext actual = projectionsContextEngine
                 .createProjectionsContext(tables, projectionsSegment, new 
GroupByContext(Collections.emptyList(), 0), new 
OrderByContext(Collections.emptyList(), false));
@@ -122,7 +122,7 @@ public final class ProjectionsContextEngineTest {
         shorthandProjectionSegment.setOwner(owner);
         
projectionsSegment.getProjections().addAll(Collections.singleton(shorthandProjectionSegment));
         SelectStatementContext selectStatementContext = new 
SelectStatementContext(schema, new LinkedList<>(), selectStatement);
-        Collection<SimpleTableSegment> tables = 
selectStatementContext.getSimpleTableSegments();
+        Collection<SimpleTableSegment> tables = 
selectStatementContext.getAllSimpleTableSegments();
         ProjectionsContext actual = new ProjectionsContextEngine(schema)
                 .createProjectionsContext(tables, projectionsSegment, new 
GroupByContext(Collections.emptyList(), 0), new 
OrderByContext(Collections.emptyList(), false));
         assertNotNull(actual);
@@ -163,7 +163,7 @@ public final class ProjectionsContextEngineTest {
         OrderByItem orderByItem = new OrderByItem(new 
IndexOrderByItemSegment(0, 1, 0, OrderDirection.ASC));
         OrderByContext orderByContext = new 
OrderByContext(Collections.singletonList(orderByItem), true);
         SelectStatementContext selectStatementContext = new 
SelectStatementContext(schema, new LinkedList<>(), selectStatement);
-        Collection<SimpleTableSegment> tables = 
selectStatementContext.getSimpleTableSegments();
+        Collection<SimpleTableSegment> tables = 
selectStatementContext.getAllSimpleTableSegments();
         ProjectionsContext actual = new ProjectionsContextEngine(schema)
                 .createProjectionsContext(tables, projectionsSegment, new 
GroupByContext(Collections.emptyList(), 0), orderByContext);
         assertNotNull(actual);
@@ -204,7 +204,7 @@ public final class ProjectionsContextEngineTest {
         OrderByItem orderByItem = new OrderByItem(new 
ExpressionOrderByItemSegment(0, 1, "", OrderDirection.ASC));
         OrderByContext orderByContext = new 
OrderByContext(Collections.singletonList(orderByItem), true);
         SelectStatementContext selectStatementContext = new 
SelectStatementContext(schema, new LinkedList<>(), selectStatement);
-        Collection<SimpleTableSegment> tables = 
selectStatementContext.getSimpleTableSegments();
+        Collection<SimpleTableSegment> tables = 
selectStatementContext.getAllSimpleTableSegments();
         ProjectionsContext actual = new ProjectionsContextEngine(schema)
                 .createProjectionsContext(tables, projectionsSegment, new 
GroupByContext(Collections.emptyList(), 0), orderByContext);
         assertNotNull(actual);
@@ -246,7 +246,7 @@ public final class ProjectionsContextEngineTest {
         OrderByItem orderByItem = new OrderByItem(new 
ColumnOrderByItemSegment(new ColumnSegment(0, 0, new IdentifierValue("name")), 
OrderDirection.ASC));
         OrderByContext orderByContext = new 
OrderByContext(Collections.singletonList(orderByItem), true);
         SelectStatementContext selectStatementContext = new 
SelectStatementContext(schema, new LinkedList<>(), selectStatement);
-        Collection<SimpleTableSegment> tables = 
selectStatementContext.getSimpleTableSegments();
+        Collection<SimpleTableSegment> tables = 
selectStatementContext.getAllSimpleTableSegments();
         ProjectionsContext actual = new ProjectionsContextEngine(schema)
                 .createProjectionsContext(tables, projectionsSegment, new 
GroupByContext(Collections.emptyList(), 0), orderByContext);
         assertNotNull(actual);
@@ -288,7 +288,7 @@ public final class ProjectionsContextEngineTest {
         OrderByItem orderByItem = new OrderByItem(new 
ColumnOrderByItemSegment(new ColumnSegment(0, 0, new IdentifierValue("name")), 
OrderDirection.ASC));
         OrderByContext orderByContext = new 
OrderByContext(Collections.singletonList(orderByItem), true);
         SelectStatementContext selectStatementContext = new 
SelectStatementContext(schema, new LinkedList<>(), selectStatement);
-        Collection<SimpleTableSegment> tables = 
selectStatementContext.getSimpleTableSegments();
+        Collection<SimpleTableSegment> tables = 
selectStatementContext.getAllSimpleTableSegments();
         ProjectionsContext actual = new ProjectionsContextEngine(schema)
                 .createProjectionsContext(tables, projectionsSegment, new 
GroupByContext(Collections.emptyList(), 0), orderByContext);
         assertNotNull(actual);
@@ -338,7 +338,7 @@ public final class ProjectionsContextEngineTest {
         OrderByItem orderByItem = new OrderByItem(new 
ColumnOrderByItemSegment(new ColumnSegment(0, 0, new IdentifierValue("name")), 
OrderDirection.ASC));
         OrderByContext orderByContext = new 
OrderByContext(Collections.singleton(orderByItem), false);
         SelectStatementContext selectStatementContext = new 
SelectStatementContext(schema, new LinkedList<>(), selectStatement);
-        Collection<SimpleTableSegment> tables = 
selectStatementContext.getSimpleTableSegments();
+        Collection<SimpleTableSegment> tables = 
selectStatementContext.getAllSimpleTableSegments();
         ProjectionsContext actual = new ProjectionsContextEngine(schema)
                 .createProjectionsContext(tables, projectionsSegment, new 
GroupByContext(Collections.emptyList(), 0), orderByContext);
         assertNotNull(actual);
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 edac300..50f5113 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
@@ -54,18 +54,17 @@ import java.util.Collection;
 import java.util.LinkedList;
 import java.util.Optional;
 
+@Getter
 public final class TableExtractor {
     
-    @Getter
     private Collection<SimpleTableSegment> rewriteTables = new LinkedList<>();
     
-    @Getter
     private Collection<TableSegment> tableContext = new LinkedList<>();
     
     /**
-     * Extract table that should be rewrited from SelectStatement.
+     * Extract table that should be rewrite from select statement.
      *
-     * @param selectStatement SelectStatement.
+     * @param selectStatement select statement
      */
     public void extractTablesFromSelect(final SelectStatement selectStatement) 
{
         if (null != selectStatement.getFrom()) {
@@ -88,6 +87,19 @@ public final class TableExtractor {
         }
     }
     
+    /**
+     * Extract tables with from clause.
+     *
+     * @param selectStatement select statement
+     * @return tables with from clause
+     */
+    public Collection<SimpleTableSegment> extractTablesWithFromClause(final 
SelectStatement selectStatement) {
+        if (null != selectStatement.getFrom()) {
+            extractTablesFromTableSegment(selectStatement.getFrom());
+        }
+        return rewriteTables;
+    }
+    
     private void extractTablesFromTableSegment(final TableSegment 
tableSegment) {
         if (tableSegment instanceof SimpleTableSegment) {
             tableContext.add(tableSegment);
@@ -97,7 +109,7 @@ public final class TableExtractor {
             tableContext.add(tableSegment);
             TableExtractor tableExtractor = new TableExtractor();
             tableExtractor.extractTablesFromSelect(((SubqueryTableSegment) 
tableSegment).getSubquery().getSelect());
-            rewriteTables.addAll(tableExtractor.getRewriteTables());
+            rewriteTables.addAll(tableExtractor.rewriteTables);
         }
         if (tableSegment instanceof JoinTableSegment) {
             extractTablesFromJoinTableSegment((JoinTableSegment) tableSegment);
@@ -185,9 +197,9 @@ public final class TableExtractor {
     }
     
     /**
-     * Extract table that should be rewrited from DeleteStatement.
+     * Extract table that should be rewrite from delete statement.
      *
-     * @param deleteStatement DeleteStatement.
+     * @param deleteStatement delete statement
      */
     public void extractTablesFromDelete(final DeleteStatement deleteStatement) 
{
         extractTablesFromTableSegment(deleteStatement.getTableSegment());
@@ -197,9 +209,9 @@ public final class TableExtractor {
     }
     
     /**
-     * Extract table that should be rewrited from InsertStatement.
+     * Extract table that should be rewrite from insert statement.
      *
-     * @param insertStatement SelectStatement.
+     * @param insertStatement insert statement
      */
     public void extractTablesFromInsert(final InsertStatement insertStatement) 
{
         if (null != insertStatement.getTable()) {
@@ -216,9 +228,9 @@ public final class TableExtractor {
     }
     
     /**
-     * Extract table that should be rewrited from UpdateStatement.
+     * Extract table that should be rewrite from update statement.
      *
-     * @param updateStatement UpdateStatement.
+     * @param updateStatement update statement.
      */
     public void extractTablesFromUpdate(final UpdateStatement updateStatement) 
{
         extractTablesFromTableSegment(updateStatement.getTableSegment());
@@ -230,8 +242,8 @@ public final class TableExtractor {
     /**
      * Check if the table needs to be overwritten.
      *
-     * @param owner OwnerSegment.
-     * @return boolean.
+     * @param owner owner
+     * @return boolean
      */
     public boolean needRewrite(final OwnerSegment owner) {
         for (TableSegment each : tableContext) {
@@ -243,9 +255,9 @@ public final class TableExtractor {
     }
     
     /**
-     * Extract the tables that should exist from RoutineBodySegment.
+     * Extract the tables that should exist from routine body segment.
      *
-     * @param routineBody RoutineBodySegment
+     * @param routineBody routine body segment
      * @return the tables that should exist
      */
     public Collection<SimpleTableSegment> 
extractExistTableFromRoutineBody(final RoutineBodySegment routineBody) {
@@ -281,9 +293,9 @@ public final class TableExtractor {
     }
     
     /**
-     * Extract the tables that should not exist from RoutineBodySegment.
+     * Extract the tables that should not exist from routine body segment.
      *
-     * @param routineBody RoutineBodySegment
+     * @param routineBody routine body segment
      * @return the tables that should not exist
      */
     public Collection<SimpleTableSegment> 
extractNotExistTableFromRoutineBody(final RoutineBodySegment routineBody) {

Reply via email to