~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/filesort.cc

  • Committer: Brian Aker
  • Date: 2010-12-18 02:06:13 UTC
  • Revision ID: brian@tangent.org-20101218020613-8lxhcvsy812bu960
Formatting + remove default from switch/case.

Show diffs side-by-side

added added

removed removed

Lines of Context:
817
817
    {                                           // Item
818
818
      Item *item=sort_field->item;
819
819
      maybe_null= item->maybe_null;
 
820
 
820
821
      switch (sort_field->result_type) {
821
822
      case STRING_RESULT:
822
 
      {
823
 
        const CHARSET_INFO * const cs=item->collation.collation;
824
 
        char fill_char= ((cs->state & MY_CS_BINSORT) ? (char) 0 : ' ');
825
 
        int diff;
826
 
        uint32_t sort_field_length;
 
823
        {
 
824
          const CHARSET_INFO * const cs=item->collation.collation;
 
825
          char fill_char= ((cs->state & MY_CS_BINSORT) ? (char) 0 : ' ');
 
826
          int diff;
 
827
          uint32_t sort_field_length;
827
828
 
828
 
        if (maybe_null)
829
 
          *to++=1;
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);
833
 
        if (!res)
834
 
        {
835
829
          if (maybe_null)
836
 
            memset(to-1, 0, sort_field->length+1);
 
830
            *to++=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);
 
834
          if (!res)
 
835
          {
 
836
            if (maybe_null)
 
837
              memset(to-1, 0, sort_field->length+1);
 
838
            else
 
839
            {
 
840
              /*
 
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.
 
844
              */
 
845
              assert(0);
 
846
              memset(to, 0, sort_field->length);        // Avoid crash
 
847
            }
 
848
            break;
 
849
          }
 
850
          length= res->length();
 
851
          sort_field_length= sort_field->length - sort_field->suffix_length;
 
852
          diff=(int) (sort_field_length - length);
 
853
          if (diff < 0)
 
854
          {
 
855
            diff=0;
 
856
            length= sort_field_length;
 
857
          }
 
858
          if (sort_field->suffix_length)
 
859
          {
 
860
            /* Store length last in result_string */
 
861
            store_length(to + sort_field_length, length,
 
862
                         sort_field->suffix_length);
 
863
          }
 
864
          if (sort_field->need_strxnfrm)
 
865
          {
 
866
            char *from=(char*) res->ptr();
 
867
            uint32_t tmp_length;
 
868
            if ((unsigned char*) from == to)
 
869
            {
 
870
              set_if_smaller(length,sort_field->length);
 
871
              memcpy(tmp_buffer,from,length);
 
872
              from= tmp_buffer;
 
873
            }
 
874
            tmp_length= my_strnxfrm(cs,to,sort_field->length,
 
875
                                    (unsigned char*) from, length);
 
876
            assert(tmp_length == sort_field->length);
 
877
          }
837
878
          else
838
879
          {
839
 
            /*
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.
843
 
            */
844
 
            assert(0);
845
 
            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);
846
882
          }
847
883
          break;
848
884
        }
849
 
        length= res->length();
850
 
        sort_field_length= sort_field->length - sort_field->suffix_length;
851
 
        diff=(int) (sort_field_length - length);
852
 
        if (diff < 0)
853
 
        {
854
 
          diff=0;
855
 
          length= sort_field_length;
856
 
        }
857
 
        if (sort_field->suffix_length)
858
 
        {
859
 
          /* Store length last in result_string */
860
 
          store_length(to + sort_field_length, length,
861
 
                       sort_field->suffix_length);
862
 
        }
863
 
        if (sort_field->need_strxnfrm)
864
 
        {
865
 
          char *from=(char*) res->ptr();
866
 
          uint32_t tmp_length;
867
 
          if ((unsigned char*) from == to)
868
 
          {
869
 
            set_if_smaller(length,sort_field->length);
870
 
            memcpy(tmp_buffer,from,length);
871
 
            from= tmp_buffer;
872
 
          }
873
 
          tmp_length= my_strnxfrm(cs,to,sort_field->length,
874
 
                                  (unsigned char*) from, length);
875
 
          assert(tmp_length == sort_field->length);
876
 
        }
877
 
        else
878
 
        {
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);
881
 
        }
882
 
        break;
883
 
      }
884
885
      case INT_RESULT:
885
 
        {
 
886
        {
886
887
          int64_t value= item->val_int_result();
887
888
          if (maybe_null)
888
889
          {
889
 
            *to++=1;
 
890
            *to++=1;
890
891
            if (item->null_value)
891
892
            {
892
893
              if (maybe_null)
898
899
              break;
899
900
            }
900
901
          }
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);
 
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);
908
909
          if (item->unsigned_flag)                    /* Fix sign */
