drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
sql_base.cc
1/* Copyright (C) 2000-2006 MySQL AB
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
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.
11
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16
17/* Basic functions needed by many modules */
18#include <config.h>
19#include <assert.h>
20
21#include <signal.h>
22#include <sys/time.h>
23#include <time.h>
24
25#include <drizzled/internal/my_pthread.h>
26#include <drizzled/internal/thread_var.h>
27
28#include <drizzled/sql_select.h>
29#include <drizzled/error.h>
30#include <drizzled/gettext.h>
31#include <drizzled/nested_join.h>
32#include <drizzled/sql_base.h>
33#include <drizzled/show.h>
34#include <drizzled/item/cmpfunc.h>
35#include <drizzled/replication_services.h>
36#include <drizzled/check_stack_overrun.h>
37#include <drizzled/lock.h>
38#include <drizzled/plugin/listen.h>
40#include <drizzled/field/epoch.h>
41#include <drizzled/field/null.h>
42#include <drizzled/sql_table.h>
43#include <drizzled/charset.h>
44#include <drizzled/pthread_globals.h>
45#include <drizzled/internal/iocache.h>
46#include <drizzled/drizzled.h>
47#include <drizzled/plugin/authorization.h>
48#include <drizzled/table/temporary.h>
49#include <drizzled/table/placeholder.h>
50#include <drizzled/table/unused.h>
51#include <drizzled/plugin/storage_engine.h>
52#include <drizzled/session.h>
53#include <drizzled/item/subselect.h>
54#include <drizzled/sql_lex.h>
55#include <drizzled/catalog/local.h>
56#include <drizzled/open_tables_state.h>
57#include <drizzled/table/cache.h>
58
59using namespace std;
60
61namespace drizzled {
62
63extern bool volatile shutdown_in_progress;
64
65void table_cache_free()
66{
67 g_refresh_version++; // Force close of open tables
68
69 table::getUnused().clear();
70 table::getCache().clear();
71}
72
73/*
74 Close cursor handle, but leave the table in the table cache
75
76 SYNOPSIS
77 close_handle_and_leave_table_as_lock()
78 table Table Cursor
79
80 NOTES
81 By leaving the table in the table cache, it disallows any other thread
82 to open the table
83
84 session->getKilled() will be set if we run out of memory
85
86 If closing a MERGE child, the calling function has to take care for
87 closing the parent too, if necessary.
88*/
89
90
91void close_handle_and_leave_table_as_lock(Table *table)
92{
93 assert(table->db_stat);
94 assert(table->getShare()->getType() == message::Table::STANDARD);
95
96 /*
97 Make a local copy of the table share and free the current one.
98 This has to be done to ensure that the table share is removed from
99 the table defintion cache as soon as the last instance is removed
100 */
101 identifier::Table identifier(table->getShare()->getTableIdentifier().getCatalog(), table->getShare()->getSchemaName(), table->getShare()->getTableName(), message::Table::INTERNAL);
102 TableShare *share= new TableShare(identifier.getType(), identifier, identifier.getKey().vector(), static_cast<uint32_t>(table->getShare()->getCacheKeySize()));
103
104 table->cursor->close();
105 table->db_stat= 0; // Mark cursor closed
106 table::instance::release(table->getMutableShare());
107 table->setShare(share);
108}
109
110
111/*****************************************************************************
112 * Functions to free open table cache
113 ****************************************************************************/
114
115
116void Table::intern_close_table()
117{ // Free all structures
118 free_io_cache();
119 if (cursor) // Not true if name lock
120 {
121 delete_table(true); // close cursor
122 }
123}
124
125/* Free resources allocated by filesort() and read_record() */
126
127void Table::free_io_cache()
128{
129 if (sort.io_cache)
130 {
131 sort.io_cache->close_cached_file();
132 safe_delete(sort.io_cache);
133 }
134}
135
136
137/*
138 Close all tables which aren't in use by any thread
139
140 @param session Thread context (may be NULL)
141 @param tables List of tables to remove from the cache
142 @param have_lock If table::Cache::mutex() is locked
143 @param wait_for_refresh Wait for a impending flush
144 @param wait_for_placeholders Wait for tables being reopened so that the GRL
145 won't proceed while write-locked tables are being reopened by other
146 threads.
147
148 @remark Session can be NULL, but then wait_for_refresh must be false
149 and tables must be NULL.
150*/
151
152bool Session::close_cached_tables(TableList *tables, bool wait_for_refresh, bool wait_for_placeholders)
153{
154 bool result= false;
155 Session *session= this;
156
157 {
158 boost::mutex::scoped_lock scopedLock(table::Cache::mutex()); /* Optionally lock for remove tables from open_cahe if not in use */
159 if (not tables)
160 {
161 g_refresh_version++; // Force close of open tables
162 table::getUnused().clear();
163 }
164 else
165 {
166 bool found= false;
167 for (TableList *table= tables; table; table= table->next_local)
168 {
169 if (table::Cache::removeTable(*session, identifier::Table(catalog().identifier(), table->getSchemaName(), table->getTableName()), RTFC_OWNED_BY_Session_FLAG))
170 {
171 found= true;
172 }
173 }
174 if (!found)
175 wait_for_refresh= false; // Nothing to wait for
176 }
177
178 if (wait_for_refresh)
179 {
180 /*
181 If there is any table that has a lower refresh_version, wait until
182 this is closed (or this thread is killed) before returning
183 */
184 session->mysys_var->current_mutex= &table::Cache::mutex();
185 session->mysys_var->current_cond= &COND_refresh;
186 session->set_proc_info("Flushing tables");
187
188 session->close_old_data_files();
189
190 bool found= true;
191 /* Wait until all threads has closed all the tables we had locked */
192 while (found && ! session->getKilled())
193 {
194 found= false;
195 for (table::CacheMap::const_iterator iter= table::getCache().begin();
196 iter != table::getCache().end();
197 iter++)
198 {
199 Table *table= iter->second;
200 /* Avoid a self-deadlock. */
201 if (table->in_use == session)
202 continue;
203 /*
204 Note that we wait here only for tables which are actually open, and
205 not for placeholders with Table::open_placeholder set. Waiting for
206 latter will cause deadlock in the following scenario, for example:
207
208 conn1-> lock table t1 write;
209 conn2-> lock table t2 write;
210 conn1-> flush tables;
211 conn2-> flush tables;
212
213 It also does not make sense to wait for those of placeholders that
214 are employed by CREATE TABLE as in this case table simply does not
215 exist yet.
216 */
217 if (table->needs_reopen_or_name_lock() && (table->db_stat ||
218 (table->open_placeholder && wait_for_placeholders)))
219 {
220 found= true;
221 COND_refresh.wait(scopedLock);
222 break;
223 }
224 }
225 }
226 /*
227 No other thread has the locked tables open; reopen them and get the
228 old locks. This should always succeed (unless some external process
229 has removed the tables)
230 */
231 result= session->reopen_tables();
232
233 /* Set version for table */
234 for (Table *table= session->open_tables.open_tables_; table ; table= table->getNext())
235 {
236 /*
237 Preserve the version (0) of write locked tables so that a impending
238 global read lock won't sneak in.
239 */
240 if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
241 table->getMutableShare()->refreshVersion();
242 }
243 }
244 }
245
246 if (wait_for_refresh)
247 {
248 boost::mutex::scoped_lock scopedLock(session->mysys_var->mutex);
249 session->mysys_var->current_mutex= 0;
250 session->mysys_var->current_cond= 0;
251 session->set_proc_info(0);
252 }
253
254 return result;
255}
256
257
261
263{
264 table::Concurrent *table= static_cast<table::Concurrent *>(open_tables_);
265
266 safe_mutex_assert_owner(table::Cache::mutex().native_handle());
267 assert(table->key_read == 0);
268 assert(not table->cursor || table->cursor->inited == Cursor::NONE);
269
270 open_tables_= table->getNext();
271
272 if (table->needs_reopen_or_name_lock() ||
273 version != g_refresh_version || !table->db_stat)
274 {
275 table::remove_table(table);
276 return true;
277 }
278 /*
279 Open placeholders have Table::db_stat set to 0, so they should be
280 handled by the first alternative.
281 */
282 assert(not table->open_placeholder);
283
284 /* Free memory and reset for next loop */
285 table->cursor->ha_reset();
286 table->in_use= NULL;
287
288 table::getUnused().link(table);
289 return false;
290}
291
292
300
302{
303 bool found_old_table= false;
304
305 safe_mutex_assert_not_owner(table::Cache::mutex().native_handle());
306
307 boost::mutex::scoped_lock scoped_lock(table::Cache::mutex()); /* Close all open tables on Session */
308
309 while (open_tables_)
310 {
311 found_old_table|= free_cached_table();
312 }
313 if (found_old_table)
314 {
315 /* Tell threads waiting for refresh that something has happened */
317 }
318}
319
320/*
321 Find table in list.
322
323 SYNOPSIS
324 find_table_in_list()
325 table Pointer to table list
326 offset Offset to which list in table structure to use
327 db_name Data base name
328 table_name Table name
329
330NOTES:
331This is called by find_table_in_global_list().
332
333RETURN VALUES
334NULL Table not found
335# Pointer to found table.
336*/
337
338TableList *find_table_in_list(TableList *table,
339 TableList *TableList::*link,
340 const char *db_name,
341 const char *table_name)
342{
343 for (; table; table= table->*link)
344 {
345 if ((table->table == 0 || table->table->getShare()->getType() == message::Table::STANDARD)
346 && not system_charset_info->strcasecmp(table->getSchemaName(), db_name)
347 && not system_charset_info->strcasecmp(table->getTableName(), table_name))
348 {
349 break;
350 }
351 }
352 return table;
353}
354
355
356/*
357 Test that table is unique (It's only exists once in the table list)
358
359 SYNOPSIS
360 unique_table()
361 session thread handle
362 table table which should be checked
363 table_list list of tables
364 check_alias whether to check tables' aliases
365
366NOTE: to exclude derived tables from check we use following mechanism:
367a) during derived table processing set Session::derived_tables_processing
368b) JOIN::prepare set SELECT::exclude_from_table_unique_test if
369Session::derived_tables_processing set. (we can't use JOIN::execute
370because for PS we perform only JOIN::prepare, but we can't set this
371flag in JOIN::prepare if we are not sure that we are in derived table
372processing loop, because multi-update call fix_fields() for some its
373items (which mean JOIN::prepare for subqueries) before unique_table
374call to detect which tables should be locked for write).
375c) unique_table skip all tables which belong to SELECT with
376SELECT::exclude_from_table_unique_test set.
377Also SELECT::exclude_from_table_unique_test used to exclude from check
378tables of main SELECT of multi-delete and multi-update
379
380We also skip tables with TableList::prelocking_placeholder set,
381because we want to allow SELECTs from them, and their modification
382will rise the error anyway.
383
384TODO: when we will have table/view change detection we can do this check
385only once for PS/SP
386
387RETURN
388found duplicate
3890 if table is unique
390*/
391
392TableList* unique_table(TableList *table, TableList *table_list,
393 bool check_alias)
394{
395 TableList *res;
396 const char *d_name, *t_name, *t_alias;
397
398 /*
399 If this function called for query which update table (INSERT/UPDATE/...)
400 then we have in table->table pointer to Table object which we are
401 updating even if it is VIEW so we need TableList of this Table object
402 to get right names (even if lower_case_table_names used).
403
404 If this function called for CREATE command that we have not opened table
405 (table->table equal to 0) and right names is in current TableList
406 object.
407 */
408 if (table->table)
409 {
410 /* temporary table is always unique */
411 if (table->table && table->table->getShare()->getType() != message::Table::STANDARD)
412 return 0;
413 table= table->find_underlying_table(table->table);
414 /*
415 as far as we have table->table we have to find real TableList of
416 it in underlying tables
417 */
418 assert(table);
419 }
420 d_name= table->getSchemaName();
421 t_name= table->getTableName();
422 t_alias= table->alias;
423
424 for (;;)
425 {
426 if ((! (res= find_table_in_global_list(table_list, d_name, t_name))) ||
427 ((!res->table || res->table != table->table) &&
428 (!check_alias || !(files_charset_info->strcasecmp(t_alias, res->alias))) &&
429 res->select_lex && !res->select_lex->exclude_from_table_unique_test))
430 break;
431 /*
432 If we found entry of this table or table of SELECT which already
433 processed in derived table or top select of multi-update/multi-delete
434 (exclude_from_table_unique_test) or prelocking placeholder.
435 */
436 table_list= res->next_global;
437 }
438 return res;
439}
440
441
442void Open_tables_state::doGetTableNames(const identifier::Schema &schema_identifier,
443 std::set<std::string>& set_of_names)
444{
445 for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
446 {
447 if (schema_identifier.compare(table->getShare()->getSchemaName()))
448 {
449 set_of_names.insert(table->getShare()->getTableName());
450 }
451 }
452}
453
454void Open_tables_state::doGetTableNames(CachedDirectory &,
455 const identifier::Schema &schema_identifier,
456 std::set<std::string> &set_of_names)
457{
458 doGetTableNames(schema_identifier, set_of_names);
459}
460
461void Open_tables_state::doGetTableIdentifiers(const identifier::Schema &schema_identifier,
462 identifier::table::vector &set_of_identifiers)
463{
464 for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
465 {
466 if (schema_identifier.compare(table->getShare()->getSchemaName()))
467 {
468 set_of_identifiers.push_back(identifier::Table(table->getShare()->getTableIdentifier().getCatalog(),
469 table->getShare()->getSchemaName(),
470 table->getShare()->getTableName(),
471 table->getShare()->getPath()));
472 }
473 }
474}
475
476void Open_tables_state::doGetTableIdentifiers(CachedDirectory &,
477 const identifier::Schema &schema_identifier,
478 identifier::table::vector &set_of_identifiers)
479{
480 doGetTableIdentifiers(schema_identifier, set_of_identifiers);
481}
482
483bool Open_tables_state::doDoesTableExist(const identifier::Table &identifier)
484{
485 for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
486 {
487 if (table->getShare()->getType() == message::Table::TEMPORARY)
488 {
489 if (identifier.getKey() == table->getShare()->getCacheKey())
490 {
491 return true;
492 }
493 }
494 }
495
496 return false;
497}
498
499int Open_tables_state::doGetTableDefinition(const identifier::Table &identifier,
500 message::Table &table_proto)
501{
502 for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
503 {
504 if (table->getShare()->getType() == message::Table::TEMPORARY)
505 {
506 if (identifier.getKey() == table->getShare()->getCacheKey())
507 {
508 table_proto.CopyFrom(*(table->getShare()->getTableMessage()));
509
510 return EEXIST;
511 }
512 }
513 }
514
515 return ENOENT;
516}
517
518Table *Open_tables_state::find_temporary_table(const identifier::Table &identifier)
519{
520 for (Table *table= temporary_tables ; table ; table= table->getNext())
521 {
522 if (identifier.getKey() == table->getShare()->getCacheKey())
523 return table;
524 }
525
526 return NULL; // Not a temporary table
527}
528
529
555
557{
558 Table* table= find_temporary_table(identifier);
559 if (not table)
560 return 1;
561
562 /* Table might be in use by some outer statement. */
563 if (table->query_id && table->query_id != session_.getQueryId())
564 {
565 my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
566 return -1;
567 }
568 close_temporary_table(table);
569 return 0;
570}
571
572
582
584{
585 const identifier::Table::Key find_key(find->getShare()->getCacheKey());
586 Table **prev;
587 safe_mutex_assert_owner(table::Cache::mutex().native_handle());
588
589 /*
590 Note that we need to hold table::Cache::mutex() while changing the
591 open_tables list. Another thread may work on it.
592 (See: table::Cache::removeTable(), wait_completed_table())
593 Closing a MERGE child before the parent would be fatal if the
594 other thread tries to abort the MERGE lock in between.
595 */
596 for (prev= &open_tables.open_tables_; *prev; )
597 {
598 Table *list= *prev;
599
600 if (list->getShare()->getCacheKey() == find_key)
601 {
602 /* Remove table from open_tables list. */
603 *prev= list->getNext();
604
605 /* Close table. */
606 table::remove_table(static_cast<table::Concurrent *>(list));
607 }
608 else
609 {
610 /* Step to next entry in open_tables list. */
611 prev= list->getNextPtr();
612 }
613 }
614
615 // Notify any 'refresh' threads
617}
618
619
638
639void Session::drop_open_table(Table *table, const identifier::Table &identifier)
640{
641 if (table->getShare()->getType())
642 {
643 open_tables.close_temporary_table(table);
644 }
645 else
646 {
647 boost::mutex::scoped_lock scoped_lock(table::Cache::mutex()); /* Close and drop a table (AUX routine) */
648 /*
649 unlink_open_table() also tells threads waiting for refresh or close
650 that something has happened.
651 */
652 unlink_open_table(table);
653 (void)plugin::StorageEngine::dropTable(*this, identifier);
654 }
655}
656
657
658/*
659 Wait for condition but allow the user to send a kill to mysqld
660
661 SYNOPSIS
662 wait_for_condition()
663 session Thread Cursor
664 mutex mutex that is currently hold that is associated with condition
665 Will be unlocked on return
666 cond Condition to wait for
667*/
668
669void Session::wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond)
670{
671 /* Wait until the current table is up to date */
672 const char *saved_proc_info;
673 mysys_var->current_mutex= &mutex;
674 mysys_var->current_cond= &cond;
675 saved_proc_info= get_proc_info();
676 set_proc_info("Waiting for table");
677 {
678 /*
679 We must unlock mutex first to avoid deadlock becasue conditions are
680 sent to this thread by doing locks in the following order:
681 lock(mysys_var->mutex)
682 lock(mysys_var->current_mutex)
683
684 One by effect of this that one can only use wait_for_condition with
685 condition variables that are guranteed to not disapper (freed) even if this
686 mutex is unlocked
687 */
688 boost::mutex::scoped_lock scopedLock(mutex, boost::adopt_lock_t());
689 if (not getKilled())
690 {
691 cond.wait(scopedLock);
692 }
693 }
694 boost::mutex::scoped_lock mysys_scopedLock(mysys_var->mutex);
695 mysys_var->current_mutex= 0;
696 mysys_var->current_cond= 0;
697 set_proc_info(saved_proc_info);
698}
699
700
713
715{
716 safe_mutex_assert_owner(table::Cache::mutex().native_handle());
717
718 /*
719 Create a table entry with the right key and with an old refresh version
720 */
721 identifier::Table identifier(catalog().identifier(),
722 arg.getSchemaName(),
723 arg.getTableName(),
724 message::Table::INTERNAL);
725 table::Placeholder* table= new table::Placeholder(this, identifier);
726 table::Cache::insert(table);
727 return *table;
728}
729
730
753{
754 const identifier::Table::Key &key(identifier.getKey());
755 boost::mutex::scoped_lock scope_lock(table::Cache::mutex()); /* Obtain a name lock even though table is not in cache (like for create table) */
756 if (find_ptr(table::getCache(), key))
757 return NULL;
758 Table& table= table_cache_insert_placeholder(identifier);
759 table.open_placeholder= true;
760 table.setNext(open_tables.open_tables_);
761 open_tables.open_tables_= &table;
762 return &table;
763}
764
765/*
766 Open a table.
767
768 SYNOPSIS
769 open_table()
770 session Thread context.
771 table_list Open first table in list.
772 refresh INOUT Pointer to memory that will be set to 1 if
773 we need to close all tables and reopen them.
774 If this is a NULL pointer, then the table is not
775 put in the thread-open-list.
776 flags Bitmap of flags to modify how open works:
777 DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
778 someone has done a flush or namelock on it.
779 No version number checking is done.
780 DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
781 table not the base table or view.
782
783 IMPLEMENTATION
784 Uses a cache of open tables to find a table not in use.
785
786 If table list element for the table to be opened has "create" flag
787 set and table does not exist, this function will automatically insert
788 a placeholder for exclusive name lock into the open tables cache and
789 will return the Table instance that corresponds to this placeholder.
790
791 RETURN
792 NULL Open failed. If refresh is set then one should close
793 all other tables and retry the open.
794# Success. Pointer to Table object for open table.
795*/
796
797
798Table *Session::openTable(TableList *table_list, bool *refresh, uint32_t flags)
799{
800 Table *table;
801 const char *alias= table_list->alias;
802
803 /* Parsing of partitioning information from .frm needs session->lex set up. */
804 assert(lex().is_lex_started);
805
806 /* find a unused table in the open table cache */
807 if (refresh)
808 {
809 *refresh= false;
810 }
811
812 /* an open table operation needs a lot of the stack space */
813 if (check_stack_overrun(this, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
814 {
815 return NULL;
816 }
817
818 if (getKilled())
819 {
820 return NULL;
821 }
822
823 identifier::Table identifier(catalog().identifier(),
824 table_list->getSchemaName(),
825 table_list->getTableName());
826 const identifier::Table::Key &key(identifier.getKey());
827 table::CacheRange ppp;
828
829 /*
830 Unless requested otherwise, try to resolve this table in the list
831 of temporary tables of this thread. In MySQL temporary tables
832 are always thread-local and "shadow" possible base tables with the
833 same name. This block implements the behaviour.
834 TODO -> move this block into a separate function.
835 */
836 bool reset= false;
837 for (table= open_tables.getTemporaryTables(); table ; table=table->getNext())
838 {
839 if (table->getShare()->getCacheKey() == key)
840 {
841 /*
842 We're trying to use the same temporary table twice in a query.
843 Right now we don't support this because a temporary table
844 is always represented by only one Table object in Session, and
845 it can not be cloned. Emit an error for an unsupported behaviour.
846 */
847 if (table->query_id)
848 {
849 my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
850 return NULL;
851 }
852 table->query_id= getQueryId();
853 reset= true;
854 break;
855 }
856 }
857
858 if (not reset)
859 {
860 if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
861 {
862 my_error(ER_TABLE_UNKNOWN, identifier);
863 return NULL;
864 }
865
866 /*
867 If it's the first table from a list of tables used in a query,
868 remember refresh_version (the version of open_cache state).
869 If the version changes while we're opening the remaining tables,
870 we will have to back off, close all the tables opened-so-far,
871 and try to reopen them.
872
873 Note-> refresh_version is currently changed only during FLUSH TABLES.
874 */
875 if (!open_tables.open_tables_)
876 {
877 open_tables.version= g_refresh_version;
878 }
879 else if ((open_tables.version != g_refresh_version) &&
880 ! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
881 {
882 /* Someone did a refresh while thread was opening tables */
883 if (refresh)
884 *refresh= true;
885
886 return NULL;
887 }
888
889 /*
890 Non pre-locked/LOCK TABLES mode, and the table is not temporary:
891 this is the normal use case.
892 Now we should:
893 - try to find the table in the table cache.
894 - if one of the discovered Table instances is name-locked
895 (table->getShare()->version == 0) back off -- we have to wait
896 until no one holds a name lock on the table.
897 - if there is no such Table in the name cache, read the table definition
898 and insert it into the cache.
899 We perform all of the above under table::Cache::mutex() which currently protects
900 the open cache (also known as table cache) and table definitions stored
901 on disk.
902 */
903
904 {
905 boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
906
907 /*
908 Actually try to find the table in the open_cache.
909 The cache may contain several "Table" instances for the same
910 physical table. The instances that are currently "in use" by
911 some thread have their "in_use" member != NULL.
912 There is no good reason for having more than one entry in the
913 hash for the same physical table, except that we use this as
914 an implicit "pending locks queue" - see
915 wait_for_locked_table_names for details.
916 */
917 ppp= table::getCache().equal_range(key);
918
919 table= NULL;
920 for (table::CacheMap::const_iterator iter= ppp.first; iter != ppp.second; ++iter, table= NULL)
921 {
922 table= iter->second;
923
924 if (not table->in_use)
925 break;
926 /*
927 Here we flush tables marked for flush.
928 Normally, table->getShare()->version contains the value of
929 refresh_version from the moment when this table was
930 (re-)opened and added to the cache.
931 If since then we did (or just started) FLUSH TABLES
932 statement, refresh_version has been increased.
933 For "name-locked" Table instances, table->getShare()->version is set
934 to 0 (see lock_table_name for details).
935 In case there is a pending FLUSH TABLES or a name lock, we
936 need to back off and re-start opening tables.
937 If we do not back off now, we may dead lock in case of lock
938 order mismatch with some other thread:
939 c1-> name lock t1; -- sort of exclusive lock
940 c2-> open t2; -- sort of shared lock
941 c1-> name lock t2; -- blocks
942 c2-> open t1; -- blocks
943 */
944 if (table->needs_reopen_or_name_lock())
945 {
946 if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
947 {
948 /* Force close at once after usage */
949 open_tables.version= table->getShare()->getVersion();
950 continue;
951 }
952
953 /* Avoid self-deadlocks by detecting self-dependencies. */
954 if (table->open_placeholder && table->in_use == this)
955 {
956 my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getShare()->getTableName());
957 return NULL;
958 }
959
960 /*
961 Back off, part 1: mark the table as "unused" for the
962 purpose of name-locking by setting table->db_stat to 0. Do
963 that only for the tables in this thread that have an old
964 table->getShare()->version (this is an optimization (?)).
965 table->db_stat == 0 signals wait_for_locked_table_names
966 that the tables in question are not used any more. See
967 table_is_used call for details.
968 */
969 close_old_data_files(false, false);
970
971 /*
972 Back-off part 2: try to avoid "busy waiting" on the table:
973 if the table is in use by some other thread, we suspend
974 and wait till the operation is complete: when any
975 operation that juggles with table->getShare()->version completes,
976 it broadcasts COND_refresh condition variable.
977 If 'old' table we met is in use by current thread we return
978 without waiting since in this situation it's this thread
979 which is responsible for broadcasting on COND_refresh
980 (and this was done already in Session::close_old_data_files()).
981 Good example of such situation is when we have statement
982 that needs two instances of table and FLUSH TABLES comes
983 after we open first instance but before we open second
984 instance.
985 */
986 if (table->in_use != this)
987 {
988 /* wait_for_conditionwill unlock table::Cache::mutex() for us */
989 wait_for_condition(table::Cache::mutex(), COND_refresh);
990 scopedLock.release();
991 }
992 else
993 {
994 scopedLock.unlock();
995 }
996
997 /*
998 There is a refresh in progress for this table.
999 Signal the caller that it has to try again.
1000 */
1001 if (refresh)
1002 *refresh= true;
1003
1004 return NULL;
1005 }
1006 }
1007
1008 if (table)
1009 {
1010 table::getUnused().unlink(static_cast<table::Concurrent *>(table));
1011 table->in_use= this;
1012 }
1013 else
1014 {
1015 /* Insert a new Table instance into the open cache */
1016 /* Free cache if too big */
1017 table::getUnused().cull();
1018
1019 if (table_list->isCreate())
1020 {
1021 identifier::Table lock_table_identifier(catalog().identifier(),
1022 table_list->getSchemaName(),
1023 table_list->getTableName(),
1024 message::Table::STANDARD);
1025
1026 if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1027 {
1028 /*
1029 Table to be created, so we need to create placeholder in table-cache.
1030 */
1031 table= &table_cache_insert_placeholder(lock_table_identifier);
1032 /*
1033 Link placeholder to the open tables list so it will be automatically
1034 removed once tables are closed. Also mark it so it won't be ignored
1035 by other trying to take name-lock.
1036 */
1037 table->open_placeholder= true;
1038 table->setNext(open_tables.open_tables_);
1039 open_tables.open_tables_= table;
1040
1041 return table ;
1042 }
1043 /* Table exists. Let us try to open it. */
1044 }
1045
1046 /* make a new table */
1047 {
1048 table::Concurrent *new_table= new table::Concurrent;
1049 table= new_table;
1050 if (new_table->open_unireg_entry(this, alias, identifier))
1051 {
1052 delete new_table;
1053 return NULL;
1054 }
1055 (void)table::Cache::insert(new_table);
1056 }
1057 }
1058 }
1059
1060 if (refresh)
1061 {
1062 table->setNext(open_tables.open_tables_); /* Link into simple list */
1063 open_tables.open_tables_= table;
1064 }
1065 table->reginfo.lock_type= TL_READ; /* Assume read */
1066
1067 }
1068 assert(table->getShare()->getTableCount() > 0 || table->getShare()->getType() != message::Table::STANDARD);
1069
1070 /* Fix alias if table name changes */
1071 if (strcmp(table->getAlias(), alias))
1072 {
1073 table->setAlias(alias);
1074 }
1075
1076 /* These variables are also set in reopen_table() */
1077 table->tablenr= open_tables.current_tablenr++;
1078 table->used_fields= 0;
1079 table->const_table= 0;
1080 table->null_row= false;
1081 table->maybe_null= false;
1082 table->force_index= false;
1083 table->status=STATUS_NO_RECORD;
1084 table->insert_values.clear();
1085 /* Catch wrong handling of the auto_increment_field_not_null. */
1086 assert(!table->auto_increment_field_not_null);
1087 table->auto_increment_field_not_null= false;
1088 if (table->timestamp_field)
1089 {
1090 table->timestamp_field_type= table->timestamp_field->get_auto_set_type();
1091 }
1092 table->pos_in_table_list= table_list;
1093 table->clear_column_bitmaps();
1094 assert(table->key_read == 0);
1095
1096 return table;
1097}
1098
1099
1116
1118{
1119 safe_mutex_assert_owner(table::Cache::mutex().native_handle()); /* Adjust locks at the end of ALTER TABLEL */
1120
1121 if (open_tables.lock)
1122 {
1123 /*
1124 If we are not under LOCK TABLES we should have only one table
1125 open and locked so it makes sense to remove the lock at once.
1126 */
1127 unlockTables(open_tables.lock);
1128 open_tables.lock= 0;
1129 }
1130
1131 /*
1132 Note that open table list may contain a name-lock placeholder
1133 for target table name if we process ALTER Table ... RENAME.
1134 So loop below makes sense even if we are not under LOCK TABLES.
1135 */
1136 for (Table *table= open_tables.open_tables_; table ; table=table->getNext())
1137 {
1138 if (table->getShare()->getCacheKey() == identifier.getKey())
1139 {
1140 table->open_placeholder= true;
1141 close_handle_and_leave_table_as_lock(table);
1142 }
1143 }
1144}
1145
1146
1166
1168{
1169 Table *table,*next,**prev;
1170 Table **tables= 0; // For locks
1171 Table **tables_ptr= 0; // For locks
1172 bool error= false;
1173 const uint32_t flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
1174 DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
1175 DRIZZLE_LOCK_IGNORE_FLUSH;
1176
1177 if (open_tables.open_tables_ == NULL)
1178 return false;
1179
1180 safe_mutex_assert_owner(table::Cache::mutex().native_handle());
1181 {
1182 /*
1183 The ptr is checked later
1184 Do not handle locks of MERGE children.
1185 */
1186 uint32_t opens= 0;
1187
1188 for (table= open_tables.open_tables_; table ; table=table->getNext())
1189 {
1190 opens++;
1191 }
1192 tables= new Table *[opens];
1193 }
1194
1195 tables_ptr =tables;
1196
1197 prev= &open_tables.open_tables_;
1198 for (table= open_tables.open_tables_; table ; table=next)
1199 {
1200 next= table->getNext();
1201
1202 my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
1203 table::remove_table(static_cast<table::Concurrent *>(table));
1204 error= 1;
1205 }
1206 *prev=0;
1207
1208 if (tables != tables_ptr) // Should we get back old locks
1209 {
1210 /*
1211 We should always get these locks. Anyway, we must not go into
1212 wait_for_tables() as it tries to acquire table::Cache::mutex(), which is
1213 already locked.
1214 */
1215
1216 if (not lockTables(tables, (uint32_t) (tables_ptr - tables), flags))
1217 {
1218 /*
1219 This case should only happen if there is a bug in the reopen logic.
1220 Need to issue error message to have a reply for the application.
1221 Not exactly what happened though, but close enough.
1222 */
1223 my_error(ER_LOCK_DEADLOCK, MYF(0));
1224 error=1;
1225 }
1226 }
1227
1228 delete[] tables;
1229
1231
1232 return error;
1233}
1234
1235
1249
1250void Session::close_old_data_files(bool morph_locks, bool send_refresh)
1251{
1252 bool found= send_refresh;
1253
1254 Table *table= open_tables.open_tables_;
1255
1256 for (; table ; table=table->getNext())
1257 {
1258 /*
1259 Reopen marked for flush.
1260 */
1261 if (table->needs_reopen_or_name_lock())
1262 {
1263 found= true;
1264 if (table->db_stat)
1265 {
1266 if (morph_locks)
1267 {
1268 Table *ulcktbl= table;
1269 if (ulcktbl->lock_count)
1270 {
1271 /*
1272 Wake up threads waiting for table-level lock on this table
1273 so they won't sneak in when we will temporarily remove our
1274 lock on it. This will also give them a chance to close their
1275 instances of this table.
1276 */
1277 abortLock(ulcktbl);
1278 removeLock(ulcktbl);
1279 ulcktbl->lock_count= 0;
1280 }
1281 if ((ulcktbl != table) && ulcktbl->db_stat)
1282 {
1283 /*
1284 Close the parent too. Note that parent can come later in
1285 the list of tables. It will then be noticed as closed and
1286 as a placeholder. When this happens, do not clear the
1287 placeholder flag. See the branch below ("***").
1288 */
1289 ulcktbl->open_placeholder= true;
1290 close_handle_and_leave_table_as_lock(ulcktbl);
1291 }
1292 /*
1293 We want to protect the table from concurrent DDL operations
1294 (like RENAME Table) until we will re-open and re-lock it.
1295 */
1296 table->open_placeholder= true;
1297 }
1298 close_handle_and_leave_table_as_lock(table);
1299 }
1300 else if (table->open_placeholder && !morph_locks)
1301 {
1302 /*
1303 We come here only in close-for-back-off scenario. So we have to
1304 "close" create placeholder here to avoid deadlocks (for example,
1305 in case of concurrent execution of CREATE TABLE t1 SELECT * FROM t2
1306 and RENAME Table t2 TO t1). In close-for-re-open scenario we will
1307 probably want to let it stay.
1308
1309 Note "***": We must not enter this branch if the placeholder
1310 flag has been set because of a former close through a child.
1311 See above the comment that refers to this note.
1312 */
1313 table->open_placeholder= false;
1314 }
1315 }
1316 }
1317 if (found)
1319}
1320
1321
1322/*
1323 drop tables from locked list
1324
1325 SYNOPSIS
1326 drop_locked_tables()
1327 session Thread thandler
1328 db Database
1329 table_name Table name
1330
1331 INFORMATION
1332 This is only called on drop tables
1333
1334 The Table object for the dropped table is unlocked but still kept around
1335 as a name lock, which means that the table will be available for other
1336 thread as soon as we call unlock_table_names().
1337 If there is multiple copies of the table locked, all copies except
1338 the first, which acts as a name lock, is removed.
1339
1340 RETURN
1341# If table existed, return table
13420 Table was not locked
1343*/
1344
1345
1346Table *drop_locked_tables(Session *session, const drizzled::identifier::Table &identifier)
1347{
1348 Table *table,*next,**prev, *found= 0;
1349 prev= &session->open_tables.open_tables_;
1350
1351 /*
1352 Note that we need to hold table::Cache::mutex() while changing the
1353 open_tables list. Another thread may work on it.
1354 (See: table::Cache::removeTable(), wait_completed_table())
1355 Closing a MERGE child before the parent would be fatal if the
1356 other thread tries to abort the MERGE lock in between.
1357 */
1358 for (table= session->open_tables.open_tables_; table ; table=next)
1359 {
1360 next=table->getNext();
1361 if (table->getShare()->getCacheKey() == identifier.getKey())
1362 {
1363 session->removeLock(table);
1364
1365 if (!found)
1366 {
1367 found= table;
1368 /* Close engine table, but keep object around as a name lock */
1369 if (table->db_stat)
1370 {
1371 table->db_stat= 0;
1372 table->cursor->close();
1373 }
1374 }
1375 else
1376 {
1377 /* We already have a name lock, remove copy */
1378 table::remove_table(static_cast<table::Concurrent *>(table));
1379 }
1380 }
1381 else
1382 {
1383 *prev=table;
1384 prev= table->getNextPtr();
1385 }
1386 }
1387 *prev=0;
1388
1389 if (found)
1391
1392 return found;
1393}
1394
1395
1396/*
1397 If we have the table open, which only happens when a LOCK Table has been
1398 done on the table, change the lock type to a lock that will abort all
1399 other threads trying to get the lock.
1400*/
1401
1402void abort_locked_tables(Session *session, const drizzled::identifier::Table &identifier)
1403{
1404 Table *table;
1405 for (table= session->open_tables.open_tables_; table ; table= table->getNext())
1406 {
1407 if (table->getShare()->getCacheKey() == identifier.getKey())
1408 {
1409 /* If MERGE child, forward lock handling to parent. */
1410 session->abortLock(table);
1411 assert(0);
1412 break;
1413 }
1414 }
1415}
1416
1417
1418/*
1419 Open all tables in list
1420
1421 SYNOPSIS
1422 open_tables()
1423 session - thread Cursor
1424 start - list of tables in/out
1425 counter - number of opened tables will be return using this parameter
1426 flags - bitmap of flags to modify how the tables will be open:
1427 DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
1428 done a flush or namelock on it.
1429
1430 NOTE
1431 Unless we are already in prelocked mode, this function will also precache
1432 all SP/SFs explicitly or implicitly (via views and triggers) used by the
1433 query and add tables needed for their execution to table list. If resulting
1434 tables list will be non empty it will mark query as requiring precaching.
1435 Prelocked mode will be enabled for such query during lock_tables() call.
1436
1437 If query for which we are opening tables is already marked as requiring
1438 prelocking it won't do such precaching and will simply reuse table list
1439 which is already built.
1440
1441 RETURN
1442 0 - OK
1443 -1 - error
1444*/
1445
1446int Session::open_tables_from_list(TableList **start, uint32_t *counter, uint32_t flags)
1447{
1448 TableList *tables= NULL;
1449 bool refresh;
1450 int result= 0;
1451 /* Also used for indicating that prelocking is need */
1452 bool safe_to_ignore_table;
1453
1454 open_tables.current_tablenr= 0;
1455restart:
1456 *counter= 0;
1457 set_proc_info("Opening tables");
1458
1459 /*
1460 For every table in the list of tables to open, try to find or open
1461 a table.
1462 */
1463 for (tables= *start; tables ;tables= tables->next_global)
1464 {
1465 safe_to_ignore_table= false;
1466
1467 /*
1468 Ignore placeholders for derived tables. After derived tables
1469 processing, link to created temporary table will be put here.
1470 If this is derived table for view then we still want to process
1471 routines used by this view.
1472 */
1473 if (tables->derived)
1474 {
1475 continue;
1476 }
1477 (*counter)++;
1478
1479 /*
1480 * Is the user authorized to see this table? Do this before we check
1481 * to see if it exists so that an unauthorized user cannot phish for
1482 * table/schema information via error messages
1483 */
1484 identifier::Table the_table(catalog().identifier(),
1485 tables->getSchemaName(),
1486 tables->getTableName());
1487 if (not plugin::Authorization::isAuthorized(*user(), the_table))
1488 {
1489 result= -1; // Fatal error
1490 break;
1491 }
1492
1493
1494 /*
1495 Not a placeholder: must be a base table or a view, and the table is
1496 not opened yet. Try to open the table.
1497 */
1498 if (tables->table == NULL)
1499 tables->table= openTable(tables, &refresh, flags);
1500
1501 if (tables->table == NULL)
1502 {
1503 if (refresh) // Refresh in progress
1504 {
1505 /*
1506 We have met name-locked or old version of table. Now we have
1507 to close all tables which are not up to date. We also have to
1508 throw away set of prelocked tables (and thus close tables from
1509 this set that were open by now) since it possible that one of
1510 tables which determined its content was changed.
1511
1512 Instead of implementing complex/non-robust logic mentioned
1513 above we simply close and then reopen all tables.
1514
1515 In order to prepare for recalculation of set of prelocked tables
1516 we pretend that we have finished calculation which we were doing
1517 currently.
1518 */
1520 goto restart;
1521 }
1522
1523 if (safe_to_ignore_table)
1524 continue;
1525
1526 result= -1; // Fatal error
1527 break;
1528 }
1529 if (tables->lock_type != TL_UNLOCK)
1530 {
1531 if (tables->lock_type == TL_WRITE_DEFAULT)
1532 tables->table->reginfo.lock_type= update_lock_default;
1533 else if (tables->table->getShare()->getType() == message::Table::STANDARD)
1534 tables->table->reginfo.lock_type= tables->lock_type;
1535 }
1536 }
1537
1538 set_proc_info(0);
1539
1540 if (result && tables)
1541 {
1542 /*
1543 Some functions determine success as (tables->table != NULL).
1544 tables->table is in session->open_tables.
1545 */
1546 tables->table= NULL;
1547 }
1548
1549 return(result);
1550}
1551
1552
1553/*
1554 Open and lock one table
1555
1556 SYNOPSIS
1557 openTableLock()
1558 session Thread Cursor
1559 table_list Table to open is first table in this list
1560 lock_type Lock to use for open
1561 lock_flags Flags passed to mysql_lock_table
1562
1563 NOTE
1564 This function don't do anything like SP/SF/views/triggers analysis done
1565 in open_tables(). It is intended for opening of only one concrete table.
1566 And used only in special contexts.
1567
1568 RETURN VALUES
1569 table Opened table
1570 0 Error
1571
1572 If ok, the following are also set:
1573 table_list->lock_type lock_type
1574 table_list->table table
1575*/
1576
1577Table *Session::openTableLock(TableList *table_list, thr_lock_type lock_type)
1578{
1579 Table *table;
1580 bool refresh;
1581
1582 set_proc_info("Opening table");
1583 open_tables.current_tablenr= 0;
1584 while (!(table= openTable(table_list, &refresh)) && refresh) ;
1585
1586 if (table)
1587 {
1588 table_list->lock_type= lock_type;
1589 table_list->table= table;
1590
1591 assert(open_tables.lock == 0); // You must lock everything at once
1592 if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
1593 {
1594 if (not (open_tables.lock= lockTables(&table_list->table, 1, 0)))
1595 table= NULL;
1596 }
1597 }
1598
1599 set_proc_info(0);
1600
1601 return table;
1602}
1603
1604/*
1605 Lock all tables in list
1606
1607 SYNOPSIS
1608 lock_tables()
1609 session Thread Cursor
1610 tables Tables to lock
1611 count Number of opened tables
1612 need_reopen Out parameter which if true indicates that some
1613 tables were dropped or altered during this call
1614 and therefore invoker should reopen tables and
1615 try to lock them once again (in this case
1616 lock_tables() will also return error).
1617
1618 NOTES
1619 You can't call lock_tables twice, as this would break the dead-lock-free
1620 handling thr_lock gives us. You most always get all needed locks at
1621 once.
1622
1623 If query for which we are calling this function marked as requring
1624 prelocking, this function will do implicit LOCK TABLES and change
1625 session::prelocked_mode accordingly.
1626
1627 RETURN VALUES
1628 0 ok
1629 -1 Error
1630*/
1631
1632int Session::lock_tables(TableList *tables, uint32_t count, bool *need_reopen)
1633{
1634 /*
1635 We can't meet statement requiring prelocking if we already
1636 in prelocked mode.
1637 */
1638 *need_reopen= false;
1639
1640 if (tables == NULL)
1641 return 0;
1642
1643 assert(not open_tables.lock); // You must lock everything at once
1644
1645 Table** start;
1646 Table** ptr=start= new (mem) Table*[count];
1647 for (TableList* table= tables; table; table= table->next_global)
1648 {
1649 if (!table->placeholder())
1650 *(ptr++)= table->table;
1651 }
1652 if (not (open_tables.lock= lockTables(start, (uint32_t) (ptr - start), DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN)))
1653 {
1654 return -1;
1655 }
1656 return 0;
1657}
1658
1659
1660/*
1661 Open a single table without table caching and don't set it in open_list
1662
1663 SYNPOSIS
1664 open_temporary_table()
1665 session Thread object
1666 path Path (without .frm)
1667 db database
1668 table_name Table name
1669 link_in_list 1 if table should be linked into session->temporary_tables
1670
1671NOTES:
1672Used by alter_table to open a temporary table and when creating
1673a temporary table with CREATE TEMPORARY ...
1674
1675RETURN
16760 Error
1677# Table object
1678*/
1679
1680Table* Session::open_temporary_table(const identifier::Table &identifier, bool link_in_list)
1681{
1682 assert(identifier.isTmp());
1683 table::Temporary *new_tmp_table= new table::Temporary(identifier.getType(), identifier,
1684 identifier.getPath().c_str(), static_cast<uint32_t>(identifier.getPath().length()));
1685 /*
1686 First open the share, and then open the table from the share we just opened.
1687 */
1688 if (new_tmp_table->getMutableShare()->open_table_def(*this, identifier) ||
1689 new_tmp_table->getMutableShare()->open_table_from_share(this, identifier, identifier.getTableName().c_str(),
1690 (uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE | HA_GET_INDEX), ha_open_options, *new_tmp_table))
1691 {
1692 /* No need to lock share->mutex as this is not needed for tmp tables */
1693 delete new_tmp_table->getMutableShare();
1694 delete new_tmp_table;
1695 return NULL;
1696 }
1697
1698 new_tmp_table->reginfo.lock_type= TL_WRITE; // Simulate locked
1699
1700 if (link_in_list)
1701 {
1702 /* growing temp list at the head */
1703 new_tmp_table->setNext(open_tables.temporary_tables);
1704 if (new_tmp_table->getNext())
1705 {
1706 new_tmp_table->getNext()->setPrev(new_tmp_table);
1707 }
1708 open_tables.temporary_tables= new_tmp_table;
1709 open_tables.temporary_tables->setPrev(0);
1710 }
1711 new_tmp_table->pos_in_table_list= 0;
1712
1713 return new_tmp_table;
1714}
1715
1716
1717/*****************************************************************************
1718 * The following find_field_in_XXX procedures implement the core of the
1719 * name resolution functionality. The entry point to resolve a column name in a
1720 * list of tables is 'find_field_in_tables'. It calls 'find_field_in_table_ref'
1721 * for each table reference. In turn, depending on the type of table reference,
1722 * 'find_field_in_table_ref' calls one of the 'find_field_in_XXX' procedures
1723 * below specific for the type of table reference.
1724 ******************************************************************************/
1725
1726/* Special Field pointers as return values of find_field_in_XXX functions. */
1727Field *not_found_field= (Field*) 0x1;
1728Field *view_ref_found= (Field*) 0x2;
1729
1730static void update_field_dependencies(Session *session, Field *field, Table *table)
1731{
1732 if (session->mark_used_columns != MARK_COLUMNS_NONE)
1733 {
1734 boost::dynamic_bitset<> *current_bitmap= NULL;
1735
1736 /*
1737 We always want to register the used keys, as the column bitmap may have
1738 been set for all fields (for example for view).
1739 */
1740
1741 table->covering_keys&= field->part_of_key;
1742 table->merge_keys|= field->part_of_key;
1743
1744 if (session->mark_used_columns == MARK_COLUMNS_READ)
1745 {
1746 current_bitmap= table->read_set;
1747 }
1748 else
1749 {
1750 current_bitmap= table->write_set;
1751 }
1752
1753 //if (current_bitmap->testAndSet(field->position()))
1754 if (current_bitmap->test(field->position()))
1755 {
1756 if (session->mark_used_columns == MARK_COLUMNS_WRITE)
1757 session->dup_field= field;
1758 return;
1759 }
1760 table->used_fields++;
1761 }
1762}
1763
1764
1765/*
1766 Find field by name in a NATURAL/USING join table reference.
1767
1768 SYNOPSIS
1769 find_field_in_natural_join()
1770 session [in] thread Cursor
1771 table_ref [in] table reference to search
1772 name [in] name of field
1773 length [in] length of name
1774 ref [in/out] if 'name' is resolved to a view field, ref is
1775 set to point to the found view field
1776 register_tree_change [in] true if ref is not stack variable and we
1777 need register changes in item tree
1778 actual_table [out] the original table reference where the field
1779 belongs - differs from 'table_list' only for
1780 NATURAL/USING joins
1781
1782 DESCRIPTION
1783 Search for a field among the result fields of a NATURAL/USING join.
1784 Notice that this procedure is called only for non-qualified field
1785 names. In the case of qualified fields, we search directly the base
1786 tables of a natural join.
1787
1788 RETURN
1789 NULL if the field was not found
1790 PTR Pointer to the found Field
1791*/
1792
1793static Field *
1794find_field_in_natural_join(Session *session, TableList *table_ref,
1795 const char *name, uint32_t , Item **,
1796 bool, TableList **actual_table)
1797{
1798 List<Natural_join_column>::iterator field_it(table_ref->join_columns->begin());
1799 Natural_join_column *nj_col, *curr_nj_col;
1800 Field *found_field;
1801
1802 assert(table_ref->is_natural_join && table_ref->join_columns);
1803 assert(*actual_table == NULL);
1804
1805 for (nj_col= NULL, curr_nj_col= field_it++; curr_nj_col; curr_nj_col= field_it++)
1806 {
1807 if (!system_charset_info->strcasecmp(curr_nj_col->name(), name))
1808 {
1809 if (nj_col)
1810 {
1811 my_error(ER_NON_UNIQ_ERROR, MYF(0), name, session->where());
1812 return NULL;
1813 }
1814 nj_col= curr_nj_col;
1815 }
1816 }
1817 if (!nj_col)
1818 return NULL;
1819 {
1820 /* This is a base table. */
1821 assert(nj_col->table_ref->table == nj_col->table_field->getTable());
1822 found_field= nj_col->table_field;
1823 update_field_dependencies(session, found_field, nj_col->table_ref->table);
1824 }
1825
1826 *actual_table= nj_col->table_ref;
1827
1828 return(found_field);
1829}
1830
1831
1832/*
1833 Find field by name in a base table or a view with temp table algorithm.
1834
1835 SYNOPSIS
1836 find_field_in_table()
1837 session thread Cursor
1838 table table where to search for the field
1839 name name of field
1840 length length of name
1841 allow_rowid do allow finding of "_rowid" field?
1842 cached_field_index_ptr cached position in field list (used to speedup
1843 lookup for fields in prepared tables)
1844
1845 RETURN
1846 0 field is not found
1847# pointer to field
1848*/
1849
1850Field *
1851find_field_in_table(Session *session, Table *table, const char *name, uint32_t length,
1852 bool allow_rowid, uint32_t *cached_field_index_ptr)
1853{
1854 Field **field_ptr, *field;
1855 uint32_t cached_field_index= *cached_field_index_ptr;
1856
1857 /* We assume here that table->field < NO_CACHED_FIELD_INDEX = UINT_MAX */
1858 if (cached_field_index < table->getShare()->sizeFields()
1859 && not system_charset_info->strcasecmp(table->getField(cached_field_index)->field_name, name))
1860 {
1861 field_ptr= table->getFields() + cached_field_index;
1862 }
1863 else if (table->getShare()->getNamedFieldSize())
1864 {
1865 field_ptr= table->getMutableShare()->getNamedField(std::string(name, length));
1866 if (field_ptr)
1867 {
1868 /*
1869 field_ptr points to field in TableShare. Convert it to the matching
1870 field in table
1871 */
1872 field_ptr= (table->getFields() + table->getShare()->positionFields(field_ptr));
1873 }
1874 }
1875 else
1876 {
1877 if (!(field_ptr= table->getFields()))
1878 return NULL;
1879 for (; *field_ptr; ++field_ptr)
1880 if (not system_charset_info->strcasecmp((*field_ptr)->field_name, name))
1881 break;
1882 }
1883
1884 if (field_ptr && *field_ptr)
1885 {
1886 *cached_field_index_ptr= field_ptr - table->getFields();
1887 field= *field_ptr;
1888 }
1889 else
1890 {
1891 if (!allow_rowid ||
1892 system_charset_info->strcasecmp(name, "_rowid") ||
1893 table->getShare()->rowid_field_offset == 0)
1894 return NULL;
1895 field= table->getField(table->getShare()->rowid_field_offset-1);
1896 }
1897
1898 update_field_dependencies(session, field, table);
1899
1900 return field;
1901}
1902
1903
1904/*
1905 Find field in a table reference.
1906
1907 SYNOPSIS
1908 find_field_in_table_ref()
1909 session [in] thread Cursor
1910 table_list [in] table reference to search
1911 name [in] name of field
1912 length [in] field length of name
1913 item_name [in] name of item if it will be created (VIEW)
1914 db_name [in] optional database name that qualifies the
1915 table_name [in] optional table name that qualifies the field
1916 ref [in/out] if 'name' is resolved to a view field, ref
1917 is set to point to the found view field
1918 allow_rowid [in] do allow finding of "_rowid" field?
1919 cached_field_index_ptr [in] cached position in field list (used to
1920 speedup lookup for fields in prepared tables)
1921 register_tree_change [in] true if ref is not stack variable and we
1922 need register changes in item tree
1923 actual_table [out] the original table reference where the field
1924 belongs - differs from 'table_list' only for
1925 NATURAL_USING joins.
1926
1927 DESCRIPTION
1928 Find a field in a table reference depending on the type of table
1929 reference. There are three types of table references with respect
1930 to the representation of their result columns:
1931 - an array of Field_translator objects for MERGE views and some
1932 information_schema tables,
1933 - an array of Field objects (and possibly a name hash) for stored
1934 tables,
1935 - a list of Natural_join_column objects for NATURAL/USING joins.
1936 This procedure detects the type of the table reference 'table_list'
1937 and calls the corresponding search routine.
1938
1939 RETURN
1940 0 field is not found
1941 view_ref_found found value in VIEW (real result is in *ref)
1942# pointer to field
1943*/
1944
1945Field *
1946find_field_in_table_ref(Session *session, TableList *table_list,
1947 const char *name, uint32_t length,
1948 const char *item_name, const char *db_name,
1949 const char *table_name, Item **ref,
1950 bool allow_rowid,
1951 uint32_t *cached_field_index_ptr,
1952 bool register_tree_change, TableList **actual_table)
1953{
1954 Field *fld= NULL;
1955
1956 assert(table_list->alias);
1957 assert(name);
1958 assert(item_name);
1959
1960 /*
1961 Check that the table and database that qualify the current field name
1962 are the same as the table reference we are going to search for the field.
1963
1964 Exclude from the test below nested joins because the columns in a
1965 nested join generally originate from different tables. Nested joins
1966 also have no table name, except when a nested join is a merge view
1967 or an information schema table.
1968
1969 We include explicitly table references with a 'field_translation' table,
1970 because if there are views over natural joins we don't want to search
1971 inside the view, but we want to search directly in the view columns
1972 which are represented as a 'field_translation'.
1973
1974 TODO-> Ensure that table_name, db_name and tables->db always points to something !
1975 */
1976 if (/* Exclude nested joins. */
1977 (!table_list->getNestedJoin()) &&
1978 /* Include merge views and information schema tables. */
1979 /*
1980 Test if the field qualifiers match the table reference we plan
1981 to search.
1982 */
1983 table_name && table_name[0] &&
1984 (table_alias_charset->strcasecmp(table_list->alias, table_name) ||
1985 (db_name && db_name[0] && table_list->getSchemaName() && table_list->getSchemaName()[0] &&
1986 strcmp(db_name, table_list->getSchemaName()))))
1987 return 0;
1988
1989 *actual_table= NULL;
1990
1991 if (!table_list->getNestedJoin())
1992 {
1993 /* 'table_list' is a stored table. */
1994 assert(table_list->table);
1995 if ((fld= find_field_in_table(session, table_list->table, name, length,
1996 allow_rowid,
1997 cached_field_index_ptr)))
1998 *actual_table= table_list;
1999 }
2000 else
2001 {
2002 /*
2003 'table_list' is a NATURAL/USING join, or an operand of such join that
2004 is a nested join itself.
2005
2006 If the field name we search for is qualified, then search for the field
2007 in the table references used by NATURAL/USING the join.
2008 */
2009 if (table_name && table_name[0])
2010 {
2011 List<TableList>::iterator it(table_list->getNestedJoin()->join_list.begin());
2012 TableList *table;
2013 while ((table= it++))
2014 {
2015 if ((fld= find_field_in_table_ref(session, table, name, length, item_name,
2016 db_name, table_name, ref,
2017 allow_rowid,
2018 cached_field_index_ptr,
2019 register_tree_change, actual_table)))
2020 return fld;
2021 }
2022 return NULL;
2023 }
2024 /*
2025 Non-qualified field, search directly in the result columns of the
2026 natural join. The condition of the outer IF is true for the top-most
2027 natural join, thus if the field is not qualified, we will search
2028 directly the top-most NATURAL/USING join.
2029 */
2030 fld= find_field_in_natural_join(session, table_list, name, length, ref,
2031 register_tree_change, actual_table);
2032 }
2033
2034 if (fld)
2035 {
2036 if (session->mark_used_columns != MARK_COLUMNS_NONE)
2037 {
2038 /*
2039 Get rw_set correct for this field so that the Cursor
2040 knows that this field is involved in the query and gets
2041 retrieved/updated
2042 */
2043 Field *field_to_set= NULL;
2044 if (fld == view_ref_found)
2045 {
2046 Item *it= (*ref)->real_item();
2047 if (it->type() == Item::FIELD_ITEM)
2048 field_to_set= ((Item_field*)it)->field;
2049 else
2050 {
2051 if (session->mark_used_columns == MARK_COLUMNS_READ)
2052 it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
2053 }
2054 }
2055 else
2056 field_to_set= fld;
2057 if (field_to_set)
2058 {
2059 Table *table= field_to_set->getTable();
2060 if (session->mark_used_columns == MARK_COLUMNS_READ)
2061 table->setReadSet(field_to_set->position());
2062 else
2063 table->setWriteSet(field_to_set->position());
2064 }
2065 }
2066 }
2067 return(fld);
2068}
2069
2070
2071/*
2072 Find field in table list.
2073
2074 SYNOPSIS
2075 find_field_in_tables()
2076 session pointer to current thread structure
2077 item field item that should be found
2078 first_table list of tables to be searched for item
2079 last_table end of the list of tables to search for item. If NULL
2080 then search to the end of the list 'first_table'.
2081 ref if 'item' is resolved to a view field, ref is set to
2082 point to the found view field
2083 report_error Degree of error reporting:
2084 - IGNORE_ERRORS then do not report any error
2085 - IGNORE_EXCEPT_NON_UNIQUE report only non-unique
2086 fields, suppress all other errors
2087 - REPORT_EXCEPT_NON_UNIQUE report all other errors
2088 except when non-unique fields were found
2089 - REPORT_ALL_ERRORS
2090 register_tree_change true if ref is not a stack variable and we
2091 to need register changes in item tree
2092
2093 RETURN VALUES
2094 0 If error: the found field is not unique, or there are
2095 no sufficient access priviliges for the found field,
2096 or the field is qualified with non-existing table.
2097 not_found_field The function was called with report_error ==
2098 (IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
2099 field was not found.
2100 view_ref_found View field is found, item passed through ref parameter
2101 found field If a item was resolved to some field
2102*/
2103
2104Field *
2105find_field_in_tables(Session *session, Item_ident *item,
2106 TableList *first_table, TableList *last_table,
2107 Item **ref, find_item_error_report_type report_error,
2108 bool register_tree_change)
2109{
2110 Field *found=0;
2111 const char *db= item->db_name;
2112 const char *table_name= item->table_name;
2113 const char *name= item->field_name;
2114 uint32_t length=(uint32_t) strlen(name);
2115 char name_buff[NAME_LEN+1];
2116 TableList *cur_table= first_table;
2117 TableList *actual_table;
2118 bool allow_rowid;
2119
2120 if (!table_name || !table_name[0])
2121 {
2122 table_name= 0; // For easier test
2123 db= 0;
2124 }
2125
2126 allow_rowid= table_name || (cur_table && !cur_table->next_local);
2127
2128 if (item->cached_table)
2129 {
2130 /*
2131 This shortcut is used by prepared statements. We assume that
2132 TableList *first_table is not changed during query execution (which
2133 is true for all queries except RENAME but luckily RENAME doesn't
2134 use fields...) so we can rely on reusing pointer to its member.
2135 With this optimization we also miss case when addition of one more
2136 field makes some prepared query ambiguous and so erroneous, but we
2137 accept this trade off.
2138 */
2139 TableList *table_ref= item->cached_table;
2140 /*
2141 The condition (table_ref->view == NULL) ensures that we will call
2142 find_field_in_table even in the case of information schema tables
2143 when table_ref->field_translation != NULL.
2144 */
2145 if (table_ref->table)
2146 found= find_field_in_table(session, table_ref->table, name, length,
2147 true, &(item->cached_field_index));
2148 else
2149 found= find_field_in_table_ref(session, table_ref, name, length, item->name,
2150 NULL, NULL, ref,
2151 true, &(item->cached_field_index),
2152 register_tree_change,
2153 &actual_table);
2154 if (found)
2155 {
2156 /*
2157 Only views fields should be marked as dependent, not an underlying
2158 fields.
2159 */
2160 {
2161 Select_Lex *current_sel= session->lex().current_select;
2162 Select_Lex *last_select= table_ref->select_lex;
2163 /*
2164 If the field was an outer referencee, mark all selects using this
2165 sub query as dependent on the outer query
2166 */
2167 if (current_sel != last_select)
2168 mark_select_range_as_dependent(session, last_select, current_sel,
2169 found, *ref, item);
2170 }
2171 return found;
2172 }
2173 }
2174
2175 if (db)
2176 {
2177 /*
2178 convert database to lower case for comparison.
2179 We can't do this in Item_field as this would change the
2180 'name' of the item which may be used in the select list
2181 */
2182 strncpy(name_buff, db, sizeof(name_buff)-1);
2183 files_charset_info->casedn_str(name_buff);
2184 db= name_buff;
2185 }
2186
2187 if (last_table)
2188 last_table= last_table->next_name_resolution_table;
2189
2190 for (; cur_table != last_table ;
2191 cur_table= cur_table->next_name_resolution_table)
2192 {
2193 Field *cur_field= find_field_in_table_ref(session, cur_table, name, length,
2194 item->name, db, table_name, ref,
2195 allow_rowid,
2196 &(item->cached_field_index),
2197 register_tree_change,
2198 &actual_table);
2199 if (cur_field)
2200 {
2201 /*
2202 Store the original table of the field, which may be different from
2203 cur_table in the case of NATURAL/USING join.
2204 */
2205 item->cached_table= found ? 0 : actual_table;
2206
2207 assert(session->where());
2208 /*
2209 If we found a fully qualified field we return it directly as it can't
2210 have duplicates.
2211 */
2212 if (db)
2213 return cur_field;
2214
2215 if (found)
2216 {
2217 if (report_error == REPORT_ALL_ERRORS ||
2218 report_error == IGNORE_EXCEPT_NON_UNIQUE)
2219 my_error(ER_NON_UNIQ_ERROR, MYF(0),
2220 table_name ? item->full_name() : name, session->where());
2221 return (Field*) 0;
2222 }
2223 found= cur_field;
2224 }
2225 }
2226
2227 if (found)
2228 return found;
2229
2230 /*
2231 If the field was qualified and there were no tables to search, issue
2232 an error that an unknown table was given. The situation is detected
2233 as follows: if there were no tables we wouldn't go through the loop
2234 and cur_table wouldn't be updated by the loop increment part, so it
2235 will be equal to the first table.
2236 */
2237 if (table_name && (cur_table == first_table) &&
2238 (report_error == REPORT_ALL_ERRORS ||
2239 report_error == REPORT_EXCEPT_NON_UNIQUE))
2240 {
2241 char buff[NAME_LEN*2+1];
2242 if (db && db[0])
2243 {
2244 /* We're in an error condition, two extra strlen's aren't going
2245 * to kill us */
2246 assert(strlen(db) <= NAME_LEN);
2247 assert(strlen(table_name) <= NAME_LEN);
2248 strcpy(buff, db);
2249 strcat(buff,".");
2250 strcat(buff, table_name);
2251 table_name=buff;
2252 }
2253 my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, session->where());
2254 }
2255 else
2256 {
2257 if (report_error == REPORT_ALL_ERRORS ||
2258 report_error == REPORT_EXCEPT_NON_UNIQUE)
2259 my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(), session->where());
2260 else
2261 found= not_found_field;
2262 }
2263 return found;
2264}
2265
2266
2267/*
2268 Find Item in list of items (find_field_in_tables analog)
2269
2270 TODO
2271 is it better return only counter?
2272
2273 SYNOPSIS
2274 find_item_in_list()
2275 find Item to find
2276 items List of items
2277 counter To return number of found item
2278 report_error
2279 REPORT_ALL_ERRORS report errors, return 0 if error
2280 REPORT_EXCEPT_NOT_FOUND Do not report 'not found' error and
2281 return not_found_item, report other errors,
2282 return 0
2283 IGNORE_ERRORS Do not report errors, return 0 if error
2284 resolution Set to the resolution type if the item is found
2285 (it says whether the item is resolved
2286 against an alias name,
2287 or as a field name without alias,
2288 or as a field hidden by alias,
2289 or ignoring alias)
2290
2291 RETURN VALUES
2292 0 Item is not found or item is not unique,
2293 error message is reported
2294 not_found_item Function was called with
2295 report_error == REPORT_EXCEPT_NOT_FOUND and
2296 item was not found. No error message was reported
2297 found field
2298*/
2299
2300/* Special Item pointer to serve as a return value from find_item_in_list(). */
2301Item **not_found_item= (Item**) 0x1;
2302
2303
2304Item **
2305find_item_in_list(Session *session,
2306 Item *find, List<Item> &items, uint32_t *counter,
2307 find_item_error_report_type report_error,
2308 enum_resolution_type *resolution)
2309{
2310 List<Item>::iterator li(items.begin());
2311 Item **found=0, **found_unaliased= 0, *item;
2312 const char *db_name=0;
2313 const char *field_name=0;
2314 const char *table_name=0;
2315 bool found_unaliased_non_uniq= 0;
2316 /*
2317 true if the item that we search for is a valid name reference
2318 (and not an item that happens to have a name).
2319 */
2320 bool is_ref_by_name= 0;
2321 uint32_t unaliased_counter= 0;
2322
2323 *resolution= NOT_RESOLVED;
2324
2325 is_ref_by_name= (find->type() == Item::FIELD_ITEM ||
2326 find->type() == Item::REF_ITEM);
2327 if (is_ref_by_name)
2328 {
2329 field_name= ((Item_ident*) find)->field_name;
2330 table_name= ((Item_ident*) find)->table_name;
2331 db_name= ((Item_ident*) find)->db_name;
2332 }
2333
2334 for (uint32_t i= 0; (item=li++); i++)
2335 {
2336 if (field_name && item->real_item()->type() == Item::FIELD_ITEM)
2337 {
2338 Item_ident *item_field= (Item_ident*) item;
2339
2340 /*
2341 In case of group_concat() with ORDER BY condition in the QUERY
2342 item_field can be field of temporary table without item name
2343 (if this field created from expression argument of group_concat()),
2344 => we have to check presence of name before compare
2345 */
2346 if (!item_field->name)
2347 continue;
2348
2349 if (table_name)
2350 {
2351 /*
2352 If table name is specified we should find field 'field_name' in
2353 table 'table_name'. According to SQL-standard we should ignore
2354 aliases in this case.
2355
2356 Since we should NOT prefer fields from the select list over
2357 other fields from the tables participating in this select in
2358 case of ambiguity we have to do extra check outside this function.
2359
2360 We use strcmp for table names and database names as these may be
2361 case sensitive. In cases where they are not case sensitive, they
2362 are always in lower case.
2363
2364 item_field->field_name and item_field->table_name can be 0x0 if
2365 item is not fix_field()'ed yet.
2366 */
2367 if (item_field->field_name && item_field->table_name &&
2368 not system_charset_info->strcasecmp(item_field->field_name, field_name) &&
2369 not table_alias_charset->strcasecmp(item_field->table_name, table_name) &&
2370 (!db_name || (item_field->db_name && !strcmp(item_field->db_name, db_name))))
2371 {
2372 if (found_unaliased)
2373 {
2374 if ((*found_unaliased)->eq(item, 0))
2375 continue;
2376 /*
2377 Two matching fields in select list.
2378 We already can bail out because we are searching through
2379 unaliased names only and will have duplicate error anyway.
2380 */
2381 if (report_error != IGNORE_ERRORS)
2382 my_error(ER_NON_UNIQ_ERROR, MYF(0),
2383 find->full_name(), session->where());
2384 return (Item**) 0;
2385 }
2386 found_unaliased= li.ref();
2387 unaliased_counter= i;
2388 *resolution= RESOLVED_IGNORING_ALIAS;
2389 if (db_name)
2390 break; // Perfect match
2391 }
2392 }
2393 else
2394 {
2395 int fname_cmp= system_charset_info->strcasecmp(item_field->field_name, field_name);
2396 if (not system_charset_info->strcasecmp(item_field->name, field_name))
2397 {
2398 /*
2399 If table name was not given we should scan through aliases
2400 and non-aliased fields first. We are also checking unaliased
2401 name of the field in then next else-if, to be able to find
2402 instantly field (hidden by alias) if no suitable alias or
2403 non-aliased field was found.
2404 */
2405 if (found)
2406 {
2407 if ((*found)->eq(item, 0))
2408 continue; // Same field twice
2409 if (report_error != IGNORE_ERRORS)
2410 my_error(ER_NON_UNIQ_ERROR, MYF(0),
2411 find->full_name(), session->where());
2412 return (Item**) 0;
2413 }
2414 found= li.ref();
2415 *counter= i;
2416 *resolution= fname_cmp ? RESOLVED_AGAINST_ALIAS : RESOLVED_WITH_NO_ALIAS;
2417 }
2418 else if (!fname_cmp)
2419 {
2420 /*
2421 We will use non-aliased field or react on such ambiguities only if
2422 we won't be able to find aliased field.
2423 Again if we have ambiguity with field outside of select list
2424 we should prefer fields from select list.
2425 */
2426 if (found_unaliased)
2427 {
2428 if ((*found_unaliased)->eq(item, 0))
2429 continue; // Same field twice
2430 found_unaliased_non_uniq= 1;
2431 }
2432 found_unaliased= li.ref();
2433 unaliased_counter= i;
2434 }
2435 }
2436 }
2437 else if (!table_name)
2438 {
2439 if (is_ref_by_name && find->name && item->name &&
2440 not system_charset_info->strcasecmp(item->name,find->name))
2441 {
2442 found= li.ref();
2443 *counter= i;
2444 *resolution= RESOLVED_AGAINST_ALIAS;
2445 break;
2446 }
2447 else if (find->eq(item,0))
2448 {
2449 found= li.ref();
2450 *counter= i;
2451 *resolution= RESOLVED_IGNORING_ALIAS;
2452 break;
2453 }
2454 }
2455 }
2456 if (!found)
2457 {
2458 if (found_unaliased_non_uniq)
2459 {
2460 if (report_error != IGNORE_ERRORS)
2461 my_error(ER_NON_UNIQ_ERROR, MYF(0),
2462 find->full_name(), session->where());
2463 return (Item **) 0;
2464 }
2465 if (found_unaliased)
2466 {
2467 found= found_unaliased;
2468 *counter= unaliased_counter;
2469 *resolution= RESOLVED_BEHIND_ALIAS;
2470 }
2471 }
2472 if (found)
2473 return found;
2474 if (report_error != REPORT_EXCEPT_NOT_FOUND)
2475 {
2476 if (report_error == REPORT_ALL_ERRORS)
2477 my_error(ER_BAD_FIELD_ERROR, MYF(0),
2478 find->full_name(), session->where());
2479 return (Item **) 0;
2480 }
2481 else
2482 return (Item **) not_found_item;
2483}
2484
2485
2486/*
2487 Test if a string is a member of a list of strings.
2488
2489 SYNOPSIS
2490 test_if_string_in_list()
2491 find the string to look for
2492 str_list a list of strings to be searched
2493
2494 DESCRIPTION
2495 Sequentially search a list of strings for a string, and test whether
2496 the list contains the same string.
2497
2498 RETURN
2499 true if find is in str_list
2500 false otherwise
2501*/
2502
2503static bool
2504test_if_string_in_list(const char *find, List<String> *str_list)
2505{
2506 List<String>::iterator str_list_it(str_list->begin());
2507 String *curr_str;
2508 size_t find_length= strlen(find);
2509 while ((curr_str= str_list_it++))
2510 {
2511 if (find_length != curr_str->length())
2512 continue;
2513 if (not system_charset_info->strcasecmp(find, curr_str->ptr()))
2514 return true;
2515 }
2516 return false;
2517}
2518
2519
2520/*
2521 Create a new name resolution context for an item so that it is
2522 being resolved in a specific table reference.
2523
2524 SYNOPSIS
2525 set_new_item_local_context()
2526 session pointer to current thread
2527 item item for which new context is created and set
2528 table_ref table ref where an item showld be resolved
2529
2530 DESCRIPTION
2531 Create a new name resolution context for an item, so that the item
2532 is resolved only the supplied 'table_ref'.
2533
2534 RETURN
2535 false if all OK
2536 true otherwise
2537*/
2538
2539static void set_new_item_local_context(Session *session, Item_ident *item, TableList *table_ref)
2540{
2541 Name_resolution_context* context= new (session->mem_root) Name_resolution_context;
2542 context->init();
2543 context->first_name_resolution_table= context->last_name_resolution_table= table_ref;
2544 item->context= context;
2545}
2546
2547
2548/*
2549 Find and mark the common columns of two table references.
2550
2551 SYNOPSIS
2552 mark_common_columns()
2553 session [in] current thread
2554 table_ref_1 [in] the first (left) join operand
2555 table_ref_2 [in] the second (right) join operand
2556 using_fields [in] if the join is JOIN...USING - the join columns,
2557 if NATURAL join, then NULL
2558 found_using_fields [out] number of fields from the USING clause that were
2559 found among the common fields
2560
2561 DESCRIPTION
2562 The procedure finds the common columns of two relations (either
2563 tables or intermediate join results), and adds an equi-join condition
2564 to the ON clause of 'table_ref_2' for each pair of matching columns.
2565 If some of table_ref_XXX represents a base table or view, then we
2566 create new 'Natural_join_column' instances for each column
2567 reference and store them in the 'join_columns' of the table
2568 reference.
2569
2570 IMPLEMENTATION
2571 The procedure assumes that store_natural_using_join_columns() was
2572 called for the previous level of NATURAL/USING joins.
2573
2574 RETURN
2575 true error when some common column is non-unique, or out of memory
2576 false OK
2577*/
2578
2579static bool
2580mark_common_columns(Session *session, TableList *table_ref_1, TableList *table_ref_2,
2581 List<String> *using_fields, uint32_t *found_using_fields)
2582{
2583 Field_iterator_table_ref it_1, it_2;
2584 Natural_join_column *nj_col_1, *nj_col_2;
2585 bool first_outer_loop= true;
2586 /*
2587 Leaf table references to which new natural join columns are added
2588 if the leaves are != NULL.
2589 */
2590 TableList *leaf_1= (table_ref_1->getNestedJoin() &&
2591 ! table_ref_1->is_natural_join) ?
2592 NULL : table_ref_1;
2593 TableList *leaf_2= (table_ref_2->getNestedJoin() &&
2594 ! table_ref_2->is_natural_join) ?
2595 NULL : table_ref_2;
2596
2597 *found_using_fields= 0;
2598
2599 for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
2600 {
2601 bool found= false;
2602 const char *field_name_1;
2603 /* true if field_name_1 is a member of using_fields */
2604 bool is_using_column_1;
2605 if (!(nj_col_1= it_1.get_or_create_column_ref(leaf_1)))
2606 return true;
2607 field_name_1= nj_col_1->name();
2608 is_using_column_1= using_fields &&
2609 test_if_string_in_list(field_name_1, using_fields);
2610
2611 /*
2612 Find a field with the same name in table_ref_2.
2613
2614 Note that for the second loop, it_2.set() will iterate over
2615 table_ref_2->join_columns and not generate any new elements or
2616 lists.
2617 */
2618 nj_col_2= NULL;
2619 for (it_2.set(table_ref_2); !it_2.end_of_fields(); it_2.next())
2620 {
2621 Natural_join_column *cur_nj_col_2;
2622 const char *cur_field_name_2;
2623 if (!(cur_nj_col_2= it_2.get_or_create_column_ref(leaf_2)))
2624 return true;
2625 cur_field_name_2= cur_nj_col_2->name();
2626
2627 /*
2628 Compare the two columns and check for duplicate common fields.
2629 A common field is duplicate either if it was already found in
2630 table_ref_2 (then found == true), or if a field in table_ref_2
2631 was already matched by some previous field in table_ref_1
2632 (then cur_nj_col_2->is_common == true).
2633 Note that it is too early to check the columns outside of the
2634 USING list for ambiguity because they are not actually "referenced"
2635 here. These columns must be checked only on unqualified reference
2636 by name (e.g. in SELECT list).
2637 */
2638 if (!system_charset_info->strcasecmp(field_name_1, cur_field_name_2))
2639 {
2640 if (cur_nj_col_2->is_common ||
2641 (found && (!using_fields || is_using_column_1)))
2642 {
2643 my_error(ER_NON_UNIQ_ERROR, MYF(0), field_name_1, session->where());
2644 return true;
2645 }
2646 nj_col_2= cur_nj_col_2;
2647 found= true;
2648 }
2649 }
2650 if (first_outer_loop && leaf_2)
2651 {
2652 /*
2653 Make sure that the next inner loop "knows" that all columns
2654 are materialized already.
2655 */
2656 leaf_2->is_join_columns_complete= true;
2657 first_outer_loop= false;
2658 }
2659 if (!found)
2660 continue; // No matching field
2661
2662 /*
2663 field_1 and field_2 have the same names. Check if they are in the USING
2664 clause (if present), mark them as common fields, and add a new
2665 equi-join condition to the ON clause.
2666 */
2667 if (nj_col_2 && (!using_fields ||is_using_column_1))
2668 {
2669 Item *item_1= nj_col_1->create_item(session);
2670 Item *item_2= nj_col_2->create_item(session);
2671 Field *field_1= nj_col_1->field();
2672 Field *field_2= nj_col_2->field();
2673
2674 if (!item_1 || !item_2)
2675 return true; // out of memory
2676
2677 /*
2678 In the case of no_wrap_view_item == 0, the created items must be
2679 of sub-classes of Item_ident.
2680 */
2681 assert(item_1->type() == Item::FIELD_ITEM ||
2682 item_1->type() == Item::REF_ITEM);
2683 assert(item_2->type() == Item::FIELD_ITEM ||
2684 item_2->type() == Item::REF_ITEM);
2685
2686 /*
2687 We need to cast item_1,2 to Item_ident, because we need to hook name
2688 resolution contexts specific to each item.
2689 */
2690 Item_ident* item_ident_1= (Item_ident*) item_1;
2691 Item_ident* item_ident_2= (Item_ident*) item_2;
2692 /*
2693 Create and hook special name resolution contexts to each item in the
2694 new join condition . We need this to both speed-up subsequent name
2695 resolution of these items, and to enable proper name resolution of
2696 the items during the execute phase of PS.
2697 */
2698 set_new_item_local_context(session, item_ident_1, nj_col_1->table_ref);
2699 set_new_item_local_context(session, item_ident_2, nj_col_2->table_ref);
2700
2701 Item_func_eq* eq_cond= new Item_func_eq(item_ident_1, item_ident_2);
2702
2703 /*
2704 Add the new equi-join condition to the ON clause. Notice that
2705 fix_fields() is applied to all ON conditions in setup_conds()
2706 so we don't do it here.
2707 */
2708 add_join_on((table_ref_1->outer_join & JOIN_TYPE_RIGHT ?
2709 table_ref_1 : table_ref_2),
2710 eq_cond);
2711
2712 nj_col_1->is_common= nj_col_2->is_common= true;
2713
2714 if (field_1)
2715 {
2716 Table *table_1= nj_col_1->table_ref->table;
2717 /* Mark field_1 used for table cache. */
2718 table_1->setReadSet(field_1->position());
2719 table_1->covering_keys&= field_1->part_of_key;
2720 table_1->merge_keys|= field_1->part_of_key;
2721 }
2722 if (field_2)
2723 {
2724 Table *table_2= nj_col_2->table_ref->table;
2725 /* Mark field_2 used for table cache. */
2726 table_2->setReadSet(field_2->position());
2727 table_2->covering_keys&= field_2->part_of_key;
2728 table_2->merge_keys|= field_2->part_of_key;
2729 }
2730
2731 if (using_fields != NULL)
2732 ++(*found_using_fields);
2733 }
2734 }
2735 if (leaf_1)
2736 leaf_1->is_join_columns_complete= true;
2737
2738 /*
2739 Everything is OK.
2740 Notice that at this point there may be some column names in the USING
2741 clause that are not among the common columns. This is an SQL error and
2742 we check for this error in store_natural_using_join_columns() when
2743 (found_using_fields < length(join_using_fields)).
2744 */
2745 return false;
2746}
2747
2748
2749
2750/*
2751 Materialize and store the row type of NATURAL/USING join.
2752
2753 SYNOPSIS
2754 store_natural_using_join_columns()
2755 session current thread
2756 natural_using_join the table reference of the NATURAL/USING join
2757 table_ref_1 the first (left) operand (of a NATURAL/USING join).
2758 table_ref_2 the second (right) operand (of a NATURAL/USING join).
2759 using_fields if the join is JOIN...USING - the join columns,
2760 if NATURAL join, then NULL
2761 found_using_fields number of fields from the USING clause that were
2762 found among the common fields
2763
2764 DESCRIPTION
2765 Iterate over the columns of both join operands and sort and store
2766 all columns into the 'join_columns' list of natural_using_join
2767 where the list is formed by three parts:
2768part1: The coalesced columns of table_ref_1 and table_ref_2,
2769sorted according to the column order of the first table.
2770part2: The other columns of the first table, in the order in
2771which they were defined in CREATE TABLE.
2772part3: The other columns of the second table, in the order in
2773which they were defined in CREATE TABLE.
2774Time complexity - O(N1+N2), where Ni = length(table_ref_i).
2775
2776IMPLEMENTATION
2777The procedure assumes that mark_common_columns() has been called
2778for the join that is being processed.
2779
2780RETURN
2781true error: Some common column is ambiguous
2782false OK
2783*/
2784
2785static bool
2786store_natural_using_join_columns(Session *session,
2787 TableList *natural_using_join,
2788 TableList *table_ref_1,
2789 TableList *table_ref_2,
2790 List<String> *using_fields,
2791 uint32_t found_using_fields)
2792{
2793 Field_iterator_table_ref it_1, it_2;
2794 Natural_join_column *nj_col_1, *nj_col_2;
2795
2796 assert(!natural_using_join->join_columns);
2797
2799 natural_using_join->join_columns= new List<Natural_join_column>;
2800
2801 /* Append the columns of the first join operand. */
2802 for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
2803 {
2804 nj_col_1= it_1.get_natural_column_ref();
2805 if (nj_col_1->is_common)
2806 {
2807 natural_using_join->join_columns->push_back(nj_col_1);
2808 /* Reset the common columns for the next call to mark_common_columns. */
2809 nj_col_1->is_common= false;
2810 }
2811 else
2812 non_join_columns->push_back(nj_col_1);
2813 }
2814
2815 /*
2816 Check that all columns in the USING clause are among the common
2817 columns. If this is not the case, report the first one that was
2818 not found in an error.
2819 */
2820 if (using_fields && found_using_fields < using_fields->size())
2821 {
2822 List<String>::iterator using_fields_it(using_fields->begin());
2823 while (String* using_field_name= using_fields_it++)
2824 {
2825 const char *using_field_name_ptr= using_field_name->c_ptr();
2826 List<Natural_join_column>::iterator it(natural_using_join->join_columns->begin());
2827 for (;;)
2828 {
2829 /* If reached the end of fields, and none was found, report error. */
2830 Natural_join_column* common_field= it++;
2831 if (not common_field)
2832 {
2833 my_error(ER_BAD_FIELD_ERROR, MYF(0), using_field_name_ptr, session->where());
2834 return true;
2835 }
2836 if (!system_charset_info->strcasecmp(common_field->name(), using_field_name_ptr))
2837 break; // Found match
2838 }
2839 }
2840 }
2841
2842 /* Append the non-equi-join columns of the second join operand. */
2843 for (it_2.set(table_ref_2); !it_2.end_of_fields(); it_2.next())
2844 {
2845 nj_col_2= it_2.get_natural_column_ref();
2846 if (!nj_col_2->is_common)
2847 non_join_columns->push_back(nj_col_2);
2848 else
2849 {
2850 /* Reset the common columns for the next call to mark_common_columns. */
2851 nj_col_2->is_common= false;
2852 }
2853 }
2854
2855 if (non_join_columns->size() > 0)
2856 natural_using_join->join_columns->concat(non_join_columns);
2857 natural_using_join->is_join_columns_complete= true;
2858
2859 return false;
2860}
2861
2862
2863/*
2864 Precompute and store the row types of the top-most NATURAL/USING joins.
2865
2866 SYNOPSIS
2867 store_top_level_join_columns()
2868 session current thread
2869 table_ref nested join or table in a FROM clause
2870 left_neighbor neighbor table reference to the left of table_ref at the
2871 same level in the join tree
2872 right_neighbor neighbor table reference to the right of table_ref at the
2873 same level in the join tree
2874
2875 DESCRIPTION
2876 The procedure performs a post-order traversal of a nested join tree
2877 and materializes the row types of NATURAL/USING joins in a
2878 bottom-up manner until it reaches the TableList elements that
2879 represent the top-most NATURAL/USING joins. The procedure should be
2880 applied to each element of Select_Lex::top_join_list (i.e. to each
2881 top-level element of the FROM clause).
2882
2883 IMPLEMENTATION
2884 Notice that the table references in the list nested_join->join_list
2885 are in reverse order, thus when we iterate over it, we are moving
2886 from the right to the left in the FROM clause.
2887
2888 RETURN
2889 true Error
2890 false OK
2891*/
2892
2893static bool
2894store_top_level_join_columns(Session *session, TableList *table_ref,
2895 TableList *left_neighbor,
2896 TableList *right_neighbor)
2897{
2898 /* Call the procedure recursively for each nested table reference. */
2899 if (table_ref->getNestedJoin())
2900 {
2901 List<TableList>::iterator nested_it(table_ref->getNestedJoin()->join_list.begin());
2902 TableList *same_level_left_neighbor= nested_it++;
2903 TableList *same_level_right_neighbor= NULL;
2904 /* Left/right-most neighbors, possibly at higher levels in the join tree. */
2905 TableList *real_left_neighbor, *real_right_neighbor;
2906
2907 while (same_level_left_neighbor)
2908 {
2909 TableList *cur_table_ref= same_level_left_neighbor;
2910 same_level_left_neighbor= nested_it++;
2911 /*
2912 The order of RIGHT JOIN operands is reversed in 'join list' to
2913 transform it into a LEFT JOIN. However, in this procedure we need
2914 the join operands in their lexical order, so below we reverse the
2915 join operands. Notice that this happens only in the first loop,
2916 and not in the second one, as in the second loop
2917 same_level_left_neighbor == NULL.
2918 This is the correct behavior, because the second loop sets
2919 cur_table_ref reference correctly after the join operands are
2920 swapped in the first loop.
2921 */
2922 if (same_level_left_neighbor &&
2923 cur_table_ref->outer_join & JOIN_TYPE_RIGHT)
2924 {
2925 /* This can happen only for JOIN ... ON. */
2926 assert(table_ref->getNestedJoin()->join_list.size() == 2);
2927 std::swap(same_level_left_neighbor, cur_table_ref);
2928 }
2929
2930 /*
2931 Pick the parent's left and right neighbors if there are no immediate
2932 neighbors at the same level.
2933 */
2934 real_left_neighbor= (same_level_left_neighbor) ?
2935 same_level_left_neighbor : left_neighbor;
2936 real_right_neighbor= (same_level_right_neighbor) ?
2937 same_level_right_neighbor : right_neighbor;
2938
2939 if (cur_table_ref->getNestedJoin() &&
2940 store_top_level_join_columns(session, cur_table_ref, real_left_neighbor, real_right_neighbor))
2941 return true;
2942 same_level_right_neighbor= cur_table_ref;
2943 }
2944 }
2945
2946 /*
2947 If this is a NATURAL/USING join, materialize its result columns and
2948 convert to a JOIN ... ON.
2949 */
2950 if (table_ref->is_natural_join)
2951 {
2952 assert(table_ref->getNestedJoin() &&
2953 table_ref->getNestedJoin()->join_list.size() == 2);
2954 List<TableList>::iterator operand_it(table_ref->getNestedJoin()->join_list.begin());
2955 /*
2956 Notice that the order of join operands depends on whether table_ref
2957 represents a LEFT or a RIGHT join. In a RIGHT join, the operands are
2958 in inverted order.
2959 */
2960 TableList *table_ref_2= operand_it++; /* Second NATURAL join operand.*/
2961 TableList *table_ref_1= operand_it++; /* First NATURAL join operand. */
2962 List<String> *using_fields= table_ref->join_using_fields;
2963 uint32_t found_using_fields;
2964
2965 /*
2966 The two join operands were interchanged in the parser, change the order
2967 back for 'mark_common_columns'.
2968 */
2969 if (table_ref_2->outer_join & JOIN_TYPE_RIGHT)
2970 std::swap(table_ref_1, table_ref_2);
2971 if (mark_common_columns(session, table_ref_1, table_ref_2,
2972 using_fields, &found_using_fields))
2973 return true;
2974
2975 /*
2976 Swap the join operands back, so that we pick the columns of the second
2977 one as the coalesced columns. In this way the coalesced columns are the
2978 same as of an equivalent LEFT JOIN.
2979 */
2980 if (table_ref_1->outer_join & JOIN_TYPE_RIGHT)
2981 std::swap(table_ref_1, table_ref_2);
2982 if (store_natural_using_join_columns(session, table_ref, table_ref_1,
2983 table_ref_2, using_fields,
2984 found_using_fields))
2985 return true;
2986
2987 /*
2988 Change NATURAL JOIN to JOIN ... ON. We do this for both operands
2989 because either one of them or the other is the one with the
2990 natural join flag because RIGHT joins are transformed into LEFT,
2991 and the two tables may be reordered.
2992 */
2993 table_ref_1->natural_join= table_ref_2->natural_join= NULL;
2994
2995 /* Add a true condition to outer joins that have no common columns. */
2996 if (table_ref_2->outer_join &&
2997 !table_ref_1->on_expr && !table_ref_2->on_expr)
2998 table_ref_2->on_expr= new Item_int((int64_t) 1,1); /* Always true. */
2999
3000 /* Change this table reference to become a leaf for name resolution. */
3001 if (left_neighbor)
3002 {
3003 TableList *last_leaf_on_the_left;
3004 last_leaf_on_the_left= left_neighbor->last_leaf_for_name_resolution();
3005 last_leaf_on_the_left->next_name_resolution_table= table_ref;
3006 }
3007 if (right_neighbor)
3008 {
3009 TableList *first_leaf_on_the_right;
3010 first_leaf_on_the_right= right_neighbor->first_leaf_for_name_resolution();
3011 table_ref->next_name_resolution_table= first_leaf_on_the_right;
3012 }
3013 else
3014 table_ref->next_name_resolution_table= NULL;
3015 }
3016 return false;
3017}
3018
3019
3020/*
3021 Compute and store the row types of the top-most NATURAL/USING joins
3022 in a FROM clause.
3023
3024 SYNOPSIS
3025 setup_natural_join_row_types()
3026 session current thread
3027 from_clause list of top-level table references in a FROM clause
3028
3029 DESCRIPTION
3030 Apply the procedure 'store_top_level_join_columns' to each of the
3031 top-level table referencs of the FROM clause. Adjust the list of tables
3032 for name resolution - context->first_name_resolution_table to the
3033 top-most, lef-most NATURAL/USING join.
3034
3035 IMPLEMENTATION
3036 Notice that the table references in 'from_clause' are in reverse
3037 order, thus when we iterate over it, we are moving from the right
3038 to the left in the FROM clause.
3039
3040 RETURN
3041 true Error
3042 false OK
3043*/
3044static bool setup_natural_join_row_types(Session *session,
3045 List<TableList> *from_clause,
3046 Name_resolution_context *context)
3047{
3048 session->setWhere("from clause");
3049 if (from_clause->size() == 0)
3050 return false; /* We come here in the case of UNIONs. */
3051
3052 List<TableList>::iterator table_ref_it(from_clause->begin());
3053 TableList *table_ref; /* Current table reference. */
3054 /* Table reference to the left of the current. */
3055 TableList *left_neighbor;
3056 /* Table reference to the right of the current. */
3057 TableList *right_neighbor= NULL;
3058
3059 /* Note that tables in the list are in reversed order */
3060 for (left_neighbor= table_ref_it++; left_neighbor ; )
3061 {
3062 table_ref= left_neighbor;
3063 left_neighbor= table_ref_it++;
3064 if (store_top_level_join_columns(session, table_ref, left_neighbor, right_neighbor))
3065 return true;
3066 if (left_neighbor)
3067 {
3068 TableList *first_leaf_on_the_right;
3069 first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution();
3070 left_neighbor->next_name_resolution_table= first_leaf_on_the_right;
3071 }
3072 right_neighbor= table_ref;
3073 }
3074
3075 /*
3076 Store the top-most, left-most NATURAL/USING join, so that we start
3077 the search from that one instead of context->table_list. At this point
3078 right_neighbor points to the left-most top-level table reference in the
3079 FROM clause.
3080 */
3081 assert(right_neighbor);
3082 context->first_name_resolution_table=
3083 right_neighbor->first_leaf_for_name_resolution();
3084
3085 return false;
3086}
3087
3088
3089/****************************************************************************
3090 ** Expand all '*' in given fields
3091 ****************************************************************************/
3092
3093int setup_wild(Session *session, List<Item> &fields,
3094 List<Item> *sum_func_list,
3095 uint32_t wild_num)
3096{
3097 if (!wild_num)
3098 return 0;
3099
3100 Item *item;
3101 List<Item>::iterator it(fields.begin());
3102
3103 session->lex().current_select->cur_pos_in_select_list= 0;
3104 while (wild_num && (item= it++))
3105 {
3106 if (item->type() == Item::FIELD_ITEM &&
3107 ((Item_field*) item)->field_name &&
3108 ((Item_field*) item)->field_name[0] == '*' &&
3109 !((Item_field*) item)->field)
3110 {
3111 uint32_t elem= fields.size();
3112 bool any_privileges= ((Item_field *) item)->any_privileges;
3113 Item_subselect *subsel= session->lex().current_select->master_unit()->item;
3114 if (subsel &&
3115 subsel->substype() == Item_subselect::EXISTS_SUBS)
3116 {
3117 /*
3118 It is EXISTS(SELECT * ...) and we can replace * by any constant.
3119
3120 Item_int do not need fix_fields() because it is basic constant.
3121 */
3122 it.replace(new Item_int("Not_used", (int64_t) 1,
3123 MY_INT64_NUM_DECIMAL_DIGITS));
3124 }
3125 else if (insert_fields(session, ((Item_field*) item)->context,
3126 ((Item_field*) item)->db_name,
3127 ((Item_field*) item)->table_name, &it,
3128 any_privileges))
3129 {
3130 return -1;
3131 }
3132 if (sum_func_list)
3133 {
3134 /*
3135 sum_func_list is a list that has the fields list as a tail.
3136 Because of this we have to update the element count also for this
3137 list after expanding the '*' entry.
3138 */
3139 sum_func_list->set_size(sum_func_list->size() + fields.size() - elem);
3140 }
3141 wild_num--;
3142 }
3143 else
3144 session->lex().current_select->cur_pos_in_select_list++;
3145 }
3146 session->lex().current_select->cur_pos_in_select_list= UNDEF_POS;
3147
3148 return 0;
3149}
3150
3151/****************************************************************************
3152 ** Check that all given fields exists and fill struct with current data
3153 ****************************************************************************/
3154
3155bool setup_fields(Session *session, Item **ref_pointer_array,
3156 List<Item> &fields, enum_mark_columns mark_used_columns,
3157 List<Item> *sum_func_list, bool allow_sum_func)
3158{
3159 Item *item;
3160 enum_mark_columns save_mark_used_columns= session->mark_used_columns;
3161 nesting_map save_allow_sum_func= session->lex().allow_sum_func;
3162 List<Item>::iterator it(fields.begin());
3163 bool save_is_item_list_lookup;
3164
3165 session->mark_used_columns= mark_used_columns;
3166 if (allow_sum_func)
3167 session->lex().allow_sum_func|= 1 << session->lex().current_select->nest_level;
3168 session->setWhere(Session::DEFAULT_WHERE);
3169 save_is_item_list_lookup= session->lex().current_select->is_item_list_lookup;
3170 session->lex().current_select->is_item_list_lookup= 0;
3171
3172 /*
3173 To prevent fail on forward lookup we fill it with zerows,
3174 then if we got pointer on zero after find_item_in_list we will know
3175 that it is forward lookup.
3176
3177 There is other way to solve problem: fill array with pointers to list,
3178 but it will be slower.
3179
3180 TODO-> remove it when (if) we made one list for allfields and ref_pointer_array
3181 */
3182 if (ref_pointer_array)
3183 {
3184 memset(ref_pointer_array, 0, sizeof(Item *) * fields.size());
3185 }
3186
3187 Item **ref= ref_pointer_array;
3188 session->lex().current_select->cur_pos_in_select_list= 0;
3189 while ((item= it++))
3190 {
3191 if ((!item->fixed && item->fix_fields(session, it.ref())) || (item= *(it.ref()))->check_cols(1))
3192 {
3193 session->lex().current_select->is_item_list_lookup= save_is_item_list_lookup;
3194 session->lex().allow_sum_func= save_allow_sum_func;
3195 session->mark_used_columns= save_mark_used_columns;
3196 return true;
3197 }
3198 if (ref)
3199 *(ref++)= item;
3200 if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM &&
3201 sum_func_list)
3202 item->split_sum_func(session, ref_pointer_array, *sum_func_list);
3203 session->used_tables|= item->used_tables();
3204 session->lex().current_select->cur_pos_in_select_list++;
3205 }
3206 session->lex().current_select->is_item_list_lookup= save_is_item_list_lookup;
3207 session->lex().current_select->cur_pos_in_select_list= UNDEF_POS;
3208
3209 session->lex().allow_sum_func= save_allow_sum_func;
3210 session->mark_used_columns= save_mark_used_columns;
3211 return(test(session->is_error()));
3212}
3213
3214
3215/*
3216 make list of leaves of join table tree
3217
3218 SYNOPSIS
3219 make_leaves_list()
3220 list pointer to pointer on list first element
3221 tables table list
3222
3223 RETURN pointer on pointer to next_leaf of last element
3224*/
3225
3226static TableList **make_leaves_list(TableList **list, TableList *tables)
3227{
3228 for (TableList *table= tables; table; table= table->next_local)
3229 {
3230 {
3231 *list= table;
3232 list= &table->next_leaf;
3233 }
3234 }
3235 return list;
3236}
3237
3238/*
3239 prepare tables
3240
3241 SYNOPSIS
3242 setup_tables()
3243 session Thread Cursor
3244 context name resolution contest to setup table list there
3245 from_clause Top-level list of table references in the FROM clause
3246 tables Table list (select_lex->table_list)
3247 leaves List of join table leaves list (select_lex->leaf_tables)
3248 refresh It is onle refresh for subquery
3249 select_insert It is SELECT ... INSERT command
3250
3251 NOTE
3252 Check also that the 'used keys' and 'ignored keys' exists and set up the
3253 table structure accordingly.
3254 Create a list of leaf tables. For queries with NATURAL/USING JOINs,
3255 compute the row types of the top most natural/using join table references
3256 and link these into a list of table references for name resolution.
3257
3258 This has to be called for all tables that are used by items, as otherwise
3259 table->map is not set and all Item_field will be regarded as const items.
3260
3261 RETURN
3262 false ok; In this case *map will includes the chosen index
3263 true error
3264*/
3265
3266bool setup_tables(Session *session, Name_resolution_context *context,
3267 List<TableList> *from_clause, TableList *tables,
3268 TableList **leaves, bool select_insert)
3269{
3270 assert((select_insert && !tables->next_name_resolution_table) || !tables ||
3271 (context->table_list && context->first_name_resolution_table));
3272 /*
3273 this is used for INSERT ... SELECT.
3274 For select we setup tables except first (and its underlying tables)
3275 */
3276 TableList *first_select_table= select_insert ? tables->next_local : NULL;
3277
3278 if (not *leaves)
3279 make_leaves_list(leaves, tables);
3280
3281 uint32_t tablenr= 0;
3282 for (TableList* table_list= *leaves; table_list; table_list= table_list->next_leaf, tablenr++)
3283 {
3284 Table *table= table_list->table;
3285 table->pos_in_table_list= table_list;
3286 if (first_select_table &&
3287 table_list->top_table() == first_select_table)
3288 {
3289 /* new counting for SELECT of INSERT ... SELECT command */
3290 first_select_table= 0;
3291 tablenr= 0;
3292 }
3293 table->setup_table_map(table_list, tablenr);
3294 if (table_list->process_index_hints(table))
3295 return 1;
3296 }
3297 if (tablenr > MAX_TABLES)
3298 {
3299 my_error(ER_TOO_MANY_TABLES,MYF(0),MAX_TABLES);
3300 return 1;
3301 }
3302
3303 /* Precompute and store the row types of NATURAL/USING joins. */
3304 if (setup_natural_join_row_types(session, from_clause, context))
3305 return 1;
3306
3307 return 0;
3308}
3309
3310
3311/*
3312 prepare tables and check access for the view tables
3313
3314 SYNOPSIS
3315 setup_tables_and_check_view_access()
3316 session Thread Cursor
3317 context name resolution contest to setup table list there
3318 from_clause Top-level list of table references in the FROM clause
3319 tables Table list (select_lex->table_list)
3320 conds Condition of current SELECT (can be changed by VIEW)
3321 leaves List of join table leaves list (select_lex->leaf_tables)
3322 refresh It is onle refresh for subquery
3323 select_insert It is SELECT ... INSERT command
3324 want_access what access is needed
3325
3326 NOTE
3327 a wrapper for check_tables that will also check the resulting
3328 table leaves list for access to all the tables that belong to a view
3329
3330 RETURN
3331 false ok; In this case *map will include the chosen index
3332 true error
3333*/
3334bool setup_tables_and_check_access(Session *session,
3335 Name_resolution_context *context,
3336 List<TableList> *from_clause,
3337 TableList *tables,
3338 TableList **leaves,
3339 bool select_insert)
3340{
3341 TableList *leaves_tmp= NULL;
3342
3343 if (setup_tables(session, context, from_clause, tables, &leaves_tmp, select_insert))
3344 return true;
3345
3346 if (leaves)
3347 *leaves= leaves_tmp;
3348
3349 return false;
3350}
3351
3352
3353/*
3354 Drops in all fields instead of current '*' field
3355
3356 SYNOPSIS
3357 insert_fields()
3358 session Thread Cursor
3359 context Context for name resolution
3360 db_name Database name in case of 'database_name.table_name.*'
3361 table_name Table name in case of 'table_name.*'
3362 it Pointer to '*'
3363 any_privileges 0 If we should ensure that we have SELECT privileges
3364 for all columns
3365 1 If any privilege is ok
3366 RETURN
3367 0 ok 'it' is updated to point at last inserted
3368 1 error. Error message is generated but not sent to client
3369*/
3370
3371bool
3372insert_fields(Session *session, Name_resolution_context *context, const char *db_name,
3373 const char *table_name, List<Item>::iterator *it,
3374 bool )
3375{
3376 Field_iterator_table_ref field_iterator;
3377 bool found;
3378 char name_buff[NAME_LEN+1];
3379
3380 if (db_name)
3381 {
3382 /*
3383 convert database to lower case for comparison
3384 We can't do this in Item_field as this would change the
3385 'name' of the item which may be used in the select list
3386 */
3387 strncpy(name_buff, db_name, sizeof(name_buff)-1);
3388 files_charset_info->casedn_str(name_buff);
3389 db_name= name_buff;
3390 }
3391
3392 found= false;
3393
3394 /*
3395 If table names are qualified, then loop over all tables used in the query,
3396 else treat natural joins as leaves and do not iterate over their underlying
3397 tables.
3398 */
3399 for (TableList *tables= (table_name ? context->table_list :
3400 context->first_name_resolution_table);
3401 tables;
3402 tables= (table_name ? tables->next_local :
3403 tables->next_name_resolution_table)
3404 )
3405 {
3406 Field *field;
3407 Table *table= tables->table;
3408
3409 assert(tables->is_leaf_for_name_resolution());
3410
3411 if ((table_name && table_alias_charset->strcasecmp(table_name, tables->alias)) ||
3412 (db_name && system_charset_info->strcasecmp(tables->getSchemaName(),db_name)))
3413 continue;
3414
3415 /*
3416 Update the tables used in the query based on the referenced fields. For
3417 views and natural joins this update is performed inside the loop below.
3418 */
3419 if (table)
3420 session->used_tables|= table->map;
3421
3422 /*
3423 Initialize a generic field iterator for the current table reference.
3424 Notice that it is guaranteed that this iterator will iterate over the
3425 fields of a single table reference, because 'tables' is a leaf (for
3426 name resolution purposes).
3427 */
3428 field_iterator.set(tables);
3429
3430 for (; !field_iterator.end_of_fields(); field_iterator.next())
3431 {
3432 Item *item= field_iterator.create_item(session);
3433 if (not item)
3434 return true;
3435
3436 if (not found)
3437 {
3438 found= true;
3439 it->replace(item); /* Replace '*' with the first found item. */
3440 }
3441 else
3442 it->after(item); /* Add 'item' to the SELECT list. */
3443
3444 if ((field= field_iterator.field()))
3445 {
3446 /* Mark fields as used to allow storage engine to optimze access */
3447 field->getTable()->setReadSet(field->position());
3448 if (table)
3449 {
3450 table->covering_keys&= field->part_of_key;
3451 table->merge_keys|= field->part_of_key;
3452 }
3453 if (tables->is_natural_join)
3454 {
3455 Table *field_table;
3456 /*
3457 In this case we are sure that the column ref will not be created
3458 because it was already created and stored with the natural join.
3459 */
3460 Natural_join_column *nj_col;
3461 if (!(nj_col= field_iterator.get_natural_column_ref()))
3462 return true;
3463 assert(nj_col->table_field);
3464 field_table= nj_col->table_ref->table;
3465 if (field_table)
3466 {
3467 session->used_tables|= field_table->map;
3468 field_table->covering_keys&= field->part_of_key;
3469 field_table->merge_keys|= field->part_of_key;
3470 field_table->used_fields++;
3471 }
3472 }
3473 }
3474 else
3475 {
3476 session->used_tables|= item->used_tables();
3477 }
3478
3479 session->lex().current_select->cur_pos_in_select_list++;
3480 }
3481 /*
3482 In case of stored tables, all fields are considered as used,
3483 while in the case of views, the fields considered as used are the
3484 ones marked in setup_tables during fix_fields of view columns.
3485 For NATURAL joins, used_tables is updated in the IF above.
3486 */
3487 if (table)
3488 table->used_fields= table->getShare()->sizeFields();
3489 }
3490 if (found)
3491 return false;
3492
3493 /*
3494 @TODO in the case when we skipped all columns because there was a
3495 qualified '*', and all columns were coalesced, we have to give a more
3496 meaningful message than ER_BAD_TABLE_ERROR.
3497 */
3498 if (not table_name)
3499 {
3500 my_message(ER_NO_TABLES_USED, ER(ER_NO_TABLES_USED), MYF(0));
3501 }
3502 else
3503 {
3504 my_error(ER_BAD_TABLE_ERROR, MYF(0), table_name);
3505 }
3506
3507 return true;
3508}
3509
3510
3511/*
3512 Fix all conditions and outer join expressions.
3513
3514 SYNOPSIS
3515 setup_conds()
3516 session thread Cursor
3517 tables list of tables for name resolving (select_lex->table_list)
3518 leaves list of leaves of join table tree (select_lex->leaf_tables)
3519 conds WHERE clause
3520
3521 DESCRIPTION
3522 TODO
3523
3524 RETURN
3525 true if some error occured (e.g. out of memory)
3526 false if all is OK
3527*/
3528
3529int Session::setup_conds(TableList *leaves, COND **conds)
3530{
3531 Session *session= this;
3532 Select_Lex *select_lex= session->lex().current_select;
3533 TableList *table= NULL; // For HP compilers
3534 void *save_session_marker= session->session_marker;
3535 /*
3536 it_is_update set to true when tables of primary Select_Lex (Select_Lex
3537 which belong to LEX, i.e. most up SELECT) will be updated by
3538 INSERT/UPDATE/LOAD
3539 NOTE-> using this condition helps to prevent call of prepare_check_option()
3540 from subquery of VIEW, because tables of subquery belongs to VIEW
3541 (see condition before prepare_check_option() call)
3542 */
3543 bool save_is_item_list_lookup= select_lex->is_item_list_lookup;
3544 select_lex->is_item_list_lookup= 0;
3545
3546 session->mark_used_columns= MARK_COLUMNS_READ;
3547 select_lex->cond_count= 0;
3548 select_lex->between_count= 0;
3549 select_lex->max_equal_elems= 0;
3550
3551 session->session_marker= (void*)1;
3552 if (*conds)
3553 {
3554 session->setWhere("where clause");
3555 if ((!(*conds)->fixed && (*conds)->fix_fields(session, conds)) ||
3556 (*conds)->check_cols(1))
3557 goto err_no_arena;
3558 }
3559 session->session_marker= save_session_marker;
3560
3561 /*
3562 Apply fix_fields() to all ON clauses at all levels of nesting,
3563 including the ones inside view definitions.
3564 */
3565 for (table= leaves; table; table= table->next_leaf)
3566 {
3567 TableList *embedded; /* The table at the current level of nesting. */
3568 TableList *embedding= table; /* The parent nested table reference. */
3569 do
3570 {
3571 embedded= embedding;
3572 if (embedded->on_expr)
3573 {
3574 /* Make a join an a expression */
3575 session->session_marker= (void*)embedded;
3576 session->setWhere("on clause");
3577 if ((!embedded->on_expr->fixed && embedded->on_expr->fix_fields(session, &embedded->on_expr)) ||
3578 embedded->on_expr->check_cols(1))
3579 goto err_no_arena;
3580 select_lex->cond_count++;
3581 }
3582 embedding= embedded->getEmbedding();
3583 }
3584 while (embedding &&
3585 &embedding->getNestedJoin()->join_list.front() == embedded);
3586
3587 }
3588 session->session_marker= save_session_marker;
3589
3590 session->lex().current_select->is_item_list_lookup= save_is_item_list_lookup;
3591 return(test(session->is_error()));
3592
3593err_no_arena:
3594 select_lex->is_item_list_lookup= save_is_item_list_lookup;
3595
3596 return 1;
3597}
3598
3599
3600/******************************************************************************
3601 ** Fill a record with data (for INSERT or UPDATE)
3602 ** Returns : 1 if some field has wrong type
3603 ******************************************************************************/
3604
3605
3606/*
3607 Fill fields with given items.
3608
3609 SYNOPSIS
3610 fill_record()
3611 fields Item_fields list to be filled
3612 values values to fill with
3613 ignore_errors true if we should ignore errors
3614
3615 NOTE
3616 fill_record() may set table->auto_increment_field_not_null and a
3617 caller should make sure that it is reset after their last call to this
3618 function.
3619
3620 RETURN
3621 false OK
3622 true error occured
3623*/
3624
3625bool
3626fill_record(Session *session, List<Item> &fields, List<Item> &values, bool ignore_errors)
3627{
3628 List<Item>::iterator f(fields.begin());
3629 List<Item>::iterator v(values.begin());
3630 Item *value;
3631 Item_field *field;
3632 Table *table;
3633
3634 /*
3635 Reset the table->auto_increment_field_not_null as it is valid for
3636 only one row.
3637 */
3638 if (fields.size())
3639 {
3640 /*
3641 On INSERT or UPDATE fields are checked to be from the same table,
3642 thus we safely can take table from the first field.
3643 */
3644 field= static_cast<Item_field *>(f++);
3645 table= field->field->getTable();
3646 table->auto_increment_field_not_null= false;
3647 f= fields.begin();
3648 }
3649
3650 while ((field= static_cast<Item_field *>(f++)))
3651 {
3652 value= v++;
3653
3654 Field *rfield= field->field;
3655 table= rfield->getTable();
3656
3657 if (rfield == table->next_number_field)
3658 table->auto_increment_field_not_null= true;
3659 if ((value->save_in_field(rfield, 0) < 0) && !ignore_errors)
3660 {
3661 my_message(ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR), MYF(0));
3662 if (table)
3663 table->auto_increment_field_not_null= false;
3664
3665 return true;
3666 }
3667 }
3668
3669 return session->is_error();
3670}
3671
3672
3673/*
3674 Fill field buffer with values from Field list
3675
3676 SYNOPSIS
3677 fill_record()
3678 ptr pointer on pointer to record
3679 values list of fields
3680 ignore_errors true if we should ignore errors
3681
3682 NOTE
3683 fill_record() may set table->auto_increment_field_not_null and a
3684 caller should make sure that it is reset after their last call to this
3685 function.
3686
3687 RETURN
3688 false OK
3689 true error occured
3690*/
3691
3692bool fill_record(Session *session, Field **ptr, List<Item> &values, bool)
3693{
3694 List<Item>::iterator v(values.begin());
3695 Item *value;
3696 Table *table= 0;
3697 Field *field;
3698
3699 /*
3700 Reset the table->auto_increment_field_not_null as it is valid for
3701 only one row.
3702 */
3703 if (*ptr)
3704 {
3705 /*
3706 On INSERT or UPDATE fields are checked to be from the same table,
3707 thus we safely can take table from the first field.
3708 */
3709 table= (*ptr)->getTable();
3710 table->auto_increment_field_not_null= false;
3711 }
3712
3713 while ((field = *ptr++) && ! session->is_error())
3714 {
3715 value=v++;
3716 table= field->getTable();
3717
3718 if (field == table->next_number_field)
3719 {
3720 table->auto_increment_field_not_null= true;
3721 }
3722
3723 if (value->save_in_field(field, 0) < 0)
3724 {
3725 if (table)
3726 table->auto_increment_field_not_null= false;
3727
3728 return true;
3729 }
3730 }
3731
3732 return(session->is_error());
3733}
3734
3735
3736void drizzle_rm_tmp_tables()
3737{
3738 assert(drizzle_tmpdir.size());
3739 Session::shared_ptr session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local());
3740 session->thread_stack= (char*) session.get();
3741 session->storeGlobals();
3742 plugin::StorageEngine::removeLostTemporaryTables(*session, drizzle_tmpdir.c_str());
3743}
3744
3748
3750{
3751 pthread_kill(signal_thread, SIGTERM);
3752 shutdown_in_progress= true; // Safety if kill didn't work
3753}
3754
3755} /* namespace drizzled */
Defines the interface to the CachedDirectory class.
int drop_temporary_table(const identifier::Table &)
Definition sql_base.cc:556
table::Placeholder & table_cache_insert_placeholder(const identifier::Table &)
Definition sql_base.cc:714
void set_proc_info(const char *info)
Definition session.h:605
void close_data_files_and_morph_locks(const identifier::Table &)
Definition sql_base.cc:1117
void close_old_data_files(bool morph_locks=false, bool send_refresh=false)
Definition sql_base.cc:1250
query_id_t getQueryId() const
Definition session.h:621
void drop_open_table(Table *table, const identifier::Table &identifier)
Definition sql_base.cc:639
void close_tables_for_reopen(TableList **tables)
Definition session.cc:1760
Table * lock_table_name_if_not_cached(const identifier::Table &)
Definition sql_base.cc:752
static const char *const DEFAULT_WHERE
Definition session.h:170
void unlink_open_table(Table *find)
Definition sql_base.cc:583
TableList * last_leaf_for_name_resolution()
Definition table_list.cc:69
TableList * next_leaf
Definition table_list.h:214
TableList * next_local
Definition table_list.h:107
TableList * first_leaf_for_name_resolution()
TableList * next_name_resolution_table
Definition table_list.h:197
int delete_table(bool free_share=false)
static bool isAuthorized(const drizzled::identifier::User &user_ctx, const identifier::Schema &schema_identifier, bool send_error=true)
static plugin::Client * getNullClient()
Definition listen.cc:184
static bool doesTableExist(Session &session, const drizzled::identifier::Table &identifier, bool include_temporary_tables=true)
void removeLock(Table *table)
Definition global.cc:423
void abortLock(Table *table)
Definition global.cc:431
void broadcast_refresh(void)
Definition global.cc:1115
TODO: Rename this file - func.h is stupid.
enum_resolution_type
Definition sql_base.h:82
void mark_select_range_as_dependent(Session *session, Select_Lex *last_select, Select_Lex *current_sel, Field *found_field, Item *found_item, Item_ident *resolved_item)
Definition item.cc:797
void kill_drizzle(void)
Definition sql_base.cc:3749
bool check_stack_overrun(Session *session, long margin, void *)
STL namespace.