Search in sources :

Example 11 with Expression

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

the class MySQLDMLUpdateParser method update.

/**
 * nothing has been pre-consumed <code><pre>
 * 'UPDATE' 'LOW_PRIORITY'? 'IGNORE'? table_reference
 *   'SET' colName ('='|':=') (expr|'DEFAULT') (',' colName ('='|':=') (expr|'DEFAULT'))*
 *     ('WHERE' cond)?
 *     {singleTable}? => ('ORDER' 'BY' orderBy)?  ('LIMIT' count)?
 * </pre></code>
 */
public DMLUpdateStatement update() throws SQLSyntaxErrorException {
    match(KW_UPDATE);
    boolean lowPriority = false;
    boolean ignore = false;
    if (lexer.token() == KW_LOW_PRIORITY) {
        lexer.nextToken();
        lowPriority = true;
    }
    if (lexer.token() == KW_IGNORE) {
        lexer.nextToken();
        ignore = true;
    }
    TableReferences tableRefs = tableRefs();
    match(KW_SET);
    List<Pair<Identifier, Expression>> values;
    Identifier col = identifier();
    match(OP_EQUALS, OP_ASSIGN);
    Expression expr = exprParser.expression();
    if (lexer.token() == PUNC_COMMA) {
        values = new LinkedList<Pair<Identifier, Expression>>();
        values.add(new Pair<Identifier, Expression>(col, expr));
        for (; lexer.token() == PUNC_COMMA; ) {
            lexer.nextToken();
            col = identifier();
            match(OP_EQUALS, OP_ASSIGN);
            expr = exprParser.expression();
            values.add(new Pair<Identifier, Expression>(col, expr));
        }
    } else {
        values = new ArrayList<Pair<Identifier, Expression>>(1);
        values.add(new Pair<Identifier, Expression>(col, expr));
    }
    Expression where = null;
    if (lexer.token() == KW_WHERE) {
        lexer.nextToken();
        where = exprParser.expression();
    }
    OrderBy orderBy = null;
    Limit limit = null;
    if (tableRefs.isSingleTable()) {
        orderBy = orderBy();
        limit = limit();
    }
    return new DMLUpdateStatement(lowPriority, ignore, tableRefs, values, where, orderBy, limit);
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Expression(com.alibaba.cobar.parser.ast.expression.Expression) DMLUpdateStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLUpdateStatement) Limit(com.alibaba.cobar.parser.ast.fragment.Limit) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences) Pair(com.alibaba.cobar.parser.util.Pair)

Example 12 with Expression

use of com.alibaba.cobar.parser.ast.expression.Expression 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 13 with Expression

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

the class ServerRouter method validateAST.

private static void validateAST(SQLStatement ast, TableConfig tc, RuleConfig rule, PartitionKeyVisitor visitor) throws SQLNonTransientException {
    if (ast instanceof DMLUpdateStatement) {
        List<Identifier> columns = null;
        List<String> ruleCols = rule.getColumns();
        DMLUpdateStatement update = (DMLUpdateStatement) ast;
        for (Pair<Identifier, Expression> pair : update.getValues()) {
            for (String ruleCol : ruleCols) {
                if (equals(pair.getKey().getIdTextUpUnescape(), ruleCol)) {
                    if (columns == null) {
                        columns = new ArrayList<Identifier>(ruleCols.size());
                    }
                    columns.add(pair.getKey());
                }
            }
        }
        if (columns == null) {
            return;
        }
        Map<String, String> alias = visitor.getTableAlias();
        for (Identifier column : columns) {
            String table = column.getLevelUnescapeUpName(2);
            table = alias.get(table);
            if (table != null && table.equals(tc.getName())) {
                throw new SQLFeatureNotSupportedException("partition key cannot be changed");
            }
        }
    }
}
Also used : Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) 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) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) DMLUpdateStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLUpdateStatement)

Example 14 with Expression

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

the class MySQLExprParser method intervalExpression.

/**
 * first <code>INTERVAL</code> has been consumed
 */
private Expression intervalExpression() throws SQLSyntaxErrorException {
    Expression fstExpr;
    List<Expression> argList = null;
    if (lexer.token() == PUNC_LEFT_PAREN) {
        if (lexer.nextToken() == KW_SELECT) {
            fstExpr = subQuery();
            match(PUNC_RIGHT_PAREN);
        } else {
            fstExpr = expression();
            if (lexer.token() == PUNC_COMMA) {
                lexer.nextToken();
                argList = new LinkedList<Expression>();
                argList.add(fstExpr);
                argList = expressionList(argList);
            } else {
                match(PUNC_RIGHT_PAREN);
            }
        }
    } else {
        fstExpr = expression();
    }
    if (argList != null) {
        return new Interval(argList).setCacheEvalRst(cacheEvalRst);
    }
    return new IntervalPrimary(fstExpr, intervalPrimaryUnit()).setCacheEvalRst(cacheEvalRst);
}
Also used : 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) IntervalPrimary(com.alibaba.cobar.parser.ast.expression.primary.literal.IntervalPrimary) Interval(com.alibaba.cobar.parser.ast.expression.primary.function.comparison.Interval)

Example 15 with Expression

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

the class MySQLExprParser method functionChar.

/**
 * first '(' has been consumed
 */
private Char functionChar() throws SQLSyntaxErrorException {
    Char chr;
    for (List<Expression> tempExprList = new LinkedList<Expression>(); ; ) {
        Expression tempExpr = expression();
        tempExprList.add(tempExpr);
        switch(lexer.token()) {
            case PUNC_COMMA:
                lexer.nextToken();
                continue;
            case PUNC_RIGHT_PAREN:
                lexer.nextToken();
                chr = new Char(tempExprList, null);
                chr.setCacheEvalRst(cacheEvalRst);
                return chr;
            case KW_USING:
                lexer.nextToken();
                String tempStr = lexer.stringValue();
                match(IDENTIFIER);
                match(PUNC_RIGHT_PAREN);
                chr = new Char(tempExprList, tempStr);
                chr.setCacheEvalRst(cacheEvalRst);
                return chr;
            default:
                throw err("expect ',' or 'USING' or ')' but is " + lexer.token());
        }
    }
}
Also used : 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) Char(com.alibaba.cobar.parser.ast.expression.primary.function.string.Char) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString) LinkedList(java.util.LinkedList)

Aggregations

Expression (com.alibaba.cobar.parser.ast.expression.Expression)128 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)96 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)93 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)91 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)88 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)88 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)87 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)87 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)87 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)87 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)87 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)87 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)87 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)87 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)76 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)71 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)58 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)58 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)58 ArithmeticAddExpression (com.alibaba.cobar.parser.ast.expression.arithmeic.ArithmeticAddExpression)45