Skip to content

Commit d9752d8

Browse files
committed
MDEV-31926 UUID v7 are compared incorrectly
According to the standart draft UUIDv6 and UUIDv7 values must be compared as opaque raw bytes. Let's only compare with byte-swapping if both values need byte swapping.
1 parent 5436b5d commit d9752d8

File tree

4 files changed

+103
-12
lines changed

4 files changed

+103
-12
lines changed

plugin/type_uuid/mysql-test/type_uuid/order.result

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,36 @@ NULL 48
756756
NULL 52
757757
NULL 56
758758
NULL 60
759+
select * from t1, t1 t where t1.b div 4 in (6,7) and t.b div 4 in (6,7) and t1.a > t.a;
760+
a b a b
761+
02601234-5566-6777-c888-99aabbccddee 26 02401234-5566-6777-0888-99aabbccddee 24
762+
02801234-5566-7777-0888-99aabbccddee 28 02401234-5566-6777-0888-99aabbccddee 24
763+
02801234-5566-7777-0888-99aabbccddee 28 02601234-5566-6777-c888-99aabbccddee 26
764+
03001234-5566-7777-c888-99aabbccddee 30 02401234-5566-6777-0888-99aabbccddee 24
765+
03001234-5566-7777-c888-99aabbccddee 30 02601234-5566-6777-c888-99aabbccddee 26
766+
03001234-5566-7777-c888-99aabbccddee 30 02801234-5566-7777-0888-99aabbccddee 28
767+
12501234-5566-6777-8888-99aabbccddee 25 02401234-5566-6777-0888-99aabbccddee 24
768+
12501234-5566-6777-8888-99aabbccddee 25 02601234-5566-6777-c888-99aabbccddee 26
769+
12501234-5566-6777-8888-99aabbccddee 25 02801234-5566-7777-0888-99aabbccddee 28
770+
12501234-5566-6777-8888-99aabbccddee 25 03001234-5566-7777-c888-99aabbccddee 30
771+
12701234-5566-6777-e888-99aabbccddee 27 02401234-5566-6777-0888-99aabbccddee 24
772+
12701234-5566-6777-e888-99aabbccddee 27 02601234-5566-6777-c888-99aabbccddee 26
773+
12701234-5566-6777-e888-99aabbccddee 27 02801234-5566-7777-0888-99aabbccddee 28
774+
12701234-5566-6777-e888-99aabbccddee 27 03001234-5566-7777-c888-99aabbccddee 30
775+
12701234-5566-6777-e888-99aabbccddee 27 12501234-5566-6777-8888-99aabbccddee 25
776+
12901234-5566-7777-8888-99aabbccddee 29 02401234-5566-6777-0888-99aabbccddee 24
777+
12901234-5566-7777-8888-99aabbccddee 29 02601234-5566-6777-c888-99aabbccddee 26
778+
12901234-5566-7777-8888-99aabbccddee 29 02801234-5566-7777-0888-99aabbccddee 28
779+
12901234-5566-7777-8888-99aabbccddee 29 03001234-5566-7777-c888-99aabbccddee 30
780+
12901234-5566-7777-8888-99aabbccddee 29 12501234-5566-6777-8888-99aabbccddee 25
781+
12901234-5566-7777-8888-99aabbccddee 29 12701234-5566-6777-e888-99aabbccddee 27
782+
13101234-5566-7777-e888-99aabbccddee 31 02401234-5566-6777-0888-99aabbccddee 24
783+
13101234-5566-7777-e888-99aabbccddee 31 02601234-5566-6777-c888-99aabbccddee 26
784+
13101234-5566-7777-e888-99aabbccddee 31 02801234-5566-7777-0888-99aabbccddee 28
785+
13101234-5566-7777-e888-99aabbccddee 31 03001234-5566-7777-c888-99aabbccddee 30
786+
13101234-5566-7777-e888-99aabbccddee 31 12501234-5566-6777-8888-99aabbccddee 25
787+
13101234-5566-7777-e888-99aabbccddee 31 12701234-5566-6777-e888-99aabbccddee 27
788+
13101234-5566-7777-e888-99aabbccddee 31 12901234-5566-7777-8888-99aabbccddee 29
759789
alter ignore table t2 force;
760790
Warnings:
761791
Warning 1292 Incorrect uuid value: '03201234-5566-8777-0888-99aabbccddee' for column `test`.`t2`.`a` at row 33
@@ -766,4 +796,38 @@ Warning 1292 Incorrect uuid value: '04801234-5566-c777-0888-99aabbccddee' for co
766796
Warning 1292 Incorrect uuid value: '05201234-5566-d777-0888-99aabbccddee' for column `test`.`t2`.`a` at row 53
767797
Warning 1292 Incorrect uuid value: '05601234-5566-e777-0888-99aabbccddee' for column `test`.`t2`.`a` at row 57
768798
Warning 1292 Incorrect uuid value: '06001234-5566-f777-0888-99aabbccddee' for column `test`.`t2`.`a` at row 61
769-
drop table t1, t2;
799+
drop table t2;
800+
#
801+
# MDEV-31926 UUID v7 are compared incorrectly
802+
#
803+
select * from t1, t1 t where t1.b div 4 in (6,7) and t.b div 4 in (6,7) and concat(t1.a) > concat(t.a);
804+
a b a b
805+
02601234-5566-6777-c888-99aabbccddee 26 02401234-5566-6777-0888-99aabbccddee 24
806+
02801234-5566-7777-0888-99aabbccddee 28 02401234-5566-6777-0888-99aabbccddee 24
807+
02801234-5566-7777-0888-99aabbccddee 28 02601234-5566-6777-c888-99aabbccddee 26
808+
03001234-5566-7777-c888-99aabbccddee 30 02401234-5566-6777-0888-99aabbccddee 24
809+
03001234-5566-7777-c888-99aabbccddee 30 02601234-5566-6777-c888-99aabbccddee 26
810+
03001234-5566-7777-c888-99aabbccddee 30 02801234-5566-7777-0888-99aabbccddee 28
811+
12501234-5566-6777-8888-99aabbccddee 25 02401234-5566-6777-0888-99aabbccddee 24
812+
12501234-5566-6777-8888-99aabbccddee 25 02601234-5566-6777-c888-99aabbccddee 26
813+
12501234-5566-6777-8888-99aabbccddee 25 02801234-5566-7777-0888-99aabbccddee 28
814+
12501234-5566-6777-8888-99aabbccddee 25 03001234-5566-7777-c888-99aabbccddee 30
815+
12701234-5566-6777-e888-99aabbccddee 27 02401234-5566-6777-0888-99aabbccddee 24
816+
12701234-5566-6777-e888-99aabbccddee 27 02601234-5566-6777-c888-99aabbccddee 26
817+
12701234-5566-6777-e888-99aabbccddee 27 02801234-5566-7777-0888-99aabbccddee 28
818+
12701234-5566-6777-e888-99aabbccddee 27 03001234-5566-7777-c888-99aabbccddee 30
819+
12701234-5566-6777-e888-99aabbccddee 27 12501234-5566-6777-8888-99aabbccddee 25
820+
12901234-5566-7777-8888-99aabbccddee 29 02401234-5566-6777-0888-99aabbccddee 24
821+
12901234-5566-7777-8888-99aabbccddee 29 02601234-5566-6777-c888-99aabbccddee 26
822+
12901234-5566-7777-8888-99aabbccddee 29 02801234-5566-7777-0888-99aabbccddee 28
823+
12901234-5566-7777-8888-99aabbccddee 29 03001234-5566-7777-c888-99aabbccddee 30
824+
12901234-5566-7777-8888-99aabbccddee 29 12501234-5566-6777-8888-99aabbccddee 25
825+
12901234-5566-7777-8888-99aabbccddee 29 12701234-5566-6777-e888-99aabbccddee 27
826+
13101234-5566-7777-e888-99aabbccddee 31 02401234-5566-6777-0888-99aabbccddee 24
827+
13101234-5566-7777-e888-99aabbccddee 31 02601234-5566-6777-c888-99aabbccddee 26
828+
13101234-5566-7777-e888-99aabbccddee 31 02801234-5566-7777-0888-99aabbccddee 28
829+
13101234-5566-7777-e888-99aabbccddee 31 03001234-5566-7777-c888-99aabbccddee 30
830+
13101234-5566-7777-e888-99aabbccddee 31 12501234-5566-6777-8888-99aabbccddee 25
831+
13101234-5566-7777-e888-99aabbccddee 31 12701234-5566-6777-e888-99aabbccddee 27
832+
13101234-5566-7777-e888-99aabbccddee 31 12901234-5566-7777-8888-99aabbccddee 29
833+
drop table t1;

plugin/type_uuid/mysql-test/type_uuid/order.test

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ select * from t2 left join t1 on (t1.a<=>t2.a);
3636
--sorted_result
3737
select * from t1 union select * from t2;
3838

39+
--sorted_result
40+
select * from t1, t1 t where t1.b div 4 in (6,7) and t.b div 4 in (6,7) and t1.a > t.a;
41+
3942
alter ignore table t2 force;
4043

41-
drop table t1, t2;
44+
drop table t2;
45+
46+
--echo #
47+
--echo # MDEV-31926 UUID v7 are compared incorrectly
48+
--echo #
49+
50+
# for v6 and v7 string comparison should produce the same result.
51+
# Quoting the standard draft:
52+
#
53+
# 6.11. Sorting
54+
#
55+
# UUIDv6 and UUIDv7 are designed so that implementations that require
56+
# sorting (e.g., database indexes) sort as opaque raw bytes, without
57+
# need for parsing or introspection.
58+
#
59+
--sorted_result
60+
select * from t1, t1 t where t1.b div 4 in (6,7) and t.b div 4 in (6,7) and concat(t1.a) > concat(t.a);
61+
62+
drop table t1;

plugin/type_uuid/mysql-test/type_uuid/type_uuid.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3182,10 +3182,10 @@ d
31823182
11111111-0000-0000-0000-000000000000
31833183
SELECT * FROM t1 WHERE d <= ALL (SELECT * FROM t1);
31843184
d
3185-
11111111-0000-0000-0000-000000000000
3185+
00000000-0000-0000-0000-111111111111
31863186
SELECT * FROM t1 WHERE d >= ALL (SELECT * FROM t1);
31873187
d
3188-
00000000-0000-0000-0000-111111111111
3188+
11111111-0000-0000-0000-000000000000
31893189
DROP TABLE t1;
31903190
#
31913191
# MDEV-31719 Wrong result of: WHERE inet6_column IN ('','::1')

