~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/filesort.cc

  • Committer: Stewart Smith
  • Date: 2010-11-03 03:28:23 UTC
  • mto: (1902.1.1 build) (1910.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1903.
  • Revision ID: stewart@flamingspork.com-20101103032823-44k21f0njmk97omr
fix docs warning: Title underline (and overline) is too short in brief_history_of_drizzle.rst

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
#include <queue>
30
30
#include <algorithm>
31
 
#include <iostream>
32
31
 
33
 
#include "drizzled/drizzled.h"
34
32
#include "drizzled/sql_sort.h"
35
 
#include "drizzled/filesort.h"
36
33
#include "drizzled/error.h"
37
34
#include "drizzled/probes.h"
38
35
#include "drizzled/session.h"
44
41
#include "drizzled/internal/my_sys.h"
45
42
#include "plugin/myisam/myisam.h"
46
43
#include "drizzled/plugin/transactional_storage_engine.h"
47
 
#include "drizzled/atomics.h"
48
 
#include "drizzled/global_buffer.h"
49
 
 
50
44
 
51
45
using namespace std;
52
46
 
53
47
namespace drizzled
54
48
{
55
49
 
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
 
 
133
50
/* functions defined in this file */
134
51
 
135
52
static char **make_char_array(char **old_pos, register uint32_t fields,
139
56
                                             uint32_t count,
140
57
                                             unsigned char *buf);
141
58
 
 
59
static ha_rows find_all_keys(Session *session,
 
60
                             SORTPARAM *param,
 
61
                             optimizer::SqlSelect *select,
 
62
                             unsigned char * *sort_keys, 
 
63
                             internal::IO_CACHE *buffer_file,
 
64
                             internal::IO_CACHE *tempfile,
 
65
                             internal::IO_CACHE *indexfile);
 
66
 
 
67
static int write_keys(SORTPARAM *param,
 
68
                      unsigned char * *sort_keys,
 
69
                      uint32_t count,
 
70
                      internal::IO_CACHE *buffer_file,
 
71
                      internal::IO_CACHE *tempfile);
 
72
 
 
73
static void make_sortkey(SORTPARAM *param,
 
74
                         unsigned char *to,
 
75
                         unsigned char *ref_pos);
 
76
static void register_used_fields(SORTPARAM *param);
 
77
static int merge_index(SORTPARAM *param,
 
78
                       unsigned char *sort_buffer,
 
79
                       buffpek *buffpek,
 
80
                       uint32_t maxbuffer,
 
81
                       internal::IO_CACHE *tempfile,
 
82
                       internal::IO_CACHE *outfile);
 
83
static bool save_index(SORTPARAM *param,
 
84
                       unsigned char **sort_keys,
 
85
                       uint32_t count,
 
86
                       filesort_info *table_sort);
142
87
static uint32_t suffix_length(uint32_t string_length);
 
88
static uint32_t sortlength(Session *session,
 
89
                           SortField *sortorder,
 
90
                           uint32_t s_length,
 
91
                           bool *multi_byte_charset);
 
92
static sort_addon_field *get_addon_fields(Session *session,
 
93
                                             Field **ptabfield,
 
94
                                             uint32_t sortlength,
 
95
                                             uint32_t *plength);
143
96
static void unpack_addon_fields(sort_addon_field *addon_field,
144
97
                                unsigned char *buff);
145
 
 
146
 
FileSort::FileSort(Session &arg) :
147
 
  _session(arg)
148
 
149
 
}
150
 
 
151
98
/**
152
99
  Sort a table.
153
100
  Creates a set of pointers that can be used to read the rows
160
107
  The result set is stored in table->io_cache or
161
108
  table->record_pointers.
162
109
 
 
110
  @param session           Current thread
163
111
  @param table          Table to sort
164
112
  @param sortorder      How to sort the table
165
113
  @param s_length       Number of elements in sortorder
183
131
    examined_rows       will be set to number of examined rows
184
132
*/
185
133
 
186
 
ha_rows FileSort::run(Table *table, SortField *sortorder, uint32_t s_length,
187
 
                      optimizer::SqlSelect *select, ha_rows max_rows,
188
 
                      bool sort_positions, ha_rows &examined_rows)
 
134
ha_rows filesort(Session *session, Table *table, SortField *sortorder, uint32_t s_length,
 
135
                 optimizer::SqlSelect *select, ha_rows max_rows,
 
136
                 bool sort_positions, ha_rows *examined_rows)
189
137
{
190
 
  int error= 1;
191
 
  uint32_t memavl= 0, min_sort_memory;
 
138
  int error;
 
139
  uint32_t memavl, min_sort_memory;
192
140
  uint32_t maxbuffer;
193
 
  size_t allocated_sort_memory= 0;
194
 
  buffpek *buffpek_inst= 0;
 
141
  buffpek *buffpek_inst;
195
142
  ha_rows records= HA_POS_ERROR;
196
143
  unsigned char **sort_keys= 0;
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;
 
144
  internal::IO_CACHE tempfile, buffpek_pointers, *selected_records_file, *outfile;
 
145
  SORTPARAM param;
202
146
  bool multi_byte_charset;
203
147
 
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
 
 
 
148
  filesort_info table_sort;
212
149
  TableList *tab= table->pos_in_table_list;
213
150
  Item_subselect *subselect= tab ? tab->containing_subselect() : 0;
214
151
 
218
155
   Release InnoDB's adaptive hash index latch (if holding) before
219
156
   running a sort.
220
157
  */
221
 
  plugin::TransactionalStorageEngine::releaseTemporaryLatches(&getSession());
 
158
  plugin::TransactionalStorageEngine::releaseTemporaryLatches(session);
222
159
 
 
160
  /*
 
161
    Don't use table->sort in filesort as it is also used by
 
162
    QuickIndexMergeSelect. Work with a copy and put it back at the end
 
163
    when index_merge select has finished with it.
 
164
  */
 
165
  memcpy(&table_sort, &table->sort, sizeof(filesort_info));
 
166
  table->sort.io_cache= NULL;
223
167
 
224
168
  outfile= table_sort.io_cache;
225
 
  assert(tempfile.buffer == 0);
226
 
  assert(buffpek_pointers.buffer == 0);
227
 
 
228
 
  param.sort_length= sortlength(sortorder, s_length, &multi_byte_charset);
 
169
  my_b_clear(&tempfile);
 
170
  my_b_clear(&buffpek_pointers);
 
171
  buffpek_inst=0;
 
172
  error= 1;
 
173
  memset(&param, 0, sizeof(param));
 
174
  param.sort_length= sortlength(session, sortorder, s_length, &multi_byte_charset);
229
175
  param.ref_length= table->cursor->ref_length;
230
 
 
 
176
  param.addon_field= 0;
 
177
  param.addon_length= 0;
231
178
  if (!(table->cursor->getEngine()->check_flag(HTON_BIT_FAST_KEY_READ)) && !sort_positions)
232
179
  {
233
180
    /*
234
181
      Get the descriptors of all fields whose values are appended
235
182
      to sorted fields and get its total length in param.spack_length.
236
183
    */
237
 
    param.addon_field= get_addon_fields(table->getFields(),
 
184
    param.addon_field= get_addon_fields(session, table->getFields(),
238
185
                                        param.sort_length,
239
186
                                        &param.addon_length);
240
187
  }
247
194
  {
248
195
    param.res_length= param.addon_length;
249
196
    if (!(table_sort.addon_buf= (unsigned char *) malloc(param.addon_length)))
250
 
    {
251
197
      goto err;
252
 
    }
253
198
  }
254
199
  else
255
200
  {
265
210
 
266
211
  if (select && select->quick)
267
212
  {
268
 
    getSession().status_var.filesort_range_count++;
 
213
    session->status_var.filesort_range_count++;
269
214
  }
270
215
  else
271
216
  {
272
 
    getSession().status_var.filesort_scan_count++;
 
217
    session->status_var.filesort_scan_count++;
273
218
  }
274
219
#ifdef CAN_TRUST_RANGE
275
220
  if (select && select->quick && select->quick->records > 0L)
291
236
    selected_records_file= 0;
292
237
  }
293
238
 
294
 
  if (multi_byte_charset && !(param.tmp_buffer= (char*) malloc(param.sort_length)))
295
 
  {
 
239
  if (multi_byte_charset &&
 
240
      !(param.tmp_buffer= (char*) malloc(param.sort_length)))
296
241
    goto err;
297
 
  }
298
242
 
299
 
  memavl= getSession().variables.sortbuff_size;
 
243
  memavl= session->variables.sortbuff_size;
300
244
  min_sort_memory= max((uint32_t)MIN_SORT_MEMORY, param.sort_length*MERGEBUFF2);
301
245
  while (memavl >= min_sort_memory)
302
246
  {
303
247
    uint32_t old_memavl;
304
248
    uint32_t keys= memavl/(param.rec_length+sizeof(char*));
305
249
    param.keys= (uint32_t) min(records+1, (ha_rows)keys);
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
 
 
314
250
    if ((table_sort.sort_keys=
315
251
         (unsigned char **) make_char_array((char **) table_sort.sort_keys,
316
252
                                            param.keys, param.rec_length)))
317
253
      break;
318
 
 
319
 
    global_sort_buffer.sub(allocated_sort_memory);
320
254
    old_memavl= memavl;
321
255
    if ((memavl= memavl/4*3) < min_sort_memory && old_memavl > min_sort_memory)
322
256
      memavl= min_sort_memory;
327
261
    my_error(ER_OUT_OF_SORTMEMORY,MYF(ME_ERROR+ME_WAITTANG));
328
262
    goto err;
329
263
  }
330
 
 
331
 
  if (buffpek_pointers.open_cached_file(drizzle_tmpdir.c_str(),TEMP_PREFIX, DISK_BUFFER_SIZE, MYF(MY_WME)))
332
 
  {
 
264
  if (open_cached_file(&buffpek_pointers,drizzle_tmpdir.c_str(),TEMP_PREFIX,
 
265
                       DISK_BUFFER_SIZE, MYF(MY_WME)))
333
266
    goto err;
334
 
  }
335
267
 
336
268
  param.keys--;                         /* TODO: check why we do this */
337
269
  param.sort_form= table;
338
270
  param.end=(param.local_sortorder=sortorder)+s_length;
339
 
  if ((records= find_all_keys(&param,select,sort_keys, &buffpek_pointers,
340
 
                              &tempfile, selected_records_file)) == HA_POS_ERROR)
341
 
  {
 
271
  if ((records=find_all_keys(session, &param,select,sort_keys, &buffpek_pointers,
 
272
                             &tempfile, selected_records_file)) ==
 
273
      HA_POS_ERROR)
342
274
    goto err;
343
 
  }
344
275
  maxbuffer= (uint32_t) (my_b_tell(&buffpek_pointers)/sizeof(*buffpek_inst));
345
276
 
346
277
  if (maxbuffer == 0)                   // The whole set is in memory
347
278
  {
348
 
    if (param.save_index(sort_keys,(uint32_t) records, &table_sort))
349
 
    {
 
279
    if (save_index(&param,sort_keys,(uint32_t) records, &table_sort))
350
280
      goto err;
351
 
    }
352
281
  }
353
282
  else
354
283
  {
359
288
      table_sort.buffpek = 0;
360
289
    }
361
290
    if (!(table_sort.buffpek=
362
 
          (unsigned char *) read_buffpek_from_file(&buffpek_pointers, maxbuffer, table_sort.buffpek)))
363
 
    {
 
291
          (unsigned char *) read_buffpek_from_file(&buffpek_pointers, maxbuffer,
 
292
                                 table_sort.buffpek)))
364
293
      goto err;
365
 
    }
366
294
    buffpek_inst= (buffpek *) table_sort.buffpek;
367
295
    table_sort.buffpek_len= maxbuffer;
368
 
    buffpek_pointers.close_cached_file();
 
296
    close_cached_file(&buffpek_pointers);
369
297
        /* Open cached file if it isn't open */
370
 
    if (! my_b_inited(outfile) && outfile->open_cached_file(drizzle_tmpdir.c_str(),TEMP_PREFIX,READ_RECORD_BUFFER, MYF(MY_WME)))
371
 
    {
372
 
      goto err;
373
 
    }
374
 
 
375
 
    if (outfile->reinit_io_cache(internal::WRITE_CACHE,0L,0,0))
376
 
    {
377
 
      goto err;
378
 
    }
 
298
    if (! my_b_inited(outfile) &&
 
299
        open_cached_file(outfile,drizzle_tmpdir.c_str(),TEMP_PREFIX,READ_RECORD_BUFFER,
 
300
                          MYF(MY_WME)))
 
301
      goto err;
 
302
    if (reinit_io_cache(outfile,internal::WRITE_CACHE,0L,0,0))
 
303
      goto err;
379
304
 
380
305
    /*
381
306
      Use also the space previously used by string pointers in sort_buffer
382
307
      for temporary key storage.
383
308
    */
384
 
    param.keys=((param.keys*(param.rec_length+sizeof(char*))) / param.rec_length-1);
385
 
 
 
309
    param.keys=((param.keys*(param.rec_length+sizeof(char*))) /
 
310
                param.rec_length-1);
386
311
    maxbuffer--;                                // Offset from 0
387
312
    if (merge_many_buff(&param,(unsigned char*) sort_keys,buffpek_inst,&maxbuffer, &tempfile))
388
 
    {
389
 
      goto err;
390
 
    }
391
 
 
392
 
    if (flush_io_cache(&tempfile) || tempfile.reinit_io_cache(internal::READ_CACHE,0L,0,0))
393
 
    {
394
 
      goto err;
395
 
    }
396
 
 
 
313
      goto err;
 
314
    if (flush_io_cache(&tempfile) ||
 
315
        reinit_io_cache(&tempfile,internal::READ_CACHE,0L,0,0))
 
316
      goto err;
397
317
    if (merge_index(&param,(unsigned char*) sort_keys,buffpek_inst,maxbuffer,&tempfile, outfile))
398
 
    {
399
318
      goto err;
400
 
    }
401
319
  }
