sql-metadata icon indicating copy to clipboard operation
sql-metadata copied to clipboard

Enhancement: GCP BigQuery UNNEST syntax

Open Jerodsun opened this issue 3 years ago • 2 comments

Hi,

Thank you for supporting this repo. GCP BigQuery has nested arrays that are flattened via the UNNEST operator. Time pending, I'd like to explore a method to add functionality to process UNNEST in queries. For example, given

SELECT A, B, metrics.C, metrics.D
FROM table,
UNNEST(metrics) as metrics

the parser for this query includes UNNEST and metrics as table names. A simple solution for my use case would be to simply remove the UNNEST group with regex. There are definitely better ways to integrate this that I can test first.

Jerodsun avatar Oct 26 '22 19:10 Jerodsun

HI, what's going on?

river2022next avatar Jul 05 '23 04:07 river2022next

Additionally, UNNEST WITH OFFSET fails to parse:

Example:

sql = f"""
SELECT A, B, metrics.C, metrics.D
FROM table,
UNNEST(metrics) with offset as metrics
"""

p = Parser(sql)

p.tables

Error:

with_names
    token.next_token_not_comment.normalized
AttributeError: 'NoneType' object has no attribute 'normalized'

The workaround would be something like this:

sql = f"""
WITH UnnestedMetrics AS (
    SELECT A, B, metrics, ARRAY_POSITION(metrics, metrics.C) AS offset
    FROM table,
    UNNEST(metrics) as metrics
)
SELECT A, B, metrics.C, metrics.D
FROM UnnestedMetrics
"""

p = Parser(sql)
p.tables

Result:

['table', 'UNNEST', 'metrics']

akburner avatar Oct 26 '23 11:10 akburner