post-merge changes

* remove duplicate test file
* move all uuidv7 tests into plugin/type_uuid/mysql-test/type_uuid/
* remove mysys/ changes
* auto my_random_bytes() fallback - removes duplicate code from uuid,
  and fixes all other users of my_random_bytes() that don't check
  the return value (because, perhaps, they don't need crypto-strong
  random bytes)
* End of 11.6 -> 11.7 in tests
* clarify the warning text
* UUID_VERSION_MASK()/UUID_VARIANT_MASK() must not depend on the version
* allow 4x more monotonic uuidv7 per millisecond - instead of stretching
  1000 microseconds over 12 bits, let's use extra 2 bits as a counter
* rename for compatibility with Percona Server (uuid_v4, uuid_v7)
This commit is contained in:
Sergei Golubchik 2024-09-12 11:54:24 +02:00
parent 2fe269fdcb
commit 5e5c3c7cb6
36 changed files with 195 additions and 234 deletions

View file

@ -469,8 +469,7 @@ PSI_mutex_key key_BITMAP_mutex, key_IO_CACHE_append_buffer_lock,
key_THR_LOCK_lock, key_THR_LOCK_malloc, key_THR_LOCK_lock, key_THR_LOCK_malloc,
key_THR_LOCK_mutex, key_THR_LOCK_myisam, key_THR_LOCK_net, key_THR_LOCK_mutex, key_THR_LOCK_myisam, key_THR_LOCK_net,
key_THR_LOCK_open, key_THR_LOCK_threads, key_THR_LOCK_open, key_THR_LOCK_threads,
key_TMPDIR_mutex, key_THR_LOCK_myisam_mmap, key_LOCK_uuid_generator, key_TMPDIR_mutex, key_THR_LOCK_myisam_mmap, key_LOCK_uuid_generator;
key_LOCK_uuid_v7_generator;
static PSI_mutex_info all_mysys_mutexes[]= static PSI_mutex_info all_mysys_mutexes[]=
{ {
@ -497,8 +496,7 @@ static PSI_mutex_info all_mysys_mutexes[]=
{ &key_THR_LOCK_threads, "THR_LOCK_threads", PSI_FLAG_GLOBAL}, { &key_THR_LOCK_threads, "THR_LOCK_threads", PSI_FLAG_GLOBAL},
{ &key_TMPDIR_mutex, "TMPDIR_mutex", PSI_FLAG_GLOBAL}, { &key_TMPDIR_mutex, "TMPDIR_mutex", PSI_FLAG_GLOBAL},
{ &key_THR_LOCK_myisam_mmap, "THR_LOCK_myisam_mmap", PSI_FLAG_GLOBAL}, { &key_THR_LOCK_myisam_mmap, "THR_LOCK_myisam_mmap", PSI_FLAG_GLOBAL},
{ &key_LOCK_uuid_generator, "LOCK_uuid_generator", PSI_FLAG_GLOBAL }, { &key_LOCK_uuid_generator, "LOCK_uuid_generator", PSI_FLAG_GLOBAL }
{ &key_LOCK_uuid_v7_generator, "LOCK_uuid_v7_generator", PSI_FLAG_GLOBAL }
}; };
PSI_cond_key key_COND_timer, key_IO_CACHE_SHARE_cond, PSI_cond_key key_COND_timer, key_IO_CACHE_SHARE_cond,

View file

@ -48,8 +48,7 @@ extern PSI_mutex_key key_BITMAP_mutex, key_IO_CACHE_append_buffer_lock,
key_THR_LOCK_lock, key_THR_LOCK_malloc, key_THR_LOCK_lock, key_THR_LOCK_malloc,
key_THR_LOCK_mutex, key_THR_LOCK_myisam, key_THR_LOCK_net, key_THR_LOCK_mutex, key_THR_LOCK_myisam, key_THR_LOCK_net,
key_THR_LOCK_open, key_THR_LOCK_threads, key_LOCK_uuid_generator, key_THR_LOCK_open, key_THR_LOCK_threads, key_LOCK_uuid_generator,
key_LOCK_uuid_v7_generator, key_TMPDIR_mutex, key_THR_LOCK_myisam_mmap, key_TMPDIR_mutex, key_THR_LOCK_myisam_mmap, key_LOCK_timer;
key_LOCK_timer;
extern PSI_cond_key key_COND_timer, key_IO_CACHE_SHARE_cond, extern PSI_cond_key key_COND_timer, key_IO_CACHE_SHARE_cond,
key_IO_CACHE_SHARE_cond_writer, key_my_thread_var_suspend, key_IO_CACHE_SHARE_cond_writer, key_my_thread_var_suspend,

View file

