| 27 | } |
| 28 | |
| 29 | void registerDataTypeUUID(DataTypeFactory & factory) |
| 30 | { |
| 31 | factory.registerSimpleDataType("UUID", [] { return DataTypePtr(std::make_shared<DataTypeUUID>()); }, DataTypeFactory::Case::Sensitive, |
| 32 | Documentation{ |
| 33 | .description = R"DOCS_MD( |
| 34 | A Universally Unique Identifier (UUID) is a 16-byte value used to identify records. For detailed information about UUIDs, see [Wikipedia](https://en.wikipedia.org/wiki/Universally_unique_identifier). |
| 35 | |
| 36 | While different UUID variants exist, e.g. UUIDv4 and UUIDv7 (see [here](https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis)), ClickHouse does not validate that inserted UUIDs conform to a particular variant. |
| 37 | UUIDs are internally treated as a sequence of 16 random bytes with [8-4-4-4-12 representation](https://en.wikipedia.org/wiki/Universally_unique_identifier#Textual_representation) at SQL level. |
| 38 | |
| 39 | Example UUID value: |
| 40 | |
| 41 | ```text |
| 42 | 61f0c404-5cb3-11e7-907b-a6006ad3dba0 |
| 43 | ``` |
| 44 | |
| 45 | The default UUID is all-zero. It is used, for example, when a new record is inserted but no value for a UUID column is specified: |
| 46 | |
| 47 | ```text |
| 48 | 00000000-0000-0000-0000-000000000000 |
| 49 | ``` |
| 50 | |
| 51 | :::warning |
| 52 | Due to historical reasons, UUIDs are sorted by their second half. |
| 53 | |
| 54 | While this is fine for UUIDv4 values, this can deteriorate performance with UUIDv7 columns used in primary index definitions (usage in ordering keys or partition keys is fine). |
| 55 | More specifically, UUIDv7 values consist of a timestamp in the first half and a counter in the second half. |
| 56 | UUIDv7 sorting in sparse primary key indexes (i.e., the first values of each index granule) will therefore be by counter field. |
| 57 | Assuming UUIDs were sorted by the first half (timestamp), then the primary key index analysis step at the beginning of queries is expected to prune all marks in all but one part. |
| 58 | However, with sorting by the second half (counter), at least one mark is expected to be returned for all parts, leading to unnecessary unnecessary disk accesses. |
| 59 | ::: |
| 60 | |
| 61 | Example: |
| 62 | |
| 63 | ```sql title="Query" |
| 64 | CREATE TABLE tab (uuid UUID) ENGINE = MergeTree PRIMARY KEY (uuid); |
| 65 | |
| 66 | INSERT INTO tab SELECT generateUUIDv7() FROM numbers(2); |
| 67 | INSERT INTO tab SELECT generateUUIDv7() FROM numbers(2); |
| 68 | INSERT INTO tab SELECT generateUUIDv7() FROM numbers(2); |
| 69 | INSERT INTO tab SELECT generateUUIDv7() FROM numbers(2); |
| 70 | INSERT INTO tab SELECT generateUUIDv7() FROM numbers(2); |
| 71 | SELECT * FROM tab; |
| 72 | ``` |
| 73 | |
| 74 | ```text title="Response" |
| 75 | ┌─uuid─────────────────────────────────┐ |
| 76 | │ 019d2555-7874-7e9d-a284-9b45a0b2f165 │ |
| 77 | │ 019d2555-7874-7e9d-a284-9b46c3353be7 │ |
| 78 | │ 019d2555-7878-77fc-a36f-4081aa58ec2b │ |
| 79 | │ 019d2555-7878-77fc-a36f-40826555fb9b │ |
| 80 | │ 019d2555-7870-7432-ba62-5250ac595328 │ |
| 81 | │ 019d2555-7870-7432-ba62-5251da22bd19 │ |
| 82 | │ 019d2555-786c-73e9-a031-4a7936df7d56 │ |
| 83 | │ 019d2555-786c-73e9-a031-4a7a35a9544f │ |
| 84 | │ 019d2555-7868-7333-89d1-2bd1639899c3 │ |
| 85 | │ 019d2555-7868-7333-89d1-2bd297eb7d42 │ |
| 86 | └──────────────────────────────────────┘ |
no test coverage detected