Skip to content

Commit 324891a

Browse files
committed
fix: parse a bracketed operand at the start of a PARTITION BY list
`SELECT SUM(x) OVER (PARTITION BY (1 + a) / 2) FROM t` failed with a ParseException at the `/`. The four PARTITION BY sites chose between `LOOKAHEAD(3) ComplexExpressionList()` and a bracketed list `"(" ComplexExpressionList() ")"`. The probe scans into `Expression()`, whose binary operators live in a Java loop the scan cannot see, so it failed on `( 1 +`, the bracketed alternative won, parsed `(1 + a)` as the whole list and then met `/`. The bracketed alternative only won when the probe failed, and the parse then failed too, so dropping it changes nothing that parsed before. All four sites now call `ComplexExpressionList()` directly.
1 parent a075737 commit 324891a

2 files changed

Lines changed: 27 additions & 19 deletions

File tree

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

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9475,7 +9475,6 @@ PlainSelect PlainSelect() #PlainSelect:
94759475
OracleHierarchicalExpression oracleHierarchicalQueryClause = null;
94769476
PreferringClause preferringClause = null;
94779477
ExpressionList expressionList = null;
9478-
boolean partitionByBrackets = false;
94799478
List<Table> intoTables = null;
94809479
MySqlSelectIntoClause mySqlSelectIntoClause = null;
94819480
MySqlProcedureAnalyse mySqlProcedureAnalyse = null;
@@ -9581,13 +9580,9 @@ PlainSelect PlainSelect() #PlainSelect:
95819580
[ LOOKAHEAD(2) oracleHierarchicalQueryClause=OracleHierarchicalQueryClause() { plainSelect.setOracleHierarchical(oracleHierarchicalQueryClause); } ]
95829581
[ LOOKAHEAD(2) preferringClause=PreferringClause() { plainSelect.setPreferringClause(preferringClause); }
95839582
[LOOKAHEAD(2) <K_PARTITION> <K_BY>
9584-
(
9585-
LOOKAHEAD(2) expressionList=ComplexExpressionList()
9586-
|
9587-
"(" {partitionByBrackets = true;} expressionList=ComplexExpressionList() ")"
9588-
)
9583+
expressionList=ComplexExpressionList()
95899584
{
9590-
preferringClause.setPartitionExpressionList(expressionList, partitionByBrackets);
9585+
preferringClause.setPartitionExpressionList(expressionList, false);
95919586
}
95929587
]
95939588
]
@@ -14463,7 +14458,6 @@ JsonAggregateFunction JsonAggregateFunction() : {
1446314458
ExpressionList expressionList = null;
1446414459
List<OrderByElement> olist = null;
1446514460
WindowElement windowElement = null;
14466-
boolean partitionByBrackets = false;
1446714461
}
1446814462
{
1446914463
(
@@ -14550,13 +14544,12 @@ JsonAggregateFunction JsonAggregateFunction() : {
1455014544
<K_OVER> {result.setAnalyticType(AnalyticType.OVER);}
1455114545
"("
1455214546
[<K_PARTITION> <K_BY>
14553-
(LOOKAHEAD(3) expressionList=ComplexExpressionList()
14554-
| "(" {partitionByBrackets = true;} expressionList=ComplexExpressionList() ")" )
14547+
expressionList=ComplexExpressionList()
1455514548
]
1455614549
[olist=OrderByElements() ]
1455714550
[windowElement = WindowElement() ]
1455814551
{
14559-
result.setPartitionExpressionList(expressionList, partitionByBrackets);
14552+
result.setPartitionExpressionList(expressionList, false);
1456014553
result.setOrderByElements(olist);
1456114554
result.setWindowElement(windowElement);
1456214555
}
@@ -14698,7 +14691,6 @@ KeepExpression KeepExpression() : {
1469814691

1469914692
void windowFun(AnalyticExpression retval):{
1470014693
ExpressionList expressionList = null;
14701-
boolean partitionByBrackets = false;
1470214694
String windowName = null;
1470314695
WindowDefinition winDef;
1470414696
} {
@@ -14722,10 +14714,9 @@ void windowFun(AnalyticExpression retval):{
1472214714
[
1472314715
LOOKAHEAD(2) <K_OVER> "("
1472414716
[ <K_PARTITION> <K_BY>
14725-
(LOOKAHEAD(3) expressionList=ComplexExpressionList()
14726-
| "(" {partitionByBrackets = true;} expressionList=ComplexExpressionList() ")" )
14717+
expressionList=ComplexExpressionList()
1472714718
]
14728-
")" { winDef.setPartitionExpressionList(expressionList, partitionByBrackets); retval.setType(AnalyticType.WITHIN_GROUP_OVER); }
14719+
")" { winDef.setPartitionExpressionList(expressionList, false); retval.setType(AnalyticType.WITHIN_GROUP_OVER); }
1472914720
]
1473014721
)
1473114722
}
@@ -14734,7 +14725,6 @@ WindowDefinition windowDefinition() : {
1473414725
ExpressionList expressionList = null;
1473514726
List<OrderByElement> olist = null;
1473614727
WindowElement windowElement = null;
14737-
boolean partitionByBrackets = false;
1473814728
WindowDefinition winDef = new WindowDefinition();
1473914729
String baseWindowName;
1474014730
} {
@@ -14748,13 +14738,12 @@ WindowDefinition windowDefinition() : {
1474814738
{}
1474914739
)
1475014740
[<K_PARTITION> <K_BY>
14751-
(LOOKAHEAD(3) expressionList=ComplexExpressionList()
14752-
| "(" {partitionByBrackets = true;} expressionList=ComplexExpressionList() ")" )
14741+
expressionList=ComplexExpressionList()
1475314742
]
1475414743
[olist=OrderByElements() ]
1475514744
[windowElement = WindowElement() ]
1475614745
{
14757-
winDef.setPartitionExpressionList(expressionList, partitionByBrackets);
14746+
winDef.setPartitionExpressionList(expressionList, false);
1475814747
winDef.setOrderByElements(olist);
1475914748
winDef.setWindowElement(windowElement);
1476014749
}

‎src/test/java/net/sf/jsqlparser/expression/AnalyticExpressionTest.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,23 @@ void testDatabricks() throws JSQLParserException {
4343

4444
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
4545
}
46+
47+
@Test
48+
void testPartitionByBracketedOperand() throws JSQLParserException {
49+
TestUtils.assertSqlCanBeParsedAndDeparsed(
50+
"SELECT SUM(x) OVER (PARTITION BY (1 + a) / 2 ORDER BY b) FROM t", true);
51+
52+
TestUtils.assertSqlCanBeParsedAndDeparsed(
53+
"SELECT SUM(x) OVER w FROM t WINDOW w AS (PARTITION BY (1 + a) / 2)", true);
54+
55+
TestUtils.assertSqlCanBeParsedAndDeparsed(
56+
"SELECT LISTAGG(x, ',') WITHIN GROUP (ORDER BY y) OVER (PARTITION BY (1 + a) / 2) FROM t",
57+
true);
58+
59+
TestUtils.assertSqlCanBeParsedAndDeparsed(
60+
"SELECT * FROM t PREFERRING HIGH x PARTITION BY (1 + a) / 2", true);
61+
62+
TestUtils.assertSqlCanBeParsedAndDeparsed(
63+
"SELECT SUM(x) OVER (PARTITION BY (a, b), c) FROM t", true);
64+
}
4665
}

0 commit comments

Comments
 (0)