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 , a subscript with the field name, as in s.note, or s['note']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 . To give the fields names, cast the value to a t[2]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 , which returns m['a']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 in SQL reference.
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 pairs, such as key=value{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.
Topics
The following table lists the struct, map, and union functions, with a link to the description of each one.
| Function | Description |
|---|---|
Returns the number of entries in a map, or the number of elements in a list. |
|
element_at |
Alias of map_extract. |
Builds a map by pairing each key in one list with the value at the same position in a second list. |
|
Merges two or more maps into one map. |
|
Tests whether a key is present in a map. |
|
Tests whether a map pairs the given key with the given value. |
|
Tests whether any key of a map has the given value. |
|
Turns each key-value pair of a map into a struct with |
|
Returns the value for a key in a map, as a list that holds the value. |
|
Looks up a key in a map and returns its value, or |
|
Creates a map from a list of structs that each hold a key and a value. |
|
Extracts the keys of a map into a list, in map order. |
|
Extracts the values of a map into a list, in map order. |
|
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. |
|
Builds an unnamed struct with one field for each argument, in order. |
|
Combines two or more structs into one struct that has all of their fields. |
|
Tests whether any field of an unnamed struct equals a value. |
|
Returns one field of a struct, by name, or by position for an unnamed struct. |
|
Returns one field of a struct by its position. |
|
struct_has |
Alias of struct_contains. |
struct_indexof |
Alias of struct_position. |
Adds one or more fields to the end of a struct. |
|
Returns a list that holds the name of each top-level field of a struct. |
|
Builds a struct from named values. |
|
Returns the position of the first field of an unnamed struct that equals a value. |
|
Changes the values of fields in a struct, and adds the fields that it does not have. |
|
Returns an unnamed struct ( |
|
Returns the value that a constant map holds for a key, like a simple |
|
Returns the value of a member of a |
|
Returns the name of the member that a |
|
Builds a |