~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/sum.cc

Merge Stewart's dead code removal

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
  @brief
21
21
  Sum functions (COUNT, MIN...)
22
22
*/
23
 
#include "config.h"
24
 
#include <math.h>
 
23
#include <drizzled/server_includes.h>
25
24
#include <drizzled/sql_select.h>
26
25
#include <drizzled/error.h>
27
26
#include <drizzled/hybrid_type_traits.h>
36
35
#include <drizzled/field/date.h>
37
36
#include <drizzled/field/datetime.h>
38
37
 
39
 
#include "drizzled/internal/m_string.h"
40
 
 
41
38
#include <algorithm>
42
39
 
43
40
using namespace std;
44
41
 
45
 
namespace drizzled
46
 
{
47
 
 
48
42
extern my_decimal decimal_zero;
49
 
extern plugin::StorageEngine *heap_engine;
50
43
 
51
44
/**
52
45
  Prepare an aggregate function item for checking context conditions.
380
373
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements),
381
374
  forced_const(false)
382
375
{
383
 
  if ((args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
 
376
  if ((args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
384
377
  {
385
378
    uint32_t i=0;
386
379
    List_iterator_fast<Item> li(list);
524
517
                               name, table->s, collation.collation);
525
518
    break;
526
519
  case DECIMAL_RESULT:
527
 
    field= new Field_decimal(max_length, maybe_null, name,
 
520
    field= new Field_new_decimal(max_length, maybe_null, name,
528
521
                                 decimals, unsigned_flag);
529
522
    break;
530
523
  case ROW_RESULT:
876
869
 
877
870
/***************************************************************************/
878
871
 
 
872
#ifdef __cplusplus
 
873
extern "C" {
 
874
#endif
 
875
 
879
876
/* Declarations for auxilary C-callbacks */
880
877
 
881
878
static int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
885
882
 
886
883
 
887
884
static int item_sum_distinct_walk(void *element,
888
 
                                  uint32_t ,
 
885
                                  element_count ,
889
886
                                  void *item)
890
887
{
891
888
  return ((Item_sum_distinct*) (item))->unique_walk_function(element);
892
889
}
893
890
 
 
891
#ifdef __cplusplus
 
892
}
 
893
#endif
 
894
 
894
895
/* Item_sum_distinct */
895
896
 
896
897
Item_sum_distinct::Item_sum_distinct(Item *item_arg)
979
980
  case DECIMAL_RESULT:
980
981
    val.traits= Hybrid_type_traits_decimal::instance();
981
982
    if (table_field_type != DRIZZLE_TYPE_LONGLONG)
982
 
      table_field_type= DRIZZLE_TYPE_DECIMAL;
 
983
      table_field_type= DRIZZLE_TYPE_NEWDECIMAL;
983
984
    break;
984
985
  case ROW_RESULT:
985
986
  default:
1258
1259
                               0, name, table->s, &my_charset_bin);
1259
1260
  }
1260
1261
  else if (hybrid_type == DECIMAL_RESULT)
1261
 
    field= new Field_decimal(max_length, maybe_null, name,
1262
 
                             decimals, unsigned_flag);
 
1262
    field= new Field_new_decimal(max_length, maybe_null, name,
 
1263
                                 decimals, unsigned_flag);
1263
1264
  else
1264
1265
    field= new Field_double(max_length, maybe_null, name, decimals, true);
1265
1266
  if (field)
2508
2509
  return 0;
2509
2510
}
2510
2511
 
 
2512
#ifdef __cplusplus
 
2513
extern "C" {
 
2514
#endif
 
2515
 
2511
2516
static int count_distinct_walk(void *,
2512
 
                               uint32_t ,
 
2517
                               element_count ,
2513
2518
                               void *arg)
2514
2519
{
2515
2520
  (*((uint64_t*)arg))++;
2516
2521
  return 0;
2517
2522
}
2518
2523
 
 
2524
#ifdef __cplusplus
 
2525
}
 
2526
#endif
 
2527
 
 
2528
 
 
2529
 
2519
2530
void Item_sum_count_distinct::cleanup()
2520
2531
{
2521
2532
  Item_sum_int::cleanup();
2603
2614
                                (select_lex->options | session->options),
2604
2615
                                HA_POS_ERROR, (char*)"")))
2605
2616
    return true;
2606
 
  table->cursor->extra(HA_EXTRA_NO_ROWS);               // Don't update rows
 
2617
  table->file->extra(HA_EXTRA_NO_ROWS);         // Don't update rows
2607
2618
  table->no_rows=1;
2608
2619
 
2609
2620
  if (table->s->db_type() == heap_engine)
2695
2706
  }
2696
2707
  else if (table)
2697
2708
  {
2698
 
    table->cursor->extra(HA_EXTRA_NO_CACHE);
2699
 
    table->cursor->ha_delete_all_rows();
2700
 
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
 
2709
    table->file->extra(HA_EXTRA_NO_CACHE);
 
2710
    table->file->ha_delete_all_rows();
 
2711
    table->file->extra(HA_EXTRA_WRITE_CACHE);
2701
2712
  }
2702
2713
}
2703
2714
 
2724
2735
    */
2725
2736
    return tree->unique_add(table->record[0] + table->s->null_bytes);
2726
2737
  }
2727
 
  if ((error= table->cursor->ha_write_row(table->record[0])) &&
2728
 
      table->cursor->is_fatal_error(error, HA_CHECK_DUP))
 
2738
  if ((error= table->file->ha_write_row(table->record[0])) &&
 
2739
      table->file->is_fatal_error(error, HA_CHECK_DUP))
