Skip to main content

Map Functions

NameDescription
cardinality(map)Return the size of the map (or the number of entries in the map).
element_at(map, key)Return the value for a given key as a list, or an empty list if the key is not contained in the map. The type of the key provided in the second parameter must match the type of the map's keys; else, an error is thrown.
map_concat(maps...)Returns a map created from merging the input maps. On key collision the value is taken from the last map with that key.
map_contains(map, key)Checks if a map contains a given key.
map_contains_entry(map, key, value)Check if a map contains a given key-value pair.
map_contains_value(map, value)Checks if a map contains a given value.
map_entries(map)Return a list of struct(k, v) for each key-value pair in the map.
map_extract(map, key)Return the value for a given key as a list, or an empty list if the key is not contained in the map. The type of the key provided in the second parameter must match the type of the map's keys; else, an error is thrown.
map_extract_value(map, key)Returns the value for a given key or NULL if the key is not contained in the map. The type of the key provided in the second parameter must match the type of the map's keys; else, an error is thrown.
map_from_entries(STRUCT(k, v)[])Returns a map created from the entries of the array.
map_keys(map)Return a list of all keys in the map.
map_values(map)Return a list of all values in the map.
map()Returns an empty map.
map[entry]Returns the value for a given key or NULL if the key is not contained in the map. The type of the key provided in the second parameter must match the type of the map's keys; else, an error is thrown.

cardinality(map)

Return the size of the map (or the number of entries in the map).

Query
SELECT cardinality(map([4, 2], ['a', 'b'])) AS cardinality;
Result
 cardinality-------------           2

element_at(map, key)

Return the value for a given key as a list, or an empty list if the key is not contained in the map. The type of the key provided in the second parameter must match the type of the map's keys else an error is thrown. Alias: map_extract(map, key).

Query
SELECT element_at(map([100, 5], [42, 43]), 100) AS element_at;
Result
 element_at------------ {42}

map_concat(maps...)

Returns a map created from merging the input maps. On key collision the value is taken from the last map with that key.

Query
SELECT map_concat(MAP {'key1': 10, 'key2': 20}, MAP {'key3': 30}, MAP {'key2': 5}) AS map_concat;
Result
 map_concat-------------------------------------- {"(key1,10)","(key2,5)","(key3,30)"}

map_contains(map, key)

Checks if a map contains a given key.

Query
SELECT map_contains(MAP {'key1': 10, 'key2': 20, 'key3': 30}, 'key2') AS map_contains;
Result
 map_contains-------------- t

map_contains_entry(map, key, value)

Check if a map contains a given key-value pair.

Query
SELECT map_contains_entry(MAP {'key1': 10, 'key2': 20, 'key3': 30}, 'key2', 20) AS map_contains_entry;
Result
 map_contains_entry-------------------- t

map_contains_value(map, value)

Checks if a map contains a given value.

Query
SELECT map_contains_value(MAP {'key1': 10, 'key2': 20, 'key3': 30}, 20) AS map_contains_value;
Result
 map_contains_value-------------------- t

map_entries(map)

Return a list of struct(k, v) for each key-value pair in the map.

Query
SELECT map_entries(map([100, 5], [42, 43])) AS map_entries;
Result
 map_entries----------------------- {"(100,42)","(5,43)"}

map_extract(map, key)

Return the value for a given key as a list, or an empty list if the key is not contained in the map. The type of the key provided in the second parameter must match the type of the map's keys else an error is thrown. Alias: element_at(map, key).

Query
SELECT map_extract(map([100, 5], [42, 43]), 100) AS map_extract;
Result
 map_extract------------- {42}

map_extract_value(map, key)

Returns the value for a given key or NULL if the key is not contained in the map. The type of the key provided in the second parameter must match the type of the map's keys else an error is thrown. Alias: map[key].

Query
SELECT map_extract_value(map([100, 5], [42, 43]), 100) AS map_extract_value;
Result
 map_extract_value-------------------                42

map_from_entries(STRUCT(k, v)[])

Returns a map created from the entries of the array.

Query
SELECT map_from_entries([{k: 5, v: 'val1'}, {k: 3, v: 'val2'}]) AS map_from_entries;
Result
 map_from_entries------------------------- {"(5,val1)","(3,val2)"}

map_keys(map)

Return a list of all keys in the map.

Query
SELECT map_keys(map([100, 5], [42, 43])) AS map_keys;
Result
 map_keys---------- {100,5}

map_values(map)

Return a list of all values in the map.

Query
SELECT map_values(map([100, 5], [42, 43])) AS map_values;
Result
 map_values------------ {42,43}

map()

Returns an empty map.

Query
SELECT map() AS map;
Result
 map----- {}

map[entry]

Returns the value for a given key or NULL if the key is not contained in the map. The type of the key provided in the second parameter must match the type of the map's keys else an error is thrown. Alias: map_extract_value(map, key).

Query
SELECT map([100, 5], ['a', 'b'])[100] AS map_entry;
Result
 map_entry----------- a

This page contains: