Skip to content

Commit 6c3ec54

Browse files
AliSQLAliSQL
authored andcommitted
[Feature] Issue#49 Sequence support currval
Description ----------- 'select currval for Seq' will return the last queried nextval, It was stored into the current session context, and roll up when query nextval.
1 parent c258646 commit 6c3ec54

19 files changed

Lines changed: 505 additions & 126 deletions

include/my_base.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,9 @@ is the global server default. */
491491
#define HA_ERR_FTS_TOO_MANY_WORDS_IN_PHRASE 191 /* Too many words in a phrase */
492492
#define HA_ERR_SEQUENCE_RUN_OUT 192 /* Sequence has been run out */
493493
#define HA_ERR_SEQUENCE_INVALID 193 /* Sequence structure or number is invalid.*/
494-
#define HA_ERR_SEQUENCE_ACCESS_ERROR 194 /* Sequence access error */
495-
#define HA_ERR_LAST 194 /* Copy of last error nr */
494+
#define HA_ERR_SEQUENCE_NOT_DEFINED 194 /* Sequence is not yet defined in this session. */
495+
#define HA_ERR_SEQUENCE_ACCESS_ERROR 195 /* Sequence access error */
496+
#define HA_ERR_LAST 195 /* Copy of last error nr */
496497

497498
/* Number of different errors */
498499
#define HA_ERR_ERRORS (HA_ERR_LAST - HA_ERR_FIRST + 1)

