From 9ee0adb9dd72b3a10bf37d5474e4ea88b41e9bc5 Mon Sep 17 00:00:00 2001 From: harryfoster2006 Date: Tue, 24 Nov 2026 13:13:10 +0000 Subject: [PATCH 1/2] fix: bigquery throws on if as first argument --- .../net/sf/jsqlparser/parser/JSqlParserCC.jjt | 2 +- .../select/BigQueryArraySubselectTest.java | 86 +++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/test/java/net/sf/jsqlparser/statement/select/BigQueryArraySubselectTest.java diff --git a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt index 0925d0511..a59fea642 100644 --- a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt +++ b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt @@ -2064,7 +2064,7 @@ public class CCJSqlParser extends AbstractJSqlParser { if (!prev.image.isEmpty()) { switch (prev.image.charAt(0)) { // Structural delimiters - case '.': case ',': case ':': case '(': case '=': + case '.': case ',': case ':': case '(': case '=': case '[': // Comparison and arithmetic operators case '>': case '<': case '!': case '^': case '+': case '-': case '*': case '/': case '%': diff --git a/src/test/java/net/sf/jsqlparser/statement/select/BigQueryArraySubselectTest.java b/src/test/java/net/sf/jsqlparser/statement/select/BigQueryArraySubselectTest.java new file mode 100644 index 000000000..3f05cca72 --- /dev/null +++ b/src/test/java/net/sf/jsqlparser/statement/select/BigQueryArraySubselectTest.java @@ -0,0 +1,86 @@ +/*- + * #%L + * JSQLParser library + * %% + * Copyright (C) 2004 - 2025 JSQLParser + * %% + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 + * #L% + */ +package net.sf.jsqlparser.statement.select; + +import java.util.stream.Stream; +import net.sf.jsqlparser.JSQLParserException; +import net.sf.jsqlparser.parser.CCJSqlParserUtil; +import net.sf.jsqlparser.test.TestUtils; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +class BigQueryArraySubselectTest { + + /** + * IF is a reserved keyword and a function name. Directly after "[" it has to be read as a + * function call, not rejected as a keyword. + */ + @ParameterizedTest + @ValueSource(strings = { + // IF as the first and only array element + "SELECT [IF(a > 1, 'x', NULL)] AS arr FROM t", + // IF as the first of several elements + "SELECT [IF(a > 1, 'x', NULL), 'y'] AS arr FROM t", + // IF as a later element (the comma already allowed it) + "SELECT ['a', IF(a > 1, 'x', NULL)] AS arr FROM t", + // inside UNNEST + "SELECT * FROM UNNEST([IF(a > 1, 'x', NULL)]) w", + "SELECT * FROM UNNEST([IF(a > 1, 'x', NULL), 'y']) w", + // inside another function call + "SELECT ARRAY_LENGTH([IF(a > 1, 'x', NULL)]) FROM t", + // inside an ARRAY subquery + "SELECT ARRAY(SELECT w FROM UNNEST([IF(a > 1, 'x', NULL)]) w) AS flags FROM t", + "SELECT ARRAY(SELECT w FROM UNNEST(['a', IF(a > 1, 'x', NULL)]) w) AS flags FROM t", + // other expressions first + "SELECT ARRAY(SELECT w FROM UNNEST([CASE WHEN a > 1 THEN 'x' END, 'y']) w) AS flags " + + "FROM t"}) + void parsesIfAsFirstArrayElement(String sqlStr) throws JSQLParserException { + TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true); + } + + /** + * A keyword right after "[" in a subscript must keep its keyword meaning. + */ + @ParameterizedTest + @ValueSource(strings = { + "SELECT arr[OFFSET(1)] FROM t", + "SELECT arr[SAFE_OFFSET(1)] FROM t", + "SELECT arr[ORDINAL(1)] FROM t"}) + void subscriptKeywordsAreUnaffected(String sqlStr) throws JSQLParserException { + TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true); + } + + /** + * A backquoted dotted name is split into its parts and quoted again on output. + */ + @ParameterizedTest + @MethodSource("createTableAsSelect") + void parsesCreateTableAsSelect(String sqlStr, String expected) throws JSQLParserException { + TestUtils.assertStatementCanBeDeparsedAs(CCJSqlParserUtil.parse(sqlStr), expected, true); + } + + static Stream createTableAsSelect() { + return Stream.of( + Arguments.of( + "CREATE OR REPLACE TABLE `p.d.t` AS\n" + + "SELECT ARRAY(SELECT w FROM UNNEST([IF(a > 1, 'x', NULL)]) w) AS flags\n" + + "FROM `p.d.src`", + "CREATE OR REPLACE TABLE \"p\".\"d\".\"t\" AS\n" + + "SELECT ARRAY(SELECT w FROM UNNEST([IF(a > 1, 'x', NULL)]) w) AS flags\n" + + "FROM \"p\".\"d\".\"src\""), + Arguments.of( + "CREATE OR REPLACE TABLE `p.d.t` AS " + + "SELECT [IF(a > 1, 'x', NULL), 'y'] AS arr FROM `p.d.src`", + "CREATE OR REPLACE TABLE \"p\".\"d\".\"t\" AS " + + "SELECT [IF(a > 1, 'x', NULL), 'y'] AS arr FROM \"p\".\"d\".\"src\"")); + } +} From 99e90b0254ecda7a835b36a8a43ae72434128bde Mon Sep 17 00:00:00 2001 From: harryfoster2006 Date: Thu, 26 Nov 2026 13:47:49 +0000 Subject: [PATCH 2/2] fix: keep BigQuery array subscripts and IF() inside array literals parsing --- .../net/sf/jsqlparser/parser/JSqlParserCC.jjt | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt index 637c4a76f..f959e602a 100644 --- a/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt +++ b/src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt @@ -2029,6 +2029,20 @@ public class CCJSqlParser extends AbstractJSqlParser { } } + /** + * Whether the "[" follows an operand (a name, a call or another subscript) and so opens a subscript, + * rather than an array literal. The token before it is found from the last consumed token, as the + * parser may be scanning ahead. + */ + private boolean isSubscriptOpening(Token opening) { + Token before = null; + for (Token t = token; t != null && t != opening; t = t.next) { + before = t; + } + return before != null && (before.kind == S_IDENTIFIER || before.kind == S_QUOTED_IDENTIFIER + || ")".equals(before.image) || "]".equals(before.image)); + } + /** * Determines whether a reserved keyword token can be treated as an unquoted * identifier in the current parser position. @@ -2064,7 +2078,7 @@ public class CCJSqlParser extends AbstractJSqlParser { if (!prev.image.isEmpty()) { switch (prev.image.charAt(0)) { // Structural delimiters - case '.': case ',': case ':': case '(': case '=': case '[': + case '.': case ',': case ':': case '(': case '=': // Comparison and arithmetic operators case '>': case '<': case '!': case '^': case '+': case '-': case '*': case '/': case '%': @@ -2073,6 +2087,12 @@ public class CCJSqlParser extends AbstractJSqlParser { } } + // After the "[" of an array literal an expression starts, so any keyword may name a function, + // as in [IF(a, b, c)]. After the "[" of a subscript the keyword keeps its meaning (arr[OFFSET(1)]). + if ("[".equals(prev.image) && !isSubscriptOpening(prev)) { + return true; + } + // ── 3. If followed by . or = the keyword is a name/property key ── Token next2 = getToken(2); if (next2.image != null @@ -12556,7 +12576,8 @@ Column Column() #Column : // @todo: we better should return a SEQUENCE instead of a COLUMN [ LOOKAHEAD(2) "." { data.getNames().add("nextval"); name = getToken(0); } ] - [ LOOKAHEAD(2) arrayConstructor = Subscript() ] + // BigQuery's arr[OFFSET(n)] is an ArrayExpression with the OFFSET accessor, not a column subscript. + [ LOOKAHEAD(2, { !(getToken(2).kind == K_OFFSET && "(".equals(getToken(3).image)) }) arrayConstructor = Subscript() ] { Column col = createColumn(data.getNames(), data.getDelimiters()); if (tk != null) { col.withCommentText(tk.image); }