item_subselect.cc 25.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/* Copyright (C) 2000 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/* 
   subselect Item

SUBSELECT TODO:
   - add function from mysql_select that use JOIN* as parameter to JOIN methods
     (sql_select.h/sql_select.cc)
*/

#ifdef __GNUC__
#pragma implementation				// gcc: Class implementation
#endif

#include "mysql_priv.h"
#include "sql_select.h"

unknown's avatar
unknown committed
32 33 34 35 36
inline Item * and_items(Item* cond, Item *item)
{
  return (cond? (new Item_cond_and(cond, item)) : item);
}

37
Item_subselect::Item_subselect():
38
  Item_result_field(), engine_owner(1), value_assigned(0), substitution(0),
39
  have_to_be_excluded(0), engine_changed(0)
40
{
41
  reset();
42 43 44 45 46 47 48 49
  /*
    item value is NULL if select_subselect not changed this value 
    (i.e. some rows will be found returned)
  */
  null_value= 1;
}

void Item_subselect::init(THD *thd, st_select_lex *select_lex,
unknown's avatar
unknown committed
50
			  select_subselect *result)
51 52 53
{

  DBUG_ENTER("Item_subselect::init");
unknown's avatar
unknown committed
54
  DBUG_PRINT("subs", ("select_lex 0x%xl", (ulong) select_lex));
unknown's avatar
unknown committed
55 56 57 58 59 60 61

  if (select_lex->next_select())
    engine= new subselect_union_engine(thd, select_lex->master_unit(), result,
				       this);
  else
    engine= new subselect_single_select_engine(thd, select_lex, result,
					       this);
62 63 64
  DBUG_VOID_RETURN;
}

unknown's avatar
unknown committed
65 66 67 68 69 70
Item_subselect::~Item_subselect()
{
  if (engine_owner)
    delete engine;
}

71
Item_subselect::trans_res
unknown's avatar
unknown committed
72
Item_subselect::select_transformer(JOIN *join) 
unknown's avatar
unknown committed
73 74
{
  DBUG_ENTER("Item_subselect::select_transformer");
75
  DBUG_RETURN(RES_OK);
unknown's avatar
unknown committed
76 77 78
}


79
bool Item_subselect::fix_fields(THD *thd_param, TABLE_LIST *tables, Item **ref)
80
{
81 82
  thd= thd_param;

83
  char const *save_where= thd->where;
84
  int res= engine->prepare();
85
  if (!res)
unknown's avatar
unknown committed
86
  {
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    if (substitution)
    {
      (*ref)= substitution;
      substitution->name= name;
      if (have_to_be_excluded)
	engine->exclude();
      substitution= 0;
      fixed= 1;
      thd->where= "checking transformed subquery";
      int ret= (*ref)->fix_fields(thd, tables, ref);
      // We can't substitute aggregate functions (like (SELECT (max(i)))
      if ((*ref)->with_sum_func)
      {
	my_error(ER_INVALID_GROUP_FUNC_USE, MYF(0));
	return 1;
      }
      return ret;
    }
unknown's avatar
unknown committed
105 106 107
    // Is it one field subselect?
    if (engine->cols() > max_columns)
    {  
unknown's avatar
unknown committed
108
      my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
unknown's avatar
unknown committed
109 110
      return 1;
    }
111
    fix_length_and_dec();
unknown's avatar
unknown committed
112
  }
113
  fixed= 1;
114
  thd->where= save_where;
115 116 117
  return res;
}

118 119
bool Item_subselect::exec()
{
120
  int res;
121 122 123 124
  MEM_ROOT *old_root= my_pthread_getspecific_ptr(MEM_ROOT*, THR_MALLOC);
  if (&thd->mem_root != old_root)
  {
    my_pthread_setspecific_ptr(THR_MALLOC, &thd->mem_root);
125
    res= engine->exec();
126 127 128
    my_pthread_setspecific_ptr(THR_MALLOC, old_root);
  }
  else
129 130 131 132 133 134 135
    res= engine->exec();
  if (engine_changed)
  {
    engine_changed= 0;
    return exec();
  }
  return (res);
136 137
}

138 139 140 141 142
Item::Type Item_subselect::type() const 
{
  return SUBSELECT_ITEM;
}

143 144
void Item_subselect::fix_length_and_dec()
{
145
  engine->fix_length_and_dec(0);
146
}
unknown's avatar
unknown committed
147 148 149

inline table_map Item_subselect::used_tables() const
{
150
  return (table_map) (engine->dependent() ? 1L :
unknown's avatar
unknown committed
151
		      (engine->uncacheable() ? RAND_TABLE_BIT : 0L));
unknown's avatar
unknown committed
152 153
}

unknown's avatar
unknown committed
154
Item_singlerow_subselect::Item_singlerow_subselect(THD *thd,
unknown's avatar
unknown committed
155
						   st_select_lex *select_lex):
156
  Item_subselect(), value(0)
unknown's avatar
unknown committed
157
{
unknown's avatar
unknown committed
158 159
  DBUG_ENTER("Item_singlerow_subselect::Item_singlerow_subselect");
  init(thd, select_lex, new select_singlerow_subselect(this));
unknown's avatar
unknown committed
160 161
  max_columns= 1;
  maybe_null= 1;
162
  max_columns= UINT_MAX;
unknown's avatar
unknown committed
163
  DBUG_VOID_RETURN;
unknown's avatar
unknown committed
164 165
}

unknown's avatar
unknown committed
166
void Item_singlerow_subselect::reset()
167
{
168 169 170
  null_value= 1;
  if (value)
    value->null_value= 1;
171 172
}

173
Item_subselect::trans_res
unknown's avatar
unknown committed
174
Item_singlerow_subselect::select_transformer(JOIN *join)
175
{
176
  SELECT_LEX *select_lex= join->select_lex;
177
  
178 179
  if (!select_lex->master_unit()->first_select()->next_select() &&
      !select_lex->table_list.elements &&
unknown's avatar
unknown committed
180
      select_lex->item_list.elements == 1 &&
unknown's avatar
unknown committed
181 182 183 184 185 186 187
      /*
	We cant change name of Item_field or Item_ref, because it will
	prevent it's correct resolving, but we should save name of
	removed item => we do not make optimization if top item of
	list is field or reference.
	TODO: solve above problem
      */
unknown's avatar
unknown committed
188
      !(select_lex->item_list.head()->type() == FIELD_ITEM ||
189
	select_lex->item_list.head()->type() == REF_ITEM) 
unknown's avatar
unknown committed
190
      )
191 192 193
  {
    
    have_to_be_excluded= 1;
unknown's avatar
unknown committed
194
    if (join->thd->lex.describe)
195 196 197
    {
      char warn_buff[MYSQL_ERRMSG_SIZE];
      sprintf(warn_buff, ER(ER_SELECT_REDUCED), select_lex->select_number);
unknown's avatar
unknown committed
198
      push_warning(join->thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
199 200 201
		   ER_SELECT_REDUCED, warn_buff);
    }
    substitution= select_lex->item_list.head();
unknown's avatar
unknown committed
202 203 204 205 206 207
    /*
      as far as we moved content to upper leven, field which depend of
      'upper' select is not really dependent => we remove this dependence
    */
    substitution->walk(&Item::remove_dependence_processor,
		       (byte *) select_lex->outer_select());
208 209 210
    if (select_lex->where || select_lex->having)
    {
      Item *cond;
211 212 213 214
      if (!join->having)
	cond= join->conds;
      else if (!join->conds)
	cond= join->having;
215
      else
216
	if (!(cond= new Item_cond_and(join->conds, join->having)))
217
	  return RES_ERROR;
218 219
      if (!(substitution= new Item_func_if(cond, substitution,
					   new Item_null())))
220
	return RES_ERROR;
221
    }
222
    return RES_REDUCE;
223
  }
224
  return RES_OK;
225 226
}

unknown's avatar
unknown committed
227
void Item_singlerow_subselect::store(uint i, Item *item)
unknown's avatar
unknown committed
228
{
229
  row[i]->store(item);
unknown's avatar
unknown committed
230 231
}

unknown's avatar
unknown committed
232
enum Item_result Item_singlerow_subselect::result_type() const
unknown's avatar
unknown committed
233
{
234 235 236
  return engine->type();
}

unknown's avatar
unknown committed
237
void Item_singlerow_subselect::fix_length_and_dec()
238
{
239 240 241 242 243 244 245
  if ((max_columns= engine->cols()) == 1)
  {
    engine->fix_length_and_dec(row= &value);
    if (!(value= Item_cache::get_cache(engine->type())))
      return;
  }
  else
246
  {
247 248 249 250 251
    THD *thd= current_thd;
    if (!(row= (Item_cache**)thd->alloc(sizeof(Item_cache*)*max_columns)))
      return;
    engine->fix_length_and_dec(row);
    value= *row;
252
  }
unknown's avatar
unknown committed
253
  maybe_null= engine->may_be_null();
254 255
}

unknown's avatar
unknown committed
256
uint Item_singlerow_subselect::cols()
unknown's avatar
unknown committed
257
{
258 259 260
  return engine->cols();
}

unknown's avatar
unknown committed
261
bool Item_singlerow_subselect::check_cols(uint c)
262 263 264
{
  if (c != engine->cols())
  {
unknown's avatar
unknown committed
265
    my_error(ER_OPERAND_COLUMNS, MYF(0), c);
266 267 268 269 270
    return 1;
  }
  return 0;
}

unknown's avatar
unknown committed
271
bool Item_singlerow_subselect::null_inside()
272 273 274 275 276 277 278 279 280
{
  for (uint i= 0; i < max_columns ; i++)
  {
    if (row[i]->null_value)
      return 1;
  }
  return 0;
}

unknown's avatar
unknown committed
281
void Item_singlerow_subselect::bring_value()
282
{
283
  exec();
unknown's avatar
unknown committed
284 285
}

unknown's avatar
unknown committed
286
double Item_singlerow_subselect::val () 
unknown's avatar
unknown committed
287
{
288
  if (!exec() && !value->null_value)
289 290 291 292 293
  {
    null_value= 0;
    return value->val();
  }
  else
unknown's avatar
unknown committed
294
  {
295
    reset();
unknown's avatar
unknown committed
296
    return 0;
unknown's avatar
unknown committed
297
  }
unknown's avatar
unknown committed
298 299
}

unknown's avatar
unknown committed
300
longlong Item_singlerow_subselect::val_int () 
unknown's avatar
unknown committed
301
{
302
  if (!exec() && !value->null_value)
303 304 305 306 307
  {
    null_value= 0;
    return value->val_int();
  }
  else
unknown's avatar
unknown committed
308
  {
309
    reset();
unknown's avatar
unknown committed
310
    return 0;
unknown's avatar
unknown committed
311
  }
unknown's avatar
unknown committed
312 313
}

unknown's avatar
unknown committed
314
String *Item_singlerow_subselect::val_str (String *str) 
unknown's avatar
unknown committed
315
{
316
  if (!exec() && !value->null_value)
317 318 319 320 321
  {
    null_value= 0;
    return value->val_str(str);
  }
  else
unknown's avatar
unknown committed
322
  {
323
    reset();
unknown's avatar
unknown committed
324
    return 0;
unknown's avatar
unknown committed
325
  }
unknown's avatar
unknown committed
326 327 328
}

Item_exists_subselect::Item_exists_subselect(THD *thd,
unknown's avatar
unknown committed
329
					     st_select_lex *select_lex):
330
  Item_subselect()
unknown's avatar
unknown committed
331
{
unknown's avatar
unknown committed
332
  DBUG_ENTER("Item_exists_subselect::Item_exists_subselect");
unknown's avatar
unknown committed
333
  init(thd, select_lex, new select_exists_subselect(this));
unknown's avatar
unknown committed
334 335 336 337
  max_columns= UINT_MAX;
  null_value= 0; //can't be NULL
  maybe_null= 0; //can't be NULL
  value= 0;
unknown's avatar
unknown committed
338 339 340 341 342
  // We need only 1 row to determinate existence
  select_lex->master_unit()->global_parameters->select_limit= 1;
  DBUG_VOID_RETURN;
}

343 344
bool Item_in_subselect::test_limit(SELECT_LEX_UNIT *unit)
{
unknown's avatar
unknown committed
345 346
  if (unit->fake_select_lex &&
      unit->fake_select_lex->test_limit())
347
    return(1);
unknown's avatar
unknown committed
348

349 350 351
  SELECT_LEX *sl= unit->first_select();
  for (; sl; sl= sl->next_select())
  {
unknown's avatar
unknown committed
352
    if (sl->test_limit())
353 354 355 356 357
      return(1);
  }
  return(0);
}

unknown's avatar
unknown committed
358
Item_in_subselect::Item_in_subselect(THD *thd, Item * left_exp,
unknown's avatar
unknown committed
359 360 361 362
				     st_select_lex *select_lex):
  Item_exists_subselect()
{
  DBUG_ENTER("Item_in_subselect::Item_in_subselect");
unknown's avatar
unknown committed
363 364 365
  left_expr= left_exp;
  init(thd, select_lex, new select_exists_subselect(this));
  max_columns= UINT_MAX;
366
  maybe_null= 1;
367
  abort_on_null= 0;
368
  reset();
unknown's avatar
unknown committed
369
  //if test_limit will fail then error will be reported to client
370
  test_limit(select_lex->master_unit());
unknown's avatar
unknown committed
371 372 373 374
  DBUG_VOID_RETURN;
}

Item_allany_subselect::Item_allany_subselect(THD *thd, Item * left_exp,
375
					     compare_func_creator fn,
unknown's avatar
unknown committed
376 377 378 379 380
					     st_select_lex *select_lex):
  Item_in_subselect()
{
  DBUG_ENTER("Item_in_subselect::Item_in_subselect");
  left_expr= left_exp;
381
  func= fn;
unknown's avatar
unknown committed
382
  init(thd, select_lex, new select_exists_subselect(this));
unknown's avatar
unknown committed
383
  max_columns= 1;
384
  abort_on_null= 0;
385
  reset();
unknown's avatar
unknown committed
386
  //if test_limit will fail then error will be reported to client
387
  test_limit(select_lex->master_unit());
unknown's avatar
unknown committed
388
  DBUG_VOID_RETURN;
unknown's avatar
unknown committed
389 390
}

unknown's avatar
unknown committed
391

392 393
void Item_exists_subselect::fix_length_and_dec()
{
394
   decimals= 0;
395
   max_length= 1;
unknown's avatar
unknown committed
396
   max_columns= engine->cols();
397 398
}

unknown's avatar
unknown committed
399 400
double Item_exists_subselect::val () 
{
401
  if (exec())
unknown's avatar
unknown committed
402
  {
403
    reset();
unknown's avatar
unknown committed
404
    return 0;
unknown's avatar
unknown committed
405
  }
unknown's avatar
unknown committed
406 407 408 409 410
  return (double) value;
}

longlong Item_exists_subselect::val_int () 
{
411
  if (exec())
unknown's avatar
unknown committed
412
  {
413
    reset();
unknown's avatar
unknown committed
414
    return 0;
unknown's avatar
unknown committed
415
  }
unknown's avatar
unknown committed
416 417 418 419 420
  return value;
}

