~drizzle-trunk/drizzle/development

660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1
/* Copyright (C) 2000-2006 MySQL AB
1 by brian
clean slate
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
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
1 by brian
clean slate
15
16
17
/**
18
  @file
19
20
  @brief
21
  Sorts a database
22
*/
23
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
24
#include "config.h"
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
25
26
#include <float.h>
1241.9.59 by Monty Taylor
Removed the first mystrings header.
27
#include <limits.h>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
28
29
#include <queue>
30
#include <algorithm>
31
1796.4.1 by Andrew Hutchings
Add initial template work and test
32
#include "drizzled/drizzled.h"
1237.9.2 by Padraig O'Sullivan
Moved opt_range.[cc,h] into the optimizer directory and namespace and renamed the files to
33
#include "drizzled/sql_sort.h"
1905.1.1 by Brian Aker
Adding FileSort class.
34
#include "drizzled/filesort.h"
1237.9.2 by Padraig O'Sullivan
Moved opt_range.[cc,h] into the optimizer directory and namespace and renamed the files to
35
#include "drizzled/error.h"
36
#include "drizzled/probes.h"
37
#include "drizzled/session.h"
38
#include "drizzled/table.h"
39
#include "drizzled/table_list.h"
40
#include "drizzled/optimizer/range.h"
1237.9.4 by Padraig O'Sullivan
Removed the inclusion of drizzled/field.h in the server_includes header file.
41
#include "drizzled/records.h"
1241.9.64 by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal.
42
#include "drizzled/internal/iocache.h"
43
#include "drizzled/internal/my_sys.h"
1241.9.62 by Monty Taylor
Removed plugin/myisam/myisam.h from session.h
44
#include "plugin/myisam/myisam.h"
1273.1.15 by Jay Pipes
This patch completes the first step in the splitting of
45
#include "drizzled/plugin/transactional_storage_engine.h"
1796.4.1 by Andrew Hutchings
Add initial template work and test
46
#include "drizzled/atomics.h"
47
#include "drizzled/global_buffer.h"
1 by brian
clean slate
48
1905.1.1 by Brian Aker
Adding FileSort class.
49
897.1.6 by Padraig
Cleaning up merge_buffers() function a little bit.
50
using namespace std;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
51
52
namespace drizzled
53
{
897.1.6 by Padraig
Cleaning up merge_buffers() function a little bit.
54
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
55
56
/* Defines used by filesort and uniques */
57
#define MERGEBUFF		7
58
#define MERGEBUFF2		15
59
60
class BufferCompareContext
61
{
62
public:
63
  qsort_cmp2 key_compare;
64
  void *key_compare_arg;
65
66
  BufferCompareContext() :
67
    key_compare(0),
68
    key_compare_arg(0)
69
  { }
70
71
};
72
73
class SortParam {
74
public:
75
  uint32_t rec_length;          /* Length of sorted records */
76
  uint32_t sort_length;			/* Length of sorted columns */
77
  uint32_t ref_length;			/* Length of record ref. */
78
  uint32_t addon_length;        /* Length of added packed fields */
79
  uint32_t res_length;          /* Length of records in final sorted file/buffer */
80
  uint32_t keys;				/* Max keys / buffer */
81
  ha_rows max_rows,examined_rows;
82
  Table *sort_form;			/* For quicker make_sortkey */
83
  SortField *local_sortorder;
84
  SortField *end;
85
  sort_addon_field *addon_field; /* Descriptors for companion fields */
86
  unsigned char *unique_buff;
87
  bool not_killable;
88
  char *tmp_buffer;
89
  /* The fields below are used only by Unique class */
90
  qsort2_cmp compare;
91
  BufferCompareContext cmp_context;
92
93
  SortParam() :
94
    rec_length(0),
95
    sort_length(0),
96
    ref_length(0),
97
    addon_length(0),
98
    res_length(0),
99
    keys(0),
100
    max_rows(0),
101
    examined_rows(0),
102
    sort_form(0),
103
    local_sortorder(0),
104
    end(0),
105
    addon_field(0),
106
    unique_buff(0),
107
    not_killable(0),
108
    tmp_buffer(0),
109
    compare(0)
110
  {
111
  }
112
113
  ~SortParam()
114
  {
115
    if (tmp_buffer)
116
      free(tmp_buffer);
117
  }
118
119
  int write_keys(unsigned char * *sort_keys,
120
                 uint32_t count,
121
                 internal::IO_CACHE *buffer_file,
122
                 internal::IO_CACHE *tempfile);
123
124
  void make_sortkey(unsigned char *to,
125
                    unsigned char *ref_pos);
126
  void register_used_fields();
127
  bool save_index(unsigned char **sort_keys,
128
                  uint32_t count,
129
                  filesort_info *table_sort);
130
131
};
132
520.7.1 by Monty Taylor
Moved hash.c to drizzled.
133
/* functions defined in this file */
1 by brian
clean slate
134
438.1.13 by Brian Aker
uint cleanup.
135
static char **make_char_array(char **old_pos, register uint32_t fields,
641.3.7 by Monty Taylor
More my_malloc removal.
136
                              uint32_t length);
1237.9.2 by Padraig O'Sullivan
Moved opt_range.[cc,h] into the optimizer directory and namespace and renamed the files to
137
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
138
static unsigned char *read_buffpek_from_file(internal::IO_CACHE *buffer_file,
139
                                             uint32_t count,
140
                                             unsigned char *buf);
1237.9.2 by Padraig O'Sullivan
Moved opt_range.[cc,h] into the optimizer directory and namespace and renamed the files to
141
438.1.13 by Brian Aker
uint cleanup.
142
static uint32_t suffix_length(uint32_t string_length);
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
143
static void unpack_addon_fields(sort_addon_field *addon_field,
481 by Brian Aker
Remove all of uchar.
144
                                unsigned char *buff);
1905.1.1 by Brian Aker
Adding FileSort class.
145
146
FileSort::FileSort(Session &arg) :
147
  _session(arg)
148
{ 
149
}
150
1 by brian
clean slate
151
/**
152
  Sort a table.
153
  Creates a set of pointers that can be used to read the rows
154
  in sorted order. This should be done with the functions
155
  in records.cc.
156
157
  Before calling filesort, one must have done
158
  table->file->info(HA_STATUS_VARIABLE)
159
160
  The result set is stored in table->io_cache or
161
  table->record_pointers.
162
163
  @param table		Table to sort
164
  @param sortorder	How to sort the table
165
  @param s_length	Number of elements in sortorder
166
  @param select		condition to apply to the rows
167
  @param max_rows	Return only this many rows
168
  @param sort_positions	Set to 1 if we want to force sorting by position
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
169
			(Needed by UPDATE/INSERT or ALTER Table)
1 by brian
clean slate
170
  @param examined_rows	Store number of examined rows here
171
172
  @todo
173
    check why we do this (param.keys--)
174
  @note
175
    If we sort by position (like if sort_positions is 1) filesort() will
176
    call table->prepare_for_position().
177
178
  @retval
179
    HA_POS_ERROR	Error
180
  @retval
181
    \#			Number of rows
182
  @retval
183
    examined_rows	will be set to number of examined rows
184
*/
185
1905.1.1 by Brian Aker
Adding FileSort class.
186
ha_rows FileSort::run(Table *table, SortField *sortorder, uint32_t s_length,
187
                      optimizer::SqlSelect *select, ha_rows max_rows,
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
188
                      bool sort_positions, ha_rows &examined_rows)
1 by brian
clean slate
189
{
1909 by Brian Aker
Filesort encapsulation, plus modification to copy contructor
190
  int error= 1;
1796.4.1 by Andrew Hutchings
Add initial template work and test
191
  uint32_t memavl= 0, min_sort_memory;
438.1.13 by Brian Aker
uint cleanup.
192
  uint32_t maxbuffer;
1796.4.6 by Andrew Hutchings
Fix global sort buffer usage calculation
193
  size_t allocated_sort_memory= 0;
1909 by Brian Aker
Filesort encapsulation, plus modification to copy contructor
194
  buffpek *buffpek_inst= 0;
1 by brian
clean slate
195
  ha_rows records= HA_POS_ERROR;
481 by Brian Aker
Remove all of uchar.
196
  unsigned char **sort_keys= 0;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
197
  internal::IO_CACHE tempfile;
198
  internal::IO_CACHE buffpek_pointers;
199
  internal::IO_CACHE *selected_records_file;
200
  internal::IO_CACHE *outfile;
201
  SortParam param;
1 by brian
clean slate
202
  bool multi_byte_charset;
51.1.12 by Jay Pipes
Removed/replaced DBUG symbols
203
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
204
  /*
205
    Don't use table->sort in filesort as it is also used by
206
    QuickIndexMergeSelect. Work with a copy and put it back at the end
207
    when index_merge select has finished with it.
208
  */
209
  filesort_info table_sort(table->sort);
210
  table->sort.io_cache= NULL;
211
327.2.4 by Brian Aker
Refactoring table.h
212
  TableList *tab= table->pos_in_table_list;
1 by brian
clean slate
213
  Item_subselect *subselect= tab ? tab->containing_subselect() : 0;
214
1532.1.15 by Brian Aker
Partial encapsulation of TableShare from Table.
215
  DRIZZLE_FILESORT_START(table->getShare()->getSchemaName(), table->getShare()->getTableName());
1 by brian
clean slate
216
217
  /*
218
   Release InnoDB's adaptive hash index latch (if holding) before
219
   running a sort.
220
  */
1905.1.1 by Brian Aker
Adding FileSort class.
221
  plugin::TransactionalStorageEngine::releaseTemporaryLatches(&getSession());
1 by brian
clean slate
222
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
223
1 by brian
clean slate
224
  outfile= table_sort.io_cache;
1909 by Brian Aker
Filesort encapsulation, plus modification to copy contructor
225
  assert(tempfile.buffer == 0);
226
  assert(buffpek_pointers.buffer == 0);
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
227
1905.1.1 by Brian Aker
Adding FileSort class.
228
  param.sort_length= sortlength(sortorder, s_length, &multi_byte_charset);
1208.3.2 by brian
Update for Cursor renaming.
229
  param.ref_length= table->cursor->ref_length;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
230
1233.1.5 by Brian Aker
More table_flags converted.
231
  if (!(table->cursor->getEngine()->check_flag(HTON_BIT_FAST_KEY_READ)) && !sort_positions)
1 by brian
clean slate
232
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
233
    /*
234
      Get the descriptors of all fields whose values are appended
1 by brian
clean slate
235
      to sorted fields and get its total length in param.spack_length.
236
    */
1905.1.1 by Brian Aker
Adding FileSort class.
237
    param.addon_field= get_addon_fields(table->getFields(),
1 by brian
clean slate
238
                                        param.sort_length,
239
                                        &param.addon_length);
240
  }
241
242
  table_sort.addon_buf= 0;
243
  table_sort.addon_length= param.addon_length;
244
  table_sort.addon_field= param.addon_field;
245
  table_sort.unpack= unpack_addon_fields;
246
  if (param.addon_field)
247
  {
248
    param.res_length= param.addon_length;
641.3.7 by Monty Taylor
More my_malloc removal.
249
    if (!(table_sort.addon_buf= (unsigned char *) malloc(param.addon_length)))
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
250
    {
1 by brian
clean slate
251
      goto err;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
252
    }
1 by brian
clean slate
253
  }
254
  else
255
  {
256
    param.res_length= param.ref_length;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
257
    /*
258
      The reference to the record is considered
1 by brian
clean slate
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
  {
1905.1.1 by Brian Aker
Adding FileSort class.
268
    getSession().status_var.filesort_range_count++;
1 by brian
clean slate
269
  }
270
  else
271
  {
1905.1.1 by Brian Aker
Adding FileSort class.
272
    getSession().status_var.filesort_scan_count++;
1 by brian
clean slate
273
  }
274
#ifdef CAN_TRUST_RANGE
275
  if (select && select->quick && select->quick->records > 0L)
276
  {
1067.4.1 by Nathan Williams
First few changes at converting cmin to std::min.
277
    records= min((ha_rows) (select->quick->records*2+EXTRA_RECORDS*2),
1208.3.2 by brian
Update for Cursor renaming.
278
                 table->cursor->stats.records)+EXTRA_RECORDS;
1 by brian
clean slate
279
    selected_records_file=0;
280
  }
281
  else
282
#endif
283
  {
1208.3.2 by brian
Update for Cursor renaming.
284
    records= table->cursor->estimate_rows_upper_bound();
1 by brian
clean slate
285
    /*
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
286
      If number of records is not known, use as much of sort buffer
287
      as possible.
1 by brian
clean slate
288
    */
289
    if (records == HA_POS_ERROR)
290
      records--;  // we use 'records+1' below.
291
    selected_records_file= 0;
292
  }
293
1909 by Brian Aker
Filesort encapsulation, plus modification to copy contructor
294
  if (multi_byte_charset && !(param.tmp_buffer= (char*) malloc(param.sort_length)))
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
295
  {
1 by brian
clean slate
296
    goto err;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
297
  }
1 by brian
clean slate
298
1905.1.1 by Brian Aker
Adding FileSort class.
299
  memavl= getSession().variables.sortbuff_size;
1067.4.7 by Nathan Williams
The remaining files using cmax have been converted to std::max.
300
  min_sort_memory= max((uint32_t)MIN_SORT_MEMORY, param.sort_length*MERGEBUFF2);
1 by brian
clean slate
301
  while (memavl >= min_sort_memory)
302
  {
308 by Brian Aker
ulong conversion
303
    uint32_t old_memavl;
304
    uint32_t keys= memavl/(param.rec_length+sizeof(char*));
1067.4.1 by Nathan Williams
First few changes at converting cmin to std::min.
305
    param.keys= (uint32_t) min(records+1, (ha_rows)keys);
1796.4.8 by Andrew Hutchings
Change default setting
306
307
    allocated_sort_memory= param.keys * param.rec_length;
308
    if (not global_sort_buffer.add(allocated_sort_memory))
309
    {
310
      my_error(ER_OUT_OF_GLOBAL_SORTMEMORY, MYF(ME_ERROR+ME_WAITTANG));
311
      goto err;
312
    }
313
1 by brian
clean slate
314
    if ((table_sort.sort_keys=
481 by Brian Aker
Remove all of uchar.
315
	 (unsigned char **) make_char_array((char **) table_sort.sort_keys,
641.3.7 by Monty Taylor
More my_malloc removal.
316
                                            param.keys, param.rec_length)))
1 by brian
clean slate
317
      break;
1796.4.8 by Andrew Hutchings
Change default setting
318
319
    global_sort_buffer.sub(allocated_sort_memory);
1034.1.6 by Brian Aker
Increase the default sort buffer size.
320
    old_memavl= memavl;
321
    if ((memavl= memavl/4*3) < min_sort_memory && old_memavl > min_sort_memory)
1 by brian
clean slate
322
      memavl= min_sort_memory;
323
  }
324
  sort_keys= table_sort.sort_keys;
325
  if (memavl < min_sort_memory)
326
  {
327
    my_error(ER_OUT_OF_SORTMEMORY,MYF(ME_ERROR+ME_WAITTANG));
328
    goto err;
329
  }
1796.4.1 by Andrew Hutchings
Add initial template work and test
330
1909.1.2 by Brian Aker
Additional io_cache encapsulation.
331
  if (buffpek_pointers.open_cached_file(drizzle_tmpdir.c_str(),TEMP_PREFIX, DISK_BUFFER_SIZE, MYF(MY_WME)))
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
332
  {
1 by brian
clean slate
333
    goto err;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
334
  }
1 by brian
clean slate
335
336
  param.keys--;  			/* TODO: check why we do this */
337
  param.sort_form= table;
338
  param.end=(param.local_sortorder=sortorder)+s_length;
1905.1.1 by Brian Aker
Adding FileSort class.
339
  if ((records= find_all_keys(&param,select,sort_keys, &buffpek_pointers,
340
                              &tempfile, selected_records_file)) == HA_POS_ERROR)
341
  {
1 by brian
clean slate
342
    goto err;
1905.1.1 by Brian Aker
Adding FileSort class.
343
  }
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
344
  maxbuffer= (uint32_t) (my_b_tell(&buffpek_pointers)/sizeof(*buffpek_inst));
1 by brian
clean slate
345
346
  if (maxbuffer == 0)			// The whole set is in memory
347
  {
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
348
    if (param.save_index(sort_keys,(uint32_t) records, &table_sort))
349
    {
1 by brian
clean slate
350
      goto err;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
351
    }
1 by brian
clean slate
352
  }
353
  else
354
  {
355
    if (table_sort.buffpek && table_sort.buffpek_len < maxbuffer)
356
    {
460 by Monty Taylor
Removed x_free calls.
357
      if (table_sort.buffpek)
358
        free(table_sort.buffpek);
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
359
      table_sort.buffpek = 0;
1 by brian
clean slate
360
    }
361
    if (!(table_sort.buffpek=
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
362
          (unsigned char *) read_buffpek_from_file(&buffpek_pointers, maxbuffer, table_sort.buffpek)))
363
    {
1 by brian
clean slate
364
      goto err;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
365
    }
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
366
    buffpek_inst= (buffpek *) table_sort.buffpek;
1 by brian
clean slate
367
    table_sort.buffpek_len= maxbuffer;
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
368
    buffpek_pointers.close_cached_file();
1 by brian
clean slate
369
	/* Open cached file if it isn't open */
1909.1.2 by Brian Aker
Additional io_cache encapsulation.
370
    if (! my_b_inited(outfile) && outfile->open_cached_file(drizzle_tmpdir.c_str(),TEMP_PREFIX,READ_RECORD_BUFFER, MYF(MY_WME)))
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
371
    {
1 by brian
clean slate
372
      goto err;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
373
    }
374
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
375
    if (outfile->reinit_io_cache(internal::WRITE_CACHE,0L,0,0))
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
376
    {
1 by brian
clean slate
377
      goto err;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
378
    }
1 by brian
clean slate
379
380
    /*
381
      Use also the space previously used by string pointers in sort_buffer
382
      for temporary key storage.
383
    */
384
    param.keys=((param.keys*(param.rec_length+sizeof(char*))) /
385
		param.rec_length-1);
386
    maxbuffer--;				// Offset from 0
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
387
    if (merge_many_buff(&param,(unsigned char*) sort_keys,buffpek_inst,&maxbuffer, &tempfile))
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
388
    {
389
      goto err;
390
    }
391
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
392
    if (flush_io_cache(&tempfile) || tempfile.reinit_io_cache(internal::READ_CACHE,0L,0,0))
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
393
    {
394
      goto err;
395
    }
396
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
397
    if (merge_index(&param,(unsigned char*) sort_keys,buffpek_inst,maxbuffer,&tempfile, outfile))
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
398
    {
1 by brian
clean slate
399
      goto err;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
400
    }
1 by brian
clean slate
401
  }
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
402
1 by brian
clean slate
403
  if (records > param.max_rows)
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
404
  {
1909 by Brian Aker
Filesort encapsulation, plus modification to copy contructor
405
    records= param.max_rows;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
406
  }
1 by brian
clean slate
407
  error =0;
408
409
 err:
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
410
  if (not subselect || not subselect->is_uncacheable())
1 by brian
clean slate
411
  {
1510.3.3 by mordred
Fixed note from cppcheck - passing NULL to free is valid, so the check for non-null here is redundant. Also, casting to unsigned char * is pretty pointless.
412
    free(sort_keys);
1 by brian
clean slate
413
    table_sort.sort_keys= 0;
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
414
    free(buffpek_inst);
1 by brian
clean slate
415
    table_sort.buffpek= 0;
416
    table_sort.buffpek_len= 0;
417
  }
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
418
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
419
  tempfile.close_cached_file();
420
  buffpek_pointers.close_cached_file();
1 by brian
clean slate
421
  if (my_b_inited(outfile))
422
  {
423
    if (flush_io_cache(outfile))
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
424
    {
1 by brian
clean slate
425
      error=1;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
426
    }
1 by brian
clean slate
427
    {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
428
      internal::my_off_t save_pos=outfile->pos_in_file;
1 by brian
clean slate
429
      /* For following reads */
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
430
      if (outfile->reinit_io_cache(internal::READ_CACHE,0L,0,0))
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
431
      {
1 by brian
clean slate
432
	error=1;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
433
      }
1 by brian
clean slate
434
      outfile->end_of_file=save_pos;
435
    }
436
  }
437
  if (error)
1689.5.1 by Joseph Daly
remove increment calls
438
  {
1 by brian
clean slate
439
    my_message(ER_FILSORT_ABORT, ER(ER_FILSORT_ABORT),
440
               MYF(ME_ERROR+ME_WAITTANG));
1689.5.1 by Joseph Daly
remove increment calls
441
  }
1 by brian
clean slate
442
  else
1689.5.1 by Joseph Daly
remove increment calls
443
  {
1905.1.1 by Brian Aker
Adding FileSort class.
444
    getSession().status_var.filesort_rows+= (uint32_t) records;
1689.5.1 by Joseph Daly
remove increment calls
445
  }
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
446
  examined_rows= param.examined_rows;
1796.4.6 by Andrew Hutchings
Fix global sort buffer usage calculation
447
  global_sort_buffer.sub(allocated_sort_memory);
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
448
  table->sort= table_sort;
1126.10.9 by Padraig O'Sullivan
Added calls to the filesort dtrace probes.
449
  DRIZZLE_FILESORT_DONE(error, records);
450
  return (error ? HA_POS_ERROR : records);
1 by brian
clean slate
451
} /* filesort */
452
453
/** Make a array of string pointers. */
454
438.1.13 by Brian Aker
uint cleanup.
455
static char **make_char_array(char **old_pos, register uint32_t fields,
641.3.7 by Monty Taylor
More my_malloc removal.
456
                              uint32_t length)
