~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table.cc

Read Fields out of proto instead of FRM.

As non-radioactive fallout:
- no implicit defaults (see http://bugs.mysql.com/bug.php?id=43151)
- no real defaults for BLOB yet... but this paves the way (and it shouldn't be too hard now)

As radioactive fallout:
- parse_table_proto is approaching the length of War and Peace
- handling of corrupted proto is about non-existant. abort() is not ideal

Show diffs side-by-side

added added

removed removed

Lines of Context:
653
653
  share->vfields= 0;
654
654
  share->stored_fields= share->fields;
655
655
 
656
 
/*  share->field= (Field**) alloc_root(&share->mem_root,
657
 
                                     (share->fields * sizeof(Field*)));
658
 
*/
 
656
  share->field= (Field**) alloc_root(&share->mem_root,
 
657
                                     ((share->fields+1) * sizeof(Field*)));
 
658
  share->field[share->fields]= NULL;
 
659
 
659
660
  uint32_t null_fields= 0;
660
661
  share->reclength= 0;
661
662
 
794
795
  else
795
796
    share->intervals= NULL;
796
797
 
797
 
  /* Now fix the TYPELIBs for the intervals (enum values) */
 
798
  share->fieldnames.type_names= (const char**)alloc_root(&share->mem_root,
 
799
                                  (share->fields+1)*sizeof(char*));
 
800
 
 
801
  share->fieldnames.type_lengths= (unsigned int*) alloc_root(&share->mem_root,
 
802
                                  (share->fields+1)*sizeof(unsigned int));
 
803
 
 
804
  share->fieldnames.type_names[share->fields]= NULL;
 
805
  share->fieldnames.type_lengths[share->fields]= 0;
 
806
  share->fieldnames.count= share->fields;
 
807
 
 
808
 
 
809
  /* Now fix the TYPELIBs for the intervals (enum values)
 
810
     and field names.
 
811
   */
798
812
 
799
813
  uint32_t interval_nr= 0;
800
814
 
802
816
  {
803
817
    drizzle::Table::Field pfield= table.field(fieldnr);
804
818
 
 
819
    /* field names */
 
820
    share->fieldnames.type_names[fieldnr]= strmake_root(&share->mem_root,
 
821
                                                        pfield.name().c_str(),
 
822
                                                        pfield.name().length());
 
823
 
 
824
    share->fieldnames.type_lengths[fieldnr]= pfield.name().length();
 
825
 
 
826
    /* enum typelibs */
805
827
    if(pfield.type() != drizzle::Table::Field::ENUM)
806
828
      continue;
807
829
 
808
830
    drizzle::Table::Field::SetFieldOptions field_options=
809
831
      pfield.set_options();
810
832
 
 
833
    const CHARSET_INFO *charset= get_charset(field_options.has_collation_id()?
 
834
                                             field_options.collation_id() : 0);
 
835
 
 
836
    if (!charset)
 
837
      charset= default_charset_info;
 
838
 
811
839
    TYPELIB *t= &(share->intervals[interval_nr]);
812
840
 
813
841
    t->type_names= (const char**)alloc_root(&share->mem_root,
828
856
                                     field_options.field_value(n).c_str(),
829
857
                                     field_options.field_value(n).length());
830
858
 
831
 
      t->type_lengths[n]= field_options.field_value(n).length();
 
859
      /* Go ask the charset what the length is as for "" length=1
 
860
         and there's stripping spaces or some other crack going on.
 
861
       */
 
862
      uint32_t lengthsp;
 
863
      lengthsp= charset->cset->lengthsp(charset, t->type_names[n],
 
864
                                        field_options.field_value(n).length());
 
865
      t->type_lengths[n]= lengthsp;
832
866
    }
833
867
    interval_nr++;
834
868
  }
839
873
 
840
874
  interval_nr= 0;
841
875
 
 
876
  bool use_hash= share->fields >= MAX_FIELDS_BEFORE_HASH;
 
877
 
 
878
  if(use_hash)
 
879
    use_hash= !hash_init(&share->name_hash,
 
880
                         system_charset_info,
 
881
                         share->fields, 0, 0,
 
882
                         (hash_get_key) get_field_name, 0, 0);
 
883
 
 
884
  unsigned char* null_pos= record;;
 
885
  int null_bit_pos= (table_options.pack_record()) ? 0 : 1;
 
886
 
842
887
  for(unsigned int fieldnr=0; fieldnr < share->fields; fieldnr++)
843
888
  {
844
889
    drizzle::Table::Field pfield= table.field(fieldnr);
947
992
 
948
993
    }
949
994
 
 
995
    if(field_type==DRIZZLE_TYPE_ENUM)
 
996
    {
 
997
      drizzle::Table::Field::SetFieldOptions field_options=
 
998
        pfield.set_options();
 
999
 
 
1000
      charset= get_charset(field_options.has_collation_id()?
 
1001
                           field_options.collation_id() : 0);
 
1002
 
 
1003
      if (!charset)
 
1004
        charset= default_charset_info;
 
1005
 
 
1006
    }
 
1007
 
950
1008
    Item *default_value= NULL;
951
1009
 
952
1010
    if(pfield.options().has_default_value()
962
1020
 
963
1021
    uint32_t pack_flag= pfield.pack_flag(); /* TODO: MUST DIE */
964
1022
 
965
 
    TABLE_SHARE temp_share;
966
 
    Table temp_table;
967
 
 
968
 
    memset(&temp_share, 0, sizeof(temp_share));
 
1023
    Table temp_table; /* Use this so that BLOB DEFAULT '' works */
969
1024
    memset(&temp_table, 0, sizeof(temp_table));
970
 
    temp_table.s= &temp_share;
 
1025
    temp_table.s= share;
971
1026
    temp_table.in_use= session;
972
1027
    temp_table.s->db_low_byte_first= 1; //handler->low_byte_first();
973
1028
    temp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
974
1029
 
975
 
    Field* f= make_field(&temp_share, record+recordpos+data_offset,
 
1030
    Field* f= make_field(share, &share->mem_root, record+recordpos+data_offset,
976
1031
                         pfield.options().length(),
977
 
                         record + null_count / 8,
978
 
                         null_count & 7,
 
1032
                         null_pos,
 
1033
                         null_bit_pos,
979
1034
                         pack_flag,
980
1035
                         field_type,
981
1036
                         charset,
983
1038
                         ((field_type==DRIZZLE_TYPE_ENUM)?
984
1039
                         share->intervals+(interval_nr++)
985
1040
                         : (TYPELIB*) 0),
986
 
                         pfield.name().c_str());
987
 
 
988
 
    f->init(&temp_table);
 
1041
                        share->fieldnames.type_names[fieldnr]);
 
1042
 
 
1043
    share->field[fieldnr]= f;
 
1044
 
 
1045
    f->init(&temp_table); /* blob default values need table obj */
989
1046
 
990
1047
    if(!(f->flags & NOT_NULL_FLAG))
991
1048
    {
992
1049
      *f->null_ptr|= f->null_bit;
 
1050
      if (!(null_bit_pos= (null_bit_pos + 1) & 7))
 
1051
        null_pos++;
993
1052
      null_count++;
994
1053
    }
995
1054
 
1007
1066
    else
1008
1067
      f->reset();
1009
1068
 
 
1069
    /* hack to undo f->init() */
 
1070
    f->table= NULL;
 
1071
    f->orig_table= NULL;
 
1072
 
1010
1073
    recordpos+= field_pack_length[fieldnr];
 
1074
 
 
1075
    f->field_index= fieldnr;
 
1076
    f->comment= comment;
 
1077
    f->vcol_info= vcol_info;
 
1078
    f->is_stored= field_is_stored;
 
1079
    if(!default_value
 
1080
       && !(f->unireg_check==Field::NEXT_NUMBER)
 
1081
       && (f->flags & NOT_NULL_FLAG)
 
1082
       && (f->real_type() != DRIZZLE_TYPE_TIMESTAMP))
 
1083
      f->flags|= NO_DEFAULT_VALUE_FLAG;
 
1084
 
 
1085
    if(f->unireg_check == Field::NEXT_NUMBER)
 
1086
      share->found_next_number_field= &(share->field[fieldnr]);
 
1087
 
 
1088
    if(share->timestamp_field == f)
 
1089
      share->timestamp_field_offset= fieldnr;
 
1090
 
 
1091
    if (use_hash) /* supposedly this never fails... but comments lie */
 
1092
      (void) my_hash_insert(&share->name_hash,
 
1093
                            (unsigned char*)&(share->field[fieldnr]));
 
1094
 
 
1095
    if(!f->is_stored)
 
1096
    {
 
1097
      share->stored_fields--;
 
1098
      if(share->stored_rec_length>=recordpos)
 
1099
        share->stored_rec_length= recordpos-1;
 
1100
    }
 
1101
 
1011
1102
  }
1012
1103
 
1013
1104
  /*
1018
1109
  if (null_count & 7)
1019
1110
    *(record + null_count / 8)|= ~(((unsigned char) 1 << (null_count & 7)) - 1);
1020
1111
 
 
1112
  share->null_bytes= (null_pos - (unsigned char*) record +
 
1113
                      (null_bit_pos + 7) / 8);
 
1114
 
 
1115
  share->last_null_bit_pos= null_bit_pos;
 
1116
 
1021
1117
  free(field_offsets);
1022
1118
  free(field_pack_length);
1023
1119
 
 
1120
  handler *handler_file;
 
1121
 
 
1122
  if(!(handler_file= get_new_handler(share, session->mem_root,
 
1123
                                     share->db_type())))
 
1124
    abort(); // FIXME
 
1125
 
 
1126
  /* Fix key stuff */
 
1127
  if (share->key_parts)
 
1128
  {
 
1129
    uint32_t primary_key=(uint32_t) (find_type((char*) "PRIMARY",
 
1130
                                       &share->keynames, 3) - 1);
 
1131
 
 
1132
    int64_t ha_option= handler_file->ha_table_flags();
 
1133
 
 
1134
    keyinfo= share->key_info;
 
1135
    key_part= keyinfo->key_part;
 
1136
 
 
1137
    for (uint32_t key=0 ; key < share->keys ; key++,keyinfo++)
 
1138
    {
 
1139
      uint32_t usable_parts= 0;
 
1140
 
 
1141
      if (primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
 
1142
      {
 
1143
        /*
 
1144
          If the UNIQUE key doesn't have NULL columns and is not a part key
 
1145
          declare this as a primary key.
 
1146
        */
 
1147
        primary_key=key;
 
1148
        for (uint32_t i=0 ; i < keyinfo->key_parts ;i++)
 
1149
        {
 
1150
          uint32_t fieldnr= key_part[i].fieldnr;
 
1151
          if (!fieldnr ||
 
1152
              share->field[fieldnr-1]->null_ptr ||
 
1153
              share->field[fieldnr-1]->key_length() !=
 
1154
              key_part[i].length)
 
1155
          {
 
1156
            primary_key=MAX_KEY;                // Can't be used
 
1157
            break;
 
1158
          }
 
1159
        }
 
1160
      }
 
1161
 
 
1162
      for (uint32_t i=0 ; i < keyinfo->key_parts ; key_part++,i++)
 
1163
      {
 
1164
        Field *field;
 
1165
        if (!key_part->fieldnr)
 
1166
        {
 
1167
//          error= 4;                             // Wrong file
 
1168
          abort(); // goto err;
 
1169
        }
 
1170
        field= key_part->field= share->field[key_part->fieldnr-1];
 
1171
        key_part->type= field->key_type();
 
1172
        if (field->null_ptr)
 
1173
        {
 
1174
          key_part->null_offset=(uint32_t) ((unsigned char*) field->null_ptr -
 
1175
                                        share->default_values);
 
1176
          key_part->null_bit= field->null_bit;
 
1177
          key_part->store_length+=HA_KEY_NULL_LENGTH;
 
1178
          keyinfo->flags|=HA_NULL_PART_KEY;
 
1179
          keyinfo->extra_length+= HA_KEY_NULL_LENGTH;
 
1180
          keyinfo->key_length+= HA_KEY_NULL_LENGTH;
 
1181
        }
 
1182
        if (field->type() == DRIZZLE_TYPE_BLOB ||
 
1183
            field->real_type() == DRIZZLE_TYPE_VARCHAR)
 
1184
        {
 
1185
          if (field->type() == DRIZZLE_TYPE_BLOB)
 
1186
            key_part->key_part_flag|= HA_BLOB_PART;
 
1187
          else
 
1188
            key_part->key_part_flag|= HA_VAR_LENGTH_PART;
 
1189
          keyinfo->extra_length+=HA_KEY_BLOB_LENGTH;
 
1190
          key_part->store_length+=HA_KEY_BLOB_LENGTH;
 
1191
          keyinfo->key_length+= HA_KEY_BLOB_LENGTH;
 
1192
        }
 
1193
        if (i == 0 && key != primary_key)
 
1194
          field->flags |= (((keyinfo->flags & HA_NOSAME) &&
 
1195
                           (keyinfo->key_parts == 1)) ?
 
1196
                           UNIQUE_KEY_FLAG : MULTIPLE_KEY_FLAG);
 
1197
        if (i == 0)
 
1198
          field->key_start.set_bit(key);
 
1199
        if (field->key_length() == key_part->length &&
 
1200
            !(field->flags & BLOB_FLAG))
 
1201
        {
 
1202
          if (handler_file->index_flags(key, i, 0) & HA_KEYREAD_ONLY)
 
1203
          {
 
1204
            share->keys_for_keyread.set_bit(key);
 
1205
            field->part_of_key.set_bit(key);
 
1206
            field->part_of_key_not_clustered.set_bit(key);
 
1207
          }
 
1208
          if (handler_file->index_flags(key, i, 1) & HA_READ_ORDER)
 
1209
            field->part_of_sortkey.set_bit(key);
 
1210
        }
 
1211
        if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
 
1212
            usable_parts == i)
 
1213
          usable_parts++;                       // For FILESORT
 
1214
        field->flags|= PART_KEY_FLAG;
 
1215
        if (key == primary_key)
 
1216
        {
 
1217
          field->flags|= PRI_KEY_FLAG;
 
1218
          /*
 
1219
            If this field is part of the primary key and all keys contains
 
1220
            the primary key, then we can use any key to find this column
 
1221
          */
 
1222
          if (ha_option & HA_PRIMARY_KEY_IN_READ_INDEX)
 
1223
          {
 
1224
            field->part_of_key= share->keys_in_use;
 
1225
            if (field->part_of_sortkey.is_set(key))
 
1226
              field->part_of_sortkey= share->keys_in_use;
 
1227
          }
 
1228
        }
 
1229
        if (field->key_length() != key_part->length)
 
1230
        {
 
1231
          key_part->key_part_flag|= HA_PART_KEY_SEG;
 
1232
        }
 
1233
      }
 
1234
      keyinfo->usable_key_parts= usable_parts; // Filesort
 
1235
 
 
1236
      set_if_bigger(share->max_key_length,keyinfo->key_length+
 
1237
                    keyinfo->key_parts);
 
1238
      share->total_key_length+= keyinfo->key_length;
 
1239
      /*
 
1240
        MERGE tables do not have unique indexes. But every key could be
 
1241
        an unique index on the underlying MyISAM table. (Bug #10400)
 
1242
      */
 
1243
      if ((keyinfo->flags & HA_NOSAME) ||
 
1244
          (ha_option & HA_ANY_INDEX_MAY_BE_UNIQUE))
 
1245
        set_if_bigger(share->max_unique_length,keyinfo->key_length);
 
1246
    }
 
1247
    if (primary_key < MAX_KEY &&
 
1248
        (share->keys_in_use.is_set(primary_key)))
 
1249
    {
 
1250
      share->primary_key= primary_key;
 
1251
      /*
 
1252
        If we are using an integer as the primary key then allow the user to
 
1253
        refer to it as '_rowid'
 
1254
      */
 
1255
      if (share->key_info[primary_key].key_parts == 1)
 
1256
      {
 
1257
        Field *field= share->key_info[primary_key].key_part[0].field;
 
1258
        if (field && field->result_type() == INT_RESULT)
 
1259
        {
 
1260
          /* note that fieldnr here (and rowid_field_offset) starts from 1 */
 
1261
          share->rowid_field_offset= (share->key_info[primary_key].key_part[0].
 
1262
                                      fieldnr);
 
1263
        }
 
1264
      }
 
1265
 
 
1266
    }
 
1267
    else
 
1268
      share->primary_key = MAX_KEY; // we do not have a primary key
 
1269
  }
 
1270
  else
 
1271
    share->primary_key= MAX_KEY;
 
1272
 
 
1273
  delete handler_file;
 
1274
 
1024
1275
  return 0;
1025
1276
}
1026
1277
 
