~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
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
1 by brian
clean slate
15
16
/*
17
  UNION  of select's
18
  UNION's  were introduced by Monty and Sinisa <sinisa@mysql.com>
19
*/
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
20
#include <config.h>
2154.2.17 by Brian Aker
Additional removal of session
21
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
22
#include <drizzled/sql_select.h>
549 by Monty Taylor
Took gettext.h out of header files.
23
#include <drizzled/error.h>
584.4.5 by Monty Taylor
Split out cache_row and type_holder.
24
#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.
25
#include <drizzled/sql_base.h>
1008.3.22 by Stewart Smith
s/mysql_union/drizzle_union/
26
#include <drizzled/sql_union.h>
2154.2.17 by Brian Aker
Additional removal of session
27
#include <drizzled/select_union.h>
2154.2.18 by Brian Aker
Merge in all changes for include files.
28
#include <drizzled/sql_lex.h>
29
#include <drizzled/session.h>
2198.1.2 by Olaf van der Spek
Refactor includes
30
#include <drizzled/item/subselect.h>
1 by brian
clean slate
31
2318.6.55 by Olaf van der Spek
Refactor
32
namespace drizzled {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
33
1008.3.22 by Stewart Smith
s/mysql_union/drizzle_union/
34
bool drizzle_union(Session *session, LEX *, select_result *result,
35
		   Select_Lex_Unit *unit, uint64_t setup_tables_done_option)
1 by brian
clean slate
36
{
2318.6.55 by Olaf van der Spek
Refactor
37
  bool res= unit->prepare(session, result, SELECT_NO_UNLOCK | setup_tables_done_option);
2318.6.57 by Olaf van der Spek
Refactor
38
  if (not res)
1 by brian
clean slate
39
    res= unit->exec();
40
  if (res)
2318.6.57 by Olaf van der Spek
Refactor
41
    unit->cleanup();
2318.6.55 by Olaf van der Spek
Refactor
42
  return res;
1 by brian
clean slate
43
}
44
45
46
/***************************************************************************
47
** store records in temporary table for UNION
48
***************************************************************************/
49
848 by Brian Aker
typdef class removal (just... use the name of the class).
50
int select_union::prepare(List<Item> &, Select_Lex_Unit *u)
1 by brian
clean slate
51
{
52
  unit= u;
53
  return 0;
54
}
55
56
57
bool select_union::send_data(List<Item> &values)
58
{
59
  int error= 0;
60
  if (unit->offset_limit_cnt)
61
  {						// using limit offset,count
62
    unit->offset_limit_cnt--;
63
    return 0;
64
  }
1578.2.16 by Brian Aker
Merge in change to getTable() to private the field objects.
65
  fill_record(session, table->getFields(), values, true);
520.1.22 by Brian Aker
Second pass of thd cleanup
66
  if (session->is_error())
1 by brian
clean slate
67
    return 1;
68
1672.3.6 by Brian Aker
First pass in encapsulating row
69
  if ((error= table->cursor->insertRecord(table->getInsertRecord())))
1 by brian
clean slate
70
  {
71
    /* 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
72
    if (table->cursor->is_fatal_error(error, HA_CHECK_DUP))
73
    {
74
      my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
75
      return true;
76
    }
1 by brian
clean slate
77
  }
78
  return 0;
79
}
80
81
82
bool select_union::send_eof()
83
{
84
  return 0;
85
}
86
87
88
bool select_union::flush()
89
{
90
  int error;
1208.3.2 by brian
Update for Cursor renaming.
91
  if ((error=table->cursor->extra(HA_EXTRA_NO_CACHE)))
1 by brian
clean slate
92
  {
1216.1.1 by Brian Aker
Move print_error up to Engine.
93
    table->print_error(error, MYF(0));
1 by brian
clean slate
94
    return 1;
95
  }
96
  return 0;
97
}
98
99
/*
100
  Create a temporary table to store the result of select_union.
101
102
  SYNOPSIS
103
    select_union::create_result_table()
520.1.22 by Brian Aker
Second pass of thd cleanup
104
      session                thread handle
1 by brian
clean slate
105
      column_types       a list of items used to define columns of the
106
                         temporary table
107
      is_union_distinct  if set, the temporary table will eliminate
108
                         duplicates on insert
109
      options            create options
110
      table_alias        name of the temporary table
111
112
  DESCRIPTION
113
    Create a temporary table that is used to store the result of a UNION,
114
    derived table, or a materialized cursor.
115
116
  RETURN VALUE
117
    0                    The table has been created successfully.
118
    1                    create_tmp_table failed.
119
*/
120
121
bool
520.1.22 by Brian Aker
Second pass of thd cleanup
122
select_union::create_result_table(Session *session_arg, List<Item> *column_types,
151 by Brian Aker
Ulonglong to uint64_t
123
                                  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.
124
                                  const char *table_alias)
1 by brian
clean slate
125
{
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
126
  assert(table == NULL);
1101.1.16 by Monty Taylor
Reverted 1103
127
  tmp_table_param.init();
2183.2.20 by Olaf van der Spek
Use List::size()
128
  tmp_table_param.field_count= column_types->size();
1 by brian
clean slate
129
520.1.22 by Brian Aker
Second pass of thd cleanup
130
  if (! (table= create_tmp_table(session_arg, &tmp_table_param, *column_types,
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
131
                                 (Order*) NULL, is_union_distinct, 1,
1 by brian
clean slate
132
                                 options, HA_POS_ERROR, (char*) table_alias)))
1532.1.1 by Brian Aker
Merge of change to flip table instance to be share instance
133
  {
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
134
    return true;
1532.1.1 by Brian Aker
Merge of change to flip table instance to be share instance
135
  }
136
1208.3.2 by brian
Update for Cursor renaming.
137
  table->cursor->extra(HA_EXTRA_WRITE_CACHE);
138
  table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
1532.1.1 by Brian Aker
Merge of change to flip table instance to be share instance
139
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
140
  return false;
1 by brian
clean slate
141
}
142
143
144
/**
145
  Reset and empty the temporary table that stores the materialized query result.
146
147
  @note The cleanup performed here is exactly the same as for the two temp
148
  tables of JOIN - exec_tmp_table_[1 | 2].
149
*/
150
151
void select_union::cleanup()
152
{
1208.3.2 by brian
Update for Cursor renaming.
153
  table->cursor->extra(HA_EXTRA_RESET_STATE);
154
  table->cursor->ha_delete_all_rows();
1109.1.4 by Brian Aker
More Table refactor
155
  table->free_io_cache();
156
  table->filesort_free_buffers();
1 by brian
clean slate
157
}
158
159
160
/*
161
  initialization procedures before fake_select_lex preparation()
162
163
  SYNOPSIS
848 by Brian Aker
typdef class removal (just... use the name of the class).
164
    Select_Lex_Unit::init_prepare_fake_select_lex()
520.1.22 by Brian Aker
Second pass of thd cleanup
165
    session		- thread handler
1 by brian
clean slate
166
167
  RETURN
168
    options of SELECT
169
*/
170
171
void
848 by Brian Aker
typdef class removal (just... use the name of the class).
172
Select_Lex_Unit::init_prepare_fake_select_lex(Session *session_arg)
1 by brian
clean slate
173
{
2227.4.8 by Olaf van der Spek
Session::lex()
174
  session_arg->lex().current_select= fake_select_lex;
481 by Brian Aker
Remove all of uchar.
175
  fake_select_lex->table_list.link_in_list((unsigned char *)&result_table_list,
176
					   (unsigned char **)
1 by brian
clean slate
177
					   &result_table_list.next_local);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
178
  fake_select_lex->context.table_list=
179
    fake_select_lex->context.first_name_resolution_table=
1 by brian
clean slate
180
    fake_select_lex->get_table_list();
404 by Brian Aker
Removed dead variable
181
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
182
  for (Order *order= (Order *) global_parameters->order_list.first;
404 by Brian Aker
Removed dead variable
183
       order;
184
       order= order->next)
185
    order->item= &order->item_ptr;
186
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
187
  for (Order *order= (Order *)global_parameters->order_list.first;
1 by brian
clean slate
188
       order;
189
       order=order->next)
190
  {
191
    (*order->item)->walk(&Item::change_context_processor, 0,
481 by Brian Aker
Remove all of uchar.
192
                         (unsigned char*) &fake_select_lex->context);
1 by brian
clean slate
193
  }
194
}
195
196
848 by Brian Aker
typdef class removal (just... use the name of the class).
197
bool Select_Lex_Unit::prepare(Session *session_arg, select_result *sel_result,
892.2.5 by Monty Taylor
Fixed an oops.
198
                              uint64_t additional_options)
1 by brian
clean slate
199
{
2227.4.8 by Olaf van der Spek
Session::lex()
200
  Select_Lex *lex_select_save= session_arg->lex().current_select;
846 by Brian Aker
Removing on typedeffed class.
201
  Select_Lex *sl, *first_sl= first_select();
1 by brian
clean slate
202
  select_result *tmp_result;
203
  bool is_union_select;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
204
  Table *empty_table= 0;
1 by brian
clean slate
205
206
  describe= test(additional_options & SELECT_DESCRIBE);
207
208
  /*
209
    result object should be reassigned even if preparing already done for
210
    max/min subquery (ALL/ANY optimization)
211
  */
212
  result= sel_result;
213
214
  if (prepared)
215
  {
216
    if (describe)
217
    {
218
      /* fast reinit for EXPLAIN */
219
      for (sl= first_sl; sl; sl= sl->next_select())
220
      {
221
	sl->join->result= result;
222
	select_limit_cnt= HA_POS_ERROR;
223
	offset_limit_cnt= 0;
224
	if (result->prepare(sl->join->fields_list, this))
225
	{
2318.6.55 by Olaf van der Spek
Refactor
226
	  return true;
1 by brian
clean slate
227
	}
228
	sl->join->select_options|= SELECT_DESCRIBE;
229
	sl->join->reinit();
230
      }
231
    }
2318.6.55 by Olaf van der Spek
Refactor
232
    return false;
1 by brian
clean slate
233
  }
234
  prepared= 1;
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
235
  saved_error= false;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
236
2227.4.8 by Olaf van der Spek
Session::lex()
237
  session_arg->lex().current_select= sl= first_sl;
1 by brian
clean slate
238
  found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
239
  is_union_select= is_union() || fake_select_lex;
240
241
  /* Global option */
242
243
  if (is_union_select)
244
  {
2318.6.90 by Olaf van der Spek
Refactor
245
    tmp_result= union_result= new select_union;
1 by brian
clean slate
246
    if (describe)
247
      tmp_result= sel_result;
248
  }
249
  else
250
    tmp_result= sel_result;
251
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
252
  sl->context.resolve_in_select_list= true;
1 by brian
clean slate
253
254
  for (;sl; sl= sl->next_select())
255
  {
256
    bool can_skip_order_by;
257
    sl->options|=  SELECT_NO_UNLOCK;
1541.1.1 by Brian Aker
JOIN -> Join rename
258
    Join *join= new Join(session_arg, sl->item_list,
520.1.22 by Brian Aker
Second pass of thd cleanup
259
			 sl->options | session_arg->options | additional_options,
1 by brian
clean slate
260
			 tmp_result);
261
    /*
262
      setup_tables_done_option should be set only for very first SELECT,
263
      because it protect from secont setup_tables call for select-like non
264
      select commands (DELETE/INSERT/...) and they use only very first
265
      SELECT (for union it can be only INSERT ... SELECT).
266
    */
267
    additional_options&= ~OPTION_SETUP_TABLES_DONE;
268
    if (!join)
269
      goto err;
270
2227.4.8 by Olaf van der Spek
Session::lex()
271
    session_arg->lex().current_select= sl;
1 by brian
clean slate
272
273
    can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit);
274
275
    saved_error= join->prepare(&sl->ref_pointer_array,
327.2.4 by Brian Aker
Refactoring table.h
276
                               (TableList*) sl->table_list.first,
1 by brian
clean slate
277
                               sl->with_wild,
278
                               sl->where,
279
                               (can_skip_order_by ? 0 :
2183.2.20 by Olaf van der Spek
Use List::size()
280
                                sl->order_list.size()) +
281
                               sl->group_list.size(),
1 by brian
clean slate
282
                               can_skip_order_by ?
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
283
                               (Order*) NULL : (Order *)sl->order_list.first,
284
                               (Order*) sl->group_list.first,
1 by brian
clean slate
285
                               sl->having,
286
                               sl, this);
287
    /* There are no * in the statement anymore (for PS) */
288
    sl->with_wild= 0;
289
520.1.22 by Brian Aker
Second pass of thd cleanup
290
    if (saved_error || (saved_error= session_arg->is_fatal_error))
1 by brian
clean slate
291
      goto err;
292
    /*
293
      Use items list of underlaid select for derived tables to preserve
294
      information about fields lengths and exact types
295
    */
296
    if (!is_union_select)
297
      types= first_sl->item_list;
298
    else if (sl == first_sl)
299
    {
300
      /*
301
        We need to create an empty table object. It is used
302
        to create tmp_table fields in Item_type_holder.
303
        The main reason of this is that we can't create
304
        field object without table.
305
      */
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
306
      assert(!empty_table);
2318.6.20 by Olaf van der Spek
Refactor
307
      empty_table= (Table*) session->mem.calloc(sizeof(Table));
2179.2.1 by Olaf van der Spek
Rename List::empty to clear (std::list uses clear)
308
      types.clear();
2183.2.13 by Olaf van der Spek
Use List::begin()
309
      List<Item>::iterator it(sl->item_list.begin());
2318.6.20 by Olaf van der Spek
Refactor
310
      while (Item* item_tmp= it++)
1 by brian
clean slate
311
      {
312
	/* Error's in 'new' will be detected after loop */
520.1.22 by Brian Aker
Second pass of thd cleanup
313
	types.push_back(new Item_type_holder(session_arg, item_tmp));
1 by brian
clean slate
314
      }
315
520.1.22 by Brian Aker
Second pass of thd cleanup
316
      if (session_arg->is_fatal_error)
1 by brian
clean slate
317
	goto err; // out of memory
318
    }
319
    else
320
    {
2183.2.20 by Olaf van der Spek
Use List::size()
321
      if (types.size() != sl->item_list.size())
1 by brian
clean slate
322
      {
323
	my_message(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT,
324
		   ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT),MYF(0));
325
	goto err;
326
      }
2183.2.13 by Olaf van der Spek
Use List::begin()
327
      List<Item>::iterator it(sl->item_list.begin());
328
      List<Item>::iterator tp(types.begin());
1 by brian
clean slate
329
      Item *type, *item_tmp;
330
      while ((type= tp++, item_tmp= it++))
331
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
332
        if (((Item_type_holder*)type)->join_types(session_arg, item_tmp))
2318.6.55 by Olaf van der Spek
Refactor
333
	  return true;
1 by brian
clean slate
334
      }
335
    }
336
  }
337
338
  if (is_union_select)
339
  {
340
    /*
341
      Check that it was possible to aggregate
342
      all collations together for UNION.
343
    */
2183.2.13 by Olaf van der Spek
Use List::begin()
344
    List<Item>::iterator tp(types.begin());
1 by brian
clean slate
345
    Item *type;
151 by Brian Aker
Ulonglong to uint64_t
346
    uint64_t create_options;
1 by brian
clean slate
347
348
    while ((type= tp++))
349
    {
350
      if (type->result_type() == STRING_RESULT &&
351
          type->collation.derivation == DERIVATION_NONE)
352
      {
353
        my_error(ER_CANT_AGGREGATE_NCOLLATIONS, MYF(0), "UNION");
354
        goto err;
355
      }
356
    }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
357
2385.1.4 by Olaf van der Spek
Refactor setTableName
358
    create_options= first_sl->options | session_arg->options | TMP_TABLE_ALL_COLUMNS;
1 by brian
clean slate
359
2385.1.4 by Olaf van der Spek
Refactor setTableName
360
    if (union_result->create_result_table(session, &types, test(union_distinct), create_options, ""))
1 by brian
clean slate
361
      goto err;
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
362
    memset(&result_table_list, 0, sizeof(result_table_list));
2385.1.9 by Olaf van der Spek
Use const char* instead of str_ref
363
    result_table_list.setSchemaName("");
1039.1.4 by Brian Aker
Modified alias to being const.
364
    result_table_list.alias= "union";
2385.1.9 by Olaf van der Spek
Use const char* instead of str_ref
365
    result_table_list.setTableName("union");
1 by brian
clean slate
366
    result_table_list.table= table= union_result->table;
367
2227.4.8 by Olaf van der Spek
Session::lex()
368
    session_arg->lex().current_select= lex_select_save;
2246.3.2 by Olaf van der Spek
Refactor
369
    if (item_list.is_empty())
370
      table->fill_item_list(item_list);
1 by brian
clean slate
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
      */
2318.7.22 by Olaf van der Spek
Use assert(false) instead of assert(1)
377
      assert(false);
1 by brian
clean slate
378
    }
379
  }
