~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;
481 by Brian Aker
Remove all of uchar.
167
  fake_select_lex->table_list.link_in_list((unsigned char *)&result_table_list,
168
					   (unsigned char **)
1 by brian
clean slate
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();
404 by Brian Aker
Removed dead variable
173
174
  for (order_st *order= (order_st *) global_parameters->order_list.first;
175
       order;
176
       order= order->next)
177
    order->item= &order->item_ptr;
178
327.2.3 by Brian Aker
Refactoring of class Table
179
  for (order_st *order= (order_st *)global_parameters->order_list.first;
1 by brian
clean slate
180
       order;
181
       order=order->next)
182
  {
183
    (*order->item)->walk(&Item::change_context_processor, 0,
481 by Brian Aker
Remove all of uchar.
184
                         (unsigned char*) &fake_select_lex->context);
1 by brian
clean slate
185
  }
186
}
187
188
189
bool st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result,
202 by Brian Aker
Cleanup sql_lex to modern types.
190
                                 uint32_t additional_options)
1 by brian
clean slate
191
{
192
  SELECT_LEX *lex_select_save= thd_arg->lex->current_select;
193
  SELECT_LEX *sl, *first_sl= first_select();
194
  select_result *tmp_result;
195
  bool is_union_select;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
196
  Table *empty_table= 0;
1 by brian
clean slate
197
198
  describe= test(additional_options & SELECT_DESCRIBE);
199
200
  /*
201
    result object should be reassigned even if preparing already done for
202
    max/min subquery (ALL/ANY optimization)
203
  */
204
  result= sel_result;
205
206
  if (prepared)
207
  {
208
    if (describe)
209
    {
210
      /* fast reinit for EXPLAIN */
211
      for (sl= first_sl; sl; sl= sl->next_select())
212
      {
213
	sl->join->result= result;
214
	select_limit_cnt= HA_POS_ERROR;
215
	offset_limit_cnt= 0;
216
	if (result->prepare(sl->join->fields_list, this))
217
	{
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
218
	  return(true);
1 by brian
clean slate
219
	}
220
	sl->join->select_options|= SELECT_DESCRIBE;
221
	sl->join->reinit();
222
      }
223
    }
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
224
    return(false);
1 by brian
clean slate
225
  }
226
  prepared= 1;
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
227
  saved_error= false;
1 by brian
clean slate
228
  
229
  thd_arg->lex->current_select= sl= first_sl;
230
  found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
231
  is_union_select= is_union() || fake_select_lex;
232
233
  /* Global option */
234
235
  if (is_union_select)
236
  {
237
    if (!(tmp_result= union_result= new select_union))
238
      goto err;
239
    if (describe)
240
      tmp_result= sel_result;
241
  }
242
  else
243
    tmp_result= sel_result;
244
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
245
  sl->context.resolve_in_select_list= true;
1 by brian
clean slate
246
247
  for (;sl; sl= sl->next_select())
248
  {
249
    bool can_skip_order_by;
250
    sl->options|=  SELECT_NO_UNLOCK;
251
    JOIN *join= new JOIN(thd_arg, sl->item_list, 
252
			 sl->options | thd_arg->options | additional_options,
253
			 tmp_result);
254
    /*
255
      setup_tables_done_option should be set only for very first SELECT,
256
      because it protect from secont setup_tables call for select-like non
257
      select commands (DELETE/INSERT/...) and they use only very first
258
      SELECT (for union it can be only INSERT ... SELECT).
259
    */
260
    additional_options&= ~OPTION_SETUP_TABLES_DONE;
261
    if (!join)
262
      goto err;
263
264
    thd_arg->lex->current_select= sl;
265
266
    can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit);
267
268
    saved_error= join->prepare(&sl->ref_pointer_array,
327.2.4 by Brian Aker
Refactoring table.h
269
                               (TableList*) sl->table_list.first,
1 by brian
clean slate
270
                               sl->with_wild,
271
                               sl->where,
272
                               (can_skip_order_by ? 0 :
273
                                sl->order_list.elements) +
274
                               sl->group_list.elements,
275
                               can_skip_order_by ?
327.2.3 by Brian Aker
Refactoring of class Table
276
                               (order_st*) 0 : (order_st *)sl->order_list.first,
277
                               (order_st*) sl->group_list.first,
1 by brian
clean slate
278
                               sl->having,
327.2.3 by Brian Aker
Refactoring of class Table
279
                               (is_union_select ? (order_st*) 0 :
280
                                (order_st*) thd_arg->lex->proc_list.first),
1 by brian
clean slate
281
                               sl, this);
282
    /* There are no * in the statement anymore (for PS) */
283
    sl->with_wild= 0;
284
285
    if (saved_error || (saved_error= thd_arg->is_fatal_error))
286
      goto err;
287
    /*
288
      Use items list of underlaid select for derived tables to preserve
289
      information about fields lengths and exact types
290
    */
291
    if (!is_union_select)
292
      types= first_sl->item_list;
293
    else if (sl == first_sl)
294
    {
295
      /*
296
        We need to create an empty table object. It is used
297
        to create tmp_table fields in Item_type_holder.
298
        The main reason of this is that we can't create
299
        field object without table.
300
      */
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
301
      assert(!empty_table);
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
302
      empty_table= (Table*) thd->calloc(sizeof(Table));
1 by brian
clean slate
303
      types.empty();
304
      List_iterator_fast<Item> it(sl->item_list);
305
      Item *item_tmp;
306
      while ((item_tmp= it++))
307
      {
308
	/* Error's in 'new' will be detected after loop */
309
	types.push_back(new Item_type_holder(thd_arg, item_tmp));
310
      }
311
312
      if (thd_arg->is_fatal_error)
313
	goto err; // out of memory
314
    }
315
    else
316
    {
317
      if (types.elements != sl->item_list.elements)
318
      {
319
	my_message(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT,
320
		   ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT),MYF(0));
321
	goto err;
322
      }
323
      List_iterator_fast<Item> it(sl->item_list);
324
      List_iterator_fast<Item> tp(types);	
325
      Item *type, *item_tmp;
326
      while ((type= tp++, item_tmp= it++))
327
      {
328
        if (((Item_type_holder*)type)->join_types(thd_arg, item_tmp))
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
329
	  return(true);
1 by brian
clean slate
330
      }
331
    }
