~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2003 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/*
17
  UNION  of select's
18
  UNION's  were introduced by Monty and Sinisa <sinisa@mysql.com>
19
*/
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
20
#include <drizzled/server_includes.h>
21
#include <drizzled/sql_select.h>
202.3.6 by Monty Taylor
First pass at gettexizing the error messages.
22
#include <drizzled/drizzled_error_messages.h>
1 by brian
clean slate
23
77.1.46 by Monty Taylor
Finished the warnings work!
24
bool mysql_union(THD *thd,
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
25
                 LEX *lex __attribute__((unused)),
77.1.46 by Monty Taylor
Finished the warnings work!
26
                 select_result *result,
1 by brian
clean slate
27
                 SELECT_LEX_UNIT *unit, ulong setup_tables_done_option)
28
{
29
  bool res;
30
  if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK |
31
                           setup_tables_done_option)))
32
    res= unit->exec();
33
  if (res)
34
    res|= unit->cleanup();
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
35
  return(res);
1 by brian
clean slate
36
}
37
38
39
/***************************************************************************
40
** store records in temporary table for UNION
41
***************************************************************************/
42
212.1.3 by Monty Taylor
Renamed __attribute__((__unused__)) to __attribute__((unused)).
43
int select_union::prepare(List<Item> &list __attribute__((unused)),
77.1.46 by Monty Taylor
Finished the warnings work!
44
                          SELECT_LEX_UNIT *u)
1 by brian
clean slate
45
{
46
  unit= u;
47
  return 0;
48
}
49
50
51
bool select_union::send_data(List<Item> &values)
52
{
53
  int error= 0;
54
  if (unit->offset_limit_cnt)
55
  {						// using limit offset,count
56
    unit->offset_limit_cnt--;
57
    return 0;
58
  }
59
  fill_record(thd, table->field, values, 1);
60
  if (thd->is_error())
61
    return 1;
62
63
  if ((error= table->file->ha_write_row(table->record[0])))
64
  {
65
    /* create_myisam_from_heap will generate error if needed */
66
    if (table->file->is_fatal_error(error, HA_CHECK_DUP) &&
67
        create_myisam_from_heap(thd, table, tmp_table_param.start_recinfo, 
68
                                &tmp_table_param.recinfo, error, 1))
69
      return 1;
70
  }
71
  return 0;
72
}
73
74
75
bool select_union::send_eof()
76
{
77
  return 0;
78
}
79
80
81
bool select_union::flush()
82
{
83
  int error;
84
  if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
85
  {
86
    table->file->print_error(error, MYF(0));
87
    return 1;
88
  }
89
  return 0;
90
}
91
92
/*
93
  Create a temporary table to store the result of select_union.
94
95
  SYNOPSIS
96
    select_union::create_result_table()
97
      thd                thread handle
98
      column_types       a list of items used to define columns of the
99
                         temporary table
100
      is_union_distinct  if set, the temporary table will eliminate
101
                         duplicates on insert
102
      options            create options
103
      table_alias        name of the temporary table
151 by Brian Aker
Ulonglong to uint64_t
104
      bit_fields_as_long convert bit fields to uint64_t
1 by brian
clean slate
105
106
  DESCRIPTION
107
    Create a temporary table that is used to store the result of a UNION,
108
    derived table, or a materialized cursor.
109
110
  RETURN VALUE
111
    0                    The table has been created successfully.
112
    1                    create_tmp_table failed.
113
*/
114
115
bool
116
select_union::create_result_table(THD *thd_arg, List<Item> *column_types,
151 by Brian Aker
Ulonglong to uint64_t
117
                                  bool is_union_distinct, uint64_t options,
1 by brian
clean slate
118
                                  const char *table_alias,
119
                                  bool bit_fields_as_long)
