~ubuntu-branches/ubuntu/precise/mysql-5.5/precise-201203300109

« back to all changes in this revision

Viewing changes to sql/sql_union.cc

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2011-11-08 11:31:13 UTC
  • Revision ID: package-import@ubuntu.com-20111108113113-3ulw01fvi4vn8m25
Tags: upstream-5.5.17
ImportĀ upstreamĀ versionĀ 5.5.17

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
 
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
 
15
 
 
16
 
 
17
/*
 
18
  UNION  of select's
 
19
  UNION's  were introduced by Monty and Sinisa <sinisa@mysql.com>
 
20
*/
 
21
 
 
22
 
 
23
#include "sql_priv.h"
 
24
#include "unireg.h"
 
25
#include "sql_union.h"
 
26
#include "sql_select.h"
 
27
#include "sql_cursor.h"
 
28
#include "sql_base.h"                           // fill_record
 
29
#include "filesort.h"                           // filesort_free_buffers
 
30
 
 
31
bool mysql_union(THD *thd, LEX *lex, select_result *result,
 
32
                 SELECT_LEX_UNIT *unit, ulong setup_tables_done_option)
 
33
{
 
34
  DBUG_ENTER("mysql_union");
 
35
  bool res;
 
36
  if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK |
 
37
                           setup_tables_done_option)))
 
38
    res= unit->exec();
 
39
  res|= unit->cleanup();
 
40
  DBUG_RETURN(res);
 
41
}
 
42
 
 
43
 
 
44
/***************************************************************************
 
45
** store records in temporary table for UNION
 
46
***************************************************************************/
 
47
 
 
48
int select_union::prepare(List<Item> &list, SELECT_LEX_UNIT *u)
 
49
{
 
50
  unit= u;
 
51
  return 0;
 
52
}
 
53
 
 
54
 
 
55
bool select_union::send_data(List<Item> &values)
 
56
{
 
57
  int error= 0;
 
58
  if (unit->offset_limit_cnt)
 
59
  {                                             // using limit offset,count
 
60
    unit->offset_limit_cnt--;
 
61
    return 0;
 
62
  }
 
63
  fill_record(thd, table->field, values, 1);
 
64
  if (thd->is_error())
 
65
    return 1;
 
66
 
 
67
  if ((error= table->file->ha_write_row(table->record[0])))
 
68
  {
 
69
    /* create_myisam_from_heap will generate error if needed */
 
70
    if (table->file->is_fatal_error(error, HA_CHECK_DUP) &&
 
71
        create_myisam_from_heap(thd, table, &tmp_table_param, error, 1))
 
72
      return 1;
 
73
  }
 
74
  return 0;
 
75
}
 
76
 
 
77
 
 
78
bool select_union::send_eof()
 
79
{
 
80
  return 0;
 
81
}
 
82
 
 
83
 
 
84
bool select_union::flush()
 
85
{
 
86
  int error;
 
87
  if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
 
88
  {
 
89
    table->file->print_error(error, MYF(0));
 
90
    return 1;
 
91
  }
 
92
  return 0;
 
93
}
 
94
 
 
95
/*
 
96
  Create a temporary table to store the result of select_union.
 
97
 
 
98
  SYNOPSIS
 
99
    select_union::create_result_table()
 
100
      thd                thread handle
 
101
      column_types       a list of items used to define columns of the
 
102
                         temporary table
 
103
      is_union_distinct  if set, the temporary table will eliminate
 
104
                         duplicates on insert
 
105
      options            create options
 
106
 
 
107
  DESCRIPTION
 
108
    Create a temporary table that is used to store the result of a UNION,
 
109
    derived table, or a materialized cursor.
 
110
 
 
111
  RETURN VALUE
 
112
    0                    The table has been created successfully.
 
113
    1                    create_tmp_table failed.
 
114
*/
 
115
 
 
116
bool
 
117
select_union::create_result_table(THD *thd_arg, List<Item> *column_types,
 
118
                                  bool is_union_distinct, ulonglong options,
 
119
                                  const char *alias)
 
