diff --git a/pegjs/athena.pegjs b/pegjs/athena.pegjs index d40944cb..12985183 100644 --- a/pegjs/athena.pegjs +++ b/pegjs/athena.pegjs @@ -1102,7 +1102,7 @@ with_offset } from_unnest_item - = 'UNNEST'i __ LPAREN __ a:expr? __ RPAREN __ alias:alias_clause? __ wf:with_offset? { + = 'UNNEST'i __ LPAREN __ a:expr? __ RPAREN __ alias:(func_call / alias_clause)? __ wf:with_offset? { return { type: 'unnest', expr: a, diff --git a/test/athena.spec.js b/test/athena.spec.js index c62afd69..b76865ee 100644 --- a/test/athena.spec.js +++ b/test/athena.spec.js @@ -332,5 +332,10 @@ describe('athena', () => { WHERE i.field = 'status'` expect(getParsedSql(sql)).to.be.equal("SELECT `j`.`id`, `h`.`created` AS `change_time`, `i`.`fromstring` AS `from_status`, `i`.`tostring` AS `to_status` FROM `bronze_prod`.`jira_issues` AS `j` CROSS JOIN UNNEST(`j`.`changelog`.`histories`) AS T(`h`) CROSS JOIN UNNEST(`h`.`items`) AS T(`i`) WHERE `i`.`field` = 'status'") + sql = `SELECT id, array_agg(json_extract_scalar(elem, '$.value')) er_teams + FROM "bronze_prod"."jira_issues" + CROSS JOIN UNNEST(cast(json_extract(json_parse(fields), '$.customfield_10100') AS array(json))) AS t(elem) + GROUP BY id` + expect(getParsedSql(sql)).to.be.equal("SELECT `id`, array_agg(json_extract_scalar(`elem`, '$.value')) AS `er_teams` FROM `bronze_prod`.`jira_issues` CROSS JOIN UNNEST(CAST(json_extract(json_parse(`fields`), '$.customfield_10100') AS ARRAY(JSON))) AS t(`elem`) GROUP BY `id`") }) }) \ No newline at end of file