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