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

Function registerDataTypeArray

src/DataTypes/DataTypeArray.cpp:118–232  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

116
117
118void registerDataTypeArray(DataTypeFactory & factory)
119{
120 factory.registerDataType("Array", create, DataTypeFactory::Case::Sensitive, Documentation{
121 .description = R"DOCS_MD(
122An 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
126You can use a function to create an array:
127
128```sql
129array(T)
130```
131
132You can also use `[]`.
133
134```sql
135[]
136```
137
138Example of creating an array:
139
140```sql
141SELECT 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
151SELECT [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
162When 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
164If 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
166Examples of automatic data type detection:
167
168```sql
169SELECT 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└────────────┴───────────────────────────────┘

Callers 1

DataTypeFactoryMethod · 0.85

Calls 1

registerDataTypeMethod · 0.80

Tested by

no test coverage detected