00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00080
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
00091
00092
00093
00094 unit= select_lex->master_unit();
00095
00096 if (unit->item)
00097 {
00098
00099
00100
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
00113
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
00201 changed= 1;
00202
00203 if (!res)
00204 {
00205
00206
00207
00208
00209
00210
00211 if (substitution)
00212 {
00213 int ret= 0;
00214
00215
00216 if (unit->outer_select()->where == (*ref))
00217 {
00218 unit->outer_select()->where= substitution;
00219 }
00220 else if (unit->outer_select()->having == (*ref))
00221 {
00222 unit->outer_select()->having= substitution;
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
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
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
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
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 if (!left_expr_cache && exec_method == MATERIALIZATION)
00348 init_left_expr_cache();
00349
00350
00351 if (left_expr_cache && test_if_item_cache_changed(*left_expr_cache) < 0)
00352 {
00353
00354 if (!first_execution)
00355 return(false);
00356 first_execution= false;
00357 }
00358
00359
00360
00361
00362
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
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
00435
00436
00437
00438
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
00459
00460
00461 used_tables_cache= parent->get_used_tables_cache();
00462 const_item_cache= parent->get_const_item_cache();
00463
00464
00465
00466
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
00479
00480
00481
00482
00483
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
00529
00530
00531
00532
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
00551
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
00573
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
00596
00597
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;
00715 maybe_null= 0;
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
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
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
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
00857
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
00878
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
00898
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
00928
00929
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
00943
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
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
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
01003
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
01013
01014
01015
01016
01017
01018
01019
01020
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
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
01044
01045
01046 item= new Item_sum_max(*select_lex->ref_pointer_array);
01047 }
01048 else
01049 {
01050
01051
01052
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
01069
01070
01071
01072 if (item->fix_fields(session, 0))
01073 return(RES_ERROR);
01074 session->lex().allow_sum_func= save_allow_sum_func;
01075
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
01089 substitution= func->create(left_expr, subs);
01090 return(RES_OK);
01091 }
01092
01093 if (!substitution)
01094 {
01095
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
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
01112
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
01131
01132
01133 if (exec_method == MATERIALIZATION)
01134 return(RES_OK);
01135
01136
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
01198
01199
01200 item= new Item_func_trig_cond(item, get_cond_guard(0));
01201 }
01202
01203
01204
01205
01206
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
01215
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
01248
01249
01250
01251 having->name= (char*)in_having_cond;
01252 select_lex->having= join->having= having;
01253 select_lex->having_fix_field= 1;
01254
01255
01256
01257
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
01268
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
01277
01278
01279
01280 item->name= (char *)in_additional_cond;
01281
01282
01283
01284
01285
01286
01287 select_lex->where= join->conds= and_items(join->conds, item);
01288 select_lex->where->top_level_item();
01289
01290
01291
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
01303
01304
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
01324
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
01334 item= func->create(left_expr, item);
01335
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
01368
01369
01370 if (!substitution)
01371 {
01372
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
01379 if (!optimizer || optimizer->fix_left(session, 0))
01380 {
01381 session->lex().current_select= current;
01382 return(RES_ERROR);
01383 }
01384
01385
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
01403
01404
01405 if (exec_method == MATERIALIZATION)
01406 return(RES_OK);
01407
01408
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
01446
01447
01448
01449
01450
01451
01452
01453
01454
01455
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
01518
01519
01520
01521
01522
01523
01524
01525
01526
01527
01528
01529
01530
01531
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
01580
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
01596
01597
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
01613
01614
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
01668
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
01685
01686
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
01699 left_expr= optimizer->arguments()[0];
01700
01701 session->lex().current_select= current;
01702 if (result)
01703 goto err;
01704
01705
01706
01707
01708
01709 if (exec_method == NOT_TRANSFORMED)
01710 exec_method= IN_TO_EXISTS;
01711
01712
01713
01714
01715
01716
01717
01718
01719 if (left_expr->cols() == 1)
01720 res= single_value_transformer(join, func);
01721 else
01722 {
01723
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
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
01804
01805
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
01828 if (new_engine)
01829 {
01830
01831
01832
01833
01834
01835
01836
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
01882
01883
01884
01885
01886
01887
01888
01889
01890
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
01968
01969
01970
01971
01972
01973
01974
01975
01976
01977
01978
01979
01980
01981 bool subselect_union_engine::no_rows()
01982 {
01983
01984 return test(!unit->fake_select_lex->join->send_records);
01985 }
01986
01987
01988 void subselect_uniquesubquery_engine::cleanup()
01989 {
01990
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;
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
02067 assert(false);
02068 return 1;
02069 }
02070
02071
02072
02073
02074
02075
02076
02077
02078
02079
02080
02081
02082
02083
02084
02085
02086
02087 bool subselect_single_select_engine::no_rows()
02088 {
02089 return !item->assigned();
02090 }
02091
02092
02093
02094
02095
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
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
02198
02199
02200
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
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
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
02255
02256
02257
02258
02259
02260
02261
02262
02263
02264
02265
02266 if (session->lex().describe &&
02267 select_lex->uncacheable.none() &&
02268 !(join->select_options & SELECT_DESCRIBE) &&
02269 join->need_tmp &&
02270 item->const_item())
02271 {
02272
02273
02274
02275
02276
02277
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
02300
02301
02302
02303
02304
02305
02306
02307
02308
02309
02310
02311
02312
02313
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
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
02360
02361
02362
02363
02364
02365
02366
02367
02368
02369
02370
02371
02372
02373
02374
02375
02376
02377
02378
02379
02380
02381
02382
02383
02384
02385
02386
02387
02388
02389
02390
02391
02392
02393
02394
02395
02396
02397
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
02409
02410
02411
02412
02413
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
02422 return(1);
02423 }
02424 else
02425 {
02426
02427 break;
02428 }
02429 }
02430
02431
02432
02433
02434
02435
02436
02437
02438
02439 if (store_res == StoredKey::STORE_KEY_FATAL)
02440 {
02441
02442
02443
02444
02445 tab->table->status= STATUS_NOT_FOUND;
02446 break;
02447 }
02448 }
02449 return(0);
02450 }
02451
02452
02453
02454
02455
02456
02457
02458
02459
02460
02461
02462
02463
02464
02465
02466
02467
02468
02469
02470
02471
02472
02473
02474
02475
02476
02477
02478
02479
02480
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
02491 if (copy_ref_key())
02492 return(1);
02493 if (table->status)
02494 {
02495
02496
02497
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
02543
02544
02545
02546
02547
02548
02549
02550
02551
02552
02553
02554
02555
02556
02557
02558
02559
02560
02561
02562
02563
02564
02565
02566
02567
02568
02569
02570
02571
02572
02573
02574
02575
02576
02577
02578
02579
02580
02581
02582
02583
02584
02585
02586
02587
02588
02589
02590
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
02607 *tab->ref.null_ref_key= 0;
02608 ((Item_in_subselect *) item)->was_null= 0;
02609 }
02610
02611
02612 if (copy_ref_key())
02613 return(1);
02614
02615 if (table->status)
02616 {
02617
02618
02619
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;
02675 *tab->ref.null_ref_key= 1;
02676 null_finding= 1;
02677
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
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
02789
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
02808
02809
02810
02811
02812
02813
02814
02815
02816
02817
02818
02819
02820
02821
02822
02823
02824
02825
02826
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
02933
02934
02935
02936
02937
02938
02939
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
02978 return 0;
02979 }
02980
02981
02982
02983
02984
02985
02986
03007 bool subselect_hash_sj_engine::init_permanent(List<Item> *tmp_columns)
03008 {
03009
03010 select_union *tmp_result_sink;
03011
03012 Table *tmp_table;
03013 KeyInfo *tmp_key;
03014 uint32_t tmp_key_parts;
03015 Item_in_subselect *item_in= (Item_in_subselect *) item;
03016
03017
03018
03019
03020
03021
03022
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
03039
03040
03041
03042
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
03060
03061
03062 assert(tmp_table->getShare()->sizeKeys() == 1 && tmp_columns->size() == tmp_key_parts);
03063
03064
03065
03066
03067
03068
03069
03070
03071
03072
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;
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
03099
03100
03101
03102
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;
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
03129
03130
03131 materialize_engine->prepare();
03132
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();
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
03182
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
03201
03202
03203
03204
03205
03206
03207
03208 is_materialized= true;
03209
03210
03211
03212
03213
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
03221 return(false);
03222 }
03223
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
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 }