-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
join_test.go
1855 lines (1710 loc) · 79.2 KB
/
join_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 2016 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 executor_test
import (
"context"
"fmt"
"math/rand"
"strings"
"time"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/testkit"
)
type testSuiteJoin1 struct {
*baseTestSuite
}
type testSuiteJoin2 struct {
*baseTestSuite
}
type testSuiteJoin3 struct {
*baseTestSuite
}
func (s *testSuiteJoin1) TestJoinPanic(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set sql_mode = 'ONLY_FULL_GROUP_BY'")
tk.MustExec("drop table if exists events")
tk.MustExec("create table events (clock int, source int)")
tk.MustQuery("SELECT * FROM events e JOIN (SELECT MAX(clock) AS clock FROM events e2 GROUP BY e2.source) e3 ON e3.clock=e.clock")
err := tk.ExecToErr("SELECT * FROM events e JOIN (SELECT clock FROM events e2 GROUP BY e2.source) e3 ON e3.clock=e.clock")
c.Check(err, NotNil)
}
func (s *testSuite) TestJoinInDisk(c *C) {
originCfg := config.GetGlobalConfig()
newConf := *originCfg
newConf.OOMUseTmpStorage = true
config.StoreGlobalConfig(&newConf)
defer config.StoreGlobalConfig(originCfg)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
sm := &mockSessionManager1{
PS: make([]*util.ProcessInfo, 0),
}
tk.Se.SetSessionManager(sm)
s.domain.ExpensiveQueryHandle().SetSessionManager(sm)
// TODO(fengliyuan): how to ensure that it is using disk really?
tk.MustExec("set @@tidb_mem_quota_query=1;")
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t(c1 int, c2 int)")
tk.MustExec("create table t1(c1 int, c2 int)")
tk.MustExec("insert into t values(1,1),(2,2)")
tk.MustExec("insert into t1 values(2,3),(4,4)")
result := tk.MustQuery("select /*+ TIDB_HJ(t, t2) */ * from t, t1 where t.c1 = t1.c1")
result.Check(testkit.Rows("2 2 2 3"))
}
func (s *testSuiteJoin2) TestJoin(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @@tidb_index_lookup_join_concurrency = 200")
c.Assert(tk.Se.GetSessionVars().IndexLookupJoinConcurrency, Equals, 200)
tk.MustExec("set @@tidb_index_lookup_join_concurrency = 4")
c.Assert(tk.Se.GetSessionVars().IndexLookupJoinConcurrency, Equals, 4)
tk.MustExec("set @@tidb_index_lookup_size = 2")
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (c int)")
tk.MustExec("insert t values (1)")
tests := []struct {
sql string
result [][]interface{}
}{
{
"select 1 from t as a left join t as b on 0",
testkit.Rows("1"),
},
{
"select 1 from t as a join t as b on 1",
testkit.Rows("1"),
},
}
for _, tt := range tests {
result := tk.MustQuery(tt.sql)
result.Check(tt.result)
}
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t(c1 int, c2 int)")
tk.MustExec("create table t1(c1 int, c2 int)")
tk.MustExec("insert into t values(1,1),(2,2)")
tk.MustExec("insert into t1 values(2,3),(4,4)")
result := tk.MustQuery("select * from t left outer join t1 on t.c1 = t1.c1 where t.c1 = 1 or t1.c2 > 20")
result.Check(testkit.Rows("1 1 <nil> <nil>"))
result = tk.MustQuery("select * from t1 right outer join t on t.c1 = t1.c1 where t.c1 = 1 or t1.c2 > 20")
result.Check(testkit.Rows("<nil> <nil> 1 1"))
result = tk.MustQuery("select * from t right outer join t1 on t.c1 = t1.c1 where t.c1 = 1 or t1.c2 > 20")
result.Check(testkit.Rows())
result = tk.MustQuery("select * from t left outer join t1 on t.c1 = t1.c1 where t1.c1 = 3 or false")
result.Check(testkit.Rows())
result = tk.MustQuery("select * from t left outer join t1 on t.c1 = t1.c1 and t.c1 != 1 order by t1.c1")
result.Check(testkit.Rows("1 1 <nil> <nil>", "2 2 2 3"))
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t2")
tk.MustExec("drop table if exists t3")
tk.MustExec("create table t1 (c1 int, c2 int)")
tk.MustExec("create table t2 (c1 int, c2 int)")
tk.MustExec("create table t3 (c1 int, c2 int)")
tk.MustExec("insert into t1 values (1,1), (2,2), (3,3)")
tk.MustExec("insert into t2 values (1,1), (3,3), (5,5)")
tk.MustExec("insert into t3 values (1,1), (5,5), (9,9)")
result = tk.MustQuery("select * from t1 left join t2 on t1.c1 = t2.c1 right join t3 on t2.c1 = t3.c1 order by t1.c1, t1.c2, t2.c1, t2.c2, t3.c1, t3.c2;")
result.Check(testkit.Rows("<nil> <nil> <nil> <nil> 5 5", "<nil> <nil> <nil> <nil> 9 9", "1 1 1 1 1 1"))
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1 (c1 int)")
tk.MustExec("insert into t1 values (1), (1), (1)")
result = tk.MustQuery("select * from t1 a join t1 b on a.c1 = b.c1;")
result.Check(testkit.Rows("1 1", "1 1", "1 1", "1 1", "1 1", "1 1", "1 1", "1 1", "1 1"))
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t(c1 int, index k(c1))")
tk.MustExec("create table t1(c1 int)")
tk.MustExec("insert into t values (1),(2),(3),(4),(5),(6),(7)")
tk.MustExec("insert into t1 values (1),(2),(3),(4),(5),(6),(7)")
result = tk.MustQuery("select a.c1 from t a , t1 b where a.c1 = b.c1 order by a.c1;")
result.Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7"))
// Test race.
result = tk.MustQuery("select a.c1 from t a , t1 b where a.c1 = b.c1 and a.c1 + b.c1 > 5 order by b.c1")
result.Check(testkit.Rows("3", "4", "5", "6", "7"))
result = tk.MustQuery("select a.c1 from t a , (select * from t1 limit 3) b where a.c1 = b.c1 order by b.c1;")
result.Check(testkit.Rows("1", "2", "3"))
tk.MustExec("drop table if exists t,t2,t1")
tk.MustExec("create table t(c1 int)")
tk.MustExec("create table t1(c1 int, c2 int)")
tk.MustExec("create table t2(c1 int, c2 int)")
tk.MustExec("insert into t1 values(1,2),(2,3),(3,4)")
tk.MustExec("insert into t2 values(1,0),(2,0),(3,0)")
tk.MustExec("insert into t values(1),(2),(3)")
result = tk.MustQuery("select * from t1 , t2 where t2.c1 = t1.c1 and t2.c2 = 0 and t1.c2 in (select * from t)")
result.Sort().Check(testkit.Rows("1 2 1 0", "2 3 2 0"))
result = tk.MustQuery("select * from t1 , t2 where t2.c1 = t1.c1 and t2.c2 = 0 and t1.c1 = 1 order by t1.c2 limit 1")
result.Sort().Check(testkit.Rows("1 2 1 0"))
tk.MustExec("drop table if exists t, t1")
tk.MustExec("create table t(a int primary key, b int)")
tk.MustExec("create table t1(a int, b int, key s(b))")
tk.MustExec("insert into t values(1, 1), (2, 2), (3, 3)")
tk.MustExec("insert into t1 values(1, 2), (1, 3), (1, 4), (3, 4), (4, 5)")
// The physical plans of the two sql are tested at physical_plan_test.go
tk.MustQuery("select /*+ INL_JOIN(t, t1) */ * from t join t1 on t.a=t1.a").Check(testkit.Rows("1 1 1 2", "1 1 1 3", "1 1 1 4", "3 3 3 4"))
tk.MustQuery("select /*+ INL_HASH_JOIN(t, t1) */ * from t join t1 on t.a=t1.a").Check(testkit.Rows("1 1 1 2", "1 1 1 3", "1 1 1 4", "3 3 3 4"))
tk.MustQuery("select /*+ INL_MERGE_JOIN(t, t1) */ * from t join t1 on t.a=t1.a").Check(testkit.Rows("1 1 1 2", "1 1 1 3", "1 1 1 4", "3 3 3 4"))
tk.MustQuery("select /*+ INL_JOIN(t) */ * from t1 join t on t.a=t1.a and t.a < t1.b").Check(testkit.Rows("1 2 1 1", "1 3 1 1", "1 4 1 1", "3 4 3 3"))
tk.MustQuery("select /*+ INL_HASH_JOIN(t) */ * from t1 join t on t.a=t1.a and t.a < t1.b").Check(testkit.Rows("1 2 1 1", "1 3 1 1", "1 4 1 1", "3 4 3 3"))
tk.MustQuery("select /*+ INL_MERGE_JOIN(t) */ * from t1 join t on t.a=t1.a and t.a < t1.b").Check(testkit.Rows("1 2 1 1", "1 3 1 1", "1 4 1 1", "3 4 3 3"))
// Test single index reader.
tk.MustQuery("select /*+ INL_JOIN(t, t1) */ t1.b from t1 join t on t.b=t1.b").Check(testkit.Rows("2", "3"))
tk.MustQuery("select /*+ INL_HASH_JOIN(t, t1) */ t1.b from t1 join t on t.b=t1.b").Check(testkit.Rows("2", "3"))
tk.MustQuery("select /*+ INL_MERGE_JOIN(t, t1) */ t1.b from t1 join t on t.b=t1.b").Check(testkit.Rows("2", "3"))
tk.MustQuery("select /*+ INL_JOIN(t1) */ * from t right outer join t1 on t.a=t1.a").Check(testkit.Rows("1 1 1 2", "1 1 1 3", "1 1 1 4", "3 3 3 4", "<nil> <nil> 4 5"))
tk.MustQuery("select /*+ INL_HASH_JOIN(t1) */ * from t right outer join t1 on t.a=t1.a").Check(testkit.Rows("1 1 1 2", "1 1 1 3", "1 1 1 4", "3 3 3 4", "<nil> <nil> 4 5"))
tk.MustQuery("select /*+ INL_MERGE_JOIN(t1) */ * from t right outer join t1 on t.a=t1.a").Check(testkit.Rows("1 1 1 2", "1 1 1 3", "1 1 1 4", "3 3 3 4", "<nil> <nil> 4 5"))
tk.MustQuery("select /*+ INL_JOIN(t) */ avg(t.b) from t right outer join t1 on t.a=t1.a").Check(testkit.Rows("1.5000"))
tk.MustQuery("select /*+ INL_HASH_JOIN(t) */ avg(t.b) from t right outer join t1 on t.a=t1.a").Check(testkit.Rows("1.5000"))
tk.MustQuery("select /*+ INL_MERGE_JOIN(t) */ avg(t.b) from t right outer join t1 on t.a=t1.a").Check(testkit.Rows("1.5000"))
// Test that two conflict hints will return warning.
tk.MustExec("select /*+ TIDB_INLJ(t) TIDB_SMJ(t) */ * from t join t1 on t.a=t1.a")
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings(), HasLen, 1)
tk.MustExec("select /*+ TIDB_INLJ(t) TIDB_HJ(t) */ * from t join t1 on t.a=t1.a")
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings(), HasLen, 1)
tk.MustExec("select /*+ TIDB_SMJ(t) TIDB_HJ(t) */ * from t join t1 on t.a=t1.a")
c.Assert(tk.Se.GetSessionVars().StmtCtx.GetWarnings(), HasLen, 1)
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int)")
tk.MustExec("insert into t values(1),(2), (3)")
tk.MustQuery("select @a := @a + 1 from t, (select @a := 0) b;").Check(testkit.Rows("1", "2", "3"))
tk.MustExec("drop table if exists t, t1")
tk.MustExec("create table t(a int primary key, b int, key s(b))")
tk.MustExec("create table t1(a int, b int)")
tk.MustExec("insert into t values(1, 3), (2, 2), (3, 1)")
tk.MustExec("insert into t1 values(0, 0), (1, 2), (1, 3), (3, 4)")
tk.MustQuery("select /*+ INL_JOIN(t1) */ * from t join t1 on t.a=t1.a order by t.b").Sort().Check(testkit.Rows("1 3 1 2", "1 3 1 3", "3 1 3 4"))
tk.MustQuery("select /*+ INL_HASH_JOIN(t1) */ * from t join t1 on t.a=t1.a order by t.b").Sort().Check(testkit.Rows("1 3 1 2", "1 3 1 3", "3 1 3 4"))
tk.MustQuery("select /*+ INL_MERGE_JOIN(t1) */ * from t join t1 on t.a=t1.a order by t.b").Sort().Check(testkit.Rows("1 3 1 2", "1 3 1 3", "3 1 3 4"))
tk.MustQuery("select /*+ INL_JOIN(t) */ t.a, t.b from t join t1 on t.a=t1.a where t1.b = 4 limit 1").Check(testkit.Rows("3 1"))
tk.MustQuery("select /*+ INL_HASH_JOIN(t) */ t.a, t.b from t join t1 on t.a=t1.a where t1.b = 4 limit 1").Check(testkit.Rows("3 1"))
tk.MustQuery("select /*+ INL_MERGE_JOIN(t) */ t.a, t.b from t join t1 on t.a=t1.a where t1.b = 4 limit 1").Check(testkit.Rows("3 1"))
tk.MustQuery("select /*+ INL_JOIN(t, t1) */ * from t right join t1 on t.a=t1.a order by t.b").Sort().Check(testkit.Rows("1 3 1 2", "1 3 1 3", "3 1 3 4", "<nil> <nil> 0 0"))
tk.MustQuery("select /*+ INL_HASH_JOIN(t, t1) */ * from t right join t1 on t.a=t1.a order by t.b").Sort().Check(testkit.Rows("1 3 1 2", "1 3 1 3", "3 1 3 4", "<nil> <nil> 0 0"))
tk.MustQuery("select /*+ INL_MERGE_JOIN(t, t1) */ * from t right join t1 on t.a=t1.a order by t.b").Sort().Check(testkit.Rows("1 3 1 2", "1 3 1 3", "3 1 3 4", "<nil> <nil> 0 0"))
// join reorder will disorganize the resulting schema
tk.MustExec("drop table if exists t, t1")
tk.MustExec("create table t(a int, b int)")
tk.MustExec("create table t1(a int, b int)")
tk.MustExec("insert into t values(1,2)")
tk.MustExec("insert into t1 values(3,4)")
tk.MustQuery("select (select t1.a from t1 , t where t.a = s.a limit 2) from t as s").Check(testkit.Rows("3"))
// test index join bug
tk.MustExec("drop table if exists t, t1")
tk.MustExec("create table t(a int, b int, key s1(a,b), key s2(b))")
tk.MustExec("create table t1(a int)")
tk.MustExec("insert into t values(1,2), (5,3), (6,4)")
tk.MustExec("insert into t1 values(1), (2), (3)")
tk.MustQuery("select /*+ INL_JOIN(t) */ t1.a from t1, t where t.a = 5 and t.b = t1.a").Check(testkit.Rows("3"))
tk.MustQuery("select /*+ INL_HASH_JOIN(t) */ t1.a from t1, t where t.a = 5 and t.b = t1.a").Check(testkit.Rows("3"))
tk.MustQuery("select /*+ INL_MERGE_JOIN(t) */ t1.a from t1, t where t.a = 5 and t.b = t1.a").Check(testkit.Rows("3"))
// test issue#4997
tk.MustExec("drop table if exists t1, t2")
tk.MustExec(`
CREATE TABLE t1 (
pk int(11) NOT NULL AUTO_INCREMENT primary key,
a int(11) DEFAULT NULL,
b date DEFAULT NULL,
c varchar(1) DEFAULT NULL,
KEY a (a),
KEY b (b),
KEY c (c,a)
)`)
tk.MustExec(`
CREATE TABLE t2 (
pk int(11) NOT NULL AUTO_INCREMENT primary key,
a int(11) DEFAULT NULL,
b date DEFAULT NULL,
c varchar(1) DEFAULT NULL,
KEY a (a),
KEY b (b),
KEY c (c,a)
)`)
tk.MustExec(`insert into t1 value(1,1,"2000-11-11", null);`)
result = tk.MustQuery(`
SELECT table2.b AS field2 FROM
(
t1 AS table1 LEFT OUTER JOIN
(SELECT tmp_t2.* FROM ( t2 AS tmp_t1 RIGHT JOIN t1 AS tmp_t2 ON (tmp_t2.a = tmp_t1.a))) AS table2
ON (table2.c = table1.c)
) `)
result.Check(testkit.Rows("<nil>"))
// test virtual rows are included (issue#5771)
result = tk.MustQuery(`SELECT 1 FROM (SELECT 1) t1, (SELECT 1) t2`)
result.Check(testkit.Rows("1"))
result = tk.MustQuery(`
SELECT @NUM := @NUM + 1 as NUM FROM
( SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3
) a
INNER JOIN
( SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3
) b,
(SELECT @NUM := 0) d;
`)
result.Check(testkit.Rows("1", "2", "3", "4", "5", "6", "7", "8", "9"))
// This case is for testing:
// when the main thread calls Executor.Close() while the out data fetch worker and join workers are still working,
// we need to stop the goroutines as soon as possible to avoid unexpected error.
tk.MustExec("set @@tidb_hash_join_concurrency=5")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a int)")
for i := 0; i < 100; i++ {
tk.MustExec("insert into t value(1)")
}
result = tk.MustQuery("select /*+ TIDB_HJ(s, r) */ * from t as s join t as r on s.a = r.a limit 1;")
result.Check(testkit.Rows("1 1"))
tk.MustExec("drop table if exists user, aa, bb")
tk.MustExec("create table aa(id int)")
tk.MustExec("insert into aa values(1)")
tk.MustExec("create table bb(id int)")
tk.MustExec("insert into bb values(1)")
tk.MustExec("create table user(id int, name varchar(20))")
tk.MustExec("insert into user values(1, 'a'), (2, 'b')")
tk.MustQuery("select user.id,user.name from user left join aa on aa.id = user.id left join bb on aa.id = bb.id where bb.id < 10;").Check(testkit.Rows("1 a"))
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t (a bigint);`)
tk.MustExec(`insert into t values (1);`)
tk.MustQuery(`select t2.a, t1.a from t t1 inner join (select "1" as a) t2 on t2.a = t1.a;`).Check(testkit.Rows("1 1"))
tk.MustQuery(`select t2.a, t1.a from t t1 inner join (select "2" as b, "1" as a) t2 on t2.a = t1.a;`).Check(testkit.Rows("1 1"))
tk.MustExec("drop table if exists t1, t2, t3, t4")
tk.MustExec("create table t1(a int, b int)")
tk.MustExec("create table t2(a int, b int)")
tk.MustExec("create table t3(a int, b int)")
tk.MustExec("create table t4(a int, b int)")
tk.MustExec("insert into t1 values(1, 1)")
tk.MustExec("insert into t2 values(1, 1)")
tk.MustExec("insert into t3 values(1, 1)")
tk.MustExec("insert into t4 values(1, 1)")
tk.MustQuery("select min(t2.b) from t1 right join t2 on t2.a=t1.a right join t3 on t2.a=t3.a left join t4 on t3.a=t4.a").Check(testkit.Rows("1"))
}
func (s *testSuiteJoin2) TestJoinCast(c *C) {
tk := testkit.NewTestKit(c, s.store)
var result *testkit.Result
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t(c1 int)")
tk.MustExec("create table t1(c1 int unsigned)")
tk.MustExec("insert into t values (1)")
tk.MustExec("insert into t1 values (1)")
result = tk.MustQuery("select t.c1 from t , t1 where t.c1 = t1.c1")
result.Check(testkit.Rows("1"))
// int64(-1) != uint64(18446744073709551615)
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t(c1 bigint)")
tk.MustExec("create table t1(c1 bigint unsigned)")
tk.MustExec("insert into t values (-1)")
tk.MustExec("insert into t1 values (18446744073709551615)")
result = tk.MustQuery("select * from t , t1 where t.c1 = t1.c1")
result.Check(testkit.Rows())
// float(1) == double(1)
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t(c1 float)")
tk.MustExec("create table t1(c1 double)")
tk.MustExec("insert into t values (1.0)")
tk.MustExec("insert into t1 values (1.00)")
result = tk.MustQuery("select t.c1 from t , t1 where t.c1 = t1.c1")
result.Check(testkit.Rows("1"))
// varchar("x") == char("x")
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t(c1 varchar(1))")
tk.MustExec("create table t1(c1 char(1))")
tk.MustExec(`insert into t values ("x")`)
tk.MustExec(`insert into t1 values ("x")`)
result = tk.MustQuery("select t.c1 from t , t1 where t.c1 = t1.c1")
result.Check(testkit.Rows("x"))
// varchar("x") != char("y")
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t(c1 varchar(1))")
tk.MustExec("create table t1(c1 char(1))")
tk.MustExec(`insert into t values ("x")`)
tk.MustExec(`insert into t1 values ("y")`)
result = tk.MustQuery("select t.c1 from t , t1 where t.c1 = t1.c1")
result.Check(testkit.Rows())
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t(c1 int,c2 double)")
tk.MustExec("create table t1(c1 double,c2 int)")
tk.MustExec("insert into t values (1, 2), (1, NULL)")
tk.MustExec("insert into t1 values (1, 2), (1, NULL)")
result = tk.MustQuery("select * from t a , t1 b where (a.c1, a.c2) = (b.c1, b.c2);")
result.Check(testkit.Rows("1 2 1 2"))
/* Enable & fix this test after https://github.com/pingcap/tidb/issues/11895 is fixed.
tk.MustExec("drop table if exists t;")
tk.MustExec("drop table if exists t1;")
tk.MustExec("create table t(c1 bigint unsigned);")
tk.MustExec("create table t1(c1 bit(64));")
tk.MustExec("insert into t value(18446744073709551615);")
tk.MustExec("insert into t1 value(-1);")
result = tk.MustQuery("select * from t, t1 where t.c1 = t1.c1;")
c.Check(len(result.Rows()), Equals, 1)
*/
/* https://github.com/pingcap/tidb/issues/11896
tk.MustExec("drop table if exists t;")
tk.MustExec("drop table if exists t1;")
tk.MustExec("create table t(c1 bigint);")
tk.MustExec("create table t1(c1 bit(64));")
tk.MustExec("insert into t value(1);")
tk.MustExec("insert into t1 value(1);")
result = tk.MustQuery("select * from t, t1 where t.c1 = t1.c1;")
c.Check(len(result.Rows()), Equals, 1)
*/
tk.MustExec("drop table if exists t;")
tk.MustExec("drop table if exists t1;")
tk.MustExec("create table t(c1 bigint);")
tk.MustExec("create table t1(c1 bit(64));")
tk.MustExec("insert into t value(-1);")
tk.MustExec("insert into t1 value(18446744073709551615);")
result = tk.MustQuery("select * from t, t1 where t.c1 = t1.c1;")
c.Check(len(result.Rows()), Equals, 0)
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t(c1 bigint)")
tk.MustExec("create table t1(c1 bigint unsigned)")
tk.MustExec("create table t2(c1 Date)")
tk.MustExec("insert into t value(20191111)")
tk.MustExec("insert into t1 value(20191111)")
tk.MustExec("insert into t2 value('2019-11-11')")
result = tk.MustQuery("select * from t, t1, t2 where t.c1 = t2.c1 and t1.c1 = t2.c1")
result.Check(testkit.Rows("20191111 20191111 2019-11-11"))
tk.MustExec("drop table if exists t;")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t2;")
tk.MustExec("create table t(c1 bigint);")
tk.MustExec("create table t1(c1 bigint unsigned);")
tk.MustExec("create table t2(c1 enum('a', 'b', 'c', 'd'));")
tk.MustExec("insert into t value(3);")
tk.MustExec("insert into t1 value(3);")
tk.MustExec("insert into t2 value('c');")
result = tk.MustQuery("select * from t, t1, t2 where t.c1 = t2.c1 and t1.c1 = t2.c1;")
result.Check(testkit.Rows("3 3 c"))
tk.MustExec("drop table if exists t;")
tk.MustExec("drop table if exists t1;")
tk.MustExec("drop table if exists t2;")
tk.MustExec("create table t(c1 bigint);")
tk.MustExec("create table t1(c1 bigint unsigned);")
tk.MustExec("create table t2 (c1 SET('a', 'b', 'c', 'd'));")
tk.MustExec("insert into t value(9);")
tk.MustExec("insert into t1 value(9);")
tk.MustExec("insert into t2 value('a,d');")
result = tk.MustQuery("select * from t, t1, t2 where t.c1 = t2.c1 and t1.c1 = t2.c1;")
result.Check(testkit.Rows("9 9 a,d"))
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t(c1 int)")
tk.MustExec("create table t1(c1 decimal(4,2))")
tk.MustExec("insert into t values(0), (2)")
tk.MustExec("insert into t1 values(0), (9)")
result = tk.MustQuery("select * from t left join t1 on t1.c1 = t.c1")
result.Sort().Check(testkit.Rows("0 0.00", "2 <nil>"))
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t(c1 decimal(4,1))")
tk.MustExec("create table t1(c1 decimal(4,2))")
tk.MustExec("insert into t values(0), (2)")
tk.MustExec("insert into t1 values(0), (9)")
result = tk.MustQuery("select * from t left join t1 on t1.c1 = t.c1")
result.Sort().Check(testkit.Rows("0.0 0.00", "2.0 <nil>"))
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t(c1 decimal(4,1))")
tk.MustExec("create table t1(c1 decimal(4,2))")
tk.MustExec("create index k1 on t1(c1)")
tk.MustExec("insert into t values(0), (2)")
tk.MustExec("insert into t1 values(0), (9)")
result = tk.MustQuery("select /*+ INL_JOIN(t1) */ * from t left join t1 on t1.c1 = t.c1")
result.Sort().Check(testkit.Rows("0.0 0.00", "2.0 <nil>"))
result = tk.MustQuery("select /*+ INL_HASH_JOIN(t1) */ * from t left join t1 on t1.c1 = t.c1")
result.Sort().Check(testkit.Rows("0.0 0.00", "2.0 <nil>"))
result = tk.MustQuery("select /*+ INL_MERGE_JOIN(t1) */ * from t left join t1 on t1.c1 = t.c1")
result.Sort().Check(testkit.Rows("0.0 0.00", "2.0 <nil>"))
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t(c1 char(10))")
tk.MustExec("create table t1(c1 char(10))")
tk.MustExec("create table t2(c1 char(10))")
tk.MustExec("insert into t values('abd')")
tk.MustExec("insert into t1 values('abc')")
tk.MustExec("insert into t2 values('abc')")
result = tk.MustQuery("select * from (select * from t union all select * from t1) t1 join t2 on t1.c1 = t2.c1")
result.Sort().Check(testkit.Rows("abc abc"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a varchar(10), index idx(a))")
tk.MustExec("insert into t values('1'), ('2'), ('3')")
tk.MustExec("set @@tidb_init_chunk_size=1")
result = tk.MustQuery("select a from (select /*+ INL_JOIN(t1, t2) */ t1.a from t t1 join t t2 on t1.a=t2.a) t group by a")
result.Sort().Check(testkit.Rows("1", "2", "3"))
result = tk.MustQuery("select a from (select /*+ INL_HASH_JOIN(t1, t2) */ t1.a from t t1 join t t2 on t1.a=t2.a) t group by a")
result.Sort().Check(testkit.Rows("1", "2", "3"))
result = tk.MustQuery("select a from (select /*+ INL_MERGE_JOIN(t1, t2) */ t1.a from t t1 join t t2 on t1.a=t2.a) t group by a")
result.Sort().Check(testkit.Rows("1", "2", "3"))
tk.MustExec("set @@tidb_init_chunk_size=32")
}
func (s *testSuiteJoin3) TestUsing(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1, t2, t3, t4")
tk.MustExec("create table t1 (a int, c int)")
tk.MustExec("create table t2 (a int, d int)")
tk.MustExec("create table t3 (a int)")
tk.MustExec("create table t4 (a int)")
tk.MustExec("insert t1 values (2, 4), (1, 3)")
tk.MustExec("insert t2 values (2, 5), (3, 6)")
tk.MustExec("insert t3 values (1)")
tk.MustQuery("select * from t1 join t2 using (a)").Check(testkit.Rows("2 4 5"))
tk.MustQuery("select t1.a, t2.a from t1 join t2 using (a)").Check(testkit.Rows("2 2"))
tk.MustQuery("select * from t1 right join t2 using (a) order by a").Check(testkit.Rows("2 5 4", "3 6 <nil>"))
tk.MustQuery("select t1.a, t2.a from t1 right join t2 using (a) order by t2.a").Check(testkit.Rows("2 2", "<nil> 3"))
tk.MustQuery("select * from t1 left join t2 using (a) order by a").Check(testkit.Rows("1 3 <nil>", "2 4 5"))
tk.MustQuery("select t1.a, t2.a from t1 left join t2 using (a) order by t1.a").Check(testkit.Rows("1 <nil>", "2 2"))
tk.MustQuery("select * from t1 join t2 using (a) right join t3 using (a)").Check(testkit.Rows("1 <nil> <nil>"))
tk.MustQuery("select * from t1 join t2 using (a) right join t3 on (t2.a = t3.a)").Check(testkit.Rows("<nil> <nil> <nil> 1"))
tk.MustQuery("select t2.a from t1 join t2 using (a) right join t3 on (t1.a = t3.a)").Check(testkit.Rows("<nil>"))
tk.MustQuery("select t1.a, t2.a, t3.a from t1 join t2 using (a) right join t3 using (a)").Check(testkit.Rows("<nil> <nil> 1"))
tk.MustQuery("select t1.c, t2.d from t1 join t2 using (a) right join t3 using (a)").Check(testkit.Rows("<nil> <nil>"))
tk.MustExec("alter table t1 add column b int default 1 after a")
tk.MustExec("alter table t2 add column b int default 1 after a")
tk.MustQuery("select * from t1 join t2 using (b, a)").Check(testkit.Rows("2 1 4 5"))
tk.MustExec("select * from (t1 join t2 using (a)) join (t3 join t4 using (a)) on (t2.a = t4.a and t1.a = t3.a)")
tk.MustExec("drop table if exists t, tt")
tk.MustExec("create table t(a int, b int)")
tk.MustExec("create table tt(b int, a int)")
tk.MustExec("insert into t (a, b) values(1, 1)")
tk.MustExec("insert into tt (a, b) values(1, 2)")
tk.MustQuery("select * from t join tt using(a)").Check(testkit.Rows("1 1 2"))
tk.MustExec("drop table if exists t, tt")
tk.MustExec("create table t(a float, b int)")
tk.MustExec("create table tt(b bigint, a int)")
// Check whether this sql can execute successfully.
tk.MustExec("select * from t join tt using(a)")
}
func (s *testSuiteJoin1) TestNaturalJoin(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1 (a int, b int)")
tk.MustExec("create table t2 (a int, c int)")
tk.MustExec("insert t1 values (1, 2), (10, 20)")
tk.MustExec("insert t2 values (1, 3), (100, 200)")
tk.MustQuery("select * from t1 natural join t2").Check(testkit.Rows("1 2 3"))
tk.MustQuery("select * from t1 natural left join t2 order by a").Check(testkit.Rows("1 2 3", "10 20 <nil>"))
tk.MustQuery("select * from t1 natural right join t2 order by a").Check(testkit.Rows("1 3 2", "100 200 <nil>"))
}
func (s *testSuiteJoin3) TestMultiJoin(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t35(a35 int primary key, b35 int, x35 int)")
tk.MustExec("create table t40(a40 int primary key, b40 int, x40 int)")
tk.MustExec("create table t14(a14 int primary key, b14 int, x14 int)")
tk.MustExec("create table t42(a42 int primary key, b42 int, x42 int)")
tk.MustExec("create table t15(a15 int primary key, b15 int, x15 int)")
tk.MustExec("create table t7(a7 int primary key, b7 int, x7 int)")
tk.MustExec("create table t64(a64 int primary key, b64 int, x64 int)")
tk.MustExec("create table t19(a19 int primary key, b19 int, x19 int)")
tk.MustExec("create table t9(a9 int primary key, b9 int, x9 int)")
tk.MustExec("create table t8(a8 int primary key, b8 int, x8 int)")
tk.MustExec("create table t57(a57 int primary key, b57 int, x57 int)")
tk.MustExec("create table t37(a37 int primary key, b37 int, x37 int)")
tk.MustExec("create table t44(a44 int primary key, b44 int, x44 int)")
tk.MustExec("create table t38(a38 int primary key, b38 int, x38 int)")
tk.MustExec("create table t18(a18 int primary key, b18 int, x18 int)")
tk.MustExec("create table t62(a62 int primary key, b62 int, x62 int)")
tk.MustExec("create table t4(a4 int primary key, b4 int, x4 int)")
tk.MustExec("create table t48(a48 int primary key, b48 int, x48 int)")
tk.MustExec("create table t31(a31 int primary key, b31 int, x31 int)")
tk.MustExec("create table t16(a16 int primary key, b16 int, x16 int)")
tk.MustExec("create table t12(a12 int primary key, b12 int, x12 int)")
tk.MustExec("insert into t35 values(1,1,1)")
tk.MustExec("insert into t40 values(1,1,1)")
tk.MustExec("insert into t14 values(1,1,1)")
tk.MustExec("insert into t42 values(1,1,1)")
tk.MustExec("insert into t15 values(1,1,1)")
tk.MustExec("insert into t7 values(1,1,1)")
tk.MustExec("insert into t64 values(1,1,1)")
tk.MustExec("insert into t19 values(1,1,1)")
tk.MustExec("insert into t9 values(1,1,1)")
tk.MustExec("insert into t8 values(1,1,1)")
tk.MustExec("insert into t57 values(1,1,1)")
tk.MustExec("insert into t37 values(1,1,1)")
tk.MustExec("insert into t44 values(1,1,1)")
tk.MustExec("insert into t38 values(1,1,1)")
tk.MustExec("insert into t18 values(1,1,1)")
tk.MustExec("insert into t62 values(1,1,1)")
tk.MustExec("insert into t4 values(1,1,1)")
tk.MustExec("insert into t48 values(1,1,1)")
tk.MustExec("insert into t31 values(1,1,1)")
tk.MustExec("insert into t16 values(1,1,1)")
tk.MustExec("insert into t12 values(1,1,1)")
tk.MustExec("insert into t35 values(7,7,7)")
tk.MustExec("insert into t40 values(7,7,7)")
tk.MustExec("insert into t14 values(7,7,7)")
tk.MustExec("insert into t42 values(7,7,7)")
tk.MustExec("insert into t15 values(7,7,7)")
tk.MustExec("insert into t7 values(7,7,7)")
tk.MustExec("insert into t64 values(7,7,7)")
tk.MustExec("insert into t19 values(7,7,7)")
tk.MustExec("insert into t9 values(7,7,7)")
tk.MustExec("insert into t8 values(7,7,7)")
tk.MustExec("insert into t57 values(7,7,7)")
tk.MustExec("insert into t37 values(7,7,7)")
tk.MustExec("insert into t44 values(7,7,7)")
tk.MustExec("insert into t38 values(7,7,7)")
tk.MustExec("insert into t18 values(7,7,7)")
tk.MustExec("insert into t62 values(7,7,7)")
tk.MustExec("insert into t4 values(7,7,7)")
tk.MustExec("insert into t48 values(7,7,7)")
tk.MustExec("insert into t31 values(7,7,7)")
tk.MustExec("insert into t16 values(7,7,7)")
tk.MustExec("insert into t12 values(7,7,7)")
result := tk.MustQuery(`SELECT x4,x8,x38,x44,x31,x9,x57,x48,x19,x40,x14,x12,x7,x64,x37,x18,x62,x35,x42,x15,x16 FROM
t35,t40,t14,t42,t15,t7,t64,t19,t9,t8,t57,t37,t44,t38,t18,t62,t4,t48,t31,t16,t12
WHERE b48=a57
AND a4=b19
AND a14=b16
AND b37=a48
AND a40=b42
AND a31=7
AND a15=b40
AND a38=b8
AND b15=a31
AND b64=a18
AND b12=a44
AND b7=a8
AND b35=a16
AND a12=b14
AND a64=b57
AND b62=a7
AND a35=b38
AND b9=a19
AND a62=b18
AND b4=a37
AND b44=a42`)
result.Check(testkit.Rows("7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7"))
}
func (s *testSuiteJoin3) TestSubquerySameTable(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int)")
tk.MustExec("insert t values (1), (2)")
result := tk.MustQuery("select a from t where exists(select 1 from t as x where x.a < t.a)")
result.Check(testkit.Rows("2"))
result = tk.MustQuery("select a from t where not exists(select 1 from t as x where x.a < t.a)")
result.Check(testkit.Rows("1"))
}
func (s *testSuiteJoin3) TestSubquery(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @@tidb_hash_join_concurrency=1")
tk.MustExec("set @@tidb_hashagg_partial_concurrency=1")
tk.MustExec("set @@tidb_hashagg_final_concurrency=1")
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (c int, d int)")
tk.MustExec("insert t values (1, 1)")
tk.MustExec("insert t values (2, 2)")
tk.MustExec("insert t values (3, 4)")
tk.MustExec("commit")
tk.MustExec("set sql_mode = 'STRICT_TRANS_TABLES'")
result := tk.MustQuery("select * from t where exists(select * from t k where t.c = k.c having sum(c) = 1)")
result.Check(testkit.Rows("1 1"))
result = tk.MustQuery("select * from t where exists(select k.c, k.d from t k, t p where t.c = k.d)")
result.Check(testkit.Rows("1 1", "2 2"))
result = tk.MustQuery("select 1 = (select count(*) from t where t.c = k.d) from t k")
result.Check(testkit.Rows("1", "1", "0"))
result = tk.MustQuery("select 1 = (select count(*) from t where exists( select * from t m where t.c = k.d)) from t k")
result.Sort().Check(testkit.Rows("0", "1", "1"))
result = tk.MustQuery("select t.c = any (select count(*) from t) from t")
result.Sort().Check(testkit.Rows("0", "0", "1"))
result = tk.MustQuery("select * from t where (t.c, 6) = any (select count(*), sum(t.c) from t)")
result.Check(testkit.Rows("3 4"))
result = tk.MustQuery("select t.c from t where (t.c) < all (select count(*) from t)")
result.Check(testkit.Rows("1", "2"))
result = tk.MustQuery("select t.c from t where (t.c, t.d) = any (select * from t)")
result.Sort().Check(testkit.Rows("1", "2", "3"))
result = tk.MustQuery("select t.c from t where (t.c, t.d) != all (select * from t)")
result.Check(testkit.Rows())
result = tk.MustQuery("select (select count(*) from t where t.c = k.d) from t k")
result.Sort().Check(testkit.Rows("0", "1", "1"))
result = tk.MustQuery("select t.c from t where (t.c, t.d) in (select * from t)")
result.Sort().Check(testkit.Rows("1", "2", "3"))
result = tk.MustQuery("select t.c from t where (t.c, t.d) not in (select * from t)")
result.Check(testkit.Rows())
result = tk.MustQuery("select * from t A inner join t B on A.c = B.c and A.c > 100")
result.Check(testkit.Rows())
// = all empty set is true
result = tk.MustQuery("select t.c from t where (t.c, t.d) != all (select * from t where d > 1000)")
result.Sort().Check(testkit.Rows("1", "2", "3"))
result = tk.MustQuery("select t.c from t where (t.c) < any (select c from t where d > 1000)")
result.Check(testkit.Rows())
tk.MustExec("insert t values (NULL, NULL)")
result = tk.MustQuery("select (t.c) < any (select c from t) from t")
result.Sort().Check(testkit.Rows("1", "1", "<nil>", "<nil>"))
result = tk.MustQuery("select (10) > all (select c from t) from t")
result.Check(testkit.Rows("<nil>", "<nil>", "<nil>", "<nil>"))
result = tk.MustQuery("select (c) > all (select c from t) from t")
result.Check(testkit.Rows("0", "0", "0", "<nil>"))
tk.MustExec("drop table if exists a")
tk.MustExec("create table a (c int, d int)")
tk.MustExec("insert a values (1, 2)")
tk.MustExec("drop table if exists b")
tk.MustExec("create table b (c int, d int)")
tk.MustExec("insert b values (2, 1)")
result = tk.MustQuery("select * from a b where c = (select d from b a where a.c = 2 and b.c = 1)")
result.Check(testkit.Rows("1 2"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(c int)")
tk.MustExec("insert t values(10), (8), (7), (9), (11)")
result = tk.MustQuery("select * from t where 9 in (select c from t s where s.c < t.c limit 3)")
result.Check(testkit.Rows("10"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(id int, v int)")
tk.MustExec("insert into t values(1, 1), (2, 2), (3, 3)")
result = tk.MustQuery("select * from t where v=(select min(t1.v) from t t1, t t2, t t3 where t1.id=t2.id and t2.id=t3.id and t1.id=t.id)")
result.Check(testkit.Rows("1 1", "2 2", "3 3"))
result = tk.MustQuery("select exists (select t.id from t where s.id < 2 and t.id = s.id) from t s")
result.Sort().Check(testkit.Rows("0", "0", "1"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(c int)")
result = tk.MustQuery("select exists(select count(*) from t)")
result.Check(testkit.Rows("1"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(id int primary key, v int)")
tk.MustExec("insert into t values(1, 1), (2, 2), (3, 3)")
result = tk.MustQuery("select (select t.id from t where s.id < 2 and t.id = s.id) from t s")
result.Sort().Check(testkit.Rows("1", "<nil>", "<nil>"))
rs, err := tk.Exec("select (select t.id from t where t.id = t.v and t.v != s.id) from t s")
c.Check(err, IsNil)
_, err = session.GetRows4Test(context.Background(), tk.Se, rs)
c.Check(err, NotNil)
c.Check(rs.Close(), IsNil)
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists s")
tk.MustExec("create table t(id int)")
tk.MustExec("create table s(id int)")
tk.MustExec("insert into t values(1), (2)")
tk.MustExec("insert into s values(2), (2)")
result = tk.MustQuery("select id from t where(select count(*) from s where s.id = t.id) > 0")
result.Check(testkit.Rows("2"))
result = tk.MustQuery("select *, (select count(*) from s where id = t.id limit 1, 1) from t")
result.Check(testkit.Rows("1 <nil>", "2 <nil>"))
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists s")
tk.MustExec("create table t(id int primary key)")
tk.MustExec("create table s(id int)")
tk.MustExec("insert into t values(1), (2)")
tk.MustExec("insert into s values(2), (2)")
result = tk.MustQuery("select *, (select count(id) from s where id = t.id) from t")
result.Check(testkit.Rows("1 0", "2 2"))
result = tk.MustQuery("select *, 0 < any (select count(id) from s where id = t.id) from t")
result.Check(testkit.Rows("1 0", "2 1"))
result = tk.MustQuery("select (select count(*) from t k where t.id = id) from s, t where t.id = s.id limit 1")
result.Check(testkit.Rows("1"))
tk.MustExec("drop table if exists t, s")
tk.MustExec("create table t(id int primary key)")
tk.MustExec("create table s(id int, index k(id))")
tk.MustExec("insert into t values(1), (2)")
tk.MustExec("insert into s values(2), (2)")
result = tk.MustQuery("select (select id from s where s.id = t.id order by s.id limit 1) from t")
result.Check(testkit.Rows("<nil>", "2"))
tk.MustExec("drop table if exists t, s")
tk.MustExec("create table t(id int)")
tk.MustExec("create table s(id int)")
tk.MustExec("insert into t values(2), (2)")
tk.MustExec("insert into s values(2)")
result = tk.MustQuery("select (select id from s where s.id = t.id order by s.id) from t")
result.Check(testkit.Rows("2", "2"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(dt datetime)")
result = tk.MustQuery("select (select 1 from t where DATE_FORMAT(o.dt,'%Y-%m')) from t o")
result.Check(testkit.Rows())
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(f1 int, f2 int)")
tk.MustExec("create table t2(fa int, fb int)")
tk.MustExec("insert into t1 values (1,1),(1,1),(1,2),(1,2),(1,2),(1,3)")
tk.MustExec("insert into t2 values (1,1),(1,2),(1,3)")
result = tk.MustQuery("select f1,f2 from t1 group by f1,f2 having count(1) >= all (select fb from t2 where fa = f1)")
result.Check(testkit.Rows("1 2"))
tk.MustExec("DROP TABLE IF EXISTS t1, t2")
tk.MustExec("CREATE TABLE t1(a INT)")
tk.MustExec("CREATE TABLE t2 (d BINARY(2), PRIMARY KEY (d(1)), UNIQUE KEY (d))")
tk.MustExec("INSERT INTO t1 values(1)")
result = tk.MustQuery("SELECT 1 FROM test.t1, test.t2 WHERE 1 = (SELECT test.t2.d FROM test.t2 WHERE test.t1.a >= 1) and test.t2.d = 1;")
result.Check(testkit.Rows())
tk.MustExec("DROP TABLE IF EXISTS t1")
tk.MustExec("CREATE TABLE t1(a int, b int default 0)")
tk.MustExec("create index k1 on t1(a)")
tk.MustExec("INSERT INTO t1 (a) values(1), (2), (3), (4), (5)")
result = tk.MustQuery("select (select /*+ INL_JOIN(x2) */ x2.a from t1 x1, t1 x2 where x1.a = t1.a and x1.a = x2.a) from t1")
result.Check(testkit.Rows("1", "2", "3", "4", "5"))
result = tk.MustQuery("select (select /*+ INL_HASH_JOIN(x2) */ x2.a from t1 x1, t1 x2 where x1.a = t1.a and x1.a = x2.a) from t1")
result.Check(testkit.Rows("1", "2", "3", "4", "5"))
result = tk.MustQuery("select (select /*+ INL_MERGE_JOIN(x2) */ x2.a from t1 x1, t1 x2 where x1.a = t1.a and x1.a = x2.a) from t1")
result.Check(testkit.Rows("1", "2", "3", "4", "5"))
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(a int)")
tk.MustExec("create table t2(b int)")
tk.MustExec("insert into t1 values(1)")
tk.MustExec("insert into t2 values(1)")
tk.MustQuery("select * from t1 where a in (select a from t2)").Check(testkit.Rows("1"))
tk.MustExec("set @@tidb_hash_join_concurrency=5")
}
func (s *testSuiteJoin1) TestInSubquery(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int)")
tk.MustExec("insert t values (1, 1), (2, 1)")
result := tk.MustQuery("select m1.a from t as m1 where m1.a in (select m2.b from t as m2)")
result.Check(testkit.Rows("1"))
result = tk.MustQuery("select m1.a from t as m1 where (3, m1.b) not in (select * from t as m2)")
result.Sort().Check(testkit.Rows("1", "2"))
result = tk.MustQuery("select m1.a from t as m1 where m1.a in (select m2.b+? from t as m2)", 1)
result.Check(testkit.Rows("2"))
tk.MustExec(`prepare stmt1 from 'select m1.a from t as m1 where m1.a in (select m2.b+? from t as m2)'`)
tk.MustExec("set @a = 1")
result = tk.MustQuery(`execute stmt1 using @a;`)
result.Check(testkit.Rows("2"))
tk.MustExec("set @a = 0")
result = tk.MustQuery(`execute stmt1 using @a;`)
result.Check(testkit.Rows("1"))
result = tk.MustQuery("select m1.a from t as m1 where m1.a in (1, 3, 5)")
result.Check(testkit.Rows("1"))
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1 (a float)")
tk.MustExec("insert t1 values (281.37)")
tk.MustQuery("select a from t1 where (a in (select a from t1))").Check(testkit.Rows("281.37"))
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1 (a int, b int)")
tk.MustExec("insert into t1 values (0,0),(1,1),(2,2),(3,3),(4,4)")
tk.MustExec("create table t2 (a int)")
tk.MustExec("insert into t2 values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)")
result = tk.MustQuery("select a from t1 where (1,1) in (select * from t2 s , t2 t where t1.a = s.a and s.a = t.a limit 1)")
result.Check(testkit.Rows("1"))
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1 (a int)")
tk.MustExec("create table t2 (a int)")
tk.MustExec("insert into t1 values (1),(2)")
tk.MustExec("insert into t2 values (1),(2)")
tk.MustExec("set @@session.tidb_opt_insubq_to_join_and_agg = 0")
result = tk.MustQuery("select * from t1 where a in (select * from t2)")
result.Sort().Check(testkit.Rows("1", "2"))
result = tk.MustQuery("select * from t1 where a in (select * from t2 where false)")
result.Check(testkit.Rows())
result = tk.MustQuery("select * from t1 where a not in (select * from t2 where false)")
result.Sort().Check(testkit.Rows("1", "2"))
tk.MustExec("set @@session.tidb_opt_insubq_to_join_and_agg = 1")
result = tk.MustQuery("select * from t1 where a in (select * from t2)")
result.Sort().Check(testkit.Rows("1", "2"))
result = tk.MustQuery("select * from t1 where a in (select * from t2 where false)")
result.Check(testkit.Rows())
result = tk.MustQuery("select * from t1 where a not in (select * from t2 where false)")
result.Sort().Check(testkit.Rows("1", "2"))
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1 (a int, key b (a))")
tk.MustExec("create table t2 (a int, key b (a))")
tk.MustExec("insert into t1 values (1),(2),(2)")
tk.MustExec("insert into t2 values (1),(2),(2)")
result = tk.MustQuery("select * from t1 where a in (select * from t2) order by a desc")
result.Check(testkit.Rows("2", "2", "1"))
result = tk.MustQuery("select * from t1 where a in (select count(*) from t2 where t1.a = t2.a) order by a desc")
result.Check(testkit.Rows("2", "2", "1"))
}
func (s *testSuiteJoin1) TestJoinLeak(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @@tidb_hash_join_concurrency=1")
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (d int)")
tk.MustExec("begin")
for i := 0; i < 1002; i++ {
tk.MustExec("insert t values (1)")
}
tk.MustExec("commit")
result, err := tk.Exec("select * from t t1 left join (select 1) t2 on 1")
c.Assert(err, IsNil)
req := result.NewChunk()
err = result.Next(context.Background(), req)
c.Assert(err, IsNil)
time.Sleep(time.Millisecond)
result.Close()
tk.MustExec("set @@tidb_hash_join_concurrency=5")
}
func (s *testSuiteJoin1) TestHashJoinExecEncodeDecodeRow(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t1 (id int)")
tk.MustExec("create table t2 (id int, name varchar(255), ts timestamp)")
tk.MustExec("insert into t1 values (1)")
tk.MustExec("insert into t2 values (1, 'xxx', '2003-06-09 10:51:26')")
result := tk.MustQuery("select ts from t1 inner join t2 where t2.name = 'xxx'")
result.Check(testkit.Rows("2003-06-09 10:51:26"))
}
func (s *testSuiteJoin1) TestSubqueryInJoinOn(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t2")
tk.MustExec("create table t1 (id int)")
tk.MustExec("create table t2 (id int)")
tk.MustExec("insert into t1 values (1)")
tk.MustExec("insert into t2 values (1)")
err := tk.ExecToErr("SELECT * FROM t1 JOIN t2 on (t2.id < all (SELECT 1))")
c.Check(err, NotNil)
}
func (s *testSuiteJoin1) TestIssue5255(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1, t2")
tk.MustExec("create table t1(a int, b date, c float, primary key(a, b))")
tk.MustExec("create table t2(a int primary key)")
tk.MustExec("insert into t1 values(1, '2017-11-29', 2.2)")
tk.MustExec("insert into t2 values(1)")
tk.MustQuery("select /*+ INL_JOIN(t1) */ * from t1 join t2 on t1.a=t2.a").Check(testkit.Rows("1 2017-11-29 2.2 1"))
tk.MustQuery("select /*+ INL_HASH_JOIN(t1) */ * from t1 join t2 on t1.a=t2.a").Check(testkit.Rows("1 2017-11-29 2.2 1"))
tk.MustQuery("select /*+ INL_MERGE_JOIN(t1) */ * from t1 join t2 on t1.a=t2.a").Check(testkit.Rows("1 2017-11-29 2.2 1"))
}
func (s *testSuiteJoin1) TestIssue5278(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t, tt")
tk.MustExec("create table t(a int, b int)")
tk.MustExec("create table tt(a varchar(10), b int)")