Repository navigation
feat(function): version-specific UUID generators, timestamp/interval forms, and extraction functions - #26945
Conversation
…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]>
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
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
left a comment
There was a problem hiding this comment.
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]>
|
Addressed both review points in c022836: P1 (RFC 9562 v6 layout): P2 (NULL binding): added the ANY→UUID cast path (cast rule + Verified end-to-end on a local single-node build; the BVT case passes 44/44. |
aunjgr
left a comment
There was a problem hiding this comment.
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.
# 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]>
|
Fixed the MinInt64 interval blocker in 3750aa6: Also merged upstream/main (folding the UUID extractor ids into the #26902 |
XuPeng-SH
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
Merge Queue Status
This pull request spent 25 minutes 3 seconds in the queue, with no time running CI. Waiting for
All conditions
ReasonPull 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.
Failing checks:
HintYou 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. Tick the box to put this pull request back in the merge queue (same as
|
What type of PR is this?
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 ofuuid()(which already generates UUIDv7)uuid_v1(),uuid_v4(),uuid_v6()— new version-specific generatorsBoundary 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'suuidv7(shift interval): each evaluation generates a fresh random UUID whose embedded timestamp is the wall clock at evaluation time shifted by the interval (unlikeuuid_v7(now() + interval ...), which fixes onenow()). The binder lowers the INTERVAL to internal(count, unit)args the same waydate_adddoes; calendar units (month/year) useDatetime.AddIntervalso arithmetic matchesdate_add. Direct calls to the internal form (uuid_v7(5, 3)) are rejected at bind time. Invalid/overflowing intervals yield NULL, matchingdate_add.Extraction —
uuid_extract_version(uuid) → smallintanduuid_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 likecurrent_timestamp.Implementation note on v6: google/uuid's
NewV6deviates 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). Sinceuuid_v6()generates viauuid.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