Search in sources :

Example 1 with HiveInsert

use of com.alibaba.druid.sql.dialect.hive.ast.HiveInsert in project druid by alibaba.

the class HiveStatementParser method parseInsert.

public SQLStatement parseInsert() {
    if (lexer.token() == Token.FROM) {
        lexer.nextToken();
        HiveMultiInsertStatement stmt = new HiveMultiInsertStatement();
        if (lexer.token() == Token.IDENTIFIER) {
            SQLName tableName = this.exprParser.name();
            SQLExprTableSource from = new SQLExprTableSource(tableName);
            SQLTableSource tableSource = createSQLSelectParser().parseTableSourceRest(from);
            stmt.setFrom(tableSource);
            if (lexer.token() == Token.IDENTIFIER) {
                from.setAlias(lexer.stringVal());
                lexer.nextToken();
            }
        } else {
            accept(Token.LPAREN);
            SQLSelectParser selectParser = createSQLSelectParser();
            SQLSelect select = selectParser.select();
            accept(Token.RPAREN);
            String alias = lexer.stringVal();
            accept(Token.IDENTIFIER);
            SQLTableSource from = new SQLSubqueryTableSource(select, alias);
            switch(lexer.token()) {
                case LEFT:
                case RIGHT:
                case FULL:
                case JOIN:
                    from = selectParser.parseTableSourceRest(from);
                    break;
                default:
                    break;
            }
            stmt.setFrom(from);
        }
        for (; ; ) {
            HiveInsert insert = parseHiveInsert();
            stmt.addItem(insert);
            if (lexer.token() != Token.INSERT) {
                break;
            }
        }
        return stmt;
    }
    return parseHiveInsertStmt();
}
Also used : HiveInsert(com.alibaba.druid.sql.dialect.hive.ast.HiveInsert) HiveMultiInsertStatement(com.alibaba.druid.sql.dialect.hive.ast.HiveMultiInsertStatement)

Example 2 with HiveInsert

use of com.alibaba.druid.sql.dialect.hive.ast.HiveInsert in project druid by alibaba.

the class HiveOutputVisitor method visit.

@Override
public boolean visit(HiveMultiInsertStatement x) {
    SQLWithSubqueryClause with = x.getWith();
    if (with != null) {
        visit(with);
        println();
    }
    SQLTableSource from = x.getFrom();
    if (x.getFrom() != null) {
        if (from instanceof SQLSubqueryTableSource) {
            SQLSelect select = ((SQLSubqueryTableSource) from).getSelect();
            print0(ucase ? "FROM (" : "from (");
            this.indentCount++;
            println();
            select.accept(this);
            this.indentCount--;
            println();
            print0(") ");
            String alias = x.getFrom().getAlias();
            if (alias != null) {
                print0(alias);
            }
        } else {
            print0(ucase ? "FROM " : "from ");
            from.accept(this);
        }
        println();
    }
    for (int i = 0; i < x.getItems().size(); ++i) {
        HiveInsert insert = x.getItems().get(i);
        if (i != 0) {
            println();
        }
        insert.accept(this);
    }
    return false;
}
Also used : HiveInsert(com.alibaba.druid.sql.dialect.hive.ast.HiveInsert)

Example 3 with HiveInsert

use of com.alibaba.druid.sql.dialect.hive.ast.HiveInsert in project druid by alibaba.

the class SQLStatementParser method parseHiveInsert.

protected HiveInsert parseHiveInsert() {
    HiveInsert insert = new HiveInsert();
    if (lexer.isKeepComments() && lexer.hasComment()) {
        insert.addBeforeComment(lexer.readAndResetComments());
    }
    SQLSelectParser selectParser = createSQLSelectParser();
    accept(Token.INSERT);
    if (lexer.token == Token.INTO) {
        lexer.nextToken();
    } else {
        accept(Token.OVERWRITE);
        insert.setOverwrite(true);
    }
    if (lexer.token == Token.TABLE) {
        lexer.nextToken();
    }
    insert.setTableSource(this.exprParser.name());
    if (lexer.token == Token.PARTITION) {
        lexer.nextToken();
        accept(Token.LPAREN);
        for (; ; ) {
            SQLAssignItem ptExpr = new SQLAssignItem();
            ptExpr.setTarget(this.exprParser.name());
            if (lexer.token == Token.EQ) {
                lexer.nextToken();
                SQLExpr ptValue = this.exprParser.expr();
                ptExpr.setValue(ptValue);
            }
            insert.addPartition(ptExpr);
            if (lexer.token != Token.COMMA) {
                break;
            } else {
                lexer.nextToken();
            }
        }
        accept(Token.RPAREN);
    }
    if (lexer.token == LPAREN) {
        lexer.nextToken();
        this.exprParser.exprList(insert.getColumns(), insert);
        accept(RPAREN);
    }
    if (lexer.token == Token.VALUES) {
        lexer.nextToken();
        for (; ; ) {
            if (lexer.token == Token.LPAREN) {
                lexer.nextToken();
                SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause();
                this.exprParser.exprList(values.getValues(), values);
                insert.addValueCause(values);
                accept(Token.RPAREN);
            }
            if (lexer.token == Token.COMMA) {
                lexer.nextToken();
                continue;
            } else {
                break;
            }
        }
    } else {
        SQLSelect query = selectParser.select();
        insert.setQuery(query);
    }
    return insert;
}
Also used : HiveInsert(com.alibaba.druid.sql.dialect.hive.ast.HiveInsert)

