> ## Documentation Index
> Fetch the complete documentation index at: https://docs.risingwave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Struct

> Use the struct data type to create a column that contains nested columns. The nested columns can be of different data types, including the struct type.

## Define a struct type

Syntax:`STRUCT< >`

Struct types are declared using the angle brackets (`<` and `>`).

<Note>
  Starting from v2.7.0, RisingWave supports empty struct types, allowing you to define a struct with zero fields, serving as a placeholder for potential future additions of fields. To declare an empty struct, include a space between the angle brackets `< >`. This prevents conflicts with the `<>` (not equal) operator during SQL parsing.
</Note>

### Examples

The statement below creates a table `x` that contains struct `a`, which contains two nested columns (`b` and `c`) that are both integers.

```sql theme={null}
CREATE TABLE x (a STRUCT <b INTEGER, c INTEGER>, d INTEGER);
```

The statement below creates a table `y` that contains struct `a`, which contains another struct `c`.

```sql theme={null}
CREATE TABLE y (a STRUCT <b STRUCT<c INTEGER>, d INTEGER>, e VARCHAR);
```

Below is a real world example.

```sql theme={null}
CREATE TABLE trip (
        id VARCHAR,
        started_at TIMESTAMP,
        completed_at TIMESTAMP,
        distance DOUBLE PRECISION,
        fare STRUCT <
            initial_charge DOUBLE PRECISION,
            subsequent_charge DOUBLE PRECISION,
            surcharge DOUBLE PRECISION,
            tolls DOUBLE PRECISION
            >
        );
```

You can also use the struct type to parse a string that contains data in JSON format into its proper format.

For instance, if your data is structured like so:

```sql theme={null}
{
  "nested": "{\"v1\": 10, \"v2\": \"hello\"}"
}
```

And you want it to be structured like so:

```json theme={null}
{
  "nested": {
    "v1": 10,
    "v2": "hello"
  }
}
```

You can parse it using the struct type.

```sql theme={null}
CREATE SOURCE z (nested STRUCT <v1 INTEGER, v2 VARCHAR>) FORMAT JSON;
```

## Add values to a struct

To add values to structs, enclose the nested data with `()` in the SQL statement. For example, `(1, true)`. Alternatively, you can also use `ROW(1, true)`.

### Examples

The statement below adds values to table `x`.

```sql theme={null}
INSERT INTO x VALUES (ROW(3, 4), 5);
```

The statement below adds values to table `y`.

```sql theme={null}
INSERT INTO y VALUES (ROW(ROW(6), 7), 8);
```

The statement below adds values to table `trip`.

```sql theme={null}
INSERT INTO trip VALUES
        (
            '1234ABCD',
            '2022-07-28 11:04:05',
            '2022-07-28 11:15:22',
            6.1,
            ROW(1.0, 4.0, 1.5, 2.0)
        );
```

## Retrieve data in a struct

To retrieve data in a struct, enclose the struct name with `()` and use the dot operator to specify the nested column. For example, to access the `initial_charge` column under `fare` in the `trip` schema, use `(fare).initial_charge`.

### Examples[](#examples-2 "Direct link to Examples")

```sql theme={null}
SELECT (a).b, d
FROM x;
```

```sql theme={null}
SELECT ((a).b).c, e
FROM y;
```

```sql theme={null}
SELECT id, (fare).initial_charge
FROM trip;
```

## Casting

Structs can be casted explicitly or implicitly to structs if the nested expressions and types can be casted.

### Examples

```sql theme={null}
SELECT (1, (2, 3))::STRUCT<i BIGINT, j STRUCT<a BIGINT, b VARCHAR>>;
-----Result
(1,(2,3))
```

```sql theme={null}
SELECT ROW(1, ROW('1', 1)) = ROW('1', ROW(1, '1'));
-----
t
```
