~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/filesort.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:
21
21
  Sorts a database
22
22
*/
23
23
 
24
 
#include <drizzled/server_includes.h>
 
24
#include "mysql_priv.h"
 
25
#include <m_ctype.h>
25
26
#include "sql_sort.h"
26
 
#include <drizzled/drizzled_error_messages.h>
 
27
 
 
28
/// How to write record_ref.
 
29
#define WRITE_REF(file,from) \
 
30
if (my_b_write((file),(uchar*) (from),param->ref_length)) \
 
31
  return(1);
27
32
 
28
33
        /* functions defined in this file */
29
34
 
30
 
static char **make_char_array(char **old_pos, register uint32_t fields,
31
 
                              uint32_t length, myf my_flag);
32
 
static unsigned char *read_buffpek_from_file(IO_CACHE *buffer_file, uint32_t count,
33
 
                                     unsigned char *buf);
 
35
static char **make_char_array(char **old_pos, register uint fields,
 
36
                              uint length, myf my_flag);
 
37
static uchar *read_buffpek_from_file(IO_CACHE *buffer_file, uint count,
 
38
                                     uchar *buf);
34
39
static ha_rows find_all_keys(SORTPARAM *param,SQL_SELECT *select,
35
 
                             unsigned char * *sort_keys, IO_CACHE *buffer_file,
 
40
                             uchar * *sort_keys, IO_CACHE *buffer_file,
36
41
                             IO_CACHE *tempfile,IO_CACHE *indexfile);
37
 
static int write_keys(SORTPARAM *param,unsigned char * *sort_keys,
38
 
                      uint32_t count, IO_CACHE *buffer_file, IO_CACHE *tempfile);
39
 
static void make_sortkey(SORTPARAM *param,unsigned char *to, unsigned char *ref_pos);
 
42
static int write_keys(SORTPARAM *param,uchar * *sort_keys,
 
43
                      uint count, IO_CACHE *buffer_file, IO_CACHE *tempfile);
 
44
static void make_sortkey(SORTPARAM *param,uchar *to, uchar *ref_pos);
40
45
static void register_used_fields(SORTPARAM *param);
41
 
static int merge_index(SORTPARAM *param,unsigned char *sort_buffer,
 
46
static int merge_index(SORTPARAM *param,uchar *sort_buffer,
42
47
                       BUFFPEK *buffpek,
43
 
                       uint32_t maxbuffer,IO_CACHE *tempfile,
 
48
                       uint maxbuffer,IO_CACHE *tempfile,
44
49
                       IO_CACHE *outfile);
45
 
static bool save_index(SORTPARAM *param,unsigned char **sort_keys, uint32_t count, 
46
 
                       filesort_info_st *table_sort);
47
 
static uint32_t suffix_length(uint32_t string_length);
48
 
static uint32_t sortlength(THD *thd, SORT_FIELD *sortorder, uint32_t s_length,
 
50
static bool save_index(SORTPARAM *param,uchar **sort_keys, uint count, 
 
51
                       FILESORT_INFO *table_sort);
 
52
static uint suffix_length(ulong string_length);
 
53
static uint sortlength(THD *thd, SORT_FIELD *sortorder, uint s_length,
49
54
                       bool *multi_byte_charset);
50
55
static SORT_ADDON_FIELD *get_addon_fields(THD *thd, Field **ptabfield,
51
 
                                          uint32_t sortlength, uint32_t *plength);
 
56
                                          uint sortlength, uint *plength);
52
57
static void unpack_addon_fields(struct st_sort_addon_field *addon_field,
53
 
                                unsigned char *buff);
 
58
                                uchar *buff);
54
59
/**
55
60
  Sort a table.
56
61
  Creates a set of pointers that can be used to read the rows
70
75
  @param select         condition to apply to the rows
71
76
  @param max_rows       Return only this many rows
72
77
  @param sort_positions Set to 1 if we want to force sorting by position
73
 
                        (Needed by UPDATE/INSERT or ALTER Table)
 
78
                        (Needed by UPDATE/INSERT or ALTER TABLE)
74
79
  @param examined_rows  Store number of examined rows here
75
80
 
76
81
  @todo
87
92
    examined_rows       will be set to number of examined rows
88
93
*/
89
94
 
90
 