120
{
 
121
  DBUG_ASSERT(table == 0);
 
122
  tmp_table_param.init();
 
123
  tmp_table_param.field_count= column_types->elements;
 
124
 
 
125
  if (! (table= create_tmp_table(thd_arg, &tmp_table_param, *column_types,
 
126
                                 (ORDER*) 0, is_union_distinct, 1,
 
127
                                 options, HA_POS_ERROR, alias)))
 
128
    return TRUE;
 
129
  table->file->extra(HA_EXTRA_WRITE_CACHE);
 
130
  table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
 
131
  return FALSE;
 
132
}
 
133
 
 
134
 
 
135
/*
 
136
  initialization procedures before fake_select_lex preparation()
 
137
 
 
138
  SYNOPSIS
 
139
    st_select_lex_unit::init_prepare_fake_select_lex()
 
140
    thd         - thread handler
 
141
 
 
142
  RETURN
 
143
    options of SELECT
 
144
*/
 
145
 
 
146
void
 
147
st_select_lex_unit::init_prepare_fake_select_lex(THD *thd_arg) 
 
148
{
 
149
  thd_arg->lex->current_select= fake_select_lex;
 
150
  fake_select_lex->table_list.link_in_list(&result_table_list,
 
151
                                           &result_table_list.next_local);
 
152
  fake_select_lex->context.table_list= 
 
153
    fake_select_lex->context.first_name_resolution_table= 
 
154
    fake_select_lex->get_table_list();
 
155
  if (!fake_select_lex->first_execution)
 
156
  {
 
157
    for (ORDER *order= global_parameters->order_list.first;
 
158
         order;
 
159
         order= order->next)
 
160
      order->item= &order->item_ptr;
 
161
  }
 
162
  for (ORDER *order= global_parameters->order_list.first;
 
163
       order;
 
164
       order=order->next)
 
165
  {
 
166
    (*order->item)->walk(&Item::change_context_processor, 0,
 
167
                         (uchar*) &fake_select_lex->context);
 
168
  }
 
169
}
 
170
 
 
171
 
 
172
bool st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result,
 
173
                                 ulong additional_options)
 
