Search in sources :

Example 1 with TableLineage

use of com.dtstack.taier.develop.sql.TableLineage 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 2 with TableLineage

use of com.dtstack.taier.develop.sql.TableLineage in project Taier by DTStack.

the class AstNodeParser method getTableLineages.

/**
 * 对表级血缘进行组装
 *
 * @param sqlNode
 * @param lineageParser
 * @return
 */
private List<TableLineage> getTableLineages(com.dtstack.taier.develop.sql.node.Node sqlNode, LineageParser lineageParser) {
    List<TableLineage> resList = Lists.newArrayList();
    List<Pair<Identifier, Identifier>> pairs = lineageParser.parseTableLineage(sqlNode);
    if (CollectionUtils.isNotEmpty(pairs)) {
        for (Pair<Identifier, Identifier> pair : pairs) {
            Identifier key = pair.getKey();
            Identifier value = pair.getValue();
            TableLineage tableLineage = new TableLineage();
            tableLineage.setFromDb(value.getDb());
            tableLineage.setFromTable(value.getTable());
            tableLineage.setToDb(key.getDb());
            tableLineage.setToTable(key.getTable());
            resList.add(tableLineage);
        }
    }
    return resList;
}
Also used : Identifier(com.dtstack.taier.develop.sql.node.Identifier) TableLineage(com.dtstack.taier.develop.sql.TableLineage) Pair(com.dtstack.taier.develop.sql.Pair)

Aggregations

TableLineage (com.dtstack.taier.develop.sql.TableLineage)2 Pair (com.dtstack.taier.develop.sql.Pair)1 ParseResult (com.dtstack.taier.develop.sql.ParseResult)1 Table (com.dtstack.taier.develop.sql.Table)1 LineageParser (com.dtstack.taier.develop.sql.calcite.LineageParser)1 NodeParser (com.dtstack.taier.develop.sql.hive.node.NodeParser)1 OtherNodeParser (com.dtstack.taier.develop.sql.hive.node.OtherNodeParser)1 Identifier (com.dtstack.taier.develop.sql.node.Identifier)1 ASTNode (org.apache.hadoop.hive.ql.parse.ASTNode)1 ParseException (org.apache.hadoop.hive.ql.parse.ParseException)1