Search in sources :

Example 6 with Table

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

the class SelectNodeParser method parseSqlTable.

@Override
public void parseSqlTable(Node node, Set<Table> tables) {
    if (node instanceof Identifier) {
        Table table = new Table();
        table.setName(((Identifier) node).getTable());
        table.setDb(((Identifier) node).getDb());
        table.setOperate(TableOperateEnum.SELECT);
        tables.add(table);
    } else if (node instanceof SelectNode) {
        parseSqlTable(((SelectNode) node).getFromClause(), tables);
    } else if (node instanceof JoinCall) {
        for (Node comboList : ((JoinCall) node).getComboList()) {
            parseSqlTable(comboList, tables);
        }
    } else if (node instanceof UnionCall) {
        for (Node comboList : ((UnionCall) node).getComboFromList()) {
            parseSqlTable(comboList, tables);
        }
    } else {
        logger.info("该类型未解析");
    }
}
Also used : Table(com.dtstack.taier.develop.sql.Table) ASTNode(org.apache.hadoop.hive.ql.parse.ASTNode)

Example 7 with Table

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

the class SqlTypeRegexUtil method getParseResultByRegexSql.

/**
 * 对语法解析器未成功解析的sql进行简单的解析
 *
 * @param parseResult
 * @return
 */
public static ParseResult getParseResultByRegexSql(ParseResult parseResult) {
    String sql = parseResult.getStandardSql().trim().toLowerCase().replaceAll("\n", " ").replaceAll("\t", " ");
    Map<String, String> tableAndDb = new HashMap<>();
    if (sql.matches(createSql)) {
        tableAndDb = SqlRegexUtil.parseDbTableFromCreateSql(parseResult.getOriginSql());
        parseResult.setSqlType(SqlType.CREATE);
    } else if (sql.matches(insertSql)) {
        tableAndDb = SqlRegexUtil.parseDbTableFromInsertSql(parseResult.getOriginSql());
        parseResult.setSqlType(SqlType.INSERT);
    } else if (sql.matches(alterSql)) {
        tableAndDb = SqlRegexUtil.parseDbTableFromAlterSql(parseResult.getOriginSql());
        parseResult.setSqlType(SqlType.ALTER);
    } else if (sql.matches(dropSql)) {
        tableAndDb = SqlRegexUtil.parseDbTableFromDropSql(parseResult.getOriginSql());
        parseResult.setSqlType(SqlType.DROP);
    } else if (sql.matches(selectSql)) {
        parseResult.setSqlType(SqlType.QUERY);
    } else if (sql.matches(showSql)) {
        parseResult.setSqlType(SqlType.SHOW);
    } else if (sql.matches(withSql)) {
        if (sql.indexOf("create") > -1) {
            parseResult.setSqlType(SqlType.CREATE);
        } else if (sql.indexOf("insert") > -1) {
            parseResult.setSqlType(SqlType.INSERT);
        } else {
            parseResult.setSqlType(SqlType.SHOW);
        }
    } else {
        parseResult.setSqlType(SqlType.OTHER);
    }
    if (!tableAndDb.isEmpty()) {
        Table table = new Table();
        table.setName(tableAndDb.get(SqlRegexUtil.KEY_TABLE));
        if (tableAndDb.containsKey(SqlRegexUtil.KEY_DB)) {
            table.setDb(tableAndDb.get(SqlRegexUtil.KEY_DB));
        } else {
            table.setDb(parseResult.getCurrentDb());
        }
        parseResult.setMainTable(table);
    }
    return parseResult;
}
Also used : Table(com.dtstack.taier.develop.sql.Table) HashMap(java.util.HashMap)

Example 8 with Table

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

the class AstNodeParser method parseTableFromASTNode.

