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);
}
}
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);
}
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;
}
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);
}
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);
}
Aggregations