mysql-test/r/feature_sequence.result

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,25 @@ insert into s2 values(0, 0, 1, 10, 1, 2, 1, 1, 0);
8383
commit;
8484
select * for s2;
8585
currval nextval minvalue maxvalue start increment cache cycle round
86-
0 1 1 10 1 2 1 1 0
86+
1 1 1 10 1 2 1 1 0
8787
select * for s2;
8888
currval nextval minvalue maxvalue start increment cache cycle round
89-
0 3 1 10 1 2 1 1 0
89+
3 3 1 10 1 2 1 1 0
9090
select * for s2;
9191
currval nextval minvalue maxvalue start increment cache cycle round
92-
0 5 1 10 1 2 1 1 0
92+
5 5 1 10 1 2 1 1 0
9393
select * for s2;
9494
currval nextval minvalue maxvalue start increment cache cycle round
95-
0 7 1 10 1 2 1 1 0
95+
7 7 1 10 1 2 1 1 0
9696
select * for s2;
9797
currval nextval minvalue maxvalue start increment cache cycle round
98-
0 9 1 10 1 2 1 1 0
98+
9 9 1 10 1 2 1 1 0
9999
select * for s2;
100100
currval nextval minvalue maxvalue start increment cache cycle round
101-
0 1 1 10 1 2 1 1 1
101+
1 1 1 10 1 2 1 1 1
102102
select * for s2;
103103
currval nextval minvalue maxvalue start increment cache cycle round
104-
0 3 1 10 1 2 1 1 1
104+
3 3 1 10 1 2 1 1 1
105105
select * from s2;
106106
currval nextval minvalue maxvalue start increment cache cycle round
107107
0 5 1 10 1 2 1 1 1
@@ -184,7 +184,7 @@ insert into t2 select nextval for s2;
184184
commit;
185185
select * for s2;
186186
currval nextval minvalue maxvalue start increment cache cycle round
187-
0 2 1 9223372036854775807 1 1 10000 0 0
187+
2 2 1 9223372036854775807 1 1 10000 0 0
188188
select * for t2;
189189
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
190190
select * from s2, t2;
@@ -203,7 +203,7 @@ ERROR HY000: Table storage engine 'sequence' does not support the create option
203203
rename table s2 to s2_1;
204204
select * for s2_1;
205205
currval nextval minvalue maxvalue start increment cache cycle round
206-
0 1 1 9223372036854775807 1 1 10000 0 0
206+
1 1 1 9223372036854775807 1 1 10000 0 0
207207
truncate table s2_1;
208208
ERROR HY000: Table storage engine for 's2_1' doesn't have this option
209209
rename table s2_1 to s2;
@@ -297,13 +297,13 @@ nextval
297297
set session sequence_read_skip_cache=false;
298298
select * for s1;
299299
currval nextval minvalue maxvalue start increment cache cycle round
300-
0 1 1 9223372036854775807 1 1 10000 0 0
300+
1 1 1 9223372036854775807 1 1 10000 0 0
301301
select nextval for s1;
302302
nextval
303303
2
304304
select * for s1;
305305
currval nextval minvalue maxvalue start increment cache cycle round
306-
0 3 1 9223372036854775807 1 1 10000 0 0
306+
3 3 1 9223372036854775807 1 1 10000 0 0
307307
select nextval for s1;
308308
nextval
309309
4
@@ -327,7 +327,7 @@ priv test
327327
create sequence s_db.s1;
328328
select * for s_db.s1;
329329
currval nextval minvalue maxvalue start increment cache cycle round
330-
0 1 1 9223372036854775807 1 1 10000 0 0
330+
1 1 1 9223372036854775807 1 1 10000 0 0
331331
create sequence s_db.s2;
332332
drop sequence s_db.s2;
333333
select * for s_db.s1;
@@ -348,9 +348,9 @@ insert into t select nextval for s_t;
348348
insert into t select nextval for s_t;
349349
insert into t select nextval for s_t;
350350
insert into t select nextval for s_t;
351-
ERROR HY000: Sequence 's_db.s_t' has been run out.
351+
ERROR HY000: Sequence 's_db.s_t' has run out.
352352
insert into t select nextval for s_t;
353-
ERROR HY000: Sequence 's_db.s_t' has been run out.
353+
ERROR HY000: Sequence 's_db.s_t' has run out.
354354
commit;
355355
select * from t;
356356
id
@@ -381,7 +381,7 @@ Variable_name Value
381381
read_only OFF
382382
select * for s_db.s1;
383383
currval nextval minvalue maxvalue start increment cache cycle round
384-
0 1 1 9223372036854775807 1 1 10000 0 0
384+
1 1 1 9223372036854775807 1 1 10000 0 0
385385
show global variables like 'read_only';
386386
Variable_name Value
387387
read_only ON
@@ -423,7 +423,7 @@ currval nextval minvalue maxvalue start increment cache cycle round
423423
set session sequence_read_skip_cache=off;
424424
select * for s_t;
425425
currval nextval minvalue maxvalue start increment cache cycle round
426-
0 11 1 20 1 1 5 1 0
426+
11 11 1 20 1 1 5 1 0
427427
select * from s_t;
428428
currval nextval minvalue maxvalue start increment cache cycle round
429429
0 17 1 20 1 1 5 1 0
@@ -702,7 +702,7 @@ ERROR HY000: Sequence requires binlog_format= row
702702
set session binlog_format=row;
703703
select * for s1;
704704
currval nextval minvalue maxvalue start increment cache cycle round
705-
0 1 1 9223372036854775807 1 1 2 0 0
705+
1 1 1 9223372036854775807 1 1 2 0 0
706706
use s_db;
707707
select * from s1;
708708
currval nextval minvalue maxvalue start increment cache cycle round
@@ -724,7 +724,7 @@ ERROR HY000: Sequence requires binlog_format= row
724724
set session binlog_format=row;
725725
select * for s1;
726726
currval nextval minvalue maxvalue start increment cache cycle round
727-
0 1 1 9223372036854775807 1 1 2 0 0
727+
1 1 1 9223372036854775807 1 1 2 0 0
728728
use s_db;
729729
select * from s1;
730730
currval nextval minvalue maxvalue start increment cache cycle round
@@ -771,7 +771,7 @@ create sequence s1 cache 2;
771771
create table t as select * for s1;
772772
select * from t;
773773
currval nextval minvalue maxvalue start increment cache cycle round
774-
0 1 1 9223372036854775807 1 1 2 0 0
774+
1 1 1 9223372036854775807 1 1 2 0 0
775775
drop sequence s1;
776776
drop table t;
777777
###########################################
@@ -903,19 +903,19 @@ round increment by round
903903
create sequence s1 start with 5 minvalue 2 maxvalue 7 cache 1 cycle;
904904
select * for s1;
905905
currval nextval minvalue maxvalue start increment cache cycle round
906-
0 5 2 7 5 1 1 1 0
906+
5 5 2 7 5 1 1 1 0
907907
select * for s1;
908908
currval nextval minvalue maxvalue start increment cache cycle round
909-
0 6 2 7 5 1 1 1 0
909+
6 6 2 7 5 1 1 1 0
910910
select * for s1;
911911
currval nextval minvalue maxvalue start increment cache cycle round
912-
0 7 2 7 5 1 1 1 0
912+
7 7 2 7 5 1 1 1 0
913913
select * for s1;
914914
currval nextval minvalue maxvalue start increment cache cycle round
915-
0 2 2 7 5 1 1 1 1
915+
2 2 2 7 5 1 1 1 1
916916
select * for s1;
917917
currval nextval minvalue maxvalue start increment cache cycle round
918-
0 3 2 7 5 1 1 1 1
918+
3 3 2 7 5 1 1 1 1
919919
drop sequence s1;
920920
create sequence s1 start with 5 minvalue 2 maxvalue 7 cache 10 nocycle;
921921
select nextval, round for s1;
@@ -928,31 +928,31 @@ select nextval, round for s1;
928928
nextval round
929929
7 0
930930
select nextval, round for s1;
931-
ERROR HY000: Sequence 's_db.s1' has been run out.
931+
ERROR HY000: Sequence 's_db.s1' has run out.
932932
drop sequence s1;
933933
create sequence s1 start with 2 minvalue 1 maxvalue 3 increment by 3 nocache cycle;
934934
select * for s1;
935935
currval nextval minvalue maxvalue start increment cache cycle round
936-
0 2 1 3 2 3 0 1 0
936+
2 2 1 3 2 3 0 1 0
937937
select * for s1;
938938
currval nextval minvalue maxvalue start increment cache cycle round
939-
0 1 1 3 2 3 0 1 1
939+
1 1 1 3 2 3 0 1 1
940940
select * for s1;
941941
currval nextval minvalue maxvalue start increment cache cycle round
942-
0 1 1 3 2 3 0 1 2
942+
1 1 1 3 2 3 0 1 2
943943
select * for s1;
944944
currval nextval minvalue maxvalue start increment cache cycle round
945-
0 1 1 3 2 3 0 1 3
945+
1 1 1 3 2 3 0 1 3
946946
select * for s1;
947947
currval nextval minvalue maxvalue start increment cache cycle round
948-
0 1 1 3 2 3 0 1 4
948+
1 1 1 3 2 3 0 1 4
949949
drop sequence s1;
950950
create sequence s1 start with 2 minvalue 1 maxvalue 3 increment by 3 cache 2 nocycle;
951951
select * for s1;
952952
currval nextval minvalue maxvalue start increment cache cycle round
953-
0 2 1 3 2 3 2 0 0
953+
2 2 1 3 2 3 2 0 0
954954
select * for s1;
955-
ERROR HY000: Sequence 's_db.s1' has been run out.
955+
ERROR HY000: Sequence 's_db.s1' has run out.
956956
drop sequence s1;
957957
------------------------------------------
958958
beyond ulonglong maxvalue
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
include/master-slave.inc
2+
Warnings:
3+
Note #### Sending passwords in plain text without SSL/TLS is extremely insecure.
4+
Note #### Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
5+
[connection master]
6+
create database s_db;
7+
grant all on s_db.* to normal_1@'%' identified by 'pass';
8+
grant all on test.* to normal_2@'%' identified by 'pass';
9+
grant all on s_db.* to normal_3@'%' identified by 'pass';
10+
grant all on test.* to normal_4@'%' identified by 'pass';
11+
set global read_only=on;
12+
###########################################
13+
master and slave sync sequence.
14+
###########################################
15+
use s_db;
16+
create sequence s1;
17+
show create table s1;
18+
Table Create Table
19+
s1 CREATE SEQUENCE `s1` (
20+
`currval` bigint(21) NOT NULL COMMENT 'current value',
21+
`nextval` bigint(21) NOT NULL COMMENT 'next value',
22+
`minvalue` bigint(21) NOT NULL COMMENT 'min value',
23+
`maxvalue` bigint(21) NOT NULL COMMENT 'max value',
24+
`start` bigint(21) NOT NULL COMMENT 'start value',
25+
`increment` bigint(21) NOT NULL COMMENT 'increment value',
26+
`cache` bigint(21) NOT NULL COMMENT 'cache size',
27+
`cycle` bigint(21) NOT NULL COMMENT 'cycle state',
28+
`round` bigint(21) NOT NULL COMMENT 'already how many round'
29+
) ENGINE=InnoDB DEFAULT CHARSET=latin1
30+
use s_db;
31+
show create table s1;
32+
Table Create Table
33+
s1 CREATE SEQUENCE `s1` (
34+
`currval` bigint(21) NOT NULL COMMENT 'current value',
35+
`nextval` bigint(21) NOT NULL COMMENT 'next value',
36+
`minvalue` bigint(21) NOT NULL COMMENT 'min value',
37+
`maxvalue` bigint(21) NOT NULL COMMENT 'max value',
38+
`start` bigint(21) NOT NULL COMMENT 'start value',
39+
`increment` bigint(21) NOT NULL COMMENT 'increment value',
40+
`cache` bigint(21) NOT NULL COMMENT 'cache size',
41+
`cycle` bigint(21) NOT NULL COMMENT 'cycle state',
42+
`round` bigint(21) NOT NULL COMMENT 'already how many round'
43+
) ENGINE=InnoDB DEFAULT CHARSET=latin1
44+
use s_db;
45+
drop sequence s1;
46+
###########################################
47+
Session context currval
48+
###########################################
49+
use s_db;
50+
create sequence s1 start with 1 minvalue 1 maxvalue 7 cache 2 cycle increment by 2;
51+
select currval for s1;
52+
ERROR HY000: Sequence 's_db.s1' is not yet defined in this session
53+
select currval from s1;
54+
currval
55+
0
56+
select currval, start, minvalue, cache, cycle, increment, round for s1;
57+
ERROR HY000: Sequence 's_db.s1' is not yet defined in this session
58+
select currval, start, minvalue, cache, cycle, increment, round from s1;
59+
currval start minvalue cache cycle increment round
60+
0 1 1 2 1 2 0
61+
select nextval for s1;
62+
nextval
63+
1
64+
select currval for s1;
65+
currval
66+
1
67+
select nextval for s1;
68+
nextval
69+
3
70+
select currval for s1;
71+
currval
72+
3
73+
select nextval for s1;
74+
nextval
75+
5
76+
select currval for s1;
77+
currval
78+
5
79+
select nextval for s1;
80+
nextval
81+
7
82+
select currval for s1;
83+
currval
84+
7
85+
select nextval for s1;
86+
nextval
87+
1
88+
select currval for s1;
89+
currval
90+
1
91+
select nextval for s1;
92+
nextval
93+
3
94+
select currval for s1;
95+
currval
96+
3
97+
drop sequence s1;
98+
create sequence s1;
99+
select currval for s1;
100+
ERROR HY000: Sequence 's_db.s1' is not yet defined in this session
101+
select * from s1;
102+
currval nextval minvalue maxvalue start increment cache cycle round
103+
0 0 1 9223372036854775807 1 1 10000 0 0
104+
use s_db;
105+
drop database s_db;
106+
drop user normal_1@'%';
107+
drop user normal_2@'%';
108+
drop user normal_3@'%';
109+
drop user normal_4@'%';
110+
include/rpl_end.inc

