简体   繁体   中英

is there a way to unnest bigquery array independently?

Let's say I have this database:

with tbl as (
select 
  ['Unknown','Eletric','High Voltage'] AS product_category, 
  ['Premium','New'] as client_cluster
) select * from tbl 

And its output:

row | product_category                  | client_cluster
--------------------------------------------------------------
1   | [Unknown, Eletric, High Voltage]  | [Premium, New]

I would like to unnest this columns independently in a way that it will be then N rows where N would be the size of the biggest array I unnest and the output would look like this:

row | product_category     | client_cluster
---------------------------------------------
1   | Unknown              | Premium
2   | Eletric              | New
3   | High Voltage         | Null

And there is no order that I would like to imply. Is there a way to do that? I tried this stackoverflow but in my case it did not work as expected because of my arrays does not have the same size.

it did not work as expected because of my arrays does not have the same size.

for your specific sample in your question, you can left join unnested arrays.

WITH tbl AS (
  SELECT ['Unknown','Eletric','High Voltage'] AS product_category, ['Premium','New'] as client_cluster 
)
SELECT p AS product_category, c AS client_cluster 
  FROM tbl, UNNEST(product_category) p WITH offset
  LEFT JOIN UNNEST(client_cluster) c WITH offset USING (offset);

在此处输入图像描述

But the length of product_category is less than that of client_cluster , it won't work as you wish.

WITH tbl AS (
  SELECT ['Eletric','High Voltage'] AS product_category, ['Supreme', 'Premium','New'] as client_cluster 
)
SELECT p AS product_category, c AS client_cluster 
  FROM tbl, UNNEST(product_category) p WITH offset
  LEFT JOIN UNNEST(client_cluster) c WITH offset USING (offset);

在此处输入图像描述

I might be wrong, but as far as I know you can't use FULL JOIN or RIGHT JOIN with flattened array to solve this issue. If you try to do so, you will get:

Query error: Array scan is not allowed with FULL JOIN: UNNEST expression at [31:13]

So you might consider below workaround using a hidden table(array) for join key.

WITH tbl AS (
  SELECT 1 id, ['Unknown','Eletric','High Voltage'] AS product_category, ['Premium','New'] as client_cluster,
   UNION ALL
  SELECT 2 id, ['Eletric','High Voltage'], ['Premium','New', 'Supreme']
)
SELECT id, p AS product_category, c AS client_cluster 
  FROM tbl, UNNEST(GENERATE_ARRAY(0, GREATEST(ARRAY_LENGTH(client_cluster), ARRAY_LENGTH(product_category)) - 1)) o0
  LEFT JOIN UNNEST(product_category) p WITH offset o1 ON o0 = o1
  LEFT JOIN UNNEST(client_cluster) c WITH offset o2 ON o0 = o2;

Query results

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM