Skip to content

fix: bigquery throws on if as first argument - #2959

Open
harryfoster2006 wants to merge 3 commits into
JSQLParser:masterfrom
harryfoster2006:fix/bigquery-if-as-first-argument
Open

harryfoster2006 wants to merge 3 commits into
JSQLParser:masterfrom
harryfoster2006:fix/bigquery-if-as-first-argument

Conversation

@harryfoster2006

@harryfoster2006 harryfoster2006 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

In BigQuery, IF as the first element of an array literal inside a function call fails to parse:

CREATE OR REPLACE TABLE `p.d.t` AS
SELECT ARRAY(SELECT w FROM UNNEST([IF(a > 1, 'x', NULL)]) w) AS flags
FROM `p.d.src`

Encountered: "[" / "[", at line 2, column 35
Was expecting one of: <K_RESPECT>, <K_HAVING>, <K_IGNORE>, <K_LIMIT>, <CLOSING_BRACKET> ...

The same statement parses when IF isn't the first element, e.g. UNNEST(['a', IF(...)]).

Cause

IF is a reserved keyword, so RelObjectName only accepts it as a function name when isReservedKeywordAsIdentifier() allows it. That guard checks the previous token, and [ was not in its list of delimiters that start an expression position (., ,, :, (, =, operators). [ fell through to isReservedKeywordSafeByFollower(), which rejects IF when followed by (.

InternalFunction uses a syntactic LOOKAHEAD(2) to decide whether to enter its argument list. That probe runs the guard, so it failed, the whole optional argument block was skipped, and the parser then hit [ where it expected ). With , before IF the guard already returned true, which is why only the first-element case failed.

Fix

[ is no longer treated as an unconditional delimiter. A new helper, isSubscriptOpening(), tells the two kinds of [ apart by the token before it:

  • After an operand (a name, a quoted name, ) or ]) the [ opens a subscript, and keywords keep their existing meaning.
  • Otherwise it opens an array literal, so an expression starts and any keyword may name a function: [IF(a, b, c)].

Treating every [ as an expression start would have broken BigQuery's arr[OFFSET(1)], because OFFSET would then be read as a function name inside a plain column subscript. To keep that working, Column() now skips its Subscript() branch when the bracket contains OFFSET(. The expression falls through to the ArrayExpression path with the OFFSET accessor, as it did before.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant