| 60 | } |
| 61 | |
| 62 | void registerDataTypeNested(DataTypeFactory & factory) |
| 63 | { |
| 64 | factory.registerDataTypeCustom("Nested", create, DataTypeFactory::Case::Sensitive, Documentation{ |
| 65 | .description = R"DOCS_MD( |
| 66 | ## Nested(name1 Type1, Name2 Type2, ...) {#nestedname1-type1-name2-type2-} |
| 67 | |
| 68 | A nested data structure is like a table inside a cell. The parameters of a nested data structure – the column names and types – are specified the same way as in a [CREATE TABLE](../../../sql-reference/statements/create/table.md) query. Each table row can correspond to any number of rows in a nested data structure. |
| 69 | |
| 70 | :::tip[Avoid using dots in column names] |
| 71 | Column names containing dots, columns sharing a common dot-prefix, and columns with the `Array` type can each be interpreted as part of a flattened Nested structure when `flatten_nested = 1` (the default). This can cause unexpected array-length validation on inserts and renaming restrictions. |
| 72 | |
| 73 | Avoid using dots in column names if possible. |
| 74 | Use underscores (`_`) or another separator instead of dots in column names unless you intentionally need `Nested` semantics. |
| 75 | ::: |
| 76 | |
| 77 | Example: |
| 78 | |
| 79 | ```sql |
| 80 | CREATE TABLE test.visits |
| 81 | ( |
| 82 | CounterID UInt32, |
| 83 | StartDate Date, |
| 84 | Sign Int8, |
| 85 | IsNew UInt8, |
| 86 | VisitID UInt64, |
| 87 | UserID UInt64, |
| 88 | ... |
| 89 | Goals Nested |
| 90 | ( |
| 91 | ID UInt32, |
| 92 | Serial UInt32, |
| 93 | EventTime DateTime, |
| 94 | Price Int64, |
| 95 | OrderID String, |
| 96 | CurrencyID UInt32 |
| 97 | ), |
| 98 | ... |
| 99 | ) ENGINE = CollapsingMergeTree(StartDate, intHash32(UserID), (CounterID, StartDate, intHash32(UserID), VisitID), 8192, Sign) |
| 100 | ``` |
| 101 | |
| 102 | This example declares the `Goals` nested data structure, which contains data about conversions (goals reached). Each row in the 'visits' table can correspond to zero or any number of conversions. |
| 103 | |
| 104 | When [flatten_nested](/operations/settings/settings#flatten_nested) is set to `0` (which is not by default), arbitrary levels of nesting are supported. |
| 105 | |
| 106 | In most cases, when working with a nested data structure, its columns are specified with column names separated by a dot. These columns make up an array of matching types. All the column arrays of a single nested data structure have the same length. |
| 107 | |
| 108 | Example: |
| 109 | |
| 110 | ```sql |
| 111 | SELECT |
| 112 | Goals.ID, |
| 113 | Goals.EventTime |
| 114 | FROM test.visits |
| 115 | WHERE CounterID = 101500 AND length(Goals.ID) < 5 |
| 116 | LIMIT 10 |
| 117 | ``` |
| 118 | |
| 119 | ```text |
no test coverage detected