DOCAPI-7436: enum (#6411)

* Link fix.

* DOCAPI-7436: Updated Enum docs.
This commit is contained in:
BayoNet 2019-08-09 16:22:07 +03:00 committed by alexey-milovidov
parent 86f321a7cd
commit 39eaba4aa4

View File

@ -1,26 +1,29 @@
# Enum
# Enum8, Enum16 Enumerated type storing pairs of the `'string' = integer` format.
Includes the `Enum8` and `Enum16` types. `Enum` saves the finite set of pairs of `'string' = integer`. In ClickHouse, all operations with the `Enum` data type are performed as if value contains integers, although the user is working with string constants. This is more effective in terms of performance than working with the `String` data type. ClickHouse supports:
- `Enum8` is described by pairs of `'String' = Int8`. - 8-bit `Enum`. It can contain up to 256 values with enumeration of `[-128, 127]`.
- `Enum16` is described by pairs of `'String' = Int16`. - 16-bit `Enum`. It can contain up to 65536 values with enumeration of `[-32768, 32767]`.
ClickHouse automatically chooses a type for `Enum` at data insertion. Also, you can use `Enum8` or `Enum16` types to be sure in size of storage.
## Usage examples ## Usage examples
Here we create a table with an `Enum8('hello' = 1, 'world' = 2)` type column: Here we create a table with an `Enum8('hello' = 1, 'world' = 2)` type column:
``` ```sql
CREATE TABLE t_enum CREATE TABLE t_enum
( (
x Enum8('hello' = 1, 'world' = 2) x Enum('hello' = 1, 'world' = 2)
) )
ENGINE = TinyLog ENGINE = TinyLog
``` ```
This column `x` can only store the values that are listed in the type definition: `'hello'` or `'world'`. If you try to save any other value, ClickHouse will generate an exception. This column `x` can only store the values that are listed in the type definition: `'hello'` or `'world'`. If you try to save any other value, ClickHouse will generate an exception. ClickHouse automatically chooses the 8-bit size for enumeration of this `Enum`.
``` ```sql
:) INSERT INTO t_enum VALUES ('hello'), ('world'), ('hello') :) INSERT INTO t_enum VALUES ('hello'), ('world'), ('hello')
INSERT INTO t_enum VALUES INSERT INTO t_enum VALUES
@ -35,12 +38,12 @@ INSERT INTO t_enum VALUES
Exception on client: Exception on client:
Code: 49. DB::Exception: Unknown element 'a' for type Enum8('hello' = 1, 'world' = 2) Code: 49. DB::Exception: Unknown element 'a' for type Enum('hello' = 1, 'world' = 2)
``` ```
When you query data from the table, ClickHouse outputs the string values from `Enum`. When you query data from the table, ClickHouse outputs the string values from `Enum`.
``` ```sql
SELECT * FROM t_enum SELECT * FROM t_enum
┌─x─────┐ ┌─x─────┐
@ -52,7 +55,7 @@ SELECT * FROM t_enum
If you need to see the numeric equivalents of the rows, you must cast the `Enum` value to integer type. If you need to see the numeric equivalents of the rows, you must cast the `Enum` value to integer type.
``` ```sql
SELECT CAST(x, 'Int8') FROM t_enum SELECT CAST(x, 'Int8') FROM t_enum
┌─CAST(x, 'Int8')─┐ ┌─CAST(x, 'Int8')─┐
@ -64,12 +67,12 @@ SELECT CAST(x, 'Int8') FROM t_enum
To create an Enum value in a query, you also need to use `CAST`. To create an Enum value in a query, you also need to use `CAST`.
``` ```sql
SELECT toTypeName(CAST('a', 'Enum8(\'a\' = 1, \'b\' = 2)')) SELECT toTypeName(CAST('a', 'Enum(\'a\' = 1, \'b\' = 2)'))
┌─toTypeName(CAST('a', 'Enum8(\'a\' = 1, \'b\' = 2)'))─┐ ┌─toTypeName(CAST('a', 'Enum(\'a\' = 1, \'b\' = 2)'))─┐
│ Enum8('a' = 1, 'b' = 2) │ Enum8('a' = 1, 'b' = 2) │
└───────────────────────────────────────────────────── └─────────────────────────────────────────────────────┘
``` ```
## General rules and usage ## General rules and usage