1 by brian
clean slate
457
{
458
  register char **pos;
459
  char *char_pos;
460
461
  if (old_pos ||
641.3.9 by Monty Taylor
More removal of my_malloc.
462
      (old_pos= (char**) malloc((uint32_t) fields*(length+sizeof(char*)))))
1 by brian
clean slate
463
  {
464
    pos=old_pos; char_pos=((char*) (pos+fields)) -length;
465
    while (fields--) *(pos++) = (char_pos+= length);
466
  }
467
51.1.12 by Jay Pipes
Removed/replaced DBUG symbols
468
  return(old_pos);
1 by brian
clean slate
469
} /* make_char_array */
470
471
472
/** Read 'count' number of buffer pointers into memory. */
473
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
474
static unsigned char *read_buffpek_from_file(internal::IO_CACHE *buffpek_pointers, uint32_t count,
481 by Brian Aker
Remove all of uchar.
475
                                     unsigned char *buf)
1 by brian
clean slate
476
{
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
477
  uint32_t length= sizeof(buffpek)*count;
481 by Brian Aker
Remove all of uchar.
478
  unsigned char *tmp= buf;
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
479
  if (count > UINT_MAX/sizeof(buffpek))
480
    return 0; /* sizeof(buffpek)*count will overflow */
1 by brian
clean slate
481
  if (!tmp)
641.3.7 by Monty Taylor
More my_malloc removal.
482
    tmp= (unsigned char *)malloc(length);
1 by brian
clean slate
483
  if (tmp)
484
  {
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
485
    if (buffpek_pointers->reinit_io_cache(internal::READ_CACHE,0L,0,0) ||
481 by Brian Aker
Remove all of uchar.
486
	my_b_read(buffpek_pointers, (unsigned char*) tmp, length))
1 by brian
clean slate
487
    {
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
488
      free((char*) tmp);
1 by brian
clean slate
489
      tmp=0;
490
    }
491
  }
51.1.12 by Jay Pipes
Removed/replaced DBUG symbols
492
  return(tmp);
1 by brian
clean slate
493
}
494
495
496
/**
497
  Search after sort_keys and write them into tempfile.
498
  All produced sequences are guaranteed to be non-empty.
499
500
  @param param             Sorting parameter
501
  @param select            Use this to get source data
502
  @param sort_keys         Array of pointers to sort key + addon buffers.
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
503
  @param buffpek_pointers  File to write buffpeks describing sorted segments
1 by brian
clean slate
504
                           in tempfile.
505
  @param tempfile          File to write sorted sequences of sortkeys to.
506
  @param indexfile         If !NULL, use it for source data (contains rowids)
507
508
  @note
509
    Basic idea:
510
    @verbatim
511
     while (get_next_sortkey())
512
     {
513
       if (no free space in sort_keys buffers)
514
       {
515
         sort sort_keys buffer;
516
         dump sorted sequence to 'tempfile';
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
517
         dump buffpek describing sequence location into 'buffpek_pointers';
1 by brian
clean slate
518
       }
519
       put sort key into 'sort_keys';
520
     }
521
     if (sort_keys has some elements && dumped at least once)
522
       sort-dump-dump as above;
523
     else
524
       don't sort, leave sort_keys array to be sorted by caller.
525
  @endverbatim
526
527
  @retval
528
    Number of records written on success.
529
  @retval
530
    HA_POS_ERROR on error.
531
*/
532
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
533
ha_rows FileSort::find_all_keys(SortParam *param, 
1905.1.1 by Brian Aker
Adding FileSort class.
534
                                optimizer::SqlSelect *select,
535
                                unsigned char **sort_keys,
536
                                internal::IO_CACHE *buffpek_pointers,
537
                                internal::IO_CACHE *tempfile, internal::IO_CACHE *indexfile)
