Skip to content

Commit 6738aba

Browse files
authored
Issues 1974 (#1975)
* [fix][common]Support 'BETWEEN AND' query condition parameter parsing `CURRENT`. #1972
1 parent 493a803 commit 6738aba

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

common/src/main/java/com/tencent/supersonic/common/jsqlparser/JsqlConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class JsqlConstants {
2626
public static final String EQUAL_CONSTANT = " 1 = 1 ";
2727
public static final String IN_CONSTANT = " 1 in (1) ";
2828
public static final String LIKE_CONSTANT = "1 like 1";
29+
public static final String BETWEEN_AND_CONSTANT = "1 between 2 and 3";
2930
public static final String IN = "IN";
3031
public static final Map<String, String> rightMap = Stream.of(
3132
new AbstractMap.SimpleEntry<>("<=", "<="), new AbstractMap.SimpleEntry<>("<", "<"),

common/src/main/java/com/tencent/supersonic/common/jsqlparser/SqlRemoveHelper.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,7 @@
88
import net.sf.jsqlparser.expression.Parenthesis;
99
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
1010
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
11-
import net.sf.jsqlparser.expression.operators.relational.ComparisonOperator;
12-
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
13-
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
14-
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
15-
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
16-
import net.sf.jsqlparser.expression.operators.relational.InExpression;
17-
import net.sf.jsqlparser.expression.operators.relational.LikeExpression;
18-
import net.sf.jsqlparser.expression.operators.relational.MinorThan;
19-
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
20-
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
11+
import net.sf.jsqlparser.expression.operators.relational.*;
2112
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
2213
import net.sf.jsqlparser.schema.Column;
2314
import net.sf.jsqlparser.statement.select.AllColumns;
@@ -183,6 +174,8 @@ private static void removeExpressionWithConstant(Expression expression,
183174
handleInExpression((InExpression) expression, removeFieldNames);
184175
} else if (expression instanceof LikeExpression) {
185176
handleLikeExpression((LikeExpression) expression, removeFieldNames);
177+
} else if (expression instanceof Between) {
178+
handleBetweenExpression((Between) expression, removeFieldNames);
186179
}
187180
} catch (JSQLParserException e) {
188181
log.error("JSQLParserException", e);
@@ -226,6 +219,17 @@ private static void handleLikeExpression(LikeExpression likeExpression,
226219
updateLikeExpression(likeExpression, constantExpression);
227220
}
228221

222+
private static void handleBetweenExpression(Between between,
223+
Set<String> removeFieldNames) throws JSQLParserException {
224+
String columnName = SqlSelectHelper.getColumnName(between.getLeftExpression());
225+
if (!removeFieldNames.contains(columnName)) {
226+
return;
227+
}
228+
Between constantExpression =
229+
(Between) CCJSqlParserUtil.parseCondExpression(JsqlConstants.BETWEEN_AND_CONSTANT);
230+
updateBetweenExpression(between, constantExpression);
231+
}
232+
229233
private static void updateComparisonOperator(ComparisonOperator original,
230234
ComparisonOperator constantExpression) {
231235
original.setLeftExpression(constantExpression.getLeftExpression());
@@ -245,6 +249,12 @@ private static void updateLikeExpression(LikeExpression original,
245249
original.setRightExpression(constantExpression.getRightExpression());
246250
}
247251

252+
private static void updateBetweenExpression(Between between, Between constantExpression) {
253+
between.setBetweenExpressionEnd(constantExpression.getBetweenExpressionEnd());
254+
between.setBetweenExpressionStart(constantExpression.getBetweenExpressionStart());
255+
between.setLeftExpression(constantExpression.getLeftExpression());
256+
}
257+
248258
public static String removeHavingCondition(String sql, Set<String> removeFieldNames) {
249259
Select selectStatement = SqlSelectHelper.getSelect(sql);
250260
if (!(selectStatement instanceof PlainSelect)) {
@@ -373,6 +383,10 @@ private static Expression dealComparisonOperatorFilter(Expression expression,
373383
LikeExpression likeExpression = (LikeExpression) expression;
374384
Expression leftExpression = likeExpression.getLeftExpression();
375385
return recursionBase(leftExpression, expression, sqlEditEnum);
386+
} else if (expression instanceof Between) {
387+
Between between = (Between) expression;
388+
Expression leftExpression = between.getLeftExpression();
389+
return recursionBase(leftExpression, expression, sqlEditEnum);
376390
}
377391
return expression;
378392
}

common/src/test/java/com/tencent/supersonic/common/jsqlparser/SqlRemoveHelperTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ void testRemoveWhereCondition() {
157157
replaceSql = SqlRemoveHelper.removeWhereCondition(sql, removeFieldNames);
158158
Assert.assertEquals("SELECT 歌曲名 FROM 歌曲库 WHERE (datediff('day', 发布日期, '2023-08-09') <= 1) "
159159
+ "AND 数据日期 = '2023-08-09' ORDER BY 播放量 DESC LIMIT 11", replaceSql);
160+
161+
sql = "select 歌曲名 from 歌曲库 where datediff('day', 发布日期, '2023-08-09') <= 1 "
162+
+ "and 歌曲名 between '2023-08-09' and '2024-08-09' and 数据日期 = '2023-08-09' and 歌曲发布时 = '2023-08-01'"
163+
+ " order by 播放量 desc limit 11";
164+
replaceSql = SqlRemoveHelper.removeWhereCondition(sql, removeFieldNames);
165+
Assert.assertEquals("SELECT 歌曲名 FROM 歌曲库 WHERE datediff('day', 发布日期, '2023-08-09') <= 1 "
166+
+ "AND 数据日期 = '2023-08-09' AND 歌曲发布时 = '2023-08-01' "
167+
+ "ORDER BY 播放量 DESC LIMIT 11", replaceSql);
160168
}
161169

162170
@Test

0 commit comments

Comments
 (0)