Search in sources :

Example 1 with GroupBy

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

the class MySQLOutputASTVisitor method visit.

@Override
public void visit(DMLSelectStatement node) {
    appendable.append("SELECT ");
    final DMLSelectStatement.SelectOption option = node.getOption();
    switch(option.resultDup) {
        case ALL:
            break;
        case DISTINCT:
            appendable.append("DISTINCT ");
            break;
        case DISTINCTROW:
            appendable.append("DISTINCTROW ");
            break;
        default:
            throw new IllegalArgumentException("unknown option for SELECT: " + option);
    }
    if (option.highPriority) {
        appendable.append("HIGH_PRIORITY ");
    }
    if (option.straightJoin) {
        appendable.append("STRAIGHT_JOIN ");
    }
    switch(option.resultSize) {
        case SQL_BIG_RESULT:
            appendable.append("SQL_BIG_RESULT ");
            break;
        case SQL_SMALL_RESULT:
            appendable.append("SQL_SMALL_RESULT ");
            break;
        case UNDEF:
            break;
        default:
            throw new IllegalArgumentException("unknown option for SELECT: " + option);
    }
    if (option.sqlBufferResult) {
        appendable.append("SQL_BUFFER_RESULT ");
    }
    switch(option.queryCache) {
        case SQL_CACHE:
            appendable.append("SQL_CACHE ");
            break;
        case SQL_NO_CACHE:
            appendable.append("SQL_NO_CACHE ");
            break;
        case UNDEF:
            break;
        default:
            throw new IllegalArgumentException("unknown option for SELECT: " + option);
    }
    if (option.sqlCalcFoundRows) {
        appendable.append("SQL_CALC_FOUND_ROWS ");
    }
    boolean isFst = true;
    List<Pair<Expression, String>> exprList = node.getSelectExprList();
    for (Pair<Expression, String> p : exprList) {
        if (isFst)
            isFst = false;
        else
            appendable.append(", ");
        p.getKey().accept(this);
        String alias = p.getValue();
        if (alias != null) {
            appendable.append(" AS ").append(alias);
        }
    }
    TableReferences from = node.getTables();
    if (from != null) {
        appendable.append(" FROM ");
        from.accept(this);
    }
    Expression where = node.getWhere();
    if (where != null) {
        appendable.append(" WHERE ");
        where.accept(this);
    }
    GroupBy group = node.getGroup();
    if (group != null) {
        appendable.append(' ');
        group.accept(this);
    }
    Expression having = node.getHaving();
    if (having != null) {
        appendable.append(" HAVING ");
        having.accept(this);
    }
    OrderBy order = node.getOrder();
    if (order != null) {
        appendable.append(' ');
        order.accept(this);
    }
    Limit limit = node.getLimit();
    if (limit != null) {
        appendable.append(' ');
        limit.accept(this);
    }
    switch(option.lockMode) {
        case FOR_UPDATE:
            appendable.append(" FOR UPDATE");
            break;
        case LOCK_IN_SHARE_MODE:
            appendable.append(" LOCK IN SHARE MODE");
            break;
        case UNDEF:
            break;
        default:
            throw new IllegalArgumentException("unknown option for SELECT: " + option);
    }
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) GroupBy(com.alibaba.cobar.parser.ast.fragment.GroupBy) LiteralString(com.alibaba.cobar.parser.ast.expression.primary.literal.LiteralString) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences) 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) DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement) Limit(com.alibaba.cobar.parser.ast.fragment.Limit) Pair(com.alibaba.cobar.parser.util.Pair)

Example 2 with GroupBy

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

the class MySQLDMLParserTest method testGroupBy.

public void testGroupBy() throws SQLSyntaxErrorException {
    String sql = "group by c1 asc, c2 desc  , c3 with rollup";
    MySQLLexer lexer = new MySQLLexer(sql);
    MySQLDMLParser parser = getDMLParser(lexer);
    GroupBy groupBy = parser.groupBy();
    String output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC), new Pair<Expression, SortOrder>(new Identifier(null, "c2"), SortOrder.DESC), new Pair<Expression, SortOrder>(new Identifier(null, "c3"), SortOrder.ASC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1, c2 DESC, c3 WITH ROLLUP", output);
    sql = "group by c1 asc, c2 desc  , c3 ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    groupBy = parser.groupBy();
    output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC), new Pair<Expression, SortOrder>(new Identifier(null, "c2"), SortOrder.DESC), new Pair<Expression, SortOrder>(new Identifier(null, "c3"), SortOrder.ASC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1, c2 DESC, c3", output);
    sql = "group by c1   ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    groupBy = parser.groupBy();
    output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1", output);
    sql = "group by c1 asc  ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    groupBy = parser.groupBy();
    output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1", output);
    sql = "group by c1 desc  ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    groupBy = parser.groupBy();
    output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.DESC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1 DESC", output);
    sql = "group by c1 with rollup  ";
    lexer = new MySQLLexer(sql);
    parser = getDMLParser(lexer);
    groupBy = parser.groupBy();
    output = output2MySQL(groupBy, sql);
    ListUtil.isEquals(ListUtil.createList(new Pair<Expression, SortOrder>(new Identifier(null, "c1"), SortOrder.ASC)), groupBy.getOrderByList());
    Assert.assertEquals("GROUP BY c1 WITH ROLLUP", output);
}
Also used : MySQLLexer(com.alibaba.cobar.parser.recognizer.mysql.lexer.MySQLLexer) GroupBy(com.alibaba.cobar.parser.ast.fragment.GroupBy) Identifier(com.alibaba.cobar.parser.ast.expression.primary.Identifier) Pair(com.alibaba.cobar.parser.util.Pair)

