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 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
Why this happens
A column name with a dot is valid. It is how the elements of aNested 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.statustopayload.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.
ALTER TABLE ... COLUMN.
Copy the data to a new column
Add the new column with the old column as itsDEFAULT expression:
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:
MATERIALIZE COLUMN is a mutation: it rewrites the column in every part and runs in the background by default. With mutations_sync = 2 the statement returns only when the mutation has finished on all replicas. If you run it without this setting, wait until system.mutations shows it as done:
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 theORDER BY, PRIMARY KEY or PARTITION BY expression cannot be dropped, so the last step of the procedure above fails:
- Keep the stored column and give it a second name with an
ALIAScolumn, as described in How to rename a column that is part of the sorting key. - Create a new table with the column names you want, copy the data with
INSERT INTO ... SELECT, and swap the two tables withEXCHANGE TABLES:
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. In such a table a dot in a column name has no special meaning, and the rename works:
ALTER TABLE ... MODIFY SETTING share_nested_offsets = 0 on an existing table fails with Setting 'share_nested_offsets' is readonly for storage 'MergeTree'.