Search in sources :

Example 31 with SQLSelectOrderByItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem in project druid by alibaba.

the class AntsparkCreateTableParser method parseCreateTable.

public SQLCreateTableStatement parseCreateTable(boolean acceptCreate) {
    AntsparkCreateTableStatement stmt = new AntsparkCreateTableStatement();
    if (acceptCreate) {
        if (lexer.hasComment() && lexer.isKeepComments()) {
            stmt.addBeforeComment(lexer.readAndResetComments());
        }
        accept(Token.CREATE);
    }
    if (lexer.identifierEquals(FnvHash.Constants.EXTERNAL)) {
        lexer.nextToken();
        stmt.setExternal(true);
    }
    if (lexer.identifierEquals(FnvHash.Constants.TEMPORARY)) {
        lexer.nextToken();
        stmt.setType(SQLCreateTableStatement.Type.TEMPORARY);
    }
    accept(Token.TABLE);
    if (lexer.token() == Token.IF || lexer.identifierEquals(FnvHash.Constants.IF)) {
        lexer.nextToken();
        accept(Token.NOT);
        accept(Token.EXISTS);
        stmt.setIfNotExiists(true);
    }
    stmt.setName(this.exprParser.name());
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        for (; ; ) {
            Token token = lexer.token();
            if (// 
            token == Token.IDENTIFIER || token == Token.LITERAL_ALIAS) {
                SQLColumnDefinition column = this.exprParser.parseColumn();
                stmt.getTableElementList().add(column);
            } else if (// 
            token == Token.PRIMARY || // 
            token == Token.UNIQUE || // 
            token == Token.CHECK || token == Token.CONSTRAINT || token == Token.FOREIGN) {
                SQLConstraint constraint = this.exprParser.parseConstaint();
                constraint.setParent(stmt);
                stmt.getTableElementList().add((SQLTableElement) constraint);
            } else if (token == Token.TABLESPACE) {
                throw new ParserException("TODO " + lexer.info());
            } else {
                SQLColumnDefinition column = this.exprParser.parseColumn();
                stmt.getTableElementList().add(column);
            }
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                if (lexer.token() == Token.RPAREN) {
                    // compatible for sql server
                    break;
                }
                continue;
            }
            break;
        }
        accept(Token.RPAREN);
    }
    // add using
    if (lexer.token() == Token.USING) {
        lexer.nextToken();
        SQLName expr = this.exprParser.name();
        stmt.setDatasource(expr);
    }
    if (lexer.token() == Token.COMMENT) {
        lexer.nextToken();
        SQLExpr comment = this.exprParser.expr();
        stmt.setComment(comment);
    }
    if (lexer.identifierEquals(FnvHash.Constants.MAPPED)) {
        lexer.nextToken();
        accept(Token.BY);
        this.exprParser.parseAssignItem(stmt.getMappedBy(), stmt);
    }
    if (lexer.token() == Token.PARTITIONED) {
        lexer.nextToken();
        accept(Token.BY);
        accept(Token.LPAREN);
        for (; ; ) {
            if (lexer.token() != Token.IDENTIFIER) {
                throw new ParserException("expect identifier. " + lexer.info());
            }
            SQLColumnDefinition column = this.exprParser.parseColumn();
            stmt.addPartitionColumn(column);
            if (lexer.isKeepComments() && lexer.hasComment()) {
                column.addAfterComment(lexer.readAndResetComments());
            }
            if (lexer.token() != Token.COMMA) {
                break;
            } else {
                lexer.nextToken();
                if (lexer.isKeepComments() && lexer.hasComment()) {
                    column.addAfterComment(lexer.readAndResetComments());
                }
            }
        }
        accept(Token.RPAREN);
    }
    if (lexer.identifierEquals(FnvHash.Constants.CLUSTERED)) {
        lexer.nextToken();
        accept(Token.BY);
        accept(Token.LPAREN);
        for (; ; ) {
            SQLSelectOrderByItem item = this.exprParser.parseSelectOrderByItem();
            stmt.addClusteredByItem(item);
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }
        accept(Token.RPAREN);
    }
    if (lexer.identifierEquals(FnvHash.Constants.SKEWED)) {
        lexer.nextToken();
        accept(Token.BY);
        accept(Token.LPAREN);
        this.exprParser.exprList(stmt.getSkewedBy(), stmt);
        accept(Token.RPAREN);
        accept(Token.ON);
        accept(Token.LPAREN);
        for (; ; ) {
            if (lexer.token() == Token.LPAREN) {
                SQLListExpr list = new SQLListExpr();
                lexer.nextToken();
                this.exprParser.exprList(list.getItems(), list);
                accept(Token.RPAREN);
                stmt.addSkewedByOn(list);
            } else {
                SQLExpr expr = this.exprParser.expr();
                stmt.addSkewedByOn(expr);
            }
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            }
            break;
        }
        accept(Token.RPAREN);
    }
    if (lexer.identifierEquals(FnvHash.Constants.SORTED)) {
        parseSortedBy(stmt);
    }
    if (lexer.token() == Token.ROW || lexer.identifierEquals(FnvHash.Constants.ROW)) {
        parseRowFormat(stmt);
    }
    if (lexer.identifierEquals(FnvHash.Constants.SORTED)) {
        parseSortedBy(stmt);
    }
    if (stmt.getClusteredBy().size() > 0 || stmt.getSortedBy().size() > 0) {
        accept(Token.INTO);
        if (lexer.token() == Token.LITERAL_INT) {
            stmt.setBuckets(lexer.integerValue().intValue());
            lexer.nextToken();
        } else {
            throw new ParserException("into buckets must be integer. " + lexer.info());
        }
        acceptIdentifier("BUCKETS");
    }
    if (lexer.token() == Token.ROW || lexer.identifierEquals(FnvHash.Constants.ROW)) {
        parseRowFormat(stmt);
    }
    if (lexer.identifierEquals(FnvHash.Constants.STORED)) {
        lexer.nextToken();
        accept(Token.AS);
        if (lexer.identifierEquals(FnvHash.Constants.INPUTFORMAT)) {
            HiveInputOutputFormat format = new HiveInputOutputFormat();
            lexer.nextToken();
            format.setInput(this.exprParser.primary());
            if (lexer.identifierEquals(FnvHash.Constants.OUTPUTFORMAT)) {
                lexer.nextToken();
                format.setOutput(this.exprParser.primary());
            }
            stmt.setStoredAs(format);
        } else {
            SQLName name = this.exprParser.name();
            stmt.setStoredAs(name);
        }
    }
    if (lexer.identifierEquals(FnvHash.Constants.LOCATION)) {
        lexer.nextToken();
        SQLExpr location = this.exprParser.expr();
        stmt.setLocation(location);
    }
    if (lexer.identifierEquals(FnvHash.Constants.TBLPROPERTIES)) {
        lexer.nextToken();
        accept(Token.LPAREN);
        for (; ; ) {
            String name = lexer.stringVal();
            lexer.nextToken();
            accept(Token.EQ);
            SQLExpr value = this.exprParser.primary();
            stmt.addOption(name, value);
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                if (lexer.token() == Token.RPAREN) {
                    break;
                }
                continue;
            }
            break;
        }
        accept(Token.RPAREN);
    }
    if (lexer.identifierEquals(FnvHash.Constants.META)) {
        lexer.nextToken();
        acceptIdentifier("LIFECYCLE");
        stmt.setMetaLifeCycle(this.exprParser.primary());
    }
    if (lexer.token() == Token.AS) {
        lexer.nextToken();
        SQLSelect select = this.createSQLSelectParser().select();
        stmt.setSelect(select);
    }
    if (lexer.token() == Token.LIKE) {
        lexer.nextToken();
        SQLName name = this.exprParser.name();
        stmt.setLike(name);
    }
    if (lexer.token() == Token.COMMENT) {
        lexer.nextToken();
        SQLExpr comment = this.exprParser.expr();
        stmt.setComment(comment);
    }
    return stmt;
}
Also used : ParserException(com.alibaba.druid.sql.parser.ParserException) SQLConstraint(com.alibaba.druid.sql.ast.statement.SQLConstraint) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLName(com.alibaba.druid.sql.ast.SQLName) Token(com.alibaba.druid.sql.parser.Token) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) HiveInputOutputFormat(com.alibaba.druid.sql.dialect.hive.ast.HiveInputOutputFormat) AntsparkCreateTableStatement(com.alibaba.druid.sql.dialect.antspark.ast.AntsparkCreateTableStatement) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLListExpr(com.alibaba.druid.sql.ast.expr.SQLListExpr)

