~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_sum.cc

  • Committer: Monty Taylor
  • Date: 2008-10-16 06:32:30 UTC
  • mto: (511.1.5 codestyle)
  • mto: This revision was merged to the branch mainline in revision 521.
  • Revision ID: monty@inaugust.com-20081016063230-4brxsra0qsmsg84q
Added -Wunused-macros.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
  @brief
21
21
  Sum functions (COUNT, MIN...)
22
22
*/
23
 
 
24
 
#ifdef USE_PRAGMA_IMPLEMENTATION
25
 
#pragma implementation                          // gcc: Class implementation
26
 
#endif
27
 
 
28
 
#include "mysql_priv.h"
29
 
#include "sql_select.h"
 
23
#include <drizzled/server_includes.h>
 
24
#include <drizzled/sql_select.h>
 
25
#include <drizzled/drizzled_error_messages.h>
30
26
 
31
27
/**
32
28
  Prepare an aggregate function item for checking context conditions.
151
147
    if (register_sum_func(thd, ref))
152
148
      return true;
153
149
    invalid= aggr_level < 0 && !(allow_sum_func & (1 << nest_level));
154
 
    if (!invalid && thd->variables.sql_mode & MODE_ANSI)
 
150
    if (!invalid && false)
155
151
      invalid= aggr_level < 0 && max_arg_level < nest_level;
156
152
  }
157
153
  if (!invalid && aggr_level < 0)
360
356
{
361
357
  if ((args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
362
358
  {
363
 
    uint i=0;
 
359
    uint32_t i=0;
364
360
    List_iterator_fast<Item> li(list);
365
361
    Item *item;
366
362
 
426
422
void Item_sum::print(String *str, enum_query_type query_type)
427
423
{
428
424
  str->append(func_name());
429
 
  for (uint i=0 ; i < arg_count ; i++)
 
425
  for (uint32_t i=0 ; i < arg_count ; i++)
430
426
  {
431
427
    if (i)
432
428
      str->append(',');
438
434
void Item_sum::fix_num_length_and_dec()
439
435
{
440
436
  decimals=0;
441
 
  for (uint i=0 ; i < arg_count ; i++)
 
437
  for (uint32_t i=0 ; i < arg_count ; i++)
442
438
    set_if_bigger(decimals,args[i]->decimals);
443
439
  max_length=float_length(decimals);
444
440
}
449
445
  if (sum_item && sum_item->result_field)          // If not a const sum func
450
446
  {
451
447
    Field *result_field_tmp= sum_item->result_field;
452
 
    for (uint i=0 ; i < sum_item->arg_count ; i++)
 
448
    for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
453
449
    {
454
450
      Item *arg= sum_item->args[i];
455
451
      if (!arg->const_item())
466
462
 
467
463
 
468
464
bool Item_sum::walk (Item_processor processor, bool walk_subquery,
469
 
                     uchar *argument)
 
465
                     unsigned char *argument)
470
466
{
471
467
  if (arg_count)
472
468
  {
481
477
}
482
478
 
483
479
 
484
 
Field *Item_sum::create_tmp_field(bool group __attribute__((__unused__)),
485
 
                                  TABLE *table,
486
 
                                  uint convert_blob_length)
 
480
Field *Item_sum::create_tmp_field(bool group __attribute__((unused)),
 
481
                                  Table *table,
 
482
                                  uint32_t convert_blob_length)
487
483
{
488
484
  Field *field;
489
485
  switch (result_type()) {
522
518
  if (!forced_const)
523
519
  {
524
520
    used_tables_cache= 0;
525
 
    for (uint i=0 ; i < arg_count ; i++)
 
521
    for (uint32_t i=0 ; i < arg_count ; i++)
526
522
    {
527
523
      args[i]->update_used_tables();
528
524
      used_tables_cache|= args[i]->used_tables();
572
568
 
573
569
  decimals=0;
574
570
  maybe_null=0;
575
 
  for (uint i=0 ; i < arg_count ; i++)
 
571
  for (uint32_t i=0 ; i < arg_count ; i++)
576
572
  {
577
573
    if (args[i]->fix_fields(thd, args + i) || args[i]->check_cols(1))
578
574
      return true;
677
673
  return false;
678
674
}
679
675
 
680
 
Field *Item_sum_hybrid::create_tmp_field(bool group, TABLE *table,
681
 
                                         uint convert_blob_length)
 
676
Field *Item_sum_hybrid::create_tmp_field(bool group, Table *table,
 
677
                                         uint32_t convert_blob_length)
682
678
{
683
679
  Field *field;
684
680
  if (args[0]->type() == Item::FIELD_ITEM)
696
692
    fields creations separately.
697
693
  */
