# TestMultiJoin drop table if exists t35,t40,t14,t42,t15,t7,t64,t19,t8,t57,t37,t44,t38,t18,t62,t4,t48,t31,t16,t12; create table t35(a35 int primary key, b35 int, x35 int); create table t40(a40 int primary key, b40 int, x40 int); create table t14(a14 int primary key, b14 int, x14 int); create table t42(a42 int primary key, b42 int, x42 int); create table t15(a15 int primary key, b15 int, x15 int); create table t7(a7 int primary key, b7 int, x7 int); create table t64(a64 int primary key, b64 int, x64 int); create table t19(a19 int primary key, b19 int, x19 int); create table t9(a9 int primary key, b9 int, x9 int); create table t8(a8 int primary key, b8 int, x8 int); create table t57(a57 int primary key, b57 int, x57 int); create table t37(a37 int primary key, b37 int, x37 int); create table t44(a44 int primary key, b44 int, x44 int); create table t38(a38 int primary key, b38 int, x38 int); create table t18(a18 int primary key, b18 int, x18 int); create table t62(a62 int primary key, b62 int, x62 int); create table t4(a4 int primary key, b4 int, x4 int); create table t48(a48 int primary key, b48 int, x48 int); create table t31(a31 int primary key, b31 int, x31 int); create table t16(a16 int primary key, b16 int, x16 int); create table t12(a12 int primary key, b12 int, x12 int); insert into t35 values(1,1,1); insert into t40 values(1,1,1); insert into t14 values(1,1,1); insert into t42 values(1,1,1); insert into t15 values(1,1,1); insert into t7 values(1,1,1); insert into t64 values(1,1,1); insert into t19 values(1,1,1); insert into t9 values(1,1,1); insert into t8 values(1,1,1); insert into t57 values(1,1,1); insert into t37 values(1,1,1); insert into t44 values(1,1,1); insert into t38 values(1,1,1); insert into t18 values(1,1,1); insert into t62 values(1,1,1); insert into t4 values(1,1,1); insert into t48 values(1,1,1); insert into t31 values(1,1,1); insert into t16 values(1,1,1); insert into t12 values(1,1,1); insert into t35 values(7,7,7); insert into t40 values(7,7,7); insert into t14 values(7,7,7); insert into t42 values(7,7,7); insert into t15 values(7,7,7); insert into t7 values(7,7,7); insert into t64 values(7,7,7); insert into t19 values(7,7,7); insert into t9 values(7,7,7); insert into t8 values(7,7,7); insert into t57 values(7,7,7); insert into t37 values(7,7,7); insert into t44 values(7,7,7); insert into t38 values(7,7,7); insert into t18 values(7,7,7); insert into t62 values(7,7,7); insert into t4 values(7,7,7); insert into t48 values(7,7,7); insert into t31 values(7,7,7); insert into t16 values(7,7,7); insert into t12 values(7,7,7); 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; # TestSubquerySameTable drop table if exists t; create table t (a int); insert t values (1), (2); select a from t where exists(select 1 from t as x where x.a < t.a); select a from t where not exists(select 1 from t as x where x.a < t.a); # TestInSubquery drop table if exists t; create table t (a int, b int); insert t values (1, 1), (2, 1); select m1.a from t as m1 where m1.a in (select m2.b from t as m2); --sorted_result select m1.a from t as m1 where (3, m1.b) not in (select * from t as m2); select m1.a from t as m1 where m1.a in (select m2.b+1 from t as m2); prepare stmt1 from 'select m1.a from t as m1 where m1.a in (select m2.b+? from t as m2)'; set @a = 1; execute stmt1 using @a; set @a = 0; execute stmt1 using @a; select m1.a from t as m1 where m1.a in (1, 3, 5); drop table if exists t1; create table t1 (a float); insert t1 values (281.37); select a from t1 where (a in (select a from t1)); drop table if exists t1, t2; create table t1 (a int, b int); insert into t1 values (0,0),(1,1),(2,2),(3,3),(4,4); create table t2 (a int); insert into t2 values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10); 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); drop table if exists t1, t2; create table t1 (a int); create table t2 (a int); insert into t1 values (1),(2); insert into t2 values (1),(2); set @@session.tidb_opt_insubq_to_join_and_agg = 0; --sorted_result select * from t1 where a in (select * from t2); --sorted_result select * from t1 where a in (select * from t2 where false); --sorted_result select * from t1 where a not in (select * from t2 where false); set @@session.tidb_opt_insubq_to_join_and_agg = 1; --sorted_result select * from t1 where a in (select * from t2); --sorted_result select * from t1 where a in (select * from t2 where false); --sorted_result select * from t1 where a not in (select * from t2 where false); drop table if exists t1, t2; create table t1 (a int, key b (a)); create table t2 (a int, key b (a)); insert into t1 values (1),(2),(2); insert into t2 values (1),(2),(2); --sorted_result select * from t1 where a in (select * from t2) order by a desc; --sorted_result select * from t1 where a in (select count(*) from t2 where t1.a = t2.a) order by a desc; set @@session.tidb_opt_insubq_to_join_and_agg = default; # TestIssue5255 drop table if exists t1, t2; create table t1(a int, b date, c float, primary key(a, b)); create table t2(a int primary key); insert into t1 values(1, '2017-11-29', 2.2); insert into t2 values(1); select /*+ INL_JOIN(t1) */ * from t1 join t2 on t1.a=t2.a; select /*+ INL_HASH_JOIN(t1) */ * from t1 join t2 on t1.a=t2.a; select /*+ INL_MERGE_JOIN(t1) */ * from t1 join t2 on t1.a=t2.a; # TestIssue5278 drop table if exists t, tt; create table t(a int, b int); create table tt(a varchar(10), b int); insert into t values(1, 1); select * from t left join tt on t.a=tt.a left join t ttt on t.a=ttt.a; # TestIssue15686 drop table if exists t, k; create table k (a int, pk int primary key, index(a)); create table t (a int, pk int primary key, index(a)); insert into k values(0,8),(0,23),(1,21),(1,33),(1,52),(2,17),(2,34),(2,39),(2,40),(2,66),(2,67),(3,9),(3,25),(3,41),(3,48),(4,4),(4,11),(4,15),(4,26),(4,27),(4,31),(4,35),(4,45),(4,47),(4,49); insert into t values(3,4),(3,5),(3,27),(3,29),(3,57),(3,58),(3,79),(3,84),(3,92),(3,95); select /*+ inl_join(t) */ count(*) from k left join t on k.a = t.a and k.pk > t.pk; select /*+ inl_hash_join(t) */ count(*) from k left join t on k.a = t.a and k.pk > t.pk; select /*+ inl_merge_join(t) */ count(*) from k left join t on k.a = t.a and k.pk > t.pk; # TestMergejoinOrder drop table if exists t1, t2; create table t1(a bigint primary key, b bigint); create table t2(a bigint primary key, b bigint); insert into t1 values(1, 100), (2, 100), (3, 100), (4, 100), (5, 100); insert into t2 select a*100, b*100 from t1; explain format = 'brief' select /*+ TIDB_SMJ(t2) */ * from t1 left outer join t2 on t1.a=t2.a and t1.a!=3 order by t1.a; set @@tidb_init_chunk_size=1; select /*+ TIDB_SMJ(t2) */ * from t1 left outer join t2 on t1.a=t2.a and t1.a!=3 order by t1.a; drop table if exists t; create table t(a bigint, b bigint, index idx_1(a,b)); insert into t values(1, 1), (1, 2), (2, 1), (2, 2); select /*+ TIDB_SMJ(t1, t2) */ * from t t1 join t t2 on t1.b = t2.b and t1.a=t2.a; drop table if exists t; create table t(a decimal(6,2), index idx(a)); insert into t values(1.01), (2.02), (NULL); select /*+ TIDB_SMJ(t1) */ t1.a from t t1 join t t2 on t1.a=t2.a order by t1.a; set @@tidb_init_chunk_size=default; # TestEmbeddedOuterJoin drop table if exists t1, t2; create table t1(a int, b int); create table t2(a int, b int); insert into t1 values(1, 1); select * from (t1 left join t2 on t1.a = t2.a) left join (t2 t3 left join t2 t4 on t3.a = t4.a) on t2.b = 1; # TestJoinDifferentDecimals Drop table if exists t1; Create table t1 (v int); Insert into t1 value (1); Insert into t1 value (2); Insert into t1 value (3); Drop table if exists t2; Create table t2 (v decimal(12, 3)); Insert into t2 value (1); Insert into t2 value (2.0); Insert into t2 value (000003.000000); Select * from t1, t2 where t1.v = t2.v order by t1.v; # TestScalarFuncNullSemiJoin drop table if exists t; create table t(a int, b int); insert into t values(null, 1), (1, 2); drop table if exists s; create table s(a varchar(20), b varchar(20)); insert into s values(null, '1'); select a in (select a from s) from t; drop table s; create table s(a int, b int); insert into s values(null, 1); select a in (select a+b from s) from t; # TestInjectProjOnTopN drop table if exists t1; drop table if exists t2; create table t1(a bigint, b bigint); create table t2(a bigint, b bigint); insert into t1 values(1, 1); select t1.a+t1.b as result from t1 left join t2 on 1 = 0 order by result limit 20; # TestIssue11544 drop table if exists 11544t, 11544tt; create table 11544t(a int); create table 11544tt(a int, b varchar(10), index idx(a, b(3))); insert into 11544t values(1); insert into 11544tt values(1, 'aaaaaaa'), (1, 'aaaabbb'), (1, 'aaaacccc'); select /*+ INL_JOIN(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and (tt.b = 'aaaaaaa' or tt.b = 'aaaabbb'); select /*+ INL_HASH_JOIN(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and (tt.b = 'aaaaaaa' or tt.b = 'aaaabbb'); --sorted_result select /*+ INL_MERGE_JOIN(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and (tt.b = 'aaaaaaa' or tt.b = 'aaaabbb'); select /*+ INL_JOIN(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and tt.b in ('aaaaaaa', 'aaaabbb', 'aaaacccc'); select /*+ INL_HASH_JOIN(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and tt.b in ('aaaaaaa', 'aaaabbb', 'aaaacccc'); --sorted_result select /*+ INL_MERGE_JOIN(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and tt.b in ('aaaaaaa', 'aaaabbb', 'aaaacccc'); # TestIssue11390 drop table if exists 11390t; create table 11390t (k1 int unsigned, k2 int unsigned, key(k1, k2)); insert into 11390t values(1, 1); select /*+ INL_JOIN(t1, t2) */ * from 11390t t1, 11390t t2 where t1.k2 > 0 and t1.k2 = t2.k2 and t2.k1=1; select /*+ INL_HASH_JOIN(t1, t2) */ * from 11390t t1, 11390t t2 where t1.k2 > 0 and t1.k2 = t2.k2 and t2.k1=1; select /*+ INL_MERGE_JOIN(t1, t2) */ * from 11390t t1, 11390t t2 where t1.k2 > 0 and t1.k2 = t2.k2 and t2.k1=1; # TestIssue13177 drop table if exists t1, t2; create table t1(a varchar(20), b int, c int); create table t2(a varchar(20), b int, c int, primary key(a, b)); insert into t1 values("abcd", 1, 1), ("bacd", 2, 2), ("cbad", 3, 3); insert into t2 values("bcd", 1, 1), ("acd", 2, 2), ("bad", 3, 3); --sorted_result select /*+ inl_join(t1, t2) */ * from t1 join t2 on substr(t1.a, 2, 4) = t2.a and t1.b = t2.b where t1.c between 1 and 5; --sorted_result select /*+ inl_hash_join(t1, t2) */ * from t1 join t2 on substr(t1.a, 2, 4) = t2.a and t1.b = t2.b where t1.c between 1 and 5; --sorted_result select /*+ inl_merge_join(t1, t2) */ * from t1 join t2 on substr(t1.a, 2, 4) = t2.a and t1.b = t2.b where t1.c between 1 and 5; --sorted_result select /*+ inl_join(t1, t2) */ t1.* from t1 join t2 on substr(t1.a, 2, 4) = t2.a and t1.b = t2.b where t1.c between 1 and 5; --sorted_result select /*+ inl_hash_join(t1, t2) */ t1.* from t1 join t2 on substr(t1.a, 2, 4) = t2.a and t1.b = t2.b where t1.c between 1 and 5; --sorted_result select /*+ inl_merge_join(t1, t2) */ t1.* from t1 join t2 on substr(t1.a, 2, 4) = t2.a and t1.b = t2.b where t1.c between 1 and 5; # TestIssue14514 drop table if exists t; create table t (pk varchar(14) primary key, a varchar(12)); select * from (select t1.pk or '/' as c from t as t1 left join t as t2 on t1.a = t2.pk) as t where t.c = 1; # TestOuterMatchStatusIssue14742 drop table if exists testjoin; create table testjoin(a int); set @@tidb_max_chunk_size=2; insert into testjoin values (NULL); insert into testjoin values (1); insert into testjoin values (2), (2), (2); SELECT * FROM testjoin t1 RIGHT JOIN testjoin t2 ON t1.a > t2.a order by t1.a, t2.a; set @@tidb_max_chunk_size=default; # TestIssue18564 drop table if exists t1, t2; create table t1(a int, b int, primary key(a), index idx(b,a)); create table t2(a int, b int, primary key(a), index idx(b,a)); insert into t1 values(1, 1); insert into t2 values(1, 1); select /*+ INL_JOIN(t1) */ * from t1 FORCE INDEX (idx) join t2 on t1.b=t2.b and t1.a = t2.a; # TestInvalidEnumVal set sql_mode = ''; drop table if exists t1; create table t1(c1 enum('a', 'b')); insert into t1 values('a'); insert into t1 values(0); insert into t1 values(100); select /*+ hash_join(t_alias1, t_alias2)*/ * from t1 t_alias1 inner join t1 t_alias2 on t_alias1.c1 = t_alias2.c1; set sql_mode = default; # TestApplyOuterAggEmptyInput drop table if exists t1, t2; create table t1(a int); create table t2(a int); insert into t1 values(1); insert into t2 values(1); select count(1), (select count(1) from t2 where t2.a > t1.a) as field from t1 where t1.a = 100; select /*+ agg_to_cop() */ count(1), (select count(1) from t2 where t2.a > t1.a) as field from t1 where t1.a = 100; select count(1), (select count(1) from t2 where t2.a > t1.a) as field from t1 where t1.a = 1; select /*+ agg_to_cop() */ count(1), (select count(1) from t2 where t2.a > t1.a) as field from t1 where t1.a = 1; # TestIssue19112 drop table if exists t1, t2; create table t1 ( c_int int, c_decimal decimal(12, 6), key(c_int), unique key(c_decimal) ); create table t2 like t1; insert into t1 (c_int, c_decimal) values (1, 4.064000), (2, 0.257000), (3, 1.010000); insert into t2 (c_int, c_decimal) values (1, 4.064000), (3, 1.010000); select /*+ HASH_JOIN(t1,t2) */ * from t1 join t2 on t1.c_decimal = t2.c_decimal order by t1.c_int; # TestIssue11896 drop table if exists t; drop table if exists t1; create table t(c1 bigint); create table t1(c1 bit(64)); insert into t value(1); insert into t1 value(1); select t.c1, hex(t1.c1) from t, t1 where t.c1 = t1.c1; drop table if exists t; drop table if exists t1; create table t(c1 int); create table t1(c1 bit(32)); insert into t value(1); insert into t1 value(1); select t.c1, hex(t1.c1) from t, t1 where t.c1 = t1.c1; drop table if exists t; drop table if exists t1; create table t(c1 mediumint); create table t1(c1 bit(24)); insert into t value(1); insert into t1 value(1); select t.c1, hex(t1.c1) from t, t1 where t.c1 = t1.c1; drop table if exists t; drop table if exists t1; create table t(c1 smallint); create table t1(c1 bit(16)); insert into t value(1); insert into t1 value(1); select t.c1, hex(t1.c1) from t, t1 where t.c1 = t1.c1; drop table if exists t; drop table if exists t1; create table t(c1 tinyint); create table t1(c1 bit(8)); insert into t value(1); insert into t1 value(1); select t.c1, hex(t1.c1) from t, t1 where t.c1 = t1.c1; # TestIssue19498 drop table if exists t1; create table t1 (c_int int, primary key (c_int)); insert into t1 values (1),(2),(3),(4); drop table if exists t2; create table t2 (c_str varchar(40)); insert into t2 values ('zen sammet'); insert into t2 values ('happy fermat'); insert into t2 values ('happy archimedes'); insert into t2 values ('happy hypatia'); drop table if exists t3; create table t3 (c_int int, c_str varchar(40), primary key (c_int), key (c_str)); insert into t3 values (1, 'sweet hoover'); insert into t3 values (2, 'awesome elion'); insert into t3 values (3, 'hungry khayyam'); insert into t3 values (4, 'objective kapitsa'); select c_str, (select /*+ INL_JOIN(t1,t3) */ max(t1.c_int) from t1, t3 where t1.c_int = t3.c_int and t2.c_str > t3.c_str) q from t2 order by c_str; select c_str, (select /*+ INL_HASH_JOIN(t1,t3) */ max(t1.c_int) from t1, t3 where t1.c_int = t3.c_int and t2.c_str > t3.c_str) q from t2 order by c_str; select c_str, (select /*+ INL_MERGE_JOIN(t1,t3) */ max(t1.c_int) from t1, t3 where t1.c_int = t3.c_int and t2.c_str > t3.c_str) q from t2 order by c_str; # TestIssue19500 drop table if exists t1; create table t1 (c_int int, primary key (c_int)); insert into t1 values (1),(2),(3),(4),(5); drop table if exists t2; create table t2 (c_int int unsigned, c_str varchar(40), primary key (c_int), key (c_str)); insert into t2 values (1, 'dazzling panini'),(2, 'infallible perlman'),(3, 'recursing cannon'),(4, 'vigorous satoshi'),(5, 'vigilant gauss'),(6, 'nervous jackson'); drop table if exists t3; create table t3 (c_int int, c_str varchar(40), key (c_str)); insert into t3 values (1, 'sweet morse'),(2, 'reverent golick'),(3, 'clever rubin'),(4, 'flamboyant morse'); select (select (select sum(c_int) from t3 where t3.c_str > t2.c_str) from t2 where t2.c_int > t1.c_int order by c_int limit 1) q from t1 order by q; # TestIssue20710 drop table if exists t; drop table if exists s; create table t(a int, b int); create table s(a int, b int, index(a)); insert into t values(1,1),(1,2),(2,2); insert into s values(1,1),(2,2),(2,1); --enable_warnings select /*+ inl_join(s) */ * from t join s on t.a=s.a and t.b = s.b; select /*+ inl_join(s) */ * from t join s on t.a=s.a and t.b = s.a; select /*+ inl_join(s) */ * from t join s on t.a=s.a and t.a = s.b; select /*+ inl_join(s) */ * from t join s on t.a=s.a and t.b = s.b; select /*+ inl_join(s) */ * from t join s on t.a=s.a and t.b = s.a; select /*+ inl_join(s) */ * from t join s on t.a=s.a and t.a = s.b; --disable_warnings # TestIssue20219 drop table if exists t,s ; CREATE TABLE `t` ( `a` set('a','b','c','d','e','f','g','h','i','j') DEFAULT NULL ); insert into t values('i'), ('j'); CREATE TABLE `s` ( `a` char(1) DEFAULT NULL, KEY `a` (`a`) ); insert into s values('i'), ('j'); --enable_warnings select /*+ inl_hash_join(s)*/ t.a from t left join s on t.a = s.a; select /*+ inl_join(s)*/ t.a from t left join s on t.a = s.a; --disable_warnings # TestIssue25902 drop table if exists tt1,tt2,tt3; ; create table tt1 (ts timestamp); create table tt2 (ts varchar(32)); create table tt3 (ts datetime); insert into tt1 values ("2001-01-01 00:00:00"); insert into tt2 values ("2001-01-01 00:00:00"); insert into tt3 values ("2001-01-01 00:00:00"); select * from tt1 where ts in (select ts from tt2); select * from tt1 where ts in (select ts from tt3); set @@session.time_zone = '+10:00'; select * from tt1 where ts in (select ts from tt2); set @@session.time_zone = default; # TestOuterJoin drop table if exists t1, t2, t3, t4; create table t1(a int, b int, c int); create table t2(a int, b int, c int); create table t3(a int, b int, c int); create table t4(a int, b int, c int); INSERT INTO t1 VALUES (1,3,0), (2,2,0), (3,2,0); INSERT INTO t2 VALUES (3,3,0), (4,2,0), (5,3,0); INSERT INTO t3 VALUES (1,2,0), (2,2,0); INSERT INTO t4 VALUES (3,2,0), (4,2,0); SELECT t2.a,t2.b,t3.a,t3.b,t4.a,t4.b from (t3, t4) left join (t1, t2) on t3.a=1 AND t3.b=t2.b AND t2.b=t4.b order by 1, 2, 3, 4, 5; drop table if exists t1, t2, t3; create table t1 (a1 int, a2 int); create table t2 (b1 int not null, b2 int); create table t3 (c1 int, c2 int); insert into t1 values (1,2), (2,2), (3,2); insert into t2 values (1,3), (2,3); insert into t3 values (2,4), (3,4); select * from t1 left join t2 on b1 = a1 left join t3 on c1 = a1 and b1 is null order by 1, 2, 3, 4, 5, 6; # TestJoinPanic2 set sql_mode = 'ONLY_FULL_GROUP_BY'; drop table if exists events; create table events (clock int, source int); SELECT * FROM events e JOIN (SELECT MAX(clock) AS clock FROM events e2 GROUP BY e2.source) e3 ON e3.clock=e.clock; --error 1055 SELECT * FROM events e JOIN (SELECT clock FROM events e2 GROUP BY e2.source) e3 ON e3.clock=e.clock; drop table if exists tpj1,tpj2; create table tpj1 (id int, b int, unique index (id)); create table tpj2 (id int, b int, unique index (id)); insert into tpj1 values (1,1); insert into tpj2 values (1,1); select tpj1.b,tpj2.b from tpj1 left join tpj2 on tpj1.id=tpj2.id where tpj1.id=1; set sql_mode = default; # TestSubqueryInJoinOn drop table if exists t1; drop table if exists t2; create table t1 (id int); create table t2 (id int); insert into t1 values (1); insert into t2 values (1); --error 1105 SELECT * FROM t1 JOIN t2 on (t2.id < all (SELECT 1)); # TestIssue15850JoinNullValue --enable_warnings SELECT * FROM (select null) v NATURAL LEFT JOIN (select null) v1; --disable_warnings drop table if exists t0; drop view if exists v0; CREATE TABLE t0(c0 TEXT); CREATE VIEW v0(c0) AS SELECT NULL; --enable_warnings SELECT /*+ HASH_JOIN(v0) */ * FROM v0 NATURAL LEFT JOIN t0; SELECT /*+ MERGE_JOIN(v0) */ * FROM v0 NATURAL LEFT JOIN t0; --disable_warnings