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