1208
1459
  uint32_t new_frm_ver, field_pack_length, new_field_pack_flag;
1209
1460
  uint32_t interval_count, interval_parts, read_length, int_length;
1210
1461
  uint32_t db_create_options, keys, key_parts, n_length;
1211
 
  uint32_t key_info_length, com_length, null_bit_pos=0;
 
1462
  uint32_t key_info_length, com_length;
1212
1463
  uint32_t vcol_screen_length;
1213
1464
  uint32_t i,j;
1214
 
  bool use_hash;
1215
1465
  unsigned char forminfo[288];
1216
1466
  char *names, *comment_pos, *vcol_screen_pos;
1217
1467
  unsigned char *record;
1218
 
  unsigned char *disk_buff, *strpos, *null_flags=NULL, *null_pos=NULL;
 
1468
  unsigned char *disk_buff, *strpos;
1219
1469
  ulong pos, record_offset, rec_buff_length;
1220
1470
  handler *handler_file= 0;
1221
1471
  KEY   *keyinfo;
1423
1673
                               vcol_screen_length)))))
1424
1674
    goto err;                                   /* purecov: inspected */
1425
1675
 
1426
 
  share->field= field_ptr;
 
1676
//  share->field= field_ptr;
1427
1677
  read_length=(uint32_t) (share->fields * field_pack_length +
1428
1678
                      pos+ (uint32_t) (n_length+int_length+com_length+
1429
1679
                                   vcol_screen_length));
