~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_union.cc

Merge Monty - Updates to pandora-build to support features of gcc 4.5.

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>
21
 
 
 
20
#include "config.h"
22
21
#include <drizzled/sql_select.h>
23
22
#include <drizzled/error.h>
24
23
#include <drizzled/item/type_holder.h>
25
24
#include <drizzled/sql_base.h>
26
25
#include <drizzled/sql_union.h>
27
 
#include <drizzled/select_union.h>
28
 
#include <drizzled/sql_lex.h>
29
 
#include <drizzled/session.h>
30
 
#include <drizzled/item/subselect.h>
31
26
 
32
 
namespace drizzled {
 
27
namespace drizzled
 
28
{
33
29
 
34
30
bool drizzle_union(Session *session, LEX *, select_result *result,
35
31
                   Select_Lex_Unit *unit, uint64_t setup_tables_done_option)
36
32
{
37
 
  bool res= unit->prepare(session, result, SELECT_NO_UNLOCK | setup_tables_done_option);
38
 
  if (not res)
 
33
  bool res;
 
34
  if (!(res= unit->prepare(session, result, SELECT_NO_UNLOCK |
 
35
                           setup_tables_done_option)))
39
36
    res= unit->exec();
40
37
  if (res)
41
 
    unit->cleanup();
42
 
  return res;
 
38
    res|= unit->cleanup();
 
39
  return(res);
43
40
}
44
41
 
45
42
 
125
122
{
126
123
  assert(table == NULL);
127
124
  tmp_table_param.init();
128
 
  tmp_table_param.field_count= column_types->size();
 
125
  tmp_table_param.field_count= column_types->elements;
129
126
 
130
127
  if (! (table= create_tmp_table(session_arg, &tmp_table_param, *column_types,
131
128
                                 (Order*) NULL, is_union_distinct, 1,
171
168
void
172
169
Select_Lex_Unit::init_prepare_fake_select_lex(Session *session_arg)
173
170
{
174
 
  session_arg->lex().current_select= fake_select_lex;
 
171
  session_arg->lex->current_select= fake_select_lex;
175
172
  fake_select_lex->table_list.link_in_list((unsigned char *)&result_table_list,
176
173
                                           (unsigned char **)
177
174
                                           &result_table_list.next_local);
197
194
bool Select_Lex_Unit::prepare(Session *session_arg, select_result *sel_result,
198
195
                              uint64_t additional_options)
199
196
{
200
 
  Select_Lex *lex_select_save= session_arg->lex().current_select;
 
197
  Select_Lex *lex_select_save= session_arg->lex->current_select;
201
198
  Select_Lex *sl, *first_sl= first_select();
202
199
  select_result *tmp_result;
203
200
  bool is_union_select;
223
220
        offset_limit_cnt= 0;
224
221
        if (result->prepare(sl->join->fields_list, this))
225
222
        {
226
 
          return true;
 
223
          return(true);
227
224
        }
228
225
        sl->join->select_options|= SELECT_DESCRIBE;
229
226
        sl->join->reinit();
230
227
      }
231
228
    }
232
 
    return false;
 
229
    return(false);
233
230
  }
234
231
  prepared= 1;
235
232
  saved_error= false;
236
233
 
237
 
  session_arg->lex().current_select= sl= first_sl;
 
234
  session_arg->lex->current_select= sl= first_sl;
238
235
  found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
239
236
  is_union_select= is_union() || fake_select_lex;
240
237
 
242
239
 
243
240
  if (is_union_select)
244
241
  {
245
 
    tmp_result= union_result= new select_union;
 
242
    if (!(tmp_result= union_result= new select_union))
 
243
      goto err;
246
244
    if (describe)
247
245
      tmp_result= sel_result;
248
246
  }
268
266
    if (!join)
269
267
      goto err;
270
268
 
271
 
    session_arg->lex().current_select= sl;
 
269
    session_arg->lex->current_select= sl;
272
270
 
273
271
    can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit);
274
272
 
277
275
                               sl->with_wild,
278
276
                               sl->where,
279
277
                               (can_skip_order_by ? 0 :
280
 
                                sl->order_list.size()) +
281
 
                               sl->group_list.size(),
 
278
                                sl->order_list.elements) +
 
279
                               sl->group_list.elements,
282
280
                               can_skip_order_by ?
283
281
                               (Order*) NULL : (Order *)sl->order_list.first,
284
282
                               (Order*) sl->group_list.first,
304
302
        field object without table.
305
303
      */
306
304
      assert(!empty_table);
307
 
      empty_table= (Table*) session->mem.calloc(sizeof(Table));
308
 
      types.clear();
309
 
      List<Item>::iterator it(sl->item_list.begin());
310
 
      while (Item* item_tmp= it++)
 
305
      empty_table= (Table*) session->calloc(sizeof(Table));
 
306
      types.empty();
 
307
      List_iterator_fast<Item> it(sl->item_list);
 
308
      Item *item_tmp;
 
309
      while ((item_tmp= it++))
311
310
      {
312
311
        /* Error's in 'new' will be detected after loop */
313
312
        types.push_back(new Item_type_holder(session_arg, item_tmp));
318
317
    }
319
318
    else
320
319
    {
321
 
      if (types.size() != sl->item_list.size())
 
320
      if (types.elements != sl->item_list.elements)
322
321
      {
323
322
        my_message(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT,
324
323
                   ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT),MYF(0));
325
324
        goto err;
326
325
      }
327
 
      List<Item>::iterator it(sl->item_list.begin());
328
 
      List<Item>::iterator tp(types.begin());
 
326
      List_iterator_fast<Item> it(sl->item_list);
 
327
      List_iterator_fast<Item> tp(types);
329
328
      Item *type, *item_tmp;
330
329
      while ((type= tp++, item_tmp= it++))
331
330
      {
332
331
        if (((Item_type_holder*)type)->join_types(session_arg, item_tmp))
333
 
          return true;
 
332
          return(true);
334
333
      }
335
334
    }
336
335
  }
341
340
      Check that it was possible to aggregate
342
341
      all collations together for UNION.
343
342
    */
344
 
