~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/sql_union.cc

Merged.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
 
16
 
16
17
/*
17
18
  UNION  of select's
18
19
  UNION's  were introduced by Monty and Sinisa <sinisa@mysql.com>
19
20
*/
20
 
#include <drizzled/server_includes.h>
21
 
#include <drizzled/sql_select.h>
22
 
#include <drizzled/drizzled_error_messages.h>
 
21
 
 
22
 
 
23
#include "mysql_priv.h"
 
24
#include "sql_select.h"
23
25
 
24
26
bool mysql_union(THD *thd,
25
 
                 LEX *lex __attribute__((unused)),
 
27
                 LEX *lex __attribute__((__unused__)),
26
28
                 select_result *result,
27
29
                 SELECT_LEX_UNIT *unit, ulong setup_tables_done_option)
28
30
{
40
42
** store records in temporary table for UNION
41
43
***************************************************************************/
42
44
 
43
 
int select_union::prepare(List<Item> &list __attribute__((unused)),
 
45
int select_union::prepare(List<Item> &list __attribute__((__unused__)),
44
46
                          SELECT_LEX_UNIT *u)
45
47
{
46
48
  unit= u;
124
126
  tmp_table_param.bit_fields_as_long= bit_fields_as_long;
125
127
 
126
128
  if (! (table= create_tmp_table(thd_arg, &tmp_table_param, *column_types,
127
 
                                 (order_st*) 0, is_union_distinct, 1,
 
129
                                 (ORDER*) 0, is_union_distinct, 1,
128
130
                                 options, HA_POS_ERROR, (char*) table_alias)))
129
131
    return true;
130
132
  table->file->extra(HA_EXTRA_WRITE_CACHE);
164
166
st_select_lex_unit::init_prepare_fake_select_lex(THD *thd_arg) 
165
167
{
166
168
  thd_arg->lex->current_select= fake_select_lex;
167
 
  fake_select_lex->table_list.link_in_list((unsigned char *)&result_table_list,
168
 
                                           (unsigned char **)
 
169
  fake_select_lex->table_list.link_in_list((uchar *)&result_table_list,
 
170
                                           (uchar **)
169
171
                                           &result_table_list.next_local);
170
172
  fake_select_lex->context.table_list= 
171
173
    fake_select_lex->context.first_name_resolution_table= 
172
174
    fake_select_lex->get_table_list();
173
 
 
174
 
  for (order_st *order= (order_st *) global_parameters->order_list.first;
175
 
       order;
176
 
       order= order->next)
177
 
    order->item= &order->item_ptr;
178
 
 
179
 
  for (order_st *order= (order_st *)global_parameters->order_list.first;
 
175
  if (!fake_select_lex->first_execution)
 
176
  {
 
177
    for (ORDER *order= (ORDER *) global_parameters->order_list.first;
 
178
         order;
 
179
         order= order->next)
 
180
      order->item= &order->item_ptr;
 
181
  }
 
182
  for (ORDER *order= (ORDER *)global_parameters->order_list.first;
180
183
       order;
181
184
       order=order->next)
182
185
  {
183
186
    (*order->item)->walk(&Item::change_context_processor, 0,
184
 
                         (unsigned char*) &fake_select_lex->context);
 
187
                         (uchar*) &fake_select_lex->context);
185
188
  }
186
189
}
187
190
 
188
191
 
189
192
bool st_select_lex_unit::prepare(THD *thd_arg, select_result *sel_result,
190
 
                                 uint32_t additional_options)
 
193
                                 ulong additional_options)
191
194
{
192
195
  SELECT_LEX *lex_select_save= thd_arg->lex->current_select;
193
196
  SELECT_LEX *sl, *first_sl= first_select();
194
197
  select_result *tmp_result;
195
198
  bool is_union_select;
196
 
  Table *empty_table= 0;
 
199
  TABLE *empty_table= 0;
197
200
 
198
201
  describe= test(additional_options & SELECT_DESCRIBE);
199
202
 
266
269
    can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit);
267
270
 
268
271
    saved_error= join->prepare(&sl->ref_pointer_array,
269
 
                               (TableList*) sl->table_list.first,
 
272
                               (TABLE_LIST*) sl->table_list.first,
270
273
                               sl->with_wild,
271
274
                               sl->where,
272
275
                               (can_skip_order_by ? 0 :
273
276
                                sl->order_list.elements) +
274
277
                               sl->group_list.elements,
275
278
                               can_skip_order_by ?
276
 
                               (order_st*) 0 : (order_st *)sl->order_list.first,
277
 
                               (order_st*) sl->group_list.first,
 
279
                               (ORDER*) 0 : (ORDER *)sl->order_list.first,
 
280
                               (ORDER*) sl->group_list.first,
278
281
                               sl->having,
279
 
                               (is_union_select ? (order_st*) 0 :
280
 
                                (order_st*) thd_arg->lex->proc_list.first),
 
282
                               (is_union_select ? (ORDER*) 0 :
 
283
                                (ORDER*) thd_arg->lex->proc_list.first),
281
284
                               sl, this);
282
285
    /* There are no * in the statement anymore (for PS) */
283
286
    sl->with_wild= 0;
299
302
        field object without table.
300
303
      */
301
304
      assert(!empty_table);
302
 
      empty_table= (Table*) thd->calloc(sizeof(Table));
 
305
      empty_table= (TABLE*) thd->calloc(sizeof(TABLE));
303
306
      types.empty();
304
307
      List_iterator_fast<Item> it(sl->item_list);
305
308
      Item *item_tmp;
357
360
    if (union_result->create_result_table(thd, &types, test(union_distinct),
358
361
                                          create_options, "", false))
359
362
      goto err;
360
 
    memset(&result_table_list, 0, sizeof(result_table_list));
 
363
    bzero((char*) &result_table_list, sizeof(result_table_list));
361
364
    result_table_list.db= (char*) "";
362
365
    result_table_list.table_name= result_table_list.alias= (char*) "union";
363
366
    result_table_list.table= table= union_result->table;
371
374
    }
372
375
    else
373
376
    {
 
377
      assert(thd->stmt_arena->is_conventional() == false);
374
378
      /*
375
379
        We're in execution of a prepared statement or stored procedure:
376
380
        reset field items to point at fields from the created temporary table.
377
381
      */
378
 
      assert(1);
 
382
      table->reset_item_list(&item_list);
379
383
    }
380
384
  }
381
385
 
432
436
        {
433
437
          offset_limit_cnt= 0;
434
438
          /*
435
 
            We can't use LIMIT at this stage if we are using order_st BY for the
 
439
            We can't use LIMIT at this stage if we are using ORDER BY for the
436
440
            whole query
437
441
          */
438
442
          if (sl->order_list.first || describe)
448
452
          (select_limit_cnt == HA_POS_ERROR || sl->braces) ?
449
453
          sl->options & ~OPTION_FOUND_ROWS : sl->options | found_rows_for_union;
450
454
 
451
 
        /* dump_TableList_struct(select_lex, select_lex->leaf_tables); */
 
455
        /* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
452
456
        if (sl->join->flatten_subqueries())
453
457
          return(true);
454
458
 
455
 
        /* dump_TableList_struct(select_lex, select_lex->leaf_tables); */
 
459
        /* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
456
460
        saved_error= sl->join->optimize();
457
461
      }
458
462
      if (!saved_error)
542
546
                              &result_table_list,
543
547
                              0, item_list, NULL,
544
548
                              global_parameters->order_list.elements,
545
 
                              (order_st*)global_parameters->order_list.first,
546
 
                              (order_st*) NULL, NULL, (order_st*) NULL,
 
549
                              (ORDER*)global_parameters->order_list.first,
 
550
                              (ORDER*) NULL, NULL, (ORDER*) NULL,
547
551
                              fake_select_lex->options | SELECT_NO_UNLOCK,
548
552
                              result, this, fake_select_lex);
549
553
      }
565
569
                                &result_table_list,
566
570
                                0, item_list, NULL,
567
571
                                global_parameters->order_list.elements,
568
 
                                (order_st*)global_parameters->order_list.first,
569
 
                                (order_st*) NULL, NULL, (order_st*) NULL,
 
572
                                (ORDER*)global_parameters->order_list.first,
 
573
                                (ORDER*) NULL, NULL, (ORDER*) NULL,
570
574
                                fake_select_lex->options | SELECT_NO_UNLOCK,
571
575
                                result, this, fake_select_lex);
572
576
        }
610
614
    delete union_result;
611
615
    union_result=0; // Safety
612
616
    if (table)
613
 
      table->free_tmp_table(thd);
 
617
      free_tmp_table(thd, table);
614
618
    table= 0; // Safety
615
619
  }
616
620
 
628
632
    error|= fake_select_lex->cleanup();
629
633
    if (fake_select_lex->order_list.elements)
630
634
    {
631
 
      order_st *ord;
632
 
      for (ord= (order_st*)fake_select_lex->order_list.first; ord; ord= ord->next)
 
635
      ORDER *ord;
 
636
      for (ord= (ORDER*)fake_select_lex->order_list.first; ord; ord= ord->next)
633
637
        (*ord->item)->cleanup();
634
638
    }
635
639
  }