Functions for Working with UUID

The functions for working with UUID are listed below.

generateUUIDv4

Generates the UUID of version 4.

  1. generateUUIDv4()

Returned value

The UUID type value.

Usage example

This example demonstrates creating a table with the UUID type column and inserting a value into the table.

  1. CREATE TABLE t_uuid (x UUID) ENGINE=TinyLog
  2. INSERT INTO t_uuid SELECT generateUUIDv4()
  3. SELECT * FROM t_uuid
  1. ┌────────────────────────────────────x─┐
  2. f4bf890f-f9dc-4332-ad5c-0c18e73f28e9
  3. └──────────────────────────────────────┘

empty

Checks whether the input UUID is empty.

Syntax

  1. empty(UUID)

The UUID is considered empty if it contains all zeros (zero UUID).

The function also works for arrays or strings.

Arguments

  • x — Input UUID. UUID.

Returned value

  • Returns 1 for an empty UUID or 0 for a non-empty UUID.

Type: UInt8.

Example

To generate the UUID value, ClickHouse provides the generateUUIDv4 function.

Query:

  1. SELECT empty(generateUUIDv4());

Result:

  1. ┌─empty(generateUUIDv4())─┐
  2. 0
  3. └─────────────────────────┘

notEmpty

Checks whether the input UUID is non-empty.

Syntax

  1. notEmpty(UUID)

The UUID is considered empty if it contains all zeros (zero UUID).

The function also works for arrays or strings.

Arguments

  • x — Input UUID. UUID.

Returned value

  • Returns 1 for a non-empty UUID or 0 for an empty UUID.

Type: UInt8.

Example

To generate the UUID value, ClickHouse provides the generateUUIDv4 function.

Query:

  1. SELECT notEmpty(generateUUIDv4());

Result:

  1. ┌─notEmpty(generateUUIDv4())─┐
  2. 1
  3. └────────────────────────────┘

toUUID (x)

Converts String type value to UUID type.

  1. toUUID(String)

Returned value

The UUID type value.

Usage example

  1. SELECT toUUID('61f0c404-5cb3-11e7-907b-a6006ad3dba0') AS uuid
  1. ┌─────────────────────────────────uuid─┐
  2. 61f0c404-5cb3-11e7-907b-a6006ad3dba0
  3. └──────────────────────────────────────┘

toUUIDOrNull (x)

It takes an argument of type String and tries to parse it into UUID. If failed, returns NULL.

  1. toUUIDOrNull(String)

Returned value

The Nullable(UUID) type value.

Usage example

  1. SELECT toUUIDOrNull('61f0c404-5cb3-11e7-907b-a6006ad3dba0T') AS uuid
  1. ┌─uuid─┐
  2. ᴺᵁᴸᴸ
  3. └──────┘

toUUIDOrZero (x)

It takes an argument of type String and tries to parse it into UUID. If failed, returns zero UUID.

  1. toUUIDOrZero(String)

Returned value

The UUID type value.

Usage example

  1. SELECT toUUIDOrZero('61f0c404-5cb3-11e7-907b-a6006ad3dba0T') AS uuid
  1. ┌─────────────────────────────────uuid─┐
  2. 00000000-0000-0000-0000-000000000000
  3. └──────────────────────────────────────┘

UUIDStringToNum

Accepts a string containing 36 characters in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, and returns it as a set of bytes in a FixedString(16).

  1. UUIDStringToNum(String)

Returned value

FixedString(16)

Usage examples

  1. SELECT
  2. '612f3c40-5d3b-217e-707b-6a546a3d7b29' AS uuid,
  3. UUIDStringToNum(uuid) AS bytes
  1. ┌─uuid─────────────────────────────────┬─bytes────────────┐
  2. 612f3c40-5d3b-217e-707b-6a546a3d7b29 a/<@];!~p{jTj={)
  3. └──────────────────────────────────────┴──────────────────┘

UUIDNumToString

Accepts a FixedString(16) value, and returns a string containing 36 characters in text format.

  1. UUIDNumToString(FixedString(16))

Returned value

String.

Usage example

  1. SELECT
  2. 'a/<@];!~p{jTj={)' AS bytes,
  3. UUIDNumToString(toFixedString(bytes, 16)) AS uuid
  1. ┌─bytes────────────┬─uuid─────────────────────────────────┐
  2. a/<@];!~p{jTj={) 612f3c40-5d3b-217e-707b-6a546a3d7b29
  3. └──────────────────┴──────────────────────────────────────┘

serverUUID()

Returns the random and unique UUID, which is generated when the server is first started and stored forever. The result writes to the file uuid created in the ClickHouse server directory /var/lib/clickhouse/.

Syntax

  1. serverUUID()

Returned value

  • The UUID of the server.

Type: UUID.

See Also