380
2227.4.8 by Olaf van der Spek
Session::lex()
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:
2227.4.8 by Olaf van der Spek
Session::lex()
386
  session_arg->lex().current_select= lex_select_save;
2318.6.55 by Olaf van der Spek
Refactor
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
{
2227.4.8 by Olaf van der Spek
Session::lex()
393
  Select_Lex *lex_select_save= session->lex().current_select;
846 by Brian Aker
Removing on typedeffed class.
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
1858.1.1 by Padraig O'Sullivan
Replaced a uint8_t member with a standard bitset since it was being used as a bitset....
398
  if (executed && uncacheable.none() && ! describe)
399
    return false;
1 by brian
clean slate
400
  executed= 1;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
401
1858.1.1 by Padraig O'Sullivan
Replaced a uint8_t member with a standard bitset since it was being used as a bitset....
402
  if (uncacheable.any() || ! item || ! item->assigned() || describe)
1 by brian
clean slate
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;
2227.4.8 by Olaf van der Spek
Session::lex()
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
	  /*
1273.2.7 by Stewart Smith
fix accidental mangling of comment: s/order_st BY/ORDER BY/. in sql_union.cc
434
	    We can't use LIMIT at this stage if we are using ORDER 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))
2318.6.55 by Olaf van der Spek
Refactor
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
	  {
2227.4.8 by Olaf van der Spek
Session::lex()
471
	    session->lex().current_select= lex_select_save;
2318.6.77 by Olaf van der Spek
Refactor
472
	    return 1;
1 by brian
clean slate
473
	  }
474
	}
475
      }
476
      if (saved_error)
477
      {
2227.4.8 by Olaf van der Spek
Session::lex()
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));
2318.6.77 by Olaf van der Spek
Refactor
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);
1541.1.1 by Brian Aker
JOIN -> Join rename
511
      Join *join= fake_select_lex->join;
1 by brian
clean slate
512
      if (!join)
513
      {
514
	/*
515
	  allocate JOIN for fake select only once (prevent
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
516
	  select_query automatic allocation)
517
          TODO: The above is nonsense. select_query() will not allocate the
1 by brian
clean slate
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
	*/
2318.6.90 by Olaf van der Spek
Refactor
522
	fake_select_lex->join= new Join(session, item_list, fake_select_lex->options, result);
523
  fake_select_lex->join->no_const_tables= true;
1 by brian
clean slate
524
525
	/*
846 by Brian Aker
Removing on typedeffed class.
526
	  Fake Select_Lex should have item list for correctref_array
1 by brian
clean slate
527
	  allocation.
528
	*/
529
	fake_select_lex->item_list= item_list;
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
530
        saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
1 by brian
clean slate
531
                              &result_table_list,
532
                              0, item_list, NULL,
2183.2.20 by Olaf van der Spek
Use List::size()
533
                              global_parameters->order_list.size(),
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
534
                              (Order*)global_parameters->order_list.first,
535
                              (Order*) NULL, NULL,
1 by brian
clean slate
536
                              fake_select_lex->options | SELECT_NO_UNLOCK,
537
                              result, this, fake_select_lex);