1442
1692
  memcpy(vcol_screen_pos, disk_buff+read_length-vcol_screen_length,
1443
1693
         vcol_screen_length);
1444
1694
 
1445
 
  fix_type_pointers(&interval_array, &share->fieldnames, 1, &names);
1446
 
  if (share->fieldnames.count != share->fields)
1447
 
    goto err;
 
1695
//  fix_type_pointers(&interval_array, &share->fieldnames, 1, &names);
 
1696
  assert(share->fieldnames.count == share->fields);
1448
1697
 
1449
1698
/*  if (keynames)
1450
1699
    fix_type_pointers(&interval_array, &share->keynames, 1, &keynames);*/
1456
1705
 
1457
1706
  record= share->default_values-1;              /* Fieldstart = 1 */
1458
1707
 
1459
 
  null_flags= null_pos= (unsigned char*) record+1;
1460
 
  null_bit_pos= (db_create_options & HA_OPTION_PACK_RECORD) ? 0 : 1;
1461
 
  /*
1462
 
    null_bytes below is only correct under the condition that
1463
 
    there are no bit fields.  Correct values is set below after the
1464
 
    table struct is initialized
1465
 
  */
1466
 
  share->null_bytes= (share->null_fields + null_bit_pos + 7) / 8;
1467
 
 
1468
 
  use_hash= share->fields >= MAX_FIELDS_BEFORE_HASH;
