~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_union.cc

  • Committer: patrick crews
  • Date: 2011-02-23 17:17:25 UTC
  • mto: (2195.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2196.
  • Revision ID: gleebix@gmail.com-20110223171725-4tgewemxhsw1m7q8
Integrated randgen with dbqp.  We now have mode=randgen and a set of randgen test suites (very basic now).  Output = same as dtr : )  We also have mode=cleanup to kill any servers we have started.  Docs updates too.  Gendata utility allows us to populate test servers 

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>
26
30
 
27
31
namespace drizzled
28
32
{
168
172
void
169
173
Select_Lex_Unit::init_prepare_fake_select_lex(Session *session_arg)
170
174
{
171
 
  session_arg->lex->current_select= fake_select_lex;
 
175
  session_arg->getLex()->current_select= fake_select_lex;
172
176
  fake_select_lex->table_list.link_in_list((unsigned char *)&result_table_list,
173
177
                                           (unsigned char **)
174
178
                                           &result_table_list.next_local);
194
198
bool Select_Lex_Unit::prepare(Session *session_arg, select_result *sel_result,
195
199
                              uint64_t additional_options)
196
200
{
197
 
  Select_Lex *lex_select_save= session_arg->lex->current_select;
 
201
  Select_Lex *lex_select_save= session_arg->getLex()->current_select;
198
202
  Select_Lex *sl, *first_sl= first_select();
199
203
  select_result *tmp_result;
200
204
  bool is_union_select;
231
235
  prepared= 1;
232
236
  saved_error= false;
233
237
 
234
 
  session_arg->lex->current_select= sl= first_sl;
 
238
  session_arg->getLex()->current_select= sl= first_sl;
235
239
  found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
236
240
  is_union_select= is_union() || fake_select_lex;
237
241
 
266
270
    if (!join)
267
271
      goto err;
268
272
 
269
 
    session_arg->lex->current_select= sl;
 
273
    session_arg->getLex()->current_select= sl;
270
274
 
271
275
    can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit);
272
276
 
303
307
      */
304
308
      assert(!empty_table);
305
309
      empty_table= (Table*) session->calloc(sizeof(Table));
306
 
      types.empty();
307
 
      List_iterator_fast<Item> it(sl->item_list);
 
310
      types.clear();
 
311
      List<Item>::iterator it(sl->item_list.begin());
308
312
      Item *item_tmp;
309
313
      while ((item_tmp= it++))
310
314
      {
323
327
                   ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT),MYF(0));
324
328
        goto err;
325
329
      }
326
 
      List_iterator_fast<Item> it(sl->item_list);
327
 
      List_iterator_fast<Item> tp(types);
 
330
      List<Item>::iterator it(sl->item_list.begin());
 
331
      List<Item>::iterator tp(types.begin());
328
332
      Item *type, *item_tmp;
329
333
      while ((type= tp++, item_tmp= it++))
330
334
      {
340
344
      Check that it was possible to aggregate
341
345
      all collations together for UNION.
342
346
    */
343
 
    List_iterator_fast<Item> tp(types);
 
347
    List<Item>::iterator tp(types.begin());
344
348
    Item *type;
345
349
    uint64_t create_options;
346
350
 
366
370
    result_table_list.setTableName((char *) "union");
367
371
    result_table_list.table= table= union_result->table;
368
372
 
369
 
    session_arg->lex->current_select= lex_select_save;
 
373
    session_arg->getLex()->current_select= lex_select_save;
370
374
    if (!item_list.elements)
371
375
    {
372
376
      saved_error= table->fill_item_list(&item_list);
383
387
    }
384
388
  }
385
389
 
386
 
  session_arg->lex->current_select= lex_select_save;
 
390
  session_arg->getLex()->current_select= lex_select_save;
387
391
 
388
392
  return(saved_error || session_arg->is_fatal_error);
389
393
 
390
394
err:
391
 
  session_arg->lex->current_select= lex_select_save;
 
395
  session_arg->getLex()->current_select= lex_select_save;
392
396
  return(true);
393
397
}
394
398
 
395
399
 
