Skip to content

Conversation

@varundeepsaini
Copy link

  • The Jira issue number for this PR is: MDEV-33710

Description

Adds UUID_TIMESTAMP(uuid) function to extract timestamps from UUIDv1 and UUIDv7 values. Returns NULL for other versions (e.g., v4) or invalid input.

Release Notes

New function: UUID_TIMESTAMP(uuid) - Returns the generation timestamp from UUIDv1/v7 as TIMESTAMP(6).

How can this PR be tested?

./mysql-test/mtr --suite=type_uuid func_uuid_timestamp

Basing the PR against the correct MariaDB version

  • This is a new feature and the PR is based against the main branch.
  • This is a bug fix.

PR quality check

  • I checked the CODING_STANDARDS.md file and my PR conforms to this where appropriate.
  • For any trivial modifications to the PR, I am ok with the reviewer making the changes themselves.

@varundeepsaini varundeepsaini force-pushed the MDEV-33710-uuid-timestamp branch from 5bbed91 to 9aa596f Compare December 23, 2025 19:33
@FooBarrior
Copy link
Contributor

I have some concern about the tests... it's not easy to validate the UUID values used there.
One approach I can think of is:

  1. set timestamp to a fixed value. This will affect NOW() output
  2. See that value returned by UUID_TIMESTAMP(UUID*()) matches our value

We can test a couple of random values, plus corner cases: min and max value for timestamp variable, and for UUID

@FooBarrior
Copy link
Contributor

Strangely, there is a different result outpoutted in amd64-debian-11-debug-ps-embedded test run:

--- /home/buildbot/amd64-debian-11-debug-ps-embedded/build/plugin/type_uuid/mysql-test/type_uuid/func_uuid_timestamp.result	2025-12-23 19:37:10.000000000 +0000
+++ /home/buildbot/amd64-debian-11-debug-ps-embedded/build/plugin/type_uuid/mysql-test/type_uuid/func_uuid_timestamp.reject	2025-12-23 19:42:40.539670984 +0000
@@ -1,9 +1,9 @@
 SELECT UUID_TIMESTAMP('018d17f3-e9a3-7000-8000-000000000000');
 UUID_TIMESTAMP('018d17f3-e9a3-7000-8000-000000000000')
-2024-01-17 20:34:37.539000
+2024-01-17 15:04:37.539000
 SELECT UUID_TIMESTAMP('0188b5b8-8000-7000-8000-000000000000');
 UUID_TIMESTAMP('0188b5b8-8000-7000-8000-000000000000')
-2023-06-13 22:35:47.520000
+2023-06-13 17:05:47.520000

Please take a look

@varundeepsaini
Copy link
Author

i think the test failing is a timezone issue (exact 5.30 HRS), ill fix that

Copy link
Contributor

@FooBarrior FooBarrior left a comment

Choose a reason for hiding this comment

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

Thank you @varundeepsaini for the update, and besides such aquick one!
The PR moves in the right direction. Still some work to be done.


my_time_t seconds;
ulong usec;
const uchar *buf= (const uchar *) uuid.to_lex_cstring().str;
Copy link
Contributor

Choose a reason for hiding this comment

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

No functions really need uchar (except the newly added one, which we can change), also uuid is natively stored in char, so why make a conversion?

Copy link
Author

@varundeepsaini varundeepsaini Dec 25, 2025

Choose a reason for hiding this comment

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

the mi_ prefix functions use uchar (they do cast it themselves tho, i can remove it from here but again, the cast is still there)

Copy link
Contributor

Choose a reason for hiding this comment

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

what do you mean? where??

Copy link
Author

Choose a reason for hiding this comment

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

SELECT UUID_TIMESTAMP('1ee81f00-a8a8-11ee-8000-000000000000') IS NOT NULL AS has_timestamp;

# Test dynamic UUID generation - timestamp should match NOW()
SELECT TIMESTAMPDIFF(SECOND, UUID_TIMESTAMP(UUID()), NOW()) AS v1_diff_seconds;
Copy link
Contributor

Choose a reason for hiding this comment

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

This test is (sort of, implicitly) indeterministic: each time it will check against the new NOW value. So if it will ever fail (because of the bug), noone will be able to reproduce it.

Fix NOW() with set timestamp statement.

Copy link
Author

Choose a reason for hiding this comment

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

UUID generation uses my_interval_timer() which gets the real system time, not the session's SET timestamp

i looked at other tests as well and tey also use this pattern itself (with NOW() / CURDATE() )

Copy link
Contributor

Choose a reason for hiding this comment

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

Then using it vs NOW etc would mean test would fail sporadically. Can you show the example?

Copy link
Contributor

Choose a reason for hiding this comment

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

Besides we had no timestamp extraction before, so what do you mean

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

We can go the other way:
Generate some UUID, somewhere, for example https://www.uuidgenerator.net/, or right in mariadb
Decode: https://www.uuidtools.com/decode
For example, 2731d872-e26e-11f0-8de9-0242ac120002 has 2025-12-26 15:18:38.034341.0 UTC
add test that outputs both correct timestamp (from decoder) and the result from your function

Alternatively, we could add a mockup for using timestamp in my_uuid, but maybe even better if we will do it in the less invasive way.

mysys/my_uuid.c Outdated
Bytes 6-7: version (4 bits) + sub-millisecond precision (12 bits)
*/

my_bool my_uuid_extract_ts(const uchar *uuid, my_time_t *seconds, ulong *usec)
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's use just int.
Or alternatively, bool, with true meaning success

@FooBarrior
Copy link
Contributor

Never mind the failing appveyor, it is used to be failing. However it may be useful to check the results, as it can reveal some windows build quirks (for example msvc doesn't like bool vs int mismatch, and has a 32-bit long )

@FooBarrior FooBarrior requested review from FooBarrior and removed request for FooBarrior December 26, 2025 14:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants