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

Function registerDataTypeDynamic

src/DataTypes/DataTypeDynamic.cpp:104–844  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

102}
103
104void registerDataTypeDynamic(DataTypeFactory & factory)
105{
106 factory.registerDataType("Dynamic", create, DataTypeFactory::Case::Sensitive, Documentation{
107 .description = R"DOCS_MD(
108This type allows to store values of any type inside it without knowing all of them in advance.
109
110To declare a column of `Dynamic` type, use the following syntax:
111
112```sql
113<column_name> Dynamic(max_types=N)
114```
115
116Where `N` is an optional parameter between `0` and `254` indicating how many different data types can be stored as separate subcolumns inside a column with type `Dynamic` across single block of data that is stored separately (for example across single data part for MergeTree table). If this limit is exceeded, all values with new types will be stored together in a special shared data structure in binary form. Default value of `max_types` is `32`.
117
118## Creating Dynamic {#creating-dynamic}
119
120Using `Dynamic` type in table column definition:
121
122```sql
123CREATE TABLE test (d Dynamic) ENGINE = Memory;
124INSERT INTO test VALUES (NULL), (42), ('Hello, World!'), ([1, 2, 3]);
125SELECT d, dynamicType(d) FROM test;
126```
127
128```text
129┌─d─────────────┬─dynamicType(d)─┐
130│ ᴺᵁᴸᴸ │ None │
131│ 42 │ Int64 │
132│ Hello, World! │ String │
133│ [1,2,3] │ Array(Int64) │
134└───────────────┴────────────────┘
135```
136
137Using CAST from ordinary column:
138
139```sql
140SELECT 'Hello, World!'::Dynamic AS d, dynamicType(d);
141```
142
143```text
144┌─d─────────────┬─dynamicType(d)─┐
145│ Hello, World! │ String │
146└───────────────┴────────────────┘
147```
148
149Using CAST from `Variant` column:
150
151```sql
152SET use_variant_as_common_type = 1;
153SELECT multiIf((number % 3) = 0, number, (number % 3) = 1, range(number + 1), NULL)::Dynamic AS d, dynamicType(d) FROM numbers(3)
154```
155
156```text
157┌─d─────┬─dynamicType(d)─┐
158│ 0 │ UInt64 │
159│ [0,1] │ Array(UInt64) │
160│ ᴺᵁᴸᴸ │ None │
161└───────┴────────────────┘

Callers 1

DataTypeFactoryMethod · 0.85

Calls 1

registerDataTypeMethod · 0.80

Tested by

no test coverage detected