332
  }
333
334
  if (is_union_select)
335
  {
336
    /*
337
      Check that it was possible to aggregate
338
      all collations together for UNION.
339
    */
340
    List_iterator_fast<Item> tp(types);
341
    Item *type;
151 by Brian Aker
Ulonglong to uint64_t
342
    uint64_t create_options;
1 by brian
clean slate
343
344
    while ((type= tp++))
345
    {
346
      if (type->result_type() == STRING_RESULT &&
347
          type->collation.derivation == DERIVATION_NONE)
348
      {
349
        my_error(ER_CANT_AGGREGATE_NCOLLATIONS, MYF(0), "UNION");
350
        goto err;
351
      }
352
    }
353
    
354
    create_options= (first_sl->options | thd_arg->options |
355
                     TMP_TABLE_ALL_COLUMNS);
356
357
    if (union_result->create_result_table(thd, &types, test(union_distinct),
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
358
                                          create_options, "", false))
1 by brian
clean slate
359
      goto err;
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
360
    memset(&result_table_list, 0, sizeof(result_table_list));
1 by brian
clean slate
361
    result_table_list.db= (char*) "";
362
    result_table_list.table_name= result_table_list.alias= (char*) "union";
363
    result_table_list.table= table= union_result->table;
364
365
    thd_arg->lex->current_select= lex_select_save;
366
    if (!item_list.elements)
367
    {
368
      saved_error= table->fill_item_list(&item_list);
369
      if (saved_error)
370
        goto err;
371
    }
372
    else
373
    {
374
      /*
375
        We're in execution of a prepared statement or stored procedure:
376
        reset field items to point at fields from the created temporary table.
377
      */
401 by Brian Aker
Partial Query_arena removal
378
      assert(1);
1 by brian
clean slate
379
    }
380
  }
381
382
  thd_arg->lex->current_select= lex_select_save;
383
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
384
  return(saved_error || thd_arg->is_fatal_error);
1 by brian
clean slate
385
386
err:
387
  thd_arg->lex->current_select= lex_select_save;
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
388
  return(true);
1 by brian
clean slate
389
}
390
391
392
bool st_select_lex_unit::exec()
393
{
394
  SELECT_LEX *lex_select_save= thd->lex->current_select;
395
  SELECT_LEX *select_cursor=first_select();
151 by Brian Aker
Ulonglong to uint64_t
396
  uint64_t add_rows=0;
1 by brian
clean slate
397
  ha_rows examined_rows= 0;
398
399
  if (executed && !uncacheable && !describe)
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
400
    return(false);
1 by brian
clean slate
401
  executed= 1;
402
  
403
  if (uncacheable || !item || !item->assigned() || describe)
404
  {
405
    if (item)
406
      item->reset_value_registration();
407
    if (optimized && item)
408
    {
409
      if (item->assigned())
410
      {
411
        item->assigned(0); // We will reinit & rexecute unit
412
        item->reset();
413
        table->file->ha_delete_all_rows();
414
      }
415
      /* re-enabling indexes for next subselect iteration */
416
      if (union_distinct && table->file->ha_enable_indexes(HA_KEY_SWITCH_ALL))
417
      {
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
418
        assert(0);
1 by brian
clean slate
419
      }
420
    }
421
    for (SELECT_LEX *sl= select_cursor; sl; sl= sl->next_select())
422
    {
423
      ha_rows records_at_start= 0;
424
      thd->lex->current_select= sl;
425
426
      if (optimized)
427
	saved_error= sl->join->reinit();
428
      else
429
      {
430
        set_limit(sl);
431
	if (sl == global_parameters || describe)
432
	{
433
	  offset_limit_cnt= 0;
434
	  /*
327.2.3 by Brian Aker
Refactoring of class Table
435
	    We can't use LIMIT at this stage if we are using order_st BY for the
1 by brian
clean slate
436
	    whole query
437
	  */
438
	  if (sl->order_list.first || describe)
439
	    select_limit_cnt= HA_POS_ERROR;
440
        }
441
442
        /*
443
          When using braces, SQL_CALC_FOUND_ROWS affects the whole query:
444
          we don't calculate found_rows() per union part.
445
          Otherwise, SQL_CALC_FOUND_ROWS should be done on all sub parts.
446
        */
447
        sl->join->select_options= 
448
          (select_limit_cnt == HA_POS_ERROR || sl->braces) ?
449
          sl->options & ~OPTION_FOUND_ROWS : sl->options | found_rows_for_union;
450
327.2.4 by Brian Aker
Refactoring table.h
451
        /* dump_TableList_struct(select_lex, select_lex->leaf_tables); */
1 by brian
clean slate
452
        if (sl->join->flatten_subqueries())
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
453
          return(true);
1 by brian
clean slate
454
327.2.4 by Brian Aker
Refactoring table.h
455
        /* dump_TableList_struct(select_lex, select_lex->leaf_tables); */
1 by brian
clean slate
456
	saved_error= sl->join->optimize();
457
      }
458
      if (!saved_error)
459
      {
460
	records_at_start= table->file->stats.records;
461
	sl->join->exec();
462
        if (sl == union_distinct)
463
	{
464
	  if (table->file->ha_disable_indexes(HA_KEY_SWITCH_ALL))
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
465
	    return(true);
1 by brian
clean slate
466
	  table->no_keyread=1;
467
	}
468
	saved_error= sl->join->error;
469
	offset_limit_cnt= (ha_rows)(sl->offset_limit ?
470
                                    sl->offset_limit->val_uint() :
471
                                    0);
472
	if (!saved_error)
473
	{
474
	  examined_rows+= thd->examined_row_count;
475
	  if (union_result->flush())
476
	  {
477
	    thd->lex->current_select= lex_select_save;
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
478
	    return(1);
1 by brian
clean slate
479
	  }
480
	}
481
      }
482
      if (saved_error)
483
      {
484
	thd->lex->current_select= lex_select_save;
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
485
	return(saved_error);
1 by brian
clean slate
486
      }
487
      /* Needed for the following test and for records_at_start in next loop */
488
      int error= table->file->info(HA_STATUS_VARIABLE);
489
      if(error)
490
      {
491
        table->file->print_error(error, MYF(0));
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
492
        return(1);
1 by brian
clean slate
493
      }
494
      if (found_rows_for_union && !sl->braces && 
495
          select_limit_cnt != HA_POS_ERROR)
496
      {
497
	/*
498
	  This is a union without braces. Remember the number of rows that
499
	  could also have been part of the result set.
500
	  We get this from the difference of between total number of possible
501
	  rows and actual rows added to the temporary table.
502
	*/
151 by Brian Aker
Ulonglong to uint64_t
503
	add_rows+= (uint64_t) (thd->limit_found_rows - (uint64_t)
1 by brian
clean slate
504
			      ((table->file->stats.records -  records_at_start)));
505
      }
506
    }
507
  }
508
  optimized= 1;
509
510
  /* Send result to 'result' */
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
511
  saved_error= true;
1 by brian
clean slate
512
  {
513
    if (!thd->is_fatal_error)				// Check if EOM
514
    {
515
      set_limit(global_parameters);
516
      init_prepare_fake_select_lex(thd);
517
      JOIN *join= fake_select_lex->join;
518
      if (!join)
519
      {
520
	/*
521
	  allocate JOIN for fake select only once (prevent
522
	  mysql_select automatic allocation)
523
          TODO: The above is nonsense. mysql_select() will not allocate the
524
          join if one already exists. There must be some other reason why we
525
          don't let it allocate the join. Perhaps this is because we need
526
          some special parameter values passed to join constructor?
527
	*/
528
	if (!(fake_select_lex->join= new JOIN(thd, item_list,
529
					      fake_select_lex->options, result)))
530
	{
531
	  fake_select_lex->table_list.empty();
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
532
	  return(true);
1 by brian
clean slate
533
	}
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
534
        fake_select_lex->join->no_const_tables= true;
1 by brian
clean slate
535
536
	/*
537
	  Fake st_select_lex should have item list for correctref_array
538
	  allocation.
539
	*/
540
	fake_select_lex->item_list= item_list;
541
        saved_error= mysql_select(thd, &fake_select_lex->ref_pointer_array,
542
                              &result_table_list,
543
                              0, item_list, NULL,
544
                              global_parameters->order_list.elements,
327.2.3 by Brian Aker
Refactoring of class Table
545
                              (order_st*)global_parameters->order_list.first,
546
                              (order_st*) NULL, NULL, (order_st*) NULL,
1 by brian
clean slate
547
                              fake_select_lex->options | SELECT_NO_UNLOCK,
548
                              result, this, fake_select_lex);
549
      }
550
      else
551
      {
552
        if (describe)
553
        {
554
          /*
555
            In EXPLAIN command, constant subqueries that do not use any
556
            tables are executed two times:
557
             - 1st time is a real evaluation to get the subquery value
558
             - 2nd time is to produce EXPLAIN output rows.
559
            1st execution sets certain members (e.g. select_result) to perform
560
            subquery execution rather than EXPLAIN line production. In order 
561
            to reset them back, we re-do all of the actions (yes it is ugly):
562
          */
563
	  join->init(thd, item_list, fake_select_lex->options, result);
564
          saved_error= mysql_select(thd, &fake_select_lex->ref_pointer_array,
565
                                &result_table_list,
566
                                0, item_list, NULL,
567
                                global_parameters->order_list.elements,
327.2.3 by Brian Aker
Refactoring of class Table
568
                                (order_st*)global_parameters->order_list.first,
569
                                (order_st*) NULL, NULL, (order_st*) NULL,
1 by brian
clean slate
570
                                fake_select_lex->options | SELECT_NO_UNLOCK,
571
                                result, this, fake_select_lex);
572
        }
573
        else
574
        {
575
          join->examined_rows= 0;
576
          saved_error= join->reinit();
577
          join->exec();
578
        }
579
      }
580
581
      fake_select_lex->table_list.empty();
582
      if (!saved_error)
583
      {
151 by Brian Aker
Ulonglong to uint64_t
584
	thd->limit_found_rows = (uint64_t)table->file->stats.records + add_rows;
1 by brian
clean slate
585
        thd->examined_row_count+= examined_rows;
586
      }
587
      /*
588
	Mark for slow query log if any of the union parts didn't use
589
	indexes efficiently
590
      */
591
    }
592
  }
593
  thd->lex->current_select= lex_select_save;
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
594
  return(saved_error);
1 by brian
clean slate
595
}
596
597
598
bool st_select_lex_unit::cleanup()
599
{
600
  int error= 0;
601
602
  if (cleaned)
603
  {
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
604
    return(false);
1 by brian
clean slate
605
  }
606
  cleaned= 1;
607
608
  if (union_result)
609
  {
610
    delete union_result;
611
    union_result=0; // Safety
612
    if (table)
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
613
      table->free_tmp_table(thd);
1 by brian
clean slate
614
    table= 0; // Safety
615
  }
616
617
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
618
    error|= sl->cleanup();
619
620
  if (fake_select_lex)
621
  {
622
    JOIN *join;
623
    if ((join= fake_select_lex->join))
624
    {
625
      join->tables_list= 0;
626
      join->tables= 0;
627
    }
628
    error|= fake_select_lex->cleanup();
629
    if (fake_select_lex->order_list.elements)
630
    {
327.2.3 by Brian Aker
Refactoring of class Table
631
      order_st *ord;
632
      for (ord= (order_st*)fake_select_lex->order_list.first; ord; ord= ord->next)
1 by brian
clean slate
633
        (*ord->item)->cleanup();
634
    }
635
  }
636
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
637
  return(error);
1 by brian
clean slate
638
}
639
640
641
void st_select_lex_unit::reinit_exec_mechanism()
642
{
643
  prepared= optimized= executed= 0;
644
}
645
646
647
/*
648
  change select_result object of unit
649
650
  SYNOPSIS
651
    st_select_lex_unit::change_result()
652
    result	new select_result object
653
    old_result	old select_result object
654
655
  RETURN
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
656
    false - OK
657
    true  - error
1 by brian
clean slate
658
*/
659
660
bool st_select_lex_unit::change_result(select_result_interceptor *new_result,
661
                                       select_result_interceptor *old_result)
