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

Function registerDataTypeUUID

src/DataTypes/DataTypeUUID.cpp:29–174  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27}
28
29void registerDataTypeUUID(DataTypeFactory & factory)
30{
31 factory.registerSimpleDataType("UUID", [] { return DataTypePtr(std::make_shared<DataTypeUUID>()); }, DataTypeFactory::Case::Sensitive,
32 Documentation{
33 .description = R"DOCS_MD(
34A 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
36While 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.
37UUIDs 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
39Example UUID value:
40
41```text
4261f0c404-5cb3-11e7-907b-a6006ad3dba0
43```
44
45The 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
4800000000-0000-0000-0000-000000000000
49```
50
51:::warning
52Due to historical reasons, UUIDs are sorted by their second half.
53
54While 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).
55More specifically, UUIDv7 values consist of a timestamp in the first half and a counter in the second half.
56UUIDv7 sorting in sparse primary key indexes (i.e., the first values of each index granule) will therefore be by counter field.
57Assuming 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.
58However, 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
61Example:
62
63```sql title="Query"
64CREATE TABLE tab (uuid UUID) ENGINE = MergeTree PRIMARY KEY (uuid);
65
66INSERT INTO tab SELECT generateUUIDv7() FROM numbers(2);
67INSERT INTO tab SELECT generateUUIDv7() FROM numbers(2);
68INSERT INTO tab SELECT generateUUIDv7() FROM numbers(2);
69INSERT INTO tab SELECT generateUUIDv7() FROM numbers(2);
70INSERT INTO tab SELECT generateUUIDv7() FROM numbers(2);
71SELECT * 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└──────────────────────────────────────┘

Callers 1

DataTypeFactoryMethod · 0.85

Calls 1

Tested by

no test coverage detected