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.
Asked
Active
Viewed 3.9k times
2 Answers
41
You can use the cardinality
function: https://prestodb.io/docs/current/functions/array.html#cardinality
select cardinality(array[]) = 0;
_col0
-------
true
(1 row)

FreePeter
- 752
- 6
- 6
6
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)

Piotr Findeisen
- 19,480
- 2
- 52
- 82
-
I got `'=' cannot be applied to array(row(value integer)), array(row(VALUE integer))`, do you know what could be wrong? – Leo Jiang May 30 '17 at 17:41
-
@LeoJiang how can I reproduce? I tried `select array[cast(row(1) as row(value integer))] = array[];` and it correctly returned `false` (in Presto `master`) – Piotr Findeisen May 30 '17 at 18:40
-
We might be using a weird version of Presto, I found another solution by casting the map to JSON and then casting it to a string. Thanks. – Leo Jiang May 30 '17 at 18:51