1 by brian
clean slate
538
{
539
  int error,flag,quick_select;
438.1.13 by Brian Aker
uint cleanup.
540
  uint32_t idx,indexpos,ref_length;
481 by Brian Aker
Remove all of uchar.
541
  unsigned char *ref_pos,*next_pos,ref_buff[MAX_REFLENGTH];
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
542
  internal::my_off_t record;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
543
  Table *sort_form;
1910.2.8 by Brian Aker
Enapsulate Kill.
544
  volatile Session::killed_state_t *killed= getSession().getKilledPtr();
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
545
  Cursor *file;
1802.16.8 by Padraig O'Sullivan
Removal of all MyBitmap from the code base. Compiles but test failures exist now.
546
  boost::dynamic_bitset<> *save_read_set= NULL;
547
  boost::dynamic_bitset<> *save_write_set= NULL;
1 by brian
clean slate
548
549
  idx=indexpos=0;
550
  error=quick_select=0;
551
  sort_form=param->sort_form;
1208.3.2 by brian
Update for Cursor renaming.
552
  file= sort_form->cursor;
1 by brian
clean slate
553
  ref_length=param->ref_length;
554
  ref_pos= ref_buff;
555
  quick_select=select && select->quick;
556
  record=0;
1220.2.1 by Brian Aker
Move cursor flags up to storage engine flags. These need to be merged.
557
  flag= ((!indexfile && ! file->isOrdered())
1 by brian
clean slate
558
	 || quick_select);
559
  if (indexfile || flag)
560
    ref_pos= &file->ref[0];
561
  next_pos=ref_pos;
562
  if (! indexfile && ! quick_select)
563
  {
481 by Brian Aker
Remove all of uchar.
564
    next_pos=(unsigned char*) 0;			/* Find records in sequence */
1491.1.10 by Jay Pipes
ha_rnd_init -> startTableScan, rnd_init -> doStartTableScan, ha_rnd_end -> endTableScan, rnd_end -> doEndTableScan
565
    file->startTableScan(1);
1905.1.1 by Brian Aker
Adding FileSort class.
566
    file->extra_opt(HA_EXTRA_CACHE, getSession().variables.read_buff_size);
1 by brian
clean slate
567
  }
568
1538 by Brian Aker
Code shuffle on ReadRecord
569
  ReadRecord read_record_info;
1 by brian
clean slate
570
  if (quick_select)
571
  {
572
    if (select->quick->reset())
51.1.12 by Jay Pipes
Removed/replaced DBUG symbols
573
      return(HA_POS_ERROR);
1538 by Brian Aker
Code shuffle on ReadRecord
574
1905.1.1 by Brian Aker
Adding FileSort class.
575
    read_record_info.init_read_record(&getSession(), select->quick->head, select, 1, 1);
1 by brian
clean slate
576
  }
577
578
  /* Remember original bitmaps */
579
  save_read_set=  sort_form->read_set;
580
  save_write_set= sort_form->write_set;
581
  /* Set up temporary column read map for columns used by sort */
1802.16.8 by Padraig O'Sullivan
Removal of all MyBitmap from the code base. Compiles but test failures exist now.
582
  sort_form->tmp_set.reset();
1 by brian
clean slate
583
  /* Temporary set for register_used_fields and register_field_in_read_map */
584
  sort_form->read_set= &sort_form->tmp_set;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
585
  param->register_used_fields();
1 by brian
clean slate
586
  if (select && select->cond)
587
    select->cond->walk(&Item::register_field_in_read_map, 1,
481 by Brian Aker
Remove all of uchar.
588
                       (unsigned char*) sort_form);
1802.16.8 by Padraig O'Sullivan
Removal of all MyBitmap from the code base. Compiles but test failures exist now.
589
  sort_form->column_bitmaps_set(sort_form->tmp_set, sort_form->tmp_set);
1 by brian
clean slate
590
591
  for (;;)
592
  {
593
    if (quick_select)
594
    {
595
      if ((error= read_record_info.read_record(&read_record_info)))
596
      {
597
        error= HA_ERR_END_OF_FILE;
598
        break;
599
      }
1672.3.6 by Brian Aker
First pass in encapsulating row
600
      file->position(sort_form->getInsertRecord());
1 by brian
clean slate
601
    }
602
    else					/* Not quick-select */
603
    {
604
      if (indexfile)
605
      {
971.6.11 by Eric Day
Removed purecov messages.
606
	if (my_b_read(indexfile,(unsigned char*) ref_pos,ref_length))
1 by brian
clean slate
607
	{
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
608
	  error= errno ? errno : -1;		/* Abort */
1 by brian
clean slate
609
	  break;
610
	}
1672.3.6 by Brian Aker
First pass in encapsulating row
611
	error=file->rnd_pos(sort_form->getInsertRecord(),next_pos);
1 by brian
clean slate
612
      }
613
      else
614
      {
1672.3.6 by Brian Aker
First pass in encapsulating row
615
	error=file->rnd_next(sort_form->getInsertRecord());
383.7.1 by Andrey Zhakov
Initial submit of code and tests
616
1 by brian
clean slate
617
	if (!flag)
618
	{
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
619
	  internal::my_store_ptr(ref_pos,ref_length,record); // Position to row
1532.1.15 by Brian Aker
Partial encapsulation of TableShare from Table.
620
	  record+= sort_form->getShare()->db_record_offset;
1 by brian
clean slate
621
	}
622
	else if (!error)
1672.3.6 by Brian Aker
First pass in encapsulating row
623
	  file->position(sort_form->getInsertRecord());
1 by brian
clean slate
624
      }
625
      if (error && error != HA_ERR_RECORD_DELETED)
626
	break;
627
    }
628
629
    if (*killed)
630
    {
631
      if (!indexfile && !quick_select)
632
      {
633
        (void) file->extra(HA_EXTRA_NO_CACHE);
1491.1.10 by Jay Pipes
ha_rnd_init -> startTableScan, rnd_init -> doStartTableScan, ha_rnd_end -> endTableScan, rnd_end -> doEndTableScan
634
        file->endTableScan();
1 by brian
clean slate
635
      }
971.6.11 by Eric Day
Removed purecov messages.
636
      return(HA_POS_ERROR);
1 by brian
clean slate
637
    }
638
    if (error == 0)
639
      param->examined_rows++;
640
    if (error == 0 && (!select || select->skip_record() == 0))
641
    {
642
      if (idx == param->keys)
643
      {
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
644
	if (param->write_keys(sort_keys, idx, buffpek_pointers, tempfile))
51.1.12 by Jay Pipes
Removed/replaced DBUG symbols
645
	  return(HA_POS_ERROR);
1 by brian
clean slate
646
	idx=0;
647
	indexpos++;
648
      }
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
649
      param->make_sortkey(sort_keys[idx++], ref_pos);
1 by brian
clean slate
650
    }
651
    else
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
652
    {
1 by brian
clean slate
653
      file->unlock_row();
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
654
    }
655
1 by brian
clean slate
656
    /* It does not make sense to read more keys in case of a fatal error */
1905.1.1 by Brian Aker
Adding FileSort class.
657
    if (getSession().is_error())
1 by brian
clean slate
658
      break;
659
  }
660
  if (quick_select)
661
  {
662
    /*
663
      index_merge quick select uses table->sort when retrieving rows, so free
664
      resoures it has allocated.
665
    */
1538 by Brian Aker
Code shuffle on ReadRecord
666
    read_record_info.end_read_record();
1 by brian
clean slate
667
  }
668
  else
669
  {
670
    (void) file->extra(HA_EXTRA_NO_CACHE);	/* End cacheing of records */
671
    if (!next_pos)
1491.1.10 by Jay Pipes
ha_rnd_init -> startTableScan, rnd_init -> doStartTableScan, ha_rnd_end -> endTableScan, rnd_end -> doEndTableScan
672
      file->endTableScan();
1 by brian
clean slate
673
  }
674
1905.1.1 by Brian Aker
Adding FileSort class.
675
  if (getSession().is_error())
51.1.12 by Jay Pipes
Removed/replaced DBUG symbols
676
    return(HA_POS_ERROR);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
677
1 by brian
clean slate
678
  /* Signal we should use orignal column read and write maps */
1802.16.8 by Padraig O'Sullivan
Removal of all MyBitmap from the code base. Compiles but test failures exist now.
679
  sort_form->column_bitmaps_set(*save_read_set, *save_write_set);
1 by brian
clean slate
680
681
  if (error != HA_ERR_END_OF_FILE)
682
  {
1216.1.1 by Brian Aker
Move print_error up to Engine.
683
    sort_form->print_error(error,MYF(ME_ERROR | ME_WAITTANG));
971.6.11 by Eric Day
Removed purecov messages.
684
    return(HA_POS_ERROR);
1 by brian
clean slate
685
  }
686
  if (indexpos && idx &&
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
687
      param->write_keys(sort_keys,idx,buffpek_pointers,tempfile))
971.6.11 by Eric Day
Removed purecov messages.
688
    return(HA_POS_ERROR);
51.1.12 by Jay Pipes
Removed/replaced DBUG symbols
689
  return(my_b_inited(tempfile) ?
1 by brian
clean slate
690
	      (ha_rows) (my_b_tell(tempfile)/param->rec_length) :
691
	      idx);
692
} /* find_all_keys */
693
694
695
/**
696
  @details
697
  Sort the buffer and write:
698
  -# the sorted sequence to tempfile
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
699
  -# a buffpek describing the sorted sequence position to buffpek_pointers
1 by brian
clean slate
700
701
    (was: Skriver en buffert med nycklar till filen)
702
703
  @param param             Sort parameters
704
  @param sort_keys         Array of pointers to keys to sort
705
  @param count             Number of elements in sort_keys array
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
706
  @param buffpek_pointers  One 'buffpek' struct will be written into this file.
707
                           The buffpek::{file_pos, count} will indicate where
1 by brian
clean slate
708
                           the sorted data was stored.
709
  @param tempfile          The sorted sequence will be written into this file.
710
711
  @retval
712
    0 OK
713
  @retval
714
    1 Error
715
*/
716
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
717
int SortParam::write_keys(register unsigned char **sort_keys, uint32_t count,
718
                          internal::IO_CACHE *buffpek_pointers, internal::IO_CACHE *tempfile)
