~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
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
27
namespace drizzled
28
{
29
1008.3.22 by Stewart Smith
s/mysql_union/drizzle_union/
30
bool drizzle_union(Session *session, LEX *, select_result *result,
31
		   Select_Lex_Unit *unit, uint64_t setup_tables_done_option)
1 by brian
clean slate
32
{
33
  bool res;
520.1.22 by Brian Aker
Second pass of thd cleanup
34
  if (!(res= unit->prepare(session, result, SELECT_NO_UNLOCK |
1 by brian
clean slate
35
                           setup_tables_done_option)))
36
    res= unit->exec();
37
  if (res)
38
    res|= unit->cleanup();
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
39
  return(res);
1 by brian
clean slate
40
}
41
42
43
/***************************************************************************
44
** store records in temporary table for UNION
45
***************************************************************************/
46
848 by Brian Aker
typdef class removal (just... use the name of the class).
47
int select_union::prepare(List<Item> &, Select_Lex_Unit *u)
1 by brian
clean slate
48
{
49
  unit= u;
50
  return 0;
51
}
52
53
54
bool select_union::send_data(List<Item> &values)
55
{
56
  int error= 0;
57
  if (unit->offset_limit_cnt)
58
  {						// using limit offset,count
59
    unit->offset_limit_cnt--;
60
    return 0;
61
  }
1235.1.11 by Brian Aker
Small cleanups, did in MERGE table only engine flag.
62
  fill_record(session, table->field, values, true);
520.1.22 by Brian Aker
Second pass of thd cleanup
63
  if (session->is_error())
1 by brian
clean slate
64
    return 1;
65
1491.1.2 by Jay Pipes
Cursor::write_row() -> Cursor::doInsertRecord(). Cursor::ha_write_row() -> Cursor::insertRecord()
66
  if ((error= table->cursor->insertRecord(table->record[0])))
1 by brian
clean slate
67
  {
68
    /* create_myisam_from_heap will generate error if needed */
1487.1.1 by Brian Aker
There is room for improvement around this. We should be using rows as well
69
    if (table->cursor->is_fatal_error(error, HA_CHECK_DUP))
70
    {
71
      my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
72
      return true;
73
    }
1 by brian
clean slate
74
  }
75
  return 0;
76
}
77
78
79
bool select_union::send_eof()
80
{
81
  return 0;
82
}
83
84
85
bool select_union::flush()
86
{
87
  int error;
1208.3.2 by brian
Update for Cursor renaming.
88
  if ((error=table->cursor->extra(HA_EXTRA_NO_CACHE)))
1 by brian
clean slate
89
  {
1216.1.1 by Brian Aker
Move print_error up to Engine.
90
    table->print_error(error, MYF(0));
1 by brian
clean slate
91
    return 1;
92
  }
93
  return 0;
94
}
95
96
/*
97
  Create a temporary table to store the result of select_union.
98
99
  SYNOPSIS
100
    select_union::create_result_table()
520.1.22 by Brian Aker
Second pass of thd cleanup
101
      session                thread handle
1 by brian
clean slate
102
      column_types       a list of items used to define columns of the
103
                         temporary table
104
      is_union_distinct  if set, the temporary table will eliminate
105
                         duplicates on insert
106
      options            create options
107
      table_alias        name of the temporary table
108
109
  DESCRIPTION
110
    Create a temporary table that is used to store the result of a UNION,
111
    derived table, or a materialized cursor.
112
113
  RETURN VALUE
114
    0                    The table has been created successfully.
115
    1                    create_tmp_table failed.
116
*/
117
118
bool
520.1.22 by Brian Aker
Second pass of thd cleanup
119
select_union::create_result_table(Session *session_arg, List<Item> *column_types,
151 by Brian Aker
Ulonglong to uint64_t
120
                                  bool is_union_distinct, uint64_t options,
1273.2.2 by Stewart Smith
it turns out that bit_fields_as_long in tmp_table_param is in fact unused. remove it.
121
                                  const char *table_alias)
1 by brian
clean slate
122
{
1273.2.1 by Stewart Smith
simply semantic that 0 is not a pointer, NULL is the null pointer, not 0. in sql_union.cc
123
  assert(table == NULL);
1101.1.16 by Monty Taylor
Reverted 1103
124
  tmp_table_param.init();
1 by brian
clean slate
125
  tmp_table_param.field_count= column_types->elements;
126
520.1.22 by Brian Aker
Second pass of thd cleanup
127
  if (! (table= create_tmp_table(session_arg, &tmp_table_param, *column_types,
1273.2.1 by Stewart Smith
simply semantic that 0 is not a pointer, NULL is the null pointer, not 0. in sql_union.cc
128
                                 (order_st*) NULL, is_union_distinct, 1,
1 by brian
clean slate
129
                                 options, HA_POS_ERROR, (char*) table_alias)))
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
130
    return true;
1208.3.2 by brian
Update for Cursor renaming.
131
  table->cursor->extra(HA_EXTRA_WRITE_CACHE);
132
  table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
133
  return false;
1 by brian
clean slate
134
}
135
136
137
/**
138
  Reset and empty the temporary table that stores the materialized query result.
139
140
  @note The cleanup performed here is exactly the same as for the two temp
141
  tables of JOIN - exec_tmp_table_[1 | 2].
142
*/
143
144
void select_union::cleanup()
145
{
1208.3.2 by brian
Update for Cursor renaming.
146
  table->cursor->extra(HA_EXTRA_RESET_STATE);
147
  table->cursor->ha_delete_all_rows();
1109.1.4 by Brian Aker
More Table refactor
148
  table->free_io_cache();
149
  table->filesort_free_buffers();
1 by brian
clean slate
150
}
151
152
153
/*
154
  initialization procedures before fake_select_lex preparation()
155
156
  SYNOPSIS
848 by Brian Aker
typdef class removal (just... use the name of the class).
157
    Select_Lex_Unit::init_prepare_fake_select_lex()
520.1.22 by Brian Aker
Second pass of thd cleanup
158
    session		- thread handler
1 by brian
clean slate
159
160
  RETURN
161
    options of SELECT
162
*/
163
164
void
848 by Brian Aker
typdef class removal (just... use the name of the class).
165
Select_Lex_Unit::init_prepare_fake_select_lex(Session *session_arg)
1 by brian
clean slate
166
{
520.1.22 by Brian Aker
Second pass of thd cleanup
167
  session_arg->lex->current_select= fake_select_lex;
481 by Brian Aker
Remove all of uchar.
168
  fake_select_lex->table_list.link_in_list((unsigned char *)&result_table_list,
169
					   (unsigned char **)
1 by brian
clean slate
170
					   &result_table_list.next_local);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
171
  fake_select_lex->context.table_list=
172
    fake_select_lex->context.first_name_resolution_table=
1 by brian
clean slate
173
    fake_select_lex->get_table_list();
404 by Brian Aker
Removed dead variable
174
175
  for (order_st *order= (order_st *) global_parameters->order_list.first;
176
       order;
177
       order= order->next)
178
    order->item= &order->item_ptr;
179
327.2.3 by Brian Aker
Refactoring of class Table
180
  for (order_st *order= (order_st *)global_parameters->order_list.first;
1 by brian
clean slate
181
       order;
182
       order=order->next)
183
  {
184
    (*order->item)->walk(&Item::change_context_processor, 0,
481 by Brian Aker
Remove all of uchar.
185
                         (unsigned char*) &fake_select_lex->context);
1 by brian
clean slate
186
  }
187
}
188
189
848 by Brian Aker
typdef class removal (just... use the name of the class).
190
bool Select_Lex_Unit::prepare(Session *session_arg, select_result *sel_result,
892.2.5 by Monty Taylor
Fixed an oops.
191
                              uint64_t additional_options)
1 by brian
clean slate
192
{
846 by Brian Aker
Removing on typedeffed class.
193
  Select_Lex *lex_select_save= session_arg->lex->current_select;
194
  Select_Lex *sl, *first_sl= first_select();
1 by brian
clean slate
195
  select_result *tmp_result;
196
  bool is_union_select;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
197
  Table *empty_table= 0;
1 by brian
clean slate
198
199
  describe= test(additional_options & SELECT_DESCRIBE);
200
201
  /*
202
    result object should be reassigned even if preparing already done for
203
    max/min subquery (ALL/ANY optimization)
204
  */
205
  result= sel_result;
206
207
  if (prepared)
208
  {
209
    if (describe)
210
    {
211
      /* fast reinit for EXPLAIN */
212
      for (sl= first_sl; sl; sl= sl->next_select())
213
      {
214
	sl->join->result= result;
215
	select_limit_cnt= HA_POS_ERROR;
216
	offset_limit_cnt= 0;
217
	if (result->prepare(sl->join->fields_list, this))
218
	{
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
219
	  return(true);
1 by brian
clean slate
220
	}
221
	sl->join->select_options|= SELECT_DESCRIBE;
222
	sl->join->reinit();
223
      }
224
    }
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
225
    return(false);
1 by brian
clean slate
226
  }
227
  prepared= 1;
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
228
  saved_error= false;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
229
520.1.22 by Brian Aker
Second pass of thd cleanup
230
  session_arg->lex->current_select= sl= first_sl;
1 by brian
clean slate
231
  found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
232
  is_union_select= is_union() || fake_select_lex;
233
234
  /* Global option */
235
236
  if (is_union_select)
237
  {
238
    if (!(tmp_result= union_result= new select_union))
239
      goto err;
240
    if (describe)
241
      tmp_result= sel_result;
242
  }
243
  else
244
    tmp_result= sel_result;
245
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
246
  sl->context.resolve_in_select_list= true;
1 by brian
clean slate
247
248
  for (;sl; sl= sl->next_select())
249
  {
250
    bool can_skip_order_by;
251
    sl->options|=  SELECT_NO_UNLOCK;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
252
    JOIN *join= new JOIN(session_arg, sl->item_list,
520.1.22 by Brian Aker
Second pass of thd cleanup
253
			 sl->options | session_arg->options | additional_options,
1 by brian
clean slate
254
			 tmp_result);
255
    /*
256
      setup_tables_done_option should be set only for very first SELECT,
257
      because it protect from secont setup_tables call for select-like non
258
      select commands (DELETE/INSERT/...) and they use only very first
259
      SELECT (for union it can be only INSERT ... SELECT).
260
    */
261
    additional_options&= ~OPTION_SETUP_TABLES_DONE;
262
    if (!join)
263
      goto err;
264
520.1.22 by Brian Aker
Second pass of thd cleanup
265
    session_arg->lex->current_select= sl;
1 by brian
clean slate
266
267
    can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit);
268
269
    saved_error= join->prepare(&sl->ref_pointer_array,
327.2.4 by Brian Aker
Refactoring table.h
270
                               (TableList*) sl->table_list.first,
1 by brian
clean slate
271
                               sl->with_wild,
272
                               sl->where,
273
                               (can_skip_order_by ? 0 :
274
                                sl->order_list.elements) +
275
                               sl->group_list.elements,
276
                               can_skip_order_by ?
1273.2.1 by Stewart Smith
simply semantic that 0 is not a pointer, NULL is the null pointer, not 0. in sql_union.cc
277
                               (order_st*) NULL : (order_st *)sl->order_list.first,
327.2.3 by Brian Aker
Refactoring of class Table
278
                               (order_st*) sl->group_list.first,
1 by brian
clean slate
279
                               sl->having,
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),
1273.2.2 by Stewart Smith
it turns out that bit_fields_as_long in tmp_table_param is in fact unused. remove it.
357
                                          create_options, ""))
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*) "";
1039.1.4 by Brian Aker
Modified alias to being const.
361
    result_table_list.alias= "union";
