1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
/* Basic functions needed by many modules */
18
#include <drizzled/server_includes.h>
19
#include <drizzled/sql_select.h>
20
#include <mysys/my_dir.h>
21
#include <drizzled/drizzled_error_messages.h>
22
#include <libdrizzle/gettext.h>
24
#define FLAGSTR(S,F) ((S) & (F) ? #F " " : "")
27
@defgroup Data_Dictionary Data Dictionary
30
Table *unused_tables; /* Used by mysql_test */
31
HASH open_cache; /* Used by mysql_test */
32
static HASH table_def_cache;
33
static TABLE_SHARE *oldest_unused_share, end_of_unused_share;
34
static pthread_mutex_t LOCK_table_share;
35
static bool table_def_inited= 0;
37
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
39
char *cache_key, uint32_t cache_key_length);
40
static void free_cache_entry(Table *entry);
41
static void close_old_data_files(THD *thd, Table *table, bool morph_locks,
45
extern "C" unsigned char *table_cache_key(const unsigned char *record, size_t *length,
46
bool not_used __attribute__((unused)))
48
Table *entry=(Table*) record;
49
*length= entry->s->table_cache_key.length;
50
return (unsigned char*) entry->s->table_cache_key.str;
54
bool table_cache_init(void)
56
return hash_init(&open_cache, &my_charset_bin, table_cache_size+16,
57
0, 0, table_cache_key,
58
(hash_free_key) free_cache_entry, 0);
61
void table_cache_free(void)
65
close_cached_tables(NULL, NULL, false, false, false);
66
if (!open_cache.records) // Safety first
67
hash_free(&open_cache);
72
uint32_t cached_open_tables(void)
74
return open_cache.records;
78
Create a table cache key
81
create_table_def_key()
83
key Create key here (must be of size MAX_DBKEY_LENGTH)
84
table_list Table definition
85
tmp_table Set if table is a tmp table
88
The table cache_key is created from:
92
if the table is a tmp table, we add the following to make each tmp table
95
4 bytes for master thread id
96
4 bytes pseudo thread id
102
uint32_t create_table_def_key(THD *thd, char *key, TableList *table_list,
105
uint32_t key_length= (uint) (my_stpcpy(my_stpcpy(key, table_list->db)+1,
106
table_list->table_name)-key)+1;
109
int4store(key + key_length, thd->server_id);
110
int4store(key + key_length + 4, thd->variables.pseudo_thread_id);
111
key_length+= TMP_TABLE_KEY_EXTRA;
118
/*****************************************************************************
119
Functions to handle table definition cach (TABLE_SHARE)
120
*****************************************************************************/
122
extern "C" unsigned char *table_def_key(const unsigned char *record, size_t *length,
123
bool not_used __attribute__((unused)))
125
TABLE_SHARE *entry=(TABLE_SHARE*) record;
126
*length= entry->table_cache_key.length;
127
return (unsigned char*) entry->table_cache_key.str;
131
static void table_def_free_entry(TABLE_SHARE *share)
135
/* remove from old_unused_share list */
136
pthread_mutex_lock(&LOCK_table_share);
137
*share->prev= share->next;
138
share->next->prev= share->prev;
139
pthread_mutex_unlock(&LOCK_table_share);
141
free_table_share(share);
146
bool table_def_init(void)
149
pthread_mutex_init(&LOCK_table_share, MY_MUTEX_INIT_FAST);
150
oldest_unused_share= &end_of_unused_share;
151
end_of_unused_share.prev= &oldest_unused_share;
153
return hash_init(&table_def_cache, &my_charset_bin, table_def_size,
155
(hash_free_key) table_def_free_entry, 0);
159
void table_def_free(void)
161
if (table_def_inited)
164
pthread_mutex_destroy(&LOCK_table_share);
165
hash_free(&table_def_cache);
171
uint32_t cached_table_definitions(void)
173
return table_def_cache.records;
178
Get TABLE_SHARE for a table.
182
table_list Table that should be opened
184
key_length Length of key
185
db_flags Flags to open_table_def():
187
error out: Error code from open_table_def()
190
Get a table definition from the table definition cache.
191
If it doesn't exist, create a new from the table definition file.
194
We must have wrlock on LOCK_open when we come here
195
(To be changed later)
202
TABLE_SHARE *get_table_share(THD *thd, TableList *table_list, char *key,
203
uint32_t key_length, uint32_t db_flags, int *error)
209
/* Read table definition from cache */
210
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key,
214
if (!(share= alloc_table_share(table_list, key, key_length)))
220
Lock mutex to be able to read table definition from file without
223
(void) pthread_mutex_lock(&share->mutex);
226
We assign a new table id under the protection of the LOCK_open and
227
the share's own mutex. We do this insted of creating a new mutex
228
and using it for the sole purpose of serializing accesses to a
229
static variable, we assign the table id here. We assign it to the
230
share before inserting it into the table_def_cache to be really
231
sure that it cannot be read from the cache without having a table
234
CAVEAT. This means that the table cannot be used for
235
binlogging/replication purposes, unless get_table_share() has been
236
called directly or indirectly.
238
assign_new_table_id(share);
240
if (my_hash_insert(&table_def_cache, (unsigned char*) share))
242
free_table_share(share);
243
return(0); // return error
245
if (open_table_def(thd, share, db_flags))
247
*error= share->error;
248
(void) hash_delete(&table_def_cache, (unsigned char*) share);
251
share->ref_count++; // Mark in use
252
(void) pthread_mutex_unlock(&share->mutex);
257
We found an existing table definition. Return it if we didn't get
258
an error when reading the table definition from file.
261
/* We must do a lock to ensure that the structure is initialized */
262
(void) pthread_mutex_lock(&share->mutex);
265
/* Table definition contained an error */
266
open_table_error(share, share->error, share->open_errno, share->errarg);
267
(void) pthread_mutex_unlock(&share->mutex);
271
if (!share->ref_count++ && share->prev)
274
Share was not used before and it was in the old_unused_share list
275
Unlink share from this list
277
pthread_mutex_lock(&LOCK_table_share);
278
*share->prev= share->next;
279
share->next->prev= share->prev;
282
pthread_mutex_unlock(&LOCK_table_share);
284
(void) pthread_mutex_unlock(&share->mutex);
286
/* Free cache if too big */
287
while (table_def_cache.records > table_def_size &&
288
oldest_unused_share->next)
290
pthread_mutex_lock(&oldest_unused_share->mutex);
291
hash_delete(&table_def_cache, (unsigned char*) oldest_unused_share);
299
Get a table share. If it didn't exist, try creating it from engine
301
For arguments and return values, see get_table_from_share()
305
*get_table_share_with_create(THD *thd, TableList *table_list,
306
char *key, uint32_t key_length,
307
uint32_t db_flags, int *error)
312
share= get_table_share(thd, table_list, key, key_length, db_flags, error);
314
If share is not NULL, we found an existing share.
316
If share is NULL, and there is no error, we're inside
317
pre-locking, which silences 'ER_NO_SUCH_TABLE' errors
318
with the intention to silently drop non-existing tables
319
from the pre-locking list. In this case we still need to try
320
auto-discover before returning a NULL share.
322
If share is NULL and the error is ER_NO_SUCH_TABLE, this is
323
the same as above, only that the error was not silenced by
324
pre-locking. Once again, we need to try to auto-discover
327
Finally, if share is still NULL, it's a real error and we need
330
@todo Rework alternative ways to deal with ER_NO_SUCH Table.
332
if (share || (thd->is_error() && (thd->main_da.sql_errno() != ER_NO_SUCH_TABLE)))
336
/* Table didn't exist. Check if some engine can provide it */
337
if ((tmp= ha_create_table_from_engine(thd, table_list->db,
338
table_list->table_name)) < 0)
343
/* Give right error message */
345
my_printf_error(ER_UNKNOWN_ERROR,
346
"Failed to open '%-.64s', error while "
347
"unpacking from engine",
348
MYF(0), table_list->table_name);
351
/* Table existed in engine. Let's open it */
352
drizzle_reset_errors(thd, 1); // Clear warnings
353
thd->clear_error(); // Clear error message
354
return(get_table_share(thd, table_list, key, key_length,
360
Mark that we are not using table share anymore.
363
release_table_share()
365
release_type How the release should be done:
367
- Release without checking
368
RELEASE_WAIT_FOR_DROP
369
- Don't return until we get a signal that the
370
table is deleted or the thread is killed.
373
If ref_count goes to zero and (we have done a refresh or if we have
374
already too many open table shares) then delete the definition.
376
If type == RELEASE_WAIT_FOR_DROP then don't return until we get a signal
377
that the table is deleted or the thread is killed.
380
void release_table_share(TABLE_SHARE *share,
381
enum release_type type __attribute__((unused)))
383
bool to_be_deleted= 0;
385
safe_mutex_assert_owner(&LOCK_open);
387
pthread_mutex_lock(&share->mutex);
388
if (!--share->ref_count)
390
if (share->version != refresh_version)
394
/* Link share last in used_table_share list */
395
assert(share->next == 0);
396
pthread_mutex_lock(&LOCK_table_share);
397
share->prev= end_of_unused_share.prev;
398
*end_of_unused_share.prev= share;
399
end_of_unused_share.prev= &share->next;
400
share->next= &end_of_unused_share;
401
pthread_mutex_unlock(&LOCK_table_share);
403
to_be_deleted= (table_def_cache.records > table_def_size);
409
hash_delete(&table_def_cache, (unsigned char*) share);
412
pthread_mutex_unlock(&share->mutex);
418
Check if table definition exits in cache
421
get_cached_table_share()
423
table_name Table name
427
# TABLE_SHARE for table
430
TABLE_SHARE *get_cached_table_share(const char *db, const char *table_name)
432
char key[NAME_LEN*2+2];
433
TableList table_list;
435
safe_mutex_assert_owner(&LOCK_open);
437
table_list.db= (char*) db;
438
table_list.table_name= (char*) table_name;
439
key_length= create_table_def_key((THD*) 0, key, &table_list, 0);
440
return (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key, key_length);
445
Close file handle, but leave the table in the table cache
448
close_handle_and_leave_table_as_lock()
452
By leaving the table in the table cache, it disallows any other thread
455
thd->killed will be set if we run out of memory
457
If closing a MERGE child, the calling function has to take care for
458
closing the parent too, if necessary.
462
void close_handle_and_leave_table_as_lock(Table *table)
464
TABLE_SHARE *share, *old_share= table->s;
466
MEM_ROOT *mem_root= &table->mem_root;
468
assert(table->db_stat);
471
Make a local copy of the table share and free the current one.
472
This has to be done to ensure that the table share is removed from
473
the table defintion cache as soon as the last instance is removed
475
if (multi_alloc_root(mem_root,
476
&share, sizeof(*share),
477
&key_buff, old_share->table_cache_key.length,
480
memset(share, 0, sizeof(*share));
481
share->set_table_cache_key(key_buff, old_share->table_cache_key.str,
482
old_share->table_cache_key.length);
483
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table()
486
table->file->close();
487
table->db_stat= 0; // Mark file closed
488
release_table_share(table->s, RELEASE_NORMAL);
490
table->file->change_table_ptr(table, table->s);
498
Create a list for all open tables matching SQL expression
503
wild SQL like expression
506
One gets only a list of tables for which one has any kind of privilege.
507
db and table names are allocated in result struct, so one doesn't need
508
a lock on LOCK_open when traversing the return list.
511
NULL Error (Probably OOM)
512
# Pointer to list of names of open tables.
515
OPEN_TableList *list_open_tables(THD *thd __attribute__((unused)),
516
const char *db, const char *wild)
519
OPEN_TableList **start_list, *open_list;
520
TableList table_list;
522
pthread_mutex_lock(&LOCK_open);
523
memset(&table_list, 0, sizeof(table_list));
524
start_list= &open_list;
527
for (uint32_t idx=0 ; result == 0 && idx < open_cache.records; idx++)
529
OPEN_TableList *table;
530
Table *entry=(Table*) hash_element(&open_cache,idx);
531
TABLE_SHARE *share= entry->s;
533
if (db && my_strcasecmp(system_charset_info, db, share->db.str))
535
if (wild && wild_compare(share->table_name.str, wild, 0))
538
/* Check if user has SELECT privilege for any column in the table */
539
table_list.db= share->db.str;
540
table_list.table_name= share->table_name.str;
542
/* need to check if we haven't already listed it */
543
for (table= open_list ; table ; table=table->next)
545
if (!strcmp(table->table, share->table_name.str) &&
546
!strcmp(table->db, share->db.str))
550
if (entry->locked_by_name)
557
if (!(*start_list = (OPEN_TableList *)
558
sql_alloc(sizeof(**start_list)+share->table_cache_key.length)))
560
open_list=0; // Out of memory
563
my_stpcpy((*start_list)->table=
564
my_stpcpy(((*start_list)->db= (char*) ((*start_list)+1)),
566
share->table_name.str);
567
(*start_list)->in_use= entry->in_use ? 1 : 0;
568
(*start_list)->locked= entry->locked_by_name ? 1 : 0;
569
start_list= &(*start_list)->next;
572
pthread_mutex_unlock(&LOCK_open);
576
/*****************************************************************************
577
* Functions to free open table cache
578
****************************************************************************/
581
void intern_close_table(Table *table)
582
{ // Free all structures
583
free_io_cache(table);
584
if (table->file) // Not true if name lock
585
closefrm(table, 1); // close file
590
Remove table from the open table cache
594
table Table to remove
597
We need to have a lock on LOCK_open when calling this
600
static void free_cache_entry(Table *table)
602
intern_close_table(table);
605
table->next->prev=table->prev; /* remove from used chain */
606
table->prev->next=table->next;
607
if (table == unused_tables)
609
unused_tables=unused_tables->next;
610
if (table == unused_tables)
614
free((unsigned char*) table);
618
/* Free resources allocated by filesort() and read_record() */
620
void free_io_cache(Table *table)
622
if (table->sort.io_cache)
624
close_cached_file(table->sort.io_cache);
625
free((unsigned char*) table->sort.io_cache);
626
table->sort.io_cache=0;
633
Close all tables which aren't in use by any thread
635
@param thd Thread context
636
@param tables List of tables to remove from the cache
637
@param have_lock If LOCK_open is locked
638
@param wait_for_refresh Wait for a impending flush
639
@param wait_for_placeholders Wait for tables being reopened so that the GRL
640
won't proceed while write-locked tables are being reopened by other
643
@remark THD can be NULL, but then wait_for_refresh must be false
644
and tables must be NULL.
647
bool close_cached_tables(THD *thd, TableList *tables, bool have_lock,
648
bool wait_for_refresh, bool wait_for_placeholders)
651
assert(thd || (!wait_for_refresh && !tables));
654
pthread_mutex_lock(&LOCK_open);
657
refresh_version++; // Force close of open tables
658
while (unused_tables)
661
if (hash_delete(&open_cache,(unsigned char*) unused_tables))
662
printf("Warning: Couldn't delete open table from hash\n");
664
hash_delete(&open_cache,(unsigned char*) unused_tables);
667
/* Free table shares */
668
while (oldest_unused_share->next)
670
pthread_mutex_lock(&oldest_unused_share->mutex);
671
hash_delete(&table_def_cache, (unsigned char*) oldest_unused_share);
673
if (wait_for_refresh)
676
Other threads could wait in a loop in open_and_lock_tables(),
677
trying to lock one or more of our tables.
679
If they wait for the locks in thr_multi_lock(), their lock
680
request is aborted. They loop in open_and_lock_tables() and
681
enter open_table(). Here they notice the table is refreshed and
682
wait for COND_refresh. Then they loop again in
683
open_and_lock_tables() and this time open_table() succeeds. At
684
this moment, if we (the FLUSH TABLES thread) are scheduled and
685
on another FLUSH TABLES enter close_cached_tables(), they could
686
awake while we sleep below, waiting for others threads (us) to
687
close their open tables. If this happens, the other threads
688
would find the tables unlocked. They would get the locks, one
689
after the other, and could do their destructive work. This is an
690
issue if we have LOCK TABLES in effect.
692
The problem is that the other threads passed all checks in
693
open_table() before we refresh the table.
695
The fix for this problem is to set some_tables_deleted for all
696
threads with open tables. These threads can still get their
697
locks, but will immediately release them again after checking
698
this variable. They will then loop in open_and_lock_tables()
699
again. There they will wait until we update all tables version
702
Setting some_tables_deleted is done by remove_table_from_cache()
705
In other words (reviewer suggestion): You need this setting of
706
some_tables_deleted for the case when table was opened and all
707
related checks were passed before incrementing refresh_version
708
(which you already have) but attempt to lock the table happened
709
after the call to close_old_data_files() i.e. after removal of
710
current thread locks.
712
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
714
Table *table=(Table*) hash_element(&open_cache,idx);
716
table->in_use->some_tables_deleted= 1;
723
for (TableList *table= tables; table; table= table->next_local)
725
if (remove_table_from_cache(thd, table->db, table->table_name,
726
RTFC_OWNED_BY_THD_FLAG))
730
wait_for_refresh=0; // Nothing to wait for
733
if (wait_for_refresh)
736
If there is any table that has a lower refresh_version, wait until
737
this is closed (or this thread is killed) before returning
739
thd->mysys_var->current_mutex= &LOCK_open;
740
thd->mysys_var->current_cond= &COND_refresh;
741
thd_proc_info(thd, "Flushing tables");
743
close_old_data_files(thd,thd->open_tables,1,1);
747
/* Wait until all threads has closed all the tables we had locked */
748
while (found && ! thd->killed)
751
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
753
Table *table=(Table*) hash_element(&open_cache,idx);
754
/* Avoid a self-deadlock. */
755
if (table->in_use == thd)
758
Note that we wait here only for tables which are actually open, and
759
not for placeholders with Table::open_placeholder set. Waiting for
760
latter will cause deadlock in the following scenario, for example:
762
conn1: lock table t1 write;
763
conn2: lock table t2 write;
767
It also does not make sense to wait for those of placeholders that
768
are employed by CREATE TABLE as in this case table simply does not
771
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
772
(table->open_placeholder && wait_for_placeholders)))
775
pthread_cond_wait(&COND_refresh,&LOCK_open);
781
No other thread has the locked tables open; reopen them and get the
782
old locks. This should always succeed (unless some external process
783
has removed the tables)
785
thd->in_lock_tables=1;
786
result=reopen_tables(thd,1,1);
787
thd->in_lock_tables=0;
788
/* Set version for table */
789
for (Table *table=thd->open_tables; table ; table= table->next)
792
Preserve the version (0) of write locked tables so that a impending
793
global read lock won't sneak in.
795
if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
796
table->s->version= refresh_version;
800
pthread_mutex_unlock(&LOCK_open);
801
if (wait_for_refresh)
803
pthread_mutex_lock(&thd->mysys_var->mutex);
804
thd->mysys_var->current_mutex= 0;
805
thd->mysys_var->current_cond= 0;
806
thd_proc_info(thd, 0);
807
pthread_mutex_unlock(&thd->mysys_var->mutex);
814
Close all tables which match specified connection string or
815
if specified string is NULL, then any table with a connection string.
818
bool close_cached_connection_tables(THD *thd, bool if_wait_for_refresh,
819
LEX_STRING *connection, bool have_lock)
822
TableList tmp, *tables= NULL;
826
memset(&tmp, 0, sizeof(TableList));
829
pthread_mutex_lock(&LOCK_open);
831
for (idx= 0; idx < table_def_cache.records; idx++)
833
TABLE_SHARE *share= (TABLE_SHARE *) hash_element(&table_def_cache, idx);
835
/* Ignore if table is not open or does not have a connect_string */
836
if (!share->connect_string.length || !share->ref_count)
839
/* Compare the connection string */
841
(connection->length > share->connect_string.length ||
842
(connection->length < share->connect_string.length &&
843
(share->connect_string.str[connection->length] != '/' &&
844
share->connect_string.str[connection->length] != '\\')) ||
845
strncasecmp(connection->str, share->connect_string.str,
846
connection->length)))
849
/* close_cached_tables() only uses these elements */
850
tmp.db= share->db.str;
851
tmp.table_name= share->table_name.str;
852
tmp.next_local= tables;
854
tables= (TableList *) memdup_root(thd->mem_root, (char*)&tmp,
859
result= close_cached_tables(thd, tables, true, false, false);
862
pthread_mutex_unlock(&LOCK_open);
864
if (if_wait_for_refresh)
866
pthread_mutex_lock(&thd->mysys_var->mutex);
867
thd->mysys_var->current_mutex= 0;
868
thd->mysys_var->current_cond= 0;
869
thd->set_proc_info(0);
870
pthread_mutex_unlock(&thd->mysys_var->mutex);
878
Mark all temporary tables which were used by the current statement or
879
substatement as free for reuse, but only if the query_id can be cleared.
881
@param thd thread context
883
@remark For temp tables associated with a open SQL HANDLER the query_id
884
is not reset until the HANDLER is closed.
887
static void mark_temp_tables_as_free_for_reuse(THD *thd)
889
for (Table *table= thd->temporary_tables ; table ; table= table->next)
891
if ((table->query_id == thd->query_id) && ! table->open_by_handler)
894
table->file->ha_reset();
901
Mark all tables in the list which were used by current substatement
905
mark_used_tables_as_free_for_reuse()
907
table - head of the list of tables
910
Marks all tables in the list which were used by current substatement
911
(they are marked by its query_id) as free for reuse.
914
The reason we reset query_id is that it's not enough to just test
915
if table->query_id != thd->query_id to know if a table is in use.
918
SELECT f1_that_uses_t1() FROM t1;
919
In f1_that_uses_t1() we will see one instance of t1 where query_id is
920
set to query_id of original query.
923
static void mark_used_tables_as_free_for_reuse(THD *thd, Table *table)
925
for (; table ; table= table->next)
927
if (table->query_id == thd->query_id)
930
table->file->ha_reset();
937
Auxiliary function to close all tables in the open_tables list.
939
@param thd Thread context.
941
@remark It should not ordinarily be called directly.
944
static void close_open_tables(THD *thd)
946
bool found_old_table= 0;
948
safe_mutex_assert_not_owner(&LOCK_open);
950
pthread_mutex_lock(&LOCK_open);
952
while (thd->open_tables)
953
found_old_table|= close_thread_table(thd, &thd->open_tables);
954
thd->some_tables_deleted= 0;
956
/* Free tables to hold down open files */
957
while (open_cache.records > table_cache_size && unused_tables)
958
hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
961
/* Tell threads waiting for refresh that something has happened */
965
pthread_mutex_unlock(&LOCK_open);
970
Close all tables used by the current substatement, or all tables
971
used by this thread if we are on the upper level.
974
close_thread_tables()
978
Unlocks tables and frees derived tables.
979
Put all normal tables used by thread in free list.
981
It will only close/mark as free for reuse tables opened by this
982
substatement, it will also check if we are closing tables after
983
execution of complete query (i.e. we are on upper level) and will
984
leave prelocked mode if needed.
987
void close_thread_tables(THD *thd)
992
We are assuming here that thd->derived_tables contains ONLY derived
993
tables for this substatement. i.e. instead of approach which uses
994
query_id matching for determining which of the derived tables belong
995
to this substatement we rely on the ability of substatements to
996
save/restore thd->derived_tables during their execution.
998
TODO: Probably even better approach is to simply associate list of
999
derived tables with (sub-)statement instead of thread and destroy
1000
them at the end of its execution.
1002
if (thd->derived_tables)
1006
Close all derived tables generated in queries like
1007
SELECT * FROM (SELECT * FROM t1)
1009
for (table= thd->derived_tables ; table ; table= next)
1012
table->free_tmp_table(thd);
1014
thd->derived_tables= 0;
1018
Mark all temporary tables used by this statement as free for reuse.
1020
mark_temp_tables_as_free_for_reuse(thd);
1022
Let us commit transaction for statement. Since in 5.0 we only have
1023
one statement transaction and don't allow several nested statement
1024
transactions this call will do nothing if we are inside of stored
1025
function or trigger (i.e. statement transaction is already active and
1026
does not belong to statement for which we do close_thread_tables()).
1027
TODO: This should be fixed in later releases.
1029
if (!(thd->state_flags & Open_tables_state::BACKUPS_AVAIL))
1031
thd->main_da.can_overwrite_status= true;
1032
ha_autocommit_or_rollback(thd, thd->is_error());
1033
thd->main_da.can_overwrite_status= false;
1034
thd->transaction.stmt.reset();
1037
if (thd->locked_tables)
1040
/* Ensure we are calling ha_reset() for all used tables */
1041
mark_used_tables_as_free_for_reuse(thd, thd->open_tables);
1044
We are under simple LOCK TABLES so should not do anything else.
1052
For RBR we flush the pending event just before we unlock all the
1053
tables. This means that we are at the end of a topmost
1054
statement, so we ensure that the STMT_END_F flag is set on the
1055
pending event. For statements that are *inside* stored
1056
functions, the pending event will not be flushed: that will be
1057
handled either before writing a query log event (inside
1058
binlog_query()) or when preparing a pending event.
1060
thd->binlog_flush_pending_rows_event(true);
1061
mysql_unlock_tables(thd, thd->lock);
1065
Note that we need to hold LOCK_open while changing the
1066
open_tables list. Another thread may work on it.
1067
(See: remove_table_from_cache(), mysql_wait_completed_table())
1068
Closing a MERGE child before the parent would be fatal if the
1069
other thread tries to abort the MERGE lock in between.
1071
if (thd->open_tables)
1072
close_open_tables(thd);
1078
/* move one table to free list */
1080
bool close_thread_table(THD *thd, Table **table_ptr)
1082
bool found_old_table= 0;
1083
Table *table= *table_ptr;
1085
assert(table->key_read == 0);
1086
assert(!table->file || table->file->inited == handler::NONE);
1088
*table_ptr=table->next;
1090
if (table->needs_reopen_or_name_lock() ||
1091
thd->version != refresh_version || !table->db_stat)
1093
hash_delete(&open_cache,(unsigned char*) table);
1099
Open placeholders have Table::db_stat set to 0, so they should be
1100
handled by the first alternative.
1102
assert(!table->open_placeholder);
1104
/* Free memory and reset for next loop */
1105
table->file->ha_reset();
1109
table->next=unused_tables; /* Link in last */
1110
table->prev=unused_tables->prev;
1111
unused_tables->prev=table;
1112
table->prev->next=table;
1115
unused_tables=table->next=table->prev=table;
1117
return(found_old_table);
1121
/* close_temporary_tables' internal, 4 is due to uint4korr definition */
1122
static inline uint32_t tmpkeyval(THD *thd __attribute__((unused)),
1125
return uint4korr(table->s->table_cache_key.str + table->s->table_cache_key.length - 4);
1130
Close all temporary tables created by 'CREATE TEMPORARY TABLE' for thread
1131
creates one DROP TEMPORARY Table binlog event for each pseudo-thread
1134
void close_temporary_tables(THD *thd)
1139
/* Assume thd->options has OPTION_QUOTE_SHOW_CREATE */
1140
bool was_quote_show= true;
1142
if (!thd->temporary_tables)
1145
if (!mysql_bin_log.is_open() || thd->current_stmt_binlog_row_based)
1148
for (table= thd->temporary_tables; table; table= tmp_next)
1150
tmp_next= table->next;
1151
close_temporary(table, 1, 1);
1153
thd->temporary_tables= 0;
1157
/* Better add "if exists", in case a RESET MASTER has been done */
1158
const char stub[]= "DROP /*!40005 TEMPORARY */ Table IF EXISTS ";
1159
uint32_t stub_len= sizeof(stub) - 1;
1161
String s_query= String(buf, sizeof(buf), system_charset_info);
1162
bool found_user_tables= false;
1164
memcpy(buf, stub, stub_len);
1167
Insertion sort of temp tables by pseudo_thread_id to build ordered list
1168
of sublists of equal pseudo_thread_id
1171
for (prev_table= thd->temporary_tables, table= prev_table->next;
1173
prev_table= table, table= table->next)
1175
Table *prev_sorted /* same as for prev_table */, *sorted;
1176
if (is_user_table(table))
1178
if (!found_user_tables)
1179
found_user_tables= true;
1180
for (prev_sorted= NULL, sorted= thd->temporary_tables; sorted != table;
1181
prev_sorted= sorted, sorted= sorted->next)
1183
if (!is_user_table(sorted) ||
1184
tmpkeyval(thd, sorted) > tmpkeyval(thd, table))
1186
/* move into the sorted part of the list from the unsorted */
1187
prev_table->next= table->next;
1188
table->next= sorted;
1191
prev_sorted->next= table;
1195
thd->temporary_tables= table;
1204
/* We always quote db,table names though it is slight overkill */
1205
if (found_user_tables &&
1206
!(was_quote_show= test(thd->options & OPTION_QUOTE_SHOW_CREATE)))
1208
thd->options |= OPTION_QUOTE_SHOW_CREATE;
1211
/* scan sorted tmps to generate sequence of DROP */
1212
for (table= thd->temporary_tables; table; table= next)
1214
if (is_user_table(table))
1216
my_thread_id save_pseudo_thread_id= thd->variables.pseudo_thread_id;
1217
/* Set pseudo_thread_id to be that of the processed table */
1218
thd->variables.pseudo_thread_id= tmpkeyval(thd, table);
1220
Loop forward through all tables within the sublist of
1221
common pseudo_thread_id to create single DROP query.
1223
for (s_query.length(stub_len);
1224
table && is_user_table(table) &&
1225
tmpkeyval(thd, table) == thd->variables.pseudo_thread_id;
1229
We are going to add 4 ` around the db/table names and possible more
1230
due to special characters in the names
1232
append_identifier(thd, &s_query, table->s->db.str, strlen(table->s->db.str));
1233
s_query.append('.');
1234
append_identifier(thd, &s_query, table->s->table_name.str,
1235
strlen(table->s->table_name.str));
1236
s_query.append(',');
1238
close_temporary(table, 1, 1);
1241
const CHARSET_INFO * const cs_save= thd->variables.character_set_client;
1242
thd->variables.character_set_client= system_charset_info;
1243
Query_log_event qinfo(thd, s_query.ptr(),
1244
s_query.length() - 1 /* to remove trailing ',' */,
1246
thd->variables.character_set_client= cs_save;
1248
Imagine the thread had created a temp table, then was doing a
1249
SELECT, and the SELECT was killed. Then it's not clever to
1250
mark the statement above as "killed", because it's not really
1251
a statement updating data, and there are 99.99% chances it
1252
will succeed on slave. If a real update (one updating a
1253
persistent table) was killed on the master, then this real
1254
update will be logged with error_code=killed, rightfully
1255
causing the slave to stop.
1257
qinfo.error_code= 0;
1258
mysql_bin_log.write(&qinfo);
1259
thd->variables.pseudo_thread_id= save_pseudo_thread_id;
1264
close_temporary(table, 1, 1);
1267
if (!was_quote_show)
1268
thd->options&= ~OPTION_QUOTE_SHOW_CREATE; /* restore option */
1269
thd->temporary_tables=0;
1276
find_table_in_list()
1277
table Pointer to table list
1278
offset Offset to which list in table structure to use
1279
db_name Data base name
1280
table_name Table name
1283
This is called by find_table_in_local_list() and
1284
find_table_in_global_list().
1287
NULL Table not found
1288
# Pointer to found table.
1291
TableList *find_table_in_list(TableList *table,
1292
TableList *TableList::*link,
1293
const char *db_name,
1294
const char *table_name)
1296
for (; table; table= table->*link )
1298
if ((table->table == 0 || table->table->s->tmp_table == NO_TMP_TABLE) &&
1299
strcmp(table->db, db_name) == 0 &&
1300
strcmp(table->table_name, table_name) == 0)
1308
Test that table is unique (It's only exists once in the table list)
1313
table table which should be checked
1314
table_list list of tables
1315
check_alias whether to check tables' aliases
1317
NOTE: to exclude derived tables from check we use following mechanism:
1318
a) during derived table processing set THD::derived_tables_processing
1319
b) JOIN::prepare set SELECT::exclude_from_table_unique_test if
1320
THD::derived_tables_processing set. (we can't use JOIN::execute
1321
because for PS we perform only JOIN::prepare, but we can't set this
1322
flag in JOIN::prepare if we are not sure that we are in derived table
1323
processing loop, because multi-update call fix_fields() for some its
1324
items (which mean JOIN::prepare for subqueries) before unique_table
1325
call to detect which tables should be locked for write).
1326
c) unique_table skip all tables which belong to SELECT with
1327
SELECT::exclude_from_table_unique_test set.
1328
Also SELECT::exclude_from_table_unique_test used to exclude from check
1329
tables of main SELECT of multi-delete and multi-update
1331
We also skip tables with TableList::prelocking_placeholder set,
1332
because we want to allow SELECTs from them, and their modification
1333
will rise the error anyway.
1335
TODO: when we will have table/view change detection we can do this check
1340
0 if table is unique
1343
TableList* unique_table(THD *thd, TableList *table, TableList *table_list,
1347
const char *d_name, *t_name, *t_alias;
1350
If this function called for query which update table (INSERT/UPDATE/...)
1351
then we have in table->table pointer to Table object which we are
1352
updating even if it is VIEW so we need TableList of this Table object
1353
to get right names (even if lower_case_table_names used).
1355
If this function called for CREATE command that we have not opened table
1356
(table->table equal to 0) and right names is in current TableList
1361
/* temporary table is always unique */
1362
if (table->table && table->table->s->tmp_table != NO_TMP_TABLE)
1364
table= table->find_underlying_table(table->table);
1366
as far as we have table->table we have to find real TableList of
1367
it in underlying tables
1372
t_name= table->table_name;
1373
t_alias= table->alias;
1377
if (((! (res= find_table_in_global_list(table_list, d_name, t_name))) &&
1378
(! (res= mysql_lock_have_duplicate(thd, table, table_list)))) ||
1379
((!res->table || res->table != table->table) &&
1380
(!check_alias || !(lower_case_table_names ?
1381
my_strcasecmp(files_charset_info, t_alias, res->alias) :
1382
strcmp(t_alias, res->alias))) &&
1383
res->select_lex && !res->select_lex->exclude_from_table_unique_test))
1386
If we found entry of this table or table of SELECT which already
1387
processed in derived table or top select of multi-update/multi-delete
1388
(exclude_from_table_unique_test) or prelocking placeholder.
1390
table_list= res->next_global;
1397
Issue correct error message in case we found 2 duplicate tables which
1398
prevent some update operation
1401
update_non_unique_table_error()
1402
update table which we try to update
1403
operation name of update operation
1404
duplicate duplicate table which we found
1407
here we hide view underlying tables if we have them
1410
void update_non_unique_table_error(TableList *update,
1411
const char *operation __attribute__((unused)),
1412
TableList *duplicate __attribute__((unused)))
1414
my_error(ER_UPDATE_TABLE_USED, MYF(0), update->alias);
1418
Table *find_temporary_table(THD *thd, const char *db, const char *table_name)
1420
TableList table_list;
1422
table_list.db= (char*) db;
1423
table_list.table_name= (char*) table_name;
1424
return find_temporary_table(thd, &table_list);
1428
Table *find_temporary_table(THD *thd, TableList *table_list)
1430
char key[MAX_DBKEY_LENGTH];
1434
key_length= create_table_def_key(thd, key, table_list, 1);
1435
for (table=thd->temporary_tables ; table ; table= table->next)
1437
if (table->s->table_cache_key.length == key_length &&
1438
!memcmp(table->s->table_cache_key.str, key, key_length))
1441
return(0); // Not a temporary table
1446
Drop a temporary table.
1448
Try to locate the table in the list of thd->temporary_tables.
1449
If the table is found:
1450
- if the table is being used by some outer statement, fail.
1451
- if the table is in thd->locked_tables, unlock it and
1452
remove it from the list of locked tables. Currently only transactional
1453
temporary tables are present in the locked_tables list.
1454
- Close the temporary table, remove its .FRM
1455
- remove the table from the list of temporary tables
1457
This function is used to drop user temporary tables, as well as
1458
internal tables created in CREATE TEMPORARY TABLE ... SELECT
1459
or ALTER Table. Even though part of the work done by this function
1460
is redundant when the table is internal, as long as we
1461
link both internal and user temporary tables into the same
1462
thd->temporary_tables list, it's impossible to tell here whether
1463
we're dealing with an internal or a user temporary table.
1465
@retval 0 the table was found and dropped successfully.
1466
@retval 1 the table was not found in the list of temporary tables
1468
@retval -1 the table is in use by a outer query
1471
int drop_temporary_table(THD *thd, TableList *table_list)
1475
if (!(table= find_temporary_table(thd, table_list)))
1478
/* Table might be in use by some outer statement. */
1479
if (table->query_id && table->query_id != thd->query_id)
1481
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1486
If LOCK TABLES list is not empty and contains this table,
1487
unlock the table and remove the table from this list.
1489
mysql_lock_remove(thd, thd->locked_tables, table, false);
1490
close_temporary_table(thd, table, 1, 1);
1495
unlink from thd->temporary tables and close temporary table
1498
void close_temporary_table(THD *thd, Table *table,
1499
bool free_share, bool delete_table)
1503
table->prev->next= table->next;
1504
if (table->prev->next)
1505
table->next->prev= table->prev;
1509
/* removing the item from the list */
1510
assert(table == thd->temporary_tables);
1512
slave must reset its temporary list pointer to zero to exclude
1513
passing non-zero value to end_slave via rli->save_temporary_tables
1514
when no temp tables opened, see an invariant below.
1516
thd->temporary_tables= table->next;
1517
if (thd->temporary_tables)
1518
table->next->prev= 0;
1520
if (thd->slave_thread)
1522
/* natural invariant of temporary_tables */
1523
assert(slave_open_temp_tables || !thd->temporary_tables);
1524
slave_open_temp_tables--;
1526
close_temporary(table, free_share, delete_table);
1532
Close and delete a temporary table
1535
This dosn't unlink table from thd->temporary
1536
If this is needed, use close_temporary_table()
1539
void close_temporary(Table *table, bool free_share, bool delete_table)
1541
handlerton *table_type= table->s->db_type();
1543
free_io_cache(table);
1546
Check that temporary table has not been created with
1547
frm_only because it has then not been created in any storage engine
1550
rm_temporary_table(table_type, table->s->path.str,
1551
table->s->tmp_table == TMP_TABLE_FRM_FILE_ONLY);
1554
free_table_share(table->s);
1555
free((char*) table);
1562
Used by ALTER Table when the table is a temporary one. It changes something
1563
only if the ALTER contained a RENAME clause (otherwise, table_name is the old
1565
Prepares a table cache key, which is the concatenation of db, table_name and
1566
thd->slave_proxy_id, separated by '\0'.
1569
bool rename_temporary_table(THD* thd, Table *table, const char *db,
1570
const char *table_name)
1573
uint32_t key_length;
1574
TABLE_SHARE *share= table->s;
1575
TableList table_list;
1577
if (!(key=(char*) alloc_root(&share->mem_root, MAX_DBKEY_LENGTH)))
1578
return(1); /* purecov: inspected */
1580
table_list.db= (char*) db;
1581
table_list.table_name= (char*) table_name;
1582
key_length= create_table_def_key(thd, key, &table_list, 1);
1583
share->set_table_cache_key(key, key_length);
1588
/* move table first in unused links */
1590
static void relink_unused(Table *table)
1592
if (table != unused_tables)
1594
table->prev->next=table->next; /* Remove from unused list */
1595
table->next->prev=table->prev;
1596
table->next=unused_tables; /* Link in unused tables */
1597
table->prev=unused_tables->prev;
1598
unused_tables->prev->next=table;
1599
unused_tables->prev=table;
1600
unused_tables=table;
1606
Remove all instances of table from thread's open list and
1609
@param thd Thread context
1610
@param find Table to remove
1611
@param unlock true - free all locks on tables removed that are
1612
done with LOCK TABLES
1615
@note When unlock parameter is false or current thread doesn't have
1616
any tables locked with LOCK TABLES, tables are assumed to be
1617
not locked (for example already unlocked).
1620
void unlink_open_table(THD *thd, Table *find, bool unlock)
1622
char key[MAX_DBKEY_LENGTH];
1623
uint32_t key_length= find->s->table_cache_key.length;
1624
Table *list, **prev;
1626
safe_mutex_assert_owner(&LOCK_open);
1628
memcpy(key, find->s->table_cache_key.str, key_length);
1630
Note that we need to hold LOCK_open while changing the
1631
open_tables list. Another thread may work on it.
1632
(See: remove_table_from_cache(), mysql_wait_completed_table())
1633
Closing a MERGE child before the parent would be fatal if the
1634
other thread tries to abort the MERGE lock in between.
1636
for (prev= &thd->open_tables; *prev; )
1640
if (list->s->table_cache_key.length == key_length &&
1641
!memcmp(list->s->table_cache_key.str, key, key_length))
1643
if (unlock && thd->locked_tables)
1644
mysql_lock_remove(thd, thd->locked_tables, list, true);
1646
/* Remove table from open_tables list. */
1649
hash_delete(&open_cache,(unsigned char*) list); // Close table
1653
/* Step to next entry in open_tables list. */
1658
// Notify any 'refresh' threads
1659
broadcast_refresh();
1665
Auxiliary routine which closes and drops open table.
1667
@param thd Thread handle
1668
@param table Table object for table to be dropped
1669
@param db_name Name of database for this table
1670
@param table_name Name of this table
1672
@note This routine assumes that table to be closed is open only
1673
by calling thread so we needn't wait until other threads
1674
will close the table. Also unless called under implicit or
1675
explicit LOCK TABLES mode it assumes that table to be
1676
dropped is already unlocked. In the former case it will
1677
also remove lock on the table. But one should not rely on
1678
this behaviour as it may change in future.
1679
Currently, however, this function is never called for a
1680
table that was locked with LOCK TABLES.
1683
void drop_open_table(THD *thd, Table *table, const char *db_name,
1684
const char *table_name)
1686
if (table->s->tmp_table)
1687
close_temporary_table(thd, table, 1, 1);
1690
handlerton *table_type= table->s->db_type();
1691
pthread_mutex_lock(&LOCK_open);
1693
unlink_open_table() also tells threads waiting for refresh or close
1694
that something has happened.
1696
unlink_open_table(thd, table, false);
1697
quick_rm_table(table_type, db_name, table_name, 0);
1698
pthread_mutex_unlock(&LOCK_open);
1704
Wait for condition but allow the user to send a kill to mysqld
1707
wait_for_condition()
1709
mutex mutex that is currently hold that is associated with condition
1710
Will be unlocked on return
1711
cond Condition to wait for
1714
void wait_for_condition(THD *thd, pthread_mutex_t *mutex, pthread_cond_t *cond)
1716
/* Wait until the current table is up to date */
1717
const char *proc_info;
1718
thd->mysys_var->current_mutex= mutex;
1719
thd->mysys_var->current_cond= cond;
1720
proc_info=thd->get_proc_info();
1721
thd_proc_info(thd, "Waiting for table");
1723
(void) pthread_cond_wait(cond, mutex);
1726
We must unlock mutex first to avoid deadlock becasue conditions are
1727
sent to this thread by doing locks in the following order:
1728
lock(mysys_var->mutex)
1729
lock(mysys_var->current_mutex)
1731
One by effect of this that one can only use wait_for_condition with
1732
condition variables that are guranteed to not disapper (freed) even if this
1736
pthread_mutex_unlock(mutex);
1737
pthread_mutex_lock(&thd->mysys_var->mutex);
1738
thd->mysys_var->current_mutex= 0;
1739
thd->mysys_var->current_cond= 0;
1740
thd_proc_info(thd, proc_info);
1741
pthread_mutex_unlock(&thd->mysys_var->mutex);
1747
Exclusively name-lock a table that is already write-locked by the
1750
@param thd current thread context
1751
@param tables table list containing one table to open.
1753
@return false on success, true otherwise.
1756
bool name_lock_locked_table(THD *thd, TableList *tables)
1758
/* Under LOCK TABLES we must only accept write locked tables. */
1759
tables->table= find_locked_table(thd, tables->db, tables->table_name);
1762
my_error(ER_TABLE_NOT_LOCKED, MYF(0), tables->alias);
1763
else if (tables->table->reginfo.lock_type < TL_WRITE_LOW_PRIORITY)
1764
my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0), tables->alias);
1768
Ensures that table is opened only by this thread and that no
1769
other statement will open this table.
1771
wait_while_table_is_used(thd, tables->table, HA_EXTRA_FORCE_REOPEN);
1780
Open table which is already name-locked by this thread.
1783
reopen_name_locked_table()
1785
table_list TableList object for table to be open, TableList::table
1786
member should point to Table object which was used for
1788
link_in true - if Table object for table to be opened should be
1789
linked into THD::open_tables list.
1790
false - placeholder used for name-locking is already in
1791
this list so we only need to preserve Table::next
1795
This function assumes that its caller already acquired LOCK_open mutex.
1802
bool reopen_name_locked_table(THD* thd, TableList* table_list, bool link_in)
1804
Table *table= table_list->table;
1806
char *table_name= table_list->table_name;
1809
safe_mutex_assert_owner(&LOCK_open);
1811
if (thd->killed || !table)
1816
if (open_unireg_entry(thd, table, table_list, table_name,
1817
table->s->table_cache_key.str,
1818
table->s->table_cache_key.length))
1820
intern_close_table(table);
1822
If there was an error during opening of table (for example if it
1823
does not exist) '*table' object can be wiped out. To be able
1824
properly release name-lock in this case we should restore this
1825
object to its original state.
1833
We want to prevent other connections from opening this table until end
1834
of statement as it is likely that modifications of table's metadata are
1835
not yet finished (for example CREATE TRIGGER have to change .TRG file,
1836
or we might want to drop table if CREATE TABLE ... SELECT fails).
1837
This also allows us to assume that no other connection will sneak in
1838
before we will get table-level lock on this table.
1841
table->in_use = thd;
1845
table->next= thd->open_tables;
1846
thd->open_tables= table;
1851
Table object should be already in THD::open_tables list so we just
1852
need to set Table::next correctly.
1854
table->next= orig_table.next;
1857
table->tablenr=thd->current_tablenr++;
1858
table->used_fields=0;
1859
table->const_table=0;
1860
table->null_row= false;
1861
table->maybe_null= false;
1862
table->force_index= false;
1863
table->status=STATUS_NO_RECORD;
1869
Create and insert into table cache placeholder for table
1870
which will prevent its opening (or creation) (a.k.a lock
1873
@param thd Thread context
1874
@param key Table cache key for name to be locked
1875
@param key_length Table cache key length
1877
@return Pointer to Table object used for name locking or 0 in
1881
Table *table_cache_insert_placeholder(THD *thd, const char *key,
1882
uint32_t key_length)
1888
safe_mutex_assert_owner(&LOCK_open);
1891
Create a table entry with the right key and with an old refresh version
1892
Note that we must use my_multi_malloc() here as this is freed by the
1895
if (!my_multi_malloc(MYF(MY_WME | MY_ZEROFILL),
1896
&table, sizeof(*table),
1897
&share, sizeof(*share),
1898
&key_buff, key_length,
1903
share->set_table_cache_key(key_buff, key, key_length);
1904
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table
1906
table->locked_by_name=1;
1908
if (my_hash_insert(&open_cache, (unsigned char*)table))
1910
free((unsigned char*) table);
1919
Obtain an exclusive name lock on the table if it is not cached
1922
@param thd Thread context
1923
@param db Name of database
1924
@param table_name Name of table
1925
@param[out] table Out parameter which is either:
1926
- set to NULL if table cache contains record for
1928
- set to point to the Table instance used for
1931
@note This function takes into account all records for table in table
1932
cache, even placeholders used for name-locking. This means that
1933
'table' parameter can be set to NULL for some situations when
1934
table does not really exist.
1936
@retval true Error occured (OOM)
1937
@retval false Success. 'table' parameter set according to above rules.
1940
bool lock_table_name_if_not_cached(THD *thd, const char *db,
1941
const char *table_name, Table **table)
1943
char key[MAX_DBKEY_LENGTH];
1944
uint32_t key_length;
1946
key_length= (uint)(my_stpcpy(my_stpcpy(key, db) + 1, table_name) - key) + 1;
1947
pthread_mutex_lock(&LOCK_open);
1949
if (hash_search(&open_cache, (unsigned char *)key, key_length))
1951
pthread_mutex_unlock(&LOCK_open);
1955
if (!(*table= table_cache_insert_placeholder(thd, key, key_length)))
1957
pthread_mutex_unlock(&LOCK_open);
1960
(*table)->open_placeholder= 1;
1961
(*table)->next= thd->open_tables;
1962
thd->open_tables= *table;
1963
pthread_mutex_unlock(&LOCK_open);
1969
Check that table exists in table definition cache, on disk
1970
or in some storage engine.
1972
@param thd Thread context
1973
@param table Table list element
1974
@param[out] exists Out parameter which is set to true if table
1975
exists and to false otherwise.
1977
@note This function assumes that caller owns LOCK_open mutex.
1978
It also assumes that the fact that there are no name-locks
1979
on the table was checked beforehand.
1981
@note If there is no .FRM file for the table but it exists in one
1982
of engines (e.g. it was created on another node of NDB cluster)
1983
this function will fetch and create proper .FRM file for it.
1985
@retval true Some error occured
1986
@retval false No error. 'exists' out parameter set accordingly.
1989
bool check_if_table_exists(THD *thd, TableList *table, bool *exists)
1991
char path[FN_REFLEN];
1994
safe_mutex_assert_owner(&LOCK_open);
1998
if (get_cached_table_share(table->db, table->table_name))
2001
build_table_filename(path, sizeof(path) - 1, table->db, table->table_name,
2004
if (!access(path, F_OK))
2007
/* .FRM file doesn't exist. Check if some engine can provide it. */
2009
rc= ha_create_table_from_engine(thd, table->db, table->table_name);
2013
/* Table does not exists in engines as well. */
2019
/* Table exists in some engine and .FRM for it was created. */
2024
my_printf_error(ER_UNKNOWN_ERROR, "Failed to open '%-.64s', error while "
2025
"unpacking from engine", MYF(0), table->table_name);
2037
table_list Open first table in list.
2038
refresh INOUT Pointer to memory that will be set to 1 if
2039
we need to close all tables and reopen them.
2040
If this is a NULL pointer, then the table is not
2041
put in the thread-open-list.
2042
flags Bitmap of flags to modify how open works:
2043
DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
2044
someone has done a flush or namelock on it.
2045
No version number checking is done.
2046
DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
2047
table not the base table or view.
2050
Uses a cache of open tables to find a table not in use.
2052
If table list element for the table to be opened has "create" flag
2053
set and table does not exist, this function will automatically insert
2054
a placeholder for exclusive name lock into the open tables cache and
2055
will return the Table instance that corresponds to this placeholder.
2058
NULL Open failed. If refresh is set then one should close
2059
all other tables and retry the open.
2060
# Success. Pointer to Table object for open table.
2064
Table *open_table(THD *thd, TableList *table_list, bool *refresh, uint32_t flags)
2066
register Table *table;
2067
char key[MAX_DBKEY_LENGTH];
2068
unsigned int key_length;
2069
char *alias= table_list->alias;
2070
HASH_SEARCH_STATE state;
2072
/* Parsing of partitioning information from .frm needs thd->lex set up. */
2073
assert(thd->lex->is_lex_started);
2075
/* find a unused table in the open table cache */
2079
/* an open table operation needs a lot of the stack space */
2080
if (check_stack_overrun(thd, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
2086
key_length= (create_table_def_key(thd, key, table_list, 1) -
2087
TMP_TABLE_KEY_EXTRA);
2090
Unless requested otherwise, try to resolve this table in the list
2091
of temporary tables of this thread. In MySQL temporary tables
2092
are always thread-local and "shadow" possible base tables with the
2093
same name. This block implements the behaviour.
2094
TODO: move this block into a separate function.
2097
for (table= thd->temporary_tables; table ; table=table->next)
2099
if (table->s->table_cache_key.length == key_length +
2100
TMP_TABLE_KEY_EXTRA &&
2101
!memcmp(table->s->table_cache_key.str, key,
2102
key_length + TMP_TABLE_KEY_EXTRA))
2105
We're trying to use the same temporary table twice in a query.
2106
Right now we don't support this because a temporary table
2107
is always represented by only one Table object in THD, and
2108
it can not be cloned. Emit an error for an unsupported behaviour.
2110
if (table->query_id)
2112
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
2115
table->query_id= thd->query_id;
2116
thd->thread_specific_used= true;
2122
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
2124
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
2129
The table is not temporary - if we're in pre-locked or LOCK TABLES
2130
mode, let's try to find the requested table in the list of pre-opened
2131
and locked tables. If the table is not there, return an error - we can't
2132
open not pre-opened tables in pre-locked/LOCK TABLES mode.
2133
TODO: move this block into a separate function.
2135
if (thd->locked_tables)
2136
{ // Using table locks
2137
Table *best_table= 0;
2138
int best_distance= INT_MIN;
2139
bool check_if_used= false;
2140
for (table=thd->open_tables; table ; table=table->next)
2142
if (table->s->table_cache_key.length == key_length &&
2143
!memcmp(table->s->table_cache_key.str, key, key_length))
2145
if (check_if_used && table->query_id &&
2146
table->query_id != thd->query_id)
2149
If we are in stored function or trigger we should ensure that
2150
we won't change table that is already used by calling statement.
2151
So if we are opening table for writing, we should check that it
2152
is not already open by some calling stamement.
2154
my_error(ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG, MYF(0),
2155
table->s->table_name.str);
2159
When looking for a usable Table, ignore MERGE children, as they
2160
belong to their parent and cannot be used explicitly.
2162
if (!my_strcasecmp(system_charset_info, table->alias, alias) &&
2163
table->query_id != thd->query_id) /* skip tables already used */
2165
int distance= ((int) table->reginfo.lock_type -
2166
(int) table_list->lock_type);
2168
Find a table that either has the exact lock type requested,
2169
or has the best suitable lock. In case there is no locked
2170
table that has an equal or higher lock than requested,
2171
we us the closest matching lock to be able to produce an error
2172
message about wrong lock mode on the table. The best_table
2173
is changed if bd < 0 <= d or bd < d < 0 or 0 <= d < bd.
2175
distance < 0 - No suitable lock found
2176
distance > 0 - we have lock mode higher then we require
2177
distance == 0 - we have lock mode exactly which we need
2179
if ((best_distance < 0 && distance > best_distance) || (distance >= 0 && distance < best_distance))
2181
best_distance= distance;
2183
if (best_distance == 0 && !check_if_used)
2186
If we have found perfect match and we don't need to check that
2187
table is not used by one of calling statements (assuming that
2188
we are inside of function or trigger) we can finish iterating
2189
through open tables list.
2200
table->query_id= thd->query_id;
2204
No table in the locked tables list. In case of explicit LOCK TABLES
2205
this can happen if a user did not include the able into the list.
2206
In case of pre-locked mode locked tables list is generated automatically,
2207
so we may only end up here if the table did not exist when
2208
locked tables list was created.
2210
my_error(ER_TABLE_NOT_LOCKED, MYF(0), alias);
2215
Non pre-locked/LOCK TABLES mode, and the table is not temporary:
2216
this is the normal use case.
2218
- try to find the table in the table cache.
2219
- if one of the discovered Table instances is name-locked
2220
(table->s->version == 0) or some thread has started FLUSH TABLES
2221
(refresh_version > table->s->version), back off -- we have to wait
2222
until no one holds a name lock on the table.
2223
- if there is no such Table in the name cache, read the table definition
2224
and insert it into the cache.
2225
We perform all of the above under LOCK_open which currently protects
2226
the open cache (also known as table cache) and table definitions stored
2230
pthread_mutex_lock(&LOCK_open);
2233
If it's the first table from a list of tables used in a query,
2234
remember refresh_version (the version of open_cache state).
2235
If the version changes while we're opening the remaining tables,
2236
we will have to back off, close all the tables opened-so-far,
2237
and try to reopen them.
2238
Note: refresh_version is currently changed only during FLUSH TABLES.
2240
if (!thd->open_tables)
2241
thd->version=refresh_version;
2242
else if ((thd->version != refresh_version) &&
2243
! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
2245
/* Someone did a refresh while thread was opening tables */
2248
pthread_mutex_unlock(&LOCK_open);
2253
In order for the back off and re-start process to work properly,
2254
handler tables having old versions (due to FLUSH TABLES or pending
2255
name-lock) MUST be closed. This is specially important if a name-lock
2256
is pending for any table of the handler_tables list, otherwise a
2259
if (thd->handler_tables)
2260
mysql_ha_flush(thd);
2263
Actually try to find the table in the open_cache.
2264
The cache may contain several "Table" instances for the same
2265
physical table. The instances that are currently "in use" by
2266
some thread have their "in_use" member != NULL.
2267
There is no good reason for having more than one entry in the
2268
hash for the same physical table, except that we use this as
2269
an implicit "pending locks queue" - see
2270
wait_for_locked_table_names for details.
2272
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
2274
table && table->in_use ;
2275
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
2279
Here we flush tables marked for flush.
2280
Normally, table->s->version contains the value of
2281
refresh_version from the moment when this table was
2282
(re-)opened and added to the cache.
2283
If since then we did (or just started) FLUSH TABLES
2284
statement, refresh_version has been increased.
2285
For "name-locked" Table instances, table->s->version is set
2286
to 0 (see lock_table_name for details).
2287
In case there is a pending FLUSH TABLES or a name lock, we
2288
need to back off and re-start opening tables.
2289
If we do not back off now, we may dead lock in case of lock
2290
order mismatch with some other thread:
2291
c1: name lock t1; -- sort of exclusive lock
2292
c2: open t2; -- sort of shared lock
2293
c1: name lock t2; -- blocks
2294
c2: open t1; -- blocks
2296
if (table->needs_reopen_or_name_lock())
2298
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
2300
/* Force close at once after usage */
2301
thd->version= table->s->version;
2305
/* Avoid self-deadlocks by detecting self-dependencies. */
2306
if (table->open_placeholder && table->in_use == thd)
2308
pthread_mutex_unlock(&LOCK_open);
2309
my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
2314
Back off, part 1: mark the table as "unused" for the
2315
purpose of name-locking by setting table->db_stat to 0. Do
2316
that only for the tables in this thread that have an old
2317
table->s->version (this is an optimization (?)).
2318
table->db_stat == 0 signals wait_for_locked_table_names
2319
that the tables in question are not used any more. See
2320
table_is_used call for details.
2322
Notice that HANDLER tables were already taken care of by
2323
the earlier call to mysql_ha_flush() in this same critical
2326
close_old_data_files(thd,thd->open_tables,0,0);
2328
Back-off part 2: try to avoid "busy waiting" on the table:
2329
if the table is in use by some other thread, we suspend
2330
and wait till the operation is complete: when any
2331
operation that juggles with table->s->version completes,
2332
it broadcasts COND_refresh condition variable.
2333
If 'old' table we met is in use by current thread we return
2334
without waiting since in this situation it's this thread
2335
which is responsible for broadcasting on COND_refresh
2336
(and this was done already in close_old_data_files()).
2337
Good example of such situation is when we have statement
2338
that needs two instances of table and FLUSH TABLES comes
2339
after we open first instance but before we open second
2342
if (table->in_use != thd)
2344
/* wait_for_conditionwill unlock LOCK_open for us */
2345
wait_for_condition(thd, &LOCK_open, &COND_refresh);
2349
pthread_mutex_unlock(&LOCK_open);
2352
There is a refresh in progress for this table.
2353
Signal the caller that it has to try again.
2362
/* Unlink the table from "unused_tables" list. */
2363
if (table == unused_tables)
2365
unused_tables=unused_tables->next; // Remove from link
2366
if (table == unused_tables)
2369
table->prev->next=table->next; /* Remove from unused list */
2370
table->next->prev=table->prev;
2375
/* Insert a new Table instance into the open cache */
2377
/* Free cache if too big */
2378
while (open_cache.records > table_cache_size && unused_tables)
2379
hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
2381
if (table_list->create)
2385
if (check_if_table_exists(thd, table_list, &exists))
2387
pthread_mutex_unlock(&LOCK_open);
2394
Table to be created, so we need to create placeholder in table-cache.
2396
if (!(table= table_cache_insert_placeholder(thd, key, key_length)))
2398
pthread_mutex_unlock(&LOCK_open);
2402
Link placeholder to the open tables list so it will be automatically
2403
removed once tables are closed. Also mark it so it won't be ignored
2404
by other trying to take name-lock.
2406
table->open_placeholder= 1;
2407
table->next= thd->open_tables;
2408
thd->open_tables= table;
2409
pthread_mutex_unlock(&LOCK_open);
2412
/* Table exists. Let us try to open it. */
2415
/* make a new table */
2416
if (!(table=(Table*) my_malloc(sizeof(*table),MYF(MY_WME))))
2418
pthread_mutex_unlock(&LOCK_open);
2422
error= open_unireg_entry(thd, table, table_list, alias, key, key_length);
2423
/* Combine the follow two */
2426
free((unsigned char*)table);
2427
pthread_mutex_unlock(&LOCK_open);
2432
free((unsigned char*)table);
2433
pthread_mutex_unlock(&LOCK_open);
2436
my_hash_insert(&open_cache,(unsigned char*) table);
2439
pthread_mutex_unlock(&LOCK_open);
2442
table->next=thd->open_tables; /* Link into simple list */
2443
thd->open_tables=table;
2445
table->reginfo.lock_type=TL_READ; /* Assume read */
2448
assert(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
2450
if (thd->lex->need_correct_ident())
2451
table->alias_name_used= my_strcasecmp(table_alias_charset,
2452
table->s->table_name.str, alias);
2453
/* Fix alias if table name changes */
2454
if (strcmp(table->alias, alias))
2456
uint32_t length=(uint) strlen(alias)+1;
2457
table->alias= (char*) my_realloc((char*) table->alias, length,
2459
memcpy((void*) table->alias, alias, length);
2461
/* These variables are also set in reopen_table() */
2462
table->tablenr=thd->current_tablenr++;
2463
table->used_fields=0;
2464
table->const_table=0;
2465
table->null_row= false;
2466
table->maybe_null= false;
2467
table->force_index= false;
2468
table->status=STATUS_NO_RECORD;
2469
table->insert_values= 0;
2470
/* Catch wrong handling of the auto_increment_field_not_null. */
2471
assert(!table->auto_increment_field_not_null);
2472
table->auto_increment_field_not_null= false;
2473
if (table->timestamp_field)
2474
table->timestamp_field_type= table->timestamp_field->get_auto_set_type();
2475
table->pos_in_table_list= table_list;
2476
table->clear_column_bitmaps();
2477
assert(table->key_read == 0);
2482
Table *find_locked_table(THD *thd, const char *db,const char *table_name)
2484
char key[MAX_DBKEY_LENGTH];
2485
uint32_t key_length=(uint) (my_stpcpy(my_stpcpy(key,db)+1,table_name)-key)+1;
2487
for (Table *table=thd->open_tables; table ; table=table->next)
2489
if (table->s->table_cache_key.length == key_length &&
2490
!memcmp(table->s->table_cache_key.str, key, key_length))
2498
Reopen an table because the definition has changed.
2505
The data file for the table is already closed and the share is released
2506
The table has a 'dummy' share that mainly contains database and table name.
2510
1 error. The old table object is not changed.
2513
bool reopen_table(Table *table)
2519
TableList table_list;
2520
THD *thd= table->in_use;
2522
assert(table->s->ref_count == 0);
2523
assert(!table->sort.io_cache);
2527
sql_print_error(_("Table %s had a open data handler in reopen_table"),
2530
memset(&table_list, 0, sizeof(TableList));
2531
table_list.db= table->s->db.str;
2532
table_list.table_name= table->s->table_name.str;
2533
table_list.table= table;
2535
if (wait_for_locked_table_names(thd, &table_list))
2536
return(1); // Thread was killed
2538
if (open_unireg_entry(thd, &tmp, &table_list,
2540
table->s->table_cache_key.str,
2541
table->s->table_cache_key.length))
2544
/* This list copies variables set by open_table */
2545
tmp.tablenr= table->tablenr;
2546
tmp.used_fields= table->used_fields;
2547
tmp.const_table= table->const_table;
2548
tmp.null_row= table->null_row;
2549
tmp.maybe_null= table->maybe_null;
2550
tmp.status= table->status;
2552
tmp.s->table_map_id= table->s->table_map_id;
2556
tmp.reginfo.lock_type=table->reginfo.lock_type;
2558
/* Replace table in open list */
2559
tmp.next= table->next;
2560
tmp.prev= table->prev;
2563
closefrm(table, 1); // close file, free everything
2566
table->default_column_bitmaps();
2567
table->file->change_table_ptr(table, table->s);
2569
assert(table->alias != 0);
2570
for (field=table->field ; *field ; field++)
2572
(*field)->table= (*field)->orig_table= table;
2573
(*field)->table_name= &table->alias;
2575
for (key=0 ; key < table->s->keys ; key++)
2577
for (part=0 ; part < table->key_info[key].usable_key_parts ; part++)
2578
table->key_info[key].key_part[part].field->table= table;
2581
Do not attach MERGE children here. The children might be reopened
2582
after the parent. Attach children after reopening all tables that
2583
require reopen. See for example reopen_tables().
2586
broadcast_refresh();
2595
Close all instances of a table open by this thread and replace
2596
them with exclusive name-locks.
2598
@param thd Thread context
2599
@param db Database name for the table to be closed
2600
@param table_name Name of the table to be closed
2602
@note This function assumes that if we are not under LOCK TABLES,
2603
then there is only one table open and locked. This means that
2604
the function probably has to be adjusted before it can be used
2605
anywhere outside ALTER Table.
2607
@note Must not use TABLE_SHARE::table_name/db of the table being closed,
2608
the strings are used in a loop even after the share may be freed.
2611
void close_data_files_and_morph_locks(THD *thd, const char *db,
2612
const char *table_name)
2616
safe_mutex_assert_owner(&LOCK_open);
2621
If we are not under LOCK TABLES we should have only one table
2622
open and locked so it makes sense to remove the lock at once.
2624
mysql_unlock_tables(thd, thd->lock);
2629
Note that open table list may contain a name-lock placeholder
2630
for target table name if we process ALTER Table ... RENAME.
2631
So loop below makes sense even if we are not under LOCK TABLES.
2633
for (table=thd->open_tables; table ; table=table->next)
2635
if (!strcmp(table->s->table_name.str, table_name) &&
2636
!strcmp(table->s->db.str, db))
2638
if (thd->locked_tables)
2640
mysql_lock_remove(thd, thd->locked_tables, table, true);
2642
table->open_placeholder= 1;
2643
close_handle_and_leave_table_as_lock(table);
2651
Reopen all tables with closed data files.
2653
@param thd Thread context
2654
@param get_locks Should we get locks after reopening tables ?
2655
@param mark_share_as_old Mark share as old to protect from a impending
2658
@note Since this function can't properly handle prelocking and
2659
create placeholders it should be used in very special
2660
situations like FLUSH TABLES or ALTER Table. In general
2661
case one should just repeat open_tables()/lock_tables()
2662
combination when one needs tables to be reopened (for
2663
example see open_and_lock_tables()).
2665
@note One should have lock on LOCK_open when calling this.
2667
@return false in case of success, true - otherwise.
2670
bool reopen_tables(THD *thd, bool get_locks, bool mark_share_as_old)
2672
Table *table,*next,**prev;
2673
Table **tables,**tables_ptr; // For locks
2674
bool error=0, not_used;
2675
const uint32_t flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
2676
DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
2677
DRIZZLE_LOCK_IGNORE_FLUSH;
2679
if (!thd->open_tables)
2682
safe_mutex_assert_owner(&LOCK_open);
2686
The ptr is checked later
2687
Do not handle locks of MERGE children.
2690
for (table= thd->open_tables; table ; table=table->next)
2692
tables= (Table**) my_alloca(sizeof(Table*)*opens);
2695
tables= &thd->open_tables;
2698
prev= &thd->open_tables;
2699
for (table=thd->open_tables; table ; table=next)
2701
uint32_t db_stat=table->db_stat;
2703
if (!tables || (!db_stat && reopen_table(table)))
2705
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
2706
hash_delete(&open_cache,(unsigned char*) table);
2713
/* Do not handle locks of MERGE children. */
2714
if (get_locks && !db_stat)
2715
*tables_ptr++= table; // need new lock on this
2716
if (mark_share_as_old)
2718
table->s->version=0;
2719
table->open_placeholder= 0;
2724
if (tables != tables_ptr) // Should we get back old locks
2728
We should always get these locks. Anyway, we must not go into
2729
wait_for_tables() as it tries to acquire LOCK_open, which is
2732
thd->some_tables_deleted=0;
2733
if ((lock= mysql_lock_tables(thd, tables, (uint) (tables_ptr - tables),
2736
thd->locked_tables=mysql_lock_merge(thd->locked_tables,lock);
2741
This case should only happen if there is a bug in the reopen logic.
2742
Need to issue error message to have a reply for the application.
2743
Not exactly what happened though, but close enough.
2745
my_error(ER_LOCK_DEADLOCK, MYF(0));
2749
if (get_locks && tables)
2751
my_afree((unsigned char*) tables);
2753
broadcast_refresh();
2759
Close handlers for tables in list, but leave the Table structure
2760
intact so that we can re-open these quickly.
2762
@param thd Thread context
2763
@param table Head of the list of Table objects
2764
@param morph_locks true - remove locks which we have on tables being closed
2765
but ensure that no DML or DDL will sneak in before
2766
we will re-open the table (i.e. temporarily morph
2767
our table-level locks into name-locks).
2769
@param send_refresh Should we awake waiters even if we didn't close any tables?
2772
static void close_old_data_files(THD *thd, Table *table, bool morph_locks,
2775
bool found= send_refresh;
2777
for (; table ; table=table->next)
2780
Reopen marked for flush.
2782
if (table->needs_reopen_or_name_lock())
2789
Table *ulcktbl= table;
2790
if (ulcktbl->lock_count)
2793
Wake up threads waiting for table-level lock on this table
2794
so they won't sneak in when we will temporarily remove our
2795
lock on it. This will also give them a chance to close their
2796
instances of this table.
2798
mysql_lock_abort(thd, ulcktbl, true);
2799
mysql_lock_remove(thd, thd->locked_tables, ulcktbl, true);
2800
ulcktbl->lock_count= 0;
2802
if ((ulcktbl != table) && ulcktbl->db_stat)
2805
Close the parent too. Note that parent can come later in
2806
the list of tables. It will then be noticed as closed and
2807
as a placeholder. When this happens, do not clear the
2808
placeholder flag. See the branch below ("***").
2810
ulcktbl->open_placeholder= 1;
2811
close_handle_and_leave_table_as_lock(ulcktbl);
2814
We want to protect the table from concurrent DDL operations
2815
(like RENAME Table) until we will re-open and re-lock it.
2817
table->open_placeholder= 1;
2819
close_handle_and_leave_table_as_lock(table);
2821
else if (table->open_placeholder && !morph_locks)
2824
We come here only in close-for-back-off scenario. So we have to
2825
"close" create placeholder here to avoid deadlocks (for example,
2826
in case of concurrent execution of CREATE TABLE t1 SELECT * FROM t2
2827
and RENAME Table t2 TO t1). In close-for-re-open scenario we will
2828
probably want to let it stay.
2830
Note "***": We must not enter this branch if the placeholder
2831
flag has been set because of a former close through a child.
2832
See above the comment that refers to this note.
2834
table->open_placeholder= 0;
2839
broadcast_refresh();
2845
Wait until all threads has closed the tables in the list
2846
We have also to wait if there is thread that has a lock on this table even
2847
if the table is closed
2850
bool table_is_used(Table *table, bool wait_for_name_lock)
2854
char *key= table->s->table_cache_key.str;
2855
uint32_t key_length= table->s->table_cache_key.length;
2857
HASH_SEARCH_STATE state;
2858
for (Table *search= (Table*) hash_first(&open_cache, (unsigned char*) key,
2859
key_length, &state);
2861
search= (Table*) hash_next(&open_cache, (unsigned char*) key,
2862
key_length, &state))
2864
if (search->in_use == table->in_use)
2865
continue; // Name locked by this thread
2867
We can't use the table under any of the following conditions:
2868
- There is an name lock on it (Table is to be deleted or altered)
2869
- If we are in flush table and we didn't execute the flush
2870
- If the table engine is open and it's an old version
2871
(We must wait until all engines are shut down to use the table)
2873
if ( (search->locked_by_name && wait_for_name_lock) ||
2874
(search->is_name_opened() && search->needs_reopen_or_name_lock()))
2877
} while ((table=table->next));
2882
/* Wait until all used tables are refreshed */
2884
bool wait_for_tables(THD *thd)
2888
thd_proc_info(thd, "Waiting for tables");
2889
pthread_mutex_lock(&LOCK_open);
2890
while (!thd->killed)
2892
thd->some_tables_deleted=0;
2893
close_old_data_files(thd,thd->open_tables,0,dropping_tables != 0);
2894
mysql_ha_flush(thd);
2895
if (!table_is_used(thd->open_tables,1))
2897
(void) pthread_cond_wait(&COND_refresh,&LOCK_open);
2900
result= 1; // aborted
2903
/* Now we can open all tables without any interference */
2904
thd_proc_info(thd, "Reopen tables");
2905
thd->version= refresh_version;
2906
result=reopen_tables(thd,0,0);
2908
pthread_mutex_unlock(&LOCK_open);
2909
thd_proc_info(thd, 0);
2915
drop tables from locked list
2918
drop_locked_tables()
2921
table_name Table name
2924
This is only called on drop tables
2926
The Table object for the dropped table is unlocked but still kept around
2927
as a name lock, which means that the table will be available for other
2928
thread as soon as we call unlock_table_names().
2929
If there is multiple copies of the table locked, all copies except
2930
the first, which acts as a name lock, is removed.
2933
# If table existed, return table
2934
0 Table was not locked
2938
Table *drop_locked_tables(THD *thd,const char *db, const char *table_name)
2940
Table *table,*next,**prev, *found= 0;
2941
prev= &thd->open_tables;
2944
Note that we need to hold LOCK_open while changing the
2945
open_tables list. Another thread may work on it.
2946
(See: remove_table_from_cache(), mysql_wait_completed_table())
2947
Closing a MERGE child before the parent would be fatal if the
2948
other thread tries to abort the MERGE lock in between.
2950
for (table= thd->open_tables; table ; table=next)
2953
if (!strcmp(table->s->table_name.str, table_name) &&
2954
!strcmp(table->s->db.str, db))
2956
mysql_lock_remove(thd, thd->locked_tables, table, true);
2961
/* Close engine table, but keep object around as a name lock */
2965
table->file->close();
2970
/* We already have a name lock, remove copy */
2971
hash_delete(&open_cache,(unsigned char*) table);
2982
broadcast_refresh();
2983
if (thd->locked_tables && thd->locked_tables->table_count == 0)
2985
free((unsigned char*) thd->locked_tables);
2986
thd->locked_tables=0;
2993
If we have the table open, which only happens when a LOCK Table has been
2994
done on the table, change the lock type to a lock that will abort all
2995
other threads trying to get the lock.
2998
void abort_locked_tables(THD *thd,const char *db, const char *table_name)
3001
for (table= thd->open_tables; table ; table= table->next)
3003
if (!strcmp(table->s->table_name.str, table_name) &&
3004
!strcmp(table->s->db.str, db))
3006
/* If MERGE child, forward lock handling to parent. */
3007
mysql_lock_abort(thd, table, true);
3015
Function to assign a new table map id to a table share.
3019
share - Pointer to table share structure
3023
We are intentionally not checking that share->mutex is locked
3024
since this function should only be called when opening a table
3025
share and before it is entered into the table_def_cache (meaning
3026
that it cannot be fetched by another thread, even accidentally).
3031
The LOCK_open mutex is locked
3035
share->table_map_id is given a value that with a high certainty is
3036
not used by any other table (the only case where a table id can be
3037
reused is on wrap-around, which means more than 4 billion table
3038
share opens have been executed while one table was open all the
3041
share->table_map_id is not UINT32_MAX.
3043
void assign_new_table_id(TABLE_SHARE *share)
3045
static uint32_t last_table_id= UINT32_MAX;
3048
assert(share != NULL);
3049
safe_mutex_assert_owner(&LOCK_open);
3051
ulong tid= ++last_table_id; /* get next id */
3053
There is one reserved number that cannot be used. Remember to
3054
change this when 6-byte global table id's are introduced.
3056
if (unlikely(tid == UINT32_MAX))
3057
tid= ++last_table_id;
3058
share->table_map_id= tid;
3060
/* Post conditions */
3061
assert(share->table_map_id != UINT32_MAX);
3067
Load a table definition from file and open unireg table
3072
entry Store open table definition here
3073
table_list TableList with db, table_name
3075
cache_key Key for share_cache
3076
cache_key_length length of cache_key
3079
Extra argument for open is taken from thd->open_options
3080
One must have a lock on LOCK_open when calling this function
3087
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
3089
char *cache_key, uint32_t cache_key_length)
3093
uint32_t discover_retry_count= 0;
3095
safe_mutex_assert_owner(&LOCK_open);
3097
if (!(share= get_table_share_with_create(thd, table_list, cache_key,
3099
table_list->i_s_requested_object,
3103
while ((error= open_table_from_share(thd, share, alias,
3104
(uint) (HA_OPEN_KEYFILE |
3109
thd->open_options, entry, OTM_OPEN)))
3111
if (error == 7) // Table def changed
3113
share->version= 0; // Mark share as old
3114
if (discover_retry_count++) // Retry once
3119
Here we should wait until all threads has released the table.
3120
For now we do one retry. This may cause a deadlock if there
3121
is other threads waiting for other tables used by this thread.
3123
Proper fix would be to if the second retry failed:
3124
- Mark that table def changed
3125
- Return from open table
3126
- Close all tables used by this thread
3127
- Start waiting that the share is released
3128
- Retry by opening all tables again
3130
if (ha_create_table_from_engine(thd, table_list->db,
3131
table_list->table_name))
3135
To avoid deadlock, only wait for release if no one else is
3138
if (share->ref_count != 1)
3140
/* Free share and wait until it's released by all threads */
3141
release_table_share(share, RELEASE_WAIT_FOR_DROP);
3144
drizzle_reset_errors(thd, 1); // Clear warnings
3145
thd->clear_error(); // Clear error message
3150
if (!entry->s || !entry->s->crashed)
3152
// Code below is for repairing a crashed file
3153
if ((error= lock_table_name(thd, table_list, true)))
3157
if (wait_for_locked_table_names(thd, table_list))
3159
unlock_table_name(thd, table_list);
3163
pthread_mutex_unlock(&LOCK_open);
3164
thd->clear_error(); // Clear error message
3166
if (open_table_from_share(thd, share, alias,
3167
(uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3171
ha_open_options | HA_OPEN_FOR_REPAIR,
3172
entry, OTM_OPEN) || ! entry->file ||
3173
(entry->file->is_crashed() && entry->file->ha_check_and_repair(thd)))
3175
/* Give right error message */
3177
my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
3178
sql_print_error(_("Couldn't repair table: %s.%s"), share->db.str,
3179
share->table_name.str);
3185
thd->clear_error(); // Clear error message
3186
pthread_mutex_lock(&LOCK_open);
3187
unlock_table_name(thd, table_list);
3195
If we are here, there was no fatal error (but error may be still
3198
if (unlikely(entry->file->implicit_emptied))
3200
entry->file->implicit_emptied= 0;
3201
if (mysql_bin_log.is_open())
3204
uint32_t query_buf_size= 20 + share->db.length + share->table_name.length +1;
3205
if ((query= (char*) my_malloc(query_buf_size,MYF(MY_WME))))
3207
/* this DELETE FROM is needed even with row-based binlogging */
3208
end = strxmov(my_stpcpy(query, "DELETE FROM `"),
3209
share->db.str,"`.`",share->table_name.str,"`", NULL);
3210
thd->binlog_query(THD::STMT_QUERY_TYPE,
3211
query, (ulong)(end-query), false, false);
3217
As replication is maybe going to be corrupted, we need to warn the
3218
DBA on top of warning the client (which will automatically be done
3219
because of MYF(MY_WME) in my_malloc() above).
3221
sql_print_error(_("When opening HEAP table, could not allocate memory "
3222
"to write 'DELETE FROM `%s`.`%s`' to the binary log"),
3223
table_list->db, table_list->table_name);
3232
release_table_share(share, RELEASE_NORMAL);
3238
Open all tables in list
3242
thd - thread handler
3243
start - list of tables in/out
3244
counter - number of opened tables will be return using this parameter
3245
flags - bitmap of flags to modify how the tables will be open:
3246
DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
3247
done a flush or namelock on it.
3250
Unless we are already in prelocked mode, this function will also precache
3251
all SP/SFs explicitly or implicitly (via views and triggers) used by the
3252
query and add tables needed for their execution to table list. If resulting
3253
tables list will be non empty it will mark query as requiring precaching.
3254
Prelocked mode will be enabled for such query during lock_tables() call.
3256
If query for which we are opening tables is already marked as requiring
3257
prelocking it won't do such precaching and will simply reuse table list
3258
which is already built.
3265
int open_tables(THD *thd, TableList **start, uint32_t *counter, uint32_t flags)
3267
TableList *tables= NULL;
3270
MEM_ROOT new_frm_mem;
3271
/* Also used for indicating that prelocking is need */
3272
bool safe_to_ignore_table;
3275
temporary mem_root for new .frm parsing.
3276
TODO: variables for size
3278
init_sql_alloc(&new_frm_mem, 8024, 8024);
3280
thd->current_tablenr= 0;
3283
thd_proc_info(thd, "Opening tables");
3286
For every table in the list of tables to open, try to find or open
3289
for (tables= *start; tables ;tables= tables->next_global)
3291
safe_to_ignore_table= false;
3294
Ignore placeholders for derived tables. After derived tables
3295
processing, link to created temporary table will be put here.
3296
If this is derived table for view then we still want to process
3297
routines used by this view.
3299
if (tables->derived)
3304
If this TableList object is a placeholder for an information_schema
3305
table, create a temporary table to represent the information_schema
3306
table in the query. Do not fill it yet - will be filled during
3309
if (tables->schema_table)
3311
if (!mysql_schema_table(thd, thd->lex, tables))
3318
Not a placeholder: must be a base table or a view, and the table is
3319
not opened yet. Try to open the table.
3322
tables->table= open_table(thd, tables, &refresh, flags);
3326
free_root(&new_frm_mem, MYF(MY_KEEP_PREALLOC));
3328
if (refresh) // Refresh in progress
3331
We have met name-locked or old version of table. Now we have
3332
to close all tables which are not up to date. We also have to
3333
throw away set of prelocked tables (and thus close tables from
3334
this set that were open by now) since it possible that one of
3335
tables which determined its content was changed.
3337
Instead of implementing complex/non-robust logic mentioned
3338
above we simply close and then reopen all tables.
3340
In order to prepare for recalculation of set of prelocked tables
3341
we pretend that we have finished calculation which we were doing
3344
close_tables_for_reopen(thd, start);
3348
if (safe_to_ignore_table)
3351
result= -1; // Fatal error
3354
if (tables->lock_type != TL_UNLOCK && ! thd->locked_tables)
3356
if (tables->lock_type == TL_WRITE_DEFAULT)
3357
tables->table->reginfo.lock_type= thd->update_lock_default;
3358
else if (tables->table->s->tmp_table == NO_TMP_TABLE)
3359
tables->table->reginfo.lock_type= tables->lock_type;
3363
thd_proc_info(thd, 0);
3364
free_root(&new_frm_mem, MYF(0)); // Free pre-alloced block
3366
if (result && tables)
3369
Some functions determine success as (tables->table != NULL).
3370
tables->table is in thd->open_tables.
3372
tables->table= NULL;
3379
Check that lock is ok for tables; Call start stmt if ok
3382
check_lock_and_start_stmt()
3384
table_list Table to check
3385
lock_type Lock used for table
3392
static bool check_lock_and_start_stmt(THD *thd, Table *table,
3393
thr_lock_type lock_type)
3397
if ((int) lock_type >= (int) TL_WRITE_ALLOW_READ &&
3398
(int) table->reginfo.lock_type < (int) TL_WRITE_ALLOW_READ)
3400
my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0),table->alias);
3403
if ((error=table->file->start_stmt(thd, lock_type)))
3405
table->file->print_error(error,MYF(0));
3413
@brief Open and lock one table
3415
@param[in] thd thread handle
3416
@param[in] table_l table to open is first table in this list
3417
@param[in] lock_type lock to use for table
3420
@retval != NULL OK, opened table returned
3424
If ok, the following are also set:
3425
table_list->lock_type lock_type
3426
table_list->table table
3429
If table_l is a list, not a single table, the list is temporarily
3433
This function is meant as a replacement for open_ltable() when
3434
MERGE tables can be opened. open_ltable() cannot open MERGE tables.
3436
There may be more differences between open_n_lock_single_table() and
3437
open_ltable(). One known difference is that open_ltable() does
3438
neither call decide_logging_format() nor handle some other logging
3439
and locking issues because it does not call lock_tables().
3442
Table *open_n_lock_single_table(THD *thd, TableList *table_l,
3443
thr_lock_type lock_type)
3445
TableList *save_next_global;
3447
/* Remember old 'next' pointer. */
3448
save_next_global= table_l->next_global;
3450
table_l->next_global= NULL;
3452
/* Set requested lock type. */
3453
table_l->lock_type= lock_type;
3455
/* Open the table. */
3456
if (simple_open_n_lock_tables(thd, table_l))
3457
table_l->table= NULL; /* Just to be sure. */
3460
table_l->next_global= save_next_global;
3462
return(table_l->table);
3467
Open and lock one table
3472
table_list Table to open is first table in this list
3473
lock_type Lock to use for open
3474
lock_flags Flags passed to mysql_lock_table
3477
This function don't do anything like SP/SF/views/triggers analysis done
3478
in open_tables(). It is intended for opening of only one concrete table.
3479
And used only in special contexts.
3485
If ok, the following are also set:
3486
table_list->lock_type lock_type
3487
table_list->table table
3490
Table *open_ltable(THD *thd, TableList *table_list, thr_lock_type lock_type,
3491
uint32_t lock_flags)
3496
thd_proc_info(thd, "Opening table");
3497
thd->current_tablenr= 0;
3498
while (!(table= open_table(thd, table_list, &refresh, 0)) &&
3504
table_list->lock_type= lock_type;
3505
table_list->table= table;
3506
if (thd->locked_tables)
3508
if (check_lock_and_start_stmt(thd, table, lock_type))
3513
assert(thd->lock == 0); // You must lock everything at once
3514
if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
3515
if (! (thd->lock= mysql_lock_tables(thd, &table_list->table, 1,
3516
lock_flags, &refresh)))
3521
thd_proc_info(thd, 0);
3527
Open all tables in list, locks them and optionally process derived tables.
3530
open_and_lock_tables_derived()
3531
thd - thread handler
3532
tables - list of tables for open&locking
3533
derived - if to handle derived tables
3540
The lock will automaticaly be freed by close_thread_tables()
3543
There are two convenience functions:
3544
- simple_open_n_lock_tables(thd, tables) without derived handling
3545
- open_and_lock_tables(thd, tables) with derived handling
3546
Both inline functions call open_and_lock_tables_derived() with
3547
the third argument set appropriately.
3550
int open_and_lock_tables_derived(THD *thd, TableList *tables, bool derived)
3557
if (open_tables(thd, &tables, &counter, 0))
3560
if (!lock_tables(thd, tables, counter, &need_reopen))
3564
close_tables_for_reopen(thd, &tables);
3567
(mysql_handle_derived(thd->lex, &mysql_derived_prepare) ||
3568
(thd->fill_derived_tables() &&
3569
mysql_handle_derived(thd->lex, &mysql_derived_filling))))
3570
return(true); /* purecov: inspected */
3576
Open all tables in list and process derived tables
3579
open_normal_and_derived_tables
3580
thd - thread handler
3581
tables - list of tables for open
3582
flags - bitmap of flags to modify how the tables will be open:
3583
DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
3584
done a flush or namelock on it.
3591
This is to be used on prepare stage when you don't read any
3592
data from the tables.
3595
bool open_normal_and_derived_tables(THD *thd, TableList *tables, uint32_t flags)
3598
assert(!thd->fill_derived_tables());
3599
if (open_tables(thd, &tables, &counter, flags) ||
3600
mysql_handle_derived(thd->lex, &mysql_derived_prepare))
3601
return(true); /* purecov: inspected */
3607
Decide on logging format to use for the statement.
3609
Compute the capabilities vector for the involved storage engines
3610
and mask out the flags for the binary log. Right now, the binlog
3611
flags only include the capabilities of the storage engines, so this
3614
We now have three alternatives that prevent the statement from
3617
1. If there are no capabilities left (all flags are clear) it is
3618
not possible to log the statement at all, so we roll back the
3619
statement and report an error.
3621
2. Statement mode is set, but the capabilities indicate that
3622
statement format is not possible.
3624
3. Row mode is set, but the capabilities indicate that row
3625
format is not possible.
3627
4. Statement is unsafe, but the capabilities indicate that row
3628
format is not possible.
3630
If we are in MIXED mode, we then decide what logging format to use:
3632
1. If the statement is unsafe, row-based logging is used.
3634
2. If statement-based logging is not possible, row-based logging is
3637
3. Otherwise, statement-based logging is used.
3639
@param thd Client thread
3640
@param tables Tables involved in the query
3643
int decide_logging_format(THD *thd, TableList *tables)
3645
if (mysql_bin_log.is_open() && (thd->options & OPTION_BIN_LOG))
3647
handler::Table_flags flags_some_set= handler::Table_flags();
3648
handler::Table_flags flags_all_set= ~handler::Table_flags();
3649
bool multi_engine= false;
3650
void* prev_ht= NULL;
3651
for (TableList *table= tables; table; table= table->next_global)
3653
if (!table->placeholder() && table->lock_type >= TL_WRITE_ALLOW_WRITE)
3655
uint64_t const flags= table->table->file->ha_table_flags();
3656
if (prev_ht && prev_ht != table->table->file->ht)
3658
prev_ht= table->table->file->ht;
3659
flags_all_set &= flags;
3660
flags_some_set |= flags;
3665
if (flags_all_set == 0)
3667
my_error((error= ER_BINLOG_LOGGING_IMPOSSIBLE), MYF(0),
3668
"Statement cannot be logged to the binary log in"
3669
" row-based nor statement-based format");
3671
else if (thd->variables.binlog_format == BINLOG_FORMAT_STMT &&
3672
(flags_all_set & HA_BINLOG_STMT_CAPABLE) == 0)
3674
my_error((error= ER_BINLOG_LOGGING_IMPOSSIBLE), MYF(0),
3675
"Statement-based format required for this statement,"
3676
" but not allowed by this combination of engines");
3678
else if ((thd->variables.binlog_format == BINLOG_FORMAT_ROW ||
3679
thd->lex->is_stmt_unsafe()) &&
3680
(flags_all_set & HA_BINLOG_ROW_CAPABLE) == 0)
3682
my_error((error= ER_BINLOG_LOGGING_IMPOSSIBLE), MYF(0),
3683
"Row-based format required for this statement,"
3684
" but not allowed by this combination of engines");
3691
We switch to row-based format if we are in mixed mode and one of
3692
the following are true:
3694
1. If the statement is unsafe
3695
2. If statement format cannot be used
3697
Observe that point to cannot be decided before the tables
3698
involved in a statement has been checked, i.e., we cannot put
3699
this code in reset_current_stmt_binlog_row_based(), it has to be
3702
if (thd->lex->is_stmt_unsafe() ||
3703
(flags_all_set & HA_BINLOG_STMT_CAPABLE) == 0)
3705
thd->set_current_stmt_binlog_row_based_if_mixed();
3713
Lock all tables in list
3718
tables Tables to lock
3719
count Number of opened tables
3720
need_reopen Out parameter which if true indicates that some
3721
tables were dropped or altered during this call
3722
and therefore invoker should reopen tables and
3723
try to lock them once again (in this case
3724
lock_tables() will also return error).
3727
You can't call lock_tables twice, as this would break the dead-lock-free
3728
handling thr_lock gives us. You most always get all needed locks at
3731
If query for which we are calling this function marked as requring
3732
prelocking, this function will do implicit LOCK TABLES and change
3733
thd::prelocked_mode accordingly.
3740
int lock_tables(THD *thd, TableList *tables, uint32_t count, bool *need_reopen)
3745
We can't meet statement requiring prelocking if we already
3748
*need_reopen= false;
3751
return(decide_logging_format(thd, tables));
3753
if (!thd->locked_tables)
3755
assert(thd->lock == 0); // You must lock everything at once
3756
Table **start,**ptr;
3757
uint32_t lock_flag= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN;
3759
if (!(ptr=start=(Table**) thd->alloc(sizeof(Table*)*count)))
3761
for (table= tables; table; table= table->next_global)
3763
if (!table->placeholder())
3764
*(ptr++)= table->table;
3767
if (!(thd->lock= mysql_lock_tables(thd, start, (uint) (ptr - start),
3768
lock_flag, need_reopen)))
3775
TableList *first_not_own= thd->lex->first_not_own_table();
3777
When open_and_lock_tables() is called for a single table out of
3778
a table list, the 'next_global' chain is temporarily broken. We
3779
may not find 'first_not_own' before the end of the "list".
3780
Look for example at those places where open_n_lock_single_table()
3781
is called. That function implements the temporary breaking of
3782
a table list for opening a single table.
3785
table && table != first_not_own;
3786
table= table->next_global)
3788
if (!table->placeholder() &&
3789
check_lock_and_start_stmt(thd, table->table, table->lock_type))
3796
return(decide_logging_format(thd, tables));
3801
Prepare statement for reopening of tables and recalculation of set of
3805
close_tables_for_reopen()
3806
thd in Thread context
3807
tables in/out List of tables which we were trying to open and lock
3811
void close_tables_for_reopen(THD *thd, TableList **tables)
3814
If table list consists only from tables from prelocking set, table list
3815
for new attempt should be empty, so we have to update list's root pointer.
3817
if (thd->lex->first_not_own_table() == *tables)
3819
thd->lex->chop_off_not_own_tables();
3820
for (TableList *tmp= *tables; tmp; tmp= tmp->next_global)
3822
close_thread_tables(thd);
3827
Open a single table without table caching and don't set it in open_list
3830
open_temporary_table()
3832
path Path (without .frm)
3834
table_name Table name
3835
link_in_list 1 if table should be linked into thd->temporary_tables
3838
Used by alter_table to open a temporary table and when creating
3839
a temporary table with CREATE TEMPORARY ...
3846
Table *open_temporary_table(THD *thd, const char *path, const char *db,
3847
const char *table_name, bool link_in_list,
3848
open_table_mode open_mode)
3852
char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
3853
uint32_t key_length;
3854
TableList table_list;
3856
table_list.db= (char*) db;
3857
table_list.table_name= (char*) table_name;
3858
/* Create the cache_key for temporary tables */
3859
key_length= create_table_def_key(thd, cache_key, &table_list, 1);
3861
if (!(tmp_table= (Table*) my_malloc(sizeof(*tmp_table) + sizeof(*share) +
3862
strlen(path)+1 + key_length,
3864
return(0); /* purecov: inspected */
3866
share= (TABLE_SHARE*) (tmp_table+1);
3867
tmp_path= (char*) (share+1);
3868
saved_cache_key= my_stpcpy(tmp_path, path)+1;
3869
memcpy(saved_cache_key, cache_key, key_length);
3871
init_tmp_table_share(thd, share, saved_cache_key, key_length,
3872
strchr(saved_cache_key, '\0')+1, tmp_path);
3874
if (open_table_def(thd, share, 0) ||
3875
open_table_from_share(thd, share, table_name,
3876
(open_mode == OTM_ALTER) ? 0 :
3877
(uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3879
(open_mode == OTM_ALTER) ?
3880
(EXTRA_RECORD | OPEN_FRM_FILE_ONLY)
3883
tmp_table, open_mode))
3885
/* No need to lock share->mutex as this is not needed for tmp tables */
3886
free_table_share(share);
3887
free((char*) tmp_table);
3891
tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
3892
if (open_mode == OTM_ALTER)
3895
Temporary table has been created with frm_only
3896
and has not been created in any storage engine
3898
share->tmp_table= TMP_TABLE_FRM_FILE_ONLY;
3901
share->tmp_table= (tmp_table->file->has_transactions() ?
3902
TRANSACTIONAL_TMP_TABLE : NON_TRANSACTIONAL_TMP_TABLE);
3906
/* growing temp list at the head */
3907
tmp_table->next= thd->temporary_tables;
3908
if (tmp_table->next)
3909
tmp_table->next->prev= tmp_table;
3910
thd->temporary_tables= tmp_table;
3911
thd->temporary_tables->prev= 0;
3912
if (thd->slave_thread)
3913
slave_open_temp_tables++;
3915
tmp_table->pos_in_table_list= 0;
3920
bool rm_temporary_table(handlerton *base, char *path, bool frm_only)
3926
my_stpcpy(ext= strchr(path, '\0'), reg_ext);
3927
if (my_delete(path,MYF(0)))
3928
error=1; /* purecov: inspected */
3929
*ext= 0; // remove extension
3930
file= get_new_handler((TABLE_SHARE*) 0, current_thd->mem_root, base);
3931
if (!frm_only && file && file->ha_delete_table(path))
3934
sql_print_warning(_("Could not remove temporary table: '%s', error: %d"),
3942
/*****************************************************************************
3943
* The following find_field_in_XXX procedures implement the core of the
3944
* name resolution functionality. The entry point to resolve a column name in a
3945
* list of tables is 'find_field_in_tables'. It calls 'find_field_in_table_ref'
3946
* for each table reference. In turn, depending on the type of table reference,
3947
* 'find_field_in_table_ref' calls one of the 'find_field_in_XXX' procedures
3948
* below specific for the type of table reference.
3949
******************************************************************************/
3951
/* Special Field pointers as return values of find_field_in_XXX functions. */
3952
Field *not_found_field= (Field*) 0x1;
3953
Field *view_ref_found= (Field*) 0x2;
3955
#define WRONG_GRANT (Field*) -1
3957
static void update_field_dependencies(THD *thd, Field *field, Table *table)
3959
if (thd->mark_used_columns != MARK_COLUMNS_NONE)
3961
MY_BITMAP *current_bitmap, *other_bitmap;
3964
We always want to register the used keys, as the column bitmap may have
3965
been set for all fields (for example for view).
3968
table->covering_keys.intersect(field->part_of_key);
3969
table->merge_keys.merge(field->part_of_key);
3971
if (thd->mark_used_columns == MARK_COLUMNS_READ)
3973
current_bitmap= table->read_set;
3974
other_bitmap= table->write_set;
3978
current_bitmap= table->write_set;
3979
other_bitmap= table->read_set;
3982
if (bitmap_fast_test_and_set(current_bitmap, field->field_index))
3984
if (thd->mark_used_columns == MARK_COLUMNS_WRITE)
3985
thd->dup_field= field;
3988
if (table->get_fields_in_item_tree)
3989
field->flags|= GET_FIXED_FIELDS_FLAG;
3990
table->used_fields++;
3992
else if (table->get_fields_in_item_tree)
3993
field->flags|= GET_FIXED_FIELDS_FLAG;
3999
Find field by name in a NATURAL/USING join table reference.
4002
find_field_in_natural_join()
4003
thd [in] thread handler
4004
table_ref [in] table reference to search
4005
name [in] name of field
4006
length [in] length of name
4007
ref [in/out] if 'name' is resolved to a view field, ref is
4008
set to point to the found view field
4009
register_tree_change [in] true if ref is not stack variable and we
4010
need register changes in item tree
4011
actual_table [out] the original table reference where the field
4012
belongs - differs from 'table_list' only for
4016
Search for a field among the result fields of a NATURAL/USING join.
4017
Notice that this procedure is called only for non-qualified field
4018
names. In the case of qualified fields, we search directly the base
4019
tables of a natural join.
4022
NULL if the field was not found
4023
WRONG_GRANT if no access rights to the found field
4024
# Pointer to the found Field
4028
find_field_in_natural_join(THD *thd, TableList *table_ref, const char *name,
4029
uint32_t length __attribute__((unused)),
4030
Item **ref __attribute__((unused)), bool register_tree_change __attribute__((unused)),
4031
TableList **actual_table)
4033
List_iterator_fast<Natural_join_column>
4034
field_it(*(table_ref->join_columns));
4035
Natural_join_column *nj_col, *curr_nj_col;
4038
assert(table_ref->is_natural_join && table_ref->join_columns);
4039
assert(*actual_table == NULL);
4041
for (nj_col= NULL, curr_nj_col= field_it++; curr_nj_col;
4042
curr_nj_col= field_it++)
4044
if (!my_strcasecmp(system_charset_info, curr_nj_col->name(), name))
4048
my_error(ER_NON_UNIQ_ERROR, MYF(0), name, thd->where);
4051
nj_col= curr_nj_col;
4057
/* This is a base table. */
4058
assert(nj_col->table_ref->table == nj_col->table_field->table);
4059
found_field= nj_col->table_field;
4060
update_field_dependencies(thd, found_field, nj_col->table_ref->table);
4063
*actual_table= nj_col->table_ref;
4065
return(found_field);
4070
Find field by name in a base table or a view with temp table algorithm.
4073
find_field_in_table()
4075
table table where to search for the field
4077
length length of name
4078
allow_rowid do allow finding of "_rowid" field?
4079
cached_field_index_ptr cached position in field list (used to speedup
4080
lookup for fields in prepared tables)
4083
0 field is not found
4088
find_field_in_table(THD *thd, Table *table, const char *name, uint32_t length,
4089
bool allow_rowid, uint32_t *cached_field_index_ptr)
4091
Field **field_ptr, *field;
4092
uint32_t cached_field_index= *cached_field_index_ptr;
4094
/* We assume here that table->field < NO_CACHED_FIELD_INDEX = UINT_MAX */
4095
if (cached_field_index < table->s->fields &&
4096
!my_strcasecmp(system_charset_info,
4097
table->field[cached_field_index]->field_name, name))
4098
field_ptr= table->field + cached_field_index;
4099
else if (table->s->name_hash.records)
4101
field_ptr= (Field**) hash_search(&table->s->name_hash, (unsigned char*) name,
4106
field_ptr points to field in TABLE_SHARE. Convert it to the matching
4109
field_ptr= (table->field + (field_ptr - table->s->field));
4114
if (!(field_ptr= table->field))
4116
for (; *field_ptr; ++field_ptr)
4117
if (!my_strcasecmp(system_charset_info, (*field_ptr)->field_name, name))
4121
if (field_ptr && *field_ptr)
4123
*cached_field_index_ptr= field_ptr - table->field;
4129
my_strcasecmp(system_charset_info, name, "_rowid") ||
4130
table->s->rowid_field_offset == 0)
4132
field= table->field[table->s->rowid_field_offset-1];
4135
update_field_dependencies(thd, field, table);
4142
Find field in a table reference.
4145
find_field_in_table_ref()
4146
thd [in] thread handler
4147
table_list [in] table reference to search
4148
name [in] name of field
4149
length [in] field length of name
4150
item_name [in] name of item if it will be created (VIEW)
4151
db_name [in] optional database name that qualifies the
4152
table_name [in] optional table name that qualifies the field
4153
ref [in/out] if 'name' is resolved to a view field, ref
4154
is set to point to the found view field
4155
check_privileges [in] check privileges
4156
allow_rowid [in] do allow finding of "_rowid" field?
4157
cached_field_index_ptr [in] cached position in field list (used to
4158
speedup lookup for fields in prepared tables)
4159
register_tree_change [in] true if ref is not stack variable and we
4160
need register changes in item tree
4161
actual_table [out] the original table reference where the field
4162
belongs - differs from 'table_list' only for
4163
NATURAL_USING joins.
4166
Find a field in a table reference depending on the type of table
4167
reference. There are three types of table references with respect
4168
to the representation of their result columns:
4169
- an array of Field_translator objects for MERGE views and some
4170
information_schema tables,
4171
- an array of Field objects (and possibly a name hash) for stored
4173
- a list of Natural_join_column objects for NATURAL/USING joins.
4174
This procedure detects the type of the table reference 'table_list'
4175
and calls the corresponding search routine.
4178
0 field is not found
4179
view_ref_found found value in VIEW (real result is in *ref)
4184
find_field_in_table_ref(THD *thd, TableList *table_list,
4185
const char *name, uint32_t length,
4186
const char *item_name, const char *db_name,
4187
const char *table_name, Item **ref,
4188
bool check_privileges, bool allow_rowid,
4189
uint32_t *cached_field_index_ptr,
4190
bool register_tree_change, TableList **actual_table)
4194
assert(table_list->alias);
4199
Check that the table and database that qualify the current field name
4200
are the same as the table reference we are going to search for the field.
4202
Exclude from the test below nested joins because the columns in a
4203
nested join generally originate from different tables. Nested joins
4204
also have no table name, except when a nested join is a merge view
4205
or an information schema table.
4207
We include explicitly table references with a 'field_translation' table,
4208
because if there are views over natural joins we don't want to search
4209
inside the view, but we want to search directly in the view columns
4210
which are represented as a 'field_translation'.
4212
TODO: Ensure that table_name, db_name and tables->db always points to
4215
if (/* Exclude nested joins. */
4216
(!table_list->nested_join ||
4217
/* Include merge views and information schema tables. */
4218
table_list->field_translation) &&
4220
Test if the field qualifiers match the table reference we plan
4223
table_name && table_name[0] &&
4224
(my_strcasecmp(table_alias_charset, table_list->alias, table_name) ||
4225
(db_name && db_name[0] && table_list->db && table_list->db[0] &&
4226
strcmp(db_name, table_list->db))))
4229
*actual_table= NULL;
4231
if (table_list->field_translation)
4234
else if (!table_list->nested_join)
4236
/* 'table_list' is a stored table. */
4237
assert(table_list->table);
4238
if ((fld= find_field_in_table(thd, table_list->table, name, length,
4240
cached_field_index_ptr)))
4241
*actual_table= table_list;
4246
'table_list' is a NATURAL/USING join, or an operand of such join that
4247
is a nested join itself.
4249
If the field name we search for is qualified, then search for the field
4250
in the table references used by NATURAL/USING the join.
4252
if (table_name && table_name[0])
4254
List_iterator<TableList> it(table_list->nested_join->join_list);
4256
while ((table= it++))
4258
if ((fld= find_field_in_table_ref(thd, table, name, length, item_name,
4259
db_name, table_name, ref,
4260
check_privileges, allow_rowid,
4261
cached_field_index_ptr,
4262
register_tree_change, actual_table)))
4268
Non-qualified field, search directly in the result columns of the
4269
natural join. The condition of the outer IF is true for the top-most
4270
natural join, thus if the field is not qualified, we will search
4271
directly the top-most NATURAL/USING join.
4273
fld= find_field_in_natural_join(thd, table_list, name, length, ref,
4274
register_tree_change, actual_table);
4279
if (thd->mark_used_columns != MARK_COLUMNS_NONE)
4282
Get rw_set correct for this field so that the handler
4283
knows that this field is involved in the query and gets
4286
Field *field_to_set= NULL;
4287
if (fld == view_ref_found)
4289
Item *it= (*ref)->real_item();
4290
if (it->type() == Item::FIELD_ITEM)
4291
field_to_set= ((Item_field*)it)->field;
4294
if (thd->mark_used_columns == MARK_COLUMNS_READ)
4295
it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
4302
Table *table= field_to_set->table;
4303
if (thd->mark_used_columns == MARK_COLUMNS_READ)
4304
bitmap_set_bit(table->read_set, field_to_set->field_index);
4306
bitmap_set_bit(table->write_set, field_to_set->field_index);
4315
Find field in table, no side effects, only purpose is to check for field
4316
in table object and get reference to the field if found.
4319
find_field_in_table_sef()
4321
table table where to find
4322
name Name of field searched for
4325
0 field is not found
4329
Field *find_field_in_table_sef(Table *table, const char *name)
4332
if (table->s->name_hash.records)
4334
field_ptr= (Field**)hash_search(&table->s->name_hash,(unsigned char*) name,
4339
field_ptr points to field in TABLE_SHARE. Convert it to the matching
4342
field_ptr= (table->field + (field_ptr - table->s->field));
4347
if (!(field_ptr= table->field))
4349
for (; *field_ptr; ++field_ptr)
4350
if (!my_strcasecmp(system_charset_info, (*field_ptr)->field_name, name))
4361
Find field in table list.
4364
find_field_in_tables()
4365
thd pointer to current thread structure
4366
item field item that should be found
4367
first_table list of tables to be searched for item
4368
last_table end of the list of tables to search for item. If NULL
4369
then search to the end of the list 'first_table'.
4370
ref if 'item' is resolved to a view field, ref is set to
4371
point to the found view field
4372
report_error Degree of error reporting:
4373
- IGNORE_ERRORS then do not report any error
4374
- IGNORE_EXCEPT_NON_UNIQUE report only non-unique
4375
fields, suppress all other errors
4376
- REPORT_EXCEPT_NON_UNIQUE report all other errors
4377
except when non-unique fields were found
4379
check_privileges need to check privileges
4380
register_tree_change true if ref is not a stack variable and we
4381
to need register changes in item tree
4384
0 If error: the found field is not unique, or there are
4385
no sufficient access priviliges for the found field,
4386
or the field is qualified with non-existing table.
4387
not_found_field The function was called with report_error ==
4388
(IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
4389
field was not found.
4390
view_ref_found View field is found, item passed through ref parameter
4391
found field If a item was resolved to some field
4395
find_field_in_tables(THD *thd, Item_ident *item,
4396
TableList *first_table, TableList *last_table,
4397
Item **ref, find_item_error_report_type report_error,
4398
bool check_privileges, bool register_tree_change)
4401
const char *db= item->db_name;
4402
const char *table_name= item->table_name;
4403
const char *name= item->field_name;
4404
uint32_t length=(uint) strlen(name);
4405
char name_buff[NAME_LEN+1];
4406
TableList *cur_table= first_table;
4407
TableList *actual_table;
4410
if (!table_name || !table_name[0])
4412
table_name= 0; // For easier test
4416
allow_rowid= table_name || (cur_table && !cur_table->next_local);
4418
if (item->cached_table)
4421
This shortcut is used by prepared statements. We assume that
4422
TableList *first_table is not changed during query execution (which
4423
is true for all queries except RENAME but luckily RENAME doesn't
4424
use fields...) so we can rely on reusing pointer to its member.
4425
With this optimization we also miss case when addition of one more
4426
field makes some prepared query ambiguous and so erroneous, but we
4427
accept this trade off.
4429
TableList *table_ref= item->cached_table;
4431
The condition (table_ref->view == NULL) ensures that we will call
4432
find_field_in_table even in the case of information schema tables
4433
when table_ref->field_translation != NULL.
4435
if (table_ref->table)
4436
found= find_field_in_table(thd, table_ref->table, name, length,
4437
true, &(item->cached_field_index));
4439
found= find_field_in_table_ref(thd, table_ref, name, length, item->name,
4440
NULL, NULL, ref, check_privileges,
4441
true, &(item->cached_field_index),
4442
register_tree_change,
4446
if (found == WRONG_GRANT)
4450
Only views fields should be marked as dependent, not an underlying
4454
SELECT_LEX *current_sel= thd->lex->current_select;
4455
SELECT_LEX *last_select= table_ref->select_lex;
4457
If the field was an outer referencee, mark all selects using this
4458
sub query as dependent on the outer query
4460
if (current_sel != last_select)
4461
mark_select_range_as_dependent(thd, last_select, current_sel,
4468
if (db && lower_case_table_names)
4471
convert database to lower case for comparison.
4472
We can't do this in Item_field as this would change the
4473
'name' of the item which may be used in the select list
4475
strmake(name_buff, db, sizeof(name_buff)-1);
4476
my_casedn_str(files_charset_info, name_buff);
4481
last_table= last_table->next_name_resolution_table;
4483
for (; cur_table != last_table ;
4484
cur_table= cur_table->next_name_resolution_table)
4486
Field *cur_field= find_field_in_table_ref(thd, cur_table, name, length,
4487
item->name, db, table_name, ref,
4488
(thd->lex->sql_command ==
4490
? false : check_privileges,
4492
&(item->cached_field_index),
4493
register_tree_change,
4497
if (cur_field == WRONG_GRANT)
4499
if (thd->lex->sql_command != SQLCOM_SHOW_FIELDS)
4503
cur_field= find_field_in_table_ref(thd, cur_table, name, length,
4504
item->name, db, table_name, ref,
4507
&(item->cached_field_index),
4508
register_tree_change,
4512
Field *nf=new Field_null(NULL,0,Field::NONE,
4513
cur_field->field_name,
4515
nf->init(cur_table->table);
4521
Store the original table of the field, which may be different from
4522
cur_table in the case of NATURAL/USING join.
4524
item->cached_table= (!actual_table->cacheable_table || found) ?
4529
If we found a fully qualified field we return it directly as it can't
4537
if (report_error == REPORT_ALL_ERRORS ||
4538
report_error == IGNORE_EXCEPT_NON_UNIQUE)
4539
my_error(ER_NON_UNIQ_ERROR, MYF(0),
4540
table_name ? item->full_name() : name, thd->where);
4551
If the field was qualified and there were no tables to search, issue
4552
an error that an unknown table was given. The situation is detected
4553
as follows: if there were no tables we wouldn't go through the loop
4554
and cur_table wouldn't be updated by the loop increment part, so it
4555
will be equal to the first table.
4557
if (table_name && (cur_table == first_table) &&
4558
(report_error == REPORT_ALL_ERRORS ||
4559
report_error == REPORT_EXCEPT_NON_UNIQUE))
4561
char buff[NAME_LEN*2+1];
4564
strxnmov(buff,sizeof(buff)-1,db,".",table_name,NULL);
4567
my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, thd->where);
4571
if (report_error == REPORT_ALL_ERRORS ||
4572
report_error == REPORT_EXCEPT_NON_UNIQUE)
4573
my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(), thd->where);
4575
found= not_found_field;
4582
Find Item in list of items (find_field_in_tables analog)
4585
is it better return only counter?
4591
counter To return number of found item
4593
REPORT_ALL_ERRORS report errors, return 0 if error
4594
REPORT_EXCEPT_NOT_FOUND Do not report 'not found' error and
4595
return not_found_item, report other errors,
4597
IGNORE_ERRORS Do not report errors, return 0 if error
4598
resolution Set to the resolution type if the item is found
4599
(it says whether the item is resolved
4600
against an alias name,
4601
or as a field name without alias,
4602
or as a field hidden by alias,
4606
0 Item is not found or item is not unique,
4607
error message is reported
4608
not_found_item Function was called with
4609
report_error == REPORT_EXCEPT_NOT_FOUND and
4610
item was not found. No error message was reported
4614
/* Special Item pointer to serve as a return value from find_item_in_list(). */
4615
Item **not_found_item= (Item**) 0x1;
4619
find_item_in_list(Item *find, List<Item> &items, uint32_t *counter,
4620
find_item_error_report_type report_error,
4621
enum_resolution_type *resolution)
4623
List_iterator<Item> li(items);
4624
Item **found=0, **found_unaliased= 0, *item;
4625
const char *db_name=0;
4626
const char *field_name=0;
4627
const char *table_name=0;
4628
bool found_unaliased_non_uniq= 0;
4630
true if the item that we search for is a valid name reference
4631
(and not an item that happens to have a name).
4633
bool is_ref_by_name= 0;
4634
uint32_t unaliased_counter= 0;
4636
*resolution= NOT_RESOLVED;
4638
is_ref_by_name= (find->type() == Item::FIELD_ITEM ||
4639
find->type() == Item::REF_ITEM);
4642
field_name= ((Item_ident*) find)->field_name;
4643
table_name= ((Item_ident*) find)->table_name;
4644
db_name= ((Item_ident*) find)->db_name;
4647
for (uint32_t i= 0; (item=li++); i++)
4649
if (field_name && item->real_item()->type() == Item::FIELD_ITEM)
4651
Item_ident *item_field= (Item_ident*) item;
4654
In case of group_concat() with ORDER BY condition in the QUERY
4655
item_field can be field of temporary table without item name
4656
(if this field created from expression argument of group_concat()),
4657
=> we have to check presence of name before compare
4659
if (!item_field->name)
4665
If table name is specified we should find field 'field_name' in
4666
table 'table_name'. According to SQL-standard we should ignore
4667
aliases in this case.
4669
Since we should NOT prefer fields from the select list over
4670
other fields from the tables participating in this select in
4671
case of ambiguity we have to do extra check outside this function.
4673
We use strcmp for table names and database names as these may be
4674
case sensitive. In cases where they are not case sensitive, they
4675
are always in lower case.
4677
item_field->field_name and item_field->table_name can be 0x0 if
4678
item is not fix_field()'ed yet.
4680
if (item_field->field_name && item_field->table_name &&
4681
!my_strcasecmp(system_charset_info, item_field->field_name,
4683
!my_strcasecmp(table_alias_charset, item_field->table_name,
4685
(!db_name || (item_field->db_name &&
4686
!strcmp(item_field->db_name, db_name))))
4688
if (found_unaliased)
4690
if ((*found_unaliased)->eq(item, 0))
4693
Two matching fields in select list.
4694
We already can bail out because we are searching through
4695
unaliased names only and will have duplicate error anyway.
4697
if (report_error != IGNORE_ERRORS)
4698
my_error(ER_NON_UNIQ_ERROR, MYF(0),
4699
find->full_name(), current_thd->where);
4702
found_unaliased= li.ref();
4703
unaliased_counter= i;
4704
*resolution= RESOLVED_IGNORING_ALIAS;
4706
break; // Perfect match
4711
int fname_cmp= my_strcasecmp(system_charset_info,
4712
item_field->field_name,
4714
if (!my_strcasecmp(system_charset_info,
4715
item_field->name,field_name))
4718
If table name was not given we should scan through aliases
4719
and non-aliased fields first. We are also checking unaliased
4720
name of the field in then next else-if, to be able to find
4721
instantly field (hidden by alias) if no suitable alias or
4722
non-aliased field was found.
4726
if ((*found)->eq(item, 0))
4727
continue; // Same field twice
4728
if (report_error != IGNORE_ERRORS)
4729
my_error(ER_NON_UNIQ_ERROR, MYF(0),
4730
find->full_name(), current_thd->where);
4735
*resolution= fname_cmp ? RESOLVED_AGAINST_ALIAS:
4736
RESOLVED_WITH_NO_ALIAS;
4738
else if (!fname_cmp)
4741
We will use non-aliased field or react on such ambiguities only if
4742
we won't be able to find aliased field.
4743
Again if we have ambiguity with field outside of select list
4744
we should prefer fields from select list.
4746
if (found_unaliased)
4748
if ((*found_unaliased)->eq(item, 0))
4749
continue; // Same field twice
4750
found_unaliased_non_uniq= 1;
4752
found_unaliased= li.ref();
4753
unaliased_counter= i;
4757
else if (!table_name)
4759
if (is_ref_by_name && find->name && item->name &&
4760
!my_strcasecmp(system_charset_info,item->name,find->name))
4764
*resolution= RESOLVED_AGAINST_ALIAS;
4767
else if (find->eq(item,0))
4771
*resolution= RESOLVED_IGNORING_ALIAS;
4775
else if (table_name && item->type() == Item::REF_ITEM &&
4776
((Item_ref *)item)->ref_type() == Item_ref::VIEW_REF)
4779
TODO:Here we process prefixed view references only. What we should
4780
really do is process all types of Item_refs. But this will currently
4781
lead to a clash with the way references to outer SELECTs (from the
4782
HAVING clause) are handled in e.g. :
4783
SELECT 1 FROM t1 AS t1_o GROUP BY a
4784
HAVING (SELECT t1_o.a FROM t1 AS t1_i GROUP BY t1_i.a LIMIT 1).
4785
Processing all Item_refs here will cause t1_o.a to resolve to itself.
4786
We still need to process the special case of Item_direct_view_ref
4787
because in the context of views they have the same meaning as
4788
Item_field for tables.
4790
Item_ident *item_ref= (Item_ident *) item;
4791
if (item_ref->name && item_ref->table_name &&
4792
!my_strcasecmp(system_charset_info, item_ref->name, field_name) &&
4793
!my_strcasecmp(table_alias_charset, item_ref->table_name,
4795
(!db_name || (item_ref->db_name &&
4796
!strcmp (item_ref->db_name, db_name))))
4800
*resolution= RESOLVED_IGNORING_ALIAS;
4807
if (found_unaliased_non_uniq)
4809
if (report_error != IGNORE_ERRORS)
4810
my_error(ER_NON_UNIQ_ERROR, MYF(0),
4811
find->full_name(), current_thd->where);
4814
if (found_unaliased)
4816
found= found_unaliased;
4817
*counter= unaliased_counter;
4818
*resolution= RESOLVED_BEHIND_ALIAS;
4823
if (report_error != REPORT_EXCEPT_NOT_FOUND)
4825
if (report_error == REPORT_ALL_ERRORS)
4826
my_error(ER_BAD_FIELD_ERROR, MYF(0),
4827
find->full_name(), current_thd->where);
4831
return (Item **) not_found_item;
4836
Test if a string is a member of a list of strings.
4839
test_if_string_in_list()
4840
find the string to look for
4841
str_list a list of strings to be searched
4844
Sequentially search a list of strings for a string, and test whether
4845
the list contains the same string.
4848
true if find is in str_list
4853
test_if_string_in_list(const char *find, List<String> *str_list)
4855
List_iterator<String> str_list_it(*str_list);
4857
size_t find_length= strlen(find);
4858
while ((curr_str= str_list_it++))
4860
if (find_length != curr_str->length())
4862
if (!my_strcasecmp(system_charset_info, find, curr_str->ptr()))
4870
Create a new name resolution context for an item so that it is
4871
being resolved in a specific table reference.
4874
set_new_item_local_context()
4875
thd pointer to current thread
4876
item item for which new context is created and set
4877
table_ref table ref where an item showld be resolved
4880
Create a new name resolution context for an item, so that the item
4881
is resolved only the supplied 'table_ref'.
4889
set_new_item_local_context(THD *thd, Item_ident *item, TableList *table_ref)
4891
Name_resolution_context *context;
4892
if (!(context= new (thd->mem_root) Name_resolution_context))
4895
context->first_name_resolution_table=
4896
context->last_name_resolution_table= table_ref;
4897
item->context= context;
4903
Find and mark the common columns of two table references.
4906
mark_common_columns()
4907
thd [in] current thread
4908
table_ref_1 [in] the first (left) join operand
4909
table_ref_2 [in] the second (right) join operand
4910
using_fields [in] if the join is JOIN...USING - the join columns,
4911
if NATURAL join, then NULL
4912
found_using_fields [out] number of fields from the USING clause that were
4913
found among the common fields
4916
The procedure finds the common columns of two relations (either
4917
tables or intermediate join results), and adds an equi-join condition
4918
to the ON clause of 'table_ref_2' for each pair of matching columns.
4919
If some of table_ref_XXX represents a base table or view, then we
4920
create new 'Natural_join_column' instances for each column
4921
reference and store them in the 'join_columns' of the table
4925
The procedure assumes that store_natural_using_join_columns() was
4926
called for the previous level of NATURAL/USING joins.
4929
true error when some common column is non-unique, or out of memory
4934
mark_common_columns(THD *thd, TableList *table_ref_1, TableList *table_ref_2,
4935
List<String> *using_fields, uint32_t *found_using_fields)
4937
Field_iterator_table_ref it_1, it_2;
4938
Natural_join_column *nj_col_1, *nj_col_2;
4940
bool first_outer_loop= true;
4942
Leaf table references to which new natural join columns are added
4943
if the leaves are != NULL.
4945
TableList *leaf_1= (table_ref_1->nested_join &&
4946
!table_ref_1->is_natural_join) ?
4948
TableList *leaf_2= (table_ref_2->nested_join &&
4949
!table_ref_2->is_natural_join) ?
4952
*found_using_fields= 0;
4954
for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
4957
const char *field_name_1;
4958
/* true if field_name_1 is a member of using_fields */
4959
bool is_using_column_1;
4960
if (!(nj_col_1= it_1.get_or_create_column_ref(leaf_1)))
4962
field_name_1= nj_col_1->name();
4963
is_using_column_1= using_fields &&
4964
test_if_string_in_list(field_name_1, using_fields);
4967
Find a field with the same name in table_ref_2.
4969
Note that for the second loop, it_2.set() will iterate over
4970
table_ref_2->join_columns and not generate any new elements or
4974
for (it_2.set(table_ref_2); !it_2.end_of_fields(); it_2.next())
4976
Natural_join_column *cur_nj_col_2;
4977
const char *cur_field_name_2;
4978
if (!(cur_nj_col_2= it_2.get_or_create_column_ref(leaf_2)))
4980
cur_field_name_2= cur_nj_col_2->name();
4983
Compare the two columns and check for duplicate common fields.
4984
A common field is duplicate either if it was already found in
4985
table_ref_2 (then found == true), or if a field in table_ref_2
4986
was already matched by some previous field in table_ref_1
4987
(then cur_nj_col_2->is_common == true).
4988
Note that it is too early to check the columns outside of the
4989
USING list for ambiguity because they are not actually "referenced"
4990
here. These columns must be checked only on unqualified reference
4991
by name (e.g. in SELECT list).
4993
if (!my_strcasecmp(system_charset_info, field_name_1, cur_field_name_2))
4995
if (cur_nj_col_2->is_common ||
4996
(found && (!using_fields || is_using_column_1)))
4998
my_error(ER_NON_UNIQ_ERROR, MYF(0), field_name_1, thd->where);
5001
nj_col_2= cur_nj_col_2;
5005
if (first_outer_loop && leaf_2)
5008
Make sure that the next inner loop "knows" that all columns
5009
are materialized already.
5011
leaf_2->is_join_columns_complete= true;
5012
first_outer_loop= false;
5015
continue; // No matching field
5018
field_1 and field_2 have the same names. Check if they are in the USING
5019
clause (if present), mark them as common fields, and add a new
5020
equi-join condition to the ON clause.
5022
if (nj_col_2 && (!using_fields ||is_using_column_1))
5024
Item *item_1= nj_col_1->create_item(thd);
5025
Item *item_2= nj_col_2->create_item(thd);
5026
Field *field_1= nj_col_1->field();
5027
Field *field_2= nj_col_2->field();
5028
Item_ident *item_ident_1, *item_ident_2;
5029
Item_func_eq *eq_cond;
5031
if (!item_1 || !item_2)
5032
goto err; // out of memory
5035
In the case of no_wrap_view_item == 0, the created items must be
5036
of sub-classes of Item_ident.
5038
assert(item_1->type() == Item::FIELD_ITEM ||
5039
item_1->type() == Item::REF_ITEM);
5040
assert(item_2->type() == Item::FIELD_ITEM ||
5041
item_2->type() == Item::REF_ITEM);
5044
We need to cast item_1,2 to Item_ident, because we need to hook name
5045
resolution contexts specific to each item.
5047
item_ident_1= (Item_ident*) item_1;
5048
item_ident_2= (Item_ident*) item_2;
5050
Create and hook special name resolution contexts to each item in the
5051
new join condition . We need this to both speed-up subsequent name
5052
resolution of these items, and to enable proper name resolution of
5053
the items during the execute phase of PS.
5055
if (set_new_item_local_context(thd, item_ident_1, nj_col_1->table_ref) ||
5056
set_new_item_local_context(thd, item_ident_2, nj_col_2->table_ref))
5059
if (!(eq_cond= new Item_func_eq(item_ident_1, item_ident_2)))
5060
goto err; /* Out of memory. */
5063
Add the new equi-join condition to the ON clause. Notice that
5064
fix_fields() is applied to all ON conditions in setup_conds()
5065
so we don't do it here.
5067
add_join_on((table_ref_1->outer_join & JOIN_TYPE_RIGHT ?
5068
table_ref_1 : table_ref_2),
5071
nj_col_1->is_common= nj_col_2->is_common= true;
5075
Table *table_1= nj_col_1->table_ref->table;
5076
/* Mark field_1 used for table cache. */
5077
bitmap_set_bit(table_1->read_set, field_1->field_index);
5078
table_1->covering_keys.intersect(field_1->part_of_key);
5079
table_1->merge_keys.merge(field_1->part_of_key);
5083
Table *table_2= nj_col_2->table_ref->table;
5084
/* Mark field_2 used for table cache. */
5085
bitmap_set_bit(table_2->read_set, field_2->field_index);
5086
table_2->covering_keys.intersect(field_2->part_of_key);
5087
table_2->merge_keys.merge(field_2->part_of_key);
5090
if (using_fields != NULL)
5091
++(*found_using_fields);
5095
leaf_1->is_join_columns_complete= true;
5099
Notice that at this point there may be some column names in the USING
5100
clause that are not among the common columns. This is an SQL error and
5101
we check for this error in store_natural_using_join_columns() when
5102
(found_using_fields < length(join_using_fields)).
5113
Materialize and store the row type of NATURAL/USING join.
5116
store_natural_using_join_columns()
5118
natural_using_join the table reference of the NATURAL/USING join
5119
table_ref_1 the first (left) operand (of a NATURAL/USING join).
5120
table_ref_2 the second (right) operand (of a NATURAL/USING join).
5121
using_fields if the join is JOIN...USING - the join columns,
5122
if NATURAL join, then NULL
5123
found_using_fields number of fields from the USING clause that were
5124
found among the common fields
5127
Iterate over the columns of both join operands and sort and store
5128
all columns into the 'join_columns' list of natural_using_join
5129
where the list is formed by three parts:
5130
part1: The coalesced columns of table_ref_1 and table_ref_2,
5131
sorted according to the column order of the first table.
5132
part2: The other columns of the first table, in the order in
5133
which they were defined in CREATE TABLE.
5134
part3: The other columns of the second table, in the order in
5135
which they were defined in CREATE TABLE.
5136
Time complexity - O(N1+N2), where Ni = length(table_ref_i).
5139
The procedure assumes that mark_common_columns() has been called
5140
for the join that is being processed.
5143
true error: Some common column is ambiguous
5148
store_natural_using_join_columns(THD *thd __attribute__((unused)),
5149
TableList *natural_using_join,
5150
TableList *table_ref_1,
5151
TableList *table_ref_2,
5152
List<String> *using_fields,
5153
uint32_t found_using_fields)
5155
Field_iterator_table_ref it_1, it_2;
5156
Natural_join_column *nj_col_1, *nj_col_2;
5158
List<Natural_join_column> *non_join_columns;
5160
assert(!natural_using_join->join_columns);
5162
if (!(non_join_columns= new List<Natural_join_column>) ||
5163
!(natural_using_join->join_columns= new List<Natural_join_column>))
5166
/* Append the columns of the first join operand. */
5167
for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
5169
nj_col_1= it_1.get_natural_column_ref();
5170
if (nj_col_1->is_common)
5172
natural_using_join->join_columns->push_back(nj_col_1);
5173
/* Reset the common columns for the next call to mark_common_columns. */
5174
nj_col_1->is_common= false;
5177
non_join_columns->push_back(nj_col_1);
5181
Check that all columns in the USING clause are among the common
5182
columns. If this is not the case, report the first one that was
5183
not found in an error.
5185
if (using_fields && found_using_fields < using_fields->elements)
5187
String *using_field_name;
5188
List_iterator_fast<String> using_fields_it(*using_fields);
5189
while ((using_field_name= using_fields_it++))
5191
const char *using_field_name_ptr= using_field_name->c_ptr();
5192
List_iterator_fast<Natural_join_column>
5193
it(*(natural_using_join->join_columns));
5194
Natural_join_column *common_field;
5198
/* If reached the end of fields, and none was found, report error. */
5199
if (!(common_field= it++))
5201
my_error(ER_BAD_FIELD_ERROR, MYF(0), using_field_name_ptr,
5202
current_thd->where);
5205
if (!my_strcasecmp(system_charset_info,
5206
common_field->name(), using_field_name_ptr))
5207
break; // Found match
5212
/* Append the non-equi-join columns of the second join operand. */
5213
for (it_2.set(table_ref_2); !it_2.end_of_fields(); it_2.next())
5215
nj_col_2= it_2.get_natural_column_ref();
5216
if (!nj_col_2->is_common)
5217
non_join_columns->push_back(nj_col_2);
5220
/* Reset the common columns for the next call to mark_common_columns. */
5221
nj_col_2->is_common= false;
5225
if (non_join_columns->elements > 0)
5226
natural_using_join->join_columns->concat(non_join_columns);
5227
natural_using_join->is_join_columns_complete= true;
5237
Precompute and store the row types of the top-most NATURAL/USING joins.
5240
store_top_level_join_columns()
5242
table_ref nested join or table in a FROM clause
5243
left_neighbor neighbor table reference to the left of table_ref at the
5244
same level in the join tree
5245
right_neighbor neighbor table reference to the right of table_ref at the
5246
same level in the join tree
5249
The procedure performs a post-order traversal of a nested join tree
5250
and materializes the row types of NATURAL/USING joins in a
5251
bottom-up manner until it reaches the TableList elements that
5252
represent the top-most NATURAL/USING joins. The procedure should be
5253
applied to each element of SELECT_LEX::top_join_list (i.e. to each
5254
top-level element of the FROM clause).
5257
Notice that the table references in the list nested_join->join_list
5258
are in reverse order, thus when we iterate over it, we are moving
5259
from the right to the left in the FROM clause.
5267
store_top_level_join_columns(THD *thd, TableList *table_ref,
5268
TableList *left_neighbor,
5269
TableList *right_neighbor)
5273
/* Call the procedure recursively for each nested table reference. */
5274
if (table_ref->nested_join)
5276
List_iterator_fast<TableList> nested_it(table_ref->nested_join->join_list);
5277
TableList *same_level_left_neighbor= nested_it++;
5278
TableList *same_level_right_neighbor= NULL;
5279
/* Left/right-most neighbors, possibly at higher levels in the join tree. */
5280
TableList *real_left_neighbor, *real_right_neighbor;
5282
while (same_level_left_neighbor)
5284
TableList *cur_table_ref= same_level_left_neighbor;
5285
same_level_left_neighbor= nested_it++;
5287
The order of RIGHT JOIN operands is reversed in 'join list' to
5288
transform it into a LEFT JOIN. However, in this procedure we need
5289
the join operands in their lexical order, so below we reverse the
5290
join operands. Notice that this happens only in the first loop,
5291
and not in the second one, as in the second loop
5292
same_level_left_neighbor == NULL.
5293
This is the correct behavior, because the second loop sets
5294
cur_table_ref reference correctly after the join operands are
5295
swapped in the first loop.
5297
if (same_level_left_neighbor &&
5298
cur_table_ref->outer_join & JOIN_TYPE_RIGHT)
5300
/* This can happen only for JOIN ... ON. */
5301
assert(table_ref->nested_join->join_list.elements == 2);
5302
std::swap(same_level_left_neighbor, cur_table_ref);
5306
Pick the parent's left and right neighbors if there are no immediate
5307
neighbors at the same level.
5309
real_left_neighbor= (same_level_left_neighbor) ?
5310
same_level_left_neighbor : left_neighbor;
5311
real_right_neighbor= (same_level_right_neighbor) ?
5312
same_level_right_neighbor : right_neighbor;
5314
if (cur_table_ref->nested_join &&
5315
store_top_level_join_columns(thd, cur_table_ref,
5316
real_left_neighbor, real_right_neighbor))
5318
same_level_right_neighbor= cur_table_ref;
5323
If this is a NATURAL/USING join, materialize its result columns and
5324
convert to a JOIN ... ON.
5326
if (table_ref->is_natural_join)
5328
assert(table_ref->nested_join &&
5329
table_ref->nested_join->join_list.elements == 2);
5330
List_iterator_fast<TableList> operand_it(table_ref->nested_join->join_list);
5332
Notice that the order of join operands depends on whether table_ref
5333
represents a LEFT or a RIGHT join. In a RIGHT join, the operands are
5336
TableList *table_ref_2= operand_it++; /* Second NATURAL join operand.*/
5337
TableList *table_ref_1= operand_it++; /* First NATURAL join operand. */
5338
List<String> *using_fields= table_ref->join_using_fields;
5339
uint32_t found_using_fields;
5342
The two join operands were interchanged in the parser, change the order
5343
back for 'mark_common_columns'.
5345
if (table_ref_2->outer_join & JOIN_TYPE_RIGHT)
5346
std::swap(table_ref_1, table_ref_2);
5347
if (mark_common_columns(thd, table_ref_1, table_ref_2,
5348
using_fields, &found_using_fields))
5352
Swap the join operands back, so that we pick the columns of the second
5353
one as the coalesced columns. In this way the coalesced columns are the
5354
same as of an equivalent LEFT JOIN.
5356
if (table_ref_1->outer_join & JOIN_TYPE_RIGHT)
5357
std::swap(table_ref_1, table_ref_2);
5358
if (store_natural_using_join_columns(thd, table_ref, table_ref_1,
5359
table_ref_2, using_fields,
5360
found_using_fields))
5364
Change NATURAL JOIN to JOIN ... ON. We do this for both operands
5365
because either one of them or the other is the one with the
5366
natural join flag because RIGHT joins are transformed into LEFT,
5367
and the two tables may be reordered.
5369
table_ref_1->natural_join= table_ref_2->natural_join= NULL;
5371
/* Add a true condition to outer joins that have no common columns. */
5372
if (table_ref_2->outer_join &&
5373
!table_ref_1->on_expr && !table_ref_2->on_expr)
5374
table_ref_2->on_expr= new Item_int((int64_t) 1,1); /* Always true. */
5376
/* Change this table reference to become a leaf for name resolution. */
5379
TableList *last_leaf_on_the_left;
5380
last_leaf_on_the_left= left_neighbor->last_leaf_for_name_resolution();
5381
last_leaf_on_the_left->next_name_resolution_table= table_ref;
5385
TableList *first_leaf_on_the_right;
5386
first_leaf_on_the_right= right_neighbor->first_leaf_for_name_resolution();
5387
table_ref->next_name_resolution_table= first_leaf_on_the_right;
5390
table_ref->next_name_resolution_table= NULL;
5392
result= false; /* All is OK. */
5400
Compute and store the row types of the top-most NATURAL/USING joins
5404
setup_natural_join_row_types()
5406
from_clause list of top-level table references in a FROM clause
5409
Apply the procedure 'store_top_level_join_columns' to each of the
5410
top-level table referencs of the FROM clause. Adjust the list of tables
5411
for name resolution - context->first_name_resolution_table to the
5412
top-most, lef-most NATURAL/USING join.
5415
Notice that the table references in 'from_clause' are in reverse
5416
order, thus when we iterate over it, we are moving from the right
5417
to the left in the FROM clause.
5423
static bool setup_natural_join_row_types(THD *thd,
5424
List<TableList> *from_clause,
5425
Name_resolution_context *context)
5427
thd->where= "from clause";
5428
if (from_clause->elements == 0)
5429
return false; /* We come here in the case of UNIONs. */
5431
List_iterator_fast<TableList> table_ref_it(*from_clause);
5432
TableList *table_ref; /* Current table reference. */
5433
/* Table reference to the left of the current. */
5434
TableList *left_neighbor;
5435
/* Table reference to the right of the current. */
5436
TableList *right_neighbor= NULL;
5438
/* Note that tables in the list are in reversed order */
5439
for (left_neighbor= table_ref_it++; left_neighbor ; )
5441
table_ref= left_neighbor;
5442
left_neighbor= table_ref_it++;
5443
if (store_top_level_join_columns(thd, table_ref,
5444
left_neighbor, right_neighbor))
5448
TableList *first_leaf_on_the_right;
5449
first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution();
5450
left_neighbor->next_name_resolution_table= first_leaf_on_the_right;
5452
right_neighbor= table_ref;
5456
Store the top-most, left-most NATURAL/USING join, so that we start
5457
the search from that one instead of context->table_list. At this point
5458
right_neighbor points to the left-most top-level table reference in the
5461
assert(right_neighbor);
5462
context->first_name_resolution_table=
5463
right_neighbor->first_leaf_for_name_resolution();
5469
/****************************************************************************
5470
** Expand all '*' in given fields
5471
****************************************************************************/
5473
int setup_wild(THD *thd,
5474
TableList *tables __attribute__((unused)),
5476
List<Item> *sum_func_list,
5483
List_iterator<Item> it(fields);
5485
thd->lex->current_select->cur_pos_in_select_list= 0;
5486
while (wild_num && (item= it++))
5488
if (item->type() == Item::FIELD_ITEM &&
5489
((Item_field*) item)->field_name &&
5490
((Item_field*) item)->field_name[0] == '*' &&
5491
!((Item_field*) item)->field)
5493
uint32_t elem= fields.elements;
5494
bool any_privileges= ((Item_field *) item)->any_privileges;
5495
Item_subselect *subsel= thd->lex->current_select->master_unit()->item;
5497
subsel->substype() == Item_subselect::EXISTS_SUBS)
5500
It is EXISTS(SELECT * ...) and we can replace * by any constant.
5502
Item_int do not need fix_fields() because it is basic constant.
5504
it.replace(new Item_int("Not_used", (int64_t) 1,
5505
MY_INT64_NUM_DECIMAL_DIGITS));
5507
else if (insert_fields(thd, ((Item_field*) item)->context,
5508
((Item_field*) item)->db_name,
5509
((Item_field*) item)->table_name, &it,
5517
sum_func_list is a list that has the fields list as a tail.
5518
Because of this we have to update the element count also for this
5519
list after expanding the '*' entry.
5521
sum_func_list->elements+= fields.elements - elem;
5526
thd->lex->current_select->cur_pos_in_select_list++;
5528
thd->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
5532
/****************************************************************************
5533
** Check that all given fields exists and fill struct with current data
5534
****************************************************************************/
5536
bool setup_fields(THD *thd, Item **ref_pointer_array,
5537
List<Item> &fields, enum_mark_columns mark_used_columns,
5538
List<Item> *sum_func_list, bool allow_sum_func)
5540
register Item *item;
5541
enum_mark_columns save_mark_used_columns= thd->mark_used_columns;
5542
nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
5543
List_iterator<Item> it(fields);
5544
bool save_is_item_list_lookup;
5546
thd->mark_used_columns= mark_used_columns;
5548
thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
5549
thd->where= THD::DEFAULT_WHERE;
5550
save_is_item_list_lookup= thd->lex->current_select->is_item_list_lookup;
5551
thd->lex->current_select->is_item_list_lookup= 0;
5554
To prevent fail on forward lookup we fill it with zerows,
5555
then if we got pointer on zero after find_item_in_list we will know
5556
that it is forward lookup.
5558
There is other way to solve problem: fill array with pointers to list,
5559
but it will be slower.
5561
TODO: remove it when (if) we made one list for allfields and
5564
if (ref_pointer_array)
5565
memset(ref_pointer_array, 0, sizeof(Item *) * fields.elements);
5567
Item **ref= ref_pointer_array;
5568
thd->lex->current_select->cur_pos_in_select_list= 0;
5569
while ((item= it++))
5571
if ((!item->fixed && item->fix_fields(thd, it.ref())) || (item= *(it.ref()))->check_cols(1))
5573
thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
5574
thd->lex->allow_sum_func= save_allow_sum_func;
5575
thd->mark_used_columns= save_mark_used_columns;
5576
return(true); /* purecov: inspected */
5580
if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM &&
5582
item->split_sum_func(thd, ref_pointer_array, *sum_func_list);
5583
thd->used_tables|= item->used_tables();
5584
thd->lex->current_select->cur_pos_in_select_list++;
5586
thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
5587
thd->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
5589
thd->lex->allow_sum_func= save_allow_sum_func;
5590
thd->mark_used_columns= save_mark_used_columns;
5591
return(test(thd->is_error()));
5596
make list of leaves of join table tree
5600
list pointer to pointer on list first element
5603
RETURN pointer on pointer to next_leaf of last element
5606
TableList **make_leaves_list(TableList **list, TableList *tables)
5608
for (TableList *table= tables; table; table= table->next_local)
5612
list= &table->next_leaf;
5624
context name resolution contest to setup table list there
5625
from_clause Top-level list of table references in the FROM clause
5626
tables Table list (select_lex->table_list)
5627
leaves List of join table leaves list (select_lex->leaf_tables)
5628
refresh It is onle refresh for subquery
5629
select_insert It is SELECT ... INSERT command
5632
Check also that the 'used keys' and 'ignored keys' exists and set up the
5633
table structure accordingly.
5634
Create a list of leaf tables. For queries with NATURAL/USING JOINs,
5635
compute the row types of the top most natural/using join table references
5636
and link these into a list of table references for name resolution.
5638
This has to be called for all tables that are used by items, as otherwise
5639
table->map is not set and all Item_field will be regarded as const items.
5642
false ok; In this case *map will includes the chosen index
5646
bool setup_tables(THD *thd, Name_resolution_context *context,
5647
List<TableList> *from_clause, TableList *tables,
5648
TableList **leaves, bool select_insert)
5650
uint32_t tablenr= 0;
5652
assert ((select_insert && !tables->next_name_resolution_table) || !tables ||
5653
(context->table_list && context->first_name_resolution_table));
5655
this is used for INSERT ... SELECT.
5656
For select we setup tables except first (and its underlying tables)
5658
TableList *first_select_table= (select_insert ?
5662
make_leaves_list(leaves, tables);
5664
TableList *table_list;
5665
for (table_list= *leaves;
5667
table_list= table_list->next_leaf, tablenr++)
5669
Table *table= table_list->table;
5670
table->pos_in_table_list= table_list;
5671
if (first_select_table &&
5672
table_list->top_table() == first_select_table)
5674
/* new counting for SELECT of INSERT ... SELECT command */
5675
first_select_table= 0;
5678
setup_table_map(table, table_list, tablenr);
5679
if (table_list->process_index_hints(table))
5682
if (tablenr > MAX_TABLES)
5684
my_error(ER_TOO_MANY_TABLES,MYF(0),MAX_TABLES);
5688
/* Precompute and store the row types of NATURAL/USING joins. */
5689
if (setup_natural_join_row_types(thd, from_clause, context))
5697
prepare tables and check access for the view tables
5700
setup_tables_and_check_view_access()
5702
context name resolution contest to setup table list there
5703
from_clause Top-level list of table references in the FROM clause
5704
tables Table list (select_lex->table_list)
5705
conds Condition of current SELECT (can be changed by VIEW)
5706
leaves List of join table leaves list (select_lex->leaf_tables)
5707
refresh It is onle refresh for subquery
5708
select_insert It is SELECT ... INSERT command
5709
want_access what access is needed
5712
a wrapper for check_tables that will also check the resulting
5713
table leaves list for access to all the tables that belong to a view
5716
false ok; In this case *map will include the chosen index
5719
bool setup_tables_and_check_access(THD *thd,
5720
Name_resolution_context *context,
5721
List<TableList> *from_clause,
5726
TableList *leaves_tmp= NULL;
5727
bool first_table= true;
5729
if (setup_tables(thd, context, from_clause, tables,
5730
&leaves_tmp, select_insert))
5734
*leaves= leaves_tmp;
5736
for (; leaves_tmp; leaves_tmp= leaves_tmp->next_leaf)
5745
Create a key_map from a list of index names
5748
get_key_map_from_key_list()
5749
map key_map to fill in
5751
index_list List of index names
5754
0 ok; In this case *map will includes the choosed index
5758
bool get_key_map_from_key_list(key_map *map, Table *table,
5759
List<String> *index_list)
5761
List_iterator_fast<String> it(*index_list);
5768
if (table->s->keynames.type_names == 0 ||
5769
(pos= find_type(&table->s->keynames, name->ptr(),
5770
name->length(), 1)) <=
5773
my_error(ER_KEY_DOES_NOT_EXITS, MYF(0), name->c_ptr(),
5774
table->pos_in_table_list->alias);
5778
map->set_bit(pos-1);
5785
Drops in all fields instead of current '*' field
5790
context Context for name resolution
5791
db_name Database name in case of 'database_name.table_name.*'
5792
table_name Table name in case of 'table_name.*'
5794
any_privileges 0 If we should ensure that we have SELECT privileges
5796
1 If any privilege is ok
5798
0 ok 'it' is updated to point at last inserted
5799
1 error. Error message is generated but not sent to client
5803
insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
5804
const char *table_name, List_iterator<Item> *it,
5805
bool any_privileges __attribute__((unused)))
5807
Field_iterator_table_ref field_iterator;
5809
char name_buff[NAME_LEN+1];
5811
if (db_name && lower_case_table_names)
5814
convert database to lower case for comparison
5815
We can't do this in Item_field as this would change the
5816
'name' of the item which may be used in the select list
5818
strmake(name_buff, db_name, sizeof(name_buff)-1);
5819
my_casedn_str(files_charset_info, name_buff);
5826
If table names are qualified, then loop over all tables used in the query,
5827
else treat natural joins as leaves and do not iterate over their underlying
5830
for (TableList *tables= (table_name ? context->table_list :
5831
context->first_name_resolution_table);
5833
tables= (table_name ? tables->next_local :
5834
tables->next_name_resolution_table)
5838
Table *table= tables->table;
5840
assert(tables->is_leaf_for_name_resolution());
5842
if ((table_name && my_strcasecmp(table_alias_charset, table_name, tables->alias)) ||
5843
(db_name && strcmp(tables->db,db_name)))
5847
Update the tables used in the query based on the referenced fields. For
5848
views and natural joins this update is performed inside the loop below.
5851
thd->used_tables|= table->map;
5854
Initialize a generic field iterator for the current table reference.
5855
Notice that it is guaranteed that this iterator will iterate over the
5856
fields of a single table reference, because 'tables' is a leaf (for
5857
name resolution purposes).
5859
field_iterator.set(tables);
5861
for (; !field_iterator.end_of_fields(); field_iterator.next())
5865
if (!(item= field_iterator.create_item(thd)))
5871
it->replace(item); /* Replace '*' with the first found item. */
5874
it->after(item); /* Add 'item' to the SELECT list. */
5876
if ((field= field_iterator.field()))
5878
/* Mark fields as used to allow storage engine to optimze access */
5879
bitmap_set_bit(field->table->read_set, field->field_index);
5882
table->covering_keys.intersect(field->part_of_key);
5883
table->merge_keys.merge(field->part_of_key);
5885
if (tables->is_natural_join)
5889
In this case we are sure that the column ref will not be created
5890
because it was already created and stored with the natural join.
5892
Natural_join_column *nj_col;
5893
if (!(nj_col= field_iterator.get_natural_column_ref()))
5895
assert(nj_col->table_field);
5896
field_table= nj_col->table_ref->table;
5899
thd->used_tables|= field_table->map;
5900
field_table->covering_keys.intersect(field->part_of_key);
5901
field_table->merge_keys.merge(field->part_of_key);
5902
field_table->used_fields++;
5907
thd->used_tables|= item->used_tables();
5908
thd->lex->current_select->cur_pos_in_select_list++;
5911
In case of stored tables, all fields are considered as used,
5912
while in the case of views, the fields considered as used are the
5913
ones marked in setup_tables during fix_fields of view columns.
5914
For NATURAL joins, used_tables is updated in the IF above.
5917
table->used_fields= table->s->fields;
5923
TODO: in the case when we skipped all columns because there was a
5924
qualified '*', and all columns were coalesced, we have to give a more
5925
meaningful message than ER_BAD_TABLE_ERROR.
5928
my_message(ER_NO_TABLES_USED, ER(ER_NO_TABLES_USED), MYF(0));
5930
my_error(ER_BAD_TABLE_ERROR, MYF(0), table_name);
5937
Fix all conditions and outer join expressions.
5942
tables list of tables for name resolving (select_lex->table_list)
5943
leaves list of leaves of join table tree (select_lex->leaf_tables)
5950
true if some error occured (e.g. out of memory)
5954
int setup_conds(THD *thd, TableList *tables __attribute__((unused)),
5958
SELECT_LEX *select_lex= thd->lex->current_select;
5959
TableList *table= NULL; // For HP compilers
5960
void *save_thd_marker= thd->thd_marker;
5962
it_is_update set to true when tables of primary SELECT_LEX (SELECT_LEX
5963
which belong to LEX, i.e. most up SELECT) will be updated by
5965
NOTE: using this condition helps to prevent call of prepare_check_option()
5966
from subquery of VIEW, because tables of subquery belongs to VIEW
5967
(see condition before prepare_check_option() call)
5969
bool save_is_item_list_lookup= select_lex->is_item_list_lookup;
5970
select_lex->is_item_list_lookup= 0;
5972
thd->mark_used_columns= MARK_COLUMNS_READ;
5973
select_lex->cond_count= 0;
5974
select_lex->between_count= 0;
5975
select_lex->max_equal_elems= 0;
5977
thd->thd_marker= (void*)1;
5980
thd->where="where clause";
5981
if ((!(*conds)->fixed && (*conds)->fix_fields(thd, conds)) ||
5982
(*conds)->check_cols(1))
5985
thd->thd_marker= save_thd_marker;
5988
Apply fix_fields() to all ON clauses at all levels of nesting,
5989
including the ones inside view definitions.
5991
for (table= leaves; table; table= table->next_leaf)
5993
TableList *embedded; /* The table at the current level of nesting. */
5994
TableList *embedding= table; /* The parent nested table reference. */
5997
embedded= embedding;
5998
if (embedded->on_expr)
6000
/* Make a join an a expression */
6001
thd->thd_marker= (void*)embedded;
6002
thd->where="on clause";
6003
if ((!embedded->on_expr->fixed && embedded->on_expr->fix_fields(thd, &embedded->on_expr)) ||
6004
embedded->on_expr->check_cols(1))
6006
select_lex->cond_count++;
6008
embedding= embedded->embedding;
6011
embedding->nested_join->join_list.head() == embedded);
6014
thd->thd_marker= save_thd_marker;
6016
thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
6017
return(test(thd->is_error()));
6020
select_lex->is_item_list_lookup= save_is_item_list_lookup;
6025
/******************************************************************************
6026
** Fill a record with data (for INSERT or UPDATE)
6027
** Returns : 1 if some field has wrong type
6028
******************************************************************************/
6032
Fill fields with given items.
6037
fields Item_fields list to be filled
6038
values values to fill with
6039
ignore_errors true if we should ignore errors
6042
fill_record() may set table->auto_increment_field_not_null and a
6043
caller should make sure that it is reset after their last call to this
6052
fill_record(THD * thd, List<Item> &fields, List<Item> &values, bool ignore_errors)
6054
List_iterator_fast<Item> f(fields),v(values);
6060
Reset the table->auto_increment_field_not_null as it is valid for
6063
if (fields.elements)
6066
On INSERT or UPDATE fields are checked to be from the same table,
6067
thus we safely can take table from the first field.
6069
fld= (Item_field*)f++;
6070
if (!(field= fld->filed_for_view_update()))
6072
my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), fld->name);
6075
table= field->field->table;
6076
table->auto_increment_field_not_null= false;
6081
if (!(field= fld->filed_for_view_update()))
6083
my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), fld->name);
6087
Field *rfield= field->field;
6088
table= rfield->table;
6089
if (rfield == table->next_number_field)
6090
table->auto_increment_field_not_null= true;
6091
if ((value->save_in_field(rfield, 0) < 0) && !ignore_errors)
6093
my_message(ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR), MYF(0));
6097
return(thd->is_error());
6100
table->auto_increment_field_not_null= false;
6106
Fill field buffer with values from Field list
6111
ptr pointer on pointer to record
6112
values list of fields
6113
ignore_errors true if we should ignore errors
6116
fill_record() may set table->auto_increment_field_not_null and a
6117
caller should make sure that it is reset after their last call to this
6126
fill_record(THD *thd, Field **ptr, List<Item> &values,
6127
bool ignore_errors __attribute__((unused)))
6129
List_iterator_fast<Item> v(values);
6135
Reset the table->auto_increment_field_not_null as it is valid for
6141
On INSERT or UPDATE fields are checked to be from the same table,
6142
thus we safely can take table from the first field.
6144
table= (*ptr)->table;
6145
table->auto_increment_field_not_null= false;
6147
while ((field = *ptr++) && ! thd->is_error())
6150
table= field->table;
6151
if (field == table->next_number_field)
6152
table->auto_increment_field_not_null= true;
6153
if (value->save_in_field(field, 0) < 0)
6156
return(thd->is_error());
6160
table->auto_increment_field_not_null= false;
6165
bool mysql_rm_tmp_tables(void)
6168
char filePath[FN_REFLEN], *tmpdir, filePathCopy[FN_REFLEN];
6174
if (!(thd= new THD))
6176
thd->thread_stack= (char*) &thd;
6177
thd->store_globals();
6179
for (i=0; i<=mysql_tmpdir_list.max; i++)
6181
tmpdir=mysql_tmpdir_list.list[i];
6182
/* See if the directory exists */
6183
if (!(dirp = my_dir(tmpdir,MYF(MY_WME | MY_DONT_SORT))))
6186
/* Remove all SQLxxx tables from directory */
6188
for (idx=0 ; idx < (uint) dirp->number_off_files ; idx++)
6190
file=dirp->dir_entry+idx;
6192
/* skiping . and .. */
6193
if (file->name[0] == '.' && (!file->name[1] ||
6194
(file->name[1] == '.' && !file->name[2])))
6197
if (!memcmp(file->name, tmp_file_prefix, tmp_file_prefix_length))
6199
char *ext= fn_ext(file->name);
6200
uint32_t ext_len= strlen(ext);
6201
uint32_t filePath_len= snprintf(filePath, sizeof(filePath),
6202
"%s%c%s", tmpdir, FN_LIBCHAR,
6204
if (!memcmp(reg_ext, ext, ext_len))
6206
handler *handler_file= 0;
6207
/* We should cut file extention before deleting of table */
6208
memcpy(filePathCopy, filePath, filePath_len - ext_len);
6209
filePathCopy[filePath_len - ext_len]= 0;
6210
init_tmp_table_share(thd, &share, "", 0, "", filePathCopy);
6211
if (!open_table_def(thd, &share, 0) &&
6212
((handler_file= get_new_handler(&share, thd->mem_root,
6215
handler_file->ha_delete_table(filePathCopy);
6216
delete handler_file;
6218
free_table_share(&share);
6221
File can be already deleted by tmp_table.file->delete_table().
6222
So we hide error messages which happnes during deleting of these
6225
my_delete(filePath, MYF(0));
6231
my_pthread_setspecific_ptr(THR_THD, 0);
6237
/*****************************************************************************
6238
unireg support functions
6239
*****************************************************************************/
6242
Invalidate any cache entries that are for some DB
6245
remove_db_from_cache()
6246
db Database name. This will be in lower case if
6247
lower_case_table_name is set
6250
We can't use hash_delete when looping hash_elements. We mark them first
6251
and afterwards delete those marked unused.
6254
void remove_db_from_cache(const char *db)
6256
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
6258
Table *table=(Table*) hash_element(&open_cache,idx);
6259
if (!strcmp(table->s->db.str, db))
6261
table->s->version= 0L; /* Free when thread is ready */
6263
relink_unused(table);
6266
while (unused_tables && !unused_tables->s->version)
6267
hash_delete(&open_cache,(unsigned char*) unused_tables);
6272
free all unused tables
6275
This is called by 'handle_manager' when one wants to periodicly flush
6276
all not used tables.
6281
(void) pthread_mutex_lock(&LOCK_open);
6282
while (unused_tables)
6283
hash_delete(&open_cache,(unsigned char*) unused_tables);
6284
(void) pthread_mutex_unlock(&LOCK_open);
6289
Mark all entries with the table as deleted to force an reopen of the table
6291
The table will be closed (not stored in cache) by the current thread when
6292
close_thread_tables() is called.
6298
0 This thread now have exclusive access to this table and no other thread
6299
can access the table until close_thread_tables() is called.
6300
1 Table is in use by another thread
6303
bool remove_table_from_cache(THD *thd, const char *db, const char *table_name,
6306
char key[MAX_DBKEY_LENGTH];
6307
uint32_t key_length;
6310
bool result= 0, signalled= 0;
6312
key_length=(uint) (my_stpcpy(my_stpcpy(key,db)+1,table_name)-key)+1;
6315
HASH_SEARCH_STATE state;
6316
result= signalled= 0;
6318
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
6321
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
6326
table->s->version=0L; /* Free when thread is ready */
6327
if (!(in_use=table->in_use))
6329
relink_unused(table);
6331
else if (in_use != thd)
6334
Mark that table is going to be deleted from cache. This will
6335
force threads that are in mysql_lock_tables() (but not yet
6336
in thr_multi_lock()) to abort it's locks, close all tables and retry
6338
in_use->some_tables_deleted= 1;
6339
if (table->is_name_opened())
6344
Now we must abort all tables locks used by this thread
6345
as the thread may be waiting to get a lock for another table.
6346
Note that we need to hold LOCK_open while going through the
6347
list. So that the other thread cannot change it. The other
6348
thread must also hold LOCK_open whenever changing the
6349
open_tables list. Aborting the MERGE lock after a child was
6350
closed and before the parent is closed would be fatal.
6352
for (Table *thd_table= in_use->open_tables;
6354
thd_table= thd_table->next)
6356
/* Do not handle locks of MERGE children. */
6357
if (thd_table->db_stat) // If table is open
6358
signalled|= mysql_lock_abort_for_thread(thd, thd_table);
6362
result= result || (flags & RTFC_OWNED_BY_THD_FLAG);
6364
while (unused_tables && !unused_tables->s->version)
6365
hash_delete(&open_cache,(unsigned char*) unused_tables);
6367
/* Remove table from table definition cache if it's not in use */
6368
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key,
6371
share->version= 0; // Mark for delete
6372
if (share->ref_count == 0)
6374
pthread_mutex_lock(&share->mutex);
6375
hash_delete(&table_def_cache, (unsigned char*) share);
6379
if (result && (flags & RTFC_WAIT_OTHER_THREAD_FLAG))
6382
Signal any thread waiting for tables to be freed to
6385
broadcast_refresh();
6386
if (!(flags & RTFC_CHECK_KILLED_FLAG) || !thd->killed)
6389
if (likely(signalled))
6390
(void) pthread_cond_wait(&COND_refresh, &LOCK_open);
6393
struct timespec abstime;
6395
It can happen that another thread has opened the
6396
table but has not yet locked any table at all. Since
6397
it can be locked waiting for a table that our thread
6398
has done LOCK Table x WRITE on previously, we need to
6399
ensure that the thread actually hears our signal
6400
before we go to sleep. Thus we wait for a short time
6401
and then we retry another loop in the
6402
remove_table_from_cache routine.
6404
set_timespec(abstime, 10);
6405
pthread_cond_timedwait(&COND_refresh, &LOCK_open, &abstime);
6417
bool is_equal(const LEX_STRING *a, const LEX_STRING *b)
6419
return a->length == b->length && !strncmp(a->str, b->str, a->length);
6422
@} (end of group Data_Dictionary)