1 by brian
clean slate
719
{
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
720
  buffpek buffpek;
1 by brian
clean slate
721
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
722
  internal::my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, sort_length);
1 by brian
clean slate
723
  if (!my_b_inited(tempfile) &&
1909.1.2 by Brian Aker
Additional io_cache encapsulation.
724
      tempfile->open_cached_file(drizzle_tmpdir.c_str(), TEMP_PREFIX, DISK_BUFFER_SIZE, MYF(MY_WME)))
1864.3.19 by Brian Aker
Update to remove additional goto.
725
  {
726
    return 1;
727
  }
1 by brian
clean slate
728
  /* check we won't have more buffpeks than we can possibly keep in memory */
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
729
  if (my_b_tell(buffpek_pointers) + sizeof(buffpek) > (uint64_t)UINT_MAX)
1864.3.19 by Brian Aker
Update to remove additional goto.
730
  {
731
    return 1;
732
  }
733
1 by brian
clean slate
734
  buffpek.file_pos= my_b_tell(tempfile);
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
735
  if ((ha_rows) count > max_rows)
736
    count=(uint32_t) max_rows;
737
1 by brian
clean slate
738
  buffpek.count=(ha_rows) count;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
739
740
  for (unsigned char **ptr= sort_keys + count ; sort_keys != ptr ; sort_keys++)
1864.3.19 by Brian Aker
Update to remove additional goto.
741
  {
481 by Brian Aker
Remove all of uchar.
742
    if (my_b_write(tempfile, (unsigned char*) *sort_keys, (uint32_t) rec_length))
1864.3.19 by Brian Aker
Update to remove additional goto.
743
    {
744
      return 1;
745
    }
746
  }
747
481 by Brian Aker
Remove all of uchar.
748
  if (my_b_write(buffpek_pointers, (unsigned char*) &buffpek, sizeof(buffpek)))
1864.3.19 by Brian Aker
Update to remove additional goto.
749
  {
750
    return 1;
751
  }
1 by brian
clean slate
752
1864.3.19 by Brian Aker
Update to remove additional goto.
753
  return 0;
1 by brian
clean slate
754
} /* write_keys */
755
756
757
/**
758
  Store length as suffix in high-byte-first order.
759
*/
760
481 by Brian Aker
Remove all of uchar.
761
static inline void store_length(unsigned char *to, uint32_t length, uint32_t pack_length)
1 by brian
clean slate
762
{
763
  switch (pack_length) {
764
  case 1:
481 by Brian Aker
Remove all of uchar.
765
    *to= (unsigned char) length;
1 by brian
clean slate
766
    break;
767
  case 2:
768
    mi_int2store(to, length);
769
    break;
770
  case 3:
771
    mi_int3store(to, length);
772
    break;
773
  default:
774
    mi_int4store(to, length);
775
    break;
776
  }
777
}
778
779
780
/** Make a sort-key from record. */
781
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
782
void SortParam::make_sortkey(register unsigned char *to, unsigned char *ref_pos)
1 by brian
clean slate
783
{
1711.6.1 by Brian Aker
Style on structure cleanup
784
  Field *field;
785
  SortField *sort_field;
937.2.7 by Stewart Smith
typesafe set_if_smaller/set_if_greater fixes from Solaris
786
  size_t length;
1 by brian
clean slate
787
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
788
  for (sort_field= local_sortorder ;
789
       sort_field != end ;
1 by brian
clean slate
790
       sort_field++)
791
  {
792
    bool maybe_null=0;
793
    if ((field=sort_field->field))
794
    {						// Field
795
      if (field->maybe_null())
796
      {
797
	if (field->is_null())
798
	{
799
	  if (sort_field->reverse)
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
800
	    memset(to, 255, sort_field->length+1);
1 by brian
clean slate
801
	  else
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
802
	    memset(to, 0, sort_field->length+1);
1 by brian
clean slate
803
	  to+= sort_field->length+1;
804
	  continue;
805
	}
806
	else
807
	  *to++=1;
808
      }
809
      field->sort_string(to, sort_field->length);
810
    }
811
    else
812
    {						// Item
813
      Item *item=sort_field->item;
814
      maybe_null= item->maybe_null;
815
      switch (sort_field->result_type) {
816
      case STRING_RESULT:
817
      {
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
818
        const CHARSET_INFO * const cs=item->collation.collation;
1 by brian
clean slate
819
        char fill_char= ((cs->state & MY_CS_BINSORT) ? (char) 0 : ' ');
820
        int diff;
438.1.13 by Brian Aker
uint cleanup.
821
        uint32_t sort_field_length;
1 by brian
clean slate
822
823
        if (maybe_null)
824
          *to++=1;
825
        /* All item->str() to use some extra byte for end null.. */
826
        String tmp((char*) to,sort_field->length+4,cs);
827
        String *res= item->str_result(&tmp);
828
        if (!res)
829
        {
830
          if (maybe_null)
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
831
            memset(to-1, 0, sort_field->length+1);
1 by brian
clean slate
832
          else
833
          {
834
            /*
835
              This should only happen during extreme conditions if we run out
836
              of memory or have an item marked not null when it can be null.
837
              This code is here mainly to avoid a hard crash in this case.
838
            */
51.1.12 by Jay Pipes
Removed/replaced DBUG symbols
839
            assert(0);
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
840
            memset(to, 0, sort_field->length);	// Avoid crash
1 by brian
clean slate
841
          }
842
          break;
843
        }
844
        length= res->length();
845
        sort_field_length= sort_field->length - sort_field->suffix_length;
846
        diff=(int) (sort_field_length - length);
847
        if (diff < 0)
848
        {
849
          diff=0;
850
          length= sort_field_length;
851
        }
852
        if (sort_field->suffix_length)
853
        {
854
          /* Store length last in result_string */
855
          store_length(to + sort_field_length, length,
856
                       sort_field->suffix_length);
857
        }
858
        if (sort_field->need_strxnfrm)
859
        {
860
          char *from=(char*) res->ptr();
438.1.13 by Brian Aker
uint cleanup.
861
          uint32_t tmp_length;
481 by Brian Aker
Remove all of uchar.
862
          if ((unsigned char*) from == to)
1 by brian
clean slate
863
          {
864
            set_if_smaller(length,sort_field->length);
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
865
            memcpy(tmp_buffer,from,length);
866
            from= tmp_buffer;
1 by brian
clean slate
867
          }
868
          tmp_length= my_strnxfrm(cs,to,sort_field->length,
481 by Brian Aker
Remove all of uchar.
869
                                  (unsigned char*) from, length);
51.1.12 by Jay Pipes
Removed/replaced DBUG symbols
870
          assert(tmp_length == sort_field->length);
1 by brian
clean slate
871
        }
872
        else
873
        {
481 by Brian Aker
Remove all of uchar.
874
          my_strnxfrm(cs,(unsigned char*)to,length,(const unsigned char*)res->ptr(),length);
1 by brian
clean slate
875
          cs->cset->fill(cs, (char *)to+length,diff,fill_char);
876
        }
877
        break;
878
      }
879
      case INT_RESULT:
880
	{
152 by Brian Aker
longlong replacement
881
          int64_t value= item->val_int_result();
1 by brian
clean slate
882
          if (maybe_null)
883
          {
971.6.11 by Eric Day
Removed purecov messages.
884
	    *to++=1;
1 by brian
clean slate
885
            if (item->null_value)
886
            {
887
              if (maybe_null)
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
888
                memset(to-1, 0, sort_field->length+1);
1 by brian
clean slate
889
              else
890
              {
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
891
                memset(to, 0, sort_field->length);
1 by brian
clean slate
892
              }
893
              break;
894
            }
895
          }
481 by Brian Aker
Remove all of uchar.
896
	  to[7]= (unsigned char) value;
897
	  to[6]= (unsigned char) (value >> 8);
898
	  to[5]= (unsigned char) (value >> 16);
899
	  to[4]= (unsigned char) (value >> 24);
900
	  to[3]= (unsigned char) (value >> 32);
901
	  to[2]= (unsigned char) (value >> 40);
902
	  to[1]= (unsigned char) (value >> 48);
1 by brian
clean slate
903
          if (item->unsigned_flag)                    /* Fix sign */
481 by Brian Aker
Remove all of uchar.
904
            to[0]= (unsigned char) (value >> 56);
1 by brian
clean slate
905
          else
481 by Brian Aker
Remove all of uchar.
906
            to[0]= (unsigned char) (value >> 56) ^ 128;	/* Reverse signbit */
1 by brian
clean slate
907
	  break;
908
	}
909
      case DECIMAL_RESULT:
910
        {
911
          my_decimal dec_buf, *dec_val= item->val_decimal_result(&dec_buf);
912
          if (maybe_null)
913
          {
914
            if (item->null_value)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
915
            {
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
916
              memset(to, 0, sort_field->length+1);
1 by brian
clean slate
917
              to++;
918
              break;
919
            }
920
            *to++=1;
921
          }
922
          my_decimal2binary(E_DEC_FATAL_ERROR, dec_val, to,
923
                            item->max_length - (item->decimals ? 1:0),
924
                            item->decimals);
925
         break;
926
        }
927
      case REAL_RESULT:
928
	{
929
          double value= item->val_result();
930
	  if (maybe_null)
931
          {
932
            if (item->null_value)
933
            {
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
934
              memset(to, 0, sort_field->length+1);
1 by brian
clean slate
935
              to++;
936
              break;
937
            }
938
	    *to++=1;
939
          }
481 by Brian Aker
Remove all of uchar.
940
	  change_double_for_sort(value,(unsigned char*) to);
1 by brian
clean slate
941
	  break;
942
	}
943
      case ROW_RESULT:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
944
      default:
1 by brian
clean slate
945
	// This case should never be choosen
51.1.12 by Jay Pipes
Removed/replaced DBUG symbols
946
	assert(0);
1 by brian
clean slate
947
	break;
948
      }
949
    }
950
    if (sort_field->reverse)
951
    {							/* Revers key */
952
      if (maybe_null)
953
        to[-1]= ~to[-1];
954
      length=sort_field->length;
955
      while (length--)
956
      {
481 by Brian Aker
Remove all of uchar.
957
	*to = (unsigned char) (~ *to);
1 by brian
clean slate
958
	to++;
959
      }
960
    }
961
    else
962
      to+= sort_field->length;
963
  }
964
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
965
  if (addon_field)
1 by brian
clean slate
966
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
967
    /*
1 by brian
clean slate
968
      Save field values appended to sorted fields.
969
      First null bit indicators are appended then field values follow.
970
      In this implementation we use fixed layout for field values -
971
      the same for all records.
972
    */
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
973
    sort_addon_field *addonf= addon_field;
481 by Brian Aker
Remove all of uchar.
974
    unsigned char *nulls= to;
51.1.12 by Jay Pipes
Removed/replaced DBUG symbols
975
    assert(addonf != 0);
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
976
    memset(nulls, 0, addonf->offset);
1 by brian
clean slate
977
    to+= addonf->offset;
978
    for ( ; (field= addonf->field) ; addonf++)
979
    {
980
      if (addonf->null_bit && field->is_null())
981
      {
982
        nulls[addonf->null_offset]|= addonf->null_bit;
1859.2.14 by Brian Aker
Add support for --with-valgrind
983
#ifdef HAVE_VALGRIND
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
984
	memset(to, 0, addonf->length);
1 by brian
clean slate
985
#endif
986
      }
987
      else
988
      {
1859.2.14 by Brian Aker
Add support for --with-valgrind
989
#ifdef HAVE_VALGRIND
481 by Brian Aker
Remove all of uchar.
990
        unsigned char *end= field->pack(to, field->ptr);
1859.2.14 by Brian Aker
Add support for --with-valgrind
991
	uint32_t local_length= (uint32_t) ((to + addonf->length) - end);
992
	assert((int) local_length >= 0);
993
	if (local_length)
994
	  memset(end, 0, local_length);
1 by brian
clean slate
995
#else
996
        (void) field->pack(to, field->ptr);
997
#endif
998
      }
999
      to+= addonf->length;
1000
    }
1001
  }
1002
  else
1003
  {
1004
    /* Save filepos last */
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
1005
    memcpy(to, ref_pos, (size_t) ref_length);
1 by brian
clean slate
1006
  }
1007
}
1008
1009
1010
/*
1011
  Register fields used by sorting in the sorted table's read set
1012
*/
1013
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
1014
void SortParam::register_used_fields()
1 by brian
clean slate
1015
{
1711.6.1 by Brian Aker
Style on structure cleanup
1016
  SortField *sort_field;
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
1017
  Table *table= sort_form;
1 by brian
clean slate
1018
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
1019
  for (sort_field= local_sortorder ;
1020
       sort_field != end ;
1 by brian
clean slate
1021
       sort_field++)
1022
  {
1023
    Field *field;
1024
    if ((field= sort_field->field))
1025
    {
1660.1.3 by Brian Aker
Encapsulate Table in field
1026
      if (field->getTable() == table)
1005.2.12 by Monty Taylor
Moved some things to the API.
1027
        table->setReadSet(field->field_index);
1 by brian
clean slate
1028
    }
1029
    else
1030
    {						// Item
1031
      sort_field->item->walk(&Item::register_field_in_read_map, 1,
481 by Brian Aker
Remove all of uchar.
1032
                             (unsigned char *) table);
1 by brian
clean slate
1033
    }
1034
  }
1035
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
1036
  if (addon_field)
1 by brian
clean slate
1037
  {
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
1038
    sort_addon_field *addonf= addon_field;
1 by brian
clean slate
1039
    Field *field;
1040
    for ( ; (field= addonf->field) ; addonf++)
1005.2.12 by Monty Taylor
Moved some things to the API.
1041
      table->setReadSet(field->field_index);
1 by brian
clean slate
1042
  }
1043
  else
1044
  {
1045
    /* Save filepos last */
1046
    table->prepare_for_position();
1047
  }
1048
}
1049
1050
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
1051
bool SortParam::save_index(unsigned char **sort_keys, uint32_t count,
1052
                           filesort_info *table_sort)
1 by brian
clean slate
1053
{
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
1054
  uint32_t offset;
481 by Brian Aker
Remove all of uchar.
1055
  unsigned char *to;
1 by brian
clean slate
1056
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
1057
  internal::my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, sort_length);
1058
  offset= rec_length - res_length;
1059
  if ((ha_rows) count > max_rows)
1060
    count=(uint32_t) max_rows;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1061
  if (!(to= table_sort->record_pointers=
641.3.7 by Monty Taylor
More my_malloc removal.
1062
        (unsigned char*) malloc(res_length*count)))
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
1063
    return true;
1064
1065
  for (unsigned char **end_ptr= sort_keys+count ; sort_keys != end_ptr ; sort_keys++)
1 by brian
clean slate
1066
  {
1067
    memcpy(to, *sort_keys+offset, res_length);
1068
    to+= res_length;
1069
  }
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
1070
  return false;
1 by brian
clean slate
1071
}
1072
1073
1074
/** Merge buffers to make < MERGEBUFF2 buffers. */
1075
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
1076
int FileSort::merge_many_buff(SortParam *param, unsigned char *sort_buffer,
1905.1.2 by Brian Aker
Filesort fix.
1077
                              buffpek *buffpek_inst, uint32_t *maxbuffer, internal::IO_CACHE *t_file)
1 by brian
clean slate
1078
{
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
1079
  internal::IO_CACHE t_file2,*from_file,*to_file,*temp;
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1080
  buffpek *lastbuff;
1 by brian
clean slate
1081
1082
  if (*maxbuffer < MERGEBUFF2)
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1083
    return 0;
1 by brian
clean slate
1084
  if (flush_io_cache(t_file) ||
1909.1.2 by Brian Aker
Additional io_cache encapsulation.
1085
      t_file2.open_cached_file(drizzle_tmpdir.c_str(),TEMP_PREFIX,DISK_BUFFER_SIZE, MYF(MY_WME)))
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1086
  {
1087
    return 1;
1088
  }
1 by brian
clean slate
1089
1090
  from_file= t_file ; to_file= &t_file2;
1091
  while (*maxbuffer >= MERGEBUFF2)
1092
  {
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1093
    register uint32_t i;
1094
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
1095
    if (from_file->reinit_io_cache(internal::READ_CACHE,0L,0,0))
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1096
    {
1097
      break;
1098
    }
1099
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
1100
    if (to_file->reinit_io_cache(internal::WRITE_CACHE,0L,0,0))
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1101
    {
1102
      break;
1103
    }
1104
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1105
    lastbuff=buffpek_inst;
1 by brian
clean slate
1106
    for (i=0 ; i <= *maxbuffer-MERGEBUFF*3/2 ; i+=MERGEBUFF)
1107
    {
1108
      if (merge_buffers(param,from_file,to_file,sort_buffer,lastbuff++,
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1109
			buffpek_inst+i,buffpek_inst+i+MERGEBUFF-1,0))
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1110
      {
1111
        goto cleanup;
1112
      }
1 by brian
clean slate
1113
    }
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1114
1 by brian
clean slate
1115
    if (merge_buffers(param,from_file,to_file,sort_buffer,lastbuff++,
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1116
		      buffpek_inst+i,buffpek_inst+ *maxbuffer,0))
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1117
    {
971.6.11 by Eric Day
Removed purecov messages.
1118
      break;
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1119
    }
1120
1 by brian
clean slate
1121
    if (flush_io_cache(to_file))
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1122
    {
971.6.11 by Eric Day
Removed purecov messages.
1123
      break;
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1124
    }
1125
1 by brian
clean slate
1126
    temp=from_file; from_file=to_file; to_file=temp;
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
1127
    from_file->setup_io_cache();
1128
    to_file->setup_io_cache();
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1129
    *maxbuffer= (uint32_t) (lastbuff-buffpek_inst)-1;
1 by brian
clean slate
1130
  }
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1131
1 by brian
clean slate
1132
cleanup:
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
1133
  to_file->close_cached_file();			// This holds old result
1 by brian
clean slate
1134
  if (to_file == t_file)
1135
  {
1136
    *t_file=t_file2;				// Copy result file
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
1137
    t_file->setup_io_cache();
1 by brian
clean slate
1138
  }
1139
51.1.12 by Jay Pipes
Removed/replaced DBUG symbols
1140
  return(*maxbuffer >= MERGEBUFF2);	/* Return 1 if interrupted */
1 by brian
clean slate
1141
} /* merge_many_buff */
1142
1143
1144
/**
1145
  Read data to buffer.
1146
1147
  @retval
438.1.13 by Brian Aker
uint cleanup.
1148
    (uint32_t)-1 if something goes wrong
1 by brian
clean slate
1149
*/
1150
1905.1.2 by Brian Aker
Filesort fix.
1151
uint32_t FileSort::read_to_buffer(internal::IO_CACHE *fromfile, buffpek *buffpek_inst, uint32_t rec_length)
1 by brian
clean slate
1152
{
438.1.13 by Brian Aker
uint cleanup.
1153
  register uint32_t count;
1154
  uint32_t length;
1 by brian
clean slate
1155
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1156
  if ((count= (uint32_t) min((ha_rows) buffpek_inst->max_keys,buffpek_inst->count)))
1 by brian
clean slate
1157
  {
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1158
    if (pread(fromfile->file,(unsigned char*) buffpek_inst->base, (length= rec_length*count),buffpek_inst->file_pos) == 0)
971.6.11 by Eric Day
Removed purecov messages.
1159
      return((uint32_t) -1);
1067.4.1 by Nathan Williams
First few changes at converting cmin to std::min.
1160
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1161
    buffpek_inst->key= buffpek_inst->base;
1162
    buffpek_inst->file_pos+= length;			/* New filepos */
1163
    buffpek_inst->count-= count;
1164
    buffpek_inst->mem_count= count;
1 by brian
clean slate
1165
  }
1166
  return (count*rec_length);
1167
} /* read_to_buffer */
1168
1169
897.1.11 by Padraig
Giving function object a better name for this scenario.
1170
class compare_functor
897.1.5 by Padraig
Replacing the instance of QUEUE in the merge_buffers() function in
1171
{
897.1.6 by Padraig
Cleaning up merge_buffers() function a little bit.
1172
  qsort2_cmp key_compare;
897.1.5 by Padraig
Replacing the instance of QUEUE in the merge_buffers() function in
1173
  void *key_compare_arg;
1174
  public:
897.1.11 by Padraig
Giving function object a better name for this scenario.
1175
  compare_functor(qsort2_cmp in_key_compare, void *in_compare_arg)
897.1.6 by Padraig
Cleaning up merge_buffers() function a little bit.
1176
    : key_compare(in_key_compare), key_compare_arg(in_compare_arg) { }
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1177
  inline bool operator()(const buffpek *i, const buffpek *j) const
897.1.5 by Padraig
Replacing the instance of QUEUE in the merge_buffers() function in
1178
  {
897.1.16 by Padraig O'Sullivan
Cleaning up my function object. Making it const correct and putting
1179
    int val= key_compare(key_compare_arg,
1180
                      &i->key, &j->key);
897.1.18 by Padraig O'Sullivan
Cleaning up my function object a little bit.
1181
    return (val >= 0);
897.1.5 by Padraig
Replacing the instance of QUEUE in the merge_buffers() function in
1182
  }
1183
};
1184
1 by brian
clean slate
1185
1186
/**
1187
  Merge buffers to one buffer.
1188
1189
  @param param        Sort parameter
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1190
  @param from_file    File with source data (buffpeks point to this file)
1 by brian
clean slate
1191
  @param to_file      File to write the sorted result data.
1192
  @param sort_buffer  Buffer for data to store up to MERGEBUFF2 sort keys.
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1193
  @param lastbuff     OUT Store here buffpek describing data written to to_file
1194
  @param Fb           First element in source buffpeks array
1195
  @param Tb           Last element in source buffpeks array
1 by brian
clean slate
1196
  @param flag
1197
1198
  @retval
1199
    0      OK
1200
  @retval
1201
    other  error
1202
*/
1203
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
1204
int FileSort::merge_buffers(SortParam *param, internal::IO_CACHE *from_file,
1905.1.2 by Brian Aker
Filesort fix.
1205
                            internal::IO_CACHE *to_file, unsigned char *sort_buffer,
1206
                            buffpek *lastbuff, buffpek *Fb, buffpek *Tb,
1207
                            int flag)
1 by brian
clean slate
1208
{
1209
  int error;
438.1.13 by Brian Aker
uint cleanup.
1210
  uint32_t rec_length,res_length,offset;
1 by brian
clean slate
1211
  size_t sort_length;
308 by Brian Aker
ulong conversion
1212
  uint32_t maxcount;
1 by brian
clean slate
1213
  ha_rows max_rows,org_max_rows;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
1214
  internal::my_off_t to_start_filepos;
481 by Brian Aker
Remove all of uchar.
1215
  unsigned char *strpos;
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1216
  buffpek *buffpek_inst;
1 by brian
clean slate
1217
  qsort2_cmp cmp;
1218
  void *first_cmp_arg;
1910.2.8 by Brian Aker
Enapsulate Kill.
1219
  volatile Session::killed_state_t *killed= getSession().getKilledPtr();
1220
  Session::killed_state_t not_killable;
1 by brian
clean slate
1221
1905.1.2 by Brian Aker
Filesort fix.
1222
  getSession().status_var.filesort_merge_passes++;
1 by brian
clean slate
1223
  if (param->not_killable)
1224
  {
1225
    killed= &not_killable;
520.1.21 by Brian Aker
THD -> Session rename
1226
    not_killable= Session::NOT_KILLED;
1 by brian
clean slate
1227
  }
1228
1229
  error=0;
1230
  rec_length= param->rec_length;
1231
  res_length= param->res_length;
1232
  sort_length= param->sort_length;
1233
  offset= rec_length-res_length;
438.1.13 by Brian Aker
uint cleanup.
1234
  maxcount= (uint32_t) (param->keys/((uint32_t) (Tb-Fb) +1));
1 by brian
clean slate
1235
  to_start_filepos= my_b_tell(to_file);
481 by Brian Aker
Remove all of uchar.
1236
  strpos= (unsigned char*) sort_buffer;
1 by brian
clean slate
1237
  org_max_rows=max_rows= param->max_rows;
1238
1239
  /* The following will fire if there is not enough space in sort_buffer */
51.1.12 by Jay Pipes
Removed/replaced DBUG symbols
1240
  assert(maxcount!=0);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1241
1 by brian
clean slate
1242
  if (param->unique_buff)
1243
  {
1244
    cmp= param->compare;
1245
    first_cmp_arg= (void *) &param->cmp_context;
1246
  }
1247
  else
1248
  {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
1249
    cmp= internal::get_ptr_compare(sort_length);
1 by brian
clean slate
1250
    first_cmp_arg= (void*) &sort_length;
1251
  }
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1252
  priority_queue<buffpek *, vector<buffpek *>, compare_functor >
897.1.11 by Padraig
Giving function object a better name for this scenario.
1253
    queue(compare_functor(cmp, first_cmp_arg));
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1254
  for (buffpek_inst= Fb ; buffpek_inst <= Tb ; buffpek_inst++)
1 by brian
clean slate
1255
  {
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1256
    buffpek_inst->base= strpos;
1257
    buffpek_inst->max_keys= maxcount;
1258
    strpos+= (uint32_t) (error= (int) read_to_buffer(from_file, buffpek_inst,
1 by brian
clean slate
1259
                                                                         rec_length));
1260
    if (error == -1)
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1261
      return -1;
1262
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1263
    buffpek_inst->max_keys= buffpek_inst->mem_count;	// If less data in buffers than expected
1264
    queue.push(buffpek_inst);
1 by brian
clean slate
1265
  }
1266
1267
  if (param->unique_buff)
1268
  {
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1269
    /*
1 by brian
clean slate
1270
       Called by Unique::get()
1271
       Copy the first argument to param->unique_buff for unique removal.
1272
       Store it also in 'to_file'.
1273
1274
       This is safe as we know that there is always more than one element
1275
       in each block to merge (This is guaranteed by the Unique:: algorithm
1276
    */
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1277
    buffpek_inst= queue.top();
1278
    memcpy(param->unique_buff, buffpek_inst->key, rec_length);
1279
    if (my_b_write(to_file, (unsigned char*) buffpek_inst->key, rec_length))
1 by brian
clean slate
1280
    {
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1281
      return 1;
1 by brian
clean slate
1282
    }
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1283
    buffpek_inst->key+= rec_length;
1284
    buffpek_inst->mem_count--;
1 by brian
clean slate
1285
    if (!--max_rows)
1286
    {
971.6.11 by Eric Day
Removed purecov messages.
1287
      error= 0;
1288
      goto end;
1 by brian
clean slate
1289
    }
897.1.5 by Padraig
Replacing the instance of QUEUE in the merge_buffers() function in
1290
    /* Top element has been used */
1291
    queue.pop();
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1292
    queue.push(buffpek_inst);
1 by brian
clean slate
1293
  }
1294
  else
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1295
  {
1 by brian
clean slate
1296
    cmp= 0;                                        // Not unique
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1297
  }
1 by brian
clean slate
1298
897.1.6 by Padraig
Cleaning up merge_buffers() function a little bit.
1299
  while (queue.size() > 1)
1 by brian
clean slate
1300
  {
1301
    if (*killed)
1302
    {
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1303
      return 1;
1 by brian
clean slate
1304
    }
1305
    for (;;)
1306
    {
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1307
      buffpek_inst= queue.top();
1 by brian
clean slate
1308
      if (cmp)                                        // Remove duplicates
1309
      {
1310
        if (!(*cmp)(first_cmp_arg, &(param->unique_buff),
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1311
                    (unsigned char**) &buffpek_inst->key))
1 by brian
clean slate
1312
              goto skip_duplicate;
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1313
            memcpy(param->unique_buff, buffpek_inst->key, rec_length);
1 by brian
clean slate
1314
      }
1315
      if (flag == 0)
1316
      {
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1317
        if (my_b_write(to_file,(unsigned char*) buffpek_inst->key, rec_length))
1 by brian
clean slate
1318
        {
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1319
          return 1;
1 by brian
clean slate
1320
        }
1321
      }
1322
      else
1323
      {
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1324
        if (my_b_write(to_file, (unsigned char*) buffpek_inst->key+offset, res_length))
1 by brian
clean slate
1325
        {
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1326
          return 1;
1 by brian
clean slate
1327
        }
1328
      }
1329
      if (!--max_rows)
1330
      {
971.6.11 by Eric Day
Removed purecov messages.
1331
        error= 0;
1332
        goto end;
1 by brian
clean slate
1333
      }
1334
1335
    skip_duplicate:
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1336
      buffpek_inst->key+= rec_length;
1337
      if (! --buffpek_inst->mem_count)
1 by brian
clean slate
1338
      {
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1339
        if (!(error= (int) read_to_buffer(from_file,buffpek_inst,
1 by brian
clean slate
1340
                                          rec_length)))
1341
        {
897.1.5 by Padraig
Replacing the instance of QUEUE in the merge_buffers() function in
1342
          queue.pop();
1 by brian
clean slate
1343
          break;                        /* One buffer have been removed */
1344
        }
1345
        else if (error == -1)
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1346
        {
1347
          return -1;
1348
        }
1 by brian
clean slate
1349
      }
897.1.5 by Padraig
Replacing the instance of QUEUE in the merge_buffers() function in
1350
      /* Top element has been replaced */
1351
      queue.pop();
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1352
      queue.push(buffpek_inst);
1 by brian
clean slate
1353
    }
1354
  }
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1355
  buffpek_inst= queue.top();
1356
  buffpek_inst->base= sort_buffer;
1357
  buffpek_inst->max_keys= param->keys;
1 by brian
clean slate
1358
1359
  /*
1360
    As we know all entries in the buffer are unique, we only have to
1361
    check if the first one is the same as the last one we wrote
1362
  */
1363
  if (cmp)
1364
  {
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1365
    if (!(*cmp)(first_cmp_arg, &(param->unique_buff), (unsigned char**) &buffpek_inst->key))
1 by brian
clean slate
1366
    {
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1367
      buffpek_inst->key+= rec_length;         // Remove duplicate
1368
      --buffpek_inst->mem_count;
1 by brian
clean slate
1369
    }
1370
  }
1371
1372
  do
1373
  {
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1374
    if ((ha_rows) buffpek_inst->mem_count > max_rows)
1 by brian
clean slate
1375
    {                                        /* Don't write too many records */
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1376
      buffpek_inst->mem_count= (uint32_t) max_rows;
1377
      buffpek_inst->count= 0;                        /* Don't read more */
1 by brian
clean slate
1378
    }
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1379
    max_rows-= buffpek_inst->mem_count;
1 by brian
clean slate
1380
    if (flag == 0)
1381
    {
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1382
      if (my_b_write(to_file,(unsigned char*) buffpek_inst->key,
1383
                     (rec_length*buffpek_inst->mem_count)))
1 by brian
clean slate
1384
      {
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1385
        return 1;
1 by brian
clean slate
1386
      }
1387
    }
1388
    else
1389
    {
481 by Brian Aker
Remove all of uchar.
1390
      register unsigned char *end;
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1391
      strpos= buffpek_inst->key+offset;
1392
      for (end= strpos+buffpek_inst->mem_count*rec_length ;
1 by brian
clean slate
1393
           strpos != end ;
1394
           strpos+= rec_length)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1395
      {
481 by Brian Aker
Remove all of uchar.
1396
        if (my_b_write(to_file, (unsigned char *) strpos, res_length))
1 by brian
clean slate
1397
        {
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1398
          return 1;
1 by brian
clean slate
1399
        }
1400
      }
1401
    }
1402
  }
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1403
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1404
  while ((error=(int) read_to_buffer(from_file,buffpek_inst, rec_length))
1 by brian
clean slate
1405
         != -1 && error != 0);
1406
1407
end:
1067.4.1 by Nathan Williams
First few changes at converting cmin to std::min.
1408
  lastbuff->count= min(org_max_rows-max_rows, param->max_rows);
1 by brian
clean slate
1409
  lastbuff->file_pos= to_start_filepos;
1864.3.20 by Brian Aker
Remove goto from filesort (not all).
1410
1411
  return error;
1 by brian
clean slate
1412
} /* merge_buffers */
1413
1414
1415
	/* Do a merge to output-file (save only positions) */
1416
1908.1.1 by Brian Aker
Merge in encapsulations in filesort.
1417
int FileSort::merge_index(SortParam *param, unsigned char *sort_buffer,
1905.1.2 by Brian Aker
Filesort fix.
1418
                          buffpek *buffpek_inst, uint32_t maxbuffer,
1419
                          internal::IO_CACHE *tempfile, internal::IO_CACHE *outfile)
