Skip to content

Commit 8b8c8fc

Browse files
MDEV-33836 Compute modulus correctly in sequence
When the sequence is unsigned bigint, it needs to be cast to unsigned for correct computation of the modulus.
1 parent 2603453 commit 8b8c8fc

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

mysql-test/suite/sql_sequence/auto_increment.result

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,20 @@ create sequence s as bigint start with -9223372036854775805 minvalue -9223372036
4747
drop sequence s;
4848
set global auto_increment_increment= default, auto_increment_offset= default;
4949
#
50+
# MDEV-33836 Assertion `(ulonglong) next_free_value % real_increment == (ulonglong) offset' failed in void sequence_definition::adjust_values(longlong)
51+
#
52+
CREATE SEQUENCE s AS BIGINT UNSIGNED START WITH 9223372036854775800 INCREMENT 0;
53+
set @old_AUTO_INCREMENT_INCREMENT=@@global.AUTO_INCREMENT_INCREMENT;
54+
set global AUTO_INCREMENT_INCREMENT=10;
55+
SELECT NEXTVAL (s);
56+
NEXTVAL (s)
57+
9223372036854775800
58+
FLUSH TABLES WITH READ LOCK;
59+
UPDATE s SET a=1;
60+
Got one of the listed errors
61+
unlock tables;
62+
set global AUTO_INCREMENT_INCREMENT=@old_AUTO_INCREMENT_INCREMENT;
63+
drop sequence s;
64+
#
5065
# End of 11.5 tests
5166
#

mysql-test/suite/sql_sequence/auto_increment.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ create sequence s as bigint start with -9223372036854775805 minvalue -9223372036
4848
drop sequence s;
4949

5050
set global auto_increment_increment= default, auto_increment_offset= default;
51+
52+
--echo #
53+
--echo # MDEV-33836 Assertion `(ulonglong) next_free_value % real_increment == (ulonglong) offset' failed in void sequence_definition::adjust_values(longlong)
54+
--echo #
55+
56+
CREATE SEQUENCE s AS BIGINT UNSIGNED START WITH 9223372036854775800 INCREMENT 0;
57+
set @old_AUTO_INCREMENT_INCREMENT=@@global.AUTO_INCREMENT_INCREMENT;
58+
set global AUTO_INCREMENT_INCREMENT=10;
59+
SELECT NEXTVAL (s);
60+
FLUSH TABLES WITH READ LOCK;
61+
# ER_CANT_UPDATE_WITH_READLOCK when executed normally
62+
# ER_BAD_FIELD_ERROR when executed as a prepared statement
63+
--error ER_CANT_UPDATE_WITH_READLOCK,ER_BAD_FIELD_ERROR
64+
UPDATE s SET a=1;
65+
unlock tables;
66+
set global AUTO_INCREMENT_INCREMENT=@old_AUTO_INCREMENT_INCREMENT;
67+
drop sequence s;
5168
--enable_ps2_protocol
5269

5370
--echo #

mysql-test/suite/sql_sequence/setval.result

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,4 +418,15 @@ setval(s, 32767)
418418
select nextval(s);
419419
ERROR HY000: Sequence 'test.s' has run out
420420
drop sequence s;
421+
#
422+
# MDEV-33836 Assertion `(ulonglong) next_free_value % real_increment == (ulonglong) offset' failed in void sequence_definition::adjust_values(longlong)
423+
#
424+
CREATE SEQUENCE s AS BIGINT UNSIGNED START WITH 9223372036854775800 INCREMENT 0;
425+
set @old_AUTO_INCREMENT_INCREMENT=@@global.AUTO_INCREMENT_INCREMENT;
426+
set global AUTO_INCREMENT_INCREMENT=100;
427+
SELECT SETVAL (s,12345678901234567890);
428+
SETVAL (s,12345678901234567890)
429+
12345678901234567890
430+
drop sequence s;
431+
set global AUTO_INCREMENT_INCREMENT=@old_AUTO_INCREMENT_INCREMENT;
421432
# End of 11.5 tests

mysql-test/suite/sql_sequence/setval.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,15 @@ select nextval(s);
253253
drop sequence s;
254254
--enable_ps2_protocol
255255

256+
--echo #
257+
--echo # MDEV-33836 Assertion `(ulonglong) next_free_value % real_increment == (ulonglong) offset' failed in void sequence_definition::adjust_values(longlong)
258+
--echo #
259+
260+
CREATE SEQUENCE s AS BIGINT UNSIGNED START WITH 9223372036854775800 INCREMENT 0;
261+
set @old_AUTO_INCREMENT_INCREMENT=@@global.AUTO_INCREMENT_INCREMENT;
262+
set global AUTO_INCREMENT_INCREMENT=100;
263+
SELECT SETVAL (s,12345678901234567890);
264+
drop sequence s;
265+
set global AUTO_INCREMENT_INCREMENT=@old_AUTO_INCREMENT_INCREMENT;
266+
256267
--echo # End of 11.5 tests

sql/sql_sequence.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,10 @@ void sequence_definition::adjust_values(longlong next_value)
751751
752752
next_free_value % real_increment == offset
753753
*/
754-
off= next_free_value % real_increment;
754+
if (is_unsigned)
755+
off= (ulonglong) next_free_value % real_increment;
756+
else
757+
off= next_free_value % real_increment;
755758
if (off < 0)
756759
off+= real_increment;
757760
to_add= (real_increment + offset - off) % real_increment;

0 commit comments

Comments
 (0)