Skip to content

Commit 5706fb4

Browse files
committed
next correction of parenthesis around unions
1 parent e2ff225 commit 5706fb4

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,7 @@ SelectBody SetOperationList() #SetOperationList: {
15831583
{
15841584
(("(" select=SelectBody() ")" { bracket=true;} )
15851585
| ( select=PlainSelect() | select=Values() ) { bracket=false;} ) {selects.add(select);brackets.add(bracket); }
1586-
(
1586+
( LOOKAHEAD(2)
15871587
((<K_UNION> { UnionOp union = new UnionOp();linkAST(union,jjtThis);operations.add(union); } [ <K_ALL> { union.setAll(true); } | <K_DISTINCT> { union.setDistinct(true); } ])
15881588
| <K_INTERSECT> { operations.add(new IntersectOp()); }
15891589
| <K_MINUS> { operations.add(new MinusOp()); }
@@ -1618,23 +1618,33 @@ SelectBody SetOperationList() #SetOperationList: {
16181618
}
16191619
}
16201620

1621-
SelectBody SetOperationListWithoutIntialSelect(SelectBody _select, boolean _bracket) #SetOperationList:
1621+
SelectBody SetOperationListWithoutIntialSelect(FromItem fromItem) #SetOperationList:
16221622
{
16231623
SetOperationList list = new SetOperationList();
16241624
List<OrderByElement> orderByElements = null;
16251625
Limit limit = null;
16261626
Offset offset = null;
16271627
Fetch fetch = null;
1628-
SelectBody select = _select;
1628+
SelectBody select;
16291629
List<SelectBody> selects = new ArrayList<SelectBody>();
16301630
List<SetOperation> operations = new ArrayList<SetOperation>();
16311631
List<Boolean> brackets = new ArrayList<Boolean>();
1632-
boolean bracket = _bracket;
1632+
boolean bracket = false;
16331633
}
16341634
{
16351635
{
1636+
while (fromItem instanceof ParenthesisFromItem) {
1637+
fromItem = ((ParenthesisFromItem)fromItem).getFromItem();
1638+
}
1639+
1640+
if (fromItem instanceof SubSelect) {
1641+
select = ((SubSelect)fromItem).getSelectBody();
1642+
} else {
1643+
throw new IllegalArgumentException("this type of set operation is not allowed");
1644+
}
1645+
16361646
selects.add(select);
1637-
brackets.add(bracket);
1647+
brackets.add(true);
16381648
}
16391649
( LOOKAHEAD(2)
16401650
((<K_UNION> { UnionOp union = new UnionOp();linkAST(union,jjtThis);operations.add(union); } [ <K_ALL> { union.setAll(true); } | <K_DISTINCT> { union.setDistinct(true); } ])
@@ -2010,15 +2020,15 @@ FromItem FromItem():
20102020
|
20112021
fromItem=SubSelect()
20122022
)
2023+
[ selectBody = SetOperationListWithoutIntialSelect(fromItem)
2024+
{
2025+
if (!(selectBody instanceof PlainSelect)) {
2026+
fromItem = new SubSelect().withSelectBody(selectBody);
2027+
}
2028+
}
2029+
]
20132030
")"
20142031
[ LOOKAHEAD(2) unpivot=UnPivot() { fromItem.setUnPivot(unpivot); } ]
2015-
[ LOOKAHEAD(2) selectBody = SetOperationListWithoutIntialSelect(((SubSelect)fromItem).getSelectBody(), true)
2016-
{
2017-
if (!(selectBody instanceof PlainSelect)) {
2018-
fromItem = new SubSelect().withSelectBody(selectBody);
2019-
}
2020-
}
2021-
]
20222032
)
20232033
|
20242034
LOOKAHEAD(TableFunction())

src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4408,4 +4408,27 @@ public void testH2CaseWhenFunctionIssue1091() throws JSQLParserException {
44084408
public void testMultiPartTypesIssue992() throws JSQLParserException {
44094409
assertSqlCanBeParsedAndDeparsed("SELECT CAST('*' AS pg_catalog.text)");
44104410
}
4411+
4412+
@Test
4413+
public void testSetOperationWithParenthesisIssue1094() throws JSQLParserException {
4414+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM ((SELECT A FROM tbl) UNION DISTINCT (SELECT B FROM tbl2)) AS union1");
4415+
}
4416+
4417+
@Test
4418+
public void testSetOperationWithParenthesisIssue1094_2() throws JSQLParserException {
4419+
Statement stmt = CCJSqlParserUtil.parse("SELECT * FROM (((SELECT A FROM tbl)) UNION DISTINCT (SELECT B FROM tbl2)) AS union1");
4420+
assertEquals("SELECT * FROM ((SELECT A FROM tbl) UNION DISTINCT (SELECT B FROM tbl2)) AS union1", stmt.toString());
4421+
}
4422+
4423+
@Test
4424+
public void testSetOperationWithParenthesisIssue1094_3() throws JSQLParserException {
4425+
Statement stmt = CCJSqlParserUtil.parse("SELECT * FROM (((SELECT A FROM tbl)) UNION DISTINCT ((SELECT B FROM tbl2))) AS union1");
4426+
assertEquals("SELECT * FROM ((SELECT A FROM tbl) UNION DISTINCT ((SELECT B FROM tbl2))) AS union1", stmt.toString());
4427+
}
4428+
4429+
@Test
4430+
public void testSetOperationWithParenthesisIssue1094_4() throws JSQLParserException {
4431+
Statement stmt = CCJSqlParserUtil.parse("SELECT * FROM (((((SELECT A FROM tbl)))) UNION DISTINCT (((((((SELECT B FROM tbl2)))))))) AS union1");
4432+
assertEquals("SELECT * FROM ((SELECT A FROM tbl) UNION DISTINCT ((SELECT B FROM tbl2))) AS union1", stmt.toString());
4433+
}
44114434
}

0 commit comments

Comments
 (0)