~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table.cc

  • Committer: Brian Aker
  • Date: 2010-07-09 17:59:07 UTC
  • Revision ID: brian@gaz-20100709175907-h014kkb6cztsr21d
Fix const_cast, disable the unittest for generators.

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
/* Some general useful functions */
18
18
 
19
 
#include <config.h>
 
19
#include "config.h"
20
20
 
21
21
#include <float.h>
22
22
#include <fcntl.h>
28
28
#include <drizzled/error.h>
29
29
#include <drizzled/gettext.h>
30
30
 
31
 
#include <drizzled/plugin/transactional_storage_engine.h>
32
 
#include <drizzled/plugin/authorization.h>
 
31
#include "drizzled/plugin/transactional_storage_engine.h"
 
32
#include "drizzled/plugin/authorization.h"
33
33
#include <drizzled/nested_join.h>
34
34
#include <drizzled/sql_parse.h>
35
35
#include <drizzled/item/sum.h>
42
42
#include <drizzled/field/double.h>
43
43
#include <drizzled/unireg.h>
44
44
#include <drizzled/message/table.pb.h>
45
 
#include <drizzled/sql_table.h>
46
 
#include <drizzled/charset.h>
47
 
#include <drizzled/internal/m_string.h>
48
 
#include <plugin/myisam/myisam.h>
49
 
#include <drizzled/plugin/storage_engine.h>
 
45
#include "drizzled/sql_table.h"
 
46
#include "drizzled/charset.h"
 
47
#include "drizzled/internal/m_string.h"
 
48
#include "plugin/myisam/myisam.h"
50
49
 
51
50
#include <drizzled/item/string.h>
52
51
#include <drizzled/item/int.h>
55
54
#include <drizzled/item/null.h>
56
55
#include <drizzled/temporal.h>
57
56
 
58
 
#include <drizzled/refresh_version.h>
59
 
 
60
 
#include <drizzled/table/singular.h>
61
 
 
62
 
#include <drizzled/table_proto.h>
63
 
#include <drizzled/typelib.h>
 
57
#include "drizzled/table_share_instance.h"
 
58
 
 
59
#include "drizzled/table_proto.h"
64
60
 
65
61
using namespace std;
66
62
 
67
63
namespace drizzled
68
64
{
69
65
 
 
66
extern pid_t current_pid;
70
67
extern plugin::StorageEngine *heap_engine;
71
68
extern plugin::StorageEngine *myisam_engine;
72
69
 
73
70
/* Functions defined in this cursor */
74
71
 
 
72
void open_table_error(TableShare *share, int error, int db_errno,
 
73
                      myf errortype, int errarg);
 
74
 
75
75
/*************************************************************************/
76
76
 
77
77
// @note this should all be the destructor
81
81
 
82
82
  if (db_stat)
83
83
    error= cursor->close();
84
 
  _alias.clear();
85
 
 
 
84
  free((char*) alias);
 
85
  alias= NULL;
86
86
  if (field)
87
87
  {
88
88
    for (Field **ptr=field ; *ptr ; ptr++)
91
91
    }
92
92
    field= 0;
93
93
  }
94
 
  safe_delete(cursor);
 
94
  delete cursor;
 
95
  cursor= 0;                            /* For easier errorchecking */
95
96
 
96
97
  if (free_share)
97
98
  {
98
 
    release();
 
99
    if (s->getType() == message::Table::STANDARD)
 
100
    {
 
101
      TableShare::release(s);
 
102
    }
 
103
    else
 
104
    {
 
105
      assert(s->newed);
 
106
      delete s;
 
107
    }
 
108
 
 
109
    s= NULL;
99
110
  }
 
111
  mem_root.free_root(MYF(0));
100
112
 
101
113
  return error;
102
114
}
103
115
 
104
 
Table::~Table()
105
 
{
106
 
  mem_root.free_root(MYF(0));
107
 
}
108
 
 
109
116
 
110
117
void Table::resetTable(Session *session,
111
118
                       TableShare *share,
112
119
                       uint32_t db_stat_arg)
