Skip to content

feat(function): version-specific UUID generators, timestamp/interval forms, and extraction functions - #26945

Merged
XuPeng-SH merged 8 commits into
matrixorigin:mainfrom
fengttt:uuid-versions
Aug 11, 2026
Merged

XuPeng-SH merged 8 commits into
matrixorigin:mainfrom
fengttt:uuid-versions

Conversation

@fengttt

@fengttt fengttt commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #26946

What this PR does / why we need it:

Adds a family of version-aware UUID builtins for time-ordered unique IDs:

Generators

  • uuid_v7() — alias of uuid() (which already generates UUIDv7)
  • uuid_v1(), uuid_v4(), uuid_v6() — new version-specific generators

Boundary form — uuid(ts) / uuid_v7(ts) / uuid_v1(ts) / uuid_v6(ts) with a datetime argument return the deterministic minimal UUID of that version for that instant (timestamp + version/variant bits set, all random bits zero). Useful as range-predicate boundaries over v7/v6 keys, e.g. WHERE id >= uuid_v7('2026-01-01') AND id < uuid_v7('2026-02-01'). The datetime is interpreted in the session timezone; out-of-range instants error.

Shift form — uuid(interval 1 minute) etc., like PostgreSQL 18's uuidv7(shift interval): each evaluation generates a fresh random UUID whose embedded timestamp is the wall clock at evaluation time shifted by the interval (unlike uuid_v7(now() + interval ...), which fixes one now()). The binder lowers the INTERVAL to internal (count, unit) args the same way date_add does; calendar units (month/year) use Datetime.AddInterval so arithmetic matches date_add. Direct calls to the internal form (uuid_v7(5, 3)) are rejected at bind time. Invalid/overflowing intervals yield NULL, matching date_add.

Extraction — uuid_extract_version(uuid) → smallint and uuid_extract_timestamp(uuid) → timestamp(6), with PostgreSQL semantics: NULL for non-RFC-4122 variants, and timestamps only for v1/v6/v7 (NULL for v4). The timestamp renders in the session timezone like current_timestamp.

Implementation note on v6: google/uuid's NewV6 deviates from the RFC 9562 field layout (it writes the 60-bit timestamp as a plain 64-bit BE integer and lets the version nibble clobber bits 15-12). Since uuid_v6() generates via uuid.NewV6, the boundary/shift/extract code follows that same layout so values compare consistently against generated ids. Consequence: v6 time ordering and extraction are ~6.5ms-granular; the tests use tolerances accordingly.

