Skip to content

Commit 0c0989b

Browse files
edeadingtonclaude
andcommitted
Parse a parenthesed LATERAL FROM item
isParenthesedFromItemAhead() declines ParenthesedFromItem when the token after "(" is LATERAL, on the basis that LateralSubSelect handles it. LateralSubSelect begins at <K_LATERAL>, so it can never match input that begins at "(", and no later alternative accepts it either. Every parenthesed LATERAL item therefore fails at the opening parenthesis with "Encountered: <OPENING_BRACKET>". Remove the guard so ParenthesedFromItem is tried, as it was before 2d2ee4b replaced the syntactic LOOKAHEAD(ParenthesedFromItem()) with this two-token predicate. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 7123d25 commit 0c0989b

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,6 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
12021202
if (k2 == K_TABLE && isTableQueryAhead(2)) return false;
12031203
// ( ( SELECT ) UNION ( SELECT ) ) → isNestedSetOperationAhead handles it
12041204
// Other (( patterns: ( (SELECT...) JOIN ... ) or ( (table) ) → ParenthesedFromItem
1205-
if (k2 == K_LATERAL) return false; // handled by LateralSubSelect
12061205
if (k2 == CLOSING_BRACKET) return false; // empty "()" is not valid
12071206
// EXASOL: ( IMPORT ... ) is a SubImport; handled at the outer FromItem level
12081207
// by LOOKAHEAD(2, Dialect.EXASOL...) SubImport() - must NOT go into ParenthesedFromItem

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,6 +2728,13 @@ public void testLateral1() throws JSQLParserException {
27282728
assertSqlCanBeParsedAndDeparsed(stmt);
27292729
}
27302730

2731+
@Test
2732+
public void testParenthesedLateral() throws JSQLParserException {
2733+
String stmt =
2734+
"SELECT O.ORDERID, O.CUSTNAME, OL.LINETOTAL FROM ORDERS AS O, (LATERAL(SELECT SUM(NETAMT) AS LINETOTAL FROM ORDERLINES AS LINES WHERE LINES.ORDERID = O.ORDERID)) AS OL";
2735+
assertSqlCanBeParsedAndDeparsed(stmt);
2736+
}
2737+
27312738
@Test
27322739
public void testLateralComplex1() throws IOException, JSQLParserException {
27332740
String stmt = IOUtils.toString(

‎src/test/java/net/sf/jsqlparser/statement/select/TableFunctionTest.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ void testLateralFlat() throws JSQLParserException {
3333
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
3434
}
3535

36+
/**
37+
* A LATERAL item may be wrapped in parentheses, in which case it is a parenthesed FROM item
38+
* whose content happens to be lateral, not a bare LateralSubSelect.
39+
*/
40+
@Test
41+
void testParenthesedLateralFlat() throws JSQLParserException {
42+
String sqlStr = "SELECT DIM, count(value) AS COUNT_\n"
43+
+ "FROM t a,\n"
44+
+ "(LATERAL FLATTEN(input => a.user_items)) b\n"
45+
+ "group by 1";
46+
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
47+
}
48+
3649
/**
3750
* The SQL keyword "OUTER" is a valid parameter name for Snowflake's FLATTEN table function.
3851
*/

0 commit comments

Comments
 (0)