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