Skip to content

Commit 0f7493f

Browse files
feat: Nested WithItem allows FromQuery
- fixes #2168 Signed-off-by: Andreas Reichel <[email protected]>
1 parent f932b8f commit 0f7493f

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,13 @@ Select Select() #Select:
23162316
}
23172317
{
23182318
(
2319+
2320+
//@todo: avoid this expensive semantic look ahead
2321+
LOOKAHEAD( [ WithList() ] FromQuery()) (
2322+
[ with=WithList() ]
2323+
select = FromQuery()
2324+
)
2325+
|
23192326
(
23202327
[ with=WithList() ]
23212328
(
@@ -2335,8 +2342,6 @@ Select Select() #Select:
23352342
[ LOOKAHEAD(<K_FETCH>) fetch = Fetch() { select.setFetch(fetch);} ]
23362343
[ LOOKAHEAD(<K_WITH> <K_ISOLATION>) withIsolation = WithIsolation() { select.setIsolation(withIsolation);} ]
23372344
)
2338-
|
2339-
select = FromQuery()
23402345
)
23412346
{
23422347
linkAST(select, jjtThis);
@@ -3166,7 +3171,8 @@ List<WithItem<?>> WithList():
31663171
WithItem with = null;
31673172
}
31683173
{
3169-
<K_WITH> with=WithItem() { withItemsList.add(with); } ("," with=WithItem() { withItemsList.add(with); } )*
3174+
<K_WITH> with=WithItem() { withItemsList.add(with); }
3175+
( LOOKAHEAD(3) "," with=WithItem() { withItemsList.add(with); } )*
31703176

31713177
{ return withItemsList; }
31723178
}

src/test/java/net/sf/jsqlparser/statement/piped/FromQueryTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,17 @@ void testParseAndDeparseIssue74() throws JSQLParserException {
231231
// formatter:on
232232
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
233233
}
234+
235+
@Test
236+
void testParseAndDeparseNestedWithIssue2168() throws JSQLParserException {
237+
// formatter:off
238+
String sqlStr =
239+
"with b as (\n" +
240+
" with a as (select 1)\n" +
241+
" from a )\n" +
242+
"from b\n" +
243+
";";
244+
// formatter:on
245+
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
246+
}
234247
}

0 commit comments

Comments
 (0)