402
 
 
403
320
  if (records > param.max_rows)
404
 
  {
405
 
    records= param.max_rows;
406
 
  }
 
321
    records=param.max_rows;
407
322
  error =0;
408
323
 
409
324
 err:
410
 
  if (not subselect || not subselect->is_uncacheable())
 
325
  if (param.tmp_buffer)
 
326
    if (param.tmp_buffer)
 
327
      free(param.tmp_buffer);
 
328
  if (!subselect || !subselect->is_uncacheable())
411
329
  {
412
330
    free(sort_keys);
413
331
    table_sort.sort_keys= 0;
415
333
    table_sort.buffpek= 0;
416
334
    table_sort.buffpek_len= 0;
417
335
  }
418
 
 
419
 
  tempfile.close_cached_file();
420
 
  buffpek_pointers.close_cached_file();
421
 
 
 
336
  close_cached_file(&tempfile);
 
337
  close_cached_file(&buffpek_pointers);
422
338
  if (my_b_inited(outfile))
423
339
  {
424
340
    if (flush_io_cache(outfile))
425
 
    {
426
341
      error=1;
427
 
    }
428
342
    {
429
 
      internal::my_off_t save_pos= outfile->pos_in_file;
 
343
      internal::my_off_t save_pos=outfile->pos_in_file;
430
344
      /* For following reads */
431
 
      if (outfile->reinit_io_cache(internal::READ_CACHE,0L,0,0))
432
 
      {
 
345
      if (reinit_io_cache(outfile,internal::READ_CACHE,0L,0,0))
433
346
        error=1;
434
 
      }
435
347
      outfile->end_of_file=save_pos;
436
348
    }
437
349
  }
