30
30
#include <algorithm>
31
31
#include <iostream>
33
#include <drizzled/drizzled.h>
34
#include <drizzled/sql_sort.h>
35
#include <drizzled/filesort.h>
36
#include <drizzled/error.h>
37
#include <drizzled/probes.h>
38
#include <drizzled/session.h>
39
#include <drizzled/table.h>
40
#include <drizzled/table_list.h>
41
#include <drizzled/optimizer/range.h>
42
#include <drizzled/records.h>
43
#include <drizzled/internal/iocache.h>
44
#include <drizzled/internal/my_sys.h>
45
#include <plugin/myisam/myisam.h>
46
#include <drizzled/plugin/transactional_storage_engine.h>
47
#include <drizzled/atomics.h>
48
#include <drizzled/global_buffer.h>
50
#include <drizzled/sort_field.h>
33
#include "drizzled/drizzled.h"
34
#include "drizzled/sql_sort.h"
35
#include "drizzled/filesort.h"
36
#include "drizzled/error.h"
37
#include "drizzled/probes.h"
38
#include "drizzled/session.h"
39
#include "drizzled/table.h"
40
#include "drizzled/table_list.h"
41
#include "drizzled/optimizer/range.h"
42
#include "drizzled/records.h"
43
#include "drizzled/internal/iocache.h"
44
#include "drizzled/internal/my_sys.h"
45
#include "plugin/myisam/myisam.h"
46
#include "drizzled/plugin/transactional_storage_engine.h"
47
#include "drizzled/atomics.h"
48
#include "drizzled/global_buffer.h"
53
51
using namespace std;
822
818
Item *item=sort_field->item;
823
819
maybe_null= item->maybe_null;
825
820
switch (sort_field->result_type) {
826
821
case STRING_RESULT:
828
const CHARSET_INFO * const cs=item->collation.collation;
829
char fill_char= ((cs->state & MY_CS_BINSORT) ? (char) 0 : ' ');
831
uint32_t sort_field_length;
823
const CHARSET_INFO * const cs=item->collation.collation;
824
char fill_char= ((cs->state & MY_CS_BINSORT) ? (char) 0 : ' ');
826
uint32_t sort_field_length;
830
/* All item->str() to use some extra byte for end null.. */
831
String tmp((char*) to,sort_field->length+4,cs);
832
String *res= item->str_result(&tmp);
835
/* All item->str() to use some extra byte for end null.. */
836
String tmp((char*) to,sort_field->length+4,cs);
837
String *res= item->str_result(&tmp);
841
memset(to-1, 0, sort_field->length+1);
845
This should only happen during extreme conditions if we run out
846
of memory or have an item marked not null when it can be null.
847
This code is here mainly to avoid a hard crash in this case.
850
memset(to, 0, sort_field->length); // Avoid crash
854
length= res->length();
855
sort_field_length= sort_field->length - sort_field->suffix_length;
856
diff=(int) (sort_field_length - length);
860
length= sort_field_length;
862
if (sort_field->suffix_length)
864
/* Store length last in result_string */
865
store_length(to + sort_field_length, length,
866
sort_field->suffix_length);
868
if (sort_field->need_strxnfrm)
870
char *from=(char*) res->ptr();
872
if ((unsigned char*) from == to)
874
set_if_smaller(length,sort_field->length);
875
memcpy(tmp_buffer,from,length);
878
tmp_length= my_strnxfrm(cs,to,sort_field->length,
879
(unsigned char*) from, length);
880
assert(tmp_length == sort_field->length);
836
memset(to-1, 0, sort_field->length+1);
884
my_strnxfrm(cs,(unsigned char*)to,length,(const unsigned char*)res->ptr(),length);
885
cs->cset->fill(cs, (char *)to+length,diff,fill_char);
840
This should only happen during extreme conditions if we run out
841
of memory or have an item marked not null when it can be null.
842
This code is here mainly to avoid a hard crash in this case.
845
memset(to, 0, sort_field->length); // Avoid crash
849
length= res->length();
850
sort_field_length= sort_field->length - sort_field->suffix_length;
851
diff=(int) (sort_field_length - length);
855
length= sort_field_length;
857
if (sort_field->suffix_length)
859
/* Store length last in result_string */
860
store_length(to + sort_field_length, length,
861
sort_field->suffix_length);
863
if (sort_field->need_strxnfrm)
865
char *from=(char*) res->ptr();
867
if ((unsigned char*) from == to)
869
set_if_smaller(length,sort_field->length);
870
memcpy(tmp_buffer,from,length);
873
tmp_length= my_strnxfrm(cs,to,sort_field->length,
874
(unsigned char*) from, length);
875
assert(tmp_length == sort_field->length);
879
my_strnxfrm(cs,(unsigned char*)to,length,(const unsigned char*)res->ptr(),length);
880
cs->cset->fill(cs, (char *)to+length,diff,fill_char);
891
886
int64_t value= item->val_int_result();
895
890
if (item->null_value)
906
to[7]= (unsigned char) value;
907
to[6]= (unsigned char) (value >> 8);
908
to[5]= (unsigned char) (value >> 16);
909
to[4]= (unsigned char) (value >> 24);
910
to[3]= (unsigned char) (value >> 32);
911
to[2]= (unsigned char) (value >> 40);
912
to[1]= (unsigned char) (value >> 48);
901
to[7]= (unsigned char) value;
902
to[6]= (unsigned char) (value >> 8);
903
to[5]= (unsigned char) (value >> 16);
904
to[4]= (unsigned char) (value >> 24);
905
to[3]= (unsigned char) (value >> 32);
906
to[2]= (unsigned char) (value >> 40);
907
to[1]= (unsigned char) (value >> 48);
913
908
if (item->unsigned_flag) /* Fix sign */
914
909
to[0]= (unsigned char) (value >> 56);
916
911
to[0]= (unsigned char) (value >> 56) ^ 128; /* Reverse signbit */
919
914
case DECIMAL_RESULT:
921
type::Decimal dec_buf, *dec_val= item->val_decimal_result(&dec_buf);
916
my_decimal dec_buf, *dec_val= item->val_decimal_result(&dec_buf);
924
919
if (item->null_value)
1504
1496
sortorder->result_type= sortorder->item->result_type();
1505
1497
if (sortorder->item->result_as_int64_t())
1506
1498
sortorder->result_type= INT_RESULT;
1508
1499
switch (sortorder->result_type) {
1509
1500
case STRING_RESULT:
1510
sortorder->length=sortorder->item->max_length;
1501
sortorder->length=sortorder->item->max_length;
1511
1502
set_if_smaller(sortorder->length,
1512
1503
getSession().variables.max_sort_length);
1513
if (use_strnxfrm((cs=sortorder->item->collation.collation)))
1504
if (use_strnxfrm((cs=sortorder->item->collation.collation)))
1515
1506
sortorder->length= cs->coll->strnxfrmlen(cs, sortorder->length);
1516
sortorder->need_strxnfrm= 1;
1517
*multi_byte_charset= 1;
1507
sortorder->need_strxnfrm= 1;
1508
*multi_byte_charset= 1;
1519
1510
else if (cs == &my_charset_bin)
1521
1512
/* Store length last to be able to sort blob/varbinary */
1522
1513
sortorder->suffix_length= suffix_length(sortorder->length);
1523
1514
sortorder->length+= sortorder->suffix_length;
1526
1517
case INT_RESULT:
1527
sortorder->length=8; // Size of intern int64_t
1518
sortorder->length=8; // Size of intern int64_t
1529
1520
case DECIMAL_RESULT:
1530
1521
sortorder->length=
1531
class_decimal_get_binary_size(sortorder->item->max_length -
1522
my_decimal_get_binary_size(sortorder->item->max_length -
1532
1523
(sortorder->item->decimals ? 1 : 0),
1533
1524
sortorder->item->decimals);
1535
1526
case REAL_RESULT:
1536
sortorder->length=sizeof(double);
1527
sortorder->length=sizeof(double);
1538
1529
case ROW_RESULT:
1539
// This case should never be choosen
1531
// This case should never be choosen
1543
1535
if (sortorder->item->maybe_null)
1544
length++; // Place for NULL marker
1536
length++; // Place for NULL marker
1546
1538
set_if_smaller(sortorder->length, (size_t)getSession().variables.max_sort_length);
1547
1539
length+=sortorder->length;