Repository: apache/incubator-shardingsphere
Pull request #4528 (2020-02-29 09:21:43)

Move getAllTables' logic from TableTokenGenerator to SQLStatement
For #4520.




Patch:
diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/TableTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/TableTokenGenerator.java
index 339ffcb0ba4..d5b37258215 100644
--- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/TableTokenGenerator.java
+++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/TableTokenGenerator.java
@@ -17,31 +17,13 @@
 
 package org.apache.shardingsphere.sharding.rewrite.token.generator.impl;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
 import lombok.Setter;
 import org.apache.shardingsphere.core.rule.ShardingRule;
 import org.apache.shardingsphere.core.rule.aware.ShardingRuleAware;
 import org.apache.shardingsphere.sharding.rewrite.token.pojo.impl.TableToken;
-import org.apache.shardingsphere.sql.parser.relation.segment.table.Table;
-import org.apache.shardingsphere.sql.parser.relation.segment.table.TablesContext;
 import org.apache.shardingsphere.sql.parser.relation.statement.SQLStatementContext;
-import org.apache.shardingsphere.sql.parser.sql.segment.dml.column.ColumnSegment;
-import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ColumnProjectionSegment;
-import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ProjectionSegment;
-import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ProjectionsSegment;
-import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ShorthandProjectionSegment;
-import org.apache.shardingsphere.sql.parser.sql.segment.dml.order.item.ColumnOrderByItemSegment;
-import org.apache.shardingsphere.sql.parser.sql.segment.dml.order.item.OrderByItemSegment;
-import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.AndPredicate;
-import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.PredicateSegment;
-import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.WhereSegment;
-import org.apache.shardingsphere.sql.parser.sql.segment.generic.OwnerAvailable;
-import org.apache.shardingsphere.sql.parser.sql.segment.generic.OwnerSegment;
 import org.apache.shardingsphere.sql.parser.sql.segment.generic.TableSegment;
-import org.apache.shardingsphere.sql.parser.sql.statement.dml.SelectStatement;
 import org.apache.shardingsphere.sql.parser.sql.statement.generic.TableSegmentsAvailable;
-import org.apache.shardingsphere.sql.parser.sql.statement.generic.WhereSegmentAvailable;
 import org.apache.shardingsphere.underlying.rewrite.sql.token.generator.CollectionSQLTokenGenerator;
 
 import java.util.Collection;
