| 78 | |
| 79 | |
| 80 | void registerDataTypeFixedString(DataTypeFactory & factory) |
| 81 | { |
| 82 | factory.registerDataType("FixedString", create, DataTypeFactory::Case::Sensitive, |
| 83 | Documentation{ |
| 84 | .description = R"DOCS_MD( |
| 85 | A fixed-length string of `N` bytes (neither characters nor code points). |
| 86 | |
| 87 | To declare a column of `FixedString` type, use the following syntax: |
| 88 | |
| 89 | ```sql |
| 90 | <column_name> FixedString(N) |
| 91 | ``` |
| 92 | |
| 93 | Where `N` is a natural number. |
| 94 | |
| 95 | The `FixedString` type is efficient when data has the length of precisely `N` bytes. In all other cases, it is likely to reduce efficiency. |
| 96 | |
| 97 | Examples 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 | |
| 104 | To store UUID values, use the [UUID](../../sql-reference/data-types/uuid.md) data type. |
| 105 | |
| 106 | When 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 | |
| 111 | Let's consider the following table with the single `FixedString(2)` column: |
| 112 | |
| 113 | ```sql |
| 114 | |
| 115 | |
| 116 | INSERT INTO FixedStringTable VALUES ('a'), ('ab'), (''); |
| 117 | ``` |
| 118 | |
| 119 | ```sql |
| 120 | SELECT |
| 121 | name, |
| 122 | toTypeName(name), |
| 123 | length(name), |
| 124 | empty(name) |
| 125 | FROM 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 | |
| 136 | Note 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 |
no test coverage detected