fix : HiveInsertStatement accept miss with

This commit is contained in:
shaojin.wensj
2022-09-15 14:10:51 +08:00
parent 80d1f38e3e
commit 3ad7921883
3 changed files with 85 additions and 0 deletions
@@ -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"));
}
}