Search in sources :

Example 21 with Pair

use of com.alibaba.cobar.parser.util.Pair in project cobar by alibaba.

the class ServerRouter method dispatchWhereBasedStmt.

private static void dispatchWhereBasedStmt(RouteResultsetNode[] rn, SQLStatement stmtAST, List<String> ruleColumns, Map<Integer, List<Object[]>> dataNodeMap, TableConfig matchedTable, String originalSQL, PartitionKeyVisitor visitor) {
    // [perf tag] 11.617 us: sharding multivalue
    if (ruleColumns.size() > 1) {
        String sql;
        if (visitor.isSchemaTrimmed()) {
            sql = genSQL(stmtAST, originalSQL);
        } else {
            sql = originalSQL;
        }
        int i = -1;
        for (Integer dataNodeId : dataNodeMap.keySet()) {
            String dataNode = matchedTable.getDataNodes()[dataNodeId];
            rn[++i] = new RouteResultsetNode(dataNode, sql);
        }
        return;
    }
    final String table = matchedTable.getName();
    Map<String, Map<Object, Set<Pair<Expression, ASTNode>>>> columnIndex = visitor.getColumnIndex(table);
    Map<Object, Set<Pair<Expression, ASTNode>>> valueMap = columnIndex.get(ruleColumns.get(0));
    replacePartitionKeyOperand(columnIndex, ruleColumns);
    Map<InExpression, Set<Expression>> unreplacedInExpr = new HashMap<InExpression, Set<Expression>>(1, 1);
    Set<ReplacableExpression> unreplacedSingleExprs = new HashSet<ReplacableExpression>();
    // [perf tag] 12.2755 us: sharding multivalue
    int nodeId = -1;
    for (Entry<Integer, List<Object[]>> en : dataNodeMap.entrySet()) {
        List<Object[]> tuples = en.getValue();
        unreplacedSingleExprs.clear();
        unreplacedInExpr.clear();
        for (Object[] tuple : tuples) {
            Object value = tuple[0];
            Set<Pair<Expression, ASTNode>> indexedExpressionPair = getExpressionSet(valueMap, value);
            for (Pair<Expression, ASTNode> pair : indexedExpressionPair) {
                Expression expr = pair.getKey();
                ASTNode parent = pair.getValue();
                if (PartitionKeyVisitor.isPartitionKeyOperandSingle(expr, parent)) {
                    unreplacedSingleExprs.add((ReplacableExpression) expr);
                } else if (PartitionKeyVisitor.isPartitionKeyOperandIn(expr, parent)) {
                    Set<Expression> newInSet = unreplacedInExpr.get(parent);
                    if (newInSet == null) {
                        newInSet = new HashSet<Expression>(indexedExpressionPair.size(), 1);
                        unreplacedInExpr.put((InExpression) parent, newInSet);
                    }
                    newInSet.add(expr);
                }
            }
        }
        for (ReplacableExpression expr : unreplacedSingleExprs) {
            expr.clearReplaceExpr();
        }
        for (Entry<InExpression, Set<Expression>> entemp : unreplacedInExpr.entrySet()) {
            InExpression in = entemp.getKey();
            Set<Expression> set = entemp.getValue();
            if (set == null || set.isEmpty()) {
                in.setReplaceExpr(ReplacableExpression.BOOL_FALSE);
            } else {
                in.clearReplaceExpr();
                InExpressionList inlist = in.getInExpressionList();
                if (inlist != null)
                    inlist.setReplaceExpr(new ArrayList<Expression>(set));
            }
        }
        // [perf tag] 16.506 us: sharding multivalue
        String sql = genSQL(stmtAST, originalSQL);
        // [perf tag] 21.3425 us: sharding multivalue
        String dataNodeName = matchedTable.getDataNodes()[en.getKey()];
        rn[++nodeId] = new RouteResultsetNode(dataNodeName, sql);
        for (ReplacableExpression expr : unreplacedSingleExprs) {
            expr.setReplaceExpr(ReplacableExpression.BOOL_FALSE);
        }
        for (InExpression in : unreplacedInExpr.keySet()) {
            in.setReplaceExpr(ReplacableExpression.BOOL_FALSE);
            InExpressionList list = in.getInExpressionList();
            if (list != null)
                list.clearReplaceExpr();
        }
    // [perf tag] 22.0965 us: sharding multivalue
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) ArrayList(java.util.ArrayList) ReplacableExpression(com.alibaba.cobar.parser.ast.expression.ReplacableExpression) ASTNode(com.alibaba.cobar.parser.ast.ASTNode) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) InExpressionList(com.alibaba.cobar.parser.ast.expression.misc.InExpressionList) Pair(com.alibaba.cobar.parser.util.Pair) HashSet(java.util.HashSet) CobarHint(com.alibaba.cobar.route.hint.CobarHint) InExpressionList(com.alibaba.cobar.parser.ast.expression.misc.InExpressionList) Expression(com.alibaba.cobar.parser.ast.expression.Expression) ReplacableExpression(com.alibaba.cobar.parser.ast.expression.ReplacableExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) HashMap(java.util.HashMap) Map(java.util.Map)

