~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/sql_union.cc

  • Committer: Jay Pipes
  • Date: 2008-07-18 20:20:47 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: jay@mysql.com-20080718202047-1tnd4i9z3k3cvg9v
DBUG entirely removed from server and client

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
15
16
 
16
17
/*
17
18
  UNION  of select's
18
19
  UNION's  were introduced by Monty and Sinisa <sinisa@mysql.com>
19
20
*/
20
 
#include <config.h>
21
 
 
22
 
#include <drizzled/sql_select.h>
23
 
#include <drizzled/error.h>
24
 
#include <drizzled/item/type_holder.h>
25
 
#include <drizzled/sql_base.h>
26
 
#include <drizzled/sql_union.h>
27
 
#include <drizzled/select_union.h>
28
 
#include <drizzled/sql_lex.h>
29
 
#include <drizzled/session.h>
30
 
 
31
 
namespace drizzled
32
 
{
33
 
 
34
 
bool drizzle_union(Session *session, LEX *, select_result *result,
35
 
                   Select_Lex_Unit *unit, uint64_t setup_tables_done_option)
 
21
 
 
22
 
 
23
#include "mysql_priv.h"
 
24
#include "sql_select.h"
 
25
 
 
26
bool mysql_union(THD *thd,
 
27
                 LEX *lex __attribute__((__unused__)),
 
28
                 select_result *result,
 
29
                 SELECT_LEX_UNIT *unit, ulong setup_tables_done_option)
36
30
{
37
31
  bool res;
38
 
  if (!(res= unit->prepare(session, result, SELECT_NO_UNLOCK |
 
32
  if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK |
39
33
                           setup_tables_done_option)))
40
34
    res= unit->exec();
41
35
  if (res)
48
42
** store records in temporary table for UNION
49
43
***************************************************************************/
50
44
 
51
 
int select_union::prepare(List<Item> &, Select_Lex_Unit *u)
 
45
int select_union::prepare(List<Item> &list __attribute__((__unused__)),
 
46
                          SELECT_LEX_UNIT *u)
52
47
{
53
48
  unit= u;
54
49
  return 0;
63
58
    unit->offset_limit_cnt--;
64
59
    return 0;
65
60
  }
66
 
  fill_record(session, table->getFields(), values, true);
67
 
  if (session->is_error())
 
61
  fill_record(thd, table->field, values, 1);
 
62
  if (thd->is_error())
68
63
    return 1;
69
64
 
70
 
  if ((error= table->cursor->insertRecord(table->getInsertRecord())))
 
65
  if ((error= table->file->ha_write_row(table->record[0])))
71
66
  {
72
67
    /* create_myisam_from_heap will generate error if needed */
73
 
    if (table->cursor->is_fatal_error(error, HA_CHECK_DUP))
74
 
    {
75
 
      my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
76
 
      return true;
77
 
    }
 
68
    if (table->file->is_fatal_error(error, HA_CHECK_DUP) &&
 
69
        create_myisam_from_heap(thd, table, tmp_table_param.start_recinfo, 
 
70
                                &tmp_table_param.recinfo, error, 1))
 
71
      return 1;
78
72
  }
79
73
  return 0;
80
74
}
89
83
bool select_union::flush()
90
84
{
91
85
  int error;
92
 
  if ((error=table->cursor->extra(HA_EXTRA_NO_CACHE)))
 
86
  if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
93
87
  {
94
 
    table->print_error(error, MYF(0));
 
88
    table->file->print_error(error, MYF(0));
95
89
    return 1;
96
90
  }
97
91
  return 0;
102
96
 
103
97
  SYNOPSIS
104
98
    select_union::create_result_table()
105
 
      session                thread handle
 
99
      thd                thread handle
106
100
      column_types       a list of items used to define columns of the
107
101
                         temporary table
108
102
      is_union_distinct  if set, the temporary table will eliminate
109
103
                         duplicates on insert
110
104
      options            create options
111
105
      table_alias        name of the temporary table
 
106
      bit_fields_as_long convert bit fields to uint64_t
112
107
 
113
108
  DESCRIPTION
114
109
    Create a temporary table that is used to store the result of a UNION,
120
115
*/
121
116
 
122
117
bool
123
 
select_union::create_result_table(Session *session_arg, List<Item> *column_types,
 
118
select_union::create_result_table(THD *thd_arg, List<Item> *column_types,
124
119
                                  bool is_union_distinct, uint64_t options,
125
 
                                  const char *table_alias)
 
120
                                  const char *table_alias,
 
121
                                  bool bit_fields_as_long)
126
122
{
127
 
  assert(table == NULL);
 
123
  assert(table == 0);
128
124
  tmp_table_param.init();
129
125
  tmp_table_param.field_count= column_types->elements;
 
126
  tmp_table_param.bit_fields_as_long= bit_fields_as_long;
130
127
 
131
 
  if (! (table= create_tmp_table(session_arg, &tmp_table_param, *column_types,
132
 
                                 (Order*) NULL, is_union_distinct, 1,
 
128
  if (! (table= create_tmp_table(thd_arg, &tmp_table_param, *column_types,
 
129
                                 (ORDER*) 0, is_union_distinct, 1,
133
130
                                 options, HA_POS_ERROR, (char*) table_alias)))
134
 
  {
135
131
    return true;
136
 
  }
137
 
 
138
 
  table->cursor->extra(HA_EXTRA_WRITE_CACHE);
139
 
  table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
140
 
 
 
132
  table->file->extra(HA_EXTRA_WRITE_CACHE);
 
133
  table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
141
134
  return false;
142
135
}
143
136
 
151
144
 
152
145
void select_union::cleanup()
153
146
{
154
 
  table->cursor->extra(HA_EXTRA_RESET_STATE);
155
 
  table->cursor->ha_delete_all_rows();
156
 
  table->free_io_cache();
157
 
  table->filesort_free_buffers();
 
147
  table->file->extra(HA_EXTRA_RESET_STATE);
 
148
  table->file->ha_delete_all_rows();
 
149
  free_io_cache(table);
 
150
  filesort_free_buffers(table,0);
158
151
}
159
152
 
160
153
 
162
155
  initialization procedures before fake_select_lex preparation()
163
156
 
164
157
  SYNOPSIS
165
 
    Select_Lex_Unit::init_prepare_fake_select_lex()
166
 
    session             - thread handler
 
158
    st_select_lex_unit::init_prepare_fake_select_lex()
 
159
    thd         - thread handler
167
160
 
168
161
  RETURN
169
162
    options of SELECT
170
163
*/
171
164
 
172
165
void
173
 
Select_Lex_Unit::init_prepare_fake_select_lex(Session *session_arg)
 
166
st_select_lex_unit::init_prepare_fake_select_lex(THD *thd_arg) 
174
167
{
175
 
  session_arg->getLex()->current_select= fake_select_lex;
176
 
  fake_select_lex->table_list.link_in_list((unsigned char *)&result_table_list,
177
 
                                           (unsigned char **)
 
168
  thd_arg->lex->current_select= fake_select_lex;
 
169
  fake_select_lex->table_list.link_in_list((uchar *)&result_table_list,
 
170
                                           (uchar **)
178
171
                                           &result_table_list.next_local);
179
 
  fake_select_lex->context.table_list=
180
 
    fake_select_lex->context.first_name_resolution_table=
 
172
  fake_select_lex->context.table_list= 
 
173
    fake_select_lex->context.first_name_resolution_table= 
181
174
    fake_select_lex->get_table_list();
182
 
 
183
 
  for (Order *order= (Order *) global_parameters->order_list.first;
184
 
       order;
185
 
       order= order->next)
186
 
    order->item= &order->item_ptr;
187
 
 
188
 
  for (Order *order= (Order *)global_parameters->order_list.first;
 
175
  if (!fake_select_lex->first_execution)
 
176
  {
 
177
    for (ORDER *order= (ORDER *) global_parameters->order_list.first;
 
178
         order;
 
179
         order= order->next)
 
180
      order->item= &order->item_ptr;
 
181
  }
 
182
  for (ORDER *order= (ORDER *)global_parameters->order_list.first;
189
183
       order;
190
184
       order=order->next)
191
185
  {
192
186
    (*order->item)->walk(&Item::change_context_processor, 0,
193
 
                         (unsigned char*) &fake_select_lex->context);
 
187
                         (uchar*) &fake_select_lex->context);
194
188
  }
195
189
}
196
190
 
197
191
 
198
 
bool Select_Lex_Unit::prepare(Session *session_arg, select_result *sel_result,
199
 
                              uint64_t additional_options)
 
192
bool st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result,
 
193
                                 ulong additional_options)
200
194
{
201
 
  Select_Lex *lex_select_save= session_arg->getLex()->current_select;
202
 
  Select_Lex *sl, *first_sl= first_select();
 
195
  SELECT_LEX *lex_select_save= thd_arg->lex->current_select;
 
196
  SELECT_LEX *sl, *first_sl= first_select();
203
197
  select_result *tmp_result;
204
198
  bool is_union_select;
205
 
  Table *empty_table= 0;
 
199
  TABLE *empty_table= 0;
206
200
 
207
201
  describe= test(additional_options & SELECT_DESCRIBE);
208
202
 
234
228
  }
235
229
  prepared= 1;
236
230
  saved_error= false;
237
 
 
238
 
  session_arg->getLex()->current_select= sl= first_sl;
 
231
  
 
232
  thd_arg->lex->current_select= sl= first_sl;
239
233
  found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
240
234
  is_union_select= is_union() || fake_select_lex;
241
235
 
257
251
  {
258
252
    bool can_skip_order_by;
259
253
    sl->options|=  SELECT_NO_UNLOCK;
260
 
    Join *join= new Join(session_arg, sl->item_list,
261
 
                         sl->options | session_arg->options | additional_options,
 
254
    JOIN *join= new JOIN(thd_arg, sl->item_list, 
 
255
                         sl->options | thd_arg->options | additional_options,
262
256
                         tmp_result);
263
257
    /*
264
258
      setup_tables_done_option should be set only for very first SELECT,
270
264
    if (!join)
271
265
      goto err;
272
266
 
273
 
    session_arg->getLex()->current_select= sl;
 
267
    thd_arg->lex->current_select= sl;
274
268
 
275
269
    can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit);
276
270
 
277
271
    saved_error= join->prepare(&sl->ref_pointer_array,
278
 
                               (TableList*) sl->table_list.first,
 
272
                               (TABLE_LIST*) sl->table_list.first,
279
273
                               sl->with_wild,
280
274
                               sl->where,
281
275
                               (can_skip_order_by ? 0 :
282
276
                                sl->order_list.elements) +
283
277
                               sl->group_list.elements,
284
278
                               can_skip_order_by ?
285
 
                               (Order*) NULL : (Order *)sl->order_list.first,
286
 
                               (Order*) sl->group_list.first,
 
279
                               (ORDER*) 0 : (ORDER *)sl->order_list.first,
 
280
                               (ORDER*) sl->group_list.first,
287
281
                               sl->having,
 
282
                               (is_union_select ? (ORDER*) 0 :
 
283
                                (ORDER*) thd_arg->lex->proc_list.first),
288
284
                               sl, this);
289
285
    /* There are no * in the statement anymore (for PS) */
290
286
    sl->with_wild= 0;
291
287
 
292
 
    if (saved_error || (saved_error= session_arg->is_fatal_error))
 
288
    if (saved_error || (saved_error= thd_arg->is_fatal_error))
293
289
      goto err;
294
290
    /*
295
291
      Use items list of underlaid select for derived tables to preserve
306
302
        field object without table.
307
303
      */
308
304
      assert(!empty_table);
309
 
      empty_table= (Table*) session->calloc(sizeof(Table));
310
 
      types.clear();
311
 
      List<Item>::iterator it(sl->item_list.begin());
 
305
      empty_table= (TABLE*) thd->calloc(sizeof(TABLE));
 
306
      types.empty();
 
307
      List_iterator_fast<Item> it(sl->item_list);
312
308
      Item *item_tmp;
313
309
      while ((item_tmp= it++))
314
310
      {
315
311
        /* Error's in 'new' will be detected after loop */
316
 
        types.push_back(new Item_type_holder(session_arg, item_tmp));
 
312
        types.push_back(new Item_type_holder(thd_arg, item_tmp));
317
313
      }
318
314
 
319
 
      if (session_arg->is_fatal_error)
 
315
      if (thd_arg->is_fatal_error)
320
316
        goto err; // out of memory
321
317
    }
322
318
    else
327
323
                   ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT),MYF(0));
328
324
        goto err;
329
325
      }
330
 
      List<Item>::iterator it(sl->item_list.begin());
331
 
      List<Item>::iterator tp(types.begin());
 
326
      List_iterator_fast<Item> it(sl->item_list);
 
327
      List_iterator_fast<Item> tp(types);       
332
328
      Item *type, *item_tmp;
333
329
      while ((type= tp++, item_tmp= it++))
334
330
      {
335
 
        if (((Item_type_holder*)type)->join_types(session_arg, item_tmp))
 
331
        if (((Item_type_holder*)type)->join_types(thd_arg, item_tmp))
336
332
          return(true);
337
333
      }
338
334
    }
344
340
      Check that it was possible to aggregate
345
341
      all collations together for UNION.
346
342
    */
347
 
    List<Item>::iterator tp(types.begin());
 
343
    List_iterator_fast<Item> tp(types);
348
344
    Item *type;
349
345
    uint64_t create_options;
350
346
 
357
353
        goto err;
358
354
      }
359
355
    }
