drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
filesort.cc
Go to the documentation of this file.
1/* Copyright (C) 2000-2006 MySQL AB
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15
16
23
24#include <config.h>
25
26#include <float.h>
27#include <limits.h>
28
29#include <queue>
30#include <algorithm>
31#include <iostream>
32
33#include <drizzled/drizzled.h>
34#include <drizzled/sql_sort.h>
35#include <drizzled/filesort.h>
36#include <drizzled/error.h>
37#include <drizzled/probes.h>
38#include <drizzled/session.h>
39#include <drizzled/table.h>
40#include <drizzled/table_list.h>
41#include <drizzled/optimizer/range.h>
42#include <drizzled/records.h>
43#include <drizzled/internal/iocache.h>
44#include <drizzled/internal/my_sys.h>
45#include <plugin/myisam/myisam.h>
46#include <drizzled/plugin/transactional_storage_engine.h>
47#include <drizzled/atomics.h>
48#include <drizzled/global_buffer.h>
49#include <drizzled/sort_field.h>
50#include <drizzled/item/subselect.h>
51#include <drizzled/statistics_variables.h>
52#include <drizzled/system_variables.h>
53
54#include <drizzled/item/cmpfunc.h>
55
56using namespace std;
57
58namespace drizzled {
59
60/* Defines used by filesort and uniques */
61#define MERGEBUFF 7
62#define MERGEBUFF2 15
63
64class BufferCompareContext
65{
66public:
67 qsort_cmp2 key_compare;
68 void *key_compare_arg;
69
70 BufferCompareContext() :
71 key_compare(0),
72 key_compare_arg(0)
73 { }
74
75};
76
77class SortParam {
78public:
79 uint32_t rec_length; /* Length of sorted records */
80 uint32_t sort_length; /* Length of sorted columns */
81 uint32_t ref_length; /* Length of record ref. */
82 uint32_t addon_length; /* Length of added packed fields */
83 uint32_t res_length; /* Length of records in final sorted file/buffer */
84 uint32_t keys; /* Max keys / buffer */
85 ha_rows max_rows,examined_rows;
86 Table *sort_form; /* For quicker make_sortkey */
87 SortField *local_sortorder;
88 SortField *end;
89 sort_addon_field *addon_field; /* Descriptors for companion fields */
90 unsigned char *unique_buff;
91 bool not_killable;
92 char *tmp_buffer;
93 /* The fields below are used only by Unique class */
94 qsort2_cmp compare;
95 BufferCompareContext cmp_context;
96
97 SortParam() :
98 rec_length(0),
99 sort_length(0),
100 ref_length(0),
101 addon_length(0),
102 res_length(0),
103 keys(0),
104 max_rows(0),
105 examined_rows(0),
106 sort_form(0),
107 local_sortorder(0),
108 end(0),
109 addon_field(0),
110 unique_buff(0),
111 not_killable(0),
112 tmp_buffer(0),
113 compare(0)
114 {
115 }
116
117 ~SortParam()
118 {
119 free(tmp_buffer);
120 }
121
122 int write_keys(unsigned char * *sort_keys,
123 uint32_t count,
124 internal::io_cache_st *buffer_file,
125 internal::io_cache_st *tempfile);
126
127 void make_sortkey(unsigned char *to,
128 unsigned char *ref_pos);
129 void register_used_fields();
130 void save_index(unsigned char **sort_keys,
131 uint32_t count,
132 filesort_info *table_sort);
133
134};
135
136/* functions defined in this file */
137
138static char **make_char_array(char **old_pos, uint32_t fields,
139 uint32_t length);
140
141static unsigned char *read_buffpek_from_file(internal::io_cache_st *buffer_file,
142 uint32_t count,
143 unsigned char *buf);
144
145static uint32_t suffix_length(uint32_t string_length);
146static void unpack_addon_fields(sort_addon_field *addon_field,
147 unsigned char *buff);
148
149FileSort::FileSort(Session &arg) :
150 _session(arg)
151{
152}
153
188
189ha_rows FileSort::run(Table *table, SortField *sortorder, uint32_t s_length,
190 optimizer::SqlSelect *select, ha_rows max_rows,
191 bool sort_positions, ha_rows &examined_rows)
192{
193 int error= 1;
194 uint32_t memavl= 0, min_sort_memory;
195 uint32_t maxbuffer;
196 size_t allocated_sort_memory= 0;
197 buffpek *buffpek_inst= 0;
198 ha_rows records= HA_POS_ERROR;
199 unsigned char **sort_keys= 0;
200 internal::io_cache_st tempfile;
201 internal::io_cache_st buffpek_pointers;
202 internal::io_cache_st *selected_records_file;
203 internal::io_cache_st *outfile;
204 SortParam param;
205 bool multi_byte_charset;
206
207 /*
208 Don't use table->sort in filesort as it is also used by
209 QuickIndexMergeSelect. Work with a copy and put it back at the end
210 when index_merge select has finished with it.
211 */
212 filesort_info table_sort(table->sort);
213 table->sort.io_cache= NULL;
214
215 TableList *tab= table->pos_in_table_list;
216 Item_subselect *subselect= tab ? tab->containing_subselect() : 0;
217
218 DRIZZLE_FILESORT_START(table->getShare()->getSchemaName(), table->getShare()->getTableName());
219
220 /*
221 Release InnoDB's adaptive hash index latch (if holding) before
222 running a sort.
223 */
225
226
227 outfile= table_sort.io_cache;
228 assert(tempfile.buffer == 0);
229 assert(buffpek_pointers.buffer == 0);
230
231 param.sort_length= sortlength(sortorder, s_length, &multi_byte_charset);
232 param.ref_length= table->cursor->ref_length;
233
234 if (!(table->cursor->getEngine()->check_flag(HTON_BIT_FAST_KEY_READ)) && !sort_positions)
235 {
236 /*
237 Get the descriptors of all fields whose values are appended
238 to sorted fields and get its total length in param.spack_length.
239 */
240 param.addon_field= get_addon_fields(table->getFields(),
241 param.sort_length,
242 &param.addon_length);
243 }
244
245 table_sort.addon_buf= 0;
246 table_sort.addon_length= param.addon_length;
247 table_sort.addon_field= param.addon_field;
248 table_sort.unpack= unpack_addon_fields;
249 if (param.addon_field)
250 {
251 param.res_length= param.addon_length;
252 table_sort.addon_buf= (unsigned char *) malloc(param.addon_length);
253 }
254 else
255 {
256 param.res_length= param.ref_length;
257 /*
258 The reference to the record is considered
259 as an additional sorted field
260 */
261 param.sort_length+= param.ref_length;
262 }
263 param.rec_length= param.sort_length+param.addon_length;
264 param.max_rows= max_rows;
265
266 if (select && select->quick)
267 {
268 getSession().status_var.filesort_range_count++;
269 }
270 else
271 {
272 getSession().status_var.filesort_scan_count++;
273 }
274#ifdef CAN_TRUST_RANGE
275 if (select && select->quick && select->quick->records > 0L)
276 {
277 records= min((ha_rows) (select->quick->records*2+EXTRA_RECORDS*2),
278 table->cursor->stats.records)+EXTRA_RECORDS;
279 selected_records_file=0;
280 }
281 else
282#endif
283 {
284 records= table->cursor->estimate_rows_upper_bound();
285 /*
286 If number of records is not known, use as much of sort buffer
287 as possible.
288 */
289 if (records == HA_POS_ERROR)
290 records--; // we use 'records+1' below.
291 selected_records_file= 0;
292 }
293
294 if (multi_byte_charset)
295 param.tmp_buffer= (char*) malloc(param.sort_length);
296
297 memavl= getSession().variables.sortbuff_size;
298 min_sort_memory= max((uint32_t)MIN_SORT_MEMORY, param.sort_length*MERGEBUFF2);
299 while (memavl >= min_sort_memory)
300 {
301 uint32_t old_memavl;
302 uint32_t keys= memavl/(param.rec_length+sizeof(char*));
303 param.keys= (uint32_t) min(records+1, (ha_rows)keys);
304
305 allocated_sort_memory= param.keys * param.rec_length;
306 if (not global_sort_buffer.add(allocated_sort_memory))
307 {
308 my_error(ER_OUT_OF_GLOBAL_SORTMEMORY, MYF(ME_ERROR+ME_WAITTANG));
309 goto err;
310 }
311
312 if ((table_sort.sort_keys=
313 (unsigned char **) make_char_array((char **) table_sort.sort_keys,
314 param.keys, param.rec_length)))
315 break;
316
317 global_sort_buffer.sub(allocated_sort_memory);
318 old_memavl= memavl;
319 if ((memavl= memavl/4*3) < min_sort_memory && old_memavl > min_sort_memory)
320 memavl= min_sort_memory;
321 }
322 sort_keys= table_sort.sort_keys;
323 if (memavl < min_sort_memory)
324 {
325 my_error(ER_OUT_OF_SORTMEMORY,MYF(ME_ERROR+ME_WAITTANG));
326 goto err;
327 }
328
329 if (buffpek_pointers.open_cached_file(drizzle_tmpdir.c_str(),TEMP_PREFIX, DISK_BUFFER_SIZE, MYF(MY_WME)))
330 {
331 goto err;
332 }
333
334 param.keys--; /* TODO: check why we do this */
335 param.sort_form= table;
336 param.end=(param.local_sortorder=sortorder)+s_length;
337 if ((records= find_all_keys(&param,select,sort_keys, &buffpek_pointers,
338 &tempfile, selected_records_file)) == HA_POS_ERROR)
339 {
340 goto err;
341 }
342 maxbuffer= (uint32_t)(buffpek_pointers.tell() / sizeof(*buffpek_inst));
343
344 if (maxbuffer == 0) // The whole set is in memory
345 {
346 param.save_index(sort_keys,(uint32_t) records, &table_sort);
347 }
348 else
349 {
350 if (table_sort.buffpek && table_sort.buffpek_len < maxbuffer)
351 {
352 free(table_sort.buffpek);
353 table_sort.buffpek = 0;
354 }
355 if (!(table_sort.buffpek=
356 (unsigned char *) read_buffpek_from_file(&buffpek_pointers, maxbuffer, table_sort.buffpek)))
357 {
358 goto err;
359 }
360 buffpek_inst= (buffpek *) table_sort.buffpek;
361 table_sort.buffpek_len= maxbuffer;
362 buffpek_pointers.close_cached_file();
363 /* Open cached file if it isn't open */
364 if (not outfile->inited() && outfile->open_cached_file(drizzle_tmpdir.c_str(),TEMP_PREFIX,READ_RECORD_BUFFER, MYF(MY_WME)))
365 {
366 goto err;
367 }
368
369 if (outfile->reinit_io_cache(internal::WRITE_CACHE,0L,0,0))
370 {
371 goto err;
372 }
373
374 /*
375 Use also the space previously used by string pointers in sort_buffer
376 for temporary key storage.
377 */
378 param.keys=((param.keys*(param.rec_length+sizeof(char*))) / param.rec_length-1);
379
380 maxbuffer--; // Offset from 0
381 if (merge_many_buff(&param,(unsigned char*) sort_keys,buffpek_inst,&maxbuffer, &tempfile))
382 {
383 goto err;
384 }
385
386 if (tempfile.flush() || tempfile.reinit_io_cache(internal::READ_CACHE,0L,0,0))
387 {
388 goto err;
389 }
390
391 if (merge_index(&param,(unsigned char*) sort_keys,buffpek_inst,maxbuffer,&tempfile, outfile))
392 {
393 goto err;
394 }
395 }
396
397 if (records > param.max_rows)
398 {
399 records= param.max_rows;
400 }
401 error =0;
402
403 err:
404 if (not subselect || not subselect->is_uncacheable())
405 {
406 free(sort_keys);
407 table_sort.sort_keys= 0;
408 free(buffpek_inst);
409 table_sort.buffpek= 0;
410 table_sort.buffpek_len= 0;
411 }
412
413 tempfile.close_cached_file();
414 buffpek_pointers.close_cached_file();
415
416 if (outfile->inited())
417 {
418 if (outfile->flush())
419 {
420 error= 1;
421 }
422 {
423 internal::my_off_t save_pos= outfile->pos_in_file;
424 /* For following reads */
425 if (outfile->reinit_io_cache(internal::READ_CACHE,0L,0,0))
426 {
427 error=1;
428 }
429 outfile->end_of_file=save_pos;
430 }
431 }
432
433 if (error)
434 {
435 my_message(ER_FILSORT_ABORT, ER(ER_FILSORT_ABORT),
436 MYF(ME_ERROR+ME_WAITTANG));
437 }
438 else
439 {
440 getSession().status_var.filesort_rows+= (uint32_t) records;
441 }
442 examined_rows= param.examined_rows;
443 global_sort_buffer.sub(allocated_sort_memory);
444 table->sort= table_sort;
445 DRIZZLE_FILESORT_DONE(error, records);
446 return (error ? HA_POS_ERROR : records);
447} /* filesort */
448
450
451static char **make_char_array(char **old_pos, uint32_t fields,
452 uint32_t length)
453{
454 if (not old_pos)
455 old_pos= (char**) malloc((uint32_t) fields * (length + sizeof(char*)));
456 char** pos= old_pos;
457 char* char_pos= ((char*) (pos+fields)) - length;
458 while (fields--)
459 *(pos++) = (char_pos+= length);
460
461 return old_pos;
462} /* make_char_array */
463
464
466
467static unsigned char *read_buffpek_from_file(internal::io_cache_st *buffpek_pointers, uint32_t count, unsigned char *buf)
468{
469 uint32_t length= sizeof(buffpek)*count;
470 unsigned char *tmp= buf;
471 if (count > UINT_MAX/sizeof(buffpek))
472 return 0; /* sizeof(buffpek)*count will overflow */
473 if (!tmp)
474 tmp= (unsigned char *)malloc(length);
475 {
476 if (buffpek_pointers->reinit_io_cache(internal::READ_CACHE,0L,0,0) ||
477 buffpek_pointers->read(tmp, length))
478 {
479 free(tmp);
480 tmp= 0;
481 }
482 }
483 return tmp;
484}
485
486
523
525 optimizer::SqlSelect *select,
526 unsigned char **sort_keys,
527 internal::io_cache_st *buffpek_pointers,
528 internal::io_cache_st *tempfile, internal::io_cache_st *indexfile)
529{
530 int error,flag,quick_select;
531 uint32_t idx,indexpos,ref_length;
532 unsigned char *ref_pos,*next_pos,ref_buff[MAX_REFLENGTH];
533 internal::my_off_t record;
534 Table *sort_form;
535 volatile Session::killed_state_t *killed= getSession().getKilledPtr();
536 Cursor *file;
537 boost::dynamic_bitset<> *save_read_set= NULL;
538 boost::dynamic_bitset<> *save_write_set= NULL;
539
540 idx=indexpos=0;
541 error=quick_select=0;
542 sort_form=param->sort_form;
543 file= sort_form->cursor;
544 ref_length=param->ref_length;
545 ref_pos= ref_buff;
546 quick_select=select && select->quick;
547 record=0;
548 flag= ((!indexfile && ! file->isOrdered())
549 || quick_select);
550 if (indexfile || flag)
551 ref_pos= &file->ref[0];
552 next_pos=ref_pos;
553 if (! indexfile && ! quick_select)
554 {
555 next_pos=(unsigned char*) 0; /* Find records in sequence */
556 if (file->startTableScan(1))
557 return(HA_POS_ERROR);
558 file->extra_opt(HA_EXTRA_CACHE, getSession().variables.read_buff_size);
559 }
560
561 ReadRecord read_record_info;
562 if (quick_select)
563 {
564 if (select->quick->reset())
565 return(HA_POS_ERROR);
566
567 if (read_record_info.init_read_record(&getSession(), select->quick->head, select, 1, 1))
568 return(HA_POS_ERROR);
569 }
570
571 /* Remember original bitmaps */
572 save_read_set= sort_form->read_set;
573 save_write_set= sort_form->write_set;
574 /* Set up temporary column read map for columns used by sort */
575 sort_form->tmp_set.reset();
576 /* Temporary set for register_used_fields and register_field_in_read_map */
577 sort_form->read_set= &sort_form->tmp_set;
578 param->register_used_fields();
579 if (select && select->cond)
580 select->cond->walk(&Item::register_field_in_read_map, 1,
581 (unsigned char*) sort_form);
582 sort_form->column_bitmaps_set(sort_form->tmp_set, sort_form->tmp_set);
583
584 for (;;)
585 {
586 if (quick_select)
587 {
588 if ((error= read_record_info.read_record(&read_record_info)))
589 {
590 error= HA_ERR_END_OF_FILE;
591 break;
592 }
593 file->position(sort_form->getInsertRecord());
594 }
595 else /* Not quick-select */
596 {
597 if (indexfile)
598 {
599 if (indexfile->read(ref_pos, ref_length))
600 {
601 error= errno ? errno : -1; /* Abort */
602 break;
603 }
604 error=file->rnd_pos(sort_form->getInsertRecord(),next_pos);
605 }
606 else
607 {
608 error=file->rnd_next(sort_form->getInsertRecord());
609
610 if (!flag)
611 {
612 internal::my_store_ptr(ref_pos,ref_length,record); // Position to row
613 record+= sort_form->getShare()->db_record_offset;
614 }
615 else if (!error)
616 file->position(sort_form->getInsertRecord());
617 }
618 if (error && error != HA_ERR_RECORD_DELETED)
619 break;
620 }
621
622 if (*killed)
623 {
624 if (!indexfile && !quick_select)
625 {
626 (void) file->extra(HA_EXTRA_NO_CACHE);
627 file->endTableScan();
628 }
629 return(HA_POS_ERROR);
630 }
631 if (error == 0)
632 param->examined_rows++;
633 if (error == 0 && (!select || select->skip_record() == 0))
634 {
635 if (idx == param->keys)
636 {
637 if (param->write_keys(sort_keys, idx, buffpek_pointers, tempfile))
638 return(HA_POS_ERROR);
639 idx=0;
640 indexpos++;
641 }
642 param->make_sortkey(sort_keys[idx++], ref_pos);
643 }
644 else
645 {
646 file->unlock_row();
647 }
648
649 /* It does not make sense to read more keys in case of a fatal error */
650 if (getSession().is_error())
651 break;
652 }
653 if (quick_select)
654 {
655 /*
656 index_merge quick select uses table->sort when retrieving rows, so free
657 resoures it has allocated.
658 */
659 read_record_info.end_read_record();
660 }
661 else
662 {
663 (void) file->extra(HA_EXTRA_NO_CACHE); /* End cacheing of records */
664 if (!next_pos)
665 file->endTableScan();
666 }
667
668 if (getSession().is_error())
669 return(HA_POS_ERROR);
670
671 /* Signal we should use orignal column read and write maps */
672 sort_form->column_bitmaps_set(*save_read_set, *save_write_set);
673
674 if (error != HA_ERR_END_OF_FILE)
675 {
676 sort_form->print_error(error,MYF(ME_ERROR | ME_WAITTANG));
677 return(HA_POS_ERROR);
678 }
679
680 if (indexpos && idx && param->write_keys(sort_keys,idx,buffpek_pointers,tempfile))
681 {
682 return(HA_POS_ERROR);
683 }
684
685 return tempfile->inited() ? (ha_rows) (tempfile->tell() / param->rec_length) : idx;
686} /* find_all_keys */
687
688
710
711int SortParam::write_keys(unsigned char **sort_keys, uint32_t count,
712 internal::io_cache_st *buffpek_pointers, internal::io_cache_st *tempfile)
713{
715
716 internal::my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, sort_length);
717 if (not tempfile->inited() &&
718 tempfile->open_cached_file(drizzle_tmpdir.c_str(), TEMP_PREFIX, DISK_BUFFER_SIZE, MYF(MY_WME)))
719 {
720 return 1;
721 }
722 /* check we won't have more buffpeks than we can possibly keep in memory */
723 if (buffpek_pointers->tell() + sizeof(buffpek) > UINT_MAX)
724 {
725 return 1;
726 }
727
728 buffpek.file_pos= tempfile->tell();
729 if ((ha_rows) count > max_rows)
730 count=(uint32_t) max_rows;
731
732 buffpek.count=(ha_rows) count;
733
734 for (unsigned char **ptr= sort_keys + count ; sort_keys != ptr ; sort_keys++)
735 {
736 if (tempfile->write(*sort_keys, rec_length))
737 {
738 return 1;
739 }
740 }
741
742 if (buffpek_pointers->write(&buffpek, sizeof(buffpek)))
743 {
744 return 1;
745 }
746
747 return 0;
748} /* write_keys */
749
750
754
755static inline void store_length(unsigned char *to, uint32_t length, uint32_t pack_length)
756{
757 switch (pack_length) {
758 case 1:
759 *to= (unsigned char) length;
760 break;
761 case 2:
762 mi_int2store(to, length);
763 break;
764 case 3:
765 mi_int3store(to, length);
766 break;
767 default:
768 mi_int4store(to, length);
769 break;
770 }
771}
772
773
775
776void SortParam::make_sortkey(unsigned char *to, unsigned char *ref_pos)
777{
778 Field *field;
779 SortField *sort_field;
780 size_t length;
781
782 for (sort_field= local_sortorder ;
783 sort_field != end ;
784 sort_field++)
785 {
786 bool maybe_null=0;
787 if ((field=sort_field->field))
788 { // Field
789 if (field->maybe_null())
790 {
791 if (field->is_null())
792 {
793 if (sort_field->reverse)
794 memset(to, 255, sort_field->length+1);
795 else
796 memset(to, 0, sort_field->length+1);
797 to+= sort_field->length+1;
798 continue;
799 }
800 else
801 *to++=1;
802 }
803 field->sort_string(to, sort_field->length);
804 }
805 else
806 { // Item
807 Item *item=sort_field->item;
808 maybe_null= item->maybe_null;
809
810 switch (sort_field->result_type) {
811 case STRING_RESULT:
812 {
813 const charset_info_st * const cs=item->collation.collation;
814 char fill_char= ((cs->state & MY_CS_BINSORT) ? (char) 0 : ' ');
815 int diff;
816 uint32_t sort_field_length;
817
818 if (maybe_null)
819 *to++=1;
820 /* All item->str() to use some extra byte for end null.. */
821 String tmp((char*) to,sort_field->length+4,cs);
822 String *res= item->str_result(&tmp);
823 if (!res)
824 {
825 if (maybe_null)
826 memset(to-1, 0, sort_field->length+1);
827 else
828 {
829 /*
830 This should only happen during extreme conditions if we run out
831 of memory or have an item marked not null when it can be null.
832 This code is here mainly to avoid a hard crash in this case.
833 */
834 assert(0);
835 memset(to, 0, sort_field->length); // Avoid crash
836 }
837 break;
838 }
839 length= res->length();
840 sort_field_length= sort_field->length - sort_field->suffix_length;
841 diff=(int) (sort_field_length - length);
842 if (diff < 0)
843 {
844 diff=0;
845 length= sort_field_length;
846 }
847 if (sort_field->suffix_length)
848 {
849 /* Store length last in result_string */
850 store_length(to + sort_field_length, length,
851 sort_field->suffix_length);
852 }
853 if (sort_field->need_strxnfrm)
854 {
855 char *from=(char*) res->ptr();
856 uint32_t tmp_length;
857 if ((unsigned char*) from == to)
858 {
859 set_if_smaller(length,sort_field->length);
860 memcpy(tmp_buffer,from,length);
861 from= tmp_buffer;
862 }
863 tmp_length= cs->strnxfrm(to,sort_field->length, (unsigned char*) from, length);
864 (void)(tmp_length);
865 assert(tmp_length == sort_field->length);
866 }
867 else
868 {
869 cs->strnxfrm((unsigned char*)to,length,(const unsigned char*)res->ptr(),length);
870 cs->cset->fill(cs, (char *)to+length,diff,fill_char);
871 }
872 break;
873 }
874 case INT_RESULT:
875 {
876 int64_t value= item->val_int_result();
877 if (maybe_null)
878 {
879 *to++=1;
880 if (item->null_value)
881 {
882 if (maybe_null)
883 memset(to-1, 0, sort_field->length+1);
884 else
885 {
886 memset(to, 0, sort_field->length);
887 }
888 break;
889 }
890 }
891 to[7]= (unsigned char) value;
892 to[6]= (unsigned char) (value >> 8);
893 to[5]= (unsigned char) (value >> 16);
894 to[4]= (unsigned char) (value >> 24);
895 to[3]= (unsigned char) (value >> 32);
896 to[2]= (unsigned char) (value >> 40);
897 to[1]= (unsigned char) (value >> 48);
898 if (item->unsigned_flag) /* Fix sign */
899 to[0]= (unsigned char) (value >> 56);
900 else
901 to[0]= (unsigned char) (value >> 56) ^ 128; /* Reverse signbit */
902 break;
903 }
904 case DECIMAL_RESULT:
905 {
906 type::Decimal dec_buf, *dec_val= item->val_decimal_result(&dec_buf);
907 if (maybe_null)
908 {
909 if (item->null_value)
910 {
911 memset(to, 0, sort_field->length+1);
912 to++;
913 break;
914 }
915 *to++=1;
916 }
917 dec_val->val_binary(E_DEC_FATAL_ERROR, to,
918 item->max_length - (item->decimals ? 1:0),
919 item->decimals);
920 break;
921 }
922 case REAL_RESULT:
923 {
924 double value= item->val_result();
925 if (maybe_null)
926 {
927 if (item->null_value)
928 {
929 memset(to, 0, sort_field->length+1);
930 to++;
931 break;
932 }
933 *to++=1;
934 }
935 change_double_for_sort(value,(unsigned char*) to);
936 break;
937 }
938 case ROW_RESULT:
939 default:
940 // This case should never be choosen
941 assert(0);
942 break;
943 }
944 }
945
946 if (sort_field->reverse)
947 { /* Revers key */
948 if (maybe_null)
949 to[-1]= ~to[-1];
950 length=sort_field->length;
951 while (length--)
952 {
953 *to = (unsigned char) (~ *to);
954 to++;
955 }
956 }
957 else
958 {
959 to+= sort_field->length;
960 }
961 }
962
963 if (addon_field)
964 {
965 /*
966 Save field values appended to sorted fields.
967 First null bit indicators are appended then field values follow.
968 In this implementation we use fixed layout for field values -
969 the same for all records.
970 */
971 sort_addon_field *addonf= addon_field;
972 unsigned char *nulls= to;
973 assert(addonf != 0);
974 memset(nulls, 0, addonf->offset);
975 to+= addonf->offset;
976 for ( ; (field= addonf->field) ; addonf++)
977 {
978 if (addonf->null_bit && field->is_null())
979 {
980 nulls[addonf->null_offset]|= addonf->null_bit;
981#ifdef HAVE_VALGRIND
982 memset(to, 0, addonf->length);
983#endif
984 }
985 else
986 {
987#ifdef HAVE_VALGRIND
988 unsigned char *end= field->pack(to, field->ptr);
989 uint32_t local_length= (uint32_t) ((to + addonf->length) - end);
990 assert((int) local_length >= 0);
991 if (local_length)
992 memset(end, 0, local_length);
993#else
994 (void) field->pack(to, field->ptr);
995#endif
996 }
997 to+= addonf->length;
998 }
999 }
1000 else
1001 {
1002 /* Save filepos last */
1003 memcpy(to, ref_pos, (size_t) ref_length);
1004 }
1005}
1006
1007
1008/*
1009 fields used by sorting in the sorted table's read set
1010*/
1011
1012void SortParam::register_used_fields()
1013{
1014 SortField *sort_field;
1015 Table *table= sort_form;
1016
1017 for (sort_field= local_sortorder ;
1018 sort_field != end ;
1019 sort_field++)
1020 {
1021 Field *field;
1022 if ((field= sort_field->field))
1023 {
1024 if (field->getTable() == table)
1025 table->setReadSet(field->position());
1026 }
1027 else
1028 { // Item
1029 sort_field->item->walk(&Item::register_field_in_read_map, 1,
1030 (unsigned char *) table);
1031 }
1032 }
1033
1034 if (addon_field)
1035 {
1036 sort_addon_field *addonf= addon_field;
1037 Field *field;
1038 for ( ; (field= addonf->field) ; addonf++)
1039 table->setReadSet(field->position());
1040 }
1041 else
1042 {
1043 /* Save filepos last */
1044 table->prepare_for_position();
1045 }
1046}
1047
1048
1049void SortParam::save_index(unsigned char **sort_keys, uint32_t count, filesort_info *table_sort)
1050{
1051 internal::my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, sort_length);
1052 uint32_t offset= rec_length - res_length;
1053
1054 if ((ha_rows) count > max_rows)
1055 count=(uint32_t) max_rows;
1056
1057 unsigned char* to= table_sort->record_pointers= (unsigned char*) malloc(res_length*count);
1058
1059 for (unsigned char **end_ptr= sort_keys+count ; sort_keys != end_ptr ; sort_keys++)
1060 {
1061 memcpy(to, *sort_keys+offset, res_length);
1062 to+= res_length;
1063 }
1064}
1065
1066
1068
1069int FileSort::merge_many_buff(SortParam *param, unsigned char *sort_buffer,
1070 buffpek *buffpek_inst, uint32_t *maxbuffer, internal::io_cache_st *t_file)
1071{
1072 internal::io_cache_st t_file2,*from_file,*to_file,*temp;
1073 buffpek *lastbuff;
1074
1075 if (*maxbuffer < MERGEBUFF2)
1076 return 0;
1077 if (t_file->flush() ||
1078 t_file2.open_cached_file(drizzle_tmpdir.c_str(),TEMP_PREFIX,DISK_BUFFER_SIZE, MYF(MY_WME)))
1079 {
1080 return 1;
1081 }
1082
1083 from_file= t_file ; to_file= &t_file2;
1084 while (*maxbuffer >= MERGEBUFF2)
1085 {
1086 if (from_file->reinit_io_cache(internal::READ_CACHE, 0, 0, 0)
1087 || to_file->reinit_io_cache(internal::WRITE_CACHE, 0, 0, 0))
1088 break;
1089
1090 uint32_t i= 0;
1091 lastbuff= buffpek_inst;
1092 for (; i <= *maxbuffer - MERGEBUFF * 3 / 2; i += MERGEBUFF)
1093 {
1094 if (merge_buffers(param, from_file, to_file, sort_buffer, lastbuff++, buffpek_inst + i, buffpek_inst + i + MERGEBUFF - 1, 0))
1095 {
1096 goto cleanup;
1097 }
1098 }
1099
1100 if (merge_buffers(param, from_file, to_file, sort_buffer, lastbuff++, buffpek_inst + i, buffpek_inst + *maxbuffer, 0)
1101 || to_file->flush())
1102 break;
1103
1104 temp=from_file; from_file=to_file; to_file=temp;
1105 from_file->setup_io_cache();
1106 to_file->setup_io_cache();
1107 *maxbuffer= (uint32_t) (lastbuff-buffpek_inst)-1;
1108 }
1109
1110cleanup:
1111 to_file->close_cached_file(); // This holds old result
1112 if (to_file == t_file)
1113 {
1114 *t_file=t_file2; // Copy result file
1115 t_file->setup_io_cache();
1116 }
1117
1118 return(*maxbuffer >= MERGEBUFF2); /* Return 1 if interrupted */
1119} /* merge_many_buff */
1120
1121
1128
1129uint32_t FileSort::read_to_buffer(internal::io_cache_st *fromfile, buffpek *buffpek_inst, uint32_t rec_length)
1130{
1131 uint32_t count;
1132 uint32_t length;
1133
1134 if ((count= (uint32_t) min((ha_rows) buffpek_inst->max_keys,buffpek_inst->count)))
1135 {
1136 if (pread(fromfile->file,(unsigned char*) buffpek_inst->base, (length= rec_length*count),buffpek_inst->file_pos) == 0)
1137 return((uint32_t) -1);
1138
1139 buffpek_inst->key= buffpek_inst->base;
1140 buffpek_inst->file_pos+= length; /* New filepos */
1141 buffpek_inst->count-= count;
1142 buffpek_inst->mem_count= count;
1143 }
1144 return (count*rec_length);
1145} /* read_to_buffer */
1146
1147
1148class compare_functor
1149{
1150 qsort2_cmp key_compare;
1151 void *key_compare_arg;
1152
1153 public:
1154 compare_functor(qsort2_cmp in_key_compare, void *in_compare_arg) :
1155 key_compare(in_key_compare),
1156 key_compare_arg(in_compare_arg)
1157 { }
1158
1159 inline bool operator()(const buffpek *i, const buffpek *j) const
1160 {
1161 int val= key_compare(key_compare_arg, &i->key, &j->key);
1162
1163 return (val >= 0);
1164 }
1165};
1166
1167
1185
1187 internal::io_cache_st *to_file, unsigned char *sort_buffer,
1188 buffpek *lastbuff, buffpek *Fb, buffpek *Tb,
1189 int flag)
1190{
1191 int error;
1192 uint32_t rec_length,res_length,offset;
1193 size_t sort_length;
1194 uint32_t maxcount;
1195 ha_rows max_rows,org_max_rows;
1196 internal::my_off_t to_start_filepos;
1197 unsigned char *strpos;
1198 buffpek *buffpek_inst;
1199 qsort2_cmp cmp;
1200 void *first_cmp_arg;
1201 volatile Session::killed_state_t *killed= getSession().getKilledPtr();
1202 Session::killed_state_t not_killable;
1203
1204 getSession().status_var.filesort_merge_passes++;
1205 if (param->not_killable)
1206 {
1207 killed= &not_killable;
1208 not_killable= Session::NOT_KILLED;
1209 }
1210
1211 error=0;
1212 rec_length= param->rec_length;
1213 res_length= param->res_length;
1214 sort_length= param->sort_length;
1215 offset= rec_length-res_length;
1216 maxcount= (uint32_t) (param->keys/((uint32_t) (Tb-Fb) +1));
1217 to_start_filepos= to_file->tell();
1218 strpos= (unsigned char*) sort_buffer;
1219 org_max_rows=max_rows= param->max_rows;
1220
1221 /* The following will fire if there is not enough space in sort_buffer */
1222 assert(maxcount!=0);
1223
1224 if (param->unique_buff)
1225 {
1226 cmp= param->compare;
1227 first_cmp_arg= (void *) &param->cmp_context;
1228 }
1229 else
1230 {
1231 cmp= internal::get_ptr_compare(sort_length);
1232 first_cmp_arg= (void*) &sort_length;
1233 }
1234 priority_queue<buffpek *, vector<buffpek *>, compare_functor >
1235 queue(compare_functor(cmp, first_cmp_arg));
1236 for (buffpek_inst= Fb ; buffpek_inst <= Tb ; buffpek_inst++)
1237 {
1238 buffpek_inst->base= strpos;
1239 buffpek_inst->max_keys= maxcount;
1240 strpos+= (uint32_t) (error= (int) read_to_buffer(from_file, buffpek_inst,
1241 rec_length));
1242 if (error == -1)
1243 return -1;
1244
1245 buffpek_inst->max_keys= buffpek_inst->mem_count; // If less data in buffers than expected
1246 queue.push(buffpek_inst);
1247 }
1248
1249 if (param->unique_buff)
1250 {
1251 /*
1252 Called by Unique::get()
1253 Copy the first argument to param->unique_buff for unique removal.
1254 Store it also in 'to_file'.
1255
1256 This is safe as we know that there is always more than one element
1257 in each block to merge (This is guaranteed by the Unique:: algorithm
1258 */
1259 buffpek_inst= queue.top();
1260 memcpy(param->unique_buff, buffpek_inst->key, rec_length);
1261 if (to_file->write(buffpek_inst->key, rec_length))
1262 {
1263 return 1;
1264 }
1265 buffpek_inst->key+= rec_length;
1266 buffpek_inst->mem_count--;
1267 if (!--max_rows)
1268 {
1269 error= 0;
1270 goto end;
1271 }
1272 /* Top element has been used */
1273 queue.pop();
1274 queue.push(buffpek_inst);
1275 }
1276 else
1277 {
1278 cmp= 0; // Not unique
1279 }
1280
1281 while (queue.size() > 1)
1282 {
1283 if (*killed)
1284 {
1285 return 1;
1286 }
1287 for (;;)
1288 {
1289 buffpek_inst= queue.top();
1290 if (cmp) // Remove duplicates
1291 {
1292 if (!(*cmp)(first_cmp_arg, &(param->unique_buff),
1293 (unsigned char**) &buffpek_inst->key))
1294 goto skip_duplicate;
1295 memcpy(param->unique_buff, buffpek_inst->key, rec_length);
1296 }
1297 if (flag == 0)
1298 {
1299 if (to_file->write(buffpek_inst->key, rec_length))
1300 {
1301 return 1;
1302 }
1303 }
1304 else
1305 {
1306 if (to_file->write(buffpek_inst->key+offset, res_length))
1307 {
1308 return 1;
1309 }
1310 }
1311 if (!--max_rows)
1312 {
1313 error= 0;
1314 goto end;
1315 }
1316
1317 skip_duplicate:
1318 buffpek_inst->key+= rec_length;
1319 if (! --buffpek_inst->mem_count)
1320 {
1321 if (!(error= (int) read_to_buffer(from_file,buffpek_inst,
1322 rec_length)))
1323 {
1324 queue.pop();
1325 break; /* One buffer have been removed */
1326 }
1327 else if (error == -1)
1328 {
1329 return -1;
1330 }
1331 }
1332 /* Top element has been replaced */
1333 queue.pop();
1334 queue.push(buffpek_inst);
1335 }
1336 }
1337 buffpek_inst= queue.top();
1338 buffpek_inst->base= sort_buffer;
1339 buffpek_inst->max_keys= param->keys;
1340
1341 /*
1342 As we know all entries in the buffer are unique, we only have to
1343 check if the first one is the same as the last one we wrote
1344 */
1345 if (cmp)
1346 {
1347 if (!(*cmp)(first_cmp_arg, &(param->unique_buff), (unsigned char**) &buffpek_inst->key))
1348 {
1349 buffpek_inst->key+= rec_length; // Remove duplicate
1350 --buffpek_inst->mem_count;
1351 }
1352 }
1353
1354 do
1355 {
1356 if ((ha_rows) buffpek_inst->mem_count > max_rows)
1357 { /* Don't write too many records */
1358 buffpek_inst->mem_count= (uint32_t) max_rows;
1359 buffpek_inst->count= 0; /* Don't read more */
1360 }
1361 max_rows-= buffpek_inst->mem_count;
1362 if (flag == 0)
1363 {
1364 if (to_file->write(buffpek_inst->key, (rec_length*buffpek_inst->mem_count)))
1365 {
1366 return 1;
1367 }
1368 }
1369 else
1370 {
1371 unsigned char *end;
1372 strpos= buffpek_inst->key+offset;
1373 for (end= strpos+buffpek_inst->mem_count*rec_length ;
1374 strpos != end ;
1375 strpos+= rec_length)
1376 {
1377 if (to_file->write(strpos, res_length))
1378 {
1379 return 1;
1380 }
1381 }
1382 }
1383 }
1384
1385 while ((error=(int) read_to_buffer(from_file,buffpek_inst, rec_length))
1386 != -1 && error != 0);
1387
1388end:
1389 lastbuff->count= min(org_max_rows-max_rows, param->max_rows);
1390 lastbuff->file_pos= to_start_filepos;
1391
1392 return error;
1393} /* merge_buffers */
1394
1395
1396 /* Do a merge to output-file (save only positions) */
1397
1398int FileSort::merge_index(SortParam *param, unsigned char *sort_buffer,
1399 buffpek *buffpek_inst, uint32_t maxbuffer,
1400 internal::io_cache_st *tempfile, internal::io_cache_st *outfile)
1401{
1402 if (merge_buffers(param,tempfile,outfile,sort_buffer,buffpek_inst,buffpek_inst,
1403 buffpek_inst+maxbuffer,1))
1404 return 1;
1405
1406 return 0;
1407} /* merge_index */
1408
1409
1410static uint32_t suffix_length(uint32_t string_length)
1411{
1412 if (string_length < 256)
1413 return 1;
1414 if (string_length < 256L*256L)
1415 return 2;
1416 if (string_length < 256L*256L*256L)
1417 return 3;
1418 return 4; // Can't sort longer than 4G
1419}
1420
1421
1422
1439
1440uint32_t FileSort::sortlength(SortField *sortorder, uint32_t s_length, bool *multi_byte_charset)
1441{
1442 uint32_t length;
1443 const charset_info_st *cs;
1444 *multi_byte_charset= 0;
1445
1446 length=0;
1447 for (; s_length-- ; sortorder++)
1448 {
1449 sortorder->need_strxnfrm= 0;
1450 sortorder->suffix_length= 0;
1451 if (sortorder->field)
1452 {
1453 cs= sortorder->field->sort_charset();
1454 sortorder->length= sortorder->field->sort_length();
1455 cs= sortorder->field->sort_charset();
1456 if (cs->use_strnxfrm())
1457 {
1458 sortorder->need_strxnfrm= 1;
1459 *multi_byte_charset= 1;
1460 sortorder->length= cs->coll->strnxfrmlen(cs, sortorder->length);
1461 }
1462 if (sortorder->field->maybe_null())
1463 length++; // Place for NULL marker
1464 }
1465 else
1466 {
1467 sortorder->result_type= sortorder->item->result_type();
1468 if (sortorder->item->result_as_int64_t())
1469 sortorder->result_type= INT_RESULT;
1470
1471 switch (sortorder->result_type) {
1472 case STRING_RESULT:
1473 sortorder->length=sortorder->item->max_length;
1474 set_if_smaller(sortorder->length, getSession().variables.max_sort_length);
1475 cs= sortorder->item->collation.collation;
1476 if (cs->use_strnxfrm())
1477 {
1478 sortorder->length= cs->coll->strnxfrmlen(cs, sortorder->length);
1479 sortorder->need_strxnfrm= 1;
1480 *multi_byte_charset= 1;
1481 }
1482 else if (cs == &my_charset_bin)
1483 {
1484 /* Store length last to be able to sort blob/varbinary */
1485 sortorder->suffix_length= suffix_length(sortorder->length);
1486 sortorder->length+= sortorder->suffix_length;
1487 }
1488 break;
1489 case INT_RESULT:
1490 sortorder->length=8; // Size of intern int64_t
1491 break;
1492 case DECIMAL_RESULT:
1493 sortorder->length=
1494 class_decimal_get_binary_size(sortorder->item->max_length -
1495 (sortorder->item->decimals ? 1 : 0),
1496 sortorder->item->decimals);
1497 break;
1498 case REAL_RESULT:
1499 sortorder->length=sizeof(double);
1500 break;
1501 case ROW_RESULT:
1502 // This case should never be choosen
1503 assert(0);
1504 break;
1505 }
1506 if (sortorder->item->maybe_null)
1507 length++; // Place for NULL marker
1508 }
1509 set_if_smaller(sortorder->length, (size_t)getSession().variables.max_sort_length);
1510 length+=sortorder->length;
1511 }
1512 sortorder->field= (Field*) 0; // end marker
1513 return length;
1514}
1515
1516
1542
1543sort_addon_field *FileSort::get_addon_fields(Field **ptabfield, uint32_t sortlength_arg, uint32_t *plength)
1544{
1545 Field **pfield;
1546 Field *field;
1547 sort_addon_field *addonf;
1548 uint32_t length= 0;
1549 uint32_t fields= 0;
1550 uint32_t null_fields= 0;
1551
1552 /*
1553 If there is a reference to a field in the query add it
1554 to the the set of appended fields.
1555 Note for future refinement:
1556 This this a too strong condition.
1557 Actually we need only the fields referred in the
1558 result set. And for some of them it makes sense to use
1559 the values directly from sorted fields.
1560 */
1561 *plength= 0;
1562
1563 for (pfield= ptabfield; (field= *pfield) ; pfield++)
1564 {
1565 if (!(field->isReadSet()))
1566 continue;
1567 if (field->flags & BLOB_FLAG)
1568 return 0;
1569 length+= field->max_packed_col_length(field->pack_length());
1570 if (field->maybe_null())
1571 null_fields++;
1572 fields++;
1573 }
1574 if (!fields)
1575 return 0;
1576 length+= (null_fields+7)/8;
1577
1578 if (length+sortlength_arg > getSession().variables.max_length_for_sort_data)
1579 return 0;
1580 addonf= (sort_addon_field *) malloc(sizeof(sort_addon_field) * (fields+1));
1581
1582 *plength= length;
1583 length= (null_fields+7)/8;
1584 null_fields= 0;
1585 for (pfield= ptabfield; (field= *pfield) ; pfield++)
1586 {
1587 if (!(field->isReadSet()))
1588 continue;
1589 addonf->field= field;
1590 addonf->offset= length;
1591 if (field->maybe_null())
1592 {
1593 addonf->null_offset= null_fields/8;
1594 addonf->null_bit= 1<<(null_fields & 7);
1595 null_fields++;
1596 }
1597 else
1598 {
1599 addonf->null_offset= 0;
1600 addonf->null_bit= 0;
1601 }
1602 addonf->length= field->max_packed_col_length(field->pack_length());
1603 length+= addonf->length;
1604 addonf++;
1605 }
1606 addonf->field= 0; // Put end marker
1607
1608 return (addonf-fields);
1609}
1610
1611
1626
1627static void
1628unpack_addon_fields(sort_addon_field *addon_field, unsigned char *buff)
1629{
1630 Field *field;
1631 sort_addon_field *addonf= addon_field;
1632
1633 for ( ; (field= addonf->field) ; addonf++)
1634 {
1635 if (addonf->null_bit && (addonf->null_bit & buff[addonf->null_offset]))
1636 {
1637 field->set_null();
1638 continue;
1639 }
1640 field->set_notnull();
1641 field->unpack(field->ptr, buff + addonf->offset);
1642 }
1643}
1644
1645/*
1646** functions to change a double or float to a sortable string
1647** The following should work for IEEE
1648*/
1649
1650#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
1651
1652void change_double_for_sort(double nr,unsigned char *to)
1653{
1654 unsigned char *tmp=(unsigned char*) to;
1655 if (compare_double(nr, 0.0))
1656 { /* Change to zero string */
1657 tmp[0]=(unsigned char) 128;
1658 memset(tmp+1, 0, sizeof(nr)-1);
1659 }
1660 else
1661 {
1662#ifdef WORDS_BIGENDIAN
1663 memcpy(tmp,&nr,sizeof(nr));
1664#else
1665 {
1666 unsigned char *ptr= (unsigned char*) &nr;
1667#if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
1668 tmp[0]= ptr[3]; tmp[1]=ptr[2]; tmp[2]= ptr[1]; tmp[3]=ptr[0];
1669 tmp[4]= ptr[7]; tmp[5]=ptr[6]; tmp[6]= ptr[5]; tmp[7]=ptr[4];
1670#else
1671 tmp[0]= ptr[7]; tmp[1]=ptr[6]; tmp[2]= ptr[5]; tmp[3]=ptr[4];
1672 tmp[4]= ptr[3]; tmp[5]=ptr[2]; tmp[6]= ptr[1]; tmp[7]=ptr[0];
1673#endif
1674 }
1675#endif
1676 if (tmp[0] & 128) /* Negative */
1677 { /* make complement */
1678 uint32_t i;
1679 for (i=0 ; i < sizeof(nr); i++)
1680 tmp[i]=tmp[i] ^ (unsigned char) 255;
1681 }
1682 else
1683 { /* Set high and move exponent one up */
1684 uint16_t exp_part=(((uint16_t) tmp[0] << 8) | (uint16_t) tmp[1] |
1685 (uint16_t) 32768);
1686 exp_part+= (uint16_t) 1 << (16-1-DBL_EXP_DIG);
1687 tmp[0]= (unsigned char) (exp_part >> 8);
1688 tmp[1]= (unsigned char) exp_part;
1689 }
1690 }
1691}
1692
1693} /* namespace drizzled */
sort_addon_field * get_addon_fields(Field **ptabfield, uint32_t sortlength, uint32_t *plength)
Definition filesort.cc:1543
uint32_t sortlength(SortField *sortorder, uint32_t s_length, bool *multi_byte_charset)
Definition filesort.cc:1440
uint32_t read_to_buffer(internal::io_cache_st *fromfile, buffpek *buffpek, uint32_t sort_length)
Definition filesort.cc:1129
ha_rows find_all_keys(SortParam *param, optimizer::SqlSelect *select, unsigned char **sort_keys, internal::io_cache_st *buffpek_pointers, internal::io_cache_st *tempfile, internal::io_cache_st *indexfile)
Definition filesort.cc:524
int merge_buffers(SortParam *param, internal::io_cache_st *from_file, internal::io_cache_st *to_file, unsigned char *sort_buffer, buffpek *lastbuff, buffpek *Fb, buffpek *Tb, int flag)
Definition filesort.cc:1186
ha_rows run(Table *table, SortField *sortorder, uint32_t s_length, optimizer::SqlSelect *select, ha_rows max_rows, bool sort_positions, ha_rows &examined_rows)
Definition filesort.cc:189
int merge_many_buff(SortParam *param, unsigned char *sort_buffer, buffpek *buffpek, uint32_t *maxbuffer, internal::io_cache_st *t_file)
Definition filesort.cc:1069
bool maybe_null
Definition item.h:123
virtual bool result_as_int64_t()
Definition item.cc:683
uint32_t suffix_length
Definition sort_field.h:36
Item_result result_type
Definition sort_field.h:37
void make_sortkey(unsigned char *to, unsigned char *ref_pos)
Definition filesort.cc:776
int write_keys(unsigned char **sort_keys, uint32_t count, internal::io_cache_st *buffer_file, internal::io_cache_st *tempfile)
Definition filesort.cc:711
Item_subselect * containing_subselect()
bool reinit_io_cache(cache_type type_arg, my_off_t seek_offset, bool use_async_io, bool clear_cache)
Reset the cache.
void setup_io_cache()
Setup internal pointers inside io_cache_st.
QuickSelectInterface * quick
Definition range.h:277
TODO: Rename this file - func.h is stupid.
static void unpack_addon_fields(sort_addon_field *addon_field, unsigned char *buff)
Definition filesort.cc:1628
static void store_length(unsigned char *to, uint32_t length, uint32_t pack_length)
Definition filesort.cc:755
static char ** make_char_array(char **old_pos, uint32_t fields, uint32_t length)
Definition filesort.cc:451
static unsigned char * read_buffpek_from_file(internal::io_cache_st *buffer_file, uint32_t count, unsigned char *buf)
Definition filesort.cc:467
STL namespace.