ha_rows filesort(THD *thd, Table *table, SORT_FIELD *sortorder, uint32_t s_length,
 
95
ha_rows filesort(THD *thd, TABLE *table, SORT_FIELD *sortorder, uint s_length,
91
96
                 SQL_SELECT *select, ha_rows max_rows,
92
97
                 bool sort_positions, ha_rows *examined_rows)
93
98
{
94
99
  int error;
95
 
  uint32_t memavl, min_sort_memory;
96
 
  uint32_t maxbuffer;
 
100
  ulong memavl, min_sort_memory;
 
101
  uint maxbuffer;
97
102
  BUFFPEK *buffpek;
98
103
  ha_rows records= HA_POS_ERROR;
99
 
  unsigned char **sort_keys= 0;
 
104
  uchar **sort_keys= 0;
100
105
  IO_CACHE tempfile, buffpek_pointers, *selected_records_file, *outfile; 
101
106
  SORTPARAM param;
102
107
  bool multi_byte_charset;
103
108
 
104
 
  filesort_info_st table_sort;
105
 
  TableList *tab= table->pos_in_table_list;
 
109
  FILESORT_INFO table_sort;
 
110
  TABLE_LIST *tab= table->pos_in_table_list;
106
111
  Item_subselect *subselect= tab ? tab->containing_subselect() : 0;
107
112
 
108
 
  DRIZZLE_FILESORT_START();
 
113
  MYSQL_FILESORT_START();
109
114
 
110
115
  /*
111
116
   Release InnoDB's adaptive hash index latch (if holding) before
118
123
    QUICK_INDEX_MERGE_SELECT. Work with a copy and put it back at the end 
119
124
    when index_merge select has finished with it.
120
125
  */
121
 
  memcpy(&table_sort, &table->sort, sizeof(filesort_info_st));
 
126
  memcpy(&table_sort, &table->sort, sizeof(FILESORT_INFO));
122
127
  table->sort.io_cache= NULL;
123
128
  
124
129
  outfile= table_sort.io_cache;
126
131
  my_b_clear(&buffpek_pointers);
127
132
  buffpek=0;
128
133
  error= 1;
129
 
  memset(&param, 0, sizeof(param));
 
134
  bzero((char*) &param,sizeof(param));
130
135
  param.sort_length= sortlength(thd, sortorder, s_length, &multi_byte_charset);
131
136
  param.ref_length= table->file->ref_length;
132
137
  param.addon_field= 0;
149
154
  if (param.addon_field)
150
155
  {
151
156
    param.res_length= param.addon_length;
152
 
    if (!(table_sort.addon_buf= (unsigned char *) my_malloc(param.addon_length,
 
157
    if (!(table_sort.addon_buf= (uchar *) my_malloc(param.addon_length,
153
158
                                                    MYF(MY_WME))))
154
159
      goto err;
155
160
  }
176
181
#ifdef CAN_TRUST_RANGE
177
182
  if (select && select->quick && select->quick->records > 0L)
178
183
  {
179
 
    records=cmin((ha_rows) (select->quick->records*2+EXTRA_RECORDS*2),
 
184
    records=min((ha_rows) (select->quick->records*2+EXTRA_RECORDS*2),
180
185
                table->file->stats.records)+EXTRA_RECORDS;
181
186
    selected_records_file=0;
182
187
  }
198
203
    goto err;
199
204
 
200
205
  memavl= thd->variables.sortbuff_size;
201
 
  min_sort_memory= cmax((uint32_t)MIN_SORT_MEMORY, param.sort_length*MERGEBUFF2);
 
206
  min_sort_memory= max(MIN_SORT_MEMORY, param.sort_length*MERGEBUFF2);
202
207
  while (memavl >= min_sort_memory)
203
208
  {
204
 
    uint32_t old_memavl;
205
 
    uint32_t keys= memavl/(param.rec_length+sizeof(char*));
206
 
    param.keys=(uint32_t) cmin(records+1, keys);
 
209
    ulong old_memavl;
 
210
    ulong keys= memavl/(param.rec_length+sizeof(char*));
 
211
    param.keys=(uint) min(records+1, keys);
207
212
    if ((table_sort.sort_keys=
208
 
         (unsigned char **) make_char_array((char **) table_sort.sort_keys,
 
213
         (uchar **) make_char_array((char **) table_sort.sort_keys,
209
214
                                    param.keys, param.rec_length, MYF(0))))
210
215
      break;
211
216
    old_memavl=memavl;
229
234
                             &tempfile, selected_records_file)) ==
230
235
      HA_POS_ERROR)
231
236
    goto err;
232
 
  maxbuffer= (uint32_t) (my_b_tell(&buffpek_pointers)/sizeof(*buffpek));
 
237
  maxbuffer= (uint) (my_b_tell(&buffpek_pointers)/sizeof(*buffpek));
233
238
 
234
239
  if (maxbuffer == 0)                   // The whole set is in memory
235
240
  {
236
 
    if (save_index(&param,sort_keys,(uint32_t) records, &table_sort))
 
241
    if (save_index(&param,sort_keys,(uint) records, &table_sort))
237
242
      goto err;
238
243
  }
239
244
  else
240
245
  {
241
246
    if (table_sort.buffpek && table_sort.buffpek_len < maxbuffer)
242
247
    {
243
 
      if (table_sort.buffpek)
244
 
        free(table_sort.buffpek);
 
248
      x_free(table_sort.buffpek);
245
249
      table_sort.buffpek= 0;
246
250
    }
247
251
    if (!(table_sort.buffpek=
248
 
          (unsigned char *) read_buffpek_from_file(&buffpek_pointers, maxbuffer,
 
252
          (uchar *) read_buffpek_from_file(&buffpek_pointers, maxbuffer,
249
253
                                 table_sort.buffpek)))
250
254
      goto err;
251
255
    buffpek= (BUFFPEK *) table_sort.buffpek;
266
270
    param.keys=((param.keys*(param.rec_length+sizeof(char*))) /
267
271
                param.rec_length-1);
268
272
    maxbuffer--;                                // Offset from 0
269
 
    if (merge_many_buff(&param,(unsigned char*) sort_keys,buffpek,&maxbuffer,
 
273
    if (merge_many_buff(&param,(uchar*) sort_keys,buffpek,&maxbuffer,
270
274
                        &tempfile))
271
275
      goto err;
272
276
    if (flush_io_cache(&tempfile) ||
273
277
        reinit_io_cache(&tempfile,READ_CACHE,0L,0,0))
274
278
      goto err;
275
 
    if (merge_index(&param,(unsigned char*) sort_keys,buffpek,maxbuffer,&tempfile,
 
279
    if (merge_index(&param,(uchar*) sort_keys,buffpek,maxbuffer,&tempfile,
276
280
                    outfile))
277
281
      goto err;
278
282
  }
282
286
 
283
287
 err:
284
288
  if (param.tmp_buffer)
285
 
    if (param.tmp_buffer)
286
 
      free(param.tmp_buffer);
 
289
    x_free(param.tmp_buffer);
287
290
  if (!subselect || !subselect->is_uncacheable())
288
291
  {
289
 
    if ((unsigned char*) sort_keys)
290
 
      free((unsigned char*) sort_keys);
 
292
    x_free((uchar*) sort_keys);
291
293
    table_sort.sort_keys= 0;
292
 
    if ((unsigned char*) buffpek)
293
 
      free((unsigned char*) buffpek);
 
294
    x_free((uchar*) buffpek);
294
295
    table_sort.buffpek= 0;
295
296
    table_sort.buffpek_len= 0;
296
297
  }
313
314
               MYF(ME_ERROR+ME_WAITTANG));
314
315
  else
315
316
    statistic_add(thd->status_var.filesort_rows,
316
 
                  (uint32_t) records, &LOCK_status);
 
317
                  (ulong) records, &LOCK_status);
317
318
  *examined_rows= param.examined_rows;
318
 
  memcpy(&table->sort, &table_sort, sizeof(filesort_info_st));
319
 
  DRIZZLE_FILESORT_END();
 
319
  memcpy(&table->sort, &table_sort, sizeof(FILESORT_INFO));
 
320
  MYSQL_FILESORT_END();
320
321
  return(error ? HA_POS_ERROR : records);
321
322
} /* filesort */
322
323
 
323
324
 
324
 
void filesort_free_buffers(Table *table, bool full)
 
325
void filesort_free_buffers(TABLE *table, bool full)
325
326
{
326
327
  if (table->sort.record_pointers)
327
328
  {
328
 
    free((unsigned char*) table->sort.record_pointers);
 
329
    my_free((uchar*) table->sort.record_pointers,MYF(0));
329
330
    table->sort.record_pointers=0;
330
331
  }
331
332
  if (full)
332
333
  {
333
334
    if (table->sort.sort_keys )
334
335
    {
335
 
      if ((unsigned char*) table->sort.sort_keys)
336
 
        free((unsigned char*) table->sort.sort_keys);
 
336
      x_free((uchar*) table->sort.sort_keys);
337
337
      table->sort.sort_keys= 0;
338
338
    }
339
339
    if (table->sort.buffpek)
340
340
    {
341
 
      if ((unsigned char*) table->sort.buffpek)
342
 
        free((unsigned char*) table->sort.buffpek);
 
341
      x_free((uchar*) table->sort.buffpek);
343
342
      table->sort.buffpek= 0;
344
343
      table->sort.buffpek_len= 0;
345
344
    }
346
345
  }
347
346
  if (table->sort.addon_buf)
348
347
  {
349
 
    free((char *) table->sort.addon_buf);
350
 
    free((char *) table->sort.addon_field);
 
348
    my_free((char *) table->sort.addon_buf, MYF(0));
 
349
    my_free((char *) table->sort.addon_field, MYF(MY_ALLOW_ZERO_PTR));
351
350
    table->sort.addon_buf=0;
352
351
    table->sort.addon_field=0;
353
352
  }
355
354
 
356
355
/** Make a array of string pointers. */
357
356
 
358
 
static char **make_char_array(char **old_pos, register uint32_t fields,
359
 
                              uint32_t length, myf my_flag)
 
357
static char **make_char_array(char **old_pos, register uint fields,
 
358
                              uint length, myf my_flag)
360
359
{
361
360
  register char **pos;
362
361
  char *char_pos;
363
362
 
364
363
  if (old_pos ||
365
 
      (old_pos= (char**) my_malloc((uint32_t) fields*(length+sizeof(char*)),
 
364
      (old_pos= (char**) my_malloc((uint) fields*(length+sizeof(char*)),
366
365
                                   my_flag)))
367
366
  {
368
367
    pos=old_pos; char_pos=((char*) (pos+fields)) -length;
375
374
 
376
375
/** Read 'count' number of buffer pointers into memory. */
377
376
 
378
 
static unsigned char *read_buffpek_from_file(IO_CACHE *buffpek_pointers, uint32_t count,
379
 
                                     unsigned char *buf)
 
377
static uchar *read_buffpek_from_file(IO_CACHE *buffpek_pointers, uint count,
 
378
                                     uchar *buf)
380
379
{
381
 
  uint32_t length= sizeof(BUFFPEK)*count;
382
 
  unsigned char *tmp= buf;
 
380
  ulong length= sizeof(BUFFPEK)*count;
 
381
  uchar *tmp= buf;
383
382
  if (count > UINT_MAX/sizeof(BUFFPEK))
384
383
    return 0; /* sizeof(BUFFPEK)*count will overflow */
385
384
  if (!tmp)
386
 
    tmp= (unsigned char *)my_malloc(length, MYF(MY_WME));
 
385
    tmp= (uchar *)my_malloc(length, MYF(MY_WME));
387
386
  if (tmp)
388
387
  {
389
388
    if (reinit_io_cache(buffpek_pointers,READ_CACHE,0L,0,0) ||
390
 
        my_b_read(buffpek_pointers, (unsigned char*) tmp, length))
 
389
        my_b_read(buffpek_pointers, (uchar*) tmp, length))
391
390
    {
392
 
      free((char*) tmp);
 
391
      my_free((char*) tmp, MYF(0));
393
392
      tmp=0;
394
393
    }
395
394
  }
435
434
*/
436
435
 
437
436
static ha_rows find_all_keys(SORTPARAM *param, SQL_SELECT *select,
438
 
                             unsigned char **sort_keys,
 
437
                             uchar **sort_keys,
439
438
                             IO_CACHE *buffpek_pointers,
440
439
                             IO_CACHE *tempfile, IO_CACHE *indexfile)
441
440
{
442
441
  int error,flag,quick_select;
443
 
  uint32_t idx,indexpos,ref_length;
444
 
  unsigned char *ref_pos,*next_pos,ref_buff[MAX_REFLENGTH];
 
442
  uint idx,indexpos,ref_length;
 
443
  uchar *ref_pos,*next_pos,ref_buff[MAX_REFLENGTH];
445
444
  my_off_t record;
446
 
  Table *sort_form;
 
445
  TABLE *sort_form;
447
446
  THD *thd= current_thd;
448
447
  volatile THD::killed_state *killed= &thd->killed;
449
448
  handler *file;
464
463
  next_pos=ref_pos;
465
464
  if (! indexfile && ! quick_select)
466
465
  {
467
 
    next_pos=(unsigned char*) 0;                        /* Find records in sequence */
 
466
    next_pos=(uchar*) 0;                        /* Find records in sequence */
468
467
    file->ha_rnd_init(1);
469
468
    file->extra_opt(HA_EXTRA_CACHE,
470
469
                    current_thd->variables.read_buff_size);
489
488
  register_used_fields(param);
490
489
  if (select && select->cond)
491
490
    select->cond->walk(&Item::register_field_in_read_map, 1,
492
 
                       (unsigned char*) sort_form);
 
491
                       (uchar*) sort_form);
493
492
  sort_form->column_bitmaps_set(&sort_form->tmp_set, &sort_form->tmp_set);
494
493
 
495
494
  for (;;)
507
506
    {
508
507
      if (indexfile)
509
508
      {
510
 
        if (my_b_read(indexfile,(unsigned char*) ref_pos,ref_length)) /* purecov: deadcode */
 
509
        if (my_b_read(indexfile,(uchar*) ref_pos,ref_length)) /* purecov: deadcode */
511
510
        {
512
511
          error= my_errno ? my_errno : -1;              /* Abort */
513
512
          break;
517
516
      else
518
517
      {
519
518
        error=file->rnd_next(sort_form->record[0]);
520
 
        if (!error)
521
 
          update_virtual_fields_marked_for_write(sort_form);
522
 
 
523
519
        if (!flag)
524
520
        {
525
521
          my_store_ptr(ref_pos,ref_length,record); // Position to row
618
614
*/
619
615
 
620
616
static int
621
 
write_keys(SORTPARAM *param, register unsigned char **sort_keys, uint32_t count,
 
617
write_keys(SORTPARAM *param, register uchar **sort_keys, uint count,
622
618
           IO_CACHE *buffpek_pointers, IO_CACHE *tempfile)
623
619
{
624
620
  size_t sort_length, rec_length;
625
 
  unsigned char **end;
 
621
  uchar **end;
626
622
  BUFFPEK buffpek;
627
623
 
628
624
  sort_length= param->sort_length;
629
625
  rec_length= param->rec_length;
630
 
  my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, sort_length);
 
626
  my_string_ptr_sort((uchar*) sort_keys, (uint) count, sort_length);
631
627
  if (!my_b_inited(tempfile) &&
632
628
      open_cached_file(tempfile, mysql_tmpdir, TEMP_PREFIX, DISK_BUFFER_SIZE,
633
629
                       MYF(MY_WME)))
637
633
    goto err;
638
634
  buffpek.file_pos= my_b_tell(tempfile);
639
635
  if ((ha_rows) count > param->max_rows)
640
 
    count=(uint32_t) param->max_rows;               /* purecov: inspected */
 
636
    count=(uint) param->max_rows;               /* purecov: inspected */
641
637
  buffpek.count=(ha_rows) count;
642
638
  for (end=sort_keys+count ; sort_keys != end ; sort_keys++)
643
 
    if (my_b_write(tempfile, (unsigned char*) *sort_keys, (uint32_t) rec_length))
 
639
    if (my_b_write(tempfile, (uchar*) *sort_keys, (uint) rec_length))
644
640
      goto err;
645
 
  if (my_b_write(buffpek_pointers, (unsigned char*) &buffpek, sizeof(buffpek)))
 
641
  if (my_b_write(buffpek_pointers, (uchar*) &buffpek, sizeof(buffpek)))
646
642
    goto err;
647
643
  return(0);
648
644
 
655
651
  Store length as suffix in high-byte-first order.
656
652
*/
657
653
 
658
 
static inline void store_length(unsigned char *to, uint32_t length, uint32_t pack_length)
 
654
static inline void store_length(uchar *to, uint length, uint pack_length)
659
655
{
660
656
  switch (pack_length) {
661
657
  case 1:
662
 
    *to= (unsigned char) length;
 
658
    *to= (uchar) length;
663
659
    break;
664
660
  case 2:
665
661
    mi_int2store(to, length);
677
673
/** Make a sort-key from record. */
678
674
 
679
675
static void make_sortkey(register SORTPARAM *param,
680
 
                         register unsigned char *to, unsigned char *ref_pos)
 
676
                         register uchar *to, uchar *ref_pos)
681
677
{
682
678
  register Field *field;
683
679
  register SORT_FIELD *sort_field;
684
 
  register uint32_t length;
 
680
  register uint length;
685
681
 
686
682
  for (sort_field=param->local_sortorder ;
687
683
       sort_field != param->end ;
695
691
        if (field->is_null())
696
692
        {
697
693
          if (sort_field->reverse)
698
 
            memset(to, 255, sort_field->length+1);
 
694
            bfill(to,sort_field->length+1,(char) 255);
699
695
          else
700
 
            memset(to, 0, sort_field->length+1);
 
696
            bzero((char*) to,sort_field->length+1);
701
697
          to+= sort_field->length+1;
702
698
          continue;
703
699
        }
713
709
      switch (sort_field->result_type) {
714
710
      case STRING_RESULT:
715
711
      {
716
 
        const CHARSET_INFO * const cs=item->collation.collation;
 
712
        CHARSET_INFO *cs=item->collation.collation;
717
713
        char fill_char= ((cs->state & MY_CS_BINSORT) ? (char) 0 : ' ');
718
714
        int diff;
719
 
        uint32_t sort_field_length;
 
715
        uint sort_field_length;
720
716
 
721
717
        if (maybe_null)
722
718
          *to++=1;
726
722
        if (!res)
727
723
        {
728
724
          if (maybe_null)
729
 
            memset(to-1, 0, sort_field->length+1);
 
725
            bzero((char*) to-1,sort_field->length+1);
730
726
          else
731
727
          {
732
728
            /* purecov: begin deadcode */
736
732
              This code is here mainly to avoid a hard crash in this case.
737
733
            */
738
734
            assert(0);
739
 
            memset(to, 0, sort_field->length);  // Avoid crash
 
735
            bzero((char*) to,sort_field->length);       // Avoid crash
740
736
            /* purecov: end */
741
737
          }
742
738
          break;
758
754
        if (sort_field->need_strxnfrm)
759
755
        {
760
756
          char *from=(char*) res->ptr();
761
 
          uint32_t tmp_length;
762
 
          if ((unsigned char*) from == to)
 
757
          uint tmp_length;
 
758
          if ((uchar*) from == to)
763
759
          {
764
760
            set_if_smaller(length,sort_field->length);
765
761
            memcpy(param->tmp_buffer,from,length);
766
762
            from=param->tmp_buffer;
767
763
          }
768
764
          tmp_length= my_strnxfrm(cs,to,sort_field->length,
769
 
                                  (unsigned char*) from, length);
 
765
                                  (uchar*) from, length);
770
766
          assert(tmp_length == sort_field->length);
771
767
        }
772
768
        else
773
769
        {
774
 
          my_strnxfrm(cs,(unsigned char*)to,length,(const unsigned char*)res->ptr(),length);
 
770
          my_strnxfrm(cs,(uchar*)to,length,(const uchar*)res->ptr(),length);
775
771
          cs->cset->fill(cs, (char *)to+length,diff,fill_char);
776
772
        }
777
773
        break;
785
781
            if (item->null_value)
786
782
            {
787
783
              if (maybe_null)
788
 
                memset(to-1, 0, sort_field->length+1);
 
784
                bzero((char*) to-1,sort_field->length+1);
789
785
              else
790
786
              {
791
 
                memset(to, 0, sort_field->length);
 
787
                bzero((char*) to,sort_field->length);
792
788
              }
793
789
              break;
794
790
            }
795
791
          }
796
 
          to[7]= (unsigned char) value;
797
 
          to[6]= (unsigned char) (value >> 8);
798
 
          to[5]= (unsigned char) (value >> 16);
799
 
          to[4]= (unsigned char) (value >> 24);
800
 
          to[3]= (unsigned char) (value >> 32);
801
 
          to[2]= (unsigned char) (value >> 40);
802
 
          to[1]= (unsigned char) (value >> 48);
803
 
          if (item->unsigned_flag)                    /* Fix sign */
804
 
            to[0]= (unsigned char) (value >> 56);
805
 
          else
806
 
            to[0]= (unsigned char) (value >> 56) ^ 128; /* Reverse signbit */
 
792
#if SIZEOF_LONG_LONG > 4
 
793
          to[7]= (uchar) value;
 
794
          to[6]= (uchar) (value >> 8);
 
795
          to[5]= (uchar) (value >> 16);
 
796
          to[4]= (uchar) (value >> 24);
 
797
          to[3]= (uchar) (value >> 32);
 
798
          to[2]= (uchar) (value >> 40);
 
799
          to[1]= (uchar) (value >> 48);
 
800
          if (item->unsigned_flag)                    /* Fix sign */
 
801
            to[0]= (uchar) (value >> 56);
 
802
          else
 
803
            to[0]= (uchar) (value >> 56) ^ 128; /* Reverse signbit */
 
804
#else
 
805
          to[3]= (uchar) value;
 
806
          to[2]= (uchar) (value >> 8);
 
807
          to[1]= (uchar) (value >> 16);
 
808
          if (item->unsigned_flag)                    /* Fix sign */
 
809
            to[0]= (uchar) (value >> 24);
 
810
          else
 
811
            to[0]= (uchar) (value >> 24) ^ 128; /* Reverse signbit */
 
812
#endif
807
813
          break;
808
814
        }
809
815
      case DECIMAL_RESULT:
813
819
          {
814
820
            if (item->null_value)
815
821
            { 
816
 
              memset(to, 0, sort_field->length+1);
 
822
              bzero((char*)to, sort_field->length+1);
817
823
              to++;
818
824
              break;
819
825
            }
831
837
          {
832
838
            if (item->null_value)
833
839
            {
834
 
              memset(to, 0, sort_field->length+1);
 
840
              bzero((char*) to,sort_field->length+1);
835
841
              to++;
836
842
              break;
837
843
            }
838
844
            *to++=1;
839
845
          }
840
 
          change_double_for_sort(value,(unsigned char*) to);
 
846
          change_double_for_sort(value,(uchar*) to);
841
847
          break;
842
848
        }
843
849
      case ROW_RESULT:
854
860
      length=sort_field->length;
855
861
      while (length--)
856
862
      {
857
 
        *to = (unsigned char) (~ *to);
 
863
        *to = (uchar) (~ *to);
858
864
        to++;
859
865
      }
860
866
    }
871
877
      the same for all records.
872
878
    */
873
879
    SORT_ADDON_FIELD *addonf= param->addon_field;
874
 
    unsigned char *nulls= to;
 
880
    uchar *nulls= to;
875
881
    assert(addonf != 0);
876
 
    memset(nulls, 0, addonf->offset);
 
882
    bzero((char *) nulls, addonf->offset);
877
883
    to+= addonf->offset;
878
884
    for ( ; (field= addonf->field) ; addonf++)
879
885
    {
881
887
      {
882
888
        nulls[addonf->null_offset]|= addonf->null_bit;
883
889
#ifdef HAVE_purify
884
 
        memset(to, 0, addonf->length);
 
890
        bzero(to, addonf->length);
885
891
#endif
886
892
      }
887
893
      else
888
894
      {
889
895
#ifdef HAVE_purify
890
 
        unsigned char *end= field->pack(to, field->ptr);
891
 
        uint32_t length= (uint32_t) ((to + addonf->length) - end);
 
896
        uchar *end= field->pack(to, field->ptr);
 
897
        uint length= (uint) ((to + addonf->length) - end);
892
898
        assert((int) length >= 0);
893
899
        if (length)
894
 
          memset(end, 0, length);
 
900
          bzero(end, length);
895
901
#else
896
902
        (void) field->pack(to, field->ptr);
897
903
#endif
902
908
  else
903
909
  {
904
910
    /* Save filepos last */
905
 
    memcpy(to, ref_pos, (size_t) param->ref_length);
 
911
    memcpy((uchar*) to, ref_pos, (size_t) param->ref_length);
906
912
  }
907
913
  return;
908
914
}
915
921
static void register_used_fields(SORTPARAM *param)
916
922
{
917
923
  register SORT_FIELD *sort_field;
918
 
  Table *table=param->sort_form;
 
924
  TABLE *table=param->sort_form;
919
925
  MY_BITMAP *bitmap= table->read_set;
920
926
 
921
927
  for (sort_field= param->local_sortorder ;
931
937
    else
932
938
    {                                           // Item
933
939
      sort_field->item->walk(&Item::register_field_in_read_map, 1,
934
 
                             (unsigned char *) table);
 
940
                             (uchar *) table);
935
941
    }
936
942
  }
937
943
 
950
956
}
951
957
 
952
958
 
953
 
static bool save_index(SORTPARAM *param, unsigned char **sort_keys, uint32_t count, 
954
 
                       filesort_info_st *table_sort)
 
959
static bool save_index(SORTPARAM *param, uchar **sort_keys, uint count, 
 
960
                       FILESORT_INFO *table_sort)
955
961
{
956
 
  uint32_t offset,res_length;
957
 
  unsigned char *to;
 
962
  uint offset,res_length;
 
963
  uchar *to;
958
964
 
959
 
  my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, param->sort_length);
 
965
  my_string_ptr_sort((uchar*) sort_keys, (uint) count, param->sort_length);
960
966
  res_length= param->res_length;
961
967
  offset= param->rec_length-res_length;
962
968
  if ((ha_rows) count > param->max_rows)
963
 
    count=(uint32_t) param->max_rows;
 
969
    count=(uint) param->max_rows;
964
970
  if (!(to= table_sort->record_pointers= 
965
 
        (unsigned char*) my_malloc(res_length*count, MYF(MY_WME))))
 
971
        (uchar*) my_malloc(res_length*count, MYF(MY_WME))))
966
972
    return(1);                 /* purecov: inspected */
967
 
  for (unsigned char **end= sort_keys+count ; sort_keys != end ; sort_keys++)
 
973
  for (uchar **end= sort_keys+count ; sort_keys != end ; sort_keys++)
968
974
  {
969
975
    memcpy(to, *sort_keys+offset, res_length);
970
976
    to+= res_length;
975
981
 
976
982
/** Merge buffers to make < MERGEBUFF2 buffers. */
977
983
 
978
 
int merge_many_buff(SORTPARAM *param, unsigned char *sort_buffer,
979
 
                    BUFFPEK *buffpek, uint32_t *maxbuffer, IO_CACHE *t_file)
 
984
int merge_many_buff(SORTPARAM *param, uchar *sort_buffer,
 
985
                    BUFFPEK *buffpek, uint *maxbuffer, IO_CACHE *t_file)
980
986
{
981
 
  register uint32_t i;
 
987
  register uint i;
982
988
  IO_CACHE t_file2,*from_file,*to_file,*temp;
983
989
  BUFFPEK *lastbuff;
984
990
 
1011
1017
    temp=from_file; from_file=to_file; to_file=temp;
1012
1018
    setup_io_cache(from_file);
1013
1019
    setup_io_cache(to_file);
1014
 
    *maxbuffer= (uint32_t) (lastbuff-buffpek)-1;
 
1020
    *maxbuffer= (uint) (lastbuff-buffpek)-1;
1015
1021
  }
1016
1022
cleanup:
1017
1023
  close_cached_file(to_file);                   // This holds old result
1029
1035
  Read data to buffer.
1030
1036
 
1031
1037
  @retval
1032
 
    (uint32_t)-1 if something goes wrong
 
1038
    (uint)-1 if something goes wrong
1033
1039
*/
1034
1040
 
1035
 
uint32_t read_to_buffer(IO_CACHE *fromfile, BUFFPEK *buffpek,
1036
 
                    uint32_t rec_length)
 
1041
uint read_to_buffer(IO_CACHE *fromfile, BUFFPEK *buffpek,
 
1042
                    uint rec_length)
1037
1043
{
1038
 
  register uint32_t count;
1039
 
  uint32_t length;
 
1044
  register uint count;
 
1045
  uint length;
1040
1046
 
1041
 
  if ((count=(uint32_t) cmin((ha_rows) buffpek->max_keys,buffpek->count)))
 
1047
  if ((count=(uint) min((ha_rows) buffpek->max_keys,buffpek->count)))
1042
1048
  {
1043
 
    if (pread(fromfile->file,(unsigned char*) buffpek->base, (length= rec_length*count),buffpek->file_pos) == 0)
1044
 
      return((uint32_t) -1);                    /* purecov: inspected */
 
1049
    if (pread(fromfile->file,(uchar*) buffpek->base, (length= rec_length*count),buffpek->file_pos) == 0)
 
1050
      return((uint) -1);                        /* purecov: inspected */
1045
1051
    buffpek->key=buffpek->base;
1046
1052
    buffpek->file_pos+= length;                 /* New filepos */
1047
1053
    buffpek->count-=    count;
1062
1068
  @param[in] key_length key length
1063
1069
*/
1064
1070
 
1065
 
void reuse_freed_buff(QUEUE *queue, BUFFPEK *reuse, uint32_t key_length)
 
1071
void reuse_freed_buff(QUEUE *queue, BUFFPEK *reuse, uint key_length)
1066
1072
{
1067
 
  unsigned char *reuse_end= reuse->base + reuse->max_keys * key_length;
1068
 
  for (uint32_t i= 0; i < queue->elements; ++i)
 
1073
  uchar *reuse_end= reuse->base + reuse->max_keys * key_length;
 
1074
  for (uint i= 0; i < queue->elements; ++i)
1069
1075
  {
1070
1076
    BUFFPEK *bp= (BUFFPEK *) queue_element(queue, i);
1071
1077
    if (bp->base + bp->max_keys * key_length == reuse->base)
1103
1109
*/
1104
1110
 
1105
1111
int merge_buffers(SORTPARAM *param, IO_CACHE *from_file,
1106
 
                  IO_CACHE *to_file, unsigned char *sort_buffer,
 
1112
                  IO_CACHE *to_file, uchar *sort_buffer,
1107
1113
                  BUFFPEK *lastbuff, BUFFPEK *Fb, BUFFPEK *Tb,
1108
1114
                  int flag)
1109
1115
{
1110
1116
  int error;
1111
 
  uint32_t rec_length,res_length,offset;
 
1117
  uint rec_length,res_length,offset;
1112
1118
  size_t sort_length;
1113
 
  uint32_t maxcount;
 
1119
  ulong maxcount;
1114
1120
  ha_rows max_rows,org_max_rows;
1115
1121
  my_off_t to_start_filepos;
1116
 
  unsigned char *strpos;
 
1122
  uchar *strpos;
1117
1123
  BUFFPEK *buffpek;
1118
1124
  QUEUE queue;
1119
1125
  qsort2_cmp cmp;
1133
1139
  res_length= param->res_length;
1134
1140
  sort_length= param->sort_length;
1135
1141
  offset= rec_length-res_length;
1136
 
  maxcount= (uint32_t) (param->keys/((uint32_t) (Tb-Fb) +1));
 
1142
  maxcount= (ulong) (param->keys/((uint) (Tb-Fb) +1));
1137
1143
  to_start_filepos= my_b_tell(to_file);
1138
 
  strpos= (unsigned char*) sort_buffer;
 
1144
  strpos= (uchar*) sort_buffer;
1139
1145
  org_max_rows=max_rows= param->max_rows;
1140
1146
 
1141
1147
  /* The following will fire if there is not enough space in sort_buffer */
1151
1157
    cmp= get_ptr_compare(sort_length);
1152
1158
    first_cmp_arg= (void*) &sort_length;
1153
1159
  }
1154
 
  if (init_queue(&queue, (uint32_t) (Tb-Fb)+1, offsetof(BUFFPEK,key), 0,
 
1160
  if (init_queue(&queue, (uint) (Tb-Fb)+1, offsetof(BUFFPEK,key), 0,
1155
1161
                 (queue_compare) cmp, first_cmp_arg))
1156
1162
    return(1);                                /* purecov: inspected */
1157
1163
  for (buffpek= Fb ; buffpek <= Tb ; buffpek++)
1158
1164
  {
1159
1165
    buffpek->base= strpos;
1160
1166
    buffpek->max_keys= maxcount;
1161
 
    strpos+= (uint32_t) (error= (int) read_to_buffer(from_file, buffpek,
 
1167
    strpos+= (uint) (error= (int) read_to_buffer(from_file, buffpek,
1162
1168
                                                                         rec_length));
1163
1169
    if (error == -1)
1164
1170
      goto err;                                 /* purecov: inspected */
1165
1171
    buffpek->max_keys= buffpek->mem_count;      // If less data in buffers than expected
1166
 
    queue_insert(&queue, (unsigned char*) buffpek);
 
1172
    queue_insert(&queue, (uchar*) buffpek);
1167
1173
  }
1168
1174
 
1169
1175
  if (param->unique_buff)
1178
1184
    */
1179
1185
    buffpek= (BUFFPEK*) queue_top(&queue);
1180
1186
    memcpy(param->unique_buff, buffpek->key, rec_length);
1181
 
    if (my_b_write(to_file, (unsigned char*) buffpek->key, rec_length))
 
1187
    if (my_b_write(to_file, (uchar*) buffpek->key, rec_length))
1182
1188
    {
1183
1189
      error=1; goto err;                        /* purecov: inspected */
1184
1190
    }
1206
1212
      if (cmp)                                        // Remove duplicates
1207
1213
      {
1208
1214
        if (!(*cmp)(first_cmp_arg, &(param->unique_buff),
1209
 
                    (unsigned char**) &buffpek->key))
 
1215
                    (uchar**) &buffpek->key))
1210
1216
              goto skip_duplicate;
1211
 
            memcpy(param->unique_buff, buffpek->key, rec_length);
 
1217
            memcpy(param->unique_buff, (uchar*) buffpek->key, rec_length);
1212
1218
      }
1213
1219
      if (flag == 0)
1214
1220
      {
1215
 
        if (my_b_write(to_file,(unsigned char*) buffpek->key, rec_length))
 
1221
        if (my_b_write(to_file,(uchar*) buffpek->key, rec_length))
1216
1222
        {
1217
1223
          error=1; goto err;                        /* purecov: inspected */
1218
1224
        }
1219
1225
      }
1220
1226
      else
1221
1227
      {
1222
 
        if (my_b_write(to_file, (unsigned char*) buffpek->key+offset, res_length))
 
1228
        if (my_b_write(to_file, (uchar*) buffpek->key+offset, res_length))
1223
1229
        {
1224
1230
          error=1; goto err;                        /* purecov: inspected */
1225
1231
        }
1237
1243
        if (!(error= (int) read_to_buffer(from_file,buffpek,
1238
1244
                                          rec_length)))
1239
1245
        {
1240
 
          queue_remove(&queue,0);
 
1246
          VOID(queue_remove(&queue,0));
1241
1247
          reuse_freed_buff(&queue, buffpek, rec_length);
1242
1248
          break;                        /* One buffer have been removed */
1243
1249
        }
1257
1263
  */
1258
1264
  if (cmp)
1259
1265
  {
1260
 
    if (!(*cmp)(first_cmp_arg, &(param->unique_buff), (unsigned char**) &buffpek->key))
 
1266
    if (!(*cmp)(first_cmp_arg, &(param->unique_buff), (uchar**) &buffpek->key))
1261
1267
    {
1262
1268
      buffpek->key+= rec_length;         // Remove duplicate
1263
1269
      --buffpek->mem_count;
1268
1274
  {
1269
1275
    if ((ha_rows) buffpek->mem_count > max_rows)
1270
1276
    {                                        /* Don't write too many records */
1271
 
      buffpek->mem_count= (uint32_t) max_rows;
 
1277
      buffpek->mem_count= (uint) max_rows;
1272
1278
      buffpek->count= 0;                        /* Don't read more */
1273
1279
    }
1274
1280
    max_rows-= buffpek->mem_count;
1275
1281
    if (flag == 0)
1276
1282
    {
1277
 
      if (my_b_write(to_file,(unsigned char*) buffpek->key,
 
1283
      if (my_b_write(to_file,(uchar*) buffpek->key,
1278
1284
                     (rec_length*buffpek->mem_count)))
1279
1285
      {
1280
1286
        error= 1; goto err;                        /* purecov: inspected */
1282
1288
    }
1283
1289
    else
1284
1290
    {
1285
 
      register unsigned char *end;
 
1291
      register uchar *end;
1286
1292
      strpos= buffpek->key+offset;
1287
1293
      for (end= strpos+buffpek->mem_count*rec_length ;
1288
1294
           strpos != end ;
1289
1295
           strpos+= rec_length)
1290
1296
      {     
1291
 
        if (my_b_write(to_file, (unsigned char *) strpos, res_length))
 
1297
        if (my_b_write(to_file, (uchar *) strpos, res_length))
1292
1298
        {
1293
1299
          error=1; goto err;                        
1294
1300
        }
1299
1305
         != -1 && error != 0);
1300
1306
 
1301
1307
end:
1302
 
  lastbuff->count= cmin(org_max_rows-max_rows, param->max_rows);
 
1308
  lastbuff->count= min(org_max_rows-max_rows, param->max_rows);
1303
1309
  lastbuff->file_pos= to_start_filepos;
1304
1310
err:
1305
1311
  delete_queue(&queue);
1309
1315
 
1310
1316
        /* Do a merge to output-file (save only positions) */
1311
1317
 
1312
 
static int merge_index(SORTPARAM *param, unsigned char *sort_buffer,
1313
 
                       BUFFPEK *buffpek, uint32_t maxbuffer,
 
1318
static int merge_index(SORTPARAM *param, uchar *sort_buffer,
 
1319
                       BUFFPEK *buffpek, uint maxbuffer,
1314
1320
                       IO_CACHE *tempfile, IO_CACHE *outfile)
1315
1321
{
1316
1322
  if (merge_buffers(param,tempfile,outfile,sort_buffer,buffpek,buffpek,
1320
1326
} /* merge_index */
1321
1327
 
1322
1328
 
1323
 
static uint32_t suffix_length(uint32_t string_length)
 
1329
static uint suffix_length(ulong string_length)
1324
1330
{
1325
1331
  if (string_length < 256)
1326
1332
    return 1;
1351
1357
    Total length of sort buffer in bytes
1352
1358
*/
1353
1359
 
1354
 
static uint32_t
1355
 
sortlength(THD *thd, SORT_FIELD *sortorder, uint32_t s_length,
 
1360
static uint
 
1361
sortlength(THD *thd, SORT_FIELD *sortorder, uint s_length,
1356
1362
           bool *multi_byte_charset)
1357
1363
{
1358
 
  register uint32_t length;
1359
 
  const CHARSET_INFO *cs;
 
1364
  register uint length;
 
1365
  CHARSET_INFO *cs;
1360
1366
  *multi_byte_charset= 0;
1361
1367
 
1362
1368
  length=0;
1401
1407
        }
1402
1408
        break;
1403
1409
      case INT_RESULT:
 
1410
#if SIZEOF_LONG_LONG > 4
1404
1411
        sortorder->length=8;                    // Size of intern int64_t
 
1412
#else
 
1413
        sortorder->length=4;
 
1414
#endif
1405
1415
        break;
1406
1416
      case DECIMAL_RESULT:
1407
1417
        sortorder->length=
1457
1467
*/
1458
1468
 
1459
1469
static SORT_ADDON_FIELD *
1460
 
get_addon_fields(THD *thd, Field **ptabfield, uint32_t sortlength, uint32_t *plength)
 
1470
get_addon_fields(THD *thd, Field **ptabfield, uint sortlength, uint *plength)
1461
1471
{
1462
1472
  Field **pfield;
1463
1473
  Field *field;
1464
1474
  SORT_ADDON_FIELD *addonf;
1465
 
  uint32_t length= 0;
1466
 
  uint32_t fields= 0;
1467
 
  uint32_t null_fields= 0;
 
1475
  uint length= 0;
 
1476
  uint fields= 0;
 
1477
  uint null_fields= 0;
1468
1478
  MY_BITMAP *read_set= (*ptabfield)->table->read_set;
1469
1479
 
1470
1480
  /*
1544
1554
*/
1545
1555
 
1546
1556
static void 
1547
 
unpack_addon_fields(struct st_sort_addon_field *addon_field, unsigned char *buff)
 
1557
unpack_addon_fields(struct st_sort_addon_field *addon_field, uchar *buff)
1548
1558
{
1549
1559
  Field *field;
1550
1560
  SORT_ADDON_FIELD *addonf= addon_field;
1568
1578
 
1569
1579
#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
1570
1580
 
1571
 
void change_double_for_sort(double nr,unsigned char *to)
 
1581
void change_double_for_sort(double nr,uchar *to)
1572
1582
{
1573
 
  unsigned char *tmp=(unsigned char*) to;
 
1583
  uchar *tmp=(uchar*) to;
1574
1584
  if (nr == 0.0)
1575
1585
  {                                             /* Change to zero string */
1576
 
    tmp[0]=(unsigned char) 128;
1577
 
    memset(tmp+1, 0, sizeof(nr)-1);
 
1586
    tmp[0]=(uchar) 128;
 
1587
    bzero((char*) tmp+1,sizeof(nr)-1);
1578
1588
  }
1579
1589
  else
1580
1590
  {
1581
1591
#ifdef WORDS_BIGENDIAN
1582
 
    memcpy(tmp,&nr,sizeof(nr));
 
1592
    memcpy_fixed(tmp,&nr,sizeof(nr));
1583
1593
#else
1584
1594
    {
1585
 
      unsigned char *ptr= (unsigned char*) &nr;
 
1595
      uchar *ptr= (uchar*) &nr;
1586
1596
#if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
1587
1597
      tmp[0]= ptr[3]; tmp[1]=ptr[2]; tmp[2]= ptr[1]; tmp[3]=ptr[0];
1588
1598
      tmp[4]= ptr[7]; tmp[5]=ptr[6]; tmp[6]= ptr[5]; tmp[7]=ptr[4];
1594
1604
#endif
1595
1605
    if (tmp[0] & 128)                           /* Negative */
1596
1606
    {                                           /* make complement */
1597
 
      uint32_t i;
 
1607
      uint i;
1598
1608
      for (i=0 ; i < sizeof(nr); i++)
1599
 
        tmp[i]=tmp[i] ^ (unsigned char) 255;
 
1609
        tmp[i]=tmp[i] ^ (uchar) 255;
1600
1610
    }
1601
1611
    else
1602
1612
    {                                   /* Set high and move exponent one up */
1603
 
      uint16_t exp_part=(((uint16_t) tmp[0] << 8) | (uint16_t) tmp[1] |
1604
 
                       (uint16_t) 32768);
1605
 
      exp_part+= (uint16_t) 1 << (16-1-DBL_EXP_DIG);
1606
 
      tmp[0]= (unsigned char) (exp_part >> 8);
1607
 
      tmp[1]= (unsigned char) exp_part;
 
1613
      ushort exp_part=(((ushort) tmp[0] << 8) | (ushort) tmp[1] |
 
1614
                       (ushort) 32768);
 
1615
      exp_part+= (ushort) 1 << (16-1-DBL_EXP_DIG);
 
1616
      tmp[0]= (uchar) (exp_part >> 8);
 
1617
      tmp[1]= (uchar) exp_part;
1608
1618
    }
1609
1619
  }
1610
1620
}