Search in sources :

Example 1 with LineageParser

use of com.dtstack.taier.develop.sql.calcite.LineageParser 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 LineageParser

use of com.dtstack.taier.develop.sql.calcite.LineageParser 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)

Aggregations

Table (com.dtstack.taier.develop.sql.Table)2 LineageParser (com.dtstack.taier.develop.sql.calcite.LineageParser)2 NodeParser (com.dtstack.taier.develop.sql.hive.node.NodeParser)2 OtherNodeParser (com.dtstack.taier.develop.sql.hive.node.OtherNodeParser)2 ParseException (org.apache.hadoop.hive.ql.parse.ParseException)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 HashSet (java.util.HashSet)1 ASTNode (org.apache.hadoop.hive.ql.parse.ASTNode)1