Skip to content

Commit 74a0f2f

Browse files
Postgres NATURAL LEFT/RIGHT joins (#1560)
* Postgres NATURAL LEFT/RIGHT joins Fixes #1559 Make NATURAL an optional Join Keyword, which can be combined with LEFT, RIGHT, INNER Add tests * Postgres NATURAL LEFT/RIGHT joins Amend readme Revert successful Oracle test
1 parent c1c38fe commit 74a0f2f

File tree

5 files changed

+48
-17
lines changed

5 files changed

+48
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Additionally, we have fixed many errors and improved the code quality and the te
6767
* Add support for `... ALTER COLUMN ... DROP DEFAULT`
6868
* `INSERT` supports `SetOperations` (e. g. `INSERT INTO ... SELECT ... FROM ... UNION SELECT ... FROM ...`), those `SetOperations` are used both for `SELECT` and `VALUES` clauses (API change) in order to simplify the Grammar
6969
* `(WITH ... SELECT ...)` statements within brackets are now supported
70+
* Postgres `NATURAL { INNER | LEFT | RIGHT } JOIN` support
7071

7172

7273
## Building from the sources

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,12 @@ public String toString() {
304304
} else if (isSimple()) {
305305
builder.append(rightItem);
306306
} else {
307+
if (isNatural()) {
308+
builder.append("NATURAL ");
309+
}
310+
307311
if (isRight()) {
308312
builder.append("RIGHT ");
309-
} else if (isNatural()) {
310-
builder.append("NATURAL ");
311313
} else if (isFull()) {
312314
builder.append("FULL ");
313315
} else if (isLeft()) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,12 @@ public void deparseJoin(Join join) {
401401
buffer.append(", ");
402402
} else {
403403

404+
if (join.isNatural()) {
405+
buffer.append(" NATURAL");
406+
}
407+
404408
if (join.isRight()) {
405409
buffer.append(" RIGHT");
406-
} else if (join.isNatural()) {
407-
buffer.append(" NATURAL");
408410
} else if (join.isFull()) {
409411
buffer.append(" FULL");
410412
} else if (join.isLeft()) {

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

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,24 +2579,37 @@ Join JoinerExpression() #JoinerExpression:
25792579

25802580
}
25812581
{
2582-
2582+
[ <K_NATURAL> { join.setNatural(true); } ]
25832583

25842584
[
2585-
<K_LEFT> { join.setLeft(true); } [ <K_SEMI> { join.setSemi(true); } | <K_OUTER> { join.setOuter(true); } ]
2586-
| ( <K_RIGHT> { join.setRight(true); }
2587-
| <K_FULL> { join.setFull(true); }
2588-
) [ <K_OUTER> { join.setOuter(true); } ]
2589-
| <K_INNER> { join.setInner(true); }
2590-
| <K_NATURAL> { join.setNatural(true); }
2591-
| <K_CROSS> { join.setCross(true); }
2592-
| <K_OUTER> { join.setOuter(true); }
2585+
(
2586+
<K_LEFT> { join.setLeft(true); } [ <K_SEMI> { join.setSemi(true); } | <K_OUTER> { join.setOuter(true); } ]
2587+
|
2588+
(
2589+
<K_RIGHT> { join.setRight(true); }
2590+
|
2591+
<K_FULL> { join.setFull(true); }
2592+
) [ <K_OUTER> { join.setOuter(true); } ]
2593+
|
2594+
<K_INNER> { join.setInner(true); }
2595+
)
2596+
|
2597+
<K_CROSS> { join.setCross(true); }
2598+
|
2599+
<K_OUTER> { join.setOuter(true); }
25932600
]
25942601

2595-
( <K_JOIN> | "," { join.setSimple(true); } (<K_OUTER> { join.setOuter(true); } )?
2596-
| <K_STRAIGHT> { join.setStraight(true); } | <K_APPLY> {join.setApply(true); } )
2597-
2598-
right=FromItem()
2602+
(
2603+
<K_JOIN>
2604+
|
2605+
"," { join.setSimple(true); } (<K_OUTER> { join.setOuter(true); } )?
2606+
|
2607+
<K_STRAIGHT> { join.setStraight(true); }
2608+
|
2609+
<K_APPLY> {join.setApply(true); }
2610+
)
25992611

2612+
right=FromItem()
26002613

26012614
[
26022615
LOOKAHEAD(2) (

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5213,4 +5213,17 @@ public void testLoclTimezone1471() throws JSQLParserException {
52135213
public void testMissingLimitIssue1505() throws JSQLParserException {
52145214
assertSqlCanBeParsedAndDeparsed("(SELECT * FROM mytable) LIMIT 1");
52155215
}
5216+
5217+
@Test
5218+
public void testPostgresNaturalJoinIssue1559() throws JSQLParserException {
5219+
assertSqlCanBeParsedAndDeparsed(
5220+
"SELECT t1.ID,t1.name, t2.DID, t2.name\n" +
5221+
"FROM table1 as t1\n" +
5222+
"NATURAL RIGHT JOIN table2 as t2", true);
5223+
5224+
assertSqlCanBeParsedAndDeparsed(
5225+
"SELECT t1.ID,t1.name, t2.DID, t2.name\n" +
5226+
"FROM table1 as t1\n" +
5227+
"NATURAL RIGHT JOIN table2 as t2", true);
5228+
}
52165229
}

0 commit comments

Comments
 (0)