~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/table.cc

  • Committer: Brian Aker
  • Date: 2008-07-14 03:04:13 UTC
  • mfrom: (77.1.90 codestyle)
  • Revision ID: brian@tangent.org-20080714030413-dpv6opb0eoy1rd3f
Merging Monty's code, I did remove error on compile though (since it does
not currently work because of sql_plugin.cc)

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
/* Some general useful functions */
18
18
 
19
 
#include <drizzled/server_includes.h>
20
 
#include <drizzled/drizzled_error_messages.h>
21
 
 
22
 
#include "tmp_table.h"
23
 
#include "sj_tmp_table.h"
 
19
#include "mysql_priv.h"
 
20
#include <m_ctype.h>
 
21
#include "my_md5.h"
24
22
 
25
23
/* INFORMATION_SCHEMA name */
26
24
LEX_STRING INFORMATION_SCHEMA_NAME= {C_STRING_WITH_LEN("information_schema")};
27
25
 
28
 
/* Keyword for parsing virtual column functions */
29
 
LEX_STRING parse_vcol_keyword= { C_STRING_WITH_LEN("PARSE_VCOL_EXPR ") };
 
26
/* MYSQL_SCHEMA name */
 
27
LEX_STRING MYSQL_SCHEMA_NAME= {C_STRING_WITH_LEN("mysql")};
30
28
 
31
29
/* Functions defined in this file */
32
30
 
33
31
void open_table_error(TABLE_SHARE *share, int error, int db_errno,
34
32
                      myf errortype, int errarg);
35
33
static int open_binary_frm(THD *thd, TABLE_SHARE *share,
36
 
                           unsigned char *head, File file);
 
34
                           uchar *head, File file);
37
35
static void fix_type_pointers(const char ***array, TYPELIB *point_to_type,
38
 
                              uint32_t types, char **names);
39
 
static uint32_t find_field(Field **fields, unsigned char *record, uint32_t start, uint32_t length);
 
36
                              uint types, char **names);
 
37
static uint find_field(Field **fields, uchar *record, uint start, uint length);
 
38
 
 
39
/**************************************************************************
 
40
  Object_creation_ctx implementation.
 
41
**************************************************************************/
 
42
 
 
43
Object_creation_ctx *Object_creation_ctx::set_n_backup(THD *thd)
 
44
{
 
45
  Object_creation_ctx *backup_ctx;
 
46
 
 
47
  backup_ctx= create_backup_ctx(thd);
 
48
  change_env(thd);
 
49
 
 
50
  return(backup_ctx);
 
51
}
 
52
 
 
53
void Object_creation_ctx::restore_env(THD *thd, Object_creation_ctx *backup_ctx)
 
54
{
 
55
  if (!backup_ctx)
 
56
    return;
 
57
 
 
58
  backup_ctx->change_env(thd);
 
59
 
 
60
  delete backup_ctx;
 
61
}
 
62
 
 
63
/**************************************************************************
 
64
  Default_object_creation_ctx implementation.
 
65
**************************************************************************/
 
66
 
 
67
Default_object_creation_ctx::Default_object_creation_ctx(THD *thd)
 
68
  : m_client_cs(thd->variables.character_set_client),
 
69
    m_connection_cl(thd->variables.collation_connection)
 
70
{ }
 
71
 
 
72
Default_object_creation_ctx::Default_object_creation_ctx(
 
73
  CHARSET_INFO *client_cs, CHARSET_INFO *connection_cl)
 
74
  : m_client_cs(client_cs),
 
75
    m_connection_cl(connection_cl)
 
76
{ }
 
77
 
 
78
Object_creation_ctx *
 
79
Default_object_creation_ctx::create_backup_ctx(THD *thd) const
 
80
{
 
81
  return new Default_object_creation_ctx(thd);
 
82
}
 
83
 
 
84
void Default_object_creation_ctx::change_env(THD *thd) const
 
85
{
 
86
  thd->variables.character_set_client= m_client_cs;
 
87
  thd->variables.collation_connection= m_connection_cl;
 
88
 
 
89
  thd->update_charset();
 
90
}
40
91
 
41
92
/*************************************************************************/
42
93
 
43
94
/* Get column name from column hash */
44
95
 
45
 