String *Item_exists_subselect::val_str(String *str)
{
421
  if (exec())
unknown's avatar
unknown committed
422
  {
423 424 425
    reset();
    return 0;
  }
426
  str->set(value,default_charset());
427 428 429 430 431
  return str;
}

double Item_in_subselect::val () 
{
432
  if (exec())
433 434 435 436 437 438 439 440 441 442 443 444
  {
    reset();
    null_value= 1;
    return 0;
  }
  if (was_null && !value)
    null_value= 1;
  return (double) value;
}

longlong Item_in_subselect::val_int () 
{
445
  if (exec())
446 447 448 449 450 451 452 453 454 455 456 457
  {
    reset();
    null_value= 1;
    return 0;
  }
  if (was_null && !value)
    null_value= 1;
  return value;
}

String *Item_in_subselect::val_str(String *str)
{
458
  if (exec())
459 460 461 462 463 464 465 466
  {
    reset();
    null_value= 1;
    return 0;
  }
  if (was_null && !value)
  {
    null_value= 1;
unknown's avatar
unknown committed
467
    return 0;
unknown's avatar
unknown committed
468
  }
469
  str->set(value,default_charset());
unknown's avatar
unknown committed
470 471 472
  return str;
}

unknown's avatar
unknown committed
473 474 475
Item_in_subselect::Item_in_subselect(Item_in_subselect *item):
  Item_exists_subselect(item)
{
unknown's avatar
unknown committed
476
  left_expr= item->left_expr;
477
  abort_on_null= item->abort_on_null;
unknown's avatar
unknown committed
478 479
}

unknown's avatar
unknown committed
480 481
Item_allany_subselect::Item_allany_subselect(Item_allany_subselect *item):
  Item_in_subselect(item)
unknown's avatar
unknown committed
482
{
unknown's avatar
unknown committed
483 484 485
  func= item->func;
}

486
Item_subselect::trans_res
unknown's avatar
unknown committed
487
Item_in_subselect::single_value_transformer(JOIN *join,
488 489
					    Item *left_expr,
					    compare_func_creator func)
unknown's avatar
unknown committed
490 491
{
  DBUG_ENTER("Item_in_subselect::single_value_transformer");
492

493
  SELECT_LEX *select_lex= join->select_lex;
unknown's avatar
unknown committed
494

unknown's avatar
unknown committed
495
  THD *thd= join->thd;
496
  thd->where= "scalar IN/ALL/ANY subquery";
unknown's avatar
unknown committed
497

498
  if (!substitution)
unknown's avatar
unknown committed
499
  {
500 501 502 503
    //first call for this unit
    SELECT_LEX_UNIT *unit= select_lex->master_unit();
    substitution= optimizer= new Item_in_optimizer(left_expr, this);

unknown's avatar
unknown committed
504 505
    SELECT_LEX *current= thd->lex.current_select, *up;

506 507 508
    thd->lex.current_select= up= current->return_after_parsing();
    //optimizer never use Item **ref => we can pass 0 as parameter
    if (!optimizer || optimizer->fix_left(thd, up->get_table_list(), 0))
509
    {
510
      thd->lex.current_select= current;
511
      DBUG_RETURN(RES_ERROR);
512
    }
513
    thd->lex.current_select= current;
514

515 516 517 518 519 520
    /*
      As far as  Item_ref_in_optimizer do not substitude itself on fix_fields
      we can use same item for all selects.
    */
    expr= new Item_ref((Item**)optimizer->get_cache(), 
		       (char *)"<no matter>",
521
		       (char *)in_left_expr_name);
522 523 524

    unit->dependent= 1;
  }
525

526 527 528 529
  select_lex->dependent= 1;
  Item *item;
  if (select_lex->item_list.elements > 1)
  {
unknown's avatar
unknown committed
530
    my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
531
    DBUG_RETURN(RES_ERROR);
532
  }
unknown's avatar
unknown committed
533 534

  item= (Item*) select_lex->item_list.head();
unknown's avatar
unknown committed
535

536 537 538 539 540 541 542 543 544 545 546
  if (join->having || select_lex->with_sum_func ||
      select_lex->group_list.elements)
  {
    item= (*func)(expr,
		  new Item_ref_null_helper(this,
					   select_lex->ref_pointer_array,
					   (char *)"<ref>",
					   this->full_name()));
    join->having= and_items(join->having, item);
    select_lex->having_fix_field= 1;
    if (join->having->fix_fields(thd, join->tables_list, &join->having))
547
    {
548
      select_lex->having_fix_field= 0;
549
      DBUG_RETURN(RES_ERROR);
550
    }
551 552 553 554 555 556 557 558 559
    select_lex->having_fix_field= 0;
  }
  else
  {
    select_lex->item_list.empty();
    select_lex->item_list.push_back(new Item_int("Not_used",
						 (longlong) 1, 21));
    select_lex->ref_pointer_array[0]= select_lex->item_list.head();
    if (select_lex->table_list.elements)
560
    {
561 562 563
      Item *having= item, *isnull= item;
      item= (*func)(expr, item);
      if (!abort_on_null)
unknown's avatar
unknown committed
564
      {
565 566 567 568 569 570
	having= new Item_is_not_null_test(this, having);
	join->having= (join->having ?
		       new Item_cond_and(having, join->having) :
		       having);
	select_lex->having_fix_field= 1;
	if (join->having->fix_fields(thd, join->tables_list, &join->having))
unknown's avatar
unknown committed
571
	{
572
	  select_lex->having_fix_field= 0;
573
	  DBUG_RETURN(RES_ERROR);
unknown's avatar
unknown committed
574
	}
575 576 577 578
	select_lex->having_fix_field= 0;
	item= new Item_cond_or(item,
			       new Item_func_isnull(isnull));
      }
579
      item->name= (char *)in_additional_cond;
580 581
      join->conds= and_items(join->conds, item);
      if (join->conds->fix_fields(thd, join->tables_list, &join->conds))
582
	DBUG_RETURN(RES_ERROR);
583 584 585 586 587 588
    }
    else
    {
      if (select_lex->master_unit()->first_select()->next_select())
      {
	join->having= (*func)(expr, 
589 590 591
			      new Item_null_helper(this, item,
						   (char *)"<no matter>",
						   (char *)"<result>"));
592 593 594 595
	select_lex->having_fix_field= 1;
	if (join->having->fix_fields(thd, join->tables_list, &join->having))
	{
	  select_lex->having_fix_field= 0;
596
	  DBUG_RETURN(RES_ERROR);
unknown's avatar
unknown committed
597
	}
598 599 600 601 602 603 604 605 606 607
	select_lex->having_fix_field= 0;
      }
      else
      {
	// it is single select without tables => possible optimization
	item= (*func)(left_expr, item);
	// fix_field of item will be done in time of substituting 
	substitution= item;
	have_to_be_excluded= 1;
	if (thd->lex.describe)
unknown's avatar
unknown committed
608
	{
609 610 611 612
	  char warn_buff[MYSQL_ERRMSG_SIZE];
	  sprintf(warn_buff, ER(ER_SELECT_REDUCED), select_lex->select_number);
	  push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
		       ER_SELECT_REDUCED, warn_buff);
unknown's avatar
unknown committed
613
	}
614
	DBUG_RETURN(RES_REDUCE);
unknown's avatar
unknown committed
615
      }
616
    }
unknown's avatar
unknown committed
617
  }