174
{
 
175
  SELECT_LEX *lex_select_save= thd_arg->lex->current_select;
 
176
  SELECT_LEX *sl, *first_sl= first_select();
 
177
  select_result *tmp_result;
 
178
  bool is_union_select;
 
179
  DBUG_ENTER("st_select_lex_unit::prepare");
 
180
 
 
181
  describe= test(additional_options & SELECT_DESCRIBE);
 
182
 
 
183
  /*
 
184
    result object should be reassigned even if preparing already done for
 
185
    max/min subquery (ALL/ANY optimization)
 
186
  */
 
187
  result= sel_result;
 
188
 
 
189
  if (prepared)
 
190
  {
 
191
    if (describe)
 
192
    {
 
193
      /* fast reinit for EXPLAIN */
 
194
      for (sl= first_sl; sl; sl= sl->next_select())
 
195
      {
 
196
        sl->join->result= result;
 
197
        select_limit_cnt= HA_POS_ERROR;
 
198
        offset_limit_cnt= 0;
 
199
        if (!sl->join->procedure &&
 
200
            result->prepare(sl->join->fields_list, this))
 
201
        {
 
202
          DBUG_RETURN(TRUE);
 
203
        }
 
204
        sl->join->select_options|= SELECT_DESCRIBE;
 
205
        sl->join->reinit();
 
206
      }
 
207
    }
 
208
    DBUG_RETURN(FALSE);
 
209
  }
 
210
  prepared= 1;
 
211
  saved_error= FALSE;
 
212
  
 
213
  thd_arg->lex->current_select= sl= first_sl;
 
214
  found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
 
215
  is_union_select= is_union() || fake_select_lex;
 
216
 
 
217
  /* Global option */
 
218
 
 
219
  if (is_union_select)
 
220
  {
 
221
    if (!(tmp_result= union_result= new select_union))
 
222
      goto err;
 
223
    if (describe)
 
224
      tmp_result= sel_result;
 
225
  }
 
226
  else
 
227
    tmp_result= sel_result;
 
228
 
 
229
  sl->context.resolve_in_select_list= TRUE;
 
230
 
 
231
  for (;sl; sl= sl->next_select())
 
232
  {
 
233
    bool can_skip_order_by;
 
234
    sl->options|=  SELECT_NO_UNLOCK;
 
235
    JOIN *join= new JOIN(thd_arg, sl->item_list, 
 
236
                         sl->options | thd_arg->variables.option_bits | additional_options,
 
237
                         tmp_result);
 
238
    /*
 
239
      setup_tables_done_option should be set only for very first SELECT,
 
240
      because it protect from secont setup_tables call for select-like non
 
241
      select commands (DELETE/INSERT/...) and they use only very first
 
242
      SELECT (for union it can be only INSERT ... SELECT).
 
243
    */
 
244
    additional_options&= ~OPTION_SETUP_TABLES_DONE;
 
245
    if (!join)
 
246
      goto err;
 
247
 
 
248
    thd_arg->lex->current_select= sl;
 
249
 
 
250
    can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit);
 
251
 
 
252
    saved_error= join->prepare(&sl->ref_pointer_array,
 
253
                               sl->table_list.first,
 
254
                               sl->with_wild,
 
255
                               sl->where,
 
256
                               (can_skip_order_by ? 0 :
 
257
                                sl->order_list.elements) +
 
258
                               sl->group_list.elements,
 
259
                               can_skip_order_by ?
 
260
                               NULL : sl->order_list.first,
 
261
                               sl->group_list.first,
 
262
                               sl->having,
 
263
                               (is_union_select ? NULL :
 
264
                                thd_arg->lex->proc_list.first),
 
265
                               sl, this);
 
266
    /* There are no * in the statement anymore (for PS) */
 
267
    sl->with_wild= 0;
 
268
    last_procedure= join->procedure;
 
269
 
 
270
    if (saved_error || (saved_error= thd_arg->is_fatal_error))
 
271
      goto err;
 
272
    /*
 
273
      Use items list of underlaid select for derived tables to preserve
 
274
      information about fields lengths and exact types
 
275
    */
 
276
    if (!is_union_select)
 
277
      types= first_sl->item_list;
 
278
    else if (sl == first_sl)
 
279
    {
 
280
      types.empty();
 
281
      List_iterator_fast<Item> it(sl->item_list);
 
282
      Item *item_tmp;
 
283
      while ((item_tmp= it++))
 
284
      {
 
285
        /* Error's in 'new' will be detected after loop */
 
286
        types.push_back(new Item_type_holder(thd_arg, item_tmp));
 
287
      }
 
288
 
 
289
      if (thd_arg->is_fatal_error)
 
290
        goto err; // out of memory
 
291
    }
 
292
    else
 
293
    {
 
294
      if (types.elements != sl->item_list.elements)
 
295
      {
 
296
        my_message(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT,
 
297
                   ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT),MYF(0));
 
298
        goto err;
 
299
      }
 
300
      List_iterator_fast<Item> it(sl->item_list);
 
301
      List_iterator_fast<Item> tp(types);       
 
302
      Item *type, *item_tmp;
 
303
      while ((type= tp++, item_tmp= it++))
 
304
      {
 
305
        if (((Item_type_holder*)type)->join_types(thd_arg, item_tmp))
 
306
          DBUG_RETURN(TRUE);
 
307
      }
 
308
    }
 
309
  }
 
310
 
 
311
  if (is_union_select)
 