113
120
{
114
 
  setShare(share);
115
 
  in_use= session;
116
 
 
 
121
  s= share;
117
122
  field= NULL;
118
123
 
119
124
  cursor= NULL;
126
131
  tablenr= 0;
127
132
  db_stat= db_stat_arg;
128
133
 
 
134
  in_use= session;
129
135
  record[0]= (unsigned char *) NULL;
130
136
  record[1]= (unsigned char *) NULL;
131
137
 
132
 
  insert_values.clear();
 
138
  insert_values= NULL;
133
139
  key_info= NULL;
134
140
  next_number_field= NULL;
135
141
  found_next_number_field= NULL;
137
143
 
138
144
  pos_in_table_list= NULL;
139
145
  group= NULL;
140
 
  _alias.clear();
 
146
  alias= NULL;
141
147
  null_flags= NULL;
142
148
 
143
149
  lock_position= 0;
191
197
  memset(quick_n_ranges, 0, sizeof(unsigned int) * MAX_KEY);
192
198
 
193
199
  memory::init_sql_alloc(&mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
 
200
  memset(&sort, 0, sizeof(filesort_info_st));
194
201
}
195
202
 
196
203
 
197
204
 
198
205
/* Deallocate temporary blob storage */
199
206
 
200
 
void free_blobs(Table *table)
 
207
void free_blobs(register Table *table)
201
208
{
202
209
  uint32_t *ptr, *end;
203
210
  for (ptr= table->getBlobField(), end=ptr + table->sizeBlobFields();
223
230
    
224
231
  result->type_lengths= (uint*) (result->type_names + result->count + 1);
225
232
 
226
 
  List<String>::iterator it(strings.begin());
 
233
  List_iterator<String> it(strings);
227
234
  String *tmp;
228
235
  for (uint32_t i= 0; (tmp= it++); i++)
229
236
  {
239
246
 
240
247
        /* Check that the integer is in the internal */
241
248
 
242
 
int set_zone(int nr, int min_zone, int max_zone)
 
249
int set_zone(register int nr, int min_zone, int max_zone)
243
250
{
244
251
  if (nr<=min_zone)
245
252
    return (min_zone);
248
255
  return (nr);
249
256
} /* set_zone */
250
257
 
 
258
        /* Adjust number to next larger disk buffer */
 
259
 
 
260
ulong next_io_size(register ulong pos)
 
261
{
 
262
  register ulong offset;
 
263
  if ((offset= pos & (IO_SIZE-1)))
 
264
    return pos-offset+IO_SIZE;
 
265
  return pos;
 
266
} /* next_io_size */
 
267
 
251
268
 
252
269
/*
253
270
  Store an SQL quoted string.
275
292
        (mblen= my_ismbchar(default_charset_info, pos, end)))
276
293
    {
277
294
      res->append(pos, mblen);
278
 
      pos+= mblen - 1;
 
295
      pos+= mblen;
279
296
      if (pos >= end)
280
297
        break;
281
298
      continue;
311
328
}
312
329
 
313
330
 
 
331
/*
 
332
  Set up column usage bitmaps for a temporary table
 
333
 
 
334
  IMPLEMENTATION
 
335
    For temporary tables, we need one bitmap with all columns set and
 
336
    a tmp_set bitmap to be used by things like filesort.
 
337
*/
 
338
 
 
339
void Table::setup_tmp_table_column_bitmaps(unsigned char *bitmaps)
 
340
{
 
341
  uint32_t field_count= s->sizeFields();
 
342
 
 
343
  this->def_read_set.init((my_bitmap_map*) bitmaps, field_count);
 
344
  this->tmp_set.init((my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)), field_count);
 
345
 
 
346
  /* write_set and all_set are copies of read_set */
 
347
  def_write_set= def_read_set;
 
348
  s->all_set= def_read_set;
 
349
  this->getMutableShare()->all_set.setAll();
 
350
  default_column_bitmaps();
 
351
}
 
352
 
 
353
 
314
354
int rename_file_ext(const char * from,const char * to,const char * ext)
315
355
{
316
356
  string from_s, to_s;
323
363
}
324
364
 
325
365
/*
 
366
  Check if database name is valid
 
367
 
 
368
  SYNPOSIS
 
369
    check_db_name()
 
370
    org_name            Name of database and length
 
371
 
 
372
  RETURN
 
373
    false error
 
374
    true ok
 
375
*/
 
376
 
 
377
bool check_db_name(Session *session, SchemaIdentifier &schema_identifier)
 
378
{
 
379
  if (not plugin::Authorization::isAuthorized(session->getSecurityContext(), schema_identifier))
 
380
  {
 
381
    return false;
 
382
  }
 
383
 
 
384
  return schema_identifier.isValid();
 
385
}
 
386
 
 
387
/*
326
388
  Allow anything as a table name, as long as it doesn't contain an
327
389
  ' ' at the end
328
390
  returns 1 on error
392
454
    bitmap_clear_all(&table->def_read_set);
393
455
    bitmap_clear_all(&table->def_write_set);
394
456
  */
395
 
  def_read_set.reset();
396
 
  def_write_set.reset();
397
 
  column_bitmaps_set(def_read_set, def_write_set);
 
457
  def_read_set.clearAll();
 
458
  def_write_set.clearAll();
 
459
  column_bitmaps_set(&def_read_set, &def_write_set);
398
460
}
399
461
 
400
462
 
411
473
{
412
474
 
413
475
  if ((cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
414
 
      getShare()->hasPrimaryKey())
 
476
      s->hasPrimaryKey())
415
477
  {
416
 
    mark_columns_used_by_index_no_reset(getShare()->getPrimaryKey());
 
478
    mark_columns_used_by_index_no_reset(s->getPrimaryKey());
417
479
  }
418
480
  return;
419
481
}
431
493
 
432
494
void Table::mark_columns_used_by_index(uint32_t index)
433
495
{
434
 
  boost::dynamic_bitset<> *bitmap= &tmp_set;
 
496
  MyBitmap *bitmap= &tmp_set;
435
497
 
436
498
  (void) cursor->extra(HA_EXTRA_KEYREAD);
437
 
  bitmap->reset();
438
 
  mark_columns_used_by_index_no_reset(index, *bitmap);
439
 
  column_bitmaps_set(*bitmap, *bitmap);
 
499
  bitmap->clearAll();
 
500
  mark_columns_used_by_index_no_reset(index, bitmap);
 
501
  column_bitmaps_set(bitmap, bitmap);
440
502
  return;
441
503
}
442
504
 
468
530
 
469
531
void Table::mark_columns_used_by_index_no_reset(uint32_t index)
470
532
{
471
 
    mark_columns_used_by_index_no_reset(index, *read_set);
 
533
    mark_columns_used_by_index_no_reset(index, read_set);
472
534
}
473
535
 
474
 
 
475
536
void Table::mark_columns_used_by_index_no_reset(uint32_t index,
476
 
                                                boost::dynamic_bitset<>& bitmap)
 
537
                                                MyBitmap *bitmap)
477
538
{
478
539
  KeyPartInfo *key_part= key_info[index].key_part;
479
 
  KeyPartInfo *key_part_end= (key_part + key_info[index].key_parts);
480
 
  for (; key_part != key_part_end; key_part++)
481
 
  {
482
 
    if (! bitmap.empty())
483
 
      bitmap.set(key_part->fieldnr-1);
484
 
  }
 
540
  KeyPartInfo *key_part_end= (key_part +
 
541
                                key_info[index].key_parts);
 
542
  for (;key_part != key_part_end; key_part++)
 
543
    bitmap->setBit(key_part->fieldnr-1);
485
544
}
486
545
 
487
546
 
500
559
    We must set bit in read set as update_auto_increment() is using the
501
560
    store() to check overflow of auto_increment values
502
561
  */
503
 
  setReadSet(found_next_number_field->position());
504
 
  setWriteSet(found_next_number_field->position());
505
 
  if (getShare()->next_number_keypart)
506
 
    mark_columns_used_by_index_no_reset(getShare()->next_number_index);
 
562
  setReadSet(found_next_number_field->field_index);
 
563
  setWriteSet(found_next_number_field->field_index);
 
564
  if (s->next_number_keypart)
 
565
    mark_columns_used_by_index_no_reset(s->next_number_index);
507
566
}
508
567
 
509
568
 
534
593
    be able to do an delete
535
594
 
536
595
  */
537
 
  if (not getShare()->hasPrimaryKey())
 
596
  if (not s->hasPrimaryKey())
538
597
  {
539
598
    /* fallback to use all columns in the table to identify row */
540
599
    use_all_columns();
541
600
    return;
542
601
  }
543
602
  else
544
 
    mark_columns_used_by_index_no_reset(getShare()->getPrimaryKey());
 
603
    mark_columns_used_by_index_no_reset(s->getPrimaryKey());
545
604
 
546
605
  /* If we the engine wants all predicates we mark all keys */
547
606
  if (cursor->getEngine()->check_flag(HTON_BIT_REQUIRES_KEY_COLUMNS_FOR_DELETE))
550
609
    for (reg_field= field ; *reg_field ; reg_field++)
551
610
    {
552
611
      if ((*reg_field)->flags & PART_KEY_FLAG)
553
 
        setReadSet((*reg_field)->position());
 
612
        setReadSet((*reg_field)->field_index);
554
613
    }
555
614
  }
556
615
}
582
641
    the primary key, the hidden primary key or all columns to be
583
642
    able to do an update
584
643
  */
585
 
  if (not getShare()->hasPrimaryKey())
 
644
  if (not s->hasPrimaryKey())
586
645
  {
587
646
    /* fallback to use all columns in the table to identify row */
588
647
    use_all_columns();
589
648
    return;
590
649
  }
591
650
  else
592
 
    mark_columns_used_by_index_no_reset(getShare()->getPrimaryKey());
 
651
    mark_columns_used_by_index_no_reset(s->getPrimaryKey());
593
652
 
594
653
  if (cursor->getEngine()->check_flag(HTON_BIT_REQUIRES_KEY_COLUMNS_FOR_DELETE))
595
654
  {
599
658
    {
600
659
      /* Merge keys is all keys that had a column refered to in the query */
601
660
      if (is_overlapping(merge_keys, (*reg_field)->part_of_key))
602
 
        setReadSet((*reg_field)->position());
 
661
        setReadSet((*reg_field)->field_index);
603
662
    }
604
663
  }
605
664
 
631
690
  {
632
691
    Field_blob* const blob= (Field_blob*) field[*ptr];
633
692
    length+= blob->get_length((const unsigned char*)
634
 
                              (data + blob->offset(getInsertRecord()))) +
 
693
                              (data + blob->offset(record[0]))) +
635
694
      HA_KEY_BLOB_LENGTH;
636
695
  }
637
696
  return length;
638
697
}
639
698
 
640
 
void Table::setVariableWidth(void)
641
 
{
642
 
  assert(in_use);
643
 
  if (in_use && in_use->getLex()->sql_command == SQLCOM_CREATE_TABLE)
644
 
  {
645
 
    getMutableShare()->setVariableWidth();
646
 
    return;
647
 
  }
648
 
 
649
 
  assert(0); // Programming error, you can't set this on a plain old Table.
650
 
}
651
 
 
652
699
/****************************************************************************
653
700
 Functions for creating temporary tables.
654
701
****************************************************************************/
686
733
  */
687
734
  if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
688
735
      (org_field->flags & BLOB_FLAG))
689
 
  {
690
 
    table->setVariableWidth();
691
736
    new_field= new Field_varstring(convert_blob_length,
692
737
                                   org_field->maybe_null(),
693
 
                                   org_field->field_name,
 
738
                                   org_field->field_name, table->getMutableShare(),
694
739
                                   org_field->charset());
695
 
  }
