~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/filesort.cc

  • Committer: Brian Aker
  • Date: 2008-07-07 14:25:25 UTC
  • mto: (77.1.25 codestyle)
  • mto: This revision was merged to the branch mainline in revision 82.
  • Revision ID: brian@tangent.org-20080707142525-xzy2nl3ie2ebwfln
LL() cleanup

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
  DBUG_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
 
 
104
 
  filesort_info_st table_sort;
105
 
  TableList *tab= table->pos_in_table_list;
 
108
  DBUG_ENTER("filesort");
 
109
  DBUG_EXECUTE("info",TEST_filesort(sortorder,s_length););
 
110
#ifdef SKIP_DBUG_IN_FILESORT
 
111
  DBUG_PUSH("");                /* No DBUG here */
 
112
#endif
 
113
  FILESORT_INFO table_sort;
 
114
  TABLE_LIST *tab= table->pos_in_table_list;
106
115
  Item_subselect *subselect= tab ? tab->containing_subselect() : 0;
107
116
 
108
 
  DRIZZLE_FILESORT_START();
 
117
  MYSQL_FILESORT_START();
109
118
 
110
119
  /*
111
120
   Release InnoDB's adaptive hash index latch (if holding) before
118
127
    QUICK_INDEX_MERGE_SELECT. Work with a copy and put it back at the end 
119
128
    when index_merge select has finished with it.
120
129
  */
121
 
  memcpy(&table_sort, &table->sort, sizeof(filesort_info_st));
 
130
  memcpy(&table_sort, &table->sort, sizeof(FILESORT_INFO));
122
131
  table->sort.io_cache= NULL;
123
132
  
124
133
  outfile= table_sort.io_cache;
126
135
  my_b_clear(&buffpek_pointers);
127
136
  buffpek=0;
128
137
  error= 1;
129
 
  memset(&param, 0, sizeof(param));
 
138
  bzero((char*) &param,sizeof(param));
130
139
  param.sort_length= sortlength(thd, sortorder, s_length, &multi_byte_charset);
131
140
  param.ref_length= table->file->ref_length;
132
141
  param.addon_field= 0;
133
142
  param.addon_length= 0;
134
 
  if (!(table->file->ha_table_flags() & HA_FAST_KEY_READ) && !sort_positions)
 
143
  if (!(table->file->ha_table_flags() & HA_FAST_KEY_READ) &&
 
144
      !table->fulltext_searched && !sort_positions)
135
145
  {
136
146
    /* 
137
147
      Get the descriptors of all fields whose values are appended 
149
159
  if (param.addon_field)
150
160
  {
151
161
    param.res_length= param.addon_length;
152
 
    if (!(table_sort.addon_buf= (unsigned char *) my_malloc(param.addon_length,
 
162
    if (!(table_sort.addon_buf= (uchar *) my_malloc(param.addon_length,
153
163
                                                    MYF(MY_WME))))
154
164
      goto err;
155
165
  }
176
186
#ifdef CAN_TRUST_RANGE
177
187
  if (select && select->quick && select->quick->records > 0L)
178
188
  {
179
 
    records=cmin((ha_rows) (select->quick->records*2+EXTRA_RECORDS*2),
 
189
    records=min((ha_rows) (select->quick->records*2+EXTRA_RECORDS*2),
180
190
                table->file->stats.records)+EXTRA_RECORDS;
181
191
    selected_records_file=0;
182
192
  }
198
208
    goto err;
199
209
 
200
210
  memavl= thd->variables.sortbuff_size;
201
 
  min_sort_memory= cmax((uint32_t)MIN_SORT_MEMORY, param.sort_length*MERGEBUFF2);
 
211
  min_sort_memory= max(MIN_SORT_MEMORY, param.sort_length*MERGEBUFF2);
202
212
  while (memavl >= min_sort_memory)
203
213
  {
204
 
    uint32_t old_memavl;
205
 
    uint32_t keys= memavl/(param.rec_length+sizeof(char*));
206
 
    param.keys=(uint32_t) cmin(records+1, keys);
 
214
    ulong old_memavl;
 
215
    ulong keys= memavl/(param.rec_length+sizeof(char*));
 
216
    param.keys=(uint) min(records+1, keys);
207
217
    if ((table_sort.sort_keys=
208
 
         (unsigned char **) make_char_array((char **) table_sort.sort_keys,
 
218
         (uchar **) make_char_array((char **) table_sort.sort_keys,
209
219
                                    param.keys, param.rec_length, MYF(0))))
210
220
      break;
211
221
    old_memavl=memavl;
229
239
                             &tempfile, selected_records_file)) ==
230
240
      HA_POS_ERROR)
231
241
    goto err;
232
 
  maxbuffer= (uint32_t) (my_b_tell(&buffpek_pointers)/sizeof(*buffpek));
 
242
  maxbuffer= (uint) (my_b_tell(&buffpek_pointers)/sizeof(*buffpek));
233
243
 
234
244
  if (maxbuffer == 0)                   // The whole set is in memory
235
245
  {
236
 
    if (save_index(&param,sort_keys,(uint32_t) records, &table_sort))
 
246
    if (save_index(&param,sort_keys,(uint) records, &table_sort))
237
247
      goto err;
238
248
  }
239
249
  else
240
250
  {
241
251
    if (table_sort.buffpek && table_sort.buffpek_len < maxbuffer)
242
252
    {
243
 
      if (table_sort.buffpek)
244
 
        free(table_sort.buffpek);
 
253
      x_free(table_sort.buffpek);
245
254
      table_sort.buffpek= 0;
246
255
    }
247
256
    if (!(table_sort.buffpek=
248
 
          (unsigned char *) read_buffpek_from_file(&buffpek_pointers, maxbuffer,
 
257
          (uchar *) read_buffpek_from_file(&buffpek_pointers, maxbuffer,
249
258
                                 table_sort.buffpek)))
250
259
      goto err;
251
260
    buffpek= (BUFFPEK *) table_sort.buffpek;
266
275
    param.keys=((param.keys*(param.rec_length+sizeof(char*))) /
267
276
                param.rec_length-1);
268
277
    maxbuffer--;                                // Offset from 0
269
 
    if (merge_many_buff(&param,(unsigned char*) sort_keys,buffpek,&maxbuffer,
 
278
    if (merge_many_buff(&param,(uchar*) sort_keys,buffpek,&maxbuffer,
270
279
                        &tempfile))
271
280
      goto err;
272
281
    if (flush_io_cache(&tempfile) ||
273
282
        reinit_io_cache(&tempfile,READ_CACHE,0L,0,0))
274
283
      goto err;
275
 
    if (merge_index(&param,(unsigned char*) sort_keys,buffpek,maxbuffer,&tempfile,
 
284
    if (merge_index(&param,(uchar*) sort_keys,buffpek,maxbuffer,&tempfile,
276
285
                    outfile))
277
286
      goto err;
278
287
  }
282
291
 
283
292
 err:
284
293
  if (param.tmp_buffer)
285
 
    if (param.tmp_buffer)
286
 
      free(param.tmp_buffer);
 
294
    x_free(param.tmp_buffer);
287
295
  if (!subselect || !subselect->is_uncacheable())
288
296
  {
289
 
    if ((unsigned char*) sort_keys)
290
 
      free((unsigned char*) sort_keys);
 
297
    x_free((uchar*) sort_keys);
291
298
    table_sort.sort_keys= 0;
292
 
    if ((unsigned char*) buffpek)
293
 
      free((unsigned char*) buffpek);
 
299
    x_free((uchar*) buffpek);
294
300
    table_sort.buffpek= 0;
295
301
    table_sort.buffpek_len= 0;
296
302
  }
313
319
               MYF(ME_ERROR+ME_WAITTANG));
314
320
  else
315
321
    statistic_add(thd->status_var.filesort_rows,
316
 
                  (uint32_t) records, &LOCK_status);
 
322
                  (ulong) records, &LOCK_status);
317
323
  *examined_rows= param.examined_rows;
318
 
  memcpy(&table->sort, &table_sort, sizeof(filesort_info_st));
319
 
  DRIZZLE_FILESORT_END();
320
 
  return(error ? HA_POS_ERROR : records);
 
324
#ifdef SKIP_DBUG_IN_FILESORT
 
325
  DBUG_POP();                   /* Ok to DBUG */
 
326
#endif
 
327
  memcpy(&table->sort, &table_sort, sizeof(FILESORT_INFO));
 
328
  DBUG_PRINT("exit",("records: %ld", (long) records));
 
329
  MYSQL_FILESORT_END();
 
330
  DBUG_RETURN(error ? HA_POS_ERROR : records);
321
331
} /* filesort */
322
332
 
