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