696
740
  else
697
 
  {
698
741
    new_field= org_field->new_field(session->mem_root, table,
699
 
                                    table == org_field->getTable());
700
 
  }
 
742
                                    table == org_field->table);
701
743
  if (new_field)
702
744
  {
703
745
    new_field->init(table);
750
792
 
751
793
Table *
752
794
create_tmp_table(Session *session,Tmp_Table_Param *param,List<Item> &fields,
753
 
                 Order *group, bool distinct, bool save_sum_fields,
 
795
                 order_st *group, bool distinct, bool save_sum_fields,
754
796
                 uint64_t select_options, ha_rows rows_limit,
755
797
                 const char *table_alias)
756
798
{
757
799
  memory::Root *mem_root_save;
 
800
  Table *table;
758
801
  uint  i,field_count,null_count,null_pack_length;
759
802
  uint32_t  copy_func_count= param->func_count;
760
803
  uint32_t  hidden_null_count, hidden_null_pack_length, hidden_field_count;
764
807
  bool  using_unique_constraint= false;
765
808
  bool  use_packed_rows= true;
766
809
  bool  not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
767
 
  unsigned char *pos, *group_buff;
 
810
  unsigned char *pos, *group_buff, *bitmaps;
768
811
  unsigned char *null_flags;
769
812
  Field **reg_field, **from_field, **default_field;
770
813
  CopyField *copy= 0;
776
819
  bool force_copy_fields= param->force_copy_fields;
777
820
  uint64_t max_rows= 0;
778
821
 
779
 
  session->status_var.created_tmp_tables++;
 
822
  status_var_increment(session->status_var.created_tmp_tables);
780
823
 
781
824
  if (group)
782
825
  {
784
827
    {
785
828
      group= 0;                                 // Can't use group key
786
829
    }
787
 
    else for (Order *tmp=group ; tmp ; tmp=tmp->next)
 
830
    else for (order_st *tmp=group ; tmp ; tmp=tmp->next)
788
831
    {
789
832
      /*
790
833
        marker == 4 means two things:
817
860
    copy_func_count+= param->sum_func_count;
818
861
  }
819
862
 
820
 
  table::Singular *table;
821
 
  table= session->getInstanceTable(); // This will not go into the tableshare cache, so no key is used.
 
863
  TableShareInstance *share= session->getTemporaryShare(message::Table::INTERNAL); // This will not go into the tableshare cache, so no key is used.
822
864
 
823
 
  if (not table->getMemRoot()->multi_alloc_root(0,
 
865
  if (not share->getMemRoot()->multi_alloc_root(0,
824
866
                                                &default_field, sizeof(Field*) * (field_count),
825
867
                                                &from_field, sizeof(Field*)*field_count,
826
868
                                                &copy_func, sizeof(*copy_func)*(copy_func_count+1),
829
871
                                                &param->start_recinfo, sizeof(*param->recinfo)*(field_count*2+4),
830
872
                                                &group_buff, (group && ! using_unique_constraint ?
831
873
                                                              param->group_length : 0),
 
874
                                                &bitmaps, bitmap_buffer_size(field_count)*2,
832
875
                                                NULL))
833
876
  {
834
877
    return NULL;
841
884
  param->items_to_copy= copy_func;
842
885
  /* make table according to fields */
843
886
 
 
887
  table= share->getTable();
 
888
 
844
889
  memset(default_field, 0, sizeof(Field*) * (field_count));
845
890
  memset(from_field, 0, sizeof(Field*)*field_count);
846
891
 
847
892
  mem_root_save= session->mem_root;
848
893
  session->mem_root= table->getMemRoot();
849
894
 
850
 
  table->getMutableShare()->setFields(field_count+1);
851
 
  table->setFields(table->getMutableShare()->getFields(true));
852
 
  reg_field= table->getMutableShare()->getFields(true);
853
 
  table->setAlias(table_alias);
 
895
  share->setFields(field_count+1);
 
896
  table->setFields(share->getFields(true));
 
897
  reg_field= share->getFields(true);
 
898
  table->alias= table_alias;
854
899
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
855
900
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
856
901
  table->map=1;
861
906
  table->covering_keys.reset();
862
907
  table->keys_in_use_for_query.reset();
863
908
 
864
 
  table->getMutableShare()->blob_field.resize(field_count+1);
865
 
  uint32_t *blob_field= &table->getMutableShare()->blob_field[0];
866
 
  table->getMutableShare()->db_low_byte_first=1;                // True for HEAP and MyISAM
867
 
  table->getMutableShare()->table_charset= param->table_charset;
868
 
  table->getMutableShare()->keys_for_keyread.reset();
869
 
  table->getMutableShare()->keys_in_use.reset();
 
909
  table->setShare(share);
 
910
  share->blob_field.resize(field_count+1);
 
911
  uint32_t *blob_field= &share->blob_field[0];
 
912
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
913
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
 
914
  share->table_charset= param->table_charset;
 
915
  share->keys_for_keyread.reset();
 
916
  share->keys_in_use.reset();
870
917
 
871
918
  /* Calculate which type of fields we will store in the temporary table */
872
919
 
874
921
  blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
875
922
  param->using_indirect_summary_function= 0;
876
923
 
877
 
  List<Item>::iterator li(fields.begin());
 
924
  List_iterator_fast<Item> li(fields);
878
925
  Item *item;
879
926
  Field **tmp_from_field=from_field;
880
927
  while ((item=li++))
943
990
            */
944
991
            (*argp)->maybe_null=1;
945
992
          }
946
 
          new_field->setPosition(fieldnr++);
 
993
          new_field->field_index= fieldnr++;
947
994
        }
948
995
      }
949
996
    }
990
1037
        group_null_items++;
991
1038
        new_field->flags|= GROUP_FLAG;
992
1039
      }
993
 
      new_field->setPosition(fieldnr++);
 
1040
      new_field->field_index= fieldnr++;
994
1041
      *(reg_field++)= new_field;
995
1042
    }
996
1043
    if (!--hidden_field_count)
1013
1060
  field_count= fieldnr;
1014
1061
  *reg_field= 0;
1015
1062
  *blob_field= 0;                               // End marker
1016
 
  table->getMutableShare()->setFieldSize(field_count);
 
1063
  share->fields= field_count;
1017
1064
 
1018
1065
  /* If result table is small; use a heap */
1019
1066
  /* future: storage engine selection can be made dynamic? */
1020
1067
  if (blob_count || using_unique_constraint || 
1021
 
      (session->getLex()->select_lex.options & SELECT_BIG_RESULT) ||
1022
 
      (session->getLex()->current_select->olap == ROLLUP_TYPE) ||
 
1068
      (session->lex->select_lex.options & SELECT_BIG_RESULT) ||
 
1069
      (session->lex->current_select->olap == ROLLUP_TYPE) ||
1023
1070
      (select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) == OPTION_BIG_TABLES)
1024
1071
  {
1025
 
    table->getMutableShare()->storage_engine= myisam_engine;
1026
 
    table->cursor= table->getMutableShare()->db_type()->getCursor(*table);
 
1072
    share->storage_engine= myisam_engine;
 
1073
    table->cursor= share->db_type()->getCursor(*share, table->getMemRoot());
1027
1074
    if (group &&
1028
1075
        (param->group_parts > table->cursor->getEngine()->max_key_parts() ||
1029
1076
         param->group_length > table->cursor->getEngine()->max_key_length()))
1033
1080
  }
1034
1081
  else
1035
1082
  {
1036
 
    table->getMutableShare()->storage_engine= heap_engine;
1037
 
    table->cursor= table->getMutableShare()->db_type()->getCursor(*table);
 
1083
    share->storage_engine= heap_engine;
 
1084
    table->cursor= share->db_type()->getCursor(*share, table->getMemRoot());
1038
1085
  }
1039
1086
  if (! table->cursor)
1040
1087
    goto err;
