View a markdown version of this page

Struct, map, and union functions in DuckDB - Amazon Athena

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

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

Function Description

cardinality

Returns the number of entries in a map, or the number of elements in a list.

element_at

Alias of map_extract.

map

Builds a map by pairing each key in one list with the value at the same position in a second list.

map_concat

Merges two or more maps into one map.

map_contains

Tests whether a key is present in a map.

map_contains_entry

Tests whether a map pairs the given key with the given value.

map_contains_value

Tests whether any key of a map has the given value.

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

Returns the value for a key in a map, as a list that holds the value.

map_extract_value

Looks up a key in a map and returns its value, or NULL when the key is missing.

map_from_entries

Creates a map from a list of structs that each hold a key and a value.

map_keys

Extracts the keys of a map into a list, in map order.

map_values

Extracts the values of a map into a list, in map order.

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

Builds an unnamed struct with one field for each argument, in order.

struct_concat

Combines two or more structs into one struct that has all of their fields.

struct_contains

Tests whether any field of an unnamed struct equals a value.

struct_extract

Returns one field of a struct, by name, or by position for an unnamed struct.

struct_extract_at

Returns one field of a struct by its position.

struct_has

Alias of struct_contains.

struct_indexof

Alias of struct_position.

struct_insert

Adds one or more fields to the end of a struct.

struct_keys

Returns a list that holds the name of each top-level field of a struct.

struct_pack

Builds a struct from named values.

struct_position

Returns the position of the first field of an unnamed struct that equals a value.

struct_update

Changes the values of fields in a struct, and adds the fields that it does not have.

struct_values

Returns an unnamed struct (TUPLE) that holds the value of each field of a struct, without the field names.

switch

Returns the value that a constant map holds for a key, like a simple CASE expression.

union_extract

Returns the value of a member of a UNION value, or NULL when the value holds another member.

union_tag

Returns the name of the member that a UNION value holds.

union_value

Builds a UNION value with one member.