909
910
            to[0]= (unsigned char) (value >> 56);
910
911
          else
911
912
            to[0]= (unsigned char) (value >> 56) ^ 128; /* Reverse signbit */
912
 
          break;
913
 
        }
 
913
          break;
 
914
        }
914
915
      case DECIMAL_RESULT:
915
916
        {
916
917
          my_decimal dec_buf, *dec_val= item->val_decimal_result(&dec_buf);
927
928
          my_decimal2binary(E_DEC_FATAL_ERROR, dec_val, to,
928
929
                            item->max_length - (item->decimals ? 1:0),
929
930
                            item->decimals);
930
 
         break;
 
931
          break;
931
932
        }
932
933
      case REAL_RESULT:
933
 
        {
 
934
        {
934
935
          double value= item->val_result();
935
 
          if (maybe_null)
 
936
          if (maybe_null)
936
937
          {
937
938
            if (item->null_value)
938
939
            {
940
941
              to++;
941
942
              break;
942
943
            }
943
 
            *to++=1;
 
944
            *to++=1;
944
945
          }
945
 
          change_double_for_sort(value,(unsigned char*) to);
946
 
          break;
947
 
        }
 
946
          change_double_for_sort(value,(unsigned char*) to);
 
947
          break;
 
948
        }
948
949
      case ROW_RESULT:
949
950
      default:
950
 
        // This case should never be choosen
951
 
        assert(0);
952
 
        break;
 
951
        // This case should never be choosen
 
952
        assert(0);
 
953
        break;
953
954
      }
954
955
    }
 
956
 
955
957
    if (sort_field->reverse)
956
958
    {                                                   /* Revers key */
957
959
      if (maybe_null)
964
966
      }
965
967
    }
966
968
    else
 
969
    {
967
970
      to+= sort_field->length;
 
971
    }
968
972
  }
969
973
 
970
974
  if (addon_field)
1496
1500
      sortorder->result_type= sortorder->item->result_type();
1497
1501
      if (sortorder->item->result_as_int64_t())
1498
1502
        sortorder->result_type= INT_RESULT;
 
1503
 
1499
1504
      switch (sortorder->result_type) {
1500
1505
      case STRING_RESULT:
1501
 
        sortorder->length=sortorder->item->max_length;
 
1506
        sortorder->length=sortorder->item->max_length;
1502
1507
        set_if_smaller(sortorder->length,
1503
1508
                       getSession().variables.max_sort_length);
1504
 
        if (use_strnxfrm((cs=sortorder->item->collation.collation)))
1505
 
        {
 
1509
        if (use_strnxfrm((cs=sortorder->item->collation.collation)))
 
1510
        {
1506
1511
          sortorder->length= cs->coll->strnxfrmlen(cs, sortorder->length);
1507
 
          sortorder->need_strxnfrm= 1;
1508
 
          *multi_byte_charset= 1;
1509
 
        }
 
1512
          sortorder->need_strxnfrm= 1;
 
1513
          *multi_byte_charset= 1;
 
1514
        }
1510
1515
        else if (cs == &my_charset_bin)
1511
1516
        {
1512
1517
          /* Store length last to be able to sort blob/varbinary */
1513
1518
          sortorder->suffix_length= suffix_length(sortorder->length);
1514
1519
          sortorder->length+= sortorder->suffix_length;
1515
1520
        }
1516
 
        break;
 
1521
        break;
1517
1522
      case INT_RESULT:
1518
 
        sortorder->length=8;                    // Size of intern int64_t
1519
 
        break;
 
1523
        sortorder->length=8;                    // Size of intern int64_t
 
1524
        break;
1520
1525
      case DECIMAL_RESULT:
1521
1526
        sortorder->length=
1522
1527
          my_decimal_get_binary_size(sortorder->item->max_length -
1524
1529
                                     sortorder->item->decimals);
1525
1530
        break;
1526
1531
      case REAL_RESULT:
1527
 
        sortorder->length=sizeof(double);
1528
 
        break;
 
1532
        sortorder->length=sizeof(double);
 
1533
        break;
1529
1534
      case ROW_RESULT:
1530
 
      default:
1531
 
        // This case should never be choosen
1532
 
        assert(0);
1533
 
        break;
 
1535
        // This case should never be choosen
 
1536
        assert(0);
 
1537
        break;
1534
1538
      }
1535
1539
      if (sortorder->item->maybe_null)
1536
 
        length++;                               // Place for NULL marker
 
1540
        length++;                               // Place for NULL marker
1537
1541
    }
1538
1542
    set_if_smaller(sortorder->length, (size_t)getSession().variables.max_sort_length);
1539
1543
    length+=sortorder->length;