| 116 | |
| 117 | |
| 118 | void registerDataTypeArray(DataTypeFactory & factory) |
| 119 | { |
| 120 | factory.registerDataType("Array", create, DataTypeFactory::Case::Sensitive, Documentation{ |
| 121 | .description = R"DOCS_MD( |
| 122 | An array of `T`-type items, with the starting array index as 1. `T` can be any data type, including an array. |
| 123 | |
| 124 | ## Creating an Array {#creating-an-array} |
| 125 | |
| 126 | You can use a function to create an array: |
| 127 | |
| 128 | ```sql |
| 129 | array(T) |
| 130 | ``` |
| 131 | |
| 132 | You can also use `[]`. |
| 133 | |
| 134 | ```sql |
| 135 | [] |
| 136 | ``` |
| 137 | |
| 138 | Example of creating an array: |
| 139 | |
| 140 | ```sql |
| 141 | SELECT array(1, 2) AS x, toTypeName(x) |
| 142 | ``` |
| 143 | |
| 144 | ```text |
| 145 | ┌─x─────┬─toTypeName(array(1, 2))─┐ |
| 146 | │ [1,2] │ Array(UInt8) │ |
| 147 | └───────┴─────────────────────────┘ |
| 148 | ``` |
| 149 | |
| 150 | ```sql |
| 151 | SELECT [1, 2] AS x, toTypeName(x) |
| 152 | ``` |
| 153 | |
| 154 | ```text |
| 155 | ┌─x─────┬─toTypeName([1, 2])─┐ |
| 156 | │ [1,2] │ Array(UInt8) │ |
| 157 | └───────┴────────────────────┘ |
| 158 | ``` |
| 159 | |
| 160 | ## Working with Data Types {#working-with-data-types} |
| 161 | |
| 162 | When creating an array on the fly, ClickHouse automatically defines the argument type as the narrowest data type that can store all the listed arguments. If there are any [Nullable](/sql-reference/data-types/nullable) or literal [NULL](/operations/settings/formats#input_format_null_as_default) values, the type of an array element also becomes [Nullable](../../sql-reference/data-types/nullable.md). |
| 163 | |
| 164 | If ClickHouse couldn't determine the data type, it generates an exception. For instance, this happens when trying to create an array with strings and numbers simultaneously (`SELECT array(1, 'a')`). |
| 165 | |
| 166 | Examples of automatic data type detection: |
| 167 | |
| 168 | ```sql |
| 169 | SELECT array(1, 2, NULL) AS x, toTypeName(x) |
| 170 | ``` |
| 171 | |
| 172 | ```text |
| 173 | ┌─x──────────┬─toTypeName(array(1, 2, NULL))─┐ |
| 174 | │ [1,2,NULL] │ Array(Nullable(UInt8)) │ |
| 175 | └────────────┴───────────────────────────────┘ |
no test coverage detected