312
  {
 
313
    /*
 
314
      Check that it was possible to aggregate
 
315
      all collations together for UNION.
 
316
    */
 
317
    List_iterator_fast<Item> tp(types);
 
318
    Item *type;
 
319
    ulonglong create_options;
 
320
 
 
321
    while ((type= tp++))
 
322
    {
 
323
      if (type->result_type() == STRING_RESULT &&
 
324
          type->collation.derivation == DERIVATION_NONE)
 
325
      {
 
326
        my_error(ER_CANT_AGGREGATE_NCOLLATIONS, MYF(0), "UNION");
 
327
        goto err;
 
328
      }
 
329
    }
 
330
    
 
331
    /*
 
332
      Disable the usage of fulltext searches in the last union branch.
 
333
      This is a temporary 5.x limitation because of the way the fulltext
 
334
      search functions are handled by the optimizer.
 
335
      This is manifestation of the more general problems of "taking away"
 
336
      parts of a SELECT statement post-fix_fields(). This is generally not
 
337
      doable since various flags are collected in various places (e.g. 
 
338
      SELECT_LEX) that carry information about the presence of certain 
 
339
      expressions or constructs in the parts of the query.
 
340
      When part of the query is taken away it's not clear how to "divide" 
 
341
      the meaning of these accumulated flags and what to carry over to the
 
342
      recipient query (SELECT_LEX).
 
343
    */
 
344
    if (global_parameters->ftfunc_list->elements && 
 
345
        global_parameters->order_list.elements &&
 
346
        global_parameters != fake_select_lex)
 
347
    {
 
348
      ORDER *ord;
 
349
      Item_func::Functype ft=  Item_func::FT_FUNC;
 
350
      for (ord= global_parameters->order_list.first; ord; ord= ord->next)
 
351
        if ((*ord->item)->walk (&Item::find_function_processor, FALSE, 
 
352
                                (uchar *) &ft))
 
353
        {
 
354
          my_error (ER_CANT_USE_OPTION_HERE, MYF(0), "MATCH()");
 
355
          goto err;
 
356
        }
 
357
    }
 
358
 
 
359
 
 
360
    create_options= (first_sl->options | thd_arg->variables.option_bits |
 
361
                     TMP_TABLE_ALL_COLUMNS);
 
362
    /*
 
363
      Force the temporary table to be a MyISAM table if we're going to use
 
364
      fullext functions (MATCH ... AGAINST .. IN BOOLEAN MODE) when reading
 
365
      from it (this should be removed in 5.2 when fulltext search is moved 
 
366
      out of MyISAM).
 
367
    */
 
368
    if (global_parameters->ftfunc_list->elements)
 
369
      create_options= create_options | TMP_TABLE_FORCE_MYISAM;
 
370
 
 
371
    if (union_result->create_result_table(thd, &types, test(union_distinct),
 
372
                                          create_options, ""))
 
373
      goto err;
 
374
    bzero((char*) &result_table_list, sizeof(result_table_list));
 
375
    result_table_list.db= (char*) "";
 
376
    result_table_list.table_name= result_table_list.alias= (char*) "union";
 
377
    result_table_list.table= table= union_result->table;
 
378
 
 
379
    thd_arg->lex->current_select= lex_select_save;
 
380
    if (!item_list.elements)
 
381
    {
 
382
      Query_arena *arena, backup_arena;
 
383
 
 
384
      arena= thd->activate_stmt_arena_if_needed(&backup_arena);
 
385
      
 
386
      saved_error= table->fill_item_list(&item_list);
 
387
 
 
388
      if (arena)
 
389
        thd->restore_active_arena(arena, &backup_arena);
 
390
 
 
391
      if (saved_error)
 
392
        goto err;
 
393
 
 
394
      if (thd->stmt_arena->is_stmt_prepare())
 
395
      {
 
396
        /* Validate the global parameters of this union */
 
397
 
 
398
        init_prepare_fake_select_lex(thd);
 
399
        /* Should be done only once (the only item_list per statement) */
 
400
        DBUG_ASSERT(fake_select_lex->join == 0);
 
401
        if (!(fake_select_lex->join= new JOIN(thd, item_list, thd->variables.option_bits,
 
402
                                              result)))
 
403
        {
 
404
          fake_select_lex->table_list.empty();
 
405
          DBUG_RETURN(TRUE);
 
406
        }
 
407
 
 
408
        /*
 
409
          Fake st_select_lex should have item list for correct ref_array
 
410
          allocation.
 
411
        */
 
412
        fake_select_lex->item_list= item_list;
 
413
 
 
414
        thd_arg->lex->current_select= fake_select_lex;
 
415
 
 
416
        /*
 
417
          We need to add up n_sum_items in order to make the correct
 
418
          allocation in setup_ref_array().
 
419
        */
 
420
        fake_select_lex->n_child_sum_items+= global_parameters->n_sum_items;
 
421
 
 
422
        saved_error= fake_select_lex->join->
 
423
          prepare(&fake_select_lex->ref_pointer_array,
 
424
                  fake_select_lex->table_list.first,
 
425
                  0, 0,
 
426
                  global_parameters->order_list.elements, // og_num
 
427
                  global_parameters->order_list.first,    // order
 
428
                  NULL, NULL, NULL,
 
429
                  fake_select_lex, this);
 
430
        fake_select_lex->table_list.empty();
 
431
      }
 
432
    }
 
433
    else
 
434
    {
 
435
      /*
 
436
        We're in execution of a prepared statement or stored procedure:
 
437
        reset field items to point at fields from the created temporary table.
 
438
      */
 
439
      table->reset_item_list(&item_list);
 
440
    }
 
441
  }
 
