Search in sources :

Example 1 with OdpsSelectQueryBlock

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);
}
Also used : SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) OdpsSelectQueryBlock(com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 2 with OdpsSelectQueryBlock

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;
}
Also used : SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) SQLSelectQuery(com.alibaba.druid.sql.ast.statement.SQLSelectQuery) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) OdpsSelectQueryBlock(com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 3 with OdpsSelectQueryBlock

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);
}
Also used : SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) OdpsSelectQueryBlock(com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock)

Example 4 with OdpsSelectQueryBlock

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();
}
Also used : SQLServerTop(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop) OracleSelectQueryBlock(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock) SQLServerSelectQueryBlock(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock) SQLLimit(com.alibaba.druid.sql.ast.SQLLimit) DB2SelectQueryBlock(com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) OdpsSelectQueryBlock(com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock) MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) PGSelectQueryBlock(com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

OdpsSelectQueryBlock (com.alibaba.druid.sql.dialect.odps.ast.OdpsSelectQueryBlock)4 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)3 SQLLimit (com.alibaba.druid.sql.ast.SQLLimit)3 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)2 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)2 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)2 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)1 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)1 SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)1 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)1 DB2SelectQueryBlock (com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock)1 OracleSelectQueryBlock (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock)1 PGSelectQueryBlock (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)1 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)1 SQLServerTop (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop)1