~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_union.cc

  • Committer: Lee Bieber
  • Date: 2011-03-29 22:31:41 UTC
  • mfrom: (2257.1.3 build)
  • Revision ID: kalebral@gmail.com-20110329223141-yxc22h3l2he58sk0
Merge Andrew - 743842: Build failure using GCC 4.6
Merge Stewart - 738022: CachedDirectory silently fails to add entries if stat() fails
Merge Olaf - Common fwd: add copyright, add more declaration

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
  UNION  of select's
18
18
  UNION's  were introduced by Monty and Sinisa <sinisa@mysql.com>
19
19
*/
20
 
#include "config.h"
 
20
#include <config.h>
 
21
 
21
22
#include <drizzled/sql_select.h>
22
23
#include <drizzled/error.h>
23
24
#include <drizzled/item/type_holder.h>
24
25
#include <drizzled/sql_base.h>
25
26
#include <drizzled/sql_union.h>
 
27
#include <drizzled/select_union.h>
 
28
#include <drizzled/sql_lex.h>
 
29
#include <drizzled/session.h>
 
30
#include <drizzled/item/subselect.h>
26
31
 
27
32
namespace drizzled
28
33
{
122
127
{
123
128
  assert(table == NULL);
124
129
  tmp_table_param.init();
125
 
  tmp_table_param.field_count= column_types->elements;
 
130
  tmp_table_param.field_count= column_types->size();
126
131
 
127
132
  if (! (table= create_tmp_table(session_arg, &tmp_table_param, *column_types,
128
133
                                 (Order*) NULL, is_union_distinct, 1,
168
173
void
169
174
Select_Lex_Unit::init_prepare_fake_select_lex(Session *session_arg)
170
175
{
171
 
  session_arg->lex->current_select= fake_select_lex;
 
176
  session_arg->lex().current_select= fake_select_lex;
172
177
  fake_select_lex->table_list.link_in_list((unsigned char *)&result_table_list,
173
178
                                           (unsigned char **)
174
179
                                           &result_table_list.next_local);
194
199
bool Select_Lex_Unit::prepare(Session *session_arg, select_result *sel_result,
195
200
                              uint64_t additional_options)
196
201
{
197
 
  Select_Lex *lex_select_save= session_arg->lex->current_select;
 
202
  Select_Lex *lex_select_save= session_arg->lex().current_select;
198
203
  Select_Lex *sl, *first_sl= first_select();
199
204
  select_result *tmp_result;
200
205
  bool is_union_select;
231
236
  prepared= 1;
232
237
  saved_error= false;
233
238
 
234
 
  session_arg->lex->current_select= sl= first_sl;
 
239
  session_arg->lex().current_select= sl= first_sl;
235
240
  found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
236
241
  is_union_select= is_union() || fake_select_lex;
237
242
 
266
271
    if (!join)
267
272
      goto err;
268
273
 
269
 
    session_arg->lex->current_select= sl;
 
274
    session_arg->lex().current_select= sl;
270
275
 
271
276
    can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit);
272
277
 
275
280
                               sl->with_wild,
276
281
                               sl->where,
277
282
                               (can_skip_order_by ? 0 :
278
 
                                sl->order_list.elements) +
279
 
                               sl->group_list.elements,
 
283
                                sl->order_list.size()) +
 
284
                               sl->group_list.size(),
280
285
                               can_skip_order_by ?
281
286
                               (Order*) NULL : (Order *)sl->order_list.first,
282
287
                               (Order*) sl->group_list.first,
303
308
      */
304
309
      assert(!empty_table);
305
310
      empty_table= (Table*) session->calloc(sizeof(Table));
306
 
      types.empty();
307
 
      List_iterator_fast<Item> it(sl->item_list);
 
311
      types.clear();
 
312
      List<Item>::iterator it(sl->item_list.begin());
308
313
      Item *item_tmp;
309
314
      while ((item_tmp= it++))
310
315
      {
317
322
    }
318
323
    else
319
324
    {
320
 
      if (types.elements != sl->item_list.elements)
 
325
      if (types.size() != sl->item_list.size())
321
326
      {
322
327
        my_message(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT,
323
328
                   ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT),MYF(0));
324
329
        goto err;
325
330
      }
326
 
      List_iterator_fast<Item> it(sl->item_list);
327
 
      List_iterator_fast<Item> tp(types);
 
331
      List<Item>::iterator it(sl->item_list.begin());
 
332
      List<Item>::iterator tp(types.begin());
328
333
      Item *type, *item_tmp;
329
334
      while ((type= tp++, item_tmp= it++))
330
335
      {
340
345
      Check that it was possible to aggregate
341
346
      all collations together for UNION.
342
347
    */
343
 
    List_iterator_fast<Item> tp(types);
 
348
    List<Item>::iterator tp(types.begin());
344
349
    Item *type;
345
350
    uint64_t create_options;
346
351
 
366
371
    result_table_list.setTableName((char *) "union");
367
372
    result_table_list.table= table= union_result->table;
368
373
 
369
 
    session_arg->lex->current_select= lex_select_save;
370
 
    if (!item_list.elements)
371
 
    {
372
 
      saved_error= table->fill_item_list(&item_list);
373
 
      if (saved_error)
374
 
        goto err;
375
 
    }
 
374
    session_arg->lex().current_select= lex_select_save;
 
375
    if (item_list.is_empty())
 
376
      table->fill_item_list(item_list);
376
377
    else
377
378
    {
378
379
      /*
379
380
        We're in execution of a prepared statement or stored procedure:
380
381
        reset field items to point at fields from the created temporary table.
381
382
      */
382
 
      assert(1);
 
383
      assert(1); // Olaf: should this be assert(false)?
383
384
    }
384
385
  }
385
386
 
386
 
  session_arg->lex->current_select= lex_select_save;
 
387
  session_arg->lex().current_select= lex_select_save;
387
388
 
388
389
  return(saved_error || session_arg->is_fatal_error);
389
390
 
390
391
err:
391
 
  session_arg->lex->current_select= lex_select_save;
 
392
  session_arg->lex().current_select= lex_select_save;
392
393
  return(true);
393
394
}
394
395
 
395
396
 
396
397
bool Select_Lex_Unit::exec()
397
398
{
398
 
  Select_Lex *lex_select_save= session->lex->current_select;
 
399
  Select_Lex *lex_select_save= session->lex().current_select;
399
400
  Select_Lex *select_cursor=first_select();
400
401
  uint64_t add_rows=0;
401
402
  ha_rows examined_rows= 0;
425
426
    for (Select_Lex *sl= select_cursor; sl; sl= sl->next_select())
426
427
    {
427
428
      ha_rows records_at_start= 0;
428
 
      session->lex->current_select= sl;
 
429
      session->lex().current_select= sl;
429
430
 
430
431
      if (optimized)
431
432
        saved_error= sl->join->reinit();
473
474
          examined_rows+= session->examined_row_count;
474
475
          if (union_result->flush())
475
476
          {
476
 
            session->lex->current_select= lex_select_save;
 
477
            session->lex().current_select= lex_select_save;
477
478
            return(1);
478
479
          }
479
480
        }
480
481
      }
481
482
      if (saved_error)
482
483
      {
483
 
        session->lex->current_select= lex_select_save;
 
484
        session->lex().current_select= lex_select_save;
484
485
        return(saved_error);
485
486
      }
486
487
      /* Needed for the following test and for records_at_start in next loop */
518
519
      {
519
520
        /*
520
521
          allocate JOIN for fake select only once (prevent
521
 
          mysql_select automatic allocation)
522
 
          TODO: The above is nonsense. mysql_select() will not allocate the
 
522
          select_query automatic allocation)
 
523
          TODO: The above is nonsense. select_query() will not allocate the
523
524
          join if one already exists. There must be some other reason why we
524
525
          don't let it allocate the join. Perhaps this is because we need
525
526
          some special parameter values passed to join constructor?
527
528
        if (!(fake_select_lex->join= new Join(session, item_list,
528
529
                                              fake_select_lex->options, result)))
529
530
        {
530
 
          fake_select_lex->table_list.empty();
 
531
          fake_select_lex->table_list.clear();
531
532
          return(true);
532
533
        }
533
534
        fake_select_lex->join->no_const_tables= true;
537
538
          allocation.
538
539
        */
539
540
        fake_select_lex->item_list= item_list;
540
 
        saved_error= mysql_select(session, &fake_select_lex->ref_pointer_array,
 
541
        saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
541
542
                              &result_table_list,
542
543
                              0, item_list, NULL,
543
 
                              global_parameters->order_list.elements,
 
544
                              global_parameters->order_list.size(),
544
545
                              (Order*)global_parameters->order_list.first,
545
546
                              (Order*) NULL, NULL,
546
547
                              fake_select_lex->options | SELECT_NO_UNLOCK,
560
561
            to reset them back, we re-do all of the actions (yes it is ugly):
561
562
          */
562
563
                join->reset(session, item_list, fake_select_lex->options, result);
563
 
          saved_error= mysql_select(session, &fake_select_lex->ref_pointer_array,
 
564
          saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
564
565
                                &result_table_list,
565
566
                                0, item_list, NULL,
566
 
                                global_parameters->order_list.elements,
 
567
                                global_parameters->order_list.size(),
567
568
                                (Order*)global_parameters->order_list.first,
568
569
                                (Order*) NULL, NULL,
569
570
                                fake_select_lex->options | SELECT_NO_UNLOCK,
577
578
        }
578
579
      }
579
580
 
580
 
      fake_select_lex->table_list.empty();
 
581
      fake_select_lex->table_list.clear();
581
582
      if (!saved_error)
582
583
      {
583
584
        session->limit_found_rows = (uint64_t)table->cursor->stats.records + add_rows;
589
590
      */
590
591
    }
591
592
  }
592
 
  session->lex->current_select= lex_select_save;
 
593
  session->lex().current_select= lex_select_save;
593
594
  return(saved_error);
594
595
}
595
596
 
606
607
 
607
608
  if (union_result)
608
609
  {
609
 
    delete union_result;
610
 
    union_result=0; // Safety
 
610
    safe_delete(union_result);
611
611
    table= 0; // Safety
612
612
  }
613
613
 
623
623
      join->tables= 0;
624
624
    }
625
625
    error|= fake_select_lex->cleanup();
626
 
    if (fake_select_lex->order_list.elements)
 
626
    if (fake_select_lex->order_list.size())
627
627
    {
628
628
      Order *ord;
629
629
      for (ord= (Order*)fake_select_lex->order_list.first; ord; ord= ord->next)
709
709
  {
710
710
    assert((Select_Lex*)join->select_lex == this);
711
711
    error= join->destroy();
712
 
    delete join;
713
 
    join= 0;
 
712
    safe_delete(join);
714
713
  }
715
714
  for (Select_Lex_Unit *lex_unit= first_inner_unit(); lex_unit ;
716
715
       lex_unit= lex_unit->next_unit())
717
716
  {
718
717
    error= (bool) ((uint32_t) error | (uint32_t) lex_unit->cleanup());
719
718
  }
720
 
  non_agg_fields.empty();
721
 
  inner_refs_list.empty();
 
719
  non_agg_fields.clear();
 
720
  inner_refs_list.clear();
722
721
  return(error);
723
722
}
724
723