    List<Item>::iterator tp(types.begin());
 
343
    List_iterator_fast<Item> tp(types);
345
344
    Item *type;
346
345
    uint64_t create_options;
347
346
 
355
354
      }
356
355
    }
357
356
 
358
 
    create_options= first_sl->options | session_arg->options | TMP_TABLE_ALL_COLUMNS;
 
357
    create_options= (first_sl->options | session_arg->options |
 
358
                     TMP_TABLE_ALL_COLUMNS);
359
359
 
360
 
    if (union_result->create_result_table(session, &types, test(union_distinct), create_options, ""))
 
360
    if (union_result->create_result_table(session, &types, test(union_distinct),
 
361
                                          create_options, ""))
361
362
      goto err;
362
363
    memset(&result_table_list, 0, sizeof(result_table_list));
363
 
    result_table_list.setSchemaName("");
 
364
    result_table_list.setSchemaName((char*) "");
364
365
    result_table_list.alias= "union";
365
 
    result_table_list.setTableName("union");
 
366
    result_table_list.setTableName((char *) "union");
366
367
    result_table_list.table= table= union_result->table;
367
368
 
368
 
    session_arg->lex().current_select= lex_select_save;
369
 
    if (item_list.is_empty())
370
 
      table->fill_item_list(item_list);
 
369
    session_arg->lex->current_select= lex_select_save;
 
370
    if (!item_list.elements)
 
371
    {
 
372
      saved_error= table->fill_item_list(&item_list);
 
373
      if (saved_error)
 
374
        goto err;
 
375
    }
371
376
    else
372
377
    {
373
378
      /*
374
379
        We're in execution of a prepared statement or stored procedure:
375
380
        reset field items to point at fields from the created temporary table.
376
381
      */
377
 
      assert(false);
 
382
      assert(1);
378
383
    }
379
384
  }
380
385
 
381
 
  session_arg->lex().current_select= lex_select_save;
 
386
  session_arg->lex->current_select= lex_select_save;
382
387
 
383
388
  return(saved_error || session_arg->is_fatal_error);
384
389
 
385
390
err:
386
 
  session_arg->lex().current_select= lex_select_save;
387
 
  return true;
 
391
  session_arg->lex->current_select= lex_select_save;
 
392
  return(true);
388
393
}
389
394
 
390
395
 
391
396
bool Select_Lex_Unit::exec()
392
397
{
393
 
  Select_Lex *lex_select_save= session->lex().current_select;
 
398
  Select_Lex *lex_select_save= session->lex->current_select;
394
399
  Select_Lex *select_cursor=first_select();
395
400
  uint64_t add_rows=0;
396
401
  ha_rows examined_rows= 0;
420
425
    for (Select_Lex *sl= select_cursor; sl; sl= sl->next_select())
421
426
    {
422
427
      ha_rows records_at_start= 0;
423
 
      session->lex().current_select= sl;
 
428
      session->lex->current_select= sl;
424
429
 
425
430
      if (optimized)
426
431
        saved_error= sl->join->reinit();
456
461
        if (sl == union_distinct)
457
462
        {
458
463
          if (table->cursor->ha_disable_indexes(HA_KEY_SWITCH_ALL))
459
 
            return true;
 
464
            return(true);
460
465
          table->no_keyread=1;
461
466
        }
462
467
        saved_error= sl->join->error;
468
473
          examined_rows+= session->examined_row_count;
469
474
          if (union_result->flush())
470
475
          {
471
 
            session->lex().current_select= lex_select_save;
472
 
            return 1;
 
476
            session->lex->current_select= lex_select_save;
 
477
            return(1);
473
478
          }
474
479
        }
475
480
      }
476
481
      if (saved_error)
