use of com.dtstack.taier.develop.sql.SqlParserImpl in project Taier by DTStack.
the class BatchFunctionService method buildContainFunction.
/**
* 判断sql是否包含自定义函数
*
* @param sql
* @param tenantId
* @return
*/
public String buildContainFunction(String sql, Long tenantId, Integer taskType) {
StringBuilder sb = new StringBuilder();
sql = SqlFormatUtil.formatSql(sql).toLowerCase();
// sql中的自定义函数
SqlParserImpl sqlParser = parserFactory.getSqlParser(ETableType.HIVE);
Set<String> sqlFunctionNames = sqlParser.parseFunction(sql);
if (CollectionUtils.isEmpty(sqlFunctionNames)) {
return StringUtils.EMPTY;
}
// 获取此项目下的自定义函数名称
List<BatchFunction> tenantCustomFunctions = listTenantFunction(tenantId, FunctionType.USER.getType(), taskType);
if (CollectionUtils.isEmpty(tenantCustomFunctions)) {
return StringUtils.EMPTY;
}
List<String> customFunctionNames = tenantCustomFunctions.stream().map(BatchFunction::getName).collect(Collectors.toList());
// 循环sql中的函数判断是否是项目中的名称
for (String sqlFunctionName : sqlFunctionNames) {
// 如果sql中的函数存在于此项目下
if (customFunctionNames.contains(sqlFunctionName)) {
BatchFunction byNameAndTenantId = developFunctionDao.getByNameAndTenantId(tenantId, sqlFunctionName);
sb.append(createTempUDF(byNameAndTenantId));
}
}
return sb.toString();
}
use of com.dtstack.taier.develop.sql.SqlParserImpl in project Taier by DTStack.
the class BatchFunctionService method validContainSelfFunction.
/**
* 校验是否包含了函数
*
* @param sql
* @param tenantId
* @param functionType
* @return
*/
public boolean validContainSelfFunction(String sql, Long tenantId, Integer functionType, Integer taskType) {
if (StringUtils.isBlank(sql) || Objects.isNull(tenantId)) {
return false;
}
sql = SqlFormatUtil.formatSql(sql).toLowerCase();
// sql中的自定义函数
SqlParserImpl sqlParser = parserFactory.getSqlParser(ETableType.HIVE);
Set<String> sqlFunctionNames = sqlParser.parseFunction(sql);
if (CollectionUtils.isEmpty(sqlFunctionNames)) {
return false;
}
// 获取此项目下的自定义函数名称
List<BatchFunction> projectFunctions = listTenantFunction(tenantId, functionType, taskType);
if (CollectionUtils.isEmpty(projectFunctions)) {
return false;
}
List<String> projectFunctionNames = projectFunctions.stream().map(BatchFunction::getName).collect(Collectors.toList());
// 循环sql中的函数判断是否是项目中的名称
for (String sqlFunctionName : sqlFunctionNames) {
// 如果sql中的函数存在于此项目下
if (projectFunctionNames.contains(sqlFunctionName)) {
return true;
}
}
return false;
}
use of com.dtstack.taier.develop.sql.SqlParserImpl in project Taier by DTStack.
the class BatchSqlExeService method parseSql.
/**
* 解析sql
*
* @param executeContent
* @return
*/
private ParseResult parseSql(final ExecuteContent executeContent) {
String dbName = this.getDbName(executeContent);
executeContent.setDatabase(dbName);
TenantComponent tenantEngine = developTenantComponentService.getByTenantAndEngineType(executeContent.getTenantId(), executeContent.getTaskType());
Preconditions.checkNotNull(tenantEngine, String.format("tenantEngine %d not support hadoop engine.", executeContent.getTenantId()));
SqlParserImpl sqlParser = parserFactory.getSqlParser(ETableType.HIVE);
ParseResult parseResult = null;
try {
parseResult = sqlParser.parseSql(executeContent.getSql(), tenantEngine.getComponentIdentity(), new HashMap<>());
} catch (final Exception e) {
LOGGER.error("解析sql异常,sql:{}", executeContent.getSql(), e);
parseResult = new ParseResult();
// libra解析失败也提交sql执行
if (MultiEngineType.HADOOP.getType() == executeContent.getTaskType()) {
parseResult.setParseSuccess(false);
}
parseResult.setFailedMsg(ExceptionUtils.getStackTrace(e));
parseResult.setStandardSql(SqlFormatUtil.getStandardSql(executeContent.getSql()));
}
return parseResult;
}
Aggregations