618
  DBUG_RETURN(RES_OK);
unknown's avatar
unknown committed
619
}
unknown's avatar
unknown committed
620

621
Item_subselect::trans_res
unknown's avatar
unknown committed
622 623
Item_in_subselect::row_value_transformer(JOIN *join,
					 Item *left_expr)
unknown's avatar
unknown committed
624 625
{
  DBUG_ENTER("Item_in_subselect::row_value_transformer");
unknown's avatar
unknown committed
626

unknown's avatar
unknown committed
627
  THD *thd= join->thd;
628
  thd->where= "row IN/ALL/ANY subquery";
unknown's avatar
unknown committed
629

630
  SELECT_LEX *select_lex= join->select_lex;
unknown's avatar
unknown committed
631

632
  if (!substitution)
unknown's avatar
unknown committed
633
  {
634 635 636 637
    //first call for this unit
    SELECT_LEX_UNIT *unit= select_lex->master_unit();
    substitution= optimizer= new Item_in_optimizer(left_expr, this);

unknown's avatar
unknown committed
638
    SELECT_LEX *current= thd->lex.current_select, *up;
639 640 641
    thd->lex.current_select= up= current->return_after_parsing();
    //optimizer never use Item **ref => we can pass 0 as parameter
    if (!optimizer || optimizer->fix_left(thd, up->get_table_list(), 0))
642
    {
643
      thd->lex.current_select= current;
644
      DBUG_RETURN(RES_ERROR);
645
    }
646 647 648 649 650 651 652 653
    thd->lex.current_select= current;

    unit->dependent= 1;
  }

  uint n= left_expr->cols();

  select_lex->dependent= 1;
unknown's avatar
unknown committed
654 655 656
  select_lex->setup_ref_array(thd,
			      select_lex->order_list.elements +
			      select_lex->group_list.elements);
657 658 659 660
  Item *item= 0;
  List_iterator_fast<Item> li(select_lex->item_list);
  for (uint i= 0; i < n; i++)
  {
unknown's avatar
unknown committed
661 662 663 664
    Item *func= new Item_ref_null_helper(this, 
					 select_lex->ref_pointer_array+i,
					 (char *) "<no matter>",
					 (char *) "<list ref>");
665 666 667 668
    func=
      Item_bool_func2::eq_creator(new Item_ref((*optimizer->get_cache())->
					       addr(i), 
					       (char *)"<no matter>",
669
					       (char *)in_left_expr_name),
670 671 672
				  func);
    item= and_items(item, func);
  }
unknown's avatar
unknown committed
673

674 675 676 677 678 679 680
  if (join->having || select_lex->with_sum_func ||
      select_lex->group_list.first ||
      !select_lex->table_list.elements)
  {
    join->having= and_items(join->having, item);
    select_lex->having_fix_field= 1;
    if (join->having->fix_fields(thd, join->tables_list, &join->having))
unknown's avatar
unknown committed
681
    {
682
      select_lex->having_fix_field= 0;
683
      DBUG_RETURN(RES_ERROR);
unknown's avatar
unknown committed
684
    }
685
    select_lex->having_fix_field= 0;
unknown's avatar
unknown committed
686
  }
687 688 689 690
  else
  {
    join->conds= and_items(join->conds, item);
    if (join->conds->fix_fields(thd, join->tables_list, &join->having))
691
      DBUG_RETURN(RES_ERROR);
692
  }
693
  DBUG_RETURN(RES_OK);
unknown's avatar
unknown committed
694 695
}