396
400
bool Select_Lex_Unit::exec()
397
401
{
398
 
  Select_Lex *lex_select_save= session->lex->current_select;
 
402
  Select_Lex *lex_select_save= session->getLex()->current_select;
399
403
  Select_Lex *select_cursor=first_select();
400
404
  uint64_t add_rows=0;
401
405
  ha_rows examined_rows= 0;
425
429
    for (Select_Lex *sl= select_cursor; sl; sl= sl->next_select())
426
430
    {
427
431
      ha_rows records_at_start= 0;
428
 
      session->lex->current_select= sl;
 
432
      session->getLex()->current_select= sl;
429
433
 
430
434
      if (optimized)
431
435
        saved_error= sl->join->reinit();
473
477
          examined_rows+= session->examined_row_count;
474
478
          if (union_result->flush())
475
479
          {
476
 
            session->lex->current_select= lex_select_save;
 
480
            session->getLex()->current_select= lex_select_save;
477
481
            return(1);
478
482
          }
479
483
        }
480
484
      }
481
485
      if (saved_error)
482
486
      {
483
 
        session->lex->current_select= lex_select_save;
 
487
        session->getLex()->current_select= lex_select_save;
484
488
        return(saved_error);
485
489
      }
486
490
      /* Needed for the following test and for records_at_start in next loop */
518
522
      {
519
523
        /*
520
524
          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
 
525
          select_query automatic allocation)
 
526
          TODO: The above is nonsense. select_query() will not allocate the
523
527
          join if one already exists. There must be some other reason why we
524
528
          don't let it allocate the join. Perhaps this is because we need
525
529
          some special parameter values passed to join constructor?
527
531
        if (!(fake_select_lex->join= new Join(session, item_list,
528
532
                                              fake_select_lex->options, result)))
529
533
        {
530
 
          fake_select_lex->table_list.empty();
 
534
          fake_select_lex->table_list.clear();
531
535
          return(true);
532
536
        }
533
537
        fake_select_lex->join->no_const_tables= true;
537
541
          allocation.
538
542
        */
539
543
        fake_select_lex->item_list= item_list;
540
 
        saved_error= mysql_select(session, &fake_select_lex->ref_pointer_array,
 
544
        saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
541
545
                              &result_table_list,
542
546
                              0, item_list, NULL,
543
547
                              global_parameters->order_list.elements,
560
564
            to reset them back, we re-do all of the actions (yes it is ugly):
561
565
          */
562
566
                join->reset(session, item_list, fake_select_lex->options, result);
563
 
          saved_error= mysql_select(session, &fake_select_lex->ref_pointer_array,
 
567
          saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
564
568
                                &result_table_list,
565
569
                                0, item_list, NULL,
566
570
                                global_parameters->order_list.elements,
577
581
        }
578
582
      }
579
583
 
580
 
      fake_select_lex->table_list.empty();
 
584
      fake_select_lex->table_list.clear();
581
585
      if (!saved_error)
582
586
      {
583
587
        session->limit_found_rows = (uint64_t)table->cursor->stats.records + add_rows;
589
593
      */
590
594
    }
591
595
  }
592
 
  session->lex->current_select= lex_select_save;
 
596
  session->getLex()->current_select= lex_select_save;
593
597
  return(saved_error);
594
598
}
595
599
 
606
610
 
607
611
  if (union_result)
608
612
  {
609
 
    delete union_result;
610
 
    union_result=0; // Safety
 
613
    safe_delete(union_result);
611
614
    table= 0; // Safety
612
615
  }
613
616
 
709
712
  {
710
713
    assert((Select_Lex*)join->select_lex == this);
711
714
    error= join->destroy();
712
 
    delete join;
713
 
    join= 0;
 
715
    safe_delete(join);
714
716
  }
715
717
  for (Select_Lex_Unit *lex_unit= first_inner_unit(); lex_unit ;
716
718
       lex_unit= lex_unit->next_unit())
717
719
  {
718
720
    error= (bool) ((uint32_t) error | (uint32_t) lex_unit->cleanup());
719
721
  }
720
 
  non_agg_fields.empty();
721
 
  inner_refs_list.empty();
 
722
  non_agg_fields.clear();
 
723
  inner_refs_list.clear();
722
724
  return(error);
723
725
}
724
726