mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-10 19:38:13 +00:00
(improvement)(headless) Delete 'is null' and 'is not null' expressions only in the WHERE clause, and keep them in the HAVING clause (#1680)
This commit is contained in:
@@ -406,9 +406,18 @@ public class SqlRemoveHelper {
|
||||
SelectDeParser selectDeParser = new SelectDeParser(expressionDeParser, buffer);
|
||||
expressionDeParser.setSelectVisitor(selectDeParser);
|
||||
expressionDeParser.setBuffer(buffer);
|
||||
|
||||
selectStatement.accept(selectDeParser);
|
||||
return buffer.toString();
|
||||
PlainSelect plainSelect = (PlainSelect) selectStatement.getSelectBody();
|
||||
if (plainSelect.getWhere() != null) {
|
||||
plainSelect.getWhere().accept(expressionDeParser);
|
||||
}
|
||||
// Parse the modified WHERE clause back to an Expression
|
||||
try {
|
||||
Expression newWhere = CCJSqlParserUtil.parseCondExpression(buffer.toString());
|
||||
plainSelect.setWhere(newWhere);
|
||||
} catch (Exception e) {
|
||||
log.error("parseCondExpression error:{}", buffer, e);
|
||||
}
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
private static boolean isInvalidSelect(Select selectStatement) {
|
||||
|
||||
@@ -185,6 +185,16 @@ class SqlRemoveHelperTest {
|
||||
Assert.assertEquals(
|
||||
"SELECT 数据日期 FROM 歌曲库 WHERE 数据日期 = '2023-08-09' AND 歌曲发布时间 = '2023-08-01' GROUP BY 数据日期",
|
||||
replaceSql);
|
||||
|
||||
sql =
|
||||
"select 数据日期 from 歌曲库 where 歌曲名 is null and 数据日期 = '2023-08-09' and "
|
||||
+ "歌曲发布时间 = '2023-08-01' group by 数据日期 having 歌曲名 is null";
|
||||
|
||||
replaceSql = SqlRemoveHelper.removeIsNullInWhere(sql, removeFieldNames);
|
||||
|
||||
Assert.assertEquals(
|
||||
"SELECT 数据日期 FROM 歌曲库 WHERE 数据日期 = '2023-08-09' AND 歌曲发布时间 = '2023-08-01' GROUP BY 数据日期 HAVING 歌曲名 IS NULL",
|
||||
replaceSql);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -200,5 +210,15 @@ class SqlRemoveHelperTest {
|
||||
Assert.assertEquals(
|
||||
"SELECT 数据日期 FROM 歌曲库 WHERE 数据日期 = '2023-08-09' AND 歌曲发布时间 = '2023-08-01' GROUP BY 数据日期",
|
||||
replaceSql);
|
||||
|
||||
sql =
|
||||
"select 数据日期 from 歌曲库 where 歌曲名 is not null and 数据日期 = '2023-08-09' and "
|
||||
+ "歌曲发布时间 = '2023-08-01' group by 数据日期 having 歌曲名 is not null";
|
||||
|
||||
replaceSql = SqlRemoveHelper.removeNotNullInWhere(sql, removeFieldNames);
|
||||
|
||||
Assert.assertEquals(
|
||||
"SELECT 数据日期 FROM 歌曲库 WHERE 数据日期 = '2023-08-09' AND 歌曲发布时间 = '2023-08-01' GROUP BY 数据日期 HAVING 歌曲名 IS NOT NULL",
|
||||
replaceSql);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user