24
#include <drizzled/server_includes.h>
25
#include <drizzled/sql_sort.h>
26
#include <drizzled/error.h>
27
#include <drizzled/probes.h>
28
#include <drizzled/session.h>
29
#include <drizzled/table.h>
30
#include <drizzled/table_list.h>
30
33
#include <algorithm>
32
#include "drizzled/sql_sort.h"
33
#include "drizzled/error.h"
34
#include "drizzled/probes.h"
35
#include "drizzled/session.h"
36
#include "drizzled/table.h"
37
#include "drizzled/table_list.h"
38
#include "drizzled/optimizer/range.h"
39
#include "drizzled/records.h"
40
#include "drizzled/internal/iocache.h"
41
#include "drizzled/internal/my_sys.h"
42
#include "plugin/myisam/myisam.h"
43
#include "drizzled/plugin/transactional_storage_engine.h"
45
35
using namespace std;
50
37
/* functions defined in this file */
52
39
static char **make_char_array(char **old_pos, register uint32_t fields,
55
static unsigned char *read_buffpek_from_file(internal::IO_CACHE *buffer_file,
59
static ha_rows find_all_keys(SORTPARAM *param,
60
optimizer::SqlSelect *select,
61
unsigned char * *sort_keys,
62
internal::IO_CACHE *buffer_file,
63
internal::IO_CACHE *tempfile,
64
internal::IO_CACHE *indexfile);
66
static int write_keys(SORTPARAM *param,
67
unsigned char * *sort_keys,
69
internal::IO_CACHE *buffer_file,
70
internal::IO_CACHE *tempfile);
72
static void make_sortkey(SORTPARAM *param,
74
unsigned char *ref_pos);
41
static unsigned char *read_buffpek_from_file(IO_CACHE *buffer_file, uint32_t count,
43
static ha_rows find_all_keys(SORTPARAM *param,SQL_SELECT *select,
44
unsigned char * *sort_keys, IO_CACHE *buffer_file,
45
IO_CACHE *tempfile,IO_CACHE *indexfile);
46
static int write_keys(SORTPARAM *param,unsigned char * *sort_keys,
47
uint32_t count, IO_CACHE *buffer_file, IO_CACHE *tempfile);
48
static void make_sortkey(SORTPARAM *param,unsigned char *to, unsigned char *ref_pos);
75
49
static void register_used_fields(SORTPARAM *param);
76
static int merge_index(SORTPARAM *param,
77
unsigned char *sort_buffer,
50
static int merge_index(SORTPARAM *param,unsigned char *sort_buffer,
80
internal::IO_CACHE *tempfile,
81
internal::IO_CACHE *outfile);
82
static bool save_index(SORTPARAM *param,
83
unsigned char **sort_keys,
52
uint32_t maxbuffer,IO_CACHE *tempfile,
54
static bool save_index(SORTPARAM *param,unsigned char **sort_keys, uint32_t count,
85
55
filesort_info_st *table_sort);
86
56
static uint32_t suffix_length(uint32_t string_length);
87
static uint32_t sortlength(Session *session,
88
SORT_FIELD *sortorder,
90
bool *multi_byte_charset);
91
static SORT_ADDON_FIELD *get_addon_fields(Session *session,
57
static uint32_t sortlength(Session *session, SORT_FIELD *sortorder, uint32_t s_length,
58
bool *multi_byte_charset);
59
static SORT_ADDON_FIELD *get_addon_fields(Session *session, Field **ptabfield,
60
uint32_t sortlength, uint32_t *plength);
95
61
static void unpack_addon_fields(struct st_sort_addon_field *addon_field,
96
62
unsigned char *buff);
148
114
TableList *tab= table->pos_in_table_list;
149
115
Item_subselect *subselect= tab ? tab->containing_subselect() : 0;
151
DRIZZLE_FILESORT_START(table->s->getSchemaName(), table->s->getTableName());
117
DRIZZLE_FILESORT_START();
154
120
Release InnoDB's adaptive hash index latch (if holding) before
157
plugin::TransactionalStorageEngine::releaseTemporaryLatches(session);
123
ha_release_temporary_latches(session);
160
126
Don't use table->sort in filesort as it is also used by
161
QuickIndexMergeSelect. Work with a copy and put it back at the end
127
QUICK_INDEX_MERGE_SELECT. Work with a copy and put it back at the end
162
128
when index_merge select has finished with it.
164
130
memcpy(&table_sort, &table->sort, sizeof(filesort_info_st));
475
441
HA_POS_ERROR on error.
478
static ha_rows find_all_keys(SORTPARAM *param,
479
optimizer::SqlSelect *select,
444
static ha_rows find_all_keys(SORTPARAM *param, SQL_SELECT *select,
480
445
unsigned char **sort_keys,
481
internal::IO_CACHE *buffpek_pointers,
482
internal::IO_CACHE *tempfile, internal::IO_CACHE *indexfile)
446
IO_CACHE *buffpek_pointers,
447
IO_CACHE *tempfile, IO_CACHE *indexfile)
484
449
int error,flag,quick_select;
485
450
uint32_t idx,indexpos,ref_length;
486
451
unsigned char *ref_pos,*next_pos,ref_buff[MAX_REFLENGTH];
487
internal::my_off_t record;
488
453
Table *sort_form;
489
454
Session *session= current_session;
490
455
volatile Session::killed_state *killed= &session->killed;
492
457
MyBitmap *save_read_set, *save_write_set;
495
460
error=quick_select=0;
496
461
sort_form=param->sort_form;
497
file= sort_form->cursor;
462
file=sort_form->file;
498
463
ref_length=param->ref_length;
499
464
ref_pos= ref_buff;
500
465
quick_select=select && select->quick;
502
flag= ((!indexfile && ! file->isOrdered())
467
flag= ((!indexfile && file->ha_table_flags() & HA_REC_NOT_IN_SEQ)
503
468
|| quick_select);
504
469
if (indexfile || flag)
505
470
ref_pos= &file->ref[0];
668
633
sort_length= param->sort_length;
669
634
rec_length= param->rec_length;
670
internal::my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, sort_length);
635
my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, sort_length);
671
636
if (!my_b_inited(tempfile) &&
672
637
open_cached_file(tempfile, drizzle_tmpdir, TEMP_PREFIX, DISK_BUFFER_SIZE,
639
goto err; /* purecov: inspected */
675
640
/* check we won't have more buffpeks than we can possibly keep in memory */
676
641
if (my_b_tell(buffpek_pointers) + sizeof(BUFFPEK) > (uint64_t)UINT_MAX)
678
643
buffpek.file_pos= my_b_tell(tempfile);
679
644
if ((ha_rows) count > param->max_rows)
680
count=(uint32_t) param->max_rows;
645
count=(uint32_t) param->max_rows; /* purecov: inspected */
681
646
buffpek.count=(ha_rows) count;
682
647
for (end=sort_keys+count ; sort_keys != end ; sort_keys++)
683
648
if (my_b_write(tempfile, (unsigned char*) *sort_keys, (uint32_t) rec_length))
993
960
uint32_t offset,res_length;
994
961
unsigned char *to;
996
internal::my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, param->sort_length);
963
my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, param->sort_length);
997
964
res_length= param->res_length;
998
965
offset= param->rec_length-res_length;
999
966
if ((ha_rows) count > param->max_rows)
1000
967
count=(uint32_t) param->max_rows;
1001
968
if (!(to= table_sort->record_pointers=
1002
969
(unsigned char*) malloc(res_length*count)))
970
return(1); /* purecov: inspected */
1004
971
for (unsigned char **end= sort_keys+count ; sort_keys != end ; sort_keys++)
1006
973
memcpy(to, *sort_keys+offset, res_length);
1013
980
/** Merge buffers to make < MERGEBUFF2 buffers. */
1015
982
int merge_many_buff(SORTPARAM *param, unsigned char *sort_buffer,
1016
BUFFPEK *buffpek, uint32_t *maxbuffer, internal::IO_CACHE *t_file)
983
BUFFPEK *buffpek, uint32_t *maxbuffer, IO_CACHE *t_file)
1018
985
register uint32_t i;
1019
internal::IO_CACHE t_file2,*from_file,*to_file,*temp;
986
IO_CACHE t_file2,*from_file,*to_file,*temp;
1020
987
BUFFPEK *lastbuff;
1022
989
if (*maxbuffer < MERGEBUFF2)
990
return(0); /* purecov: inspected */
1024
991
if (flush_io_cache(t_file) ||
1025
992
open_cached_file(&t_file2,drizzle_tmpdir,TEMP_PREFIX,DISK_BUFFER_SIZE,
994
return(1); /* purecov: inspected */
1029
996
from_file= t_file ; to_file= &t_file2;
1030
997
while (*maxbuffer >= MERGEBUFF2)
1032
if (reinit_io_cache(from_file,internal::READ_CACHE,0L,0,0))
999
if (reinit_io_cache(from_file,READ_CACHE,0L,0,0))
1034
if (reinit_io_cache(to_file,internal::WRITE_CACHE,0L,0,0))
1001
if (reinit_io_cache(to_file,WRITE_CACHE,0L,0,0))
1036
1003
lastbuff=buffpek;
1037
1004
for (i=0 ; i <= *maxbuffer-MERGEBUFF*3/2 ; i+=MERGEBUFF)