~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table.cc

  • Committer: Brian Aker
  • Date: 2009-06-03 19:30:45 UTC
  • mfrom: (1046.1.6 merge)
  • Revision ID: brian@gaz-20090603193045-4xgeczyfixh07beg
MergeĀ forĀ Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
370
370
  share->keys= table.indexes_size();
371
371
 
372
372
  share->key_parts= 0;
373
 
  for(int indx= 0; indx < table.indexes_size(); indx++)
 
373
  for (int indx= 0; indx < table.indexes_size(); indx++)
374
374
    share->key_parts+= table.indexes(indx).index_part_size();
375
375
 
376
376
  share->key_info= (KEY*) alloc_root(&share->mem_root,
399
399
  share->keynames.type_lengths[share->keynames.count]= 0;
400
400
 
401
401
  KEY* keyinfo= share->key_info;
402
 
  for (int keynr=0; keynr < table.indexes_size(); keynr++, keyinfo++)
 
402
  for (int keynr= 0; keynr < table.indexes_size(); keynr++, keyinfo++)
403
403
  {
404
404
    drizzled::message::Table::Index indx= table.indexes(keynr);
405
405
 
472
472
    keyinfo->key_part= key_part;
473
473
    keyinfo->rec_per_key= rec_per_key;
474
474
 
475
 
    for(unsigned int partnr= 0;
 
475
    for (unsigned int partnr= 0;
476
476
        partnr < keyinfo->key_parts;
477
477
        partnr++, key_part++)
478
478
    {
479
479
      drizzled::message::Table::Index::IndexPart part;
480
480
      part= indx.index_part(partnr);
481
481
 
482
 
      *rec_per_key++=0;
 
482
      *rec_per_key++= 0;
483
483
 
484
484
      key_part->field= NULL;
485
485
      key_part->fieldnr= part.fieldnr() + 1; // start from 1.
565
565
 
566
566
  uint32_t stored_columns_reclength= 0;
567
567
 
568
 
  for (unsigned int fieldnr=0; fieldnr < share->fields; fieldnr++)
 
568
  for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
569
569
  {
570
570
    drizzled::message::Table::Field pfield= table.field(fieldnr);
571
571
    if (pfield.has_constraints() && pfield.constraints().is_nullable())
692
692
 
693
693
  uint32_t interval_nr= 0;
694
694
 
695
 
  for(unsigned int fieldnr=0; fieldnr < share->fields; fieldnr++)
 
695
  for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
696
696
  {
697
697
    drizzled::message::Table::Field pfield= table.field(fieldnr);
698
698
 
730
730
    t->count= field_options.field_value_size();
731
731
    t->name= NULL;
732
732
 
733
 
    for(int n=0; n < field_options.field_value_size(); n++)
 
733
    for (int n= 0; n < field_options.field_value_size(); n++)
734
734
    {
735
735
      t->type_names[n]= strmake_root(&share->mem_root,
736
736
                                     field_options.field_value(n).c_str(),
762
762
  unsigned char* null_pos= record;;
763
763
  int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
764
764
 
765
 
  for(unsigned int fieldnr=0; fieldnr < share->fields; fieldnr++)
 
765
  for (unsigned int fieldnr= 0; fieldnr < share->fields; fieldnr++)
766
766
  {
767
767
    drizzled::message::Table::Field pfield= table.field(fieldnr);
768
768
 
793
793
 
794
794
    if (pfield.has_options()
795
795
       && pfield.options().has_default_value()
796
 
       && pfield.options().default_value().compare("NOW()")==0)
 
796
       && pfield.options().default_value().compare("NOW()") == 0)
797
797
    {
798
798
      if (pfield.options().has_update_value()
799
 
         && pfield.options().update_value().compare("NOW()")==0)
 
799
         && pfield.options().update_value().compare("NOW()") == 0)
800
800
      {
801
801
        unireg_type= Field::TIMESTAMP_DNUN_FIELD;
802
802
      }
809
809
    }
810
810
    else if (pfield.has_options()
811
811
             && pfield.options().has_update_value()
812
 
             && pfield.options().update_value().compare("NOW()")==0)
 
812
             && pfield.options().update_value().compare("NOW()") == 0)
813
813
    {
814
814
      unireg_type= Field::TIMESTAMP_UN_FIELD;
815
815
    }
957
957
  }
958
958
 
959
959
  keyinfo= share->key_info;
960
 
  for (unsigned int keynr=0; keynr < share->keys; keynr++, keyinfo++)
 
960
  for (unsigned int keynr= 0; keynr < share->keys; keynr++, keyinfo++)
961
961
  {
962
962
    key_part= keyinfo->key_part;
963
963
 
964
 
    for(unsigned int partnr= 0;
965
 
        partnr < keyinfo->key_parts;
966
 
        partnr++, key_part++)
 
964
    for (unsigned int partnr= 0;
 
965
         partnr < keyinfo->key_parts;
 
966
         partnr++, key_part++)
967
967
    {
968
968
      /* Fix up key_part->offset by adding data_offset.
969
969
         We really should compute offset as well.
1003
1003
    keyinfo= share->key_info;
1004
1004
    key_part= keyinfo->key_part;
1005
1005
 
1006
 
    for (uint32_t key=0 ; key < share->keys ; key++,keyinfo++)
 
1006
    for (uint32_t key= 0 ; key < share->keys ; key++,keyinfo++)
1007
1007
    {
1008
1008
      uint32_t usable_parts= 0;
1009
1009
 
1014
1014
          declare this as a primary key.
1015
1015
        */
1016
1016
        primary_key=key;
1017
 
        for (uint32_t i=0 ; i < keyinfo->key_parts ;i++)
 
1017
        for (uint32_t i= 0 ; i < keyinfo->key_parts ;i++)
1018
1018
        {
1019
1019
          uint32_t fieldnr= key_part[i].fieldnr;
1020
1020
          if (!fieldnr ||
1028
1028
        }
1029
1029
      }
1030
1030
 
1031
 
      for (uint32_t i=0 ; i < keyinfo->key_parts ; key_part++,i++)
 
1031
      for (uint32_t i= 0 ; i < keyinfo->key_parts ; key_part++,i++)
1032
1032
      {
1033
1033
        Field *field;
1034
1034
        if (!key_part->fieldnr)
1166
1166
          (uint*) alloc_root(&share->mem_root,
1167
1167
                             (uint32_t) (share->blob_fields* sizeof(uint32_t)))))
1168
1168
      goto err;
1169
 
    for (k=0, ptr= share->field ; *ptr ; ptr++, k++)
 
1169
    for (k= 0, ptr= share->field ; *ptr ; ptr++, k++)
1170
1170
    {
1171
1171
      if ((*ptr)->flags & BLOB_FLAG)
1172
1172
        (*save++)= k;
1336
1336
  }
1337
1337
 
1338
1338
  error= 4;
1339
 
  records=0;
 
1339
  records= 0;
1340
1340
  if ((db_stat & HA_OPEN_KEYFILE) || (prgflag & DELAYED_OPEN))
1341
1341
    records=1;
1342
1342
  if (prgflag & (READ_ALL+EXTRA_RECORD))
1387
1387
  outparam->null_flags= (unsigned char*) record+1;
1388
1388
 
1389
1389
  /* Setup copy of fields from share, but use the right alias and record */
1390
 
  for (i=0 ; i < share->fields; i++, field_ptr++)
 
1390
  for (i= 0 ; i < share->fields; i++, field_ptr++)
1391
1391
  {
1392
1392
    if (!((*field_ptr)= share->field[i]->clone(&outparam->mem_root, outparam)))
1393
1393
      goto err;
1521
1521
    share->open_table_error(error, my_errno, 0);
1522
1522
  delete outparam->file;
1523
1523
  outparam->file= 0;                            // For easier error checking
1524
 
  outparam->db_stat=0;
 
1524
  outparam->db_stat= 0;
1525
1525
  free_root(&outparam->mem_root, MYF(0));       // Safe to call on zeroed root
1526
1526
  free((char*) outparam->alias);
1527
1527
  return (error);
1538
1538
 
1539
1539
int Table::closefrm(bool free_share)
1540
1540
{
1541
 
  int error=0;
 
1541
  int error= 0;
1542
1542
 
1543
1543
  if (db_stat)
1544
1544
    error= file->close();
1664
1664
  result->type_lengths= (uint*) (result->type_names + result->count + 1);
1665
1665
  List_iterator<String> it(strings);
1666
1666
  String *tmp;
1667
 
  for (uint32_t i=0; (tmp=it++) ; i++)
 
1667
  for (uint32_t i= 0; (tmp=it++) ; i++)
1668
1668
  {
1669
1669
    result->type_names[i]= tmp->ptr();
1670
1670
    result->type_lengths[i]= tmp->length();
2743
2743
  unsigned char *null_flags;
2744
2744
  Field **reg_field, **from_field, **default_field;
2745
2745
  uint32_t *blob_field;
2746
 
  Copy_field *copy=0;
 
2746
  Copy_field *copy= 0;
2747
2747
  KEY *keyinfo;
2748
2748
  KEY_PART_INFO *key_part_info;
2749
2749
  Item **copy_func;
2767
2767
  if (group)
2768
2768
  {
2769
2769
    if (!param->quick_group)
2770
 
      group=0;                                  // Can't use group key
 
2770
      group= 0;                                 // Can't use group key
2771
2771
    else for (order_st *tmp=group ; tmp ; tmp=tmp->next)
2772
2772
    {
2773
2773
      /*
2783
2783
    if (param->group_length >= MAX_BLOB_WIDTH)
2784
2784
      using_unique_constraint=1;
2785
2785
    if (group)
2786
 
      distinct=0;                               // Can't use distinct
 
2786
      distinct= 0;                              // Can't use distinct
2787
2787
  }
2788
2788
 
2789
2789
  field_count=param->field_count+param->func_count+param->sum_func_count;
2866
2866
 
2867
2867
  reclength= string_total_length= 0;
2868
2868
  blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
2869
 
  param->using_indirect_summary_function=0;
 
2869
  param->using_indirect_summary_function= 0;
2870
2870
 
2871
2871
  List_iterator_fast<Item> li(fields);
2872
2872
  Item *item;
2897
2897
    }
2898
2898
    if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
2899
2899
    {                                           /* Can't calc group yet */
2900
 
      ((Item_sum*) item)->result_field=0;
2901
 
      for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
 
2900
      ((Item_sum*) item)->result_field= 0;
 
2901
      for (i= 0 ; i < ((Item_sum*) item)->arg_count ; i++)
2902
2902
      {
2903
2903
        Item **argp= ((Item_sum*) item)->args + i;
2904
2904
        Item *arg= *argp;
2959
2959
                         tmp_from_field, &default_field[fieldnr],
2960
2960
                         group != 0,
2961
2961
                         !force_copy_fields &&
2962
 
                           (not_all_columns || group !=0),
 
2962
                           (not_all_columns || group != 0),
2963
2963
                         /*
2964
2964
                           If item->marker == 4 then we force create_tmp_field
2965
2965
                           to create a 64-bit longs for BIT fields because HEAP
3074
3074
    table->record[1]= table->record[0]+alloc_length;
3075
3075
    share->default_values= table->record[1]+alloc_length;
3076
3076
  }
3077
 
  copy_func[0]=0;                               // End marker
 
3077
  copy_func[0]= 0;                              // End marker
3078
3078
  param->func_count= copy_func - param->items_to_copy;
3079
3079
 
3080
3080
  table->setup_tmp_table_column_bitmaps(bitmaps);
3096
3096
  }
3097
3097
  null_count= (blob_count == 0) ? 1 : 0;
3098
3098
  hidden_field_count=param->hidden_field_count;
3099
 
  for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
 
3099
  for (i= 0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
3100
3100
  {
3101
3101
    Field *field= *reg_field;
3102
3102
    uint32_t length;
3110
3110
          We have to reserve one byte here for NULL bits,
3111
3111
          as this is updated by 'end_update()'
3112
3112
        */
3113
 
        *pos++=0;                               // Null is stored here
3114
 
        recinfo->length=1;
 
3113
        *pos++= NULL;                           // Null is stored here
 
3114
        recinfo->length= 1;
3115
3115
        recinfo->type=FIELD_NORMAL;
3116
3116
        recinfo++;
3117
3117
        memset(recinfo, 0, sizeof(*recinfo));
3209
3209
    keyinfo->key_part=key_part_info;
3210
3210
    keyinfo->flags=HA_NOSAME;
3211
3211
    keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
3212
 
    keyinfo->key_length=0;
3213
 
    keyinfo->rec_per_key=0;
 
3212
    keyinfo->key_length= 0;
 
3213
    keyinfo->rec_per_key= 0;
3214
3214
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
3215
3215
    keyinfo->name= (char*) "group_key";
3216
3216
    order_st *cur_group= group;
3218
3218
    {
3219
3219
      Field *field=(*cur_group->item)->get_tmp_table_field();
3220
3220
      bool maybe_null=(*cur_group->item)->maybe_null;
3221
 
      key_part_info->null_bit=0;
 
3221
      key_part_info->null_bit= 0;
3222
3222
      key_part_info->field=  field;
3223
3223
      key_part_info->offset= field->offset(table->record[0]);
3224
3224
      key_part_info->length= (uint16_t) field->key_length();
3293
3293
    keyinfo->key_length=(uint16_t) reclength;
3294
3294
    keyinfo->name= (char*) "distinct_key";
3295
3295
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
3296
 
    keyinfo->rec_per_key=0;
 
3296
    keyinfo->rec_per_key= 0;
3297
3297
 
3298
3298
    /*
3299
3299
      Create an extra field to hold NULL bits so that unique indexes on
3302
3302
    */
3303
3303
    if (null_pack_length && share->uniques)
3304
3304
    {
3305
 
      key_part_info->null_bit=0;
 
3305
      key_part_info->null_bit= 0;
3306
3306
      key_part_info->offset=hidden_null_pack_length;
3307
3307
      key_part_info->length=null_pack_length;
3308
3308
      key_part_info->field= new Field_varstring(table->record[0],
3326
3326
         i < field_count;
3327
3327
         i++, reg_field++, key_part_info++)
3328
3328
    {
3329
 
      key_part_info->null_bit=0;
 
3329
      key_part_info->null_bit= 0;
3330
3330
      key_part_info->field=    *reg_field;
3331
3331
      key_part_info->offset=   (*reg_field)->offset(table->record[0]);
3332
3332
      key_part_info->length=   (uint16_t) (*reg_field)->pack_length();
3504
3504
  return 0;
3505
3505
}
3506
3506
 
3507
 
 
3508
3507
bool Table::open_tmp_table()
3509
3508
{
3510
3509
  int error;
3512
3511
                                  HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
3513
3512
  {
3514
3513
    file->print_error(error,MYF(0)); /* purecov: inspected */
3515
 
    db_stat=0;
3516
 
    return(1);
 
3514
    db_stat= 0;
 
3515
    return true;
3517
3516
  }
3518
3517
  (void) file->extra(HA_EXTRA_QUICK);           /* Faster */
3519
 
  return(0);
 
3518
  return false;
3520
3519
}
3521
3520
 
3522
3521
 
3561
3560
 
3562
3561
  if (share->keys)
3563
3562
  {                                             // Get keys for ni_create
3564
 
    bool using_unique_constraint=0;
 
3563
    bool using_unique_constraint= 0;
3565
3564
    HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&this->mem_root,
3566
3565
                                            sizeof(*seg) * keyinfo->key_parts);
3567
3566
    if (!seg)
3596
3595
      keydef.keysegs=  keyinfo->key_parts;
3597
3596
      keydef.seg= seg;
3598
3597
    }
3599
 
    for (uint32_t i=0; i < keyinfo->key_parts ; i++,seg++)
 
3598
    for (uint32_t i= 0; i < keyinfo->key_parts ; i++,seg++)
3600
3599
    {
3601
3600
      Field *key_field=keyinfo->key_part[i].field;
3602
3601
      seg->flag=     0;
3611
3610
        seg->bit_start= (uint8_t)(key_field->pack_length()
3612
3611
                                  - share->blob_ptr_size);
3613
3612
        seg->flag= HA_BLOB_PART;
3614
 
        seg->length=0;                  // Whole blob in unique constraint
 
3613
        seg->length= 0;                 // Whole blob in unique constraint
3615
3614
      }
3616
3615
      else
3617
3616
      {
3646
3645
                       HA_CREATE_TMP_TABLE)))
3647
3646
  {
3648
3647
    file->print_error(error,MYF(0));    /* purecov: inspected */
3649
 
    db_stat=0;
 
3648
    db_stat= 0;
3650
3649
    goto err;
3651
3650
  }
3652
3651
  status_var_increment(in_use->status_var.created_tmp_disk_tables);
3684
3683
 
3685
3684
  free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
3686
3685
  session->set_proc_info(save_proc_info);
3687
 
 
3688
 
  return;
3689
3686
}
3690
3687
 
3691
3688
/**
3707
3704
      error != HA_ERR_RECORD_FILE_FULL)
3708
3705
  {
3709
3706
    table->file->print_error(error,MYF(0));
3710
 
    return(1);
 
3707
    return true;
3711
3708
  }
3712
3709
 
3713
3710
  // Release latches since this can take a long time
3719
3716
  new_table.s->storage_engine= myisam_engine;
3720
3717
  if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
3721
3718
                                        new_table.s->db_type())))
3722
 
    return(1);                          // End of memory
 
3719
    return true;                                // End of memory
3723
3720
 
3724
3721
  save_proc_info=session->get_proc_info();
3725
3722
  session->set_proc_info("converting HEAP to MyISAM");
3766
3763
  (void) table->file->ha_rnd_end();
3767
3764
  (void) table->file->close();                  // This deletes the table !
3768
3765
  delete table->file;
3769
 
  table->file=0;
 
3766
  table->file= NULL;
3770
3767
  new_table.s= table->s;                       // Keep old share
3771
3768
  *table= new_table;
3772
3769
  *table->s= share;
3780
3777
      "Copying to tmp table on disk" : save_proc_info);
3781
3778
    session->set_proc_info(new_proc_info);
3782
3779
  }
3783
 
  return(0);
 
3780
  return false;
3784
3781
 
3785
3782
 err:
3786
3783
  table->file->print_error(write_err, MYF(0));
3792
3789
  delete new_table.file;
3793
3790
  session->set_proc_info(save_proc_info);
3794
3791
  table->mem_root= new_table.mem_root;
3795
 
  return(1);
 
3792
  return true;
3796
3793
}
3797
3794
 
3798
3795
my_bitmap_map *Table::use_all_columns(MY_BITMAP *bitmap)
3813
3810
  uint32_t best= MAX_KEY;
3814
3811
  if (usable_keys->any())
3815
3812
  {
3816
 
    for (uint32_t nr=0; nr < s->keys ; nr++)
 
3813
    for (uint32_t nr= 0; nr < s->keys ; nr++)
3817
3814
    {
3818
3815
      if (usable_keys->test(nr))
3819
3816
      {