438
 
 
439
350
  if (error)
440
351
  {
441
352
    my_message(ER_FILSORT_ABORT, ER(ER_FILSORT_ABORT),
443
354
  }
444
355
  else
445
356
  {
446
 
    getSession().status_var.filesort_rows+= (uint32_t) records;
 
357
    session->status_var.filesort_rows+= (uint32_t) records;
447
358
  }
448
 
  examined_rows= param.examined_rows;
449
 
  global_sort_buffer.sub(allocated_sort_memory);
450
 
  table->sort= table_sort;
 
359
  *examined_rows= param.examined_rows;
 
360
  memcpy(&table->sort, &table_sort, sizeof(filesort_info));
451
361
  DRIZZLE_FILESORT_DONE(error, records);
452
362
  return (error ? HA_POS_ERROR : records);
453
363
} /* filesort */
454
364
 
 
365
 
 
366
void Table::filesort_free_buffers(bool full)
 
367
{
 
368
  if (sort.record_pointers)
 
369
  {
 
370
    free((unsigned char*) sort.record_pointers);
 
371
    sort.record_pointers=0;
 
372
  }
 
373
  if (full)
 
374
  {
 
375
    if (sort.sort_keys )
 
376
    {
 
377
      if ((unsigned char*) sort.sort_keys)
 
378
        free((unsigned char*) sort.sort_keys);
 
379
      sort.sort_keys= 0;
 
380
    }
 
381
    if (sort.buffpek)
 
382
    {
 
383
      if ((unsigned char*) sort.buffpek)
 
384
        free((unsigned char*) sort.buffpek);
 
385
      sort.buffpek= 0;
 
386
      sort.buffpek_len= 0;
 
387
    }
 
388
  }
 
389
  if (sort.addon_buf)
 
390
  {
 
391
    free((char *) sort.addon_buf);
 
392
    free((char *) sort.addon_field);
 
393
    sort.addon_buf=0;
 
394
    sort.addon_field=0;
 
395
  }
 
396
}
 
397
 
455
398
/** Make a array of string pointers. */
456
399
 
