From 4379af4bd438e41189438079fb8847d0c85f4db7 Mon Sep 17 00:00:00 2001 From: jerryjzhang Date: Fri, 3 Apr 2026 11:59:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor(common):=20=E4=BC=98=E5=8C=96SQL?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E5=99=A8=E4=B8=AD=E7=9A=84=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E5=92=8C=E5=B5=8C=E5=A5=97=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用模式匹配简化ParenthesedSelect类型的转换 - 改进SetOperationList中子查询的处理逻辑 - 增加对嵌套ParenthesedSelect的递归处理支持 - 优化字段替换功能以支持更复杂的查询结构 - 提高代码可读性和类型安全性 --- .../common/jsqlparser/SqlReplaceHelper.java | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/common/src/main/java/com/tencent/supersonic/common/jsqlparser/SqlReplaceHelper.java b/common/src/main/java/com/tencent/supersonic/common/jsqlparser/SqlReplaceHelper.java index 0c9cf3777..f7aae8919 100644 --- a/common/src/main/java/com/tencent/supersonic/common/jsqlparser/SqlReplaceHelper.java +++ b/common/src/main/java/com/tencent/supersonic/common/jsqlparser/SqlReplaceHelper.java @@ -118,22 +118,26 @@ public class SqlReplaceHelper { } public static void getFromSelect(FromItem fromItem, List plainSelectList) { - if (!(fromItem instanceof ParenthesedSelect)) { + if (!(fromItem instanceof ParenthesedSelect parenthesedSelect)) { return; } - ParenthesedSelect parenthesedSelect = (ParenthesedSelect) fromItem; Select select = parenthesedSelect.getSelect(); - if (select instanceof PlainSelect) { - PlainSelect plainSelect = (PlainSelect) select; + if (select instanceof PlainSelect plainSelect) { plainSelectList.add(plainSelect); getFromSelect(plainSelect.getFromItem(), plainSelectList); - } else if (select instanceof SetOperationList) { - SetOperationList setOperationList = (SetOperationList) select; + } else if (select instanceof SetOperationList setOperationList) { if (!CollectionUtils.isEmpty(setOperationList.getSelects())) { setOperationList.getSelects().forEach(subSelectBody -> { - PlainSelect subPlainSelect = (PlainSelect) subSelectBody; - plainSelectList.add(subPlainSelect); - getFromSelect(subPlainSelect.getFromItem(), plainSelectList); + if (subSelectBody instanceof PlainSelect subPlainSelect) { + plainSelectList.add(subPlainSelect); + getFromSelect(subPlainSelect.getFromItem(), plainSelectList); + } else if (subSelectBody instanceof ParenthesedSelect subParenthesedSelect) { + Select innerSelect = subParenthesedSelect.getSelect(); + if (innerSelect instanceof PlainSelect innerPlainSelect) { + plainSelectList.add(innerPlainSelect); + getFromSelect(innerPlainSelect.getFromItem(), plainSelectList); + } + } }); } } @@ -188,8 +192,13 @@ public class SqlReplaceHelper { SetOperationList setOperationList = (SetOperationList) select; if (!CollectionUtils.isEmpty(setOperationList.getSelects())) { setOperationList.getSelects().forEach(subSelectBody -> { - PlainSelect subPlainSelect = (PlainSelect) subSelectBody; - replaceFieldsInPlainOneSelect(fieldNameMap, exactReplace, subPlainSelect); + if (subSelectBody instanceof PlainSelect) { + PlainSelect subPlainSelect = (PlainSelect) subSelectBody; + replaceFieldsInPlainOneSelect(fieldNameMap, exactReplace, subPlainSelect); + } else if (subSelectBody instanceof ParenthesedSelect) { + replaceFieldsInPlainOneSelect(fieldNameMap, exactReplace, + ((ParenthesedSelect) subSelectBody).getPlainSelect()); + } }); } }