1043
1090
  if (! using_unique_constraint)
1044
1091
    reclength+= group_null_items;       // null flag is stored separately
1045
1092
 
1046
 
  table->getMutableShare()->blob_fields= blob_count;
 
1093
  share->blob_fields= blob_count;
1047
1094
  if (blob_count == 0)
1048
1095
  {
1049
1096
    /* We need to ensure that first byte is not 0 for the delete link */
1062
1109
  if (blob_count || ((string_total_length >= STRING_TOTAL_LENGTH_TO_PACK_ROWS) && (reclength / string_total_length <= RATIO_TO_PACK_ROWS || (string_total_length / string_count) >= AVG_STRING_LENGTH_TO_PACK_ROWS)))
1063
1110
    use_packed_rows= 1;
1064
1111
 
1065
 
  table->getMutableShare()->setRecordLength(reclength);
 
1112
  share->setRecordLength(reclength);
1066
1113
  {
1067
1114
    uint32_t alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
1068
 
    table->getMutableShare()->rec_buff_length= alloc_length;
1069
 
    if (!(table->record[0]= (unsigned char*) table->alloc_root(alloc_length*2)))
 
1115
    share->rec_buff_length= alloc_length;
 
1116
    if (!(table->record[0]= (unsigned char*)
 
1117
                            table->alloc_root(alloc_length*2)))
1070
1118
    {
1071
1119
      goto err;
1072
1120
    }
1073
 
    table->record[1]= table->getInsertRecord()+alloc_length;
1074
 
    table->getMutableShare()->resizeDefaultValues(alloc_length);
 
1121
    table->record[1]= table->record[0]+alloc_length;
 
1122
    share->resizeDefaultValues(alloc_length);
1075
1123
  }
1076
1124
  copy_func[0]= 0;                              // End marker
1077
1125
  param->func_count= copy_func - param->items_to_copy;
1078
1126
 
1079
 
  table->setup_tmp_table_column_bitmaps();
 
1127
  table->setup_tmp_table_column_bitmaps(bitmaps);
1080
1128
 
1081
1129
  recinfo=param->start_recinfo;
1082
 
  null_flags=(unsigned char*) table->getInsertRecord();
1083
 
  pos=table->getInsertRecord()+ null_pack_length;
 
1130
  null_flags=(unsigned char*) table->record[0];
 
1131
  pos=table->record[0]+ null_pack_length;
1084
1132
  if (null_pack_length)
1085
1133
  {
1086
1134
    memset(recinfo, 0, sizeof(*recinfo));
1089
1137
    recinfo++;
1090
1138
    memset(null_flags, 255, null_pack_length);  // Set null fields
1091
1139
 
1092
 
    table->null_flags= (unsigned char*) table->getInsertRecord();
1093
 
    table->getMutableShare()->null_fields= null_count+ hidden_null_count;
1094
 
    table->getMutableShare()->null_bytes= null_pack_length;
 
1140
    table->null_flags= (unsigned char*) table->record[0];
 
1141
    share->null_fields= null_count+ hidden_null_count;
 
1142
    share->null_bytes= null_pack_length;
1095
1143
  }
1096
1144
  null_count= (blob_count == 0) ? 1 : 0;
1097
1145
  hidden_field_count=param->hidden_field_count;
1142
1190
      ptrdiff_t diff;
1143
1191
      Field *orig_field= default_field[i];
1144
1192
      /* Get the value from default_values */
1145
 
      diff= (ptrdiff_t) (orig_field->getTable()->getDefaultValues() - orig_field->getTable()->getInsertRecord());
 
1193
      diff= (ptrdiff_t) (orig_field->table->getDefaultValues() - orig_field->table->record[0]);
1146
1194
      orig_field->move_field_offset(diff);      // Points now at default_values
1147
1195
      if (orig_field->is_real_null())
1148
1196
        field->set_null();
1151
1199
        field->set_notnull();
1152
1200
        memcpy(field->ptr, orig_field->ptr, field->pack_length());
1153
1201
      }
1154
 
      orig_field->move_field_offset(-diff);     // Back to getInsertRecord()
 
1202
      orig_field->move_field_offset(-diff);     // Back to record[0]
1155
1203
    }
1156
1204
 
1157
1205
    if (from_field[i])
1170
1218
      recinfo->type=FIELD_NORMAL;
1171
1219
    if (!--hidden_field_count)
1172
1220
      null_count=(null_count+7) & ~7;           // move to next byte
 
1221
 
 
1222
    // fix table name in field entry
 
1223
    field->table_name= &table->alias;
1173
1224
  }
1174
1225
 
1175
1226
  param->copy_field_end=copy;
1182
1233
  }
1183
1234
  else
1184
1235
  {
1185
 
    max_rows= (uint64_t) (((table->getMutableShare()->db_type() == heap_engine) ?
 
1236
    max_rows= (uint64_t) (((share->db_type() == heap_engine) ?
1186
1237
                           min(session->variables.tmp_table_size,
1187
1238
                               session->variables.max_heap_table_size) :
1188
1239
                           session->variables.tmp_table_size) /
1189
 
                          table->getMutableShare()->getRecordLength());
 
1240
                          share->getRecordLength());
1190
1241
  }
1191
1242
 
1192
1243
  set_if_bigger(max_rows, (uint64_t)1); // For dummy start options
1196
1247
  */
1197
1248
  set_if_smaller(max_rows, rows_limit);
1198
1249
 
1199
 
  table->getMutableShare()->setMaxRows(max_rows);
 
1250
  share->setMaxRows(max_rows);
1200
1251
 
1201
1252
  param->end_write_records= rows_limit;
1202
1253
 
1206
1257
  {
1207
1258
    table->group=group;                         /* Table is grouped by key */
1208
1259
    param->group_buff=group_buff;
1209
 
    table->getMutableShare()->keys=1;
1210
 
    table->getMutableShare()->uniques= test(using_unique_constraint);
 
1260
    share->keys=1;
 
1261
    share->uniques= test(using_unique_constraint);
1211
1262
    table->key_info=keyinfo;
1212
1263
    keyinfo->key_part=key_part_info;
1213
1264
    keyinfo->flags=HA_NOSAME;
1216
1267
    keyinfo->rec_per_key= 0;
1217
1268
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
1218
1269
    keyinfo->name= (char*) "group_key";
1219
 
    Order *cur_group= group;
 
1270
    order_st *cur_group= group;
1220
1271
    for (; cur_group ; cur_group= cur_group->next, key_part_info++)
1221
1272
    {
1222
1273
      Field *field=(*cur_group->item)->get_tmp_table_field();
1223
1274
      bool maybe_null=(*cur_group->item)->maybe_null;
1224
1275
      key_part_info->null_bit= 0;
1225
1276
      key_part_info->field=  field;
1226
 
      key_part_info->offset= field->offset(table->getInsertRecord());
 
1277
      key_part_info->offset= field->offset(table->record[0]);
1227
1278
      key_part_info->length= (uint16_t) field->key_length();
1228
1279
      key_part_info->type=   (uint8_t) field->key_type();
1229
1280
      key_part_info->key_type= 
1251
1302
          keyinfo->flags|= HA_NULL_ARE_EQUAL;   // def. that NULL == NULL
1252
1303
          key_part_info->null_bit=field->null_bit;
1253
1304
          key_part_info->null_offset= (uint32_t) (field->null_ptr -
1254
 
                                              (unsigned char*) table->getInsertRecord());
 
1305
                                              (unsigned char*) table->record[0]);
1255
1306
          cur_group->buff++;                        // Pointer to field data
1256
1307
          group_buff++;                         // Skipp null flag
1257
1308
        }
1278
1329
        indexes on blobs with arbitrary length. Such indexes cannot be
1279
1330
        used for lookups.
1280
1331
      */
1281
 
      table->getMutableShare()->uniques= 1;
 
1332
      share->uniques= 1;
1282
1333
    }
