

# Struct, map, and union functions in DuckDB

Use struct, map, and union functions to build nested values, to read their parts, and to change their shape.

A `STRUCT` holds a fixed set of named fields, and each field has its own type, as in `STRUCT(id INTEGER, note VARCHAR)`. Write a struct literal in curly braces, with each field name followed by a colon and the value, such as `{'id': 1, 'note': 'shipped'}`, or build one with `struct_pack`. To read a field, use dot notation, as in `{{s}}.note`, a subscript with the field name, as in `{{s}}['note']`, or `struct_extract`.

An unnamed struct is a struct whose fields have no names. Create one with `row`, or write two or more values in parentheses, such as `(1, 'shipped')`. `typeof` reports this type as `TUPLE`, for example `TUPLE(INTEGER, VARCHAR)`. To read a field of an unnamed struct, use its position, as in `{{t}}[2]`. To give the fields names, cast the value to a `STRUCT` type.

A `MAP` holds key-value pairs, called entries. The keys of a map share one type, and its values share one type, as in `MAP(VARCHAR, INTEGER)`. Keys must be unique and cannot be `NULL`, but values can be `NULL`. Write a map literal with the `MAP` keyword and curly braces, such as `MAP {'a': 1, 'b': 2}`. You can also build a map from a list of keys and a list of values with `map`. A map keeps its entries in the order in which you add them. To read a value, use a subscript with the key, as in `{{m}}['a']`, which returns `NULL` when the map does not have the key.

A `UNION` value holds one value from a fixed set of alternatives. Each alternative has a tag name and a type, as in `UNION(num INTEGER, str VARCHAR)`. Create a union value with `union_value` or by casting a value to a `UNION` type, read its tag with `union_tag`, and read its value with `union_extract`.

Hive and Iceberg tables cannot store `UNION` values, and Iceberg tables cannot store unnamed structs. For more information about the types that each table format can store, see [Data types that each table format can store](duckdb-sql-reference.md#athena-duckdb-sql-data-types) in [SQL reference](duckdb-sql-reference.md).

Query results show a struct as its field names in single quotes, each followed by its value, such as `{'id': 1, 'note': shipped}`. They show an unnamed struct as its values in parentheses, such as `(1, shipped)`, and a map as `{{key}}={{value}}` pairs, such as `{a=1, b=2}`. They show a union as the value of its current alternative. String values are shown without quotation marks. To see the exact type of a value, use the [`typeof` function](duckdb-functions-utility-types-and-expressions.md#athena-duckdb-functions-utility-typeof).

**Topics**
+ [Maps](duckdb-functions-struct-map-maps.md)
+ [Structs, unions](duckdb-functions-struct-map-structs-and-unions.md)

The following table lists the struct, map, and union functions, with a link to the description of each one.


| Function | Description | 
| --- | --- | 
| [cardinality](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-cardinality) | Returns the number of entries in a map, or the number of elements in a list. | 
| element\_at | Alias of [map\_extract](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-map-extract). | 
| [map](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-map) | Builds a map by pairing each key in one list with the value at the same position in a second list. | 
| [map\_concat](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-map-concat) | Merges two or more maps into one map. | 
| [map\_contains](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-map-contains) | Tests whether a key is present in a map. | 
| [map\_contains\_entry](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-map-contains-entry) | Tests whether a map pairs the given key with the given value. | 
| [map\_contains\_value](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-map-contains-value) | Tests whether any key of a map has the given value. | 
| [map\_entries](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-map-entries) | Turns each key-value pair of a map into a struct with `key` and `value` fields, and returns the structs in a list. | 
| [map\_extract](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-map-extract) | Returns the value for a key in a map, as a list that holds the value. | 
| [map\_extract\_value](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-map-extract-value) | Looks up a key in a map and returns its value, or `NULL` when the key is missing. | 
| [map\_from\_entries](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-map-from-entries) | Creates a map from a list of structs that each hold a key and a value. | 
| [map\_keys](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-map-keys) | Extracts the keys of a map into a list, in map order. | 
| [map\_values](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-map-values) | Extracts the values of a map into a list, in map order. | 
| [remap\_struct](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-remap-struct) | Builds a struct of a target type from another struct, with a mapping that names the input field for each target field and default values for the rest. | 
| [row](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-row) | Builds an unnamed struct with one field for each argument, in order. | 
| [struct\_concat](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-struct-concat) | Combines two or more structs into one struct that has all of their fields. | 
| [struct\_contains](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-struct-contains) | Tests whether any field of an unnamed struct equals a value. | 
| [struct\_extract](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-struct-extract) | Returns one field of a struct, by name, or by position for an unnamed struct. | 
| [struct\_extract\_at](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-struct-extract-at) | Returns one field of a struct by its position. | 
| struct\_has | Alias of [struct\_contains](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-struct-contains). | 
| struct\_indexof | Alias of [struct\_position](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-struct-position). | 
| [struct\_insert](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-struct-insert) | Adds one or more fields to the end of a struct. | 
| [struct\_keys](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-struct-keys) | Returns a list that holds the name of each top-level field of a struct. | 
| [struct\_pack](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-struct-pack) | Builds a struct from named values. | 
| [struct\_position](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-struct-position) | Returns the position of the first field of an unnamed struct that equals a value. | 
| [struct\_update](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-struct-update) | Changes the values of fields in a struct, and adds the fields that it does not have. | 
| [struct\_values](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-struct-values) | Returns an unnamed struct (`TUPLE`) that holds the value of each field of a struct, without the field names. | 
| [switch](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-switch) | Returns the value that a constant map holds for a key, like a simple `CASE` expression. | 
| [union\_extract](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-union-extract) | Returns the value of a member of a `UNION` value, or `NULL` when the value holds another member. | 
| [union\_tag](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-union-tag) | Returns the name of the member that a `UNION` value holds. | 
| [union\_value](duckdb-functions-struct-map-structs-and-unions.md#athena-duckdb-functions-struct-map-union-value) | Builds a `UNION` value with one member. | 