Return the keys of a map as an array.
MAP_KEYS(map)
## Overview Returns the keys of a map as an ARRAY<K>. MAP_KEYS is the canonical way to inspect the key set of a map-typed column and is useful for bridging to array-based predicates such as ARRAY_CONTAINS and ARRAY_OVERLAP. Typical uses include enumerating keys for serialisation, collecting the union of keys across grouped maps, and feeding keys to an EXPLODE for a row-level denormalisation. ## Behavior - Returns an ARRAY<K> containing every key in the map, with no duplicates (maps cannot have duplicate keys). - Iteration order is not guaranteed; different invocations may produce differently-ordered arrays. - Length of the result equals the map's cardinality. - Keys are non-NULL; this engine does not permit NULL keys in maps. - Does not mutate the input. ## Null and empty handling - NULL input map returns NULL. - Empty input map returns an empty array of the key type. - Keys themselves are never NULL by construction. - Ordering of keys is unspecified; wrap with ARRAY_SORT for deterministic output. ## Compatibility - Matches the array/map SQL convention for MAP_KEYS. Equivalent to TRANSFORM(MAP_ENTRIES(m), e -> e.key).
| Name | Type | Description |
|---|---|---|
map | Specifies the input map. |
-- Keys from a string-keyed map
SELECT MAP_KEYS(MAP('a', 1, 'b', 2, 'c', 3)); -- ['a', 'b', 'c']
-- Integer keys
SELECT MAP_KEYS(MAP(1, 'x', 2, 'y')); -- [1, 2]
-- Empty map returns an empty array
SELECT MAP_KEYS(CAST(MAP() AS MAP<STRING, INT>)); -- []
-- NULL map returns NULL
SELECT MAP_KEYS(CAST(NULL AS MAP<STRING, INT>)); -- NULL
-- Materialise the set of keys across per-session attribute maps
SELECT user_id, ARRAY_DISTINCT(FLATTEN(ARRAY_AGG(MAP_KEYS(attributes)))) AS all_keys
FROM analytics.events.user_sessions
GROUP BY user_id;
-- Filter sessions whose attribute map contains a specific key using MAP_KEYS
SELECT session_id FROM analytics.events.user_sessions
WHERE ARRAY_CONTAINS(MAP_KEYS(attributes), 'campaign_id');