442
 
 
443
  thd_arg->lex->current_select= lex_select_save;
 
444
 
 
445
  DBUG_RETURN(saved_error || thd_arg->is_fatal_error);
 
446
 
 
447
err:
 
448
  thd_arg->lex->current_select= lex_select_save;
 
449
  (void) cleanup();
 
450
  DBUG_RETURN(TRUE);
 
451
}
 
452
 
 
453
 
 
454
bool st_select_lex_unit::exec()
 
455
{
 
456
  SELECT_LEX *lex_select_save= thd->lex->current_select;
 
457
  SELECT_LEX *select_cursor=first_select();
 
458
  ulonglong add_rows=0;
 
459
  ha_rows examined_rows= 0;
 
460
  DBUG_ENTER("st_select_lex_unit::exec");
 
461
 
 
462
  if (executed && !uncacheable && !describe)
 
463
    DBUG_RETURN(FALSE);
 
464
  executed= 1;
 
465
  
 
466
  if (uncacheable || !item || !item->assigned() || describe)
 
467
  {
 
468
    if (item)
 
469
      item->reset_value_registration();
 
470
    if (optimized && item)
 
471
    {
 
472
      if (item->assigned())
 
473
      {
 
474
        item->assigned(0); // We will reinit & rexecute unit
 
475
        item->reset();
 
476
        table->file->ha_delete_all_rows();
 
477
      }
 
478
      /* re-enabling indexes for next subselect iteration */
 
479
      if (union_distinct && table->file->ha_enable_indexes(HA_KEY_SWITCH_ALL))
 
480
      {
 
481
        DBUG_ASSERT(0);
 
482
      }
 
483
    }
 
484
    for (SELECT_LEX *sl= select_cursor; sl; sl= sl->next_select())
 
485
    {
 
486
      ha_rows records_at_start= 0;
 
487
      thd->lex->current_select= sl;
 
488
 
 
489
      if (optimized)
 
490
        saved_error= sl->join->reinit();
 
491
      else
 
492
      {
 
493
        set_limit(sl);
 
494
        if (sl == global_parameters || describe)
 
495
        {
 
496
          offset_limit_cnt= 0;
 
497
          /*
 
498
            We can't use LIMIT at this stage if we are using ORDER BY for the
 
499
            whole query
 
500
          */
 
501
          if (sl->order_list.first || describe)
 
502
            select_limit_cnt= HA_POS_ERROR;
 
503
        }
 
504
 
 
505
        /*
 
506
          When using braces, SQL_CALC_FOUND_ROWS affects the whole query:
 
507
          we don't calculate found_rows() per union part.
 
508
          Otherwise, SQL_CALC_FOUND_ROWS should be done on all sub parts.
 
509
        */
 
510
        sl->join->select_options= 
 
511
          (select_limit_cnt == HA_POS_ERROR || sl->braces) ?
 
512
          sl->options & ~OPTION_FOUND_ROWS : sl->options | found_rows_for_union;
 
513
        saved_error= sl->join->optimize();
 
514
      }
 
515
      if (!saved_error)
 
516
      {
 
517
        records_at_start= table->file->stats.records;
 
518
        sl->join->exec();
 
519
        if (sl == union_distinct)
 
520
        {
 
521
          if (table->file->ha_disable_indexes(HA_KEY_SWITCH_ALL))
 
522
            DBUG_RETURN(TRUE);
 
523
          table->no_keyread=1;
 
524
        }
 
525
        saved_error= sl->join->error;
 
526
        offset_limit_cnt= (ha_rows)(sl->offset_limit ?
 
527
                                    sl->offset_limit->val_uint() :
 
528
                                    0);
 
529
        if (!saved_error)
 
530
        {
 
531
          examined_rows+= thd->examined_row_count;
 
532
          if (union_result->flush())
 
533
          {
 
534
            thd->lex->current_select= lex_select_save;
 
535
            DBUG_RETURN(1);
 
536
          }
 
537
        }
 
538
      }
 
539
      if (saved_error)
 
540
      {
 
541
        thd->lex->current_select= lex_select_save;
 
542
        DBUG_RETURN(saved_error);
 
543
      }
 
544
      /* Needed for the following test and for records_at_start in next loop */
 
545
      int error= table->file->info(HA_STATUS_VARIABLE);
 
546
      if(error)
 
547
      {
 
548
        table->file->print_error(error, MYF(0));
 
549
        DBUG_RETURN(1);
 
550
      }
 
551
      if (found_rows_for_union && !sl->braces && 
 
552
          select_limit_cnt != HA_POS_ERROR)
 
553
      {
 
554
        /*
 
555
          This is a union without braces. Remember the number of rows that
 
556
          could also have been part of the result set.
 
557
          We get this from the difference of between total number of possible
 
558
          rows and actual rows added to the temporary table.
 
559
        */
 
560
        add_rows+= (ulonglong) (thd->limit_found_rows - (ulonglong)
 
561
                              ((table->file->stats.records -  records_at_start)));
 
562
      }
 
563
    }
 
564
  }
 
565
  optimized= 1;
 
566
 
 
567
  /* Send result to 'result' */
 
568
  saved_error= TRUE;
 
569
  {
 
570
    List<Item_func_match> empty_list;
 
571
    empty_list.empty();
 
572
 
 
573
    if (!thd->is_fatal_error)                           // Check if EOM
 
574
    {
 
575
      set_limit(global_parameters);
 
576
      init_prepare_fake_select_lex(thd);
 
577
      JOIN *join= fake_select_lex->join;
 
578
      if (!join)
 
579
      {
 
580
        /*
 
581
          allocate JOIN for fake select only once (prevent
 
582
          mysql_select automatic allocation)
 
583
          TODO: The above is nonsense. mysql_select() will not allocate the
 
584
          join if one already exists. There must be some other reason why we
 
585
          don't let it allocate the join. Perhaps this is because we need
 
586
          some special parameter values passed to join constructor?
 
587
        */
 
588
        if (!(fake_select_lex->join= new JOIN(thd, item_list,
 
589
                                              fake_select_lex->options, result)))
 
590
        {
 
591
          fake_select_lex->table_list.empty();
 
592
          DBUG_RETURN(TRUE);
 
593
        }
 
594
        fake_select_lex->join->no_const_tables= TRUE;
 
595
 
 
596
        /*
 
597
          Fake st_select_lex should have item list for correct ref_array
 
598
          allocation.
 
599
        */
 
600
        fake_select_lex->item_list= item_list;
 
601
 
 
602
        /*
 
603
          We need to add up n_sum_items in order to make the correct
 
604
          allocation in setup_ref_array().
 
605
          Don't add more sum_items if we have already done JOIN::prepare
 
606
          for this (with a different join object)
 
607
        */
 
608
        if (!fake_select_lex->ref_pointer_array)
 
609
          fake_select_lex->n_child_sum_items+= global_parameters->n_sum_items;
 
610
 
 
611
        saved_error= mysql_select(thd, &fake_select_lex->ref_pointer_array,
 
612
                              &result_table_list,
 
613
                              0, item_list, NULL,
 
614
                              global_parameters->order_list.elements,
 
615
                              global_parameters->order_list.first,
 
616
                              NULL, NULL, NULL,
 
617
                              fake_select_lex->options | SELECT_NO_UNLOCK,
 
618
                              result, this, fake_select_lex);
 
619
      }
 
620
      else
 
621
      {
 
622
        if (describe)
 
623
        {
 
624
          /*
 
625
            In EXPLAIN command, constant subqueries that do not use any
 
626
            tables are executed two times:
 
627
             - 1st time is a real evaluation to get the subquery value
 
628
             - 2nd time is to produce EXPLAIN output rows.
 
629
            1st execution sets certain members (e.g. select_result) to perform
 
630
            subquery execution rather than EXPLAIN line production. In order 
 
631
            to reset them back, we re-do all of the actions (yes it is ugly):
 
632
          */
 
633
          join->init(thd, item_list, fake_select_lex->options, result);
 
634
          saved_error= mysql_select(thd, &fake_select_lex->ref_pointer_array,
 
635
                                &result_table_list,
 
636
                                0, item_list, NULL,
 
637
                                global_parameters->order_list.elements,
 
638
                                global_parameters->order_list.first,
 
639
                                NULL, NULL, NULL,
 
640
                                fake_select_lex->options | SELECT_NO_UNLOCK,
 
641
                                result, this, fake_select_lex);
 
642
        }
 
643
        else
 
644
        {
 
645
          join->examined_rows= 0;
 
646
          saved_error= join->reinit();
 
647
          join->exec();
 
648
        }
 
649
      }
 
650
 
 
651
      fake_select_lex->table_list.empty();
 
652
      if (!saved_error)
 
653
      {
 
654
        thd->limit_found_rows = (ulonglong)table->file->stats.records + add_rows;
 
655
        thd->examined_row_count+= examined_rows;
 
656
      }
 
657
      /*
 
658
        Mark for slow query log if any of the union parts didn't use
 
659
        indexes efficiently
 
660
      */
 
661
    }
 
662
  }
 
663
  thd->lex->current_select= lex_select_save;
 
664
  DBUG_RETURN(saved_error);
 
665
}
 
