Skip to content

feat: support distinctrow keyword #2238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class ParserKeywordsUtils {
{"CURRENT", RESTRICTED_JSQLPARSER},
{"DEFAULT", RESTRICTED_ALIAS},
{"DISTINCT", RESTRICTED_SQL2016},
{"DISTINCTROW", RESTRICTED_SQL2016},
{"DOUBLE", RESTRICTED_ALIAS},
{"ELSE", RESTRICTED_JSQLPARSER},
{"EXCEPT", RESTRICTED_SQL2016},
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/net/sf/jsqlparser/statement/select/Distinct.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Distinct implements Serializable {

private List<SelectItem<?>> onSelectItems;
private boolean useUnique = false;
private boolean useDistinctRow = false;

public Distinct() {}

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

public boolean isUseDistinctRow() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since DISTINCTROW is a synonym for DISTINCT, I re-used the existing Distinct class and only augmented it with the useDistinctRow attribute to determine the string representation of Distinct.

return useDistinctRow;
}

public void setUseDistinctRow(boolean useDistinctRow) {
this.useDistinctRow = useDistinctRow;
}

@Override
public String toString() {
String sql = useUnique ? "UNIQUE" : "DISTINCT";
String distinctIdentifier = useDistinctRow ? "DISTINCTROW" : "DISTINCT";
String sql = useUnique ? "UNIQUE" : distinctIdentifier;

if (onSelectItems != null && !onSelectItems.isEmpty()) {
sql += " ON (" + PlainSelect.getStringList(onSelectItems) + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ protected void deparseDistinctClause(Distinct distinct) {
if (distinct != null) {
if (distinct.isUseUnique()) {
builder.append("UNIQUE ");
} else if (distinct.isUseDistinctRow()) {
builder.append("DISTINCTROW ");
} else {
builder.append("DISTINCT ");
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_DISCARD : "DISCARD">
| <K_DISCONNECT:"DISCONNECT">
| <K_DISTINCT:"DISTINCT">
| <K_DISTINCTROW:"DISTINCTROW">
| <K_DIV:"DIV">
| <K_DDL:"DDL">
| <K_DML:"DML">
Expand Down Expand Up @@ -3027,6 +3028,8 @@ PlainSelect PlainSelect() #PlainSelect:
[ LOOKAHEAD(2) "ON" "(" distinctOn=SelectItemsList() { plainSelect.getDistinct().setOnSelectItems(distinctOn); } ")" ]
)
|
<K_DISTINCTROW> { Distinct distinct = new Distinct(); distinct.setUseDistinctRow(true); plainSelect.setDistinct(distinct); }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is separate from the <K_DISTINCT> case right above, because DISTINCT ON or DISTINCTROW ON is not applicable in MySQL.

|
<K_UNIQUE> { Distinct distinct = new Distinct(true); plainSelect.setDistinct(distinct); }
|
<K_SQL_CALC_FOUND_ROWS> { plainSelect.setMySqlSqlCalcFoundRows(true); }
Expand Down
2 changes: 2 additions & 0 deletions src/site/sphinx/keywords.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ The following Keywords are **restricted** in JSQLParser-|JSQLPARSER_VERSION| and
+----------------------+-------------+-----------+
| DISTINCT | Yes | Yes |
+----------------------+-------------+-----------+
| DISTINCTROW | Yes | Yes |
+----------------------+-------------+-----------+
| DOUBLE | Yes | |
+----------------------+-------------+-----------+
| ELSE | Yes | Yes |
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/select/SelectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,27 @@ public void testDistinct() throws JSQLParserException {
.getExpression()).getColumnName());
}

@Test
public void testDistinctRow() throws JSQLParserException {
String statement =
"SELECT DISTINCTROW col1, col2 FROM mytable WHERE mytable.col = 9";
Select select = (Select) TestUtils.assertSqlCanBeParsedAndDeparsed(statement, true);

assertInstanceOf(PlainSelect.class, select);

PlainSelect plainSelect = (PlainSelect) select;
Distinct distinct = plainSelect.getDistinct();

assertNotNull(distinct);
assertTrue(distinct.isUseDistinctRow());
assertNull(distinct.getOnSelectItems());

assertEquals("col1", ((Column) (plainSelect.getSelectItems().get(0))
.getExpression()).getColumnName());
assertEquals("col2", ((Column) (plainSelect.getSelectItems().get(1))
.getExpression()).getColumnName());
}

@Test
public void testIsDistinctFrom() throws JSQLParserException {
String stmt = "SELECT name FROM tbl WHERE name IS DISTINCT FROM foo";
Expand Down
Loading