Search in sources :

Example 1 with InExpressionList

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

the class MySQLExprParser method rightOprandOfIn.

/**
     * @return {@link QueryExpression} or {@link InExpressionList}
     */
private Expression rightOprandOfIn() throws SQLSyntaxErrorException {
    match(PUNC_LEFT_PAREN);
    if (KW_SELECT == lexer.token()) {
        QueryExpression subq = subQuery();
        match(PUNC_RIGHT_PAREN);
        return subq;
    }
    return new InExpressionList(expressionList(new LinkedList<Expression>())).setCacheEvalRst(cacheEvalRst);
}
Also used : InExpressionList(com.alibaba.cobar.parser.ast.expression.misc.InExpressionList) SubqueryAllExpression(com.alibaba.cobar.parser.ast.expression.misc.SubqueryAllExpression) MinusExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.MinusExpression) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) BitAndExpression(com.alibaba.cobar.parser.ast.expression.bit.BitAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) NegativeValueExpression(com.alibaba.cobar.parser.ast.expression.logical.NegativeValueExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) CastBinaryExpression(com.alibaba.cobar.parser.ast.expression.type.CastBinaryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) BitInvertExpression(com.alibaba.cobar.parser.ast.expression.bit.BitInvertExpression) BitXORExpression(com.alibaba.cobar.parser.ast.expression.bit.BitXORExpression) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) BitShiftExpression(com.alibaba.cobar.parser.ast.expression.bit.BitShiftExpression) BitOrExpression(com.alibaba.cobar.parser.ast.expression.bit.BitOrExpression) ComparisionGreaterThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanOrEqualsExpression) MatchExpression(com.alibaba.cobar.parser.ast.expression.primary.MatchExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ComparisionGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionGreaterThanExpression) ComparisionLessOrGreaterThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessOrGreaterThanExpression) SubqueryAnyExpression(com.alibaba.cobar.parser.ast.expression.misc.SubqueryAnyExpression) RegexpExpression(com.alibaba.cobar.parser.ast.expression.string.RegexpExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) ArithmeticMultiplyExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticMultiplyExpression) ComparisionLessThanOrEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanOrEqualsExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) ArithmeticSubtractExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticSubtractExpression) SoundsLikeExpression(com.alibaba.cobar.parser.ast.expression.string.SoundsLikeExpression) ArithmeticIntegerDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticIntegerDivideExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) LogicalNotExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalNotExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) AssignmentExpression(com.alibaba.cobar.parser.ast.expression.misc.AssignmentExpression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) LogicalXORExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalXORExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) ArithmeticAddExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression) ComparisionNotEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNotEqualsExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) ComparisionLessThanExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionLessThanExpression) ArithmeticModExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticModExpression) ArithmeticDivideExpression(com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticDivideExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)

Example 2 with InExpressionList

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

the class PartitionKeyVisitor method visit.

@Override
public void visit(InExpression node) {
    Expression left = node.getLeftOprand();
    Expression right = node.getRightOprand();
    visitChild(2, false, false, left, right);
    if (verdictColumn && !node.isNot() && left instanceof Identifier && right instanceof InExpressionList) {
        Identifier col = (Identifier) left;
        String colName = col.getIdTextUpUnescape();
        String table = tableAlias.get(col.getLevelUnescapeUpName(2));
        if (isRuledColumn(table, colName)) {
            List<Object> valList = ensureColumnValueList(ensureColumnValueByTable(table), colName);
            Map<Object, Set<Pair<Expression, ASTNode>>> valMap = ensureColumnValueIndexObjMap(ensureColumnValueIndexByTable(table), colName);
            InExpressionList inlist = (InExpressionList) right;
            for (Expression expr : inlist.getList()) {
                Object value = expr.evaluation(evaluationParameter);
                if (value != Expression.UNEVALUATABLE) {
                    valList.add(value);
                    addIntoColumnValueIndex(valMap, value, expr, node);
                }
            }
        }
    }
}
Also used : InExpressionList(com.alibaba.cobar.parser.ast.expression.misc.InExpressionList) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) SmallSet(com.alibaba.cobar.util.SmallSet) Set(java.util.Set) HashSet(java.util.HashSet) ShowCharaterSet(com.alibaba.cobar.parser.ast.stmt.dal.ShowCharaterSet) BetweenAndExpression(com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression) ComparisionEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) InExpression(com.alibaba.cobar.parser.ast.expression.comparison.InExpression) FunctionExpression(com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression) MatchExpression(com.alibaba.cobar.parser.ast.expression.primary.MatchExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) ReplacableExpression(com.alibaba.cobar.parser.ast.expression.ReplacableExpression) LogicalAndExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression) UnaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression) ComparisionNullSafeEqualsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression) LikeExpression(com.alibaba.cobar.parser.ast.expression.string.LikeExpression) CollateExpression(com.alibaba.cobar.parser.ast.expression.type.CollateExpression) Expression(com.alibaba.cobar.parser.ast.expression.Expression) CaseWhenOperatorExpression(com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression) UserExpression(com.alibaba.cobar.parser.ast.expression.misc.UserExpression) PolyadicOperatorExpression(com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression) BinaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression) RowExpression(com.alibaba.cobar.parser.ast.expression.primary.RowExpression) ASTNode(com.alibaba.cobar.parser.ast.ASTNode) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString)

Example 3 with InExpressionList

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

Expression (com.alibaba.cobar.parser.ast.expression.Expression)3 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)3 InExpressionList (com.alibaba.cobar.parser.ast.expression.misc.InExpressionList)3 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)3 ASTNode (com.alibaba.cobar.parser.ast.ASTNode)2 ReplacableExpression (com.alibaba.cobar.parser.ast.expression.ReplacableExpression)2 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)2 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)2 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)2 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)2 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)2 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)2 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)2 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)2 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)2 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)2 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)2 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)2 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)2 HashSet (java.util.HashSet)2