~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/sum.cc

Remove dead memset call.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
  Sum functions (COUNT, MIN...)
22
22
*/
23
23
#include "config.h"
 
24
#include <cstdio>
24
25
#include <math.h>
25
26
#include <drizzled/sql_select.h>
26
27
#include <drizzled/error.h>
521
522
        !convert_blob_length)
522
523
      return make_string_field(table);
523
524
    field= new Field_varstring(convert_blob_length, maybe_null,
524
 
                               name, table->s, collation.collation);
 
525
                               name, table->getMutableShare(), collation.collation);
525
526
    break;
526
527
  case DECIMAL_RESULT:
527
528
    field= new Field_decimal(max_length, maybe_null, name,
952
953
{
953
954
  assert(args[0]->fixed);
954
955
 
 
956
  null_value= maybe_null= true;
955
957
  table_field_type= args[0]->field_type();
956
958
 
957
959
  /* Adjust tmp table type according to the chosen aggregation type */
1023
1025
  field_def.init_for_tmp_table(table_field_type, args[0]->max_length,
1024
1026
                               args[0]->decimals, args[0]->maybe_null);
1025
1027
 
1026
 
  if (! (table= create_virtual_tmp_table(session, field_list)))
 
1028
  if (! (table= session->create_virtual_tmp_table(field_list)))
1027
1029
    return(true);
1028
1030
 
1029
1031
  /* XXX: check that the case of CHAR(0) works OK */
1030
 
  tree_key_length= table->s->reclength - table->s->null_bytes;
 
1032
  tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
1031
1033
 
1032
1034
  /*
1033
1035
    Unique handles all unique elements in a tree until they can't fit
1046
1048
 
1047
1049
bool Item_sum_distinct::add()
1048
1050
{
1049
 
  args[0]->save_in_field(table->field[0], false);
 
1051
  args[0]->save_in_field(table->getField(0), false);
1050
1052
  is_evaluated= false;
1051
 
  if (!table->field[0]->is_null())
 
1053
  if (!table->getField(0)->is_null())
1052
1054
  {
1053
1055
    assert(tree);
1054
1056
    null_value= 0;
1056
1058
      '0' values are also stored in the tree. This doesn't matter
1057
1059
      for SUM(DISTINCT), but is important for AVG(DISTINCT)
1058
1060
    */
1059
 
    return tree->unique_add(table->field[0]->ptr);
 
1061
    return tree->unique_add(table->getField(0)->ptr);
1060
1062
  }
1061
1063
  return 0;
1062
1064
}
1064
1066
 
1065
1067
bool Item_sum_distinct::unique_walk_function(void *element)
1066
1068
{
1067
 
  memcpy(table->field[0]->ptr, element, tree_key_length);
 
1069
  memcpy(table->getField(0)->ptr, element, tree_key_length);
1068
1070
  ++count;
1069
 
  val.traits->add(&val, table->field[0]);
 
1071
  val.traits->add(&val, table->getField(0));
1070
1072
  return 0;
1071
1073
}
1072
1074
 
1108
1110
     */
1109
1111
    if (tree)
1110
1112
    {
1111
 
      table->field[0]->set_notnull();
 
1113
      table->getField(0)->set_notnull();
1112
1114
      tree->walk(item_sum_distinct_walk, (void*) this);
1113
1115
    }
1114
1116
    is_evaluated= true;
1255
1257
    */
1256
1258
    field= new Field_varstring(((hybrid_type == DECIMAL_RESULT) ?
1257
1259
                                dec_bin_size : sizeof(double)) + sizeof(int64_t),
1258
 
                               0, name, table->s, &my_charset_bin);
 
1260
                               0, name, table->getMutableShare(), &my_charset_bin);
1259
1261
  }
1260
1262
  else if (hybrid_type == DECIMAL_RESULT)
1261
1263
    field= new Field_decimal(max_length, maybe_null, name,
1469
1471
      The easiest way is to do this is to store both value in a string
1470
1472
      and unpack on access.
1471
1473
    */
1472
 
    field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, table->s, &my_charset_bin);
 
1474
    field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, table->getMutableShare(), &my_charset_bin);
1473
1475
  }
1474
1476
  else
1475
1477
    field= new Field_double(max_length, maybe_null, name, decimals, true);
