fix(数据集): sql数据集子查询中使用union执行异常 #8022

This commit is contained in:
taojinlong 2024-02-18 10:57:10 +08:00
parent ad25f77fe6
commit 97938782fa

View File

@ -1116,21 +1116,31 @@ public class DataSetTableService {
}
private void handleFromItems(PlainSelect plainSelect, String dsType) throws Exception {
FromItem fromItem = plainSelect.getFromItem();
if (fromItem instanceof ParenthesedSelect) {
PlainSelect selectBody = ((ParenthesedSelect) fromItem).getPlainSelect();
Select subSelectTmp = (Select) CCJSqlParserUtil.parse(removeVariables(selectBody.toString(), dsType));
((ParenthesedSelect) fromItem).setSelect(subSelectTmp.getSelectBody());
if (dsType.equals(DatasourceTypes.oracle.getType())) {
if (fromItem.getAlias() != null) {
if(((ParenthesedSelect) fromItem).getSelect() instanceof SetOperationList){
StringBuilder result = new StringBuilder();
SetOperationList setOperationList = (SetOperationList) ((ParenthesedSelect) fromItem).getSelect().getSelectBody();
for (int i = 0; i < setOperationList.getSelects().size(); i++) {
result.append(handlePlainSelect((PlainSelect) setOperationList.getSelects().get(i), null, dsType));
if (i < setOperationList.getSelects().size() - 1) {
result.append(" ").append(setOperationList.getOperations().get(i).toString()).append(" ");
}
}
}else {
PlainSelect selectBody = ((ParenthesedSelect) fromItem).getSelect().getPlainSelect();
Select subSelectTmp = (Select) CCJSqlParserUtil.parse(removeVariables(selectBody.toString(), dsType));
((ParenthesedSelect) fromItem).setSelect(subSelectTmp.getSelectBody());
if (dsType.equals(DatasourceTypes.oracle.getType())) {
if (fromItem.getAlias() != null) {
fromItem.setAlias(new Alias(fromItem.getAlias().toString(), false));
}
} else {
if (fromItem.getAlias() == null) {
throw new Exception("Failed to parse sql, Every derived table must have its own alias");
}
fromItem.setAlias(new Alias(fromItem.getAlias().toString(), false));
}
} else {
if (fromItem.getAlias() == null) {
throw new Exception("Failed to parse sql, Every derived table must have its own alias");
}
fromItem.setAlias(new Alias(fromItem.getAlias().toString(), false));
}
plainSelect.setFromItem(fromItem);
}