696
Item_subselect::trans_res
unknown's avatar
unknown committed
697
Item_in_subselect::select_transformer(JOIN *join)
unknown's avatar
unknown committed
698
{
unknown's avatar
unknown committed
699
  if (left_expr->cols() == 1)
unknown's avatar
unknown committed
700
    return single_value_transformer(join, left_expr,
701
				    &Item_bool_func2::eq_creator);
unknown's avatar
unknown committed
702
  return row_value_transformer(join, left_expr);
unknown's avatar
unknown committed
703 704
}

705
Item_subselect::trans_res
unknown's avatar
unknown committed
706
Item_allany_subselect::select_transformer(JOIN *join)
unknown's avatar
unknown committed
707
{
unknown's avatar
unknown committed
708
  return single_value_transformer(join, left_expr, func);
unknown's avatar
unknown committed
709 710
}

unknown's avatar
unknown committed
711 712 713 714 715
subselect_single_select_engine::subselect_single_select_engine(THD *thd, 
							       st_select_lex *select,
							       select_subselect *result,
							       Item_subselect *item):
  subselect_engine(thd, item, result),
unknown's avatar
unknown committed
716
    prepared(0), optimized(0), executed(0)
unknown's avatar
unknown committed
717 718 719 720 721 722 723 724 725 726 727 728 729
{
  select_lex= select;
  SELECT_LEX_UNIT *unit= select_lex->master_unit();
  unit->offset_limit_cnt= unit->global_parameters->offset_limit;
  unit->select_limit_cnt= unit->global_parameters->select_limit+
    unit->global_parameters ->offset_limit;
  if (unit->select_limit_cnt < unit->global_parameters->select_limit)
    unit->select_limit_cnt= HA_POS_ERROR;		// no limit
  if (unit->select_limit_cnt == HA_POS_ERROR)
    select_lex->options&= ~OPTION_FOUND_ROWS;
  join= new JOIN(thd, select_lex->item_list, select_lex->options, result);
  if (!join || !result)
    //out of memory
730
    thd->fatal_error();
unknown's avatar
unknown committed
731
  unit->item= item;
unknown's avatar
unknown committed
732 733 734 735 736 737 738 739 740 741
  this->select_lex= select_lex;
}

subselect_union_engine::subselect_union_engine(THD *thd,
					       st_select_lex_unit *u,
					       select_subselect *result,
					       Item_subselect *item):
  subselect_engine(thd, item, result)
{
  unit= u;
unknown's avatar
unknown committed
742
  if (!result)
unknown's avatar
unknown committed
743
    //out of memory
744
    thd->fatal_error();
unknown's avatar
unknown committed
745 746 747 748 749
  unit->item= item;
}

int subselect_single_select_engine::prepare()
{
unknown's avatar
unknown committed
750 751 752
  if (prepared)
    return 0;
  prepared= 1;
unknown's avatar
unknown committed
753
  SELECT_LEX *save_select= thd->lex.current_select;
754
  thd->lex.current_select= select_lex;
unknown's avatar
unknown committed
755 756 757
  if (join->prepare(&select_lex->ref_pointer_array,
		    (TABLE_LIST*) select_lex->table_list.first,
		    select_lex->with_wild,
unknown's avatar
unknown committed
758
		    select_lex->where,
unknown's avatar
unknown committed
759 760
		    select_lex->order_list.elements +
		    select_lex->group_list.elements,
unknown's avatar
unknown committed
761 762 763 764
		    (ORDER*) select_lex->order_list.first,
		    (ORDER*) select_lex->group_list.first,
		    select_lex->having,
		    (ORDER*) 0, select_lex, 
765
		    select_lex->master_unit(), 0))
unknown's avatar
unknown committed
766
    return 1;
767
  thd->lex.current_select= save_select;
unknown's avatar
unknown committed
768 769 770 771 772
  return 0;
}

int subselect_union_engine::prepare()
{
773
  return unit->prepare(thd, result, 0);
unknown's avatar
unknown committed
774 775
}

776
int subselect_uniquesubquery_engine::prepare()
777 778 779 780 781 782
{
  //this never should be called
  DBUG_ASSERT(0);
  return 1;
}

783
static Item_result set_row(SELECT_LEX *select_lex, Item * item,
unknown's avatar
unknown committed
784
			   Item_cache **row, bool *maybe_null)
