Search in sources :

Example 6 with ColumnRoutePair

use of io.mycat.sqlengine.mpp.ColumnRoutePair in project Mycat-Server by MyCATApache.

the class RouterUtil method ruleByJoinValueCalculate.

/**
 * @return dataNodeIndex -> [partitionKeysValueTuple+]
 */
public static Set<String> ruleByJoinValueCalculate(RouteResultset rrs, TableConfig tc, Set<ColumnRoutePair> colRoutePairSet) throws SQLNonTransientException {
    String joinValue = "";
    if (colRoutePairSet.size() > 1) {
        LOGGER.warn("joinKey can't have multi Value");
    } else {
        Iterator<ColumnRoutePair> it = colRoutePairSet.iterator();
        ColumnRoutePair joinCol = it.next();
        joinValue = joinCol.colValue;
    }
    Set<String> retNodeSet = new LinkedHashSet<String>();
    Set<String> nodeSet;
    if (tc.isSecondLevel() && tc.getParentTC().getPartitionColumn().equals(tc.getParentKey())) {
        // using
        // parent
        // rule to
        // find
        // datanode
        nodeSet = ruleCalculate(tc.getParentTC(), colRoutePairSet, rrs.getDataNodeSlotMap());
        if (nodeSet.isEmpty()) {
            throw new SQLNonTransientException("parent key can't find  valid datanode ,expect 1 but found: " + nodeSet.size());
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("found partion node (using parent partion rule directly) for child table to insert  " + nodeSet + " sql :" + rrs.getStatement());
        }
        retNodeSet.addAll(nodeSet);
        // }
        return retNodeSet;
    } else {
        retNodeSet.addAll(tc.getParentTC().getDataNodes());
    }
    return retNodeSet;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SQLNonTransientException(java.sql.SQLNonTransientException) ColumnRoutePair(io.mycat.sqlengine.mpp.ColumnRoutePair)

Example 7 with ColumnRoutePair

use of io.mycat.sqlengine.mpp.ColumnRoutePair in project Mycat_plus by coderczp.

the class RouterUtil method ruleByJoinValueCalculate.

/**
 * @return dataNodeIndex -&gt; [partitionKeysValueTuple+]
 */
public static Set<String> ruleByJoinValueCalculate(RouteResultset rrs, TableConfig tc, Set<ColumnRoutePair> colRoutePairSet) throws SQLNonTransientException {
    String joinValue = "";
    if (colRoutePairSet.size() > 1) {
        LOGGER.warn("joinKey can't have multi Value");
    } else {
        Iterator<ColumnRoutePair> it = colRoutePairSet.iterator();
        ColumnRoutePair joinCol = it.next();
        joinValue = joinCol.colValue;
    }
    Set<String> retNodeSet = new LinkedHashSet<String>();
    Set<String> nodeSet;
    if (tc.isSecondLevel() && tc.getParentTC().getPartitionColumn().equals(tc.getParentKey())) {
        // using
        // parent
        // rule to
        // find
        // datanode
        nodeSet = ruleCalculate(tc.getParentTC(), colRoutePairSet, rrs.getDataNodeSlotMap());
        if (nodeSet.isEmpty()) {
            throw new SQLNonTransientException("parent key can't find  valid datanode ,expect 1 but found: " + nodeSet.size());
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("found partion node (using parent partion rule directly) for child table to insert  " + nodeSet + " sql :" + rrs.getStatement());
        }
        retNodeSet.addAll(nodeSet);
        // }
        return retNodeSet;
    } else {
        retNodeSet.addAll(tc.getParentTC().getDataNodes());
    }
    return retNodeSet;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SQLNonTransientException(java.sql.SQLNonTransientException) ColumnRoutePair(io.mycat.sqlengine.mpp.ColumnRoutePair)

Example 8 with ColumnRoutePair

use of io.mycat.sqlengine.mpp.ColumnRoutePair in project Mycat_plus by coderczp.

the class RouterUtil method ruleCalculate.

/**
 * @return dataNodeIndex -&gt; [partitionKeysValueTuple+]
 */
public static Set<String> ruleCalculate(TableConfig tc, Set<ColumnRoutePair> colRoutePairSet, Map<String, Integer> dataNodeSlotMap) {
    Set<String> routeNodeSet = new LinkedHashSet<String>();
    String col = tc.getRule().getColumn();
    RuleConfig rule = tc.getRule();
    AbstractPartitionAlgorithm algorithm = rule.getRuleAlgorithm();
    for (ColumnRoutePair colPair : colRoutePairSet) {
        if (colPair.colValue != null) {
            Integer nodeIndx = algorithm.calculate(colPair.colValue);
            if (nodeIndx == null) {
                throw new IllegalArgumentException("can't find datanode for sharding column:" + col + " val:" + colPair.colValue);
            } else {
                String dataNode = tc.getDataNodes().get(nodeIndx);
                routeNodeSet.add(dataNode);
                if (algorithm instanceof SlotFunction) {
                    dataNodeSlotMap.put(dataNode, ((SlotFunction) algorithm).slotValue());
                }
                colPair.setNodeId(nodeIndx);
            }
        } else if (colPair.rangeValue != null) {
            Integer[] nodeRange = algorithm.calculateRange(String.valueOf(colPair.rangeValue.beginValue), String.valueOf(colPair.rangeValue.endValue));
            if (nodeRange != null) {
                /**
                 * 不能确认 colPair的 nodeid是否会有其它影响
                 */
                if (nodeRange.length == 0) {
                    routeNodeSet.addAll(tc.getDataNodes());
                } else {
                    ArrayList<String> dataNodes = tc.getDataNodes();
                    String dataNode = null;
                    for (Integer nodeId : nodeRange) {
                        dataNode = dataNodes.get(nodeId);
                        if (algorithm instanceof SlotFunction) {
                            dataNodeSlotMap.put(dataNode, ((SlotFunction) algorithm).slotValue());
                        }
                        routeNodeSet.add(dataNode);
                    }
                }
            }
        }
    }
    return routeNodeSet;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) ColumnRoutePair(io.mycat.sqlengine.mpp.ColumnRoutePair) ArrayList(java.util.ArrayList) RuleConfig(io.mycat.config.model.rule.RuleConfig) SlotFunction(io.mycat.route.function.SlotFunction)

Example 9 with ColumnRoutePair

use of io.mycat.sqlengine.mpp.ColumnRoutePair in project Mycat_plus by coderczp.

the class RouterUtil method routeToDistTableNode.

private static RouteResultset routeToDistTableNode(String tableName, SchemaConfig schema, RouteResultset rrs, String orgSql, Map<String, Map<String, Set<ColumnRoutePair>>> tablesAndConditions, LayerCachePool cachePool, boolean isSelect) throws SQLNonTransientException {
    TableConfig tableConfig = schema.getTables().get(tableName);
    if (tableConfig == null) {
        String msg = "can't find table define in schema " + tableName + " schema:" + schema.getName();
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    if (tableConfig.isGlobalTable()) {
        String msg = "can't suport district table  " + tableName + " schema:" + schema.getName() + " for global table ";
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    String partionCol = tableConfig.getPartitionColumn();
    // String primaryKey = tableConfig.getPrimaryKey();
    boolean isLoadData = false;
    Set<String> tablesRouteSet = new HashSet<String>();
    List<String> dataNodes = tableConfig.getDataNodes();
    if (dataNodes.size() > 1) {
        String msg = "can't suport district table  " + tableName + " schema:" + schema.getName() + " for mutiple dataNode " + dataNodes;
        LOGGER.warn(msg);
        throw new SQLNonTransientException(msg);
    }
    String dataNode = dataNodes.get(0);
    // 主键查找缓存暂时不实现
    if (tablesAndConditions.isEmpty()) {
        List<String> subTables = tableConfig.getDistTables();
        tablesRouteSet.addAll(subTables);
    }
    for (Map.Entry<String, Map<String, Set<ColumnRoutePair>>> entry : tablesAndConditions.entrySet()) {
        boolean isFoundPartitionValue = partionCol != null && entry.getValue().get(partionCol) != null;
        Map<String, Set<ColumnRoutePair>> columnsMap = entry.getValue();
        Set<ColumnRoutePair> partitionValue = columnsMap.get(partionCol);
        if (partitionValue == null || partitionValue.size() == 0) {
            tablesRouteSet.addAll(tableConfig.getDistTables());
        } else {
            for (ColumnRoutePair pair : partitionValue) {
                AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
                if (pair.colValue != null) {
                    Integer tableIndex = algorithm.calculate(pair.colValue);
                    if (tableIndex == null) {
                        String msg = "can't find any valid datanode :" + tableConfig.getName() + " -> " + tableConfig.getPartitionColumn() + " -> " + pair.colValue;
                        LOGGER.warn(msg);
                        throw new SQLNonTransientException(msg);
                    }
                    String subTable = tableConfig.getDistTables().get(tableIndex);
                    if (subTable != null) {
                        tablesRouteSet.add(subTable);
                        if (algorithm instanceof SlotFunction) {
                            rrs.getDataNodeSlotMap().put(subTable, ((SlotFunction) algorithm).slotValue());
                        }
                    }
                }
                if (pair.rangeValue != null) {
                    Integer[] tableIndexs = algorithm.calculateRange(pair.rangeValue.beginValue.toString(), pair.rangeValue.endValue.toString());
                    for (Integer idx : tableIndexs) {
                        String subTable = tableConfig.getDistTables().get(idx);
                        if (subTable != null) {
                            tablesRouteSet.add(subTable);
                            if (algorithm instanceof SlotFunction) {
                                rrs.getDataNodeSlotMap().put(subTable, ((SlotFunction) algorithm).slotValue());
                            }
                        }
                    }
                }
            }
        }
    }
    Object[] subTables = tablesRouteSet.toArray();
    RouteResultsetNode[] nodes = new RouteResultsetNode[subTables.length];
    Map<String, Integer> dataNodeSlotMap = rrs.getDataNodeSlotMap();
    for (int i = 0; i < nodes.length; i++) {
        String table = String.valueOf(subTables[i]);
        String changeSql = orgSql;
        // rrs.getStatement()
        nodes[i] = new RouteResultsetNode(dataNode, rrs.getSqlType(), changeSql);
        nodes[i].setSubTableName(table);
        nodes[i].setSource(rrs);
        if (rrs.getDataNodeSlotMap().containsKey(dataNode)) {
            nodes[i].setSlot(rrs.getDataNodeSlotMap().get(dataNode));
        }
        if (rrs.getCanRunInReadDB() != null) {
            nodes[i].setCanRunInReadDB(rrs.getCanRunInReadDB());
        }
        if (dataNodeSlotMap.containsKey(table)) {
            nodes[i].setSlot(dataNodeSlotMap.get(table));
        }
        if (rrs.getRunOnSlave() != null) {
            nodes[0].setRunOnSlave(rrs.getRunOnSlave());
        }
    }
    rrs.setNodes(nodes);
    rrs.setSubTables(tablesRouteSet);
    rrs.setFinishedRoute(true);
    return rrs;
}
Also used : AbstractPartitionAlgorithm(io.mycat.route.function.AbstractPartitionAlgorithm) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) ColumnRoutePair(io.mycat.sqlengine.mpp.ColumnRoutePair) SlotFunction(io.mycat.route.function.SlotFunction) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultsetNode(io.mycat.route.RouteResultsetNode) TableConfig(io.mycat.config.model.TableConfig) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 10 with ColumnRoutePair

use of io.mycat.sqlengine.mpp.ColumnRoutePair in project Mycat_plus by coderczp.

the class RouterUtil method routeByERParentKey.

/**
 * 根据 ER分片规则获取路由集合
 *
 * @param stmt            执行的语句
 * @param rrs      		     数据路由集合
 * @param tc	      	     表实体
 * @param joinKeyVal      连接属性
 * @return RouteResultset(数据路由集合)	 *
 * @throws SQLNonTransientException,IllegalShardingColumnValueException
 * @author mycat
 */
public static RouteResultset routeByERParentKey(ServerConnection sc, SchemaConfig schema, int sqlType, String stmt, RouteResultset rrs, TableConfig tc, String joinKeyVal) throws SQLNonTransientException {
    // table's partition key
    if (tc.isSecondLevel() && // 判断是否为二级子表(父表不再有父表)
    tc.getParentTC().getPartitionColumn().equals(tc.getParentKey())) {
        // using
        // parent
        // rule to
        // find
        // datanode
        Set<ColumnRoutePair> parentColVal = new HashSet<ColumnRoutePair>(1);
        ColumnRoutePair pair = new ColumnRoutePair(joinKeyVal);
        parentColVal.add(pair);
        Set<String> dataNodeSet = ruleCalculate(tc.getParentTC(), parentColVal, rrs.getDataNodeSlotMap());
        if (dataNodeSet.isEmpty() || dataNodeSet.size() > 1) {
            throw new SQLNonTransientException("parent key can't find  valid datanode ,expect 1 but found: " + dataNodeSet.size());
        }
        String dn = dataNodeSet.iterator().next();
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("found partion node (using parent partion rule directly) for child table to insert  " + dn + " sql :" + stmt);
        }
        return RouterUtil.routeToSingleNode(rrs, dn, stmt);
    }
    return null;
}
Also used : SQLNonTransientException(java.sql.SQLNonTransientException) ColumnRoutePair(io.mycat.sqlengine.mpp.ColumnRoutePair) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

ColumnRoutePair (io.mycat.sqlengine.mpp.ColumnRoutePair)16 LinkedHashSet (java.util.LinkedHashSet)14 SQLNonTransientException (java.sql.SQLNonTransientException)12 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 Set (java.util.Set)8 TableConfig (io.mycat.config.model.TableConfig)6 AbstractPartitionAlgorithm (io.mycat.route.function.AbstractPartitionAlgorithm)6 SlotFunction (io.mycat.route.function.SlotFunction)6 RouteResultsetNode (io.mycat.route.RouteResultsetNode)4 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)2 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)2 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)2 SQLSelectGroupByClause (com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause)2 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)2 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)2 SQLTableSource (com.alibaba.druid.sql.ast.statement.SQLTableSource)2 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)2 Limit (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock.Limit)2