538
      }
539
      else
540
      {
541
        if (describe)
542
        {
543
          /*
544
            In EXPLAIN command, constant subqueries that do not use any
545
            tables are executed two times:
546
             - 1st time is a real evaluation to get the subquery value
547
             - 2nd time is to produce EXPLAIN output rows.
548
            1st execution sets certain members (e.g. select_result) to perform
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
549
            subquery execution rather than EXPLAIN line production. In order
1 by brian
clean slate
550
            to reset them back, we re-do all of the actions (yes it is ugly):
551
          */
1039.2.1 by Jay Pipes
First phase refactoring the JOIN class:
552
	        join->reset(session, item_list, fake_select_lex->options, result);
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
553
          saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
1 by brian
clean slate
554
                                &result_table_list,
555
                                0, item_list, NULL,
2183.2.20 by Olaf van der Spek
Use List::size()
556
                                global_parameters->order_list.size(),
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
557
                                (Order*)global_parameters->order_list.first,
558
                                (Order*) NULL, NULL,
1 by brian
clean slate
559
                                fake_select_lex->options | SELECT_NO_UNLOCK,
560
                                result, this, fake_select_lex);
561
        }
562
        else
563
        {
564
          join->examined_rows= 0;
565
          saved_error= join->reinit();
566
          join->exec();
567
        }