1469
 
  if (use_hash)
1470
 
    use_hash= !hash_init(&share->name_hash,
1471
 
                         system_charset_info,
1472
 
                         share->fields,0,0,
1473
 
                         (hash_get_key) get_field_name,0,0);
1474
 
 
1475
 
  for (i=0 ; i < share->fields; i++, strpos+=field_pack_length, field_ptr++)
1476
 
  {
1477
 
    uint32_t pack_flag, interval_nr, unireg_type, recpos, field_length;
1478
 
    uint32_t vcol_info_length=0;
1479
 
    enum_field_types field_type;
1480
 
    enum column_format_type column_format= COLUMN_FORMAT_TYPE_DEFAULT;
1481
 
    const CHARSET_INFO *charset= NULL;
1482
 
    LEX_STRING comment;
1483
 
    virtual_column_info *vcol_info= NULL;
1484
 
    bool fld_is_stored= true;
1485
 
 
1486
 
    if (field_extra_info)
1487
 
    {
1488
 
      char tmp= field_extra_info[i];
1489
 
      column_format= (enum column_format_type)
1490
 
                    ((tmp >> COLUMN_FORMAT_SHIFT) & COLUMN_FORMAT_MASK);
1491
 
    }
1492
 
    if (new_frm_ver >= 3)
1493
 
    {
1494
 
      /* new frm file in 4.1 */
1495
 
      field_length= uint2korr(strpos+3);
1496
 
      recpos=       uint3korr(strpos+5);
1497
 
      pack_flag=    uint2korr(strpos+8);
1498
 
      unireg_type=  (uint32_t) strpos[10];
1499
 
      interval_nr=  (uint32_t) strpos[12];
1500
 
      uint32_t comment_length=uint2korr(strpos+15);
1501
 
      field_type=(enum_field_types) (uint32_t) strpos[13];
1502
 
 
1503
 
      {
1504
 
        if (!strpos[14])
1505
 
          charset= &my_charset_bin;
1506
 
        else if (!(charset=get_charset((uint32_t) strpos[14])))
1507
 
        {
1508
 
          error= 5; // Unknown or unavailable charset
1509
 
          errarg= (int) strpos[14];
1510
 
          goto err;
1511
 
        }
1512
 
      }
1513
 
      if (field_type == DRIZZLE_TYPE_VIRTUAL)
1514
 
      {
1515
 
        assert(interval_nr); // Expect non-null expression
1516
 
        /*
1517
 
          The interval_id byte in the .frm file stores the length of the
1518
 
          expression statement for a virtual column.
1519
 
        */
1520
 
        vcol_info_length= interval_nr;
1521
 
        interval_nr= 0;
1522
 
      }
1523
 
      if (!comment_length)
1524
 
      {
1525
 
        comment.str= (char*) "";
1526
 
        comment.length=0;
1527
 
      }
1528
 
      else
1529
 
      {
1530
 
        comment.str=    strmake_root(&share->mem_root, comment_pos, comment_length);
1531
 
        comment.length= comment_length;
1532
 
        comment_pos+=   comment_length;
1533
 
      }
1534
 
      if (vcol_info_length)
1535
 
      {
1536
 
        /*
1537
 
          Get virtual column data stored in the .frm file as follows:
1538
 
          byte 1      = 1 (always 1 to allow for future extensions)
1539
 
          byte 2      = sql_type
1540
 
          byte 3      = flags (as of now, 0 - no flags, 1 - field is physically stored)
1541
 
          byte 4-...  = virtual column expression (text data)
1542
 
        */
1543
 
        vcol_info= new virtual_column_info();
1544
 
        if ((uint32_t)vcol_screen_pos[0] != 1)
1545
 
        {
1546
 
          error= 4;
1547
 
          goto err;
1548
 
        }
1549
 
        field_type= (enum_field_types) (unsigned char) vcol_screen_pos[1];
1550
 
        fld_is_stored= (bool) (uint32_t) vcol_screen_pos[2];
1551
 
        vcol_info->expr_str.str= (char *)memdup_root(&share->mem_root,
1552
 
                                                     vcol_screen_pos+
1553
 
                                                       (uint32_t)FRM_VCOL_HEADER_SIZE,
1554
 
                                                     vcol_info_length-
1555
 
                                                       (uint32_t)FRM_VCOL_HEADER_SIZE);
1556
 
        vcol_info->expr_str.length= vcol_info_length-(uint32_t)FRM_VCOL_HEADER_SIZE;
1557
 
        vcol_screen_pos+= vcol_info_length;
1558
 
        share->vfields++;
1559
 
      }
1560
 
    }
1561
 
    else
1562
 
    {
1563
 
      assert(false); /* Old (pre-4.1) FRM file. This is Drizzle. */
1564
 
    }
1565
 
 
1566
 
    if (interval_nr && charset->mbminlen > 1)
1567
 
    {
1568
 
      /* Unescape UCS2 intervals from HEX notation */
1569
 
      TYPELIB *interval= share->intervals + interval_nr - 1;
1570
 
      unhex_type2(interval);
1571
 
    }
1572
 
 
1573
 
    *field_ptr= reg_field=
1574
 
      make_field(share, record+recpos,
1575
 
                 (uint32_t) field_length,
1576
 
                 null_pos, null_bit_pos,
1577
 
                 pack_flag,
1578
 
                 field_type,
1579
 
                 charset,
1580
 
                 (Field::utype) MTYP_TYPENR(unireg_type),
1581
 
                 (interval_nr ?
1582
 
                  share->intervals+interval_nr-1 :
1583
 
                  (TYPELIB*) 0),
1584
 
                 share->fieldnames.type_names[i]);
1585
 
    if (!reg_field)                             // Not supported field type
1586
 
    {
1587
 
      error= 4;
1588
 
      goto err;                 /* purecov: inspected */
1589
 
    }
1590
 
 
1591
 
    reg_field->flags|= ((uint32_t)column_format << COLUMN_FORMAT_FLAGS);
1592
 
    reg_field->field_index= i;
1593
 
    reg_field->comment=comment;
1594
 
    reg_field->vcol_info= vcol_info;
1595
 
    reg_field->is_stored= fld_is_stored;
1596
 
    if (!(reg_field->flags & NOT_NULL_FLAG))
1597
 
    {
1598
 
      if (!(null_bit_pos= (null_bit_pos + 1) & 7))
1599
 
        null_pos++;
1600
 
    }
1601
 
    if (f_no_default(pack_flag))
1602
 
      reg_field->flags|= NO_DEFAULT_VALUE_FLAG;
1603
 
 
1604
 
    if (reg_field->unireg_check == Field::NEXT_NUMBER)
1605
 
      share->found_next_number_field= field_ptr;
1606
 
    if (share->timestamp_field == reg_field)
1607
 
      share->timestamp_field_offset= i;
1608
 
 
1609
 
    if (use_hash)
1610
 
      (void) my_hash_insert(&share->name_hash,
1611
 
                            (unsigned char*) field_ptr); // never fail
1612
 
    if (!reg_field->is_stored)
1613
 
    {
1614
 
      share->stored_fields--;
1615
 
      if (share->stored_rec_length>=recpos)
1616
 
        share->stored_rec_length= recpos-1;
1617
 
    }
1618
 
  }