120
{
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
121
  assert(table == 0);
1 by brian
clean slate
122
  tmp_table_param.init();
123
  tmp_table_param.field_count= column_types->elements;
124
  tmp_table_param.bit_fields_as_long= bit_fields_as_long;
125
126
  if (! (table= create_tmp_table(thd_arg, &tmp_table_param, *column_types,
327.2.3 by Brian Aker
Refactoring of class Table
127
                                 (order_st*) 0, is_union_distinct, 1,
1 by brian
clean slate
128
                                 options, HA_POS_ERROR, (char*) table_alias)))
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
129
    return true;
1 by brian
clean slate
130
  table->file->extra(HA_EXTRA_WRITE_CACHE);
131
  table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
132
  return false;
1 by brian
clean slate
133
}
134
135
136
/**
137
  Reset and empty the temporary table that stores the materialized query result.
138
139
  @note The cleanup performed here is exactly the same as for the two temp
140
  tables of JOIN - exec_tmp_table_[1 | 2].
141
*/
142
143
void select_union::cleanup()
144
{
145
  table->file->extra(HA_EXTRA_RESET_STATE);
146
  table->file->ha_delete_all_rows();
147
  free_io_cache(table);
148
  filesort_free_buffers(table,0);
149
}
150
151
152
/*
153
  initialization procedures before fake_select_lex preparation()
154
155
  SYNOPSIS
156
    st_select_lex_unit::init_prepare_fake_select_lex()
157
    thd		- thread handler
158
159
  RETURN
160
    options of SELECT
161
*/
162
163
void
164
st_select_lex_unit::init_prepare_fake_select_lex(THD *thd_arg) 
165
{
166
  thd_arg->lex->current_select= fake_select_lex;
167
  fake_select_lex->table_list.link_in_list((uchar *)&result_table_list,
168
					   (uchar **)
169
					   &result_table_list.next_local);
170
  fake_select_lex->context.table_list= 
171
    fake_select_lex->context.first_name_resolution_table= 
172
    fake_select_lex->get_table_list();
173
  if (!fake_select_lex->first_execution)
174
  {
327.2.3 by Brian Aker
Refactoring of class Table
175
    for (order_st *order= (order_st *) global_parameters->order_list.first;
1 by brian
clean slate
176
         order;
177
         order= order->next)
178
      order->item= &order->item_ptr;
179
  }
327.2.3 by Brian Aker
Refactoring of class Table
180
  for (order_st *order= (order_st *)global_parameters->order_list.first;
1 by brian
clean slate
181
       order;
182
       order=order->next)
183
  {
184
    (*order->item)->walk(&Item::change_context_processor, 0,
185
                         (uchar*) &fake_select_lex->context);
186
  }
187
}
188
189
190
bool st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result,
202 by Brian Aker
Cleanup sql_lex to modern types.
191
                                 uint32_t additional_options)
