Skip to content

Commit 3515208

Browse files
authored
fix: parse arithmetic after subqueries in BETWEEN bounds (#2946)
Use the existing arithmetic continuation for parenthesized subqueries in both BETWEEN bounds. Check the reported expressions and their parsed node types. Refs #2944
1 parent 702e79b commit 3515208

2 files changed

Lines changed: 49 additions & 8 deletions

File tree

‎src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,12 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
12341234
return kind == K_RETURNING || kind == K_ABSENT && getToken(2).kind == K_ON;
12351235
}
12361236

1237+
private Expression prattArithRest(Expression left, Token first) throws ParseException {
1238+
// MySQL has separate lower tiers for | and &.
1239+
int arithFloor = Dialect.MYSQL.name().equals(getAsString(Feature.dialect)) ? 2 : 4;
1240+
return prattArithRest(left, first, arithFloor);
1241+
}
1242+
12371243
protected Expression prattArithRest(Expression left, Token first, int minPrec) throws ParseException {
12381244
while (!interrupted) {
12391245
Token t = getToken(1);
@@ -16633,7 +16639,7 @@ Expression Between(Expression leftExpression) :
1663316639
)
1663416640
]
1663516641
(
16636-
LOOKAHEAD({ isParenthesedSelectAhead() }) betweenExpressionStart = ParenthesedSelect()
16642+
LOOKAHEAD({ isParenthesedSelectAhead() }) betweenExpressionStart = BetweenSubqueryBound()
1663716643
|
1663816644
betweenExpressionStart = SimpleExpression()
1663916645
[
@@ -16653,7 +16659,7 @@ Expression Between(Expression leftExpression) :
1665316659

1665416660
<K_AND>
1665516661
(
16656-
LOOKAHEAD({ isParenthesedSelectAhead() }) betweenExpressionEnd = ParenthesedSelect()
16662+
LOOKAHEAD({ isParenthesedSelectAhead() }) betweenExpressionEnd = BetweenSubqueryBound()
1665716663
|
1665816664
betweenExpressionEnd = SimpleExpression()
1665916665
[
@@ -16680,6 +16686,16 @@ Expression Between(Expression leftExpression) :
1668016686
}
1668116687
}
1668216688

16689+
Expression BetweenSubqueryBound() :
16690+
{
16691+
Token first = getToken(1);
16692+
Expression bound;
16693+
}
16694+
{
16695+
bound = ParenthesedSelect()
16696+
{ return prattArithRest(bound, first); }
16697+
}
16698+
1668316699
Expression LikeExpression(Expression leftExpression) #LikeExpression:
1668416700
{
1668516701
LikeExpression result = new LikeExpression();
@@ -17143,12 +17159,7 @@ Expression SimpleExpression():
1714317159
{ first = getToken(1); }
1714417160
retval=PrimaryExpression()
1714517161
// PostgreSQL path operators bind below arithmetic and above comparisons.
17146-
// MySQL also gives | and & their own tiers below <<, >>, so the loop's
17147-
// entry floor drops to 2 there instead of the shared bitwise level 4.
17148-
{
17149-
int arithFloor = Dialect.MYSQL.name().equals(getAsString(Feature.dialect)) ? 2 : 4;
17150-
retval = prattArithRest(retval, first, arithFloor);
17151-
}
17162+
{ retval = prattArithRest(retval, first); }
1715217163
{
1715317164
if (user != null) {
1715417165
VariableAssignment assignment = new VariableAssignment();

‎src/test/java/net/sf/jsqlparser/expression/operators/relational/BetweenTest.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
package net.sf.jsqlparser.expression.operators.relational;
1111

1212
import net.sf.jsqlparser.JSQLParserException;
13+
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
14+
import net.sf.jsqlparser.expression.operators.arithmetic.Subtraction;
1315
import net.sf.jsqlparser.schema.Column;
16+
import net.sf.jsqlparser.statement.select.ParenthesedSelect;
1417
import net.sf.jsqlparser.statement.select.PlainSelect;
1518
import net.sf.jsqlparser.test.TestUtils;
1619
import org.junit.jupiter.api.Assertions;
1720
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.ValueSource;
1823

1924
import static org.junit.jupiter.api.Assertions.*;
2025

@@ -26,6 +31,31 @@ void testBetweenWithAdditionIssue1948() throws JSQLParserException {
2631
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
2732
}
2833

34+
@ParameterizedTest
35+
@ValueSource(strings = {
36+
"SELECT 1 FROM t WHERE d BETWEEN (SELECT m FROM x) - 1 AND (SELECT m FROM x)",
37+
"SELECT 1 FROM t WHERE d BETWEEN (SELECT m FROM x) - INTERVAL '7 days' AND now()"
38+
})
39+
void testBetweenWithSubqueryArithmeticInLowerBoundIssue2944(String sql)
40+
throws JSQLParserException {
41+
PlainSelect select = (PlainSelect) TestUtils.assertSqlCanBeParsedAndDeparsed(sql, true);
42+
Between between = assertInstanceOf(Between.class, select.getWhere());
43+
Subtraction lower =
44+
assertInstanceOf(Subtraction.class, between.getBetweenExpressionStart());
45+
assertInstanceOf(ParenthesedSelect.class, lower.getLeftExpression());
46+
}
47+
48+
@Test
49+
void testBetweenWithSubqueryArithmeticInUpperBoundIssue2944() throws JSQLParserException {
50+
String sql =
51+
"SELECT 1 FROM t WHERE d BETWEEN (SELECT m FROM x) AND (SELECT m FROM x) + 1";
52+
PlainSelect select = (PlainSelect) TestUtils.assertSqlCanBeParsedAndDeparsed(sql, true);
53+
Between between = assertInstanceOf(Between.class, select.getWhere());
54+
assertInstanceOf(ParenthesedSelect.class, between.getBetweenExpressionStart());
55+
Addition upper = assertInstanceOf(Addition.class, between.getBetweenExpressionEnd());
56+
assertInstanceOf(ParenthesedSelect.class, upper.getLeftExpression());
57+
}
58+
2959
@Test
3060
void testBetweenSymmetricIssue2250() throws JSQLParserException {
3161
String sqlStr =

0 commit comments

Comments
 (0)