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 2021/09/05 06:16:17 UTC

[shardingsphere] branch master updated: fix oracle pagination wrong result (#12199)

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 499fc0b  fix oracle pagination wrong result (#12199)
499fc0b is described below

commit 499fc0b734066b1a65a5a28d460fa7d7bc764bf6
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Sun Sep 5 14:15:46 2021 +0800

    fix oracle pagination wrong result (#12199)
    
    * fix oracle pagination wrong result
    
    * optimize logic
    
    * optimize logic
---
 .../impl/EncryptProjectionTokenGenerator.java      |   2 +-
 .../pagination/engine/PaginationContextEngine.java |  21 +++-
 .../engine/RowNumberPaginationContextEngine.java   |  21 ++--
 .../engine/TopPaginationContextEngine.java         |   9 +-
 .../select/projection/ProjectionsContext.java      |   5 +-
 .../select/projection/engine/ProjectionEngine.java |  75 +++++++----
 .../engine/ProjectionsContextEngine.java           | 137 +++++++--------------
 .../projection/impl/ShorthandProjection.java       |  11 +-
 .../statement/dml/SelectStatementContext.java      |  40 ++----
 .../RowNumberPaginationContextEngineTest.java      |  14 +--
 .../engine/TopPaginationContextEngineTest.java     |  14 +--
 .../projection/engine/ProjectionEngineTest.java    |  35 +++---
 .../engine/ProjectionsContextEngineTest.java       |  28 ++---
 13 files changed, 200 insertions(+), 212 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 acdaf1b..e49030e 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
@@ -108,7 +108,7 @@ public final class EncryptProjectionTokenGenerator extends BaseEncryptSQLTokenGe
     private SubstitutableColumnNameToken generateSQLToken(final ShorthandProjectionSegment segment,
                                                           final ShorthandProjection shorthandProjection, final String tableName, final EncryptTable encryptTable, final DatabaseType databaseType) {
         List<ColumnProjection> projections = new LinkedList<>();
-        for (ColumnProjection each : shorthandProjection.getActualColumns()) {
+        for (ColumnProjection each : shorthandProjection.getActualColumns().values()) {
             if (encryptTable.getLogicColumns().contains(each.getName())) {
                 projections.add(new ColumnProjection(null == each.getOwner() ? null : each.getOwner(), getEncryptColumnName(tableName, each.getName()), each.getName()));
             } else {
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/PaginationContextEngine.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/PaginationContextEngine.java
index 0ee803a..dbe12e0 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/PaginationContextEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/PaginationContextEngine.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.infra.binder.segment.select.pagination.engine;
 
 import org.apache.shardingsphere.infra.binder.segment.select.pagination.PaginationContext;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.ProjectionsContext;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.limit.LimitSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.top.TopProjectionSegment;
@@ -26,12 +27,16 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.Whe
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SubqueryTableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
+import org.apache.shardingsphere.sql.parser.sql.common.util.WhereExtractUtil;
 import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.SelectStatementHandler;
 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.oracle.OracleStatement;
 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.sqlserver.SQLServerStatement;
 
+import java.util.Collection;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Optional;
+import java.util.stream.Collectors;
 
 /**
  * Pagination context engine.
@@ -52,16 +57,24 @@ public final class PaginationContextEngine {
             return new LimitPaginationContextEngine().createPaginationContext(limitSegment.get(), parameters);
         }
         Optional<TopProjectionSegment> topProjectionSegment = findTopProjection(selectStatement);
-        Optional<WhereSegment> whereSegment = selectStatement.getWhere();
+        Collection<ExpressionSegment> expressions = getWhereSegments(selectStatement).stream().map(WhereSegment::getExpr).collect(Collectors.toList());
         if (topProjectionSegment.isPresent()) {
-            return new TopPaginationContextEngine().createPaginationContext(topProjectionSegment.get(), whereSegment.map(WhereSegment::getExpr).orElse(null), parameters);
+            return new TopPaginationContextEngine().createPaginationContext(topProjectionSegment.get(), expressions, parameters);
         }
-        if (whereSegment.isPresent() && containsRowNumberPagination(selectStatement)) {
-            return new RowNumberPaginationContextEngine().createPaginationContext(whereSegment.get().getExpr(), projectionsContext, parameters);
+        if (!expressions.isEmpty() && containsRowNumberPagination(selectStatement)) {
+            return new RowNumberPaginationContextEngine().createPaginationContext(expressions, projectionsContext, parameters);
         }
         return new PaginationContext(null, null, parameters);
     }
     
+    private Collection<WhereSegment> getWhereSegments(final SelectStatement selectStatement) {
+        Collection<WhereSegment> result = new LinkedList<>();
+        selectStatement.getWhere().ifPresent(result::add);
+        result.addAll(WhereExtractUtil.getSubqueryWhereSegments(selectStatement));
+        result.addAll(WhereExtractUtil.getJoinWhereSegments(selectStatement));
+        return result;
+    }
+    
     private boolean containsRowNumberPagination(final SelectStatement selectStatement) {
         return selectStatement instanceof OracleStatement || selectStatement instanceof SQLServerStatement;
     }
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/RowNumberPaginationContextEngine.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/RowNumberPaginationContextEngine.java
index 4fb7b1e..57e8bbc 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/RowNumberPaginationContextEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/RowNumberPaginationContextEngine.java
@@ -31,37 +31,38 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.And
 import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionExtractUtil;
 
 import java.util.Collection;
-import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Optional;
+import java.util.TreeSet;
+import java.util.stream.Collectors;
 
 /**
  * Pagination context engine for row number.
  */
 public final class RowNumberPaginationContextEngine {
     
-    private static final Collection<String> ROW_NUMBER_IDENTIFIERS = new HashSet<>();
+    private static final Collection<String> ROW_NUMBER_IDENTIFIERS = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
     
     static {
-        ROW_NUMBER_IDENTIFIERS.add("rownum");
+        ROW_NUMBER_IDENTIFIERS.add("ROWNUM");
         ROW_NUMBER_IDENTIFIERS.add("ROW_NUMBER");
     }
     
     /**
      * Create pagination context.
      * 
-     * @param where where condition
+     * @param expressions expressions
      * @param projectionsContext projections context
      * @param parameters SQL parameters
      * @return pagination context
      */
-    public PaginationContext createPaginationContext(final ExpressionSegment where, final ProjectionsContext projectionsContext, final List<Object> parameters) {
+    public PaginationContext createPaginationContext(final Collection<ExpressionSegment> expressions, final ProjectionsContext projectionsContext, final List<Object> parameters) {
         Optional<String> rowNumberAlias = isRowNumberAlias(projectionsContext);
         if (!rowNumberAlias.isPresent()) {
             return new PaginationContext(null, null, parameters);
         }
-        Collection<AndPredicate> andPredicates = ExpressionExtractUtil.getAndPredicates(where);
+        Collection<AndPredicate> andPredicates = expressions.stream().flatMap(each -> ExpressionExtractUtil.getAndPredicates(each).stream()).collect(Collectors.toList());
         Collection<BinaryOperationExpression> rowNumberPredicates = getRowNumberPredicates(andPredicates, rowNumberAlias.get());
         return rowNumberPredicates.isEmpty() ? new PaginationContext(null, null, parameters) : createPaginationWithRowNumber(rowNumberPredicates, parameters);
     }
@@ -91,8 +92,12 @@ public final class RowNumberPaginationContextEngine {
     private boolean isRowNumberColumn(final ExpressionSegment predicate, final String rowNumberAlias) {
         if (predicate instanceof BinaryOperationExpression) {
             ExpressionSegment left = ((BinaryOperationExpression) predicate).getLeft();
-            return left instanceof ColumnSegment ? ROW_NUMBER_IDENTIFIERS.contains(((ColumnSegment) left).getIdentifier().getValue())
-                    || ((ColumnSegment) left).getIdentifier().getValue().equalsIgnoreCase(rowNumberAlias) : false;
+            if (left instanceof ColumnSegment) {
+                String leftColumnValue = ((ColumnSegment) left).getIdentifier().getValue();
+                return ROW_NUMBER_IDENTIFIERS.contains(leftColumnValue) || leftColumnValue.equalsIgnoreCase(rowNumberAlias);
+            } else {
+                return false;
+            }
         }
         return false;
     }
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/TopPaginationContextEngine.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/TopPaginationContextEngine.java
index 364b083..a4fd084 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/TopPaginationContextEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/TopPaginationContextEngine.java
@@ -34,6 +34,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.util.ExpressionExtractUti
 import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
+import java.util.stream.Collectors;
 
 /**
  * Pagination context engine for top.
@@ -44,13 +45,13 @@ public final class TopPaginationContextEngine {
      * Create pagination context.
      * 
      * @param topProjectionSegment top projection segment
-     * @param where where condition
+     * @param expressions expressions
      * @param parameters SQL parameters
      * @return pagination context
      */
-    public PaginationContext createPaginationContext(final TopProjectionSegment topProjectionSegment, final ExpressionSegment where, final List<Object> parameters) {
-        Collection<AndPredicate> andPredicates = ExpressionExtractUtil.getAndPredicates(where);
-        Optional<ExpressionSegment> rowNumberPredicate = null != where ? getRowNumberPredicate(andPredicates, topProjectionSegment.getAlias()) : Optional.empty();
+    public PaginationContext createPaginationContext(final TopProjectionSegment topProjectionSegment, final Collection<ExpressionSegment> expressions, final List<Object> parameters) {
+        Collection<AndPredicate> andPredicates = expressions.stream().flatMap(each -> ExpressionExtractUtil.getAndPredicates(each).stream()).collect(Collectors.toList());
+        Optional<ExpressionSegment> rowNumberPredicate = !expressions.isEmpty() ? getRowNumberPredicate(andPredicates, topProjectionSegment.getAlias()) : Optional.empty();
         Optional<PaginationValueSegment> offset = rowNumberPredicate.isPresent() ? createOffsetWithRowNumber(rowNumberPredicate.get()) : Optional.empty();
         PaginationValueSegment rowCount = topProjectionSegment.getTop();
         return new PaginationContext(offset.orElse(null), rowCount, parameters);
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/ProjectionsContext.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/ProjectionsContext.java
index 86c8faa..e338d1c 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/ProjectionsContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/ProjectionsContext.java
@@ -69,6 +69,9 @@ public final class ProjectionsContext {
      */
     public Optional<String> findAlias(final String projectionName) {
         for (Projection each : projections) {
+            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();
             }
@@ -134,7 +137,7 @@ public final class ProjectionsContext {
         List<Projection> result = new ArrayList<>();
         for (Projection each : projections) {
             if (each instanceof ShorthandProjection) {
-                result.addAll(((ShorthandProjection) each).getActualColumns());
+                result.addAll(((ShorthandProjection) each).getActualColumns().values());
             } else if (!(each instanceof DerivedProjection)) {
                 result.add(each);
             }
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 ddccea7..d4f312c 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
@@ -36,11 +36,17 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.Expressi
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ShorthandProjectionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.SubqueryProjectionSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.JoinTableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SubqueryTableSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
 
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashSet;
+import java.util.LinkedList;
+import java.util.Objects;
 import java.util.Optional;
 import java.util.stream.Collectors;
 
@@ -61,13 +67,13 @@ public final class ProjectionEngine {
     /**
      * Create projection.
      * 
-     * @param tableSegments table segments
+     * @param table table segment
      * @param projectionSegment projection segment
      * @return projection
      */
-    public Optional<Projection> createProjection(final Collection<SimpleTableSegment> tableSegments, final ProjectionSegment projectionSegment) {
+    public Optional<Projection> createProjection(final TableSegment table, final ProjectionSegment projectionSegment) {
         if (projectionSegment instanceof ShorthandProjectionSegment) {
-            return Optional.of(createProjection(tableSegments, (ShorthandProjectionSegment) projectionSegment));
+            return Optional.of(createProjection(table, (ShorthandProjectionSegment) projectionSegment));
         }
         if (projectionSegment instanceof ColumnProjectionSegment) {
             return Optional.of(createProjection((ColumnProjectionSegment) projectionSegment));
@@ -92,10 +98,13 @@ public final class ProjectionEngine {
         return new SubqueryProjection(projectionSegment.getText(), projectionSegment.getAlias().orElse(null));
     }
     
-    private ShorthandProjection createProjection(final Collection<SimpleTableSegment> tableSegments, final ShorthandProjectionSegment projectionSegment) {
+    private ShorthandProjection createProjection(final TableSegment table, final ShorthandProjectionSegment projectionSegment) {
         String owner = projectionSegment.getOwner().map(ownerSegment -> ownerSegment.getIdentifier().getValue()).orElse(null);
-        Collection<ColumnProjection> shorthandColumns = getShorthandColumns(tableSegments, owner);
-        return new ShorthandProjection(owner, shorthandColumns);
+        Collection<ColumnProjection> columnProjections = new LinkedHashSet<>();
+        columnProjections.addAll(getShorthandColumnsFromSimpleTableSegment(table, owner));
+        columnProjections.addAll(getShorthandColumnsFromSubqueryTableSegment(table));
+        columnProjections.addAll(getShorthandColumnsFromJoinTableSegment(table, projectionSegment));
+        return new ShorthandProjection(owner, columnProjections);
     }
     
     private ColumnProjection createProjection(final ColumnProjectionSegment projectionSegment) {
@@ -128,28 +137,52 @@ public final class ProjectionEngine {
         return result;
     }
     
-    private Collection<ColumnProjection> getShorthandColumns(final Collection<SimpleTableSegment> tables, final String owner) {
-        return null == owner ? getUnqualifiedShorthandColumns(tables) : getQualifiedShorthandColumns(tables, owner);
-    }
-    
-    private Collection<ColumnProjection> getUnqualifiedShorthandColumns(final Collection<SimpleTableSegment> tables) {
-        Collection<ColumnProjection> result = new LinkedHashSet<>();
-        for (SimpleTableSegment each : tables) {
-            String tableName = each.getTableName().getIdentifier().getValue();
-            String owner = each.getAlias().orElse(tableName);
+    private Collection<ColumnProjection> getShorthandColumnsFromSimpleTableSegment(final TableSegment table, final String owner) {
+        if (!(table instanceof SimpleTableSegment)) {
+            return Collections.emptyList();
+        }
+        String tableName = ((SimpleTableSegment) table).getTableName().getIdentifier().getValue();
+        String tableAlias = table.getAlias().orElse(tableName);
+        Collection<ColumnProjection> result = new LinkedList<>();
+        if (null == owner) {
+            schema.getAllColumnNames(tableName).stream().map(columnName -> new ColumnProjection(tableAlias, columnName, null)).forEach(result::add);
+        } else if (owner.equalsIgnoreCase(tableAlias)) {
             schema.getAllColumnNames(tableName).stream().map(columnName -> new ColumnProjection(owner, columnName, null)).forEach(result::add);
         }
         return result;
     }
     
-    private Collection<ColumnProjection> getQualifiedShorthandColumns(final Collection<SimpleTableSegment> tables, final String owner) {
-        for (SimpleTableSegment each : tables) {
-            String tableName = each.getTableName().getIdentifier().getValue();
-            if (owner.equalsIgnoreCase(each.getAlias().orElse(tableName))) {
-                return schema.getAllColumnNames(tableName).stream().map(columnName -> new ColumnProjection(owner, columnName, null)).collect(Collectors.toList());
+    private Collection<ColumnProjection> getShorthandColumnsFromSubqueryTableSegment(final TableSegment table) {
+        if (!(table instanceof SubqueryTableSegment)) {
+            return Collections.emptyList();
+        }
+        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 getColumnProjections(projections);
+    }
+    
+    private Collection<ColumnProjection> getShorthandColumnsFromJoinTableSegment(final TableSegment table, final ProjectionSegment projectionSegment) {
+        if (!(table instanceof JoinTableSegment)) {
+            return Collections.emptyList();
+        }
+        Collection<Projection> projections = new LinkedList<>();
+        createProjection(((JoinTableSegment) table).getLeft(), projectionSegment).ifPresent(projections::add);
+        createProjection(((JoinTableSegment) table).getRight(), projectionSegment).ifPresent(projections::add);
+        return getColumnProjections(projections);
+    }
+    
+    private Collection<ColumnProjection> getColumnProjections(final Collection<Projection> projections) {
+        Collection<ColumnProjection> result = new LinkedList<>();
+        for (Projection each : projections) {
+            if (each instanceof ColumnProjection) {
+                result.add((ColumnProjection) each);
+            }
+            if (each instanceof ShorthandProjection) {
+                result.addAll(((ShorthandProjection) each).getActualColumns().values());
             }
         }
-        return Collections.emptyList();
+        return result;
     }
     
     private void appendAverageDistinctDerivedProjection(final AggregationDistinctProjection averageDistinctProjection) {
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java
index 5ebf243..9c4d397 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java
@@ -17,185 +17,134 @@
 
 package org.apache.shardingsphere.infra.binder.segment.select.projection.engine;
 
-import com.google.common.base.Preconditions;
 import org.apache.shardingsphere.infra.binder.segment.select.groupby.GroupByContext;
 import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByContext;
 import org.apache.shardingsphere.infra.binder.segment.select.orderby.OrderByItem;
 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.ProjectionsContext;
+import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ColumnProjection;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.DerivedProjection;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ShorthandProjection;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionsSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ColumnOrderByItemSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ExpressionOrderByItemSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.IndexOrderByItemSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.OrderByItemSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.TextOrderByItemSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
 
 import java.util.Collection;
 import java.util.LinkedList;
-import java.util.Optional;
 
 /**
  * Projections context engine.
  */
 public final class ProjectionsContextEngine {
     
-    private final ShardingSphereSchema schema;
-    
     private final ProjectionEngine projectionEngine;
     
     public ProjectionsContextEngine(final ShardingSphereSchema schema, final DatabaseType databaseType) {
-        this.schema = schema;
         projectionEngine = new ProjectionEngine(schema, databaseType);
     }
     
     /**
      * Create projections context.
      *
-     * @param tables tables
-     * @param projectionsSegment projection Segments
+     * @param table table segment
+     * @param projectionsSegment projection segments
      * @param groupByContext group by context
      * @param orderByContext order by context
      * @return projections context
      */
-    public ProjectionsContext createProjectionsContext(final Collection<SimpleTableSegment> tables, final ProjectionsSegment projectionsSegment,
+    public ProjectionsContext createProjectionsContext(final TableSegment table, final ProjectionsSegment projectionsSegment, 
                                                        final GroupByContext groupByContext, final OrderByContext orderByContext) {
-        Collection<Projection> projections = getProjections(tables, projectionsSegment);
+        Collection<Projection> projections = getProjections(table, projectionsSegment);
         ProjectionsContext result = new ProjectionsContext(projectionsSegment.getStartIndex(), projectionsSegment.getStopIndex(), projectionsSegment.isDistinctRow(), projections);
-        result.getProjections().addAll(getDerivedGroupByColumns(projections, groupByContext, tables));
-        result.getProjections().addAll(getDerivedOrderByColumns(projections, orderByContext, tables));
+        result.getProjections().addAll(getDerivedGroupByColumns(groupByContext, projections));
+        result.getProjections().addAll(getDerivedOrderByColumns(orderByContext, projections));
         return result;
     }
     
-    private Collection<Projection> getProjections(final Collection<SimpleTableSegment> tableSegments, final ProjectionsSegment projectionsSegment) {
+    private Collection<Projection> getProjections(final TableSegment table, final ProjectionsSegment projectionsSegment) {
         Collection<Projection> result = new LinkedList<>();
         for (ProjectionSegment each : projectionsSegment.getProjections()) {
-            projectionEngine.createProjection(tableSegments, each).ifPresent(result::add);
+            projectionEngine.createProjection(table, each).ifPresent(result::add);
         }
         return result;
     }
     
-    private Collection<Projection> getDerivedGroupByColumns(final Collection<Projection> projections, final GroupByContext groupByContext, final Collection<SimpleTableSegment> tables) {
-        return getDerivedOrderColumns(projections, groupByContext.getItems(), DerivedColumn.GROUP_BY_ALIAS, tables);
+    private Collection<Projection> getDerivedGroupByColumns(final GroupByContext groupByContext, final Collection<Projection> projections) {
+        return getDerivedOrderColumns(groupByContext.getItems(), DerivedColumn.GROUP_BY_ALIAS, projections);
     }
     
-    private Collection<Projection> getDerivedOrderByColumns(final Collection<Projection> projections, final OrderByContext orderByContext, final Collection<SimpleTableSegment> tables) {
-        return getDerivedOrderColumns(projections, orderByContext.getItems(), DerivedColumn.ORDER_BY_ALIAS, tables);
+    private Collection<Projection> getDerivedOrderByColumns(final OrderByContext orderByContext, final Collection<Projection> projections) {
+        return getDerivedOrderColumns(orderByContext.getItems(), DerivedColumn.ORDER_BY_ALIAS, projections);
     }
     
-    private Collection<Projection> getDerivedOrderColumns(final Collection<Projection> projections,
-                                                          final Collection<OrderByItem> orderItems, final DerivedColumn derivedColumn, final Collection<SimpleTableSegment> tables) {
+    private Collection<Projection> getDerivedOrderColumns(final Collection<OrderByItem> orderItems, final DerivedColumn derivedColumn, final Collection<Projection> projections) {
         Collection<Projection> result = new LinkedList<>();
         int derivedColumnOffset = 0;
         for (OrderByItem each : orderItems) {
-            if (!containsProjection(projections, each.getSegment(), tables)) {
+            if (!containsProjection(each.getSegment(), projections)) {
                 result.add(new DerivedProjection(((TextOrderByItemSegment) each.getSegment()).getText(), derivedColumn.getDerivedColumnAlias(derivedColumnOffset++), each.getSegment()));
             }
         }
         return result;
     }
     
-    private boolean containsProjection(final Collection<Projection> projections, final OrderByItemSegment orderByItemSegment, final Collection<SimpleTableSegment> tables) {
-        return containsItemInShorthandProjection(projections, orderByItemSegment, tables) || containsProjection(projections, orderByItemSegment);
-    }
-    
-    private boolean containsProjection(final Collection<Projection> projections, final OrderByItemSegment orderItem) {
-        if (orderItem instanceof IndexOrderByItemSegment) {
+    private boolean containsProjection(final OrderByItemSegment orderByItem, final Collection<Projection> projections) {
+        if (orderByItem instanceof IndexOrderByItemSegment) {
             return true;
         }
         for (Projection each : projections) {
-            if (isSameAlias(each, (TextOrderByItemSegment) orderItem) || isSameQualifiedName(each, (TextOrderByItemSegment) orderItem)) {
+            if (orderByItem instanceof ColumnOrderByItemSegment && isSameColumn(each, ((ColumnOrderByItemSegment) orderByItem).getColumn())) {
+                return true;
+            }
+            String text = ((TextOrderByItemSegment) orderByItem).getText();
+            if (isSameAlias(each, text) || isSameQualifiedName(each, text)) {
                 return true;
             }
         }
         return false;
     }
     
-    private boolean containsItemInShorthandProjection(final Collection<Projection> projections, final OrderByItemSegment orderByItemSegment, final Collection<SimpleTableSegment> tables) {
-        return isUnqualifiedShorthandProjection(projections, orderByItemSegment) || containsItemWithOwnerInShorthandProjections(projections, orderByItemSegment, tables)
-                || containsItemWithoutOwnerInShorthandProjections(projections, orderByItemSegment, tables);
-    }
-    
-    private boolean isUnqualifiedShorthandProjection(final Collection<Projection> projections, final OrderByItemSegment orderByItemSegment) {
-        if (1 != projections.size() || orderByItemSegment instanceof ExpressionOrderByItemSegment) {
+    private boolean isSameColumn(final Projection projection, final ColumnSegment columnSegment) {
+        Collection<ColumnProjection> columns = getColumnProjections(projection);
+        if (columns.isEmpty()) {
             return false;
         }
-        Projection projection = projections.iterator().next();
-        return projection instanceof ShorthandProjection && !((ShorthandProjection) projection).getOwner().isPresent();
-    }
-    
-    private boolean containsItemWithOwnerInShorthandProjections(final Collection<Projection> projections, final OrderByItemSegment orderItem, final Collection<SimpleTableSegment> tables) {
-        return orderItem instanceof ColumnOrderByItemSegment && ((ColumnOrderByItemSegment) orderItem).getColumn().getOwner().isPresent()
-                && findShorthandProjection(projections, ((ColumnOrderByItemSegment) orderItem).getColumn().getOwner().get().getIdentifier().getValue(), tables).isPresent();
-    }
-    
-    private Optional<ShorthandProjection> findShorthandProjection(final Collection<Projection> projections, final String tableNameOrAlias, final Collection<SimpleTableSegment> tables) {
-        SimpleTableSegment tableSegment = find(tableNameOrAlias, tables);
-        for (Projection each : projections) {
-            if (!(each instanceof ShorthandProjection)) {
-                continue;
-            }
-            ShorthandProjection shorthandProjection = (ShorthandProjection) each;
-            if (shorthandProjection.getOwner().isPresent() && find(
-                    shorthandProjection.getOwner().get(), tables).getTableName().getIdentifier().getValue().equalsIgnoreCase(tableSegment.getTableName().getIdentifier().getValue())) {
-                return Optional.of(shorthandProjection);
-            }
+        if (columnSegment.getOwner().isPresent()) {
+            return columns.stream().anyMatch(each -> isSameQualifiedName(each, columnSegment.getQualifiedName()));
+        } else {
+            return columns.stream().anyMatch(each -> isSameName(each, columnSegment.getQualifiedName()));
         }
-        return Optional.empty();
     }
     
-    private boolean containsItemWithoutOwnerInShorthandProjections(final Collection<Projection> projections, final OrderByItemSegment orderItem, final Collection<SimpleTableSegment> tables) {
-        if (!(orderItem instanceof ColumnOrderByItemSegment)) {
-            return false;
+    private Collection<ColumnProjection> getColumnProjections(final Projection projection) {
+        Collection<ColumnProjection> result = new LinkedList<>();
+        if (projection instanceof ColumnProjection) {
+            result.add((ColumnProjection) projection);
         }
-        if (!((ColumnOrderByItemSegment) orderItem).getColumn().getOwner().isPresent()) {
-            for (ShorthandProjection each : getQualifiedShorthandProjections(projections)) {
-                if (isSameProjection(each, (ColumnOrderByItemSegment) orderItem, tables)) {
-                    return true;
-                }
-            }
-        }
-        return false;
-    }
-    
-    private Collection<ShorthandProjection> getQualifiedShorthandProjections(final Collection<Projection> projections) {
-        Collection<ShorthandProjection> result = new LinkedList<>();
-        for (Projection each : projections) {
-            if (each instanceof ShorthandProjection && ((ShorthandProjection) each).getOwner().isPresent()) {
-                result.add((ShorthandProjection) each);
-            }
+        if (projection instanceof ShorthandProjection) {
+            result.addAll(((ShorthandProjection) projection).getActualColumns().values());
         }
         return result;
     }
     
-    private boolean isSameProjection(final ShorthandProjection shorthandProjection, final ColumnOrderByItemSegment orderItem, final Collection<SimpleTableSegment> tables) {
-        Preconditions.checkState(shorthandProjection.getOwner().isPresent());
-        SimpleTableSegment tableSegment = find(shorthandProjection.getOwner().get(), tables);
-        return schema.containsColumn(tableSegment.getTableName().getIdentifier().getValue(), orderItem.getColumn().getIdentifier().getValue());
-    }
-    
-    private boolean isSameAlias(final Projection projection, final TextOrderByItemSegment orderItem) {
-        return projection.getAlias().isPresent() 
-                && (SQLUtil.getExactlyValue(orderItem.getText()).equalsIgnoreCase(projection.getAlias().get()) || orderItem.getText().equalsIgnoreCase(projection.getExpression()));
+    private boolean isSameName(final ColumnProjection projection, final String text) {
+        return SQLUtil.getExactlyValue(text).equalsIgnoreCase(projection.getName());
     }
     
-    private boolean isSameQualifiedName(final Projection projection, final TextOrderByItemSegment orderItem) {
-        return !projection.getAlias().isPresent() && projection.getExpression().equalsIgnoreCase(orderItem.getText());
+    private boolean isSameAlias(final Projection projection, final String text) {
+        return projection.getAlias().isPresent() && SQLUtil.getExactlyValue(text).equalsIgnoreCase(projection.getAlias().get());
     }
     
-    private SimpleTableSegment find(final String tableNameOrAlias, final Collection<SimpleTableSegment> tables) {
-        for (SimpleTableSegment each : tables) {
-            if (tableNameOrAlias.equalsIgnoreCase(each.getTableName().getIdentifier().getValue()) || tableNameOrAlias.equalsIgnoreCase(each.getAlias().orElse(null))) {
-                return each;
-            }
-        }
-        throw new IllegalStateException("Can not find owner from table.");
+    private boolean isSameQualifiedName(final Projection projection, final String text) {
+        return SQLUtil.getExactlyValue(text).equalsIgnoreCase(projection.getExpression());
     }
 }
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..4be2404 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
@@ -20,17 +20,17 @@ 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.LinkedHashMap;
+import java.util.Map;
 import java.util.Optional;
 
 /**
  * Shorthand projection.
  */
-@RequiredArgsConstructor
 @Getter
 @EqualsAndHashCode
 @ToString
@@ -38,7 +38,12 @@ public final class ShorthandProjection implements Projection {
     
     private final String owner;
     
-    private final Collection<ColumnProjection> actualColumns;
+    private final Map<String, ColumnProjection> actualColumns = new LinkedHashMap<>();
+    
+    public ShorthandProjection(final String owner, final Collection<ColumnProjection> columnProjections) {
+        this.owner = owner;
+        columnProjections.forEach(each -> actualColumns.put(each.getExpression().toLowerCase(), each));
+    }
     
     @Override
     public String getExpression() {
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 3911fac..472dc86 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
@@ -31,6 +31,7 @@ import org.apache.shardingsphere.infra.binder.segment.select.projection.Projecti
 import org.apache.shardingsphere.infra.binder.segment.select.projection.engine.ProjectionsContextEngine;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationDistinctProjection;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationProjection;
+import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ColumnProjection;
 import org.apache.shardingsphere.infra.binder.segment.table.TablesContext;
 import org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
 import org.apache.shardingsphere.infra.binder.type.TableAvailable;
@@ -44,20 +45,16 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.Aggregat
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ColumnOrderByItemSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.ExpressionOrderByItemSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.IndexOrderByItemSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.OrderByItemSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.order.item.TextOrderByItemSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.WhereSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.JoinTableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SubqueryTableSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtil;
 import org.apache.shardingsphere.sql.parser.sql.common.util.SubqueryExtractUtil;
-import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
 
 import java.util.Collection;
-import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -90,7 +87,7 @@ public final class SelectStatementContext extends CommonSQLStatementContext<Sele
         groupByContext = new GroupByContextEngine().createGroupByContext(sqlStatement);
         orderByContext = new OrderByContextEngine().createOrderBy(schema, sqlStatement, groupByContext);
         projectionsContext = new ProjectionsContextEngine(schema, getDatabaseType())
-                .createProjectionsContext(getFromSimpleTableSegments(), getSqlStatement().getProjections(), groupByContext, orderByContext);
+                .createProjectionsContext(getSqlStatement().getFrom(), getSqlStatement().getProjections(), groupByContext, orderByContext);
         paginationContext = new PaginationContextEngine().createPaginationContext(sqlStatement, projectionsContext, parameters);
         Collection<SubquerySegment> subquerySegments = SubqueryExtractUtil.getSubquerySegments(getSqlStatement());
         containsSubquery = !subquerySegments.isEmpty();
@@ -170,7 +167,7 @@ public final class SelectStatementContext extends CommonSQLStatementContext<Sele
                     continue;
                 }
             }
-            String columnLabel = getAlias(((TextOrderByItemSegment) each.getSegment()).getText()).orElseGet(() -> getOrderItemText((TextOrderByItemSegment) each.getSegment()));
+            String columnLabel = getAlias(each.getSegment()).orElseGet(() -> getOrderItemText((TextOrderByItemSegment) each.getSegment()));
             Preconditions.checkState(columnLabelIndexMap.containsKey(columnLabel), "Can't find index: %s", each);
             if (columnLabelIndexMap.containsKey(columnLabel)) {
                 each.setIndex(columnLabelIndexMap.get(columnLabel));
@@ -178,11 +175,11 @@ public final class SelectStatementContext extends CommonSQLStatementContext<Sele
         }
     }
     
-    private Optional<String> getAlias(final String name) {
+    private Optional<String> getAlias(final OrderByItemSegment orderByItem) {
         if (projectionsContext.isUnqualifiedShorthandProjection()) {
             return Optional.empty();
         }
-        String rawName = SQLUtil.getExactlyValue(name);
+        String rawName = SQLUtil.getExactlyValue(((TextOrderByItemSegment) orderByItem).getText());
         for (Projection each : projectionsContext.getProjections()) {
             if (SQLUtil.getExactlyExpression(rawName).equalsIgnoreCase(SQLUtil.getExactlyExpression(SQLUtil.getExactlyValue(each.getExpression())))) {
                 return each.getAlias();
@@ -190,10 +187,17 @@ public final class SelectStatementContext extends CommonSQLStatementContext<Sele
             if (rawName.equalsIgnoreCase(each.getAlias().orElse(null))) {
                 return Optional.of(rawName);
             }
+            if (isSameColumnName(each, rawName)) {
+                return each.getAlias();
+            }
         }
         return Optional.empty();
     }
     
+    private boolean isSameColumnName(final Projection projection, final String name) {
+        return projection instanceof ColumnProjection && name.equalsIgnoreCase(((ColumnProjection) projection).getName());
+    }
+    
     private String getOrderItemText(final TextOrderByItemSegment orderByItemSegment) {
         return orderByItemSegment instanceof ColumnOrderByItemSegment
                 ? ((ColumnOrderByItemSegment) orderByItemSegment).getColumn().getIdentifier().getValue() : ((ExpressionOrderByItemSegment) orderByItemSegment).getExpression();
@@ -223,22 +227,4 @@ public final class SelectStatementContext extends CommonSQLStatementContext<Sele
         tableExtractor.extractTablesFromSelect(getSqlStatement());
         return tableExtractor.getRewriteTables();
     }
-    
-    /**
-     * Get tables with from clause.
-     *
-     * @return tables with from clause
-     */
-    public Collection<SimpleTableSegment> getFromSimpleTableSegments() {
-        Collection<SimpleTableSegment> result = new LinkedList<>();
-        TableExtractor extractor = new TableExtractor();
-        result.addAll(extractor.extractTablesWithFromClause(getSqlStatement()));
-        result.addAll(getTemporarySimpleTableSegments(extractor.getTableContext()));
-        return result;
-    }
-    
-    private Collection<SimpleTableSegment> getTemporarySimpleTableSegments(final Collection<TableSegment> tableSegments) {
-        return tableSegments.stream().filter(each -> each instanceof SubqueryTableSegment).map(each -> new SimpleTableSegment(
-                new TableNameSegment(each.getStartIndex(), each.getStopIndex(), new IdentifierValue(each.getAlias().orElse(""))))).collect(Collectors.toList());
-    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/RowNumberPaginationContextEngineTest.java b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/RowNumberPaginationContextEngineTest.java
index af2387b..687092e 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/RowNumberPaginationContextEngineTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/RowNumberPaginationContextEngineTest.java
@@ -17,9 +17,9 @@
 
 package org.apache.shardingsphere.infra.binder.segment.select.pagination.engine;
 
+import org.apache.shardingsphere.infra.binder.segment.select.pagination.PaginationContext;
 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.pagination.PaginationContext;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ColumnProjection;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
@@ -50,7 +50,7 @@ public final class RowNumberPaginationContextEngineTest {
     @Test
     public void assertCreatePaginationContextWhenRowNumberAliasNotPresent() {
         ProjectionsContext projectionsContext = new ProjectionsContext(0, 0, false, Collections.emptyList());
-        PaginationContext paginationContext = new RowNumberPaginationContextEngine().createPaginationContext(null, projectionsContext, Collections.emptyList());
+        PaginationContext paginationContext = new RowNumberPaginationContextEngine().createPaginationContext(Collections.emptyList(), projectionsContext, Collections.emptyList());
         assertFalse(paginationContext.getOffsetSegment().isPresent());
         assertFalse(paginationContext.getRowCountSegment().isPresent());
     }
@@ -59,7 +59,7 @@ public final class RowNumberPaginationContextEngineTest {
     public void assertCreatePaginationContextWhenRowNumberAliasIsPresentAndRowNumberPredicatesIsEmpty() {
         Projection projectionWithRowNumberAlias = new ColumnProjection(null, ROW_NUMBER_COLUMN_NAME, ROW_NUMBER_COLUMN_ALIAS);
         ProjectionsContext projectionsContext = new ProjectionsContext(0, 0, false, Collections.singleton(projectionWithRowNumberAlias));
-        PaginationContext paginationContext = new RowNumberPaginationContextEngine().createPaginationContext(null, projectionsContext, Collections.emptyList());
+        PaginationContext paginationContext = new RowNumberPaginationContextEngine().createPaginationContext(Collections.emptyList(), projectionsContext, Collections.emptyList());
         assertFalse(paginationContext.getOffsetSegment().isPresent());
         assertFalse(paginationContext.getRowCountSegment().isPresent());
     }
@@ -92,7 +92,7 @@ public final class RowNumberPaginationContextEngineTest {
         ColumnSegment left = new ColumnSegment(0, 10, new IdentifierValue(ROW_NUMBER_COLUMN_NAME));
         BinaryOperationExpression predicateSegment = new BinaryOperationExpression(0, 0, left, null, null, null);
         andPredicate.getPredicates().addAll(Collections.singleton(predicateSegment));
-        PaginationContext paginationContext = new RowNumberPaginationContextEngine().createPaginationContext(null, projectionsContext, Collections.emptyList());
+        PaginationContext paginationContext = new RowNumberPaginationContextEngine().createPaginationContext(Collections.emptyList(), projectionsContext, Collections.emptyList());
         assertFalse(paginationContext.getOffsetSegment().isPresent());
         assertFalse(paginationContext.getRowCountSegment().isPresent());
     }
@@ -105,7 +105,7 @@ public final class RowNumberPaginationContextEngineTest {
         ParameterMarkerExpressionSegment right = new ParameterMarkerExpressionSegment(0, 10, 0);
         BinaryOperationExpression expression = new BinaryOperationExpression(0, 0, left, right, ">", null);
         PaginationContext paginationContext = new RowNumberPaginationContextEngine()
-                .createPaginationContext(expression, projectionsContext, Collections.singletonList(1));
+                .createPaginationContext(Collections.singletonList(expression), projectionsContext, Collections.singletonList(1));
         Optional<PaginationValueSegment> offSetSegmentPaginationValue = paginationContext.getOffsetSegment();
         assertTrue(offSetSegmentPaginationValue.isPresent());
         assertThat(offSetSegmentPaginationValue.get(), instanceOf(ParameterMarkerRowNumberValueSegment.class));
@@ -118,7 +118,7 @@ public final class RowNumberPaginationContextEngineTest {
         ColumnSegment left = new ColumnSegment(0, 10, new IdentifierValue(ROW_NUMBER_COLUMN_NAME));
         LiteralExpressionSegment right = new LiteralExpressionSegment(0, 10, 100);
         BinaryOperationExpression expression = new BinaryOperationExpression(0, 0, left, right, operator, null);
-        PaginationContext paginationContext = new RowNumberPaginationContextEngine().createPaginationContext(expression, projectionsContext, Collections.emptyList());
+        PaginationContext paginationContext = new RowNumberPaginationContextEngine().createPaginationContext(Collections.singletonList(expression), projectionsContext, Collections.emptyList());
         assertFalse(paginationContext.getOffsetSegment().isPresent());
         Optional<PaginationValueSegment> paginationValueSegmentOptional = paginationContext.getRowCountSegment();
         assertTrue(paginationValueSegmentOptional.isPresent());
@@ -137,7 +137,7 @@ public final class RowNumberPaginationContextEngineTest {
         LiteralExpressionSegment right = new LiteralExpressionSegment(0, 10, 100);
         BinaryOperationExpression expression = new BinaryOperationExpression(0, 0, left, right, operator, null);
         PaginationContext rowNumberPaginationContextEngine = new RowNumberPaginationContextEngine()
-                .createPaginationContext(expression, projectionsContext, Collections.emptyList());
+                .createPaginationContext(Collections.singletonList(expression), projectionsContext, Collections.emptyList());
         Optional<PaginationValueSegment> paginationValueSegment = rowNumberPaginationContextEngine.getOffsetSegment();
         assertTrue(paginationValueSegment.isPresent());
         PaginationValueSegment actualPaginationValueSegment = paginationValueSegment.get();
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/TopPaginationContextEngineTest.java b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/TopPaginationContextEngineTest.java
index 9f7fa6c..5c395fb 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/TopPaginationContextEngineTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/pagination/engine/TopPaginationContextEngineTest.java
@@ -28,12 +28,10 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.Pa
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.rownum.NumberLiteralRowNumberValueSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.rownum.ParameterMarkerRowNumberValueSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.pagination.top.TopProjectionSegment;
-import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.predicate.AndPredicate;
 import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
 import org.junit.Before;
 import org.junit.Test;
 
-import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 
@@ -55,9 +53,8 @@ public final class TopPaginationContextEngineTest {
     @Test
     public void assertCreatePaginationContextWhenRowNumberPredicateNotPresent() {
         TopProjectionSegment topProjectionSegment = new TopProjectionSegment(0, 10, null, "rowNumberAlias");
-        Collection<AndPredicate> andPredicates = Collections.emptyList();
         List<Object> parameters = Collections.emptyList();
-        PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(topProjectionSegment, null, parameters);
+        PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(topProjectionSegment, Collections.emptyList(), parameters);
         assertFalse(paginationContext.getOffsetSegment().isPresent());
         assertFalse(paginationContext.getRowCountSegment().isPresent());
     }
@@ -77,7 +74,8 @@ public final class TopPaginationContextEngineTest {
         String name = "rowNumberAlias";
         ColumnSegment columnSegment = new ColumnSegment(0, 10, new IdentifierValue(name));
         InExpression inExpression = new InExpression(0, 0, columnSegment, new ListExpression(0, 0), false);
-        PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(new TopProjectionSegment(0, 10, null, name), inExpression, Collections.emptyList());
+        PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(
+                new TopProjectionSegment(0, 10, null, name), Collections.singletonList(inExpression), Collections.emptyList());
         assertFalse(paginationContext.getOffsetSegment().isPresent());
         assertFalse(paginationContext.getRowCountSegment().isPresent());
     }
@@ -88,7 +86,8 @@ public final class TopPaginationContextEngineTest {
         ColumnSegment left = new ColumnSegment(0, 10, new IdentifierValue(name));
         ParameterMarkerExpressionSegment right = new ParameterMarkerExpressionSegment(0, 10, 0);
         BinaryOperationExpression expression = new BinaryOperationExpression(0, 0, left, right, ">", null);
-        PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(new TopProjectionSegment(0, 10, null, name), expression, Collections.singletonList(1));
+        PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(
+                new TopProjectionSegment(0, 10, null, name), Collections.singletonList(expression), Collections.singletonList(1));
         assertTrue(paginationContext.getOffsetSegment().isPresent());
         PaginationValueSegment paginationValueSegment = paginationContext.getOffsetSegment().get();
         assertThat(paginationValueSegment, instanceOf(ParameterMarkerRowNumberValueSegment.class));
@@ -104,7 +103,8 @@ public final class TopPaginationContextEngineTest {
         ColumnSegment left = new ColumnSegment(0, 10, new IdentifierValue(name));
         LiteralExpressionSegment right = new LiteralExpressionSegment(0, 10, 100);
         BinaryOperationExpression expression = new BinaryOperationExpression(0, 0, left, right, operator, null);
-        PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(new TopProjectionSegment(0, 10, null, name), expression, Collections.emptyList());
+        PaginationContext paginationContext = topPaginationContextEngine.createPaginationContext(
+                new TopProjectionSegment(0, 10, null, name), Collections.singletonList(expression), Collections.emptyList());
         assertTrue(paginationContext.getOffsetSegment().isPresent());
         PaginationValueSegment paginationValueSegment = paginationContext.getOffsetSegment().get();
         assertThat(paginationValueSegment, instanceOf(NumberLiteralRowNumberValueSegment.class));
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngineTest.java b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngineTest.java
index 5fc722b..6d48084 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngineTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionEngineTest.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.infra.binder.segment.select.projection.engine;
 
-import com.google.common.collect.Sets;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.Projection;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationDistinctProjection;
 import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.AggregationProjection;
@@ -37,6 +36,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegm
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableSegment;
 import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -44,9 +44,8 @@ import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 
 import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.LinkedList;
+import java.util.LinkedHashMap;
+import java.util.Map;
 import java.util.Optional;
 
 import static org.hamcrest.CoreMatchers.instanceOf;
@@ -54,6 +53,7 @@ import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 @RunWith(MockitoJUnitRunner.class)
@@ -67,36 +67,37 @@ public final class ProjectionEngineTest {
     
     @Test
     public void assertCreateProjectionWhenProjectionSegmentNotMatched() {
-        assertFalse(new ProjectionEngine(schema, databaseType).createProjection(Collections.emptyList(), null).isPresent());
+        assertFalse(new ProjectionEngine(schema, databaseType).createProjection(mock(TableSegment.class), null).isPresent());
     }
     
     @Test
     public void assertCreateProjectionWhenProjectionSegmentInstanceOfShorthandProjectionSegment() {
         ShorthandProjectionSegment shorthandProjectionSegment = new ShorthandProjectionSegment(0, 0);
         shorthandProjectionSegment.setOwner(new OwnerSegment(0, 0, new IdentifierValue("tbl")));
-        Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(Collections.emptyList(), shorthandProjectionSegment);
+        Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(mock(TableSegment.class), shorthandProjectionSegment);
         assertTrue(actual.isPresent());
         assertThat(actual.get(), instanceOf(ShorthandProjection.class));
     }
     
     @Test
     public void assertCreateProjectionWhenProjectionSegmentInstanceOfShorthandProjectionSegmentAndDuplicateTableSegment() {
-        Collection<SimpleTableSegment> tableSegments = new LinkedList<>();
-        tableSegments.add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
-        tableSegments.add(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order"))));
+        SimpleTableSegment table = new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order")));
         when(schema.getAllColumnNames("t_order")).thenReturn(Arrays.asList("order_id", "content"));
-        Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(tableSegments, new ShorthandProjectionSegment(0, 0));
+        Optional<Projection> actual = new ProjectionEngine(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));
-        assertThat(((ShorthandProjection) actual.get()).getActualColumns(), is(Sets.newHashSet(new ColumnProjection("t_order", "order_id", null), new ColumnProjection("t_order", "content", null))));
+        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));
     }
     
     @Test
     public void assertCreateProjectionWhenProjectionSegmentInstanceOfColumnProjectionSegment() {
         ColumnProjectionSegment columnProjectionSegment = new ColumnProjectionSegment(new ColumnSegment(0, 10, new IdentifierValue("name")));
         columnProjectionSegment.setAlias(new AliasSegment(0, 0, new IdentifierValue("alias")));
-        Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(Collections.emptyList(), columnProjectionSegment);
+        Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(mock(TableSegment.class), columnProjectionSegment);
         assertTrue(actual.isPresent());
         assertThat(actual.get(), instanceOf(ColumnProjection.class));
     }
@@ -104,7 +105,7 @@ public final class ProjectionEngineTest {
     @Test
     public void assertCreateProjectionWhenProjectionSegmentInstanceOfExpressionProjectionSegment() {
         ExpressionProjectionSegment expressionProjectionSegment = new ExpressionProjectionSegment(0, 10, "text");
-        Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(Collections.emptyList(), expressionProjectionSegment);
+        Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(mock(TableSegment.class), expressionProjectionSegment);
         assertTrue(actual.isPresent());
         assertThat(actual.get(), instanceOf(ExpressionProjection.class));
     }
@@ -112,7 +113,7 @@ public final class ProjectionEngineTest {
     @Test
     public void assertCreateProjectionWhenProjectionSegmentInstanceOfAggregationDistinctProjectionSegment() {
         AggregationDistinctProjectionSegment aggregationDistinctProjectionSegment = new AggregationDistinctProjectionSegment(0, 10, AggregationType.COUNT, "(1)", "distinctExpression");
-        Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(Collections.emptyList(), aggregationDistinctProjectionSegment);
+        Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(mock(TableSegment.class), aggregationDistinctProjectionSegment);
         assertTrue(actual.isPresent());
         assertThat(actual.get(), instanceOf(AggregationDistinctProjection.class));
     }
@@ -120,7 +121,7 @@ public final class ProjectionEngineTest {
     @Test
     public void assertCreateProjectionWhenProjectionSegmentInstanceOfAggregationProjectionSegment() {
         AggregationProjectionSegment aggregationProjectionSegment = new AggregationProjectionSegment(0, 10, AggregationType.COUNT, "(1)");
-        Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(Collections.emptyList(), aggregationProjectionSegment);
+        Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(mock(TableSegment.class), aggregationProjectionSegment);
         assertTrue(actual.isPresent());
         assertThat(actual.get(), instanceOf(AggregationProjection.class));
     }
@@ -128,7 +129,7 @@ public final class ProjectionEngineTest {
     @Test
     public void assertCreateProjectionWhenProjectionSegmentInstanceOfAggregationDistinctProjectionSegmentAndAggregationTypeIsAvg() {
         AggregationDistinctProjectionSegment aggregationDistinctProjectionSegment = new AggregationDistinctProjectionSegment(0, 10, AggregationType.AVG, "(1)", "distinctExpression");
-        Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(Collections.emptyList(), aggregationDistinctProjectionSegment);
+        Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(mock(TableSegment.class), aggregationDistinctProjectionSegment);
         assertTrue(actual.isPresent());
         assertThat(actual.get(), instanceOf(AggregationDistinctProjection.class));
     }
@@ -136,7 +137,7 @@ public final class ProjectionEngineTest {
     @Test
     public void assertCreateProjectionWhenProjectionSegmentInstanceOfAggregationProjectionSegmentAndAggregationTypeIsAvg() {
         AggregationProjectionSegment aggregationProjectionSegment = new AggregationProjectionSegment(0, 10, AggregationType.AVG, "(1)");
-        Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(Collections.emptyList(), aggregationProjectionSegment);
+        Optional<Projection> actual = new ProjectionEngine(schema, databaseType).createProjection(mock(TableSegment.class), aggregationProjectionSegment);
         assertTrue(actual.isPresent());
         assertThat(actual.get(), instanceOf(AggregationProjection.class));
     }
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 a1d5621..9e42664 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
@@ -51,7 +51,6 @@ import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 
 import java.util.Arrays;
-import java.util.Collection;
 import java.util.Collections;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -94,11 +93,10 @@ public final class ProjectionsContextEngineTest {
     private void assertProjectionsContextCreatedProperly(final SelectStatement selectStatement) {
         selectStatement.setProjections(new ProjectionsSegment(0, 0));
         SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement);
-        Collection<SimpleTableSegment> tables = selectStatementContext.getAllTables();
         ProjectionsSegment projectionsSegment = selectStatement.getProjections();
         ProjectionsContextEngine projectionsContextEngine = new ProjectionsContextEngine(null, selectStatementContext.getDatabaseType());
         ProjectionsContext actual = projectionsContextEngine
-                .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false));
+                .createProjectionsContext(selectStatement.getFrom(), projectionsSegment, new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false));
         assertNotNull(actual);
     }
     
@@ -135,9 +133,8 @@ public final class ProjectionsContextEngineTest {
         shorthandProjectionSegment.setOwner(owner);
         projectionsSegment.getProjections().addAll(Collections.singleton(shorthandProjectionSegment));
         SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement);
-        Collection<SimpleTableSegment> tables = selectStatementContext.getAllTables();
         ProjectionsContext actual = new ProjectionsContextEngine(schema, selectStatementContext.getDatabaseType())
-                .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false));
+                .createProjectionsContext(selectStatement.getFrom(), projectionsSegment, new GroupByContext(Collections.emptyList()), new OrderByContext(Collections.emptyList(), false));
         assertNotNull(actual);
     }
     
@@ -176,9 +173,8 @@ 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 = createSelectStatementContext(selectStatement);
-        Collection<SimpleTableSegment> tables = selectStatementContext.getAllTables();
         ProjectionsContext actual = new ProjectionsContextEngine(schema, selectStatementContext.getDatabaseType())
-                .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
+                .createProjectionsContext(selectStatement.getFrom(), projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
         assertNotNull(actual);
     }
     
@@ -217,9 +213,8 @@ public final class ProjectionsContextEngineTest {
         OrderByItem orderByItem = new OrderByItem(new ExpressionOrderByItemSegment(0, 1, "", OrderDirection.ASC));
         OrderByContext orderByContext = new OrderByContext(Collections.singletonList(orderByItem), true);
         SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement);
-        Collection<SimpleTableSegment> tables = selectStatementContext.getAllTables();
         ProjectionsContext actual = new ProjectionsContextEngine(schema, selectStatementContext.getDatabaseType())
-                .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
+                .createProjectionsContext(selectStatement.getFrom(), projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
         assertNotNull(actual);
     }
     
@@ -266,9 +261,8 @@ 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 = createSelectStatementContext(selectStatement);
-        Collection<SimpleTableSegment> tables = selectStatementContext.getAllTables();
         ProjectionsContext actual = new ProjectionsContextEngine(schema, selectStatementContext.getDatabaseType())
-                .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
+                .createProjectionsContext(selectStatement.getFrom(), projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
         assertNotNull(actual);
     }
     
@@ -308,9 +302,8 @@ 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 = createSelectStatementContext(selectStatement);
-        Collection<SimpleTableSegment> tables = selectStatementContext.getAllTables();
         ProjectionsContext actual = new ProjectionsContextEngine(schema, selectStatementContext.getDatabaseType())
-                .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
+                .createProjectionsContext(selectStatement.getFrom(), projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
         assertNotNull(actual);
     }
     
@@ -358,9 +351,8 @@ 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 = createSelectStatementContext(selectStatement);
-        Collection<SimpleTableSegment> tables = selectStatementContext.getAllTables();
         ProjectionsContext actual = new ProjectionsContextEngine(schema, selectStatementContext.getDatabaseType())
-                .createProjectionsContext(tables, projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
+                .createProjectionsContext(selectStatement.getFrom(), projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
         assertNotNull(actual);
     }
         
@@ -405,7 +397,7 @@ public final class ProjectionsContextEngineTest {
         GroupByContext groupByContext = new GroupByContext(Collections.singleton(groupByItem));
         SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement);
         ProjectionsContext actual = new ProjectionsContextEngine(schema, selectStatementContext.getDatabaseType())
-                .createProjectionsContext(selectStatementContext.getFromSimpleTableSegments(), projectionsSegment, groupByContext, new OrderByContext(Collections.emptyList(), false));
+                .createProjectionsContext(selectStatement.getFrom(), projectionsSegment, groupByContext, new OrderByContext(Collections.emptyList(), false));
         assertNotNull(actual);
     }
     
@@ -450,7 +442,7 @@ public final class ProjectionsContextEngineTest {
         GroupByContext groupByContext = new GroupByContext(Collections.singleton(groupByItem));
         SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement);
         ProjectionsContext actual = new ProjectionsContextEngine(schema, selectStatementContext.getDatabaseType())
-                .createProjectionsContext(selectStatementContext.getFromSimpleTableSegments(), projectionsSegment, groupByContext, new OrderByContext(Collections.emptyList(), false));
+                .createProjectionsContext(selectStatement.getFrom(), projectionsSegment, groupByContext, new OrderByContext(Collections.emptyList(), false));
         assertNotNull(actual);
     }
     
@@ -465,7 +457,7 @@ public final class ProjectionsContextEngineTest {
         OrderByContext orderByContext = new OrderByContext(Collections.singleton(orderByItem), false);
         SelectStatementContext selectStatementContext = createSelectStatementContext(selectStatement);
         ProjectionsContext actual = new ProjectionsContextEngine(schema, selectStatementContext.getDatabaseType())
-                .createProjectionsContext(selectStatementContext.getFromSimpleTableSegments(), projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
+                .createProjectionsContext(selectStatement.getFrom(), projectionsSegment, new GroupByContext(Collections.emptyList()), orderByContext);
         assertThat(actual.getProjections().size(), is(2));
     }
 }