Skip to main content

explode_projection_to_unnest

Function explode_projection_to_unnest 

Source
pub fn explode_projection_to_unnest(
    expr: Expression,
    target: DialectType,
) -> Result<Expression>
Expand description

Convert EXPLODE/POSEXPLODE in SELECT projections into CROSS JOIN UNNEST patterns.

This implements the explode_projection_to_unnest transform from Python sqlglot. It restructures queries like: SELECT EXPLODE(x) FROM tbl into: SELECT IF(pos = pos_2, col, NULL) AS col FROM tbl CROSS JOIN UNNEST(...) AS pos CROSS JOIN UNNEST(x) AS col WITH OFFSET AS pos_2 WHERE ...

The transform handles:

  • EXPLODE(x) and POSEXPLODE(x) functions
  • Name collision avoidance (_u, _u_2, … and col, col_2, …)
  • Multiple EXPLODE/POSEXPLODE in one SELECT
  • Queries with or without FROM clause
  • Presto (index_offset=1) and BigQuery (index_offset=0) variants