360
 
 
361
 
    create_options= (first_sl->options | session_arg->options |
 
356
    
 
357
    create_options= (first_sl->options | thd_arg->options |
362
358
                     TMP_TABLE_ALL_COLUMNS);
363
359
 
364
 
    if (union_result->create_result_table(session, &types, test(union_distinct),
365
 
                                          create_options, ""))
 
360
    if (union_result->create_result_table(thd, &types, test(union_distinct),
 
361
                                          create_options, "", false))
366
362
      goto err;
367
 
    memset(&result_table_list, 0, sizeof(result_table_list));
368
 
    result_table_list.setSchemaName((char*) "");
369
 
    result_table_list.alias= "union";
370
 
    result_table_list.setTableName((char *) "union");
 
363
    bzero((char*) &result_table_list, sizeof(result_table_list));
 
364
    result_table_list.db= (char*) "";
 
365
    result_table_list.table_name= result_table_list.alias= (char*) "union";
371
366
    result_table_list.table= table= union_result->table;
372
367
 
373
 
    session_arg->getLex()->current_select= lex_select_save;
 
368
    thd_arg->lex->current_select= lex_select_save;
374
369
    if (!item_list.elements)
375
370
    {
376
371
      saved_error= table->fill_item_list(&item_list);
379
374
    }
380
375
    else
381
376
    {
 
377
      assert(thd->stmt_arena->is_conventional() == false);
382
378
      /*
383
379
        We're in execution of a prepared statement or stored procedure:
384
380
        reset field items to point at fields from the created temporary table.
385
381
      */
386
 
      assert(1);
 
382
      table->reset_item_list(&item_list);
387
383
    }
388
384
  }
