Map Functions and Operators

Subscript Operator: []

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

  1. 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.

  1. SELECT map(); -- {}

map(array(K), array(V)) -> map(K, V)

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

  1. 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.

  1. SELECT map_from_entries(ARRAY[(1, 'x'), (2, 'y')]); -- {1 -> 'x', 2 -> 'y'}

multimap_from_entries(array(row(K, V))) -> map(K, array(V))

Returns a multimap created from the given array of entries. Each key can be associated with multiple values.

  1. SELECT multimap_from_entries(ARRAY[(1, 'x'), (2, 'y'), (1, 'z')]); -- {1 -> ['x', 'z'], 2 -> ['y']}

map_entries(map(K, V)) -> array(row(K, V))

Returns an array of all entries in the given map.

  1. 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(K, V, boolean)) -> map(K, V)

Constructs a map from those entries of map for which function returns true:

  1. SELECT map_filter(MAP(ARRAY[], ARRAY[]), (k, v) -> true); -- {}
  2. SELECT map_filter(MAP(ARRAY[10, 20, 30], ARRAY['a', NULL, 'c']), (k, v) -> v IS NOT NULL); -- {10 -> a, 30 -> c}
  3. SELECT map_filter(MAP(ARRAY['k1', 'k2', 'k3'], ARRAY[20, 3, 15]), (k, v) -> v > 10); -- {k1 -> 20, k3 -> 15}

map_remove_null_values(x(K, V)) -> map(K, V)

Removes all the entries where the value is null from the map x.

map_subset(map(K, V), array(k)) -> map(K, V)

Constructs a map from those entries of map for which the key is in the array given:

  1. SELECT map_subset(MAP(ARRAY[1,2], ARRAY['a','b']), ARRAY[10]); -- {}
  2. SELECT map_subset(MAP(ARRAY[1,2], ARRAY['a','b']), ARRAY[1]); -- {1->'a'}
  3. SELECT map_subset(MAP(ARRAY[1,2], ARRAY['a','b']), ARRAY[1,3]); -- {1->'a'}
  4. SELECT map_subset(MAP(ARRAY[1,2], ARRAY['a','b']), ARRAY[]); -- {}
  5. SELECT map_subset(MAP(ARRAY[], ARRAY[]), ARRAY[1,2]); -- {}

map_keys(x(K, V)) -> array(K)

Returns all the keys in the map x.

map_top_n_keys(x(K, V), n) -> array(K)

Returns top n keys in the map x. n must be a non-negative integer For bottom n keys, use the function with lambda operator to perform custom sorting

SELECT map_top_n_keys(map(ARRAY[‘a’, ‘b’, ‘c’], ARRAY[1, 2, 3]), 2) — [‘c’, ‘b’]

map_top_n_keys(x(K, V), n, function(K, K, int)) -> array(K)

Returns top n keys in the map x based on the given comparator function. The comparator will take two non-nullable arguments representing two keys of the map. It returns -1, 0, or 1 as the first key is less than, equal to, or greater than the second key. If the comparator function returns other values (including NULL), the query will fail and raise an error

  1. SELECT map_top_n_keys(map(ARRAY['a', 'b', 'c'], ARRAY[1, 2, 3]), 2, (x, y) -> IF(x < y, -1, IF(x = y, 0, 1))) --- ['c', 'b']

map_top_n(x(K, V), n) -> map(K, V)

Truncates map items. Keeps only the top N elements by value. n must be a non-negative integer

SELECT map_top_n(map(ARRAY[‘a’, ‘b’, ‘c’], ARRAY[2, 3, 1]), 2) — {‘b’ -> 3, ‘a’ -> 2}

map_normalize(x(varchar, double)) -> map(varchar, double)

Returns the map with the same keys but all non-null values are scaled proportionally so that the sum of values becomes 1. Map entries with null values remain unchanged.

map_values(x(K, V)) -> array(V)

Returns all the values in the map x.

map_top_n_values(x(K, V), n) -> array(K)

Returns top n values in the map x. n must be a positive integer For bottom n values, use the function with lambda operator to perform custom sorting

SELECT map_top_n_values(map(ARRAY[‘a’, ‘b’, ‘c’], ARRAY[1, 2, 3]), 2) — [3, 2]