Example 22 with Pair

use of com.alibaba.cobar.parser.util.Pair in project cobar by alibaba.

the class ServerRouter method dispatchInsertReplace.

@SuppressWarnings("unchecked")
private static void dispatchInsertReplace(RouteResultsetNode[] rn, DMLInsertReplaceStatement stmt, List<String> ruleColumns, Map<Integer, List<Object[]>> dataNodeMap, TableConfig matchedTable, String originalSQL, PartitionKeyVisitor visitor) {
    if (stmt.getSelect() != null) {
        dispatchWhereBasedStmt(rn, stmt, ruleColumns, dataNodeMap, matchedTable, originalSQL, visitor);
        return;
    }
    Map<String, Map<Object, Set<Pair<Expression, ASTNode>>>> colsIndex = visitor.getColumnIndex(stmt.getTable().getIdTextUpUnescape());
    if (colsIndex == null || colsIndex.isEmpty()) {
        throw new IllegalArgumentException("columns index is empty: " + originalSQL);
    }
    ArrayList<Map<Object, Set<Pair<Expression, ASTNode>>>> colsIndexList = new ArrayList<Map<Object, Set<Pair<Expression, ASTNode>>>>(ruleColumns.size());
    for (int i = 0, len = ruleColumns.size(); i < len; ++i) {
        colsIndexList.add(colsIndex.get(ruleColumns.get(i)));
    }
    int dataNodeId = -1;
    for (Entry<Integer, List<Object[]>> en : dataNodeMap.entrySet()) {
        List<Object[]> tuples = en.getValue();
        HashSet<RowExpression> replaceRowList = new HashSet<RowExpression>(tuples.size());
        for (Object[] tuple : tuples) {
            Set<Pair<Expression, ASTNode>> tupleExprs = null;
            for (int i = 0; i < tuple.length; ++i) {
                Map<Object, Set<Pair<Expression, ASTNode>>> valueMap = colsIndexList.get(i);
                Object value = tuple[i];
                Set<Pair<Expression, ASTNode>> set = getExpressionSet(valueMap, value);
                tupleExprs = (Set<Pair<Expression, ASTNode>>) CollectionUtil.intersectSet(tupleExprs, set);
            }
            if (tupleExprs == null || tupleExprs.isEmpty()) {
                throw new IllegalArgumentException("route: empty expression list for insertReplace stmt: " + originalSQL);
            }
            for (Pair<Expression, ASTNode> p : tupleExprs) {
                if (p.getValue() == stmt && p.getKey() instanceof RowExpression) {
                    replaceRowList.add((RowExpression) p.getKey());
                }
            }
        }
        stmt.setReplaceRowList(new ArrayList<RowExpression>(replaceRowList));
        String sql = genSQL(stmt, originalSQL);
        stmt.clearReplaceRowList();
        String dataNodeName = matchedTable.getDataNodes()[en.getKey()];
        rn[++dataNodeId] = new RouteResultsetNode(dataNodeName, sql);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) ASTNode(com.alibaba.cobar.parser.ast.ASTNode) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) InExpressionList(com.alibaba.cobar.parser.ast.expression.misc.InExpressionList) Pair(com.alibaba.cobar.parser.util.Pair) HashSet(java.util.HashSet) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) CobarHint(com.alibaba.cobar.route.hint.CobarHint) Expression(com.alibaba.cobar.parser.ast.expression.Expression) ReplacableExpression(com.alibaba.cobar.parser.ast.expression.ReplacableExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Pair (com.alibaba.cobar.parser.util.Pair)22 Expression (com.alibaba.cobar.parser.ast.expression.Expression)20 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)13 Identifier (com.alibaba.cobar.parser.ast.expression.primary.Identifier)12 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)11 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)10 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)8 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)8 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)8 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)8 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)8 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)8 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)8 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)8 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)8 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)8 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)8 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)8 ASTNode (com.alibaba.cobar.parser.ast.ASTNode)6 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)6