Skip to content

fix: allow if after a struct keyword - #2960

Open
harryfoster2006 wants to merge 1 commit into
JSQLParser:masterfrom
harryfoster2006:fix/bigquery-parse-fail-select-struct-as-if
Open

harryfoster2006 wants to merge 1 commit into
JSQLParser:masterfrom
harryfoster2006:fix/bigquery-parse-fail-select-struct-as-if

Conversation

@harryfoster2006

Copy link
Copy Markdown
Contributor

BigQuery's IF() function failed to parse as the first select item after SELECT AS STRUCT or SELECT AS VALUE:

CREATE OR REPLACE TABLE p.d.t AS
SELECT ARRAY(SELECT AS STRUCT IF(x.l, 'a', 'b') AS label, x.prob AS prob
             FROM UNNEST(s.arr) x) AS probs
FROM p.d.src s

This failed with ParseException: Encountered: , at line 2, column 31, the position of IF.

Cause

IF is a reserved word. isReservedKeywordAsIdentifier() accepts it as a name unconditionally after structural keywords such as DISTINCT, which is why SELECT DISTINCT IF(...) already worked. STRUCT and VALUE are not in that list, so the check fell through to the follower-based rule, which rejects IF (.

Fix

Add a narrow rule to isReservedKeywordAsIdentifier(): IF followed by ( is accepted right after STRUCT or VALUE.

STRUCT and VALUE are deliberately not added to the unconditional keyword list. They are also valid column names, so a following keyword such as LIMIT could otherwise be misread as an alias.

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