Example 3 with GroupBy

use of com.alibaba.cobar.parser.ast.fragment.GroupBy 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 4 with GroupBy

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

the class MySQLDMLSelectParser method select.

@Override
public DMLSelectStatement select() throws SQLSyntaxErrorException {
    match(KW_SELECT);
    DMLSelectStatement.SelectOption option = selectOption();
    List<Pair<Expression, String>> exprList = selectExprList();
    TableReferences tables = null;
    Expression where = null;
    GroupBy group = null;
    Expression having = null;
    OrderBy order = null;
    Limit limit = null;
    boolean dual = false;
    if (lexer.token() == KW_FROM) {
        if (lexer.nextToken() == KW_DUAL) {
            lexer.nextToken();
            dual = true;
            List<TableReference> trs = new ArrayList<TableReference>(1);
            trs.add(new Dual());
            tables = new TableReferences(trs);
        } else {
            tables = tableRefs();
        }
    }
    if (lexer.token() == KW_WHERE) {
        lexer.nextToken();
        where = exprParser.expression();
    }
    if (!dual) {
        group = groupBy();
        if (lexer.token() == KW_HAVING) {
            lexer.nextToken();
            having = exprParser.expression();
        }
        order = orderBy();
    }
    limit = limit();
    if (!dual) {
        switch(lexer.token()) {
            case KW_FOR:
                lexer.nextToken();
                match(KW_UPDATE);
                option.lockMode = DMLSelectStatement.LockMode.FOR_UPDATE;
                break;
            case KW_LOCK:
                lexer.nextToken();
                match(KW_IN);
                matchIdentifier("SHARE");
                matchIdentifier("MODE");
                option.lockMode = DMLSelectStatement.LockMode.LOCK_IN_SHARE_MODE;
                break;
        }
    }
    return new DMLSelectStatement(option, exprList, tables, where, group, having, order, limit);
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) GroupBy(com.alibaba.cobar.parser.ast.fragment.GroupBy) ArrayList(java.util.ArrayList) TableReferences(com.alibaba.cobar.parser.ast.fragment.tableref.TableReferences) TableReference(com.alibaba.cobar.parser.ast.fragment.tableref.TableReference) Expression(com.alibaba.cobar.parser.ast.expression.Expression) DMLSelectStatement(com.alibaba.cobar.parser.ast.stmt.dml.DMLSelectStatement) Limit(com.alibaba.cobar.parser.ast.fragment.Limit) Dual(com.alibaba.cobar.parser.ast.fragment.tableref.Dual) Pair(com.alibaba.cobar.parser.util.Pair)

Example 5 with GroupBy

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

the class PartitionKeyVisitor method visit.

@Override
public void visit(DMLSelectStatement node) {
    boolean verdictGroup = true;
    List<Expression> exprList = node.getSelectExprListWithoutAlias();
    if (verdictGroupFunc) {
        for (Expression expr : exprList) {
            if (!isGroupFuncPassthroughSelect(expr)) {
                groupFuncType = GROUP_CANCEL;
                verdictGroup = false;
                break;
            }
        }
        limit(node.getLimit());
    }
    visitChild(2, false, verdictGroupFunc && verdictGroup, exprList);
    TableReference tr = node.getTables();
    visitChild(1, verdictColumn, verdictGroupFunc && verdictGroup, tr);
    Expression where = node.getWhere();
    visitChild(2, verdictColumn, false, where);
    GroupBy group = node.getGroup();
    visitChild(2, false, false, group);
    Expression having = node.getHaving();
    visitChild(2, verdictColumn, false, having);
    OrderBy order = node.getOrder();
    visitChild(2, false, false, order);
}
Also used : OrderBy(com.alibaba.cobar.parser.ast.fragment.OrderBy) TableReference(com.alibaba.cobar.parser.ast.fragment.tableref.TableReference) GroupBy(com.alibaba.cobar.parser.ast.fragment.GroupBy) 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)

Aggregations

GroupBy (com.alibaba.cobar.parser.ast.fragment.GroupBy)5 Expression (com.alibaba.cobar.parser.ast.expression.Expression)4 QueryExpression (com.alibaba.cobar.parser.ast.expression.misc.QueryExpression)3 OrderBy (com.alibaba.cobar.parser.ast.fragment.OrderBy)3 Pair (com.alibaba.cobar.parser.util.Pair)3 BinaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.BinaryOperatorExpression)2 PolyadicOperatorExpression (com.alibaba.cobar.parser.ast.expression.PolyadicOperatorExpression)2 UnaryOperatorExpression (com.alibaba.cobar.parser.ast.expression.UnaryOperatorExpression)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 InExpression (com.alibaba.cobar.parser.ast.expression.comparison.InExpression)2 LogicalAndExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalAndExpression)2 LogicalOrExpression (com.alibaba.cobar.parser.ast.expression.logical.LogicalOrExpression)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 RowExpression (com.alibaba.cobar.parser.ast.expression.primary.RowExpression)2 FunctionExpression (com.alibaba.cobar.parser.ast.expression.primary.function.FunctionExpression)2