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