Skip to content

Commit e716d5b

Browse files
fix: allow if after a struct keyword (#2960)
* fix: allow if after a struct keyword * chore: spotlessApply
1 parent 67c6fe6 commit e716d5b

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,6 +2135,13 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
21352135
return true;
21362136
}
21372137

2138+
// BigQuery SELECT AS STRUCT / AS VALUE IF(...): STRUCT and VALUE can also be plain
2139+
// identifiers, so only the IF( function call is accepted after them.
2140+
if ((prev.kind == K_STRUCT || prev.kind == K_VALUE)
2141+
&& getToken(1).kind == K_IF && getToken(2).kind == OPENING_BRACKET) {
2142+
return true;
2143+
}
2144+
21382145
// ── 5. Conservative default: fall back to follower check ──────
21392146
return isReservedKeywordSafeByFollower();
21402147
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,21 @@ void testCreateTableCloneWithOptions() throws JSQLParserException {
340340
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
341341
}
342342

343+
@Test
344+
void testCreateTableAsSelectAsStructWithIfFunction() throws JSQLParserException {
345+
String sqlStr = "CREATE OR REPLACE TABLE p.d.t AS\n"
346+
+ "SELECT ARRAY(SELECT AS STRUCT IF(x.l, 'a', 'b') AS label, x.prob AS prob FROM UNNEST(s.arr) x) AS probs\n"
347+
+ "FROM p.d.src s";
348+
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
349+
}
350+
351+
@Test
352+
void testSelectAsValueWithIfFunction() throws JSQLParserException {
353+
String sqlStr =
354+
"SELECT ARRAY(SELECT AS VALUE IF(x.l, 'a', 'b') FROM UNNEST(s.arr) x) FROM t s";
355+
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
356+
}
357+
343358
@Test
344359
void testCloneRemainsUsableAsIdentifier() throws JSQLParserException {
345360
String sqlStr = "SELECT clone FROM t";

0 commit comments

Comments
 (0)