389
385
 
390
 
  session_arg->getLex()->current_select= lex_select_save;
 
386
  thd_arg->lex->current_select= lex_select_save;
391
387
 
392
 
  return(saved_error || session_arg->is_fatal_error);
 
388
  return(saved_error || thd_arg->is_fatal_error);
393
389
 
394
390
err:
395
 
  session_arg->getLex()->current_select= lex_select_save;
 
391
  thd_arg->lex->current_select= lex_select_save;
396
392
  return(true);
397
393
}
398
394
 
399
395
 
400
 
bool Select_Lex_Unit::exec()
 
396
bool st_select_lex_unit::exec()
401
397
{
402
 
  Select_Lex *lex_select_save= session->getLex()->current_select;
403
 
  Select_Lex *select_cursor=first_select();
 
398
  SELECT_LEX *lex_select_save= thd->lex->current_select;
 
399
  SELECT_LEX *select_cursor=first_select();
404
400
  uint64_t add_rows=0;
405
401
  ha_rows examined_rows= 0;
406
402
 
407
 
  if (executed && uncacheable.none() && ! describe)
408
 
    return false;
 
403
  if (executed && !uncacheable && !describe)
 
404
    return(false);
409
405
  executed= 1;
410
 
 
411
 
  if (uncacheable.any() || ! item || ! item->assigned() || describe)
 
406
  
 
407
  if (uncacheable || !item || !item->assigned() || describe)
412
408
  {
413
409
    if (item)
414
410
      item->reset_value_registration();
418
414
      {
419
415
        item->assigned(0); // We will reinit & rexecute unit
420
416
        item->reset();
421
 
        table->cursor->ha_delete_all_rows();
 
417
        table->file->ha_delete_all_rows();
422
418
      }
423
419
      /* re-enabling indexes for next subselect iteration */
424
 
      if (union_distinct && table->cursor->ha_enable_indexes(HA_KEY_SWITCH_ALL))
 
420
      if (union_distinct && table->file->ha_enable_indexes(HA_KEY_SWITCH_ALL))
425
421
      {
426
422
        assert(0);
427
423
      }
428
424
    }
429
 
    for (Select_Lex *sl= select_cursor; sl; sl= sl->next_select())
 
425
    for (SELECT_LEX *sl= select_cursor; sl; sl= sl->next_select())
430
426
    {
431
427
      ha_rows records_at_start= 0;
432
 
      session->getLex()->current_select= sl;
 
428
      thd->lex->current_select= sl;
433
429
 
434
430
      if (optimized)
435
431
        saved_error= sl->join->reinit();
452
448
          we don't calculate found_rows() per union part.
453
449
          Otherwise, SQL_CALC_FOUND_ROWS should be done on all sub parts.
454
450
        */
455
 
        sl->join->select_options=
 
451
        sl->join->select_options= 
456
452
          (select_limit_cnt == HA_POS_ERROR || sl->braces) ?
457
453
          sl->options & ~OPTION_FOUND_ROWS : sl->options | found_rows_for_union;
458
454
 
 
455
        /* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
 
456
        if (sl->join->flatten_subqueries())
 
457
          return(true);
 
458
 
 
459
        /* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
459
460
        saved_error= sl->join->optimize();
460
461
      }
461
462
      if (!saved_error)
462
463
      {
463
 
        records_at_start= table->cursor->stats.records;
 
464
        records_at_start= table->file->stats.records;
464
465
        sl->join->exec();
465
466
        if (sl == union_distinct)
466
467
        {
467
 
          if (table->cursor->ha_disable_indexes(HA_KEY_SWITCH_ALL))
 
468
          if (table->file->ha_disable_indexes(HA_KEY_SWITCH_ALL))
468
469
            return(true);
469
470
          table->no_keyread=1;
470
471
        }
474
475
                                    0);
475
476
        if (!saved_error)
476
477
        {
477
 
          examined_rows+= session->examined_row_count;
 
478
          examined_rows+= thd->examined_row_count;
478
479
          if (union_result->flush())
479
480
          {
480
 
            session->getLex()->current_select= lex_select_save;
 
481
            thd->lex->current_select= lex_select_save;
481
482
            return(1);
482
483
          }
483
484
        }
484
485
      }
485
486
      if (saved_error)
486
487
      {
487
 
        session->getLex()->current_select= lex_select_save;
 
488
        thd->lex->current_select= lex_select_save;
488
489
        return(saved_error);
489
490
      }
490
491
      /* Needed for the following test and for records_at_start in next loop */
491
 
      int error= table->cursor->info(HA_STATUS_VARIABLE);
492
 
      if (error)
 
492
      int error= table->file->info(HA_STATUS_VARIABLE);
 
493
      if(error)
493
494
      {
494
 
        table->print_error(error, MYF(0));
 
495
        table->file->print_error(error, MYF(0));
495
496
        return(1);
496
497
      }
497
 
      if (found_rows_for_union && !sl->braces &&
 
498
      if (found_rows_for_union && !sl->braces && 
498
499
          select_limit_cnt != HA_POS_ERROR)
499
500
      {
500
501
        /*
503
504
          We get this from the difference of between total number of possible
504
505
          rows and actual rows added to the temporary table.
505
506
        */
506
 
        add_rows+= (uint64_t) (session->limit_found_rows - (uint64_t)
507
 
                              ((table->cursor->stats.records -  records_at_start)));
 
507
        add_rows+= (uint64_t) (thd->limit_found_rows - (uint64_t)
 
508
                              ((table->file->stats.records -  records_at_start)));
508
509
      }
509
510
    }
510
511
  }
513
514
  /* Send result to 'result' */
514
515
  saved_error= true;
515
516
  {
516
 
    if (!session->is_fatal_error)                               // Check if EOM
 
517
    if (!thd->is_fatal_error)                           // Check if EOM
517
518
    {
518
519
      set_limit(global_parameters);
519
 
      init_prepare_fake_select_lex(session);
520
 
      Join *join= fake_select_lex->join;
 
520
      init_prepare_fake_select_lex(thd);
 
521
      JOIN *join= fake_select_lex->join;
521
522
      if (!join)
522
523
      {
523
524
        /*
524
525
          allocate JOIN for fake select only once (prevent
525
 
          select_query automatic allocation)
526
 
          TODO: The above is nonsense. select_query() will not allocate the
 
526
          mysql_select automatic allocation)
 
527
          TODO: The above is nonsense. mysql_select() will not allocate the
527
528
          join if one already exists. There must be some other reason why we
528
529
          don't let it allocate the join. Perhaps this is because we need
529
530
          some special parameter values passed to join constructor?
530
531
        */
531
 
        if (!(fake_select_lex->join= new Join(session, item_list,
 
532
        if (!(fake_select_lex->join= new JOIN(thd, item_list,
532
533
                                              fake_select_lex->options, result)))
533
534
        {
534
 
          fake_select_lex->table_list.clear();
 
535
          fake_select_lex->table_list.empty();
535
536
          return(true);
536
537
        }
537
538
        fake_select_lex->join->no_const_tables= true;
538
539
 
539
540
        /*
540
 
          Fake Select_Lex should have item list for correctref_array
 
541
          Fake st_select_lex should have item list for correctref_array
541
542
          allocation.
542
543
        */
543
544
        fake_select_lex->item_list= item_list;
544
 
        saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
 
545
        saved_error= mysql_select(thd, &fake_select_lex->ref_pointer_array,
545
546
                              &result_table_list,
546
547
                              0, item_list, NULL,
547
548
                              global_parameters->order_list.elements,
548
 
                              (Order*)global_parameters->order_list.first,
549
 
                              (Order*) NULL, NULL,
 
549
                              (ORDER*)global_parameters->order_list.first,
 
550
                              (ORDER*) NULL, NULL, (ORDER*) NULL,
550
551
                              fake_select_lex->options | SELECT_NO_UNLOCK,
551
552
                              result, this, fake_select_lex);
552
553
      }
560
561
             - 1st time is a real evaluation to get the subquery value
561
562
             - 2nd time is to produce EXPLAIN output rows.
562
563
            1st execution sets certain members (e.g. select_result) to perform
563
 
            subquery execution rather than EXPLAIN line production. In order
 
564
            subquery execution rather than EXPLAIN line production. In order 
564
565
            to reset them back, we re-do all of the actions (yes it is ugly):
565
566
          */
566
 
                join->reset(session, item_list, fake_select_lex->options, result);
567
 
          saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
 
567
          join->init(thd, item_list, fake_select_lex->options, result);
 
568
          saved_error= mysql_select(thd, &fake_select_lex->ref_pointer_array,
568
569
                                &result_table_list,
569
570
                                0, item_list, NULL,
570
571
                                global_parameters->order_list.elements,
571
 
                                (Order*)global_parameters->order_list.first,
572
 
                                (Order*) NULL, NULL,
 
572
                                (ORDER*)global_parameters->order_list.first,
 
573
                                (ORDER*) NULL, NULL, (ORDER*) NULL,
573
574
                                fake_select_lex->options | SELECT_NO_UNLOCK,
574
575
                                result, this, fake_select_lex);
575
576
        }
581
582
        }
582
583
      }
583
584
 
584
 
      fake_select_lex->table_list.clear();
 
585
      fake_select_lex->table_list.empty();
585
586
      if (!saved_error)
586
587
      {
587
 
        session->limit_found_rows = (uint64_t)table->cursor->stats.records + add_rows;
588
 
        session->examined_row_count+= examined_rows;
 
588
        thd->limit_found_rows = (uint64_t)table->file->stats.records + add_rows;
 
589
        thd->examined_row_count+= examined_rows;
589
590
      }
590
591
      /*
591
592
        Mark for slow query log if any of the union parts didn't use
593
594
      */
594
595
    }
595
596
  }
596
 
  session->getLex()->current_select= lex_select_save;
 
597
  thd->lex->current_select= lex_select_save;
597
598
  return(saved_error);
598
599
}
599
600
 