666
 
 
667
 
 
668
bool st_select_lex_unit::cleanup()
 
669
{
 
670
  int error= 0;
 
671
  DBUG_ENTER("st_select_lex_unit::cleanup");
 
672
 
 
673
  if (cleaned)
 
674
  {
 
675
    DBUG_RETURN(FALSE);
 
676
  }
 
677
  cleaned= 1;
 
678
 
 
679
  if (union_result)
 
680
  {
 
681
    delete union_result;
 
682
    union_result=0; // Safety
 
683
    if (table)
 
684
      free_tmp_table(thd, table);
 
685
    table= 0; // Safety
 
686
  }
 
687
 
 
688
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
689
    error|= sl->cleanup();
 
690
 
 
691
  if (fake_select_lex)
 
692
  {
 
693
    JOIN *join;
 
694
    if ((join= fake_select_lex->join))
 
695
    {
 
696
      join->tables_list= 0;
 
697
      join->tables= 0;
 
698
    }
 
699
    error|= fake_select_lex->cleanup();
 
700
    /*
 
701
      There are two cases when we should clean order items:
 
702
      1. UNION with SELECTs which all enclosed into braces
 
703
        in this case global_parameters == fake_select_lex
 
704
      2. UNION where last SELECT is not enclosed into braces
 
705
        in this case global_parameters == 'last select'
 
706
      So we should use global_parameters->order_list for
 
707
      proper order list clean up.
 
708
      Note: global_parameters and fake_select_lex are always
 
709
            initialized for UNION
 
710
    */
 
711
    DBUG_ASSERT(global_parameters);
 
712
    if (global_parameters->order_list.elements)
 
713
    {
 
714
      ORDER *ord;
 
715
      for (ord= global_parameters->order_list.first; ord; ord= ord->next)
 
716
        (*ord->item)->walk (&Item::cleanup_processor, 0, 0);
 
717
    }
 
718
  }
 
719
 
 
720
  DBUG_RETURN(error);
 
721
}
 