662
{
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
663
  bool res= false;
1 by brian
clean slate
664
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
665
  {
666
    if (sl->join && sl->join->result == old_result)
667
      if (sl->join->change_result(new_result))
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
668
	return true;
1 by brian
clean slate
669
  }
670
  if (fake_select_lex && fake_select_lex->join)
671
    res= fake_select_lex->join->change_result(new_result);
672
  return (res);
673
}
674
675
/*
676
  Get column type information for this unit.
677
678
  SYNOPSIS
679
    st_select_lex_unit::get_unit_column_types()
680
681
  DESCRIPTION
682
    For a single-select the column types are taken
683
    from the list of selected items. For a union this function
684
    assumes that st_select_lex_unit::prepare has been called
685
    and returns the type holders that were created for unioned
686
    column types of all selects.
687
688
  NOTES
689
    The implementation of this function should be in sync with
690
    st_select_lex_unit::prepare()
691
*/
692
693
List<Item> *st_select_lex_unit::get_unit_column_types()
694
{
695
  SELECT_LEX *sl= first_select();
696
697
  if (is_union())
698
  {
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
699
    assert(prepared);
1 by brian
clean slate
700
    /* Types are generated during prepare */
701
    return &types;
702
  }
703
704
  return &sl->item_list;
705
}
706
707
bool st_select_lex::cleanup()
708
{
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
709
  bool error= false;
1 by brian
clean slate
710
711
  if (join)
712
  {
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
713
    assert((st_select_lex*)join->select_lex == this);
1 by brian
clean slate
714
    error= join->destroy();
715
    delete join;
716
    join= 0;
717
  }
718
  for (SELECT_LEX_UNIT *lex_unit= first_inner_unit(); lex_unit ;
719
       lex_unit= lex_unit->next_unit())
720
  {
721
    error= (bool) ((uint) error | (uint) lex_unit->cleanup());
722
  }
723
  non_agg_fields.empty();
724
  inner_refs_list.empty();
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
725
  return(error);
1 by brian
clean slate
726
}
727
728
729
void st_select_lex::cleanup_all_joins(bool full)
730
{
731
  SELECT_LEX_UNIT *unit;
732
  SELECT_LEX *sl;
733
734
  if (join)
735
    join->cleanup(full);
736
737
  for (unit= first_inner_unit(); unit; unit= unit->next_unit())
738
    for (sl= unit->first_select(); sl; sl= sl->next_select())
739
      sl->cleanup_all_joins(full);
740
}