Insert values to enums

710 views
Skip to first unread message

garif...@tvil.ru

unread,
Aug 31, 2016, 8:58:23 AM8/31/16
to ClickHouse
What is the correct way to insert values into Enum8 type field?

When I try to do that, I got:
DB::Exception: Type mismatch for column type. Column has type Enum8  ... got type String.

man...@gmail.com

unread,
Sep 1, 2016, 9:32:37 PM9/1/16
to ClickHouse
What interface are you using to insert data and what format?

Specifying values of Enums as strings is perfectly correct for all text formats.
Look at examples in https://github.com/yandex/ClickHouse/blob/master/dbms/tests/queries/0_stateless/00294_enums.sql
But for binary formats (RowBinary), you must specify underlying numeric values.


Yuriy Nazarenko

unread,
Sep 7, 2016, 3:28:41 AM9/7/16
to ClickHouse
Actually I encounter the same problem using:
insert into table select * from other_table
Using insert into table values... works fine.

man...@gmail.com

unread,
Sep 8, 2016, 8:53:26 AM9/8/16
to click...@googlegroups.com
When do INSERT SELECT to a table with different types of columns (even Enums and Strings considered different), you need to add explicit cast expressions.
To cast one type to another, there are two ways:

1. toT family of functions, like toString, toUInt32... for simple data types.
2. CAST expression, like CAST(x AS Enum8('hello' = 1, 'world' = 2)) for more complex data types, like Enums.
CAST expression is not yet documented, but it works as expected.

Example: INSERT INTO t1 SELECT CAST(x AS Enum8('hello' = 1, 'world' = 2)) AS x FROM t2
Reply all
Reply to author
Forward
0 new messages