forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplan_cache_test.go
2522 lines (2220 loc) · 112 KB
/
plan_cache_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 2022 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 core_test
import (
"context"
"errors"
"fmt"
"math/rand"
"strings"
"sync"
"testing"
"time"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/planner"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/types"
driver "github.com/pingcap/tidb/types/parser_driver"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/size"
"github.com/stretchr/testify/require"
)
func TestInitLRUWithSystemVar(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("set @@session.tidb_prepared_plan_cache_size = 0") // MinValue: 1
tk.MustQuery("select @@session.tidb_prepared_plan_cache_size").Check(testkit.Rows("1"))
sessionVar := tk.Session().GetSessionVars()
lru := plannercore.NewLRUPlanCache(uint(sessionVar.PreparedPlanCacheSize), 0, 0, tk.Session(), false)
require.NotNil(t, lru)
}
func TestIssue45086(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`CREATE TABLE t (a int(11) DEFAULT NULL, b date DEFAULT NULL)`)
tk.MustExec(`INSERT INTO t VALUES (1, current_date())`)
tk.MustExec(`PREPARE stmt FROM 'SELECT * FROM t WHERE b=current_date()'`)
require.Equal(t, len(tk.MustQuery(`EXECUTE stmt`).Rows()), 1)
}
func TestIssue43311(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table test.t (id int, value decimal(7,4), c1 int, c2 int)`)
tk.MustExec(`insert into test.t values (1,1.9285,54,28), (1,1.9286,54,28)`)
tk.MustExec(`set session tidb_enable_non_prepared_plan_cache=0`)
tk.MustQuery(`select * from t where value = 54 / 28`).Check(testkit.Rows()) // empty
tk.MustExec(`set session tidb_enable_non_prepared_plan_cache=1`)
tk.MustQuery(`select * from t where value = 54 / 28`).Check(testkit.Rows()) // empty
tk.MustQuery(`select * from t where value = 54 / 28`).Check(testkit.Rows()) // empty
tk.MustExec(`prepare st from 'select * from t where value = ? / ?'`)
tk.MustExec(`set @a=54, @b=28`)
tk.MustQuery(`execute st using @a, @b`).Check(testkit.Rows()) // empty
tk.MustQuery(`execute st using @a, @b`).Check(testkit.Rows()) // empty
}
func TestIssue44830(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`set @@tidb_opt_fix_control = "44830:ON"`)
tk.MustExec(`create table t (a int, primary key(a))`)
tk.MustExec(`create table t1 (a int, b int, primary key(a, b))`) // multiple-column primary key
tk.MustExec(`insert into t values (1), (2), (3)`)
tk.MustExec(`insert into t1 values (1, 1), (2, 2), (3, 3)`)
tk.MustExec(`set @a=1, @b=2, @c=3`)
// single-column primary key cases
tk.MustExec(`prepare st from 'select * from t where 1=1 and a in (?, ?, ?)'`)
tk.MustQuery(`execute st using @a, @b, @c`).Sort().Check(testkit.Rows("1", "2", "3"))
tk.MustQuery(`execute st using @a, @b, @c`).Sort().Check(testkit.Rows("1", "2", "3"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustQuery(`execute st using @a, @b, @b`).Sort().Check(testkit.Rows("1", "2"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0")) // range length changed
tk.MustQuery(`execute st using @b, @b, @b`).Sort().Check(testkit.Rows("2"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0")) // range length changed
tk.MustQuery(`execute st using @a, @b, @c`).Sort().Check(testkit.Rows("1", "2", "3"))
tk.MustQuery(`execute st using @a, @b, @b`).Sort().Check(testkit.Rows("1", "2"))
tk.MustQuery(`execute st using @a, @b, @b`).Sort().Check(testkit.Rows("1", "2"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0")) // contain duplicated values in the in-list
// multi-column primary key cases
tk.MustExec(`prepare st from 'select * from t1 where 1=1 and (a, b) in ((?, ?), (?, ?), (?, ?))'`)
tk.MustQuery(`execute st using @a, @a, @b, @b, @c, @c`).Sort().Check(testkit.Rows("1 1", "2 2", "3 3"))
tk.MustQuery(`execute st using @a, @a, @b, @b, @c, @c`).Sort().Check(testkit.Rows("1 1", "2 2", "3 3"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustQuery(`execute st using @a, @a, @b, @b, @b, @b`).Sort().Check(testkit.Rows("1 1", "2 2"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0")) // range length changed
tk.MustQuery(`execute st using @b, @b, @b, @b, @b, @b`).Sort().Check(testkit.Rows("2 2"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0")) // range length changed
tk.MustQuery(`execute st using @b, @b, @b, @b, @c, @c`).Sort().Check(testkit.Rows("2 2", "3 3"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0")) // range length changed
tk.MustQuery(`execute st using @a, @a, @a, @a, @a, @a`).Sort().Check(testkit.Rows("1 1"))
tk.MustQuery(`execute st using @a, @a, @a, @a, @a, @a`).Sort().Check(testkit.Rows("1 1"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0")) // contain duplicated values in the in-list
tk.MustQuery(`execute st using @a, @a, @b, @b, @b, @b`).Sort().Check(testkit.Rows("1 1", "2 2"))
tk.MustQuery(`execute st using @a, @a, @b, @b, @b, @b`).Sort().Check(testkit.Rows("1 1", "2 2"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0")) // contain duplicated values in the in-list
}
func TestIssue44830NonPrep(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`set @@tidb_enable_non_prepared_plan_cache=1`)
tk.MustExec(`set @@tidb_opt_fix_control = "44830:ON"`)
tk.MustExec(`create table t1 (a int, b int, primary key(a, b))`) // multiple-column primary key
tk.MustExec(`insert into t1 values (1, 1), (2, 2), (3, 3)`)
tk.MustExec(`set @a=1, @b=2, @c=3`)
tk.MustQuery(`select * from t1 where 1=1 and (a, b) in ((1, 1), (2, 2), (3, 3))`).Sort().Check(testkit.Rows("1 1", "2 2", "3 3"))
tk.MustQuery(`select * from t1 where 1=1 and (a, b) in ((1, 1), (2, 2), (3, 3))`).Sort().Check(testkit.Rows("1 1", "2 2", "3 3"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustQuery(`select * from t1 where 1=1 and (a, b) in ((1, 1), (2, 2), (2, 2))`).Sort().Check(testkit.Rows("1 1", "2 2"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
tk.MustQuery(`select * from t1 where 1=1 and (a, b) in ((2, 2), (2, 2), (2, 2))`).Sort().Check(testkit.Rows("2 2"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
tk.MustQuery(`select * from t1 where 1=1 and (a, b) in ((1, 1), (1, 1), (1, 1))`).Sort().Check(testkit.Rows("1 1"))
tk.MustQuery(`select * from t1 where 1=1 and (a, b) in ((1, 1), (1, 1), (1, 1))`).Sort().Check(testkit.Rows("1 1"))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
}
func TestPlanCacheSizeSwitch(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
// default value = 100
tk.MustQuery(`select @@tidb_prepared_plan_cache_size`).Check(testkit.Rows("100"))
tk.MustQuery(`select @@tidb_session_plan_cache_size`).Check(testkit.Rows("100"))
// keep the same value when updating any one of them
tk.MustExec(`set @@tidb_prepared_plan_cache_size = 200`)
tk.MustQuery(`select @@tidb_prepared_plan_cache_size`).Check(testkit.Rows("200"))
tk.MustQuery(`select @@tidb_session_plan_cache_size`).Check(testkit.Rows("200"))
tk.MustExec(`set @@tidb_session_plan_cache_size = 300`)
tk.MustQuery(`select @@tidb_prepared_plan_cache_size`).Check(testkit.Rows("300"))
tk.MustQuery(`select @@tidb_session_plan_cache_size`).Check(testkit.Rows("300"))
tk.MustExec(`set global tidb_prepared_plan_cache_size = 400`)
tk1 := testkit.NewTestKit(t, store)
tk1.MustQuery(`select @@tidb_prepared_plan_cache_size`).Check(testkit.Rows("400"))
tk1.MustQuery(`select @@tidb_session_plan_cache_size`).Check(testkit.Rows("400"))
tk.MustExec(`set global tidb_session_plan_cache_size = 500`)
tk2 := testkit.NewTestKit(t, store)
tk2.MustQuery(`select @@tidb_prepared_plan_cache_size`).Check(testkit.Rows("500"))
tk2.MustQuery(`select @@tidb_session_plan_cache_size`).Check(testkit.Rows("500"))
}
func TestPlanCacheUnsafeRange(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t (a int unsigned, key(a))`)
tk.MustExec(`prepare st from 'select a from t use index(a) where a<?'`)
tk.MustExec(`set @a=10`)
tk.MustExec(`execute st using @a`)
tk.MustExec(`execute st using @a`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustExec(`set @a=-10`) // invalid range for an unsigned column
tk.MustExec(`execute st using @a`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
tk.MustExec(`set @a=10`) // plan cache can work again
tk.MustExec(`execute st using @a`)
tk.MustExec(`execute st using @a`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustExec(`create table t1 (a enum('1', '2'), key(a))`)
tk.MustExec(`prepare st from 'select a from t1 use index(a) where a=?'`)
tk.MustExec(`set @a='1'`)
tk.MustExec(`execute st using @a`)
tk.MustExec(`execute st using @a`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustExec(`set @a='x'`) // invalid value for this column
tk.MustExec(`execute st using @a`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
tk.MustExec(`set @a='1'`) // plan cache can work again
tk.MustExec(`execute st using @a`)
tk.MustExec(`execute st using @a`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
}
func TestIssue43405(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t (a int)`)
tk.MustExec(`insert into t values (1), (2), (3), (4)`)
tk.MustExec(`prepare st from 'select * from t where a!=? and a in (?, ?, ?)'`)
tk.MustExec(`set @a=1, @b=2, @c=3, @d=4`)
tk.MustQuery(`execute st using @a, @a, @a, @a`).Sort().Check(testkit.Rows())
tk.MustQuery(`show warnings`).Check(testkit.Rows("Warning 1105 skip prepared plan-cache: NE/INList simplification is triggered"))
tk.MustQuery(`execute st using @a, @a, @b, @c`).Sort().Check(testkit.Rows("2", "3"))
tk.MustQuery(`show warnings`).Check(testkit.Rows("Warning 1105 skip prepared plan-cache: NE/INList simplification is triggered"))
tk.MustQuery(`execute st using @a, @b, @c, @d`).Sort().Check(testkit.Rows("2", "3", "4"))
tk.MustQuery(`show warnings`).Check(testkit.Rows("Warning 1105 skip prepared plan-cache: NE/INList simplification is triggered"))
tk.MustExec(`CREATE TABLE UK_SIGNED_19384 (
COL1 decimal(37,4) unsigned DEFAULT NULL COMMENT 'WITH DEFAULT',
COL2 varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
COL4 datetime DEFAULT NULL,
COL3 bigint DEFAULT NULL,
COL5 float DEFAULT NULL,
UNIQUE KEY UK_COL1 (COL1)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin`)
tk.MustExec(`INSERT INTO UK_SIGNED_19384 VALUES
(729024465529090.5423,'劗驻胭毤橰亀讁陶ĉ突錌ͳ河碡祁聓兕锻觰俆','4075-07-11 12:02:57',6021562653572886552,1.93349e38),
(492790234219503.0846,'硴皡箒嫹璞玚囑蚂身囈軔獰髴囥慍廂頚禌浖蕐','1193-09-27 12:13:40',1836453747944153034,-2.67982e38),
(471841432147994.4981,'豻貐裝濂婝蒙蘦镢県蟎髓蓼窘搴熾臐哥递泒執','1618-01-24 05:06:44',6669616052974883820,9.38232e37)`)
tk.MustExec(`prepare stmt from 'select/*+ tidb_inlj(t1) */ t1.col1 from UK_SIGNED_19384 t1 join UK_SIGNED_19384 t2 on t1.col1 = t2.col1 where t1. col1 != ? and t2. col1 in (?, ?, ?)'`)
tk.MustExec(`set @a=999999999999999999999999999999999.9999, @b=999999999999999999999999999999999.9999, @c=999999999999999999999999999999999.9999, @d=999999999999999999999999999999999.9999`)
tk.MustQuery(`execute stmt using @a,@b,@c,@d`).Check(testkit.Rows()) // empty result
tk.MustExec(`set @a=895769331208356.9029, @b=471841432147994.4981, @c=729024465529090.5423, @d=492790234219503.0846`)
tk.MustQuery(`execute stmt using @a,@b,@c,@d`).Sort().Check(testkit.Rows(
"471841432147994.4981",
"492790234219503.0846",
"729024465529090.5423"))
}
func TestIssue40296(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`create database test_40296`)
tk.MustExec(`use test_40296`)
tk.MustExec(`CREATE TABLE IDT_MULTI15880STROBJSTROBJ (
COL1 enum('aa','bb','cc','dd','ff','gg','kk','ll','mm','ee') DEFAULT NULL,
COL2 decimal(20,0) DEFAULT NULL,
COL3 date DEFAULT NULL,
KEY U_M_COL4 (COL1,COL2),
KEY U_M_COL5 (COL3,COL2))`)
tk.MustExec(`insert into IDT_MULTI15880STROBJSTROBJ values("ee", -9605492323393070105, "0850-03-15")`)
tk.MustExec(`set session tidb_enable_non_prepared_plan_cache=on`)
tk.MustQuery(`select * from IDT_MULTI15880STROBJSTROBJ where col1 in ("dd", "dd") or col2 = 9923875910817805958 or col3 = "9994-11-11"`).Check(
testkit.Rows())
tk.MustQuery(`select * from IDT_MULTI15880STROBJSTROBJ where col1 in ("aa", "aa") or col2 = -9605492323393070105 or col3 = "0005-06-22"`).Check(
testkit.Rows("ee -9605492323393070105 0850-03-15"))
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0")) // unary operator '-' is not supported now.
}
func TestIssue43522(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`CREATE TABLE UK_SIGNED_19385 (
COL1 decimal(37,4) unsigned DEFAULT '101.0000' COMMENT 'WITH DEFAULT',
COL2 varchar(20) DEFAULT NULL,
COL4 datetime DEFAULT NULL,
COL3 bigint(20) DEFAULT NULL,
COL5 float DEFAULT NULL,
UNIQUE KEY UK_COL1 (COL1) /*!80000 INVISIBLE */)`)
tk.MustExec(`INSERT INTO UK_SIGNED_19385 VALUES (999999999999999999999999999999999.9999,'苊檷鞤寰抿逿詸叟艟俆錟什姂庋鴪鎅枀礰扚匝','8618-02-11 03:30:03',7016504421081900731,2.77465e38)`)
tk.MustQuery(`select * from UK_SIGNED_19385 where col1 = 999999999999999999999999999999999.9999 and
col1 * 999999999999999999999999999999999.9999 between 999999999999999999999999999999999.9999 and
999999999999999999999999999999999.9999`).Check(testkit.Rows()) // empty and no error
}
func TestIssue43520(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`CREATE TABLE IDT_20290 (
COL1 mediumtext DEFAULT NULL,
COL2 decimal(52,7) DEFAULT NULL,
COL3 datetime DEFAULT NULL,
KEY U_M_COL (COL1(10),COL2,COL3) /*!80000 INVISIBLE */)`)
tk.MustExec(`INSERT INTO IDT_20290 VALUES
('',210255309400.4264137,'4273-04-17 17:26:51'),
(NULL,952470120213.2538798,'7087-08-19 21:38:49'),
('俦',486763966102.1656494,'8846-06-12 12:02:13'),
('憁',610644171405.5953911,'2529-07-19 17:24:49'),
('顜',-359717183823.5275069,'2599-04-01 00:12:08'),
('塼',466512908211.1135111,'1477-10-20 07:14:51'),
('宻',-564216096745.0427987,'7071-11-20 13:38:24'),
('網',-483373421083.4724254,'2910-02-19 18:29:17'),
('顥',164020607693.9988781,'2820-10-12 17:38:44'),
('谪',25949740494.3937876,'6527-05-30 22:58:37')`)
err := tk.QueryToErr(`select * from IDT_20290 where col2 * 049015787697063065230692384394107598316198958.1850509 >= 659971401668884663953087553591534913868320924.5040396 and col2 = 869042976700631943559871054704914143535627349.9659934`)
require.ErrorContains(t, err, "value is out of range in")
}
func TestIssue14875(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t(a varchar(8) not null, b varchar(8) not null)`)
tk.MustExec(`insert into t values('1','1')`)
tk.MustExec(`prepare stmt from "select count(1) from t t1, t t2 where t1.a = t2.a and t2.b = '1' and t2.b = ?"`)
tk.MustExec(`set @a = '1'`)
tk.MustQuery(`execute stmt using @a`).Check(testkit.Rows("1"))
tk.MustExec(`set @a = '2'`)
tk.MustQuery(`execute stmt using @a`).Check(testkit.Rows("0"))
tk.MustExec(`prepare stmt from "select count(1) from t t1, t t2 where t1.a = t2.a and t1.a > ?"`)
tk.MustExec(`set @a = '1'`)
tk.MustQuery(`execute stmt using @a`).Check(testkit.Rows("0"))
tk.MustExec(`set @a = '0'`)
tk.MustQuery(`execute stmt using @a`).Check(testkit.Rows("1"))
}
func TestIssue14871(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t(a varchar(8), b varchar(8))`)
tk.MustExec(`insert into t values('1','1')`)
tk.MustExec(`prepare stmt from "select count(1) from t t1 left join t t2 on t1.a = t2.a where t2.b = ? and t2.b = ?"`)
tk.MustExec(`set @p0 = '1', @p1 = '2'`)
tk.MustQuery(`execute stmt using @p0, @p1`).Check(testkit.Rows("0"))
tk.MustExec(`set @p0 = '1', @p1 = '1'`)
tk.MustQuery(`execute stmt using @p0, @p1`).Check(testkit.Rows("1"))
}
func TestNonPreparedPlanCacheDMLHints(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t (a int)`)
tk.MustExec(`set @@tidb_enable_non_prepared_plan_cache=1`)
tk.MustExec(`set @@tidb_enable_non_prepared_plan_cache_for_dml=1`)
tk.MustExec(`insert into t values (1)`)
tk.MustExec(`insert into t values (1)`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustExec(`update t set a=1`)
tk.MustExec(`update t set a=1`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustExec(`delete from t where a=1`)
tk.MustExec(`delete from t where a=1`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustExec(`insert /*+ ignore_plan_cache() */ into t values (1)`)
tk.MustExec(`insert /*+ ignore_plan_cache() */ into t values (1)`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
tk.MustExec(`update /*+ ignore_plan_cache() */ t set a=1`)
tk.MustExec(`update /*+ ignore_plan_cache() */ t set a=1`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
tk.MustExec(`delete /*+ ignore_plan_cache() */ from t where a=1`)
tk.MustExec(`delete /*+ ignore_plan_cache() */ from t where a=1`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
tk.MustExec(`insert into t values (1)`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustExec(`update t set a=1`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustExec(`delete from t where a=1`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
}
func TestNonPreparedPlanCachePlanString(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t (a int, b int, key(a))`)
tk.MustExec(`set @@tidb_enable_non_prepared_plan_cache=1`)
ctx := tk.Session()
planString := func(sql string) string {
stmts, err := session.Parse(ctx, sql)
require.NoError(t, err)
stmt := stmts[0]
ret := &plannercore.PreprocessorReturn{}
err = plannercore.Preprocess(context.Background(), ctx, stmt, plannercore.WithPreprocessorReturn(ret))
require.NoError(t, err)
p, _, err := planner.Optimize(context.TODO(), ctx, stmt, ret.InfoSchema)
require.NoError(t, err)
return plannercore.ToString(p)
}
require.Equal(t, planString("select a from t where a < 1"), "IndexReader(Index(t.a)[[-inf,1)])")
require.Equal(t, planString("select a from t where a < 10"), "IndexReader(Index(t.a)[[-inf,10)])") // range 1 -> 10
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
require.Equal(t, planString("select * from t where b < 1"), "TableReader(Table(t)->Sel([lt(test.t.b, 1)]))")
require.Equal(t, planString("select * from t where b < 10"), "TableReader(Table(t)->Sel([lt(test.t.b, 10)]))") // filter 1 -> 10
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
}
func TestNonPreparedPlanCacheJSONFilter(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int, b json)")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec(`select * from t where a<1`)
tk.MustExec(`select * from t where a<2`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
// queries with filters with JSON columns are not supported
tk.MustExec(`select * from t where b<1`)
tk.MustExec(`select * from t where b<2`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
tk.MustExec(`select b from t where a<1`)
tk.MustExec(`select b from t where a<2`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
}
func TestNonPreparedPlanCacheEnumFilter(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int, b enum('1', '2', '3'))")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec(`select * from t where a<1`)
tk.MustExec(`select * from t where a<2`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
// queries with filters with enum columns are not supported
tk.MustExec(`select * from t where b='1'`)
tk.MustExec(`select * from t where b='2'`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
tk.MustExec(`select b from t where a<1`)
tk.MustExec(`select b from t where a<2`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
}
func TestNonPreparedPlanCacheDateFormat(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec(`create table t1 (s1 char(20) character set latin1)`)
tk.MustExec(`insert into t1 values (date_format('2004-02-02','%M'))`) // no error
tk.MustQuery(`select * from t1`).Check(testkit.Rows(`February`))
}
func TestNonPreparedPlanCacheNullValue(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int)")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec(`select * from t where a=1`)
tk.MustExec(`select * from t where a=2`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
tk.MustExec(`select * from t where a=null`) // query with null value cannot hit
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
tk.MustExec(`select * from t where a=2`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
}
func TestNonPreparedPlanCacheInListChange(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int)")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec(`select * from t where a in (1, 2, 3)`)
tk.MustExec(`select * from t where a in (2, 3, 4)`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
tk.MustExec(`select * from t where a in (2, 3, 4, 5)`) // cannot hit the previous plan
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
tk.MustExec(`select * from t where a in (1, 2, 3, 4)`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
}
func TestNonPreparedPlanCacheMemoryTable(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec(`select data_type from INFORMATION_SCHEMA.columns where table_name = 'v'`)
tk.MustExec(`select data_type from INFORMATION_SCHEMA.columns where table_name = 'v'`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
}
func TestNonPreparedPlanCacheTooManyConsts(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int)")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
var x []string
for i := 0; i < 201; i++ {
x = append(x, fmt.Sprintf("%v", i))
}
list1 := strings.Join(x[:199], ", ")
list2 := strings.Join(x[:200], ", ")
list3 := strings.Join(x[:201], ", ")
tk.MustExec(fmt.Sprintf(`select * from t where a in (%v)`, list1))
tk.MustExec(fmt.Sprintf(`select * from t where a in (%v)`, list1))
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
tk.MustExec(fmt.Sprintf(`select * from t where a in (%v)`, list2))
tk.MustExec(fmt.Sprintf(`select * from t where a in (%v)`, list2))
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
// query has more than 50 consts cannot hit
tk.MustExec(fmt.Sprintf(`select * from t where a in (%v)`, list3))
tk.MustExec(fmt.Sprintf(`select * from t where a in (%v)`, list3))
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
}
func TestNonPreparedPlanCacheSchemaChange(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int)")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec("select * from t where a=1")
tk.MustExec("select * from t where a=1")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
tk.MustExec("alter table t add index idx_a(a)")
tk.MustExec("select * from t where a=1")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0")) // cannot hit since the schema changed
tk.MustExec("select * from t where a=1")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
}
func TestNonPreparedCacheWithPreparedCache(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int)")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec(`prepare st from 'select * from t where a=1'`)
tk.MustExec(`execute st`)
tk.MustExec(`execute st`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
tk.MustExec(`select * from t where a=1`) // cannot hit since these 2 plan cache are separated
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
tk.MustExec(`select * from t where a=1`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
}
func TestNonPreparedPlanCacheSwitch(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int)")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec(`select * from t where a=1`)
tk.MustExec(`select * from t where a=1`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
tk.MustExec("set tidb_enable_non_prepared_plan_cache=0")
tk.MustExec(`select * from t where a=1`) // the session-level switch can take effect in real time
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
}
func TestNonPreparedPlanCacheSwitch2(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
for nonPrep := 0; nonPrep <= 1; nonPrep++ {
for prep := 0; prep <= 1; prep++ {
tk.MustExec("create table t(a int)")
tk.MustExec(fmt.Sprintf(`set tidb_enable_non_prepared_plan_cache=%v`, nonPrep))
tk.MustExec(fmt.Sprintf(`set tidb_enable_prepared_plan_cache=%v`, prep))
tk.MustExec(`select * from t where a<1`)
tk.MustExec(`select * from t where a<2`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows(fmt.Sprintf("%v", nonPrep)))
tk.MustExec(`prepare st from 'select * from t where a<?'`)
tk.MustExec(`set @a=1`)
tk.MustExec(`execute st using @a`)
tk.MustExec(`set @a=2`)
tk.MustExec(`execute st using @a`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows(fmt.Sprintf("%v", prep)))
tk.MustExec("drop table t")
}
}
}
func TestNonPreparedPlanCacheUnknownSchema(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table tt(a char(2) primary key, b char(2))`)
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
err := tk.ExecToErr(`select tt.* from tt tmp where a='aa'`)
require.Equal(t, err.Error(), "[planner:1051]Unknown table 'tt'")
}
func TestNonPreparedPlanCacheReason(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int)")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec(`explain format = 'plan_cache' select * from t where a=1`)
tk.MustExec(`explain format = 'plan_cache' select * from t where a=1`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustExec(`explain format = 'plan_cache' select * from (select * from t) tx`)
tk.MustQuery(`show warnings`).Check(testkit.Rows(`Warning 1105 skip non-prepared plan-cache: queries that have sub-queries are not supported`))
// no warning if disable this feature
tk.MustExec("set tidb_enable_non_prepared_plan_cache=0")
tk.MustExec(`explain format = 'plan_cache' select * from t where a+1=1`)
tk.MustQuery(`show warnings`).Check(testkit.Rows())
tk.MustExec(`explain format = 'plan_cache' select * from t t1, t t2`)
tk.MustQuery(`show warnings`).Check(testkit.Rows())
tk.MustExec(`explain format = 'plan_cache' select * from t where a in (select a from t)`)
tk.MustQuery(`show warnings`).Check(testkit.Rows())
}
func TestNonPreparedPlanCacheSysSchema(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec(`explain format='plan_cache' select address from PERFORMANCE_SCHEMA.tikv_profile_cpu`)
tk.MustQuery(`show warnings`).Check(testkit.Rows(`Warning 1105 skip non-prepared plan-cache: access tables in system schema`))
tk.MustExec(`use PERFORMANCE_SCHEMA`)
tk.MustExec(`explain format='plan_cache' select address from tikv_profile_cpu`)
tk.MustQuery(`show warnings`).Check(testkit.Rows(`Warning 1105 skip non-prepared plan-cache: access tables in system schema`))
}
func TestNonPreparedPlanCacheSQLMode(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int)")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec(`select * from t where a=1`)
tk.MustExec(`select * from t where a=1`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
tk.MustExec("set @@sql_mode=''") // cannot hit since sql-mode changed
tk.MustExec(`select * from t where a=1`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
tk.MustExec(`select * from t where a=1`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
}
func TestPreparedPlanCacheLargePlan(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int, b int, c varchar(2048))")
baseSQL := "select * from t, t t1 where t1.c=space(2048) and t.c=space(2048) and t.a=t1.b"
var baseSQLs []string
for i := 0; i < 30; i++ {
baseSQLs = append(baseSQLs, baseSQL)
}
tk.MustExec("prepare st from '" + strings.Join(baseSQLs[:15], " union all ") + "'")
tk.MustExec("execute st")
tk.MustExec("execute st")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) // less than 2MB threshold
tk.MustExec("prepare st from '" + strings.Join(baseSQLs[:30], " union all ") + "'")
tk.MustExec("execute st")
tk.MustExec("execute st")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0")) // large than 2MB threshold
tk.MustExec(fmt.Sprintf("set tidb_plan_cache_max_plan_size=%v", 1*size.GB))
tk.MustExec("execute st")
tk.MustExec("execute st")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1")) // less than 1GB threshold
}
func TestPreparedPlanCacheLongInList(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int, b int)")
genInList := func(l int) string {
var elements []string
for i := 0; i < l; i++ {
elements = append(elements, fmt.Sprintf("%v", i))
}
return "(" + strings.Join(elements, ",") + ")"
}
// the limitation is 200
tk.MustExec(fmt.Sprintf(`prepare st_199 from 'select * from t where a in %v'`, genInList(199)))
tk.MustExec(`execute st_199`)
tk.MustExec(`execute st_199`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustExec(fmt.Sprintf(`prepare st_201 from 'select * from t where a in %v'`, genInList(201)))
tk.MustExec(`execute st_201`)
tk.MustExec(`execute st_201`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
tk.MustExec(fmt.Sprintf(`prepare st_99_100 from 'select * from t where a in %v and b in %v'`, genInList(99), genInList(100)))
tk.MustExec(`execute st_99_100`)
tk.MustExec(`execute st_99_100`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustExec(fmt.Sprintf(`prepare st_100_101 from 'select * from t where a in %v and b in %v'`, genInList(100), genInList(101)))
tk.MustExec(`execute st_100_101`)
tk.MustExec(`execute st_100_101`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
}
func TestPreparedPlanCacheStats(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int)")
tk.MustExec("insert into t values (2)")
tk.MustExec(`prepare st from 'select * from t where a=?'`)
tk.MustExec(`set @a=1`)
tk.MustExec(`execute st using @a`)
tk.MustExec(`execute st using @a`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustExec("analyze table t")
tk.MustExec("set tidb_plan_cache_invalidation_on_fresh_stats = 0")
tk.MustExec(`execute st using @a`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustExec("set tidb_plan_cache_invalidation_on_fresh_stats = 1")
tk.MustExec(`execute st using @a`) // stats changes can affect prep cache hit if we turn on the variable
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
tk.MustExec(`execute st using @a`)
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
}
func TestNonPreparedPlanCacheStats(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int)")
tk.MustExec("insert into t values (2)")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec(`select * from t where a=1`)
tk.MustExec(`select * from t where a=1`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
tk.MustExec("analyze table t")
tk.MustExec(`select * from t where a=1`) // stats changes can affect non-prep cache hit
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
tk.MustExec(`select * from t where a=1`)
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
}
func TestNonPreparedPlanCacheHints(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int, index(a))")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec("select /*+ use_index(t, a) */ * from t where a=1")
tk.MustExec("select /*+ use_index(t, a) */ * from t where a=1") // cannot hit since it has a hint
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
tk.MustExec("select * from t where a=1")
tk.MustExec("select * from t where a=1")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
}
func TestNonPreparedPlanCacheParamInit(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table tx(a double, b int)")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec(`insert into tx values (3.0, 3)`)
tk.MustQuery("select json_object('k', a) = json_object('k', b) from tx").Check(testkit.Rows("1")) // no error
tk.MustQuery("select json_object('k', a) = json_object('k', b) from tx").Check(testkit.Rows("1"))
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
}
func TestNonPreparedPlanCacheBinding(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int, index(a))")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec("create binding for select * from t where a=1 using select /*+ use_index(t, a) */ * from t where a=1")
tk.MustExec("select * from t where a=1")
tk.MustExec("select * from t where a=1")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
tk.MustExec("drop binding for select * from t where a=1")
tk.MustExec("select * from t where a=1")
tk.MustExec("select * from t where a=1")
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("1"))
}
func TestNonPreparedPlanCacheWithExplain(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec("create table t(a int)")
tk.MustExec("set tidb_enable_non_prepared_plan_cache=1")
tk.MustExec("select * from t where a=1") // cache this plan
tk.MustQuery("explain select * from t where a=2").Check(testkit.Rows(
`TableReader_7 10.00 root data:Selection_6`,
`└─Selection_6 10.00 cop[tikv] eq(test.t.a, 2)`,
` └─TableFullScan_5 10000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
tk.MustQuery("explain format=verbose select * from t where a=2").Check(testkit.Rows(
`TableReader_7 10.00 168975.57 root data:Selection_6`,
`└─Selection_6 10.00 2534000.00 cop[tikv] eq(test.t.a, 2)`,
` └─TableFullScan_5 10000.00 2035000.00 cop[tikv] table:t keep order:false, stats:pseudo`))
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
tk.MustQuery("explain analyze select * from t where a=2").CheckAt([]int{0, 1, 2, 3}, [][]interface{}{
{"TableReader_7", "10.00", "0", "root"},
{"└─Selection_6", "10.00", "0", "cop[tikv]"},
{" └─TableFullScan_5", "10000.00", "0", "cop[tikv]"},
})
tk.MustQuery("select @@last_plan_from_cache").Check(testkit.Rows("0"))
}
func TestNonPreparedPlanCacheFastPointGet(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t (a int primary key, b int, unique key(b))`)
tk.MustExec(`set tidb_enable_non_prepared_plan_cache=1`)
// fast plans have a higher priority than non-prep cache plan
tk.MustQuery(`explain format='brief' select a from t where a in (1, 2)`).Check(testkit.Rows(
`Batch_Point_Get 2.00 root table:t handle:[1 2], keep order:false, desc:false`))
tk.MustQuery(`select a from t where a in (1, 2)`).Check(testkit.Rows())
tk.MustQuery(`select a from t where a in (1, 2)`).Check(testkit.Rows())
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
tk.MustQuery(`explain format='brief' select b from t where b = 1`).Check(testkit.Rows(
`Point_Get 1.00 root table:t, index:b(b) `))
tk.MustQuery(`select b from t where b = 1`).Check(testkit.Rows())
tk.MustQuery(`select b from t where b = 1`).Check(testkit.Rows())
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
}
func TestNonPreparedPlanCacheSetOperations(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t (a int)`)
tk.MustExec(`set tidb_enable_non_prepared_plan_cache=1`)
// queries with set operations cannot hit the cache
for _, q := range []string{
`select * from t union select * from t`,
`select * from t union distinct select * from t`,
`select * from t union all select * from t`,
`select * from t except select * from t`,
`select * from t intersect select * from t`,
} {
tk.MustQuery(q).Check(testkit.Rows())
tk.MustQuery(q).Check(testkit.Rows())
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
}
}
func TestNonPreparedPlanCacheInformationSchema(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_non_prepared_plan_cache=1")
p := parser.New()
is := infoschema.MockInfoSchema([]*model.TableInfo{plannercore.MockSignedTable(), plannercore.MockUnsignedTable()})
stmt, err := p.ParseOneStmt("select avg(a),avg(b),avg(c) from t", "", "")
require.NoError(t, err)
err = plannercore.Preprocess(context.Background(), tk.Session(), stmt, plannercore.WithPreprocessorReturn(&plannercore.PreprocessorReturn{InfoSchema: is}))
require.NoError(t, err) // no error
_, _, err = planner.Optimize(context.TODO(), tk.Session(), stmt, is)
require.NoError(t, err) // no error
_, _, err = planner.Optimize(context.TODO(), tk.Session(), stmt, is)
require.NoError(t, err) // no error
require.True(t, tk.Session().GetSessionVars().FoundInPlanCache)
}
func TestNonPreparedPlanCacheSpecialTables(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t (a int)`)
tk.MustExec(`create definer='root'@'localhost' view t_v as select * from t`)
tk.MustExec(`create table t_p (a int) partition by hash(a) partitions 4`)
tk.MustExec(`create temporary table t_t (a int)`)
tk.MustExec(`set tidb_enable_non_prepared_plan_cache=1`)
// queries that access partitioning tables, view, temporary tables or contain CTE cannot hit the cache.
for _, q := range []string{
`select * from t_v`,
`select * from t_p`,
`select * from t_t`,
`with t_cte as (select * from t) select * from t_cte`,
} {
tk.MustQuery(q).Check(testkit.Rows())
tk.MustQuery(q).Check(testkit.Rows())
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
}
}
func TestNonPreparedPlanParameterType(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t (a int, key(a))`)
tk.MustExec(`set tidb_enable_non_prepared_plan_cache=1`)
tk.MustQuery(`select * from t where a=1`).Check(testkit.Rows())
tk.MustQuery(`select * from t where a=1`).Check(testkit.Rows())
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
tk.MustQuery(`select * from t where a=1.1`).Check(testkit.Rows())
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
tk.MustExec(`explain format = 'plan_cache' select * from t where a=1.1`)
tk.MustQuery(`show warnings`).Check(testkit.Rows(`Warning 1105 skip non-prepared plan-cache: '1.1' may be converted to INT`))
tk.MustQuery(`select * from t where a='1'`).Check(testkit.Rows())
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("0"))
tk.MustExec(`explain format = 'plan_cache' select * from t where a='1'`)
tk.MustQuery(`show warnings`).Check(testkit.Rows(`Warning 1105 skip non-prepared plan-cache: '1' may be converted to INT`))
}
func TestIssue43852(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t6 (a date, b date, key(a))`)
tk.MustExec(`insert into t6 values ('2023-01-21', '2023-01-05')`)
tk.MustExec(`set tidb_enable_non_prepared_plan_cache=1`)
tk.MustQuery(`select * from t6 where a in (2015, '8')`).Check(testkit.Rows())
tk.MustQuery(`select * from t6 where a in (2009, '2023-01-21')`).Check(testkit.Rows(`2023-01-21 2023-01-05`))
tk.MustQuery(`select @@last_plan_from_cache`).Check(testkit.Rows("1"))
}
func TestNonPreparedPlanTypeRandomly(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec(`use test`)
tk.MustExec(`create table t1 (a int, b int, key(a))`)
tk.MustExec(`create table t2 (a varchar(8), b varchar(8), key(a))`)
tk.MustExec(`create table t3 (a double, b double, key(a))`)
tk.MustExec(`create table t4 (a decimal(4, 2), b decimal(4, 2), key(a))`)
tk.MustExec(`create table t5 (a year, b year, key(a))`)
tk.MustExec(`create table t6 (a date, b date, key(a))`)
tk.MustExec(`create table t7 (a datetime, b datetime, key(a))`)
n := 30
for i := 0; i < n; i++ {
tk.MustExec(fmt.Sprintf(`insert into t1 values (%v, %v)`, randNonPrepTypeVal(t, n, "int"), randNonPrepTypeVal(t, n, "int")))
tk.MustExec(fmt.Sprintf(`insert into t2 values (%v, %v)`, randNonPrepTypeVal(t, n, "varchar"), randNonPrepTypeVal(t, n, "varchar")))
tk.MustExec(fmt.Sprintf(`insert into t3 values (%v, %v)`, randNonPrepTypeVal(t, n, "double"), randNonPrepTypeVal(t, n, "double")))
tk.MustExec(fmt.Sprintf(`insert into t4 values (%v, %v)`, randNonPrepTypeVal(t, n, "decimal"), randNonPrepTypeVal(t, n, "decimal")))
// TODO: fix it later
//tk.MustExec(fmt.Sprintf(`insert into t5 values (%v, %v)`, randNonPrepTypeVal(t, n, "year"), randNonPrepTypeVal(t, n, "year")))
tk.MustExec(fmt.Sprintf(`insert into t6 values (%v, %v)`, randNonPrepTypeVal(t, n, "date"), randNonPrepTypeVal(t, n, "date")))
tk.MustExec(fmt.Sprintf(`insert into t7 values (%v, %v)`, randNonPrepTypeVal(t, n, "datetime"), randNonPrepTypeVal(t, n, "datetime")))
}
for i := 0; i < 200; i++ {
q := fmt.Sprintf(`select * from t%v where %v`, rand.Intn(7)+1, randNonPrepFilter(t, n))
tk.MustExec(`set tidb_enable_non_prepared_plan_cache=1`)
r0 := tk.MustQuery(q).Sort() // the first execution