2492
2494
int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2493
2495
{
2494
2496
  Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2495
 
  Field **field    = item->table->field;
2496
 
  Field **field_end= field + item->table->s->fields;
 
2497
  Field **field    = item->table->getFields();
 
2498
  Field **field_end= field + item->table->getShare()->sizeFields();
2497
2499
  uint32_t *lengths=item->field_lengths;
2498
2500
  for (; field < field_end; ++field)
2499
2501
  {
2533
2535
    is_evaluated= false;
2534
2536
    if (table)
2535
2537
    {
2536
 
      table->free_tmp_table(table->in_use);
2537
2538
      table= 0;
2538
2539
    }
2539
2540
    delete tmp_table_param;
2602
2603
                                0,
2603
2604
                                (select_lex->options | session->options),
2604
2605
                                HA_POS_ERROR, (char*)"")))
 
2606
  {
2605
2607
    return true;
 
2608
  }
2606
2609
  table->cursor->extra(HA_EXTRA_NO_ROWS);               // Don't update rows
2607
2610
  table->no_rows=1;
2608
2611
 
2609
 
  if (table->s->db_type() == heap_engine)
 
2612
  if (table->getShare()->db_type() == heap_engine)
2610
2613
  {
2611
2614
    /*
2612
2615
      No blobs, otherwise it would have been MyISAM: set up a compare
2614
2617
    */
2615
2618
    qsort_cmp2 compare_key;
2616
2619
    void* cmp_arg;
2617
 
    Field **field= table->field;
2618
 
    Field **field_end= field + table->s->fields;
 
2620
    Field **field= table->getFields();
 
2621
    Field **field_end= field + table->getShare()->sizeFields();
2619
2622
    bool all_binary= true;
2620
2623
 
2621
2624
    for (tree_key_length= 0; field < field_end; ++field)
2636
2639
    }
2637
2640
    else