static unsigned char *get_field_name(Field **buff, size_t *length,
 
96
static uchar *get_field_name(Field **buff, size_t *length,
46
97
                             bool not_used __attribute__((unused)))
47
98
{
48
99
  *length= (uint) strlen((*buff)->field_name);
49
 
  return (unsigned char*) (*buff)->field_name;
 
100
  return (uchar*) (*buff)->field_name;
50
101
}
51
102
 
52
103
 
101
152
 
102
153
  SYNOPSIS
103
154
    alloc_table_share()
104
 
    TableList           Take database and table name from there
 
155
    TABLE_LIST          Take database and table name from there
105
156
    key                 Table cache key (db \0 table_name \0...)
106
157
    key_length          Length of key
107
158
 
110
161
    #  Share
111
162
*/
112
163
 
113
 
TABLE_SHARE *alloc_table_share(TableList *table_list, char *key,
114
 
                               uint32_t key_length)
 
164
TABLE_SHARE *alloc_table_share(TABLE_LIST *table_list, char *key,
 
165
                               uint key_length)
115
166
{
116
167
  MEM_ROOT mem_root;
117
168
  TABLE_SHARE *share;
118
169
  char *key_buff, *path_buff;
119
170
  char path[FN_REFLEN];
120
 
  uint32_t path_length;
 
171
  uint path_length;
121
172
 
122
173
  path_length= build_table_filename(path, sizeof(path) - 1,
123
174
                                    table_list->db,
129
180
                       &path_buff, path_length + 1,
130
181
                       NULL))
131
182
  {
132
 
    memset(share, 0, sizeof(*share));
 
183
    bzero((char*) share, sizeof(*share));
133
184
 
134
185
    share->set_table_cache_key(key_buff, key, key_length);
135
186
 
136
187
    share->path.str= path_buff;
137
188
    share->path.length= path_length;
138
 
    my_stpcpy(share->path.str, path);
 
189
    strmov(share->path.str, path);
139
190
    share->normalized_path.str=    share->path.str;
140
191
    share->normalized_path.length= path_length;
141
192
 
144
195
    /*
145
196
      This constant is used to mark that no table map version has been
146
197
      assigned.  No arithmetic is done on the value: it will be
147
 
      overwritten with a value taken from DRIZZLE_BIN_LOG.
 
198
      overwritten with a value taken from MYSQL_BIN_LOG.
148
199
    */
149
 
    share->table_map_version= UINT64_MAX;
 
200
    share->table_map_version= ~(uint64_t)0;
150
201
 
151
202
    /*
152
203
      Since alloc_table_share() can be called without any locking (for
155
206
      elsewhere, and then assign a table map id inside open_table()
156
207
      under the protection of the LOCK_open mutex.
157
208
    */
158
 
    share->table_map_id= UINT32_MAX;
 
209
    share->table_map_id= ~0UL;
159
210
    share->cached_row_logging_check= -1;
160
211
 
161
 
    memcpy(&share->mem_root, &mem_root, sizeof(mem_root));
 
212
    memcpy((char*) &share->mem_root, (char*) &mem_root, sizeof(mem_root));
162
213
    pthread_mutex_init(&share->mutex, MY_MUTEX_INIT_FAST);
163
214
    pthread_cond_init(&share->cond, NULL);
164
215
  }
190
241
*/
191
242
 
192
243
void init_tmp_table_share(THD *thd, TABLE_SHARE *share, const char *key,
193
 
                          uint32_t key_length, const char *table_name,
 
244
                          uint key_length, const char *table_name,
194
245
                          const char *path)
195
246
{
196
247
 
197
 
  memset(share, 0, sizeof(*share));
 
248
  bzero((char*) share, sizeof(*share));
198
249
  init_sql_alloc(&share->mem_root, TABLE_ALLOC_BLOCK_SIZE, 0);
199
250
  share->table_category=         TABLE_CATEGORY_TEMPORARY;
200
251
  share->tmp_table=              INTERNAL_TMP_TABLE;
264
315
  share->db_plugin= NULL;
265
316
 
266
317
  /* We must copy mem_root from share because share is allocated through it */
267
 
  memcpy(&mem_root, &share->mem_root, sizeof(mem_root));
 
318
  memcpy((char*) &mem_root, (char*) &share->mem_root, sizeof(mem_root));
268
319
  free_root(&mem_root, MYF(0));                 // Free's share
269
320
  return;
270
321
}
294
345
   6    Unknown .frm version
295
346
*/
296
347
 
297
 
int open_table_def(THD *thd, TABLE_SHARE *share, uint32_t db_flags  __attribute__((unused)))
 
348
int open_table_def(THD *thd, TABLE_SHARE *share, uint db_flags)
298
349
{
299
350
  int error, table_type;
300
351
  bool error_given;
301
352
  File file;
302
 
  unsigned char head[64], *disk_buff;
 
353
  uchar head[64], *disk_buff;
303
354
  char  path[FN_REFLEN];
304
355
  MEM_ROOT **root_ptr, *old_root;
305
356
 
307
358
  error_given= 0;
308
359
  disk_buff= NULL;
309
360
 
310
 
  strxmov(path, share->normalized_path.str, reg_ext, NULL);
311
 
  if ((file= open(path, O_RDONLY)) < 0)
 
361
  strxmov(path, share->normalized_path.str, reg_ext, NullS);
 
362
  if ((file= my_open(path, O_RDONLY | O_SHARE, MYF(0))) < 0)
312
363
  {
313
364
    /*
314
365
      We don't try to open 5.0 unencoded name, if
319
370
        
320
371
      - non-encoded db or table name contain "#mysql50#" prefix.
321
372
        This kind of tables must have been opened only by the
322
 
        open() above.
 
373
        my_open() above.
323
374
    */
324
375
    if (strchr(share->table_name.str, '@') ||
325
376
        !strncmp(share->db.str, MYSQL50_TABLE_NAME_PREFIX,
329
380
      goto err_not_open;
330
381
 
331
382
    /* Try unencoded 5.0 name */
332
 
    uint32_t length;
 
383
    uint length;
333
384
    strxnmov(path, sizeof(path)-1,
334
385
             mysql_data_home, "/", share->db.str, "/",
335
 
             share->table_name.str, reg_ext, NULL);
 
386
             share->table_name.str, reg_ext, NullS);
336
387
    length= unpack_filename(path, path) - reg_ext_length;
337
388
    /*
338
389
      The following is a safety test and should never fail
345
396
      so no need to check the old file name.
346
397
    */
347
398
    if (length == share->normalized_path.length ||
348
 
        ((file= open(path, O_RDONLY)) < 0))
 
399
        ((file= my_open(path, O_RDONLY | O_SHARE, MYF(0))) < 0))
349
400
      goto err_not_open;
350
401
 
351
402
    /* Unencoded 5.0 table name found */
352
403
    path[length]= '\0'; // Remove .frm extension
353
 
    my_stpcpy(share->normalized_path.str, path);
 
404
    strmov(share->normalized_path.str, path);
354
405
    share->normalized_path.length= length;
355
406
  }
356
407
 
358
409
  if (my_read(file, head, 64, MYF(MY_NABP)))
359
410
    goto err;
360
411
 
361
 
  if (head[0] == (unsigned char) 254 && head[1] == 1)
 
412
  if (head[0] == (uchar) 254 && head[1] == 1)
362
413
  {
363
414
    if (head[2] == FRM_VER || head[2] == FRM_VER+1 ||
364
415
        (head[2] >= FRM_VER+3 && head[2] <= FRM_VER+4))
365
416
    {
 
417
      /* Open view only */
 
418
      if (db_flags & OPEN_VIEW_ONLY)
 
419
      {
 
420
        error_given= 1;
 
421
        goto err;
 
422
      }
366
423
      table_type= 1;
367
424
    }
368
425
    else
410
467
  Read data from a binary .frm file from MySQL 3.23 - 5.0 into TABLE_SHARE
411
468
*/
412
469
 
413
 
static int open_binary_frm(THD *thd, TABLE_SHARE *share, unsigned char *head,
 
470
static int open_binary_frm(THD *thd, TABLE_SHARE *share, uchar *head,
414
471
                           File file)
415
472
{
416
473
  int error, errarg= 0;
417
 
  uint32_t new_frm_ver, field_pack_length, new_field_pack_flag;
418
 
  uint32_t interval_count, interval_parts, read_length, int_length;
419
 
  uint32_t db_create_options, keys, key_parts, n_length;
420
 
  uint32_t key_info_length, com_length, null_bit_pos=0;
421
 
  uint32_t vcol_screen_length;
422
 
  uint32_t extra_rec_buf_length;
423
 
  uint32_t i,j;
 
474
  uint new_frm_ver, field_pack_length, new_field_pack_flag;
 
475
  uint interval_count, interval_parts, read_length, int_length;
 
476
  uint db_create_options, keys, key_parts, n_length;
 
477
  uint key_info_length, com_length, null_bit_pos=0;
 
478
  uint extra_rec_buf_length;
 
479
  uint i,j;
424
480
  bool use_hash;
425
 
  unsigned char forminfo[288];
426
 
  char *keynames, *names, *comment_pos, *vcol_screen_pos;
427
 
  unsigned char *record;
428
 
  unsigned char *disk_buff, *strpos, *null_flags=NULL, *null_pos=NULL;
 
481
  uchar forminfo[288];
 
482
  char *keynames, *names, *comment_pos;
 
483
  uchar *record;
 
484
  uchar *disk_buff, *strpos, *null_flags=NULL, *null_pos=NULL;
429
485
  ulong pos, record_offset, *rec_per_key, rec_buff_length;
430
486
  handler *handler_file= 0;
431
487
  KEY   *keyinfo;
432
488
  KEY_PART_INFO *key_part;
 
489
  SQL_CRYPT *crypted=0;
433
490
  Field  **field_ptr, *reg_field;
434
491
  const char **interval_array;
435
492
  enum legacy_db_type legacy_db_type;
436
493
  my_bitmap_map *bitmaps;
437
 
  unsigned char *buff= 0;
438
 
  unsigned char *field_extra_info= 0;
 
494
  uchar *buff= 0;
 
495
  uchar *field_extra_info= 0;
439
496
 
440
497
  new_field_pack_flag= head[27];
441
498
  new_frm_ver= (head[2] - FRM_VER);
445
502
  error= 3;
446
503
  if (!(pos=get_form_pos(file,head,(TYPELIB*) 0)))
447
504
    goto err;                                   /* purecov: inspected */
448
 
  my_seek(file,pos,MY_SEEK_SET,MYF(0));
 
505
  VOID(my_seek(file,pos,MY_SEEK_SET,MYF(0)));
449
506
  if (my_read(file,forminfo,288,MYF(MY_NABP)))
450
507
    goto err;
451
508
 
479
536
    share->transactional= (ha_choice) (head[39] & 3);
480
537
    share->page_checksum= (ha_choice) ((head[39] >> 2) & 3);
481
538
    share->row_type= (row_type) head[40];
482
 
    share->block_size= uint4korr(head+43);
483
539
    share->table_charset= get_charset((uint) head[38],MYF(0));
484
540
    share->null_field_first= 1;
485
541
  }
489
545
    if (use_mb(default_charset_info))
490
546
    {
491
547
      /* Warn that we may be changing the size of character columns */
492
 
      sql_print_warning(_("'%s' had no or invalid character set, "
 
548
      sql_print_warning("'%s' had no or invalid character set, "
493
549
                        "and default character set is multi-byte, "
494
 
                        "so character column sizes may have changed"),
 
550
                        "so character column sizes may have changed",
495
551
                        share->path.str);
496
552
    }
497
553
    share->table_charset= default_charset_info;
500
556
  if (db_create_options & HA_OPTION_LONG_BLOB_PTR)
501
557
    share->blob_ptr_size= portable_sizeof_char_ptr;
502
558
  /* Set temporarily a good value for db_low_byte_first */
503
 
  share->db_low_byte_first= true;
 
559
  share->db_low_byte_first= test(legacy_db_type != DB_TYPE_ISAM);
504
560
  error=4;
505
561
  share->max_rows= uint4korr(head+18);
506
562
  share->min_rows= uint4korr(head+22);
507
563
 
508
564
  /* Read keyinformation */
509
565
  key_info_length= (uint) uint2korr(head+28);
510
 
  my_seek(file,(ulong) uint2korr(head+6),MY_SEEK_SET,MYF(0));
511
 
  if (read_string(file,(unsigned char**) &disk_buff,key_info_length))
 
566
  VOID(my_seek(file,(ulong) uint2korr(head+6),MY_SEEK_SET,MYF(0)));
 
567
  if (read_string(file,(uchar**) &disk_buff,key_info_length))
512
568
    goto err;                                   /* purecov: inspected */
513
569
  if (disk_buff[0] & 0x80)
514
570
  {
527
583
  if (!(keyinfo = (KEY*) alloc_root(&share->mem_root,
528
584
                                    n_length + uint2korr(disk_buff+4))))
529
585
    goto err;                                   /* purecov: inspected */
530
 
  memset(keyinfo, 0, n_length);
 
586
  bzero((char*) keyinfo,n_length);
531
587
  share->key_info= keyinfo;
532
 
  key_part= reinterpret_cast<KEY_PART_INFO*> (keyinfo+keys);
 
588
  key_part= my_reinterpret_cast(KEY_PART_INFO*) (keyinfo+keys);
533
589
  strpos=disk_buff+6;
534
590
 
535
591
  if (!(rec_per_key= (ulong*) alloc_root(&share->mem_root,
554
610
    for (j=keyinfo->key_parts ; j-- ; key_part++)
555
611
    {
556
612
      *rec_per_key++=0;
557
 
      key_part->fieldnr=        (uint16_t) (uint2korr(strpos) & FIELD_NR_MASK);
 
613
      key_part->fieldnr=        (uint16) (uint2korr(strpos) & FIELD_NR_MASK);
558
614
      key_part->offset= (uint) uint2korr(strpos+2)-1;
559
615
      key_part->key_type=       (uint) uint2korr(strpos+5);
560
616
      // key_part->field=       (Field*) 0;     // Will be fixed later
579
635
    }
580
636
  }
581
637
  keynames=(char*) key_part;
582
 
  strpos+= (my_stpcpy(keynames, (char *) strpos) - keynames)+1;
 
638
  strpos+= (strmov(keynames, (char *) strpos) - keynames)+1;
583
639
 
584
640
  //reading index comments
585
641
  for (keyinfo= share->key_info, i=0; i < keys; i++, keyinfo++)
596
652
  }
597
653
 
598
654
  share->reclength = uint2korr((head+16));
599
 
  share->stored_rec_length= share->reclength;
 
655
  if (*(head+26) == 1)
 
656
    share->system= 1;                           /* one-record-database */
600
657
 
601
658
  record_offset= (ulong) (uint2korr(head+6)+
602
659
                          ((uint2korr(head+14) == 0xffff ?
605
662
  if ((n_length= uint4korr(head+55)))
606
663
  {
607
664
    /* Read extra data segment */
608
 
    unsigned char *next_chunk, *buff_end;
609
 
    if (!(next_chunk= buff= (unsigned char*) my_malloc(n_length, MYF(MY_WME))))
 
665
    uchar *next_chunk, *buff_end;
 
666
    if (!(next_chunk= buff= (uchar*) my_malloc(n_length, MYF(MY_WME))))
610
667
      goto err;
611
668
    if (pread(file, buff, n_length, record_offset + share->reclength) == 0)
612
669
    {
624
681
    buff_end= buff + n_length;
625
682
    if (next_chunk + 2 < buff_end)
626
683
    {
627
 
      uint32_t str_db_type_length= uint2korr(next_chunk);
 
684
      uint str_db_type_length= uint2korr(next_chunk);
628
685
      LEX_STRING name;
629
686
      name.str= (char*) next_chunk + 2;
630
687
      name.length= str_db_type_length;
638
695
                plugin_data(tmp_plugin, handlerton *)))
639
696
        {
640
697
          /* bad file, legacy_db_type did not match the name */
641
 
          free(buff);
 
698
          my_free(buff, MYF(0));
642
699
          goto err;
643
700
        }
644
701
        /*
654
711
        /* purecov: begin inspected */
655
712
        error= 8;
656
713
        my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), name.str);
657
 
        free(buff);
 
714
        my_free(buff, MYF(0));
658
715
        goto err;
659
716
        /* purecov: end */
660
717
      }
665
722
      /* New auto_partitioned indicator introduced in 5.1.11 */
666
723
      next_chunk++;
667
724
    }
668
 
    if (forminfo[46] == (unsigned char)255)
 
725
    if (forminfo[46] == (uchar)255)
669
726
    {
670
727
      //reading long table comment
671
728
      if (next_chunk + 2 > buff_end)
672
729
      {
673
 
          free(buff);
 
730
          my_free(buff, MYF(0));
674
731
          goto err;
675
732
      }
676
733
      share->comment.length = uint2korr(next_chunk);
677
734
      if (! (share->comment.str= strmake_root(&share->mem_root,
678
735
                               (char*)next_chunk + 2, share->comment.length)))
679
736
      {
680
 
          free(buff);
 
737
          my_free(buff, MYF(0));
681
738
          goto err;
682
739
      }
683
740
      next_chunk+= 2 + share->comment.length;
684
741
    }
685
742
    assert(next_chunk <= buff_end);
686
 
    if (share->mysql_version >= DRIZZLE_VERSION_TABLESPACE_IN_FRM_CGE)
 
743
    if (share->mysql_version >= MYSQL_VERSION_TABLESPACE_IN_FRM_CGE)
687
744
    {
688
745
      /*
689
746
       New frm format in mysql_version 5.2.5 (originally in
694
751
      */
695
752
      if (next_chunk >= buff_end)
696
753
      {
697
 
        if (share->mysql_version >= DRIZZLE_VERSION_TABLESPACE_IN_FRM)
 
754
        if (share->mysql_version >= MYSQL_VERSION_TABLESPACE_IN_FRM)
698
755
        {
699
756
          goto err;
700
757
        }
701
758
      }
702
759
      else
703
760
      {
704
 
        const uint32_t format_section_header_size= 8;
705
 
        uint32_t format_section_len= uint2korr(next_chunk+0);
 
761
        const uint format_section_header_size= 8;
 
762
        uint format_section_len= uint2korr(next_chunk+0);
706
763
 
707
764
        field_extra_info= next_chunk + format_section_header_size + 1;
708
765
        next_chunk+= format_section_len;
720
777
  extra_rec_buf_length= uint2korr(head+59);
721
778
  rec_buff_length= ALIGN_SIZE(share->reclength + 1 + extra_rec_buf_length);
722
779
  share->rec_buff_length= rec_buff_length;
723
 
  if (!(record= (unsigned char *) alloc_root(&share->mem_root,
 
780
  if (!(record= (uchar *) alloc_root(&share->mem_root,
724
781
                                     rec_buff_length)))
725
782
    goto err;                                   /* purecov: inspected */
726
783
  share->default_values= record;
727
784
  if (pread(file, record, (size_t) share->reclength, record_offset) == 0)
728
785
    goto err;                                   /* purecov: inspected */
729
786
 
730
 
  my_seek(file,pos+288,MY_SEEK_SET,MYF(0));
 
787
  VOID(my_seek(file,pos+288,MY_SEEK_SET,MYF(0)));
731
788
 
732
789
  share->fields= uint2korr(forminfo+258);
733
790
  pos= uint2korr(forminfo+260);                 /* Length of all screens */
737
794
  int_length= uint2korr(forminfo+274);
738
795
  share->null_fields= uint2korr(forminfo+282);
739
796
  com_length= uint2korr(forminfo+284);
740
 
  vcol_screen_length= uint2korr(forminfo+286);
741
 
  share->vfields= 0;
742
 
  share->stored_fields= share->fields;
743
 
  if (forminfo[46] != (unsigned char)255)
 
797
  if (forminfo[46] != (uchar)255)
744
798
  {
745
799
    share->comment.length=  (int) (forminfo[46]);
746
800
    share->comment.str= strmake_root(&share->mem_root, (char*) forminfo+47,
754
808
                           interval_count*sizeof(TYPELIB)+
755
809
                           (share->fields+interval_parts+
756
810
                            keys+3)*sizeof(char *)+
757
 
                           (n_length+int_length+com_length+
758
 
                               vcol_screen_length)))))
 
811
                           (n_length+int_length+com_length)))))
759
812
    goto err;                                   /* purecov: inspected */
760
813
 
761
814
  share->field= field_ptr;
762
815
  read_length=(uint) (share->fields * field_pack_length +
763
 
                      pos+ (uint) (n_length+int_length+com_length+
764
 
                                   vcol_screen_length));
765
 
  if (read_string(file,(unsigned char**) &disk_buff,read_length))
 
816
                      pos+ (uint) (n_length+int_length+com_length));
 
817
  if (read_string(file,(uchar**) &disk_buff,read_length))
766
818
    goto err;                                   /* purecov: inspected */
767
819
  strpos= disk_buff+pos;
768
820
 
771
823
  names= (char*) (interval_array+share->fields+interval_parts+keys+3);
772
824
  if (!interval_count)
773
825
    share->intervals= 0;                        // For better debugging
774
 
  memcpy(names, strpos+(share->fields*field_pack_length),
 
826
  memcpy((char*) names, strpos+(share->fields*field_pack_length),
775
827
         (uint) (n_length+int_length));
776
828
  comment_pos= names+(n_length+int_length);
777
 
  memcpy(comment_pos, disk_buff+read_length-com_length-vcol_screen_length, 
778
 
         com_length);
779
 
  vcol_screen_pos= names+(n_length+int_length+com_length);
780
 
  memcpy(vcol_screen_pos, disk_buff+read_length-vcol_screen_length, 
781
 
         vcol_screen_length);
 
829
  memcpy(comment_pos, disk_buff+read_length-com_length, com_length);
782
830
 
783
831
  fix_type_pointers(&interval_array, &share->fieldnames, 1, &names);
784
832
  if (share->fieldnames.count != share->fields)
793
841
         interval < share->intervals + interval_count;
794
842
         interval++)
795
843
    {
796
 
      uint32_t count= (uint) (interval->count + 1) * sizeof(uint);
797
 
      if (!(interval->type_lengths= (uint32_t *) alloc_root(&share->mem_root,
 
844
      uint count= (uint) (interval->count + 1) * sizeof(uint);
 
845
      if (!(interval->type_lengths= (uint *) alloc_root(&share->mem_root,
798
846
                                                        count)))
799
847
        goto err;
800
848
      for (count= 0; count < interval->count; count++)
817
865
  record= share->default_values-1;              /* Fieldstart = 1 */
818
866
  if (share->null_field_first)
819
867
  {
820
 
    null_flags= null_pos= (unsigned char*) record+1;
 
868
    null_flags= null_pos= (uchar*) record+1;
821
869
    null_bit_pos= (db_create_options & HA_OPTION_PACK_RECORD) ? 0 : 1;
822
870
    /*
823
871
      null_bytes below is only correct under the condition that
836
884
 
837
885
  for (i=0 ; i < share->fields; i++, strpos+=field_pack_length, field_ptr++)
838
886
  {
839
 
    uint32_t pack_flag, interval_nr, unireg_type, recpos, field_length;
840
 
    uint32_t vcol_info_length=0;
 
887
    uint pack_flag, interval_nr, unireg_type, recpos, field_length;
841
888
    enum_field_types field_type;
842
889
    enum column_format_type column_format= COLUMN_FORMAT_TYPE_DEFAULT;
843
 
    const CHARSET_INFO *charset= NULL;
 
890
    CHARSET_INFO *charset=NULL;
844
891
    LEX_STRING comment;
845
 
    virtual_column_info *vcol_info= NULL;
846
 
    bool fld_is_stored= true;
847
892
 
848
893
    if (field_extra_info)
849
894
    {
859
904
      pack_flag=    uint2korr(strpos+8);
860
905
      unireg_type=  (uint) strpos[10];
861
906
      interval_nr=  (uint) strpos[12];
862
 
      uint32_t comment_length=uint2korr(strpos+15);
 
907
      uint comment_length=uint2korr(strpos+15);
863
908
      field_type=(enum_field_types) (uint) strpos[13];
864
909
 
865
910
      {
872
917
          goto err;
873
918
        }
874
919
      }
875
 
      if (field_type == DRIZZLE_TYPE_VIRTUAL)
876
 
      {
877
 
        assert(interval_nr); // Expect non-null expression
878
 
        /* 
879
 
          The interval_id byte in the .frm file stores the length of the
880
 
          expression statement for a virtual column.
881
 
        */
882
 
        vcol_info_length= interval_nr;
883
 
        interval_nr= 0;
884
 
      }
885
920
      if (!comment_length)
886
921
      {
887
922
        comment.str= (char*) "";
893
928
        comment.length= comment_length;
894
929
        comment_pos+=   comment_length;
895
930
      }
896
 
      if (vcol_info_length)
897
 
      {
898
 
        /*
899
 
          Get virtual column data stored in the .frm file as follows:
900
 
          byte 1      = 1 (always 1 to allow for future extensions)
901
 
          byte 2      = sql_type
902
 
          byte 3      = flags (as of now, 0 - no flags, 1 - field is physically stored)
903
 
          byte 4-...  = virtual column expression (text data)
904
 
        */
905
 
        vcol_info= new virtual_column_info();
906
 
        if ((uint)vcol_screen_pos[0] != 1)
907
 
        {
908
 
          error= 4;
909
 
          goto err;
910
 
        }
911
 
        field_type= (enum_field_types) (unsigned char) vcol_screen_pos[1];
912
 
        fld_is_stored= (bool) (uint) vcol_screen_pos[2];
913
 
        vcol_info->expr_str.str= (char *)memdup_root(&share->mem_root,
914
 
                                                     vcol_screen_pos+
915
 
                                                       (uint)FRM_VCOL_HEADER_SIZE,
916
 
                                                     vcol_info_length-
917
 
                                                       (uint)FRM_VCOL_HEADER_SIZE);
918
 
        vcol_info->expr_str.length= vcol_info_length-(uint)FRM_VCOL_HEADER_SIZE;
919
 
        vcol_screen_pos+= vcol_info_length;
920
 
        share->vfields++;
921
 
      }
922
931
    }
923
932
    else
924
933
    {
951
960
      }
952
961
      else
953
962
        charset= share->table_charset;
954
 
      memset(&comment, 0, sizeof(comment));
 
963
      bzero((char*) &comment, sizeof(comment));
955
964
    }
956
965
 
957
966
    if (interval_nr && charset->mbminlen > 1)
960
969
      TYPELIB *interval= share->intervals + interval_nr - 1;
961
970
      unhex_type2(interval);
962
971
    }
 
972
    
 
973
#ifndef TO_BE_DELETED_ON_PRODUCTION
 
974
    if (field_type == MYSQL_TYPE_NEWDECIMAL && !share->mysql_version)
 
975
    {
 
976
      /*
 
977
        Fix pack length of old decimal values from 5.0.3 -> 5.0.4
 
978
        The difference is that in the old version we stored precision
 
979
        in the .frm table while we now store the display_length
 
980
      */
 
981
      uint decimals= f_decimals(pack_flag);
 
982
      field_length= my_decimal_precision_to_length(field_length,
 
983
                                                   decimals,
 
984
                                                   f_is_dec(pack_flag) == 0);
 
985
      sql_print_error("Found incompatible DECIMAL field '%s' in %s; "
 
986
                      "Please do \"ALTER TABLE '%s' FORCE\" to fix it!",
 
987
                      share->fieldnames.type_names[i], share->table_name.str,
 
988
                      share->table_name.str);
 
989
      push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
 
990
                          ER_CRASHED_ON_USAGE,
 
991
                          "Found incompatible DECIMAL field '%s' in %s; "
 
992
                          "Please do \"ALTER TABLE '%s' FORCE\" to fix it!",
 
993
                          share->fieldnames.type_names[i],
 
994
                          share->table_name.str,
 
995
                          share->table_name.str);
 
996
      share->crashed= 1;                        // Marker for CHECK TABLE
 
997
    }
 
998
#endif
963
999
 
964
1000
    *field_ptr= reg_field=
965
1001
      make_field(share, record+recpos,
966
 
                 (uint32_t) field_length,
 
1002
                 (uint32) field_length,
967
1003
                 null_pos, null_bit_pos,
968
1004
                 pack_flag,
969
1005
                 field_type,
982
1018
    reg_field->flags|= ((uint)column_format << COLUMN_FORMAT_FLAGS);
983
1019
    reg_field->field_index= i;
984
1020
    reg_field->comment=comment;
985
 
    reg_field->vcol_info= vcol_info;
986
 
    reg_field->is_stored= fld_is_stored;
987
1021
    if (!(reg_field->flags & NOT_NULL_FLAG))
988
1022
    {
989
1023
      if (!(null_bit_pos= (null_bit_pos + 1) & 7))
999
1033
 
1000
1034
    if (use_hash)
1001
1035
      (void) my_hash_insert(&share->name_hash,
1002
 
                            (unsigned char*) field_ptr); // never fail
1003
 
    if (!reg_field->is_stored)
1004
 
    {
1005
 
      share->stored_fields--;
1006
 
      if (share->stored_rec_length>=recpos)
1007
 
        share->stored_rec_length= recpos-1;
1008
 
    }
 
1036
                            (uchar*) field_ptr); // never fail
1009
1037
  }
1010
1038
  *field_ptr=0;                                 // End marker
1011
 
  /* Sanity checks: */
1012
 
  assert(share->fields>=share->stored_fields);
1013
 
  assert(share->reclength>=share->stored_rec_length);
1014
1039
 
1015
1040
  /* Fix key->name and key_part->field */
1016
1041
  if (key_parts)
1017
1042
  {
1018
 
    uint32_t primary_key=(uint) (find_type((char*) primary_key_name,
 
1043
    uint primary_key=(uint) (find_type((char*) primary_key_name,
1019
1044
                                       &share->keynames, 3) - 1);
1020
1045
    int64_t ha_option= handler_file->ha_table_flags();
1021
1046
    keyinfo= share->key_info;
1022
1047
    key_part= keyinfo->key_part;
1023
1048
 
1024
 
    for (uint32_t key=0 ; key < share->keys ; key++,keyinfo++)
 
1049
    for (uint key=0 ; key < share->keys ; key++,keyinfo++)
1025
1050
    {
1026
 
      uint32_t usable_parts= 0;
 
1051
      uint usable_parts= 0;
1027
1052
      keyinfo->name=(char*) share->keynames.type_names[key];
1028
1053
 
1029
1054
      if (primary_key >= MAX_KEY && (keyinfo->flags & HA_NOSAME))
1035
1060
        primary_key=key;
1036
1061
        for (i=0 ; i < keyinfo->key_parts ;i++)
1037
1062
        {
1038
 
          uint32_t fieldnr= key_part[i].fieldnr;
 
1063
          uint fieldnr= key_part[i].fieldnr;
1039
1064
          if (!fieldnr ||
1040
1065
              share->field[fieldnr-1]->null_ptr ||
1041
1066
              share->field[fieldnr-1]->key_length() !=
1051
1076
      {
1052
1077
        Field *field;
1053
1078
        if (new_field_pack_flag <= 1)
1054
 
          key_part->fieldnr= (uint16_t) find_field(share->field,
 
1079
          key_part->fieldnr= (uint16) find_field(share->field,
1055
1080
                                                 share->default_values,
1056
1081
                                                 (uint) key_part->offset,
1057
1082
                                                 (uint) key_part->length);
1064
1089
        key_part->type= field->key_type();
1065
1090
        if (field->null_ptr)
1066
1091
        {
1067
 
          key_part->null_offset=(uint) ((unsigned char*) field->null_ptr -
 
1092
          key_part->null_offset=(uint) ((uchar*) field->null_ptr -
1068
1093
                                        share->default_values);
1069
1094
          key_part->null_bit= field->null_bit;
1070
1095
          key_part->store_length+=HA_KEY_NULL_LENGTH;
1072
1097
          keyinfo->extra_length+= HA_KEY_NULL_LENGTH;
1073
1098
          keyinfo->key_length+= HA_KEY_NULL_LENGTH;
1074
1099
        }
1075
 
        if (field->type() == DRIZZLE_TYPE_BLOB ||
1076
 
            field->real_type() == DRIZZLE_TYPE_VARCHAR)
 
1100
        if (field->type() == MYSQL_TYPE_BLOB ||
 
1101
            field->real_type() == MYSQL_TYPE_VARCHAR)
1077
1102
        {
1078
 
          if (field->type() == DRIZZLE_TYPE_BLOB)
 
1103
          if (field->type() == MYSQL_TYPE_BLOB)
1079
1104
            key_part->key_part_flag|= HA_BLOB_PART;
1080
1105
          else
1081
1106
            key_part->key_part_flag|= HA_VAR_LENGTH_PART;
1121
1146
        }
1122
1147
        if (field->key_length() != key_part->length)
1123
1148
        {
 
1149
#ifndef TO_BE_DELETED_ON_PRODUCTION
 
1150
          if (field->type() == MYSQL_TYPE_NEWDECIMAL)
 
1151
          {
 
1152
            /*
 
1153
              Fix a fatal error in decimal key handling that causes crashes
 
1154
              on Innodb. We fix it by reducing the key length so that
 
1155
              InnoDB never gets a too big key when searching.
 
1156
              This allows the end user to do an ALTER TABLE to fix the
 
1157
              error.
 
1158
            */
 
1159
            keyinfo->key_length-= (key_part->length - field->key_length());
 
1160
            key_part->store_length-= (uint16)(key_part->length -
 
1161
                                              field->key_length());
 
1162
            key_part->length= (uint16)field->key_length();
 
1163
            sql_print_error("Found wrong key definition in %s; "
 
1164
                            "Please do \"ALTER TABLE '%s' FORCE \" to fix it!",
 
1165
                            share->table_name.str,
 
1166
                            share->table_name.str);
 
1167
            push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
 
1168
                                ER_CRASHED_ON_USAGE,
 
1169
                                "Found wrong key definition in %s; "
 
1170
                                "Please do \"ALTER TABLE '%s' FORCE\" to fix "
 
1171
                                "it!",
 
1172
                                share->table_name.str,
 
1173
                                share->table_name.str);
 
1174
            share->crashed= 1;                // Marker for CHECK TABLE
 
1175
            continue;
 
1176
          }
 
1177
#endif
1124
1178
          key_part->key_part_flag|= HA_PART_KEY_SEG;
1125
1179
        }
1126
1180
      }
1161
1215
  }
1162
1216
  else
1163
1217
    share->primary_key= MAX_KEY;
1164
 
  if (disk_buff)
1165
 
    free(disk_buff);
1166
 
  disk_buff= NULL;
 
1218
  x_free((uchar*) disk_buff);
 
1219
  disk_buff=0;
1167
1220
  if (new_field_pack_flag <= 1)
1168
1221
  {
1169
1222
    /* Old file format with default as not null */
1170
 
    uint32_t null_length= (share->null_fields+7)/8;
1171
 
    memset(share->default_values + (null_flags - (unsigned char*) record), 
 
1223
    uint null_length= (share->null_fields+7)/8;
 
1224
    bfill(share->default_values + (null_flags - (uchar*) record),
1172
1225
          null_length, 255);
1173
1226
  }
1174
1227
 
1192
1245
  if (share->blob_fields)
1193
1246
  {
1194
1247
    Field **ptr;
1195
 
    uint32_t k, *save;
 
1248
    uint k, *save;
1196
1249
 
1197
1250
    /* Store offsets to blob fields to find them fast */
1198
1251
    if (!(share->blob_field= save=
1210
1263
    the correct null_bytes can now be set, since bitfields have been taken
1211
1264
    into account
1212
1265
  */
1213
 
  share->null_bytes= (null_pos - (unsigned char*) null_flags +
 
1266
  share->null_bytes= (null_pos - (uchar*) null_flags +
1214
1267
                      (null_bit_pos + 7) / 8);
1215
1268
  share->last_null_bit_pos= null_bit_pos;
1216
1269
 
1225
1278
 
1226
1279
  delete handler_file;
1227
1280
  if (buff)
1228
 
    free(buff);
 
1281
    my_free(buff, MYF(0));
1229
1282
  return (0);
1230
1283
 
1231
1284
 err:
1232
1285
  if (buff)
1233
 
    free(buff);
 
1286
    my_free(buff, MYF(0));
1234
1287
  share->error= error;
1235
1288
  share->open_errno= my_errno;
1236
1289
  share->errarg= errarg;
1237
 
  if (disk_buff)
1238
 
    free(disk_buff);
 
1290
  x_free((uchar*) disk_buff);
 
1291
  delete crypted;
1239
1292
  delete handler_file;
1240
1293
  hash_free(&share->name_hash);
1241
1294
 
1245
1298
 
1246
1299
 
1247
1300
/*
1248
 
  Clear flag GET_FIXED_FIELDS_FLAG in all fields of the table.
1249
 
  This routine is used for error handling purposes.
1250
 
 
1251
 
  SYNOPSIS
1252
 
    clear_field_flag()
1253
 
    table                Table object for which virtual columns are set-up
1254
 
 
1255
 
  RETURN VALUE
1256
 
    NONE
1257
 
*/
1258
 
static void clear_field_flag(Table *table)
1259
 
{
1260
 
  Field **ptr;
1261
 
 
1262
 
  for (ptr= table->field; *ptr; ptr++)
1263
 
    (*ptr)->flags&= (~GET_FIXED_FIELDS_FLAG);
1264
 
}
1265
 
 
1266
 
/*
1267
 
  The function uses the feature in fix_fields where the flag 
1268
 
  GET_FIXED_FIELDS_FLAG is set for all fields in the item tree.
1269
 
  This field must always be reset before returning from the function
1270
 
  since it is used for other purposes as well.
1271
 
 
1272
 
  SYNOPSIS
1273
 
    fix_fields_vcol_func()
1274
 
    thd                  The thread object
1275
 
    func_item            The item tree reference of the virtual columnfunction
1276
 
    table                The table object
1277
 
    field_name           The name of the processed field
1278
 
 
1279
 
  RETURN VALUE
1280
 
    true                 An error occurred, something was wrong with the
1281
 
                         function.
1282
 
    false                Ok, a partition field array was created
1283
 
*/
1284
 
 
1285
 
bool fix_fields_vcol_func(THD *thd,
1286
 
                          Item* func_expr,
1287
 
                          Table *table,
1288
 
                          const char *field_name)
1289
 
{
1290
 
  uint dir_length, home_dir_length;
1291
 
  bool result= true;
1292
 
  TableList tables;
1293
 
  TableList *save_table_list, *save_first_table, *save_last_table;
1294
 
  int error;
1295
 
  Name_resolution_context *context;
1296
 
  const char *save_where;
1297
 
  char* db_name;
1298
 
  char db_name_string[FN_REFLEN];
1299
 
  bool save_use_only_table_context;
1300
 
  Field **ptr, *field;
1301
 
  enum_mark_columns save_mark_used_columns= thd->mark_used_columns;
1302
 
  assert(func_expr);
1303
 
 
1304
 
  /*
1305
 
    Set-up the TABLE_LIST object to be a list with a single table
1306
 
    Set the object to zero to create NULL pointers and set alias
1307
 
    and real name to table name and get database name from file name.
1308
 
  */
1309
 
 
1310
 
  bzero((void*)&tables, sizeof(TableList));
1311
 
  tables.alias= tables.table_name= (char*) table->s->table_name.str;
1312
 
  tables.table= table;
1313
 
  tables.next_local= NULL;
1314
 
  tables.next_name_resolution_table= NULL;
1315
 
  memcpy(db_name_string,
1316
 
         table->s->normalized_path.str,
1317
 
         table->s->normalized_path.length);
1318
 
  dir_length= dirname_length(db_name_string);
1319
 
  db_name_string[dir_length - 1]= 0;
1320
 
  home_dir_length= dirname_length(db_name_string);
1321
 
  db_name= &db_name_string[home_dir_length];
1322
 
  tables.db= db_name;
1323
 
 
1324
 
  thd->mark_used_columns= MARK_COLUMNS_NONE;
1325
 
 
1326
 
  context= thd->lex->current_context();
1327
 
  table->map= 1; //To ensure correct calculation of const item
1328
 
  table->get_fields_in_item_tree= true;
1329
 
  save_table_list= context->table_list;
1330
 
  save_first_table= context->first_name_resolution_table;
1331
 
  save_last_table= context->last_name_resolution_table;
1332
 
  context->table_list= &tables;
1333
 
  context->first_name_resolution_table= &tables;
1334
 
  context->last_name_resolution_table= NULL;
1335
 
  func_expr->walk(&Item::change_context_processor, 0, (unsigned char*) context);
1336
 
  save_where= thd->where;
1337
 
  thd->where= "virtual column function";
1338
 
 
1339
 
  /* Save the context before fixing the fields*/
1340
 
  save_use_only_table_context= thd->lex->use_only_table_context;
1341
 
  thd->lex->use_only_table_context= true;
1342
 
  /* Fix fields referenced to by the virtual column function */
1343
 
  error= func_expr->fix_fields(thd, (Item**)0);
1344
 
  /* Restore the original context*/
1345
 
  thd->lex->use_only_table_context= save_use_only_table_context;
1346
 
  context->table_list= save_table_list;
1347
 
  context->first_name_resolution_table= save_first_table;
1348
 
  context->last_name_resolution_table= save_last_table;
1349
 
 
1350
 
  if (unlikely(error))
1351
 
  {
1352
 
    clear_field_flag(table);
1353
 
    goto end;
1354
 
  }
1355
 
  thd->where= save_where;
1356
 
  /* 
1357
 
    Walk through the Item tree checking if all items are valid 
1358
 
   to be part of the virtual column
1359
 
 */
1360
 
  error= func_expr->walk(&Item::check_vcol_func_processor, 0, NULL);
1361
 
  if (error)
1362
 
  {
1363
 
    my_error(ER_VIRTUAL_COLUMN_FUNCTION_IS_NOT_ALLOWED, MYF(0), field_name);
1364
 
    clear_field_flag(table);
1365
 
    goto end;
1366
 
  }
1367
 
  if (unlikely(func_expr->const_item()))
1368
 
  {
1369
 
    my_error(ER_CONST_EXPR_IN_VCOL, MYF(0));
1370
 
    clear_field_flag(table);
1371
 
    goto end;
1372
 
  }
1373
 
  /* Ensure that this virtual column is not based on another virtual field. */
1374
 
  ptr= table->field;
1375
 
  while ((field= *(ptr++))) 
1376
 
  {
1377
 
    if ((field->flags & GET_FIXED_FIELDS_FLAG) &&
1378
 
        (field->vcol_info))
1379
 
    {
1380
 
      my_error(ER_VCOL_BASED_ON_VCOL, MYF(0));
1381
 
      clear_field_flag(table);
1382
 
      goto end;
1383
 
    }
1384
 
  }
1385
 
  /*
1386
 
    Cleanup the fields marked with flag GET_FIXED_FIELDS_FLAG
1387
 
    when calling fix_fields.
1388
 
  */
1389
 
  clear_field_flag(table);
1390
 
  result= false;
1391
 
 
1392
 
end:
1393
 
  table->get_fields_in_item_tree= false;
1394
 
  thd->mark_used_columns= save_mark_used_columns;
1395
 
  table->map= 0; //Restore old value
1396
 
  return(result);
1397
 
}
1398
 
 
1399
 
/*
1400
 
  Unpack the definition of a virtual column
1401
 
 
1402
 
  SYNOPSIS
1403
 
    unpack_vcol_info_from_frm()
1404
 
    thd                  Thread handler
1405
 
    table                Table with the checked field
1406
 
    field                Pointer to Field object
1407
 
    open_mode            Open table mode needed to determine
1408
 
                         which errors need to be generated in a failure
1409
 
    error_reported       updated flag for the caller that no other error
1410
 
                         messages are to be generated.
1411
 
 
1412
 
  RETURN VALUES
1413
 
    true            Failure
1414
 
    false           Success
1415
 
*/
1416
 
bool unpack_vcol_info_from_frm(THD *thd,
1417
 
                               Table *table,
1418
 
                               Field *field,
1419
 
                               LEX_STRING *vcol_expr,
1420
 
                               open_table_mode open_mode,
1421
 
                               bool *error_reported)
1422
 
{
1423
 
  assert(vcol_expr);
1424
 
 
1425
 
  /* 
1426
 
    Step 1: Construct a statement for the parser.
1427
 
    The parsed string needs to take the following format:
1428
 
    "PARSE_VCOL_EXPR (<expr_string_from_frm>)"
1429
 
  */
1430
 
  char *vcol_expr_str;
1431
 
  int str_len= 0;
1432
 
  
1433
 
  if (!(vcol_expr_str= (char*) alloc_root(&table->mem_root,
1434
 
                                          vcol_expr->length + 
1435
 
                                            parse_vcol_keyword.length + 3)))
1436
 
  {
1437
 
    return(true);
1438
 
  }
1439
 
  memcpy(vcol_expr_str,
1440
 
         (char*) parse_vcol_keyword.str,
1441
 
         parse_vcol_keyword.length);
1442
 
  str_len= parse_vcol_keyword.length;
1443
 
  memcpy(vcol_expr_str + str_len, "(", 1);
1444
 
  str_len++;
1445
 
  memcpy(vcol_expr_str + str_len, 
1446
 
         (char*) vcol_expr->str, 
1447
 
         vcol_expr->length);
1448
 
  str_len+= vcol_expr->length;
1449
 
  memcpy(vcol_expr_str + str_len, ")", 1);
1450
 
  str_len++;
1451
 
  memcpy(vcol_expr_str + str_len, "\0", 1);
1452
 
  str_len++;
1453
 
  Lex_input_stream lip(thd, vcol_expr_str, str_len);
1454
 
 
1455
 
  /* 
1456
 
    Step 2: Setup thd for parsing.
1457
 
    1) make Item objects be created in the memory allocated for the Table
1458
 
       object (not TABLE_SHARE)
1459
 
    2) ensure that created Item's are not put on to thd->free_list 
1460
 
       (which is associated with the parsed statement and hence cleared after 
1461
 
       the parsing)
1462
 
    3) setup a flag in the LEX structure to allow "PARSE_VCOL_EXPR" 
1463
 
       to be parsed as a SQL command.
1464
 
  */
1465
 
  MEM_ROOT **root_ptr, *old_root;
1466
 
  Item *backup_free_list= thd->free_list;
1467
 
  root_ptr= (MEM_ROOT **)pthread_getspecific(THR_MALLOC);
1468
 
  old_root= *root_ptr;
1469
 
  *root_ptr= &table->mem_root;
1470
 
  thd->free_list= NULL;
1471
 
  thd->lex->parse_vcol_expr= true;
1472
 
 
1473
 
  /* 
1474
 
    Step 3: Use the parser to build an Item object from.
1475
 
  */
1476
 
  if (parse_sql(thd, &lip))
1477
 
  {
1478
 
    goto parse_err;
1479
 
  }
1480
 
  /* From now on use vcol_info generated by the parser. */
1481
 
  field->vcol_info= thd->lex->vcol_info;
1482
 
 
1483
 
  /* Validate the Item tree. */
1484
 
  if (fix_fields_vcol_func(thd,
1485
 
                           field->vcol_info->expr_item,
1486
 
                           table,
1487
 
                           field->field_name))
1488
 
  {
1489
 
    if (open_mode == OTM_CREATE)
1490
 
    {
1491
 
      /*
1492
 
        During CREATE/ALTER TABLE it is ok to receive errors here.
1493
 
        It is not ok if it happens during the opening of an frm
1494
 
        file as part of a normal query.
1495
 
      */
1496
 
      *error_reported= true;
1497
 
    }
1498
 
    field->vcol_info= NULL;
1499
 
    goto parse_err;
1500
 
  }
1501
 
  field->vcol_info->item_free_list= thd->free_list;
1502
 
  thd->free_list= backup_free_list;
1503
 
  *root_ptr= old_root;
1504
 
 
1505
 
  return(false);
1506
 
 
1507
 
parse_err:
1508
 
  thd->lex->parse_vcol_expr= false;
1509
 
  thd->free_items();
1510
 
  *root_ptr= old_root;
1511
 
  thd->free_list= backup_free_list;
1512
 
  return(true);
1513
 
}
1514
 
 
1515
 
 
1516
 
/*
1517
1301
  Open a table based on a TABLE_SHARE
1518
1302
 
1519
1303
  SYNOPSIS
1542
1326
*/
1543
1327
 
1544
1328
int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias,
1545
 
                          uint32_t db_stat, uint32_t prgflag, uint32_t ha_open_flags,
1546
 
                          Table *outparam, open_table_mode open_mode)
 
1329
                          uint db_stat, uint prgflag, uint ha_open_flags,
 
1330
                          TABLE *outparam, open_table_mode open_mode)
1547
1331
{
1548
1332
  int error;
1549
 
  uint32_t records, i, bitmap_size;
 
1333
  uint records, i, bitmap_size;
1550
1334
  bool error_reported= false;
1551
 
  unsigned char *record, *bitmaps;
1552
 
  Field **field_ptr, **vfield_ptr;
 
1335
  uchar *record, *bitmaps;
 
1336
  Field **field_ptr;
1553
1337
 
1554
1338
  /* Parsing of partitioning information from .frm needs thd->lex set up. */
1555
1339
  assert(thd->lex->is_lex_started);
1556
1340
 
1557
1341
  error= 1;
1558
 
  memset(outparam, 0, sizeof(*outparam));
 
1342
  bzero((char*) outparam, sizeof(*outparam));
1559
1343
  outparam->in_use= thd;
1560
1344
  outparam->s= share;
1561
1345
  outparam->db_stat= db_stat;
1591
1375
  if (prgflag & (READ_ALL+EXTRA_RECORD))
1592
1376
    records++;
1593
1377
 
1594
 
  if (!(record= (unsigned char*) alloc_root(&outparam->mem_root,
 
1378
  if (!(record= (uchar*) alloc_root(&outparam->mem_root,
1595
1379
                                   share->rec_buff_length * records)))
1596
1380
    goto err;                                   /* purecov: inspected */
1597
1381
 
1631
1415
 
1632
1416
  outparam->field= field_ptr;
1633
1417
 
1634
 
  record= (unsigned char*) outparam->record[0]-1;       /* Fieldstart = 1 */
 
1418
  record= (uchar*) outparam->record[0]-1;       /* Fieldstart = 1 */
1635
1419
  if (share->null_field_first)
1636
 
    outparam->null_flags= (unsigned char*) record+1;
 
1420
    outparam->null_flags= (uchar*) record+1;
1637
1421
  else
1638
 
    outparam->null_flags= (unsigned char*) (record+ 1+ share->reclength -
 
1422
    outparam->null_flags= (uchar*) (record+ 1+ share->reclength -
1639
1423
                                    share->null_bytes);
1640
1424
 
1641
1425
  /* Setup copy of fields from share, but use the right alias and record */
1658
1442
  {
1659
1443
    KEY *key_info, *key_info_end;
1660
1444
    KEY_PART_INFO *key_part;
1661
 
    uint32_t n_length;
 
1445
    uint n_length;
1662
1446
    n_length= share->keys*sizeof(KEY) + share->key_parts*sizeof(KEY_PART_INFO);
1663
1447
    if (!(key_info= (KEY*) alloc_root(&outparam->mem_root, n_length)))
1664
1448
      goto err;
1665
1449
    outparam->key_info= key_info;
1666
 
    key_part= (reinterpret_cast<KEY_PART_INFO*> (key_info+share->keys));
 
1450
    key_part= (my_reinterpret_cast(KEY_PART_INFO*) (key_info+share->keys));
1667
1451
    
1668
1452
    memcpy(key_info, share->key_info, sizeof(*key_info)*share->keys);
1669
1453
    memcpy(key_part, share->key_info[0].key_part, (sizeof(*key_part) *
1699
1483
    }
1700
1484
  }
1701
1485
 
1702
 
  /*
1703
 
    Process virtual columns, if any.
1704
 
  */
1705
 
  if (not (vfield_ptr = (Field **) alloc_root(&outparam->mem_root,
1706
 
                                              (uint) ((share->vfields+1)*
1707
 
                                                      sizeof(Field*)))))
1708
 
    goto err;
1709
 
 
1710
 
  outparam->vfield= vfield_ptr;
1711
 
  
1712
 
  for (field_ptr= outparam->field; *field_ptr; field_ptr++)
1713
 
  {
1714
 
    if ((*field_ptr)->vcol_info)
1715
 
    {
1716
 
      if (unpack_vcol_info_from_frm(thd,
1717
 
                                    outparam,
1718
 
                                    *field_ptr,
1719
 
                                    &(*field_ptr)->vcol_info->expr_str,
1720
 
                                    open_mode,
1721
 
                                    &error_reported))
1722
 
      {
1723
 
        error= 4; // in case no error is reported
1724
 
        goto err;
1725
 
      }
1726
 
      *(vfield_ptr++)= *field_ptr;
1727
 
    }
1728
 
  }
1729
 
  *vfield_ptr= NULL;                              // End marker
1730
 
  /* Check virtual columns against table's storage engine. */
1731
 
  if ((share->vfields && outparam->file) && 
1732
 
        (not outparam->file->check_if_supported_virtual_columns()))
1733
 
  {
1734
 
    my_error(ER_UNSUPPORTED_ACTION_ON_VIRTUAL_COLUMN,
1735
 
             MYF(0), 
1736
 
             "Specified storage engine");
1737
 
    error_reported= true;
1738
 
    goto err;
1739
 
  }
1740
 
 
1741
1486
  /* Allocate bitmaps */
1742
1487
 
1743
1488
  bitmap_size= share->column_bitmap_size;
1744
 
  if (!(bitmaps= (unsigned char*) alloc_root(&outparam->mem_root, bitmap_size*3)))
 
1489
  if (!(bitmaps= (uchar*) alloc_root(&outparam->mem_root, bitmap_size*3)))
1745
1490
    goto err;
1746
1491
  bitmap_init(&outparam->def_read_set,
1747
1492
              (my_bitmap_map*) bitmaps, share->fields, false);
1760
1505
                  ha_open(outparam, share->normalized_path.str,
1761
1506
                          (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
1762
1507
                          (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE :
1763
 
                           (db_stat & HA_WAIT_IF_LOCKED) ?  HA_OPEN_WAIT_IF_LOCKED :
 
1508
                           ((db_stat & HA_WAIT_IF_LOCKED) ||
 
1509
                            (specialflag & SPECIAL_WAIT_IF_LOCKED)) ?
 
1510
                           HA_OPEN_WAIT_IF_LOCKED :
1764
1511
                           (db_stat & (HA_ABORT_IF_LOCKED | HA_GET_INFO)) ?
1765
1512
                          HA_OPEN_ABORT_IF_LOCKED :
1766
1513
                           HA_OPEN_IGNORE_IF_LOCKED) | ha_open_flags))))
1800
1547
  }
1801
1548
 
1802
1549
#if defined(HAVE_purify) 
1803
 
  memset(bitmaps, 0, bitmap_size*3);
 
1550
  bzero((char*) bitmaps, bitmap_size*3);
1804
1551
#endif
1805
1552
 
1806
 
  outparam->no_replicate= outparam->file;
 
1553
  outparam->no_replicate= outparam->file &&
 
1554
                          test(outparam->file->ha_table_flags() &
 
1555
                               HA_HAS_OWN_BINLOGGING);
1807
1556
  thd->status_var.opened_tables++;
1808
1557
 
1809
1558
  return (0);
1814
1563
  delete outparam->file;
1815
1564
  outparam->file= 0;                            // For easier error checking
1816
1565
  outparam->db_stat=0;
1817
 
  free_root(&outparam->mem_root, MYF(0));       // Safe to call on zeroed root
1818
 
  free((char*) outparam->alias);
 
1566
  free_root(&outparam->mem_root, MYF(0));       // Safe to call on bzero'd root
 
1567
  my_free((char*) outparam->alias, MYF(MY_ALLOW_ZERO_PTR));
1819
1568
  return (error);
1820
1569
}
1821
1570
 
1825
1574
 
1826
1575
  SYNOPSIS
1827
1576
    closefrm()
1828
 
    table               Table object to free
 
1577
    table               TABLE object to free
1829
1578
    free_share          Is 1 if we also want to free table_share
1830
1579
*/
1831
1580
 
1832
 
int closefrm(register Table *table, bool free_share)
 
1581
int closefrm(register TABLE *table, bool free_share)
1833
1582
{
1834
1583
  int error=0;
1835
1584
 
1836
1585
  if (table->db_stat)
1837
1586
    error=table->file->close();
1838
 
  free((char*) table->alias);
 
1587
  my_free((char*) table->alias, MYF(MY_ALLOW_ZERO_PTR));
1839
1588
  table->alias= 0;
1840
1589
  if (table->field)
1841
1590
  {
1859
1608
 
1860
1609
/* Deallocate temporary blob storage */
1861
1610
 
1862
 
void free_blobs(register Table *table)
 
1611
void free_blobs(register TABLE *table)
1863
1612
{
1864
 
  uint32_t *ptr, *end;
1865
 
  for (ptr= table->getBlobField(), end=ptr + table->sizeBlobFields();
 
1613
  uint *ptr, *end;
 
1614
  for (ptr= table->s->blob_field, end=ptr + table->s->blob_fields ;
1866
1615
       ptr != end ;
1867
1616
       ptr++)
1868
1617
    ((Field_blob*) table->field[*ptr])->free();
1870
1619
 
1871
1620
 
1872
1621
        /* Find where a form starts */
1873
 
        /* if formname is NULL then only formnames is read */
 
1622
        /* if formname is NullS then only formnames is read */
1874
1623
 
1875
 
ulong get_form_pos(File file, unsigned char *head, TYPELIB *save_names)
 
1624
ulong get_form_pos(File file, uchar *head, TYPELIB *save_names)
1876
1625
{
1877
 
  uint32_t a_length,names,length;
1878
 
  unsigned char *pos,*buf;
 
1626
  uint a_length,names,length;
 
1627
  uchar *pos,*buf;
1879
1628
  ulong ret_value=0;
1880
1629
 
1881
1630
  names=uint2korr(head+8);
1889
1638
  if (names)
1890
1639
  {
1891
1640
    length=uint2korr(head+4);
1892
 
    my_seek(file,64L,MY_SEEK_SET,MYF(0));
1893
 
    if (!(buf= (unsigned char*) my_malloc((size_t) length+a_length+names*4,
 
1641
    VOID(my_seek(file,64L,MY_SEEK_SET,MYF(0)));
 
1642
    if (!(buf= (uchar*) my_malloc((size_t) length+a_length+names*4,
1894
1643
                                  MYF(MY_WME))) ||
1895
1644
        my_read(file, buf+a_length, (size_t) (length+names*4),
1896
1645
                MYF(MY_NABP)))
1897
1646
    {                                           /* purecov: inspected */
1898
 
      if (buf)
1899
 
        free(buf);
 
1647
      x_free((uchar*) buf);                     /* purecov: inspected */
1900
1648
      return(0L);                               /* purecov: inspected */
1901
1649
    }
1902
1650
    pos= buf+a_length+length;
1905
1653
  if (! save_names)
1906
1654
  {
1907
1655
    if (names)
1908
 
      free((unsigned char*) buf);
 
1656
      my_free((uchar*) buf,MYF(0));
1909
1657
  }
1910
1658
  else if (!names)
1911
 
    memset(save_names, 0, sizeof(save_names));
 
1659
    bzero((char*) save_names,sizeof(save_names));
1912
1660
  else
1913
1661
  {
1914
1662
    char *str;
1926
1674
    We add an \0 at end of the read string to make reading of C strings easier
1927
1675
*/
1928
1676
 
1929
 
int read_string(File file, unsigned char**to, size_t length)
 
1677
int read_string(File file, uchar**to, size_t length)
1930
1678
{
1931
1679
 
1932
 
  if (*to)
1933
 
    free(*to);
1934
 
  if (!(*to= (unsigned char*) my_malloc(length+1,MYF(MY_WME))) ||
 
1680
  x_free(*to);
 
1681
  if (!(*to= (uchar*) my_malloc(length+1,MYF(MY_WME))) ||
1935
1682
      my_read(file, *to, length,MYF(MY_NABP)))
1936
1683
  {
1937
 
    if (*to)
1938
 
      free(*to);
1939
 
    *to= NULL;
 
1684
    x_free(*to);                              /* purecov: inspected */
 
1685
    *to= 0;                                   /* purecov: inspected */
1940
1686
    return(1);                           /* purecov: inspected */
1941
1687
  }
1942
1688
  *((char*) *to+length)= '\0';
1946
1692
 
1947
1693
        /* Add a new form to a form file */
1948
1694
 
1949
 
ulong make_new_entry(File file, unsigned char *fileinfo, TYPELIB *formnames,
 
1695
ulong make_new_entry(File file, uchar *fileinfo, TYPELIB *formnames,
1950
1696
                     const char *newname)
1951
1697
{
1952
 
  uint32_t i,bufflength,maxlength,n_length,length,names;
 
1698
  uint i,bufflength,maxlength,n_length,length,names;
1953
1699
  ulong endpos,newpos;
1954
 
  unsigned char buff[IO_SIZE];
1955
 
  unsigned char *pos;
 
1700
  uchar buff[IO_SIZE];
 
1701
  uchar *pos;
1956
1702
 
1957
1703
  length=(uint) strlen(newname)+1;
1958
1704
  n_length=uint2korr(fileinfo+4);
1969
1715
 
1970
1716
    while (endpos > maxlength)
1971
1717
    {
1972
 
      my_seek(file,(ulong) (endpos-bufflength),MY_SEEK_SET,MYF(0));
 
1718
      VOID(my_seek(file,(ulong) (endpos-bufflength),MY_SEEK_SET,MYF(0)));
1973
1719
      if (my_read(file, buff, bufflength, MYF(MY_NABP+MY_WME)))
1974
1720
        return(0L);
1975
 
      my_seek(file,(ulong) (endpos-bufflength+IO_SIZE),MY_SEEK_SET,
1976
 
                   MYF(0));
 
1721
      VOID(my_seek(file,(ulong) (endpos-bufflength+IO_SIZE),MY_SEEK_SET,
 
1722
                   MYF(0)));
1977
1723
      if ((my_write(file, buff,bufflength,MYF(MY_NABP+MY_WME))))
1978
1724
        return(0);
1979
1725
      endpos-=bufflength; bufflength=IO_SIZE;
1980
1726
    }
1981
 
    memset(buff, 0, IO_SIZE);                   /* Null new block */
1982
 
    my_seek(file,(ulong) maxlength,MY_SEEK_SET,MYF(0));
 
1727
    bzero(buff,IO_SIZE);                        /* Null new block */
 
1728
    VOID(my_seek(file,(ulong) maxlength,MY_SEEK_SET,MYF(0)));
1983
1729
    if (my_write(file,buff,bufflength,MYF(MY_NABP+MY_WME)))
1984
1730
        return(0L);
1985
1731
    maxlength+=IO_SIZE;                         /* Fix old ref */
1986
1732
    int2store(fileinfo+6,maxlength);
1987
 
    for (i=names, pos= (unsigned char*) *formnames->type_names+n_length-1; i-- ;
 
1733
    for (i=names, pos= (uchar*) *formnames->type_names+n_length-1; i-- ;
1988
1734
         pos+=4)
1989
1735
    {
1990
1736
      endpos=uint4korr(pos)+IO_SIZE;
1995
1741
  if (n_length == 1 )
1996
1742
  {                                             /* First name */
1997
1743
    length++;
1998
 
    strxmov((char*) buff,"/",newname,"/",NULL);
 
1744
    VOID(strxmov((char*) buff,"/",newname,"/",NullS));
1999
1745
  }
2000
1746
  else
2001
 
    strxmov((char*) buff,newname,"/",NULL); /* purecov: inspected */
2002
 
  my_seek(file,63L+(ulong) n_length,MY_SEEK_SET,MYF(0));
 
1747
    VOID(strxmov((char*) buff,newname,"/",NullS)); /* purecov: inspected */
 
1748
  VOID(my_seek(file,63L+(ulong) n_length,MY_SEEK_SET,MYF(0)));
2003
1749
  if (my_write(file, buff, (size_t) length+1,MYF(MY_NABP+MY_WME)) ||
2004
 
      (names && my_write(file,(unsigned char*) (*formnames->type_names+n_length-1),
 
1750
      (names && my_write(file,(uchar*) (*formnames->type_names+n_length-1),
2005
1751
                         names*4, MYF(MY_NABP+MY_WME))) ||
2006
1752
      my_write(file, fileinfo+10, 4,MYF(MY_NABP+MY_WME)))
2007
1753
    return(0L); /* purecov: inspected */
2028
1774
      my_error(ER_NO_SUCH_TABLE, MYF(0), share->db.str, share->table_name.str);
2029
1775
    else
2030
1776
    {
2031
 
      strxmov(buff, share->normalized_path.str, reg_ext, NULL);
 
1777
      strxmov(buff, share->normalized_path.str, reg_ext, NullS);
2032
1778
      my_error((db_errno == EMFILE) ? ER_CANT_OPEN_FILE : ER_FILE_NOT_FOUND,
2033
1779
               errortype, buff, db_errno);
2034
1780
    }
2049
1795
    }
2050
1796
    err_no= (db_errno == ENOENT) ? ER_FILE_NOT_FOUND : (db_errno == EAGAIN) ?
2051
1797
      ER_FILE_USED : ER_CANT_OPEN_FILE;
2052
 
    strxmov(buff, share->normalized_path.str, datext, NULL);
 
1798
    strxmov(buff, share->normalized_path.str, datext, NullS);
2053
1799
    my_error(err_no,errortype, buff, db_errno);
2054
1800
    delete file;
2055
1801
    break;
2064
1810
      csname= tmp;
2065
1811
    }
2066
1812
    my_printf_error(ER_UNKNOWN_COLLATION,
2067
 
                    _("Unknown collation '%s' in table '%-.64s' definition"), 
 
1813
                    "Unknown collation '%s' in table '%-.64s' definition", 
2068
1814
                    MYF(0), csname, share->table_name.str);
2069
1815
    break;
2070
1816
  }
2071
1817
  case 6:
2072
 
    strxmov(buff, share->normalized_path.str, reg_ext, NULL);
 
1818
    strxmov(buff, share->normalized_path.str, reg_ext, NullS);
2073
1819
    my_printf_error(ER_NOT_FORM_FILE,
2074
 
                    _("Table '%-.64s' was created with a different version "
2075
 
                    "of MySQL and cannot be read"), 
 
1820
                    "Table '%-.64s' was created with a different version "
 
1821
                    "of MySQL and cannot be read", 
2076
1822
                    MYF(0), buff);
2077
1823
    break;
2078
1824
  case 8:
2079
1825
    break;
2080
1826
  default:                              /* Better wrong error than none */
2081
1827
  case 4:
2082
 
    strxmov(buff, share->normalized_path.str, reg_ext, NULL);
 
1828
    strxmov(buff, share->normalized_path.str, reg_ext, NullS);
2083
1829
    my_error(ER_NOT_FORM_FILE, errortype, buff, 0);
2084
1830
    break;
2085
1831
  }
2094
1840
        */
2095
1841
 
2096
1842
static void
2097
 
fix_type_pointers(const char ***array, TYPELIB *point_to_type, uint32_t types,
 
1843
fix_type_pointers(const char ***array, TYPELIB *point_to_type, uint types,
2098
1844
                  char **names)
2099
1845
{
2100
1846
  char *type_name, *ptr;
2108
1854
 
2109
1855
    if ((chr= *ptr))                    /* Test if empty type */
2110
1856
    {
2111
 
      while ((type_name=strchr(ptr+1,chr)) != NULL)
 
1857
      while ((type_name=strchr(ptr+1,chr)) != NullS)
2112
1858
      {
2113
1859
        *((*array)++) = ptr+1;
2114
1860
        *type_name= '\0';               /* End string */
2120
1866
      ptr++;
2121
1867
    point_to_type->count= (uint) (*array - point_to_type->type_names);
2122
1868
    point_to_type++;
2123
 
    *((*array)++)= NULL;                /* End of type */
 
1869
    *((*array)++)= NullS;               /* End of type */
2124
1870
  }
2125
1871
  *names=ptr;                           /* Update end */
2126
1872
  return;
2134
1880
    return 0;
2135
1881
  result->count=strings.elements;
2136
1882
  result->name="";
2137
 
  uint32_t nbytes= (sizeof(char*) + sizeof(uint)) * (result->count + 1);
 
1883
  uint nbytes= (sizeof(char*) + sizeof(uint)) * (result->count + 1);
2138
1884
  if (!(result->type_names= (const char**) alloc_root(mem_root, nbytes)))
2139
1885
    return 0;
2140
1886
  result->type_lengths= (uint*) (result->type_names + result->count + 1);
2141
1887
  List_iterator<String> it(strings);
2142
1888
  String *tmp;
2143
 
  for (uint32_t i=0; (tmp=it++) ; i++)
 
1889
  for (uint i=0; (tmp=it++) ; i++)
2144
1890
  {
2145
1891
    result->type_names[i]= tmp->ptr();
2146
1892
    result->type_lengths[i]= tmp->length();
2164
1910
   #  field number +1
2165
1911
*/
2166
1912
 
2167
 
static uint32_t find_field(Field **fields, unsigned char *record, uint32_t start, uint32_t length)
 
1913
static uint find_field(Field **fields, uchar *record, uint start, uint length)
2168
1914
{
2169
1915
  Field **field;
2170
 
  uint32_t i, pos;
 
1916
  uint i, pos;
2171
1917
 
2172
1918
  pos= 0;
2173
1919
  for (field= fields, i=1 ; *field ; i++,field++)
2221
1967
    May fail with some multibyte charsets though.
2222
1968
*/
2223
1969
 
2224
 
void append_unescaped(String *res, const char *pos, uint32_t length)
 
1970
void append_unescaped(String *res, const char *pos, uint length)
2225
1971
{
2226
1972
  const char *end= pos+length;
2227
1973
  res->append('\'');
2228
1974
 
2229
1975
  for (; pos != end ; pos++)
2230
1976
  {
2231
 
#if defined(USE_MB)
2232
 
    uint32_t mblen;
 
1977
#if defined(USE_MB) && MYSQL_VERSION_ID < 40100
 
1978
    uint mblen;
2233
1979
    if (use_mb(default_charset_info) &&
2234
1980
        (mblen= my_ismbchar(default_charset_info, pos, end)))
2235
1981
    {
2272
2018
        /* Create a .frm file */
2273
2019
 
2274
2020
File create_frm(THD *thd, const char *name, const char *db,
2275
 
                const char *table, uint32_t reclength, unsigned char *fileinfo,
2276
 
                HA_CREATE_INFO *create_info, uint32_t keys, KEY *key_info)
 
2021
                const char *table, uint reclength, uchar *fileinfo,
 
2022
                HA_CREATE_INFO *create_info, uint keys, KEY *key_info)
2277
2023
{
2278
2024
  register File file;
2279
2025
  ulong length;
2280
 
  unsigned char fill[IO_SIZE];
 
2026
  uchar fill[IO_SIZE];
2281
2027
  int create_flags= O_RDWR | O_TRUNC;
2282
2028
  ulong key_comment_total_bytes= 0;
2283
 
  uint32_t i;
 
2029
  uint i;
2284
2030
 
2285
2031
  if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
2286
 
    create_flags|= O_EXCL;
 
2032
    create_flags|= O_EXCL | O_NOFOLLOW;
2287
2033
 
2288
2034
  /* Fix this when we have new .frm files;  Current limit is 4G rows (QQ) */
2289
 
  if (create_info->max_rows > UINT32_MAX)
2290
 
    create_info->max_rows= UINT32_MAX;
2291
 
  if (create_info->min_rows > UINT32_MAX)
2292
 
    create_info->min_rows= UINT32_MAX;
 
2035
  if (create_info->max_rows > UINT_MAX32)
 
2036
    create_info->max_rows= UINT_MAX32;
 
2037
  if (create_info->min_rows > UINT_MAX32)
 
2038
    create_info->min_rows= UINT_MAX32;
2293
2039
 
2294
2040
  if ((file= my_create(name, CREATE_MODE, create_flags, MYF(0))) >= 0)
2295
2041
  {
2296
 
    uint32_t key_length, tmp_key_length;
2297
 
    uint32_t tmp;
2298
 
    memset(fileinfo, 0, 64);
 
2042
    uint key_length, tmp_key_length;
 
2043
    uint tmp;
 
2044
    bzero((char*) fileinfo,64);
2299
2045
    /* header */
2300
 
    fileinfo[0]=(unsigned char) 254;
 
2046
    fileinfo[0]=(uchar) 254;
2301
2047
    fileinfo[1]= 1;
2302
2048
    fileinfo[2]= FRM_VER+3+ test(create_info->varchar);
2303
2049
 
2304
 
    fileinfo[3]= (unsigned char) ha_legacy_type(
 
2050
    fileinfo[3]= (uchar) ha_legacy_type(
2305
2051
          ha_checktype(thd,ha_legacy_type(create_info->db_type),0,0));
2306
2052
    fileinfo[4]=1;
2307
2053
    int2store(fileinfo+6,IO_SIZE);              /* Next block starts here */
2345
2091
    int4store(fileinfo+34,create_info->avg_row_length);
2346
2092
    fileinfo[38]= (create_info->default_table_charset ?
2347
2093
                   create_info->default_table_charset->number : 0);
2348
 
    fileinfo[39]= (unsigned char) create_info->page_checksum;
2349
 
    fileinfo[40]= (unsigned char) create_info->row_type;
2350
 
    /* Next few bytes were for RAID support */
 
2094
    fileinfo[39]= (uchar) ((uint) create_info->transactional |
 
2095
                           ((uint) create_info->page_checksum << 2));
 
2096
    fileinfo[40]= (uchar) create_info->row_type;
 
2097
    /* Next few bytes where for RAID support */
2351
2098
    fileinfo[41]= 0;
2352
2099
    fileinfo[42]= 0;
2353
 
    int4store(fileinfo+43,create_info->block_size);
2354
 
 
 
2100
    fileinfo[43]= 0;
2355
2101
    fileinfo[44]= 0;
2356
2102
    fileinfo[45]= 0;
2357
2103
    fileinfo[46]= 0;
2358
2104
    int4store(fileinfo+47, key_length);
2359
 
    tmp= DRIZZLE_VERSION_ID;          // Store to avoid warning from int4store
 
2105
    tmp= MYSQL_VERSION_ID;          // Store to avoid warning from int4store
2360
2106
    int4store(fileinfo+51, tmp);
2361
2107
    int4store(fileinfo+55, create_info->extra_size);
2362
2108
    /*
2364
2110
      61 for default_part_db_type
2365
2111
    */
2366
2112
    int2store(fileinfo+62, create_info->key_block_size);
2367
 
    memset(fill, 0, IO_SIZE);
 
2113
    bzero(fill,IO_SIZE);
2368
2114
    for (; length > IO_SIZE ; length-= IO_SIZE)
2369
2115
    {
2370
2116
      if (my_write(file,fill, IO_SIZE, MYF(MY_WME | MY_NABP)))
2371
2117
      {
2372
 
        my_close(file,MYF(0));
2373
 
        my_delete(name,MYF(0));
 
2118
        VOID(my_close(file,MYF(0)));
 
2119
        VOID(my_delete(name,MYF(0)));
2374
2120
        return(-1);
2375
2121
      }
2376
2122
    }
2385
2131
  return (file);
2386
2132
} /* create_frm */
2387
2133
 
2388
 
/*
2389
 
  Set up column usage bitmaps for a temporary table
2390
 
 
2391
 
  IMPLEMENTATION
2392
 
    For temporary tables, we need one bitmap with all columns set and
2393
 
    a tmp_set bitmap to be used by things like filesort.
2394
 
*/
2395
 
 
2396
 
void Table::setup_tmp_table_column_bitmaps(unsigned char *bitmaps)
2397
 
{
2398
 
  uint32_t field_count= s->fields;
2399
 
 
2400
 
  bitmap_init(&this->def_read_set, (my_bitmap_map*) bitmaps, field_count, false);
2401
 
  bitmap_init(&this->tmp_set, (my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)), field_count, false);
2402
 
 
2403
 
  /* write_set and all_set are copies of read_set */
2404
 
  def_write_set= def_read_set;
2405
 
  s->all_set= def_read_set;
2406
 
  bitmap_set_all(&this->s->all_set);
2407
 
  default_column_bitmaps();
2408
 
}
2409
 
 
2410
 
 
2411
 
 
2412
 
void Table::updateCreateInfo(HA_CREATE_INFO *create_info)
2413
 
{
2414
 
  create_info->max_rows= s->max_rows;
2415
 
  create_info->min_rows= s->min_rows;
2416
 
  create_info->table_options= s->db_create_options;
2417
 
  create_info->avg_row_length= s->avg_row_length;
2418
 
  create_info->block_size= s->block_size;
2419
 
  create_info->row_type= s->row_type;
2420
 
  create_info->default_table_charset= s->table_charset;
 
2134
 
 
2135
void update_create_info_from_table(HA_CREATE_INFO *create_info, TABLE *table)
 
2136
{
 
2137
  TABLE_SHARE *share= table->s;
 
2138
 
 
2139
  create_info->max_rows= share->max_rows;
 
2140
  create_info->min_rows= share->min_rows;
 
2141
  create_info->table_options= share->db_create_options;
 
2142
  create_info->avg_row_length= share->avg_row_length;
 
2143
  create_info->row_type= share->row_type;
 
2144
  create_info->default_table_charset= share->table_charset;
2421
2145
  create_info->table_charset= 0;
2422
 
  create_info->comment= s->comment;
 
2146
  create_info->comment= share->comment;
2423
2147
 
2424
2148
  return;
2425
2149
}
2428
2152
rename_file_ext(const char * from,const char * to,const char * ext)
2429
2153
{
2430
2154
  char from_b[FN_REFLEN],to_b[FN_REFLEN];
2431
 
  strxmov(from_b,from,ext,NULL);
2432
 
  strxmov(to_b,to,ext,NULL);
 
2155
  VOID(strxmov(from_b,from,ext,NullS));
 
2156
  VOID(strxmov(to_b,to,ext,NullS));
2433
2157
  return (my_rename(from_b,to_b,MYF(MY_WME)));
2434
2158
}
2435
2159
 
2452
2176
{
2453
2177
  char buff[MAX_FIELD_WIDTH], *to;
2454
2178
  String str(buff,sizeof(buff),&my_charset_bin);
2455
 
  uint32_t length;
 
2179
  uint length;
2456
2180
 
2457
2181
  field->val_str(&str);
2458
2182
  if (!(length= str.length()))
2476
2200
    field       Field for retrieving of string
2477
2201
 
2478
2202
  RETURN VALUES
2479
 
    NULL  string is empty
 
2203
    NullS  string is empty
2480
2204
    #      pointer to NULL-terminated string value of field
2481
2205
*/
2482
2206
 
2484
2208
{
2485
2209
  char buff[MAX_FIELD_WIDTH], *to;
2486
2210
  String str(buff,sizeof(buff),&my_charset_bin);
2487
 
  uint32_t length;
 
2211
  uint length;
2488
2212
 
2489
2213
  field->val_str(&str);
2490
2214
  length= str.length();
2491
2215
  if (!length || !(to= (char*) alloc_root(mem,length+1)))
2492
 
    return NULL;
 
2216
    return NullS;
2493
2217
  memcpy(to,str.ptr(),(uint) length);
2494
2218
  to[length]=0;
2495
2219
  return to;
2500
2224
    given a buffer with a key value, and a map of keyparts
2501
2225
    that are present in this value, returns the length of the value
2502
2226
*/
2503
 
uint32_t calculate_key_len(Table *table, uint32_t key,
2504
 
                       const unsigned char *buf __attribute__((unused)),
 
2227
uint calculate_key_len(TABLE *table, uint key,
 
2228
                       const uchar *buf __attribute__((__unused__)),
2505
2229
                       key_part_map keypart_map)
2506
2230
{
2507
2231
  /* works only with key prefixes */
2510
2234
  KEY *key_info= table->s->key_info+key;
2511
2235
  KEY_PART_INFO *key_part= key_info->key_part;
2512
2236
  KEY_PART_INFO *end_key_part= key_part + key_info->key_parts;
2513
 
  uint32_t length= 0;
 
2237
  uint length= 0;
2514
2238
 
2515
2239
  while (key_part < end_key_part && keypart_map)
2516
2240
  {
2539
2263
bool check_db_name(LEX_STRING *org_name)
2540
2264
{
2541
2265
  char *name= org_name->str;
2542
 
  uint32_t name_length= org_name->length;
 
2266
  uint name_length= org_name->length;
2543
2267
 
2544
2268
  if (!name_length || name_length > NAME_LEN || name[name_length - 1] == ' ')
2545
2269
    return 1;
2558
2282
*/
2559
2283
 
2560
2284
 
2561
 
bool check_table_name(const char *name, uint32_t length)
 
2285
bool check_table_name(const char *name, uint length)
2562
2286
{
2563
2287
  if (!length || length > NAME_LEN || name[length - 1] == ' ')
2564
2288
    return 1;
2576
2300
*/
2577
2301
bool check_column_name(const char *name)
2578
2302
{
2579
 
  uint32_t name_length= 0;  // name length in symbols
 
2303
  uint name_length= 0;  // name length in symbols
2580
2304
  bool last_char_is_space= true;
2581
2305
  
2582
2306
  while (*name)
2632
2356
*/
2633
2357
 
2634
2358
bool
2635
 
Table::table_check_intact(const uint32_t table_f_count,
2636
 
                          const TABLE_FIELD_W_TYPE *table_def)
 
2359
table_check_intact(TABLE *table, const uint table_f_count,
 
2360
                   const TABLE_FIELD_W_TYPE *table_def)
2637
2361
{
2638
 
  uint32_t i;
 
2362
  uint i;
2639
2363
  bool error= false;
2640
2364
  bool fields_diff_count;
2641
2365
 
2642
 
  fields_diff_count= (s->fields != table_f_count);
 
2366
  fields_diff_count= (table->s->fields != table_f_count);
2643
2367
  if (fields_diff_count)
2644
2368
  {
2645
2369
 
2646
2370
    /* previous MySQL version */
2647
 
    if (DRIZZLE_VERSION_ID > s->mysql_version)
 
2371
    if (MYSQL_VERSION_ID > table->s->mysql_version)
2648
2372
    {
2649
2373
      sql_print_error(ER(ER_COL_COUNT_DOESNT_MATCH_PLEASE_UPDATE),
2650
 
                      alias, table_f_count, s->fields,
2651
 
                      s->mysql_version, DRIZZLE_VERSION_ID);
 
2374
                      table->alias, table_f_count, table->s->fields,
 
2375
                      table->s->mysql_version, MYSQL_VERSION_ID);
2652
2376
      return(true);
2653
2377
    }
2654
 
    else if (DRIZZLE_VERSION_ID == s->mysql_version)
 
2378
    else if (MYSQL_VERSION_ID == table->s->mysql_version)
2655
2379
    {
2656
 
      sql_print_error(ER(ER_COL_COUNT_DOESNT_MATCH_CORRUPTED), alias,
2657
 
                      table_f_count, s->fields);
 
2380
      sql_print_error(ER(ER_COL_COUNT_DOESNT_MATCH_CORRUPTED), table->alias,
 
2381
                      table_f_count, table->s->fields);
2658
2382
      return(true);
2659
2383
    }
2660
2384
    /*
2670
2394
  {
2671
2395
    String sql_type(buffer, sizeof(buffer), system_charset_info);
2672
2396
    sql_type.length(0);
2673
 
    if (i < s->fields)
 
2397
    if (i < table->s->fields)
2674
2398
    {
2675
 
      Field *field= this->field[i];
 
2399
      Field *field= table->field[i];
2676
2400
 
2677
2401
      if (strncmp(field->field_name, table_def->name.str,
2678
2402
                  table_def->name.length))
2682
2406
          Still this can be a sign of a tampered table, output an error
2683
2407
          to the error log.
2684
2408
        */
2685
 
        sql_print_error(_("Incorrect definition of table %s.%s: "
2686
 
                        "expected column '%s' at position %d, found '%s'."),
2687
 
                        s->db.str, alias, table_def->name.str, i,
 
2409
        sql_print_error("Incorrect definition of table %s.%s: "
 
2410
                        "expected column '%s' at position %d, found '%s'.",
 
2411
                        table->s->db.str, table->alias, table_def->name.str, i,
2688
2412
                        field->field_name);
2689
2413
      }
2690
2414
      field->sql_type(sql_type);
2708
2432
      if (strncmp(sql_type.c_ptr_safe(), table_def->type.str,
2709
2433
                  table_def->type.length - 1))
2710
2434
      {
2711
 
        sql_print_error(_("Incorrect definition of table %s.%s: "
 
2435
        sql_print_error("Incorrect definition of table %s.%s: "
2712
2436
                        "expected column '%s' at position %d to have type "
2713
 
                        "%s, found type %s."), s->db.str, alias,
 
2437
                        "%s, found type %s.", table->s->db.str, table->alias,
2714
2438
                        table_def->name.str, i, table_def->type.str,
2715
2439
                        sql_type.c_ptr_safe());
2716
2440
        error= true;
2717
2441
      }
2718
2442
      else if (table_def->cset.str && !field->has_charset())
2719
2443
      {
2720
 
        sql_print_error(_("Incorrect definition of table %s.%s: "
 
2444
        sql_print_error("Incorrect definition of table %s.%s: "
2721
2445
                        "expected the type of column '%s' at position %d "
2722
2446
                        "to have character set '%s' but the type has no "
2723
 
                        "character set."), s->db.str, alias,
 
2447
                        "character set.", table->s->db.str, table->alias,
2724
2448
                        table_def->name.str, i, table_def->cset.str);
2725
2449
        error= true;
2726
2450
      }
2727
2451
      else if (table_def->cset.str &&
2728
2452
               strcmp(field->charset()->csname, table_def->cset.str))
2729
2453
      {
2730
 
        sql_print_error(_("Incorrect definition of table %s.%s: "
 
2454
        sql_print_error("Incorrect definition of table %s.%s: "
2731
2455
                        "expected the type of column '%s' at position %d "
2732
2456
                        "to have character set '%s' but found "
2733
 
                        "character set '%s'."), s->db.str, alias,
 
2457
                        "character set '%s'.", table->s->db.str, table->alias,
2734
2458
                        table_def->name.str, i, table_def->cset.str,
2735
2459
                        field->charset()->csname);
2736
2460
        error= true;
2738
2462
    }
2739
2463
    else
2740
2464
    {
2741
 
      sql_print_error(_("Incorrect definition of table %s.%s: "
 
2465
      sql_print_error("Incorrect definition of table %s.%s: "
2742
2466
                      "expected column '%s' at position %d to have type %s "
2743
 
                      " but the column is not found."),
2744
 
                      s->db.str, alias,
 
2467
                      " but the column is not found.",
 
2468
                      table->s->db.str, table->alias,
2745
2469
                      table_def->name.str, i, table_def->type.str);
2746
2470
      error= true;
2747
2471
    }
2754
2478
  Create Item_field for each column in the table.
2755
2479
 
2756
2480
  SYNPOSIS
2757
 
    Table::fill_item_list()
 
2481
    st_table::fill_item_list()
2758
2482
      item_list          a pointer to an empty list used to store items
2759
2483
 
2760
2484
  DESCRIPTION
2767
2491
    1                    out of memory
2768
2492
*/
2769
2493
 
2770
 
bool Table::fill_item_list(List<Item> *item_list) const
 
2494
bool st_table::fill_item_list(List<Item> *item_list) const
2771
2495
{
2772
2496
  /*
2773
2497
    All Item_field's created using a direct pointer to a field
2787
2511
  Fields of this table.
2788
2512
 
2789
2513
  SYNPOSIS
2790
 
    Table::fill_item_list()
 
2514
    st_table::fill_item_list()
2791
2515
      item_list          a non-empty list with Item_fields
2792
2516
 
2793
2517
  DESCRIPTION
2797
2521
    is the same as the number of columns in the table.
2798
2522
*/
2799
2523
 
2800
 
void Table::reset_item_list(List<Item> *item_list) const
 
2524
void st_table::reset_item_list(List<Item> *item_list) const
2801
2525
{
2802
2526
  List_iterator_fast<Item> it(*item_list);
2803
2527
  for (Field **ptr= field; *ptr; ptr++)
2810
2534
 
2811
2535
 
2812
2536
/*
2813
 
  Find underlying base tables (TableList) which represent given
2814
 
  table_to_find (Table)
2815
 
 
2816
 
  SYNOPSIS
2817
 
    TableList::find_underlying_table()
 
2537
  Merge ON expressions for a view
 
2538
 
 
2539
  SYNOPSIS
 
2540
    merge_on_conds()
 
2541
    thd             thread handle
 
2542
    table           table for the VIEW
 
2543
    is_cascaded     TRUE <=> merge ON expressions from underlying views
 
2544
 
 
2545
  DESCRIPTION
 
2546
    This function returns the result of ANDing the ON expressions
 
2547
    of the given view and all underlying views. The ON expressions
 
2548
    of the underlying views are added only if is_cascaded is TRUE.
 
2549
 
 
2550
  RETURN
 
2551
    Pointer to the built expression if there is any.
 
2552
    Otherwise and in the case of a failure NULL is returned.
 
2553
*/
 
2554
 
 
2555
static Item *
 
2556
merge_on_conds(THD *thd, TABLE_LIST *table, bool is_cascaded)
 
2557
{
 
2558
 
 
2559
  Item *cond= NULL;
 
2560
  if (table->on_expr)
 
2561
    cond= table->on_expr->copy_andor_structure(thd);
 
2562
  if (!table->nested_join)
 
2563
    return(cond);
 
2564
  List_iterator<TABLE_LIST> li(table->nested_join->join_list);
 
2565
  while (TABLE_LIST *tbl= li++)
 
2566
  {
 
2567
    cond= and_conds(cond, merge_on_conds(thd, tbl, is_cascaded));
 
2568
  }
 
2569
  return(cond);
 
2570
}
 
2571
 
 
2572
 
 
2573
/*
 
2574
  Find underlying base tables (TABLE_LIST) which represent given
 
2575
  table_to_find (TABLE)
 
2576
 
 
2577
  SYNOPSIS
 
2578
    TABLE_LIST::find_underlying_table()
2818
2579
    table_to_find table to find
2819
2580
 
2820
2581
  RETURN
2822
2583
    found table reference
2823
2584
*/
2824
2585
 
2825
 
TableList *TableList::find_underlying_table(Table *table_to_find)
 
2586
TABLE_LIST *TABLE_LIST::find_underlying_table(TABLE *table_to_find)
2826
2587
{
2827
2588
  /* is this real table and table which we are looking for? */
2828
2589
  if (table == table_to_find && merge_underlying_list == 0)
2829
2590
    return this;
2830
2591
 
2831
 
  for (TableList *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
 
2592
  for (TABLE_LIST *tbl= merge_underlying_list; tbl; tbl= tbl->next_local)
2832
2593
  {
2833
 
    TableList *result;
 
2594
    TABLE_LIST *result;
2834
2595
    if ((result= tbl->find_underlying_table(table_to_find)))
2835
2596
      return result;
2836
2597
  }
2841
2602
  cleunup items belonged to view fields translation table
2842
2603
 
2843
2604
  SYNOPSIS
2844
 
    TableList::cleanup_items()
 
2605
    TABLE_LIST::cleanup_items()
2845
2606
*/
2846
2607
 
2847
 
void TableList::cleanup_items()
 
2608
void TABLE_LIST::cleanup_items()
2848
2609
{
2849
2610
  if (!field_translation)
2850
2611
    return;
2868
2629
    TRUE  - out of memory
2869
2630
*/
2870
2631
 
2871
 
bool TableList::set_insert_values(MEM_ROOT *mem_root)
 
2632
bool TABLE_LIST::set_insert_values(MEM_ROOT *mem_root)
2872
2633
{
2873
2634
  if (table)
2874
2635
  {
2875
2636
    if (!table->insert_values &&
2876
 
        !(table->insert_values= (unsigned char *)alloc_root(mem_root,
 
2637
        !(table->insert_values= (uchar *)alloc_root(mem_root,
2877
2638
                                                   table->s->rec_buff_length)))
2878
2639
      return true;
2879
2640
  }
2886
2647
  Test if this is a leaf with respect to name resolution.
2887
2648
 
2888
2649
  SYNOPSIS
2889
 
    TableList::is_leaf_for_name_resolution()
 
2650
    TABLE_LIST::is_leaf_for_name_resolution()
2890
2651
 
2891
2652
  DESCRIPTION
2892
2653
    A table reference is a leaf with respect to name resolution if
2898
2659
  RETURN
2899
2660
    TRUE if a leaf, false otherwise.
2900
2661
*/
2901
 
bool TableList::is_leaf_for_name_resolution()
 
2662
bool TABLE_LIST::is_leaf_for_name_resolution()
2902
2663
{
2903
2664
  return (is_natural_join || is_join_columns_complete || !nested_join);
2904
2665
}
2909
2670
  respect to name resolution.
2910
2671
 
2911
2672
  SYNOPSIS
2912
 
    TableList::first_leaf_for_name_resolution()
 
2673
    TABLE_LIST::first_leaf_for_name_resolution()
2913
2674
 
2914
2675
  DESCRIPTION
2915
2676
    Given that 'this' is a nested table reference, recursively walk
2927
2688
    else return 'this'
2928
2689
*/
2929
2690
 
2930
 
TableList *TableList::first_leaf_for_name_resolution()
 
2691
TABLE_LIST *TABLE_LIST::first_leaf_for_name_resolution()
2931
2692
{
2932
 
  TableList *cur_table_ref= NULL;
2933
 
  nested_join_st *cur_nested_join;
 
2693
  TABLE_LIST *cur_table_ref;
 
2694
  NESTED_JOIN *cur_nested_join;
2934
2695
 
2935
2696
  if (is_leaf_for_name_resolution())
2936
2697
    return this;
2940
2701
       cur_nested_join;
2941
2702
       cur_nested_join= cur_table_ref->nested_join)
2942
2703
  {
2943
 
    List_iterator_fast<TableList> it(cur_nested_join->join_list);
 
2704
    List_iterator_fast<TABLE_LIST> it(cur_nested_join->join_list);
2944
2705
    cur_table_ref= it++;
2945
2706
    /*
2946
2707
      If the current nested join is a RIGHT JOIN, the operands in
2950
2711
    */
2951
2712
    if (!(cur_table_ref->outer_join & JOIN_TYPE_RIGHT))
2952
2713
    {
2953
 
      TableList *next;
 
2714
      TABLE_LIST *next;
2954
2715
      while ((next= it++))
2955
2716
        cur_table_ref= next;
2956
2717
    }
2966
2727
  respect to name resolution.
2967
2728
 
2968
2729
  SYNOPSIS
2969
 
    TableList::last_leaf_for_name_resolution()
 
2730
    TABLE_LIST::last_leaf_for_name_resolution()
2970
2731
 
2971
2732
  DESCRIPTION
2972
2733
    Given that 'this' is a nested table reference, recursively walk
2984
2745
    - else - 'this'
2985
2746
*/
2986
2747
 
2987
 
TableList *TableList::last_leaf_for_name_resolution()
 
2748
TABLE_LIST *TABLE_LIST::last_leaf_for_name_resolution()
2988
2749
{
2989
 
  TableList *cur_table_ref= this;
2990
 
  nested_join_st *cur_nested_join;
 
2750
  TABLE_LIST *cur_table_ref= this;
 
2751
  NESTED_JOIN *cur_nested_join;
2991
2752
 
2992
2753
  if (is_leaf_for_name_resolution())
2993
2754
    return this;
3005
2766
    */
3006
2767
    if ((cur_table_ref->outer_join & JOIN_TYPE_RIGHT))
3007
2768
    {
3008
 
      List_iterator_fast<TableList> it(cur_nested_join->join_list);
3009
 
      TableList *next;
 
2769
      List_iterator_fast<TABLE_LIST> it(cur_nested_join->join_list);
 
2770
      TABLE_LIST *next;
3010
2771
      cur_table_ref= it++;
3011
2772
      while ((next= it++))
3012
2773
        cur_table_ref= next;
3018
2779
}
3019
2780
 
3020
2781
 
 
2782
Natural_join_column::Natural_join_column(Field_translator *field_param,
 
2783
                                         TABLE_LIST *tab)
 
2784
{
 
2785
  assert(tab->field_translation);
 
2786
  view_field= field_param;
 
2787
  table_field= NULL;
 
2788
  table_ref= tab;
 
2789
  is_common= false;
 
2790
}
 
2791
 
 
2792
 
 
2793
Natural_join_column::Natural_join_column(Field *field_param,
 
2794
                                         TABLE_LIST *tab)
 
2795
{
 
2796
  assert(tab->table == field_param->table);
 
2797
  table_field= field_param;
 
2798
  view_field= NULL;
 
2799
  table_ref= tab;
 
2800
  is_common= false;
 
2801
}
 
2802
 
 
2803
 
 
2804
const char *Natural_join_column::name()
 
2805
{
 
2806
  if (view_field)
 
2807
  {
 
2808
    assert(table_field == NULL);
 
2809
    return view_field->name;
 
2810
  }
 
2811
 
 
2812
  return table_field->field_name;
 
2813
}
 
2814
 
 
2815
 
 
2816
Item *Natural_join_column::create_item(THD *thd)
 
2817
{
 
2818
  if (view_field)
 
2819
  {
 
2820
    assert(table_field == NULL);
 
2821
    return create_view_field(thd, table_ref, &view_field->item,
 
2822
                             view_field->name);
 
2823
  }
 
2824
  return new Item_field(thd, &thd->lex->current_select->context, table_field);
 
2825
}
 
2826
 
 
2827
 
 
2828
Field *Natural_join_column::field()
 
2829
{
 
2830
  if (view_field)
 
2831
  {
 
2832
    assert(table_field == NULL);
 
2833
    return NULL;
 
2834
  }
 
2835
  return table_field;
 
2836
}
 
2837
 
 
2838
 
 
2839
const char *Natural_join_column::table_name()
 
2840
{
 
2841
  assert(table_ref);
 
2842
  return table_ref->alias;
 
2843
}
 
2844
 
 
2845
 
 
2846
const char *Natural_join_column::db_name()
 
2847
{
 
2848
  /*
 
2849
    Test that TABLE_LIST::db is the same as st_table_share::db to
 
2850
    ensure consistency. An exception are I_S schema tables, which
 
2851
    are inconsistent in this respect.
 
2852
  */
 
2853
  assert(!strcmp(table_ref->db,
 
2854
                      table_ref->table->s->db.str) ||
 
2855
              (table_ref->schema_table &&
 
2856
               table_ref->table->s->db.str[0] == 0));
 
2857
  return table_ref->db;
 
2858
}
 
2859
 
 
2860
 
 
2861
void Field_iterator_view::set(TABLE_LIST *table)
 
2862
{
 
2863
  assert(table->field_translation);
 
2864
  view= table;
 
2865
  ptr= table->field_translation;
 
2866
  array_end= table->field_translation_end;
 
2867
}
 
2868
 
 
2869
 
 
2870
const char *Field_iterator_table::name()
 
2871
{
 
2872
  return (*ptr)->field_name;
 
2873
}
 
2874
 
 
2875
 
 
2876
Item *Field_iterator_table::create_item(THD *thd)
 
2877
{
 
2878
  SELECT_LEX *select= thd->lex->current_select;
 
2879
 
 
2880
  Item_field *item= new Item_field(thd, &select->context, *ptr);
 
2881
  if (item && thd->variables.sql_mode & MODE_ONLY_FULL_GROUP_BY &&
 
2882
      !thd->lex->in_sum_func && select->cur_pos_in_select_list != UNDEF_POS)
 
2883
  {
 
2884
    select->non_agg_fields.push_back(item);
 
2885
    item->marker= select->cur_pos_in_select_list;
 
2886
  }
 
2887
  return item;
 
2888
}
 
2889
 
 
2890
 
 
2891
const char *Field_iterator_view::name()
 
2892
{
 
2893
  return ptr->name;
 
2894
}
 
2895
 
 
2896
 
 
2897
Item *Field_iterator_view::create_item(THD *thd)
 
2898
{
 
2899
  return create_view_field(thd, view, &ptr->item, ptr->name);
 
2900
}
 
2901
 
 
2902
Item *create_view_field(THD *thd __attribute__((__unused__)),
 
2903
                        TABLE_LIST *view, Item **field_ref,
 
2904
                        const char *name __attribute__((__unused__)))
 
2905
{
 
2906
  if (view->schema_table_reformed)
 
2907
  {
 
2908
    Item *field= *field_ref;
 
2909
 
 
2910
    /*
 
2911
      Translation table items are always Item_fields and already fixed
 
2912
      ('mysql_schema_table' function). So we can return directly the
 
2913
      field. This case happens only for 'show & where' commands.
 
2914
    */
 
2915
    assert(field && field->fixed);
 
2916
    return(field);
 
2917
  }
 
2918
 
 
2919
  return(NULL);
 
2920
}
 
2921
 
 
2922
 
 
2923
void Field_iterator_natural_join::set(TABLE_LIST *table_ref)
 
2924
{
 
2925
  assert(table_ref->join_columns);
 
2926
  column_ref_it.init(*(table_ref->join_columns));
 
2927
  cur_column_ref= column_ref_it++;
 
2928
}
 
2929
 
 
2930
 
 
2931
void Field_iterator_natural_join::next()
 
2932
{
 
2933
  cur_column_ref= column_ref_it++;
 
2934
  assert(!cur_column_ref || ! cur_column_ref->table_field ||
 
2935
              cur_column_ref->table_ref->table ==
 
2936
              cur_column_ref->table_field->table);
 
2937
}
 
2938
 
 
2939
 
 
2940
void Field_iterator_table_ref::set_field_iterator()
 
2941
{
 
2942
  /*
 
2943
    If the table reference we are iterating over is a natural join, or it is
 
2944
    an operand of a natural join, and TABLE_LIST::join_columns contains all
 
2945
    the columns of the join operand, then we pick the columns from
 
2946
    TABLE_LIST::join_columns, instead of the  orginial container of the
 
2947
    columns of the join operator.
 
2948
  */
 
2949
  if (table_ref->is_join_columns_complete)
 
2950
  {
 
2951
    /* Necesary, but insufficient conditions. */
 
2952
    assert(table_ref->is_natural_join ||
 
2953
                table_ref->nested_join ||
 
2954
                ((table_ref->join_columns && /* This is a merge view. */ (table_ref->field_translation && table_ref->join_columns->elements == (ulong)(table_ref->field_translation_end - table_ref->field_translation))) ||
 
2955
                 /* This is stored table or a tmptable view. */
 
2956
                 (!table_ref->field_translation && table_ref->join_columns->elements == table_ref->table->s->fields)));
 
2957
    field_it= &natural_join_it;
 
2958
  }
 
2959
  /* This is a base table or stored view. */
 
2960
  else
 
2961
  {
 
2962
    assert(table_ref->table);
 
2963
    field_it= &table_field_it;
 
2964
  }
 
2965
  field_it->set(table_ref);
 
2966
  return;
 
2967
}
 
2968
 
 
2969
 
 
2970
void Field_iterator_table_ref::set(TABLE_LIST *table)
 
2971
{
 
2972
  assert(table);
 
2973
  first_leaf= table->first_leaf_for_name_resolution();
 
2974
  last_leaf=  table->last_leaf_for_name_resolution();
 
2975
  assert(first_leaf && last_leaf);
 
2976
  table_ref= first_leaf;
 
2977
  set_field_iterator();
 
2978
}
 
2979
 
 
2980
 
 
2981
void Field_iterator_table_ref::next()
 
2982
{
 
2983
  /* Move to the next field in the current table reference. */
 
2984
  field_it->next();
 
2985
  /*
 
2986
    If all fields of the current table reference are exhausted, move to
 
2987
    the next leaf table reference.
 
2988
  */
 
2989
  if (field_it->end_of_fields() && table_ref != last_leaf)
 
2990
  {
 
2991
    table_ref= table_ref->next_name_resolution_table;
 
2992
    assert(table_ref);
 
2993
    set_field_iterator();
 
2994
  }
 
2995
}
 
2996
 
 
2997
 
 
2998
const char *Field_iterator_table_ref::table_name()
 
2999
{
 
3000
  if (table_ref->is_natural_join)
 
3001
    return natural_join_it.column_ref()->table_name();
 
3002
 
 
3003
  assert(!strcmp(table_ref->table_name,
 
3004
                      table_ref->table->s->table_name.str));
 
3005
  return table_ref->table_name;
 
3006
}
 
3007
 
 
3008
 
 
3009
const char *Field_iterator_table_ref::db_name()
 
3010
{
 
3011
  if (table_ref->is_natural_join)
 
3012
    return natural_join_it.column_ref()->db_name();
 
3013
 
 
3014
  /*
 
3015
    Test that TABLE_LIST::db is the same as st_table_share::db to
 
3016
    ensure consistency. An exception are I_S schema tables, which
 
3017
    are inconsistent in this respect.
 
3018
  */
 
3019
  assert(!strcmp(table_ref->db, table_ref->table->s->db.str) ||
 
3020
              (table_ref->schema_table &&
 
3021
               table_ref->table->s->db.str[0] == 0));
 
3022
 
 
3023
  return table_ref->db;
 
3024
}
 
3025
 
 
3026
 
 
3027
/*
 
3028
  Create new or return existing column reference to a column of a
 
3029
  natural/using join.
 
3030
 
 
3031
  SYNOPSIS
 
3032
    Field_iterator_table_ref::get_or_create_column_ref()
 
3033
    parent_table_ref  the parent table reference over which the
 
3034
                      iterator is iterating
 
3035
 
 
3036
  DESCRIPTION
 
3037
    Create a new natural join column for the current field of the
 
3038
    iterator if no such column was created, or return an already
 
3039
    created natural join column. The former happens for base tables or
 
3040
    views, and the latter for natural/using joins. If a new field is
 
3041
    created, then the field is added to 'parent_table_ref' if it is
 
3042
    given, or to the original table referene of the field if
 
3043
    parent_table_ref == NULL.
 
3044
 
 
3045
  NOTES
 
3046
    This method is designed so that when a Field_iterator_table_ref
 
3047
    walks through the fields of a table reference, all its fields
 
3048
    are created and stored as follows:
 
3049
    - If the table reference being iterated is a stored table, view or
 
3050
      natural/using join, store all natural join columns in a list
 
3051
      attached to that table reference.
 
3052
    - If the table reference being iterated is a nested join that is
 
3053
      not natural/using join, then do not materialize its result
 
3054
      fields. This is OK because for such table references
 
3055
      Field_iterator_table_ref iterates over the fields of the nested
 
3056
      table references (recursively). In this way we avoid the storage
 
3057
      of unnecessay copies of result columns of nested joins.
 
3058
 
 
3059
  RETURN
 
3060
    #     Pointer to a column of a natural join (or its operand)
 
3061
    NULL  No memory to allocate the column
 
3062
*/
 
3063
 
 
3064
Natural_join_column *
 
3065
Field_iterator_table_ref::get_or_create_column_ref(TABLE_LIST *parent_table_ref)
 
3066
{
 
3067
  Natural_join_column *nj_col;
 
3068
  bool is_created= true;
 
3069
  uint field_count=0;
 
3070
  TABLE_LIST *add_table_ref= parent_table_ref ?
 
3071
                             parent_table_ref : table_ref;
 
3072
 
 
3073
  if (field_it == &table_field_it)
 
3074
  {
 
3075
    /* The field belongs to a stored table. */
 
3076
    Field *tmp_field= table_field_it.field();
 
3077
    nj_col= new Natural_join_column(tmp_field, table_ref);
 
3078
    field_count= table_ref->table->s->fields;
 
3079
  }
 
3080
  else if (field_it == &view_field_it)
 
3081
  {
 
3082
    /* The field belongs to a merge view or information schema table. */
 
3083
    Field_translator *translated_field= view_field_it.field_translator();
 
3084
    nj_col= new Natural_join_column(translated_field, table_ref);
 
3085
    field_count= table_ref->field_translation_end -
 
3086
                 table_ref->field_translation;
 
3087
  }
 
3088
  else
 
3089
  {
 
3090
    /*
 
3091
      The field belongs to a NATURAL join, therefore the column reference was
 
3092
      already created via one of the two constructor calls above. In this case
 
3093
      we just return the already created column reference.
 
3094
    */
 
3095
    assert(table_ref->is_join_columns_complete);
 
3096
    is_created= false;
 
3097
    nj_col= natural_join_it.column_ref();
 
3098
    assert(nj_col);
 
3099
  }
 
3100
  assert(!nj_col->table_field ||
 
3101
              nj_col->table_ref->table == nj_col->table_field->table);
 
3102
 
 
3103
  /*
 
3104
    If the natural join column was just created add it to the list of
 
3105
    natural join columns of either 'parent_table_ref' or to the table
 
3106
    reference that directly contains the original field.
 
3107
  */
 
3108
  if (is_created)
 
3109
  {
 
3110
    /* Make sure not all columns were materialized. */
 
3111
    assert(!add_table_ref->is_join_columns_complete);
 
3112
    if (!add_table_ref->join_columns)
 
3113
    {
 
3114
      /* Create a list of natural join columns on demand. */
 
3115
      if (!(add_table_ref->join_columns= new List<Natural_join_column>))
 
3116
        return NULL;
 
3117
      add_table_ref->is_join_columns_complete= false;
 
3118
    }
 
3119
    add_table_ref->join_columns->push_back(nj_col);
 
3120
    /*
 
3121
      If new fields are added to their original table reference, mark if
 
3122
      all fields were added. We do it here as the caller has no easy way
 
3123
      of knowing when to do it.
 
3124
      If the fields are being added to parent_table_ref, then the caller
 
3125
      must take care to mark when all fields are created/added.
 
3126
    */
 
3127
    if (!parent_table_ref &&
 
3128
        add_table_ref->join_columns->elements == field_count)
 
3129
      add_table_ref->is_join_columns_complete= true;
 
3130
  }
 
3131
 
 
3132
  return nj_col;
 
3133
}
 
3134
 
 
3135
 
 
3136
/*
 
3137
  Return an existing reference to a column of a natural/using join.
 
3138
 
 
3139
  SYNOPSIS
 
3140
    Field_iterator_table_ref::get_natural_column_ref()
 
3141
 
 
3142
  DESCRIPTION
 
3143
    The method should be called in contexts where it is expected that
 
3144
    all natural join columns are already created, and that the column
 
3145
    being retrieved is a Natural_join_column.
 
3146
 
 
3147
  RETURN
 
3148
    #     Pointer to a column of a natural join (or its operand)
 
3149
    NULL  No memory to allocate the column
 
3150
*/
 
3151
 
 
3152
Natural_join_column *
 
3153
Field_iterator_table_ref::get_natural_column_ref()
 
3154
{
 
3155
  Natural_join_column *nj_col;
 
3156
 
 
3157
  assert(field_it == &natural_join_it);
 
3158
  /*
 
3159
    The field belongs to a NATURAL join, therefore the column reference was
 
3160
    already created via one of the two constructor calls above. In this case
 
3161
    we just return the already created column reference.
 
3162
  */
 
3163
  nj_col= natural_join_it.column_ref();
 
3164
  assert(nj_col &&
 
3165
              (!nj_col->table_field ||
 
3166
               nj_col->table_ref->table == nj_col->table_field->table));
 
3167
  return nj_col;
 
3168
}
 
3169
 
3021
3170
/*****************************************************************************
3022
3171
  Functions to handle column usage bitmaps (read_set, write_set etc...)
3023
3172
*****************************************************************************/
3024
3173
 
3025
3174
/* Reset all columns bitmaps */
3026
3175
 
3027
 
void Table::clear_column_bitmaps()
 
3176
void st_table::clear_column_bitmaps()
3028
3177
{
3029
3178
  /*
3030
3179
    Reset column read/write usage. It's identical to:
3031
3180
    bitmap_clear_all(&table->def_read_set);
3032
3181
    bitmap_clear_all(&table->def_write_set);
3033
3182
  */
3034
 
  memset(def_read_set.bitmap, 0, s->column_bitmap_size*2);
 
3183
  bzero((char*) def_read_set.bitmap, s->column_bitmap_size*2);
3035
3184
  column_bitmaps_set(&def_read_set, &def_write_set);
3036
3185
}
3037
3186
 
3045
3194
  key fields.
3046
3195
*/
3047
3196
 
3048
 
void Table::prepare_for_position()
 
3197
void st_table::prepare_for_position()
3049
3198
{
3050
3199
 
3051
3200
  if ((file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
3065
3214
  NOTE:
3066
3215
    This changes the bitmap to use the tmp bitmap
3067
3216
    After this, you can't access any other columns in the table until
3068
 
    bitmaps are reset, for example with Table::clear_column_bitmaps()
3069
 
    or Table::restore_column_maps_after_mark_index()
 
3217
    bitmaps are reset, for example with st_table::clear_column_bitmaps()
 
3218
    or st_table::restore_column_maps_after_mark_index()
3070
3219
*/
3071
3220
 
3072
 
void Table::mark_columns_used_by_index(uint32_t index)
 
3221
void st_table::mark_columns_used_by_index(uint index)
3073
3222
{
3074
3223
  MY_BITMAP *bitmap= &tmp_set;
3075
3224
 
3092
3241
    when calling mark_columns_used_by_index
3093
3242
*/
3094
3243
 
3095
 
void Table::restore_column_maps_after_mark_index()
 
3244
void st_table::restore_column_maps_after_mark_index()
3096
3245
{
3097
3246
 
3098
3247
  key_read= 0;
3107
3256
  mark columns used by key, but don't reset other fields
3108
3257
*/
3109
3258
 
3110
 
void Table::mark_columns_used_by_index_no_reset(uint32_t index,
 
3259
void st_table::mark_columns_used_by_index_no_reset(uint index,
3111
3260
                                                   MY_BITMAP *bitmap)
3112
3261
{
3113
3262
  KEY_PART_INFO *key_part= key_info[index].key_part;
3114
3263
  KEY_PART_INFO *key_part_end= (key_part +
3115
3264
                                key_info[index].key_parts);
3116
3265
  for (;key_part != key_part_end; key_part++)
3117
 
  {
3118
3266
    bitmap_set_bit(bitmap, key_part->fieldnr-1);
3119
 
    if (key_part->field->vcol_info &&
3120
 
        key_part->field->vcol_info->expr_item)
3121
 
      key_part->field->vcol_info->
3122
 
               expr_item->walk(&Item::register_field_in_bitmap, 
3123
 
                               1, (unsigned char *) bitmap);
3124
 
  }
3125
3267
}
3126
3268
 
3127
3269
 
3133
3275
    always set and sometimes read.
3134
3276
*/
3135
3277
 
3136
 
void Table::mark_auto_increment_column()
 
3278
void st_table::mark_auto_increment_column()
3137
3279
{
3138
3280
  assert(found_next_number_field);
3139
3281
  /*
3166
3308
    retrieve the row again.
3167
3309
*/
3168
3310
 
3169
 
void Table::mark_columns_needed_for_delete()
 
3311
void st_table::mark_columns_needed_for_delete()
3170
3312
{
3171
3313
  if (file->ha_table_flags() & HA_REQUIRES_KEY_COLUMNS_FOR_DELETE)
3172
3314
  {
3216
3358
    retrieve the row again.
3217
3359
*/
3218
3360
 
3219
 
void Table::mark_columns_needed_for_update()
 
3361
void st_table::mark_columns_needed_for_update()
3220
3362
{
3221
3363
  if (file->ha_table_flags() & HA_REQUIRES_KEY_COLUMNS_FOR_DELETE)
3222
3364
  {
3247
3389
      file->column_bitmaps_signal();
3248
3390
    }
3249
3391
  }
3250
 
  /* Mark all virtual columns as writable */
3251
 
  mark_virtual_columns();
3252
3392
  return;
3253
3393
}
3254
3394
 
3260
3400
  as changed.
3261
3401
*/
3262
3402
 
3263
 
void Table::mark_columns_needed_for_insert()
 
3403
void st_table::mark_columns_needed_for_insert()
3264
3404
{
3265
3405
  if (found_next_number_field)
3266
3406
    mark_auto_increment_column();
3267
 
  /* Mark all virtual columns as writable */
3268
 
  mark_virtual_columns();
3269
 
}
3270
 
 
3271
 
/* 
3272
 
  @brief Update the write and read table bitmap to allow
3273
 
         using procedure save_in_field for all virtual columns
3274
 
         in the table.
3275
 
 
3276
 
  @return       void
3277
 
 
3278
 
  @detail
3279
 
    Each virtual field is set in the write column map.
3280
 
    All fields that the virtual columns are based on are set in the
3281
 
    read bitmap.
3282
 
*/
3283
 
 
3284
 
void Table::mark_virtual_columns(void)
3285
 
{
3286
 
  Field **vfield_ptr, *tmp_vfield;
3287
 
  bool bitmap_updated= false;
3288
 
 
3289
 
  for (vfield_ptr= vfield; *vfield_ptr; vfield_ptr++)
3290
 
  {
3291
 
    tmp_vfield= *vfield_ptr;
3292
 
    assert(tmp_vfield->vcol_info && tmp_vfield->vcol_info->expr_item);
3293
 
    tmp_vfield->vcol_info->expr_item->walk(&Item::register_field_in_read_map, 
3294
 
                                           1, (unsigned char *) 0);
3295
 
    bitmap_set_bit(read_set, tmp_vfield->field_index);
3296
 
    bitmap_set_bit(write_set, tmp_vfield->field_index);
3297
 
    bitmap_updated= true;
3298
 
  }
3299
 
  if (bitmap_updated)
3300
 
    file->column_bitmaps_signal();
3301
 
}
3302
 
 
 
3407
}
3303
3408
 
3304
3409
/*
3305
3410
  Cleanup this table for re-execution.
3306
3411
 
3307
3412
  SYNOPSIS
3308
 
    TableList::reinit_before_use()
 
3413
    TABLE_LIST::reinit_before_use()
3309
3414
*/
3310
3415
 
3311
 
void TableList::reinit_before_use(THD *thd)
 
3416
void TABLE_LIST::reinit_before_use(THD *thd)
3312
3417
{
3313
3418
  /*
3314
3419
    Reset old pointers to TABLEs: they are not valid since the tables
3318
3423
  /* Reset is_schema_table_processed value(needed for I_S tables */
3319
3424
  schema_table_state= NOT_PROCESSED;
3320
3425
 
3321
 
  TableList *embedded; /* The table at the current level of nesting. */
3322
 
  TableList *parent_embedding= this; /* The parent nested table reference. */
 
3426
  TABLE_LIST *embedded; /* The table at the current level of nesting. */
 
3427
  TABLE_LIST *parent_embedding= this; /* The parent nested table reference. */
3323
3428
  do
3324
3429
  {
3325
3430
    embedded= parent_embedding;
3335
3440
  Return subselect that contains the FROM list this table is taken from
3336
3441
 
3337
3442
  SYNOPSIS
3338
 
    TableList::containing_subselect()
 
3443
    TABLE_LIST::containing_subselect()
3339
3444
 
3340
3445
  RETURN
3341
3446
    Subselect item for the subquery that contains the FROM list
3344
3449
 
3345
3450
*/
3346
3451
 
3347
 
Item_subselect *TableList::containing_subselect()
 
3452
Item_subselect *TABLE_LIST::containing_subselect()
3348
3453
{    
3349
3454
  return (select_lex ? select_lex->master_unit()->item : 0);
3350
3455
}
3354
3459
 
3355
3460
  SYNOPSIS
3356
3461
    process_index_hints()
3357
 
      table         the Table to operate on.
 
3462
      table         the TABLE to operate on.
3358
3463
 
3359
3464
  DESCRIPTION
3360
3465
    The parser collects the index hints for each table in a "tagged list" 
3361
 
    (TableList::index_hints). Using the information in this tagged list
3362
 
    this function sets the members Table::keys_in_use_for_query, 
3363
 
    Table::keys_in_use_for_group_by, Table::keys_in_use_for_order_by,
3364
 
    Table::force_index and Table::covering_keys.
 
3466
    (TABLE_LIST::index_hints). Using the information in this tagged list
 
3467
    this function sets the members st_table::keys_in_use_for_query, 
 
3468
    st_table::keys_in_use_for_group_by, st_table::keys_in_use_for_order_by,
 
3469
    st_table::force_index and st_table::covering_keys.
3365
3470
 
3366
3471
    Current implementation of the runtime does not allow mixing FORCE INDEX
3367
3472
    and USE INDEX, so this is checked here. Then the FORCE INDEX list 
3393
3498
    as if we had "USE INDEX i1, USE INDEX i1, IGNORE INDEX i1".
3394
3499
 
3395
3500
    As an optimization if there is a covering index, and we have 
3396
 
    IGNORE INDEX FOR GROUP/order_st, and this index is used for the JOIN part, 
3397
 
    then we have to ignore the IGNORE INDEX FROM GROUP/order_st.
 
3501
    IGNORE INDEX FOR GROUP/ORDER, and this index is used for the JOIN part, 
 
3502
    then we have to ignore the IGNORE INDEX FROM GROUP/ORDER.
3398
3503
 
3399
3504
  RETURN VALUE
3400
3505
    false                no errors found
3401
3506
    TRUE                 found and reported an error.
3402
3507
*/
3403
 
bool TableList::process_index_hints(Table *tbl)
 
3508
bool TABLE_LIST::process_index_hints(TABLE *tbl)
3404
3509
{
3405
3510
  /* initialize the result variables */
3406
3511
  tbl->keys_in_use_for_query= tbl->keys_in_use_for_group_by= 
3429
3534
    /* iterate over the hints list */
3430
3535
    while ((hint= iter++))
3431
3536
    {
3432
 
      uint32_t pos;
 
3537
      uint pos;
3433
3538
 
3434
3539
      /* process empty USE INDEX () */
3435
3540
      if (hint->type == INDEX_HINT_USE && !hint->key_name.str)
3519
3624
}
3520
3625
 
3521
3626
 
3522
 
size_t Table::max_row_length(const unsigned char *data)
 
3627
size_t max_row_length(TABLE *table, const uchar *data)
3523
3628
{
3524
 
  size_t length= getRecordLength() + 2 * sizeFields();
3525
 
  uint32_t *const beg= getBlobField();
3526
 
  uint32_t *const end= beg + sizeBlobFields();
 
3629
  TABLE_SHARE *table_s= table->s;
 
3630
  size_t length= table_s->reclength + 2 * table_s->fields;
 
3631
  uint *const beg= table_s->blob_field;
 
3632
  uint *const end= beg + table_s->blob_fields;
3527
3633
 
3528
 
  for (uint32_t *ptr= beg ; ptr != end ; ++ptr)
 
3634
  for (uint *ptr= beg ; ptr != end ; ++ptr)
3529
3635
  {
3530
 
    Field_blob* const blob= (Field_blob*) field[*ptr];
3531
 
    length+= blob->get_length((const unsigned char*)
3532
 
                              (data + blob->offset(record[0]))) +
 
3636
    Field_blob* const blob= (Field_blob*) table->field[*ptr];
 
3637
    length+= blob->get_length((const uchar*)
 
3638
                              (data + blob->offset(table->record[0]))) +
3533
3639
      HA_KEY_BLOB_LENGTH;
3534
3640
  }
3535
3641
  return length;
3543
3649
  path        path to file
3544
3650
 
3545
3651
  RETURN
3546
 
  false       error
3547
 
  true       table
 
3652
  FRMTYPE_ERROR       error
 
3653
  FRMTYPE_TABLE       table
3548
3654
*/
3549
3655
 
3550
 
bool mysql_frm_type(THD *thd __attribute__((unused)),
3551
 
                    char *path, enum legacy_db_type *dbt)
 
3656
frm_type_enum mysql_frm_type(THD *thd __attribute__((__unused__)),
 
3657
                             char *path, enum legacy_db_type *dbt)
3552
3658
{
3553
3659
  File file;
3554
 
  unsigned char header[10];     /* This should be optimized */
 
3660
  uchar header[10];     /* This should be optimized */
3555
3661
  int error;
3556
3662
 
3557
3663
  *dbt= DB_TYPE_UNKNOWN;
3558
3664
 
3559
 
  if ((file= open(path, O_RDONLY)) < 0)
3560
 
    return false;
3561
 
  error= my_read(file, (unsigned char*) header, sizeof(header), MYF(MY_NABP));
 
3665
  if ((file= my_open(path, O_RDONLY | O_SHARE, MYF(0))) < 0)
 
3666
    return(FRMTYPE_ERROR);
 
3667
  error= my_read(file, (uchar*) header, sizeof(header), MYF(MY_NABP));
3562
3668
  my_close(file, MYF(MY_WME));
3563
3669
 
3564
3670
  if (error)
3565
 
    return false;
 
3671
    return(FRMTYPE_ERROR);
3566
3672
 
3567
3673
  /*  
3568
3674
    This is just a check for DB_TYPE. We'll return default unknown type
3569
3675
    if the following test is true (arg #3). This should not have effect
3570
3676
    on return value from this function (default FRMTYPE_TABLE)
3571
3677
   */  
3572
 
  if (header[0] != (unsigned char) 254 || header[1] != 1 ||
 
3678
  if (header[0] != (uchar) 254 || header[1] != 1 ||
3573
3679
      (header[2] != FRM_VER && header[2] != FRM_VER+1 &&
3574
3680
       (header[2] < FRM_VER+3 || header[2] > FRM_VER+4)))
3575
 
    return true;
 
3681
    return(FRMTYPE_TABLE);
3576
3682
 
3577
3683
  *dbt= (enum legacy_db_type) (uint) *(header + 3);
3578
 
  return true;                   // Is probably a .frm table
3579
 
}
3580
 
 
3581
 
/****************************************************************************
3582
 
 Functions for creating temporary tables.
3583
 
****************************************************************************/
3584
 
 
3585
 
 
3586
 
/* Prototypes */
3587
 
void free_tmp_table(THD *thd, Table *entry);
3588
 
 
3589
 
/**
3590
 
  Create field for temporary table from given field.
3591
 
 
3592
 
  @param thd           Thread handler
3593
 
  @param org_field    field from which new field will be created
3594
 
  @param name         New field name
3595
 
  @param table         Temporary table
3596
 
  @param item          !=NULL if item->result_field should point to new field.
3597
 
                      This is relevant for how fill_record() is going to work:
3598
 
                      If item != NULL then fill_record() will update
3599
 
                      the record in the original table.
3600
 
                      If item == NULL then fill_record() will update
3601
 
                      the temporary table
3602
 
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
3603
 
                               field instead of blob.
3604
 
 
3605
 
  @retval
3606
 
    NULL                on error
3607
 
  @retval
3608
 
    new_created field
3609
 
*/
3610
 
 
3611
 
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
3612
 
                                   const char *name, Table *table,
3613
 
                                   Item_field *item, uint32_t convert_blob_length)
3614
 
{
3615
 
  Field *new_field;
3616
 
 
3617
 
  /* 
3618
 
    Make sure that the blob fits into a Field_varstring which has 
3619
 
    2-byte lenght. 
3620
 
  */
3621
 
  if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
3622
 
      (org_field->flags & BLOB_FLAG))
3623
 
    new_field= new Field_varstring(convert_blob_length,
3624
 
                                   org_field->maybe_null(),
3625
 
                                   org_field->field_name, table->s,
3626
 
                                   org_field->charset());
3627
 
  else
3628
 
    new_field= org_field->new_field(thd->mem_root, table,
3629
 
                                    table == org_field->table);
3630
 
  if (new_field)
3631
 
  {
3632
 
    new_field->init(table);
3633
 
    new_field->orig_table= org_field->orig_table;
3634
 
    if (item)
3635
 
      item->result_field= new_field;
3636
 
    else
3637
 
      new_field->field_name= name;
3638
 
    new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
3639
 
    if (org_field->maybe_null() || (item && item->maybe_null))
3640
 
      new_field->flags&= ~NOT_NULL_FLAG;        // Because of outer join
3641
 
    if (org_field->type() == DRIZZLE_TYPE_VARCHAR)
3642
 
      table->s->db_create_options|= HA_OPTION_PACK_RECORD;
3643
 
    else if (org_field->type() == DRIZZLE_TYPE_DOUBLE)
3644
 
      ((Field_double *) new_field)->not_fixed= true;
3645
 
  }
3646
 
  return new_field;
3647
 
}
3648
 
 
3649
 
/**
3650
 
  Create field for temporary table using type of given item.
3651
 
 
3652
 
  @param thd                   Thread handler
3653
 
  @param item                  Item to create a field for
3654
 
  @param table                 Temporary table
3655
 
  @param copy_func             If set and item is a function, store copy of
3656
 
                               item in this array
3657
 
  @param modify_item           1 if item->result_field should point to new
3658
 
                               item. This is relevent for how fill_record()
3659
 
                               is going to work:
3660
 
                               If modify_item is 1 then fill_record() will
3661
 
                               update the record in the original table.
3662
 
                               If modify_item is 0 then fill_record() will
3663
 
                               update the temporary table
3664
 
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
3665
 
                               field instead of blob.
3666
 
 
3667
 
  @retval
3668
 
    0  on error
3669
 
  @retval
3670
 
    new_created field
3671
 
*/
3672
 
 
3673
 
static Field *create_tmp_field_from_item(THD *thd __attribute__((unused)),
3674
 
                                         Item *item, Table *table,
3675
 
                                         Item ***copy_func, bool modify_item,
3676
 
                                         uint32_t convert_blob_length)
3677
 
{
3678
 
  bool maybe_null= item->maybe_null;
3679
 
  Field *new_field;
3680
 
 
3681
 
  switch (item->result_type()) {
3682
 
  case REAL_RESULT:
3683
 
    new_field= new Field_double(item->max_length, maybe_null,
3684
 
                                item->name, item->decimals, true);
3685
 
    break;
3686
 
  case INT_RESULT:
3687
 
    /* 
3688
 
      Select an integer type with the minimal fit precision.
3689
 
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
3690
 
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into 
3691
 
      Field_long : make them Field_int64_t.  
3692
 
    */
3693
 
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
3694
 
      new_field=new Field_int64_t(item->max_length, maybe_null,
3695
 
                                   item->name, item->unsigned_flag);
3696
 
    else
3697
 
      new_field=new Field_long(item->max_length, maybe_null,
3698
 
                               item->name, item->unsigned_flag);
3699
 
    break;
3700
 
  case STRING_RESULT:
3701
 
    assert(item->collation.collation);
3702
 
  
3703
 
    enum enum_field_types type;
3704
 
    /*
3705
 
      DATE/TIME fields have STRING_RESULT result type. 
3706
 
      To preserve type they needed to be handled separately.
3707
 
    */
3708
 
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
3709
 
        type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_NEWDATE ||
3710
 
        type == DRIZZLE_TYPE_TIMESTAMP)
3711
 
      new_field= item->tmp_table_field_from_field_type(table, 1);
3712
 
    /* 
3713
 
      Make sure that the blob fits into a Field_varstring which has 
3714
 
      2-byte lenght. 
3715
 
    */
3716
 
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
3717
 
             convert_blob_length <= Field_varstring::MAX_SIZE && 
3718
 
             convert_blob_length)
3719
 
      new_field= new Field_varstring(convert_blob_length, maybe_null,
3720
 
                                     item->name, table->s,
3721
 
                                     item->collation.collation);
3722
 
    else
3723
 
      new_field= item->make_string_field(table);
3724
 
    new_field->set_derivation(item->collation.derivation);
3725
 
    break;
3726
 
  case DECIMAL_RESULT:
3727
 
  {
3728
 
    uint8_t dec= item->decimals;
3729
 
    uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
3730
 
    uint32_t len= item->max_length;
3731
 
 
3732
 
    /*
3733
 
      Trying to put too many digits overall in a DECIMAL(prec,dec)
3734
 
      will always throw a warning. We must limit dec to
3735
 
      DECIMAL_MAX_SCALE however to prevent an assert() later.
3736
 
    */
3737
 
 
3738
 
    if (dec > 0)
3739
 
    {
3740
 
      signed int overflow;
3741
 
 
3742
 
      dec= cmin(dec, (uint8_t)DECIMAL_MAX_SCALE);
3743
 
 
3744
 
      /*
3745
 
        If the value still overflows the field with the corrected dec,
3746
 
        we'll throw out decimals rather than integers. This is still
3747
 
        bad and of course throws a truncation warning.
3748
 
        +1: for decimal point
3749
 
      */
3750
 
 
3751
 
      overflow= my_decimal_precision_to_length(intg + dec, dec,
3752
 
                                               item->unsigned_flag) - len;
3753
 
 
3754
 
      if (overflow > 0)
3755
 
        dec= cmax(0, dec - overflow);            // too long, discard fract
3756
 
      else
3757
 
        len -= item->decimals - dec;            // corrected value fits
3758
 
    }
3759
 
 
3760
 
    new_field= new Field_new_decimal(len, maybe_null, item->name,
3761
 
                                     dec, item->unsigned_flag);
3762
 
    break;
3763
 
  }
3764
 
  case ROW_RESULT:
3765
 
  default:
3766
 
    // This case should never be choosen
3767
 
    assert(0);
3768
 
    new_field= 0;
3769
 
    break;
3770
 
  }
3771
 
  if (new_field)
3772
 
    new_field->init(table);
3773
 
    
3774
 
  if (copy_func && item->is_result_field())
3775
 
    *((*copy_func)++) = item;                   // Save for copy_funcs
3776
 
  if (modify_item)
3777
 
    item->set_result_field(new_field);
3778
 
  if (item->type() == Item::NULL_ITEM)
3779
 
    new_field->is_created_from_null_item= true;
3780
 
  return new_field;
3781
 
}
3782
 
 
3783
 
 
3784
 
/**
3785
 
  Create field for information schema table.
3786
 
 
3787
 
  @param thd            Thread handler
3788
 
  @param table          Temporary table
3789
 
  @param item           Item to create a field for
3790
 
 
3791
 
  @retval
3792
 
    0                   on error
3793
 
  @retval
3794
 
    new_created field
3795
 
*/
3796
 
 
3797
 
Field *create_tmp_field_for_schema(THD *thd __attribute__((unused)),
3798
 
                                   Item *item, Table *table)
3799
 
{
3800
 
  if (item->field_type() == DRIZZLE_TYPE_VARCHAR)
3801
 
  {
3802
 
    Field *field;
3803
 
    if (item->max_length > MAX_FIELD_VARCHARLENGTH)
3804
 
      field= new Field_blob(item->max_length, item->maybe_null,
3805
 
                            item->name, item->collation.collation);
3806
 
    else
3807
 
      field= new Field_varstring(item->max_length, item->maybe_null,
3808
 
                                 item->name,
3809
 
                                 table->s, item->collation.collation);
3810
 
    if (field)
3811
 
      field->init(table);
3812
 
    return field;
3813
 
  }
3814
 
  return item->tmp_table_field_from_field_type(table, 0);
3815
 
}
3816
 
 
3817
 
 
3818
 
/**
3819
 
  Create field for temporary table.
3820
 
 
3821
 
  @param thd            Thread handler
3822
 
  @param table          Temporary table
3823
 
  @param item           Item to create a field for
3824
 
  @param type           Type of item (normally item->type)
3825
 
  @param copy_func      If set and item is a function, store copy of item
3826
 
                       in this array
3827
 
  @param from_field    if field will be created using other field as example,
3828
 
                       pointer example field will be written here
3829
 
  @param default_field  If field has a default value field, store it here
3830
 
  @param group          1 if we are going to do a relative group by on result
3831
 
  @param modify_item    1 if item->result_field should point to new item.
3832
 
                       This is relevent for how fill_record() is going to
3833
 
                       work:
3834
 
                       If modify_item is 1 then fill_record() will update
3835
 
                       the record in the original table.
3836
 
                       If modify_item is 0 then fill_record() will update
3837
 
                       the temporary table
3838
 
  @param convert_blob_length If >0 create a varstring(convert_blob_length)
3839
 
                             field instead of blob.
3840
 
 
3841
 
  @retval
3842
 
    0                   on error
3843
 
  @retval
3844
 
    new_created field
3845
 
*/
3846
 
 
3847
 
Field *create_tmp_field(THD *thd, Table *table,Item *item, Item::Type type,
3848
 
                        Item ***copy_func, Field **from_field,
3849
 
                        Field **default_field,
3850
 
                        bool group, bool modify_item,
3851
 
                        bool table_cant_handle_bit_fields __attribute__((unused)),
3852
 
                        bool make_copy_field,
3853
 
                        uint32_t convert_blob_length)
3854
 
{
3855
 
  Field *result;
3856
 
  Item::Type orig_type= type;
3857
 
  Item *orig_item= 0;
3858
 
 
3859
 
  if (type != Item::FIELD_ITEM &&
3860
 
      item->real_item()->type() == Item::FIELD_ITEM)
3861
 
  {
3862
 
    orig_item= item;
3863
 
    item= item->real_item();
3864
 
    type= Item::FIELD_ITEM;
3865
 
  }
3866
 
 
3867
 
  switch (type) {
3868
 
  case Item::SUM_FUNC_ITEM:
3869
 
  {
3870
 
    Item_sum *item_sum=(Item_sum*) item;
3871
 
    result= item_sum->create_tmp_field(group, table, convert_blob_length);
3872
 
    if (!result)
3873
 
      my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
3874
 
    return result;
3875
 
  }
3876
 
  case Item::FIELD_ITEM:
3877
 
  case Item::DEFAULT_VALUE_ITEM:
3878
 
  {
3879
 
    Item_field *field= (Item_field*) item;
3880
 
    bool orig_modify= modify_item;
3881
 
    if (orig_type == Item::REF_ITEM)
3882
 
      modify_item= 0;
3883
 
    /*
3884
 
      If item have to be able to store NULLs but underlaid field can't do it,
3885
 
      create_tmp_field_from_field() can't be used for tmp field creation.
3886
 
    */
3887
 
    if (field->maybe_null && !field->field->maybe_null())
3888
 
    {
3889
 
      result= create_tmp_field_from_item(thd, item, table, NULL,
3890
 
                                         modify_item, convert_blob_length);
3891
 
      *from_field= field->field;
3892
 
      if (result && modify_item)
3893
 
        field->result_field= result;
3894
 
    } 
3895
 
    else
3896
 
      result= create_tmp_field_from_field(thd, (*from_field= field->field),
3897
 
                                          orig_item ? orig_item->name :
3898
 
                                          item->name,
3899
 
                                          table,
3900
 
                                          modify_item ? field :
3901
 
                                          NULL,
3902
 
                                          convert_blob_length);
3903
 
    if (orig_type == Item::REF_ITEM && orig_modify)
3904
 
      ((Item_ref*)orig_item)->set_result_field(result);
3905
 
    if (field->field->eq_def(result))
3906
 
      *default_field= field->field;
3907
 
    return result;
3908
 
  }
3909
 
  /* Fall through */
3910
 
  case Item::FUNC_ITEM:
3911
 
    /* Fall through */
3912
 
  case Item::COND_ITEM:
3913
 
  case Item::FIELD_AVG_ITEM:
3914
 
  case Item::FIELD_STD_ITEM:
3915
 
  case Item::SUBSELECT_ITEM:
3916
 
    /* The following can only happen with 'CREATE TABLE ... SELECT' */
3917
 
  case Item::PROC_ITEM:
3918
 
  case Item::INT_ITEM:
3919
 
  case Item::REAL_ITEM:
3920
 
  case Item::DECIMAL_ITEM:
3921
 
  case Item::STRING_ITEM:
3922
 
  case Item::REF_ITEM:
3923
 
  case Item::NULL_ITEM:
3924
 
  case Item::VARBIN_ITEM:
3925
 
    if (make_copy_field)
3926
 
    {
3927
 
      assert(((Item_result_field*)item)->result_field);
3928
 
      *from_field= ((Item_result_field*)item)->result_field;
3929
 
    }
3930
 
    return create_tmp_field_from_item(thd, item, table,
3931
 
                                      (make_copy_field ? 0 : copy_func),
3932
 
                                       modify_item, convert_blob_length);
3933
 
  case Item::TYPE_HOLDER:  
3934
 
    result= ((Item_type_holder *)item)->make_field_by_type(table);
3935
 
    result->set_derivation(item->collation.derivation);
3936
 
    return result;
3937
 
  default:                                      // Dosen't have to be stored
3938
 
    return 0;
3939
 
  }
3940
 
}
3941
 
 
3942
 
/**
3943
 
  Create a temp table according to a field list.
3944
 
 
3945
 
  Given field pointers are changed to point at tmp_table for
3946
 
  send_fields. The table object is self contained: it's
3947
 
  allocated in its own memory root, as well as Field objects
3948
 
  created for table columns.
3949
 
  This function will replace Item_sum items in 'fields' list with
3950
 
  corresponding Item_field items, pointing at the fields in the
3951
 
  temporary table, unless this was prohibited by true
3952
 
  value of argument save_sum_fields. The Item_field objects
3953
 
  are created in THD memory root.
3954
 
 
3955
 
  @param thd                  thread handle
3956
 
  @param param                a description used as input to create the table
3957
 
  @param fields               list of items that will be used to define
3958
 
                              column types of the table (also see NOTES)
3959
 
  @param group                TODO document
3960
 
  @param distinct             should table rows be distinct
3961
 
  @param save_sum_fields      see NOTES
3962
 
  @param select_options
3963
 
  @param rows_limit
3964
 
  @param table_alias          possible name of the temporary table that can
3965
 
                              be used for name resolving; can be "".
3966
 
*/
3967
 
 
3968
 
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
3969
 
#define AVG_STRING_LENGTH_TO_PACK_ROWS   64
3970
 
#define RATIO_TO_PACK_ROWS             2
3971
 
#define MIN_STRING_LENGTH_TO_PACK_ROWS   10
3972
 
 
3973
 
Table *
3974
 
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
3975
 
                 order_st *group, bool distinct, bool save_sum_fields,
3976
 
                 uint64_t select_options, ha_rows rows_limit,
3977
 
                 char *table_alias)
3978
 
{
3979
 
  MEM_ROOT *mem_root_save, own_root;
3980
 
  Table *table;
3981
 
  TABLE_SHARE *share;
3982
 
  uint  i,field_count,null_count,null_pack_length;
3983
 
  uint32_t  copy_func_count= param->func_count;
3984
 
  uint32_t  hidden_null_count, hidden_null_pack_length, hidden_field_count;
3985
 
  uint32_t  blob_count,group_null_items, string_count;
3986
 
  uint32_t  temp_pool_slot=MY_BIT_NONE;
3987
 
  uint32_t fieldnr= 0;
3988
 
  ulong reclength, string_total_length;
3989
 
  bool  using_unique_constraint= 0;
3990
 
  bool  use_packed_rows= 0;
3991
 
  bool  not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
3992
 
  char  *tmpname,path[FN_REFLEN];
3993
 
  unsigned char *pos, *group_buff, *bitmaps;
3994
 
  unsigned char *null_flags;
3995
 
  Field **reg_field, **from_field, **default_field;
3996
 
  uint32_t *blob_field;
3997
 
  Copy_field *copy=0;
3998
 
  KEY *keyinfo;
3999
 
  KEY_PART_INFO *key_part_info;
4000
 
  Item **copy_func;
4001
 
  MI_COLUMNDEF *recinfo;
4002
 
  uint32_t total_uneven_bit_length= 0;
4003
 
  bool force_copy_fields= param->force_copy_fields;
4004
 
 
4005
 
  status_var_increment(thd->status_var.created_tmp_tables);
4006
 
 
4007
 
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
4008
 
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
4009
 
 
4010
 
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
4011
 
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
4012
 
            current_pid, temp_pool_slot);
4013
 
  else
4014
 
  {
4015
 
    /* if we run out of slots or we are not using tempool */
4016
 
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
4017
 
            thd->thread_id, thd->tmp_table++);
4018
 
  }
4019
 
 
4020
 
  /*
4021
 
    No need to change table name to lower case as we are only creating
4022
 
    MyISAM or HEAP tables here
4023
 
  */
4024
 
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
4025
 
 
4026
 
 
4027
 
  if (group)
4028
 
  {
4029
 
    if (!param->quick_group)
4030
 
      group=0;                                  // Can't use group key
4031
 
    else for (order_st *tmp=group ; tmp ; tmp=tmp->next)
4032
 
    {
4033
 
      /*
4034
 
        marker == 4 means two things:
4035
 
        - store NULLs in the key, and
4036
 
        - convert BIT fields to 64-bit long, needed because MEMORY tables
4037
 
          can't index BIT fields.
4038
 
      */
4039
 
      (*tmp->item)->marker= 4;
4040
 
      if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
4041
 
        using_unique_constraint=1;
4042
 
    }
4043
 
    if (param->group_length >= MAX_BLOB_WIDTH)
4044
 
      using_unique_constraint=1;
4045
 
    if (group)
4046
 
      distinct=0;                               // Can't use distinct
4047
 
  }
4048
 
 
4049
 
  field_count=param->field_count+param->func_count+param->sum_func_count;
4050
 
  hidden_field_count=param->hidden_field_count;
4051
 
 
4052
 
  /*
4053
 
    When loose index scan is employed as access method, it already
4054
 
    computes all groups and the result of all aggregate functions. We
4055
 
    make space for the items of the aggregate function in the list of
4056
 
    functions TMP_TABLE_PARAM::items_to_copy, so that the values of
4057
 
    these items are stored in the temporary table.
4058
 
  */
4059
 
  if (param->precomputed_group_by)
4060
 
    copy_func_count+= param->sum_func_count;
4061
 
  
4062
 
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
4063
 
 
4064
 
  if (!multi_alloc_root(&own_root,
4065
 
                        &table, sizeof(*table),
4066
 
                        &share, sizeof(*share),
4067
 
                        &reg_field, sizeof(Field*) * (field_count+1),
4068
 
                        &default_field, sizeof(Field*) * (field_count),
4069
 
                        &blob_field, sizeof(uint)*(field_count+1),
4070
 
                        &from_field, sizeof(Field*)*field_count,
4071
 
                        &copy_func, sizeof(*copy_func)*(copy_func_count+1),
4072
 
                        &param->keyinfo, sizeof(*param->keyinfo),
4073
 
                        &key_part_info,
4074
 
                        sizeof(*key_part_info)*(param->group_parts+1),
4075
 
                        &param->start_recinfo,
4076
 
                        sizeof(*param->recinfo)*(field_count*2+4),
4077
 
                        &tmpname, (uint) strlen(path)+1,
4078
 
                        &group_buff, (group && ! using_unique_constraint ?
4079
 
                                      param->group_length : 0),
4080
 
                        &bitmaps, bitmap_buffer_size(field_count)*2,
4081
 
                        NULL))
4082
 
  {
4083
 
    if (temp_pool_slot != MY_BIT_NONE)
4084
 
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
4085
 
    return(NULL);                               /* purecov: inspected */
4086
 
  }
4087
 
  /* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
4088
 
  if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
4089
 
  {
4090
 
    if (temp_pool_slot != MY_BIT_NONE)
4091
 
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
4092
 
    free_root(&own_root, MYF(0));               /* purecov: inspected */
4093
 
    return(NULL);                               /* purecov: inspected */
4094
 
  }
4095
 
  param->items_to_copy= copy_func;
4096
 
  my_stpcpy(tmpname,path);
4097
 
  /* make table according to fields */
4098
 
 
4099
 
  memset(table, 0, sizeof(*table));
4100
 
  memset(reg_field, 0, sizeof(Field*)*(field_count+1));
4101
 
  memset(default_field, 0, sizeof(Field*) * (field_count));
4102
 
  memset(from_field, 0, sizeof(Field*)*field_count);
4103
 
 
4104
 
  table->mem_root= own_root;
4105
 
  mem_root_save= thd->mem_root;
4106
 
  thd->mem_root= &table->mem_root;
4107
 
 
4108
 
  table->field=reg_field;
4109
 
  table->alias= table_alias;
4110
 
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
4111
 
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
4112
 
  table->map=1;
4113
 
  table->temp_pool_slot = temp_pool_slot;
4114
 
  table->copy_blobs= 1;
4115
 
  table->in_use= thd;
4116
 
  table->quick_keys.init();
4117
 
  table->covering_keys.init();
4118
 
  table->keys_in_use_for_query.init();
4119
 
 
4120
 
  table->setShare(share);
4121
 
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
4122
 
  share->blob_field= blob_field;
4123
 
  share->blob_ptr_size= portable_sizeof_char_ptr;
4124
 
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
4125
 
  share->table_charset= param->table_charset;
4126
 
  share->primary_key= MAX_KEY;               // Indicate no primary key
4127
 
  share->keys_for_keyread.init();
4128
 
  share->keys_in_use.init();
4129
 
 
4130
 
  /* Calculate which type of fields we will store in the temporary table */
4131
 
 
4132
 
  reclength= string_total_length= 0;
4133
 
  blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
4134
 
  param->using_indirect_summary_function=0;
4135
 
 
4136
 
  List_iterator_fast<Item> li(fields);
4137
 
  Item *item;
4138
 
  Field **tmp_from_field=from_field;
4139
 
  while ((item=li++))
4140
 
  {
4141
 
    Item::Type type=item->type();
4142
 
    if (not_all_columns)
4143
 
    {
4144
 
      if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
4145
 
      {
4146
 
        if (item->used_tables() & OUTER_REF_TABLE_BIT)
4147
 
          item->update_used_tables();
4148
 
        if (type == Item::SUBSELECT_ITEM ||
4149
 
            (item->used_tables() & ~OUTER_REF_TABLE_BIT))
4150
 
        {
4151
 
          /*
4152
 
            Mark that the we have ignored an item that refers to a summary
4153
 
            function. We need to know this if someone is going to use
4154
 
            DISTINCT on the result.
4155
 
          */
4156
 
          param->using_indirect_summary_function=1;
4157
 
          continue;
4158
 
        }
4159
 
      }
4160
 
      if (item->const_item() && (int) hidden_field_count <= 0)
4161
 
        continue; // We don't have to store this
4162
 
    }
4163
 
    if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
4164
 
    {                                           /* Can't calc group yet */
4165
 
      ((Item_sum*) item)->result_field=0;
4166
 
      for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
4167
 
      {
4168
 
        Item **argp= ((Item_sum*) item)->args + i;
4169
 
        Item *arg= *argp;
4170
 
        if (!arg->const_item())
4171
 
        {
4172
 
          Field *new_field=
4173
 
            create_tmp_field(thd, table, arg, arg->type(), &copy_func,
4174
 
                             tmp_from_field, &default_field[fieldnr],
4175
 
                             group != 0,not_all_columns,
4176
 
                             distinct, 0,
4177
 
                             param->convert_blob_length);
4178
 
          if (!new_field)
4179
 
            goto err;                                   // Should be OOM
4180
 
          tmp_from_field++;
4181
 
          reclength+=new_field->pack_length();
4182
 
          if (new_field->flags & BLOB_FLAG)
4183
 
          {
4184
 
            *blob_field++= fieldnr;
4185
 
            blob_count++;
4186
 
          }
4187
 
          *(reg_field++)= new_field;
4188
 
          if (new_field->real_type() == DRIZZLE_TYPE_VARCHAR)
4189
 
          {
4190
 
            string_count++;
4191
 
            string_total_length+= new_field->pack_length();
4192
 
          }
4193
 
          thd->mem_root= mem_root_save;
4194
 
          thd->change_item_tree(argp, new Item_field(new_field));
4195
 
          thd->mem_root= &table->mem_root;
4196
 
          if (!(new_field->flags & NOT_NULL_FLAG))
4197
 
          {
4198
 
            null_count++;
4199
 
            /*
4200
 
              new_field->maybe_null() is still false, it will be
4201
 
              changed below. But we have to setup Item_field correctly
4202
 
            */
4203
 
            (*argp)->maybe_null=1;
4204
 
          }
4205
 
          new_field->field_index= fieldnr++;
4206
 
        }
4207
 
      }
4208
 
    }
4209
 
    else
4210
 
    {
4211
 
      /*
4212
 
        The last parameter to create_tmp_field() is a bit tricky:
4213
 
 
4214
 
        We need to set it to 0 in union, to get fill_record() to modify the
4215
 
        temporary table.
4216
 
        We need to set it to 1 on multi-table-update and in select to
4217
 
        write rows to the temporary table.
4218
 
        We here distinguish between UNION and multi-table-updates by the fact
4219
 
        that in the later case group is set to the row pointer.
4220
 
      */
4221
 
      Field *new_field= (param->schema_table) ?
4222
 
        create_tmp_field_for_schema(thd, item, table) :
4223
 
        create_tmp_field(thd, table, item, type, &copy_func,
4224
 
                         tmp_from_field, &default_field[fieldnr],
4225
 
                         group != 0,
4226
 
                         !force_copy_fields &&
4227
 
                           (not_all_columns || group !=0),
4228
 
                         /*
4229
 
                           If item->marker == 4 then we force create_tmp_field
4230
 
                           to create a 64-bit longs for BIT fields because HEAP
4231
 
                           tables can't index BIT fields directly. We do the same
4232
 
                           for distinct, as we want the distinct index to be
4233
 
                           usable in this case too.
4234
 
                         */
4235
 
                         item->marker == 4 || param->bit_fields_as_long,
4236
 
                         force_copy_fields,
4237
 
                         param->convert_blob_length);
4238
 
 
4239
 
      if (!new_field)
4240
 
      {
4241
 
        if (thd->is_fatal_error)
4242
 
          goto err;                             // Got OOM
4243
 
        continue;                               // Some kindf of const item
4244
 
      }
4245
 
      if (type == Item::SUM_FUNC_ITEM)
4246
 
        ((Item_sum *) item)->result_field= new_field;
4247
 
      tmp_from_field++;
4248
 
      reclength+=new_field->pack_length();
4249
 
      if (!(new_field->flags & NOT_NULL_FLAG))
4250
 
        null_count++;
4251
 
      if (new_field->flags & BLOB_FLAG)
4252
 
      {
4253
 
        *blob_field++= fieldnr;
4254
 
        blob_count++;
4255
 
      }
4256
 
      if (item->marker == 4 && item->maybe_null)
4257
 
      {
4258
 
        group_null_items++;
4259
 
        new_field->flags|= GROUP_FLAG;
4260
 
      }
4261
 
      new_field->field_index= fieldnr++;
4262
 
      *(reg_field++)= new_field;
4263
 
    }
4264
 
    if (!--hidden_field_count)
4265
 
    {
4266
 
      /*
4267
 
        This was the last hidden field; Remember how many hidden fields could
4268
 
        have null
4269
 
      */
4270
 
      hidden_null_count=null_count;
4271
 
      /*
4272
 
        We need to update hidden_field_count as we may have stored group
4273
 
        functions with constant arguments
4274
 
      */
4275
 
      param->hidden_field_count= fieldnr;
4276
 
      null_count= 0;
4277
 
    }
4278
 
  }
4279
 
  assert(fieldnr == (uint) (reg_field - table->field));
4280
 
  assert(field_count >= (uint) (reg_field - table->field));
4281
 
  field_count= fieldnr;
4282
 
  *reg_field= 0;
4283
 
  *blob_field= 0;                               // End marker
4284
 
  share->fields= field_count;
4285
 
 
4286
 
  /* If result table is small; use a heap */
4287
 
  /* future: storage engine selection can be made dynamic? */
4288
 
  if (blob_count || using_unique_constraint ||
4289
 
      (select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
4290
 
      OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
4291
 
  {
4292
 
    share->db_plugin= ha_lock_engine(0, myisam_hton);
4293
 
    table->file= get_new_handler(share, &table->mem_root,
4294
 
                                 share->db_type());
4295
 
    if (group &&
4296
 
        (param->group_parts > table->file->max_key_parts() ||
4297
 
         param->group_length > table->file->max_key_length()))
4298
 
      using_unique_constraint=1;
4299
 
  }
4300
 
  else
4301
 
  {
4302
 
    share->db_plugin= ha_lock_engine(0, heap_hton);
4303
 
    table->file= get_new_handler(share, &table->mem_root,
4304
 
                                 share->db_type());
4305
 
  }
4306
 
  if (!table->file)
4307
 
    goto err;
4308
 
 
4309
 
 
4310
 
  if (!using_unique_constraint)
4311
 
    reclength+= group_null_items;       // null flag is stored separately
4312
 
 
4313
 
  share->blob_fields= blob_count;
4314
 
  if (blob_count == 0)
4315
 
  {
4316
 
    /* We need to ensure that first byte is not 0 for the delete link */
4317
 
    if (param->hidden_field_count)
4318
 
      hidden_null_count++;
4319
 
    else
4320
 
      null_count++;
4321
 
  }
4322
 
  hidden_null_pack_length=(hidden_null_count+7)/8;
4323
 
  null_pack_length= (hidden_null_pack_length +
4324
 
                     (null_count + total_uneven_bit_length + 7) / 8);
4325
 
  reclength+=null_pack_length;
4326
 
  if (!reclength)
4327
 
    reclength=1;                                // Dummy select
4328
 
  /* Use packed rows if there is blobs or a lot of space to gain */
4329
 
  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)))
4330
 
    use_packed_rows= 1;
4331
 
 
4332
 
  share->reclength= reclength;
4333
 
  {
4334
 
    uint32_t alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
4335
 
    share->rec_buff_length= alloc_length;
4336
 
    if (!(table->record[0]= (unsigned char*)
4337
 
                            alloc_root(&table->mem_root, alloc_length*3)))
4338
 
      goto err;
4339
 
    table->record[1]= table->record[0]+alloc_length;
4340
 
    share->default_values= table->record[1]+alloc_length;
4341
 
  }
4342
 
  copy_func[0]=0;                               // End marker
4343
 
  param->func_count= copy_func - param->items_to_copy; 
4344
 
 
4345
 
  table->setup_tmp_table_column_bitmaps(bitmaps);
4346
 
 
4347
 
  recinfo=param->start_recinfo;
4348
 
  null_flags=(unsigned char*) table->record[0];
4349
 
  pos=table->record[0]+ null_pack_length;
4350
 
  if (null_pack_length)
4351
 
  {
4352
 
    memset(recinfo, 0, sizeof(*recinfo));
4353
 
    recinfo->type=FIELD_NORMAL;
4354
 
    recinfo->length=null_pack_length;
4355
 
    recinfo++;
4356
 
    memset(null_flags, 255, null_pack_length);  // Set null fields
4357
 
 
4358
 
    table->null_flags= (unsigned char*) table->record[0];
4359
 
    share->null_fields= null_count+ hidden_null_count;
4360
 
    share->null_bytes= null_pack_length;
4361
 
  }
4362
 
  null_count= (blob_count == 0) ? 1 : 0;
4363
 
  hidden_field_count=param->hidden_field_count;
4364
 
  for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
4365
 
  {
4366
 
    Field *field= *reg_field;
4367
 
    uint32_t length;
4368
 
    memset(recinfo, 0, sizeof(*recinfo));
4369
 
 
4370
 
    if (!(field->flags & NOT_NULL_FLAG))
4371
 
    {
4372
 
      if (field->flags & GROUP_FLAG && !using_unique_constraint)
4373
 
      {
4374
 
        /*
4375
 
          We have to reserve one byte here for NULL bits,
4376
 
          as this is updated by 'end_update()'
4377
 
        */
4378
 
        *pos++=0;                               // Null is stored here
4379
 
        recinfo->length=1;
4380
 
        recinfo->type=FIELD_NORMAL;
4381
 
        recinfo++;
4382
 
        memset(recinfo, 0, sizeof(*recinfo));
4383
 
      }
4384
 
      else
4385
 
      {
4386
 
        recinfo->null_bit= 1 << (null_count & 7);
4387
 
        recinfo->null_pos= null_count/8;
4388
 
      }
4389
 
      field->move_field(pos,null_flags+null_count/8,
4390
 
                        1 << (null_count & 7));
4391
 
      null_count++;
4392
 
    }
4393
 
    else
4394
 
      field->move_field(pos,(unsigned char*) 0,0);
4395
 
    field->reset();
4396
 
 
4397
 
    /*
4398
 
      Test if there is a default field value. The test for ->ptr is to skip
4399
 
      'offset' fields generated by initalize_tables
4400
 
    */
4401
 
    if (default_field[i] && default_field[i]->ptr)
4402
 
    {
4403
 
      /* 
4404
 
         default_field[i] is set only in the cases  when 'field' can
4405
 
         inherit the default value that is defined for the field referred
4406
 
         by the Item_field object from which 'field' has been created.
4407
 
      */
4408
 
      my_ptrdiff_t diff;
4409
 
      Field *orig_field= default_field[i];
4410
 
      /* Get the value from default_values */
4411
 
      diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
4412
 
                            orig_field->table->record[0]);
4413
 
      orig_field->move_field_offset(diff);      // Points now at default_values
4414
 
      if (orig_field->is_real_null())
4415
 
        field->set_null();
4416
 
      else
4417
 
      {
4418
 
        field->set_notnull();
4419
 
        memcpy(field->ptr, orig_field->ptr, field->pack_length());
4420
 
      }
4421
 
      orig_field->move_field_offset(-diff);     // Back to record[0]
4422
 
    } 
4423
 
 
4424
 
    if (from_field[i])
4425
 
    {                                           /* Not a table Item */
4426
 
      copy->set(field,from_field[i],save_sum_fields);
4427
 
      copy++;
4428
 
    }
4429
 
    length=field->pack_length();
4430
 
    pos+= length;
4431
 
 
4432
 
    /* Make entry for create table */
4433
 
    recinfo->length=length;
4434
 
    if (field->flags & BLOB_FLAG)
4435
 
      recinfo->type= (int) FIELD_BLOB;
4436
 
    else
4437
 
      recinfo->type=FIELD_NORMAL;
4438
 
    if (!--hidden_field_count)
4439
 
      null_count=(null_count+7) & ~7;           // move to next byte
4440
 
 
4441
 
    // fix table name in field entry
4442
 
    field->table_name= &table->alias;
4443
 
  }
4444
 
 
4445
 
  param->copy_field_end=copy;
4446
 
  param->recinfo=recinfo;
4447
 
  store_record(table,s->default_values);        // Make empty default record
4448
 
 
4449
 
  if (thd->variables.tmp_table_size == ~ (uint64_t) 0)          // No limit
4450
 
    share->max_rows= ~(ha_rows) 0;
4451
 
  else
4452
 
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
4453
 
                                 cmin(thd->variables.tmp_table_size,
4454
 
                                     thd->variables.max_heap_table_size) :
4455
 
                                 thd->variables.tmp_table_size) /
4456
 
                                 share->reclength);
4457
 
  set_if_bigger(share->max_rows,1);             // For dummy start options
4458
 
  /*
4459
 
    Push the LIMIT clause to the temporary table creation, so that we
4460
 
    materialize only up to 'rows_limit' records instead of all result records.
4461
 
  */
4462
 
  set_if_smaller(share->max_rows, rows_limit);
4463
 
  param->end_write_records= rows_limit;
4464
 
 
4465
 
  keyinfo= param->keyinfo;
4466
 
 
4467
 
  if (group)
4468
 
  {
4469
 
    table->group=group;                         /* Table is grouped by key */
4470
 
    param->group_buff=group_buff;
4471
 
    share->keys=1;
4472
 
    share->uniques= test(using_unique_constraint);
4473
 
    table->key_info=keyinfo;
4474
 
    keyinfo->key_part=key_part_info;
4475
 
    keyinfo->flags=HA_NOSAME;
4476
 
    keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
4477
 
    keyinfo->key_length=0;
4478
 
    keyinfo->rec_per_key=0;
4479
 
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
4480
 
    keyinfo->name= (char*) "group_key";
4481
 
    order_st *cur_group= group;
4482
 
    for (; cur_group ; cur_group= cur_group->next, key_part_info++)
4483
 
    {
4484
 
      Field *field=(*cur_group->item)->get_tmp_table_field();
4485
 
      bool maybe_null=(*cur_group->item)->maybe_null;
4486
 
      key_part_info->null_bit=0;
4487
 
      key_part_info->field=  field;
4488
 
      key_part_info->offset= field->offset(table->record[0]);
4489
 
      key_part_info->length= (uint16_t) field->key_length();
4490
 
      key_part_info->type=   (uint8_t) field->key_type();
4491
 
      key_part_info->key_type =
4492
 
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
4493
 
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
4494
 
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
4495
 
        0 : FIELDFLAG_BINARY;
4496
 
      if (!using_unique_constraint)
4497
 
      {
4498
 
        cur_group->buff=(char*) group_buff;
4499
 
        if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
4500
 
                                                     group_buff +
4501
 
                                                     test(maybe_null),
4502
 
                                                     field->null_ptr,
4503
 
                                                     field->null_bit)))
4504
 
          goto err; /* purecov: inspected */
4505
 
        if (maybe_null)
4506
 
        {
4507
 
          /*
4508
 
            To be able to group on NULL, we reserved place in group_buff
4509
 
            for the NULL flag just before the column. (see above).
4510
 
            The field data is after this flag.
4511
 
            The NULL flag is updated in 'end_update()' and 'end_write()'
4512
 
          */
4513
 
          keyinfo->flags|= HA_NULL_ARE_EQUAL;   // def. that NULL == NULL
4514
 
          key_part_info->null_bit=field->null_bit;
4515
 
          key_part_info->null_offset= (uint) (field->null_ptr -
4516
 
                                              (unsigned char*) table->record[0]);
4517
 
          cur_group->buff++;                        // Pointer to field data
4518
 
          group_buff++;                         // Skipp null flag
4519
 
        }
4520
 
        /* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
4521
 
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
4522
 
        group_buff+= cur_group->field->pack_length();
4523
 
      }
4524
 
      keyinfo->key_length+=  key_part_info->length;
4525
 
    }
4526
 
  }
4527
 
 
4528
 
  if (distinct && field_count != param->hidden_field_count)
4529
 
  {
4530
 
    /*
4531
 
      Create an unique key or an unique constraint over all columns
4532
 
      that should be in the result.  In the temporary table, there are
4533
 
      'param->hidden_field_count' extra columns, whose null bits are stored
4534
 
      in the first 'hidden_null_pack_length' bytes of the row.
4535
 
    */
4536
 
    if (blob_count)
4537
 
    {
4538
 
      /*
4539
 
        Special mode for index creation in MyISAM used to support unique
4540
 
        indexes on blobs with arbitrary length. Such indexes cannot be
4541
 
        used for lookups.
4542
 
      */
4543
 
      share->uniques= 1;
4544
 
    }
4545
 
    null_pack_length-=hidden_null_pack_length;
4546
 
    keyinfo->key_parts= ((field_count-param->hidden_field_count)+
4547
 
                         (share->uniques ? test(null_pack_length) : 0));
4548
 
    table->distinct= 1;
4549
 
    share->keys= 1;
4550
 
    if (!(key_part_info= (KEY_PART_INFO*)
4551
 
          alloc_root(&table->mem_root,
4552
 
                     keyinfo->key_parts * sizeof(KEY_PART_INFO))))
4553
 
      goto err;
4554
 
    memset(key_part_info, 0, keyinfo->key_parts * sizeof(KEY_PART_INFO));
4555
 
    table->key_info=keyinfo;
4556
 
    keyinfo->key_part=key_part_info;
4557
 
    keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
4558
 
    keyinfo->key_length=(uint16_t) reclength;
4559
 
    keyinfo->name= (char*) "distinct_key";
4560
 
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
4561
 
    keyinfo->rec_per_key=0;
4562
 
 
4563
 
    /*
4564
 
      Create an extra field to hold NULL bits so that unique indexes on
4565
 
      blobs can distinguish NULL from 0. This extra field is not needed
4566
 
      when we do not use UNIQUE indexes for blobs.
4567
 
    */
4568
 
    if (null_pack_length && share->uniques)
4569
 
    {
4570
 
      key_part_info->null_bit=0;
4571
 
      key_part_info->offset=hidden_null_pack_length;
4572
 
      key_part_info->length=null_pack_length;
4573
 
      key_part_info->field= new Field_varstring(table->record[0],
4574
 
                                                (uint32_t) key_part_info->length,
4575
 
                                                0,
4576
 
                                                (unsigned char*) 0,
4577
 
                                                (uint) 0,
4578
 
                                                Field::NONE,
4579
 
                                                NULL, 
4580
 
                                                table->s,
4581
 
                                                &my_charset_bin);
4582
 
      if (!key_part_info->field)
4583
 
        goto err;
4584
 
      key_part_info->field->init(table);
4585
 
      key_part_info->key_type=FIELDFLAG_BINARY;
4586
 
      key_part_info->type=    HA_KEYTYPE_BINARY;
4587
 
      key_part_info++;
4588
 
    }
4589
 
    /* Create a distinct key over the columns we are going to return */
4590
 
    for (i=param->hidden_field_count, reg_field=table->field + i ;
4591
 
         i < field_count;
4592
 
         i++, reg_field++, key_part_info++)
4593
 
    {
4594
 
      key_part_info->null_bit=0;
4595
 
      key_part_info->field=    *reg_field;
4596
 
      key_part_info->offset=   (*reg_field)->offset(table->record[0]);
4597
 
      key_part_info->length=   (uint16_t) (*reg_field)->pack_length();
4598
 
      /* TODO:
4599
 
        The below method of computing the key format length of the
4600
 
        key part is a copy/paste from opt_range.cc, and table.cc.
4601
 
        This should be factored out, e.g. as a method of Field.
4602
 
        In addition it is not clear if any of the Field::*_length
4603
 
        methods is supposed to compute the same length. If so, it
4604
 
        might be reused.
4605
 
      */
4606
 
      key_part_info->store_length= key_part_info->length;
4607
 
 
4608
 
      if ((*reg_field)->real_maybe_null())
4609
 
        key_part_info->store_length+= HA_KEY_NULL_LENGTH;
4610
 
      if ((*reg_field)->type() == DRIZZLE_TYPE_BLOB || 
4611
 
          (*reg_field)->real_type() == DRIZZLE_TYPE_VARCHAR)
4612
 
        key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
4613
 
 
4614
 
      key_part_info->type=     (uint8_t) (*reg_field)->key_type();
4615
 
      key_part_info->key_type =
4616
 
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
4617
 
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
4618
 
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
4619
 
        0 : FIELDFLAG_BINARY;
4620
 
    }
4621
 
  }
4622
 
 
4623
 
  if (thd->is_fatal_error)                              // If end of memory
4624
 
    goto err;                                    /* purecov: inspected */
4625
 
  share->db_record_offset= 1;
4626
 
  if (share->db_type() == myisam_hton)
4627
 
  {
4628
 
    if (table->create_myisam_tmp_table(param->keyinfo, param->start_recinfo,
4629
 
                                       &param->recinfo, select_options))
4630
 
      goto err;
4631
 
  }
4632
 
  if (table->open_tmp_table())
4633
 
    goto err;
4634
 
 
4635
 
  thd->mem_root= mem_root_save;
4636
 
 
4637
 
  return(table);
4638
 
 
4639
 
err:
4640
 
  thd->mem_root= mem_root_save;
4641
 
  table->free_tmp_table(thd);                    /* purecov: inspected */
4642
 
  if (temp_pool_slot != MY_BIT_NONE)
4643
 
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
4644
 
  return(NULL);                         /* purecov: inspected */
4645
 
}
4646
 
 
4647
 
/****************************************************************************/
4648
 
 
4649
 
/**
4650
 
  Create a reduced Table object with properly set up Field list from a
4651
 
  list of field definitions.
4652
 
 
4653
 
    The created table doesn't have a table handler associated with
4654
 
    it, has no keys, no group/distinct, no copy_funcs array.
4655
 
    The sole purpose of this Table object is to use the power of Field
4656
 
    class to read/write data to/from table->record[0]. Then one can store
4657
 
    the record in any container (RB tree, hash, etc).
4658
 
    The table is created in THD mem_root, so are the table's fields.
4659
 
    Consequently, if you don't BLOB fields, you don't need to free it.
4660
 
 
4661
 
  @param thd         connection handle
4662
 
  @param field_list  list of column definitions
4663
 
 
4664
 
  @return
4665
 
    0 if out of memory, Table object in case of success
4666
 
*/
4667
 
 
4668
 
Table *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
4669
 
{
4670
 
  uint32_t field_count= field_list.elements;
4671
 
  uint32_t blob_count= 0;
4672
 
  Field **field;
4673
 
  Create_field *cdef;                           /* column definition */
4674
 
  uint32_t record_length= 0;
4675
 
  uint32_t null_count= 0;                 /* number of columns which may be null */
4676
 
  uint32_t null_pack_length;              /* NULL representation array length */
4677
 
  uint32_t *blob_field;
4678
 
  unsigned char *bitmaps;
4679
 
  Table *table;
4680
 
  TABLE_SHARE *share;
4681
 
 
4682
 
  if (!multi_alloc_root(thd->mem_root,
4683
 
                        &table, sizeof(*table),
4684
 
                        &share, sizeof(*share),
4685
 
                        &field, (field_count + 1) * sizeof(Field*),
4686
 
                        &blob_field, (field_count+1) *sizeof(uint),
4687
 
                        &bitmaps, bitmap_buffer_size(field_count)*2,
4688
 
                        NULL))
4689
 
    return 0;
4690
 
 
4691
 
  memset(table, 0, sizeof(*table));
4692
 
  memset(share, 0, sizeof(*share));
4693
 
  table->field= field;
4694
 
  table->s= share;
4695
 
  share->blob_field= blob_field;
4696
 
  share->fields= field_count;
4697
 
  share->blob_ptr_size= portable_sizeof_char_ptr;
4698
 
  table->setup_tmp_table_column_bitmaps(bitmaps);
4699
 
 
4700
 
  /* Create all fields and calculate the total length of record */
4701
 
  List_iterator_fast<Create_field> it(field_list);
4702
 
  while ((cdef= it++))
4703
 
  {
4704
 
    *field= make_field(share, 0, cdef->length,
4705
 
                       (unsigned char*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
4706
 
                       f_maybe_null(cdef->pack_flag) ? 1 : 0,
4707
 
                       cdef->pack_flag, cdef->sql_type, cdef->charset,
4708
 
                       cdef->unireg_check,
4709
 
                       cdef->interval, cdef->field_name);
4710
 
    if (!*field)
4711
 
      goto error;
4712
 
    (*field)->init(table);
4713
 
    record_length+= (*field)->pack_length();
4714
 
    if (! ((*field)->flags & NOT_NULL_FLAG))
4715
 
      null_count++;
4716
 
 
4717
 
    if ((*field)->flags & BLOB_FLAG)
4718
 
      share->blob_field[blob_count++]= (uint) (field - table->field);
4719
 
 
4720
 
    field++;
4721
 
  }
4722
 
  *field= NULL;                             /* mark the end of the list */
4723
 
  share->blob_field[blob_count]= 0;            /* mark the end of the list */
4724
 
  share->blob_fields= blob_count;
4725
 
 
4726
 
  null_pack_length= (null_count + 7)/8;
4727
 
  share->reclength= record_length + null_pack_length;
4728
 
  share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
4729
 
  table->record[0]= (unsigned char*) thd->alloc(share->rec_buff_length);
4730
 
  if (!table->record[0])
4731
 
    goto error;
4732
 
 
4733
 
  if (null_pack_length)
4734
 
  {
4735
 
    table->null_flags= (unsigned char*) table->record[0];
4736
 
    share->null_fields= null_count;
4737
 
    share->null_bytes= null_pack_length;
4738
 
  }
4739
 
 
4740
 
  table->in_use= thd;           /* field->reset() may access table->in_use */
4741
 
  {
4742
 
    /* Set up field pointers */
4743
 
    unsigned char *null_pos= table->record[0];
4744
 
    unsigned char *field_pos= null_pos + share->null_bytes;
4745
 
    uint32_t null_bit= 1;
4746
 
 
4747
 
    for (field= table->field; *field; ++field)
4748
 
    {
4749
 
      Field *cur_field= *field;
4750
 
      if ((cur_field->flags & NOT_NULL_FLAG))
4751
 
        cur_field->move_field(field_pos);
4752
 
      else
4753
 
      {
4754
 
        cur_field->move_field(field_pos, (unsigned char*) null_pos, null_bit);
4755
 
        null_bit<<= 1;
4756
 
        if (null_bit == (1 << 8))
4757
 
        {
4758
 
          ++null_pos;
4759
 
          null_bit= 1;
4760
 
        }
4761
 
      }
4762
 
      cur_field->reset();
4763
 
 
4764
 
      field_pos+= cur_field->pack_length();
4765
 
    }
4766
 
  }
4767
 
  return table;
4768
 
error:
4769
 
  for (field= table->field; *field; ++field)
4770
 
    delete *field;                         /* just invokes field destructor */
4771
 
  return 0;
4772
 
}
4773
 
 
4774
 
 
4775
 
bool Table::open_tmp_table()
4776
 
{
4777
 
  int error;
4778
 
  if ((error=file->ha_open(this, s->table_name.str,O_RDWR,
4779
 
                                  HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
4780
 
  {
4781
 
    file->print_error(error,MYF(0)); /* purecov: inspected */
4782
 
    db_stat=0;
4783
 
    return(1);
4784
 
  }
4785
 
  (void) file->extra(HA_EXTRA_QUICK);           /* Faster */
4786
 
  return(0);
4787
 
}
4788
 
 
4789
 
 
4790
 
/*
4791
 
  Create MyISAM temporary table
4792
 
 
4793
 
  SYNOPSIS
4794
 
    create_myisam_tmp_table()
4795
 
      keyinfo         Description of the index (there is always one index)
4796
 
      start_recinfo   MyISAM's column descriptions
4797
 
      recinfo INOUT   End of MyISAM's column descriptions
4798
 
      options         Option bits
4799
 
   
4800
 
  DESCRIPTION
4801
 
    Create a MyISAM temporary table according to passed description. The is
4802
 
    assumed to have one unique index or constraint.
4803
 
 
4804
 
    The passed array or MI_COLUMNDEF structures must have this form:
4805
 
 
4806
 
      1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
4807
 
         when there are many nullable columns)
4808
 
      2. Table columns
4809
 
      3. One free MI_COLUMNDEF element (*recinfo points here)
4810
 
   
4811
 
    This function may use the free element to create hash column for unique
4812
 
    constraint.
4813
 
 
4814
 
   RETURN
4815
 
     false - OK
4816
 
     true  - Error
4817
 
*/
4818
 
 
4819
 
bool Table::create_myisam_tmp_table(KEY *keyinfo, 
4820
 
                                    MI_COLUMNDEF *start_recinfo,
4821
 
                                    MI_COLUMNDEF **recinfo, 
4822
 
                                    uint64_t options)
4823
 
{
4824
 
  int error;
4825
 
  MI_KEYDEF keydef;
4826
 
  MI_UNIQUEDEF uniquedef;
4827
 
  TABLE_SHARE *share= s;
4828
 
 
4829
 
  if (share->keys)
4830
 
  {                                             // Get keys for ni_create
4831
 
    bool using_unique_constraint=0;
4832
 
    HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&this->mem_root,
4833
 
                                            sizeof(*seg) * keyinfo->key_parts);
4834
 
    if (!seg)
4835
 
      goto err;
4836
 
 
4837
 
    memset(seg, 0, sizeof(*seg) * keyinfo->key_parts);
4838
 
    if (keyinfo->key_length >= file->max_key_length() ||
4839
 
        keyinfo->key_parts > file->max_key_parts() ||
4840
 
        share->uniques)
4841
 
    {
4842
 
      /* Can't create a key; Make a unique constraint instead of a key */
4843
 
      share->keys=    0;
4844
 
      share->uniques= 1;
4845
 
      using_unique_constraint=1;
4846
 
      memset(&uniquedef, 0, sizeof(uniquedef));
4847
 
      uniquedef.keysegs=keyinfo->key_parts;
4848
 
      uniquedef.seg=seg;
4849
 
      uniquedef.null_are_equal=1;
4850
 
 
4851
 
      /* Create extra column for hash value */
4852
 
      memset(*recinfo, 0, sizeof(**recinfo));
4853
 
      (*recinfo)->type= FIELD_CHECK;
4854
 
      (*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
4855
 
      (*recinfo)++;
4856
 
      share->reclength+=MI_UNIQUE_HASH_LENGTH;
4857
 
    }
4858
 
    else
4859
 
    {
4860
 
      /* Create an unique key */
4861
 
      memset(&keydef, 0, sizeof(keydef));
4862
 
      keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
4863
 
      keydef.keysegs=  keyinfo->key_parts;
4864
 
      keydef.seg= seg;
4865
 
    }
4866
 
    for (uint32_t i=0; i < keyinfo->key_parts ; i++,seg++)
4867
 
    {
4868
 
      Field *field=keyinfo->key_part[i].field;
4869
 
      seg->flag=     0;
4870
 
      seg->language= field->charset()->number;
4871
 
      seg->length=   keyinfo->key_part[i].length;
4872
 
      seg->start=    keyinfo->key_part[i].offset;
4873
 
      if (field->flags & BLOB_FLAG)
4874
 
      {
4875
 
        seg->type=
4876
 
        ((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
4877
 
         HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
4878
 
        seg->bit_start= (uint8_t)(field->pack_length() - share->blob_ptr_size);
4879
 
        seg->flag= HA_BLOB_PART;
4880
 
        seg->length=0;                  // Whole blob in unique constraint
4881
 
      }
4882
 
      else
4883
 
      {
4884
 
        seg->type= keyinfo->key_part[i].type;
4885
 
      }
4886
 
      if (!(field->flags & NOT_NULL_FLAG))
4887
 
      {
4888
 
        seg->null_bit= field->null_bit;
4889
 
        seg->null_pos= (uint) (field->null_ptr - (unsigned char*) record[0]);
4890
 
        /*
4891
 
          We are using a GROUP BY on something that contains NULL
4892
 
          In this case we have to tell MyISAM that two NULL should
4893
 
          on INSERT be regarded at the same value
4894
 
        */
4895
 
        if (!using_unique_constraint)
4896
 
          keydef.flag|= HA_NULL_ARE_EQUAL;
4897
 
      }
4898
 
    }
4899
 
  }
4900
 
  MI_CREATE_INFO create_info;
4901
 
  memset(&create_info, 0, sizeof(create_info));
4902
 
 
4903
 
  if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
4904
 
      OPTION_BIG_TABLES)
4905
 
    create_info.data_file_length= ~(uint64_t) 0;
4906
 
 
4907
 
  if ((error=mi_create(share->table_name.str, share->keys, &keydef,
4908
 
                       (uint) (*recinfo-start_recinfo),
4909
 
                       start_recinfo,
4910
 
                       share->uniques, &uniquedef,
4911
 
                       &create_info,
4912
 
                       HA_CREATE_TMP_TABLE)))
4913
 
  {
4914
 
    file->print_error(error,MYF(0));    /* purecov: inspected */
4915
 
    db_stat=0;
4916
 
    goto err;
4917
 
  }
4918
 
  status_var_increment(in_use->status_var.created_tmp_disk_tables);
4919
 
  share->db_record_offset= 1;
4920
 
  return false;
4921
 
 err:
4922
 
  return true;
4923
 
}
4924
 
 
4925
 
 
4926
 
void Table::free_tmp_table(THD *thd)
4927
 
{
4928
 
  MEM_ROOT own_root= mem_root;
4929
 
  const char *save_proc_info;
4930
 
 
4931
 
  save_proc_info=thd->get_proc_info();
4932
 
  thd->set_proc_info("removing tmp table");
4933
 
 
4934
 
  if (file)
4935
 
  {
4936
 
    if (db_stat)
4937
 
      file->ha_drop_table(s->table_name.str);
4938
 
    else
4939
 
      file->ha_delete_table(s->table_name.str);
4940
 
    delete file;
4941
 
  }
4942
 
 
4943
 
  /* free blobs */
4944
 
  for (Field **ptr= field ; *ptr ; ptr++)
4945
 
    (*ptr)->free();
4946
 
  free_io_cache(this);
4947
 
 
4948
 
  if (temp_pool_slot != MY_BIT_NONE)
4949
 
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
4950
 
 
4951
 
  plugin_unlock(0, s->db_plugin);
4952
 
 
4953
 
  free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
4954
 
  thd->set_proc_info(save_proc_info);
4955
 
 
4956
 
  return;
4957
 
}
4958
 
 
4959
 
/**
4960
 
  If a HEAP table gets full, create a MyISAM table and copy all rows
4961
 
  to this.
4962
 
*/
4963
 
 
4964
 
bool create_myisam_from_heap(THD *thd, Table *table,
4965
 
                             MI_COLUMNDEF *start_recinfo,
4966
 
                             MI_COLUMNDEF **recinfo, 
4967
 
                             int error, bool ignore_last_dupp_key_error)
4968
 
{
4969
 
  Table new_table;
4970
 
  TABLE_SHARE share;
4971
 
  const char *save_proc_info;
4972
 
  int write_err;
4973
 
 
4974
 
  if (table->s->db_type() != heap_hton || 
4975
 
      error != HA_ERR_RECORD_FILE_FULL)
4976
 
  {
4977
 
    table->file->print_error(error,MYF(0));
4978
 
    return(1);
4979
 
  }
4980
 
  new_table= *table;
4981
 
  share= *table->s;
4982
 
  new_table.s= &share;
4983
 
  new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
4984
 
  if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
4985
 
                                        new_table.s->db_type())))
4986
 
    return(1);                          // End of memory
4987
 
 
4988
 
  save_proc_info=thd->get_proc_info();
4989
 
  thd->set_proc_info("converting HEAP to MyISAM");
4990
 
 
4991
 
  if (new_table.create_myisam_tmp_table(table->key_info, start_recinfo,
4992
 
                                        recinfo, thd->lex->select_lex.options | 
4993
 
                                        thd->options))
4994
 
    goto err2;
4995
 
  if (new_table.open_tmp_table())
4996
 
    goto err1;
4997
 
  if (table->file->indexes_are_disabled())
4998
 
    new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
4999
 
  table->file->ha_index_or_rnd_end();
5000
 
  table->file->ha_rnd_init(1);
5001
 
  if (table->no_rows)
5002
 
  {
5003
 
    new_table.file->extra(HA_EXTRA_NO_ROWS);
5004
 
    new_table.no_rows=1;
5005
 
  }
5006
 
 
5007
 
#ifdef TO_BE_DONE_LATER_IN_4_1
5008
 
  /*
5009
 
    To use start_bulk_insert() (which is new in 4.1) we need to find
5010
 
    all places where a corresponding end_bulk_insert() should be put.
5011
 
  */
5012
 
  table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
5013
 
  new_table.file->ha_start_bulk_insert(table->file->stats.records);
5014
 
#else
5015
 
  /* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
5016
 
  new_table.file->extra(HA_EXTRA_WRITE_CACHE);
5017
 
#endif
5018
 
 
5019
 
  /*
5020
 
    copy all old rows from heap table to MyISAM table
5021
 
    This is the only code that uses record[1] to read/write but this
5022
 
    is safe as this is a temporary MyISAM table without timestamp/autoincrement.
5023
 
  */
5024
 
  while (!table->file->rnd_next(new_table.record[1]))
5025
 
  {
5026
 
    write_err= new_table.file->ha_write_row(new_table.record[1]);
5027
 
    if (write_err)
5028
 
      goto err;
5029
 
  }
5030
 
  /* copy row that filled HEAP table */
5031
 
  if ((write_err=new_table.file->ha_write_row(table->record[0])))
5032
 
  {
5033
 
    if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
5034
 
        !ignore_last_dupp_key_error)
5035
 
      goto err;
5036
 
  }
5037
 
 
5038
 
  /* remove heap table and change to use myisam table */
5039
 
  (void) table->file->ha_rnd_end();
5040
 
  (void) table->file->close();                  // This deletes the table !
5041
 
  delete table->file;
5042
 
  table->file=0;
5043
 
  plugin_unlock(0, table->s->db_plugin);
5044
 
  share.db_plugin= my_plugin_lock(0, &share.db_plugin);
5045
 
  new_table.s= table->s;                       // Keep old share
5046
 
  *table= new_table;
5047
 
  *table->s= share;
5048
 
  
5049
 
  table->file->change_table_ptr(table, table->s);
5050
 
  table->use_all_columns();
5051
 
  if (save_proc_info)
5052
 
  {
5053
 
    const char *new_proc_info=
5054
 
      (!strcmp(save_proc_info,"Copying to tmp table") ?
5055
 
      "Copying to tmp table on disk" : save_proc_info);
5056
 
    thd->set_proc_info(new_proc_info);
5057
 
  }
5058
 
  return(0);
5059
 
 
5060
 
 err:
5061
 
  table->file->print_error(write_err, MYF(0));
5062
 
  (void) table->file->ha_rnd_end();
5063
 
  (void) new_table.file->close();
5064
 
 err1:
5065
 
  new_table.file->ha_delete_table(new_table.s->table_name.str);
5066
 
 err2:
5067
 
  delete new_table.file;
5068
 
  thd->set_proc_info(save_proc_info);
5069
 
  table->mem_root= new_table.mem_root;
5070
 
  return(1);
5071
 
}
5072
 
 
5073
 
my_bitmap_map *Table::use_all_columns(MY_BITMAP *bitmap)
5074
 
{
5075
 
  my_bitmap_map *old= bitmap->bitmap;
5076
 
  bitmap->bitmap= s->all_set.bitmap;
5077
 
  return old;
5078
 
}
5079
 
 
5080
 
void Table::restore_column_map(my_bitmap_map *old)
5081
 
{
5082
 
  read_set->bitmap= old;
5083
 
}
5084
 
 
5085
 
uint32_t Table::find_shortest_key(const key_map *usable_keys)
5086
 
{
5087
 
  uint32_t min_length= UINT32_MAX;
5088
 
  uint32_t best= MAX_KEY;
5089
 
  if (!usable_keys->is_clear_all())
5090
 
  {
5091
 
    for (uint32_t nr=0; nr < s->keys ; nr++)
5092
 
    {
5093
 
      if (usable_keys->is_set(nr))
5094
 
      {
5095
 
        if (key_info[nr].key_length < min_length)
5096
 
        {
5097
 
          min_length= key_info[nr].key_length;
5098
 
          best=nr;
5099
 
        }
5100
 
      }
5101
 
    }
5102
 
  }
5103
 
  return best;
5104
 
}
5105
 
 
5106
 
/*****************************************************************************
5107
 
  Remove duplicates from tmp table
5108
 
  This should be recoded to add a unique index to the table and remove
5109
 
  duplicates
5110
 
  Table is a locked single thread table
5111
 
  fields is the number of fields to check (from the end)
5112
 
*****************************************************************************/
5113
 
 
5114
 
bool Table::compare_record(Field **ptr)
5115
 
{
5116
 
  for (; *ptr ; ptr++)
5117
 
  {
5118
 
    if ((*ptr)->cmp_offset(s->rec_buff_length))
5119
 
      return true;
5120
 
  }
5121
 
  return false;
5122
 
}
5123
 
 
5124
 
/* Return false if row hasn't changed */
5125
 
 
5126
 
bool Table::compare_record()
5127
 
{
5128
 
  if (s->blob_fields + s->varchar_fields == 0)
5129
 
    return cmp_record(this, record[1]);
5130
 
  /* Compare null bits */
5131
 
  if (memcmp(null_flags,
5132
 
             null_flags + s->rec_buff_length,
5133
 
             s->null_bytes))
5134
 
    return true;                                // Diff in NULL value
5135
 
  /* Compare updated fields */
5136
 
  for (Field **ptr= field ; *ptr ; ptr++)
5137
 
  {
5138
 
    if (bitmap_is_set(write_set, (*ptr)->field_index) &&
5139
 
        (*ptr)->cmp_binary_offset(s->rec_buff_length))
5140
 
      return true;
5141
 
  }
5142
 
  return false;
5143
 
}
5144
 
 
5145
 
 
5146
 
 
5147
 
 
5148
 
 
5149
 
/*****************************************************************************
5150
 
  The different ways to read a record
5151
 
  Returns -1 if row was not found, 0 if row was found and 1 on errors
5152
 
*****************************************************************************/
5153
 
 
5154
 
/** Help function when we get some an error from the table handler. */
5155
 
 
5156
 
int Table::report_error(int error)
5157
 
{
5158
 
  if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
5159
 
  {
5160
 
    status= STATUS_GARBAGE;
5161
 
    return -1;                                  // key not found; ok
5162
 
  }
5163
 
  /*
5164
 
    Locking reads can legally return also these errors, do not
5165
 
    print them to the .err log
5166
 
  */
5167
 
  if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
5168
 
    sql_print_error(_("Got error %d when reading table '%s'"),
5169
 
                    error, s->path.str);
5170
 
  file->print_error(error,MYF(0));
5171
 
 
5172
 
  return 1;
5173
 
}
5174
 
 
5175
 
 
5176
 
/*
5177
 
  Calculate data for each virtual field marked for write in the
5178
 
  corresponding column map.
5179
 
 
5180
 
  SYNOPSIS
5181
 
    update_virtual_fields_marked_for_write()
5182
 
    table                  The Table object
5183
 
    ignore_stored          Indication whether physically stored virtual
5184
 
                           fields do not need updating.
5185
 
                           This value is false when during INSERT and UPDATE
5186
 
                           and true in all other cases.
5187
 
 
5188
 
  RETURN
5189
 
    0  - Success
5190
 
    >0 - Error occurred during the generation/calculation of a virtual field value
5191
 
 
5192
 
*/
5193
 
 
5194
 
int update_virtual_fields_marked_for_write(Table *table,
5195
 
                                           bool ignore_stored)
5196
 
{
5197
 
  Field **vfield_ptr, *vfield;
5198
 
  int error= 0;
5199
 
  if ((not table) or (not table->vfield))
5200
 
    return(0);
5201
 
 
5202
 
  /* Iterate over virtual fields in the table */
5203
 
  for (vfield_ptr= table->vfield; *vfield_ptr; vfield_ptr++)
5204
 
  {
5205
 
    vfield= (*vfield_ptr);
5206
 
    assert(vfield->vcol_info && vfield->vcol_info->expr_item);
5207
 
    /*
5208
 
      Only update those fields that are marked in the write_set bitmap
5209
 
      and not _already_ physically stored in the database.
5210
 
    */
5211
 
    if (bitmap_is_set(table->write_set, vfield->field_index) &&
5212
 
        (not (ignore_stored && vfield->is_stored))
5213
 
       )
5214
 
    {
5215
 
      /* Generate the actual value of the virtual fields */
5216
 
      error= vfield->vcol_info->expr_item->save_in_field(vfield, 0);
5217
 
    }
5218
 
  }
5219
 
  return(0);
 
3684
  return(FRMTYPE_TABLE);                   // Is probably a .frm table
5220
3685
}
5221
3686
 
5222
3687