

# Struct and union functions

Use these functions to build a struct or a `UNION` value, to read its fields or members, and to add, change, or combine struct fields. Some of these functions accept only structs whose fields have names, and some accept only unnamed structs, which have the type `TUPLE`.

Hive and Iceberg tables cannot store `UNION` values, so extract the value or cast it to another type before you write it to a table.

For a description of the sample tables that the examples on this page use, see [SELECT statement in DuckDB](duckdb-statement-select.md).

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

Returns a struct of the type of `target_type`. For each field of the target type, `mapping` names the field of `input` to copy into it, and the value is cast to the type of the target field. The `defaults` argument gives the values of the target fields that `mapping` does not name. Fields of `input` that `mapping` does not name are left out.

Each target field must get its value from either `mapping` or `defaults`, and not from both. Otherwise, the query fails. If `input` is `NULL`, the result is `NULL`.

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

```sql
remap_struct({{input}}, {{target_type}}, {{mapping}}, {{defaults}})
```

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

The `remap_struct` function takes the following arguments:

**{{input}}**  
The struct to convert, as a `STRUCT` value.

**{{target\_type}}**  
A value of the target `STRUCT` type. Only its type is used, so pass `NULL` cast to that type, as in `NULL::STRUCT(product_id BIGINT, product_name VARCHAR)`.

**{{mapping}}**  
The field mapping. It must be a constant `STRUCT`. Each field name is the name of a target field, and each value is the name of the field of `input` to copy into it, as a string. For a target field that is itself a struct, the value can be a pair of the source field name and a nested mapping, as in `('info', {'color': 'c'})`.

**{{defaults}}**  
The default values for the target fields that `mapping` does not name, as a constant `STRUCT` with a value for each of those fields. Each value must already have the type of its target field, so cast it if needed, as in `NULL::INTEGER`. If `mapping` names every target field, pass `NULL`.

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

`STRUCT`, with the field names and types of `target_type`.

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

The following SQL query copies field `j` into `v1` and field `i` into `v3`, and sets `v2` to a `NULL` default.

```sql
SELECT
    remap_struct(
        {'i': 1, 'j': 2},
        NULL::ROW(v1 INT, v2 INT, v3 INT),
        {'v1': 'j', 'v3': 'i'},
        {'v2': NULL::INTEGER}
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'v1': 2, 'v2': NULL, 'v3': 1}` | 

The following SQL query renames two fields, casts `id` from `INTEGER` to `BIGINT`, and adds a field `qty` with the default value `1`.

```sql
SELECT
    remap_struct(
        {'id': 7, 'name': 'widget'},
        NULL::STRUCT(
            product_id BIGINT, product_name VARCHAR,
            qty INTEGER
        ),
        {'product_id': 'id', 'product_name': 'name'},
        {'qty': 1}
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'product_id': 7, 'product_name': widget, 'qty': 1}` | 

The following SQL query converts a struct column, one row at a time. `mapping` names every target field, so `defaults` is `NULL`.

```sql
SELECT
    remap_struct(
        s,
        NULL::STRUCT(
            product_id BIGINT, product_name VARCHAR
        ),
        {'product_id': 'id', 'product_name': 'name'},
        NULL
    ) AS result
FROM (VALUES
    ({'id': 8, 'name': 'gadget'}),
    ({'id': 7, 'name': 'widget'})
) AS t(s)
ORDER BY s.id
```

The query returns the following results.


| result | 
| --- | 
| `{'product_id': 7, 'product_name': widget}` | 
| `{'product_id': 8, 'product_name': gadget}` | 

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

`mapping` and `defaults` must be constants. You cannot build them from a column.

If `mapping` names a field that `input` does not have, or if a value cannot be cast to the type of its target field, the query fails.

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

Returns a struct whose fields are the arguments, in order. The fields have no names, so `typeof` reports the type as `TUPLE`, for example `TUPLE(INTEGER, VARCHAR)`. Two or more values in parentheses, such as `(1, 'shipped')`, create the same value.

To read a field, use its position, as in `row(1, 'shipped')[2]`, or `struct_extract_at`. To give the fields names, cast the value to a `STRUCT` type, as in `CAST(row(1, 'shipped') AS STRUCT(id INTEGER, note VARCHAR))`. A `NULL` argument becomes a `NULL` field, and the struct itself is not `NULL`.

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