2638
2641
    {
2639
 
      if (table->s->fields == 1)
 
2642
      if (table->getShare()->sizeFields() == 1)
2640
2643
      {
2641
2644
        /*
2642
2645
          If we have only one field, which is the most common use of
2645
2648
          about other fields.
2646
2649
        */
2647
2650
        compare_key= (qsort_cmp2) simple_str_key_cmp;
2648
 
        cmp_arg= (void*) table->field[0];
 
2651
        cmp_arg= (void*) table->getField(0);
2649
2652
        /* tree_key_length has been set already */
2650
2653
      }
2651
2654
      else
2653
2656
        uint32_t *length;
2654
2657
        compare_key= (qsort_cmp2) composite_key_cmp;
2655
2658
        cmp_arg= (void*) this;
2656
 
        field_lengths= (uint32_t*) session->alloc(table->s->fields * sizeof(uint32_t));
2657
 
        for (tree_key_length= 0, length= field_lengths, field= table->field;
 
2659
        field_lengths= (uint32_t*) session->alloc(table->getShare()->sizeFields() * sizeof(uint32_t));
 
2660
        for (tree_key_length= 0, length= field_lengths, field= table->getFields();
2658
2661
             field < field_end; ++field, ++length)
2659
2662
        {
2660
2663
          *length= (*field)->pack_length();
2709
2712
  copy_fields(tmp_table_param);
2710
2713
  copy_funcs(tmp_table_param->items_to_copy);
2711
2714
 
2712
 
  for (Field **field=table->field ; *field ; field++)
 
2715
  for (Field **field= table->getFields() ; *field ; field++)
 
2716
  {
2713
2717
    if ((*field)->is_real_null(0))
 
2718
    {
2714
2719
      return 0;                                 // Don't count NULL
 
2720
    }
 
2721
  }
2715
2722
 
2716
2723
  is_evaluated= false;
2717
2724
  if (tree)
2722
2729
      bloat the tree without providing any valuable info. Besides,
2723
2730
      key_length used to initialize the tree didn't include space for them.
2724
2731
    */
2725
 
    return tree->unique_add(table->record[0] + table->s->null_bytes);
 
2732
    return tree->unique_add(table->record[0] + table->getShare()->null_bytes);
2726
2733
  }
2727
 
  if ((error= table->cursor->ha_write_row(table->record[0])) &&
 
2734
  if ((error= table->cursor->insertRecord(table->record[0])) &&
2728
2735
      table->cursor->is_fatal_error(error, HA_CHECK_DUP))
2729
2736
    return true;
2730
2737
  return false;
2811
2818
    */
2812
2819
    Field *field= item->get_tmp_table_field();
2813
2820
    int res;
2814
 
    uint32_t offset= field->offset(field->table->record[0])-table->s->null_bytes;
 
2821
    uint32_t offset= field->offset(field->getTable()->record[0])-table->getShare()->null_bytes;
2815
2822
    if((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2816
2823
      return res;
2817
2824
  }
2848
2855
    if (field && !item->const_item())
2849
2856
    {
2850
2857
      int res;
2851
 
      uint32_t offset= (field->offset(field->table->record[0]) -
2852
 
                    table->s->null_bytes);
 
2858
      uint32_t offset= (field->offset(field->getTable()->record[0]) -
 
2859
                    table->getShare()->null_bytes);
2853
2860
      if ((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2854
2861
        return (*order_item)->asc ? res : -res;
2855
2862
    }
2871
2878
                  Item_func_group_concat *item)
2872
2879
{
2873
2880
  Table *table= item->table;
2874
 
  String tmp((char *)table->record[1], table->s->reclength,
 
2881
  String tmp((char *)table->getUpdateRecord(), table->getShare()->getRecordLength(),
2875
2882
             default_charset_info);
2876
2883
  String tmp2;
2877
2884
  String *result= &item->result;
2898
2905
        because it contains both order and arg list fields.
2899
2906
      */
2900
2907
      Field *field= (*arg)->get_tmp_table_field();
2901
 
      uint32_t offset= (field->offset(field->table->record[0]) -
2902
 
                    table->s->null_bytes);
2903
 
      assert(offset < table->s->reclength);
 
2908
      uint32_t offset= (field->offset(field->getTable()->record[0]) -
 
2909
                    table->getShare()->null_bytes);
 
2910
      assert(offset < table->getShare()->getRecordLength());
2904
2911
      res= field->val_str(&tmp, key + offset);
2905
2912
    }
2906
2913
    else
3047
3054
    if (table)
3048
3055
    {
3049
3056
      Session *session= table->in_use;
3050
 
      table->free_tmp_table(session);
3051
3057
      table= 0;
3052
3058
      if (tree)
3053
3059
      {
3119
3125
  {
3120
3126
    /* Filter out duplicate rows. */
3121
3127
    uint32_t count= unique_filter->elements_in_tree();
3122
 
    unique_filter->unique_add(table->record[0] + table->s->null_bytes);
 
3128
    unique_filter->unique_add(table->record[0] + table->getShare()->null_bytes);
3123
3129
    if (count == unique_filter->elements_in_tree())
3124
3130
      row_eligible= false;
3125
3131
  }
3126
3132
 
3127
3133
  TREE_ELEMENT *el= 0;                          // Only for safety
3128
3134
  if (row_eligible && tree)
3129
 
    el= tree_insert(tree, table->record[0] + table->s->null_bytes, 0,
 
3135
    el= tree_insert(tree, table->record[0] + table->getShare()->null_bytes, 0,
3130
3136
                    tree->custom_arg);
3131
3137
  /*
3132
3138
    If the row is not a duplicate (el->count == 1)
3135
3141
  */
3136
3142
  if (row_eligible && !warning_for_row &&
3137
3143
      (!tree || (el->count == 1 && distinct && !arg_count_order)))
3138
 
    dump_leaf_key(table->record[0] + table->s->null_bytes, 1, this);
 
3144
    dump_leaf_key(table->record[0] + table->getShare()->null_bytes, 1, this);
3139
3145
 
3140
3146
  return 0;
3141
3147
}
3256
3262
                                (order_st*) 0, 0, true,
3257
3263
                                (select_lex->options | session->options),
3258
3264
                                HA_POS_ERROR, (char*) "")))
 
3265
  {
3259
3266
    return(true);
 
3267
  }
 
3268
 
3260
3269
  table->cursor->extra(HA_EXTRA_NO_ROWS);
3261
3270
  table->no_rows= 1;
3262
3271
 
3265
3274
     Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3266
3275
     the row is not added to the result.
3267
3276
  */
3268
 
  uint32_t tree_key_length= table->s->reclength - table->s->null_bytes;
 
3277
  uint32_t tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
3269
3278
 
3270
3279
  if (arg_count_order)
3271
3280
  {