trinodb/trino

Make UNNEST with JOIN clearer in the documentation.

Aperta

#17.897 aperta il 14 giu 2023

 (10 commenti) (0 reazioni) (2 assegnatari)Java (2678 fork)batch import
docsgood first issue

Metriche repository

Star
 (9113 stelle)
Metriche merge PR
 (Merge medio 5g 9h) (306 PR mergiate in 30 g)

Descrizione

Taken from this awesome example from @kasiafi, I was initially confused when I ran the following query, I got the error message, Column 't2.y' cannot be resolved:

WITH
  t (x, y) AS (
    VALUES
      (1, ARRAY['a', 'b', 'c'])
  ),
  t2 (x, y) AS (
    VALUES
      (1, ARRAY['a', 'b', 'c'])
  ),
  t3 (x, y) AS (
    SELECT
      x,
      y_unnested
    FROM
      t
      CROSS JOIN UNNEST (t2.y) t4 (y_unnested)
  )
SELECT
  *
FROM
  t3;

Eventually I figured out that t3 query's FROM statement I was joining between two different tables. UNNEST (according to the docs) references relations from the left side of the join (which apparently means the tables in the join need to be the same). So I needed to be joining t2 on the left side with UNNEST(t2.y) rather than t on the left side.

  t3 (x, y) AS (
    SELECT
      x,
      y_unnested
    FROM
      t2
      CROSS JOIN UNNEST (t2.y) t4 (y_unnested)
  )

We should add a cleaned up example of this and make that requirement clearer.

Guida contributor