forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dag_plan_test.go
958 lines (924 loc) · 33.3 KB
/
dag_plan_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
// Copyright 2017 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package plan_test
import (
. "github.com/pingcap/check"
"github.com/pingcap/tidb"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/plan"
"github.com/pingcap/tidb/util/testleak"
)
var _ = Suite(&testPlanSuite{})
type testPlanSuite struct {
*parser.Parser
}
func (s *testPlanSuite) SetUpSuite(c *C) {
s.Parser = parser.New()
}
func (s *testPlanSuite) TestDAGPlanBuilderSimpleCase(c *C) {
store, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer store.Close()
se, err := tidb.CreateSession(store)
c.Assert(err, IsNil)
defer func() {
testleak.AfterTest(c)()
}()
tests := []struct {
sql string
best string
}{
// Test unready index hint.
{
sql: "select * from t t1 use index(e)",
best: "TableReader(Table(t))",
},
// Test index hint.
{
sql: "select * from t t1 use index(c_d_e)",
best: "IndexLookUp(Index(t.c_d_e)[[<nil>,+inf]], Table(t))",
},
// Test ts + Sort vs. DoubleRead + filter.
{
sql: "select a from t where a between 1 and 2 order by c",
best: "TableReader(Table(t))->Sort->Projection",
},
// Test DNF condition + Double Read.
// FIXME: Some bugs still exist in selectivity.
//{
// sql: "select * from t where (t.c > 0 and t.c < 1) or (t.c > 2 and t.c < 3) or (t.c > 4 and t.c < 5) or (t.c > 6 and t.c < 7) or (t.c > 9 and t.c < 10)",
// best: "IndexLookUp(Index(t.c_d_e)[(0 +inf,1 <nil>) (2 +inf,3 <nil>) (4 +inf,5 <nil>) (6 +inf,7 <nil>) (9 +inf,10 <nil>)], Table(t))",
//},
// Test TopN to table branch in double read.
{
sql: "select * from t where t.c = 1 and t.e = 1 order by t.b limit 1",
best: "IndexLookUp(Index(t.c_d_e)[[1,1]]->Sel([eq(test.t.e, 1)]), Table(t)->TopN([test.t.b],0,1))->TopN([test.t.b],0,1)",
},
// Test Null Range
{
sql: "select * from t where t.c is null",
best: "IndexLookUp(Index(t.c_d_e)[[<nil>,<nil>]], Table(t))",
},
// Test TopN to index branch in double read.
{
sql: "select * from t where t.c = 1 and t.e = 1 order by t.e limit 1",
best: "IndexLookUp(Index(t.c_d_e)[[1,1]]->Sel([eq(test.t.e, 1)])->TopN([test.t.e],0,1), Table(t))->TopN([test.t.e],0,1)",
},
// Test TopN to Limit in double read.
{
sql: "select * from t where t.c = 1 and t.e = 1 order by t.d limit 1",
best: "IndexLookUp(Index(t.c_d_e)[[1,1]]->Sel([eq(test.t.e, 1)])->Limit, Table(t))->Limit",
},
// Test TopN to Limit in index single read.
{
sql: "select c from t where t.c = 1 and t.e = 1 order by t.d limit 1",
best: "IndexReader(Index(t.c_d_e)[[1,1]]->Sel([eq(test.t.e, 1)])->Limit)->Limit->Projection",
},
// Test TopN to Limit in table single read.
{
sql: "select c from t order by t.a limit 1",
best: "TableReader(Table(t)->Limit)->Limit->Projection",
},
// Test TopN push down in table single read.
{
sql: "select c from t order by t.a + t.b limit 1",
best: "TableReader(Table(t)->TopN([plus(test.t.a, test.t.b)],0,1))->TopN([plus(test.t.a, test.t.b)],0,1)->Projection",
},
// Test Limit push down in table single read.
{
sql: "select c from t limit 1",
best: "TableReader(Table(t)->Limit)->Limit",
},
// Test Limit push down in index single read.
{
sql: "select c from t where c = 1 limit 1",
best: "IndexReader(Index(t.c_d_e)[[1,1]]->Limit)->Limit",
},
// Test index single read and Selection.
{
sql: "select c from t where c = 1",
best: "IndexReader(Index(t.c_d_e)[[1,1]])",
},
// Test index single read and Sort.
{
sql: "select c from t order by c",
best: "IndexReader(Index(t.c_d_e)[[<nil>,+inf]])",
},
// Test index single read and Sort.
{
sql: "select c from t where c = 1 order by e",
best: "IndexReader(Index(t.c_d_e)[[1,1]])->Sort->Projection",
},
// Test Limit push down in double single read.
{
sql: "select c, b from t where c = 1 limit 1",
best: "IndexLookUp(Index(t.c_d_e)[[1,1]]->Limit, Table(t))->Limit->Projection",
},
// Test Selection + Limit push down in double single read.
{
sql: "select c, b from t where c = 1 and e = 1 and b = 1 limit 1",
best: "IndexLookUp(Index(t.c_d_e)[[1,1]]->Sel([eq(test.t.e, 1)]), Table(t)->Sel([eq(test.t.b, 1)])->Limit)->Limit->Projection",
},
// Test Order by multi columns.
{
sql: "select c from t where c = 1 order by d, c",
best: "IndexReader(Index(t.c_d_e)[[1,1]])->Sort->Projection",
},
// Test for index with length.
{
sql: "select c_str from t where e_str = '1' order by d_str, c_str",
best: "IndexLookUp(Index(t.e_d_c_str_prefix)[[1,1]], Table(t))->Sort->Projection",
},
// Test PK in index single read.
{
sql: "select c from t where t.c = 1 and t.a = 1 order by t.d limit 1",
best: "IndexReader(Index(t.c_d_e)[[1,1]]->Sel([eq(test.t.a, 1)])->Limit)->Limit->Projection",
},
// Test composed index.
// FIXME: The TopN didn't be pushed.
{
sql: "select c from t where t.c = 1 and t.d = 1 order by t.a limit 1",
best: "IndexReader(Index(t.c_d_e)[[1 1,1 1]])->TopN([test.t.a],0,1)->Projection",
},
// Test PK in index double read.
{
sql: "select * from t where t.c = 1 and t.a = 1 order by t.d limit 1",
best: "IndexLookUp(Index(t.c_d_e)[[1,1]]->Sel([eq(test.t.a, 1)])->Limit, Table(t))->Limit",
},
// Test index filter condition push down.
{
sql: "select * from t use index(e_d_c_str_prefix) where t.c_str = 'abcdefghijk' and t.d_str = 'd' and t.e_str = 'e'",
best: "IndexLookUp(Index(t.e_d_c_str_prefix)[[e d [97 98 99 100 101 102 103 104 105 106],e d [97 98 99 100 101 102 103 104 105 106]]], Table(t)->Sel([eq(test.t.c_str, abcdefghijk)]))",
},
}
for _, tt := range tests {
comment := Commentf("for %s", tt.sql)
stmt, err := s.ParseOneStmt(tt.sql, "", "")
c.Assert(err, IsNil, comment)
err = se.NewTxn()
c.Assert(err, IsNil)
is, err := plan.MockResolve(stmt)
c.Assert(err, IsNil)
p, err := plan.Optimize(se, stmt, is)
c.Assert(err, IsNil)
c.Assert(plan.ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
}
func (s *testPlanSuite) TestDAGPlanBuilderJoin(c *C) {
store, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer store.Close()
se, err := tidb.CreateSession(store)
c.Assert(err, IsNil)
defer func() {
testleak.AfterTest(c)()
}()
tests := []struct {
sql string
best string
}{
{
sql: "select * from t t1 join t t2 on t1.a = t2.c_str",
best: "LeftHashJoin{TableReader(Table(t))->Projection->TableReader(Table(t))->Projection}(cast(t1.a),cast(t2.c_str))->Projection",
},
{
sql: "select * from t t1 join t t2 on t1.b = t2.a",
best: "LeftHashJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.b,t2.a)",
},
{
sql: "select * from t t1 join t t2 on t1.a = t2.a join t t3 on t1.a = t3.a",
best: "MergeJoin{MergeJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.a,t2.a)->TableReader(Table(t))}(t1.a,t3.a)",
},
{
sql: "select * from t t1 join t t2 on t1.a = t2.a join t t3 on t1.b = t3.a",
best: "LeftHashJoin{MergeJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.a,t2.a)->TableReader(Table(t))}(t1.b,t3.a)",
},
{
sql: "select * from t t1 join t t2 on t1.b = t2.a order by t1.a",
best: "LeftHashJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.b,t2.a)->Sort",
},
{
sql: "select * from t t1 join t t2 on t1.b = t2.a order by t1.a limit 1",
best: "IndexJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.b,t2.a)->Limit",
},
{
sql: "select * from t t1 join t t2 on t1.b = t2.a and t1.c = 1 and t1.d = 1 and t1.e = 1 order by t1.a limit 1",
best: "IndexJoin{IndexLookUp(Index(t.c_d_e)[[1 1 1,1 1 1]], Table(t))->TableReader(Table(t))}(t1.b,t2.a)->TopN([t1.a],0,1)",
},
{
sql: "select * from t t1 join t t2 on t1.b = t2.b join t t3 on t1.b = t3.b",
best: "LeftHashJoin{LeftHashJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.b,t2.b)->TableReader(Table(t))}(t1.b,t3.b)",
},
{
sql: "select * from t t1 join t t2 on t1.a = t2.a order by t1.a",
best: "MergeJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.a,t2.a)",
},
{
sql: "select * from t t1 left outer join t t2 on t1.a = t2.a right outer join t t3 on t1.a = t3.a",
best: "MergeJoin{MergeJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.a,t2.a)->TableReader(Table(t))}(t1.a,t3.a)",
},
{
sql: "select * from t t1 join t t2 on t1.a = t2.a join t t3 on t1.a = t3.a and t1.b = 1 and t3.c = 1",
best: "LeftHashJoin{IndexJoin{TableReader(Table(t)->Sel([eq(t1.b, 1)]))->TableReader(Table(t))}(t1.a,t2.a)->IndexLookUp(Index(t.c_d_e)[[1,1]], Table(t))}(t1.a,t3.a)",
},
{
sql: "select * from t where t.c in (select b from t s where s.a = t.a)",
best: "SemiJoin{TableReader(Table(t))->TableReader(Table(t))}(test.t.a,s.a)(test.t.c,s.b)",
},
{
sql: "select t.c in (select b from t s where s.a = t.a) from t",
best: "SemiJoinWithAux{TableReader(Table(t))->TableReader(Table(t))}(test.t.a,s.a)(test.t.c,s.b)->Projection",
},
// Test Single Merge Join.
// Merge Join will no longer enforce a sort. If a hint doesn't take effect, we will choose other types of join.
{
sql: "select /*+ TIDB_SMJ(t1,t2)*/ * from t t1, t t2 where t1.a = t2.b",
best: "LeftHashJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.a,t2.b)",
},
// Test Single Merge Join + Sort.
{
sql: "select /*+ TIDB_SMJ(t1,t2)*/ * from t t1, t t2 where t1.a = t2.a order by t2.a",
best: "MergeJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.a,t2.a)",
},
// Test Single Merge Join + Sort + desc.
{
sql: "select /*+ TIDB_SMJ(t1,t2)*/ * from t t1, t t2 where t1.a = t2.a order by t2.a desc",
best: "MergeJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.a,t2.a)->Sort",
},
// Test Multi Merge Join.
{
sql: "select /*+ TIDB_SMJ(t1,t2,t3)*/ * from t t1, t t2, t t3 where t1.a = t2.a and t2.a = t3.a",
best: "MergeJoin{MergeJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.a,t2.a)->TableReader(Table(t))}(t2.a,t3.a)",
},
// Test Multi Merge Join with multi keys.
// TODO: More tests should be added.
{
sql: "select /*+ TIDB_SMJ(t1,t2,t3)*/ * from t t1, t t2, t t3 where t1.c = t2.c and t1.d = t2.d and t3.c = t1.c and t3.d = t1.d",
best: "MergeJoin{MergeJoin{IndexLookUp(Index(t.c_d_e)[[<nil>,+inf]], Table(t))->IndexLookUp(Index(t.c_d_e)[[<nil>,+inf]], Table(t))}(t1.c,t2.c)(t1.d,t2.d)->IndexLookUp(Index(t.c_d_e)[[<nil>,+inf]], Table(t))}(t1.c,t3.c)(t1.d,t3.d)",
},
// Test Multi Merge Join + Outer Join.
{
sql: "select /*+ TIDB_SMJ(t1,t2,t3)*/ * from t t1 left outer join t t2 on t1.a = t2.a left outer join t t3 on t2.a = t3.a",
best: "MergeJoin{MergeJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.a,t2.a)->Sort->TableReader(Table(t))}(t2.a,t3.a)",
},
{
sql: "select /*+ TIDB_SMJ(t1,t2,t3)*/ * from t t1 left outer join t t2 on t1.a = t2.a left outer join t t3 on t1.a = t3.a",
best: "MergeJoin{MergeJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.a,t2.a)->TableReader(Table(t))}(t1.a,t3.a)",
},
// Test Index Join + TableScan.
{
sql: "select /*+ TIDB_INLJ(t1, t2) */ * from t t1, t t2 where t1.a = t2.a",
best: "IndexJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.a,t2.a)",
},
// Test Index Join + DoubleRead.
{
sql: "select /*+ TIDB_INLJ(t1, t2) */ * from t t1, t t2 where t1.a = t2.c",
best: "IndexJoin{TableReader(Table(t))->IndexLookUp(Index(t.c_d_e)[[<nil>,+inf]], Table(t))}(t1.a,t2.c)",
},
// Test Index Join + SingleRead.
{
sql: "select /*+ TIDB_INLJ(t1, t2) */ t1.a , t2.a from t t1, t t2 where t1.a = t2.c",
best: "IndexJoin{TableReader(Table(t))->IndexReader(Index(t.c_d_e)[[<nil>,+inf]])}(t1.a,t2.c)->Projection",
},
// Test Index Join + Order by.
{
sql: "select /*+ TIDB_INLJ(t1, t2) */ t1.a, t2.a from t t1, t t2 where t1.a = t2.a order by t1.c",
best: "IndexJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.a,t2.a)->Sort->Projection",
},
// Test Index Join + Order by.
{
sql: "select /*+ TIDB_INLJ(t1, t2) */ t1.a, t2.a from t t1, t t2 where t1.a = t2.a order by t2.c",
best: "IndexJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.a,t2.a)->Sort->Projection",
},
// Test Index Join + TableScan + Rotate.
{
sql: "select /*+ TIDB_INLJ(t2) */ t1.a , t2.a from t t1, t t2 where t1.a = t2.c",
best: "IndexJoin{TableReader(Table(t))->TableReader(Table(t))}(t2.c,t1.a)->Projection",
},
// Test Index Join + OuterJoin + TableScan.
{
sql: "select /*+ TIDB_INLJ(t1, t2) */ * from t t1 left outer join t t2 on t1.a = t2.a and t2.b < 1",
best: "IndexJoin{TableReader(Table(t))->TableReader(Table(t)->Sel([lt(t2.b, 1)]))}(t1.a,t2.a)",
},
// Test Index Join failed.
{
sql: "select /*+ TIDB_INLJ(t1, t2) */ * from t t1 left outer join t t2 on t1.a = t2.b",
best: "LeftHashJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.a,t2.b)",
},
// Test Index Join failed.
{
sql: "select /*+ TIDB_INLJ(t1) */ * from t t1 right outer join t t2 on t1.a = t2.b",
best: "RightHashJoin{TableReader(Table(t))->TableReader(Table(t))}(t1.a,t2.b)",
},
}
for _, tt := range tests {
comment := Commentf("for %s", tt.sql)
stmt, err := s.ParseOneStmt(tt.sql, "", "")
c.Assert(err, IsNil, comment)
is, err := plan.MockResolve(stmt)
c.Assert(err, IsNil)
p, err := plan.Optimize(se, stmt, is)
c.Assert(err, IsNil)
c.Assert(plan.ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
}
func (s *testPlanSuite) TestDAGPlanBuilderSubquery(c *C) {
store, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer store.Close()
se, err := tidb.CreateSession(store)
c.Assert(err, IsNil)
defer func() {
testleak.AfterTest(c)()
}()
tests := []struct {
sql string
best string
}{
// Test join key with cast.
{
sql: "select * from t where exists (select s.a from t s having sum(s.a) = t.a )",
best: "SemiJoin{TableReader(Table(t))->Projection->TableReader(Table(t)->HashAgg)->HashAgg}(cast(test.t.a),sel_agg_1)->Projection",
},
{
sql: "select * from t where exists (select s.a from t s having sum(s.a) = t.a ) order by t.a",
best: "SemiJoin{TableReader(Table(t))->Projection->TableReader(Table(t)->HashAgg)->HashAgg}(cast(test.t.a),sel_agg_1)->Projection",
},
// FIXME: Report error by resolver.
//{
// sql: "select * from t where exists (select s.a from t s having s.a = t.a ) order by t.a",
// best: "SemiJoin{TableReader(Table(t))->Projection->TableReader(Table(t)->HashAgg)->HashAgg}(cast(test.t.a),sel_agg_1)->Projection->Sort",
//},
{
sql: "select * from t where a in (select s.a from t s) order by t.a",
best: "SemiJoin{TableReader(Table(t))->TableReader(Table(t))}(test.t.a,s.a)",
},
// Test Nested sub query.
{
sql: "select * from t where exists (select s.a from t s where s.c in (select c from t as k where k.d = s.d) having sum(s.a) = t.a )",
best: "SemiJoin{TableReader(Table(t))->Projection->SemiJoin{TableReader(Table(t))->TableReader(Table(t))}(s.d,k.d)(s.c,k.c)->HashAgg}(cast(test.t.a),sel_agg_1)->Projection",
},
// Test Semi Join + Order by.
{
sql: "select * from t where a in (select a from t) order by b",
best: "SemiJoin{TableReader(Table(t))->TableReader(Table(t))}(test.t.a,test.t.a)->Sort",
},
// Test Apply.
{
sql: "select t.c in (select count(*) from t s , t t1 where s.a = t.a and s.a = t1.a) from t",
best: "Apply{TableReader(Table(t))->MergeJoin{TableReader(Table(t))->Sel([eq(s.a, test.t.a)])->TableReader(Table(t))}(s.a,t1.a)->HashAgg}->Projection",
},
{
sql: "select (select count(*) from t s , t t1 where s.a = t.a and s.a = t1.a) from t",
best: "Apply{TableReader(Table(t))->MergeJoin{TableReader(Table(t))->Sel([eq(s.a, test.t.a)])->TableReader(Table(t))}(s.a,t1.a)->HashAgg}->Projection",
},
{
sql: "select (select count(*) from t s , t t1 where s.a = t.a and s.a = t1.a) from t order by t.a",
best: "Apply{TableReader(Table(t))->MergeJoin{TableReader(Table(t))->Sel([eq(s.a, test.t.a)])->TableReader(Table(t))}(s.a,t1.a)->HashAgg}->Projection",
},
}
for _, tt := range tests {
comment := Commentf("for %s", tt.sql)
stmt, err := s.ParseOneStmt(tt.sql, "", "")
c.Assert(err, IsNil, comment)
is, err := plan.MockResolve(stmt)
c.Assert(err, IsNil)
p, err := plan.Optimize(se, stmt, is)
c.Assert(err, IsNil)
c.Assert(plan.ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
}
func (s *testPlanSuite) TestDAGPlanTopN(c *C) {
store, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer store.Close()
se, err := tidb.CreateSession(store)
c.Assert(err, IsNil)
defer func() {
testleak.AfterTest(c)()
}()
tests := []struct {
sql string
best string
}{
{
sql: "select * from t t1 left join t t2 on t1.b = t2.b left join t t3 on t2.b = t3.b order by t1.a limit 1",
best: "LeftHashJoin{LeftHashJoin{TableReader(Table(t)->Limit)->TableReader(Table(t))}(t1.b,t2.b)->TableReader(Table(t))}(t2.b,t3.b)->TopN([t1.a],0,1)",
},
{
sql: "select * from t t1 left join t t2 on t1.b = t2.b left join t t3 on t2.b = t3.b order by t1.b limit 1",
best: "LeftHashJoin{LeftHashJoin{TableReader(Table(t)->TopN([t1.b],0,1))->TableReader(Table(t))}(t1.b,t2.b)->TableReader(Table(t))}(t2.b,t3.b)->TopN([t1.b],0,1)",
},
{
sql: "select * from t t1 left join t t2 on t1.b = t2.b left join t t3 on t2.b = t3.b limit 1",
best: "LeftHashJoin{LeftHashJoin{TableReader(Table(t)->Limit)->TableReader(Table(t))}(t1.b,t2.b)->TableReader(Table(t))}(t2.b,t3.b)->Limit",
},
{
sql: "select * from t where b = 1 and c = 1 order by c limit 1",
best: "IndexLookUp(Index(t.c_d_e)[[1,1]], Table(t)->Sel([eq(test.t.b, 1)]))->Limit",
},
{
sql: "select * from t where c = 1 order by c limit 1",
best: "IndexLookUp(Index(t.c_d_e)[[1,1]]->Limit, Table(t))->Limit",
},
{
sql: "select * from t order by a limit 1",
best: "TableReader(Table(t)->Limit)->Limit",
},
{
sql: "select c from t order by c limit 1",
best: "IndexReader(Index(t.c_d_e)[[<nil>,+inf]]->Limit)->Limit",
},
}
for _, tt := range tests {
comment := Commentf("for %s", tt.sql)
stmt, err := s.ParseOneStmt(tt.sql, "", "")
c.Assert(err, IsNil, comment)
is, err := plan.MockResolve(stmt)
c.Assert(err, IsNil)
p, err := plan.Optimize(se, stmt, is)
c.Assert(err, IsNil)
c.Assert(plan.ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
}
func (s *testPlanSuite) TestDAGPlanBuilderBasePhysicalPlan(c *C) {
store, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer store.Close()
se, err := tidb.CreateSession(store)
c.Assert(err, IsNil)
defer func() {
testleak.AfterTest(c)()
}()
tests := []struct {
sql string
best string
}{
// Test for update.
{
sql: "select * from t order by b limit 1 for update",
// TODO: This is not reasonable. Mysql do like this because the limit of InnoDB, should TiDB keep consistency with MySQL?
best: "TableReader(Table(t))->Lock->TopN([test.t.b],0,1)",
},
// Test complex update.
{
sql: "update t set a = 5 where b < 1 order by d limit 1",
best: "TableReader(Table(t)->Sel([lt(test.t.b, 1)])->TopN([test.t.d],0,1))->TopN([test.t.d],0,1)->*plan.Update",
},
// Test simple update.
{
sql: "update t set a = 5",
best: "TableReader(Table(t))->*plan.Update",
},
// TODO: Test delete/update with join.
// Test complex delete.
{
sql: "delete from t where b < 1 order by d limit 1",
best: "TableReader(Table(t)->Sel([lt(test.t.b, 1)])->TopN([test.t.d],0,1))->TopN([test.t.d],0,1)->*plan.Delete",
},
// Test simple delete.
{
sql: "delete from t",
best: "TableReader(Table(t))->*plan.Delete",
},
// Test complex insert.
{
sql: "insert into t select * from t where b < 1 order by d limit 1",
best: "TableReader(Table(t)->Sel([lt(test.t.b, 1)])->TopN([test.t.d],0,1))->TopN([test.t.d],0,1)->*plan.Insert",
},
// Test simple insert.
{
sql: "insert into t values(0,0,0,0,0,0,0)",
best: "*plan.Insert",
},
// Test dual.
{
sql: "select 1",
best: "Dual->Projection",
},
{
sql: "select * from t where false",
best: "Dual",
},
// Test show.
{
sql: "show tables",
best: "*plan.Show",
},
}
for _, tt := range tests {
comment := Commentf("for %s", tt.sql)
stmt, err := s.ParseOneStmt(tt.sql, "", "")
c.Assert(err, IsNil, comment)
is, err := plan.MockResolve(stmt)
c.Assert(err, IsNil)
p, err := plan.Optimize(se, stmt, is)
c.Assert(err, IsNil)
c.Assert(plan.ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
}
func (s *testPlanSuite) TestDAGPlanBuilderUnion(c *C) {
store, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer store.Close()
se, err := tidb.CreateSession(store)
c.Assert(err, IsNil)
defer func() {
testleak.AfterTest(c)()
}()
tests := []struct {
sql string
best string
}{
// Test simple union.
{
sql: "select * from t union all select * from t",
best: "UnionAll{TableReader(Table(t))->TableReader(Table(t))}",
},
// Test Order by + Union.
{
sql: "select * from t union all (select * from t) order by a ",
best: "UnionAll{TableReader(Table(t))->TableReader(Table(t))}->Sort",
},
// Test Limit + Union.
{
sql: "select * from t union all (select * from t) limit 1",
best: "UnionAll{TableReader(Table(t)->Limit)->TableReader(Table(t)->Limit)}->Limit",
},
// Test TopN + Union.
{
sql: "select a from t union all (select c from t) order by a limit 1",
best: "UnionAll{TableReader(Table(t)->Limit)->IndexReader(Index(t.c_d_e)[[<nil>,+inf]]->Limit)}->TopN([a],0,1)",
},
}
for _, tt := range tests {
comment := Commentf("for %s", tt.sql)
stmt, err := s.ParseOneStmt(tt.sql, "", "")
c.Assert(err, IsNil, comment)
is, err := plan.MockResolve(stmt)
c.Assert(err, IsNil)
p, err := plan.Optimize(se, stmt, is)
c.Assert(err, IsNil)
c.Assert(plan.ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
}
func (s *testPlanSuite) TestDAGPlanBuilderUnionScan(c *C) {
store, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer store.Close()
se, err := tidb.CreateSession(store)
c.Assert(err, IsNil)
defer func() {
testleak.AfterTest(c)()
}()
tests := []struct {
sql string
best string
}{
// Read table.
{
sql: "select * from t",
best: "TableReader(Table(t))->UnionScan([])",
},
{
sql: "select * from t where b = 1",
best: "TableReader(Table(t)->Sel([eq(test.t.b, 1)]))->UnionScan([eq(test.t.b, 1)])",
},
{
sql: "select * from t where a = 1",
best: "TableReader(Table(t))->UnionScan([eq(test.t.a, 1)])",
},
{
sql: "select * from t where a = 1 order by a",
best: "TableReader(Table(t))->UnionScan([eq(test.t.a, 1)])",
},
{
sql: "select * from t where a = 1 order by b",
best: "TableReader(Table(t))->UnionScan([eq(test.t.a, 1)])->Sort",
},
{
sql: "select * from t where a = 1 limit 1",
best: "TableReader(Table(t))->UnionScan([eq(test.t.a, 1)])->Limit",
},
{
sql: "select * from t where c = 1",
best: "IndexLookUp(Index(t.c_d_e)[[1,1]], Table(t))->UnionScan([eq(test.t.c, 1)])",
},
{
sql: "select c from t where c = 1",
best: "IndexReader(Index(t.c_d_e)[[1,1]])->UnionScan([eq(test.t.c, 1)])",
},
}
for _, tt := range tests {
comment := Commentf("for %s", tt.sql)
stmt, err := s.ParseOneStmt(tt.sql, "", "")
c.Assert(err, IsNil, comment)
is, err := plan.MockResolve(stmt)
c.Assert(err, IsNil)
err = se.NewTxn()
c.Assert(err, IsNil)
// Make txn not read only.
se.Txn().Set(nil, nil)
p, err := plan.Optimize(se, stmt, is)
c.Assert(err, IsNil)
c.Assert(plan.ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
}
func (s *testPlanSuite) TestDAGPlanBuilderAgg(c *C) {
store, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer store.Close()
se, err := tidb.CreateSession(store)
c.Assert(err, IsNil)
defer func() {
testleak.AfterTest(c)()
}()
tests := []struct {
sql string
best string
}{
// Test agg + table.
{
sql: "select sum(a), avg(b + c) from t group by d",
best: "TableReader(Table(t)->HashAgg)->HashAgg",
},
{
sql: "select sum(distinct a), avg(b + c) from t group by d",
best: "TableReader(Table(t))->HashAgg",
},
// Test agg + index single.
{
sql: "select sum(e), avg(e + c) from t where c = 1 group by d",
best: "IndexReader(Index(t.c_d_e)[[1,1]]->HashAgg)->HashAgg",
},
// Test agg + index double.
{
sql: "select sum(e), avg(b + c) from t where c = 1 and e = 1 group by d",
best: "IndexLookUp(Index(t.c_d_e)[[1,1]]->Sel([eq(test.t.e, 1)]), Table(t)->HashAgg)->HashAgg",
},
// Test agg + order.
{
sql: "select sum(e) as k, avg(b + c) from t where c = 1 and b = 1 and e = 1 group by d order by k",
best: "IndexLookUp(Index(t.c_d_e)[[1,1]]->Sel([eq(test.t.e, 1)]), Table(t)->Sel([eq(test.t.b, 1)])->HashAgg)->HashAgg->Sort",
},
// Test agg can't push down.
{
sql: "select sum(to_base64(e)) from t where c = 1",
best: "IndexReader(Index(t.c_d_e)[[1,1]])->HashAgg",
},
}
for _, tt := range tests {
comment := Commentf("for %s", tt.sql)
stmt, err := s.ParseOneStmt(tt.sql, "", "")
c.Assert(err, IsNil, comment)
is, err := plan.MockResolve(stmt)
c.Assert(err, IsNil)
p, err := plan.Optimize(se, stmt, is)
c.Assert(err, IsNil)
c.Assert(plan.ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
}
func (s *testPlanSuite) TestRefine(c *C) {
store, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
defer store.Close()
se, err := tidb.CreateSession(store)
c.Assert(err, IsNil)
defer func() {
testleak.AfterTest(c)()
}()
tests := []struct {
sql string
best string
}{
{
sql: "select a from t where c is not null",
best: "TableReader(Table(t)->Sel([not(isnull(test.t.c))]))->Projection",
},
{
sql: "select a from t where c >= 4",
best: "IndexReader(Index(t.c_d_e)[[4,+inf]])->Projection",
},
{
sql: "select a from t where c <= 4",
best: "IndexReader(Index(t.c_d_e)[[-inf,4]])->Projection",
},
{
sql: "select a from t where c = 4 and d = 5 and e = 6",
best: "IndexReader(Index(t.c_d_e)[[4 5 6,4 5 6]])->Projection",
},
{
sql: "select a from t where d = 4 and c = 5",
best: "IndexReader(Index(t.c_d_e)[[5 4,5 4]])->Projection",
},
{
sql: "select a from t where c = 4 and e < 5",
best: "IndexReader(Index(t.c_d_e)[[4,4]]->Sel([lt(test.t.e, 5)]))->Projection",
},
{
sql: "select a from t where c = 4 and d <= 5 and d > 3",
best: "IndexReader(Index(t.c_d_e)[(4 3,4 5]])->Projection",
},
{
sql: "select a from t where d <= 5 and d > 3",
best: "TableReader(Table(t)->Sel([le(test.t.d, 5) gt(test.t.d, 3)]))->Projection",
},
{
sql: "select a from t where c between 1 and 2",
best: "IndexReader(Index(t.c_d_e)[[1,2]])->Projection",
},
{
sql: "select a from t where c not between 1 and 2",
best: "IndexReader(Index(t.c_d_e)[[-inf,1) (2,+inf]])->Projection",
},
{
sql: "select a from t where c <= 5 and c >= 3 and d = 1",
best: "IndexReader(Index(t.c_d_e)[[3,5]]->Sel([eq(test.t.d, 1)]))->Projection",
},
{
sql: "select a from t where c = 1 or c = 2 or c = 3",
best: "IndexReader(Index(t.c_d_e)[[1,1] [2,2] [3,3]])->Projection",
},
{
sql: "select b from t where c = 1 or c = 2 or c = 3 or c = 4 or c = 5",
best: "IndexLookUp(Index(t.c_d_e)[[1,1] [2,2] [3,3] [4,4] [5,5]], Table(t))->Projection",
},
{
sql: "select a from t where c = 5",
best: "IndexReader(Index(t.c_d_e)[[5,5]])->Projection",
},
{
sql: "select a from t where c = 5 and b = 1",
best: "IndexLookUp(Index(t.c_d_e)[[5,5]], Table(t)->Sel([eq(test.t.b, 1)]))->Projection",
},
{
sql: "select a from t where not a",
best: "TableReader(Table(t)->Sel([not(test.t.a)]))",
},
{
sql: "select a from t where c in (1)",
best: "IndexReader(Index(t.c_d_e)[[1,1]])->Projection",
},
{
sql: "select a from t where c in ('1')",
best: "IndexReader(Index(t.c_d_e)[[1,1]])->Projection",
},
{
sql: "select a from t where c = 1.0",
best: "IndexReader(Index(t.c_d_e)[[1,1]])->Projection",
},
{
sql: "select a from t where c in (1) and d > 3",
best: "IndexReader(Index(t.c_d_e)[(1 3,1 +inf]])->Projection",
},
// TODO: func in is rewritten to DNF which will influence the extraction behavior of accessCondition.
//{
// sql: "select a from t where c in (1, 2, 3) and (d > 3 and d < 4 or d > 5 and d < 6)",
// best: "Index(t.c_d_e)[(1 3 +inf,1 4 <nil>) (1 5 +inf,1 6 <nil>) (2 3 +inf,2 4 <nil>) (2 5 +inf,2 6 <nil>) (3 3 +inf,3 4 <nil>) (3 5 +inf,3 6 <nil>)]->Projection",
//},
{
sql: "select a from t where c in (1, 2, 3)",
best: "IndexReader(Index(t.c_d_e)[[1,1] [2,2] [3,3]])->Projection",
},
// TODO: func in is rewritten to DNF which will influence the extraction behavior of accessCondition.
//{
// sql: "select a from t where c in (1, 2, 3) and d in (1,2) and e = 1",
// best: "Index(t.c_d_e)[[1 1 1,1 1 1] [1 2 1,1 2 1] [2 1 1,2 1 1] [2 2 1,2 2 1] [3 1 1,3 1 1] [3 2 1,3 2 1]]->Projection",
//},
{
sql: "select a from t where d in (1, 2, 3)",
best: "TableReader(Table(t)->Sel([or(or(eq(test.t.d, 1), eq(test.t.d, 2)), eq(test.t.d, 3))]))->Projection",
},
// TODO: func in is rewritten to DNF which will influence the extraction behavior of accessCondition.
//{
// sql: "select a from t where c not in (1)",
// best: "Table(t)->Projection",
//},
// test like
{
sql: "select a from t use index(c_d_e) where c != 1",
best: "IndexReader(Index(t.c_d_e)[[-inf,1) (1,+inf]])->Projection",
},
{
sql: "select a from t where c_str like ''",
best: "IndexReader(Index(t.c_d_e_str)[[,]])->Projection",
},
{
sql: "select a from t where c_str like 'abc'",
best: "IndexReader(Index(t.c_d_e_str)[[abc,abc]])->Projection",
},
{
sql: "select a from t where c_str not like 'abc'",
best: "TableReader(Table(t)->Sel([not(like(test.t.c_str, abc, 92))]))->Projection",
},
{
sql: "select a from t where not (c_str like 'abc' or c_str like 'abd')",
best: "TableReader(Table(t)->Sel([and(not(like(test.t.c_str, abc, 92)), not(like(test.t.c_str, abd, 92)))]))->Projection",
},
{
sql: "select a from t where c_str like '_abc'",
best: "TableReader(Table(t))->Sel([like(test.t.c_str, _abc, 92)])->Projection",
},
{
sql: "select a from t where c_str like 'abc%'",
best: "IndexReader(Index(t.c_d_e_str)[[abc,abd)])->Projection",
},
{
// FIXME: Should use index reader.
sql: "select a from t where c_str like 'abc_'",
best: "TableReader(Table(t))->Sel([like(test.t.c_str, abc_, 92)])->Projection",
},
{
sql: "select a from t where c_str like 'abc%af'",
best: "TableReader(Table(t))->Sel([like(test.t.c_str, abc%af, 92)])->Projection",
// best: "Index(t.c_d_e_str)[[abc <nil>,abd <nil>)]->Selection->Projection",
},
{
sql: `select a from t where c_str like 'abc\\_' escape ''`,
best: "TableReader(Table(t))->Sel([like(test.t.c_str, abc\\_, 92)])->Projection",
// best: "Index(t.c_d_e_str)[[abc_,abc_]]->Projection",
},
//{
// sql: `select a from t where c_str like 'abc\\_'`,
// best: "Index(t.c_d_e_str)[[abc_,abc_]]->Projection",
//},
//{
// sql: `select a from t where c_str like 'abc\\\\_'`,
// best: "Index(t.c_d_e_str)[(abc\\ +inf,abc] <nil>)]->Selection->Projection",
//},
//{
// sql: `select a from t where c_str like 'abc\\_%'`,
// best: "Index(t.c_d_e_str)[[abc_ <nil>,abc` <nil>)]->Projection",
//},
//{
// sql: `select a from t where c_str like 'abc=_%' escape '='`,
// best: "Index(t.c_d_e_str)[[abc_ <nil>,abc` <nil>)]->Projection",
//},
//{
// sql: `select a from t where c_str like 'abc\\__'`,
// best: "Index(t.c_d_e_str)[(abc_ +inf,abc` <nil>)]->Selection->Projection",
//},
//{
// // Check that 123 is converted to string '123'. index can be used.
// sql: `select a from t where c_str like 123`,
// best: "Index(t.c_d_e_str)[[123,123]]->Projection",
//},
// c is type int which will be added cast to specified type when building function signature, no index can be used.
//{
// sql: `select a from t where c like '1'`,
// best: "Table(t)->Selection->Projection",
//},
//{
// sql: `select a from t where c = 1.1 and d > 3`,
// best: "Index(t.c_d_e)[]->Projection",
//},
//{
// sql: `select a from t where c = 1.9 and d > 3`,
// best: "Index(t.c_d_e)[]->Projection",
//},
{
sql: `select a from t where c < 1.1`,
best: "IndexReader(Index(t.c_d_e)[[-inf,2)])->Projection",
},
{
sql: `select a from t where c <= 1.9`,
best: "IndexReader(Index(t.c_d_e)[[-inf,1]])->Projection",
},
{
sql: `select a from t where c >= 1.1`,
best: "IndexReader(Index(t.c_d_e)[[2,+inf]])->Projection",
},
{
sql: `select a from t where c > 1.9`,
best: "IndexReader(Index(t.c_d_e)[(1,+inf]])->Projection",
},
{
sql: `select a from t where c = 123456789098765432101234`,
best: "TableReader(Table(t))->Sel([eq(cast(test.t.c), 123456789098765432101234)])->Projection",
},
{
sql: `select a from t where c = 'hanfei'`,
best: "TableReader(Table(t))->Sel([eq(cast(test.t.c), cast(hanfei))])->Projection",
},
}
for _, tt := range tests {
comment := Commentf("for %s", tt.sql)
stmt, err := s.ParseOneStmt(tt.sql, "", "")
c.Assert(err, IsNil, comment)
is, err := plan.MockResolve(stmt)
c.Assert(err, IsNil)
p, err := plan.Optimize(se, stmt, is)
c.Assert(err, IsNil)
c.Assert(plan.ToString(p), Equals, tt.best, Commentf("for %s", tt.sql))
}
}