Search in sources :

Example 1 with NodeParser

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;
}
Also used : Table(com.dtstack.taier.develop.sql.Table) OtherNodeParser(com.dtstack.taier.develop.sql.hive.node.OtherNodeParser) NodeParser(com.dtstack.taier.develop.sql.hive.node.NodeParser) LineageParser(com.dtstack.taier.develop.sql.calcite.LineageParser) ParseException(org.apache.hadoop.hive.ql.parse.ParseException) ColumnLineage(com.dtstack.taier.develop.sql.ColumnLineage) OtherNodeParser(com.dtstack.taier.develop.sql.hive.node.OtherNodeParser) HashSet(java.util.HashSet)

Example 2 with NodeParser

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;
}
Also used : Table(com.dtstack.taier.develop.sql.Table) ParseResult(com.dtstack.taier.develop.sql.ParseResult) OtherNodeParser(com.dtstack.taier.develop.sql.hive.node.OtherNodeParser) NodeParser(com.dtstack.taier.develop.sql.hive.node.NodeParser) LineageParser(com.dtstack.taier.develop.sql.calcite.LineageParser) TableLineage(com.dtstack.taier.develop.sql.TableLineage) ASTNode(org.apache.hadoop.hive.ql.parse.ASTNode) ParseException(org.apache.hadoop.hive.ql.parse.ParseException)

Example 3 with NodeParser

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);
}
Also used : Table(com.dtstack.taier.develop.sql.Table) OtherNodeParser(com.dtstack.taier.develop.sql.hive.node.OtherNodeParser) NodeParser(com.dtstack.taier.develop.sql.hive.node.NodeParser) ArrayList(java.util.ArrayList) ASTNode(org.apache.hadoop.hive.ql.parse.ASTNode) ParseException(org.apache.hadoop.hive.ql.parse.ParseException) HashSet(java.util.HashSet)

Aggregations

Table (com.dtstack.taier.develop.sql.Table)3 NodeParser (com.dtstack.taier.develop.sql.hive.node.NodeParser)3 OtherNodeParser (com.dtstack.taier.develop.sql.hive.node.OtherNodeParser)3 ParseException (org.apache.hadoop.hive.ql.parse.ParseException)3 LineageParser (com.dtstack.taier.develop.sql.calcite.LineageParser)2 HashSet (java.util.HashSet)2 ASTNode (org.apache.hadoop.hive.ql.parse.ASTNode)2 ColumnLineage (com.dtstack.taier.develop.sql.ColumnLineage)1 ParseResult (com.dtstack.taier.develop.sql.ParseResult)1 TableLineage (com.dtstack.taier.develop.sql.TableLineage)1 ArrayList (java.util.ArrayList)1