1619
 
  *field_ptr=0;                                 // End marker
1620
1708
  /* Sanity checks: */
1621
1709
  assert(share->fields>=share->stored_fields);
1622
1710
  assert(share->reclength>=share->stored_rec_length);
1623
1711
 
1624
 
  /* Fix key->name and key_part->field */
1625
 
  if (key_parts)
1626
 
  {
1627
 
    uint32_t primary_key=(uint32_t) (find_type((char*) "PRIMARY",
1628
 
                                       &share->keynames, 3) - 1);
1629
 
    int64_t ha_option= handler_file->ha_table_flags();
1630
 
    keyinfo= share->key_info;
1631
 
    key_part= keyinfo->key_part;
1632
 
 
1633
 
    for (uint32_t key=0 ; key < share->keys ; key++,keyinfo++)
1634
 
    {
1635
 
      uint32_t usable_parts= 0;
1636
 
//      keyinfo->name=(char*) share->keynames.type_names[key];
1637
 
 
1638
 
      if (primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
1639
 
      {
1640
 
        /*
1641
 
          If the UNIQUE key doesn't have NULL columns and is not a part key
1642
 
          declare this as a primary key.
1643
 
        */
1644
 
        primary_key=key;
1645
 
        for (i=0 ; i < keyinfo->key_parts ;i++)
1646
 
        {
1647
 
          uint32_t fieldnr= key_part[i].fieldnr;
1648
 
          if (!fieldnr ||
1649
 
              share->field[fieldnr-1]->null_ptr ||
1650
 
              share->field[fieldnr-1]->key_length() !=
1651
 
              key_part[i].length)
1652
 
          {
1653
 
            primary_key=MAX_KEY;                // Can't be used
1654
 
            break;
1655
 
          }
1656
 
        }
1657
 
      }
1658
 
 
1659
 
      for (i=0 ; i < keyinfo->key_parts ; key_part++,i++)
1660
 
      {
1661
 
        Field *field;
1662
 
        if (!key_part->fieldnr)
1663
 
        {
1664
 
          error= 4;                             // Wrong file
1665
 
          goto err;
1666
 
        }
1667
 
        field= key_part->field= share->field[key_part->fieldnr-1];
1668
 
        key_part->type= field->key_type();
1669
 
        if (field->null_ptr)
1670
 
        {
1671
 
          key_part->null_offset=(uint32_t) ((unsigned char*) field->null_ptr -
1672
 
                                        share->default_values);
1673
 
          key_part->null_bit= field->null_bit;
1674
 
          key_part->store_length+=HA_KEY_NULL_LENGTH;
1675
 
          keyinfo->flags|=HA_NULL_PART_KEY;
1676
 
          keyinfo->extra_length+= HA_KEY_NULL_LENGTH;
1677
 
          keyinfo->key_length+= HA_KEY_NULL_LENGTH;
1678
 
        }
1679
 
        if (field->type() == DRIZZLE_TYPE_BLOB ||
1680
 
            field->real_type() == DRIZZLE_TYPE_VARCHAR)
1681
 
        {
1682
 
          if (field->type() == DRIZZLE_TYPE_BLOB)
1683
 
            key_part->key_part_flag|= HA_BLOB_PART;
1684
 
          else
1685
 
            key_part->key_part_flag|= HA_VAR_LENGTH_PART;
1686
 
          keyinfo->extra_length+=HA_KEY_BLOB_LENGTH;
1687
 
          key_part->store_length+=HA_KEY_BLOB_LENGTH;
1688
 
          keyinfo->key_length+= HA_KEY_BLOB_LENGTH;
1689
 
        }
1690
 
        if (i == 0 && key != primary_key)
1691
 
          field->flags |= (((keyinfo->flags & HA_NOSAME) &&
1692
 
                           (keyinfo->key_parts == 1)) ?
1693
 
                           UNIQUE_KEY_FLAG : MULTIPLE_KEY_FLAG);
1694
 
        if (i == 0)
1695
 
          field->key_start.set_bit(key);
1696
 
        if (field->key_length() == key_part->length &&
1697
 
            !(field->flags & BLOB_FLAG))
1698
 
        {
1699
 
          if (handler_file->index_flags(key, i, 0) & HA_KEYREAD_ONLY)
1700
 
          {
1701
 
            share->keys_for_keyread.set_bit(key);
1702
 
            field->part_of_key.set_bit(key);
1703
 
            field->part_of_key_not_clustered.set_bit(key);
1704
 
          }
1705
 
          if (handler_file->index_flags(key, i, 1) & HA_READ_ORDER)
1706
 
            field->part_of_sortkey.set_bit(key);
1707
 
        }
1708
 
        if (!(key_part->key_part_flag & HA_REVERSE_SORT) &&
1709
 
            usable_parts == i)
1710
 
          usable_parts++;                       // For FILESORT
1711
 
        field->flags|= PART_KEY_FLAG;
1712
 
        if (key == primary_key)
1713
 
        {
1714
 
          field->flags|= PRI_KEY_FLAG;
1715
 
          /*
1716
 
            If this field is part of the primary key and all keys contains
1717
 
            the primary key, then we can use any key to find this column
1718
 
          */
1719
 
          if (ha_option & HA_PRIMARY_KEY_IN_READ_INDEX)
1720
 
          {
1721
 
            field->part_of_key= share->keys_in_use;
1722
 
            if (field->part_of_sortkey.is_set(key))
1723
 
              field->part_of_sortkey= share->keys_in_use;
1724
 
          }
1725
 
        }
1726
 
        if (field->key_length() != key_part->length)
1727
 
        {
1728
 
          key_part->key_part_flag|= HA_PART_KEY_SEG;
1729
 
        }
1730
 
      }
1731
 
      keyinfo->usable_key_parts= usable_parts; // Filesort
1732
 
 
1733
 
      set_if_bigger(share->max_key_length,keyinfo->key_length+
1734
 
                    keyinfo->key_parts);
1735
 
      share->total_key_length+= keyinfo->key_length;
1736
 
      /*
1737
 
        MERGE tables do not have unique indexes. But every key could be
1738
 
        an unique index on the underlying MyISAM table. (Bug #10400)
1739
 
      */
1740
 
      if ((keyinfo->flags & HA_NOSAME) ||
1741
 
          (ha_option & HA_ANY_INDEX_MAY_BE_UNIQUE))
1742
 
        set_if_bigger(share->max_unique_length,keyinfo->key_length);
1743
 
    }
1744
 
    if (primary_key < MAX_KEY &&
1745
 
        (share->keys_in_use.is_set(primary_key)))
1746
 
    {
1747
 
      share->primary_key= primary_key;
1748
 
      /*
1749
 
        If we are using an integer as the primary key then allow the user to
1750
 
        refer to it as '_rowid'
1751
 
      */
1752
 
      if (share->key_info[primary_key].key_parts == 1)
1753
 
      {
1754
 
        Field *field= share->key_info[primary_key].key_part[0].field;
1755
 
        if (field && field->result_type() == INT_RESULT)
1756
 
        {
1757
 
          /* note that fieldnr here (and rowid_field_offset) starts from 1 */
1758
 
          share->rowid_field_offset= (share->key_info[primary_key].key_part[0].
1759
 
                                      fieldnr);
1760
 
        }
1761
 
      }
1762
 
    }
1763
 
    else
1764
 
      share->primary_key = MAX_KEY; // we do not have a primary key
1765
 
  }
1766
 
  else
1767
 
    share->primary_key= MAX_KEY;
 
1712
 
1768
1713
  if (disk_buff)
1769
1714
    free(disk_buff);
1770
1715
  disk_buff= NULL;
1771
 
  if (new_field_pack_flag <= 1)
1772
 
  {
1773
 
    /* Old file format with default as not null */
1774
 
    uint32_t null_length= (share->null_fields+7)/8;
1775
 
    memset(share->default_values + (null_flags - (unsigned char*) record),
1776
 
          null_length, 255);
1777
 
  }
1778
1716
 
1779
1717
  if (share->found_next_number_field)
1780
1718
  {
1814
1752
    the correct null_bytes can now be set, since bitfields have been taken
1815
1753
    into account
1816
1754
  */
1817
 
  share->null_bytes= (null_pos - (unsigned char*) null_flags +
1818
 
                      (null_bit_pos + 7) / 8);
1819
 
  share->last_null_bit_pos= null_bit_pos;
1820
1755
 
1821
1756
  share->db_low_byte_first= handler_file->low_byte_first();
1822
1757
  share->column_bitmap_size= bitmap_buffer_size(share->fields);
4976
4911
  List_iterator_fast<Create_field> it(field_list);
4977
4912
  while ((cdef= it++))
4978
4913
  {
4979
 
    *field= make_field(share, 0, cdef->length,
 
4914
    *field= make_field(share, NULL, 0, cdef->length,
4980
4915
                       (unsigned char*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
4981
4916
                       f_maybe_null(cdef->pack_flag) ? 1 : 0,
4982
4917
                       cdef->pack_flag, cdef->sql_type, cdef->charset,