/*
 * 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.core.route.type.broadcast;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.ddl.DDLStatement;
import org.apache.shardingsphere.core.route.type.RoutingEngine;
import org.apache.shardingsphere.core.route.type.RoutingResult;
import org.apache.shardingsphere.core.route.type.RoutingUnit;
import org.apache.shardingsphere.core.route.type.TableUnit;
import org.apache.shardingsphere.core.rule.DataNode;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.core.rule.TableRule;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;

/**
 * Broadcast routing engine for tables.
 * 
 * @author zhangliang
 * @author maxiaoguang
 * @author panjuan
 */
@RequiredArgsConstructor
public final class TableBroadcastRoutingEngine implements RoutingEngine {
    
    private final ShardingRule shardingRule;
    
    private final SQLStatement sqlStatement;
    
    @Override
    public RoutingResult route() {
        RoutingResult result = new RoutingResult();
        for (String each : getLogicTableNames()) {
            result.getRoutingUnits().addAll(getAllRoutingUnits(each));
        }
        return result;
    }
    
    private Collection<String> getLogicTableNames() {
        if (isOperateIndexWithoutTable()) {
            return Collections.singletonList(shardingRule.getLogicTableName(((DDLStatement) sqlStatement).getIndexName()));
        }
        return sqlStatement.getTables().getTableNames();
    }
    
    private boolean isOperateIndexWithoutTable() {
        return sqlStatement instanceof DDLStatement && sqlStatement.getTables().isEmpty();
    }
    
    private Collection<RoutingUnit> getAllRoutingUnits(final String logicTableName) {
        Collection<RoutingUnit> result = new LinkedList<>();
        TableRule tableRule = shardingRule.getTableRule(logicTableName);
        for (DataNode each : tableRule.getActualDataNodes()) {
            RoutingUnit routingUnit = new RoutingUnit(each.getDataSourceName());
            routingUnit.getTableUnits().add(new TableUnit(logicTableName, each.getTableName()));
            result.add(routingUnit);
        }
        return result;
    }
}

@@ -939,39 +939,37 @@
 ore.
-parse.sql.statement.SQLStatemen
+optimize.result.OptimizeResul
 t;%0Ai
@@ -1029,14 +1029,10 @@
 ent.
-ddl.DD
+SQ
 LSta
@@ -1859,24 +1859,78 @@
 ement;%0A    %0A
+    private final OptimizeResult optimizeResult;%0A    %0A
     @Overrid
@@ -2254,34 +2254,64 @@
 if (
-isOperateIndexWithoutTable
+optimizeResult.getLogicTableNameForDropIndex().isPresent
 ()) 
@@ -2357,28 +2357,30 @@
 tonList(
-shardingRule
+optimizeResult
 .getLogi
@@ -2393,53 +2393,27 @@
 Name
-(((DDLStatement) sqlStatement).getIndexName()
+ForDropIndex().get(
 ));%0A
@@ -2494,161 +2494,8 @@
    %0A
-    private boolean isOperateIndexWithoutTable() %7B%0A        return sqlStatement instanceof DDLStatement && sqlStatement.getTables().isEmpty();%0A    %7D%0A    %0A
     

add optimizeResult