@ -27,7 +27,8 @@
#include <my_crypt.h> #include <my_crypt.h>
#include <ssl_compat.h> #include <ssl_compat.h>
#include <cstdint>
#include <random>
#define CTX_ALIGN 16 #define CTX_ALIGN 16
@ -359,10 +360,25 @@ unsigned int my_aes_ctx_size(enum my_aes_mode)
return MY_AES_CTX_SIZE; return MY_AES_CTX_SIZE;
} }
static std::mt19937 rnd;
int my_random_bytes(uchar *buf, int num) int my_random_bytes(uchar *buf, int num)
{ {
if (RAND_bytes(buf, num) != 1) if (RAND_bytes(buf, num) != 1)
{ /* shouldn't happen */
uchar *end= buf + num - 3;
uint r= rnd();
for (; buf < end; buf+= 4, r= rnd())
int4store(buf, r);
switch (num % 4)
{
case 0: break;
case 1: *buf= rnd(); break;
case 2: r=rnd(); int2store(buf, r); break;
case 3: r=rnd(); int3store(buf, r); break;
}
return MY_AES_OPENSSL_ERROR; return MY_AES_OPENSSL_ERROR;
}
return MY_AES_OK; return MY_AES_OK;
} }

View file

@ -96,7 +96,7 @@ public:
using Item_func_uuid_vx::Item_func_uuid_vx; using Item_func_uuid_vx::Item_func_uuid_vx;
LEX_CSTRING func_name_cstring() const override LEX_CSTRING func_name_cstring() const override
{ {
static LEX_CSTRING name= {STRING_WITH_LEN("uuidv4") }; static LEX_CSTRING name= {STRING_WITH_LEN("uuid_v4") };
return name; return name;
} }
Item *do_get_copy(THD *thd) const override Item *do_get_copy(THD *thd) const override
@ -109,7 +109,7 @@ public:
using Item_func_uuid_vx::Item_func_uuid_vx; using Item_func_uuid_vx::Item_func_uuid_vx;
LEX_CSTRING func_name_cstring() const override LEX_CSTRING func_name_cstring() const override
{ {
static LEX_CSTRING name= {STRING_WITH_LEN("uuidv7") }; static LEX_CSTRING name= {STRING_WITH_LEN("uuid_v7") };
return name; return name;
} }
Item *do_get_copy(THD *thd) const override Item *do_get_copy(THD *thd) const override

View file

