From 2e77f20f08211f931a19a34b586e5113c5c401df Mon Sep 17 00:00:00 2001 From: hutiefang76 <137664623+hutiefang76@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:25:16 +0800 Subject: [PATCH] fix: parse arithmetic after subqueries in BETWEEN bounds Use the existing arithmetic continuation for parenthesized subqueries in both BETWEEN bounds. Check the reported expressions and their parsed node types. Refs #2944 --- .../net/sf/jsqlparser/parser/JSqlParserCC.jjt | 27 ++++++++++++----- .../operators/relational/BetweenTest.java | 30 +++++++++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt index 08ae097a63..74859ab6c9 100644 --- a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt +++ b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt @@ -1234,6 +1234,12 @@ public class CCJSqlParser extends AbstractJSqlParser { return kind == K_RETURNING || kind == K_ABSENT && getToken(2).kind == K_ON; } + private Expression prattArithRest(Expression left, Token first) throws ParseException { + // MySQL has separate lower tiers for | and &. + int arithFloor = Dialect.MYSQL.name().equals(getAsString(Feature.dialect)) ? 2 : 4; + return prattArithRest(left, first, arithFloor); + } + protected Expression prattArithRest(Expression left, Token first, int minPrec) throws ParseException { while (!interrupted) { Token t = getToken(1); @@ -16615,7 +16621,7 @@ Expression Between(Expression leftExpression) : ) ] ( - LOOKAHEAD({ isParenthesedSelectAhead() }) betweenExpressionStart = ParenthesedSelect() + LOOKAHEAD({ isParenthesedSelectAhead() }) betweenExpressionStart = BetweenSubqueryBound() | betweenExpressionStart = SimpleExpression() [ @@ -16635,7 +16641,7 @@ Expression Between(Expression leftExpression) : ( - LOOKAHEAD({ isParenthesedSelectAhead() }) betweenExpressionEnd = ParenthesedSelect() + LOOKAHEAD({ isParenthesedSelectAhead() }) betweenExpressionEnd = BetweenSubqueryBound() | betweenExpressionEnd = SimpleExpression() [ @@ -16662,6 +16668,16 @@ Expression Between(Expression leftExpression) : } } +Expression BetweenSubqueryBound() : +{ + Token first = getToken(1); + Expression bound; +} +{ + bound = ParenthesedSelect() + { return prattArithRest(bound, first); } +} + Expression LikeExpression(Expression leftExpression) #LikeExpression: { LikeExpression result = new LikeExpression(); @@ -17122,12 +17138,7 @@ Expression SimpleExpression(): { first = getToken(1); } retval=PrimaryExpression() // PostgreSQL path operators bind below arithmetic and above comparisons. - // MySQL also gives | and & their own tiers below <<, >>, so the loop's - // entry floor drops to 2 there instead of the shared bitwise level 4. - { - int arithFloor = Dialect.MYSQL.name().equals(getAsString(Feature.dialect)) ? 2 : 4; - retval = prattArithRest(retval, first, arithFloor); - } + { retval = prattArithRest(retval, first); } { if (user != null) { VariableAssignment assignment = new VariableAssignment(); diff --git a/src/test/java/net/sf/jsqlparser/expression/operators/relational/BetweenTest.java b/src/test/java/net/sf/jsqlparser/expression/operators/relational/BetweenTest.java index ee04111bc4..82a41cead9 100644 --- a/src/test/java/net/sf/jsqlparser/expression/operators/relational/BetweenTest.java +++ b/src/test/java/net/sf/jsqlparser/expression/operators/relational/BetweenTest.java @@ -10,11 +10,16 @@ package net.sf.jsqlparser.expression.operators.relational; import net.sf.jsqlparser.JSQLParserException; +import net.sf.jsqlparser.expression.operators.arithmetic.Addition; +import net.sf.jsqlparser.expression.operators.arithmetic.Subtraction; import net.sf.jsqlparser.schema.Column; +import net.sf.jsqlparser.statement.select.ParenthesedSelect; import net.sf.jsqlparser.statement.select.PlainSelect; import net.sf.jsqlparser.test.TestUtils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.*; @@ -26,6 +31,31 @@ void testBetweenWithAdditionIssue1948() throws JSQLParserException { TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true); } + @ParameterizedTest + @ValueSource(strings = { + "SELECT 1 FROM t WHERE d BETWEEN (SELECT m FROM x) - 1 AND (SELECT m FROM x)", + "SELECT 1 FROM t WHERE d BETWEEN (SELECT m FROM x) - INTERVAL '7 days' AND now()" + }) + void testBetweenWithSubqueryArithmeticInLowerBoundIssue2944(String sql) + throws JSQLParserException { + PlainSelect select = (PlainSelect) TestUtils.assertSqlCanBeParsedAndDeparsed(sql, true); + Between between = assertInstanceOf(Between.class, select.getWhere()); + Subtraction lower = + assertInstanceOf(Subtraction.class, between.getBetweenExpressionStart()); + assertInstanceOf(ParenthesedSelect.class, lower.getLeftExpression()); + } + + @Test + void testBetweenWithSubqueryArithmeticInUpperBoundIssue2944() throws JSQLParserException { + String sql = + "SELECT 1 FROM t WHERE d BETWEEN (SELECT m FROM x) AND (SELECT m FROM x) + 1"; + PlainSelect select = (PlainSelect) TestUtils.assertSqlCanBeParsedAndDeparsed(sql, true); + Between between = assertInstanceOf(Between.class, select.getWhere()); + assertInstanceOf(ParenthesedSelect.class, between.getBetweenExpressionStart()); + Addition upper = assertInstanceOf(Addition.class, between.getBetweenExpressionEnd()); + assertInstanceOf(ParenthesedSelect.class, upper.getLeftExpression()); + } + @Test void testBetweenSymmetricIssue2250() throws JSQLParserException { String sqlStr =