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.

2631 lines
89 KiB

Back-ported the patch of the mysql-5.6 code line that fixed several defects in the greedy optimization: 1) The greedy optimizer calculated the 'compare-cost' (CPU-cost) for iterating over the partial plan result at each level in the query plan as 'record_count / (double) TIME_FOR_COMPARE' This cost was only used locally for 'best' calculation at each level, and *not* accumulated into the total cost for the query plan. This fix added the 'CPU-cost' of processing 'current_record_count' records at each level to 'current_read_time' *before* it is used as 'accumulated cost' argument to recursive best_extension_by_limited_search() calls. This ensured that the cost of a huge join-fanout early in the QEP was correctly reflected in the cost of the final QEP. To get identical cost for a 'best' optimized query and a straight_join with the same join order, the same change was also applied to optimize_straight_join() and get_partial_join_cost() 2) Furthermore to get equal cost for 'best' optimized query and a straight_join the new code substrcated the same '0.001' in optimize_straight_join() as it had been already done in best_extension_by_limited_search() 3) When best_extension_by_limited_search() aggregated the 'best' plan a plan was 'best' by the check : 'if ((search_depth == 1) || (current_read_time < join->best_read))' The term '(search_depth == 1' incorrectly caused a new best plan to be collected whenever the specified 'search_depth' was reached - even if this partial query plan was more expensive than what we had already found.
14 years ago
Back-ported the patch of the mysql-5.6 code line that fixed several defects in the greedy optimization: 1) The greedy optimizer calculated the 'compare-cost' (CPU-cost) for iterating over the partial plan result at each level in the query plan as 'record_count / (double) TIME_FOR_COMPARE' This cost was only used locally for 'best' calculation at each level, and *not* accumulated into the total cost for the query plan. This fix added the 'CPU-cost' of processing 'current_record_count' records at each level to 'current_read_time' *before* it is used as 'accumulated cost' argument to recursive best_extension_by_limited_search() calls. This ensured that the cost of a huge join-fanout early in the QEP was correctly reflected in the cost of the final QEP. To get identical cost for a 'best' optimized query and a straight_join with the same join order, the same change was also applied to optimize_straight_join() and get_partial_join_cost() 2) Furthermore to get equal cost for 'best' optimized query and a straight_join the new code substrcated the same '0.001' in optimize_straight_join() as it had been already done in best_extension_by_limited_search() 3) When best_extension_by_limited_search() aggregated the 'best' plan a plan was 'best' by the check : 'if ((search_depth == 1) || (current_read_time < join->best_read))' The term '(search_depth == 1' incorrectly caused a new best plan to be collected whenever the specified 'search_depth' was reached - even if this partial query plan was more expensive than what we had already found.
14 years ago
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
15 years ago
14 years ago
14 years ago
Back-ported the patch of the mysql-5.6 code line that fixed several defects in the greedy optimization: 1) The greedy optimizer calculated the 'compare-cost' (CPU-cost) for iterating over the partial plan result at each level in the query plan as 'record_count / (double) TIME_FOR_COMPARE' This cost was only used locally for 'best' calculation at each level, and *not* accumulated into the total cost for the query plan. This fix added the 'CPU-cost' of processing 'current_record_count' records at each level to 'current_read_time' *before* it is used as 'accumulated cost' argument to recursive best_extension_by_limited_search() calls. This ensured that the cost of a huge join-fanout early in the QEP was correctly reflected in the cost of the final QEP. To get identical cost for a 'best' optimized query and a straight_join with the same join order, the same change was also applied to optimize_straight_join() and get_partial_join_cost() 2) Furthermore to get equal cost for 'best' optimized query and a straight_join the new code substrcated the same '0.001' in optimize_straight_join() as it had been already done in best_extension_by_limited_search() 3) When best_extension_by_limited_search() aggregated the 'best' plan a plan was 'best' by the check : 'if ((search_depth == 1) || (current_read_time < join->best_read))' The term '(search_depth == 1' incorrectly caused a new best plan to be collected whenever the specified 'search_depth' was reached - even if this partial query plan was more expensive than what we had already found.
14 years ago
Back-ported the patch of the mysql-5.6 code line that fixed several defects in the greedy optimization: 1) The greedy optimizer calculated the 'compare-cost' (CPU-cost) for iterating over the partial plan result at each level in the query plan as 'record_count / (double) TIME_FOR_COMPARE' This cost was only used locally for 'best' calculation at each level, and *not* accumulated into the total cost for the query plan. This fix added the 'CPU-cost' of processing 'current_record_count' records at each level to 'current_read_time' *before* it is used as 'accumulated cost' argument to recursive best_extension_by_limited_search() calls. This ensured that the cost of a huge join-fanout early in the QEP was correctly reflected in the cost of the final QEP. To get identical cost for a 'best' optimized query and a straight_join with the same join order, the same change was also applied to optimize_straight_join() and get_partial_join_cost() 2) Furthermore to get equal cost for 'best' optimized query and a straight_join the new code substrcated the same '0.001' in optimize_straight_join() as it had been already done in best_extension_by_limited_search() 3) When best_extension_by_limited_search() aggregated the 'best' plan a plan was 'best' by the check : 'if ((search_depth == 1) || (current_read_time < join->best_read))' The term '(search_depth == 1' incorrectly caused a new best plan to be collected whenever the specified 'search_depth' was reached - even if this partial query plan was more expensive than what we had already found.
14 years ago
Back-ported the patch of the mysql-5.6 code line that fixed several defects in the greedy optimization: 1) The greedy optimizer calculated the 'compare-cost' (CPU-cost) for iterating over the partial plan result at each level in the query plan as 'record_count / (double) TIME_FOR_COMPARE' This cost was only used locally for 'best' calculation at each level, and *not* accumulated into the total cost for the query plan. This fix added the 'CPU-cost' of processing 'current_record_count' records at each level to 'current_read_time' *before* it is used as 'accumulated cost' argument to recursive best_extension_by_limited_search() calls. This ensured that the cost of a huge join-fanout early in the QEP was correctly reflected in the cost of the final QEP. To get identical cost for a 'best' optimized query and a straight_join with the same join order, the same change was also applied to optimize_straight_join() and get_partial_join_cost() 2) Furthermore to get equal cost for 'best' optimized query and a straight_join the new code substrcated the same '0.001' in optimize_straight_join() as it had been already done in best_extension_by_limited_search() 3) When best_extension_by_limited_search() aggregated the 'best' plan a plan was 'best' by the check : 'if ((search_depth == 1) || (current_read_time < join->best_read))' The term '(search_depth == 1' incorrectly caused a new best plan to be collected whenever the specified 'search_depth' was reached - even if this partial query plan was more expensive than what we had already found.
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 optimizer_switch=ifnull(@optimizer_switch_for_subselect_sj_test,'outer_join_with_cache=off');
  8. SET optimizer_switch=ifnull(@optimizer_switch_for_subselect_sj_test,'semijoin_with_cache=off');
  9. set join_cache_level=1;
  10. set @save_optimizer_switch=@@optimizer_switch;
  11. create table t0 (a int);
  12. insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  13. create table t1(a int, b int);
  14. insert into t1 values (0,0),(1,1),(2,2);
  15. create table t2 as select * from t1;
  16. create table t11(a int, b int);
  17. create table t10 (pk int, a int, primary key(pk));
  18. insert into t10 select a,a from t0;
  19. create table t12 like t10;
  20. insert into t12 select * from t10;
  21. Flattened because of dependency, t10=func(t1)
  22. explain select * from t1 where a in (select pk from t10);
  23. id select_type table type possible_keys key key_len ref rows Extra
  24. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
  25. 1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using index
  26. select * from t1 where a in (select pk from t10);
  27. a b
  28. 0 0
  29. 1 1
  30. 2 2
  31. A confluent case of dependency
  32. explain select * from t1 where a in (select a from t10 where pk=12);
  33. id select_type table type possible_keys key key_len ref rows Extra
  34. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  35. select * from t1 where a in (select a from t10 where pk=12);
  36. a b
  37. explain select * from t1 where a in (select a from t10 where pk=9);
  38. id select_type table type possible_keys key key_len ref rows Extra
  39. 1 PRIMARY t10 const PRIMARY PRIMARY 4 const 1
  40. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
  41. select * from t1 where a in (select a from t10 where pk=9);
  42. a b
  43. An empty table inside
  44. explain select * from t1 where a in (select a from t11);
  45. id select_type table type possible_keys key key_len ref rows Extra
  46. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  47. select * from t1 where a in (select a from t11);
  48. a b
  49. explain select * from t1 where a in (select pk from t10) and b in (select pk from t10);
  50. id select_type table type possible_keys key key_len ref rows Extra
  51. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
  52. 1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using index
  53. 1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.b 1 Using index
  54. select * from t1 where a in (select pk from t10) and b in (select pk from t10);
  55. a b
  56. 0 0
  57. 1 1
  58. 2 2
  59. flattening a nested subquery
  60. explain select * from t1 where a in (select pk from t10 where t10.a in (select pk from t12));
  61. id select_type table type possible_keys key key_len ref rows Extra
  62. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where
  63. 1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 Using where
  64. 1 PRIMARY t12 eq_ref PRIMARY PRIMARY 4 test.t10.a 1 Using index
  65. select * from t1 where a in (select pk from t10 where t10.a in (select pk from t12));
  66. a b
  67. 0 0
  68. 1 1
  69. 2 2
  70. flattening subquery w/ several tables
  71. explain extended select * from t1 where a in (select t10.pk from t10, t12 where t12.pk=t10.a);
  72. id select_type table type possible_keys key key_len ref rows filtered Extra
  73. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
  74. 1 PRIMARY t10 eq_ref PRIMARY PRIMARY 4 test.t1.a 1 100.00 Using where
  75. 1 PRIMARY t12 eq_ref PRIMARY PRIMARY 4 test.t10.a 1 100.00 Using index
  76. Warnings:
  77. 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`))
  78. subqueries within outer joins go into ON expr.
  79. explAin extended
  80. select * from t1 left join (t2 A, t2 B) on ( A.A= t1.A And B.A in (select pk from t10));
  81. id select_type tABle type possiBle_keys key key_len ref rows filtered ExtrA
  82. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
  83. 1 PRIMARY A ALL NULL NULL NULL NULL 3 100.00 Using where
  84. 1 PRIMARY B ALL NULL NULL NULL NULL 3 100.00 Using where
  85. 2 MATERIALIZED t10 index PRIMARY PRIMARY 4 NULL 10 100.00 Using index
  86. Warnings:
  87. 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
  88. t2 should be wrapped into OJ-nest, so we have "t1 LJ (t2 J t10)"
  89. explAin extended
  90. select * from t1 left join t2 on (t2.A= t1.A And t2.A in (select pk from t10));
  91. id select_type tABle type possiBle_keys key key_len ref rows filtered ExtrA
  92. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00
  93. 1 PRIMARY t2 ALL NULL NULL NULL NULL 3 100.00 Using where
  94. 2 MATERIALIZED t10 index PRIMARY PRIMARY 4 NULL 10 100.00 Using index
  95. Warnings:
  96. 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
  97. we shouldn't flatten if we're going to get a join of > MAX_TABLES.
  98. explain select * from
  99. t1 s00, t1 s01, t1 s02, t1 s03, t1 s04,t1 s05,t1 s06,t1 s07,t1 s08,t1 s09,
  100. t1 s10, t1 s11, t1 s12, t1 s13, t1 s14,t1 s15,t1 s16,t1 s17,t1 s18,t1 s19,
  101. t1 s20, t1 s21, t1 s22, t1 s23, t1 s24,t1 s25,t1 s26,t1 s27,t1 s28,t1 s29,
  102. t1 s30, t1 s31, t1 s32, t1 s33, t1 s34,t1 s35,t1 s36,t1 s37,t1 s38,t1 s39,
  103. t1 s40, t1 s41, t1 s42, t1 s43, t1 s44,t1 s45,t1 s46,t1 s47,t1 s48,t1 s49
  104. where
  105. s00.a in (
  106. select m00.a from
  107. t1 m00, t1 m01, t1 m02, t1 m03, t1 m04,t1 m05,t1 m06,t1 m07,t1 m08,t1 m09,
  108. t1 m10, t1 m11, t1 m12, t1 m13, t1 m14,t1 m15,t1 m16,t1 m17,t1 m18,t1 m19
  109. );
  110. id select_type table type possible_keys key key_len ref rows Extra
  111. 1 PRIMARY s00 ALL NULL NULL NULL NULL 3 Using where
  112. 1 PRIMARY s01 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  113. 1 PRIMARY s02 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  114. 1 PRIMARY s03 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  115. 1 PRIMARY s04 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  116. 1 PRIMARY s05 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  117. 1 PRIMARY s06 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  118. 1 PRIMARY s07 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  119. 1 PRIMARY s08 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  120. 1 PRIMARY s09 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  121. 1 PRIMARY s10 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  122. 1 PRIMARY s11 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  123. 1 PRIMARY s12 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  124. 1 PRIMARY s13 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  125. 1 PRIMARY s14 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  126. 1 PRIMARY s15 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  127. 1 PRIMARY s16 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  128. 1 PRIMARY s17 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  129. 1 PRIMARY s18 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  130. 1 PRIMARY s19 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  131. 1 PRIMARY s20 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  132. 1 PRIMARY s21 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  133. 1 PRIMARY s22 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  134. 1 PRIMARY s23 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  135. 1 PRIMARY s24 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  136. 1 PRIMARY s25 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  137. 1 PRIMARY s26 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  138. 1 PRIMARY s27 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  139. 1 PRIMARY s28 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  140. 1 PRIMARY s29 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  141. 1 PRIMARY s30 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  142. 1 PRIMARY s31 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  143. 1 PRIMARY s32 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  144. 1 PRIMARY s33 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  145. 1 PRIMARY s34 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  146. 1 PRIMARY s35 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  147. 1 PRIMARY s36 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  148. 1 PRIMARY s37 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  149. 1 PRIMARY s38 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  150. 1 PRIMARY s39 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  151. 1 PRIMARY s40 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  152. 1 PRIMARY s41 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  153. 1 PRIMARY s42 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  154. 1 PRIMARY s43 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  155. 1 PRIMARY s44 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  156. 1 PRIMARY s45 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  157. 1 PRIMARY s46 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  158. 1 PRIMARY s47 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  159. 1 PRIMARY s48 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  160. 1 PRIMARY s49 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  161. 2 DEPENDENT SUBQUERY m00 ALL NULL NULL NULL NULL 3 Using where
  162. 2 DEPENDENT SUBQUERY m01 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  163. 2 DEPENDENT SUBQUERY m02 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  164. 2 DEPENDENT SUBQUERY m03 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  165. 2 DEPENDENT SUBQUERY m04 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  166. 2 DEPENDENT SUBQUERY m05 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  167. 2 DEPENDENT SUBQUERY m06 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  168. 2 DEPENDENT SUBQUERY m07 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  169. 2 DEPENDENT SUBQUERY m08 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  170. 2 DEPENDENT SUBQUERY m09 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  171. 2 DEPENDENT SUBQUERY m10 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  172. 2 DEPENDENT SUBQUERY m11 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  173. 2 DEPENDENT SUBQUERY m12 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  174. 2 DEPENDENT SUBQUERY m13 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  175. 2 DEPENDENT SUBQUERY m14 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  176. 2 DEPENDENT SUBQUERY m15 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  177. 2 DEPENDENT SUBQUERY m16 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  178. 2 DEPENDENT SUBQUERY m17 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  179. 2 DEPENDENT SUBQUERY m18 ALL NULL NULL NULL NULL 3 Using join buffer (flat, BNL join)
  180. 2 DEPENDENT SUBQUERY m19 ALL NULL NULL NULL NULL 3 Using where; Using join buffer (flat, BNL join)
  181. select * from
  182. t1 left join t2 on (t2.a= t1.a and t2.a in (select pk from t10))
  183. where t1.a < 5;
  184. a b a b
  185. 0 0 0 0
  186. 1 1 1 1
  187. 2 2 2 2
  188. prepare s1 from
  189. ' select * from
  190. t1 left join t2 on (t2.a= t1.a and t2.a in (select pk from t10))
  191. where t1.a < 5';
  192. execute s1;
  193. a b a b
  194. 0 0 0 0
  195. 1 1 1 1
  196. 2 2 2 2
  197. execute s1;
  198. a b a b
  199. 0 0 0 0
  200. 1 1 1 1
  201. 2 2 2 2
  202. insert into t1 select (A.a + 10 * B.a),1 from t0 A, t0 B;
  203. explain extended select * from t1 where a in (select pk from t10 where pk<3);
  204. id select_type table type possible_keys key key_len ref rows filtered Extra
  205. 1 PRIMARY t10 range PRIMARY PRIMARY 4 NULL 4 100.00 Using where; Using index
  206. 1 PRIMARY t1 ALL NULL NULL NULL NULL 103 100.00 Using where; Using join buffer (flat, BNL join)
  207. Warnings:
  208. 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))
  209. drop table t0, t1, t2;
  210. drop table t10, t11, t12;
  211. Bug#37899: Wrongly checked optimization prerequisite caused failed
  212. assertion.
  213. CREATE TABLE t1 (
  214. `pk` int(11),
  215. `varchar_nokey` varchar(5)
  216. );
  217. INSERT INTO t1 VALUES
  218. (1,'qk'),(2,'j'),(3,'aew');
  219. SELECT *
  220. FROM t1
  221. WHERE varchar_nokey IN (
  222. SELECT
  223. varchar_nokey
  224. FROM
  225. t1
  226. ) XOR pk = 30;
  227. pk varchar_nokey
  228. 1 qk
  229. 2 j
  230. 3 aew
  231. drop table t1;
  232. #
  233. # BUG#41842: Semi-join materialization strategy crashes when the upper query has HAVING
  234. #
  235. CREATE TABLE t1 (
  236. pk int(11) NOT NULL AUTO_INCREMENT,
  237. int_nokey int(11) NOT NULL,
  238. time_key time NOT NULL,
  239. datetime_key datetime NOT NULL,
  240. datetime_nokey datetime NOT NULL,
  241. varchar_key varchar(1) NOT NULL,
  242. varchar_nokey varchar(1) NOT NULL,
  243. PRIMARY KEY (pk),
  244. KEY time_key (time_key),
  245. KEY datetime_key (datetime_key),
  246. KEY varchar_key (varchar_key)
  247. );
  248. INSERT INTO t1 VALUES
  249. (1,0, '00:16:10','2008-09-03 14:25:40','2008-09-03 14:25:40','h','h'),
  250. (2,7, '00:00:00','2001-01-13 00:00:00','2001-01-13 00:00:00','',''),
  251. (3,0, '00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','x','x'),
  252. (4,2, '16:29:24','2000-10-16 01:39:08','2000-10-16 01:39:08','w','w'),
  253. (5,1, '09:23:32','0000-00-00 00:00:00','0000-00-00 00:00:00','p','p'),
  254. (6,3, '00:00:00','2007-12-02 00:00:00','2007-12-02 00:00:00','o','o'),
  255. (7,3, '00:00:00','2008-09-11 00:00:00','2008-09-11 00:00:00','',''),
  256. (8,0, '13:59:04','0000-00-00 00:00:00','0000-00-00 00:00:00','s','s'),
  257. (9,7, '09:01:06','0000-00-00 00:00:00','0000-00-00 00:00:00','d','d'),
  258. (10,5,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','n','n'),
  259. (11,0,'21:06:46','0000-00-00 00:00:00','0000-00-00 00:00:00','o','o'),
  260. (12,2,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','',''),
  261. (13,6,'14:45:34','2003-07-28 02:34:08','2003-07-28 02:34:08','w','w'),
  262. (14,1,'15:04:12','0000-00-00 00:00:00','0000-00-00 00:00:00','o','o'),
  263. (15,0,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','x','x'),
  264. (16,0,'15:55:23','2004-03-17 00:32:27','2004-03-17 00:32:27','p','p'),
  265. (17,1,'16:30:00','2004-12-27 19:20:00','2004-12-27 19:20:00','d','d'),
  266. (18,0,'00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00','h','h'),
  267. (19,0,'14:13:26','2008-11-09 05:53:48','2008-11-09 05:53:48','o','o'),
  268. (20,0,'00:00:00','2009-10-11 06:58:04','2009-10-11 06:58:04','k','k');
  269. CREATE TABLE t2 (
  270. pk int(11) NOT NULL AUTO_INCREMENT,
  271. int_nokey int(11) NOT NULL,
  272. time_key time NOT NULL,
  273. datetime_key datetime NOT NULL,
  274. datetime_nokey datetime NOT NULL,
  275. varchar_key varchar(1) NOT NULL,
  276. varchar_nokey varchar(1) NOT NULL,
  277. PRIMARY KEY (pk),
  278. KEY time_key (time_key),
  279. KEY datetime_key (datetime_key),
  280. KEY varchar_key (varchar_key)
  281. );
  282. INSERT INTO t2 VALUES
  283. (10,0,'19:39:13','0000-00-00 00:00:00','0000-00-00 00:00:00','g','g'),
  284. (11,8,'03:43:53','0000-00-00 00:00:00','0000-00-00 00:00:00','b','b');
  285. SELECT OUTR.datetime_nokey AS X FROM t1 AS OUTR
  286. WHERE
  287. OUTR.varchar_nokey IN (SELECT
  288. INNR . varchar_nokey AS Y
  289. FROM t2 AS INNR
  290. WHERE
  291. INNR . datetime_key >= INNR . time_key OR
  292. INNR . pk = INNR . int_nokey
  293. )
  294. AND OUTR . varchar_nokey <= 'w'
  295. HAVING X > '2012-12-12';
  296. X
  297. drop table t1, t2;
  298. #
  299. # Bug#45191: Incorrectly initialized semi-join led to a wrong result.
  300. #
  301. CREATE TABLE STAFF (EMPNUM CHAR(3) NOT NULL,
  302. EMPNAME CHAR(20), GRADE DECIMAL(4), CITY CHAR(15));
  303. CREATE TABLE PROJ (PNUM CHAR(3) NOT NULL,
  304. PNAME CHAR(20), PTYPE CHAR(6),
  305. BUDGET DECIMAL(9),
  306. CITY CHAR(15));
  307. CREATE TABLE WORKS (EMPNUM CHAR(3) NOT NULL,
  308. PNUM CHAR(3) NOT NULL, HOURS DECIMAL(5));
  309. INSERT INTO STAFF VALUES ('E1','Alice',12,'Deale');
  310. INSERT INTO STAFF VALUES ('E2','Betty',10,'Vienna');
  311. INSERT INTO STAFF VALUES ('E3','Carmen',13,'Vienna');
  312. INSERT INTO STAFF VALUES ('E4','Don',12,'Deale');
  313. INSERT INTO STAFF VALUES ('E5','Ed',13,'Akron');
  314. INSERT INTO PROJ VALUES ('P1','MXSS','Design',10000,'Deale');
  315. INSERT INTO PROJ VALUES ('P2','CALM','Code',30000,'Vienna');
  316. INSERT INTO PROJ VALUES ('P3','SDP','Test',30000,'Tampa');
  317. INSERT INTO PROJ VALUES ('P4','SDP','Design',20000,'Deale');
  318. INSERT INTO PROJ VALUES ('P5','IRM','Test',10000,'Vienna');
  319. INSERT INTO PROJ VALUES ('P6','PAYR','Design',50000,'Deale');
  320. INSERT INTO WORKS VALUES ('E1','P1',40);
  321. INSERT INTO WORKS VALUES ('E1','P2',20);
  322. INSERT INTO WORKS VALUES ('E1','P3',80);
  323. INSERT INTO WORKS VALUES ('E1','P4',20);
  324. INSERT INTO WORKS VALUES ('E1','P5',12);
  325. INSERT INTO WORKS VALUES ('E1','P6',12);
  326. INSERT INTO WORKS VALUES ('E2','P1',40);
  327. INSERT INTO WORKS VALUES ('E2','P2',80);
  328. INSERT INTO WORKS VALUES ('E3','P2',20);
  329. INSERT INTO WORKS VALUES ('E4','P2',20);
  330. INSERT INTO WORKS VALUES ('E4','P4',40);
  331. INSERT INTO WORKS VALUES ('E4','P5',80);
  332. set optimizer_switch=@save_optimizer_switch;
  333. set optimizer_switch='materialization=off';
  334. explain SELECT EMPNUM, EMPNAME
  335. FROM STAFF
  336. WHERE EMPNUM IN
  337. (SELECT EMPNUM FROM WORKS
  338. WHERE PNUM IN
  339. (SELECT PNUM FROM PROJ));
  340. id select_type table type possible_keys key key_len ref rows Extra
  341. 1 PRIMARY STAFF ALL NULL NULL NULL NULL 5
  342. 1 PRIMARY PROJ ALL NULL NULL NULL NULL 6
  343. 1 PRIMARY WORKS ALL NULL NULL NULL NULL 12 Using where; FirstMatch(STAFF)
  344. SELECT EMPNUM, EMPNAME
  345. FROM STAFF
  346. WHERE EMPNUM IN
  347. (SELECT EMPNUM FROM WORKS
  348. WHERE PNUM IN
  349. (SELECT PNUM FROM PROJ));
  350. EMPNUM EMPNAME
  351. E1 Alice
  352. E2 Betty
  353. E3 Carmen
  354. E4 Don
  355. set optimizer_switch=@save_optimizer_switch;
  356. drop table STAFF,WORKS,PROJ;
  357. # End of bug#45191
  358. #
  359. # Bug#46550 Azalea returning duplicate results for some IN subqueries
  360. # w/ semijoin=on
  361. #
  362. DROP TABLE IF EXISTS t0, t1, t2;
  363. CREATE TABLE t0 (
  364. int_key int(11) DEFAULT NULL,
  365. varchar_key varchar(1) DEFAULT NULL,
  366. varchar_nokey varchar(1) DEFAULT NULL,
  367. KEY int_key (int_key),
  368. KEY varchar_key (varchar_key,int_key)
  369. );
  370. INSERT INTO t0 VALUES
  371. (1,'m','m'),
  372. (40,'h','h'),
  373. (1,'r','r'),
  374. (1,'h','h'),
  375. (9,'x','x'),
  376. (NULL,'q','q'),
  377. (NULL,'k','k'),
  378. (7,'l','l'),
  379. (182,'k','k'),
  380. (202,'a','a'),
  381. (7,'x','x'),
  382. (6,'j','j'),
  383. (119,'z','z'),
  384. (4,'d','d'),
  385. (5,'h','h'),
  386. (1,'u','u'),
  387. (3,'q','q'),
  388. (7,'a','a'),
  389. (3,'e','e'),
  390. (6,'l','l');
  391. CREATE TABLE t1 (
  392. int_key int(11) DEFAULT NULL,
  393. varchar_key varchar(1) DEFAULT NULL,
  394. varchar_nokey varchar(1) DEFAULT NULL,
  395. KEY int_key (int_key),
  396. KEY varchar_key (varchar_key,int_key)
  397. );
  398. INSERT INTO t1 VALUES (7,NULL,NULL),(4,'x','x');
  399. CREATE TABLE t2 (
  400. int_key int(11) DEFAULT NULL,
  401. varchar_key varchar(1) DEFAULT NULL,
  402. varchar_nokey varchar(1) DEFAULT NULL,
  403. KEY int_key (int_key),
  404. KEY varchar_key (varchar_key,int_key)
  405. );
  406. INSERT INTO t2 VALUES (123,NULL,NULL);
  407. SELECT int_key
  408. FROM t0
  409. WHERE varchar_nokey IN (
  410. SELECT t1 .varchar_key from t1
  411. );
  412. int_key
  413. 9
  414. 7
  415. DROP TABLE t0, t1, t2;
  416. # End of bug#46550
  417. #
  418. # Bug #46744 Crash in optimize_semijoin_nests on empty view
  419. # with limit and procedure.
  420. #
  421. DROP TABLE IF EXISTS t1, t2;
  422. DROP VIEW IF EXISTS v1;
  423. DROP PROCEDURE IF EXISTS p1;
  424. CREATE TABLE t1 ( f1 int );
  425. CREATE TABLE t2 ( f1 int );
  426. insert into t2 values (5), (7);
  427. CREATE OR REPLACE VIEW v1 AS SELECT * FROM t1 LIMIT 2;
  428. create procedure p1()
  429. select COUNT(*)
  430. FROM v1 WHERE f1 IN
  431. (SELECT f1 FROM t2 WHERE f1 = ANY (SELECT f1 FROM v1));
  432. SET SESSION optimizer_switch = 'semijoin=on';
  433. CALL p1();
  434. COUNT(*)
  435. 0
  436. SET SESSION optimizer_switch = 'semijoin=off';
  437. CALL p1();
  438. COUNT(*)
  439. 0
  440. drop table t1, t2;
  441. drop view v1;
  442. drop procedure p1;
  443. set SESSION optimizer_switch=@save_optimizer_switch;
  444. # End of bug#46744
  445. Bug#46797 "Crash in fix_semijoin_strategies_for_picked_join_order
  446. with semijoin=on"
  447. CREATE TABLE t1 (
  448. varchar_key varchar(1) DEFAULT NULL,
  449. KEY varchar_key (varchar_key)
  450. );
  451. CREATE TABLE t2 (
  452. varchar_key varchar(1) DEFAULT NULL,
  453. KEY varchar_key (varchar_key)
  454. );
  455. INSERT INTO t2 VALUES
  456. (NULL),(NULL),(NULL),(NULL),('a'),('a'),('a'),('b'),('b'),('b'),('b'),('c'),
  457. ('c'),('c'),('c'),('c'),('c'),('c'),('d'),('d'),('d'),('d'),('d'),('d'),('e'),
  458. ('e'),('e'),('e'),('e'),('e'),('f'),('f'),('f'),('g'),('g'),('h'),('h'),('h'),
  459. ('h'),('i'),('j'),('j'),('j'),('k'),('k'),('l'),('l'),('m'),('m'),('m'),('m'),
  460. ('n'),('n'),('n'),('o'),('o'),('o'),('p'),('p'),('p'),('q'),('q'),('q'),('r'),
  461. ('r'),('r'),('r'),('s'),('s'),('s'),('s'),('t'),('t'),('t'),('t'),('u'),('u'),
  462. ('u'),('u'),('v'),('v'),('v'),('v'),('w'),('w'),('w'),('w'),('w'),('w'),('x'),
  463. ('x'),('x'),('y'),('y'),('y'),('y'),('z'),('z'),('z'),('z');
  464. CREATE TABLE t3 (
  465. varchar_key varchar(1) DEFAULT NULL,
  466. KEY varchar_key (varchar_key)
  467. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  468. INSERT INTO t3 VALUES
  469. (NULL),('c'),('d'),('e'),('f'),('h'),('j'),('k'),('k'),('m'),('m'),('m'),
  470. ('n'),('o'),('r'),('t'),('t'),('u'),('w'),('y');
  471. SELECT varchar_key FROM t3
  472. WHERE (SELECT varchar_key FROM t3
  473. WHERE (varchar_key,varchar_key)
  474. IN (SELECT t1.varchar_key, t2 .varchar_key
  475. FROM t1 RIGHT JOIN t2 ON t1.varchar_key
  476. )
  477. );
  478. varchar_key
  479. DROP TABLE t1, t2, t3;
  480. #
  481. # Bug#46556 Returning incorrect, empty results for some IN subqueries
  482. # w/semijoin=on
  483. #
  484. CREATE TABLE t0 (
  485. pk INTEGER,
  486. vkey VARCHAR(1),
  487. vnokey VARCHAR(1),
  488. PRIMARY KEY (pk),
  489. KEY vkey(vkey)
  490. );
  491. INSERT INTO t0
  492. VALUES (1,'g','g'), (2,'v','v'), (3,'t','t'), (4,'u','u'), (5,'n','n');
  493. EXPLAIN EXTENDED SELECT vkey FROM t0 WHERE pk IN
  494. (SELECT t1.pk FROM t0 t1 JOIN t0 t2 ON t2.vkey = t1.vnokey);
  495. id select_type table type possible_keys key key_len ref rows filtered Extra
  496. 1 PRIMARY t0 ALL PRIMARY NULL NULL NULL 5 100.00
  497. 1 PRIMARY t1 eq_ref PRIMARY PRIMARY 4 test.t0.pk 1 100.00 Using where
  498. 1 PRIMARY t2 ref vkey vkey 4 test.t1.vnokey 2 100.00 Using index; FirstMatch(t1)
  499. Warnings:
  500. 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`))
  501. SELECT vkey FROM t0 WHERE pk IN
  502. (SELECT t1.pk FROM t0 t1 JOIN t0 t2 ON t2.vkey = t1.vnokey);
  503. vkey
  504. g
  505. v
  506. t
  507. u
  508. n
  509. DROP TABLE t0;
  510. # End of bug#46556
  511. Bug #48073 Subquery on char columns from view crashes Mysql
  512. DROP TABLE IF EXISTS t1, t2;
  513. DROP VIEW IF EXISTS v1;
  514. CREATE TABLE t1 (
  515. city VARCHAR(50) NOT NULL,
  516. country_id SMALLINT UNSIGNED NOT NULL
  517. );
  518. INSERT INTO t1 VALUES
  519. ('Batna',2),
  520. ('Bchar',2),
  521. ('Skikda',2),
  522. ('Tafuna',3),
  523. ('Algeria',2) ;
  524. CREATE TABLE t2 (
  525. country_id SMALLINT UNSIGNED NOT NULL,
  526. country VARCHAR(50) NOT NULL
  527. );
  528. INSERT INTO t2 VALUES
  529. (2,'Algeria'),
  530. (3,'American Samoa') ;
  531. CREATE VIEW v1 AS
  532. SELECT country_id, country
  533. FROM t2
  534. WHERE LEFT(country,1) = "A"
  535. ;
  536. SELECT city, country_id
  537. FROM t1
  538. WHERE city IN (
  539. SELECT country
  540. FROM t2
  541. WHERE LEFT(country, 1) = "A"
  542. );
  543. city country_id
  544. Algeria 2
  545. SELECT city, country_id
  546. FROM t1
  547. WHERE city IN (
  548. SELECT country
  549. FROM v1
  550. );
  551. city country_id
  552. Algeria 2
  553. drop table t1, t2;
  554. drop view v1;
  555. # End of bug#48073
  556. Bug#48834: Procedure with view + subquery + semijoin=on
  557. crashes on second call.
  558. SET SESSION optimizer_switch ='semijoin=on';
  559. CREATE TABLE t1 ( t1field integer, primary key (t1field));
  560. CREATE TABLE t2 ( t2field integer, primary key (t2field));
  561. CREATE VIEW v1 AS
  562. SELECT t1field as v1field
  563. FROM t1 A
  564. WHERE A.t1field IN (SELECT t1field FROM t2 );
  565. CREATE VIEW v2 AS
  566. SELECT t2field as v2field
  567. FROM t2 A
  568. WHERE A.t2field IN (SELECT t2field FROM t2 );
  569. CREATE PROCEDURE p1 ()
  570. BEGIN
  571. SELECT v1field
  572. FROM v1
  573. WHERE v1field IN ( SELECT v2field as vf_inner FROM v2 );
  574. END|
  575. INSERT INTO t1 VALUES (1),(2),(3);
  576. INSERT INTO t2 VALUES (2),(3),(4);
  577. CALL p1;
  578. v1field
  579. 2
  580. 3
  581. CALL p1;
  582. v1field
  583. 2
  584. 3
  585. DROP TABLE t1,t2;
  586. DROP VIEW v1,v2;
  587. DROP PROCEDURE p1;
  588. set SESSION optimizer_switch=@save_optimizer_switch;
  589. # End of BUG#48834
  590. Bug#49097 subquery with view generates wrong result with
  591. non-prepared statement
  592. DROP TABLE IF EXISTS t1, t2;
  593. DROP VIEW IF EXISTS v1;
  594. CREATE TABLE t1 (
  595. city VARCHAR(50) NOT NULL,
  596. country_id SMALLINT UNSIGNED NOT NULL
  597. );
  598. INSERT INTO t1 VALUES
  599. ('Batna',2),
  600. ('Bchar',2),
  601. ('Skikda',2),
  602. ('Tafuna',3),
  603. ('Algeria',2) ;
  604. CREATE TABLE t2 (
  605. country_id SMALLINT UNSIGNED NOT NULL,
  606. country VARCHAR(50) NOT NULL
  607. );
  608. INSERT INTO t2 VALUES
  609. (2,'Algeria'),
  610. (3,'XAmerican Samoa') ;
  611. CREATE VIEW v1 AS
  612. SELECT country_id, country
  613. FROM t2
  614. WHERE LEFT(country,1) = "A"
  615. ;
  616. SELECT city, country_id
  617. FROM t1
  618. WHERE country_id IN (
  619. SELECT country_id
  620. FROM t2
  621. WHERE LEFT(country,1) = "A"
  622. );
  623. city country_id
  624. Batna 2
  625. Bchar 2
  626. Skikda 2
  627. Algeria 2
  628. SELECT city, country_id
  629. FROM t1
  630. WHERE country_id IN (
  631. SELECT country_id
  632. FROM v1
  633. );
  634. city country_id
  635. Batna 2
  636. Bchar 2
  637. Skikda 2
  638. Algeria 2
  639. PREPARE stmt FROM
  640. "
  641. SELECT city, country_id
  642. FROM t1
  643. WHERE country_id IN (
  644. SELECT country_id
  645. FROM v1
  646. );
  647. ";
  648. execute stmt;
  649. city country_id
  650. Batna 2
  651. Bchar 2
  652. Skikda 2
  653. Algeria 2
  654. deallocate prepare stmt;
  655. drop table t1, t2;
  656. drop view v1;
  657. # End of Bug#49097
  658. #
  659. # BUG#38075: Wrong result: rows matching a subquery with outer join not returned
  660. #
  661. DROP TABLE IF EXISTS ot1, it1, it2;
  662. CREATE TABLE it2 (
  663. int_key int(11) NOT NULL,
  664. datetime_key datetime NOT NULL,
  665. KEY int_key (int_key),
  666. KEY datetime_key (datetime_key)
  667. );
  668. INSERT INTO it2 VALUES
  669. (5,'2002-04-10 14:25:30'), (0,'0000-00-00 00:00:00'),
  670. (0,'2006-09-14 04:01:02'), (4,'0000-00-00 00:00:00'),
  671. (8,'0000-00-00 00:00:00'), (5,'0000-00-00 00:00:00'),
  672. (9,'0000-00-00 00:00:00'), (8,'2007-04-01 11:04:17'),
  673. (1,'0000-00-00 00:00:00'), (7,'2009-01-12 00:00:00'),
  674. (0,'2009-06-05 00:00:00'), (3,'2006-02-14 18:06:35'),
  675. (5,'2006-02-21 07:08:16'), (0,'0000-00-00 00:00:00'),
  676. (7,'0000-00-00 00:00:00'), (0,'0000-00-00 00:00:00'),
  677. (0,'2007-02-13 00:00:00'), (1,'0000-00-00 00:00:00'),
  678. (0,'0000-00-00 00:00:00'), (1,'2003-08-11 00:00:00');
  679. CREATE TABLE ot1 (
  680. int_nokey int(11) NOT NULL,
  681. int_key int(11) NOT NULL,
  682. KEY int_key (int_key)
  683. );
  684. INSERT INTO ot1 VALUES
  685. (5,0), (3,0), (0,2), (3,0), (1,3), (0,0), (1,7), (7,0), (1,7), (0,7),
  686. (0,9), (8,2), (4,4), (9,3), (0,9), (2,5), (0,5), (8,0), (5,8), (1,5);
  687. CREATE TABLE it1 (
  688. int_nokey int(11) NOT NULL,
  689. int_key int(11) NOT NULL,
  690. KEY int_key (int_key)
  691. );
  692. INSERT INTO it1 VALUES
  693. (9,5), (0,4);
  694. SELECT int_key FROM ot1
  695. WHERE int_nokey IN (SELECT it2.int_key
  696. FROM it1 LEFT JOIN it2 ON it2.datetime_key);
  697. int_key
  698. 0
  699. 0
  700. 0
  701. 0
  702. 0
  703. 0
  704. 2
  705. 2
  706. 3
  707. 5
  708. 5
  709. 7
  710. 7
  711. 7
  712. 8
  713. 9
  714. 9
  715. EXPLAIN
  716. SELECT int_key FROM ot1
  717. WHERE int_nokey IN (SELECT it2.int_key
  718. FROM it1 LEFT JOIN it2 ON it2.datetime_key);
  719. id select_type table type possible_keys key key_len ref rows Extra
  720. 1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 11
  721. 1 PRIMARY ot1 ALL NULL NULL NULL NULL 20 Using where; Using join buffer (flat, BNL join)
  722. 2 MATERIALIZED it1 index NULL int_key 4 NULL 2 Using index
  723. 2 MATERIALIZED it2 ALL int_key,datetime_key NULL NULL NULL 20 Using where
  724. DROP TABLE ot1, it1, it2;
  725. # End of BUG#38075
  726. #
  727. # BUG#31480: Incorrect result for nested subquery when executed via semi join
  728. #
  729. create table t1 (a int not null, b int not null);
  730. create table t2 (c int not null, d int not null);
  731. create table t3 (e int not null);
  732. insert into t1 values (1,10);
  733. insert into t1 values (2,10);
  734. insert into t1 values (1,20);
  735. insert into t1 values (2,20);
  736. insert into t1 values (3,20);
  737. insert into t1 values (2,30);
  738. insert into t1 values (4,40);
  739. insert into t2 values (2,10);
  740. insert into t2 values (2,20);
  741. insert into t2 values (4,10);
  742. insert into t2 values (5,10);
  743. insert into t2 values (3,20);
  744. insert into t2 values (2,40);
  745. insert into t3 values (10);
  746. insert into t3 values (30);
  747. insert into t3 values (10);
  748. insert into t3 values (20);
  749. explain extended
  750. select a from t1
  751. where a in (select c from t2 where d >= some(select e from t3 where b=e));
  752. id select_type table type possible_keys key key_len ref rows filtered Extra
  753. 1 PRIMARY t2 ALL NULL NULL NULL NULL 6 100.00 Start temporary
  754. 1 PRIMARY t1 ALL NULL NULL NULL NULL 7 100.00 Using where; End temporary; Using join buffer (flat, BNL join)
  755. 3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 4 100.00 Using where
  756. Warnings:
  757. Note 1276 Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1
  758. 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`)))))))
  759. show warnings;
  760. Level Code Message
  761. Note 1276 Field or reference 'test.t1.b' of SELECT #3 was resolved in SELECT #1
  762. 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`)))))))
  763. select a from t1
  764. where a in (select c from t2 where d >= some(select e from t3 where b=e));
  765. a
  766. 2
  767. 2
  768. 3
  769. 2
  770. drop table t1, t2, t3;
  771. #
  772. # Bug#48213 Materialized subselect crashes if using GEOMETRY type
  773. #
  774. CREATE TABLE t1 (
  775. pk int,
  776. a varchar(1),
  777. b varchar(4),
  778. c tinyblob,
  779. d blob,
  780. e mediumblob,
  781. f longblob,
  782. g tinytext,
  783. h text,
  784. i mediumtext,
  785. j longtext,
  786. k geometry,
  787. PRIMARY KEY (pk)
  788. );
  789. 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))'));
  790. CREATE TABLE t2 LIKE t1;
  791. 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))'));
  792. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (a, b) IN (SELECT a, b FROM t2 WHERE pk > 0);
  793. id select_type table type possible_keys key key_len ref rows filtered Extra
  794. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  795. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 11 func,func 1 100.00
  796. 2 MATERIALIZED t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Rowid-ordered scan
  797. Warnings:
  798. Note 1003 select `test`.`t1`.`pk` AS `pk` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`pk` > 0))
  799. SELECT pk FROM t1 WHERE (a, b) IN (SELECT a, b FROM t2 WHERE pk > 0);
  800. pk
  801. 2
  802. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, c) IN (SELECT b, c FROM t2 WHERE pk > 0);
  803. id select_type table type possible_keys key key_len ref rows filtered Extra
  804. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  805. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  806. Warnings:
  807. 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))
  808. SELECT pk FROM t1 WHERE (b, c) IN (SELECT b, c FROM t2 WHERE pk > 0);
  809. pk
  810. 1
  811. 2
  812. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, d) IN (SELECT b, d FROM t2 WHERE pk > 0);
  813. id select_type table type possible_keys key key_len ref rows filtered Extra
  814. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  815. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  816. Warnings:
  817. 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))
  818. SELECT pk FROM t1 WHERE (b, d) IN (SELECT b, d FROM t2 WHERE pk > 0);
  819. pk
  820. 2
  821. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, e) IN (SELECT b, e FROM t2 WHERE pk > 0);
  822. id select_type table type possible_keys key key_len ref rows filtered Extra
  823. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  824. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  825. Warnings:
  826. 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))
  827. SELECT pk FROM t1 WHERE (b, e) IN (SELECT b, e FROM t2 WHERE pk > 0);
  828. pk
  829. 1
  830. 2
  831. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, f) IN (SELECT b, f FROM t2 WHERE pk > 0);
  832. id select_type table type possible_keys key key_len ref rows filtered Extra
  833. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  834. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  835. Warnings:
  836. 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))
  837. SELECT pk FROM t1 WHERE (b, f) IN (SELECT b, f FROM t2 WHERE pk > 0);
  838. pk
  839. 1
  840. 2
  841. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, g) IN (SELECT b, g FROM t2 WHERE pk > 0);
  842. id select_type table type possible_keys key key_len ref rows filtered Extra
  843. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  844. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  845. Warnings:
  846. 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))
  847. SELECT pk FROM t1 WHERE (b, g) IN (SELECT b, g FROM t2 WHERE pk > 0);
  848. pk
  849. 1
  850. 2
  851. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, h) IN (SELECT b, h FROM t2 WHERE pk > 0);
  852. id select_type table type possible_keys key key_len ref rows filtered Extra
  853. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  854. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  855. Warnings:
  856. 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))
  857. SELECT pk FROM t1 WHERE (b, h) IN (SELECT b, h FROM t2 WHERE pk > 0);
  858. pk
  859. 1
  860. 2
  861. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, i) IN (SELECT b, i FROM t2 WHERE pk > 0);
  862. id select_type table type possible_keys key key_len ref rows filtered Extra
  863. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  864. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  865. Warnings:
  866. 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))
  867. SELECT pk FROM t1 WHERE (b, i) IN (SELECT b, i FROM t2 WHERE pk > 0);
  868. pk
  869. 1
  870. 2
  871. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, j) IN (SELECT b, j FROM t2 WHERE pk > 0);
  872. id select_type table type possible_keys key key_len ref rows filtered Extra
  873. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  874. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  875. Warnings:
  876. 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))
  877. SELECT pk FROM t1 WHERE (b, j) IN (SELECT b, j FROM t2 WHERE pk > 0);
  878. pk
  879. 1
  880. 2
  881. EXPLAIN EXTENDED SELECT pk FROM t1 WHERE (b, k) IN (SELECT b, k FROM t2 WHERE pk > 0);
  882. id select_type table type possible_keys key key_len ref rows filtered Extra
  883. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
  884. 1 PRIMARY t2 range PRIMARY PRIMARY 4 NULL 2 100.00 Using index condition; Using where; Rowid-ordered scan; FirstMatch(t1)
  885. Warnings:
  886. 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))
  887. SELECT pk FROM t1 WHERE (b, k) IN (SELECT b, k FROM t2 WHERE pk > 0);
  888. pk
  889. 1
  890. 2
  891. DROP TABLE t1, t2;
  892. # End of Bug#48213
  893. #
  894. # Bug#49198 Wrong result for second call of procedure
  895. # with view in subselect.
  896. #
  897. CREATE TABLE t1 (t1field integer, primary key (t1field));
  898. CREATE TABLE t2 (t2field integer, primary key (t2field));
  899. CREATE TABLE t3 (t3field integer, primary key (t3field));
  900. CREATE VIEW v2 AS SELECT * FROM t2;
  901. CREATE VIEW v3 AS SELECT * FROM t3;
  902. INSERT INTO t1 VALUES(1),(2);
  903. INSERT INTO t2 VALUES(1),(2);
  904. INSERT INTO t3 VALUES(1),(2);
  905. PREPARE stmt FROM
  906. "
  907. SELECT t1field
  908. FROM t1
  909. WHERE t1field IN (SELECT * FROM v2);
  910. ";
  911. EXECUTE stmt;
  912. t1field
  913. 1
  914. 2
  915. EXECUTE stmt;
  916. t1field
  917. 1
  918. 2
  919. PREPARE stmt FROM
  920. "
  921. EXPLAIN
  922. SELECT t1field
  923. FROM t1
  924. WHERE t1field IN (SELECT * FROM v2)
  925. AND t1field IN (SELECT * FROM v3)
  926. ";
  927. EXECUTE stmt;
  928. id select_type table type possible_keys key key_len ref rows Extra
  929. 1 PRIMARY t1 index PRIMARY PRIMARY 4 NULL 2 Using index
  930. 1 PRIMARY t2 eq_ref PRIMARY PRIMARY 4 test.t1.t1field 1 Using index
  931. 1 PRIMARY t3 eq_ref PRIMARY PRIMARY 4 test.t1.t1field 1 Using index
  932. EXECUTE stmt;
  933. id select_type table type possible_keys key key_len ref rows Extra
  934. 1 SIMPLE t1 index PRIMARY PRIMARY 4 NULL 2 Using index
  935. 1 SIMPLE t2 eq_ref PRIMARY PRIMARY 4 test.t1.t1field 1 Using index
  936. 1 SIMPLE t3 eq_ref PRIMARY PRIMARY 4 test.t1.t1field 1 Using index
  937. DROP TABLE t1, t2, t3;
  938. DROP VIEW v2, v3;
  939. # End of Bug#49198
  940. #
  941. # Bug#45174: Incorrectly applied equality propagation caused wrong
  942. # result on a query with a materialized semi-join.
  943. #
  944. CREATE TABLE `t1` (
  945. `pk` int(11) NOT NULL AUTO_INCREMENT,
  946. `varchar_key` varchar(1) NOT NULL,
  947. `varchar_nokey` varchar(1) NOT NULL,
  948. PRIMARY KEY (`pk`),
  949. KEY `varchar_key` (`varchar_key`)
  950. );
  951. 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');
  952. CREATE TABLE `t2` (
  953. `varchar_nokey` varchar(1) NOT NULL
  954. );
  955. INSERT INTO `t2` VALUES ('v'),('u'),('n'),('l'),('h'),('u'),('n'),('j'),('k'),('e'),('i'),('u'),('n'),('b'),('x'),(''),('q'),('u');
  956. EXPLAIN EXTENDED SELECT varchar_nokey
  957. FROM t2
  958. WHERE ( `varchar_nokey` , `varchar_nokey` ) IN (
  959. SELECT `varchar_key` , `varchar_nokey`
  960. FROM t1
  961. WHERE `varchar_nokey` < 'n' XOR `pk` ) ;
  962. id select_type table type possible_keys key key_len ref rows filtered Extra
  963. 1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 15 100.00
  964. 1 PRIMARY t2 ALL NULL NULL NULL NULL 18 100.00 Using where; Using join buffer (flat, BNL join)
  965. 2 MATERIALIZED t1 ALL varchar_key NULL NULL NULL 15 100.00 Using where
  966. Warnings:
  967. 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`.`t2`.`varchar_nokey` = `test`.`t1`.`varchar_key`) and ((`test`.`t1`.`varchar_key` < 'n') xor `test`.`t1`.`pk`))
  968. SELECT varchar_nokey
  969. FROM t2
  970. WHERE ( `varchar_nokey` , `varchar_nokey` ) IN (
  971. SELECT `varchar_key` , `varchar_nokey`
  972. FROM t1
  973. WHERE `varchar_nokey` < 'n' XOR `pk` ) ;
  974. varchar_nokey
  975. DROP TABLE t1, t2;
  976. # End of the test for bug#45174.
  977. #
  978. # BUG#43768: Prepared query with nested subqueries core dumps on second execution
  979. #
  980. create table t1 (
  981. id int(11) unsigned not null primary key auto_increment,
  982. partner_id varchar(35) not null,
  983. t1_status_id int(10) unsigned
  984. );
  985. insert into t1 values ("1", "partner1", "10"), ("2", "partner2", "10"),
  986. ("3", "partner3", "10"), ("4", "partner4", "10");
  987. create table t2 (
  988. id int(11) unsigned not null default '0',
  989. t1_line_id int(11) unsigned not null default '0',
  990. article_id varchar(20),
  991. sequence int(11) not null default '0',
  992. primary key (id,t1_line_id)
  993. );
  994. insert into t2 values ("1", "1", "sup", "0"), ("2", "1", "sup", "1"),
  995. ("2", "2", "sup", "2"), ("2", "3", "sup", "3"),
  996. ("2", "4", "imp", "4"), ("3", "1", "sup", "0"),
  997. ("4", "1", "sup", "0");
  998. create table t3 (
  999. id int(11) not null default '0',
  1000. preceeding_id int(11) not null default '0',
  1001. primary key (id,preceeding_id)
  1002. );
  1003. create table t4 (
  1004. user_id varchar(50) not null,
  1005. article_id varchar(20) not null,
  1006. primary key (user_id,article_id)
  1007. );
  1008. insert into t4 values("nicke", "imp");
  1009. prepare stmt from
  1010. 'select t1.partner_id
  1011. from t1
  1012. where
  1013. t1.id in (
  1014. select pl_inner.id
  1015. from t2 as pl_inner
  1016. where pl_inner.article_id in (
  1017. select t4.article_id from t4
  1018. where t4.user_id = \'nicke\'
  1019. )
  1020. )';
  1021. execute stmt;
  1022. partner_id
  1023. partner2
  1024. execute stmt;
  1025. partner_id
  1026. partner2
  1027. drop table t1,t2,t3,t4;
  1028. #
  1029. # Bug#48623 Multiple subqueries are optimized incorrectly
  1030. #
  1031. CREATE TABLE t1(val VARCHAR(10));
  1032. CREATE TABLE t2(val VARCHAR(10));
  1033. CREATE TABLE t3(val VARCHAR(10));
  1034. INSERT INTO t1 VALUES('aaa'), ('bbb'), ('eee'), ('mmm'), ('ppp');
  1035. INSERT INTO t2 VALUES('aaa'), ('aaa'), ('bbb'), ('eee'), ('mmm'), ('ppp');
  1036. INSERT INTO t3 VALUES('aaa'), ('bbb'), ('eee'), ('mmm'), ('ppp');
  1037. EXPLAIN
  1038. SELECT *
  1039. FROM t1
  1040. WHERE t1.val IN (SELECT t2.val FROM t2
  1041. WHERE t2.val LIKE 'a%' OR t2.val LIKE 'e%')
  1042. AND t1.val IN (SELECT t3.val FROM t3
  1043. WHERE t3.val LIKE 'a%' OR t3.val LIKE 'e%');
  1044. id select_type table type possible_keys key key_len ref rows Extra
  1045. 1 PRIMARY t1 ALL NULL NULL NULL NULL 5
  1046. 1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 13 func 1
  1047. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 13 func 1
  1048. 3 MATERIALIZED t3 ALL NULL NULL NULL NULL 5 Using where
  1049. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 6 Using where
  1050. SELECT *
  1051. FROM t1
  1052. WHERE t1.val IN (SELECT t2.val FROM t2
  1053. WHERE t2.val LIKE 'a%' OR t2.val LIKE 'e%')
  1054. AND t1.val IN (SELECT t3.val FROM t3
  1055. WHERE t3.val LIKE 'a%' OR t3.val LIKE 'e%');
  1056. val
  1057. aaa
  1058. eee
  1059. DROP TABLE t1;
  1060. DROP TABLE t2;
  1061. DROP TABLE t3;
  1062. # End of Bug#48623
  1063. #
  1064. # LPBUG#602574: RQG: sql_select.cc:5385: bool greedy_search(JOIN*, table_map, uint,
  1065. # uint): Assertion `join->best_read <
  1066. #
  1067. set @save_optimizer_switch=@@optimizer_switch;
  1068. set optimizer_switch='materialization=off';
  1069. CREATE TABLE t1 (
  1070. varchar_key varchar(1) DEFAULT NULL,
  1071. KEY varchar_key (varchar_key)
  1072. );
  1073. CREATE TABLE t2 (
  1074. varchar_key varchar(1) DEFAULT NULL,
  1075. KEY varchar_key (varchar_key)
  1076. );
  1077. INSERT INTO t2 VALUES
  1078. (NULL),(NULL),(NULL),(NULL),('a'),('a'),('a'),('b'),('b'),('b'),('b'),('c'),
  1079. ('c'),('c'),('c'),('c'),('c'),('c'),('d'),('d'),('d'),('d'),('d'),('d'),('e'),
  1080. ('e'),('e'),('e'),('e'),('e'),('f'),('f'),('f'),('g'),('g'),('h'),('h'),('h'),
  1081. ('h'),('i'),('j'),('j'),('j'),('k'),('k'),('l'),('l'),('m'),('m'),('m'),('m'),
  1082. ('n'),('n'),('n'),('o'),('o'),('o'),('p'),('p'),('p'),('q'),('q'),('q'),('r'),
  1083. ('r'),('r'),('r'),('s'),('s'),('s'),('s'),('t'),('t'),('t'),('t'),('u'),('u'),
  1084. ('u'),('u'),('v'),('v'),('v'),('v'),('w'),('w'),('w'),('w'),('w'),('w'),('x'),
  1085. ('x'),('x'),('y'),('y'),('y'),('y'),('z'),('z'),('z'),('z');
  1086. CREATE TABLE t3 (
  1087. varchar_key varchar(1) DEFAULT NULL,
  1088. KEY varchar_key (varchar_key)
  1089. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  1090. INSERT INTO t3 VALUES
  1091. (NULL),('c'),('d'),('e'),('f'),('h'),('j'),('k'),('k'),('m'),('m'),('m'),
  1092. ('n'),('o'),('r'),('t'),('t'),('u'),('w'),('y');
  1093. SELECT varchar_key FROM t3
  1094. WHERE (SELECT varchar_key FROM t3
  1095. WHERE (varchar_key,varchar_key)
  1096. IN (SELECT t1.varchar_key, t2 .varchar_key
  1097. FROM t1 RIGHT JOIN t2 ON t1.varchar_key
  1098. )
  1099. );
  1100. varchar_key
  1101. set optimizer_switch=@save_optimizer_switch;
  1102. DROP TABLE t1, t2, t3;
  1103. #
  1104. # Bug#46692 "Crash occurring on queries with nested FROM subqueries
  1105. # using materialization."
  1106. #
  1107. CREATE TABLE t1 (
  1108. pk INTEGER PRIMARY KEY,
  1109. int_key INTEGER,
  1110. KEY int_key(int_key)
  1111. );
  1112. INSERT INTO t1 VALUES (10,186),(11,NULL),(12,2),(13,3),(14,0),(15,133),(16,1);
  1113. CREATE TABLE t2 (
  1114. pk INTEGER PRIMARY KEY,
  1115. int_key INTEGER,
  1116. KEY int_key(int_key)
  1117. );
  1118. INSERT INTO t2 VALUES (1,7),(2,2);
  1119. SELECT * FROM t1 WHERE (140, 4) IN
  1120. (SELECT t2.int_key, t2 .pk FROM t2 STRAIGHT_JOIN t1 ON t2.int_key);
  1121. pk int_key
  1122. DROP TABLE t1, t2;
  1123. #
  1124. # Bug#42353 "SELECT ... WHERE oe IN (SELECT w/ LEFT JOIN) query
  1125. # causes crash."
  1126. #
  1127. CREATE TABLE t1 (
  1128. pk INTEGER PRIMARY KEY,
  1129. int_nokey INTEGER,
  1130. int_key INTEGER,
  1131. date_key DATE,
  1132. datetime_nokey DATETIME,
  1133. varchar_nokey VARCHAR(1)
  1134. );
  1135. CREATE TABLE t2 (
  1136. date_nokey DATE
  1137. );
  1138. CREATE TABLE t3 (
  1139. pk INTEGER PRIMARY KEY,
  1140. int_nokey INTEGER,
  1141. date_key date,
  1142. varchar_key VARCHAR(1),
  1143. varchar_nokey VARCHAR(1),
  1144. KEY date_key (date_key)
  1145. );
  1146. SELECT date_key FROM t1
  1147. WHERE (int_key, int_nokey)
  1148. IN (SELECT t3.int_nokey, t3.pk
  1149. FROM t2 LEFT JOIN t3 ON (t2.date_nokey < t3.date_key)
  1150. WHERE t3.varchar_key <= t3.varchar_nokey OR t3.int_nokey <= t3.pk
  1151. )
  1152. AND (varchar_nokey <> 'f' OR NOT int_key < 7);
  1153. date_key
  1154. #
  1155. # Bug#45933 "Crash in optimize_semijoin_nests on JOIN in subquery
  1156. # + AND in outer query".
  1157. #
  1158. INSERT INTO t1 VALUES (10,7,5,'2009-06-16','2002-04-10 14:25:30','w'),
  1159. (11,7,0,'0000-00-00','0000-00-00 00:00:00','s'),
  1160. (12,4,0,'2003-07-14','2006-09-14 04:01:02','y'),
  1161. (13,0,4,'2002-07-25','0000-00-00 00:00:00','c'),
  1162. (14,1,8,'2007-07-03','0000-00-00 00:00:00','q'),
  1163. (15,6,5,'2001-11-12','0000-00-00 00:00:00',''),
  1164. (16,2,9,'0000-00-00','0000-00-00 00:00:00','j'),
  1165. (29,9,1,'0000-00-00','2003-08-11 00:00:00','m');
  1166. INSERT INTO t3 VALUES (1,9,'0000-00-00','b','b'),
  1167. (2,2,'2002-09-17','h','h');
  1168. SELECT t1.varchar_nokey FROM t1 JOIN t3 ON t1.datetime_nokey
  1169. WHERE t1.varchar_nokey
  1170. IN (SELECT varchar_nokey FROM t1
  1171. WHERE (pk)
  1172. IN (SELECT t3.int_nokey
  1173. FROM t3 LEFT JOIN t1 ON t1.varchar_nokey
  1174. WHERE t3.date_key BETWEEN '2008-06-07' AND '2006-06-26'
  1175. )
  1176. );
  1177. varchar_nokey
  1178. DROP TABLE t1, t2, t3;
  1179. #
  1180. # Bug#45219 "Crash on SELECT DISTINCT query containing a
  1181. # LEFT JOIN in subquery"
  1182. #
  1183. CREATE TABLE t1 (
  1184. pk INTEGER NOT NULL,
  1185. int_nokey INTEGER NOT NULL,
  1186. datetime_key DATETIME NOT NULL,
  1187. varchar_key VARCHAR(1) NOT NULL,
  1188. PRIMARY KEY (pk),
  1189. KEY datetime_key (datetime_key),
  1190. KEY varchar_key (varchar_key)
  1191. );
  1192. INSERT INTO t1 VALUES
  1193. (1,9,'0000-00-00 00:00:00','p'),(2,0,'2002-02-09 07:38:13','v'),
  1194. (3,8,'2001-05-03 12:08:14','t'),(4,3,'0000-00-00 00:00:00','u'),
  1195. (5,7,'2009-07-28 03:43:30','n'),(6,0,'2009-08-04 00:00:00','l'),
  1196. (7,1,'0000-00-00 00:00:00','h'),(8,9,'0000-00-00 00:00:00','u'),
  1197. (9,0,'2005-08-02 17:16:54','n'),(10,9,'2002-12-21 00:00:00','j'),
  1198. (11,0,'2005-08-15 12:37:35','k'),(12,5,'0000-00-00 00:00:00','e'),
  1199. (13,0,'2006-03-10 00:00:00','i'),(14,8,'2005-05-16 11:02:36','u'),
  1200. (15,8,'2008-11-02 00:00:00','n'),(16,5,'2006-03-15 00:00:00','b'),
  1201. (17,1,'0000-00-00 00:00:00','x'),(18,7,'0000-00-00 00:00:00',''),
  1202. (19,0,'2008-12-17 20:15:40','q'),(20,9,'0000-00-00 00:00:00','u');
  1203. CREATE TABLE t2 LIKE t1;
  1204. INSERT INTO t2 VALUES
  1205. (10,0,'2006-07-07 07:26:28','q'),(11,5,'2002-09-23 00:00:00','m'),
  1206. (12,7,'0000-00-00 00:00:00','j'),(13,1,'2006-06-07 00:00:00','z'),
  1207. (14,8,'2000-09-16 12:15:34','a'),(15,2,'2007-08-05 15:47:52',''),
  1208. (16,1,'0000-00-00 00:00:00','e'),(17,8,'2005-12-02 19:34:26','t'),
  1209. (18,5,'0000-00-00 00:00:00','q'),(19,4,'0000-00-00 00:00:00','b'),
  1210. (20,5,'2007-12-28 00:00:00','w'),(21,3,'2004-08-02 11:48:43','m'),
  1211. (22,0,'0000-00-00 00:00:00','x'),(23,8,'2004-04-19 12:18:43',''),
  1212. (24,0,'2009-04-27 00:00:00','w'),(25,4,'2006-10-20 14:52:15','x'),
  1213. (26,0,'0000-00-00 00:00:00','e'),(27,0,'2002-03-22 11:48:37','e'),
  1214. (28,2,'0000-00-00 00:00:00','p'),(29,0,'2001-01-04 03:55:07','x');
  1215. CREATE TABLE t3 LIKE t1;
  1216. INSERT INTO t3 VALUES
  1217. (10,8,'2007-08-19 08:08:38','i'),(11,0,'2000-05-21 03:51:51','');
  1218. SELECT DISTINCT datetime_key FROM t1
  1219. WHERE (int_nokey, pk)
  1220. IN (SELECT t3.pk, t3.pk FROM t2 LEFT JOIN t3 ON t3.varchar_key)
  1221. AND pk = 9;
  1222. datetime_key
  1223. DROP TABLE t1, t2, t3;
  1224. #
  1225. # BUG#53060: LooseScan semijoin strategy does not return all rows
  1226. #
  1227. set @save_optimizer_switch=@@optimizer_switch;
  1228. set optimizer_switch='semijoin=on,materialization=off';
  1229. set optimizer_switch='firstmatch=off,loosescan=on';
  1230. CREATE TABLE t1 (i INTEGER);
  1231. INSERT INTO t1 VALUES (1), (2), (3), (4), (5);
  1232. CREATE TABLE t2 (i INTEGER, j INTEGER, KEY k(i, j));
  1233. INSERT INTO t2 VALUES (1, 0), (1, 1), (2, 0), (2, 1);
  1234. EXPLAIN
  1235. SELECT * FROM t1 WHERE (i) IN (SELECT i FROM t2 where j > 0);
  1236. id select_type table type possible_keys key key_len ref rows Extra
  1237. 1 PRIMARY t2 index k k 10 NULL 4 Using where; Using index; LooseScan
  1238. 1 PRIMARY t1 ALL NULL NULL NULL NULL 5 Using where; Using join buffer (flat, BNL join)
  1239. SELECT * FROM t1 WHERE (i) IN (SELECT i FROM t2 where j > 0);
  1240. i
  1241. 1
  1242. 2
  1243. DROP TABLE t1, t2;
  1244. set optimizer_switch=@save_optimizer_switch;
  1245. #
  1246. # BUG#49453: re-execution of prepared statement with view
  1247. # and semijoin crashes
  1248. #
  1249. CREATE TABLE t1 (city VARCHAR(50), country_id INT);
  1250. CREATE TABLE t2 (country_id INT, country VARCHAR(50));
  1251. INSERT INTO t1 VALUES
  1252. ('Batna',2),('Bchar',2),('Skikda',2),('Tafuna',3),('Algeria',2) ;
  1253. INSERT INTO t2 VALUES (2,'Algeria'),(2,'AlgeriaDup'),(3,'XAmerican Samoa');
  1254. CREATE VIEW v1 AS
  1255. SELECT country_id as vf_country_id
  1256. FROM t2
  1257. WHERE LEFT(country,1) = "A";
  1258. PREPARE stmt FROM "
  1259. SELECT city, country_id
  1260. FROM t1
  1261. WHERE country_id IN (SELECT vf_country_id FROM v1);
  1262. ";
  1263. EXECUTE stmt;
  1264. city country_id
  1265. Batna 2
  1266. Bchar 2
  1267. Skikda 2
  1268. Algeria 2
  1269. EXECUTE stmt;
  1270. city country_id
  1271. Batna 2
  1272. Bchar 2
  1273. Skikda 2
  1274. Algeria 2
  1275. DROP TABLE t1,t2;
  1276. DROP VIEW v1;
  1277. #
  1278. # Bug#54437 Extra rows with LEFT JOIN + semijoin
  1279. #
  1280. create table t1 (a int);
  1281. create table t2 (a int);
  1282. create table t3 (a int);
  1283. insert into t1 values(1),(1);
  1284. insert into t2 values(1),(1),(1),(1);
  1285. insert into t3 values(2),(2);
  1286. set @save_optimizer_switch=@@optimizer_switch;
  1287. set optimizer_switch='materialization=off';
  1288. set optimizer_switch='semijoin=off';
  1289. explain
  1290. select * from t1 where t1.a in (select t2.a from t2 left join t3 on t2.a=t3.a);
  1291. id select_type table type possible_keys key key_len ref rows Extra
  1292. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
  1293. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 4 Using where
  1294. 2 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 Using where
  1295. select * from t1 where t1.a in (select t2.a from t2 left join t3 on t2.a=t3.a);
  1296. a
  1297. 1
  1298. 1
  1299. set optimizer_switch='semijoin=on';
  1300. explain
  1301. select * from t1 where t1.a in (select t2.a from t2 left join t3 on t2.a=t3.a);
  1302. id select_type table type possible_keys key key_len ref rows Extra
  1303. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2
  1304. 1 PRIMARY t2 ALL NULL NULL NULL NULL 4 Using where; Start temporary
  1305. 1 PRIMARY t3 ALL NULL NULL NULL NULL 2 Using where; End temporary
  1306. select * from t1 where t1.a in (select t2.a from t2 left join t3 on t2.a=t3.a);
  1307. a
  1308. 1
  1309. 1
  1310. set optimizer_switch=@save_optimizer_switch;
  1311. drop table t1,t2,t3;
  1312. #
  1313. # Bug#55955: crash in MEMORY engine with IN(LEFT JOIN (JOIN))
  1314. #
  1315. CREATE TABLE t1 (a INT);
  1316. CREATE TABLE t2 (a INT);
  1317. CREATE TABLE t3 (a INT);
  1318. INSERT INTO t1 VALUES(1),(1);
  1319. INSERT INTO t2 VALUES(1),(1);
  1320. INSERT INTO t3 VALUES(2),(2);
  1321. set @save_optimizer_switch=@@optimizer_switch;
  1322. set optimizer_switch='semijoin=off,materialization=off';
  1323. EXPLAIN
  1324. SELECT * FROM t1
  1325. WHERE t1.a IN (SELECT t2.a
  1326. FROM t2 LEFT JOIN (t2 AS t2inner, t3) ON t2.a=t3.a);
  1327. id select_type table type possible_keys key key_len ref rows Extra
  1328. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
  1329. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where
  1330. 2 DEPENDENT SUBQUERY t2inner ALL NULL NULL NULL NULL 2
  1331. 2 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 Using where
  1332. SELECT * FROM t1
  1333. WHERE t1.a IN (SELECT t2.a
  1334. FROM t2 LEFT JOIN (t2 AS t2inner, t3) ON t2.a=t3.a);
  1335. a
  1336. 1
  1337. 1
  1338. set optimizer_switch='semijoin=off,materialization=on';
  1339. EXPLAIN
  1340. SELECT * FROM t1
  1341. WHERE t1.a IN (SELECT t2.a
  1342. FROM t2 LEFT JOIN (t2 AS t2inner, t3) ON t2.a=t3.a);
  1343. id select_type table type possible_keys key key_len ref rows Extra
  1344. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
  1345. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 2
  1346. 2 MATERIALIZED t2inner ALL NULL NULL NULL NULL 2
  1347. 2 MATERIALIZED t3 ALL NULL NULL NULL NULL 2 Using where
  1348. SELECT * FROM t1
  1349. WHERE t1.a IN (SELECT t2.a
  1350. FROM t2 LEFT JOIN (t2 AS t2inner, t3) ON t2.a=t3.a);
  1351. a
  1352. 1
  1353. 1
  1354. set optimizer_switch='semijoin=on,materialization=off';
  1355. EXPLAIN
  1356. SELECT * FROM t1
  1357. WHERE t1.a IN (SELECT t2.a
  1358. FROM t2 LEFT JOIN (t2 AS t2inner, t3) ON t2.a=t3.a);
  1359. id select_type table type possible_keys key key_len ref rows Extra
  1360. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2
  1361. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Start temporary
  1362. 1 PRIMARY t2inner ALL NULL NULL NULL NULL 2
  1363. 1 PRIMARY t3 ALL NULL NULL NULL NULL 2 Using where; End temporary
  1364. SELECT * FROM t1
  1365. WHERE t1.a IN (SELECT t2.a
  1366. FROM t2 LEFT JOIN (t2 AS t2inner, t3) ON t2.a=t3.a);
  1367. a
  1368. 1
  1369. 1
  1370. set optimizer_switch=@save_optimizer_switch;
  1371. DROP TABLE t1,t2,t3;
  1372. #
  1373. # BUG#52329 - Wrong result: subquery materialization, IN,
  1374. # non-null field followed by nullable
  1375. #
  1376. CREATE TABLE t1 (a1 CHAR(8) NOT NULL, a2 char(8) NOT NULL);
  1377. CREATE TABLE t2a (b1 char(8), b2 char(8));
  1378. CREATE TABLE t2b (b1 CHAR(8), b2 char(8) NOT NULL);
  1379. CREATE TABLE t2c (b1 CHAR(8) NOT NULL, b2 char(8));
  1380. INSERT INTO t1 VALUES ('1 - 12', '2 - 22');
  1381. INSERT INTO t2a VALUES ('1 - 11', '2 - 21'),
  1382. ('1 - 11', '2 - 21'),
  1383. ('1 - 12', '2 - 22'),
  1384. ('1 - 12', '2 - 22'),
  1385. ('1 - 13', '2 - 23');
  1386. INSERT INTO t2b SELECT * FROM t2a;
  1387. INSERT INTO t2c SELECT * FROM t2a;
  1388. set @save_optimizer_switch=@@optimizer_switch;
  1389. set optimizer_switch='semijoin=off,materialization=on';
  1390. SELECT * FROM t1
  1391. WHERE (a1, a2) IN (
  1392. SELECT b1, b2 FROM t2c WHERE b1 > '0' GROUP BY b1, b2);
  1393. a1 a2
  1394. 1 - 12 2 - 22
  1395. SELECT * FROM t1
  1396. WHERE (a1, a2) IN (
  1397. SELECT b1, b2 FROM t2a WHERE b1 > '0');
  1398. a1 a2
  1399. 1 - 12 2 - 22
  1400. SELECT * FROM t1
  1401. WHERE (a1, a2) IN (
  1402. SELECT b1, b2 FROM t2b WHERE b1 > '0');
  1403. a1 a2
  1404. 1 - 12 2 - 22
  1405. SELECT * FROM t1
  1406. WHERE (a1, a2) IN (
  1407. SELECT b1, b2 FROM t2c WHERE b1 > '0');
  1408. a1 a2
  1409. 1 - 12 2 - 22
  1410. set optimizer_switch=@save_optimizer_switch;
  1411. DROP TABLE t1,t2a,t2b,t2c;
  1412. #
  1413. # Bug#57623: subquery within before insert trigger causes crash (sj=on)
  1414. #
  1415. CREATE TABLE ot1(a INT);
  1416. CREATE TABLE ot2(a INT);
  1417. CREATE TABLE ot3(a INT);
  1418. CREATE TABLE it1(a INT);
  1419. INSERT INTO ot1 VALUES(0),(1),(2),(3),(4),(5),(6),(7);
  1420. INSERT INTO ot2 VALUES(0),(2),(4),(6);
  1421. INSERT INTO ot3 VALUES(0),(3),(6);
  1422. INSERT INTO it1 VALUES(0),(1),(2),(3),(4),(5),(6),(7);
  1423. set @save_optimizer_switch=@@optimizer_switch;
  1424. set optimizer_switch='semijoin=on';
  1425. set optimizer_switch='materialization=off';
  1426. explain SELECT *
  1427. FROM ot1
  1428. LEFT JOIN
  1429. (ot2 JOIN ot3 on ot2.a=ot3.a)
  1430. ON ot1.a=ot2.a AND ot1.a IN (SELECT a from it1);
  1431. id select_type table type possible_keys key key_len ref rows Extra
  1432. 1 PRIMARY ot1 ALL NULL NULL NULL NULL 8
  1433. 1 PRIMARY ot3 ALL NULL NULL NULL NULL 3 Using where
  1434. 1 PRIMARY ot2 ALL NULL NULL NULL NULL 4 Using where
  1435. 2 DEPENDENT SUBQUERY it1 ALL NULL NULL NULL NULL 8 Using where
  1436. SELECT *
  1437. FROM ot1
  1438. LEFT JOIN
  1439. (ot2 JOIN ot3 on ot2.a=ot3.a)
  1440. ON ot1.a=ot2.a AND ot1.a IN (SELECT a from it1);
  1441. a a a
  1442. 0 0 0
  1443. 1 NULL NULL
  1444. 2 NULL NULL
  1445. 3 NULL NULL
  1446. 4 NULL NULL
  1447. 5 NULL NULL
  1448. 6 6 6
  1449. 7 NULL NULL
  1450. prepare s from 'SELECT *
  1451. FROM ot1
  1452. LEFT JOIN
  1453. (ot2 JOIN ot3 on ot2.a=ot3.a)
  1454. ON ot1.a=ot2.a AND ot1.a IN (SELECT a from it1)';
  1455. execute s;
  1456. a a a
  1457. 0 0 0
  1458. 1 NULL NULL
  1459. 2 NULL NULL
  1460. 3 NULL NULL
  1461. 4 NULL NULL
  1462. 5 NULL NULL
  1463. 6 6 6
  1464. 7 NULL NULL
  1465. execute s;
  1466. a a a
  1467. 0 0 0
  1468. 1 NULL NULL
  1469. 2 NULL NULL
  1470. 3 NULL NULL
  1471. 4 NULL NULL
  1472. 5 NULL NULL
  1473. 6 6 6
  1474. 7 NULL NULL
  1475. deallocate prepare s;
  1476. set optimizer_switch='materialization=on';
  1477. explain SELECT *
  1478. FROM ot1
  1479. LEFT JOIN
  1480. (ot2 JOIN ot3 on ot2.a=ot3.a)
  1481. ON ot1.a=ot2.a AND ot1.a IN (SELECT a from it1);
  1482. id select_type table type possible_keys key key_len ref rows Extra
  1483. 1 PRIMARY ot1 ALL NULL NULL NULL NULL 8
  1484. 1 PRIMARY ot3 ALL NULL NULL NULL NULL 3 Using where
  1485. 1 PRIMARY ot2 ALL NULL NULL NULL NULL 4 Using where
  1486. 2 MATERIALIZED it1 ALL NULL NULL NULL NULL 8
  1487. SELECT *
  1488. FROM ot1
  1489. LEFT JOIN
  1490. (ot2 JOIN ot3 on ot2.a=ot3.a)
  1491. ON ot1.a=ot2.a AND ot1.a IN (SELECT a from it1);
  1492. a a a
  1493. 0 0 0
  1494. 1 NULL NULL
  1495. 2 NULL NULL
  1496. 3 NULL NULL
  1497. 4 NULL NULL
  1498. 5 NULL NULL
  1499. 6 6 6
  1500. 7 NULL NULL
  1501. prepare s from 'SELECT *
  1502. FROM ot1
  1503. LEFT JOIN
  1504. (ot2 JOIN ot3 on ot2.a=ot3.a)
  1505. ON ot1.a=ot2.a AND ot1.a IN (SELECT a from it1)';
  1506. execute s;
  1507. a a a
  1508. 0 0 0
  1509. 1 NULL NULL
  1510. 2 NULL NULL
  1511. 3 NULL NULL
  1512. 4 NULL NULL
  1513. 5 NULL NULL
  1514. 6 6 6
  1515. 7 NULL NULL
  1516. execute s;
  1517. a a a
  1518. 0 0 0
  1519. 1 NULL NULL
  1520. 2 NULL NULL
  1521. 3 NULL NULL
  1522. 4 NULL NULL
  1523. 5 NULL NULL
  1524. 6 6 6
  1525. 7 NULL NULL
  1526. deallocate prepare s;
  1527. set optimizer_switch=@save_optimizer_switch;
  1528. DROP TABLE ot1, ot2, ot3, it1;
  1529. #
  1530. # Bug#59919/11766739: Crash in tmp_table_param::init() with semijoin=on
  1531. #
  1532. CREATE TABLE t1 (f1 INTEGER) ENGINE=MyISAM;
  1533. CREATE TABLE t2 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
  1534. CREATE TABLE t3 (f1 INTEGER, f2 INTEGER) ENGINE=MyISAM;
  1535. INSERT INTO t1 VALUES (1);
  1536. INSERT INTO t2 VALUES (1,1), (2,1);
  1537. INSERT INTO t3 VALUES
  1538. (1,1), (2,1), (5,4), (7,3), (8,2), (8,1), (7,3),
  1539. (9,5), (4,3), (7,2), (7,7), (3,1), (5,8), (9,7);
  1540. set @save_optimizer_switch=@@optimizer_switch;
  1541. set optimizer_switch='semijoin=off,materialization=on';
  1542. EXPLAIN
  1543. SELECT * FROM t3
  1544. WHERE f2 IN (SELECT t1.f1
  1545. FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
  1546. id select_type table type possible_keys key key_len ref rows Extra
  1547. 1 PRIMARY t3 ALL NULL NULL NULL NULL 14 Using where
  1548. 2 MATERIALIZED t1 system NULL NULL NULL NULL 1
  1549. 2 MATERIALIZED b1 ALL NULL NULL NULL NULL 2 Using where
  1550. 2 MATERIALIZED b2 ALL NULL NULL NULL NULL 2
  1551. SELECT * FROM t3
  1552. WHERE f2 IN (SELECT t1.f1
  1553. FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
  1554. f1 f2
  1555. 1 1
  1556. 2 1
  1557. 8 1
  1558. 3 1
  1559. set optimizer_switch='semijoin=on,materialization=on';
  1560. EXPLAIN
  1561. SELECT * FROM t3
  1562. WHERE f2 IN (SELECT t1.f1
  1563. FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
  1564. id select_type table type possible_keys key key_len ref rows Extra
  1565. 1 PRIMARY t1 system NULL NULL NULL NULL 1
  1566. 1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 1
  1567. 1 PRIMARY t3 ALL NULL NULL NULL NULL 14 Using where; Using join buffer (flat, BNL join)
  1568. 2 MATERIALIZED b1 ALL NULL NULL NULL NULL 2 Using where
  1569. 2 MATERIALIZED b2 ALL NULL NULL NULL NULL 2
  1570. SELECT * FROM t3
  1571. WHERE f2 IN (SELECT t1.f1
  1572. FROM t1 LEFT OUTER JOIN (t2 AS b1 JOIN t2 AS b2 ON TRUE) ON TRUE);
  1573. f1 f2
  1574. 1 1
  1575. 2 1
  1576. 8 1
  1577. 3 1
  1578. set optimizer_switch=@save_optimizer_switch;
  1579. DROP TABLE t1, t2, t3 ;
  1580. #
  1581. #
  1582. # BUG#784723: Wrong result with semijoin + nested subqueries in maria-5.3
  1583. #
  1584. CREATE TABLE t1 ( t1field integer, primary key (t1field));
  1585. CREATE TABLE t2 ( t2field integer, primary key (t2field));
  1586. INSERT INTO t1 VALUES (1),(2),(3);
  1587. INSERT INTO t2 VALUES (2),(3),(4);
  1588. explain
  1589. SELECT * FROM t1 A
  1590. WHERE
  1591. A.t1field IN (SELECT A.t1field FROM t2 B) AND
  1592. A.t1field IN (SELECT C.t2field FROM t2 C
  1593. WHERE C.t2field IN (SELECT D.t2field FROM t2 D));
  1594. id select_type table type possible_keys key key_len ref rows Extra
  1595. 1 PRIMARY A index PRIMARY PRIMARY 4 NULL 3 Using index
  1596. 1 PRIMARY B index NULL PRIMARY 4 NULL 3 Using index; Start temporary; End temporary
  1597. 1 PRIMARY C eq_ref PRIMARY PRIMARY 4 test.A.t1field 1 Using index
  1598. 1 PRIMARY D eq_ref PRIMARY PRIMARY 4 test.A.t1field 1 Using index
  1599. SELECT * FROM t1 A
  1600. WHERE
  1601. A.t1field IN (SELECT A.t1field FROM t2 B) AND
  1602. A.t1field IN (SELECT C.t2field FROM t2 C
  1603. WHERE C.t2field IN (SELECT D.t2field FROM t2 D));
  1604. t1field
  1605. 2
  1606. 3
  1607. drop table t1,t2;
  1608. #
  1609. # BUG#787299: Valgrind complains on a join query with two IN subqueries
  1610. #
  1611. create table t1 (a int);
  1612. insert into t1 values (1), (2), (3);
  1613. create table t2 as select * from t1;
  1614. select * from t1 A, t1 B
  1615. where A.a = B.a and A.a in (select a from t2 C) and B.a in (select a from t2 D);
  1616. a a
  1617. 1 1
  1618. 2 2
  1619. 3 3
  1620. explain
  1621. select * from t1 A, t1 B
  1622. where A.a = B.a and A.a in (select a from t2 C) and B.a in (select a from t2 D);
  1623. id select_type table type possible_keys key key_len ref rows Extra
  1624. 1 PRIMARY A ALL NULL NULL NULL NULL 3
  1625. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
  1626. 1 PRIMARY B ALL NULL NULL NULL NULL 3 Using where; Using join buffer (flat, BNL join)
  1627. 1 PRIMARY <subquery3> eq_ref distinct_key distinct_key 4 func 1
  1628. 2 MATERIALIZED C ALL NULL NULL NULL NULL 3
  1629. 3 MATERIALIZED D ALL NULL NULL NULL NULL 3
  1630. drop table t1, t2;
  1631. #
  1632. # BUG#784441: Abort on semijoin with a view as the inner table
  1633. #
  1634. CREATE TABLE t1 (a int) ;
  1635. INSERT INTO t1 VALUES (1), (1);
  1636. CREATE TABLE t2 (a int) ;
  1637. INSERT INTO t2 VALUES (1), (1);
  1638. CREATE VIEW v1 AS SELECT 1;
  1639. EXPLAIN
  1640. SELECT * FROM t1 INNER JOIN t2 ON t2.a != 0 AND t2.a IN (SELECT * FROM v1);
  1641. id select_type table type possible_keys key key_len ref rows Extra
  1642. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2
  1643. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
  1644. 2 MATERIALIZED <derived3> system NULL NULL NULL NULL 1
  1645. 3 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
  1646. SELECT * FROM t1 INNER JOIN t2 ON t2.a != 0 AND t2.a IN (SELECT * FROM v1);
  1647. a a
  1648. 1 1
  1649. 1 1
  1650. 1 1
  1651. 1 1
  1652. DROP VIEW v1;
  1653. DROP TABLE t1,t2;
  1654. #
  1655. # BUG#751439 Assertion `!table->file || table->file->inited == handler::NONE' failed with subquery
  1656. #
  1657. CREATE TABLE t1 ( f10 int, f11 int) ;
  1658. INSERT IGNORE INTO t1 VALUES (0,0),(0,0);
  1659. CREATE TABLE t2 ( f11 int);
  1660. INSERT IGNORE INTO t2 VALUES (0),(0);
  1661. CREATE TABLE t3 ( f11 int) ;
  1662. INSERT IGNORE INTO t3 VALUES (0);
  1663. SELECT alias1.f11 AS field2
  1664. FROM ( t3 AS alias2 JOIN t1 AS alias3 ON alias3.f10 = 1)
  1665. LEFT JOIN ( t2 AS alias1 ) ON alias3.f11 = 1
  1666. WHERE alias2.f11 IN ( SELECT f11 FROM t2 )
  1667. GROUP BY field2 ;
  1668. field2
  1669. drop table t1, t2, t3;
  1670. #
  1671. # BUG#778406 Crash in hp_movelink with Aria engine and subqueries
  1672. #
  1673. CREATE TABLE t4 (f10 varchar(32) , KEY (f10)) ENGINE=Aria;
  1674. INSERT INTO t4 VALUES ('x'),('m'),('c');
  1675. CREATE TABLE t1 (f11 int) ENGINE=Aria;
  1676. INSERT INTO t1 VALUES (0),(0),(0);
  1677. CREATE TABLE t2 ( f10 int) ENGINE=Aria;
  1678. INSERT INTO t2 VALUES (0),(0),(0);
  1679. CREATE TABLE t3 ( f10 int, f11 int) ENGINE=Aria;
  1680. SELECT *
  1681. FROM t4
  1682. WHERE f10 IN
  1683. ( SELECT t1.f11
  1684. FROM t1
  1685. LEFT JOIN t2 JOIN t3 ON t3.f10 = t2.f10 ON t3.f11 != 0 );
  1686. f10
  1687. x
  1688. m
  1689. c
  1690. drop table t1,t2,t3,t4;
  1691. #
  1692. # BUG#751484: Valgrind warning / sporadic crash in evaluate_join_record sql_select.cc:14099 with semijoin
  1693. #
  1694. CREATE TABLE t1 ( f10 int, f11 int, KEY (f10));
  1695. INSERT IGNORE INTO t1 VALUES (0, 0),(0, 0);
  1696. CREATE TABLE t3 ( f10 int);
  1697. INSERT IGNORE INTO t3 VALUES (0);
  1698. set @tmp_751484= @@optimizer_switch;
  1699. set optimizer_switch='materialization=on';
  1700. SELECT * FROM t1
  1701. WHERE f11 IN (
  1702. SELECT C_SQ1_alias1.f11
  1703. FROM t1 AS C_SQ1_alias1
  1704. JOIN t3 AS C_SQ1_alias2
  1705. ON C_SQ1_alias2.f10 = C_SQ1_alias1.f10
  1706. );
  1707. f10 f11
  1708. 0 0
  1709. 0 0
  1710. set optimizer_switch='materialization=off';
  1711. SELECT * FROM t1
  1712. WHERE f11 IN (
  1713. SELECT C_SQ1_alias1.f11
  1714. FROM t1 AS C_SQ1_alias1
  1715. JOIN t3 AS C_SQ1_alias2
  1716. ON C_SQ1_alias2.f10 = C_SQ1_alias1.f10
  1717. );
  1718. f10 f11
  1719. 0 0
  1720. 0 0
  1721. set optimizer_switch=@tmp_751484;
  1722. drop table t1, t3;
  1723. # BUG#795530 Wrong result with subquery semijoin materialization and outer join
  1724. # Simplified testcase that uses DuplicateElimination
  1725. #
  1726. create table t1 (a int);
  1727. create table t2 (a int, b char(10));
  1728. insert into t1 values (1),(2);
  1729. insert into t2 values (1, 'one'), (3, 'three');
  1730. create table t3 (b char(10));
  1731. insert into t3 values('three'),( 'four');
  1732. insert into t3 values('three'),( 'four');
  1733. insert into t3 values('three'),( 'four');
  1734. insert into t3 values('three'),( 'four');
  1735. explain select * from t3 where t3.b in (select t2.b from t1 left join t2 on t1.a=t2.a);
  1736. id select_type table type possible_keys key key_len ref rows Extra
  1737. 1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 2
  1738. 1 PRIMARY t3 ALL NULL NULL NULL NULL 8 Using where; Using join buffer (flat, BNL join)
  1739. 2 MATERIALIZED t1 ALL NULL NULL NULL NULL 2
  1740. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using where
  1741. select * from t3 where t3.b in (select t2.b from t1 left join t2 on t1.a=t2.a);
  1742. b
  1743. drop table t1, t2, t3;
  1744. #
  1745. # BUG#600958 RQG: Crash in optimize_semijoin_nests
  1746. #
  1747. CREATE TABLE t1 (
  1748. pk int(11) NOT NULL AUTO_INCREMENT,
  1749. col_int_key int(11) DEFAULT NULL,
  1750. col_date_key date DEFAULT NULL,
  1751. col_varchar_key varchar(1) DEFAULT NULL,
  1752. PRIMARY KEY (pk),
  1753. KEY col_int_key (col_int_key),
  1754. KEY col_date_key (col_date_key),
  1755. KEY col_varchar_key (col_varchar_key,col_int_key)
  1756. ) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
  1757. INSERT INTO t1 VALUES (10,8,'2002-02-21',NULL);
  1758. CREATE TABLE t2 (
  1759. pk int(11) NOT NULL AUTO_INCREMENT,
  1760. col_int_key int(11) DEFAULT NULL,
  1761. col_date_key date DEFAULT NULL,
  1762. col_varchar_key varchar(1) DEFAULT NULL,
  1763. PRIMARY KEY (pk),
  1764. KEY col_int_key (col_int_key),
  1765. KEY col_date_key (col_date_key),
  1766. KEY col_varchar_key (col_varchar_key,col_int_key)
  1767. ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
  1768. INSERT INTO t2 VALUES (1,7,'1900-01-01','f');
  1769. SELECT col_date_key FROM t1
  1770. WHERE 5 IN (
  1771. SELECT SUBQUERY3_t1 .col_int_key
  1772. FROM t2 SUBQUERY3_t1
  1773. LEFT JOIN t1 SUBQUERY3_t2 ON SUBQUERY3_t1 .col_varchar_key
  1774. );
  1775. col_date_key
  1776. drop table t2, t1;
  1777. #
  1778. # No BUG#: Duplicate weedout check is not done for outer joins
  1779. #
  1780. create table t1 (a int);
  1781. create table t2 (a int);
  1782. insert into t1 values (1),(1),(2),(2);
  1783. insert into t2 values (1);
  1784. create table t0 (a int);
  1785. insert into t0 values (1),(2);
  1786. set @tmp_20110622= @@optimizer_switch;
  1787. set optimizer_switch='firstmatch=off,loosescan=off,materialization=off';
  1788. # Check DuplicateWeedout + join buffer
  1789. explain
  1790. select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a);
  1791. id select_type table type possible_keys key key_len ref rows Extra
  1792. 1 PRIMARY t0 ALL NULL NULL NULL NULL 2
  1793. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary
  1794. 1 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where; End temporary
  1795. select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a);
  1796. a
  1797. 1
  1798. 2
  1799. # Check DuplicateWeedout without join buffer
  1800. set @tmp_jcl_20110622= @@join_cache_level;
  1801. set join_cache_level= 0;
  1802. explain
  1803. select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a);
  1804. id select_type table type possible_keys key key_len ref rows Extra
  1805. 1 PRIMARY t0 ALL NULL NULL NULL NULL 2
  1806. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary
  1807. 1 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where; End temporary
  1808. select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a);
  1809. a
  1810. 1
  1811. 2
  1812. # Check FirstMatch without join buffer:
  1813. set optimizer_switch='firstmatch=on';
  1814. explain
  1815. select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a);
  1816. id select_type table type possible_keys key key_len ref rows Extra
  1817. 1 PRIMARY t0 ALL NULL NULL NULL NULL 2
  1818. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary
  1819. 1 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where; End temporary
  1820. select * from t0 where a in (select t1.a from t1 left join t2 on t1.a=t2.a);
  1821. a
  1822. 1
  1823. 2
  1824. #
  1825. # Now, check the same for multiple inner tables:
  1826. alter table t2 add b int;
  1827. update t2 set b=a;
  1828. create table t3 as select * from t2;
  1829. set optimizer_switch='firstmatch=off';
  1830. set join_cache_level= 0;
  1831. # DuplicateWeedout without join buffer
  1832. explain
  1833. select * from t0
  1834. where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a);
  1835. id select_type table type possible_keys key key_len ref rows Extra
  1836. 1 PRIMARY t0 ALL NULL NULL NULL NULL 2
  1837. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary
  1838. 1 PRIMARY t3 ALL NULL NULL NULL NULL 1 Using where
  1839. 1 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where; End temporary
  1840. select * from t0
  1841. where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a);
  1842. a
  1843. 1
  1844. 2
  1845. set @@join_cache_level=@tmp_jcl_20110622;
  1846. # DuplicateWeedout + join buffer
  1847. explain
  1848. select * from t0
  1849. where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a);
  1850. id select_type table type possible_keys key key_len ref rows Extra
  1851. 1 PRIMARY t0 ALL NULL NULL NULL NULL 2
  1852. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary
  1853. 1 PRIMARY t3 ALL NULL NULL NULL NULL 1 Using where
  1854. 1 PRIMARY t2 ALL NULL NULL NULL NULL 1 Using where; End temporary
  1855. select * from t0
  1856. where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a);
  1857. a
  1858. 1
  1859. 2
  1860. # Now, let the inner join side have a 'partial' match
  1861. select * from t3;
  1862. a b
  1863. 1 1
  1864. insert into t3 values(2,2);
  1865. explain
  1866. select * from t0
  1867. where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a);
  1868. id select_type table type possible_keys key key_len ref rows Extra
  1869. 1 PRIMARY t0 ALL NULL NULL NULL NULL 2
  1870. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; Start temporary
  1871. 1 PRIMARY t2 ALL NULL NULL NULL NULL 1
  1872. 1 PRIMARY t3 ALL NULL NULL NULL NULL 2 Using where; End temporary
  1873. select * from t0
  1874. where a in (select t1.a from t1 left join (t3 join t2 on t3.b=t2.b) on t1.a=t3.a);
  1875. a
  1876. 1
  1877. 2
  1878. set @@optimizer_switch=@tmp_20110622;
  1879. drop table t0, t1, t2, t3;
  1880. #
  1881. # BUG#802965: Crash in do_copy_not_null with semijoin=on in maria-5.3
  1882. #
  1883. set @save_802965= @@optimizer_switch;
  1884. set optimizer_switch='semijoin=on,materialization=off,firstmatch=off,loosescan=off';
  1885. CREATE TABLE t2 ( f1 int NOT NULL , PRIMARY KEY (f1)) ;
  1886. INSERT IGNORE INTO t2 VALUES (19),(20);
  1887. CREATE TABLE t1 ( f1 int NOT NULL , PRIMARY KEY (f1)) ;
  1888. INSERT IGNORE INTO t1 VALUES (21),(22),(23),(24);
  1889. SELECT *
  1890. FROM t2 , t1
  1891. WHERE t2.f1 IN
  1892. (
  1893. SELECT SQ1_alias1.f1
  1894. FROM t1 AS SQ1_alias1 LEFT JOIN t2 AS SQ1_alias2 JOIN t2 AS SQ1_alias3 ON SQ1_alias3.f1 ON SQ1_alias3.f1
  1895. )
  1896. AND t1.f1 = t2.f1 ;
  1897. f1 f1
  1898. DROP TABLE t1, t2;
  1899. set optimizer_switch=@save_802965;
  1900. #
  1901. # BUG#803365: Crash in pull_out_semijoin_tables with outer join + semijoin + derived tables in maria-5.3 with WL#106
  1902. #
  1903. CREATE TABLE t1 ( f1 int) ;
  1904. INSERT INTO t1 VALUES (1),(1);
  1905. CREATE TABLE t2 ( f2 int) ;
  1906. INSERT INTO t2 VALUES (1),(1);
  1907. CREATE TABLE t3 ( f3 int) ;
  1908. INSERT INTO t3 VALUES (1),(1);
  1909. SELECT *
  1910. FROM t1
  1911. WHERE t1.f1 IN (
  1912. SELECT t2.f2
  1913. FROM t2
  1914. LEFT JOIN (
  1915. SELECT *
  1916. FROM t3
  1917. ) AS alias1
  1918. ON alias1.f3 = t2.f2
  1919. );
  1920. f1
  1921. 1
  1922. 1
  1923. DROP TABLE t1,t2,t3;
  1924. #
  1925. # BUG#611704: Crash in replace_where_subcondition with nested subquery and semijoin=on
  1926. #
  1927. CREATE TABLE t1 ( f1 int) ;
  1928. CREATE TABLE t2 ( f1 int) ;
  1929. CREATE TABLE t3 ( f1 int) ;
  1930. SELECT * FROM (
  1931. SELECT t3.*
  1932. FROM t2 STRAIGHT_JOIN t3
  1933. ON t3.f1
  1934. AND (t3.f1 ) IN (
  1935. SELECT t1.f1
  1936. FROM t1
  1937. )
  1938. ) AS alias1;
  1939. f1
  1940. DROP TABLE t1,t2,t3;
  1941. # BUG#611704: another testcase:
  1942. CREATE TABLE t1 ( f1 int(11), f3 varchar(1), f4 varchar(1)) ;
  1943. CREATE TABLE t2 ( f2 int(11), KEY (f2));
  1944. CREATE TABLE t3 ( f4 varchar(1)) ;
  1945. PREPARE st1 FROM '
  1946. SELECT *
  1947. FROM t1
  1948. STRAIGHT_JOIN ( t2 STRAIGHT_JOIN t3 ON t2.f2 )
  1949. ON (t1.f3) IN ( SELECT f4 FROM t1 )
  1950. ';
  1951. EXECUTE st1;
  1952. f1 f3 f4 f2 f4
  1953. DROP TABLE t1,t2,t3;
  1954. #
  1955. # BUG#803457: Wrong result with semijoin + view + outer join in maria-5.3-subqueries-mwl90
  1956. # (Original testcase)
  1957. #
  1958. CREATE TABLE t1 (f1 int, f2 int );
  1959. INSERT INTO t1 VALUES (2,0),(4,0),(0,NULL);
  1960. CREATE TABLE t2 (f2 int, f3 int );
  1961. INSERT INTO t2 VALUES (NULL,NULL),(0,0);
  1962. CREATE TABLE t3 ( f1 int, f3 int );
  1963. INSERT INTO t3 VALUES (2,0),(4,0),(0,NULL),(4,0),(8,0);
  1964. CREATE TABLE t4 ( f2 int, KEY (f2) );
  1965. INSERT INTO t4 VALUES (0),(NULL);
  1966. CREATE VIEW v4 AS SELECT DISTINCT f2 FROM t4 ;
  1967. # The following must not have outer joins:
  1968. explain extended
  1969. SELECT * FROM t1 NATURAL LEFT JOIN (t2, t3) WHERE t2.f3 IN (SELECT * FROM t4);
  1970. id select_type table type possible_keys key key_len ref rows filtered Extra
  1971. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
  1972. 1 PRIMARY t4 ref f2 f2 5 test.t2.f3 2 100.00 Using index; FirstMatch(t2)
  1973. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join)
  1974. 1 PRIMARY t3 ALL NULL NULL NULL NULL 5 100.00 Using where; Using join buffer (flat, BNL join)
  1975. Warnings:
  1976. 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`))
  1977. SELECT * FROM t1 NATURAL LEFT JOIN (t2, t3) WHERE t2.f3 IN (SELECT * FROM t4);
  1978. f1 f2 f3 f3
  1979. 2 0 0 0
  1980. 4 0 0 0
  1981. 4 0 0 0
  1982. drop view v4;
  1983. drop table t1, t2, t3, t4;
  1984. #
  1985. # BUG#803303: Wrong result with semijoin=on, outer join in maria-5.3-subqueries-mwl90
  1986. #
  1987. # Testcase#1:
  1988. set @tmp803303= @@optimizer_switch;
  1989. set optimizer_switch = 'semijoin=on,materialization=off,firstmatch=off,loosescan=off';
  1990. CREATE TABLE t2 ( f1 int) ;
  1991. INSERT IGNORE INTO t2 VALUES (6),(8);
  1992. CREATE TABLE t1 ( f1 int, f2 int, f3 int) ;
  1993. INSERT IGNORE INTO t1 VALUES (8,0,0),(7,0,0),(9,0,0);
  1994. SELECT alias2.f1
  1995. FROM t2 AS alias1
  1996. LEFT JOIN ( t1 AS alias2 JOIN t1 AS alias3 ON alias3.f2 = alias2.f3 )
  1997. ON alias3.f2 = alias2.f2
  1998. WHERE alias2.f1 IN ( SELECT f1 FROM t2 AS alias4 ) ;
  1999. f1
  2000. 8
  2001. 8
  2002. 8
  2003. 8
  2004. 8
  2005. 8
  2006. drop table t1,t2;
  2007. set optimizer_switch= @tmp803303;
  2008. # Testcase #2:
  2009. CREATE TABLE t1 ( f10 int) ;
  2010. INSERT INTO t1 VALUES (0),(0);
  2011. CREATE TABLE t2 ( f10 int, f11 varchar(1)) ;
  2012. INSERT INTO t2 VALUES (0,'a'),(0,'b');
  2013. CREATE TABLE t3 ( f10 int) ;
  2014. INSERT INTO t3 VALUES (0),(0),(0),(0),(0);
  2015. CREATE TABLE t4 ( f10 varchar(1), f11 int) ;
  2016. INSERT INTO t4 VALUES ('a',0),('b',0);
  2017. SELECT * FROM t1
  2018. LEFT JOIN ( t2 JOIN t3 ON t3.f10 = t2.f10 ) ON t1.f10 = t2.f10
  2019. WHERE t2.f10 IN (
  2020. SELECT t4.f11
  2021. FROM t4
  2022. WHERE t4.f10 != t2.f11
  2023. );
  2024. f10 f10 f11 f10
  2025. 0 0 a 0
  2026. 0 0 a 0
  2027. 0 0 b 0
  2028. 0 0 b 0
  2029. 0 0 a 0
  2030. 0 0 a 0
  2031. 0 0 b 0
  2032. 0 0 b 0
  2033. 0 0 a 0
  2034. 0 0 a 0
  2035. 0 0 b 0
  2036. 0 0 b 0
  2037. 0 0 a 0
  2038. 0 0 a 0
  2039. 0 0 b 0
  2040. 0 0 b 0
  2041. 0 0 a 0
  2042. 0 0 a 0
  2043. 0 0 b 0
  2044. 0 0 b 0
  2045. drop table t1,t2,t3,t4;
  2046. #
  2047. # BUG#803457: Wrong result with semijoin + view + outer join in maria-5.3-subqueries-mwl90
  2048. #
  2049. set @tmp803457=@@optimizer_switch;
  2050. set optimizer_switch='materialization=off';
  2051. CREATE TABLE t1 (f1 int, f2 int );
  2052. INSERT INTO t1 VALUES (2,0),(4,0),(0,NULL);
  2053. CREATE TABLE t2 (f2 int, f3 int );
  2054. INSERT INTO t2 VALUES (NULL,NULL),(0,0);
  2055. CREATE TABLE t3 ( f1 int, f3 int );
  2056. INSERT INTO t3 VALUES (2,0),(4,0),(0,NULL),(4,0),(8,0);
  2057. CREATE TABLE t4 ( f2 int);
  2058. INSERT INTO t4 VALUES (0),(NULL);
  2059. # The following uses Duplicate Weedout, and "End temporary" must not be
  2060. # in the middle of the inner side of an outer join:
  2061. explain
  2062. SELECT * FROM t1 NATURAL LEFT JOIN (t2, t3) WHERE IFNULL(t2.f3,'foo') IN (SELECT * FROM t4);
  2063. id select_type table type possible_keys key key_len ref rows Extra
  2064. 1 PRIMARY t1 ALL NULL NULL NULL NULL 3
  2065. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where
  2066. 1 PRIMARY t3 ALL NULL NULL NULL NULL 5 Using where
  2067. 1 PRIMARY t4 ALL NULL NULL NULL NULL 2 Using where; Start temporary; End temporary
  2068. SELECT * FROM t1 NATURAL LEFT JOIN (t2, t3 ) WHERE IFNULL(t2.f3,'foo') IN (SELECT * FROM t4);
  2069. f1 f2 f3 f3
  2070. 2 0 0 0
  2071. 4 0 0 0
  2072. 4 0 0 0
  2073. 0 NULL NULL NULL
  2074. DROP TABLE t1, t2, t3, t4;
  2075. set @tmp803457=@@optimizer_switch;
  2076. #
  2077. # BUG#818280: crash in do_copy_not_null() in maria-5.3 with semijoin
  2078. #
  2079. CREATE TABLE t1 ( c1 int NOT NULL , c2 int NOT NULL, PRIMARY KEY (c1)) ;
  2080. INSERT IGNORE INTO t1 VALUES (2,7),(1,3),(5,6);
  2081. CREATE TABLE t3 ( c1 int NOT NULL , c2 int NOT NULL, PRIMARY KEY (c1)) ;
  2082. INSERT IGNORE INTO t3 VALUES (2,7),(1,3),(5,6);
  2083. CREATE TABLE t2 ( c1 int NOT NULL , c5 int NOT NULL );
  2084. INSERT IGNORE INTO t2 VALUES (2,2),(2,2),(5,6);
  2085. SELECT * FROM t1 WHERE c1 IN ( SELECT t3.c1 FROM t3 LEFT JOIN t2 ON t2 .c1 = t3 .c1 WHERE t2.c5 != 0 );
  2086. c1 c2
  2087. 2 7
  2088. 5 6
  2089. DROP TABLE t1, t2, t3;
  2090. #
  2091. # BUG#834534: Assertion `0' failed in replace_where_subcondition with semijoin subquery in HAVING
  2092. #
  2093. CREATE TABLE t1 ( d int );
  2094. INSERT INTO t1 VALUES (2),(2),(0),(2),(2);
  2095. CREATE TABLE t2 ( b int );
  2096. INSERT INTO t2 VALUES (4),(3),(3);
  2097. CREATE TABLE t3 ( a int );
  2098. SELECT *
  2099. FROM t3
  2100. WHERE (t3.a) IN (
  2101. SELECT t1.d
  2102. FROM t1
  2103. HAVING ( 4 ) IN (
  2104. SELECT t2.b
  2105. FROM t2
  2106. )
  2107. );
  2108. a
  2109. drop table t1, t2,t3;
  2110. #
  2111. # BUG#834758: Wrong result with innner join, LooseScan, two-column IN() predicate
  2112. #
  2113. set @tmp835758=@@optimizer_switch;
  2114. set optimizer_switch='semijoin=on,loosescan=on,materialization=off,firstmatch=off';
  2115. CREATE TABLE t1 (b int) ;
  2116. INSERT INTO t1 VALUES (1),(5);
  2117. CREATE TABLE t2 (a int, PRIMARY KEY (a)) ;
  2118. INSERT INTO t2 VALUES (6),(10);
  2119. CREATE TABLE t3 (a int, b int, KEY (b)) ;
  2120. INSERT INTO t3 VALUES (6,5),(6,2),(8,0),(9,1),(6,5);
  2121. # This used to incorrectly pick a join order of (t1, LooseScan(t3), t2):
  2122. explain
  2123. SELECT * FROM t1, t2 WHERE (t2.a , t1.b) IN (SELECT a, b FROM t3);
  2124. id select_type table type possible_keys key key_len ref rows Extra
  2125. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2
  2126. 1 PRIMARY t2 index PRIMARY PRIMARY 4 NULL 2 Using index; Using join buffer (flat, BNL join)
  2127. 1 PRIMARY t3 ALL b NULL NULL NULL 5 Using where; Start temporary; End temporary
  2128. SELECT * FROM t1, t2 WHERE (t2.a , t1.b) IN (SELECT a, b FROM t3);
  2129. b a
  2130. 5 6
  2131. DROP TABLE t1, t2, t3;
  2132. set @@optimizer_switch= @tmp835758;
  2133. #
  2134. # BUG#834739: Wrong result with 3-way inner join, LooseScan,multipart keys
  2135. #
  2136. set @tmp834739=@@optimizer_switch;
  2137. set optimizer_switch='semijoin=on,loosescan=on,materialization=off,firstmatch=off';
  2138. CREATE TABLE t2 ( b int, c int, KEY (b)) ;
  2139. INSERT INTO t2 VALUES (1,0),(1,0),(9,0),(1,0),(5,0);
  2140. INSERT INTO t2 VALUES (2,0),(3,0),(8,0),(6,0),(5,0);
  2141. CREATE TABLE t3 ( a int);
  2142. INSERT INTO t3 VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0),(0);
  2143. CREATE TABLE t4 ( a int);
  2144. INSERT INTO t4 VALUES (0),(0),(0);
  2145. CREATE TABLE t5 ( b int, a int , KEY (a,b)) ;
  2146. INSERT INTO t5 VALUES (7,0),(9,0);
  2147. explain
  2148. SELECT * FROM t3 WHERE t3.a IN (SELECT t5.a FROM t2, t4, t5 WHERE t2.c = t5.a AND t2.b = t5.b);
  2149. id select_type table type possible_keys key key_len ref rows Extra
  2150. 1 PRIMARY t5 index a a 10 NULL 2 Using where; Using index; LooseScan
  2151. 1 PRIMARY t4 ALL NULL NULL NULL NULL 3
  2152. 1 PRIMARY t2 ref b b 5 test.t5.b 2 Using where; FirstMatch(t5)
  2153. 1 PRIMARY t3 ALL NULL NULL NULL NULL 15 Using where; Using join buffer (flat, BNL join)
  2154. SELECT * FROM t3 WHERE t3.a IN (SELECT t5.a FROM t2, t4, t5 WHERE t2.c = t5.a AND t2.b = t5.b);
  2155. a
  2156. 0
  2157. 0
  2158. 0
  2159. 0
  2160. 0
  2161. 0
  2162. 0
  2163. 0
  2164. 0
  2165. 0
  2166. 0
  2167. 0
  2168. 0
  2169. 0
  2170. 0
  2171. DROP TABLE t2, t3, t4, t5;
  2172. set @@optimizer_switch=@tmp834739;
  2173. #
  2174. # BUG#830993: Crash in end_read_record with derived table
  2175. #
  2176. set @tmp_830993=@@optimizer_switch;
  2177. set optimizer_switch='semijoin=on,loosescan=off,materialization=off,firstmatch=off';
  2178. set @tmp_830993_jbs= @@join_buffer_size;
  2179. set join_buffer_size=160;
  2180. CREATE TABLE t1 (
  2181. a int(11) NOT NULL AUTO_INCREMENT,
  2182. b int(11) DEFAULT NULL,
  2183. c int(11) DEFAULT NULL,
  2184. d time DEFAULT NULL,
  2185. e varchar(1) DEFAULT NULL,
  2186. f varchar(1) DEFAULT NULL,
  2187. PRIMARY KEY (a),
  2188. KEY c (c),
  2189. KEY d (d),
  2190. KEY e (e,c)
  2191. );
  2192. INSERT INTO t1 VALUES (10,NULL,8,'22:55:23','x','x'),
  2193. (11,8,7,'10:19:31','d','d'),(12,1,1,'14:40:36','r','r'),
  2194. (13,9,7,'04:37:47','f','f'),(14,4,9,'19:34:06','y','y'),
  2195. (15,3,NULL,'20:35:33','u','u'),(16,2,1,NULL,'m','m'),
  2196. (17,NULL,9,'14:43:37',NULL,NULL),(18,2,2,'02:23:09','o','o'),
  2197. (19,NULL,9,'01:22:45','w','w'),(20,6,2,'00:00:00','m','m'),
  2198. (21,7,4,'00:13:25','q','q'),(22,2,0,'03:47:16',NULL,NULL),
  2199. (23,5,4,'01:41:48','d','d'),(24,7,8,'00:00:00','g','g'),
  2200. (25,6,NULL,'22:32:04','x','x'),(26,6,NULL,'16:44:14','f','f'),
  2201. (27,2,0,'17:38:37','p','p'),(28,9,NULL,'08:46:48','j','j'),
  2202. (29,6,8,'14:11:27','c','c');
  2203. CREATE TABLE t2 like t1;
  2204. INSERT INTO t2 VALUES (1,2,4,'22:34:09','v','v'),
  2205. (2,150,62,'14:26:02','v','v'),(3,NULL,7,'14:03:03','c','c'),
  2206. (4,2,1,'01:46:09',NULL,NULL),(5,5,0,'16:21:18','x','x'),
  2207. (6,3,7,'18:56:33','i','i'),(7,1,7,NULL,'e','e'),
  2208. (8,4,1,'09:29:08','p','p'),(9,NULL,7,'19:11:10','s','s'),
  2209. (10,2,1,'11:57:26','j','j'),(11,6,5,'00:39:46','z','z'),
  2210. (12,6,2,'03:28:15','c','c'),(13,8,0,'06:44:18','a','a'),
  2211. (14,2,1,'14:36:39','q','q'),(15,6,8,'18:42:45','y','y'),
  2212. (16,8,1,'02:57:29',NULL,NULL),(17,3,1,'16:46:13','r','r'),
  2213. (18,3,9,'19:39:02','v','v'),(19,9,1,NULL,NULL,NULL),
  2214. (20,6,5,'20:58:33','r','r');
  2215. set @tmp_optimizer_switch=@@optimizer_switch;
  2216. set optimizer_switch='derived_merge=off,derived_with_keys=off';
  2217. explain
  2218. SELECT
  2219. alias1.a, alias1.b, alias1.c, alias1.d, alias1.e, alias1.f,
  2220. alias2.a as a2_a, alias2.b as a2_b, alias2.c as a2_c, alias2.d as a2_d,
  2221. alias2.e as a2_e, alias2.f as a2_f,
  2222. 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
  2223. FROM
  2224. (SELECT * FROM t2) AS alias1,
  2225. t1 AS alias2,
  2226. t2
  2227. WHERE
  2228. alias1.c IN (SELECT SQ3_alias1.b
  2229. FROM t2 AS SQ3_alias1 STRAIGHT_JOIN t2 AS SQ3_alias2)
  2230. LIMIT 100;
  2231. id select_type table type possible_keys key key_len ref rows Extra
  2232. 1 PRIMARY <derived2> ALL NULL NULL NULL NULL 20
  2233. 1 PRIMARY alias2 ALL NULL NULL NULL NULL 20 Using join buffer (flat, BNL join)
  2234. 1 PRIMARY t2 ALL NULL NULL NULL NULL 20 Using join buffer (flat, BNL join)
  2235. 1 PRIMARY SQ3_alias1 ALL NULL NULL NULL NULL 20 Using where; Start temporary
  2236. 1 PRIMARY SQ3_alias2 index NULL PRIMARY 4 NULL 20 Using index; End temporary
  2237. 2 DERIVED t2 ALL NULL NULL NULL NULL 20
  2238. create table t3 as
  2239. SELECT
  2240. alias1.a, alias1.b, alias1.c, alias1.d, alias1.e, alias1.f,
  2241. alias2.a as a2_a, alias2.b as a2_b, alias2.c as a2_c, alias2.d as a2_d,
  2242. alias2.e as a2_e, alias2.f as a2_f,
  2243. 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
  2244. FROM
  2245. (SELECT * FROM t2) AS alias1,
  2246. t1 AS alias2,
  2247. t2
  2248. WHERE
  2249. alias1.c IN (SELECT SQ3_alias1.b
  2250. FROM t2 AS SQ3_alias1 STRAIGHT_JOIN t2 AS SQ3_alias2)
  2251. LIMIT 100;
  2252. set optimizer_switch=@tmp_optimizer_switch;
  2253. drop table t1,t2, t3;
  2254. set optimizer_switch=@tmp_830993;
  2255. set join_buffer_size= @tmp_830993_jbs;
  2256. #
  2257. # BUG##849717: Crash in Item_func::fix_fields on second execution of a prepared statement with semijoin
  2258. #
  2259. CREATE TABLE t1 (a int);
  2260. CREATE TABLE t2 (a int);
  2261. CREATE TABLE t3 (a int, b int) ;
  2262. PREPARE st1 FROM "SELECT * FROM t2 LEFT JOIN t1 ON t2.a != 0 AND ('j','r') IN ( SELECT b,a FROM t3)";
  2263. EXECUTE st1;
  2264. a a
  2265. EXECUTE st1;
  2266. a a
  2267. DROP TABLE t1, t2, t3;
  2268. #
  2269. # BUG#849776: Wrong result with semijoin + "Impossible where"
  2270. #
  2271. CREATE TABLE t1 ( b varchar(1), a integer) ;
  2272. INSERT INTO t1 VALUES ('z',8);
  2273. CREATE TABLE t2 ( a integer, b varchar(1)) ;
  2274. CREATE TABLE t4 ( a integer, b varchar(1)) ;
  2275. CREATE TABLE t5 ( a integer) ;
  2276. INSERT INTO t5 VALUES (8);
  2277. select * from t5 where (a) in (
  2278. SELECT t1.a
  2279. FROM t1 LEFT JOIN t2 ON t1.a = t2.a
  2280. WHERE t2.b NOT IN (SELECT t4.b FROM t4 WHERE t4.b < t1.b)
  2281. );
  2282. a
  2283. 8
  2284. DROP TABLE t1, t2, t4, t5;
  2285. #
  2286. # BUG#861147: Assertion `fixed == 1' failed in Item_func_eq::val_int() with semijoin + materialization + max_join_size
  2287. #
  2288. create table t1 (a int);
  2289. insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  2290. set @tmp_mjs_861147= @@max_join_size;
  2291. SET max_join_size=10;
  2292. set @tmp_os_861147= @@optimizer_switch;
  2293. set @@optimizer_switch='semijoin=on,materialization=on';
  2294. explain
  2295. select * from t1 where a in (select max(A.a + B.a + C.a) from t1 A, t1 B, t1 C);
  2296. ERROR 42000: The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay
  2297. set max_join_size= @tmp_mjs_861147;
  2298. set optimizer_switch= @tmp_os_861147;
  2299. drop table t1;
  2300. #
  2301. # BUG#877288: Wrong result with semijoin + materialization + multipart key
  2302. #
  2303. set @tmp_877288=@@optimizer_switch;
  2304. set optimizer_switch='semijoin=ON,materialization=ON';
  2305. CREATE TABLE t1 ( a int) ;
  2306. INSERT INTO t1 VALUES (19),(19),(19),(20),(20),(20),(20),(20),(20);
  2307. CREATE TABLE t2 ( b int NOT NULL , c int NOT NULL , KEY (b,c)) ;
  2308. INSERT INTO t2 VALUES (14,1),(15,1),(16,1),(17,1),(18,1),(19,1),(20,1);
  2309. CREATE TABLE t3 ( a int, d int) ;
  2310. 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);
  2311. explain
  2312. SELECT * FROM t1 WHERE (a) IN (SELECT a FROM t2 JOIN t3 ON b = a);
  2313. id select_type table type possible_keys key key_len ref rows Extra
  2314. 1 PRIMARY t1 ALL NULL NULL NULL NULL 9
  2315. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
  2316. 2 MATERIALIZED t3 ALL NULL NULL NULL NULL 13 Using where
  2317. 2 MATERIALIZED t2 ref b b 4 test.t3.a 1 Using index
  2318. SELECT * FROM t1 WHERE (a) IN (SELECT a FROM t2 JOIN t3 ON b = a);
  2319. a
  2320. 19
  2321. 19
  2322. 19
  2323. 20
  2324. 20
  2325. 20
  2326. 20
  2327. 20
  2328. 20
  2329. DROP TABLE t1,t2,t3;
  2330. set optimizer_switch=@tmp_877288;
  2331. #
  2332. # BUG#878753: Assertion '0' failed in replace_where_subcondition with derived_merge
  2333. #
  2334. set @tmp878753= @@optimizer_switch;
  2335. set optimizer_switch= 'semijoin=on,derived_merge=on';
  2336. CREATE TABLE t1 (b int(11)) ;
  2337. CREATE TABLE t2 (c int, b int, d varchar(52) NOT NULL) ;
  2338. CREATE TABLE t3 (b int(11)) ;
  2339. PREPARE st1 FROM '
  2340. SELECT * FROM t1
  2341. JOIN (
  2342. SELECT t2.* FROM t2
  2343. WHERE t2.d <> "a"
  2344. AND t2.c IN (
  2345. SELECT t3.b
  2346. FROM t3
  2347. )
  2348. ) AS alias2
  2349. ON ( alias2.b = t1.b );
  2350. ';
  2351. EXECUTE st1;
  2352. b c b d
  2353. DROP TABLE t1,t2,t3;
  2354. set optimizer_switch=@tmp878753;
  2355. #
  2356. # Bug #889750: semijoin=on + firstmatch=off + semijoin_with_cache=off
  2357. #
  2358. create table t1 (a int);
  2359. insert into t1 values (7), (1), (5), (3);
  2360. create table t2 (a int);
  2361. insert into t2 values (4), (1), (8), (3), (9), (2);
  2362. set @tmp_otimizer_switch= @@optimizer_switch;
  2363. set optimizer_switch='semijoin=on';
  2364. set optimizer_switch='firstmatch=off';
  2365. set optimizer_switch='semijoin_with_cache=on';
  2366. explain
  2367. select * from t1 where t1.a in (select t2.a from t2);
  2368. id select_type table type possible_keys key key_len ref rows Extra
  2369. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4
  2370. 1 PRIMARY t2 ALL NULL NULL NULL NULL 6 Using where; Start temporary; End temporary; Using join buffer (flat, BNL join)
  2371. select * from t1 where t1.a in (select t2.a from t2);
  2372. a
  2373. 1
  2374. 3
  2375. set optimizer_switch='semijoin_with_cache=off';
  2376. explain
  2377. select * from t1 where t1.a in (select t2.a from t2);
  2378. id select_type table type possible_keys key key_len ref rows Extra
  2379. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4
  2380. 1 PRIMARY t2 ALL NULL NULL NULL NULL 6 Using where; Start temporary; End temporary
  2381. select * from t1 where t1.a in (select t2.a from t2);
  2382. a
  2383. 1
  2384. 3
  2385. set optimizer_switch= @tmp_otimizer_switch;
  2386. drop table t1,t2;
  2387. #
  2388. # Bug #887496: semijoin with IN equality for the second part of an index
  2389. #
  2390. CREATE TABLE t1 (a int);
  2391. INSERT INTO t1 VALUES (9), (0), (8), (5);
  2392. CREATE TABLE t2 (a int, b varchar(1), INDEX idx (b,a));
  2393. INSERT INTO t2 VALUES (5,'r'), (5,'z');
  2394. CREATE TABLE t3 (a int, b varchar(1), INDEX idx (b,a));
  2395. INSERT INTO t3 VALUES (5,'r'), (5,'z');
  2396. set @tmp_otimizer_switch= @@optimizer_switch;
  2397. SET SESSION optimizer_switch='semijoin=on,firstmatch=on';
  2398. SET SESSION optimizer_switch='loosescan=off';
  2399. EXPLAIN
  2400. SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b);
  2401. id select_type table type possible_keys key key_len ref rows Extra
  2402. 1 PRIMARY t2 index idx idx 9 NULL 2 Using where; Using index; Start temporary
  2403. 1 PRIMARY t3 ref idx idx 4 test.t2.b 1 Using index
  2404. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; End temporary; Using join buffer (flat, BNL join)
  2405. SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b);
  2406. a
  2407. 5
  2408. SET SESSION optimizer_switch='loosescan=on';
  2409. EXPLAIN
  2410. SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b);
  2411. id select_type table type possible_keys key key_len ref rows Extra
  2412. 1 PRIMARY t2 index idx idx 9 NULL 2 Using where; Using index; Start temporary
  2413. 1 PRIMARY t3 ref idx idx 4 test.t2.b 1 Using index
  2414. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where; End temporary; Using join buffer (flat, BNL join)
  2415. SELECT * FROM t1 WHERE a IN (SELECT t2.a FROM t2,t3 WHERE t2.b = t3.b);
  2416. a
  2417. 5
  2418. set optimizer_switch= @tmp_otimizer_switch;
  2419. DROP TABLE t1,t2,t3;
  2420. #
  2421. # Bug #901312: materialized semijoin + right join
  2422. #
  2423. CREATE TABLE t1 (a int);
  2424. INSERT INTO t1 VALUES (4), (1);
  2425. CREATE TABLE t2 (b int);
  2426. INSERT INTO t2 VALUES (4), (1);
  2427. CREATE TABLE t3 (c int);
  2428. INSERT INTO t3 VALUES (4), (1);
  2429. set @tmp_otimizer_switch= @@optimizer_switch;
  2430. SET SESSION optimizer_switch='semijoin=on,materialization=on';
  2431. EXPLAIN
  2432. SELECT * FROM t1 RIGHT JOIN t2 ON b = a WHERE t2.b IN (SELECT c FROM t3);
  2433. id select_type table type possible_keys key key_len ref rows Extra
  2434. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2
  2435. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
  2436. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
  2437. 2 MATERIALIZED t3 ALL NULL NULL NULL NULL 2
  2438. SELECT * FROM t1 RIGHT JOIN t2 ON b = a WHERE t2.b IN (SELECT c FROM t3);
  2439. a b
  2440. 4 4
  2441. 1 1
  2442. set optimizer_switch= @tmp_otimizer_switch;
  2443. DROP TABLE t1,t2,t3;
  2444. #
  2445. # Bug #901709: assertion failure with record count == 0
  2446. #
  2447. CREATE TABLE t1 (a int, KEY (a));
  2448. INSERT INTO t1 VALUES (4), (6);
  2449. CREATE TABLE t2 (a int, KEY (a));
  2450. INSERT INTO t2 VALUES (4), (6);
  2451. CREATE TABLE t3 (b int);
  2452. INSERT INTO t3 VALUES (4);
  2453. CREATE TABLE t4 (c int);
  2454. SET @tmp_optimizer_switch=@@optimizer_switch;
  2455. SET @@optimizer_switch='semijoin=on';
  2456. SET @@optimizer_switch='materialization=on';
  2457. SET @@optimizer_switch='firstmatch=on';
  2458. SET optimizer_switch='semijoin_with_cache=on';
  2459. SET optimizer_prune_level=0;
  2460. EXPLAIN
  2461. SELECT * FROM t1, t2
  2462. WHERE t1.a = t2.a AND t2.a IN (SELECT b FROM t3 STRAIGHT_JOIN t4);
  2463. id select_type table type possible_keys key key_len ref rows Extra
  2464. 1 PRIMARY t3 system NULL NULL NULL NULL 1
  2465. 1 PRIMARY <subquery2> ALL distinct_key NULL NULL NULL 1
  2466. 1 PRIMARY t1 ref a a 5 const 1 Using index
  2467. 1 PRIMARY t2 ref a a 5 test.t3.b 1 Using index
  2468. 2 MATERIALIZED t4 ALL NULL NULL NULL NULL 0
  2469. SELECT * FROM t1, t2
  2470. WHERE t1.a = t2.a AND t2.a IN (SELECT b FROM t3 STRAIGHT_JOIN t4);
  2471. a a
  2472. SET optimizer_prune_level=DEFAULT;
  2473. SET optimizer_switch=@tmp_optimizer_switch;
  2474. DROP TABLE t1,t2,t3,t4;
  2475. #
  2476. # BUG#901399: Wrong result (extra row) with semijoin=ON, materialization=OFF, optimizer_prune_level=0
  2477. #
  2478. set @opl_901399= @@optimizer_prune_level;
  2479. set @os_091399= @@optimizer_switch;
  2480. SET optimizer_prune_level=0;
  2481. SET optimizer_switch = 'materialization=off';
  2482. CREATE TABLE t1 ( c INT ) ENGINE=MyISAM;
  2483. INSERT INTO t1 VALUES
  2484. (0),(1),(2),(3),(4),(5),
  2485. (6),(7),(8),(9),(10),(11),(12);
  2486. CREATE TABLE t2 ( a INT, b INT, KEY(a)) ENGINE=MyISAM;
  2487. INSERT INTO t2 VALUES (3,20),(2,21),(3,22);
  2488. SELECT *
  2489. FROM t1 AS alias1, t1 AS alias2
  2490. WHERE ( alias1.c, alias2.c )
  2491. IN (
  2492. SELECT alias3.a, alias3.a
  2493. FROM t2 AS alias3, t2 alias4
  2494. WHERE alias3.b = alias4.b
  2495. );
  2496. c c
  2497. 2 2
  2498. 3 3
  2499. set optimizer_prune_level= @opl_901399;
  2500. set optimizer_switch= @os_091399;
  2501. DROP TABLE t1,t2;
  2502. #
  2503. # BUG#912510: Crash in do_copy_not_null with semijoin=ON, firstmatch=ON, aggregate ...
  2504. #
  2505. CREATE TABLE t1 ( a VARCHAR(1) NOT NULL );
  2506. INSERT INTO t1 VALUES ('k'),('l');
  2507. CREATE TABLE t2 ( b VARCHAR(1) NOT NULL, KEY(b) );
  2508. INSERT INTO t2 VALUES ('k'),('l');
  2509. CREATE TABLE t3 ( c VARCHAR(1) NOT NULL, KEY(c) );
  2510. INSERT INTO t3 VALUES ('m'),('n');
  2511. SELECT a, COUNT(*) FROM t1
  2512. WHERE a IN (
  2513. SELECT b FROM t2 force index(b), t3 force index(c)
  2514. WHERE c = b AND b = a
  2515. );
  2516. a COUNT(*)
  2517. NULL 0
  2518. DROP TABLE t1, t2, t3;
  2519. #
  2520. # BUG#920255: Wrong result (extra rows) with loosescan and IN subquery
  2521. #
  2522. CREATE TABLE t1 ( a INT PRIMARY KEY, b INT, KEY(b) );
  2523. INSERT INTO t1 VALUES
  2524. (1,2),(2,1),(3,3),(4,2),(5,5),
  2525. (6,3),(7,1),(8,4),(9,3),(10,2);
  2526. CREATE TABLE t2 ( c INT, d INT, UNIQUE KEY(c) );
  2527. INSERT INTO t2 VALUES
  2528. (1,2),(2,1),(3,3),(4,2),(5,5),(6,3),(7,1);
  2529. SELECT a, b, d FROM t1, t2
  2530. WHERE ( b, d ) IN
  2531. ( SELECT b, d FROM t1, t2 WHERE b = c );
  2532. a b d
  2533. 2 1 2
  2534. 7 1 2
  2535. 2 1 2
  2536. 7 1 2
  2537. 1 2 1
  2538. 4 2 1
  2539. 10 2 1
  2540. 1 2 1
  2541. 4 2 1
  2542. 10 2 1
  2543. 3 3 3
  2544. 6 3 3
  2545. 9 3 3
  2546. 3 3 3
  2547. 6 3 3
  2548. 9 3 3
  2549. 8 4 2
  2550. 8 4 2
  2551. 5 5 5
  2552. DROP TABLE t1, t2;
  2553. #
  2554. # BUG#920713: Wrong result (missing rows) with firstmatch+BNL, IN subquery, ...
  2555. #
  2556. CREATE TABLE t1 ( a VARCHAR(1) ) ENGINE=MyISAM;
  2557. INSERT INTO t1 VALUES ('e'),('w'),('a'),('h'),('x'),('k'),('g');
  2558. CREATE TABLE t2 ( b INT, c VARCHAR(1) );
  2559. INSERT INTO t2 VALUES (0,'j'),(8,'v');
  2560. SELECT * FROM t1 alias1, t2 alias2
  2561. WHERE alias2.c IN (
  2562. SELECT alias4.c FROM t1 alias3, t2 alias4
  2563. );
  2564. a b c
  2565. e 0 j
  2566. e 8 v
  2567. w 0 j
  2568. w 8 v
  2569. a 0 j
  2570. a 8 v
  2571. h 0 j
  2572. h 8 v
  2573. x 0 j
  2574. x 8 v
  2575. k 0 j
  2576. k 8 v
  2577. g 0 j
  2578. g 8 v
  2579. DROP TABLE t1, t2;
  2580. #
  2581. # BUG#923246: Loosescan reports different result than other semijoin methods
  2582. #
  2583. set @tmp_923246= @@optimizer_switch;
  2584. set optimizer_switch='mrr=on,materialization=off';
  2585. create table t0 (a int);
  2586. insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  2587. create table t1 (kp1 int, kp2 int, c int, filler char(100), key(kp1, kp2));
  2588. insert into t1 select A.a+10*(B.a+10*C.a), 0, 0, 'filler' from t0 A, t0 B, t0 C;
  2589. insert into t1 select * from t1 where kp1 < 20;
  2590. create table t3 (a int);
  2591. insert into t3 select A.a + 10*B.a from t0 A, t0 B;
  2592. select * from t3 where a in (select kp1 from t1 where kp1<20);
  2593. a
  2594. 0
  2595. 1
  2596. 2
  2597. 3
  2598. 4
  2599. 5
  2600. 6
  2601. 7
  2602. 8
  2603. 9
  2604. 10
  2605. 11
  2606. 12
  2607. 13
  2608. 14
  2609. 15
  2610. 16
  2611. 17
  2612. 18
  2613. 19
  2614. explain select * from t3 where a in (select kp1 from t1 where kp1<20);
  2615. id select_type table type possible_keys key key_len ref rows Extra
  2616. 1 PRIMARY t1 range kp1 kp1 5 NULL 48 Using where; Using index; LooseScan
  2617. 1 PRIMARY t3 ALL NULL NULL NULL NULL 100 Using where; Using join buffer (flat, BNL join)
  2618. drop table t0,t1,t3;
  2619. set optimizer_switch= @tmp_923246;
  2620. set optimizer_switch=@subselect_sj_tmp;