Skip to content

Commit 13e61a7

Browse files
feat: RedShift allows TOP before DISTINCT, see https://docs.aws.amazon.com/redshift/latest/dg/r_SELECT_list.html
Signed-off-by: Andreas Reichel <andreas@manticore-projects.com>
1 parent cf7fe15 commit 13e61a7

2 files changed

Lines changed: 15 additions & 8 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,11 @@ PlainSelect PlainSelect() #PlainSelect:
23422342

23432343
[ LOOKAHEAD(2) first = First() { plainSelect.setFirst(first); } ]
23442344

2345+
// Redshift allows TOP before DISTINCT
2346+
// https://docs.aws.amazon.com/redshift/latest/dg/r_SELECT_list.html
2347+
// @Todo: reflect the order when de-parsing
2348+
[ LOOKAHEAD(2) top = Top() { plainSelect.setTop(top); } ]
2349+
23452350
[ LOOKAHEAD(2)
23462351
(
23472352
<K_ALL>

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
1717
import static net.sf.jsqlparser.test.TestUtils.assertStatementCanBeDeparsedAs;
1818
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1920
import static org.junit.jupiter.api.Assertions.assertEquals;
2021
import static org.junit.jupiter.api.Assertions.assertFalse;
2122
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
@@ -24,7 +25,6 @@
2425
import static org.junit.jupiter.api.Assertions.assertNull;
2526
import static org.junit.jupiter.api.Assertions.assertSame;
2627
import static org.junit.jupiter.api.Assertions.assertTrue;
27-
import static org.junit.jupiter.api.Assertions.fail;
2828

2929
import java.io.IOException;
3030
import java.io.StringReader;
@@ -1080,13 +1080,15 @@ public void testDistinctTop() throws JSQLParserException {
10801080

10811081
@Test
10821082
public void testDistinctTop2() {
1083-
String statement = "SELECT TOP 5 DISTINCT myid, mycol FROM mytable WHERE mytable.col = 9";
1084-
try {
1085-
parserManager.parse(new StringReader(statement));
1086-
fail("sould not work");
1087-
} catch (JSQLParserException ex) {
1088-
// expected to fail
1089-
}
1083+
// valid on Redshift
1084+
// https://docs.aws.amazon.com/redshift/latest/dg/r_SELECT_list.html
1085+
String sqlStr = "SELECT TOP 5 DISTINCT myid, mycol FROM mytable WHERE mytable.col = 9";
1086+
assertDoesNotThrow(new Executable() {
1087+
@Override
1088+
public void execute() throws Throwable {
1089+
CCJSqlParserUtil.parse(sqlStr);
1090+
}
1091+
});
10901092
}
10911093

10921094
@Test

0 commit comments

Comments
 (0)