You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2097 lines
74 KiB

16 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
BUG#834534: Assertion `0' failed in replace_where_subcondition with semijoin subquery in HAVING - The problem was that the code that made the check whether the subquery is an AND-part of the WHERE clause didn't work correctly for nested subqueries. In particular, grand-child subquery in HAVING was treated as if it was in the WHERE, which eventually caused an assert when replace_where_subcondition looked for the subquery predicate in the WHERE and couldn't find it there. - The fix: Removed implementation of "thd_marker approach". thd->thd_marker was used to determine the location of subquery predicate: setup_conds() would set accordingly it when making the {where|on_expr}->fix_fields(...) call so that AND-parts of the WHERE/ON clauses can determine they are the AND-parts. Item_cond_or::fix_fields(), Item_func::fix_fields(), Item_subselect::fix_fields (this one was missed), and all other items-that-contain-items had to reset thd->thd_marker before calling fix_fields() for their children items, so that the children can see they are not AND-parts of WHERE/ON. - The "thd_marker approach" required that a lot of code in different locations maintains correct value of thd->thd_marker, so it was replaced with: - The new approach with mark_as_condition_AND_part does not keep context in thd->thd_marker. Instead, setup_conds() now calls {where|on_expr}->mark_as_condition_AND_part() and implementations of that function make sure that: - parts of AND-expressions get the mark_as_condition_AND_part() call - Item_in_subselect objects record that they are AND-parts of WHERE/ON
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
  1. drop table if exists t0, t1, t2, t3, t4, t5, t10, t11, t12;
  2. drop view if exists v1, v2, v3, v4;
  3. drop procedure if exists p1;
  4. set @subselect_sj_tmp= @@optimizer_switch;
  5. set optimizer_switch='semijoin=on,firstmatch=on,loosescan=on';
  6. set optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on';
  7. set @save_optimizer_switch=@@optimizer_switch;
  8. create table t0 (a int);
  9. insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  10. create table t1(a int, b int);
  11. insert into t1 values (0,0),(1,1),(2,2);
  12. create table t2 as select * from t1;
  13. create table t11(a int, b int);
  14. create table t10 (pk int, a int, primary key(pk));
  15. insert into t10 select a,a from t0;
  16. create table t12 like t10;
  17. insert into t12 select * from t10;
  18. Flattened because of dependency, t10=func(t1)
  19. explain select * from t1 where a in (select pk from t10);
  20. id select_type table type possible_keys key key_len ref rows Extra
  21. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
  22. 1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using index
  23. select * from t1 where a in (select pk from t10);
  24. a b
  25. 0 0
  26. 1 1
  27. 2 2
  28. A confluent case of dependency
  29. explain select * from t1 where a in (select a from t10 where pk=12);
  30. id select_type table type possible_keys key key_len ref rows Extra
  31. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  32. select * from t1 where a in (select a from t10 where pk=12);
  33. a b
  34. explain select * from t1 where a in (select a from t10 where pk=9);
  35. id select_type table type possible_keys key key_len ref rows Extra
  36. 1 PRIMARY t10 const PRIMARY PRIMARY 4 const 1
  37. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
  38. select * from t1 where a in (select a from t10 where pk=9);
  39. a b
  40. An empty table inside
  41. explain select * from t1 where a in (select a from t11);
  42. id select_type table type possible_keys key key_len ref rows Extra
  43. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  44. select * from t1 where a in (select a from t11);
  45. a b
  46. explain select * from t1 where a in (select pk from t10) and b in (select pk from t10);
  47. id select_type table type possible_keys key key_len ref rows Extra
  48. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
  49. 1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using index
  50. 1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.b 1 Using index
  51. select * from t1 where a in (select pk from t10) and b in (select pk from t10);
  52. a b
  53. 0 0
  54. 1 1
  55. 2 2
  56. flattening a nested subquery
  57. explain select * from t1 where a in (select pk from t10 where t10.a in (select pk from t12));
  58. id select_type table type possible_keys key key_len ref rows Extra
  59. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
  60. 1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using where
  61. 1 PRIMARY t12 eq_ref PRIMARY PRIMARY 4 test.t10.a 1 Using index
  62. select * from t1 where a in (select pk from t10 where t10.a in (select pk from t12));
  63. a b
  64. 0 0
  65. 1 1
  66. 2 2
  67. flattening subquery w/ several tables
  68. explain extended select * from t1 where a in (select t10.pk from t10, t12 where t12.pk=t10.a);
  69. id select_type table type possible_keys key key_len ref rows filtered Extra
  70. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
  71. 1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 100.00 Using where
  72. 1 PRIMARY t12 eq_ref PRIMARY PRIMARY 4 test.t10.a 1 100.00 Using index
  73. Warnings:
  74. Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t10` join `test`.`t12` join `test`.`t1` where ((`test`.`t10`.`pk` = `test`.`t1`.`a`) and (`test`.`t12`.`pk` = `test`.`t10`.`a`))
  75. subqueries within outer joins go into ON expr.
  76. explAin extended
  77. select * from t1 left join (t2 A, t2 B) on ( A.A= t1.A And B.A in (select pk from t10));
  78. id select_type tABle type possiBle_keys key key_len ref rows filtered ExtrA
  79. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
  80. 1 PRIMARY A ALL NULL NULL NULL NULL 3 100.00 Using where
  81. 1 PRIMARY B ALL NULL NULL NULL NULL 3 100.00 Using where
  82. 2 MATERIALIZED t10 index PRIMARY PRIMARY 4 NULL 10 100.00 Using index
  83. Warnings:
  84. Note 1003 select `test`.`t1`.`A` AS `A`,`test`.`t1`.`B` AS `B`,`test`.`A`.`A` AS `A`,`test`.`A`.`B` AS `B`,`test`.`B`.`A` AS `A`,`test`.`B`.`B` AS `B` from `test`.`t1` left join (`test`.`t2` `A` join `test`.`t2` `B`) on((<in_optimizer>(`test`.`B`.`A`,`test`.`B`.`A` in ( <mAteriAlize> (select `test`.`t10`.`pk` from `test`.`t10` ), <primAry_index_lookup>(`test`.`B`.`A` in <temporAry tABle> on distinct_key where ((`test`.`B`.`A` = `<suBquery2>`.`pk`))))) And (`test`.`A`.`A` = `test`.`t1`.`A`))) where 1
  85. t2 should be wrapped into OJ-nest, so we have "t1 LJ (t2 J t10)"
  86. explAin extended
  87. select * from t1 left join t2 on (t2.A= t1.A And t2.A in (select pk from t10));
  88. id select_type tABle type possiBle_keys key key_len ref rows filtered ExtrA
  89. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
  90. 1 PRIMARY t2 ALL NULL NULL NULL NULL 3 100.00 Using where
  91. 2 MATERIALIZED t10 index PRIMARY PRIMARY 4 NULL 10 100.00 Using index
  92. Warnings:
  93. Note 1003 select `test`.`t1`.`A` AS `A`,`test`.`t1`.`B` AS `B`,`test`.`t2`.`A` AS `A`,`test`.`t2`.`B` AS `B` from `test`.`t1` left join `test`.`t2` on((<in_optimizer>(`test`.`t2`.`A`,`test`.`t2`.`A` in ( <mAteriAlize> (select `test`.`t10`.`pk` from `test`.`t10` ), <primAry_index_lookup>(`test`.`t2`.`A` in <temporAry tABle> on distinct_key where ((`test`.`t2`.`A` = `<suBquery2>`.`pk`))))) And (`test`.`t2`.`A` = `test`.`t1`.`A`))) where 1
  94. we shouldn't flatten if we're going to get a join of > MAX_TABLES.
  95. explain select * from
  96. t1 s00, t1 s01, t1 s02, t1 s03, t1 s04,t1 s05,t1 s06,t1 s07,t1 s08,t1 s09,
  97. t1 s10, t1 s11, t1 s12, t1 s13, t1 s14,t1 s15,t1 s16,t1 s17,t1 s18,t1 s19,
  98. t1 s20, t1 s21, t1 s22, t1 s23, t1 s24,t1 s25,t1 s26,t1 s27,t1 s28,t1 s29,
  99. t1 s30, t1 s31, t1 s32, t1 s33, t1 s34,t1 s35,t1 s36,t1 s37,t1 s38,t1 s39,
  100. t1 s40, t1 s41, t1 s42, t1 s43, t1 s44,t1 s45,t1 s46,t1 s47,t1 s48,t1 s49
  101. where
  102. s00.a in (
  103. select m00.a from
  104. t1 m00, t1 m01, t1 m02, t1 m03, t1 m04,t1 m05,t1 m06,t1 m07,t1 m08,t1 m09,
  105. t1 m10, t1 m11, t1 m12, t1 m13, t1 m14,t1 m15,t1 m16,t1 m17,t1 m18,t1 m19
  106. );
  107. id select_type table type possible_keys key key_len ref rows Extra
  108. 1 PRIMARY s00 ALL NULL NULL NULL NULL 3 Using where
  109. 1 PRIMARY s01 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  110. 1 PRIMARY s02 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  111. 1 PRIMARY s03 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  112. 1 PRIMARY s04 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  113. 1 PRIMARY s05 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  114. 1 PRIMARY s06 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  115. 1 PRIMARY s07 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  116. 1 PRIMARY s08 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  117. 1 PRIMARY s09 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  118. 1 PRIMARY s10 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  119. 1 PRIMARY s11 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  120. 1 PRIMARY s12 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  121. 1 PRIMARY s13 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  122. 1 PRIMARY s14 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  123. 1 PRIMARY s15 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  124. 1 PRIMARY s16 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  125. 1 PRIMARY s17 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  126. 1 PRIMARY s18 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  127. 1 PRIMARY s19 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  128. 1 PRIMARY s20 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  129. 1 PRIMARY s21 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  130. 1 PRIMARY s22 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  131. 1 PRIMARY s23 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  132. 1 PRIMARY s24 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  133. 1 PRIMARY s25 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  134. 1 PRIMARY s26 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  135. 1 PRIMARY s27 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  136. 1 PRIMARY s28 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  137. 1 PRIMARY s29 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  138. 1 PRIMARY s30 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  139. 1 PRIMARY s31 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  140. 1 PRIMARY s32 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  141. 1 PRIMARY s33 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  142. 1 PRIMARY s34 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  143. 1 PRIMARY s35 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  144. 1 PRIMARY s36 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  145. 1 PRIMARY s37 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  146. 1 PRIMARY s38 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  147. 1 PRIMARY s39 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  148. 1 PRIMARY s40 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  149. 1 PRIMARY s41 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  150. 1 PRIMARY s42 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  151. 1 PRIMARY s43 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  152. 1 PRIMARY s44 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  153. 1 PRIMARY s45 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  154. 1 PRIMARY s46 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  155. 1 PRIMARY s47 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  156. 1 PRIMARY s48 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  157. 1 PRIMARY s49 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  158. 2 DEPENDENT SUBQUERY m00 ALL NULL NULL NULL NULL 3 Using where
  159. 2 DEPENDENT SUBQUERY m01 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  160. 2 DEPENDENT SUBQUERY m02 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  161. 2 DEPENDENT SUBQUERY m03 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  162. 2 DEPENDENT SUBQUERY m04 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  163. 2 DEPENDENT SUBQUERY m05 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  164. 2 DEPENDENT SUBQUERY m06 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  165. 2 DEPENDENT SUBQUERY m07 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  166. 2 DEPENDENT SUBQUERY m08 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  167. 2 DEPENDENT SUBQUERY m09 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  168. 2 DEPENDENT SUBQUERY m10 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  169. 2 DEPENDENT SUBQUERY m11 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  170. 2 DEPENDENT SUBQUERY m12 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  171. 2 DEPENDENT SUBQUERY m13 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  172. 2 DEPENDENT SUBQUERY m14 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  173. 2 DEPENDENT SUBQUERY m15 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  174. 2 DEPENDENT SUBQUERY m16 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  175. 2 DEPENDENT SUBQUERY m17 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  176. 2 DEPENDENT SUBQUERY m18 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  177. 2 DEPENDENT SUBQUERY m19 ALL NULL NULL NULL NULL 3 Using where; Using join buffer (flat, BNL join)
  178. select * from
  179. t1 left join t2 on (t2.a= t1.a and t2.a in (select pk from t10))
  180. where t1.a < 5;
  181. a b a b
  182. 0 0 0 0
  183. 1 1 1 1
  184. 2 2 2 2
  185. prepare s1 from
  186. ' select * from
  187. t1 left join t2 on (t2.a= t1.a and t2.a in (select pk from t10))
  188. where t1.a < 5';
  189. execute s1;
  190. a b a b
  191. 0 0 0 0
  192. 1 1 1 1
  193. 2 2 2 2
  194. execute s1;
  195. a b a b
  196. 0 0 0 0
  197. 1 1 1 1
  198. 2 2 2 2
  199. insert into t1 select (A.a + 10 * B.a),1 from t0 A, t0 B;
  200. explain extended select * from t1 where a in (select pk from t10 where pk<3);
  201. id select_type table type possible_keys key key_len ref rows filtered Extra
  202. 1 PRIMARY t10 range PRIMARY PRIMARY 4 NULL 4 100.00 Using where; Using index
  203. 1 PRIMARY t1 ALL NULL NULL NULL NULL 103 100.00 Using where; Using join buffer (flat, BNL join)
  204. Warnings:
  205. Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t10` join `test`.`t1` where ((`test`.`t1`.`a` = `test`.`t10`.`pk`) and (`test`.`t10`.`pk` < 3))
  206. drop table t0, t1, t2;
  207. drop table t10, t11, t12;
  208. Bug#37899: Wrongly checked optimization prerequisite caused failed
  209. assertion.
  210. CREATE TABLE t1 (
  211. `pk` int(11),
  212. `varchar_nokey` varchar(5)
  213. );
  214. INSERT INTO t1 VALUES
  215. (1,'qk'),(2,'j'),(3,'aew');
  216. SELECT *
  217. FROM t1
  218. WHERE varchar_nokey IN (
  219. SELECT
  220. varchar_nokey
  221. FROM
  222. t1
  223. ) XOR pk = 30;
  224. pk varchar_nokey
  225. 1 qk
  226. 2 j
  227. 3 aew
  228. drop table t1;
  229. #
  230. # BUG#41842: Semi-join materialization strategy crashes when the upper query has HAVING
  231. #
  232. CREATE TABLE t1 (
  233. pk int(11) NOT NULL AUTO_INCREMENT,
  234. int_nokey int(11) NOT NULL,
  235. time_key time NOT NULL,
  236. datetime_key datetime NOT NULL,
  237. datetime_nokey datetime NOT NULL,
  238. varchar_key varchar(1) NOT NULL,
  239. varchar_nokey varchar(1) NOT NULL,
  240. PRIMARY KEY (pk),
  241. KEY time_key (time_key),
  242. KEY datetime_key (datetime_key),
  243. KEY varchar_key (varchar_key)
  244. );
  245. INSERT INTO t1 VALUES
  246. (1,0, '00:16:10','2008-09-03 14:25:40','2008-09-03 14:25:40','h','h'),
  247. (2,7, '00:00:00','2001-01-13 00:00:00','2001-01-13 00:00:00','',''),
  248. (3,0, '00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','x','x'),
  249. (4,2, '16:29:24','2000-10-16 01:39:08','2000-10-16 01:39:08','w','w'),
  250. (5,1, '09:23:32','0000-00-00 00:00:00','0000-00-00 00:00:00','p','p'),
  251. (6,3, '00:00:00','2007-12-02 00:00:00','2007-12-02 00:00:00','o','o'),
  252. (7,3, '00:00:00','2008-09-11 00:00:00','2008-09-11 00:00:00','',''),
  253. (8,0, '13:59:04','0000-00-00 00:00:00','0000-00-00 00:00:00','s','s'),
  254. (9,7, '09:01:06','0000-00-00 00:00:00','0000-00-00 00:00:00','d','d'),
  255. (10,5,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','n','n'),
  256. (11,0,'21:06:46','0000-00-00 00:00:00','0000-00-00 00:00:00','o','o'),
  257. (12,2,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','',''),
  258. (13,6,'14:45:34','2003-07-28 02:34:08','2003-07-28 02:34:08','w','w'),
  259. (14,1,'15:04:12','0000-00-00 00:00:00','0000-00-00 00:00:00','o','o'),
  260. (15,0,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','x','x'),
  261. (16,0,'15:55:23','2004-03-17 00:32:27','2004-03-17 00:32:27','p','p'),
  262. (17,1,'16:30:00','2004-12-27 19:20:00','2004-12-27 19:20:00','d','d'),
  263. (18,0,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','h','h'),
  264. (19,0,'14:13:26','2008-11-09 05:53:48','2008-11-09 05:53:48','o','o'),
  265. (20,0,'00:00:00','2009-10-11 06:58:04','2009-10-11 06:58:04','k','k');
  266. CREATE TABLE t2 (
  267. pk int(11) NOT NULL AUTO_INCREMENT,
  268. int_nokey int(11) NOT NULL,
  269. time_key time NOT NULL,
  270. datetime_key datetime NOT NULL,
  271. datetime_nokey datetime NOT NULL,
  272. varchar_key varchar(1) NOT NULL,
  273. varchar_nokey varchar(1) NOT NULL,
  274. PRIMARY KEY (pk),
  275. KEY time_key (time_key),
  276. KEY datetime_key (datetime_key),
  277. KEY varchar_key (varchar_key)
  278. );
  279. INSERT INTO t2 VALUES
  280. (10,0,'19:39:13','0000-00-00 00:00:00','0000-00-00 00:00:00','g','g'),
  281. (11,8,'03:43:53','0000-00-00 00:00:00','0000-00-00 00:00:00','b','b');
  282. SELECT OUTR.datetime_nokey AS X FROM t1 AS OUTR
  283. WHERE
  284. OUTR.varchar_nokey IN (SELECT
  285. INNR . varchar_nokey AS Y
  286. FROM t2 AS INNR
  287. WHERE
  288. INNR . datetime_key >= INNR . time_key OR
  289. INNR . pk = INNR . int_nokey
  290. )
  291. AND OUTR . varchar_nokey <= 'w'
  292. HAVING X > '2012-12-12';
  293. X
  294. drop table t1, t2;
  295. #
  296. # Bug#45191: Incorrectly initialized semi-join led to a wrong result.
  297. #
  298. CREATE TABLE STAFF (EMPNUM CHAR(3) NOT NULL,
  299. EMPNAME CHAR(20), GRADE DECIMAL(4), CITY CHAR(15));
  300. CREATE TABLE PROJ (PNUM CHAR(3) NOT NULL,
  301. PNAME CHAR(20), PTYPE CHAR(6),
  302. BUDGET DECIMAL(9),
  303. CITY CHAR(15));
  304. CREATE TABLE WORKS (EMPNUM CHAR(3) NOT NULL,
  305. PNUM CHAR(3) NOT NULL, HOURS DECIMAL(5));
  306. INSERT INTO STAFF VALUES ('E1','Alice',12,'Deale');
  307. INSERT INTO STAFF VALUES ('E2','Betty',10,'Vienna');
  308. INSERT INTO STAFF VALUES ('E3','Carmen',13,'Vienna');
  309. INSERT INTO STAFF VALUES ('E4','Don',12,'Deale');
  310. INSERT INTO STAFF VALUES ('E5','Ed',13,'Akron');
  311. INSERT INTO PROJ VALUES ('P1','MXSS','Design',10000,'Deale');
  312. INSERT INTO PROJ VALUES ('P2','CALM','Code',30000,'Vienna');
  313. INSERT INTO PROJ VALUES ('P3','SDP','Test',30000,'Tampa');
  314. INSERT INTO PROJ VALUES ('P4','SDP','Design',20000,'Deale');
  315. INSERT INTO PROJ VALUES ('P5','IRM','Test',10000,'Vienna');
  316. INSERT INTO PROJ VALUES ('P6','PAYR','Design',50000,'Deale');
  317. INSERT INTO WORKS VALUES ('E1','P1',40);
  318. INSERT INTO WORKS VALUES ('E1','P2',20);
  319. INSERT INTO WORKS VALUES ('E1','P3',80);
  320. INSERT INTO WORKS VALUES ('E1','P4',20);
  321. INSERT INTO WORKS VALUES ('E1','P5',12);
  322. INSERT INTO WORKS VALUES ('E1','P6',12);
  323. INSERT INTO WORKS VALUES ('E2','P1',40);
  324. INSERT INTO WORKS VALUES ('E2','P2',80);
  325. INSERT INTO WORKS VALUES ('E3','P2',20);
  326. INSERT INTO WORKS VALUES ('E4','P2',20);
  327. INSERT INTO WORKS VALUES ('E4','P4',40);
  328. INSERT INTO WORKS VALUES ('E4','P5',80);
  329. set optimizer_switch=@save_optimizer_switch;
  330. set optimizer_switch='materialization=off';
  331. explain SELECT EMPNUM, EMPNAME
  332. FROM STAFF
  333. WHERE EMPNUM IN
  334. (SELECT EMPNUM FROM WORKS
  335. WHERE PNUM IN
  336. (SELECT PNUM FROM PROJ));
  337. id select_type table type possible_keys key key_len ref rows Extra
  338. 1 PRIMARY STAFF ALL NULL NULL NULL NULL 5
  339. 1 PRIMARY PROJ ALL NULL NULL NULL NULL 6
  340. 1 PRIMARY WORKS ALL NULL NULL NULL NULL 12 Using where; FirstMatch(STAFF)
  341. SELECT EMPNUM, EMPNAME
  342. FROM STAFF
  343. WHERE EMPNUM IN
  344. (SELECT EMPNUM FROM WORKS
  345. WHERE PNUM IN
  346. (SELECT PNUM FROM PROJ));
  347. EMPNUM EMPNAME
  348. E1 Alice
  349. E2 Betty
  350. E3 Carmen
  351. E4 Don
  352. set optimizer_switch=@save_optimizer_switch;
  353. drop table STAFF,WORKS,PROJ;
  354. # End of bug#45191
  355. #
  356. # Bug#46550 Azalea returning duplicate results for some IN subqueries
  357. # w/ semijoin=on
  358. #
  359. DROP TABLE IF EXISTS t0, t1, t2;
  360. CREATE TABLE t0 (
  361. int_key int(11) DEFAULT NULL,
  362. varchar_key varchar(1) DEFAULT NULL,
  363. varchar_nokey varchar(1) DEFAULT NULL,
  364. KEY int_key (int_key),
  365. KEY varchar_key (varchar_key,int_key)
  366. );
  367. INSERT INTO t0 VALUES
  368. (1,'m','m'),
  369. (40,'h','h'),
  370. (1,'r','r'),
  371. (1,'h','h'),
  372. (9,'x','x'),
  373. (NULL,'q','q'),
  374. (NULL,'k','k'),
  375. (7,'l','l'),
  376. (182,'k','k'),
  377. (202,'a','a'),
  378. (7,'x','x'),
  379. (6,'j','j'),
  380. (119,'z','z'),
  381. (4,'d','d'),
  382. (5,'h','h'),
  383. (1,'u','u'),
  384. (3,'q','q'),
  385. (7,'a','a'),
  386. (3,'e','e'),
  387. (6,'l','l');
  388. CREATE TABLE t1 (
  389. int_key int(11) DEFAULT NULL,
  390. varchar_key varchar(1) DEFAULT NULL,
  391. varchar_nokey varchar(1) DEFAULT NULL,
  392. KEY int_key (int_key),
  393. KEY varchar_key (varchar_key,int_key)
  394. );
  395. INSERT INTO t1 VALUES (7,NULL,NULL),(4,'x','x');
  396. CREATE TABLE t2 (
  397. int_key int(11) DEFAULT NULL,
  398. varchar_key varchar(1) DEFAULT NULL,
  399. varchar_nokey varchar(1) DEFAULT NULL,
  400. KEY int_key (int_key),
  401. KEY varchar_key (varchar_key,int_key)
  402. );
  403. INSERT INTO t2 VALUES (123,NULL,NULL);
  404. SELECT int_key
  405. FROM t0
  406. WHERE varchar_nokey IN (
  407. SELECT t1 .varchar_key from t1
  408. );
  409. int_key
  410. 9
  411. 7
  412. DROP TABLE t0, t1, t2;
  413. # End of bug#46550
  414. #
  415. # Bug #46744 Crash in optimize_semijoin_nests on empty view
  416. # with limit and procedure.
  417. #
  418. DROP TABLE IF EXISTS t1, t2;
  419. DROP VIEW IF EXISTS v1;
  420. DROP PROCEDURE IF EXISTS p1;
  421. CREATE TABLE t1 ( f1 int );
  422. CREATE TABLE t2 ( f1 int );
  423. insert into t2 values (5), (7);
  424. CREATE OR REPLACE VIEW v1 AS SELECT * FROM t1 LIMIT 2;
  425. create procedure p1()
  426. select COUNT(*)
  427. FROM v1 WHERE f1 IN
  428. (SELECT f1 FROM t2 WHERE f1 = ANY (SELECT f1 FROM v1));
  429. SET SESSION optimizer_switch = 'semijoin=on';
  430. CALL p1();
  431. COUNT(*)
  432. 0
  433. SET SESSION optimizer_switch = 'semijoin=off';
  434. CALL p1();
  435. COUNT(*)
  436. 0
  437. drop table t1, t2;
  438. drop view v1;
  439. drop procedure p1;
  440. set SESSION optimizer_switch=@save_optimizer_switch;
  441. # End of bug#46744
  442. Bug#46797 "Crash in fix_semijoin_strategies_for_picked_join_order
  443. with semijoin=on"
  444. CREATE TABLE t1 (
  445. varchar_key varchar(1) DEFAULT NULL,
  446. KEY varchar_key (varchar_key)
  447. );
  448. CREATE TABLE t2 (
  449. varchar_key varchar(1) DEFAULT NULL,
  450. KEY varchar_key (varchar_key)
  451. );
  452. INSERT INTO t2 VALUES
  453. (NULL),(NULL),(NULL),(NULL),('a'),('a'),('a'),('b'),('b'),('b'),('b'),('c'),
  454. ('c'),('c'),('c'),('c'),('c'),('c'),('d'),('d'),('d'),('d'),('d'),('d'),('e'),
  455. ('e'),('e'),('e'),('e'),('e'),('f'),('f'),('f'),('g'),('g'),('h'),('h'),('h'),
  456. ('h'),('i'),('j'),('j'),('j'),('k'),('k'),('l'),('l'),('m'),('m'),('m'),('m'),
  457. ('n'),('n'),('n'),('o'),('o'),('o'),('p'),('p'),('p'),('q'),('q'),('q'),('r'),
  458. ('r'),('r'),('r'),('s'),('s'),('s'),('s'),('t'),('t'),('t'),('t'),('u'),('u'),
  459. ('u'),('u'),('v'),('v'),('v'),('v'),('w'),('w'),('w'),('w'),('w'),('w'),('x'),
  460. ('x'),('x'),('y'),('y'),('y'),('y'),('z'),('z'),('z'),('z');
  461. CREATE TABLE t3 (
  462. varchar_key varchar(1) DEFAULT NULL,
  463. KEY varchar_key (varchar_key)
  464. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  465. INSERT INTO t3 VALUES
  466. (NULL),('c'),('d'),('e'),('f'),('h'),('j'),('k'),('k'),('m'),('m'),('m'),
  467. ('n'),('o'),('r'),('t'),('t'),('u'),('w'),('y');
  468. SELECT varchar_key FROM t3
  469. WHERE (SELECT varchar_key FROM t3
  470. WHERE (varchar_key,varchar_key)
  471. IN (SELECT t1.varchar_key, t2 .varchar_key
  472. FROM t1 RIGHT JOIN t2 ON t1.varchar_key
  473. )
  474. );
  475. varchar_key
  476. DROP TABLE t1, t2, t3;
  477. #
  478. # Bug#46556 Returning incorrect, empty results for some IN subqueries
  479. # w/semijoin=on
  480. #
  481. CREATE TABLE t0 (
  482. pk INTEGER,
  483. vkey VARCHAR(1),
  484. vnokey VARCHAR(1),
  485. PRIMARY KEY (pk),
  486. KEY vkey(vkey)
  487. );
  488. INSERT INTO t0
  489. VALUES (1,'g','g'), (2,'v','v'), (3,'t','t'), (4,'u','u'), (5,'n','n');
  490. EXPLAIN EXTENDED SELECT vkey FROM t0 WHERE pk IN
  491. (SELECT t1.pk FROM t0 t1 JOIN t0 t2 ON t2.vkey = t1.vnokey);
  492. id select_type table type possible_keys key key_len ref rows filtered Extra
  493. 1 PRIMARY t0 ALL PRIMARY NULL NULL NULL 5 100.00
  494. 1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.t0.pk 1 100.00 Using where
  495. 1 PRIMARY t2 ref vkey vkey 4 test.t1.vnokey 2 100.00 Using index; FirstMatch(t1)
  496. Warnings:
  497. Note 1003 select `test`.`t0`.`vkey` AS `vkey` from `test`.`t0` `t1` semi join (`test`.`t0` `t2`) join `test`.`t0` where ((`test`.`t2`.`vkey` = `test`.`t1`.`vnokey`) and (`test`.`t1`.`pk` = `test`.`t0`.`pk`))
  498. SELECT vkey FROM t0 WHERE pk IN
  499. (SELECT t1.pk FROM t0 t1 JOIN t0 t2 ON t2.vkey = t1.vnokey);
  500. vkey
  501. g
  502. v
  503. t
  504. u
  505. n
  506. DROP TABLE t0;
  507. # End of bug#46556
  508. Bug #48073 Subquery on char columns from view crashes Mysql
  509. DROP TABLE IF EXISTS t1, t2;
  510. DROP VIEW IF EXISTS v1;
  511. CREATE TABLE t1 (
  512. city VARCHAR(50) NOT NULL,
  513. country_id SMALLINT UNSIGNED NOT NULL
  514. );
  515. INSERT INTO t1 VALUES
  516. ('Batna',2),
  517. ('Bchar',2),
  518. ('Skikda',2),
  519. ('Tafuna',3),
  520. ('Algeria',2) ;
  521. CREATE TABLE t2 (
  522. country_id SMALLINT UNSIGNED NOT NULL,
  523. country VARCHAR(50) NOT NULL
  524. );
  525. INSERT INTO t2 VALUES
  526. (2,'Algeria'),
  527. (3,'American Samoa') ;
  528. CREATE VIEW v1 AS
  529. SELECT country_id, country
  530. FROM t2
  531. WHERE LEFT(country,1) = "A"
  532. ;
  533. SELECT city, country_id
  534. FROM t1
  535. WHERE city IN (
  536. SELECT country
  537. FROM t2
  538. WHERE LEFT(country, 1) = "A"
  539. );
  540. city country_id
  541. Algeria 2
  542. SELECT city, country_id
  543. FROM t1
  544. WHERE city IN (
  545. SELECT country
  546. FROM v1
  547. );
  548. city country_id
  549. Algeria 2
  550. drop table t1, t2;
  551. drop view v1;
  552. # End of bug#48073
  553. Bug#48834: Procedure with view + subquery + semijoin=on
  554. crashes on second call.
  555. SET SESSION optimizer_switch ='semijoin=on';
  556. CREATE TABLE t1 ( t1field integer, primary key (t1field));
  557. CREATE TABLE t2 ( t2field integer, primary key (t2field));
  558. CREATE VIEW v1 AS
  559. SELECT t1field as v1field
  560. FROM t1 A
  561. WHERE A.t1field IN (SELECT t1field FROM t2 );
  562. CREATE VIEW v2 AS
  563. SELECT t2field as v2field
  564. FROM t2 A
  565. WHERE A.t2field IN (SELECT t2field FROM t2 );
  566. CREATE PROCEDURE p1 ()
  567. BEGIN
  568. SELECT v1field
  569. FROM v1
  570. WHERE v1field IN ( SELECT v2field as vf_inner FROM v2 );
  571. END|
  572. INSERT INTO t1 VALUES (1),(2),(3);
  573. INSERT INTO t2 VALUES (2),(3),(4);
  574. CALL p1;
  575. v1field
  576. 2
  577. 3
  578. CALL p1;
  579. v1field
  580. 2
  581. 3
  582. DROP TABLE t1,t2;
  583. DROP VIEW v1,v2;
  584. DROP PROCEDURE p1;
  585. set SESSION optimizer_switch=@save_optimizer_switch;
  586. # End of BUG#48834
  587. Bug#49097 subquery with view generates wrong result with
  588. non-prepared statement
  589. DROP TABLE IF EXISTS t1, t2;
  590. DROP VIEW IF EXISTS v1;
  591. CREATE TABLE t1 (
  592. city VARCHAR(50) NOT NULL,
  593. country_id SMALLINT UNSIGNED NOT NULL
  594. );
  595. INSERT INTO t1 VALUES
  596. ('Batna',2),
  597. ('Bchar',2),
  598. ('Skikda',2),
  599. ('Tafuna',3),
  600. ('Algeria',2) ;
  601. CREATE TABLE t2 (
  602. country_id SMALLINT UNSIGNED NOT NULL,
  603. country VARCHAR(50) NOT NULL
  604. );
  605. INSERT INTO t2 VALUES
  606. (2,'Algeria'),
  607. (3,'XAmerican Samoa') ;
  608. CREATE VIEW v1 AS
  609. SELECT country_id, country
  610. FROM t2
  611. WHERE LEFT(country,1) = "A"
  612. ;
  613. SELECT city, country_id
  614. FROM t1
  615. WHERE country_id IN (
  616. SELECT country_id
  617. FROM t2
  618. WHERE LEFT(country,1) = "A"
  619. );
  620. city country_id
  621. Batna 2
  622. Bchar 2
  623. Skikda 2
  624. Algeria 2
  625. SELECT city, country_id
  626. FROM t1
  627. WHERE country_id IN (
  628. SELECT country_id
  629. FROM v1
  630. );
  631. city country_id
  632. Batna 2
  633. Bchar 2
  634. Skikda 2
  635. Algeria 2
  636. PREPARE stmt FROM
  637. "
  638. SELECT city, country_id
  639. FROM t1
  640. WHERE country_id IN (
  641. SELECT country_id
  642. FROM v1
  643. );
  644. ";
  645. execute stmt;
  646. city country_id
  647. Batna 2
  648. Bchar 2
  649. Skikda 2
  650. Algeria 2
  651. deallocate prepare stmt;
  652. drop table t1, t2;
  653. drop view v1;
  654. # End of Bug#49097
  655. #
  656. # BUG#38075: Wrong result: rows matching a subquery with outer join not returned
  657. #
  658. DROP TABLE IF EXISTS ot1, it1, it2;
  659. CREATE TABLE it2 (
  660. int_key int(11) NOT NULL,
  661. datetime_key datetime NOT NULL,
  662. KEY int_key (int_key),
  663. KEY datetime_key (datetime_key)
  664. );
  665. INSERT INTO it2 VALUES
  666. (5,'2002-04-10 14:25:30'), (0,'0000-00-00 00:00:00'),
  667. (0,'2006-09-14 04:01:02'), (4,'0000-00-00 00:00:00'),
  668. (8,'0000-00-00 00:00:00'), (5,'0000-00-00 00:00:00'),
  669. (9,'0000-00-00 00:00:00'), (8,'2007-04-01 11:04:17'),
  670. (1,'0000-00-00 00:00:00'), (7,'2009-01-12 00:00:00'),
  671. (0,'2009-06-05 00:00:00'), (3,'2006-02-14 18:06:35'),
  672. (5,'2006-02-21 07:08:16'), (0,'0000-00-00 00:00:00'),
  673. (7,'0000-00-00 00:00:00'), (0,'0000-00-00 00:00:00'),
  674. (0,'2007-02-13 00:00:00'), (1,'0000-00-00 00:00:00'),
  675. (0,'0000-00-00 00:00:00'), (1,'2003-08-11 00:00:00');
  676. CREATE TABLE ot1 (
  677. int_nokey int(11) NOT NULL,
  678. int_key int(11) NOT NULL,
  679. KEY int_key (int_key)
  680. );
  681. INSERT INTO ot1 VALUES
  682. (5,0), (3,0), (0,2), (3,0), (1,3), (0,0), (1,7), (7,0), (1,7), (0,7),
  683. (0,9), (8,2), (4,4), (9,3), (0,9), (2,5), (0,5), (8,0), (5,8), (1,5);
  684. CREATE TABLE it1 (
  685. int_nokey int(11) NOT NULL,
  686. int_key int(11) NOT NULL,
  687. KEY int_key (int_key)
  688. );
  689. INSERT INTO it1 VALUES
  690. (9,5), (0,4);
  691. SELECT int_key FROM ot1
  692. WHERE int_nokey IN (SELECT it2.int_key
  693. FROM it1 LEFT JOIN it2 ON it2.datetime_key);
  694. int_key
  695. 0
  696. 0
  697. 0
  698. 0
  699. 0
  700. 0
  701. 2
  702. 2
  703. 3
  704. 5
  705. 5
  706. 7
  707. 7
  708. 7
  709. 8
  710. 9
  711. 9
  712. EXPLAIN
  713. SELECT int_key FROM ot1
  714. WHERE int_nokey IN (SELECT it2.int_key
  715. FROM it1 LEFT JOIN it2 ON it2.datetime_key);
  716. id select_type table type possible_keys key key_len ref rows Extra
  717. 1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 11
  718. 1 PRIMARY ot1 ALL NULL NULL NULL NULL 20 Using where; Using join buffer (flat, BNL join)
  719. 2 MATERIALIZED it1 index NULL int_key 4 NULL 2 Using index
  720. 2 MATERIALIZED it2 ALL int_key,datetime_key NULL NULL NULL 20 Using where
  721. DROP TABLE ot1, it1, it2;
  722. # End of BUG#38075
  723. #
  724. # BUG#31480: Incorrect result for nested subquery when executed via semi join
  725. #
  726. create table t1 (a int not null, b int not null);
  727. create table t2 (c int not null, d int not null);
  728. create table t3 (e int not null);
  729. insert into t1 values (1,10);
  730. insert into t1 values (2,10);
  731. insert into t1 values (1,20);
  732. insert into t1 values (2,20);
  733. insert into t1 values (3,20);
  734. insert into t1 values (2,30);
  735. insert into t1 values (4,40);
  736. insert into t2 values (2,10);
  737. insert into t2 values (2,20);
  738. insert into t2 values (4,10);
  739. insert into t2 values (5,10);
  740. insert into t2 values (3,20);
  741. insert into t2 values (2,40);
  742. insert into t3 values (10);
  743. insert into t3 values (30);
  744. insert into t3 values (10);
  745. insert into t3 values (20);
  746. explain extended
  747. select a from t1
  748. where a in (select c from t2 where d >= some(select e from t3 where b=e));
  749. id select_type table type possible_keys key key_len ref rows filtered Extra
  750. 1 PRIMARY t2 ALL NULL NULL NULL NULL 6 100.00 Start temporary
  751. 1 PRIMARY t1 ALL NULL NULL NULL NULL 7 100.00 Using where; End temporary; Using join buffer (flat, BNL join)
  752. 3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 4 100.00 Using where
  753. Warnings:
  754. Note 1276 Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1
  755. Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t1`.`a` = `test`.`t2`.`c`) and <nop>(<expr_cache><`test`.`t2`.`d`,`test`.`t1`.`b`>(<in_optimizer>(`test`.`t2`.`d`,<exists>(select `test`.`t3`.`e` from `test`.`t3` where ((`test`.`t1`.`b` = `test`.`t3`.`e`) and (<cache>(`test`.`t2`.`d`) >= `test`.`t3`.`e`)))))))
  756. show warnings;
  757. Level Code Message
  758. Note 1276 Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1
  759. Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t1`.`a` = `test`.`t2`.`c`) and <nop>(<expr_cache><`test`.`t2`.`d`,`test`.`t1`.`b`>(<in_optimizer>(`test`.`t2`.`d`,<exists>(select `test`.`t3`.`e` from `test`.`t3` where ((`test`.`t1`.`b` = `test`.`t3`.`e`) and (<cache>(`test`.`t2`.`d`) >= `test`.`t3`.`e`)))))))
  760. select a from t1
  761. where a in (select c from t2 where d >= some(select e from t3 where b=e));
  762. a
  763. 2
  764. 2
  765. 3
  766. 2
  767. drop table t1, t2, t3;
  768. #
  769. # Bug#48213 Materialized subselect crashes if using GEOMETRY type
  770. #
  771. CREATE TABLE t1 (
  772. pk int,
  773. a varchar(1),
  774. b varchar(4),
  775. c tinyblob,
  776. d blob,
  777. e mediumblob,
  778. f longblob,
  779. g tinytext,
  780. h text,
  781. i mediumtext,
  782. j longtext,
  783. k geometry,
  784. PRIMARY KEY (pk)
  785. );
  786. INSERT INTO t1 VALUES (1,'o','ffff','ffff','ffoo','ffff','ffff','ffff','ffff','ffff','ffff',GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))')), (2,'f','ffff','ffff','ffff', 'ffff','ffff','ffff','ffff','ffff','ffff',GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))'));
  787. CREATE TABLE t2 LIKE t1;
  788. INSERT INTO t2 VALUES (1,'i','iiii','iiii','iiii','iiii','ffff','ffff','ffff','ffff','ffff',GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))')), (2,'f','ffff','ffff','ffff','ffff','ffff','ffff','ffff','ffff','ffff',GeomFromText('POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))'));
  789. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (a, b) IN (SELECT a, b FROM t2 WHERE pk > 0);
  790. id select_type table type possible_keys key key_len ref rows filtered Extra
  791. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  792. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 13 func,func 1 100.00
  793. 2 MATERIALIZED t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Rowid-ordered scan
  794. Warnings:
  795. Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`pk` > 0))
  796. SELECT pk FROM t1 WHERE (a, b) IN (SELECT a, b FROM t2 WHERE pk > 0);
  797. pk
  798. 2
  799. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, c) IN (SELECT b, c FROM t2 WHERE pk > 0);
  800. id select_type table type possible_keys key key_len ref rows filtered Extra
  801. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  802. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  803. Warnings:
  804. Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`c` = `test`.`t1`.`c`) and (`test`.`t2`.`b` = `test`.`t1`.`b`) and (`test`.`t2`.`pk` > 0))
  805. SELECT pk FROM t1 WHERE (b, c) IN (SELECT b, c FROM t2 WHERE pk > 0);
  806. pk
  807. 1
  808. 2
  809. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, d) IN (SELECT b, d FROM t2 WHERE pk > 0);
  810. id select_type table type possible_keys key key_len ref rows filtered Extra
  811. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  812. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  813. Warnings:
  814. Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`d` = `test`.`t1`.`d`) and (`test`.`t2`.`b` = `test`.`t1`.`b`) and (`test`.`t2`.`pk` > 0))
  815. SELECT pk FROM t1 WHERE (b, d) IN (SELECT b, d FROM t2 WHERE pk > 0);
  816. pk
  817. 2
  818. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, e) IN (SELECT b, e FROM t2 WHERE pk > 0);
  819. id select_type table type possible_keys key key_len ref rows filtered Extra
  820. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  821. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  822. Warnings:
  823. Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`e` = `test`.`t1`.`e`) and (`test`.`t2`.`b` = `test`.`t1`.`b`) and (`test`.`t2`.`pk` > 0))
  824. SELECT pk FROM t1 WHERE (b, e) IN (SELECT b, e FROM t2 WHERE pk > 0);
  825. pk
  826. 1
  827. 2
  828. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, f) IN (SELECT b, f FROM t2 WHERE pk > 0);
  829. id select_type table type possible_keys key key_len ref rows filtered Extra
  830. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  831. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  832. Warnings:
  833. Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`f` = `test`.`t1`.`f`) and (`test`.`t2`.`b` = `test`.`t1`.`b`) and (`test`.`t2`.`pk` > 0))
  834. SELECT pk FROM t1 WHERE (b, f) IN (SELECT b, f FROM t2 WHERE pk > 0);
  835. pk
  836. 1
  837. 2
  838. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, g) IN (SELECT b, g FROM t2 WHERE pk > 0);
  839. id select_type table type possible_keys key key_len ref rows filtered Extra
  840. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  841. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  842. Warnings:
  843. Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`g` = `test`.`t1`.`g`) and (`test`.`t2`.`b` = `test`.`t1`.`b`) and (`test`.`t2`.`pk` > 0))
  844. SELECT pk FROM t1 WHERE (b, g) IN (SELECT b, g FROM t2 WHERE pk > 0);
  845. pk
  846. 1
  847. 2
  848. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, h) IN (SELECT b, h FROM t2 WHERE pk > 0);
  849. id select_type table type possible_keys key key_len ref rows filtered Extra
  850. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  851. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  852. Warnings:
  853. Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`h` = `test`.`t1`.`h`) and (`test`.`t2`.`b` = `test`.`t1`.`b`) and (`test`.`t2`.`pk` > 0))
  854. SELECT pk FROM t1 WHERE (b, h) IN (SELECT b, h FROM t2 WHERE pk > 0);
  855. pk
  856. 1
  857. 2
  858. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, i) IN (SELECT b, i FROM t2 WHERE pk > 0);
  859. id select_type table type possible_keys key key_len ref rows filtered Extra
  860. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  861. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  862. Warnings:
  863. Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`i` = `test`.`t1`.`i`) and (`test`.`t2`.`b` = `test`.`t1`.`b`) and (`test`.`t2`.`pk` > 0))
  864. SELECT pk FROM t1 WHERE (b, i) IN (SELECT b, i FROM t2 WHERE pk > 0);
  865. pk
  866. 1
  867. 2
  868. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, j) IN (SELECT b, j FROM t2 WHERE pk > 0);
  869. id select_type table type possible_keys key key_len ref rows filtered Extra
  870. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  871. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  872. Warnings:
  873. Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`j` = `test`.`t1`.`j`) and (`test`.`t2`.`b` = `test`.`t1`.`b`) and (`test`.`t2`.`pk` > 0))
  874. SELECT pk FROM t1 WHERE (b, j) IN (SELECT b, j FROM t2 WHERE pk > 0);
  875. pk
  876. 1
  877. 2
  878. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, k) IN (SELECT b, k FROM t2 WHERE pk > 0);
  879. id select_type table type possible_keys key key_len ref rows filtered Extra
  880. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  881. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  882. Warnings:
  883. Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`k` = `test`.`t1`.`k`) and (`test`.`t2`.`b` = `test`.`t1`.`b`) and (`test`.`t2`.`pk` > 0))
  884. SELECT pk FROM t1 WHERE (b, k) IN (SELECT b, k FROM t2 WHERE pk > 0);
  885. pk
  886. 1
  887. 2
  888. DROP TABLE t1, t2;
  889. # End of Bug#48213
  890. #
  891. # Bug#49198 Wrong result for second call of procedure
  892. # with view in subselect.
  893. #
  894. CREATE TABLE t1 (t1field integer, primary key (t1field));
  895. CREATE TABLE t2 (t2field integer, primary key (t2field));
  896. CREATE TABLE t3 (t3field integer, primary key (t3field));
  897. CREATE VIEW v2 AS SELECT * FROM t2;
  898. CREATE VIEW v3 AS SELECT * FROM t3;
  899. INSERT INTO t1 VALUES(1),(2);
  900. INSERT INTO t2 VALUES(1),(2);
  901. INSERT INTO t3 VALUES(1),(2);
  902. PREPARE stmt FROM
  903. "
  904. SELECT t1field
  905. FROM t1
  906. WHERE t1field IN (SELECT * FROM v2);
  907. ";
  908. EXECUTE stmt;
  909. t1field
  910. 1
  911. 2
  912. EXECUTE stmt;
  913. t1field
  914. 1
  915. 2
  916. PREPARE stmt FROM
  917. "
  918. EXPLAIN
  919. SELECT t1field
  920. FROM t1
  921. WHERE t1field IN (SELECT * FROM v2)
  922. AND t1field IN (SELECT * FROM v3)
  923. ";
  924. EXECUTE stmt;
  925. id select_type table type possible_keys key key_len ref rows Extra
  926. 1 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 2 Using index
  927. 1 PRIMARY t2 eq_ref PRIMARY PRIMARY 4 test.t1.t1field 1 Using index
  928. 1 PRIMARY t3 eq_ref PRIMARY PRIMARY 4 test.t1.t1field 1 Using index
  929. EXECUTE stmt;
  930. id select_type table type possible_keys key key_len ref rows Extra
  931. 1 SIMPLE t1 index PRIMARY PRIMARY 4 NULL 2 Using index
  932. 1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.t1field 1 Using index
  933. 1 SIMPLE t3 eq_ref PRIMARY PRIMARY 4 test.t1.t1field 1 Using index
  934. DROP TABLE t1, t2, t3;
  935. DROP VIEW v2, v3;
  936. # End of Bug#49198
  937. #
  938. # Bug#45174: Incorrectly applied equality propagation caused wrong
  939. # result on a query with a materialized semi-join.
  940. #
  941. CREATE TABLE `t1` (
  942. `pk` int(11) NOT NULL AUTO_INCREMENT,
  943. `varchar_key` varchar(1) NOT NULL,
  944. `varchar_nokey` varchar(1) NOT NULL,
  945. PRIMARY KEY (`pk`),
  946. KEY `varchar_key` (`varchar_key`)
  947. );
  948. INSERT INTO `t1` VALUES (11,'m','m'),(12,'j','j'),(13,'z','z'),(14,'a','a'),(15,'',''),(16,'e','e'),(17,'t','t'),(19,'b','b'),(20,'w','w'),(21,'m','m'),(23,'',''),(24,'w','w'),(26,'e','e'),(27,'e','e'),(28,'p','p');
  949. CREATE TABLE `t2` (
  950. `varchar_nokey` varchar(1) NOT NULL
  951. );
  952. INSERT INTO `t2` VALUES ('v'),('u'),('n'),('l'),('h'),('u'),('n'),('j'),('k'),('e'),('i'),('u'),('n'),('b'),('x'),(''),('q'),('u');
  953. EXPLAIN EXTENDED SELECT varchar_nokey
  954. FROM t2
  955. WHERE ( `varchar_nokey` , `varchar_nokey` ) IN (
  956. SELECT `varchar_key` , `varchar_nokey`
  957. FROM t1
  958. WHERE `varchar_nokey` < 'n' XOR `pk` ) ;
  959. id select_type table type possible_keys key key_len ref rows filtered Extra
  960. 1 PRIMARY t2 ALL NULL NULL NULL NULL 18 100.00
  961. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 8 func,func 1 100.00
  962. 2 MATERIALIZED t1 ALL varchar_key NULL NULL NULL 15 100.00 Using where
  963. Warnings:
  964. Note 1003 select `test`.`t2`.`varchar_nokey` AS `varchar_nokey` from `test`.`t2` semi join (`test`.`t1`) where ((`test`.`t1`.`varchar_nokey` = `test`.`t1`.`varchar_key`) and ((`test`.`t1`.`varchar_key` < 'n') xor `test`.`t1`.`pk`))
  965. SELECT varchar_nokey
  966. FROM t2
  967. WHERE ( `varchar_nokey` , `varchar_nokey` ) IN (
  968. SELECT `varchar_key` , `varchar_nokey`
  969. FROM t1
  970. WHERE `varchar_nokey` < 'n' XOR `pk` ) ;
  971. varchar_nokey
  972. DROP TABLE t1, t2;
  973. # End of the test for bug#45174.
  974. #
  975. # BUG#43768: Prepared query with nested subqueries core dumps on second execution
  976. #
  977. create table t1 (
  978. id int(11) unsigned not null primary key auto_increment,
  979. partner_id varchar(35) not null,
  980. t1_status_id int(10) unsigned
  981. );
  982. insert into t1 values ("1", "partner1", "10"), ("2", "partner2", "10"),
  983. ("3", "partner3", "10"), ("4", "partner4", "10");
  984. create table t2 (
  985. id int(11) unsigned not null default '0',
  986. t1_line_id int(11) unsigned not null default '0',
  987. article_id varchar(20),
  988. sequence int(11) not null default '0',
  989. primary key (id,t1_line_id)
  990. );
  991. insert into t2 values ("1", "1", "sup", "0"), ("2", "1", "sup", "1"),
  992. ("2", "2", "sup", "2"), ("2", "3", "sup", "3"),
  993. ("2", "4", "imp", "4"), ("3", "1", "sup", "0"),
  994. ("4", "1", "sup", "0");
  995. create table t3 (
  996. id int(11) not null default '0',
  997. preceeding_id int(11) not null default '0',
  998. primary key (id,preceeding_id)
  999. );
  1000. create table t4 (
  1001. user_id varchar(50) not null,
  1002. article_id varchar(20) not null,
  1003. primary key (user_id,article_id)
  1004. );
  1005. insert into t4 values("nicke", "imp");
  1006. prepare stmt from
  1007. 'select t1.partner_id
  1008. from t1
  1009. where
  1010. t1.id in (
  1011. select pl_inner.id
  1012. from t2 as pl_inner
  1013. where pl_inner.article_id in (
  1014. select t4.article_id from t4
  1015. where t4.user_id = \'nicke\'
  1016. )
  1017. )';
  1018. execute stmt;
  1019. partner_id
  1020. partner2
  1021. execute stmt;
  1022. partner_id
  1023. partner2
  1024. drop table t1,t2,t3,t4;
  1025. #
  1026. # Bug#48623 Multiple subqueries are optimized incorrectly
  1027. #
  1028. CREATE TABLE t1(val VARCHAR(10));
  1029. CREATE TABLE t2(val VARCHAR(10));
  1030. CREATE TABLE t3(val VARCHAR(10));
  1031. INSERT INTO t1 VALUES('aaa'), ('bbb'), ('eee'), ('mmm'), ('ppp');
  1032. INSERT INTO t2 VALUES('aaa'), ('aaa'), ('bbb'), ('eee'), ('mmm'), ('ppp');
  1033. INSERT INTO t3 VALUES('aaa'), ('bbb'), ('eee'), ('mmm'), ('ppp');
  1034. EXPLAIN
  1035. SELECT *
  1036. FROM t1
  1037. WHERE t1.val IN (SELECT t2.val FROM t2
  1038. WHERE t2.val LIKE 'a%' OR t2.val LIKE 'e%')
  1039. AND t1.val IN (SELECT t3.val FROM t3
  1040. WHERE t3.val LIKE 'a%' OR t3.val LIKE 'e%');
  1041. id select_type table type possible_keys key key_len ref rows Extra
  1042. 1 PRIMARY t1 ALL NULL NULL NULL NULL 5
  1043. 1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 14 func 1
  1044. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 14 func 1
  1045. 3 MATERIALIZED t3 ALL NULL NULL NULL NULL 5 Using where
  1046. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 6 Using where
  1047. SELECT *
  1048. FROM t1
  1049. WHERE t1.val IN (SELECT t2.val FROM t2
  1050. WHERE t2.val LIKE 'a%' OR t2.val LIKE 'e%')
  1051. AND t1.val IN (SELECT t3.val FROM t3
  1052. WHERE t3.val LIKE 'a%' OR t3.val LIKE 'e%');
  1053. val
  1054. aaa
  1055. eee
  1056. DROP TABLE t1;
  1057. DROP TABLE t2;
  1058. DROP TABLE t3;
  1059. # End of Bug#48623
  1060. #
  1061. # LPBUG#602574: RQG: sql_select.cc:5385: bool greedy_search(JOIN*, table_map, uint,
  1062. # uint): Assertion `join->best_read <
  1063. #
  1064. set @save_optimizer_switch=@@optimizer_switch;
  1065. set optimizer_switch='materialization=off';
  1066. CREATE TABLE t1 (
  1067. varchar_key varchar(1) DEFAULT NULL,
  1068. KEY varchar_key (varchar_key)
  1069. );
  1070. CREATE TABLE t2 (
  1071. varchar_key varchar(1) DEFAULT NULL,
  1072. KEY varchar_key (varchar_key)
  1073. );
  1074. INSERT INTO t2 VALUES
  1075. (NULL),(NULL),(NULL),(NULL),('a'),('a'),('a'),('b'),('b'),('b'),('b'),('c'),
  1076. ('c'),('c'),('c'),('c'),('c'),('c'),('d'),('d'),('d'),('d'),('d'),('d'),('e'),
  1077. ('e'),('e'),('e'),('e'),('e'),('f'),('f'),('f'),('g'),('g'),('h'),('h'),('h'),
  1078. ('h'),('i'),('j'),('j'),('j'),('k'),('k'),('l'),('l'),('m'),('m'),('m'),('m'),
  1079. ('n'),('n'),('n'),('o'),('o'),('o'),('p'),('p'),('p'),('q'),('q'),('q'),('r'),
  1080. ('r'),('r'),('r'),('s'),('s'),('s'),('s'),('t'),('t'),('t'),('t'),('u'),('u'),
  1081. ('u'),('u'),('v'),('v'),('v'),('v'),('w'),('w'),('w'),('w'),('w'),('w'),('x'),
  1082. ('x'),('x'),('y'),('y'),('y'),('y'),('z'),('z'),('z'),('z');
  1083. CREATE TABLE t3 (
  1084. varchar_key varchar(1) DEFAULT NULL,
  1085. KEY varchar_key (varchar_key)
  1086. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  1087. INSERT INTO t3 VALUES
  1088. (NULL),('c'),('d'),('e'),('f'),('h'),('j'),('k'),('k'),('m'),('m'),('m'),
  1089. ('n'),('o'),('r'),('t'),('t'),('u'),('w'),('y');
  1090. SELECT varchar_key FROM t3
  1091. WHERE (SELECT varchar_key FROM t3
  1092. WHERE (varchar_key,varchar_key)
  1093. IN (SELECT t1.varchar_key, t2 .varchar_key
  1094. FROM t1 RIGHT JOIN t2 ON t1.varchar_key
  1095. )
  1096. );
  1097. varchar_key
  1098. set optimizer_switch=@save_optimizer_switch;
  1099. DROP TABLE t1, t2, t3;
  1100. #
  1101. # Bug#46692 "Crash occurring on queries with nested FROM subqueries
  1102. # using materialization."
  1103. #
  1104. CREATE TABLE t1 (
  1105. pk INTEGER PRIMARY KEY,
  1106. int_key INTEGER,
  1107. KEY int_key(int_key)
  1108. );
  1109. INSERT INTO t1 VALUES (10,186),(11,NULL),(12,2),(13,3),(14,0),(15,133),(16,1);
  1110. CREATE TABLE t2 (
  1111. pk INTEGER PRIMARY KEY,
  1112. int_key INTEGER,
  1113. KEY int_key(int_key)
  1114. );
  1115. INSERT INTO t2 VALUES (1,7),(2,2);
  1116. SELECT * FROM t1 WHERE (140, 4) IN
  1117. (SELECT t2.int_key, t2 .pk FROM t2 STRAIGHT_JOIN t1 ON t2.int_key);
  1118. pk int_key
  1119. DROP TABLE t1, t2;
  1120. #
  1121. # Bug#42353 "SELECT ... WHERE oe IN (SELECT w/ LEFT JOIN) query
  1122. # causes crash."
  1123. #
  1124. CREATE TABLE t1 (
  1125. pk INTEGER PRIMARY KEY,
  1126. int_nokey INTEGER,
  1127. int_key INTEGER,
  1128. date_key DATE,
  1129. datetime_nokey DATETIME,
  1130. varchar_nokey VARCHAR(1)
  1131. );
  1132. CREATE TABLE t2 (
  1133. date_nokey DATE
  1134. );
  1135. CREATE TABLE t3 (
  1136. pk INTEGER PRIMARY KEY,
  1137. int_nokey INTEGER,
  1138. date_key date,
  1139. varchar_key VARCHAR(1),
  1140. varchar_nokey VARCHAR(1),
  1141. KEY date_key (date_key)
  1142. );
  1143. SELECT date_key FROM t1
  1144. WHERE (int_key, int_nokey)
  1145. IN (SELECT t3.int_nokey, t3.pk
  1146. FROM t2 LEFT JOIN t3 ON (t2.date_nokey < t3.date_key)
  1147. WHERE t3.varchar_key <= t3.varchar_nokey OR t3.int_nokey <= t3.pk
  1148. )
  1149. AND (varchar_nokey <> 'f' OR NOT int_key < 7);
  1150. date_key
  1151. #
  1152. # Bug#45933 "Crash in optimize_semijoin_nests on JOIN in subquery
  1153. # + AND in outer query".
  1154. #
  1155. INSERT INTO t1 VALUES (10,7,5,'2009-06-16','2002-04-10 14:25:30','w'),
  1156. (11,7,0,'0000-00-00','0000-00-00 00:00:00','s'),
  1157. (12,4,0,'2003-07-14','2006-09-14 04:01:02','y'),
  1158. (13,0,4,'2002-07-25','0000-00-00 00:00:00','c'),
  1159. (14,1,8,'2007-07-03','0000-00-00 00:00:00','q'),
  1160. (15,6,5,'2001-11-12','0000-00-00 00:00:00',''),
  1161. (16,2,9,'0000-00-00','0000-00-00 00:00:00','j'),
  1162. (29,9,1,'0000-00-00','2003-08-11 00:00:00','m');
  1163. INSERT INTO t3 VALUES (1,9,'0000-00-00','b','b'),
  1164. (2,2,'2002-09-17','h','h');
  1165. SELECT t1.varchar_nokey FROM t1 JOIN t3 ON t1.datetime_nokey
  1166. WHERE t1.varchar_nokey
  1167. IN (SELECT varchar_nokey FROM t1
  1168. WHERE (pk)
  1169. IN (SELECT t3.int_nokey
  1170. FROM t3 LEFT JOIN t1 ON t1.varchar_nokey
  1171. WHERE t3.date_key BETWEEN '2008-06-07' AND '2006-06-26'
  1172. )
  1173. );
  1174. varchar_nokey
  1175. DROP TABLE t1, t2, t3;
  1176. #
  1177. # Bug#45219 "Crash on SELECT DISTINCT query containing a
  1178. # LEFT JOIN in subquery"
  1179. #
  1180. CREATE TABLE t1 (
  1181. pk INTEGER NOT NULL,
  1182. int_nokey INTEGER NOT NULL,
  1183. datetime_key DATETIME NOT NULL,
  1184. varchar_key VARCHAR(1) NOT NULL,
  1185. PRIMARY KEY (pk),
  1186. KEY datetime_key (datetime_key),
  1187. KEY varchar_key (varchar_key)
  1188. );
  1189. INSERT INTO t1 VALUES
  1190. (1,9,'0000-00-00 00:00:00','p'),(2,0,'2002-02-09 07:38:13','v'),
  1191. (3,8,'2001-05-03 12:08:14','t'),(4,3,'0000-00-00 00:00:00','u'),
  1192. (5,7,'2009-07-28 03:43:30','n'),(6,0,'2009-08-04 00:00:00','l'),
  1193. (7,1,'0000-00-00 00:00:00','h'),(8,9,'0000-00-00 00:00:00','u'),
  1194. (9,0,'2005-08-02 17:16:54','n'),(10,9,'2002-12-21 00:00:00','j'),
  1195. (11,0,'2005-08-15 12:37:35','k'),(12,5,'0000-00-00 00:00:00','e'),
  1196. (13,0,'2006-03-10 00:00:00','i'),(14,8,'2005-05-16 11:02:36','u'),
  1197. (15,8,'2008-11-02 00:00:00','n'),(16,5,'2006-03-15 00:00:00','b'),
  1198. (17,1,'0000-00-00 00:00:00','x'),(18,7,'0000-00-00 00:00:00',''),
  1199. (19,0,'2008-12-17 20:15:40','q'),(20,9,'0000-00-00 00:00:00','u');
  1200. CREATE TABLE t2 LIKE t1;
  1201. INSERT INTO t2 VALUES
  1202. (10,0,'2006-07-07 07:26:28','q'),(11,5,'2002-09-23 00:00:00','m'),
  1203. (12,7,'0000-00-00 00:00:00','j'),(13,1,'2006-06-07 00:00:00','z'),
  1204. (14,8,'2000-09-16 12:15:34','a'),(15,2,'2007-08-05 15:47:52',''),
  1205. (16,1,'0000-00-00 00:00:00','e'),(17,8,'2005-12-02 19:34:26','t'),
  1206. (18,5,'0000-00-00 00:00:00','q'),(19,4,'0000-00-00 00:00:00','b'),
  1207. (20,5,'2007-12-28 00:00:00','w'),(21,3,'2004-08-02 11:48:43','m'),
  1208. (22,0,'0000-00-00 00:00:00','x'),(23,8,'2004-04-19 12:18:43',''),
  1209. (24,0,'2009-04-27 00:00:00','w'),(25,4,'2006-10-20 14:52:15','x'),
  1210. (26,0,'0000-00-00 00:00:00','e'),(27,0,'2002-03-22 11:48:37','e'),
  1211. (28,2,'0000-00-00 00:00:00','p'),(29,0,'2001-01-04 03:55:07','x');
  1212. CREATE TABLE t3 LIKE t1;
  1213. INSERT INTO t3 VALUES
  1214. (10,8,'2007-08-19 08:08:38','i'),(11,0,'2000-05-21 03:51:51','');
  1215. SELECT DISTINCT datetime_key FROM t1
  1216. WHERE (int_nokey, pk)
  1217. IN (SELECT t3.pk, t3.pk FROM t2 LEFT JOIN t3 ON t3.varchar_key)
  1218. AND pk = 9;
  1219. datetime_key
  1220. DROP TABLE t1, t2, t3;
  1221. #
  1222. # BUG#784723: Wrong result with semijoin + nested subqueries in maria-5.3
  1223. #
  1224. CREATE TABLE t1 ( t1field integer, primary key (t1field));
  1225. CREATE TABLE t2 ( t2field integer, primary key (t2field));
  1226. INSERT INTO t1 VALUES (1),(2),(3);
  1227. INSERT INTO t2 VALUES (2),(3),(4);
  1228. explain
  1229. SELECT * FROM t1 A
  1230. WHERE
  1231. A.t1field IN (SELECT A.t1field FROM t2 B) AND
  1232. A.t1field IN (SELECT C.t2field FROM t2 C
  1233. WHERE C.t2field IN (SELECT D.t2field FROM t2 D));
  1234. id select_type table type possible_keys key key_len ref rows Extra
  1235. 1 PRIMARY A index PRIMARY PRIMARY 4 NULL 3 Using index
  1236. 1 PRIMARY B index NULL PRIMARY 4 NULL 3 Using index; Start temporary; End temporary
  1237. 1 PRIMARY C eq_ref PRIMARY PRIMARY 4 test.A.t1field 1 Using index
  1238. 1 PRIMARY D eq_ref PRIMARY PRIMARY 4 test.A.t1field 1 Using index
  1239. SELECT * FROM t1 A
  1240. WHERE
  1241. A.t1field IN (SELECT A.t1field FROM t2 B) AND
  1242. A.t1field IN (SELECT C.t2field FROM t2 C
  1243. WHERE C.t2field IN (SELECT D.t2field FROM t2 D));
  1244. t1field
  1245. 2
  1246. 3
  1247. drop table t1,t2;
  1248. #
  1249. # BUG#787299: Valgrind complains on a join query with two IN subqueries
  1250. #
  1251. create table t1 (a int);
  1252. insert into t1 values (1), (2), (3);
  1253. create table t2 as select * from t1;
  1254. select * from t1 A, t1 B
  1255. where A.a = B.a and A.a in (select a from t2 C) and B.a in (select a from t2 D);
  1256. a a
  1257. 1 1
  1258. 2 2
  1259. 3 3
  1260. explain
  1261. select * from t1 A, t1 B
  1262. where A.a = B.a and A.a in (select a from t2 C) and B.a in (select a from t2 D);
  1263. id select_type table type possible_keys key key_len ref rows Extra
  1264. 1 PRIMARY A ALL NULL NULL NULL NULL 3
  1265. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 5 func 1
  1266. 1 PRIMARY B ALL NULL NULL NULL NULL 3 Using where; Using join buffer (flat, BNL join)
  1267. 1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 5 func 1
  1268. 2 MATERIALIZED C ALL NULL NULL NULL NULL 3
  1269. 3 MATERIALIZED D ALL NULL NULL NULL NULL 3
  1270. drop table t1, t2;
  1271. #
  1272. # BUG#784441: Abort on semijoin with a view as the inner table
  1273. #
  1274. CREATE TABLE t1 (a int) ;
  1275. INSERT INTO t1 VALUES (1), (1);
  1276. CREATE TABLE t2 (a int) ;
  1277. INSERT INTO t2 VALUES (1), (1);
  1278. CREATE VIEW v1 AS SELECT 1;
  1279. EXPLAIN
  1280. SELECT * FROM t1 INNER JOIN t2 ON t2.a != 0 AND t2.a IN (SELECT * FROM v1);
  1281. id select_type table type possible_keys key key_len ref rows Extra
  1282. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2
  1283. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
  1284. 2 MATERIALIZED <derived3> system NULL NULL NULL NULL 1
  1285. 3 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
  1286. SELECT * FROM t1 INNER JOIN t2 ON t2.a != 0 AND t2.a IN (SELECT * FROM v1);
  1287. a a
  1288. 1 1
  1289. 1 1
  1290. 1 1
  1291. 1 1
  1292. DROP VIEW v1;
  1293. DROP TABLE t1,t2;
  1294. #
  1295. # BUG#751439 Assertion `!table->file || table->file->inited == handler::NONE' failed with subquery
  1296. #
  1297. CREATE TABLE t1 ( f10 int, f11 int) ;
  1298. INSERT IGNORE INTO t1 VALUES (0,0),(0,0);
  1299. CREATE TABLE t2 ( f11 int);
  1300. INSERT IGNORE INTO t2 VALUES (0),(0);
  1301. CREATE TABLE t3 ( f11 int) ;
  1302. INSERT IGNORE INTO t3 VALUES (0);
  1303. SELECT alias1.f11 AS field2
  1304. FROM ( t3 AS alias2 JOIN t1 AS alias3 ON alias3.f10 = 1)
  1305. LEFT JOIN ( t2 AS alias1 ) ON alias3.f11 = 1
  1306. WHERE alias2.f11 IN ( SELECT f11 FROM t2 )
  1307. GROUP BY field2 ;
  1308. field2
  1309. drop table t1, t2, t3;
  1310. #
  1311. # BUG#778406 Crash in hp_movelink with Aria engine and subqueries
  1312. #
  1313. CREATE TABLE t4 (f10 varchar(32) , KEY (f10)) ENGINE=Aria;
  1314. INSERT INTO t4 VALUES ('x'),('m'),('c');
  1315. CREATE TABLE t1 (f11 int) ENGINE=Aria;
  1316. INSERT INTO t1 VALUES (0),(0),(0);
  1317. CREATE TABLE t2 ( f10 int) ENGINE=Aria;
  1318. INSERT INTO t2 VALUES (0),(0),(0);
  1319. CREATE TABLE t3 ( f10 int, f11 int) ENGINE=Aria;
  1320. SELECT *
  1321. FROM t4
  1322. WHERE f10 IN
  1323. ( SELECT t1.f11
  1324. FROM t1
  1325. LEFT JOIN t2 JOIN t3 ON t3.f10 = t2.f10 ON t3.f11 != 0 );
  1326. f10
  1327. x
  1328. m
  1329. c
  1330. drop table t1,t2,t3,t4;
  1331. #
  1332. # BUG#751484: Valgrind warning / sporadic crash in evaluate_join_record sql_select.cc:14099 with semijoin
  1333. #
  1334. CREATE TABLE t1 ( f10 int, f11 int, KEY (f10));
  1335. INSERT IGNORE INTO t1 VALUES (0, 0),(0, 0);
  1336. CREATE TABLE t3 ( f10 int);
  1337. INSERT IGNORE INTO t3 VALUES (0);
  1338. set @tmp_751484= @@optimizer_switch;
  1339. set optimizer_switch='materialization=on';
  1340. SELECT * FROM t1
  1341. WHERE f11 IN (
  1342. SELECT C_SQ1_alias1.f11
  1343. FROM t1 AS C_SQ1_alias1
  1344. JOIN t3 AS C_SQ1_alias2
  1345. ON C_SQ1_alias2.f10 = C_SQ1_alias1.f10
  1346. );
  1347. f10 f11
  1348. 0 0
  1349. 0 0
  1350. set optimizer_switch='materialization=off';
  1351. SELECT * FROM t1
  1352. WHERE f11 IN (
  1353. SELECT C_SQ1_alias1.f11
  1354. FROM t1 AS C_SQ1_alias1
  1355. JOIN t3 AS C_SQ1_alias2
  1356. ON C_SQ1_alias2.f10 = C_SQ1_alias1.f10
  1357. );
  1358. f10 f11
  1359. 0 0
  1360. 0 0
  1361. set optimizer_switch=@tmp_751484;
  1362. drop table t1, t3;
  1363. # BUG#795530 Wrong result with subquery semijoin materialization and outer join
  1364. # Simplified testcase that uses DuplicateElimination
  1365. #
  1366. create table t1 (a int);
  1367. create table t2 (a int, b char(10));
  1368. insert into t1 values (1),(2);
  1369. insert into t2 values (1, 'one'), (3, 'three');
  1370. create table t3 (b char(10));
  1371. insert into t3 values('three'),( 'four');
  1372. insert into t3 values('three'),( 'four');
  1373. insert into t3 values('three'),( 'four');
  1374. insert into t3 values('three'),( 'four');
  1375. explain select * from t3 where t3.b in (select t2.b from t1 left join t2 on t1.a=t2.a);
  1376. id select_type table type possible_keys key key_len ref rows Extra
  1377. 1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 2
  1378. 1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where; Using join buffer (flat, BNL join)
  1379. 2 MATERIALIZED t1 ALL NULL NULL NULL NULL 2
  1380. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using where
  1381. select * from t3 where t3.b in (select t2.b from t1 left join t2 on t1.a=t2.a);
  1382. b
  1383. drop table t1, t2, t3;
  1384. #
  1385. # BUG#600958 RQG: Crash in optimize_semijoin_nests
  1386. #
  1387. CREATE TABLE t1 (
  1388. pk int(11) NOT NULL AUTO_INCREMENT,
  1389. col_int_key int(11) DEFAULT NULL,
  1390. col_date_key date DEFAULT NULL,
  1391. col_varchar_key varchar(1) DEFAULT NULL,
  1392. PRIMARY KEY (pk),
  1393. KEY col_int_key (col_int_key),
  1394. KEY col_date_key (col_date_key),
  1395. KEY col_varchar_key (col_varchar_key,col_int_key)
  1396. ) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
  1397. INSERT INTO t1 VALUES (10,8,'2002-02-21',NULL);
  1398. CREATE TABLE t2 (
  1399. pk int(11) NOT NULL AUTO_INCREMENT,
  1400. col_int_key int(11) DEFAULT NULL,
  1401. col_date_key date DEFAULT NULL,
  1402. col_varchar_key varchar(1) DEFAULT NULL,
  1403. PRIMARY KEY (pk),
  1404. KEY col_int_key (col_int_key),
  1405. KEY col_date_key (col_date_key),
  1406. KEY col_varchar_key (col_varchar_key,col_int_key)
  1407. ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
  1408. INSERT INTO t2 VALUES (1,7,'1900-01-01','f');
  1409. SELECT col_date_key FROM t1
  1410. WHERE 5 IN (
  1411. SELECT SUBQUERY3_t1 .col_int_key
  1412. FROM t2 SUBQUERY3_t1
  1413. LEFT JOIN t1 SUBQUERY3_t2 ON SUBQUERY3_t1 .col_varchar_key
  1414. );
  1415. col_date_key
  1416. drop table t2, t1;
  1417. #
  1418. # No BUG#: Duplicate weedout check is not done for outer joins
  1419. #
  1420. create table t1 (a int);
  1421. create table t2 (a int);
  1422. insert into t1 values (1),(1),(2),(2);
  1423. insert into t2 values (1);
  1424. create table t0 (a int);
  1425. insert into t0 values (1),(2);
  1426. set @tmp_20110622= @@optimizer_switch;
  1427. set optimizer_switch='firstmatch=off,loosescan=off,materialization=off';
  1428. # Check DuplicateWeedout + join buffer
  1429. explain
  1430. select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a);
  1431. id select_type table type possible_keys key key_len ref rows Extra
  1432. 1 PRIMARY t0 ALL NULL NULL NULL NULL 2
  1433. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary
  1434. 1 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where; End temporary
  1435. select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a);
  1436. a
  1437. 1
  1438. 2
  1439. # Check DuplicateWeedout without join buffer
  1440. set @tmp_jcl_20110622= @@join_cache_level;
  1441. set join_cache_level= 0;
  1442. explain
  1443. select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a);
  1444. id select_type table type possible_keys key key_len ref rows Extra
  1445. 1 PRIMARY t0 ALL NULL NULL NULL NULL 2
  1446. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary
  1447. 1 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where; End temporary
  1448. select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a);
  1449. a
  1450. 1
  1451. 2
  1452. # Check FirstMatch without join buffer:
  1453. set optimizer_switch='firstmatch=on';
  1454. explain
  1455. select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a);
  1456. id select_type table type possible_keys key key_len ref rows Extra
  1457. 1 PRIMARY t0 ALL NULL NULL NULL NULL 2
  1458. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary
  1459. 1 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where; End temporary
  1460. select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a);
  1461. a
  1462. 1
  1463. 2
  1464. #
  1465. # Now, check the same for multiple inner tables:
  1466. alter table t2 add b int;
  1467. update t2 set b=a;
  1468. create table t3 as select * from t2;
  1469. set optimizer_switch='firstmatch=off';
  1470. set join_cache_level= 0;
  1471. # DuplicateWeedout without join buffer
  1472. explain
  1473. select * from t0
  1474. where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a);
  1475. id select_type table type possible_keys key key_len ref rows Extra
  1476. 1 PRIMARY t0 ALL NULL NULL NULL NULL 2
  1477. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary
  1478. 1 PRIMARY t3 ALL NULL NULL NULL NULL 1 Using where
  1479. 1 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where; End temporary
  1480. select * from t0
  1481. where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a);
  1482. a
  1483. 1
  1484. 2
  1485. set @@join_cache_level=@tmp_jcl_20110622;
  1486. # DuplicateWeedout + join buffer
  1487. explain
  1488. select * from t0
  1489. where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a);
  1490. id select_type table type possible_keys key key_len ref rows Extra
  1491. 1 PRIMARY t0 ALL NULL NULL NULL NULL 2
  1492. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary
  1493. 1 PRIMARY t3 ALL NULL NULL NULL NULL 1 Using where
  1494. 1 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where; End temporary
  1495. select * from t0
  1496. where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a);
  1497. a
  1498. 1
  1499. 2
  1500. # Now, let the inner join side have a 'partial' match
  1501. select * from t3;
  1502. a b
  1503. 1 1
  1504. insert into t3 values(2,2);
  1505. explain
  1506. select * from t0
  1507. where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a);
  1508. id select_type table type possible_keys key key_len ref rows Extra
  1509. 1 PRIMARY t0 ALL NULL NULL NULL NULL 2
  1510. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary
  1511. 1 PRIMARY t2 ALL NULL NULL NULL NULL 1
  1512. 1 PRIMARY t3 ALL NULL NULL NULL NULL 2 Using where; End temporary
  1513. select * from t0
  1514. where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a);
  1515. a
  1516. 1
  1517. 2
  1518. set @@optimizer_switch=@tmp_20110622;
  1519. drop table t0, t1, t2, t3;
  1520. #
  1521. # BUG#802965: Crash in do_copy_not_null with semijoin=on in maria-5.3
  1522. #
  1523. set @save_802965= @@optimizer_switch;
  1524. set optimizer_switch='semijoin=on,materialization=off,firstmatch=off,loosescan=off';
  1525. CREATE TABLE t2 ( f1 int NOT NULL , PRIMARY KEY (f1)) ;
  1526. INSERT IGNORE INTO t2 VALUES (19),(20);
  1527. CREATE TABLE t1 ( f1 int NOT NULL , PRIMARY KEY (f1)) ;
  1528. INSERT IGNORE INTO t1 VALUES (21),(22),(23),(24);
  1529. SELECT *
  1530. FROM t2 , t1
  1531. WHERE t2.f1 IN
  1532. (
  1533. SELECT SQ1_alias1.f1
  1534. FROM t1 AS SQ1_alias1 LEFT JOIN t2 AS SQ1_alias2 JOIN t2 AS SQ1_alias3 ON SQ1_alias3.f1 ON SQ1_alias3.f1
  1535. )
  1536. AND t1.f1 = t2.f1 ;
  1537. f1 f1
  1538. DROP TABLE t1, t2;
  1539. set optimizer_switch=@save_802965;
  1540. #
  1541. # BUG#803365: Crash in pull_out_semijoin_tables with outer join + semijoin + derived tables in maria-5.3 with WL#106
  1542. #
  1543. CREATE TABLE t1 ( f1 int) ;
  1544. INSERT INTO t1 VALUES (1),(1);
  1545. CREATE TABLE t2 ( f2 int) ;
  1546. INSERT INTO t2 VALUES (1),(1);
  1547. CREATE TABLE t3 ( f3 int) ;
  1548. INSERT INTO t3 VALUES (1),(1);
  1549. SELECT *
  1550. FROM t1
  1551. WHERE t1.f1 IN (
  1552. SELECT t2.f2
  1553. FROM t2
  1554. LEFT JOIN (
  1555. SELECT *
  1556. FROM t3
  1557. ) AS alias1
  1558. ON alias1.f3 = t2.f2
  1559. );
  1560. f1
  1561. 1
  1562. 1
  1563. DROP TABLE t1,t2,t3;
  1564. #
  1565. # BUG#611704: Crash in replace_where_subcondition with nested subquery and semijoin=on
  1566. #
  1567. CREATE TABLE t1 ( f1 int) ;
  1568. CREATE TABLE t2 ( f1 int) ;
  1569. CREATE TABLE t3 ( f1 int) ;
  1570. SELECT * FROM (
  1571. SELECT t3.*
  1572. FROM t2 STRAIGHT_JOIN t3
  1573. ON t3.f1
  1574. AND (t3.f1 ) IN (
  1575. SELECT t1.f1
  1576. FROM t1
  1577. )
  1578. ) AS alias1;
  1579. f1
  1580. DROP TABLE t1,t2,t3;
  1581. # BUG#611704: another testcase:
  1582. CREATE TABLE t1 ( f1 int(11), f3 varchar(1), f4 varchar(1)) ;
  1583. CREATE TABLE t2 ( f2 int(11), KEY (f2));
  1584. CREATE TABLE t3 ( f4 varchar(1)) ;
  1585. PREPARE st1 FROM '
  1586. SELECT *
  1587. FROM t1
  1588. STRAIGHT_JOIN ( t2 STRAIGHT_JOIN t3 ON t2.f2 )
  1589. ON (t1.f3) IN ( SELECT f4 FROM t1 )
  1590. ';
  1591. EXECUTE st1;
  1592. f1 f3 f4 f2 f4
  1593. DROP TABLE t1,t2,t3;
  1594. #
  1595. # BUG#803457: Wrong result with semijoin + view + outer join in maria-5.3-subqueries-mwl90
  1596. # (Original testcase)
  1597. #
  1598. CREATE TABLE t1 (f1 int, f2 int );
  1599. INSERT INTO t1 VALUES (2,0),(4,0),(0,NULL);
  1600. CREATE TABLE t2 (f2 int, f3 int );
  1601. INSERT INTO t2 VALUES (NULL,NULL),(0,0);
  1602. CREATE TABLE t3 ( f1 int, f3 int );
  1603. INSERT INTO t3 VALUES (2,0),(4,0),(0,NULL),(4,0),(8,0);
  1604. CREATE TABLE t4 ( f2 int, KEY (f2) );
  1605. INSERT INTO t4 VALUES (0),(NULL);
  1606. CREATE VIEW v4 AS SELECT DISTINCT f2 FROM t4 ;
  1607. # The following must not have outer joins:
  1608. explain extended
  1609. SELECT * FROM t1 NATURAL LEFT JOIN (t2, t3) WHERE t2.f3 IN (SELECT * FROM t4);
  1610. id select_type table type possible_keys key key_len ref rows filtered Extra
  1611. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
  1612. 1 PRIMARY t4 ref f2 f2 5 test.t2.f3 2 100.00 Using index; FirstMatch(t2)
  1613. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join)
  1614. 1 PRIMARY t3 ALL NULL NULL NULL NULL 5 100.00 Using where; Using join buffer (flat, BNL join)
  1615. Warnings:
  1616. Note 1003 select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2`,`test`.`t2`.`f3` AS `f3`,`test`.`t3`.`f3` AS `f3` from `test`.`t1` semi join (`test`.`t4`) join `test`.`t2` join `test`.`t3` where ((`test`.`t1`.`f2` = `test`.`t2`.`f2`) and (`test`.`t3`.`f1` = `test`.`t1`.`f1`) and (`test`.`t4`.`f2` = `test`.`t2`.`f3`))
  1617. SELECT * FROM t1 NATURAL LEFT JOIN (t2, t3) WHERE t2.f3 IN (SELECT * FROM t4);
  1618. f1 f2 f3 f3
  1619. 2 0 0 0
  1620. 4 0 0 0
  1621. 4 0 0 0
  1622. drop view v4;
  1623. drop table t1, t2, t3, t4;
  1624. #
  1625. # BUG#803303: Wrong result with semijoin=on, outer join in maria-5.3-subqueries-mwl90
  1626. #
  1627. # Testcase#1:
  1628. set @tmp803303= @@optimizer_switch;
  1629. set optimizer_switch = 'semijoin=on,materialization=off,firstmatch=off,loosescan=off';
  1630. CREATE TABLE t2 ( f1 int) ;
  1631. INSERT IGNORE INTO t2 VALUES (6),(8);
  1632. CREATE TABLE t1 ( f1 int, f2 int, f3 int) ;
  1633. INSERT IGNORE INTO t1 VALUES (8,0,0),(7,0,0),(9,0,0);
  1634. SELECT alias2.f1
  1635. FROM t2 AS alias1
  1636. LEFT JOIN ( t1 AS alias2 JOIN t1 AS alias3 ON alias3.f2 = alias2.f3 )
  1637. ON alias3.f2 = alias2.f2
  1638. WHERE alias2.f1 IN ( SELECT f1 FROM t2 AS alias4 ) ;
  1639. f1
  1640. 8
  1641. 8
  1642. 8
  1643. 8
  1644. 8
  1645. 8
  1646. drop table t1,t2;
  1647. set optimizer_switch= @tmp803303;
  1648. # Testcase #2:
  1649. CREATE TABLE t1 ( f10 int) ;
  1650. INSERT INTO t1 VALUES (0),(0);
  1651. CREATE TABLE t2 ( f10 int, f11 varchar(1)) ;
  1652. INSERT INTO t2 VALUES (0,'a'),(0,'b');
  1653. CREATE TABLE t3 ( f10 int) ;
  1654. INSERT INTO t3 VALUES (0),(0),(0),(0),(0);
  1655. CREATE TABLE t4 ( f10 varchar(1), f11 int) ;
  1656. INSERT INTO t4 VALUES ('a',0),('b',0);
  1657. SELECT * FROM t1
  1658. LEFT JOIN ( t2 JOIN t3 ON t3.f10 = t2.f10 ) ON t1.f10 = t2.f10
  1659. WHERE t2.f10 IN (
  1660. SELECT t4.f11
  1661. FROM t4
  1662. WHERE t4.f10 != t2.f11
  1663. );
  1664. f10 f10 f11 f10
  1665. 0 0 a 0
  1666. 0 0 a 0
  1667. 0 0 b 0
  1668. 0 0 b 0
  1669. 0 0 a 0
  1670. 0 0 a 0
  1671. 0 0 b 0
  1672. 0 0 b 0
  1673. 0 0 a 0
  1674. 0 0 a 0
  1675. 0 0 b 0
  1676. 0 0 b 0
  1677. 0 0 a 0
  1678. 0 0 a 0
  1679. 0 0 b 0
  1680. 0 0 b 0
  1681. 0 0 a 0
  1682. 0 0 a 0
  1683. 0 0 b 0
  1684. 0 0 b 0
  1685. drop table t1,t2,t3,t4;
  1686. #
  1687. # BUG#803457: Wrong result with semijoin + view + outer join in maria-5.3-subqueries-mwl90
  1688. #
  1689. set @tmp803457=@@optimizer_switch;
  1690. set optimizer_switch='materialization=off';
  1691. CREATE TABLE t1 (f1 int, f2 int );
  1692. INSERT INTO t1 VALUES (2,0),(4,0),(0,NULL);
  1693. CREATE TABLE t2 (f2 int, f3 int );
  1694. INSERT INTO t2 VALUES (NULL,NULL),(0,0);
  1695. CREATE TABLE t3 ( f1 int, f3 int );
  1696. INSERT INTO t3 VALUES (2,0),(4,0),(0,NULL),(4,0),(8,0);
  1697. CREATE TABLE t4 ( f2 int);
  1698. INSERT INTO t4 VALUES (0),(NULL);
  1699. # The following uses Duplicate Weedout, and "End temporary" must not be
  1700. # in the middle of the inner side of an outer join:
  1701. explain
  1702. SELECT * FROM t1 NATURAL LEFT JOIN (t2, t3) WHERE IFNULL(t2.f3,'foo') IN (SELECT * FROM t4);
  1703. id select_type table type possible_keys key key_len ref rows Extra
  1704. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3
  1705. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where
  1706. 1 PRIMARY t3 ALL NULL NULL NULL NULL 5 Using where
  1707. 1 PRIMARY t4 ALL NULL NULL NULL NULL 2 Using where; Start temporary; End temporary
  1708. SELECT * FROM t1 NATURAL LEFT JOIN (t2, t3 ) WHERE IFNULL(t2.f3,'foo') IN (SELECT * FROM t4);
  1709. f1 f2 f3 f3
  1710. 2 0 0 0
  1711. 4 0 0 0
  1712. 4 0 0 0
  1713. 0 NULL NULL NULL
  1714. DROP TABLE t1, t2, t3, t4;
  1715. set @tmp803457=@@optimizer_switch;
  1716. #
  1717. # BUG#818280: crash in do_copy_not_null() in maria-5.3 with semijoin
  1718. #
  1719. CREATE TABLE t1 ( c1 int NOT NULL , c2 int NOT NULL, PRIMARY KEY (c1)) ;
  1720. INSERT IGNORE INTO t1 VALUES (2,7),(1,3),(5,6);
  1721. CREATE TABLE t3 ( c1 int NOT NULL , c2 int NOT NULL, PRIMARY KEY (c1)) ;
  1722. INSERT IGNORE INTO t3 VALUES (2,7),(1,3),(5,6);
  1723. CREATE TABLE t2 ( c1 int NOT NULL , c5 int NOT NULL );
  1724. INSERT IGNORE INTO t2 VALUES (2,2),(2,2),(5,6);
  1725. SELECT * FROM t1 WHERE c1 IN ( SELECT t3.c1 FROM t3 LEFT JOIN t2 ON t2 .c1 = t3 .c1 WHERE t2.c5 != 0 );
  1726. c1 c2
  1727. 2 7
  1728. 5 6
  1729. DROP TABLE t1, t2, t3;
  1730. #
  1731. # BUG#834534: Assertion `0' failed in replace_where_subcondition with semijoin subquery in HAVING
  1732. #
  1733. CREATE TABLE t1 ( d int );
  1734. INSERT INTO t1 VALUES (2),(2),(0),(2),(2);
  1735. CREATE TABLE t2 ( b int );
  1736. INSERT INTO t2 VALUES (4),(3),(3);
  1737. CREATE TABLE t3 ( a int );
  1738. SELECT *
  1739. FROM t3
  1740. WHERE (t3.a) IN (
  1741. SELECT t1.d
  1742. FROM t1
  1743. HAVING ( 4 ) IN (
  1744. SELECT t2.b
  1745. FROM t2
  1746. )
  1747. );
  1748. a
  1749. drop table t1, t2,t3;
  1750. #
  1751. # BUG#834758: Wrong result with innner join, LooseScan, two-column IN() predicate
  1752. #
  1753. set @tmp835758=@@optimizer_switch;
  1754. set optimizer_switch='semijoin=on,loosescan=on,materialization=off,firstmatch=off';
  1755. CREATE TABLE t1 (b int) ;
  1756. INSERT INTO t1 VALUES (1),(5);
  1757. CREATE TABLE t2 (a int, PRIMARY KEY (a)) ;
  1758. INSERT INTO t2 VALUES (6),(10);
  1759. CREATE TABLE t3 (a int, b int, KEY (b)) ;
  1760. INSERT INTO t3 VALUES (6,5),(6,2),(8,0),(9,1),(6,5);
  1761. # This used to incorrectly pick a join order of (t1, LooseScan(t3), t2):
  1762. explain
  1763. SELECT * FROM t1, t2 WHERE (t2.a , t1.b) IN (SELECT a, b FROM t3);
  1764. id select_type table type possible_keys key key_len ref rows Extra
  1765. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2
  1766. 1 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 2 Using index; Using join buffer (flat, BNL join)
  1767. 1 PRIMARY t3 ALL b NULL NULL NULL 5 Using where; Start temporary; End temporary
  1768. SELECT * FROM t1, t2 WHERE (t2.a , t1.b) IN (SELECT a, b FROM t3);
  1769. b a
  1770. 5 6
  1771. DROP TABLE t1, t2, t3;
  1772. set @@optimizer_switch= @tmp835758;
  1773. #
  1774. # BUG#834739: Wrong result with 3-way inner join, LooseScan,multipart keys
  1775. #
  1776. set @tmp834739=@@optimizer_switch;
  1777. set optimizer_switch='semijoin=on,loosescan=on,materialization=off,firstmatch=off';
  1778. CREATE TABLE t2 ( b int, c int, KEY (b)) ;
  1779. INSERT INTO t2 VALUES (1,0),(1,0),(9,0),(1,0),(5,0);
  1780. INSERT INTO t2 VALUES (2,0),(3,0),(8,0),(6,0),(5,0);
  1781. CREATE TABLE t3 ( a int);
  1782. INSERT INTO t3 VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0);
  1783. CREATE TABLE t4 ( a int);
  1784. INSERT INTO t4 VALUES (0),(0),(0);
  1785. CREATE TABLE t5 ( b int, a int , KEY (a,b)) ;
  1786. INSERT INTO t5 VALUES (7,0),(9,0);
  1787. explain
  1788. SELECT * FROM t3 WHERE t3.a IN (SELECT t5.a FROM t2, t4, t5 WHERE t2.c = t5.a AND t2.b = t5.b);
  1789. id select_type table type possible_keys key key_len ref rows Extra
  1790. 1 PRIMARY t5 index a a 10 NULL 2 Using where; Using index; LooseScan
  1791. 1 PRIMARY t4 ALL NULL NULL NULL NULL 3
  1792. 1 PRIMARY t2 ref b b 5 test.t5.b 2 Using where; FirstMatch(t5)
  1793. 1 PRIMARY t3 ALL NULL NULL NULL NULL 15 Using where; Using join buffer (flat, BNL join)
  1794. SELECT * FROM t3 WHERE t3.a IN (SELECT t5.a FROM t2, t4, t5 WHERE t2.c = t5.a AND t2.b = t5.b);
  1795. a
  1796. 0
  1797. 0
  1798. 0
  1799. 0
  1800. 0
  1801. 0
  1802. 0
  1803. 0
  1804. 0
  1805. 0
  1806. 0
  1807. 0
  1808. 0
  1809. 0
  1810. 0
  1811. DROP TABLE t2, t3, t4, t5;
  1812. set @@optimizer_switch=@tmp834739;
  1813. #
  1814. # BUG#830993: Crash in end_read_record with derived table
  1815. #
  1816. set @tmp_830993=@@optimizer_switch;
  1817. set optimizer_switch='semijoin=on,loosescan=off,materialization=off,firstmatch=off';
  1818. set @tmp_830993_jbs= @@join_buffer_size;
  1819. set join_buffer_size=160;
  1820. CREATE TABLE t1 (
  1821. a int(11) NOT NULL AUTO_INCREMENT,
  1822. b int(11) DEFAULT NULL,
  1823. c int(11) DEFAULT NULL,
  1824. d time DEFAULT NULL,
  1825. e varchar(1) DEFAULT NULL,
  1826. f varchar(1) DEFAULT NULL,
  1827. PRIMARY KEY (a),
  1828. KEY c (c),
  1829. KEY d (d),
  1830. KEY e (e,c)
  1831. );
  1832. INSERT INTO t1 VALUES (10,NULL,8,'22:55:23','x','x'),
  1833. (11,8,7,'10:19:31','d','d'),(12,1,1,'14:40:36','r','r'),
  1834. (13,9,7,'04:37:47','f','f'),(14,4,9,'19:34:06','y','y'),
  1835. (15,3,NULL,'20:35:33','u','u'),(16,2,1,NULL,'m','m'),
  1836. (17,NULL,9,'14:43:37',NULL,NULL),(18,2,2,'02:23:09','o','o'),
  1837. (19,NULL,9,'01:22:45','w','w'),(20,6,2,'00:00:00','m','m'),
  1838. (21,7,4,'00:13:25','q','q'),(22,2,0,'03:47:16',NULL,NULL),
  1839. (23,5,4,'01:41:48','d','d'),(24,7,8,'00:00:00','g','g'),
  1840. (25,6,NULL,'22:32:04','x','x'),(26,6,NULL,'16:44:14','f','f'),
  1841. (27,2,0,'17:38:37','p','p'),(28,9,NULL,'08:46:48','j','j'),
  1842. (29,6,8,'14:11:27','c','c');
  1843. CREATE TABLE t2 like t1;
  1844. INSERT INTO t2 VALUES (1,2,4,'22:34:09','v','v'),
  1845. (2,150,62,'14:26:02','v','v'),(3,NULL,7,'14:03:03','c','c'),
  1846. (4,2,1,'01:46:09',NULL,NULL),(5,5,0,'16:21:18','x','x'),
  1847. (6,3,7,'18:56:33','i','i'),(7,1,7,NULL,'e','e'),
  1848. (8,4,1,'09:29:08','p','p'),(9,NULL,7,'19:11:10','s','s'),
  1849. (10,2,1,'11:57:26','j','j'),(11,6,5,'00:39:46','z','z'),
  1850. (12,6,2,'03:28:15','c','c'),(13,8,0,'06:44:18','a','a'),
  1851. (14,2,1,'14:36:39','q','q'),(15,6,8,'18:42:45','y','y'),
  1852. (16,8,1,'02:57:29',NULL,NULL),(17,3,1,'16:46:13','r','r'),
  1853. (18,3,9,'19:39:02','v','v'),(19,9,1,NULL,NULL,NULL),
  1854. (20,6,5,'20:58:33','r','r');
  1855. set @tmp_optimizer_switch=@@optimizer_switch;
  1856. set optimizer_switch='derived_merge=off,derived_with_keys=off';
  1857. explain
  1858. SELECT
  1859. alias1.a, alias1.b, alias1.c, alias1.d, alias1.e, alias1.f,
  1860. alias2.a as a2_a, alias2.b as a2_b, alias2.c as a2_c, alias2.d as a2_d,
  1861. alias2.e as a2_e, alias2.f as a2_f,
  1862. t2.a as t2_a, t2.b as t2_b, t2.c as t2_c, t2.d as t2_d, t2.e as t2_e, t2.f as t2_f
  1863. FROM
  1864. (SELECT * FROM t2) AS alias1,
  1865. t1 AS alias2,
  1866. t2
  1867. WHERE
  1868. alias1.c IN (SELECT SQ3_alias1.b
  1869. FROM t2 AS SQ3_alias1 STRAIGHT_JOIN t2 AS SQ3_alias2)
  1870. LIMIT 100;
  1871. id select_type table type possible_keys key key_len ref rows Extra
  1872. 1 PRIMARY <derived2> ALL NULL NULL NULL NULL 20
  1873. 1 PRIMARY alias2 ALL NULL NULL NULL NULL 20 Using join buffer (flat, BNL join)
  1874. 1 PRIMARY t2 ALL NULL NULL NULL NULL 20 Using join buffer (flat, BNL join)
  1875. 1 PRIMARY SQ3_alias1 ALL NULL NULL NULL NULL 20 Using where; Start temporary
  1876. 1 PRIMARY SQ3_alias2 index NULL PRIMARY 4 NULL 20 Using index; End temporary
  1877. 2 DERIVED t2 ALL NULL NULL NULL NULL 20
  1878. create table t3 as
  1879. SELECT
  1880. alias1.a, alias1.b, alias1.c, alias1.d, alias1.e, alias1.f,
  1881. alias2.a as a2_a, alias2.b as a2_b, alias2.c as a2_c, alias2.d as a2_d,
  1882. alias2.e as a2_e, alias2.f as a2_f,
  1883. t2.a as t2_a, t2.b as t2_b, t2.c as t2_c, t2.d as t2_d, t2.e as t2_e, t2.f as t2_f
  1884. FROM
  1885. (SELECT * FROM t2) AS alias1,
  1886. t1 AS alias2,
  1887. t2
  1888. WHERE
  1889. alias1.c IN (SELECT SQ3_alias1.b
  1890. FROM t2 AS SQ3_alias1 STRAIGHT_JOIN t2 AS SQ3_alias2)
  1891. LIMIT 100;
  1892. set optimizer_switch=@tmp_optimizer_switch;
  1893. drop table t1,t2, t3;
  1894. set optimizer_switch=@tmp_830993;
  1895. set join_buffer_size= @tmp_830993_jbs;
  1896. #
  1897. # BUG##849717: Crash in Item_func::fix_fields on second execution of a prepared statement with semijoin
  1898. #
  1899. CREATE TABLE t1 (a int);
  1900. CREATE TABLE t2 (a int);
  1901. CREATE TABLE t3 (a int, b int) ;
  1902. PREPARE st1 FROM "SELECT * FROM t2 LEFT JOIN t1 ON t2.a != 0 AND ('j','r') IN ( SELECT b,a FROM t3)";
  1903. EXECUTE st1;
  1904. a a
  1905. EXECUTE st1;
  1906. a a
  1907. DROP TABLE t1, t2, t3;
  1908. #
  1909. # BUG#849776: Wrong result with semijoin + "Impossible where"
  1910. #
  1911. CREATE TABLE t1 ( b varchar(1), a integer) ;
  1912. INSERT INTO t1 VALUES ('z',8);
  1913. CREATE TABLE t2 ( a integer, b varchar(1)) ;
  1914. CREATE TABLE t4 ( a integer, b varchar(1)) ;
  1915. CREATE TABLE t5 ( a integer) ;
  1916. INSERT INTO t5 VALUES (8);
  1917. select * from t5 where (a) in (
  1918. SELECT t1.a
  1919. FROM t1 LEFT JOIN t2 ON t1.a = t2.a
  1920. WHERE t2.b NOT IN (SELECT t4.b FROM t4 WHERE t4.b < t1.b)
  1921. );
  1922. a
  1923. 8
  1924. DROP TABLE t1, t2, t4, t5;
  1925. #
  1926. # BUG#861147: Assertion `fixed == 1' failed in Item_func_eq::val_int() with semijoin + materialization + max_join_size
  1927. #
  1928. CREATE TABLE t1 ( f2 int) ;
  1929. CREATE TABLE t2 ( f1 int, f3 int, f4 varchar(3), f5 varchar(35)) ;
  1930. INSERT INTO t2 VALUES (4057,9,'USA','Visalia'),(3993,11,'USA','Waco'),
  1931. (3948,14,'USA','Warren'),(3813,57,'USA','Washington'),
  1932. (4010,11,'USA','Waterbury'),(4017,11,'USA','West Covina'),
  1933. (4004,11,'USA','West Valley City'),(4033,10,'USA','Westminster'),
  1934. (3842,34,'USA','Wichita'),(4018,10,'USA','Wichita Falls'),
  1935. (3899,19,'USA','Winston-Salem'),(3914,17,'USA','Worcester'),
  1936. (3888,20,'USA','Yonkers');
  1937. CREATE TABLE t3 ( f3 int, f4 varchar(3)) ;
  1938. INSERT INTO t3 VALUES (86,'USA');
  1939. CREATE TABLE t4 ( f3 int, f4 varchar(3), f5 varchar(52)) ;
  1940. INSERT INTO t4 VALUES (0,'RUS','Belorussian'),(0,'USA','Portuguese');
  1941. CREATE TABLE t5 ( f2 int) ;
  1942. CREATE TABLE t6 ( f4 varchar(3));
  1943. INSERT INTO t6 VALUES ('RUS'),('USA');
  1944. set @tmp_mjs_861147= @@max_join_size;
  1945. SET max_join_size=10;
  1946. set @tmp_os_861147= @@optimizer_switch;
  1947. set @@optimizer_switch='semijoin=on,materialization=on';
  1948. SELECT *
  1949. FROM t1
  1950. WHERE ( 1 , 3 ) IN (
  1951. SELECT t2.f1 , MAX( t3.f3 )
  1952. FROM t2
  1953. JOIN t3
  1954. WHERE t3.f4 IN (
  1955. SELECT t4.f5
  1956. FROM t4
  1957. STRAIGHT_JOIN t5
  1958. WHERE t4.f4 < t2.f5
  1959. )
  1960. ) AND ( 'p' , 'k' ) IN (
  1961. SELECT f4 , f4 FROM t6
  1962. );
  1963. ERROR 42000: The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET SQL_MAX_JOIN_SIZE=# if the SELECT is okay
  1964. set max_join_size= @tmp_mjs_861147;
  1965. set optimizer_switch= @tmp_os_861147;
  1966. DROP TABLE t1,t2,t3,t4,t5,t6;
  1967. #
  1968. # BUG#877288: Wrong result with semijoin + materialization + multipart key
  1969. #
  1970. set @tmp_877288=@@optimizer_switch;
  1971. set optimizer_switch='semijoin=ON,materialization=ON';
  1972. CREATE TABLE t1 ( a int) ;
  1973. INSERT INTO t1 VALUES (19),(19),(19),(20),(20),(20),(20),(20),(20);
  1974. CREATE TABLE t2 ( b int NOT NULL , c int NOT NULL , KEY (b,c)) ;
  1975. INSERT INTO t2 VALUES (14,1),(15,1),(16,1),(17,1),(18,1),(19,1),(20,1);
  1976. CREATE TABLE t3 ( a int, d int) ;
  1977. INSERT INTO t3 VALUES (19,1),(7,1),(3,1),(3,1),(20,1),(3,1),(16,1),(17,1),(9,1),(4,1),(6,1),(15,1),(17,1);
  1978. explain
  1979. SELECT * FROM t1 WHERE (a) IN (SELECT a FROM t2 JOIN t3 ON b = a);
  1980. id select_type table type possible_keys key key_len ref rows Extra
  1981. 1 PRIMARY t1 ALL NULL NULL NULL NULL 9
  1982. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 5 func 1
  1983. 2 MATERIALIZED t3 ALL NULL NULL NULL NULL 13 Using where
  1984. 2 MATERIALIZED t2 ref b b 4 test.t3.a 1 Using index
  1985. SELECT * FROM t1 WHERE (a) IN (SELECT a FROM t2 JOIN t3 ON b = a);
  1986. a
  1987. 19
  1988. 19
  1989. 19
  1990. 20
  1991. 20
  1992. 20
  1993. 20
  1994. 20
  1995. 20
  1996. DROP TABLE t1,t2,t3;
  1997. set optimizer_switch=@tmp_877288;
  1998. #
  1999. # BUG#878753: Assertion '0' failed in replace_where_subcondition with derived_merge
  2000. #
  2001. set @tmp878753= @@optimizer_switch;
  2002. set optimizer_switch= 'semijoin=on,derived_merge=on';
  2003. CREATE TABLE t1 (b int(11)) ;
  2004. CREATE TABLE t2 (c int, b int, d varchar(52) NOT NULL) ;
  2005. CREATE TABLE t3 (b int(11)) ;
  2006. PREPARE st1 FROM '
  2007. SELECT * FROM t1
  2008. JOIN (
  2009. SELECT t2.* FROM t2
  2010. WHERE t2.d <> "a"
  2011. AND t2.c IN (
  2012. SELECT t3.b
  2013. FROM t3
  2014. )
  2015. ) AS alias2
  2016. ON ( alias2.b = t1.b );
  2017. ';
  2018. EXECUTE st1;
  2019. b c b d
  2020. DROP TABLE t1,t2,t3;
  2021. set optimizer_switch=@tmp878753;
  2022. #
  2023. # Bug #889750: semijoin=on + firstmatch=off + semijoin_with_cache=off
  2024. #
  2025. create table t1 (a int);
  2026. insert into t1 values (7), (1), (5), (3);
  2027. create table t2 (a int);
  2028. insert into t2 values (4), (1), (8), (3), (9), (2);
  2029. set @tmp_otimizer_switch= @@optimizer_switch;
  2030. set optimizer_switch='semijoin=on';
  2031. set optimizer_switch='firstmatch=off';
  2032. set optimizer_switch='semijoin_with_cache=on';
  2033. explain
  2034. select * from t1 where t1.a in (select t2.a from t2);
  2035. id select_type table type possible_keys key key_len ref rows Extra
  2036. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4
  2037. 1 PRIMARY t2 ALL NULL NULL NULL NULL 6 Using where; Start temporary; End temporary; Using join buffer (flat, BNL join)
  2038. select * from t1 where t1.a in (select t2.a from t2);
  2039. a
  2040. 1
  2041. 3
  2042. set optimizer_switch='semijoin_with_cache=off';
  2043. explain
  2044. select * from t1 where t1.a in (select t2.a from t2);
  2045. id select_type table type possible_keys key key_len ref rows Extra
  2046. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4
  2047. 1 PRIMARY t2 ALL NULL NULL NULL NULL 6 Using where; Start temporary; End temporary
  2048. select * from t1 where t1.a in (select t2.a from t2);
  2049. a
  2050. 1
  2051. 3
  2052. set optimizer_switch= @tmp_otimizer_switch;
  2053. drop table t1,t2;
  2054. #
  2055. # Bug #887496: semijoin with IN equality for the second part of an index
  2056. #
  2057. CREATE TABLE t1 (a int);
  2058. INSERT INTO t1 VALUES (9), (0), (8), (5);
  2059. CREATE TABLE t2 (a int, b varchar(1), INDEX idx (b,a));
  2060. INSERT INTO t2 VALUES (5,'r'), (5,'z');
  2061. CREATE TABLE t3 (a int, b varchar(1), INDEX idx (b,a));
  2062. INSERT INTO t3 VALUES (5,'r'), (5,'z');
  2063. set @tmp_otimizer_switch= @@optimizer_switch;
  2064. SET SESSION optimizer_switch='semijoin=on,firstmatch=on';
  2065. SET SESSION optimizer_switch='loosescan=off';
  2066. EXPLAIN
  2067. SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b);
  2068. id select_type table type possible_keys key key_len ref rows Extra
  2069. 1 PRIMARY t2 index idx idx 9 NULL 2 Using where; Using index; Start temporary
  2070. 1 PRIMARY t3 ref idx idx 4 test.t2.b 1 Using index
  2071. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; End temporary; Using join buffer (flat, BNL join)
  2072. SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b);
  2073. a
  2074. 5
  2075. SET SESSION optimizer_switch='loosescan=on';
  2076. EXPLAIN
  2077. SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b);
  2078. id select_type table type possible_keys key key_len ref rows Extra
  2079. 1 PRIMARY t2 index idx idx 9 NULL 2 Using where; Using index; Start temporary
  2080. 1 PRIMARY t3 ref idx idx 4 test.t2.b 1 Using index
  2081. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; End temporary; Using join buffer (flat, BNL join)
  2082. SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b);
  2083. a
  2084. 5
  2085. set optimizer_switch= @tmp_otimizer_switch;
  2086. DROP TABLE t1,t2,t3;
  2087. set optimizer_switch=@subselect_sj_tmp;