457
400
static char **make_char_array(char **old_pos, register uint32_t fields,
484
427
    tmp= (unsigned char *)malloc(length);
485
428
  if (tmp)
486
429
  {
487
 
    if (buffpek_pointers->reinit_io_cache(internal::READ_CACHE,0L,0,0) ||
 
430
    if (reinit_io_cache(buffpek_pointers,internal::READ_CACHE,0L,0,0) ||
488
431
        my_b_read(buffpek_pointers, (unsigned char*) tmp, length))
489
432
    {
490
433
      free((char*) tmp);
532
475
    HA_POS_ERROR on error.
533
476
*/
534
477
 
535
 
ha_rows FileSort::find_all_keys(SortParam *param, 
536
 
                                optimizer::SqlSelect *select,
537
 
                                unsigned char **sort_keys,
538
 
                                internal::IO_CACHE *buffpek_pointers,
539
 
                                internal::IO_CACHE *tempfile, internal::IO_CACHE *indexfile)
 
478
static ha_rows find_all_keys(Session *session,
 
479
                             SORTPARAM *param, 
 
480
                             optimizer::SqlSelect *select,
 
481
                             unsigned char **sort_keys,
 
482
                             internal::IO_CACHE *buffpek_pointers,
 
483
                             internal::IO_CACHE *tempfile, internal::IO_CACHE *indexfile)
540
484
{
541
485
  int error,flag,quick_select;
542
486
  uint32_t idx,indexpos,ref_length;
543
487
  unsigned char *ref_pos,*next_pos,ref_buff[MAX_REFLENGTH];
544
488
  internal::my_off_t record;
545
489
  Table *sort_form;
546
 
  volatile Session::killed_state_t *killed= getSession().getKilledPtr();
 
490
  volatile Session::killed_state *killed= &session->killed;
547
491
  Cursor *file;
548
492
  boost::dynamic_bitset<> *save_read_set= NULL;
549
493
  boost::dynamic_bitset<> *save_write_set= NULL;
564
508
  if (! indexfile && ! quick_select)
565
509
  {
566
510
    next_pos=(unsigned char*) 0;                        /* Find records in sequence */
567
 
    if (file->startTableScan(1))
568
 
      return(HA_POS_ERROR);
569
 
    file->extra_opt(HA_EXTRA_CACHE, getSession().variables.read_buff_size);
 
511
    file->startTableScan(1);
 
512
    file->extra_opt(HA_EXTRA_CACHE,
 
513
                    session->variables.read_buff_size);
570
514
  }
571
515
 
572
516
  ReadRecord read_record_info;
575
519
    if (select->quick->reset())
576
520
      return(HA_POS_ERROR);
577
521
 
578
 
    if (read_record_info.init_read_record(&getSession(), select->quick->head, select, 1, 1))
579
 
      return(HA_POS_ERROR);
 
522
    read_record_info.init_read_record(session, select->quick->head, select, 1, 1);
580
523
  }
581
524
 
582
525
  /* Remember original bitmaps */
586
529
  sort_form->tmp_set.reset();
587
530
  /* Temporary set for register_used_fields and register_field_in_read_map */
588
531
  sort_form->read_set= &sort_form->tmp_set;
589
 
  param->register_used_fields();
 
532
  register_used_fields(param);
590
533
  if (select && select->cond)
591
534
    select->cond->walk(&Item::register_field_in_read_map, 1,
592
535
                       (unsigned char*) sort_form);
645
588
    {
646
589
      if (idx == param->keys)
647
590
      {
648
 
        if (param->write_keys(sort_keys, idx, buffpek_pointers, tempfile))
 
591
        if (write_keys(param,sort_keys,idx,buffpek_pointers,tempfile))
649
592
          return(HA_POS_ERROR);
650
593
        idx=0;
651
594
        indexpos++;
652
595
      }
653
 
      param->make_sortkey(sort_keys[idx++], ref_pos);
 
596
      make_sortkey(param,sort_keys[idx++],ref_pos);
654
597
    }
655
598
    else
656
 
    {
657
599
      file->unlock_row();
658
 
    }
659
 
 
660
600
    /* It does not make sense to read more keys in case of a fatal error */
661
 
    if (getSession().is_error())
 
601
    if (session->is_error())
662
602
      break;
663
603
  }
664
604
  if (quick_select)
676
616
      file->endTableScan();
677
617
  }
678
618
 
679
 
  if (getSession().is_error())
 
619
  if (session->is_error())
680
620
    return(HA_POS_ERROR);
681
621
 
682
622
  /* Signal we should use orignal column read and write maps */
687
627
    sort_form->print_error(error,MYF(ME_ERROR | ME_WAITTANG));
688
628
    return(HA_POS_ERROR);
689
629
  }
690
 
 
691
 
  if (indexpos && idx && param->write_keys(sort_keys,idx,buffpek_pointers,tempfile))
692
 
  {
 
630
  if (indexpos && idx &&
 
631
      write_keys(param,sort_keys,idx,buffpek_pointers,tempfile))
693
632
    return(HA_POS_ERROR);
694
 
  }
695
 
 
696
633
  return(my_b_inited(tempfile) ?
697
634
              (ha_rows) (my_b_tell(tempfile)/param->rec_length) :
698
635
              idx);
721
658
    1 Error
722
659
*/
723
660
 
724
 
int SortParam::write_keys(register unsigned char **sort_keys, uint32_t count,
725
 
                          internal::IO_CACHE *buffpek_pointers, internal::IO_CACHE *tempfile)
 
661
static int
 
662
write_keys(SORTPARAM *param, register unsigned char **sort_keys, uint32_t count,
 
663
           internal::IO_CACHE *buffpek_pointers, internal::IO_CACHE *tempfile)
726
664
{
 
665
  size_t sort_length, rec_length;
 
666
  unsigned char **end;
727
667
  buffpek buffpek;
728
668
 
 
669
  sort_length= param->sort_length;
 
670
  rec_length= param->rec_length;
729
671
  internal::my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, sort_length);
730
672
  if (!my_b_inited(tempfile) &&
731
 
      tempfile->open_cached_file(drizzle_tmpdir.c_str(), TEMP_PREFIX, DISK_BUFFER_SIZE, MYF(MY_WME)))
 
673
      open_cached_file(tempfile, drizzle_tmpdir.c_str(), TEMP_PREFIX, DISK_BUFFER_SIZE,
 
674
                       MYF(MY_WME)))
732
675
  {
733
676
    return 1;
734
677
  }
739
682
  }
740
683
 
741
684
  buffpek.file_pos= my_b_tell(tempfile);
742
 
  if ((ha_rows) count > max_rows)
743
 
    count=(uint32_t) max_rows;
744
 
 
 
685
  if ((ha_rows) count > param->max_rows)
 
686
    count=(uint32_t) param->max_rows;
745
687
  buffpek.count=(ha_rows) count;
746
 
 
747
 
  for (unsigned char **ptr= sort_keys + count ; sort_keys != ptr ; sort_keys++)
 
688
  for (end=sort_keys+count ; sort_keys != end ; sort_keys++)
