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