(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:
lexluo09
2024-09-15 11:05:54 +08:00
committed by GitHub
parent 2086a560b1
commit 70fff17fbe
2 changed files with 32 additions and 3 deletions

View File

@@ -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) {

View File

@@ -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);
}
}