Tested with unit tests (pinned boundary values cross-checked against google/uuid's Time() decoder) and a new BVT case (42 statements) covering generators, boundaries, shifts, extraction, error paths, and NULL handling.

🤖 Generated with Claude Code

…forms, and extraction functions

Add a family of UUID builtins:

- uuid_v7() as an alias of uuid() (which already generates UUIDv7), plus
  new uuid_v1(), uuid_v4(), uuid_v6() generators.
- uuid(ts)/uuid_v7(ts)/uuid_v1(ts)/uuid_v6(ts): deterministic minimal
  (boundary) UUID for a datetime instant, for range predicates over
  time-ordered keys, e.g. id < uuid_v7('2026-01-01').
- uuid(interval n unit) etc. (PostgreSQL uuidv7(shift) style): a fresh
  random UUID whose embedded timestamp is the evaluation-time wall clock
  shifted by the interval; the binder lowers INTERVAL to internal
  (count, unit) args like date_add, and direct calls to that internal
  form are rejected.
- uuid_extract_version(uuid) -> smallint and
  uuid_extract_timestamp(uuid) -> timestamp(6) (NULL for versions
  without a time source), rendering in the session timezone like
  current_timestamp.

The v6 boundary/shift/extract code follows google/uuid's NewV6 layout
(64-bit BE timestamp, version nibble clobbering bits 15-12, not the
RFC 9562 field order) so values compare consistently against generated
uuid_v6() ids; this makes v6 time ordering ~6.5ms-granular, which the
tests account for.

Co-Authored-By: Claude Fable 5 <[email protected]>
@fengttt
fengttt requested a review from a user August 10, 2026 23:59
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

The PR-coverage CI gate flagged the new list_builtIn.go overload
closures (executed only via registration, which the direct-executor
unit tests bypassed) and the base_binder.go uuid INTERVAL rewrite /
arity guard. Add a registration-iteration test that executes every
retType/newOp closure of the uuid-family function ids, and plan
builder SQL cases exercising the INTERVAL rewrite for all four names,
the boundary and extraction forms, and both rejection paths.

Co-Authored-By: Claude Fable 5 <[email protected]>

@aunjgr aunjgr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 074679392b760190a8be8791fe36914813bb5f98; all 26 checks are terminal and passing.

[P1] Emit and decode the RFC 9562 UUIDv6 layout instead of google/uuid's known non-standard layout (pkg/sql/plan/function/func_builtin.go:1736-1750,1788-1796,1826-1827,1914-1923,1949-1950).

RFC 9562 places the most significant 32 timestamp bits in octets 0-3, the next 16 in octets 4-5, and the least significant 12 after the version nibble. This head deliberately writes the raw 60-bit timestamp as a 64-bit big-endian value and overwrites bits 15-12. For the pinned 2026-01-02 instant, it emits 01f0e787-b2ea-64e0-...; the RFC layout is 1f0e787b-2ea3-64e0-.... Feeding that standard UUIDv6 value to this head's extractor is decoded through google/uuid's matching non-standard Time() layout and produces year 8674 instead of 2026.

That breaks interchange with every conforming UUIDv6 producer, makes the advertised boundary values incompatible with standard UUIDv6 columns, and discards four timestamp bits without replacing them with entropy, creating avoidable collision aliases. The existing tests are circular because boundary generation and extraction are cross-checked against the same non-standard library behavior. Please generate/reorder v6 locally according to RFC 9562, decode the RFC fields explicitly, and add the RFC v6 test vector plus an externally constructed standard-v6 extraction control.

[P2] Preserve strict-function NULL propagation for untyped NULL literals (test/distributed/cases/function/func_uuid_versions.test:42, result line 96).

The test says uuid_extract_version(null) is null, uuid_extract_timestamp(null) is null, but the checked-in expected result is actually invalid argument operator cast, bad value [ANY UUID]. Both functions register only a UUID overload, while the implicit-cast table has no ANY-to-UUID path, so the call fails during binding and never reaches the strict NULL behavior. This contradicts the PR's NULL-handling claim and PostgreSQL-compatible behavior; nearby is_uuid(null) already returns NULL. Please make scalar NULL bind to these UUID overloads and assert a two-column 1, 1 result instead of recording the error.

…ct functions

Address review on matrixorigin#26945:

- P1: generate, bound, shift, and decode UUIDv6 in the RFC 9562 field
  layout (time_high 32 | time_mid 16 | version | time_low 12) instead of
  google/uuid's non-standard NewV6 layout. Generation reorders a
  uuid.NewUUID() v1 (managed monotonic timestamp/clock-seq/node) into
  v6 fields, so no timestamp bits are lost; boundary/shift/extract use
  explicit RFC field encode/decode helpers. v6 extraction is now exact
  and interchange-safe; tests pin the RFC layout and add the standard
  UUIDv6 example from RFC 9562 appendix A.4 as an externally
  constructed extraction control.

- P2: allow scalar NULL to bind to the UUID-typed overloads by adding
  the ANY->UUID cast path (cast rule + scalarNullToOthers), so
  uuid_extract_version(null)/uuid_extract_timestamp(null) return NULL
  instead of failing to bind; BVT now asserts the NULL results.

Co-Authored-By: Claude Fable 5 <[email protected]>
@fengttt

fengttt commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

Addressed both review points in c022836:

P1 (RFC 9562 v6 layout): uuid_v6() now generates RFC-compliant v6 by reordering a uuid.NewUUID() v1 (which manages the monotonic timestamp, clock sequence, and node id) into the RFC field layout — no timestamp bits lost, no entropy discarded. Boundary, shift, and extraction all use explicit RFC field encode/decode helpers (rfcV6EncodeTime/rfcV6DecodeTime) rather than google/uuid's non-standard v6 codec. The pinned boundary for the 2026-01-02 instant is now 1f0e787b-2ea3-64e0-... as you noted, v6 extraction is exact (2026-01-02 03:04:05.678000), and the tests include the standard UUIDv6 example from RFC 9562 appendix A.4 (1ec9414c-232a-6b00-b3c8-9f6bdeced846 → epoch 1645557742) as an externally constructed extraction control, breaking the circularity you pointed out.

P2 (NULL binding): added the ANY→UUID cast path (cast rule + scalarNullToOthers), so scalar NULL binds to the UUID overloads and uuid_extract_version(null) / uuid_extract_timestamp(null) return NULL. The BVT now asserts the two-column 1, 1 (plus cast(null as uuid) is null) instead of the recorded bind error — good catch, the genrs output had captured the error without me noticing.

Verified end-to-end on a local single-node build; the BVT case passes 44/44.

@matrix-meow matrix-meow added the size/XL Denotes a PR that changes [1000, 1999] lines label Aug 11, 2026

@aunjgr aunjgr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 8a31ea2b3980ad58bda6ae48a73ce694343c0ad3; all CI checks are terminal and successful. The prior RFC UUIDv6, untyped NULL, DST, and nullable-metadata findings are fixed. One distinct interval-boundary blocker remains.

[P1] Reject math.MinInt64 before interval arithmetic (pkg/sql/plan/function/func_builtin.go:1873-1875).

JudgeIntervalNumOverflow checks the negative side with -num, which overflows for math.MinInt64, so this guard accepts the minimum signed value. The public SQL form reaches it: uuid_v7(interval -9223372036854775808 second) binds that exact int64. Datetime.AddInterval then multiplies it by microseconds-per-second; the multiplication wraps to zero, and the function returns a fresh non-NULL UUID with the current timestamp instead of the documented NULL for an overflowing interval. MICROSECOND reaches the same missed guard and currently returns an out-of-range query error rather than NULL.

I reproduced the SECOND case on the exact head through the function path: null=false and the extracted timestamp was the current instant. Please explicitly reject math.MinInt64 (or use an overflow-safe absolute-value/range check) before AddInterval, and add public SQL plus focused regressions for both SECOND and MICROSECOND minimum values.

The PR description also still describes the superseded non-RFC google/uuid v6 implementation and should be refreshed after the earlier fix.

fengttt and others added 2 commits August 11, 2026 01:07
# Conflicts:
#	pkg/sql/plan/function/function.go
Address review on matrixorigin#26945: JudgeIntervalNumOverflow negates num for its
negative-side check, which overflows for math.MinInt64 and lets the
value through to wrapping interval arithmetic — uuid_v7(interval
-9223372036854775808 second) returned a fresh non-NULL UUID with the
current timestamp instead of NULL. Reject math.MinInt64 explicitly
alongside the math.MaxInt64 invalid-interval marker. Adds focused unit
regressions (SECOND and MICROSECOND at MinInt64) and public-SQL BVT
coverage for v1/v6/v7.

Also merges upstream/main (the NULL-synthesizing DeduceNotNullable
contract from matrixorigin#26902 landed there; our UUID extractor ids are folded
into that case list).

Co-Authored-By: Claude Fable 5 <[email protected]>
@fengttt

fengttt commented Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the MinInt64 interval blocker in 3750aa6: math.MinInt64 is now rejected explicitly alongside the math.MaxInt64 invalid-interval marker (as you noted, JudgeIntervalNumOverflow's negation overflows there and let the value through to wrapping arithmetic). Verified on a live build that uuid_v7(interval -9223372036854775808 second) and the MICROSECOND form both return NULL now; added focused unit regressions for both units plus public-SQL BVT lines for v1/v6/v7.

Also merged upstream/main (folding the UUID extractor ids into the #26902 DeduceNotNullable contract that landed there, resolving the branch conflict) and refreshed the PR description to describe the RFC 9562 v6 implementation instead of the superseded google-layout one.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep-reviewed exact head 3750aa655b57a53ca2caa94776468501904d6868.

The prior DST blocker is resolved. The shift path now decodes the instant actually embedded in each generated v1/v6/v7 UUID, applies calendar arithmetic to its wall clock in the session location, converts the shifted wall clock back to an instant, and re-encodes only the timestamp bits. This preserves the UTC-offset transition and the UUID's non-time bits. Deterministic forward, backward, and negative America/New_York month-shift tests cover both DST directions and round-trip all three versions.

I also checked the RFC 9562 v6 layout, boundary ranges, extraction nullability, and the added MinInt64 overflow guard. The focused UUID generator/boundary/shift/extraction/registration suite passes on this exact head, and all live CI checks are terminal and green.

@aunjgr aunjgr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed exact head 3750aa6 with all checks terminal and green. The RFC 9562 v6 layout, untyped NULL binding, extractor nullability metadata, DST-aware interval shifting, and MinInt64 overflow findings are all closed. The current implementation has external RFC controls plus forward/backward DST and public SQL coverage. LGTM.

@mergify mergify Bot added the queued label Aug 11, 2026
@mergify

mergify Bot commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-11 11:13 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-08-11 11:38 UTC · at 799c55990b54a1388ad8eadb3618b88b00650004

This pull request spent 25 minutes 3 seconds in the queue, with no time running CI.

Waiting for
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
All conditions
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]

Reason

Pull request #26945 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Failing checks:

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

@XuPeng-SH
XuPeng-SH merged commit 337a371 into matrixorigin:main Aug 11, 2026
25 of 26 checks passed
@mergify mergify Bot added dequeued and removed queued labels Aug 11, 2026

This branch was previously deployed

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

Labels

kind/feature size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants