/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed 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.
 * </p>
 */

package io.shardingsphere.core.parsing.antlr.filler.engine;

import io.shardingsphere.core.metadata.table.ShardingTableMetaData;
import io.shardingsphere.core.parsing.antlr.filler.SQLSegmentFiller;
import io.shardingsphere.core.parsing.antlr.sql.segment.SQLSegment;
import io.shardingsphere.core.parsing.antlr.sql.segment.table.TableSegment;
import io.shardingsphere.core.parsing.parser.context.table.Table;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import io.shardingsphere.core.parsing.parser.sql.dql.select.SelectStatement;
import io.shardingsphere.core.parsing.parser.token.TableToken;
import io.shardingsphere.core.rule.ShardingRule;

/**
 * Table filler.
 *
 * @author duhongjun
 */
public class TableFiller implements SQLSegmentFiller {
    
    @Override
    public void fill(final SQLSegment sqlSegment, final SQLStatement sqlStatement, final String sql, final ShardingRule shardingRule, final ShardingTableMetaData shardingTableMetaData) {
        TableSegment tableSegment = (TableSegment) sqlSegment;
        String tableName = tableSegment.getName();
        boolean needAdd = false;
        if (!(sqlStatement instanceof SelectStatement)) {
            needAdd = true;
        } else if (shardingRule.tryFindTableRuleByLogicTable(tableName).isPresent() || shardingRule.isBroadcastTable(tableName) || shardingRule.findBindingTableRule(tableName).isPresent()
                || shardingRule.getShardingDataSourceNames().getDataSourceNames().contains(shardingRule.getShardingDataSourceNames().getDefaultDataSourceName())) {
            needAdd = true;
        }
        if (needAdd) {
            sqlStatement.getTables().add(new Table(tableSegment.getName(), tableSegment.getAlias()));
            sqlStatement.getSQLTokens().add(tableSegment.getToken());
            if (tableSegment.getAlias().isPresent()) {
                if (sqlStatement.getTables().getTableNames().contains(tableSegment.getAlias().get())) {
                    sqlStatement.addSQLToken(new TableToken(tableSegment.getAliasStartPosition(), 0, tableSegment.getAlias().get()));
                }
            }
        }
    }
}

@@ -1659,587 +1659,116 @@
     
-String tableName = tableSegment.getName();%0A        boolean needAdd = false;%0A        if (!(sqlStatement instanceof SelectStatement)) %7B%0A            needAdd = true;%0A        %7D else if (shardingRule.tryFindTableRuleByLogicTable(tableName).isPresent() %7C%7C shardingRule.isBroadcastTable(tableName) %7C%7C shardingRule.findBindingTableRule(tableName).isPresent()%0A                %7C%7C shardingRule.getShardingDataSourceNames().getDataSourceNames().contains(shardingRule.getShardingDataSourceNames().getDefaultDataSourceName())) %7B%0A            needAdd = true;%0A        %7D%0A        if (needAdd) %7B%0A    
+if (!isNeedToFillTable(sqlStatement, shardingRule, tableSegment.getName())) %7B%0A            return;%0A        %7D%0A
     
@@ -1857,28 +1857,24 @@
 tAlias()));%0A
-    
         sqlS
@@ -1931,28 +1931,24 @@
 ));%0A        
-    
 if (tableSeg
@@ -1978,32 +1978,12 @@
 nt()
-) %7B%0A                if (
+ && 
 sqlS
@@ -2058,32 +2058,24 @@
 ).get())) %7B%0A
-        
             
@@ -2200,41 +2200,645 @@
     
-        %7D%0A            %7D%0A        %7D
+%7D%0A    %7D%0A    %0A    private boolean isNeedToFillTable(final SQLStatement sqlStatement, final ShardingRule shardingRule, final String tableName) %7B%0A        if (!(sqlStatement instanceof SelectStatement)) %7B%0A            return true;%0A        %7D%0A        if (shardingRule.tryFindTableRuleByLogicTable(tableName).isPresent() %7C%7C shardingRule.isBroadcastTable(tableName) %7C%7C shardingRule.findBindingTableRule(tableName).isPresent()%0A                %7C%7C shardingRule.getShardingDataSourceNames().getDataSourceNames().contains(shardingRule.getShardingDataSourceNames().getDefaultDataSourceName())) %7B%0A            return true;%0A        %7D%0A        return false;
 %0A   

refactor TableFiller