~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_union.cc

  • Committer: Brian Aker
  • Date: 2009-11-24 02:06:37 UTC
  • mfrom: (1223.1.7 push)
  • Revision ID: brian@gaz-20091124020637-9gb65vj98x1arydm
MergeĀ forĀ staging.

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
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->getInsertRecord())))
 
63
  if ((error= table->cursor->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->cursor->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
}
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*) 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
130
  table->cursor->extra(HA_EXTRA_WRITE_CACHE);
135
131
  table->cursor->extra(HA_EXTRA_IGNORE_DUP_KEY);
136
 
 
137
132
  return false;
138
133
}
139
134
 
176
171
    fake_select_lex->context.first_name_resolution_table=
177
172
    fake_select_lex->get_table_list();
178
173
 
179
 
  for (Order *order= (Order *) global_parameters->order_list.first;
 
174
  for (order_st *order= (order_st *) global_parameters->order_list.first;
180
175
       order;
181
176
       order= order->next)
182
177
    order->item= &order->item_ptr;
183
178
 
184
 
  for (Order *order= (Order *)global_parameters->order_list.first;
 
179
  for (order_st *order= (order_st *)global_parameters->order_list.first;
185
180
       order;
186
181
       order=order->next)
187
182
  {
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*) NULL : (Order *)sl->order_list.first,
282
 
                               (Order*) sl->group_list.first,
 
276
                               (order_st*) 0 : (order_st *)sl->order_list.first,
 
277
                               (order_st*) sl->group_list.first,
283
278
                               sl->having,
284
279
                               sl, this);
285
280
    /* There are no * in the statement anymore (for PS) */
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
 
    result_table_list.setSchemaName((char*) "");
 
359
    result_table_list.db= (char*) "";
365
360
    result_table_list.alias= "union";
366
 
    result_table_list.setTableName((char *) "union");
 
361
    result_table_list.table_name= (char *) "union";
367
362
    result_table_list.table= table= union_result->table;
368
363
 
369
364
    session_arg->lex->current_select= lex_select_save;
400
395
  uint64_t add_rows=0;
401
396
  ha_rows examined_rows= 0;
402
397
 
403
 
  if (executed && uncacheable.none() && ! describe)
404
 
    return false;
 
398
  if (executed && !uncacheable && !describe)
 
399
    return(false);
405
400
  executed= 1;
406
401
 
407
 
  if (uncacheable.any() || ! item || ! item->assigned() || describe)
 
402
  if (uncacheable || !item || !item->assigned() || describe)
408
403
  {
409
404
    if (item)
410
405
      item->reset_value_registration();
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)
513
508
    {
514
509
      set_limit(global_parameters);
515
510
      init_prepare_fake_select_lex(session);
516
 
      Join *join= fake_select_lex->join;
 
511
      JOIN *join= fake_select_lex->join;
517
512
      if (!join)
518
513
      {
519
514
        /*
520
515
          allocate JOIN for fake select only once (prevent
521
 
          select_query automatic allocation)
522
 
          TODO: The above is nonsense. select_query() will not allocate the
 
516
          mysql_select automatic allocation)
 
517
          TODO: The above is nonsense. mysql_select() will not allocate the
523
518
          join if one already exists. There must be some other reason why we
524
519
          don't let it allocate the join. Perhaps this is because we need
525
520
          some special parameter values passed to join constructor?
526
521
        */
527
 
        if (!(fake_select_lex->join= new Join(session, item_list,
 
522
        if (!(fake_select_lex->join= new JOIN(session, item_list,
528
523
                                              fake_select_lex->options, result)))
529
524
        {
530
525
          fake_select_lex->table_list.empty();
537
532
          allocation.
538
533
        */
539
534
        fake_select_lex->item_list= item_list;
540
 
        saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
 
535
        saved_error= mysql_select(session, &fake_select_lex->ref_pointer_array,
541
536
                              &result_table_list,
542
537
                              0, item_list, NULL,
543
538
                              global_parameters->order_list.elements,
544
 
                              (Order*)global_parameters->order_list.first,
545
 
                              (Order*) NULL, NULL,
 
539
                              (order_st*)global_parameters->order_list.first,
 
540
                              (order_st*) NULL, NULL,
546
541
                              fake_select_lex->options | SELECT_NO_UNLOCK,
547
542
                              result, this, fake_select_lex);
548
543
      }
560
555
            to reset them back, we re-do all of the actions (yes it is ugly):
561
556
          */
562
557
                join->reset(session, item_list, fake_select_lex->options, result);
563
 
          saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
 
558
          saved_error= mysql_select(session, &fake_select_lex->ref_pointer_array,
564
559
                                &result_table_list,
565
560
                                0, item_list, NULL,
566
561
                                global_parameters->order_list.elements,
567
 
                                (Order*)global_parameters->order_list.first,
568
 
                                (Order*) NULL, NULL,
 
562
                                (order_st*)global_parameters->order_list.first,
 
563
                                (order_st*) NULL, NULL,
569
564
                                fake_select_lex->options | SELECT_NO_UNLOCK,
570
565
                                result, this, fake_select_lex);
571
566
        }
608
603
  {
609
604
    delete union_result;
610
605
    union_result=0; // Safety
 
606
    if (table)
 
607
      table->free_tmp_table(session);
611
608
    table= 0; // Safety
612
609
  }
613
610
 
616
613
 
617
614
  if (fake_select_lex)
618
615
  {
619
 
    Join *join;
 
616
    JOIN *join;
620
617
    if ((join= fake_select_lex->join))
621
618
    {
622
619
      join->tables_list= 0;
625
622
    error|= fake_select_lex->cleanup();
626
623
    if (fake_select_lex->order_list.elements)
627
624
    {
628
 
      Order *ord;
629
 
      for (ord= (Order*)fake_select_lex->order_list.first; ord; ord= ord->next)
 
625
      order_st *ord;
 
626
      for (ord= (order_st*)fake_select_lex->order_list.first; ord; ord= ord->next)
630
627
        (*ord->item)->cleanup();
631
628
    }
632
629
  }
735
732
    for (sl= unit->first_select(); sl; sl= sl->next_select())
736
733
      sl->cleanup_all_joins(full);
737
734
}
738
 
 
739
 
} /* namespace drizzled */