1 by brian
clean slate
1420
{
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1421
  if (merge_buffers(param,tempfile,outfile,sort_buffer,buffpek_inst,buffpek_inst,
1422
		    buffpek_inst+maxbuffer,1))
1905.1.2 by Brian Aker
Filesort fix.
1423
    return 1;
1424
1425
  return 0;
1 by brian
clean slate
1426
} /* merge_index */
1427
1428
438.1.13 by Brian Aker
uint cleanup.
1429
static uint32_t suffix_length(uint32_t string_length)
1 by brian
clean slate
1430
{
1431
  if (string_length < 256)
1432
    return 1;
1433
  if (string_length < 256L*256L)
1434
    return 2;
1435
  if (string_length < 256L*256L*256L)
1436
    return 3;
1437
  return 4;                                     // Can't sort longer than 4G
1438
}
1439
1440
1441
1442
/**
1443
  Calculate length of sort key.
1444
1445
  @param sortorder		  Order of items to sort
1446
  @param s_length	          Number of items to sort
1447
  @param[out] multi_byte_charset Set to 1 if we are using multi-byte charset
1448
                                 (In which case we have to use strxnfrm())
1449
1450
  @note
1451
    sortorder->length is updated for each sort item.
1452
  @n
1453
    sortorder->need_strxnfrm is set 1 if we have to use strxnfrm
1454
1455
  @return
1456
    Total length of sort buffer in bytes
1457
*/
1458
1905.1.1 by Brian Aker
Adding FileSort class.
1459
uint32_t FileSort::sortlength(SortField *sortorder, uint32_t s_length, bool *multi_byte_charset)
1 by brian
clean slate
1460
{
438.1.13 by Brian Aker
uint cleanup.
1461
  register uint32_t length;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1462
  const CHARSET_INFO *cs;
1 by brian
clean slate
1463
  *multi_byte_charset= 0;
1464
1465
  length=0;
1466
  for (; s_length-- ; sortorder++)
1467
  {
1468
    sortorder->need_strxnfrm= 0;
1469
    sortorder->suffix_length= 0;
1470
    if (sortorder->field)
1471
    {
1472
      cs= sortorder->field->sort_charset();
1473
      sortorder->length= sortorder->field->sort_length();
1474
1475
      if (use_strnxfrm((cs=sortorder->field->sort_charset())))
1476
      {
1477
        sortorder->need_strxnfrm= 1;
1478
        *multi_byte_charset= 1;
1479
        sortorder->length= cs->coll->strnxfrmlen(cs, sortorder->length);
1480
      }
1481
      if (sortorder->field->maybe_null())
1482
	length++;				// Place for NULL marker
1483
    }
1484
    else
1485
    {
1486
      sortorder->result_type= sortorder->item->result_type();
152 by Brian Aker
longlong replacement
1487
      if (sortorder->item->result_as_int64_t())
1 by brian
clean slate
1488
        sortorder->result_type= INT_RESULT;
1489
      switch (sortorder->result_type) {
1490
      case STRING_RESULT:
1491
	sortorder->length=sortorder->item->max_length;
892.2.2 by Monty Taylor
More solaris warnings.
1492
        set_if_smaller(sortorder->length,
1905.1.1 by Brian Aker
Adding FileSort class.
1493
                       getSession().variables.max_sort_length);
1 by brian
clean slate
1494
	if (use_strnxfrm((cs=sortorder->item->collation.collation)))
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1495
	{
1 by brian
clean slate
1496
          sortorder->length= cs->coll->strnxfrmlen(cs, sortorder->length);
1497
	  sortorder->need_strxnfrm= 1;
1498
	  *multi_byte_charset= 1;
1499
	}
1500
        else if (cs == &my_charset_bin)
1501
        {
1502
          /* Store length last to be able to sort blob/varbinary */
1503
          sortorder->suffix_length= suffix_length(sortorder->length);
1504
          sortorder->length+= sortorder->suffix_length;
1505
        }
1506
	break;
1507
      case INT_RESULT:
152 by Brian Aker
longlong replacement
1508
	sortorder->length=8;			// Size of intern int64_t
1 by brian
clean slate
1509
	break;
1510
      case DECIMAL_RESULT:
1511
        sortorder->length=
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1512
          my_decimal_get_binary_size(sortorder->item->max_length -
1 by brian
clean slate
1513
                                     (sortorder->item->decimals ? 1 : 0),
1514
                                     sortorder->item->decimals);
1515
        break;
1516
      case REAL_RESULT:
1517
	sortorder->length=sizeof(double);
1518
	break;
1519
      case ROW_RESULT:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1520
      default:
1 by brian
clean slate
1521
	// This case should never be choosen
51.1.12 by Jay Pipes
Removed/replaced DBUG symbols
1522
	assert(0);
1 by brian
clean slate
1523
	break;
1524
      }
1525
      if (sortorder->item->maybe_null)
1526
	length++;				// Place for NULL marker
1527
    }
1905.1.1 by Brian Aker
Adding FileSort class.
1528
    set_if_smaller(sortorder->length, (size_t)getSession().variables.max_sort_length);
1 by brian
clean slate
1529
    length+=sortorder->length;
1530
  }
1531
  sortorder->field= (Field*) 0;			// end marker
1532
  return length;
1533
}
1534
1535
1536
/**
1537
  Get descriptors of fields appended to sorted fields and
1538
  calculate its total length.
1539
1540
  The function first finds out what fields are used in the result set.
1541
  Then it calculates the length of the buffer to store the values of
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1542
  these fields together with the value of sort values.
1 by brian
clean slate
1543
  If the calculated length is not greater than max_length_for_sort_data
1544
  the function allocates memory for an array of descriptors containing
1545
  layouts for the values of the non-sorted fields in the buffer and
1546
  fills them.
1547
1548
  @param ptabfield           Array of references to the table fields
1549
  @param sortlength          Total length of sorted fields
1550
  @param[out] plength        Total length of appended fields
1551
1552
  @note
1553
    The null bits for the appended values are supposed to be put together
1554
    and stored the buffer just ahead of the value of the first field.
1555
1556
  @return
1557
    Pointer to the layout descriptors for the appended fields, if any
1558
  @retval
1559
    NULL   if we do not store field values with sort data.
1560
*/
1561
1905.1.1 by Brian Aker
Adding FileSort class.
1562
sort_addon_field *FileSort::get_addon_fields(Field **ptabfield, uint32_t sortlength_arg, uint32_t *plength)
1 by brian
clean slate
1563
{
1564
  Field **pfield;
1565
  Field *field;
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1566
  sort_addon_field *addonf;
438.1.13 by Brian Aker
uint cleanup.
1567
  uint32_t length= 0;
1568
  uint32_t fields= 0;
1569
  uint32_t null_fields= 0;
1 by brian
clean slate
1570
1571
  /*
1572
    If there is a reference to a field in the query add it
1573
    to the the set of appended fields.
1574
    Note for future refinement:
1575
    This this a too strong condition.
1576
    Actually we need only the fields referred in the
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1577
    result set. And for some of them it makes sense to use
1 by brian
clean slate
1578
    the values directly from sorted fields.
1579
  */
1580
  *plength= 0;
1581
1582
  for (pfield= ptabfield; (field= *pfield) ; pfield++)
1583
  {
1003.1.12 by Brian Aker
Begin of abstract out the bitmap from direct reference.
1584
    if (!(field->isReadSet()))
1 by brian
clean slate
1585
      continue;
1586
    if (field->flags & BLOB_FLAG)
1587
      return 0;
1588
    length+= field->max_packed_col_length(field->pack_length());
1589
    if (field->maybe_null())
1590
      null_fields++;
1591
    fields++;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1592
  }
1 by brian
clean slate
1593
  if (!fields)
1594
    return 0;
1595
  length+= (null_fields+7)/8;
1596
1905.1.1 by Brian Aker
Adding FileSort class.
1597
  if (length+sortlength_arg > getSession().variables.max_length_for_sort_data ||
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1598
      !(addonf= (sort_addon_field *) malloc(sizeof(sort_addon_field)*
641.3.7 by Monty Taylor
More my_malloc removal.
1599
                                            (fields+1))))
1 by brian
clean slate
1600
    return 0;
1601
1602
  *plength= length;
1603
  length= (null_fields+7)/8;
1604
  null_fields= 0;
1605
  for (pfield= ptabfield; (field= *pfield) ; pfield++)
1606
  {
1003.1.12 by Brian Aker
Begin of abstract out the bitmap from direct reference.
1607
    if (!(field->isReadSet()))
1 by brian
clean slate
1608
      continue;
1609
    addonf->field= field;
1610
    addonf->offset= length;
1611
    if (field->maybe_null())
1612
    {
1613
      addonf->null_offset= null_fields/8;
1614
      addonf->null_bit= 1<<(null_fields & 7);
1615
      null_fields++;
1616
    }
1617
    else
1618
    {
1619
      addonf->null_offset= 0;
1620
      addonf->null_bit= 0;
1621
    }
1622
    addonf->length= field->max_packed_col_length(field->pack_length());
1623
    length+= addonf->length;
1624
    addonf++;
1625
  }
1626
  addonf->field= 0;     // Put end marker
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1627
1 by brian
clean slate
1628
  return (addonf-fields);
1629
}
1630
1631
1632
/**
1633
  Copy (unpack) values appended to sorted fields from a buffer back to
1634
  their regular positions specified by the Field::ptr pointers.
1635
1636
  @param addon_field     Array of descriptors for appended fields
1637
  @param buff            Buffer which to unpack the value from
1638
1639
  @note
1640
    The function is supposed to be used only as a callback function
1641
    when getting field values for the sorted result set.
1642
1643
  @return
1644
    void.
1645
*/
1646
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1647
static void
1891.2.1 by Monty Taylor
Fixed things to make things compile with clang
1648
unpack_addon_fields(sort_addon_field *addon_field, unsigned char *buff)
1 by brian
clean slate
1649
{
1650
  Field *field;
1826.1.1 by tdavies
Bug:621861 Changed C structs to C++ class in the following files: filesort.cc, filesort_info.h, sql_sort.h, table.h. removed the '_st' from the name of some of the classes. For more detail of changes made read the merge proposal notes.
1651
  sort_addon_field *addonf= addon_field;
1 by brian
clean slate
1652
1653
  for ( ; (field= addonf->field) ; addonf++)
1654
  {
1655
    if (addonf->null_bit && (addonf->null_bit & buff[addonf->null_offset]))
1656
    {
1657
      field->set_null();
1658
      continue;
1659
    }
1660
    field->set_notnull();
1661
    field->unpack(field->ptr, buff + addonf->offset);
1662
  }
1663
}
1664
1665
/*
1666
** functions to change a double or float to a sortable string
1667
** The following should work for IEEE
1668
*/
1669
1670
#define DBL_EXP_DIG (sizeof(double)*8-DBL_MANT_DIG)
1671
481 by Brian Aker
Remove all of uchar.
1672
void change_double_for_sort(double nr,unsigned char *to)
1 by brian
clean slate
1673
{
481 by Brian Aker
Remove all of uchar.
1674
  unsigned char *tmp=(unsigned char*) to;
1 by brian
clean slate
1675
  if (nr == 0.0)
1676
  {						/* Change to zero string */
481 by Brian Aker
Remove all of uchar.
1677
    tmp[0]=(unsigned char) 128;
212.6.6 by Mats Kindahl
Removing redundant use of casts in drizzled/ for memcmp(), memcpy(), memset(), and memmove().
1678
    memset(tmp+1, 0, sizeof(nr)-1);
1 by brian
clean slate
1679
  }
1680
  else
1681
  {
1682
#ifdef WORDS_BIGENDIAN
212.6.3 by Mats Kindahl
Removing deprecated functions from code and replacing them with C99 equivalents:
1683
    memcpy(tmp,&nr,sizeof(nr));
1 by brian
clean slate
1684
#else
1685
    {
481 by Brian Aker
Remove all of uchar.
1686
      unsigned char *ptr= (unsigned char*) &nr;
1 by brian
clean slate
1687
#if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
1688
      tmp[0]= ptr[3]; tmp[1]=ptr[2]; tmp[2]= ptr[1]; tmp[3]=ptr[0];
1689
      tmp[4]= ptr[7]; tmp[5]=ptr[6]; tmp[6]= ptr[5]; tmp[7]=ptr[4];
1690
#else
1691
      tmp[0]= ptr[7]; tmp[1]=ptr[6]; tmp[2]= ptr[5]; tmp[3]=ptr[4];
1692
      tmp[4]= ptr[3]; tmp[5]=ptr[2]; tmp[6]= ptr[1]; tmp[7]=ptr[0];
1693
#endif
1694
    }
1695
#endif
1696
    if (tmp[0] & 128)				/* Negative */
1697
    {						/* make complement */
438.1.13 by Brian Aker
uint cleanup.
1698
      uint32_t i;
1 by brian
clean slate
1699
      for (i=0 ; i < sizeof(nr); i++)
481 by Brian Aker
Remove all of uchar.
1700
	tmp[i]=tmp[i] ^ (unsigned char) 255;
1 by brian
clean slate
1701
    }
1702
    else
1703
    {					/* Set high and move exponent one up */
438.1.13 by Brian Aker
uint cleanup.
1704
      uint16_t exp_part=(((uint16_t) tmp[0] << 8) | (uint16_t) tmp[1] |
1705
		       (uint16_t) 32768);
1706
      exp_part+= (uint16_t) 1 << (16-1-DBL_EXP_DIG);
481 by Brian Aker
Remove all of uchar.
1707
      tmp[0]= (unsigned char) (exp_part >> 8);
1708
      tmp[1]= (unsigned char) exp_part;
1 by brian
clean slate
1709
    }
1710
  }
1711
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
1712
1713
} /* namespace drizzled */