Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,20 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
}
}

/**
* 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.
Expand Down Expand Up @@ -2073,6 +2087,12 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
}
}

// 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
Expand Down Expand Up @@ -12556,7 +12576,8 @@ Column Column() #Column :
// @todo: we better should return a SEQUENCE instead of a COLUMN
[ LOOKAHEAD(2) "." <K_NEXTVAL> { 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); }
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Arguments> 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\""));
}
}
Loading