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))
356
356
/** Make a array of string pointers. */
358
static char **make_char_array(char **old_pos, register uint fields,
359
uint length, myf my_flag)
358
static char **make_char_array(char **old_pos, register uint32_t fields,
359
uint32_t length, myf my_flag)
361
361
register char **pos;
365
(old_pos= (char**) my_malloc((uint) fields*(length+sizeof(char*)),
365
(old_pos= (char**) my_malloc((uint32_t) fields*(length+sizeof(char*)),
368
368
pos=old_pos; char_pos=((char*) (pos+fields)) -length;
440
440
IO_CACHE *tempfile, IO_CACHE *indexfile)
442
442
int error,flag,quick_select;
443
uint idx,indexpos,ref_length;
443
uint32_t idx,indexpos,ref_length;
444
444
uchar *ref_pos,*next_pos,ref_buff[MAX_REFLENGTH];
446
446
Table *sort_form;
618
write_keys(SORTPARAM *param, register uchar **sort_keys, uint count,
618
write_keys(SORTPARAM *param, register uchar **sort_keys, uint32_t count,
619
619
IO_CACHE *buffpek_pointers, IO_CACHE *tempfile)
621
621
size_t sort_length, rec_length;
625
625
sort_length= param->sort_length;
626
626
rec_length= param->rec_length;
627
my_string_ptr_sort((uchar*) sort_keys, (uint) count, sort_length);
627
my_string_ptr_sort((uchar*) sort_keys, (uint32_t) count, sort_length);
628
628
if (!my_b_inited(tempfile) &&
629
629
open_cached_file(tempfile, mysql_tmpdir, TEMP_PREFIX, DISK_BUFFER_SIZE,
635
635
buffpek.file_pos= my_b_tell(tempfile);
636
636
if ((ha_rows) count > param->max_rows)
637
count=(uint) param->max_rows; /* purecov: inspected */
637
count=(uint32_t) param->max_rows; /* purecov: inspected */
638
638
buffpek.count=(ha_rows) count;
639
639
for (end=sort_keys+count ; sort_keys != end ; sort_keys++)
640
if (my_b_write(tempfile, (uchar*) *sort_keys, (uint) rec_length))
640
if (my_b_write(tempfile, (uchar*) *sort_keys, (uint32_t) rec_length))
642
642
if (my_b_write(buffpek_pointers, (uchar*) &buffpek, sizeof(buffpek)))
960
static bool save_index(SORTPARAM *param, uchar **sort_keys, uint count,
960
static bool save_index(SORTPARAM *param, uchar **sort_keys, uint32_t count,
961
961
filesort_info_st *table_sort)
963
uint offset,res_length;
963
uint32_t offset,res_length;
966
my_string_ptr_sort((uchar*) sort_keys, (uint) count, param->sort_length);
966
my_string_ptr_sort((uchar*) sort_keys, (uint32_t) count, param->sort_length);
967
967
res_length= param->res_length;
968
968
offset= param->rec_length-res_length;
969
969
if ((ha_rows) count > param->max_rows)
970
count=(uint) param->max_rows;
970
count=(uint32_t) param->max_rows;
971
971
if (!(to= table_sort->record_pointers=
972
972
(uchar*) my_malloc(res_length*count, MYF(MY_WME))))
973
973
return(1); /* purecov: inspected */
983
983
/** Merge buffers to make < MERGEBUFF2 buffers. */
985
985
int merge_many_buff(SORTPARAM *param, uchar *sort_buffer,
986
BUFFPEK *buffpek, uint *maxbuffer, IO_CACHE *t_file)
986
BUFFPEK *buffpek, uint32_t *maxbuffer, IO_CACHE *t_file)
989
989
IO_CACHE t_file2,*from_file,*to_file,*temp;
990
990
BUFFPEK *lastbuff;
1036
1036
Read data to buffer.
1039
(uint)-1 if something goes wrong
1039
(uint32_t)-1 if something goes wrong
1042
uint read_to_buffer(IO_CACHE *fromfile, BUFFPEK *buffpek,
1042
uint32_t read_to_buffer(IO_CACHE *fromfile, BUFFPEK *buffpek,
1043
uint32_t rec_length)
1045
register uint count;
1045
register uint32_t count;
1048
if ((count=(uint) cmin((ha_rows) buffpek->max_keys,buffpek->count)))
1048
if ((count=(uint32_t) cmin((ha_rows) buffpek->max_keys,buffpek->count)))
1050
1050
if (pread(fromfile->file,(uchar*) buffpek->base, (length= rec_length*count),buffpek->file_pos) == 0)
1051
return((uint) -1); /* purecov: inspected */
1051
return((uint32_t) -1); /* purecov: inspected */
1052
1052
buffpek->key=buffpek->base;
1053
1053
buffpek->file_pos+= length; /* New filepos */
1054
1054
buffpek->count-= count;
1069
1069
@param[in] key_length key length
1072
void reuse_freed_buff(QUEUE *queue, BUFFPEK *reuse, uint key_length)
1072
void reuse_freed_buff(QUEUE *queue, BUFFPEK *reuse, uint32_t key_length)
1074
1074
uchar *reuse_end= reuse->base + reuse->max_keys * key_length;
1075
for (uint i= 0; i < queue->elements; ++i)
1075
for (uint32_t i= 0; i < queue->elements; ++i)
1077
1077
BUFFPEK *bp= (BUFFPEK *) queue_element(queue, i);
1078
1078
if (bp->base + bp->max_keys * key_length == reuse->base)
1140
1140
res_length= param->res_length;
1141
1141
sort_length= param->sort_length;
1142
1142
offset= rec_length-res_length;
1143
maxcount= (uint32_t) (param->keys/((uint) (Tb-Fb) +1));
1143
maxcount= (uint32_t) (param->keys/((uint32_t) (Tb-Fb) +1));
1144
1144
to_start_filepos= my_b_tell(to_file);
1145
1145
strpos= (uchar*) sort_buffer;
1146
1146
org_max_rows=max_rows= param->max_rows;
1158
1158
cmp= get_ptr_compare(sort_length);
1159
1159
first_cmp_arg= (void*) &sort_length;
1161
if (init_queue(&queue, (uint) (Tb-Fb)+1, offsetof(BUFFPEK,key), 0,
1161
if (init_queue(&queue, (uint32_t) (Tb-Fb)+1, offsetof(BUFFPEK,key), 0,
1162
1162
(queue_compare) cmp, first_cmp_arg))
1163
1163
return(1); /* purecov: inspected */
1164
1164
for (buffpek= Fb ; buffpek <= Tb ; buffpek++)
1166
1166
buffpek->base= strpos;
1167
1167
buffpek->max_keys= maxcount;
1168
strpos+= (uint) (error= (int) read_to_buffer(from_file, buffpek,
1168
strpos+= (uint32_t) (error= (int) read_to_buffer(from_file, buffpek,
1170
1170
if (error == -1)
1171
1171
goto err; /* purecov: inspected */
1317
1317
/* Do a merge to output-file (save only positions) */
1319
1319
static int merge_index(SORTPARAM *param, uchar *sort_buffer,
1320
BUFFPEK *buffpek, uint maxbuffer,
1320
BUFFPEK *buffpek, uint32_t maxbuffer,
1321
1321
IO_CACHE *tempfile, IO_CACHE *outfile)
1323
1323
if (merge_buffers(param,tempfile,outfile,sort_buffer,buffpek,buffpek,
1358
1358
Total length of sort buffer in bytes
1362
sortlength(THD *thd, SORT_FIELD *sortorder, uint s_length,
1362
sortlength(THD *thd, SORT_FIELD *sortorder, uint32_t s_length,
1363
1363
bool *multi_byte_charset)
1365
register uint length;
1365
register uint32_t length;
1366
1366
const CHARSET_INFO *cs;
1367
1367
*multi_byte_charset= 0;
1470
1470
static SORT_ADDON_FIELD *
1471
get_addon_fields(THD *thd, Field **ptabfield, uint sortlength, uint *plength)
1471
get_addon_fields(THD *thd, Field **ptabfield, uint32_t sortlength, uint32_t *plength)
1473
1473
Field **pfield;
1475
1475
SORT_ADDON_FIELD *addonf;
1478
uint null_fields= 0;
1478
uint32_t null_fields= 0;
1479
1479
MY_BITMAP *read_set= (*ptabfield)->table->read_set;
1606
1606
if (tmp[0] & 128) /* Negative */
1607
1607
{ /* make complement */
1609
1609
for (i=0 ; i < sizeof(nr); i++)
1610
1610
tmp[i]=tmp[i] ^ (uchar) 255;
1613
1613
{ /* Set high and move exponent one up */
1614
ushort exp_part=(((ushort) tmp[0] << 8) | (ushort) tmp[1] |
1616
exp_part+= (ushort) 1 << (16-1-DBL_EXP_DIG);
1614
uint16_t exp_part=(((uint16_t) tmp[0] << 8) | (uint16_t) tmp[1] |
1616
exp_part+= (uint16_t) 1 << (16-1-DBL_EXP_DIG);
1617
1617
tmp[0]= (uchar) (exp_part >> 8);
1618
1618
tmp[1]= (uchar) exp_part;