Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Postgres NATURAL LEFT/RIGHT joins
Fixes #1559
Make NATURAL an optional Join Keyword, which can be combined with LEFT, RIGHT, INNER
Add tests
  • Loading branch information
manticore-projects committed Jun 9, 2022
commit c894aa6fe8ed9f459b2f2f6801976776cd53f7c8
6 changes: 4 additions & 2 deletions src/main/java/net/sf/jsqlparser/statement/select/Join.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,12 @@ public String toString() {
} else if (isSimple()) {
builder.append(rightItem);
} else {
if (isNatural()) {
builder.append("NATURAL ");
}

if (isRight()) {
builder.append("RIGHT ");
} else if (isNatural()) {
builder.append("NATURAL ");
} else if (isFull()) {
builder.append("FULL ");
} else if (isLeft()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,12 @@ public void deparseJoin(Join join) {
buffer.append(", ");
} else {

if (join.isNatural()) {
buffer.append(" NATURAL");
}

if (join.isRight()) {
buffer.append(" RIGHT");
} else if (join.isNatural()) {
buffer.append(" NATURAL");
} else if (join.isFull()) {
buffer.append(" FULL");
} else if (join.isLeft()) {
Expand Down
39 changes: 26 additions & 13 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2579,24 +2579,37 @@ Join JoinerExpression() #JoinerExpression:

}
{

[ <K_NATURAL> { join.setNatural(true); } ]

[
<K_LEFT> { join.setLeft(true); } [ <K_SEMI> { join.setSemi(true); } | <K_OUTER> { join.setOuter(true); } ]
| ( <K_RIGHT> { join.setRight(true); }
| <K_FULL> { join.setFull(true); }
) [ <K_OUTER> { join.setOuter(true); } ]
| <K_INNER> { join.setInner(true); }
| <K_NATURAL> { join.setNatural(true); }
| <K_CROSS> { join.setCross(true); }
| <K_OUTER> { join.setOuter(true); }
(
<K_LEFT> { join.setLeft(true); } [ <K_SEMI> { join.setSemi(true); } | <K_OUTER> { join.setOuter(true); } ]
|
(
<K_RIGHT> { join.setRight(true); }
|
<K_FULL> { join.setFull(true); }
) [ <K_OUTER> { join.setOuter(true); } ]
|
<K_INNER> { join.setInner(true); }
)
|
<K_CROSS> { join.setCross(true); }
|
<K_OUTER> { join.setOuter(true); }
]

( <K_JOIN> | "," { join.setSimple(true); } (<K_OUTER> { join.setOuter(true); } )?
| <K_STRAIGHT> { join.setStraight(true); } | <K_APPLY> {join.setApply(true); } )

right=FromItem()
(
<K_JOIN>
|
"," { join.setSimple(true); } (<K_OUTER> { join.setOuter(true); } )?
|
<K_STRAIGHT> { join.setStraight(true); }
|
<K_APPLY> {join.setApply(true); }
)

right=FromItem()

[
LOOKAHEAD(2) (
Expand Down
13 changes: 13 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 @@ -5213,4 +5213,17 @@ public void testLoclTimezone1471() throws JSQLParserException {
public void testMissingLimitIssue1505() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("(SELECT * FROM mytable) LIMIT 1");
}

@Test
public void testPostgresNaturalJoinIssue1559() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"SELECT t1.ID,t1.name, t2.DID, t2.name\n" +
"FROM table1 as t1\n" +
"NATURAL RIGHT JOIN table2 as t2", true);

assertSqlCanBeParsedAndDeparsed(
"SELECT t1.ID,t1.name, t2.DID, t2.name\n" +
"FROM table1 as t1\n" +
"NATURAL RIGHT JOIN table2 as t2", true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
select * from sys.dual natural join sys.dual


--@SUCCESSFULLY_PARSED_AND_DEPARSED first on Aug 3, 2021, 7:20:08 AM
--@SUCCESSFULLY_PARSED_AND_DEPARSED first on Aug 3, 2021, 7:20:08 AM
--@FAILURE: Encountered unexpected token: "natural" "NATURAL" recorded first on 9 Jun 2022, 21:50:07