698
694
  switch (args[0]->field_type()) {
699
 
  case MYSQL_TYPE_NEWDATE:
 
695
  case DRIZZLE_TYPE_NEWDATE:
700
696
    field= new Field_newdate(maybe_null, name, collation.collation);
701
697
    break;
702
 
  case MYSQL_TYPE_TIME:
 
698
  case DRIZZLE_TYPE_TIME:
703
699
    field= new Field_time(maybe_null, name, collation.collation);
704
700
    break;
705
 
  case MYSQL_TYPE_TIMESTAMP:
706
 
  case MYSQL_TYPE_DATETIME:
 
701
  case DRIZZLE_TYPE_TIMESTAMP:
 
702
  case DRIZZLE_TYPE_DATETIME:
707
703
    field= new Field_datetime(maybe_null, name, collation.collation);
708
704
    break;
709
705
  default:
850
846
 
851
847
/***************************************************************************/
852
848
 
853
 
C_MODE_START
 
849
#ifdef __cplusplus
 
850
extern "C" {
 
851
#endif
854
852
 
855
853
/* Declarations for auxilary C-callbacks */
856
854
 
857
855
static int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
858
856
{
859
 
    return memcmp(key1, key2, *(uint *) arg);
 
857
    return memcmp(key1, key2, *(uint32_t *) arg);
860
858
}
861
859
 
862
860
 
863
861
static int item_sum_distinct_walk(void *element,
864
 
                                  element_count num_of_dups __attribute__((__unused__)),
 
862
                                  element_count num_of_dups __attribute__((unused)),
865
863
                                  void *item)
866
864
{
867
865
  return ((Item_sum_distinct*) (item))->unique_walk_function(element);
868
866
}
869
867
 
870
 
C_MODE_END
 
868
#ifdef __cplusplus
 
869
}
 
870
#endif
871
871
 
872
872
/* Item_sum_distinct */
873
873
 
936
936
  case STRING_RESULT:
937
937
  case REAL_RESULT:
938
938
    val.traits= Hybrid_type_traits::instance();
939
 
    table_field_type= MYSQL_TYPE_DOUBLE;
 
939
    table_field_type= DRIZZLE_TYPE_DOUBLE;
940
940
    break;
941
941
  case INT_RESULT:
942
942
  /*
946
946
    calculations. The range of int64 is enough to hold sum 2^32 distinct
947
947
    integers each <= 2^32.
948
948
  */
949
 
  if (table_field_type >= MYSQL_TYPE_TINY && table_field_type <= MYSQL_TYPE_LONG)
 
949
  if (table_field_type >= DRIZZLE_TYPE_TINY && table_field_type <= DRIZZLE_TYPE_LONG)
950
950
  {
951
951
    val.traits= Hybrid_type_traits_fast_decimal::instance();
952
952
    break;
953
953
  }
954
 
  table_field_type= MYSQL_TYPE_LONGLONG;
 
954
  table_field_type= DRIZZLE_TYPE_LONGLONG;
955
955
  /* fallthrough */
956
956
  case DECIMAL_RESULT:
957
957
    val.traits= Hybrid_type_traits_decimal::instance();
958
 
    if (table_field_type != MYSQL_TYPE_LONGLONG)
959
 
      table_field_type= MYSQL_TYPE_NEWDECIMAL;
 
958
    if (table_field_type != DRIZZLE_TYPE_LONGLONG)
 
959
      table_field_type= DRIZZLE_TYPE_NEWDECIMAL;
960
960
    break;
961
961
  case ROW_RESULT:
962
962
  default:
1131
1131
    AVG() will divide val by count. We need to reserve digits
1132
1132
    after decimal point as the result can be fractional.
1133
1133
  */
1134
 
  decimals= min(decimals + prec_increment, NOT_FIXED_DEC);
 
1134
  decimals= cmin(decimals + prec_increment, (unsigned int)NOT_FIXED_DEC);
1135
1135
}
1136
1136
 
1137
1137
 
1193
1193
  if (hybrid_type == DECIMAL_RESULT)
1194
1194
  {
1195
1195
    int precision= args[0]->decimal_precision() + prec_increment;
1196
 
    decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
 
1196
    decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1197
1197
    max_length= my_decimal_precision_to_length(precision, decimals,
1198
1198
                                               unsigned_flag);
1199
 
    f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
 
1199
    f_precision= cmin(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1200
1200
    f_scale=  args[0]->decimals;
1201
1201
    dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1202
1202
  }
1203
1203
  else {
1204
 
    decimals= min(args[0]->decimals + prec_increment, NOT_FIXED_DEC);
 
1204
    decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1205
1205
    max_length= args[0]->max_length + prec_increment;
1206
1206
  }
1207
1207
}
1213
1213
}
1214
1214
 
1215
1215
 
1216
 
Field *Item_sum_avg::create_tmp_field(bool group, TABLE *table,
1217
 
                                      uint convert_blob_len __attribute__((__unused__)))
 
1216
Field *Item_sum_avg::create_tmp_field(bool group, Table *table,
 
1217
                                      uint32_t convert_blob_len __attribute__((unused)))
1218
1218
{
1219
1219
  Field *field;
1220
1220
  if (group)
1224
1224
      The easiest way is to do this is to store both value in a string
1225
1225
      and unpack on access.
1226
1226
    */
1227
 
    field= new Field_string(((hybrid_type == DECIMAL_RESULT) ?
1228
 
                             dec_bin_size : sizeof(double)) + sizeof(int64_t),
1229
 
                            0, name, &my_charset_bin);
 
1227
    field= new Field_varstring(((hybrid_type == DECIMAL_RESULT) ?
 
1228
                                dec_bin_size : sizeof(double)) + sizeof(int64_t),
 
1229
                               0, name, table->s, &my_charset_bin);
1230
1230
  }