477
482
      {
478
 
        session->lex().current_select= lex_select_save;
 
483
        session->lex->current_select= lex_select_save;
479
484
        return(saved_error);
480
485
      }
481
486
      /* Needed for the following test and for records_at_start in next loop */
483
488
      if (error)
484
489
      {
485
490
        table->print_error(error, MYF(0));
486
 
        return 1;
 
491
        return(1);
487
492
      }
488
493
      if (found_rows_for_union && !sl->braces &&
489
494
          select_limit_cnt != HA_POS_ERROR)
513
518
      {
514
519
        /*
515
520
          allocate JOIN for fake select only once (prevent
516
 
          select_query automatic allocation)
517
 
          TODO: The above is nonsense. select_query() will not allocate the
 
521
          mysql_select automatic allocation)
 
522
          TODO: The above is nonsense. mysql_select() will not allocate the
518
523
          join if one already exists. There must be some other reason why we
519
524
          don't let it allocate the join. Perhaps this is because we need
520
525
          some special parameter values passed to join constructor?
521
526
        */
522
 
        fake_select_lex->join= new Join(session, item_list, fake_select_lex->options, result);
523
 
  fake_select_lex->join->no_const_tables= true;
 
527
        if (!(fake_select_lex->join= new Join(session, item_list,
 
528
                                              fake_select_lex->options, result)))
 
529
        {
 
530
          fake_select_lex->table_list.empty();
 
531
          return(true);
 
532
        }
 
533
        fake_select_lex->join->no_const_tables= true;
524
534
 
525
535
        /*
526
536
          Fake Select_Lex should have item list for correctref_array
527
537
          allocation.
528
538
        */
529
539
        fake_select_lex->item_list= item_list;
530
 
        saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
 
540
        saved_error= mysql_select(session, &fake_select_lex->ref_pointer_array,
531
541
                              &result_table_list,
532
542
                              0, item_list, NULL,
533
 
                              global_parameters->order_list.size(),
 
543
                              global_parameters->order_list.elements,
534
544
                              (Order*)global_parameters->order_list.first,
535
545
                              (Order*) NULL, NULL,
536
546
                              fake_select_lex->options | SELECT_NO_UNLOCK,
550
560
            to reset them back, we re-do all of the actions (yes it is ugly):
551
561
          */
552
562
                join->reset(session, item_list, fake_select_lex->options, result);
553
 
          saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
 
563
          saved_error= mysql_select(session, &fake_select_lex->ref_pointer_array,
554
564
                                &result_table_list,
555
565
                                0, item_list, NULL,
556
 
                                global_parameters->order_list.size(),
 
566
                                global_parameters->order_list.elements,
557
567
                                (Order*)global_parameters->order_list.first,
558
568
                                (Order*) NULL, NULL,
559
569
                                fake_select_lex->options | SELECT_NO_UNLOCK,
567
577
        }
568
578
      }
569
579
 
570
 
      fake_select_lex->table_list.clear();
 
580
      fake_select_lex->table_list.empty();
571
581
      if (!saved_error)
572
582
      {
573
583
        session->limit_found_rows = (uint64_t)table->cursor->stats.records + add_rows;
579
589
      */
580
590
    }
581
591
  }
582
 
  session->lex().current_select= lex_select_save;
 
592
  session->lex->current_select= lex_select_save;
583
593
  return(saved_error);
584
594
}
585
595
 
590
600
 
591
601
  if (cleaned)
592
602
  {
593
 
    return false;
 
603
    return(false);
594
604
  }
595
605
  cleaned= 1;
596
606
 
597
607
  if (union_result)
598
608
  {
599
 
    safe_delete(union_result);
 
609
    delete union_result;
 
610
    union_result=0; // Safety
600
611
    table= 0; // Safety
601
612
  }
602
613
 
612
623
      join->tables= 0;
613
624
    }
614
625
    error|= fake_select_lex->cleanup();
615
 
    if (fake_select_lex->order_list.size())
 
626
    if (fake_select_lex->order_list.elements)
616
627
    {
617
628
      Order *ord;
618
629
      for (ord= (Order*)fake_select_lex->order_list.first; ord; ord= ord->next)
698
709
  {
699
710
    assert((Select_Lex*)join->select_lex == this);
700
711
    error= join->destroy();
701
 
    safe_delete(join);
 
712
    delete join;
 
713
    join= 0;
702
714
  }
703
715
  for (Select_Lex_Unit *lex_unit= first_inner_unit(); lex_unit ;
704
716
       lex_unit= lex_unit->next_unit())
705
717
  {
706
718
    error= (bool) ((uint32_t) error | (uint32_t) lex_unit->cleanup());
707
719
  }
708
 
  non_agg_fields.clear();
709
 
  inner_refs_list.clear();
 
720
  non_agg_fields.empty();
 
721
  inner_refs_list.empty();
710
722
  return(error);
711
723
}
712
724