3

I am using unnest for more flatten more than one array in Athena query. When the array has some records it returns the correct result. But when the second array is empty it is returning no records. Can someone please let me know how to do unnest to unnest more than one array in a single query?

The following query returns empty row.

WITH example AS (
 SELECT devop, devs 
 FROM

   UNNEST(ARRAY['Sharon', 'John', 'Bob', 'Sally']) AS t(devop),
     UNNEST(ARRAY[]) AS t(devs)
)
select array_join(array_agg(distinct example.devop),';'),array_join(array_agg(distinct example.devs),';') from example

The following query returns the correct result.

WITH example AS (
 SELECT devop, devs 
 FROM

   UNNEST(ARRAY['Sharon', 'John', 'Bob', 'Sally']) AS t(devop),
     UNNEST(ARRAY['a','b']) AS t(devs)
)
select array_join(array_agg(distinct example.devop),';'),array_join(array_agg(distinct example.devs),';') from example

When the second array is empty I want the following result

_col0                             _col1
----------------------------------------------
Sally;John;Bob;Sharon              
GMB
  • 216,147
  • 25
  • 84
  • 135
somename
  • 978
  • 11
  • 30

4 Answers4

5

Since LEFT JOIN UNNEST doesn't work on Athena Presto, you can cross-join on null values using the IF and CARDINALITY like this:

WITH example AS (
  SELECT devop, devs 
  FROM UNNEST(ARRAY['Sharon', 'John', 'Bob', 'Sally']) AS t(devop) CROSS JOIN
       UNNEST(IF(CARDINALITY(ARRAY[])=0, ARRAY[NULL], ARRAY[]) AS t(devs)
)

This way, if the ARRAY[]/Column is empty (CARDINALITY checks the size of array), ARRAY[NULL] is returned and the row is not skipped.

(tested on Presto 0.217 / Athena engine version 2)

BillaD
  • 86
  • 1
  • 5
4

Use a left join:

WITH example AS (
      SELECT devop, devs 
      FROM UNNEST(ARRAY['Sharon', 'John', 'Bob', 'Sally']) AS t(devop) LEFT JOIN
           UNNEST(ARRAY[]) AS t(devs)
           ON 1=1
    )
. . .
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • 1
    `LEFT JOIN UNNEST` is the proper answer, and this problem is exactly why we implemented it! Note that you need Presto 323 or newer, so in particular it does not work in Athena yet. – Piotr Findeisen May 03 '20 at 09:12
  • 3
    In Athena I get this error `SYNTAX_ERROR: line 4:12: UNNEST on other than the right side of CROSS JOIN is not supported` I tried the following command `WITH example AS ( SELECT devop, devs FROM UNNEST(ARRAY['Sharon', 'John', 'Bob', 'Sally']) AS t(devop) LEFT JOIN UNNEST(ARRAY[]) AS t(devs) ON 1=1) select array_join(array_agg(distinct example.devop),';'),array_join(array_agg(distinct example.devs),';') from example` – somename May 03 '20 at 23:20
  • 2
    Even Athena engine version 2 is based on Presto 0.217. So no way to do `LEFT JOIN UNNEST`. Any other workaround? – Jimson James Mar 04 '21 at 19:51
2

I don't think that you want a cross join here. Instead, you could phrase this as:

select 
    array_join(
        array_agg(distinct unnest(array['Sharon', 'John', 'Bob', 'Sally'])),
        ';'
    ) devops,
    array_join(
        array_agg(distinct unnest(array[])),
        ';'
    ) devs
GMB
  • 216,147
  • 25
  • 84
  • 135
2

Try to array_union with a null array,

Below query will give you the desired result. Tested on Athena engine version 1.

WITH example AS (
 SELECT devop, devs 
 FROM

   UNNEST(ARRAY['Sharon', 'John', 'Bob', 'Sally']) AS t(devop),
     UNNEST(array_union(ARRAY[], ARRAY[null])) AS t(devs)
)
select array_join(array_agg(distinct example.devop),';'),array_join(array_agg(distinct example.devs),';') from example
Jimson James
  • 2,937
  • 6
  • 43
  • 78