~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_union.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-03-24 15:33:01 UTC
  • mto: (968.2.18 mordred)
  • mto: This revision was merged to the branch mainline in revision 971.
  • Revision ID: osullivan.padraig@gmail.com-20090324153301-90gw63j1lsie1k9j
Removing a very mis-leading comment from sql_locale.cc

The comment was totally wrong and read "This file is build from
my_locale.pl"

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/*
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 <drizzled/server_includes.h>
21
21
#include <drizzled/sql_select.h>
22
22
#include <drizzled/error.h>
23
23
#include <drizzled/item/type_holder.h>
24
24
#include <drizzled/sql_base.h>
25
 
#include <drizzled/sql_union.h>
26
 
 
27
 
namespace drizzled
28
 
{
29
 
 
30
 
bool drizzle_union(Session *session, LEX *, select_result *result,
31
 
                   Select_Lex_Unit *unit, uint64_t setup_tables_done_option)
 
25
 
 
26
bool mysql_union(Session *session, LEX *, select_result *result,
 
27
                 Select_Lex_Unit *unit, uint64_t setup_tables_done_option)
32
28
{
33
29
  bool res;
34
30
  if (!(res= unit->prepare(session, result, SELECT_NO_UNLOCK |
59
55
    unit->offset_limit_cnt--;
60
56
    return 0;
61
57
  }
62
 
  fill_record(session, table->getFields(), values, true);
 
58
  fill_record(session, table->field, values, 1);
63
59
  if (session->is_error())
64
60
    return 1;
65
61
 
66
 
  if ((error= table->cursor->insertRecord(table->getInsertRecord())))
 
62
  if ((error= table->file->ha_write_row(table->record[0])))
67
63
  {
68
64
    /* create_myisam_from_heap will generate error if needed */
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
 
    }
 
65
    if (table->file->is_fatal_error(error, HA_CHECK_DUP) &&
 
66
        create_myisam_from_heap(session, table, tmp_table_param.start_recinfo,
 
67
                                &tmp_table_param.recinfo, error, 1))
 
68
      return 1;
74
69
  }
75
70
  return 0;
76
71
}
85
80
bool select_union::flush()
86
81
{
87
82
  int error;
88
 
  if ((error=table->cursor->extra(HA_EXTRA_NO_CACHE)))
 
83
  if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
89
84
  {
90
 
    table->print_error(error, MYF(0));
 
85
    table->file->print_error(error, MYF(0));
91
86
    return 1;
92
87
  }
93
88
  return 0;
105
100
                         duplicates on insert
106
101
      options            create options
107
102
      table_alias        name of the temporary table
 
103
      bit_fields_as_long convert bit fields to uint64_t
108
104
 
109
105
  DESCRIPTION
110
106
    Create a temporary table that is used to store the result of a UNION,
118
114
bool
119
115
select_union::create_result_table(Session *session_arg, List<Item> *column_types,
120
116
                                  bool is_union_distinct, uint64_t options,
121
 
                                  const char *table_alias)
 
117
                                  const char *table_alias,
 
118
                                  bool bit_fields_as_long)
122
119
{
123
 
  assert(table == NULL);
 
120
  assert(table == 0);
124
121
  tmp_table_param.init();
125
122
  tmp_table_param.field_count= column_types->elements;
 
123
  tmp_table_param.bit_fields_as_long= bit_fields_as_long;
126
124
 
127
125
  if (! (table= create_tmp_table(session_arg, &tmp_table_param, *column_types,
128
 
                                 (Order*) NULL, is_union_distinct, 1,
 
126
                                 (order_st*) 0, is_union_distinct, 1,
129
127
                                 options, HA_POS_ERROR, (char*) table_alias)))
130
 
  {
131
128
    return true;
132
 
  }
133
 
 
134
 
  table->cursor->extra(HA_EXTRA_WRITE_CACHE);
135
 
  table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
136
 
 
 
129
  table->file->extra(HA_EXTRA_WRITE_CACHE);
 
130
  table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
137
131
  return false;
138
132
}
139
133
 
147
141
 
148
142
void select_union::cleanup()
149
143
{
150
 
  table->cursor->extra(HA_EXTRA_RESET_STATE);
151
 
  table->cursor->ha_delete_all_rows();
152
 
  table->free_io_cache();
153
 
  table->filesort_free_buffers();
 
144
  table->file->extra(HA_EXTRA_RESET_STATE);
 
145
  table->file->ha_delete_all_rows();
 
146
  free_io_cache(table);
 
147
  filesort_free_buffers(table,0);
154
148
}
155
149
 
156
150
 