722
 
 
723
 
 
724
void st_select_lex_unit::reinit_exec_mechanism()
 
725
{
 
726
  prepared= optimized= executed= 0;
 
727
#ifndef DBUG_OFF
 
728
  if (is_union())
 
729
  {
 
730
    List_iterator_fast<Item> it(item_list);
 
731
    Item *field;
 
732
    while ((field= it++))
 
733
    {
 
734
      /*
 
735
        we can't cleanup here, because it broke link to temporary table field,
 
736
        but have to drop fixed flag to allow next fix_field of this field
 
737
        during re-executing
 
738
      */
 
739
      field->fixed= 0;
 
740
    }
 
741
  }
 
742
#endif
 
743
}
 
744
 
 
745
 
 
746
/*
 
747
  change select_result object of unit
 
748
 
 
749
  SYNOPSIS
 
750
    st_select_lex_unit::change_result()
 
751
    result      new select_result object
 
752
    old_result  old select_result object
 
753
 
 
754
  RETURN
 
755
    FALSE - OK
 
756
    TRUE  - error
 
757
*/
 
758
 
 
759
bool st_select_lex_unit::change_result(select_subselect *new_result,
 
760
                                       select_subselect *old_result)
 
761
{
 
762
  bool res= FALSE;
 
763
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
764
  {
 
765
    if (sl->join && sl->join->result == old_result)
 
766
      if (sl->join->change_result(new_result))
 
767
        return TRUE;
 
768
  }
 
769
  if (fake_select_lex && fake_select_lex->join)
 
770
    res= fake_select_lex->join->change_result(new_result);
 
771
  return (res);
 
772
}
 