Example 32 with SQLSelectOrderByItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem in project druid by alibaba.

the class AntsparkOutputVisitor method visit.

// add using statment
@Override
public boolean visit(AntsparkCreateTableStatement x) {
    print0(ucase ? "CREATE " : "create ");
    if (x.isExternal()) {
        print0(ucase ? "EXTERNAL " : "external ");
    }
    if (x.isIfNotExists()) {
        print0(ucase ? "TABLE IF NOT EXISTS " : "table if not exists ");
    } else {
        print0(ucase ? "TABLE " : "table ");
    }
    x.getName().accept(this);
    if (x.getLike() != null) {
        print0(ucase ? " LIKE " : " like ");
        x.getLike().accept(this);
    }
    final List<SQLTableElement> tableElementList = x.getTableElementList();
    int size = tableElementList.size();
    if (size > 0) {
        print0(" (");
        if (this.isPrettyFormat() && x.hasBodyBeforeComment()) {
            print(' ');
            printlnComment(x.getBodyBeforeCommentsDirect());
        }
        this.indentCount++;
        println();
        for (int i = 0; i < size; ++i) {
            SQLTableElement element = tableElementList.get(i);
            element.accept(this);
            if (i != size - 1) {
                print(',');
            }
            if (this.isPrettyFormat() && element.hasAfterComment()) {
                print(' ');
                printlnComment(element.getAfterCommentsDirect());
            }
            if (i != size - 1) {
                println();
            }
        }
        this.indentCount--;
        println();
        print(')');
    }
    if (x.getDatasource() != null) {
        println();
        print0(ucase ? "USING " : "using ");
        print0(x.getDatasource().toString());
    }
    if (x.getComment() != null) {
        println();
        print0(ucase ? "COMMENT " : "comment ");
        x.getComment().accept(this);
    }
    int partitionSize = x.getPartitionColumns().size();
    if (partitionSize > 0) {
        println();
        print0(ucase ? "PARTITIONED BY (" : "partitioned by (");
        this.indentCount++;
        println();
        for (int i = 0; i < partitionSize; ++i) {
            SQLColumnDefinition column = x.getPartitionColumns().get(i);
            column.accept(this);
            if (i != partitionSize - 1) {
                print(',');
            }
            if (this.isPrettyFormat() && column.hasAfterComment()) {
                print(' ');
                printlnComment(column.getAfterCommentsDirect());
            }
            if (i != partitionSize - 1) {
                println();
            }
        }
        this.indentCount--;
        println();
        print(')');
    }
    List<SQLSelectOrderByItem> clusteredBy = x.getClusteredBy();
    if (clusteredBy.size() > 0) {
        println();
        print0(ucase ? "CLUSTERED BY (" : "clustered by (");
        printAndAccept(clusteredBy, ",");
        print(')');
    }
    List<SQLSelectOrderByItem> sortedBy = x.getSortedBy();
    if (sortedBy.size() > 0) {
        println();
        print0(ucase ? "SORTED BY (" : "sorted by (");
        printAndAccept(sortedBy, ", ");
        print(')');
    }
    int buckets = x.getBuckets();
    if (buckets > 0) {
        println();
        print0(ucase ? "INTO " : "into ");
        print(buckets);
        print0(ucase ? " BUCKETS" : " buckets");
    }
    SQLExpr storedAs = x.getStoredAs();
    if (storedAs != null) {
        println();
        print0(ucase ? "STORED AS " : "stored as ");
        storedAs.accept(this);
    }
    SQLSelect select = x.getSelect();
    if (select != null) {
        println();
        print0(ucase ? "AS" : "as");
        println();
        select.accept(this);
    }
    Map<String, SQLObject> serdeProperties = x.getSerdeProperties();
    if (serdeProperties.size() > 0) {
        println();
        print0(ucase ? "TBLPROPERTIES (" : "tblproperties (");
        String seperator = "";
        for (Entry<String, SQLObject> entry : serdeProperties.entrySet()) {
            print0("'" + entry.getKey() + "'='");
            entry.getValue().accept(this);
            print0("'" + seperator);
            seperator = ",";
        }
        print(')');
    }
    SQLExpr location = x.getLocation();
    if (location != null) {
        println();
        print0(ucase ? "LOCATION " : "location ");
        location.accept(this);
    }
    return false;
}
Also used : SQLObject(com.alibaba.druid.sql.ast.SQLObject) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 33 with SQLSelectOrderByItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem in project druid by alibaba.