@@ -63,128 +45,17 @@ public final class TableTokenGenerator implements CollectionSQLTokenGenerator, S
     
     @Override
     public Collection<TableToken> generateSQLTokens(final SQLStatementContext sqlStatementContext) {
-        if (!(sqlStatementContext.getSqlStatement() instanceof TableSegmentsAvailable)) {
-            return Collections.emptyList();
-        }
-        Collection<TableToken> result = new LinkedList<>();
-        for (TableSegment each : ((TableSegmentsAvailable) sqlStatementContext.getSqlStatement()).getAllTables()) {
-            if (shardingRule.findTableRule(each.getIdentifier().getValue()).isPresent()) {
-                result.add(new TableToken(each.getStartIndex(), each.getStopIndex(), each.getIdentifier()));
-            }
-        }
-        if (sqlStatementContext.getSqlStatement() instanceof WhereSegmentAvailable && ((WhereSegmentAvailable) sqlStatementContext.getSqlStatement()).getWhere().isPresent()) {
-            result.addAll(generateSQLTokens(sqlStatementContext, ((WhereSegmentAvailable) sqlStatementContext.getSqlStatement()).getWhere().get()));
-        }
-        if (sqlStatementContext.getSqlStatement() instanceof SelectStatement) {
-            result.addAll(generateSQLTokens(sqlStatementContext, ((SelectStatement) sqlStatementContext.getSqlStatement()).getProjections()));
-            if (((SelectStatement) sqlStatementContext.getSqlStatement()).getGroupBy().isPresent()) {
-                result.addAll(generateSQLTokens(sqlStatementContext, ((SelectStatement) sqlStatementContext.getSqlStatement()).getGroupBy().get().getGroupByItems()));
-            }
-            if (((SelectStatement) sqlStatementContext.getSqlStatement()).getOrderBy().isPresent()) {
-                result.addAll(generateSQLTokens(sqlStatementContext, ((SelectStatement) sqlStatementContext.getSqlStatement()).getOrderBy().get().getOrderByItems()));
-            }
-        }
-        return result;
-    }
-    
-    private Collection<TableToken> generateSQLTokens(final SQLStatementContext sqlStatementContext, final WhereSegment where) {
-        Collection<TableToken> result = new LinkedList<>();
-        for (AndPredicate each : where.getAndPredicates()) {
-            for (PredicateSegment predicate : each.getPredicates()) {
-                result.addAll(getTableTokens(sqlStatementContext, predicate));
-            }
-        }
-        return result;
-    }
-    
-    private Collection<TableToken> generateSQLTokens(final SQLStatementContext sqlStatementContext, final ProjectionsSegment projectionsSegment) {
-        Collection<TableToken> result = new LinkedList<>();
-        for (ProjectionSegment each : projectionsSegment.getProjections()) {
-            if (each instanceof ShorthandProjectionSegment) {
-                Optional<TableToken> tableToken = generateSQLToken(sqlStatementContext, (ShorthandProjectionSegment) each);
-                if (tableToken.isPresent()) {
-                    result.add(tableToken.get());
-                }
-            }
-            if (each instanceof ColumnProjectionSegment) {
-                Optional<TableToken> tableToken = generateSQLToken(sqlStatementContext, (ColumnProjectionSegment) each);
-                if (tableToken.isPresent()) {
-                    result.add(tableToken.get());
-                }
-            }
-        }
-        return result;
-    }
-    
-    private Collection<TableToken> generateSQLTokens(final SQLStatementContext sqlStatementContext, final Collection<OrderByItemSegment> orderBys) {
-        Collection<TableToken> result = new LinkedList<>();
-        for (OrderByItemSegment each : orderBys) {
-            if (isToGenerateTableToken(sqlStatementContext.getTablesContext(), each)) {
-                Preconditions.checkState(((ColumnOrderByItemSegment) each).getColumn().getOwner().isPresent());
-                OwnerSegment segment = ((ColumnOrderByItemSegment) each).getColumn().getOwner().get();
-                result.add(new TableToken(segment.getStartIndex(), segment.getStopIndex(), segment.getIdentifier()));
-            }
-        }
-        return result;
-    }
-    
-    private Optional<TableToken> generateSQLToken(final SQLStatementContext sqlStatementContext, final OwnerAvailable segment) {
-        Optional<OwnerSegment> owner = segment.getOwner();
-        return owner.isPresent() && isToGenerateTableToken(sqlStatementContext, owner.get())
-                ? Optional.of(new TableToken(owner.get().getStartIndex(), owner.get().getStopIndex(), owner.get().getIdentifier())) : Optional.<TableToken>absent();
+        return sqlStatementContext.getSqlStatement() instanceof TableSegmentsAvailable
+                ? generateSQLTokens((TableSegmentsAvailable) sqlStatementContext.getSqlStatement()) : Collections.<TableToken>emptyList();
     }
     