568
      }
569
2179.2.1 by Olaf van der Spek
Rename List::empty to clear (std::list uses clear)
570
      fake_select_lex->table_list.clear();
1 by brian
clean slate
571
      if (!saved_error)
572
      {
1208.3.2 by brian
Update for Cursor renaming.
573
	session->limit_found_rows = (uint64_t)table->cursor->stats.records + add_rows;
520.1.22 by Brian Aker
Second pass of thd cleanup
574
        session->examined_row_count+= examined_rows;
1 by brian
clean slate
575
      }
576
      /*
577
	Mark for slow query log if any of the union parts didn't use
578
	indexes efficiently
579
      */
580
    }
581
  }
2227.4.8 by Olaf van der Spek
Session::lex()
582
  session->lex().current_select= lex_select_save;
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
583
  return(saved_error);
1 by brian
clean slate
584
}
585
586
848 by Brian Aker
typdef class removal (just... use the name of the class).
587
bool Select_Lex_Unit::cleanup()
1 by brian
clean slate
588
{
589
  int error= 0;
590
591
  if (cleaned)
592
  {
2318.6.55 by Olaf van der Spek
Refactor
593
    return false;
1 by brian
clean slate
594
  }
595
  cleaned= 1;
596
597
  if (union_result)
598
  {
2172.3.22 by Brian Aker
This patch adds safe_delete(). All of these locations in the code should be
599
    safe_delete(union_result);
1 by brian
clean slate
600
    table= 0; // Safety
601
  }
602
846 by Brian Aker
Removing on typedeffed class.
603
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1 by brian
clean slate
604
    error|= sl->cleanup();
605
606
  if (fake_select_lex)
607
  {
1541.1.1 by Brian Aker
JOIN -> Join rename
608
    Join *join;
1 by brian
clean slate
609
    if ((join= fake_select_lex->join))
610
    {
611
      join->tables_list= 0;
612
      join->tables= 0;
613
    }
614
    error|= fake_select_lex->cleanup();
2183.2.20 by Olaf van der Spek
Use List::size()
615
    if (fake_select_lex->order_list.size())
1 by brian
clean slate
616
    {
1892.3.3 by tdavies
struct order_st changed and renamed to c++ class named:Order
617
      Order *ord;
618
      for (ord= (Order*)fake_select_lex->order_list.first; ord; ord= ord->next)
1 by brian
clean slate
619
        (*ord->item)->cleanup();
620
    }
621
  }
622
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
623
  return(error);
1 by brian
clean slate
624
}
625
626
848 by Brian Aker
typdef class removal (just... use the name of the class).
627
void Select_Lex_Unit::reinit_exec_mechanism()
1 by brian
clean slate
628
{
629
  prepared= optimized= executed= 0;
630
}
631
632
633
/*
634
  change select_result object of unit
635
636
  SYNOPSIS
848 by Brian Aker
typdef class removal (just... use the name of the class).
637
    Select_Lex_Unit::change_result()
1 by brian
clean slate
638
    result	new select_result object
639
    old_result	old select_result object
640
641
  RETURN
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
642
    false - OK
643
    true  - error
1 by brian
clean slate
644
*/
645
848 by Brian Aker
typdef class removal (just... use the name of the class).
646
bool Select_Lex_Unit::change_result(select_result_interceptor *new_result,
1 by brian
clean slate
647
                                       select_result_interceptor *old_result)
