14.16. Map Functions and Operators

Subscript Operator: []

The [] operator is used to retrieve the value corresponding to a given key from a map:

SELECT name_to_age_map['Bob'] AS bob_age;

Map Functions

cardinality(x) → bigint

Returns the cardinality (size) of the map x.

element_at(map<K, V>, key) → V

Returns value for given key, or NULL if the key is not contained in the map.

map() → map<unknown, unknown>

Returns an empty map.

SELECT map(); -- {}
map(array<K>, array<V>) → map<K,V>

Returns a map created using the given key/value arrays.

SELECT map(ARRAY[1,3], ARRAY[2,4]); -- {1 -> 2, 3 -> 4}

See also map_agg() and multimap_agg() for creating a map as an aggregation.

map_from_entries(array<row<K, V>>) → map<K,V>

Returns a map created from the given array of entries.

SELECT map_from_entries(ARRAY[(1, 'x'), (2, 'y')]); -- {1 -> 'x', 2 -> 'y'}
map_entries(map<K, V>) → array<row<K,V>>

Returns an array of all entries in the given map.

SELECT map_entries(MAP(ARRAY[1, 2], ARRAY['x', 'y'])); -- [ROW(1, 'x'), ROW(2, 'y')]
map_concat(map1<K, V>, map2<K, V>, ..., mapN<K, V>) → map<K,V>

Returns the union of all the given maps. If a key is found in multiple given maps, that key’s value in the resulting map comes from the last one of those maps.

map_filter(map<K, V>, function) → map<K,V>

See map_filter().

transform_keys(map<K1, V>, function) → MAP<K2,V>

See transform_keys().

transform_values(map<K, V1>, function) → MAP<K,V2>

See transform_values().

map_keys(x<K, V>) → array<K>

Returns all the keys in the map x.

map_values(x<K, V>) → array<V>

Returns all the values in the map x.