Functions for Working with UUIDs
generateUUIDv4
Syntax
Arguments
expr
— An arbitrary expression used to bypass common subexpression elimination if the function is called multiple times in a query. The value of the expression has no effect on the returned UUID. Optional.
Returned value
A value of type UUIDv4.
Example
First, create a table with a column of type UUID, then insert a generated UUIDv4 into the table.
Result:
Example with multiple UUIDs generated per row
generateUUIDv7
The generated UUID contains the current Unix timestamp in milliseconds (48 bits), followed by version "7" (4 bits), a counter (42 bit) to distinguish UUIDs within a millisecond (including a variant field "2", 2 bit), and a random field (32 bits). For any given timestamp (unix_ts_ms), the counter starts at a random value and is incremented by 1 for each new UUID until the timestamp changes. In case the counter overflows, the timestamp field is incremented by 1 and the counter is reset to a random new start value.
Function generateUUIDv7
guarantees that the counter field within a timestamp increments monotonically across all function invocations in concurrently running threads and queries.
As of April 2024, version 7 UUIDs are in draft status and their layout may change in future.
Syntax
Arguments
expr
— An arbitrary expression used to bypass common subexpression elimination if the function is called multiple times in a query. The value of the expression has no effect on the returned UUID. Optional.
Returned value
A value of type UUIDv7.
Example
First, create a table with a column of type UUID, then insert a generated UUIDv7 into the table.
Result:
Example with multiple UUIDs generated per row
empty
Checks whether the input UUID is empty.
Syntax
The UUID is considered empty if it contains all zeros (zero UUID).
The function also works for Arrays and Strings.
Arguments
x
— A UUID. UUID.
Returned value
- Returns
1
for an empty UUID or0
for a non-empty UUID. UInt8.
Example
To generate the UUID value, ClickHouse provides the generateUUIDv4 function.
Query:
Result:
notEmpty
Checks whether the input UUID is non-empty.
Syntax
The UUID is considered empty if it contains all zeros (zero UUID).
The function also works for Arrays or Strings.
Arguments
x
— A UUID. UUID.
Returned value
- Returns
1
for a non-empty UUID or0
for an empty UUID. UInt8.
Example
To generate the UUID value, ClickHouse provides the generateUUIDv4 function.
Query:
Result:
toUUID
Converts a value of type String to a UUID.
Returned value
The UUID type value.
Usage example
Result:
toUUIDOrDefault
Arguments
string
— String of 36 characters or FixedString(36). String.default
— UUID to be used as the default if the first argument cannot be converted to a UUID type. UUID.
Returned value
UUID
Returned value
The UUID type value.
Usage examples
This first example returns the first argument converted to a UUID type as it can be converted:
Result:
This second example returns the second argument (the provided default UUID) as the first argument cannot be converted to a UUID type:
Result:
toUUIDOrNull
Takes an argument of type String and tries to parse it into UUID. If failed, returns NULL.
Returned value
The Nullable(UUID) type value.
Usage example
Result:
toUUIDOrZero
It takes an argument of type String and tries to parse it into UUID. If failed, returns zero UUID.
Returned value
The UUID type value.
Usage example
Result:
UUIDStringToNum
Accepts string
containing 36 characters in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
, and returns a FixedString(16) as its binary representation, with its format optionally specified by variant
(Big-endian
by default).
Syntax
Arguments
string
— A String of 36 characters or FixedStringvariant
— Integer, representing a variant as specified by RFC4122. 1 =Big-endian
(default), 2 =Microsoft
.
Returned value
FixedString(16)
Usage examples
Result:
Result:
UUIDNumToString
Accepts binary
containing a binary representation of a UUID, with its format optionally specified by variant
(Big-endian
by default), and returns a string containing 36 characters in text format.
Syntax
Arguments
binary
— FixedString(16) as a binary representation of a UUID.variant
— Integer, representing a variant as specified by RFC4122. 1 =Big-endian
(default), 2 =Microsoft
.
Returned value
String.
Usage example
Result:
Result:
UUIDToNum
Accepts a UUID and returns its binary representation as a FixedString(16), with its format optionally specified by variant
(Big-endian
by default). This function replaces calls to two separate functions UUIDStringToNum(toString(uuid))
so no intermediate conversion from UUID to string is required to extract bytes from a UUID.
Syntax
Arguments
uuid
— UUID.variant
— Integer, representing a variant as specified by RFC4122. 1 =Big-endian
(default), 2 =Microsoft
.
Returned value
The binary representation of the UUID.
Usage examples
Result:
Result:
UUIDv7ToDateTime
Returns the timestamp component of a UUID version 7.
Syntax
Arguments
uuid
— UUID of version 7.timezone
— Timezone name for the returned value (optional). String.
Returned value
- Timestamp with milliseconds precision. If the UUID is not a valid version 7 UUID, it returns 1970-01-01 00:00:00.000. DateTime64(3).
Usage examples
Result:
Result:
serverUUID
Returns the random UUID generated during the first start of the ClickHouse server. The UUID is stored in file uuid
in the ClickHouse server directory (e.g. /var/lib/clickhouse/
) and retained between server restarts.
Syntax
Returned value
- The UUID of the server. UUID.
generateSnowflakeID
Generates a Snowflake ID.
The generated Snowflake ID contains the current Unix timestamp in milliseconds (41 + 1 top zero bits), followed by a machine id (10 bits), and a counter (12 bits) to distinguish IDs within a millisecond. For any given timestamp (unix_ts_ms), the counter starts at 0 and is incremented by 1 for each new Snowflake ID until the timestamp changes. In case the counter overflows, the timestamp field is incremented by 1 and the counter is reset to 0.
Function generateSnowflakeID
guarantees that the counter field within a timestamp increments monotonically across all function invocations in concurrently running threads and queries.
The generated Snowflake IDs are based on the UNIX epoch 1970-01-01. While no standard or recommendation exists for the epoch of Snowflake IDs, implementations in other systems may use a different epoch, e.g. Twitter/X (2010-11-04) or Mastodon (2015-01-01).
Syntax
Arguments
expr
— An arbitrary expression used to bypass common subexpression elimination if the function is called multiple times in a query. The value of the expression has no effect on the returned Snowflake ID. Optional.machine_id
— A machine ID, the lowest 10 bits are used. Int64. Optional.
Returned value
A value of type UInt64.
Example
First, create a table with a column of type UInt64, then insert a generated Snowflake ID into the table.
Result:
Example with multiple Snowflake IDs generated per row
Example with expression and a machine ID
snowflakeToDateTime
This function is deprecated and can only be used if setting allow_deprecated_snowflake_conversion_functions is enabled. The function will be removed at some point in future.
Extracts the timestamp component of a Snowflake ID in DateTime format.
Syntax
Arguments
value
— Snowflake ID. Int64.time_zone
— Timezone. The function parsestime_string
according to the timezone. Optional. String.
Returned value
- The timestamp component of
value
as a DateTime value.
Example
Query:
Result:
snowflakeToDateTime64
This function is deprecated and can only be used if setting allow_deprecated_snowflake_conversion_functions is enabled. The function will be removed at some point in future.
Extracts the timestamp component of a Snowflake ID in DateTime64 format.
Syntax
Arguments
value
— Snowflake ID. Int64.time_zone
— Timezone. The function parsestime_string
according to the timezone. Optional. String.
Returned value
- The timestamp component of
value
as a DateTime64 with scale = 3, i.e. millisecond precision.
Example
Query:
Result:
dateTimeToSnowflake
This function is deprecated and can only be used if setting allow_deprecated_snowflake_conversion_functions is enabled. The function will be removed at some point in future.
Converts a DateTime value to the first Snowflake ID at the giving time.
Syntax
Arguments
value
— Date with time. DateTime.
Returned value
- Input value converted to the Int64 data type as the first Snowflake ID at that time.
Example
Query:
Result:
dateTime64ToSnowflake
This function is deprecated and can only be used if setting allow_deprecated_snowflake_conversion_functions is enabled. The function will be removed at some point in future.
Convert a DateTime64 to the first Snowflake ID at the giving time.
Syntax
Arguments
value
— Date with time. DateTime64.
Returned value
- Input value converted to the Int64 data type as the first Snowflake ID at that time.
Example
Query:
Result:
snowflakeIDToDateTime
Returns the timestamp component of a Snowflake ID as a value of type DateTime.
Syntax
Arguments
value
— Snowflake ID. UInt64.epoch
- Epoch of the Snowflake ID in milliseconds since 1970-01-01. Defaults to 0 (1970-01-01). For the Twitter/X epoch (2015-01-01), provide 1288834974657. Optional. UInt*.time_zone
— Timezone. The function parsestime_string
according to the timezone. Optional. String.
Returned value
- The timestamp component of
value
as a DateTime value.
Example
Query:
Result:
snowflakeIDToDateTime64
Returns the timestamp component of a Snowflake ID as a value of type DateTime64.
Syntax
Arguments
value
— Snowflake ID. UInt64.epoch
- Epoch of the Snowflake ID in milliseconds since 1970-01-01. Defaults to 0 (1970-01-01). For the Twitter/X epoch (2015-01-01), provide 1288834974657. Optional. UInt*.time_zone
— Timezone. The function parsestime_string
according to the timezone. Optional. String.
Returned value
- The timestamp component of
value
as a DateTime64 with scale = 3, i.e. millisecond precision.
Example
Query:
Result:
dateTimeToSnowflakeID
Converts a DateTime value to the first Snowflake ID at the giving time.
Syntax
Arguments
value
— Date with time. DateTime.epoch
- Epoch of the Snowflake ID in milliseconds since 1970-01-01. Defaults to 0 (1970-01-01). For the Twitter/X epoch (2015-01-01), provide 1288834974657. Optional. UInt*.
Returned value
- Input value converted to UInt64 as the first Snowflake ID at that time.
Example
Query:
Result:
dateTime64ToSnowflakeID
Convert a DateTime64 to the first Snowflake ID at the giving time.
Syntax
Arguments
value
— Date with time. DateTime64.epoch
- Epoch of the Snowflake ID in milliseconds since 1970-01-01. Defaults to 0 (1970-01-01). For the Twitter/X epoch (2015-01-01), provide 1288834974657. Optional. UInt*.
Returned value
- Input value converted to UInt64 as the first Snowflake ID at that time.
Example
Query:
Result: