use of com.dtstack.taier.develop.sql.hive.node.NodeParser in project Taier by DTStack.
the class AstNodeParser method getParseResult.
/**
* 进行语法树转换 和 血缘解析的具体类
*
* @param originSql
* @param currentDb
* @param tableColumnsMap
* @param parseResult
* @param root
* @return
*/
private ParseResult getParseResult(String originSql, String currentDb, Map<String, List<Column>> tableColumnsMap, ParseResult parseResult, ASTNode root) {
NodeParser nodeParser = ASTNodeLineageParser.ParserProxy.getParser((ASTNode) root.getChild(0), originSql);
if (Objects.isNull(nodeParser)) {
return parseResult;
}
if (nodeParser instanceof OtherNodeParser) {
((OtherNodeParser) nodeParser).getSqlType((ASTNode) root.getChild(0), parseResult);
return parseResult;
}
com.dtstack.taier.develop.sql.node.Node node = nodeParser.parseSql((ASTNode) root.getChild(0), currentDb, tableColumnsMap, new HashMap<>());
if (Objects.isNull(node)) {
return parseResult;
}
LineageParser lineageParser = LineageParser.HiveParserProxy.getParser(node, currentDb, tableColumnsMap);
if (lineageParser != null) {
List<ColumnLineage> columnLineages = getColumnLineages(node, lineageParser);
parseResult.setColumnLineages(columnLineages);
}
getMainTable(node, parseResult, root);
try {
Set<Table> tables = new HashSet<>();
nodeParser.parseSqlTable(node, tables);
parseResult.setTables(new ArrayList<>(tables));
} catch (Exception e) {
e.printStackTrace();
}
return parseResult;
}
use of com.dtstack.taier.develop.sql.hive.node.NodeParser in project Taier by DTStack.
the class AstNodeParser method parseTableLineage.
@Override
public ParseResult parseTableLineage(String originSql, String currentDb) throws Exception {
ParseResult parseResult = new ParseResult();
parseResult.setCurrentDb(currentDb);
parseResult.setOriginSql(originSql);
parseResult.setMainTable(new Table());
ASTNode root;
originSql = SqlFormatUtil.formatSql(originSql);
String standardSql = SqlFormatUtil.getStandardSql(originSql);
SqlFormatUtil.checkSql(standardSql);
parseResult.setStandardSql(standardSql);
// 解析和去掉sql中的生命周期和类目信息
parseLifecycleAndCatalogue(parseResult);
String formattedSql = this.uglySqlHandler.parseUglySql(originSql);
if (StringUtils.isEmpty(formattedSql)) {
return parseResult;
}
parseResult.setStandardSql(formattedSql);
try {
root = parseDriver.parse(parseResult.getStandardSql());
} catch (ParseException e) {
LOG.error("sql解析异常========>>>>:{}", e.getMessage());
// 用正则匹配一下
ParseResult p = SqlTypeRegexUtil.getParseResultByRegexSql(parseResult);
return p;
}
NodeParser nodeParser = ASTNodeLineageParser.ParserProxy.getParser((ASTNode) root.getChild(0), originSql);
com.dtstack.taier.develop.sql.node.Node node = nodeParser.parseSql((ASTNode) root.getChild(0), currentDb, new HashMap<>(), new HashMap<>());
LineageParser lineageParser = LineageParser.HiveParserProxy.getParser(node, currentDb, new HashMap<>());
if (lineageParser != null) {
List<TableLineage> tableLineages = getTableLineages(node, lineageParser);
parseResult.setTableLineages(tableLineages);
}
getMainTable(node, parseResult, root);
return parseResult;
}
use of com.dtstack.taier.develop.sql.hive.node.NodeParser in project Taier by DTStack.
the class AstNodeParser method parseTables.
@Override
public List<Table> parseTables(String currentDb, String sql) throws Exception {
Set<Table> tables = new HashSet<>();
ASTNode root;
sql = SqlFormatUtil.formatSql(sql);
String standardSql = SqlFormatUtil.getStandardSql(sql);
SqlFormatUtil.checkSql(standardSql);
String formattedSql = null;
formattedSql = this.uglySqlHandler.parseUglySql(standardSql);
if (StringUtils.isEmpty(formattedSql)) {
return Lists.newArrayList();
}
try {
root = parseDriver.parse(formattedSql);
} catch (ParseException e) {
LOG.error("sql解析异常========>>>>:{}", e.getMessage());
return new ArrayList<>();
}
if (Objects.isNull(root)) {
return new ArrayList<>();
}
NodeParser nodeParser = ASTNodeLineageParser.ParserProxy.getParser((ASTNode) root.getChild(0), sql);
if (Objects.isNull(nodeParser)) {
return new ArrayList<>();
}
com.dtstack.taier.develop.sql.node.Node node = nodeParser.parseSql((ASTNode) root.getChild(0), currentDb, new HashMap<>(), new HashMap<>());
if (Objects.isNull(node)) {
return new ArrayList<>();
}
nodeParser.parseSqlTable(node, tables);
return new ArrayList<>(tables);
}
Aggregations