785
{
786 787
  Item_result res_type= STRING_RESULT;
  Item *sel_item;
788
  List_iterator_fast<Item> li(select_lex->item_list);
789 790 791 792 793
  for (uint i= 0; (sel_item= li++); i++)
  {
    item->max_length= sel_item->max_length;
    res_type= sel_item->result_type();
    item->decimals= sel_item->decimals;
unknown's avatar
unknown committed
794
    *maybe_null= sel_item->maybe_null;
795 796 797 798 799
    if (row)
    {
      if (!(row[i]= Item_cache::get_cache(res_type)))
	return STRING_RESULT; // we should return something
      row[i]->set_len_n_dec(sel_item->max_length, sel_item->decimals);
800
      row[i]->collation.set(sel_item->collation);
801 802 803 804 805
    }
  }
  if (select_lex->item_list.elements > 1)
    res_type= ROW_RESULT;
  return res_type;
806 807
}

808
void subselect_single_select_engine::fix_length_and_dec(Item_cache **row)
809
{
810
  DBUG_ASSERT(row || select_lex->item_list.elements==1);
unknown's avatar
unknown committed
811
  res_type= set_row(select_lex, item, row, &maybe_null);
812
  item->collation.set(row[0]->collation);
unknown's avatar
unknown committed
813 814
  if (cols() != 1)
    maybe_null= 0;
815 816 817 818 819 820 821
}

void subselect_union_engine::fix_length_and_dec(Item_cache **row)
{
  DBUG_ASSERT(row || unit->first_select()->item_list.elements==1);

  if (unit->first_select()->item_list.elements == 1)
822
  {
823 824 825 826 827 828 829 830 831 832
    uint32 mlen= 0, len;
    Item *sel_item= 0;
    for (SELECT_LEX *sl= unit->first_select(); sl; sl= sl->next_select())
    {
      List_iterator_fast<Item> li(sl->item_list);
      Item *s_item= li++;
      if ((len= s_item->max_length) > mlen)
	mlen= len;
      if (!sel_item)
	sel_item= s_item;
unknown's avatar
unknown committed
833
      maybe_null= s_item->maybe_null;
834 835 836 837 838 839 840 841 842 843 844 845 846 847
    }
    item->max_length= mlen;
    res_type= sel_item->result_type();
    item->decimals= sel_item->decimals;
    if (row)
    {
      if (!(row[0]= Item_cache::get_cache(res_type)))
	return;
      row[0]->set_len_n_dec(mlen, sel_item->decimals);
    }
  }
  else
  {
    SELECT_LEX *sl= unit->first_select();
unknown's avatar
unknown committed
848 849
    bool fake= 0;
    res_type= set_row(sl, item, row, &fake);
unknown's avatar
unknown committed
850
    for (sl= sl->next_select(); sl; sl= sl->next_select())
851 852 853 854 855 856 857 858 859
    {
      List_iterator_fast<Item> li(sl->item_list);
      Item *sel_item;
      for (uint i= 0; (sel_item= li++); i++)
      {
	if (sel_item->max_length > row[i]->max_length)
	  row[i]->max_length= sel_item->max_length;
      }
    }
860 861
  }
}
unknown's avatar
unknown committed
862

863
void subselect_uniquesubquery_engine::fix_length_and_dec(Item_cache **row)
864 865 866 867 868
{
  //this never should be called
  DBUG_ASSERT(0);
}

unknown's avatar
unknown committed
869 870 871
int subselect_single_select_engine::exec()
{
  DBUG_ENTER("subselect_single_select_engine::exec");
872
  char const *save_where= join->thd->where;
unknown's avatar
unknown committed
873
  SELECT_LEX *save_select= join->thd->lex.current_select;
874
  join->thd->lex.current_select= select_lex;
unknown's avatar
unknown committed
875 876 877 878 879
  if (!optimized)
  {
    optimized=1;
    if (join->optimize())
    {
880
      join->thd->where= save_where;
unknown's avatar
unknown committed
881
      executed= 1;
882
      join->thd->lex.current_select= save_select;
unknown's avatar
unknown committed
883 884
      DBUG_RETURN(join->error?join->error:1);
    }
885 886 887 888
    if (item->engine_changed)
    {
      DBUG_RETURN(1);
    }
unknown's avatar
unknown committed
889
  }
890
  if ((select_lex->dependent || select_lex->uncacheable) && executed)
unknown's avatar
unknown committed
891 892
  {
    if (join->reinit())
893 894
    {
      join->thd->where= save_where;
895
      join->thd->lex.current_select= save_select;
unknown's avatar
unknown committed
896
      DBUG_RETURN(1);
897
    }
898
    item->reset();
unknown's avatar
unknown committed
899 900 901 902 903 904
    item->assigned((executed= 0));
  }
  if (!executed)
  {
    join->exec();
    executed= 1;
905
    join->thd->where= save_where;
906
    join->thd->lex.current_select= save_select;
907
    DBUG_RETURN(join->error||thd->is_fatal_error);
unknown's avatar
unknown committed
908
  }
909
  join->thd->where= save_where;
910
  join->thd->lex.current_select= save_select;
unknown's avatar
unknown committed
911 912 913 914 915
  DBUG_RETURN(0);
}

int subselect_union_engine::exec()
{
916 917 918 919
  char const *save_where= unit->thd->where;
  int res= unit->exec();
  unit->thd->where= save_where;
  return res;
unknown's avatar
unknown committed
920 921
}

