use of com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock in project druid by alibaba.
the class OdpsSelectParser method query.
@Override
public SQLSelectQuery query() {
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
SQLSelectQuery select = query();
accept(Token.RPAREN);
return queryRest(select);
}
OdpsSelectQueryBlock queryBlock = new OdpsSelectQueryBlock();
if (lexer.hasComment() && lexer.isKeepComments()) {
queryBlock.addBeforeComment(lexer.readAndResetComments());
}
accept(Token.SELECT);
if (lexer.token() == Token.HINT) {
this.exprParser.parseHints(queryBlock.getHints());
}
if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
}
if (lexer.token() == Token.DISTINCT) {
queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
lexer.nextToken();
} else if (lexer.token() == Token.UNIQUE) {
queryBlock.setDistionOption(SQLSetQuantifier.UNIQUE);
lexer.nextToken();
} else if (lexer.token() == Token.ALL) {
queryBlock.setDistionOption(SQLSetQuantifier.ALL);
lexer.nextToken();
}
parseSelectList(queryBlock);
parseFrom(queryBlock);
parseWhere(queryBlock);
parseGroupBy(queryBlock);
queryBlock.setOrderBy(this.exprParser.parseOrderBy());
if (lexer.token() == Token.DISTRIBUTE) {
lexer.nextToken();
accept(Token.BY);
this.exprParser.exprList(queryBlock.getDistributeBy(), queryBlock);
if (identifierEquals("SORT")) {
lexer.nextToken();
accept(Token.BY);
for (; ; ) {
SQLExpr expr = this.expr();
SQLSelectOrderByItem sortByItem = new SQLSelectOrderByItem(expr);
if (lexer.token() == Token.ASC) {
sortByItem.setType(SQLOrderingSpecification.ASC);
lexer.nextToken();
} else if (lexer.token() == Token.DESC) {
sortByItem.setType(SQLOrderingSpecification.DESC);
lexer.nextToken();
}
queryBlock.getSortBy().add(sortByItem);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
} else {
break;
}
}
}
}
if (lexer.token() == Token.LIMIT) {
lexer.nextToken();
queryBlock.setLimit(new SQLLimit(this.expr()));
}
return queryRest(queryBlock);
}
use of com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock in project druid by alibaba.
the class PagerUtils method getLimit.
/**
*
* @param sql
* @param dbType
* @return if not exists limit, return -1;
*/
public static int getLimit(String sql, String dbType) {
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
if (stmtList.size() != 1) {
return -1;
}
SQLStatement stmt = stmtList.get(0);
if (stmt instanceof SQLSelectStatement) {
SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
SQLSelectQuery query = selectStmt.getSelect().getQuery();
if (query instanceof SQLSelectQueryBlock) {
if (query instanceof MySqlSelectQueryBlock) {
SQLLimit limit = ((MySqlSelectQueryBlock) query).getLimit();
if (limit == null) {
return -1;
}
SQLExpr rowCountExpr = limit.getRowCount();
if (rowCountExpr instanceof SQLNumericLiteralExpr) {
int rowCount = ((SQLNumericLiteralExpr) rowCountExpr).getNumber().intValue();
return rowCount;
}
return Integer.MAX_VALUE;
}
if (query instanceof OdpsSelectQueryBlock) {
SQLLimit limit = ((OdpsSelectQueryBlock) query).getLimit();
SQLExpr rowCountExpr = limit != null ? limit.getRowCount() : null;
if (rowCountExpr instanceof SQLNumericLiteralExpr) {
int rowCount = ((SQLNumericLiteralExpr) rowCountExpr).getNumber().intValue();
return rowCount;
}
return Integer.MAX_VALUE;
}
return -1;
}
}
return -1;
}
use of com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock in project druid by alibaba.
the class OdpsSelectParser method query.
@Override
public SQLSelectQuery query(SQLObject parent, boolean acceptUnion) {
if (lexer.token() == Token.LPAREN) {
lexer.nextToken();
SQLSelectQuery select = query();
accept(Token.RPAREN);
return queryRest(select, acceptUnion);
}
OdpsSelectQueryBlock queryBlock = new OdpsSelectQueryBlock();
if (lexer.hasComment() && lexer.isKeepComments()) {
queryBlock.addBeforeComment(lexer.readAndResetComments());
}
if (lexer.token() == Token.FROM) {
parseFrom(queryBlock);
parseWhere(queryBlock);
parseGroupBy(queryBlock);
if (lexer.token() == Token.SELECT) {
lexer.nextToken();
if (lexer.token() == Token.HINT) {
this.exprParser.parseHints(queryBlock.getHints());
}
if (lexer.token() == Token.COMMENT) {
lexer.nextToken();
}
if (lexer.token() == Token.DISTINCT) {
queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
lexer.nextToken();
} else if (lexer.token() == Token.UNIQUE) {
Lexer.SavePoint mark = lexer.mark();
lexer.nextToken();
if (lexer.token() == Token.DOT) {
lexer.reset(mark);
} else {
queryBlock.setDistionOption(SQLSetQuantifier.UNIQUE);
}
} else if (lexer.token() == Token.ALL) {
String str = lexer.stringVal();
lexer.nextToken();
if (lexer.token() == Token.DOT) {
}
queryBlock.setDistionOption(SQLSetQuantifier.ALL);
}
parseSelectList(queryBlock);
}
if (queryBlock.getWhere() == null && lexer.token() == Token.WHERE) {
parseWhere(queryBlock);
}
} else {
accept(Token.SELECT);
if (lexer.token() == Token.HINT) {
this.exprParser.parseHints(queryBlock.getHints());
}
if (lexer.token() == Token.COMMENT) {
Lexer.SavePoint mark = lexer.mark();
String tokenStr = lexer.stringVal();
lexer.nextToken();
if (lexer.token() == Token.COMMA) {
SQLIdentifierExpr expr = new SQLIdentifierExpr(tokenStr);
queryBlock.addSelectItem(expr);
lexer.nextToken();
} else {
lexer.reset(mark);
}
}
if (queryBlock.getSelectList().isEmpty()) {
if (lexer.token() == Token.DISTINCT) {
queryBlock.setDistionOption(SQLSetQuantifier.DISTINCT);
lexer.nextToken();
} else if (lexer.token() == Token.UNIQUE) {
Lexer.SavePoint mark = lexer.mark();
lexer.nextToken();
if (lexer.token() == Token.DOT || lexer.token() == Token.COMMA) {
lexer.reset(mark);
} else {
queryBlock.setDistionOption(SQLSetQuantifier.UNIQUE);
}
} else if (lexer.token() == Token.ALL) {
Lexer.SavePoint mark = lexer.mark();
lexer.nextToken();
switch(lexer.token()) {
case DOT:
case COMMA:
case SUB:
case PLUS:
case SLASH:
case GT:
case GTEQ:
case EQ:
case LT:
case LTEQ:
lexer.reset(mark);
break;
default:
queryBlock.setDistionOption(SQLSetQuantifier.ALL);
break;
}
}
}
parseSelectList(queryBlock);
parseFrom(queryBlock);
if (queryBlock.getFrom() == null && lexer.token() == Token.LATERAL) {
lexer.nextToken();
SQLTableSource tableSource = this.parseLateralView(null);
queryBlock.setFrom(tableSource);
}
parseWhere(queryBlock);
parseGroupBy(queryBlock);
}
if (lexer.identifierEquals(FnvHash.Constants.WINDOW)) {
parseWindow(queryBlock);
}
parseGroupBy(queryBlock);
queryBlock.setOrderBy(this.exprParser.parseOrderBy());
queryBlock.setZOrderBy(this.exprParser.parseZOrderBy());
if (lexer.token() == Token.DISTRIBUTE) {
lexer.nextToken();
accept(Token.BY);
for (; ; ) {
SQLSelectOrderByItem distributeByItem = this.exprParser.parseSelectOrderByItem();
queryBlock.addDistributeBy(distributeByItem);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
} else {
break;
}
}
}
if (lexer.identifierEquals(FnvHash.Constants.ZORDER)) {
queryBlock.setZOrderBy(this.exprParser.parseZOrderBy());
}
if (lexer.identifierEquals(FnvHash.Constants.SORT)) {
lexer.nextToken();
accept(Token.BY);
for (; ; ) {
SQLSelectOrderByItem sortByItem = this.exprParser.parseSelectOrderByItem();
queryBlock.addSortBy(sortByItem);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
} else {
break;
}
}
}
if (lexer.identifierEquals(FnvHash.Constants.CLUSTER)) {
lexer.nextToken();
accept(Token.BY);
for (; ; ) {
SQLSelectOrderByItem clusterByItem = this.exprParser.parseSelectOrderByItem();
queryBlock.addClusterBy(clusterByItem);
if (lexer.token() == Token.COMMA) {
lexer.nextToken();
} else {
break;
}
}
}
if (lexer.token() == Token.LIMIT) {
SQLLimit limit = exprParser.parseLimit();
queryBlock.setLimit(limit);
}
return queryRest(queryBlock, acceptUnion);
}
use of com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock in project druid by alibaba.
the class SQLSelectBuilderImpl method limit.
@Override
public SQLSelectBuilderImpl limit(int rowCount, int offset) {
SQLSelectQueryBlock queryBlock = getQueryBlock();
if (queryBlock instanceof MySqlSelectQueryBlock) {
MySqlSelectQueryBlock mySqlQueryBlock = (MySqlSelectQueryBlock) queryBlock;
SQLLimit limit = new SQLLimit();
limit.setRowCount(new SQLIntegerExpr(rowCount));
if (offset > 0) {
limit.setOffset(new SQLIntegerExpr(offset));
}
mySqlQueryBlock.setLimit(limit);
return this;
}
if (queryBlock instanceof SQLServerSelectQueryBlock) {
SQLServerSelectQueryBlock sqlserverQueryBlock = (SQLServerSelectQueryBlock) queryBlock;
if (offset <= 0) {
SQLServerTop top = new SQLServerTop();
top.setExpr(new SQLIntegerExpr(rowCount));
sqlserverQueryBlock.setTop(top);
} else {
throw new UnsupportedOperationException("not support offset");
}
return this;
}
if (queryBlock instanceof PGSelectQueryBlock) {
PGSelectQueryBlock pgQueryBlock = (PGSelectQueryBlock) queryBlock;
SQLLimit limit = new SQLLimit();
if (offset > 0) {
limit.setOffset(new SQLIntegerExpr(offset));
}
limit.setRowCount(new SQLIntegerExpr(rowCount));
pgQueryBlock.setLimit(limit);
return this;
}
if (queryBlock instanceof DB2SelectQueryBlock) {
DB2SelectQueryBlock db2QueryBlock = (DB2SelectQueryBlock) queryBlock;
if (offset <= 0) {
SQLExpr rowCountExpr = new SQLIntegerExpr(rowCount);
db2QueryBlock.setFirst(rowCountExpr);
} else {
throw new UnsupportedOperationException("not support offset");
}
return this;
}
if (queryBlock instanceof OracleSelectQueryBlock) {
OracleSelectQueryBlock oracleQueryBlock = (OracleSelectQueryBlock) queryBlock;
if (offset <= 0) {
SQLExpr rowCountExpr = new SQLIntegerExpr(rowCount);
SQLExpr newCondition = SQLUtils.buildCondition(SQLBinaryOperator.BooleanAnd, rowCountExpr, false, oracleQueryBlock.getWhere());
queryBlock.setWhere(newCondition);
} else {
throw new UnsupportedOperationException("not support offset");
}
return this;
}
if (queryBlock instanceof OdpsSelectQueryBlock) {
OdpsSelectQueryBlock odpsQueryBlock = (OdpsSelectQueryBlock) queryBlock;
if (offset > 0) {
throw new UnsupportedOperationException("not support offset");
}
odpsQueryBlock.setLimit(new SQLLimit(new SQLIntegerExpr(rowCount)));
return this;
}
throw new UnsupportedOperationException();
}
Aggregations