~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_union.cc

  • Committer: Brian Aker
  • Date: 2008-10-20 04:28:21 UTC
  • mto: (492.3.21 drizzle-clean-code)
  • mto: This revision was merged to the branch mainline in revision 530.
  • Revision ID: brian@tangent.org-20081020042821-rqqdrccuu8195k3y
Second pass of thd cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
#include <drizzled/sql_select.h>
22
22
#include <drizzled/drizzled_error_messages.h>
23
23
 
24
 
bool mysql_union(Session *thd,
 
24
bool mysql_union(Session *session,
25
25
                 LEX *lex __attribute__((unused)),
26
26
                 select_result *result,
27
27
                 SELECT_LEX_UNIT *unit, ulong setup_tables_done_option)
28
28
{
29
29
  bool res;
30
 
  if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK |
 
30
  if (!(res= unit->prepare(session, result, SELECT_NO_UNLOCK |
31
31
                           setup_tables_done_option)))
32
32
    res= unit->exec();
33
33
  if (res)
56
56
    unit->offset_limit_cnt--;
57
57
    return 0;
58
58
  }
59
 
  fill_record(thd, table->field, values, 1);
60
 
  if (thd->is_error())
 
59
  fill_record(session, table->field, values, 1);
 
60
  if (session->is_error())
61
61
    return 1;
62
62
 
63
63
  if ((error= table->file->ha_write_row(table->record[0])))
64
64
  {
65
65
    /* create_myisam_from_heap will generate error if needed */
66
66
    if (table->file->is_fatal_error(error, HA_CHECK_DUP) &&
67
 
        create_myisam_from_heap(thd, table, tmp_table_param.start_recinfo, 
 
67
        create_myisam_from_heap(session, table, tmp_table_param.start_recinfo, 
68
68
                                &tmp_table_param.recinfo, error, 1))
69
69
      return 1;
70
70
  }
94
94
 
95
95
  SYNOPSIS
96
96
    select_union::create_result_table()
97
 
      thd                thread handle
 
97
      session                thread handle
98
98
      column_types       a list of items used to define columns of the
99
99
                         temporary table
100
100
      is_union_distinct  if set, the temporary table will eliminate
113
113
*/
114
114
 
115
115
bool
116
 
select_union::create_result_table(Session *thd_arg, List<Item> *column_types,
 
116
select_union::create_result_table(Session *session_arg, List<Item> *column_types,
117
117
                                  bool is_union_distinct, uint64_t options,
118
118
                                  const char *table_alias,
119
119
                                  bool bit_fields_as_long)
123
123
  tmp_table_param.field_count= column_types->elements;
124
124
  tmp_table_param.bit_fields_as_long= bit_fields_as_long;
125
125
 
126
 
  if (! (table= create_tmp_table(thd_arg, &tmp_table_param, *column_types,
 
126
  if (! (table= create_tmp_table(session_arg, &tmp_table_param, *column_types,
127
127
                                 (order_st*) 0, is_union_distinct, 1,
128
128
                                 options, HA_POS_ERROR, (char*) table_alias)))
129
129
    return true;
154
154
 
155
155
  SYNOPSIS
156
156
    st_select_lex_unit::init_prepare_fake_select_lex()
157
 
    thd         - thread handler
 
157
    session             - thread handler
158
158
 
159
159
  RETURN
160
160
    options of SELECT
161
161
*/
162
162
 
163
163
void
164
 
st_select_lex_unit::init_prepare_fake_select_lex(Session *thd_arg) 
 
164
st_select_lex_unit::init_prepare_fake_select_lex(Session *session_arg) 
165
165
{
166
 
  thd_arg->lex->current_select= fake_select_lex;
 
166
  session_arg->lex->current_select= fake_select_lex;
167
167
  fake_select_lex->table_list.link_in_list((unsigned char *)&result_table_list,
168
168
                                           (unsigned char **)
169
169
                                           &result_table_list.next_local);
186
186
}
187
187
 
188
188
 
189
 
bool st_select_lex_unit::prepare(Session *thd_arg, select_result *sel_result,
 
189
bool st_select_lex_unit::prepare(Session *session_arg, select_result *sel_result,
190
190
                                 uint32_t additional_options)
191
191
{
192
 
  SELECT_LEX *lex_select_save= thd_arg->lex->current_select;
 
192
  SELECT_LEX *lex_select_save= session_arg->lex->current_select;
193
193
  SELECT_LEX *sl, *first_sl= first_select();
194
194
  select_result *tmp_result;
195
195
  bool is_union_select;
226
226
  prepared= 1;
227
227
  saved_error= false;
228
228
  
229
 
  thd_arg->lex->current_select= sl= first_sl;
 
229
  session_arg->lex->current_select= sl= first_sl;
230
230
  found_rows_for_union= first_sl->options & OPTION_FOUND_ROWS;
231
231
  is_union_select= is_union() || fake_select_lex;
232
232
 
248
248
  {
249
249
    bool can_skip_order_by;
250
250
    sl->options|=  SELECT_NO_UNLOCK;
251
 
    JOIN *join= new JOIN(thd_arg, sl->item_list, 
252
 
                         sl->options | thd_arg->options | additional_options,
 
251
    JOIN *join= new JOIN(session_arg, sl->item_list, 
 
252
                         sl->options | session_arg->options | additional_options,
253
253
                         tmp_result);
254
254
    /*
255
255
      setup_tables_done_option should be set only for very first SELECT,
261
261
    if (!join)
262
262
      goto err;
263
263
 
264
 
    thd_arg->lex->current_select= sl;
 
264
    session_arg->lex->current_select= sl;
265
265
 
266
266
    can_skip_order_by= is_union_select && !(sl->braces && sl->explicit_limit);
267
267
 
277
277
                               (order_st*) sl->group_list.first,
278
278
                               sl->having,
279
279
                               (is_union_select ? (order_st*) 0 :
280
 
                                (order_st*) thd_arg->lex->proc_list.first),
 
280
                                (order_st*) session_arg->lex->proc_list.first),
281
281
                               sl, this);
282
282
    /* There are no * in the statement anymore (for PS) */
283
283
    sl->with_wild= 0;
284
284
 
285
 
    if (saved_error || (saved_error= thd_arg->is_fatal_error))
 
285
    if (saved_error || (saved_error= session_arg->is_fatal_error))
286
286
      goto err;
287
287
    /*
288
288
      Use items list of underlaid select for derived tables to preserve
299
299
        field object without table.
300
300
      */
301
301
      assert(!empty_table);
302
 
      empty_table= (Table*) thd->calloc(sizeof(Table));
 
302
      empty_table= (Table*) session->calloc(sizeof(Table));
303
303
      types.empty();
304
304
      List_iterator_fast<Item> it(sl->item_list);
305
305
      Item *item_tmp;
306
306
      while ((item_tmp= it++))
307
307
      {
308
308
        /* Error's in 'new' will be detected after loop */
309
 
        types.push_back(new Item_type_holder(thd_arg, item_tmp));
 
309
        types.push_back(new Item_type_holder(session_arg, item_tmp));
310
310
      }
311
311
 
312
 
      if (thd_arg->is_fatal_error)
 
312
      if (session_arg->is_fatal_error)
313
313
        goto err; // out of memory
314
314
    }
315
315
    else
325
325
      Item *type, *item_tmp;
326
326
      while ((type= tp++, item_tmp= it++))
327
327
      {
328
 
        if (((Item_type_holder*)type)->join_types(thd_arg, item_tmp))
 
328
        if (((Item_type_holder*)type)->join_types(session_arg, item_tmp))
329
329
          return(true);
330
330
      }
331
331
    }
351
351
      }
352
352
    }
353
353
    
354
 
    create_options= (first_sl->options | thd_arg->options |
 
354
    create_options= (first_sl->options | session_arg->options |
355
355
                     TMP_TABLE_ALL_COLUMNS);
356
356
 
357
 
    if (union_result->create_result_table(thd, &types, test(union_distinct),
 
357
    if (union_result->create_result_table(session, &types, test(union_distinct),
358
358
                                          create_options, "", false))
359
359
      goto err;
360
360
    memset(&result_table_list, 0, sizeof(result_table_list));
362
362
    result_table_list.table_name= result_table_list.alias= (char*) "union";
363
363
    result_table_list.table= table= union_result->table;
364
364
 
365
 
    thd_arg->lex->current_select= lex_select_save;
 
365
    session_arg->lex->current_select= lex_select_save;
366
366
    if (!item_list.elements)
367
367
    {
368
368
      saved_error= table->fill_item_list(&item_list);
379
379
    }
380
380
  }
381
381
 
382
 
  thd_arg->lex->current_select= lex_select_save;
 
382
  session_arg->lex->current_select= lex_select_save;
383
383
 
384
 
  return(saved_error || thd_arg->is_fatal_error);
 
384
  return(saved_error || session_arg->is_fatal_error);
385
385
 
386
386
err:
387
 
  thd_arg->lex->current_select= lex_select_save;
 
387
  session_arg->lex->current_select= lex_select_save;
388
388
  return(true);
389
389
}
390
390
 
391
391
 
392
392
bool st_select_lex_unit::exec()
393
393
{
394
 
  SELECT_LEX *lex_select_save= thd->lex->current_select;
 
394
  SELECT_LEX *lex_select_save= session->lex->current_select;
395
395
  SELECT_LEX *select_cursor=first_select();
396
396
  uint64_t add_rows=0;
397
397
  ha_rows examined_rows= 0;
421
421
    for (SELECT_LEX *sl= select_cursor; sl; sl= sl->next_select())
422
422
    {
423
423
      ha_rows records_at_start= 0;
424
 
      thd->lex->current_select= sl;
 
424
      session->lex->current_select= sl;
425
425
 
426
426
      if (optimized)
427
427
        saved_error= sl->join->reinit();
471
471
                                    0);
472
472
        if (!saved_error)
473
473
        {
474
 
          examined_rows+= thd->examined_row_count;
 
474
          examined_rows+= session->examined_row_count;
475
475
          if (union_result->flush())
476
476
          {
477
 
            thd->lex->current_select= lex_select_save;
 
477
            session->lex->current_select= lex_select_save;
478
478
            return(1);
479
479
          }
480
480
        }
481
481
      }
482
482
      if (saved_error)
483
483
      {
484
 
        thd->lex->current_select= lex_select_save;
 
484
        session->lex->current_select= lex_select_save;
485
485
        return(saved_error);
486
486
      }
487
487
      /* Needed for the following test and for records_at_start in next loop */
500
500
          We get this from the difference of between total number of possible
501
501
          rows and actual rows added to the temporary table.
502
502
        */
503
 
        add_rows+= (uint64_t) (thd->limit_found_rows - (uint64_t)
 
503
        add_rows+= (uint64_t) (session->limit_found_rows - (uint64_t)
504
504
                              ((table->file->stats.records -  records_at_start)));
505
505
      }
506
506
    }
510
510
  /* Send result to 'result' */
511
511
  saved_error= true;
512
512
  {
513
 
    if (!thd->is_fatal_error)                           // Check if EOM
 
513
    if (!session->is_fatal_error)                               // Check if EOM
514
514
    {
515
515
      set_limit(global_parameters);
516
 
      init_prepare_fake_select_lex(thd);
 
516
      init_prepare_fake_select_lex(session);
517
517
      JOIN *join= fake_select_lex->join;
518
518
      if (!join)
519
519
      {
525
525
          don't let it allocate the join. Perhaps this is because we need
526
526
          some special parameter values passed to join constructor?
527
527
        */
528
 
        if (!(fake_select_lex->join= new JOIN(thd, item_list,
 
528
        if (!(fake_select_lex->join= new JOIN(session, item_list,
529
529
                                              fake_select_lex->options, result)))
530
530
        {
531
531
          fake_select_lex->table_list.empty();
538
538
          allocation.
539
539
        */
540
540
        fake_select_lex->item_list= item_list;
541
 
        saved_error= mysql_select(thd, &fake_select_lex->ref_pointer_array,
 
541
        saved_error= mysql_select(session, &fake_select_lex->ref_pointer_array,
542
542
                              &result_table_list,
543
543
                              0, item_list, NULL,
544
544
                              global_parameters->order_list.elements,
560
560
            subquery execution rather than EXPLAIN line production. In order 
561
561
            to reset them back, we re-do all of the actions (yes it is ugly):
562
562
          */
563
 
          join->init(thd, item_list, fake_select_lex->options, result);
564
 
          saved_error= mysql_select(thd, &fake_select_lex->ref_pointer_array,
 
563
          join->init(session, item_list, fake_select_lex->options, result);
 
564
          saved_error= mysql_select(session, &fake_select_lex->ref_pointer_array,
565
565
                                &result_table_list,
566
566
                                0, item_list, NULL,
567
567
                                global_parameters->order_list.elements,
581
581
      fake_select_lex->table_list.empty();
582
582
      if (!saved_error)
583
583
      {
584
 
        thd->limit_found_rows = (uint64_t)table->file->stats.records + add_rows;
585
 
        thd->examined_row_count+= examined_rows;
 
584
        session->limit_found_rows = (uint64_t)table->file->stats.records + add_rows;
 
585
        session->examined_row_count+= examined_rows;
586
586
      }
587
587
      /*
588
588
        Mark for slow query log if any of the union parts didn't use
590
590
      */
591
591
    }
592
592
  }
593
 
  thd->lex->current_select= lex_select_save;
 
593
  session->lex->current_select= lex_select_save;
594
594
  return(saved_error);
595
595
}
596
596
 
610
610
    delete union_result;
611
611
    union_result=0; // Safety
612
612
    if (table)
613
 
      table->free_tmp_table(thd);
 
613
      table->free_tmp_table(session);
614
614
    table= 0; // Safety
615
615
  }
616
616