@ -1,10 +1,10 @@
# #
# MDEV-33827 UUID() returns a NULL-able result # MDEV-33827 UUID() returns a NULL-able result
# #
CREATE TABLE t1 AS SELECT UUIDv4(); CREATE TABLE t1 AS SELECT UUID_v4();
SHOW CREATE TABLE t1; SHOW CREATE TABLE t1;
Table Create Table Table Create Table
t1 CREATE TABLE `t1` ( t1 CREATE TABLE `t1` (
`UUIDv4()` uuid NOT NULL `UUID_v4()` uuid NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t1; DROP TABLE t1;

View file

@ -2,6 +2,6 @@
--echo # MDEV-33827 UUID() returns a NULL-able result --echo # MDEV-33827 UUID() returns a NULL-able result
--echo # --echo #
CREATE TABLE t1 AS SELECT UUIDv4(); CREATE TABLE t1 AS SELECT UUID_v4();
SHOW CREATE TABLE t1; SHOW CREATE TABLE t1;
DROP TABLE t1; DROP TABLE t1;

View file

@ -0,0 +1,39 @@
SET debug_dbug="+d,simulate_uuidv4_my_random_bytes_failure";
#
# MDEV-11339 Support UUID v4 generation
#
CREATE TABLE t1 (
a int primary key not null,
u UUID DEFAULT UUID_v4(),
unique key(u)
);
INSERT INTO t1(a) SELECT seq FROM seq_1_to_16;
Warnings:
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v4: RANDOM_BYTES() failed, using fallback
SELECT COUNT(DISTINCT u) AS distinct_uuid_count FROM t1;
distinct_uuid_count
16
SELECT
u REGEXP '[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' AS is_correct_version_and_revision,
COUNT(*)
FROM t1 GROUP BY is_correct_version_and_revision;
is_correct_version_and_revision COUNT(*)
1 16
DROP TABLE t1;
# End of 11.7 tests
SET debug_dbug="";

View file

@ -4,17 +4,13 @@ SET debug_dbug="+d,simulate_uuidv4_my_random_bytes_failure";
source include/have_sequence.inc; source include/have_sequence.inc;
--echo #
--echo # Start of 11.6 tests
--echo #
--echo # --echo #
--echo # MDEV-11339 Support UUID v4 generation --echo # MDEV-11339 Support UUID v4 generation
--echo # --echo #
CREATE TABLE t1 ( CREATE TABLE t1 (
a int primary key not null, a int primary key not null,
u UUID DEFAULT UUIDv4(), u UUID DEFAULT UUID_v4(),
unique key(u) unique key(u)
); );
INSERT INTO t1(a) SELECT seq FROM seq_1_to_16; INSERT INTO t1(a) SELECT seq FROM seq_1_to_16;
@ -25,8 +21,6 @@ SELECT
FROM t1 GROUP BY is_correct_version_and_revision; FROM t1 GROUP BY is_correct_version_and_revision;
DROP TABLE t1; DROP TABLE t1;
--echo # --echo # End of 11.7 tests
--echo # End of 11.6 tests
--echo #
SET debug_dbug=""; SET debug_dbug="";

View file

@ -1,7 +1,4 @@
# #
# Start of 11.6 tests
#
#
# MDEV-11339 Support UUID v4 generation # MDEV-11339 Support UUID v4 generation
# #
SELECT SELECT
@ -18,18 +15,16 @@ PLUGIN_AUTH_VERSION
FROM INFORMATION_SCHEMA.PLUGINS FROM INFORMATION_SCHEMA.PLUGINS
WHERE PLUGIN_TYPE='FUNCTION' WHERE PLUGIN_TYPE='FUNCTION'
AND PLUGIN_NAME IN AND PLUGIN_NAME IN
('uuidv4') ('uuid_v4')
ORDER BY PLUGIN_NAME; ORDER BY PLUGIN_NAME;
---- ---- ---- ----
PLUGIN_NAME uuidv4 PLUGIN_NAME uuid_v4
PLUGIN_VERSION 1.0 PLUGIN_VERSION 1.0
PLUGIN_STATUS ACTIVE PLUGIN_STATUS ACTIVE
PLUGIN_TYPE FUNCTION PLUGIN_TYPE FUNCTION
PLUGIN_AUTHOR Stefano Petrilli PLUGIN_AUTHOR Stefano Petrilli
PLUGIN_DESCRIPTION Function UUIDv4() PLUGIN_DESCRIPTION Function UUID_v4()
PLUGIN_LICENSE GPL PLUGIN_LICENSE GPL
PLUGIN_MATURITY Experimental PLUGIN_MATURITY Experimental
PLUGIN_AUTH_VERSION 1.0 PLUGIN_AUTH_VERSION 1.0
# # End of 11.7 tests
# End of 11.6 tests
#

View file

@ -1,7 +1,3 @@
--echo #
--echo # Start of 11.6 tests
--echo #
--echo # --echo #
--echo # MDEV-11339 Support UUID v4 generation --echo # MDEV-11339 Support UUID v4 generation
--echo # --echo #
@ -21,10 +17,8 @@ SELECT
FROM INFORMATION_SCHEMA.PLUGINS FROM INFORMATION_SCHEMA.PLUGINS
WHERE PLUGIN_TYPE='FUNCTION' WHERE PLUGIN_TYPE='FUNCTION'
AND PLUGIN_NAME IN AND PLUGIN_NAME IN
('uuidv4') ('uuid_v4')
ORDER BY PLUGIN_NAME; ORDER BY PLUGIN_NAME;
--horizontal_results --horizontal_results
--echo # --echo # End of 11.7 tests
--echo # End of 11.6 tests
--echo #

View file

@ -1,15 +1,10 @@
# #
# Start of 11.6 tests
#
#
# MDEV-11339 Support UUID v4 generation # MDEV-11339 Support UUID v4 generation
# #
CREATE TABLE t1 (a int primary key not null, u UUID DEFAULT UUIDv4(), unique key(u)); CREATE TABLE t1 (a int primary key not null, u UUID DEFAULT UUID_v4(), unique key(u));
insert into t1(a) select seq from seq_1_to_100; insert into t1(a) select seq from seq_1_to_100;
select count(distinct u) AS distinct_uuid_count from t1; select count(distinct u) AS distinct_uuid_count from t1;
distinct_uuid_count distinct_uuid_count
100 100
drop table t1; drop table t1;
# # End of 11.7 tests
# End of 11.6 tests
#

View file

@ -1,18 +1,12 @@
source include/have_sequence.inc; source include/have_sequence.inc;
--echo #
--echo # Start of 11.6 tests
--echo #
--echo # --echo #
--echo # MDEV-11339 Support UUID v4 generation --echo # MDEV-11339 Support UUID v4 generation
--echo # --echo #
CREATE TABLE t1 (a int primary key not null, u UUID DEFAULT UUIDv4(), unique key(u)); CREATE TABLE t1 (a int primary key not null, u UUID DEFAULT UUID_v4(), unique key(u));
insert into t1(a) select seq from seq_1_to_100; insert into t1(a) select seq from seq_1_to_100;
select count(distinct u) AS distinct_uuid_count from t1; select count(distinct u) AS distinct_uuid_count from t1;
drop table t1; drop table t1;
--echo # --echo # End of 11.7 tests
--echo # End of 11.6 tests
--echo #

View file

@ -0,0 +1,7 @@
#
# MDEV-11339 Support UUID v4 generation
#
SELECT UUID_v4() REGEXP '[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' AS is_correct_version_and_revision;
is_correct_version_and_revision
1
# End of 11.7 tests

View file

@ -0,0 +1,7 @@
--echo #
--echo # MDEV-11339 Support UUID v4 generation
--echo #
SELECT UUID_v4() REGEXP '[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' AS is_correct_version_and_revision;
--echo # End of 11.7 tests

View file

@ -1,10 +1,10 @@
# #
# MDEV-33827 UUID() returns a NULL-able result # MDEV-33827 UUID() returns a NULL-able result
# #
CREATE TABLE t1 AS SELECT UUIDv7(); CREATE TABLE t1 AS SELECT UUID_v7();
SHOW CREATE TABLE t1; SHOW CREATE TABLE t1;
Table Create Table Table Create Table
t1 CREATE TABLE `t1` ( t1 CREATE TABLE `t1` (
`UUIDv7()` uuid NOT NULL `UUID_v7()` uuid NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
DROP TABLE t1; DROP TABLE t1;

View file

@ -2,6 +2,6 @@
--echo # MDEV-33827 UUID() returns a NULL-able result --echo # MDEV-33827 UUID() returns a NULL-able result
--echo # --echo #
CREATE TABLE t1 AS SELECT UUIDv7(); CREATE TABLE t1 AS SELECT UUID_v7();
SHOW CREATE TABLE t1; SHOW CREATE TABLE t1;
DROP TABLE t1; DROP TABLE t1;

View file

@ -0,0 +1,44 @@
SET debug_dbug="+d,simulate_uuidv7_my_random_bytes_failure";
#
# Start of 11.7 tests
#
#
# MDEV-32637 Implement native UUID7 function
#
CREATE TABLE t1 (
a int primary key not null,
u UUID DEFAULT UUID_v7(),
unique key(u)
);
INSERT INTO t1(a) SELECT seq FROM seq_1_to_16;
Warnings:
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
Note 1105 UUID_v7: RANDOM_BYTES() failed, using fallback
SELECT COUNT(DISTINCT u) AS distinct_uuid_count FROM t1;
distinct_uuid_count
16
SELECT
u REGEXP '[a-f0-9]{8}-[a-f0-9]{4}-7[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' AS is_correct_version_and_revision,
COUNT(*)
FROM t1 GROUP BY is_correct_version_and_revision;
is_correct_version_and_revision COUNT(*)
1 16
DROP TABLE t1;
#
# End of 11.7 tests
#
SET debug_dbug="";

View file

@ -14,7 +14,7 @@ source include/have_sequence.inc;
CREATE TABLE t1 ( CREATE TABLE t1 (
a int primary key not null, a int primary key not null,
u UUID DEFAULT UUIDv7(), u UUID DEFAULT UUID_v7(),
unique key(u) unique key(u)
); );
INSERT INTO t1(a) SELECT seq FROM seq_1_to_16; INSERT INTO t1(a) SELECT seq FROM seq_1_to_16;

View file

@ -18,15 +18,15 @@ PLUGIN_AUTH_VERSION
FROM INFORMATION_SCHEMA.PLUGINS FROM INFORMATION_SCHEMA.PLUGINS
WHERE PLUGIN_TYPE='FUNCTION' WHERE PLUGIN_TYPE='FUNCTION'
AND PLUGIN_NAME IN AND PLUGIN_NAME IN
('uuidv7') ('uuid_v7')
ORDER BY PLUGIN_NAME; ORDER BY PLUGIN_NAME;
---- ---- ---- ----
PLUGIN_NAME uuidv7 PLUGIN_NAME uuid_v7
PLUGIN_VERSION 1.0 PLUGIN_VERSION 1.0
PLUGIN_STATUS ACTIVE PLUGIN_STATUS ACTIVE
PLUGIN_TYPE FUNCTION PLUGIN_TYPE FUNCTION
PLUGIN_AUTHOR Stefano Petrilli PLUGIN_AUTHOR Stefano Petrilli
PLUGIN_DESCRIPTION Function UUIDv7() PLUGIN_DESCRIPTION Function UUID_v7()
PLUGIN_LICENSE GPL PLUGIN_LICENSE GPL
PLUGIN_MATURITY Experimental PLUGIN_MATURITY Experimental
PLUGIN_AUTH_VERSION 1.0 PLUGIN_AUTH_VERSION 1.0

View file

@ -21,7 +21,7 @@ SELECT
FROM INFORMATION_SCHEMA.PLUGINS FROM INFORMATION_SCHEMA.PLUGINS
WHERE PLUGIN_TYPE='FUNCTION' WHERE PLUGIN_TYPE='FUNCTION'
AND PLUGIN_NAME IN AND PLUGIN_NAME IN
('uuidv7') ('uuid_v7')
ORDER BY PLUGIN_NAME; ORDER BY PLUGIN_NAME;
--horizontal_results --horizontal_results

View file

@ -4,7 +4,7 @@
# #
# MDEV-32637 Implement native UUID7 function # MDEV-32637 Implement native UUID7 function
# #
CREATE TABLE t1 (a int primary key not null, u UUID DEFAULT UUIDv7(), unique key(u)); CREATE TABLE t1 (a int primary key not null, u UUID DEFAULT UUID_v7(), unique key(u));
insert into t1(a) select seq from seq_1_to_100; insert into t1(a) select seq from seq_1_to_100;
select count(distinct u) AS distinct_uuid_count from t1; select count(distinct u) AS distinct_uuid_count from t1;
distinct_uuid_count distinct_uuid_count

View file

@ -8,7 +8,7 @@ source include/have_sequence.inc;
--echo # MDEV-32637 Implement native UUID7 function --echo # MDEV-32637 Implement native UUID7 function
--echo # --echo #
CREATE TABLE t1 (a int primary key not null, u UUID DEFAULT UUIDv7(), unique key(u)); CREATE TABLE t1 (a int primary key not null, u UUID DEFAULT UUID_v7(), unique key(u));
insert into t1(a) select seq from seq_1_to_100; insert into t1(a) select seq from seq_1_to_100;
select count(distinct u) AS distinct_uuid_count from t1; select count(distinct u) AS distinct_uuid_count from t1;
drop table t1; drop table t1;

View file

@ -4,7 +4,7 @@
# #
# MDEV-32637 Implement native UUID7 function # MDEV-32637 Implement native UUID7 function
# #
SELECT UUIDv7() REGEXP '[a-f0-9]{8}-[a-f0-9]{4}-7[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' AS is_correct_version_and_revision; SELECT UUID_v7() REGEXP '[a-f0-9]{8}-[a-f0-9]{4}-7[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' AS is_correct_version_and_revision;
is_correct_version_and_revision is_correct_version_and_revision
1 1
# #

View file

@ -6,7 +6,7 @@
--echo # MDEV-32637 Implement native UUID7 function --echo # MDEV-32637 Implement native UUID7 function
--echo # --echo #
SELECT UUIDv7() REGEXP '[a-f0-9]{8}-[a-f0-9]{4}-7[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' AS is_correct_version_and_revision; SELECT UUID_v7() REGEXP '[a-f0-9]{8}-[a-f0-9]{4}-7[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' AS is_correct_version_and_revision;
--echo # --echo #
--echo # End of 11.7 tests --echo # End of 11.7 tests

View file

@ -1,44 +0,0 @@
SET debug_dbug="+d,simulate_uuidv4_my_random_bytes_failure";
#
# Start of 11.6 tests
#
#
# MDEV-11339 Support UUID v4 generation
#
CREATE TABLE t1 (
a int primary key not null,
u UUID DEFAULT UUIDv4(),
unique key(u)
);
INSERT INTO t1(a) SELECT seq FROM seq_1_to_16;
Warnings:
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
Note 1105 UUIDv4 generation failed; using a my_rnd fallback
SELECT COUNT(DISTINCT u) AS distinct_uuid_count FROM t1;
distinct_uuid_count
16
SELECT
u REGEXP '[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' AS is_correct_version_and_revision,
COUNT(*)
FROM t1 GROUP BY is_correct_version_and_revision;
is_correct_version_and_revision COUNT(*)
1 16
DROP TABLE t1;
#
# End of 11.6 tests
#
SET debug_dbug="";

View file

@ -1,12 +0,0 @@
#
# Start of 11.6 tests
#
#
# MDEV-11339 Support UUID v4 generation
#
SELECT UUIDv4() REGEXP '[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' AS is_correct_version_and_revision;
is_correct_version_and_revision
1
#
# End of 11.6 tests
#

View file

@ -1,13 +0,0 @@
--echo #
--echo # Start of 11.6 tests
--echo #
--echo #
--echo # MDEV-11339 Support UUID v4 generation
--echo #
SELECT UUIDv4() REGEXP '[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' AS is_correct_version_and_revision;
--echo #
--echo # End of 11.6 tests
--echo #

View file

@ -1,44 +0,0 @@
SET debug_dbug="+d,simulate_uuidv7_my_random_bytes_failure";
#
# Start of 11.7 tests
#
#
# MDEV-32637 Implement native UUID7 function
#
CREATE TABLE t1 (
a int primary key not null,
u UUID DEFAULT UUIDv7(),
unique key(u)
);
INSERT INTO t1(a) SELECT seq FROM seq_1_to_16;
Warnings:
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
Note 1105 UUIDv7 generation failed; using a my_rnd fallback
SELECT COUNT(DISTINCT u) AS distinct_uuid_count FROM t1;
distinct_uuid_count
16
SELECT
u REGEXP '[a-f0-9]{8}-[a-f0-9]{4}-7[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}' AS is_correct_version_and_revision,
COUNT(*)
FROM t1 GROUP BY is_correct_version_and_revision;
is_correct_version_and_revision COUNT(*)
1 16
DROP TABLE t1;
#
# End of 11.7 tests
#
SET debug_dbug="";

View file

@ -834,7 +834,7 @@ drop table t1;
# #
# MDEV-32637 Implement native UUID7 function # MDEV-32637 Implement native UUID7 function
# #
CREATE TABLE t1 (a int primary key not null, u UUID DEFAULT UUIDv7(), unique key(u)); CREATE TABLE t1 (a int primary key not null, u UUID DEFAULT UUID_v7(), unique key(u));
insert into t1(a) select seq from seq_1_to_10; insert into t1(a) select seq from seq_1_to_10;
select a from t1 order by u asc; select a from t1 order by u asc;
a a

View file

@ -67,7 +67,7 @@ drop table t1;
# Verify that UUIDv7 are time-ordered and unique # Verify that UUIDv7 are time-ordered and unique
CREATE TABLE t1 (a int primary key not null, u UUID DEFAULT UUIDv7(), unique key(u)); CREATE TABLE t1 (a int primary key not null, u UUID DEFAULT UUID_v7(), unique key(u));
insert into t1(a) select seq from seq_1_to_10; insert into t1(a) select seq from seq_1_to_10;
select a from t1 order by u asc; select a from t1 order by u asc;
drop table t1; drop table t1;

View file

@ -5,18 +5,18 @@ include/master-slave.inc
# #
connection master; connection master;
CREATE TABLE test.t1 (a INT, blob_column LONGBLOB, vchar_column VARCHAR(100), PRIMARY KEY(a)) engine=myisam; CREATE TABLE test.t1 (a INT, blob_column LONGBLOB, vchar_column VARCHAR(100), PRIMARY KEY(a)) engine=myisam;
INSERT INTO test.t1 VALUES(1,UUIDv7(),UUIDv7()); INSERT INTO test.t1 VALUES(1,UUID_v7(),UUID_v7());
create procedure test.p1() create procedure test.p1()
begin begin
INSERT INTO test.t1 VALUES(2,UUIDv7(),UUIDv7()); INSERT INTO test.t1 VALUES(2,UUID_v7(),UUID_v7());
INSERT INTO test.t1 VALUES(3,UUIDv7(),UUIDv7()); INSERT INTO test.t1 VALUES(3,UUID_v7(),UUID_v7());
end| end|
CALL test.p1(); CALL test.p1();
create function test.fn1(x int) create function test.fn1(x int)
returns int returns int
begin begin
insert into t1 values (4+x,UUIDv7(),UUIDv7()); insert into t1 values (4+x,UUID_v7(),UUID_v7());
insert into t1 values (5+x,UUIDv7(),UUIDv7()); insert into t1 values (5+x,UUID_v7(),UUID_v7());
return 0; return 0;
end| end|
select fn1(0); select fn1(0);

View file

@ -10,12 +10,12 @@ connection master;
# Start the actual test # Start the actual test
CREATE TABLE test.t1 (a INT, blob_column LONGBLOB, vchar_column VARCHAR(100), PRIMARY KEY(a)) engine=myisam; CREATE TABLE test.t1 (a INT, blob_column LONGBLOB, vchar_column VARCHAR(100), PRIMARY KEY(a)) engine=myisam;
INSERT INTO test.t1 VALUES(1,UUIDv7(),UUIDv7()); INSERT INTO test.t1 VALUES(1,UUID_v7(),UUID_v7());
delimiter |; delimiter |;
create procedure test.p1() create procedure test.p1()
begin begin
INSERT INTO test.t1 VALUES(2,UUIDv7(),UUIDv7()); INSERT INTO test.t1 VALUES(2,UUID_v7(),UUID_v7());
INSERT INTO test.t1 VALUES(3,UUIDv7(),UUIDv7()); INSERT INTO test.t1 VALUES(3,UUID_v7(),UUID_v7());
end| end|
delimiter ;| delimiter ;|
@ -25,8 +25,8 @@ delimiter |;
create function test.fn1(x int) create function test.fn1(x int)
returns int returns int
begin begin
insert into t1 values (4+x,UUIDv7(),UUIDv7()); insert into t1 values (4+x,UUID_v7(),UUID_v7());
insert into t1 values (5+x,UUIDv7(),UUIDv7()); insert into t1 values (5+x,UUID_v7(),UUID_v7());
return 0; return 0;
end| end|
@ -46,8 +46,8 @@ SHOW CREATE TABLE test.t1;
# then LOAD DATA INFILE in slave, and use a query to compare. # then LOAD DATA INFILE in slave, and use a query to compare.
# This would have the advantage that it would not assume # This would have the advantage that it would not assume
# the system has a 'diff' # the system has a 'diff'
--exec $MYSQL_DUMP --compact --order-by-primary --skip-extended-insert --no-create-info test > $MYSQLTEST_VARDIR/tmp/rpl_row_UUIDv7_master.sql --exec $MYSQL_DUMP --compact --order-by-primary --skip-extended-insert --no-create-info test > $MYSQLTEST_VARDIR/tmp/rpl_row_UUID_v7_master.sql
--exec $MYSQL_DUMP_SLAVE --compact --order-by-primary --skip-extended-insert --no-create-info test > $MYSQLTEST_VARDIR/tmp/rpl_row_UUIDv7_slave.sql --exec $MYSQL_DUMP_SLAVE --compact --order-by-primary --skip-extended-insert --no-create-info test > $MYSQLTEST_VARDIR/tmp/rpl_row_UUID_v7_slave.sql
connection master; connection master;
@ -61,10 +61,10 @@ DROP TABLE test.t2;
# will be created. You will need to go to the mysql-test dir and diff # will be created. You will need to go to the mysql-test dir and diff
# the files your self to see what is not matching :-) # the files your self to see what is not matching :-)
diff_files $MYSQLTEST_VARDIR/tmp/rpl_row_UUIDv7_master.sql $MYSQLTEST_VARDIR/tmp/rpl_row_UUIDv7_slave.sql; diff_files $MYSQLTEST_VARDIR/tmp/rpl_row_UUID_v7_master.sql $MYSQLTEST_VARDIR/tmp/rpl_row_UUID_v7_slave.sql;
remove_file $MYSQLTEST_VARDIR/tmp/rpl_row_UUIDv7_master.sql; remove_file $MYSQLTEST_VARDIR/tmp/rpl_row_UUID_v7_master.sql;
remove_file $MYSQLTEST_VARDIR/tmp/rpl_row_UUIDv7_slave.sql; remove_file $MYSQLTEST_VARDIR/tmp/rpl_row_UUID_v7_slave.sql;
--echo # End of 11.7 tests --echo # End of 11.7 tests

View file

@ -202,7 +202,19 @@ int uuid_init(void*)
int uuidv7_init(void*) int uuidv7_init(void*)
{ {
mysql_mutex_init(0, &LOCK_uuid_v7_generator, MY_MUTEX_INIT_FAST); #ifdef HAVE_PSI_INTERFACE
static PSI_mutex_key key_LOCK_uuid_v7_generator;
static PSI_mutex_info psi_mutexes[]=
{
{ &key_LOCK_uuid_v7_generator, "LOCK_uuid_v7_generator", PSI_FLAG_GLOBAL }
};
mysql_mutex_register("uuid_v7", psi_mutexes, 1);
#else
#define key_LOCK_uuid_v7_generator 0
#endif
mysql_mutex_init(key_LOCK_uuid_v7_generator, &LOCK_uuid_v7_generator,
MY_MUTEX_INIT_FAST);
return 0; return 0;
} }
@ -263,9 +275,9 @@ maria_declare_plugin(type_uuid)
{ {
MariaDB_FUNCTION_PLUGIN, // the plugin type (see include/mysql/plugin.h) MariaDB_FUNCTION_PLUGIN, // the plugin type (see include/mysql/plugin.h)
&plugin_descriptor_function_uuid_v4, // pointer to type-specific plugin descriptor &plugin_descriptor_function_uuid_v4, // pointer to type-specific plugin descriptor
"uuidv4", // plugin name "uuid_v4", // plugin name
"Stefano Petrilli", // plugin author "Stefano Petrilli", // plugin author
"Function UUIDv4()", // the plugin description "Function UUID_v4()", // the plugin description
PLUGIN_LICENSE_GPL, // the plugin license (see include/mysql/plugin.h) PLUGIN_LICENSE_GPL, // the plugin license (see include/mysql/plugin.h)
0, // Pointer to plugin initialization function 0, // Pointer to plugin initialization function
0, // Pointer to plugin deinitialization function 0, // Pointer to plugin deinitialization function
@ -278,9 +290,9 @@ maria_declare_plugin(type_uuid)
{ {
MariaDB_FUNCTION_PLUGIN, // the plugin type (see include/mysql/plugin.h) MariaDB_FUNCTION_PLUGIN, // the plugin type (see include/mysql/plugin.h)
&plugin_descriptor_function_uuid_v7, // pointer to type-specific plugin descriptor &plugin_descriptor_function_uuid_v7, // pointer to type-specific plugin descriptor
"uuidv7", // plugin name "uuid_v7", // plugin name
"Stefano Petrilli", // plugin author "Stefano Petrilli", // plugin author
"Function UUIDv7()", // the plugin description "Function UUID_v7()", // the plugin description
PLUGIN_LICENSE_GPL, // the plugin license (see include/mysql/plugin.h) PLUGIN_LICENSE_GPL, // the plugin license (see include/mysql/plugin.h)
uuidv7_init, // Pointer to plugin initialization function uuidv7_init, // Pointer to plugin initialization function
uuidv7_terminate, // Pointer to plugin deinitialization function uuidv7_terminate, // Pointer to plugin deinitialization function

View file

@ -18,6 +18,9 @@
#include "sql_type_fixedbin_storage.h" #include "sql_type_fixedbin_storage.h"
static constexpr uchar UUID_VERSION_MASK() { return 0x0F; }
static constexpr uchar UUID_VARIANT_MASK() { return 0x3F; }
template <bool force_swap> template <bool force_swap>
class UUID: public FixedBinTypeStorage<MY_UUID_SIZE, MY_UUID_STRING_LENGTH> class UUID: public FixedBinTypeStorage<MY_UUID_SIZE, MY_UUID_STRING_LENGTH>
{ {

View file

@ -18,7 +18,7 @@
/* /*
Implements Universal Unique Identifiers version 4, as described in Implements Universal Unique Identifiers version 4, as described in
draft-ietf-uuidrev-rfc4122bis-14. RFC 9562.
Field Octet # Note Field Octet # Note
random_a 0-5 Random CSPRNG 48 bits. random_a 0-5 Random CSPRNG 48 bits.
@ -36,14 +36,11 @@
*/ */
#include "sql_type_uuid.h" #include "sql_type_uuid.h"
#include "my_rnd.h"
class UUIDv4: public Type_handler_uuid_new::Fbt class UUIDv4: public Type_handler_uuid_new::Fbt
{ {
static constexpr uchar UUID_VERSION() { return 0x40; } static constexpr uchar UUID_VERSION() { return 0x40; }
static constexpr uchar UUID_VERSION_MASK() { return 0x0F; }
static constexpr uchar UUID_VARIANT() { return 0x80; } static constexpr uchar UUID_VARIANT() { return 0x80; }
static constexpr uchar UUID_VARIANT_MASK() { return 0x3F; }
static void inject_version_and_variant(uchar *to) static void inject_version_and_variant(uchar *to)
{ {
@ -51,13 +48,6 @@ class UUIDv4: public Type_handler_uuid_new::Fbt
to[8]= ((to[8] & UUID_VARIANT_MASK()) | UUID_VARIANT()); to[8]= ((to[8] & UUID_VARIANT_MASK()) | UUID_VARIANT());
} }
// Construct using a my_rnd()-based fallback method
static void construct_fallback(char *to)
{
for (uint i= 0; i < 4; i++)
int4store(&to[i * 4], (uint32) (my_rnd(&sql_rand)*0xFFFFFFFF));
}
static void construct(char *to) static void construct(char *to)
{ {
bool error= my_random_bytes((uchar*) to, 16) != MY_AES_OK; bool error= my_random_bytes((uchar*) to, 16) != MY_AES_OK;
@ -67,8 +57,7 @@ class UUIDv4: public Type_handler_uuid_new::Fbt
{ {
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_NOTE, push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_NOTE,
ER_UNKNOWN_ERROR, ER_UNKNOWN_ERROR,
"UUIDv4 generation failed; using a my_rnd fallback"); "UUID_v4: RANDOM_BYTES() failed, using fallback");
construct_fallback(to);
} }
/* /*
We have random bytes at to[6] and to[8]. We have random bytes at to[6] and to[8].

View file

@ -41,7 +41,6 @@
*/ */
#include "sql_type_uuid.h" #include "sql_type_uuid.h"
#include <my_rnd.h>
#include <m_string.h> #include <m_string.h>
#include <myisampack.h> /* mi_int2store, mi_int6store */ #include <myisampack.h> /* mi_int2store, mi_int6store */
#include <errmsg.h> #include <errmsg.h>
@ -52,10 +51,7 @@ extern mysql_mutex_t LOCK_uuid_v7_generator;
class UUIDv7: public Type_handler_uuid_new::Fbt class UUIDv7: public Type_handler_uuid_new::Fbt
{ {
static constexpr uchar UUID_VERSION() { return 0x70; } static constexpr uchar UUID_VERSION() { return 0x70; }
static constexpr uchar UUID_VERSION_MASK() { return 0x0F; }
static constexpr uchar UUID_VARIANT() { return 0x80; } static constexpr uchar UUID_VARIANT() { return 0x80; }
static constexpr uchar UUID_VARIANT_MASK() { return 0x3F; }
static constexpr double MICROSECONDS_TO_12BIT_MAPPING_FACTOR() { return 4.096; }
static void inject_version_and_variant(uchar *to) static void inject_version_and_variant(uchar *to)
{ {
@ -63,13 +59,6 @@ class UUIDv7: public Type_handler_uuid_new::Fbt
to[8]= ((to[8] & UUID_VARIANT_MASK()) | UUID_VARIANT()); to[8]= ((to[8] & UUID_VARIANT_MASK()) | UUID_VARIANT());
} }
// Construct using a my_rnd()-based fallback method
static void construct_fallback(char *to)
{
for (uint i= 0; i < 4; i++)
int4store(&to[i * 4], (uint32) (my_rnd(&sql_rand)*0xFFFFFFFF));
}
static void construct(char *to) static void construct(char *to)
{ {
bool error= my_random_bytes((uchar*) to+8, 8) != MY_AES_OK; bool error= my_random_bytes((uchar*) to+8, 8) != MY_AES_OK;
@ -79,23 +68,22 @@ class UUIDv7: public Type_handler_uuid_new::Fbt
{ {
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_NOTE, push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_NOTE,
ER_UNKNOWN_ERROR, ER_UNKNOWN_ERROR,
"UUIDv7 generation failed; using a my_rnd fallback"); "UUID_v7: RANDOM_BYTES() failed, using fallback");
construct_fallback(to);
} }
uint64 tv= my_hrtime().val; /*
We have 12 bits for to ensure monotonocity. Let's store microseconds
there (from 0 to 999) as described in section 6.2, Method 3 of RFC 9562,
and use two remaining bits as a counter, thus allowing 4000 UUIDv7
values to be generated within one millisecond.
*/
uint64 tv= my_hrtime().val * 4;
mysql_mutex_lock(&LOCK_uuid_v7_generator); mysql_mutex_lock(&LOCK_uuid_v7_generator);
last_uuidv7_timestamp= tv= MY_MAX(last_uuidv7_timestamp+1, tv); last_uuidv7_timestamp= tv= MY_MAX(last_uuidv7_timestamp+1, tv);
mysql_mutex_unlock(&LOCK_uuid_v7_generator); mysql_mutex_unlock(&LOCK_uuid_v7_generator);
mi_int6store(to, tv / 1000); mi_int6store(to, tv / 4000);
mi_int2store(to+6, tv % 4000);
/**
Map all the possible microseconds values (from 0 to 999) to all the
values that can be represented in 12 bits (from 0 to 4095) as
described in section 6.2, Method 3 of RFC 9562.
*/
mi_int2store(to+6, MICROSECONDS_TO_12BIT_MAPPING_FACTOR() * (tv % 1000));
/* /*
Let's inject proper version and variant to make it good UUIDv7. Let's inject proper version and variant to make it good UUIDv7.