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("该类型未解析");
}
}
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;
}
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);
}
}
}
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);
}
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;
}
Aggregations