Search in sources :

Example 1 with Table

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

the class InsertNodeParser method parseSqlTable.

@Override
public void parseSqlTable(Node node, Set<Table> tables) {
    if (node instanceof InsertNode) {
        Table table = new Table();
        table.setName(((InsertNode) node).getTargetTable().getTable());
        table.setDb(((InsertNode) node).getTargetTable().getDb());
        table.setOperate(TableOperateEnum.INSERT);
        tables.add(table);
        SelectNodeParser selectNodeParser = new SelectNodeParser();
        selectNodeParser.parseSqlTable(((InsertNode) node).getSource(), tables);
    }
}
Also used : Table(com.dtstack.taier.develop.sql.Table)

Example 2 with Table

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

the class LibraUglySqlHandler method parserUglySql.

public ParseResult parserUglySql() {
    ParseResult parseResult = new ParseResult();
    String parsedSql = parseUglySql();
    parseResult.setStandardSql(parsedSql);
    // 针对comment_on特殊处理
    if ((sqlMode & COMMENT_ON_TABLE) == COMMENT_ON_TABLE) {
        parseResult.setSqlType(SqlType.COMMENT_ON);
        Matcher matcher = COMMENT_ON_TABLE_PATTERN.matcher(sql);
        if (matcher.find()) {
            String db = matcher.group("db");
            if (StringUtils.isNotEmpty(db)) {
                parseResult.setCurrentDb(db);
            }
            String table = matcher.group("table");
            if (StringUtils.isNotEmpty(table)) {
                Table table1 = new Table();
                table1.setName(table);
                parseResult.setMainTable(table1);
            }
        }
    } else if ((sqlMode & COMMENT_ON_COLUMN) == COMMENT_ON_COLUMN) {
        parseResult.setSqlType(SqlType.COMMENT_ON);
        Matcher matcher = COMMENT_ON_COLUMN_PATTERN.matcher(sql);
        if (matcher.find()) {
            String columnStr = matcher.group("column");
            String[] split = columnStr.split("\\.");
            if (split.length == 3) {
                parseResult.setCurrentDb(split[0]);
                Table table = new Table();
                table.setName(split[1]);
                parseResult.setMainTable(table);
            } else if (split.length == 2) {
                Table table = new Table();
                table.setName(split[0]);
                parseResult.setMainTable(table);
            }
        }
    }
    return parseResult;
}
Also used : Table(com.dtstack.taier.develop.sql.Table) ParseResult(com.dtstack.taier.develop.sql.ParseResult) Matcher(java.util.regex.Matcher)

Example 3 with Table

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

the class AstNodeParser method parseInvalidateTable.

/**
 * 从invalidate 语句中刷新表信息
 */
private ParseResult parseInvalidateTable(ParseResult result) {
    Map<String, String> dbTable = SqlRegexUtil.parseDbTableFromInvalidateSql(result.getStandardSql());
    Table table = result.getMainTable();
    if (Objects.isNull(table)) {
        table = new Table();
    }
    table.setDb(dbTable.get(SqlRegexUtil.KEY_DB) == null ? result.getCurrentDb() : dbTable.get(SqlRegexUtil.KEY_DB));
    table.setName(dbTable.get(SqlRegexUtil.KEY_TABLE));
    table.setOperate(TableOperateEnum.INVALIDATE_TABLE);
    if (Objects.isNull(result.getTables())) {
        result.setTables(new ArrayList<>());
    }
    result.getTables().add(table);
    result.setMainTable(table);
    result.setSqlType(SqlType.INVALIDATE_TABLE);
    return result;
}
Also used : Table(com.dtstack.taier.develop.sql.Table)

Example 4 with Table

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

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

the class AstNodeParser method parseSql.

/**
 * 解析sql
 */
@Override
public ParseResult parseSql(String originSql, String currentDb, Map<String, List<Column>> tableColumnsMap) throws Exception {
    ParseResult parseResult = new ParseResult();
    parseResult.setCurrentDb(currentDb);
    parseResult.setOriginSql(originSql);
    parseResult.setMainTable(new Table());
    ASTNode root;
    originSql = SqlFormatUtil.formatSql(originSql);
    // 解析和去掉sql中的生命周期和类目信息
    parseLifecycleAndCatalogue(parseResult);
    SqlFormatUtil.checkSql(parseResult.getStandardSql());
    if (isNotParse(originSql, parseResult)) {
        return parseResult;
    }
    if (SqlRegexUtil.isCreateTemp(parseResult.getStandardSql())) {
        parseResult.setStandardSql(SqlRegexUtil.removeTempKey(parseResult.getStandardSql()));
        parseResult.setExtraSqlType(SqlType.CREATE_TEMP);
    }
    String formattedSql = this.uglySqlHandler.parseUglySql(originSql);
    if (StringUtils.isEmpty(formattedSql)) {
        return parseResult;
    }
    try {
        root = parseDriver.parse(formattedSql);
    } catch (ParseException e) {
        LOG.error("sql解析异常========>>>>:{}", e.getMessage());
        // 用正则匹配一下
        ParseResult p = SqlTypeRegexUtil.getParseResultByRegexSql(parseResult);
        return p;
    }
    return getParseResult(originSql, currentDb, tableColumnsMap, parseResult, root);
}
Also used : Table(com.dtstack.taier.develop.sql.Table) ParseResult(com.dtstack.taier.develop.sql.ParseResult) ASTNode(org.apache.hadoop.hive.ql.parse.ASTNode) ParseException(org.apache.hadoop.hive.ql.parse.ParseException)

Aggregations

Table (com.dtstack.taier.develop.sql.Table)14 ASTNode (org.apache.hadoop.hive.ql.parse.ASTNode)6 ParseResult (com.dtstack.taier.develop.sql.ParseResult)4 ParseException (org.apache.hadoop.hive.ql.parse.ParseException)4 NodeParser (com.dtstack.taier.develop.sql.hive.node.NodeParser)3 OtherNodeParser (com.dtstack.taier.develop.sql.hive.node.OtherNodeParser)3 CreateNode (com.dtstack.taier.develop.sql.node.CreateNode)3 LineageParser (com.dtstack.taier.develop.sql.calcite.LineageParser)2 AlterNode (com.dtstack.taier.develop.sql.node.AlterNode)2 DropNode (com.dtstack.taier.develop.sql.node.DropNode)2 InsertNode (com.dtstack.taier.develop.sql.node.InsertNode)2 SelectNode (com.dtstack.taier.develop.sql.node.SelectNode)2 HashSet (java.util.HashSet)2 Matcher (java.util.regex.Matcher)2 ColumnLineage (com.dtstack.taier.develop.sql.ColumnLineage)1 TableLineage (com.dtstack.taier.develop.sql.TableLineage)1 Identifier (com.dtstack.taier.develop.sql.node.Identifier)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Node (org.apache.hadoop.hive.ql.lib.Node)1