Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a map or array is empty in Presto?

Tags:

sql

presto

How do I check if a map has no keys in Presto? If I have a way to check if an array is empty, I can use the map_keys function to determine if the map is empty.

like image 214
Leo Jiang Avatar asked May 26 '17 01:05

Leo Jiang


2 Answers

You can use the cardinality function: https://prestodb.io/docs/current/functions/array.html#cardinality

select cardinality(array[]) = 0;
 _col0
-------
 true
(1 row)
like image 194
FreePeter Avatar answered Oct 24 '22 10:10

FreePeter


To check array is empty just compare it with = array[]. Example:

presto> select (map_keys(map(array[], array[])) = array[]) as is_empty;

 is_empty
----------
 true
(1 row)

Likewise, to check if a map is empty just compare it with = map(). Example:

presto> select (map(array[], array[]) = map()) as is_empty;

 is_empty
----------
 true
(1 row)
like image 33
Piotr Findeisen Avatar answered Oct 24 '22 12:10

Piotr Findeisen