922
int subselect_uniquesubquery_engine::exec()
923
{
924
  DBUG_ENTER("subselect_uniquesubquery_engine::exec");
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
  int error;
  TABLE *table= tab->table;
  if ((tab->ref.key_err= (*tab->ref.key_copy)->copy()))
  {
    table->status= STATUS_NOT_FOUND;
    error= -1;
  }
  else
  {
    error= table->file->index_read(table->record[0],
				   tab->ref.key_buff,
				   tab->ref.key_length,HA_READ_KEY_EXACT);
    if (error && error != HA_ERR_KEY_NOT_FOUND)
      error= report_error(table, error);
    else
    {
      error= 0;
      table->null_row= 0;
      if (table->status)
	((Item_in_subselect *) item)->value= 0;
      else
	((Item_in_subselect *) item)->value= (!cond || cond->val_int()?1:0);
    }
  }
949 950 951
  DBUG_RETURN(end_exec(table) || (error != 0));
}

952
int subselect_uniquesubquery_engine::end_exec(TABLE *table)
953
{
954
  DBUG_ENTER("subselect_uniquesubquery_engine::end_exec");
955 956
  int error=0, tmp;
  if ((tmp= table->file->extra(HA_EXTRA_NO_CACHE)))
957
  {
958 959 960 961 962 963 964 965 966 967 968 969 970
    DBUG_PRINT("error", ("extra(HA_EXTRA_NO_CACHE) failed"));
    error= 1;
  }
  if ((tmp= table->file->index_end()))
  {
    DBUG_PRINT("error", ("index_end() failed"));
    error= 1;
  }
  if (error == 1)
    table->file->print_error(tmp, MYF(0));
  DBUG_RETURN(error != 0);
}

971
int subselect_indexsubquery_engine::exec()
972
{
973
  DBUG_ENTER("subselect_indexsubselect_engine::exec");
974
  int error;
975
  bool null_finding= 0;
976
  TABLE *table= tab->table;
977

978
  ((Item_in_subselect *) item)->value= 0;
979 980 981 982 983 984 985
  if (check_null)
  {
    *tab->null_ref_key= 0;
    ((Item_in_subselect *) item)->was_null= 0;
  }

  if ((*tab->ref.key_copy) && (tab->ref.key_err= (*tab->ref.key_copy)->copy()))
986 987 988 989 990 991 992 993 994 995 996 997
  {
    table->status= STATUS_NOT_FOUND;
    error= -1;
  }
  else
  {
    error= table->file->index_read(table->record[0],
				   tab->ref.key_buff,
				   tab->ref.key_length,HA_READ_KEY_EXACT);
    if (error && error != HA_ERR_KEY_NOT_FOUND)
      error= report_error(table, error);
    else
998
    {
999 1000 1001 1002 1003 1004 1005 1006
      for(;;)
      {
	error= 0;
	table->null_row= 0;
	if (!table->status)
	{
	  if (!cond || cond->val_int())
	  {
1007
	    if (null_finding)
1008 1009 1010
	      ((Item_in_subselect *) item)->was_null= 1;
	    else
	      ((Item_in_subselect *) item)->value= 1;
1011 1012
	    goto finish;
	  }
1013 1014 1015 1016 1017 1018 1019 1020
	  error= table->file->index_next_same(table->record[0],
					      tab->ref.key_buff,
					      tab->ref.key_length);
	  if (error && error != HA_ERR_END_OF_FILE)
	  {
	    error= report_error(table, error);
	    goto finish;
	  }
1021 1022
	}
	else
1023
	{
1024
	  if (!check_null || null_finding)
1025 1026
	    goto finish;
	  *tab->null_ref_key= 1;
1027
	  null_finding= 1;
1028 1029 1030
	  if (safe_index_read(tab))
	    goto finish;
	}
1031
      }
1032 1033
    }
  }
1034 1035
finish:
  DBUG_RETURN(end_exec(table) || (error != 0));
1036 1037
}

unknown's avatar
unknown committed
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
uint subselect_single_select_engine::cols()
{
  return select_lex->item_list.elements;
}

uint subselect_union_engine::cols()
{
  return unit->first_select()->item_list.elements;
}

1048
bool subselect_single_select_engine::dependent()
unknown's avatar
unknown committed
1049
{
1050
  return select_lex->dependent;
unknown's avatar
unknown committed
1051 1052
}

1053
bool subselect_union_engine::dependent()
unknown's avatar
unknown committed
1054
{
1055
  return unit->dependent;
unknown's avatar
unknown committed
1056
}
unknown's avatar
unknown committed
1057

1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
bool subselect_single_select_engine::uncacheable()
{
  return select_lex->uncacheable;
}

bool subselect_union_engine::uncacheable()
{
  return unit->uncacheable;
}

unknown's avatar
unknown committed
1068 1069 1070 1071 1072 1073 1074 1075 1076
void subselect_single_select_engine::exclude()
{
  select_lex->master_unit()->exclude_level();
}

void subselect_union_engine::exclude()
{
  unit->exclude_level();
}
1077

1078
void subselect_uniquesubquery_engine::exclude()
1079 1080 1081 1082
{
  //this never should be called
  DBUG_ASSERT(0);
}