1 /* Copyright (c) 2000, 2016, Oracle and/or its affiliates.
2    Copyright (c) 2010, 2021, MariaDB
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA */
16 
17 
18 /* Basic functions needed by many modules */
19 
20 #include "mariadb.h"
21 #include "sql_base.h"                           // setup_table_map
22 #include "sql_priv.h"
23 #include "unireg.h"
24 #include "debug_sync.h"
25 #include "lock.h"        // mysql_lock_remove,
26                          // mysql_unlock_tables,
27                          // mysql_lock_have_duplicate
28 #include "sql_show.h"    // append_identifier
29 #include "strfunc.h"     // find_type
30 #include "sql_view.h"    // mysql_make_view, VIEW_ANY_ACL
31 #include "sql_parse.h"   // check_table_access
32 #include "sql_insert.h"  // kill_delayed_threads
33 #include "sql_partition.h"               // ALTER_PARTITION_PARAM_TYPE
34 #include "sql_derived.h" // mysql_derived_prepare,
35                          // mysql_handle_derived,
36                          // mysql_derived_filling
37 #include "sql_handler.h" // mysql_ha_flush
38 #include "sql_test.h"
39 #include "sql_partition.h"                      // ALTER_PARTITION_PARAM_TYPE
40 #include "log_event.h"                          // Query_log_event
41 #include "sql_select.h"
42 #include "sp_head.h"
43 #include "sp.h"
44 #include "sp_cache.h"
45 #include "sql_trigger.h"
46 #include "transaction.h"
47 #include "sql_prepare.h"
48 #include "sql_statistics.h"
49 #include "sql_cte.h"
50 #include <m_ctype.h>
51 #include <my_dir.h>
52 #include <hash.h>
53 #include "rpl_filter.h"
54 #include "sql_table.h"                          // build_table_filename
55 #include "datadict.h"   // dd_frm_is_view()
56 #include "rpl_rli.h"   // rpl_group_info
57 #ifdef  __WIN__
58 #include <io.h>
59 #endif
60 #include "wsrep_mysqld.h"
61 #ifdef WITH_WSREP
62 #include "wsrep_thd.h"
63 #include "wsrep_trans_observer.h"
64 #endif /* WITH_WSREP */
65 
66 
67 bool
handle_condition(THD *,uint sql_errno,const char *,Sql_condition::enum_warning_level * level,const char *,Sql_condition ** cond_hdl)68 No_such_table_error_handler::handle_condition(THD *,
69                                               uint sql_errno,
70                                               const char*,
71                                               Sql_condition::enum_warning_level *level,
72                                               const char*,
73                                               Sql_condition ** cond_hdl)
74 {
75   *cond_hdl= NULL;
76   if (sql_errno == ER_NO_SUCH_TABLE || sql_errno == ER_NO_SUCH_TABLE_IN_ENGINE)
77   {
78     m_handled_errors++;
79     return TRUE;
80   }
81 
82   if (*level == Sql_condition::WARN_LEVEL_ERROR)
83     m_unhandled_errors++;
84   return FALSE;
85 }
86 
87 
safely_trapped_errors()88 bool No_such_table_error_handler::safely_trapped_errors()
89 {
90   /*
91     If m_unhandled_errors != 0, something else, unanticipated, happened,
92     so the error is not trapped but returned to the caller.
93     Multiple ER_NO_SUCH_TABLE can be raised in case of views.
94   */
95   return ((m_handled_errors > 0) && (m_unhandled_errors == 0));
96 }
97 
98 
99 /**
100   This internal handler is used to trap ER_NO_SUCH_TABLE and
101   ER_WRONG_MRG_TABLE errors during CHECK/REPAIR TABLE for MERGE
102   tables.
103 */
104 
105 class Repair_mrg_table_error_handler : public Internal_error_handler
106 {
107 public:
Repair_mrg_table_error_handler()108   Repair_mrg_table_error_handler()
109     : m_handled_errors(false), m_unhandled_errors(false)
110   {}
111 
112   bool handle_condition(THD *thd,
113                         uint sql_errno,
114                         const char* sqlstate,
115                         Sql_condition::enum_warning_level *level,
116                         const char* msg,
117                         Sql_condition ** cond_hdl);
118 
119   /**
120     Returns TRUE if there were ER_NO_SUCH_/WRONG_MRG_TABLE and there
121     were no unhandled errors. FALSE otherwise.
122   */
safely_trapped_errors()123   bool safely_trapped_errors()
124   {
125     /*
126       Check for m_handled_errors is here for extra safety.
127       It can be useful in situation when call to open_table()
128       fails because some error which was suppressed by another
129       error handler (e.g. in case of MDL deadlock which we
130       decided to solve by back-off and retry).
131     */
132     return (m_handled_errors && (! m_unhandled_errors));
133   }
134 
135 private:
136   bool m_handled_errors;
137   bool m_unhandled_errors;
138 };
139 
140 
141 bool
handle_condition(THD *,uint sql_errno,const char *,Sql_condition::enum_warning_level * level,const char *,Sql_condition ** cond_hdl)142 Repair_mrg_table_error_handler::handle_condition(THD *,
143                                                  uint sql_errno,
144                                                  const char*,
145                                                  Sql_condition::enum_warning_level *level,
146                                                  const char*,
147                                                  Sql_condition ** cond_hdl)
148 {
149   *cond_hdl= NULL;
150   if (sql_errno == ER_NO_SUCH_TABLE ||
151       sql_errno == ER_NO_SUCH_TABLE_IN_ENGINE ||
152       sql_errno == ER_WRONG_MRG_TABLE)
153   {
154     m_handled_errors= true;
155     return TRUE;
156   }
157 
158   m_unhandled_errors= true;
159   return FALSE;
160 }
161 
162 
163 /**
164   @defgroup Data_Dictionary Data Dictionary
165   @{
166 */
167 
168 static bool check_and_update_table_version(THD *thd, TABLE_LIST *tables,
169                                            TABLE_SHARE *table_share);
170 static bool open_table_entry_fini(THD *thd, TABLE_SHARE *share, TABLE *entry);
171 static bool auto_repair_table(THD *thd, TABLE_LIST *table_list);
172 
173 
174 /**
175   Get table cache key for a table list element.
176 
177   @param table_list[in]  Table list element.
178   @param key[out]        On return points to table cache key for the table.
179 
180   @note Unlike create_table_def_key() call this function doesn't construct
181         key in a buffer provided by caller. Instead it relies on the fact
182         that table list element for which key is requested has properly
183         initialized MDL_request object and the fact that table definition
184         cache key is suffix of key used in MDL subsystem. So to get table
185         definition key it simply needs to return pointer to appropriate
186         part of MDL_key object nested in this table list element.
187         Indeed, this means that lifetime of key produced by this call is
188         limited by the lifetime of table list element which it got as
189         parameter.
190 
191   @return Length of key.
192 */
193 
get_table_def_key(const TABLE_LIST * table_list,const char ** key)194 uint get_table_def_key(const TABLE_LIST *table_list, const char **key)
195 {
196   /*
197     This call relies on the fact that TABLE_LIST::mdl_request::key object
198     is properly initialized, so table definition cache can be produced
199     from key used by MDL subsystem.
200   */
201   DBUG_ASSERT(!strcmp(table_list->get_db_name(),
202                       table_list->mdl_request.key.db_name()));
203   DBUG_ASSERT(!strcmp(table_list->get_table_name(),
204                       table_list->mdl_request.key.name()));
205 
206   *key= (const char*)table_list->mdl_request.key.ptr() + 1;
207   return table_list->mdl_request.key.length() - 1;
208 }
209 
210 
211 
212 /*****************************************************************************
213   Functions to handle table definition cache (TABLE_SHARE)
214 *****************************************************************************/
215 
216 /*
217   Create a list for all open tables matching SQL expression
218 
219   SYNOPSIS
220     list_open_tables()
221     thd			Thread THD
222     wild		SQL like expression
223 
224   NOTES
225     One gets only a list of tables for which one has any kind of privilege.
226     db and table names are allocated in result struct, so one doesn't need
227     a lock when traversing the return list.
228 
229   RETURN VALUES
230     NULL	Error (Probably OOM)
231     #		Pointer to list of names of open tables.
232 */
233 
234 struct list_open_tables_arg
235 {
236   THD *thd;
237   const char *db;
238   const char *wild;
239   TABLE_LIST table_list;
240   OPEN_TABLE_LIST **start_list, *open_list;
241 };
242 
243 
list_open_tables_callback(TDC_element * element,list_open_tables_arg * arg)244 static my_bool list_open_tables_callback(TDC_element *element,
245                                          list_open_tables_arg *arg)
246 {
247   const char *db= (char*) element->m_key;
248   size_t db_length= strlen(db);
249   const char *table_name= db + db_length + 1;
250 
251   if (arg->db && my_strcasecmp(system_charset_info, arg->db, db))
252     return FALSE;
253   if (arg->wild && wild_compare(table_name, arg->wild, 0))
254     return FALSE;
255 
256   /* Check if user has SELECT privilege for any column in the table */
257   arg->table_list.db.str= db;
258   arg->table_list.db.length= db_length;
259   arg->table_list.table_name.str= table_name;
260   arg->table_list.table_name.length= strlen(table_name);
261   arg->table_list.grant.privilege= NO_ACL;
262 
263   if (check_table_access(arg->thd, SELECT_ACL, &arg->table_list, TRUE, 1, TRUE))
264     return FALSE;
265 
266   if (!(*arg->start_list= (OPEN_TABLE_LIST *) arg->thd->alloc(
267                     sizeof(**arg->start_list) + element->m_key_length)))
268     return TRUE;
269 
270   strmov((*arg->start_list)->table=
271          strmov(((*arg->start_list)->db= (char*) ((*arg->start_list) + 1)),
272                 db) + 1, table_name);
273   (*arg->start_list)->in_use= 0;
274 
275   mysql_mutex_lock(&element->LOCK_table_share);
276   All_share_tables_list::Iterator it(element->all_tables);
277   TABLE *table;
278   while ((table= it++))
279     if (table->in_use)
280       ++(*arg->start_list)->in_use;
281   mysql_mutex_unlock(&element->LOCK_table_share);
282   (*arg->start_list)->locked= 0;                   /* Obsolete. */
283   arg->start_list= &(*arg->start_list)->next;
284   *arg->start_list= 0;
285   return FALSE;
286 }
287 
288 
list_open_tables(THD * thd,const char * db,const char * wild)289 OPEN_TABLE_LIST *list_open_tables(THD *thd, const char *db, const char *wild)
290 {
291   list_open_tables_arg argument;
292   DBUG_ENTER("list_open_tables");
293 
294   argument.thd= thd;
295   argument.db= db;
296   argument.wild= wild;
297   bzero((char*) &argument.table_list, sizeof(argument.table_list));
298   argument.start_list= &argument.open_list;
299   argument.open_list= 0;
300 
301   if (tdc_iterate(thd, (my_hash_walk_action) list_open_tables_callback,
302                   &argument, true))
303     DBUG_RETURN(0);
304 
305   DBUG_RETURN(argument.open_list);
306 }
307 
308 
309 /**
310    Close all tables that are not in use in table definition cache
311 */
312 
purge_tables()313 void purge_tables()
314 {
315   /*
316     Force close of all open tables.
317 
318     Note that code in TABLE_SHARE::wait_for_old_version() assumes that
319     incrementing of refresh_version is followed by purge of unused table
320     shares.
321   */
322   kill_delayed_threads();
323   /*
324     Get rid of all unused TABLE and TABLE_SHARE instances. By doing
325     this we automatically close all tables which were marked as "old".
326   */
327   tc_purge();
328   /* Free table shares which were not freed implicitly by loop above. */
329   tdc_purge(true);
330 }
331 
332 
333 /**
334    close_cached_tables
335 
336    This function has two separate usages:
337    1) Close not used tables in the table cache to free memory
338    2) Close a list of tables and wait until they are not used anymore. This
339       is used mainly when preparing a table for export.
340 
341    If there are locked tables, they are closed and reopened before
342    function returns. This is done to ensure that table files will be closed
343    by all threads and thus external copyable when FLUSH TABLES returns.
344 */
345 
close_cached_tables(THD * thd,TABLE_LIST * tables,bool wait_for_refresh,ulong timeout)346 bool close_cached_tables(THD *thd, TABLE_LIST *tables,
347                          bool wait_for_refresh, ulong timeout)
348 {
349   DBUG_ENTER("close_cached_tables");
350   DBUG_ASSERT(thd || (!wait_for_refresh && !tables));
351   DBUG_ASSERT(wait_for_refresh || !tables);
352 
353   if (!tables)
354   {
355     /* Free tables that are not used */
356     purge_tables();
357     if (!wait_for_refresh)
358       DBUG_RETURN(false);
359   }
360 
361   DBUG_PRINT("info", ("open table definitions: %d",
362                       (int) tdc_records()));
363 
364   if (thd->locked_tables_mode)
365   {
366     /*
367       If we are under LOCK TABLES, we need to reopen the tables without
368       opening a door for any concurrent threads to sneak in and get
369       lock on our tables. To achieve this we use exclusive metadata
370       locks.
371     */
372     TABLE_LIST *tables_to_reopen= (tables ? tables :
373                                   thd->locked_tables_list.locked_tables());
374     bool result= false;
375 
376     /* close open HANDLER for this thread to allow table to be closed */
377     mysql_ha_flush_tables(thd, tables_to_reopen);
378 
379     for (TABLE_LIST *table_list= tables_to_reopen; table_list;
380          table_list= table_list->next_global)
381     {
382       int err;
383       /* A check that the table was locked for write is done by the caller. */
384       TABLE *table= find_table_for_mdl_upgrade(thd, table_list->db.str,
385                                             table_list->table_name.str, &err);
386 
387       /* May return NULL if this table has already been closed via an alias. */
388       if (! table)
389         continue;
390 
391       if (wait_while_table_is_used(thd, table,
392                                    HA_EXTRA_PREPARE_FOR_FORCED_CLOSE))
393       {
394         result= true;
395         break;
396       }
397       close_all_tables_for_name(thd, table->s, HA_EXTRA_NOT_USED, NULL);
398     }
399     /*
400       No other thread has the locked tables open; reopen them and get the
401       old locks. This should always succeed (unless some external process
402       has removed the tables)
403     */
404     if (thd->locked_tables_list.reopen_tables(thd, false))
405       result= true;
406 
407     /*
408       Since downgrade_lock() won't do anything with shared
409       metadata lock it is much simpler to go through all open tables rather
410       than picking only those tables that were flushed.
411     */
412     for (TABLE *tab= thd->open_tables; tab; tab= tab->next)
413       tab->mdl_ticket->downgrade_lock(MDL_SHARED_NO_READ_WRITE);
414 
415     DBUG_RETURN(result);
416   }
417   else if (tables)
418   {
419     /*
420       Get an explicit MDL lock for all requested tables to ensure they are
421       not used by any other thread
422     */
423     MDL_request_list mdl_requests;
424 
425     DBUG_PRINT("info", ("Waiting for other threads to close their open tables"));
426     DEBUG_SYNC(thd, "after_flush_unlock");
427 
428     /* close open HANDLER for this thread to allow table to be closed */
429     mysql_ha_flush_tables(thd, tables);
430 
431     for (TABLE_LIST *table= tables; table; table= table->next_local)
432     {
433       MDL_request *mdl_request= new (thd->mem_root) MDL_request;
434       if (mdl_request == NULL)
435         DBUG_RETURN(true);
436       MDL_REQUEST_INIT_BY_KEY(mdl_request, &table->mdl_request.key,
437                               MDL_EXCLUSIVE, MDL_STATEMENT);
438       mdl_requests.push_front(mdl_request);
439     }
440 
441     if (thd->mdl_context.acquire_locks(&mdl_requests, timeout))
442       DBUG_RETURN(true);
443 
444     for (TABLE_LIST *table= tables; table; table= table->next_local)
445       tdc_remove_table(thd, table->db.str, table->table_name.str);
446   }
447   DBUG_RETURN(false);
448 }
449 
450 
451 /**
452   Collect all shares that has open tables
453 */
454 
455 struct tc_collect_arg
456 {
457   DYNAMIC_ARRAY shares;
458   flush_tables_type flush_type;
459 };
460 
tc_collect_used_shares(TDC_element * element,tc_collect_arg * arg)461 static my_bool tc_collect_used_shares(TDC_element *element,
462                                       tc_collect_arg *arg)
463 {
464   my_bool result= FALSE;
465 
466   DYNAMIC_ARRAY *shares= &arg->shares;
467   mysql_mutex_lock(&element->LOCK_table_share);
468   if (element->ref_count > 0 && !element->share->is_view)
469   {
470     DBUG_ASSERT(element->share);
471     bool do_flush= 0;
472     switch (arg->flush_type) {
473     case FLUSH_ALL:
474       do_flush= 1;
475       break;
476     case FLUSH_NON_TRANS_TABLES:
477       if (!element->share->online_backup &&
478           element->share->table_category == TABLE_CATEGORY_USER)
479         do_flush= 1;
480       break;
481     case FLUSH_SYS_TABLES:
482       if (!element->share->online_backup &&
483           element->share->table_category != TABLE_CATEGORY_USER)
484         do_flush= 1;
485     }
486     if (do_flush)
487     {
488       element->ref_count++;                       // Protect against delete
489       if (push_dynamic(shares, (uchar*) &element->share))
490         result= TRUE;
491     }
492   }
493   mysql_mutex_unlock(&element->LOCK_table_share);
494   return result;
495 }
496 
497 
498 /*
499   Ignore errors from opening read only tables
500 */
501 
502 class flush_tables_error_handler : public Internal_error_handler
503 {
504 public:
505   int handled_errors;
506   int unhandled_errors;
flush_tables_error_handler()507   flush_tables_error_handler() : handled_errors(0), unhandled_errors(0)
508   {}
509 
handle_condition(THD * thd,uint sql_errno,const char * sqlstate,Sql_condition::enum_warning_level * level,const char * msg,Sql_condition ** cond_hdl)510   bool handle_condition(THD *thd,
511                         uint sql_errno,
512                         const char* sqlstate,
513                         Sql_condition::enum_warning_level *level,
514                         const char* msg,
515                         Sql_condition ** cond_hdl)
516   {
517     *cond_hdl= NULL;
518     if (sql_errno == ER_OPEN_AS_READONLY)
519     {
520       handled_errors++;
521       return TRUE;
522     }
523     if (*level == Sql_condition::WARN_LEVEL_ERROR)
524       unhandled_errors++;
525     return FALSE;
526   }
527 
got_fatal_error()528   bool got_fatal_error()
529   {
530     return unhandled_errors > 0;
531   }
532 };
533 
534 
535 /**
536    Flush cached table as part of global read lock
537 
538    @param thd
539    @param flag   What type of tables should be flushed
540 
541    @return 0  ok
542    @return 1  error
543 
544    After we get the list of table shares, we will call flush on all
545    possible tables, even if some flush fails.
546 */
547 
flush_tables(THD * thd,flush_tables_type flag)548 bool flush_tables(THD *thd, flush_tables_type flag)
549 {
550   bool result= TRUE;
551   tc_collect_arg collect_arg;
552   TABLE *tmp_table;
553   flush_tables_error_handler error_handler;
554   DBUG_ENTER("flush_tables");
555 
556   purge_tables();  /* Flush unused tables and shares */
557 
558   /*
559     Loop over all shares and collect shares that have open tables
560     TODO:
561     Optimize this to only collect shares that have been used for
562     write after last time all tables was closed.
563   */
564 
565   if (!(tmp_table= (TABLE*) my_malloc(PSI_INSTRUMENT_ME, sizeof(*tmp_table),
566                                       MYF(MY_WME | MY_THREAD_SPECIFIC))))
567     DBUG_RETURN(1);
568 
569   my_init_dynamic_array(PSI_INSTRUMENT_ME, &collect_arg.shares,
570                         sizeof(TABLE_SHARE*), 100, 100, MYF(0));
571   collect_arg.flush_type= flag;
572   if (tdc_iterate(thd, (my_hash_walk_action) tc_collect_used_shares,
573                   &collect_arg, true))
574   {
575     /* Release already collected shares */
576     for (uint i= 0 ; i < collect_arg.shares.elements ; i++)
577     {
578       TABLE_SHARE *share= *dynamic_element(&collect_arg.shares, i,
579                                            TABLE_SHARE**);
580       tdc_release_share(share);
581     }
582     goto err;
583   }
584 
585   /* Call HA_EXTRA_FLUSH on all found shares */
586 
587   thd->push_internal_handler(&error_handler);
588   for (uint i= 0 ; i < collect_arg.shares.elements ; i++)
589   {
590     TABLE_SHARE *share= *dynamic_element(&collect_arg.shares, i,
591                                          TABLE_SHARE**);
592     TABLE *table= tc_acquire_table(thd, share->tdc);
593     if (table)
594     {
595       (void) table->file->extra(HA_EXTRA_FLUSH);
596       tc_release_table(table);
597     }
598     else
599     {
600       /*
601         HA_OPEN_FOR_FLUSH is used to allow us to open the table even if
602         TABLE_SHARE::incompatible_version is set. It also will tell
603         SEQUENCE engine that we don't have to read the sequence information
604         (which may cause deadlocks with concurrently running ALTER TABLE or
605         ALTER SEQUENCE) as we will close the table at once.
606       */
607       if (!open_table_from_share(thd, share, &empty_clex_str,
608                                  HA_OPEN_KEYFILE, 0,
609                                  HA_OPEN_FOR_ALTER | HA_OPEN_FOR_FLUSH,
610                                  tmp_table, FALSE,
611                                  NULL))
612       {
613         (void) tmp_table->file->extra(HA_EXTRA_FLUSH);
614         /*
615           We don't put the table into the TDC as the table was not fully
616           opened (we didn't open triggers)
617         */
618         closefrm(tmp_table);
619       }
620     }
621     tdc_release_share(share);
622   }
623   thd->pop_internal_handler();
624   result= error_handler.got_fatal_error();
625   DBUG_PRINT("note", ("open_errors: %u %u",
626                       error_handler.handled_errors,
627                       error_handler.unhandled_errors));
628 err:
629   my_free(tmp_table);
630   delete_dynamic(&collect_arg.shares);
631   DBUG_RETURN(result);
632 }
633 
634 
635 /*
636   Mark all tables in the list which were used by current substatement
637   as free for reuse.
638 
639   SYNOPSIS
640     mark_used_tables_as_free_for_reuse()
641       thd   - thread context
642       table - head of the list of tables
643 
644   DESCRIPTION
645     Marks all tables in the list which were used by current substatement
646     (they are marked by its query_id) as free for reuse.
647 
648     Clear 'check_table_binlog_row_based_done' flag. For tables which were used
649     by current substatement the flag is cleared as part of 'ha_reset()' call.
650     For the rest of the open tables not used by current substament if this
651     flag is enabled as part of current substatement execution,
652     (for example when THD::binlog_write_table_maps() calls
653     prepare_for_row_logging()), clear the flag explicitly.
654 
655   NOTE
656     The reason we reset query_id is that it's not enough to just test
657     if table->query_id != thd->query_id to know if a table is in use.
658 
659     For example
660     SELECT f1_that_uses_t1() FROM t1;
661     In f1_that_uses_t1() we will see one instance of t1 where query_id is
662     set to query_id of original query.
663 */
664 
mark_used_tables_as_free_for_reuse(THD * thd,TABLE * table)665 static void mark_used_tables_as_free_for_reuse(THD *thd, TABLE *table)
666 {
667   DBUG_ENTER("mark_used_tables_as_free_for_reuse");
668   for (; table ; table= table->next)
669   {
670     DBUG_ASSERT(table->pos_in_locked_tables == NULL ||
671                 table->pos_in_locked_tables->table == table);
672     if (table->query_id == thd->query_id)
673     {
674       table->query_id= 0;
675       table->file->ha_reset();
676     }
677     else
678       table->file->clear_cached_table_binlog_row_based_flag();
679   }
680   DBUG_VOID_RETURN;
681 }
682 
683 
684 /**
685   Close all open instances of the table but keep the MDL lock.
686 
687   Works both under LOCK TABLES and in the normal mode.
688   Removes all closed instances of the table from the table cache.
689 
690   @param     thd     thread handle
691   @param[in] share   table share, but is just a handy way to
692                      access the table cache key
693 
694   @param[in] extra
695                      HA_EXTRA_PREPARE_FOR_DROP
696                         - The table is dropped
697                      HA_EXTRA_PREPARE_FOR_RENAME
698                         - The table is renamed
699                      HA_EXTRA_NOT_USED
700                         - The table is marked as closed in the
701                           locked_table_list but kept there so one can call
702                           locked_table_list->reopen_tables() to put it back.
703 
704                      In case of drop/rename the documented behavior is to
705                      implicitly remove the table from LOCK TABLES
706                      list.
707 
708   @pre Must be called with an X MDL lock on the table.
709 */
710 
711 void
close_all_tables_for_name(THD * thd,TABLE_SHARE * share,ha_extra_function extra,TABLE * skip_table)712 close_all_tables_for_name(THD *thd, TABLE_SHARE *share,
713                           ha_extra_function extra,
714                           TABLE *skip_table)
715 {
716   DBUG_ASSERT(!share->tmp_table);
717   DBUG_ASSERT(share->tdc->flushed);
718 
719   char key[MAX_DBKEY_LENGTH];
720   size_t key_length= share->table_cache_key.length;
721   bool remove_from_locked_tables= extra != HA_EXTRA_NOT_USED;
722 
723   memcpy(key, share->table_cache_key.str, key_length);
724 
725   for (TABLE **prev= &thd->open_tables; *prev; )
726   {
727     TABLE *table= *prev;
728 
729     if (table->s->table_cache_key.length == key_length &&
730         !memcmp(table->s->table_cache_key.str, key, key_length) &&
731         table != skip_table)
732     {
733       thd->locked_tables_list.unlink_from_list(thd,
734                                                table->pos_in_locked_tables,
735                                                remove_from_locked_tables);
736       /* Inform handler that there is a drop table or a rename going on */
737       if (extra != HA_EXTRA_NOT_USED && table->db_stat)
738       {
739         table->file->extra(extra);
740         extra= HA_EXTRA_NOT_USED;               // Call extra once!
741       }
742 
743       /*
744         Does nothing if the table is not locked.
745         This allows one to use this function after a table
746         has been unlocked, e.g. in partition management.
747       */
748       mysql_lock_remove(thd, thd->lock, table);
749       close_thread_table(thd, prev);
750     }
751     else
752     {
753       /* Step to next entry in open_tables list. */
754       prev= &table->next;
755     }
756   }
757 }
758 
759 
760 /*
761   Close all tables used by the current substatement, or all tables
762   used by this thread if we are on the upper level.
763 
764   SYNOPSIS
765     close_thread_tables()
766     thd			Thread handler
767 
768   IMPLEMENTATION
769     Unlocks tables and frees derived tables.
770     Put all normal tables used by thread in free list.
771 
772     It will only close/mark as free for reuse tables opened by this
773     substatement, it will also check if we are closing tables after
774     execution of complete query (i.e. we are on upper level) and will
775     leave prelocked mode if needed.
776 */
777 
close_thread_tables(THD * thd)778 int close_thread_tables(THD *thd)
779 {
780   TABLE *table;
781   int error= 0;
782   DBUG_ENTER("close_thread_tables");
783 
784   THD_STAGE_INFO(thd, stage_closing_tables);
785 
786 #ifdef EXTRA_DEBUG
787   DBUG_PRINT("tcache", ("open tables:"));
788   for (table= thd->open_tables; table; table= table->next)
789     DBUG_PRINT("tcache", ("table: '%s'.'%s' %p", table->s->db.str,
790                           table->s->table_name.str, table));
791 #endif
792 
793 #if defined(ENABLED_DEBUG_SYNC)
794   /* debug_sync may not be initialized for some slave threads */
795   if (thd->debug_sync_control)
796     DEBUG_SYNC(thd, "before_close_thread_tables");
797 #endif
798 
799   DBUG_ASSERT(thd->transaction->stmt.is_empty() || thd->in_sub_stmt ||
800               (thd->state_flags & Open_tables_state::BACKUPS_AVAIL));
801 
802   for (table= thd->open_tables; table; table= table->next)
803   {
804     /* Table might be in use by some outer statement. */
805     DBUG_PRINT("tcache", ("table: '%s'  query_id: %lu",
806                           table->s->table_name.str, (ulong) table->query_id));
807 
808     /* Detach MERGE children after every statement. Even under LOCK TABLES. */
809     if (thd->locked_tables_mode <= LTM_LOCK_TABLES ||
810         table->query_id == thd->query_id)
811     {
812       DBUG_ASSERT(table->file);
813       table->file->extra(HA_EXTRA_DETACH_CHILDREN);
814     }
815   }
816 
817   /*
818     We are assuming here that thd->derived_tables contains ONLY derived
819     tables for this substatement. i.e. instead of approach which uses
820     query_id matching for determining which of the derived tables belong
821     to this substatement we rely on the ability of substatements to
822     save/restore thd->derived_tables during their execution.
823 
824     TODO: Probably even better approach is to simply associate list of
825           derived tables with (sub-)statement instead of thread and destroy
826           them at the end of its execution.
827   */
828   if (thd->derived_tables)
829   {
830     TABLE *next;
831     /*
832       Close all derived tables generated in queries like
833       SELECT * FROM (SELECT * FROM t1)
834     */
835     for (table= thd->derived_tables ; table ; table= next)
836     {
837       next= table->next;
838       free_tmp_table(thd, table);
839     }
840     thd->derived_tables= 0;
841   }
842 
843   if (thd->rec_tables)
844   {
845     TABLE *next;
846     /*
847       Close all temporary tables created for recursive table references.
848       This action was postponed because the table could be used in the
849       statements like  ANALYZE WITH r AS (...) SELECT * from r
850       where r is defined through recursion.
851     */
852     for (table= thd->rec_tables ; table ; table= next)
853     {
854       next= table->next;
855       free_tmp_table(thd, table);
856     }
857     thd->rec_tables= 0;
858   }
859 
860   /*
861     Mark all temporary tables used by this statement as free for reuse.
862   */
863   thd->mark_tmp_tables_as_free_for_reuse();
864 
865   if (thd->locked_tables_mode)
866   {
867 
868     /* Ensure we are calling ha_reset() for all used tables */
869     mark_used_tables_as_free_for_reuse(thd, thd->open_tables);
870 
871     /*
872       We are under simple LOCK TABLES or we're inside a sub-statement
873       of a prelocked statement, so should not do anything else.
874 
875       Note that even if we are in LTM_LOCK_TABLES mode and statement
876       requires prelocking (e.g. when we are closing tables after
877       failing ot "open" all tables required for statement execution)
878       we will exit this function a few lines below.
879     */
880     if (! thd->lex->requires_prelocking())
881       DBUG_RETURN(0);
882 
883     /*
884       We are in the top-level statement of a prelocked statement,
885       so we have to leave the prelocked mode now with doing implicit
886       UNLOCK TABLES if needed.
887     */
888     if (thd->locked_tables_mode == LTM_PRELOCKED_UNDER_LOCK_TABLES)
889       thd->locked_tables_mode= LTM_LOCK_TABLES;
890 
891     if (thd->locked_tables_mode == LTM_LOCK_TABLES)
892       DBUG_RETURN(0);
893 
894     thd->leave_locked_tables_mode();
895 
896     /* Fallthrough */
897   }
898 
899   if (thd->lock)
900   {
901     /*
902       For RBR we flush the pending event just before we unlock all the
903       tables.  This means that we are at the end of a topmost
904       statement, so we ensure that the STMT_END_F flag is set on the
905       pending event.  For statements that are *inside* stored
906       functions, the pending event will not be flushed: that will be
907       handled either before writing a query log event (inside
908       binlog_query()) or when preparing a pending event.
909      */
910     (void)thd->binlog_flush_pending_rows_event(TRUE);
911     error= mysql_unlock_tables(thd, thd->lock);
912     thd->lock=0;
913   }
914   /*
915     Closing a MERGE child before the parent would be fatal if the
916     other thread tries to abort the MERGE lock in between.
917   */
918   while (thd->open_tables)
919     (void) close_thread_table(thd, &thd->open_tables);
920 
921   DBUG_RETURN(error);
922 }
923 
924 
925 /* move one table to free list */
926 
close_thread_table(THD * thd,TABLE ** table_ptr)927 void close_thread_table(THD *thd, TABLE **table_ptr)
928 {
929   TABLE *table= *table_ptr;
930   DBUG_ENTER("close_thread_table");
931   DBUG_PRINT("tcache", ("table: '%s'.'%s' %p", table->s->db.str,
932                         table->s->table_name.str, table));
933   DBUG_ASSERT(!table->file->keyread_enabled());
934   DBUG_ASSERT(!table->file || table->file->inited == handler::NONE);
935 
936   /*
937     The metadata lock must be released after giving back
938     the table to the table cache.
939   */
940   DBUG_ASSERT(thd->mdl_context.is_lock_owner(MDL_key::TABLE,
941                                              table->s->db.str,
942                                              table->s->table_name.str,
943                                              MDL_SHARED));
944   table->mdl_ticket= NULL;
945 
946   if (table->file)
947   {
948     table->file->update_global_table_stats();
949     table->file->update_global_index_stats();
950   }
951 
952   /*
953     This look is needed to allow THD::notify_shared_lock() to
954     traverse the thd->open_tables list without having to worry that
955     some of the tables are removed from under it
956   */
957 
958   mysql_mutex_lock(&thd->LOCK_thd_data);
959   *table_ptr=table->next;
960   mysql_mutex_unlock(&thd->LOCK_thd_data);
961 
962   if (! table->needs_reopen())
963   {
964     /* Avoid having MERGE tables with attached children in table cache. */
965     table->file->extra(HA_EXTRA_DETACH_CHILDREN);
966     /* Free memory and reset for next loop. */
967     free_field_buffers_larger_than(table, MAX_TDC_BLOB_SIZE);
968     table->file->ha_reset();
969   }
970 
971   /*
972     Do this *before* entering the TABLE_SHARE::tdc.LOCK_table_share
973     critical section.
974   */
975   MYSQL_UNBIND_TABLE(table->file);
976 
977   tc_release_table(table);
978   DBUG_VOID_RETURN;
979 }
980 
981 
982 /*
983   Find table in list.
984 
985   SYNOPSIS
986     find_table_in_list()
987     table		Pointer to table list
988     offset		Offset to which list in table structure to use
989     db_name		Data base name
990     table_name		Table name
991 
992   NOTES:
993     This is called by find_table_in_global_list().
994 
995   RETURN VALUES
996     NULL	Table not found
997     #		Pointer to found table.
998 */
999 
find_table_in_list(TABLE_LIST * table,TABLE_LIST * TABLE_LIST::* link,const LEX_CSTRING * db_name,const LEX_CSTRING * table_name)1000 TABLE_LIST *find_table_in_list(TABLE_LIST *table,
1001                                TABLE_LIST *TABLE_LIST::*link,
1002                                const LEX_CSTRING *db_name,
1003                                const LEX_CSTRING *table_name)
1004 {
1005   for (; table; table= table->*link )
1006   {
1007     if (cmp(&table->db, db_name) == 0 &&
1008         cmp(&table->table_name, table_name) == 0)
1009       break;
1010   }
1011   return table;
1012 }
1013 
1014 
1015 /**
1016   Test that table is unique (It's only exists once in the table list)
1017 
1018   @param  thd                   thread handle
1019   @param  table                 table which should be checked
1020   @param  table_list            list of tables
1021   @param  check_flag            whether to check tables' aliases
1022                                 Currently this is only used by INSERT
1023 
1024   NOTE: to exclude derived tables from check we use following mechanism:
1025     a) during derived table processing set THD::derived_tables_processing
1026     b) JOIN::prepare set SELECT::exclude_from_table_unique_test if
1027        THD::derived_tables_processing set. (we can't use JOIN::execute
1028        because for PS we perform only JOIN::prepare, but we can't set this
1029        flag in JOIN::prepare if we are not sure that we are in derived table
1030        processing loop, because multi-update call fix_fields() for some its
1031        items (which mean JOIN::prepare for subqueries) before unique_table
1032        call to detect which tables should be locked for write).
1033     c) find_dup_table skip all tables which belong to SELECT with
1034        SELECT::exclude_from_table_unique_test set.
1035     Also SELECT::exclude_from_table_unique_test used to exclude from check
1036     tables of main SELECT of multi-delete and multi-update
1037 
1038     We also skip tables with TABLE_LIST::prelocking_placeholder set,
1039     because we want to allow SELECTs from them, and their modification
1040     will rise the error anyway.
1041 
1042     TODO: when we will have table/view change detection we can do this check
1043           only once for PS/SP
1044 
1045   @retval !=0  found duplicate
1046   @retval 0 if table is unique
1047 */
1048 
1049 static
find_dup_table(THD * thd,TABLE_LIST * table,TABLE_LIST * table_list,uint check_flag)1050 TABLE_LIST* find_dup_table(THD *thd, TABLE_LIST *table, TABLE_LIST *table_list,
1051                            uint check_flag)
1052 {
1053   TABLE_LIST *res= 0;
1054   LEX_CSTRING *d_name, *t_name, *t_alias;
1055   DBUG_ENTER("find_dup_table");
1056   DBUG_PRINT("enter", ("table alias: %s", table->alias.str));
1057 
1058   /*
1059     If this function called for query which update table (INSERT/UPDATE/...)
1060     then we have in table->table pointer to TABLE object which we are
1061     updating even if it is VIEW so we need TABLE_LIST of this TABLE object
1062     to get right names (even if lower_case_table_names used).
1063 
1064     If this function called for CREATE command that we have not opened table
1065     (table->table equal to 0) and right names is in current TABLE_LIST
1066     object.
1067   */
1068   if (table->table)
1069   {
1070     /* All MyISAMMRG children are plain MyISAM tables. */
1071     DBUG_ASSERT(table->table->file->ht->db_type != DB_TYPE_MRG_MYISAM);
1072 
1073     table= table->find_underlying_table(table->table);
1074     /*
1075       as far as we have table->table we have to find real TABLE_LIST of
1076       it in underlying tables
1077     */
1078     DBUG_ASSERT(table);
1079   }
1080   d_name= &table->db;
1081   t_name= &table->table_name;
1082   t_alias= &table->alias;
1083 
1084 retry:
1085   DBUG_PRINT("info", ("real table: %s.%s", d_name->str, t_name->str));
1086   for (TABLE_LIST *tl= table_list; tl ; tl= tl->next_global, res= 0)
1087   {
1088     if (tl->select_lex && tl->select_lex->master_unit() &&
1089         tl->select_lex->master_unit()->executed)
1090     {
1091       /*
1092         There is no sense to check tables of already executed parts
1093         of the query
1094       */
1095       continue;
1096     }
1097     /*
1098       Table is unique if it is present only once in the global list
1099       of tables and once in the list of table locks.
1100     */
1101     if (! (res= find_table_in_global_list(tl, d_name, t_name)))
1102       break;
1103     tl= res;                       // We can continue search after this table
1104 
1105     /* Skip if same underlying table. */
1106     if (res->table && (res->table == table->table))
1107       continue;
1108 
1109     /* Skip if table is tmp table */
1110     if (check_flag & CHECK_DUP_SKIP_TEMP_TABLE &&
1111         res->table && res->table->s->tmp_table != NO_TMP_TABLE)
1112     {
1113       continue;
1114     }
1115     if (check_flag & CHECK_DUP_FOR_CREATE)
1116       DBUG_RETURN(res);
1117 
1118     /* Skip if table alias does not match. */
1119     if (check_flag & CHECK_DUP_ALLOW_DIFFERENT_ALIAS)
1120     {
1121       if (my_strcasecmp(table_alias_charset, t_alias->str, res->alias.str))
1122         continue;
1123     }
1124 
1125     /*
1126       If table is not excluded (could be a derived table) and table is not
1127       a prelocking placeholder then we found either a duplicate entry
1128       or a table that is part of a derived table (handled below).
1129       Examples are:
1130       INSERT INTO t1 SELECT * FROM t1;
1131       INSERT INTO t1 SELECT * FROM view_containing_t1;
1132     */
1133     if (res->select_lex &&
1134         !res->select_lex->exclude_from_table_unique_test &&
1135         !res->prelocking_placeholder)
1136       break;
1137 
1138     /*
1139       If we found entry of this table or table of SELECT which already
1140       processed in derived table or top select of multi-update/multi-delete
1141       (exclude_from_table_unique_test) or prelocking placeholder.
1142     */
1143     DBUG_PRINT("info",
1144                ("found same copy of table or table which we should skip"));
1145   }
1146   if (res && res->belong_to_derived)
1147   {
1148     /*
1149       We come here for queries of type:
1150       INSERT INTO t1 (SELECT tmp.a FROM (select * FROM t1) as tmp);
1151 
1152       Try to fix by materializing the derived table
1153     */
1154     TABLE_LIST *derived=  res->belong_to_derived;
1155     if (derived->is_merged_derived() && !derived->derived->is_excluded())
1156     {
1157       DBUG_PRINT("info",
1158                  ("convert merged to materialization to resolve the conflict"));
1159       derived->change_refs_to_fields();
1160       derived->set_materialized_derived();
1161       goto retry;
1162     }
1163   }
1164   DBUG_RETURN(res);
1165 }
1166 
1167 
1168 /**
1169   Test that the subject table of INSERT/UPDATE/DELETE/CREATE
1170   or (in case of MyISAMMRG) one of its children are not used later
1171   in the query.
1172 
1173   For MyISAMMRG tables, it is assumed that all the underlying
1174   tables of @c table (if any) are listed right after it and that
1175   their @c parent_l field points at the main table.
1176 
1177 
1178   @retval non-NULL The table list element for the table that
1179                    represents the duplicate.
1180   @retval NULL     No duplicates found.
1181 */
1182 
1183 TABLE_LIST*
unique_table(THD * thd,TABLE_LIST * table,TABLE_LIST * table_list,uint check_flag)1184 unique_table(THD *thd, TABLE_LIST *table, TABLE_LIST *table_list,
1185              uint check_flag)
1186 {
1187   TABLE_LIST *dup;
1188 
1189   table= table->find_table_for_update();
1190 
1191   if (table->table &&
1192       table->table->file->ha_table_flags() & HA_CAN_MULTISTEP_MERGE)
1193   {
1194     TABLE_LIST *child;
1195     dup= NULL;
1196     /* Check duplicates of all merge children. */
1197     for (child= table->next_global; child;
1198          child= child->next_global)
1199     {
1200       if (child->table &&
1201           child->table->file->ha_table_flags() & HA_CAN_MULTISTEP_MERGE)
1202         continue;
1203 
1204       /*
1205         Ensure that the child has one parent that is the table that is
1206         updated.
1207       */
1208       TABLE_LIST *tmp_parent= child;
1209       while ((tmp_parent= tmp_parent->parent_l))
1210       {
1211         if (tmp_parent == table)
1212           break;
1213       }
1214       if (!tmp_parent)
1215         break;
1216 
1217       if ((dup= find_dup_table(thd, child, child->next_global, check_flag)))
1218         break;
1219     }
1220   }
1221   else
1222     dup= find_dup_table(thd, table, table_list, check_flag);
1223   return dup;
1224 }
1225 
1226 
1227 /*
1228   Issue correct error message in case we found 2 duplicate tables which
1229   prevent some update operation
1230 
1231   SYNOPSIS
1232     update_non_unique_table_error()
1233     update      table which we try to update
1234     operation   name of update operation
1235     duplicate   duplicate table which we found
1236 
1237   NOTE:
1238     here we hide view underlying tables if we have them
1239 */
1240 
update_non_unique_table_error(TABLE_LIST * update,const char * operation,TABLE_LIST * duplicate)1241 void update_non_unique_table_error(TABLE_LIST *update,
1242                                    const char *operation,
1243                                    TABLE_LIST *duplicate)
1244 {
1245   update= update->top_table();
1246   duplicate= duplicate->top_table();
1247   if (!update->view || !duplicate->view ||
1248       update->view == duplicate->view ||
1249       update->view_name.length != duplicate->view_name.length ||
1250       update->view_db.length != duplicate->view_db.length ||
1251       lex_string_cmp(table_alias_charset,
1252                      &update->view_name, &duplicate->view_name) != 0 ||
1253       lex_string_cmp(table_alias_charset,
1254                      &update->view_db, &duplicate->view_db) != 0)
1255   {
1256     /*
1257       it is not the same view repeated (but it can be parts of the same copy
1258       of view), so we have to hide underlying tables.
1259     */
1260     if (update->view)
1261     {
1262       /* Issue the ER_NON_INSERTABLE_TABLE error for an INSERT */
1263       if (update->view == duplicate->view)
1264         my_error(!strncmp(operation, "INSERT", 6) ?
1265                  ER_NON_INSERTABLE_TABLE : ER_NON_UPDATABLE_TABLE, MYF(0),
1266                  update->alias.str, operation);
1267       else
1268         my_error(ER_VIEW_PREVENT_UPDATE, MYF(0),
1269                  (duplicate->view ? duplicate->alias.str : update->alias.str),
1270                  operation, update->alias.str);
1271       return;
1272     }
1273     if (duplicate->view)
1274     {
1275       my_error(ER_VIEW_PREVENT_UPDATE, MYF(0), duplicate->alias.str, operation,
1276                update->alias.str);
1277       return;
1278     }
1279   }
1280   my_error(ER_UPDATE_TABLE_USED, MYF(0), update->alias.str, operation);
1281 }
1282 
1283 
1284 /**
1285    Force all other threads to stop using the table by upgrading
1286    metadata lock on it and remove unused TABLE instances from cache.
1287 
1288    @param thd      Thread handler
1289    @param table    Table to remove from cache
1290    @param function HA_EXTRA_PREPARE_FOR_DROP if table is to be deleted
1291                    HA_EXTRA_FORCE_REOPEN if table is not be used
1292                    HA_EXTRA_PREPARE_FOR_RENAME if table is to be renamed
1293                    HA_EXTRA_NOT_USED             Don't call extra()
1294 
1295    @note When returning, the table will be unusable for other threads
1296          until metadata lock is downgraded.
1297 
1298    @retval FALSE Success.
1299    @retval TRUE  Failure (e.g. because thread was killed).
1300 */
1301 
wait_while_table_is_used(THD * thd,TABLE * table,enum ha_extra_function function)1302 bool wait_while_table_is_used(THD *thd, TABLE *table,
1303                               enum ha_extra_function function)
1304 {
1305   DBUG_ENTER("wait_while_table_is_used");
1306   DBUG_ASSERT(!table->s->tmp_table);
1307   DBUG_PRINT("enter", ("table: '%s'  share: %p  db_stat: %u",
1308                        table->s->table_name.str, table->s,
1309                        table->db_stat));
1310 
1311   if (thd->mdl_context.upgrade_shared_lock(
1312              table->mdl_ticket, MDL_EXCLUSIVE,
1313              thd->variables.lock_wait_timeout))
1314     DBUG_RETURN(TRUE);
1315 
1316   table->s->tdc->flush(thd, true);
1317   /* extra() call must come only after all instances above are closed */
1318   if (function != HA_EXTRA_NOT_USED)
1319   {
1320     int error= table->file->extra(function);
1321     if (error)
1322       table->file->print_error(error, MYF(0));
1323     DBUG_RETURN(error);
1324   }
1325   DBUG_RETURN(FALSE);
1326 }
1327 
1328 
1329 /**
1330   Close a and drop a just created table in CREATE TABLE ... SELECT.
1331 
1332   @param  thd         Thread handle
1333   @param  table       TABLE object for the table to be dropped
1334   @param  db_name     Name of database for this table
1335   @param  table_name  Name of this table
1336 
1337   This routine assumes that the table to be closed is open only
1338   by the calling thread, so we needn't wait until other threads
1339   close the table. It also assumes that the table is first
1340   in thd->open_ables and a data lock on it, if any, has been
1341   released. To sum up, it's tuned to work with
1342   CREATE TABLE ... SELECT and CREATE TABLE .. SELECT only.
1343   Note, that currently CREATE TABLE ... SELECT is not supported
1344   under LOCK TABLES. This function, still, can be called in
1345   prelocked mode, e.g. if we do CREATE TABLE .. SELECT f1();
1346 */
1347 
drop_open_table(THD * thd,TABLE * table,const LEX_CSTRING * db_name,const LEX_CSTRING * table_name)1348 void drop_open_table(THD *thd, TABLE *table, const LEX_CSTRING *db_name,
1349                      const LEX_CSTRING *table_name)
1350 {
1351   DBUG_ENTER("drop_open_table");
1352   if (table->s->tmp_table)
1353     thd->drop_temporary_table(table, NULL, true);
1354   else
1355   {
1356     DBUG_ASSERT(table == thd->open_tables);
1357 
1358     handlerton *table_type= table->s->db_type();
1359     table->file->extra(HA_EXTRA_PREPARE_FOR_DROP);
1360     table->s->tdc->flush(thd, true);
1361     close_thread_table(thd, &thd->open_tables);
1362     /* Remove the table from the storage engine and rm the .frm. */
1363     quick_rm_table(thd, table_type, db_name, table_name, 0);
1364  }
1365   DBUG_VOID_RETURN;
1366 }
1367 
1368 
1369 /**
1370   An error handler which converts, if possible, ER_LOCK_DEADLOCK error
1371   that can occur when we are trying to acquire a metadata lock to
1372   a request for back-off and re-start of open_tables() process.
1373 */
1374 
1375 class MDL_deadlock_handler : public Internal_error_handler
1376 {
1377 public:
MDL_deadlock_handler(Open_table_context * ot_ctx_arg)1378   MDL_deadlock_handler(Open_table_context *ot_ctx_arg)
1379     : m_ot_ctx(ot_ctx_arg), m_is_active(FALSE)
1380   {}
1381 
~MDL_deadlock_handler()1382   virtual ~MDL_deadlock_handler() {}
1383 
1384   virtual bool handle_condition(THD *thd,
1385                                 uint sql_errno,
1386                                 const char* sqlstate,
1387                                 Sql_condition::enum_warning_level *level,
1388                                 const char* msg,
1389                                 Sql_condition ** cond_hdl);
1390 
1391 private:
1392   /** Open table context to be used for back-off request. */
1393   Open_table_context *m_ot_ctx;
1394   /**
1395     Indicates that we are already in the process of handling
1396     ER_LOCK_DEADLOCK error. Allows to re-emit the error from
1397     the error handler without falling into infinite recursion.
1398   */
1399   bool m_is_active;
1400 };
1401 
1402 
handle_condition(THD *,uint sql_errno,const char *,Sql_condition::enum_warning_level *,const char *,Sql_condition ** cond_hdl)1403 bool MDL_deadlock_handler::handle_condition(THD *,
1404                                             uint sql_errno,
1405                                             const char*,
1406                                             Sql_condition::enum_warning_level*,
1407                                             const char*,
1408                                             Sql_condition ** cond_hdl)
1409 {
1410   *cond_hdl= NULL;
1411   if (! m_is_active && sql_errno == ER_LOCK_DEADLOCK)
1412   {
1413     /* Disable the handler to avoid infinite recursion. */
1414     m_is_active= TRUE;
1415     (void) m_ot_ctx->request_backoff_action(
1416              Open_table_context::OT_BACKOFF_AND_RETRY,
1417              NULL);
1418     m_is_active= FALSE;
1419     /*
1420       If the above back-off request failed, a new instance of
1421       ER_LOCK_DEADLOCK error was emitted. Thus the current
1422       instance of error condition can be treated as handled.
1423     */
1424     return TRUE;
1425   }
1426   return FALSE;
1427 }
1428 
1429 
1430 /**
1431   Try to acquire an MDL lock for a table being opened.
1432 
1433   @param[in,out] thd      Session context, to report errors.
1434   @param[out]    ot_ctx   Open table context, to hold the back off
1435                           state. If we failed to acquire a lock
1436                           due to a lock conflict, we add the
1437                           failed request to the open table context.
1438   @param[in,out] mdl_request A request for an MDL lock.
1439                           If we managed to acquire a ticket
1440                           (no errors or lock conflicts occurred),
1441                           contains a reference to it on
1442                           return. However, is not modified if MDL
1443                           lock type- modifying flags were provided.
1444   @param[in]    flags flags MYSQL_OPEN_FORCE_SHARED_MDL,
1445                           MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL or
1446                           MYSQL_OPEN_FAIL_ON_MDL_CONFLICT
1447                           @sa open_table().
1448   @param[out]   mdl_ticket Only modified if there was no error.
1449                           If we managed to acquire an MDL
1450                           lock, contains a reference to the
1451                           ticket, otherwise is set to NULL.
1452 
1453   @retval TRUE  An error occurred.
1454   @retval FALSE No error, but perhaps a lock conflict, check mdl_ticket.
1455 */
1456 
1457 static bool
open_table_get_mdl_lock(THD * thd,Open_table_context * ot_ctx,MDL_request * mdl_request,uint flags,MDL_ticket ** mdl_ticket)1458 open_table_get_mdl_lock(THD *thd, Open_table_context *ot_ctx,
1459                         MDL_request *mdl_request,
1460                         uint flags,
1461                         MDL_ticket **mdl_ticket)
1462 {
1463   MDL_request mdl_request_shared;
1464 
1465   if (flags & (MYSQL_OPEN_FORCE_SHARED_MDL |
1466                MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL))
1467   {
1468     /*
1469       MYSQL_OPEN_FORCE_SHARED_MDL flag means that we are executing
1470       PREPARE for a prepared statement and want to override
1471       the type-of-operation aware metadata lock which was set
1472       in the parser/during view opening with a simple shared
1473       metadata lock.
1474       This is necessary to allow concurrent execution of PREPARE
1475       and LOCK TABLES WRITE statement against the same table.
1476 
1477       MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL flag means that we open
1478       the table in order to get information about it for one of I_S
1479       queries and also want to override the type-of-operation aware
1480       shared metadata lock which was set earlier (e.g. during view
1481       opening) with a high-priority shared metadata lock.
1482       This is necessary to avoid unnecessary waiting and extra
1483       ER_WARN_I_S_SKIPPED_TABLE warnings when accessing I_S tables.
1484 
1485       These two flags are mutually exclusive.
1486     */
1487     DBUG_ASSERT(!(flags & MYSQL_OPEN_FORCE_SHARED_MDL) ||
1488                 !(flags & MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL));
1489 
1490     MDL_REQUEST_INIT_BY_KEY(&mdl_request_shared, &mdl_request->key,
1491         flags & MYSQL_OPEN_FORCE_SHARED_MDL ? MDL_SHARED : MDL_SHARED_HIGH_PRIO,
1492         MDL_TRANSACTION);
1493     mdl_request= &mdl_request_shared;
1494   }
1495 
1496   if (flags & MYSQL_OPEN_FAIL_ON_MDL_CONFLICT)
1497   {
1498     /*
1499       When table is being open in order to get data for I_S table,
1500       we might have some tables not only open but also locked (e.g. when
1501       this happens under LOCK TABLES or in a stored function).
1502       As a result by waiting on a conflicting metadata lock to go away
1503       we may create a deadlock which won't entirely belong to the
1504       MDL subsystem and thus won't be detectable by this subsystem's
1505       deadlock detector.
1506       To avoid such situation we skip the trouble-making table if
1507       there is a conflicting lock.
1508     */
1509     if (thd->mdl_context.try_acquire_lock(mdl_request))
1510       return TRUE;
1511     if (mdl_request->ticket == NULL)
1512     {
1513       my_error(ER_WARN_I_S_SKIPPED_TABLE, MYF(0),
1514                mdl_request->key.db_name(), mdl_request->key.name());
1515       return TRUE;
1516     }
1517   }
1518   else
1519   {
1520     /*
1521       We are doing a normal table open. Let us try to acquire a metadata
1522       lock on the table. If there is a conflicting lock, acquire_lock()
1523       will wait for it to go away. Sometimes this waiting may lead to a
1524       deadlock, with the following results:
1525       1) If a deadlock is entirely within MDL subsystem, it is
1526          detected by the deadlock detector of this subsystem.
1527          ER_LOCK_DEADLOCK error is produced. Then, the error handler
1528          that is installed prior to the call to acquire_lock() attempts
1529          to request a back-off and retry. Upon success, ER_LOCK_DEADLOCK
1530          error is suppressed, otherwise propagated up the calling stack.
1531       2) Otherwise, a deadlock may occur when the wait-for graph
1532          includes edges not visible to the MDL deadlock detector.
1533          One such example is a wait on an InnoDB row lock, e.g. when:
1534          conn C1 gets SR MDL lock on t1 with SELECT * FROM t1
1535          conn C2 gets a row lock on t2 with  SELECT * FROM t2 FOR UPDATE
1536          conn C3 gets in and waits on C1 with DROP TABLE t0, t1
1537          conn C2 continues and blocks on C3 with SELECT * FROM t0
1538          conn C1 deadlocks by waiting on C2 by issuing SELECT * FROM
1539          t2 LOCK IN SHARE MODE.
1540          Such circular waits are currently only resolved by timeouts,
1541          e.g. @@innodb_lock_wait_timeout or @@lock_wait_timeout.
1542     */
1543     MDL_deadlock_handler mdl_deadlock_handler(ot_ctx);
1544 
1545     thd->push_internal_handler(&mdl_deadlock_handler);
1546     bool result= thd->mdl_context.acquire_lock(mdl_request,
1547                                                ot_ctx->get_timeout());
1548     thd->pop_internal_handler();
1549 
1550     if (result && !ot_ctx->can_recover_from_failed_open())
1551       return TRUE;
1552   }
1553   *mdl_ticket= mdl_request->ticket;
1554   return FALSE;
1555 }
1556 
1557 #ifdef WITH_PARTITION_STORAGE_ENGINE
1558 /* Set all [named] partitions as used. */
set_partitions_as_used(TABLE_LIST * tl,TABLE * t)1559 static int set_partitions_as_used(TABLE_LIST *tl, TABLE *t)
1560 {
1561   if (t->part_info)
1562     return t->file->change_partitions_to_open(tl->partition_names);
1563   return 0;
1564 }
1565 #endif
1566 
1567 
1568 /**
1569   Check if the given table is actually a VIEW that was LOCK-ed
1570 
1571   @param thd            Thread context.
1572   @param t              Table to check.
1573 
1574   @retval TRUE  The 't'-table is a locked view
1575                 needed to remedy problem before retrying again.
1576   @retval FALSE 't' was not locked, not a VIEW or an error happened.
1577 */
1578 
is_locked_view(THD * thd,TABLE_LIST * t)1579 bool is_locked_view(THD *thd, TABLE_LIST *t)
1580 {
1581   DBUG_ENTER("is_locked_view");
1582   /*
1583    Is this table a view and not a base table?
1584    (it is work around to allow to open view with locked tables,
1585    real fix will be made after definition cache will be made)
1586 
1587    Since opening of view which was not explicitly locked by LOCK
1588    TABLES breaks metadata locking protocol (potentially can lead
1589    to deadlocks) it should be disallowed.
1590   */
1591   if (thd->mdl_context.is_lock_owner(MDL_key::TABLE, t->db.str,
1592                                      t->table_name.str, MDL_SHARED))
1593   {
1594     char path[FN_REFLEN + 1];
1595     build_table_filename(path, sizeof(path) - 1,
1596                          t->db.str, t->table_name.str, reg_ext, 0);
1597     /*
1598       Note that we can't be 100% sure that it is a view since it's
1599       possible that we either simply have not found unused TABLE
1600       instance in THD::open_tables list or were unable to open table
1601       during prelocking process (in this case in theory we still
1602       should hold shared metadata lock on it).
1603     */
1604     if (dd_frm_is_view(thd, path))
1605     {
1606       /*
1607         If parent_l of the table_list is non null then a merge table
1608         has this view as child table, which is not supported.
1609       */
1610       if (t->parent_l)
1611       {
1612         my_error(ER_WRONG_MRG_TABLE, MYF(0));
1613         DBUG_RETURN(FALSE);
1614       }
1615 
1616       if (!tdc_open_view(thd, t, CHECK_METADATA_VERSION))
1617       {
1618         DBUG_ASSERT(t->view != 0);
1619         DBUG_RETURN(TRUE); // VIEW
1620       }
1621     }
1622   }
1623 
1624   DBUG_RETURN(FALSE);
1625 }
1626 
1627 
1628 /**
1629   Open a base table.
1630 
1631   @param thd            Thread context.
1632   @param table_list     Open first table in list.
1633   @param ot_ctx         Context with flags which modify how open works
1634                         and which is used to recover from a failed
1635                         open_table() attempt.
1636                         Some examples of flags:
1637                         MYSQL_OPEN_IGNORE_FLUSH - Open table even if
1638                         someone has done a flush. No version number
1639                         checking is done.
1640                         MYSQL_OPEN_HAS_MDL_LOCK - instead of acquiring
1641                         metadata locks rely on that caller already has
1642                         appropriate ones.
1643 
1644   Uses a cache of open tables to find a TABLE instance not in use.
1645 
1646   If TABLE_LIST::open_strategy is set to OPEN_IF_EXISTS, the table is
1647   opened only if it exists. If the open strategy is OPEN_STUB, the
1648   underlying table is never opened. In both cases, metadata locks are
1649   always taken according to the lock strategy.
1650 
1651   The function used to open temporary tables, but now it opens base tables
1652   only.
1653 
1654   @retval TRUE  Open failed. "action" parameter may contain type of action
1655                 needed to remedy problem before retrying again.
1656   @retval FALSE Success. Members of TABLE_LIST structure are filled properly
1657                 (e.g.  TABLE_LIST::table is set for real tables and
1658                 TABLE_LIST::view is set for views).
1659 */
1660 
open_table(THD * thd,TABLE_LIST * table_list,Open_table_context * ot_ctx)1661 bool open_table(THD *thd, TABLE_LIST *table_list, Open_table_context *ot_ctx)
1662 {
1663   TABLE *table;
1664   const char *key;
1665   uint	key_length;
1666   const char *alias= table_list->alias.str;
1667   uint flags= ot_ctx->get_flags();
1668   MDL_ticket *mdl_ticket;
1669   TABLE_SHARE *share;
1670   uint gts_flags;
1671 #ifdef WITH_PARTITION_STORAGE_ENGINE
1672   int part_names_error=0;
1673 #endif
1674   DBUG_ENTER("open_table");
1675 
1676   /*
1677     The table must not be opened already. The table can be pre-opened for
1678     some statements if it is a temporary table.
1679 
1680     open_temporary_table() must be used to open temporary tables.
1681   */
1682   DBUG_ASSERT(!table_list->table);
1683 
1684   /* an open table operation needs a lot of the stack space */
1685   if (check_stack_overrun(thd, STACK_MIN_SIZE_FOR_OPEN, (uchar *)&alias))
1686     DBUG_RETURN(TRUE);
1687 
1688   if (!(flags & MYSQL_OPEN_IGNORE_KILLED) && thd->killed)
1689   {
1690     thd->send_kill_message();
1691     DBUG_RETURN(TRUE);
1692   }
1693 
1694   /*
1695     Check if we're trying to take a write lock in a read only transaction.
1696 
1697     Note that we allow write locks on log tables as otherwise logging
1698     to general/slow log would be disabled in read only transactions.
1699   */
1700   if (table_list->mdl_request.is_write_lock_request() &&
1701       thd->tx_read_only &&
1702       !(flags & (MYSQL_LOCK_LOG_TABLE | MYSQL_OPEN_HAS_MDL_LOCK)))
1703   {
1704     my_error(ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION, MYF(0));
1705     DBUG_RETURN(true);
1706   }
1707 
1708   if (!table_list->db.str)
1709   {
1710     my_error(ER_NO_DB_ERROR, MYF(0));
1711     DBUG_RETURN(true);
1712   }
1713 
1714   key_length= get_table_def_key(table_list, &key);
1715 
1716   /*
1717     If we're in pre-locked or LOCK TABLES mode, let's try to find the
1718     requested table in the list of pre-opened and locked tables. If the
1719     table is not there, return an error - we can't open not pre-opened
1720     tables in pre-locked/LOCK TABLES mode.
1721     TODO: move this block into a separate function.
1722   */
1723   if (thd->locked_tables_mode &&
1724       ! (flags & MYSQL_OPEN_GET_NEW_TABLE))
1725   {						// Using table locks
1726     TABLE *best_table= 0;
1727     int best_distance= INT_MIN;
1728     for (table=thd->open_tables; table ; table=table->next)
1729     {
1730       if (table->s->table_cache_key.length == key_length &&
1731 	  !memcmp(table->s->table_cache_key.str, key, key_length))
1732       {
1733         if (!my_strcasecmp(system_charset_info, table->alias.c_ptr(), alias) &&
1734             table->query_id != thd->query_id && /* skip tables already used */
1735             (thd->locked_tables_mode == LTM_LOCK_TABLES ||
1736              table->query_id == 0))
1737         {
1738           int distance= ((int) table->reginfo.lock_type -
1739                          (int) table_list->lock_type);
1740 
1741           /*
1742             Find a table that either has the exact lock type requested,
1743             or has the best suitable lock. In case there is no locked
1744             table that has an equal or higher lock than requested,
1745             we us the closest matching lock to be able to produce an error
1746             message about wrong lock mode on the table. The best_table
1747             is changed if bd < 0 <= d or bd < d < 0 or 0 <= d < bd.
1748 
1749             distance <  0 - No suitable lock found
1750             distance >  0 - we have lock mode higher then we require
1751             distance == 0 - we have lock mode exactly which we need
1752           */
1753           if ((best_distance < 0 && distance > best_distance) ||
1754               (distance >= 0 && distance < best_distance))
1755           {
1756             best_distance= distance;
1757             best_table= table;
1758             if (best_distance == 0)
1759             {
1760               /*
1761                 We have found a perfect match and can finish iterating
1762                 through open tables list. Check for table use conflict
1763                 between calling statement and SP/trigger is done in
1764                 lock_tables().
1765               */
1766               break;
1767             }
1768           }
1769         }
1770       }
1771     }
1772     if (best_table)
1773     {
1774       table= best_table;
1775       table->query_id= thd->query_id;
1776       table->init(thd, table_list);
1777       DBUG_PRINT("info",("Using locked table"));
1778 #ifdef WITH_PARTITION_STORAGE_ENGINE
1779       part_names_error= set_partitions_as_used(table_list, table);
1780 #endif
1781       goto reset;
1782     }
1783 
1784     if (is_locked_view(thd, table_list))
1785     {
1786       if (table_list->sequence)
1787       {
1788         my_error(ER_NOT_SEQUENCE, MYF(0), table_list->db.str, table_list->alias.str);
1789         DBUG_RETURN(true);
1790       }
1791       DBUG_RETURN(FALSE); // VIEW
1792     }
1793 
1794     /*
1795       No table in the locked tables list. In case of explicit LOCK TABLES
1796       this can happen if a user did not include the table into the list.
1797       In case of pre-locked mode locked tables list is generated automatically,
1798       so we may only end up here if the table did not exist when
1799       locked tables list was created.
1800     */
1801     if (thd->locked_tables_mode == LTM_PRELOCKED)
1802       my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db.str, table_list->alias.str);
1803     else
1804       my_error(ER_TABLE_NOT_LOCKED, MYF(0), alias);
1805     DBUG_RETURN(TRUE);
1806   }
1807 
1808   /*
1809     Non pre-locked/LOCK TABLES mode, and the table is not temporary.
1810     This is the normal use case.
1811   */
1812 
1813   if (! (flags & MYSQL_OPEN_HAS_MDL_LOCK))
1814   {
1815     if (open_table_get_mdl_lock(thd, ot_ctx, &table_list->mdl_request,
1816                                 flags, &mdl_ticket) ||
1817         mdl_ticket == NULL)
1818     {
1819       DEBUG_SYNC(thd, "before_open_table_wait_refresh");
1820       DBUG_RETURN(TRUE);
1821     }
1822     DEBUG_SYNC(thd, "after_open_table_mdl_shared");
1823   }
1824   else
1825   {
1826     /*
1827       Grab reference to the MDL lock ticket that was acquired
1828       by the caller.
1829     */
1830     mdl_ticket= table_list->mdl_request.ticket;
1831   }
1832 
1833   if (table_list->open_strategy == TABLE_LIST::OPEN_IF_EXISTS)
1834   {
1835     if (!ha_table_exists(thd, &table_list->db, &table_list->table_name))
1836       DBUG_RETURN(FALSE);
1837   }
1838   else if (table_list->open_strategy == TABLE_LIST::OPEN_STUB)
1839     DBUG_RETURN(FALSE);
1840 
1841   /* Table exists. Let us try to open it. */
1842 
1843   if (table_list->i_s_requested_object & OPEN_TABLE_ONLY)
1844     gts_flags= GTS_TABLE;
1845   else if (table_list->i_s_requested_object &  OPEN_VIEW_ONLY)
1846     gts_flags= GTS_VIEW;
1847   else
1848     gts_flags= GTS_TABLE | GTS_VIEW;
1849 
1850 retry_share:
1851 
1852   share= tdc_acquire_share(thd, table_list, gts_flags, &table);
1853 
1854   if (unlikely(!share))
1855   {
1856     /*
1857       Hide "Table doesn't exist" errors if the table belongs to a view.
1858       The check for thd->is_error() is necessary to not push an
1859       unwanted error in case the error was already silenced.
1860       @todo Rework the alternative ways to deal with ER_NO_SUCH TABLE.
1861     */
1862     if (thd->is_error())
1863     {
1864       if (table_list->parent_l)
1865       {
1866         thd->clear_error();
1867         my_error(ER_WRONG_MRG_TABLE, MYF(0));
1868       }
1869       else if (table_list->belong_to_view)
1870       {
1871         TABLE_LIST *view= table_list->belong_to_view;
1872         thd->clear_error();
1873         my_error(ER_VIEW_INVALID, MYF(0),
1874                  view->view_db.str, view->view_name.str);
1875       }
1876     }
1877     DBUG_RETURN(TRUE);
1878   }
1879 
1880   /*
1881     Check if this TABLE_SHARE-object corresponds to a view. Note, that there is
1882     no need to check TABLE_SHARE::tdc.flushed as we do for regular tables,
1883     because view shares are always up to date.
1884   */
1885   if (share->is_view)
1886   {
1887     /*
1888       If parent_l of the table_list is non null then a merge table
1889       has this view as child table, which is not supported.
1890     */
1891     if (table_list->parent_l)
1892     {
1893       my_error(ER_WRONG_MRG_TABLE, MYF(0));
1894       goto err_lock;
1895     }
1896     if (table_list->sequence)
1897     {
1898       my_error(ER_NOT_SEQUENCE, MYF(0), table_list->db.str,
1899                table_list->alias.str);
1900       goto err_lock;
1901     }
1902     /*
1903       This table is a view. Validate its metadata version: in particular,
1904       that it was a view when the statement was prepared.
1905     */
1906     if (check_and_update_table_version(thd, table_list, share))
1907       goto err_lock;
1908 
1909     /* Open view */
1910     if (mysql_make_view(thd, share, table_list, false))
1911       goto err_lock;
1912 
1913     /* TODO: Don't free this */
1914     tdc_release_share(share);
1915 
1916     DBUG_ASSERT(table_list->view);
1917 
1918     DBUG_RETURN(FALSE);
1919   }
1920 
1921 #ifdef WITH_WSREP
1922   if (!((flags & MYSQL_OPEN_IGNORE_FLUSH) ||
1923         (thd->wsrep_applier)))
1924 #else
1925   if (!(flags & MYSQL_OPEN_IGNORE_FLUSH))
1926 #endif
1927   {
1928     if (share->tdc->flushed)
1929     {
1930       /*
1931         We already have an MDL lock. But we have encountered an old
1932         version of table in the table definition cache which is possible
1933         when someone changes the table version directly in the cache
1934         without acquiring a metadata lock (e.g. this can happen during
1935         "rolling" FLUSH TABLE(S)).
1936         Release our reference to share, wait until old version of
1937         share goes away and then try to get new version of table share.
1938       */
1939       if (table)
1940         tc_release_table(table);
1941       else
1942         tdc_release_share(share);
1943 
1944       MDL_deadlock_handler mdl_deadlock_handler(ot_ctx);
1945       bool wait_result;
1946 
1947       thd->push_internal_handler(&mdl_deadlock_handler);
1948       wait_result= tdc_wait_for_old_version(thd, table_list->db.str,
1949                                             table_list->table_name.str,
1950                                             ot_ctx->get_timeout(),
1951                                             mdl_ticket->get_deadlock_weight());
1952       thd->pop_internal_handler();
1953 
1954       if (wait_result)
1955         DBUG_RETURN(TRUE);
1956 
1957       goto retry_share;
1958     }
1959 
1960     if (thd->open_tables && thd->open_tables->s->tdc->flushed)
1961     {
1962       /*
1963         If the version changes while we're opening the tables,
1964         we have to back off, close all the tables opened-so-far,
1965         and try to reopen them. Note: refresh_version is currently
1966         changed only during FLUSH TABLES.
1967       */
1968       if (table)
1969         tc_release_table(table);
1970       else
1971         tdc_release_share(share);
1972       (void)ot_ctx->request_backoff_action(Open_table_context::OT_REOPEN_TABLES,
1973                                            NULL);
1974       DBUG_RETURN(TRUE);
1975     }
1976   }
1977 
1978   if (table)
1979   {
1980     DBUG_ASSERT(table->file != NULL);
1981     if (table->file->discover_check_version())
1982     {
1983       tc_release_table(table);
1984       (void) ot_ctx->request_backoff_action(Open_table_context::OT_DISCOVER,
1985                                             table_list);
1986       DBUG_RETURN(TRUE);
1987     }
1988     table->file->rebind_psi();
1989 #ifdef WITH_PARTITION_STORAGE_ENGINE
1990     part_names_error= set_partitions_as_used(table_list, table);
1991 #endif
1992   }
1993   else
1994   {
1995     enum open_frm_error error;
1996     /* make a new table */
1997     if (!(table=(TABLE*) my_malloc(key_memory_TABLE, sizeof(*table),
1998                                    MYF(MY_WME))))
1999       goto err_lock;
2000 
2001     error= open_table_from_share(thd, share, &table_list->alias,
2002                                  HA_OPEN_KEYFILE | HA_TRY_READ_ONLY,
2003                                  EXTRA_RECORD,
2004                                  thd->open_options, table, FALSE,
2005                                  IF_PARTITIONING(table_list->partition_names,0));
2006 
2007     if (unlikely(error))
2008     {
2009       my_free(table);
2010 
2011       if (error == OPEN_FRM_DISCOVER)
2012         (void) ot_ctx->request_backoff_action(Open_table_context::OT_DISCOVER,
2013                                               table_list);
2014       else if (share->crashed)
2015       {
2016         if (!(flags & MYSQL_OPEN_IGNORE_REPAIR))
2017           (void) ot_ctx->request_backoff_action(Open_table_context::OT_REPAIR,
2018                                                 table_list);
2019         else
2020           table_list->crashed= 1;  /* Mark that table was crashed */
2021       }
2022       goto err_lock;
2023     }
2024     if (open_table_entry_fini(thd, share, table))
2025     {
2026       closefrm(table);
2027       my_free(table);
2028       goto err_lock;
2029     }
2030 
2031     /* Add table to the share's used tables list. */
2032     tc_add_table(thd, table);
2033   }
2034 
2035   if (!(flags & MYSQL_OPEN_HAS_MDL_LOCK) &&
2036       table->s->table_category < TABLE_CATEGORY_INFORMATION)
2037   {
2038     /*
2039       We are not under LOCK TABLES and going to acquire write-lock/
2040       modify the base table. We need to acquire protection against
2041       global read lock until end of this statement in order to have
2042       this statement blocked by active FLUSH TABLES WITH READ LOCK.
2043 
2044       We don't need to acquire this protection under LOCK TABLES as
2045       such protection already acquired at LOCK TABLES time and
2046       not released until UNLOCK TABLES.
2047 
2048       We don't block statements which modify only temporary tables
2049       as these tables are not preserved by any form of
2050       backup which uses FLUSH TABLES WITH READ LOCK.
2051 
2052       TODO: The fact that we sometimes acquire protection against
2053             GRL only when we encounter table to be write-locked
2054             slightly increases probability of deadlock.
2055             This problem will be solved once Alik pushes his
2056             temporary table refactoring patch and we can start
2057             pre-acquiring metadata locks at the beggining of
2058             open_tables() call.
2059     */
2060     enum enum_mdl_type mdl_type= MDL_BACKUP_DML;
2061 
2062     if (table->s->table_category != TABLE_CATEGORY_USER)
2063       mdl_type= MDL_BACKUP_SYS_DML;
2064     else if (table->s->online_backup)
2065       mdl_type= MDL_BACKUP_TRANS_DML;
2066 
2067     if (table_list->mdl_request.is_write_lock_request() &&
2068         ! (flags & (MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK |
2069                     MYSQL_OPEN_FORCE_SHARED_MDL |
2070                     MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL |
2071                     MYSQL_OPEN_SKIP_SCOPED_MDL_LOCK)) &&
2072         ! ot_ctx->has_protection_against_grl(mdl_type))
2073     {
2074       MDL_request protection_request;
2075       MDL_deadlock_handler mdl_deadlock_handler(ot_ctx);
2076 
2077       if (thd->has_read_only_protection())
2078       {
2079         MYSQL_UNBIND_TABLE(table->file);
2080         tc_release_table(table);
2081         DBUG_RETURN(TRUE);
2082       }
2083 
2084       MDL_REQUEST_INIT(&protection_request, MDL_key::BACKUP, "", "", mdl_type,
2085                        MDL_STATEMENT);
2086 
2087       /*
2088         Install error handler which if possible will convert deadlock error
2089         into request to back-off and restart process of opening tables.
2090       */
2091       thd->push_internal_handler(&mdl_deadlock_handler);
2092       bool result= thd->mdl_context.acquire_lock(&protection_request,
2093                                                  ot_ctx->get_timeout());
2094       thd->pop_internal_handler();
2095 
2096       if (result)
2097       {
2098         MYSQL_UNBIND_TABLE(table->file);
2099         tc_release_table(table);
2100         DBUG_RETURN(TRUE);
2101       }
2102 
2103       ot_ctx->set_has_protection_against_grl(mdl_type);
2104     }
2105   }
2106 
2107   table->mdl_ticket= mdl_ticket;
2108   table->reginfo.lock_type=TL_READ;		/* Assume read */
2109 
2110   table->init(thd, table_list);
2111 
2112   table->next= thd->open_tables;		/* Link into simple list */
2113   thd->set_open_tables(table);
2114 
2115  reset:
2116   /*
2117     Check that there is no reference to a condition from an earlier query
2118     (cf. Bug#58553).
2119   */
2120   DBUG_ASSERT(table->file->pushed_cond == NULL);
2121   table_list->updatable= 1; // It is not derived table nor non-updatable VIEW
2122   table_list->table= table;
2123 
2124 #ifdef WITH_PARTITION_STORAGE_ENGINE
2125   if (unlikely(table->part_info))
2126   {
2127     /* Partitions specified were incorrect.*/
2128     if (part_names_error)
2129     {
2130       table->file->print_error(part_names_error, MYF(0));
2131       DBUG_RETURN(true);
2132     }
2133   }
2134   else if (table_list->partition_names)
2135   {
2136     /* Don't allow PARTITION () clause on a nonpartitioned table */
2137     my_error(ER_PARTITION_CLAUSE_ON_NONPARTITIONED, MYF(0));
2138     DBUG_RETURN(true);
2139   }
2140 #endif
2141   if (table_list->sequence && table->s->table_type != TABLE_TYPE_SEQUENCE)
2142   {
2143     my_error(ER_NOT_SEQUENCE, MYF(0), table_list->db.str, table_list->alias.str);
2144     DBUG_RETURN(true);
2145   }
2146 
2147   DBUG_ASSERT(thd->locked_tables_mode || table->file->row_logging == 0);
2148   DBUG_RETURN(false);
2149 
2150 err_lock:
2151   tdc_release_share(share);
2152 
2153   DBUG_PRINT("exit", ("failed"));
2154   DBUG_RETURN(true);
2155 }
2156 
2157 
2158 /**
2159    Find table in the list of open tables.
2160 
2161    @param list       List of TABLE objects to be inspected.
2162    @param db         Database name
2163    @param table_name Table name
2164 
2165    @return Pointer to the TABLE object found, 0 if no table found.
2166 */
2167 
find_locked_table(TABLE * list,const char * db,const char * table_name)2168 TABLE *find_locked_table(TABLE *list, const char *db, const char *table_name)
2169 {
2170   char	key[MAX_DBKEY_LENGTH];
2171   uint key_length= tdc_create_key(key, db, table_name);
2172 
2173   for (TABLE *table= list; table ; table=table->next)
2174   {
2175     if (table->s->table_cache_key.length == key_length &&
2176 	!memcmp(table->s->table_cache_key.str, key, key_length))
2177       return table;
2178   }
2179   return(0);
2180 }
2181 
2182 
2183 /**
2184    Find instance of TABLE with upgradable or exclusive metadata
2185    lock from the list of open tables, emit error if no such table
2186    found.
2187 
2188    @param thd        Thread context
2189    @param db         Database name.
2190    @param table_name Name of table.
2191    @param p_error    In the case of an error (when the function returns NULL)
2192                      the error number is stored there.
2193                      If the p_error is NULL, function launches the error itself.
2194 
2195    @note This function checks if the connection holds a global IX
2196          metadata lock. If no such lock is found, it is not safe to
2197          upgrade the lock and ER_TABLE_NOT_LOCKED_FOR_WRITE will be
2198          reported.
2199 
2200    @return Pointer to TABLE instance with MDL_SHARED_UPGRADABLE
2201            MDL_SHARED_NO_WRITE, MDL_SHARED_NO_READ_WRITE, or
2202            MDL_EXCLUSIVE metadata lock, NULL otherwise.
2203 */
2204 
find_table_for_mdl_upgrade(THD * thd,const char * db,const char * table_name,int * p_error)2205 TABLE *find_table_for_mdl_upgrade(THD *thd, const char *db,
2206                                   const char *table_name, int *p_error)
2207 {
2208   TABLE *tab= find_locked_table(thd->open_tables, db, table_name);
2209   int error;
2210 
2211   if (unlikely(!tab))
2212   {
2213     error= ER_TABLE_NOT_LOCKED;
2214     goto err_exit;
2215   }
2216 
2217   /*
2218     It is not safe to upgrade the metadata lock without a global IX lock.
2219     This can happen with FLUSH TABLES <list> WITH READ LOCK as we in these
2220     cases don't take a global IX lock in order to be compatible with
2221     global read lock.
2222   */
2223   if (unlikely(!thd->mdl_context.is_lock_owner(MDL_key::BACKUP, "", "",
2224                                                MDL_BACKUP_DDL)))
2225   {
2226     error= ER_TABLE_NOT_LOCKED_FOR_WRITE;
2227     goto err_exit;
2228   }
2229 
2230   while (tab->mdl_ticket != NULL &&
2231          !tab->mdl_ticket->is_upgradable_or_exclusive() &&
2232          (tab= find_locked_table(tab->next, db, table_name)))
2233     continue;
2234 
2235   if (unlikely(!tab))
2236   {
2237     error= ER_TABLE_NOT_LOCKED_FOR_WRITE;
2238     goto err_exit;
2239   }
2240 
2241   return tab;
2242 
2243 err_exit:
2244   if (p_error)
2245     *p_error= error;
2246   else
2247     my_error(error, MYF(0), table_name);
2248 
2249   return NULL;
2250 }
2251 
2252 
2253 /***********************************************************************
2254   class Locked_tables_list implementation. Declared in sql_class.h
2255 ************************************************************************/
2256 
2257 /**
2258   Enter LTM_LOCK_TABLES mode.
2259 
2260   Enter the LOCK TABLES mode using all the tables that are
2261   currently open and locked in this connection.
2262   Initializes a TABLE_LIST instance for every locked table.
2263 
2264   @param  thd  thread handle
2265 
2266   @return TRUE if out of memory.
2267 */
2268 
2269 bool
init_locked_tables(THD * thd)2270 Locked_tables_list::init_locked_tables(THD *thd)
2271 {
2272   DBUG_ASSERT(thd->locked_tables_mode == LTM_NONE);
2273   DBUG_ASSERT(m_locked_tables == NULL);
2274   DBUG_ASSERT(m_reopen_array == NULL);
2275   DBUG_ASSERT(m_locked_tables_count == 0);
2276 
2277   for (TABLE *table= thd->open_tables; table;
2278        table= table->next, m_locked_tables_count++)
2279   {
2280     TABLE_LIST *src_table_list= table->pos_in_table_list;
2281     LEX_CSTRING db, table_name, alias;
2282 
2283     db.length=         table->s->db.length;
2284     table_name.length= table->s->table_name.length;
2285     alias.length=      table->alias.length();
2286     TABLE_LIST *dst_table_list;
2287 
2288     if (! multi_alloc_root(&m_locked_tables_root,
2289                            &dst_table_list, sizeof(*dst_table_list),
2290                            &db.str, (size_t) db.length + 1,
2291                            &table_name.str, (size_t) table_name.length + 1,
2292                            &alias.str, (size_t) alias.length + 1,
2293                            NullS))
2294     {
2295       reset();
2296       return TRUE;
2297     }
2298 
2299     memcpy((char*) db.str,         table->s->db.str, db.length + 1);
2300     memcpy((char*) table_name.str, table->s->table_name.str,
2301            table_name.length + 1);
2302     memcpy((char*) alias.str,      table->alias.c_ptr(), alias.length + 1);
2303     dst_table_list->init_one_table(&db, &table_name,
2304                                    &alias, table->reginfo.lock_type);
2305     dst_table_list->table= table;
2306     dst_table_list->mdl_request.ticket= src_table_list->mdl_request.ticket;
2307 
2308     /* Link last into the list of tables */
2309     *(dst_table_list->prev_global= m_locked_tables_last)= dst_table_list;
2310     m_locked_tables_last= &dst_table_list->next_global;
2311     table->pos_in_locked_tables= dst_table_list;
2312   }
2313   if (m_locked_tables_count)
2314   {
2315     /**
2316       Allocate an auxiliary array to pass to mysql_lock_tables()
2317       in reopen_tables(). reopen_tables() is a critical
2318       path and we don't want to complicate it with extra allocations.
2319     */
2320     m_reopen_array= (TABLE_LIST**)alloc_root(&m_locked_tables_root,
2321                                              sizeof(TABLE_LIST*) *
2322                                              (m_locked_tables_count+1));
2323     if (m_reopen_array == NULL)
2324     {
2325       reset();
2326       return TRUE;
2327     }
2328   }
2329 
2330   TRANSACT_TRACKER(add_trx_state(thd, TX_LOCKED_TABLES));
2331 
2332   thd->enter_locked_tables_mode(LTM_LOCK_TABLES);
2333 
2334   return FALSE;
2335 }
2336 
2337 
2338 /**
2339   Leave LTM_LOCK_TABLES mode if it's been entered.
2340 
2341   Close all locked tables, free memory, and leave the mode.
2342 
2343   @note This function is a no-op if we're not in LOCK TABLES.
2344 */
2345 
2346 int
unlock_locked_tables(THD * thd)2347 Locked_tables_list::unlock_locked_tables(THD *thd)
2348 {
2349   int error;
2350   DBUG_ASSERT(!thd->in_sub_stmt &&
2351               !(thd->state_flags & Open_tables_state::BACKUPS_AVAIL));
2352   /*
2353     Sic: we must be careful to not close open tables if
2354     we're not in LOCK TABLES mode: unlock_locked_tables() is
2355     sometimes called implicitly, expecting no effect on
2356     open tables, e.g. from begin_trans().
2357   */
2358   if (thd->locked_tables_mode != LTM_LOCK_TABLES)
2359     return 0;
2360 
2361   for (TABLE_LIST *table_list= m_locked_tables;
2362        table_list; table_list= table_list->next_global)
2363   {
2364     /*
2365       Clear the position in the list, the TABLE object will be
2366       returned to the table cache.
2367     */
2368     if (table_list->table)                    // If not closed
2369       table_list->table->pos_in_locked_tables= NULL;
2370   }
2371   thd->leave_locked_tables_mode();
2372 
2373   TRANSACT_TRACKER(clear_trx_state(thd, TX_LOCKED_TABLES));
2374 
2375   DBUG_ASSERT(thd->transaction->stmt.is_empty());
2376   error= close_thread_tables(thd);
2377 
2378   /*
2379     We rely on the caller to implicitly commit the
2380     transaction and release transactional locks.
2381   */
2382 
2383   /*
2384     After closing tables we can free memory used for storing lock
2385     request for metadata locks and TABLE_LIST elements.
2386   */
2387   reset();
2388   return error;
2389 }
2390 
2391 
2392 /**
2393   Remove all meta data locks associated with table and release locked
2394   table mode if there is no locked tables anymore
2395 */
2396 
2397 int
unlock_locked_table(THD * thd,MDL_ticket * mdl_ticket)2398 Locked_tables_list::unlock_locked_table(THD *thd, MDL_ticket *mdl_ticket)
2399 {
2400   /*
2401     Ensure we are in locked table mode.
2402     As this function is only called on error condition it's better
2403     to check this condition here than in the caller.
2404   */
2405   if (thd->locked_tables_mode != LTM_LOCK_TABLES)
2406     return 0;
2407 
2408   if (mdl_ticket)
2409   {
2410     /*
2411       Under LOCK TABLES we may have several instances of table open
2412       and locked and therefore have to remove several metadata lock
2413       requests associated with them.
2414     */
2415     thd->mdl_context.release_all_locks_for_name(mdl_ticket);
2416   }
2417 
2418   if (thd->lock->table_count == 0)
2419     return unlock_locked_tables(thd);
2420   return 0;
2421 }
2422 
2423 
2424 /*
2425   Free memory allocated for storing locks
2426 */
2427 
reset()2428 void Locked_tables_list::reset()
2429 {
2430   free_root(&m_locked_tables_root, MYF(0));
2431   m_locked_tables= NULL;
2432   m_locked_tables_last= &m_locked_tables;
2433   m_reopen_array= NULL;
2434   m_locked_tables_count= 0;
2435   some_table_marked_for_reopen= 0;
2436 }
2437 
2438 
2439 /**
2440   Unlink a locked table from the locked tables list, either
2441   temporarily or permanently.
2442 
2443   @param  thd        thread handle
2444   @param  table_list the element of locked tables list.
2445                      The implementation assumes that this argument
2446                      points to a TABLE_LIST element linked into
2447                      the locked tables list. Passing a TABLE_LIST
2448                      instance that is not part of locked tables
2449                      list will lead to a crash.
2450   @param  remove_from_locked_tables
2451                       TRUE if the table is removed from the list
2452                       permanently.
2453 
2454   This function is a no-op if we're not under LOCK TABLES.
2455 
2456   @sa Locked_tables_list::reopen_tables()
2457 */
2458 
2459 
unlink_from_list(THD * thd,TABLE_LIST * table_list,bool remove_from_locked_tables)2460 void Locked_tables_list::unlink_from_list(THD *thd,
2461                                           TABLE_LIST *table_list,
2462                                           bool remove_from_locked_tables)
2463 {
2464   /*
2465     If mode is not LTM_LOCK_TABLES, we needn't do anything. Moreover,
2466     outside this mode pos_in_locked_tables value is not trustworthy.
2467   */
2468   if (thd->locked_tables_mode != LTM_LOCK_TABLES &&
2469       thd->locked_tables_mode != LTM_PRELOCKED_UNDER_LOCK_TABLES)
2470     return;
2471 
2472   /*
2473     table_list must be set and point to pos_in_locked_tables of some
2474     table.
2475   */
2476   DBUG_ASSERT(table_list->table->pos_in_locked_tables == table_list);
2477 
2478   /* Clear the pointer, the table will be returned to the table cache. */
2479   table_list->table->pos_in_locked_tables= NULL;
2480 
2481   /* Mark the table as closed in the locked tables list. */
2482   table_list->table= NULL;
2483 
2484   /*
2485     If the table is being dropped or renamed, remove it from
2486     the locked tables list (implicitly drop the LOCK TABLES lock
2487     on it).
2488   */
2489   if (remove_from_locked_tables)
2490   {
2491     *table_list->prev_global= table_list->next_global;
2492     if (table_list->next_global == NULL)
2493       m_locked_tables_last= table_list->prev_global;
2494     else
2495       table_list->next_global->prev_global= table_list->prev_global;
2496     m_locked_tables_count--;
2497   }
2498 }
2499 
2500 /**
2501   This is an attempt to recover (somewhat) in case of an error.
2502   If we failed to reopen a closed table, let's unlink it from the
2503   list and forget about it. From a user perspective that would look
2504   as if the server "lost" the lock on one of the locked tables.
2505 
2506   @note This function is a no-op if we're not under LOCK TABLES.
2507 */
2508 
2509 void Locked_tables_list::
unlink_all_closed_tables(THD * thd,MYSQL_LOCK * lock,size_t reopen_count)2510 unlink_all_closed_tables(THD *thd, MYSQL_LOCK *lock, size_t reopen_count)
2511 {
2512   /* If we managed to take a lock, unlock tables and free the lock. */
2513   if (lock)
2514     mysql_unlock_tables(thd, lock);
2515   /*
2516     If a failure happened in reopen_tables(), we may have succeeded
2517     reopening some tables, but not all.
2518     This works when the connection was killed in mysql_lock_tables().
2519   */
2520   if (reopen_count)
2521   {
2522     while (reopen_count--)
2523     {
2524       /*
2525         When closing the table, we must remove it
2526         from thd->open_tables list.
2527         We rely on the fact that open_table() that was used
2528         in reopen_tables() always links the opened table
2529         to the beginning of the open_tables list.
2530       */
2531       DBUG_ASSERT(thd->open_tables == m_reopen_array[reopen_count]->table);
2532 
2533       thd->open_tables->pos_in_locked_tables->table= NULL;
2534       thd->open_tables->pos_in_locked_tables= NULL;
2535 
2536       close_thread_table(thd, &thd->open_tables);
2537     }
2538   }
2539   /* Exclude all closed tables from the LOCK TABLES list. */
2540   for (TABLE_LIST *table_list= m_locked_tables; table_list; table_list=
2541        table_list->next_global)
2542   {
2543     if (table_list->table == NULL)
2544     {
2545       /* Unlink from list. */
2546       *table_list->prev_global= table_list->next_global;
2547       if (table_list->next_global == NULL)
2548         m_locked_tables_last= table_list->prev_global;
2549       else
2550         table_list->next_global->prev_global= table_list->prev_global;
2551       m_locked_tables_count--;
2552     }
2553   }
2554 
2555   /* If no tables left, do an automatic UNLOCK TABLES */
2556   if (thd->lock && thd->lock->table_count == 0)
2557   {
2558     /*
2559       We have to rollback any open transactions here.
2560       This is required in the case where the server has been killed
2561       but some transations are still open (as part of locked tables).
2562       If we don't do this, we will get an assert in unlock_locked_tables().
2563     */
2564     ha_rollback_trans(thd, FALSE);
2565     ha_rollback_trans(thd, TRUE);
2566     unlock_locked_tables(thd);
2567   }
2568 }
2569 
2570 
2571 /*
2572   Mark all instances of the table to be reopened
2573 
2574   This is only needed when LOCK TABLES is active
2575 */
2576 
mark_table_for_reopen(THD * thd,TABLE * table)2577 void Locked_tables_list::mark_table_for_reopen(THD *thd, TABLE *table)
2578 {
2579   TABLE_SHARE *share= table->s;
2580 
2581     for (TABLE_LIST *table_list= m_locked_tables;
2582        table_list; table_list= table_list->next_global)
2583   {
2584     if (table_list->table->s == share)
2585       table_list->table->internal_set_needs_reopen(true);
2586   }
2587   /* This is needed in the case where lock tables where not used */
2588   table->internal_set_needs_reopen(true);
2589   some_table_marked_for_reopen= 1;
2590 }
2591 
2592 
2593 /**
2594   Reopen the tables locked with LOCK TABLES and temporarily closed
2595   by a DDL statement or FLUSH TABLES.
2596 
2597   @param need_reopen  If set, reopen open tables that are marked with
2598                       for reopen.
2599                       If not set, reopen tables that where closed.
2600 
2601   @note This function is a no-op if we're not under LOCK TABLES.
2602 
2603   @return TRUE if an error reopening the tables. May happen in
2604                case of some fatal system error only, e.g. a disk
2605                corruption, out of memory or a serious bug in the
2606                locking.
2607 */
2608 
2609 bool
reopen_tables(THD * thd,bool need_reopen)2610 Locked_tables_list::reopen_tables(THD *thd, bool need_reopen)
2611 {
2612   bool is_ok= thd->get_stmt_da()->is_ok();
2613   Open_table_context ot_ctx(thd, !is_ok ? MYSQL_OPEN_REOPEN:
2614                                   MYSQL_OPEN_IGNORE_KILLED | MYSQL_OPEN_REOPEN);
2615   uint reopen_count= 0;
2616   MYSQL_LOCK *lock;
2617   MYSQL_LOCK *merged_lock;
2618   DBUG_ENTER("Locked_tables_list::reopen_tables");
2619 
2620   DBUG_ASSERT(some_table_marked_for_reopen || !need_reopen);
2621 
2622 
2623   /* Reset flag that some table was marked for reopen */
2624   if (need_reopen)
2625     some_table_marked_for_reopen= 0;
2626 
2627   for (TABLE_LIST *table_list= m_locked_tables;
2628        table_list; table_list= table_list->next_global)
2629   {
2630     if (need_reopen)
2631     {
2632       if (!table_list->table || !table_list->table->needs_reopen())
2633         continue;
2634       for (TABLE **prev= &thd->open_tables; *prev; prev= &(*prev)->next)
2635       {
2636         if (*prev == table_list->table)
2637         {
2638           thd->locked_tables_list.unlink_from_list(thd, table_list, false);
2639           mysql_lock_remove(thd, thd->lock, *prev);
2640           (*prev)->file->extra(HA_EXTRA_PREPARE_FOR_FORCED_CLOSE);
2641           close_thread_table(thd, prev);
2642           break;
2643         }
2644       }
2645       DBUG_ASSERT(table_list->table == NULL);
2646     }
2647     else
2648     {
2649       if (table_list->table)                      /* The table was not closed */
2650         continue;
2651     }
2652 
2653     DBUG_ASSERT(reopen_count < m_locked_tables_count);
2654     m_reopen_array[reopen_count++]= table_list;
2655   }
2656   if (reopen_count)
2657   {
2658     TABLE **tables= (TABLE**) my_alloca(reopen_count * sizeof(TABLE*));
2659 
2660     for (uint i= 0 ; i < reopen_count ; i++)
2661     {
2662       TABLE_LIST *table_list= m_reopen_array[i];
2663       /* Links into thd->open_tables upon success */
2664       if (open_table(thd, table_list, &ot_ctx))
2665       {
2666         unlink_all_closed_tables(thd, 0, i);
2667         my_afree((void*) tables);
2668         DBUG_RETURN(TRUE);
2669       }
2670       tables[i]= table_list->table;
2671       table_list->table->pos_in_locked_tables= table_list;
2672       /* See also the comment on lock type in init_locked_tables(). */
2673       table_list->table->reginfo.lock_type= table_list->lock_type;
2674     }
2675 
2676     thd->in_lock_tables= 1;
2677     /*
2678       We re-lock all tables with mysql_lock_tables() at once rather
2679       than locking one table at a time because of the case
2680       reported in Bug#45035: when the same table is present
2681       in the list many times, thr_lock.c fails to grant READ lock
2682       on a table that is already locked by WRITE lock, even if
2683       WRITE lock is taken by the same thread. If READ and WRITE
2684       lock are passed to thr_lock.c in the same list, everything
2685       works fine. Patching legacy code of thr_lock.c is risking to
2686       break something else.
2687     */
2688     lock= mysql_lock_tables(thd, tables, reopen_count,
2689                             MYSQL_OPEN_REOPEN | MYSQL_LOCK_USE_MALLOC);
2690     thd->in_lock_tables= 0;
2691     if (lock == NULL || (merged_lock=
2692                          mysql_lock_merge(thd->lock, lock)) == NULL)
2693     {
2694       unlink_all_closed_tables(thd, lock, reopen_count);
2695       if (! thd->killed)
2696         my_error(ER_LOCK_DEADLOCK, MYF(0));
2697       my_afree((void*) tables);
2698       DBUG_RETURN(TRUE);
2699     }
2700     thd->lock= merged_lock;
2701     my_afree((void*) tables);
2702   }
2703   DBUG_RETURN(FALSE);
2704 }
2705 
2706 /**
2707   Add back a locked table to the locked list that we just removed from it.
2708   This is needed in CREATE OR REPLACE TABLE where we are dropping, creating
2709   and re-opening a locked table.
2710 
2711   @return 0  0k
2712   @return 1  error
2713 */
2714 
restore_lock(THD * thd,TABLE_LIST * dst_table_list,TABLE * table,MYSQL_LOCK * lock)2715 bool Locked_tables_list::restore_lock(THD *thd, TABLE_LIST *dst_table_list,
2716                                       TABLE *table, MYSQL_LOCK *lock)
2717 {
2718   MYSQL_LOCK *merged_lock;
2719   DBUG_ENTER("restore_lock");
2720   DBUG_ASSERT(!strcmp(dst_table_list->table_name.str, table->s->table_name.str));
2721 
2722   /* Ensure we have the memory to add the table back */
2723   if (!(merged_lock= mysql_lock_merge(thd->lock, lock)))
2724     DBUG_RETURN(1);
2725   thd->lock= merged_lock;
2726 
2727   /* Link to the new table */
2728   dst_table_list->table= table;
2729   /*
2730     The lock type may have changed (normally it should not as create
2731     table will lock the table in write mode
2732   */
2733   dst_table_list->lock_type= table->reginfo.lock_type;
2734   table->pos_in_locked_tables= dst_table_list;
2735 
2736   add_back_last_deleted_lock(dst_table_list);
2737 
2738   table->mdl_ticket->downgrade_lock(table->reginfo.lock_type >=
2739                                     TL_WRITE_ALLOW_WRITE ?
2740                                     MDL_SHARED_NO_READ_WRITE :
2741                                     MDL_SHARED_READ);
2742 
2743   DBUG_RETURN(0);
2744 }
2745 
2746 /*
2747   Add back the last deleted lock structure.
2748   This should be followed by a call to reopen_tables() to
2749   open the table.
2750 */
2751 
add_back_last_deleted_lock(TABLE_LIST * dst_table_list)2752 void Locked_tables_list::add_back_last_deleted_lock(TABLE_LIST *dst_table_list)
2753 {
2754   /* Link the lock back in the locked tables list */
2755   dst_table_list->prev_global= m_locked_tables_last;
2756   *m_locked_tables_last= dst_table_list;
2757   m_locked_tables_last= &dst_table_list->next_global;
2758   dst_table_list->next_global= 0;
2759   m_locked_tables_count++;
2760 }
2761 
2762 
2763 #ifndef DBUG_OFF
2764 /* Cause a spurious statement reprepare for debug purposes. */
inject_reprepare(THD * thd)2765 static bool inject_reprepare(THD *thd)
2766 {
2767   if (thd->m_reprepare_observer && thd->stmt_arena->is_reprepared == FALSE)
2768   {
2769     thd->m_reprepare_observer->report_error(thd);
2770     return TRUE;
2771   }
2772 
2773   return FALSE;
2774 }
2775 #endif
2776 
2777 /**
2778   Compare metadata versions of an element obtained from the table
2779   definition cache and its corresponding node in the parse tree.
2780 
2781   @details If the new and the old values mismatch, invoke
2782   Metadata_version_observer.
2783   At prepared statement prepare, all TABLE_LIST version values are
2784   NULL and we always have a mismatch. But there is no observer set
2785   in THD, and therefore no error is reported. Instead, we update
2786   the value in the parse tree, effectively recording the original
2787   version.
2788   At prepared statement execute, an observer may be installed.  If
2789   there is a version mismatch, we push an error and return TRUE.
2790 
2791   For conventional execution (no prepared statements), the
2792   observer is never installed.
2793 
2794   @sa Execute_observer
2795   @sa check_prepared_statement() to see cases when an observer is installed
2796   @sa TABLE_LIST::is_table_ref_id_equal()
2797   @sa TABLE_SHARE::get_table_ref_id()
2798 
2799   @param[in]      thd         used to report errors
2800   @param[in,out]  tables      TABLE_LIST instance created by the parser
2801                               Metadata version information in this object
2802                               is updated upon success.
2803   @param[in]      table_share an element from the table definition cache
2804 
2805   @retval  TRUE  an error, which has been reported
2806   @retval  FALSE success, version in TABLE_LIST has been updated
2807 */
2808 
2809 static bool
check_and_update_table_version(THD * thd,TABLE_LIST * tables,TABLE_SHARE * table_share)2810 check_and_update_table_version(THD *thd,
2811                                TABLE_LIST *tables, TABLE_SHARE *table_share)
2812 {
2813   if (! tables->is_table_ref_id_equal(table_share))
2814   {
2815     if (thd->m_reprepare_observer &&
2816         thd->m_reprepare_observer->report_error(thd))
2817     {
2818       /*
2819         Version of the table share is different from the
2820         previous execution of the prepared statement, and it is
2821         unacceptable for this SQLCOM. Error has been reported.
2822       */
2823       DBUG_ASSERT(thd->is_error());
2824       return TRUE;
2825     }
2826     /* Always maintain the latest version and type */
2827     tables->set_table_ref_id(table_share);
2828   }
2829 
2830   DBUG_EXECUTE_IF("reprepare_each_statement", return inject_reprepare(thd););
2831   return FALSE;
2832 }
2833 
2834 
2835 /**
2836   Compares versions of a stored routine obtained from the sp cache
2837   and the version used at prepare.
2838 
2839   @details If the new and the old values mismatch, invoke
2840   Metadata_version_observer.
2841   At prepared statement prepare, all Sroutine_hash_entry version values
2842   are NULL and we always have a mismatch. But there is no observer set
2843   in THD, and therefore no error is reported. Instead, we update
2844   the value in Sroutine_hash_entry, effectively recording the original
2845   version.
2846   At prepared statement execute, an observer may be installed.  If
2847   there is a version mismatch, we push an error and return TRUE.
2848 
2849   For conventional execution (no prepared statements), the
2850   observer is never installed.
2851 
2852   @param[in]      thd         used to report errors
2853   @param[in/out]  rt          pointer to stored routine entry in the
2854                               parse tree
2855   @param[in]      sp          pointer to stored routine cache entry.
2856                               Can be NULL if there is no such routine.
2857   @retval  TRUE  an error, which has been reported
2858   @retval  FALSE success, version in Sroutine_hash_entry has been updated
2859 */
2860 
2861 static bool
check_and_update_routine_version(THD * thd,Sroutine_hash_entry * rt,sp_head * sp)2862 check_and_update_routine_version(THD *thd, Sroutine_hash_entry *rt,
2863                                  sp_head *sp)
2864 {
2865   ulong spc_version= sp_cache_version();
2866   /* sp is NULL if there is no such routine. */
2867   ulong version= sp ? sp->sp_cache_version() : spc_version;
2868   /*
2869     If the version in the parse tree is stale,
2870     or the version in the cache is stale and sp is not used,
2871     we need to reprepare.
2872     Sic: version != spc_version <--> sp is not NULL.
2873   */
2874   if (rt->m_sp_cache_version != version ||
2875       (version != spc_version && !sp->is_invoked()))
2876   {
2877     if (thd->m_reprepare_observer &&
2878         thd->m_reprepare_observer->report_error(thd))
2879     {
2880       /*
2881         Version of the sp cache is different from the
2882         previous execution of the prepared statement, and it is
2883         unacceptable for this SQLCOM. Error has been reported.
2884       */
2885       DBUG_ASSERT(thd->is_error());
2886       return TRUE;
2887     }
2888     /* Always maintain the latest cache version. */
2889     rt->m_sp_cache_version= version;
2890   }
2891   return FALSE;
2892 }
2893 
2894 
2895 /**
2896    Open view by getting its definition from disk (and table cache in future).
2897 
2898    @param thd               Thread handle
2899    @param table_list        TABLE_LIST with db, table_name & belong_to_view
2900    @param flags             Flags which modify how we open the view
2901 
2902    @todo This function is needed for special handling of views under
2903          LOCK TABLES. We probably should get rid of it in long term.
2904 
2905    @return FALSE if success, TRUE - otherwise.
2906 */
2907 
tdc_open_view(THD * thd,TABLE_LIST * table_list,uint flags)2908 bool tdc_open_view(THD *thd, TABLE_LIST *table_list, uint flags)
2909 {
2910   TABLE not_used;
2911   TABLE_SHARE *share;
2912   bool err= TRUE;
2913 
2914   if (!(share= tdc_acquire_share(thd, table_list, GTS_VIEW)))
2915     return TRUE;
2916 
2917   DBUG_ASSERT(share->is_view);
2918 
2919   if (flags & CHECK_METADATA_VERSION)
2920   {
2921     /*
2922       Check TABLE_SHARE-version of view only if we have been instructed to do
2923       so. We do not need to check the version if we're executing CREATE VIEW or
2924       ALTER VIEW statements.
2925 
2926       In the future, this functionality should be moved out from
2927       tdc_open_view(), and  tdc_open_view() should became a part of a clean
2928       table-definition-cache interface.
2929     */
2930     if (check_and_update_table_version(thd, table_list, share))
2931       goto ret;
2932   }
2933 
2934   err= mysql_make_view(thd, share, table_list, (flags & OPEN_VIEW_NO_PARSE));
2935 ret:
2936   tdc_release_share(share);
2937 
2938   return err;
2939 }
2940 
2941 
2942 /**
2943    Finalize the process of TABLE creation by loading table triggers
2944    and taking action if a HEAP table content was emptied implicitly.
2945 */
2946 
open_table_entry_fini(THD * thd,TABLE_SHARE * share,TABLE * entry)2947 static bool open_table_entry_fini(THD *thd, TABLE_SHARE *share, TABLE *entry)
2948 {
2949   if (Table_triggers_list::check_n_load(thd, &share->db,
2950                                         &share->table_name, entry, 0))
2951     return TRUE;
2952 
2953   /*
2954     If we are here, there was no fatal error (but error may be still
2955     unitialized).
2956   */
2957   if (unlikely(entry->file->implicit_emptied))
2958   {
2959     entry->file->implicit_emptied= 0;
2960     if (mysql_bin_log.is_open())
2961     {
2962       char query_buf[2*FN_REFLEN + 21];
2963       String query(query_buf, sizeof(query_buf), system_charset_info);
2964 
2965       query.length(0);
2966       query.append("DELETE FROM ");
2967       append_identifier(thd, &query, &share->db);
2968       query.append(".");
2969       append_identifier(thd, &query, &share->table_name);
2970 
2971       /*
2972         we bypass thd->binlog_query() here,
2973         as it does a lot of extra work, that is simply wrong in this case
2974       */
2975       Query_log_event qinfo(thd, query.ptr(), query.length(),
2976                             FALSE, TRUE, TRUE, 0);
2977       if (mysql_bin_log.write(&qinfo))
2978         return TRUE;
2979     }
2980   }
2981   return FALSE;
2982 }
2983 
2984 
2985 /**
2986    Auxiliary routine which is used for performing automatical table repair.
2987 */
2988 
auto_repair_table(THD * thd,TABLE_LIST * table_list)2989 static bool auto_repair_table(THD *thd, TABLE_LIST *table_list)
2990 {
2991   TABLE_SHARE *share;
2992   TABLE entry;
2993   bool result= TRUE;
2994 
2995   thd->clear_error();
2996 
2997   if (!(share= tdc_acquire_share(thd, table_list, GTS_TABLE)))
2998     return result;
2999 
3000   DBUG_ASSERT(! share->is_view);
3001 
3002   if (open_table_from_share(thd, share, &table_list->alias,
3003                             HA_OPEN_KEYFILE | HA_TRY_READ_ONLY,
3004                             EXTRA_RECORD,
3005                             ha_open_options | HA_OPEN_FOR_REPAIR,
3006                             &entry, FALSE) || ! entry.file ||
3007       (entry.file->is_crashed() && entry.file->ha_check_and_repair(thd)))
3008   {
3009     /* Give right error message */
3010     thd->clear_error();
3011     my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str);
3012     sql_print_error("Couldn't repair table: %s.%s", share->db.str,
3013                     share->table_name.str);
3014     if (entry.file)
3015       closefrm(&entry);
3016   }
3017   else
3018   {
3019     thd->clear_error();			// Clear error message
3020     closefrm(&entry);
3021     result= FALSE;
3022   }
3023 
3024   tdc_remove_referenced_share(thd, share);
3025   return result;
3026 }
3027 
3028 
3029 /** Open_table_context */
3030 
Open_table_context(THD * thd,uint flags)3031 Open_table_context::Open_table_context(THD *thd, uint flags)
3032   :m_thd(thd),
3033    m_failed_table(NULL),
3034    m_start_of_statement_svp(thd->mdl_context.mdl_savepoint()),
3035    m_timeout(flags & MYSQL_LOCK_IGNORE_TIMEOUT ?
3036              LONG_TIMEOUT : thd->variables.lock_wait_timeout),
3037    m_flags(flags),
3038    m_action(OT_NO_ACTION),
3039    m_has_locks(thd->mdl_context.has_locks()),
3040    m_has_protection_against_grl(0)
3041 {}
3042 
3043 
3044 /**
3045   Check if we can back-off and set back off action if we can.
3046   Otherwise report and return error.
3047 
3048   @retval  TRUE if back-off is impossible.
3049   @retval  FALSE if we can back off. Back off action has been set.
3050 */
3051 
3052 bool
3053 Open_table_context::
request_backoff_action(enum_open_table_action action_arg,TABLE_LIST * table)3054 request_backoff_action(enum_open_table_action action_arg,
3055                        TABLE_LIST *table)
3056 {
3057   /*
3058     A back off action may be one of three kinds:
3059 
3060     * We met a broken table that needs repair, or a table that
3061       is not present on this MySQL server and needs re-discovery.
3062       To perform the action, we need an exclusive metadata lock on
3063       the table. Acquiring X lock while holding other shared
3064       locks can easily lead to deadlocks. We rely on MDL deadlock
3065       detector to discover them. If this is a multi-statement
3066       transaction that holds metadata locks for completed statements,
3067       we should keep these locks after discovery/repair.
3068       The action type in this case is OT_DISCOVER or OT_REPAIR.
3069     * Our attempt to acquire an MDL lock lead to a deadlock,
3070       detected by the MDL deadlock detector. The current
3071       session was chosen a victim. If this is a multi-statement
3072       transaction that holds metadata locks taken by completed
3073       statements, restarting locking for the current statement
3074       may lead to a livelock. Releasing locks of completed
3075       statements can not be done as will lead to violation
3076       of ACID. Thus, again, if m_has_locks is set,
3077       we report an error. Otherwise, when there are no metadata
3078       locks other than which belong to this statement, we can
3079       try to recover from error by releasing all locks and
3080       restarting the pre-locking.
3081       Similarly, a deadlock error can occur when the
3082       pre-locking process met a TABLE_SHARE that is being
3083       flushed, and unsuccessfully waited for the flush to
3084       complete. A deadlock in this case can happen, e.g.,
3085       when our session is holding a metadata lock that
3086       is being waited on by a session which is using
3087       the table which is being flushed. The only way
3088       to recover from this error is, again, to close all
3089       open tables, release all locks, and retry pre-locking.
3090       Action type name is OT_REOPEN_TABLES. Re-trying
3091       while holding some locks may lead to a livelock,
3092       and thus we don't do it.
3093     * Finally, this session has open TABLEs from different
3094       "generations" of the table cache. This can happen, e.g.,
3095       when, after this session has successfully opened one
3096       table used for a statement, FLUSH TABLES interfered and
3097       expelled another table used in it. FLUSH TABLES then
3098       blocks and waits on the table already opened by this
3099       statement.
3100       We detect this situation by ensuring that table cache
3101       version of all tables used in a statement is the same.
3102       If it isn't, all tables needs to be reopened.
3103       Note, that we can always perform a reopen in this case,
3104       even if we already have metadata locks, since we don't
3105       keep tables open between statements and a livelock
3106       is not possible.
3107   */
3108   if (action_arg == OT_BACKOFF_AND_RETRY && m_has_locks)
3109   {
3110     my_error(ER_LOCK_DEADLOCK, MYF(0));
3111     m_thd->mark_transaction_to_rollback(true);
3112     return TRUE;
3113   }
3114   /*
3115     If auto-repair or discovery are requested, a pointer to table
3116     list element must be provided.
3117   */
3118   if (table)
3119   {
3120     DBUG_ASSERT(action_arg == OT_DISCOVER || action_arg == OT_REPAIR);
3121     m_failed_table= (TABLE_LIST*) m_thd->alloc(sizeof(TABLE_LIST));
3122     if (m_failed_table == NULL)
3123       return TRUE;
3124     m_failed_table->init_one_table(&table->db, &table->table_name, &table->alias, TL_WRITE);
3125     m_failed_table->open_strategy= table->open_strategy;
3126     m_failed_table->mdl_request.set_type(MDL_EXCLUSIVE);
3127   }
3128   m_action= action_arg;
3129   return FALSE;
3130 }
3131 
3132 
3133 /**
3134   An error handler to mark transaction to rollback on DEADLOCK error
3135   during DISCOVER / REPAIR.
3136 */
3137 class MDL_deadlock_discovery_repair_handler : public Internal_error_handler
3138 {
3139 public:
handle_condition(THD * thd,uint sql_errno,const char * sqlstate,Sql_condition::enum_warning_level * level,const char * msg,Sql_condition ** cond_hdl)3140   virtual bool handle_condition(THD *thd,
3141                                   uint sql_errno,
3142                                   const char* sqlstate,
3143                                   Sql_condition::enum_warning_level *level,
3144                                   const char* msg,
3145                                   Sql_condition ** cond_hdl)
3146   {
3147     if (sql_errno == ER_LOCK_DEADLOCK)
3148     {
3149       thd->mark_transaction_to_rollback(true);
3150     }
3151     /*
3152       We have marked this transaction to rollback. Return false to allow
3153       error to be reported or handled by other handlers.
3154     */
3155     return false;
3156   }
3157 };
3158 
3159 /**
3160    Recover from failed attempt of open table by performing requested action.
3161 
3162    @pre This function should be called only with "action" != OT_NO_ACTION
3163         and after having called @sa close_tables_for_reopen().
3164 
3165    @retval FALSE - Success. One should try to open tables once again.
3166    @retval TRUE  - Error
3167 */
3168 
3169 bool
recover_from_failed_open()3170 Open_table_context::recover_from_failed_open()
3171 {
3172   bool result= FALSE;
3173   MDL_deadlock_discovery_repair_handler handler;
3174   /*
3175     Install error handler to mark transaction to rollback on DEADLOCK error.
3176   */
3177   m_thd->push_internal_handler(&handler);
3178 
3179   /* Execute the action. */
3180   switch (m_action)
3181   {
3182     case OT_BACKOFF_AND_RETRY:
3183     case OT_REOPEN_TABLES:
3184       break;
3185     case OT_DISCOVER:
3186     case OT_REPAIR:
3187       if ((result= lock_table_names(m_thd, m_thd->lex->create_info,
3188                                     m_failed_table, NULL,
3189                                     get_timeout(), 0)))
3190         break;
3191 
3192       tdc_remove_table(m_thd, m_failed_table->db.str,
3193                        m_failed_table->table_name.str);
3194 
3195       switch (m_action)
3196       {
3197         case OT_DISCOVER:
3198         {
3199           m_thd->get_stmt_da()->clear_warning_info(m_thd->query_id);
3200           m_thd->clear_error();                 // Clear error message
3201 
3202           No_such_table_error_handler no_such_table_handler;
3203           bool open_if_exists= m_failed_table->open_strategy == TABLE_LIST::OPEN_IF_EXISTS;
3204 
3205           if (open_if_exists)
3206             m_thd->push_internal_handler(&no_such_table_handler);
3207 
3208           result= !tdc_acquire_share(m_thd, m_failed_table,
3209                                  GTS_TABLE | GTS_FORCE_DISCOVERY | GTS_NOLOCK);
3210           if (open_if_exists)
3211           {
3212             m_thd->pop_internal_handler();
3213             if (result && no_such_table_handler.safely_trapped_errors())
3214               result= FALSE;
3215           }
3216           break;
3217         }
3218         case OT_REPAIR:
3219           result= auto_repair_table(m_thd, m_failed_table);
3220           break;
3221         case OT_BACKOFF_AND_RETRY:
3222         case OT_REOPEN_TABLES:
3223         case OT_NO_ACTION:
3224           DBUG_ASSERT(0);
3225       }
3226       /*
3227         Rollback to start of the current statement to release exclusive lock
3228         on table which was discovered but preserve locks from previous statements
3229         in current transaction.
3230       */
3231       m_thd->mdl_context.rollback_to_savepoint(start_of_statement_svp());
3232       break;
3233     case OT_NO_ACTION:
3234       DBUG_ASSERT(0);
3235   }
3236   m_thd->pop_internal_handler();
3237   /*
3238     Reset the pointers to conflicting MDL request and the
3239     TABLE_LIST element, set when we need auto-discovery or repair,
3240     for safety.
3241   */
3242   m_failed_table= NULL;
3243   /*
3244     Reset flag indicating that we have already acquired protection
3245     against GRL. It is no longer valid as the corresponding lock was
3246     released by close_tables_for_reopen().
3247   */
3248   m_has_protection_against_grl= 0;
3249   /* Prepare for possible another back-off. */
3250   m_action= OT_NO_ACTION;
3251   return result;
3252 }
3253 
3254 
3255 /*
3256   Return a appropriate read lock type given a table object.
3257 
3258   @param thd              Thread context
3259   @param prelocking_ctx   Prelocking context.
3260   @param table_list       Table list element for table to be locked.
3261   @param routine_modifies_data
3262                           Some routine that is invoked by statement
3263                           modifies data.
3264 
3265   @remark Due to a statement-based replication limitation, statements such as
3266           INSERT INTO .. SELECT FROM .. and CREATE TABLE .. SELECT FROM need
3267           to grab a TL_READ_NO_INSERT lock on the source table in order to
3268           prevent the replication of a concurrent statement that modifies the
3269           source table. If such a statement gets applied on the slave before
3270           the INSERT .. SELECT statement finishes, data on the master could
3271           differ from data on the slave and end-up with a discrepancy between
3272           the binary log and table state.
3273           This also applies to SELECT/SET/DO statements which use stored
3274           functions. Calls to such functions are going to be logged as a
3275           whole and thus should be serialized against concurrent changes
3276           to tables used by those functions. This is avoided when functions
3277           do not modify data but only read it, since in this case nothing is
3278           written to the binary log. Argument routine_modifies_data
3279           denotes the same. So effectively, if the statement is not a
3280           update query and routine_modifies_data is false, then
3281           prelocking_placeholder does not take importance.
3282 
3283           Furthermore, this does not apply to I_S and log tables as it's
3284           always unsafe to replicate such tables under statement-based
3285           replication as the table on the slave might contain other data
3286           (ie: general_log is enabled on the slave). The statement will
3287           be marked as unsafe for SBR in decide_logging_format().
3288   @remark Note that even in prelocked mode it is important to correctly
3289           determine lock type value. In this mode lock type is passed to
3290           handler::start_stmt() method and can be used by storage engine,
3291           for example, to determine what kind of row locks it should acquire
3292           when reading data from the table.
3293 */
3294 
read_lock_type_for_table(THD * thd,Query_tables_list * prelocking_ctx,TABLE_LIST * table_list,bool routine_modifies_data)3295 thr_lock_type read_lock_type_for_table(THD *thd,
3296                                        Query_tables_list *prelocking_ctx,
3297                                        TABLE_LIST *table_list,
3298                                        bool routine_modifies_data)
3299 {
3300   /*
3301     In cases when this function is called for a sub-statement executed in
3302     prelocked mode we can't rely on OPTION_BIN_LOG flag in THD::options
3303     bitmap to determine that binary logging is turned on as this bit can
3304     be cleared before executing sub-statement. So instead we have to look
3305     at THD::variables::sql_log_bin member.
3306   */
3307   bool log_on= mysql_bin_log.is_open() && thd->variables.sql_log_bin;
3308   if ((log_on == FALSE) || (thd->wsrep_binlog_format() == BINLOG_FORMAT_ROW) ||
3309       (table_list->table->s->table_category == TABLE_CATEGORY_LOG) ||
3310       (table_list->table->s->table_category == TABLE_CATEGORY_PERFORMANCE) ||
3311       !(is_update_query(prelocking_ctx->sql_command) ||
3312         (routine_modifies_data && table_list->prelocking_placeholder) ||
3313         (thd->locked_tables_mode > LTM_LOCK_TABLES)))
3314     return TL_READ;
3315   else
3316     return TL_READ_NO_INSERT;
3317 }
3318 
3319 
3320 /*
3321   Extend the prelocking set with tables and routines used by a routine.
3322 
3323   @param[in]  thd                   Thread context.
3324   @param[in]  rt                    Element of prelocking set to be processed.
3325   @param[in]  ot_ctx                Context of open_table used to recover from
3326                                     locking failures.
3327   @retval false  Success.
3328   @retval true   Failure (Conflicting metadata lock, OOM, other errors).
3329 */
3330 static bool
sp_acquire_mdl(THD * thd,Sroutine_hash_entry * rt,Open_table_context * ot_ctx)3331 sp_acquire_mdl(THD *thd, Sroutine_hash_entry *rt, Open_table_context *ot_ctx)
3332 {
3333   DBUG_ENTER("sp_acquire_mdl");
3334   /*
3335     Since we acquire only shared lock on routines we don't
3336     need to care about global intention exclusive locks.
3337   */
3338   DBUG_ASSERT(rt->mdl_request.type == MDL_SHARED);
3339 
3340   /*
3341     Waiting for a conflicting metadata lock to go away may
3342     lead to a deadlock, detected by MDL subsystem.
3343     If possible, we try to resolve such deadlocks by releasing all
3344     metadata locks and restarting the pre-locking process.
3345     To prevent the error from polluting the diagnostics area
3346     in case of successful resolution, install a special error
3347     handler for ER_LOCK_DEADLOCK error.
3348   */
3349   MDL_deadlock_handler mdl_deadlock_handler(ot_ctx);
3350 
3351   thd->push_internal_handler(&mdl_deadlock_handler);
3352   bool result= thd->mdl_context.acquire_lock(&rt->mdl_request,
3353                                              ot_ctx->get_timeout());
3354   thd->pop_internal_handler();
3355 
3356   DBUG_RETURN(result);
3357 }
3358 
3359 
3360 /*
3361   Handle element of prelocking set other than table. E.g. cache routine
3362   and, if prelocking strategy prescribes so, extend the prelocking set
3363   with tables and routines used by it.
3364 
3365   @param[in]  thd                   Thread context.
3366   @param[in]  prelocking_ctx        Prelocking context.
3367   @param[in]  rt                    Element of prelocking set to be processed.
3368   @param[in]  prelocking_strategy   Strategy which specifies how the
3369                                     prelocking set should be extended when
3370                                     one of its elements is processed.
3371   @param[in]  has_prelocking_list   Indicates that prelocking set/list for
3372                                     this statement has already been built.
3373   @param[in]  ot_ctx                Context of open_table used to recover from
3374                                     locking failures.
3375   @param[out] need_prelocking       Set to TRUE if it was detected that this
3376                                     statement will require prelocked mode for
3377                                     its execution, not touched otherwise.
3378   @param[out] routine_modifies_data Set to TRUE if it was detected that this
3379                                     routine does modify table data.
3380 
3381   @retval FALSE  Success.
3382   @retval TRUE   Failure (Conflicting metadata lock, OOM, other errors).
3383 */
3384 
3385 static bool
open_and_process_routine(THD * thd,Query_tables_list * prelocking_ctx,Sroutine_hash_entry * rt,Prelocking_strategy * prelocking_strategy,bool has_prelocking_list,Open_table_context * ot_ctx,bool * need_prelocking,bool * routine_modifies_data)3386 open_and_process_routine(THD *thd, Query_tables_list *prelocking_ctx,
3387                          Sroutine_hash_entry *rt,
3388                          Prelocking_strategy *prelocking_strategy,
3389                          bool has_prelocking_list,
3390                          Open_table_context *ot_ctx,
3391                          bool *need_prelocking, bool *routine_modifies_data)
3392 {
3393   MDL_key::enum_mdl_namespace mdl_type= rt->mdl_request.key.mdl_namespace();
3394   DBUG_ENTER("open_and_process_routine");
3395 
3396   *routine_modifies_data= false;
3397 
3398   switch (mdl_type)
3399   {
3400   case MDL_key::PACKAGE_BODY:
3401     DBUG_ASSERT(rt != (Sroutine_hash_entry*)prelocking_ctx->sroutines_list.first);
3402     /*
3403       No need to cache the package body itself.
3404       It gets cached during open_and_process_routine()
3405       for the first used package routine. See the package related code
3406       in the "case" below.
3407     */
3408     if (sp_acquire_mdl(thd, rt, ot_ctx))
3409       DBUG_RETURN(TRUE);
3410     break;
3411   case MDL_key::FUNCTION:
3412   case MDL_key::PROCEDURE:
3413     {
3414       sp_head *sp;
3415       /*
3416         Try to get MDL lock on the routine.
3417         Note that we do not take locks on top-level CALLs as this can
3418         lead to a deadlock. Not locking top-level CALLs does not break
3419         the binlog as only the statements in the called procedure show
3420         up there, not the CALL itself.
3421       */
3422       if (rt != (Sroutine_hash_entry*)prelocking_ctx->sroutines_list.first ||
3423           mdl_type != MDL_key::PROCEDURE)
3424       {
3425         /*
3426           TODO: If this is a package routine, we should not put MDL
3427           TODO: on the routine itself. We should put only the package MDL.
3428         */
3429         if (sp_acquire_mdl(thd, rt, ot_ctx))
3430           DBUG_RETURN(TRUE);
3431 
3432         /* Ensures the routine is up-to-date and cached, if exists. */
3433         if (rt->sp_cache_routine(thd, has_prelocking_list, &sp))
3434           DBUG_RETURN(TRUE);
3435 
3436         /* Remember the version of the routine in the parse tree. */
3437         if (check_and_update_routine_version(thd, rt, sp))
3438           DBUG_RETURN(TRUE);
3439 
3440         /* 'sp' is NULL when there is no such routine. */
3441         if (sp)
3442         {
3443           *routine_modifies_data= sp->modifies_data();
3444 
3445           if (!has_prelocking_list)
3446           {
3447             prelocking_strategy->handle_routine(thd, prelocking_ctx, rt, sp,
3448                                                 need_prelocking);
3449             if (sp->m_parent)
3450             {
3451               /*
3452                 If it's a package routine, we need also to handle the
3453                 package body, as its initialization section can use
3454                 some tables and routine calls.
3455                 TODO: Only package public routines actually need this.
3456                 TODO: Skip package body handling for private routines.
3457               */
3458               *routine_modifies_data|= sp->m_parent->modifies_data();
3459               prelocking_strategy->handle_routine(thd, prelocking_ctx, rt,
3460                                                   sp->m_parent,
3461                                                   need_prelocking);
3462             }
3463           }
3464         }
3465       }
3466       else
3467       {
3468         /*
3469           If it's a top level call, just make sure we have a recent
3470           version of the routine, if it exists.
3471           Validating routine version is unnecessary, since CALL
3472           does not affect the prepared statement prelocked list.
3473         */
3474         if (rt->sp_cache_routine(thd, false, &sp))
3475           DBUG_RETURN(TRUE);
3476       }
3477     }
3478     break;
3479   case MDL_key::TRIGGER:
3480     /**
3481       We add trigger entries to lex->sroutines_list, but we don't
3482       load them here. The trigger entry is only used when building
3483       a transitive closure of objects used in a statement, to avoid
3484       adding to this closure objects that are used in the trigger more
3485       than once.
3486       E.g. if a trigger trg refers to table t2, and the trigger table t1
3487       is used multiple times in the statement (say, because it's used in
3488       function f1() twice), we will only add t2 once to the list of
3489       tables to prelock.
3490 
3491       We don't take metadata locks on triggers either: they are protected
3492       by a respective lock on the table, on which the trigger is defined.
3493 
3494       The only two cases which give "trouble" are SHOW CREATE TRIGGER
3495       and DROP TRIGGER statements. For these, statement syntax doesn't
3496       specify the table on which this trigger is defined, so we have
3497       to make a "dirty" read in the data dictionary to find out the
3498       table name. Once we discover the table name, we take a metadata
3499       lock on it, and this protects all trigger operations.
3500       Of course the table, in theory, may disappear between the dirty
3501       read and metadata lock acquisition, but in that case we just return
3502       a run-time error.
3503 
3504       Grammar of other trigger DDL statements (CREATE, DROP) requires
3505       the table to be specified explicitly, so we use the table metadata
3506       lock to protect trigger metadata in these statements. Similarly, in
3507       DML we always use triggers together with their tables, and thus don't
3508       need to take separate metadata locks on them.
3509     */
3510     break;
3511   default:
3512     /* Impossible type value. */
3513     DBUG_ASSERT(0);
3514   }
3515   DBUG_RETURN(FALSE);
3516 }
3517 
3518 /*
3519   If we are not already in prelocked mode and extended table list is not
3520   yet built we might have to build the prelocking set for this statement.
3521 
3522   Since currently no prelocking strategy prescribes doing anything for
3523   tables which are only read, we do below checks only if table is going
3524   to be changed.
3525 */
extend_table_list(THD * thd,TABLE_LIST * tables,Prelocking_strategy * prelocking_strategy,bool has_prelocking_list)3526 bool extend_table_list(THD *thd, TABLE_LIST *tables,
3527                        Prelocking_strategy *prelocking_strategy,
3528                        bool has_prelocking_list)
3529 {
3530   bool error= false;
3531   LEX *lex= thd->lex;
3532   bool maybe_need_prelocking=
3533     (tables->updating && tables->lock_type >= TL_WRITE_ALLOW_WRITE)
3534     || thd->lex->default_used;
3535 
3536   if (thd->locked_tables_mode <= LTM_LOCK_TABLES &&
3537       ! has_prelocking_list && maybe_need_prelocking)
3538   {
3539     bool need_prelocking= FALSE;
3540     TABLE_LIST **save_query_tables_last= lex->query_tables_last;
3541     /*
3542       Extend statement's table list and the prelocking set with
3543       tables and routines according to the current prelocking
3544       strategy.
3545 
3546       For example, for DML statements we need to add tables and routines
3547       used by triggers which are going to be invoked for this element of
3548       table list and also add tables required for handling of foreign keys.
3549     */
3550     error= prelocking_strategy->handle_table(thd, lex, tables,
3551                                              &need_prelocking);
3552 
3553     if (need_prelocking && ! lex->requires_prelocking())
3554       lex->mark_as_requiring_prelocking(save_query_tables_last);
3555   }
3556   return error;
3557 }
3558 
3559 
3560 /**
3561   Handle table list element by obtaining metadata lock, opening table or view
3562   and, if prelocking strategy prescribes so, extending the prelocking set with
3563   tables and routines used by it.
3564 
3565   @param[in]     thd                  Thread context.
3566   @param[in]     lex                  LEX structure for statement.
3567   @param[in]     tables               Table list element to be processed.
3568   @param[in,out] counter              Number of tables which are open.
3569   @param[in]     flags                Bitmap of flags to modify how the tables
3570                                       will be open, see open_table() description
3571                                       for details.
3572   @param[in]     prelocking_strategy  Strategy which specifies how the
3573                                       prelocking set should be extended
3574                                       when table or view is processed.
3575   @param[in]     has_prelocking_list  Indicates that prelocking set/list for
3576                                       this statement has already been built.
3577   @param[in]     ot_ctx               Context used to recover from a failed
3578                                       open_table() attempt.
3579 
3580   @retval  FALSE  Success.
3581   @retval  TRUE   Error, reported unless there is a chance to recover from it.
3582 */
3583 
3584 static bool
open_and_process_table(THD * thd,TABLE_LIST * tables,uint * counter,uint flags,Prelocking_strategy * prelocking_strategy,bool has_prelocking_list,Open_table_context * ot_ctx)3585 open_and_process_table(THD *thd, TABLE_LIST *tables, uint *counter, uint flags,
3586                        Prelocking_strategy *prelocking_strategy,
3587                        bool has_prelocking_list, Open_table_context *ot_ctx)
3588 {
3589   bool error= FALSE;
3590   bool safe_to_ignore_table= FALSE;
3591   LEX *lex= thd->lex;
3592   DBUG_ENTER("open_and_process_table");
3593   DEBUG_SYNC(thd, "open_and_process_table");
3594 
3595   /*
3596     Ignore placeholders for derived tables. After derived tables
3597     processing, link to created temporary table will be put here.
3598     If this is derived table for view then we still want to process
3599     routines used by this view.
3600   */
3601   if (tables->derived)
3602   {
3603     if (!tables->view)
3604     {
3605       if (!tables->is_derived())
3606         tables->set_derived();
3607       goto end;
3608     }
3609     /*
3610       We restore view's name and database wiped out by derived tables
3611       processing and fall back to standard open process in order to
3612       obtain proper metadata locks and do other necessary steps like
3613       stored routine processing.
3614     */
3615     tables->db= tables->view_db;
3616     tables->table_name= tables->view_name;
3617   }
3618 
3619   if (!tables->derived && is_infoschema_db(&tables->db))
3620   {
3621     /*
3622       Check whether the information schema contains a table
3623       whose name is tables->schema_table_name
3624     */
3625     ST_SCHEMA_TABLE *schema_table= tables->schema_table;
3626     if (!schema_table ||
3627         (schema_table->hidden &&
3628          ((sql_command_flags[lex->sql_command] & CF_STATUS_COMMAND) == 0 ||
3629           /*
3630             this check is used for show columns|keys from I_S hidden table
3631           */
3632           lex->sql_command == SQLCOM_SHOW_FIELDS ||
3633           lex->sql_command == SQLCOM_SHOW_KEYS)))
3634     {
3635       my_error(ER_UNKNOWN_TABLE, MYF(0),
3636                tables->table_name.str, INFORMATION_SCHEMA_NAME.str);
3637       DBUG_RETURN(1);
3638     }
3639   }
3640   /*
3641     If this TABLE_LIST object is a placeholder for an information_schema
3642     table, create a temporary table to represent the information_schema
3643     table in the query. Do not fill it yet - will be filled during
3644     execution.
3645   */
3646   if (tables->schema_table)
3647   {
3648     /*
3649       If this information_schema table is merged into a mergeable
3650       view, ignore it for now -- it will be filled when its respective
3651       TABLE_LIST is processed. This code works only during re-execution.
3652     */
3653     if (tables->view)
3654     {
3655       MDL_ticket *mdl_ticket;
3656       /*
3657         We still need to take a MDL lock on the merged view to protect
3658         it from concurrent changes.
3659       */
3660       if (!open_table_get_mdl_lock(thd, ot_ctx, &tables->mdl_request,
3661                                    flags, &mdl_ticket) &&
3662           mdl_ticket != NULL)
3663         goto process_view_routines;
3664       /* Fall-through to return error. */
3665     }
3666     else if (!mysql_schema_table(thd, lex, tables) &&
3667              !check_and_update_table_version(thd, tables, tables->table->s))
3668     {
3669       goto end;
3670     }
3671     error= TRUE;
3672     goto end;
3673   }
3674   DBUG_PRINT("tcache", ("opening table: '%s'.'%s'  item: %p",
3675                         tables->db.str, tables->table_name.str, tables));
3676   (*counter)++;
3677 
3678   /*
3679     Not a placeholder: must be a base/temporary table or a view. Let us open it.
3680   */
3681   if (tables->table)
3682   {
3683     /*
3684       If this TABLE_LIST object has an associated open TABLE object
3685       (TABLE_LIST::table is not NULL), that TABLE object must be a pre-opened
3686       temporary table or SEQUENCE (see sequence_insert()).
3687     */
3688     DBUG_ASSERT(is_temporary_table(tables) || tables->table->s->sequence);
3689     if (tables->sequence &&
3690         tables->table->s->table_type != TABLE_TYPE_SEQUENCE)
3691     {
3692       my_error(ER_NOT_SEQUENCE, MYF(0), tables->db.str, tables->alias.str);
3693       DBUG_RETURN(true);
3694     }
3695   }
3696   else if (tables->open_type == OT_TEMPORARY_ONLY)
3697   {
3698     /*
3699       OT_TEMPORARY_ONLY means that we are in CREATE TEMPORARY TABLE statement.
3700       Also such table list element can't correspond to prelocking placeholder
3701       or to underlying table of merge table.
3702       So existing temporary table should have been preopened by this moment
3703       and we can simply continue without trying to open temporary or base
3704       table.
3705     */
3706     DBUG_ASSERT(tables->open_strategy);
3707     DBUG_ASSERT(!tables->prelocking_placeholder);
3708     DBUG_ASSERT(!tables->parent_l);
3709     DBUG_RETURN(0);
3710   }
3711 
3712   /* Not a placeholder: must be a base table or a view. Let us open it. */
3713   if (tables->prelocking_placeholder)
3714   {
3715     /*
3716       For the tables added by the pre-locking code, attempt to open
3717       the table but fail silently if the table does not exist.
3718       The real failure will occur when/if a statement attempts to use
3719       that table.
3720     */
3721     No_such_table_error_handler no_such_table_handler;
3722     thd->push_internal_handler(&no_such_table_handler);
3723 
3724     /*
3725       We're opening a table from the prelocking list.
3726 
3727       Since this table list element might have been added after pre-opening
3728       of temporary tables we have to try to open temporary table for it.
3729 
3730       We can't simply skip this table list element and postpone opening of
3731       temporary table till the execution of substatement for several reasons:
3732       - Temporary table can be a MERGE table with base underlying tables,
3733         so its underlying tables has to be properly open and locked at
3734         prelocking stage.
3735       - Temporary table can be a MERGE table and we might be in PREPARE
3736         phase for a prepared statement. In this case it is important to call
3737         HA_ATTACH_CHILDREN for all merge children.
3738         This is necessary because merge children remember "TABLE_SHARE ref type"
3739         and "TABLE_SHARE def version" in the HA_ATTACH_CHILDREN operation.
3740         If HA_ATTACH_CHILDREN is not called, these attributes are not set.
3741         Then, during the first EXECUTE, those attributes need to be updated.
3742         That would cause statement re-preparing (because changing those
3743         attributes during EXECUTE is caught by THD::m_reprepare_observers).
3744         The problem is that since those attributes are not set in merge
3745         children, another round of PREPARE will not help.
3746     */
3747     if (!thd->has_temporary_tables() ||
3748         (!(error= thd->open_temporary_table(tables)) &&
3749          !tables->table))
3750       error= open_table(thd, tables, ot_ctx);
3751 
3752     thd->pop_internal_handler();
3753     safe_to_ignore_table= no_such_table_handler.safely_trapped_errors();
3754   }
3755   else if (tables->parent_l && (thd->open_options & HA_OPEN_FOR_REPAIR))
3756   {
3757     /*
3758       Also fail silently for underlying tables of a MERGE table if this
3759       table is opened for CHECK/REPAIR TABLE statement. This is needed
3760       to provide complete list of problematic underlying tables in
3761       CHECK/REPAIR TABLE output.
3762     */
3763     Repair_mrg_table_error_handler repair_mrg_table_handler;
3764     thd->push_internal_handler(&repair_mrg_table_handler);
3765 
3766     if (!thd->has_temporary_tables() ||
3767         (!(error= thd->open_temporary_table(tables)) &&
3768          !tables->table))
3769       error= open_table(thd, tables, ot_ctx);
3770 
3771     thd->pop_internal_handler();
3772     safe_to_ignore_table= repair_mrg_table_handler.safely_trapped_errors();
3773   }
3774   else
3775   {
3776     if (tables->parent_l)
3777     {
3778       /*
3779         Even if we are opening table not from the prelocking list we
3780         still might need to look for a temporary table if this table
3781         list element corresponds to underlying table of a merge table.
3782       */
3783       if (thd->has_temporary_tables())
3784         error= thd->open_temporary_table(tables);
3785     }
3786 
3787     if (!error && !tables->table)
3788       error= open_table(thd, tables, ot_ctx);
3789   }
3790 
3791   if (unlikely(error))
3792   {
3793     if (! ot_ctx->can_recover_from_failed_open() && safe_to_ignore_table)
3794     {
3795       DBUG_PRINT("info", ("open_table: ignoring table '%s'.'%s'",
3796                           tables->db.str, tables->alias.str));
3797       error= FALSE;
3798     }
3799     goto end;
3800   }
3801 
3802   /*
3803     We can't rely on simple check for TABLE_LIST::view to determine
3804     that this is a view since during re-execution we might reopen
3805     ordinary table in place of view and thus have TABLE_LIST::view
3806     set from repvious execution and TABLE_LIST::table set from
3807     current.
3808   */
3809   if (!tables->table && tables->view)
3810   {
3811     /* VIEW placeholder */
3812     (*counter)--;
3813 
3814     /*
3815       tables->next_global list consists of two parts:
3816       1) Query tables and underlying tables of views.
3817       2) Tables used by all stored routines that this statement invokes on
3818          execution.
3819       We need to know where the bound between these two parts is. If we've
3820       just opened a view, which was the last table in part #1, and it
3821       has added its base tables after itself, adjust the boundary pointer
3822       accordingly.
3823     */
3824     if (lex->query_tables_own_last == &(tables->next_global) &&
3825         tables->view->query_tables)
3826       lex->query_tables_own_last= tables->view->query_tables_last;
3827     /*
3828       Let us free memory used by 'sroutines' hash here since we never
3829       call destructor for this LEX.
3830     */
3831     my_hash_free(&tables->view->sroutines);
3832     goto process_view_routines;
3833   }
3834 
3835   /*
3836     Special types of open can succeed but still don't set
3837     TABLE_LIST::table to anything.
3838   */
3839   if (tables->open_strategy && !tables->table)
3840     goto end;
3841 
3842   error= extend_table_list(thd, tables, prelocking_strategy, has_prelocking_list);
3843   if (unlikely(error))
3844     goto end;
3845 
3846   /* Copy grant information from TABLE_LIST instance to TABLE one. */
3847   tables->table->grant= tables->grant;
3848 
3849   /* Check and update metadata version of a base table. */
3850   error= check_and_update_table_version(thd, tables, tables->table->s);
3851 
3852   if (unlikely(error))
3853     goto end;
3854   /*
3855     After opening a MERGE table add the children to the query list of
3856     tables, so that they are opened too.
3857     Note that placeholders don't have the handler open.
3858   */
3859   /* MERGE tables need to access parent and child TABLE_LISTs. */
3860   DBUG_ASSERT(tables->table->pos_in_table_list == tables);
3861   /* Non-MERGE tables ignore this call. */
3862   if (tables->table->file->extra(HA_EXTRA_ADD_CHILDREN_LIST))
3863   {
3864     error= TRUE;
3865     goto end;
3866   }
3867 
3868 process_view_routines:
3869   /*
3870     Again we may need cache all routines used by this view and add
3871     tables used by them to table list.
3872   */
3873   if (tables->view &&
3874       thd->locked_tables_mode <= LTM_LOCK_TABLES &&
3875       ! has_prelocking_list)
3876   {
3877     bool need_prelocking= FALSE;
3878     TABLE_LIST **save_query_tables_last= lex->query_tables_last;
3879 
3880     error= prelocking_strategy->handle_view(thd, lex, tables,
3881                                             &need_prelocking);
3882 
3883     if (need_prelocking && ! lex->requires_prelocking())
3884       lex->mark_as_requiring_prelocking(save_query_tables_last);
3885 
3886     if (unlikely(error))
3887       goto end;
3888   }
3889 
3890 end:
3891   DBUG_RETURN(error);
3892 }
3893 
3894 
upgrade_lock_if_not_exists(THD * thd,const DDL_options_st & create_info,TABLE_LIST * create_table,ulong lock_wait_timeout)3895 static bool upgrade_lock_if_not_exists(THD *thd,
3896                                        const DDL_options_st &create_info,
3897                                        TABLE_LIST *create_table,
3898                                        ulong lock_wait_timeout)
3899 {
3900   DBUG_ENTER("upgrade_lock_if_not_exists");
3901 
3902   if (thd->lex->sql_command == SQLCOM_CREATE_TABLE ||
3903       thd->lex->sql_command == SQLCOM_CREATE_SEQUENCE)
3904   {
3905     DEBUG_SYNC(thd,"create_table_before_check_if_exists");
3906     if (!create_info.or_replace() &&
3907         ha_table_exists(thd, &create_table->db, &create_table->table_name,
3908                         &create_table->db_type))
3909     {
3910       if (create_info.if_not_exists())
3911       {
3912         push_warning_printf(thd, Sql_condition::WARN_LEVEL_NOTE,
3913                             ER_TABLE_EXISTS_ERROR,
3914                             ER_THD(thd, ER_TABLE_EXISTS_ERROR),
3915                             create_table->table_name.str);
3916       }
3917       else
3918         my_error(ER_TABLE_EXISTS_ERROR, MYF(0), create_table->table_name.str);
3919       DBUG_RETURN(true);
3920     }
3921     DBUG_RETURN(thd->mdl_context.upgrade_shared_lock(
3922                                    create_table->mdl_request.ticket,
3923                                    MDL_EXCLUSIVE,
3924                                    lock_wait_timeout));
3925   }
3926   DBUG_RETURN(false);
3927 }
3928 
3929 
3930 /**
3931   Acquire upgradable (SNW, SNRW) metadata locks on tables used by
3932   LOCK TABLES or by a DDL statement. Under LOCK TABLES, we can't take
3933   new locks, so use open_tables_check_upgradable_mdl() instead.
3934 
3935   @param thd               Thread context.
3936   @param options           DDL options.
3937   @param tables_start      Start of list of tables on which upgradable locks
3938                            should be acquired.
3939   @param tables_end        End of list of tables.
3940   @param lock_wait_timeout Seconds to wait before timeout.
3941   @param flags             Bitmap of flags to modify how the tables will be
3942                            open, see open_table() description for details.
3943 
3944   @retval FALSE  Success.
3945   @retval TRUE   Failure (e.g. connection was killed) or table existed
3946 	         for a CREATE TABLE.
3947 
3948   @notes
3949   In case of CREATE TABLE we avoid a wait for tables that are in use
3950   by first trying to do a meta data lock with timeout == 0.  If we get a
3951   timeout we will check if table exists (it should) and retry with
3952   normal timeout if it didn't exists.
3953   Note that for CREATE TABLE IF EXISTS we only generate a warning
3954   but still return TRUE (to abort the calling open_table() function).
3955   On must check THD->is_error() if one wants to distinguish between warning
3956   and error.  If table existed, tables_start->db_type is set to the handlerton
3957   for the found table.
3958 */
3959 
3960 bool
lock_table_names(THD * thd,const DDL_options_st & options,TABLE_LIST * tables_start,TABLE_LIST * tables_end,ulong lock_wait_timeout,uint flags)3961 lock_table_names(THD *thd, const DDL_options_st &options,
3962                  TABLE_LIST *tables_start, TABLE_LIST *tables_end,
3963                  ulong lock_wait_timeout, uint flags)
3964 {
3965   MDL_request_list mdl_requests;
3966   TABLE_LIST *table;
3967   MDL_request global_request;
3968   MDL_savepoint mdl_savepoint;
3969   DBUG_ENTER("lock_table_names");
3970 
3971   DBUG_ASSERT(!thd->locked_tables_mode);
3972 
3973   for (table= tables_start; table && table != tables_end;
3974        table= table->next_global)
3975   {
3976     DBUG_PRINT("info", ("mdl_request.type: %d  open_type: %d",
3977                         table->mdl_request.type, table->open_type));
3978     if (table->mdl_request.type < MDL_SHARED_UPGRADABLE ||
3979         table->mdl_request.type == MDL_SHARED_READ_ONLY ||
3980         table->open_type == OT_TEMPORARY_ONLY ||
3981         (table->open_type == OT_TEMPORARY_OR_BASE && is_temporary_table(table)))
3982     {
3983       continue;
3984     }
3985 
3986     /* Write lock on normal tables is not allowed in a read only transaction. */
3987     if (thd->tx_read_only)
3988     {
3989       my_error(ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION, MYF(0));
3990       DBUG_RETURN(true);
3991     }
3992 
3993     /* Scoped locks: Take intention exclusive locks on all involved schemas. */
3994     if (!(flags & MYSQL_OPEN_SKIP_SCOPED_MDL_LOCK))
3995     {
3996       MDL_request *schema_request= new (thd->mem_root) MDL_request;
3997       if (schema_request == NULL)
3998         DBUG_RETURN(TRUE);
3999       MDL_REQUEST_INIT(schema_request, MDL_key::SCHEMA, table->db.str, "",
4000                        MDL_INTENTION_EXCLUSIVE, MDL_TRANSACTION);
4001       mdl_requests.push_front(schema_request);
4002     }
4003 
4004     mdl_requests.push_front(&table->mdl_request);
4005   }
4006 
4007   if (mdl_requests.is_empty())
4008     DBUG_RETURN(FALSE);
4009 
4010   if (flags & MYSQL_OPEN_SKIP_SCOPED_MDL_LOCK)
4011   {
4012     DBUG_RETURN(thd->mdl_context.acquire_locks(&mdl_requests,
4013                                                lock_wait_timeout) ||
4014                 upgrade_lock_if_not_exists(thd, options, tables_start,
4015                                            lock_wait_timeout));
4016   }
4017 
4018   /* Protect this statement against concurrent BACKUP STAGE or FTWRL. */
4019   if (thd->has_read_only_protection())
4020     DBUG_RETURN(true);
4021 
4022   MDL_REQUEST_INIT(&global_request, MDL_key::BACKUP, "", "", MDL_BACKUP_DDL,
4023                    MDL_STATEMENT);
4024   mdl_savepoint= thd->mdl_context.mdl_savepoint();
4025 
4026   while (!thd->mdl_context.acquire_locks(&mdl_requests, lock_wait_timeout) &&
4027          !upgrade_lock_if_not_exists(thd, options, tables_start,
4028                                      lock_wait_timeout) &&
4029          !thd->mdl_context.try_acquire_lock(&global_request))
4030   {
4031     if (global_request.ticket)
4032     {
4033       thd->mdl_backup_ticket= global_request.ticket;
4034       DBUG_RETURN(false);
4035     }
4036 
4037     /*
4038       There is ongoing or pending BACKUP STAGE or FTWRL.
4039       Wait until it finishes and re-try.
4040     */
4041     thd->mdl_context.rollback_to_savepoint(mdl_savepoint);
4042     if (thd->mdl_context.acquire_lock(&global_request, lock_wait_timeout))
4043       break;
4044     thd->mdl_context.rollback_to_savepoint(mdl_savepoint);
4045 
4046     /* Reset tickets for all acquired locks */
4047     global_request.ticket= 0;
4048     MDL_request_list::Iterator it(mdl_requests);
4049     while (auto mdl_request= it++)
4050       mdl_request->ticket= 0;
4051   }
4052   DBUG_RETURN(true);
4053 }
4054 
4055 
4056 /**
4057   Check for upgradable (SNW, SNRW) metadata locks on tables to be opened
4058   for a DDL statement. Under LOCK TABLES, we can't take new locks, so we
4059   must check if appropriate locks were pre-acquired.
4060 
4061   @param thd           Thread context.
4062   @param tables_start  Start of list of tables on which upgradable locks
4063                        should be searched for.
4064   @param tables_end    End of list of tables.
4065   @param flags         Bitmap of flags to modify how the tables will be
4066                        open, see open_table() description for details.
4067 
4068   @retval FALSE  Success.
4069   @retval TRUE   Failure (e.g. connection was killed)
4070 */
4071 
4072 static bool
open_tables_check_upgradable_mdl(THD * thd,TABLE_LIST * tables_start,TABLE_LIST * tables_end,uint flags)4073 open_tables_check_upgradable_mdl(THD *thd, TABLE_LIST *tables_start,
4074                                  TABLE_LIST *tables_end, uint flags)
4075 {
4076   TABLE_LIST *table;
4077 
4078   DBUG_ASSERT(thd->locked_tables_mode);
4079 
4080   for (table= tables_start; table && table != tables_end;
4081        table= table->next_global)
4082   {
4083     /*
4084       Check below needs to be updated if this function starts
4085       called for SRO locks.
4086     */
4087     DBUG_ASSERT(table->mdl_request.type != MDL_SHARED_READ_ONLY);
4088     if (table->mdl_request.type < MDL_SHARED_UPGRADABLE ||
4089         table->open_type == OT_TEMPORARY_ONLY ||
4090         (table->open_type == OT_TEMPORARY_OR_BASE && is_temporary_table(table)))
4091     {
4092       continue;
4093     }
4094 
4095     /*
4096       We don't need to do anything about the found TABLE instance as it
4097       will be handled later in open_tables(), we only need to check that
4098       an upgradable lock is already acquired. When we enter LOCK TABLES
4099       mode, SNRW locks are acquired before all other locks. So if under
4100       LOCK TABLES we find that there is TABLE instance with upgradeable
4101       lock, all other instances of TABLE for the same table will have the
4102       same ticket.
4103 
4104       Note that this works OK even for CREATE TABLE statements which
4105       request X type of metadata lock. This is because under LOCK TABLES
4106       such statements don't create the table but only check if it exists
4107       or, in most complex case, only insert into it.
4108       Thus SNRW lock should be enough.
4109 
4110       Note that find_table_for_mdl_upgrade() will report an error if
4111       no suitable ticket is found.
4112     */
4113     if (!find_table_for_mdl_upgrade(thd, table->db.str, table->table_name.str,
4114                                     NULL))
4115       return TRUE;
4116   }
4117 
4118   return FALSE;
4119 }
4120 
4121 
4122 /**
4123   Open all tables in list
4124 
4125   @param[in]     thd      Thread context.
4126   @param[in]     options  DDL options.
4127   @param[in,out] start    List of tables to be open (it can be adjusted for
4128                           statement that uses tables only implicitly, e.g.
4129                           for "SELECT f1()").
4130   @param[out]    counter  Number of tables which were open.
4131   @param[in]     flags    Bitmap of flags to modify how the tables will be
4132                           open, see open_table() description for details.
4133   @param[in]     prelocking_strategy  Strategy which specifies how prelocking
4134                                       algorithm should work for this statement.
4135 
4136   @note
4137     Unless we are already in prelocked mode and prelocking strategy prescribes
4138     so this function will also precache all SP/SFs explicitly or implicitly
4139     (via views and triggers) used by the query and add tables needed for their
4140     execution to table list. Statement that uses SFs, invokes triggers or
4141     requires foreign key checks will be marked as requiring prelocking.
4142     Prelocked mode will be enabled for such query during lock_tables() call.
4143 
4144     If query for which we are opening tables is already marked as requiring
4145     prelocking it won't do such precaching and will simply reuse table list
4146     which is already built.
4147 
4148   @retval  FALSE  Success.
4149   @retval  TRUE   Error, reported.
4150 */
4151 
open_tables(THD * thd,const DDL_options_st & options,TABLE_LIST ** start,uint * counter,uint flags,Prelocking_strategy * prelocking_strategy)4152 bool open_tables(THD *thd, const DDL_options_st &options,
4153                  TABLE_LIST **start, uint *counter, uint flags,
4154                  Prelocking_strategy *prelocking_strategy)
4155 {
4156   /*
4157     We use pointers to "next_global" member in the last processed
4158     TABLE_LIST element and to the "next" member in the last processed
4159     Sroutine_hash_entry element as iterators over, correspondingly,
4160     the table list and stored routines list which stay valid and allow
4161     to continue iteration when new elements are added to the tail of
4162     the lists.
4163   */
4164   TABLE_LIST **table_to_open;
4165   Sroutine_hash_entry **sroutine_to_open;
4166   TABLE_LIST *tables;
4167   Open_table_context ot_ctx(thd, flags);
4168   bool error= FALSE;
4169   bool some_routine_modifies_data= FALSE;
4170   bool has_prelocking_list;
4171   DBUG_ENTER("open_tables");
4172 
4173   /* Data access in XA transaction is only allowed when it is active. */
4174   for (TABLE_LIST *table= *start; table; table= table->next_global)
4175     if (!table->schema_table)
4176     {
4177       if (thd->transaction->xid_state.check_has_uncommitted_xa())
4178       {
4179 	thd->transaction->xid_state.er_xaer_rmfail();
4180         DBUG_RETURN(true);
4181       }
4182       else
4183         break;
4184     }
4185 
4186   thd->current_tablenr= 0;
4187 restart:
4188   /*
4189     Close HANDLER tables which are marked for flush or against which there
4190     are pending exclusive metadata locks. This is needed both in order to
4191     avoid deadlocks and to have a point during statement execution at
4192     which such HANDLERs are closed even if they don't create problems for
4193     the current session (i.e. to avoid having a DDL blocked by HANDLERs
4194     opened for a long time).
4195   */
4196   if (thd->handler_tables_hash.records)
4197     mysql_ha_flush(thd);
4198 
4199   has_prelocking_list= thd->lex->requires_prelocking();
4200   table_to_open= start;
4201   sroutine_to_open= &thd->lex->sroutines_list.first;
4202   *counter= 0;
4203   THD_STAGE_INFO(thd, stage_opening_tables);
4204   prelocking_strategy->reset(thd);
4205 
4206   /*
4207     If we are executing LOCK TABLES statement or a DDL statement
4208     (in non-LOCK TABLES mode) we might have to acquire upgradable
4209     semi-exclusive metadata locks (SNW or SNRW) on some of the
4210     tables to be opened.
4211     When executing CREATE TABLE .. If NOT EXISTS .. SELECT, the
4212     table may not yet exist, in which case we acquire an exclusive
4213     lock.
4214     We acquire all such locks at once here as doing this in one
4215     by one fashion may lead to deadlocks or starvation. Later when
4216     we will be opening corresponding table pre-acquired metadata
4217     lock will be reused (thanks to the fact that in recursive case
4218     metadata locks are acquired without waiting).
4219   */
4220   if (! (flags & (MYSQL_OPEN_HAS_MDL_LOCK |
4221                   MYSQL_OPEN_FORCE_SHARED_MDL |
4222                   MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL)))
4223   {
4224     if (thd->locked_tables_mode)
4225     {
4226       /*
4227         Under LOCK TABLES, we can't acquire new locks, so we instead
4228         need to check if appropriate locks were pre-acquired.
4229       */
4230       if (open_tables_check_upgradable_mdl(thd, *start,
4231                                            thd->lex->first_not_own_table(),
4232                                            flags))
4233       {
4234         error= TRUE;
4235         goto error;
4236       }
4237     }
4238     else
4239     {
4240       TABLE_LIST *table;
4241       if (lock_table_names(thd, options, *start,
4242                            thd->lex->first_not_own_table(),
4243                            ot_ctx.get_timeout(), flags))
4244       {
4245         error= TRUE;
4246         goto error;
4247       }
4248       for (table= *start; table && table != thd->lex->first_not_own_table();
4249            table= table->next_global)
4250       {
4251         if (table->mdl_request.type >= MDL_SHARED_UPGRADABLE)
4252           table->mdl_request.ticket= NULL;
4253       }
4254     }
4255   }
4256 
4257   /*
4258     Perform steps of prelocking algorithm until there are unprocessed
4259     elements in prelocking list/set.
4260   */
4261   while (*table_to_open  ||
4262          (thd->locked_tables_mode <= LTM_LOCK_TABLES && *sroutine_to_open))
4263   {
4264     /*
4265       For every table in the list of tables to open, try to find or open
4266       a table.
4267     */
4268     for (tables= *table_to_open; tables;
4269          table_to_open= &tables->next_global, tables= tables->next_global)
4270     {
4271       error= open_and_process_table(thd, tables, counter, flags,
4272                                     prelocking_strategy, has_prelocking_list,
4273                                     &ot_ctx);
4274 
4275       if (unlikely(error))
4276       {
4277         if (ot_ctx.can_recover_from_failed_open())
4278         {
4279           /*
4280             We have met exclusive metadata lock or old version of table.
4281             Now we have to close all tables and release metadata locks.
4282             We also have to throw away set of prelocked tables (and thus
4283             close tables from this set that were open by now) since it
4284             is possible that one of tables which determined its content
4285             was changed.
4286 
4287             Instead of implementing complex/non-robust logic mentioned
4288             above we simply close and then reopen all tables.
4289 
4290             We have to save pointer to table list element for table which we
4291             have failed to open since closing tables can trigger removal of
4292             elements from the table list (if MERGE tables are involved),
4293           */
4294           close_tables_for_reopen(thd, start, ot_ctx.start_of_statement_svp());
4295 
4296           /*
4297             Here we rely on the fact that 'tables' still points to the valid
4298             TABLE_LIST element. Altough currently this assumption is valid
4299             it may change in future.
4300           */
4301           if (ot_ctx.recover_from_failed_open())
4302             goto error;
4303 
4304           /* Re-open temporary tables after close_tables_for_reopen(). */
4305           if (thd->open_temporary_tables(*start))
4306             goto error;
4307 
4308           error= FALSE;
4309           goto restart;
4310         }
4311         goto error;
4312       }
4313 
4314       DEBUG_SYNC(thd, "open_tables_after_open_and_process_table");
4315     }
4316 
4317     /*
4318       If we are not already in prelocked mode and extended table list is
4319       not yet built for our statement we need to cache routines it uses
4320       and build the prelocking list for it.
4321       If we are not in prelocked mode but have built the extended table
4322       list, we still need to call open_and_process_routine() to take
4323       MDL locks on the routines.
4324     */
4325     if (thd->locked_tables_mode <= LTM_LOCK_TABLES && *sroutine_to_open)
4326     {
4327       /*
4328         Process elements of the prelocking set which are present there
4329         since parsing stage or were added to it by invocations of
4330         Prelocking_strategy methods in the above loop over tables.
4331 
4332         For example, if element is a routine, cache it and then,
4333         if prelocking strategy prescribes so, add tables it uses to the
4334         table list and routines it might invoke to the prelocking set.
4335       */
4336       for (Sroutine_hash_entry *rt= *sroutine_to_open; rt;
4337            sroutine_to_open= &rt->next, rt= rt->next)
4338       {
4339         bool need_prelocking= false;
4340         bool routine_modifies_data;
4341         TABLE_LIST **save_query_tables_last= thd->lex->query_tables_last;
4342 
4343         error= open_and_process_routine(thd, thd->lex, rt, prelocking_strategy,
4344                                         has_prelocking_list, &ot_ctx,
4345                                         &need_prelocking,
4346                                         &routine_modifies_data);
4347 
4348         // Remember if any of SF modifies data.
4349         some_routine_modifies_data|= routine_modifies_data;
4350 
4351         if (need_prelocking && ! thd->lex->requires_prelocking())
4352           thd->lex->mark_as_requiring_prelocking(save_query_tables_last);
4353 
4354         if (need_prelocking && ! *start)
4355           *start= thd->lex->query_tables;
4356 
4357         if (unlikely(error))
4358         {
4359           if (ot_ctx.can_recover_from_failed_open())
4360           {
4361             close_tables_for_reopen(thd, start,
4362                                     ot_ctx.start_of_statement_svp());
4363             if (ot_ctx.recover_from_failed_open())
4364               goto error;
4365 
4366             /* Re-open temporary tables after close_tables_for_reopen(). */
4367             if (thd->open_temporary_tables(*start))
4368               goto error;
4369 
4370             error= FALSE;
4371             goto restart;
4372           }
4373           /*
4374             Serious error during reading stored routines from mysql.proc table.
4375             Something is wrong with the table or its contents, and an error has
4376             been emitted; we must abort.
4377           */
4378           goto error;
4379         }
4380       }
4381     }
4382     if ((error= prelocking_strategy->handle_end(thd)))
4383       goto error;
4384   }
4385 
4386   /*
4387     After successful open of all tables, including MERGE parents and
4388     children, attach the children to their parents. At end of statement,
4389     the children are detached. Attaching and detaching are always done,
4390     even under LOCK TABLES.
4391 
4392     We also convert all TL_WRITE_DEFAULT and TL_READ_DEFAULT locks to
4393     appropriate "real" lock types to be used for locking and to be passed
4394     to storage engine.
4395 
4396     And start wsrep TOI if needed.
4397   */
4398   for (tables= *start; tables; tables= tables->next_global)
4399   {
4400     TABLE *tbl= tables->table;
4401 
4402     if (!tbl)
4403       continue;
4404 
4405     /* Schema tables may not have a TABLE object here. */
4406     if (tbl->file->ha_table_flags() & HA_CAN_MULTISTEP_MERGE)
4407     {
4408       /* MERGE tables need to access parent and child TABLE_LISTs. */
4409       DBUG_ASSERT(tbl->pos_in_table_list == tables);
4410       if (tbl->file->extra(HA_EXTRA_ATTACH_CHILDREN))
4411       {
4412         error= TRUE;
4413         goto error;
4414       }
4415     }
4416 
4417     /* Set appropriate TABLE::lock_type. */
4418     if (tbl && tables->lock_type != TL_UNLOCK && !thd->locked_tables_mode)
4419     {
4420       if (tables->lock_type == TL_WRITE_DEFAULT)
4421         tbl->reginfo.lock_type= thd->update_lock_default;
4422       else if (tables->lock_type == TL_READ_DEFAULT)
4423           tbl->reginfo.lock_type=
4424             read_lock_type_for_table(thd, thd->lex, tables,
4425                                      some_routine_modifies_data);
4426       else
4427         tbl->reginfo.lock_type= tables->lock_type;
4428     }
4429   }
4430 
4431 #ifdef WITH_WSREP
4432   if (WSREP(thd)                                       &&
4433       wsrep_replicate_myisam                           &&
4434       (*start)                                         &&
4435       (*start)->table                                  &&
4436       (*start)->table->file->ht == myisam_hton         &&
4437       wsrep_thd_is_local(thd)                          &&
4438       !is_stat_table(&(*start)->db, &(*start)->alias)  &&
4439       thd->get_command() != COM_STMT_PREPARE           &&
4440       !thd->stmt_arena->is_stmt_prepare()              &&
4441       ((thd->lex->sql_command == SQLCOM_INSERT         ||
4442         thd->lex->sql_command == SQLCOM_INSERT_SELECT  ||
4443         thd->lex->sql_command == SQLCOM_REPLACE        ||
4444         thd->lex->sql_command == SQLCOM_REPLACE_SELECT ||
4445         thd->lex->sql_command == SQLCOM_UPDATE         ||
4446         thd->lex->sql_command == SQLCOM_UPDATE_MULTI   ||
4447         thd->lex->sql_command == SQLCOM_LOAD           ||
4448         thd->lex->sql_command == SQLCOM_DELETE)))
4449   {
4450       wsrep_before_rollback(thd, true);
4451       wsrep_after_rollback(thd, true);
4452       wsrep_after_statement(thd);
4453       WSREP_TO_ISOLATION_BEGIN(NULL, NULL, (*start));
4454   }
4455 #endif /* WITH_WSREP */
4456 
4457 error:
4458 #ifdef WITH_WSREP
4459 wsrep_error_label:
4460 #endif
4461   THD_STAGE_INFO(thd, stage_after_opening_tables);
4462   thd_proc_info(thd, 0);
4463 
4464   if (unlikely(error) && *table_to_open)
4465   {
4466     (*table_to_open)->table= NULL;
4467   }
4468   DBUG_PRINT("open_tables", ("returning: %d", (int) error));
4469   DBUG_RETURN(error);
4470 }
4471 
4472 
4473 /**
4474   Defines how prelocking algorithm for DML statements should handle routines:
4475   - For CALL statements we do unrolling (i.e. open and lock tables for each
4476     sub-statement individually). So for such statements prelocking is enabled
4477     only if stored functions are used in parameter list and only for period
4478     during which we calculate values of parameters. Thus in this strategy we
4479     ignore procedure which is directly called by such statement and extend
4480     the prelocking set only with tables/functions used by SF called from the
4481     parameter list.
4482   - For any other statement any routine which is directly or indirectly called
4483     by statement is going to be executed in prelocked mode. So in this case we
4484     simply add all tables and routines used by it to the prelocking set.
4485 
4486   @param[in]  thd              Thread context.
4487   @param[in]  prelocking_ctx   Prelocking context of the statement.
4488   @param[in]  rt               Prelocking set element describing routine.
4489   @param[in]  sp               Routine body.
4490   @param[out] need_prelocking  Set to TRUE if method detects that prelocking
4491                                required, not changed otherwise.
4492 
4493   @retval FALSE  Success.
4494   @retval TRUE   Failure (OOM).
4495 */
4496 
4497 bool DML_prelocking_strategy::
handle_routine(THD * thd,Query_tables_list * prelocking_ctx,Sroutine_hash_entry * rt,sp_head * sp,bool * need_prelocking)4498 handle_routine(THD *thd, Query_tables_list *prelocking_ctx,
4499                Sroutine_hash_entry *rt, sp_head *sp, bool *need_prelocking)
4500 {
4501   /*
4502     We assume that for any "CALL proc(...)" statement sroutines_list will
4503     have 'proc' as first element (it may have several, consider e.g.
4504     "proc(sp_func(...)))". This property is currently guaranted by the
4505     parser.
4506   */
4507 
4508   if (rt != (Sroutine_hash_entry*)prelocking_ctx->sroutines_list.first ||
4509       rt->mdl_request.key.mdl_namespace() != MDL_key::PROCEDURE)
4510   {
4511     *need_prelocking= TRUE;
4512     sp_update_stmt_used_routines(thd, prelocking_ctx, &sp->m_sroutines,
4513                                  rt->belong_to_view);
4514     (void)sp->add_used_tables_to_table_list(thd,
4515                                             &prelocking_ctx->query_tables_last,
4516                                             rt->belong_to_view);
4517   }
4518   sp->propagate_attributes(prelocking_ctx);
4519   return FALSE;
4520 }
4521 
4522 
4523 /*
4524   @note this can be changed to use a hash, instead of scanning the linked
4525   list, if the performance of this function will ever become an issue
4526 */
table_already_fk_prelocked(TABLE_LIST * tl,LEX_CSTRING * db,LEX_CSTRING * table,thr_lock_type lock_type)4527 bool table_already_fk_prelocked(TABLE_LIST *tl, LEX_CSTRING *db,
4528                                 LEX_CSTRING *table, thr_lock_type lock_type)
4529 {
4530   for (; tl; tl= tl->next_global )
4531   {
4532     if (tl->lock_type >= lock_type &&
4533         tl->prelocking_placeholder == TABLE_LIST::PRELOCK_FK &&
4534         strcmp(tl->db.str, db->str) == 0 &&
4535         strcmp(tl->table_name.str, table->str) == 0)
4536       return true;
4537   }
4538   return false;
4539 }
4540 
4541 
internal_table_exists(TABLE_LIST * global_list,const char * table_name)4542 static TABLE_LIST *internal_table_exists(TABLE_LIST *global_list,
4543                                          const char *table_name)
4544 {
4545   do
4546   {
4547     if (global_list->table_name.str == table_name)
4548       return global_list;
4549   } while ((global_list= global_list->next_global));
4550   return 0;
4551 }
4552 
4553 
4554 static bool
add_internal_tables(THD * thd,Query_tables_list * prelocking_ctx,TABLE_LIST * tables)4555 add_internal_tables(THD *thd, Query_tables_list *prelocking_ctx,
4556                     TABLE_LIST *tables)
4557 {
4558   TABLE_LIST *global_table_list= prelocking_ctx->query_tables;
4559   DBUG_ENTER("add_internal_tables");
4560 
4561   do
4562   {
4563     TABLE_LIST *tmp __attribute__((unused));
4564     DBUG_PRINT("info", ("table name: %s", tables->table_name.str));
4565     /*
4566       Skip table if already in the list. Can happen with prepared statements
4567     */
4568     if ((tmp= internal_table_exists(global_table_list,
4569                                     tables->table_name.str)))
4570     {
4571       /*
4572         Use the original value for the next local, used by the
4573         original prepared statement. We cannot trust the original
4574         next_local value as it may have been changed by a previous
4575         statement using the same table.
4576       */
4577       tables->next_local= tmp;
4578       continue;
4579     }
4580 
4581     TABLE_LIST *tl= (TABLE_LIST *) thd->alloc(sizeof(TABLE_LIST));
4582     if (!tl)
4583       DBUG_RETURN(TRUE);
4584     tl->init_one_table_for_prelocking(&tables->db,
4585                                       &tables->table_name,
4586                                       NULL, tables->lock_type,
4587                                       TABLE_LIST::PRELOCK_NONE,
4588                                       0, 0,
4589                                       &prelocking_ctx->query_tables_last,
4590                                       tables->for_insert_data);
4591     /*
4592       Store link to the new table_list that will be used by open so that
4593       Item_func_nextval() can find it
4594     */
4595     tables->next_local= tl;
4596     DBUG_PRINT("info", ("table name: %s added", tables->table_name.str));
4597   } while ((tables= tables->next_global));
4598   DBUG_RETURN(FALSE);
4599 }
4600 
4601 /**
4602   Extend the table_list to include foreign tables for prelocking.
4603 
4604   @param[in]  thd              Thread context.
4605   @param[in]  prelocking_ctx   Prelocking context of the statement.
4606   @param[in]  table_list       Table list element for table.
4607   @param[in]  sp               Routine body.
4608   @param[out] need_prelocking  Set to TRUE if method detects that prelocking
4609                                required, not changed otherwise.
4610 
4611   @retval FALSE  Success.
4612   @retval TRUE   Failure (OOM).
4613 */
4614 inline bool
prepare_fk_prelocking_list(THD * thd,Query_tables_list * prelocking_ctx,TABLE_LIST * table_list,bool * need_prelocking,uint8 op)4615 prepare_fk_prelocking_list(THD *thd, Query_tables_list *prelocking_ctx,
4616                            TABLE_LIST *table_list, bool *need_prelocking,
4617                            uint8 op)
4618 {
4619   DBUG_ENTER("prepare_fk_prelocking_list");
4620   List <FOREIGN_KEY_INFO> fk_list;
4621   List_iterator<FOREIGN_KEY_INFO> fk_list_it(fk_list);
4622   FOREIGN_KEY_INFO *fk;
4623   Query_arena *arena, backup;
4624   TABLE *table= table_list->table;
4625 
4626   arena= thd->activate_stmt_arena_if_needed(&backup);
4627 
4628   table->file->get_parent_foreign_key_list(thd, &fk_list);
4629   if (unlikely(thd->is_error()))
4630   {
4631     if (arena)
4632       thd->restore_active_arena(arena, &backup);
4633     return TRUE;
4634   }
4635 
4636   *need_prelocking= TRUE;
4637 
4638   while ((fk= fk_list_it++))
4639   {
4640     // FK_OPTION_RESTRICT and FK_OPTION_NO_ACTION only need read access
4641     thr_lock_type lock_type;
4642 
4643     if ((op & (1 << TRG_EVENT_DELETE) && fk_modifies_child(fk->delete_method))
4644         || (op & (1 << TRG_EVENT_UPDATE) && fk_modifies_child(fk->update_method)))
4645       lock_type= TL_WRITE_ALLOW_WRITE;
4646     else
4647       lock_type= TL_READ;
4648 
4649     if (table_already_fk_prelocked(prelocking_ctx->query_tables,
4650           fk->foreign_db, fk->foreign_table,
4651           lock_type))
4652       continue;
4653 
4654     TABLE_LIST *tl= (TABLE_LIST *) thd->alloc(sizeof(TABLE_LIST));
4655     tl->init_one_table_for_prelocking(fk->foreign_db,
4656         fk->foreign_table,
4657         NULL, lock_type,
4658         TABLE_LIST::PRELOCK_FK,
4659         table_list->belong_to_view, op,
4660         &prelocking_ctx->query_tables_last,
4661         table_list->for_insert_data);
4662   }
4663   if (arena)
4664     thd->restore_active_arena(arena, &backup);
4665   DBUG_RETURN(FALSE);
4666 }
4667 
4668 /**
4669   Defines how prelocking algorithm for DML statements should handle table list
4670   elements:
4671   - If table has triggers we should add all tables and routines
4672     used by them to the prelocking set.
4673 
4674   We do not need to acquire metadata locks on trigger names
4675   in DML statements, since all DDL statements
4676   that change trigger metadata always lock their
4677   subject tables.
4678 
4679   @param[in]  thd              Thread context.
4680   @param[in]  prelocking_ctx   Prelocking context of the statement.
4681   @param[in]  table_list       Table list element for table.
4682   @param[in]  sp               Routine body.
4683   @param[out] need_prelocking  Set to TRUE if method detects that prelocking
4684                                required, not changed otherwise.
4685 
4686   @retval FALSE  Success.
4687   @retval TRUE   Failure (OOM).
4688 */
4689 
4690 bool DML_prelocking_strategy::
handle_table(THD * thd,Query_tables_list * prelocking_ctx,TABLE_LIST * table_list,bool * need_prelocking)4691 handle_table(THD *thd, Query_tables_list *prelocking_ctx,
4692              TABLE_LIST *table_list, bool *need_prelocking)
4693 {
4694   DBUG_ENTER("handle_table");
4695   TABLE *table= table_list->table;
4696   /* We rely on a caller to check that table is going to be changed. */
4697   DBUG_ASSERT(table_list->lock_type >= TL_WRITE_ALLOW_WRITE ||
4698               thd->lex->default_used);
4699 
4700   if (table_list->trg_event_map)
4701   {
4702     if (table->triggers)
4703     {
4704       *need_prelocking= TRUE;
4705 
4706       if (table->triggers->
4707           add_tables_and_routines_for_triggers(thd, prelocking_ctx, table_list))
4708         return TRUE;
4709     }
4710 
4711     if (table->file->referenced_by_foreign_key())
4712     {
4713       if (prepare_fk_prelocking_list(thd, prelocking_ctx, table_list,
4714                                      need_prelocking,
4715                                      table_list->trg_event_map))
4716         return TRUE;
4717     }
4718   }
4719   else if (table_list->slave_fk_event_map &&
4720            table->file->referenced_by_foreign_key())
4721   {
4722     if (prepare_fk_prelocking_list(thd, prelocking_ctx, table_list,
4723                                    need_prelocking,
4724                                    table_list->slave_fk_event_map))
4725       return TRUE;
4726   }
4727 
4728   /* Open any tables used by DEFAULT (like sequence tables) */
4729   DBUG_PRINT("info", ("table: %p  name: %s  db: %s  flags: %u",
4730                       table_list, table_list->table_name.str,
4731                       table_list->db.str, table_list->for_insert_data));
4732   if (table->internal_tables &&
4733       (table_list->for_insert_data ||
4734        thd->lex->default_used))
4735   {
4736     Query_arena *arena, backup;
4737     bool error;
4738     arena= thd->activate_stmt_arena_if_needed(&backup);
4739     error= add_internal_tables(thd, prelocking_ctx,
4740                                table->internal_tables);
4741     if (arena)
4742       thd->restore_active_arena(arena, &backup);
4743     if (unlikely(error))
4744     {
4745       *need_prelocking= TRUE;
4746       DBUG_RETURN(TRUE);
4747     }
4748   }
4749   DBUG_RETURN(FALSE);
4750 }
4751 
4752 
4753 /**
4754   Open all tables used by DEFAULT functions.
4755 
4756   This is different from normal open_and_lock_tables() as we may
4757   already have other tables opened and locked and we have to merge the
4758   new table with the old ones.
4759 */
4760 
open_and_lock_internal_tables(TABLE * table,bool lock_table)4761 bool open_and_lock_internal_tables(TABLE *table, bool lock_table)
4762 {
4763   THD *thd= table->in_use;
4764   TABLE_LIST *tl;
4765   MYSQL_LOCK *save_lock,*new_lock;
4766   DBUG_ENTER("open_and_lock_internal_tables");
4767 
4768   /* remove pointer to old select_lex which is already destroyed */
4769   for (tl= table->internal_tables ; tl ; tl= tl->next_global)
4770     tl->select_lex= 0;
4771 
4772   uint counter;
4773   MDL_savepoint mdl_savepoint= thd->mdl_context.mdl_savepoint();
4774   TABLE_LIST *tmp= table->internal_tables;
4775   DML_prelocking_strategy prelocking_strategy;
4776 
4777   if (open_tables(thd, thd->lex->create_info, &tmp, &counter, 0,
4778                   &prelocking_strategy))
4779     goto err;
4780 
4781   if (lock_table)
4782   {
4783     save_lock= thd->lock;
4784     thd->lock= 0;
4785     if (lock_tables(thd, table->internal_tables, counter,
4786                     MYSQL_LOCK_USE_MALLOC))
4787       goto err;
4788 
4789     if (!(new_lock= mysql_lock_merge(save_lock, thd->lock)))
4790     {
4791       thd->lock= save_lock;
4792       mysql_unlock_tables(thd, save_lock, 1);
4793       /* We don't have to close tables as caller will do that */
4794       goto err;
4795     }
4796     thd->lock= new_lock;
4797   }
4798   DBUG_RETURN(0);
4799 
4800 err:
4801   thd->mdl_context.rollback_to_savepoint(mdl_savepoint);
4802   DBUG_RETURN(1);
4803 }
4804 
4805 
4806 /**
4807   Defines how prelocking algorithm for DML statements should handle view -
4808   all view routines should be added to the prelocking set.
4809 
4810   @param[in]  thd              Thread context.
4811   @param[in]  prelocking_ctx   Prelocking context of the statement.
4812   @param[in]  table_list       Table list element for view.
4813   @param[in]  sp               Routine body.
4814   @param[out] need_prelocking  Set to TRUE if method detects that prelocking
4815                                required, not changed otherwise.
4816 
4817   @retval FALSE  Success.
4818   @retval TRUE   Failure (OOM).
4819 */
4820 
4821 bool DML_prelocking_strategy::
handle_view(THD * thd,Query_tables_list * prelocking_ctx,TABLE_LIST * table_list,bool * need_prelocking)4822 handle_view(THD *thd, Query_tables_list *prelocking_ctx,
4823             TABLE_LIST *table_list, bool *need_prelocking)
4824 {
4825   if (table_list->view->uses_stored_routines())
4826   {
4827     *need_prelocking= TRUE;
4828 
4829     sp_update_stmt_used_routines(thd, prelocking_ctx,
4830                                  &table_list->view->sroutines_list,
4831                                  table_list->top_table());
4832   }
4833 
4834   /*
4835     If a trigger was defined on one of the associated tables then assign the
4836     'trg_event_map' value of the view to the next table in table_list. When a
4837     Stored function is invoked, all the associated tables including the tables
4838     associated with the trigger are prelocked.
4839   */
4840   if (table_list->trg_event_map && table_list->next_global)
4841     table_list->next_global->trg_event_map= table_list->trg_event_map;
4842   return FALSE;
4843 }
4844 
4845 
4846 /**
4847   Defines how prelocking algorithm for LOCK TABLES statement should handle
4848   table list elements.
4849 
4850   @param[in]  thd              Thread context.
4851   @param[in]  prelocking_ctx   Prelocking context of the statement.
4852   @param[in]  table_list       Table list element for table.
4853   @param[in]  sp               Routine body.
4854   @param[out] need_prelocking  Set to TRUE if method detects that prelocking
4855                                required, not changed otherwise.
4856 
4857   @retval FALSE  Success.
4858   @retval TRUE   Failure (OOM).
4859 */
4860 
4861 bool Lock_tables_prelocking_strategy::
handle_table(THD * thd,Query_tables_list * prelocking_ctx,TABLE_LIST * table_list,bool * need_prelocking)4862 handle_table(THD *thd, Query_tables_list *prelocking_ctx,
4863              TABLE_LIST *table_list, bool *need_prelocking)
4864 {
4865   TABLE_LIST **last= prelocking_ctx->query_tables_last;
4866 
4867   if (DML_prelocking_strategy::handle_table(thd, prelocking_ctx, table_list,
4868                                             need_prelocking))
4869     return TRUE;
4870 
4871   /*
4872     normally we don't need to open FK-prelocked tables for RESTRICT,
4873     MDL is enough. But under LOCK TABLES we have to open everything
4874   */
4875   for (TABLE_LIST *tl= *last; tl; tl= tl->next_global)
4876     tl->open_strategy= TABLE_LIST::OPEN_NORMAL;
4877 
4878   /* We rely on a caller to check that table is going to be changed. */
4879   DBUG_ASSERT(table_list->lock_type >= TL_WRITE_ALLOW_WRITE);
4880 
4881   return FALSE;
4882 }
4883 
4884 
4885 /**
4886   Defines how prelocking algorithm for ALTER TABLE statement should handle
4887   routines - do nothing as this statement is not supposed to call routines.
4888 
4889   We still can end up in this method when someone tries
4890   to define a foreign key referencing a view, and not just
4891   a simple view, but one that uses stored routines.
4892 */
4893 
4894 bool Alter_table_prelocking_strategy::
handle_routine(THD * thd,Query_tables_list * prelocking_ctx,Sroutine_hash_entry * rt,sp_head * sp,bool * need_prelocking)4895 handle_routine(THD *thd, Query_tables_list *prelocking_ctx,
4896                Sroutine_hash_entry *rt, sp_head *sp, bool *need_prelocking)
4897 {
4898   return FALSE;
4899 }
4900 
4901 
4902 /**
4903   Defines how prelocking algorithm for ALTER TABLE statement should handle
4904   table list elements.
4905 
4906   Unlike in DML, we do not process triggers here.
4907 
4908   @param[in]  thd              Thread context.
4909   @param[in]  prelocking_ctx   Prelocking context of the statement.
4910   @param[in]  table_list       Table list element for table.
4911   @param[in]  sp               Routine body.
4912   @param[out] need_prelocking  Set to TRUE if method detects that prelocking
4913                                required, not changed otherwise.
4914 
4915 
4916   @retval FALSE  Success.
4917   @retval TRUE   Failure (OOM).
4918 */
4919 
4920 bool Alter_table_prelocking_strategy::
handle_table(THD * thd,Query_tables_list * prelocking_ctx,TABLE_LIST * table_list,bool * need_prelocking)4921 handle_table(THD *thd, Query_tables_list *prelocking_ctx,
4922              TABLE_LIST *table_list, bool *need_prelocking)
4923 {
4924   return FALSE;
4925 }
4926 
4927 
4928 /**
4929   Defines how prelocking algorithm for ALTER TABLE statement
4930   should handle view - do nothing. We don't need to add view
4931   routines to the prelocking set in this case as view is not going
4932   to be materialized.
4933 */
4934 
4935 bool Alter_table_prelocking_strategy::
handle_view(THD * thd,Query_tables_list * prelocking_ctx,TABLE_LIST * table_list,bool * need_prelocking)4936 handle_view(THD *thd, Query_tables_list *prelocking_ctx,
4937             TABLE_LIST *table_list, bool *need_prelocking)
4938 {
4939   return FALSE;
4940 }
4941 
4942 
4943 /**
4944   Check that lock is ok for tables; Call start stmt if ok
4945 
4946   @param thd             Thread handle.
4947   @param prelocking_ctx  Prelocking context.
4948   @param table_list      Table list element for table to be checked.
4949 
4950   @retval FALSE - Ok.
4951   @retval TRUE  - Error.
4952 */
4953 
check_lock_and_start_stmt(THD * thd,Query_tables_list * prelocking_ctx,TABLE_LIST * table_list)4954 static bool check_lock_and_start_stmt(THD *thd,
4955                                       Query_tables_list *prelocking_ctx,
4956                                       TABLE_LIST *table_list)
4957 {
4958   int error;
4959   thr_lock_type lock_type;
4960   DBUG_ENTER("check_lock_and_start_stmt");
4961 
4962   /*
4963     Prelocking placeholder is not set for TABLE_LIST that
4964     are directly used by TOP level statement.
4965   */
4966   DBUG_ASSERT(table_list->prelocking_placeholder == TABLE_LIST::PRELOCK_NONE);
4967 
4968   /*
4969     TL_WRITE_DEFAULT and TL_READ_DEFAULT are supposed to be parser only
4970     types of locks so they should be converted to appropriate other types
4971     to be passed to storage engine. The exact lock type passed to the
4972     engine is important as, for example, InnoDB uses it to determine
4973     what kind of row locks should be acquired when executing statement
4974     in prelocked mode or under LOCK TABLES with @@innodb_table_locks = 0.
4975 
4976     Last argument routine_modifies_data for read_lock_type_for_table()
4977     is ignored, as prelocking placeholder will never be set here.
4978   */
4979   if (table_list->lock_type == TL_WRITE_DEFAULT)
4980     lock_type= thd->update_lock_default;
4981   else if (table_list->lock_type == TL_READ_DEFAULT)
4982     lock_type= read_lock_type_for_table(thd, prelocking_ctx, table_list, true);
4983   else
4984     lock_type= table_list->lock_type;
4985 
4986   if ((int) lock_type >= (int) TL_WRITE_ALLOW_WRITE &&
4987       (int) table_list->table->reginfo.lock_type < (int) TL_WRITE_ALLOW_WRITE)
4988   {
4989     my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0),
4990              table_list->table->alias.c_ptr());
4991     DBUG_RETURN(1);
4992   }
4993   if (unlikely((error= table_list->table->file->start_stmt(thd, lock_type))))
4994   {
4995     table_list->table->file->print_error(error, MYF(0));
4996     DBUG_RETURN(1);
4997   }
4998 
4999   /*
5000     Record in transaction state tracking
5001   */
5002   TRANSACT_TRACKER(add_trx_state(thd, lock_type,
5003                                  table_list->table->file->has_transactions()));
5004 
5005   DBUG_RETURN(0);
5006 }
5007 
5008 
5009 /**
5010   @brief Open and lock one table
5011 
5012   @param[in]    thd             thread handle
5013   @param[in]    table_l         table to open is first table in this list
5014   @param[in]    lock_type       lock to use for table
5015   @param[in]    flags           options to be used while opening and locking
5016                                 table (see open_table(), mysql_lock_tables())
5017   @param[in]    prelocking_strategy  Strategy which specifies how prelocking
5018                                      algorithm should work for this statement.
5019 
5020   @return       table
5021     @retval     != NULL         OK, opened table returned
5022     @retval     NULL            Error
5023 
5024   @note
5025     If ok, the following are also set:
5026       table_list->lock_type 	lock_type
5027       table_list->table		table
5028 
5029   @note
5030     If table_l is a list, not a single table, the list is temporarily
5031     broken.
5032 
5033   @detail
5034     This function is meant as a replacement for open_ltable() when
5035     MERGE tables can be opened. open_ltable() cannot open MERGE tables.
5036 
5037     There may be more differences between open_n_lock_single_table() and
5038     open_ltable(). One known difference is that open_ltable() does
5039     neither call thd->decide_logging_format() nor handle some other logging
5040     and locking issues because it does not call lock_tables().
5041 */
5042 
open_n_lock_single_table(THD * thd,TABLE_LIST * table_l,thr_lock_type lock_type,uint flags,Prelocking_strategy * prelocking_strategy)5043 TABLE *open_n_lock_single_table(THD *thd, TABLE_LIST *table_l,
5044                                 thr_lock_type lock_type, uint flags,
5045                                 Prelocking_strategy *prelocking_strategy)
5046 {
5047   TABLE_LIST *save_next_global;
5048   DBUG_ENTER("open_n_lock_single_table");
5049 
5050   /* Remember old 'next' pointer. */
5051   save_next_global= table_l->next_global;
5052   /* Break list. */
5053   table_l->next_global= NULL;
5054 
5055   /* Set requested lock type. */
5056   table_l->lock_type= lock_type;
5057   /* Allow to open real tables only. */
5058   table_l->required_type= TABLE_TYPE_NORMAL;
5059 
5060   /* Open the table. */
5061   if (open_and_lock_tables(thd, table_l, FALSE, flags,
5062                            prelocking_strategy))
5063     table_l->table= NULL; /* Just to be sure. */
5064 
5065   /* Restore list. */
5066   table_l->next_global= save_next_global;
5067 
5068   DBUG_RETURN(table_l->table);
5069 }
5070 
5071 
5072 /*
5073   Open and lock one table
5074 
5075   SYNOPSIS
5076     open_ltable()
5077     thd			Thread handler
5078     table_list		Table to open is first table in this list
5079     lock_type		Lock to use for open
5080     lock_flags          Flags passed to mysql_lock_table
5081 
5082   NOTE
5083     This function doesn't do anything like SP/SF/views/triggers analysis done
5084     in open_table()/lock_tables(). It is intended for opening of only one
5085     concrete table. And used only in special contexts.
5086 
5087   RETURN VALUES
5088     table		Opened table
5089     0			Error
5090 
5091     If ok, the following are also set:
5092       table_list->lock_type 	lock_type
5093       table_list->table		table
5094 */
5095 
open_ltable(THD * thd,TABLE_LIST * table_list,thr_lock_type lock_type,uint lock_flags)5096 TABLE *open_ltable(THD *thd, TABLE_LIST *table_list, thr_lock_type lock_type,
5097                    uint lock_flags)
5098 {
5099   TABLE *table;
5100   Open_table_context ot_ctx(thd, lock_flags);
5101   bool error;
5102   DBUG_ENTER("open_ltable");
5103 
5104   /* Ignore temporary tables as they have already been opened. */
5105   if (table_list->table)
5106     DBUG_RETURN(table_list->table);
5107 
5108   /* should not be used in a prelocked_mode context, see NOTE above */
5109   DBUG_ASSERT(thd->locked_tables_mode < LTM_PRELOCKED);
5110 
5111   THD_STAGE_INFO(thd, stage_opening_tables);
5112   thd->current_tablenr= 0;
5113   /* open_ltable can be used only for BASIC TABLEs */
5114   table_list->required_type= TABLE_TYPE_NORMAL;
5115 
5116   /* This function can't properly handle requests for such metadata locks. */
5117   DBUG_ASSERT(table_list->mdl_request.type < MDL_SHARED_UPGRADABLE);
5118 
5119   while ((error= open_table(thd, table_list, &ot_ctx)) &&
5120          ot_ctx.can_recover_from_failed_open())
5121   {
5122     /*
5123       Even though we have failed to open table we still need to
5124       call release_transactional_locks() to release metadata locks which
5125       might have been acquired successfully.
5126     */
5127     thd->mdl_context.rollback_to_savepoint(ot_ctx.start_of_statement_svp());
5128     table_list->mdl_request.ticket= 0;
5129     if (ot_ctx.recover_from_failed_open())
5130       break;
5131   }
5132 
5133   if (likely(!error))
5134   {
5135     /*
5136       We can't have a view or some special "open_strategy" in this function
5137       so there should be a TABLE instance.
5138     */
5139     DBUG_ASSERT(table_list->table);
5140     table= table_list->table;
5141     if (table->file->ha_table_flags() & HA_CAN_MULTISTEP_MERGE)
5142     {
5143       /* A MERGE table must not come here. */
5144       /* purecov: begin tested */
5145       my_error(ER_WRONG_OBJECT, MYF(0), table->s->db.str,
5146                table->s->table_name.str, "BASE TABLE");
5147       table= 0;
5148       goto end;
5149       /* purecov: end */
5150     }
5151 
5152     table_list->lock_type= lock_type;
5153     table->grant= table_list->grant;
5154     if (thd->locked_tables_mode)
5155     {
5156       if (check_lock_and_start_stmt(thd, thd->lex, table_list))
5157 	table= 0;
5158     }
5159     else
5160     {
5161       DBUG_ASSERT(thd->lock == 0);	// You must lock everything at once
5162       if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
5163 	if (! (thd->lock= mysql_lock_tables(thd, &table_list->table, 1,
5164                                             lock_flags)))
5165         {
5166           table= 0;
5167         }
5168     }
5169   }
5170   else
5171     table= 0;
5172 
5173 end:
5174   if (table == NULL)
5175   {
5176     if (!thd->in_sub_stmt)
5177       trans_rollback_stmt(thd);
5178     close_thread_tables(thd);
5179   }
5180   THD_STAGE_INFO(thd, stage_after_opening_tables);
5181 
5182   thd_proc_info(thd, 0);
5183   DBUG_RETURN(table);
5184 }
5185 
5186 
5187 /**
5188   Open all tables in list, locks them and optionally process derived tables.
5189 
5190   @param thd		      Thread context.
5191   @param options              DDL options.
5192   @param tables	              List of tables for open and locking.
5193   @param derived              Whether to handle derived tables.
5194   @param flags                Bitmap of options to be used to open and lock
5195                               tables (see open_tables() and mysql_lock_tables()
5196                               for details).
5197   @param prelocking_strategy  Strategy which specifies how prelocking algorithm
5198                               should work for this statement.
5199 
5200   @note
5201     The thr_lock locks will automatically be freed by
5202     close_thread_tables().
5203 
5204   @retval FALSE  OK.
5205   @retval TRUE   Error
5206 */
5207 
open_and_lock_tables(THD * thd,const DDL_options_st & options,TABLE_LIST * tables,bool derived,uint flags,Prelocking_strategy * prelocking_strategy)5208 bool open_and_lock_tables(THD *thd, const DDL_options_st &options,
5209                           TABLE_LIST *tables,
5210                           bool derived, uint flags,
5211                           Prelocking_strategy *prelocking_strategy)
5212 {
5213   uint counter;
5214   MDL_savepoint mdl_savepoint= thd->mdl_context.mdl_savepoint();
5215   DBUG_ENTER("open_and_lock_tables");
5216   DBUG_PRINT("enter", ("derived handling: %d", derived));
5217 
5218   if (open_tables(thd, options, &tables, &counter, flags, prelocking_strategy))
5219     goto err;
5220 
5221   DBUG_EXECUTE_IF("sleep_open_and_lock_after_open", {
5222                   const char *old_proc_info= thd->proc_info;
5223                   thd->proc_info= "DBUG sleep";
5224                   my_sleep(6000000);
5225                   thd->proc_info= old_proc_info;});
5226 
5227   if (lock_tables(thd, tables, counter, flags))
5228     goto err;
5229 
5230   /* Don't read statistics tables when opening internal tables */
5231   if (!(flags & MYSQL_OPEN_IGNORE_LOGGING_FORMAT))
5232     (void) read_statistics_for_tables_if_needed(thd, tables);
5233 
5234   if (derived)
5235   {
5236     if (mysql_handle_derived(thd->lex, DT_INIT))
5237       goto err;
5238     if (thd->prepare_derived_at_open &&
5239         (mysql_handle_derived(thd->lex, DT_PREPARE)))
5240       goto err;
5241   }
5242 
5243   DBUG_RETURN(FALSE);
5244 err:
5245   if (! thd->in_sub_stmt)
5246     trans_rollback_stmt(thd);  /* Necessary if derived handling failed. */
5247   close_thread_tables(thd);
5248   /* Don't keep locks for a failed statement. */
5249   thd->mdl_context.rollback_to_savepoint(mdl_savepoint);
5250   DBUG_RETURN(TRUE);
5251 }
5252 
5253 
5254 /*
5255   Open all tables in list and process derived tables
5256 
5257   SYNOPSIS
5258     open_normal_and_derived_tables
5259     thd		- thread handler
5260     tables	- list of tables for open
5261     flags       - bitmap of flags to modify how the tables will be open:
5262                   MYSQL_LOCK_IGNORE_FLUSH - open table even if someone has
5263                   done a flush on it.
5264     dt_phases   - set of flags to pass to the mysql_handle_derived
5265 
5266   RETURN
5267     FALSE - ok
5268     TRUE  - error
5269 
5270   NOTE
5271     This is to be used on prepare stage when you don't read any
5272     data from the tables.
5273 */
5274 
open_normal_and_derived_tables(THD * thd,TABLE_LIST * tables,uint flags,uint dt_phases)5275 bool open_normal_and_derived_tables(THD *thd, TABLE_LIST *tables, uint flags,
5276                                     uint dt_phases)
5277 {
5278   DML_prelocking_strategy prelocking_strategy;
5279   uint counter;
5280   MDL_savepoint mdl_savepoint= thd->mdl_context.mdl_savepoint();
5281   DBUG_ENTER("open_normal_and_derived_tables");
5282   if (open_tables(thd, &tables, &counter, flags, &prelocking_strategy) ||
5283       mysql_handle_derived(thd->lex, dt_phases))
5284     goto end;
5285 
5286   DBUG_RETURN(0);
5287 end:
5288   /*
5289     No need to commit/rollback the statement transaction: it's
5290     either not started or we're filling in an INFORMATION_SCHEMA
5291     table on the fly, and thus mustn't manipulate with the
5292     transaction of the enclosing statement.
5293   */
5294   DBUG_ASSERT(thd->transaction->stmt.is_empty() ||
5295               (thd->state_flags & Open_tables_state::BACKUPS_AVAIL));
5296   close_thread_tables(thd);
5297   /* Don't keep locks for a failed statement. */
5298   thd->mdl_context.rollback_to_savepoint(mdl_savepoint);
5299 
5300   DBUG_RETURN(TRUE); /* purecov: inspected */
5301 }
5302 
5303 
5304 /**
5305   Open a table to read its structure, e.g. for:
5306   - SHOW FIELDS
5307   - delayed SP variable data type definition: DECLARE a t1.a%TYPE
5308 
5309   The flag MYSQL_OPEN_GET_NEW_TABLE is passed to make %TYPE work
5310   in stored functions, as during a stored function call
5311   (e.g. in a SELECT query) the tables referenced in %TYPE can already be locked,
5312   and attempt to open it again would return an error in open_table().
5313 
5314   The flag MYSQL_OPEN_GET_NEW_TABLE is not really needed for
5315   SHOW FIELDS or for a "CALL sp()" statement, but it's not harmful,
5316   so let's pass it unconditionally.
5317 */
5318 
open_tables_only_view_structure(THD * thd,TABLE_LIST * table_list,bool can_deadlock)5319 bool open_tables_only_view_structure(THD *thd, TABLE_LIST *table_list,
5320                                      bool can_deadlock)
5321 {
5322   DBUG_ENTER("open_tables_only_view_structure");
5323   /*
5324     Let us set fake sql_command so views won't try to merge
5325     themselves into main statement. If we don't do this,
5326     SELECT * from information_schema.xxxx will cause problems.
5327     SQLCOM_SHOW_FIELDS is used because it satisfies
5328     'LEX::only_view_structure()'.
5329   */
5330   enum_sql_command save_sql_command= thd->lex->sql_command;
5331   thd->lex->sql_command= SQLCOM_SHOW_FIELDS;
5332   bool rc= (thd->open_temporary_tables(table_list) ||
5333            open_normal_and_derived_tables(thd, table_list,
5334                                           (MYSQL_OPEN_IGNORE_FLUSH |
5335                                            MYSQL_OPEN_FORCE_SHARED_HIGH_PRIO_MDL |
5336                                            MYSQL_OPEN_GET_NEW_TABLE |
5337                                            (can_deadlock ?
5338                                             MYSQL_OPEN_FAIL_ON_MDL_CONFLICT : 0)),
5339                                           DT_INIT | DT_PREPARE));
5340   /*
5341     Restore old value of sql_command back as it is being looked at in
5342     process_table() function.
5343   */
5344   thd->lex->sql_command= save_sql_command;
5345   DBUG_RETURN(rc);
5346 }
5347 
5348 
5349 /*
5350   Mark all real tables in the list as free for reuse.
5351 
5352   SYNOPSIS
5353     mark_real_tables_as_free_for_reuse()
5354       thd   - thread context
5355       table - head of the list of tables
5356 
5357   DESCRIPTION
5358     Marks all real tables in the list (i.e. not views, derived
5359     or schema tables) as free for reuse.
5360 */
5361 
mark_real_tables_as_free_for_reuse(TABLE_LIST * table_list)5362 static void mark_real_tables_as_free_for_reuse(TABLE_LIST *table_list)
5363 {
5364   TABLE_LIST *table;
5365   DBUG_ENTER("mark_real_tables_as_free_for_reuse");
5366 
5367   /*
5368     We have to make two loops as HA_EXTRA_DETACH_CHILDREN may
5369     remove items from the table list that we have to reset
5370   */
5371   for (table= table_list; table; table= table->next_global)
5372   {
5373     if (!table->placeholder())
5374       table->table->query_id= 0;
5375   }
5376   for (table= table_list; table; table= table->next_global)
5377   {
5378     if (!table->placeholder())
5379     {
5380       /*
5381         Detach children of MyISAMMRG tables used in
5382         sub-statements, they will be reattached at open.
5383         This has to be done in a separate loop to make sure
5384         that children have had their query_id cleared.
5385       */
5386       table->table->file->extra(HA_EXTRA_DETACH_CHILDREN);
5387     }
5388   }
5389   DBUG_VOID_RETURN;
5390 }
5391 
fix_vcol_exprs(THD * thd)5392 int TABLE::fix_vcol_exprs(THD *thd)
5393 {
5394   for (Field **vf= vfield; vf && *vf; vf++)
5395     if (fix_session_vcol_expr(thd, (*vf)->vcol_info))
5396       return 1;
5397 
5398   for (Field **df= default_field; df && *df; df++)
5399     if ((*df)->default_value &&
5400         fix_session_vcol_expr(thd, (*df)->default_value))
5401       return 1;
5402 
5403   for (Virtual_column_info **cc= check_constraints; cc && *cc; cc++)
5404     if (fix_session_vcol_expr(thd, (*cc)))
5405       return 1;
5406 
5407   return 0;
5408 }
5409 
5410 
fix_all_session_vcol_exprs(THD * thd,TABLE_LIST * tables)5411 static bool fix_all_session_vcol_exprs(THD *thd, TABLE_LIST *tables)
5412 {
5413   Security_context *save_security_ctx= thd->security_ctx;
5414   TABLE_LIST *first_not_own= thd->lex->first_not_own_table();
5415   DBUG_ENTER("fix_session_vcol_expr");
5416 
5417   int error= 0;
5418   for (TABLE_LIST *table= tables; table && table != first_not_own && !error;
5419        table= table->next_global)
5420   {
5421     TABLE *t= table->table;
5422     if (!table->placeholder() && t->s->vcols_need_refixing &&
5423          table->lock_type >= TL_WRITE_ALLOW_WRITE)
5424     {
5425       Query_arena *stmt_backup= thd->stmt_arena;
5426       if (thd->stmt_arena->is_conventional())
5427         thd->stmt_arena= t->expr_arena;
5428       if (table->security_ctx)
5429         thd->security_ctx= table->security_ctx;
5430 
5431       error= t->fix_vcol_exprs(thd);
5432 
5433       thd->security_ctx= save_security_ctx;
5434       thd->stmt_arena= stmt_backup;
5435     }
5436   }
5437   DBUG_RETURN(error);
5438 }
5439 
5440 
5441 /**
5442   Lock all tables in a list.
5443 
5444   @param  thd           Thread handler
5445   @param  tables        Tables to lock
5446   @param  count         Number of opened tables
5447   @param  flags         Options (see mysql_lock_tables() for details)
5448 
5449   You can't call lock_tables() while holding thr_lock locks, as
5450   this would break the dead-lock-free handling thr_lock gives us.
5451   You must always get all needed locks at once.
5452 
5453   If the query for which we are calling this function is marked as
5454   requiring prelocking, this function will change
5455   locked_tables_mode to LTM_PRELOCKED.
5456 
5457   @retval FALSE         Success.
5458   @retval TRUE          A lock wait timeout, deadlock or out of memory.
5459 */
5460 
lock_tables(THD * thd,TABLE_LIST * tables,uint count,uint flags)5461 bool lock_tables(THD *thd, TABLE_LIST *tables, uint count, uint flags)
5462 {
5463   TABLE_LIST *table, *first_not_own;
5464   DBUG_ENTER("lock_tables");
5465   /*
5466     We can't meet statement requiring prelocking if we already
5467     in prelocked mode.
5468   */
5469   DBUG_ASSERT(thd->locked_tables_mode <= LTM_LOCK_TABLES ||
5470               !thd->lex->requires_prelocking());
5471 
5472   if (!tables && !thd->lex->requires_prelocking())
5473     DBUG_RETURN(0);
5474 
5475   first_not_own= thd->lex->first_not_own_table();
5476 
5477   /*
5478     Check for thd->locked_tables_mode to avoid a redundant
5479     and harmful attempt to lock the already locked tables again.
5480     Checking for thd->lock is not enough in some situations. For example,
5481     if a stored function contains
5482     "drop table t3; create temporary t3 ..; insert into t3 ...;"
5483     thd->lock may be 0 after drop tables, whereas locked_tables_mode
5484     is still on. In this situation an attempt to lock temporary
5485     table t3 will lead to a memory leak.
5486   */
5487   if (! thd->locked_tables_mode)
5488   {
5489     DBUG_ASSERT(thd->lock == 0);	// You must lock everything at once
5490     TABLE **start,**ptr;
5491     bool found_first_not_own= 0;
5492 
5493     if (!(ptr=start=(TABLE**) thd->alloc(sizeof(TABLE*)*count)))
5494       DBUG_RETURN(TRUE);
5495 
5496     /*
5497       Collect changes tables for table lock.
5498       Mark own tables with query id as this is needed by
5499       prepare_for_row_logging()
5500     */
5501     for (table= tables; table; table= table->next_global)
5502     {
5503       if (table == first_not_own)
5504         found_first_not_own= 1;
5505       if (!table->placeholder())
5506       {
5507         *(ptr++)= table->table;
5508         if (!found_first_not_own)
5509           table->table->query_id= thd->query_id;
5510       }
5511     }
5512 
5513     DEBUG_SYNC(thd, "before_lock_tables_takes_lock");
5514 
5515     if (! (thd->lock= mysql_lock_tables(thd, start, (uint) (ptr - start),
5516                                         flags)))
5517       DBUG_RETURN(TRUE);
5518 
5519     DEBUG_SYNC(thd, "after_lock_tables_takes_lock");
5520 
5521     if (thd->lex->requires_prelocking() &&
5522         thd->lex->sql_command != SQLCOM_LOCK_TABLES)
5523     {
5524       /*
5525         We just have done implicit LOCK TABLES, and now we have
5526         to emulate first open_and_lock_tables() after it.
5527 
5528         When open_and_lock_tables() is called for a single table out of
5529         a table list, the 'next_global' chain is temporarily broken. We
5530         may not find 'first_not_own' before the end of the "list".
5531         Look for example at those places where open_n_lock_single_table()
5532         is called. That function implements the temporary breaking of
5533         a table list for opening a single table.
5534       */
5535       for (table= tables;
5536            table && table != first_not_own;
5537            table= table->next_global)
5538       {
5539         if (!table->placeholder())
5540         {
5541           if (check_lock_and_start_stmt(thd, thd->lex, table))
5542           {
5543             mysql_unlock_tables(thd, thd->lock);
5544             thd->lock= 0;
5545             DBUG_RETURN(TRUE);
5546           }
5547         }
5548       }
5549       /*
5550         Let us mark all tables which don't belong to the statement itself,
5551         and was marked as occupied during open_tables() as free for reuse.
5552       */
5553       mark_real_tables_as_free_for_reuse(first_not_own);
5554       DBUG_PRINT("info",("locked_tables_mode= LTM_PRELOCKED"));
5555       thd->enter_locked_tables_mode(LTM_PRELOCKED);
5556     }
5557   }
5558   else
5559   {
5560     /*
5561       When open_and_lock_tables() is called for a single table out of
5562       a table list, the 'next_global' chain is temporarily broken. We
5563       may not find 'first_not_own' before the end of the "list".
5564       Look for example at those places where open_n_lock_single_table()
5565       is called. That function implements the temporary breaking of
5566       a table list for opening a single table.
5567     */
5568     for (table= tables;
5569          table && table != first_not_own;
5570          table= table->next_global)
5571     {
5572       if (table->placeholder())
5573         continue;
5574 
5575       table->table->query_id= thd->query_id;
5576       /*
5577         In a stored function or trigger we should ensure that we won't change
5578         a table that is already used by the calling statement.
5579       */
5580       if (thd->locked_tables_mode >= LTM_PRELOCKED &&
5581           table->lock_type >= TL_WRITE_ALLOW_WRITE)
5582       {
5583         for (TABLE* opentab= thd->open_tables; opentab; opentab= opentab->next)
5584         {
5585           if (table->table->s == opentab->s && opentab->query_id &&
5586               table->table->query_id != opentab->query_id)
5587           {
5588             my_error(ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG, MYF(0),
5589                      table->table->s->table_name.str);
5590             DBUG_RETURN(TRUE);
5591           }
5592         }
5593       }
5594 
5595       if (check_lock_and_start_stmt(thd, thd->lex, table))
5596       {
5597 	DBUG_RETURN(TRUE);
5598       }
5599     }
5600     /*
5601       If we are under explicit LOCK TABLES and our statement requires
5602       prelocking, we should mark all "additional" tables as free for use
5603       and enter prelocked mode.
5604     */
5605     if (thd->lex->requires_prelocking())
5606     {
5607       mark_real_tables_as_free_for_reuse(first_not_own);
5608       DBUG_PRINT("info",
5609                  ("thd->locked_tables_mode= LTM_PRELOCKED_UNDER_LOCK_TABLES"));
5610       thd->locked_tables_mode= LTM_PRELOCKED_UNDER_LOCK_TABLES;
5611     }
5612   }
5613 
5614   bool res= fix_all_session_vcol_exprs(thd, tables);
5615   if (!res && !(flags & MYSQL_OPEN_IGNORE_LOGGING_FORMAT))
5616     res= thd->decide_logging_format(tables);
5617 
5618   DBUG_RETURN(res);
5619 }
5620 
5621 
5622 /*
5623   Restart transaction for tables
5624 
5625   This is used when we had to do an implicit commit after tables are opened
5626   and want to restart transactions on tables.
5627 
5628   This is used in case of:
5629   LOCK TABLES xx
5630   CREATE OR REPLACE TABLE xx;
5631 */
5632 
restart_trans_for_tables(THD * thd,TABLE_LIST * table)5633 bool restart_trans_for_tables(THD *thd, TABLE_LIST *table)
5634 {
5635   DBUG_ENTER("restart_trans_for_tables");
5636 
5637   for (; table; table= table->next_global)
5638   {
5639     if (table->placeholder())
5640       continue;
5641 
5642     if (check_lock_and_start_stmt(thd, thd->lex, table))
5643     {
5644       DBUG_ASSERT(0);                           // Should never happen
5645       DBUG_RETURN(TRUE);
5646     }
5647   }
5648   DBUG_RETURN(FALSE);
5649 }
5650 
5651 
5652 /**
5653   Prepare statement for reopening of tables and recalculation of set of
5654   prelocked tables.
5655 
5656   @param[in] thd         Thread context.
5657   @param[in,out] tables  List of tables which we were trying to open
5658                          and lock.
5659   @param[in] start_of_statement_svp MDL savepoint which represents the set
5660                          of metadata locks which the current transaction
5661                          managed to acquire before execution of the current
5662                          statement and to which we should revert before
5663                          trying to reopen tables. NULL if no metadata locks
5664                          were held and thus all metadata locks should be
5665                          released.
5666 */
5667 
close_tables_for_reopen(THD * thd,TABLE_LIST ** tables,const MDL_savepoint & start_of_statement_svp)5668 void close_tables_for_reopen(THD *thd, TABLE_LIST **tables,
5669                              const MDL_savepoint &start_of_statement_svp)
5670 {
5671   TABLE_LIST *first_not_own_table= thd->lex->first_not_own_table();
5672   TABLE_LIST *tmp;
5673 
5674   /*
5675     If table list consists only from tables from prelocking set, table list
5676     for new attempt should be empty, so we have to update list's root pointer.
5677   */
5678   if (first_not_own_table == *tables)
5679     *tables= 0;
5680   thd->lex->chop_off_not_own_tables();
5681   /* Reset MDL tickets for procedures/functions */
5682   for (Sroutine_hash_entry *rt=
5683          (Sroutine_hash_entry*)thd->lex->sroutines_list.first;
5684        rt; rt= rt->next)
5685     rt->mdl_request.ticket= NULL;
5686   sp_remove_not_own_routines(thd->lex);
5687   for (tmp= *tables; tmp; tmp= tmp->next_global)
5688   {
5689     tmp->table= 0;
5690     tmp->mdl_request.ticket= NULL;
5691     /* We have to cleanup translation tables of views. */
5692     tmp->cleanup_items();
5693   }
5694   /*
5695     No need to commit/rollback the statement transaction: it's
5696     either not started or we're filling in an INFORMATION_SCHEMA
5697     table on the fly, and thus mustn't manipulate with the
5698     transaction of the enclosing statement.
5699   */
5700   DBUG_ASSERT(thd->transaction->stmt.is_empty() ||
5701               (thd->state_flags & Open_tables_state::BACKUPS_AVAIL));
5702   close_thread_tables(thd);
5703   thd->mdl_context.rollback_to_savepoint(start_of_statement_svp);
5704 }
5705 
5706 
5707 /*****************************************************************************
5708 * The following find_field_in_XXX procedures implement the core of the
5709 * name resolution functionality. The entry point to resolve a column name in a
5710 * list of tables is 'find_field_in_tables'. It calls 'find_field_in_table_ref'
5711 * for each table reference. In turn, depending on the type of table reference,
5712 * 'find_field_in_table_ref' calls one of the 'find_field_in_XXX' procedures
5713 * below specific for the type of table reference.
5714 ******************************************************************************/
5715 
5716 /* Special Field pointers as return values of find_field_in_XXX functions. */
5717 Field *not_found_field= (Field*) 0x1;
5718 Field *view_ref_found= (Field*) 0x2;
5719 
5720 #define WRONG_GRANT (Field*) -1
5721 
update_field_dependencies(THD * thd,Field * field,TABLE * table)5722 static void update_field_dependencies(THD *thd, Field *field, TABLE *table)
5723 {
5724   DBUG_ENTER("update_field_dependencies");
5725   if (should_mark_column(thd->column_usage))
5726   {
5727     /*
5728       We always want to register the used keys, as the column bitmap may have
5729       been set for all fields (for example for view).
5730     */
5731     table->covering_keys.intersect(field->part_of_key);
5732 
5733     if (thd->column_usage == MARK_COLUMNS_READ)
5734     {
5735       if (table->mark_column_with_deps(field))
5736         DBUG_VOID_RETURN; // Field was already marked
5737     }
5738     else
5739     {
5740       if (bitmap_fast_test_and_set(table->write_set, field->field_index))
5741       {
5742         DBUG_PRINT("warning", ("Found duplicated field"));
5743         thd->dup_field= field;
5744         DBUG_VOID_RETURN;
5745       }
5746     }
5747 
5748     table->used_fields++;
5749   }
5750   if (table->get_fields_in_item_tree)
5751     field->flags|= GET_FIXED_FIELDS_FLAG;
5752   DBUG_VOID_RETURN;
5753 }
5754 
5755 
5756 /*
5757   Find a field by name in a view that uses merge algorithm.
5758 
5759   SYNOPSIS
5760     find_field_in_view()
5761     thd				thread handler
5762     table_list			view to search for 'name'
5763     name			name of field
5764     length			length of name
5765     item_name                   name of item if it will be created (VIEW)
5766     ref				expression substituted in VIEW should be passed
5767                                 using this reference (return view_ref_found)
5768     register_tree_change        TRUE if ref is not stack variable and we
5769                                 need register changes in item tree
5770 
5771   RETURN
5772     0			field is not found
5773     view_ref_found	found value in VIEW (real result is in *ref)
5774     #			pointer to field - only for schema table fields
5775 */
5776 
5777 static Field *
find_field_in_view(THD * thd,TABLE_LIST * table_list,const char * name,size_t length,const char * item_name,Item ** ref,bool register_tree_change)5778 find_field_in_view(THD *thd, TABLE_LIST *table_list,
5779                    const char *name, size_t length,
5780                    const char *item_name, Item **ref,
5781                    bool register_tree_change)
5782 {
5783   DBUG_ENTER("find_field_in_view");
5784   DBUG_PRINT("enter",
5785              ("view: '%s', field name: '%s', item name: '%s', ref %p",
5786               table_list->alias.str, name, item_name, ref));
5787   Field_iterator_view field_it;
5788   field_it.set(table_list);
5789   Query_arena *arena= 0, backup;
5790 
5791   for (; !field_it.end_of_fields(); field_it.next())
5792   {
5793     if (!my_strcasecmp(system_charset_info, field_it.name()->str, name))
5794     {
5795       // in PS use own arena or data will be freed after prepare
5796       if (register_tree_change &&
5797           thd->stmt_arena->is_stmt_prepare_or_first_stmt_execute())
5798         arena= thd->activate_stmt_arena_if_needed(&backup);
5799       /*
5800         create_item() may, or may not create a new Item, depending on
5801         the column reference. See create_view_field() for details.
5802       */
5803       Item *item= field_it.create_item(thd);
5804       if (arena)
5805         thd->restore_active_arena(arena, &backup);
5806 
5807       if (!item)
5808         DBUG_RETURN(0);
5809       if (!ref)
5810         DBUG_RETURN((Field*) view_ref_found);
5811       /*
5812        *ref != NULL means that *ref contains the item that we need to
5813        replace. If the item was aliased by the user, set the alias to
5814        the replacing item.
5815       */
5816       if (*ref && !(*ref)->is_autogenerated_name())
5817         item->set_name(thd, (*ref)->name);
5818       if (register_tree_change)
5819         thd->change_item_tree(ref, item);
5820       else
5821         *ref= item;
5822       DBUG_RETURN((Field*) view_ref_found);
5823     }
5824   }
5825   DBUG_RETURN(0);
5826 }
5827 
5828 
5829 /*
5830   Find field by name in a NATURAL/USING join table reference.
5831 
5832   SYNOPSIS
5833     find_field_in_natural_join()
5834     thd			 [in]  thread handler
5835     table_ref            [in]  table reference to search
5836     name		 [in]  name of field
5837     length		 [in]  length of name
5838     ref                  [in/out] if 'name' is resolved to a view field, ref is
5839                                set to point to the found view field
5840     register_tree_change [in]  TRUE if ref is not stack variable and we
5841                                need register changes in item tree
5842     actual_table         [out] the original table reference where the field
5843                                belongs - differs from 'table_list' only for
5844                                NATURAL/USING joins
5845 
5846   DESCRIPTION
5847     Search for a field among the result fields of a NATURAL/USING join.
5848     Notice that this procedure is called only for non-qualified field
5849     names. In the case of qualified fields, we search directly the base
5850     tables of a natural join.
5851 
5852   RETURN
5853     NULL        if the field was not found
5854     WRONG_GRANT if no access rights to the found field
5855     #           Pointer to the found Field
5856 */
5857 
5858 static Field *
find_field_in_natural_join(THD * thd,TABLE_LIST * table_ref,const char * name,size_t length,Item ** ref,bool register_tree_change,TABLE_LIST ** actual_table)5859 find_field_in_natural_join(THD *thd, TABLE_LIST *table_ref, const char *name, size_t length, Item **ref, bool register_tree_change,
5860                            TABLE_LIST **actual_table)
5861 {
5862   List_iterator_fast<Natural_join_column>
5863     field_it(*(table_ref->join_columns));
5864   Natural_join_column *nj_col, *curr_nj_col;
5865   Field *UNINIT_VAR(found_field);
5866   Query_arena *UNINIT_VAR(arena), backup;
5867   DBUG_ENTER("find_field_in_natural_join");
5868   DBUG_PRINT("enter", ("field name: '%s', ref %p",
5869 		       name, ref));
5870   DBUG_ASSERT(table_ref->is_natural_join && table_ref->join_columns);
5871   DBUG_ASSERT(*actual_table == NULL);
5872 
5873   for (nj_col= NULL, curr_nj_col= field_it++; curr_nj_col;
5874        curr_nj_col= field_it++)
5875   {
5876     if (!my_strcasecmp(system_charset_info, curr_nj_col->name()->str, name))
5877     {
5878       if (nj_col)
5879       {
5880         my_error(ER_NON_UNIQ_ERROR, MYF(0), name, thd->where);
5881         DBUG_RETURN(NULL);
5882       }
5883       nj_col= curr_nj_col;
5884     }
5885   }
5886   if (!nj_col)
5887     DBUG_RETURN(NULL);
5888 
5889   if (nj_col->view_field)
5890   {
5891     Item *item;
5892     if (register_tree_change)
5893       arena= thd->activate_stmt_arena_if_needed(&backup);
5894     /*
5895       create_item() may, or may not create a new Item, depending on the
5896       column reference. See create_view_field() for details.
5897     */
5898     item= nj_col->create_item(thd);
5899     if (!item)
5900       DBUG_RETURN(NULL);
5901 
5902     /*
5903      *ref != NULL means that *ref contains the item that we need to
5904      replace. If the item was aliased by the user, set the alias to
5905      the replacing item.
5906      */
5907     if (*ref && !(*ref)->is_autogenerated_name())
5908       item->set_name(thd, (*ref)->name);
5909     if (register_tree_change && arena)
5910       thd->restore_active_arena(arena, &backup);
5911 
5912     if (!item)
5913       DBUG_RETURN(NULL);
5914     DBUG_ASSERT(nj_col->table_field == NULL);
5915     if (nj_col->table_ref->schema_table_reformed)
5916     {
5917       /*
5918         Translation table items are always Item_fields and fixed
5919         already('mysql_schema_table' function). So we can return
5920         ->field. It is used only for 'show & where' commands.
5921       */
5922       DBUG_RETURN(((Item_field*) (nj_col->view_field->item))->field);
5923     }
5924     if (register_tree_change)
5925       thd->change_item_tree(ref, item);
5926     else
5927       *ref= item;
5928     found_field= (Field*) view_ref_found;
5929   }
5930   else
5931   {
5932     /* This is a base table. */
5933     DBUG_ASSERT(nj_col->view_field == NULL);
5934     Item *ref= 0;
5935     /*
5936       This fix_fields is not necessary (initially this item is fixed by
5937       the Item_field constructor; after reopen_tables the Item_func_eq
5938       calls fix_fields on that item), it's just a check during table
5939       reopening for columns that was dropped by the concurrent connection.
5940     */
5941     if (nj_col->table_field->fix_fields_if_needed(thd, &ref))
5942     {
5943       DBUG_PRINT("info", ("column '%s' was dropped by the concurrent connection",
5944                           nj_col->table_field->name.str));
5945       DBUG_RETURN(NULL);
5946     }
5947     DBUG_ASSERT(ref == 0);                      // Should not have changed
5948     DBUG_ASSERT(nj_col->table_ref->table == nj_col->table_field->field->table);
5949     found_field= nj_col->table_field->field;
5950     update_field_dependencies(thd, found_field, nj_col->table_ref->table);
5951   }
5952 
5953   *actual_table= nj_col->table_ref;
5954 
5955   DBUG_RETURN(found_field);
5956 }
5957 
5958 
5959 /*
5960   Find field by name in a base table or a view with temp table algorithm.
5961 
5962   The caller is expected to check column-level privileges.
5963 
5964   SYNOPSIS
5965     find_field_in_table()
5966     thd				thread handler
5967     table			table where to search for the field
5968     name			name of field
5969     length			length of name
5970     allow_rowid			do allow finding of "_rowid" field?
5971     cached_field_index_ptr	cached position in field list (used to speedup
5972                                 lookup for fields in prepared tables)
5973 
5974   RETURN
5975     0	field is not found
5976     #	pointer to field
5977 */
5978 
5979 Field *
find_field_in_table(THD * thd,TABLE * table,const char * name,size_t length,bool allow_rowid,uint * cached_field_index_ptr)5980 find_field_in_table(THD *thd, TABLE *table, const char *name, size_t length,
5981                     bool allow_rowid, uint *cached_field_index_ptr)
5982 {
5983   Field *field;
5984   uint cached_field_index= *cached_field_index_ptr;
5985   DBUG_ENTER("find_field_in_table");
5986   DBUG_PRINT("enter", ("table: '%s', field name: '%s'", table->alias.c_ptr(),
5987                        name));
5988 
5989   /* We assume here that table->field < NO_CACHED_FIELD_INDEX = UINT_MAX */
5990   if (cached_field_index < table->s->fields &&
5991       !my_strcasecmp(system_charset_info,
5992                      table->field[cached_field_index]->field_name.str, name))
5993     field= table->field[cached_field_index];
5994   else
5995   {
5996     LEX_CSTRING fname= {name, length};
5997     field= table->find_field_by_name(&fname);
5998   }
5999 
6000   if (field)
6001   {
6002     if (field->invisible == INVISIBLE_FULL &&
6003         DBUG_EVALUATE_IF("test_completely_invisible", 0, 1))
6004       DBUG_RETURN((Field*)0);
6005 
6006     if (field->invisible == INVISIBLE_SYSTEM &&
6007         thd->column_usage != MARK_COLUMNS_READ &&
6008         thd->column_usage != COLUMNS_READ)
6009       DBUG_RETURN((Field*)0);
6010   }
6011   else
6012   {
6013     if (!allow_rowid ||
6014         my_strcasecmp(system_charset_info, name, "_rowid") ||
6015         table->s->rowid_field_offset == 0)
6016       DBUG_RETURN((Field*) 0);
6017     field= table->field[table->s->rowid_field_offset-1];
6018   }
6019   *cached_field_index_ptr= field->field_index;
6020 
6021   update_field_dependencies(thd, field, table);
6022 
6023   DBUG_RETURN(field);
6024 }
6025 
6026 
6027 /*
6028   Find field in a table reference.
6029 
6030   SYNOPSIS
6031     find_field_in_table_ref()
6032     thd			   [in]  thread handler
6033     table_list		   [in]  table reference to search
6034     name		   [in]  name of field
6035     length		   [in]  field length of name
6036     item_name              [in]  name of item if it will be created (VIEW)
6037     db_name                [in]  optional database name that qualifies the
6038     table_name             [in]  optional table name that qualifies the field
6039                                  0 for non-qualified field in natural joins
6040     ref		       [in/out] if 'name' is resolved to a view field, ref
6041                                  is set to point to the found view field
6042     check_privileges       [in]  check privileges
6043     allow_rowid		   [in]  do allow finding of "_rowid" field?
6044     cached_field_index_ptr [in]  cached position in field list (used to
6045                                  speedup lookup for fields in prepared tables)
6046     register_tree_change   [in]  TRUE if ref is not stack variable and we
6047                                  need register changes in item tree
6048     actual_table           [out] the original table reference where the field
6049                                  belongs - differs from 'table_list' only for
6050                                  NATURAL_USING joins.
6051 
6052   DESCRIPTION
6053     Find a field in a table reference depending on the type of table
6054     reference. There are three types of table references with respect
6055     to the representation of their result columns:
6056     - an array of Field_translator objects for MERGE views and some
6057       information_schema tables,
6058     - an array of Field objects (and possibly a name hash) for stored
6059       tables,
6060     - a list of Natural_join_column objects for NATURAL/USING joins.
6061     This procedure detects the type of the table reference 'table_list'
6062     and calls the corresponding search routine.
6063 
6064     The routine checks column-level privieleges for the found field.
6065 
6066   RETURN
6067     0			field is not found
6068     view_ref_found	found value in VIEW (real result is in *ref)
6069     #			pointer to field
6070 */
6071 
6072 Field *
find_field_in_table_ref(THD * thd,TABLE_LIST * table_list,const char * name,size_t length,const char * item_name,const char * db_name,const char * table_name,Item ** ref,bool check_privileges,bool allow_rowid,uint * cached_field_index_ptr,bool register_tree_change,TABLE_LIST ** actual_table)6073 find_field_in_table_ref(THD *thd, TABLE_LIST *table_list,
6074                         const char *name, size_t length,
6075                         const char *item_name, const char *db_name,
6076                         const char *table_name, Item **ref,
6077                         bool check_privileges, bool allow_rowid,
6078                         uint *cached_field_index_ptr,
6079                         bool register_tree_change, TABLE_LIST **actual_table)
6080 {
6081   Field *fld;
6082   DBUG_ENTER("find_field_in_table_ref");
6083   DBUG_ASSERT(table_list->alias.str);
6084   DBUG_ASSERT(name);
6085   DBUG_ASSERT(item_name);
6086   DBUG_PRINT("enter",
6087              ("table: '%s'  field name: '%s'  item name: '%s'  ref %p",
6088               table_list->alias.str, name, item_name, ref));
6089 
6090   /*
6091     Check that the table and database that qualify the current field name
6092     are the same as the table reference we are going to search for the field.
6093 
6094     Exclude from the test below nested joins because the columns in a
6095     nested join generally originate from different tables. Nested joins
6096     also have no table name, except when a nested join is a merge view
6097     or an information schema table.
6098 
6099     We include explicitly table references with a 'field_translation' table,
6100     because if there are views over natural joins we don't want to search
6101     inside the view, but we want to search directly in the view columns
6102     which are represented as a 'field_translation'.
6103 
6104     tables->db.str may be 0 if we are preparing a statement
6105     db_name is 0 if item doesn't have a db name
6106     table_name is 0 if item doesn't have a specified table_name
6107   */
6108   if (db_name && !db_name[0])
6109     db_name= 0;                                 // Simpler test later
6110 
6111   if (/* Exclude nested joins. */
6112       (!table_list->nested_join ||
6113        /* Include merge views and information schema tables. */
6114        table_list->field_translation) &&
6115       /*
6116         Test if the field qualifiers match the table reference we plan
6117         to search.
6118       */
6119       table_name && table_name[0] &&
6120       (my_strcasecmp(table_alias_charset, table_list->alias.str, table_name) ||
6121        (db_name && (!table_list->db.str || !table_list->db.str[0])) ||
6122        (db_name && table_list->db.str && table_list->db.str[0] &&
6123         (table_list->schema_table ?
6124          my_strcasecmp(system_charset_info, db_name, table_list->db.str) :
6125          strcmp(db_name, table_list->db.str)))))
6126     DBUG_RETURN(0);
6127 
6128   /*
6129     Don't allow usage of fields in sequence table that is opened as part of
6130     NEXT VALUE for sequence_name
6131   */
6132   if (table_list->sequence)
6133     DBUG_RETURN(0);
6134 
6135   *actual_table= NULL;
6136 
6137   if (table_list->field_translation)
6138   {
6139     /* 'table_list' is a view or an information schema table. */
6140     if ((fld= find_field_in_view(thd, table_list, name, length, item_name, ref,
6141                                  register_tree_change)))
6142       *actual_table= table_list;
6143   }
6144   else if (!table_list->nested_join)
6145   {
6146     /* 'table_list' is a stored table. */
6147     DBUG_ASSERT(table_list->table);
6148     if ((fld= find_field_in_table(thd, table_list->table, name, length,
6149                                   allow_rowid,
6150                                   cached_field_index_ptr)))
6151       *actual_table= table_list;
6152   }
6153   else
6154   {
6155     /*
6156       'table_list' is a NATURAL/USING join, or an operand of such join that
6157       is a nested join itself.
6158 
6159       If the field name we search for is qualified, then search for the field
6160       in the table references used by NATURAL/USING the join.
6161     */
6162     if (table_name && table_name[0])
6163     {
6164       List_iterator<TABLE_LIST> it(table_list->nested_join->join_list);
6165       TABLE_LIST *table;
6166       while ((table= it++))
6167       {
6168         if ((fld= find_field_in_table_ref(thd, table, name, length, item_name,
6169                                           db_name, table_name, ref,
6170                                           check_privileges, allow_rowid,
6171                                           cached_field_index_ptr,
6172                                           register_tree_change, actual_table)))
6173           DBUG_RETURN(fld);
6174       }
6175       DBUG_RETURN(0);
6176     }
6177     /*
6178       Non-qualified field, search directly in the result columns of the
6179       natural join. The condition of the outer IF is true for the top-most
6180       natural join, thus if the field is not qualified, we will search
6181       directly the top-most NATURAL/USING join.
6182     */
6183     fld= find_field_in_natural_join(thd, table_list, name, length, ref,
6184                                     register_tree_change, actual_table);
6185   }
6186 
6187   if (fld)
6188   {
6189 #ifndef NO_EMBEDDED_ACCESS_CHECKS
6190     /* Check if there are sufficient access rights to the found field. */
6191     if (check_privileges &&
6192         !table_list->is_derived() &&
6193         check_column_grant_in_table_ref(thd, *actual_table, name, length, fld))
6194       fld= WRONG_GRANT;
6195     else
6196 #endif
6197       if (should_mark_column(thd->column_usage))
6198       {
6199         /*
6200           Get rw_set correct for this field so that the handler
6201           knows that this field is involved in the query and gets
6202           retrieved/updated
6203          */
6204         Field *field_to_set= NULL;
6205         if (fld == view_ref_found)
6206         {
6207           if (!ref)
6208             DBUG_RETURN(fld);
6209           Item *it= (*ref)->real_item();
6210           if (it->type() == Item::FIELD_ITEM)
6211             field_to_set= ((Item_field*)it)->field;
6212           else
6213           {
6214             if (thd->column_usage == MARK_COLUMNS_READ)
6215               it->walk(&Item::register_field_in_read_map, 0, 0);
6216             else
6217               it->walk(&Item::register_field_in_write_map, 0, 0);
6218           }
6219         }
6220         else
6221           field_to_set= fld;
6222         if (field_to_set)
6223         {
6224           TABLE *table= field_to_set->table;
6225           DBUG_ASSERT(table);
6226           if (thd->column_usage == MARK_COLUMNS_READ)
6227             field_to_set->register_field_in_read_map();
6228           else
6229             bitmap_set_bit(table->write_set, field_to_set->field_index);
6230         }
6231       }
6232   }
6233   DBUG_RETURN(fld);
6234 }
6235 
6236 
6237 /*
6238   Find field in table, no side effects, only purpose is to check for field
6239   in table object and get reference to the field if found.
6240 
6241   SYNOPSIS
6242   find_field_in_table_sef()
6243 
6244   table                         table where to find
6245   name                          Name of field searched for
6246 
6247   RETURN
6248     0                   field is not found
6249     #                   pointer to field
6250 */
6251 
find_field_in_table_sef(TABLE * table,const char * name)6252 Field *find_field_in_table_sef(TABLE *table, const char *name)
6253 {
6254   Field **field_ptr;
6255   if (table->s->name_hash.records)
6256   {
6257     field_ptr= (Field**)my_hash_search(&table->s->name_hash,(uchar*) name,
6258                                        strlen(name));
6259     if (field_ptr)
6260     {
6261       /*
6262         field_ptr points to field in TABLE_SHARE. Convert it to the matching
6263         field in table
6264       */
6265       field_ptr= (table->field + (field_ptr - table->s->field));
6266     }
6267   }
6268   else
6269   {
6270     if (!(field_ptr= table->field))
6271       return (Field *)0;
6272     for (; *field_ptr; ++field_ptr)
6273       if (!my_strcasecmp(system_charset_info, (*field_ptr)->field_name.str,
6274                          name))
6275         break;
6276   }
6277   if (field_ptr)
6278     return *field_ptr;
6279   else
6280     return (Field *)0;
6281 }
6282 
6283 
6284 /*
6285   Find field in table list.
6286 
6287   SYNOPSIS
6288     find_field_in_tables()
6289     thd			  pointer to current thread structure
6290     item		  field item that should be found
6291     first_table           list of tables to be searched for item
6292     last_table            end of the list of tables to search for item. If NULL
6293                           then search to the end of the list 'first_table'.
6294     ref			  if 'item' is resolved to a view field, ref is set to
6295                           point to the found view field
6296     report_error	  Degree of error reporting:
6297                           - IGNORE_ERRORS then do not report any error
6298                           - IGNORE_EXCEPT_NON_UNIQUE report only non-unique
6299                             fields, suppress all other errors
6300                           - REPORT_EXCEPT_NON_UNIQUE report all other errors
6301                             except when non-unique fields were found
6302                           - REPORT_ALL_ERRORS
6303     check_privileges      need to check privileges
6304     register_tree_change  TRUE if ref is not a stack variable and we
6305                           to need register changes in item tree
6306 
6307   RETURN VALUES
6308     0			If error: the found field is not unique, or there are
6309                         no sufficient access priviliges for the found field,
6310                         or the field is qualified with non-existing table.
6311     not_found_field	The function was called with report_error ==
6312                         (IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
6313 			field was not found.
6314     view_ref_found	View field is found, item passed through ref parameter
6315     found field         If a item was resolved to some field
6316 */
6317 
6318 Field *
find_field_in_tables(THD * thd,Item_ident * item,TABLE_LIST * first_table,TABLE_LIST * last_table,Item ** ref,find_item_error_report_type report_error,bool check_privileges,bool register_tree_change)6319 find_field_in_tables(THD *thd, Item_ident *item,
6320                      TABLE_LIST *first_table, TABLE_LIST *last_table,
6321 		     Item **ref, find_item_error_report_type report_error,
6322                      bool check_privileges, bool register_tree_change)
6323 {
6324   Field *found=0;
6325   const char *db= item->db_name.str;
6326   const char *table_name= item->table_name.str;
6327   const char *name= item->field_name.str;
6328   size_t length= item->field_name.length;
6329   char name_buff[SAFE_NAME_LEN+1];
6330   TABLE_LIST *cur_table= first_table;
6331   TABLE_LIST *actual_table;
6332   bool allow_rowid;
6333 
6334   if (!table_name || !table_name[0])
6335   {
6336     table_name= 0;                              // For easier test
6337     db= 0;
6338   }
6339 
6340   allow_rowid= table_name || (cur_table && !cur_table->next_local);
6341 
6342   if (item->cached_table)
6343   {
6344     DBUG_PRINT("info", ("using cached table"));
6345     /*
6346       This shortcut is used by prepared statements. We assume that
6347       TABLE_LIST *first_table is not changed during query execution (which
6348       is true for all queries except RENAME but luckily RENAME doesn't
6349       use fields...) so we can rely on reusing pointer to its member.
6350       With this optimization we also miss case when addition of one more
6351       field makes some prepared query ambiguous and so erroneous, but we
6352       accept this trade off.
6353     */
6354     TABLE_LIST *table_ref= item->cached_table;
6355     /*
6356       The condition (table_ref->view == NULL) ensures that we will call
6357       find_field_in_table even in the case of information schema tables
6358       when table_ref->field_translation != NULL.
6359       */
6360     if (table_ref->table && !table_ref->view &&
6361         (!table_ref->is_merged_derived() ||
6362          (!table_ref->is_multitable() && table_ref->merged_for_insert)))
6363     {
6364 
6365       found= find_field_in_table(thd, table_ref->table, name, length,
6366                                  TRUE, &(item->cached_field_index));
6367 #ifndef NO_EMBEDDED_ACCESS_CHECKS
6368       /* Check if there are sufficient access rights to the found field. */
6369       if (found && check_privileges && !is_temporary_table(table_ref) &&
6370           check_column_grant_in_table_ref(thd, table_ref, name, length,
6371                                           found))
6372         found= WRONG_GRANT;
6373 #endif
6374     }
6375     else
6376       found= find_field_in_table_ref(thd, table_ref, name, length, item->name.str,
6377                                      NULL, NULL, ref, check_privileges,
6378                                      TRUE, &(item->cached_field_index),
6379                                      register_tree_change,
6380                                      &actual_table);
6381     if (found)
6382     {
6383       if (found == WRONG_GRANT)
6384 	return (Field*) 0;
6385 
6386       /*
6387         Only views fields should be marked as dependent, not an underlying
6388         fields.
6389       */
6390       if (!table_ref->belong_to_view &&
6391           !table_ref->belong_to_derived)
6392       {
6393         SELECT_LEX *current_sel= item->context->select_lex;
6394         SELECT_LEX *last_select= table_ref->select_lex;
6395         bool all_merged= TRUE;
6396         for (SELECT_LEX *sl= current_sel; sl && sl!=last_select;
6397              sl=sl->outer_select())
6398         {
6399           Item *subs= sl->master_unit()->item;
6400           if (!subs)
6401             continue;
6402 
6403           Item_in_subselect *in_subs= subs->get_IN_subquery();
6404           if (in_subs &&
6405               in_subs->substype() == Item_subselect::IN_SUBS &&
6406               in_subs->test_strategy(SUBS_SEMI_JOIN))
6407           {
6408             continue;
6409           }
6410           all_merged= FALSE;
6411           break;
6412         }
6413         /*
6414           If the field was an outer referencee, mark all selects using this
6415           sub query as dependent on the outer query
6416         */
6417         if (!all_merged && current_sel != last_select)
6418         {
6419           mark_select_range_as_dependent(thd, last_select, current_sel,
6420                                          found, *ref, item, true);
6421         }
6422       }
6423       return found;
6424     }
6425   }
6426   else
6427     item->can_be_depended= TRUE;
6428 
6429   if (db && lower_case_table_names)
6430   {
6431     /*
6432       convert database to lower case for comparison.
6433       We can't do this in Item_field as this would change the
6434       'name' of the item which may be used in the select list
6435     */
6436     strmake_buf(name_buff, db);
6437     my_casedn_str(files_charset_info, name_buff);
6438     db= name_buff;
6439   }
6440 
6441   if (last_table)
6442     last_table= last_table->next_name_resolution_table;
6443 
6444   for (; cur_table != last_table ;
6445        cur_table= cur_table->next_name_resolution_table)
6446   {
6447     Field *cur_field= find_field_in_table_ref(thd, cur_table, name, length,
6448                                               item->name.str, db, table_name, ref,
6449                                               (thd->lex->sql_command ==
6450                                                SQLCOM_SHOW_FIELDS)
6451                                               ? false : check_privileges,
6452                                               allow_rowid,
6453                                               &(item->cached_field_index),
6454                                               register_tree_change,
6455                                               &actual_table);
6456     if (cur_field)
6457     {
6458       if (cur_field == WRONG_GRANT)
6459       {
6460         if (thd->lex->sql_command != SQLCOM_SHOW_FIELDS)
6461           return (Field*) 0;
6462 
6463         thd->clear_error();
6464         cur_field= find_field_in_table_ref(thd, cur_table, name, length,
6465                                            item->name.str, db, table_name, ref,
6466                                            false,
6467                                            allow_rowid,
6468                                            &(item->cached_field_index),
6469                                            register_tree_change,
6470                                            &actual_table);
6471         if (cur_field)
6472         {
6473           Field *nf=new Field_null(NULL,0,Field::NONE,
6474                                    &cur_field->field_name,
6475                                    &my_charset_bin);
6476           nf->init(cur_table->table);
6477           cur_field= nf;
6478         }
6479       }
6480 
6481       /*
6482         Store the original table of the field, which may be different from
6483         cur_table in the case of NATURAL/USING join.
6484       */
6485       item->cached_table= (!actual_table->cacheable_table || found) ?
6486                           0 : actual_table;
6487 
6488       DBUG_ASSERT(thd->where);
6489       /*
6490         If we found a fully qualified field we return it directly as it can't
6491         have duplicates.
6492        */
6493       if (db)
6494         return cur_field;
6495 
6496       if (unlikely(found))
6497       {
6498         if (report_error == REPORT_ALL_ERRORS ||
6499             report_error == IGNORE_EXCEPT_NON_UNIQUE)
6500           my_error(ER_NON_UNIQ_ERROR, MYF(0),
6501                    table_name ? item->full_name() : name, thd->where);
6502         return (Field*) 0;
6503       }
6504       found= cur_field;
6505     }
6506   }
6507 
6508   if (likely(found))
6509     return found;
6510 
6511   /*
6512     If the field was qualified and there were no tables to search, issue
6513     an error that an unknown table was given. The situation is detected
6514     as follows: if there were no tables we wouldn't go through the loop
6515     and cur_table wouldn't be updated by the loop increment part, so it
6516     will be equal to the first table.
6517   */
6518   if (table_name && (cur_table == first_table) &&
6519       (report_error == REPORT_ALL_ERRORS ||
6520        report_error == REPORT_EXCEPT_NON_UNIQUE))
6521   {
6522     char buff[SAFE_NAME_LEN*2 + 2];
6523     if (db && db[0])
6524     {
6525       strxnmov(buff,sizeof(buff)-1,db,".",table_name,NullS);
6526       table_name=buff;
6527     }
6528     my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, thd->where);
6529   }
6530   else
6531   {
6532     if (report_error == REPORT_ALL_ERRORS ||
6533         report_error == REPORT_EXCEPT_NON_UNIQUE)
6534       my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(), thd->where);
6535     else
6536       found= not_found_field;
6537   }
6538   return found;
6539 }
6540 
6541 
6542 /*
6543   Find Item in list of items (find_field_in_tables analog)
6544 
6545   TODO
6546     is it better return only counter?
6547 
6548   SYNOPSIS
6549     find_item_in_list()
6550     find			Item to find
6551     items			List of items
6552     counter			To return number of found item
6553     report_error
6554       REPORT_ALL_ERRORS		report errors, return 0 if error
6555       REPORT_EXCEPT_NOT_FOUND	Do not report 'not found' error and
6556 				return not_found_item, report other errors,
6557 				return 0
6558       IGNORE_ERRORS		Do not report errors, return 0 if error
6559     resolution                  Set to the resolution type if the item is found
6560                                 (it says whether the item is resolved
6561                                  against an alias name,
6562                                  or as a field name without alias,
6563                                  or as a field hidden by alias,
6564                                  or ignoring alias)
6565     limit                       How many items in the list to check
6566                                 (if limit==0 then all items are to be checked)
6567 
6568   RETURN VALUES
6569     0			Item is not found or item is not unique,
6570 			error message is reported
6571     not_found_item	Function was called with
6572 			report_error == REPORT_EXCEPT_NOT_FOUND and
6573 			item was not found. No error message was reported
6574                         found field
6575 */
6576 
6577 /* Special Item pointer to serve as a return value from find_item_in_list(). */
6578 Item **not_found_item= (Item**) 0x1;
6579 
6580 
6581 Item **
find_item_in_list(Item * find,List<Item> & items,uint * counter,find_item_error_report_type report_error,enum_resolution_type * resolution,uint limit)6582 find_item_in_list(Item *find, List<Item> &items, uint *counter,
6583                   find_item_error_report_type report_error,
6584                   enum_resolution_type *resolution, uint limit)
6585 {
6586   List_iterator<Item> li(items);
6587   uint n_items= limit == 0 ? items.elements : limit;
6588   Item **found=0, **found_unaliased= 0, *item;
6589   const char *db_name=0;
6590   const LEX_CSTRING *field_name= 0;
6591   const char *table_name=0;
6592   bool found_unaliased_non_uniq= 0;
6593   /*
6594     true if the item that we search for is a valid name reference
6595     (and not an item that happens to have a name).
6596   */
6597   bool is_ref_by_name= 0;
6598   uint unaliased_counter= 0;
6599 
6600   *resolution= NOT_RESOLVED;
6601 
6602   is_ref_by_name= (find->type() == Item::FIELD_ITEM  ||
6603                    find->type() == Item::REF_ITEM);
6604   if (is_ref_by_name)
6605   {
6606     field_name= &((Item_ident*) find)->field_name;
6607     table_name= ((Item_ident*) find)->table_name.str;
6608     db_name=    ((Item_ident*) find)->db_name.str;
6609   }
6610 
6611   for (uint i= 0; i < n_items; i++)
6612   {
6613     item= li++;
6614     if (field_name && field_name->str &&
6615         (item->real_item()->type() == Item::FIELD_ITEM ||
6616          ((item->type() == Item::REF_ITEM) &&
6617           (((Item_ref *)item)->ref_type() == Item_ref::VIEW_REF))))
6618     {
6619       Item_ident *item_field= (Item_ident*) item;
6620 
6621       /*
6622 	In case of group_concat() with ORDER BY condition in the QUERY
6623 	item_field can be field of temporary table without item name
6624 	(if this field created from expression argument of group_concat()),
6625 	=> we have to check presence of name before compare
6626       */
6627       if (unlikely(!item_field->name.str))
6628         continue;
6629 
6630       if (table_name)
6631       {
6632         /*
6633           If table name is specified we should find field 'field_name' in
6634           table 'table_name'. According to SQL-standard we should ignore
6635           aliases in this case.
6636 
6637           Since we should NOT prefer fields from the select list over
6638           other fields from the tables participating in this select in
6639           case of ambiguity we have to do extra check outside this function.
6640 
6641           We use strcmp for table names and database names as these may be
6642           case sensitive. In cases where they are not case sensitive, they
6643           are always in lower case.
6644 
6645 	  item_field->field_name and item_field->table_name can be 0x0 if
6646 	  item is not fix_field()'ed yet.
6647         */
6648         if (item_field->field_name.str && item_field->table_name.str &&
6649 	    !lex_string_cmp(system_charset_info, &item_field->field_name,
6650                             field_name) &&
6651             !my_strcasecmp(table_alias_charset, item_field->table_name.str,
6652                            table_name) &&
6653             (!db_name || (item_field->db_name.str &&
6654                           !strcmp(item_field->db_name.str, db_name))))
6655         {
6656           if (found_unaliased)
6657           {
6658             if ((*found_unaliased)->eq(item, 0))
6659               continue;
6660             /*
6661               Two matching fields in select list.
6662               We already can bail out because we are searching through
6663               unaliased names only and will have duplicate error anyway.
6664             */
6665             if (report_error != IGNORE_ERRORS)
6666               my_error(ER_NON_UNIQ_ERROR, MYF(0),
6667                        find->full_name(), current_thd->where);
6668             return (Item**) 0;
6669           }
6670           found_unaliased= li.ref();
6671           unaliased_counter= i;
6672           *resolution= RESOLVED_IGNORING_ALIAS;
6673           if (db_name)
6674             break;                              // Perfect match
6675         }
6676       }
6677       else
6678       {
6679         bool fname_cmp= lex_string_cmp(system_charset_info,
6680                                        &item_field->field_name,
6681                                        field_name);
6682         if (!lex_string_cmp(system_charset_info,
6683                             &item_field->name, field_name))
6684         {
6685           /*
6686             If table name was not given we should scan through aliases
6687             and non-aliased fields first. We are also checking unaliased
6688             name of the field in then next  else-if, to be able to find
6689             instantly field (hidden by alias) if no suitable alias or
6690             non-aliased field was found.
6691           */
6692           if (found)
6693           {
6694             if ((*found)->eq(item, 0))
6695               continue;                           // Same field twice
6696             if (report_error != IGNORE_ERRORS)
6697               my_error(ER_NON_UNIQ_ERROR, MYF(0),
6698                        find->full_name(), current_thd->where);
6699             return (Item**) 0;
6700           }
6701           found= li.ref();
6702           *counter= i;
6703           *resolution= fname_cmp ? RESOLVED_AGAINST_ALIAS:
6704 	                           RESOLVED_WITH_NO_ALIAS;
6705         }
6706         else if (!fname_cmp)
6707         {
6708           /*
6709             We will use non-aliased field or react on such ambiguities only if
6710             we won't be able to find aliased field.
6711             Again if we have ambiguity with field outside of select list
6712             we should prefer fields from select list.
6713           */
6714           if (found_unaliased)
6715           {
6716             if ((*found_unaliased)->eq(item, 0))
6717               continue;                           // Same field twice
6718             found_unaliased_non_uniq= 1;
6719           }
6720           found_unaliased= li.ref();
6721           unaliased_counter= i;
6722         }
6723       }
6724     }
6725     else if (!table_name)
6726     {
6727       if (is_ref_by_name && find->name.str && item->name.str &&
6728           find->name.length == item->name.length &&
6729 	  !lex_string_cmp(system_charset_info, &item->name, &find->name))
6730       {
6731         found= li.ref();
6732         *counter= i;
6733         *resolution= RESOLVED_AGAINST_ALIAS;
6734         break;
6735       }
6736       else if (find->eq(item,0))
6737       {
6738         found= li.ref();
6739         *counter= i;
6740         *resolution= RESOLVED_IGNORING_ALIAS;
6741         break;
6742       }
6743     }
6744   }
6745 
6746   if (likely(found))
6747     return found;
6748 
6749   if (unlikely(found_unaliased_non_uniq))
6750   {
6751     if (report_error != IGNORE_ERRORS)
6752       my_error(ER_NON_UNIQ_ERROR, MYF(0),
6753                find->full_name(), current_thd->where);
6754     return (Item **) 0;
6755   }
6756   if (found_unaliased)
6757   {
6758     found= found_unaliased;
6759     *counter= unaliased_counter;
6760     *resolution= RESOLVED_BEHIND_ALIAS;
6761   }
6762 
6763   if (found)
6764     return found;
6765 
6766   if (report_error != REPORT_EXCEPT_NOT_FOUND)
6767   {
6768     if (report_error == REPORT_ALL_ERRORS)
6769       my_error(ER_BAD_FIELD_ERROR, MYF(0),
6770                find->full_name(), current_thd->where);
6771     return (Item **) 0;
6772   }
6773   else
6774     return (Item **) not_found_item;
6775 }
6776 
6777 
6778 /*
6779   Test if a string is a member of a list of strings.
6780 
6781   SYNOPSIS
6782     test_if_string_in_list()
6783     find      the string to look for
6784     str_list  a list of strings to be searched
6785 
6786   DESCRIPTION
6787     Sequentially search a list of strings for a string, and test whether
6788     the list contains the same string.
6789 
6790   RETURN
6791     TRUE  if find is in str_list
6792     FALSE otherwise
6793 */
6794 
6795 static bool
test_if_string_in_list(const char * find,List<String> * str_list)6796 test_if_string_in_list(const char *find, List<String> *str_list)
6797 {
6798   List_iterator<String> str_list_it(*str_list);
6799   String *curr_str;
6800   size_t find_length= strlen(find);
6801   while ((curr_str= str_list_it++))
6802   {
6803     if (find_length != curr_str->length())
6804       continue;
6805     if (!my_strcasecmp(system_charset_info, find, curr_str->ptr()))
6806       return TRUE;
6807   }
6808   return FALSE;
6809 }
6810 
6811 
6812 /*
6813   Create a new name resolution context for an item so that it is
6814   being resolved in a specific table reference.
6815 
6816   SYNOPSIS
6817     set_new_item_local_context()
6818     thd        pointer to current thread
6819     item       item for which new context is created and set
6820     table_ref  table ref where an item showld be resolved
6821 
6822   DESCRIPTION
6823     Create a new name resolution context for an item, so that the item
6824     is resolved only the supplied 'table_ref'.
6825 
6826   RETURN
6827     FALSE  if all OK
6828     TRUE   otherwise
6829 */
6830 
6831 static bool
set_new_item_local_context(THD * thd,Item_ident * item,TABLE_LIST * table_ref)6832 set_new_item_local_context(THD *thd, Item_ident *item, TABLE_LIST *table_ref)
6833 {
6834   Name_resolution_context *context;
6835   if (!(context= new (thd->mem_root) Name_resolution_context))
6836     return TRUE;
6837   context->init();
6838   context->first_name_resolution_table=
6839     context->last_name_resolution_table= table_ref;
6840   item->context= context;
6841   return FALSE;
6842 }
6843 
6844 
6845 /*
6846   Find and mark the common columns of two table references.
6847 
6848   SYNOPSIS
6849     mark_common_columns()
6850     thd                [in] current thread
6851     table_ref_1        [in] the first (left) join operand
6852     table_ref_2        [in] the second (right) join operand
6853     using_fields       [in] if the join is JOIN...USING - the join columns,
6854                             if NATURAL join, then NULL
6855     found_using_fields [out] number of fields from the USING clause that were
6856                              found among the common fields
6857 
6858   DESCRIPTION
6859     The procedure finds the common columns of two relations (either
6860     tables or intermediate join results), and adds an equi-join condition
6861     to the ON clause of 'table_ref_2' for each pair of matching columns.
6862     If some of table_ref_XXX represents a base table or view, then we
6863     create new 'Natural_join_column' instances for each column
6864     reference and store them in the 'join_columns' of the table
6865     reference.
6866 
6867   IMPLEMENTATION
6868     The procedure assumes that store_natural_using_join_columns() was
6869     called for the previous level of NATURAL/USING joins.
6870 
6871   RETURN
6872     TRUE   error when some common column is non-unique, or out of memory
6873     FALSE  OK
6874 */
6875 
6876 static bool
mark_common_columns(THD * thd,TABLE_LIST * table_ref_1,TABLE_LIST * table_ref_2,List<String> * using_fields,uint * found_using_fields)6877 mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2,
6878                     List<String> *using_fields, uint *found_using_fields)
6879 {
6880   Field_iterator_table_ref it_1, it_2;
6881   Natural_join_column *nj_col_1, *nj_col_2;
6882   Query_arena *arena, backup;
6883   bool result= TRUE;
6884   bool first_outer_loop= TRUE;
6885   Field *field_1;
6886   field_visibility_t field_1_invisible, field_2_invisible;
6887   /*
6888     Leaf table references to which new natural join columns are added
6889     if the leaves are != NULL.
6890   */
6891   TABLE_LIST *leaf_1= (table_ref_1->nested_join &&
6892                        !table_ref_1->is_natural_join) ?
6893                       NULL : table_ref_1;
6894   TABLE_LIST *leaf_2= (table_ref_2->nested_join &&
6895                        !table_ref_2->is_natural_join) ?
6896                       NULL : table_ref_2;
6897 
6898   DBUG_ENTER("mark_common_columns");
6899   DBUG_PRINT("info", ("operand_1: %s  operand_2: %s",
6900                       table_ref_1->alias.str, table_ref_2->alias.str));
6901 
6902   *found_using_fields= 0;
6903   arena= thd->activate_stmt_arena_if_needed(&backup);
6904 
6905   for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
6906   {
6907     bool found= FALSE;
6908     const LEX_CSTRING *field_name_1;
6909     Field *field_2= 0;
6910 
6911     /* true if field_name_1 is a member of using_fields */
6912     bool is_using_column_1;
6913     if (!(nj_col_1= it_1.get_or_create_column_ref(thd, leaf_1)))
6914       goto err;
6915 
6916     field_1= nj_col_1->field();
6917     field_1_invisible= field_1 ? field_1->invisible : VISIBLE;
6918 
6919     if (field_1_invisible == INVISIBLE_FULL)
6920       continue;
6921 
6922     field_name_1= nj_col_1->name();
6923     is_using_column_1= using_fields &&
6924       test_if_string_in_list(field_name_1->str, using_fields);
6925     DBUG_PRINT ("info", ("field_name_1=%s.%s",
6926                          nj_col_1->safe_table_name(),
6927                          field_name_1->str));
6928 
6929     if (field_1_invisible && !is_using_column_1)
6930       continue;
6931 
6932     /*
6933       Find a field with the same name in table_ref_2.
6934 
6935       Note that for the second loop, it_2.set() will iterate over
6936       table_ref_2->join_columns and not generate any new elements or
6937       lists.
6938     */
6939     nj_col_2= NULL;
6940     for (it_2.set(table_ref_2); !it_2.end_of_fields(); it_2.next())
6941     {
6942       Natural_join_column *cur_nj_col_2;
6943       const LEX_CSTRING *cur_field_name_2;
6944       if (!(cur_nj_col_2= it_2.get_or_create_column_ref(thd, leaf_2)))
6945         goto err;
6946 
6947       field_2= cur_nj_col_2->field();
6948       field_2_invisible= field_2 ? field_2->invisible : VISIBLE;
6949 
6950       if (field_2_invisible == INVISIBLE_FULL)
6951         continue;
6952 
6953       cur_field_name_2= cur_nj_col_2->name();
6954       DBUG_PRINT ("info", ("cur_field_name_2=%s.%s",
6955                            cur_nj_col_2->safe_table_name(),
6956                            cur_field_name_2->str));
6957 
6958       /*
6959         Compare the two columns and check for duplicate common fields.
6960         A common field is duplicate either if it was already found in
6961         table_ref_2 (then found == TRUE), or if a field in table_ref_2
6962         was already matched by some previous field in table_ref_1
6963         (then cur_nj_col_2->is_common == TRUE).
6964         Note that it is too early to check the columns outside of the
6965         USING list for ambiguity because they are not actually "referenced"
6966         here. These columns must be checked only on unqualified reference
6967         by name (e.g. in SELECT list).
6968       */
6969       if (!lex_string_cmp(system_charset_info, field_name_1,
6970                           cur_field_name_2))
6971       {
6972         DBUG_PRINT ("info", ("match c1.is_common=%d", nj_col_1->is_common));
6973         if (cur_nj_col_2->is_common || found)
6974         {
6975           my_error(ER_NON_UNIQ_ERROR, MYF(0), field_name_1->str, thd->where);
6976           goto err;
6977         }
6978         if ((!using_fields && !field_2_invisible) || is_using_column_1)
6979         {
6980           DBUG_ASSERT(nj_col_2 == NULL);
6981           nj_col_2= cur_nj_col_2;
6982           found= TRUE;
6983         }
6984       }
6985     }
6986     if (first_outer_loop && leaf_2)
6987     {
6988       /*
6989         Make sure that the next inner loop "knows" that all columns
6990         are materialized already.
6991       */
6992       leaf_2->is_join_columns_complete= TRUE;
6993       first_outer_loop= FALSE;
6994     }
6995     if (!found)
6996       continue;                                 // No matching field
6997 
6998     /*
6999       field_1 and field_2 have the same names. Check if they are in the USING
7000       clause (if present), mark them as common fields, and add a new
7001       equi-join condition to the ON clause.
7002     */
7003     if (nj_col_2)
7004     {
7005       /*
7006         Create non-fixed fully qualified field and let fix_fields to
7007         resolve it.
7008       */
7009       Item *item_1=   nj_col_1->create_item(thd);
7010       Item *item_2=   nj_col_2->create_item(thd);
7011       Item_ident *item_ident_1, *item_ident_2;
7012       Item_func_eq *eq_cond;
7013 
7014       if (!item_1 || !item_2)
7015         goto err;                               // out of memory
7016 
7017       /*
7018         The following assert checks that the two created items are of
7019         type Item_ident.
7020       */
7021       DBUG_ASSERT(!thd->lex->current_select->no_wrap_view_item);
7022       /*
7023         In the case of no_wrap_view_item == 0, the created items must be
7024         of sub-classes of Item_ident.
7025       */
7026       DBUG_ASSERT(item_1->type() == Item::FIELD_ITEM ||
7027                   item_1->type() == Item::REF_ITEM);
7028       DBUG_ASSERT(item_2->type() == Item::FIELD_ITEM ||
7029                   item_2->type() == Item::REF_ITEM);
7030 
7031       /*
7032         We need to cast item_1,2 to Item_ident, because we need to hook name
7033         resolution contexts specific to each item.
7034       */
7035       item_ident_1= (Item_ident*) item_1;
7036       item_ident_2= (Item_ident*) item_2;
7037       /*
7038         Create and hook special name resolution contexts to each item in the
7039         new join condition . We need this to both speed-up subsequent name
7040         resolution of these items, and to enable proper name resolution of
7041         the items during the execute phase of PS.
7042       */
7043       if (set_new_item_local_context(thd, item_ident_1, nj_col_1->table_ref) ||
7044           set_new_item_local_context(thd, item_ident_2, nj_col_2->table_ref))
7045         goto err;
7046 
7047       if (!(eq_cond= new (thd->mem_root) Item_func_eq(thd, item_ident_1, item_ident_2)))
7048         goto err;                               /* Out of memory. */
7049 
7050       /*
7051         Add the new equi-join condition to the ON clause. Notice that
7052         fix_fields() is applied to all ON conditions in setup_conds()
7053         so we don't do it here.
7054       */
7055       add_join_on(thd, (table_ref_1->outer_join & JOIN_TYPE_RIGHT ?
7056                         table_ref_1 : table_ref_2),
7057                   eq_cond);
7058 
7059       nj_col_1->is_common= nj_col_2->is_common= TRUE;
7060       DBUG_PRINT ("info", ("%s.%s and %s.%s are common",
7061                            nj_col_1->safe_table_name(),
7062                            nj_col_1->name()->str,
7063                            nj_col_2->safe_table_name(),
7064                            nj_col_2->name()->str));
7065 
7066       if (field_1)
7067         update_field_dependencies(thd, field_1, field_1->table);
7068       if (field_2)
7069         update_field_dependencies(thd, field_2, field_2->table);
7070 
7071       if (using_fields != NULL)
7072         ++(*found_using_fields);
7073     }
7074   }
7075   if (leaf_1)
7076     leaf_1->is_join_columns_complete= TRUE;
7077 
7078   /*
7079     Everything is OK.
7080     Notice that at this point there may be some column names in the USING
7081     clause that are not among the common columns. This is an SQL error and
7082     we check for this error in store_natural_using_join_columns() when
7083     (found_using_fields < length(join_using_fields)).
7084   */
7085   result= FALSE;
7086 
7087 err:
7088   if (arena)
7089     thd->restore_active_arena(arena, &backup);
7090   DBUG_RETURN(result);
7091 }
7092 
7093 
7094 
7095 /*
7096   Materialize and store the row type of NATURAL/USING join.
7097 
7098   SYNOPSIS
7099     store_natural_using_join_columns()
7100     thd                current thread
7101     natural_using_join the table reference of the NATURAL/USING join
7102     table_ref_1        the first (left) operand (of a NATURAL/USING join).
7103     table_ref_2        the second (right) operand (of a NATURAL/USING join).
7104     using_fields       if the join is JOIN...USING - the join columns,
7105                        if NATURAL join, then NULL
7106     found_using_fields number of fields from the USING clause that were
7107                        found among the common fields
7108 
7109   DESCRIPTION
7110     Iterate over the columns of both join operands and sort and store
7111     all columns into the 'join_columns' list of natural_using_join
7112     where the list is formed by three parts:
7113       part1: The coalesced columns of table_ref_1 and table_ref_2,
7114              sorted according to the column order of the first table.
7115       part2: The other columns of the first table, in the order in
7116              which they were defined in CREATE TABLE.
7117       part3: The other columns of the second table, in the order in
7118              which they were defined in CREATE TABLE.
7119     Time complexity - O(N1+N2), where Ni = length(table_ref_i).
7120 
7121   IMPLEMENTATION
7122     The procedure assumes that mark_common_columns() has been called
7123     for the join that is being processed.
7124 
7125   RETURN
7126     TRUE    error: Some common column is ambiguous
7127     FALSE   OK
7128 */
7129 
7130 static bool
store_natural_using_join_columns(THD * thd,TABLE_LIST * natural_using_join,TABLE_LIST * table_ref_1,TABLE_LIST * table_ref_2,List<String> * using_fields,uint found_using_fields)7131 store_natural_using_join_columns(THD *thd, TABLE_LIST *natural_using_join,
7132                                  TABLE_LIST *table_ref_1,
7133                                  TABLE_LIST *table_ref_2,
7134                                  List<String> *using_fields,
7135                                  uint found_using_fields)
7136 {
7137   Field_iterator_table_ref it_1, it_2;
7138   Natural_join_column *nj_col_1, *nj_col_2;
7139   Query_arena *arena, backup;
7140   bool result= TRUE;
7141   List<Natural_join_column> *non_join_columns;
7142   List<Natural_join_column> *join_columns;
7143   DBUG_ENTER("store_natural_using_join_columns");
7144 
7145   DBUG_ASSERT(!natural_using_join->join_columns);
7146 
7147   arena= thd->activate_stmt_arena_if_needed(&backup);
7148 
7149   if (!(non_join_columns= new List<Natural_join_column>) ||
7150       !(join_columns= new List<Natural_join_column>))
7151     goto err;
7152 
7153   /* Append the columns of the first join operand. */
7154   for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
7155   {
7156     nj_col_1= it_1.get_natural_column_ref();
7157     if (nj_col_1->is_common)
7158     {
7159       join_columns->push_back(nj_col_1, thd->mem_root);
7160       /* Reset the common columns for the next call to mark_common_columns. */
7161       nj_col_1->is_common= FALSE;
7162     }
7163     else
7164       non_join_columns->push_back(nj_col_1, thd->mem_root);
7165   }
7166 
7167   /*
7168     Check that all columns in the USING clause are among the common
7169     columns. If this is not the case, report the first one that was
7170     not found in an error.
7171   */
7172   if (using_fields && found_using_fields < using_fields->elements)
7173   {
7174     String *using_field_name;
7175     List_iterator_fast<String> using_fields_it(*using_fields);
7176     while ((using_field_name= using_fields_it++))
7177     {
7178       const char *using_field_name_ptr= using_field_name->c_ptr();
7179       List_iterator_fast<Natural_join_column>
7180         it(*join_columns);
7181       Natural_join_column *common_field;
7182 
7183       for (;;)
7184       {
7185         /* If reached the end of fields, and none was found, report error. */
7186         if (!(common_field= it++))
7187         {
7188           my_error(ER_BAD_FIELD_ERROR, MYF(0), using_field_name_ptr,
7189                    current_thd->where);
7190           goto err;
7191         }
7192         if (!my_strcasecmp(system_charset_info,
7193                            common_field->name()->str, using_field_name_ptr))
7194           break;                                // Found match
7195       }
7196     }
7197   }
7198 
7199   /* Append the non-equi-join columns of the second join operand. */
7200   for (it_2.set(table_ref_2); !it_2.end_of_fields(); it_2.next())
7201   {
7202     nj_col_2= it_2.get_natural_column_ref();
7203     if (!nj_col_2->is_common)
7204       non_join_columns->push_back(nj_col_2, thd->mem_root);
7205     else
7206     {
7207       /* Reset the common columns for the next call to mark_common_columns. */
7208       nj_col_2->is_common= FALSE;
7209     }
7210   }
7211 
7212   if (non_join_columns->elements > 0)
7213     join_columns->append(non_join_columns);
7214   natural_using_join->join_columns= join_columns;
7215   natural_using_join->is_join_columns_complete= TRUE;
7216 
7217   result= FALSE;
7218 
7219   if (arena)
7220     thd->restore_active_arena(arena, &backup);
7221   DBUG_RETURN(result);
7222 
7223 err:
7224   /*
7225      Actually we failed to build join columns list, so we have to
7226      clear it to avoid problems with half-build join on next run.
7227      The list was created in mark_common_columns().
7228    */
7229   table_ref_1->remove_join_columns();
7230   table_ref_2->remove_join_columns();
7231 
7232   if (arena)
7233     thd->restore_active_arena(arena, &backup);
7234   DBUG_RETURN(TRUE);
7235 }
7236 
7237 
7238 /*
7239   Precompute and store the row types of the top-most NATURAL/USING joins.
7240 
7241   SYNOPSIS
7242     store_top_level_join_columns()
7243     thd            current thread
7244     table_ref      nested join or table in a FROM clause
7245     left_neighbor  neighbor table reference to the left of table_ref at the
7246                    same level in the join tree
7247     right_neighbor neighbor table reference to the right of table_ref at the
7248                    same level in the join tree
7249 
7250   DESCRIPTION
7251     The procedure performs a post-order traversal of a nested join tree
7252     and materializes the row types of NATURAL/USING joins in a
7253     bottom-up manner until it reaches the TABLE_LIST elements that
7254     represent the top-most NATURAL/USING joins. The procedure should be
7255     applied to each element of SELECT_LEX::top_join_list (i.e. to each
7256     top-level element of the FROM clause).
7257 
7258   IMPLEMENTATION
7259     Notice that the table references in the list nested_join->join_list
7260     are in reverse order, thus when we iterate over it, we are moving
7261     from the right to the left in the FROM clause.
7262 
7263   RETURN
7264     TRUE   Error
7265     FALSE  OK
7266 */
7267 
7268 static bool
store_top_level_join_columns(THD * thd,TABLE_LIST * table_ref,TABLE_LIST * left_neighbor,TABLE_LIST * right_neighbor)7269 store_top_level_join_columns(THD *thd, TABLE_LIST *table_ref,
7270                              TABLE_LIST *left_neighbor,
7271                              TABLE_LIST *right_neighbor)
7272 {
7273   Query_arena *arena, backup;
7274   bool result= TRUE;
7275 
7276   DBUG_ENTER("store_top_level_join_columns");
7277 
7278   arena= thd->activate_stmt_arena_if_needed(&backup);
7279 
7280   /* Call the procedure recursively for each nested table reference. */
7281   if (table_ref->nested_join)
7282   {
7283     List_iterator_fast<TABLE_LIST> nested_it(table_ref->nested_join->join_list);
7284     TABLE_LIST *same_level_left_neighbor= nested_it++;
7285     TABLE_LIST *same_level_right_neighbor= NULL;
7286     /* Left/right-most neighbors, possibly at higher levels in the join tree. */
7287     TABLE_LIST *real_left_neighbor, *real_right_neighbor;
7288 
7289     while (same_level_left_neighbor)
7290     {
7291       TABLE_LIST *cur_table_ref= same_level_left_neighbor;
7292       same_level_left_neighbor= nested_it++;
7293       /*
7294         The order of RIGHT JOIN operands is reversed in 'join list' to
7295         transform it into a LEFT JOIN. However, in this procedure we need
7296         the join operands in their lexical order, so below we reverse the
7297         join operands. Notice that this happens only in the first loop,
7298         and not in the second one, as in the second loop
7299         same_level_left_neighbor == NULL.
7300         This is the correct behavior, because the second loop sets
7301         cur_table_ref reference correctly after the join operands are
7302         swapped in the first loop.
7303       */
7304       if (same_level_left_neighbor &&
7305           cur_table_ref->outer_join & JOIN_TYPE_RIGHT)
7306       {
7307         /* This can happen only for JOIN ... ON. */
7308         DBUG_ASSERT(table_ref->nested_join->join_list.elements == 2);
7309         swap_variables(TABLE_LIST*, same_level_left_neighbor, cur_table_ref);
7310       }
7311 
7312       /*
7313         Pick the parent's left and right neighbors if there are no immediate
7314         neighbors at the same level.
7315       */
7316       real_left_neighbor=  (same_level_left_neighbor) ?
7317                            same_level_left_neighbor : left_neighbor;
7318       real_right_neighbor= (same_level_right_neighbor) ?
7319                            same_level_right_neighbor : right_neighbor;
7320 
7321       if (cur_table_ref->nested_join &&
7322           store_top_level_join_columns(thd, cur_table_ref,
7323                                        real_left_neighbor, real_right_neighbor))
7324         goto err;
7325       same_level_right_neighbor= cur_table_ref;
7326     }
7327   }
7328 
7329   /*
7330     If this is a NATURAL/USING join, materialize its result columns and
7331     convert to a JOIN ... ON.
7332   */
7333   if (table_ref->is_natural_join)
7334   {
7335     DBUG_ASSERT(table_ref->nested_join &&
7336                 table_ref->nested_join->join_list.elements == 2);
7337     List_iterator_fast<TABLE_LIST> operand_it(table_ref->nested_join->join_list);
7338     /*
7339       Notice that the order of join operands depends on whether table_ref
7340       represents a LEFT or a RIGHT join. In a RIGHT join, the operands are
7341       in inverted order.
7342      */
7343     TABLE_LIST *table_ref_2= operand_it++; /* Second NATURAL join operand.*/
7344     TABLE_LIST *table_ref_1= operand_it++; /* First NATURAL join operand. */
7345     List<String> *using_fields= table_ref->join_using_fields;
7346     uint found_using_fields;
7347 
7348     /*
7349       The two join operands were interchanged in the parser, change the order
7350       back for 'mark_common_columns'.
7351     */
7352     if (table_ref_2->outer_join & JOIN_TYPE_RIGHT)
7353       swap_variables(TABLE_LIST*, table_ref_1, table_ref_2);
7354     if (mark_common_columns(thd, table_ref_1, table_ref_2,
7355                             using_fields, &found_using_fields))
7356       goto err;
7357 
7358     /*
7359       Swap the join operands back, so that we pick the columns of the second
7360       one as the coalesced columns. In this way the coalesced columns are the
7361       same as of an equivalent LEFT JOIN.
7362     */
7363     if (table_ref_1->outer_join & JOIN_TYPE_RIGHT)
7364       swap_variables(TABLE_LIST*, table_ref_1, table_ref_2);
7365     if (store_natural_using_join_columns(thd, table_ref, table_ref_1,
7366                                          table_ref_2, using_fields,
7367                                          found_using_fields))
7368       goto err;
7369 
7370     /*
7371       Change NATURAL JOIN to JOIN ... ON. We do this for both operands
7372       because either one of them or the other is the one with the
7373       natural join flag because RIGHT joins are transformed into LEFT,
7374       and the two tables may be reordered.
7375     */
7376     table_ref_1->natural_join= table_ref_2->natural_join= NULL;
7377 
7378     /* Add a TRUE condition to outer joins that have no common columns. */
7379     if (table_ref_2->outer_join &&
7380         !table_ref_1->on_expr && !table_ref_2->on_expr)
7381       table_ref_2->on_expr= new (thd->mem_root) Item_int(thd, (longlong) 1, 1); // Always true.
7382 
7383     /* Change this table reference to become a leaf for name resolution. */
7384     if (left_neighbor)
7385     {
7386       TABLE_LIST *last_leaf_on_the_left;
7387       last_leaf_on_the_left= left_neighbor->last_leaf_for_name_resolution();
7388       last_leaf_on_the_left->next_name_resolution_table= table_ref;
7389     }
7390     if (right_neighbor)
7391     {
7392       TABLE_LIST *first_leaf_on_the_right;
7393       first_leaf_on_the_right= right_neighbor->first_leaf_for_name_resolution();
7394       table_ref->next_name_resolution_table= first_leaf_on_the_right;
7395     }
7396     else
7397       table_ref->next_name_resolution_table= NULL;
7398   }
7399   result= FALSE; /* All is OK. */
7400 
7401 err:
7402   if (arena)
7403     thd->restore_active_arena(arena, &backup);
7404   DBUG_RETURN(result);
7405 }
7406 
7407 
7408 /*
7409   Compute and store the row types of the top-most NATURAL/USING joins
7410   in a FROM clause.
7411 
7412   SYNOPSIS
7413     setup_natural_join_row_types()
7414     thd          current thread
7415     from_clause  list of top-level table references in a FROM clause
7416 
7417   DESCRIPTION
7418     Apply the procedure 'store_top_level_join_columns' to each of the
7419     top-level table referencs of the FROM clause. Adjust the list of tables
7420     for name resolution - context->first_name_resolution_table to the
7421     top-most, lef-most NATURAL/USING join.
7422 
7423   IMPLEMENTATION
7424     Notice that the table references in 'from_clause' are in reverse
7425     order, thus when we iterate over it, we are moving from the right
7426     to the left in the FROM clause.
7427 
7428   NOTES
7429     We can't run this many times as the first_name_resolution_table would
7430     be different for subsequent runs when sub queries has been optimized
7431     away.
7432 
7433   RETURN
7434     TRUE   Error
7435     FALSE  OK
7436 */
7437 
setup_natural_join_row_types(THD * thd,List<TABLE_LIST> * from_clause,Name_resolution_context * context)7438 static bool setup_natural_join_row_types(THD *thd,
7439                                          List<TABLE_LIST> *from_clause,
7440                                          Name_resolution_context *context)
7441 {
7442   DBUG_ENTER("setup_natural_join_row_types");
7443   thd->where= "from clause";
7444   if (from_clause->elements == 0)
7445     DBUG_RETURN(false); /* We come here in the case of UNIONs. */
7446 
7447   /*
7448      Do not redo work if already done:
7449      1) for stored procedures,
7450      2) for multitable update after lock failure and table reopening.
7451   */
7452   if (!context->select_lex->first_natural_join_processing)
7453   {
7454     context->first_name_resolution_table= context->natural_join_first_table;
7455     DBUG_PRINT("info", ("using cached setup_natural_join_row_types"));
7456     DBUG_RETURN(false);
7457   }
7458 
7459   List_iterator_fast<TABLE_LIST> table_ref_it(*from_clause);
7460   TABLE_LIST *table_ref; /* Current table reference. */
7461   /* Table reference to the left of the current. */
7462   TABLE_LIST *left_neighbor;
7463   /* Table reference to the right of the current. */
7464   TABLE_LIST *right_neighbor= NULL;
7465 
7466   /* Note that tables in the list are in reversed order */
7467   for (left_neighbor= table_ref_it++; left_neighbor ; )
7468   {
7469     table_ref= left_neighbor;
7470     do
7471     {
7472       left_neighbor= table_ref_it++;
7473     }
7474     while (left_neighbor && left_neighbor->sj_subq_pred);
7475 
7476     if (store_top_level_join_columns(thd, table_ref,
7477                                      left_neighbor, right_neighbor))
7478       DBUG_RETURN(true);
7479     if (left_neighbor)
7480     {
7481       TABLE_LIST *first_leaf_on_the_right;
7482       first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution();
7483       left_neighbor->next_name_resolution_table= first_leaf_on_the_right;
7484     }
7485     right_neighbor= table_ref;
7486   }
7487 
7488   /*
7489     Store the top-most, left-most NATURAL/USING join, so that we start
7490     the search from that one instead of context->table_list. At this point
7491     right_neighbor points to the left-most top-level table reference in the
7492     FROM clause.
7493   */
7494   DBUG_ASSERT(right_neighbor);
7495   context->first_name_resolution_table=
7496     right_neighbor->first_leaf_for_name_resolution();
7497   /*
7498     This is only to ensure that first_name_resolution_table doesn't
7499     change on re-execution
7500   */
7501   context->natural_join_first_table= context->first_name_resolution_table;
7502   context->select_lex->first_natural_join_processing= false;
7503   DBUG_RETURN (false);
7504 }
7505 
7506 
7507 /****************************************************************************
7508 ** Expand all '*' in given fields
7509 ****************************************************************************/
7510 
setup_wild(THD * thd,TABLE_LIST * tables,List<Item> & fields,List<Item> * sum_func_list,SELECT_LEX * select_lex,bool returning_field)7511 int setup_wild(THD *thd, TABLE_LIST *tables, List<Item> &fields,
7512 	       List<Item> *sum_func_list, SELECT_LEX *select_lex, bool returning_field)
7513 {
7514   Item *item;
7515   List_iterator<Item> it(fields);
7516   Query_arena *arena, backup;
7517   DBUG_ENTER("setup_wild");
7518 
7519   if (!select_lex->with_wild)
7520     DBUG_RETURN(0);
7521 
7522   /*
7523     Don't use arena if we are not in prepared statements or stored procedures
7524     For PS/SP we have to use arena to remember the changes
7525   */
7526   arena= thd->activate_stmt_arena_if_needed(&backup);
7527 
7528   thd->lex->current_select->cur_pos_in_select_list= 0;
7529   while (select_lex->with_wild && (item= it++))
7530   {
7531     if (item->type() == Item::FIELD_ITEM &&
7532         ((Item_field*) item)->field_name.str == star_clex_str.str &&
7533 	!((Item_field*) item)->field)
7534     {
7535       uint elem= fields.elements;
7536       bool any_privileges= ((Item_field *) item)->any_privileges;
7537       Item_subselect *subsel= thd->lex->current_select->master_unit()->item;
7538       if (subsel &&
7539           subsel->substype() == Item_subselect::EXISTS_SUBS)
7540       {
7541         /*
7542           It is EXISTS(SELECT * ...) and we can replace * by any constant.
7543 
7544           Item_int do not need fix_fields() because it is basic constant.
7545         */
7546         it.replace(new (thd->mem_root) Item_int(thd, "Not_used", (longlong) 1,
7547                                 MY_INT64_NUM_DECIMAL_DIGITS));
7548       }
7549       else if (insert_fields(thd, ((Item_field*) item)->context,
7550                              ((Item_field*) item)->db_name.str,
7551                              ((Item_field*) item)->table_name.str, &it,
7552                              any_privileges, &select_lex->hidden_bit_fields, returning_field))
7553       {
7554 	if (arena)
7555 	  thd->restore_active_arena(arena, &backup);
7556 	DBUG_RETURN(-1);
7557       }
7558       if (sum_func_list)
7559       {
7560 	/*
7561 	  sum_func_list is a list that has the fields list as a tail.
7562 	  Because of this we have to update the element count also for this
7563 	  list after expanding the '*' entry.
7564 	*/
7565 	sum_func_list->elements+= fields.elements - elem;
7566       }
7567       select_lex->with_wild--;
7568     }
7569     else
7570       thd->lex->current_select->cur_pos_in_select_list++;
7571   }
7572   DBUG_ASSERT(!select_lex->with_wild);
7573   thd->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
7574   if (arena)
7575     thd->restore_active_arena(arena, &backup);
7576   DBUG_RETURN(0);
7577 }
7578 
7579 /****************************************************************************
7580 ** Check that all given fields exists and fill struct with current data
7581 ****************************************************************************/
7582 
setup_fields(THD * thd,Ref_ptr_array ref_pointer_array,List<Item> & fields,enum_column_usage column_usage,List<Item> * sum_func_list,List<Item> * pre_fix,bool allow_sum_func)7583 bool setup_fields(THD *thd, Ref_ptr_array ref_pointer_array,
7584                   List<Item> &fields, enum_column_usage column_usage,
7585                   List<Item> *sum_func_list, List<Item> *pre_fix,
7586                   bool allow_sum_func)
7587 {
7588   Item *item;
7589   enum_column_usage saved_column_usage= thd->column_usage;
7590   nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
7591   List_iterator<Item> it(fields);
7592   bool save_is_item_list_lookup;
7593   bool make_pre_fix= (pre_fix && (pre_fix->elements == 0));
7594   DBUG_ENTER("setup_fields");
7595   DBUG_PRINT("enter", ("ref_pointer_array: %p", ref_pointer_array.array()));
7596 
7597   thd->column_usage= column_usage;
7598   DBUG_PRINT("info", ("thd->column_usage: %d", thd->column_usage));
7599   /*
7600     Followimg 2 condition always should be true (but they was added
7601     due to an error present only in 10.3):
7602     1) nest_level shoud be 0 or positive;
7603     2) nest level of all SELECTs on the same level shoud be equal first
7604        SELECT on this level (and each other).
7605   */
7606   DBUG_ASSERT(thd->lex->current_select->nest_level >= 0);
7607   DBUG_ASSERT(thd->lex->current_select->master_unit()->first_select()
7608                 ->nest_level ==
7609               thd->lex->current_select->nest_level);
7610   if (allow_sum_func)
7611     thd->lex->allow_sum_func.set_bit(thd->lex->current_select->nest_level);
7612   thd->where= THD::DEFAULT_WHERE;
7613   save_is_item_list_lookup= thd->lex->current_select->is_item_list_lookup;
7614   thd->lex->current_select->is_item_list_lookup= 0;
7615 
7616   /*
7617     To prevent fail on forward lookup we fill it with zeroes,
7618     then if we got pointer on zero after find_item_in_list we will know
7619     that it is forward lookup.
7620 
7621     There is other way to solve problem: fill array with pointers to list,
7622     but it will be slower.
7623 
7624     TODO: remove it when (if) we made one list for allfields and
7625     ref_pointer_array
7626   */
7627   if (!ref_pointer_array.is_null())
7628   {
7629     DBUG_ASSERT(ref_pointer_array.size() >= fields.elements);
7630     memset(ref_pointer_array.array(), 0, sizeof(Item *) * fields.elements);
7631   }
7632 
7633   /*
7634     We call set_entry() there (before fix_fields() of the whole list of field
7635     items) because:
7636     1) the list of field items has same order as in the query, and the
7637        Item_func_get_user_var item may go before the Item_func_set_user_var:
7638           SELECT @a, @a := 10 FROM t;
7639     2) The entry->update_query_id value controls constantness of
7640        Item_func_get_user_var items, so in presence of Item_func_set_user_var
7641        items we have to refresh their entries before fixing of
7642        Item_func_get_user_var items.
7643   */
7644   List_iterator<Item_func_set_user_var> li(thd->lex->set_var_list);
7645   Item_func_set_user_var *var;
7646   while ((var= li++))
7647     var->set_entry(thd, FALSE);
7648 
7649   Ref_ptr_array ref= ref_pointer_array;
7650   thd->lex->current_select->cur_pos_in_select_list= 0;
7651   while ((item= it++))
7652   {
7653     if (make_pre_fix)
7654       pre_fix->push_back(item, thd->stmt_arena->mem_root);
7655 
7656     if (item->fix_fields_if_needed_for_scalar(thd, it.ref()))
7657     {
7658       thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
7659       thd->lex->allow_sum_func= save_allow_sum_func;
7660       thd->column_usage= saved_column_usage;
7661       DBUG_PRINT("info", ("thd->column_usage: %d", thd->column_usage));
7662       DBUG_RETURN(TRUE); /* purecov: inspected */
7663     }
7664     item= *(it.ref()); // Item might have changed in fix_fields()
7665     if (!ref.is_null())
7666     {
7667       ref[0]= item;
7668       ref.pop_front();
7669     }
7670     /*
7671       split_sum_func() must be called for Window Function items, see
7672       Item_window_func::split_sum_func.
7673     */
7674     if (sum_func_list &&
7675          ((item->with_sum_func() && item->type() != Item::SUM_FUNC_ITEM) ||
7676           item->with_window_func))
7677     {
7678       item->split_sum_func(thd, ref_pointer_array, *sum_func_list,
7679                            SPLIT_SUM_SELECT);
7680     }
7681     thd->lex->current_select->select_list_tables|= item->used_tables();
7682     thd->lex->used_tables|= item->used_tables();
7683     thd->lex->current_select->cur_pos_in_select_list++;
7684   }
7685   thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
7686   thd->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
7687 
7688   thd->lex->allow_sum_func= save_allow_sum_func;
7689   thd->column_usage= saved_column_usage;
7690   DBUG_PRINT("info", ("thd->column_usage: %d", thd->column_usage));
7691   DBUG_RETURN(MY_TEST(thd->is_error()));
7692 }
7693 
7694 
7695 /*
7696   Perform checks like all given fields exists, if exists fill struct with
7697   current data and expand all '*' in given fields for LEX::returning.
7698 
7699   SYNOPSIS
7700      thd                 Thread handler
7701      table_list          Global/local table list
7702 */
7703 
setup_returning_fields(THD * thd,TABLE_LIST * table_list)7704 int setup_returning_fields(THD* thd, TABLE_LIST* table_list)
7705 {
7706   if (!thd->lex->has_returning())
7707     return 0;
7708   return setup_wild(thd, table_list, thd->lex->returning()->item_list, NULL,
7709                     thd->lex->returning(), true)
7710       || setup_fields(thd, Ref_ptr_array(), thd->lex->returning()->item_list,
7711                       MARK_COLUMNS_READ, NULL, NULL, false);
7712 }
7713 
7714 
7715 /*
7716   make list of leaves of join table tree
7717 
7718   SYNOPSIS
7719     make_leaves_list()
7720     list    pointer to pointer on list first element
7721     tables  table list
7722     full_table_list whether to include tables from mergeable derived table/view.
7723                     we need them for checks for INSERT/UPDATE statements only.
7724 
7725   RETURN pointer on pointer to next_leaf of last element
7726 */
7727 
make_leaves_list(THD * thd,List<TABLE_LIST> & list,TABLE_LIST * tables,bool full_table_list,TABLE_LIST * boundary)7728 void make_leaves_list(THD *thd, List<TABLE_LIST> &list, TABLE_LIST *tables,
7729                       bool full_table_list, TABLE_LIST *boundary)
7730 
7731 {
7732   for (TABLE_LIST *table= tables; table; table= table->next_local)
7733   {
7734     if (table == boundary)
7735       full_table_list= !full_table_list;
7736     if (full_table_list && table->is_merged_derived())
7737     {
7738       SELECT_LEX *select_lex= table->get_single_select();
7739       /*
7740         It's safe to use select_lex->leaf_tables because all derived
7741         tables/views were already prepared and has their leaf_tables
7742         set properly.
7743       */
7744       make_leaves_list(thd, list, select_lex->get_table_list(),
7745       full_table_list, boundary);
7746     }
7747     else
7748     {
7749       list.push_back(table, thd->mem_root);
7750     }
7751   }
7752 }
7753 
7754 /*
7755   prepare tables
7756 
7757   SYNOPSIS
7758     setup_tables()
7759     thd		  Thread handler
7760     context       name resolution contest to setup table list there
7761     from_clause   Top-level list of table references in the FROM clause
7762     tables	  Table list (select_lex->table_list)
7763     leaves        List of join table leaves list (select_lex->leaf_tables)
7764     refresh       It is only refresh for subquery
7765     select_insert It is SELECT ... INSERT command
7766     full_table_list a parameter to pass to the make_leaves_list function
7767 
7768   NOTE
7769     Check also that the 'used keys' and 'ignored keys' exists and set up the
7770     table structure accordingly.
7771     Create a list of leaf tables. For queries with NATURAL/USING JOINs,
7772     compute the row types of the top most natural/using join table references
7773     and link these into a list of table references for name resolution.
7774 
7775     This has to be called for all tables that are used by items, as otherwise
7776     table->map is not set and all Item_field will be regarded as const items.
7777 
7778   RETURN
7779     FALSE ok;  In this case *map will includes the chosen index
7780     TRUE  error
7781 */
7782 
setup_tables(THD * thd,Name_resolution_context * context,List<TABLE_LIST> * from_clause,TABLE_LIST * tables,List<TABLE_LIST> & leaves,bool select_insert,bool full_table_list)7783 bool setup_tables(THD *thd, Name_resolution_context *context,
7784                   List<TABLE_LIST> *from_clause, TABLE_LIST *tables,
7785                   List<TABLE_LIST> &leaves, bool select_insert,
7786                   bool full_table_list)
7787 {
7788   uint tablenr= 0;
7789   List_iterator<TABLE_LIST> ti(leaves);
7790   TABLE_LIST *table_list;
7791 
7792   DBUG_ENTER("setup_tables");
7793 
7794   DBUG_ASSERT ((select_insert && !tables->next_name_resolution_table) || !tables ||
7795                (context->table_list && context->first_name_resolution_table));
7796   /*
7797     this is used for INSERT ... SELECT.
7798     For select we setup tables except first (and its underlying tables)
7799   */
7800   TABLE_LIST *first_select_table= (select_insert ?
7801                                    tables->next_local:
7802                                    0);
7803   SELECT_LEX *select_lex= select_insert ? thd->lex->first_select_lex() :
7804                                           thd->lex->current_select;
7805   if (select_lex->first_cond_optimization)
7806   {
7807     leaves.empty();
7808     if (select_lex->prep_leaf_list_state != SELECT_LEX::SAVED)
7809     {
7810       make_leaves_list(thd, leaves, tables, full_table_list, first_select_table);
7811       select_lex->prep_leaf_list_state= SELECT_LEX::READY;
7812       select_lex->leaf_tables_exec.empty();
7813     }
7814     else
7815     {
7816       List_iterator_fast <TABLE_LIST> ti(select_lex->leaf_tables_prep);
7817       while ((table_list= ti++))
7818         leaves.push_back(table_list, thd->mem_root);
7819     }
7820 
7821     while ((table_list= ti++))
7822     {
7823       TABLE *table= table_list->table;
7824       if (table)
7825         table->pos_in_table_list= table_list;
7826       if (first_select_table &&
7827           table_list->top_table() == first_select_table)
7828       {
7829         /* new counting for SELECT of INSERT ... SELECT command */
7830         first_select_table= 0;
7831         thd->lex->first_select_lex()->insert_tables= tablenr;
7832         tablenr= 0;
7833       }
7834       if(table_list->jtbm_subselect)
7835       {
7836         table_list->jtbm_table_no= tablenr;
7837       }
7838       else if (table)
7839       {
7840         table->pos_in_table_list= table_list;
7841         setup_table_map(table, table_list, tablenr);
7842 
7843         if (table_list->process_index_hints(table))
7844           DBUG_RETURN(1);
7845       }
7846       tablenr++;
7847       /*
7848         We test the max tables here as we setup_table_map() should not be called
7849         with tablenr >= 64
7850       */
7851       if (tablenr > MAX_TABLES)
7852       {
7853         my_error(ER_TOO_MANY_TABLES,MYF(0), static_cast<int>(MAX_TABLES));
7854         DBUG_RETURN(1);
7855       }
7856     }
7857   }
7858   else
7859   {
7860     List_iterator_fast <TABLE_LIST> ti(select_lex->leaf_tables_exec);
7861     select_lex->leaf_tables.empty();
7862     while ((table_list= ti++))
7863     {
7864       if(table_list->jtbm_subselect)
7865       {
7866         table_list->jtbm_table_no= table_list->tablenr_exec;
7867       }
7868       else
7869       {
7870         table_list->table->tablenr= table_list->tablenr_exec;
7871         table_list->table->map= table_list->map_exec;
7872         table_list->table->maybe_null= table_list->maybe_null_exec;
7873         table_list->table->pos_in_table_list= table_list;
7874         if (table_list->process_index_hints(table_list->table))
7875           DBUG_RETURN(1);
7876       }
7877       select_lex->leaf_tables.push_back(table_list);
7878     }
7879   }
7880 
7881   for (table_list= tables;
7882        table_list;
7883        table_list= table_list->next_local)
7884   {
7885     if (table_list->merge_underlying_list)
7886     {
7887       DBUG_ASSERT(table_list->is_merged_derived());
7888       Query_arena *arena, backup;
7889       arena= thd->activate_stmt_arena_if_needed(&backup);
7890       bool res;
7891       res= table_list->setup_underlying(thd);
7892       if (arena)
7893         thd->restore_active_arena(arena, &backup);
7894       if (res)
7895         DBUG_RETURN(1);
7896     }
7897 
7898     if (table_list->jtbm_subselect)
7899     {
7900       Item *item= table_list->jtbm_subselect->optimizer;
7901       if (!table_list->jtbm_subselect->optimizer->fixed &&
7902           table_list->jtbm_subselect->optimizer->fix_fields(thd, &item))
7903       {
7904         my_error(ER_TOO_MANY_TABLES,MYF(0), static_cast<int>(MAX_TABLES)); /* psergey-todo: WHY ER_TOO_MANY_TABLES ???*/
7905         DBUG_RETURN(1);
7906       }
7907       DBUG_ASSERT(item == table_list->jtbm_subselect->optimizer);
7908     }
7909   }
7910 
7911   /* Precompute and store the row types of NATURAL/USING joins. */
7912   if (setup_natural_join_row_types(thd, from_clause, context))
7913     DBUG_RETURN(1);
7914 
7915   DBUG_RETURN(0);
7916 }
7917 
7918 
7919 /*
7920   prepare tables and check access for the view tables
7921 
7922   SYNOPSIS
7923     setup_tables_and_check_access()
7924     thd		  Thread handler
7925     context       name resolution contest to setup table list there
7926     from_clause   Top-level list of table references in the FROM clause
7927     tables	  Table list (select_lex->table_list)
7928     conds	  Condition of current SELECT (can be changed by VIEW)
7929     leaves        List of join table leaves list (select_lex->leaf_tables)
7930     refresh       It is onle refresh for subquery
7931     select_insert It is SELECT ... INSERT command
7932     want_access   what access is needed
7933     full_table_list a parameter to pass to the make_leaves_list function
7934 
7935   NOTE
7936     a wrapper for check_tables that will also check the resulting
7937     table leaves list for access to all the tables that belong to a view
7938 
7939   RETURN
7940     FALSE ok;  In this case *map will include the chosen index
7941     TRUE  error
7942 */
setup_tables_and_check_access(THD * thd,Name_resolution_context * context,List<TABLE_LIST> * from_clause,TABLE_LIST * tables,List<TABLE_LIST> & leaves,bool select_insert,privilege_t want_access_first,privilege_t want_access,bool full_table_list)7943 bool setup_tables_and_check_access(THD *thd, Name_resolution_context *context,
7944                                    List<TABLE_LIST> *from_clause,
7945                                    TABLE_LIST *tables,
7946                                    List<TABLE_LIST> &leaves,
7947                                    bool select_insert,
7948                                    privilege_t want_access_first,
7949                                    privilege_t want_access,
7950                                    bool full_table_list)
7951 {
7952   DBUG_ENTER("setup_tables_and_check_access");
7953 
7954   if (setup_tables(thd, context, from_clause, tables,
7955                    leaves, select_insert, full_table_list))
7956     DBUG_RETURN(TRUE);
7957 
7958   List_iterator<TABLE_LIST> ti(leaves);
7959   TABLE_LIST *table_list;
7960   privilege_t access= want_access_first;
7961   while ((table_list= ti++))
7962   {
7963     if (table_list->belong_to_view && !table_list->view &&
7964         check_single_table_access(thd, access, table_list, FALSE))
7965     {
7966       tables->hide_view_error(thd);
7967       DBUG_RETURN(TRUE);
7968     }
7969     access= want_access;
7970   }
7971   DBUG_RETURN(FALSE);
7972 }
7973 
7974 
7975 /*
7976    Create a key_map from a list of index names
7977 
7978    SYNOPSIS
7979      get_key_map_from_key_list()
7980      map		key_map to fill in
7981      table		Table
7982      index_list		List of index names
7983 
7984    RETURN
7985      0	ok;  In this case *map will includes the choosed index
7986      1	error
7987 */
7988 
get_key_map_from_key_list(key_map * map,TABLE * table,List<String> * index_list)7989 bool get_key_map_from_key_list(key_map *map, TABLE *table,
7990                                List<String> *index_list)
7991 {
7992   List_iterator_fast<String> it(*index_list);
7993   String *name;
7994   uint pos;
7995 
7996   map->clear_all();
7997   while ((name=it++))
7998   {
7999     if (table->s->keynames.type_names == 0 ||
8000         (pos= find_type(&table->s->keynames, name->ptr(),
8001                         name->length(), 1)) <=
8002         0)
8003     {
8004       my_error(ER_KEY_DOES_NOT_EXISTS, MYF(0), name->c_ptr(),
8005 	       table->pos_in_table_list->alias.str);
8006       map->set_all();
8007       return 1;
8008     }
8009     map->set_bit(pos-1);
8010   }
8011   return 0;
8012 }
8013 
8014 
8015 /*
8016   Drops in all fields instead of current '*' field
8017 
8018   SYNOPSIS
8019     insert_fields()
8020     thd			Thread handler
8021     context             Context for name resolution
8022     db_name		Database name in case of 'database_name.table_name.*'
8023     table_name		Table name in case of 'table_name.*'
8024     it			Pointer to '*'
8025     any_privileges	0 If we should ensure that we have SELECT privileges
8026 		          for all columns
8027                         1 If any privilege is ok
8028   RETURN
8029     0	ok     'it' is updated to point at last inserted
8030     1	error.  Error message is generated but not sent to client
8031 */
8032 
8033 bool
insert_fields(THD * thd,Name_resolution_context * context,const char * db_name,const char * table_name,List_iterator<Item> * it,bool any_privileges,uint * hidden_bit_fields,bool returning_field)8034 insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
8035 	      const char *table_name, List_iterator<Item> *it,
8036               bool any_privileges, uint *hidden_bit_fields, bool returning_field)
8037 {
8038   Field_iterator_table_ref field_iterator;
8039   bool found;
8040   char name_buff[SAFE_NAME_LEN+1];
8041   DBUG_ENTER("insert_fields");
8042   DBUG_PRINT("arena", ("stmt arena: %p",thd->stmt_arena));
8043 
8044   if (db_name && lower_case_table_names)
8045   {
8046     /*
8047       convert database to lower case for comparison
8048       We can't do this in Item_field as this would change the
8049       'name' of the item which may be used in the select list
8050     */
8051     strmake_buf(name_buff, db_name);
8052     my_casedn_str(files_charset_info, name_buff);
8053     db_name= name_buff;
8054   }
8055 
8056   found= FALSE;
8057 
8058   /*
8059     If table names are qualified, then loop over all tables used in the query,
8060     else treat natural joins as leaves and do not iterate over their underlying
8061     tables.
8062   */
8063   TABLE_LIST *first= context->first_name_resolution_table;
8064   TABLE_LIST *TABLE_LIST::* next= &TABLE_LIST::next_name_resolution_table;
8065   if (table_name && !returning_field)
8066   {
8067     first= context->table_list;
8068     next= &TABLE_LIST::next_local;
8069   }
8070   for (TABLE_LIST *tables= first; tables; tables= tables->*next)
8071   {
8072     Field *field;
8073     TABLE *table= tables->table;
8074 
8075     DBUG_ASSERT(tables->is_leaf_for_name_resolution());
8076 
8077     if ((table_name && my_strcasecmp(table_alias_charset, table_name,
8078                                      tables->alias.str)) ||
8079         (db_name && strcmp(tables->db.str, db_name)))
8080       continue;
8081 
8082 #ifndef NO_EMBEDDED_ACCESS_CHECKS
8083     /*
8084        Ensure that we have access rights to all fields to be inserted
8085        the table 'tables'. Under some circumstances, this check may be skipped.
8086 
8087        The check is skipped in the following cases:
8088 
8089        - any_privileges is true
8090 
8091        - the table is a derived table
8092 
8093        - the table is a view with SELECT privilege
8094 
8095        - the table is a base table with SELECT privilege
8096     */
8097     if (!any_privileges &&
8098         !tables->is_derived() &&
8099         !(tables->is_view() && (tables->grant.privilege & SELECT_ACL)) &&
8100         !(table && (table->grant.privilege & SELECT_ACL)))
8101     {
8102       field_iterator.set(tables);
8103       if (check_grant_all_columns(thd, SELECT_ACL, &field_iterator))
8104         DBUG_RETURN(TRUE);
8105     }
8106 #endif
8107 
8108     /*
8109       Update the tables used in the query based on the referenced fields. For
8110       views and natural joins this update is performed inside the loop below.
8111     */
8112     if (table)
8113     {
8114       thd->lex->used_tables|= table->map;
8115       thd->lex->current_select->select_list_tables|= table->map;
8116     }
8117 
8118     /*
8119       Initialize a generic field iterator for the current table reference.
8120       Notice that it is guaranteed that this iterator will iterate over the
8121       fields of a single table reference, because 'tables' is a leaf (for
8122       name resolution purposes).
8123     */
8124     field_iterator.set(tables);
8125 
8126     for (; !field_iterator.end_of_fields(); field_iterator.next())
8127     {
8128       /*
8129         field() is always NULL for views (see, e.g. Field_iterator_view or
8130         Field_iterator_natural_join).
8131         But view fields can never be invisible.
8132       */
8133       if ((field= field_iterator.field()) && field->invisible != VISIBLE)
8134         continue;
8135 
8136       Item *item;
8137 
8138       if (!(item= field_iterator.create_item(thd)))
8139         DBUG_RETURN(TRUE);
8140 
8141       /* cache the table for the Item_fields inserted by expanding stars */
8142       if (item->type() == Item::FIELD_ITEM && tables->cacheable_table)
8143         ((Item_field *)item)->cached_table= tables;
8144 
8145       if (!found)
8146       {
8147         found= TRUE;
8148         it->replace(item); /* Replace '*' with the first found item. */
8149       }
8150       else
8151         it->after(item);   /* Add 'item' to the SELECT list. */
8152 
8153       if (item->type() == Item::FIELD_ITEM && item->field_type() == MYSQL_TYPE_BIT)
8154         (*hidden_bit_fields)++;
8155 
8156 #ifndef NO_EMBEDDED_ACCESS_CHECKS
8157       /*
8158         Set privilege information for the fields of newly created views.
8159         We have that (any_priviliges == TRUE) if and only if we are creating
8160         a view. In the time of view creation we can't use the MERGE algorithm,
8161         therefore if 'tables' is itself a view, it is represented by a
8162         temporary table. Thus in this case we can be sure that 'item' is an
8163         Item_field.
8164       */
8165       if (any_privileges && !tables->is_with_table() && !tables->is_derived())
8166       {
8167         DBUG_ASSERT((tables->field_translation == NULL && table) ||
8168                     tables->is_natural_join);
8169         DBUG_ASSERT(item->type() == Item::FIELD_ITEM);
8170         Item_field *fld= (Item_field*) item;
8171         const char *field_table_name= field_iterator.get_table_name();
8172 
8173         if (!tables->schema_table &&
8174             !(fld->have_privileges=
8175               (get_column_grant(thd, field_iterator.grant(),
8176                                 field_iterator.get_db_name(),
8177                                 field_table_name, fld->field_name.str) &
8178                VIEW_ANY_ACL)))
8179         {
8180           my_error(ER_TABLEACCESS_DENIED_ERROR, MYF(0), "ANY",
8181                    thd->security_ctx->priv_user,
8182                    thd->security_ctx->host_or_ip,
8183                    field_table_name);
8184           DBUG_RETURN(TRUE);
8185         }
8186       }
8187 #endif
8188 
8189       if ((field= field_iterator.field()))
8190       {
8191         field->table->mark_column_with_deps(field);
8192         if (table)
8193           table->covering_keys.intersect(field->part_of_key);
8194         if (tables->is_natural_join)
8195         {
8196           TABLE *field_table;
8197           /*
8198             In this case we are sure that the column ref will not be created
8199             because it was already created and stored with the natural join.
8200           */
8201           Natural_join_column *nj_col;
8202           if (!(nj_col= field_iterator.get_natural_column_ref()))
8203             DBUG_RETURN(TRUE);
8204           DBUG_ASSERT(nj_col->table_field);
8205           field_table= nj_col->table_ref->table;
8206           if (field_table)
8207           {
8208             thd->lex->used_tables|= field_table->map;
8209             thd->lex->current_select->select_list_tables|=
8210               field_table->map;
8211             field_table->covering_keys.intersect(field->part_of_key);
8212             field_table->used_fields++;
8213           }
8214         }
8215       }
8216       else
8217         thd->lex->used_tables|= item->used_tables();
8218       thd->lex->current_select->cur_pos_in_select_list++;
8219     }
8220     /*
8221       In case of stored tables, all fields are considered as used,
8222       while in the case of views, the fields considered as used are the
8223       ones marked in setup_tables during fix_fields of view columns.
8224       For NATURAL joins, used_tables is updated in the IF above.
8225     */
8226     if (table)
8227       table->used_fields= table->s->fields;
8228   }
8229   if (found)
8230     DBUG_RETURN(FALSE);
8231 
8232   /*
8233     TODO: in the case when we skipped all columns because there was a
8234     qualified '*', and all columns were coalesced, we have to give a more
8235     meaningful message than ER_BAD_TABLE_ERROR.
8236   */
8237   if (!table_name)
8238     my_error(ER_NO_TABLES_USED, MYF(0));
8239   else if (!db_name && !thd->db.str)
8240     my_error(ER_NO_DB_ERROR, MYF(0));
8241   else
8242   {
8243     char name[FN_REFLEN];
8244     my_snprintf(name, sizeof(name), "%s.%s",
8245                 db_name ? db_name : thd->get_db(), table_name);
8246     my_error(ER_BAD_TABLE_ERROR, MYF(0), name);
8247   }
8248 
8249   DBUG_RETURN(TRUE);
8250 }
8251 
8252 
8253 /**
8254   Wrap Item_ident
8255 
8256   @param thd             thread handle
8257   @param conds           pointer to the condition which should be wrapped
8258 */
8259 
wrap_ident(THD * thd,Item ** conds)8260 void wrap_ident(THD *thd, Item **conds)
8261 {
8262   Item_direct_ref_to_ident *wrapper;
8263   DBUG_ASSERT((*conds)->type() == Item::FIELD_ITEM || (*conds)->type() == Item::REF_ITEM);
8264   Query_arena *arena, backup;
8265   arena= thd->activate_stmt_arena_if_needed(&backup);
8266   if ((wrapper= new (thd->mem_root) Item_direct_ref_to_ident(thd, (Item_ident *) (*conds))))
8267     (*conds)= (Item*) wrapper;
8268   if (arena)
8269     thd->restore_active_arena(arena, &backup);
8270 }
8271 
8272 /**
8273   Prepare ON expression
8274 
8275   @param thd             Thread handle
8276   @param table           Pointer to table list
8277   @param is_update       Update flag
8278 
8279   @retval TRUE error.
8280   @retval FALSE OK.
8281 */
8282 
setup_on_expr(THD * thd,TABLE_LIST * table,bool is_update)8283 bool setup_on_expr(THD *thd, TABLE_LIST *table, bool is_update)
8284 {
8285   uchar buff[STACK_BUFF_ALLOC];			// Max argument in function
8286   if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
8287     return TRUE;				// Fatal error flag is set!
8288   for(; table; table= table->next_local)
8289   {
8290     TABLE_LIST *embedded; /* The table at the current level of nesting. */
8291     TABLE_LIST *embedding= table; /* The parent nested table reference. */
8292     do
8293     {
8294       embedded= embedding;
8295       if (embedded->on_expr)
8296       {
8297         thd->where="on clause";
8298         embedded->on_expr->mark_as_condition_AND_part(embedded);
8299         if (embedded->on_expr->fix_fields_if_needed_for_bool(thd,
8300                                                            &embedded->on_expr))
8301           return TRUE;
8302       }
8303       /*
8304         If it's a semi-join nest, fix its "left expression", as it is used by
8305         the SJ-Materialization
8306       */
8307       if (embedded->sj_subq_pred)
8308       {
8309         Item **left_expr= embedded->sj_subq_pred->left_exp_ptr();
8310         if ((*left_expr)->fix_fields_if_needed(thd, left_expr))
8311           return TRUE;
8312       }
8313 
8314       embedding= embedded->embedding;
8315     }
8316     while (embedding &&
8317            embedding->nested_join->join_list.head() == embedded);
8318 
8319     if (table->is_merged_derived())
8320     {
8321       SELECT_LEX *select_lex= table->get_single_select();
8322       setup_on_expr(thd, select_lex->get_table_list(), is_update);
8323     }
8324 
8325     /* process CHECK OPTION */
8326     if (is_update)
8327     {
8328       TABLE_LIST *view= table->top_table();
8329       if (view->effective_with_check)
8330       {
8331         if (view->prepare_check_option(thd))
8332           return TRUE;
8333         thd->change_item_tree(&table->check_option, view->check_option);
8334       }
8335     }
8336   }
8337   return FALSE;
8338 }
8339 
8340 /*
8341   Fix all conditions and outer join expressions.
8342 
8343   SYNOPSIS
8344     setup_conds()
8345     thd     thread handler
8346     tables  list of tables for name resolving (select_lex->table_list)
8347     leaves  list of leaves of join table tree (select_lex->leaf_tables)
8348     conds   WHERE clause
8349 
8350   DESCRIPTION
8351     TODO
8352 
8353   RETURN
8354     TRUE  if some error occurred (e.g. out of memory)
8355     FALSE if all is OK
8356 */
8357 
setup_conds(THD * thd,TABLE_LIST * tables,List<TABLE_LIST> & leaves,COND ** conds)8358 int setup_conds(THD *thd, TABLE_LIST *tables, List<TABLE_LIST> &leaves,
8359                 COND **conds)
8360 {
8361   SELECT_LEX *select_lex= thd->lex->current_select;
8362   TABLE_LIST *table= NULL;	// For HP compilers
8363   /*
8364     it_is_update set to TRUE when tables of primary SELECT_LEX (SELECT_LEX
8365     which belong to LEX, i.e. most up SELECT) will be updated by
8366     INSERT/UPDATE/LOAD
8367     NOTE: using this condition helps to prevent call of prepare_check_option()
8368     from subquery of VIEW, because tables of subquery belongs to VIEW
8369     (see condition before prepare_check_option() call)
8370   */
8371   bool it_is_update= (select_lex == thd->lex->first_select_lex()) &&
8372     thd->lex->which_check_option_applicable();
8373   bool save_is_item_list_lookup= select_lex->is_item_list_lookup;
8374   TABLE_LIST *derived= select_lex->master_unit()->derived;
8375   DBUG_ENTER("setup_conds");
8376 
8377   select_lex->is_item_list_lookup= 0;
8378 
8379   thd->column_usage= MARK_COLUMNS_READ;
8380   DBUG_PRINT("info", ("thd->column_usage: %d", thd->column_usage));
8381   select_lex->cond_count= 0;
8382   select_lex->between_count= 0;
8383   select_lex->max_equal_elems= 0;
8384 
8385   for (table= tables; table; table= table->next_local)
8386   {
8387     if (select_lex == thd->lex->first_select_lex() &&
8388         select_lex->first_cond_optimization &&
8389         table->merged_for_insert &&
8390         table->prepare_where(thd, conds, FALSE))
8391       goto err_no_arena;
8392   }
8393 
8394   if (*conds)
8395   {
8396     thd->where="where clause";
8397     DBUG_EXECUTE("where",
8398                  print_where(*conds,
8399                              "WHERE in setup_conds",
8400                              QT_ORDINARY););
8401     /*
8402       Wrap alone field in WHERE clause in case it will be outer field of subquery
8403       which need persistent pointer on it, but conds could be changed by optimizer
8404     */
8405     if ((*conds)->type() == Item::FIELD_ITEM && !derived)
8406       wrap_ident(thd, conds);
8407     (*conds)->mark_as_condition_AND_part(NO_JOIN_NEST);
8408     if ((*conds)->fix_fields_if_needed_for_bool(thd, conds))
8409       goto err_no_arena;
8410   }
8411 
8412   /*
8413     Apply fix_fields() to all ON clauses at all levels of nesting,
8414     including the ones inside view definitions.
8415   */
8416   if (setup_on_expr(thd, tables, it_is_update))
8417     goto err_no_arena;
8418 
8419   if (!thd->stmt_arena->is_conventional())
8420   {
8421     /*
8422       We are in prepared statement preparation code => we should store
8423       WHERE clause changing for next executions.
8424 
8425       We do this ON -> WHERE transformation only once per PS/SP statement.
8426     */
8427     select_lex->where= *conds;
8428   }
8429   thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
8430   DBUG_RETURN(MY_TEST(thd->is_error()));
8431 
8432 err_no_arena:
8433   select_lex->is_item_list_lookup= save_is_item_list_lookup;
8434   DBUG_RETURN(1);
8435 }
8436 
8437 
8438 /******************************************************************************
8439 ** Fill a record with data (for INSERT or UPDATE)
8440 ** Returns : 1 if some field has wrong type
8441 ******************************************************************************/
8442 
8443 
8444 /**
8445   Fill the fields of a table with the values of an Item list
8446 
8447   @param thd           thread handler
8448   @param table_arg     the table that is being modified
8449   @param fields        Item_fields list to be filled
8450   @param values        values to fill with
8451   @param ignore_errors TRUE if we should ignore errors
8452   @param update        TRUE if update query
8453 
8454   @details
8455     fill_record() may set table->auto_increment_field_not_null and a
8456     caller should make sure that it is reset after their last call to this
8457     function.
8458     default functions are executed for inserts.
8459     virtual fields are always updated
8460 
8461   @return Status
8462   @retval true An error occurred.
8463   @retval false OK.
8464 */
8465 
8466 bool
fill_record(THD * thd,TABLE * table_arg,List<Item> & fields,List<Item> & values,bool ignore_errors,bool update)8467 fill_record(THD *thd, TABLE *table_arg, List<Item> &fields, List<Item> &values,
8468             bool ignore_errors, bool update)
8469 {
8470   List_iterator_fast<Item> f(fields),v(values);
8471   Item *value, *fld;
8472   Item_field *field;
8473   Field *rfield;
8474   TABLE *table;
8475   bool only_unvers_fields= update && table_arg->versioned();
8476   bool save_abort_on_warning= thd->abort_on_warning;
8477   bool save_no_errors= thd->no_errors;
8478   DBUG_ENTER("fill_record");
8479 
8480   thd->no_errors= ignore_errors;
8481   /*
8482     Reset the table->auto_increment_field_not_null as it is valid for
8483     only one row.
8484   */
8485   if (fields.elements)
8486     table_arg->auto_increment_field_not_null= FALSE;
8487 
8488   while ((fld= f++))
8489   {
8490     if (!(field= fld->field_for_view_update()))
8491     {
8492       my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), fld->name.str);
8493       goto err;
8494     }
8495     value=v++;
8496     DBUG_ASSERT(value);
8497     rfield= field->field;
8498     table= rfield->table;
8499     if (table->next_number_field &&
8500         rfield->field_index ==  table->next_number_field->field_index)
8501       table->auto_increment_field_not_null= TRUE;
8502     const bool skip_sys_field= rfield->vers_sys_field(); // TODO: && !thd->vers_modify_history() [MDEV-16546]
8503     if ((rfield->vcol_info || skip_sys_field) &&
8504         !value->vcol_assignment_allowed_value() &&
8505         table->s->table_category != TABLE_CATEGORY_TEMPORARY)
8506     {
8507       push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
8508                           ER_WARNING_NON_DEFAULT_VALUE_FOR_GENERATED_COLUMN,
8509                           ER_THD(thd, ER_WARNING_NON_DEFAULT_VALUE_FOR_GENERATED_COLUMN),
8510                           rfield->field_name.str, table->s->table_name.str);
8511     }
8512     if (only_unvers_fields && !rfield->vers_update_unversioned())
8513       only_unvers_fields= false;
8514 
8515     if (rfield->stored_in_db())
8516     {
8517       if (!skip_sys_field &&
8518           unlikely(value->save_in_field(rfield, 0) < 0) && !ignore_errors)
8519       {
8520         my_message(ER_UNKNOWN_ERROR, ER_THD(thd, ER_UNKNOWN_ERROR), MYF(0));
8521         goto err;
8522       }
8523       /*
8524         In sql MODE_SIMULTANEOUS_ASSIGNMENT,
8525         move field pointer on value stored in record[1]
8526         which contains row before update (see MDEV-13417)
8527       */
8528       if (update && thd->variables.sql_mode & MODE_SIMULTANEOUS_ASSIGNMENT)
8529         rfield->move_field_offset((my_ptrdiff_t) (table->record[1] -
8530                                                   table->record[0]));
8531     }
8532     rfield->set_has_explicit_value();
8533   }
8534 
8535   if (update && thd->variables.sql_mode & MODE_SIMULTANEOUS_ASSIGNMENT)
8536   {
8537     // restore fields pointers on record[0]
8538     f.rewind();
8539     while ((fld= f++))
8540     {
8541       rfield= fld->field_for_view_update()->field;
8542       if (rfield->stored_in_db())
8543       {
8544         table= rfield->table;
8545         rfield->move_field_offset((my_ptrdiff_t) (table->record[0] -
8546                                                   table->record[1]));
8547       }
8548     }
8549   }
8550 
8551   if (update)
8552     table_arg->evaluate_update_default_function();
8553   else
8554     if (table_arg->default_field &&
8555         table_arg->update_default_fields(ignore_errors))
8556       goto err;
8557 
8558   if (table_arg->versioned() && !only_unvers_fields)
8559     table_arg->vers_update_fields();
8560   /* Update virtual fields */
8561   if (table_arg->vfield &&
8562       table_arg->update_virtual_fields(table_arg->file, VCOL_UPDATE_FOR_WRITE))
8563     goto err;
8564   thd->abort_on_warning= save_abort_on_warning;
8565   thd->no_errors=        save_no_errors;
8566   DBUG_RETURN(thd->is_error());
8567 err:
8568   DBUG_PRINT("error",("got error"));
8569   thd->abort_on_warning= save_abort_on_warning;
8570   thd->no_errors=        save_no_errors;
8571   if (fields.elements)
8572     table_arg->auto_increment_field_not_null= FALSE;
8573   DBUG_RETURN(TRUE);
8574 }
8575 
8576 
8577 /**
8578   Prepare Item_field's for fill_record_n_invoke_before_triggers()
8579 
8580   This means redirecting from table->field to
8581   table->field_to_fill(), if needed.
8582 */
switch_to_nullable_trigger_fields(List<Item> & items,TABLE * table)8583 void switch_to_nullable_trigger_fields(List<Item> &items, TABLE *table)
8584 {
8585   Field** field= table->field_to_fill();
8586 
8587  /* True if we have NOT NULL fields and BEFORE triggers */
8588   if (field != table->field)
8589   {
8590     List_iterator_fast<Item> it(items);
8591     Item *item;
8592 
8593     while ((item= it++))
8594       item->walk(&Item::switch_to_nullable_fields_processor, 1, field);
8595     table->triggers->reset_extra_null_bitmap();
8596   }
8597 }
8598 
8599 
8600 /**
8601   Prepare Virtual fields and field with default expressions to use
8602   trigger fields
8603 
8604   This means redirecting from table->field to
8605   table->field_to_fill(), if needed.
8606 */
8607 
switch_defaults_to_nullable_trigger_fields(TABLE * table)8608 void switch_defaults_to_nullable_trigger_fields(TABLE *table)
8609 {
8610   if (!table->default_field)
8611     return; // no defaults
8612 
8613   Field **trigger_field= table->field_to_fill();
8614 
8615  /* True if we have NOT NULL fields and BEFORE triggers */
8616   if (*trigger_field != *table->field)
8617   {
8618     for (Field **field_ptr= table->default_field; *field_ptr ; field_ptr++)
8619     {
8620       Field *field= (*field_ptr);
8621       field->default_value->expr->walk(&Item::switch_to_nullable_fields_processor, 1, trigger_field);
8622       *field_ptr= (trigger_field[field->field_index]);
8623     }
8624   }
8625 }
8626 
8627 
8628 /**
8629   Test NOT NULL constraint after BEFORE triggers
8630 */
not_null_fields_have_null_values(TABLE * table)8631 static bool not_null_fields_have_null_values(TABLE *table)
8632 {
8633   Field **orig_field= table->field;
8634   Field **filled_field= table->field_to_fill();
8635 
8636   if (filled_field != orig_field)
8637   {
8638     THD *thd=table->in_use;
8639     for (uint i=0; i < table->s->fields; i++)
8640     {
8641       Field *of= orig_field[i];
8642       Field *ff= filled_field[i];
8643       if (ff != of)
8644       {
8645         // copy after-update flags to of, copy before-update flags to ff
8646         swap_variables(uint32, of->flags, ff->flags);
8647         if (ff->is_real_null())
8648         {
8649           ff->set_notnull(); // for next row WHERE condition in UPDATE
8650           if (convert_null_to_field_value_or_error(of) || thd->is_error())
8651             return true;
8652         }
8653       }
8654     }
8655   }
8656 
8657   return false;
8658 }
8659 
8660 /**
8661   Fill fields in list with values from the list of items and invoke
8662   before triggers.
8663 
8664   @param thd           thread context
8665   @param table         the table that is being modified
8666   @param fields        Item_fields list to be filled
8667   @param values        values to fill with
8668   @param ignore_errors TRUE if we should ignore errors
8669   @param event         event type for triggers to be invoked
8670 
8671   @detail
8672     This function assumes that fields which values will be set and triggers
8673     to be invoked belong to the same table, and that TABLE::record[0] and
8674     record[1] buffers correspond to new and old versions of row respectively.
8675 
8676   @return Status
8677   @retval true An error occurred.
8678   @retval false OK.
8679 */
8680 
8681 bool
fill_record_n_invoke_before_triggers(THD * thd,TABLE * table,List<Item> & fields,List<Item> & values,bool ignore_errors,enum trg_event_type event)8682 fill_record_n_invoke_before_triggers(THD *thd, TABLE *table,
8683                                      List<Item> &fields,
8684                                      List<Item> &values, bool ignore_errors,
8685                                      enum trg_event_type event)
8686 {
8687   int result;
8688   Table_triggers_list *triggers= table->triggers;
8689 
8690   result= fill_record(thd, table, fields, values, ignore_errors,
8691                       event == TRG_EVENT_UPDATE);
8692 
8693   if (!result && triggers)
8694   {
8695     if (triggers->process_triggers(thd, event, TRG_ACTION_BEFORE,
8696                                     TRUE) ||
8697         not_null_fields_have_null_values(table))
8698       return TRUE;
8699 
8700     /*
8701       Re-calculate virtual fields to cater for cases when base columns are
8702       updated by the triggers.
8703     */
8704     if (table->vfield && fields.elements)
8705     {
8706       Item *fld= (Item_field*) fields.head();
8707       Item_field *item_field= fld->field_for_view_update();
8708       if (item_field)
8709       {
8710         DBUG_ASSERT(table == item_field->field->table);
8711         result|= table->update_virtual_fields(table->file,
8712                                               VCOL_UPDATE_FOR_WRITE);
8713       }
8714     }
8715   }
8716   return result;
8717 }
8718 
8719 
8720 /**
8721   Fill the field buffer of a table with the values of an Item list
8722   All fields are given a value
8723 
8724   @param thd           thread handler
8725   @param table_arg     the table that is being modified
8726   @param ptr           pointer on pointer to record of fields
8727   @param values        values to fill with
8728   @param ignore_errors TRUE if we should ignore errors
8729   @param use_value     forces usage of value of the items instead of result
8730 
8731   @details
8732     fill_record() may set table->auto_increment_field_not_null and a
8733     caller should make sure that it is reset after their last call to this
8734     function.
8735 
8736   @return Status
8737   @retval true An error occurred.
8738   @retval false OK.
8739 */
8740 
8741 bool
fill_record(THD * thd,TABLE * table,Field ** ptr,List<Item> & values,bool ignore_errors,bool use_value)8742 fill_record(THD *thd, TABLE *table, Field **ptr, List<Item> &values,
8743             bool ignore_errors, bool use_value)
8744 {
8745   List_iterator_fast<Item> v(values);
8746   List<TABLE> tbl_list;
8747   Item *value;
8748   Field *field;
8749   bool abort_on_warning_saved= thd->abort_on_warning;
8750   uint autoinc_index= table->next_number_field
8751                         ? table->next_number_field->field_index
8752                         : ~0U;
8753   DBUG_ENTER("fill_record");
8754   if (!*ptr)
8755   {
8756     /* No fields to update, quite strange!*/
8757     DBUG_RETURN(0);
8758   }
8759 
8760   /*
8761     On INSERT or UPDATE fields are checked to be from the same table,
8762     thus we safely can take table from the first field.
8763   */
8764   DBUG_ASSERT((*ptr)->table == table);
8765 
8766   /*
8767     Reset the table->auto_increment_field_not_null as it is valid for
8768     only one row.
8769   */
8770   table->auto_increment_field_not_null= FALSE;
8771   while ((field = *ptr++) && ! thd->is_error())
8772   {
8773     /* Ensure that all fields are from the same table */
8774     DBUG_ASSERT(field->table == table);
8775 
8776     if (unlikely(field->invisible))
8777       continue;
8778 
8779     value=v++;
8780 
8781     bool vers_sys_field= table->versioned() && field->vers_sys_field();
8782 
8783     if (field->field_index == autoinc_index)
8784       table->auto_increment_field_not_null= TRUE;
8785     if ((unlikely(field->vcol_info) || (vers_sys_field && !ignore_errors)) &&
8786         !value->vcol_assignment_allowed_value() &&
8787         table->s->table_category != TABLE_CATEGORY_TEMPORARY)
8788     {
8789       push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
8790                           ER_WARNING_NON_DEFAULT_VALUE_FOR_GENERATED_COLUMN,
8791                           ER_THD(thd, ER_WARNING_NON_DEFAULT_VALUE_FOR_GENERATED_COLUMN),
8792                           field->field_name.str, table->s->table_name.str);
8793       if (vers_sys_field)
8794         continue;
8795     }
8796 
8797     if (use_value)
8798       value->save_val(field);
8799     else
8800       if (value->save_in_field(field, 0) < 0)
8801         goto err;
8802     field->set_has_explicit_value();
8803   }
8804   /* Update virtual fields if there wasn't any errors */
8805   if (!thd->is_error())
8806   {
8807     thd->abort_on_warning= FALSE;
8808     if (table->default_field && table->update_default_fields(ignore_errors))
8809       goto err;
8810     if (table->versioned())
8811       table->vers_update_fields();
8812     if (table->vfield &&
8813         table->update_virtual_fields(table->file, VCOL_UPDATE_FOR_WRITE))
8814       goto err;
8815     thd->abort_on_warning= abort_on_warning_saved;
8816   }
8817   DBUG_RETURN(thd->is_error());
8818 
8819 err:
8820   thd->abort_on_warning= abort_on_warning_saved;
8821   table->auto_increment_field_not_null= FALSE;
8822   DBUG_RETURN(TRUE);
8823 }
8824 
8825 
8826 /*
8827   Fill fields in an array with values from the list of items and invoke
8828   before triggers.
8829 
8830   @param thd           thread context
8831   @param table         the table that is being modified
8832   @param ptr        the fields to be filled
8833   @param values        values to fill with
8834   @param ignore_errors TRUE if we should ignore errors
8835   @param event         event type for triggers to be invoked
8836 
8837   @detail
8838     This function assumes that fields which values will be set and triggers
8839     to be invoked belong to the same table, and that TABLE::record[0] and
8840     record[1] buffers correspond to new and old versions of row respectively.
8841 
8842   @return Status
8843   @retval true An error occurred.
8844   @retval false OK.
8845 */
8846 
8847 bool
fill_record_n_invoke_before_triggers(THD * thd,TABLE * table,Field ** ptr,List<Item> & values,bool ignore_errors,enum trg_event_type event)8848 fill_record_n_invoke_before_triggers(THD *thd, TABLE *table, Field **ptr,
8849                                      List<Item> &values, bool ignore_errors,
8850                                      enum trg_event_type event)
8851 {
8852   bool result;
8853   Table_triggers_list *triggers= table->triggers;
8854 
8855   result= fill_record(thd, table, ptr, values, ignore_errors, FALSE);
8856 
8857   if (!result && triggers && *ptr)
8858     result= triggers->process_triggers(thd, event, TRG_ACTION_BEFORE, TRUE) ||
8859             not_null_fields_have_null_values(table);
8860   /*
8861     Re-calculate virtual fields to cater for cases when base columns are
8862     updated by the triggers.
8863   */
8864   if (!result && triggers && *ptr)
8865   {
8866     DBUG_ASSERT(table == (*ptr)->table);
8867     if (table->vfield)
8868       result= table->update_virtual_fields(table->file, VCOL_UPDATE_FOR_WRITE);
8869   }
8870   return result;
8871 
8872 }
8873 
8874 
mysql_rm_tmp_tables(void)8875 my_bool mysql_rm_tmp_tables(void)
8876 {
8877   uint i, idx;
8878   char	path[FN_REFLEN], *tmpdir, path_copy[FN_REFLEN];
8879   MY_DIR *dirp;
8880   FILEINFO *file;
8881   TABLE_SHARE share;
8882   THD *thd;
8883   DBUG_ENTER("mysql_rm_tmp_tables");
8884 
8885   if (!(thd= new THD(0)))
8886     DBUG_RETURN(1);
8887   thd->thread_stack= (char*) &thd;
8888   thd->store_globals();
8889 
8890   for (i=0; i<=mysql_tmpdir_list.max; i++)
8891   {
8892     tmpdir=mysql_tmpdir_list.list[i];
8893     /* See if the directory exists */
8894     if (!(dirp = my_dir(tmpdir,MYF(MY_WME | MY_DONT_SORT))))
8895       continue;
8896 
8897     /* Remove all SQLxxx tables from directory */
8898 
8899     for (idx=0 ; idx < (uint) dirp->number_of_files ; idx++)
8900     {
8901       file=dirp->dir_entry+idx;
8902 
8903       if (!strncmp(file->name, tmp_file_prefix, tmp_file_prefix_length))
8904       {
8905         char *ext= fn_ext(file->name);
8906         size_t ext_len= strlen(ext);
8907         size_t path_len= my_snprintf(path, sizeof(path),
8908                                        "%s%c%s", tmpdir, FN_LIBCHAR,
8909                                        file->name);
8910         if (!strcmp(reg_ext, ext))
8911         {
8912           /* We should cut file extention before deleting of table */
8913           memcpy(path_copy, path, path_len - ext_len);
8914           path_copy[path_len - ext_len]= 0;
8915           init_tmp_table_share(thd, &share, "", 0, "", path_copy);
8916           if (!open_table_def(thd, &share))
8917             share.db_type()->drop_table(share.db_type(), path_copy);
8918           free_table_share(&share);
8919         }
8920         /*
8921           File can be already deleted by tmp_table.file->delete_table().
8922           So we hide error messages which happnes during deleting of these
8923           files(MYF(0)).
8924         */
8925         (void) mysql_file_delete(key_file_misc, path, MYF(0));
8926       }
8927     }
8928     my_dirend(dirp);
8929   }
8930   delete thd;
8931   DBUG_RETURN(0);
8932 }
8933 
8934 
8935 /*****************************************************************************
8936 	unireg support functions
8937 *****************************************************************************/
8938 
setup_ftfuncs(SELECT_LEX * select_lex)8939 int setup_ftfuncs(SELECT_LEX *select_lex)
8940 {
8941   List_iterator<Item_func_match> li(*(select_lex->ftfunc_list)),
8942                                  lj(*(select_lex->ftfunc_list));
8943   Item_func_match *ftf, *ftf2;
8944 
8945   while ((ftf=li++))
8946   {
8947     if (ftf->fix_index())
8948       return 1;
8949     lj.rewind();
8950     while ((ftf2=lj++) != ftf)
8951     {
8952       if (ftf->eq(ftf2,1) && !ftf2->master)
8953         ftf2->master=ftf;
8954     }
8955   }
8956 
8957   return 0;
8958 }
8959 
8960 
cleanup_ftfuncs(SELECT_LEX * select_lex)8961 void cleanup_ftfuncs(SELECT_LEX *select_lex)
8962 {
8963   List_iterator<Item_func_match> li(*(select_lex->ftfunc_list)),
8964                                  lj(*(select_lex->ftfunc_list));
8965   Item_func_match *ftf;
8966 
8967   while ((ftf=li++))
8968   {
8969     ftf->cleanup();
8970   }
8971 }
8972 
8973 
init_ftfuncs(THD * thd,SELECT_LEX * select_lex,bool no_order)8974 int init_ftfuncs(THD *thd, SELECT_LEX *select_lex, bool no_order)
8975 {
8976   if (select_lex->ftfunc_list->elements)
8977   {
8978     List_iterator<Item_func_match> li(*(select_lex->ftfunc_list));
8979     Item_func_match *ifm;
8980 
8981     while ((ifm=li++))
8982       if (unlikely(!ifm->is_fixed()))
8983         /*
8984           it mean that clause where was FT function was removed, so we have
8985           to remove the function from the list.
8986         */
8987         li.remove();
8988       else if (ifm->init_search(thd, no_order))
8989 	return 1;
8990   }
8991   return 0;
8992 }
8993 
8994 
is_equal(const LEX_CSTRING * a,const LEX_CSTRING * b)8995 bool is_equal(const LEX_CSTRING *a, const LEX_CSTRING *b)
8996 {
8997   return a->length == b->length && !strncmp(a->str, b->str, a->length);
8998 }
8999 
9000 /*
9001   Open and lock system tables for read.
9002 
9003   SYNOPSIS
9004     open_system_tables_for_read()
9005       thd         Thread context.
9006       table_list  List of tables to open.
9007 
9008   NOTES
9009     Caller should have used start_new_trans object to start a new
9010     transcation when reading system tables.
9011 
9012     Thanks to restrictions which we put on opening and locking of
9013     system tables for writing, we can open and lock them for reading
9014     even when we already have some other tables open and locked.
9015     One should call thd->commit_whole_transaction_and_close_tables()
9016     to close systems tables opened with this call.
9017 
9018   NOTES
9019    In some situations we  use this function to open system tables for
9020    writing. It happens, for examples, with statistical tables when
9021    they are updated by an ANALYZE command. In these cases we should
9022    guarantee that system tables will not be deadlocked.
9023 
9024   RETURN
9025     FALSE   Success
9026     TRUE    Error
9027 */
9028 
9029 bool
open_system_tables_for_read(THD * thd,TABLE_LIST * table_list)9030 open_system_tables_for_read(THD *thd, TABLE_LIST *table_list)
9031 {
9032   Query_tables_list query_tables_list_backup;
9033   LEX *lex= thd->lex;
9034   DBUG_ENTER("open_system_tables_for_read");
9035   DBUG_ASSERT(thd->internal_transaction());
9036 
9037   /*
9038     Besides using new Open_tables_state for opening system tables,
9039     we also have to backup and reset/and then restore part of LEX
9040     which is accessed by open_tables() in order to determine if
9041     prelocking is needed and what tables should be added for it.
9042   */
9043   lex->reset_n_backup_query_tables_list(&query_tables_list_backup);
9044   thd->lex->sql_command= SQLCOM_SELECT;
9045 
9046   /*
9047     Only use MYSQL_LOCK_IGNORE_TIMEOUT for tables opened for read.
9048     This is to ensure that lock_wait_timeout is honored when trying
9049     to update stats tables.
9050   */
9051   if (open_and_lock_tables(thd, table_list, FALSE,
9052                            (MYSQL_OPEN_IGNORE_FLUSH |
9053                             MYSQL_OPEN_IGNORE_LOGGING_FORMAT |
9054                             (table_list->lock_type < TL_WRITE_ALLOW_WRITE ?
9055                              MYSQL_LOCK_IGNORE_TIMEOUT : 0))))
9056   {
9057     lex->restore_backup_query_tables_list(&query_tables_list_backup);
9058     DBUG_RETURN(TRUE);
9059   }
9060 
9061   for (TABLE_LIST *tables= table_list; tables; tables= tables->next_global)
9062   {
9063     DBUG_ASSERT(tables->table->s->table_category == TABLE_CATEGORY_SYSTEM);
9064     tables->table->file->row_logging= 0;
9065     tables->table->use_all_columns();
9066   }
9067   lex->restore_backup_query_tables_list(&query_tables_list_backup);
9068 
9069   DBUG_RETURN(FALSE);
9070 }
9071 
9072 /**
9073   A helper function to close a mysql.* table opened
9074   in an auxiliary THD during bootstrap or in the main
9075   connection, when we know that there are no locks
9076   held by the connection due to a preceding implicit
9077   commit.
9078 
9079   We need this function since we'd like to not
9080   just close the system table, but also release
9081   the metadata lock on it.
9082 
9083   Note, that in LOCK TABLES mode this function
9084   does not release the metadata lock. But in this
9085   mode the table can be opened only if it is locked
9086   explicitly with LOCK TABLES.
9087 */
9088 
9089 void
close_mysql_tables(THD * thd)9090 close_mysql_tables(THD *thd)
9091 {
9092   if (! thd->in_sub_stmt)
9093   {
9094     trans_commit_stmt(thd);
9095     trans_commit(thd);
9096   }
9097   close_thread_tables(thd);
9098   thd->release_transactional_locks();
9099 }
9100 
9101 /*
9102   Open and lock one system table for update.
9103 
9104   SYNOPSIS
9105     open_system_table_for_update()
9106       thd        Thread context.
9107       one_table  Table to open.
9108 
9109   NOTES
9110     Table opened with this call should closed using close_thread_tables().
9111 
9112   RETURN
9113     0	Error
9114     #	Pointer to TABLE object of system table
9115 */
9116 
9117 TABLE *
open_system_table_for_update(THD * thd,TABLE_LIST * one_table)9118 open_system_table_for_update(THD *thd, TABLE_LIST *one_table)
9119 {
9120   DBUG_ENTER("open_system_table_for_update");
9121 
9122   TABLE *table= open_ltable(thd, one_table, one_table->lock_type,
9123                             MYSQL_LOCK_IGNORE_TIMEOUT);
9124   if (table)
9125   {
9126     DBUG_ASSERT(table->s->table_category == TABLE_CATEGORY_SYSTEM);
9127     table->use_all_columns();
9128     /* This table instance is not row logged */
9129     table->file->row_logging= 0;
9130   }
9131   DBUG_RETURN(table);
9132 }
9133 
9134 /**
9135   Open a log table.
9136   Opening such tables is performed internally in the server
9137   implementation, and is a 'nested' open, since some tables
9138   might be already opened by the current thread.
9139   The thread context before this call is saved, and is restored
9140   when calling close_log_table().
9141   @param thd The current thread
9142   @param one_table Log table to open
9143   @param backup [out] Temporary storage used to save the thread context
9144 */
9145 TABLE *
open_log_table(THD * thd,TABLE_LIST * one_table,Open_tables_backup * backup)9146 open_log_table(THD *thd, TABLE_LIST *one_table, Open_tables_backup *backup)
9147 {
9148   uint flags= ( MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK |
9149                 MYSQL_LOCK_IGNORE_GLOBAL_READ_ONLY |
9150                 MYSQL_OPEN_IGNORE_FLUSH |
9151                 MYSQL_LOCK_IGNORE_TIMEOUT |
9152                 MYSQL_LOCK_LOG_TABLE);
9153   TABLE *table;
9154   /* Save value that is changed in mysql_lock_tables() */
9155   ulonglong save_utime_after_lock= thd->utime_after_lock;
9156   DBUG_ENTER("open_log_table");
9157 
9158   thd->reset_n_backup_open_tables_state(backup);
9159 
9160   if ((table= open_ltable(thd, one_table, one_table->lock_type, flags)))
9161   {
9162     DBUG_ASSERT(table->s->table_category == TABLE_CATEGORY_LOG);
9163     DBUG_ASSERT(!table->file->row_logging);
9164 
9165     /* Make sure all columns get assigned to a default value */
9166     table->use_all_columns();
9167     DBUG_ASSERT(table->s->no_replicate);
9168   }
9169   else
9170     thd->restore_backup_open_tables_state(backup);
9171 
9172   thd->utime_after_lock= save_utime_after_lock;
9173   DBUG_RETURN(table);
9174 }
9175 
9176 /**
9177   Close a log table.
9178   The last table opened by open_log_table()
9179   is closed, then the thread context is restored.
9180   @param thd The current thread
9181   @param backup [in] the context to restore.
9182 */
9183 
close_log_table(THD * thd,Open_tables_backup * backup)9184 void close_log_table(THD *thd, Open_tables_backup *backup)
9185 {
9186   /*
9187     Inform the transaction handler that we are closing the
9188     system tables and we don't need the read view anymore.
9189   */
9190   for (TABLE *table= thd->open_tables ; table ; table= table->next)
9191     table->file->extra(HA_EXTRA_PREPARE_FOR_FORCED_CLOSE);
9192   close_thread_tables(thd);
9193   thd->restore_backup_open_tables_state(backup);
9194 }
9195 
9196 
9197 /**
9198   @brief
9199   Remove 'fixed' flag from items in a list
9200 
9201   @param items list of items to un-fix
9202 
9203   @details
9204   This function sets to 0 the 'fixed' flag for items in the 'items' list.
9205   It's needed to force correct marking of views' fields for INSERT/UPDATE
9206   statements.
9207 */
9208 
unfix_fields(List<Item> & fields)9209 void unfix_fields(List<Item> &fields)
9210 {
9211   List_iterator<Item> li(fields);
9212   Item *item;
9213   while ((item= li++))
9214     item->unfix_fields();
9215 }
9216 
9217 
9218 /**
9219   Check result of dynamic column function and issue error if it is needed
9220 
9221   @param rc              The result code of dynamic column function
9222 
9223   @return the result code which was get as an argument\
9224 */
9225 
dynamic_column_error_message(enum_dyncol_func_result rc)9226 int dynamic_column_error_message(enum_dyncol_func_result rc)
9227 {
9228   switch (rc) {
9229   case ER_DYNCOL_YES:
9230   case ER_DYNCOL_OK:
9231   case ER_DYNCOL_TRUNCATED:
9232     break; // it is not an error
9233   case ER_DYNCOL_FORMAT:
9234     my_error(ER_DYN_COL_WRONG_FORMAT, MYF(0));
9235     break;
9236   case ER_DYNCOL_LIMIT:
9237     my_error(ER_DYN_COL_IMPLEMENTATION_LIMIT, MYF(0));
9238     break;
9239   case ER_DYNCOL_RESOURCE:
9240     my_error(ER_OUT_OF_RESOURCES, MYF(0));
9241     break;
9242   case ER_DYNCOL_DATA:
9243     my_error(ER_DYN_COL_DATA, MYF(0));
9244     break;
9245   case ER_DYNCOL_UNKNOWN_CHARSET:
9246     my_error(ER_DYN_COL_WRONG_CHARSET, MYF(0));
9247     break;
9248   }
9249   return rc;
9250 }
9251 
9252 
9253 /**
9254   Turn on the SELECT_DESCRIBE flag for the primary SELECT_LEX of the statement
9255   being processed in case the statement is EXPLAIN UPDATE/DELETE.
9256 
9257   @param lex  current LEX
9258 */
9259 
promote_select_describe_flag_if_needed(LEX * lex)9260 void promote_select_describe_flag_if_needed(LEX *lex)
9261 {
9262   if (lex->describe)
9263     lex->first_select_lex()->options|= SELECT_DESCRIBE;
9264 }
9265 
9266 
9267 /**
9268   @} (end of group Data_Dictionary)
9269 */
9270