Skip to content

Commit e17cdef

Browse files
authored
feat: support distinctrow keyword (#2238)
1 parent 296d032 commit e17cdef

File tree

6 files changed

+41
-2
lines changed

6 files changed

+41
-2
lines changed

src/main/java/net/sf/jsqlparser/parser/ParserKeywordsUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class ParserKeywordsUtils {
6363
{"CURRENT", RESTRICTED_JSQLPARSER},
6464
{"DEFAULT", RESTRICTED_ALIAS},
6565
{"DISTINCT", RESTRICTED_SQL2016},
66+
{"DISTINCTROW", RESTRICTED_SQL2016},
6667
{"DOUBLE", RESTRICTED_ALIAS},
6768
{"ELSE", RESTRICTED_JSQLPARSER},
6869
{"EXCEPT", RESTRICTED_SQL2016},

src/main/java/net/sf/jsqlparser/statement/select/Distinct.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class Distinct implements Serializable {
2020

2121
private List<SelectItem<?>> onSelectItems;
2222
private boolean useUnique = false;
23+
private boolean useDistinctRow = false;
2324

2425
public Distinct() {}
2526

@@ -43,9 +44,18 @@ public void setUseUnique(boolean useUnique) {
4344
this.useUnique = useUnique;
4445
}
4546

47+
public boolean isUseDistinctRow() {
48+
return useDistinctRow;
49+
}
50+
51+
public void setUseDistinctRow(boolean useDistinctRow) {
52+
this.useDistinctRow = useDistinctRow;
53+
}
54+
4655
@Override
4756
public String toString() {
48-
String sql = useUnique ? "UNIQUE" : "DISTINCT";
57+
String distinctIdentifier = useDistinctRow ? "DISTINCTROW" : "DISTINCT";
58+
String sql = useUnique ? "UNIQUE" : distinctIdentifier;
4959

5060
if (onSelectItems != null && !onSelectItems.isEmpty()) {
5161
sql += " ON (" + PlainSelect.getStringList(onSelectItems) + ")";

src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ protected void deparseDistinctClause(Distinct distinct) {
397397
if (distinct != null) {
398398
if (distinct.isUseUnique()) {
399399
builder.append("UNIQUE ");
400+
} else if (distinct.isUseDistinctRow()) {
401+
builder.append("DISTINCTROW ");
400402
} else {
401403
builder.append("DISTINCT ");
402404
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
316316
| <K_DISCARD : "DISCARD">
317317
| <K_DISCONNECT:"DISCONNECT">
318318
| <K_DISTINCT:"DISTINCT">
319+
| <K_DISTINCTROW:"DISTINCTROW">
319320
| <K_DIV:"DIV">
320321
| <K_DDL:"DDL">
321322
| <K_DML:"DML">
@@ -3147,7 +3148,9 @@ PlainSelect PlainSelect() #PlainSelect:
31473148
[ LOOKAHEAD(2) "ON" "(" distinctOn=SelectItemsList() { plainSelect.getDistinct().setOnSelectItems(distinctOn); } ")" ]
31483149
)
31493150
|
3150-
<K_UNIQUE> { distinct = new Distinct(true); plainSelect.setDistinct(distinct); }
3151+
<K_DISTINCTROW> { distinct = new Distinct(); distinct.setUseDistinctRow(true); plainSelect.setDistinct(distinct); }
3152+
|
3153+
<K_UNIQUE> { distinct = new Distinct(true); plainSelect.setDistinct(distinct); }
31513154
|
31523155
<K_SQL_CALC_FOUND_ROWS> { plainSelect.setMySqlSqlCalcFoundRows(true); }
31533156
|

src/site/sphinx/keywords.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ The following Keywords are **restricted** in JSQLParser-|JSQLPARSER_VERSION| and
4343
+----------------------+-------------+-----------+
4444
| DISTINCT | Yes | Yes |
4545
+----------------------+-------------+-----------+
46+
| DISTINCTROW | Yes | Yes |
47+
+----------------------+-------------+-----------+
4648
| DOUBLE | Yes | |
4749
+----------------------+-------------+-----------+
4850
| ELSE | Yes | Yes |

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,27 @@ public void testDistinct() throws JSQLParserException {
10581058
.getExpression()).getColumnName());
10591059
}
10601060

1061+
@Test
1062+
public void testDistinctRow() throws JSQLParserException {
1063+
String statement =
1064+
"SELECT DISTINCTROW col1, col2 FROM mytable WHERE mytable.col = 9";
1065+
Select select = (Select) TestUtils.assertSqlCanBeParsedAndDeparsed(statement, true);
1066+
1067+
assertInstanceOf(PlainSelect.class, select);
1068+
1069+
PlainSelect plainSelect = (PlainSelect) select;
1070+
Distinct distinct = plainSelect.getDistinct();
1071+
1072+
assertNotNull(distinct);
1073+
assertTrue(distinct.isUseDistinctRow());
1074+
assertNull(distinct.getOnSelectItems());
1075+
1076+
assertEquals("col1", ((Column) (plainSelect.getSelectItems().get(0))
1077+
.getExpression()).getColumnName());
1078+
assertEquals("col2", ((Column) (plainSelect.getSelectItems().get(1))
1079+
.getExpression()).getColumnName());
1080+
}
1081+
10611082
@Test
10621083
public void testIsDistinctFrom() throws JSQLParserException {
10631084
String stmt = "SELECT name FROM tbl WHERE name IS DISTINCT FROM foo";

0 commit comments

Comments
 (0)