Example 4 with HiveInsert

use of com.alibaba.druid.sql.dialect.hive.ast.HiveInsert in project druid by alibaba.

the class OdpsStatementParser method parseInsert.

public SQLStatement parseInsert() {
    if (lexer.token() == Token.FROM) {
        lexer.nextToken();
        HiveMultiInsertStatement stmt = new HiveMultiInsertStatement();
        if (lexer.token() == Token.IDENTIFIER || lexer.token() == Token.VARIANT) {
            Lexer.SavePoint mark = lexer.mark();
            SQLExpr tableName = this.exprParser.name();
            if (lexer.token() == Token.LPAREN) {
                lexer.reset(mark);
                tableName = this.exprParser.primary();
            }
            SQLTableSource from = new SQLExprTableSource(tableName);
            if (lexer.token() == Token.IDENTIFIER) {
                String alias = alias();
                from.setAlias(alias);
            }
            SQLSelectParser selectParser = createSQLSelectParser();
            from = selectParser.parseTableSourceRest(from);
            if (lexer.token() == Token.WHERE) {
                lexer.nextToken();
                SQLExpr where = this.exprParser.expr();
                SQLSelectQueryBlock queryBlock = new SQLSelectQueryBlock();
                queryBlock.addSelectItem(new SQLAllColumnExpr());
                queryBlock.setFrom(from);
                queryBlock.setWhere(where);
                if (lexer.token() == Token.GROUP) {
                    selectParser.parseGroupBy(queryBlock);
                }
                stmt.setFrom(new SQLSubqueryTableSource(queryBlock));
            } else {
                stmt.setFrom(from);
            }
        } else {
            SQLCommentHint hint = null;
            if (lexer.token() == Token.HINT) {
                hint = this.exprParser.parseHint();
            }
            accept(Token.LPAREN);
            boolean paren2 = lexer.token() == Token.LPAREN;
            SQLSelectParser selectParser = createSQLSelectParser();
            SQLSelect select = selectParser.select();
            SQLTableSource from = null;
            if (paren2 && lexer.token() != Token.RPAREN) {
                String subQueryAs = null;
                if (lexer.token() == Token.AS) {
                    lexer.nextToken();
                    subQueryAs = tableAlias(true);
                } else {
                    subQueryAs = tableAlias(false);
                }
                SQLSubqueryTableSource subQuery = new SQLSubqueryTableSource(select, subQueryAs);
                from = selectParser.parseTableSourceRest(subQuery);
            }
            accept(Token.RPAREN);
            String alias;
            if (lexer.token() == Token.INSERT) {
                alias = null;
            } else if (lexer.token() == Token.SELECT) {
                // skip
                alias = null;
            } else {
                if (lexer.token() == Token.AS) {
                    lexer.nextToken();
                }
                alias = lexer.stringVal();
                accept(Token.IDENTIFIER);
            }
            if (from == null) {
                from = new SQLSubqueryTableSource(select, alias);
            } else {
                if (alias != null) {
                    from.setAlias(alias);
                }
            }
            SQLTableSource tableSource = selectParser.parseTableSourceRest(from);
            if (hint != null) {
                if (tableSource instanceof SQLJoinTableSource) {
                    ((SQLJoinTableSource) tableSource).setHint(hint);
                }
            }
            stmt.setFrom(tableSource);
        }
        if (lexer.token() == Token.SELECT) {
            SQLSelectParser selectParser = createSQLSelectParser();
            SQLSelect query = selectParser.select();
            HiveInsert insert = new HiveInsert();
            insert.setQuery(query);
            stmt.addItem(insert);
            return stmt;
        }
        for (; ; ) {
            HiveInsert insert = parseHiveInsert();
            stmt.addItem(insert);
            if (lexer.token() != Token.INSERT) {
                break;
            }
        }
        return stmt;
    }
    return parseHiveInsertStmt();
}
Also used : HiveInsert(com.alibaba.druid.sql.dialect.hive.ast.HiveInsert) HiveMultiInsertStatement(com.alibaba.druid.sql.dialect.hive.ast.HiveMultiInsertStatement) SQLAllColumnExpr(com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)

