~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_union.cc

  • Committer: Brian Aker
  • Date: 2009-02-10 00:14:40 UTC
  • Revision ID: brian@tangent.org-20090210001440-qjg8eofh3h93064b
Adding Multi-threaded Scheduler into the system.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#include <drizzled/server_includes.h>
21
21
#include <drizzled/sql_select.h>
22
22
#include <drizzled/error.h>
 
23
#include <drizzled/item/type_holder.h>
 
24
#include <drizzled/sql_base.h>
23
25
 
24
26
bool mysql_union(Session *session, LEX *, select_result *result,
25
 
                 SELECT_LEX_UNIT *unit, ulong setup_tables_done_option)
 
27
                 Select_Lex_Unit *unit, ulong setup_tables_done_option)
26
28
{
27
29
  bool res;
28
30
  if (!(res= unit->prepare(session, result, SELECT_NO_UNLOCK |
38
40
** store records in temporary table for UNION
39
41
***************************************************************************/
40
42
 
41
 
int select_union::prepare(List<Item> &, SELECT_LEX_UNIT *u)
 
43
int select_union::prepare(List<Item> &, Select_Lex_Unit *u)
42
44
{
43
45
  unit= u;
44
46
  return 0;
61
63
  {
62
64
    /* create_myisam_from_heap will generate error if needed */
63
65
    if (table->file->is_fatal_error(error, HA_CHECK_DUP) &&
64
 
        create_myisam_from_heap(session, table, tmp_table_param.start_recinfo, 
 
66
        create_myisam_from_heap(session, table, tmp_table_param.start_recinfo,
65
67
                                &tmp_table_param.recinfo, error, 1))
66
68
      return 1;
67
69
  }
150
152
  initialization procedures before fake_select_lex preparation()
151
153
 
152
154
  SYNOPSIS
153
 
    st_select_lex_unit::init_prepare_fake_select_lex()
 
155
    Select_Lex_Unit::init_prepare_fake_select_lex()
154
156
    session             - thread handler
155
157
 
156
158
  RETURN
158
160
*/
159
161
 
160
162
void
161
 
st_select_lex_unit::init_prepare_fake_select_lex(Session *session_arg) 
 
163
Select_Lex_Unit::init_prepare_fake_select_lex(Session *session_arg)
162
164
{
163
165
  session_arg->lex->current_select= fake_select_lex;
164
166
  fake_select_lex->table_list.link_in_list((unsigned char *)&result_table_list,
165
167
                                           (unsigned char **)
166
168
                                           &result_table_list.next_local);
167
 
  fake_select_lex->context.table_list= 
168
 
    fake_select_lex->context.first_name_resolution_table= 
 
169
  fake_select_lex->context.table_list=
 
170
    fake_select_lex->context.first_name_resolution_table=
169
171
    fake_select_lex->get_table_list();
170
172
 
171
173
  for (order_st *order= (order_st *) global_parameters->order_list.first;
183
185
}
184
186
 
185
187
 
186
 
bool st_select_lex_unit::prepare(Session *session_arg, select_result *sel_result,
 
188
bool Select_Lex_Unit::prepare(Session *session_arg, select_result *sel_result,
187
189
                                 uint32_t additional_options)
188
190
{
189
 
  SELECT_LEX *lex_select_save= session_arg->lex->current_select;
190
 
  SELECT_LEX *sl, *first_sl= first_select();
 
191
  Select_Lex *lex_select_save= session_arg->lex->current_select;
 
192
  Select_Lex *sl, *first_sl= first_select();
191
193
  select_result *tmp_result;
192
194
  bool is_union_select;
193
195
  Table *empty_table= 0;
222
224
  }
223
225
  prepared= 1;
224
226
  saved_error= false;
225
 
  
 
227
 
226
228
  session_arg->lex->current_select= sl= first_sl;
227
229
  found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
228
230
  is_union_select= is_union() || fake_select_lex;
245
247
  {
246
248
    bool can_skip_order_by;
247
249
    sl->options|=  SELECT_NO_UNLOCK;
248
 
    JOIN *join= new JOIN(session_arg, sl->item_list, 
 
250
    JOIN *join= new JOIN(session_arg, sl->item_list,
249
251
                         sl->options | session_arg->options | additional_options,
250
252
                         tmp_result);
251
253
    /*
318
320
        goto err;
319
321
      }
320
322
      List_iterator_fast<Item> it(sl->item_list);
321
 
      List_iterator_fast<Item> tp(types);       
 
323
      List_iterator_fast<Item> tp(types);
322
324
      Item *type, *item_tmp;
323
325
      while ((type= tp++, item_tmp= it++))
324
326
      {
347
349
        goto err;
348
350
      }
349
351
    }
350
 
    
 
352
 
351
353
    create_options= (first_sl->options | session_arg->options |
352
354
                     TMP_TABLE_ALL_COLUMNS);
353
355
 
386
388
}
387
389
 
388
390
 
389
 
bool st_select_lex_unit::exec()
 
391
bool Select_Lex_Unit::exec()
390
392
{
391
 
  SELECT_LEX *lex_select_save= session->lex->current_select;
392
 
  SELECT_LEX *select_cursor=first_select();
 
393
  Select_Lex *lex_select_save= session->lex->current_select;
 
394
  Select_Lex *select_cursor=first_select();
393
395
  uint64_t add_rows=0;
394
396
  ha_rows examined_rows= 0;
395
397
 
396
398
  if (executed && !uncacheable && !describe)
397
399
    return(false);
398
400
  executed= 1;
399
 
  
 
401
 
400
402
  if (uncacheable || !item || !item->assigned() || describe)
401
403
  {
402
404
    if (item)
415
417
        assert(0);
416
418
      }
417
419
    }
418
 
    for (SELECT_LEX *sl= select_cursor; sl; sl= sl->next_select())
 
420
    for (Select_Lex *sl= select_cursor; sl; sl= sl->next_select())
419
421
    {
420
422
      ha_rows records_at_start= 0;
421
423
      session->lex->current_select= sl;
441
443
          we don't calculate found_rows() per union part.
442
444
          Otherwise, SQL_CALC_FOUND_ROWS should be done on all sub parts.
443
445
        */
444
 
        sl->join->select_options= 
 
446
        sl->join->select_options=
445
447
          (select_limit_cnt == HA_POS_ERROR || sl->braces) ?
446
448
          sl->options & ~OPTION_FOUND_ROWS : sl->options | found_rows_for_union;
447
449
 
448
 
        /* dump_TableList_struct(select_lex, select_lex->leaf_tables); */
449
450
        if (sl->join->flatten_subqueries())
450
451
          return(true);
451
452
 
452
 
        /* dump_TableList_struct(select_lex, select_lex->leaf_tables); */
453
453
        saved_error= sl->join->optimize();
454
454
      }
455
455
      if (!saved_error)
488
488
        table->file->print_error(error, MYF(0));
489
489
        return(1);
490
490
      }
491
 
      if (found_rows_for_union && !sl->braces && 
 
491
      if (found_rows_for_union && !sl->braces &&
492
492
          select_limit_cnt != HA_POS_ERROR)
493
493
      {
494
494
        /*
531
531
        fake_select_lex->join->no_const_tables= true;
532
532
 
533
533
        /*
534
 
          Fake st_select_lex should have item list for correctref_array
 
534
          Fake Select_Lex should have item list for correctref_array
535
535
          allocation.
536
536
        */
537
537
        fake_select_lex->item_list= item_list;
554
554
             - 1st time is a real evaluation to get the subquery value
555
555
             - 2nd time is to produce EXPLAIN output rows.
556
556
            1st execution sets certain members (e.g. select_result) to perform
557
 
            subquery execution rather than EXPLAIN line production. In order 
 
557
            subquery execution rather than EXPLAIN line production. In order
558
558
            to reset them back, we re-do all of the actions (yes it is ugly):
559
559
          */
560
560
          join->init(session, item_list, fake_select_lex->options, result);
592
592
}
593
593
 
594
594
 
595
 
bool st_select_lex_unit::cleanup()
 
595
bool Select_Lex_Unit::cleanup()
596
596
{
597
597
  int error= 0;
598
598
 
611
611
    table= 0; // Safety
612
612
  }
613
613
 
614
 
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
614
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
615
615
    error|= sl->cleanup();
616
616
 
617
617
  if (fake_select_lex)
635
635
}
636
636
 
637
637
 
638
 
void st_select_lex_unit::reinit_exec_mechanism()
 
638
void Select_Lex_Unit::reinit_exec_mechanism()
639
639
{
640
640
  prepared= optimized= executed= 0;
641
641
}
645
645
  change select_result object of unit
646
646
 
647
647
  SYNOPSIS
648
 
    st_select_lex_unit::change_result()
 
648
    Select_Lex_Unit::change_result()
649
649
    result      new select_result object
650
650
    old_result  old select_result object
651
651
 
654
654
    true  - error
655
655
*/
656
656
 
657
 
bool st_select_lex_unit::change_result(select_result_interceptor *new_result,
 
657
bool Select_Lex_Unit::change_result(select_result_interceptor *new_result,
658
658
                                       select_result_interceptor *old_result)
659
659
{
660
660
  bool res= false;
661
 
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
 
661
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
662
662
  {
663
663
    if (sl->join && sl->join->result == old_result)
664
664
      if (sl->join->change_result(new_result))
673
673
  Get column type information for this unit.
674
674
 
675
675
  SYNOPSIS
676
 
    st_select_lex_unit::get_unit_column_types()
 
676
    Select_Lex_Unit::get_unit_column_types()
677
677
 
678
678
  DESCRIPTION
679
679
    For a single-select the column types are taken
680
680
    from the list of selected items. For a union this function
681
 
    assumes that st_select_lex_unit::prepare has been called
 
681
    assumes that Select_Lex_Unit::prepare has been called
682
682
    and returns the type holders that were created for unioned
683
683
    column types of all selects.
684
684
 
685
685
  NOTES
686
686
    The implementation of this function should be in sync with
687
 
    st_select_lex_unit::prepare()
 
687
    Select_Lex_Unit::prepare()
688
688
*/
689
689
 
690
 
List<Item> *st_select_lex_unit::get_unit_column_types()
 
690
List<Item> *Select_Lex_Unit::get_unit_column_types()
691
691
{
692
 
  SELECT_LEX *sl= first_select();
 
692
  Select_Lex *sl= first_select();
693
693
 
694
694
  if (is_union())
695
695
  {
701
701
  return &sl->item_list;
702
702
}
703
703
 
704
 
bool st_select_lex::cleanup()
 
704
bool Select_Lex::cleanup()
705
705
{
706
706
  bool error= false;
707
707
 
708
708
  if (join)
709
709
  {
710
 
    assert((st_select_lex*)join->select_lex == this);
 
710
    assert((Select_Lex*)join->select_lex == this);
711
711
    error= join->destroy();
712
712
    delete join;
713
713
    join= 0;
714
714
  }
715
 
  for (SELECT_LEX_UNIT *lex_unit= first_inner_unit(); lex_unit ;
 
715
  for (Select_Lex_Unit *lex_unit= first_inner_unit(); lex_unit ;
716
716
       lex_unit= lex_unit->next_unit())
717
717
  {
718
718
    error= (bool) ((uint) error | (uint) lex_unit->cleanup());
723
723
}
724
724
 
725
725
 
726
 
void st_select_lex::cleanup_all_joins(bool full)
 
726
void Select_Lex::cleanup_all_joins(bool full)
727
727
{
728
 
  SELECT_LEX_UNIT *unit;
729
 
  SELECT_LEX *sl;
 
728
  Select_Lex_Unit *unit;
 
729
  Select_Lex *sl;
730
730
 
731
731
  if (join)
732
732
    join->cleanup(full);