| 102 | } |
| 103 | |
| 104 | void registerDataTypeDynamic(DataTypeFactory & factory) |
| 105 | { |
| 106 | factory.registerDataType("Dynamic", create, DataTypeFactory::Case::Sensitive, Documentation{ |
| 107 | .description = R"DOCS_MD( |
| 108 | This type allows to store values of any type inside it without knowing all of them in advance. |
| 109 | |
| 110 | To declare a column of `Dynamic` type, use the following syntax: |
| 111 | |
| 112 | ```sql |
| 113 | <column_name> Dynamic(max_types=N) |
| 114 | ``` |
| 115 | |
| 116 | Where `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 | |
| 120 | Using `Dynamic` type in table column definition: |
| 121 | |
| 122 | ```sql |
| 123 | CREATE TABLE test (d Dynamic) ENGINE = Memory; |
| 124 | INSERT INTO test VALUES (NULL), (42), ('Hello, World!'), ([1, 2, 3]); |
| 125 | SELECT 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 | |
| 137 | Using CAST from ordinary column: |
| 138 | |
| 139 | ```sql |
| 140 | SELECT 'Hello, World!'::Dynamic AS d, dynamicType(d); |
| 141 | ``` |
| 142 | |
| 143 | ```text |
| 144 | ┌─d─────────────┬─dynamicType(d)─┐ |
| 145 | │ Hello, World! │ String │ |
| 146 | └───────────────┴────────────────┘ |
| 147 | ``` |
| 148 | |
| 149 | Using CAST from `Variant` column: |
| 150 | |
| 151 | ```sql |
| 152 | SET use_variant_as_common_type = 1; |
| 153 | SELECT 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 | └───────┴────────────────┘ |
no test coverage detected