Unexpected value N in enum (code 691, UNKNOWN_ELEMENT_OF_ENUM) means that the column contains the number N, and the current Enum definition has no name for it. The usual cause is an ALTER TABLE ... MODIFY COLUMN that changed the number of an existing member or removed a member that is used. An Enum column stores the numbers, and such an ALTER changes the definition without converting the stored numbers. No data is lost: give every stored number a name again and the column is readable.
Example
ALTER is accepted, but the row stored with the number 1 can no longer be shown:
ALTER is reported as done, and a filter by the renumbered name returns no rows instead of failing: SELECT id FROM orders WHERE status = 'new' returns an empty result here.
ClickHouse rejects some of these statements with
CANNOT_CONVERT_TYPE and a message like Enum conversion changes value for element 'shipped' from 2 to 20, but the check doesn’t catch every case, as the example shows. This is tracked in issue #116182. Don’t rely on the check.Find out which numbers are stored
Converting the column to its numeric type works even when the names can’t be resolved. UsetoInt8 for Enum8 and toInt16 for Enum16:
Make the column readable again
If no rows were inserted after the change, restore the previous definition:UPDATE mutation has finished and no row uses the temporary name, you can remove it from the definition.
Like the original change, each of these ALTER TABLE ... MODIFY COLUMN statements starts a mutation that rewrites the column in existing parts.
If such an ALTER is rejected with CANNOT_CONVERT_TYPE, you can still read the data by number and copy it into a new table. A cast of the stored number to a definition that names every number works in a SELECT:
Change the number of a member safely
Convert through the name, not through the number. One way is to change the column toString and back. Both steps are mutations that rewrite the column:
toString function returns the name of an Enum value, and the name is resolved against the definition of the target column. This is the only way when the column is part of the sorting key, because such a column can’t be converted to String in place (the ALTER fails with ALTER_OF_COLUMN_IS_FORBIDDEN):
EXCHANGE TABLES. The copy sees the rows that exist when it starts, and the swap does not move later rows: a row inserted in between ends up only in the old table, which now has the name orders_new.
Which changes are safe
The practical rule is to only add members to an
Enum of a table that holds data. Since ClickHouse 26.6 you can add members without repeating the whole definition. The number is optional and defaults to the next free one, so 'returned' gets the number 22 here: