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
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:
-
| thd | Current thread |
| table | Table to sort |
| filesort | How to sort the table |
| sort_positions | Set 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_rows | Store number of examined rows here This is the number of found rows before applying WHERE condition. |
[out] | found_rows | Store 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_ERROR | Error |
\:: | Number of rows in the result, could be less than found_rows if LIMIT were provided. |
Declared here so we can unit test it.
Make a sort-key from record.
Merge buffers to one buffer.
- Parameters:
-
param | Sort parameter |
from_file | File with source data (BUFFPEKs point to this file) |
to_file | File to write the sorted result data. |
sort_buffer | Buffer for data to store up to MERGEBUFF2 sort keys. |
lastbuff | OUT Store here BUFFPEK describing data written to to_file |
Fb | First element in source BUFFPEKs array |
Tb | Last element in source BUFFPEKs array |
flag | |
- Return values:
-
Merge buffers to make < MERGEBUFF2 buffers.
Read data to buffer.
- Return values:
-
(uint)-1 | if something goes wrong |
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] | queue | list of non-empty buffers, without freed buffer |
[in] | reuse | empty buffer |
[in] | key_length | key 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:
-
| thd | Thread handler |
| sortorder | Order of items to sort |
| s_length | Number of items to sort |
[out] | multi_byte_charset | Set 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