-    private Collection<TableToken> getTableTokens(final SQLStatementContext sqlStatementContext, final PredicateSegment predicate) {
+    private Collection<TableToken> generateSQLTokens(final TableSegmentsAvailable sqlStatement) {
         Collection<TableToken> result = new LinkedList<>();
-        if (isToGenerateTableTokenLeftValue(sqlStatementContext.getTablesContext(), predicate)) {
-            Preconditions.checkState(predicate.getColumn().getOwner().isPresent());
-            OwnerSegment segment = predicate.getColumn().getOwner().get();
-            if (shardingRule.findTableRule(segment.getIdentifier().getValue()).isPresent()) {
-                result.add(new TableToken(segment.getStartIndex(), segment.getStopIndex(), segment.getIdentifier()));
-            }
-        }
-        if (isToGenerateTableTokenForRightValue(sqlStatementContext.getTablesContext(), predicate)) {
-            Preconditions.checkState(((ColumnSegment) predicate.getRightValue()).getOwner().isPresent());
-            OwnerSegment segment = ((ColumnSegment) predicate.getRightValue()).getOwner().get();
-            if (shardingRule.findTableRule(segment.getIdentifier().getValue()).isPresent()) {
-                result.add(new TableToken(segment.getStartIndex(), segment.getStopIndex(), segment.getIdentifier()));
-            }
-        }
-        if (isToGenerateTableTokenForProjection(sqlStatementContext.getTablesContext(), predicate)) {
-            Preconditions.checkState(((ColumnProjectionSegment) predicate.getRightValue()).getOwner().isPresent());
-            OwnerSegment segment = ((ColumnProjectionSegment) predicate.getRightValue()).getOwner().get();
-            if (shardingRule.findTableRule(segment.getIdentifier().getValue()).isPresent()) {
-                result.add(new TableToken(segment.getStartIndex(), segment.getStopIndex(), segment.getIdentifier()));
+        for (TableSegment each : sqlStatement.getAllTables()) {
+            if (shardingRule.findTableRule(each.getIdentifier().getValue()).isPresent()) {
+                result.add(new TableToken(each.getStartIndex(), each.getStopIndex(), each.getIdentifier()));
             }
         }
         return result;
     }
-    
-    private boolean isToGenerateTableTokenLeftValue(final TablesContext tablesContext, final PredicateSegment predicate) {
-        return predicate.getColumn().getOwner().isPresent() && isTable(predicate.getColumn().getOwner().get(), tablesContext);
-    }
-    
-    private boolean isToGenerateTableTokenForRightValue(final TablesContext tablesContext, final PredicateSegment predicate) {
-        return predicate.getRightValue() instanceof ColumnSegment
-                && ((ColumnSegment) predicate.getRightValue()).getOwner().isPresent() && isTable(((ColumnSegment) predicate.getRightValue()).getOwner().get(), tablesContext);
-    }
-    
-    private boolean isToGenerateTableTokenForProjection(final TablesContext tablesContext, final PredicateSegment predicate) {
-        return predicate.getRightValue() instanceof ColumnProjectionSegment && ((ColumnProjectionSegment) predicate.getRightValue()).getOwner().isPresent()
-                && isTable(((ColumnProjectionSegment) predicate.getRightValue()).getOwner().get(), tablesContext);
-    }
-    
-    private boolean isToGenerateTableToken(final TablesContext tablesContext, final OrderByItemSegment orderByItemSegment) {
-        return orderByItemSegment instanceof ColumnOrderByItemSegment && ((ColumnOrderByItemSegment) orderByItemSegment).getColumn().getOwner().isPresent() 
-                && isTable(((ColumnOrderByItemSegment) orderByItemSegment).getColumn().getOwner().get(), tablesContext);
-    }
-    
-    private boolean isToGenerateTableToken(final SQLStatementContext sqlStatementContext, final OwnerSegment ownerSegment) {
-        Optional<Table> table = sqlStatementContext.getTablesContext().find(ownerSegment.getIdentifier().getValue());
-        return table.isPresent() && !table.get().getAlias().isPresent();
-    }
-    
-    private boolean isTable(final OwnerSegment owner, final TablesContext tablesContext) {
-        return !tablesContext.findTableFromAlias(owner.getIdentifier().getValue()).isPresent();
-    }
 }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/sql/predicate/PredicateExtractor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/sql/predicate/PredicateExtractor.java
new file mode 100644
index 00000000000..4d9753aea95
--- /dev/null
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/sql/predicate/PredicateExtractor.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.sql.parser.sql.predicate;
+
+import com.google.common.base.Preconditions;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.sql.parser.sql.segment.dml.column.ColumnSegment;
+import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ColumnProjectionSegment;
+import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.PredicateSegment;
+import org.apache.shardingsphere.sql.parser.sql.segment.generic.OwnerSegment;
+import org.apache.shardingsphere.sql.parser.sql.segment.generic.TableSegment;
+
+import java.util.Collection;
+import java.util.LinkedList;
+
+/**
+ * Predicate extractor.
+ */
+@RequiredArgsConstructor
+public final class PredicateExtractor {
+    
+    private final Collection<TableSegment> tables;
+    
+    private final PredicateSegment predicate;
+    
+    /**
+     * Extract tables.
+     * 
+     * @return table segments
+     */
+    public Collection<TableSegment> extractTables() {
+        Collection<TableSegment> result = new LinkedList<>();
+        if (isToGenerateTableTokenLeftValue()) {
+            Preconditions.checkState(predicate.getColumn().getOwner().isPresent());
+            OwnerSegment segment = predicate.getColumn().getOwner().get();
+            result.add(new TableSegment(segment.getStartIndex(), segment.getStopIndex(), segment.getIdentifier()));
+        }
+        if (isToGenerateTableTokenForRightValue()) {
+            Preconditions.checkState(((ColumnSegment) predicate.getRightValue()).getOwner().isPresent());
+            OwnerSegment segment = ((ColumnSegment) predicate.getRightValue()).getOwner().get();
+            result.add(new TableSegment(segment.getStartIndex(), segment.getStopIndex(), segment.getIdentifier()));
+        }
+        if (isToGenerateTableTokenForProjection()) {
+            Preconditions.checkState(((ColumnProjectionSegment) predicate.getRightValue()).getOwner().isPresent());
+            OwnerSegment segment = ((ColumnProjectionSegment) predicate.getRightValue()).getOwner().get();
+            new TableSegment(segment.getStartIndex(), segment.getStopIndex(), segment.getIdentifier());
+        }
+        return result;
+    }
+    
+    private boolean isToGenerateTableTokenLeftValue() {
+        return predicate.getColumn().getOwner().isPresent() && isTable(predicate.getColumn().getOwner().get());
+    }
+    
+    private boolean isToGenerateTableTokenForRightValue() {
+        return predicate.getRightValue() instanceof ColumnSegment
+                && ((ColumnSegment) predicate.getRightValue()).getOwner().isPresent() && isTable(((ColumnSegment) predicate.getRightValue()).getOwner().get());
+    }
+    
+    private boolean isToGenerateTableTokenForProjection() {
+        return predicate.getRightValue() instanceof ColumnProjectionSegment && ((ColumnProjectionSegment) predicate.getRightValue()).getOwner().isPresent()
+                && isTable(((ColumnProjectionSegment) predicate.getRightValue()).getOwner().get());
+    }
+    
+    private boolean isTable(final OwnerSegment owner) {
+        for (TableSegment each : tables) {
+            if (owner.getIdentifier().getValue().equals(each.getAlias().orNull())) {
+                return false;
+            }
+        }
+        return true;
+    }
+}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/dml/SelectStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/dml/SelectStatement.java
index 2f77a2e9838..d02e7b274e0 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/dml/SelectStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/dml/SelectStatement.java
@@ -18,14 +18,23 @@
 package org.apache.shardingsphere.sql.parser.sql.statement.dml;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 import lombok.Getter;
 import lombok.Setter;
+import org.apache.shardingsphere.sql.parser.sql.predicate.PredicateExtractor;
+import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ProjectionSegment;
 import org.apache.shardingsphere.sql.parser.sql.segment.dml.item.ProjectionsSegment;
 import org.apache.shardingsphere.sql.parser.sql.segment.dml.order.GroupBySegment;
 import org.apache.shardingsphere.sql.parser.sql.segment.dml.order.OrderBySegment;
+import org.apache.shardingsphere.sql.parser.sql.segment.dml.order.item.ColumnOrderByItemSegment;
+import org.apache.shardingsphere.sql.parser.sql.segment.dml.order.item.OrderByItemSegment;
 import org.apache.shardingsphere.sql.parser.sql.segment.dml.pagination.limit.LimitSegment;
+import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.AndPredicate;
 import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.LockSegment;
+import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.PredicateSegment;
 import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.WhereSegment;
+import org.apache.shardingsphere.sql.parser.sql.segment.generic.OwnerAvailable;
+import org.apache.shardingsphere.sql.parser.sql.segment.generic.OwnerSegment;
 import org.apache.shardingsphere.sql.parser.sql.segment.generic.TableSegment;
 import org.apache.shardingsphere.sql.parser.sql.statement.generic.TableSegmentsAvailable;
 import org.apache.shardingsphere.sql.parser.sql.statement.generic.WhereSegmentAvailable;
@@ -56,16 +65,6 @@ public final class SelectStatement extends DMLStatement implements TableSegments
     
     private LockSegment lock;
     
-    @Override
-    public Collection<TableSegment> getAllTables() {
-        return tables;
-    }
-    
-    @Override
-    public Optional<WhereSegment> getWhere() {
-        return Optional.fromNullable(where);
-    }
-    
     /**
      * Get group by segment.
      * 
@@ -101,4 +100,72 @@ public final class SelectStatement extends DMLStatement implements TableSegments
     public Optional<LockSegment> getLock() {
         return Optional.fromNullable(lock);
     }
+    
+    @Override
+    public Optional<WhereSegment> getWhere() {
+        return Optional.fromNullable(where);
+    }
+    
+    @Override
+    public Collection<TableSegment> getAllTables() {
+        Collection<TableSegment> result = new LinkedList<>(tables);
+        if (null != where) {
+            result.addAll(getAllTablesFromWhere());
+        }
+        result.addAll(getAllTablesFromProjections());
+        if (null != groupBy) {
+            result.addAll(getAllTablesFromOrderByItems(groupBy.getGroupByItems()));
+        }
+        if (null != orderBy) {
+            result.addAll(getAllTablesFromOrderByItems(orderBy.getOrderByItems()));
+        }
+        return result;
+    }
+    
+    private Collection<TableSegment> getAllTablesFromWhere() {
+        Collection<TableSegment> result = new LinkedList<>();
+        for (AndPredicate each : where.getAndPredicates()) {
+            for (PredicateSegment predicate : each.getPredicates()) {
+                result.addAll(new PredicateExtractor(tables, predicate).extractTables());
+            }
+        }
+        return result;
+    }
+    
+    private Collection<TableSegment> getAllTablesFromProjections() {
+        Collection<TableSegment> result = new LinkedList<>();
+        for (ProjectionSegment each : projections.getProjections()) {
+            if (each instanceof OwnerAvailable) {
+                Optional<OwnerSegment> owner = ((OwnerAvailable) each).getOwner();
+                if (owner.isPresent() && isTable(owner.get())) {
+                    result.add(new TableSegment(owner.get().getStartIndex(), owner.get().getStopIndex(), owner.get().getIdentifier()));
+                }
+            }
+        }
+        return result;
+    }
+    
+    private Collection<TableSegment> getAllTablesFromOrderByItems(final Collection<OrderByItemSegment> orderByItems) {
+        Collection<TableSegment> result = new LinkedList<>();
+        for (OrderByItemSegment each : orderByItems) {
+            if (each instanceof ColumnOrderByItemSegment) {
+                Optional<OwnerSegment> owner = ((ColumnOrderByItemSegment) each).getColumn().getOwner();
+                if (owner.isPresent() && isTable(owner.get())) {
+                    Preconditions.checkState(((ColumnOrderByItemSegment) each).getColumn().getOwner().isPresent());
+                    OwnerSegment segment = ((ColumnOrderByItemSegment) each).getColumn().getOwner().get();
+                    result.add(new TableSegment(segment.getStartIndex(), segment.getStopIndex(), segment.getIdentifier()));
+                }
+            }
+        }
+        return result;
+    }
+    
+    private boolean isTable(final OwnerSegment owner) {
+        for (TableSegment each : tables) {
+            if (owner.getIdentifier().getValue().equals(each.getAlias().orNull())) {
+                return false;
+            }
+        }
+        return true;
+    }
 }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-relation/src/main/java/org/apache/shardingsphere/sql/parser/relation/segment/table/TablesContext.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-relation/src/main/java/org/apache/shardingsphere/sql/parser/relation/segment/table/TablesContext.java
index 8b40568172e..aed533d3d42 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-relation/src/main/java/org/apache/shardingsphere/sql/parser/relation/segment/table/TablesContext.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-relation/src/main/java/org/apache/shardingsphere/sql/parser/relation/segment/table/TablesContext.java
@@ -24,6 +24,9 @@ import org.apache.shardingsphere.sql.parser.relation.metadata.RelationMetas;
 import org.apache.shardingsphere.sql.parser.sql.segment.dml.column.ColumnSegment;
 import org.apache.shardingsphere.sql.parser.sql.segment.generic.TableSegment;
 import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
+import org.apache.shardingsphere.sql.parser.sql.statement.dml.DeleteStatement;
+import org.apache.shardingsphere.sql.parser.sql.statement.dml.SelectStatement;
+import org.apache.shardingsphere.sql.parser.sql.statement.dml.UpdateStatement;
 import org.apache.shardingsphere.sql.parser.sql.statement.generic.TableSegmentsAvailable;
 
 import java.util.ArrayList;
@@ -48,7 +51,7 @@ public final class TablesContext {
             tables = Collections.emptyList();
             return;
         }
-        Collection<TableSegment> tableSegments = ((TableSegmentsAvailable) sqlStatement).getAllTables();
+        Collection<TableSegment> tableSegments = getAllTables((TableSegmentsAvailable) sqlStatement);
         tables = new ArrayList<>(tableSegments.size());
         Collection<String> aliases = getAlias(tableSegments);
         for (TableSegment each : tableSegments) {
@@ -61,6 +64,19 @@ public final class TablesContext {
         }
     }
     
+    private Collection<TableSegment> getAllTables(final TableSegmentsAvailable sqlStatement) {
+        if (sqlStatement instanceof SelectStatement) {
+            return ((SelectStatement) sqlStatement).getTables();
+        }
+        if (sqlStatement instanceof UpdateStatement) {
+            return ((UpdateStatement) sqlStatement).getTables();
+        }
+        if (sqlStatement instanceof DeleteStatement) {
+            return ((DeleteStatement) sqlStatement).getTables();
+        }
+        return sqlStatement.getAllTables();
+    }
+    
     private Collection<String> getAlias(final Collection<TableSegment> tableSegments) {
         Collection<String> result = new HashSet<>(tableSegments.size(), 1);
         for (TableSegment each : tableSegments) {


Test patch:
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/dml/DeleteStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/dml/DeleteStatement.java
index 90d8a9e0f46..d62c95b1da5 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/dml/DeleteStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/dml/DeleteStatement.java
@@ -20,6 +20,9 @@ package org.apache.shardingsphere.sql.parser.sql.statement.dml;
 import com.google.common.base.Optional;
 import lombok.Getter;
 import lombok.Setter;
+import org.apache.shardingsphere.sql.parser.sql.predicate.PredicateExtractor;
+import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.AndPredicate;
+import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.PredicateSegment;
 import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.WhereSegment;
 import org.apache.shardingsphere.sql.parser.sql.segment.generic.TableSegment;
 import org.apache.shardingsphere.sql.parser.sql.statement.generic.TableSegmentsAvailable;
@@ -40,12 +43,26 @@ public final class DeleteStatement extends DMLStatement implements TableSegments
     private WhereSegment where;
     
     @Override
-    public Collection<TableSegment> getAllTables() {
-        return tables;
+    public Optional<WhereSegment> getWhere() {
+        return Optional.fromNullable(where);
     }
     
     @Override
-    public Optional<WhereSegment> getWhere() {
-        return Optional.fromNullable(where);
+    public Collection<TableSegment> getAllTables() {
+        Collection<TableSegment> result = new LinkedList<>(tables);
+        if (null != where) {
+            result.addAll(getAllTablesFromWhere());
+        }
+        return result;
+    }
+    
+    private Collection<TableSegment> getAllTablesFromWhere() {
+        Collection<TableSegment> result = new LinkedList<>();
+        for (AndPredicate each : where.getAndPredicates()) {
+            for (PredicateSegment predicate : each.getPredicates()) {
+                result.addAll(new PredicateExtractor(tables, predicate).extractTables());
+            }
+        }
+        return result;
     }
 }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/dml/UpdateStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/dml/UpdateStatement.java
index 4d0481dbffa..417ff0632bf 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/dml/UpdateStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/dml/UpdateStatement.java
@@ -20,7 +20,10 @@ package org.apache.shardingsphere.sql.parser.sql.statement.dml;
 import com.google.common.base.Optional;
 import lombok.Getter;
 import lombok.Setter;
+import org.apache.shardingsphere.sql.parser.sql.predicate.PredicateExtractor;
 import org.apache.shardingsphere.sql.parser.sql.segment.dml.assignment.SetAssignmentSegment;
+import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.AndPredicate;
+import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.PredicateSegment;
 import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.WhereSegment;
 import org.apache.shardingsphere.sql.parser.sql.segment.generic.TableSegment;
 import org.apache.shardingsphere.sql.parser.sql.statement.generic.TableSegmentsAvailable;
@@ -43,12 +46,26 @@ public final class UpdateStatement extends DMLStatement implements TableSegments
     private WhereSegment where;
     
     @Override
-    public Collection<TableSegment> getAllTables() {
-        return tables;
+    public Optional<WhereSegment> getWhere() {
+        return Optional.fromNullable(where);
     }
     
     @Override
-    public Optional<WhereSegment> getWhere() {
-        return Optional.fromNullable(where);
+    public Collection<TableSegment> getAllTables() {
+        Collection<TableSegment> result = new LinkedList<>(tables);
+        if (null != where) {
+            result.addAll(getAllTablesFromWhere());
+        }
+        return result;
+    }
+    
+    private Collection<TableSegment> getAllTablesFromWhere() {
+        Collection<TableSegment> result = new LinkedList<>();
+        for (AndPredicate each : where.getAndPredicates()) {
+            for (PredicateSegment predicate : each.getPredicates()) {
+                result.addAll(new PredicateExtractor(tables, predicate).extractTables());
+            }
+        }
+        return result;
     }
 }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-relation/src/test/java/org/apache/shardingsphere/sql/parser/relation/segment/table/TablesContextTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-relation/src/test/java/org/apache/shardingsphere/sql/parser/relation/segment/table/TablesContextTest.java
index dc7bcaae43f..9909a78d8a2 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-relation/src/test/java/org/apache/shardingsphere/sql/parser/relation/segment/table/TablesContextTest.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-relation/src/test/java/org/apache/shardingsphere/sql/parser/relation/segment/table/TablesContextTest.java
@@ -51,7 +51,7 @@ public final class TablesContextTest {
     @Test
     public void assertIsNotEmpty() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.getAllTables().add(createTableSegment("table", "tbl"));
+        selectStatement.getTables().add(createTableSegment("table", "tbl"));
         TablesContext tablesContext = new TablesContext(selectStatement);
         assertFalse(tablesContext.isEmpty());
     }
@@ -59,7 +59,7 @@ public final class TablesContextTest {
     @Test
     public void assertIsSingleTable() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.getAllTables().add(createTableSegment("table", "tbl"));
+        selectStatement.getTables().add(createTableSegment("table", "tbl"));
         TablesContext tablesContext = new TablesContext(selectStatement);
         assertTrue(tablesContext.isSingleTable());
     }
@@ -67,8 +67,8 @@ public final class TablesContextTest {
     @Test
     public void assertIsSingleTableWithCaseSensitiveNames() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.getAllTables().add(createTableSegment("table", "tbl"));
-        selectStatement.getAllTables().add(createTableSegment("Table", null));
+        selectStatement.getTables().add(createTableSegment("table", "tbl"));
+        selectStatement.getTables().add(createTableSegment("Table", null));
         TablesContext tablesContext = new TablesContext(selectStatement);
         assertTrue(tablesContext.isSingleTable());
     }
@@ -76,8 +76,8 @@ public final class TablesContextTest {
     @Test
     public void assertIsSingleTableWithNameConflictAlias() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.getAllTables().add(createTableSegment("table", "tbl"));
-        selectStatement.getAllTables().add(createTableSegment("tbl", null));
+        selectStatement.getTables().add(createTableSegment("table", "tbl"));
+        selectStatement.getTables().add(createTableSegment("tbl", null));
         TablesContext tablesContext = new TablesContext(selectStatement);
         assertTrue(tablesContext.isSingleTable());
     }
@@ -85,8 +85,8 @@ public final class TablesContextTest {
     @Test
     public void assertIsNotSingleTable() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.getAllTables().add(createTableSegment("table_1", "tbl_1"));
-        selectStatement.getAllTables().add(createTableSegment("table_2", "tbl_2"));
+        selectStatement.getTables().add(createTableSegment("table_1", "tbl_1"));
+        selectStatement.getTables().add(createTableSegment("table_2", "tbl_2"));
         TablesContext tablesContext = new TablesContext(selectStatement);
         assertFalse(tablesContext.isSingleTable());
     }
@@ -94,7 +94,7 @@ public final class TablesContextTest {
     @Test
     public void assertGetSingleTableName() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.getAllTables().add(createTableSegment("table", "tbl"));
+        selectStatement.getTables().add(createTableSegment("table", "tbl"));
         TablesContext tablesContext = new TablesContext(selectStatement);
         assertThat(tablesContext.getSingleTableName(), is("table"));
     }
@@ -102,8 +102,8 @@ public final class TablesContextTest {
     @Test
     public void assertGetTableNames() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.getAllTables().add(createTableSegment("table_1", "tbl_1"));
-        selectStatement.getAllTables().add(createTableSegment("table_2", "tbl_2"));
+        selectStatement.getTables().add(createTableSegment("table_1", "tbl_1"));
+        selectStatement.getTables().add(createTableSegment("table_2", "tbl_2"));
         TablesContext tablesContext = new TablesContext(selectStatement);
         assertThat(tablesContext.getTableNames(), CoreMatchers.<Collection<String>>is(Sets.newHashSet("table_1", "table_2")));
     }
@@ -111,8 +111,8 @@ public final class TablesContextTest {
     @Test
     public void assertFindTableWithName() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.getAllTables().add(createTableSegment("table_1", "tbl_1"));
-        selectStatement.getAllTables().add(createTableSegment("table_2", "tbl_2"));
+        selectStatement.getTables().add(createTableSegment("table_1", "tbl_1"));
+        selectStatement.getTables().add(createTableSegment("table_2", "tbl_2"));
         TablesContext tablesContext = new TablesContext(selectStatement);
         Optional<Table> table = tablesContext.find("table_1");
         assertTrue(table.isPresent());
@@ -123,8 +123,8 @@ public final class TablesContextTest {
     @Test
     public void assertFindTableWithAlias() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.getAllTables().add(createTableSegment("table_1", "tbl_1"));
-        selectStatement.getAllTables().add(createTableSegment("table_2", "tbl_2"));
+        selectStatement.getTables().add(createTableSegment("table_1", "tbl_1"));
+        selectStatement.getTables().add(createTableSegment("table_2", "tbl_2"));
         TablesContext tablesContext = new TablesContext(selectStatement);
         Optional<Table> table = tablesContext.find("tbl_1");
         assertTrue(table.isPresent());
@@ -135,8 +135,8 @@ public final class TablesContextTest {
     @Test
     public void assertNotFoundTable() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.getAllTables().add(createTableSegment("table_1", "tbl_1"));
-        selectStatement.getAllTables().add(createTableSegment("table_2", "tbl_2"));
+        selectStatement.getTables().add(createTableSegment("table_1", "tbl_1"));
+        selectStatement.getTables().add(createTableSegment("table_2", "tbl_2"));
         TablesContext tablesContext = new TablesContext(selectStatement);
         Optional<Table> table = tablesContext.find("table_3");
         assertFalse(table.isPresent());
@@ -145,15 +145,15 @@ public final class TablesContextTest {
     @Test
     public void assertFindTableNameWhenSingleTable() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.getAllTables().add(createTableSegment("table_1", "tbl_1"));
+        selectStatement.getTables().add(createTableSegment("table_1", "tbl_1"));
         assertTrue(new TablesContext(selectStatement).findTableName(null, null).isPresent());
     }
     
     @Test
     public void assertFindTableNameWhenColumnSegmentOwnerPresent() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.getAllTables().add(createTableSegment("table_1", "tbl_1"));
-        selectStatement.getAllTables().add(createTableSegment("table_2", "tbl_2"));
+        selectStatement.getTables().add(createTableSegment("table_1", "tbl_1"));
+        selectStatement.getTables().add(createTableSegment("table_2", "tbl_2"));
         TablesContext tablesContext = new TablesContext(selectStatement);
         ColumnSegment columnSegment = mock(ColumnSegment.class);
         when(columnSegment.getOwner()).thenReturn(Optional.of(new OwnerSegment(0, 10, new IdentifierValue("table_1"))));
@@ -163,8 +163,8 @@ public final class TablesContextTest {
     @Test
     public void assertFindTableNameWhenColumnSegmentOwnerAbsent() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.getAllTables().add(createTableSegment("table_1", "tbl_1"));
-        selectStatement.getAllTables().add(createTableSegment("table_2", "tbl_2"));
+        selectStatement.getTables().add(createTableSegment("table_1", "tbl_1"));
+        selectStatement.getTables().add(createTableSegment("table_2", "tbl_2"));
         TablesContext tablesContext = new TablesContext(selectStatement);
         ColumnSegment columnSegment = new ColumnSegment(0, 0, new IdentifierValue("col"));
         RelationMetas relationMetas = mock(RelationMetas.class);
@@ -174,8 +174,8 @@ public final class TablesContextTest {
     @Test
     public void assertFindTableNameWhenColumnSegmentOwnerAbsentAndRelationMetasContainsColumn() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.getAllTables().add(createTableSegment("table_1", "tbl_1"));
-        selectStatement.getAllTables().add(createTableSegment("table_2", "tbl_2"));
+        selectStatement.getTables().add(createTableSegment("table_1", "tbl_1"));
+        selectStatement.getTables().add(createTableSegment("table_2", "tbl_2"));
         ColumnSegment columnSegment = new ColumnSegment(0, 0, new IdentifierValue("col"));
         RelationMetas relationMetas = mock(RelationMetas.class);
         when(relationMetas.containsColumn(anyString(), anyString())).thenReturn(true);
@@ -185,7 +185,7 @@ public final class TablesContextTest {
     @Test
     public void assertGetSchema() {
         SelectStatement selectStatement = new SelectStatement();
-        selectStatement.getAllTables().add(createTableSegment("table_1", "tbl_1"));
+        selectStatement.getTables().add(createTableSegment("table_1", "tbl_1"));
         assertFalse(new TablesContext(selectStatement).getSchema().isPresent());
     }