Skip to content

Commit 7aa80ba

Browse files
committed
Sequences with negative numbers and auto_increment_increment crashes
This also fixes MDEV-16313 Assertion `next_free_value % real_increment == offset' fails upon CREATE SEQUENCE in galera cluster Fixed by adding llabs() to assert. Also adjusted auto_increment_offset to mod auto_increment_increment.
1 parent ceb5597 commit 7aa80ba

File tree

5 files changed

+89
-2
lines changed

5 files changed

+89
-2
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
set global auto_increment_increment= 2, auto_increment_offset= 2;
2+
create sequence s start with -3 minvalue= -1000 increment 0;
3+
select nextval(s);
4+
nextval(s)
5+
-2
6+
select nextval(s);
7+
nextval(s)
8+
0
9+
flush tables;
10+
select nextval(s);
11+
nextval(s)
12+
1998
13+
drop sequence s;
14+
set global auto_increment_increment= 2, auto_increment_offset= 1;
15+
create sequence s start with -3 minvalue= -1000 increment 0;
16+
select nextval(s);
17+
nextval(s)
18+
-3
19+
select nextval(s);
20+
nextval(s)
21+
-1
22+
select nextval(s);
23+
nextval(s)
24+
1
25+
flush tables;
26+
select nextval(s);
27+
nextval(s)
28+
1997
29+
drop sequence s;
30+
set global auto_increment_increment= default, auto_increment_offset= default;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--source include/have_sequence.inc
2+
3+
#
4+
# tests with auto_increment_increment and auto_increment_offset
5+
#
6+
7+
set global auto_increment_increment= 2, auto_increment_offset= 2;
8+
9+
create sequence s start with -3 minvalue= -1000 increment 0;
10+
11+
select nextval(s);
12+
select nextval(s);
13+
flush tables;
14+
select nextval(s);
15+
drop sequence s;
16+
17+
set global auto_increment_increment= 2, auto_increment_offset= 1;
18+
19+
create sequence s start with -3 minvalue= -1000 increment 0;
20+
21+
select nextval(s);
22+
select nextval(s);
23+
select nextval(s);
24+
flush tables;
25+
select nextval(s);
26+
drop sequence s;
27+
28+
# Clean up
29+
30+
set global auto_increment_increment= default, auto_increment_offset= default;

mysql-test/suite/sql_sequence/next.result

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,3 +519,18 @@ create temporary table tmp (i int);
519519
select next value for tmp;
520520
ERROR 42S02: 'test.tmp' is not a SEQUENCE
521521
drop table tmp;
522+
#
523+
# Test negative numbers
524+
#
525+
create sequence s start with 1 minvalue=-1000 maxvalue=1000 increment -1;
526+
select next value for s;
527+
next value for s
528+
1
529+
select next value for s;
530+
next value for s
531+
0
532+
flush tables;
533+
select next value for s;
534+
next value for s
535+
-999
536+
drop sequence s;

mysql-test/suite/sql_sequence/next.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,14 @@ create temporary table tmp (i int);
269269
--error ER_NOT_SEQUENCE
270270
select next value for tmp;
271271
drop table tmp;
272+
273+
--echo #
274+
--echo # Test negative numbers
275+
--echo #
276+
277+
create sequence s start with 1 minvalue=-1000 maxvalue=1000 increment -1;
278+
select next value for s;
279+
select next value for s;
280+
flush tables;
281+
select next value for s;
282+
drop sequence s;

sql/sql_sequence.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,8 @@ void sequence_definition::adjust_values(longlong next_value)
542542

543543
if ((real_increment= global_system_variables.auto_increment_increment)
544544
!= 1)
545-
offset= global_system_variables.auto_increment_offset;
545+
offset= (global_system_variables.auto_increment_offset %
546+
global_system_variables.auto_increment_increment);
546547

547548
/*
548549
Ensure that next_free_value has the right offset, so that we
@@ -564,7 +565,7 @@ void sequence_definition::adjust_values(longlong next_value)
564565
else
565566
{
566567
next_free_value+= to_add;
567-
DBUG_ASSERT(next_free_value % real_increment == offset);
568+
DBUG_ASSERT(llabs(next_free_value % real_increment) == offset);
568569
}
569570
}
570571
}

0 commit comments

Comments
 (0)