Map functions
Use these functions to build a map, to look up or test for keys and values, and to list, count, or merge the entries of a map. You can also use a constant map with switch to choose a value, as you would with a simple CASE expression.
cardinality function
Returns the number of key-value pairs in map. Entries whose value is NULL are counted. You can also pass a LIST or an ARRAY, and then the function returns the number of elements. If the argument is NULL, the result is NULL.
To count the elements of a list, you can also use len, which returns a BIGINT. len does not accept a map.
Syntax
cardinality(map)
Arguments
The cardinality function takes the following argument:
map-
The map whose entries you want to count, as a
MAPvalue. You can also pass aLISTorARRAYvalue.
Return type
UBIGINT
Examples
The following SQL query counts the entries of a map with two keys.
SELECT cardinality(map([4, 2], ['a', 'b'])) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query counts the elements of a list.
SELECT cardinality(['a', 'b', 'c']) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query counts the entries of an empty map, and of a map in which one value is NULL. The entry with the NULL value is counted.
SELECT cardinality(MAP {}) AS empty_map, cardinality( MAP {'a': 1, 'b': NULL} ) AS with_null_value
The query returns the following result.
| empty_map | with_null_value |
|---|---|
|
|
Considerations
Hive and Iceberg tables cannot store UBIGINT values. To store the result in a table, cast it, as in CAST(cardinality(.map) AS BIGINT)
Differences from Athena engine version 3
In Athena engine version 3, cardinality returns BIGINT. In DuckDB, it returns UBIGINT, so a query that stores the result in a table needs CAST(cardinality(.map) AS BIGINT)
map function
Returns a MAP that pairs each element of keys with the element at the same position in values. The map keeps the pairs in list order. With no arguments, map returns an empty map.
The two lists must have the same number of elements, the keys must be unique, and no key can be NULL. Otherwise, the query fails. A value can be NULL. If keys or values is NULL, the result is NULL.
You can also write a map as a literal, such as MAP {'key1': 'val1', 'key2': 'val2'}.
Syntax
map(keys, values)
map()
Arguments
The map function takes the following arguments:
keys-
The keys of the map, as a
LIST. The keys must be unique and cannot beNULL. values-
The values of the map, as a
LISTwith one value for each key.
Return type
MAP(, where K, V)K is the element type of keys and V is the element type of values. For example, two lists of VARCHAR values return MAP(VARCHAR, VARCHAR).
Examples
The following SQL query creates a map from a list of two keys and a list of two values.
SELECT map(['key1', 'key2'], ['val1', 'val2']) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query writes a map as a literal and reads the value for the key b with a subscript.
SELECT MAP {'a': 1, 'b': 2}['b'] AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the ARRAY form from Athena engine version 3. The map keeps the keys in the order given, not in sorted order.
SELECT MAP(ARRAY['b', 'a'], ARRAY[1, 2]) AS result
The query returns the following result.
| result |
|---|
|
Considerations
The Athena engine version 3 form MAP(ARRAY['a', 'b'], ARRAY[1, 2]) also works, because ARRAY[1, 2] is another way to write the list [1, 2].
map_concat function
Returns a map that has every key from the input maps. If two or more maps share a key, the result uses the value from the last of those maps in the argument list. Each key keeps the position where it first appears, so the keys of the first map come first, followed by the new keys of each later map.
A NULL argument is skipped, as if it were an empty map. If every argument is NULL, the result is NULL.
Syntax
map_concat(map1, map2 [, ...])
Arguments
The map_concat function takes the following arguments:
map1-
The first map to merge, as a
MAPvalue. map2-
The second map to merge, as a
MAPvalue. You can pass more maps after it. Every map must have the same key type and the same value type asmap1.
Return type
MAP, with the key type and value type of the input maps.
Examples
The following SQL query merges two maps that both have the key 2. The result takes the value for 2 from the second map.
SELECT map_concat( map([1, 2], ['a', 'b']), map([2, 3], ['c', 'd']) ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query merges three maps. The key a keeps its first position, and its value comes from the last map.
SELECT map_concat( MAP {'a': 1}, MAP {'b': 2}, MAP {'a': 3} ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query merges a map with a NULL map. The NULL map is skipped.
SELECT map_concat( MAP {'a': 1}, CAST(NULL AS MAP(VARCHAR, INTEGER)) ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query merges a map of INTEGER values with a map of DOUBLE values. The query casts the first map to MAP(VARCHAR, DOUBLE) and casts 2.5 to DOUBLE, so that both maps have the same type.
SELECT map_concat( CAST(MAP {'a': 1} AS MAP(VARCHAR, DOUBLE)), MAP {'b': CAST(2.5 AS DOUBLE)} ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
You must pass at least two maps.
The maps must have exactly the same key and value types. For example, merging a MAP(VARCHAR, INTEGER) with a MAP(VARCHAR, DOUBLE) fails, although an INTEGER value can be converted to DOUBLE. Cast the maps to one type first, as in CAST(.map1 AS MAP(VARCHAR, DOUBLE))
Differences from Athena engine version 3
In Athena engine version 3, map_concat returns NULL when any argument is NULL. In DuckDB, a NULL argument is skipped, and the result has the entries of the other maps. To get the Athena engine version 3 result, check the arguments for NULL first, for example with CASE WHEN .map1 IS NULL OR map2 IS NULL THEN NULL ELSE map_concat(map1, map2) END
In Athena engine version 3, map_concat merges maps whose value types differ but can be converted to a common type, such as INTEGER and DOUBLE. In DuckDB, the query fails. Cast the maps to one type first.
map_contains function
Returns true if map has the key key, and false otherwise. Keys are compared exactly, so string keys are case sensitive. A key whose value is NULL still counts as present. If map or key is NULL, the result is NULL.
To check for a value, use the map_contains_value function. To check for a key with a given value, use the map_contains_entry function.
Syntax
map_contains(map, key)
Arguments
The map_contains function takes the following arguments:
map-
The map to search, as a
MAPvalue. key-
The key to look for. It must have a type in common with the keys of
map.
Return type
BOOLEAN
Examples
The following SQL query checks whether a map has the key key2.
SELECT map_contains( MAP {'key1': 10, 'key2': 20, 'key3': 30}, 'key2' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that a key whose value is NULL is found, and that a key in a different case is not.
SELECT map_contains( MAP {'a': 1, 'b': NULL}, 'b' ) AS null_value, map_contains( MAP {'a': 1, 'b': NULL}, 'A' ) AS other_case
The query returns the following result.
| null_value | other_case |
|---|---|
|
|
Considerations
The key and the keys of the map must have a common type. For example, a BIGINT key works with a map that has INTEGER keys, and a string literal works if it can be read as the key type. But map_contains(MAP {'1': 10}, 1) fails, because the map has VARCHAR keys and 1 is an INTEGER. Cast the key, as in map_contains(MAP {'1': 10}, CAST(1 AS VARCHAR)).
map_contains_entry function
Returns true if map has the key key and the value for that key equals value, and false otherwise.
Unlike most comparisons, this function treats two NULL values as equal, so a key whose value is NULL matches when value is NULL. If key is NULL, the result is false. If map is NULL, the result is NULL.
Syntax
map_contains_entry(map, key, value)
Arguments
The map_contains_entry function takes the following arguments:
map-
The map to search, as a
MAPvalue. key-
The key to look for. It must have the key type of
map. value-
The value that the key must have, of the value type of
map.
Return type
BOOLEAN
Examples
The following SQL query checks whether a map has the key a with the value 1.
SELECT map_contains_entry(MAP {'a': 1}, 'a', 1) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns false, because the map has the key a but its value is 1, not 2.
SELECT map_contains_entry( MAP {'a': 1, 'b': 2}, 'a', 2 ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns true, because the value for the key b is NULL and the value argument is also NULL.
SELECT map_contains_entry( MAP {'a': 1, 'b': NULL}, 'b', NULL ) AS result
The query returns the following result.
| result |
|---|
|
map_contains_value function
Returns true if at least one key in map has the value value, and false otherwise.
If value is NULL, the result is NULL, even when the map has a NULL value. If map is NULL, the result is NULL. To check for a key whose value is NULL, use the map_contains_entry function.
Syntax
map_contains_value(map, value)
Arguments
The map_contains_value function takes the following arguments:
map-
The map to search, as a
MAPvalue. value-
The value to look for, of the value type of
map.
Return type
BOOLEAN
Examples
The following SQL query checks whether a map has the value 1.
SELECT map_contains_value(MAP {'a': 1}, 1) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query looks for a value that the map does not have.
SELECT map_contains_value( MAP {'a': 1, 'b': 2}, 3 ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that looking for NULL returns NULL, although the map has a NULL value. The query tests the result with IS NULL, so it returns true.
SELECT map_contains_value( MAP {'a': 1, 'b': NULL}, NULL ) IS NULL AS is_null
The query returns the following result.
| is_null |
|---|
|
map_entries function
Returns a list with one STRUCT for each entry of map, in map order. Each struct has two fields: key holds the key, and value holds its value. An empty map returns an empty list, and a NULL map returns NULL.
Use map_entries with list functions, such as list_filter and list_transform, to work on the entries of a map. To turn the list back into a map, use the map_from_entries function. To get one row for each entry, use the unnest function.
Syntax
map_entries(map)
Arguments
The map_entries function takes the following argument:
map-
The map to read, as a
MAPvalue.
Return type
STRUCT("key" , a list of structs, where K, "value" V)[]K and V are the key type and the value type of map.
Examples
The following SQL query returns the entries of a map with one key.
SELECT map_entries(map(['key'], ['val'])) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query keeps only the entries whose value is greater than 2, and turns the result back into a map. The second argument of list_filter is a lambda function. Write a lambda function with the lambda keyword, a parameter name, a colon, and an expression. This query uses lambda e: e.value > 2. DuckDB does not accept the arrow form, such as e -> e.value > 2.
SELECT map_from_entries( list_filter( map_entries(MAP {'a': 1, 'b': 5, 'c': 3}), lambda e: e.value > 2 ) ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns one row for each entry of a map.
SELECT e.key AS k, e.value AS v FROM ( SELECT unnest(map_entries(MAP {'a': 1, 'b': 2})) AS e ) AS entries ORDER BY k
The query returns the following results.
| k | v |
|---|---|
|
|
|
|
map_extract function
Looks up key in map and returns the value for that key inside a list. Because map keys are unique, the list holds at most one value. When the map does not have the key, the list is empty. If key is NULL, the result is an empty list. If map is NULL, the result is NULL.
To get the value itself, use the map_extract_value function or a subscript, as in . Both return map[key]NULL when the map does not have the key.
element_at is an alias of map_extract.
Syntax
map_extract(map, key)
element_at(map, key)
Arguments
The map_extract function takes the following arguments:
map-
The map to read, as a
MAPvalue. key-
The key to look up. It must have a type in common with the keys of
map.
Return type
A list of the value type of map, such as VARCHAR[] for a map with VARCHAR values.
Examples
The following SQL query returns the value for the key key, in a list.
SELECT map_extract(map(['key'], ['val']), 'key') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query looks up a key that the map does not have, so the result is an empty list, not NULL.
SELECT map_extract(MAP {'a': 1}, 'z') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares the element_at alias with a subscript on the same map. element_at returns a list, and the subscript returns the value.
SELECT element_at(m, 'b') AS element_at_result, m['b'] AS subscript_result FROM (SELECT MAP {'a': 1, 'b': 2} AS m) AS t
The query returns the following result.
| element_at_result | subscript_result |
|---|---|
|
|
Considerations
map_extract and element_at accept only a map. For a list, use the list_extract function or a subscript, as in [10, 20, 30][2].
The key and the keys of the map must have a common type. For example, map_extract(MAP {'1': 10}, 1) fails, because the map has VARCHAR keys and 1 is an INTEGER.
Differences from Athena engine version 3
In Athena engine version 3, element_at( returns the value for the key, or map, key)NULL if the map does not have the key. In DuckDB, element_at returns a list that holds the value, or an empty list. To get the value, use map_extract_value( or map, key).map[key]
In Athena engine version 3, element_at also returns the element of an array at a position. In DuckDB, element_at accepts only a map, and a call with a list fails. Use or list[index]list_extract( instead.list, index)
map_extract_value function
Returns the value for key in map. If the map does not have the key, or if map or key is NULL, the result is NULL. A subscript, as in , returns the same result.map[key]
A missing key and a key whose value is NULL both return NULL. To tell them apart, use the map_contains function.
Syntax
map_extract_value(map, key)
Arguments
The map_extract_value function takes the following arguments:
map-
The map to read, as a
MAPvalue. key-
The key to look up. It must have a type in common with the keys of
map.
Return type
The value type of map, such as VARCHAR for a map with VARCHAR values.
Examples
The following SQL query returns the value for the key key.
SELECT map_extract_value( map(['key'], ['val']), 'key' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query looks up a key that the map does not have.
SELECT map_extract_value(MAP {'a': 1}, 'z') AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that a subscript returns the same value.
SELECT map_extract_value(m, 'b') AS function_result, m['b'] AS subscript_result FROM (SELECT MAP {'a': 1, 'b': 2} AS m) AS t
The query returns the following result.
| function_result | subscript_result |
|---|---|
|
|
map_from_entries function
Returns a MAP built from entries, a list in which each element is a struct with two fields. The first field is the key and the second field is its value. The field names do not matter, so a list of unnamed structs, such as [('a', 1), ('b', 2)], also works. The map keeps the entries in list order.
The keys must be unique, and no key can be NULL. Otherwise, the query fails. An empty list returns an empty map, and a NULL list returns NULL.
map_from_entries is the reverse of map_entries.
Syntax
map_from_entries(entries)
Arguments
The map_from_entries function takes the following argument:
entries-
The entries of the map, as a
LISTofSTRUCTvalues that each have exactly two fields: the key, and then the value.
Return type
MAP(, where K, V)K and V are the types of the first and second struct fields. For example, a list of {k: 5, v: 'val1'} structs returns MAP(INTEGER, VARCHAR).
Examples
The following SQL query creates a map from a list of two structs.
SELECT map_from_entries( [{k: 5, v: 'val1'}, {k: 3, v: 'val2'}] ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query creates a map from a list of unnamed structs.
SELECT map_from_entries([('a', 1), ('b', 2)]) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query changes every key to uppercase and multiplies every value by 10. It reads the entries with map_entries, builds new entries with list_transform and a lambda function, and turns them back into a map.
SELECT map_from_entries( list_transform( map_entries(MAP {'a': 1, 'b': 5}), lambda e: { 'key': upper(e.key), 'value': e.value * 10 } ) ) AS result
The query returns the following result.
| result |
|---|
|
map_keys function
Returns a list of the keys of map, in map order. An empty map returns an empty list, and a NULL map returns NULL.
To get the values, use the map_values function. To sort the keys, pass the result to list_sort.
Syntax
map_keys(map)
Arguments
The map_keys function takes the following argument:
map-
The map to read, as a
MAPvalue.
Return type
A list of the key type of map, such as VARCHAR[] for a map with VARCHAR keys.
Examples
The following SQL query returns the keys of a map with one key.
SELECT map_keys(map(['key'], ['val'])) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the keys come back in map order, and sorts them with list_sort.
SELECT map_keys(MAP {'b': 1, 'a': 2}) AS map_order, list_sort( map_keys(MAP {'b': 1, 'a': 2}) ) AS sorted_keys
The query returns the following result.
| map_order | sorted_keys |
|---|---|
|
|
map_values function
Returns a list of the values of map, in map order, including NULL values. An empty map returns an empty list, and a NULL map returns NULL.
To get the keys, use the map_keys function.
Syntax
map_values(map)
Arguments
The map_values function takes the following argument:
map-
The map to read, as a
MAPvalue.
Return type
A list of the value type of map, such as VARCHAR[] for a map with VARCHAR values.
Examples
The following SQL query returns the values of a map with one key.
SELECT map_values(map(['key'], ['val'])) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that a NULL value is kept in the result.
SELECT map_values( MAP {'a': 1, 'b': NULL, 'c': 3} ) AS result
The query returns the following result.
| result |
|---|
|
switch function
Looks up key in map and returns the value for that key. If map has no entry for key, or key is NULL, the function returns default, or NULL when you leave out default.
switch(x, MAP {1: 'one', 2: 'two'}, 'other') returns the same result as CASE x WHEN 1 THEN 'one' WHEN 2 THEN 'two' ELSE 'other' END.
If you leave out key, the function works like a searched CASE expression. It converts each key of map to BOOLEAN, in the order that the map lists them, and returns the value of the first key that converts to true. If no key converts to true, the function returns default, or NULL when you leave out default. A key that cannot convert to BOOLEAN, such as 'a', can make the query fail.
Syntax
switch(key, map [, default ])
switch(map [, default ])
Arguments
The switch function takes the following arguments:
key-
The value to look up. Its type must match the key type of
map, or convert to it. map-
The map that holds the values to match and their results, as a constant
MAPvalue, such asMAP {1: 'one', 2: 'two'}. Its keys are the values to match, and its values are the results. default-
The value to return when no key matches. Its type must be compatible with the values of
map. It can be any expression, including one that uses columns. This argument is optional.
Return type
The type of the values in map, or a type that holds both those values and default. For example, a map with VARCHAR values and a VARCHAR default returns VARCHAR.
Examples
The following SQL query looks up 2 in a map.
SELECT switch( 2, MAP {1: 'one', 2: 'two'}, 'other' ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query labels each row. The value 3 has no entry in the map, so it gets the default.
SELECT x, switch( x, MAP {1: 'one', 2: 'two'}, 'other' ) AS label FROM (VALUES (1), (2), (3)) AS t(x) ORDER BY x
The query returns the following results.
| x | label |
|---|---|
|
|
|
|
|
|
The following SQL query leaves out default. A key that has no entry, and a NULL key, both return NULL.
SELECT x, switch(x, MAP {1: 'one', 2: 'two'}) AS label FROM (VALUES (1), (3), (NULL)) AS t(x) ORDER BY x NULLS LAST
The query returns the following results.
| x | label |
|---|---|
|
|
|
|
|
|
Considerations
The keys and values of map must be constants. A map that refers to a column fails. When the results depend on column values, use a CASE expression.
If the type of default is not compatible with the values of map, the query fails. For example, a map with VARCHAR values and an INTEGER default fails.