Search in sources :

Example 1 with ShardingIndexTable

use of io.mycat.calcite.table.ShardingIndexTable in project Mycat2 by MyCATApache.

the class ColocatedPlanner method checkColocatedPushDown.

public static Optional<PartitionGroup> checkColocatedPushDown(MycatDataContext context, Plan plan, DrdsSqlWithParams drdsSqlWithParams) {
    CodeExecuterContext codeExecuterContext = plan.getCodeExecuterContext();
    AsyncMycatDataContextImpl.SqlMycatDataContextImpl sqlMycatDataContext = new AsyncMycatDataContextImpl.SqlMycatDataContextImpl(context, codeExecuterContext, drdsSqlWithParams);
    Map<String, MycatRelDatasourceSourceInfo> relContext = codeExecuterContext.getRelContext();
    List<List<PartitionGroup>> lists = new ArrayList<>();
    for (MycatRelDatasourceSourceInfo mycatRelDatasourceSourceInfo : relContext.values()) {
        if (mycatRelDatasourceSourceInfo.getRelNode() instanceof MycatView) {
            MycatView mycatView = (MycatView) mycatRelDatasourceSourceInfo.getRelNode();
            if (mycatView.getDistribution().type() == Distribution.Type.SHARDING) {
                for (ShardingTable shardingTable : mycatView.getDistribution().getShardingTables()) {
                    if (shardingTable instanceof ShardingIndexTable) {
                        return Optional.empty();
                    }
                }
            }
            lists.add(sqlMycatDataContext.getPartition(mycatView.getDigest()).orElse(Collections.emptyList()));
        } else {
            return Optional.empty();
        }
    }
    // Colocated Push Down
    PartitionGroup result = null;
    for (List<PartitionGroup> list : lists) {
        if (list.size() == 1) {
            PartitionGroup each = list.get(0);
            if (result == null) {
                result = new PartitionGroup(each.getTargetName(), new HashMap<>(each.getMap()));
            } else if (result.getTargetName().equals(each.getTargetName())) {
                Map<String, Partition> eachMap = each.getMap();
                Map<String, Partition> resMap = result.getMap();
                for (Map.Entry<String, Partition> entry : eachMap.entrySet()) {
                    if (resMap.containsKey(entry.getKey())) {
                        // 已经存在的分区不一致,所以不匹配
                        Partition existed = resMap.get(entry.getKey());
                        if (!existed.getUniqueName().equals(entry.getValue().getUniqueName())) {
                            return Optional.empty();
                        }
                    } else {
                        resMap.put(entry.getKey(), entry.getValue());
                    }
                }
            } else {
                return Optional.empty();
            }
        } else {
            return Optional.empty();
        }
    }
    return Optional.ofNullable(result);
}
Also used : CodeExecuterContext(io.mycat.calcite.CodeExecuterContext) MycatView(io.mycat.calcite.logical.MycatView) MycatRelDatasourceSourceInfo(io.mycat.calcite.MycatRelDatasourceSourceInfo) ShardingIndexTable(io.mycat.calcite.table.ShardingIndexTable) ShardingTable(io.mycat.calcite.table.ShardingTable) NameMap(io.mycat.util.NameMap)

Aggregations

CodeExecuterContext (io.mycat.calcite.CodeExecuterContext)1 MycatRelDatasourceSourceInfo (io.mycat.calcite.MycatRelDatasourceSourceInfo)1 MycatView (io.mycat.calcite.logical.MycatView)1 ShardingIndexTable (io.mycat.calcite.table.ShardingIndexTable)1 ShardingTable (io.mycat.calcite.table.ShardingTable)1 NameMap (io.mycat.util.NameMap)1