2729
2740
    return true;
2730
2741
  return false;
2731
2742
}
2750
2761
    return (int64_t) count;
2751
2762
  }
2752
2763
 
2753
 
  error= table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
2764
  error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2754
2765
 
2755
2766
  if(error)
2756
2767
  {
2757
 
    table->print_error(error, MYF(0));
 
2768
    table->file->print_error(error, MYF(0));
2758
2769
  }
2759
2770
 
2760
 
  return table->cursor->stats.records;
 
2771
  return table->file->stats.records;
2761
2772
}
2762
2773
 
2763
2774
/*****************************************************************************
2820
2831
 
2821
2832
 
2822
2833
/**
2823
 
  function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
 
2834
  function of sort for syntax: GROUP_CONCAT(expr,... order_st BY col,... )
2824
2835
*/
2825
2836
 
2826
2837
int group_concat_key_cmp_with_order(void* arg, const void* key1,
2867
2878
  Append data from current leaf to item->result.
2868
2879
*/
2869
2880
 
2870
 
int dump_leaf_key(unsigned char* key, uint32_t ,
 
2881
int dump_leaf_key(unsigned char* key, element_count ,
2871
2882
                  Item_func_group_concat *item)
2872
2883
{
2873
2884
  Table *table= item->table;
2949
2960
                       bool distinct_arg, List<Item> *select_list,
2950
2961
                       SQL_LIST *order_list, String *separator_arg)
2951
2962
  :tmp_table_param(0), warning(0),
2952
 
   separator(separator_arg), tree(NULL), unique_filter(NULL), table(0),
 
2963
   separator(separator_arg), tree(0), unique_filter(NULL), table(0),
2953
2964
   order(0), context(context_arg),
2954
2965
   arg_count_order(order_list ? order_list->elements : 0),
2955
2966
   arg_count_field(select_list->elements),
2970
2981
           (for possible order items in temporare tables)
2971
2982
    order - arg_count_order
2972
2983
  */
2973
 
  if (!(args= (Item**) memory::sql_alloc(sizeof(Item*) * arg_count +
 
2984
  if (!(args= (Item**) sql_alloc(sizeof(Item*) * arg_count +
2974
2985
                                 sizeof(order_st*)*arg_count_order)))
2975
2986
    return;
2976
2987
 
3031
3042
  if (warning)
3032
3043
  {
3033
3044
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
3034
 
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
 
3045
    sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3035
3046
    warning->set_msg(current_session, warn_buff);
3036
3047
    warning= 0;
3037
3048
  }
3062
3073
      if (warning)
3063
3074
      {
3064
3075
        char warn_buff[DRIZZLE_ERRMSG_SIZE];
3065
 
        snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
 
3076
        sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3066
3077
        warning->set_msg(session, warn_buff);
3067
3078
        warning= 0;
3068
3079
      }
3237
3248
  {
3238
3249
    /*
3239
3250
      Currently we have to force conversion of BLOB values to VARCHAR's
3240
 
      if we are to store them in TREE objects used for ORDER BY and
 
3251
      if we are to store them in TREE objects used for order_st BY and
3241
3252
      DISTINCT. This leads to truncation if the BLOB's size exceeds
3242
3253
      Field_varstring::MAX_SIZE.
3243
3254
    */
3249
3260
    We have to create a temporary table to get descriptions of fields
3250
3261
    (types, sizes and so on).
3251
3262
 
3252
 
    Note that in the table, we first have the ORDER BY fields, then the
 
3263
    Note that in the table, we first have the order_st BY fields, then the
3253
3264
    field list.
3254
3265
  */
3255
3266
  if (!(table= create_tmp_table(session, tmp_table_param, all_fields,
3257
3268
                                (select_lex->options | session->options),
3258
3269
                                HA_POS_ERROR, (char*) "")))
3259
3270
    return(true);
3260
 
  table->cursor->extra(HA_EXTRA_NO_ROWS);
 
3271
  table->file->extra(HA_EXTRA_NO_ROWS);
3261
3272
  table->no_rows= 1;
3262
3273
 
3263
3274
  /*
3272
3283
    tree= &tree_base;
3273
3284
    /*
3274
3285
      Create a tree for sorting. The tree is used to sort (according to the
3275
 
      syntax of this function). If there is no ORDER BY clause, we don't
 
3286
      syntax of this function). If there is no order_st BY clause, we don't
3276
3287
      create this tree.
3277
3288
    */
3278
3289
    init_tree(tree, (uint32_t) min(session->variables.max_heap_table_size,
3303
3314
  tree= 0;
3304
3315
}
3305
3316
 
3306
 
double Item_func_group_concat::val_real()
3307
 
{
3308
 
  String *res;  res=val_str(&str_value);
3309
 
  return res ? internal::my_atof(res->c_ptr()) : 0.0;
3310
 
}
3311
 
 
3312
 
int64_t Item_func_group_concat::val_int()
3313
 
{
3314
 
  String *res;
3315
 
  char *end_ptr;
3316
 
  int error;
3317
 
  if (!(res= val_str(&str_value)))
3318
 
    return (int64_t) 0;
3319
 
  end_ptr= (char*) res->ptr()+ res->length();
3320
 
  return internal::my_strtoll10(res->ptr(), &end_ptr, &error);
3321
 
}
3322
3317
 
3323
3318
String* Item_func_group_concat::val_str(String* )
3324
3319
{
3380
3375
  if (!original && unique_filter)
3381
3376
    delete unique_filter;
3382
3377
}
3383
 
 
3384
 
} /* namespace drizzled */