176
170
    fake_select_lex->context.first_name_resolution_table=
177
171
    fake_select_lex->get_table_list();
178
172
 
179
 
  for (Order *order= (Order *) global_parameters->order_list.first;
 
173
  for (order_st *order= (order_st *) global_parameters->order_list.first;
180
174
       order;
181
175
       order= order->next)
182
176
    order->item= &order->item_ptr;
183
177
 
184
 
  for (Order *order= (Order *)global_parameters->order_list.first;
 
178
  for (order_st *order= (order_st *)global_parameters->order_list.first;
185
179
       order;
186
180
       order=order->next)
187
181
  {
253
247
  {
254
248
    bool can_skip_order_by;
255
249
    sl->options|=  SELECT_NO_UNLOCK;
256
 
    Join *join= new Join(session_arg, sl->item_list,
 
250
    JOIN *join= new JOIN(session_arg, sl->item_list,
257
251
                         sl->options | session_arg->options | additional_options,
258
252
                         tmp_result);
259
253
    /*
278
272
                                sl->order_list.elements) +
279
273
                               sl->group_list.elements,
280
274
                               can_skip_order_by ?
281
 
                               (Order*) NULL : (Order *)sl->order_list.first,
282
 
                               (Order*) sl->group_list.first,
 
275
                               (order_st*) 0 : (order_st *)sl->order_list.first,
 
276
                               (order_st*) sl->group_list.first,
283
277
                               sl->having,
284
278
                               sl, this);
285
279
    /* There are no * in the statement anymore (for PS) */
358
352
                     TMP_TABLE_ALL_COLUMNS);
359
353
 
360
354
    if (union_result->create_result_table(session, &types, test(union_distinct),
361
 
                                          create_options, ""))
 
355
                                          create_options, "", false))
362
356
      goto err;
363
357
    memset(&result_table_list, 0, sizeof(result_table_list));
364
 
    result_table_list.setSchemaName((char*) "");
365
 
    result_table_list.alias= "union";
366
 
    result_table_list.setTableName((char *) "union");
 
358
    result_table_list.db= (char*) "";
 
359
    result_table_list.table_name= result_table_list.alias= (char*) "union";
367
360
    result_table_list.table= table= union_result->table;
368
361
 
369
362
    session_arg->lex->current_select= lex_select_save;
400
393
  uint64_t add_rows=0;
401
394
  ha_rows examined_rows= 0;
402
395
 
403
 
  if (executed && uncacheable.none() && ! describe)
404
 
    return false;
 
396
  if (executed && !uncacheable && !describe)
 
397
    return(false);
405
398
  executed= 1;
406
399
 
407
 
  if (uncacheable.any() || ! item || ! item->assigned() || describe)
 
400
  if (uncacheable || !item || !item->assigned() || describe)
408
401
  {
409
402
    if (item)
410
403
      item->reset_value_registration();
414
407
      {
415
408
        item->assigned(0); // We will reinit & rexecute unit
416
409
        item->reset();
417
 
        table->cursor->ha_delete_all_rows();
 
410
        table->file->ha_delete_all_rows();
418
411
      }
419
412
      /* re-enabling indexes for next subselect iteration */
420
 
      if (union_distinct && table->cursor->ha_enable_indexes(HA_KEY_SWITCH_ALL))
 
413
      if (union_distinct && table->file->ha_enable_indexes(HA_KEY_SWITCH_ALL))
421
414
      {
422
415
        assert(0);
423
416
      }
436
429
        {
437
430
          offset_limit_cnt= 0;
438
431
          /*
439
 
            We can't use LIMIT at this stage if we are using ORDER BY for the
 
432
            We can't use LIMIT at this stage if we are using order_st BY for the
440
433
            whole query
441
434
          */
442
435
          if (sl->order_list.first || describe)
452
445
          (select_limit_cnt == HA_POS_ERROR || sl->braces) ?
453
446
          sl->options & ~OPTION_FOUND_ROWS : sl->options | found_rows_for_union;
454
447
 
 
448
        if (sl->join->flatten_subqueries())
 
449
          return(true);
 
450
 
455
451
        saved_error= sl->join->optimize();
456
452
      }
457
453
      if (!saved_error)
458
454
      {
459
 
        records_at_start= table->cursor->stats.records;
 
455
        records_at_start= table->file->stats.records;
460
456
        sl->join->exec();
461
457
        if (sl == union_distinct)
462
458
        {
463
 
          if (table->cursor->ha_disable_indexes(HA_KEY_SWITCH_ALL))
 
459
          if (table->file->ha_disable_indexes(HA_KEY_SWITCH_ALL))
464
460
            return(true);
465
461
          table->no_keyread=1;
466
462
        }
484
480
        return(saved_error);
485
481
      }
486
482
      /* Needed for the following test and for records_at_start in next loop */
487
 
      int error= table->cursor->info(HA_STATUS_VARIABLE);
488
 
      if (error)
 
483
      int error= table->file->info(HA_STATUS_VARIABLE);
 
484
      if(error)
489
485
      {
490
 
        table->print_error(error, MYF(0));
 
486
        table->file->print_error(error, MYF(0));
491
487
        return(1);
492
488
      }
493
489
      if (found_rows_for_union && !sl->braces &&
500
496
          rows and actual rows added to the temporary table.
501
497
        */
502
498
        add_rows+= (uint64_t) (session->limit_found_rows - (uint64_t)
503
 
                              ((table->cursor->stats.records -  records_at_start)));
 
499
                              ((table->file->stats.records -  records_at_start)));
504
500
      }
505
501
    }
506
502
  }
