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

# How to rename a column that has a dot in its name

> Why RENAME COLUMN fails with "Cannot rename column from nested struct to normal column and vice versa" for a column name with a dot, and how to move the data to a new column.

`ALTER TABLE ... RENAME COLUMN` cannot add or remove a dot in a column name. ClickHouse reads the part of the name before the first dot as the name of a [`Nested`](https://clickhouse.com/docs/reference/data-types/nested-data-structures/index) structure, and a rename is not allowed to move a column into or out of such a structure. To get rid of the dot, add a column with the new name, copy the data into it, and drop the old column.

## The error

```sql theme={null}
CREATE TABLE events
(
    id UInt32,
    `payload.status` String
)
ENGINE = MergeTree
ORDER BY id;

INSERT INTO events VALUES (1, 'new'), (2, 'paid'), (3, 'shipped');

ALTER TABLE events RENAME COLUMN `payload.status` TO payload_status;
```

```text theme={null}
Code: 36. DB::Exception: Cannot rename column from nested struct to normal column and vice versa. (BAD_ARGUMENTS)
```

## Why this happens

A column name with a dot is valid. It is how the elements of a `Nested` column are stored (`payload Nested(status String)` becomes the column `payload.status` of type `Array(String)`), and ingestion tools that flatten nested JSON often produce such names as well. Because of that, `RENAME COLUMN` treats `payload` as the name of a nested structure, whatever the type of the column is:

* Renaming within the same prefix works: `payload.status` to `payload.state`.
* Renaming to another prefix fails with `Cannot rename column from one nested name to another`.
* Renaming between a name with a dot and a name without one fails with the error above.

This is described in the [limitations of `ALTER TABLE ... COLUMN`](https://clickhouse.com/docs/reference/statements/alter/column#limitations).

## Copy the data to a new column

Add the new column with the old column as its `DEFAULT` expression:

```sql theme={null}
ALTER TABLE events ADD COLUMN payload_status String DEFAULT `payload.status`;
```

This changes only the table metadata. From now on, reads of `payload_status` return the value of `payload.status`, and an `INSERT` that still writes only `payload.status` stores the same value in both columns. You can switch readers and writers to the new name at your own pace.

Then write the values of the new column into the existing data parts:

```sql theme={null}
ALTER TABLE events MATERIALIZE COLUMN payload_status SETTINGS mutations_sync = 2;
```

[`MATERIALIZE COLUMN`](https://clickhouse.com/docs/reference/statements/alter/column#materialize-column) is a mutation: it rewrites the column in every part and runs in the background by default. With [`mutations_sync = 2`](https://clickhouse.com/docs/reference/settings/session-settings/mutations#mutations_sync) the statement returns only when the mutation has finished on all replicas. If you run it without this setting, wait until [`system.mutations`](https://clickhouse.com/docs/reference/system-tables/mutations) shows it as done:

```sql theme={null}
SELECT command, is_done
FROM system.mutations
WHERE database = currentDatabase() AND table = 'events';
```

```text theme={null}
   ┌─command─────────────────────────────┬─is_done─┐
1. │ (MATERIALIZE COLUMN payload_status) │       1 │
   └─────────────────────────────────────┴─────────┘
```

> [!WARNING]
>
> Do not continue before the mutation is done. Until a data part is rewritten, the values of `payload_status` in it exist only as an expression over `payload.status`. If you remove the `DEFAULT` expression earlier, `payload_status` is empty for the rows of such parts.

When the mutation is done and nothing writes to the old column any more, remove the dependency between the two columns and drop the old one:

```sql theme={null}
ALTER TABLE events MODIFY COLUMN payload_status REMOVE DEFAULT;
ALTER TABLE events DROP COLUMN `payload.status`;

SELECT * FROM events ORDER BY id;
```

```text theme={null}
   ┌─id─┬─payload_status─┐
1. │  1 │ new            │
2. │  2 │ paid           │
3. │  3 │ shipped        │
   └────┴────────────────┘
```

Without the `REMOVE DEFAULT` step, the `DROP COLUMN` is rejected, because `payload_status` still depends on the old column (error `ILLEGAL_COLUMN`).

## If the column is part of the sorting key

A column that is used in the `ORDER BY`, `PRIMARY KEY` or `PARTITION BY` expression cannot be dropped, so the last step of the procedure above fails:

```sql theme={null}
CREATE TABLE events_by_status
(
    `payload.status` String,
    id UInt32
)
ENGINE = MergeTree
ORDER BY (`payload.status`, id);

INSERT INTO events_by_status VALUES ('new', 1), ('paid', 2), ('shipped', 3);

ALTER TABLE events_by_status DROP COLUMN `payload.status`;
```

```text theme={null}
Code: 524. DB::Exception: Trying to ALTER DROP key `payload.status` column which is a part of key expression. (ALTER_OF_COLUMN_IS_FORBIDDEN)
```

You have two options:

* Keep the stored column and give it a second name with an `ALIAS` column, as described in [How to rename a column that is part of the sorting key](https://clickhouse.com/docs/resources/support-center/knowledge-base/tables-schema/rename-key-column-with-alias-column.md).
* Create a new table with the column names you want, copy the data with `INSERT INTO ... SELECT`, and swap the two tables with [`EXCHANGE TABLES`](https://clickhouse.com/docs/reference/statements/exchange):

```sql theme={null}
CREATE TABLE events_by_status_new
(
    payload_status String,
    id UInt32
)
ENGINE = MergeTree
ORDER BY (payload_status, id);

INSERT INTO events_by_status_new (payload_status, id)
SELECT `payload.status`, id
FROM events_by_status;

EXCHANGE TABLES events_by_status AND events_by_status_new;

SELECT * FROM events_by_status ORDER BY id;
```

```text theme={null}
   ┌─payload_status─┬─id─┐
1. │ new            │  1 │
2. │ paid           │  2 │
3. │ shipped        │  3 │
   └────────────────┴────┘
```

After the exchange, `events_by_status_new` holds the old data and can be dropped. Rows inserted into the old table after the `INSERT INTO ... SELECT` has started are not copied, and after the exchange they are only in the table that you are about to drop. Stop writes before the copy starts and resume them only after `EXCHANGE TABLES`. From then on, an `INSERT` that names the column has to use `payload_status`.

## Avoid the problem in new tables

If you control the ingestion pipeline, configure it to join flattened keys with another separator, such as `_`.

Since version 26.4, a `MergeTree` table can also be created with the setting [`share_nested_offsets = 0`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/other#share_nested_offsets). In such a table a dot in a column name has no special meaning, and the rename works:

```sql theme={null}
CREATE TABLE events_plain_names
(
    id UInt32,
    `payload.status` String
)
ENGINE = MergeTree
ORDER BY id
SETTINGS share_nested_offsets = 0;

ALTER TABLE events_plain_names RENAME COLUMN `payload.status` TO payload_status;
```

The setting can only be chosen when the table is created. `ALTER TABLE ... MODIFY SETTING share_nested_offsets = 0` on an existing table fails with `Setting 'share_nested_offsets' is readonly for storage 'MergeTree'`.
