forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequence_test.go
1148 lines (1000 loc) · 53 KB
/
sequence_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ddl_test
import (
"strconv"
"testing"
"time"
mysql "github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/parser/auth"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/testkit/external"
"github.com/pingcap/tidb/util/dbterror"
"github.com/stretchr/testify/require"
)
func TestCreateSequence(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
session.SetSchemaLease(600 * time.Millisecond)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop sequence if exists seq")
tk.MustGetErrCode("create sequence `seq `", mysql.ErrWrongTableName)
// increment should not be set as 0.
tk.MustGetErrCode("create sequence seq increment 0", mysql.ErrSequenceInvalidData)
// maxvalue should be larger than minvalue.
tk.MustGetErrCode("create sequence seq maxvalue 1 minvalue 2", mysql.ErrSequenceInvalidData)
// maxvalue should be larger than minvalue.
tk.MustGetErrCode("create sequence seq maxvalue 1 minvalue 1", mysql.ErrSequenceInvalidData)
// maxvalue shouldn't be equal to MaxInt64.
tk.MustGetErrCode("create sequence seq maxvalue 9223372036854775807 minvalue 1", mysql.ErrSequenceInvalidData)
// TODO : minvalue shouldn't be equal to MinInt64.
// maxvalue should be larger than start.
tk.MustGetErrCode("create sequence seq maxvalue 1 start with 2", mysql.ErrSequenceInvalidData)
// cacheVal should be less than (math.MaxInt64-maxIncrement)/maxIncrement.
tk.MustGetErrCode("create sequence seq increment 100000 cache 922337203685477", mysql.ErrSequenceInvalidData)
// test unsupported table option in sequence.
tk.MustGetErrCode("create sequence seq CHARSET=utf8", mysql.ErrSequenceUnsupportedTableOption)
_, err := tk.Exec("create sequence seq comment=\"test\"")
require.NoError(t, err)
sequenceTable := external.GetTableByName(t, tk, "test", "seq")
require.Equal(t, true, sequenceTable.Meta().IsSequence())
require.Equal(t, model.DefaultSequenceIncrementValue, sequenceTable.Meta().Sequence.Increment)
require.Equal(t, model.DefaultPositiveSequenceStartValue, sequenceTable.Meta().Sequence.Start)
require.Equal(t, model.DefaultPositiveSequenceMinValue, sequenceTable.Meta().Sequence.MinValue)
require.Equal(t, model.DefaultPositiveSequenceMaxValue, sequenceTable.Meta().Sequence.MaxValue)
require.Equal(t, true, sequenceTable.Meta().Sequence.Cache)
require.Equal(t, model.DefaultSequenceCacheValue, sequenceTable.Meta().Sequence.CacheValue)
require.Equal(t, false, sequenceTable.Meta().Sequence.Cycle)
// Test create privilege.
tk.MustExec("drop user if exists myuser@localhost")
tk.MustExec("create user myuser@localhost")
tk1 := testkit.NewTestKit(t, store)
se, err := session.CreateSession4Test(store)
require.NoError(t, err)
require.True(t, se.Auth(&auth.UserIdentity{Username: "myuser", Hostname: "localhost"}, nil, nil))
tk1.SetSession(se)
// grant the myuser the access to database test.
tk.MustExec("grant select on test.* to 'myuser'@'localhost'")
tk1.MustExec("use test")
_, err = tk1.Exec("create sequence my_seq")
require.Error(t, err)
require.EqualError(t, err, "[planner:1142]CREATE command denied to user 'myuser'@'localhost' for table 'my_seq'")
}
// Test for sequence still works with a infoschema attached by temporary table
func TestIssue28881(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
session.SetSchemaLease(600 * time.Millisecond)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop sequence if exists s")
tk.MustExec("create sequence s")
defer tk.MustExec("drop sequence s")
tk.MustExec("create temporary table tmp1 (id int)")
tk.MustQuery("select nextval(s)").Check(testkit.Rows("1"))
tk.MustQuery("select lastval(s)").Check(testkit.Rows("1"))
}
func TestDropSequence(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
session.SetSchemaLease(600 * time.Millisecond)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop sequence if exists seq")
// Test sequence is unknown.
tk.MustGetErrCode("drop sequence seq", mysql.ErrUnknownSequence)
// Test non-existed sequence can't drop successfully.
tk.MustExec("create sequence seq")
_, err := tk.Exec("drop sequence seq, seq2")
require.Error(t, err)
require.EqualError(t, err, "[schema:4139]Unknown SEQUENCE: 'test.seq2'")
// Test the specified object is not sequence.
tk.MustExec("create table seq3 (a int)")
err = tk.ExecToErr("drop sequence seq3")
require.Error(t, err)
require.True(t, terror.ErrorEqual(err, dbterror.ErrWrongObject))
// Test schema is not exist.
err = tk.ExecToErr("drop sequence unknown.seq")
require.Error(t, err)
require.EqualError(t, err, "[schema:4139]Unknown SEQUENCE: 'unknown.seq'")
// Test drop sequence successfully.
tk.MustExec("create sequence seq")
tk.MustExec("drop sequence seq")
err = tk.ExecToErr("drop sequence seq")
require.Error(t, err)
require.EqualError(t, err, "[schema:4139]Unknown SEQUENCE: 'test.seq'")
// Test drop table when the object is a sequence.
tk.MustExec("create sequence seq")
err = tk.ExecToErr("drop table seq")
require.Error(t, err)
require.EqualError(t, err, "[schema:1051]Unknown table 'test.seq'")
// Test drop view when the object is a sequence.
err = tk.ExecToErr("drop view seq")
require.Error(t, err)
require.True(t, terror.ErrorEqual(err, dbterror.ErrWrongObject))
tk.MustExec("drop sequence seq")
// Test drop privilege.
tk.MustExec("drop user if exists myuser@localhost")
tk.MustExec("create user myuser@localhost")
tk1 := testkit.NewTestKit(t, store)
se, err := session.CreateSession4Test(store)
require.NoError(t, err)
require.True(t, se.Auth(&auth.UserIdentity{Username: "myuser", Hostname: "localhost"}, nil, nil))
tk1.SetSession(se)
// grant the myuser the access to database test.
tk.MustExec("create sequence my_seq")
tk.MustExec("grant select on test.* to 'myuser'@'localhost'")
tk1.MustExec("use test")
_, err = tk1.Exec("drop sequence my_seq")
require.Error(t, err)
require.EqualError(t, err, "[planner:1142]DROP command denied to user 'myuser'@'localhost' for table 'my_seq'")
// Test for `drop sequence if exists`.
tk.MustExec("drop sequence if exists seq_if_exists")
tk.MustQuery("show warnings;").Check(testkit.Rows("Note 4139 Unknown SEQUENCE: 'test.seq_if_exists'"))
}
func TestShowCreateSequence(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
session.SetSchemaLease(600 * time.Millisecond)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create table t(a int)")
tk.MustExec("create sequence seq")
// Test show privilege.
tk.MustExec("drop user if exists myuser@localhost")
tk.MustExec("create user myuser@localhost")
tk1 := testkit.NewTestKit(t, store)
se, err := session.CreateSession4Test(store)
require.NoError(t, err)
require.True(t, se.Auth(&auth.UserIdentity{Username: "myuser", Hostname: "localhost"}, nil, nil))
tk1.SetSession(se)
// Grant the myuser the access to table t in database test, but sequence seq.
tk.MustExec("grant select on test.t to 'myuser'@'localhost'")
tk1.MustExec("use test")
tk1.MustExec("show create table t")
_, err = tk1.Exec("show create sequence seq")
require.Error(t, err)
require.EqualError(t, err, "[planner:1142]SHOW command denied to user 'myuser'@'localhost' for table 'seq'")
// Grant the myuser the access to sequence seq in database test.
tk.MustExec("grant select on test.seq to 'myuser'@'localhost'")
tk1.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB"))
// Test show sequence detail.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq")
tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB"))
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq start 10")
tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 10 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB"))
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq minvalue 0")
tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 0 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB"))
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq maxvalue 100")
tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 100 increment by 1 cache 1000 nocycle ENGINE=InnoDB"))
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment = -2")
tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with -1 minvalue -9223372036854775807 maxvalue -1 increment by -2 cache 1000 nocycle ENGINE=InnoDB"))
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq nocache")
tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 nocache nocycle ENGINE=InnoDB"))
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq cycle")
tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 cycle ENGINE=InnoDB"))
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq comment=\"ccc\"")
tk.MustQuery("show create sequence seq").Check(testkit.Rows("seq CREATE SEQUENCE `seq` start with 1 minvalue 1 maxvalue 9223372036854775806 increment by 1 cache 1000 nocycle ENGINE=InnoDB COMMENT='ccc'"))
// Test show create sequence with a normal table.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create table seq (a int)")
err = tk.QueryToErr("show create sequence seq")
require.Error(t, err)
require.EqualError(t, err, "[executor:1347]'test.seq' is not SEQUENCE")
tk.MustExec("drop table if exists seq")
// Test use the show create sequence result to create sequence.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq")
showString := tk.MustQuery("show create sequence seq").Rows()[0][1].(string)
tk.MustExec("drop sequence if exists seq")
tk.MustExec(showString)
}
func TestSequenceAsDefaultValue(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
session.SetSchemaLease(600 * time.Millisecond)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq")
// test the use sequence's nextval as default.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int not null default next value for seq key)")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int not null default nextval(seq), b int, primary key(a))")
tk.MustExec("create table t1 (a int default next value for seq)")
tk.MustGetErrMsg("create table t2 (a char(1) default next value for seq)", "[ddl:8228]Unsupported sequence default value for column type 'a'")
tk.MustExec("create table t3 (a int default nextval(seq))")
tk.MustExec("create table t4 (a int)")
tk.MustExec("alter table t4 alter column a set default (next value for seq)")
tk.MustExec("alter table t4 alter column a set default (nextval(seq))")
tk.MustExec("create table t5 (a char(1))")
tk.MustGetErrMsg("alter table t5 alter column a set default (next value for seq)", "[ddl:8228]Unsupported sequence default value for column type 'a'")
tk.MustGetErrMsg("alter table t5 alter column a set default (nextval(seq))", "[ddl:8228]Unsupported sequence default value for column type 'a'")
// Specially, the new added column with sequence as it's default value is forbade.
// But alter table column with sequence as it's default value is allowed.
tk.MustGetErrMsg("alter table t5 add column c int default next value for seq", "[ddl:8230]Unsupported using sequence as default value in add column 'c'")
tk.MustExec("alter table t5 add column c int default -1")
// Alter with modify.
tk.MustExec("alter table t5 modify column c int default next value for seq")
// Alter with alter.
tk.MustExec("alter table t5 alter column c set default (next value for seq)")
// Alter with change.
tk.MustExec("alter table t5 change column c c int default next value for seq")
}
func TestSequenceFunction(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
session.SetSchemaLease(600 * time.Millisecond)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop sequence if exists seq")
tk.MustExec("drop sequence if exists seq1")
tk.MustExec("create sequence seq")
// test normal sequence function.
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1"))
tk.MustQuery("select nextval(test.seq)").Check(testkit.Rows("2"))
tk.MustQuery("select next value for seq").Check(testkit.Rows("3"))
tk.MustQuery("select next value for test.seq").Check(testkit.Rows("4"))
// test sequence function error.
tk.MustGetErrMsg("select nextval(seq1)", "[schema:1146]Table 'test.seq1' doesn't exist")
tk.MustExec("create database test2")
tk.MustExec("use test2")
tk.MustQuery("select nextval(test.seq)").Check(testkit.Rows("5"))
tk.MustQuery("select next value for test.seq").Check(testkit.Rows("6"))
tk.MustGetErrMsg("select nextval(seq)", "[schema:1146]Table 'test2.seq' doesn't exist")
tk.MustGetErrMsg("select next value for seq", "[schema:1146]Table 'test2.seq' doesn't exist")
tk.MustExec("use test")
// test sequence nocache.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq nocache")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("2"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3"))
// test sequence option logic.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment = 5")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("6"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("11"))
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment = 5 start = 3")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("8"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("13"))
// minvalue should be specified lower than start (negative here), default 1 when increment > 0.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq minvalue -5 start = -2 increment = 5")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-2"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("8"))
// test sequence cycle.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment = 5 start = 3 maxvalue = 12 cycle")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("8"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("6"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("11"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1"))
// test sequence maxvalue allocation.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment = 4 start = 2 maxvalue = 10 cycle")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("2"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("6"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("10"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("5"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("9"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1"))
// test sequence has run out.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment = 5 start = 3 maxvalue = 12 nocycle")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("8"))
err := tk.QueryToErr("select nextval(seq)")
require.EqualError(t, err, "[table:4135]Sequence 'test.seq' has run out")
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment = 3 start = 3 maxvalue = 9 nocycle")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("6"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("9"))
err = tk.QueryToErr("select nextval(seq)")
require.EqualError(t, err, "[table:4135]Sequence 'test.seq' has run out")
// test negative-growth sequence
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment = -2 start = 3 minvalue -5 maxvalue = 12 cycle")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-1"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-3"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-5"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("12"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("10"))
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment = -3 start = 2 minvalue -6 maxvalue = 11 cycle")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("2"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-1"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-4"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("11"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("8"))
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment = -4 start = 6 minvalue -6 maxvalue = 11")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("6"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("2"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-2"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-6"))
err = tk.QueryToErr("select nextval(seq)")
require.EqualError(t, err, "[table:4135]Sequence 'test.seq' has run out")
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment = -3 start = 2 minvalue -2 maxvalue 10")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("2"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-1"))
err = tk.QueryToErr("select nextval(seq)")
require.EqualError(t, err, "[table:4135]Sequence 'test.seq' has run out")
// test sequence setval function.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("2"))
// set value to a used value, will get NULL.
tk.MustQuery("select setval(seq, 2)").Check(testkit.Rows("<nil>"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3"))
// set value to a unused value, will get itself.
tk.MustQuery("select setval(seq, 5)").Check(testkit.Rows("5"))
// the next value will not be base on next value.
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("6"))
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment 3 maxvalue 11")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("4"))
tk.MustQuery("select setval(seq, 3)").Check(testkit.Rows("<nil>"))
tk.MustQuery("select setval(seq, 4)").Check(testkit.Rows("<nil>"))
tk.MustQuery("select setval(seq, 5)").Check(testkit.Rows("5"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("7"))
tk.MustQuery("select setval(seq, 8)").Check(testkit.Rows("8"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("10"))
err = tk.QueryToErr("select nextval(seq)")
require.EqualError(t, err, "[table:4135]Sequence 'test.seq' has run out")
tk.MustQuery("select setval(seq, 11)").Check(testkit.Rows("11"))
err = tk.QueryToErr("select nextval(seq)")
require.EqualError(t, err, "[table:4135]Sequence 'test.seq' has run out")
// set value can be bigger than maxvalue.
tk.MustQuery("select setval(seq, 100)").Check(testkit.Rows("100"))
err = tk.QueryToErr("select nextval(seq)")
require.EqualError(t, err, "[table:4135]Sequence 'test.seq' has run out")
// test setval in second cache round.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment 10 start 5 maxvalue 100 cache 10 cycle")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("5"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("15"))
tk.MustQuery("select setval(seq, 20)").Check(testkit.Rows("20"))
// the next value will not be base on next value.
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("25"))
sequenceTable := external.GetTableByName(t, tk, "test", "seq")
tc, ok := sequenceTable.(*tables.TableCommon)
require.Equal(t, true, ok)
_, end, round := tc.GetSequenceCommon().GetSequenceBaseEndRound()
require.Equal(t, int64(95), end)
require.Equal(t, int64(0), round)
// exhausted the sequence first round in cycle.
tk.MustQuery("select setval(seq, 95)").Check(testkit.Rows("95"))
// make sequence alloc the next batch.
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1"))
sequenceTable = external.GetTableByName(t, tk, "test", "seq")
tc, ok = sequenceTable.(*tables.TableCommon)
require.Equal(t, true, ok)
_, end, round = tc.GetSequenceCommon().GetSequenceBaseEndRound()
require.Equal(t, int64(91), end)
require.Equal(t, int64(1), round)
tk.MustQuery("select setval(seq, 15)").Check(testkit.Rows("15"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("21"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("31"))
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment 2 start 0 maxvalue 10 minvalue -10 cache 3 cycle")
tk.MustQuery("select setval(seq, -20)").Check(testkit.Rows("<nil>"))
tk.MustQuery("select setval(seq, 20)").Check(testkit.Rows("20"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-10"))
sequenceTable = external.GetTableByName(t, tk, "test", "seq")
tc, ok = sequenceTable.(*tables.TableCommon)
require.Equal(t, true, ok)
_, end, round = tc.GetSequenceCommon().GetSequenceBaseEndRound()
require.Equal(t, int64(-6), end)
require.Equal(t, int64(1), round)
// test setval in negative-growth sequence.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment -3 start 5 maxvalue 10 minvalue -10 cache 3 cycle")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("5"))
sequenceTable = external.GetTableByName(t, tk, "test", "seq")
tc, ok = sequenceTable.(*tables.TableCommon)
require.Equal(t, true, ok)
_, end, round = tc.GetSequenceCommon().GetSequenceBaseEndRound()
require.Equal(t, int64(-1), end)
require.Equal(t, int64(0), round)
// exhausted the sequence first cache batch.
tk.MustQuery("select setval(seq, -2)").Check(testkit.Rows("-2"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-4"))
sequenceTable = external.GetTableByName(t, tk, "test", "seq")
tc, ok = sequenceTable.(*tables.TableCommon)
require.Equal(t, true, ok)
_, end, round = tc.GetSequenceCommon().GetSequenceBaseEndRound()
require.Equal(t, int64(-10), end)
require.Equal(t, int64(0), round)
// exhausted the sequence second cache batch.
tk.MustQuery("select setval(seq, -10)").Check(testkit.Rows("-10"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("10"))
sequenceTable = external.GetTableByName(t, tk, "test", "seq")
tc, ok = sequenceTable.(*tables.TableCommon)
require.Equal(t, true, ok)
_, end, round = tc.GetSequenceCommon().GetSequenceBaseEndRound()
require.Equal(t, int64(4), end)
require.Equal(t, int64(1), round)
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("7"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("4"))
// test the sequence negative rebase.
tk.MustQuery("select setval(seq, 0)").Check(testkit.Rows("0"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-2"))
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment -2 start 0 maxvalue 10 minvalue -10 cache 3 cycle")
tk.MustQuery("select setval(seq, 20)").Check(testkit.Rows("<nil>"))
tk.MustQuery("select setval(seq, -20)").Check(testkit.Rows("-20"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("10"))
sequenceTable = external.GetTableByName(t, tk, "test", "seq")
tc, ok = sequenceTable.(*tables.TableCommon)
require.Equal(t, true, ok)
_, end, round = tc.GetSequenceCommon().GetSequenceBaseEndRound()
require.Equal(t, int64(6), end)
require.Equal(t, int64(1), round)
// test sequence lastval function.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq")
tk.MustQuery("select lastval(seq)").Check(testkit.Rows("<nil>"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1"))
tk.MustQuery("select lastval(seq)").Check(testkit.Rows("1"))
tk.MustQuery("select next value for seq").Check(testkit.Rows("2"))
tk.MustQuery("select lastval(seq)").Check(testkit.Rows("2"))
// setval won't change the last value.
tk.MustQuery("select setval(seq, -1)").Check(testkit.Rows("<nil>"))
tk.MustQuery("select lastval(seq)").Check(testkit.Rows("2"))
tk.MustQuery("select setval(seq, 5)").Check(testkit.Rows("5"))
tk.MustQuery("select lastval(seq)").Check(testkit.Rows("2"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("6"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("7"))
// test lastval in positive-growth sequence cycle and cache.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment 3 start 3 maxvalue 14 cache 3 cycle")
tk.MustQuery("select lastval(seq)").Check(testkit.Rows("<nil>"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3"))
sequenceTable = external.GetTableByName(t, tk, "test", "seq")
tc, ok = sequenceTable.(*tables.TableCommon)
require.Equal(t, true, ok)
_, end, round = tc.GetSequenceCommon().GetSequenceBaseEndRound()
require.Equal(t, int64(9), end)
require.Equal(t, int64(0), round)
// invalidate the current sequence cache.
tk.MustQuery("select setval(seq, 10)").Check(testkit.Rows("10"))
tk.MustQuery("select lastval(seq)").Check(testkit.Rows("3"))
// trigger the next sequence cache.
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("12"))
sequenceTable = external.GetTableByName(t, tk, "test", "seq")
tc, ok = sequenceTable.(*tables.TableCommon)
require.Equal(t, true, ok)
_, end, round = tc.GetSequenceCommon().GetSequenceBaseEndRound()
require.Equal(t, int64(14), end)
require.Equal(t, int64(0), round)
// invalidate the current sequence cache.
tk.MustQuery("select setval(seq, 13)").Check(testkit.Rows("13"))
tk.MustQuery("select lastval(seq)").Check(testkit.Rows("12"))
// trigger the next sequence cache.
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1"))
sequenceTable = external.GetTableByName(t, tk, "test", "seq")
tc, ok = sequenceTable.(*tables.TableCommon)
require.Equal(t, true, ok)
_, end, round = tc.GetSequenceCommon().GetSequenceBaseEndRound()
require.Equal(t, int64(7), end)
require.Equal(t, int64(1), round)
tk.MustQuery("select lastval(seq)").Check(testkit.Rows("1"))
// test lastval in negative-growth sequence cycle and cache.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment -3 start -2 maxvalue 10 minvalue -10 cache 3 cycle")
tk.MustQuery("select lastval(seq)").Check(testkit.Rows("<nil>"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-2"))
sequenceTable = external.GetTableByName(t, tk, "test", "seq")
tc, ok = sequenceTable.(*tables.TableCommon)
require.Equal(t, true, ok)
_, end, round = tc.GetSequenceCommon().GetSequenceBaseEndRound()
require.Equal(t, int64(-8), end)
require.Equal(t, int64(0), round)
// invalidate the current sequence cache.
tk.MustQuery("select setval(seq, -8)").Check(testkit.Rows("-8"))
tk.MustQuery("select lastval(seq)").Check(testkit.Rows("-2"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("10"))
sequenceTable = external.GetTableByName(t, tk, "test", "seq")
tc, ok = sequenceTable.(*tables.TableCommon)
require.Equal(t, true, ok)
_, end, round = tc.GetSequenceCommon().GetSequenceBaseEndRound()
require.Equal(t, int64(4), end)
require.Equal(t, int64(1), round)
tk.MustQuery("select lastval(seq)").Check(testkit.Rows("10"))
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment -1 start 1 maxvalue 10 minvalue -10 cache 3 cycle")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("1"))
tk.MustQuery("select setval(seq, -8)").Check(testkit.Rows("-8"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-9"))
sequenceTable = external.GetTableByName(t, tk, "test", "seq")
tc, ok = sequenceTable.(*tables.TableCommon)
require.Equal(t, true, ok)
_, end, round = tc.GetSequenceCommon().GetSequenceBaseEndRound()
require.Equal(t, int64(-10), end)
require.Equal(t, int64(0), round)
// Test the sequence seek formula will overflow Int64.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment 2 start -9223372036854775807 maxvalue 9223372036854775806 minvalue -9223372036854775807 cache 2 cycle")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-9223372036854775807"))
tk.MustQuery("select setval(seq, 9223372036854775800)").Check(testkit.Rows("9223372036854775800"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("9223372036854775801"))
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq increment -2 start 9223372036854775806 maxvalue 9223372036854775806 minvalue -9223372036854775807 cache 2 cycle")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("9223372036854775806"))
tk.MustQuery("select setval(seq, -9223372036854775800)").Check(testkit.Rows("-9223372036854775800"))
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("-9223372036854775802"))
// Test sequence function with wrong object name.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("drop table if exists seq")
tk.MustExec("drop view if exists seq")
tk.MustExec("drop sequence if exists seq1")
tk.MustExec("drop table if exists seq1")
tk.MustExec("drop view if exists seq1")
tk.MustExec("create table seq(a int)")
err = tk.ExecToErr("select nextval(seq)")
require.Error(t, err)
require.EqualError(t, err, "[schema:1347]'test.seq' is not SEQUENCE")
err = tk.ExecToErr("select lastval(seq)")
require.Error(t, err)
require.EqualError(t, err, "[schema:1347]'test.seq' is not SEQUENCE")
err = tk.ExecToErr("select setval(seq, 10)")
require.Error(t, err)
require.EqualError(t, err, "[schema:1347]'test.seq' is not SEQUENCE")
tk.MustExec("create view seq1 as select * from seq")
err = tk.ExecToErr("select nextval(seq1)")
require.Error(t, err)
require.EqualError(t, err, "[schema:1347]'test.seq1' is not SEQUENCE")
err = tk.ExecToErr("select lastval(seq1)")
require.Error(t, err)
require.EqualError(t, err, "[schema:1347]'test.seq1' is not SEQUENCE")
err = tk.ExecToErr("select setval(seq1, 10)")
require.Error(t, err)
require.EqualError(t, err, "[schema:1347]'test.seq1' is not SEQUENCE")
tk.MustExec("drop sequence if exists seq")
tk.MustExec("drop table if exists seq")
tk.MustExec("drop view if exists seq")
tk.MustExec("drop sequence if exists seq1")
tk.MustExec("drop table if exists seq1")
tk.MustExec("drop view if exists seq1")
// test a bug found in ticase.
tk.MustExec("create sequence seq")
tk.MustQuery("select setval(seq, 10)").Check(testkit.Rows("10"))
tk.MustQuery("select setval(seq, 5)").Check(testkit.Rows("<nil>"))
tk.MustExec("drop sequence seq")
tk.MustExec("create sequence seq increment=-1")
tk.MustQuery("select setval(seq, -10)").Check(testkit.Rows("-10"))
tk.MustQuery("select setval(seq, -5)").Check(testkit.Rows("<nil>"))
tk.MustExec("drop sequence seq")
// test the current value already satisfied setval in other session.
tk.MustExec("create sequence seq")
tk.MustQuery("select setval(seq, 100)").Check(testkit.Rows("100"))
se, err := session.CreateSession4Test(store)
require.NoError(t, err)
tk1 := testkit.NewTestKit(t, store)
tk1.SetSession(se)
tk1.MustExec("use test")
tk1.MustQuery("select setval(seq, 50)").Check(testkit.Rows("<nil>"))
tk1.MustQuery("select nextval(seq)").Check(testkit.Rows("101"))
tk1.MustQuery("select setval(seq, 100)").Check(testkit.Rows("<nil>"))
tk1.MustQuery("select setval(seq, 101)").Check(testkit.Rows("<nil>"))
tk1.MustQuery("select setval(seq, 102)").Check(testkit.Rows("102"))
tk.MustExec("drop sequence seq")
tk.MustExec("create sequence seq increment=-1")
tk.MustQuery("select setval(seq, -100)").Check(testkit.Rows("-100"))
tk1.MustQuery("select setval(seq, -50)").Check(testkit.Rows("<nil>"))
tk1.MustQuery("select nextval(seq)").Check(testkit.Rows("-101"))
tk1.MustQuery("select setval(seq, -100)").Check(testkit.Rows("<nil>"))
tk1.MustQuery("select setval(seq, -101)").Check(testkit.Rows("<nil>"))
tk1.MustQuery("select setval(seq, -102)").Check(testkit.Rows("-102"))
tk.MustExec("drop sequence seq")
// test the sequence name preprocess.
tk.MustExec("drop table if exists t")
tk.MustExec("create sequence seq")
tk.MustExec("create table t(a int)")
tk.MustExec("insert into t values(1),(2)")
tk.MustQuery("select nextval(seq), t.a from t").Check(testkit.Rows("1 1", "2 2"))
err = tk.ExecToErr("select nextval(t), t.a from t")
require.Error(t, err)
require.EqualError(t, err, "[schema:1347]'test.t' is not SEQUENCE")
err = tk.ExecToErr("select nextval(seq), nextval(t), t.a from t")
require.Error(t, err)
require.EqualError(t, err, "[schema:1347]'test.t' is not SEQUENCE")
tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3"))
tk.MustExec("drop sequence seq")
tk.MustExec("drop table t")
}
func TestInsertSequence(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
session.SetSchemaLease(600 * time.Millisecond)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop sequence if exists seq")
tk.MustExec("drop table if exists t")
// test insert with sequence default value.
tk.MustExec("create sequence seq")
tk.MustExec("create table t (a int default next value for seq)")
tk.MustExec("insert into t values()")
tk.MustQuery("select * from t").Check(testkit.Rows("1"))
tk.MustExec("insert into t values(),(),()")
tk.MustQuery("select * from t").Check(testkit.Rows("1", "2", "3", "4"))
tk.MustExec("delete from t")
tk.MustExec("insert into t values(-1),(default),(-1)")
tk.MustQuery("select * from t").Check(testkit.Rows("-1", "5", "-1"))
// test insert with specified sequence value rather than default.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int)")
tk.MustExec("insert into t values(next value for seq)")
tk.MustQuery("select * from t").Check(testkit.Rows("6"))
tk.MustExec("insert into t values(next value for seq),(nextval(seq))")
tk.MustQuery("select * from t").Check(testkit.Rows("6", "7", "8"))
// test insert with sequence expression.
tk.MustExec("delete from t")
tk.MustExec("insert into t values(next value for seq + 1),(nextval(seq) * 2)")
tk.MustQuery("select * from t").Check(testkit.Rows("10", "20"))
tk.MustExec("delete from t")
tk.MustExec("insert into t values((next value for seq - 1) / 2)")
tk.MustQuery("select * from t").Check(testkit.Rows("5"))
// test insert with user specified value.
tk.MustExec("delete from t")
tk.MustExec("insert into t values(-1),(next value for seq),(-1),(nextval(seq))")
tk.MustQuery("select * from t").Check(testkit.Rows("-1", "12", "-1", "13"))
// test insert with lastval & setval.
tk.MustExec("delete from t")
tk.MustExec("insert into t values(lastval(seq)),(-1),(nextval(seq))")
tk.MustQuery("select * from t").Check(testkit.Rows("13", "-1", "14"))
tk.MustExec("delete from t")
tk.MustQuery("select setval(seq, 100)").Check(testkit.Rows("100"))
tk.MustExec("insert into t values(lastval(seq)),(-1),(nextval(seq))")
tk.MustQuery("select * from t").Check(testkit.Rows("14", "-1", "101"))
// test insert with generated column.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (id int default next value for seq, col1 int generated always as (id + 1))")
tk.MustExec("insert into t values()")
tk.MustQuery("select * from t").Check(testkit.Rows("1 2"))
tk.MustExec("insert into t values(),()")
tk.MustQuery("select * from t").Check(testkit.Rows("1 2", "2 3", "3 4"))
tk.MustExec("delete from t")
tk.MustExec("insert into t (id) values(-1),(default)")
tk.MustQuery("select * from t").Check(testkit.Rows("-1 0", "4 5"))
// test sequence run out (overflows MaxInt64).
setSQL := "select setval(seq," + strconv.FormatInt(model.DefaultPositiveSequenceMaxValue+1, 10) + ")"
tk.MustQuery(setSQL).Check(testkit.Rows("9223372036854775807"))
err := tk.QueryToErr("select nextval(seq)")
require.EqualError(t, err, "[table:4135]Sequence 'test.seq' has run out")
}
func TestUnflodSequence(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
session.SetSchemaLease(600 * time.Millisecond)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
// test insert into select from.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("drop table if exists t1,t2,t3")
tk.MustExec("create sequence seq")
tk.MustExec("create table t1 (a int)")
tk.MustExec("create table t2 (a int, b int)")
tk.MustExec("create table t3 (a int, b int, c int)")
tk.MustExec("insert into t1 values(-1),(-1),(-1)")
// test sequence function unfold.
tk.MustQuery("select nextval(seq), a from t1").Check(testkit.Rows("1 -1", "2 -1", "3 -1"))
tk.MustExec("insert into t2 select nextval(seq), a from t1")
tk.MustQuery("select * from t2").Check(testkit.Rows("4 -1", "5 -1", "6 -1"))
tk.MustExec("delete from t2")
// if lastval is folded, the first result should be always 6.
tk.MustQuery("select lastval(seq), nextval(seq), a from t1").Check(testkit.Rows("6 7 -1", "7 8 -1", "8 9 -1"))
tk.MustExec("insert into t3 select lastval(seq), nextval(seq), a from t1")
tk.MustQuery("select * from t3").Check(testkit.Rows("9 10 -1", "10 11 -1", "11 12 -1"))
tk.MustExec("delete from t3")
// if setval is folded, the result should be "101 100 -1"...
tk.MustQuery("select nextval(seq), setval(seq,100), a from t1").Check(testkit.Rows("13 100 -1", "101 <nil> -1", "102 <nil> -1"))
tk.MustExec("insert into t3 select nextval(seq), setval(seq,200), a from t1")
tk.MustQuery("select * from t3").Check(testkit.Rows("103 200 -1", "201 <nil> -1", "202 <nil> -1"))
tk.MustExec("delete from t3")
// lastval should be evaluated after nextval in each row.
tk.MustQuery("select nextval(seq), lastval(seq), a from t1").Check(testkit.Rows("203 203 -1", "204 204 -1", "205 205 -1"))
tk.MustExec("insert into t3 select nextval(seq), lastval(seq), a from t1")
tk.MustQuery("select * from t3").Check(testkit.Rows("206 206 -1", "207 207 -1", "208 208 -1"))
tk.MustExec("delete from t3")
// double nextval should be also evaluated in each row.
tk.MustQuery("select nextval(seq), nextval(seq), a from t1").Check(testkit.Rows("209 210 -1", "211 212 -1", "213 214 -1"))
tk.MustExec("insert into t3 select nextval(seq), nextval(seq), a from t1")
tk.MustQuery("select * from t3").Check(testkit.Rows("215 216 -1", "217 218 -1", "219 220 -1"))
tk.MustExec("delete from t3")
tk.MustQuery("select nextval(seq)+lastval(seq), a from t1").Check(testkit.Rows("442 -1", "444 -1", "446 -1"))
tk.MustExec("insert into t2 select nextval(seq)+lastval(seq), a from t1")
tk.MustQuery("select * from t2").Check(testkit.Rows("448 -1", "450 -1", "452 -1"))
tk.MustExec("delete from t2")
// sub-query contain sequence function.
tk.MustQuery("select nextval(seq), b from (select nextval(seq) as b, a from t1) t2").Check(testkit.Rows("227 228", "229 230", "231 232"))
tk.MustExec("insert into t2 select nextval(seq), b from (select nextval(seq) as b, a from t1) t2")
tk.MustQuery("select * from t2").Check(testkit.Rows("233 234", "235 236", "237 238"))
tk.MustExec("delete from t2")
// For union operator like select1 union select2, select1 and select2 will be executed parallelly,
// so sequence function in both select are evaluated without order. Besides, the upper union operator
// will gather results through multi worker goroutine parallelly leading the results unordered.
// Cases like:
// `select nextval(seq), a from t1 union select lastval(seq), a from t2`
// `select nextval(seq), a from t1 union select nextval(seq), a from t2`
// The executing order of nextval and lastval is implicit, don't make any assumptions on it.
}
// before this PR:
// single insert consume: 50.498672ms
// after this PR:
// single insert consume: 33.213615ms
func BenchmarkInsertCacheDefaultExpr(b *testing.B) {
store, clean := testkit.CreateMockStore(b)
defer clean()
session.SetSchemaLease(600 * time.Millisecond)
tk := testkit.NewTestKit(b, store)
tk.MustExec("use test")
tk.MustExec("drop sequence if exists seq")
tk.MustExec("drop table if exists t")
tk.MustExec("create sequence seq")
tk.MustExec("create table t(a int default next value for seq)")
sql := "insert into t values "
for i := 0; i < 1000; i++ {
if i == 0 {
sql += "()"
} else {
sql += ",()"
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
tk.MustExec(sql)
}
}
func TestSequenceFunctionPrivilege(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
session.SetSchemaLease(600 * time.Millisecond)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
// Test sequence function privilege.
tk.MustExec("drop sequence if exists seq")
tk.MustExec("create sequence seq")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int default next value for seq)")
tk.MustExec("drop user if exists myuser@localhost")
tk.MustExec("create user myuser@localhost")
tk1 := testkit.NewTestKit(t, store)
se, err := session.CreateSession4Test(store)
require.NoError(t, err)
require.True(t, se.Auth(&auth.UserIdentity{Username: "myuser", Hostname: "localhost"}, nil, nil))
tk1.SetSession(se)
// grant the myuser the create access to the sequence.
tk.MustExec("grant insert on test.t to 'myuser'@'localhost'")
// INSERT privilege required to use nextval.
tk1.MustExec("use test")
err = tk1.QueryToErr("select nextval(seq)")
require.Error(t, err)
require.EqualError(t, err, "[expression:1142]INSERT command denied to user 'myuser'@'localhost' for table 'seq'")
_, err = tk1.Exec("insert into t values()")
require.Error(t, err)
require.EqualError(t, err, "[expression:1142]INSERT command denied to user 'myuser'@'localhost' for table 'seq'")
// SELECT privilege required to use lastval.
err = tk1.QueryToErr("select lastval(seq)")
require.Error(t, err)
require.EqualError(t, err, "[expression:1142]SELECT command denied to user 'myuser'@'localhost' for table 'seq'")
// INSERT privilege required to use setval.
err = tk1.QueryToErr("select setval(seq, 10)")
require.Error(t, err)
require.EqualError(t, err, "[expression:1142]INSERT command denied to user 'myuser'@'localhost' for table 'seq'")
// grant the myuser the SELECT & UPDATE access to sequence seq.
tk.MustExec("grant SELECT, INSERT on test.seq to 'myuser'@'localhost'")
// SELECT privilege required to use nextval.
tk1.MustQuery("select nextval(seq)").Check(testkit.Rows("1"))
tk1.MustQuery("select lastval(seq)").Check(testkit.Rows("1"))
tk1.MustQuery("select setval(seq, 10)").Check(testkit.Rows("10"))
tk1.MustExec("insert into t values()")
tk.MustExec("drop table t")
tk.MustExec("drop sequence seq")
tk.MustExec("drop user myuser@localhost")
}
// Background: the newly added column in TiDB won't fill the known rows with specific
// sequence next value immediately. Every time TiDB select the data from storage, kvDB
// will fill the originDefaultValue to these incomplete rows (but not store).
//
// In sequence case, every time filling these rows, kvDB should eval the sequence
// expr for len(incomplete rows) times, and combine these row data together. That
// means the select result is not always the same.
//
// However, the altered column with sequence as it's default value can work well.
// Because this column has already been added before the alter action, which also
// means originDefaultValue should be something but nil, so the back filling in kvDB
// can work well.
//
// The new altered sequence default value for this column only take effect on the
// subsequent inserted rows.
//
// So under current situation, TiDB will
// [1]: forbid the new added column has sequence as it's default value.
// [2]: allow the altered column with sequence as default value.
func TestSequenceDefaultLogic(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
session.SetSchemaLease(600 * time.Millisecond)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop sequence if exists seq")
tk.MustExec("drop table if exists t")
tk.MustExec("create sequence seq")
tk.MustExec("create table t(a int)")
// Alter table to use sequence as default value is ok.
tk.MustExec("insert into t values(-1),(-1),(-1)")
tk.MustExec("alter table t add column b int default -1")
tk.MustQuery("select * from t").Check(testkit.Rows("-1 -1", "-1 -1", "-1 -1"))
tk.MustExec("alter table t modify column b int default next value for seq")
tk.MustQuery("select * from t").Check(testkit.Rows("-1 -1", "-1 -1", "-1 -1"))
tk.MustExec("insert into t(a) values(-1),(-1)")
tk.MustQuery("select * from t").Check(testkit.Rows("-1 -1", "-1 -1", "-1 -1", "-1 1", "-1 2"))