362
    result_table_list.table_name= (char *) "union";
1 by brian
clean slate
363
    result_table_list.table= table= union_result->table;
364
520.1.22 by Brian Aker
Second pass of thd cleanup
365
    session_arg->lex->current_select= lex_select_save;
1 by brian
clean slate
366
    if (!item_list.elements)
367
    {
368
      saved_error= table->fill_item_list(&item_list);
369
      if (saved_error)
370
        goto err;
371
    }
372
    else
373
    {
374
      /*
375
        We're in execution of a prepared statement or stored procedure:
376
        reset field items to point at fields from the created temporary table.
377
      */
401 by Brian Aker
Partial Query_arena removal
378
      assert(1);
1 by brian
clean slate
379
    }
380
  }
381
520.1.22 by Brian Aker
Second pass of thd cleanup
382
  session_arg->lex->current_select= lex_select_save;
1 by brian
clean slate
383
520.1.22 by Brian Aker
Second pass of thd cleanup
384
  return(saved_error || session_arg->is_fatal_error);
1 by brian
clean slate
385
386
err:
520.1.22 by Brian Aker
Second pass of thd cleanup
387
  session_arg->lex->current_select= lex_select_save;
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
388
  return(true);
1 by brian
clean slate
389
}
390
391
848 by Brian Aker
typdef class removal (just... use the name of the class).
392
bool Select_Lex_Unit::exec()
1 by brian
clean slate
393
{
846 by Brian Aker
Removing on typedeffed class.
394
  Select_Lex *lex_select_save= session->lex->current_select;
395
  Select_Lex *select_cursor=first_select();
151 by Brian Aker
Ulonglong to uint64_t
396
  uint64_t add_rows=0;
1 by brian
clean slate
397
  ha_rows examined_rows= 0;
398
399
  if (executed && !uncacheable && !describe)
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
400
    return(false);
1 by brian
clean slate
401
  executed= 1;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
402
1 by brian
clean slate
403
  if (uncacheable || !item || !item->assigned() || describe)
404
  {
405
    if (item)
406
      item->reset_value_registration();
407
    if (optimized && item)
408
    {
409
      if (item->assigned())
410
      {
411
        item->assigned(0); // We will reinit & rexecute unit
412
        item->reset();
1208.3.2 by brian
Update for Cursor renaming.
413
        table->cursor->ha_delete_all_rows();
1 by brian
clean slate
414
      }
415
      /* re-enabling indexes for next subselect iteration */
1208.3.2 by brian
Update for Cursor renaming.
416
      if (union_distinct && table->cursor->ha_enable_indexes(HA_KEY_SWITCH_ALL))
1 by brian
clean slate
417
      {
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
418
        assert(0);
1 by brian
clean slate
419
      }
420
    }
846 by Brian Aker
Removing on typedeffed class.
421
    for (Select_Lex *sl= select_cursor; sl; sl= sl->next_select())
1 by brian
clean slate
422
    {
423
      ha_rows records_at_start= 0;
520.1.22 by Brian Aker
Second pass of thd cleanup
424
      session->lex->current_select= sl;
1 by brian
clean slate
425
426
      if (optimized)
427
	saved_error= sl->join->reinit();
428
      else
429
      {
430
        set_limit(sl);
431
	if (sl == global_parameters || describe)
432
	{
433
	  offset_limit_cnt= 0;
434
	  /*
1273.2.7 by Stewart Smith
fix accidental mangling of comment: s/order_st BY/ORDER BY/. in sql_union.cc
435
	    We can't use LIMIT at this stage if we are using ORDER BY for the
1 by brian
clean slate
436
	    whole query
437
	  */
438
	  if (sl->order_list.first || describe)
439
	    select_limit_cnt= HA_POS_ERROR;
440
        }
441
442
        /*
443
          When using braces, SQL_CALC_FOUND_ROWS affects the whole query:
444
          we don't calculate found_rows() per union part.
445
          Otherwise, SQL_CALC_FOUND_ROWS should be done on all sub parts.
446
        */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
447
        sl->join->select_options=
1 by brian
clean slate
448
          (select_limit_cnt == HA_POS_ERROR || sl->braces) ?
449
          sl->options & ~OPTION_FOUND_ROWS : sl->options | found_rows_for_union;
450
451
	saved_error= sl->join->optimize();
452
      }
453
      if (!saved_error)
454
      {
1208.3.2 by brian
Update for Cursor renaming.
455
	records_at_start= table->cursor->stats.records;
1 by brian
clean slate
456
	sl->join->exec();
457
        if (sl == union_distinct)
458
	{
1208.3.2 by brian
Update for Cursor renaming.
459
	  if (table->cursor->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 */
1208.3.2 by brian
Update for Cursor renaming.
483
      int error= table->cursor->info(HA_STATUS_VARIABLE);
1216.1.1 by Brian Aker
Move print_error up to Engine.
484
      if (error)
1 by brian
clean slate
485
      {
1216.1.1 by Brian Aker
Move print_error up to Engine.
486
        table->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)
1208.3.2 by brian
Update for Cursor renaming.
499
			      ((table->cursor->stats.records -  records_at_start)));
1 by brian
clean slate
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
          */
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
558
	        join->reset(session, item_list, fake_select_lex->options, result);
520.1.22 by Brian Aker
Second pass of thd cleanup
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
      {
1208.3.2 by brian
Update for Cursor renaming.
579
	session->limit_found_rows = (uint64_t)table->cursor->stats.records + add_rows;
520.1.22 by Brian Aker
Second pass of thd cleanup
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
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
736
737
} /* namespace drizzled */