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.

315 lines
10 KiB

24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
24 years ago
Fixed following problems: --Bug#52157 various crashes and assertions with multi-table update, stored function --Bug#54475 improper error handling causes cascading crashing failures in innodb/ndb --Bug#57703 create view cause Assertion failed: 0, file .\item_subselect.cc, line 846 --Bug#57352 valgrind warnings when creating view --Recently discovered problem when a nested materialized derived table is used before being populated and it leads to incorrect result We have several modes when we should disable subquery evaluation. The reasons for disabling are different. It could be uselessness of the evaluation as in case of 'CREATE VIEW' or 'PREPARE stmt', or we should disable subquery evaluation if tables are not locked yet as it happens in bug#54475, or too early evaluation of subqueries can lead to wrong result as it happened in Bug#19077. Main problem is that if subquery items are treated as const they are evaluated in ::fix_fields(), ::fix_length_and_dec() of the parental items as a lot of these methods have Item::val_...() calls inside. We have to make subqueries non-const to prevent unnecessary subquery evaluation. At the moment we have different methods for this. Here is a list of these modes: 1. PREPARE stmt; We use UNCACHEABLE_PREPARE flag. It is set during parsing in sql_parse.cc, mysql_new_select() for each SELECT_LEX object and cleared at the end of PREPARE in sql_prepare.cc, init_stmt_after_parse(). If this flag is set subquery becomes non-const and evaluation does not happen. 2. CREATE|ALTER VIEW, SHOW CREATE VIEW, I_S tables which process FRM files We use LEX::view_prepare_mode field. We set it before view preparation and check this flag in ::fix_fields(), ::fix_length_and_dec(). Some bugs are fixed using this approach, some are not(Bug#57352, Bug#57703). The problem here is that we have a lot of ::fix_fields(), ::fix_length_and_dec() where we use Item::val_...() calls for const items. 3. Derived tables with subquery = wrong result(Bug19077) The reason of this bug is too early subquery evaluation. It was fixed by adding Item::with_subselect field The check of this field in appropriate places prevents const item evaluation if the item have subquery. The fix for Bug19077 fixes only the problem with convert_constant_item() function and does not cover other places(::fix_fields(), ::fix_length_and_dec() again) where subqueries could be evaluated. Example: CREATE TABLE t1 (i INT, j BIGINT); INSERT INTO t1 VALUES (1, 2), (2, 2), (3, 2); SELECT * FROM (SELECT MIN(i) FROM t1 WHERE j = SUBSTRING('12', (SELECT * FROM (SELECT MIN(j) FROM t1) t2))) t3; DROP TABLE t1; 4. Derived tables with subquery where subquery is evaluated before table locking(Bug#54475, Bug#52157) Suggested solution is following: -Introduce new field LEX::context_analysis_only with the following possible flags: #define CONTEXT_ANALYSIS_ONLY_PREPARE 1 #define CONTEXT_ANALYSIS_ONLY_VIEW 2 #define CONTEXT_ANALYSIS_ONLY_DERIVED 4 -Set/clean these flags when we perform context analysis operation -Item_subselect::const_item() returns result depending on LEX::context_analysis_only. If context_analysis_only is set then we return FALSE that means that subquery is non-const. As all subquery types are wrapped by Item_subselect it allow as to make subquery non-const when it's necessary.
15 years ago
Fixed following problems: --Bug#52157 various crashes and assertions with multi-table update, stored function --Bug#54475 improper error handling causes cascading crashing failures in innodb/ndb --Bug#57703 create view cause Assertion failed: 0, file .\item_subselect.cc, line 846 --Bug#57352 valgrind warnings when creating view --Recently discovered problem when a nested materialized derived table is used before being populated and it leads to incorrect result We have several modes when we should disable subquery evaluation. The reasons for disabling are different. It could be uselessness of the evaluation as in case of 'CREATE VIEW' or 'PREPARE stmt', or we should disable subquery evaluation if tables are not locked yet as it happens in bug#54475, or too early evaluation of subqueries can lead to wrong result as it happened in Bug#19077. Main problem is that if subquery items are treated as const they are evaluated in ::fix_fields(), ::fix_length_and_dec() of the parental items as a lot of these methods have Item::val_...() calls inside. We have to make subqueries non-const to prevent unnecessary subquery evaluation. At the moment we have different methods for this. Here is a list of these modes: 1. PREPARE stmt; We use UNCACHEABLE_PREPARE flag. It is set during parsing in sql_parse.cc, mysql_new_select() for each SELECT_LEX object and cleared at the end of PREPARE in sql_prepare.cc, init_stmt_after_parse(). If this flag is set subquery becomes non-const and evaluation does not happen. 2. CREATE|ALTER VIEW, SHOW CREATE VIEW, I_S tables which process FRM files We use LEX::view_prepare_mode field. We set it before view preparation and check this flag in ::fix_fields(), ::fix_length_and_dec(). Some bugs are fixed using this approach, some are not(Bug#57352, Bug#57703). The problem here is that we have a lot of ::fix_fields(), ::fix_length_and_dec() where we use Item::val_...() calls for const items. 3. Derived tables with subquery = wrong result(Bug19077) The reason of this bug is too early subquery evaluation. It was fixed by adding Item::with_subselect field The check of this field in appropriate places prevents const item evaluation if the item have subquery. The fix for Bug19077 fixes only the problem with convert_constant_item() function and does not cover other places(::fix_fields(), ::fix_length_and_dec() again) where subqueries could be evaluated. Example: CREATE TABLE t1 (i INT, j BIGINT); INSERT INTO t1 VALUES (1, 2), (2, 2), (3, 2); SELECT * FROM (SELECT MIN(i) FROM t1 WHERE j = SUBSTRING('12', (SELECT * FROM (SELECT MIN(j) FROM t1) t2))) t3; DROP TABLE t1; 4. Derived tables with subquery where subquery is evaluated before table locking(Bug#54475, Bug#52157) Suggested solution is following: -Introduce new field LEX::context_analysis_only with the following possible flags: #define CONTEXT_ANALYSIS_ONLY_PREPARE 1 #define CONTEXT_ANALYSIS_ONLY_VIEW 2 #define CONTEXT_ANALYSIS_ONLY_DERIVED 4 -Set/clean these flags when we perform context analysis operation -Item_subselect::const_item() returns result depending on LEX::context_analysis_only. If context_analysis_only is set then we return FALSE that means that subquery is non-const. As all subquery types are wrapped by Item_subselect it allow as to make subquery non-const when it's necessary.
15 years ago
21 years ago
24 years ago
  1. /* Copyright (C) 2002-2003 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /*
  13. Derived tables
  14. These were introduced by Sinisa <sinisa@mysql.com>
  15. */
  16. #include "mysql_priv.h"
  17. #include "sql_select.h"
  18. /*
  19. Call given derived table processor (preparing or filling tables)
  20. SYNOPSIS
  21. mysql_handle_derived()
  22. lex LEX for this thread
  23. processor procedure of derived table processing
  24. RETURN
  25. FALSE OK
  26. TRUE Error
  27. */
  28. bool
  29. mysql_handle_derived(LEX *lex, bool (*processor)(THD*, LEX*, TABLE_LIST*))
  30. {
  31. bool res= FALSE;
  32. if (lex->derived_tables)
  33. {
  34. lex->thd->derived_tables_processing= TRUE;
  35. for (SELECT_LEX *sl= lex->all_selects_list;
  36. sl;
  37. sl= sl->next_select_in_list())
  38. {
  39. for (TABLE_LIST *cursor= sl->get_table_list();
  40. cursor;
  41. cursor= cursor->next_local)
  42. {
  43. if ((res= (*processor)(lex->thd, lex, cursor)))
  44. goto out;
  45. }
  46. if (lex->describe)
  47. {
  48. /*
  49. Force join->join_tmp creation, because we will use this JOIN
  50. twice for EXPLAIN and we have to have unchanged join for EXPLAINing
  51. */
  52. sl->uncacheable|= UNCACHEABLE_EXPLAIN;
  53. sl->master_unit()->uncacheable|= UNCACHEABLE_EXPLAIN;
  54. }
  55. }
  56. }
  57. out:
  58. lex->thd->derived_tables_processing= FALSE;
  59. return res;
  60. }
  61. /**
  62. @brief Create temporary table structure (but do not fill it).
  63. @param thd Thread handle
  64. @param lex LEX for this thread
  65. @param orig_table_list TABLE_LIST for the upper SELECT
  66. @details
  67. This function is called before any command containing derived tables is
  68. executed. Currently the function is used for derived tables, i.e.
  69. - Anonymous derived tables, or
  70. - Named derived tables (aka views) with the @c TEMPTABLE algorithm.
  71. The table reference, contained in @c orig_table_list, is updated with the
  72. fields of a new temporary table.
  73. Derived tables are stored in @c thd->derived_tables and closed by
  74. close_thread_tables().
  75. This function is part of the procedure that starts in
  76. open_and_lock_tables(), a procedure that - among other things - introduces
  77. new table and table reference objects (to represent derived tables) that
  78. don't exist in the privilege database. This means that normal privilege
  79. checking cannot handle them. Hence this function does some extra tricks in
  80. order to bypass normal privilege checking, by exploiting the fact that the
  81. current state of privilege verification is attached as GRANT_INFO structures
  82. on the relevant TABLE and TABLE_REF objects.
  83. For table references, the current state of accrued access is stored inside
  84. TABLE_LIST::grant. Hence this function must update the state of fulfilled
  85. privileges for the new TABLE_LIST, an operation which is normally performed
  86. exclusively by the table and database access checking functions,
  87. check_access() and check_grant(), respectively. This modification is done
  88. for both views and anonymous derived tables: The @c SELECT privilege is set
  89. as fulfilled by the user. However, if a view is referenced and the table
  90. reference is queried against directly (see TABLE_LIST::referencing_view),
  91. the state of privilege checking (GRANT_INFO struct) is copied as-is to the
  92. temporary table.
  93. This function implements a signature called "derived table processor", and
  94. is passed as a function pointer to mysql_handle_derived().
  95. @note This function sets @c SELECT_ACL for @c TEMPTABLE views as well as
  96. anonymous derived tables, but this is ok since later access checking will
  97. distinguish between them.
  98. @see mysql_handle_derived(), mysql_derived_filling(), GRANT_INFO
  99. @return
  100. false OK
  101. true Error
  102. */
  103. bool mysql_derived_prepare(THD *thd, LEX *lex, TABLE_LIST *orig_table_list)
  104. {
  105. SELECT_LEX_UNIT *unit= orig_table_list->derived;
  106. ulonglong create_options;
  107. DBUG_ENTER("mysql_derived_prepare");
  108. bool res= FALSE;
  109. if (unit)
  110. {
  111. SELECT_LEX *first_select= unit->first_select();
  112. TABLE *table= 0;
  113. select_union *derived_result;
  114. /* prevent name resolving out of derived table */
  115. for (SELECT_LEX *sl= first_select; sl; sl= sl->next_select())
  116. sl->context.outer_context= 0;
  117. if (!(derived_result= new select_union))
  118. DBUG_RETURN(TRUE); // out of memory
  119. lex->context_analysis_only|= CONTEXT_ANALYSIS_ONLY_DERIVED;
  120. // st_select_lex_unit::prepare correctly work for single select
  121. if ((res= unit->prepare(thd, derived_result, 0)))
  122. goto exit;
  123. lex->context_analysis_only&= ~CONTEXT_ANALYSIS_ONLY_DERIVED;
  124. if ((res= check_duplicate_names(unit->types, 0)))
  125. goto exit;
  126. create_options= (first_select->options | thd->options |
  127. TMP_TABLE_ALL_COLUMNS);
  128. /*
  129. Temp table is created so that it hounours if UNION without ALL is to be
  130. processed
  131. As 'distinct' parameter we always pass FALSE (0), because underlying
  132. query will control distinct condition by itself. Correct test of
  133. distinct underlying query will be is_union &&
  134. !unit->union_distinct->next_select() (i.e. it is union and last distinct
  135. SELECT is last SELECT of UNION).
  136. */
  137. if ((res= derived_result->create_result_table(thd, &unit->types, FALSE,
  138. create_options,
  139. orig_table_list->alias)))
  140. goto exit;
  141. table= derived_result->table;
  142. exit:
  143. /* Hide "Unknown column" or "Unknown function" error */
  144. if (orig_table_list->view)
  145. {
  146. if (thd->is_error() &&
  147. (thd->main_da.sql_errno() == ER_BAD_FIELD_ERROR ||
  148. thd->main_da.sql_errno() == ER_FUNC_INEXISTENT_NAME_COLLISION ||
  149. thd->main_da.sql_errno() == ER_SP_DOES_NOT_EXIST))
  150. {
  151. thd->clear_error();
  152. my_error(ER_VIEW_INVALID, MYF(0), orig_table_list->db,
  153. orig_table_list->table_name);
  154. }
  155. }
  156. /*
  157. if it is preparation PS only or commands that need only VIEW structure
  158. then we do not need real data and we can skip execution (and parameters
  159. is not defined, too)
  160. */
  161. if (res)
  162. {
  163. if (table)
  164. free_tmp_table(thd, table);
  165. delete derived_result;
  166. }
  167. else
  168. {
  169. if (!thd->fill_derived_tables())
  170. {
  171. delete derived_result;
  172. derived_result= NULL;
  173. }
  174. orig_table_list->derived_result= derived_result;
  175. orig_table_list->table= table;
  176. orig_table_list->table_name= table->s->table_name.str;
  177. orig_table_list->table_name_length= table->s->table_name.length;
  178. table->derived_select_number= first_select->select_number;
  179. table->s->tmp_table= NON_TRANSACTIONAL_TMP_TABLE;
  180. #ifndef NO_EMBEDDED_ACCESS_CHECKS
  181. if (orig_table_list->referencing_view)
  182. table->grant= orig_table_list->grant;
  183. else
  184. table->grant.privilege= SELECT_ACL;
  185. #endif
  186. orig_table_list->db= (char *)"";
  187. orig_table_list->db_length= 0;
  188. // Force read of table stats in the optimizer
  189. table->file->info(HA_STATUS_VARIABLE);
  190. /* Add new temporary table to list of open derived tables */
  191. table->next= thd->derived_tables;
  192. thd->derived_tables= table;
  193. }
  194. }
  195. else if (orig_table_list->merge_underlying_list)
  196. orig_table_list->set_underlying_merge();
  197. DBUG_RETURN(res);
  198. }
  199. /*
  200. fill derived table
  201. SYNOPSIS
  202. mysql_derived_filling()
  203. thd Thread handle
  204. lex LEX for this thread
  205. unit node that contains all SELECT's for derived tables
  206. orig_table_list TABLE_LIST for the upper SELECT
  207. IMPLEMENTATION
  208. Derived table is resolved with temporary table. It is created based on the
  209. queries defined. After temporary table is filled, if this is not EXPLAIN,
  210. then the entire unit / node is deleted. unit is deleted if UNION is used
  211. for derived table and node is deleted is it is a simple SELECT.
  212. If you use this function, make sure it's not called at prepare.
  213. Due to evaluation of LIMIT clause it can not be used at prepared stage.
  214. RETURN
  215. FALSE OK
  216. TRUE Error
  217. */
  218. bool mysql_derived_filling(THD *thd, LEX *lex, TABLE_LIST *orig_table_list)
  219. {
  220. TABLE *table= orig_table_list->table;
  221. SELECT_LEX_UNIT *unit= orig_table_list->derived;
  222. bool res= FALSE;
  223. /*check that table creation pass without problem and it is derived table */
  224. if (table && unit)
  225. {
  226. SELECT_LEX *first_select= unit->first_select();
  227. select_union *derived_result= orig_table_list->derived_result;
  228. SELECT_LEX *save_current_select= lex->current_select;
  229. if (unit->is_union())
  230. {
  231. // execute union without clean up
  232. res= unit->exec();
  233. }
  234. else
  235. {
  236. unit->set_limit(first_select);
  237. if (unit->select_limit_cnt == HA_POS_ERROR)
  238. first_select->options&= ~OPTION_FOUND_ROWS;
  239. lex->current_select= first_select;
  240. res= mysql_select(thd, &first_select->ref_pointer_array,
  241. first_select->table_list.first,
  242. first_select->with_wild,
  243. first_select->item_list, first_select->where,
  244. (first_select->order_list.elements+
  245. first_select->group_list.elements),
  246. first_select->order_list.first,
  247. first_select->group_list.first,
  248. first_select->having, (ORDER*) NULL,
  249. (first_select->options | thd->options |
  250. SELECT_NO_UNLOCK),
  251. derived_result, unit, first_select);
  252. }
  253. if (!res)
  254. {
  255. /*
  256. Here we entirely fix both TABLE_LIST and list of SELECT's as
  257. there were no derived tables
  258. */
  259. if (derived_result->flush())
  260. res= TRUE;
  261. if (!lex->describe)
  262. unit->cleanup();
  263. }
  264. else
  265. unit->cleanup();
  266. lex->current_select= save_current_select;
  267. }
  268. return res;
  269. }