323
333
 
324
 
void filesort_free_buffers(Table *table, bool full)
 
334
void filesort_free_buffers(TABLE *table, bool full)
325
335
{
326
336
  if (table->sort.record_pointers)
327
337
  {
328
 
    free((unsigned char*) table->sort.record_pointers);
 
338
    my_free((uchar*) table->sort.record_pointers,MYF(0));
329
339
    table->sort.record_pointers=0;
330
340
  }
331
341
  if (full)
332
342
  {
333
343
    if (table->sort.sort_keys )
334
344
    {
335
 
      if ((unsigned char*) table->sort.sort_keys)
336
 
        free((unsigned char*) table->sort.sort_keys);
 
345
      x_free((uchar*) table->sort.sort_keys);
337
346
      table->sort.sort_keys= 0;
338
347
    }
339
348
    if (table->sort.buffpek)
340
349
    {
341
 
      if ((unsigned char*) table->sort.buffpek)
342
 
        free((unsigned char*) table->sort.buffpek);
 
350
      x_free((uchar*) table->sort.buffpek);
343
351
      table->sort.buffpek= 0;
344
352
      table->sort.buffpek_len= 0;
345
353
    }
346
354
  }
347
355
  if (table->sort.addon_buf)
348
356
  {
349
 
    free((char *) table->sort.addon_buf);
350
 
    free((char *) table->sort.addon_field);
 
357
    my_free((char *) table->sort.addon_buf, MYF(0));
 
358
    my_free((char *) table->sort.addon_field, MYF(MY_ALLOW_ZERO_PTR));
351
359
    table->sort.addon_buf=0;
352
360
    table->sort.addon_field=0;
353
361
  }
355
363
 
356
364
/** Make a array of string pointers. */
357
365
 
358
 
static char **make_char_array(char **old_pos, register uint32_t fields,
359
 
                              uint32_t length, myf my_flag)
 
366
static char **make_char_array(char **old_pos, register uint fields,
 
367
                              uint length, myf my_flag)
360
368
{
361
369
  register char **pos;
362
370
  char *char_pos;
 
371
  DBUG_ENTER("make_char_array");
363
372
 
364
373
  if (old_pos ||
365
 
      (old_pos= (char**) my_malloc((uint32_t) fields*(length+sizeof(char*)),
 
374
      (old_pos= (char**) my_malloc((uint) fields*(length+sizeof(char*)),
366
375
                                   my_flag)))
367
376
  {
368
377
    pos=old_pos; char_pos=((char*) (pos+fields)) -length;
369
378
    while (fields--) *(pos++) = (char_pos+= length);
370
379
  }
371
380
 
372
 
  return(old_pos);
 
381
  DBUG_RETURN(old_pos);
373
382
} /* make_char_array */
374
383
 
375
384
 
376
385
/** Read 'count' number of buffer pointers into memory. */
377
386
 
378
 
static unsigned char *read_buffpek_from_file(IO_CACHE *buffpek_pointers, uint32_t count,
379
 
                                     unsigned char *buf)
 
387
static uchar *read_buffpek_from_file(IO_CACHE *buffpek_pointers, uint count,
 
388
                                     uchar *buf)
380
389
{
381
 
  uint32_t length= sizeof(BUFFPEK)*count;
382
 
  unsigned char *tmp= buf;
 
390
  ulong length= sizeof(BUFFPEK)*count;
 
391
  uchar *tmp= buf;
 
392
  DBUG_ENTER("read_buffpek_from_file");
383
393
  if (count > UINT_MAX/sizeof(BUFFPEK))
384
394
    return 0; /* sizeof(BUFFPEK)*count will overflow */
385
395
  if (!tmp)
386
 
    tmp= (unsigned char *)my_malloc(length, MYF(MY_WME));
 
396
    tmp= (uchar *)my_malloc(length, MYF(MY_WME));
387
397
  if (tmp)
388
398
  {
389
399
    if (reinit_io_cache(buffpek_pointers,READ_CACHE,0L,0,0) ||
390
 
        my_b_read(buffpek_pointers, (unsigned char*) tmp, length))
 
400
        my_b_read(buffpek_pointers, (uchar*) tmp, length))
391
401
    {
392
 
      free((char*) tmp);
 
402
      my_free((char*) tmp, MYF(0));
393
403
      tmp=0;
394
404
    }
395
405
  }
396
 
  return(tmp);
 
406
  DBUG_RETURN(tmp);
397
407
}
398
408
 
399
409
 
435
445
*/
436
446
 
437
447
static ha_rows find_all_keys(SORTPARAM *param, SQL_SELECT *select,
438
 
                             unsigned char **sort_keys,
 
448
                             uchar **sort_keys,
439
449
                             IO_CACHE *buffpek_pointers,
440
450
                             IO_CACHE *tempfile, IO_CACHE *indexfile)
441
451
{
442
452
  int error,flag,quick_select;
443
 
  uint32_t idx,indexpos,ref_length;
444
 
  unsigned char *ref_pos,*next_pos,ref_buff[MAX_REFLENGTH];
 
453
  uint idx,indexpos,ref_length;
 
454
  uchar *ref_pos,*next_pos,ref_buff[MAX_REFLENGTH];
445
455
  my_off_t record;
446
 
  Table *sort_form;
 
456
  TABLE *sort_form;
447
457
  THD *thd= current_thd;
448
458
  volatile THD::killed_state *killed= &thd->killed;
449
459
  handler *file;
450
460
  MY_BITMAP *save_read_set, *save_write_set;
 
461
  DBUG_ENTER("find_all_keys");
 
462
  DBUG_PRINT("info",("using: %s",
 
463
                     (select ? select->quick ? "ranges" : "where":
 
464
                      "every row")));
451
465
 
452
466
  idx=indexpos=0;
453
467
  error=quick_select=0;
464
478
  next_pos=ref_pos;
465
479
  if (! indexfile && ! quick_select)
466
480
  {
467
 
    next_pos=(unsigned char*) 0;                        /* Find records in sequence */
 
481
    next_pos=(uchar*) 0;                        /* Find records in sequence */
468
482
    file->ha_rnd_init(1);
469
483
    file->extra_opt(HA_EXTRA_CACHE,
470
484
                    current_thd->variables.read_buff_size);
474
488
  if (quick_select)
475
489
  {
476
490
    if (select->quick->reset())
477
 
      return(HA_POS_ERROR);
 
491
      DBUG_RETURN(HA_POS_ERROR);
478
492
    init_read_record(&read_record_info, current_thd, select->quick->head,
479
493
                     select, 1, 1);
480
494
  }
489
503
  register_used_fields(param);
490
504
  if (select && select->cond)
491
505
    select->cond->walk(&Item::register_field_in_read_map, 1,
492
 
                       (unsigned char*) sort_form);
 
506
                       (uchar*) sort_form);
493
507
  sort_form->column_bitmaps_set(&sort_form->tmp_set, &sort_form->tmp_set);
494
508
 
495
509
  for (;;)
507
521
    {
508
522
      if (indexfile)
509
523
      {
510
 
        if (my_b_read(indexfile,(unsigned char*) ref_pos,ref_length)) /* purecov: deadcode */
 
524
        if (my_b_read(indexfile,(uchar*) ref_pos,ref_length)) /* purecov: deadcode */
511
525
        {
512
526
          error= my_errno ? my_errno : -1;              /* Abort */
513
527
          break;
517
531
      else
518
532
      {
519
533
        error=file->rnd_next(sort_form->record[0]);
520
 
        if (!error)
521
 
          update_virtual_fields_marked_for_write(sort_form);
522
 
 
523
534
        if (!flag)
524
535
        {
525
536
          my_store_ptr(ref_pos,ref_length,record); // Position to row
534
545
 
535
546
    if (*killed)
536
547
    {
 
548
      DBUG_PRINT("info",("Sort killed by user"));
537
549
      if (!indexfile && !quick_select)
538
550
      {
539
551
        (void) file->extra(HA_EXTRA_NO_CACHE);
540
552
        file->ha_rnd_end();
541
553
      }
542
 
      return(HA_POS_ERROR);             /* purecov: inspected */
 
554
      DBUG_RETURN(HA_POS_ERROR);                /* purecov: inspected */
543
555
    }
544
556
    if (error == 0)
545
557
      param->examined_rows++;
548
560
      if (idx == param->keys)
549
561
      {
550
562
        if (write_keys(param,sort_keys,idx,buffpek_pointers,tempfile))
551
 
          return(HA_POS_ERROR);
 
563
          DBUG_RETURN(HA_POS_ERROR);
552
564
        idx=0;
553
565
        indexpos++;
554
566
      }
576
588
  }
577
589
 
578
590
  if (thd->is_error())
579
 
    return(HA_POS_ERROR);
 
591
    DBUG_RETURN(HA_POS_ERROR);
580
592
  
581
593
  /* Signal we should use orignal column read and write maps */
582
594
  sort_form->column_bitmaps_set(save_read_set, save_write_set);
583
595
 
 
596
  DBUG_PRINT("test",("error: %d  indexpos: %d",error,indexpos));
584
597
  if (error != HA_ERR_END_OF_FILE)
585
598
  {
586
599
    file->print_error(error,MYF(ME_ERROR | ME_WAITTANG)); /* purecov: inspected */
587
 
    return(HA_POS_ERROR);                       /* purecov: inspected */
 
600
    DBUG_RETURN(HA_POS_ERROR);                  /* purecov: inspected */
588
601
  }
589
602
  if (indexpos && idx &&
590
603
      write_keys(param,sort_keys,idx,buffpek_pointers,tempfile))
591
 
    return(HA_POS_ERROR);                       /* purecov: inspected */
592
 
  return(my_b_inited(tempfile) ?
 
604
    DBUG_RETURN(HA_POS_ERROR);                  /* purecov: inspected */
 
605
  DBUG_RETURN(my_b_inited(tempfile) ?
593
606
              (ha_rows) (my_b_tell(tempfile)/param->rec_length) :
594
607
              idx);
595
608
} /* find_all_keys */
618
631
*/
619
632
 
620
633
static int
621
 
write_keys(SORTPARAM *param, register unsigned char **sort_keys, uint32_t count,
 
634
write_keys(SORTPARAM *param, register uchar **sort_keys, uint count,
622
635
           IO_CACHE *buffpek_pointers, IO_CACHE *tempfile)
623
636
{
624
637
  size_t sort_length, rec_length;
625
 
  unsigned char **end;
 
638
  uchar **end;
626
639
  BUFFPEK buffpek;
 
640
  DBUG_ENTER("write_keys");
627
641
 
628
642
  sort_length= param->sort_length;
629
643
  rec_length= param->rec_length;
630
 
  my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, sort_length);
 
644
#ifdef MC68000
 
645
  quicksort(sort_keys,count,sort_length);
 
646
#else
 
647
  my_string_ptr_sort((uchar*) sort_keys, (uint) count, sort_length);
 
648
#endif
631
649
  if (!my_b_inited(tempfile) &&
632
650
      open_cached_file(tempfile, mysql_tmpdir, TEMP_PREFIX, DISK_BUFFER_SIZE,
633
651
                       MYF(MY_WME)))
634
652
    goto err;                                   /* purecov: inspected */
635
653
  /* check we won't have more buffpeks than we can possibly keep in memory */
636
 
  if (my_b_tell(buffpek_pointers) + sizeof(BUFFPEK) > (uint64_t)UINT_MAX)
 
654
  if (my_b_tell(buffpek_pointers) + sizeof(BUFFPEK) > (ulonglong)UINT_MAX)
637
655
    goto err;
638
656
  buffpek.file_pos= my_b_tell(tempfile);
639
657
  if ((ha_rows) count > param->max_rows)
640
 
    count=(uint32_t) param->max_rows;               /* purecov: inspected */
 
658
    count=(uint) param->max_rows;               /* purecov: inspected */
641
659
  buffpek.count=(ha_rows) count;
642
660
  for (end=sort_keys+count ; sort_keys != end ; sort_keys++)
643
 
    if (my_b_write(tempfile, (unsigned char*) *sort_keys, (uint32_t) rec_length))
 
661
    if (my_b_write(tempfile, (uchar*) *sort_keys, (uint) rec_length))
644
662
      goto err;
645
 
  if (my_b_write(buffpek_pointers, (unsigned char*) &buffpek, sizeof(buffpek)))
 
663
  if (my_b_write(buffpek_pointers, (uchar*) &buffpek, sizeof(buffpek)))
646
664
    goto err;
647
 
  return(0);
 
665
  DBUG_RETURN(0);
648
666
 
649
667
err:
650
 
  return(1);
 
668
  DBUG_RETURN(1);
651
669
} /* write_keys */
652
670
 
653
671
 
655
673
  Store length as suffix in high-byte-first order.
656
674
*/
657
675
 
658
 
static inline void store_length(unsigned char *to, uint32_t length, uint32_t pack_length)
 
676
static inline void store_length(uchar *to, uint length, uint pack_length)
659
677
{
660
678
  switch (pack_length) {
661
679
  case 1:
662
 
    *to= (unsigned char) length;
 
680
    *to= (uchar) length;
663
681
    break;
664
682
  case 2:
665
683
    mi_int2store(to, length);
677
695
/** Make a sort-key from record. */
678
696
 
679
697
static void make_sortkey(register SORTPARAM *param,
680
 
                         register unsigned char *to, unsigned char *ref_pos)
 
698
                         register uchar *to, uchar *ref_pos)
681
699
{
682
700
  register Field *field;
683
701
  register SORT_FIELD *sort_field;
684
 
  register uint32_t length;
 
702
  register uint length;
685
703
 
686
704
  for (sort_field=param->local_sortorder ;
687
705
       sort_field != param->end ;
695
713
        if (field->is_null())
696
714
        {
697
715
          if (sort_field->reverse)
698
 
            memset(to, 255, sort_field->length+1);
 
716
            bfill(to,sort_field->length+1,(char) 255);
699
717
          else
700
 
            memset(to, 0, sort_field->length+1);
 
718
            bzero((char*) to,sort_field->length+1);
701
719
          to+= sort_field->length+1;
702
720
          continue;
703
721
        }
713
731
      switch (sort_field->result_type) {
714
732
      case STRING_RESULT:
715
733
      {
716
 
        const CHARSET_INFO * const cs=item->collation.collation;
 
734
        CHARSET_INFO *cs=item->collation.collation;
717
735
        char fill_char= ((cs->state & MY_CS_BINSORT) ? (char) 0 : ' ');
718
736
        int diff;
719
 
        uint32_t sort_field_length;
 
737
        uint sort_field_length;
720
738
 
721
739
        if (maybe_null)
722
740
          *to++=1;
726
744
        if (!res)
727
745
        {
728
746
          if (maybe_null)
729
 
            memset(to-1, 0, sort_field->length+1);
 
747
            bzero((char*) to-1,sort_field->length+1);
730
748
          else
731
749
          {
732
750
            /* purecov: begin deadcode */
735
753
              of memory or have an item marked not null when it can be null.
736
754
              This code is here mainly to avoid a hard crash in this case.
737
755
            */
738
 
            assert(0);
739
 
            memset(to, 0, sort_field->length);  // Avoid crash
 
756
            DBUG_ASSERT(0);
 
757
            DBUG_PRINT("warning",
 
758
                       ("Got null on something that shouldn't be null"));
 
759
            bzero((char*) to,sort_field->length);       // Avoid crash
740
760
            /* purecov: end */
741
761
          }
742
762
          break;
758
778
        if (sort_field->need_strxnfrm)
759
779
        {
760
780
          char *from=(char*) res->ptr();
761
 
          uint32_t tmp_length;
762
 
          if ((unsigned char*) from == to)
 
781
          uint tmp_length;
 
782
          if ((uchar*) from == to)
763
783
          {
764
784
            set_if_smaller(length,sort_field->length);
765
785
            memcpy(param->tmp_buffer,from,length);
766
786
            from=param->tmp_buffer;
767
787
          }
768
788
          tmp_length= my_strnxfrm(cs,to,sort_field->length,
769
 
                                  (unsigned char*) from, length);
770
 
          assert(tmp_length == sort_field->length);
 
789
                                  (uchar*) from, length);
 
790
          DBUG_ASSERT(tmp_length == sort_field->length);
771
791
        }
772
792
        else
773
793
        {
774
 
          my_strnxfrm(cs,(unsigned char*)to,length,(const unsigned char*)res->ptr(),length);
 
794
          my_strnxfrm(cs,(uchar*)to,length,(const uchar*)res->ptr(),length);
775
795
          cs->cset->fill(cs, (char *)to+length,diff,fill_char);
776
796
        }
777
797
        break;
778
798
      }
779
799
      case INT_RESULT:
780
800
        {
781
 
          int64_t value= item->val_int_result();
 
801
          longlong value= item->val_int_result();
782
802
          if (maybe_null)
783
803
          {
784
804
            *to++=1;                            /* purecov: inspected */
785
805
            if (item->null_value)
786
806
            {
787
807
              if (maybe_null)
788
 
                memset(to-1, 0, sort_field->length+1);
 
808
                bzero((char*) to-1,sort_field->length+1);
789
809
              else
790
810
              {
791
 
                memset(to, 0, sort_field->length);
 
811
                DBUG_PRINT("warning",
 
812
                           ("Got null on something that shouldn't be null"));
 
813
                bzero((char*) to,sort_field->length);
792
814
              }
793
815
              break;
794
816
            }
795
817
          }
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 */
 
818
#if SIZEOF_LONG_LONG > 4
 
819
          to[7]= (uchar) value;
 
820
          to[6]= (uchar) (value >> 8);
 
821
          to[5]= (uchar) (value >> 16);
 
822
          to[4]= (uchar) (value >> 24);
 
823
          to[3]= (uchar) (value >> 32);
 
824
          to[2]= (uchar) (value >> 40);
 
825
          to[1]= (uchar) (value >> 48);
 
826
          if (item->unsigned_flag)                    /* Fix sign */
 
827
            to[0]= (uchar) (value >> 56);
 
828
          else
 
829
            to[0]= (uchar) (value >> 56) ^ 128; /* Reverse signbit */
 
830
#else
 
831
          to[3]= (uchar) value;
 
832
          to[2]= (uchar) (value >> 8);
 
833
          to[1]= (uchar) (value >> 16);
 
834
          if (item->unsigned_flag)                    /* Fix sign */
 
835
            to[0]= (uchar) (value >> 24);
 
836
          else
 
837
            to[0]= (uchar) (value >> 24) ^ 128; /* Reverse signbit */
 
838
#endif
807
839
          break;
808
840
        }
809
841
      case DECIMAL_RESULT:
813
845
          {
814
846
            if (item->null_value)
815
847
            { 
816
 
              memset(to, 0, sort_field->length+1);
 
848
              bzero((char*)to, sort_field->length+1);
817
849
              to++;
818
850
              break;
819
851
            }
831
863
          {
832
864
            if (item->null_value)
833
865
            {
834
 
              memset(to, 0, sort_field->length+1);
 
866
              bzero((char*) to,sort_field->length+1);
835
867
              to++;
836
868
              break;
837
869
            }
838
870
            *to++=1;
839
871
          }
840
 
          change_double_for_sort(value,(unsigned char*) to);
 
872
          change_double_for_sort(value,(uchar*) to);
841
873
          break;
842
874
        }
843
875
      case ROW_RESULT:
844
876
      default: 
845
877
        // This case should never be choosen
846
 
        assert(0);
 
878
        DBUG_ASSERT(0);
847
879
        break;
848
880
      }
849
881
    }
854
886
      length=sort_field->length;
855
887
      while (length--)
856
888
      {
857
 
        *to = (unsigned char) (~ *to);
 
889
        *to = (uchar) (~ *to);
858
890
        to++;
859
891
      }
860
892
    }
871
903
      the same for all records.
872
904
    */
873
905
    SORT_ADDON_FIELD *addonf= param->addon_field;
874
 
    unsigned char *nulls= to;
875
 
    assert(addonf != 0);
876
 
    memset(nulls, 0, addonf->offset);
 
906
    uchar *nulls= to;
 
907
    DBUG_ASSERT(addonf != 0);
 
908
    bzero((char *) nulls, addonf->offset);
877
909
    to+= addonf->offset;
878
910
    for ( ; (field= addonf->field) ; addonf++)
879
911
    {
881
913
      {
882
914
        nulls[addonf->null_offset]|= addonf->null_bit;
883
915
#ifdef HAVE_purify
884
 
        memset(to, 0, addonf->length);
 
916
        bzero(to, addonf->length);
885
917
#endif
886
918
      }
887
919
      else
888
920
      {
889
921
#ifdef HAVE_purify
890
 
        unsigned char *end= field->pack(to, field->ptr);
891
 
        uint32_t length= (uint32_t) ((to + addonf->length) - end);
892
 
        assert((int) length >= 0);
 
922
        uchar *end= field->pack(to, field->ptr);
 
923
        uint length= (uint) ((to + addonf->length) - end);
 
924
        DBUG_ASSERT((int) length >= 0);
893
925
        if (length)
894
 
          memset(end, 0, length);
 
926
          bzero(end, length);
895
927
#else
896
928
        (void) field->pack(to, field->ptr);
897
929
#endif
902
934
  else
903
935
  {
904
936
    /* Save filepos last */
905
 
    memcpy(to, ref_pos, (size_t) param->ref_length);
 
937
    memcpy((uchar*) to, ref_pos, (size_t) param->ref_length);
906
938
  }
907
939
  return;
908
940
}
915
947
static void register_used_fields(SORTPARAM *param)
916
948
{
917
949
  register SORT_FIELD *sort_field;
918
 
  Table *table=param->sort_form;
 
950
  TABLE *table=param->sort_form;
919
951
  MY_BITMAP *bitmap= table->read_set;
920
952
 
921
953
  for (sort_field= param->local_sortorder ;
931
963
    else
932
964
    {                                           // Item
933
965
      sort_field->item->walk(&Item::register_field_in_read_map, 1,
934
 
                             (unsigned char *) table);
 
966
                             (uchar *) table);
935
967
    }
936
968
  }
937
969
 
950
982
}
951
983
 
952
984
 
953
 
static bool save_index(SORTPARAM *param, unsigned char **sort_keys, uint32_t count, 
954
 
                       filesort_info_st *table_sort)
 
985
static bool save_index(SORTPARAM *param, uchar **sort_keys, uint count, 
 
986
                       FILESORT_INFO *table_sort)
955
987
{
956
 
  uint32_t offset,res_length;
957
 
  unsigned char *to;
 
988
  uint offset,res_length;
 
989
  uchar *to;
 
990
  DBUG_ENTER("save_index");
958
991
 
959
 
  my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, param->sort_length);
 
992
  my_string_ptr_sort((uchar*) sort_keys, (uint) count, param->sort_length);
960
993
  res_length= param->res_length;
961
994
  offset= param->rec_length-res_length;
962
995
  if ((ha_rows) count > param->max_rows)
963
 
    count=(uint32_t) param->max_rows;
 
996
    count=(uint) param->max_rows;
964
997
  if (!(to= table_sort->record_pointers= 
965
 
        (unsigned char*) my_malloc(res_length*count, MYF(MY_WME))))
966
 
    return(1);                 /* purecov: inspected */
967
 
  for (unsigned char **end= sort_keys+count ; sort_keys != end ; sort_keys++)
 
998
        (uchar*) my_malloc(res_length*count, MYF(MY_WME))))
 
999
    DBUG_RETURN(1);                 /* purecov: inspected */
 
1000
  for (uchar **end= sort_keys+count ; sort_keys != end ; sort_keys++)
968
1001
  {
969
1002
    memcpy(to, *sort_keys+offset, res_length);
970
1003
    to+= res_length;
971
1004
  }
972
 
  return(0);
 
1005
  DBUG_RETURN(0);
973
1006
}
974
1007
 
975
1008
 
976
1009
/** Merge buffers to make < MERGEBUFF2 buffers. */
977
1010
 
978
 
int merge_many_buff(SORTPARAM *param, unsigned char *sort_buffer,
979
 
                    BUFFPEK *buffpek, uint32_t *maxbuffer, IO_CACHE *t_file)
 
1011
int merge_many_buff(SORTPARAM *param, uchar *sort_buffer,
 
1012
                    BUFFPEK *buffpek, uint *maxbuffer, IO_CACHE *t_file)
980
1013
{
981
 
  register uint32_t i;
 
1014
  register uint i;
982
1015
  IO_CACHE t_file2,*from_file,*to_file,*temp;
983
1016
  BUFFPEK *lastbuff;
 
1017
  DBUG_ENTER("merge_many_buff");
984
1018
 
985
1019
  if (*maxbuffer < MERGEBUFF2)
986
 
    return(0);                          /* purecov: inspected */
 
1020
    DBUG_RETURN(0);                             /* purecov: inspected */
987
1021
  if (flush_io_cache(t_file) ||
988
1022
      open_cached_file(&t_file2,mysql_tmpdir,TEMP_PREFIX,DISK_BUFFER_SIZE,
989
1023
                        MYF(MY_WME)))
990
 
    return(1);                          /* purecov: inspected */
 
1024
    DBUG_RETURN(1);                             /* purecov: inspected */
991
1025
 
992
1026
  from_file= t_file ; to_file= &t_file2;
993
1027
  while (*maxbuffer >= MERGEBUFF2)
1011
1045
    temp=from_file; from_file=to_file; to_file=temp;
1012
1046
    setup_io_cache(from_file);
1013
1047
    setup_io_cache(to_file);
1014
 
    *maxbuffer= (uint32_t) (lastbuff-buffpek)-1;
 
1048
    *maxbuffer= (uint) (lastbuff-buffpek)-1;
1015
1049
  }
1016
1050
cleanup:
1017
1051
  close_cached_file(to_file);                   // This holds old result
1021
1055
    setup_io_cache(t_file);
1022
1056
  }
1023
1057
 
1024
 
  return(*maxbuffer >= MERGEBUFF2);     /* Return 1 if interrupted */
 
1058
  DBUG_RETURN(*maxbuffer >= MERGEBUFF2);        /* Return 1 if interrupted */
1025
1059
} /* merge_many_buff */
1026
1060
 
1027
1061
 
1029
1063
  Read data to buffer.
1030
1064
 
1031
1065
  @retval
1032
 
    (uint32_t)-1 if something goes wrong
 
1066
    (uint)-1 if something goes wrong
1033
1067
*/
1034
1068
 
1035
 
uint32_t read_to_buffer(IO_CACHE *fromfile, BUFFPEK *buffpek,
1036
 
                    uint32_t rec_length)
 
1069
uint read_to_buffer(IO_CACHE *fromfile, BUFFPEK *buffpek,
 
1070
                    uint rec_length)
1037
1071
{
1038
 
  register uint32_t count;
1039
 
  uint32_t length;
 
1072
  register uint count;
 
1073
  uint length;
1040
1074
 
1041
 
  if ((count=(uint32_t) cmin((ha_rows) buffpek->max_keys,buffpek->count)))
 
1075
  if ((count=(uint) min((ha_rows) buffpek->max_keys,buffpek->count)))
1042
1076
  {
1043
 
    if (pread(fromfile->file,(unsigned char*) buffpek->base, (length= rec_length*count),buffpek->file_pos) == 0)
1044
 
      return((uint32_t) -1);                    /* purecov: inspected */
 
1077
    if (pread(fromfile->file,(uchar*) buffpek->base, (length= rec_length*count),buffpek->file_pos) == 0)
 
1078
      return((uint) -1);                        /* purecov: inspected */
1045
1079
    buffpek->key=buffpek->base;
1046
1080
    buffpek->file_pos+= length;                 /* New filepos */
1047
1081
    buffpek->count-=    count;
1062
1096
  @param[in] key_length key length
1063
1097
*/
1064
1098
 
1065
 
void reuse_freed_buff(QUEUE *queue, BUFFPEK *reuse, uint32_t key_length)
 
1099
void reuse_freed_buff(QUEUE *queue, BUFFPEK *reuse, uint key_length)
1066
1100
{
1067
 
  unsigned char *reuse_end= reuse->base + reuse->max_keys * key_length;
1068
 
  for (uint32_t i= 0; i < queue->elements; ++i)
 
1101
  uchar *reuse_end= reuse->base + reuse->max_keys * key_length;
 
1102
  for (uint i= 0; i < queue->elements; ++i)
1069
1103
  {
1070
1104
    BUFFPEK *bp= (BUFFPEK *) queue_element(queue, i);
1071
1105
    if (bp->base + bp->max_keys * key_length == reuse->base)
1080
1114
      return;
1081
1115
    }
1082
1116
  }
1083
 
  assert(0);
 
1117
  DBUG_ASSERT(0);
1084
1118
}
1085
1119
 
1086
1120
 
1103
1137
*/
1104
1138
 
1105
1139
int merge_buffers(SORTPARAM *param, IO_CACHE *from_file,
1106
 
                  IO_CACHE *to_file, unsigned char *sort_buffer,
 
1140
                  IO_CACHE *to_file, uchar *sort_buffer,
1107
1141
                  BUFFPEK *lastbuff, BUFFPEK *Fb, BUFFPEK *Tb,
1108
1142
                  int flag)
1109
1143
{
1110
1144
  int error;
1111
 
  uint32_t rec_length,res_length,offset;
 
1145
  uint rec_length,res_length,offset;
1112
1146
  size_t sort_length;
1113
 
  uint32_t maxcount;
 
1147
  ulong maxcount;
1114
1148
  ha_rows max_rows,org_max_rows;
1115
1149
  my_off_t to_start_filepos;
1116
 
  unsigned char *strpos;
 
1150
  uchar *strpos;
1117
1151
  BUFFPEK *buffpek;
1118
1152
  QUEUE queue;
1119
1153
  qsort2_cmp cmp;
1120
1154
  void *first_cmp_arg;
1121
1155
  volatile THD::killed_state *killed= &current_thd->killed;
1122
1156
  THD::killed_state not_killable;
 
1157
  DBUG_ENTER("merge_buffers");
1123
1158
 
1124
1159
  status_var_increment(current_thd->status_var.filesort_merge_passes);
1125
1160
  if (param->not_killable)
1133
1168
  res_length= param->res_length;
1134
1169
  sort_length= param->sort_length;
1135
1170
  offset= rec_length-res_length;
1136
 
  maxcount= (uint32_t) (param->keys/((uint32_t) (Tb-Fb) +1));
 
1171
  maxcount= (ulong) (param->keys/((uint) (Tb-Fb) +1));
1137
1172
  to_start_filepos= my_b_tell(to_file);
1138
 
  strpos= (unsigned char*) sort_buffer;
 
1173
  strpos= (uchar*) sort_buffer;
1139
1174
  org_max_rows=max_rows= param->max_rows;
1140
1175
 
1141
1176
  /* The following will fire if there is not enough space in sort_buffer */
1142
 
  assert(maxcount!=0);
 
1177
  DBUG_ASSERT(maxcount!=0);
1143
1178
  
1144
1179
  if (param->unique_buff)
1145
1180
  {
1151
1186
    cmp= get_ptr_compare(sort_length);
1152
1187
    first_cmp_arg= (void*) &sort_length;
1153
1188
  }
1154
 
  if (init_queue(&queue, (uint32_t) (Tb-Fb)+1, offsetof(BUFFPEK,key), 0,
 
1189
  if (init_queue(&queue, (uint) (Tb-Fb)+1, offsetof(BUFFPEK,key), 0,
1155
1190
                 (queue_compare) cmp, first_cmp_arg))
1156
 
    return(1);                                /* purecov: inspected */
 
1191
    DBUG_RETURN(1);                                /* purecov: inspected */
1157
1192
  for (buffpek= Fb ; buffpek <= Tb ; buffpek++)
1158
1193
  {
1159
1194
    buffpek->base= strpos;
1160
1195
    buffpek->max_keys= maxcount;
1161
 
    strpos+= (uint32_t) (error= (int) read_to_buffer(from_file, buffpek,
 
1196
    strpos+= (uint) (error= (int) read_to_buffer(from_file, buffpek,
1162
1197
                                                                         rec_length));
1163
1198
    if (error == -1)
1164
1199
      goto err;                                 /* purecov: inspected */
1165
1200
    buffpek->max_keys= buffpek->mem_count;      // If less data in buffers than expected
1166
 
    queue_insert(&queue, (unsigned char*) buffpek);
 
1201
    queue_insert(&queue, (uchar*) buffpek);
1167
1202
  }
1168
1203
 
1169
1204
  if (param->unique_buff)
1178
1213
    */
1179
1214
    buffpek= (BUFFPEK*) queue_top(&queue);
1180
1215
    memcpy(param->unique_buff, buffpek->key, rec_length);
1181
 
    if (my_b_write(to_file, (unsigned char*) buffpek->key, rec_length))
 
1216
    if (my_b_write(to_file, (uchar*) buffpek->key, rec_length))
1182
1217
    {
1183
1218
      error=1; goto err;                        /* purecov: inspected */
1184
1219
    }
1206
1241
      if (cmp)                                        // Remove duplicates
1207
1242
      {
1208
1243
        if (!(*cmp)(first_cmp_arg, &(param->unique_buff),
1209
 
                    (unsigned char**) &buffpek->key))
 
1244
                    (uchar**) &buffpek->key))
1210
1245
              goto skip_duplicate;
1211
 
            memcpy(param->unique_buff, buffpek->key, rec_length);
 
1246
            memcpy(param->unique_buff, (uchar*) buffpek->key, rec_length);
1212
1247
      }
1213
1248
      if (flag == 0)
1214
1249
      {
1215
 
        if (my_b_write(to_file,(unsigned char*) buffpek->key, rec_length))
 
1250
        if (my_b_write(to_file,(uchar*) buffpek->key, rec_length))
1216
1251
        {
1217
1252
          error=1; goto err;                        /* purecov: inspected */
1218
1253
        }
1219
1254
      }
1220
1255
      else
1221
1256
      {
1222
 
        if (my_b_write(to_file, (unsigned char*) buffpek->key+offset, res_length))
 
1257
        if (my_b_write(to_file, (uchar*) buffpek->key+offset, res_length))
1223
1258
        {
1224
1259
          error=1; goto err;                        /* purecov: inspected */
1225
1260
        }
1237
1272
        if (!(error= (int) read_to_buffer(from_file,buffpek,
1238
1273
                                          rec_length)))
1239
1274
        {
1240
 
          queue_remove(&queue,0);
 
1275
          VOID(queue_remove(&queue,0));
1241
1276
          reuse_freed_buff(&queue, buffpek, rec_length);
1242
1277
          break;                        /* One buffer have been removed */
1243
1278
        }
1257
1292
  */
1258
1293
  if (cmp)
1259
1294
  {
1260
 
    if (!(*cmp)(first_cmp_arg, &(param->unique_buff), (unsigned char**) &buffpek->key))
 
1295
    if (!(*cmp)(first_cmp_arg, &(param->unique_buff), (uchar**) &buffpek->key))
1261
1296
    {
1262
1297
      buffpek->key+= rec_length;         // Remove duplicate
1263
1298
      --buffpek->mem_count;
1268
1303
  {
1269
1304
    if ((ha_rows) buffpek->mem_count > max_rows)
1270
1305
    {                                        /* Don't write too many records */
1271
 
      buffpek->mem_count= (uint32_t) max_rows;
 
1306
      buffpek->mem_count= (uint) max_rows;
1272
1307
      buffpek->count= 0;                        /* Don't read more */
1273
1308
    }
1274
1309
    max_rows-= buffpek->mem_count;
1275
1310
    if (flag == 0)
1276
1311
    {
1277
 
      if (my_b_write(to_file,(unsigned char*) buffpek->key,
 
1312
      if (my_b_write(to_file,(uchar*) buffpek->key,
1278
1313
                     (rec_length*buffpek->mem_count)))
1279
1314
      {
1280
1315
        error= 1; goto err;                        /* purecov: inspected */
1282
1317
    }
1283
1318
    else
1284
1319
    {
1285
 
      register unsigned char *end;
 
1320
      register uchar *end;
1286
1321
      strpos= buffpek->key+offset;
1287
1322
      for (end= strpos+buffpek->mem_count*rec_length ;
1288
1323
           strpos != end ;
1289
1324
           strpos+= rec_length)
1290
1325
      {     
1291
 
        if (my_b_write(to_file, (unsigned char *) strpos, res_length))
 
1326
        if (my_b_write(to_file, (uchar *) strpos, res_length))
1292
1327
        {
1293
1328
          error=1; goto err;                        
1294
1329
        }
1299
1334
         != -1 && error != 0);
1300
1335
 
1301
1336
end:
1302
 
  lastbuff->count= cmin(org_max_rows-max_rows, param->max_rows);
 
1337
  lastbuff->count= min(org_max_rows-max_rows, param->max_rows);
1303
1338
  lastbuff->file_pos= to_start_filepos;
1304
1339
err:
1305
1340
  delete_queue(&queue);
1306
 
  return(error);
 
1341
  DBUG_RETURN(error);
1307
1342
} /* merge_buffers */
1308
1343
 
1309
1344
 
1310
1345
        /* Do a merge to output-file (save only positions) */
1311
1346
 
1312
 
static int merge_index(SORTPARAM *param, unsigned char *sort_buffer,
1313
 
                       BUFFPEK *buffpek, uint32_t maxbuffer,
 
1347
static int merge_index(SORTPARAM *param, uchar *sort_buffer,
 
1348
                       BUFFPEK *buffpek, uint maxbuffer,
1314
1349
                       IO_CACHE *tempfile, IO_CACHE *outfile)
1315
1350
{
 
1351
  DBUG_ENTER("merge_index");
1316
1352
  if (merge_buffers(param,tempfile,outfile,sort_buffer,buffpek,buffpek,
1317
1353
                    buffpek+maxbuffer,1))
1318
 
    return(1);                          /* purecov: inspected */
1319
 
  return(0);
 
1354
    DBUG_RETURN(1);                             /* purecov: inspected */
 
1355
  DBUG_RETURN(0);
1320
1356
} /* merge_index */
1321
1357
 
1322
1358
 
1323
 
static uint32_t suffix_length(uint32_t string_length)
 
1359
static uint suffix_length(ulong string_length)
1324
1360
{
1325
1361
  if (string_length < 256)
1326
1362
    return 1;
1351
1387
    Total length of sort buffer in bytes
1352
1388
*/
1353
1389
 
1354
 
static uint32_t
1355
 
sortlength(THD *thd, SORT_FIELD *sortorder, uint32_t s_length,
 
1390
static uint
 
1391
sortlength(THD *thd, SORT_FIELD *sortorder, uint s_length,
1356
1392
           bool *multi_byte_charset)
1357
1393
{
1358
 
  register uint32_t length;
1359
 
  const CHARSET_INFO *cs;
 
1394
  register uint length;
 
1395
  CHARSET_INFO *cs;
1360
1396
  *multi_byte_charset= 0;
1361
1397
 
1362
1398
  length=0;
1381
1417
    else
1382
1418
    {
1383
1419
      sortorder->result_type= sortorder->item->result_type();
1384
 
      if (sortorder->item->result_as_int64_t())
 
1420
      if (sortorder->item->result_as_longlong())
1385
1421
        sortorder->result_type= INT_RESULT;
1386
1422
      switch (sortorder->result_type) {
1387
1423
      case STRING_RESULT:
1401
1437
        }
1402
1438
        break;
1403
1439
      case INT_RESULT:
1404
 
        sortorder->length=8;                    // Size of intern int64_t
 
1440
#if SIZEOF_LONG_LONG > 4
 
1441
        sortorder->length=8;                    // Size of intern longlong
 
1442
#else
 
1443
        sortorder->length=4;
 
1444
#endif
1405
1445
        break;
1406
1446
      case DECIMAL_RESULT:
1407
1447
        sortorder->length=
1415
1455
      case ROW_RESULT:
1416
1456
      default: 
1417
1457
        // This case should never be choosen
1418
 
        assert(0);
 
1458
        DBUG_ASSERT(0);
1419
1459
        break;
1420
1460
      }
1421
1461
      if (sortorder->item->maybe_null)
1425
1465
    length+=sortorder->length;
1426
1466
  }
1427
1467
  sortorder->field= (Field*) 0;                 // end marker
 
1468
  DBUG_PRINT("info",("sort_length: %d",length));
1428
1469
  return length;
1429
1470
}
1430
1471
 
1457
1498
*/
1458
1499
 
1459
1500
static SORT_ADDON_FIELD *
1460
 
get_addon_fields(THD *thd, Field **ptabfield, uint32_t sortlength, uint32_t *plength)
 
1501
get_addon_fields(THD *thd, Field **ptabfield, uint sortlength, uint *plength)
1461
1502
{
1462
1503
  Field **pfield;
1463
1504
  Field *field;
1464
1505
  SORT_ADDON_FIELD *addonf;
1465
 
  uint32_t length= 0;
1466
 
  uint32_t fields= 0;
1467
 
  uint32_t null_fields= 0;
 
1506
  uint length= 0;
 
1507
  uint fields= 0;
 
1508
  uint null_fields= 0;
1468
1509
  MY_BITMAP *read_set= (*ptabfield)->table->read_set;
1469
1510
 
1470
1511
  /*
1524
1565
  }
1525
1566
  addonf->field= 0;     // Put end marker
1526
1567
  
 
1568
  DBUG_PRINT("info",("addon_length: %d",length));
1527
1569
  return (addonf-fields);
1528
1570
}
1529
1571
 
1544
1586
*/
1545
1587
 
1546
1588
static void 
1547
 
unpack_addon_fields(struct st_sort_addon_field *addon_field, unsigned char *buff)
 
1589
unpack_addon_fields(struct st_sort_addon_field *addon_field, uchar *buff)
1548
1590
{
1549
1591
  Field *field;
1550
1592
  SORT_ADDON_FIELD *addonf= addon_field;
1568
1610
 
1569
1611
#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
1570
1612
 
1571
 
void change_double_for_sort(double nr,unsigned char *to)
 
1613
void change_double_for_sort(double nr,uchar *to)
1572
1614
{
1573
 
  unsigned char *tmp=(unsigned char*) to;
 
1615
  uchar *tmp=(uchar*) to;
1574
1616
  if (nr == 0.0)
1575
1617
  {                                             /* Change to zero string */
1576
 
    tmp[0]=(unsigned char) 128;
1577
 
    memset(tmp+1, 0, sizeof(nr)-1);
 
1618
    tmp[0]=(uchar) 128;
 
1619
    bzero((char*) tmp+1,sizeof(nr)-1);
1578
1620
  }
1579
1621
  else
1580
1622
  {
1581
1623
#ifdef WORDS_BIGENDIAN
1582
 
    memcpy(tmp,&nr,sizeof(nr));
 
1624
    memcpy_fixed(tmp,&nr,sizeof(nr));
1583
1625
#else
1584
1626
    {
1585
 
      unsigned char *ptr= (unsigned char*) &nr;
 
1627
      uchar *ptr= (uchar*) &nr;
1586
1628
#if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
1587
1629
      tmp[0]= ptr[3]; tmp[1]=ptr[2]; tmp[2]= ptr[1]; tmp[3]=ptr[0];
1588
1630
      tmp[4]= ptr[7]; tmp[5]=ptr[6]; tmp[6]= ptr[5]; tmp[7]=ptr[4];
1594
1636
#endif
1595
1637
    if (tmp[0] & 128)                           /* Negative */
1596
1638
    {                                           /* make complement */
1597
 
      uint32_t i;
 
1639
      uint i;
1598
1640
      for (i=0 ; i < sizeof(nr); i++)
1599
 
        tmp[i]=tmp[i] ^ (unsigned char) 255;
 
1641
        tmp[i]=tmp[i] ^ (uchar) 255;
1600
1642
    }
1601
1643
    else
1602
1644
    {                                   /* 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;
 
1645
      ushort exp_part=(((ushort) tmp[0] << 8) | (ushort) tmp[1] |
 
1646
                       (ushort) 32768);
 
1647
      exp_part+= (ushort) 1 << (16-1-DBL_EXP_DIG);
 
1648
      tmp[0]= (uchar) (exp_part >> 8);
 
1649
      tmp[1]= (uchar) exp_part;
1608
1650
    }
1609
1651
  }
1610
1652
}