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