mysql-test/r/feature_sequence_gtid.result

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,13 @@ nextval
150150
set session sequence_read_skip_cache=false;
151151
select * for s1;
152152
currval nextval minvalue maxvalue start increment cache cycle round
153-
0 1 1 9223372036854775807 1 1 10000 0 0
153+
1 1 1 9223372036854775807 1 1 10000 0 0
154154
select nextval for s1;
155155
nextval
156156
2
157157
select * for s1;
158158
currval nextval minvalue maxvalue start increment cache cycle round
159-
0 3 1 9223372036854775807 1 1 10000 0 0
159+
3 3 1 9223372036854775807 1 1 10000 0 0
160160
select nextval for s1;
161161
nextval
162162
4
@@ -180,7 +180,7 @@ priv test
180180
create sequence s_db.s1;
181181
select * for s_db.s1;
182182
currval nextval minvalue maxvalue start increment cache cycle round
183-
0 1 1 9223372036854775807 1 1 10000 0 0
183+
1 1 1 9223372036854775807 1 1 10000 0 0
184184
create sequence s_db.s2;
185185
drop sequence s_db.s2;
186186
select * for s_db.s1;
@@ -201,9 +201,9 @@ insert into t select nextval for s_t;
201201
insert into t select nextval for s_t;
202202
insert into t select nextval for s_t;
203203
insert into t select nextval for s_t;
204-
ERROR HY000: Sequence 's_db.s_t' has been run out.
204+
ERROR HY000: Sequence 's_db.s_t' has run out.
205205
insert into t select nextval for s_t;
206-
ERROR HY000: Sequence 's_db.s_t' has been run out.
206+
ERROR HY000: Sequence 's_db.s_t' has run out.
207207
commit;
208208
select * from t;
209209
id
@@ -234,7 +234,7 @@ Variable_name Value
234234
read_only OFF
235235
select * for s_db.s1;
236236
currval nextval minvalue maxvalue start increment cache cycle round
237-
0 1 1 9223372036854775807 1 1 10000 0 0
237+
1 1 1 9223372036854775807 1 1 10000 0 0
238238
show global variables like 'read_only';
239239
Variable_name Value
240240
read_only ON
@@ -276,7 +276,7 @@ currval nextval minvalue maxvalue start increment cache cycle round
276276
set session sequence_read_skip_cache=off;
277277
select * for s_t;
278278
currval nextval minvalue maxvalue start increment cache cycle round
279-
0 11 1 20 1 1 5 1 0
279+
11 11 1 20 1 1 5 1 0
280280
select * from s_t;
281281
currval nextval minvalue maxvalue start increment cache cycle round
282282
0 17 1 20 1 1 5 1 0
@@ -555,7 +555,7 @@ ERROR HY000: Sequence requires binlog_format= row
555555
set session binlog_format=row;
556556
select * for s1;
557557
currval nextval minvalue maxvalue start increment cache cycle round
558-
0 1 1 9223372036854775807 1 1 2 0 0
558+
1 1 1 9223372036854775807 1 1 2 0 0
559559
use s_db;
560560
select * from s1;
561561
currval nextval minvalue maxvalue start increment cache cycle round

mysql-test/r/feature_sequence_read_only.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ create sequence s1 cache 2;
3232
ERROR HY000: Sequence '(null).(null)' access error.
3333
select * for s;
3434
currval nextval minvalue maxvalue start increment cache cycle round
35-
0 1 1 9223372036854775807 1 1 2 0 0
35+
1 1 1 9223372036854775807 1 1 2 0 0
3636
flush tables;
3737
select * for s;
3838
ERROR HY000: Sequence '(null).(null)' access error.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--binlog_format=row --query_cache_type=1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--binlog_format=row --query_cache_type=1 --read_only=true

0 commit comments

Comments
 (0)