600
601
 
601
 
bool Select_Lex_Unit::cleanup()
 
602
bool st_select_lex_unit::cleanup()
602
603
{
603
604
  int error= 0;
604
605
 
610
611
 
611
612
  if (union_result)
612
613
  {
613
 
    safe_delete(union_result);
 
614
    delete union_result;
 
615
    union_result=0; // Safety
 
616
    if (table)
 
617
      free_tmp_table(thd, table);
614
618
    table= 0; // Safety
615
619
  }
616
620
 
617
 
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
 
621
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
618
622
    error|= sl->cleanup();
619
623
 
620
624
  if (fake_select_lex)
621
625
  {
622
 
    Join *join;
 
626
    JOIN *join;
623
627
    if ((join= fake_select_lex->join))
624
628
    {
625
629
      join->tables_list= 0;
628
632
    error|= fake_select_lex->cleanup();
629
633
    if (fake_select_lex->order_list.elements)
630
634
    {
631
 
      Order *ord;
632
 
      for (ord= (Order*)fake_select_lex->order_list.first; ord; ord= ord->next)
 
635
      ORDER *ord;
 
636
      for (ord= (ORDER*)fake_select_lex->order_list.first; ord; ord= ord->next)
633
637
        (*ord->item)->cleanup();
634
638
    }
635
639
  }
638
642
}
639
643
 
640
644
 
641
 
void Select_Lex_Unit::reinit_exec_mechanism()
 
645
void st_select_lex_unit::reinit_exec_mechanism()
642
646
{
643
647
  prepared= optimized= executed= 0;
644
648
}
648
652
  change select_result object of unit
649
653
 
650
654
  SYNOPSIS
651
 
    Select_Lex_Unit::change_result()
 
655
    st_select_lex_unit::change_result()
652
656
    result      new select_result object
653
657
    old_result  old select_result object
654
658
 
657
661
    true  - error
658
662
*/
659
663
 
660
 
bool Select_Lex_Unit::change_result(select_result_interceptor *new_result,
 
664
bool st_select_lex_unit::change_result(select_result_interceptor *new_result,
661
665
                                       select_result_interceptor *old_result)
662
666
{
663
667
  bool res= false;
664
 
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
 
668
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
665
669
  {
666
670
    if (sl->join && sl->join->result == old_result)
667
671
      if (sl->join->change_result(new_result))
676
680
  Get column type information for this unit.
677
681
 
678
682
  SYNOPSIS
679
 
    Select_Lex_Unit::get_unit_column_types()
 
683
    st_select_lex_unit::get_unit_column_types()
680
684
 
681
685
  DESCRIPTION
682
686
    For a single-select the column types are taken
683
687
    from the list of selected items. For a union this function
684
 
    assumes that Select_Lex_Unit::prepare has been called
 
688
    assumes that st_select_lex_unit::prepare has been called
685
689
    and returns the type holders that were created for unioned
686
690
    column types of all selects.
687
691
 
688
692
  NOTES
689
693
    The implementation of this function should be in sync with
690
 
    Select_Lex_Unit::prepare()
 
694
    st_select_lex_unit::prepare()
691
695
*/
692
696
 
693
 
List<Item> *Select_Lex_Unit::get_unit_column_types()
 
697
List<Item> *st_select_lex_unit::get_unit_column_types()
694
698
{
695
 
  Select_Lex *sl= first_select();
 
699
  SELECT_LEX *sl= first_select();
696
700
 
697
701
  if (is_union())
698
702
  {
704
708
  return &sl->item_list;
705
709
}
706
710
 
707
 
bool Select_Lex::cleanup()
 
711
bool st_select_lex::cleanup()
708
712
{
709
713
  bool error= false;
710
714
 
711
715
  if (join)
712
716
  {
713
 
    assert((Select_Lex*)join->select_lex == this);
 
717
    assert((st_select_lex*)join->select_lex == this);
714
718
    error= join->destroy();
715
 
    safe_delete(join);
 
719
    delete join;
 
720
    join= 0;
716
721
  }
717
 
  for (Select_Lex_Unit *lex_unit= first_inner_unit(); lex_unit ;
 
722
  for (SELECT_LEX_UNIT *lex_unit= first_inner_unit(); lex_unit ;
718
723
       lex_unit= lex_unit->next_unit())
719
724
  {
720
 
    error= (bool) ((uint32_t) error | (uint32_t) lex_unit->cleanup());
 
725
    error= (bool) ((uint) error | (uint) lex_unit->cleanup());
721
726
  }
722
 
  non_agg_fields.clear();
723
 
  inner_refs_list.clear();
 
727
  non_agg_fields.empty();
 
728
  inner_refs_list.empty();
724
729
  return(error);
725
730
}
726
731
 
727
732
 
728
 
void Select_Lex::cleanup_all_joins(bool full)
 
733
void st_select_lex::cleanup_all_joins(bool full)
729
734
{
730
 
  Select_Lex_Unit *unit;
731
 
  Select_Lex *sl;
 
735
  SELECT_LEX_UNIT *unit;
 
736
  SELECT_LEX *sl;
732
737
 
733
738
  if (join)
734
739
    join->cleanup(full);
737
742
    for (sl= unit->first_select(); sl; sl= sl->next_select())
738
743
      sl->cleanup_all_joins(full);
739
744
}
740
 
 
741
 
} /* namespace drizzled */