~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/item_sum.cc

  • Committer: Brian Aker
  • Date: 2008-07-14 16:24:25 UTC
  • Revision ID: brian@tangent.org-20080714162425-juw3vw221gs9kysh
Cleanup around intptr_t

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