17
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
#if TIME_WITH_SYS_TIME
25
# include <sys/time.h>
29
# include <sys/time.h>
19
#include "mysql_priv.h"
20
#include "sql_select.h"
35
25
#define FLAGSTR(S,F) ((S) & (F) ? #F " " : "")
38
return true if the table was created explicitly.
40
inline bool is_user_table(Table * table)
42
const char *name= table->s->table_name.str;
43
return strncmp(name, tmp_file_prefix, tmp_file_prefix_length);
48
28
@defgroup Data_Dictionary Data Dictionary
51
Table *unused_tables; /* Used by mysql_test */
31
TABLE *unused_tables; /* Used by mysql_test */
52
32
HASH open_cache; /* Used by mysql_test */
53
33
static HASH table_def_cache;
54
34
static TABLE_SHARE *oldest_unused_share, end_of_unused_share;
55
35
static pthread_mutex_t LOCK_table_share;
56
36
static bool table_def_inited= 0;
58
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
60
char *cache_key, uint32_t cache_key_length);
61
static void free_cache_entry(Table *entry);
62
static void close_old_data_files(THD *thd, Table *table, bool morph_locks,
38
static int open_unireg_entry(THD *thd, TABLE *entry, TABLE_LIST *table_list,
40
char *cache_key, uint cache_key_length,
41
MEM_ROOT *mem_root, uint flags);
42
static void free_cache_entry(TABLE *entry);
43
static void close_old_data_files(THD *thd, TABLE *table, bool morph_locks,
63
44
bool send_refresh);
66
extern "C" unsigned char *table_cache_key(const unsigned char *record, size_t *length,
67
bool not_used __attribute__((unused)))
47
extern "C" uchar *table_cache_key(const uchar *record, size_t *length,
48
my_bool not_used __attribute__((unused)))
69
Table *entry=(Table*) record;
50
TABLE *entry=(TABLE*) record;
70
51
*length= entry->s->table_cache_key.length;
71
return (unsigned char*) entry->s->table_cache_key.str;
52
return (uchar*) entry->s->table_cache_key.str;
77
58
return hash_init(&open_cache, &my_charset_bin, table_cache_size+16,
78
59
0, 0, table_cache_key,
79
(hash_free_key) free_cache_entry, 0);
60
(hash_free_key) free_cache_entry, 0) != 0;
82
63
void table_cache_free(void)
65
DBUG_ENTER("table_cache_free");
84
66
if (table_def_inited)
86
68
close_cached_tables(NULL, NULL, false, false, false);
87
69
if (!open_cache.records) // Safety first
88
70
hash_free(&open_cache);
93
uint32_t cached_open_tables(void)
75
uint cached_open_tables(void)
95
77
return open_cache.records;
82
static void check_unused(void)
84
uint count= 0, open_files= 0, idx= 0;
85
TABLE *cur_link,*start_link;
87
if ((start_link=cur_link=unused_tables))
91
if (cur_link != cur_link->next->prev || cur_link != cur_link->prev->next)
93
DBUG_PRINT("error",("Unused_links aren't linked properly")); /* purecov: inspected */
94
return; /* purecov: inspected */
96
} while (count++ < open_cache.records &&
97
(cur_link=cur_link->next) != start_link);
98
if (cur_link != start_link)
100
DBUG_PRINT("error",("Unused_links aren't connected")); /* purecov: inspected */
103
for (idx=0 ; idx < open_cache.records ; idx++)
105
TABLE *entry=(TABLE*) hash_element(&open_cache,idx);
113
DBUG_PRINT("error",("Unused_links doesn't match open_cache: diff: %d", /* purecov: inspected */
114
count)); /* purecov: inspected */
117
#ifdef NOT_SAFE_FOR_REPAIR
119
check that open cache and table definition cache has same number of
123
for (idx=0 ; idx < table_def_cache.records ; idx++)
125
TABLE_SHARE *entry= (TABLE_SHARE*) hash_element(&table_def_cache,idx);
126
count+= entry->ref_count;
128
if (count != open_files)
130
DBUG_PRINT("error", ("table_def ref_count: %u open_cache: %u",
132
DBUG_ASSERT(count == open_files);
137
#define check_unused()
99
142
Create a table cache key
123
uint32_t create_table_def_key(THD *thd, char *key, TableList *table_list,
166
uint create_table_def_key(THD *thd, char *key, TABLE_LIST *table_list,
126
uint32_t key_length= (uint) (my_stpcpy(my_stpcpy(key, table_list->db)+1,
169
uint key_length= (uint) (strmov(strmov(key, table_list->db)+1,
127
170
table_list->table_name)-key)+1;
140
183
Functions to handle table definition cach (TABLE_SHARE)
141
184
*****************************************************************************/
143
extern "C" unsigned char *table_def_key(const unsigned char *record, size_t *length,
144
bool not_used __attribute__((unused)))
186
extern "C" uchar *table_def_key(const uchar *record, size_t *length,
187
my_bool not_used __attribute__((unused)))
146
189
TABLE_SHARE *entry=(TABLE_SHARE*) record;
147
190
*length= entry->table_cache_key.length;
148
return (unsigned char*) entry->table_cache_key.str;
191
return (uchar*) entry->table_cache_key.str;
152
195
static void table_def_free_entry(TABLE_SHARE *share)
197
DBUG_ENTER("table_def_free_entry");
156
200
/* remove from old_unused_share list */
174
218
return hash_init(&table_def_cache, &my_charset_bin, table_def_size,
175
219
0, 0, table_def_key,
176
(hash_free_key) table_def_free_entry, 0);
220
(hash_free_key) table_def_free_entry, 0) != 0;
180
224
void table_def_free(void)
226
DBUG_ENTER("table_def_free");
182
227
if (table_def_inited)
184
229
table_def_inited= 0;
185
230
pthread_mutex_destroy(&LOCK_table_share);
186
231
hash_free(&table_def_cache);
192
uint32_t cached_table_definitions(void)
237
uint cached_table_definitions(void)
194
239
return table_def_cache.records;
220
265
# Share for table
223
TABLE_SHARE *get_table_share(THD *thd, TableList *table_list, char *key,
224
uint32_t key_length, uint32_t db_flags, int *error)
268
TABLE_SHARE *get_table_share(THD *thd, TABLE_LIST *table_list, char *key,
269
uint key_length, uint db_flags, int *error)
226
271
TABLE_SHARE *share;
272
DBUG_ENTER("get_table_share");
230
276
/* Read table definition from cache */
231
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key,
277
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key,
235
281
if (!(share= alloc_table_share(table_list, key, key_length)))
259
305
assign_new_table_id(share);
261
if (my_hash_insert(&table_def_cache, (unsigned char*) share))
307
if (my_hash_insert(&table_def_cache, (uchar*) share))
263
309
free_table_share(share);
264
return(0); // return error
310
DBUG_RETURN(0); // return error
266
312
if (open_table_def(thd, share, db_flags))
268
314
*error= share->error;
269
(void) hash_delete(&table_def_cache, (unsigned char*) share);
315
(void) hash_delete(&table_def_cache, (uchar*) share);
272
318
share->ref_count++; // Mark in use
319
DBUG_PRINT("exit", ("share: 0x%lx ref_count: %u",
320
(ulong) share, share->ref_count));
273
321
(void) pthread_mutex_unlock(&share->mutex);
309
358
oldest_unused_share->next)
311
360
pthread_mutex_lock(&oldest_unused_share->mutex);
312
hash_delete(&table_def_cache, (unsigned char*) oldest_unused_share);
361
VOID(hash_delete(&table_def_cache, (uchar*) oldest_unused_share));
364
DBUG_PRINT("exit", ("share: 0x%lx ref_count: %u",
365
(ulong) share, share->ref_count));
325
376
static TABLE_SHARE
326
*get_table_share_with_create(THD *thd, TableList *table_list,
327
char *key, uint32_t key_length,
328
uint32_t db_flags, int *error)
377
*get_table_share_with_create(THD *thd, TABLE_LIST *table_list,
378
char *key, uint key_length,
379
uint db_flags, int *error)
330
381
TABLE_SHARE *share;
383
DBUG_ENTER("get_table_share_with_create");
333
385
share= get_table_share(thd, table_list, key, key_length, db_flags, error);
348
400
Finally, if share is still NULL, it's a real error and we need
351
@todo Rework alternative ways to deal with ER_NO_SUCH Table.
403
@todo Rework alternative ways to deal with ER_NO_SUCH TABLE.
353
405
if (share || (thd->is_error() && (thd->main_da.sql_errno() != ER_NO_SUCH_TABLE)))
357
409
/* Table didn't exist. Check if some engine can provide it */
358
410
if ((tmp= ha_create_table_from_engine(thd, table_list->db,
359
411
table_list->table_name)) < 0)
414
No such table in any engine.
415
Hide "Table doesn't exist" errors if the table belongs to a view.
416
The check for thd->is_error() is necessary to not push an
417
unwanted error in case of pre-locking, which silences
418
"no such table" errors.
419
@todo Rework the alternative ways to deal with ER_NO_SUCH TABLE.
421
if (thd->is_error() && table_list->belong_to_view)
424
my_error(ER_VIEW_INVALID, MYF(0), "", "");
364
430
/* Give right error message */
365
431
thd->clear_error();
432
DBUG_PRINT("error", ("Discovery of %s/%s failed", table_list->db,
433
table_list->table_name));
366
434
my_printf_error(ER_UNKNOWN_ERROR,
367
435
"Failed to open '%-.64s', error while "
368
436
"unpacking from engine",
369
437
MYF(0), table_list->table_name);
372
440
/* Table existed in engine. Let's open it */
373
drizzle_reset_errors(thd, 1); // Clear warnings
441
mysql_reset_errors(thd, 1); // Clear warnings
374
442
thd->clear_error(); // Clear error message
375
return(get_table_share(thd, table_list, key, key_length,
443
DBUG_RETURN(get_table_share(thd, table_list, key, key_length,
376
444
db_flags, error));
398
466
that the table is deleted or the thread is killed.
401
void release_table_share(TABLE_SHARE *share,
402
enum release_type type __attribute__((unused)))
469
void release_table_share(TABLE_SHARE *share, enum release_type type)
404
471
bool to_be_deleted= 0;
472
DBUG_ENTER("release_table_share");
474
("share: 0x%lx table: %s.%s ref_count: %u version: %lu",
475
(ulong) share, share->db.str, share->table_name.str,
476
share->ref_count, share->version));
406
478
safe_mutex_assert_owner(&LOCK_open);
451
526
TABLE_SHARE *get_cached_table_share(const char *db, const char *table_name)
453
528
char key[NAME_LEN*2+2];
454
TableList table_list;
529
TABLE_LIST table_list;
456
531
safe_mutex_assert_owner(&LOCK_open);
458
533
table_list.db= (char*) db;
459
534
table_list.table_name= (char*) table_name;
460
535
key_length= create_table_def_key((THD*) 0, key, &table_list, 0);
461
return (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key, key_length);
536
return (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key, key_length);
483
void close_handle_and_leave_table_as_lock(Table *table)
558
void close_handle_and_leave_table_as_lock(TABLE *table)
485
560
TABLE_SHARE *share, *old_share= table->s;
487
562
MEM_ROOT *mem_root= &table->mem_root;
563
DBUG_ENTER("close_handle_and_leave_table_as_lock");
489
assert(table->db_stat);
565
DBUG_ASSERT(table->db_stat);
492
568
Make a local copy of the table share and free the current one.
498
574
&key_buff, old_share->table_cache_key.length,
501
memset(share, 0, sizeof(*share));
577
bzero((char*) share, sizeof(*share));
502
578
share->set_table_cache_key(key_buff, old_share->table_cache_key.str,
503
579
old_share->table_cache_key.length);
504
580
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table()
533
609
# Pointer to list of names of open tables.
536
OPEN_TableList *list_open_tables(THD *thd __attribute__((unused)),
537
const char *db, const char *wild)
612
OPEN_TABLE_LIST *list_open_tables(THD *thd, const char *db, const char *wild)
540
OPEN_TableList **start_list, *open_list;
541
TableList table_list;
615
OPEN_TABLE_LIST **start_list, *open_list;
616
TABLE_LIST table_list;
617
DBUG_ENTER("list_open_tables");
543
pthread_mutex_lock(&LOCK_open);
544
memset(&table_list, 0, sizeof(table_list));
619
VOID(pthread_mutex_lock(&LOCK_open));
620
bzero((char*) &table_list,sizeof(table_list));
545
621
start_list= &open_list;
548
for (uint32_t idx=0 ; result == 0 && idx < open_cache.records; idx++)
624
for (uint idx=0 ; result == 0 && idx < open_cache.records; idx++)
550
OPEN_TableList *table;
551
Table *entry=(Table*) hash_element(&open_cache,idx);
626
OPEN_TABLE_LIST *table;
627
TABLE *entry=(TABLE*) hash_element(&open_cache,idx);
552
628
TABLE_SHARE *share= entry->s;
554
630
if (db && my_strcasecmp(system_charset_info, db, share->db.str))
578
if (!(*start_list = (OPEN_TableList *)
654
if (!(*start_list = (OPEN_TABLE_LIST *)
579
655
sql_alloc(sizeof(**start_list)+share->table_cache_key.length)))
581
657
open_list=0; // Out of memory
584
my_stpcpy((*start_list)->table=
585
my_stpcpy(((*start_list)->db= (char*) ((*start_list)+1)),
660
strmov((*start_list)->table=
661
strmov(((*start_list)->db= (char*) ((*start_list)+1)),
586
662
share->db.str)+1,
587
663
share->table_name.str);
588
664
(*start_list)->in_use= entry->in_use ? 1 : 0;
599
675
****************************************************************************/
602
void intern_close_table(Table *table)
678
void intern_close_table(TABLE *table)
603
679
{ // Free all structures
680
DBUG_ENTER("intern_close_table");
681
DBUG_PRINT("tcache", ("table: '%s'.'%s' 0x%lx",
682
table->s ? table->s->db.str : "?",
683
table->s ? table->s->table_name.str : "?",
604
686
free_io_cache(table);
605
687
if (table->file) // Not true if name lock
606
closefrm(table, 1); // close file
688
VOID(closefrm(table, 1)); // close file
631
715
if (table == unused_tables)
718
check_unused(); // consisty check
635
free((unsigned char*) table);
720
my_free((uchar*) table,MYF(0));
639
724
/* Free resources allocated by filesort() and read_record() */
641
void free_io_cache(Table *table)
726
void free_io_cache(TABLE *table)
728
DBUG_ENTER("free_io_cache");
643
729
if (table->sort.io_cache)
645
731
close_cached_file(table->sort.io_cache);
646
free((unsigned char*) table->sort.io_cache);
732
my_free((uchar*) table->sort.io_cache,MYF(0));
647
733
table->sort.io_cache=0;
665
751
and tables must be NULL.
668
bool close_cached_tables(THD *thd, TableList *tables, bool have_lock,
754
bool close_cached_tables(THD *thd, TABLE_LIST *tables, bool have_lock,
669
755
bool wait_for_refresh, bool wait_for_placeholders)
672
assert(thd || (!wait_for_refresh && !tables));
758
DBUG_ENTER("close_cached_tables");
759
DBUG_ASSERT(thd || (!wait_for_refresh && !tables));
675
pthread_mutex_lock(&LOCK_open);
762
VOID(pthread_mutex_lock(&LOCK_open));
678
765
refresh_version++; // Force close of open tables
679
766
while (unused_tables)
681
768
#ifdef EXTRA_DEBUG
682
if (hash_delete(&open_cache,(unsigned char*) unused_tables))
769
if (hash_delete(&open_cache,(uchar*) unused_tables))
683
770
printf("Warning: Couldn't delete open table from hash\n");
685
hash_delete(&open_cache,(unsigned char*) unused_tables);
772
VOID(hash_delete(&open_cache,(uchar*) unused_tables));
688
775
/* Free table shares */
689
776
while (oldest_unused_share->next)
691
778
pthread_mutex_lock(&oldest_unused_share->mutex);
692
hash_delete(&table_def_cache, (unsigned char*) oldest_unused_share);
779
VOID(hash_delete(&table_def_cache, (uchar*) oldest_unused_share));
781
DBUG_PRINT("tcache", ("incremented global refresh_version to: %lu",
694
783
if (wait_for_refresh)
760
849
thd->mysys_var->current_mutex= &LOCK_open;
761
850
thd->mysys_var->current_cond= &COND_refresh;
762
thd->set_proc_info("Flushing tables");
851
thd_proc_info(thd, "Flushing tables");
764
853
close_old_data_files(thd,thd->open_tables,1,1);
765
854
mysql_ha_flush(thd);
768
857
/* Wait until all threads has closed all the tables we had locked */
859
("Waiting for other threads to close their open tables"));
769
860
while (found && ! thd->killed)
772
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
863
for (uint idx=0 ; idx < open_cache.records ; idx++)
774
Table *table=(Table*) hash_element(&open_cache,idx);
865
TABLE *table=(TABLE*) hash_element(&open_cache,idx);
775
866
/* Avoid a self-deadlock. */
776
867
if (table->in_use == thd)
779
870
Note that we wait here only for tables which are actually open, and
780
not for placeholders with Table::open_placeholder set. Waiting for
871
not for placeholders with TABLE::open_placeholder set. Waiting for
781
872
latter will cause deadlock in the following scenario, for example:
783
874
conn1: lock table t1 write;
839
931
bool close_cached_connection_tables(THD *thd, bool if_wait_for_refresh,
840
932
LEX_STRING *connection, bool have_lock)
843
TableList tmp, *tables= NULL;
935
TABLE_LIST tmp, *tables= NULL;
844
936
bool result= false;
937
DBUG_ENTER("close_cached_connections");
847
memset(&tmp, 0, sizeof(TableList));
940
bzero(&tmp, sizeof(TABLE_LIST));
850
pthread_mutex_lock(&LOCK_open);
943
VOID(pthread_mutex_lock(&LOCK_open));
852
945
for (idx= 0; idx < table_def_cache.records; idx++)
872
965
tmp.table_name= share->table_name.str;
873
966
tmp.next_local= tables;
875
tables= (TableList *) memdup_root(thd->mem_root, (char*)&tmp,
968
tables= (TABLE_LIST *) memdup_root(thd->mem_root, (char*)&tmp,
880
973
result= close_cached_tables(thd, tables, true, false, false);
883
pthread_mutex_unlock(&LOCK_open);
976
VOID(pthread_mutex_unlock(&LOCK_open));
885
978
if (if_wait_for_refresh)
887
980
pthread_mutex_lock(&thd->mysys_var->mutex);
888
981
thd->mysys_var->current_mutex= 0;
889
982
thd->mysys_var->current_cond= 0;
890
thd->set_proc_info(0);
891
984
pthread_mutex_unlock(&thd->mysys_var->mutex);
977
1072
/* Free tables to hold down open files */
978
1073
while (open_cache.records > table_cache_size && unused_tables)
979
hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
1074
VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
980
1076
if (found_old_table)
982
1078
/* Tell threads waiting for refresh that something has happened */
983
1079
broadcast_refresh();
986
pthread_mutex_unlock(&LOCK_open);
1082
VOID(pthread_mutex_unlock(&LOCK_open));
1092
1189
if (thd->open_tables)
1093
1190
close_open_tables(thd);
1099
1196
/* move one table to free list */
1101
bool close_thread_table(THD *thd, Table **table_ptr)
1198
bool close_thread_table(THD *thd, TABLE **table_ptr)
1103
1200
bool found_old_table= 0;
1104
Table *table= *table_ptr;
1106
assert(table->key_read == 0);
1107
assert(!table->file || table->file->inited == handler::NONE);
1201
TABLE *table= *table_ptr;
1202
DBUG_ENTER("close_thread_table");
1203
DBUG_ASSERT(table->key_read == 0);
1204
DBUG_ASSERT(!table->file || table->file->inited == handler::NONE);
1205
DBUG_PRINT("tcache", ("table: '%s'.'%s' 0x%lx", table->s->db.str,
1206
table->s->table_name.str, (long) table));
1109
1208
*table_ptr=table->next;
1111
1210
if (table->needs_reopen_or_name_lock() ||
1112
1211
thd->version != refresh_version || !table->db_stat)
1114
hash_delete(&open_cache,(unsigned char*) table);
1213
VOID(hash_delete(&open_cache,(uchar*) table));
1115
1214
found_old_table=1;
1120
Open placeholders have Table::db_stat set to 0, so they should be
1219
Open placeholders have TABLE::db_stat set to 0, so they should be
1121
1220
handled by the first alternative.
1123
assert(!table->open_placeholder);
1222
DBUG_ASSERT(!table->open_placeholder);
1125
1224
/* Free memory and reset for next loop */
1126
1225
table->file->ha_reset();
1136
1235
unused_tables=table->next=table->prev=table;
1138
return(found_old_table);
1237
DBUG_RETURN(found_old_table);
1142
1241
/* close_temporary_tables' internal, 4 is due to uint4korr definition */
1143
static inline uint32_t tmpkeyval(THD *thd __attribute__((unused)),
1242
static inline uint tmpkeyval(THD *thd, TABLE *table)
1146
1244
return uint4korr(table->s->table_cache_key.str + table->s->table_cache_key.length - 4);
1178
1276
/* Better add "if exists", in case a RESET MASTER has been done */
1179
const char stub[]= "DROP /*!40005 TEMPORARY */ Table IF EXISTS ";
1180
uint32_t stub_len= sizeof(stub) - 1;
1277
const char stub[]= "DROP /*!40005 TEMPORARY */ TABLE IF EXISTS ";
1278
uint stub_len= sizeof(stub) - 1;
1182
1280
String s_query= String(buf, sizeof(buf), system_charset_info);
1183
1281
bool found_user_tables= false;
1259
1357
close_temporary(table, 1, 1);
1261
1359
thd->clear_error();
1262
const CHARSET_INFO * const cs_save= thd->variables.character_set_client;
1360
CHARSET_INFO *cs_save= thd->variables.character_set_client;
1263
1361
thd->variables.character_set_client= system_charset_info;
1264
1362
Query_log_event qinfo(thd, s_query.ptr(),
1265
1363
s_query.length() - 1 /* to remove trailing ',' */,
1349
1447
Also SELECT::exclude_from_table_unique_test used to exclude from check
1350
1448
tables of main SELECT of multi-delete and multi-update
1352
We also skip tables with TableList::prelocking_placeholder set,
1450
We also skip tables with TABLE_LIST::prelocking_placeholder set,
1353
1451
because we want to allow SELECTs from them, and their modification
1354
1452
will rise the error anyway.
1361
1459
0 if table is unique
1364
TableList* unique_table(THD *thd, TableList *table, TableList *table_list,
1462
TABLE_LIST* unique_table(THD *thd, TABLE_LIST *table, TABLE_LIST *table_list,
1365
1463
bool check_alias)
1368
1466
const char *d_name, *t_name, *t_alias;
1467
DBUG_ENTER("unique_table");
1468
DBUG_PRINT("enter", ("table alias: %s", table->alias));
1371
1471
If this function called for query which update table (INSERT/UPDATE/...)
1372
then we have in table->table pointer to Table object which we are
1373
updating even if it is VIEW so we need TableList of this Table object
1472
then we have in table->table pointer to TABLE object which we are
1473
updating even if it is VIEW so we need TABLE_LIST of this TABLE object
1374
1474
to get right names (even if lower_case_table_names used).
1376
1476
If this function called for CREATE command that we have not opened table
1377
(table->table equal to 0) and right names is in current TableList
1477
(table->table equal to 0) and right names is in current TABLE_LIST
1380
1480
if (table->table)
1382
1482
/* temporary table is always unique */
1383
1483
if (table->table && table->table->s->tmp_table != NO_TMP_TABLE)
1385
1485
table= table->find_underlying_table(table->table);
1387
as far as we have table->table we have to find real TableList of
1487
as far as we have table->table we have to find real TABLE_LIST of
1388
1488
it in underlying tables
1392
1492
d_name= table->db;
1393
1493
t_name= table->table_name;
1394
1494
t_alias= table->alias;
1496
DBUG_PRINT("info", ("real table: %s.%s", d_name, t_name));
1398
1499
if (((! (res= find_table_in_global_list(table_list, d_name, t_name))) &&
1428
1531
here we hide view underlying tables if we have them
1431
void update_non_unique_table_error(TableList *update,
1432
const char *operation __attribute__((unused)),
1433
TableList *duplicate __attribute__((unused)))
1534
void update_non_unique_table_error(TABLE_LIST *update,
1535
const char *operation,
1536
TABLE_LIST *duplicate)
1435
1538
my_error(ER_UPDATE_TABLE_USED, MYF(0), update->alias);
1439
Table *find_temporary_table(THD *thd, const char *db, const char *table_name)
1542
TABLE *find_temporary_table(THD *thd, const char *db, const char *table_name)
1441
TableList table_list;
1544
TABLE_LIST table_list;
1443
1546
table_list.db= (char*) db;
1444
1547
table_list.table_name= (char*) table_name;
1449
Table *find_temporary_table(THD *thd, TableList *table_list)
1552
TABLE *find_temporary_table(THD *thd, TABLE_LIST *table_list)
1451
1554
char key[MAX_DBKEY_LENGTH];
1452
1555
uint key_length;
1557
DBUG_ENTER("find_temporary_table");
1558
DBUG_PRINT("enter", ("table: '%s'.'%s'",
1559
table_list->db, table_list->table_name));
1455
1561
key_length= create_table_def_key(thd, key, table_list, 1);
1456
1562
for (table=thd->temporary_tables ; table ; table= table->next)
1458
1564
if (table->s->table_cache_key.length == key_length &&
1459
1565
!memcmp(table->s->table_cache_key.str, key, key_length))
1568
("Found table. server_id: %u pseudo_thread_id: %lu",
1569
(uint) thd->server_id,
1570
(ulong) thd->variables.pseudo_thread_id));
1462
return(0); // Not a temporary table
1574
DBUG_RETURN(0); // Not a temporary table
1478
1590
This function is used to drop user temporary tables, as well as
1479
1591
internal tables created in CREATE TEMPORARY TABLE ... SELECT
1480
or ALTER Table. Even though part of the work done by this function
1592
or ALTER TABLE. Even though part of the work done by this function
1481
1593
is redundant when the table is internal, as long as we
1482
1594
link both internal and user temporary tables into the same
1483
1595
thd->temporary_tables list, it's impossible to tell here whether
1489
1601
@retval -1 the table is in use by a outer query
1492
int drop_temporary_table(THD *thd, TableList *table_list)
1604
int drop_temporary_table(THD *thd, TABLE_LIST *table_list)
1607
DBUG_ENTER("drop_temporary_table");
1608
DBUG_PRINT("tmptable", ("closing table: '%s'.'%s'",
1609
table_list->db, table_list->table_name));
1496
1611
if (!(table= find_temporary_table(thd, table_list)))
1499
1614
/* Table might be in use by some outer statement. */
1500
1615
if (table->query_id && table->query_id != thd->query_id)
1502
1617
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1510
1625
mysql_lock_remove(thd, thd->locked_tables, table, false);
1511
1626
close_temporary_table(thd, table, 1, 1);
1516
1631
unlink from thd->temporary tables and close temporary table
1519
void close_temporary_table(THD *thd, Table *table,
1634
void close_temporary_table(THD *thd, TABLE *table,
1520
1635
bool free_share, bool delete_table)
1637
DBUG_ENTER("close_temporary_table");
1638
DBUG_PRINT("tmptable", ("closing table: '%s'.'%s' 0x%lx alias: '%s'",
1639
table->s->db.str, table->s->table_name.str,
1640
(long) table, table->alias));
1522
1642
if (table->prev)
1524
1644
table->prev->next= table->next;
1557
1677
If this is needed, use close_temporary_table()
1560
void close_temporary(Table *table, bool free_share, bool delete_table)
1680
void close_temporary(TABLE *table, bool free_share, bool delete_table)
1562
1682
handlerton *table_type= table->s->db_type();
1683
DBUG_ENTER("close_temporary");
1684
DBUG_PRINT("tmptable", ("closing table: '%s'.'%s'",
1685
table->s->db.str, table->s->table_name.str));
1564
1687
free_io_cache(table);
1565
1688
closefrm(table, 0);
1573
1696
if (free_share)
1575
1698
free_table_share(table->s);
1576
free((char*) table);
1699
my_free((char*) table,MYF(0));
1583
Used by ALTER Table when the table is a temporary one. It changes something
1706
Used by ALTER TABLE when the table is a temporary one. It changes something
1584
1707
only if the ALTER contained a RENAME clause (otherwise, table_name is the old
1586
1709
Prepares a table cache key, which is the concatenation of db, table_name and
1587
1710
thd->slave_proxy_id, separated by '\0'.
1590
bool rename_temporary_table(THD* thd, Table *table, const char *db,
1713
bool rename_temporary_table(THD* thd, TABLE *table, const char *db,
1591
1714
const char *table_name)
1594
uint32_t key_length;
1595
1718
TABLE_SHARE *share= table->s;
1596
TableList table_list;
1719
TABLE_LIST table_list;
1720
DBUG_ENTER("rename_temporary_table");
1598
1722
if (!(key=(char*) alloc_root(&share->mem_root, MAX_DBKEY_LENGTH)))
1599
return(1); /* purecov: inspected */
1723
DBUG_RETURN(1); /* purecov: inspected */
1601
1725
table_list.db= (char*) db;
1602
1726
table_list.table_name= (char*) table_name;
1603
1727
key_length= create_table_def_key(thd, key, &table_list, 1);
1604
1728
share->set_table_cache_key(key, key_length);
1609
1733
/* move table first in unused links */
1611
static void relink_unused(Table *table)
1735
static void relink_unused(TABLE *table)
1613
1737
if (table != unused_tables)
1638
1763
not locked (for example already unlocked).
1641
void unlink_open_table(THD *thd, Table *find, bool unlock)
1766
void unlink_open_table(THD *thd, TABLE *find, bool unlock)
1643
1768
char key[MAX_DBKEY_LENGTH];
1644
uint32_t key_length= find->s->table_cache_key.length;
1645
Table *list, **prev;
1769
uint key_length= find->s->table_cache_key.length;
1770
TABLE *list, **prev;
1771
DBUG_ENTER("unlink_open_table");
1647
1773
safe_mutex_assert_owner(&LOCK_open);
1711
1837
handlerton *table_type= table->s->db_type();
1712
pthread_mutex_lock(&LOCK_open);
1838
VOID(pthread_mutex_lock(&LOCK_open));
1714
1840
unlink_open_table() also tells threads waiting for refresh or close
1715
1841
that something has happened.
1717
1843
unlink_open_table(thd, table, false);
1718
1844
quick_rm_table(table_type, db_name, table_name, 0);
1719
pthread_mutex_unlock(&LOCK_open);
1845
VOID(pthread_mutex_unlock(&LOCK_open));
1738
1864
const char *proc_info;
1739
1865
thd->mysys_var->current_mutex= mutex;
1740
1866
thd->mysys_var->current_cond= cond;
1741
proc_info=thd->get_proc_info();
1742
thd->set_proc_info("Waiting for table");
1867
proc_info=thd->proc_info;
1868
thd_proc_info(thd, "Waiting for table");
1869
DBUG_ENTER("wait_for_condition");
1743
1870
if (!thd->killed)
1744
1871
(void) pthread_cond_wait(cond, mutex);
1774
1901
@return false on success, true otherwise.
1777
bool name_lock_locked_table(THD *thd, TableList *tables)
1904
bool name_lock_locked_table(THD *thd, TABLE_LIST *tables)
1906
DBUG_ENTER("name_lock_locked_table");
1779
1908
/* Under LOCK TABLES we must only accept write locked tables. */
1780
1909
tables->table= find_locked_table(thd, tables->db, tables->table_name);
1804
1933
reopen_name_locked_table()
1805
1934
thd Thread handle
1806
table_list TableList object for table to be open, TableList::table
1807
member should point to Table object which was used for
1935
table_list TABLE_LIST object for table to be open, TABLE_LIST::table
1936
member should point to TABLE object which was used for
1809
link_in true - if Table object for table to be opened should be
1938
link_in true - if TABLE object for table to be opened should be
1810
1939
linked into THD::open_tables list.
1811
1940
false - placeholder used for name-locking is already in
1812
this list so we only need to preserve Table::next
1941
this list so we only need to preserve TABLE::next
1823
bool reopen_name_locked_table(THD* thd, TableList* table_list, bool link_in)
1952
bool reopen_name_locked_table(THD* thd, TABLE_LIST* table_list, bool link_in)
1825
Table *table= table_list->table;
1954
TABLE *table= table_list->table;
1826
1955
TABLE_SHARE *share;
1827
1956
char *table_name= table_list->table_name;
1958
DBUG_ENTER("reopen_name_locked_table");
1830
1960
safe_mutex_assert_owner(&LOCK_open);
1832
1962
if (thd->killed || !table)
1835
1965
orig_table= *table;
1837
1967
if (open_unireg_entry(thd, table, table_list, table_name,
1838
1968
table->s->table_cache_key.str,
1839
table->s->table_cache_key.length))
1969
table->s->table_cache_key.length, thd->mem_root, 0))
1841
1971
intern_close_table(table);
1895
2024
@param key Table cache key for name to be locked
1896
2025
@param key_length Table cache key length
1898
@return Pointer to Table object used for name locking or 0 in
2027
@return Pointer to TABLE object used for name locking or 0 in
1899
2028
case of failure.
1902
Table *table_cache_insert_placeholder(THD *thd, const char *key,
1903
uint32_t key_length)
2031
TABLE *table_cache_insert_placeholder(THD *thd, const char *key,
1906
2035
TABLE_SHARE *share;
1907
2036
char *key_buff;
2037
DBUG_ENTER("table_cache_insert_placeholder");
1909
2039
safe_mutex_assert_owner(&LOCK_open);
1961
2091
bool lock_table_name_if_not_cached(THD *thd, const char *db,
1962
const char *table_name, Table **table)
2092
const char *table_name, TABLE **table)
1964
2094
char key[MAX_DBKEY_LENGTH];
1965
uint32_t key_length;
1967
key_length= (uint)(my_stpcpy(my_stpcpy(key, db) + 1, table_name) - key) + 1;
1968
pthread_mutex_lock(&LOCK_open);
1970
if (hash_search(&open_cache, (unsigned char *)key, key_length))
2096
DBUG_ENTER("lock_table_name_if_not_cached");
2098
key_length= (uint)(strmov(strmov(key, db) + 1, table_name) - key) + 1;
2099
VOID(pthread_mutex_lock(&LOCK_open));
2101
if (hash_search(&open_cache, (uchar *)key, key_length))
1972
pthread_mutex_unlock(&LOCK_open);
2103
VOID(pthread_mutex_unlock(&LOCK_open));
2104
DBUG_PRINT("info", ("Table is cached, name-lock is not obtained"));
1976
2108
if (!(*table= table_cache_insert_placeholder(thd, key, key_length)))
1978
pthread_mutex_unlock(&LOCK_open);
2110
VOID(pthread_mutex_unlock(&LOCK_open));
1981
2113
(*table)->open_placeholder= 1;
1982
2114
(*table)->next= thd->open_tables;
1983
2115
thd->open_tables= *table;
1984
pthread_mutex_unlock(&LOCK_open);
2116
VOID(pthread_mutex_unlock(&LOCK_open));
2007
2139
@retval false No error. 'exists' out parameter set accordingly.
2010
bool check_if_table_exists(THD *thd, TableList *table, bool *exists)
2142
bool check_if_table_exists(THD *thd, TABLE_LIST *table, bool *exists)
2012
2144
char path[FN_REFLEN];
2146
DBUG_ENTER("check_if_table_exists");
2015
2148
safe_mutex_assert_owner(&LOCK_open);
2019
2152
if (get_cached_table_share(table->db, table->table_name))
2022
2155
build_table_filename(path, sizeof(path) - 1, table->db, table->table_name,
2025
2158
if (!access(path, F_OK))
2028
2161
/* .FRM file doesn't exist. Check if some engine can provide it. */
2061
2194
If this is a NULL pointer, then the table is not
2062
2195
put in the thread-open-list.
2063
2196
flags Bitmap of flags to modify how open works:
2064
DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
2197
MYSQL_LOCK_IGNORE_FLUSH - Open table even if
2065
2198
someone has done a flush or namelock on it.
2066
2199
No version number checking is done.
2067
DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
2200
MYSQL_OPEN_TEMPORARY_ONLY - Open only temporary
2068
2201
table not the base table or view.
2073
2206
If table list element for the table to be opened has "create" flag
2074
2207
set and table does not exist, this function will automatically insert
2075
2208
a placeholder for exclusive name lock into the open tables cache and
2076
will return the Table instance that corresponds to this placeholder.
2209
will return the TABLE instance that corresponds to this placeholder.
2079
2212
NULL Open failed. If refresh is set then one should close
2080
2213
all other tables and retry the open.
2081
# Success. Pointer to Table object for open table.
2214
# Success. Pointer to TABLE object for open table.
2085
Table *open_table(THD *thd, TableList *table_list, bool *refresh, uint32_t flags)
2218
TABLE *open_table(THD *thd, TABLE_LIST *table_list, MEM_ROOT *mem_root,
2219
bool *refresh, uint flags)
2087
register Table *table;
2221
register TABLE *table;
2088
2222
char key[MAX_DBKEY_LENGTH];
2089
2223
unsigned int key_length;
2090
2224
char *alias= table_list->alias;
2091
2225
HASH_SEARCH_STATE state;
2226
DBUG_ENTER("open_table");
2093
2228
/* Parsing of partitioning information from .frm needs thd->lex set up. */
2094
assert(thd->lex->is_lex_started);
2229
DBUG_ASSERT(thd->lex->is_lex_started);
2096
2231
/* find a unused table in the open table cache */
2100
2235
/* an open table operation needs a lot of the stack space */
2101
if (check_stack_overrun(thd, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
2236
if (check_stack_overrun(thd, STACK_MIN_SIZE_FOR_OPEN, (uchar *)&alias))
2104
2239
if (thd->killed)
2107
2242
key_length= (create_table_def_key(thd, key, table_list, 1) -
2108
2243
TMP_TABLE_KEY_EXTRA);
2126
2261
We're trying to use the same temporary table twice in a query.
2127
2262
Right now we don't support this because a temporary table
2128
is always represented by only one Table object in THD, and
2263
is always represented by only one TABLE object in THD, and
2129
2264
it can not be cloned. Emit an error for an unsupported behaviour.
2131
2266
if (table->query_id)
2269
("query_id: %lu server_id: %u pseudo_thread_id: %lu",
2270
(ulong) table->query_id, (uint) thd->server_id,
2271
(ulong) thd->variables.pseudo_thread_id));
2133
2272
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
2136
2275
table->query_id= thd->query_id;
2137
2276
thd->thread_specific_used= true;
2277
DBUG_PRINT("info",("Using temporary table"));
2143
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
2283
if (flags & MYSQL_OPEN_TEMPORARY_ONLY)
2145
2285
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
2175
2315
my_error(ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG, MYF(0),
2176
2316
table->s->table_name.str);
2180
When looking for a usable Table, ignore MERGE children, as they
2320
When looking for a usable TABLE, ignore MERGE children, as they
2181
2321
belong to their parent and cannot be used explicitly.
2183
2323
if (!my_strcasecmp(system_charset_info, table->alias, alias) &&
2237
2378
this is the normal use case.
2239
2380
- try to find the table in the table cache.
2240
- if one of the discovered Table instances is name-locked
2381
- if one of the discovered TABLE instances is name-locked
2241
2382
(table->s->version == 0) or some thread has started FLUSH TABLES
2242
2383
(refresh_version > table->s->version), back off -- we have to wait
2243
2384
until no one holds a name lock on the table.
2244
- if there is no such Table in the name cache, read the table definition
2385
- if there is no such TABLE in the name cache, read the table definition
2245
2386
and insert it into the cache.
2246
2387
We perform all of the above under LOCK_open which currently protects
2247
2388
the open cache (also known as table cache) and table definitions stored
2251
pthread_mutex_lock(&LOCK_open);
2392
VOID(pthread_mutex_lock(&LOCK_open));
2254
2395
If it's the first table from a list of tables used in a query,
2290
2431
an implicit "pending locks queue" - see
2291
2432
wait_for_locked_table_names for details.
2293
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
2434
for (table= (TABLE*) hash_first(&open_cache, (uchar*) key, key_length,
2295
2436
table && table->in_use ;
2296
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
2437
table= (TABLE*) hash_next(&open_cache, (uchar*) key, key_length,
2440
DBUG_PRINT("tcache", ("in_use table: '%s'.'%s' 0x%lx", table->s->db.str,
2441
table->s->table_name.str, (long) table));
2300
2443
Here we flush tables marked for flush.
2301
2444
Normally, table->s->version contains the value of
2303
2446
(re-)opened and added to the cache.
2304
2447
If since then we did (or just started) FLUSH TABLES
2305
2448
statement, refresh_version has been increased.
2306
For "name-locked" Table instances, table->s->version is set
2449
For "name-locked" TABLE instances, table->s->version is set
2307
2450
to 0 (see lock_table_name for details).
2308
2451
In case there is a pending FLUSH TABLES or a name lock, we
2309
2452
need to back off and re-start opening tables.
2317
2460
if (table->needs_reopen_or_name_lock())
2319
if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
2463
("Found table '%s.%s' with different refresh version",
2464
table_list->db, table_list->table_name));
2466
if (flags & MYSQL_LOCK_IGNORE_FLUSH)
2321
2468
/* Force close at once after usage */
2322
2469
thd->version= table->s->version;
2396
/* Insert a new Table instance into the open cache */
2545
/* Insert a new TABLE instance into the open cache */
2547
DBUG_PRINT("tcache", ("opening new table"));
2398
2548
/* Free cache if too big */
2399
2549
while (open_cache.records > table_cache_size && unused_tables)
2400
hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
2550
VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
2402
2552
if (table_list->create)
2427
2577
table->open_placeholder= 1;
2428
2578
table->next= thd->open_tables;
2429
2579
thd->open_tables= table;
2430
pthread_mutex_unlock(&LOCK_open);
2580
VOID(pthread_mutex_unlock(&LOCK_open));
2433
2583
/* Table exists. Let us try to open it. */
2436
2586
/* make a new table */
2437
if (!(table=(Table*) my_malloc(sizeof(*table),MYF(MY_WME))))
2587
if (!(table=(TABLE*) my_malloc(sizeof(*table),MYF(MY_WME))))
2439
pthread_mutex_unlock(&LOCK_open);
2589
VOID(pthread_mutex_unlock(&LOCK_open));
2443
error= open_unireg_entry(thd, table, table_list, alias, key, key_length);
2593
error= open_unireg_entry(thd, table, table_list, alias, key, key_length,
2594
mem_root, (flags & OPEN_VIEW_NO_PARSE));
2444
2595
/* Combine the follow two */
2447
free((unsigned char*)table);
2448
pthread_mutex_unlock(&LOCK_open);
2598
my_free((uchar*)table, MYF(0));
2599
VOID(pthread_mutex_unlock(&LOCK_open));
2453
free((unsigned char*)table);
2454
pthread_mutex_unlock(&LOCK_open);
2604
my_free((uchar*)table, MYF(0));
2605
VOID(pthread_mutex_unlock(&LOCK_open));
2606
DBUG_RETURN(0); // VIEW
2457
my_hash_insert(&open_cache,(unsigned char*) table);
2608
DBUG_PRINT("info", ("inserting table '%s'.'%s' 0x%lx into the cache",
2609
table->s->db.str, table->s->table_name.str,
2611
VOID(my_hash_insert(&open_cache,(uchar*) table));
2460
pthread_mutex_unlock(&LOCK_open);
2614
check_unused(); // Debugging call
2616
VOID(pthread_mutex_unlock(&LOCK_open));
2463
2619
table->next=thd->open_tables; /* Link into simple list */
2474
2630
/* Fix alias if table name changes */
2475
2631
if (strcmp(table->alias, alias))
2477
uint32_t length=(uint) strlen(alias)+1;
2633
uint length=(uint) strlen(alias)+1;
2478
2634
table->alias= (char*) my_realloc((char*) table->alias, length,
2480
memcpy((void*) table->alias, alias, length);
2636
memcpy((char*) table->alias, alias, length);
2482
2638
/* These variables are also set in reopen_table() */
2483
2639
table->tablenr=thd->current_tablenr++;
2484
2640
table->used_fields=0;
2485
2641
table->const_table=0;
2486
table->null_row= false;
2487
table->maybe_null= false;
2488
table->force_index= false;
2642
table->null_row= table->maybe_null= table->force_index= 0;
2489
2643
table->status=STATUS_NO_RECORD;
2490
2644
table->insert_values= 0;
2645
table->fulltext_searched= 0;
2646
table->file->ft_handler= 0;
2491
2647
/* Catch wrong handling of the auto_increment_field_not_null. */
2492
assert(!table->auto_increment_field_not_null);
2648
DBUG_ASSERT(!table->auto_increment_field_not_null);
2493
2649
table->auto_increment_field_not_null= false;
2494
2650
if (table->timestamp_field)
2495
2651
table->timestamp_field_type= table->timestamp_field->get_auto_set_type();
2496
2652
table->pos_in_table_list= table_list;
2497
2653
table->clear_column_bitmaps();
2498
assert(table->key_read == 0);
2654
DBUG_ASSERT(table->key_read == 0);
2503
Table *find_locked_table(THD *thd, const char *db,const char *table_name)
2659
TABLE *find_locked_table(THD *thd, const char *db,const char *table_name)
2505
2661
char key[MAX_DBKEY_LENGTH];
2506
uint32_t key_length=(uint) (my_stpcpy(my_stpcpy(key,db)+1,table_name)-key)+1;
2662
uint key_length=(uint) (strmov(strmov(key,db)+1,table_name)-key)+1;
2508
for (Table *table=thd->open_tables; table ; table=table->next)
2664
for (TABLE *table=thd->open_tables; table ; table=table->next)
2510
2666
if (table->s->table_cache_key.length == key_length &&
2511
2667
!memcmp(table->s->table_cache_key.str, key, key_length))
2531
2687
1 error. The old table object is not changed.
2534
bool reopen_table(Table *table)
2690
bool reopen_table(TABLE *table)
2540
TableList table_list;
2696
TABLE_LIST table_list;
2541
2697
THD *thd= table->in_use;
2698
DBUG_ENTER("reopen_table");
2699
DBUG_PRINT("tcache", ("table: '%s'.'%s' 0x%lx", table->s->db.str,
2700
table->s->table_name.str, (long) table));
2543
assert(table->s->ref_count == 0);
2544
assert(!table->sort.io_cache);
2702
DBUG_ASSERT(table->s->ref_count == 0);
2703
DBUG_ASSERT(!table->sort.io_cache);
2546
2705
#ifdef EXTRA_DEBUG
2547
2706
if (table->db_stat)
2548
sql_print_error(_("Table %s had a open data handler in reopen_table"),
2707
sql_print_error("Table %s had a open data handler in reopen_table",
2551
memset(&table_list, 0, sizeof(TableList));
2710
bzero((char*) &table_list, sizeof(TABLE_LIST));
2552
2711
table_list.db= table->s->db.str;
2553
2712
table_list.table_name= table->s->table_name.str;
2554
2713
table_list.table= table;
2556
2715
if (wait_for_locked_table_names(thd, &table_list))
2557
return(1); // Thread was killed
2716
DBUG_RETURN(1); // Thread was killed
2559
2718
if (open_unireg_entry(thd, &tmp, &table_list,
2561
2720
table->s->table_cache_key.str,
2562
table->s->table_cache_key.length))
2721
table->s->table_cache_key.length,
2565
2725
/* This list copies variables set by open_table */
2581
2741
tmp.prev= table->prev;
2583
2743
if (table->file)
2584
closefrm(table, 1); // close file, free everything
2744
VOID(closefrm(table, 1)); // close file, free everything
2587
2747
table->default_column_bitmaps();
2588
2748
table->file->change_table_ptr(table, table->s);
2590
assert(table->alias != 0);
2750
DBUG_ASSERT(table->alias != 0);
2591
2751
for (field=table->field ; *field ; field++)
2593
2753
(*field)->table= (*field)->orig_table= table;
2623
2783
@note This function assumes that if we are not under LOCK TABLES,
2624
2784
then there is only one table open and locked. This means that
2625
2785
the function probably has to be adjusted before it can be used
2626
anywhere outside ALTER Table.
2786
anywhere outside ALTER TABLE.
2628
2788
@note Must not use TABLE_SHARE::table_name/db of the table being closed,
2629
2789
the strings are used in a loop even after the share may be freed.
2691
2852
bool reopen_tables(THD *thd, bool get_locks, bool mark_share_as_old)
2693
Table *table,*next,**prev;
2694
Table **tables,**tables_ptr; // For locks
2854
TABLE *table,*next,**prev;
2855
TABLE **tables,**tables_ptr; // For locks
2695
2856
bool error=0, not_used;
2696
const uint32_t flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
2697
DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
2698
DRIZZLE_LOCK_IGNORE_FLUSH;
2857
const uint flags= MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN |
2858
MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK |
2859
MYSQL_LOCK_IGNORE_FLUSH;
2861
DBUG_ENTER("reopen_tables");
2700
2863
if (!thd->open_tables)
2703
2866
safe_mutex_assert_owner(&LOCK_open);
2719
2883
prev= &thd->open_tables;
2720
2884
for (table=thd->open_tables; table ; table=next)
2722
uint32_t db_stat=table->db_stat;
2886
uint db_stat=table->db_stat;
2723
2887
next=table->next;
2724
2888
if (!tables || (!db_stat && reopen_table(table)))
2726
2890
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
2727
hash_delete(&open_cache,(unsigned char*) table);
2891
VOID(hash_delete(&open_cache,(uchar*) table));
2770
2936
if (get_locks && tables)
2772
my_afree((unsigned char*) tables);
2938
my_afree((uchar*) tables);
2774
2940
broadcast_refresh();
2780
Close handlers for tables in list, but leave the Table structure
2946
Close handlers for tables in list, but leave the TABLE structure
2781
2947
intact so that we can re-open these quickly.
2783
2949
@param thd Thread context
2784
@param table Head of the list of Table objects
2950
@param table Head of the list of TABLE objects
2785
2951
@param morph_locks true - remove locks which we have on tables being closed
2786
2952
but ensure that no DML or DDL will sneak in before
2787
2953
we will re-open the table (i.e. temporarily morph
2790
2956
@param send_refresh Should we awake waiters even if we didn't close any tables?
2793
static void close_old_data_files(THD *thd, Table *table, bool morph_locks,
2959
static void close_old_data_files(THD *thd, TABLE *table, bool morph_locks,
2794
2960
bool send_refresh)
2796
2962
bool found= send_refresh;
2963
DBUG_ENTER("close_old_data_files");
2798
2965
for (; table ; table=table->next)
2967
DBUG_PRINT("tcache", ("checking table: '%s'.'%s' 0x%lx",
2968
table->s->db.str, table->s->table_name.str,
2970
DBUG_PRINT("tcache", ("needs refresh: %d is open: %u",
2971
table->needs_reopen_or_name_lock(), table->db_stat));
2801
2973
Reopen marked for flush.
2845
3017
We come here only in close-for-back-off scenario. So we have to
2846
3018
"close" create placeholder here to avoid deadlocks (for example,
2847
3019
in case of concurrent execution of CREATE TABLE t1 SELECT * FROM t2
2848
and RENAME Table t2 TO t1). In close-for-re-open scenario we will
3020
and RENAME TABLE t2 TO t1). In close-for-re-open scenario we will
2849
3021
probably want to let it stay.
2851
3023
Note "***": We must not enter this branch if the placeholder
2868
3040
if the table is closed
2871
bool table_is_used(Table *table, bool wait_for_name_lock)
3043
bool table_is_used(TABLE *table, bool wait_for_name_lock)
3045
DBUG_ENTER("table_is_used");
2875
3048
char *key= table->s->table_cache_key.str;
2876
uint32_t key_length= table->s->table_cache_key.length;
3049
uint key_length= table->s->table_cache_key.length;
3051
DBUG_PRINT("loop", ("table_name: %s", table->alias));
2878
3052
HASH_SEARCH_STATE state;
2879
for (Table *search= (Table*) hash_first(&open_cache, (unsigned char*) key,
3053
for (TABLE *search= (TABLE*) hash_first(&open_cache, (uchar*) key,
2880
3054
key_length, &state);
2882
search= (Table*) hash_next(&open_cache, (unsigned char*) key,
3056
search= (TABLE*) hash_next(&open_cache, (uchar*) key,
2883
3057
key_length, &state))
3059
DBUG_PRINT("info", ("share: 0x%lx "
3060
"open_placeholder: %d locked_by_name: %d "
3061
"db_stat: %u version: %lu",
3063
search->open_placeholder, search->locked_by_name,
3065
search->s->version));
2885
3066
if (search->in_use == table->in_use)
2886
3067
continue; // Name locked by this thread
2959
Table *drop_locked_tables(THD *thd,const char *db, const char *table_name)
3141
TABLE *drop_locked_tables(THD *thd,const char *db, const char *table_name)
2961
Table *table,*next,**prev, *found= 0;
3143
TABLE *table,*next,**prev, *found= 0;
2962
3144
prev= &thd->open_tables;
3145
DBUG_ENTER("drop_locked_tables");
2965
3148
Note that we need to hold LOCK_open while changing the
3003
3186
broadcast_refresh();
3004
3187
if (thd->locked_tables && thd->locked_tables->table_count == 0)
3006
free((unsigned char*) thd->locked_tables);
3189
my_free((uchar*) thd->locked_tables,MYF(0));
3007
3190
thd->locked_tables=0;
3014
If we have the table open, which only happens when a LOCK Table has been
3197
If we have the table open, which only happens when a LOCK TABLE has been
3015
3198
done on the table, change the lock type to a lock that will abort all
3016
3199
other threads trying to get the lock.
3019
3202
void abort_locked_tables(THD *thd,const char *db, const char *table_name)
3022
3205
for (table= thd->open_tables; table ; table= table->next)
3024
3207
if (!strcmp(table->s->table_name.str, table_name) &&
3059
3242
share opens have been executed while one table was open all the
3062
share->table_map_id is not UINT32_MAX.
3245
share->table_map_id is not ~0UL.
3064
3247
void assign_new_table_id(TABLE_SHARE *share)
3066
static uint32_t last_table_id= UINT32_MAX;
3249
static ulong last_table_id= ~0UL;
3251
DBUG_ENTER("assign_new_table_id");
3068
3253
/* Preconditions */
3069
assert(share != NULL);
3254
DBUG_ASSERT(share != NULL);
3070
3255
safe_mutex_assert_owner(&LOCK_open);
3072
3257
ulong tid= ++last_table_id; /* get next id */
3074
3259
There is one reserved number that cannot be used. Remember to
3075
3260
change this when 6-byte global table id's are introduced.
3077
if (unlikely(tid == UINT32_MAX))
3262
if (unlikely(tid == ~0UL))
3078
3263
tid= ++last_table_id;
3079
3264
share->table_map_id= tid;
3265
DBUG_PRINT("info", ("table_id=%lu", tid));
3081
3267
/* Post conditions */
3082
assert(share->table_map_id != UINT32_MAX);
3268
DBUG_ASSERT(share->table_map_id != ~0UL);
3091
3277
open_unireg_entry()
3092
3278
thd Thread handle
3093
3279
entry Store open table definition here
3094
table_list TableList with db, table_name
3280
table_list TABLE_LIST with db, table_name & belong_to_view
3095
3281
alias Alias name
3096
3282
cache_key Key for share_cache
3097
3283
cache_key_length length of cache_key
3284
mem_root temporary mem_root for parsing
3285
flags the OPEN_VIEW_NO_PARSE flag to be passed to
3286
openfrm()/open_new_frm()
3100
3289
Extra argument for open is taken from thd->open_options
3108
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
3297
static int open_unireg_entry(THD *thd, TABLE *entry, TABLE_LIST *table_list,
3109
3298
const char *alias,
3110
char *cache_key, uint32_t cache_key_length)
3299
char *cache_key, uint cache_key_length,
3300
MEM_ROOT *mem_root, uint flags)
3113
3303
TABLE_SHARE *share;
3114
uint32_t discover_retry_count= 0;
3304
uint discover_retry_count= 0;
3305
DBUG_ENTER("open_unireg_entry");
3116
3307
safe_mutex_assert_owner(&LOCK_open);
3118
3309
if (!(share= get_table_share_with_create(thd, table_list, cache_key,
3119
3310
cache_key_length,
3120
3312
table_list->i_s_requested_object,
3124
3316
while ((error= open_table_from_share(thd, share, alias,
3125
3317
(uint) (HA_OPEN_KEYFILE |
3126
3318
HA_OPEN_RNDFILE |
3128
3320
HA_TRY_READ_ONLY),
3321
(READ_KEYINFO | COMPUTE_TYPES |
3130
3323
thd->open_options, entry, OTM_OPEN)))
3132
3325
if (error == 7) // Table def changed
3222
3415
if (mysql_bin_log.is_open())
3224
3417
char *query, *end;
3225
uint32_t query_buf_size= 20 + share->db.length + share->table_name.length +1;
3418
uint query_buf_size= 20 + share->db.length + share->table_name.length +1;
3226
3419
if ((query= (char*) my_malloc(query_buf_size,MYF(MY_WME))))
3228
3421
/* this DELETE FROM is needed even with row-based binlogging */
3229
end = strxmov(my_stpcpy(query, "DELETE FROM `"),
3230
share->db.str,"`.`",share->table_name.str,"`", NULL);
3422
end = strxmov(strmov(query, "DELETE FROM `"),
3423
share->db.str,"`.`",share->table_name.str,"`", NullS);
3231
3424
thd->binlog_query(THD::STMT_QUERY_TYPE,
3232
3425
query, (ulong)(end-query), false, false);
3426
my_free(query, MYF(0));
3239
3432
DBA on top of warning the client (which will automatically be done
3240
3433
because of MYF(MY_WME) in my_malloc() above).
3242
sql_print_error(_("When opening HEAP table, could not allocate memory "
3243
"to write 'DELETE FROM `%s`.`%s`' to the binary log"),
3435
sql_print_error("When opening HEAP table, could not allocate memory "
3436
"to write 'DELETE FROM `%s`.`%s`' to the binary log",
3244
3437
table_list->db, table_list->table_name);
3245
3438
closefrm(entry, 0);
3253
3446
release_table_share(share, RELEASE_NORMAL);
3286
int open_tables(THD *thd, TableList **start, uint32_t *counter, uint32_t flags)
3479
int open_tables(THD *thd, TABLE_LIST **start, uint *counter, uint flags)
3288
TableList *tables= NULL;
3481
TABLE_LIST *tables= NULL;
3291
3484
MEM_ROOT new_frm_mem;
3292
3485
/* Also used for indicating that prelocking is need */
3293
3486
bool safe_to_ignore_table;
3488
DBUG_ENTER("open_tables");
3296
3490
temporary mem_root for new .frm parsing.
3297
3491
TODO: variables for size
3340
3537
not opened yet. Try to open the table.
3342
3539
if (!tables->table)
3343
tables->table= open_table(thd, tables, &refresh, flags);
3541
tables->table= open_table(thd, tables, &new_frm_mem, &refresh, flags);
3544
DBUG_PRINT("tcache", ("referenced table: '%s'.'%s' 0x%lx",
3545
tables->db, tables->table_name,
3546
(long) tables->table));
3345
3548
if (!tables->table)
3384
thd->set_proc_info(0);
3591
thd_proc_info(thd, 0);
3385
3592
free_root(&new_frm_mem, MYF(0)); // Free pre-alloced block
3387
3594
if (result && tables)
3390
3597
Some functions determine success as (tables->table != NULL).
3391
tables->table is in thd->open_tables.
3598
tables->table is in thd->open_tables. It won't go lost. If the
3599
error happens on a MERGE child, clear the parents TABLE reference.
3601
if (tables->parent_l)
3602
tables->parent_l->table= NULL;
3393
3603
tables->table= NULL;
3605
DBUG_PRINT("tcache", ("returning: %d", result));
3606
DBUG_RETURN(result);
3413
static bool check_lock_and_start_stmt(THD *thd, Table *table,
3624
static bool check_lock_and_start_stmt(THD *thd, TABLE *table,
3414
3625
thr_lock_type lock_type)
3628
DBUG_ENTER("check_lock_and_start_stmt");
3418
3630
if ((int) lock_type >= (int) TL_WRITE_ALLOW_READ &&
3419
3631
(int) table->reginfo.lock_type < (int) TL_WRITE_ALLOW_READ)
3421
3633
my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0),table->alias);
3424
3636
if ((error=table->file->start_stmt(thd, lock_type)))
3426
3638
table->file->print_error(error,MYF(0));
3460
3672
and locking issues because it does not call lock_tables().
3463
Table *open_n_lock_single_table(THD *thd, TableList *table_l,
3675
TABLE *open_n_lock_single_table(THD *thd, TABLE_LIST *table_l,
3464
3676
thr_lock_type lock_type)
3466
TableList *save_next_global;
3678
TABLE_LIST *save_next_global;
3679
DBUG_ENTER("open_n_lock_single_table");
3468
3681
/* Remember old 'next' pointer. */
3469
3682
save_next_global= table_l->next_global;
3508
3723
table_list->table table
3511
Table *open_ltable(THD *thd, TableList *table_list, thr_lock_type lock_type,
3512
uint32_t lock_flags)
3726
TABLE *open_ltable(THD *thd, TABLE_LIST *table_list, thr_lock_type lock_type,
3731
DBUG_ENTER("open_ltable");
3517
thd->set_proc_info("Opening table");
3733
thd_proc_info(thd, "Opening table");
3518
3734
thd->current_tablenr= 0;
3519
while (!(table= open_table(thd, table_list, &refresh, 0)) &&
3735
/* open_ltable can be used only for BASIC TABLEs */
3736
table_list->required_type= FRMTYPE_TABLE;
3737
while (!(table= open_table(thd, table_list, thd->mem_root, &refresh, 0)) &&
3568
3786
the third argument set appropriately.
3571
int open_and_lock_tables_derived(THD *thd, TableList *tables, bool derived)
3789
int open_and_lock_tables_derived(THD *thd, TABLE_LIST *tables, bool derived)
3574
3792
bool need_reopen;
3793
DBUG_ENTER("open_and_lock_tables_derived");
3794
DBUG_PRINT("enter", ("derived handling: %d", derived));
3578
3798
if (open_tables(thd, &tables, &counter, 0))
3801
DBUG_EXECUTE_IF("sleep_open_and_lock_after_open", {
3802
const char *old_proc_info= thd->proc_info;
3803
thd->proc_info= "DBUG sleep";
3805
thd->proc_info= old_proc_info;});
3581
3807
if (!lock_tables(thd, tables, counter, &need_reopen))
3583
3809
if (!need_reopen)
3585
3811
close_tables_for_reopen(thd, &tables);
3588
3814
(mysql_handle_derived(thd->lex, &mysql_derived_prepare) ||
3589
3815
(thd->fill_derived_tables() &&
3590
3816
mysql_handle_derived(thd->lex, &mysql_derived_filling))))
3591
return(true); /* purecov: inspected */
3817
DBUG_RETURN(true); /* purecov: inspected */
3613
3839
data from the tables.
3616
bool open_normal_and_derived_tables(THD *thd, TableList *tables, uint32_t flags)
3842
bool open_normal_and_derived_tables(THD *thd, TABLE_LIST *tables, uint flags)
3619
assert(!thd->fill_derived_tables());
3845
DBUG_ENTER("open_normal_and_derived_tables");
3846
DBUG_ASSERT(!thd->fill_derived_tables());
3620
3847
if (open_tables(thd, &tables, &counter, flags) ||
3621
3848
mysql_handle_derived(thd->lex, &mysql_derived_prepare))
3622
return(true); /* purecov: inspected */
3849
DBUG_RETURN(true); /* purecov: inspected */
3661
3888
@param tables Tables involved in the query
3664
int decide_logging_format(THD *thd, TableList *tables)
3891
int decide_logging_format(THD *thd, TABLE_LIST *tables)
3666
3893
if (mysql_bin_log.is_open() && (thd->options & OPTION_BIN_LOG))
3668
3895
handler::Table_flags flags_some_set= handler::Table_flags();
3669
3896
handler::Table_flags flags_all_set= ~handler::Table_flags();
3670
bool multi_engine= false;
3897
my_bool multi_engine= false;
3671
3898
void* prev_ht= NULL;
3672
for (TableList *table= tables; table; table= table->next_global)
3899
for (TABLE_LIST *table= tables; table; table= table->next_global)
3674
3901
if (!table->placeholder() && table->lock_type >= TL_WRITE_ALLOW_WRITE)
3676
uint64_t const flags= table->table->file->ha_table_flags();
3903
ulonglong const flags= table->table->file->ha_table_flags();
3904
DBUG_PRINT("info", ("table: %s; ha_table_flags: %s%s",
3906
FLAGSTR(flags, HA_BINLOG_STMT_CAPABLE),
3907
FLAGSTR(flags, HA_BINLOG_ROW_CAPABLE)));
3677
3908
if (prev_ht && prev_ht != table->table->file->ht)
3678
3909
multi_engine= true;
3679
3910
prev_ht= table->table->file->ht;
3916
DBUG_PRINT("info", ("flags_all_set: %s%s",
3917
FLAGSTR(flags_all_set, HA_BINLOG_STMT_CAPABLE),
3918
FLAGSTR(flags_all_set, HA_BINLOG_ROW_CAPABLE)));
3919
DBUG_PRINT("info", ("flags_some_set: %s%s",
3920
FLAGSTR(flags_some_set, HA_BINLOG_STMT_CAPABLE),
3921
FLAGSTR(flags_some_set, HA_BINLOG_ROW_CAPABLE)));
3922
DBUG_PRINT("info", ("thd->variables.binlog_format: %ld",
3923
thd->variables.binlog_format));
3924
DBUG_PRINT("info", ("multi_engine: %s",
3925
multi_engine ? "true" : "false"));
3686
3928
if (flags_all_set == 0)
3705
3947
" but not allowed by this combination of engines");
3951
If more than one engine is involved in the statement and at
3952
least one is doing it's own logging (is *self-logging*), the
3953
statement cannot be logged atomically, so we generate an error
3954
rather than allowing the binlog to become corrupt.
3957
(flags_some_set & HA_HAS_OWN_BINLOGGING))
3959
error= ER_BINLOG_LOGGING_IMPOSSIBLE;
3960
my_error(error, MYF(0),
3961
"Statement cannot be written atomically since more"
3962
" than one engine involved and at least one engine"
3963
" is self-logging");
3966
DBUG_PRINT("info", ("error: %d", error));
3769
4030
*need_reopen= false;
3772
return(decide_logging_format(thd, tables));
4033
DBUG_RETURN(decide_logging_format(thd, tables));
3774
4035
if (!thd->locked_tables)
3776
assert(thd->lock == 0); // You must lock everything at once
3777
Table **start,**ptr;
3778
uint32_t lock_flag= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN;
4037
DBUG_ASSERT(thd->lock == 0); // You must lock everything at once
4038
TABLE **start,**ptr;
4039
uint lock_flag= MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN;
3780
if (!(ptr=start=(Table**) thd->alloc(sizeof(Table*)*count)))
4041
if (!(ptr=start=(TABLE**) thd->alloc(sizeof(TABLE*)*count)))
3782
4043
for (table= tables; table; table= table->next_global)
3784
4045
if (!table->placeholder())
3788
4049
if (!(thd->lock= mysql_lock_tables(thd, start, (uint) (ptr - start),
3789
4050
lock_flag, need_reopen)))
3796
TableList *first_not_own= thd->lex->first_not_own_table();
4057
TABLE_LIST *first_not_own= thd->lex->first_not_own_table();
3798
4059
When open_and_lock_tables() is called for a single table out of
3799
4060
a table list, the 'next_global' chain is temporarily broken. We
3867
Table *open_temporary_table(THD *thd, const char *path, const char *db,
4128
TABLE *open_temporary_table(THD *thd, const char *path, const char *db,
3868
4129
const char *table_name, bool link_in_list,
3869
4130
open_table_mode open_mode)
3872
4133
TABLE_SHARE *share;
3873
4134
char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
3874
uint32_t key_length;
3875
TableList table_list;
4136
TABLE_LIST table_list;
4137
DBUG_ENTER("open_temporary_table");
4139
("table: '%s'.'%s' path: '%s' server_id: %u "
4140
"pseudo_thread_id: %lu",
4141
db, table_name, path,
4142
(uint) thd->server_id, (ulong) thd->variables.pseudo_thread_id));
3877
4144
table_list.db= (char*) db;
3878
4145
table_list.table_name= (char*) table_name;
3879
4146
/* Create the cache_key for temporary tables */
3880
4147
key_length= create_table_def_key(thd, cache_key, &table_list, 1);
3882
if (!(tmp_table= (Table*) my_malloc(sizeof(*tmp_table) + sizeof(*share) +
4149
if (!(tmp_table= (TABLE*) my_malloc(sizeof(*tmp_table) + sizeof(*share) +
3883
4150
strlen(path)+1 + key_length,
3885
return(0); /* purecov: inspected */
4152
DBUG_RETURN(0); /* purecov: inspected */
3887
4154
share= (TABLE_SHARE*) (tmp_table+1);
3888
4155
tmp_path= (char*) (share+1);
3889
saved_cache_key= my_stpcpy(tmp_path, path)+1;
4156
saved_cache_key= strmov(tmp_path, path)+1;
3890
4157
memcpy(saved_cache_key, cache_key, key_length);
3892
4159
init_tmp_table_share(thd, share, saved_cache_key, key_length,
3893
strchr(saved_cache_key, '\0')+1, tmp_path);
4160
strend(saved_cache_key)+1, tmp_path);
3895
4162
if (open_table_def(thd, share, 0) ||
3896
4163
open_table_from_share(thd, share, table_name,
3898
4165
(uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3900
4167
(open_mode == OTM_ALTER) ?
3901
(EXTRA_RECORD | OPEN_FRM_FILE_ONLY)
4168
(READ_KEYINFO | COMPUTE_TYPES | EXTRA_RECORD |
4170
: (READ_KEYINFO | COMPUTE_TYPES | EXTRA_RECORD),
3903
4171
ha_open_options,
3904
4172
tmp_table, open_mode))
3906
4174
/* No need to lock share->mutex as this is not needed for tmp tables */
3907
4175
free_table_share(share);
3908
free((char*) tmp_table);
4176
my_free((char*) tmp_table,MYF(0));
3912
4180
tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
3976
4247
#define WRONG_GRANT (Field*) -1
3978
static void update_field_dependencies(THD *thd, Field *field, Table *table)
4249
static void update_field_dependencies(THD *thd, Field *field, TABLE *table)
4251
DBUG_ENTER("update_field_dependencies");
3980
4252
if (thd->mark_used_columns != MARK_COLUMNS_NONE)
3982
4254
MY_BITMAP *current_bitmap, *other_bitmap;
4013
4292
else if (table->get_fields_in_item_tree)
4014
4293
field->flags|= GET_FIXED_FIELDS_FLAG;
4299
Find a field by name in a view that uses merge algorithm.
4302
find_field_in_view()
4304
table_list view to search for 'name'
4306
length length of name
4307
item_name name of item if it will be created (VIEW)
4308
ref expression substituted in VIEW should be passed
4309
using this reference (return view_ref_found)
4310
register_tree_change true if ref is not stack variable and we
4311
need register changes in item tree
4314
0 field is not found
4315
view_ref_found found value in VIEW (real result is in *ref)
4316
# pointer to field - only for schema table fields
4320
find_field_in_view(THD *thd, TABLE_LIST *table_list,
4321
const char *name, uint length,
4322
const char *item_name, Item **ref,
4323
bool register_tree_change)
4325
DBUG_ENTER("find_field_in_view");
4327
("view: '%s', field name: '%s', item name: '%s', ref 0x%lx",
4328
table_list->alias, name, item_name, (ulong) ref));
4329
Field_iterator_view field_it;
4330
field_it.set(table_list);
4332
DBUG_ASSERT(table_list->schema_table_reformed ||
4333
(ref != 0 && (0) != 0));
4334
for (; !field_it.end_of_fields(); field_it.next())
4336
if (!my_strcasecmp(system_charset_info, field_it.name(), name))
4339
create_item() may, or may not create a new Item, depending on
4340
the column reference. See create_view_field() for details.
4342
Item *item= field_it.create_item(thd);
4347
*ref != NULL means that *ref contains the item that we need to
4348
replace. If the item was aliased by the user, set the alias to
4350
We need to set alias on both ref itself and on ref real item.
4352
if (*ref && !(*ref)->is_autogenerated_name)
4354
item->set_name((*ref)->name, (*ref)->name_length,
4355
system_charset_info);
4356
item->real_item()->set_name((*ref)->name, (*ref)->name_length,
4357
system_charset_info);
4359
if (register_tree_change)
4360
thd->change_item_tree(ref, item);
4363
DBUG_RETURN((Field*) view_ref_found);
4049
find_field_in_natural_join(THD *thd, TableList *table_ref, const char *name,
4050
uint32_t length __attribute__((unused)),
4051
Item **ref __attribute__((unused)), bool register_tree_change __attribute__((unused)),
4052
TableList **actual_table)
4400
find_field_in_natural_join(THD *thd, TABLE_LIST *table_ref, const char *name,
4401
uint length, Item **ref, bool register_tree_change,
4402
TABLE_LIST **actual_table)
4054
4404
List_iterator_fast<Natural_join_column>
4055
4405
field_it(*(table_ref->join_columns));
4056
4406
Natural_join_column *nj_col, *curr_nj_col;
4057
4407
Field *found_field;
4059
assert(table_ref->is_natural_join && table_ref->join_columns);
4060
assert(*actual_table == NULL);
4408
DBUG_ENTER("find_field_in_natural_join");
4409
DBUG_PRINT("enter", ("field name: '%s', ref 0x%lx",
4410
name, (ulong) ref));
4411
DBUG_ASSERT(table_ref->is_natural_join && table_ref->join_columns);
4412
DBUG_ASSERT(*actual_table == NULL);
4062
4414
for (nj_col= NULL, curr_nj_col= field_it++; curr_nj_col;
4063
4415
curr_nj_col= field_it++)
4069
4421
my_error(ER_NON_UNIQ_ERROR, MYF(0), name, thd->where);
4072
4424
nj_col= curr_nj_col;
4430
if (nj_col->view_field)
4434
create_item() may, or may not create a new Item, depending on the
4435
column reference. See create_view_field() for details.
4437
item= nj_col->create_item(thd);
4439
*ref != NULL means that *ref contains the item that we need to
4440
replace. If the item was aliased by the user, set the alias to
4442
We need to set alias on both ref itself and on ref real item.
4444
if (*ref && !(*ref)->is_autogenerated_name)
4446
item->set_name((*ref)->name, (*ref)->name_length,
4447
system_charset_info);
4448
item->real_item()->set_name((*ref)->name, (*ref)->name_length,
4449
system_charset_info);
4454
DBUG_ASSERT(nj_col->table_field == NULL);
4455
if (nj_col->table_ref->schema_table_reformed)
4458
Translation table items are always Item_fields and fixed
4459
already('mysql_schema_table' function). So we can return
4460
->field. It is used only for 'show & where' commands.
4462
DBUG_RETURN(((Item_field*) (nj_col->view_field->item))->field);
4464
if (register_tree_change)
4465
thd->change_item_tree(ref, item);
4468
found_field= (Field*) view_ref_found;
4078
4472
/* This is a base table. */
4079
assert(nj_col->table_ref->table == nj_col->table_field->table);
4473
DBUG_ASSERT(nj_col->view_field == NULL);
4474
DBUG_ASSERT(nj_col->table_ref->table == nj_col->table_field->table);
4080
4475
found_field= nj_col->table_field;
4081
4476
update_field_dependencies(thd, found_field, nj_col->table_ref->table);
4084
4479
*actual_table= nj_col->table_ref;
4086
return(found_field);
4481
DBUG_RETURN(found_field);
4109
find_field_in_table(THD *thd, Table *table, const char *name, uint32_t length,
4110
bool allow_rowid, uint32_t *cached_field_index_ptr)
4504
find_field_in_table(THD *thd, TABLE *table, const char *name, uint length,
4505
bool allow_rowid, uint *cached_field_index_ptr)
4112
4507
Field **field_ptr, *field;
4113
uint32_t cached_field_index= *cached_field_index_ptr;
4508
uint cached_field_index= *cached_field_index_ptr;
4509
DBUG_ENTER("find_field_in_table");
4510
DBUG_PRINT("enter", ("table: '%s', field name: '%s'", table->alias, name));
4115
4512
/* We assume here that table->field < NO_CACHED_FIELD_INDEX = UINT_MAX */
4116
4513
if (cached_field_index < table->s->fields &&
4142
4539
if (field_ptr && *field_ptr)
4144
if ((*field_ptr)->vcol_info)
4146
if (thd->mark_used_columns != MARK_COLUMNS_NONE)
4148
Item *vcol_item= (*field_ptr)->vcol_info->expr_item;
4150
vcol_item->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
4152
Set the virtual field for write here if
4153
1) this procedure is called for a read-only operation (SELECT), and
4154
2) the virtual column is not phycically stored in the table
4156
if ((thd->mark_used_columns != MARK_COLUMNS_WRITE) &&
4157
(not (*field_ptr)->is_stored))
4158
bitmap_set_bit((*field_ptr)->table->write_set, (*field_ptr)->field_index);
4161
4541
*cached_field_index_ptr= field_ptr - table->field;
4162
4542
field= *field_ptr;
4222
find_field_in_table_ref(THD *thd, TableList *table_list,
4223
const char *name, uint32_t length,
4602
find_field_in_table_ref(THD *thd, TABLE_LIST *table_list,
4603
const char *name, uint length,
4224
4604
const char *item_name, const char *db_name,
4225
4605
const char *table_name, Item **ref,
4226
4606
bool check_privileges, bool allow_rowid,
4227
uint32_t *cached_field_index_ptr,
4228
bool register_tree_change, TableList **actual_table)
4607
uint *cached_field_index_ptr,
4608
bool register_tree_change, TABLE_LIST **actual_table)
4232
assert(table_list->alias);
4611
DBUG_ENTER("find_field_in_table_ref");
4612
DBUG_ASSERT(table_list->alias);
4614
DBUG_ASSERT(item_name);
4616
("table: '%s' field name: '%s' item name: '%s' ref 0x%lx",
4617
table_list->alias, name, item_name, (ulong) ref));
4237
4620
Check that the table and database that qualify the current field name
4262
4645
(my_strcasecmp(table_alias_charset, table_list->alias, table_name) ||
4263
4646
(db_name && db_name[0] && table_list->db && table_list->db[0] &&
4264
4647
strcmp(db_name, table_list->db))))
4267
4650
*actual_table= NULL;
4269
4652
if (table_list->field_translation)
4654
/* 'table_list' is a view or an information schema table. */
4655
if ((fld= find_field_in_view(thd, table_list, name, length, item_name, ref,
4656
register_tree_change)))
4657
*actual_table= table_list;
4272
4659
else if (!table_list->nested_join)
4274
4661
/* 'table_list' is a stored table. */
4275
assert(table_list->table);
4662
DBUG_ASSERT(table_list->table);
4276
4663
if ((fld= find_field_in_table(thd, table_list->table, name, length,
4278
4665
cached_field_index_ptr)))
4332
4719
if (thd->mark_used_columns == MARK_COLUMNS_READ)
4333
it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
4720
it->walk(&Item::register_field_in_read_map, 1, (uchar *) 0);
4337
4724
field_to_set= fld;
4338
4725
if (field_to_set)
4340
Table *table= field_to_set->table;
4727
TABLE *table= field_to_set->table;
4341
4728
if (thd->mark_used_columns == MARK_COLUMNS_READ)
4342
4729
bitmap_set_bit(table->read_set, field_to_set->field_index);
4364
4751
# pointer to field
4367
Field *find_field_in_table_sef(Table *table, const char *name)
4754
Field *find_field_in_table_sef(TABLE *table, const char *name)
4369
4756
Field **field_ptr;
4370
4757
if (table->s->name_hash.records)
4372
field_ptr= (Field**)hash_search(&table->s->name_hash,(unsigned char*) name,
4759
field_ptr= (Field**)hash_search(&table->s->name_hash,(uchar*) name,
4433
4820
find_field_in_tables(THD *thd, Item_ident *item,
4434
TableList *first_table, TableList *last_table,
4821
TABLE_LIST *first_table, TABLE_LIST *last_table,
4435
4822
Item **ref, find_item_error_report_type report_error,
4436
4823
bool check_privileges, bool register_tree_change)
4439
4826
const char *db= item->db_name;
4440
4827
const char *table_name= item->table_name;
4441
4828
const char *name= item->field_name;
4442
uint32_t length=(uint) strlen(name);
4829
uint length=(uint) strlen(name);
4443
4830
char name_buff[NAME_LEN+1];
4444
TableList *cur_table= first_table;
4445
TableList *actual_table;
4831
TABLE_LIST *cur_table= first_table;
4832
TABLE_LIST *actual_table;
4446
4833
bool allow_rowid;
4448
4835
if (!table_name || !table_name[0])
4459
4846
This shortcut is used by prepared statements. We assume that
4460
TableList *first_table is not changed during query execution (which
4847
TABLE_LIST *first_table is not changed during query execution (which
4461
4848
is true for all queries except RENAME but luckily RENAME doesn't
4462
4849
use fields...) so we can rely on reusing pointer to its member.
4463
4850
With this optimization we also miss case when addition of one more
4464
4851
field makes some prepared query ambiguous and so erroneous, but we
4465
4852
accept this trade off.
4467
TableList *table_ref= item->cached_table;
4854
TABLE_LIST *table_ref= item->cached_table;
4469
4856
The condition (table_ref->view == NULL) ensures that we will call
4470
4857
find_field_in_table even in the case of information schema tables
4599
4987
char buff[NAME_LEN*2+1];
4600
4988
if (db && db[0])
4602
strxnmov(buff,sizeof(buff)-1,db,".",table_name,NULL);
4990
strxnmov(buff,sizeof(buff)-1,db,".",table_name,NullS);
4603
4991
table_name=buff;
4605
4993
my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, thd->where);
4972
mark_common_columns(THD *thd, TableList *table_ref_1, TableList *table_ref_2,
4973
List<String> *using_fields, uint32_t *found_using_fields)
5360
mark_common_columns(THD *thd, TABLE_LIST *table_ref_1, TABLE_LIST *table_ref_2,
5361
List<String> *using_fields, uint *found_using_fields)
4975
5363
Field_iterator_table_ref it_1, it_2;
4976
5364
Natural_join_column *nj_col_1, *nj_col_2;
4980
5368
Leaf table references to which new natural join columns are added
4981
5369
if the leaves are != NULL.
4983
TableList *leaf_1= (table_ref_1->nested_join &&
5371
TABLE_LIST *leaf_1= (table_ref_1->nested_join &&
4984
5372
!table_ref_1->is_natural_join) ?
4985
5373
NULL : table_ref_1;
4986
TableList *leaf_2= (table_ref_2->nested_join &&
5374
TABLE_LIST *leaf_2= (table_ref_2->nested_join &&
4987
5375
!table_ref_2->is_natural_join) ?
4988
5376
NULL : table_ref_2;
5378
DBUG_ENTER("mark_common_columns");
5379
DBUG_PRINT("info", ("operand_1: %s operand_2: %s",
5380
table_ref_1->alias, table_ref_2->alias));
4990
5382
*found_using_fields= 0;
4992
5384
for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
5000
5392
field_name_1= nj_col_1->name();
5001
5393
is_using_column_1= using_fields &&
5002
5394
test_if_string_in_list(field_name_1, using_fields);
5395
DBUG_PRINT ("info", ("field_name_1=%s.%s",
5396
nj_col_1->table_name() ? nj_col_1->table_name() : "",
5005
5400
Find a field with the same name in table_ref_2.
5070
5470
goto err; // out of memory
5473
The following assert checks that the two created items are of
5476
DBUG_ASSERT(!thd->lex->current_select->no_wrap_view_item);
5073
5478
In the case of no_wrap_view_item == 0, the created items must be
5074
5479
of sub-classes of Item_ident.
5076
assert(item_1->type() == Item::FIELD_ITEM ||
5481
DBUG_ASSERT(item_1->type() == Item::FIELD_ITEM ||
5077
5482
item_1->type() == Item::REF_ITEM);
5078
assert(item_2->type() == Item::FIELD_ITEM ||
5483
DBUG_ASSERT(item_2->type() == Item::FIELD_ITEM ||
5079
5484
item_2->type() == Item::REF_ITEM);
5109
5514
nj_col_1->is_common= nj_col_2->is_common= true;
5515
DBUG_PRINT ("info", ("%s.%s and %s.%s are common",
5516
nj_col_1->table_name() ?
5517
nj_col_1->table_name() : "",
5519
nj_col_2->table_name() ?
5520
nj_col_2->table_name() : "",
5113
Table *table_1= nj_col_1->table_ref->table;
5525
TABLE *table_1= nj_col_1->table_ref->table;
5114
5526
/* Mark field_1 used for table cache. */
5115
5527
bitmap_set_bit(table_1->read_set, field_1->field_index);
5116
5528
table_1->covering_keys.intersect(field_1->part_of_key);
5186
store_natural_using_join_columns(THD *thd __attribute__((unused)),
5187
TableList *natural_using_join,
5188
TableList *table_ref_1,
5189
TableList *table_ref_2,
5598
store_natural_using_join_columns(THD *thd, TABLE_LIST *natural_using_join,
5599
TABLE_LIST *table_ref_1,
5600
TABLE_LIST *table_ref_2,
5190
5601
List<String> *using_fields,
5191
uint32_t found_using_fields)
5602
uint found_using_fields)
5193
5604
Field_iterator_table_ref it_1, it_2;
5194
5605
Natural_join_column *nj_col_1, *nj_col_2;
5195
5606
bool result= true;
5196
5607
List<Natural_join_column> *non_join_columns;
5608
DBUG_ENTER("store_natural_using_join_columns");
5198
assert(!natural_using_join->join_columns);
5610
DBUG_ASSERT(!natural_using_join->join_columns);
5200
5612
if (!(non_join_columns= new List<Natural_join_column>) ||
5201
5613
!(natural_using_join->join_columns= new List<Natural_join_column>))
5287
5699
The procedure performs a post-order traversal of a nested join tree
5288
5700
and materializes the row types of NATURAL/USING joins in a
5289
bottom-up manner until it reaches the TableList elements that
5701
bottom-up manner until it reaches the TABLE_LIST elements that
5290
5702
represent the top-most NATURAL/USING joins. The procedure should be
5291
5703
applied to each element of SELECT_LEX::top_join_list (i.e. to each
5292
5704
top-level element of the FROM clause).
5305
store_top_level_join_columns(THD *thd, TableList *table_ref,
5306
TableList *left_neighbor,
5307
TableList *right_neighbor)
5717
store_top_level_join_columns(THD *thd, TABLE_LIST *table_ref,
5718
TABLE_LIST *left_neighbor,
5719
TABLE_LIST *right_neighbor)
5309
5721
bool result= true;
5723
DBUG_ENTER("store_top_level_join_columns");
5311
5725
/* Call the procedure recursively for each nested table reference. */
5312
5726
if (table_ref->nested_join)
5314
List_iterator_fast<TableList> nested_it(table_ref->nested_join->join_list);
5315
TableList *same_level_left_neighbor= nested_it++;
5316
TableList *same_level_right_neighbor= NULL;
5728
List_iterator_fast<TABLE_LIST> nested_it(table_ref->nested_join->join_list);
5729
TABLE_LIST *same_level_left_neighbor= nested_it++;
5730
TABLE_LIST *same_level_right_neighbor= NULL;
5317
5731
/* Left/right-most neighbors, possibly at higher levels in the join tree. */
5318
TableList *real_left_neighbor, *real_right_neighbor;
5732
TABLE_LIST *real_left_neighbor, *real_right_neighbor;
5320
5734
while (same_level_left_neighbor)
5322
TableList *cur_table_ref= same_level_left_neighbor;
5736
TABLE_LIST *cur_table_ref= same_level_left_neighbor;
5323
5737
same_level_left_neighbor= nested_it++;
5325
5739
The order of RIGHT JOIN operands is reversed in 'join list' to
5336
5750
cur_table_ref->outer_join & JOIN_TYPE_RIGHT)
5338
5752
/* This can happen only for JOIN ... ON. */
5339
assert(table_ref->nested_join->join_list.elements == 2);
5340
std::swap(same_level_left_neighbor, cur_table_ref);
5753
DBUG_ASSERT(table_ref->nested_join->join_list.elements == 2);
5754
swap_variables(TABLE_LIST*, same_level_left_neighbor, cur_table_ref);
5364
5778
if (table_ref->is_natural_join)
5366
assert(table_ref->nested_join &&
5780
DBUG_ASSERT(table_ref->nested_join &&
5367
5781
table_ref->nested_join->join_list.elements == 2);
5368
List_iterator_fast<TableList> operand_it(table_ref->nested_join->join_list);
5782
List_iterator_fast<TABLE_LIST> operand_it(table_ref->nested_join->join_list);
5370
5784
Notice that the order of join operands depends on whether table_ref
5371
5785
represents a LEFT or a RIGHT join. In a RIGHT join, the operands are
5372
5786
in inverted order.
5374
TableList *table_ref_2= operand_it++; /* Second NATURAL join operand.*/
5375
TableList *table_ref_1= operand_it++; /* First NATURAL join operand. */
5788
TABLE_LIST *table_ref_2= operand_it++; /* Second NATURAL join operand.*/
5789
TABLE_LIST *table_ref_1= operand_it++; /* First NATURAL join operand. */
5376
5790
List<String> *using_fields= table_ref->join_using_fields;
5377
uint32_t found_using_fields;
5791
uint found_using_fields;
5380
5794
The two join operands were interchanged in the parser, change the order
5381
5795
back for 'mark_common_columns'.
5383
5797
if (table_ref_2->outer_join & JOIN_TYPE_RIGHT)
5384
std::swap(table_ref_1, table_ref_2);
5798
swap_variables(TABLE_LIST*, table_ref_1, table_ref_2);
5385
5799
if (mark_common_columns(thd, table_ref_1, table_ref_2,
5386
5800
using_fields, &found_using_fields))
5392
5806
same as of an equivalent LEFT JOIN.
5394
5808
if (table_ref_1->outer_join & JOIN_TYPE_RIGHT)
5395
std::swap(table_ref_1, table_ref_2);
5809
swap_variables(TABLE_LIST*, table_ref_1, table_ref_2);
5396
5810
if (store_natural_using_join_columns(thd, table_ref, table_ref_1,
5397
5811
table_ref_2, using_fields,
5398
5812
found_using_fields))
5409
5823
/* Add a true condition to outer joins that have no common columns. */
5410
5824
if (table_ref_2->outer_join &&
5411
5825
!table_ref_1->on_expr && !table_ref_2->on_expr)
5412
table_ref_2->on_expr= new Item_int((int64_t) 1,1); /* Always true. */
5826
table_ref_2->on_expr= new Item_int((longlong) 1,1); /* Always true. */
5414
5828
/* Change this table reference to become a leaf for name resolution. */
5415
5829
if (left_neighbor)
5417
TableList *last_leaf_on_the_left;
5831
TABLE_LIST *last_leaf_on_the_left;
5418
5832
last_leaf_on_the_left= left_neighbor->last_leaf_for_name_resolution();
5419
5833
last_leaf_on_the_left->next_name_resolution_table= table_ref;
5421
5835
if (right_neighbor)
5423
TableList *first_leaf_on_the_right;
5837
TABLE_LIST *first_leaf_on_the_right;
5424
5838
first_leaf_on_the_right= right_neighbor->first_leaf_for_name_resolution();
5425
5839
table_ref->next_name_resolution_table= first_leaf_on_the_right;
5461
5875
static bool setup_natural_join_row_types(THD *thd,
5462
List<TableList> *from_clause,
5876
List<TABLE_LIST> *from_clause,
5463
5877
Name_resolution_context *context)
5465
5879
thd->where= "from clause";
5466
5880
if (from_clause->elements == 0)
5467
5881
return false; /* We come here in the case of UNIONs. */
5469
List_iterator_fast<TableList> table_ref_it(*from_clause);
5470
TableList *table_ref; /* Current table reference. */
5883
List_iterator_fast<TABLE_LIST> table_ref_it(*from_clause);
5884
TABLE_LIST *table_ref; /* Current table reference. */
5471
5885
/* Table reference to the left of the current. */
5472
TableList *left_neighbor;
5886
TABLE_LIST *left_neighbor;
5473
5887
/* Table reference to the right of the current. */
5474
TableList *right_neighbor= NULL;
5888
TABLE_LIST *right_neighbor= NULL;
5476
5890
/* Note that tables in the list are in reversed order */
5477
5891
for (left_neighbor= table_ref_it++; left_neighbor ; )
5479
5893
table_ref= left_neighbor;
5480
5894
left_neighbor= table_ref_it++;
5481
if (store_top_level_join_columns(thd, table_ref,
5482
left_neighbor, right_neighbor))
5895
/* For stored procedures do not redo work if already done. */
5896
if (context->select_lex->first_execution)
5486
TableList *first_leaf_on_the_right;
5487
first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution();
5488
left_neighbor->next_name_resolution_table= first_leaf_on_the_right;
5898
if (store_top_level_join_columns(thd, table_ref,
5899
left_neighbor, right_neighbor))
5903
TABLE_LIST *first_leaf_on_the_right;
5904
first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution();
5905
left_neighbor->next_name_resolution_table= first_leaf_on_the_right;
5490
5908
right_neighbor= table_ref;
5508
5926
** Expand all '*' in given fields
5509
5927
****************************************************************************/
5511
int setup_wild(THD *thd,
5512
TableList *tables __attribute__((unused)),
5514
List<Item> *sum_func_list,
5929
int setup_wild(THD *thd, TABLE_LIST *tables, List<Item> &fields,
5930
List<Item> *sum_func_list,
5521
5937
List_iterator<Item> it(fields);
5938
DBUG_ENTER("setup_wild");
5523
5940
thd->lex->current_select->cur_pos_in_select_list= 0;
5524
5941
while (wild_num && (item= it++))
5540
5957
Item_int do not need fix_fields() because it is basic constant.
5542
it.replace(new Item_int("Not_used", (int64_t) 1,
5959
it.replace(new Item_int("Not_used", (longlong) 1,
5543
5960
MY_INT64_NUM_DECIMAL_DIGITS));
5545
5962
else if (insert_fields(thd, ((Item_field*) item)->context,
5580
5997
nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
5581
5998
List_iterator<Item> it(fields);
5582
5999
bool save_is_item_list_lookup;
6000
DBUG_ENTER("setup_fields");
5584
6002
thd->mark_used_columns= mark_used_columns;
6003
DBUG_PRINT("info", ("thd->mark_used_columns: %d", thd->mark_used_columns));
5585
6004
if (allow_sum_func)
5586
6005
thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
5587
6006
thd->where= THD::DEFAULT_WHERE;
5684
6105
bool setup_tables(THD *thd, Name_resolution_context *context,
5685
List<TableList> *from_clause, TableList *tables,
5686
TableList **leaves, bool select_insert)
6106
List<TABLE_LIST> *from_clause, TABLE_LIST *tables,
6107
TABLE_LIST **leaves, bool select_insert)
5688
uint32_t tablenr= 0;
6110
DBUG_ENTER("setup_tables");
5690
assert ((select_insert && !tables->next_name_resolution_table) || !tables ||
6112
DBUG_ASSERT ((select_insert && !tables->next_name_resolution_table) || !tables ||
5691
6113
(context->table_list && context->first_name_resolution_table));
5693
6115
this is used for INSERT ... SELECT.
5694
6116
For select we setup tables except first (and its underlying tables)
5696
TableList *first_select_table= (select_insert ?
6118
TABLE_LIST *first_select_table= (select_insert ?
5697
6119
tables->next_local:
5699
6121
if (!(*leaves))
5700
6122
make_leaves_list(leaves, tables);
5702
TableList *table_list;
6124
TABLE_LIST *table_list;
5703
6125
for (table_list= *leaves;
5705
6127
table_list= table_list->next_leaf, tablenr++)
5707
Table *table= table_list->table;
6129
TABLE *table= table_list->table;
5708
6130
table->pos_in_table_list= table_list;
5709
6131
if (first_select_table &&
5710
6132
table_list->top_table() == first_select_table)
5716
6138
setup_table_map(table, table_list, tablenr);
5717
6139
if (table_list->process_index_hints(table))
5720
6142
if (tablenr > MAX_TABLES)
5722
6144
my_error(ER_TOO_MANY_TABLES,MYF(0),MAX_TABLES);
5726
6148
/* Precompute and store the row types of NATURAL/USING joins. */
5727
6149
if (setup_natural_join_row_types(thd, from_clause, context))
5757
6179
bool setup_tables_and_check_access(THD *thd,
5758
6180
Name_resolution_context *context,
5759
List<TableList> *from_clause,
6181
List<TABLE_LIST> *from_clause,
6183
TABLE_LIST **leaves,
5762
6184
bool select_insert)
5764
TableList *leaves_tmp= NULL;
6186
TABLE_LIST *leaves_tmp= NULL;
5765
6187
bool first_table= true;
5767
6189
if (setup_tables(thd, context, from_clause, tables,
5841
6267
insert_fields(THD *thd, Name_resolution_context *context, const char *db_name,
5842
const char *table_name, List_iterator<Item> *it,
5843
bool any_privileges __attribute__((unused)))
6268
const char *table_name, List_iterator<Item> *it,
6269
bool any_privileges)
5845
6271
Field_iterator_table_ref field_iterator;
5847
6273
char name_buff[NAME_LEN+1];
6274
DBUG_ENTER("insert_fields");
5849
6276
if (db_name && lower_case_table_names)
5916
6343
/* Mark fields as used to allow storage engine to optimze access */
5917
6344
bitmap_set_bit(field->table->read_set, field->field_index);
5919
Mark virtual fields for write and others that the virtual fields
5922
if (field->vcol_info)
5924
Item *vcol_item= field->vcol_info->expr_item;
5926
vcol_item->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
5927
bitmap_set_bit(field->table->write_set, field->field_index);
5931
6347
table->covering_keys.intersect(field->part_of_key);
5934
6350
if (tables->is_natural_join)
5938
6354
In this case we are sure that the column ref will not be created
5939
6355
because it was already created and stored with the natural join.
5941
6357
Natural_join_column *nj_col;
5942
6358
if (!(nj_col= field_iterator.get_natural_column_ref()))
5944
assert(nj_col->table_field);
6360
DBUG_ASSERT(nj_col->table_field);
5945
6361
field_table= nj_col->table_ref->table;
5946
6362
if (field_table)
6000
6416
false if all is OK
6003
int setup_conds(THD *thd, TableList *tables __attribute__((unused)),
6419
int setup_conds(THD *thd, TABLE_LIST *tables, TABLE_LIST *leaves,
6007
6422
SELECT_LEX *select_lex= thd->lex->current_select;
6008
TableList *table= NULL; // For HP compilers
6423
TABLE_LIST *table= NULL; // For HP compilers
6009
6424
void *save_thd_marker= thd->thd_marker;
6011
6426
it_is_update set to true when tables of primary SELECT_LEX (SELECT_LEX
6018
6433
bool save_is_item_list_lookup= select_lex->is_item_list_lookup;
6019
6434
select_lex->is_item_list_lookup= 0;
6435
DBUG_ENTER("setup_conds");
6021
6437
thd->mark_used_columns= MARK_COLUMNS_READ;
6438
DBUG_PRINT("info", ("thd->mark_used_columns: %d", thd->mark_used_columns));
6022
6439
select_lex->cond_count= 0;
6023
6440
select_lex->between_count= 0;
6024
6441
select_lex->max_equal_elems= 0;
6140
6555
table= rfield->table;
6141
6556
if (rfield == table->next_number_field)
6142
6557
table->auto_increment_field_not_null= true;
6143
if (rfield->vcol_info &&
6144
value->type() != Item::DEFAULT_VALUE_ITEM &&
6145
value->type() != Item::NULL_ITEM &&
6146
table->s->table_category != TABLE_CATEGORY_TEMPORARY)
6148
thd->abort_on_warning= false;
6149
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
6150
ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN,
6151
ER(ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
6152
rfield->field_name, table->s->table_name.str);
6153
thd->abort_on_warning= abort_on_warning_saved;
6155
6558
if ((value->save_in_field(rfield, 0) < 0) && !ignore_errors)
6157
6560
my_message(ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR), MYF(0));
6160
tbl_list.push_back(table);
6162
/* Update virtual fields*/
6163
thd->abort_on_warning= false;
6164
if (tbl_list.head())
6166
List_iterator_fast<Table> t(tbl_list);
6167
Table *prev_table= 0;
6168
while ((table= t++))
6171
Do simple optimization to prevent unnecessary re-generating
6172
values for virtual fields
6174
if (table != prev_table)
6179
if (update_virtual_fields_marked_for_write(table, false))
6185
thd->abort_on_warning= abort_on_warning_saved;
6186
return(thd->is_error());
6564
DBUG_RETURN(thd->is_error());
6188
thd->abort_on_warning= abort_on_warning_saved;
6190
6567
table->auto_increment_field_not_null= false;
6216
fill_record(THD *thd, Field **ptr, List<Item> &values,
6217
bool ignore_errors __attribute__((unused)))
6593
fill_record(THD *thd, Field **ptr, List<Item> &values, bool ignore_errors)
6219
6595
List_iterator_fast<Item> v(values);
6598
DBUG_ENTER("fill_record");
6223
List<Table> tbl_list;
6224
bool abort_on_warning_saved= thd->abort_on_warning;
6228
6602
Reset the table->auto_increment_field_not_null as it is valid for
6243
6617
table= field->table;
6244
6618
if (field == table->next_number_field)
6245
6619
table->auto_increment_field_not_null= true;
6246
if (field->vcol_info &&
6247
value->type() != Item::DEFAULT_VALUE_ITEM &&
6248
value->type() != Item::NULL_ITEM &&
6249
table->s->table_category != TABLE_CATEGORY_TEMPORARY)
6251
thd->abort_on_warning= false;
6252
push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
6253
ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN,
6254
ER(ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
6255
field->field_name, table->s->table_name.str);
6256
thd->abort_on_warning= abort_on_warning_saved;
6258
6620
if (value->save_in_field(field, 0) < 0)
6260
tbl_list.push_back(table);
6262
/* Update virtual fields*/
6263
thd->abort_on_warning= false;
6264
if (tbl_list.head())
6266
List_iterator_fast<Table> t(tbl_list);
6267
Table *prev_table= 0;
6268
while ((table= t++))
6271
Do simple optimization to prevent unnecessary re-generating
6272
values for virtual fields
6274
if (table != prev_table)
6279
if (update_virtual_fields_marked_for_write(table, false))
6287
thd->abort_on_warning= abort_on_warning_saved;
6288
return(thd->is_error());
6623
DBUG_RETURN(thd->is_error());
6291
thd->abort_on_warning= abort_on_warning_saved;
6293
6627
table->auto_increment_field_not_null= false;
6298
bool mysql_rm_tmp_tables(void)
6632
my_bool mysql_rm_tmp_tables(void)
6301
6635
char filePath[FN_REFLEN], *tmpdir, filePathCopy[FN_REFLEN];
6303
6637
FILEINFO *file;
6304
6638
TABLE_SHARE share;
6640
DBUG_ENTER("mysql_rm_tmp_tables");
6307
6642
if (!(thd= new THD))
6309
6644
thd->thread_stack= (char*) &thd;
6310
6645
thd->store_globals();
6327
6662
(file->name[1] == '.' && !file->name[2])))
6330
if (!memcmp(file->name, tmp_file_prefix, tmp_file_prefix_length))
6665
if (!bcmp((uchar*) file->name, (uchar*) tmp_file_prefix,
6666
tmp_file_prefix_length))
6332
6668
char *ext= fn_ext(file->name);
6333
uint32_t ext_len= strlen(ext);
6334
uint32_t filePath_len= snprintf(filePath, sizeof(filePath),
6335
"%s%c%s", tmpdir, FN_LIBCHAR,
6337
if (!memcmp(reg_ext, ext, ext_len))
6669
uint ext_len= strlen(ext);
6670
uint filePath_len= my_snprintf(filePath, sizeof(filePath),
6671
"%s%c%s", tmpdir, FN_LIBCHAR,
6673
if (!bcmp((uchar*) reg_ext, (uchar*) ext, ext_len))
6339
6675
handler *handler_file= 0;
6340
6676
/* We should cut file extention before deleting of table */
6387
6723
void remove_db_from_cache(const char *db)
6389
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
6725
for (uint idx=0 ; idx < open_cache.records ; idx++)
6391
Table *table=(Table*) hash_element(&open_cache,idx);
6727
TABLE *table=(TABLE*) hash_element(&open_cache,idx);
6392
6728
if (!strcmp(table->s->db.str, db))
6394
6730
table->s->version= 0L; /* Free when thread is ready */
6436
6772
bool remove_table_from_cache(THD *thd, const char *db, const char *table_name,
6439
6775
char key[MAX_DBKEY_LENGTH];
6440
uint32_t key_length;
6442
6778
TABLE_SHARE *share;
6443
6779
bool result= 0, signalled= 0;
6780
DBUG_ENTER("remove_table_from_cache");
6781
DBUG_PRINT("enter", ("table: '%s'.'%s' flags: %u", db, table_name, flags));
6445
key_length=(uint) (my_stpcpy(my_stpcpy(key,db)+1,table_name)-key)+1;
6783
key_length=(uint) (strmov(strmov(key,db)+1,table_name)-key)+1;
6448
6786
HASH_SEARCH_STATE state;
6449
6787
result= signalled= 0;
6451
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
6789
for (table= (TABLE*) hash_first(&open_cache, (uchar*) key, key_length,
6454
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
6792
table= (TABLE*) hash_next(&open_cache, (uchar*) key, key_length,
6796
DBUG_PRINT("tcache", ("found table: '%s'.'%s' 0x%lx", table->s->db.str,
6797
table->s->table_name.str, (long) table));
6459
6799
table->s->version=0L; /* Free when thread is ready */
6460
6800
if (!(in_use=table->in_use))
6802
DBUG_PRINT("info",("Table was not in use"));
6462
6803
relink_unused(table);
6464
6805
else if (in_use != thd)
6807
DBUG_PRINT("info", ("Table was in use by other thread"));
6467
6809
Mark that table is going to be deleted from cache. This will
6468
6810
force threads that are in mysql_lock_tables() (but not yet
6471
6813
in_use->some_tables_deleted= 1;
6472
6814
if (table->is_name_opened())
6816
DBUG_PRINT("info", ("Found another active instance of the table"));
6819
/* Kill delayed insert threads */
6820
if ((in_use->system_thread & SYSTEM_THREAD_DELAYED_INSERT) &&
6823
in_use->killed= THD::KILL_CONNECTION;
6824
pthread_mutex_lock(&in_use->mysys_var->mutex);
6825
if (in_use->mysys_var->current_cond)
6827
pthread_mutex_lock(in_use->mysys_var->current_mutex);
6829
pthread_cond_broadcast(in_use->mysys_var->current_cond);
6830
pthread_mutex_unlock(in_use->mysys_var->current_mutex);
6832
pthread_mutex_unlock(&in_use->mysys_var->mutex);
6477
6835
Now we must abort all tables locks used by this thread
6478
6836
as the thread may be waiting to get a lock for another table.
6854
DBUG_PRINT("info", ("Table was in use by current thread. db_stat: %u",
6495
6856
result= result || (flags & RTFC_OWNED_BY_THD_FLAG);
6497
6859
while (unused_tables && !unused_tables->s->version)
6498
hash_delete(&open_cache,(unsigned char*) unused_tables);
6860
VOID(hash_delete(&open_cache,(uchar*) unused_tables));
6862
DBUG_PRINT("info", ("Removing table from table_def_cache"));
6500
6863
/* Remove table from table definition cache if it's not in use */
6501
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(unsigned char*) key,
6864
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key,
6867
DBUG_PRINT("info", ("share version: %lu ref_count: %u",
6868
share->version, share->ref_count));
6504
6869
share->version= 0; // Mark for delete
6505
6870
if (share->ref_count == 0)
6507
6872
pthread_mutex_lock(&share->mutex);
6508
hash_delete(&table_def_cache, (unsigned char*) share);
6873
VOID(hash_delete(&table_def_cache, (uchar*) share));
6528
6894
It can happen that another thread has opened the
6529
6895
table but has not yet locked any table at all. Since
6530
6896
it can be locked waiting for a table that our thread
6531
has done LOCK Table x WRITE on previously, we need to
6897
has done LOCK TABLE x WRITE on previously, we need to
6532
6898
ensure that the thread actually hears our signal
6533
6899
before we go to sleep. Thus we wait for a short time
6534
6900
and then we retry another loop in the
6552
6918
return a->length == b->length && !strncmp(a->str, b->str, a->length);
6923
Open and lock system tables for read.
6926
open_system_tables_for_read()
6928
table_list List of tables to open.
6929
backup Pointer to Open_tables_state instance where
6930
information about currently open tables will be
6931
saved, and from which will be restored when we will
6932
end work with system tables.
6935
Thanks to restrictions which we put on opening and locking of
6936
system tables for writing, we can open and lock them for reading
6937
even when we already have some other tables open and locked. One
6938
must call close_system_tables() to close systems tables opened
6947
open_system_tables_for_read(THD *thd, TABLE_LIST *table_list,
6948
Open_tables_state *backup)
6950
DBUG_ENTER("open_system_tables_for_read");
6952
thd->reset_n_backup_open_tables_state(backup);
6956
for (TABLE_LIST *tables= table_list; tables; tables= tables->next_global)
6958
TABLE *table= open_table(thd, tables, thd->mem_root, ¬_used,
6959
MYSQL_LOCK_IGNORE_FLUSH);
6963
DBUG_ASSERT(table->s->table_category == TABLE_CATEGORY_SYSTEM);
6965
table->use_all_columns();
6966
table->reginfo.lock_type= tables->lock_type;
6967
tables->table= table;
6972
TABLE **list= (TABLE**) thd->alloc(sizeof(TABLE*) * count);
6974
for (TABLE_LIST *tables= table_list; tables; tables= tables->next_global)
6975
*(ptr++)= tables->table;
6977
thd->lock= mysql_lock_tables(thd, list, count,
6978
MYSQL_LOCK_IGNORE_FLUSH, ¬_used);
6984
close_system_tables(thd, backup);
6991
Close system tables, opened with open_system_tables_for_read().
6994
close_system_tables()
6996
backup Pointer to Open_tables_state instance which holds
6997
information about tables which were open before we
6998
decided to access system tables.
7002
close_system_tables(THD *thd, Open_tables_state *backup)
7004
close_thread_tables(thd);
7005
thd->restore_backup_open_tables_state(backup);
7010
Open and lock one system table for update.
7013
open_system_table_for_update()
7015
one_table Table to open.
7018
Table opened with this call should closed using close_thread_tables().
7022
# Pointer to TABLE object of system table
7026
open_system_table_for_update(THD *thd, TABLE_LIST *one_table)
7028
DBUG_ENTER("open_system_table_for_update");
7030
TABLE *table= open_ltable(thd, one_table, one_table->lock_type, 0);
7033
DBUG_ASSERT(table->s->table_category == TABLE_CATEGORY_SYSTEM);
7034
table->use_all_columns();
6555
7041
@} (end of group Data_Dictionary)