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

Function registerDataTypeFixedString

src/DataTypes/DataTypeFixedString.cpp:80–192  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

78
79
80void registerDataTypeFixedString(DataTypeFactory & factory)
81{
82 factory.registerDataType("FixedString", create, DataTypeFactory::Case::Sensitive,
83 Documentation{
84 .description = R"DOCS_MD(
85A fixed-length string of `N` bytes (neither characters nor code points).
86
87To declare a column of `FixedString` type, use the following syntax:
88
89```sql
90<column_name> FixedString(N)
91```
92
93Where `N` is a natural number.
94
95The `FixedString` type is efficient when data has the length of precisely `N` bytes. In all other cases, it is likely to reduce efficiency.
96
97Examples of the values that can be efficiently stored in `FixedString`-typed columns:
98
99- The binary representation of IP addresses (`FixedString(16)` for IPv6).
100- Language codes (ru_RU, en_US, ...).
101- Currency codes (USD, RUB, ...).
102- Binary representation of hashes (`FixedString(16)` for MD5, `FixedString(32)` for SHA256).
103
104To store UUID values, use the [UUID](../../sql-reference/data-types/uuid.md) data type.
105
106When inserting the data, ClickHouse:
107
108- Complements a string with null bytes if the string contains fewer than `N` bytes.
109- Throws the `Too large value for FixedString(N)` exception if the string contains more than `N` bytes.
110
111Let's consider the following table with the single `FixedString(2)` column:
112
113```sql
114
115
116INSERT INTO FixedStringTable VALUES ('a'), ('ab'), ('');
117```
118
119```sql
120SELECT
121 name,
122 toTypeName(name),
123 length(name),
124 empty(name)
125FROM FixedStringTable;
126```
127
128```text
129┌─name─┬─toTypeName(name)─┬─length(name)─┬─empty(name)─┐
130│ a │ FixedString(2) │ 2 │ 0 │
131│ ab │ FixedString(2) │ 2 │ 0 │
132│ │ FixedString(2) │ 2 │ 1 │
133└──────┴──────────────────┴──────────────┴─────────────┘
134```
135
136Note that the length of the `FixedString(N)` value is constant. The [length](/sql-reference/functions/array-functions#length) function returns `N` even if the `FixedString(N)` value is filled only with null bytes, but the [empty](/sql-reference/functions/array-functions#empty) function returns `1` in this case.
137

Callers 1

DataTypeFactoryMethod · 0.85

Calls 2

registerDataTypeMethod · 0.80
registerAliasMethod · 0.80

Tested by

no test coverage detected