diff --git a/common/src/main/java/com/tencent/supersonic/common/jsqlparser/SqlSelectHelper.java b/common/src/main/java/com/tencent/supersonic/common/jsqlparser/SqlSelectHelper.java index 6acd6ac7d..23bc459be 100644 --- a/common/src/main/java/com/tencent/supersonic/common/jsqlparser/SqlSelectHelper.java +++ b/common/src/main/java/com/tencent/supersonic/common/jsqlparser/SqlSelectHelper.java @@ -776,24 +776,25 @@ public class SqlSelectHelper { private static void getFieldsWithSubQuery(PlainSelect plainSelect, Map> fields) { if (plainSelect.getFromItem() instanceof Table) { - boolean isWith = false; + List withAlias = new ArrayList<>(); if (!CollectionUtils.isEmpty(plainSelect.getWithItemsList())) { for (WithItem withItem : plainSelect.getWithItemsList()) { if (Objects.nonNull(withItem.getSelect())) { getFieldsWithSubQuery(withItem.getSelect().getPlainSelect(), fields); - isWith = true; + withAlias.add(withItem.getAlias().getName()); } } } - if (!isWith) { - Table table = (Table) plainSelect.getFromItem(); + Table table = (Table) plainSelect.getFromItem(); + String tableName = table.getFullyQualifiedName(); + if (!withAlias.contains(tableName)) { if (!fields.containsKey(table.getFullyQualifiedName())) { - fields.put(table.getFullyQualifiedName(), new HashSet<>()); + fields.put(tableName, new HashSet<>()); } List sqlFields = getFieldsByPlainSelect(plainSelect).stream().map(f -> f.replaceAll("`", "")) .collect( Collectors.toList()); - fields.get(table.getFullyQualifiedName()).addAll(sqlFields); + fields.get(tableName).addAll(sqlFields); } } if (plainSelect.getFromItem() instanceof ParenthesedSelect) {