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.

2536 lines
97 KiB

15 years ago
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
Patch for mdev-287: CHEAP SQ: A query with subquery in SELECT list, EXISTS, inner joins takes hundreds times longer Analysis: The fix for lp:944706 introduces early subquery optimization. While a subquery is being optimized some of its predicates may be removed. In the test case, the EXISTS subquery is constant, and is evaluated to TRUE. As a result the whole OR is TRUE, and thus the correlated condition "b = alias1.b" is optimized away. The subquery becomes non-correlated. The subquery cache is designed to work only for correlated subqueries. If constant subquery optimization is disallowed, then the constant subquery is not evaluated, the subquery remains correlated, and its execution is cached. As a result execution is fast. However, when the constant subquery was optimized away, it was neither cached by the subquery cache, nor it was cached by the internal subquery caching. The latter was due to the fact that the subquery still appeared as correlated to the subselect_XYZ_engine::exec methods, and they re-executed the subquery on each call to Item_subselect::exec. Solution: The solution is to update the correlated status of the subquery after it has been optimized. This status consists of: - st_select_lex::is_correlated - Item_subselect::is_correlated - SELECT_LEX::uncacheable - SELECT_LEX_UNIT::uncacheable The status is updated by st_select_lex::update_correlated_cache(), and its caller st_select_lex::optimize_unflattened_subqueries. The solution relies on the fact that the optimizer already called st_select_lex::update_used_tables() for each subquery. This allows to efficiently update the correlated status of each subquery without walking the whole subquery tree. Notice that his patch is an improvement over MySQL 5.6 and older, where subqueries are not pre-optimized, and the above analysis is not possible.
14 years ago
14 years ago
14 years ago
14 years ago
14 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
13 years ago
14 years ago
Fix LP BUG#718593 Analysis: Build_equal_items_for_cond() rewrites the WHERE clause in such a way, that it may merge the list join->cond_equal->current_level with the list of child Items in an AND condition of the WHERE clause. The place where this is done is: static COND *build_equal_items_for_cond(THD *thd, COND *cond, COND_EQUAL *inherited) { ... if (and_level) { args->concat(&eq_list); args->concat((List<Item> *)&cond_equal.current_level); } ... } As a result, later transformations on the WHERE clause may change the structure of the list join->cond_equal->current_level without knowing this. Specifically in this bug, Item_in_subselect::inject_in_to_exists_cond creates a new AND of the old WHERE clause and the IN->EXISTS conditions. It then calls fix_fields() for the new AND. Among other things, fix_fields flattens all nested ANDs into one by merging the AND argument lists. When there is a cond_equal for the JOIN, its list of Item_equal objects is attached to the end of the original AND. When a lower-level AND is merged into the top-level one, the argument list of the lower-level AND is concatenated to the list of multiple equalities in the upper-level AND. As a result, when substitute_for_best_equal_field processes the multiple equalities, it turns out that the multiple equality list contains the Items from the lower-level AND which were concatenated to the end of the join->cond_equal->current_level list. This results in a crash because this list must not contain any other Items except for the previously found Item_equal ones. Solution: When performing IN->EXIST predicate injection, and the where clause is an AND, detach the list of Item_equal objects before calling fix_fields on the injected where clause. After fix_fields is done, reattach back the multiple equalities list to the end of the argument list of the new AND.
15 years ago
14 years ago
14 years ago
13 years ago
13 years ago
12 years ago
12 years ago
12 years ago
Fixed lp:879939 "assertion in ha_maria::enable_indexes with derived_with_keys=on" Honor unique/not unique when creating keys for internal tempory tables. Added new variables to be used to limit how keys are created for internal temporary tables. include/maria.h: Added maria_max_key_length() and maria_max_key_segments() include/myisam.h: Added myisam_max_key_length() and myisam_max_key_segments() mysql-test/r/mysql.result: Drop all used tables mysql-test/r/subselect4.result: Added test case for lp:879939 mysql-test/t/mysql.test: Drop all used tables mysql-test/t/subselect4.test: Added test case for lp:879939 sql/mysql_priv.h: Added internal_tmp_table_max_key_length and internal_tmp_table_max_key_segments to be used to limit how keys for derived tables are created. sql/mysqld.cc: Added internal_tmp_table_max_key_length and internal_tmp_table_max_key_segments to be used to limit how keys for derived tables are created. sql/share/errmsg.txt: Added new error message for internal errors sql/sql_select.cc: Give error if we try to create a wrong key (this error should never happen) Honor unique/not unique when creating keys for internal tempory tables. storage/maria/ha_maria.cc: Added change_table_ptr() to ensure that external_ref points always to the correct table. (Not having this caused an assert in the included test) storage/maria/ha_maria.h: Added change_table_ptr() to ensure that external_ref points always to the correct table. storage/maria/ma_check.c: Fixed bug in Duplicate key error printing (now row position is printed correctly) storage/maria/ma_create.c: maria_max_key_length() -> _ma_max_key_length() storage/maria/ma_info.c: Added extern function maria_max_key_length() to calculate the max key length based on current block size. storage/maria/ma_open.c: maria_max_key_length() -> _ma_max_key_length() storage/maria/maria_def.h: maria_max_key_length() -> _ma_max_key_length() storage/myisam/ha_myisam.cc: Added change_table_ptr() to ensure that external_ref points always to the correct table. (Not having this caused an assert in the included test) storage/myisam/ha_myisam.h: Added change_table_ptr() to ensure that external_ref points always to the correct table.
14 years ago
  1. drop table if exists t0,t1,t2,t3,t4,t5,t6;
  2. drop view if exists v1, v2;
  3. set @subselect4_tmp= @@optimizer_switch;
  4. set optimizer_switch='semijoin=on,firstmatch=on,loosescan=on';
  5. set optimizer_switch='semijoin_with_cache=on';
  6. set optimizer_switch='mrr=on,mrr_sort_keys=on,index_condition_pushdown=on';
  7. #
  8. # Bug #46791: Assertion failed:(table->key_read==0),function unknown
  9. # function,file sql_base.cc
  10. #
  11. CREATE TABLE t1 (a INT, b INT, KEY(a));
  12. INSERT INTO t1 VALUES (1,1),(2,2);
  13. CREATE TABLE t2 LIKE t1;
  14. INSERT INTO t2 VALUES (1,1),(2,2);
  15. CREATE TABLE t3 LIKE t1;
  16. # should have 1 impossible where and 2 dependent subqueries
  17. EXPLAIN
  18. SELECT 1 FROM t1
  19. WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE 1 = (SELECT MIN(t2.b) FROM t3))
  20. ORDER BY count(*);
  21. id select_type table type possible_keys key key_len ref rows Extra
  22. 1 PRIMARY t1 index NULL a 5 NULL 2 Using index
  23. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where
  24. 3 DEPENDENT SUBQUERY t3 system NULL NULL NULL NULL 0 const row not found
  25. # should not crash the next statement
  26. SELECT 1 FROM t1
  27. WHERE NOT EXISTS (SELECT 1 FROM t2 WHERE 1 = (SELECT MIN(t2.b) FROM t3))
  28. ORDER BY count(*);
  29. 1
  30. 1
  31. # should not crash: the crash is caused by the previous statement
  32. SELECT 1;
  33. 1
  34. 1
  35. DROP TABLE t1,t2,t3;
  36. #
  37. # Bug #47106: Crash / segfault on adding EXPLAIN to a non-crashing
  38. # query
  39. #
  40. CREATE TABLE t1 (
  41. a INT,
  42. b INT,
  43. PRIMARY KEY (a),
  44. KEY b (b)
  45. );
  46. INSERT INTO t1 VALUES (1, 1), (2, 1);
  47. CREATE TABLE t2 LIKE t1;
  48. INSERT INTO t2 SELECT * FROM t1;
  49. CREATE TABLE t3 LIKE t1;
  50. INSERT INTO t3 SELECT * FROM t1;
  51. # Should not crash.
  52. # Should have 1 impossible where and 2 dependent subqs.
  53. EXPLAIN
  54. SELECT
  55. (SELECT 1 FROM t1,t2 WHERE t2.b > t3.b)
  56. FROM t3 WHERE 1 = 0 GROUP BY 1;
  57. id select_type table type possible_keys key key_len ref rows Extra
  58. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  59. 2 DEPENDENT SUBQUERY t1 index NULL PRIMARY 4 NULL 2 Using index
  60. 2 DEPENDENT SUBQUERY t2 ALL b NULL NULL NULL 2 Range checked for each record (index map: 0x2)
  61. # should return 0 rows
  62. SELECT
  63. (SELECT 1 FROM t1,t2 WHERE t2.b > t3.b)
  64. FROM t3 WHERE 1 = 0 GROUP BY 1;
  65. (SELECT 1 FROM t1,t2 WHERE t2.b > t3.b)
  66. DROP TABLE t1,t2,t3;
  67. End of 5.0 tests.
  68. CREATE TABLE t1 (col_int_nokey int(11) NOT NULL, col_varchar_nokey varchar(1) NOT NULL) engine=myisam;
  69. INSERT INTO t1 VALUES (2,'s'),(0,'v'),(2,'s');
  70. CREATE TABLE t2 (
  71. pk int(11) NOT NULL AUTO_INCREMENT,
  72. `col_int_key` int(11) NOT NULL,
  73. col_varchar_key varchar(1) NOT NULL,
  74. PRIMARY KEY (pk),
  75. KEY `col_int_key` (`col_int_key`),
  76. KEY `col_varchar_key` (`col_varchar_key`)
  77. ) ENGINE=MyISAM;
  78. INSERT INTO t2 VALUES (4,10,'g'), (5,20,'v');
  79. SELECT t1.col_int_nokey,(SELECT MIN( t2_a.col_int_key ) FROM t2 t2_a, t2 t2_b, t1 t1_a WHERE t1_a.col_varchar_nokey = t2_b.col_varchar_key and t1.col_int_nokey ) as sub FROM t1;
  80. col_int_nokey sub
  81. 2 10
  82. 0 NULL
  83. 2 10
  84. SELECT t1.col_int_nokey,(SELECT MIN( t2_a.col_int_key ) +1 FROM t2 t2_a, t2 t2_b, t1 t1_a WHERE t1_a.col_varchar_nokey = t2_b.col_varchar_key and t1.col_int_nokey ) as sub FROM t1;
  85. col_int_nokey sub
  86. 2 11
  87. 0 NULL
  88. 2 11
  89. DROP TABLE t1,t2;
  90. #
  91. # Bug#54568: create view cause Assertion failed: 0,
  92. # file .\item_subselect.cc, line 836
  93. #
  94. EXPLAIN SELECT 1 LIKE ( 1 IN ( SELECT 1 ) );
  95. id select_type table type possible_keys key key_len ref rows Extra
  96. 1 SIMPLE NULL NULL NULL NULL NULL NULL NULL No tables used
  97. Warnings:
  98. Note 1249 Select 2 was reduced during optimization
  99. DESCRIBE SELECT 1 LIKE ( 1 IN ( SELECT 1 ) );
  100. id select_type table type possible_keys key key_len ref rows Extra
  101. 1 SIMPLE NULL NULL NULL NULL NULL NULL NULL No tables used
  102. Warnings:
  103. Note 1249 Select 2 was reduced during optimization
  104. # None of the below should crash
  105. CREATE VIEW v1 AS SELECT 1 LIKE ( 1 IN ( SELECT 1 ) );
  106. CREATE VIEW v2 AS SELECT 1 LIKE '%' ESCAPE ( 1 IN ( SELECT 1 ) );
  107. DROP VIEW v1, v2;
  108. #
  109. # Bug#51070: Query with a NOT IN subquery predicate returns a wrong
  110. # result set
  111. #
  112. CREATE TABLE t1 ( a INT, b INT );
  113. INSERT INTO t1 VALUES ( 1, NULL ), ( 2, NULL );
  114. CREATE TABLE t2 ( c INT, d INT );
  115. INSERT INTO t2 VALUES ( NULL, 3 ), ( NULL, 4 );
  116. CREATE TABLE t3 ( e INT, f INT );
  117. INSERT INTO t3 VALUES ( NULL, NULL ), ( NULL, NULL );
  118. CREATE TABLE t4 ( a INT );
  119. INSERT INTO t4 VALUES (1), (2), (3);
  120. CREATE TABLE t5 ( a INT );
  121. INSERT INTO t5 VALUES (NULL), (2);
  122. SET @old_optimizer_switch = @@session.optimizer_switch;
  123. SET SESSION optimizer_switch = 'materialization=off,in_to_exists=on,semijoin=off';
  124. EXPLAIN
  125. SELECT * FROM t1 WHERE ( a, b ) NOT IN ( SELECT c, d FROM t2 );
  126. id select_type table type possible_keys key key_len ref rows Extra
  127. x PRIMARY x x x x x x x x
  128. x DEPENDENT SUBQUERY x x x x x x x x
  129. SELECT * FROM t1 WHERE ( a, b ) NOT IN ( SELECT c, d FROM t2 );
  130. a b
  131. EXPLAIN
  132. SELECT * FROM t1 WHERE ( a, b ) NOT IN ( SELECT c, d FROM t2 ) IS NULL;
  133. id select_type table type possible_keys key key_len ref rows Extra
  134. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
  135. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where
  136. SELECT * FROM t1 WHERE ( a, b ) NOT IN ( SELECT c, d FROM t2 ) IS NULL;
  137. a b
  138. 1 NULL
  139. 2 NULL
  140. SELECT * FROM t1 WHERE ( a, b ) IN ( SELECT c, d FROM t2 ) IS NULL;
  141. a b
  142. 1 NULL
  143. 2 NULL
  144. SELECT * FROM t1 WHERE ( a, b ) NOT IN ( SELECT c, d FROM t2 ) IS UNKNOWN;
  145. a b
  146. 1 NULL
  147. 2 NULL
  148. SELECT * FROM t1 WHERE (( a, b ) NOT IN ( SELECT c, d FROM t2 )) IS UNKNOWN;
  149. a b
  150. 1 NULL
  151. 2 NULL
  152. SELECT * FROM t1 WHERE 1 = 1 AND ( a, b ) NOT IN ( SELECT c, d FROM t2 );
  153. a b
  154. EXPLAIN
  155. SELECT * FROM t1 WHERE ( a, b ) NOT IN ( SELECT e, f FROM t3 );
  156. id select_type table type possible_keys key key_len ref rows Extra
  157. x PRIMARY x x x x x x x x
  158. x DEPENDENT SUBQUERY x x x x x x x x
  159. SELECT * FROM t1 WHERE ( a, b ) NOT IN ( SELECT e, f FROM t3 );
  160. a b
  161. EXPLAIN
  162. SELECT * FROM t2 WHERE ( c, d ) NOT IN ( SELECT a, b FROM t1 );
  163. id select_type table type possible_keys key key_len ref rows Extra
  164. x PRIMARY x x x x x x x x
  165. x DEPENDENT SUBQUERY x x x x x x x x
  166. SELECT * FROM t2 WHERE ( c, d ) NOT IN ( SELECT a, b FROM t1 );
  167. c d
  168. EXPLAIN
  169. SELECT * FROM t3 WHERE ( e, f ) NOT IN ( SELECT c, d FROM t2 );
  170. id select_type table type possible_keys key key_len ref rows Extra
  171. x PRIMARY x x x x x x x x
  172. x DEPENDENT SUBQUERY x x x x x x x x
  173. SELECT * FROM t3 WHERE ( e, f ) NOT IN ( SELECT c, d FROM t2 );
  174. e f
  175. EXPLAIN
  176. SELECT * FROM t2 WHERE ( c, d ) NOT IN ( SELECT e, f FROM t3 );
  177. id select_type table type possible_keys key key_len ref rows Extra
  178. x PRIMARY x x x x x x x x
  179. x DEPENDENT SUBQUERY x x x x x x x x
  180. SELECT * FROM t2 WHERE ( c, d ) NOT IN ( SELECT e, f FROM t3 );
  181. c d
  182. SELECT * FROM t1 WHERE ( a, b ) NOT IN
  183. ( SELECT c, d FROM t2 WHERE c = 1 AND c <> 1 );
  184. a b
  185. 1 NULL
  186. 2 NULL
  187. SELECT * FROM t1 WHERE b NOT IN ( SELECT c FROM t2 WHERE c = 1 );
  188. a b
  189. 1 NULL
  190. 2 NULL
  191. SELECT * FROM t1 WHERE NULL NOT IN ( SELECT c FROM t2 WHERE c = 1 AND c <> 1 );
  192. a b
  193. 1 NULL
  194. 2 NULL
  195. SET SESSION optimizer_switch = @old_optimizer_switch;
  196. DROP TABLE t1, t2, t3, t4, t5;
  197. #
  198. # Bug#58207: invalid memory reads when using default column value and
  199. # tmptable needed
  200. #
  201. CREATE TABLE t(a VARCHAR(245) DEFAULT
  202. 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
  203. INSERT INTO t VALUES (''),(''),(''),(''),(''),(''),(''),(''),(''),(''),('');
  204. SELECT * FROM (SELECT default(a) FROM t GROUP BY a) d;
  205. default(a)
  206. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  207. DROP TABLE t;
  208. #
  209. # LP BUG#1009187, MDEV-373, MYSQL bug#58628
  210. # Wrong result for a query with [NOT] IN subquery predicate if
  211. # the left part of the predicate is explicit NULL
  212. #
  213. CREATE TABLE t1 (pk INT NOT NULL, i INT NOT NULL);
  214. INSERT INTO t1 VALUES (0,10), (1,20), (2,30), (3,40);
  215. CREATE TABLE t2a (pk INT NOT NULL, i INT NOT NULL, PRIMARY KEY(i,pk));
  216. INSERT INTO t2a VALUES (0,0), (1,1), (2,2), (3,3);
  217. CREATE TABLE t2b (pk INT, i INT);
  218. INSERT INTO t2b VALUES (0,0), (1,1), (2,2), (3,3);
  219. CREATE TABLE t2c (pk INT NOT NULL, i INT NOT NULL);
  220. INSERT INTO t2c VALUES (0,0), (1,1), (2,2), (3,3);
  221. create index it2c on t2c (i,pk);
  222. CREATE TABLE t2d (pk INT NOT NULL, i INT NOT NULL, PRIMARY KEY(i));
  223. INSERT INTO t2d VALUES (0,0), (1,1), (2,2), (3,3);
  224. EXPLAIN
  225. SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2a.i FROM t2a WHERE t2a.pk = t1.pk);
  226. id select_type table type possible_keys key key_len ref rows Extra
  227. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
  228. 2 DEPENDENT SUBQUERY t2a unique_subquery PRIMARY PRIMARY 8 const,test.t1.pk 1 Using index; Using where; Full scan on NULL key
  229. SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2a.i FROM t2a WHERE t2a.pk = t1.pk);
  230. pk i
  231. SELECT * FROM t1 WHERE 1+NULL NOT IN (SELECT t2a.i FROM t2a WHERE t2a.pk = t1.pk);
  232. pk i
  233. SELECT * FROM t1 WHERE NULL IN (SELECT t2a.i FROM t2a WHERE t2a.pk = t1.pk) IS UNKNOWN;
  234. pk i
  235. 0 10
  236. 1 20
  237. 2 30
  238. 3 40
  239. SELECT t1.pk, NULL NOT IN (SELECT t2a.i FROM t2a WHERE t2a.pk = t1.pk) FROM t1;
  240. pk NULL NOT IN (SELECT t2a.i FROM t2a WHERE t2a.pk = t1.pk)
  241. 0 NULL
  242. 1 NULL
  243. 2 NULL
  244. 3 NULL
  245. EXPLAIN
  246. SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2b.i FROM t2b WHERE t2b.pk = t1.pk);
  247. id select_type table type possible_keys key key_len ref rows Extra
  248. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
  249. 2 DEPENDENT SUBQUERY t2b ALL NULL NULL NULL NULL 4 Using where
  250. SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2b.i FROM t2b WHERE t2b.pk = t1.pk);
  251. pk i
  252. SELECT * FROM t1 WHERE NULL IN (SELECT t2b.i FROM t2b WHERE t2b.pk = t1.pk) IS UNKNOWN;
  253. pk i
  254. 0 10
  255. 1 20
  256. 2 30
  257. 3 40
  258. SELECT t1.pk, NULL NOT IN (SELECT t2b.i FROM t2b WHERE t2b.pk = t1.pk) FROM t1;
  259. pk NULL NOT IN (SELECT t2b.i FROM t2b WHERE t2b.pk = t1.pk)
  260. 0 NULL
  261. 1 NULL
  262. 2 NULL
  263. 3 NULL
  264. EXPLAIN
  265. SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2c.i FROM t2c WHERE t2c.pk = t1.pk);
  266. id select_type table type possible_keys key key_len ref rows Extra
  267. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
  268. 2 DEPENDENT SUBQUERY t2c index_subquery it2c it2c 8 const,test.t1.pk 2 Using index; Using where; Full scan on NULL key
  269. SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2c.i FROM t2c WHERE t2c.pk = t1.pk);
  270. pk i
  271. SELECT * FROM t1 WHERE NULL IN (SELECT t2c.i FROM t2c WHERE t2c.pk = t1.pk) IS UNKNOWN;
  272. pk i
  273. 0 10
  274. 1 20
  275. 2 30
  276. 3 40
  277. SELECT t1.pk, NULL NOT IN (SELECT t2c.i FROM t2c WHERE t2c.pk = t1.pk) FROM t1;
  278. pk NULL NOT IN (SELECT t2c.i FROM t2c WHERE t2c.pk = t1.pk)
  279. 0 NULL
  280. 1 NULL
  281. 2 NULL
  282. 3 NULL
  283. EXPLAIN
  284. SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2d.i FROM t2d WHERE t2d.pk = t1.pk);
  285. id select_type table type possible_keys key key_len ref rows Extra
  286. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
  287. 2 DEPENDENT SUBQUERY t2d const PRIMARY PRIMARY 4 const 1 Using where; Full scan on NULL key
  288. SELECT * FROM t1 WHERE NULL NOT IN (SELECT t2d.i FROM t2d WHERE t2d.pk = t1.pk);
  289. pk i
  290. SELECT * FROM t1 WHERE NULL IN (SELECT t2d.i FROM t2d WHERE t2d.pk = t1.pk) IS UNKNOWN;
  291. pk i
  292. 0 10
  293. 1 20
  294. 2 30
  295. 3 40
  296. SELECT t1.pk, NULL NOT IN (SELECT t2d.i FROM t2d WHERE t2d.pk = t1.pk) FROM t1;
  297. pk NULL NOT IN (SELECT t2d.i FROM t2d WHERE t2d.pk = t1.pk)
  298. 0 NULL
  299. 1 NULL
  300. 2 NULL
  301. 3 NULL
  302. EXPLAIN
  303. SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2a.i, t2a.pk FROM t2a WHERE t2a.pk = t1.pk);
  304. id select_type table type possible_keys key key_len ref rows Extra
  305. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
  306. 2 DEPENDENT SUBQUERY t2a unique_subquery PRIMARY PRIMARY 8 const,test.t1.pk 1 Using index; Using where; Full scan on NULL key
  307. SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2a.i, t2a.pk FROM t2a WHERE t2a.pk = t1.pk);
  308. pk i
  309. 0 10
  310. 2 30
  311. 3 40
  312. SELECT (NULL, 1) NOT IN (SELECT t2a.i, t2a.pk FROM t2a WHERE t2a.pk = t1.pk) from t1;
  313. (NULL, 1) NOT IN (SELECT t2a.i, t2a.pk FROM t2a WHERE t2a.pk = t1.pk)
  314. 1
  315. NULL
  316. 1
  317. 1
  318. EXPLAIN
  319. SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2b.i, t2b.pk FROM t2b WHERE t2b.pk = t1.pk);
  320. id select_type table type possible_keys key key_len ref rows Extra
  321. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
  322. 2 DEPENDENT SUBQUERY t2b ALL NULL NULL NULL NULL 4 Using where
  323. SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2b.i, t2b.pk FROM t2b WHERE t2b.pk = t1.pk);
  324. pk i
  325. 0 10
  326. 2 30
  327. 3 40
  328. SELECT (NULL, 1) NOT IN (SELECT t2b.i, t2b.pk FROM t2b WHERE t2b.pk = t1.pk) from t1;
  329. (NULL, 1) NOT IN (SELECT t2b.i, t2b.pk FROM t2b WHERE t2b.pk = t1.pk)
  330. 1
  331. NULL
  332. 1
  333. 1
  334. EXPLAIN
  335. SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2c.i, t2c.pk FROM t2c WHERE t2c.pk = t1.pk);
  336. id select_type table type possible_keys key key_len ref rows Extra
  337. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
  338. 2 DEPENDENT SUBQUERY t2c index_subquery it2c it2c 8 const,test.t1.pk 2 Using index; Using where; Full scan on NULL key
  339. SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2c.i, t2c.pk FROM t2c WHERE t2c.pk = t1.pk);
  340. pk i
  341. 0 10
  342. 2 30
  343. 3 40
  344. SELECT (NULL, 1) NOT IN (SELECT t2c.i, t2c.pk FROM t2c WHERE t2c.pk = t1.pk) from t1;
  345. (NULL, 1) NOT IN (SELECT t2c.i, t2c.pk FROM t2c WHERE t2c.pk = t1.pk)
  346. 1
  347. NULL
  348. 1
  349. 1
  350. EXPLAIN
  351. SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2d.i, t2d.pk FROM t2d WHERE t2d.pk = t1.pk);
  352. id select_type table type possible_keys key key_len ref rows Extra
  353. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4 Using where
  354. 2 DEPENDENT SUBQUERY t2d const PRIMARY PRIMARY 4 const 1 Using where; Full scan on NULL key
  355. SELECT * FROM t1 WHERE (NULL, 1) NOT IN (SELECT t2d.i, t2d.pk FROM t2d WHERE t2d.pk = t1.pk);
  356. pk i
  357. 0 10
  358. 2 30
  359. 3 40
  360. SELECT (NULL, 1) NOT IN (SELECT t2d.i, t2d.pk FROM t2d WHERE t2d.pk = t1.pk) from t1;
  361. (NULL, 1) NOT IN (SELECT t2d.i, t2d.pk FROM t2d WHERE t2d.pk = t1.pk)
  362. 1
  363. NULL
  364. 1
  365. 1
  366. drop table t1, t2a, t2b, t2c, t2d;
  367. #
  368. # End of 5.1 tests.
  369. #
  370. #
  371. # BUG#46743 "Azalea processing correlated, aggregate SELECT
  372. # subqueries incorrectly"
  373. #
  374. CREATE TABLE t1 (c int);
  375. INSERT INTO t1 VALUES (NULL);
  376. CREATE TABLE t2 (d int , KEY (d));
  377. INSERT INTO t2 VALUES (NULL),(NULL);
  378. 0 rows in subquery
  379. SELECT 1 AS RESULT FROM t2,t1 WHERE d = c;
  380. RESULT
  381. base query
  382. SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 ;
  383. RESULT
  384. NULL
  385. EXPLAIN EXTENDED SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 ;
  386. id select_type table type possible_keys key key_len ref rows filtered Extra
  387. 1 PRIMARY t1 system NULL NULL NULL NULL 1 100.00
  388. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  389. Warnings:
  390. Note 1276 Field or reference 'test.t1.c' of SELECT #2 was resolved in SELECT #1
  391. Note 1003 select (select 1 from `test`.`t2` where 0) AS `RESULT` from dual
  392. first equivalent variant
  393. SELECT (SELECT 1 FROM t2 WHERE d = IFNULL(c,NULL)) AS RESULT FROM t1 GROUP BY c ;
  394. RESULT
  395. NULL
  396. EXPLAIN EXTENDED SELECT (SELECT 1 FROM t2 WHERE d = IFNULL(c,NULL)) AS RESULT FROM t1 GROUP BY c;
  397. id select_type table type possible_keys key key_len ref rows filtered Extra
  398. 1 PRIMARY t1 system NULL NULL NULL NULL 1 100.00
  399. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  400. Warnings:
  401. Note 1276 Field or reference 'test.t1.c' of SELECT #2 was resolved in SELECT #1
  402. Note 1003 select (select 1 from `test`.`t2` where 0) AS `RESULT` from dual group by NULL
  403. second equivalent variant
  404. SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 GROUP BY c ;
  405. RESULT
  406. NULL
  407. EXPLAIN EXTENDED SELECT (SELECT 1 FROM t2 WHERE d = c) AS RESULT FROM t1 GROUP BY c ;
  408. id select_type table type possible_keys key key_len ref rows filtered Extra
  409. 1 PRIMARY t1 system NULL NULL NULL NULL 1 100.00
  410. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  411. Warnings:
  412. Note 1276 Field or reference 'test.t1.c' of SELECT #2 was resolved in SELECT #1
  413. Note 1003 select (select 1 from `test`.`t2` where 0) AS `RESULT` from dual group by NULL
  414. DROP TABLE t1,t2;
  415. #
  416. # BUG#45928 "Differing query results depending on MRR and
  417. # engine_condition_pushdown settings"
  418. #
  419. CREATE TABLE `t1` (
  420. `pk` int(11) NOT NULL AUTO_INCREMENT,
  421. `time_nokey` time NOT NULL,
  422. `varchar_key` varchar(1) NOT NULL,
  423. `varchar_nokey` varchar(1) NOT NULL,
  424. PRIMARY KEY (`pk`),
  425. KEY `varchar_key` (`varchar_key`)
  426. ) AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;
  427. INSERT INTO `t1` VALUES (10,'00:00:00','i','i'),(11,'00:00:00','','');
  428. set @old_optimizer_switch = @@session.optimizer_switch;
  429. SET SESSION OPTIMIZER_SWITCH = 'materialization=off,semijoin=off,loosescan=off,firstmatch=off,mrr=on,engine_condition_pushdown=on';
  430. Warnings:
  431. Warning 1681 'engine_condition_pushdown=on' is deprecated and will be removed in a future release
  432. SELECT `time_nokey` G1 FROM t1 WHERE ( `varchar_nokey` , `varchar_key` ) IN (
  433. SELECT `varchar_nokey` , `varchar_nokey` ) AND `varchar_key` >= 'c' HAVING G1 ORDER
  434. BY `pk` ;
  435. G1
  436. set @@session.optimizer_switch = @old_optimizer_switch;
  437. DROP TABLE t1;
  438. #
  439. # During work with BUG#45863 I had problems with a query that was
  440. # optimized differently in regular and prepared mode.
  441. # Because there was a bug in one of the selected strategies, I became
  442. # aware of the problem. Adding an EXPLAIN query to catch this.
  443. DROP TABLE IF EXISTS t1, t2, t3;
  444. CREATE TABLE t1
  445. (EMPNUM CHAR(3) NOT NULL,
  446. EMPNAME CHAR(20),
  447. GRADE DECIMAL(4),
  448. CITY CHAR(15));
  449. CREATE TABLE t2
  450. (PNUM CHAR(3) NOT NULL,
  451. PNAME CHAR(20),
  452. PTYPE CHAR(6),
  453. BUDGET DECIMAL(9),
  454. CITY CHAR(15));
  455. CREATE TABLE t3
  456. (EMPNUM CHAR(3) NOT NULL,
  457. PNUM CHAR(3) NOT NULL,
  458. HOURS DECIMAL(5));
  459. INSERT INTO t1 VALUES ('E1','Alice',12,'Deale');
  460. INSERT INTO t1 VALUES ('E2','Betty',10,'Vienna');
  461. INSERT INTO t1 VALUES ('E3','Carmen',13,'Vienna');
  462. INSERT INTO t1 VALUES ('E4','Don',12,'Deale');
  463. INSERT INTO t1 VALUES ('E5','Ed',13,'Akron');
  464. INSERT INTO t2 VALUES ('P1','MXSS','Design',10000,'Deale');
  465. INSERT INTO t2 VALUES ('P2','CALM','Code',30000,'Vienna');
  466. INSERT INTO t2 VALUES ('P3','SDP','Test',30000,'Tampa');
  467. INSERT INTO t2 VALUES ('P4','SDP','Design',20000,'Deale');
  468. INSERT INTO t2 VALUES ('P5','IRM','Test',10000,'Vienna');
  469. INSERT INTO t2 VALUES ('P6','PAYR','Design',50000,'Deale');
  470. INSERT INTO t3 VALUES ('E1','P1',40);
  471. INSERT INTO t3 VALUES ('E1','P2',20);
  472. INSERT INTO t3 VALUES ('E1','P3',80);
  473. INSERT INTO t3 VALUES ('E1','P4',20);
  474. INSERT INTO t3 VALUES ('E1','P5',12);
  475. INSERT INTO t3 VALUES ('E1','P6',12);
  476. INSERT INTO t3 VALUES ('E2','P1',40);
  477. INSERT INTO t3 VALUES ('E2','P2',80);
  478. INSERT INTO t3 VALUES ('E3','P2',20);
  479. INSERT INTO t3 VALUES ('E4','P2',20);
  480. INSERT INTO t3 VALUES ('E4','P4',40);
  481. INSERT INTO t3 VALUES ('E4','P5',80);
  482. SET @old_optimizer_switch = @@session.optimizer_switch;
  483. SET @old_join_cache_level = @@session.join_cache_level;
  484. SET SESSION optimizer_switch = 'firstmatch=on,loosescan=on,materialization=on,in_to_exists=off,semijoin=on';
  485. SET SESSION join_cache_level = 1;
  486. CREATE UNIQUE INDEX t1_IDX ON t1(EMPNUM);
  487. EXPLAIN SELECT EMPNAME
  488. FROM t1
  489. WHERE EMPNUM IN
  490. (SELECT EMPNUM
  491. FROM t3
  492. WHERE PNUM IN
  493. (SELECT PNUM
  494. FROM t2
  495. WHERE PTYPE = 'Design'));
  496. id select_type table type possible_keys key key_len ref rows Extra
  497. 1 PRIMARY t1 ALL t1_IDX NULL NULL NULL 5
  498. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 3 func 1
  499. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 6 Using where
  500. 2 MATERIALIZED t3 ALL NULL NULL NULL NULL 12 Using where; Using join buffer (flat, BNL join)
  501. PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
  502. FROM t1
  503. WHERE EMPNUM IN
  504. (SELECT EMPNUM
  505. FROM t3
  506. WHERE PNUM IN
  507. (SELECT PNUM
  508. FROM t2
  509. WHERE PTYPE = 'Design'))";
  510. EXECUTE stmt;
  511. id select_type table type possible_keys key key_len ref rows Extra
  512. 1 PRIMARY t1 ALL t1_IDX NULL NULL NULL 5
  513. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 3 func 1
  514. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 6 Using where
  515. 2 MATERIALIZED t3 ALL NULL NULL NULL NULL 12 Using where; Using join buffer (flat, BNL join)
  516. EXECUTE stmt;
  517. id select_type table type possible_keys key key_len ref rows Extra
  518. 1 PRIMARY t1 ALL t1_IDX NULL NULL NULL 5
  519. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 3 func 1
  520. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 6 Using where
  521. 2 MATERIALIZED t3 ALL NULL NULL NULL NULL 12 Using where; Using join buffer (flat, BNL join)
  522. DEALLOCATE PREPARE stmt;
  523. DROP INDEX t1_IDX ON t1;
  524. CREATE INDEX t1_IDX ON t1(EMPNUM);
  525. EXPLAIN SELECT EMPNAME
  526. FROM t1
  527. WHERE EMPNUM IN
  528. (SELECT EMPNUM
  529. FROM t3
  530. WHERE PNUM IN
  531. (SELECT PNUM
  532. FROM t2
  533. WHERE PTYPE = 'Design'));
  534. id select_type table type possible_keys key key_len ref rows Extra
  535. 1 PRIMARY t1 ALL t1_IDX NULL NULL NULL 5
  536. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 3 func 1
  537. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 6 Using where
  538. 2 MATERIALIZED t3 ALL NULL NULL NULL NULL 12 Using where; Using join buffer (flat, BNL join)
  539. PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
  540. FROM t1
  541. WHERE EMPNUM IN
  542. (SELECT EMPNUM
  543. FROM t3
  544. WHERE PNUM IN
  545. (SELECT PNUM
  546. FROM t2
  547. WHERE PTYPE = 'Design'))";
  548. EXECUTE stmt;
  549. id select_type table type possible_keys key key_len ref rows Extra
  550. 1 PRIMARY t1 ALL t1_IDX NULL NULL NULL 5
  551. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 3 func 1
  552. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 6 Using where
  553. 2 MATERIALIZED t3 ALL NULL NULL NULL NULL 12 Using where; Using join buffer (flat, BNL join)
  554. EXECUTE stmt;
  555. id select_type table type possible_keys key key_len ref rows Extra
  556. 1 PRIMARY t1 ALL t1_IDX NULL NULL NULL 5
  557. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 3 func 1
  558. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 6 Using where
  559. 2 MATERIALIZED t3 ALL NULL NULL NULL NULL 12 Using where; Using join buffer (flat, BNL join)
  560. DEALLOCATE PREPARE stmt;
  561. DROP INDEX t1_IDX ON t1;
  562. EXPLAIN SELECT EMPNAME
  563. FROM t1
  564. WHERE EMPNUM IN
  565. (SELECT EMPNUM
  566. FROM t3
  567. WHERE PNUM IN
  568. (SELECT PNUM
  569. FROM t2
  570. WHERE PTYPE = 'Design'));
  571. id select_type table type possible_keys key key_len ref rows Extra
  572. 1 PRIMARY t1 ALL NULL NULL NULL NULL 5
  573. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 3 func 1
  574. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 6 Using where
  575. 2 MATERIALIZED t3 ALL NULL NULL NULL NULL 12 Using where; Using join buffer (flat, BNL join)
  576. PREPARE stmt FROM "EXPLAIN SELECT EMPNAME
  577. FROM t1
  578. WHERE EMPNUM IN
  579. (SELECT EMPNUM
  580. FROM t3
  581. WHERE PNUM IN
  582. (SELECT PNUM
  583. FROM t2
  584. WHERE PTYPE = 'Design'))";
  585. EXECUTE stmt;
  586. id select_type table type possible_keys key key_len ref rows Extra
  587. 1 PRIMARY t1 ALL NULL NULL NULL NULL 5
  588. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 3 func 1
  589. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 6 Using where
  590. 2 MATERIALIZED t3 ALL NULL NULL NULL NULL 12 Using where; Using join buffer (flat, BNL join)
  591. EXECUTE stmt;
  592. id select_type table type possible_keys key key_len ref rows Extra
  593. 1 PRIMARY t1 ALL NULL NULL NULL NULL 5
  594. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 3 func 1
  595. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 6 Using where
  596. 2 MATERIALIZED t3 ALL NULL NULL NULL NULL 12 Using where; Using join buffer (flat, BNL join)
  597. DEALLOCATE PREPARE stmt;
  598. SET SESSION optimizer_switch = @old_optimizer_switch;
  599. SET SESSION join_cache_level = @old_join_cache_level;
  600. DROP TABLE t1, t2, t3;
  601. #
  602. # BUG#45221 Query SELECT pk FROM C WHERE pk IN (SELECT int_key) failing
  603. #
  604. CREATE TABLE t1 (
  605. i1_key INT,
  606. i2 INT,
  607. i3 INT,
  608. KEY i1_index (i1_key)
  609. );
  610. INSERT INTO t1 VALUES (9,1,2), (9,2,1);
  611. CREATE TABLE t2 (
  612. pk INT NOT NULL,
  613. i1 INT,
  614. PRIMARY KEY (pk)
  615. );
  616. INSERT INTO t2 VALUES (9,1);
  617. # Enable Index condition pushdown
  618. set @old_icp=@@optimizer_switch;
  619. SET SESSION optimizer_switch="engine_condition_pushdown=on";
  620. Warnings:
  621. Warning 1681 'engine_condition_pushdown=on' is deprecated and will be removed in a future release
  622. SELECT pk
  623. FROM t2
  624. WHERE
  625. pk IN (
  626. SELECT i1_key
  627. FROM t1
  628. WHERE t1.i2 < t1.i3 XOR t2.i1 > 1
  629. ORDER BY t1.i2 desc);
  630. pk
  631. 9
  632. # Restore old value for Index condition pushdown
  633. SET SESSION optimizer_switch=@old_icp;
  634. DROP TABLE t1,t2;
  635. #
  636. # End of 5.3 tests.
  637. #
  638. #
  639. # Bug#53236 Segfault in DTCollation::set(DTCollation&)
  640. #
  641. CREATE TABLE t1 (
  642. pk INTEGER AUTO_INCREMENT,
  643. col_varchar VARCHAR(1),
  644. PRIMARY KEY (pk)
  645. )
  646. ;
  647. INSERT INTO t1 (col_varchar)
  648. VALUES
  649. ('w'),
  650. ('m')
  651. ;
  652. SELECT table1.pk
  653. FROM ( t1 AS table1 JOIN t1 AS table2 ON (table1.col_varchar =
  654. table2.col_varchar) )
  655. WHERE ( 1, 2 ) IN ( SELECT SUBQUERY1_t1.pk AS SUBQUERY1_field1,
  656. SUBQUERY1_t1.pk AS SUBQUERY1_field2
  657. FROM ( t1 AS SUBQUERY1_t1 JOIN t1 AS SUBQUERY1_t2
  658. ON (SUBQUERY1_t2.col_varchar =
  659. SUBQUERY1_t1.col_varchar) ) )
  660. ;
  661. pk
  662. drop table t1;
  663. #
  664. # BUG#716293: "Range checked for each record" is not used if condition refers to outside of subquery
  665. #
  666. create table t1 (a int);
  667. insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  668. create table t2 (a int, b int, `filler` char(200), key(a), key (b));
  669. insert into t2
  670. select A.a + 10*B.a + 100 * C.a, A.a + 10*B.a + 100 * C.a, 'filler' from t1 A, t1 B, t1 C;
  671. # The following must use "Range checked for each record" for table B
  672. explain
  673. select a,
  674. (select sum(X.a+B.b) from t1 X, t2 B where B.a=A.a or B.b=A.a)
  675. from t1 A;
  676. id select_type table type possible_keys key key_len ref rows Extra
  677. 1 PRIMARY A ALL NULL NULL NULL NULL 10
  678. 2 DEPENDENT SUBQUERY X ALL NULL NULL NULL NULL 10
  679. 2 DEPENDENT SUBQUERY B ALL a,b NULL NULL NULL 1000 Range checked for each record (index map: 0x3)
  680. drop table t1, t2;
  681. #
  682. # BUG#723822: Crash in get_constant_key_infix with EXISTS ( SELECT .. DISTINCT )
  683. #
  684. CREATE TABLE t1 ( f1 int(11), f3 varchar(1)) ;
  685. INSERT INTO t1 VALUES ('8','c'),('5','f');
  686. ALTER TABLE t1 ADD KEY (f3,f1);
  687. CREATE TABLE t2 ( f4 varchar(1)) ;
  688. INSERT INTO t2 VALUES ('f'),('d');
  689. SELECT * FROM t2
  690. WHERE EXISTS (
  691. SELECT DISTINCT f3
  692. FROM t1
  693. WHERE f3 <= t2.f4
  694. );
  695. f4
  696. f
  697. d
  698. drop table t1,t2;
  699. #
  700. # LP BUG#718763 Second crash in select_describe() and materialization
  701. #
  702. CREATE TABLE t1 ( f1 int(11), f3 int(11), f10 varchar(1), KEY (f3)) ;
  703. INSERT INTO t1 VALUES ('28','6','m'),('29','4','c');
  704. CREATE TABLE t2 (f11 varchar(1)) ;
  705. INSERT INTO t2 VALUES ('f'),('d');
  706. SET @old_optimizer_switch = @@session.optimizer_switch;
  707. SET SESSION optimizer_switch = 'materialization=on,in_to_exists=off';
  708. EXPLAIN
  709. SELECT * FROM t1
  710. WHERE f3 = (
  711. SELECT t1.f3 FROM t1
  712. WHERE ( t1.f10 ) IN ( SELECT f11 FROM t2 GROUP BY f11 ));
  713. id select_type table type possible_keys key key_len ref rows Extra
  714. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  715. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2
  716. 2 SUBQUERY <subquery3> eq_ref distinct_key distinct_key 4 func 1
  717. 3 MATERIALIZED t2 ALL NULL NULL NULL NULL 2
  718. SELECT * FROM t1
  719. WHERE f3 = (
  720. SELECT t1.f3 FROM t1
  721. WHERE ( t1.f10 ) IN ( SELECT f11 FROM t2 GROUP BY f11 ));
  722. f1 f3 f10
  723. EXPLAIN
  724. SELECT * FROM t1
  725. WHERE f3 = (
  726. SELECT f3 FROM t1
  727. WHERE ( f10, f10 ) IN ( SELECT f11, f11 FROM t2 GROUP BY f11 ));
  728. id select_type table type possible_keys key key_len ref rows Extra
  729. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  730. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2
  731. 2 SUBQUERY <subquery3> eq_ref distinct_key distinct_key 8 func,func 1
  732. 3 MATERIALIZED t2 ALL NULL NULL NULL NULL 2
  733. SELECT * FROM t1
  734. WHERE f3 = (
  735. SELECT f3 FROM t1
  736. WHERE ( f10, f10 ) IN ( SELECT f11, f11 FROM t2 GROUP BY f11 ));
  737. f1 f3 f10
  738. SET @@optimizer_switch = 'materialization=on,in_to_exists=off,semijoin=off';
  739. EXPLAIN
  740. SELECT * FROM t1
  741. WHERE f3 = (
  742. SELECT t1.f3 FROM t1
  743. WHERE ( t1.f10 ) IN ( SELECT max(f11) FROM t2 GROUP BY f11 ));
  744. id select_type table type possible_keys key key_len ref rows Extra
  745. 1 PRIMARY t1 ref f3 f3 5 const 0 Using where
  746. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
  747. 3 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using temporary
  748. SELECT * FROM t1
  749. WHERE f3 = (
  750. SELECT t1.f3 FROM t1
  751. WHERE ( t1.f10 ) IN ( SELECT max(f11) FROM t2 GROUP BY f11 ));
  752. f1 f3 f10
  753. EXPLAIN
  754. SELECT * FROM t1
  755. WHERE f3 = (
  756. SELECT f3 FROM t1
  757. WHERE ( f10, f10 ) IN ( SELECT max(f11), f11 FROM t2 GROUP BY f11 ));
  758. id select_type table type possible_keys key key_len ref rows Extra
  759. 1 PRIMARY t1 ref f3 f3 5 const 0 Using where
  760. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
  761. 3 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using temporary
  762. SELECT * FROM t1
  763. WHERE f3 = (
  764. SELECT f3 FROM t1
  765. WHERE ( f10, f10 ) IN ( SELECT max(f11), f11 FROM t2 GROUP BY f11 ));
  766. f1 f3 f10
  767. SET SESSION optimizer_switch = @old_optimizer_switch;
  768. drop table t1,t2;
  769. #
  770. # LP BUG#715738: Wrong result with implicit grouping and empty result set
  771. #
  772. CREATE TABLE t1 (f1 int, f2 int);
  773. CREATE TABLE t2 (f3 int, f4 int not null, PRIMARY KEY (f3));
  774. set @save_optimizer_switch=@@optimizer_switch;
  775. SET @@optimizer_switch = 'materialization=on,in_to_exists=off,semijoin=off';
  776. EXPLAIN
  777. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2);
  778. id select_type table type possible_keys key key_len ref rows Extra
  779. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  780. 2 MATERIALIZED NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  781. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2);
  782. f1 f2
  783. EXPLAIN
  784. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2);
  785. id select_type table type possible_keys key key_len ref rows Extra
  786. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  787. 2 MATERIALIZED NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  788. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2);
  789. f1 f2
  790. EXPLAIN
  791. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2);
  792. id select_type table type possible_keys key key_len ref rows Extra
  793. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  794. 2 MATERIALIZED NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  795. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2);
  796. f1 f2
  797. EXPLAIN
  798. SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2) as not_in;
  799. id select_type table type possible_keys key key_len ref rows Extra
  800. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  801. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  802. SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2) as not_in;
  803. not_in
  804. NULL
  805. EXPLAIN
  806. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2);
  807. id select_type table type possible_keys key key_len ref rows Extra
  808. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  809. 2 MATERIALIZED NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  810. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2);
  811. f1 f2
  812. EXPLAIN
  813. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2);
  814. id select_type table type possible_keys key key_len ref rows Extra
  815. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  816. 2 MATERIALIZED NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  817. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2);
  818. f1 f2
  819. EXPLAIN
  820. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2) as not_in;
  821. id select_type table type possible_keys key key_len ref rows Extra
  822. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  823. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  824. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2) as not_in;
  825. not_in
  826. NULL
  827. EXPLAIN
  828. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) > 7) as not_in;
  829. id select_type table type possible_keys key key_len ref rows Extra
  830. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  831. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  832. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) > 7) as not_in;
  833. not_in
  834. 1
  835. EXPLAIN
  836. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) is null) as not_in;
  837. id select_type table type possible_keys key key_len ref rows Extra
  838. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  839. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  840. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) is null) as not_in;
  841. not_in
  842. NULL
  843. EXPLAIN
  844. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2) as not_in;
  845. id select_type table type possible_keys key key_len ref rows Extra
  846. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  847. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  848. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2) as not_in;
  849. not_in
  850. NULL
  851. EXPLAIN
  852. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2) as not_in;
  853. id select_type table type possible_keys key key_len ref rows Extra
  854. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  855. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  856. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2) as not_in;
  857. not_in
  858. NULL
  859. EXPLAIN
  860. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2);
  861. id select_type table type possible_keys key key_len ref rows Extra
  862. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  863. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL No matching min/max row
  864. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2);
  865. f1 f2
  866. SET @@optimizer_switch = 'materialization=off,in_to_exists=on,semijoin=off';
  867. EXPLAIN
  868. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2);
  869. id select_type table type possible_keys key key_len ref rows Extra
  870. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  871. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  872. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2);
  873. f1 f2
  874. EXPLAIN
  875. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2);
  876. id select_type table type possible_keys key key_len ref rows Extra
  877. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  878. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  879. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2);
  880. f1 f2
  881. EXPLAIN
  882. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2);
  883. id select_type table type possible_keys key key_len ref rows Extra
  884. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  885. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  886. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2);
  887. f1 f2
  888. EXPLAIN
  889. SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2) as not_in;
  890. id select_type table type possible_keys key key_len ref rows Extra
  891. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  892. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  893. SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2) as not_in;
  894. not_in
  895. NULL
  896. EXPLAIN
  897. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2);
  898. id select_type table type possible_keys key key_len ref rows Extra
  899. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  900. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  901. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2);
  902. f1 f2
  903. EXPLAIN
  904. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2);
  905. id select_type table type possible_keys key key_len ref rows Extra
  906. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  907. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  908. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2);
  909. f1 f2
  910. EXPLAIN
  911. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2) as not_in;
  912. id select_type table type possible_keys key key_len ref rows Extra
  913. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  914. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  915. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2) as not_in;
  916. not_in
  917. NULL
  918. EXPLAIN
  919. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) > 7) as not_in;
  920. id select_type table type possible_keys key key_len ref rows Extra
  921. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  922. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  923. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) > 7) as not_in;
  924. not_in
  925. 1
  926. EXPLAIN
  927. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) is null) as not_in;
  928. id select_type table type possible_keys key key_len ref rows Extra
  929. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  930. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  931. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 HAVING max(f4) is null) as not_in;
  932. not_in
  933. NULL
  934. EXPLAIN
  935. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2) as not_in;
  936. id select_type table type possible_keys key key_len ref rows Extra
  937. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  938. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  939. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2) as not_in;
  940. not_in
  941. NULL
  942. EXPLAIN
  943. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2) as not_in;
  944. id select_type table type possible_keys key key_len ref rows Extra
  945. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  946. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  947. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2) as not_in;
  948. not_in
  949. NULL
  950. EXPLAIN
  951. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2);
  952. id select_type table type possible_keys key key_len ref rows Extra
  953. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  954. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL No matching min/max row
  955. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2);
  956. f1 f2
  957. INSERT INTO t1 VALUES (1, 2);
  958. INSERT INTO t1 VALUES (3, 4);
  959. INSERT INTO t2 VALUES (5, 6);
  960. INSERT INTO t2 VALUES (7, 8);
  961. SET @@optimizer_switch = 'materialization=on,in_to_exists=off,semijoin=off';
  962. EXPLAIN
  963. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10);
  964. id select_type table type possible_keys key key_len ref rows Extra
  965. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  966. 2 MATERIALIZED t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  967. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10);
  968. f1 f2
  969. EXPLAIN
  970. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2 WHERE f3 > 10);
  971. id select_type table type possible_keys key key_len ref rows Extra
  972. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  973. 2 MATERIALIZED t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  974. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2 WHERE f3 > 10);
  975. f1 f2
  976. EXPLAIN
  977. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2 WHERE f3 > 10);
  978. id select_type table type possible_keys key key_len ref rows Extra
  979. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  980. 2 MATERIALIZED t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  981. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2 WHERE f3 > 10);
  982. f1 f2
  983. EXPLAIN
  984. SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10) as not_in;
  985. id select_type table type possible_keys key key_len ref rows Extra
  986. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  987. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  988. SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10) as not_in;
  989. not_in
  990. NULL
  991. EXPLAIN
  992. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10);
  993. id select_type table type possible_keys key key_len ref rows Extra
  994. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  995. 2 MATERIALIZED t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  996. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10);
  997. f1 f2
  998. EXPLAIN
  999. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2 WHERE f3 > 10);
  1000. id select_type table type possible_keys key key_len ref rows Extra
  1001. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1002. 2 MATERIALIZED t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1003. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2 WHERE f3 > 10);
  1004. f1 f2
  1005. EXPLAIN
  1006. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10) as not_in;
  1007. id select_type table type possible_keys key key_len ref rows Extra
  1008. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1009. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1010. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10) as not_in;
  1011. not_in
  1012. NULL
  1013. EXPLAIN
  1014. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) > 7) as not_in;
  1015. id select_type table type possible_keys key key_len ref rows Extra
  1016. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1017. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1018. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) > 7) as not_in;
  1019. not_in
  1020. 1
  1021. EXPLAIN
  1022. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) is null) as not_in;
  1023. id select_type table type possible_keys key key_len ref rows Extra
  1024. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1025. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1026. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) is null) as not_in;
  1027. not_in
  1028. NULL
  1029. EXPLAIN
  1030. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2 WHERE f3 > 10) as not_in;
  1031. id select_type table type possible_keys key key_len ref rows Extra
  1032. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1033. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1034. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2 WHERE f3 > 10) as not_in;
  1035. not_in
  1036. NULL
  1037. EXPLAIN
  1038. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2 WHERE f3 > 10) as not_in;
  1039. id select_type table type possible_keys key key_len ref rows Extra
  1040. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1041. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1042. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2 WHERE f3 > 10) as not_in;
  1043. not_in
  1044. NULL
  1045. EXPLAIN
  1046. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2 WHERE f3 > 10);
  1047. id select_type table type possible_keys key key_len ref rows Extra
  1048. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1049. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL No matching min/max row
  1050. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2 WHERE f3 > 10);
  1051. f1 f2
  1052. SET @@optimizer_switch = 'materialization=off,in_to_exists=on,semijoin=off';
  1053. EXPLAIN
  1054. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10);
  1055. id select_type table type possible_keys key key_len ref rows Extra
  1056. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1057. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1058. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10);
  1059. f1 f2
  1060. EXPLAIN
  1061. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2 WHERE f3 > 10);
  1062. id select_type table type possible_keys key key_len ref rows Extra
  1063. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1064. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1065. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3+f4, min(f4) FROM t2 WHERE f3 > 10);
  1066. f1 f2
  1067. EXPLAIN
  1068. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2 WHERE f3 > 10);
  1069. id select_type table type possible_keys key key_len ref rows Extra
  1070. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1071. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1072. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, min(f4)+max(f4) FROM t2 WHERE f3 > 10);
  1073. f1 f2
  1074. EXPLAIN
  1075. SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10) as not_in;
  1076. id select_type table type possible_keys key key_len ref rows Extra
  1077. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1078. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1079. SELECT (2, 0) NOT IN (SELECT f3, min(f4) FROM t2 WHERE f3 > 10) as not_in;
  1080. not_in
  1081. NULL
  1082. EXPLAIN
  1083. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10);
  1084. id select_type table type possible_keys key key_len ref rows Extra
  1085. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1086. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1087. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10);
  1088. f1 f2
  1089. EXPLAIN
  1090. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2 WHERE f3 > 10);
  1091. id select_type table type possible_keys key key_len ref rows Extra
  1092. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1093. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1094. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT f3, f3 + count(f4) FROM t2 WHERE f3 > 10);
  1095. f1 f2
  1096. EXPLAIN
  1097. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10) as not_in;
  1098. id select_type table type possible_keys key key_len ref rows Extra
  1099. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1100. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1101. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10) as not_in;
  1102. not_in
  1103. NULL
  1104. EXPLAIN
  1105. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) > 7) as not_in;
  1106. id select_type table type possible_keys key key_len ref rows Extra
  1107. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1108. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1109. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) > 7) as not_in;
  1110. not_in
  1111. 1
  1112. EXPLAIN
  1113. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) is null) as not_in;
  1114. id select_type table type possible_keys key key_len ref rows Extra
  1115. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1116. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1117. SELECT (2, 0) NOT IN (SELECT f3, count(f4) FROM t2 WHERE f3 > 10 HAVING max(f4) is null) as not_in;
  1118. not_in
  1119. NULL
  1120. EXPLAIN
  1121. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2 WHERE f3 > 10) as not_in;
  1122. id select_type table type possible_keys key key_len ref rows Extra
  1123. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1124. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1125. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4) FROM t2 WHERE f3 > 10) as not_in;
  1126. not_in
  1127. NULL
  1128. EXPLAIN
  1129. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2 WHERE f3 > 10) as not_in;
  1130. id select_type table type possible_keys key key_len ref rows Extra
  1131. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1132. 2 SUBQUERY t2 range PRIMARY PRIMARY 4 NULL 1 Using index condition; Rowid-ordered scan
  1133. SELECT (2, 0) NOT IN (SELECT max(f3+f3), count(f4)+f3 FROM t2 WHERE f3 > 10) as not_in;
  1134. not_in
  1135. NULL
  1136. EXPLAIN
  1137. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2 WHERE f3 > 10);
  1138. id select_type table type possible_keys key key_len ref rows Extra
  1139. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1140. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL No matching min/max row
  1141. SELECT * FROM t1 WHERE (2, 0) NOT IN (SELECT min(f3)+f3, min(f4)+f3+max(f4) FROM t2 WHERE f3 > 10);
  1142. f1 f2
  1143. set @@optimizer_switch=@save_optimizer_switch;
  1144. drop table t1,t2;
  1145. #
  1146. # LP BUG#613029 Wrong result with materialization and semijoin, and
  1147. # valgrind warnings in Protocol::net_store_data with materialization
  1148. # for implicit grouping
  1149. #
  1150. CREATE TABLE t1 (
  1151. pk int(11) NOT NULL AUTO_INCREMENT,
  1152. f2 int(11) NOT NULL,
  1153. f3 varchar(1) NOT NULL,
  1154. PRIMARY KEY (pk),
  1155. KEY f2 (f2));
  1156. INSERT INTO t1 VALUES (1,9,'x');
  1157. INSERT INTO t1 VALUES (2,5,'g');
  1158. CREATE TABLE t2 (
  1159. pk int(11) NOT NULL AUTO_INCREMENT,
  1160. f2 int(11) NOT NULL,
  1161. f3 varchar(1) NOT NULL,
  1162. PRIMARY KEY (pk),
  1163. KEY f2 (f2));
  1164. INSERT INTO t2 VALUES (1,7,'p');
  1165. set @save_optimizer_switch=@@optimizer_switch;
  1166. set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=off';
  1167. EXPLAIN
  1168. SELECT t1.f3, MAX(t1.f2)
  1169. FROM t1, t2
  1170. WHERE (t2.pk = t1.pk) AND t2.pk IN (SELECT f2 FROM t1);
  1171. id select_type table type possible_keys key key_len ref rows Extra
  1172. 1 PRIMARY t2 system PRIMARY NULL NULL NULL 1
  1173. 1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1
  1174. 2 DEPENDENT SUBQUERY t1 index_subquery f2 f2 4 func 2 Using index
  1175. SELECT t1.f3, MAX(t1.f2)
  1176. FROM t1, t2
  1177. WHERE (t2.pk = t1.pk) AND t2.pk IN (SELECT f2 FROM t1);
  1178. f3 MAX(t1.f2)
  1179. NULL NULL
  1180. set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
  1181. EXPLAIN
  1182. SELECT t1.f3, MAX(t1.f2)
  1183. FROM t1, t2
  1184. WHERE (t2.pk = t1.pk) AND t2.pk IN (SELECT f2 FROM t1);
  1185. id select_type table type possible_keys key key_len ref rows Extra
  1186. 1 PRIMARY t2 system PRIMARY NULL NULL NULL 1
  1187. 1 PRIMARY t1 const PRIMARY PRIMARY 4 const 1
  1188. 2 MATERIALIZED t1 index NULL f2 4 NULL 2 Using index
  1189. SELECT t1.f3, MAX(t1.f2)
  1190. FROM t1, t2
  1191. WHERE (t2.pk = t1.pk) AND t2.pk IN (SELECT f2 FROM t1);
  1192. f3 MAX(t1.f2)
  1193. NULL NULL
  1194. TODO: add a test case for semijoin when the wrong result is fixed
  1195. set @@optimizer_switch='materialization=off,semijoin=on';
  1196. set @@optimizer_switch=@save_optimizer_switch;
  1197. drop table t1, t2;
  1198. #
  1199. # LP BUG#777691 Wrong result with subqery in select list and subquery cache=off in maria-5.3
  1200. #
  1201. CREATE TABLE t1 ( f1 varchar(32)) ;
  1202. INSERT INTO t1 VALUES ('b'),('x'),('c'),('x');
  1203. CREATE TABLE t2 ( f2 int, f3 varchar(32)) ;
  1204. INSERT INTO t2 VALUES (1,'x');
  1205. set @save_optimizer_switch=@@optimizer_switch;
  1206. set @@optimizer_switch='materialization=off,in_to_exists=on,subquery_cache=off';
  1207. EXPLAIN
  1208. SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1;
  1209. id select_type table type possible_keys key key_len ref rows Extra
  1210. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4
  1211. 2 DEPENDENT SUBQUERY t2 system NULL NULL NULL NULL 1
  1212. SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1;
  1213. f1 max_f2
  1214. b NULL
  1215. x 1
  1216. c NULL
  1217. x 1
  1218. set @@optimizer_switch='materialization=on,in_to_exists=off,subquery_cache=off';
  1219. EXPLAIN
  1220. SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1;
  1221. id select_type table type possible_keys key key_len ref rows Extra
  1222. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4
  1223. 2 DEPENDENT SUBQUERY t2 system NULL NULL NULL NULL 1
  1224. SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1;
  1225. f1 max_f2
  1226. b NULL
  1227. x 1
  1228. c NULL
  1229. x 1
  1230. set @@optimizer_switch='materialization=off,in_to_exists=on,subquery_cache=off';
  1231. Even when t2 is not constant table, the result must be the same.
  1232. INSERT INTO t2 VALUES (2,'y');
  1233. EXPLAIN
  1234. SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1;
  1235. id select_type table type possible_keys key key_len ref rows Extra
  1236. 1 PRIMARY t1 ALL NULL NULL NULL NULL 4
  1237. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where
  1238. SELECT t1.f1, ( SELECT MAX( f2 ) FROM t2 WHERE t2.f3 = t1.f1 ) as max_f2 FROM t1;
  1239. f1 max_f2
  1240. b NULL
  1241. x 1
  1242. c NULL
  1243. x 1
  1244. set @@optimizer_switch=@save_optimizer_switch;
  1245. drop table t1, t2;
  1246. #
  1247. # LP BUG#641203 Query returns rows where no result is expected (impossible WHERE)
  1248. #
  1249. CREATE TABLE t1 (c1 varchar(1) DEFAULT NULL);
  1250. CREATE TABLE t2 (c1 varchar(1) DEFAULT NULL);
  1251. INSERT INTO t2 VALUES ('k'), ('d');
  1252. CREATE TABLE t3 (c1 varchar(1) DEFAULT NULL);
  1253. INSERT INTO t3 VALUES ('a'), ('b'), ('c');
  1254. CREATE TABLE t4 (c1 varchar(1) primary key);
  1255. INSERT INTO t4 VALUES ('k'), ('d');
  1256. SET @save_optimizer_switch=@@optimizer_switch;
  1257. SET optimizer_switch='outer_join_with_cache=off';
  1258. SET optimizer_switch='semijoin_with_cache=off';
  1259. SET optimizer_switch='materialization=off';
  1260. EXPLAIN
  1261. SELECT * FROM t1 RIGHT JOIN t2 ON t1.c1 WHERE 's' IN (SELECT c1 FROM t2);
  1262. id select_type table type possible_keys key key_len ref rows Extra
  1263. 1 PRIMARY t1 system NULL NULL NULL NULL 0 const row not found
  1264. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; FirstMatch(t1)
  1265. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join)
  1266. SELECT * FROM t1 RIGHT JOIN t2 ON t1.c1 WHERE 's' IN (SELECT c1 FROM t2);
  1267. c1 c1
  1268. EXPLAIN
  1269. SELECT * FROM t2 LEFT JOIN t1 ON t1.c1 WHERE 's' IN (SELECT c1 FROM t2);
  1270. id select_type table type possible_keys key key_len ref rows Extra
  1271. 1 PRIMARY t1 system NULL NULL NULL NULL 0 const row not found
  1272. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; FirstMatch(t1)
  1273. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join)
  1274. SELECT * FROM t2 LEFT JOIN t1 ON t1.c1 WHERE 's' IN (SELECT c1 FROM t2);
  1275. c1 c1
  1276. SET optimizer_switch='materialization=on';
  1277. EXPLAIN
  1278. SELECT * FROM (t2 LEFT JOIN t1 ON t1.c1) LEFT JOIN t3 on t3.c1 WHERE 's' IN (SELECT c1 FROM t2);
  1279. id select_type table type possible_keys key key_len ref rows Extra
  1280. 1 PRIMARY t1 system NULL NULL NULL NULL 0 const row not found
  1281. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
  1282. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using join buffer (flat, BNL join)
  1283. 1 PRIMARY t3 ALL NULL NULL NULL NULL 3 Using where
  1284. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using where
  1285. SELECT * FROM (t2 LEFT JOIN t1 ON t1.c1) LEFT JOIN t3 on t3.c1 WHERE 's' IN (SELECT c1 FROM t2);
  1286. c1 c1 c1
  1287. EXPLAIN
  1288. SELECT * FROM t4 LEFT JOIN t2 ON t4.c1 WHERE 's' IN (SELECT c1 FROM t2);
  1289. id select_type table type possible_keys key key_len ref rows Extra
  1290. 1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 4 func 1
  1291. 1 PRIMARY t4 index NULL PRIMARY 3 NULL 2 Using index; Using join buffer (flat, BNL join)
  1292. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where
  1293. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 Using where
  1294. SELECT * FROM t4 LEFT JOIN t2 ON t4.c1 WHERE 's' IN (SELECT c1 FROM t2);
  1295. c1 c1
  1296. SET optimizer_switch=@save_optimizer_switch;
  1297. drop table t1, t2, t3, t4;
  1298. #
  1299. # LP BUG#675981 Assertion `cache != __null' failed in sub_select_cache()
  1300. # on EXPLAIN
  1301. #
  1302. CREATE TABLE t1 (f1 int,f2 int) ;
  1303. INSERT IGNORE INTO t1 VALUES ('2','5'),('2',NULL);
  1304. CREATE TABLE t2 (f1 int, f5 int) ;
  1305. INSERT IGNORE INTO t2 VALUES (1,0);
  1306. CREATE TABLE t3 (f4 int) ;
  1307. INSERT IGNORE INTO t3 VALUES (0),(0);
  1308. set @@optimizer_switch='in_to_exists=on,materialization=off,semijoin=off';
  1309. EXPLAIN
  1310. SELECT * FROM t2
  1311. WHERE f1 IN (SELECT t1.f2 FROM t1 JOIN t3 ON t3.f4);
  1312. id select_type table type possible_keys key key_len ref rows Extra
  1313. 1 PRIMARY t2 system NULL NULL NULL NULL 1
  1314. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
  1315. 2 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
  1316. drop table t1, t2, t3;
  1317. #
  1318. # LP BUG#680005 Second assertion `cache != __null' failed in
  1319. # sub_select_cache() on EXPLAIN
  1320. #
  1321. CREATE TABLE t1 (f1 int,f2 int,f4 int,f6 int,KEY (f4)) ;
  1322. INSERT IGNORE INTO t1 VALUES
  1323. ('1','5','1','0'),('2','1','1','0'),('2','2','2','0'),('0',NULL,'0','0'),
  1324. ('2','1','2','0'),('2','0','0','0'),('2','2','2','0'),('2','8','2','0'),
  1325. ('2','7','2','0'),('2','5','2','0'),('2',NULL,'1','0');
  1326. CREATE TABLE t2 (f3 int) ;
  1327. INSERT IGNORE INTO t2 VALUES ('7');
  1328. CREATE TABLE t3 (f3 int) ;
  1329. INSERT IGNORE INTO t3 VALUES ('2');
  1330. EXPLAIN
  1331. SELECT t1.f4
  1332. FROM t2 JOIN t1 ON t1.f6
  1333. WHERE
  1334. ( t1.f2 ) IN (SELECT SUBQUERY2_t1.f3
  1335. FROM t3 AS SUBQUERY2_t1
  1336. JOIN
  1337. (t1 AS SUBQUERY2_t2
  1338. JOIN
  1339. t1 AS SUBQUERY2_t3 ON SUBQUERY2_t3.f1)
  1340. ON SUBQUERY2_t3.f2)
  1341. GROUP BY t1.f4 ORDER BY t1.f1 LIMIT 10;
  1342. id select_type table type possible_keys key key_len ref rows Extra
  1343. 1 PRIMARY t2 system NULL NULL NULL NULL 1 Using temporary; Using filesort
  1344. 1 PRIMARY t1 ALL NULL NULL NULL NULL 11 Using where
  1345. 2 DEPENDENT SUBQUERY SUBQUERY2_t1 system NULL NULL NULL NULL 1
  1346. 2 DEPENDENT SUBQUERY SUBQUERY2_t2 index NULL f4 5 NULL 11 Using index
  1347. 2 DEPENDENT SUBQUERY SUBQUERY2_t3 ALL NULL NULL NULL NULL 11 Using where; Using join buffer (flat, BNL join)
  1348. drop table t1, t2, t3;
  1349. #
  1350. # LP BUG#680038 bool close_thread_table(THD*, TABLE**):
  1351. # Assertion `table->key_read == 0' failed in EXPLAIN
  1352. #
  1353. CREATE TABLE t1 (f1 int,f3 int,f4 int) ;
  1354. INSERT IGNORE INTO t1 VALUES (NULL,1,0);
  1355. CREATE TABLE t2 (f2 int,f4 int,f5 int) ;
  1356. INSERT IGNORE INTO t2 VALUES (8,0,0),(5,0,0);
  1357. CREATE TABLE t3 (f4 int,KEY (f4)) ;
  1358. INSERT IGNORE INTO t3 VALUES (0),(0);
  1359. set @@optimizer_switch='semijoin=off';
  1360. EXPLAIN
  1361. SELECT * FROM t1 WHERE
  1362. (SELECT f2 FROM t2
  1363. WHERE f4 <= ALL
  1364. (SELECT max(SQ1_t1.f4)
  1365. FROM t3 AS SQ1_t1 JOIN t3 AS SQ1_t3 ON SQ1_t3.f4
  1366. GROUP BY SQ1_t1.f4));
  1367. id select_type table type possible_keys key key_len ref rows Extra
  1368. 1 PRIMARY t1 system NULL NULL NULL NULL 1
  1369. 2 SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where
  1370. 3 SUBQUERY SQ1_t1 index NULL f4 5 NULL 2 Using index; Using temporary
  1371. 3 SUBQUERY SQ1_t3 index f4 f4 5 NULL 2 Using where; Using index; Using join buffer (flat, BNL join)
  1372. SELECT * FROM t1 WHERE
  1373. (SELECT f2 FROM t2
  1374. WHERE f4 <= ALL
  1375. (SELECT max(SQ1_t1.f4)
  1376. FROM t3 AS SQ1_t1 JOIN t3 AS SQ1_t3 ON SQ1_t3.f4
  1377. GROUP BY SQ1_t1.f4));
  1378. ERROR 21000: Subquery returns more than 1 row
  1379. drop table t1, t2, t3;
  1380. #
  1381. # BUG#52317: Assertion failing in Field_varstring::store()
  1382. # at field.cc:6833
  1383. #
  1384. CREATE TABLE t1 (i INTEGER);
  1385. INSERT INTO t1 VALUES (1);
  1386. CREATE TABLE t2 (i INTEGER, KEY k(i));
  1387. INSERT INTO t2 VALUES (1), (2);
  1388. EXPLAIN
  1389. SELECT i FROM t1 WHERE (1) NOT IN (SELECT i FROM t2);
  1390. id select_type table type possible_keys key key_len ref rows Extra
  1391. 1 PRIMARY t1 system NULL NULL NULL NULL 1
  1392. 2 SUBQUERY t2 index_subquery k k 5 const 2 Using index
  1393. DROP TABLE t2;
  1394. DROP TABLE t1;
  1395. #
  1396. # LP BUG#680846: Crash in clear_tables() with subqueries
  1397. #
  1398. CREATE TABLE t1 (f3 int) ;
  1399. INSERT IGNORE INTO t1 VALUES (0),(0);
  1400. CREATE TABLE t2 (f1 int,f3 int,f4 varchar(32)) ;
  1401. INSERT IGNORE INTO t2 VALUES (1,0,'f');
  1402. EXPLAIN
  1403. SELECT COUNT(t2.f3),
  1404. (SELECT COUNT(f3) FROM t1 WHERE t2.f1) AS f9
  1405. FROM t2 JOIN t1 ON t1.f3
  1406. WHERE ('v') IN (SELECT f4 FROM t2)
  1407. GROUP BY f9;
  1408. id select_type table type possible_keys key key_len ref rows Extra
  1409. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1410. 3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  1411. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2
  1412. SELECT COUNT(t2.f3),
  1413. (SELECT COUNT(f3) FROM t1 WHERE t2.f1) AS f9
  1414. FROM t2 JOIN t1 ON t1.f3
  1415. WHERE ('v') IN (SELECT f4 FROM t2)
  1416. GROUP BY f9;
  1417. COUNT(t2.f3) f9
  1418. EXPLAIN
  1419. SELECT COUNT(t2.f3),
  1420. (SELECT COUNT(f3) FROM t1 WHERE t2.f1) AS f9
  1421. FROM t2 JOIN t1 ON t1.f3
  1422. WHERE ('v') IN (SELECT f4 FROM t2)
  1423. ORDER BY f9;
  1424. id select_type table type possible_keys key key_len ref rows Extra
  1425. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1426. 3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  1427. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2
  1428. SELECT COUNT(t2.f3),
  1429. (SELECT COUNT(f3) FROM t1 WHERE t2.f1) AS f9
  1430. FROM t2 JOIN t1 ON t1.f3
  1431. WHERE ('v') IN (SELECT f4 FROM t2)
  1432. ORDER BY f9;
  1433. COUNT(t2.f3) f9
  1434. 0 NULL
  1435. EXPLAIN
  1436. SELECT COUNT(t2.f3),
  1437. (SELECT t2.f1 FROM t1 limit 1) AS f9
  1438. FROM t2 JOIN t1
  1439. WHERE ('v') IN (SELECT f4 FROM t2)
  1440. GROUP BY f9;
  1441. id select_type table type possible_keys key key_len ref rows Extra
  1442. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1443. 3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  1444. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2
  1445. SELECT COUNT(t2.f3),
  1446. (SELECT t2.f1 FROM t1 limit 1) AS f9
  1447. FROM t2 JOIN t1
  1448. WHERE ('v') IN (SELECT f4 FROM t2)
  1449. GROUP BY f9;
  1450. COUNT(t2.f3) f9
  1451. EXPLAIN
  1452. SELECT COUNT(t2.f3),
  1453. (SELECT t2.f1 FROM t1 limit 1) AS f9
  1454. FROM t2 JOIN t1
  1455. WHERE ('v') IN (SELECT f4 FROM t2)
  1456. ORDER BY f9;
  1457. id select_type table type possible_keys key key_len ref rows Extra
  1458. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1459. 3 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  1460. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2
  1461. SELECT COUNT(t2.f3),
  1462. (SELECT t2.f1 FROM t1 limit 1) AS f9
  1463. FROM t2 JOIN t1
  1464. WHERE ('v') IN (SELECT f4 FROM t2)
  1465. ORDER BY f9;
  1466. COUNT(t2.f3) f9
  1467. 0 NULL
  1468. drop table t1,t2;
  1469. #
  1470. # LP BUG#682683 Crash in create_tmp_table called from
  1471. # JOIN::init_execution
  1472. #
  1473. CREATE TABLE t2 (f1 int) ;
  1474. INSERT INTO t2 VALUES (1),(2);
  1475. CREATE TABLE t1 (f1 int) ;
  1476. EXPLAIN
  1477. SELECT (SELECT f1 FROM t1) AS field1 FROM t2 GROUP BY field1;
  1478. id select_type table type possible_keys key key_len ref rows Extra
  1479. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2
  1480. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  1481. SELECT (SELECT f1 FROM t1) AS field1 FROM t2 GROUP BY field1;
  1482. field1
  1483. NULL
  1484. EXPLAIN
  1485. SELECT (SELECT f1 FROM t1) AS field1 FROM t2 ORDER BY field1;
  1486. id select_type table type possible_keys key key_len ref rows Extra
  1487. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2
  1488. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  1489. SELECT (SELECT f1 FROM t1) AS field1 FROM t2 ORDER BY field1;
  1490. field1
  1491. NULL
  1492. NULL
  1493. INSERT INTO t1 VALUES (1),(2);
  1494. EXPLAIN
  1495. SELECT (SELECT f1 FROM t1) AS field1 FROM t2 GROUP BY field1;
  1496. id select_type table type possible_keys key key_len ref rows Extra
  1497. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2
  1498. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2
  1499. SELECT (SELECT f1 FROM t1) AS field1 FROM t2 GROUP BY field1;
  1500. ERROR 21000: Subquery returns more than 1 row
  1501. EXPLAIN
  1502. SELECT (SELECT f1 FROM t1) AS field1 FROM t2 ORDER BY field1;
  1503. id select_type table type possible_keys key key_len ref rows Extra
  1504. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2
  1505. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2
  1506. SELECT (SELECT f1 FROM t1) AS field1 FROM t2 ORDER BY field1;
  1507. ERROR 21000: Subquery returns more than 1 row
  1508. drop table t1,t2;
  1509. #
  1510. # LP BUG#680943 Assertion `!table || (!table->read_set ||
  1511. # bitmap_is_set(table->read_set, field_index))' failed with subquery
  1512. #
  1513. CREATE TABLE t1 (f1 int,f3 int) ;
  1514. INSERT IGNORE INTO t1 VALUES ('6','0'),('4','0');
  1515. CREATE TABLE t2 (f1 int,f2 int,f3 int) ;
  1516. INSERT IGNORE INTO t2 VALUES ('6','0','0'),('2','0','0');
  1517. SELECT f2
  1518. FROM (SELECT * FROM t2) AS alias1
  1519. WHERE (SELECT SQ2_t2.f1
  1520. FROM t1 JOIN t1 AS SQ2_t2 ON SQ2_t2.f3
  1521. WHERE SQ2_t2.f3 AND alias1.f1)
  1522. ORDER BY f3 ;
  1523. f2
  1524. drop table t1,t2;
  1525. #
  1526. # LP BUG#715062: Wrong result with VIEW + UNION + subquery in maria-5.3-mwl89
  1527. #
  1528. create table t1 (f1 int);
  1529. create table t2 (f2 int);
  1530. create table t3 (f3 int);
  1531. insert into t1 values (2);
  1532. insert into t2 values (2);
  1533. insert into t3 values (7);
  1534. CREATE VIEW v1 AS SELECT 2 UNION SELECT 2 ;
  1535. CREATE VIEW v2 AS SELECT * from t1 UNION SELECT * from t2 ;
  1536. set @save_optimizer_switch=@@optimizer_switch;
  1537. SET @@optimizer_switch = 'in_to_exists=off,semijoin=off,materialization=on';
  1538. EXPLAIN
  1539. SELECT 'bug' FROM DUAL WHERE ( 5 ) IN ( SELECT * FROM v1 );
  1540. id select_type table type possible_keys key key_len ref rows Extra
  1541. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1542. 2 SUBQUERY <derived3> ALL NULL NULL NULL NULL 2 Using where
  1543. 3 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
  1544. 4 UNION NULL NULL NULL NULL NULL NULL NULL No tables used
  1545. NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL
  1546. SELECT 'bug' FROM DUAL WHERE ( 5 ) IN ( SELECT * FROM v1 );
  1547. bug
  1548. EXPLAIN
  1549. SELECT ( 5 ) IN ( SELECT * FROM v1 );
  1550. id select_type table type possible_keys key key_len ref rows Extra
  1551. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1552. 2 SUBQUERY <derived3> ALL NULL NULL NULL NULL 2 Using where
  1553. 3 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
  1554. 4 UNION NULL NULL NULL NULL NULL NULL NULL No tables used
  1555. NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL
  1556. SELECT ( 5 ) IN ( SELECT * FROM v1 );
  1557. ( 5 ) IN ( SELECT * FROM v1 )
  1558. 0
  1559. EXPLAIN
  1560. SELECT 'bug' FROM DUAL WHERE ( 5 ) IN (SELECT * FROM v2);
  1561. id select_type table type possible_keys key key_len ref rows Extra
  1562. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1563. 2 SUBQUERY <derived3> ALL NULL NULL NULL NULL 2 Using where
  1564. 3 DERIVED t1 system NULL NULL NULL NULL 1
  1565. 4 UNION t2 system NULL NULL NULL NULL 1
  1566. NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL
  1567. SELECT 'bug' FROM DUAL WHERE ( 5 ) IN (SELECT * FROM v2);
  1568. bug
  1569. EXPLAIN
  1570. SELECT 'bug' FROM t3 WHERE ( 5 ) IN (SELECT * FROM v2);
  1571. id select_type table type possible_keys key key_len ref rows Extra
  1572. 1 PRIMARY t3 system NULL NULL NULL NULL 1
  1573. 2 MATERIALIZED <derived3> ALL NULL NULL NULL NULL 2
  1574. 3 DERIVED t1 system NULL NULL NULL NULL 1
  1575. 4 UNION t2 system NULL NULL NULL NULL 1
  1576. NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL
  1577. SELECT 'bug' FROM t3 WHERE ( 5 ) IN (SELECT * FROM v2);
  1578. bug
  1579. EXPLAIN
  1580. SELECT ( 5 ) IN ( SELECT * FROM v2 );
  1581. id select_type table type possible_keys key key_len ref rows Extra
  1582. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1583. 2 SUBQUERY <derived3> ALL NULL NULL NULL NULL 2 Using where
  1584. 3 DERIVED t1 system NULL NULL NULL NULL 1
  1585. 4 UNION t2 system NULL NULL NULL NULL 1
  1586. NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL
  1587. SELECT ( 5 ) IN ( SELECT * FROM v2 );
  1588. ( 5 ) IN ( SELECT * FROM v2 )
  1589. 0
  1590. SET @@optimizer_switch = 'in_to_exists=on,semijoin=off,materialization=off';
  1591. EXPLAIN
  1592. SELECT 'bug' FROM DUAL WHERE ( 5 ) IN ( SELECT * FROM v1 );
  1593. id select_type table type possible_keys key key_len ref rows Extra
  1594. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1595. 2 SUBQUERY <derived3> ALL NULL NULL NULL NULL 2 Using where
  1596. 3 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
  1597. 4 UNION NULL NULL NULL NULL NULL NULL NULL No tables used
  1598. NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL
  1599. SELECT 'bug' FROM DUAL WHERE ( 5 ) IN ( SELECT * FROM v1 );
  1600. bug
  1601. EXPLAIN
  1602. SELECT ( 5 ) IN ( SELECT * FROM v1 );
  1603. id select_type table type possible_keys key key_len ref rows Extra
  1604. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1605. 2 SUBQUERY <derived3> ALL NULL NULL NULL NULL 2 Using where
  1606. 3 DERIVED NULL NULL NULL NULL NULL NULL NULL No tables used
  1607. 4 UNION NULL NULL NULL NULL NULL NULL NULL No tables used
  1608. NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL
  1609. SELECT ( 5 ) IN ( SELECT * FROM v1 );
  1610. ( 5 ) IN ( SELECT * FROM v1 )
  1611. 0
  1612. EXPLAIN
  1613. SELECT 'bug' FROM DUAL WHERE ( 5 ) IN (SELECT * FROM v2);
  1614. id select_type table type possible_keys key key_len ref rows Extra
  1615. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1616. 2 SUBQUERY <derived3> ALL NULL NULL NULL NULL 2 Using where
  1617. 3 DERIVED t1 system NULL NULL NULL NULL 1
  1618. 4 UNION t2 system NULL NULL NULL NULL 1
  1619. NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL
  1620. SELECT 'bug' FROM DUAL WHERE ( 5 ) IN (SELECT * FROM v2);
  1621. bug
  1622. EXPLAIN
  1623. SELECT 'bug' FROM t3 WHERE ( 5 ) IN (SELECT * FROM v2);
  1624. id select_type table type possible_keys key key_len ref rows Extra
  1625. 1 PRIMARY t3 system NULL NULL NULL NULL 1
  1626. 2 SUBQUERY <derived3> ALL NULL NULL NULL NULL 2 Using where
  1627. 3 DERIVED t1 system NULL NULL NULL NULL 1
  1628. 4 UNION t2 system NULL NULL NULL NULL 1
  1629. NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL
  1630. SELECT 'bug' FROM t3 WHERE ( 5 ) IN (SELECT * FROM v2);
  1631. bug
  1632. EXPLAIN
  1633. SELECT ( 5 ) IN ( SELECT * FROM v2 );
  1634. id select_type table type possible_keys key key_len ref rows Extra
  1635. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1636. 2 SUBQUERY <derived3> ALL NULL NULL NULL NULL 2 Using where
  1637. 3 DERIVED t1 system NULL NULL NULL NULL 1
  1638. 4 UNION t2 system NULL NULL NULL NULL 1
  1639. NULL UNION RESULT <union3,4> ALL NULL NULL NULL NULL NULL
  1640. SELECT ( 5 ) IN ( SELECT * FROM v2 );
  1641. ( 5 ) IN ( SELECT * FROM v2 )
  1642. 0
  1643. set @@optimizer_switch=@save_optimizer_switch;
  1644. drop table t1,t2,t3;
  1645. drop view v1,v2;
  1646. #
  1647. # LP BUG#715069 Wrong result with GROUP BY inside subquery and materialization=off
  1648. #
  1649. CREATE TABLE t0 ( f1 int(11), f2 int(11), f10 varchar(1), PRIMARY KEY (f1)) ;
  1650. INSERT INTO t0 VALUES (8,8,'u'),(10,5,'o');
  1651. CREATE TABLE t1 (f1a int, f2a int not null, f3a varchar(3) not null, PRIMARY KEY (f1a)) ;
  1652. INSERT INTO t1 VALUES
  1653. (8,8,'a1a'),
  1654. (10,5,'b1b');
  1655. CREATE TABLE t2 (f1b int, f2b int not null, f3b varchar(3) not null, PRIMARY KEY (f1b)) ;
  1656. INSERT INTO t2 VALUES
  1657. (10,5,'d1d');
  1658. set @save_optimizer_switch=@@optimizer_switch;
  1659. SET optimizer_switch='outer_join_with_cache=off';
  1660. set @@optimizer_switch = 'in_to_exists=on,materialization=off,semijoin=off';
  1661. EXPLAIN
  1662. SELECT alias2.f1 , alias2.f2
  1663. FROM t0 AS alias1
  1664. RIGHT JOIN t0 AS alias2 ON alias2.f10
  1665. WHERE ( alias2.f1 , alias2.f2 ) IN ( SELECT max(f2) , f1 FROM t0 GROUP BY f2 , f1 );
  1666. id select_type table type possible_keys key key_len ref rows Extra
  1667. 1 PRIMARY alias2 ALL NULL NULL NULL NULL 2 Using where
  1668. 1 PRIMARY alias1 index NULL PRIMARY 4 NULL 2 Using where; Using index
  1669. 2 DEPENDENT SUBQUERY t0 ALL NULL NULL NULL NULL 2 Using temporary
  1670. SELECT alias2.f1 , alias2.f2
  1671. FROM t0 AS alias1
  1672. RIGHT JOIN t0 AS alias2 ON alias2.f10
  1673. WHERE ( alias2.f1 , alias2.f2 ) IN ( SELECT max(f2) , f1 FROM t0 GROUP BY f2 , f1 );
  1674. f1 f2
  1675. 8 8
  1676. Warnings:
  1677. Warning 1292 Truncated incorrect DOUBLE value: 'u'
  1678. EXPLAIN
  1679. SELECT * FROM t2 WHERE (f1b, f2b) IN (SELECT max(f1a), f2a FROM t1 GROUP BY f1a, f2a);
  1680. id select_type table type possible_keys key key_len ref rows Extra
  1681. 1 PRIMARY t2 system NULL NULL NULL NULL 1
  1682. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using temporary
  1683. SELECT * FROM t2 WHERE (f1b, f2b) IN (SELECT max(f1a), f2a FROM t1 GROUP BY f1a, f2a);
  1684. f1b f2b f3b
  1685. 10 5 d1d
  1686. EXPLAIN
  1687. SELECT * FROM t2 WHERE (f1b) IN (SELECT max(f1a) FROM t1 GROUP BY f1a, f2a);
  1688. id select_type table type possible_keys key key_len ref rows Extra
  1689. 1 PRIMARY t2 system NULL NULL NULL NULL 1
  1690. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using temporary
  1691. SELECT * FROM t2 WHERE (f1b) IN (SELECT max(f1a) FROM t1 GROUP BY f1a, f2a);
  1692. f1b f2b f3b
  1693. 10 5 d1d
  1694. set @@optimizer_switch = 'in_to_exists=off,materialization=on,semijoin=off';
  1695. EXPLAIN
  1696. SELECT alias2.f1 , alias2.f2
  1697. FROM t0 AS alias1
  1698. RIGHT JOIN t0 AS alias2 ON alias2.f10
  1699. WHERE ( alias2.f1 , alias2.f2 ) IN ( SELECT max(f2) , f1 FROM t0 GROUP BY f2 , f1 );
  1700. id select_type table type possible_keys key key_len ref rows Extra
  1701. 1 PRIMARY alias2 ALL NULL NULL NULL NULL 2 Using where
  1702. 1 PRIMARY alias1 index NULL PRIMARY 4 NULL 2 Using where; Using index
  1703. 2 MATERIALIZED t0 ALL NULL NULL NULL NULL 2 Using temporary
  1704. SELECT alias2.f1 , alias2.f2
  1705. FROM t0 AS alias1
  1706. RIGHT JOIN t0 AS alias2 ON alias2.f10
  1707. WHERE ( alias2.f1 , alias2.f2 ) IN ( SELECT max(f2) , f1 FROM t0 GROUP BY f2 , f1 );
  1708. f1 f2
  1709. 8 8
  1710. Warnings:
  1711. Warning 1292 Truncated incorrect DOUBLE value: 'u'
  1712. EXPLAIN
  1713. SELECT * FROM t2 WHERE (f1b, f2b) IN (SELECT max(f1a), f2a FROM t1 GROUP BY f1a, f2a);
  1714. id select_type table type possible_keys key key_len ref rows Extra
  1715. 1 PRIMARY t2 system NULL NULL NULL NULL 1
  1716. 2 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 Using temporary
  1717. SELECT * FROM t2 WHERE (f1b, f2b) IN (SELECT max(f1a), f2a FROM t1 GROUP BY f1a, f2a);
  1718. f1b f2b f3b
  1719. 10 5 d1d
  1720. EXPLAIN
  1721. SELECT * FROM t2 WHERE (f1b) IN (SELECT max(f1a) FROM t1 GROUP BY f1a, f2a);
  1722. id select_type table type possible_keys key key_len ref rows Extra
  1723. 1 PRIMARY t2 system NULL NULL NULL NULL 1
  1724. 2 MATERIALIZED t1 ALL NULL NULL NULL NULL 2 Using temporary
  1725. SELECT * FROM t2 WHERE (f1b) IN (SELECT max(f1a) FROM t1 GROUP BY f1a, f2a);
  1726. f1b f2b f3b
  1727. 10 5 d1d
  1728. set @@optimizer_switch=@save_optimizer_switch;
  1729. drop table t0,t1,t2;
  1730. #
  1731. # LP BUG#715759 Wrong result with in_to_exists=on in maria-5.3-mwl89
  1732. #
  1733. set @save_optimizer_switch=@@optimizer_switch;
  1734. CREATE TABLE t1 (a1 int, a2 int) ;
  1735. INSERT INTO t1 VALUES (1, 2);
  1736. INSERT INTO t1 VALUES (3, 4);
  1737. CREATE TABLE t2 (b1 int, b2 int) ;
  1738. INSERT INTO t2 VALUES (1, 2);
  1739. SET @@optimizer_switch = 'in_to_exists=on,materialization=off,semijoin=off';
  1740. EXPLAIN SELECT * FROM t1 WHERE a1 IN (SELECT b1 FROM t2 WHERE b1 = b2);
  1741. id select_type table type possible_keys key key_len ref rows Extra
  1742. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
  1743. 2 DEPENDENT SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  1744. SELECT * FROM t1 WHERE a1 IN (SELECT b1 FROM t2 WHERE b1 = b2);
  1745. a1 a2
  1746. set @@optimizer_switch=@save_optimizer_switch;
  1747. drop table t1, t2;
  1748. #
  1749. # LP BUG#772309 join_tab_cmp_straight(): Assertion `!jt2->emb_sj_nest' failed in maria-5.3-mwl89 with semijoin
  1750. #
  1751. CREATE TABLE t1 ( f2 int) ;
  1752. INSERT INTO t1 VALUES (0),(0);
  1753. CREATE TABLE t2 ( f1 int NOT NULL ) ;
  1754. INSERT INTO t2 VALUES (0),(0);
  1755. CREATE TABLE t3 ( f1 int NOT NULL , f2 int) ;
  1756. INSERT INTO t3 VALUES (0,0), (0,0);
  1757. EXPLAIN SELECT STRAIGHT_JOIN (
  1758. SELECT f2 FROM t1 WHERE ( f2 ) IN ( SELECT t3.f2 FROM t3 JOIN t2 ON t2.f1 = 1 )
  1759. );
  1760. id select_type table type possible_keys key key_len ref rows Extra
  1761. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  1762. 2 SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
  1763. 3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 Using where
  1764. 3 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
  1765. SELECT STRAIGHT_JOIN (
  1766. SELECT f2 FROM t1 WHERE ( f2 ) IN ( SELECT t3.f2 FROM t3 JOIN t2 ON t2.f1 = 1 )
  1767. );
  1768. (
  1769. SELECT f2 FROM t1 WHERE ( f2 ) IN ( SELECT t3.f2 FROM t3 JOIN t2 ON t2.f1 = 1 )
  1770. )
  1771. NULL
  1772. drop table t1, t2, t3;
  1773. #
  1774. # LP BUG#777597 Wrong result with multipart keys, in_to_exists=on, NOT IN in MWL#89
  1775. #
  1776. CREATE TABLE t1 ( f4 int);
  1777. INSERT IGNORE INTO t1 VALUES (2),(2);
  1778. CREATE TABLE t2 ( f3 int, f10 int, KEY (f10,f3) );
  1779. INSERT IGNORE INTO t2 VALUES (6, 1), (6, 1);
  1780. CREATE TABLE t3 ( f10 int );
  1781. INSERT IGNORE INTO t3 VALUES (1);
  1782. SET SESSION optimizer_switch='in_to_exists=on,materialization=off';
  1783. EXPLAIN
  1784. SELECT * FROM t1 WHERE ( 6 ) NOT IN ( SELECT t2.f3 FROM t2 JOIN t3 ON t3.f10 = t2.f10);
  1785. id select_type table type possible_keys key key_len ref rows Extra
  1786. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  1787. 2 SUBQUERY t3 system NULL NULL NULL NULL 1
  1788. 2 SUBQUERY t2 ref_or_null f10 f10 10 const,const 2 Using where; Using index
  1789. SELECT * FROM t1 WHERE ( 6 ) NOT IN ( SELECT t2.f3 FROM t2 JOIN t3 ON t3.f10 = t2.f10);
  1790. f4
  1791. drop table t1,t2,t3;
  1792. #
  1793. # LP BUG#778413 Third crash in select_describe() in maria-5.3-mwl89
  1794. #
  1795. CREATE TABLE t1 ( f11 int) ;
  1796. INSERT INTO t1 VALUES (1),(1);
  1797. CREATE TABLE t2 ( f1 int NOT NULL) ;
  1798. INSERT INTO t2 VALUES (20);
  1799. CREATE TABLE t3 (f3 int) ;
  1800. INSERT INTO t3 VALUES (2),(2);
  1801. EXPLAIN SELECT * FROM t2
  1802. WHERE t2.f1 = (
  1803. SELECT MAX( f3 ) FROM t3
  1804. WHERE EXISTS (
  1805. SELECT DISTINCT f11
  1806. FROM t1));
  1807. id select_type table type possible_keys key key_len ref rows Extra
  1808. 1 PRIMARY t2 system NULL NULL NULL NULL 1
  1809. 2 SUBQUERY t3 ALL NULL NULL NULL NULL 2
  1810. 3 SUBQUERY t1 ALL NULL NULL NULL NULL 2
  1811. drop table t1, t2, t3;
  1812. #
  1813. # LP BUG#802979 Assertion `table->key_read == 0' in close_thread_table
  1814. #
  1815. CREATE TABLE t1 ( f1 int, f2 int , KEY (f1)) ;
  1816. INSERT IGNORE INTO t1 VALUES (1,0),(5,0);
  1817. CREATE TABLE t2 ( f1 int, f2 int , KEY (f1)) ;
  1818. INSERT IGNORE INTO t2 VALUES (1,0),(5,0);
  1819. CREATE TABLE t3 ( f1 int, f2 int , KEY (f1)) ;
  1820. INSERT IGNORE INTO t3 VALUES (1,0),(5,0);
  1821. CREATE TABLE t4 ( f1 int, f2 int , KEY (f1)) ;
  1822. INSERT IGNORE INTO t4 VALUES (1,0),(5,0);
  1823. EXPLAIN
  1824. SELECT *
  1825. FROM t1, t2
  1826. WHERE t2.f2 = (SELECT f2 FROM t3
  1827. WHERE EXISTS (SELECT DISTINCT f1 FROM t4))
  1828. AND t2.f2 = t1.f1;
  1829. id select_type table type possible_keys key key_len ref rows Extra
  1830. 1 PRIMARY t1 ref f1 f1 5 const 0 Using where
  1831. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
  1832. 2 SUBQUERY t3 ALL NULL NULL NULL NULL 2
  1833. 3 SUBQUERY t4 index NULL f1 5 NULL 2 Using index
  1834. SELECT *
  1835. FROM t1, t2
  1836. WHERE t2.f2 = (SELECT f2 FROM t3
  1837. WHERE EXISTS (SELECT DISTINCT f1 FROM t4))
  1838. AND t2.f2 = t1.f1;
  1839. ERROR 21000: Subquery returns more than 1 row
  1840. EXPLAIN
  1841. SELECT *
  1842. FROM t1, t2
  1843. WHERE t2.f2 = (SELECT f2 FROM t3
  1844. WHERE EXISTS (SELECT DISTINCT f1 FROM t4) LIMIT 1)
  1845. AND t2.f2 = t1.f1;
  1846. id select_type table type possible_keys key key_len ref rows Extra
  1847. 1 PRIMARY t1 ref f1 f1 5 const 0 Using where
  1848. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
  1849. 2 SUBQUERY t3 ALL NULL NULL NULL NULL 2
  1850. 3 SUBQUERY t4 index NULL f1 5 NULL 2 Using index
  1851. SELECT *
  1852. FROM t1, t2
  1853. WHERE t2.f2 = (SELECT f2 FROM t3
  1854. WHERE EXISTS (SELECT DISTINCT f1 FROM t4) LIMIT 1)
  1855. AND t2.f2 = t1.f1;
  1856. f1 f2 f1 f2
  1857. drop table t1,t2,t3,t4;
  1858. #
  1859. # LP BUG#611690 Crash in select_describe() with nested subqueries
  1860. #
  1861. CREATE TABLE t1 (
  1862. col_int_key int(11) DEFAULT NULL,
  1863. col_varchar_key varchar(1) DEFAULT NULL,
  1864. col_varchar_nokey varchar(1) DEFAULT NULL,
  1865. KEY col_int_key (col_int_key),
  1866. KEY col_varchar_key (col_varchar_key,col_int_key)
  1867. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  1868. INSERT INTO t1 VALUES (8,'v','v');
  1869. INSERT INTO t1 VALUES (9,'r','r');
  1870. CREATE TABLE t2 (
  1871. col_int_key int(11) DEFAULT NULL,
  1872. col_varchar_key varchar(1) DEFAULT NULL,
  1873. col_varchar_nokey varchar(1) DEFAULT NULL,
  1874. KEY col_int_key (col_int_key),
  1875. KEY col_varchar_key (col_varchar_key,col_int_key)
  1876. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  1877. INSERT INTO t2 VALUES (2,'w','w');
  1878. INSERT INTO t2 VALUES (9,'m','m');
  1879. set @old_optimizer_switch = @@optimizer_switch;
  1880. set @@optimizer_switch='subquery_cache=off,materialization=on,in_to_exists=off,semijoin=off';
  1881. EXPLAIN
  1882. SELECT col_int_key
  1883. FROM t2
  1884. WHERE (SELECT SUBQUERY2_t1.col_int_key
  1885. FROM t1 SUBQUERY2_t1 STRAIGHT_JOIN t1 SUBQUERY2_t2
  1886. ON SUBQUERY2_t2.col_varchar_key
  1887. WHERE SUBQUERY2_t2.col_varchar_nokey IN
  1888. (SELECT col_varchar_nokey FROM t1 GROUP BY col_varchar_nokey));
  1889. id select_type table type possible_keys key key_len ref rows Extra
  1890. 1 PRIMARY t2 index NULL col_int_key 5 NULL 2 Using index
  1891. 2 SUBQUERY SUBQUERY2_t1 index NULL col_int_key 5 NULL 2 Using index
  1892. 2 SUBQUERY SUBQUERY2_t2 ALL col_varchar_key NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
  1893. 3 MATERIALIZED t1 ALL NULL NULL NULL NULL 2
  1894. SELECT col_int_key
  1895. FROM t2
  1896. WHERE (SELECT SUBQUERY2_t1.col_int_key
  1897. FROM t1 SUBQUERY2_t1 STRAIGHT_JOIN t1 SUBQUERY2_t2
  1898. ON SUBQUERY2_t2.col_varchar_key
  1899. WHERE SUBQUERY2_t2.col_varchar_nokey IN
  1900. (SELECT col_varchar_nokey FROM t1 GROUP BY col_varchar_nokey));
  1901. col_int_key
  1902. Warnings:
  1903. Warning 1292 Truncated incorrect DOUBLE value: 'v'
  1904. Warning 1292 Truncated incorrect DOUBLE value: 'r'
  1905. set @@optimizer_switch='subquery_cache=off,materialization=off,in_to_exists=on,semijoin=off';
  1906. EXPLAIN
  1907. SELECT col_int_key
  1908. FROM t2
  1909. WHERE (SELECT SUBQUERY2_t1.col_int_key
  1910. FROM t1 SUBQUERY2_t1 STRAIGHT_JOIN t1 SUBQUERY2_t2
  1911. ON SUBQUERY2_t2.col_varchar_key
  1912. WHERE SUBQUERY2_t2.col_varchar_nokey IN
  1913. (SELECT col_varchar_nokey FROM t1 GROUP BY col_varchar_nokey));
  1914. id select_type table type possible_keys key key_len ref rows Extra
  1915. 1 PRIMARY t2 index NULL col_int_key 5 NULL 2 Using index
  1916. 2 SUBQUERY SUBQUERY2_t1 index NULL col_int_key 5 NULL 2 Using index
  1917. 2 SUBQUERY SUBQUERY2_t2 ALL col_varchar_key NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
  1918. 3 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
  1919. SELECT col_int_key
  1920. FROM t2
  1921. WHERE (SELECT SUBQUERY2_t1.col_int_key
  1922. FROM t1 SUBQUERY2_t1 STRAIGHT_JOIN t1 SUBQUERY2_t2
  1923. ON SUBQUERY2_t2.col_varchar_key
  1924. WHERE SUBQUERY2_t2.col_varchar_nokey IN
  1925. (SELECT col_varchar_nokey FROM t1 GROUP BY col_varchar_nokey));
  1926. col_int_key
  1927. Warnings:
  1928. Warning 1292 Truncated incorrect DOUBLE value: 'v'
  1929. Warning 1292 Truncated incorrect DOUBLE value: 'r'
  1930. drop table t1, t2;
  1931. set @@optimizer_switch = @old_optimizer_switch;
  1932. #
  1933. # LP BUG#612543 Crash in Item_field::used_tables() with view + subquery + prepared statements
  1934. #
  1935. CREATE TABLE t1 ( f1 int(11), f2 varchar(1));
  1936. CREATE TABLE t2 ( f3 varchar(1));
  1937. insert into t1 values (2,'x'), (5,'y');
  1938. insert into t2 values ('x'), ('z');
  1939. CREATE VIEW v2 AS SELECT * FROM t2;
  1940. set @old_optimizer_switch = @@optimizer_switch;
  1941. set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off,subquery_cache=off';
  1942. EXPLAIN SELECT * FROM t1 JOIN v2 ON t1.f2 > 'a' WHERE v2.f3 IN ( SELECT f2 FROM t1 );
  1943. id select_type table type possible_keys key key_len ref rows Extra
  1944. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
  1945. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
  1946. 2 MATERIALIZED t1 ALL NULL NULL NULL NULL 2
  1947. PREPARE st1 FROM "SELECT * FROM t1 JOIN v2 ON t1.f2 > 'a' WHERE v2.f3 IN ( SELECT f2 FROM t1 )";
  1948. EXECUTE st1;
  1949. f1 f2 f3
  1950. 2 x x
  1951. 5 y x
  1952. EXECUTE st1;
  1953. f1 f2 f3
  1954. 2 x x
  1955. 5 y x
  1956. set @@optimizer_switch='materialization=off,in_to_exists=on,semijoin=off,subquery_cache=off';
  1957. EXPLAIN SELECT * FROM t1 JOIN v2 ON t1.f2 > 'a' WHERE v2.f3 IN ( SELECT f2 FROM t1 );
  1958. id select_type table type possible_keys key key_len ref rows Extra
  1959. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
  1960. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
  1961. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 Using where
  1962. PREPARE st2 FROM "SELECT * FROM t1 JOIN v2 ON t1.f2 > 'a' WHERE v2.f3 IN ( SELECT f2 FROM t1 )";
  1963. EXECUTE st2;
  1964. f1 f2 f3
  1965. 2 x x
  1966. 5 y x
  1967. EXECUTE st2;
  1968. f1 f2 f3
  1969. 2 x x
  1970. 5 y x
  1971. set @@optimizer_switch='materialization=on,in_to_exists=on,semijoin=off,subquery_cache=off';
  1972. EXPLAIN SELECT * FROM t1 JOIN v2 ON t1.f2 > 'a' WHERE v2.f3 IN ( SELECT f2 FROM t1 );
  1973. id select_type table type possible_keys key key_len ref rows Extra
  1974. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
  1975. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Using join buffer (flat, BNL join)
  1976. 2 MATERIALIZED t1 ALL NULL NULL NULL NULL 2
  1977. PREPARE st3 FROM "SELECT * FROM t1 JOIN v2 ON t1.f2 > 'a' WHERE v2.f3 IN ( SELECT f2 FROM t1 )";
  1978. EXECUTE st3;
  1979. f1 f2 f3
  1980. 2 x x
  1981. 5 y x
  1982. EXECUTE st3;
  1983. f1 f2 f3
  1984. 2 x x
  1985. 5 y x
  1986. set @@optimizer_switch = @old_optimizer_switch;
  1987. drop table t1, t2;
  1988. drop view v2;
  1989. #
  1990. # LP BUG#611396 RQG: crash in Item_field::register_field_in_read_map with semijoin=off
  1991. # and prepared statements and materialization
  1992. CREATE TABLE t1 ( f1 int(11), f2 int(11)) ;
  1993. CREATE TABLE t2 ( f1 int(11), f4 varchar(1), PRIMARY KEY (f1)) ;
  1994. INSERT INTO t2 VALUES ('23','j'),('24','e');
  1995. CREATE TABLE t3 ( f1 int(11), f4 varchar(1)) ;
  1996. INSERT INTO t3 VALUES ('8','j');
  1997. set @old_optimizer_switch = @@optimizer_switch;
  1998. set @@optimizer_switch='materialization=on,in_to_exists=off,semijoin=off';
  1999. EXPLAIN
  2000. SELECT t2.f1, (SELECT f2 FROM t1 WHERE (7) IN (SELECT f1 FROM t1))
  2001. FROM t2 JOIN t3 ON t3.f4 = t2.f4
  2002. WHERE t3.f1 = 8
  2003. GROUP BY 1, 2;
  2004. id select_type table type possible_keys key key_len ref rows Extra
  2005. 1 PRIMARY t3 system NULL NULL NULL NULL 1
  2006. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where; Using filesort
  2007. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  2008. 3 MATERIALIZED NULL NULL NULL NULL NULL NULL NULL no matching row in const table
  2009. PREPARE st1 FROM "
  2010. SELECT t2.f1, (SELECT f2 FROM t1 WHERE (7) IN (SELECT f1 FROM t1))
  2011. FROM t2 JOIN t3 ON t3.f4 = t2.f4
  2012. WHERE t3.f1 = 8
  2013. GROUP BY 1, 2";
  2014. EXECUTE st1;
  2015. f1 (SELECT f2 FROM t1 WHERE (7) IN (SELECT f1 FROM t1))
  2016. 23 NULL
  2017. EXECUTE st1;
  2018. f1 (SELECT f2 FROM t1 WHERE (7) IN (SELECT f1 FROM t1))
  2019. 23 NULL
  2020. set @@optimizer_switch = @old_optimizer_switch;
  2021. drop table t1, t2, t3;
  2022. #
  2023. # LP BUG#611382 RQG: Query returns extra rows when executed with materialization=on
  2024. #
  2025. CREATE TABLE t1 ( f4 varchar(1)) ENGINE=MyISAM;
  2026. INSERT INTO t1 VALUES (NULL);
  2027. CREATE TABLE t2 ( f2 date, f3 varchar(1), f4 varchar(1)) ;
  2028. INSERT INTO t2 VALUES ('2005-05-03','c','c'),('1900-01-01','d','d');
  2029. CREATE TABLE t3 ( f3 varchar(1)) ;
  2030. INSERT INTO t3 VALUES ('c');
  2031. set @old_optimizer_switch = @@optimizer_switch;
  2032. set @@optimizer_switch = 'materialization=on,in_to_exists=off,semijoin=off';
  2033. EXPLAIN SELECT t1.f4
  2034. FROM t1 JOIN ( t2 JOIN t3 ON t3.f3 = t2.f4 ) ON t3.f3 = t2.f3
  2035. WHERE t1.f4 IN ( SELECT f4 FROM t2 ) ;
  2036. id select_type table type possible_keys key key_len ref rows Extra
  2037. 1 PRIMARY t1 system NULL NULL NULL NULL 1
  2038. 1 PRIMARY t3 system NULL NULL NULL NULL 1
  2039. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where
  2040. 2 MATERIALIZED t2 ALL NULL NULL NULL NULL 2
  2041. SELECT t1.f4
  2042. FROM t1 JOIN ( t2 JOIN t3 ON t3.f3 = t2.f4 ) ON t3.f3 = t2.f3
  2043. WHERE t1.f4 IN ( SELECT f4 FROM t2 ) ;
  2044. f4
  2045. set @@optimizer_switch = 'materialization=off,in_to_exists=on,semijoin=off';
  2046. EXPLAIN SELECT t1.f4
  2047. FROM t1 JOIN ( t2 JOIN t3 ON t3.f3 = t2.f4 ) ON t3.f3 = t2.f3
  2048. WHERE t1.f4 IN ( SELECT f4 FROM t2 ) ;
  2049. id select_type table type possible_keys key key_len ref rows Extra
  2050. 1 PRIMARY t1 system NULL NULL NULL NULL 1
  2051. 1 PRIMARY t3 system NULL NULL NULL NULL 1
  2052. 1 PRIMARY t2 ALL NULL NULL NULL NULL 2 Using where
  2053. 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 Using where
  2054. SELECT t1.f4
  2055. FROM t1 JOIN ( t2 JOIN t3 ON t3.f3 = t2.f4 ) ON t3.f3 = t2.f3
  2056. WHERE t1.f4 IN ( SELECT f4 FROM t2 ) ;
  2057. f4
  2058. set @@optimizer_switch = @old_optimizer_switch;
  2059. drop table t1, t2, t3;
  2060. #
  2061. # LP BUG#782305: Wrong result/valgrind warning in Item_sum_hybrid::any_value()
  2062. #
  2063. CREATE TABLE t1 ( f1 int) ;
  2064. INSERT INTO t1 VALUES (2),(3);
  2065. CREATE TABLE t2 (f2 int) ;
  2066. INSERT INTO t2 VALUES (2),(3);
  2067. PREPARE st1 FROM '
  2068. SELECT * FROM t2
  2069. WHERE f2 <= SOME ( SELECT f1 FROM t1 );
  2070. ';
  2071. EXECUTE st1;
  2072. f2
  2073. 2
  2074. 3
  2075. EXECUTE st1;
  2076. f2
  2077. 2
  2078. 3
  2079. PREPARE st2 FROM '
  2080. SELECT * FROM t2
  2081. WHERE f2 <= SOME (SELECT f1-2 FROM t1 UNION SELECT f1-1 FROM t1);
  2082. ';
  2083. EXECUTE st2;
  2084. f2
  2085. 2
  2086. EXECUTE st2;
  2087. f2
  2088. 2
  2089. drop table t1, t2;
  2090. #
  2091. # LP BUG#825018: Crash in check_and_do_in_subquery_rewrites() with corrlated subquery in select list
  2092. #
  2093. CREATE TABLE t1 (a int, b int);
  2094. INSERT INTO t1 VALUES (10,1),(11,7);
  2095. CREATE TABLE t2 (a int);
  2096. INSERT INTO t2 VALUES (2),(3);
  2097. CREATE TABLE t3 (a int, b int);
  2098. INSERT INTO t3 VALUES (1,1);
  2099. CREATE PROCEDURE sp1 () LANGUAGE SQL
  2100. SELECT (SELECT t1.a
  2101. FROM t1
  2102. WHERE t1.b = t3.b
  2103. AND t1.b IN ( SELECT a FROM t2 )) sq
  2104. FROM t3
  2105. GROUP BY 1;
  2106. CALL sp1();
  2107. sq
  2108. NULL
  2109. CALL sp1();
  2110. sq
  2111. NULL
  2112. drop procedure sp1;
  2113. prepare st1 from "
  2114. SELECT (SELECT t1.a
  2115. FROM t1
  2116. WHERE t1.b = t3.b
  2117. AND t1.b IN ( SELECT a FROM t2 )) sq
  2118. FROM t3
  2119. GROUP BY 1";
  2120. execute st1;
  2121. sq
  2122. NULL
  2123. execute st1;
  2124. sq
  2125. NULL
  2126. deallocate prepare st1;
  2127. drop table t1, t2, t3;
  2128. set optimizer_switch=@subselect4_tmp;
  2129. #
  2130. # LP BUG#833702 Wrong result with nested IN and singlerow subqueries and equality propagation
  2131. #
  2132. CREATE TABLE t2 (c int , a int, b int);
  2133. INSERT INTO t2 VALUES (10,7,0);
  2134. CREATE TABLE t3 (a int, b int) ;
  2135. INSERT INTO t3 VALUES (5,0),(7,0);
  2136. CREATE TABLE t4 (a int);
  2137. INSERT INTO t4 VALUES (2),(8);
  2138. set @@optimizer_switch='semijoin=off,in_to_exists=on,materialization=off,subquery_cache=off';
  2139. SELECT * FROM t2
  2140. WHERE t2.b IN (SELECT b FROM t3 WHERE t3.a = t2.a AND a < SOME (SELECT * FROM t4))
  2141. OR ( t2.c > 242 );
  2142. c a b
  2143. 10 7 0
  2144. EXPLAIN SELECT * FROM t2
  2145. WHERE t2.b IN (SELECT t3.b FROM t3 WHERE t3.a < ANY (SELECT t4.a FROM t4) and t3.a = 7);
  2146. id select_type table type possible_keys key key_len ref rows Extra
  2147. 1 PRIMARY t2 system NULL NULL NULL NULL 1
  2148. 2 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 Using where
  2149. 3 SUBQUERY t4 ALL NULL NULL NULL NULL 2
  2150. SELECT * FROM t2
  2151. WHERE t2.b IN (SELECT t3.b FROM t3 WHERE t3.a < ANY (SELECT t4.a FROM t4) and t3.a = 7);
  2152. c a b
  2153. 10 7 0
  2154. drop table t2, t3, t4;
  2155. #
  2156. # BUG#934597: Assertion `! is_set()' failed in Diagnostics_area::set_ok_status(THD...
  2157. #
  2158. CREATE TABLE t1 ( a VARCHAR(1) );
  2159. INSERT INTO t1 VALUES ('u'),('k');
  2160. CREATE TABLE t2 AS
  2161. SELECT a AS field1 FROM t1
  2162. WHERE ( SELECT alias1.a
  2163. FROM t1 AS alias1
  2164. ) IS NOT NULL;
  2165. ERROR 21000: Subquery returns more than 1 row
  2166. DROP TABLE t2;
  2167. ERROR 42S02: Unknown table 'test.t2'
  2168. DROP TABLE t1;
  2169. #
  2170. # LP BUG#1000649 EXPLAIN shows incorrectly a non-correlated constant IN subquery is correlated
  2171. #
  2172. create table ten (a int);
  2173. insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
  2174. create table t1 (a int, b int, c int);
  2175. insert into t1 select a,a,a from ten;
  2176. create table five (a int, b int, c int);
  2177. insert into five select a,a,a from ten limit 5;
  2178. set @@optimizer_switch='semijoin=on,in_to_exists=on,materialization=off';
  2179. explain select * from t1 where 33 in (select b from five) or c > 11;
  2180. id select_type table type possible_keys key key_len ref rows Extra
  2181. 1 PRIMARY t1 ALL NULL NULL NULL NULL 10 Using where
  2182. 2 SUBQUERY five ALL NULL NULL NULL NULL 5 Using where
  2183. drop table ten, t1, five;
  2184. #
  2185. # LP BUG#1008773 Wrong result (NULL instead of a value) with no matching rows, subquery in FROM and HAVING
  2186. #
  2187. CREATE TABLE t1 (a INT) ENGINE=MyISAM;
  2188. CREATE TABLE t2 (b INT) ENGINE=MyISAM;
  2189. INSERT INTO t2 VALUES (1);
  2190. EXPLAIN
  2191. SELECT MAX(a), ( SELECT 1 FROM t2 ) AS bb FROM t1;
  2192. id select_type table type possible_keys key key_len ref rows Extra
  2193. 1 PRIMARY t1 system NULL NULL NULL NULL 0 const row not found
  2194. 2 SUBQUERY t2 system NULL NULL NULL NULL 1
  2195. SELECT MAX(a), ( SELECT 1 FROM t2 ) AS bb FROM t1;
  2196. MAX(a) bb
  2197. NULL 1
  2198. EXPLAIN
  2199. SELECT MAX(a), 1 in ( SELECT b FROM t2 ) AS bb FROM t1;
  2200. id select_type table type possible_keys key key_len ref rows Extra
  2201. 1 PRIMARY t1 system NULL NULL NULL NULL 0 const row not found
  2202. 2 SUBQUERY t2 system NULL NULL NULL NULL 1
  2203. SELECT MAX(a), 1 in ( SELECT b FROM t2 ) AS bb FROM t1;
  2204. MAX(a) bb
  2205. NULL 1
  2206. EXPLAIN
  2207. SELECT MAX(a), 1 >= ALL ( SELECT b FROM t2 ) AS bb FROM t1;
  2208. id select_type table type possible_keys key key_len ref rows Extra
  2209. 1 PRIMARY t1 system NULL NULL NULL NULL 0 const row not found
  2210. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  2211. SELECT MAX(a), 1 >= ALL ( SELECT b FROM t2 ) AS bb FROM t1;
  2212. MAX(a) bb
  2213. NULL 1
  2214. EXPLAIN
  2215. SELECT MAX(a), ( SELECT 1 FROM t2 where b = a) AS bb FROM t1;
  2216. id select_type table type possible_keys key key_len ref rows Extra
  2217. 1 PRIMARY t1 system NULL NULL NULL NULL 0 const row not found
  2218. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE noticed after reading const tables
  2219. SELECT MAX(a), ( SELECT 1 FROM t2 where b = a) AS bb FROM t1;
  2220. MAX(a) bb
  2221. NULL NULL
  2222. EXPLAIN
  2223. SELECT MAX(a), a in ( SELECT b FROM t2 ) AS bb FROM t1;
  2224. id select_type table type possible_keys key key_len ref rows Extra
  2225. 1 PRIMARY t1 system NULL NULL NULL NULL 0 const row not found
  2226. 2 DEPENDENT SUBQUERY t2 system NULL NULL NULL NULL 1
  2227. SELECT MAX(a), a in ( SELECT b FROM t2 ) AS bb FROM t1;
  2228. MAX(a) bb
  2229. NULL NULL
  2230. EXPLAIN
  2231. SELECT MAX(a), a >= ALL ( SELECT b FROM t2 ) AS bb FROM t1;
  2232. id select_type table type possible_keys key key_len ref rows Extra
  2233. 1 PRIMARY t1 system NULL NULL NULL NULL 0 const row not found
  2234. 2 DEPENDENT SUBQUERY t2 system NULL NULL NULL NULL 1
  2235. SELECT MAX(a), a >= ALL ( SELECT b FROM t2 ) AS bb FROM t1;
  2236. MAX(a) bb
  2237. NULL NULL
  2238. drop table t1, t2;
  2239. set optimizer_switch=@subselect4_tmp;
  2240. #
  2241. # MDEV-3928 Assertion `example' failed in Item_cache::is_expensive_processor with a 2-level IN subquery
  2242. #
  2243. CREATE TABLE t1 (a1 INT, b1 TIME) ENGINE=MyISAM;
  2244. INSERT INTO t1 VALUES (4,'21:22:34'),(6,'10:50:38');
  2245. CREATE TABLE t2 (a2 INT, b2 TIME) ENGINE=MyISAM;
  2246. INSERT INTO t2 VALUES (8, '06:17:39');
  2247. CREATE TABLE t3 (a3 INT, b3 TIME) ENGINE=MyISAM;
  2248. INSERT INTO t3 VALUES (1,'00:00:01'),(7,'00:00:02');
  2249. EXPLAIN
  2250. SELECT * FROM t1 WHERE a1 IN (
  2251. SELECT a2 FROM t2 WHERE a2 IN (
  2252. SELECT a3 FROM t3 WHERE b2 = b1 AND b2 <= b1 ORDER BY b3
  2253. )
  2254. );
  2255. id select_type table type possible_keys key key_len ref rows Extra
  2256. 1 PRIMARY t2 system NULL NULL NULL NULL 1
  2257. 1 PRIMARY t1 ALL NULL NULL NULL NULL 2 Using where
  2258. 3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 Using where
  2259. SELECT * FROM t1 WHERE a1 IN (
  2260. SELECT a2 FROM t2 WHERE a2 IN (
  2261. SELECT a3 FROM t3 WHERE b2 = b1 AND b2 <= b1 ORDER BY b3
  2262. )
  2263. );
  2264. a1 b1
  2265. drop table t1, t2, t3;
  2266. #
  2267. # MDEV-4056:Server crashes in Item_func_trig_cond::val_int
  2268. # with FROM and NOT IN subqueries, LEFT JOIN, derived_merge+in_to_exists
  2269. #
  2270. set @optimizer_switch_MDEV4056 = @@optimizer_switch;
  2271. SET optimizer_switch = 'derived_merge=on,in_to_exists=on';
  2272. CREATE TABLE t1 (a VARCHAR(1)) ENGINE=MyISAM;
  2273. INSERT INTO t1 VALUES ('x'),('d');
  2274. CREATE TABLE t2 (pk INT PRIMARY KEY, b INT, c VARCHAR(1)) ENGINE=MyISAM;
  2275. INSERT INTO t2 VALUES (1,2,'v'),(2,150,'v');
  2276. SELECT * FROM t1 LEFT JOIN (
  2277. SELECT * FROM t2 WHERE ( pk, pk ) NOT IN (
  2278. SELECT MIN(b), SUM(pk) FROM t1
  2279. )
  2280. ) AS alias1 ON (a = c)
  2281. WHERE b IS NULL OR a < 'u';
  2282. a pk b c
  2283. x NULL NULL NULL
  2284. d NULL NULL NULL
  2285. drop table t1,t2;
  2286. set @@optimizer_switch = @optimizer_switch_MDEV4056;
  2287. #
  2288. # MDEV-5103: server crashed on singular Item_equal
  2289. #
  2290. CREATE TABLE t1 (
  2291. a enum('p','r') NOT NULL DEFAULT 'r',
  2292. b int NOT NULL DEFAULT '0',
  2293. c char(32) NOT NULL,
  2294. d varchar(255) NOT NULL,
  2295. PRIMARY KEY (a, b), UNIQUE KEY idx(a, c)
  2296. );
  2297. INSERT INTO t1 VALUES ('r', 1, 'ad18832202b199728921807033a8a515', '001_cbr643');
  2298. CREATE TABLE t2 (
  2299. a enum('p','r') NOT NULL DEFAULT 'r',
  2300. b int NOT NULL DEFAULT '0',
  2301. e datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  2302. PRIMARY KEY (a, b, e)
  2303. );
  2304. INSERT INTO t2 VALUES ('r', 1, '2013-10-05 14:25:30');
  2305. SELECT * FROM t1 AS t
  2306. WHERE a='r' AND (c,b) NOT IN (SELECT c,b FROM t2 WHERE (c,b)=(t.c,t.b));
  2307. a b c d
  2308. DROP TABLE t1, t2;
  2309. #
  2310. # MDEV-5468: assertion failure with a simplified condition in subselect
  2311. #
  2312. CREATE TABLE t1 (a int, b int) ENGINE=MyISAM;
  2313. INSERT INTO t1 VALUES (1,1);
  2314. CREATE TABLE t2 ( pk int PRIMARY KEY, c INT) ENGINE=MyISAM;
  2315. INSERT INTO t2 VALUES (1,4), (2,6);
  2316. SELECT ( SELECT MAX(b) FROM t1, t2 WHERE pk = a AND b < from_sq.c ) AS select_sq,
  2317. COUNT( DISTINCT from_sq.c )
  2318. FROM ( SELECT DISTINCT t2_1.* FROM t2 AS t2_1, t2 AS t2_2 ) AS from_sq
  2319. GROUP BY select_sq ;
  2320. select_sq COUNT( DISTINCT from_sq.c )
  2321. 1 2
  2322. DROP TABLE t1,t2;
  2323. CREATE TABLE t1 (id int, a2 char(2), a3 char(3)) ENGINE=MyISAM;
  2324. INSERT INTO t1 VALUES (1,'BE','BEL');
  2325. CREATE TABLE t2 (id int, a2 char(2), a3 char(3)) ENGINE=MyISAM;
  2326. INSERT INTO t2 VALUES (1,'BE','BEL'), (2,'MX','MEX');
  2327. CREATE VIEW v2 AS SELECT DISTINCT * FROM t2;
  2328. SELECT * FROM t1 AS outer_t1, v2
  2329. WHERE v2.a3 = outer_t1.a3
  2330. AND EXISTS ( SELECT * FROM t1 WHERE a2 < v2.a2 AND id = outer_t1.id )
  2331. AND outer_t1.a3 < 'J'
  2332. ORDER BY v2.id;
  2333. id a2 a3 id a2 a3
  2334. DROP VIEW v2;
  2335. DROP TABLE t1,t2;
  2336. #
  2337. # MDEV-5686: degenerate disjunct in NOT IN subquery
  2338. #
  2339. CREATE TABLE t1 (a int, b int, c varchar(3)) ENGINE=MyISAM;
  2340. INSERT INTO t1 VALUES (1,1,'CAN'),(2,2,'AUS');
  2341. CREATE TABLE t2 (f int) ENGINE=MyISAM;
  2342. INSERT INTO t2 VALUES (3);
  2343. EXPLAIN EXTENDED
  2344. SELECT * FROM t2
  2345. WHERE f NOT IN (SELECT b FROM t1
  2346. WHERE 0 OR (c IN ('USA') OR c NOT IN ('USA')) AND a = b);
  2347. id select_type table type possible_keys key key_len ref rows filtered Extra
  2348. 1 PRIMARY t2 system NULL NULL NULL NULL 1 100.00
  2349. 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
  2350. Warnings:
  2351. Note 1003 select 3 AS `f` from dual where !<expr_cache><3>(<in_optimizer>(3,<exists>(select `test`.`t1`.`b` from `test`.`t1` where (`test`.`t1`.`c` = 'USA' or `test`.`t1`.`c` <> 'USA') and trigcond(<cache>(3) = `test`.`t1`.`b` or `test`.`t1`.`b` is null) and `test`.`t1`.`b` = `test`.`t1`.`a` having trigcond(`test`.`t1`.`b` is null))))
  2352. SELECT * FROM t2
  2353. WHERE f NOT IN (SELECT b FROM t1
  2354. WHERE 0 OR (c IN ('USA') OR c NOT IN ('USA')) AND a = b);
  2355. f
  2356. 3
  2357. DROP TABLE t1,t2;
  2358. #
  2359. # MDEV-3899 Valgrind warnings (blocks are definitely lost) in filesort on IN subquery with SUM and DISTINCT
  2360. #
  2361. CREATE TABLE t1 (a INT) ENGINE=MyISAM;
  2362. INSERT INTO t1 VALUES (1),(9);
  2363. CREATE TABLE t2 (b INT) ENGINE=MyISAM;
  2364. INSERT INTO t2 VALUES (8);
  2365. SELECT * FROM t1
  2366. WHERE (1, 1) IN (SELECT a, SUM(DISTINCT a) FROM t1, t2 GROUP BY a);
  2367. a
  2368. 1
  2369. 9
  2370. drop table t1, t2;
  2371. #
  2372. # MDEV-3902 Assertion `record_length == m_record_length' failed at Filesort_buffer::alloc_sort_buffer
  2373. #
  2374. CREATE TABLE t1 (a INT) ENGINE=MyISAM;
  2375. INSERT INTO t1 VALUES (1),(2);
  2376. CREATE TABLE t2 (pk INT PRIMARY KEY, b INT) ENGINE=MyISAM;
  2377. INSERT INTO t2 VALUES (1,1),(2,7);
  2378. CREATE TABLE t3 (c INT) ENGINE=MyISAM;
  2379. INSERT INTO t3 VALUES (8);
  2380. SELECT * FROM t1
  2381. WHERE (1, 5) IN (SELECT b, SUM(DISTINCT b) FROM t2, t3 GROUP BY b);
  2382. a
  2383. SELECT * FROM t2 AS alias1, t2 AS alias2
  2384. WHERE EXISTS ( SELECT 1 ) AND (alias2.pk = alias1.b )
  2385. ORDER BY alias1.b;
  2386. pk b pk b
  2387. 1 1 1 1
  2388. drop table t1, t2, t3;
  2389. #
  2390. # MDEV-4144 simple subquery causes full scan instead of range scan
  2391. #
  2392. CREATE TABLE t1 (id int not null auto_increment, x int not null, primary key(id));
  2393. INSERT INTO t1 (x) VALUES (0),(0),(0);
  2394. EXPLAIN
  2395. SELECT x FROM t1 WHERE id > (SELECT MAX(id) - 1000 FROM t1) ORDER BY x LIMIT 1;
  2396. id select_type table type possible_keys key key_len ref rows Extra
  2397. 1 PRIMARY t1 range PRIMARY PRIMARY 4 NULL 3 Using where; Using filesort
  2398. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away
  2399. SELECT x FROM t1 WHERE id > (SELECT MAX(id) - 1000 FROM t1) ORDER BY x LIMIT 1;
  2400. x
  2401. 0
  2402. drop table t1;
  2403. #
  2404. # MDEV-7691: Assertion `outer_context || !*from_field || *from_field == not_found_field' ...
  2405. #
  2406. set optimizer_switch=default;
  2407. CREATE TABLE t1 (a INT) ENGINE=MyISAM;
  2408. INSERT INTO t1 VALUES (4),(6);
  2409. CREATE TABLE t2 (b INT) ENGINE=MyISAM;
  2410. INSERT INTO t2 VALUES (1),(8);
  2411. PREPARE stmt FROM "
  2412. SELECT * FROM t2
  2413. HAVING 0 IN (
  2414. SELECT a FROM t1
  2415. WHERE a IN (
  2416. SELECT a FROM t1
  2417. WHERE b = a
  2418. )
  2419. )
  2420. ";
  2421. EXECUTE stmt;
  2422. b
  2423. EXECUTE stmt;
  2424. b
  2425. # Alternative test case, without HAVING
  2426. CREATE TABLE t3 (i INT) ENGINE=MyISAM;
  2427. INSERT INTO t3 VALUES (4),(6);
  2428. PREPARE stmt FROM "
  2429. SELECT * FROM t3 AS t10
  2430. WHERE EXISTS (
  2431. SELECT * FROM t3 AS t20 WHERE t10.i IN (
  2432. SELECT i FROM t3
  2433. )
  2434. )";
  2435. EXECUTE stmt;
  2436. i
  2437. 4
  2438. 6
  2439. EXECUTE stmt;
  2440. i
  2441. 4
  2442. 6
  2443. drop table t1, t2, t3;
  2444. #
  2445. # MDEV-11078: NULL NOT IN (non-empty subquery) should never return results
  2446. #
  2447. create table t1(a int,b int);
  2448. create table t2(a int,b int);
  2449. insert into t1 value (1,2);
  2450. select (NULL) in (select 1 from t1);
  2451. (NULL) in (select 1 from t1)
  2452. NULL
  2453. select (null) in (select 1 from t2);
  2454. (null) in (select 1 from t2)
  2455. 0
  2456. select 1 in (select 1 from t1);
  2457. 1 in (select 1 from t1)
  2458. 1
  2459. select 1 in (select 1 from t2);
  2460. 1 in (select 1 from t2)
  2461. 0
  2462. select 1 from dual where null in (select 1 from t1);
  2463. 1
  2464. select 1 from dual where null in (select 1 from t2);
  2465. 1
  2466. select (null,null) in (select * from t1);
  2467. (null,null) in (select * from t1)
  2468. NULL
  2469. select (null,null) in (select * from t2);
  2470. (null,null) in (select * from t2)
  2471. 0
  2472. select 1 from dual where null not in (select 1 from t1);
  2473. 1
  2474. select 1 from dual where null not in (select 1 from t2);
  2475. 1
  2476. 1
  2477. drop table t1,t2;
  2478. #
  2479. # MDEV-6486: Assertion `!table || (!table->read_set || bitmap_is_set(table->read_set, field_index))'
  2480. # failed with SELECT SQ, TEXT field
  2481. #
  2482. CREATE TABLE t1 (a VARCHAR(8), KEY(a)) ENGINE=MyISAM;
  2483. INSERT INTO t1 VALUES ('foo'),( 'bar');
  2484. CREATE TABLE t2 (b VARCHAR(8), c TINYTEXT, KEY(b)) ENGINE=MyISAM;
  2485. INSERT INTO t2 VALUES ('baz','baz'),('qux', 'qux');
  2486. SELECT ( SELECT COUNT(*) FROM t1 WHERE a = c ) AS field, COUNT(DISTINCT c)
  2487. FROM t2 WHERE b <= 'quux' GROUP BY field;
  2488. field COUNT(DISTINCT c)
  2489. 0 1
  2490. drop table t1,t2;
  2491. #
  2492. # MDEV-15555: select from DUAL where false yielding wrong result when in a IN
  2493. #
  2494. explain
  2495. SELECT 2 IN (SELECT 2 from DUAL WHERE 1 != 1);
  2496. id select_type table type possible_keys key key_len ref rows Extra
  2497. 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL No tables used
  2498. 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
  2499. SELECT 2 IN (SELECT 2 from DUAL WHERE 1 != 1);
  2500. 2 IN (SELECT 2 from DUAL WHERE 1 != 1)
  2501. 0
  2502. SET optimizer_switch= @@global.optimizer_switch;
  2503. set @@tmp_table_size= @@global.tmp_table_size;
  2504. #
  2505. # MDEV-14515: Wrong results for tableless query with subquery in WHERE
  2506. # and implicit aggregation
  2507. #
  2508. create table t1 (i1 int, i2 int);
  2509. insert into t1 values (1314, 1084),(1330, 1084),(1401, 1084),(580, 1084);
  2510. create table t2 (cd int);
  2511. insert into t2 values
  2512. (1330), (1330), (1330), (1330), (1330), (1330), (1330), (1330),
  2513. (1330), (1330), (1330), (1330), (1330), (1330), (1330), (1330);
  2514. select max(10) from dual
  2515. where exists (select 1 from t2 join t1 on t1.i1 = t2.cd and t1.i2 = 345);
  2516. max(10)
  2517. NULL
  2518. insert into t2 select * from t2;
  2519. select max(10) from dual
  2520. where exists (select 1 from t2 join t1 on t1.i1 = t2.cd and t1.i2 = 345);
  2521. max(10)
  2522. NULL
  2523. DROP TABLE t1,t2;
  2524. #
  2525. # MDEV-10232 Scalar result of subquery changes after adding an outer select stmt
  2526. #
  2527. create table t1(c1 int, c2 int, primary key(c2));
  2528. insert into t1 values(2,1),(1,2);
  2529. select (select c1 from t1 group by c1,c2 order by c1 limit 1) as x;
  2530. x
  2531. 1
  2532. (select c1 from t1 group by c1,c2 order by c1 limit 1);
  2533. c1
  2534. 1
  2535. drop table t1;