Drizzled Public API Documentation

subselect.cc

Go to the documentation of this file.
00001 /* Copyright (C) 2000 MySQL AB
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00026 #include <config.h>
00027 
00028 #include <cstdio>
00029 #include <limits.h>
00030 
00031 #include <drizzled/session.h>
00032 #include <drizzled/sql_select.h>
00033 #include <drizzled/error.h>
00034 #include <drizzled/item/cache.h>
00035 #include <drizzled/item/subselect.h>
00036 #include <drizzled/item/cmpfunc.h>
00037 #include <drizzled/item/ref_null_helper.h>
00038 #include <drizzled/cached_item.h>
00039 #include <drizzled/check_stack_overrun.h>
00040 #include <drizzled/item/ref_null_helper.h>
00041 #include <drizzled/item/direct_ref.h>
00042 #include <drizzled/join.h>
00043 #include <drizzled/plugin/storage_engine.h>
00044 #include <drizzled/select_singlerow_subselect.h>
00045 #include <drizzled/select_max_min_finder_subselect.h>
00046 #include <drizzled/select_exists_subselect.h>
00047 #include <drizzled/select_union.h>
00048 #include <drizzled/sql_lex.h>
00049 
00050 namespace drizzled {
00051 
00052 extern plugin::StorageEngine *myisam_engine;
00053 
00054 inline Item * and_items(Item* cond, Item *item)
00055 {
00056   return (cond? (new Item_cond_and(cond, item)) : item);
00057 }
00058 
00059 Item_subselect::Item_subselect() :
00060   Item_result_field(),
00061   value_assigned(false),
00062   session(NULL),
00063   substitution(NULL),
00064   unit(NULL),
00065   engine(NULL),
00066   old_engine(NULL),
00067   used_tables_cache(0),
00068   max_columns(0),
00069   parsing_place(NO_MATTER),
00070   have_to_be_excluded(false),
00071   const_item_cache(true),
00072   engine_changed(false),
00073   changed(false),
00074   is_correlated(false)
00075 {
00076   with_subselect= 1;
00077   reset();
00078   /*
00079     Item value is NULL if select_result_interceptor didn't change this value
00080     (i.e. some rows will be found returned)
00081   */
00082   null_value= 1;
00083 }
00084 
00085 
00086 void Item_subselect::init(Select_Lex *select_lex,
00087         select_result_interceptor *result)
00088 {
00089   /*
00090     Please see Item_singlerow_subselect::invalidate_and_restore_select_lex(),
00091     which depends on alterations to the parse tree implemented here.
00092   */
00093 
00094   unit= select_lex->master_unit();
00095 
00096   if (unit->item)
00097   {
00098     /*
00099       Item can be changed in JOIN::prepare while engine in Join::optimize
00100       => we do not copy old_engine here
00101     */
00102     engine= unit->item->engine;
00103     parsing_place= unit->item->parsing_place;
00104     unit->item->engine= 0;
00105     unit->item= this;
00106     engine->change_result(this, result);
00107   }
00108   else
00109   {
00110     Select_Lex *outer_select= unit->outer_select();
00111     /*
00112       do not take into account expression inside aggregate functions because
00113       they can access original table fields
00114     */
00115     parsing_place= (outer_select->in_sum_expr ?
00116                     NO_MATTER :
00117                     outer_select->parsing_place);
00118     if (unit->is_union())
00119       engine= new subselect_union_engine(unit, result, this);
00120     else
00121       engine= new subselect_single_select_engine(select_lex, result, this);
00122   }
00123   {
00124     Select_Lex *upper= unit->outer_select();
00125     if (upper->parsing_place == IN_HAVING)
00126       upper->subquery_in_having= 1;
00127   }
00128   return;
00129 }
00130 
00131 Select_Lex *
00132 Item_subselect::get_select_lex()
00133 {
00134   return unit->first_select();
00135 }
00136 
00137 void Item_subselect::cleanup()
00138 {
00139   Item_result_field::cleanup();
00140   if (old_engine)
00141   {
00142     if (engine)
00143       engine->cleanup();
00144     engine= old_engine;
00145     old_engine= 0;
00146   }
00147   if (engine)
00148     engine->cleanup();
00149   reset();
00150   value_assigned= 0;
00151   return;
00152 }
00153 
00154 void Item_singlerow_subselect::cleanup()
00155 {
00156   value= 0; row= 0;
00157   Item_subselect::cleanup();
00158   return;
00159 }
00160 
00161 
00162 void Item_in_subselect::cleanup()
00163 {
00164   if (left_expr_cache)
00165   {
00166     left_expr_cache->delete_elements();
00167     delete left_expr_cache;
00168     left_expr_cache= NULL;
00169   }
00170   first_execution= true;
00171   Item_subselect::cleanup();
00172   return;
00173 }
00174 
00175 Item_subselect::~Item_subselect()
00176 {
00177   delete engine;
00178 }
00179 
00180 Item_subselect::trans_res
00181 Item_subselect::select_transformer(Join *)
00182 {
00183   return(RES_OK);
00184 }
00185 
00186 
00187 bool Item_subselect::fix_fields(Session *session_param, Item **ref)
00188 {
00189   char const *save_where= session_param->where();
00190   bool res;
00191 
00192   assert(fixed == 0);
00193   engine->set_session((session= session_param));
00194 
00195   if (check_stack_overrun(session, STACK_MIN_SIZE, (unsigned char*)&res))
00196     return true;
00197 
00198   res= engine->prepare();
00199 
00200   // all transformation is done (used by prepared statements)
00201   changed= 1;
00202 
00203   if (!res)
00204   {
00205     /*
00206       Substitute the current item with an Item_in_optimizer that was
00207       created by Item_in_subselect::select_in_like_transformer and
00208       call fix_fields for the substituted item which in turn calls
00209       engine->prepare for the subquery predicate.
00210     */
00211     if (substitution)
00212     {
00213       int ret= 0;
00214 
00215       // did we changed top item of WHERE condition
00216       if (unit->outer_select()->where == (*ref))
00217       {
00218         unit->outer_select()->where= substitution; // correct WHERE for PS
00219       }
00220       else if (unit->outer_select()->having == (*ref))
00221       {
00222         unit->outer_select()->having= substitution; // correct HAVING for PS
00223       }
00224 
00225       (*ref)= substitution;
00226       substitution->name= name;
00227       if (have_to_be_excluded)
00228       {
00229         engine->exclude();
00230       }
00231       substitution= 0;
00232       session->setWhere("checking transformed subquery");
00233       if (! (*ref)->fixed)
00234       {
00235         ret= (*ref)->fix_fields(session, ref);
00236       }
00237       session->setWhere(save_where);
00238 
00239       return ret;
00240     }
00241     // Is it one field subselect?
00242     if (engine->cols() > max_columns)
00243     {
00244       my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
00245       return true;
00246     }
00247     fix_length_and_dec();
00248   }
00249   else
00250     goto err;
00251 
00252   if (engine->uncacheable())
00253   {
00254     const_item_cache= false;
00255     if (engine->uncacheable(UNCACHEABLE_RAND))
00256     {
00257       used_tables_cache|= RAND_TABLE_BIT;
00258     }
00259   }
00260   fixed= 1;
00261 
00262 err:
00263   session->setWhere(save_where);
00264   return res;
00265 }
00266 
00267 
00268 bool Item_subselect::walk(Item_processor processor, bool walk_subquery,
00269                           unsigned char *argument)
00270 {
00271 
00272   if (walk_subquery)
00273   {
00274     for (Select_Lex *lex= unit->first_select(); lex; lex= lex->next_select())
00275     {
00276       List<Item>::iterator li(lex->item_list.begin());
00277       Item *item;
00278       Order *order;
00279 
00280       if (lex->where && (lex->where)->walk(processor, walk_subquery, argument))
00281         return 1;
00282       if (lex->having && (lex->having)->walk(processor, walk_subquery,
00283                                              argument))
00284         return 1;
00285 
00286       while ((item=li++))
00287       {
00288         if (item->walk(processor, walk_subquery, argument))
00289           return 1;
00290       }
00291       for (order= (Order*) lex->order_list.first ; order; order= order->next)
00292       {
00293         if ((*order->item)->walk(processor, walk_subquery, argument))
00294           return 1;
00295       }
00296       for (order= (Order*) lex->group_list.first ; order; order= order->next)
00297       {
00298         if ((*order->item)->walk(processor, walk_subquery, argument))
00299           return 1;
00300       }
00301     }
00302   }
00303   return (this->*processor)(argument);
00304 }
00305 
00306 
00307 bool Item_subselect::exec()
00308 {
00309   int res;
00310 
00311   if (session->is_error())
00312   /* Do not execute subselect in case of a fatal error */
00313     return 1;
00314 
00315   res= engine->exec();
00316 
00317   if (engine_changed)
00318   {
00319     engine_changed= 0;
00320     return exec();
00321   }
00322   return (res);
00323 }
00324 
00325 
00326 /*
00327   Compute the IN predicate if the left operand's cache changed.
00328 */
00329 
00330 bool Item_in_subselect::exec()
00331 {
00332   assert(exec_method != MATERIALIZATION ||
00333               (exec_method == MATERIALIZATION &&
00334                engine->engine_type() == subselect_engine::HASH_SJ_ENGINE));
00335   /*
00336     Initialize the cache of the left predicate operand. This has to be done as
00337     late as now, because Cached_item directly contains a resolved field (not
00338     an item, and in some cases (when temp tables are created), these fields
00339     end up pointing to the wrong field. One solution is to change Cached_item
00340     to not resolve its field upon creation, but to resolve it dynamically
00341     from a given Item_ref object.
00342     TODO: the cache should be applied conditionally based on:
00343     - rules - e.g. only if the left operand is known to be ordered, and/or
00344     - on a cost-based basis, that takes into account the cost of a cache
00345       lookup, the cache hit rate, and the savings per cache hit.
00346   */
00347   if (!left_expr_cache && exec_method == MATERIALIZATION)
00348     init_left_expr_cache();
00349 
00350   /* If the new left operand is already in the cache, reuse the old result. */
00351   if (left_expr_cache && test_if_item_cache_changed(*left_expr_cache) < 0)
00352   {
00353     /* Always compute IN for the first row as the cache is not valid for it. */
00354     if (!first_execution)
00355       return(false);
00356     first_execution= false;
00357   }
00358 
00359   /*
00360     The exec() method below updates item::value, and item::null_value, thus if
00361     we don't call it, the next call to item::val_int() will return whatever
00362     result was computed by its previous call.
00363   */
00364   return(Item_subselect::exec());
00365 }
00366 
00367 
00368 Item::Type Item_subselect::type() const
00369 {
00370   return SUBSELECT_ITEM;
00371 }
00372 
00373 
00374 void Item_subselect::fix_length_and_dec()
00375 {
00376   engine->fix_length_and_dec(0);
00377 }
00378 
00379 
00380 table_map Item_subselect::used_tables() const
00381 {
00382   return (table_map) (engine->uncacheable() ? used_tables_cache : 0L);
00383 }
00384 
00385 
00386 bool Item_subselect::const_item() const
00387 {
00388   return const_item_cache;
00389 }
00390 
00391 Item *Item_subselect::get_tmp_table_item(Session *session_arg)
00392 {
00393   if (!with_sum_func && !const_item())
00394     return new Item_field(result_field);
00395   return copy_or_same(session_arg);
00396 }
00397 
00398 void Item_subselect::update_used_tables()
00399 {
00400   if (! engine->uncacheable())
00401   {
00402     // did all used tables become static?
00403     if (!(used_tables_cache & ~engine->upper_select_const_tables()))
00404       const_item_cache= true;
00405   }
00406 }
00407 
00408 
00409 void Item_subselect::print(String *str)
00410 {
00411   str->append('(');
00412   engine->print(str);
00413   str->append(')');
00414 }
00415 
00416 
00417 Item_singlerow_subselect::Item_singlerow_subselect(Select_Lex *select_lex)
00418   :Item_subselect(), value(0)
00419 {
00420   init(select_lex, new select_singlerow_subselect(this));
00421   maybe_null= 1;
00422   max_columns= UINT_MAX;
00423   return;
00424 }
00425 
00426 Select_Lex *
00427 Item_singlerow_subselect::invalidate_and_restore_select_lex()
00428 {
00429   Select_Lex *result= get_select_lex();
00430 
00431   assert(result);
00432 
00433   /*
00434     This code restore the parse tree in it's state before the execution of
00435     Item_singlerow_subselect::Item_singlerow_subselect(),
00436     and in particular decouples this object from the Select_Lex,
00437     so that the Select_Lex can be used with a different flavor
00438     or Item_subselect instead, as part of query rewriting.
00439   */
00440   unit->item= NULL;
00441 
00442   return(result);
00443 }
00444 
00445 Item_maxmin_subselect::Item_maxmin_subselect(Session *session_param,
00446                                              Item_subselect *parent,
00447                Select_Lex *select_lex,
00448                bool max_arg)
00449   :Item_singlerow_subselect(), was_values(true)
00450 {
00451   max= max_arg;
00452   init(select_lex, new select_max_min_finder_subselect(this, max_arg));
00453   max_columns= 1;
00454   maybe_null= 1;
00455   max_columns= 1;
00456 
00457   /*
00458     Following information was collected during performing fix_fields()
00459     of Items belonged to subquery, which will be not repeated
00460   */
00461   used_tables_cache= parent->get_used_tables_cache();
00462   const_item_cache= parent->get_const_item_cache();
00463 
00464   /*
00465     this subquery always creates during preparation, so we can assign
00466     session here
00467   */
00468   session= session_param;
00469 
00470   return;
00471 }
00472 
00473 void Item_maxmin_subselect::cleanup()
00474 {
00475   Item_singlerow_subselect::cleanup();
00476 
00477   /*
00478     By default it is true to avoid true reporting by
00479     Item_func_not_all/Item_func_nop_all if this item was never called.
00480 
00481     Engine exec() set it to false by reset_value_registration() call.
00482     select_max_min_finder_subselect::send_data() set it back to true if some
00483     value will be found.
00484   */
00485   was_values= true;
00486   return;
00487 }
00488 
00489 
00490 void Item_maxmin_subselect::print(String *str)
00491 {
00492   str->append(max?"<max>":"<min>", 5);
00493   Item_singlerow_subselect::print(str);
00494 }
00495 
00496 
00497 void Item_singlerow_subselect::reset()
00498 {
00499   null_value= 1;
00500   if (value)
00501     value->null_value= 1;
00502 }
00503 
00504 
00515 Item_subselect::trans_res
00516 Item_singlerow_subselect::select_transformer(Join *join)
00517 {
00518   if (changed)
00519     return(RES_OK);
00520 
00521   Select_Lex *select_lex= join->select_lex;
00522 
00523   if (!select_lex->master_unit()->is_union() &&
00524       !select_lex->table_list.elements &&
00525       select_lex->item_list.size() == 1 &&
00526       !select_lex->item_list.front().with_sum_func &&
00527       /*
00528   We cant change name of Item_field or Item_ref, because it will
00529   prevent it's correct resolving, but we should save name of
00530   removed item => we do not make optimization if top item of
00531   list is field or reference.
00532   TODO: solve above problem
00533       */
00534       !(select_lex->item_list.front().type() == FIELD_ITEM ||
00535   select_lex->item_list.front().type() == REF_ITEM) &&
00536       !join->conds && !join->having
00537       )
00538   {
00539 
00540     have_to_be_excluded= 1;
00541     if (session->lex().describe)
00542     {
00543       char warn_buff[DRIZZLE_ERRMSG_SIZE];
00544       snprintf(warn_buff, sizeof(warn_buff), ER(ER_SELECT_REDUCED), select_lex->select_number);
00545       push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
00546        ER_SELECT_REDUCED, warn_buff);
00547     }
00548     substitution= &select_lex->item_list.front();
00549     /*
00550       as far as we moved content to upper level, field which depend of
00551       'upper' select is not really dependent => we remove this dependence
00552     */
00553     substitution->walk(&Item::remove_dependence_processor, 0,
00554            (unsigned char *) select_lex->outer_select());
00555     return(RES_REDUCE);
00556   }
00557   return(RES_OK);
00558 }
00559 
00560 
00561 void Item_singlerow_subselect::store(uint32_t i, Item *item)
00562 {
00563   row[i]->store(item);
00564 }
00565 
00566 enum Item_result Item_singlerow_subselect::result_type() const
00567 {
00568   return engine->type();
00569 }
00570 
00571 /*
00572  Don't rely on the result type to calculate field type.
00573  Ask the engine instead.
00574 */
00575 enum_field_types Item_singlerow_subselect::field_type() const
00576 {
00577   return engine->field_type();
00578 }
00579 
00580 void Item_singlerow_subselect::fix_length_and_dec()
00581 {
00582   if ((max_columns= engine->cols()) == 1)
00583   {
00584     engine->fix_length_and_dec(row= &value);
00585   }
00586   else
00587   {
00588     if (!(row= (Item_cache**) memory::sql_alloc(sizeof(Item_cache*)*max_columns)))
00589       return;
00590     engine->fix_length_and_dec(row);
00591     value= *row;
00592   }
00593   unsigned_flag= value->unsigned_flag;
00594   /*
00595     If there are not tables in subquery then ability to have NULL value
00596     depends on SELECT list (if single row subquery have tables then it
00597     always can be NULL if there are not records fetched).
00598   */
00599   if (engine->no_tables())
00600     maybe_null= engine->may_be_null();
00601 }
00602 
00603 uint32_t Item_singlerow_subselect::cols()
00604 {
00605   return engine->cols();
00606 }
00607 
00608 bool Item_singlerow_subselect::check_cols(uint32_t c)
00609 {
00610   if (c != engine->cols())
00611   {
00612     my_error(ER_OPERAND_COLUMNS, MYF(0), c);
00613     return 1;
00614   }
00615   return 0;
00616 }
00617 
00618 bool Item_singlerow_subselect::null_inside()
00619 {
00620   for (uint32_t i= 0; i < max_columns ; i++)
00621   {
00622     if (row[i]->null_value)
00623       return 1;
00624   }
00625   return 0;
00626 }
00627 
00628 void Item_singlerow_subselect::bring_value()
00629 {
00630   exec();
00631 }
00632 
00633 double Item_singlerow_subselect::val_real()
00634 {
00635   assert(fixed == 1);
00636   if (!exec() && !value->null_value)
00637   {
00638     null_value= 0;
00639     return value->val_real();
00640   }
00641   else
00642   {
00643     reset();
00644     return 0;
00645   }
00646 }
00647 
00648 int64_t Item_singlerow_subselect::val_int()
00649 {
00650   assert(fixed == 1);
00651   if (!exec() && !value->null_value)
00652   {
00653     null_value= 0;
00654     return value->val_int();
00655   }
00656   else
00657   {
00658     reset();
00659     return 0;
00660   }
00661 }
00662 
00663 String *Item_singlerow_subselect::val_str(String *str)
00664 {
00665   if (!exec() && !value->null_value)
00666   {
00667     null_value= 0;
00668     return value->val_str(str);
00669   }
00670   else
00671   {
00672     reset();
00673     return 0;
00674   }
00675 }
00676 
00677 
00678 type::Decimal *Item_singlerow_subselect::val_decimal(type::Decimal *decimal_value)
00679 {
00680   if (!exec() && !value->null_value)
00681   {
00682     null_value= 0;
00683     return value->val_decimal(decimal_value);
00684   }
00685   else
00686   {
00687     reset();
00688     return 0;
00689   }
00690 }
00691 
00692 
00693 bool Item_singlerow_subselect::val_bool()
00694 {
00695   if (!exec() && !value->null_value)
00696   {
00697     null_value= 0;
00698     return value->val_bool();
00699   }
00700   else
00701   {
00702     reset();
00703     return 0;
00704   }
00705 }
00706 
00707 
00708 Item_exists_subselect::Item_exists_subselect(Select_Lex *select_lex):
00709   Item_subselect()
00710 {
00711   bool val_bool();
00712   init(select_lex, new select_exists_subselect(this));
00713   max_columns= UINT_MAX;
00714   null_value= 0; //can't be NULL
00715   maybe_null= 0; //can't be NULL
00716   value= 0;
00717   return;
00718 }
00719 
00720 
00721 void Item_exists_subselect::print(String *str)
00722 {
00723   str->append(STRING_WITH_LEN("exists"));
00724   Item_subselect::print(str);
00725 }
00726 
00727 
00728 bool Item_in_subselect::test_limit(Select_Lex_Unit *unit_arg)
00729 {
00730   if (unit_arg->fake_select_lex &&
00731       unit_arg->fake_select_lex->test_limit())
00732     return(1);
00733 
00734   Select_Lex *sl= unit_arg->first_select();
00735   for (; sl; sl= sl->next_select())
00736   {
00737     if (sl->test_limit())
00738       return(1);
00739   }
00740   return(0);
00741 }
00742 
00743 Item_in_subselect::Item_in_subselect(Item * left_exp,
00744                                      Select_Lex *select_lex) :
00745   Item_exists_subselect(),
00746   left_expr(left_exp),
00747   left_expr_cache(NULL),
00748   first_execution(true),
00749   optimizer(NULL),
00750   pushed_cond_guards(NULL),
00751   sj_convert_priority(0),
00752   expr_join_nest(NULL),
00753   exec_method(NOT_TRANSFORMED),
00754   upper_item(NULL)
00755 {
00756   init(select_lex, new select_exists_subselect(this));
00757   max_columns= UINT_MAX;
00758   maybe_null= 1;
00759   abort_on_null= 0;
00760   reset();
00761   //if test_limit will fail then error will be reported to client
00762   test_limit(select_lex->master_unit());
00763   return;
00764 }
00765 
00766 Item_allany_subselect::Item_allany_subselect(Item * left_exp,
00767                                              chooser_compare_func_creator fc,
00768                Select_Lex *select_lex,
00769                bool all_arg)
00770   :Item_in_subselect(), func_creator(fc), all(all_arg)
00771 {
00772   left_expr= left_exp;
00773   func= func_creator(all_arg);
00774   init(select_lex, new select_exists_subselect(this));
00775   max_columns= 1;
00776   abort_on_null= 0;
00777   reset();
00778   //if test_limit will fail then error will be reported to client
00779   test_limit(select_lex->master_unit());
00780   return;
00781 }
00782 
00783 
00784 void Item_exists_subselect::fix_length_and_dec()
00785 {
00786    decimals= 0;
00787    max_length= 1;
00788    max_columns= engine->cols();
00789   /* We need only 1 row to determine existence */
00790   unit->global_parameters->select_limit= new Item_int((int32_t) 1);
00791 }
00792 
00793 double Item_exists_subselect::val_real()
00794 {
00795   assert(fixed == 1);
00796   if (exec())
00797   {
00798     reset();
00799     return 0;
00800   }
00801   return (double) value;
00802 }
00803 
00804 int64_t Item_exists_subselect::val_int()
00805 {
00806   assert(fixed == 1);
00807   if (exec())
00808   {
00809     reset();
00810     return 0;
00811   }
00812   return value;
00813 }
00814 
00815 String *Item_exists_subselect::val_str(String *str)
00816 {
00817   assert(fixed == 1);
00818   if (exec())
00819   {
00820     reset();
00821     return 0;
00822   }
00823   str->set((uint64_t)value,&my_charset_bin);
00824   return str;
00825 }
00826 
00827 
00828 type::Decimal *Item_exists_subselect::val_decimal(type::Decimal *decimal_value)
00829 {
00830   assert(fixed == 1);
00831   if (exec())
00832   {
00833     reset();
00834     return 0;
00835   }
00836   int2_class_decimal(E_DEC_FATAL_ERROR, value, 0, decimal_value);
00837   return decimal_value;
00838 }
00839 
00840 
00841 bool Item_exists_subselect::val_bool()
00842 {
00843   assert(fixed == 1);
00844   if (exec())
00845   {
00846     reset();
00847     return 0;
00848   }
00849   return value != 0;
00850 }
00851 
00852 
00853 double Item_in_subselect::val_real()
00854 {
00855   /*
00856     As far as Item_in_subselect called only from Item_in_optimizer this
00857     method should not be used
00858   */
00859   assert(0);
00860   assert(fixed == 1);
00861   null_value= 0;
00862   if (exec())
00863   {
00864     reset();
00865     null_value= 1;
00866     return 0;
00867   }
00868   if (was_null && !value)
00869     null_value= 1;
00870   return (double) value;
00871 }
00872 
00873 
00874 int64_t Item_in_subselect::val_int()
00875 {
00876   /*
00877     As far as Item_in_subselect called only from Item_in_optimizer this
00878     method should not be used
00879   */
00880   assert(fixed == 1);
00881   null_value= 0;
00882   if (exec())
00883   {
00884     reset();
00885     null_value= 1;
00886     return 0;
00887   }
00888   if (was_null && !value)
00889     null_value= 1;
00890   return value;
00891 }
00892 
00893 
00894 String *Item_in_subselect::val_str(String *str)
00895 {
00896   /*
00897     As far as Item_in_subselect called only from Item_in_optimizer this
00898     method should not be used
00899   */
00900   assert(0);
00901   assert(fixed == 1);
00902   null_value= 0;
00903   if (exec())
00904   {
00905     reset();
00906     null_value= 1;
00907     return 0;
00908   }
00909   if (was_null && !value)
00910   {
00911     null_value= 1;
00912     return 0;
00913   }
00914   str->set((uint64_t)value, &my_charset_bin);
00915   return str;
00916 }
00917 
00918 
00919 bool Item_in_subselect::val_bool()
00920 {
00921   assert(fixed == 1);
00922   null_value= 0;
00923   if (exec())
00924   {
00925     reset();
00926     /*
00927       Must mark the IN predicate as NULL so as to make sure an enclosing NOT
00928       predicate will return false. See the comments in
00929       subselect_uniquesubquery_engine::copy_ref_key for further details.
00930     */
00931     null_value= 1;
00932     return 0;
00933   }
00934   if (was_null && !value)
00935     null_value= 1;
00936   return value;
00937 }
00938 
00939 type::Decimal *Item_in_subselect::val_decimal(type::Decimal *decimal_value)
00940 {
00941   /*
00942     As far as Item_in_subselect called only from Item_in_optimizer this
00943     method should not be used
00944   */
00945   assert(0);
00946   null_value= 0;
00947   assert(fixed == 1);
00948   if (exec())
00949   {
00950     reset();
00951     null_value= 1;
00952     return 0;
00953   }
00954   if (was_null && !value)
00955     null_value= 1;
00956   int2_class_decimal(E_DEC_FATAL_ERROR, value, 0, decimal_value);
00957   return decimal_value;
00958 }
00959 
00960 
00961 /*
00962   Rewrite a single-column IN/ALL/ANY subselect
00963 
00964   SYNOPSIS
00965     Item_in_subselect::single_value_transformer()
00966       join  Join object of the subquery (i.e. 'child' join).
00967       func  Subquery comparison creator
00968 
00969   DESCRIPTION
00970     Rewrite a single-column subquery using rule-based approach. The subquery
00971 
00972        oe $cmp$ (SELECT ie FROM ... WHERE subq_where ... HAVING subq_having)
00973 
00974     First, try to convert the subquery to scalar-result subquery in one of
00975     the forms:
00976 
00977        - oe $cmp$ (SELECT MAX(...) )  // handled by Item_singlerow_subselect
00978        - oe $cmp$ <max>(SELECT ...)   // handled by Item_maxmin_subselect
00979 
00980     If that fails, the subquery will be handled with class Item_in_optimizer.
00981     There are two possibilites:
00982     - If the subquery execution method is materialization, then the subquery is
00983       not transformed any further.
00984     - Otherwise the IN predicates is transformed into EXISTS by injecting
00985       equi-join predicates and possibly other helper predicates. For details
00986       see method single_value_in_like_transformer().
00987 
00988   RETURN
00989     RES_OK     Either subquery was transformed, or appopriate
00990                        predicates where injected into it.
00991     RES_REDUCE The subquery was reduced to non-subquery
00992     RES_ERROR  Error
00993 */
00994 
00995 Item_subselect::trans_res
00996 Item_in_subselect::single_value_transformer(Join *join,
00997               const Comp_creator *func)
00998 {
00999   Select_Lex *select_lex= join->select_lex;
01000 
01001   /*
01002     Check that the right part of the subselect contains no more than one
01003     column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
01004   */
01005   if (select_lex->item_list.size() > 1)
01006   {
01007     my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
01008     return(RES_ERROR);
01009   }
01010 
01011   /*
01012     If this is an ALL/ANY single-value subselect, try to rewrite it with
01013     a MIN/MAX subselect. We can do that if a possible NULL result of the
01014     subselect can be ignored.
01015     E.g. SELECT * FROM t1 WHERE b > ANY (SELECT a FROM t2) can be rewritten
01016     with SELECT * FROM t1 WHERE b > (SELECT MAX(a) FROM t2).
01017     We can't check that this optimization is safe if it's not a top-level
01018     item of the WHERE clause (e.g. because the WHERE clause can contain IS
01019     NULL/IS NOT NULL functions). If so, we rewrite ALL/ANY with NOT EXISTS
01020     later in this method.
01021   */
01022   if ((abort_on_null || (upper_item && upper_item->top_level())) &&
01023       select_lex->master_unit()->uncacheable.none() && !func->eqne_op())
01024   {
01025     if (substitution)
01026     {
01027       // It is second (third, ...) SELECT of UNION => All is done
01028       return(RES_OK);
01029     }
01030 
01031     Item *subs;
01032     if (!select_lex->group_list.elements &&
01033         !select_lex->having &&
01034   !select_lex->with_sum_func &&
01035   !(select_lex->next_select()) &&
01036         select_lex->table_list.elements)
01037     {
01038       Item_sum_hybrid *item;
01039       nesting_map save_allow_sum_func;
01040       if (func->l_op())
01041       {
01042   /*
01043     (ALL && (> || =>)) || (ANY && (< || =<))
01044     for ALL condition is inverted
01045   */
01046   item= new Item_sum_max(*select_lex->ref_pointer_array);
01047       }
01048       else
01049       {
01050   /*
01051     (ALL && (< || =<)) || (ANY && (> || =>))
01052     for ALL condition is inverted
01053   */
01054   item= new Item_sum_min(*select_lex->ref_pointer_array);
01055       }
01056       if (upper_item)
01057         upper_item->set_sum_test(item);
01058       *select_lex->ref_pointer_array= item;
01059       {
01060   List<Item>::iterator it(select_lex->item_list.begin());
01061   it++;
01062   it.replace(item);
01063       }
01064 
01065       save_allow_sum_func= session->lex().allow_sum_func;
01066       session->lex().allow_sum_func|= 1 << session->lex().current_select->nest_level;
01067       /*
01068   Item_sum_(max|min) can't substitute other item => we can use 0 as
01069         reference, also Item_sum_(max|min) can't be fixed after creation, so
01070         we do not check item->fixed
01071       */
01072       if (item->fix_fields(session, 0))
01073   return(RES_ERROR);
01074       session->lex().allow_sum_func= save_allow_sum_func;
01075       /* we added aggregate function => we have to change statistic */
01076       count_field_types(select_lex, &join->tmp_table_param, join->all_fields,
01077                         0);
01078 
01079       subs= new Item_singlerow_subselect(select_lex);
01080     }
01081     else
01082     {
01083       Item_maxmin_subselect *item;
01084       subs= item= new Item_maxmin_subselect(session, this, select_lex, func->l_op());
01085       if (upper_item)
01086         upper_item->set_sub_test(item);
01087     }
01088     /* fix fields is already called for  left expression */
01089     substitution= func->create(left_expr, subs);
01090     return(RES_OK);
01091   }
01092 
01093   if (!substitution)
01094   {
01095     /* We're invoked for the 1st (or the only) SELECT in the subquery UNION */
01096     Select_Lex_Unit *master_unit= select_lex->master_unit();
01097     substitution= optimizer;
01098 
01099     Select_Lex *current= session->lex().current_select, *up;
01100 
01101     session->lex().current_select= up= current->return_after_parsing();
01102     //optimizer never use Item **ref => we can pass 0 as parameter
01103     if (!optimizer || optimizer->fix_left(session, 0))
01104     {
01105       session->lex().current_select= current;
01106       return(RES_ERROR);
01107     }
01108     session->lex().current_select= current;
01109 
01110     /*
01111       As far as  Item_ref_in_optimizer do not substitute itself on fix_fields
01112       we can use same item for all selects.
01113     */
01114     expr= new Item_direct_ref(&select_lex->context,
01115                               (Item**)optimizer->get_cache(),
01116             (char *)"<no matter>",
01117             (char *)in_left_expr_name);
01118 
01119     master_unit->uncacheable.set(UNCACHEABLE_DEPENDENT);
01120   }
01121 
01122   if (!abort_on_null && left_expr->maybe_null && !pushed_cond_guards)
01123   {
01124     if (!(pushed_cond_guards= (bool*)join->session->getMemRoot()->allocate(sizeof(bool))))
01125       return(RES_ERROR);
01126     pushed_cond_guards[0]= true;
01127   }
01128 
01129   /*
01130     If this IN predicate can be computed via materialization, do not
01131     perform the IN -> EXISTS transformation.
01132   */
01133   if (exec_method == MATERIALIZATION)
01134     return(RES_OK);
01135 
01136   /* Perform the IN=>EXISTS transformation. */
01137   return(single_value_in_to_exists_transformer(join, func));
01138 }
01139 
01140 
01177 Item_subselect::trans_res
01178 Item_in_subselect::single_value_in_to_exists_transformer(Join * join, const Comp_creator *func)
01179 {
01180   Select_Lex *select_lex= join->select_lex;
01181 
01182   select_lex->uncacheable.set(UNCACHEABLE_DEPENDENT);
01183   if (join->having || select_lex->with_sum_func ||
01184       select_lex->group_list.elements)
01185   {
01186     bool tmp;
01187     Item *item= func->create(expr,
01188                              new Item_ref_null_helper(&select_lex->context,
01189                                                       this,
01190                                                       select_lex->
01191                                                       ref_pointer_array,
01192                                                       (char *)"<ref>",
01193                                                       this->full_name()));
01194     if (!abort_on_null && left_expr->maybe_null)
01195     {
01196       /*
01197         We can encounter "NULL IN (SELECT ...)". Wrap the added condition
01198         within a trig_cond.
01199       */
01200       item= new Item_func_trig_cond(item, get_cond_guard(0));
01201     }
01202 
01203     /*
01204       AND and comparison functions can't be changed during fix_fields()
01205       we can assign select_lex->having here, and pass 0 as last
01206       argument (reference) to fix_fields()
01207     */
01208     select_lex->having= join->having= and_items(join->having, item);
01209     if (join->having == item)
01210       item->name= (char*)in_having_cond;
01211     select_lex->having->top_level_item();
01212     select_lex->having_fix_field= 1;
01213     /*
01214       we do not check join->having->fixed, because Item_and (from and_items)
01215       or comparison function (from func->create) can't be fixed after creation
01216     */
01217     tmp= join->having->fix_fields(session, 0);
01218     select_lex->having_fix_field= 0;
01219     if (tmp)
01220       return(RES_ERROR);
01221   }
01222   else
01223   {
01224     Item *item= &select_lex->item_list.front();
01225 
01226     if (select_lex->table_list.elements)
01227     {
01228       bool tmp;
01229       Item *having= item, *orig_item= item;
01230       select_lex->item_list.clear();
01231       select_lex->item_list.push_back(new Item_int("Not_used",
01232                                                    (int64_t) 1,
01233                                                    MY_INT64_NUM_DECIMAL_DIGITS));
01234       select_lex->ref_pointer_array[0]= &select_lex->item_list.front();
01235 
01236       item= func->create(expr, item);
01237       if (!abort_on_null && orig_item->maybe_null)
01238       {
01239   having= new Item_is_not_null_test(this, having);
01240         if (left_expr->maybe_null)
01241         {
01242           if (!(having= new Item_func_trig_cond(having,
01243                                                 get_cond_guard(0))))
01244             return(RES_ERROR);
01245         }
01246   /*
01247     Item_is_not_null_test can't be changed during fix_fields()
01248     we can assign select_lex->having here, and pass 0 as last
01249     argument (reference) to fix_fields()
01250   */
01251         having->name= (char*)in_having_cond;
01252   select_lex->having= join->having= having;
01253   select_lex->having_fix_field= 1;
01254         /*
01255           we do not check join->having->fixed, because Item_and (from
01256           and_items) or comparison function (from func->create) can't be
01257           fixed after creation
01258         */
01259   tmp= join->having->fix_fields(session, 0);
01260         select_lex->having_fix_field= 0;
01261         if (tmp)
01262     return(RES_ERROR);
01263   item= new Item_cond_or(item,
01264              new Item_func_isnull(orig_item));
01265       }
01266       /*
01267         If we may encounter NULL IN (SELECT ...) and care whether subquery
01268         result is NULL or false, wrap condition in a trig_cond.
01269       */
01270       if (!abort_on_null && left_expr->maybe_null)
01271       {
01272         if (!(item= new Item_func_trig_cond(item, get_cond_guard(0))))
01273           return(RES_ERROR);
01274       }
01275       /*
01276         TODO: figure out why the following is done here in
01277         single_value_transformer but there is no corresponding action in
01278         row_value_transformer?
01279       */
01280       item->name= (char *)in_additional_cond;
01281 
01282       /*
01283   AND can't be changed during fix_fields()
01284   we can assign select_lex->having here, and pass 0 as last
01285   argument (reference) to fix_fields()
01286       */
01287       select_lex->where= join->conds= and_items(join->conds, item);
01288       select_lex->where->top_level_item();
01289       /*
01290         we do not check join->conds->fixed, because Item_and can't be fixed
01291         after creation
01292       */
01293       if (join->conds->fix_fields(session, 0))
01294   return(RES_ERROR);
01295     }
01296     else
01297     {
01298       bool tmp;
01299       if (select_lex->master_unit()->is_union())
01300       {
01301   /*
01302     comparison functions can't be changed during fix_fields()
01303     we can assign select_lex->having here, and pass 0 as last
01304     argument (reference) to fix_fields()
01305   */
01306         Item *new_having=
01307           func->create(expr,
01308                        new Item_ref_null_helper(&select_lex->context, this,
01309                                             select_lex->ref_pointer_array,
01310                                             (char *)"<no matter>",
01311                                             (char *)"<result>"));
01312         if (!abort_on_null && left_expr->maybe_null)
01313         {
01314           if (!(new_having= new Item_func_trig_cond(new_having,
01315                                                     get_cond_guard(0))))
01316             return(RES_ERROR);
01317         }
01318         new_having->name= (char*)in_having_cond;
01319   select_lex->having= join->having= new_having;
01320   select_lex->having_fix_field= 1;
01321 
01322         /*
01323           we do not check join->having->fixed, because comparison function
01324           (from func->create) can't be fixed after creation
01325         */
01326   tmp= join->having->fix_fields(session, 0);
01327         select_lex->having_fix_field= 0;
01328         if (tmp)
01329     return(RES_ERROR);
01330       }
01331       else
01332       {
01333   // it is single select without tables => possible optimization
01334   item= func->create(left_expr, item);
01335   // fix_field of item will be done in time of substituting
01336   substitution= item;
01337   have_to_be_excluded= 1;
01338   if (session->lex().describe)
01339   {
01340     char warn_buff[DRIZZLE_ERRMSG_SIZE];
01341     snprintf(warn_buff, sizeof(warn_buff), ER(ER_SELECT_REDUCED), select_lex->select_number);
01342     push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
01343            ER_SELECT_REDUCED, warn_buff);
01344   }
01345   return(RES_REDUCE);
01346       }
01347     }
01348   }
01349 
01350   return(RES_OK);
01351 }
01352 
01353 
01354 Item_subselect::trans_res
01355 Item_in_subselect::row_value_transformer(Join *join)
01356 {
01357   Select_Lex *select_lex= join->select_lex;
01358   uint32_t cols_num= left_expr->cols();
01359 
01360   if (select_lex->item_list.size() != left_expr->cols())
01361   {
01362     my_error(ER_OPERAND_COLUMNS, MYF(0), left_expr->cols());
01363     return(RES_ERROR);
01364   }
01365 
01366   /*
01367     Wrap the current IN predicate in an Item_in_optimizer. The actual
01368     substitution in the Item tree takes place in Item_subselect::fix_fields.
01369   */
01370   if (!substitution)
01371   {
01372     //first call for this unit
01373     Select_Lex_Unit *master_unit= select_lex->master_unit();
01374     substitution= optimizer;
01375 
01376     Select_Lex *current= session->lex().current_select, *up;
01377     session->lex().current_select= up= current->return_after_parsing();
01378     //optimizer never use Item **ref => we can pass 0 as parameter
01379     if (!optimizer || optimizer->fix_left(session, 0))
01380     {
01381       session->lex().current_select= current;
01382       return(RES_ERROR);
01383     }
01384 
01385     // we will refer to upper level cache array => we have to save it in PS
01386     optimizer->keep_top_level_cache();
01387 
01388     session->lex().current_select= current;
01389     master_unit->uncacheable.set(UNCACHEABLE_DEPENDENT);
01390 
01391     if (!abort_on_null && left_expr->maybe_null && !pushed_cond_guards)
01392     {
01393       if (!(pushed_cond_guards= (bool*)join->session->getMemRoot()->allocate(sizeof(bool) *
01394                                                         left_expr->cols())))
01395         return(RES_ERROR);
01396       for (uint32_t i= 0; i < cols_num; i++)
01397         pushed_cond_guards[i]= true;
01398     }
01399   }
01400 
01401   /*
01402     If this IN predicate can be computed via materialization, do not
01403     perform the IN -> EXISTS transformation.
01404   */
01405   if (exec_method == MATERIALIZATION)
01406     return(RES_OK);
01407 
01408   /* Perform the IN=>EXISTS transformation. */
01409   return(row_value_in_to_exists_transformer(join));
01410 }
01411 
01412 
01431 Item_subselect::trans_res
01432 Item_in_subselect::row_value_in_to_exists_transformer(Join * join)
01433 {
01434   Select_Lex *select_lex= join->select_lex;
01435   Item *having_item= 0;
01436   uint32_t cols_num= left_expr->cols();
01437   bool is_having_used= (join->having || select_lex->with_sum_func ||
01438                         select_lex->group_list.first ||
01439                         !select_lex->table_list.elements);
01440 
01441   select_lex->uncacheable.set(UNCACHEABLE_DEPENDENT);
01442   if (is_having_used)
01443   {
01444     /*
01445       (l1, l2, l3) IN (SELECT v1, v2, v3 ... HAVING having) =>
01446       EXISTS (SELECT ... HAVING having and
01447                                 (l1 = v1 or is null v1) and
01448                                 (l2 = v2 or is null v2) and
01449                                 (l3 = v3 or is null v3) and
01450                                 is_not_null_test(v1) and
01451                                 is_not_null_test(v2) and
01452                                 is_not_null_test(v3))
01453       where is_not_null_test used to register nulls in case if we have
01454       not found matching to return correct NULL value
01455       TODO: say here explicitly if the order of AND parts matters or not.
01456     */
01457     Item *item_having_part2= 0;
01458     for (uint32_t i= 0; i < cols_num; i++)
01459     {
01460       assert((left_expr->fixed && select_lex->ref_pointer_array[i]->fixed) ||
01461                   (select_lex->ref_pointer_array[i]->type() == REF_ITEM &&
01462                    ((Item_ref*)(select_lex->ref_pointer_array[i]))->ref_type() ==
01463                     Item_ref::OUTER_REF));
01464       if (select_lex->ref_pointer_array[i]->
01465           check_cols(left_expr->element_index(i)->cols()))
01466         return(RES_ERROR);
01467       Item *item_eq=
01468         new Item_func_eq(new
01469                          Item_ref(&select_lex->context,
01470                                   (*optimizer->get_cache())->
01471                                   addr(i),
01472                                   (char *)"<no matter>",
01473                                   (char *)in_left_expr_name),
01474                          new
01475                          Item_ref(&select_lex->context,
01476                                   select_lex->ref_pointer_array + i,
01477                                   (char *)"<no matter>",
01478                                   (char *)"<list ref>")
01479                         );
01480       Item *item_isnull=
01481         new Item_func_isnull(new
01482                              Item_ref(&select_lex->context,
01483                                       select_lex->ref_pointer_array+i,
01484                                       (char *)"<no matter>",
01485                                       (char *)"<list ref>")
01486                             );
01487       Item *col_item= new Item_cond_or(item_eq, item_isnull);
01488       if (!abort_on_null && left_expr->element_index(i)->maybe_null)
01489       {
01490         if (!(col_item= new Item_func_trig_cond(col_item, get_cond_guard(i))))
01491           return(RES_ERROR);
01492       }
01493       having_item= and_items(having_item, col_item);
01494 
01495       Item *item_nnull_test=
01496          new Item_is_not_null_test(this,
01497                                    new Item_ref(&select_lex->context,
01498                                                 select_lex->
01499                                                 ref_pointer_array + i,
01500                                                 (char *)"<no matter>",
01501                                                 (char *)"<list ref>"));
01502       if (!abort_on_null && left_expr->element_index(i)->maybe_null)
01503       {
01504         if (!(item_nnull_test=
01505               new Item_func_trig_cond(item_nnull_test, get_cond_guard(i))))
01506           return(RES_ERROR);
01507       }
01508       item_having_part2= and_items(item_having_part2, item_nnull_test);
01509       item_having_part2->top_level_item();
01510     }
01511     having_item= and_items(having_item, item_having_part2);
01512     having_item->top_level_item();
01513   }
01514   else
01515   {
01516     /*
01517       (l1, l2, l3) IN (SELECT v1, v2, v3 ... WHERE where) =>
01518       EXISTS (SELECT ... WHERE where and
01519                                (l1 = v1 or is null v1) and
01520                                (l2 = v2 or is null v2) and
01521                                (l3 = v3 or is null v3)
01522                          HAVING is_not_null_test(v1) and
01523                                 is_not_null_test(v2) and
01524                                 is_not_null_test(v3))
01525       where is_not_null_test register NULLs values but reject rows
01526 
01527       in case when we do not need correct NULL, we have simplier construction:
01528       EXISTS (SELECT ... WHERE where and
01529                                (l1 = v1) and
01530                                (l2 = v2) and
01531                                (l3 = v3)
01532     */
01533     Item *where_item= 0;
01534     for (uint32_t i= 0; i < cols_num; i++)
01535     {
01536       Item *item, *item_isnull;
01537       assert((left_expr->fixed && select_lex->ref_pointer_array[i]->fixed) ||
01538                   (select_lex->ref_pointer_array[i]->type() == REF_ITEM &&
01539                    ((Item_ref*)(select_lex->ref_pointer_array[i]))->ref_type() ==
01540                     Item_ref::OUTER_REF));
01541       if (select_lex->ref_pointer_array[i]->
01542           check_cols(left_expr->element_index(i)->cols()))
01543         return(RES_ERROR);
01544       item=
01545         new Item_func_eq(new
01546                          Item_direct_ref(&select_lex->context,
01547                                          (*optimizer->get_cache())->
01548                                          addr(i),
01549                                          (char *)"<no matter>",
01550                                          (char *)in_left_expr_name),
01551                          new
01552                          Item_direct_ref(&select_lex->context,
01553                                          select_lex->
01554                                          ref_pointer_array+i,
01555                                          (char *)"<no matter>",
01556                                          (char *)"<list ref>")
01557                         );
01558       if (!abort_on_null)
01559       {
01560         Item *having_col_item=
01561           new Item_is_not_null_test(this,
01562                                     new
01563                                     Item_ref(&select_lex->context,
01564                                              select_lex->ref_pointer_array + i,
01565                                              (char *)"<no matter>",
01566                                              (char *)"<list ref>"));
01567 
01568 
01569         item_isnull= new
01570           Item_func_isnull(new
01571                            Item_direct_ref(&select_lex->context,
01572                                            select_lex->
01573                                            ref_pointer_array+i,
01574                                            (char *)"<no matter>",
01575                                            (char *)"<list ref>")
01576                           );
01577         item= new Item_cond_or(item, item_isnull);
01578         /*
01579           TODO: why we create the above for cases where the right part
01580                 cant be NULL?
01581         */
01582         if (left_expr->element_index(i)->maybe_null)
01583         {
01584           if (!(item= new Item_func_trig_cond(item, get_cond_guard(i))))
01585             return(RES_ERROR);
01586           if (!(having_col_item=
01587                   new Item_func_trig_cond(having_col_item, get_cond_guard(i))))
01588             return(RES_ERROR);
01589         }
01590         having_item= and_items(having_item, having_col_item);
01591       }
01592       where_item= and_items(where_item, item);
01593     }
01594     /*
01595       AND can't be changed during fix_fields()
01596       we can assign select_lex->where here, and pass 0 as last
01597       argument (reference) to fix_fields()
01598     */
01599     select_lex->where= join->conds= and_items(join->conds, where_item);
01600     select_lex->where->top_level_item();
01601     if (join->conds->fix_fields(session, 0))
01602       return(RES_ERROR);
01603   }
01604   if (having_item)
01605   {
01606     bool res;
01607     select_lex->having= join->having= and_items(join->having, having_item);
01608     if (having_item == select_lex->having)
01609       having_item->name= (char*)in_having_cond;
01610     select_lex->having->top_level_item();
01611     /*
01612       AND can't be changed during fix_fields()
01613       we can assign select_lex->having here, and pass 0 as last
01614       argument (reference) to fix_fields()
01615     */
01616     select_lex->having_fix_field= 1;
01617     res= join->having->fix_fields(session, 0);
01618     select_lex->having_fix_field= 0;
01619     if (res)
01620     {
01621       return(RES_ERROR);
01622     }
01623   }
01624 
01625   return(RES_OK);
01626 }
01627 
01628 
01629 Item_subselect::trans_res
01630 Item_in_subselect::select_transformer(Join *join)
01631 {
01632   return select_in_like_transformer(join, Eq_creator::instance());
01633 }
01634 
01635 
01657 Item_subselect::trans_res
01658 Item_in_subselect::select_in_like_transformer(Join *join, const Comp_creator *func)
01659 {
01660   Select_Lex *current= session->lex().current_select, *up;
01661   const char *save_where= session->where();
01662   Item_subselect::trans_res res= RES_ERROR;
01663   bool result;
01664 
01665   {
01666     /*
01667       IN/SOME/ALL/ANY subqueries aren't support LIMIT clause. Without it
01668       ORDER BY clause becomes meaningless thus we drop it here.
01669     */
01670     Select_Lex *sl= current->master_unit()->first_select();
01671     for (; sl; sl= sl->next_select())
01672     {
01673       if (sl->join)
01674         sl->join->order= 0;
01675     }
01676   }
01677 
01678   if (changed)
01679     return(RES_OK);
01680 
01681   session->setWhere("IN/ALL/ANY subquery");
01682 
01683   /*
01684     In some optimisation cases we will not need this Item_in_optimizer
01685     object, but we can't know it here, but here we need address correct
01686     reference on left expresion.
01687   */
01688   if (!optimizer)
01689   {
01690     result= (!(optimizer= new Item_in_optimizer(left_expr, this)));
01691     if (result)
01692       goto err;
01693   }
01694 
01695   session->lex().current_select= up= current->return_after_parsing();
01696   result= (!left_expr->fixed &&
01697            left_expr->fix_fields(session, optimizer->arguments()));
01698   /* fix_fields can change reference to left_expr, we need reassign it */
01699   left_expr= optimizer->arguments()[0];
01700 
01701   session->lex().current_select= current;
01702   if (result)
01703     goto err;
01704 
01705   /*
01706     If we didn't choose an execution method up to this point, we choose
01707     the IN=>EXISTS transformation.
01708   */
01709   if (exec_method == NOT_TRANSFORMED)
01710     exec_method= IN_TO_EXISTS;
01711 
01712   /*
01713     Both transformers call fix_fields() only for Items created inside them,
01714     and all those items do not make permanent changes in the current item arena
01715     which allows us to call them with changed arena (if we do not know the
01716     nature of Item, we have to call fix_fields() for it only with the original
01717     arena to avoid memory leak).
01718   */
01719   if (left_expr->cols() == 1)
01720     res= single_value_transformer(join, func);
01721   else
01722   {
01723     /* we do not support row operation for ALL/ANY/SOME */
01724     if (func != Eq_creator::instance())
01725     {
01726       my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
01727       return(RES_ERROR);
01728     }
01729     res= row_value_transformer(join);
01730   }
01731 err:
01732   session->setWhere(save_where);
01733   return(res);
01734 }
01735 
01736 
01737 void Item_in_subselect::print(String *str)
01738 {
01739   if (exec_method == IN_TO_EXISTS)
01740     str->append(STRING_WITH_LEN("<exists>"));
01741   else
01742   {
01743     left_expr->print(str);
01744     str->append(STRING_WITH_LEN(" in "));
01745   }
01746   Item_subselect::print(str);
01747 }
01748 
01749 
01750 bool Item_in_subselect::fix_fields(Session *session_arg, Item **ref)
01751 {
01752   bool result = 0;
01753 
01754   if (exec_method == SEMI_JOIN)
01755     return !( (*ref)= new Item_int(1));
01756 
01757   return result || Item_subselect::fix_fields(session_arg, ref);
01758 }
01759 
01760 
01785 bool Item_in_subselect::setup_engine()
01786 {
01787   subselect_hash_sj_engine *new_engine= NULL;
01788   bool res= false;
01789 
01790   if (engine->engine_type() == subselect_engine::SINGLE_SELECT_ENGINE)
01791   {
01792     /* Create/initialize objects in permanent memory. */
01793     subselect_single_select_engine *old_engine_ptr;
01794 
01795     old_engine_ptr= static_cast<subselect_single_select_engine *>(engine);
01796 
01797     if (!(new_engine= new subselect_hash_sj_engine(session, this,
01798                                                    old_engine_ptr)) ||
01799         new_engine->init_permanent(unit->get_unit_column_types()))
01800     {
01801       Item_subselect::trans_res new_trans_res;
01802       /*
01803         If for some reason we cannot use materialization for this IN predicate,
01804         delete all materialization-related objects, and apply the IN=>EXISTS
01805         transformation.
01806       */
01807       delete new_engine;
01808       new_engine= NULL;
01809       exec_method= NOT_TRANSFORMED;
01810       if (left_expr->cols() == 1)
01811         new_trans_res= single_value_in_to_exists_transformer(
01812                            old_engine_ptr->join,
01813                            Eq_creator::instance());
01814       else
01815         new_trans_res= row_value_in_to_exists_transformer(old_engine_ptr->join);
01816       res= (new_trans_res != Item_subselect::RES_OK);
01817     }
01818     if (new_engine)
01819       engine= new_engine;
01820   }
01821   else
01822   {
01823     assert(engine->engine_type() == subselect_engine::HASH_SJ_ENGINE);
01824     new_engine= static_cast<subselect_hash_sj_engine *>(engine);
01825   }
01826 
01827   /* Initilizations done in runtime memory, repeated for each execution. */
01828   if (new_engine)
01829   {
01830     /*
01831       Reset the LIMIT 1 set in Item_exists_subselect::fix_length_and_dec.
01832       TODO:
01833       Currently we set the subquery LIMIT to infinity, and this is correct
01834       because we forbid at parse time LIMIT inside IN subqueries (see
01835       Item_in_subselect::test_limit). However, once we allow this, here
01836       we should set the correct limit if given in the query.
01837     */
01838     unit->global_parameters->select_limit= NULL;
01839     if ((res= new_engine->init_runtime()))
01840       return(res);
01841   }
01842 
01843   return(res);
01844 }
01845 
01846 
01859 bool Item_in_subselect::init_left_expr_cache()
01860 {
01861   Join *outer_join= NULL;
01862 
01863   outer_join= unit->outer_select()->join;
01864   if (! outer_join || ! outer_join->tables || ! outer_join->join_tab)
01865     return true;
01866 
01867   if (!(left_expr_cache= new List<Cached_item>))
01868     return true;
01869 
01870   for (uint32_t i= 0; i < left_expr->cols(); i++)
01871   {
01872     Cached_item *cur_item_cache= new_Cached_item(session, left_expr->element_index(i));
01873     if (!cur_item_cache || left_expr_cache->push_front(cur_item_cache))
01874       return true;
01875   }
01876   return false;
01877 }
01878 
01879 
01880 /*
01881   Callback to test if an IN predicate is expensive.
01882 
01883   @details
01884     IN predicates are considered expensive only if they will be executed via
01885     materialization. The return value affects the behavior of
01886     make_cond_for_table() in such a way that it is unchanged when we use
01887     the IN=>EXISTS transformation to compute IN.
01888 
01889   @retval true  if the predicate is expensive
01890   @retval false otherwise
01891 */
01892 
01893 bool Item_in_subselect::is_expensive_processor(unsigned char *)
01894 {
01895   return exec_method == MATERIALIZATION;
01896 }
01897 
01898 
01899 Item_subselect::trans_res
01900 Item_allany_subselect::select_transformer(Join *join)
01901 {
01902   exec_method= IN_TO_EXISTS;
01903   if (upper_item)
01904     upper_item->show= 1;
01905   return(select_in_like_transformer(join, func));
01906 }
01907 
01908 
01909 void Item_allany_subselect::print(String *str)
01910 {
01911   if (exec_method == IN_TO_EXISTS)
01912     str->append(STRING_WITH_LEN("<exists>"));
01913   else
01914   {
01915     left_expr->print(str);
01916     str->append(' ');
01917     str->append(func->symbol(all));
01918     str->append(all ? " all " : " any ", 5);
01919   }
01920   Item_subselect::print(str);
01921 }
01922 
01923 
01924 void subselect_engine::set_session(Session *session_arg)
01925 {
01926   session= session_arg;
01927   if (result)
01928     result->set_session(session_arg);
01929 }
01930 
01931 
01932 subselect_single_select_engine::
01933 subselect_single_select_engine(Select_Lex *select,
01934              select_result_interceptor *result_arg,
01935              Item_subselect *item_arg)
01936   :subselect_engine(item_arg, result_arg),
01937    prepared(0), executed(0), select_lex(select), join(0)
01938 {
01939   select_lex->master_unit()->item= item_arg;
01940 }
01941 
01942 
01943 void subselect_single_select_engine::cleanup()
01944 {
01945   prepared= executed= 0;
01946   join= 0;
01947   result->cleanup();
01948   return;
01949 }
01950 
01951 
01952 void subselect_union_engine::cleanup()
01953 {
01954   unit->reinit_exec_mechanism();
01955   result->cleanup();
01956   return;
01957 }
01958 
01959 
01960 bool subselect_union_engine::is_executed() const
01961 {
01962   return unit->executed;
01963 }
01964 
01965 
01966 /*
01967   Check if last execution of the subquery engine produced any rows
01968 
01969   SYNOPSIS
01970     subselect_union_engine::no_rows()
01971 
01972   DESCRIPTION
01973     Check if last execution of the subquery engine produced any rows. The
01974     return value is undefined if last execution ended in an error.
01975 
01976   RETURN
01977     true  - Last subselect execution has produced no rows
01978     false - Otherwise
01979 */
01980 
01981 bool subselect_union_engine::no_rows()
01982 {
01983   /* Check if we got any rows when reading UNION result from temp. table: */
01984   return test(!unit->fake_select_lex->join->send_records);
01985 }
01986 
01987 
01988 void subselect_uniquesubquery_engine::cleanup()
01989 {
01990   /* Tell handler we don't need the index anymore */
01991   if (tab->table->cursor->inited)
01992     tab->table->cursor->endIndexScan();
01993   return;
01994 }
01995 
01996 
01997 subselect_union_engine::subselect_union_engine(Select_Lex_Unit *u,
01998                  select_result_interceptor *result_arg,
01999                  Item_subselect *item_arg)
02000   :subselect_engine(item_arg, result_arg)
02001 {
02002   unit= u;
02003   unit->item= item_arg;
02004 }
02005 
02006 
02033 int subselect_single_select_engine::prepare()
02034 {
02035   if (prepared)
02036     return 0;
02037   join= new Join(session, select_lex->item_list,
02038      select_lex->options | SELECT_NO_UNLOCK, result);
02039   if (!join || !result)
02040     return 1; /* Fatal error is set already. */
02041   prepared= 1;
02042   Select_Lex *save_select= session->lex().current_select;
02043   session->lex().current_select= select_lex;
02044   if (join->prepare(&select_lex->ref_pointer_array,
02045         (TableList*) select_lex->table_list.first,
02046         select_lex->with_wild,
02047         select_lex->where,
02048         select_lex->order_list.elements +
02049         select_lex->group_list.elements,
02050         (Order*) select_lex->order_list.first,
02051         (Order*) select_lex->group_list.first,
02052         select_lex->having,
02053         select_lex, select_lex->master_unit()))
02054     return 1;
02055   session->lex().current_select= save_select;
02056   return 0;
02057 }
02058 
02059 int subselect_union_engine::prepare()
02060 {
02061   return unit->prepare(session, result, (uint32_t)SELECT_NO_UNLOCK);
02062 }
02063 
02064 int subselect_uniquesubquery_engine::prepare()
02065 {
02066   /* Should never be called. */
02067   assert(false);
02068   return 1;
02069 }
02070 
02071 
02072 /*
02073   Check if last execution of the subquery engine produced any rows
02074 
02075   SYNOPSIS
02076     subselect_single_select_engine::no_rows()
02077 
02078   DESCRIPTION
02079     Check if last execution of the subquery engine produced any rows. The
02080     return value is undefined if last execution ended in an error.
02081 
02082   RETURN
02083     true  - Last subselect execution has produced no rows
02084     false - Otherwise
02085 */
02086 
02087 bool subselect_single_select_engine::no_rows()
02088 {
02089   return !item->assigned();
02090 }
02091 
02092 
02093 /*
02094  makes storage for the output values for the subquery and calcuates
02095  their data and column types and their nullability.
02096 */
02097 void subselect_engine::set_row(List<Item> &item_list, Item_cache **row)
02098 {
02099   Item *sel_item;
02100   List<Item>::iterator li(item_list.begin());
02101   res_type= STRING_RESULT;
02102   res_field_type= DRIZZLE_TYPE_VARCHAR;
02103   for (uint32_t i= 0; (sel_item= li++); i++)
02104   {
02105     item->max_length= sel_item->max_length;
02106     res_type= sel_item->result_type();
02107     res_field_type= sel_item->field_type();
02108     item->decimals= sel_item->decimals;
02109     item->unsigned_flag= sel_item->unsigned_flag;
02110     maybe_null= sel_item->maybe_null;
02111     if (!(row[i]= Item_cache::get_cache(sel_item)))
02112       return;
02113     row[i]->setup(sel_item);
02114   }
02115   if (item_list.size() > 1)
02116     res_type= ROW_RESULT;
02117 }
02118 
02119 void subselect_single_select_engine::fix_length_and_dec(Item_cache **row)
02120 {
02121   assert(row || select_lex->item_list.size() == 1);
02122   set_row(select_lex->item_list, row);
02123   item->collation.set(row[0]->collation);
02124   if (cols() != 1)
02125     maybe_null= 0;
02126 }
02127 
02128 void subselect_union_engine::fix_length_and_dec(Item_cache **row)
02129 {
02130   assert(row || unit->first_select()->item_list.size() == 1);
02131 
02132   if (unit->first_select()->item_list.size() == 1)
02133   {
02134     set_row(unit->types, row);
02135     item->collation.set(row[0]->collation);
02136   }
02137   else
02138   {
02139     bool maybe_null_saved= maybe_null;
02140     set_row(unit->types, row);
02141     maybe_null= maybe_null_saved;
02142   }
02143 }
02144 
02145 void subselect_uniquesubquery_engine::fix_length_and_dec(Item_cache **)
02146 {
02147   //this never should be called
02148   assert(0);
02149 }
02150 
02151 int subselect_single_select_engine::exec()
02152 {
02153   char const *save_where= session->where();
02154   Select_Lex *save_select= session->lex().current_select;
02155   session->lex().current_select= select_lex;
02156   if (!join->optimized)
02157   {
02158     Select_Lex_Unit *unit= select_lex->master_unit();
02159 
02160     unit->set_limit(unit->global_parameters);
02161     if (join->optimize())
02162     {
02163       session->setWhere(save_where);
02164       executed= 1;
02165       session->lex().current_select= save_select;
02166       return(join->error ? join->error : 1);
02167     }
02168     if (save_join_if_explain())
02169      return(1);
02170 
02171     if (item->engine_changed)
02172     {
02173       return(1);
02174     }
02175   }
02176   if (select_lex->uncacheable.any() &&
02177       ! select_lex->uncacheable.test(UNCACHEABLE_EXPLAIN) &&
02178       executed)
02179   {
02180     if (join->reinit())
02181     {
02182       session->setWhere(save_where);
02183       session->lex().current_select= save_select;
02184       return 1;
02185     }
02186     item->reset();
02187     item->assigned((executed= 0));
02188   }
02189   if (!executed)
02190   {
02191     item->reset_value_registration();
02192     JoinTable *changed_tabs[MAX_TABLES];
02193     JoinTable **last_changed_tab= changed_tabs;
02194     if (item->have_guarded_conds())
02195     {
02196       /*
02197         For at least one of the pushed predicates the following is true:
02198         We should not apply optimizations based on the condition that was
02199         pushed down into the subquery. Those optimizations are ref[_or_null]
02200         acceses. Change them to be full table scans.
02201       */
02202       for (uint32_t i=join->const_tables ; i < join->tables ; i++)
02203       {
02204         JoinTable *tab=join->join_tab+i;
02205         if (tab && tab->keyuse)
02206         {
02207           for (uint32_t key_part= 0;
02208                key_part < tab->ref.key_parts;
02209                key_part++)
02210           {
02211             bool *cond_guard= tab->ref.cond_guards[key_part];
02212             if (cond_guard && !*cond_guard)
02213             {
02214               /* Change the access method to full table scan */
02215               tab->save_read_first_record= tab->read_first_record;
02216               tab->save_read_record= tab->read_record.read_record;
02217               tab->read_first_record= init_read_record_seq;
02218               tab->read_record.record= tab->table->record[0];
02219               tab->read_record.session= join->session;
02220               tab->read_record.ref_length= tab->table->cursor->ref_length;
02221               *(last_changed_tab++)= tab;
02222               break;
02223             }
02224           }
02225         }
02226       }
02227     }
02228 
02229     join->exec();
02230 
02231     /* Enable the optimizations back */
02232     for (JoinTable **ptab= changed_tabs; ptab != last_changed_tab; ptab++)
02233     {
02234       JoinTable *tab= *ptab;
02235       tab->read_record.record= 0;
02236       tab->read_record.ref_length= 0;
02237       tab->read_first_record= tab->save_read_first_record;
02238       tab->read_record.read_record= tab->save_read_record;
02239     }
02240     executed= 1;
02241     session->setWhere(save_where);
02242     session->lex().current_select= save_select;
02243     return(join->error||session->is_fatal_error);
02244   }
02245   session->setWhere(save_where);
02246   session->lex().current_select= save_select;
02247   return(0);
02248 }
02249 
02250 bool 
02251 subselect_single_select_engine::save_join_if_explain()
02252 {
02253   /*
02254     Save this JOIN to join->tmp_join since the original layout will be
02255     replaced when JOIN::exec() calls make_simple_join() if:
02256      1) We are executing an EXPLAIN query
02257      2) An uncacheable flag has not been set for the select_lex. If
02258         set, JOIN::optimize() has already saved the JOIN
02259      3) Call does not come from select_describe()). If it does,
02260         JOIN::exec() will not call make_simple_join() and the JOIN we
02261         plan to save will not be replaced anyway.
02262      4) A temp table is needed. This is what triggers JOIN::exec() to
02263         make a replacement JOIN by calling make_simple_join(). 
02264      5) The Item_subselect is cacheable
02265   */
02266   if (session->lex().describe &&                          // 1
02267       select_lex->uncacheable.none() &&                  // 2
02268       !(join->select_options & SELECT_DESCRIBE) &&       // 3
02269       join->need_tmp &&                                  // 4
02270       item->const_item())                                // 5
02271   {
02272     /*
02273       Save this JOIN to join->tmp_join since the original layout will
02274       be replaced when JOIN::exec() calls make_simple_join() due to
02275       need_tmp==TRUE. The original layout is needed so we can describe
02276       the query. No need to do this if uncacheable != 0 since in this
02277       case the JOIN has already been saved during JOIN::optimize()
02278     */
02279     select_lex->uncacheable.set(UNCACHEABLE_EXPLAIN);
02280     select_lex->master_unit()->uncacheable.set(UNCACHEABLE_EXPLAIN);
02281     if (join->init_save_join_tab())
02282       return true;
02283   }
02284   return false;
02285 }
02286 
02287 
02288 int subselect_union_engine::exec()
02289 {
02290   char const *save_where= session->where();
02291   int res= unit->exec();
02292   session->setWhere(save_where);
02293 
02294   return res;
02295 }
02296 
02297 
02298 /*
02299   Search for at least one row satisfying select condition
02300 
02301   SYNOPSIS
02302     subselect_uniquesubquery_engine::scan_table()
02303 
02304   DESCRIPTION
02305     Scan the table using sequential access until we find at least one row
02306     satisfying select condition.
02307 
02308     The caller must set this->empty_result_set=false before calling this
02309     function. This function will set it to true if it finds a matching row.
02310 
02311   RETURN
02312     false - OK
02313     true  - Error
02314 */
02315 
02316 int subselect_uniquesubquery_engine::scan_table()
02317 {
02318   int error;
02319   Table *table= tab->table;
02320 
02321   if (table->cursor->inited)
02322     table->cursor->endIndexScan();
02323 
02324   if ((error= table->cursor->startTableScan(1)))
02325   {
02326     table->print_error(error, MYF(0));
02327     return 1;
02328   }
02329 
02330   assert(table->getSession());
02331   table->cursor->extra_opt(HA_EXTRA_CACHE,
02332                            table->getSession()->variables.read_buff_size);
02333   table->null_row= 0;
02334   for (;;)
02335   {
02336     error=table->cursor->rnd_next(table->record[0]);
02337     if (error && error != HA_ERR_END_OF_FILE)
02338     {
02339       error= table->report_error(error);
02340       break;
02341     }
02342     /* No more rows */
02343     if (table->status)
02344       break;
02345 
02346     if (!cond || cond->val_int())
02347     {
02348       empty_result_set= false;
02349       break;
02350     }
02351   }
02352 
02353   table->cursor->endTableScan();
02354   return(error != 0);
02355 }
02356 
02357 
02358 /*
02359   Copy ref key and check for null parts in it
02360 
02361   SYNOPSIS
02362     subselect_uniquesubquery_engine::copy_ref_key()
02363 
02364   DESCRIPTION
02365     Copy ref key and check for null parts in it.
02366     Depending on the nullability and conversion problems this function
02367     recognizes and processes the following states :
02368       1. Partial match on top level. This means IN has a value of false
02369          regardless of the data in the subquery table.
02370          Detected by finding a NULL in the left IN operand of a top level
02371          expression.
02372          We may actually skip reading the subquery, so return true to skip
02373          the table scan in subselect_uniquesubquery_engine::exec and make
02374          the value of the IN predicate a NULL (that is equal to false on
02375          top level).
02376       2. No exact match when IN is nested inside another predicate.
02377          Detected by finding a NULL in the left IN operand when IN is not
02378          a top level predicate.
02379          We cannot have an exact match. But we must proceed further with a
02380          table scan to find out if it's a partial match (and IN has a value
02381          of NULL) or no match (and IN has a value of false).
02382          So we return false to continue with the scan and see if there are
02383          any record that would constitute a partial match (as we cannot
02384          determine that from the index).
02385       3. Error converting the left IN operand to the column type of the
02386          right IN operand. This counts as no match (and IN has the value of
02387          false). We mark the subquery table cursor as having no more rows
02388          (to ensure that the processing that follows will not find a match)
02389          and return false, so IN is not treated as returning NULL.
02390 
02391 
02392   RETURN
02393     false - The value of the IN predicate is not known. Proceed to find the
02394             value of the IN predicate using the determined values of
02395             null_keypart and table->status.
02396     true  - IN predicate has a value of NULL. Stop the processing right there
02397             and return NULL to the outer predicates.
02398 */
02399 
02400 bool subselect_uniquesubquery_engine::copy_ref_key()
02401 {
02402   for (StoredKey **copy= tab->ref.key_copy ; *copy ; copy++)
02403   {
02404     StoredKey::store_key_result store_res= (*copy)->copy();
02405     tab->ref.key_err= store_res;
02406 
02407     /*
02408       When there is a NULL part in the key we don't need to make index
02409       lookup for such key thus we don't need to copy whole key.
02410       If we later should do a sequential scan return OK. Fail otherwise.
02411 
02412       See also the comment for the subselect_uniquesubquery_engine::exec()
02413       function.
02414     */
02415     null_keypart= (*copy)->null_key;
02416     if (null_keypart)
02417     {
02418       bool top_level= ((Item_in_subselect *) item)->is_top_level_item();
02419       if (top_level)
02420       {
02421         /* Partial match on top level */
02422         return(1);
02423       }
02424       else
02425       {
02426         /* No exact match when IN is nested inside another predicate */
02427         break;
02428       }
02429     }
02430 
02431     /*
02432       Check if the error is equal to STORE_KEY_FATAL. This is not expressed
02433       using the StoredKey::store_key_result enum because ref.key_err is a
02434       boolean and we want to detect both true and STORE_KEY_FATAL from the
02435       space of the union of the values of [true, false] and
02436       StoredKey::store_key_result.
02437       TODO: fix the variable an return types.
02438     */
02439     if (store_res == StoredKey::STORE_KEY_FATAL)
02440     {
02441       /*
02442        Error converting the left IN operand to the column type of the right
02443        IN operand.
02444       */
02445       tab->table->status= STATUS_NOT_FOUND;
02446       break;
02447     }
02448   }
02449   return(0);
02450 }
02451 
02452 
02453 /*
02454   Execute subselect
02455 
02456   SYNOPSIS
02457     subselect_uniquesubquery_engine::exec()
02458 
02459   DESCRIPTION
02460     Find rows corresponding to the ref key using index access.
02461     If some part of the lookup key is NULL, then we're evaluating
02462       NULL IN (SELECT ... )
02463     This is a special case, we don't need to search for NULL in the table,
02464     instead, the result value is
02465       - NULL  if select produces empty row set
02466       - false otherwise.
02467 
02468     In some cases (IN subselect is a top level item, i.e. abort_on_null==true)
02469     the caller doesn't distinguish between NULL and false result and we just
02470     return false.
02471     Otherwise we make a full table scan to see if there is at least one
02472     matching row.
02473 
02474     The result of this function (info about whether a row was found) is
02475     stored in this->empty_result_set.
02476   NOTE
02477 
02478   RETURN
02479     false - ok
02480     true  - an error occured while scanning
02481 */
02482 
02483 int subselect_uniquesubquery_engine::exec()
02484 {
02485   int error;
02486   Table *table= tab->table;
02487   empty_result_set= true;
02488   table->status= 0;
02489 
02490   /* TODO: change to use of 'full_scan' here? */
02491   if (copy_ref_key())
02492     return(1);
02493   if (table->status)
02494   {
02495     /*
02496       We know that there will be no rows even if we scan.
02497       Can be set in copy_ref_key.
02498     */
02499     ((Item_in_subselect *) item)->value= 0;
02500     return(0);
02501   }
02502 
02503   if (null_keypart)
02504     return(scan_table());
02505 
02506   if (!table->cursor->inited)
02507   {
02508     error= table->cursor->startIndexScan(tab->ref.key, 0);
02509 
02510     if (error != 0)
02511     {
02512       error= table->report_error(error);
02513       return (error != 0);
02514     }
02515   }
02516 
02517   error= table->cursor->index_read_map(table->record[0],
02518                                      tab->ref.key_buff,
02519                                      make_prev_keypart_map(tab->ref.key_parts),
02520                                      HA_READ_KEY_EXACT);
02521   if (error &&
02522       error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
02523     error= table->report_error(error);
02524   else
02525   {
02526     error= 0;
02527     table->null_row= 0;
02528     if (!table->status && (!cond || cond->val_int()))
02529     {
02530       ((Item_in_subselect *) item)->value= 1;
02531       empty_result_set= false;
02532     }
02533     else
02534       ((Item_in_subselect *) item)->value= 0;
02535   }
02536 
02537   return(error != 0);
02538 }
02539 
02540 
02541 /*
02542   Index-lookup subselect 'engine' - run the subquery
02543 
02544   SYNOPSIS
02545     subselect_indexsubquery_engine:exec()
02546       full_scan
02547 
02548   DESCRIPTION
02549     The engine is used to resolve subqueries in form
02550 
02551       oe IN (SELECT key FROM tbl WHERE subq_where)
02552 
02553     The value of the predicate is calculated as follows:
02554     1. If oe IS NULL, this is a special case, do a full table scan on
02555        table tbl and search for row that satisfies subq_where. If such
02556        row is found, return NULL, otherwise return false.
02557     2. Make an index lookup via key=oe, search for a row that satisfies
02558        subq_where. If found, return true.
02559     3. If check_null==true, make another lookup via key=NULL, search for a
02560        row that satisfies subq_where. If found, return NULL, otherwise
02561        return false.
02562 
02563   TODO
02564     The step #1 can be optimized further when the index has several key
02565     parts. Consider a subquery:
02566 
02567       (oe1, oe2) IN (SELECT keypart1, keypart2 FROM tbl WHERE subq_where)
02568 
02569     and suppose we need to evaluate it for {oe1, oe2}=={const1, NULL}.
02570     Current code will do a full table scan and obtain correct result. There
02571     is a better option: instead of evaluating
02572 
02573       SELECT keypart1, keypart2 FROM tbl WHERE subq_where            (1)
02574 
02575     and checking if it has produced any matching rows, evaluate
02576 
02577       SELECT keypart2 FROM tbl WHERE subq_where AND keypart1=const1  (2)
02578 
02579     If this query produces a row, the result is NULL (as we're evaluating
02580     "(const1, NULL) IN { (const1, X), ... }", which has a value of UNKNOWN,
02581     i.e. NULL).  If the query produces no rows, the result is false.
02582 
02583     We currently evaluate (1) by doing a full table scan. (2) can be
02584     evaluated by doing a "ref" scan on "keypart1=const1", which can be much
02585     cheaper. We can use index statistics to quickly check whether "ref" scan
02586     will be cheaper than full table scan.
02587 
02588   RETURN
02589     0
02590     1
02591 */
02592 
02593 int subselect_indexsubquery_engine::exec()
02594 {
02595   int error;
02596   bool null_finding= 0;
02597   Table *table= tab->table;
02598 
02599   ((Item_in_subselect *) item)->value= 0;
02600   empty_result_set= true;
02601   null_keypart= 0;
02602   table->status= 0;
02603 
02604   if (check_null)
02605   {
02606     /* We need to check for NULL if there wasn't a matching value */
02607     *tab->ref.null_ref_key= 0;      // Search first for not null
02608     ((Item_in_subselect *) item)->was_null= 0;
02609   }
02610 
02611   /* Copy the ref key and check for nulls... */
02612   if (copy_ref_key())
02613     return(1);
02614 
02615   if (table->status)
02616   {
02617     /*
02618       We know that there will be no rows even if we scan.
02619       Can be set in copy_ref_key.
02620     */
02621     ((Item_in_subselect *) item)->value= 0;
02622     return(0);
02623   }
02624 
02625   if (null_keypart)
02626     return(scan_table());
02627 
02628   if (!table->cursor->inited)
02629   {
02630     error= table->cursor->startIndexScan(tab->ref.key, 1);
02631 
02632     if (error != 0)
02633     {
02634       error= table->report_error(error);
02635       return(error != 0);
02636     }
02637   }
02638   error= table->cursor->index_read_map(table->record[0],
02639                                      tab->ref.key_buff,
02640                                      make_prev_keypart_map(tab->ref.key_parts),
02641                                      HA_READ_KEY_EXACT);
02642   if (error &&
02643       error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
02644     error= table->report_error(error);
02645   else
02646   {
02647     for (;;)
02648     {
02649       error= 0;
02650       table->null_row= 0;
02651       if (!table->status)
02652       {
02653         if ((!cond || cond->val_int()) && (!having || having->val_int()))
02654         {
02655           empty_result_set= false;
02656           if (null_finding)
02657             ((Item_in_subselect *) item)->was_null= 1;
02658           else
02659             ((Item_in_subselect *) item)->value= 1;
02660           break;
02661         }
02662         error= table->cursor->index_next_same(table->record[0],
02663                                             tab->ref.key_buff,
02664                                             tab->ref.key_length);
02665         if (error && error != HA_ERR_END_OF_FILE)
02666         {
02667           error= table->report_error(error);
02668           break;
02669         }
02670       }
02671       else
02672       {
02673         if (!check_null || null_finding)
02674           break;      /* We don't need to check nulls */
02675         *tab->ref.null_ref_key= 1;
02676         null_finding= 1;
02677         /* Check if there exists a row with a null value in the index */
02678         if ((error= (safe_index_read(tab) == 1)))
02679           break;
02680       }
02681     }
02682   }
02683   return(error != 0);
02684 }
02685 
02686 
02687 uint32_t subselect_single_select_engine::cols()
02688 {
02689   return select_lex->item_list.size();
02690 }
02691 
02692 
02693 uint32_t subselect_union_engine::cols()
02694 {
02695   return unit->types.size();
02696 }
02697 
02698 
02699 bool subselect_single_select_engine::uncacheable()
02700 {
02701   return select_lex->uncacheable.any();
02702 }
02703 
02704 
02705 bool subselect_single_select_engine::uncacheable(uint32_t bit_pos)
02706 {
02707   return select_lex->uncacheable.test(bit_pos);
02708 }
02709 
02710 
02711 bool subselect_union_engine::uncacheable()
02712 {
02713   return unit->uncacheable.any();
02714 }
02715 
02716 
02717 bool subselect_union_engine::uncacheable(uint32_t bit_pos)
02718 {
02719   return unit->uncacheable.test(bit_pos);
02720 }
02721 
02722 
02723 void subselect_single_select_engine::exclude()
02724 {
02725   select_lex->master_unit()->exclude_level();
02726 }
02727 
02728 void subselect_union_engine::exclude()
02729 {
02730   unit->exclude_level();
02731 }
02732 
02733 
02734 void subselect_uniquesubquery_engine::exclude()
02735 {
02736   //this never should be called
02737   assert(0);
02738 }
02739 
02740 
02741 table_map subselect_engine::calc_const_tables(TableList *table)
02742 {
02743   table_map map= 0;
02744   for (; table; table= table->next_leaf)
02745   {
02746     Table *tbl= table->table;
02747     if (tbl && tbl->const_table)
02748       map|= tbl->map;
02749   }
02750   return map;
02751 }
02752 
02753 
02754 table_map subselect_single_select_engine::upper_select_const_tables()
02755 {
02756   return calc_const_tables((TableList *) select_lex->outer_select()->
02757          leaf_tables);
02758 }
02759 
02760 
02761 table_map subselect_union_engine::upper_select_const_tables()
02762 {
02763   return calc_const_tables((TableList *) unit->outer_select()->leaf_tables);
02764 }
02765 
02766 
02767 void subselect_single_select_engine::print(String *str)
02768 {
02769   select_lex->print(session, str);
02770 }
02771 
02772 
02773 void subselect_union_engine::print(String *str)
02774 {
02775   unit->print(str);
02776 }
02777 
02778 
02779 void subselect_uniquesubquery_engine::print(String *str)
02780 {
02781   const char *table_name= tab->table->getShare()->getTableName();
02782   str->append(STRING_WITH_LEN("<primary_index_lookup>("));
02783   tab->ref.items[0]->print(str);
02784   str->append(STRING_WITH_LEN(" in "));
02785   if (tab->table->getShare()->isTemporaryCategory())
02786   {
02787     /*
02788       Temporary tables' names change across runs, so they can't be used for
02789       EXPLAIN EXTENDED.
02790     */
02791     str->append(STRING_WITH_LEN("<temporary table>"));
02792   }
02793   else
02794     str->append(table_name, tab->table->getShare()->getTableNameSize());
02795   KeyInfo *key_info= tab->table->key_info+ tab->ref.key;
02796   str->append(STRING_WITH_LEN(" on "));
02797   str->append(key_info->name);
02798   if (cond)
02799   {
02800     str->append(STRING_WITH_LEN(" where "));
02801     cond->print(str);
02802   }
02803   str->append(')');
02804 }
02805 
02806 /*
02807 TODO:
02808 The above ::print method should be changed as below. Do it after
02809 all other tests pass.
02810 
02811 void subselect_uniquesubquery_engine::print(String *str)
02812 {
02813   KEY *key_info= tab->table->key_info + tab->ref.key;
02814   str->append(STRING_WITH_LEN("<primary_index_lookup>("));
02815   for (uint32_t i= 0; i < key_info->key_parts; i++)
02816     tab->ref.items[i]->print(str);
02817   str->append(STRING_WITH_LEN(" in "));
02818   str->append(tab->table->getShare()->getTableName(), tab->table->getShare()->getTableNameSize());
02819   str->append(STRING_WITH_LEN(" on "));
02820   str->append(key_info->name);
02821   if (cond)
02822   {
02823     str->append(STRING_WITH_LEN(" where "));
02824     cond->print(str);
02825   }
02826   str->append(')');
02827 }
02828 */
02829 
02830 void subselect_indexsubquery_engine::print(String *str)
02831 {
02832   str->append(STRING_WITH_LEN("<index_lookup>("));
02833   tab->ref.items[0]->print(str);
02834   str->append(STRING_WITH_LEN(" in "));
02835   str->append(tab->table->getShare()->getTableName(), tab->table->getShare()->getTableNameSize());
02836   KeyInfo *key_info= tab->table->key_info+ tab->ref.key;
02837   str->append(STRING_WITH_LEN(" on "));
02838   str->append(key_info->name);
02839   if (check_null)
02840     str->append(STRING_WITH_LEN(" checking NULL"));
02841   if (cond)
02842   {
02843     str->append(STRING_WITH_LEN(" where "));
02844     cond->print(str);
02845   }
02846   if (having)
02847   {
02848     str->append(STRING_WITH_LEN(" having "));
02849     having->print(str);
02850   }
02851   str->append(')');
02852 }
02853 
02866 bool subselect_single_select_engine::change_result(Item_subselect *si,
02867                                                  select_result_interceptor *res)
02868 {
02869   item= si;
02870   result= res;
02871   return select_lex->join->change_result(result);
02872 }
02873 
02874 
02887 bool subselect_union_engine::change_result(Item_subselect *si,
02888                                            select_result_interceptor *res)
02889 {
02890   item= si;
02891   int rc= unit->change_result(res, result);
02892   result= res;
02893   return rc;
02894 }
02895 
02896 
02909 bool subselect_uniquesubquery_engine::change_result(Item_subselect *,
02910                                                     select_result_interceptor *)
02911 {
02912   assert(0);
02913   return true;
02914 }
02915 
02916 
02925 bool subselect_single_select_engine::no_tables()
02926 {
02927   return(select_lex->table_list.elements == 0);
02928 }
02929 
02930 
02931 /*
02932   Check statically whether the subquery can return NULL
02933 
02934   SINOPSYS
02935     subselect_single_select_engine::may_be_null()
02936 
02937   RETURN
02938     false  can guarantee that the subquery never return NULL
02939     true   otherwise
02940 */
02941 bool subselect_single_select_engine::may_be_null()
02942 {
02943   return ((no_tables() && !join->conds && !join->having) ? maybe_null : 1);
02944 }
02945 
02946 
02955 bool subselect_union_engine::no_tables()
02956 {
02957   for (Select_Lex *sl= unit->first_select(); sl; sl= sl->next_select())
02958   {
02959     if (sl->table_list.elements)
02960       return false;
02961   }
02962   return true;
02963 }
02964 
02965 
02975 bool subselect_uniquesubquery_engine::no_tables()
02976 {
02977   /* returning value is correct, but this method should never be called */
02978   return 0;
02979 }
02980 
02981 
02982 /******************************************************************************
02983   WL#1110 - Implementation of class subselect_hash_sj_engine
02984 ******************************************************************************/
02985 
02986 
03007 bool subselect_hash_sj_engine::init_permanent(List<Item> *tmp_columns)
03008 {
03009   /* The result sink where we will materialize the subquery result. */
03010   select_union  *tmp_result_sink;
03011   /* The table into which the subquery is materialized. */
03012   Table         *tmp_table;
03013   KeyInfo           *tmp_key; /* The only index on the temporary table. */
03014   uint32_t          tmp_key_parts; /* Number of keyparts in tmp_key. */
03015   Item_in_subselect *item_in= (Item_in_subselect *) item;
03016 
03017   /* 1. Create/initialize materialization related objects. */
03018 
03019   /*
03020     Create and initialize a select result interceptor that stores the
03021     result stream in a temporary table. The temporary table itself is
03022     managed (created/filled/etc) internally by the interceptor.
03023   */
03024   if (!(tmp_result_sink= new select_union))
03025     return(true);
03026 
03027   if (tmp_result_sink->create_result_table(
03028                          session, tmp_columns, true,
03029                          session->options | TMP_TABLE_ALL_COLUMNS,
03030                          "materialized subselect"))
03031     return(true);
03032 
03033   tmp_table= tmp_result_sink->table;
03034   tmp_key= tmp_table->key_info;
03035   tmp_key_parts= tmp_key->key_parts;
03036 
03037   /*
03038      If the subquery has blobs, or the total key lenght is bigger than some
03039      length, then the created index cannot be used for lookups and we
03040      can't use hash semi join. If this is the case, delete the temporary
03041      table since it will not be used, and tell the caller we failed to
03042      initialize the engine.
03043   */
03044   if (tmp_table->getShare()->sizeKeys() == 0)
03045   {
03046     assert(tmp_table->getShare()->db_type() == myisam_engine);
03047     assert(
03048       tmp_table->getShare()->uniques ||
03049       tmp_table->key_info->key_length >= tmp_table->cursor->getEngine()->max_key_length() ||
03050       tmp_table->key_info->key_parts > tmp_table->cursor->getEngine()->max_key_parts());
03051     tmp_table= NULL;
03052     delete result;
03053     result= NULL;
03054     return(true);
03055   }
03056   result= tmp_result_sink;
03057 
03058   /*
03059     Make sure there is only one index on the temp table, and it doesn't have
03060     the extra key part created when s->uniques > 0.
03061   */
03062   assert(tmp_table->getShare()->sizeKeys() == 1 && tmp_columns->size() == tmp_key_parts);
03063 
03064 
03065   /* 2. Create/initialize execution related objects. */
03066 
03067   /*
03068     Create and initialize the JoinTable that represents an index lookup
03069     plan operator into the materialized subquery result. Notice that:
03070     - this JoinTable has no corresponding JOIN (and doesn't need one), and
03071     - here we initialize only those members that are used by
03072       subselect_uniquesubquery_engine, so these objects are incomplete.
03073   */
03074   if (!(tab= (JoinTable*) session->getMemRoot()->allocate(sizeof(JoinTable))))
03075     return(true);
03076   new (tab) JoinTable();
03077   tab->table= tmp_table;
03078   tab->ref.key= 0; /* The only temp table index. */
03079   tab->ref.key_length= tmp_key->key_length;
03080   if (!(tab->ref.key_buff=
03081         (unsigned char*) session->calloc(ALIGN_SIZE(tmp_key->key_length) * 2)) ||
03082       !(tab->ref.key_copy=
03083         (StoredKey**) session->getMemRoot()->allocate((sizeof(StoredKey*) *
03084                                   (tmp_key_parts + 1)))) ||
03085       !(tab->ref.items=
03086         (Item**) session->getMemRoot()->allocate(sizeof(Item*) * tmp_key_parts)))
03087     return(true);
03088 
03089   KeyPartInfo *cur_key_part= tmp_key->key_part;
03090   StoredKey **ref_key= tab->ref.key_copy;
03091   unsigned char *cur_ref_buff= tab->ref.key_buff;
03092 
03093   for (uint32_t i= 0; i < tmp_key_parts; i++, cur_key_part++, ref_key++)
03094   {
03095     tab->ref.items[i]= item_in->left_expr->element_index(i);
03096     int null_count= test(cur_key_part->field->real_maybe_null());
03097     *ref_key= new store_key_item(session, cur_key_part->field,
03098                                  /* TODO:
03099                                     the NULL byte is taken into account in
03100                                     cur_key_part->store_length, so instead of
03101                                     cur_ref_buff + test(maybe_null), we could
03102                                     use that information instead.
03103                                  */
03104                                  cur_ref_buff + null_count,
03105                                  null_count ? tab->ref.key_buff : 0,
03106                                  cur_key_part->length, tab->ref.items[i]);
03107     cur_ref_buff+= cur_key_part->store_length;
03108   }
03109   *ref_key= NULL; /* End marker. */
03110   tab->ref.key_err= 1;
03111   tab->ref.key_parts= tmp_key_parts;
03112 
03113   return(false);
03114 }
03115 
03116 
03125 bool subselect_hash_sj_engine::init_runtime()
03126 {
03127   /*
03128     Create and optimize the JOIN that will be used to materialize
03129     the subquery if not yet created.
03130   */
03131   materialize_engine->prepare();
03132   /* Let our engine reuse this query plan for materialization. */
03133   materialize_join= materialize_engine->join;
03134   materialize_join->change_result(result);
03135   return false;
03136 }
03137 
03138 
03139 subselect_hash_sj_engine::~subselect_hash_sj_engine()
03140 {
03141   delete result;
03142   if (tab)
03143   {
03144     tab->table= NULL;
03145   }
03146 }
03147 
03148 
03156 void subselect_hash_sj_engine::cleanup()
03157 {
03158   is_materialized= false;
03159   result->cleanup(); /* Resets the temp table as well. */
03160   materialize_engine->cleanup();
03161   subselect_uniquesubquery_engine::cleanup();
03162 }
03163 
03164 
03176 int subselect_hash_sj_engine::exec()
03177 {
03178   Item_in_subselect *item_in= (Item_in_subselect *) item;
03179 
03180   /*
03181     Optimize and materialize the subquery during the first execution of
03182     the subquery predicate.
03183   */
03184   if (!is_materialized)
03185   {
03186     int res= 0;
03187     Select_Lex *save_select= session->lex().current_select;
03188     session->lex().current_select= materialize_engine->select_lex;
03189     if ((res= materialize_join->optimize()))
03190       goto err;
03191 
03192     if (materialize_engine->save_join_if_explain())
03193       goto err;
03194 
03195     materialize_join->exec();
03196     if ((res= test(materialize_join->error || session->is_fatal_error)))
03197       goto err;
03198 
03199     /*
03200       TODO:
03201       - Unlock all subquery tables as we don't need them. To implement this
03202         we need to add new functionality to Join::join_free that can unlock
03203         all tables in a subquery (and all its subqueries).
03204       - The temp table used for grouping in the subquery can be freed
03205         immediately after materialization (yet it's done together with
03206         unlocking).
03207      */
03208     is_materialized= true;
03209     /*
03210       If the subquery returned no rows, the temporary table is empty, so we know
03211       directly that the result of IN is false. We first update the table
03212       statistics, then we test if the temporary table for the query result is
03213       empty.
03214     */
03215     tab->table->cursor->info(HA_STATUS_VARIABLE);
03216     if (!tab->table->cursor->stats.records)
03217     {
03218       empty_result_set= true;
03219       item_in->value= false;
03220       /* TODO: check we need this: item_in->null_value= false; */
03221       return(false);
03222     }
03223     /* Set tmp_param only if its usable, i.e. tmp_param->copy_field != NULL. */
03224     tmp_param= &(item_in->unit->outer_select()->join->tmp_table_param);
03225     if (tmp_param && !tmp_param->copy_field)
03226       tmp_param= NULL;
03227 
03228 err:
03229     session->lex().current_select= save_select;
03230     if (res)
03231       return(res);
03232   }
03233 
03234   /*
03235     Lookup the left IN operand in the hash index of the materialized subquery.
03236   */
03237   return(subselect_uniquesubquery_engine::exec());
03238 }
03239 
03240 
03245 void subselect_hash_sj_engine::print(String *str)
03246 {
03247   str->append(STRING_WITH_LEN(" <materialize> ("));
03248   materialize_engine->print(str);
03249   str->append(STRING_WITH_LEN(" ), "));
03250   if (tab)
03251     subselect_uniquesubquery_engine::print(str);
03252   else
03253     str->append(STRING_WITH_LEN(
03254            "<the access method for lookups is not yet created>"
03255          ));
03256 }
03257 
03258 } /* namespace drizzled */