plugin/type_uuid/sql_type_uuid.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,20 @@ class UUID: public FixedBinTypeStorage<MY_UUID_SIZE, MY_UUID_STRING_LENGTH>
241241
{
242242
DBUG_ASSERT(a.length == binary_length());
243243
DBUG_ASSERT(b.length == binary_length());
244-
int res;
245-
if ((res= segment(4).cmp_memory(a.str, b.str)) ||
246-
(res= segment(3).cmp_memory(a.str, b.str)) ||
247-
(res= segment(2).cmp_memory(a.str, b.str)) ||
248-
(res= segment(1).cmp_memory(a.str, b.str)) ||
249-
(res= segment(0).cmp_memory(a.str, b.str)))
250-
return res;
251-
return 0;
244+
bool swap_a= force_swap || mem_need_swap(a.str);
245+
bool swap_b= force_swap || mem_need_swap(b.str);
246+
if (swap_a && swap_b)
247+
{
248+
int res;
249+
if ((res= segment(4).cmp_memory(a.str, b.str)) ||
250+
(res= segment(3).cmp_memory(a.str, b.str)) ||
251+
(res= segment(2).cmp_memory(a.str, b.str)) ||
252+
(res= segment(1).cmp_memory(a.str, b.str)) ||
253+
(res= segment(0).cmp_memory(a.str, b.str)))
254+
return res;
255+
return 0;
256+
}
257+
return memcmp(a.str, b.str, binary_length());
252258
}
253259

254260
static ulong KEY_pack_flags(uint column_nr)

0 commit comments

Comments
 (0)