Skip to content

Commit ea2d1d1

Browse files
fix: parse user variables with three or more name parts (#2949)
The assignment probe in SimpleExpression looked only 5 tokens ahead, so "@db.sch.stg" filled the window before the "=" or ":=" check and was committed to as an assignment. Scan the whole variable chain instead, so Snowflake stage references such as DIRECTORY(@db.sch.stg) parse. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2d407c0 commit ea2d1d1

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17136,7 +17136,10 @@ Expression SimpleExpression():
1713617136
Token first;
1713717137
}
1713817138
{
17139-
[ LOOKAHEAD( 5, { getToken(1).kind == S_AT_IDENTIFIER } ) user = UserVariable() ( operation = "=" | operation = ":=" ) ]
17139+
// Scan the whole variable chain before the operator, so "@db.sch.stg" is not
17140+
// mistaken for the start of an assignment.
17141+
[ LOOKAHEAD( UserVariable() ( "=" | ":=" ), { getToken(1).kind == S_AT_IDENTIFIER } )
17142+
user = UserVariable() ( operation = "=" | operation = ":=" ) ]
1714017143
{ first = getToken(1); }
1714117144
retval=PrimaryExpression()
1714217145
// PostgreSQL path operators bind below arithmetic and above comparisons.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4471,6 +4471,15 @@ public void testMultiPartNamesIssue608() throws JSQLParserException {
44714471
assertSqlCanBeParsedAndDeparsed("SELECT @@sessions.tx_read_only");
44724472
}
44734473

4474+
@Test
4475+
public void testMultiPartUserVariable() throws JSQLParserException {
4476+
assertSqlCanBeParsedAndDeparsed("SELECT @db.sch.stg");
4477+
assertSqlCanBeParsedAndDeparsed("SELECT @a.b.c.d");
4478+
assertSqlCanBeParsedAndDeparsed("SELECT * FROM DIRECTORY(@db.sch.stg)");
4479+
assertSqlCanBeParsedAndDeparsed("SELECT @db.sch.stg = 1");
4480+
assertSqlCanBeParsedAndDeparsed("SELECT @db.sch.stg := @db.sch.stg + 1 AS counter");
4481+
}
4482+
44744483
@Test
44754484
public void testMultiPartNamesForFunctionsIssue944() throws JSQLParserException {
44764485
assertSqlCanBeParsedAndDeparsed("SELECT pg_catalog.now()");

0 commit comments

Comments
 (0)