Skip to content

[CALCITE-5898] Add UUID function (enabled in Presto library) - #5146

Draft
wasabii wants to merge 1 commit into
apache:mainfrom
wasabii:random_uuid
Draft

[CALCITE-5898] Add UUID function (enabled in Presto library)#5146
wasabii wants to merge 1 commit into
apache:mainfrom
wasabii:random_uuid

Conversation

@wasabii

@wasabii wasabii commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Jira Link

CALCITE-5898

Changes Proposed

Adds two niladic functions to the CALCITE library:

  • UUIDV4() returns a random (version 4) UUID, as defined by RFC 4122.
  • UUIDV7() returns a time-ordered (version 7) UUID, as defined by RFC 9562;
    values generated in sequence sort in creation order.

Both return UUID (the type added in CALCITE-6738), and are non-deterministic
and dynamic, so they are evaluated once per row.

UUIDV7() guarantees strict ordering even between values generated within the
same millisecond, using the Monotonic Random method of RFC 9562 section 6.2:
the 12-bit rand_a field is seeded randomly at the start of each millisecond
and then used as a counter. Ordering is maintained without locking, by packing
the timestamp and counter into a single AtomicLong updated with a
compare-and-set loop. Random bits come from SecureRandom, as recommended by
RFC 9562 section 6.9.

When generating SQL for other dialects:

  • PostgreSQL 17+ has native uuidv4() and uuidv7(); both pass through
    unchanged.
  • SQL Server: UUIDV4() becomes NEWID().
  • Oracle 23ai+: UUIDV4() becomes UUID().
  • UUIDV7() has no SQL Server or Oracle equivalent, and is left untranslated.
    SQL Server's uniqueidentifier does not sort by bit pattern, so a version 7
    value stored in that type is not returned in timestamp order.

Both functions are documented in site/_docs/reference.md. UuidFunctionTest
covers the version and variant bits and monotonicity, SqlOperatorTest covers
the return types, and RelToSqlConverterTest covers the generated SQL for each
dialect.

@sonarqubecloud

sonarqubecloud Bot commented Aug 3, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant