Skip to content

Commit 99e90b0

Browse files
fix: keep BigQuery array subscripts and IF() inside array literals parsing
1 parent 1e5321a commit 99e90b0

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,20 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
20292029
}
20302030
}
20312031

2032+
/**
2033+
* Whether the "[" follows an operand (a name, a call or another subscript) and so opens a subscript,
2034+
* rather than an array literal. The token before it is found from the last consumed token, as the
2035+
* parser may be scanning ahead.
2036+
*/
2037+
private boolean isSubscriptOpening(Token opening) {
2038+
Token before = null;
2039+
for (Token t = token; t != null && t != opening; t = t.next) {
2040+
before = t;
2041+
}
2042+
return before != null && (before.kind == S_IDENTIFIER || before.kind == S_QUOTED_IDENTIFIER
2043+
|| ")".equals(before.image) || "]".equals(before.image));
2044+
}
2045+
20322046
/**
20332047
* Determines whether a reserved keyword token can be treated as an unquoted
20342048
* identifier in the current parser position.
@@ -2064,7 +2078,7 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
20642078
if (!prev.image.isEmpty()) {
20652079
switch (prev.image.charAt(0)) {
20662080
// Structural delimiters
2067-
case '.': case ',': case ':': case '(': case '=': case '[':
2081+
case '.': case ',': case ':': case '(': case '=':
20682082
// Comparison and arithmetic operators
20692083
case '>': case '<': case '!': case '^':
20702084
case '+': case '-': case '*': case '/': case '%':
@@ -2073,6 +2087,12 @@ public class CCJSqlParser extends AbstractJSqlParser<CCJSqlParser> {
20732087
}
20742088
}
20752089

2090+
// After the "[" of an array literal an expression starts, so any keyword may name a function,
2091+
// as in [IF(a, b, c)]. After the "[" of a subscript the keyword keeps its meaning (arr[OFFSET(1)]).
2092+
if ("[".equals(prev.image) && !isSubscriptOpening(prev)) {
2093+
return true;
2094+
}
2095+
20762096
// ── 3. If followed by . or = the keyword is a name/property key ──
20772097
Token next2 = getToken(2);
20782098
if (next2.image != null
@@ -12556,7 +12576,8 @@ Column Column() #Column :
1255612576
// @todo: we better should return a SEQUENCE instead of a COLUMN
1255712577
[ LOOKAHEAD(2) "." <K_NEXTVAL> { data.getNames().add("nextval"); name = getToken(0); } ]
1255812578

12559-
[ LOOKAHEAD(2) arrayConstructor = Subscript() ]
12579+
// BigQuery's arr[OFFSET(n)] is an ArrayExpression with the OFFSET accessor, not a column subscript.
12580+
[ LOOKAHEAD(2, { !(getToken(2).kind == K_OFFSET && "(".equals(getToken(3).image)) }) arrayConstructor = Subscript() ]
1256012581
{
1256112582
Column col = createColumn(data.getNames(), data.getDelimiters());
1256212583
if (tk != null) { col.withCommentText(tk.image); }

0 commit comments

Comments
 (0)