~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_union.cc

  • Committer: Brian Aker
  • Date: 2009-07-03 00:39:59 UTC
  • mfrom: (1081.3.3 my_dir_removal)
  • Revision ID: brian@gaz-20090703003959-7y7mwb8u7etz9jco
Merge David

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 <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
25
#include <drizzled/sql_union.h>
26
26
 
27
 
namespace drizzled
28
 
{
29
 
 
30
27
bool drizzle_union(Session *session, LEX *, select_result *result,
31
28
                   Select_Lex_Unit *unit, uint64_t setup_tables_done_option)
32
29
{
59
56
    unit->offset_limit_cnt--;
60
57
    return 0;
61
58
  }
62
 
  fill_record(session, table->getFields(), values, true);
 
59
  fill_record(session, table->field, values, 1);
63
60
  if (session->is_error())
64
61
    return 1;
65
62
 
66
 
  if ((error= table->cursor->insertRecord(table->record[0])))
 
63
  if ((error= table->file->ha_write_row(table->record[0])))
67
64
  {
68
65
    /* 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
 
    }
 
66
    if (table->file->is_fatal_error(error, HA_CHECK_DUP) &&
 
67
        create_myisam_from_heap(session, table, tmp_table_param.start_recinfo,
 
68
                                &tmp_table_param.recinfo, error, 1))
 
69
      return 1;
74
70
  }
75
71
  return 0;
76
72
}
85
81
bool select_union::flush()
86
82
{
87
83
  int error;
88
 
  if ((error=table->cursor->extra(HA_EXTRA_NO_CACHE)))
 
84
  if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
89
85
  {
90
 
    table->print_error(error, MYF(0));
 
86
    table->file->print_error(error, MYF(0));
91
87
    return 1;
92
88
  }
93
89
  return 0;
105
101
                         duplicates on insert
106
102
      options            create options
107
103
      table_alias        name of the temporary table
 
104
      bit_fields_as_long convert bit fields to uint64_t
108
105
 
109
106
  DESCRIPTION
110
107
    Create a temporary table that is used to store the result of a UNION,
118
115
bool
119
116
select_union::create_result_table(Session *session_arg, List<Item> *column_types,
120
117
                                  bool is_union_distinct, uint64_t options,
121
 
                                  const char *table_alias)
 
118
                                  const char *table_alias,
 
119
                                  bool bit_fields_as_long)
122
120
{
123
 
  assert(table == NULL);
 
121
  assert(table == 0);
124
122
  tmp_table_param.init();
125
123
  tmp_table_param.field_count= column_types->elements;
 
124
  tmp_table_param.bit_fields_as_long= bit_fields_as_long;
126
125
 
127
126
  if (! (table= create_tmp_table(session_arg, &tmp_table_param, *column_types,
128
 
                                 (order_st*) NULL, is_union_distinct, 1,
 
127
                                 (order_st*) 0, is_union_distinct, 1,
129
128
                                 options, HA_POS_ERROR, (char*) table_alias)))
130
 
  {
131
129
    return true;
132
 
  }
133
 
 
134
 
  table->cursor->extra(HA_EXTRA_WRITE_CACHE);
135
 
  table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
136
 
 
 
130
  table->file->extra(HA_EXTRA_WRITE_CACHE);
 
131
  table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
137
132
  return false;
138
133
}
139
134
 
147
142
 
148
143
void select_union::cleanup()
149
144
{
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();
 
145
  table->file->extra(HA_EXTRA_RESET_STATE);
 
146
  table->file->ha_delete_all_rows();
 
147
  free_io_cache(table);
 
148
  filesort_free_buffers(table,0);
154
149
}
155
150
 
156
151
 
253
248
  {
254
249
    bool can_skip_order_by;
255
250
    sl->options|=  SELECT_NO_UNLOCK;
256
 
    Join *join= new Join(session_arg, sl->item_list,
 
251
    JOIN *join= new JOIN(session_arg, sl->item_list,
257
252
                         sl->options | session_arg->options | additional_options,
258
253
                         tmp_result);
259
254
    /*
278
273
                                sl->order_list.elements) +
279
274
                               sl->group_list.elements,
280
275
                               can_skip_order_by ?
281
 
                               (order_st*) NULL : (order_st *)sl->order_list.first,
 
276
                               (order_st*) 0 : (order_st *)sl->order_list.first,
282
277
                               (order_st*) sl->group_list.first,
283
278
                               sl->having,
284
279
                               sl, this);
358
353
                     TMP_TABLE_ALL_COLUMNS);
359
354
 
360
355
    if (union_result->create_result_table(session, &types, test(union_distinct),
361
 
                                          create_options, ""))
 
356
                                          create_options, "", false))
362
357
      goto err;
363
358
    memset(&result_table_list, 0, sizeof(result_table_list));
364
359
    result_table_list.db= (char*) "";
414
409
      {
415
410
        item->assigned(0); // We will reinit & rexecute unit
416
411
        item->reset();
417
 
        table->cursor->ha_delete_all_rows();
 
412
        table->file->ha_delete_all_rows();
418
413
      }
419
414
      /* re-enabling indexes for next subselect iteration */
420
 
      if (union_distinct && table->cursor->ha_enable_indexes(HA_KEY_SWITCH_ALL))
 
415
      if (union_distinct && table->file->ha_enable_indexes(HA_KEY_SWITCH_ALL))
421
416
      {
422
417
        assert(0);
423
418
      }
436
431
        {
437
432
          offset_limit_cnt= 0;
438
433
          /*
439
 
            We can't use LIMIT at this stage if we are using ORDER BY for the
 
434
            We can't use LIMIT at this stage if we are using order_st BY for the
440
435
            whole query
441
436
          */
442
437
          if (sl->order_list.first || describe)
452
447
          (select_limit_cnt == HA_POS_ERROR || sl->braces) ?
453
448
          sl->options & ~OPTION_FOUND_ROWS : sl->options | found_rows_for_union;
454
449
 
 
450
        if (sl->join->flatten_subqueries())
 
451
          return(true);
 
452
 
455
453
        saved_error= sl->join->optimize();
456
454
      }
457
455
      if (!saved_error)
458
456
      {
459
 
        records_at_start= table->cursor->stats.records;
 
457
        records_at_start= table->file->stats.records;
460
458
        sl->join->exec();
461
459
        if (sl == union_distinct)
462
460
        {
463
 
          if (table->cursor->ha_disable_indexes(HA_KEY_SWITCH_ALL))
 
461
          if (table->file->ha_disable_indexes(HA_KEY_SWITCH_ALL))
464
462
            return(true);
465
463
          table->no_keyread=1;
466
464
        }
484
482
        return(saved_error);
485
483
      }
486
484
      /* 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)
 
485
      int error= table->file->info(HA_STATUS_VARIABLE);
 
486
      if(error)
489
487
      {
490
 
        table->print_error(error, MYF(0));
 
488
        table->file->print_error(error, MYF(0));
491
489
        return(1);
492
490
      }
493
491
      if (found_rows_for_union && !sl->braces &&
500
498
          rows and actual rows added to the temporary table.
501
499
        */
502
500
        add_rows+= (uint64_t) (session->limit_found_rows - (uint64_t)
503
 
                              ((table->cursor->stats.records -  records_at_start)));
 
501
                              ((table->file->stats.records -  records_at_start)));
504
502
      }
