813
818
Item *item=sort_field->item;
814
819
maybe_null= item->maybe_null;
815
821
switch (sort_field->result_type) {
816
822
case STRING_RESULT:
818
const CHARSET_INFO * const cs=item->collation.collation;
819
char fill_char= ((cs->state & MY_CS_BINSORT) ? (char) 0 : ' ');
821
uint32_t sort_field_length;
824
const CHARSET_INFO * const cs=item->collation.collation;
825
char fill_char= ((cs->state & MY_CS_BINSORT) ? (char) 0 : ' ');
827
uint32_t sort_field_length;
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);
831
memset(to-1, 0, sort_field->length+1);
831
/* All item->str() to use some extra byte for end null.. */
832
String tmp((char*) to,sort_field->length+4,cs);
833
String *res= item->str_result(&tmp);
837
memset(to-1, 0, sort_field->length+1);
841
This should only happen during extreme conditions if we run out
842
of memory or have an item marked not null when it can be null.
843
This code is here mainly to avoid a hard crash in this case.
846
memset(to, 0, sort_field->length); // Avoid crash
850
length= res->length();
851
sort_field_length= sort_field->length - sort_field->suffix_length;
852
diff=(int) (sort_field_length - length);
856
length= sort_field_length;
858
if (sort_field->suffix_length)
860
/* Store length last in result_string */
861
store_length(to + sort_field_length, length,
862
sort_field->suffix_length);
864
if (sort_field->need_strxnfrm)
866
char *from=(char*) res->ptr();
868
if ((unsigned char*) from == to)
870
set_if_smaller(length,sort_field->length);
871
memcpy(tmp_buffer,from,length);
874
tmp_length= my_strnxfrm(cs,to,sort_field->length,
875
(unsigned char*) from, length);
876
assert(tmp_length == sort_field->length);
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.
840
memset(to, 0, sort_field->length); // Avoid crash
880
my_strnxfrm(cs,(unsigned char*)to,length,(const unsigned char*)res->ptr(),length);
881
cs->cset->fill(cs, (char *)to+length,diff,fill_char);
844
length= res->length();
845
sort_field_length= sort_field->length - sort_field->suffix_length;
846
diff=(int) (sort_field_length - length);
850
length= sort_field_length;
852
if (sort_field->suffix_length)
854
/* Store length last in result_string */
855
store_length(to + sort_field_length, length,
856
sort_field->suffix_length);
858
if (sort_field->need_strxnfrm)
860
char *from=(char*) res->ptr();
862
if ((unsigned char*) from == to)
864
set_if_smaller(length,sort_field->length);
865
memcpy(tmp_buffer,from,length);
868
tmp_length= my_strnxfrm(cs,to,sort_field->length,
869
(unsigned char*) from, length);
870
assert(tmp_length == sort_field->length);
874
my_strnxfrm(cs,(unsigned char*)to,length,(const unsigned char*)res->ptr(),length);
875
cs->cset->fill(cs, (char *)to+length,diff,fill_char);
881
887
int64_t value= item->val_int_result();
885
891
if (item->null_value)
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);
902
to[7]= (unsigned char) value;
903
to[6]= (unsigned char) (value >> 8);
904
to[5]= (unsigned char) (value >> 16);
905
to[4]= (unsigned char) (value >> 24);
906
to[3]= (unsigned char) (value >> 32);
907
to[2]= (unsigned char) (value >> 40);
908
to[1]= (unsigned char) (value >> 48);
903
909
if (item->unsigned_flag) /* Fix sign */
904
910
to[0]= (unsigned char) (value >> 56);
906
912
to[0]= (unsigned char) (value >> 56) ^ 128; /* Reverse signbit */
909
915
case DECIMAL_RESULT:
911
917
my_decimal dec_buf, *dec_val= item->val_decimal_result(&dec_buf);
1051
bool SortParam::save_index(unsigned char **sort_keys, uint32_t count,
1052
filesort_info *table_sort)
1060
bool SortParam::save_index(unsigned char **sort_keys, uint32_t count, filesort_info *table_sort)
1054
1062
uint32_t offset;
1055
1063
unsigned char *to;
1057
1065
internal::my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, sort_length);
1058
1066
offset= rec_length - res_length;
1059
1068
if ((ha_rows) count > max_rows)
1060
1069
count=(uint32_t) max_rows;
1061
if (!(to= table_sort->record_pointers=
1062
(unsigned char*) malloc(res_length*count)))
1071
if (!(to= table_sort->record_pointers= (unsigned char*) malloc(res_length*count)))
1065
1074
for (unsigned char **end_ptr= sort_keys+count ; sort_keys != end_ptr ; sort_keys++)
1486
1500
sortorder->result_type= sortorder->item->result_type();
1487
1501
if (sortorder->item->result_as_int64_t())
1488
1502
sortorder->result_type= INT_RESULT;
1489
1504
switch (sortorder->result_type) {
1490
1505
case STRING_RESULT:
1491
sortorder->length=sortorder->item->max_length;
1506
sortorder->length=sortorder->item->max_length;
1492
1507
set_if_smaller(sortorder->length,
1493
1508
getSession().variables.max_sort_length);
1494
if (use_strnxfrm((cs=sortorder->item->collation.collation)))
1509
if (use_strnxfrm((cs=sortorder->item->collation.collation)))
1496
1511
sortorder->length= cs->coll->strnxfrmlen(cs, sortorder->length);
1497
sortorder->need_strxnfrm= 1;
1498
*multi_byte_charset= 1;
1512
sortorder->need_strxnfrm= 1;
1513
*multi_byte_charset= 1;
1500
1515
else if (cs == &my_charset_bin)
1502
1517
/* Store length last to be able to sort blob/varbinary */
1503
1518
sortorder->suffix_length= suffix_length(sortorder->length);
1504
1519
sortorder->length+= sortorder->suffix_length;
1507
1522
case INT_RESULT:
1508
sortorder->length=8; // Size of intern int64_t
1523
sortorder->length=8; // Size of intern int64_t
1510
1525
case DECIMAL_RESULT:
1511
1526
sortorder->length=
1512
1527
my_decimal_get_binary_size(sortorder->item->max_length -