1
1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
17
/* Basic functions needed by many modules */
18
18
#include <drizzled/server_includes.h>
19
#include <drizzled/field/timestamp.h>
20
#include <drizzled/field/null.h>
25
#if TIME_WITH_SYS_TIME
26
# include <sys/time.h>
30
# include <sys/time.h>
35
#include <mysys/my_pthread.h>
19
37
#include <drizzled/sql_select.h>
20
38
#include <mysys/my_dir.h>
21
#include <drizzled/drizzled_error_messages.h>
22
#include <libdrizzle/gettext.h>
39
#include <drizzled/error.h>
40
#include <drizzled/gettext.h>
41
#include <drizzled/nested_join.h>
42
#include <drizzled/sql_base.h>
43
#include <drizzled/show.h>
44
#include <drizzled/item/cmpfunc.h>
45
#include <drizzled/transaction_services.h>
46
#include <drizzled/check_stack_overrun.h>
47
#include <drizzled/lock.h>
24
#define FLAGSTR(S,F) ((S) & (F) ? #F " " : "")
49
extern drizzled::TransactionServices transaction_services;
27
52
@defgroup Data_Dictionary Data Dictionary
30
55
Table *unused_tables; /* Used by mysql_test */
31
56
HASH open_cache; /* Used by mysql_test */
32
57
static HASH table_def_cache;
33
static TABLE_SHARE *oldest_unused_share, end_of_unused_share;
34
58
static pthread_mutex_t LOCK_table_share;
35
59
static bool table_def_inited= 0;
37
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
61
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
39
char *cache_key, uint cache_key_length);
40
static void free_cache_entry(Table *entry);
41
static void close_old_data_files(THD *thd, Table *table, bool morph_locks,
45
extern "C" uchar *table_cache_key(const uchar *record, size_t *length,
46
bool not_used __attribute__((unused)))
63
char *cache_key, uint32_t cache_key_length);
64
extern "C" void free_cache_entry(void *entry);
67
extern "C" unsigned char *table_cache_key(const unsigned char *record, size_t *length,
48
70
Table *entry=(Table*) record;
49
71
*length= entry->s->table_cache_key.length;
50
return (uchar*) entry->s->table_cache_key.str;
72
return (unsigned char*) entry->s->table_cache_key.str;
54
76
bool table_cache_init(void)
56
return hash_init(&open_cache, &my_charset_bin, table_cache_size+16,
57
0, 0, table_cache_key,
58
(hash_free_key) free_cache_entry, 0);
78
return hash_init(&open_cache, &my_charset_bin,
79
(size_t) table_cache_size+16,
80
0, 0, table_cache_key,
61
84
void table_cache_free(void)
63
86
if (table_def_inited)
65
close_cached_tables(NULL, NULL, false, false, false);
88
close_cached_tables(NULL, NULL, false, false);
66
89
if (!open_cache.records) // Safety first
67
90
hash_free(&open_cache);
72
uint cached_open_tables(void)
94
uint32_t cached_open_tables(void)
74
96
return open_cache.records;
78
100
Create a table cache key
81
create_table_def_key()
83
key Create key here (must be of size MAX_DBKEY_LENGTH)
84
table_list Table definition
85
tmp_table Set if table is a tmp table
88
The table cache_key is created from:
92
if the table is a tmp table, we add the following to make each tmp table
95
4 bytes for master thread id
96
4 bytes pseudo thread id
103
create_table_def_key()
104
key Create key here (must be of size MAX_DBKEY_LENGTH)
105
table_list Table definition
108
The table cache_key is created from:
112
if the table is a tmp table, we add the following to make each tmp table
115
4 bytes for master thread id
116
4 bytes pseudo thread id
102
uint create_table_def_key(THD *thd, char *key, TableList *table_list,
122
uint32_t create_table_def_key(char *key, TableList *table_list)
105
uint key_length= (uint) (stpcpy(stpcpy(key, table_list->db)+1,
106
table_list->table_name)-key)+1;
109
int4store(key + key_length, thd->server_id);
110
int4store(key + key_length + 4, thd->variables.pseudo_thread_id);
111
key_length+= TMP_TABLE_KEY_EXTRA;
126
key_pos= strcpy(key_pos, table_list->db) + strlen(table_list->db);
127
key_pos= strcpy(key_pos+1, table_list->table_name) +
128
strlen(table_list->table_name);
129
key_length= (uint32_t)(key_pos-key)+1;
113
131
return key_length;
118
136
/*****************************************************************************
119
Functions to handle table definition cach (TABLE_SHARE)
120
*****************************************************************************/
137
Functions to handle table definition cach (TableShare)
138
*****************************************************************************/
122
extern "C" uchar *table_def_key(const uchar *record, size_t *length,
123
bool not_used __attribute__((unused)))
140
extern "C" unsigned char *table_def_key(const unsigned char *record, size_t *length,
125
TABLE_SHARE *entry=(TABLE_SHARE*) record;
143
TableShare *entry=(TableShare*) record;
126
144
*length= entry->table_cache_key.length;
127
return (uchar*) entry->table_cache_key.str;
145
return (unsigned char*) entry->table_cache_key.str;
131
static void table_def_free_entry(TABLE_SHARE *share)
149
static void table_def_free_entry(TableShare *share)
135
/* remove from old_unused_share list */
136
pthread_mutex_lock(&LOCK_table_share);
137
*share->prev= share->next;
138
share->next->prev= share->prev;
139
pthread_mutex_unlock(&LOCK_table_share);
141
free_table_share(share);
151
share->free_table_share();
330
299
@todo Rework alternative ways to deal with ER_NO_SUCH Table.
332
if (share || (thd->is_error() && (thd->main_da.sql_errno() != ER_NO_SUCH_TABLE)))
301
if (share || (session->is_error() && (session->main_da.sql_errno() != ER_NO_SUCH_TABLE)))
336
/* Table didn't exist. Check if some engine can provide it */
337
if ((tmp= ha_create_table_from_engine(thd, table_list->db,
338
table_list->table_name)) < 0)
343
/* Give right error message */
345
my_printf_error(ER_UNKNOWN_ERROR,
346
"Failed to open '%-.64s', error while "
347
"unpacking from engine",
348
MYF(0), table_list->table_name);
351
/* Table existed in engine. Let's open it */
352
drizzle_reset_errors(thd, 1); // Clear warnings
353
thd->clear_error(); // Clear error message
354
return(get_table_share(thd, table_list, key, key_length,
360
Mark that we are not using table share anymore.
363
release_table_share()
365
release_type How the release should be done:
367
- Release without checking
368
RELEASE_WAIT_FOR_DROP
369
- Don't return until we get a signal that the
370
table is deleted or the thread is killed.
373
If ref_count goes to zero and (we have done a refresh or if we have
374
already too many open table shares) then delete the definition.
376
If type == RELEASE_WAIT_FOR_DROP then don't return until we get a signal
377
that the table is deleted or the thread is killed.
310
Mark that we are not using table share anymore.
313
release_table_share()
317
If ref_count goes to zero and (we have done a refresh or if we have
318
already too many open table shares) then delete the definition.
320
If type == RELEASE_WAIT_FOR_DROP then don't return until we get a signal
321
that the table is deleted or the thread is killed.
380
void release_table_share(TABLE_SHARE *share,
381
enum release_type type __attribute__((unused)))
324
void release_table_share(TableShare *share)
383
bool to_be_deleted= 0;
326
bool to_be_deleted= false;
385
328
safe_mutex_assert_owner(&LOCK_open);
387
330
pthread_mutex_lock(&share->mutex);
388
331
if (!--share->ref_count)
390
if (share->version != refresh_version)
394
/* Link share last in used_table_share list */
395
assert(share->next == 0);
396
pthread_mutex_lock(&LOCK_table_share);
397
share->prev= end_of_unused_share.prev;
398
*end_of_unused_share.prev= share;
399
end_of_unused_share.prev= &share->next;
400
share->next= &end_of_unused_share;
401
pthread_mutex_unlock(&LOCK_table_share);
403
to_be_deleted= (table_def_cache.records > table_def_size);
407
334
if (to_be_deleted)
409
hash_delete(&table_def_cache, (uchar*) share);
336
hash_delete(&table_def_cache, (unsigned char*) share);
412
339
pthread_mutex_unlock(&share->mutex);
622
544
if (table->sort.io_cache)
624
546
close_cached_file(table->sort.io_cache);
625
my_free((uchar*) table->sort.io_cache,MYF(0));
626
table->sort.io_cache=0;
547
delete table->sort.io_cache;
548
table->sort.io_cache= 0;
633
554
Close all tables which aren't in use by any thread
635
@param thd Thread context
556
@param session Thread context (may be NULL)
636
557
@param tables List of tables to remove from the cache
637
558
@param have_lock If LOCK_open is locked
638
559
@param wait_for_refresh Wait for a impending flush
639
560
@param wait_for_placeholders Wait for tables being reopened so that the GRL
640
won't proceed while write-locked tables are being reopened by other
561
won't proceed while write-locked tables are being reopened by other
643
@remark THD can be NULL, but then wait_for_refresh must be false
644
and tables must be NULL.
564
@remark Session can be NULL, but then wait_for_refresh must be false
565
and tables must be NULL.
647
bool close_cached_tables(THD *thd, TableList *tables, bool have_lock,
568
bool close_cached_tables(Session *session, TableList *tables,
648
569
bool wait_for_refresh, bool wait_for_placeholders)
651
assert(thd || (!wait_for_refresh && !tables));
654
VOID(pthread_mutex_lock(&LOCK_open));
572
assert(session || (!wait_for_refresh && !tables));
574
pthread_mutex_lock(&LOCK_open); /* Optionally lock for remove tables from open_cahe if not in use */
657
578
refresh_version++; // Force close of open tables
658
579
while (unused_tables)
660
581
#ifdef EXTRA_DEBUG
661
if (hash_delete(&open_cache,(uchar*) unused_tables))
662
printf("Warning: Couldn't delete open table from hash\n");
582
if (hash_delete(&open_cache,(unsigned char*) unused_tables))
583
printf("Warning: Couldn't delete open table from hash\n");
664
VOID(hash_delete(&open_cache,(uchar*) unused_tables));
585
hash_delete(&open_cache,(unsigned char*) unused_tables);
667
/* Free table shares */
668
while (oldest_unused_share->next)
670
pthread_mutex_lock(&oldest_unused_share->mutex);
671
VOID(hash_delete(&table_def_cache, (uchar*) oldest_unused_share));
673
588
if (wait_for_refresh)
706
621
some_tables_deleted for the case when table was opened and all
707
622
related checks were passed before incrementing refresh_version
708
623
(which you already have) but attempt to lock the table happened
709
after the call to close_old_data_files() i.e. after removal of
624
after the call to Session::close_old_data_files() i.e. after removal of
710
625
current thread locks.
712
for (uint idx=0 ; idx < open_cache.records ; idx++)
627
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
714
629
Table *table=(Table*) hash_element(&open_cache,idx);
715
630
if (table->in_use)
716
table->in_use->some_tables_deleted= 1;
631
table->in_use->some_tables_deleted= false;
723
638
for (TableList *table= tables; table; table= table->next_local)
725
if (remove_table_from_cache(thd, table->db, table->table_name,
726
RTFC_OWNED_BY_THD_FLAG))
640
if (remove_table_from_cache(session, table->db, table->table_name,
641
RTFC_OWNED_BY_Session_FLAG))
730
wait_for_refresh=0; // Nothing to wait for
645
wait_for_refresh= false; // Nothing to wait for
733
648
if (wait_for_refresh)
736
652
If there is any table that has a lower refresh_version, wait until
737
653
this is closed (or this thread is killed) before returning
739
thd->mysys_var->current_mutex= &LOCK_open;
740
thd->mysys_var->current_cond= &COND_refresh;
741
thd_proc_info(thd, "Flushing tables");
743
close_old_data_files(thd,thd->open_tables,1,1);
655
session->mysys_var->current_mutex= &LOCK_open;
656
session->mysys_var->current_cond= &COND_refresh;
657
session->set_proc_info("Flushing tables");
659
session->close_old_data_files(true, true);
747
662
/* Wait until all threads has closed all the tables we had locked */
748
while (found && ! thd->killed)
663
while (found && ! session->killed)
751
for (uint idx=0 ; idx < open_cache.records ; idx++)
666
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
753
Table *table=(Table*) hash_element(&open_cache,idx);
668
Table *table=(Table*) hash_element(&open_cache,idx);
754
669
/* Avoid a self-deadlock. */
755
if (table->in_use == thd)
670
if (table->in_use == session)
758
673
Note that we wait here only for tables which are actually open, and
759
674
not for placeholders with Table::open_placeholder set. Waiting for
760
675
latter will cause deadlock in the following scenario, for example:
762
conn1: lock table t1 write;
763
conn2: lock table t2 write;
677
conn1: lock table t1 write;
678
conn2: lock table t2 write;
767
It also does not make sense to wait for those of placeholders that
768
are employed by CREATE TABLE as in this case table simply does not
682
It also does not make sense to wait for those of placeholders that
683
are employed by CREATE TABLE as in this case table simply does not
771
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
772
(table->open_placeholder && wait_for_placeholders)))
775
pthread_cond_wait(&COND_refresh,&LOCK_open);
686
if (table->needs_reopen_or_name_lock() && (table->db_stat ||
687
(table->open_placeholder && wait_for_placeholders)))
690
pthread_cond_wait(&COND_refresh,&LOCK_open);
796
712
table->s->version= refresh_version;
800
VOID(pthread_mutex_unlock(&LOCK_open));
716
pthread_mutex_unlock(&LOCK_open);
801
718
if (wait_for_refresh)
803
pthread_mutex_lock(&thd->mysys_var->mutex);
804
thd->mysys_var->current_mutex= 0;
805
thd->mysys_var->current_cond= 0;
806
thd_proc_info(thd, 0);
807
pthread_mutex_unlock(&thd->mysys_var->mutex);
814
Close all tables which match specified connection string or
815
if specified string is NULL, then any table with a connection string.
818
bool close_cached_connection_tables(THD *thd, bool if_wait_for_refresh,
819
LEX_STRING *connection, bool have_lock)
822
TableList tmp, *tables= NULL;
826
memset(&tmp, 0, sizeof(TableList));
829
VOID(pthread_mutex_lock(&LOCK_open));
831
for (idx= 0; idx < table_def_cache.records; idx++)
833
TABLE_SHARE *share= (TABLE_SHARE *) hash_element(&table_def_cache, idx);
835
/* Ignore if table is not open or does not have a connect_string */
836
if (!share->connect_string.length || !share->ref_count)
839
/* Compare the connection string */
841
(connection->length > share->connect_string.length ||
842
(connection->length < share->connect_string.length &&
843
(share->connect_string.str[connection->length] != '/' &&
844
share->connect_string.str[connection->length] != '\\')) ||
845
strncasecmp(connection->str, share->connect_string.str,
846
connection->length)))
849
/* close_cached_tables() only uses these elements */
850
tmp.db= share->db.str;
851
tmp.table_name= share->table_name.str;
852
tmp.next_local= tables;
854
tables= (TableList *) memdup_root(thd->mem_root, (char*)&tmp,
859
result= close_cached_tables(thd, tables, true, false, false);
862
VOID(pthread_mutex_unlock(&LOCK_open));
864
if (if_wait_for_refresh)
866
pthread_mutex_lock(&thd->mysys_var->mutex);
867
thd->mysys_var->current_mutex= 0;
868
thd->mysys_var->current_cond= 0;
869
thd->set_proc_info(0);
870
pthread_mutex_unlock(&thd->mysys_var->mutex);
878
Mark all temporary tables which were used by the current statement or
879
substatement as free for reuse, but only if the query_id can be cleared.
881
@param thd thread context
883
@remark For temp tables associated with a open SQL HANDLER the query_id
884
is not reset until the HANDLER is closed.
887
static void mark_temp_tables_as_free_for_reuse(THD *thd)
889
for (Table *table= thd->temporary_tables ; table ; table= table->next)
891
if ((table->query_id == thd->query_id) && ! table->open_by_handler)
894
table->file->ha_reset();
901
Mark all tables in the list which were used by current substatement
905
mark_used_tables_as_free_for_reuse()
907
table - head of the list of tables
910
Marks all tables in the list which were used by current substatement
911
(they are marked by its query_id) as free for reuse.
914
The reason we reset query_id is that it's not enough to just test
915
if table->query_id != thd->query_id to know if a table is in use.
918
SELECT f1_that_uses_t1() FROM t1;
919
In f1_that_uses_t1() we will see one instance of t1 where query_id is
920
set to query_id of original query.
923
static void mark_used_tables_as_free_for_reuse(THD *thd, Table *table)
925
for (; table ; table= table->next)
927
if (table->query_id == thd->query_id)
930
table->file->ha_reset();
937
Auxiliary function to close all tables in the open_tables list.
939
@param thd Thread context.
941
@remark It should not ordinarily be called directly.
944
static void close_open_tables(THD *thd)
946
bool found_old_table= 0;
948
safe_mutex_assert_not_owner(&LOCK_open);
950
VOID(pthread_mutex_lock(&LOCK_open));
952
while (thd->open_tables)
953
found_old_table|= close_thread_table(thd, &thd->open_tables);
954
thd->some_tables_deleted= 0;
956
/* Free tables to hold down open files */
957
while (open_cache.records > table_cache_size && unused_tables)
958
VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
961
/* Tell threads waiting for refresh that something has happened */
965
VOID(pthread_mutex_unlock(&LOCK_open));
970
Close all tables used by the current substatement, or all tables
971
used by this thread if we are on the upper level.
974
close_thread_tables()
978
Unlocks tables and frees derived tables.
979
Put all normal tables used by thread in free list.
981
It will only close/mark as free for reuse tables opened by this
982
substatement, it will also check if we are closing tables after
983
execution of complete query (i.e. we are on upper level) and will
984
leave prelocked mode if needed.
987
void close_thread_tables(THD *thd)
992
We are assuming here that thd->derived_tables contains ONLY derived
993
tables for this substatement. i.e. instead of approach which uses
994
query_id matching for determining which of the derived tables belong
995
to this substatement we rely on the ability of substatements to
996
save/restore thd->derived_tables during their execution.
998
TODO: Probably even better approach is to simply associate list of
999
derived tables with (sub-)statement instead of thread and destroy
1000
them at the end of its execution.
1002
if (thd->derived_tables)
1006
Close all derived tables generated in queries like
1007
SELECT * FROM (SELECT * FROM t1)
1009
for (table= thd->derived_tables ; table ; table= next)
1012
table->free_tmp_table(thd);
1014
thd->derived_tables= 0;
1018
Mark all temporary tables used by this statement as free for reuse.
1020
mark_temp_tables_as_free_for_reuse(thd);
1022
Let us commit transaction for statement. Since in 5.0 we only have
1023
one statement transaction and don't allow several nested statement
1024
transactions this call will do nothing if we are inside of stored
1025
function or trigger (i.e. statement transaction is already active and
1026
does not belong to statement for which we do close_thread_tables()).
1027
TODO: This should be fixed in later releases.
1029
if (!(thd->state_flags & Open_tables_state::BACKUPS_AVAIL))
1031
thd->main_da.can_overwrite_status= true;
1032
ha_autocommit_or_rollback(thd, thd->is_error());
1033
thd->main_da.can_overwrite_status= false;
1034
thd->transaction.stmt.reset();
1037
if (thd->locked_tables)
1040
/* Ensure we are calling ha_reset() for all used tables */
1041
mark_used_tables_as_free_for_reuse(thd, thd->open_tables);
1044
We are under simple LOCK TABLES so should not do anything else.
1052
For RBR we flush the pending event just before we unlock all the
1053
tables. This means that we are at the end of a topmost
1054
statement, so we ensure that the STMT_END_F flag is set on the
1055
pending event. For statements that are *inside* stored
1056
functions, the pending event will not be flushed: that will be
1057
handled either before writing a query log event (inside
1058
binlog_query()) or when preparing a pending event.
1060
thd->binlog_flush_pending_rows_event(true);
1061
mysql_unlock_tables(thd, thd->lock);
1065
Note that we need to hold LOCK_open while changing the
1066
open_tables list. Another thread may work on it.
1067
(See: remove_table_from_cache(), mysql_wait_completed_table())
1068
Closing a MERGE child before the parent would be fatal if the
1069
other thread tries to abort the MERGE lock in between.
1071
if (thd->open_tables)
1072
close_open_tables(thd);
1078
/* move one table to free list */
1080
bool close_thread_table(THD *thd, Table **table_ptr)
1082
bool found_old_table= 0;
722
pthread_mutex_lock(&session->mysys_var->mutex);
723
session->mysys_var->current_mutex= 0;
724
session->mysys_var->current_cond= 0;
725
session->set_proc_info(0);
726
pthread_mutex_unlock(&session->mysys_var->mutex);
734
move one table to free list
737
static bool free_cached_table(Session *session, Table **table_ptr)
739
bool found_old_table= false;
1083
740
Table *table= *table_ptr;
742
safe_mutex_assert_owner(&LOCK_open);
1085
743
assert(table->key_read == 0);
1086
744
assert(!table->file || table->file->inited == handler::NONE);
1088
*table_ptr=table->next;
746
*table_ptr= table->next;
1090
748
if (table->needs_reopen_or_name_lock() ||
1091
thd->version != refresh_version || !table->db_stat)
749
session->version != refresh_version || !table->db_stat)
1093
VOID(hash_delete(&open_cache,(uchar*) table));
751
hash_delete(&open_cache,(unsigned char*) table);
752
found_old_table= true;
1115
773
unused_tables=table->next=table->prev=table;
1117
return(found_old_table);
1121
/* close_temporary_tables' internal, 4 is due to uint4korr definition */
1122
static inline uint tmpkeyval(THD *thd __attribute__((unused)),
1125
return uint4korr(table->s->table_cache_key.str + table->s->table_cache_key.length - 4);
1130
Close all temporary tables created by 'CREATE TEMPORARY TABLE' for thread
1131
creates one DROP TEMPORARY Table binlog event for each pseudo-thread
776
return found_old_table;
781
Auxiliary function to close all tables in the open_tables list.
783
@param session Thread context.
785
@remark It should not ordinarily be called directly.
1134
void close_temporary_tables(THD *thd)
788
void Session::close_open_tables()
1139
/* Assume thd->options has OPTION_QUOTE_SHOW_CREATE */
1140
bool was_quote_show= true;
1142
if (!thd->temporary_tables)
1145
if (!mysql_bin_log.is_open() || thd->current_stmt_binlog_row_based)
1148
for (table= thd->temporary_tables; table; table= tmp_next)
1150
tmp_next= table->next;
1151
close_temporary(table, 1, 1);
1153
thd->temporary_tables= 0;
1157
/* Better add "if exists", in case a RESET MASTER has been done */
1158
const char stub[]= "DROP /*!40005 TEMPORARY */ Table IF EXISTS ";
1159
uint stub_len= sizeof(stub) - 1;
1161
String s_query= String(buf, sizeof(buf), system_charset_info);
1162
bool found_user_tables= false;
1164
memcpy(buf, stub, stub_len);
1167
Insertion sort of temp tables by pseudo_thread_id to build ordered list
1168
of sublists of equal pseudo_thread_id
1171
for (prev_table= thd->temporary_tables, table= prev_table->next;
1173
prev_table= table, table= table->next)
1175
Table *prev_sorted /* same as for prev_table */, *sorted;
1176
if (is_user_table(table))
1178
if (!found_user_tables)
1179
found_user_tables= true;
1180
for (prev_sorted= NULL, sorted= thd->temporary_tables; sorted != table;
1181
prev_sorted= sorted, sorted= sorted->next)
1183
if (!is_user_table(sorted) ||
1184
tmpkeyval(thd, sorted) > tmpkeyval(thd, table))
1186
/* move into the sorted part of the list from the unsorted */
1187
prev_table->next= table->next;
1188
table->next= sorted;
1191
prev_sorted->next= table;
1195
thd->temporary_tables= table;
1204
/* We always quote db,table names though it is slight overkill */
1205
if (found_user_tables &&
1206
!(was_quote_show= test(thd->options & OPTION_QUOTE_SHOW_CREATE)))
1208
thd->options |= OPTION_QUOTE_SHOW_CREATE;
1211
/* scan sorted tmps to generate sequence of DROP */
1212
for (table= thd->temporary_tables; table; table= next)
1214
if (is_user_table(table))
1216
my_thread_id save_pseudo_thread_id= thd->variables.pseudo_thread_id;
1217
/* Set pseudo_thread_id to be that of the processed table */
1218
thd->variables.pseudo_thread_id= tmpkeyval(thd, table);
1220
Loop forward through all tables within the sublist of
1221
common pseudo_thread_id to create single DROP query.
1223
for (s_query.length(stub_len);
1224
table && is_user_table(table) &&
1225
tmpkeyval(thd, table) == thd->variables.pseudo_thread_id;
1229
We are going to add 4 ` around the db/table names and possible more
1230
due to special characters in the names
1232
append_identifier(thd, &s_query, table->s->db.str, strlen(table->s->db.str));
1233
s_query.append('.');
1234
append_identifier(thd, &s_query, table->s->table_name.str,
1235
strlen(table->s->table_name.str));
1236
s_query.append(',');
1238
close_temporary(table, 1, 1);
1241
const CHARSET_INFO * const cs_save= thd->variables.character_set_client;
1242
thd->variables.character_set_client= system_charset_info;
1243
Query_log_event qinfo(thd, s_query.ptr(),
1244
s_query.length() - 1 /* to remove trailing ',' */,
1246
thd->variables.character_set_client= cs_save;
1248
Imagine the thread had created a temp table, then was doing a
1249
SELECT, and the SELECT was killed. Then it's not clever to
1250
mark the statement above as "killed", because it's not really
1251
a statement updating data, and there are 99.99% chances it
1252
will succeed on slave. If a real update (one updating a
1253
persistent table) was killed on the master, then this real
1254
update will be logged with error_code=killed, rightfully
1255
causing the slave to stop.
1257
qinfo.error_code= 0;
1258
mysql_bin_log.write(&qinfo);
1259
thd->variables.pseudo_thread_id= save_pseudo_thread_id;
1264
close_temporary(table, 1, 1);
1267
if (!was_quote_show)
1268
thd->options&= ~OPTION_QUOTE_SHOW_CREATE; /* restore option */
1269
thd->temporary_tables=0;
790
bool found_old_table= false;
792
safe_mutex_assert_not_owner(&LOCK_open);
794
pthread_mutex_lock(&LOCK_open); /* Close all open tables on Session */
797
found_old_table|= free_cached_table(this, &open_tables);
798
some_tables_deleted= false;
802
/* Tell threads waiting for refresh that something has happened */
806
pthread_mutex_unlock(&LOCK_open);
1273
810
Find table in list.
1276
find_table_in_list()
1277
table Pointer to table list
1278
offset Offset to which list in table structure to use
1279
db_name Data base name
1280
table_name Table name
1283
This is called by find_table_in_local_list() and
1284
find_table_in_global_list().
1287
NULL Table not found
1288
# Pointer to found table.
814
table Pointer to table list
815
offset Offset to which list in table structure to use
816
db_name Data base name
817
table_name Table name
820
This is called by find_table_in_local_list() and
821
find_table_in_global_list().
825
# Pointer to found table.
1291
828
TableList *find_table_in_list(TableList *table,
1292
TableList *TableList::*link,
1293
const char *db_name,
1294
const char *table_name)
829
TableList *TableList::*link,
831
const char *table_name)
1296
833
for (; table; table= table->*link )
1308
845
Test that table is unique (It's only exists once in the table list)
1313
table table which should be checked
1314
table_list list of tables
1315
check_alias whether to check tables' aliases
1317
NOTE: to exclude derived tables from check we use following mechanism:
1318
a) during derived table processing set THD::derived_tables_processing
1319
b) JOIN::prepare set SELECT::exclude_from_table_unique_test if
1320
THD::derived_tables_processing set. (we can't use JOIN::execute
1321
because for PS we perform only JOIN::prepare, but we can't set this
1322
flag in JOIN::prepare if we are not sure that we are in derived table
1323
processing loop, because multi-update call fix_fields() for some its
1324
items (which mean JOIN::prepare for subqueries) before unique_table
1325
call to detect which tables should be locked for write).
1326
c) unique_table skip all tables which belong to SELECT with
1327
SELECT::exclude_from_table_unique_test set.
1328
Also SELECT::exclude_from_table_unique_test used to exclude from check
1329
tables of main SELECT of multi-delete and multi-update
1331
We also skip tables with TableList::prelocking_placeholder set,
1332
because we want to allow SELECTs from them, and their modification
1333
will rise the error anyway.
1335
TODO: when we will have table/view change detection we can do this check
1340
0 if table is unique
849
session thread handle
850
table table which should be checked
851
table_list list of tables
852
check_alias whether to check tables' aliases
854
NOTE: to exclude derived tables from check we use following mechanism:
855
a) during derived table processing set Session::derived_tables_processing
856
b) JOIN::prepare set SELECT::exclude_from_table_unique_test if
857
Session::derived_tables_processing set. (we can't use JOIN::execute
858
because for PS we perform only JOIN::prepare, but we can't set this
859
flag in JOIN::prepare if we are not sure that we are in derived table
860
processing loop, because multi-update call fix_fields() for some its
861
items (which mean JOIN::prepare for subqueries) before unique_table
862
call to detect which tables should be locked for write).
863
c) unique_table skip all tables which belong to SELECT with
864
SELECT::exclude_from_table_unique_test set.
865
Also SELECT::exclude_from_table_unique_test used to exclude from check
866
tables of main SELECT of multi-delete and multi-update
868
We also skip tables with TableList::prelocking_placeholder set,
869
because we want to allow SELECTs from them, and their modification
870
will rise the error anyway.
872
TODO: when we will have table/view change detection we can do this check
1343
TableList* unique_table(THD *thd, TableList *table, TableList *table_list,
880
TableList* unique_table(Session *session, TableList *table, TableList *table_list,
1347
884
const char *d_name, *t_name, *t_alias;
1398
933
prevent some update operation
1401
update_non_unique_table_error()
1402
update table which we try to update
1403
operation name of update operation
1404
duplicate duplicate table which we found
936
update_non_unique_table_error()
937
update table which we try to update
938
operation name of update operation
939
duplicate duplicate table which we found
1407
here we hide view underlying tables if we have them
942
here we hide view underlying tables if we have them
1410
945
void update_non_unique_table_error(TableList *update,
1411
const char *operation __attribute__((unused)),
1412
TableList *duplicate __attribute__((unused)))
1414
949
my_error(ER_UPDATE_TABLE_USED, MYF(0), update->alias);
1418
Table *find_temporary_table(THD *thd, const char *db, const char *table_name)
953
Table *find_temporary_table(Session *session, const char *db, const char *table_name)
1420
955
TableList table_list;
1422
957
table_list.db= (char*) db;
1423
958
table_list.table_name= (char*) table_name;
1424
return find_temporary_table(thd, &table_list);
959
return find_temporary_table(session, &table_list);
1428
Table *find_temporary_table(THD *thd, TableList *table_list)
963
Table *find_temporary_table(Session *session, TableList *table_list)
1430
965
char key[MAX_DBKEY_LENGTH];
1431
966
uint key_length;
1434
key_length= create_table_def_key(thd, key, table_list, 1);
1435
for (table=thd->temporary_tables ; table ; table= table->next)
969
key_length= create_table_def_key(key, table_list);
970
for (table=session->temporary_tables ; table ; table= table->next)
1437
972
if (table->s->table_cache_key.length == key_length &&
1438
!memcmp(table->s->table_cache_key.str, key, key_length))
973
!memcmp(table->s->table_cache_key.str, key, key_length))
1441
return(0); // Not a temporary table
976
return 0; // Not a temporary table
1446
981
Drop a temporary table.
1448
Try to locate the table in the list of thd->temporary_tables.
983
Try to locate the table in the list of session->temporary_tables.
1449
984
If the table is found:
1450
- if the table is being used by some outer statement, fail.
1451
- if the table is in thd->locked_tables, unlock it and
1452
remove it from the list of locked tables. Currently only transactional
1453
temporary tables are present in the locked_tables list.
1454
- Close the temporary table, remove its .FRM
1455
- remove the table from the list of temporary tables
985
- if the table is being used by some outer statement, fail.
986
- if the table is in session->locked_tables, unlock it and
987
remove it from the list of locked tables. Currently only transactional
988
temporary tables are present in the locked_tables list.
989
- Close the temporary table, remove its .FRM
990
- remove the table from the list of temporary tables
1457
992
This function is used to drop user temporary tables, as well as
1458
993
internal tables created in CREATE TEMPORARY TABLE ... SELECT
1459
994
or ALTER Table. Even though part of the work done by this function
1460
995
is redundant when the table is internal, as long as we
1461
996
link both internal and user temporary tables into the same
1462
thd->temporary_tables list, it's impossible to tell here whether
997
session->temporary_tables list, it's impossible to tell here whether
1463
998
we're dealing with an internal or a user temporary table.
1465
1000
@retval 0 the table was found and dropped successfully.
1466
1001
@retval 1 the table was not found in the list of temporary tables
1468
1003
@retval -1 the table is in use by a outer query
1471
int drop_temporary_table(THD *thd, TableList *table_list)
1006
int drop_temporary_table(Session *session, TableList *table_list)
1475
if (!(table= find_temporary_table(thd, table_list)))
1010
if (!(table= find_temporary_table(session, table_list)))
1478
1013
/* Table might be in use by some outer statement. */
1479
if (table->query_id && table->query_id != thd->query_id)
1014
if (table->query_id && table->query_id != session->query_id)
1481
1016
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1486
1021
If LOCK TABLES list is not empty and contains this table,
1487
1022
unlock the table and remove the table from this list.
1489
mysql_lock_remove(thd, thd->locked_tables, table, false);
1490
close_temporary_table(thd, table, 1, 1);
1024
mysql_lock_remove(session, session->locked_tables, table, false);
1025
close_temporary_table(session, table, 1, 1);
1495
unlink from thd->temporary tables and close temporary table
1031
unlink from session->temporary tables and close temporary table
1498
void close_temporary_table(THD *thd, Table *table,
1034
void close_temporary_table(Session *session, Table *table,
1499
1035
bool free_share, bool delete_table)
1501
1037
if (table->prev)
1658
1183
// Notify any 'refresh' threads
1659
1184
broadcast_refresh();
1665
Auxiliary routine which closes and drops open table.
1667
@param thd Thread handle
1668
@param table Table object for table to be dropped
1669
@param db_name Name of database for this table
1670
@param table_name Name of this table
1672
@note This routine assumes that table to be closed is open only
1673
by calling thread so we needn't wait until other threads
1674
will close the table. Also unless called under implicit or
1675
explicit LOCK TABLES mode it assumes that table to be
1676
dropped is already unlocked. In the former case it will
1677
also remove lock on the table. But one should not rely on
1678
this behaviour as it may change in future.
1679
Currently, however, this function is never called for a
1680
table that was locked with LOCK TABLES.
1189
Auxiliary routine which closes and drops open table.
1191
@param session Thread handle
1192
@param table Table object for table to be dropped
1193
@param db_name Name of database for this table
1194
@param table_name Name of this table
1196
@note This routine assumes that table to be closed is open only
1197
by calling thread so we needn't wait until other threads
1198
will close the table. Also unless called under implicit or
1199
explicit LOCK TABLES mode it assumes that table to be
1200
dropped is already unlocked. In the former case it will
1201
also remove lock on the table. But one should not rely on
1202
this behaviour as it may change in future.
1203
Currently, however, this function is never called for a
1204
table that was locked with LOCK TABLES.
1683
void drop_open_table(THD *thd, Table *table, const char *db_name,
1207
void drop_open_table(Session *session, Table *table, const char *db_name,
1684
1208
const char *table_name)
1686
1210
if (table->s->tmp_table)
1687
close_temporary_table(thd, table, 1, 1);
1211
close_temporary_table(session, table, 1, 1);
1690
handlerton *table_type= table->s->db_type();
1691
VOID(pthread_mutex_lock(&LOCK_open));
1214
StorageEngine *table_type= table->s->db_type();
1215
pthread_mutex_lock(&LOCK_open); /* Close and drop a table (AUX routine) */
1693
1217
unlink_open_table() also tells threads waiting for refresh or close
1694
1218
that something has happened.
1696
unlink_open_table(thd, table, false);
1697
quick_rm_table(table_type, db_name, table_name, 0);
1698
VOID(pthread_mutex_unlock(&LOCK_open));
1220
unlink_open_table(session, table, false);
1221
quick_rm_table(table_type, db_name, table_name, false);
1222
pthread_mutex_unlock(&LOCK_open);
1704
Wait for condition but allow the user to send a kill to mysqld
1228
Wait for condition but allow the user to send a kill to mysqld
1707
wait_for_condition()
1709
mutex mutex that is currently hold that is associated with condition
1710
Will be unlocked on return
1711
cond Condition to wait for
1231
wait_for_condition()
1232
session Thread handler
1233
mutex mutex that is currently hold that is associated with condition
1234
Will be unlocked on return
1235
cond Condition to wait for
1714
void wait_for_condition(THD *thd, pthread_mutex_t *mutex, pthread_cond_t *cond)
1238
void wait_for_condition(Session *session, pthread_mutex_t *mutex, pthread_cond_t *cond)
1716
1240
/* Wait until the current table is up to date */
1717
1241
const char *proc_info;
1718
thd->mysys_var->current_mutex= mutex;
1719
thd->mysys_var->current_cond= cond;
1720
proc_info=thd->get_proc_info();
1721
thd_proc_info(thd, "Waiting for table");
1242
session->mysys_var->current_mutex= mutex;
1243
session->mysys_var->current_cond= cond;
1244
proc_info=session->get_proc_info();
1245
session->set_proc_info("Waiting for table");
1246
if (!session->killed)
1723
1247
(void) pthread_cond_wait(cond, mutex);
1897
1421
&share, sizeof(*share),
1898
1422
&key_buff, key_length,
1902
1426
table->s= share;
1903
1427
share->set_table_cache_key(key_buff, key, key_length);
1904
1428
share->tmp_table= INTERNAL_TMP_TABLE; // for intern_close_table
1429
table->in_use= session;
1906
1430
table->locked_by_name=1;
1908
if (my_hash_insert(&open_cache, (uchar*)table))
1432
if (my_hash_insert(&open_cache, (unsigned char*)table))
1910
my_free((uchar*) table, MYF(0));
1434
free((unsigned char*) table);
1919
Obtain an exclusive name lock on the table if it is not cached
1922
@param thd Thread context
1923
@param db Name of database
1924
@param table_name Name of table
1925
@param[out] table Out parameter which is either:
1926
- set to NULL if table cache contains record for
1928
- set to point to the Table instance used for
1931
@note This function takes into account all records for table in table
1932
cache, even placeholders used for name-locking. This means that
1933
'table' parameter can be set to NULL for some situations when
1934
table does not really exist.
1936
@retval true Error occured (OOM)
1937
@retval false Success. 'table' parameter set according to above rules.
1443
Obtain an exclusive name lock on the table if it is not cached
1446
@param session Thread context
1447
@param db Name of database
1448
@param table_name Name of table
1449
@param[out] table Out parameter which is either:
1450
- set to NULL if table cache contains record for
1452
- set to point to the Table instance used for
1455
@note This function takes into account all records for table in table
1456
cache, even placeholders used for name-locking. This means that
1457
'table' parameter can be set to NULL for some situations when
1458
table does not really exist.
1460
@retval true Error occured (OOM)
1461
@retval false Success. 'table' parameter set according to above rules.
1940
bool lock_table_name_if_not_cached(THD *thd, const char *db,
1464
bool lock_table_name_if_not_cached(Session *session, const char *db,
1941
1465
const char *table_name, Table **table)
1943
1467
char key[MAX_DBKEY_LENGTH];
1946
key_length= (uint)(stpcpy(stpcpy(key, db) + 1, table_name) - key) + 1;
1947
VOID(pthread_mutex_lock(&LOCK_open));
1949
if (hash_search(&open_cache, (uchar *)key, key_length))
1469
uint32_t key_length;
1471
key_pos= strcpy(key_pos, db) + strlen(db);
1472
key_pos= strcpy(key_pos+1, table_name) + strlen(table_name);
1473
key_length= (uint32_t) (key_pos-key)+1;
1475
pthread_mutex_lock(&LOCK_open); /* Obtain a name lock even though table is not in cache (like for create table) */
1477
if (hash_search(&open_cache, (unsigned char *)key, key_length))
1951
VOID(pthread_mutex_unlock(&LOCK_open));
1479
pthread_mutex_unlock(&LOCK_open);
1955
if (!(*table= table_cache_insert_placeholder(thd, key, key_length)))
1957
VOID(pthread_mutex_unlock(&LOCK_open));
1960
(*table)->open_placeholder= 1;
1961
(*table)->next= thd->open_tables;
1962
thd->open_tables= *table;
1963
VOID(pthread_mutex_unlock(&LOCK_open));
1969
Check that table exists in table definition cache, on disk
1970
or in some storage engine.
1972
@param thd Thread context
1973
@param table Table list element
1974
@param[out] exists Out parameter which is set to true if table
1975
exists and to false otherwise.
1977
@note This function assumes that caller owns LOCK_open mutex.
1978
It also assumes that the fact that there are no name-locks
1979
on the table was checked beforehand.
1981
@note If there is no .FRM file for the table but it exists in one
1982
of engines (e.g. it was created on another node of NDB cluster)
1983
this function will fetch and create proper .FRM file for it.
1985
@retval true Some error occured
1986
@retval false No error. 'exists' out parameter set accordingly.
1989
bool check_if_table_exists(THD *thd, TableList *table, bool *exists)
1991
char path[FN_REFLEN];
1994
safe_mutex_assert_owner(&LOCK_open);
1998
if (get_cached_table_share(table->db, table->table_name))
2001
build_table_filename(path, sizeof(path) - 1, table->db, table->table_name,
2004
if (!access(path, F_OK))
2007
/* .FRM file doesn't exist. Check if some engine can provide it. */
2009
rc= ha_create_table_from_engine(thd, table->db, table->table_name);
2013
/* Table does not exists in engines as well. */
2019
/* Table exists in some engine and .FRM for it was created. */
2024
my_printf_error(ER_UNKNOWN_ERROR, "Failed to open '%-.64s', error while "
2025
"unpacking from engine", MYF(0), table->table_name);
1483
if (!(*table= table_cache_insert_placeholder(session, key, key_length)))
1485
pthread_mutex_unlock(&LOCK_open);
1488
(*table)->open_placeholder= true;
1489
(*table)->next= session->open_tables;
1490
session->open_tables= *table;
1491
pthread_mutex_unlock(&LOCK_open);
2037
table_list Open first table in list.
2038
refresh INOUT Pointer to memory that will be set to 1 if
2039
we need to close all tables and reopen them.
2040
If this is a NULL pointer, then the table is not
2041
put in the thread-open-list.
2042
flags Bitmap of flags to modify how open works:
2043
DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
2044
someone has done a flush or namelock on it.
2045
No version number checking is done.
2046
DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
2047
table not the base table or view.
1500
session Thread context.
1501
table_list Open first table in list.
1502
refresh INOUT Pointer to memory that will be set to 1 if
1503
we need to close all tables and reopen them.
1504
If this is a NULL pointer, then the table is not
1505
put in the thread-open-list.
1506
flags Bitmap of flags to modify how open works:
1507
DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
1508
someone has done a flush or namelock on it.
1509
No version number checking is done.
1510
DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
1511
table not the base table or view.
2050
Uses a cache of open tables to find a table not in use.
1514
Uses a cache of open tables to find a table not in use.
2052
If table list element for the table to be opened has "create" flag
2053
set and table does not exist, this function will automatically insert
2054
a placeholder for exclusive name lock into the open tables cache and
2055
will return the Table instance that corresponds to this placeholder.
1516
If table list element for the table to be opened has "create" flag
1517
set and table does not exist, this function will automatically insert
1518
a placeholder for exclusive name lock into the open tables cache and
1519
will return the Table instance that corresponds to this placeholder.
2058
NULL Open failed. If refresh is set then one should close
2059
all other tables and retry the open.
2060
# Success. Pointer to Table object for open table.
1522
NULL Open failed. If refresh is set then one should close
1523
all other tables and retry the open.
1524
# Success. Pointer to Table object for open table.
2064
Table *open_table(THD *thd, TableList *table_list, bool *refresh, uint flags)
1528
Table *open_table(Session *session, TableList *table_list, bool *refresh, uint32_t flags)
2066
1530
register Table *table;
2067
1531
char key[MAX_DBKEY_LENGTH];
2068
1532
unsigned int key_length;
2069
char *alias= table_list->alias;
1533
const char *alias= table_list->alias;
2070
1534
HASH_SEARCH_STATE state;
2072
/* Parsing of partitioning information from .frm needs thd->lex set up. */
2073
assert(thd->lex->is_lex_started);
1536
/* Parsing of partitioning information from .frm needs session->lex set up. */
1537
assert(session->lex->is_lex_started);
2075
1539
/* find a unused table in the open table cache */
2079
1543
/* an open table operation needs a lot of the stack space */
2080
if (check_stack_overrun(thd, STACK_MIN_SIZE_FOR_OPEN, (uchar *)&alias))
2086
key_length= (create_table_def_key(thd, key, table_list, 1) -
2087
TMP_TABLE_KEY_EXTRA);
1544
if (check_stack_overrun(session, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
1547
if (session->killed)
1550
key_length= create_table_def_key(key, table_list);
2090
1553
Unless requested otherwise, try to resolve this table in the list
2091
1554
of temporary tables of this thread. In MySQL temporary tables
2092
1555
are always thread-local and "shadow" possible base tables with the
2093
1556
same name. This block implements the behaviour.
2094
TODO: move this block into a separate function.
1557
TODO -> move this block into a separate function.
1559
for (table= session->temporary_tables; table ; table=table->next)
2097
for (table= thd->temporary_tables; table ; table=table->next)
1561
if (table->s->table_cache_key.length == key_length && !memcmp(table->s->table_cache_key.str, key, key_length))
2099
if (table->s->table_cache_key.length == key_length +
2100
TMP_TABLE_KEY_EXTRA &&
2101
!memcmp(table->s->table_cache_key.str, key,
2102
key_length + TMP_TABLE_KEY_EXTRA))
1564
We're trying to use the same temporary table twice in a query.
1565
Right now we don't support this because a temporary table
1566
is always represented by only one Table object in Session, and
1567
it can not be cloned. Emit an error for an unsupported behaviour.
1569
if (table->query_id)
2105
We're trying to use the same temporary table twice in a query.
2106
Right now we don't support this because a temporary table
2107
is always represented by only one Table object in THD, and
2108
it can not be cloned. Emit an error for an unsupported behaviour.
2110
if (table->query_id)
2112
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
2115
table->query_id= thd->query_id;
2116
thd->thread_specific_used= true;
1571
my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1574
table->query_id= session->query_id;
2122
1579
if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
2124
1581
my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
2377
1829
/* Free cache if too big */
2378
1830
while (open_cache.records > table_cache_size && unused_tables)
2379
VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
1831
hash_delete(&open_cache,(unsigned char*) unused_tables); /* purecov: tested */
2381
1833
if (table_list->create)
2385
if (check_if_table_exists(thd, table_list, &exists))
2387
VOID(pthread_mutex_unlock(&LOCK_open));
1835
if (ha_table_exists_in_engine(session, table_list->db,
1836
table_list->table_name)
1837
!= HA_ERR_TABLE_EXIST)
2394
1840
Table to be created, so we need to create placeholder in table-cache.
2396
if (!(table= table_cache_insert_placeholder(thd, key, key_length)))
1842
if (!(table= table_cache_insert_placeholder(session, key, key_length)))
2398
VOID(pthread_mutex_unlock(&LOCK_open));
1844
pthread_mutex_unlock(&LOCK_open);
2402
1848
Link placeholder to the open tables list so it will be automatically
2403
1849
removed once tables are closed. Also mark it so it won't be ignored
2404
1850
by other trying to take name-lock.
2406
table->open_placeholder= 1;
2407
table->next= thd->open_tables;
2408
thd->open_tables= table;
2409
VOID(pthread_mutex_unlock(&LOCK_open));
1852
table->open_placeholder= true;
1853
table->next= session->open_tables;
1854
session->open_tables= table;
1855
pthread_mutex_unlock(&LOCK_open);
2412
1858
/* Table exists. Let us try to open it. */
2415
1861
/* make a new table */
2416
if (!(table=(Table*) my_malloc(sizeof(*table),MYF(MY_WME))))
2418
VOID(pthread_mutex_unlock(&LOCK_open));
1865
pthread_mutex_unlock(&LOCK_open);
2422
error= open_unireg_entry(thd, table, table_list, alias, key, key_length);
2423
/* Combine the follow two */
2426
my_free((uchar*)table, MYF(0));
2427
VOID(pthread_mutex_unlock(&LOCK_open));
2432
my_free((uchar*)table, MYF(0));
2433
VOID(pthread_mutex_unlock(&LOCK_open));
2436
VOID(my_hash_insert(&open_cache,(uchar*) table));
1869
error= open_unireg_entry(session, table, table_list, alias, key, key_length);
1873
pthread_mutex_unlock(&LOCK_open);
1876
my_hash_insert(&open_cache, (unsigned char*) table);
2439
VOID(pthread_mutex_unlock(&LOCK_open));
1879
pthread_mutex_unlock(&LOCK_open);
2442
table->next=thd->open_tables; /* Link into simple list */
2443
thd->open_tables=table;
1882
table->next=session->open_tables; /* Link into simple list */
1883
session->open_tables=table;
2445
table->reginfo.lock_type=TL_READ; /* Assume read */
1885
table->reginfo.lock_type= TL_READ; /* Assume read */
2448
1888
assert(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
2450
if (thd->lex->need_correct_ident())
1890
if (session->lex->need_correct_ident())
2451
1891
table->alias_name_used= my_strcasecmp(table_alias_charset,
2452
1892
table->s->table_name.str, alias);
2453
1893
/* Fix alias if table name changes */
2454
1894
if (strcmp(table->alias, alias))
2456
uint length=(uint) strlen(alias)+1;
2457
table->alias= (char*) my_realloc((char*) table->alias, length,
1896
uint32_t length=(uint32_t) strlen(alias)+1;
1897
table->alias= (char*) realloc((char*) table->alias, length);
2459
1898
memcpy((void*) table->alias, alias, length);
2461
1901
/* These variables are also set in reopen_table() */
2462
table->tablenr=thd->current_tablenr++;
2463
table->used_fields=0;
2464
table->const_table=0;
1902
table->tablenr=session->current_tablenr++;
1903
table->used_fields= 0;
1904
table->const_table= 0;
2465
1905
table->null_row= false;
2466
1906
table->maybe_null= false;
2467
1907
table->force_index= false;
2630
2072
for target table name if we process ALTER Table ... RENAME.
2631
2073
So loop below makes sense even if we are not under LOCK TABLES.
2633
for (table=thd->open_tables; table ; table=table->next)
2075
for (table= open_tables; table ; table=table->next)
2635
if (!strcmp(table->s->table_name.str, table_name) &&
2636
!strcmp(table->s->db.str, db))
2077
if (!strcmp(table->s->table_name.str, new_table_name) &&
2078
!strcmp(table->s->db.str, new_db))
2638
if (thd->locked_tables)
2640
mysql_lock_remove(thd, thd->locked_tables, table, true);
2082
mysql_lock_remove(this, locked_tables, table, true);
2642
table->open_placeholder= 1;
2084
table->open_placeholder= true;
2643
2085
close_handle_and_leave_table_as_lock(table);
2651
Reopen all tables with closed data files.
2653
@param thd Thread context
2654
@param get_locks Should we get locks after reopening tables ?
2655
@param mark_share_as_old Mark share as old to protect from a impending
2658
@note Since this function can't properly handle prelocking and
2659
create placeholders it should be used in very special
2660
situations like FLUSH TABLES or ALTER Table. In general
2661
case one should just repeat open_tables()/lock_tables()
2662
combination when one needs tables to be reopened (for
2663
example see open_and_lock_tables()).
2665
@note One should have lock on LOCK_open when calling this.
2667
@return false in case of success, true - otherwise.
2092
Reopen all tables with closed data files.
2094
@param session Thread context
2095
@param get_locks Should we get locks after reopening tables ?
2096
@param mark_share_as_old Mark share as old to protect from a impending
2099
@note Since this function can't properly handle prelocking and
2100
create placeholders it should be used in very special
2101
situations like FLUSH TABLES or ALTER Table. In general
2102
case one should just repeat open_tables()/lock_tables()
2103
combination when one needs tables to be reopened (for
2104
example see open_and_lock_tables()).
2106
@note One should have lock on LOCK_open when calling this.
2108
@return false in case of success, true - otherwise.
2670
bool reopen_tables(THD *thd, bool get_locks, bool mark_share_as_old)
2111
bool Session::reopen_tables(bool get_locks, bool mark_share_as_old)
2672
2113
Table *table,*next,**prev;
2673
2114
Table **tables,**tables_ptr; // For locks
2674
2115
bool error=0, not_used;
2675
const uint flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
2676
DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
2677
DRIZZLE_LOCK_IGNORE_FLUSH;
2116
const uint32_t flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
2117
DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
2118
DRIZZLE_LOCK_IGNORE_FLUSH;
2679
if (!thd->open_tables)
2120
if (open_tables == NULL)
2682
2123
safe_mutex_assert_owner(&LOCK_open);
2995
2439
other threads trying to get the lock.
2998
void abort_locked_tables(THD *thd,const char *db, const char *table_name)
2442
void abort_locked_tables(Session *session,const char *db, const char *table_name)
3001
for (table= thd->open_tables; table ; table= table->next)
2445
for (table= session->open_tables; table ; table= table->next)
3003
2447
if (!strcmp(table->s->table_name.str, table_name) &&
3004
!strcmp(table->s->db.str, db))
2448
!strcmp(table->s->db.str, db))
3006
2450
/* If MERGE child, forward lock handling to parent. */
3007
mysql_lock_abort(thd, table, true);
2451
mysql_lock_abort(session, table, true);
3015
Function to assign a new table map id to a table share.
3019
share - Pointer to table share structure
3023
We are intentionally not checking that share->mutex is locked
3024
since this function should only be called when opening a table
3025
share and before it is entered into the table_def_cache (meaning
3026
that it cannot be fetched by another thread, even accidentally).
3031
The LOCK_open mutex is locked
3035
share->table_map_id is given a value that with a high certainty is
3036
not used by any other table (the only case where a table id can be
3037
reused is on wrap-around, which means more than 4 billion table
3038
share opens have been executed while one table was open all the
3041
share->table_map_id is not UINT32_MAX.
3043
void assign_new_table_id(TABLE_SHARE *share)
3045
static uint32_t last_table_id= UINT32_MAX;
3048
assert(share != NULL);
3049
safe_mutex_assert_owner(&LOCK_open);
3051
ulong tid= ++last_table_id; /* get next id */
3053
There is one reserved number that cannot be used. Remember to
3054
change this when 6-byte global table id's are introduced.
3056
if (unlikely(tid == UINT32_MAX))
3057
tid= ++last_table_id;
3058
share->table_map_id= tid;
3060
/* Post conditions */
3061
assert(share->table_map_id != UINT32_MAX);
3067
2458
Load a table definition from file and open unireg table
3072
entry Store open table definition here
3073
table_list TableList with db, table_name
3075
cache_key Key for share_cache
3076
cache_key_length length of cache_key
2462
session Thread handle
2463
entry Store open table definition here
2464
table_list TableList with db, table_name
2466
cache_key Key for share_cache
2467
cache_key_length length of cache_key
3079
Extra argument for open is taken from thd->open_options
3080
One must have a lock on LOCK_open when calling this function
2470
Extra argument for open is taken from session->open_options
2471
One must have a lock on LOCK_open when calling this function
3087
static int open_unireg_entry(THD *thd, Table *entry, TableList *table_list,
2478
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
3088
2479
const char *alias,
3089
char *cache_key, uint cache_key_length)
2480
char *cache_key, uint32_t cache_key_length)
3093
uint discover_retry_count= 0;
2484
uint32_t discover_retry_count= 0;
3095
2486
safe_mutex_assert_owner(&LOCK_open);
3097
if (!(share= get_table_share_with_create(thd, table_list, cache_key,
2488
if (!(share= get_table_share_with_create(session, table_list, cache_key,
3099
2490
table_list->i_s_requested_object,
3103
while ((error= open_table_from_share(thd, share, alias,
3104
(uint) (HA_OPEN_KEYFILE |
2494
while ((error= open_table_from_share(session, share, alias,
2495
(uint32_t) (HA_OPEN_KEYFILE |
3108
2499
(EXTRA_RECORD),
3109
thd->open_options, entry, OTM_OPEN)))
2500
session->open_options, entry, OTM_OPEN)))
3111
2502
if (error == 7) // Table def changed
3138
2527
if (share->ref_count != 1)
3140
2529
/* Free share and wait until it's released by all threads */
3141
release_table_share(share, RELEASE_WAIT_FOR_DROP);
2530
release_table_share(share);
2531
if (!session->killed)
3144
drizzle_reset_errors(thd, 1); // Clear warnings
3145
thd->clear_error(); // Clear error message
2533
drizzle_reset_errors(session, 1); // Clear warnings
2534
session->clear_error(); // Clear error message
3150
2539
if (!entry->s || !entry->s->crashed)
3152
// Code below is for repairing a crashed file
3153
if ((error= lock_table_name(thd, table_list, true)))
3157
if (wait_for_locked_table_names(thd, table_list))
3159
unlock_table_name(thd, table_list);
3163
pthread_mutex_unlock(&LOCK_open);
3164
thd->clear_error(); // Clear error message
3166
if (open_table_from_share(thd, share, alias,
3167
(uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3171
ha_open_options | HA_OPEN_FOR_REPAIR,
3172
entry, OTM_OPEN) || ! entry->file ||
3173
(entry->file->is_crashed() && entry->file->ha_check_and_repair(thd)))
3175
/* Give right error message */
3177
my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
3178
sql_print_error(_("Couldn't repair table: %s.%s"), share->db.str,
3179
share->table_name.str);
3185
thd->clear_error(); // Clear error message
3186
pthread_mutex_lock(&LOCK_open);
3187
unlock_table_name(thd, table_list);
2541
// Code below is for repairing a crashed file
2542
if ((error= lock_table_name(session, table_list, true)))
2546
if (wait_for_locked_table_names(session, table_list))
2548
unlock_table_name(table_list);
2552
pthread_mutex_unlock(&LOCK_open);
2553
session->clear_error(); // Clear error message
2555
if (open_table_from_share(session, share, alias,
2556
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
2560
ha_open_options | HA_OPEN_FOR_REPAIR,
2561
entry, OTM_OPEN) || ! entry->file ||
2562
(entry->file->is_crashed() && entry->file->ha_check_and_repair(session)))
2564
/* Give right error message */
2565
session->clear_error();
2566
my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
2567
errmsg_printf(ERRMSG_LVL_ERROR, _("Couldn't repair table: %s.%s"), share->db.str,
2568
share->table_name.str);
2570
entry->closefrm(false);
2574
session->clear_error(); // Clear error message
2575
pthread_mutex_lock(&LOCK_open);
2576
unlock_table_name(table_list);
3195
2584
If we are here, there was no fatal error (but error may be still
3198
2587
if (unlikely(entry->file->implicit_emptied))
3200
2589
entry->file->implicit_emptied= 0;
3201
if (mysql_bin_log.is_open())
3203
2591
char *query, *end;
3204
uint query_buf_size= 20 + share->db.length + share->table_name.length +1;
3205
if ((query= (char*) my_malloc(query_buf_size,MYF(MY_WME))))
2592
uint32_t query_buf_size= 20 + share->db.length + share->table_name.length +1;
2593
if ((query= (char*) malloc(query_buf_size)))
3207
/* this DELETE FROM is needed even with row-based binlogging */
3208
end = strxmov(stpcpy(query, "DELETE FROM `"),
3209
share->db.str,"`.`",share->table_name.str,"`", NullS);
3210
thd->binlog_query(THD::STMT_QUERY_TYPE,
3211
query, (ulong)(end-query), false, false);
3212
my_free(query, MYF(0));
2596
"this DELETE FROM is needed even with row-based binlogging"
2598
We inherited this from MySQL. TODO: fix it to issue a propper truncate
2599
of the table (though that may not be completely right sematics).
2602
end+= sprintf(query, "DELETE FROM `%s`.`%s`", share->db.str,
2603
share->table_name.str);
2604
transaction_services.rawStatement(session, query, (size_t)(end - query));
3217
As replication is maybe going to be corrupted, we need to warn the
3218
DBA on top of warning the client (which will automatically be done
3219
because of MYF(MY_WME) in my_malloc() above).
3221
sql_print_error(_("When opening HEAP table, could not allocate memory "
3222
"to write 'DELETE FROM `%s`.`%s`' to the binary log"),
3223
table_list->db, table_list->table_name);
2609
errmsg_printf(ERRMSG_LVL_ERROR, _("When opening HEAP table, could not allocate memory "
2610
"to write 'DELETE FROM `%s`.`%s`' to replication"),
2611
table_list->db, table_list->table_name);
2612
my_error(ER_OUTOFMEMORY, MYF(0), query_buf_size);
2613
entry->closefrm(false);
3232
release_table_share(share, RELEASE_NORMAL);
2621
release_table_share(share);
3527
2861
Open all tables in list, locks them and optionally process derived tables.
3530
open_and_lock_tables_derived()
3531
thd - thread handler
3532
tables - list of tables for open&locking
3533
derived - if to handle derived tables
2864
open_and_lock_tables_derived()
2865
session - thread handler
2866
tables - list of tables for open&locking
2867
derived - if to handle derived tables
3540
The lock will automaticaly be freed by close_thread_tables()
3543
There are two convenience functions:
3544
- simple_open_n_lock_tables(thd, tables) without derived handling
3545
- open_and_lock_tables(thd, tables) with derived handling
3546
Both inline functions call open_and_lock_tables_derived() with
3547
the third argument set appropriately.
2874
The lock will automaticaly be freed by close_thread_tables()
2877
There are two convenience functions:
2878
- simple_open_n_lock_tables(session, tables) without derived handling
2879
- open_and_lock_tables(session, tables) with derived handling
2880
Both inline functions call open_and_lock_tables_derived() with
2881
the third argument set appropriately.
3550
int open_and_lock_tables_derived(THD *thd, TableList *tables, bool derived)
2884
int open_and_lock_tables_derived(Session *session, TableList *tables, bool derived)
3553
2887
bool need_reopen;
3557
if (open_tables(thd, &tables, &counter, 0))
2891
if (open_tables(session, &tables, &counter, 0))
3560
if (!lock_tables(thd, tables, counter, &need_reopen))
2894
if (!lock_tables(session, tables, counter, &need_reopen))
3562
2896
if (!need_reopen)
3564
close_tables_for_reopen(thd, &tables);
2898
session->close_tables_for_reopen(&tables);
3567
(mysql_handle_derived(thd->lex, &mysql_derived_prepare) ||
3568
(thd->fill_derived_tables() &&
3569
mysql_handle_derived(thd->lex, &mysql_derived_filling))))
3570
return(true); /* purecov: inspected */
2901
(mysql_handle_derived(session->lex, &mysql_derived_prepare) ||
2902
(session->fill_derived_tables() &&
2903
mysql_handle_derived(session->lex, &mysql_derived_filling))))
2904
return true; /* purecov: inspected */
3576
2910
Open all tables in list and process derived tables
3579
open_normal_and_derived_tables
3580
thd - thread handler
3581
tables - list of tables for open
3582
flags - bitmap of flags to modify how the tables will be open:
3583
DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
3584
done a flush or namelock on it.
2913
open_normal_and_derived_tables
2914
session - thread handler
2915
tables - list of tables for open
2916
flags - bitmap of flags to modify how the tables will be open:
2917
DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
2918
done a flush or namelock on it.
3591
This is to be used on prepare stage when you don't read any
3592
data from the tables.
2925
This is to be used on prepare stage when you don't read any
2926
data from the tables.
3595
bool open_normal_and_derived_tables(THD *thd, TableList *tables, uint flags)
3598
assert(!thd->fill_derived_tables());
3599
if (open_tables(thd, &tables, &counter, flags) ||
3600
mysql_handle_derived(thd->lex, &mysql_derived_prepare))
3601
return(true); /* purecov: inspected */
3607
Decide on logging format to use for the statement.
3609
Compute the capabilities vector for the involved storage engines
3610
and mask out the flags for the binary log. Right now, the binlog
3611
flags only include the capabilities of the storage engines, so this
3614
We now have three alternatives that prevent the statement from
3617
1. If there are no capabilities left (all flags are clear) it is
3618
not possible to log the statement at all, so we roll back the
3619
statement and report an error.
3621
2. Statement mode is set, but the capabilities indicate that
3622
statement format is not possible.
3624
3. Row mode is set, but the capabilities indicate that row
3625
format is not possible.
3627
4. Statement is unsafe, but the capabilities indicate that row
3628
format is not possible.
3630
If we are in MIXED mode, we then decide what logging format to use:
3632
1. If the statement is unsafe, row-based logging is used.
3634
2. If statement-based logging is not possible, row-based logging is
3637
3. Otherwise, statement-based logging is used.
3639
@param thd Client thread
3640
@param tables Tables involved in the query
3643
int decide_logging_format(THD *thd, TableList *tables)
3645
if (mysql_bin_log.is_open() && (thd->options & OPTION_BIN_LOG))
3647
handler::Table_flags flags_some_set= handler::Table_flags();
3648
handler::Table_flags flags_all_set= ~handler::Table_flags();
3649
bool multi_engine= false;
3650
void* prev_ht= NULL;
3651
for (TableList *table= tables; table; table= table->next_global)
3653
if (!table->placeholder() && table->lock_type >= TL_WRITE_ALLOW_WRITE)
3655
uint64_t const flags= table->table->file->ha_table_flags();
3656
if (prev_ht && prev_ht != table->table->file->ht)
3658
prev_ht= table->table->file->ht;
3659
flags_all_set &= flags;
3660
flags_some_set |= flags;
3665
if (flags_all_set == 0)
3667
my_error((error= ER_BINLOG_LOGGING_IMPOSSIBLE), MYF(0),
3668
"Statement cannot be logged to the binary log in"
3669
" row-based nor statement-based format");
3671
else if (thd->variables.binlog_format == BINLOG_FORMAT_STMT &&
3672
(flags_all_set & HA_BINLOG_STMT_CAPABLE) == 0)
3674
my_error((error= ER_BINLOG_LOGGING_IMPOSSIBLE), MYF(0),
3675
"Statement-based format required for this statement,"
3676
" but not allowed by this combination of engines");
3678
else if ((thd->variables.binlog_format == BINLOG_FORMAT_ROW ||
3679
thd->lex->is_stmt_unsafe()) &&
3680
(flags_all_set & HA_BINLOG_ROW_CAPABLE) == 0)
3682
my_error((error= ER_BINLOG_LOGGING_IMPOSSIBLE), MYF(0),
3683
"Row-based format required for this statement,"
3684
" but not allowed by this combination of engines");
3688
If more than one engine is involved in the statement and at
3689
least one is doing it's own logging (is *self-logging*), the
3690
statement cannot be logged atomically, so we generate an error
3691
rather than allowing the binlog to become corrupt.
3694
(flags_some_set & HA_HAS_OWN_BINLOGGING))
3696
error= ER_BINLOG_LOGGING_IMPOSSIBLE;
3697
my_error(error, MYF(0),
3698
"Statement cannot be written atomically since more"
3699
" than one engine involved and at least one engine"
3700
" is self-logging");
3707
We switch to row-based format if we are in mixed mode and one of
3708
the following are true:
3710
1. If the statement is unsafe
3711
2. If statement format cannot be used
3713
Observe that point to cannot be decided before the tables
3714
involved in a statement has been checked, i.e., we cannot put
3715
this code in reset_current_stmt_binlog_row_based(), it has to be
3718
if (thd->lex->is_stmt_unsafe() ||
3719
(flags_all_set & HA_BINLOG_STMT_CAPABLE) == 0)
3721
thd->set_current_stmt_binlog_row_based_if_mixed();
2929
bool open_normal_and_derived_tables(Session *session, TableList *tables, uint32_t flags)
2932
assert(!session->fill_derived_tables());
2933
if (open_tables(session, &tables, &counter, flags) ||
2934
mysql_handle_derived(session->lex, &mysql_derived_prepare))
2935
return true; /* purecov: inspected */
3843
3054
Open a single table without table caching and don't set it in open_list
3846
open_temporary_table()
3848
path Path (without .frm)
3850
table_name Table name
3851
link_in_list 1 if table should be linked into thd->temporary_tables
3854
Used by alter_table to open a temporary table and when creating
3855
a temporary table with CREATE TEMPORARY ...
3057
open_temporary_table()
3058
session Thread object
3059
path Path (without .frm)
3061
table_name Table name
3062
link_in_list 1 if table should be linked into session->temporary_tables
3065
Used by alter_table to open a temporary table and when creating
3066
a temporary table with CREATE TEMPORARY ...
3862
Table *open_temporary_table(THD *thd, const char *path, const char *db,
3863
const char *table_name, bool link_in_list,
3073
Table *open_temporary_table(Session *session, const char *path, const char *db,
3074
const char *table_name, bool link_in_list,
3864
3075
open_table_mode open_mode)
3866
3077
Table *tmp_table;
3868
3079
char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
3080
uint32_t key_length, path_length;
3870
3081
TableList table_list;
3872
3083
table_list.db= (char*) db;
3873
3084
table_list.table_name= (char*) table_name;
3874
3085
/* Create the cache_key for temporary tables */
3875
key_length= create_table_def_key(thd, cache_key, &table_list, 1);
3877
if (!(tmp_table= (Table*) my_malloc(sizeof(*tmp_table) + sizeof(*share) +
3878
strlen(path)+1 + key_length,
3880
return(0); /* purecov: inspected */
3882
share= (TABLE_SHARE*) (tmp_table+1);
3086
key_length= create_table_def_key(cache_key, &table_list);
3087
path_length= strlen(path);
3089
if (!(tmp_table= (Table*) malloc(sizeof(*tmp_table) + sizeof(*share) +
3090
path_length + 1 + key_length)))
3093
share= (TableShare*) (tmp_table+1);
3883
3094
tmp_path= (char*) (share+1);
3884
saved_cache_key= stpcpy(tmp_path, path)+1;
3095
saved_cache_key= strcpy(tmp_path, path)+path_length+1;
3885
3096
memcpy(saved_cache_key, cache_key, key_length);
3887
init_tmp_table_share(thd, share, saved_cache_key, key_length,
3888
strchr(saved_cache_key, '\0')+1, tmp_path);
3098
share->init(saved_cache_key, key_length, strchr(saved_cache_key, '\0')+1, tmp_path);
3890
if (open_table_def(thd, share, 0) ||
3891
open_table_from_share(thd, share, table_name,
3101
First open the share, and then open the table from the share we just opened.
3103
if (open_table_def(session, share) ||
3104
open_table_from_share(session, share, table_name,
3892
3105
(open_mode == OTM_ALTER) ? 0 :
3893
(uint) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3106
(uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3895
3108
(open_mode == OTM_ALTER) ?
3896
(EXTRA_RECORD | OPEN_FRM_FILE_ONLY)
3109
(EXTRA_RECORD | OPEN_FRM_FILE_ONLY)
3897
3110
: (EXTRA_RECORD),
3898
3111
ha_open_options,
3899
3112
tmp_table, open_mode))
3901
3114
/* No need to lock share->mutex as this is not needed for tmp tables */
3902
free_table_share(share);
3903
my_free((char*) tmp_table,MYF(0));
3115
share->free_table_share();
3116
free((char*) tmp_table);
3907
3120
tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
3908
3121
if (open_mode == OTM_ALTER)
3911
Temporary table has been created with frm_only
3912
and has not been created in any storage engine
3124
Temporary table has been created with frm_only
3125
and has not been created in any storage engine
3914
3127
share->tmp_table= TMP_TABLE_FRM_FILE_ONLY;
4015
3224
Find field by name in a NATURAL/USING join table reference.
4018
find_field_in_natural_join()
4019
thd [in] thread handler
4020
table_ref [in] table reference to search
4021
name [in] name of field
4022
length [in] length of name
4023
ref [in/out] if 'name' is resolved to a view field, ref is
4024
set to point to the found view field
4025
register_tree_change [in] true if ref is not stack variable and we
4026
need register changes in item tree
4027
actual_table [out] the original table reference where the field
4028
belongs - differs from 'table_list' only for
3227
find_field_in_natural_join()
3228
session [in] thread handler
3229
table_ref [in] table reference to search
3230
name [in] name of field
3231
length [in] length of name
3232
ref [in/out] if 'name' is resolved to a view field, ref is
3233
set to point to the found view field
3234
register_tree_change [in] true if ref is not stack variable and we
3235
need register changes in item tree
3236
actual_table [out] the original table reference where the field
3237
belongs - differs from 'table_list' only for
4032
Search for a field among the result fields of a NATURAL/USING join.
4033
Notice that this procedure is called only for non-qualified field
4034
names. In the case of qualified fields, we search directly the base
4035
tables of a natural join.
3241
Search for a field among the result fields of a NATURAL/USING join.
3242
Notice that this procedure is called only for non-qualified field
3243
names. In the case of qualified fields, we search directly the base
3244
tables of a natural join.
4038
NULL if the field was not found
4039
WRONG_GRANT if no access rights to the found field
4040
# Pointer to the found Field
3247
NULL if the field was not found
3248
WRONG_GRANT if no access rights to the found field
3249
# Pointer to the found Field
4044
find_field_in_natural_join(THD *thd, TableList *table_ref, const char *name,
4045
uint length __attribute__((unused)),
4046
Item **ref __attribute__((unused)), bool register_tree_change __attribute__((unused)),
4047
TableList **actual_table)
3253
find_field_in_natural_join(Session *session, TableList *table_ref,
3254
const char *name, uint32_t , Item **,
3255
bool, TableList **actual_table)
4049
3257
List_iterator_fast<Natural_join_column>
4050
3258
field_it(*(table_ref->join_columns));
4158
3366
Find field in a table reference.
4161
find_field_in_table_ref()
4162
thd [in] thread handler
4163
table_list [in] table reference to search
4164
name [in] name of field
4165
length [in] field length of name
4166
item_name [in] name of item if it will be created (VIEW)
4167
db_name [in] optional database name that qualifies the
4168
table_name [in] optional table name that qualifies the field
4169
ref [in/out] if 'name' is resolved to a view field, ref
4170
is set to point to the found view field
4171
check_privileges [in] check privileges
4172
allow_rowid [in] do allow finding of "_rowid" field?
4173
cached_field_index_ptr [in] cached position in field list (used to
4174
speedup lookup for fields in prepared tables)
4175
register_tree_change [in] true if ref is not stack variable and we
4176
need register changes in item tree
4177
actual_table [out] the original table reference where the field
4178
belongs - differs from 'table_list' only for
4179
NATURAL_USING joins.
3369
find_field_in_table_ref()
3370
session [in] thread handler
3371
table_list [in] table reference to search
3372
name [in] name of field
3373
length [in] field length of name
3374
item_name [in] name of item if it will be created (VIEW)
3375
db_name [in] optional database name that qualifies the
3376
table_name [in] optional table name that qualifies the field
3377
ref [in/out] if 'name' is resolved to a view field, ref
3378
is set to point to the found view field
3379
check_privileges [in] check privileges
3380
allow_rowid [in] do allow finding of "_rowid" field?
3381
cached_field_index_ptr [in] cached position in field list (used to
3382
speedup lookup for fields in prepared tables)
3383
register_tree_change [in] true if ref is not stack variable and we
3384
need register changes in item tree
3385
actual_table [out] the original table reference where the field
3386
belongs - differs from 'table_list' only for
3387
NATURAL_USING joins.
4182
Find a field in a table reference depending on the type of table
4183
reference. There are three types of table references with respect
4184
to the representation of their result columns:
4185
- an array of Field_translator objects for MERGE views and some
4186
information_schema tables,
4187
- an array of Field objects (and possibly a name hash) for stored
4189
- a list of Natural_join_column objects for NATURAL/USING joins.
4190
This procedure detects the type of the table reference 'table_list'
4191
and calls the corresponding search routine.
3390
Find a field in a table reference depending on the type of table
3391
reference. There are three types of table references with respect
3392
to the representation of their result columns:
3393
- an array of Field_translator objects for MERGE views and some
3394
information_schema tables,
3395
- an array of Field objects (and possibly a name hash) for stored
3397
- a list of Natural_join_column objects for NATURAL/USING joins.
3398
This procedure detects the type of the table reference 'table_list'
3399
and calls the corresponding search routine.
4194
0 field is not found
4195
view_ref_found found value in VIEW (real result is in *ref)
3402
0 field is not found
3403
view_ref_found found value in VIEW (real result is in *ref)
4200
find_field_in_table_ref(THD *thd, TableList *table_list,
4201
const char *name, uint length,
3408
find_field_in_table_ref(Session *session, TableList *table_list,
3409
const char *name, uint32_t length,
4202
3410
const char *item_name, const char *db_name,
4203
3411
const char *table_name, Item **ref,
4204
3412
bool check_privileges, bool allow_rowid,
4205
uint *cached_field_index_ptr,
3413
uint32_t *cached_field_index_ptr,
4206
3414
bool register_tree_change, TableList **actual_table)
4208
3416
Field *fld= NULL;
4286
3490
natural join, thus if the field is not qualified, we will search
4287
3491
directly the top-most NATURAL/USING join.
4289
fld= find_field_in_natural_join(thd, table_list, name, length, ref,
3493
fld= find_field_in_natural_join(session, table_list, name, length, ref,
4290
3494
register_tree_change, actual_table);
4295
if (thd->mark_used_columns != MARK_COLUMNS_NONE)
4298
Get rw_set correct for this field so that the handler
4299
knows that this field is involved in the query and gets
4302
Field *field_to_set= NULL;
4303
if (fld == view_ref_found)
4305
Item *it= (*ref)->real_item();
4306
if (it->type() == Item::FIELD_ITEM)
4307
field_to_set= ((Item_field*)it)->field;
4310
if (thd->mark_used_columns == MARK_COLUMNS_READ)
4311
it->walk(&Item::register_field_in_read_map, 1, (uchar *) 0);
4318
Table *table= field_to_set->table;
4319
if (thd->mark_used_columns == MARK_COLUMNS_READ)
4320
bitmap_set_bit(table->read_set, field_to_set->field_index);
4322
bitmap_set_bit(table->write_set, field_to_set->field_index);
3499
if (session->mark_used_columns != MARK_COLUMNS_NONE)
3502
Get rw_set correct for this field so that the handler
3503
knows that this field is involved in the query and gets
3506
Field *field_to_set= NULL;
3507
if (fld == view_ref_found)
3509
Item *it= (*ref)->real_item();
3510
if (it->type() == Item::FIELD_ITEM)
3511
field_to_set= ((Item_field*)it)->field;
3514
if (session->mark_used_columns == MARK_COLUMNS_READ)
3515
it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
3522
Table *table= field_to_set->table;
3523
if (session->mark_used_columns == MARK_COLUMNS_READ)
3524
table->setReadSet(field_to_set->field_index);
3526
table->setWriteSet(field_to_set->field_index);
4377
3581
Find field in table list.
4380
find_field_in_tables()
4381
thd pointer to current thread structure
4382
item field item that should be found
4383
first_table list of tables to be searched for item
4384
last_table end of the list of tables to search for item. If NULL
4385
then search to the end of the list 'first_table'.
4386
ref if 'item' is resolved to a view field, ref is set to
4387
point to the found view field
4388
report_error Degree of error reporting:
4389
- IGNORE_ERRORS then do not report any error
4390
- IGNORE_EXCEPT_NON_UNIQUE report only non-unique
4391
fields, suppress all other errors
4392
- REPORT_EXCEPT_NON_UNIQUE report all other errors
4393
except when non-unique fields were found
4395
check_privileges need to check privileges
4396
register_tree_change true if ref is not a stack variable and we
4397
to need register changes in item tree
3584
find_field_in_tables()
3585
session pointer to current thread structure
3586
item field item that should be found
3587
first_table list of tables to be searched for item
3588
last_table end of the list of tables to search for item. If NULL
3589
then search to the end of the list 'first_table'.
3590
ref if 'item' is resolved to a view field, ref is set to
3591
point to the found view field
3592
report_error Degree of error reporting:
3593
- IGNORE_ERRORS then do not report any error
3594
- IGNORE_EXCEPT_NON_UNIQUE report only non-unique
3595
fields, suppress all other errors
3596
- REPORT_EXCEPT_NON_UNIQUE report all other errors
3597
except when non-unique fields were found
3599
check_privileges need to check privileges
3600
register_tree_change true if ref is not a stack variable and we
3601
to need register changes in item tree
4400
0 If error: the found field is not unique, or there are
4401
no sufficient access priviliges for the found field,
4402
or the field is qualified with non-existing table.
4403
not_found_field The function was called with report_error ==
4404
(IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
4405
field was not found.
4406
view_ref_found View field is found, item passed through ref parameter
4407
found field If a item was resolved to some field
3604
0 If error: the found field is not unique, or there are
3605
no sufficient access priviliges for the found field,
3606
or the field is qualified with non-existing table.
3607
not_found_field The function was called with report_error ==
3608
(IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
3609
field was not found.
3610
view_ref_found View field is found, item passed through ref parameter
3611
found field If a item was resolved to some field
4411
find_field_in_tables(THD *thd, Item_ident *item,
3615
find_field_in_tables(Session *session, Item_ident *item,
4412
3616
TableList *first_table, TableList *last_table,
4413
Item **ref, find_item_error_report_type report_error,
3617
Item **ref, find_item_error_report_type report_error,
4414
3618
bool check_privileges, bool register_tree_change)
4416
3620
Field *found=0;
4417
3621
const char *db= item->db_name;
4418
3622
const char *table_name= item->table_name;
4419
3623
const char *name= item->field_name;
4420
uint length=(uint) strlen(name);
3624
uint32_t length=(uint32_t) strlen(name);
4421
3625
char name_buff[NAME_LEN+1];
4422
3626
TableList *cur_table= first_table;
4423
3627
TableList *actual_table;
4598
3807
Find Item in list of items (find_field_in_tables analog)
4601
is it better return only counter?
3810
is it better return only counter?
4607
counter To return number of found item
4609
REPORT_ALL_ERRORS report errors, return 0 if error
4610
REPORT_EXCEPT_NOT_FOUND Do not report 'not found' error and
4611
return not_found_item, report other errors,
4613
IGNORE_ERRORS Do not report errors, return 0 if error
4614
resolution Set to the resolution type if the item is found
4615
(it says whether the item is resolved
4616
against an alias name,
4617
or as a field name without alias,
4618
or as a field hidden by alias,
3816
counter To return number of found item
3818
REPORT_ALL_ERRORS report errors, return 0 if error
3819
REPORT_EXCEPT_NOT_FOUND Do not report 'not found' error and
3820
return not_found_item, report other errors,
3822
IGNORE_ERRORS Do not report errors, return 0 if error
3823
resolution Set to the resolution type if the item is found
3824
(it says whether the item is resolved
3825
against an alias name,
3826
or as a field name without alias,
3827
or as a field hidden by alias,
4622
0 Item is not found or item is not unique,
4623
error message is reported
4624
not_found_item Function was called with
4625
report_error == REPORT_EXCEPT_NOT_FOUND and
4626
item was not found. No error message was reported
3831
0 Item is not found or item is not unique,
3832
error message is reported
3833
not_found_item Function was called with
3834
report_error == REPORT_EXCEPT_NOT_FOUND and
3835
item was not found. No error message was reported
4630
3839
/* Special Item pointer to serve as a return value from find_item_in_list(). */
4919
4099
Find and mark the common columns of two table references.
4922
mark_common_columns()
4923
thd [in] current thread
4924
table_ref_1 [in] the first (left) join operand
4925
table_ref_2 [in] the second (right) join operand
4926
using_fields [in] if the join is JOIN...USING - the join columns,
4927
if NATURAL join, then NULL
4928
found_using_fields [out] number of fields from the USING clause that were
4929
found among the common fields
4102
mark_common_columns()
4103
session [in] current thread
4104
table_ref_1 [in] the first (left) join operand
4105
table_ref_2 [in] the second (right) join operand
4106
using_fields [in] if the join is JOIN...USING - the join columns,
4107
if NATURAL join, then NULL
4108
found_using_fields [out] number of fields from the USING clause that were
4109
found among the common fields
4932
The procedure finds the common columns of two relations (either
4933
tables or intermediate join results), and adds an equi-join condition
4934
to the ON clause of 'table_ref_2' for each pair of matching columns.
4935
If some of table_ref_XXX represents a base table or view, then we
4936
create new 'Natural_join_column' instances for each column
4937
reference and store them in the 'join_columns' of the table
4112
The procedure finds the common columns of two relations (either
4113
tables or intermediate join results), and adds an equi-join condition
4114
to the ON clause of 'table_ref_2' for each pair of matching columns.
4115
If some of table_ref_XXX represents a base table or view, then we
4116
create new 'Natural_join_column' instances for each column
4117
reference and store them in the 'join_columns' of the table
4941
The procedure assumes that store_natural_using_join_columns() was
4942
called for the previous level of NATURAL/USING joins.
4121
The procedure assumes that store_natural_using_join_columns() was
4122
called for the previous level of NATURAL/USING joins.
4945
true error when some common column is non-unique, or out of memory
4125
true error when some common column is non-unique, or out of memory
4950
mark_common_columns(THD *thd, TableList *table_ref_1, TableList *table_ref_2,
4951
List<String> *using_fields, uint *found_using_fields)
4130
mark_common_columns(Session *session, TableList *table_ref_1, TableList *table_ref_2,
4131
List<String> *using_fields, uint32_t *found_using_fields)
4953
4133
Field_iterator_table_ref it_1, it_2;
4954
4134
Natural_join_column *nj_col_1, *nj_col_2;
5129
4309
Materialize and store the row type of NATURAL/USING join.
5132
store_natural_using_join_columns()
5134
natural_using_join the table reference of the NATURAL/USING join
5135
table_ref_1 the first (left) operand (of a NATURAL/USING join).
5136
table_ref_2 the second (right) operand (of a NATURAL/USING join).
5137
using_fields if the join is JOIN...USING - the join columns,
5138
if NATURAL join, then NULL
5139
found_using_fields number of fields from the USING clause that were
5140
found among the common fields
4312
store_natural_using_join_columns()
4313
session current thread
4314
natural_using_join the table reference of the NATURAL/USING join
4315
table_ref_1 the first (left) operand (of a NATURAL/USING join).
4316
table_ref_2 the second (right) operand (of a NATURAL/USING join).
4317
using_fields if the join is JOIN...USING - the join columns,
4318
if NATURAL join, then NULL
4319
found_using_fields number of fields from the USING clause that were
4320
found among the common fields
5143
Iterate over the columns of both join operands and sort and store
5144
all columns into the 'join_columns' list of natural_using_join
5145
where the list is formed by three parts:
5146
part1: The coalesced columns of table_ref_1 and table_ref_2,
5147
sorted according to the column order of the first table.
5148
part2: The other columns of the first table, in the order in
5149
which they were defined in CREATE TABLE.
5150
part3: The other columns of the second table, in the order in
5151
which they were defined in CREATE TABLE.
5152
Time complexity - O(N1+N2), where Ni = length(table_ref_i).
5155
The procedure assumes that mark_common_columns() has been called
5156
for the join that is being processed.
5159
true error: Some common column is ambiguous
4323
Iterate over the columns of both join operands and sort and store
4324
all columns into the 'join_columns' list of natural_using_join
4325
where the list is formed by three parts:
4326
part1: The coalesced columns of table_ref_1 and table_ref_2,
4327
sorted according to the column order of the first table.
4328
part2: The other columns of the first table, in the order in
4329
which they were defined in CREATE TABLE.
4330
part3: The other columns of the second table, in the order in
4331
which they were defined in CREATE TABLE.
4332
Time complexity - O(N1+N2), where Ni = length(table_ref_i).
4335
The procedure assumes that mark_common_columns() has been called
4336
for the join that is being processed.
4339
true error: Some common column is ambiguous
5164
store_natural_using_join_columns(THD *thd __attribute__((unused)),
4344
store_natural_using_join_columns(Session *,
5165
4345
TableList *natural_using_join,
5166
4346
TableList *table_ref_1,
5167
4347
TableList *table_ref_2,
5168
4348
List<String> *using_fields,
5169
uint found_using_fields)
4349
uint32_t found_using_fields)
5171
4351
Field_iterator_table_ref it_1, it_2;
5172
4352
Natural_join_column *nj_col_1, *nj_col_2;
5253
4433
Precompute and store the row types of the top-most NATURAL/USING joins.
5256
store_top_level_join_columns()
5258
table_ref nested join or table in a FROM clause
5259
left_neighbor neighbor table reference to the left of table_ref at the
5260
same level in the join tree
5261
right_neighbor neighbor table reference to the right of table_ref at the
5262
same level in the join tree
4436
store_top_level_join_columns()
4437
session current thread
4438
table_ref nested join or table in a FROM clause
4439
left_neighbor neighbor table reference to the left of table_ref at the
4440
same level in the join tree
4441
right_neighbor neighbor table reference to the right of table_ref at the
4442
same level in the join tree
5265
The procedure performs a post-order traversal of a nested join tree
5266
and materializes the row types of NATURAL/USING joins in a
5267
bottom-up manner until it reaches the TableList elements that
5268
represent the top-most NATURAL/USING joins. The procedure should be
5269
applied to each element of SELECT_LEX::top_join_list (i.e. to each
5270
top-level element of the FROM clause).
4445
The procedure performs a post-order traversal of a nested join tree
4446
and materializes the row types of NATURAL/USING joins in a
4447
bottom-up manner until it reaches the TableList elements that
4448
represent the top-most NATURAL/USING joins. The procedure should be
4449
applied to each element of Select_Lex::top_join_list (i.e. to each
4450
top-level element of the FROM clause).
5273
Notice that the table references in the list nested_join->join_list
5274
are in reverse order, thus when we iterate over it, we are moving
5275
from the right to the left in the FROM clause.
4453
Notice that the table references in the list nested_join->join_list
4454
are in reverse order, thus when we iterate over it, we are moving
4455
from the right to the left in the FROM clause.
5283
store_top_level_join_columns(THD *thd, TableList *table_ref,
4463
store_top_level_join_columns(Session *session, TableList *table_ref,
5284
4464
TableList *left_neighbor,
5285
4465
TableList *right_neighbor)
5524
4698
it.replace(new Item_int("Not_used", (int64_t) 1,
5525
4699
MY_INT64_NUM_DECIMAL_DIGITS));
5527
else if (insert_fields(thd, ((Item_field*) item)->context,
4701
else if (insert_fields(session, ((Item_field*) item)->context,
5528
4702
((Item_field*) item)->db_name,
5529
4703
((Item_field*) item)->table_name, &it,
5530
4704
any_privileges))
5534
4708
if (sum_func_list)
5537
sum_func_list is a list that has the fields list as a tail.
5538
Because of this we have to update the element count also for this
5539
list after expanding the '*' entry.
5541
sum_func_list->elements+= fields.elements - elem;
4711
sum_func_list is a list that has the fields list as a tail.
4712
Because of this we have to update the element count also for this
4713
list after expanding the '*' entry.
4715
sum_func_list->elements+= fields.elements - elem;
5546
thd->lex->current_select->cur_pos_in_select_list++;
4720
session->lex->current_select->cur_pos_in_select_list++;
5548
thd->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
4722
session->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
5552
4726
/****************************************************************************
5553
** Check that all given fields exists and fill struct with current data
5554
****************************************************************************/
4727
** Check that all given fields exists and fill struct with current data
4728
****************************************************************************/
5556
bool setup_fields(THD *thd, Item **ref_pointer_array,
4730
bool setup_fields(Session *session, Item **ref_pointer_array,
5557
4731
List<Item> &fields, enum_mark_columns mark_used_columns,
5558
4732
List<Item> *sum_func_list, bool allow_sum_func)
5560
4734
register Item *item;
5561
enum_mark_columns save_mark_used_columns= thd->mark_used_columns;
5562
nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
4735
enum_mark_columns save_mark_used_columns= session->mark_used_columns;
4736
nesting_map save_allow_sum_func= session->lex->allow_sum_func;
5563
4737
List_iterator<Item> it(fields);
5564
4738
bool save_is_item_list_lookup;
5566
thd->mark_used_columns= mark_used_columns;
4740
session->mark_used_columns= mark_used_columns;
5567
4741
if (allow_sum_func)
5568
thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
5569
thd->where= THD::DEFAULT_WHERE;
5570
save_is_item_list_lookup= thd->lex->current_select->is_item_list_lookup;
5571
thd->lex->current_select->is_item_list_lookup= 0;
4742
session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
4743
session->where= Session::DEFAULT_WHERE;
4744
save_is_item_list_lookup= session->lex->current_select->is_item_list_lookup;
4745
session->lex->current_select->is_item_list_lookup= 0;
5574
4748
To prevent fail on forward lookup we fill it with zerows,
5578
4752
There is other way to solve problem: fill array with pointers to list,
5579
4753
but it will be slower.
5581
TODO: remove it when (if) we made one list for allfields and
4755
TODO: remove it when (if) we made one list for allfields and
5584
4758
if (ref_pointer_array)
5585
4759
memset(ref_pointer_array, 0, sizeof(Item *) * fields.elements);
5587
4761
Item **ref= ref_pointer_array;
5588
thd->lex->current_select->cur_pos_in_select_list= 0;
4762
session->lex->current_select->cur_pos_in_select_list= 0;
5589
4763
while ((item= it++))
5591
if ((!item->fixed && item->fix_fields(thd, it.ref())) || (item= *(it.ref()))->check_cols(1))
4765
if ((!item->fixed && item->fix_fields(session, it.ref())) || (item= *(it.ref()))->check_cols(1))
5593
thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
5594
thd->lex->allow_sum_func= save_allow_sum_func;
5595
thd->mark_used_columns= save_mark_used_columns;
5596
return(true); /* purecov: inspected */
4767
session->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
4768
session->lex->allow_sum_func= save_allow_sum_func;
4769
session->mark_used_columns= save_mark_used_columns;
4770
return true; /* purecov: inspected */
5599
4773
*(ref++)= item;
5600
4774
if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM &&
5602
item->split_sum_func(thd, ref_pointer_array, *sum_func_list);
5603
thd->used_tables|= item->used_tables();
5604
thd->lex->current_select->cur_pos_in_select_list++;
4776
item->split_sum_func(session, ref_pointer_array, *sum_func_list);
4777
session->used_tables|= item->used_tables();
4778
session->lex->current_select->cur_pos_in_select_list++;
5606
thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
5607
thd->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
4780
session->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
4781
session->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
5609
thd->lex->allow_sum_func= save_allow_sum_func;
5610
thd->mark_used_columns= save_mark_used_columns;
5611
return(test(thd->is_error()));
4783
session->lex->allow_sum_func= save_allow_sum_func;
4784
session->mark_used_columns= save_mark_used_columns;
4785
return(test(session->is_error()));
5644
context name resolution contest to setup table list there
5645
from_clause Top-level list of table references in the FROM clause
5646
tables Table list (select_lex->table_list)
5647
leaves List of join table leaves list (select_lex->leaf_tables)
5648
refresh It is onle refresh for subquery
5649
select_insert It is SELECT ... INSERT command
4817
session Thread handler
4818
context name resolution contest to setup table list there
4819
from_clause Top-level list of table references in the FROM clause
4820
tables Table list (select_lex->table_list)
4821
leaves List of join table leaves list (select_lex->leaf_tables)
4822
refresh It is onle refresh for subquery
4823
select_insert It is SELECT ... INSERT command
5652
Check also that the 'used keys' and 'ignored keys' exists and set up the
5653
table structure accordingly.
5654
Create a list of leaf tables. For queries with NATURAL/USING JOINs,
5655
compute the row types of the top most natural/using join table references
5656
and link these into a list of table references for name resolution.
4826
Check also that the 'used keys' and 'ignored keys' exists and set up the
4827
table structure accordingly.
4828
Create a list of leaf tables. For queries with NATURAL/USING JOINs,
4829
compute the row types of the top most natural/using join table references
4830
and link these into a list of table references for name resolution.
5658
This has to be called for all tables that are used by items, as otherwise
5659
table->map is not set and all Item_field will be regarded as const items.
4832
This has to be called for all tables that are used by items, as otherwise
4833
table->map is not set and all Item_field will be regarded as const items.
5662
false ok; In this case *map will includes the chosen index
4836
false ok; In this case *map will includes the chosen index
5666
bool setup_tables(THD *thd, Name_resolution_context *context,
4840
bool setup_tables(Session *session, Name_resolution_context *context,
5667
4841
List<TableList> *from_clause, TableList *tables,
5668
4842
TableList **leaves, bool select_insert)
4844
uint32_t tablenr= 0;
5672
assert ((select_insert && !tables->next_name_resolution_table) || !tables ||
5673
(context->table_list && context->first_name_resolution_table));
4846
assert ((select_insert && !tables->next_name_resolution_table) || !tables ||
4847
(context->table_list && context->first_name_resolution_table));
5675
4849
this is used for INSERT ... SELECT.
5676
4850
For select we setup tables except first (and its underlying tables)
5678
TableList *first_select_table= (select_insert ?
4852
TableList *first_select_table= (select_insert ? tables->next_local: NULL);
5681
4854
if (!(*leaves))
5682
4855
make_leaves_list(leaves, tables);
5957
5130
Fix all conditions and outer join expressions.
5962
tables list of tables for name resolving (select_lex->table_list)
5963
leaves list of leaves of join table tree (select_lex->leaf_tables)
5134
session thread handler
5135
tables list of tables for name resolving (select_lex->table_list)
5136
leaves list of leaves of join table tree (select_lex->leaf_tables)
5970
true if some error occured (e.g. out of memory)
5143
true if some error occured (e.g. out of memory)
5974
int setup_conds(THD *thd, TableList *tables __attribute__((unused)),
5147
int setup_conds(Session *session, TableList *leaves, COND **conds)
5978
SELECT_LEX *select_lex= thd->lex->current_select;
5149
Select_Lex *select_lex= session->lex->current_select;
5979
5150
TableList *table= NULL; // For HP compilers
5980
void *save_thd_marker= thd->thd_marker;
5151
void *save_session_marker= session->session_marker;
5982
it_is_update set to true when tables of primary SELECT_LEX (SELECT_LEX
5153
it_is_update set to true when tables of primary Select_Lex (Select_Lex
5983
5154
which belong to LEX, i.e. most up SELECT) will be updated by
5984
5155
INSERT/UPDATE/LOAD
5985
NOTE: using this condition helps to prevent call of prepare_check_option()
5156
NOTE-> using this condition helps to prevent call of prepare_check_option()
5986
5157
from subquery of VIEW, because tables of subquery belongs to VIEW
5987
5158
(see condition before prepare_check_option() call)
5989
5160
bool save_is_item_list_lookup= select_lex->is_item_list_lookup;
5990
5161
select_lex->is_item_list_lookup= 0;
5992
thd->mark_used_columns= MARK_COLUMNS_READ;
5163
session->mark_used_columns= MARK_COLUMNS_READ;
5993
5164
select_lex->cond_count= 0;
5994
5165
select_lex->between_count= 0;
5995
5166
select_lex->max_equal_elems= 0;
5997
thd->thd_marker= (void*)1;
5168
session->session_marker= (void*)1;
6000
thd->where="where clause";
6001
if ((!(*conds)->fixed && (*conds)->fix_fields(thd, conds)) ||
6002
(*conds)->check_cols(1))
5171
session->where="where clause";
5172
if ((!(*conds)->fixed && (*conds)->fix_fields(session, conds)) ||
5173
(*conds)->check_cols(1))
6003
5174
goto err_no_arena;
6005
thd->thd_marker= save_thd_marker;
5176
session->session_marker= save_session_marker;
6008
5179
Apply fix_fields() to all ON clauses at all levels of nesting,
6214
5429
(file->name[1] == '.' && !file->name[2])))
6217
if (!memcmp(file->name, tmp_file_prefix, tmp_file_prefix_length))
5432
if (!memcmp(file->name, TMP_FILE_PREFIX, TMP_FILE_PREFIX_LENGTH))
6219
5434
char *ext= fn_ext(file->name);
6220
uint ext_len= strlen(ext);
6221
uint filePath_len= snprintf(filePath, sizeof(filePath),
6222
"%s%c%s", tmpdir, FN_LIBCHAR,
6224
if (!memcmp(reg_ext, ext, ext_len))
5435
uint32_t ext_len= strlen(ext);
5436
uint32_t filePath_len= snprintf(filePath, sizeof(filePath),
5437
"%s%c%s", drizzle_tmpdir, FN_LIBCHAR,
5439
if (!memcmp(".dfe", ext, ext_len))
6226
5442
handler *handler_file= 0;
6227
5443
/* We should cut file extention before deleting of table */
6228
5444
memcpy(filePathCopy, filePath, filePath_len - ext_len);
6229
5445
filePathCopy[filePath_len - ext_len]= 0;
6230
init_tmp_table_share(thd, &share, "", 0, "", filePathCopy);
6231
if (!open_table_def(thd, &share, 0) &&
6232
((handler_file= get_new_handler(&share, thd->mem_root,
5446
share.init(NULL, filePathCopy);
5447
if (!open_table_def(session, &share) &&
5448
((handler_file= get_new_handler(&share, session->mem_root,
6233
5449
share.db_type()))))
6235
5451
handler_file->ha_delete_table(filePathCopy);
6236
5452
delete handler_file;
6238
free_table_share(&share);
5454
share.free_table_share();
6241
5457
File can be already deleted by tmp_table.file->delete_table().
6242
5458
So we hide error messages which happnes during deleting of these
6245
VOID(my_delete(filePath, MYF(0)));
5461
my_delete(filePath, MYF(0));
6248
5464
my_dirend(dirp);
6251
my_pthread_setspecific_ptr(THR_THD, 0);
6257
5474
/*****************************************************************************
6258
unireg support functions
6259
*****************************************************************************/
5475
unireg support functions
5476
*****************************************************************************/
6262
5479
Invalidate any cache entries that are for some DB
6265
remove_db_from_cache()
6266
db Database name. This will be in lower case if
6267
lower_case_table_name is set
5482
remove_db_from_cache()
5483
db Database name. This will be in lower case if
5484
lower_case_table_name is set
6270
We can't use hash_delete when looping hash_elements. We mark them first
6271
and afterwards delete those marked unused.
5487
We can't use hash_delete when looping hash_elements. We mark them first
5488
and afterwards delete those marked unused.
6274
5491
void remove_db_from_cache(const char *db)
6276
for (uint idx=0 ; idx < open_cache.records ; idx++)
5493
for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
6278
5495
Table *table=(Table*) hash_element(&open_cache,idx);
6279
5496
if (!strcmp(table->s->db.str, db))
6281
5498
table->s->version= 0L; /* Free when thread is ready */
6282
5499
if (!table->in_use)
6283
relink_unused(table);
5500
relink_unused(table);
6286
5503
while (unused_tables && !unused_tables->s->version)
6287
VOID(hash_delete(&open_cache,(uchar*) unused_tables));
6292
free all unused tables
6295
This is called by 'handle_manager' when one wants to periodicly flush
6296
all not used tables.
6301
(void) pthread_mutex_lock(&LOCK_open);
6302
while (unused_tables)
6303
hash_delete(&open_cache,(uchar*) unused_tables);
6304
(void) pthread_mutex_unlock(&LOCK_open);
5504
hash_delete(&open_cache,(unsigned char*) unused_tables);
6312
5512
close_thread_tables() is called.
6318
0 This thread now have exclusive access to this table and no other thread
6319
can access the table until close_thread_tables() is called.
6320
1 Table is in use by another thread
5518
0 This thread now have exclusive access to this table and no other thread
5519
can access the table until close_thread_tables() is called.
5520
1 Table is in use by another thread
6323
bool remove_table_from_cache(THD *thd, const char *db, const char *table_name,
5523
bool remove_table_from_cache(Session *session, const char *db, const char *table_name,
6326
5526
char key[MAX_DBKEY_LENGTH];
5528
uint32_t key_length;
6330
bool result= 0, signalled= 0;
6332
key_length=(uint) (stpcpy(stpcpy(key,db)+1,table_name)-key)+1;
5532
bool signalled= false;
5534
key_pos= strcpy(key_pos, db) + strlen(db);
5535
key_pos= strcpy(key_pos+1, table_name) + strlen(table_name);
5536
key_length= (uint32_t) (key_pos-key)+1;
6335
5540
HASH_SEARCH_STATE state;
6336
result= signalled= 0;
5541
result= signalled= false;
6338
for (table= (Table*) hash_first(&open_cache, (uchar*) key, key_length,
5543
for (table= (Table*) hash_first(&open_cache, (unsigned char*) key, key_length,
6341
table= (Table*) hash_next(&open_cache, (uchar*) key, key_length,
5546
table= (Table*) hash_next(&open_cache, (unsigned char*) key, key_length,
6346
5551
table->s->version=0L; /* Free when thread is ready */
6347
5552
if (!(in_use=table->in_use))
6349
5554
relink_unused(table);
6351
else if (in_use != thd)
5556
else if (in_use != session)
6354
5559
Mark that table is going to be deleted from cache. This will
6355
5560
force threads that are in mysql_lock_tables() (but not yet
6356
5561
in thr_multi_lock()) to abort it's locks, close all tables and retry
6358
in_use->some_tables_deleted= 1;
5563
in_use->some_tables_deleted= true;
6359
5564
if (table->is_name_opened())
6364
Now we must abort all tables locks used by this thread
6365
as the thread may be waiting to get a lock for another table.
5569
Now we must abort all tables locks used by this thread
5570
as the thread may be waiting to get a lock for another table.
6366
5571
Note that we need to hold LOCK_open while going through the
6367
5572
list. So that the other thread cannot change it. The other
6368
5573
thread must also hold LOCK_open whenever changing the
6369
5574
open_tables list. Aborting the MERGE lock after a child was
6370
5575
closed and before the parent is closed would be fatal.
6372
for (Table *thd_table= in_use->open_tables;
6374
thd_table= thd_table->next)
5577
for (Table *session_table= in_use->open_tables;
5579
session_table= session_table->next)
6376
5581
/* Do not handle locks of MERGE children. */
6377
if (thd_table->db_stat) // If table is open
6378
signalled|= mysql_lock_abort_for_thread(thd, thd_table);
5582
if (session_table->db_stat) // If table is open
5583
signalled|= mysql_lock_abort_for_thread(session, session_table);
6382
result= result || (flags & RTFC_OWNED_BY_THD_FLAG);
5587
result= result || (flags & RTFC_OWNED_BY_Session_FLAG);
6384
5589
while (unused_tables && !unused_tables->s->version)
6385
VOID(hash_delete(&open_cache,(uchar*) unused_tables));
5590
hash_delete(&open_cache,(unsigned char*) unused_tables);
6387
5592
/* Remove table from table definition cache if it's not in use */
6388
if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key,
5593
if ((share= (TableShare*) hash_search(&table_def_cache,(unsigned char*) key,
6391
5596
share->version= 0; // Mark for delete
6392
5597
if (share->ref_count == 0)
6394
5599
pthread_mutex_lock(&share->mutex);
6395
VOID(hash_delete(&table_def_cache, (uchar*) share));
5600
hash_delete(&table_def_cache, (unsigned char*) share);