mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
fix : HiveInsertStatement accept miss with
This commit is contained in:
@@ -898,6 +898,60 @@ public class SQLUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static void acceptTableSource(String sql, DbType dbType, Consumer<SQLTableSource> consumer, Predicate<SQLTableSource> filter) {
|
||||
if (sql == null || sql.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<SQLStatement> stmtList = new ArrayList<>();
|
||||
|
||||
try {
|
||||
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType, SQLParserFeature.EnableMultiUnion, SQLParserFeature.KeepComments, SQLParserFeature.EnableSQLBinaryOpExprGroup);
|
||||
parser.parseStatementList(stmtList, -1, null);
|
||||
} catch (Exception ignored) {
|
||||
return;
|
||||
}
|
||||
|
||||
SQLASTVisitor visitor;
|
||||
switch (dbType) {
|
||||
case odps:
|
||||
visitor = new OdpsASTVisitorAdapter() {
|
||||
@Override
|
||||
public boolean visit(SQLExprTableSource x) {
|
||||
if (filter == null || filter.test(x)) {
|
||||
consumer.accept(x);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
break;
|
||||
case mysql:
|
||||
visitor = new MySqlASTVisitorAdapter() {
|
||||
public boolean visit(SQLExprTableSource x) {
|
||||
if (filter == null || filter.test(x)) {
|
||||
consumer.accept(x);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
break;
|
||||
default:
|
||||
visitor = new SQLASTVisitorAdapter() {
|
||||
public boolean visit(SQLExprTableSource x) {
|
||||
if (filter == null || filter.test(x)) {
|
||||
consumer.accept(x);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
break;
|
||||
}
|
||||
|
||||
for (SQLStatement stmt : stmtList) {
|
||||
stmt.accept(visitor);
|
||||
}
|
||||
}
|
||||
|
||||
public static void acceptSelectQueryBlock(String sql, DbType dbType, Consumer<SQLSelectQueryBlock> consumer, Predicate<SQLSelectQueryBlock> filter) {
|
||||
if (sql == null || sql.isEmpty()) {
|
||||
return;
|
||||
|
||||
@@ -39,6 +39,7 @@ public class HiveInsertStatement extends SQLInsertStatement implements SQLStatem
|
||||
|
||||
protected void accept0(HiveASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, with);
|
||||
acceptChild(visitor, tableSource);
|
||||
acceptChild(visitor, partitions);
|
||||
acceptChild(visitor, valuesList);
|
||||
@@ -49,6 +50,7 @@ public class HiveInsertStatement extends SQLInsertStatement implements SQLStatem
|
||||
|
||||
protected void accept0(OdpsASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, with);
|
||||
acceptChild(visitor, tableSource);
|
||||
acceptChild(visitor, partitions);
|
||||
acceptChild(visitor, valuesList);
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.alibaba.druid.bvt.sql.odps.issues;
|
||||
|
||||
import com.alibaba.druid.DbType;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Issue4933 extends TestCase {
|
||||
public void testInsert() {
|
||||
String sql = "with \n" +
|
||||
" a as (select * from src where key is not null),\n" +
|
||||
" b as (select * from src2 where value>0),\n" +
|
||||
" c as (select * from src3 where value>0),\n" +
|
||||
" d as (select a.key,b.value from a join b on a.key=b.key ),\n" +
|
||||
" e as (select a.key,c.value from a left outer join c on a.key=c.key and c.key is not null )\n" +
|
||||
" insert overwrite table x select * from y;";
|
||||
List<String> tables = new ArrayList<>();
|
||||
SQLUtils.acceptTableSource(
|
||||
sql,
|
||||
DbType.odps,
|
||||
e -> tables.add(((SQLExprTableSource)e).getTableName()),
|
||||
e -> e instanceof SQLExprTableSource
|
||||
);
|
||||
assertTrue(tables.contains("src"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user