Search in sources :

Example 1 with ReplacableExpression

use of com.alibaba.cobar.parser.ast.expression.ReplacableExpression in project cobar by alibaba.

the class ServerRouter method replacePartitionKeyOperand.

private static void replacePartitionKeyOperand(Map<String, Map<Object, Set<Pair<Expression, ASTNode>>>> index, List<String> cols) {
    if (cols == null) {
        return;
    }
    for (String col : cols) {
        Map<Object, Set<Pair<Expression, ASTNode>>> map = index.get(col);
        if (map == null) {
            continue;
        }
        for (Set<Pair<Expression, ASTNode>> set : map.values()) {
            if (set == null) {
                continue;
            }
            for (Pair<Expression, ASTNode> p : set) {
                Expression expr = p.getKey();
                ASTNode parent = p.getValue();
                if (PartitionKeyVisitor.isPartitionKeyOperandSingle(expr, parent)) {
                    ((ReplacableExpression) expr).setReplaceExpr(ReplacableExpression.BOOL_FALSE);
                } else if (PartitionKeyVisitor.isPartitionKeyOperandIn(expr, parent)) {
                    ((ReplacableExpression) parent).setReplaceExpr(ReplacableExpression.BOOL_FALSE);
                }
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) 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) ReplacableExpression(com.alibaba.cobar.parser.ast.expression.ReplacableExpression) ASTNode(com.alibaba.cobar.parser.ast.ASTNode) Pair(com.alibaba.cobar.parser.util.Pair)

Example 2 with ReplacableExpression

use of com.alibaba.cobar.parser.ast.expression.ReplacableExpression 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)

Aggregations

ASTNode (com.alibaba.cobar.parser.ast.ASTNode)2 Expression (com.alibaba.cobar.parser.ast.expression.Expression)2 ReplacableExpression (com.alibaba.cobar.parser.ast.expression.ReplacableExpression)2 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)2 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)2 Pair (com.alibaba.cobar.parser.util.Pair)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 InExpressionList (com.alibaba.cobar.parser.ast.expression.misc.InExpressionList)1 CobarHint (com.alibaba.cobar.route.hint.CobarHint)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1