648
{
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
649
  bool res= false;
846 by Brian Aker
Removing on typedeffed class.
650
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1 by brian
clean slate
651
  {
652
    if (sl->join && sl->join->result == old_result)
653
      if (sl->join->change_result(new_result))
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
654
	return true;
1 by brian
clean slate
655
  }
656
  if (fake_select_lex && fake_select_lex->join)
657
    res= fake_select_lex->join->change_result(new_result);
658
  return (res);
659
}
660
661
/*
662
  Get column type information for this unit.
663
664
  SYNOPSIS
848 by Brian Aker
typdef class removal (just... use the name of the class).
665
    Select_Lex_Unit::get_unit_column_types()
1 by brian
clean slate
666
667
  DESCRIPTION
668
    For a single-select the column types are taken
669
    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).
670
    assumes that Select_Lex_Unit::prepare has been called
1 by brian
clean slate
671
    and returns the type holders that were created for unioned
672
    column types of all selects.
673
674
  NOTES
675
    The implementation of this function should be in sync with
848 by Brian Aker
typdef class removal (just... use the name of the class).
676
    Select_Lex_Unit::prepare()
1 by brian
clean slate
677
*/
678
848 by Brian Aker
typdef class removal (just... use the name of the class).
679
List<Item> *Select_Lex_Unit::get_unit_column_types()
1 by brian
clean slate
680
{
846 by Brian Aker
Removing on typedeffed class.
681
  Select_Lex *sl= first_select();
1 by brian
clean slate
682
683
  if (is_union())
684
  {
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
685
    assert(prepared);
1 by brian
clean slate
686
    /* Types are generated during prepare */
687
    return &types;
688
  }
689
690
  return &sl->item_list;
691
}
692
846 by Brian Aker
Removing on typedeffed class.
693
bool Select_Lex::cleanup()
1 by brian
clean slate
694
{
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
695
  bool error= false;
1 by brian
clean slate
696
697
  if (join)
698
  {
846 by Brian Aker
Removing on typedeffed class.
699
    assert((Select_Lex*)join->select_lex == this);
1 by brian
clean slate
700
    error= join->destroy();
2172.3.22 by Brian Aker
This patch adds safe_delete(). All of these locations in the code should be
701
    safe_delete(join);
1 by brian
clean slate
702
  }
848 by Brian Aker
typdef class removal (just... use the name of the class).
703
  for (Select_Lex_Unit *lex_unit= first_inner_unit(); lex_unit ;
1 by brian
clean slate
704
       lex_unit= lex_unit->next_unit())
705
  {
895 by Brian Aker
Completion (?) of uint conversion.
706
    error= (bool) ((uint32_t) error | (uint32_t) lex_unit->cleanup());
1 by brian
clean slate
707
  }
2179.2.1 by Olaf van der Spek
Rename List::empty to clear (std::list uses clear)
708
  non_agg_fields.clear();
709
  inner_refs_list.clear();
51.1.68 by Jay Pipes
* Removed sql_test.cc custom debugging functions
710
  return(error);
1 by brian
clean slate
711
}
712
713
846 by Brian Aker
Removing on typedeffed class.
714
void Select_Lex::cleanup_all_joins(bool full)
1 by brian
clean slate
715
{
848 by Brian Aker
typdef class removal (just... use the name of the class).
716
  Select_Lex_Unit *unit;
846 by Brian Aker
Removing on typedeffed class.
717
  Select_Lex *sl;
1 by brian
clean slate
718
719
  if (join)
720
    join->cleanup(full);
721
722
  for (unit= first_inner_unit(); unit; unit= unit->next_unit())
723
    for (sl= unit->first_select(); sl; sl= sl->next_select())
724
      sl->cleanup_all_joins(full);
725
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
726
727
} /* namespace drizzled */