-
-
Notifications
You must be signed in to change notification settings - Fork 2k
MDEV-33710 Implement UUID_TIMESTAMP() function #4496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
varundeepsaini
wants to merge
8
commits into
MariaDB:main
Choose a base branch
from
varundeepsaini:MDEV-33710-uuid-timestamp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9aa596f
Implement UUID_TIMESTAMP() function that extracts timestamp from UUID…
varundeepsaini 557584c
refactoring
varundeepsaini 676675f
refactoring
varundeepsaini 75c06e7
fixed test
varundeepsaini 8444547
refactored if to switch
varundeepsaini 7515cf8
changed return type to bool
varundeepsaini d5dd95d
refactoring
varundeepsaini 879c14c
updated return types
varundeepsaini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -224,3 +224,66 @@ void my_uuid_end() | |
| mysql_mutex_destroy(&LOCK_uuid_generator); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| Extract Unix timestamp from a UUIDv1 or UUIDv7 | ||
|
|
||
| @param[in] uuid UUID bytes (16 bytes, big-endian) | ||
| @param[out] seconds Unix timestamp seconds | ||
| @param[out] usec Microseconds part | ||
|
|
||
| @return | ||
| @retval 1 Success | ||
| @retval 0 UUID version doesn't contain timestamp or timestamp invalid | ||
|
|
||
| UUIDv1 format (RFC 4122, big-endian): | ||
varundeepsaini marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Bytes 0-3: time_low (32 bits, low part of timestamp) | ||
| Bytes 4-5: time_mid (16 bits, middle part of timestamp) | ||
| Bytes 6-7: version (4 bits) + time_hi (12 bits, high part of timestamp) | ||
| Timestamp is 100-nanosecond intervals since 1582-10-15 | ||
|
|
||
| UUIDv7 format (RFC 9562, big-endian): | ||
| Bytes 0-5: Unix timestamp in milliseconds (48 bits) | ||
| Bytes 6-7: version (4 bits) + sub-millisecond precision (12 bits) | ||
| */ | ||
| int my_uuid_extract_ts(const char *uuid, my_time_t *seconds, ulong *usec) | ||
| { | ||
| uint version= (uchar) uuid[6] >> 4; | ||
varundeepsaini marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ulonglong ts; | ||
|
|
||
| if (version == 7) | ||
| { | ||
| /* UUIDv7: bytes 0-5 are Unix timestamp in milliseconds (big-endian) */ | ||
| ts= mi_uint6korr(uuid); | ||
FooBarrior marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| *seconds= ts / 1000; | ||
| *usec= (ts % 1000) * 1000; | ||
| return 1; | ||
| } | ||
|
|
||
| if (version == 1) | ||
| { | ||
|
||
| /* | ||
| UUIDv1: reconstruct 60-bit timestamp from three fields: | ||
| - time_low (bytes 0-3): bits 0-31 of timestamp | ||
| - time_mid (bytes 4-5): bits 32-47 of timestamp | ||
| - time_hi (bytes 6-7): bits 48-59 of timestamp (masked, 4 bits are version) | ||
varundeepsaini marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Formula: (time_hi << 48) | (time_mid << 32) | time_low | ||
| */ | ||
| ts= ((ulonglong)(mi_uint2korr(uuid + 6) & 0x0FFF) << 48) | | ||
| ((ulonglong) mi_uint2korr(uuid + 4) << 32) | | ||
| (ulonglong) mi_uint4korr(uuid); | ||
|
|
||
| /* Timestamp before Unix epoch (1970-01-01) */ | ||
| if (ts < UUID_TIME_OFFSET) | ||
| return 0; | ||
|
|
||
| ts= (ts - UUID_TIME_OFFSET) / 10; /* Convert to microseconds */ | ||
| *seconds= ts / 1000000; | ||
| *usec= ts % 1000000; | ||
| return 1; | ||
| } | ||
|
|
||
| /* Other versions (e.g., v4) don't contain timestamps */ | ||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
plugin/type_uuid/mysql-test/type_uuid/func_uuid_timestamp.result
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| SET time_zone='+00:00'; | ||
| # | ||
| # UUIDv1 with known timestamps | ||
| # | ||
| SELECT UUID_TIMESTAMP('63b00000-bfde-11d3-80fe-9f59977b836e'); | ||
| UUID_TIMESTAMP('63b00000-bfde-11d3-80fe-9f59977b836e') | ||
| 2000-01-01 00:00:00.000000 | ||
| SELECT UUID_TIMESTAMP('16488880-2b13-11ef-808b-603c7ba5e656'); | ||
| UUID_TIMESTAMP('16488880-2b13-11ef-808b-603c7ba5e656') | ||
| 2024-06-15 12:30:45.000000 | ||
| SELECT UUID_TIMESTAMP('d02b2980-e6a4-11f0-8072-39f36896a88f'); | ||
| UUID_TIMESTAMP('d02b2980-e6a4-11f0-8072-39f36896a88f') | ||
| 2025-12-31 23:59:59.000000 | ||
| # | ||
| # UUIDv7 with known timestamps (ms precision) | ||
| # | ||
| SELECT UUID_TIMESTAMP('00dc6acf-ac00-737d-9f12-3229a718bf21'); | ||
| UUID_TIMESTAMP('00dc6acf-ac00-737d-9f12-3229a718bf21') | ||
| 2000-01-01 00:00:00.000000 | ||
| SELECT UUID_TIMESTAMP('01901be0-f183-7e6c-9601-62a5e4f6e7b8'); | ||
| UUID_TIMESTAMP('01901be0-f183-7e6c-9601-62a5e4f6e7b8') | ||
| 2024-06-15 12:30:45.123000 | ||
| SELECT UUID_TIMESTAMP('019b76da-a7ff-7ece-a98f-44624a8d3b0f'); | ||
| UUID_TIMESTAMP('019b76da-a7ff-7ece-a98f-44624a8d3b0f') | ||
| 2025-12-31 23:59:59.999000 | ||
| # | ||
| # UUIDv4 returns NULL (no timestamp) | ||
| # | ||
| SELECT UUID_TIMESTAMP(UUID_V4()) IS NULL AS v4_returns_null; | ||
| v4_returns_null | ||
| 1 | ||
| SELECT UUID_TIMESTAMP('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11') IS NULL AS v4_string_returns_null; | ||
| v4_string_returns_null | ||
| 1 | ||
| # | ||
| # NULL and invalid input | ||
| # | ||
| SELECT UUID_TIMESTAMP(NULL) IS NULL AS null_input; | ||
| null_input | ||
| 1 | ||
| SELECT UUID_TIMESTAMP('not-a-valid-uuid'); | ||
| UUID_TIMESTAMP('not-a-valid-uuid') | ||
| NULL | ||
| Warnings: | ||
| Warning 1292 Incorrect uuid value: 'not-a-valid-uuid' | ||
| # | ||
| # Native UUID type | ||
| # | ||
| SELECT UUID_TIMESTAMP(CAST('01901be0-f183-7e6c-9601-62a5e4f6e7b8' AS UUID)); | ||
| UUID_TIMESTAMP(CAST('01901be0-f183-7e6c-9601-62a5e4f6e7b8' AS UUID)) | ||
| 2024-06-15 12:30:45.123000 | ||
| # | ||
| # Return type is TIMESTAMP(6) | ||
| # | ||
| CREATE TABLE t1 AS SELECT UUID_TIMESTAMP('01901be0-f183-7e6c-9601-62a5e4f6e7b8') AS ts; | ||
| SHOW CREATE TABLE t1; | ||
| Table Create Table | ||
| t1 CREATE TABLE `t1` ( | ||
| `ts` timestamp(6) NULL DEFAULT NULL | ||
| ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci | ||
| DROP TABLE t1; | ||
| # | ||
| # Edge cases | ||
| # | ||
| SELECT UUID_TIMESTAMP('00000000-0000-1000-8000-000000000000') IS NULL AS before_unix_epoch; | ||
| before_unix_epoch | ||
| 1 | ||
| SELECT UUID_TIMESTAMP('00000000-0000-7000-8000-000000000000'); | ||
| UUID_TIMESTAMP('00000000-0000-7000-8000-000000000000') | ||
| 0000-00-00 00:00:00.000000 | ||
| SELECT UUID_TIMESTAMP('ffffffff-ffff-7fff-8000-000000000000'); | ||
| UUID_TIMESTAMP('ffffffff-ffff-7fff-8000-000000000000') | ||
| 2042-12-13 16:54:30.655000 | ||
| SELECT UUID_TIMESTAMP('ffffffff-ffff-1fff-8000-000000000000'); | ||
| UUID_TIMESTAMP('ffffffff-ffff-1fff-8000-000000000000') | ||
| 2105-11-25 16:30:52.684697 | ||
| SET time_zone=DEFAULT; |
61 changes: 61 additions & 0 deletions
61
plugin/type_uuid/mysql-test/type_uuid/func_uuid_timestamp.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # MDEV-33710: UUID_TIMESTAMP() extracts timestamp from UUIDv1 and UUIDv7 | ||
FooBarrior marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| SET time_zone='+00:00'; | ||
|
|
||
| --echo # | ||
| --echo # UUIDv1 with known timestamps | ||
| --echo # | ||
| # 2000-01-01 00:00:00.000000 | ||
varundeepsaini marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| SELECT UUID_TIMESTAMP('63b00000-bfde-11d3-80fe-9f59977b836e'); | ||
| # 2024-06-15 12:30:45.000000 | ||
| SELECT UUID_TIMESTAMP('16488880-2b13-11ef-808b-603c7ba5e656'); | ||
| # 2025-12-31 23:59:59.000000 | ||
| SELECT UUID_TIMESTAMP('d02b2980-e6a4-11f0-8072-39f36896a88f'); | ||
|
|
||
| --echo # | ||
| --echo # UUIDv7 with known timestamps (ms precision) | ||
| --echo # | ||
| # 2000-01-01 00:00:00.000000 | ||
| SELECT UUID_TIMESTAMP('00dc6acf-ac00-737d-9f12-3229a718bf21'); | ||
| # 2024-06-15 12:30:45.123000 | ||
| SELECT UUID_TIMESTAMP('01901be0-f183-7e6c-9601-62a5e4f6e7b8'); | ||
| # 2025-12-31 23:59:59.999000 | ||
| SELECT UUID_TIMESTAMP('019b76da-a7ff-7ece-a98f-44624a8d3b0f'); | ||
|
|
||
| --echo # | ||
| --echo # UUIDv4 returns NULL (no timestamp) | ||
| --echo # | ||
| SELECT UUID_TIMESTAMP(UUID_V4()) IS NULL AS v4_returns_null; | ||
| SELECT UUID_TIMESTAMP('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11') IS NULL AS v4_string_returns_null; | ||
|
|
||
| --echo # | ||
| --echo # NULL and invalid input | ||
| --echo # | ||
| SELECT UUID_TIMESTAMP(NULL) IS NULL AS null_input; | ||
| SELECT UUID_TIMESTAMP('not-a-valid-uuid'); | ||
|
|
||
| --echo # | ||
| --echo # Native UUID type | ||
| --echo # | ||
| SELECT UUID_TIMESTAMP(CAST('01901be0-f183-7e6c-9601-62a5e4f6e7b8' AS UUID)); | ||
|
|
||
| --echo # | ||
| --echo # Return type is TIMESTAMP(6) | ||
| --echo # | ||
| CREATE TABLE t1 AS SELECT UUID_TIMESTAMP('01901be0-f183-7e6c-9601-62a5e4f6e7b8') AS ts; | ||
| SHOW CREATE TABLE t1; | ||
| DROP TABLE t1; | ||
|
|
||
| --echo # | ||
| --echo # Edge cases | ||
| --echo # | ||
| # UUIDv1 before Unix epoch | ||
| SELECT UUID_TIMESTAMP('00000000-0000-1000-8000-000000000000') IS NULL AS before_unix_epoch; | ||
varundeepsaini marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # UUIDv7 at Unix epoch (ts=0) | ||
| SELECT UUID_TIMESTAMP('00000000-0000-7000-8000-000000000000'); | ||
| # UUIDv7 max 48-bit timestamp | ||
| SELECT UUID_TIMESTAMP('ffffffff-ffff-7fff-8000-000000000000'); | ||
| # UUIDv1 max timestamp | ||
| SELECT UUID_TIMESTAMP('ffffffff-ffff-1fff-8000-000000000000'); | ||
|
|
||
| SET time_zone=DEFAULT; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.