```sql
row({{value}} [, ...])
```

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

The `row` function takes the following argument:

**{{value}}**  
The value of a field, of any type. Pass one argument for each field.

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

`TUPLE`, an unnamed struct with the types of the arguments in order, such as `TUPLE(INTEGER, VARCHAR)` for `row(1, 'shipped')`.

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

The following SQL query builds an unnamed struct with three fields for each of the numbers `0`, `1`, and `2` that `range(3)` returns. The `/` operator returns a `DOUBLE`, so the third field is a `DOUBLE`.

```sql
SELECT row(i, i % 4, i / 4) AS result
FROM range(3) AS t(i)
ORDER BY i
```

The query returns the following results.


| result | 
| --- | 
| `(0, 0, 0.0)` | 
| `(1, 1, 0.25)` | 
| `(2, 2, 0.5)` | 

The following SQL query reads the second field of an unnamed struct by its position.

```sql
SELECT row(1, 'shipped')[2] AS result
```

The query returns the following result.


| result | 
| --- | 
| `shipped` | 

The following SQL query casts an unnamed struct to a `STRUCT` type, which gives the fields names.

```sql
SELECT
    CAST(
        row(
            1, 'shipped'
        ) AS STRUCT(id INTEGER, note VARCHAR)
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'id': 1, 'note': shipped}` | 

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

Query results show an unnamed struct as its values in parentheses, such as `(1, shipped)`.

Iceberg tables cannot store unnamed structs. Before you store the result in an Iceberg table, cast it to a `STRUCT` type with field names.

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

Returns a `STRUCT` that has every field of every argument, in argument order. Pass structs whose fields have names, or unnamed structs, which have the type `TUPLE`, such as values that `row` builds. You cannot mix the two kinds in one call.

Field names must be unique across all arguments without regard to case, so two arguments with the fields `a` and `A` make the query fail. If an argument is a `NULL` struct, its fields are `NULL` in the result. The result itself is not `NULL`.

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

```sql
struct_concat({{struct1}}, {{struct2}} [, ...])
```

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

The `struct_concat` function takes the following arguments:

**{{struct1}}**  
The first struct to combine, as a `STRUCT` or `TUPLE` value.

**{{struct2}}**  
The second struct to combine, of the same kind as `struct1`. You can pass more values after it.

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

`STRUCT` with the fields of every argument, in order, or `TUPLE` when the arguments are unnamed structs. For example, combining `STRUCT(i INTEGER)` and `STRUCT(s VARCHAR)` returns `STRUCT(i INTEGER, s VARCHAR)`.

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

The following SQL query combines two structs that each have one field.

```sql
SELECT
    struct_concat(
        struct_pack(i := 4), struct_pack(s := 'string')
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'i': 4, 's': string}` | 

The following SQL query combines two unnamed structs. The result is an unnamed struct with three fields.

```sql
SELECT struct_concat(ROW(1, 2), ROW(3)) AS result
```

The query returns the following result.


| result | 
| --- | 
| `(1, 2, 3)` | 

The following SQL query combines a struct with a `NULL` struct. The result is not `NULL`: the field from the `NULL` struct is `NULL`.

```sql
SELECT
    struct_concat(
        {'a': 1}, CAST(NULL AS STRUCT(b INTEGER))
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'a': 1, 'b': NULL}` | 

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

A `NULL` literal with no type is not a struct, so `struct_concat({'a': 1}, NULL)` fails. Cast it to a struct type first, as in `CAST(NULL AS STRUCT(b INTEGER))`.

