Drizzled Public API Documentation

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