mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved hive sql parser. for issue #2180
This commit is contained in:
@@ -263,6 +263,8 @@ public class SchemaRepository {
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.DB2ResolveVisitor(this, optionsValue);
|
||||
} else if (JdbcConstants.ODPS.equals(dbType)) {
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.OdpsResolveVisitor(this, optionsValue);
|
||||
} else if (JdbcConstants.HIVE.equals(dbType)) {
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.HiveResolveVisitor(this, optionsValue);
|
||||
} else if (JdbcConstants.POSTGRESQL.equals(dbType)) {
|
||||
resolveVisitor = new SchemaResolveVisitorFactory.PGResolveVisitor(this, optionsValue);
|
||||
} else if (JdbcConstants.SQL_SERVER.equals(dbType)) {
|
||||
|
||||
@@ -22,6 +22,8 @@ import com.alibaba.druid.sql.ast.statement.*;
|
||||
import com.alibaba.druid.sql.dialect.db2.ast.DB2Object;
|
||||
import com.alibaba.druid.sql.dialect.db2.ast.stmt.DB2SelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.db2.visitor.DB2ASTVisitorAdapter;
|
||||
import com.alibaba.druid.sql.dialect.hive.ast.HiveInsert;
|
||||
import com.alibaba.druid.sql.dialect.hive.visitor.HiveASTVisitorAdapter;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCursorDeclareStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlDeclareStatement;
|
||||
@@ -764,7 +766,190 @@ class SchemaResolveVisitorFactory {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(OdpsInsert x) {
|
||||
public boolean visit(HiveInsert x) {
|
||||
Context ctx = createContext(x);
|
||||
|
||||
SQLExprTableSource tableSource = x.getTableSource();
|
||||
if (tableSource != null) {
|
||||
ctx.setTableSource(x.getTableSource());
|
||||
visit(tableSource);
|
||||
}
|
||||
|
||||
for (SQLAssignItem item : x.getPartitions()) {
|
||||
item.accept(this);
|
||||
}
|
||||
|
||||
SQLSelect select = x.getQuery();
|
||||
if (select != null) {
|
||||
visit(select);
|
||||
}
|
||||
|
||||
popContext();
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLInsertStatement x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLAlterTableStatement x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLMergeStatement x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLCreateProcedureStatement x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLBlockStatement x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLParameter x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLDeclareItem x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLOver x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled(Option option) {
|
||||
return (options & option.mask) != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Context getContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
public Context createContext(SQLObject object) {
|
||||
return this.context = new Context(object, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void popContext() {
|
||||
if (context != null) {
|
||||
context = context.parent;
|
||||
}
|
||||
}
|
||||
|
||||
public SchemaRepository getRepository() {
|
||||
return repository;
|
||||
}
|
||||
}
|
||||
|
||||
static class HiveResolveVisitor extends HiveASTVisitorAdapter implements SchemaResolveVisitor {
|
||||
private int options;
|
||||
private SchemaRepository repository;
|
||||
private Context context;
|
||||
|
||||
public HiveResolveVisitor(SchemaRepository repository, int options) {
|
||||
this.repository = repository;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
public boolean visit(SQLForeignKeyImpl x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectStatement x) {
|
||||
resolve(this, x.getSelect());
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLExprTableSource x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectQueryBlock x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(OdpsSelectQueryBlock x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelectItem x) {
|
||||
SQLExpr expr = x.getExpr();
|
||||
if (expr instanceof SQLIdentifierExpr) {
|
||||
resolve(this, (SQLIdentifierExpr) expr);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (expr instanceof SQLPropertyExpr) {
|
||||
resolve(this, (SQLPropertyExpr) expr);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(SQLIdentifierExpr x) {
|
||||
resolve(this, x);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean visit(SQLPropertyExpr x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLAllColumnExpr x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLCreateTableStatement x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(OdpsCreateTableStatement x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLUpdateStatement x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLDeleteStatement x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLSelect x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(SQLWithSubqueryClause x) {
|
||||
resolve(this, x);
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean visit(HiveInsert x) {
|
||||
Context ctx = createContext(x);
|
||||
|
||||
SQLExprTableSource tableSource = x.getTableSource();
|
||||
@@ -2046,6 +2231,12 @@ class SchemaResolveVisitorFactory {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (from instanceof SQLJoinTableSource) {
|
||||
SQLJoinTableSource join = (SQLJoinTableSource) from;
|
||||
extractColumns(visitor, join.getLeft(), columns);
|
||||
extractColumns(visitor, join.getRight(), columns);
|
||||
}
|
||||
}
|
||||
|
||||
static void resolve(SchemaResolveVisitor visitor, SQLAllColumnExpr x) {
|
||||
|
||||
Reference in New Issue
Block a user