My Project
Defines | Functions | Variables
filesort.cc File Reference

Sorts a database. More...

#include "sql_priv.h"
#include "filesort.h"
#include "unireg.h"
#include <m_ctype.h>
#include "sql_sort.h"
#include "probes_mysql.h"
#include "opt_range.h"
#include "bounded_queue.h"
#include "filesort_utils.h"
#include "sql_select.h"
#include "debug_sync.h"
#include "opt_trace.h"
#include "sql_optimizer.h"
#include "sql_base.h"
#include <algorithm>
#include <utility>

Defines

#define DBL_EXP_DIG   (sizeof(double)*8-DBL_MANT_DIG)

Functions

ha_rows filesort (THD *thd, TABLE *table, Filesort *filesort, bool sort_positions, ha_rows *examined_rows, ha_rows *found_rows)
void filesort_free_buffers (TABLE *table, bool full)
void copy_native_longlong (uchar *to, int to_length, longlong val, bool is_unsigned)
void make_sortkey (Sort_param *param, uchar *to, uchar *ref_pos)
 Declared here so we can unit test it.
int merge_many_buff (Sort_param *param, uchar *sort_buffer, BUFFPEK *buffpek, uint *maxbuffer, IO_CACHE *t_file)
uint read_to_buffer (IO_CACHE *fromfile, BUFFPEK *buffpek, uint rec_length)
void reuse_freed_buff (QUEUE *queue, BUFFPEK *reuse, uint key_length)
int merge_buffers (Sort_param *param, IO_CACHE *from_file, IO_CACHE *to_file, uchar *sort_buffer, BUFFPEK *lastbuff, BUFFPEK *Fb, BUFFPEK *Tb, int flag)
uint sortlength (THD *thd, SORT_FIELD *sortorder, uint s_length, bool *multi_byte_charset)
 Declared here so we can unit test it.
void change_double_for_sort (double nr, uchar *to)

Variables

const bool Is_big_endian = false

Detailed Description

Sorts a database.


Function Documentation

ha_rows filesort ( THD *  thd,
TABLE table,
Filesort filesort,
bool  sort_positions,
ha_rows *  examined_rows,
ha_rows *  found_rows 
)

Sort a table. Creates a set of pointers that can be used to read the rows in sorted order. This should be done with the functions in records.cc.

Before calling filesort, one must have done table->file->info(HA_STATUS_VARIABLE)

The result set is stored in table->io_cache or table->record_pointers.

Parameters:
thdCurrent thread
tableTable to sort
filesortHow to sort the table
sort_positionsSet to TRUE if we want to force sorting by position (Needed by UPDATE/INSERT or ALTER TABLE or when rowids are required by executor)
[out]examined_rowsStore number of examined rows here This is the number of found rows before applying WHERE condition.
[out]found_rowsStore the number of found rows here. This is the number of found rows after applying WHERE condition.
Note:
If we sort by position (like if sort_positions is 1) filesort() will call table->prepare_for_position().
Return values:
HA_POS_ERRORError
\::Number of rows in the result, could be less than found_rows if LIMIT were provided.
void make_sortkey ( Sort_param param,
uchar *  to,
uchar *  ref_pos 
)

Declared here so we can unit test it.

Make a sort-key from record.

int merge_buffers ( Sort_param param,
IO_CACHE *  from_file,
IO_CACHE *  to_file,
uchar *  sort_buffer,
BUFFPEK lastbuff,
BUFFPEK Fb,
BUFFPEK Tb,
int  flag 
)

Merge buffers to one buffer.

Parameters:
paramSort parameter
from_fileFile with source data (BUFFPEKs point to this file)
to_fileFile to write the sorted result data.
sort_bufferBuffer for data to store up to MERGEBUFF2 sort keys.
lastbuffOUT Store here BUFFPEK describing data written to to_file
FbFirst element in source BUFFPEKs array
TbLast element in source BUFFPEKs array
flag
Return values:
0OK
othererror
int merge_many_buff ( Sort_param param,
uchar *  sort_buffer,
BUFFPEK buffpek,
uint *  maxbuffer,
IO_CACHE *  t_file 
)

Merge buffers to make < MERGEBUFF2 buffers.

uint read_to_buffer ( IO_CACHE *  fromfile,
BUFFPEK buffpek,
uint  rec_length 
)

Read data to buffer.

Return values:
(uint)-1if something goes wrong
void reuse_freed_buff ( QUEUE *  queue,
BUFFPEK reuse,
uint  key_length 
)

Put all room used by freed buffer to use in adjacent buffer.

Note, that we can't simply distribute memory evenly between all buffers, because new areas must not overlap with old ones.

Parameters:
[in]queuelist of non-empty buffers, without freed buffer
[in]reuseempty buffer
[in]key_lengthkey length
uint sortlength ( THD *  thd,
SORT_FIELD *  sortorder,
uint  s_length,
bool *  multi_byte_charset 
)

Declared here so we can unit test it.

Calculate length of sort key.

Parameters:
thdThread handler
sortorderOrder of items to sort
s_lengthNumber of items to sort
[out]multi_byte_charsetSet to 1 if we are using multi-byte charset (In which case we have to use strxnfrm())
Note:
sortorder->length is updated for each sort item.
sortorder->need_strxnfrm is set 1 if we have to use strxnfrm
Returns:
Total length of sort buffer in bytes
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines