820
813
Item *item=sort_field->item;
821
814
maybe_null= item->maybe_null;
823
815
switch (sort_field->result_type) {
824
816
case STRING_RESULT:
826
const CHARSET_INFO * const cs=item->collation.collation;
827
char fill_char= ((cs->state & MY_CS_BINSORT) ? (char) 0 : ' ');
829
uint32_t sort_field_length;
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;
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);
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);
839
memset(to-1, 0, sort_field->length+1);
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.
848
memset(to, 0, sort_field->length); // Avoid crash
852
length= res->length();
853
sort_field_length= sort_field->length - sort_field->suffix_length;
854
diff=(int) (sort_field_length - length);
858
length= sort_field_length;
860
if (sort_field->suffix_length)
862
/* Store length last in result_string */
863
store_length(to + sort_field_length, length,
864
sort_field->suffix_length);
866
if (sort_field->need_strxnfrm)
868
char *from=(char*) res->ptr();
870
if ((unsigned char*) from == to)
872
set_if_smaller(length,sort_field->length);
873
memcpy(tmp_buffer,from,length);
876
tmp_length= my_strnxfrm(cs,to,sort_field->length,
877
(unsigned char*) from, length);
878
assert(tmp_length == sort_field->length);
831
memset(to-1, 0, sort_field->length+1);
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);
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
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);
889
881
int64_t value= item->val_int_result();
893
885
if (item->null_value)
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);
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);
911
903
if (item->unsigned_flag) /* Fix sign */
912
904
to[0]= (unsigned char) (value >> 56);
914
906
to[0]= (unsigned char) (value >> 56) ^ 128; /* Reverse signbit */
917
909
case DECIMAL_RESULT:
919
type::Decimal dec_buf, *dec_val= item->val_decimal_result(&dec_buf);
911
my_decimal dec_buf, *dec_val= item->val_decimal_result(&dec_buf);
922
914
if (item->null_value)
1062
bool SortParam::save_index(unsigned char **sort_keys, uint32_t count, filesort_info *table_sort)
1051
bool SortParam::save_index(unsigned char **sort_keys, uint32_t count,
1052
filesort_info *table_sort)
1064
1054
uint32_t offset;
1065
1055
unsigned char *to;
1067
1057
internal::my_string_ptr_sort((unsigned char*) sort_keys, (uint32_t) count, sort_length);
1068
1058
offset= rec_length - res_length;
1070
1059
if ((ha_rows) count > max_rows)
1071
1060
count=(uint32_t) max_rows;
1073
if (!(to= table_sort->record_pointers= (unsigned char*) malloc(res_length*count)))
1061
if (!(to= table_sort->record_pointers=
1062
(unsigned char*) malloc(res_length*count)))
1076
1065
for (unsigned char **end_ptr= sort_keys+count ; sort_keys != end_ptr ; sort_keys++)
1502
1486
sortorder->result_type= sortorder->item->result_type();
1503
1487
if (sortorder->item->result_as_int64_t())
1504
1488
sortorder->result_type= INT_RESULT;
1506
1489
switch (sortorder->result_type) {
1507
1490
case STRING_RESULT:
1508
sortorder->length=sortorder->item->max_length;
1491
sortorder->length=sortorder->item->max_length;
1509
1492
set_if_smaller(sortorder->length,
1510
1493
getSession().variables.max_sort_length);
1511
if (use_strnxfrm((cs=sortorder->item->collation.collation)))
1494
if (use_strnxfrm((cs=sortorder->item->collation.collation)))
1513
1496
sortorder->length= cs->coll->strnxfrmlen(cs, sortorder->length);
1514
sortorder->need_strxnfrm= 1;
1515
*multi_byte_charset= 1;
1497
sortorder->need_strxnfrm= 1;
1498
*multi_byte_charset= 1;
1517
1500
else if (cs == &my_charset_bin)
1519
1502
/* Store length last to be able to sort blob/varbinary */
1520
1503
sortorder->suffix_length= suffix_length(sortorder->length);
1521
1504
sortorder->length+= sortorder->suffix_length;
1524
1507
case INT_RESULT:
1525
sortorder->length=8; // Size of intern int64_t
1508
sortorder->length=8; // Size of intern int64_t
1527
1510
case DECIMAL_RESULT:
1528
1511
sortorder->length=
1529
class_decimal_get_binary_size(sortorder->item->max_length -
1512
my_decimal_get_binary_size(sortorder->item->max_length -
1530
1513
(sortorder->item->decimals ? 1 : 0),
1531
1514
sortorder->item->decimals);
1533
1516
case REAL_RESULT:
1534
sortorder->length=sizeof(double);
1517
sortorder->length=sizeof(double);
1536
1519
case ROW_RESULT:
1537
// This case should never be choosen
1521
// This case should never be choosen
1541
1525
if (sortorder->item->maybe_null)
1542
length++; // Place for NULL marker
1526
length++; // Place for NULL marker
1544
1528
set_if_smaller(sortorder->length, (size_t)getSession().variables.max_sort_length);
1545
1529
length+=sortorder->length;