diff --git a/common/src/main/java/com/tencent/supersonic/common/jsqlparser/JsqlConstants.java b/common/src/main/java/com/tencent/supersonic/common/jsqlparser/JsqlConstants.java index c368483a8..0ae60cdbe 100644 --- a/common/src/main/java/com/tencent/supersonic/common/jsqlparser/JsqlConstants.java +++ b/common/src/main/java/com/tencent/supersonic/common/jsqlparser/JsqlConstants.java @@ -26,6 +26,7 @@ public class JsqlConstants { public static final String EQUAL_CONSTANT = " 1 = 1 "; public static final String IN_CONSTANT = " 1 in (1) "; public static final String LIKE_CONSTANT = "1 like 1"; + public static final String BETWEEN_AND_CONSTANT = "1 between 2 and 3"; public static final String IN = "IN"; public static final Map rightMap = Stream.of( new AbstractMap.SimpleEntry<>("<=", "<="), new AbstractMap.SimpleEntry<>("<", "<"), diff --git a/common/src/main/java/com/tencent/supersonic/common/jsqlparser/SqlRemoveHelper.java b/common/src/main/java/com/tencent/supersonic/common/jsqlparser/SqlRemoveHelper.java index f3fc200b7..229e2d74e 100644 --- a/common/src/main/java/com/tencent/supersonic/common/jsqlparser/SqlRemoveHelper.java +++ b/common/src/main/java/com/tencent/supersonic/common/jsqlparser/SqlRemoveHelper.java @@ -8,16 +8,7 @@ import net.sf.jsqlparser.expression.LongValue; import net.sf.jsqlparser.expression.Parenthesis; import net.sf.jsqlparser.expression.operators.conditional.AndExpression; import net.sf.jsqlparser.expression.operators.conditional.OrExpression; -import net.sf.jsqlparser.expression.operators.relational.ComparisonOperator; -import net.sf.jsqlparser.expression.operators.relational.EqualsTo; -import net.sf.jsqlparser.expression.operators.relational.ExpressionList; -import net.sf.jsqlparser.expression.operators.relational.GreaterThan; -import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals; -import net.sf.jsqlparser.expression.operators.relational.InExpression; -import net.sf.jsqlparser.expression.operators.relational.LikeExpression; -import net.sf.jsqlparser.expression.operators.relational.MinorThan; -import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals; -import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo; +import net.sf.jsqlparser.expression.operators.relational.*; import net.sf.jsqlparser.parser.CCJSqlParserUtil; import net.sf.jsqlparser.schema.Column; import net.sf.jsqlparser.statement.select.AllColumns; @@ -183,6 +174,8 @@ public class SqlRemoveHelper { handleInExpression((InExpression) expression, removeFieldNames); } else if (expression instanceof LikeExpression) { handleLikeExpression((LikeExpression) expression, removeFieldNames); + } else if (expression instanceof Between) { + handleBetweenExpression((Between) expression, removeFieldNames); } } catch (JSQLParserException e) { log.error("JSQLParserException", e); @@ -226,6 +219,17 @@ public class SqlRemoveHelper { updateLikeExpression(likeExpression, constantExpression); } + private static void handleBetweenExpression(Between between, + Set removeFieldNames) throws JSQLParserException { + String columnName = SqlSelectHelper.getColumnName(between.getLeftExpression()); + if (!removeFieldNames.contains(columnName)) { + return; + } + Between constantExpression = + (Between) CCJSqlParserUtil.parseCondExpression(JsqlConstants.BETWEEN_AND_CONSTANT); + updateBetweenExpression(between, constantExpression); + } + private static void updateComparisonOperator(ComparisonOperator original, ComparisonOperator constantExpression) { original.setLeftExpression(constantExpression.getLeftExpression()); @@ -245,6 +249,12 @@ public class SqlRemoveHelper { original.setRightExpression(constantExpression.getRightExpression()); } + private static void updateBetweenExpression(Between between, Between constantExpression) { + between.setBetweenExpressionEnd(constantExpression.getBetweenExpressionEnd()); + between.setBetweenExpressionStart(constantExpression.getBetweenExpressionStart()); + between.setLeftExpression(constantExpression.getLeftExpression()); + } + public static String removeHavingCondition(String sql, Set removeFieldNames) { Select selectStatement = SqlSelectHelper.getSelect(sql); if (!(selectStatement instanceof PlainSelect)) { @@ -373,6 +383,10 @@ public class SqlRemoveHelper { LikeExpression likeExpression = (LikeExpression) expression; Expression leftExpression = likeExpression.getLeftExpression(); return recursionBase(leftExpression, expression, sqlEditEnum); + } else if (expression instanceof Between) { + Between between = (Between) expression; + Expression leftExpression = between.getLeftExpression(); + return recursionBase(leftExpression, expression, sqlEditEnum); } return expression; } diff --git a/common/src/test/java/com/tencent/supersonic/common/jsqlparser/SqlRemoveHelperTest.java b/common/src/test/java/com/tencent/supersonic/common/jsqlparser/SqlRemoveHelperTest.java index 72a913002..57e9d818d 100644 --- a/common/src/test/java/com/tencent/supersonic/common/jsqlparser/SqlRemoveHelperTest.java +++ b/common/src/test/java/com/tencent/supersonic/common/jsqlparser/SqlRemoveHelperTest.java @@ -157,6 +157,14 @@ class SqlRemoveHelperTest { replaceSql = SqlRemoveHelper.removeWhereCondition(sql, removeFieldNames); Assert.assertEquals("SELECT 歌曲名 FROM 歌曲库 WHERE (datediff('day', 发布日期, '2023-08-09') <= 1) " + "AND 数据日期 = '2023-08-09' ORDER BY 播放量 DESC LIMIT 11", replaceSql); + + sql = "select 歌曲名 from 歌曲库 where datediff('day', 发布日期, '2023-08-09') <= 1 " + + "and 歌曲名 between '2023-08-09' and '2024-08-09' and 数据日期 = '2023-08-09' and 歌曲发布时 = '2023-08-01'" + + " order by 播放量 desc limit 11"; + replaceSql = SqlRemoveHelper.removeWhereCondition(sql, removeFieldNames); + Assert.assertEquals("SELECT 歌曲名 FROM 歌曲库 WHERE datediff('day', 发布日期, '2023-08-09') <= 1 " + + "AND 数据日期 = '2023-08-09' AND 歌曲发布时 = '2023-08-01' " + + "ORDER BY 播放量 DESC LIMIT 11", replaceSql); } @Test