Search in sources :

Example 1 with SortOrder

use of com.alibaba.cobar.parser.ast.fragment.SortOrder in project cobar by alibaba.

the class MySQLOutputASTVisitor method visit.

@Override
public void visit(GroupBy node) {
    appendable.append("GROUP BY ");
    boolean isFst = true;
    for (Pair<Expression, SortOrder> p : node.getOrderByList()) {
        if (isFst)
            isFst = false;
        else
            appendable.append(", ");
        Expression col = p.getKey();
        col.accept(this);
        switch(p.getValue()) {
            case DESC:
                appendable.append(" DESC");
                break;
        }
    }
    if (node.isWithRollup()) {
        appendable.append(" WITH ROLLUP");
    }
}
Also used : 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) VariableExpression(com.alibaba.cobar.parser.ast.expression.primary.VariableExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) 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) TernaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression) 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) SortOrder(com.alibaba.cobar.parser.ast.fragment.SortOrder)

Example 2 with SortOrder

use of com.alibaba.cobar.parser.ast.fragment.SortOrder in project cobar by alibaba.

the class MySQLDMLParser method groupBy.

/**
 * nothing has been pre-consumed
 *
 * @return null if there is no order by
 */
protected GroupBy groupBy() throws SQLSyntaxErrorException {
    if (lexer.token() != KW_GROUP) {
        return null;
    }
    lexer.nextToken();
    match(KW_BY);
    Expression expr = exprParser.expression();
    SortOrder order = SortOrder.ASC;
    GroupBy groupBy;
    switch(lexer.token()) {
        case KW_DESC:
            order = SortOrder.DESC;
        case KW_ASC:
            lexer.nextToken();
        default:
            break;
    }
    switch(lexer.token()) {
        case KW_WITH:
            lexer.nextToken();
            matchIdentifier("ROLLUP");
            return new GroupBy(expr, order, true);
        case PUNC_COMMA:
            break;
        default:
            return new GroupBy(expr, order, false);
    }
    for (groupBy = new GroupBy().addOrderByItem(expr, order); lexer.token() == PUNC_COMMA; ) {
        lexer.nextToken();
        order = SortOrder.ASC;
        expr = exprParser.expression();
        switch(lexer.token()) {
            case KW_DESC:
                order = SortOrder.DESC;
            case KW_ASC:
                lexer.nextToken();
            default:
                break;
        }
        groupBy.addOrderByItem(expr, order);
        if (lexer.token() == KW_WITH) {
            lexer.nextToken();
            matchIdentifier("ROLLUP");
            return groupBy.setWithRollup();
        }
    }
    return groupBy;
}
Also used : GroupBy(com.alibaba.cobar.parser.ast.fragment.GroupBy) Expression(com.alibaba.cobar.parser.ast.expression.Expression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) SortOrder(com.alibaba.cobar.parser.ast.fragment.SortOrder)

Example 3 with SortOrder

use of com.alibaba.cobar.parser.ast.fragment.SortOrder in project cobar by alibaba.

the class MySQLDMLParser method orderBy.

/**
 * nothing has been pre-consumed
 *
 * @return null if there is no order by
 */
protected OrderBy orderBy() throws SQLSyntaxErrorException {
    if (lexer.token() != KW_ORDER) {
        return null;
    }
    lexer.nextToken();
    match(KW_BY);
    Expression expr = exprParser.expression();
    SortOrder order = SortOrder.ASC;
    OrderBy orderBy;
    switch(lexer.token()) {
        case KW_DESC:
            order = SortOrder.DESC;
        case KW_ASC:
            if (lexer.nextToken() != PUNC_COMMA) {
                return new OrderBy(expr, order);
            }
        case PUNC_COMMA:
            orderBy = new OrderBy();
            orderBy.addOrderByItem(expr, order);
            break;
        default:
            return new OrderBy(expr, order);
    }
    for (; lexer.token() == PUNC_COMMA; ) {
        lexer.nextToken();
        order = SortOrder.ASC;
        expr = exprParser.expression();
        switch(lexer.token()) {
            case KW_DESC:
                order = SortOrder.DESC;
            case KW_ASC:
                lexer.nextToken();
        }
        orderBy.addOrderByItem(expr, order);
    }
    return orderBy;
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) Expression(com.alibaba.cobar.parser.ast.expression.Expression) QueryExpression(com.alibaba.cobar.parser.ast.expression.misc.QueryExpression) SortOrder(com.alibaba.cobar.parser.ast.fragment.SortOrder)

Example 4 with SortOrder

use of com.alibaba.cobar.parser.ast.fragment.SortOrder in project cobar by alibaba.

the class PartitionKeyVisitor method sortPairList.

private void sortPairList(List<Pair<Expression, SortOrder>> list) {
    if (list == null || list.isEmpty())
        return;
    Expression[] exprs = new Expression[list.size()];
    int i = 0;
    for (Pair<Expression, SortOrder> p : list) {
        exprs[i] = p.getKey();
        ++i;
    }
    visitChild(2, false, false, exprs);
}
Also used : 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) SortOrder(com.alibaba.cobar.parser.ast.fragment.SortOrder) IndexHint(com.alibaba.cobar.parser.ast.fragment.tableref.IndexHint)

Example 5 with SortOrder

use of com.alibaba.cobar.parser.ast.fragment.SortOrder in project cobar by alibaba.

the class MySQLOutputASTVisitor method visit.

@Override
public void visit(OrderBy node) {
    appendable.append("ORDER BY ");
    boolean isFst = true;
    for (Pair<Expression, SortOrder> p : node.getOrderByList()) {
        if (isFst)
            isFst = false;
        else
            appendable.append(", ");
        Expression col = p.getKey();
        col.accept(this);
        switch(p.getValue()) {
            case DESC:
                appendable.append(" DESC");
                break;
        }
    }
}
Also used : 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) VariableExpression(com.alibaba.cobar.parser.ast.expression.primary.VariableExpression) ComparisionIsExpression(com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression) LogicalOrExpression(com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression) 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) TernaryOperatorExpression(com.alibaba.cobar.parser.ast.expression.TernaryOperatorExpression) 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) SortOrder(com.alibaba.cobar.parser.ast.fragment.SortOrder)

Aggregations

Expression (com.alibaba.cobar.parser.ast.expression.Expression)5 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)5 SortOrder (com.alibaba.cobar.parser.ast.fragment.SortOrder)5 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)3 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)3 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)3 BetweenAndExpression (com.alibaba.cobar.parser.ast.expression.comparison.BetweenAndExpression)3 ComparisionEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionEqualsExpression)3 ComparisionIsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionIsExpression)3 ComparisionNullSafeEqualsExpression (com.alibaba.cobar.parser.ast.expression.comparison.ComparisionNullSafeEqualsExpression)3 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)3 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)3 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)3 UserExpression (com.alibaba.cobar.parser.ast.expression.misc.UserExpression)3 CaseWhenOperatorExpression (com.alibaba.cobar.parser.ast.expression.primary.CaseWhenOperatorExpression)3 MatchExpression (com.alibaba.cobar.parser.ast.expression.primary.MatchExpression)3 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)3 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)3 LikeExpression (com.alibaba.cobar.parser.ast.expression.string.LikeExpression)3 CollateExpression (com.alibaba.cobar.parser.ast.expression.type.CollateExpression)3