the class MySqlSelectQueryBlock method accept0.

@Override
public void accept0(MySqlASTVisitor visitor) {
    if (visitor.visit(this)) {
        for (int i = 0; i < this.selectList.size(); i++) {
            SQLSelectItem item = this.selectList.get(i);
            if (item != null) {
                item.accept(visitor);
            }
        }
        if (this.from != null) {
            this.from.accept(visitor);
        }
        if (this.into != null) {
            this.into.accept(visitor);
        }
        if (this.where != null) {
            this.where.accept(visitor);
        }
        if (this.startWith != null) {
            this.startWith.accept(visitor);
        }
        if (this.connectBy != null) {
            this.connectBy.accept(visitor);
        }
        if (this.groupBy != null) {
            this.groupBy.accept(visitor);
        }
        if (this.windows != null) {
            for (SQLWindow item : windows) {
                item.accept(visitor);
            }
        }
        if (this.orderBy != null) {
            this.orderBy.accept(visitor);
        }
        if (this.distributeBy != null) {
            for (int i = 0; i < distributeBy.size(); i++) {
                SQLSelectOrderByItem item = distributeBy.get(i);
                item.accept(visitor);
            }
        }
        if (this.sortBy != null) {
            for (int i = 0; i < sortBy.size(); i++) {
                SQLSelectOrderByItem item = sortBy.get(i);
                item.accept(visitor);
            }
        }
        if (this.waitTime != null) {
            this.waitTime.accept(visitor);
        }
        if (this.limit != null) {
            this.limit.accept(visitor);
        }
        if (this.procedureName != null) {
            this.procedureName.accept(visitor);
        }
        if (this.procedureArgumentList != null) {
            for (SQLExpr item : procedureArgumentList) {
                item.accept(visitor);
            }
        }
    }
    visitor.endVisit(this);
}
Also used : SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLWindow(com.alibaba.druid.sql.ast.SQLWindow) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLCommentHint(com.alibaba.druid.sql.ast.SQLCommentHint) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 34 with SQLSelectOrderByItem

