MCPcopy Create free account
hub / github.com/ClickHouse/ClickHouse / registerDataTypeEnum

Function registerDataTypeEnum

src/DataTypes/DataTypeEnum.cpp:459–650  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

457}
458
459void registerDataTypeEnum(DataTypeFactory & factory)
460{
461 factory.registerDataType("Enum8", [](const ASTPtr & arguments)
462 {
463 return createExact<DataTypeEnum8>(arguments, false);
464 }, DataTypeFactory::Case::Sensitive,
465 Documentation{
466 .description = R"DOCS_MD(
467An enumeration type that stores values as 8-bit signed integers (`Int8`), allowing up to 256 named values in the range `[-128, 127]`. Each `'string' = integer` pair maps a human-readable name to its stored numeric value. Use it instead of `Enum16` when the set of values is small to save space.
468)DOCS_MD",
469 .syntax = "Enum8('name1' = num1, 'name2' = num2, ...)",
470 .related = {"Enum"},
471 });
472 factory.registerDataType("Enum16", [](const ASTPtr & arguments)
473 {
474 return createExact<DataTypeEnum16>(arguments, false);
475 }, DataTypeFactory::Case::Sensitive,
476 Documentation{
477 .description = R"DOCS_MD(
478An enumeration type that stores values as 16-bit signed integers (`Int16`), allowing up to 65536 named values in the range `[-32768, 32767]`. Each `'string' = integer` pair maps a human-readable name to its stored numeric value. Use it when the set of named values is too large to fit in `Enum8`.
479)DOCS_MD",
480 .syntax = "Enum16('name1' = num1, 'name2' = num2, ...)",
481 .related = {"Enum"},
482 });
483 factory.registerDataType("Enum", [](const ASTPtr & arguments)
484 {
485 return create(arguments, false);
486 }, DataTypeFactory::Case::Sensitive,
487 Documentation{
488 .description = R"DOCS_MD(
489Enumerated type consisting of named values.
490
491Named values can be declared as `'string' = integer` pairs or `'string'` names . ClickHouse stores only numbers, but supports operations with the values through their names.
492
493ClickHouse supports:
494
495- 8-bit `Enum`. It can contain up to 256 values enumerated in the `[-128, 127]` range.
496- 16-bit `Enum`. It can contain up to 65536 values enumerated in the `[-32768, 32767]` range.
497
498ClickHouse automatically chooses the type of `Enum` when data is inserted. You can also use `Enum8` or `Enum16` types to be sure in the size of storage.
499
500## Usage Examples {#usage-examples}
501
502Here we create a table with an `Enum8('hello' = 1, 'world' = 2)` type column:
503
504```sql
505CREATE TABLE t_enum
506(
507 x Enum('hello' = 1, 'world' = 2)
508)
509ENGINE = TinyLog
510```
511
512Similarly, you could omit numbers. ClickHouse will assign consecutive numbers automatically. Numbers are assigned starting from 1 by default.
513
514```sql
515CREATE TABLE t_enum
516(

Callers 1

DataTypeFactoryMethod · 0.85

Calls 3

registerDataTypeMethod · 0.80
registerAliasMethod · 0.80
createFunction · 0.70

Tested by

no test coverage detected