~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_union.cc

  • Committer: Paul McCullagh
  • Date: 2010-09-16 15:26:41 UTC
  • mto: (1771.3.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 1785.
  • Revision ID: paul.mccullagh@primebase.org-20100916152641-9mwb1hga0qwz41nu
Merged with 1.1 trunk

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>
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
26
 
31
27
namespace drizzled
32
28
{
129
125
  tmp_table_param.field_count= column_types->elements;
130
126
 
131
127
  if (! (table= create_tmp_table(session_arg, &tmp_table_param, *column_types,
132
 
                                 (Order*) NULL, is_union_distinct, 1,
 
128
                                 (order_st*) NULL, is_union_distinct, 1,
133
129
                                 options, HA_POS_ERROR, (char*) table_alias)))
134
130
  {
135
131
    return true;
172
168
void
173
169
Select_Lex_Unit::init_prepare_fake_select_lex(Session *session_arg)
174
170
{
175
 
  session_arg->getLex()->current_select= fake_select_lex;
 
171
  session_arg->lex->current_select= fake_select_lex;
176
172
  fake_select_lex->table_list.link_in_list((unsigned char *)&result_table_list,
177
173
                                           (unsigned char **)
178
174
                                           &result_table_list.next_local);
180
176
    fake_select_lex->context.first_name_resolution_table=
181
177
    fake_select_lex->get_table_list();
182
178
 
183
 
  for (Order *order= (Order *) global_parameters->order_list.first;
 
179
  for (order_st *order= (order_st *) global_parameters->order_list.first;
184
180
       order;
185
181
       order= order->next)
186
182
    order->item= &order->item_ptr;
187
183
 
188
 
  for (Order *order= (Order *)global_parameters->order_list.first;
 
184
  for (order_st *order= (order_st *)global_parameters->order_list.first;
189
185
       order;
190
186
       order=order->next)
191
187
  {
198
194
bool Select_Lex_Unit::prepare(Session *session_arg, select_result *sel_result,
199
195
                              uint64_t additional_options)
200
196
{
201
 
  Select_Lex *lex_select_save= session_arg->getLex()->current_select;
 
197
  Select_Lex *lex_select_save= session_arg->lex->current_select;
202
198
  Select_Lex *sl, *first_sl= first_select();
203
199
  select_result *tmp_result;
204
200
  bool is_union_select;
235
231
  prepared= 1;
236
232
  saved_error= false;
237
233
 
238
 
  session_arg->getLex()->current_select= sl= first_sl;
 
234
  session_arg->lex->current_select= sl= first_sl;
239
235
  found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
240
236
  is_union_select= is_union() || fake_select_lex;
241
237
 
270
266
    if (!join)
271
267
      goto err;
272
268
 
273
 
    session_arg->getLex()->current_select= sl;
 
269
    session_arg->lex->current_select= sl;
274
270
 
275
271
    can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit);
276
272
 
282
278
                                sl->order_list.elements) +
283
279
                               sl->group_list.elements,
284
280
                               can_skip_order_by ?
285
 
                               (Order*) NULL : (Order *)sl->order_list.first,
286
 
                               (Order*) sl->group_list.first,
 
281
                               (order_st*) NULL : (order_st *)sl->order_list.first,
 
282
                               (order_st*) sl->group_list.first,
287
283
                               sl->having,
288
284
                               sl, this);
289
285
    /* There are no * in the statement anymore (for PS) */
307
303
      */
308
304
      assert(!empty_table);
309
305
      empty_table= (Table*) session->calloc(sizeof(Table));
310
 
      types.clear();
311
 
      List<Item>::iterator it(sl->item_list.begin());
 
306
      types.empty();
 
307
      List_iterator_fast<Item> it(sl->item_list);
312
308
      Item *item_tmp;
313
309
      while ((item_tmp= it++))
314
310
      {
327
323
                   ER(ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT),MYF(0));
328
324
        goto err;
329
325
      }
330
 
      List<Item>::iterator it(sl->item_list.begin());
331
 
      List<Item>::iterator tp(types.begin());
 
326
      List_iterator_fast<Item> it(sl->item_list);
 
327
      List_iterator_fast<Item> tp(types);
332
328
      Item *type, *item_tmp;
333
329
      while ((type= tp++, item_tmp= it++))
334
330
      {
344
340
      Check that it was possible to aggregate
345
341
      all collations together for UNION.
346
342
    */
347
 
    List<Item>::iterator tp(types.begin());
 
343
    List_iterator_fast<Item> tp(types);
348
344
    Item *type;
349
345
    uint64_t create_options;
350
346
 
365
361
                                          create_options, ""))
366
362
      goto err;
367
363
    memset(&result_table_list, 0, sizeof(result_table_list));
368
 
    result_table_list.setSchemaName((char*) "");
 
364
    result_table_list.db= (char*) "";
369
365
    result_table_list.alias= "union";
370
 
    result_table_list.setTableName((char *) "union");
 
366
    result_table_list.table_name= (char *) "union";
371
367
    result_table_list.table= table= union_result->table;
372
368
 
373
 
    session_arg->getLex()->current_select= lex_select_save;
 
369
    session_arg->lex->current_select= lex_select_save;
374
370
    if (!item_list.elements)
375
371
    {
376
372
      saved_error= table->fill_item_list(&item_list);
387
383
    }
388
384
  }
389
385
 
390
 
  session_arg->getLex()->current_select= lex_select_save;
 
386
  session_arg->lex->current_select= lex_select_save;
391
387
 
392
388
  return(saved_error || session_arg->is_fatal_error);
393
389
 
394
390
err:
395
 
  session_arg->getLex()->current_select= lex_select_save;
 
391
  session_arg->lex->current_select= lex_select_save;
396
392
  return(true);
397
393
}
398
394
 
399
395
 
400
396
bool Select_Lex_Unit::exec()
401
397
{
402
 
  Select_Lex *lex_select_save= session->getLex()->current_select;
 
398
  Select_Lex *lex_select_save= session->lex->current_select;
403
399
  Select_Lex *select_cursor=first_select();
404
400
  uint64_t add_rows=0;
405
401
  ha_rows examined_rows= 0;
406
402
 
407
 
  if (executed && uncacheable.none() && ! describe)
408
 
    return false;
 
403
  if (executed && !uncacheable && !describe)
 
404
    return(false);
409
405
  executed= 1;
410
406
 
411
 
  if (uncacheable.any() || ! item || ! item->assigned() || describe)
 
407
  if (uncacheable || !item || !item->assigned() || describe)
412
408
  {
413
409
    if (item)
414
410
      item->reset_value_registration();
429
425
    for (Select_Lex *sl= select_cursor; sl; sl= sl->next_select())
430
426
    {
431
427
      ha_rows records_at_start= 0;
432
 
      session->getLex()->current_select= sl;
 
428
      session->lex->current_select= sl;
433
429
 
434
430
      if (optimized)
435
431
        saved_error= sl->join->reinit();
477
473
          examined_rows+= session->examined_row_count;
478
474
          if (union_result->flush())
479
475
          {
480
 
            session->getLex()->current_select= lex_select_save;
 
476
            session->lex->current_select= lex_select_save;
481
477
            return(1);
482
478
          }
483
479
        }
484
480
      }
485
481
      if (saved_error)
486
482
      {
487
 
        session->getLex()->current_select= lex_select_save;
 
483
        session->lex->current_select= lex_select_save;
488
484
        return(saved_error);
489
485
      }
490
486
      /* Needed for the following test and for records_at_start in next loop */
522
518
      {
523
519
        /*
524
520
          allocate JOIN for fake select only once (prevent
525
 
          select_query automatic allocation)
526
 
          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
527
523
          join if one already exists. There must be some other reason why we
528
524
          don't let it allocate the join. Perhaps this is because we need
529
525
          some special parameter values passed to join constructor?
531
527
        if (!(fake_select_lex->join= new Join(session, item_list,
532
528
                                              fake_select_lex->options, result)))
533
529
        {
534
 
          fake_select_lex->table_list.clear();
 
530
          fake_select_lex->table_list.empty();
535
531
          return(true);
536
532
        }
537
533
        fake_select_lex->join->no_const_tables= true;
541
537
          allocation.
542
538
        */
543
539
        fake_select_lex->item_list= item_list;
544
 
        saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
 
540
        saved_error= mysql_select(session, &fake_select_lex->ref_pointer_array,
545
541
                              &result_table_list,
546
542
                              0, item_list, NULL,
547
543
                              global_parameters->order_list.elements,
548
 
                              (Order*)global_parameters->order_list.first,
549
 
                              (Order*) NULL, NULL,
 
544
                              (order_st*)global_parameters->order_list.first,
 
545
                              (order_st*) NULL, NULL,
550
546
                              fake_select_lex->options | SELECT_NO_UNLOCK,
551
547
                              result, this, fake_select_lex);
552
548
      }
564
560
            to reset them back, we re-do all of the actions (yes it is ugly):
565
561
          */
566
562
                join->reset(session, item_list, fake_select_lex->options, result);
567
 
          saved_error= select_query(session, &fake_select_lex->ref_pointer_array,
 
563
          saved_error= mysql_select(session, &fake_select_lex->ref_pointer_array,
568
564
                                &result_table_list,
569
565
                                0, item_list, NULL,
570
566
                                global_parameters->order_list.elements,
571
 
                                (Order*)global_parameters->order_list.first,
572
 
                                (Order*) NULL, NULL,
 
567
                                (order_st*)global_parameters->order_list.first,
 
568
                                (order_st*) NULL, NULL,
573
569
                                fake_select_lex->options | SELECT_NO_UNLOCK,
574
570
                                result, this, fake_select_lex);
575
571
        }
581
577
        }
582
578
      }