1231
1231
  else if (hybrid_type == DECIMAL_RESULT)
1232
1232
    field= new Field_new_decimal(max_length, maybe_null, name,
1392
1392
  switch (args[0]->result_type()) {
1393
1393
  case REAL_RESULT:
1394
1394
  case STRING_RESULT:
1395
 
    decimals= min(args[0]->decimals + 4, NOT_FIXED_DEC);
 
1395
    decimals= cmin(args[0]->decimals + 4, NOT_FIXED_DEC);
1396
1396
    break;
1397
1397
  case INT_RESULT:
1398
1398
  case DECIMAL_RESULT:
1399
1399
  {
1400
1400
    int precision= args[0]->decimal_precision()*2 + prec_increment;
1401
 
    decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
 
1401
    decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1402
1402
    max_length= my_decimal_precision_to_length(precision, decimals,
1403
1403
                                               unsigned_flag);
1404
1404
 
1423
1423
  If we're grouping, then we need some space to serialize variables into, to
1424
1424
  pass around.
1425
1425
*/
1426
 
Field *Item_sum_variance::create_tmp_field(bool group, TABLE *table,
1427
 
                                           uint convert_blob_len __attribute__((__unused__)))
 
1426
Field *Item_sum_variance::create_tmp_field(bool group, Table *table,
 
1427
                                           uint32_t convert_blob_len __attribute__((unused)))
1428
1428
{
1429
1429
  Field *field;
1430
1430
  if (group)
1434
1434
      The easiest way is to do this is to store both value in a string
1435
1435
      and unpack on access.
1436
1436
    */
1437
 
    field= new Field_string(sizeof(double)*2 + sizeof(int64_t), 0, name, &my_charset_bin);
 
1437
    field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, table->s, &my_charset_bin);
1438
1438
  }
1439
1439
  else
1440
1440
    field= new Field_double(max_length, maybe_null, name, decimals, true);
1499
1499
void Item_sum_variance::reset_field()
1500
1500
{
1501
1501
  double nr;
1502
 
  uchar *res= result_field->ptr;
 
1502
  unsigned char *res= result_field->ptr;
1503
1503
 
1504
1504
  nr= args[0]->val_real();              /* sets null_value as side-effect */
1505
1505
 
1506
1506
  if (args[0]->null_value)
1507
 
    bzero(res,sizeof(double)*2+sizeof(int64_t));
 
1507
    memset(res, 0, sizeof(double)*2+sizeof(int64_t));
1508
1508
  else
1509
1509
  {
1510
1510
    /* Serialize format is (double)m, (double)s, (int64_t)count */
1522
1522
void Item_sum_variance::update_field()
1523
1523
{
1524
1524
  uint64_t field_count;
1525
 
  uchar *res=result_field->ptr;
 
1525
  unsigned char *res=result_field->ptr;
1526
1526
 
1527
1527
  double nr= args[0]->val_real();       /* sets null_value as side-effect */
1528
1528
 
1579
1579
                             &end_not_used, &err_not_used) : 0.0);
1580
1580
  }
1581
1581
  case INT_RESULT:
1582
 
    if (unsigned_flag)
1583
 
      return uint64_t2double(sum_int);
1584
1582
    return (double) sum_int;
1585
1583
  case DECIMAL_RESULT:
1586
1584
    my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1885
1883
void Item_sum_num::reset_field()
1886
1884
{
1887
1885
  double nr= args[0]->val_real();
1888
 
  uchar *res=result_field->ptr;
 
1886
  unsigned char *res=result_field->ptr;
1889
1887
 
1890
1888
  if (maybe_null)
1891
1889
  {
2007
2005
 
2008
2006
void Item_sum_count::reset_field()
2009
2007
{
2010
 
  uchar *res=result_field->ptr;
 
2008
  unsigned char *res=result_field->ptr;
2011
2009
  int64_t nr=0;
2012
2010
 
2013
2011
  if (!args[0]->maybe_null || !args[0]->is_null())
2018
2016
 
2019
2017
void Item_sum_avg::reset_field()
2020
2018
{
2021
 
  uchar *res=result_field->ptr;
 
2019
  unsigned char *res=result_field->ptr;
2022
2020
  if (hybrid_type == DECIMAL_RESULT)
2023
2021
  {
2024
2022
    int64_t tmp;
2039
2037
    double nr= args[0]->val_real();
2040
2038
 
2041
2039
    if (args[0]->null_value)
2042
 
      bzero(res,sizeof(double)+sizeof(int64_t));
 
2040
      memset(res, 0, sizeof(double)+sizeof(int64_t));
2043
2041
    else
2044
2042
    {
2045
2043
      int64_t tmp= 1;
2059
2057
 
2060
2058
void Item_sum_bit::update_field()
2061
2059
{
2062
 
  uchar *res=result_field->ptr;
 
2060
  unsigned char *res=result_field->ptr;
2063
2061
  bits= uint8korr(res);
2064
2062
  add();
2065
2063
  int8store(res, bits);
2094
2092
  else
2095
2093
  {
2096
2094
    double old_nr,nr;
2097
 
    uchar *res=result_field->ptr;
 
2095
    unsigned char *res=result_field->ptr;
2098
2096
 
2099
2097
    float8get(old_nr,res);
2100
2098
    nr= args[0]->val_real();
2111
2109
void Item_sum_count::update_field()
2112
2110
{
2113
2111
  int64_t nr;
2114
 
  uchar *res=result_field->ptr;
 
2112
  unsigned char *res=result_field->ptr;
2115
2113
 
2116
2114
  nr=sint8korr(res);
2117
2115
  if (!args[0]->maybe_null || !args[0]->is_null())
2123
2121
void Item_sum_avg::update_field()
2124
2122
{
2125
2123
  int64_t field_count;
2126
 
  uchar *res=result_field->ptr;
 
2124
  unsigned char *res=result_field->ptr;
2127
2125
  if (hybrid_type == DECIMAL_RESULT)
2128
2126
  {
2129
2127
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2296
2294
  // fix_fields() never calls for this Item
2297
2295
  double nr;
2298
2296
  int64_t count;
2299
 
  uchar *res;
 
2297
  unsigned char *res;
2300
2298
 
2301
2299
  if (hybrid_type == DECIMAL_RESULT)
2302
2300
    return val_real_from_decimal();
2429
2427
** COUNT(DISTINCT ...)
2430
2428
****************************************************************************/
2431
2429
 
2432
 
int simple_str_key_cmp(void* arg, uchar* key1, uchar* key2)
 
2430
int simple_str_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2433
2431
{
2434
2432
  Field *f= (Field*) arg;
2435
2433
  return f->cmp(key1, key2);
2442
2440
  static
2443
2441
*/
2444
2442
 
2445
 
int composite_key_cmp(void* arg, uchar* key1, uchar* key2)
 
2443
int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2446
2444
{
2447
2445
  Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2448
2446
  Field **field    = item->table->field;
2449
2447
  Field **field_end= field + item->table->s->fields;
2450
 
  uint32 *lengths=item->field_lengths;
 
2448
  uint32_t *lengths=item->field_lengths;
2451
2449
  for (; field < field_end; ++field)
2452
2450
  {
2453
2451
    Field* f = *field;
2461
2459
  return 0;
2462
2460
}
2463
2461
 
2464
 
 
2465
 
C_MODE_START
2466
 
 
2467
 
static int count_distinct_walk(void *elem __attribute__((__unused__)),
2468
 
                               element_count count __attribute__((__unused__)),
 
2462
#ifdef __cplusplus
 
2463
extern "C" {
 
2464
#endif
 
2465
 
 
2466
static int count_distinct_walk(void *elem __attribute__((unused)),
 
2467
                               element_count count __attribute__((unused)),
2469
2468
                               void *arg)
2470
2469
{
2471
2470
  (*((uint64_t*)arg))++;
2472
2471
  return 0;
2473
2472
}
2474
2473
 
2475
 
C_MODE_END
 
2474
#ifdef __cplusplus
 
2475
}
 
2476
#endif
 
2477
 
2476
2478
 
2477
2479
 
2478
2480
void Item_sum_count_distinct::cleanup()
2492
2494
    is_evaluated= false;
2493
2495
    if (table)
2494
2496
    {
2495
 
      free_tmp_table(table->in_use, table);
 
2497
      table->free_tmp_table(table->in_use);
2496
2498
      table= 0;
2497
2499
    }
2498
2500
    delete tmp_table_param;
2543
2545
    return true;
2544
2546
 
2545
2547
  /* Create a table with an unique key over all parameters */
2546
 
  for (uint i=0; i < arg_count ; i++)
 
2548
  for (uint32_t i=0; i < arg_count ; i++)
2547
2549
  {
2548
2550
    Item *item=args[i];
2549
2551
    if (list.push_back(item))
2557
2559
  tmp_table_param->force_copy_fields= force_copy_fields;
2558
2560
  assert(table == 0);
2559
2561
 
2560
 
  if (!(table= create_tmp_table(thd, tmp_table_param, list, (ORDER*) 0, 1,
 
2562
  if (!(table= create_tmp_table(thd, tmp_table_param, list, (order_st*) 0, 1,
2561
2563
                                0,
2562
2564
                                (select_lex->options | thd->options),
2563
2565
                                HA_POS_ERROR, (char*)"")))
2582
2584
      Field *f= *field;
2583
2585
      enum enum_field_types f_type= f->type();
2584
2586
      tree_key_length+= f->pack_length();
2585
 
      if ((f_type == MYSQL_TYPE_VARCHAR) || (!f->binary() && (f_type == MYSQL_TYPE_STRING)))
 
2587
      if (f_type == DRIZZLE_TYPE_VARCHAR)
2586
2588
      {
2587
2589
        all_binary= false;
2588
2590
        break;
2609
2611
      }
2610
2612
      else
2611
2613
      {
2612
 
        uint32 *length;
 
2614
        uint32_t *length;
2613
2615
        compare_key= (qsort_cmp2) composite_key_cmp;
2614
2616
        cmp_arg= (void*) this;
2615
 
        field_lengths= (uint32*) thd->alloc(table->s->fields * sizeof(uint32));
 
2617
        field_lengths= (uint32_t*) thd->alloc(table->s->fields * sizeof(uint32_t));
2616
2618
        for (tree_key_length= 0, length= field_lengths, field= table->field;
2617
2619
             field < field_end; ++field, ++length)
2618
2620
        {
2695
2697
  int error;
2696
2698
  assert(fixed == 1);
2697
2699
  if (!table)                                   // Empty query
2698
 
    return 0LL;
 
2700
    return 0L;
2699
2701
  if (tree)
2700
2702
  {
2701
2703
    if (is_evaluated)
2719
2721
  return table->file->stats.records;
2720
2722
}
2721
2723
 
2722
 
 
2723
 
/****************************************************************************
2724
 
** Functions to handle dynamic loadable aggregates
2725
 
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2726
 
** Adapted for UDAs by: Andreas F. Bobak <bobak@relog.ch>.
2727
 
** Rewritten by: Monty.
2728
 
****************************************************************************/
2729
 
 
2730
 
#ifdef HAVE_DLOPEN
2731
 
 
2732
 
void Item_udf_sum::clear()
2733
 
{
2734
 
  udf.clear();
2735
 
  return;
2736
 
}
2737
 
 
2738
 
bool Item_udf_sum::add()
2739
 
{
2740
 
  udf.add(&null_value);
2741
 
  return(0);
2742
 
}
2743
 
 
2744
 
void Item_udf_sum::cleanup()
2745
 
{
2746
 
  /*
2747
 
    udf_handler::cleanup() nicely handles case when we have not
2748
 
    original item but one created by copy_or_same() method.
2749
 
  */
2750
 
  udf.cleanup();
2751
 
  Item_sum::cleanup();
2752
 
}
2753
 
 
2754
 
 
2755
 
void Item_udf_sum::print(String *str, enum_query_type query_type)
2756
 
{
2757
 
  str->append(func_name());
2758
 
  str->append('(');
2759
 
  for (uint i=0 ; i < arg_count ; i++)
2760
 
  {
2761
 
    if (i)
2762
 
      str->append(',');
2763
 
    args[i]->print(str, query_type);
2764
 
  }
2765
 
  str->append(')');
2766
 
}
2767
 
 
2768
 
 
2769
 
Item *Item_sum_udf_float::copy_or_same(THD* thd)
2770
 
{
2771
 
  return new (thd->mem_root) Item_sum_udf_float(thd, this);
2772
 
}
2773
 
 
2774
 
double Item_sum_udf_float::val_real()
2775
 
{
2776
 
  assert(fixed == 1);
2777
 
  return(udf.val(&null_value));
2778
 
}
2779
 
 
2780
 
 
2781
 
String *Item_sum_udf_float::val_str(String *str)
2782
 
{
2783
 
  return val_string_from_real(str);
2784
 
}
2785
 
 
2786
 
 
2787
 
my_decimal *Item_sum_udf_float::val_decimal(my_decimal *dec)
2788
 
{
2789
 
  return val_decimal_from_real(dec);
2790
 
}
2791
 
 
2792
 
 
2793
 
String *Item_sum_udf_decimal::val_str(String *str)
2794
 
{
2795
 
  return val_string_from_decimal(str);
2796
 
}
2797
 
 
2798
 
 
2799
 
double Item_sum_udf_decimal::val_real()
2800
 
{
2801
 
  return val_real_from_decimal();
2802
 
}
2803
 
 
2804
 
 
2805
 
int64_t Item_sum_udf_decimal::val_int()
2806
 
{
2807
 
  return val_int_from_decimal();
2808
 
}
2809
 
 
2810
 
 
2811
 
my_decimal *Item_sum_udf_decimal::val_decimal(my_decimal *dec_buf)
2812
 
{
2813
 
  assert(fixed == 1);
2814
 
  return(udf.val_decimal(&null_value, dec_buf));
2815
 
}
2816
 
 
2817
 
 
2818
 
Item *Item_sum_udf_decimal::copy_or_same(THD* thd)
2819
 
{
2820
 
  return new (thd->mem_root) Item_sum_udf_decimal(thd, this);
2821
 
}
2822
 
 
2823
 
 
2824
 
Item *Item_sum_udf_int::copy_or_same(THD* thd)
2825
 
{
2826
 
  return new (thd->mem_root) Item_sum_udf_int(thd, this);
2827
 
}
2828
 
 
2829
 
int64_t Item_sum_udf_int::val_int()
2830
 
{
2831
 
  assert(fixed == 1);
2832
 
  return(udf.val_int(&null_value));
2833
 
}
2834
 
 
2835
 
 
2836
 
String *Item_sum_udf_int::val_str(String *str)
2837
 
{
2838
 
  return val_string_from_int(str);
2839
 
}
2840
 
 
2841
 
my_decimal *Item_sum_udf_int::val_decimal(my_decimal *dec)
2842
 
{
2843
 
  return val_decimal_from_int(dec);
2844
 
}
2845
 
 
2846
 
 
2847
 
/** Default max_length is max argument length. */
2848
 
 
2849
 
void Item_sum_udf_str::fix_length_and_dec()
2850
 
{
2851
 
  max_length=0;
2852
 
  for (uint i = 0; i < arg_count; i++)
2853
 
    set_if_bigger(max_length,args[i]->max_length);
2854
 
  return;
2855
 
}
2856
 
 
2857
 
 
2858
 
Item *Item_sum_udf_str::copy_or_same(THD* thd)
2859
 
{
2860
 
  return new (thd->mem_root) Item_sum_udf_str(thd, this);
2861
 
}
2862
 
 
2863
 
 
2864
 
my_decimal *Item_sum_udf_str::val_decimal(my_decimal *dec)
2865
 
{
2866
 
  return val_decimal_from_string(dec);
2867
 
}
2868
 
 
2869
 
String *Item_sum_udf_str::val_str(String *str)
2870
 
{
2871
 
  assert(fixed == 1);
2872
 
  String *res=udf.val_str(str,&str_value);
2873
 
  null_value = !res;
2874
 
  return(res);
2875
 
}
2876
 
 
2877
 
#endif /* HAVE_DLOPEN */
2878
 
 
2879
 
 
2880
2724
/*****************************************************************************
2881
2725
 GROUP_CONCAT function
2882
2726
 
2883
2727
 SQL SYNTAX:
2884
 
  GROUP_CONCAT([DISTINCT] expr,... [ORDER BY col [ASC|DESC],...]
 
2728
  GROUP_CONCAT([DISTINCT] expr,... [order_st BY col [ASC|DESC],...]
2885
2729
    [SEPARATOR str_const])
2886
2730
 
2887
2731
 concat of values from "group by" operation
2888
2732
 
2889
2733
 BUGS
2890
 
   Blobs doesn't work with DISTINCT or ORDER BY
 
2734
   Blobs doesn't work with DISTINCT or order_st BY
2891
2735
*****************************************************************************/
2892
2736
 
2893
2737
 
2896
2740
  @note
2897
2741
       
2898
2742
     GROUP_CONCAT([DISTINCT] expr [,expr ...]
2899
 
              [ORDER BY {unsigned_integer | col_name | expr}
 
2743
              [order_st BY {unsigned_integer | col_name | expr}
2900
2744
                  [ASC | DESC] [,col_name ...]]
2901
2745
              [SEPARATOR str_val])
2902
2746
 
2910
2754
                                       const void* key2)
2911
2755
{
2912
2756
  Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
2913
 
  TABLE *table= item_func->table;
 
2757
  Table *table= item_func->table;
2914
2758
 
2915
 
  for (uint i= 0; i < item_func->arg_count_field; i++)
 
2759
  for (uint32_t i= 0; i < item_func->arg_count_field; i++)
2916
2760
  {
2917
2761
    Item *item= item_func->args[i];
2918
2762
    /* 
2928
2772
    */
2929
2773
    Field *field= item->get_tmp_table_field();
2930
2774
    int res;
2931
 
    uint offset= field->offset(field->table->record[0])-table->s->null_bytes;
2932
 
    if((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset)))
 
2775
    uint32_t offset= field->offset(field->table->record[0])-table->s->null_bytes;
 
2776
    if((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2933
2777
      return res;
2934
2778
  }
2935
2779
  return 0;
2937
2781
 
2938
2782
 
2939
2783
/**
2940
 
  function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
 
2784
  function of sort for syntax: GROUP_CONCAT(expr,... order_st BY col,... )
2941
2785
*/
2942
2786
 
2943
2787
int group_concat_key_cmp_with_order(void* arg, const void* key1, 
2944
2788
                                    const void* key2)
2945
2789
{
2946
2790
  Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2947
 
  ORDER **order_item, **end;
2948
 
  TABLE *table= grp_item->table;
 
2791
  order_st **order_item, **end;
 
2792
  Table *table= grp_item->table;
2949
2793
 
2950
2794
  for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2951
2795
       order_item < end;
2965
2809
    if (field && !item->const_item())
2966
2810
    {
2967
2811
      int res;
2968
 
      uint offset= (field->offset(field->table->record[0]) -
 
2812
      uint32_t offset= (field->offset(field->table->record[0]) -
2969
2813
                    table->s->null_bytes);
2970
 
      if ((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset)))
 
2814
      if ((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2971
2815
        return (*order_item)->asc ? res : -res;
2972
2816
    }
2973
2817
  }
2984
2828
  Append data from current leaf to item->result.
2985
2829
*/
2986
2830
 
2987
 
int dump_leaf_key(uchar* key, element_count count __attribute__((unused)),
 
2831
int dump_leaf_key(unsigned char* key, element_count count __attribute__((unused)),
2988
2832
                  Item_func_group_concat *item)
2989
2833
{
2990
 
  TABLE *table= item->table;
 
2834
  Table *table= item->table;
2991
2835
  String tmp((char *)table->record[1], table->s->reclength,
2992
2836
             default_charset_info);
2993
2837
  String tmp2;
2994
2838
  String *result= &item->result;
2995
2839
  Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
2996
 
  uint old_length= result->length();
 
2840
  uint32_t old_length= result->length();
2997
2841
 
2998
2842
  if (item->no_appended)
2999
2843
    item->no_appended= false;
3015
2859
        because it contains both order and arg list fields.
3016
2860
      */
3017
2861
      Field *field= (*arg)->get_tmp_table_field();
3018
 
      uint offset= (field->offset(field->table->record[0]) -
 
2862
      uint32_t offset= (field->offset(field->table->record[0]) -
3019
2863
                    table->s->null_bytes);
3020
2864
      assert(offset < table->s->reclength);
3021
2865
      res= field->val_str(&tmp, key + offset);
3030
2874
  if (result->length() > item->max_length)
3031
2875
  {
3032
2876
    int well_formed_error;
3033
 
    CHARSET_INFO *cs= item->collation.collation;
 
2877
    const CHARSET_INFO * const cs= item->collation.collation;
3034
2878
    const char *ptr= result->ptr();
3035
 
    uint add_length;
 
2879
    uint32_t add_length;
3036
2880
    /*
3037
2881
      It's ok to use item->result.length() as the fourth argument
3038
2882
      as this is never used to limit the length of the data.
3088
2932
    order - arg_count_order
3089
2933
  */
3090
2934
  if (!(args= (Item**) sql_alloc(sizeof(Item*) * arg_count +
3091
 
                                 sizeof(ORDER*)*arg_count_order)))
 
2935
                                 sizeof(order_st*)*arg_count_order)))
3092
2936
    return;
3093
2937
 
3094
 
  order= (ORDER**)(args + arg_count);
 
2938
  order= (order_st**)(args + arg_count);
3095
2939
 
3096
2940
  /* fill args items of show and sort */
3097
2941
  List_iterator_fast<Item> li(*select_list);
3101
2945
 
3102
2946
  if (arg_count_order)
3103
2947
  {
3104
 
    ORDER **order_ptr= order;
3105
 
    for (ORDER *order_item= (ORDER*) order_list->first;
 
2948
    order_st **order_ptr= order;
 
2949
    for (order_st *order_item= (order_st*) order_list->first;
3106
2950
         order_item != NULL;
3107
2951
         order_item= order_item->next)
3108
2952
    {
3147
2991
  /* Adjust warning message to include total number of cut values */
3148
2992
  if (warning)
3149
2993
  {
3150
 
    char warn_buff[MYSQL_ERRMSG_SIZE];
 
2994
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
3151
2995
    sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3152
2996
    warning->set_msg(current_thd, warn_buff);
3153
2997
    warning= 0;
3164
3008
    if (table)
3165
3009
    {
3166
3010
      THD *thd= table->in_use;
3167
 
      free_tmp_table(thd, table);
 
3011
      table->free_tmp_table(thd);
3168
3012
      table= 0;
3169
3013
      if (tree)
3170
3014
      {
3178
3022
      }
3179
3023
      if (warning)
3180
3024
      {
3181
 
        char warn_buff[MYSQL_ERRMSG_SIZE];
 
3025
        char warn_buff[DRIZZLE_ERRMSG_SIZE];
3182
3026
        sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3183
3027
        warning->set_msg(thd, warn_buff);
3184
3028
        warning= 0;
3218
3062
  copy_fields(tmp_table_param);
3219
3063
  copy_funcs(tmp_table_param->items_to_copy);
3220
3064
 
3221
 
  for (uint i= 0; i < arg_count_field; i++)
 
3065
  for (uint32_t i= 0; i < arg_count_field; i++)
3222
3066
  {
3223
3067
    Item *show_item= args[i];
3224
3068
    if (!show_item->const_item())
3225
3069
    {
3226
3070
      Field *f= show_item->get_tmp_table_field();
3227
 
      if (f->is_null_in_record((const uchar*) table->record[0]))
 
3071
      if (f->is_null_in_record((const unsigned char*) table->record[0]))
3228
3072
        return 0;                               // Skip row if it contains null
3229
3073
    }
3230
3074
  }
3235
3079
  if (distinct) 
3236
3080
  {
3237
3081
    /* Filter out duplicate rows. */
3238
 
    uint count= unique_filter->elements_in_tree();
 
3082
    uint32_t count= unique_filter->elements_in_tree();
3239
3083
    unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3240
3084
    if (count == unique_filter->elements_in_tree())
3241
3085
      row_eligible= false;
3261
3105
bool
3262
3106
Item_func_group_concat::fix_fields(THD *thd, Item **ref)
3263
3107
{
3264
 
  uint i;                       /* for loop variable */
 
3108
  uint32_t i;                       /* for loop variable */
3265
3109
  assert(fixed == 0);
3266
3110
 
3267
3111
  if (init_sum_func_check(thd))
3270
3114
  maybe_null= 1;
3271
3115
 
3272
3116
  /*
3273
 
    Fix fields for select list and ORDER clause
 
3117
    Fix fields for select list and order_st clause
3274
3118
  */
3275
3119
 
3276
3120
  for (i=0 ; i < arg_count ; i++)
3293
3137
  null_value= 1;
3294
3138
  max_length= thd->variables.group_concat_max_len;
3295
3139
 
3296
 
  uint32 offset;
 
3140
  uint32_t offset;
3297
3141
  if (separator->needs_conversion(separator->length(), separator->charset(),
3298
3142
                                  collation.collation, &offset))
3299
3143
  {
3300
 
    uint32 buflen= collation.collation->mbmaxlen * separator->length();
3301
 
    uint errors, conv_length;
 
3144
    uint32_t buflen= collation.collation->mbmaxlen * separator->length();
 
3145
    uint32_t errors, conv_length;
3302
3146
    char *buf;
3303
3147
    String *new_separator;
3304
3148
 
3305
 
    if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
3306
 
        !(new_separator= new(thd->stmt_arena->mem_root)
 
3149
    if (!(buf= (char*) thd->alloc(buflen)) ||
 
3150
        !(new_separator= new(thd->mem_root)
3307
3151
                           String(buf, buflen, collation.collation)))
3308
3152
      return true;
3309
3153
    
3342
3186
                                        collation.collation->mbmaxlen;
3343
3187
  /* Push all not constant fields to the list and create a temp table */
3344
3188
  always_null= 0;
3345
 
  for (uint i= 0; i < arg_count_field; i++)
 
3189
  for (uint32_t i= 0; i < arg_count_field; i++)
3346
3190
  {
3347
3191
    Item *item= args[i];
3348
3192
    if (list.push_back(item))
3359
3203
 
3360
3204
  List<Item> all_fields(list);
3361
3205
  /*
3362
 
    Try to find every ORDER expression in the list of GROUP_CONCAT
 
3206
    Try to find every order_st expression in the list of GROUP_CONCAT
3363
3207
    arguments. If an expression is not found, prepend it to
3364
3208
    "all_fields". The resulting field list is used as input to create
3365
3209
    tmp table columns.
3375
3219
  {
3376
3220
    /*
3377
3221
      Currently we have to force conversion of BLOB values to VARCHAR's
3378
 
      if we are to store them in TREE objects used for ORDER BY and
 
3222
      if we are to store them in TREE objects used for order_st BY and
3379
3223
      DISTINCT. This leads to truncation if the BLOB's size exceeds
3380
3224
      Field_varstring::MAX_SIZE.
3381
3225
    */
3387
3231
    We have to create a temporary table to get descriptions of fields
3388
3232
    (types, sizes and so on).
3389
3233
 
3390
 
    Note that in the table, we first have the ORDER BY fields, then the
 
3234
    Note that in the table, we first have the order_st BY fields, then the
3391
3235
    field list.
3392
3236
  */
3393
3237
  if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
3394
 
                                (ORDER*) 0, 0, true,
 
3238
                                (order_st*) 0, 0, true,
3395
3239
                                (select_lex->options | thd->options),
3396
3240
                                HA_POS_ERROR, (char*) "")))
3397
3241
    return(true);
3403
3247
     Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3404
3248
     the row is not added to the result.
3405
3249
  */
3406
 
  uint tree_key_length= table->s->reclength - table->s->null_bytes;
 
3250
  uint32_t tree_key_length= table->s->reclength - table->s->null_bytes;
3407
3251
 
3408
3252
  if (arg_count_order)
3409
3253
  {
3410
3254
    tree= &tree_base;
3411
3255
    /*
3412
3256
      Create a tree for sorting. The tree is used to sort (according to the
3413
 
      syntax of this function). If there is no ORDER BY clause, we don't
 
3257
      syntax of this function). If there is no order_st BY clause, we don't
3414
3258
      create this tree.
3415
3259
    */
3416
 
    init_tree(tree, (uint) min(thd->variables.max_heap_table_size,
 
3260
    init_tree(tree, (uint) cmin(thd->variables.max_heap_table_size,
3417
3261
                               thd->variables.sortbuff_size/16), 0,
3418
3262
              tree_key_length, 
3419
3263
              group_concat_key_cmp_with_order , 0, NULL, (void*) this);
3441
3285
}
3442
3286
 
3443
3287
 
3444
 
String* Item_func_group_concat::val_str(String* str __attribute__((__unused__)))
 
3288
String* Item_func_group_concat::val_str(String* str __attribute__((unused)))
3445
3289
{
3446
3290
  assert(fixed == 1);
3447
3291
  if (null_value)
3457
3301
      Item_func_group_concat::cleanup().
3458
3302
    */
3459
3303
    assert(table);
3460
 
    warning= push_warning(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
 
3304
    warning= push_warning(table->in_use, DRIZZLE_ERROR::WARN_LEVEL_WARN,
3461
3305
                          ER_CUT_VALUE_GROUP_CONCAT,
3462
3306
                          ER(ER_CUT_VALUE_GROUP_CONCAT));
3463
3307
  }
3470
3314
  str->append(STRING_WITH_LEN("group_concat("));
3471
3315
  if (distinct)
3472
3316
    str->append(STRING_WITH_LEN("distinct "));
3473
 
  for (uint i= 0; i < arg_count_field; i++)
 
3317
  for (uint32_t i= 0; i < arg_count_field; i++)
3474
3318
  {
3475
3319
    if (i)
3476
3320
      str->append(',');
3479
3323
  if (arg_count_order)
3480
3324
  {
3481
3325
    str->append(STRING_WITH_LEN(" order by "));
3482
 
    for (uint i= 0 ; i < arg_count_order ; i++)
 
3326
    for (uint32_t i= 0 ; i < arg_count_order ; i++)
3483
3327
    {
3484
3328
      if (i)
3485
3329
        str->append(',');