1283
1334
    null_pack_length-=hidden_null_pack_length;
1284
1335
    keyinfo->key_parts= ((field_count-param->hidden_field_count)+
1285
 
                         (table->getMutableShare()->uniques ? test(null_pack_length) : 0));
 
1336
                         (share->uniques ? test(null_pack_length) : 0));
1286
1337
    table->distinct= 1;
1287
 
    table->getMutableShare()->keys= 1;
 
1338
    share->keys= 1;
1288
1339
    if (!(key_part_info= (KeyPartInfo*)
1289
1340
         table->alloc_root(keyinfo->key_parts * sizeof(KeyPartInfo))))
1290
1341
      goto err;
1302
1353
      blobs can distinguish NULL from 0. This extra field is not needed
1303
1354
      when we do not use UNIQUE indexes for blobs.
1304
1355
    */
1305
 
    if (null_pack_length && table->getMutableShare()->uniques)
 
1356
    if (null_pack_length && share->uniques)
1306
1357
    {
1307
1358
      key_part_info->null_bit= 0;
1308
1359
      key_part_info->offset=hidden_null_pack_length;
1309
1360
      key_part_info->length=null_pack_length;
1310
 
      table->setVariableWidth();
1311
 
      key_part_info->field= new Field_varstring(table->getInsertRecord(),
 
1361
      key_part_info->field= new Field_varstring(table->record[0],
1312
1362
                                                (uint32_t) key_part_info->length,
1313
1363
                                                0,
1314
1364
                                                (unsigned char*) 0,
1315
1365
                                                (uint32_t) 0,
1316
1366
                                                NULL,
 
1367
                                                table->getMutableShare(),
1317
1368
                                                &my_charset_bin);
1318
1369
      if (!key_part_info->field)
1319
1370
        goto err;
1329
1380
    {
1330
1381
      key_part_info->null_bit= 0;
1331
1382
      key_part_info->field=    *reg_field;
1332
 
      key_part_info->offset=   (*reg_field)->offset(table->getInsertRecord());
 
1383
      key_part_info->offset=   (*reg_field)->offset(table->record[0]);
1333
1384
      key_part_info->length=   (uint16_t) (*reg_field)->pack_length();
1334
 
      /* @todo The below method of computing the key format length of the
 
1385
      /* TODO:
 
1386
        The below method of computing the key format length of the
1335
1387
        key part is a copy/paste from optimizer/range.cc, and table.cc.
1336
1388
        This should be factored out, e.g. as a method of Field.
1337
1389
        In addition it is not clear if any of the Field::*_length
1357
1409
 
1358
1410
  if (session->is_fatal_error)                          // If end of memory
1359
1411
    goto err;
1360
 
  table->getMutableShare()->db_record_offset= 1;
1361
 
  if (table->getShare()->db_type() == myisam_engine)
 
1412
  share->db_record_offset= 1;
 
1413
  if (share->db_type() == myisam_engine)
1362
1414
  {
1363
1415
    if (table->create_myisam_tmp_table(param->keyinfo, param->start_recinfo,
1364
1416
                                       &param->recinfo, select_options))
1381
1433
 
1382
1434
/****************************************************************************/
1383
1435
 
1384
 
void Table::column_bitmaps_set(boost::dynamic_bitset<>& read_set_arg,
1385
 
                               boost::dynamic_bitset<>& write_set_arg)
1386
 
{
1387
 
  read_set= &read_set_arg;
1388
 
  write_set= &write_set_arg;
1389
 
}
1390
 
 
1391
 
 
1392
 
const boost::dynamic_bitset<> Table::use_all_columns(boost::dynamic_bitset<>& in_map)
1393
 
{
1394
 
  const boost::dynamic_bitset<> old= in_map;
1395
 
  in_map= getShare()->all_set;
 
1436
/**
 
1437
  Create a reduced Table object with properly set up Field list from a
 
1438
  list of field definitions.
 
1439
 
 
1440
    The created table doesn't have a table Cursor associated with
 
1441
    it, has no keys, no group/distinct, no copy_funcs array.
 
1442
    The sole purpose of this Table object is to use the power of Field
 
1443
    class to read/write data to/from table->record[0]. Then one can store
 
1444
    the record in any container (RB tree, hash, etc).
 
1445
    The table is created in Session mem_root, so are the table's fields.
 
1446
    Consequently, if you don't BLOB fields, you don't need to free it.
 
1447
 
 
1448
  @param session         connection handle
 
1449
  @param field_list  list of column definitions
 
1450
 
 
1451
  @return
 
1452
    0 if out of memory, Table object in case of success
 
1453
*/
 
1454
 
 
1455
Table *Session::create_virtual_tmp_table(List<CreateField> &field_list)
 
1456
{
 
1457
  uint32_t field_count= field_list.elements;
 
1458
  uint32_t blob_count= 0;
 
1459
  Field **field;
 
1460
  CreateField *cdef;                           /* column definition */
 
1461
  uint32_t record_length= 0;
 
1462
  uint32_t null_count= 0;                 /* number of columns which may be null */
 
1463
  uint32_t null_pack_length;              /* NULL representation array length */
 
1464
  unsigned char *bitmaps;
 
1465
  Table *table;
 
1466
 
 
1467
  TableShareInstance *share= getTemporaryShare(message::Table::INTERNAL); // This will not go into the tableshare cache, so no key is used.
 
1468
 
 
1469
  if (! share->getMemRoot()->multi_alloc_root(0,
 
1470
                                              &bitmaps, bitmap_buffer_size(field_count)*2,
 
1471
                                              NULL))
 
1472
  {
 
1473
    return NULL;
 
1474
  }
 
1475
 
 
1476
  table= share->getTable();
 
1477
  share->setFields(field_count + 1);
 
1478
  table->setFields(share->getFields(true));
 
1479
  field= share->getFields(true);
 
1480
  share->blob_field.resize(field_count+1);
 
1481
  share->fields= field_count;
 
1482
  share->blob_ptr_size= portable_sizeof_char_ptr;
 
1483
  table->setup_tmp_table_column_bitmaps(bitmaps);
 
1484
 
 
1485
  table->in_use= this;           /* field->reset() may access table->in_use */
 
1486
 
 
1487
  /* Create all fields and calculate the total length of record */
 
1488
  List_iterator_fast<CreateField> it(field_list);
 
1489
  while ((cdef= it++))
 
1490
  {
 
1491
    *field= share->make_field(NULL,
 
1492
                              cdef->length,
 
1493
                              (cdef->flags & NOT_NULL_FLAG) ? false : true,
 
1494
                              (unsigned char *) ((cdef->flags & NOT_NULL_FLAG) ? 0 : ""),
 
1495
                              (cdef->flags & NOT_NULL_FLAG) ? 0 : 1,
 
1496
                              cdef->decimals,
 
1497
                              cdef->sql_type,
 
1498
                              cdef->charset,
 
1499
                              cdef->unireg_check,
 
1500
                              cdef->interval,
 
1501
                              cdef->field_name);
 
1502
    if (!*field)
 
1503
      goto error;
 
1504
    (*field)->init(table);
 
1505
    record_length+= (*field)->pack_length();
 
1506
    if (! ((*field)->flags & NOT_NULL_FLAG))
 
1507
      null_count++;
 
1508
 
 
1509
    if ((*field)->flags & BLOB_FLAG)
 
1510
      share->blob_field[blob_count++]= (uint32_t) (field - table->getFields());
 
1511
 
 
1512
    field++;
 
1513
  }
 
1514
  *field= NULL;                             /* mark the end of the list */
 
1515
  share->blob_field[blob_count]= 0;            /* mark the end of the list */
 
1516
  share->blob_fields= blob_count;
 
1517
 
 
1518
  null_pack_length= (null_count + 7)/8;
 
1519
  share->setRecordLength(record_length + null_pack_length);
 
1520
  share->rec_buff_length= ALIGN_SIZE(share->getRecordLength() + 1);
 
1521
  table->record[0]= (unsigned char*)alloc(share->rec_buff_length);
 
1522
  if (!table->record[0])
 
1523
    goto error;
 
1524
 
 
1525
  if (null_pack_length)
 
1526
  {
 
1527
    table->null_flags= (unsigned char*) table->record[0];
 
1528
    share->null_fields= null_count;
 
1529
    share->null_bytes= null_pack_length;
 
1530
  }
 
1531
  {
 
1532
    /* Set up field pointers */
 
1533
    unsigned char *null_pos= table->record[0];
 
1534
    unsigned char *field_pos= null_pos + share->null_bytes;
 
1535
    uint32_t null_bit= 1;
 
1536
 
 
1537
    for (field= table->getFields(); *field; ++field)
 
1538
    {
 
1539
      Field *cur_field= *field;
 
1540
      if ((cur_field->flags & NOT_NULL_FLAG))
 
1541
        cur_field->move_field(field_pos);
 
1542
      else
 
1543
      {
 
1544
        cur_field->move_field(field_pos, (unsigned char*) null_pos, null_bit);
 
1545
        null_bit<<= 1;
 
1546
        if (null_bit == (1 << 8))
 
1547
        {
 
1548
          ++null_pos;
 
1549
          null_bit= 1;
 
1550
        }
 
1551
      }
 
1552
      cur_field->reset();
 
1553
 
 
1554
      field_pos+= cur_field->pack_length();
 
1555
    }
 
1556
  }
 
1557
 
 
1558
  return table;
 
1559
 
 
1560
error:
 
1561
  for (field= table->getFields(); *field; ++field)
 
1562
  {
 
1563
    delete *field;                         /* just invokes field destructor */
 
1564
  }
 
1565
  return 0;
 
1566
}
 
1567
 
 
1568
bool Table::open_tmp_table()
 
1569
{
 
1570
  int error;
 
1571
  
 
1572
  TableIdentifier identifier(s->getSchemaName(), s->getTableName(), s->getPath());
 
1573
  if ((error=cursor->ha_open(identifier,
 
1574
                             this,
 
1575
                             s->getTableName(),
 
1576
                             O_RDWR,
 
1577
                             HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
 
1578
  {
 
1579
    print_error(error, MYF(0));
 
1580
    db_stat= 0;
 
1581
    return true;
 
1582
  }
 
1583
  (void) cursor->extra(HA_EXTRA_QUICK);         /* Faster */
 
1584
  return false;
 
1585
}
 
1586
 
 
1587
 
 
1588
/*
 
1589
  Create MyISAM temporary table
 
1590
 
 
1591
  SYNOPSIS
 
1592
    create_myisam_tmp_table()
 
1593
      keyinfo         Description of the index (there is always one index)
 
1594
      start_recinfo   MyISAM's column descriptions
 
1595
      recinfo INOUT   End of MyISAM's column descriptions
 
1596
      options         Option bits
 
1597
 
 
1598
  DESCRIPTION
 
1599
    Create a MyISAM temporary table according to passed description. The is
 
1600
    assumed to have one unique index or constraint.
 
1601
 
 
1602
    The passed array or MI_COLUMNDEF structures must have this form:
 
1603
 
 
1604
      1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
 
1605
         when there are many nullable columns)
 
1606
      2. Table columns
 
1607
      3. One free MI_COLUMNDEF element (*recinfo points here)
 
1608
 
 
1609
    This function may use the free element to create hash column for unique
 
1610
    constraint.
 
1611
 
 
1612
   RETURN
 
1613
     false - OK
 
1614
     true  - Error
 
1615
*/
 
1616
 
 
1617
bool Table::create_myisam_tmp_table(KeyInfo *keyinfo,
 
1618
                                    MI_COLUMNDEF *start_recinfo,
 
1619
                                    MI_COLUMNDEF **recinfo,
 
1620
                                    uint64_t options)
 
1621
{
 
1622
  int error;
 
1623
  MI_KEYDEF keydef;
 
1624
  MI_UNIQUEDEF uniquedef;
 
1625
  TableShare *share= s;
 
1626
 
 
1627
  if (share->sizeKeys())
 
1628
  {                                             // Get keys for ni_create
 
1629
    bool using_unique_constraint= false;
 
1630
    HA_KEYSEG *seg= (HA_KEYSEG*) this->mem_root.alloc_root(sizeof(*seg) * keyinfo->key_parts);
 
1631
    if (!seg)
 
1632
      goto err;
 
1633
 
 
1634
    memset(seg, 0, sizeof(*seg) * keyinfo->key_parts);
 
1635
    if (keyinfo->key_length >= cursor->getEngine()->max_key_length() ||
 
1636
        keyinfo->key_parts > cursor->getEngine()->max_key_parts() ||
 
1637
        share->uniques)
 
1638
    {
 
1639
      /* Can't create a key; Make a unique constraint instead of a key */
 
1640
      share->keys=    0;
 
1641
      share->uniques= 1;
 
1642
      using_unique_constraint= true;
 
1643
      memset(&uniquedef, 0, sizeof(uniquedef));
 
1644
      uniquedef.keysegs=keyinfo->key_parts;
 
1645
      uniquedef.seg=seg;
 
1646
      uniquedef.null_are_equal=1;
 
1647
 
 
1648
      /* Create extra column for hash value */
 
1649
      memset(*recinfo, 0, sizeof(**recinfo));
 
1650
      (*recinfo)->type= FIELD_CHECK;
 
1651
      (*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
 
1652
      (*recinfo)++;
 
1653
      share->setRecordLength(share->getRecordLength() + MI_UNIQUE_HASH_LENGTH);
 
1654
    }
 
1655
    else
 
1656
    {
 
1657
      /* Create an unique key */
 
1658
      memset(&keydef, 0, sizeof(keydef));
 
1659
      keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
 
1660
      keydef.keysegs=  keyinfo->key_parts;
 
1661
      keydef.seg= seg;
 
1662
    }
 
1663
    for (uint32_t i= 0; i < keyinfo->key_parts ; i++,seg++)
 
1664
    {
 
1665
      Field *key_field=keyinfo->key_part[i].field;
 
1666
      seg->flag=     0;
 
1667
      seg->language= key_field->charset()->number;
 
1668
      seg->length=   keyinfo->key_part[i].length;
 
1669
      seg->start=    keyinfo->key_part[i].offset;
 
1670
      if (key_field->flags & BLOB_FLAG)
 
1671
      {
 
1672
        seg->type= ((keyinfo->key_part[i].key_type & 1 /* binary */) ?
 
1673
         HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
 
1674
        seg->bit_start= (uint8_t)(key_field->pack_length()
 
1675
                                  - share->blob_ptr_size);
 
1676
        seg->flag= HA_BLOB_PART;
 
1677
        seg->length= 0;                 // Whole blob in unique constraint
 
1678
      }
 
1679
      else
 
1680
      {
 
1681
        seg->type= keyinfo->key_part[i].type;
 
1682
      }
 
1683
      if (!(key_field->flags & NOT_NULL_FLAG))
 
1684
      {
 
1685
        seg->null_bit= key_field->null_bit;
 
1686
        seg->null_pos= (uint32_t) (key_field->null_ptr - (unsigned char*) record[0]);
 
1687
        /*
 
1688
          We are using a GROUP BY on something that contains NULL
 
1689
          In this case we have to tell MyISAM that two NULL should
 
1690
          on INSERT be regarded at the same value
 
1691
        */
 
1692
        if (! using_unique_constraint)
 
1693
          keydef.flag|= HA_NULL_ARE_EQUAL;
 
1694
      }
 
1695
    }
 
1696
  }
 
1697
  MI_CREATE_INFO create_info;
 
1698
  memset(&create_info, 0, sizeof(create_info));
 
1699
 
 
1700
  if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
 
1701
      OPTION_BIG_TABLES)
 
1702
    create_info.data_file_length= ~(uint64_t) 0;
 
1703
 
 
1704
  if ((error=mi_create(share->getTableName(), share->sizeKeys(), &keydef,
 
1705
                       (uint32_t) (*recinfo-start_recinfo),
 
1706
                       start_recinfo,
 
1707
                       share->uniques, &uniquedef,
 
1708
                       &create_info,
 
1709
                       HA_CREATE_TMP_TABLE)))
 
1710
  {
 
1711
    print_error(error, MYF(0));
 
1712
    db_stat= 0;
 
1713
    goto err;
 
1714
  }
 
1715
  status_var_increment(in_use->status_var.created_tmp_disk_tables);
 
1716
  share->db_record_offset= 1;
 
1717
  return false;
 
1718
 err:
 
1719
  return true;
 
1720
}
 
1721
 
 
1722
 
 
1723
void Table::free_tmp_table(Session *session)
 
1724
{
 
1725
  memory::Root own_root= mem_root;
 
1726
  const char *save_proc_info;
 
1727
 
 
1728
  save_proc_info= session->get_proc_info();
 
1729
  session->set_proc_info("removing tmp table");
 
1730
 
 
1731
  // Release latches since this can take a long time
 
1732
  plugin::TransactionalStorageEngine::releaseTemporaryLatches(session);
 
1733
 
 
1734
  if (cursor)
 
1735
  {
 
1736
    if (db_stat)
 
1737
    {
 
1738
      cursor->closeMarkForDelete(s->getTableName());
 
1739
    }
 
1740
 
 
1741
    TableIdentifier identifier(s->getSchemaName(), s->getTableName(), s->getTableName());
 
1742
    s->db_type()->doDropTable(*session, identifier);
 
1743
 
 
1744
    delete cursor;
 
1745
  }
 
1746
 
 
1747
  /* free blobs */
 
1748
  for (Field **ptr= field ; *ptr ; ptr++)
 
1749
  {
 
1750
    (*ptr)->free();
 
1751
  }
 
1752
  free_io_cache();
 
1753
 
 
1754
  own_root.free_root(MYF(0)); /* the table is allocated in its own root */
 
1755
  session->set_proc_info(save_proc_info);
 
1756
}
 
1757
 
 
1758
my_bitmap_map *Table::use_all_columns(MyBitmap *bitmap)
 
1759
{
 
1760
  my_bitmap_map *old= bitmap->getBitmap();
 
1761
  bitmap->setBitmap(s->all_set.getBitmap());
1396
1762
  return old;
1397
1763
}
1398
1764
 
1399
 
void Table::restore_column_map(const boost::dynamic_bitset<>& old)
 
1765
void Table::restore_column_map(my_bitmap_map *old)
1400
1766
{
1401
 
  for (boost::dynamic_bitset<>::size_type i= 0; i < old.size(); i++)
1402
 
  {
1403
 
    if (old.test(i))
1404
 
    {
1405
 
      read_set->set(i);
1406
 
    }
1407
 
    else
1408
 
    {
1409
 
      read_set->reset(i);
1410
 
    }
1411
 
  }
 
1767
  read_set->setBitmap(old);
1412
1768
}
1413
1769
 
1414
1770
uint32_t Table::find_shortest_key(const key_map *usable_keys)
1417
1773
  uint32_t best= MAX_KEY;
1418
1774
  if (usable_keys->any())
1419
1775
  {
1420
 
    for (uint32_t nr= 0; nr < getShare()->sizeKeys() ; nr++)
 
1776
    for (uint32_t nr= 0; nr < s->sizeKeys() ; nr++)
1421
1777
    {
1422
1778
      if (usable_keys->test(nr))
1423
1779
      {
1444
1800
{
1445
1801
  for (; *ptr ; ptr++)
1446
1802
  {
1447
 
    if ((*ptr)->cmp_offset(getShare()->rec_buff_length))
 
1803
    if ((*ptr)->cmp_offset(s->rec_buff_length))
1448
1804
      return true;
1449
1805
  }
1450
1806
  return false;
1451
1807
}
1452
1808
 
1453
 
/**
1454
 
   True if the table's input and output record buffers are comparable using
1455
 
   compare_records(TABLE*).
1456
 
 */
1457
 
bool Table::records_are_comparable()
1458
 
{
1459
 
  return ((getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ) == 0) ||
1460
 
          write_set->is_subset_of(*read_set));
1461
 
}
1462
 
 
1463
 
/**
1464
 
   Compares the input and outbut record buffers of the table to see if a row
1465
 
   has changed. The algorithm iterates over updated columns and if they are
1466
 
   nullable compares NULL bits in the buffer before comparing actual
1467
 
   data. Special care must be taken to compare only the relevant NULL bits and
1468
 
   mask out all others as they may be undefined. The storage engine will not
1469
 
   and should not touch them.
1470
 
 
1471
 
   @param table The table to evaluate.
1472
 
 
1473
 
   @return true if row has changed.
1474
 
   @return false otherwise.
1475
 
*/
1476
 
bool Table::compare_records()
1477
 
{
1478
 
  if (getEngine()->check_flag(HTON_BIT_PARTIAL_COLUMN_READ) != 0)
1479
 
  {
1480
 
    /*
1481
 
      Storage engine may not have read all columns of the record.  Fields
1482
 
      (including NULL bits) not in the write_set may not have been read and
1483
 
      can therefore not be compared.
1484
 
    */
1485
 
    for (Field **ptr= this->field ; *ptr != NULL; ptr++)
1486
 
    {
1487
 
      Field *f= *ptr;
1488
 
      if (write_set->test(f->position()))
1489
 
      {
1490
 
        if (f->real_maybe_null())
1491
 
        {
1492
 
          unsigned char null_byte_index= f->null_ptr - record[0];
1493
 
 
1494
 
          if (((record[0][null_byte_index]) & f->null_bit) !=
1495
 
              ((record[1][null_byte_index]) & f->null_bit))
1496
 
            return true;
1497
 
        }
1498
 
        if (f->cmp_binary_offset(getShare()->rec_buff_length))
1499
 
          return true;
1500
 
      }
1501
 
    }
1502
 
    return false;
1503
 
  }
1504
 
 
1505
 
  /*
1506
 
    The storage engine has read all columns, so it's safe to compare all bits
1507
 
    including those not in the write_set. This is cheaper than the
1508
 
    field-by-field comparison done above.
1509
 
  */
1510
 
  if (not getShare()->blob_fields + getShare()->hasVariableWidth())
1511
 
    // Fixed-size record: do bitwise comparison of the records
1512
 
    return memcmp(this->getInsertRecord(), this->getUpdateRecord(), (size_t) getShare()->getRecordLength());
1513
 
 
 
1809
/* Return false if row hasn't changed */
 
1810
 
 
1811
bool Table::compare_record()
 
1812
{
 
1813
  if (s->blob_fields + s->varchar_fields == 0)
 
1814
    return memcmp(this->record[0], this->record[1], (size_t) s->getRecordLength());
 
1815
  
1514
1816
  /* Compare null bits */
1515
 
  if (memcmp(null_flags, null_flags + getShare()->rec_buff_length, getShare()->null_bytes))
 
1817
  if (memcmp(null_flags, null_flags + s->rec_buff_length, s->null_bytes))
1516
1818
    return true; /* Diff in NULL value */
1517
1819
 
1518
1820
  /* Compare updated fields */
1519
1821
  for (Field **ptr= field ; *ptr ; ptr++)
1520
1822
  {
1521
 
    if (isWriteSet((*ptr)->position()) &&
1522
 
        (*ptr)->cmp_binary_offset(getShare()->rec_buff_length))
 
1823
    if (isWriteSet((*ptr)->field_index) &&
 
1824
        (*ptr)->cmp_binary_offset(s->rec_buff_length))
1523
1825
      return true;
1524
1826
  }
1525
1827
  return false;
1531
1833
 */
1532
1834
void Table::storeRecord()
1533
1835
{
1534
 
  memcpy(getUpdateRecord(), getInsertRecord(), (size_t) getShare()->getRecordLength());
 
1836
  memcpy(record[1], record[0], (size_t) s->getRecordLength());
1535
1837
}
1536
1838
 
1537
1839
/*
1540
1842
 */
1541
1843
void Table::storeRecordAsInsert()
1542
1844
{
1543
 
  assert(insert_values.size() >= getShare()->getRecordLength());
1544
 
  memcpy(&insert_values[0], getInsertRecord(), (size_t) getShare()->getRecordLength());
 
1845
  memcpy(insert_values, record[0], (size_t) s->getRecordLength());
1545
1846
}
1546
1847
 
1547
1848
/*
1550
1851
 */
1551
1852
void Table::storeRecordAsDefault()
1552
1853
{
1553
 
  memcpy(getMutableShare()->getDefaultValues(), getInsertRecord(), (size_t) getShare()->getRecordLength());
 
1854
  memcpy(s->getDefaultValues(), record[0], (size_t) s->getRecordLength());
1554
1855
}
1555
1856
 
1556
1857
/*
1559
1860
 */
1560
1861
void Table::restoreRecord()
1561
1862
{
1562
 
  memcpy(getInsertRecord(), getUpdateRecord(), (size_t) getShare()->getRecordLength());
 
1863
  memcpy(record[0], record[1], (size_t) s->getRecordLength());
1563
1864
}
1564
1865
 
1565
1866
/*
1568
1869
 */
1569
1870
void Table::restoreRecordAsDefault()
1570
1871
{
1571
 
  memcpy(getInsertRecord(), getMutableShare()->getDefaultValues(), (size_t) getShare()->getRecordLength());
 
1872
  memcpy(record[0], s->getDefaultValues(), (size_t) s->getRecordLength());
1572
1873
}
1573
1874
 
1574
1875
/*
1578
1879
void Table::emptyRecord()
1579
1880
{
1580
1881
  restoreRecordAsDefault();
1581
 
  memset(null_flags, 255, getShare()->null_bytes);
 
1882
  memset(null_flags, 255, s->null_bytes);
1582
1883
}
1583
1884
 
1584
1885
Table::Table() : 
 
1886
  s(NULL),
1585
1887
  field(NULL),
1586
1888
  cursor(NULL),
1587
1889
  next(NULL),
1590
1892
  write_set(NULL),
1591
1893
  tablenr(0),
1592
1894
  db_stat(0),
1593
 
  def_read_set(),
1594
 
  def_write_set(),
1595
 
  tmp_set(),
1596
1895
  in_use(NULL),
 
1896
  insert_values(NULL),
1597
1897
  key_info(NULL),
1598
1898
  next_number_field(NULL),
1599
1899
  found_next_number_field(NULL),
1600
1900
  timestamp_field(NULL),
1601
1901
  pos_in_table_list(NULL),
1602
1902
  group(NULL),
 
1903
  alias(NULL),
1603
1904
  null_flags(NULL),
1604
1905
  lock_position(0),
1605
1906
  lock_data_start(0),
1626
1927
  quick_condition_rows(0),
1627
1928
  timestamp_field_type(TIMESTAMP_NO_AUTO_SET),
1628
1929
  map(0),
1629
 
  quick_rows(),
1630
 
  const_key_parts(),
1631
 
  quick_key_parts(),
1632
 
  quick_n_ranges()
 
1930
  is_placeholder_created(0)
1633
1931
{
 
1932
  memset(&def_read_set, 0, sizeof(MyBitmap)); /**< Default read set of columns */
 
1933
  memset(&def_write_set, 0, sizeof(MyBitmap)); /**< Default write set of columns */
 
1934
  memset(&tmp_set, 0, sizeof(MyBitmap)); /* Not sure about this... */
 
1935
 
1634
1936
  record[0]= (unsigned char *) 0;
1635
1937
  record[1]= (unsigned char *) 0;
 
1938
 
 
1939
  reginfo.reset();
 
1940
  covering_keys.reset();
 
1941
  quick_keys.reset();
 
1942
  merge_keys.reset();
 
1943
 
 
1944
  keys_in_use_for_query.reset();
 
1945
  keys_in_use_for_group_by.reset();
 
1946
  keys_in_use_for_order_by.reset();
 
1947
 
 
1948
  memset(quick_rows, 0, sizeof(ha_rows) * MAX_KEY);
 
1949
  memset(const_key_parts, 0, sizeof(ha_rows) * MAX_KEY);
 
1950
 
 
1951
  memset(quick_key_parts, 0, sizeof(unsigned int) * MAX_KEY);
 
1952
  memset(quick_n_ranges, 0, sizeof(unsigned int) * MAX_KEY);
 
1953
 
 
1954
  memset(&mem_root, 0, sizeof(memory::Root));
 
1955
  memset(&sort, 0, sizeof(filesort_info_st));
1636
1956
}
1637
1957
 
1638
1958
/*****************************************************************************
1654
1974
    print them to the .err log
1655
1975
  */
1656
1976
  if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
1657
 
    errmsg_printf(error::ERROR, _("Got error %d when reading table '%s'"),
1658
 
                  error, getShare()->getPath());
 
1977
    errmsg_printf(ERRMSG_LVL_ERROR, _("Got error %d when reading table '%s'"),
 
1978
                  error, s->getPath());
1659
1979
  print_error(error, MYF(0));
1660
1980
 
1661
1981
  return 1;
1678
1998
  tablenr= table_number;
1679
1999
  map= (table_map) 1 << table_number;
1680
2000
  force_index= table_list->force_index;
1681
 
  covering_keys= getShare()->keys_for_keyread;
 
2001
  covering_keys= s->keys_for_keyread;
1682
2002
  merge_keys.reset();
1683
2003
}
1684
2004
 
1698
2018
  return false;
1699
2019
}
1700
2020
 
1701
 
 
1702
 
void Table::filesort_free_buffers(bool full)
1703
 
{
1704
 
  if (sort.record_pointers)
1705
 
  {
1706
 
    free((unsigned char*) sort.record_pointers);
1707
 
    sort.record_pointers=0;
1708
 
  }
1709
 
  if (full)
1710
 
  {
1711
 
    if (sort.sort_keys )
1712
 
    {
1713
 
      if ((unsigned char*) sort.sort_keys)
1714
 
        free((unsigned char*) sort.sort_keys);
1715
 
      sort.sort_keys= 0;
1716
 
    }
1717
 
    if (sort.buffpek)
1718
 
    {
1719
 
      if ((unsigned char*) sort.buffpek)
1720
 
        free((unsigned char*) sort.buffpek);
1721
 
      sort.buffpek= 0;
1722
 
      sort.buffpek_len= 0;
1723
 
    }
1724
 
  }
1725
 
 
1726
 
  if (sort.addon_buf)
1727
 
  {
1728
 
    free((char *) sort.addon_buf);
1729
 
    free((char *) sort.addon_field);
1730
 
    sort.addon_buf=0;
1731
 
    sort.addon_field=0;
1732
 
  }
1733
 
}
1734
 
 
1735
 
/*
1736
 
  Is this instance of the table should be reopen or represents a name-lock?
1737
 
*/
1738
 
bool Table::needs_reopen_or_name_lock() const
1739
 
1740
 
  return getShare()->getVersion() != refresh_version;
1741
 
}
1742
 
 
1743
 
uint32_t Table::index_flags(uint32_t idx) const
1744
 
{
1745
 
  return getShare()->getEngine()->index_flags(getShare()->getKeyInfo(idx).algorithm);
1746
 
}
1747
 
 
1748
 
void Table::print_error(int error, myf errflag) const
1749
 
{
1750
 
  getShare()->getEngine()->print_error(error, errflag, *this);
1751
 
}
1752
 
 
1753
2021
} /* namespace drizzled */