773
 
 
774
/*
 
775
  Get column type information for this unit.
 
776
 
 
777
  SYNOPSIS
 
778
    st_select_lex_unit::get_unit_column_types()
 
779
 
 
780
  DESCRIPTION
 
781
    For a single-select the column types are taken
 
782
    from the list of selected items. For a union this function
 
783
    assumes that st_select_lex_unit::prepare has been called
 
784
    and returns the type holders that were created for unioned
 
785
    column types of all selects.
 
786
 
 
787
  NOTES
 
788
    The implementation of this function should be in sync with
 
789
    st_select_lex_unit::prepare()
 
790
*/
 
791
 
 
792
List<Item> *st_select_lex_unit::get_unit_column_types()
 
793
{
 
794
  SELECT_LEX *sl= first_select();
 
795
  bool is_procedure= test(sl->join->procedure);
 
796
 
 
797
  if (is_procedure)
 
798
  {
 
799
    /* Types for "SELECT * FROM t1 procedure analyse()"
 
800
       are generated during execute */
 
801
    return &sl->join->procedure_fields_list;
 
802
  }
 
803
 
 
804
 
 
805
  if (is_union())
 
806
  {
 
807
    DBUG_ASSERT(prepared);
 
808
    /* Types are generated during prepare */
 
809
    return &types;
 
810
  }
 
811
 
 
812
  return &sl->item_list;
 
813
}
 
814
 
 
815
bool st_select_lex::cleanup()
 
816
{
 
817
  bool error= FALSE;
 
818
  DBUG_ENTER("st_select_lex::cleanup()");
 
819
 
 
820
  if (join)
 
821
  {
 
822
    DBUG_ASSERT((st_select_lex*)join->select_lex == this);
 
823
    error= join->destroy();
 
824
    delete join;
 
825
    join= 0;
 
826
  }
 
827
  for (SELECT_LEX_UNIT *lex_unit= first_inner_unit(); lex_unit ;
 
828
       lex_unit= lex_unit->next_unit())
 
829
  {
 
830
    error= (bool) ((uint) error | (uint) lex_unit->cleanup());
 
831
  }
 
832
  non_agg_fields.empty();
 
833
  inner_refs_list.empty();
 
834
  DBUG_RETURN(error);
 
835
}
 
836
 
 
837
 
 
838
void st_select_lex::cleanup_all_joins(bool full)
 
839
{
 
840
  SELECT_LEX_UNIT *unit;
 
841
  SELECT_LEX *sl;
 
842
 
 
843
  if (join)
 
844
    join->cleanup(full);
 
845
 
 
846
  for (unit= first_inner_unit(); unit; unit= unit->next_unit())
 
847
    for (sl= unit->first_select(); sl; sl= sl->next_select())
 
848
      sl->cleanup_all_joins(full);
 
849
}