505
503
    }
506
504
  }
513
511
    {
514
512
      set_limit(global_parameters);
515
513
      init_prepare_fake_select_lex(session);
516
 
      Join *join= fake_select_lex->join;
 
514
      JOIN *join= fake_select_lex->join;
517
515
      if (!join)
518
516
      {
519
517
        /*
524
522
          don't let it allocate the join. Perhaps this is because we need
525
523
          some special parameter values passed to join constructor?
526
524
        */
527
 
        if (!(fake_select_lex->join= new Join(session, item_list,
 
525
        if (!(fake_select_lex->join= new JOIN(session, item_list,
528
526
                                              fake_select_lex->options, result)))
529
527
        {
530
528
          fake_select_lex->table_list.empty();
580
578
      fake_select_lex->table_list.empty();
581
579
      if (!saved_error)
582
580
      {
583
 
        session->limit_found_rows = (uint64_t)table->cursor->stats.records + add_rows;
 
581
        session->limit_found_rows = (uint64_t)table->file->stats.records + add_rows;
584
582
        session->examined_row_count+= examined_rows;
585
583
      }
586
584
      /*
608
606
  {
609
607
    delete union_result;
610
608
    union_result=0; // Safety
 
609
    if (table)
 
610
      table->free_tmp_table(session);
611
611
    table= 0; // Safety
612
612
  }
613
613
 
616
616
 
617
617
  if (fake_select_lex)
618
618
  {
619
 
    Join *join;
 
619
    JOIN *join;
620
620
    if ((join= fake_select_lex->join))
621
621
    {
622
622
      join->tables_list= 0;
735
735
    for (sl= unit->first_select(); sl; sl= sl->next_select())
736
736
      sl->cleanup_all_joins(full);
737
737
}
738
 
 
739
 
} /* namespace drizzled */