~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Lee Bieber
  • Date: 2011-01-12 02:31:03 UTC
  • mfrom: (2068.7.5 session-fix)
  • mto: This revision was merged to the branch mainline in revision 2076.
  • Revision ID: kalebral@gmail.com-20110112023103-nmz26cv1j32jc6n3
Merge Brian - fix bug 527084 - DROP TABLE: getTableDefiniton returns EEXIST but doDropTable returns ENOENT leads to SIGSEGV

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
#include "drizzled/records.h"
53
53
#include "drizzled/internal/iocache.h"
54
54
#include "drizzled/drizzled.h"
55
 
#include "drizzled/plugin/storage_engine.h"
56
55
 
57
56
#include "drizzled/sql_union.h"
58
57
#include "drizzled/optimizer/key_field.h"
573
572
  sz= sizeof(optimizer::KeyField) *
574
573
      (((session->lex->current_select->cond_count+1)*2 +
575
574
        session->lex->current_select->between_count)*m+1);
576
 
  if (! (key_fields= (optimizer::KeyField*) session->getMemRoot()->allocate(sz)))
 
575
  if (! (key_fields= (optimizer::KeyField*) session->alloc(sz)))
577
576
    return true;
578
577
  and_level= 0;
579
578
  field= end= key_fields;
1005
1004
  j->ref.key_length=length;
1006
1005
  j->ref.key=(int) key;