use of com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem in project druid by alibaba.

the class OrderByResolve method visit.

public boolean visit(SQLSelect x) {
    SQLSelectQueryBlock queryBlock = x.getQueryBlock();
    if (queryBlock == null) {
        return super.visit(x);
    }
    if (x.getOrderBy() != null && queryBlock.isForUpdate() && queryBlock.getOrderBy() == null) {
        queryBlock.setOrderBy(x.getOrderBy());
        x.setOrderBy(null);
    }
    SQLOrderBy orderBy = queryBlock.getOrderBy();
    if (orderBy == null) {
        return super.visit(x);
    }
    if (!queryBlock.selectItemHasAllColumn(false)) {
        List<SQLSelectOrderByItem> notContainsOrderBy = new ArrayList<SQLSelectOrderByItem>();
        for (SQLSelectOrderByItem orderByItem : orderBy.getItems()) {
            SQLExpr orderByExpr = orderByItem.getExpr();
            if (orderByExpr instanceof SQLName) {
                if (((SQLName) orderByExpr).hashCode64() == DBMS_RANDOM_VALUE) {
                    continue;
                }
                long hashCode64 = ((SQLName) orderByExpr).nameHashCode64();
                SQLSelectItem selectItem = queryBlock.findSelectItem(hashCode64);
                if (selectItem == null) {
                    queryBlock.addSelectItem(orderByExpr.clone());
                }
            }
        }
        if (notContainsOrderBy.size() > 0) {
            for (SQLSelectOrderByItem orderByItem : notContainsOrderBy) {
                queryBlock.addSelectItem(orderByItem.getExpr());
            }
            OracleSelectQueryBlock queryBlock1 = new OracleSelectQueryBlock();
            queryBlock1.setFrom(queryBlock, "x");
            x.setQuery(queryBlock1);
        }
    }
    return super.visit(x);
}
Also used : SQLOrderBy(com.alibaba.druid.sql.ast.SQLOrderBy) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) OracleSelectQueryBlock(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSelectQueryBlock) ArrayList(java.util.ArrayList) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLSelectOrderByItem(com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

SQLSelectOrderByItem (com.alibaba.druid.sql.ast.statement.SQLSelectOrderByItem)34 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)11 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)5 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)4 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)3 SQLListExpr (com.alibaba.druid.sql.ast.expr.SQLListExpr)3 SQLNullExpr (com.alibaba.druid.sql.ast.expr.SQLNullExpr)3 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)3 SQLConstraint (com.alibaba.druid.sql.ast.statement.SQLConstraint)3 SQLSelectGroupByClause (com.alibaba.druid.sql.ast.statement.SQLSelectGroupByClause)3 Order (com.actiontech.dble.plan.Order)2 SQLExprImpl (com.alibaba.druid.sql.ast.SQLExprImpl)2 SQLName (com.alibaba.druid.sql.ast.SQLName)2 SQLObject (com.alibaba.druid.sql.ast.SQLObject)2 SQLOrderingSpecification (com.alibaba.druid.sql.ast.SQLOrderingSpecification)2 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)2 SQLInSubQueryExpr (com.alibaba.druid.sql.ast.expr.SQLInSubQueryExpr)2 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)2 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)2 SQLTableElement (com.alibaba.druid.sql.ast.statement.SQLTableElement)2