583
579
 
584
 
      fake_select_lex->table_list.clear();
 
580
      fake_select_lex->table_list.empty();
585
581
      if (!saved_error)
586
582
      {
587
583
        session->limit_found_rows = (uint64_t)table->cursor->stats.records + add_rows;
593
589
      */
594
590
    }
595
591
  }
596
 
  session->getLex()->current_select= lex_select_save;
 
592
  session->lex->current_select= lex_select_save;
597
593
  return(saved_error);
598
594
}
599
595
 
610
606
 
611
607
  if (union_result)
612
608
  {
613
 
    safe_delete(union_result);
 
609
    delete union_result;
 
610
    union_result=0; // Safety
614
611
    table= 0; // Safety
615
612
  }
616
613
 
628
625
    error|= fake_select_lex->cleanup();
629
626
    if (fake_select_lex->order_list.elements)
630
627
    {
631
 
      Order *ord;
632
 
      for (ord= (Order*)fake_select_lex->order_list.first; ord; ord= ord->next)
 
628
      order_st *ord;
 
629
      for (ord= (order_st*)fake_select_lex->order_list.first; ord; ord= ord->next)
633
630
        (*ord->item)->cleanup();
634
631
    }
635
632
  }
712
709
  {
713
710
    assert((Select_Lex*)join->select_lex == this);
714
711
    error= join->destroy();
715
 
    safe_delete(join);
 
712
    delete join;
 
713
    join= 0;
716
714
  }
717
715
  for (Select_Lex_Unit *lex_unit= first_inner_unit(); lex_unit ;
718
716
       lex_unit= lex_unit->next_unit())
719
717
  {
720
718
    error= (bool) ((uint32_t) error | (uint32_t) lex_unit->cleanup());
721
719
  }
722
 
  non_agg_fields.clear();
723
 
  inner_refs_list.clear();
 
720
  non_agg_fields.empty();
 
721
  inner_refs_list.empty();
724
722
  return(error);
725
723
}
726
724