Example 5 with HiveInsert

use of com.alibaba.druid.sql.dialect.hive.ast.HiveInsert in project druid by alibaba.

the class SchemaStatVisitor method visit.

public boolean visit(SQLSelectQueryBlock x) {
    SQLTableSource from = x.getFrom();
    setMode(x, Mode.Select);
    boolean isHiveMultiInsert = false;
    if (from == null) {
        isHiveMultiInsert = x.getParent() != null && x.getParent().getParent() instanceof HiveInsert && x.getParent().getParent().getParent() instanceof HiveMultiInsertStatement;
        if (isHiveMultiInsert) {
            from = ((HiveMultiInsertStatement) x.getParent().getParent().getParent()).getFrom();
        }
    }
    if (from == null) {
        for (SQLSelectItem selectItem : x.getSelectList()) {
            statExpr(selectItem.getExpr());
        }
        return false;
    }
    if (from != null) {
        // 提前执行,获得aliasMap
        from.accept(this);
    }
    SQLExprTableSource into = x.getInto();
    if (into != null && into.getExpr() instanceof SQLName) {
        SQLName intoExpr = (SQLName) into.getExpr();
        boolean isParam = intoExpr instanceof SQLIdentifierExpr && isParam((SQLIdentifierExpr) intoExpr);
        if (!isParam) {
            TableStat stat = getTableStat(intoExpr);
            if (stat != null) {
                stat.incrementInsertCount();
            }
        }
        into.accept(this);
    }
    for (SQLSelectItem selectItem : x.getSelectList()) {
        if (selectItem.getClass() == SQLSelectItem.class) {
            statExpr(selectItem.getExpr());
        } else {
            selectItem.accept(this);
        }
    }
    SQLExpr where = x.getWhere();
    if (where != null) {
        statExpr(where);
    }
    SQLExpr startWith = x.getStartWith();
    if (startWith != null) {
        statExpr(startWith);
    }
    SQLExpr connectBy = x.getConnectBy();
    if (connectBy != null) {
        statExpr(connectBy);
    }
    SQLSelectGroupByClause groupBy = x.getGroupBy();
    if (groupBy != null) {
        for (SQLExpr expr : groupBy.getItems()) {
            statExpr(expr);
        }
    }
    List<SQLWindow> windows = x.getWindows();
    if (windows != null && windows.size() > 0) {
        for (SQLWindow window : windows) {
            window.accept(this);
        }
    }
    SQLOrderBy orderBy = x.getOrderBy();
    if (orderBy != null) {
        this.visit(orderBy);
    }
    SQLExpr first = x.getFirst();
    if (first != null) {
        statExpr(first);
    }
    List<SQLSelectOrderByItem> distributeBy = x.getDistributeBy();
    if (distributeBy != null) {
        for (SQLSelectOrderByItem item : distributeBy) {
            statExpr(item.getExpr());
        }
    }
    List<SQLSelectOrderByItem> sortBy = x.getSortBy();
    if (sortBy != null) {
        for (SQLSelectOrderByItem orderByItem : sortBy) {
            statExpr(orderByItem.getExpr());
        }
    }
    for (SQLExpr expr : x.getForUpdateOf()) {
        statExpr(expr);
    }
    return false;
}
Also used : HiveInsert(com.alibaba.druid.sql.dialect.hive.ast.HiveInsert) TableStat(com.alibaba.druid.stat.TableStat) HiveMultiInsertStatement(com.alibaba.druid.sql.dialect.hive.ast.HiveMultiInsertStatement)

Aggregations

HiveInsert (com.alibaba.druid.sql.dialect.hive.ast.HiveInsert)5 HiveMultiInsertStatement (com.alibaba.druid.sql.dialect.hive.ast.HiveMultiInsertStatement)3 SQLAllColumnExpr (com.alibaba.druid.sql.ast.expr.SQLAllColumnExpr)1 TableStat (com.alibaba.druid.stat.TableStat)1