

# 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
<a name="athena-duckdb-functions-struct-map-cardinality"></a>

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
<a name="athena-duckdb-functions-struct-map-cardinality-syntax"></a>

```sql
cardinality({{map}})
```

### Arguments
<a name="athena-duckdb-functions-struct-map-cardinality-arguments"></a>

The `cardinality` function takes the following argument:

**{{map}}**  
The map whose entries you want to count, as a `MAP` value. You can also pass a `LIST` or `ARRAY` value.

### Return type
<a name="athena-duckdb-functions-struct-map-cardinality-return-type"></a>

`UBIGINT`

### Examples
<a name="athena-duckdb-functions-struct-map-cardinality-examples"></a>

The following SQL query counts the entries of a map with two keys.

```sql
SELECT cardinality(map([4, 2], ['a', 'b'])) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2` | 

The following SQL query counts the elements of a list.

```sql
SELECT cardinality(['a', 'b', 'c']) AS result
```

The query returns the following result.


| result | 
| --- | 
| `3` | 

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.

```sql
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 | 
| --- | --- | 
| `0` | `2` | 

### Considerations
<a name="athena-duckdb-functions-struct-map-cardinality-considerations"></a>

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
<a name="athena-duckdb-functions-struct-map-cardinality-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-struct-map-map"></a>

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
<a name="athena-duckdb-functions-struct-map-map-syntax"></a>

```sql
map({{keys}}, {{values}})
map()
```

### Arguments
<a name="athena-duckdb-functions-struct-map-map-arguments"></a>

The `map` function takes the following arguments:

**{{keys}}**  
The keys of the map, as a `LIST`. The keys must be unique and cannot be `NULL`.

**{{values}}**  
The values of the map, as a `LIST` with one value for each key.

### Return type
<a name="athena-duckdb-functions-struct-map-map-return-type"></a>

`MAP({{K}}, {{V}})`, where `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
<a name="athena-duckdb-functions-struct-map-map-examples"></a>

The following SQL query creates a map from a list of two keys and a list of two values.

```sql
SELECT map(['key1', 'key2'], ['val1', 'val2']) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{key1=val1, key2=val2}` | 

The following SQL query writes a map as a literal and reads the value for the key `b` with a subscript.

```sql
SELECT MAP {'a': 1, 'b': 2}['b'] AS result
```

The query returns the following result.


| result | 
| --- | 
| `2` | 

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.

```sql
SELECT MAP(ARRAY['b', 'a'], ARRAY[1, 2]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{b=1, a=2}` | 

### Considerations
<a name="athena-duckdb-functions-struct-map-map-considerations"></a>

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
<a name="athena-duckdb-functions-struct-map-map-concat"></a>

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
<a name="athena-duckdb-functions-struct-map-map-concat-syntax"></a>

```sql
map_concat({{map1}}, {{map2}} [, ...])
```

### Arguments
<a name="athena-duckdb-functions-struct-map-map-concat-arguments"></a>

The `map_concat` function takes the following arguments:

**{{map1}}**  
The first map to merge, as a `MAP` value.

**{{map2}}**  
The second map to merge, as a `MAP` value. You can pass more maps after it. Every map must have the same key type and the same value type as `map1`.

### Return type
<a name="athena-duckdb-functions-struct-map-map-concat-return-type"></a>

`MAP`, with the key type and value type of the input maps.

### Examples
<a name="athena-duckdb-functions-struct-map-map-concat-examples"></a>

The following SQL query merges two maps that both have the key `2`. The result takes the value for `2` from the second map.

```sql
SELECT
    map_concat(
        map([1, 2], ['a', 'b']), map([2, 3], ['c', 'd'])
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{1=a, 2=c, 3=d}` | 

The following SQL query merges three maps. The key `a` keeps its first position, and its value comes from the last map.

```sql
SELECT
    map_concat(
        MAP {'a': 1}, MAP {'b': 2}, MAP {'a': 3}
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{a=3, b=2}` | 

The following SQL query merges a map with a `NULL` map. The `NULL` map is skipped.

```sql
SELECT
    map_concat(
        MAP {'a': 1},
        CAST(NULL AS MAP(VARCHAR, INTEGER))
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{a=1}` | 

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.