748
689
  {
749
690
    if (my_b_write(tempfile, (unsigned char*) *sort_keys, (uint32_t) rec_length))
750
691
    {
786
727
 
787
728
/** Make a sort-key from record. */
788
729
 
789
 
void SortParam::make_sortkey(register unsigned char *to, unsigned char *ref_pos)
 
730
static void make_sortkey(register SORTPARAM *param,
 
731
                         register unsigned char *to, unsigned char *ref_pos)
790
732
{
791
733
  Field *field;
792
734
  SortField *sort_field;
793
735
  size_t length;
794
736
 
795
 
  for (sort_field= local_sortorder ;
796
 
       sort_field != end ;
 
737
  for (sort_field=param->local_sortorder ;
 
738
       sort_field != param->end ;
797
739
       sort_field++)
798
740
  {
799
741
    bool maybe_null=0;
819
761
    {                                           // Item
820
762
      Item *item=sort_field->item;
821
763
      maybe_null= item->maybe_null;
822
 
 
823
764
      switch (sort_field->result_type) {
824
765
      case STRING_RESULT:
825
 
        {
826
 
          const CHARSET_INFO * const cs=item->collation.collation;
827
 
          char fill_char= ((cs->state & MY_CS_BINSORT) ? (char) 0 : ' ');
828
 
          int diff;
829
 
          uint32_t sort_field_length;
 
766
      {
 
767
        const CHARSET_INFO * const cs=item->collation.collation;
 
768
        char fill_char= ((cs->state & MY_CS_BINSORT) ? (char) 0 : ' ');
 
769
        int diff;
 
770
        uint32_t sort_field_length;
830
771
 
 
772
        if (maybe_null)
 
773
          *to++=1;
 
774
        /* All item->str() to use some extra byte for end null.. */
 
775
        String tmp((char*) to,sort_field->length+4,cs);
 
776
        String *res= item->str_result(&tmp);
 
777
        if (!res)
 
778
        {
831
779
          if (maybe_null)
832
 
            *to++=1;
833
 
          /* All item->str() to use some extra byte for end null.. */
834
 
          String tmp((char*) to,sort_field->length+4,cs);
835
 
          String *res= item->str_result(&tmp);
836
 
          if (!res)
837
 
          {
838
 
            if (maybe_null)
839
 
              memset(to-1, 0, sort_field->length+1);
840
 
            else
841
 
            {
842
 
              /*
843
 
                This should only happen during extreme conditions if we run out
844
 
                of memory or have an item marked not null when it can be null.
845
 
                This code is here mainly to avoid a hard crash in this case.
846
 
              */
847
 
              assert(0);
848
 
              memset(to, 0, sort_field->length);        // Avoid crash
849
 
            }
850
 
            break;
851
 
          }
852
 
          length= res->length();
853
 
          sort_field_length= sort_field->length - sort_field->suffix_length;
854
 
          diff=(int) (sort_field_length - length);
855
 
          if (diff < 0)
856
 
          {
857
 
            diff=0;
858
 
            length= sort_field_length;
859
 
          }
860
 
          if (sort_field->suffix_length)
861
 
          {
862
 
            /* Store length last in result_string */
863
 
            store_length(to + sort_field_length, length,
864
 
                         sort_field->suffix_length);
865
 
          }
866
 
          if (sort_field->need_strxnfrm)
867
 
          {
868
 
            char *from=(char*) res->ptr();
869
 
            uint32_t tmp_length;
870
 
            if ((unsigned char*) from == to)
871
 
            {
872
 
              set_if_smaller(length,sort_field->length);
873
 
              memcpy(tmp_buffer,from,length);
874
 
              from= tmp_buffer;
875
 
            }
876
 
            tmp_length= my_strnxfrm(cs,to,sort_field->length,
877
 
                                    (unsigned char*) from, length);
878
 
            assert(tmp_length == sort_field->length);
879
 
          }
 
780
            memset(to-1, 0, sort_field->length+1);
880
781
          else
881
782
          {
882
 
            my_strnxfrm(cs,(unsigned char*)to,length,(const unsigned char*)res->ptr(),length);
883
 
            cs->cset->fill(cs, (char *)to+length,diff,fill_char);
 
783
            /*
 
784
              This should only happen during extreme conditions if we run out
 
785
              of memory or have an item marked not null when it can be null.
 
786
              This code is here mainly to avoid a hard crash in this case.
 
787
            */
 
788
            assert(0);
 
789
            memset(to, 0, sort_field->length);  // Avoid crash
884
790
          }
885
791
          break;
886
792
        }
 
793
        length= res->length();
 
794
        sort_field_length= sort_field->length - sort_field->suffix_length;
 
795
        diff=(int) (sort_field_length - length);
 
796
        if (diff < 0)
 
797
        {
 
798
          diff=0;
 
799
          length= sort_field_length;
 
800
        }
 
801
        if (sort_field->suffix_length)
 
802
        {
 
803
          /* Store length last in result_string */
 
804
          store_length(to + sort_field_length, length,
 
805
                       sort_field->suffix_length);
 
806
        }
 
807
        if (sort_field->need_strxnfrm)
 
808
        {
 
809
          char *from=(char*) res->ptr();
 
810
          uint32_t tmp_length;
 
811
          if ((unsigned char*) from == to)
 
812
          {
 
813
            set_if_smaller(length,sort_field->length);
 
814
            memcpy(param->tmp_buffer,from,length);
 
815
            from=param->tmp_buffer;
 
816
          }
 
817
          tmp_length= my_strnxfrm(cs,to,sort_field->length,
 
818
                                  (unsigned char*) from, length);
 
819
          assert(tmp_length == sort_field->length);
 
820
        }
 
821
        else
 
822
        {
 
823
          my_strnxfrm(cs,(unsigned char*)to,length,(const unsigned char*)res->ptr(),length);
 
824
          cs->cset->fill(cs, (char *)to+length,diff,fill_char);
 
825
        }
 
826
        break;
 
827
      }
887
828
      case INT_RESULT:
888
 
        {
 
829
        {
889
830
          int64_t value= item->val_int_result();
890
831
          if (maybe_null)
891
832
          {
892
 
            *to++=1;
 
833
            *to++=1;
893
834
            if (item->null_value)
894
835
            {
895
836
              if (maybe_null)
901
842
              break;
902
843
            }
903
844
          }
904
 
          to[7]= (unsigned char) value;
905
 
          to[6]= (unsigned char) (value >> 8);
906
 
          to[5]= (unsigned char) (value >> 16);
907
 
          to[4]= (unsigned char) (value >> 24);
908
 
          to[3]= (unsigned char) (value >> 32);
909
 
          to[2]= (unsigned char) (value >> 40);
910
 
          to[1]= (unsigned char) (value >> 48);
 
845
          to[7]= (unsigned char) value;
 
846
          to[6]= (unsigned char) (value >> 8);
 
847
          to[5]= (unsigned char) (value >> 16);
 
848
          to[4]= (unsigned char) (value >> 24);
 
849
          to[3]= (unsigned char) (value >> 32);
 
850
          to[2]= (unsigned char) (value >> 40);
 
851
          to[1]= (unsigned char) (value >> 48);
911
852
          if (item->unsigned_flag)                    /* Fix sign */
912
853
            to[0]= (unsigned char) (value >> 56);
913
854
          else
914
855
            to[0]= (unsigned char) (value >> 56) ^ 128; /* Reverse signbit */
915
 
          break;
916
 
        }
 
856
          break;
 
857
        }
917
858
      case DECIMAL_RESULT:
918
859
        {
919
 
          type::Decimal dec_buf, *dec_val= item->val_decimal_result(&dec_buf);
 
860
          my_decimal dec_buf, *dec_val= item->val_decimal_result(&dec_buf);
920
861
          if (maybe_null)
921
862
          {
922
863
            if (item->null_value)
927
868
            }
928
869
            *to++=1;
929
870
          }
930
 
          dec_val->val_binary(E_DEC_FATAL_ERROR, to,
931
 
                              item->max_length - (item->decimals ? 1:0),
932
 
                              item->decimals);
933
 
          break;
 
871
          my_decimal2binary(E_DEC_FATAL_ERROR, dec_val, to,
 
872
                            item->max_length - (item->decimals ? 1:0),
 
873
                            item->decimals);
 
874
         break;
934
875
        }
935
876
      case REAL_RESULT:
936
 
        {
 
877
        {
937
878
          double value= item->val_result();
938
 
          if (maybe_null)
 
879
          if (maybe_null)
939
880
          {
940
881
            if (item->null_value)
941
882
            {
943
884
              to++;
944
885
              break;
945
886
            }
946
 
            *to++=1;
 
887
            *to++=1;
947
888
          }
948
 
          change_double_for_sort(value,(unsigned char*) to);
949
 
          break;
950
 
        }
 
889
          change_double_for_sort(value,(unsigned char*) to);
 
890
          break;
 
891
        }
951
892
      case ROW_RESULT:
952
893
      default:
953
 
        // This case should never be choosen
954
 
        assert(0);
955
 
        break;
 
894
        // This case should never be choosen
 
895
        assert(0);
 
896
        break;
956
897
      }
957
898
    }
958
 
 
959
899
    if (sort_field->reverse)
960
900
    {                                                   /* Revers key */
961
901
      if (maybe_null)
968
908
      }
969
909
    }
970
910
    else
971
 
    {
972
911
      to+= sort_field->length;
973
 
    }
974
912
  }
975
913
 
976
 
  if (addon_field)
 
914
  if (param->addon_field)
977
915
  {
978
916
    /*
979
917
      Save field values appended to sorted fields.
981
919
      In this implementation we use fixed layout for field values -
982
920
      the same for all records.
983
921
    */
984
 
    sort_addon_field *addonf= addon_field;
 
922
    sort_addon_field *addonf= param->addon_field;
985
923
    unsigned char *nulls= to;
986
924
    assert(addonf != 0);
987
925
    memset(nulls, 0, addonf->offset);
1013
951
  else
1014
952
  {
1015
953
    /* Save filepos last */
1016
 
    memcpy(to, ref_pos, (size_t) ref_length);
 
954
    memcpy(to, ref_pos, (size_t) param->ref_length);
1017
955
  }
 
956
  return;
1018
957
}
1019
958
 
1020
959
 
1022
961
  Register fields used by sorting in the sorted table's read set
1023
962
*/
1024
963
 
1025
 
void SortParam::register_used_fields()
 
964
static void register_used_fields(SORTPARAM *param)
1026
965
{
1027
966
  SortField *sort_field;
1028
 
  Table *table= sort_form;
 
967
  Table *table=param->sort_form;
1029
968
 
1030
 
  for (sort_field= local_sortorder ;
1031
 
       sort_field != end ;
 
969
  for (sort_field= param->local_sortorder ;
 
970
       sort_field != param->end ;
1032
971
       sort_field++)
1033
972
  {
1034
973
    Field *field;
1035
974
    if ((field= sort_field->field))
1036
975
    {
1037
976
      if (field->getTable() == table)
1038
 
        table->setReadSet(field->position());
 
977
        table->setReadSet(field->field_index);
1039
978
    }
1040
979
    else
1041
980
    {                                           // Item
1044
983
    }
1045
984
  }
1046
985
 
1047
 
  if (addon_field)
 
986
  if (param->addon_field)
1048
987
  {
1049
 
    sort_addon_field *addonf= addon_field;
 
988
    sort_addon_field *addonf= param->addon_field;
1050
989
    Field *field;
1051
990
    for ( ; (field= addonf->field) ; addonf++)
1052
 
      table->setReadSet(field->position());
 
991
      table->setReadSet(field->field_index);
1053
992
  }
1054
993
  else
1055
994
  {
1059
998
}
1060
999
 
1061
1000
 
1062
 
bool SortParam::save_index(unsigned char **sort_keys, uint32_t count, filesort_info *table_sort)
 
1001
static bool save_index(SORTPARAM *param, unsigned char **sort_keys, uint32_t count,
 
1002
                       filesort_info *table_sort)
1063
1003
{
1064
 
  uint32_t offset;
 
1004
  uint32_t offset,res_length;
1065
1005
  unsigned char *to;
1066
1006
 
1067
 
  internal::my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, sort_length);
1068
 
  offset= rec_length - res_length;
1069
 
 
1070
 
  if ((ha_rows) count > max_rows)
1071
 
    count=(uint32_t) max_rows;
1072
 
 
1073
 
  if (!(to= table_sort->record_pointers= (unsigned char*) malloc(res_length*count)))
1074
 
    return true;
1075
 
 
1076
 
  for (unsigned char **end_ptr= sort_keys+count ; sort_keys != end_ptr ; sort_keys++)
 
1007
  internal::my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, param->sort_length);
 
1008
  res_length= param->res_length;
 
1009
  offset= param->rec_length-res_length;
 
1010
  if ((ha_rows) count > param->max_rows)
 
1011
    count=(uint32_t) param->max_rows;
 
1012
  if (!(to= table_sort->record_pointers=
 
1013
        (unsigned char*) malloc(res_length*count)))
 
1014
    return(1);
 
1015
  for (unsigned char **end= sort_keys+count ; sort_keys != end ; sort_keys++)
1077
1016
  {
1078
1017
    memcpy(to, *sort_keys+offset, res_length);
1079
1018
    to+= res_length;
1080
1019
  }
1081
 
 
1082
 
  return false;
 
1020
  return(0);
1083
1021
}
1084
1022
 
1085
1023
 
1086
1024
/** Merge buffers to make < MERGEBUFF2 buffers. */
1087
1025
 
1088
 
int FileSort::merge_many_buff(SortParam *param, unsigned char *sort_buffer,
1089
 
                              buffpek *buffpek_inst, uint32_t *maxbuffer, internal::IO_CACHE *t_file)
 
1026
int merge_many_buff(SORTPARAM *param, unsigned char *sort_buffer,
 
1027
                    buffpek *buffpek_inst, uint32_t *maxbuffer, internal::IO_CACHE *t_file)
1090
1028
{
1091
1029
  internal::IO_CACHE t_file2,*from_file,*to_file,*temp;
1092
1030
  buffpek *lastbuff;
1094
1032
  if (*maxbuffer < MERGEBUFF2)
1095
1033
    return 0;
1096
1034
  if (flush_io_cache(t_file) ||
1097
 
      t_file2.open_cached_file(drizzle_tmpdir.c_str(),TEMP_PREFIX,DISK_BUFFER_SIZE, MYF(MY_WME)))
 
1035
      open_cached_file(&t_file2,drizzle_tmpdir.c_str(),TEMP_PREFIX,DISK_BUFFER_SIZE, MYF(MY_WME)))
1098
1036
  {
1099
1037
    return 1;
1100
1038
  }
1104
1042
  {
1105
1043
    register uint32_t i;
1106
1044
 
1107
 
    if (from_file->reinit_io_cache(internal::READ_CACHE,0L,0,0))
 
1045
    if (reinit_io_cache(from_file,internal::READ_CACHE,0L,0,0))
1108
1046
    {
1109
1047
      break;
1110
1048
    }
1111
1049
 
1112
 
    if (to_file->reinit_io_cache(internal::WRITE_CACHE,0L,0,0))
 
1050
    if (reinit_io_cache(to_file,internal::WRITE_CACHE,0L,0,0))
1113
1051
    {
1114
1052
      break;
1115
1053
    }
1136
1074
    }
1137
1075
 
1138
1076
    temp=from_file; from_file=to_file; to_file=temp;
1139
 
    from_file->setup_io_cache();
1140
 
    to_file->setup_io_cache();
 
1077
    setup_io_cache(from_file);
 
1078
    setup_io_cache(to_file);
1141
1079
    *maxbuffer= (uint32_t) (lastbuff-buffpek_inst)-1;
1142
1080
  }
1143
1081
 
1144
1082
cleanup:
1145
 
  to_file->close_cached_file();                 // This holds old result
 
1083
  close_cached_file(to_file);                   // This holds old result
1146
1084
  if (to_file == t_file)
1147
1085
  {
1148
1086
    *t_file=t_file2;                            // Copy result file
1149
 
    t_file->setup_io_cache();
 
1087
    setup_io_cache(t_file);
1150
1088
  }
1151
1089
 
1152
1090
  return(*maxbuffer >= MERGEBUFF2);     /* Return 1 if interrupted */
1160
1098
    (uint32_t)-1 if something goes wrong
1161
1099
*/
1162
1100
 
1163
 
uint32_t FileSort::read_to_buffer(internal::IO_CACHE *fromfile, buffpek *buffpek_inst, uint32_t rec_length)
 
1101
uint32_t read_to_buffer(internal::IO_CACHE *fromfile, buffpek *buffpek_inst,
 
1102
                        uint32_t rec_length)
1164
1103
{
1165
1104
  register uint32_t count;
1166
1105
  uint32_t length;
1183
1122
{
1184
1123
  qsort2_cmp key_compare;
1185
1124
  void *key_compare_arg;
1186
 
 
1187
1125
  public:
1188
 
  compare_functor(qsort2_cmp in_key_compare, void *in_compare_arg) :
1189
 
    key_compare(in_key_compare),
1190
 
    key_compare_arg(in_compare_arg)
1191
 
  { }
1192
 
  
 
1126
  compare_functor(qsort2_cmp in_key_compare, void *in_compare_arg)
 
1127
    : key_compare(in_key_compare), key_compare_arg(in_compare_arg) { }
1193
1128
  inline bool operator()(const buffpek *i, const buffpek *j) const
1194
1129
  {
1195
 
    int val= key_compare(key_compare_arg, &i->key, &j->key);
1196
 
 
 
1130
    int val= key_compare(key_compare_arg,
 
1131
                      &i->key, &j->key);
1197
1132
    return (val >= 0);
1198
1133
  }
1199
1134
};
1217
1152
    other  error
1218
1153
*/
1219
1154
 
1220
 
int FileSort::merge_buffers(SortParam *param, internal::IO_CACHE *from_file,
1221
 
                            internal::IO_CACHE *to_file, unsigned char *sort_buffer,
1222
 
                            buffpek *lastbuff, buffpek *Fb, buffpek *Tb,
1223
 
                            int flag)
 
1155
int merge_buffers(SORTPARAM *param, internal::IO_CACHE *from_file,
 
1156
                  internal::IO_CACHE *to_file, unsigned char *sort_buffer,
 
1157
                  buffpek *lastbuff, buffpek *Fb, buffpek *Tb,
 
1158
                  int flag)
1224
1159
{
1225
1160
  int error;
1226
1161
  uint32_t rec_length,res_length,offset;
1232
1167
  buffpek *buffpek_inst;
1233
1168
  qsort2_cmp cmp;
1234
1169
  void *first_cmp_arg;
1235
 
  volatile Session::killed_state_t *killed= getSession().getKilledPtr();
1236
 
  Session::killed_state_t not_killable;
 
1170
  volatile Session::killed_state *killed= &current_session->killed;
 
1171
  Session::killed_state not_killable;
1237
1172
 
1238
 
  getSession().status_var.filesort_merge_passes++;
 
1173
  current_session->status_var.filesort_merge_passes++;
1239
1174
  if (param->not_killable)
1240
1175
  {
1241
1176
    killed= &not_killable;
1430
1365
 
1431
1366
        /* Do a merge to output-file (save only positions) */
1432
1367
 
1433
 
int FileSort::merge_index(SortParam *param, unsigned char *sort_buffer,
1434
 
                          buffpek *buffpek_inst, uint32_t maxbuffer,
1435
 
                          internal::IO_CACHE *tempfile, internal::IO_CACHE *outfile)
 
1368
static int merge_index(SORTPARAM *param, unsigned char *sort_buffer,
 
1369
                       buffpek *buffpek_inst, uint32_t maxbuffer,
 
1370
                       internal::IO_CACHE *tempfile, internal::IO_CACHE *outfile)
1436
1371
{
1437
1372
  if (merge_buffers(param,tempfile,outfile,sort_buffer,buffpek_inst,buffpek_inst,
1438
1373
                    buffpek_inst+maxbuffer,1))
1439
 
    return 1;
1440
 
 
1441
 
  return 0;
 
1374
    return(1);
 
1375
  return(0);
1442
1376
} /* merge_index */
1443
1377
 
1444
1378
 
1458
1392
/**
1459
1393
  Calculate length of sort key.
1460
1394
 
 
1395
  @param session                          Thread Cursor
1461
1396
  @param sortorder                Order of items to sort
1462
1397
  @param s_length                 Number of items to sort
1463
1398
  @param[out] multi_byte_charset Set to 1 if we are using multi-byte charset
1472
1407
    Total length of sort buffer in bytes
1473
1408
*/
1474
1409
 
1475
 
uint32_t FileSort::sortlength(SortField *sortorder, uint32_t s_length, bool *multi_byte_charset)
 
1410
static uint32_t
 
1411
sortlength(Session *session, SortField *sortorder, uint32_t s_length,
 
1412
           bool *multi_byte_charset)
1476
1413
{
1477
1414
  register uint32_t length;
1478
1415
  const CHARSET_INFO *cs;
1502
1439
      sortorder->result_type= sortorder->item->result_type();
1503
1440
      if (sortorder->item->result_as_int64_t())
1504
1441
        sortorder->result_type= INT_RESULT;
1505
 
 
1506
1442
      switch (sortorder->result_type) {
1507
1443
      case STRING_RESULT:
1508
 
        sortorder->length=sortorder->item->max_length;
 
1444
        sortorder->length=sortorder->item->max_length;
1509
1445
        set_if_smaller(sortorder->length,
1510
 
                       getSession().variables.max_sort_length);
1511
 
        if (use_strnxfrm((cs=sortorder->item->collation.collation)))
1512
 
        {
 
1446
                       session->variables.max_sort_length);
 
1447
        if (use_strnxfrm((cs=sortorder->item->collation.collation)))
 
1448
        {
1513
1449
          sortorder->length= cs->coll->strnxfrmlen(cs, sortorder->length);
1514
 
          sortorder->need_strxnfrm= 1;
1515
 
          *multi_byte_charset= 1;
1516
 
        }
 
1450
          sortorder->need_strxnfrm= 1;
 
1451
          *multi_byte_charset= 1;
 
1452
        }
1517
1453
        else if (cs == &my_charset_bin)
1518
1454
        {
1519
1455
          /* Store length last to be able to sort blob/varbinary */
1520
1456
          sortorder->suffix_length= suffix_length(sortorder->length);
1521
1457
          sortorder->length+= sortorder->suffix_length;
1522
1458
        }
1523
 
        break;
 
1459
        break;
1524
1460
      case INT_RESULT:
1525
 
        sortorder->length=8;                    // Size of intern int64_t
1526
 
        break;
 
1461
        sortorder->length=8;                    // Size of intern int64_t
 
1462
        break;
1527
1463
      case DECIMAL_RESULT:
1528
1464
        sortorder->length=
1529
 
          class_decimal_get_binary_size(sortorder->item->max_length -
 
1465
          my_decimal_get_binary_size(sortorder->item->max_length -
1530
1466
                                     (sortorder->item->decimals ? 1 : 0),
1531
1467
                                     sortorder->item->decimals);
1532
1468
        break;
1533
1469
      case REAL_RESULT:
1534
 
        sortorder->length=sizeof(double);
1535
 
        break;
 
1470
        sortorder->length=sizeof(double);
 
1471
        break;
1536
1472
      case ROW_RESULT:
1537
 
        // This case should never be choosen
1538
 
        assert(0);
1539
 
        break;
 
1473
      default:
 
1474
        // This case should never be choosen
 
1475
        assert(0);
 
1476
        break;
1540
1477
      }
1541
1478
      if (sortorder->item->maybe_null)
1542
 
        length++;                               // Place for NULL marker
 
1479
        length++;                               // Place for NULL marker
1543
1480
    }
1544
 
    set_if_smaller(sortorder->length, (size_t)getSession().variables.max_sort_length);
 
1481
    set_if_smaller(sortorder->length,
 
1482
                   (size_t)session->variables.max_sort_length);
1545
1483
    length+=sortorder->length;
1546
1484
  }
1547
1485
  sortorder->field= (Field*) 0;                 // end marker
1561
1499
  layouts for the values of the non-sorted fields in the buffer and
1562
1500
  fills them.
1563
1501
 
 
1502
  @param session                 Current thread
1564
1503
  @param ptabfield           Array of references to the table fields
1565
1504
  @param sortlength          Total length of sorted fields
1566
1505
  @param[out] plength        Total length of appended fields
1575
1514
    NULL   if we do not store field values with sort data.
1576
1515
*/
1577
1516
 
1578
 
sort_addon_field *FileSort::get_addon_fields(Field **ptabfield, uint32_t sortlength_arg, uint32_t *plength)
 
1517
static sort_addon_field *
 
1518
get_addon_fields(Session *session, Field **ptabfield, uint32_t sortlength, uint32_t *plength)
1579
1519
{
1580
1520
  Field **pfield;
1581
1521
  Field *field;
1610
1550
    return 0;
1611
1551
  length+= (null_fields+7)/8;
1612
1552
 
1613
 
  if (length+sortlength_arg > getSession().variables.max_length_for_sort_data ||
 
1553
  if (length+sortlength > session->variables.max_length_for_sort_data ||
1614
1554
      !(addonf= (sort_addon_field *) malloc(sizeof(sort_addon_field)*
1615
1555
                                            (fields+1))))
1616
1556
    return 0;