Skip to content

Commit 56c2dfe

Browse files
authored
Add support for union without brackets and with limit (#1132)
* add support for union without brackets and with limit * Fixing the last commit.
1 parent d2f61d1 commit 56c2dfe

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1609,9 +1609,13 @@ SelectBody SetOperationList() #SetOperationList: {
16091609
} else {
16101610
if (selects.size()>1 && selects.get(selects.size()-1) instanceof PlainSelect) {
16111611
PlainSelect ps = (PlainSelect)selects.get(selects.size()-1);
1612-
if (ps.getOrderByElements() != null) {
1612+
if (ps.getOrderByElements() != null && !brackets.get(brackets.size() - 1)) {
16131613
list.setOrderByElements(ps.getOrderByElements());
1614+
list.setLimit(ps.getLimit());
1615+
list.setOffset(ps.getOffset());
16141616
ps.setOrderByElements(null);
1617+
ps.setLimit(null);
1618+
ps.setOffset(null);
16151619
}
16161620
}
16171621
list.setBracketsOpsAndSelects(brackets,selects,operations);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,12 @@ public void testSelectItems() throws JSQLParserException {
882882
assertStatementCanBeDeparsedAs(select, statement);
883883
}
884884

885+
@Test
886+
public void testUnionWithOrderByAndLimitAndNoBrackets() throws JSQLParserException {
887+
String stmt = "SELECT id FROM table1 UNION SELECT id FROM table2 ORDER BY id ASC LIMIT 55";
888+
assertSqlCanBeParsedAndDeparsed(stmt);
889+
}
890+
885891
@Test
886892
public void testUnion() throws JSQLParserException {
887893
String statement = "SELECT * FROM mytable WHERE mytable.col = 9 UNION "

0 commit comments

Comments
 (0)