To add fields to a struct by name, you can also use the [`struct_insert` function](#athena-duckdb-functions-struct-map-struct-insert).

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

Returns `true` if at least one field of `struct` equals `value`, and `false` if none does. The `struct` argument must be an unnamed struct, which has the type `TUPLE`, such as a value that `row` builds. For a struct whose fields have names, the query fails, so pass it through `struct_values` first.

If `value` or `struct` is `NULL`, the result is `NULL`. A `NULL` field never matches. To get the position of the matching field, use the [`struct_position` function](#athena-duckdb-functions-struct-map-struct-position).

`struct_has` is an alias of `struct_contains`.

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

```sql
struct_contains({{struct}}, {{value}})
```

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

The `struct_contains` function takes the following arguments:

**{{struct}}**  
The unnamed struct (`TUPLE`) to search.

**{{value}}**  
The value to look for.

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

`BOOLEAN`

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

The following SQL query tests whether an unnamed struct has a field equal to `3`.

```sql
SELECT struct_contains(ROW(3, 3, 0), 3) AS result
```

The query returns the following result.


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

The following SQL query uses the `struct_has` alias. No field equals `'d'`, so the result is `false`.

```sql
SELECT struct_has(ROW('a', 'b', 'c'), 'd') AS result
```

The query returns the following result.


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

The following SQL query searches a struct with named fields by passing it through `struct_values`.

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

The query returns the following result.


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

The following SQL query searches for `NULL`. The result is `NULL`, even though the struct has a `NULL` field.

```sql
SELECT struct_contains(ROW(1, NULL), NULL) AS result
```

The query returns the following result.


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

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

Numbers of different types are compared as numbers, so `2` matches a field that holds `2.0`. A string never matches a number, and the function does not convert one to the other: `struct_contains(ROW(1, 2), '2')` returns `false`.

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

Returns the value of the field named `name` in `struct`. Field names are matched without regard to case. For an unnamed struct, which has the type `TUPLE`, you can pass the 1-based position of the field instead of a name.

The name or position must be a constant, and it must identify a field of the struct. A name that the struct does not have, or a position outside the struct, makes the query fail. If `struct` is `NULL`, the result is `NULL`.

`{{struct}}.{{name}}` and `{{struct}}['{{name}}']` return the same result as `struct_extract({{struct}}, '{{name}}')`, and `{{tuple}}[{{position}}]` returns the same result as `struct_extract({{tuple}}, {{position}})`.

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

```sql
struct_extract({{struct}}, {{name}})
struct_extract({{tuple}}, {{position}})
```

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

The `struct_extract` function takes the following arguments:

**{{struct}}**  
The struct to read from, as a `STRUCT` value.

**{{name}}**  
The name of the field. It must be a constant `VARCHAR` value.

**{{tuple}}**  
The unnamed struct (`TUPLE`) to read from.

**{{position}}**  
The 1-based position of the field. It must be a constant `BIGINT` value.

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

The type of the field that you extract. For example, extracting an `INTEGER` field returns `INTEGER`, and extracting a `VARCHAR` field returns `VARCHAR`.

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

The following SQL query returns the field named `i`.

```sql
SELECT
    struct_extract(
        {'i': 3, 'v2': 3, 'v3': 0}, 'i'
    ) AS result
```

The query returns the following result.


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

The following SQL query returns the second field of an unnamed struct.

```sql
SELECT struct_extract(ROW(10, 'a'), 2) AS result
```

The query returns the following result.


| result | 
| --- | 
| `a` | 

The following SQL query reads the same field of a struct column in three ways. The function call matches `'NAME'` to the field `name` because names are matched without regard to case.

```sql
SELECT
    s.name AS by_dot,
    s['name'] AS by_bracket,
    struct_extract(s, 'NAME') AS by_function
FROM (VALUES ({'id': 1, 'name': 'alice'})) AS t(s)
```

The query returns the following result.


| by\_dot | by\_bracket | by\_function | 
| --- | --- | --- | 
| `alice` | `alice` | `alice` | 

The following SQL query reads a field of a `NULL` struct, so the result is `NULL`.

```sql
SELECT
    struct_extract(
        CAST(NULL AS STRUCT(a INTEGER)), 'a'
    ) AS result
```

The query returns the following result.


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

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

A position works only on an unnamed struct. On a struct whose fields have names, `struct_extract({{struct}}, 2)` and `{{struct}}[2]` fail. To read a named field by position, use the [`struct_extract_at` function](#athena-duckdb-functions-struct-map-struct-extract-at).

The name cannot come from a column or another expression that is not a constant.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-struct-map-struct-extract-differences-from-engine-version-3"></a>

In Athena engine version 3, the subscript operator reads a field of a `ROW` value by its position even when the fields have names, as in `{{row}}[2]`. In DuckDB, a position works only on an unnamed struct, so `{{struct}}[2]` fails on a struct whose fields have names, such as a struct column of a table. To read a named field by position, use `struct_extract_at({{struct}}, 2)`, or read the field by name.

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

Returns the field at the 1-based position `position` in `struct`. It works on structs whose fields have names and on unnamed structs, so use it to read a field by position when the fields have names. A numeric subscript, such as `{{struct}}[2]`, works only on an unnamed struct. If `struct` is `NULL`, the result is `NULL`.

The position must be a constant from `1` to the number of fields. A position of `0`, a negative position, or a position past the last field makes the query fail.

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

```sql
struct_extract_at({{struct}}, {{position}})
```

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

The `struct_extract_at` function takes the following arguments:

**{{struct}}**  
The struct to read from, as a `STRUCT` or an unnamed struct (`TUPLE`).

**{{position}}**  
The 1-based position of the field. It must be a constant `BIGINT` value.

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

The type of the field at that position. For example, a `VARCHAR` field returns `VARCHAR`.

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

The following SQL query returns the second field of a struct whose fields have names.

```sql
SELECT
    struct_extract_at(
        {'id': 7, 'name': 'alice', 'active': true}, 2
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `alice` | 

The following SQL query returns the second field of a struct column for each row.

```sql
SELECT struct_extract_at(s, 2) AS second_field
FROM (VALUES
    ({'id': 1, 'name': 'alice'}),
    ({'id': 2, 'name': 'bob'})
) AS t(s)
ORDER BY second_field
```

The query returns the following results.


| second\_field | 
| --- | 
| `alice` | 
| `bob` | 

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

Returns a copy of `struct` with new fields added after its existing fields. Write each new field as `{{name}} := {{value}}`, where `name` becomes the field name. To change the value of a field that already exists, use the [`struct_update` function](#athena-duckdb-functions-struct-map-struct-update).

If a new name equals the name of an existing field without regard to case, the query fails. If `struct` is `NULL`, the result is not `NULL`: it is a struct whose existing fields are `NULL`, followed by the new fields.

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

```sql
struct_insert({{struct}}, {{name}} := {{value}} [, ...])
```

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

The `struct_insert` function takes the following arguments:

**{{struct}}**  
The struct to add fields to, as a `STRUCT` value. Its fields must have names.

**{{name}}**  
The name of a new field, written as an identifier, not as a string.

**{{value}}**  
The value of the new field, as an expression of any type.

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

`STRUCT` with the fields of `struct` followed by the new fields. For example, `struct_insert({'a': 1}, b := 2)` returns `STRUCT(a INTEGER, b INTEGER)`.

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

The following SQL query adds the field `b` to a struct.

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

The query returns the following result.


| result | 
| --- | 
| `{'a': 1, 'b': 2}` | 

The following SQL query adds two fields of different types in one call.

```sql
SELECT
    struct_insert(
        {'id': 1}, name := 'alice', tags := ['x', 'y']
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'id': 1, 'name': alice, 'tags': [x, y]}` | 

The following SQL query adds a field to a `NULL` struct. The result is a struct, not `NULL`.

```sql
SELECT
    struct_insert(
        CAST(NULL AS STRUCT(a INTEGER)), b := 2
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'a': NULL, 'b': 2}` | 

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

`struct_insert` works only on a struct whose fields have names. To add a field to an unnamed struct, use the [`struct_concat` function](#athena-duckdb-functions-struct-map-struct-concat) with another unnamed struct, as in `struct_concat(ROW(1, 2), ROW(3))`.

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

Returns a list of the field names of `struct`, in field order. Only the top-level names are returned, not the names of fields inside a nested struct.

If `struct` is `NULL`, the result is `NULL`, not the list of names. To get the field values instead of the names, use the [`struct_values` function](#athena-duckdb-functions-struct-map-struct-values).

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

```sql
struct_keys({{struct}})
```

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

The `struct_keys` function takes the following argument:

**{{struct}}**  
The struct whose field names you want, as a `STRUCT` value. Its fields must have names.

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

`VARCHAR[]`, a list of `VARCHAR` values.

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

The following SQL query returns the field names of a struct.

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

The query returns the following result.


| result | 
| --- | 
| `[a, b]` | 

The following SQL query returns only the top-level names. The names `city` and `zip` of the nested struct are not included.

```sql
SELECT
    struct_keys(
        {
            'id': 1,
            'address': {
                'city': 'Seattle', 'zip': '98101'
            }
        }
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[id, address]` | 

The following SQL query passes a `NULL` struct, so the result is `NULL`.

```sql
SELECT
    struct_keys(
        CAST(NULL AS STRUCT(a INTEGER))
    ) IS NULL AS is_null
```

The query returns the following result.


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

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

`struct_keys` fails for an unnamed struct (`TUPLE`) and for a `MAP`. To get the keys of a map, use the [`map_keys` function](duckdb-functions-struct-map-maps.md#athena-duckdb-functions-struct-map-map-keys).

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

Returns a `STRUCT` with one field for each argument, in argument order. Name each field with `{{name}} := {{value}}`. If you pass a column reference without a name, the field takes the name of the column.

The result is the same as a struct literal such as `{'i': 4, 's': 'string'}`, but you write each field name as an identifier, not as a string. Field names must be unique without regard to case. A `NULL` value gives a `NULL` field, not a `NULL` struct.

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

```sql
struct_pack({{name}} := {{value}} [, ...])
struct_pack({{column}} [, ...])
```

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

The `struct_pack` function takes the following arguments:

**{{name}}**  
The name of a field, written as an identifier, not as a string.

**{{value}}**  
The value of the field, as an expression of any type.

**{{column}}**  
A column reference. The field gets the name of the column. You can mix column references and `{{name}} := {{value}}` arguments in one call.

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

`STRUCT` with one field for each argument, typed by its value. For example, `struct_pack(i := 4, s := 'string')` returns `STRUCT(i INTEGER, s VARCHAR)`.

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

The following SQL query builds a struct with two named fields.

```sql
SELECT struct_pack(i := 4, s := 'string') AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'i': 4, 's': string}` | 

The following SQL query builds a struct from table columns. Each field takes the name of its column.

```sql
SELECT struct_pack(order_id, customer, amount) AS result
FROM sales_db.sales_parquet
WHERE order_id <= 2
ORDER BY order_id
```

The query returns the following results.


| result | 
| --- | 
| `{'order_id': 1, 'customer': alice, 'amount': 120.50}` | 
| `{'order_id': 2, 'customer': bob, 'amount': 75.00}` | 

The following SQL query passes a `NULL` value. The field is `NULL`, and the struct is not.

```sql
SELECT
    struct_pack(
        id := 1, note := CAST(NULL AS VARCHAR)
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'id': 1, 'note': NULL}` | 

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

An argument that is an expression other than a column reference needs a name. For example, `struct_pack(x + 1)` fails, and `struct_pack(next := x + 1)` works.

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

Returns the 1-based position of the first field of `struct` that equals `value`. If no field matches, the result is `NULL`. `struct` must be an unnamed struct, which has the type `TUPLE`. For a struct whose fields have names, the query fails, so pass it through `struct_values` first.

Unlike `struct_contains`, `struct_position` can find a `NULL` field: when `value` is `NULL`, the result is the position of the first `NULL` field. If `struct` is `NULL`, the result is `NULL`.

`struct_indexof` is an alias of `struct_position`.

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

```sql
struct_position({{struct}}, {{value}})
```

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

The `struct_position` function takes the following arguments:

**{{struct}}**  
The unnamed struct (`TUPLE`) to search.

**{{value}}**  
The value to look for.

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

`INTEGER`

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

The following SQL query returns the position of the first field that equals `3`. Two fields equal `3`, and the first one is at position `1`.

```sql
SELECT struct_position(ROW(3, 3, 0), 3) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1` | 

The following SQL query uses the `struct_indexof` alias. The third field equals `'c'`, so the result is `3`.

```sql
SELECT struct_indexof(ROW('a', 'b', 'c'), 'c') AS result
```

The query returns the following result.


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

The following SQL query looks for a value that no field has, so the result is `NULL`.

```sql
SELECT struct_position(ROW(3, 3, 0), 7) AS result
```

The query returns the following result.


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

The following SQL query finds the first `NULL` field.

```sql
SELECT struct_position(ROW(1, NULL, 3), NULL) AS result
```

The query returns the following result.


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

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

A string never matches a number, and the function does not convert one to the other. For example, `struct_position(ROW(1, 2), '2')` returns `NULL`.

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

Returns a copy of `struct` in which each field that you name in a `{{name}} := {{value}}` argument has the new value. The field keeps its position, and the new value can have a different type than the old one. A name that the struct does not have adds a new field at the end.

Names are matched without regard to case, and a changed field takes the name as you write it in the call. If `struct` is `NULL`, the result is not `NULL`: it is a struct with the new values, in which the other fields are `NULL`.

To add fields and have the query fail when a field already exists, use the [`struct_insert` function](#athena-duckdb-functions-struct-map-struct-insert).

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

```sql
struct_update({{struct}}, {{name}} := {{value}} [, ...])
```

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

The `struct_update` function takes the following arguments:

**{{struct}}**  
The struct to change, as a `STRUCT` value. Its fields must have names.

**{{name}}**  
The name of a field to change or add, written as an identifier, not as a string.

**{{value}}**  
The new value of the field, as an expression of any type.

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

`STRUCT`. Each field that you change takes the type of its new value. For example, `struct_update({'a': 1, 'b': 2}, b := 'two')` returns `STRUCT(a INTEGER, b VARCHAR)`.

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

The following SQL query changes the value of the field `a`.

```sql
SELECT struct_update({'a': 1}, a := 2) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'a': 2}` | 

The following SQL query changes the field `status` from an `INTEGER` to a `VARCHAR`, and adds the field `note`, which the struct did not have.

```sql
SELECT
    struct_update(
        {'id': 1, 'status': 0}, status := 'shipped',
        note := 'gift'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'id': 1, 'status': shipped, 'note': gift}` | 

The following SQL query changes a field of a `NULL` struct. The result is a struct in which the field that you did not change is `NULL`.

```sql
SELECT
    struct_update(
        CAST(NULL AS STRUCT(a INTEGER, b INTEGER)),
        a := 2
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'a': 2, 'b': NULL}` | 

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

`struct_update` works only on a struct whose fields have names. It fails for an unnamed struct (`TUPLE`).

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

Returns an unnamed struct, which has the type `TUPLE`, that holds the values of the fields of `struct` in field order, without their names. Each value keeps its type. If `struct` is `NULL`, the result is `NULL`.

Use `struct_values` to pass a struct whose fields have names to a function that accepts only an unnamed struct, such as `struct_contains` or `struct_position`. To get the field names instead, use the [`struct_keys` function](#athena-duckdb-functions-struct-map-struct-keys).

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

```sql
struct_values({{struct}})
```

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

The `struct_values` function takes the following argument:

**{{struct}}**  
The struct whose field values you want, as a `STRUCT` value.

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

`TUPLE` with the field types of `struct`, in order. For example, a `STRUCT(a INTEGER, b VARCHAR)` value returns `TUPLE(INTEGER, VARCHAR)`.

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

The following SQL query returns the field values of a struct.

```sql
SELECT struct_values({'a': 1, 'b': 'world'}) AS result
```

The query returns the following result.


| result | 
| --- | 
| `(1, world)` | 

The following SQL query finds the position of the first field that equals `0` in a struct whose fields have names.

```sql
SELECT
    struct_position(
        struct_values({'q1': 10, 'q2': 0, 'q3': 7}), 0
    ) AS first_zero
```

The query returns the following result.


| first\_zero | 
| --- | 
| `2` | 

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

Iceberg tables cannot store `TUPLE` values. To write the result to an Iceberg table, cast it to a struct whose fields have names, as in `CAST(struct_values({{struct}}) AS STRUCT(x INTEGER, y VARCHAR))`.

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

Returns the value that `union` holds when its current member is `tag`. When `union` holds a different member, the result is `NULL`. If `union` is `NULL`, the result is `NULL`.

The tag must be a constant that names a member of the `UNION` type. It is matched without regard to case. A tag that is not a member of the type makes the query fail. `{{union}}.{{tag}}` returns the same result as `union_extract({{union}}, '{{tag}}')`.

To find out which member a value holds, use the [`union_tag` function](#athena-duckdb-functions-struct-map-union-tag).

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

```sql
union_extract({{union}}, {{tag}})
```

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

The `union_extract` function takes the following arguments:

**{{union}}**  
The union to read from, as a `UNION` value.

**{{tag}}**  
The name of a member of the `UNION` type. It must be a constant `VARCHAR` value.

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

The type of the member that `tag` names. For example, the member `num` of `UNION(num INTEGER, str VARCHAR)` returns `INTEGER`.

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

The following SQL query reads the member `k` of a `UNION` value.

```sql
SELECT
    union_extract(union_value(k := 'x'), 'k') AS result
```

The query returns the following result.


| result | 
| --- | 
| `x` | 

The following SQL query reads both members of a `UNION` column. Each row holds only one member, so the other member is `NULL`.

```sql
SELECT
    union_extract(u, 'num') AS num,
    union_extract(u, 'str') AS str
FROM (VALUES
    (CAST(5 AS UNION(num INTEGER, str VARCHAR))),
    (CAST('abc' AS UNION(num INTEGER, str VARCHAR)))
) AS t(u)
ORDER BY num NULLS LAST
```

The query returns the following results.


| num | str | 
| --- | --- | 
| `5` | `NULL` | 
| `NULL` | `abc` | 

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

Returns the tag of the current member of `union`, which is the name of the member that holds the value. The result is an `ENUM` whose values are the member names of the `UNION` type. If `union` is `NULL`, the result is `NULL`.

To read the value of a member, use the [`union_extract` function](#athena-duckdb-functions-struct-map-union-extract).

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

```sql
union_tag({{union}})
```

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

The `union_tag` function takes the following argument:

**{{union}}**  
The union to inspect, as a `UNION` value.

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

`ENUM` of the member names of the `UNION` type. For example, a `UNION(num INTEGER, str VARCHAR)` value returns `ENUM('num', 'str')`.

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

The following SQL query returns the tag of a `UNION` value, cast to `VARCHAR`.

```sql
SELECT
    CAST(
        union_tag(union_value(k := 'foo')) AS VARCHAR
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `k` | 

The following SQL query returns the tag of each row of a `UNION` column.

```sql
SELECT CAST(union_tag(u) AS VARCHAR) AS tag
FROM (VALUES
    (CAST(5 AS UNION(num INTEGER, str VARCHAR))),
    (CAST('abc' AS UNION(num INTEGER, str VARCHAR)))
) AS t(u)
ORDER BY tag
```

The query returns the following results.


| tag | 
| --- | 
| `num` | 
| `str` | 

The following SQL query keeps only the rows whose current member is `str`, by comparing the tag with a string.

```sql
SELECT union_extract(u, 'str') AS str
FROM (VALUES
    (CAST(5 AS UNION(num INTEGER, str VARCHAR))),
    (CAST('abc' AS UNION(num INTEGER, str VARCHAR)))
) AS t(u)
WHERE union_tag(u) = 'str'
```

The query returns the following result.


| str | 
| --- | 
| `abc` | 

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

A query result cannot hold an `ENUM` value, so a query that returns `union_tag({{union}})` as a column fails. Cast the result to `VARCHAR`, as in `CAST(union_tag({{union}}) AS VARCHAR)`, or compare it with a string, as in `union_tag({{union}}) = 'num'`.

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

Returns a value of type `UNION` with a single member, whose tag is `tag` and whose value is `value`. A `UNION` value holds one value at a time, and its tag tells you which member holds it.

To build a value of a `UNION` type that has several members, cast the result to that type, as in `CAST(union_value(num := 5) AS UNION(num INTEGER, str VARCHAR))`. To read the value, use the [`union_extract` function](#athena-duckdb-functions-struct-map-union-extract). To read the tag, use the [`union_tag` function](#athena-duckdb-functions-struct-map-union-tag).

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

```sql
union_value({{tag}} := {{value}})
```

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

The `union_value` function takes the following arguments:

**{{tag}}**  
The name of the member, written as an identifier, not as a string.

**{{value}}**  
The value to store, as an expression of any type.

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

`UNION` with one member, named by `tag` and typed by `value`. For example, `union_value(k := 'hello')` returns `UNION(k VARCHAR)`.

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

The following SQL query builds a `UNION` value with the member `k`. The result shows only the value of the member, without its tag.

```sql
SELECT union_value(k := 'hello') AS result
```

The query returns the following result.


| result | 
| --- | 
| `hello` | 

The following SQL query casts a one-member `UNION` value to a `UNION` type with two members.

```sql
SELECT
    CAST(
        union_value(num := 5) AS UNION(
            num INTEGER, str VARCHAR
        )
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `5` | 

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

Pass exactly one `{{tag}} := {{value}}` argument. A call without a tag, such as `union_value('hello')`, or with more than one argument fails.

A query result shows a `UNION` value as the value of its current member, without the tag.

Hive tables and Iceberg tables cannot store `UNION` values. Before you write the value to a table, extract it with `union_extract` or cast it to another type.