map_top_n_values(x(K, V), n, function(V, V, int)) -> array(V)

Returns top n values in the map x based on the given comparator function. The comparator will take two nullable arguments representing two values of the map. It returns -1, 0, or 1 as the first value is less than, equal to, or greater than the second value. If the comparator function returns other values (including NULL), the query will fail and raise an error

  1. SELECT map_top_n_values(map(ARRAY['a', 'b', 'c'], ARRAY[1, 2, 3]), 2, (x, y) -> IF(x < y, -1, IF(x = y, 0, 1))) --- [3, 2]

map_zip_with(map(K, V1), map(K, V2), function(K, V1, V2, V3)) -> map(K, V3)

Merges the two given maps into a single map by applying function to the pair of values with the same key. For keys only presented in one map, NULL will be passed as the value for the missing key.

  1. SELECT map_zip_with(MAP(ARRAY[1, 2, 3], ARRAY['a', 'b', 'c']), -- {1 -> ad, 2 -> be, 3 -> cf}
  2. MAP(ARRAY[1, 2, 3], ARRAY['d', 'e', 'f']),
  3. (k, v1, v2) -> concat(v1, v2));
  4. SELECT map_zip_with(MAP(ARRAY['k1', 'k2'], ARRAY[1, 2]), -- {k1 -> ROW(1, null), k2 -> ROW(2, 4), k3 -> ROW(null, 9)}
  5. MAP(ARRAY['k2', 'k3'], ARRAY[4, 9]),
  6. (k, v1, v2) -> (v1, v2));
  7. SELECT map_zip_with(MAP(ARRAY['a', 'b', 'c'], ARRAY[1, 8, 27]), -- {a -> a1, b -> b4, c -> c9}
  8. MAP(ARRAY['a', 'b', 'c'], ARRAY[1, 2, 3]),
  9. (k, v1, v2) -> k || CAST(v1/v2 AS VARCHAR));

transform_keys(map(K1, V), function(K1, V, K2)) -> map(K2, V)

Returns a map that applies function to each entry of map and transforms the keys:

  1. SELECT transform_keys(MAP(ARRAY[], ARRAY[]), (k, v) -> k + 1); -- {}
  2. SELECT transform_keys(MAP(ARRAY [1, 2, 3], ARRAY ['a', 'b', 'c']), (k, v) -> k + 1); -- {2 -> a, 3 -> b, 4 -> c}
  3. SELECT transform_keys(MAP(ARRAY ['a', 'b', 'c'], ARRAY [1, 2, 3]), (k, v) -> v * v); -- {1 -> 1, 4 -> 2, 9 -> 3}
  4. SELECT transform_keys(MAP(ARRAY ['a', 'b'], ARRAY [1, 2]), (k, v) -> k || CAST(v as VARCHAR)); -- {a1 -> 1, b2 -> 2}
  5. SELECT transform_keys(MAP(ARRAY [1, 2], ARRAY [1.0, 1.4]), -- {one -> 1.0, two -> 1.4}
  6. (k, v) -> MAP(ARRAY[1, 2], ARRAY['one', 'two'])[k]);

transform_values(map(K, V1), function(K, V1, V2)) -> map(K, V2)

Returns a map that applies function to each entry of map and transforms the values:

  1. SELECT transform_values(MAP(ARRAY[], ARRAY[]), (k, v) -> v + 1); -- {}
  2. SELECT transform_values(MAP(ARRAY [1, 2, 3], ARRAY [10, 20, 30]), (k, v) -> v + k); -- {1 -> 11, 2 -> 22, 3 -> 33}
  3. SELECT transform_values(MAP(ARRAY [1, 2, 3], ARRAY ['a', 'b', 'c']), (k, v) -> k * k); -- {1 -> 1, 2 -> 4, 3 -> 9}
  4. SELECT transform_values(MAP(ARRAY ['a', 'b'], ARRAY [1, 2]), (k, v) -> k || CAST(v as VARCHAR)); -- {a -> a1, b -> b2}
  5. SELECT transform_values(MAP(ARRAY [1, 2], ARRAY [1.0, 1.4]), -- {1 -> one_1.0, 2 -> two_1.4}
  6. (k, v) -> MAP(ARRAY[1, 2], ARRAY['one', 'two'])[k] || '_' || CAST(v AS VARCHAR));