513
509
    {
514
510
      set_limit(global_parameters);
515
511
      init_prepare_fake_select_lex(session);
516
 
      Join *join= fake_select_lex->join;
 
512
      JOIN *join= fake_select_lex->join;
517
513
      if (!join)
518
514
      {
519
515
        /*
524
520
          don't let it allocate the join. Perhaps this is because we need
525
521
          some special parameter values passed to join constructor?
526
522
        */
527
 
        if (!(fake_select_lex->join= new Join(session, item_list,
 
523
        if (!(fake_select_lex->join= new JOIN(session, item_list,
528
524
                                              fake_select_lex->options, result)))
529
525
        {
530
526
          fake_select_lex->table_list.empty();
541
537
                              &result_table_list,
542
538
                              0, item_list, NULL,
543
539
                              global_parameters->order_list.elements,
544
 
                              (Order*)global_parameters->order_list.first,
545
 
                              (Order*) NULL, NULL,
 
540
                              (order_st*)global_parameters->order_list.first,
 
541
                              (order_st*) NULL, NULL,
546
542
                              fake_select_lex->options | SELECT_NO_UNLOCK,
547
543
                              result, this, fake_select_lex);
548
544
      }
559
555
            subquery execution rather than EXPLAIN line production. In order
560
556
            to reset them back, we re-do all of the actions (yes it is ugly):
561
557
          */
562
 
                join->reset(session, item_list, fake_select_lex->options, result);
 
558
          join->init(session, item_list, fake_select_lex->options, result);
563
559
          saved_error= mysql_select(session, &fake_select_lex->ref_pointer_array,
564
560
                                &result_table_list,
565
561
                                0, item_list, NULL,
566
562
                                global_parameters->order_list.elements,
567
 
                                (Order*)global_parameters->order_list.first,
568
 
                                (Order*) NULL, NULL,
 
563
                                (order_st*)global_parameters->order_list.first,
 
564
                                (order_st*) NULL, NULL,
569
565
                                fake_select_lex->options | SELECT_NO_UNLOCK,
570
566
                                result, this, fake_select_lex);
571
567
        }
580
576
      fake_select_lex->table_list.empty();
581
577
      if (!saved_error)
582
578
      {
583
 
        session->limit_found_rows = (uint64_t)table->cursor->stats.records + add_rows;
 
579
        session->limit_found_rows = (uint64_t)table->file->stats.records + add_rows;
584
580
        session->examined_row_count+= examined_rows;
585
581
      }
586
582
      /*
608
604
  {
609
605
    delete union_result;
610
606
    union_result=0; // Safety
 
607
    if (table)
 
608
      table->free_tmp_table(session);
611
609
    table= 0; // Safety
612
610
  }
613
611
 
616
614
 
617
615
  if (fake_select_lex)
618
616
  {
619
 
    Join *join;
 
617
    JOIN *join;
620
618
    if ((join= fake_select_lex->join))
621
619
    {
622
620
      join->tables_list= 0;
625
623
    error|= fake_select_lex->cleanup();
626
624
    if (fake_select_lex->order_list.elements)
627
625
    {
628
 
      Order *ord;
629
 
      for (ord= (Order*)fake_select_lex->order_list.first; ord; ord= ord->next)
 
626
      order_st *ord;
 
627
      for (ord= (order_st*)fake_select_lex->order_list.first; ord; ord= ord->next)
630
628
        (*ord->item)->cleanup();
631
629
    }
632
630
  }
735
733
    for (sl= unit->first_select(); sl; sl= sl->next_select())
736
734
      sl->cleanup_all_joins(full);
737
735
}
738
 
 
739
 
} /* namespace drizzled */