```sql
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 | 
| --- | 
| `{a=1.0, b=2.5}` | 

### Considerations
<a name="athena-duckdb-functions-struct-map-map-concat-considerations"></a>

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
<a name="athena-duckdb-functions-struct-map-map-concat-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-struct-map-map-contains"></a>

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](#athena-duckdb-functions-struct-map-map-contains-value). To check for a key with a given value, use the [`map_contains_entry` function](#athena-duckdb-functions-struct-map-map-contains-entry).

### Syntax
<a name="athena-duckdb-functions-struct-map-map-contains-syntax"></a>

```sql
map_contains({{map}}, {{key}})
```

### Arguments
<a name="athena-duckdb-functions-struct-map-map-contains-arguments"></a>

The `map_contains` function takes the following arguments:

**{{map}}**  
The map to search, as a `MAP` value.

**{{key}}**  
The key to look for. It must have a type in common with the keys of `map`.

### Return type
<a name="athena-duckdb-functions-struct-map-map-contains-return-type"></a>

`BOOLEAN`

### Examples
<a name="athena-duckdb-functions-struct-map-map-contains-examples"></a>

The following SQL query checks whether a map has the key `key2`.

```sql
SELECT
    map_contains(
        MAP {'key1': 10, 'key2': 20, 'key3': 30}, 'key2'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `true` | 

The following SQL query shows that a key whose value is `NULL` is found, and that a key in a different case is not.

```sql
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 | 
| --- | --- | 
| `true` | `false` | 

### Considerations
<a name="athena-duckdb-functions-struct-map-map-contains-considerations"></a>

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
<a name="athena-duckdb-functions-struct-map-map-contains-entry"></a>

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
<a name="athena-duckdb-functions-struct-map-map-contains-entry-syntax"></a>

```sql
map_contains_entry({{map}}, {{key}}, {{value}})
```

### Arguments
<a name="athena-duckdb-functions-struct-map-map-contains-entry-arguments"></a>

The `map_contains_entry` function takes the following arguments:

**{{map}}**  
The map to search, as a `MAP` value.

**{{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
<a name="athena-duckdb-functions-struct-map-map-contains-entry-return-type"></a>

`BOOLEAN`

### Examples
<a name="athena-duckdb-functions-struct-map-map-contains-entry-examples"></a>

The following SQL query checks whether a map has the key `a` with the value `1`.

```sql
SELECT
    map_contains_entry(MAP {'a': 1}, 'a', 1) AS result
```

The query returns the following result.


| result | 
| --- | 
| `true` | 

The following SQL query returns `false`, because the map has the key `a` but its value is `1`, not `2`.

```sql
SELECT
    map_contains_entry(
        MAP {'a': 1, 'b': 2}, 'a', 2
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `false` | 

The following SQL query returns `true`, because the value for the key `b` is `NULL` and the `value` argument is also `NULL`.

```sql
SELECT
    map_contains_entry(
        MAP {'a': 1, 'b': NULL}, 'b', NULL
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `true` | 

## `map_contains_value` function
<a name="athena-duckdb-functions-struct-map-map-contains-value"></a>

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](#athena-duckdb-functions-struct-map-map-contains-entry).

### Syntax
<a name="athena-duckdb-functions-struct-map-map-contains-value-syntax"></a>

```sql
map_contains_value({{map}}, {{value}})
```

### Arguments
<a name="athena-duckdb-functions-struct-map-map-contains-value-arguments"></a>

The `map_contains_value` function takes the following arguments:

**{{map}}**  
The map to search, as a `MAP` value.

**{{value}}**  
The value to look for, of the value type of `map`.

### Return type
<a name="athena-duckdb-functions-struct-map-map-contains-value-return-type"></a>

`BOOLEAN`

### Examples
<a name="athena-duckdb-functions-struct-map-map-contains-value-examples"></a>

The following SQL query checks whether a map has the value `1`.

```sql
SELECT map_contains_value(MAP {'a': 1}, 1) AS result
```

The query returns the following result.


| result | 
| --- | 
| `true` | 

The following SQL query looks for a value that the map does not have.

```sql
SELECT
    map_contains_value(
        MAP {'a': 1, 'b': 2}, 3
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `false` | 

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

```sql
SELECT
    map_contains_value(
        MAP {'a': 1, 'b': NULL}, NULL
    ) IS NULL AS is_null
```

The query returns the following result.


| is\_null | 
| --- | 
| `true` | 

## `map_entries` function
<a name="athena-duckdb-functions-struct-map-map-entries"></a>

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](#athena-duckdb-functions-struct-map-map-from-entries). To get one row for each entry, use the [`unnest` function](duckdb-functions-list-ranges-and-unnesting.md#athena-duckdb-functions-list-unnest).

### Syntax
<a name="athena-duckdb-functions-struct-map-map-entries-syntax"></a>

```sql
map_entries({{map}})
```

### Arguments
<a name="athena-duckdb-functions-struct-map-map-entries-arguments"></a>

The `map_entries` function takes the following argument:

**{{map}}**  
The map to read, as a `MAP` value.

### Return type
<a name="athena-duckdb-functions-struct-map-map-entries-return-type"></a>

`STRUCT("key" {{K}}, "value" {{V}})[]`, a list of structs, where `K` and `V` are the key type and the value type of `map`.

### Examples
<a name="athena-duckdb-functions-struct-map-map-entries-examples"></a>

The following SQL query returns the entries of a map with one key.

```sql
SELECT map_entries(map(['key'], ['val'])) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[{'key': key, 'value': val}]` | 

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

```sql
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 | 
| --- | 
| `{b=5, c=3}` | 

The following SQL query returns one row for each entry of a map.

```sql
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 | 
| --- | --- | 
| `a` | `1` | 
| `b` | `2` | 

## `map_extract` function
<a name="athena-duckdb-functions-struct-map-map-extract"></a>

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](#athena-duckdb-functions-struct-map-map-extract-value) or a subscript, as in `{{map}}[{{key}}]`. Both return `NULL` when the map does not have the key.

`element_at` is an alias of `map_extract`.

### Syntax
<a name="athena-duckdb-functions-struct-map-map-extract-syntax"></a>

```sql
map_extract({{map}}, {{key}})
element_at({{map}}, {{key}})
```

### Arguments
<a name="athena-duckdb-functions-struct-map-map-extract-arguments"></a>

The `map_extract` function takes the following arguments:

**{{map}}**  
The map to read, as a `MAP` value.

**{{key}}**  
The key to look up. It must have a type in common with the keys of `map`.

### Return type
<a name="athena-duckdb-functions-struct-map-map-extract-return-type"></a>

A list of the value type of `map`, such as `VARCHAR[]` for a map with `VARCHAR` values.

### Examples
<a name="athena-duckdb-functions-struct-map-map-extract-examples"></a>

The following SQL query returns the value for the key `key`, in a list.

```sql
SELECT
    map_extract(map(['key'], ['val']), 'key') AS result
```

The query returns the following result.


| result | 
| --- | 
| `[val]` | 

The following SQL query looks up a key that the map does not have, so the result is an empty list, not `NULL`.

```sql
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.

```sql
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 | 
| --- | --- | 
| `[2]` | `2` | 

### Considerations
<a name="athena-duckdb-functions-struct-map-map-extract-considerations"></a>

`map_extract` and `element_at` accept only a map. For a list, use the [`list_extract` function](duckdb-functions-list-length-and-elements.md#athena-duckdb-functions-list-list-extract) 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
<a name="athena-duckdb-functions-struct-map-map-extract-differences-from-engine-version-3"></a>

In Athena engine version 3, `element_at({{map}}, {{key}})` returns the value for the key, or `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({{map}}, {{key}})` or `{{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 `{{list}}[{{index}}]` or `list_extract({{list}}, {{index}})` instead.

## `map_extract_value` function
<a name="athena-duckdb-functions-struct-map-map-extract-value"></a>

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 `{{map}}[{{key}}]`, returns the same result.

A missing key and a key whose value is `NULL` both return `NULL`. To tell them apart, use the [`map_contains` function](#athena-duckdb-functions-struct-map-map-contains).

### Syntax
<a name="athena-duckdb-functions-struct-map-map-extract-value-syntax"></a>

```sql
map_extract_value({{map}}, {{key}})
```

### Arguments
<a name="athena-duckdb-functions-struct-map-map-extract-value-arguments"></a>

The `map_extract_value` function takes the following arguments:

**{{map}}**  
The map to read, as a `MAP` value.

**{{key}}**  
The key to look up. It must have a type in common with the keys of `map`.

### Return type
<a name="athena-duckdb-functions-struct-map-map-extract-value-return-type"></a>

The value type of `map`, such as `VARCHAR` for a map with `VARCHAR` values.

### Examples
<a name="athena-duckdb-functions-struct-map-map-extract-value-examples"></a>

The following SQL query returns the value for the key `key`.

```sql
SELECT
    map_extract_value(
        map(['key'], ['val']), 'key'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `val` | 

The following SQL query looks up a key that the map does not have.

```sql
SELECT map_extract_value(MAP {'a': 1}, 'z') AS result
```

The query returns the following result.


| result | 
| --- | 
| `NULL` | 

The following SQL query shows that a subscript returns the same value.

```sql
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 | 
| --- | --- | 
| `2` | `2` | 

## `map_from_entries` function
<a name="athena-duckdb-functions-struct-map-map-from-entries"></a>

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
<a name="athena-duckdb-functions-struct-map-map-from-entries-syntax"></a>

```sql
map_from_entries({{entries}})
```

### Arguments
<a name="athena-duckdb-functions-struct-map-map-from-entries-arguments"></a>

The `map_from_entries` function takes the following argument:

**{{entries}}**  
The entries of the map, as a `LIST` of `STRUCT` values that each have exactly two fields: the key, and then the value.

### Return type
<a name="athena-duckdb-functions-struct-map-map-from-entries-return-type"></a>

`MAP({{K}}, {{V}})`, where `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
<a name="athena-duckdb-functions-struct-map-map-from-entries-examples"></a>

The following SQL query creates a map from a list of two structs.

```sql
SELECT
    map_from_entries(
        [{k: 5, v: 'val1'}, {k: 3, v: 'val2'}]
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{5=val1, 3=val2}` | 

The following SQL query creates a map from a list of unnamed structs.

```sql
SELECT map_from_entries([('a', 1), ('b', 2)]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{a=1, b=2}` | 

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.

```sql
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 | 
| --- | 
| `{A=10, B=50}` | 

## `map_keys` function
<a name="athena-duckdb-functions-struct-map-map-keys"></a>

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](#athena-duckdb-functions-struct-map-map-values). To sort the keys, pass the result to `list_sort`.

### Syntax
<a name="athena-duckdb-functions-struct-map-map-keys-syntax"></a>

```sql
map_keys({{map}})
```

### Arguments
<a name="athena-duckdb-functions-struct-map-map-keys-arguments"></a>

The `map_keys` function takes the following argument:

**{{map}}**  
The map to read, as a `MAP` value.

### Return type
<a name="athena-duckdb-functions-struct-map-map-keys-return-type"></a>

A list of the key type of `map`, such as `VARCHAR[]` for a map with `VARCHAR` keys.

### Examples
<a name="athena-duckdb-functions-struct-map-map-keys-examples"></a>

The following SQL query returns the keys of a map with one key.

```sql
SELECT map_keys(map(['key'], ['val'])) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[key]` | 

The following SQL query shows that the keys come back in map order, and sorts them with `list_sort`.

```sql
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 | 
| --- | --- | 
| `[b, a]` | `[a, b]` | 

## `map_values` function
<a name="athena-duckdb-functions-struct-map-map-values"></a>

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](#athena-duckdb-functions-struct-map-map-keys).

### Syntax
<a name="athena-duckdb-functions-struct-map-map-values-syntax"></a>

```sql
map_values({{map}})
```

### Arguments
<a name="athena-duckdb-functions-struct-map-map-values-arguments"></a>

The `map_values` function takes the following argument:

**{{map}}**  
The map to read, as a `MAP` value.

### Return type
<a name="athena-duckdb-functions-struct-map-map-values-return-type"></a>

A list of the value type of `map`, such as `VARCHAR[]` for a map with `VARCHAR` values.

### Examples
<a name="athena-duckdb-functions-struct-map-map-values-examples"></a>

The following SQL query returns the values of a map with one key.

```sql
SELECT map_values(map(['key'], ['val'])) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[val]` | 

The following SQL query shows that a `NULL` value is kept in the result.

```sql
SELECT
    map_values(
        MAP {'a': 1, 'b': NULL, 'c': 3}
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[1, NULL, 3]` | 

## `switch` function
<a name="athena-duckdb-functions-struct-map-switch"></a>

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
<a name="athena-duckdb-functions-struct-map-switch-syntax"></a>

```sql
switch({{key}}, {{map}} [, {{default}} ])
switch({{map}} [, {{default}} ])
```

### Arguments
<a name="athena-duckdb-functions-struct-map-switch-arguments"></a>

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 `MAP` value, such as `MAP {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
<a name="athena-duckdb-functions-struct-map-switch-return-type"></a>

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
<a name="athena-duckdb-functions-struct-map-switch-examples"></a>

The following SQL query looks up `2` in a map.

```sql
SELECT
    switch(
        2, MAP {1: 'one', 2: 'two'}, 'other'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `two` | 

The following SQL query labels each row. The value `3` has no entry in the map, so it gets the default.

```sql
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 | 
| --- | --- | 
| `1` | `one` | 
| `2` | `two` | 
| `3` | `other` | 

The following SQL query leaves out `default`. A key that has no entry, and a `NULL` key, both return `NULL`.

```sql
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 | 
| --- | --- | 
| `1` | `one` | 
| `3` | `NULL` | 
| `NULL` | `NULL` | 

### Considerations
<a name="athena-duckdb-functions-struct-map-switch-considerations"></a>

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.