1 by brian
clean slate
192
{
193
  SELECT_LEX *lex_select_save= thd_arg->lex->current_select;
194
  SELECT_LEX *sl, *first_sl= first_select();
195
  select_result *tmp_result;
196
  bool is_union_select;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
197
  Table *empty_table= 0;
1 by brian
clean slate
198
199
  describe= test(additional_options & SELECT_DESCRIBE);
200
201
  /*
202
    result object should be reassigned even if preparing already done for
203
    max/min subquery (ALL/ANY optimization)
204
  */
205
  result= sel_result;
206
207
  if (prepared)
208
  {
209
    if (describe)
210
    {
211
      /* fast reinit for EXPLAIN */
212
      for (sl= first_sl; sl; sl= sl->next_select())
213
      {
214
	sl->join->result= result;
215
	select_limit_cnt= HA_POS_ERROR;
216
	offset_limit_cnt= 0;
217
	if (result->prepare(sl->join->fields_list, this))
218
	{
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
219
	  return(true);
1 by brian
clean slate
220
	}
221
	sl->join->select_options|= SELECT_DESCRIBE;
222
	sl->join->reinit();
223
      }
224
    }
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
225
    return(false);
1 by brian
clean slate
226
  }
227
  prepared= 1;
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
228
  saved_error= false;
1 by brian
clean slate
229
  
230
  thd_arg->lex->current_select= sl= first_sl;
231
  found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
232
  is_union_select= is_union() || fake_select_lex;
233
234
  /* Global option */
235
236
  if (is_union_select)
237
  {
238
    if (!(tmp_result= union_result= new select_union))
239
      goto err;
240
    if (describe)
241
      tmp_result= sel_result;
242
  }
243
  else
244
    tmp_result= sel_result;
245
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
246
  sl->context.resolve_in_select_list= true;
1 by brian
clean slate
247
248
  for (;sl; sl= sl->next_select())
249
  {
250
    bool can_skip_order_by;
251
    sl->options|=  SELECT_NO_UNLOCK;
252
    JOIN *join= new JOIN(thd_arg, sl->item_list, 
253
			 sl->options | thd_arg->options | additional_options,
254
			 tmp_result);
255
    /*
256
      setup_tables_done_option should be set only for very first SELECT,
257
      because it protect from secont setup_tables call for select-like non
258
      select commands (DELETE/INSERT/...) and they use only very first
259
      SELECT (for union it can be only INSERT ... SELECT).
260
    */
261
    additional_options&= ~OPTION_SETUP_TABLES_DONE;
262
    if (!join)
263
      goto err;
264
265
    thd_arg->lex->current_select= sl;
266
267
    can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit);
268
269
    saved_error= join->prepare(&sl->ref_pointer_array,
327.2.4 by Brian Aker
Refactoring table.h
270
                               (TableList*) sl->table_list.first,
1 by brian
clean slate
271
                               sl->with_wild,
272
                               sl->where,
273
                               (can_skip_order_by ? 0 :
274
                                sl->order_list.elements) +
275
                               sl->group_list.elements,
276
                               can_skip_order_by ?
327.2.3 by Brian Aker
Refactoring of class Table
277
                               (order_st*) 0 : (order_st *)sl->order_list.first,
278
                               (order_st*) sl->group_list.first,
1 by brian
clean slate
279
                               sl->having,
327.2.3 by Brian Aker
Refactoring of class Table
280
                               (is_union_select ? (order_st*) 0 :
281
                                (order_st*) thd_arg->lex->proc_list.first),
1 by brian
clean slate
282
                               sl, this);
283
    /* There are no * in the statement anymore (for PS) */
284
    sl->with_wild= 0;
285
286
    if (saved_error || (saved_error= thd_arg->is_fatal_error))
287
      goto err;
288
    /*
289
      Use items list of underlaid select for derived tables to preserve
290
      information about fields lengths and exact types
291
    */
292
    if (!is_union_select)
293
      types= first_sl->item_list;
294
    else if (sl == first_sl)
295
    {
296
      /*
297
        We need to create an empty table object. It is used
298
        to create tmp_table fields in Item_type_holder.
299
        The main reason of this is that we can't create
300
        field object without table.
301
      */
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
302
      assert(!empty_table);
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
303
      empty_table= (Table*) thd->calloc(sizeof(Table));
1 by brian
clean slate
304
      types.empty();
305
      List_iterator_fast<Item> it(sl->item_list);
306
      Item *item_tmp;
307
      while ((item_tmp= it++))
308
      {
309
	/* Error's in 'new' will be detected after loop */
310
	types.push_back(new Item_type_holder(thd_arg, item_tmp));
311
      }
312
313
      if (thd_arg->is_fatal_error)
314
	goto err; // out of memory
315
    }
316
    else
317
    {
318
      if (types.elements != sl->item_list.elements)
319
      {
320
	my_message(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT,
321
		   ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT),MYF(0));
322
	goto err;
323
      }
324
      List_iterator_fast<Item> it(sl->item_list);
325
      List_iterator_fast<Item> tp(types);	
326
      Item *type, *item_tmp;
327
      while ((type= tp++, item_tmp= it++))
328
      {
329
        if (((Item_type_holder*)type)->join_types(thd_arg, item_tmp))
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
330
	  return(true);
1 by brian
clean slate
331
      }
332
    }
333
  }
334
335
  if (is_union_select)
336
  {
337
    /*
338
      Check that it was possible to aggregate
339
      all collations together for UNION.
340
    */
341
    List_iterator_fast<Item> tp(types);
342
    Item *type;
151 by Brian Aker
Ulonglong to uint64_t
343
    uint64_t create_options;
1 by brian
clean slate
344
345
    while ((type= tp++))
346
    {
347
      if (type->result_type() == STRING_RESULT &&
348
          type->collation.derivation == DERIVATION_NONE)
349
      {
350
        my_error(ER_CANT_AGGREGATE_NCOLLATIONS, MYF(0), "UNION");
351
        goto err;
352
      }
353
    }
354
    
355
    create_options= (first_sl->options | thd_arg->options |
356
                     TMP_TABLE_ALL_COLUMNS);
357
358
    if (union_result->create_result_table(thd, &types, test(union_distinct),
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
359
                                          create_options, "", false))
1 by brian
clean slate
360
      goto err;
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
361
    memset(&result_table_list, 0, sizeof(result_table_list));
1 by brian
clean slate
362
    result_table_list.db= (char*) "";
363
    result_table_list.table_name= result_table_list.alias= (char*) "union";
364
    result_table_list.table= table= union_result->table;
365
366
    thd_arg->lex->current_select= lex_select_save;
367
    if (!item_list.elements)
368
    {
369
      saved_error= table->fill_item_list(&item_list);
370
      if (saved_error)
371
        goto err;
372
    }
373
    else
374
    {
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
375
      assert(thd->stmt_arena->is_conventional() == false);
1 by brian
clean slate
376
      /*
377
        We're in execution of a prepared statement or stored procedure:
378
        reset field items to point at fields from the created temporary table.
379
      */
380
      table->reset_item_list(&item_list);
381
    }
382
  }
383
384
  thd_arg->lex->current_select= lex_select_save;
385
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
386
  return(saved_error || thd_arg->is_fatal_error);
1 by brian
clean slate
387
388
err:
389
  thd_arg->lex->current_select= lex_select_save;
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
390
  return(true);
1 by brian
clean slate
391
}
392
393
394
bool st_select_lex_unit::exec()
395
{
396
  SELECT_LEX *lex_select_save= thd->lex->current_select;
397
  SELECT_LEX *select_cursor=first_select();
151 by Brian Aker
Ulonglong to uint64_t
398
  uint64_t add_rows=0;
1 by brian
clean slate
399
  ha_rows examined_rows= 0;
400
401
  if (executed && !uncacheable && !describe)
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
402
    return(false);
1 by brian
clean slate
403
  executed= 1;
404
  
405
  if (uncacheable || !item || !item->assigned() || describe)
406
  {
407
    if (item)
408
      item->reset_value_registration();
409
    if (optimized && item)
410
    {
411
      if (item->assigned())
412
      {
413
        item->assigned(0); // We will reinit & rexecute unit
414
        item->reset();
415
        table->file->ha_delete_all_rows();
416
      }
417
      /* re-enabling indexes for next subselect iteration */
418
      if (union_distinct && table->file->ha_enable_indexes(HA_KEY_SWITCH_ALL))
419
      {
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
420
        assert(0);
1 by brian
clean slate
421
      }
422
    }
423
    for (SELECT_LEX *sl= select_cursor; sl; sl= sl->next_select())
424
    {
425
      ha_rows records_at_start= 0;
426
      thd->lex->current_select= sl;
427
428
      if (optimized)
429
	saved_error= sl->join->reinit();
430
      else
431
      {
432
        set_limit(sl);
433
	if (sl == global_parameters || describe)
434
	{
435
	  offset_limit_cnt= 0;
436
	  /*
327.2.3 by Brian Aker
Refactoring of class Table
437
	    We can't use LIMIT at this stage if we are using order_st BY for the
1 by brian
clean slate
438
	    whole query
439
	  */
440
	  if (sl->order_list.first || describe)
441
	    select_limit_cnt= HA_POS_ERROR;
442
        }
443
444
        /*
445
          When using braces, SQL_CALC_FOUND_ROWS affects the whole query:
446
          we don't calculate found_rows() per union part.
447
          Otherwise, SQL_CALC_FOUND_ROWS should be done on all sub parts.
448
        */
449
        sl->join->select_options= 
450
          (select_limit_cnt == HA_POS_ERROR || sl->braces) ?
451
          sl->options & ~OPTION_FOUND_ROWS : sl->options | found_rows_for_union;
452
327.2.4 by Brian Aker
Refactoring table.h
453
        /* dump_TableList_struct(select_lex, select_lex->leaf_tables); */
1 by brian
clean slate
454
        if (sl->join->flatten_subqueries())
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
455
          return(true);
1 by brian
clean slate
456
327.2.4 by Brian Aker
Refactoring table.h
457
        /* dump_TableList_struct(select_lex, select_lex->leaf_tables); */
1 by brian
clean slate
458
	saved_error= sl->join->optimize();
459
      }
460
      if (!saved_error)
461
      {
462
	records_at_start= table->file->stats.records;
463
	sl->join->exec();
464
        if (sl == union_distinct)
465
	{
466
	  if (table->file->ha_disable_indexes(HA_KEY_SWITCH_ALL))
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
467
	    return(true);
1 by brian
clean slate
468
	  table->no_keyread=1;
469
	}
470
	saved_error= sl->join->error;
471
	offset_limit_cnt= (ha_rows)(sl->offset_limit ?
472
                                    sl->offset_limit->val_uint() :
473
                                    0);
474
	if (!saved_error)
475
	{
476
	  examined_rows+= thd->examined_row_count;
477
	  if (union_result->flush())
478
	  {
479
	    thd->lex->current_select= lex_select_save;
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
480
	    return(1);
1 by brian
clean slate
481
	  }
482
	}
483
      }
484
      if (saved_error)
485
      {
486
	thd->lex->current_select= lex_select_save;
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
487
	return(saved_error);
1 by brian
clean slate
488
      }
489
      /* Needed for the following test and for records_at_start in next loop */
490
      int error= table->file->info(HA_STATUS_VARIABLE);
491
      if(error)
492
      {
493
        table->file->print_error(error, MYF(0));
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
494
        return(1);
1 by brian
clean slate
495
      }
496
      if (found_rows_for_union && !sl->braces && 
497
          select_limit_cnt != HA_POS_ERROR)
498
      {
499
	/*
500
	  This is a union without braces. Remember the number of rows that
501
	  could also have been part of the result set.
502
	  We get this from the difference of between total number of possible
503
	  rows and actual rows added to the temporary table.
504
	*/
151 by Brian Aker
Ulonglong to uint64_t
505
	add_rows+= (uint64_t) (thd->limit_found_rows - (uint64_t)
1 by brian
clean slate
506
			      ((table->file->stats.records -  records_at_start)));
507
      }
508
    }
509
  }
510
  optimized= 1;
511
512
  /* Send result to 'result' */
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
513
  saved_error= true;
1 by brian
clean slate
514
  {
515
    if (!thd->is_fatal_error)				// Check if EOM
516
    {
517
      set_limit(global_parameters);
518
      init_prepare_fake_select_lex(thd);
519
      JOIN *join= fake_select_lex->join;
520
      if (!join)
521
      {
522
	/*
523
	  allocate JOIN for fake select only once (prevent
524
	  mysql_select automatic allocation)
525
          TODO: The above is nonsense. mysql_select() will not allocate the
526
          join if one already exists. There must be some other reason why we
527
          don't let it allocate the join. Perhaps this is because we need
528
          some special parameter values passed to join constructor?
529
	*/
530
	if (!(fake_select_lex->join= new JOIN(thd, item_list,
531
					      fake_select_lex->options, result)))
532
	{
533
	  fake_select_lex->table_list.empty();
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
534
	  return(true);
1 by brian
clean slate
535
	}
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
536
        fake_select_lex->join->no_const_tables= true;
1 by brian
clean slate
537
538
	/*
539
	  Fake st_select_lex should have item list for correctref_array
540
	  allocation.
541
	*/
542
	fake_select_lex->item_list= item_list;
543
        saved_error= mysql_select(thd, &fake_select_lex->ref_pointer_array,
544
                              &result_table_list,
545
                              0, item_list, NULL,
546
                              global_parameters->order_list.elements,
327.2.3 by Brian Aker
Refactoring of class Table
547
                              (order_st*)global_parameters->order_list.first,
548
                              (order_st*) NULL, NULL, (order_st*) NULL,
1 by brian
clean slate
549
                              fake_select_lex->options | SELECT_NO_UNLOCK,
550
                              result, this, fake_select_lex);
551
      }
552
      else
553
      {
554
        if (describe)
555
        {
556
          /*
557
            In EXPLAIN command, constant subqueries that do not use any
558
            tables are executed two times:
559
             - 1st time is a real evaluation to get the subquery value
560
             - 2nd time is to produce EXPLAIN output rows.
561
            1st execution sets certain members (e.g. select_result) to perform
562
            subquery execution rather than EXPLAIN line production. In order 
563
            to reset them back, we re-do all of the actions (yes it is ugly):
564
          */
565
	  join->init(thd, item_list, fake_select_lex->options, result);
566
          saved_error= mysql_select(thd, &fake_select_lex->ref_pointer_array,
567
                                &result_table_list,
568
                                0, item_list, NULL,
569
                                global_parameters->order_list.elements,
327.2.3 by Brian Aker
Refactoring of class Table
570
                                (order_st*)global_parameters->order_list.first,
571
                                (order_st*) NULL, NULL, (order_st*) NULL,
1 by brian
clean slate
572
                                fake_select_lex->options | SELECT_NO_UNLOCK,
573
                                result, this, fake_select_lex);
574
        }
575
        else
576
        {
577
          join->examined_rows= 0;
578
          saved_error= join->reinit();
579
          join->exec();
580
        }
581
      }
582
583
      fake_select_lex->table_list.empty();
584
      if (!saved_error)
585
      {
151 by Brian Aker
Ulonglong to uint64_t
586
	thd->limit_found_rows = (uint64_t)table->file->stats.records + add_rows;
1 by brian
clean slate
587
        thd->examined_row_count+= examined_rows;
588
      }
589
      /*
590
	Mark for slow query log if any of the union parts didn't use
591
	indexes efficiently
592
      */
593
    }
594
  }
595
  thd->lex->current_select= lex_select_save;
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
596
  return(saved_error);
1 by brian
clean slate
597
}
598
599
600
bool st_select_lex_unit::cleanup()
601
{
602
  int error= 0;
603
604
  if (cleaned)
605
  {
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
606
    return(false);
1 by brian
clean slate
607
  }
608
  cleaned= 1;
609
610
  if (union_result)
611
  {
612
    delete union_result;
613
    union_result=0; // Safety
614
    if (table)
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
615
      table->free_tmp_table(thd);
1 by brian
clean slate
616
    table= 0; // Safety
617
  }
618
619
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
620
    error|= sl->cleanup();
621
622
  if (fake_select_lex)
623
  {
624
    JOIN *join;
625
    if ((join= fake_select_lex->join))
626
    {
627
      join->tables_list= 0;
628
      join->tables= 0;
629
    }
630
    error|= fake_select_lex->cleanup();
631
    if (fake_select_lex->order_list.elements)
632
    {
327.2.3 by Brian Aker
Refactoring of class Table
633
      order_st *ord;
634
      for (ord= (order_st*)fake_select_lex->order_list.first; ord; ord= ord->next)
1 by brian
clean slate
635
        (*ord->item)->cleanup();
636
    }
637
  }
638
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
639
  return(error);
1 by brian
clean slate
640
}
641
642
643
void st_select_lex_unit::reinit_exec_mechanism()
644
{
645
  prepared= optimized= executed= 0;
646
}
647
648
649
/*
650
  change select_result object of unit
651
652
  SYNOPSIS
653
    st_select_lex_unit::change_result()
654
    result	new select_result object
655
    old_result	old select_result object
656
657
  RETURN
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
658
    false - OK
659
    true  - error
1 by brian
clean slate
660
*/
661
662
bool st_select_lex_unit::change_result(select_result_interceptor *new_result,
663
                                       select_result_interceptor *old_result)
664
{
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
665
  bool res= false;
1 by brian
clean slate
666
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
667
  {
668
    if (sl->join && sl->join->result == old_result)
669
      if (sl->join->change_result(new_result))
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
670
	return true;
1 by brian
clean slate
671
  }
672
  if (fake_select_lex && fake_select_lex->join)
673
    res= fake_select_lex->join->change_result(new_result);
674
  return (res);
675
}
676
677
/*
678
  Get column type information for this unit.
679
680
  SYNOPSIS
681
    st_select_lex_unit::get_unit_column_types()
682
683
  DESCRIPTION
684
    For a single-select the column types are taken
685
    from the list of selected items. For a union this function
686
    assumes that st_select_lex_unit::prepare has been called
687
    and returns the type holders that were created for unioned
688
    column types of all selects.
689
690
  NOTES
691
    The implementation of this function should be in sync with
692
    st_select_lex_unit::prepare()
693
*/
694
695
List<Item> *st_select_lex_unit::get_unit_column_types()
696
{
697
  SELECT_LEX *sl= first_select();
698
699
  if (is_union())
700
  {
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
701
    assert(prepared);
1 by brian
clean slate
702
    /* Types are generated during prepare */
703
    return &types;
704
  }
705
706
  return &sl->item_list;
707
}
708
709
bool st_select_lex::cleanup()
710
{
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
711
  bool error= false;
1 by brian
clean slate
712
713
  if (join)
714
  {
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
715
    assert((st_select_lex*)join->select_lex == this);
1 by brian
clean slate
716
    error= join->destroy();
717
    delete join;
718
    join= 0;
719
  }
720
  for (SELECT_LEX_UNIT *lex_unit= first_inner_unit(); lex_unit ;
721
       lex_unit= lex_unit->next_unit())
722
  {
723
    error= (bool) ((uint) error | (uint) lex_unit->cleanup());
724
  }
725
  non_agg_fields.empty();
726
  inner_refs_list.empty();
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
727
  return(error);
1 by brian
clean slate
728
}
729
730
731
void st_select_lex::cleanup_all_joins(bool full)
732
{
733
  SELECT_LEX_UNIT *unit;
734
  SELECT_LEX *sl;
735
736
  if (join)
737
    join->cleanup(full);
738
739
  for (unit= first_inner_unit(); unit; unit= unit->next_unit())
740
    for (sl= unit->first_select(); sl; sl= sl->next_select())
741
      sl->cleanup_all_joins(full);
742
}