private void parseTableFromASTNode(ASTNode root, Set<Table> tables, String defaultDb) {
    if (root.getToken().getType() == HiveParser.TOK_TABNAME) {
        Table table = new Table();
        int type = root.getParent().getType();
        if (type == HiveParser.TOK_TAB) {
            table.setOperate(TableOperateEnum.INSERT);
        } else if (type == HiveParser.TOK_TABREF) {
            table.setOperate(TableOperateEnum.SELECT);
        } else if (type == HiveParser.TOK_ALTERTABLE) {
            if (ASTNodeUtil.contains((ASTNode) root.getParent(), HiveParser.TOK_ALTERTABLE_ADDPARTS)) {
                table.setOperate(TableOperateEnum.ALTERTABLE_ADDPARTS);
            } else {
                table.setOperate(TableOperateEnum.ALTER);
            }
        } else {
            table.setOperate(TableOperateEnum.getOperateBySqlType(type));
        }
        Map<String, String> tableDb = ASTNodeUtil.getTableNameAndDbName((ASTNode) root.getParent());
        table.setName(tableDb.get(ASTNodeUtil.TABLE_NAME_KEY));
        table.setDb(tableDb.get(ASTNodeUtil.DB_NAME_KEY) == null ? defaultDb : tableDb.get(ASTNodeUtil.DB_NAME_KEY));
        tables.add(table);
    } else if (root.getChildCount() > 0) {
        for (Node node : root.getChildren()) {
            parseTableFromASTNode((ASTNode) node, tables, defaultDb);
        }
    }
}
Also used : Table(com.dtstack.taier.develop.sql.Table) AlterNode(com.dtstack.taier.develop.sql.node.AlterNode) CreateNode(com.dtstack.taier.develop.sql.node.CreateNode) InsertNode(com.dtstack.taier.develop.sql.node.InsertNode) ASTNode(org.apache.hadoop.hive.ql.parse.ASTNode) Node(org.apache.hadoop.hive.ql.lib.Node) SelectNode(com.dtstack.taier.develop.sql.node.SelectNode) DropNode(com.dtstack.taier.develop.sql.node.DropNode) ASTNode(org.apache.hadoop.hive.ql.parse.ASTNode)

Example 9 with Table

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

the class AstNodeParser method getMainTable.

/*
       获取main主表
    */
private void getMainTable(com.dtstack.taier.develop.sql.node.Node node, ParseResult parseResult, ASTNode root) {
    Table mainTable = parseResult.getMainTable();
    if (null == mainTable) {
        mainTable = new Table();
    }
    Identifier table = null;
    if (node instanceof InsertNode) {
        table = ((InsertNode) node).getTargetTable();
        parseResult.setSqlType(SqlType.INSERT);
    } else if (node instanceof CreateNode) {
        table = ((CreateNode) node).getName();
        if (((CreateNode) node).getSqlKind().equals(SqlKind.SELECT)) {
            parseResult.setSqlType(SqlType.CREATE_AS);
        } else if (((CreateNode) node).getSqlKind().equals(SqlKind.LIKE)) {
            parseResult.setSqlType(SqlType.CREATE_LIKE);
        } else {
            parseResult.setSqlType(SqlType.CREATE);
        }
    } else if (node instanceof DropNode) {
        table = ((DropNode) node).getTargetTable();
        parseResult.setSqlType(SqlType.DROP);
    } else if (node instanceof AlterNode) {
        AlterNode alterNode = (AlterNode) node;
        table = alterNode.getSourceTable();
        converAlterResultVO(parseResult, alterNode);
        parseResult.setSqlType(SqlType.ALTER);
    } else if (node instanceof SelectNode) {
        parseResult.setLimit(((SelectNode) node).getLimit());
        if (((SelectNode) node).getFromClause() instanceof Identifier) {
            table = (Identifier) ((SelectNode) node).getFromClause();
        }
        if (parseResult.getStandardSql().toLowerCase().startsWith("with")) {
            parseResult.setSqlType(SqlType.WITH_QUERY);
        } else {
            parseResult.setSqlType(SqlType.QUERY);
        }
    } else {
        parseResult.setSqlType(SqlType.OTHER);
    }
    if (table != null) {
        mainTable.setName(table.getTable());
        mainTable.setDb(StringUtils.isNotEmpty(table.getDb()) ? table.getDb() : table.getDefaultDb());
    }
    boolean isIgnore = ASTNodeUtil.contains((ASTNode) root.getChild(0), HiveParser.TOK_IFNOTEXISTS) || ASTNodeUtil.contains((ASTNode) root.getChild(0), HiveParser.TOK_IFEXISTS);
    if (root.getChild(0).getType() == HiveParser.TOK_CREATEVIEW) {
        mainTable.setView(true);
    }
    mainTable.setIgnore(isIgnore);
    parseResult.setMainTable(mainTable);
}
Also used : Table(com.dtstack.taier.develop.sql.Table) Identifier(com.dtstack.taier.develop.sql.node.Identifier) CreateNode(com.dtstack.taier.develop.sql.node.CreateNode) SelectNode(com.dtstack.taier.develop.sql.node.SelectNode) ASTNode(org.apache.hadoop.hive.ql.parse.ASTNode) InsertNode(com.dtstack.taier.develop.sql.node.InsertNode) DropNode(com.dtstack.taier.develop.sql.node.DropNode) AlterNode(com.dtstack.taier.develop.sql.node.AlterNode)

Example 10 with Table

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