28
28
/* functions defined in this file */
30
static char **make_char_array(char **old_pos, register uint fields,
31
uint length, myf my_flag);
32
static uchar *read_buffpek_from_file(IO_CACHE *buffer_file, uint count,
30
static char **make_char_array(char **old_pos, register uint32_t fields,
31
uint32_t length, myf my_flag);
32
static uchar *read_buffpek_from_file(IO_CACHE *buffer_file, uint32_t count,
34
34
static ha_rows find_all_keys(SORTPARAM *param,SQL_SELECT *select,
35
35
uchar * *sort_keys, IO_CACHE *buffer_file,
36
36
IO_CACHE *tempfile,IO_CACHE *indexfile);
37
37
static int write_keys(SORTPARAM *param,uchar * *sort_keys,
38
uint count, IO_CACHE *buffer_file, IO_CACHE *tempfile);
38
uint32_t count, IO_CACHE *buffer_file, IO_CACHE *tempfile);
39
39
static void make_sortkey(SORTPARAM *param,uchar *to, uchar *ref_pos);
40
40
static void register_used_fields(SORTPARAM *param);
41
41
static int merge_index(SORTPARAM *param,uchar *sort_buffer,
43
uint maxbuffer,IO_CACHE *tempfile,
43
uint32_t maxbuffer,IO_CACHE *tempfile,
44
44
IO_CACHE *outfile);
45
static bool save_index(SORTPARAM *param,uchar **sort_keys, uint count,
45
static bool save_index(SORTPARAM *param,uchar **sort_keys, uint32_t count,
46
46
filesort_info_st *table_sort);
47
static uint suffix_length(uint32_t string_length);
48
static uint sortlength(THD *thd, SORT_FIELD *sortorder, uint s_length,
47
static uint32_t suffix_length(uint32_t string_length);
48
static uint32_t sortlength(THD *thd, SORT_FIELD *sortorder, uint32_t s_length,
49
49
bool *multi_byte_charset);
50
50
static SORT_ADDON_FIELD *get_addon_fields(THD *thd, Field **ptabfield,
51
uint sortlength, uint *plength);
51
uint32_t sortlength, uint32_t *plength);
52
52
static void unpack_addon_fields(struct st_sort_addon_field *addon_field,
87
87
examined_rows will be set to number of examined rows
90
ha_rows filesort(THD *thd, Table *table, SORT_FIELD *sortorder, uint s_length,
90
ha_rows filesort(THD *thd, Table *table, SORT_FIELD *sortorder, uint32_t s_length,
91
91
SQL_SELECT *select, ha_rows max_rows,
92
92
bool sort_positions, ha_rows *examined_rows)
95
95
uint32_t memavl, min_sort_memory;
98
98
ha_rows records= HA_POS_ERROR;
99
99
uchar **sort_keys= 0;
200
200
memavl= thd->variables.sortbuff_size;
201
min_sort_memory= cmax((uint)MIN_SORT_MEMORY, param.sort_length*MERGEBUFF2);
201
min_sort_memory= cmax((uint32_t)MIN_SORT_MEMORY, param.sort_length*MERGEBUFF2);
202
202
while (memavl >= min_sort_memory)
204
204
uint32_t old_memavl;
205
205
uint32_t keys= memavl/(param.rec_length+sizeof(char*));
206
param.keys=(uint) cmin(records+1, keys);
206
param.keys=(uint32_t) cmin(records+1, keys);
207
207
if ((table_sort.sort_keys=
208
208
(uchar **) make_char_array((char **) table_sort.sort_keys,
209
209
param.keys, param.rec_length, MYF(0))))
229
229
&tempfile, selected_records_file)) ==
232
maxbuffer= (uint) (my_b_tell(&buffpek_pointers)/sizeof(*buffpek));
232
maxbuffer= (uint32_t) (my_b_tell(&buffpek_pointers)/sizeof(*buffpek));
234
234
if (maxbuffer == 0) // The whole set is in memory
236
if (save_index(¶m,sort_keys,(uint) records, &table_sort))
236
if (save_index(¶m,sort_keys,(uint32_t) records, &table_sort))
350
350
/** Make a array of string pointers. */
352
static char **make_char_array(char **old_pos, register uint fields,
353
uint length, myf my_flag)
352
static char **make_char_array(char **old_pos, register uint32_t fields,
353
uint32_t length, myf my_flag)
355
355
register char **pos;
359
(old_pos= (char**) my_malloc((uint) fields*(length+sizeof(char*)),
359
(old_pos= (char**) my_malloc((uint32_t) fields*(length+sizeof(char*)),
362
362
pos=old_pos; char_pos=((char*) (pos+fields)) -length;
434
434
IO_CACHE *tempfile, IO_CACHE *indexfile)
436
436
int error,flag,quick_select;
437
uint idx,indexpos,ref_length;
437
uint32_t idx,indexpos,ref_length;
438
438
uchar *ref_pos,*next_pos,ref_buff[MAX_REFLENGTH];
440
440
Table *sort_form;
612
write_keys(SORTPARAM *param, register uchar **sort_keys, uint count,
612
write_keys(SORTPARAM *param, register uchar **sort_keys, uint32_t count,
613
613
IO_CACHE *buffpek_pointers, IO_CACHE *tempfile)
615
615
size_t sort_length, rec_length;
619
619
sort_length= param->sort_length;
620
620
rec_length= param->rec_length;
621
my_string_ptr_sort((uchar*) sort_keys, (uint) count, sort_length);
621
my_string_ptr_sort((uchar*) sort_keys, (uint32_t) count, sort_length);
622
622
if (!my_b_inited(tempfile) &&
623
623
open_cached_file(tempfile, mysql_tmpdir, TEMP_PREFIX, DISK_BUFFER_SIZE,
629
629
buffpek.file_pos= my_b_tell(tempfile);
630
630
if ((ha_rows) count > param->max_rows)
631
count=(uint) param->max_rows; /* purecov: inspected */
631
count=(uint32_t) param->max_rows; /* purecov: inspected */
632
632
buffpek.count=(ha_rows) count;
633
633
for (end=sort_keys+count ; sort_keys != end ; sort_keys++)
634
if (my_b_write(tempfile, (uchar*) *sort_keys, (uint) rec_length))
634
if (my_b_write(tempfile, (uchar*) *sort_keys, (uint32_t) rec_length))
636
636
if (my_b_write(buffpek_pointers, (uchar*) &buffpek, sizeof(buffpek)))
954
static bool save_index(SORTPARAM *param, uchar **sort_keys, uint count,
954
static bool save_index(SORTPARAM *param, uchar **sort_keys, uint32_t count,
955
955
filesort_info_st *table_sort)
957
uint offset,res_length;
957
uint32_t offset,res_length;
960
my_string_ptr_sort((uchar*) sort_keys, (uint) count, param->sort_length);
960
my_string_ptr_sort((uchar*) sort_keys, (uint32_t) count, param->sort_length);
961
961
res_length= param->res_length;
962
962
offset= param->rec_length-res_length;
963
963
if ((ha_rows) count > param->max_rows)
964
count=(uint) param->max_rows;
964
count=(uint32_t) param->max_rows;
965
965
if (!(to= table_sort->record_pointers=
966
966
(uchar*) my_malloc(res_length*count, MYF(MY_WME))))
967
967
return(1); /* purecov: inspected */
977
977
/** Merge buffers to make < MERGEBUFF2 buffers. */
979
979
int merge_many_buff(SORTPARAM *param, uchar *sort_buffer,
980
BUFFPEK *buffpek, uint *maxbuffer, IO_CACHE *t_file)
980
BUFFPEK *buffpek, uint32_t *maxbuffer, IO_CACHE *t_file)
983
983
IO_CACHE t_file2,*from_file,*to_file,*temp;
984
984
BUFFPEK *lastbuff;
1030
1030
Read data to buffer.
1033
(uint)-1 if something goes wrong
1033
(uint32_t)-1 if something goes wrong
1036
uint read_to_buffer(IO_CACHE *fromfile, BUFFPEK *buffpek,
1036
uint32_t read_to_buffer(IO_CACHE *fromfile, BUFFPEK *buffpek,
1037
uint32_t rec_length)
1039
register uint count;
1039
register uint32_t count;
1042
if ((count=(uint) cmin((ha_rows) buffpek->max_keys,buffpek->count)))
1042
if ((count=(uint32_t) cmin((ha_rows) buffpek->max_keys,buffpek->count)))
1044
1044
if (pread(fromfile->file,(uchar*) buffpek->base, (length= rec_length*count),buffpek->file_pos) == 0)
1045
return((uint) -1); /* purecov: inspected */
1045
return((uint32_t) -1); /* purecov: inspected */
1046
1046
buffpek->key=buffpek->base;
1047
1047
buffpek->file_pos+= length; /* New filepos */
1048
1048
buffpek->count-= count;
1063
1063
@param[in] key_length key length
1066
void reuse_freed_buff(QUEUE *queue, BUFFPEK *reuse, uint key_length)
1066
void reuse_freed_buff(QUEUE *queue, BUFFPEK *reuse, uint32_t key_length)
1068
1068
uchar *reuse_end= reuse->base + reuse->max_keys * key_length;
1069
for (uint i= 0; i < queue->elements; ++i)
1069
for (uint32_t i= 0; i < queue->elements; ++i)
1071
1071
BUFFPEK *bp= (BUFFPEK *) queue_element(queue, i);
1072
1072
if (bp->base + bp->max_keys * key_length == reuse->base)
1134
1134
res_length= param->res_length;
1135
1135
sort_length= param->sort_length;
1136
1136
offset= rec_length-res_length;
1137
maxcount= (uint32_t) (param->keys/((uint) (Tb-Fb) +1));
1137
maxcount= (uint32_t) (param->keys/((uint32_t) (Tb-Fb) +1));
1138
1138
to_start_filepos= my_b_tell(to_file);
1139
1139
strpos= (uchar*) sort_buffer;
1140
1140
org_max_rows=max_rows= param->max_rows;
1152
1152
cmp= get_ptr_compare(sort_length);
1153
1153
first_cmp_arg= (void*) &sort_length;
1155
if (init_queue(&queue, (uint) (Tb-Fb)+1, offsetof(BUFFPEK,key), 0,
1155
if (init_queue(&queue, (uint32_t) (Tb-Fb)+1, offsetof(BUFFPEK,key), 0,
1156
1156
(queue_compare) cmp, first_cmp_arg))
1157
1157
return(1); /* purecov: inspected */
1158
1158
for (buffpek= Fb ; buffpek <= Tb ; buffpek++)
1160
1160
buffpek->base= strpos;
1161
1161
buffpek->max_keys= maxcount;
1162
strpos+= (uint) (error= (int) read_to_buffer(from_file, buffpek,
1162
strpos+= (uint32_t) (error= (int) read_to_buffer(from_file, buffpek,
1164
1164
if (error == -1)
1165
1165
goto err; /* purecov: inspected */
1311
1311
/* Do a merge to output-file (save only positions) */
1313
1313
static int merge_index(SORTPARAM *param, uchar *sort_buffer,
1314
BUFFPEK *buffpek, uint maxbuffer,
1314
BUFFPEK *buffpek, uint32_t maxbuffer,
1315
1315
IO_CACHE *tempfile, IO_CACHE *outfile)
1317
1317
if (merge_buffers(param,tempfile,outfile,sort_buffer,buffpek,buffpek,
1352
1352
Total length of sort buffer in bytes
1356
sortlength(THD *thd, SORT_FIELD *sortorder, uint s_length,
1356
sortlength(THD *thd, SORT_FIELD *sortorder, uint32_t s_length,
1357
1357
bool *multi_byte_charset)
1359
register uint length;
1359
register uint32_t length;
1360
1360
const CHARSET_INFO *cs;
1361
1361
*multi_byte_charset= 0;
1464
1464
static SORT_ADDON_FIELD *
1465
get_addon_fields(THD *thd, Field **ptabfield, uint sortlength, uint *plength)
1465
get_addon_fields(THD *thd, Field **ptabfield, uint32_t sortlength, uint32_t *plength)
1467
1467
Field **pfield;
1469
1469
SORT_ADDON_FIELD *addonf;
1472
uint null_fields= 0;
1472
uint32_t null_fields= 0;
1473
1473
MY_BITMAP *read_set= (*ptabfield)->table->read_set;
1600
1600
if (tmp[0] & 128) /* Negative */
1601
1601
{ /* make complement */
1603
1603
for (i=0 ; i < sizeof(nr); i++)
1604
1604
tmp[i]=tmp[i] ^ (uchar) 255;
1607
1607
{ /* Set high and move exponent one up */
1608
ushort exp_part=(((ushort) tmp[0] << 8) | (ushort) tmp[1] |
1610
exp_part+= (ushort) 1 << (16-1-DBL_EXP_DIG);
1608
uint16_t exp_part=(((uint16_t) tmp[0] << 8) | (uint16_t) tmp[1] |
1610
exp_part+= (uint16_t) 1 << (16-1-DBL_EXP_DIG);
1611
1611
tmp[0]= (uchar) (exp_part >> 8);
1612
1612
tmp[1]= (uchar) exp_part;