1007
1006
  if (!(j->ref.key_buff= (unsigned char*) session->calloc(ALIGN_SIZE(length)*2)) ||
1008
 
      !(j->ref.key_copy= (StoredKey**) session->getMemRoot()->allocate((sizeof(StoredKey*) *
 
1007
      !(j->ref.key_copy= (StoredKey**) session->alloc((sizeof(StoredKey*) *
1009
1008
               (keyparts+1)))) ||
1010
 
      !(j->ref.items=    (Item**) session->getMemRoot()->allocate(sizeof(Item*)*keyparts)) ||
1011
 
      !(j->ref.cond_guards= (bool**) session->getMemRoot()->allocate(sizeof(uint*)*keyparts)))
 
1009
      !(j->ref.items=    (Item**) session->alloc(sizeof(Item*)*keyparts)) ||
 
1010
      !(j->ref.cond_guards= (bool**) session->alloc(sizeof(uint*)*keyparts)))
1012
1011
  {
1013
1012
    return(true);
1014
1013
  }
2308
2307
  @param cond       condition whose multiple equalities are to be checked
2309
2308
  @param table      constant table that has been read
2310
2309
*/
2311
 
void update_const_equal_items(COND *cond, JoinTable *tab)
 
2310
static void update_const_equal_items(COND *cond, JoinTable *tab)
2312
2311
{
2313
2312
  if (!(cond->used_tables() & tab->table->map))
2314
2313
    return;
2607
2606
         position:
2608
2607
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
2609
2608
             joins) we've opened but didn't close.
2610
 
          2. {each NestedJoin class not simplified away}->counter - number
 
2609
          2. {each nested_join_st structure not simplified away}->counter - number
2611
2610
             of this nested join's children that have already been added to to
2612
2611
             the partial join order.
2613
2612
  @endverbatim
3368
3367
  return 0;
3369
3368
}
3370
3369
 
 
3370
int join_read_const_table(JoinTable *tab, optimizer::Position *pos)
 
3371
{
 
3372
  int error;
 
3373
  Table *table=tab->table;
 
3374
  table->const_table=1;
 
3375
  table->null_row=0;
 
3376
  table->status=STATUS_NO_RECORD;
 
3377
 
 
3378
  if (tab->type == AM_SYSTEM)
 
3379
  {
 
3380
    if ((error=join_read_system(tab)))
 
3381
    {                                           // Info for DESCRIBE
 
3382
      tab->info="const row not found";
 
3383
      /* Mark for EXPLAIN that the row was not found */
 
3384
      pos->setFanout(0.0);
 
3385
      pos->clearRefDependMap();
 
3386
      if (! table->maybe_null || error > 0)
 
3387
        return(error);
 
3388
    }
 
3389
  }
 
3390
  else
 
3391
  {
 
3392
    if (! table->key_read && 
 
3393
        table->covering_keys.test(tab->ref.key) && 
 
3394
        ! table->no_keyread &&
 
3395
        (int) table->reginfo.lock_type <= (int) TL_READ_WITH_SHARED_LOCKS)
 
3396
    {
 
3397
      table->key_read=1;
 
3398
      table->cursor->extra(HA_EXTRA_KEYREAD);
 
3399
      tab->index= tab->ref.key;
 
3400
    }
 
3401
    error=join_read_const(tab);
 
3402
    if (table->key_read)
 
3403
    {
 
3404
      table->key_read=0;
 
3405
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
 
3406
    }
 
3407
    if (error)
 
3408
    {
 
3409
      tab->info="unique row not found";
 
3410
      /* Mark for EXPLAIN that the row was not found */
 
3411
      pos->setFanout(0.0);
 
3412
      pos->clearRefDependMap();
 
3413
      if (!table->maybe_null || error > 0)
 
3414
        return(error);
 
3415
    }
 
3416
  }
 
3417
  if (*tab->on_expr_ref && !table->null_row)
 
3418
  {
 
3419
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
 
3420
      table->mark_as_null_row();
 
3421
  }
 
3422
  if (!table->null_row)
 
3423
    table->maybe_null=0;
 
3424
 
 
3425
  /* Check appearance of new constant items in Item_equal objects */
 
3426
  Join *join= tab->join;
 
3427
  if (join->conds)
 
3428
    update_const_equal_items(join->conds, tab);
 
3429
  TableList *tbl;
 
3430
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
 
3431
  {
 
3432
    TableList *embedded;
 
3433
    TableList *embedding= tbl;
 
3434
    do
 
3435
    {
 
3436
      embedded= embedding;
 
3437
      if (embedded->on_expr)
 
3438
         update_const_equal_items(embedded->on_expr, tab);
 
3439
      embedding= embedded->getEmbedding();
 
3440
    }
 
3441
    while (embedding &&
 
3442
           embedding->getNestedJoin()->join_list.head() == embedded);
 
3443
  }
 
3444
 
 
3445
  return(0);
 
3446
}
 
3447
 
 
3448
int join_read_system(JoinTable *tab)
 
3449
{
 
3450
  Table *table= tab->table;
 
3451
  int error;
 
3452
  if (table->status & STATUS_GARBAGE)           // If first read
 
3453
  {
 
3454
    if ((error=table->cursor->read_first_row(table->getInsertRecord(),
 
3455
                                           table->getShare()->getPrimaryKey())))
 
3456
    {
 
3457
      if (error != HA_ERR_END_OF_FILE)
 
3458
        return table->report_error(error);
 
3459
      tab->table->mark_as_null_row();
 
3460
      table->emptyRecord();                     // Make empty record
 
3461
      return -1;
 
3462
    }
 
3463
    table->storeRecord();
 
3464
  }
 
3465
  else if (!table->status)                      // Only happens with left join
 
3466
    table->restoreRecord();                     // restore old record
 
3467
  table->null_row=0;
 
3468
  return table->status ? -1 : 0;
 
3469
}
 
3470
 
3371
3471
/**
3372
3472
  Read a (constant) table when there is at most one matching row.
3373
3473
 
3832
3932
              error=join->result->send_data(*join->fields) ? 1 : 0;
3833
3933
            join->send_records++;
3834
3934
          }
3835
 
          if (join->rollup.getState() != Rollup::STATE_NONE && error <= 0)
 
3935
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
3836
3936
          {
3837
3937
            if (join->rollup_send_data((uint32_t) (idx+1)))
3838
3938
              error= 1;
3922
4022
            return NESTED_LOOP_ERROR;
3923
4023
          }
3924
4024
        }
3925
 
        if (join->rollup.getState() != Rollup::STATE_NONE)
 
4025
        if (join->rollup.state != ROLLUP::STATE_NONE)
3926
4026
        {
3927
4027
          if (join->rollup_write_data((uint32_t) (idx+1), table))
3928
4028
            return NESTED_LOOP_ERROR;
5364
5464
    if (!count || count > fields.elements)
5365
5465
    {
5366
5466
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
5367
 
               order_item->full_name(), session->where());
 
5467
               order_item->full_name(), session->where);
5368
5468
      return true;
5369
5469
    }
5370
5470
    order->item= ref_pointer_array + count - 1;
5441
5541
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
5442
5542
                          ER(ER_NON_UNIQ_ERROR),
5443
5543
                          ((Item_ident*) order_item)->field_name,
5444
 
                          session->where());
 
5544
                          session->where);
5445
5545
    }
5446
5546
  }
5447
5547
 
5483
5583
                List<Item> &all_fields,
5484
5584
                Order *order)
5485
5585
{
5486
 
  session->setWhere("order clause");
 
5586
  session->where="order clause";
5487
5587
  for (; order; order=order->next)
5488
5588
  {
5489
5589
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields,
5534
5634
 
5535
5635
  uint32_t org_fields=all_fields.elements;
5536
5636
 
5537
 
  session->setWhere("group statement");
 
5637
  session->where="group statement";
5538
5638
  for (ord= order; ord; ord= ord->next)
5539
5639
  {
5540
5640
    if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
5637
5737
  {
5638
5738
    if (order->in_field_list)
5639
5739
    {
5640
 
      Order *ord=(Order*) session->getMemRoot()->duplicate((char*) order,sizeof(Order));
 
5740
      Order *ord=(Order*) session->memdup((char*) order,sizeof(Order));
5641
5741
      if (!ord)
5642
5742
        return 0;
5643
5743
      *prev=ord;
6292
6392
{
6293
6393
  /* List is reversed => we should reverse it before using */
6294
6394
  List_iterator_fast<TableList> ti(*tables);
6295
 
  TableList **table= (TableList **)session->getMemRoot()->allocate(sizeof(TableList*) *
 
6395
  TableList **table= (TableList **)session->alloc(sizeof(TableList*) *
6296
6396
                                                tables->elements);
6297
6397
  if (table == 0)
6298
6398
    return;  // out of memory