~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/sum.cc

  • Committer: Brian Aker
  • Date: 2009-01-09 22:07:54 UTC
  • Revision ID: brian@tangent.org-20090109220754-1y50h7lqi9i1ifcs
Dead test/wrong test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
/**
20
20
  @brief
21
21
  Sum functions (COUNT, MIN...)
22
22
*/
23
 
#include "config.h"
24
 
#include <cstdio>
25
 
#include <math.h>
 
23
#include <drizzled/server_includes.h>
26
24
#include <drizzled/sql_select.h>
27
25
#include <drizzled/error.h>
28
26
#include <drizzled/hybrid_type_traits.h>
33
31
#include <drizzled/item/sum.h>
34
32
#include <drizzled/field/decimal.h>
35
33
#include <drizzled/field/double.h>
36
 
#include <drizzled/field/int64.h>
 
34
#include <drizzled/field/int64_t.h>
37
35
#include <drizzled/field/date.h>
 
36
#include <drizzled/field/timetype.h>
38
37
#include <drizzled/field/datetime.h>
39
38
 
40
 
#include "drizzled/internal/m_string.h"
41
 
 
42
 
#include <algorithm>
43
 
 
44
 
using namespace std;
45
 
 
46
 
namespace drizzled
47
 
{
 
39
#include CMATH_H
 
40
 
 
41
#if defined(CMATH_NAMESPACE)
 
42
using namespace CMATH_NAMESPACE;
 
43
#endif
48
44
 
49
45
extern my_decimal decimal_zero;
50
 
extern plugin::StorageEngine *heap_engine;
51
46
 
52
47
/**
53
48
  Prepare an aggregate function item for checking context conditions.
105
100
    If the context conditions are not met the method reports an error.
106
101
    If the set function is aggregated in some outer subquery the method
107
102
    adds it to the chain of items for such set functions that is attached
108
 
    to the the Select_Lex structure for this subquery.
 
103
    to the the st_select_lex structure for this subquery.
109
104
 
110
105
    A number of designated members of the object are used to check the
111
106
    conditions. They are specified in the comment before the Item_sum
256
251
    List_iterator<Item_field> of(outer_fields);
257
252
    while ((field= of++))
258
253
    {
259
 
      Select_Lex *sel= field->cached_table->select_lex;
 
254
      SELECT_LEX *sel= field->cached_table->select_lex;
260
255
      if (sel->nest_level < aggr_level)
261
256
      {
262
257
        if (in_sum_func)
268
263
          in_sum_func->outer_fields.push_back(field);
269
264
        }
270
265
        else
271
 
        {
272
 
          sel->full_group_by_flag.set(NON_AGG_FIELD_USED);
273
 
        }
 
266
          sel->full_group_by_flag|= NON_AGG_FIELD_USED;
274
267
      }
275
268
      if (sel->nest_level > aggr_level &&
276
 
          (sel->full_group_by_flag.test(SUM_FUNC_USED)) &&
277
 
          ! sel->group_list.elements)
 
269
          (sel->full_group_by_flag & SUM_FUNC_USED) &&
 
270
          !sel->group_list.elements)
278
271
      {
279
272
        my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
280
273
                   ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
282
275
      }
283
276
    }
284
277
  }
285
 
  aggr_sel->full_group_by_flag.set(SUM_FUNC_USED);
 
278
  aggr_sel->full_group_by_flag|= SUM_FUNC_USED;
286
279
  update_used_tables();
287
280
  session->lex->in_sum_func= in_sum_func;
288
281
  return false;
315
308
 
316
309
bool Item_sum::register_sum_func(Session *session, Item **ref)
317
310
{
318
 
  Select_Lex *sl;
 
311
  SELECT_LEX *sl;
319
312
  nesting_map allow_sum_func= session->lex->allow_sum_func;
320
313
  for (sl= session->lex->current_select->master_unit()->outer_select() ;
321
314
       sl && sl->nest_level > max_arg_level;
357
350
      Mark Item_subselect(s) as containing aggregate function all the way up
358
351
      to aggregate function's calculation context.
359
352
      Note that we must not mark the Item of calculation context itself
360
 
      because with_sum_func on the calculation context Select_Lex is
 
353
      because with_sum_func on the calculation context st_select_lex is
361
354
      already set above.
362
355
 
363
356
      with_sum_func being set for an Item means that this Item refers
365
358
      or through intermediate items to an aggregate function that is calculated
366
359
      in a context "outside" of the Item (e.g. in the current or outer select).
367
360
 
368
 
      with_sum_func being set for an Select_Lex means that this Select_Lex
 
361
      with_sum_func being set for an st_select_lex means that this st_select_lex
369
362
      has aggregate functions directly referenced (i.e. not through a sub-select).
370
363
    */
371
364
    for (sl= session->lex->current_select;
381
374
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements),
382
375
  forced_const(false)
383
376
{
384
 
  if ((args=(Item**) memory::sql_alloc(sizeof(Item*)*arg_count)))
 
377
  if ((args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
385
378
  {
386
379
    uint32_t i=0;
387
380
    List_iterator_fast<Item> li(list);
419
412
 
420
413
void Item_sum::mark_as_sum_func()
421
414
{
422
 
  Select_Lex *cur_select= current_session->lex->current_select;
 
415
  SELECT_LEX *cur_select= current_session->lex->current_select;
423
416
  cur_select->n_sum_items++;
424
417
  cur_select->with_sum_func= 1;
425
418
  with_sum_func= 1;
426
419
}
427
420
 
428
421
 
429
 
void Item_sum::make_field(SendField *tmp_field)
 
422
void Item_sum::make_field(Send_field *tmp_field)
430
423
{
431
424
  if (args[0]->type() == Item::FIELD_ITEM && keep_field_type())
432
425
  {
504
497
}
505
498
 
506
499
 
507
 
Field *Item_sum::create_tmp_field(bool ,
 
500
Field *Item_sum::create_tmp_field(bool group __attribute__((unused)),
508
501
                                  Table *table,
509
502
                                  uint32_t convert_blob_length)
510
503
{
511
 
  Field *field= NULL;
512
 
 
 
504
  Field *field;
513
505
  switch (result_type()) {
514
506
  case REAL_RESULT:
515
507
    field= new Field_double(max_length, maybe_null, name, decimals, true);
516
508
    break;
517
 
 
518
509
  case INT_RESULT:
519
 
    field= new field::Int64(max_length, maybe_null, name, unsigned_flag);
 
510
    field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
520
511
    break;
521
 
 
522
512
  case STRING_RESULT:
523
513
    if (max_length/collation.collation->mbmaxlen <= 255 ||
524
514
        convert_blob_length > Field_varstring::MAX_SIZE ||
525
515
        !convert_blob_length)
526
 
    {
527
516
      return make_string_field(table);
528
 
    }
529
 
 
530
 
    table->setVariableWidth();
531
517
    field= new Field_varstring(convert_blob_length, maybe_null,
532
 
                               name, collation.collation);
 
518
                               name, table->s, collation.collation);
533
519
    break;
534
 
 
535
520
  case DECIMAL_RESULT:
536
 
    field= new Field_decimal(max_length, maybe_null, name,
537
 
                             decimals, unsigned_flag);
 
521
    field= new Field_new_decimal(max_length, maybe_null, name,
 
522
                                 decimals, unsigned_flag);
538
523
    break;
539
 
 
540
524
  case ROW_RESULT:
 
525
  default:
541
526
    // This case should never be choosen
542
527
    assert(0);
543
528
    return 0;
544
529
  }
545
 
 
546
530
  if (field)
547
531
    field->init(table);
548
 
 
549
532
  return field;
550
533
}
551
534
 
655
638
    */
656
639
    break;
657
640
  case ROW_RESULT:
 
641
  default:
658
642
    assert(0);
659
643
  }
660
644
  collation.set(item->collation);
693
677
    max_length= item->max_length;
694
678
    break;
695
679
  case ROW_RESULT:
 
680
  default:
696
681
    assert(0);
697
682
  };
698
683
  /* MIN/MAX can return NULL for empty set indepedent of the used column */
737
722
  case DRIZZLE_TYPE_DATE:
738
723
    field= new Field_date(maybe_null, name, collation.collation);
739
724
    break;
 
725
  case DRIZZLE_TYPE_TIME:
 
726
    field= new Field_time(maybe_null, name, collation.collation);
 
727
    break;
740
728
  case DRIZZLE_TYPE_TIMESTAMP:
741
729
  case DRIZZLE_TYPE_DATETIME:
742
730
    field= new Field_datetime(maybe_null, name, collation.collation);
804
792
    break;
805
793
  case INT_RESULT:
806
794
  case DECIMAL_RESULT:
807
 
    {
808
 
      /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
809
 
      int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
810
 
      max_length= my_decimal_precision_to_length(precision, decimals,
811
 
                                                 unsigned_flag);
812
 
      curr_dec_buff= 0;
813
 
      hybrid_type= DECIMAL_RESULT;
814
 
      my_decimal_set_zero(dec_buffs);
815
 
      break;
816
 
    }
 
795
  {
 
796
    /* SUM result can't be longer than length(arg) + length(MAX_ROWS) */
 
797
    int precision= args[0]->decimal_precision() + DECIMAL_LONGLONG_DIGITS;
 
798
    max_length= my_decimal_precision_to_length(precision, decimals,
 
799
                                               unsigned_flag);
 
800
    curr_dec_buff= 0;
 
801
    hybrid_type= DECIMAL_RESULT;
 
802
    my_decimal_set_zero(dec_buffs);
 
803
    break;
 
804
  }
817
805
  case ROW_RESULT:
 
806
  default:
818
807
    assert(0);
819
808
  }
 
809
  return;
820
810
}
821
811
 
822
812
 
883
873
 
884
874
/***************************************************************************/
885
875
 
 
876
#ifdef __cplusplus
 
877
extern "C" {
 
878
#endif
 
879
 
886
880
/* Declarations for auxilary C-callbacks */
887
881
 
888
882
static int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
892
886
 
893
887
 
894
888
static int item_sum_distinct_walk(void *element,
895
 
                                  uint32_t ,
 
889
                                  element_count num_of_dups __attribute__((unused)),
896
890
                                  void *item)
897
891
{
898
892
  return ((Item_sum_distinct*) (item))->unique_walk_function(element);
899
893
}
900
894
 
 
895
#ifdef __cplusplus
 
896
}
 
897
#endif
 
898
 
901
899
/* Item_sum_distinct */
902
900
 
903
901
Item_sum_distinct::Item_sum_distinct(Item *item_arg)
959
957
{
960
958
  assert(args[0]->fixed);
961
959
 
962
 
  null_value= maybe_null= true;
963
960
  table_field_type= args[0]->field_type();
964
961
 
965
962
  /* Adjust tmp table type according to the chosen aggregation type */
970
967
    table_field_type= DRIZZLE_TYPE_DOUBLE;
971
968
    break;
972
969
  case INT_RESULT:
973
 
    /*
974
 
      Preserving int8, int16, int32 field types gives ~10% performance boost
975
 
      as the size of result tree becomes significantly smaller.
976
 
      Another speed up we gain by using int64_t for intermediate
977
 
      calculations. The range of int64 is enough to hold sum 2^32 distinct
978
 
      integers each <= 2^32.
979
 
    */
980
 
    if (table_field_type == DRIZZLE_TYPE_LONG)
981
 
    {
982
 
      val.traits= Hybrid_type_traits_fast_decimal::instance();
983
 
      break;
984
 
    }
985
 
    table_field_type= DRIZZLE_TYPE_LONGLONG;
986
 
    /* fallthrough */
 
970
  /*
 
971
    Preserving int8, int16, int32 field types gives ~10% performance boost
 
972
    as the size of result tree becomes significantly smaller.
 
973
    Another speed up we gain by using int64_t for intermediate
 
974
    calculations. The range of int64 is enough to hold sum 2^32 distinct
 
975
    integers each <= 2^32.
 
976
  */
 
977
  if (table_field_type == DRIZZLE_TYPE_LONG)
 
978
  {
 
979
    val.traits= Hybrid_type_traits_fast_decimal::instance();
 
980
    break;
 
981
  }
 
982
  table_field_type= DRIZZLE_TYPE_LONGLONG;
 
983
  /* fallthrough */
987
984
  case DECIMAL_RESULT:
988
985
    val.traits= Hybrid_type_traits_decimal::instance();
989
986
    if (table_field_type != DRIZZLE_TYPE_LONGLONG)
990
 
      table_field_type= DRIZZLE_TYPE_DECIMAL;
 
987
      table_field_type= DRIZZLE_TYPE_NEWDECIMAL;
991
988
    break;
992
989
  case ROW_RESULT:
 
990
  default:
993
991
    assert(0);
994
992
  }
995
 
 
996
993
  val.traits->fix_length_and_dec(this, args[0]);
997
994
}
998
995
 
1009
1006
*/
1010
1007
bool Item_sum_distinct::setup(Session *session)
1011
1008
{
1012
 
  List<CreateField> field_list;
1013
 
  CreateField field_def;                              /* field definition */
 
1009
  List<Create_field> field_list;
 
1010
  Create_field field_def;                              /* field definition */
1014
1011
  /* It's legal to call setup() more than once when in a subquery */
1015
1012
  if (tree)
1016
1013
    return(false);
1029
1026
  assert(args[0]->fixed);
1030
1027
 
1031
1028
  field_def.init_for_tmp_table(table_field_type, args[0]->max_length,
1032
 
                               args[0]->decimals, args[0]->maybe_null);
 
1029
                               args[0]->decimals, args[0]->maybe_null,
 
1030
                               args[0]->unsigned_flag);
1033
1031
 
1034
 
  if (! (table= session->getInstanceTable(field_list)))
 
1032
  if (! (table= create_virtual_tmp_table(session, field_list)))
1035
1033
    return(true);
1036
1034
 
1037
1035
  /* XXX: check that the case of CHAR(0) works OK */
1038
 
  tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
 
1036
  tree_key_length= table->s->reclength - table->s->null_bytes;
1039
1037
 
1040
1038
  /*
1041
1039
    Unique handles all unique elements in a tree until they can't fit
1043
1041
    simple_raw_key_cmp because the table contains numbers only; decimals
1044
1042
    are converted to binary representation as well.
1045
1043
  */
1046
 
  tree= new Unique(simple_raw_key_cmp, &tree_key_length,
1047
 
                   tree_key_length,
1048
 
                   (size_t)session->variables.max_heap_table_size);
 
1044
  tree= new Unique(simple_raw_key_cmp, &tree_key_length, tree_key_length,
 
1045
                   session->variables.max_heap_table_size);
1049
1046
 
1050
1047
  is_evaluated= false;
1051
1048
  return(tree == 0);
1054
1051
 
1055
1052
bool Item_sum_distinct::add()
1056
1053
{
1057
 
  args[0]->save_in_field(table->getField(0), false);
 
1054
  args[0]->save_in_field(table->field[0], false);
1058
1055
  is_evaluated= false;
1059
 
  if (!table->getField(0)->is_null())
 
1056
  if (!table->field[0]->is_null())
1060
1057
  {
1061
1058
    assert(tree);
1062
1059
    null_value= 0;
1064
1061
      '0' values are also stored in the tree. This doesn't matter
1065
1062
      for SUM(DISTINCT), but is important for AVG(DISTINCT)
1066
1063
    */
1067
 
    return tree->unique_add(table->getField(0)->ptr);
 
1064
    return tree->unique_add(table->field[0]->ptr);
1068
1065
  }
1069
1066
  return 0;
1070
1067
}
1072
1069
 
1073
1070
bool Item_sum_distinct::unique_walk_function(void *element)
1074
1071
{
1075
 
  memcpy(table->getField(0)->ptr, element, tree_key_length);
 
1072
  memcpy(table->field[0]->ptr, element, tree_key_length);
1076
1073
  ++count;
1077
 
  val.traits->add(&val, table->getField(0));
 
1074
  val.traits->add(&val, table->field[0]);
1078
1075
  return 0;
1079
1076
}
1080
1077
 
1116
1113
     */
1117
1114
    if (tree)
1118
1115
    {
1119
 
      table->getField(0)->set_notnull();
 
1116
      table->field[0]->set_notnull();
1120
1117
      tree->walk(item_sum_distinct_walk, (void*) this);
1121
1118
    }
1122
1119
    is_evaluated= true;
1168
1165
    AVG() will divide val by count. We need to reserve digits
1169
1166
    after decimal point as the result can be fractional.
1170
1167
  */
1171
 
  decimals= min(decimals + prec_increment, (unsigned int)NOT_FIXED_DEC);
 
1168
  decimals= cmin(decimals + prec_increment, (unsigned int)NOT_FIXED_DEC);
1172
1169
}
1173
1170
 
1174
1171
 
1230
1227
  if (hybrid_type == DECIMAL_RESULT)
1231
1228
  {
1232
1229
    int precision= args[0]->decimal_precision() + prec_increment;
1233
 
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
 
1230
    decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1234
1231
    max_length= my_decimal_precision_to_length(precision, decimals,
1235
1232
                                               unsigned_flag);
1236
 
    f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
 
1233
    f_precision= cmin(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1237
1234
    f_scale=  args[0]->decimals;
1238
1235
    dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1239
1236
  }
1240
1237
  else {
1241
 
    decimals= min(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
 
1238
    decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
1242
1239
    max_length= args[0]->max_length + prec_increment;
1243
1240
  }
1244
1241
}
1251
1248
 
1252
1249
 
1253
1250
Field *Item_sum_avg::create_tmp_field(bool group, Table *table,
1254
 
                                      uint32_t )
 
1251
                                      uint32_t convert_blob_len __attribute__((unused)))
1255
1252
{
1256
1253
  Field *field;
1257
1254
  if (group)
1261
1258
      The easiest way is to do this is to store both value in a string
1262
1259
      and unpack on access.
1263
1260
    */
1264
 
    table->setVariableWidth();
1265
1261
    field= new Field_varstring(((hybrid_type == DECIMAL_RESULT) ?
1266
1262
                                dec_bin_size : sizeof(double)) + sizeof(int64_t),
1267
 
                               0, name, &my_charset_bin);
 
1263
                               0, name, table->s, &my_charset_bin);
1268
1264
  }
1269
1265
  else if (hybrid_type == DECIMAL_RESULT)
1270
 
    field= new Field_decimal(max_length, maybe_null, name,
1271
 
                             decimals, unsigned_flag);
 
1266
    field= new Field_new_decimal(max_length, maybe_null, name,
 
1267
                                 decimals, unsigned_flag);
1272
1268
  else
1273
1269
    field= new Field_double(max_length, maybe_null, name, decimals, true);
1274
1270
  if (field)
1436
1432
  switch (args[0]->result_type()) {
1437
1433
  case REAL_RESULT:
1438
1434
  case STRING_RESULT:
1439
 
    decimals= min(args[0]->decimals + 4, (int)NOT_FIXED_DEC);
 
1435
    decimals= cmin(args[0]->decimals + 4, NOT_FIXED_DEC);
1440
1436
    break;
1441
1437
  case INT_RESULT:
1442
1438
  case DECIMAL_RESULT:
1443
 
    {
1444
 
      int precision= args[0]->decimal_precision()*2 + prec_increment;
1445
 
      decimals= min(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
1446
 
      max_length= my_decimal_precision_to_length(precision, decimals,
1447
 
                                                 unsigned_flag);
 
1439
  {
 
1440
    int precision= args[0]->decimal_precision()*2 + prec_increment;
 
1441
    decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
 
1442
    max_length= my_decimal_precision_to_length(precision, decimals,
 
1443
                                               unsigned_flag);
1448
1444
 
1449
 
      break;
1450
 
    }
 
1445
    break;
 
1446
  }
1451
1447
  case ROW_RESULT:
 
1448
  default:
1452
1449
    assert(0);
1453
1450
  }
 
1451
  return;
1454
1452
}
1455
1453
 
1456
1454
 
1466
1464
  pass around.
1467
1465
*/
1468
1466
Field *Item_sum_variance::create_tmp_field(bool group, Table *table,
1469
 
                                           uint32_t )
 
1467
                                           uint32_t convert_blob_len __attribute__((unused)))
1470
1468
{
1471
1469
  Field *field;
1472
1470
  if (group)
1476
1474
      The easiest way is to do this is to store both value in a string
1477
1475
      and unpack on access.
1478
1476
    */
1479
 
    table->setVariableWidth();
1480
 
    field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, &my_charset_bin);
 
1477
    field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, table->s, &my_charset_bin);
1481
1478
  }
1482
1479
  else
1483
1480
    field= new Field_double(max_length, maybe_null, name, decimals, true);
1619
1616
  assert(fixed == 1);
1620
1617
  if (null_value)
1621
1618
    return 0.0;
1622
 
 
1623
1619
  switch (hybrid_type) {
1624
1620
  case STRING_RESULT:
1625
 
    {
1626
 
      char *end_not_used;
1627
 
      int err_not_used;
1628
 
      String *res;  res=val_str(&str_value);
1629
 
      return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
1630
 
                               &end_not_used, &err_not_used) : 0.0);
1631
 
    }
 
1621
  {
 
1622
    char *end_not_used;
 
1623
    int err_not_used;
 
1624
    String *res;  res=val_str(&str_value);
 
1625
    return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
 
1626
                             &end_not_used, &err_not_used) : 0.0);
 
1627
  }
1632
1628
  case INT_RESULT:
1633
1629
    return (double) sum_int;
1634
1630
  case DECIMAL_RESULT:
1637
1633
  case REAL_RESULT:
1638
1634
    return sum;
1639
1635
  case ROW_RESULT:
 
1636
  default:
1640
1637
    // This case should never be choosen
1641
 
    break;
 
1638
    assert(0);
 
1639
    return 0;
1642
1640
  }
1643
 
 
1644
 
  assert(0);
1645
 
  return 0;
1646
1641
}
1647
1642
 
1648
1643
int64_t Item_sum_hybrid::val_int()
1670
1665
  assert(fixed == 1);
1671
1666
  if (null_value)
1672
1667
    return 0;
1673
 
 
1674
1668
  switch (hybrid_type) {
1675
1669
  case STRING_RESULT:
1676
1670
    string2my_decimal(E_DEC_FATAL_ERROR, &value, val);
1685
1679
    int2my_decimal(E_DEC_FATAL_ERROR, sum_int, unsigned_flag, val);
1686
1680
    break;
1687
1681
  case ROW_RESULT:
 
1682
  default:
1688
1683
    // This case should never be choosen
1689
1684
    assert(0);
1690
1685
    break;
1691
1686
  }
1692
 
 
1693
1687
  return val;                                   // Keep compiler happy
1694
1688
}
1695
1689
 
1700
1694
  assert(fixed == 1);
1701
1695
  if (null_value)
1702
1696
    return 0;
1703
 
 
1704
1697
  switch (hybrid_type) {
1705
1698
  case STRING_RESULT:
1706
1699
    return &value;
1716
1709
  case ROW_RESULT:
1717
1710
  default:
1718
1711
    // This case should never be choosen
 
1712
    assert(0);
1719
1713
    break;
1720
1714
  }
1721
 
 
1722
1715
  return str;                                   // Keep compiler happy
1723
1716
}
1724
1717
 
1756
1749
{
1757
1750
  switch (hybrid_type) {
1758
1751
  case STRING_RESULT:
 
1752
  {
 
1753
    String *result=args[0]->val_str(&tmp_value);
 
1754
    if (!args[0]->null_value &&
 
1755
        (null_value || sortcmp(&value,result,collation.collation) > 0))
1759
1756
    {
1760
 
      String *result=args[0]->val_str(&tmp_value);
1761
 
      if (!args[0]->null_value &&
1762
 
          (null_value || sortcmp(&value,result,collation.collation) > 0))
1763
 
      {
1764
 
        value.copy(*result);
1765
 
        null_value=0;
1766
 
      }
 
1757
      value.copy(*result);
 
1758
      null_value=0;
1767
1759
    }
1768
 
    break;
 
1760
  }
 
1761
  break;
1769
1762
  case INT_RESULT:
 
1763
  {
 
1764
    int64_t nr=args[0]->val_int();
 
1765
    if (!args[0]->null_value && (null_value ||
 
1766
                                 (unsigned_flag &&
 
1767
                                  (uint64_t) nr < (uint64_t) sum_int) ||
 
1768
                                 (!unsigned_flag && nr < sum_int)))
1770
1769
    {
1771
 
      int64_t nr=args[0]->val_int();
1772
 
      if (!args[0]->null_value && (null_value ||
1773
 
                                   (unsigned_flag &&
1774
 
                                    (uint64_t) nr < (uint64_t) sum_int) ||
1775
 
                                   (!unsigned_flag && nr < sum_int)))
1776
 
      {
1777
 
        sum_int=nr;
1778
 
        null_value=0;
1779
 
      }
 
1770
      sum_int=nr;
 
1771
      null_value=0;
1780
1772
    }
1781
 
    break;
 
1773
  }
 
1774
  break;
1782
1775
  case DECIMAL_RESULT:
 
1776
  {
 
1777
    my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1778
    if (!args[0]->null_value &&
 
1779
        (null_value || (my_decimal_cmp(&sum_dec, val) > 0)))
1783
1780
    {
1784
 
      my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1785
 
      if (!args[0]->null_value &&
1786
 
          (null_value || (my_decimal_cmp(&sum_dec, val) > 0)))
1787
 
      {
1788
 
        my_decimal2decimal(val, &sum_dec);
1789
 
        null_value= 0;
1790
 
      }
 
1781
      my_decimal2decimal(val, &sum_dec);
 
1782
      null_value= 0;
1791
1783
    }
1792
 
    break;
 
1784
  }
 
1785
  break;
1793
1786
  case REAL_RESULT:
 
1787
  {
 
1788
    double nr= args[0]->val_real();
 
1789
    if (!args[0]->null_value && (null_value || nr < sum))
1794
1790
    {
1795
 
      double nr= args[0]->val_real();
1796
 
      if (!args[0]->null_value && (null_value || nr < sum))
1797
 
      {
1798
 
        sum=nr;
1799
 
        null_value=0;
1800
 
      }
 
1791
      sum=nr;
 
1792
      null_value=0;
1801
1793
    }
1802
 
    break;
 
1794
  }
 
1795
  break;
1803
1796
  case ROW_RESULT:
 
1797
  default:
1804
1798
    // This case should never be choosen
1805
1799
    assert(0);
1806
1800
    break;
1819
1813
{
1820
1814
  switch (hybrid_type) {
1821
1815
  case STRING_RESULT:
 
1816
  {
 
1817
    String *result=args[0]->val_str(&tmp_value);
 
1818
    if (!args[0]->null_value &&
 
1819
        (null_value || sortcmp(&value,result,collation.collation) < 0))
1822
1820
    {
1823
 
      String *result=args[0]->val_str(&tmp_value);
1824
 
      if (!args[0]->null_value &&
1825
 
          (null_value || sortcmp(&value,result,collation.collation) < 0))
1826
 
      {
1827
 
        value.copy(*result);
1828
 
        null_value=0;
1829
 
      }
 
1821
      value.copy(*result);
 
1822
      null_value=0;
1830
1823
    }
1831
 
    break;
 
1824
  }
 
1825
  break;
1832
1826
  case INT_RESULT:
 
1827
  {
 
1828
    int64_t nr=args[0]->val_int();
 
1829
    if (!args[0]->null_value && (null_value ||
 
1830
                                 (unsigned_flag &&
 
1831
                                  (uint64_t) nr > (uint64_t) sum_int) ||
 
1832
                                 (!unsigned_flag && nr > sum_int)))
1833
1833
    {
1834
 
      int64_t nr=args[0]->val_int();
1835
 
      if (!args[0]->null_value && (null_value ||
1836
 
                                   (unsigned_flag &&
1837
 
                                    (uint64_t) nr > (uint64_t) sum_int) ||
1838
 
                                   (!unsigned_flag && nr > sum_int)))
1839
 
      {
1840
 
        sum_int=nr;
1841
 
        null_value=0;
1842
 
      }
 
1834
      sum_int=nr;
 
1835
      null_value=0;
1843
1836
    }
1844
 
    break;
 
1837
  }
 
1838
  break;
1845
1839
  case DECIMAL_RESULT:
 
1840
  {
 
1841
    my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
 
1842
    if (!args[0]->null_value &&
 
1843
        (null_value || (my_decimal_cmp(val, &sum_dec) > 0)))
1846
1844
    {
1847
 
      my_decimal value_buff, *val= args[0]->val_decimal(&value_buff);
1848
 
      if (!args[0]->null_value &&
1849
 
          (null_value || (my_decimal_cmp(val, &sum_dec) > 0)))
1850
 
      {
1851
 
        my_decimal2decimal(val, &sum_dec);
1852
 
        null_value= 0;
1853
 
      }
 
1845
      my_decimal2decimal(val, &sum_dec);
 
1846
      null_value= 0;
1854
1847
    }
1855
 
    break;
 
1848
  }
 
1849
  break;
1856
1850
  case REAL_RESULT:
 
1851
  {
 
1852
    double nr= args[0]->val_real();
 
1853
    if (!args[0]->null_value && (null_value || nr > sum))
1857
1854
    {
1858
 
      double nr= args[0]->val_real();
1859
 
      if (!args[0]->null_value && (null_value || nr > sum))
1860
 
      {
1861
 
        sum=nr;
1862
 
        null_value=0;
1863
 
      }
 
1855
      sum=nr;
 
1856
      null_value=0;
1864
1857
    }
1865
 
    break;
 
1858
  }
 
1859
  break;
1866
1860
  case ROW_RESULT:
 
1861
  default:
1867
1862
    // This case should never be choosen
1868
1863
    assert(0);
1869
1864
    break;
1870
1865
  }
1871
 
 
1872
1866
  return 0;
1873
1867
}
1874
1868
 
1956
1950
{
1957
1951
  switch(hybrid_type) {
1958
1952
  case STRING_RESULT:
1959
 
    {
1960
 
      char buff[MAX_FIELD_WIDTH];
1961
 
      String tmp(buff,sizeof(buff),result_field->charset()),*res;
1962
 
 
1963
 
      res=args[0]->val_str(&tmp);
1964
 
      if (args[0]->null_value)
1965
 
      {
 
1953
  {
 
1954
    char buff[MAX_FIELD_WIDTH];
 
1955
    String tmp(buff,sizeof(buff),result_field->charset()),*res;
 
1956
 
 
1957
    res=args[0]->val_str(&tmp);
 
1958
    if (args[0]->null_value)
 
1959
    {
 
1960
      result_field->set_null();
 
1961
      result_field->reset();
 
1962
    }
 
1963
    else
 
1964
    {
 
1965
      result_field->set_notnull();
 
1966
      result_field->store(res->ptr(),res->length(),tmp.charset());
 
1967
    }
 
1968
    break;
 
1969
  }
 
1970
  case INT_RESULT:
 
1971
  {
 
1972
    int64_t nr=args[0]->val_int();
 
1973
 
 
1974
    if (maybe_null)
 
1975
    {
 
1976
      if (args[0]->null_value)
 
1977
      {
 
1978
        nr=0;
 
1979
        result_field->set_null();
 
1980
      }
 
1981
      else
 
1982
        result_field->set_notnull();
 
1983
    }
 
1984
    result_field->store(nr, unsigned_flag);
 
1985
    break;
 
1986
  }
 
1987
  case REAL_RESULT:
 
1988
  {
 
1989
    double nr= args[0]->val_real();
 
1990
 
 
1991
    if (maybe_null)
 
1992
    {
 
1993
      if (args[0]->null_value)
 
1994
      {
 
1995
        nr=0.0;
 
1996
        result_field->set_null();
 
1997
      }
 
1998
      else
 
1999
        result_field->set_notnull();
 
2000
    }
 
2001
    result_field->store(nr);
 
2002
    break;
 
2003
  }
 
2004
  case DECIMAL_RESULT:
 
2005
  {
 
2006
    my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
 
2007
 
 
2008
    if (maybe_null)
 
2009
    {
 
2010
      if (args[0]->null_value)
1966
2011
        result_field->set_null();
1967
 
        result_field->reset();
1968
 
      }
1969
2012
      else
1970
 
      {
1971
2013
        result_field->set_notnull();
1972
 
        result_field->store(res->ptr(),res->length(),tmp.charset());
1973
 
      }
1974
 
      break;
1975
 
    }
1976
 
  case INT_RESULT:
1977
 
    {
1978
 
      int64_t nr=args[0]->val_int();
1979
 
 
1980
 
      if (maybe_null)
1981
 
      {
1982
 
        if (args[0]->null_value)
1983
 
        {
1984
 
          nr=0;
1985
 
          result_field->set_null();
1986
 
        }
1987
 
        else
1988
 
          result_field->set_notnull();
1989
 
      }
1990
 
      result_field->store(nr, unsigned_flag);
1991
 
      break;
1992
 
    }
1993
 
  case REAL_RESULT:
1994
 
    {
1995
 
      double nr= args[0]->val_real();
1996
 
 
1997
 
      if (maybe_null)
1998
 
      {
1999
 
        if (args[0]->null_value)
2000
 
        {
2001
 
          nr=0.0;
2002
 
          result_field->set_null();
2003
 
        }
2004
 
        else
2005
 
          result_field->set_notnull();
2006
 
      }
2007
 
      result_field->store(nr);
2008
 
      break;
2009
 
    }
2010
 
  case DECIMAL_RESULT:
2011
 
    {
2012
 
      my_decimal value_buff, *arg_dec= args[0]->val_decimal(&value_buff);
2013
 
 
2014
 
      if (maybe_null)
2015
 
      {
2016
 
        if (args[0]->null_value)
2017
 
          result_field->set_null();
2018
 
        else
2019
 
          result_field->set_notnull();
2020
 
      }
2021
 
      /*
2022
 
        We must store zero in the field as we will use the field value in
2023
 
        add()
2024
 
      */
2025
 
      if (!arg_dec)                               // Null
2026
 
        arg_dec= &decimal_zero;
2027
 
      result_field->store_decimal(arg_dec);
2028
 
      break;
2029
 
    }
 
2014
    }
 
2015
    /*
 
2016
      We must store zero in the field as we will use the field value in
 
2017
      add()
 
2018
    */
 
2019
    if (!arg_dec)                               // Null
 
2020
      arg_dec= &decimal_zero;
 
2021
    result_field->store_decimal(arg_dec);
 
2022
    break;
 
2023
  }
2030
2024
  case ROW_RESULT:
 
2025
  default:
2031
2026
    assert(0);
2032
2027
  }
2033
2028
}
2222
2217
  case DECIMAL_RESULT:
2223
2218
    min_max_update_decimal_field();
2224
2219
    break;
2225
 
  case REAL_RESULT:
2226
 
  case ROW_RESULT:
 
2220
  default:
2227
2221
    min_max_update_real_field();
2228
2222
  }
2229
2223
}
2236
2230
 
2237
2231
  if (!args[0]->null_value)
2238
2232
  {
2239
 
    result_field->val_str_internal(&tmp_value);
 
2233
    result_field->val_str(&tmp_value);
2240
2234
 
2241
2235
    if (result_field->is_null() ||
2242
2236
        (cmp_sign * sortcmp(res_str,&tmp_value,collation.collation)) < 0)
2503
2497
int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
2504
2498
{
2505
2499
  Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2506
 
  Field **field    = item->table->getFields();
2507
 
  Field **field_end= field + item->table->getShare()->sizeFields();
 
2500
  Field **field    = item->table->field;
 
2501
  Field **field_end= field + item->table->s->fields;
2508
2502
  uint32_t *lengths=item->field_lengths;
2509
2503
  for (; field < field_end; ++field)
2510
2504
  {
2519
2513
  return 0;
2520
2514
}
2521
2515
 
2522
 
static int count_distinct_walk(void *,
2523
 
                               uint32_t ,
 
2516
#ifdef __cplusplus
 
2517
extern "C" {
 
2518
#endif
 
2519
 
 
2520
static int count_distinct_walk(void *elem __attribute__((unused)),
 
2521
                               element_count count __attribute__((unused)),
2524
2522
                               void *arg)
2525
2523
{
2526
2524
  (*((uint64_t*)arg))++;
2527
2525
  return 0;
2528
2526
}
2529
2527
 
 
2528
#ifdef __cplusplus
 
2529
}
 
2530
#endif
 
2531
 
 
2532
 
 
2533
 
2530
2534
void Item_sum_count_distinct::cleanup()
2531
2535
{
2532
2536
  Item_sum_int::cleanup();
2544
2548
    is_evaluated= false;
2545
2549
    if (table)
2546
2550
    {
 
2551
      table->free_tmp_table(table->in_use);
2547
2552
      table= 0;
2548
2553
    }
2549
2554
    delete tmp_table_param;
2580
2585
bool Item_sum_count_distinct::setup(Session *session)
2581
2586
{
2582
2587
  List<Item> list;
2583
 
  Select_Lex *select_lex= session->lex->current_select;
 
2588
  SELECT_LEX *select_lex= session->lex->current_select;
2584
2589
 
2585
2590
  /*
2586
2591
    Setup can be called twice for ROLLUP items. This is a bug.
2590
2595
  if (tree || table || tmp_table_param)
2591
2596
    return false;
2592
2597
 
2593
 
  if (!(tmp_table_param= new Tmp_Table_Param))
 
2598
  if (!(tmp_table_param= new TMP_TABLE_PARAM))
2594
2599
    return true;
2595
2600
 
2596
2601
  /* Create a table with an unique key over all parameters */
2608
2613
  tmp_table_param->force_copy_fields= force_copy_fields;
2609
2614
  assert(table == 0);
2610
2615
 
2611
 
  if (!(table= create_tmp_table(session, tmp_table_param, list, (Order*) 0, 1,
 
2616
  if (!(table= create_tmp_table(session, tmp_table_param, list, (order_st*) 0, 1,
2612
2617
                                0,
2613
2618
                                (select_lex->options | session->options),
2614
2619
                                HA_POS_ERROR, (char*)"")))
2615
 
  {
2616
2620
    return true;
2617
 
  }
2618
 
  table->cursor->extra(HA_EXTRA_NO_ROWS);               // Don't update rows
 
2621
  table->file->extra(HA_EXTRA_NO_ROWS);         // Don't update rows
2619
2622
  table->no_rows=1;
2620
2623
 
2621
 
  if (table->getShare()->db_type() == heap_engine)
 
2624
  if (table->s->db_type() == heap_hton)
2622
2625
  {
2623
2626
    /*
2624
2627
      No blobs, otherwise it would have been MyISAM: set up a compare
2626
2629
    */
2627
2630
    qsort_cmp2 compare_key;
2628
2631
    void* cmp_arg;
2629
 
    Field **field= table->getFields();
2630
 
    Field **field_end= field + table->getShare()->sizeFields();
 
2632
    Field **field= table->field;
 
2633
    Field **field_end= field + table->s->fields;
2631
2634
    bool all_binary= true;
2632
2635
 
2633
2636
    for (tree_key_length= 0; field < field_end; ++field)
2648
2651
    }
2649
2652
    else
2650
2653
    {
2651
 
      if (table->getShare()->sizeFields() == 1)
 
2654
      if (table->s->fields == 1)
2652
2655
      {
2653
2656
        /*
2654
2657
          If we have only one field, which is the most common use of
2657
2660
          about other fields.
2658
2661
        */
2659
2662
        compare_key= (qsort_cmp2) simple_str_key_cmp;
2660
 
        cmp_arg= (void*) table->getField(0);
 
2663
        cmp_arg= (void*) table->field[0];
2661
2664
        /* tree_key_length has been set already */
2662
2665
      }
2663
2666
      else
2665
2668
        uint32_t *length;
2666
2669
        compare_key= (qsort_cmp2) composite_key_cmp;
2667
2670
        cmp_arg= (void*) this;
2668
 
        field_lengths= (uint32_t*) session->alloc(table->getShare()->sizeFields() * sizeof(uint32_t));
2669
 
        for (tree_key_length= 0, length= field_lengths, field= table->getFields();
 
2671
        field_lengths= (uint32_t*) session->alloc(table->s->fields * sizeof(uint32_t));
 
2672
        for (tree_key_length= 0, length= field_lengths, field= table->field;
2670
2673
             field < field_end; ++field, ++length)
2671
2674
        {
2672
2675
          *length= (*field)->pack_length();
2676
2679
    }
2677
2680
    assert(tree == 0);
2678
2681
    tree= new Unique(compare_key, cmp_arg, tree_key_length,
2679
 
                     (size_t)session->variables.max_heap_table_size);
 
2682
                     session->variables.max_heap_table_size);
2680
2683
    /*
2681
2684
      The only time tree_key_length could be 0 is if someone does
2682
2685
      count(distinct) on a char(0) field - stupid thing to do,
2707
2710
  }
2708
2711
  else if (table)
2709
2712
  {
2710
 
    table->cursor->extra(HA_EXTRA_NO_CACHE);
2711
 
    table->cursor->ha_delete_all_rows();
2712
 
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
 
2713
    table->file->extra(HA_EXTRA_NO_CACHE);
 
2714
    table->file->ha_delete_all_rows();
 
2715
    table->file->extra(HA_EXTRA_WRITE_CACHE);
2713
2716
  }
2714
2717
}
2715
2718
 
2719
2722
  if (always_null)
2720
2723
    return 0;
2721
2724
  copy_fields(tmp_table_param);
2722
 
  if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
2723
 
    return true;
 
2725
  copy_funcs(tmp_table_param->items_to_copy);
2724
2726
 
2725
 
  for (Field **field= table->getFields() ; *field ; field++)
2726
 
  {
 
2727
  for (Field **field=table->field ; *field ; field++)
2727
2728
    if ((*field)->is_real_null(0))
2728
 
    {
2729
2729
      return 0;                                 // Don't count NULL
2730
 
    }
2731
 
  }
2732
2730
 
2733
2731
  is_evaluated= false;
2734
2732
  if (tree)
2739
2737
      bloat the tree without providing any valuable info. Besides,
2740
2738
      key_length used to initialize the tree didn't include space for them.
2741
2739
    */
2742
 
    return tree->unique_add(table->record[0] + table->getShare()->null_bytes);
 
2740
    return tree->unique_add(table->record[0] + table->s->null_bytes);
2743
2741
  }
2744
 
  if ((error= table->cursor->insertRecord(table->record[0])) &&
2745
 
      table->cursor->is_fatal_error(error, HA_CHECK_DUP))
 
2742
  if ((error= table->file->ha_write_row(table->record[0])) &&
 
2743
      table->file->is_fatal_error(error, HA_CHECK_DUP))
2746
2744
    return true;
2747
2745
  return false;
2748
2746
}
2767
2765
    return (int64_t) count;
2768
2766
  }
2769
2767
 
2770
 
  error= table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
2768
  error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2771
2769
 
2772
2770
  if(error)
2773
2771
  {
2774
 
    table->print_error(error, MYF(0));
 
2772
    table->file->print_error(error, MYF(0));
2775
2773
  }
2776
2774
 
2777
 
  return table->cursor->stats.records;
 
2775
  return table->file->stats.records;
2778
2776
}
2779
2777
 
2780
2778
/*****************************************************************************
2828
2826
    */
2829
2827
    Field *field= item->get_tmp_table_field();
2830
2828
    int res;
2831
 
    uint32_t offset= field->offset(field->getTable()->record[0])-table->getShare()->null_bytes;
 
2829
    uint32_t offset= field->offset(field->table->record[0])-table->s->null_bytes;
2832
2830
    if((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2833
2831
      return res;
2834
2832
  }
2837
2835
 
2838
2836
 
2839
2837
/**
2840
 
  function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
 
2838
  function of sort for syntax: GROUP_CONCAT(expr,... order_st BY col,... )
2841
2839
*/
2842
2840
 
2843
2841
int group_concat_key_cmp_with_order(void* arg, const void* key1,
2844
2842
                                    const void* key2)
2845
2843
{
2846
2844
  Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2847
 
  Order **order_item, **end;
 
2845
  order_st **order_item, **end;
2848
2846
  Table *table= grp_item->table;
2849
2847
 
2850
2848
  for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2865
2863
    if (field && !item->const_item())
2866
2864
    {
2867
2865
      int res;
2868
 
      uint32_t offset= (field->offset(field->getTable()->record[0]) -
2869
 
                    table->getShare()->null_bytes);
 
2866
      uint32_t offset= (field->offset(field->table->record[0]) -
 
2867
                    table->s->null_bytes);
2870
2868
      if ((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
2871
2869
        return (*order_item)->asc ? res : -res;
2872
2870
    }
2884
2882
  Append data from current leaf to item->result.
2885
2883
*/
2886
2884
 
2887
 
int dump_leaf_key(unsigned char* key, uint32_t ,
 
2885
int dump_leaf_key(unsigned char* key, element_count count __attribute__((unused)),
2888
2886
                  Item_func_group_concat *item)
2889
2887
{
2890
2888
  Table *table= item->table;
2891
 
  String tmp((char *)table->getUpdateRecord(), table->getShare()->getRecordLength(),
 
2889
  String tmp((char *)table->record[1], table->s->reclength,
2892
2890
             default_charset_info);
2893
2891
  String tmp2;
2894
2892
  String *result= &item->result;
2915
2913
        because it contains both order and arg list fields.
2916
2914
      */
2917
2915
      Field *field= (*arg)->get_tmp_table_field();
2918
 
      uint32_t offset= (field->offset(field->getTable()->record[0]) -
2919
 
                    table->getShare()->null_bytes);
2920
 
      assert(offset < table->getShare()->getRecordLength());
2921
 
      res= field->val_str_internal(&tmp, key + offset);
 
2916
      uint32_t offset= (field->offset(field->table->record[0]) -
 
2917
                    table->s->null_bytes);
 
2918
      assert(offset < table->s->reclength);
 
2919
      res= field->val_str(&tmp, key + offset);
2922
2920
    }
2923
2921
    else
2924
2922
      res= (*arg)->val_str(&tmp);
2966
2964
                       bool distinct_arg, List<Item> *select_list,
2967
2965
                       SQL_LIST *order_list, String *separator_arg)
2968
2966
  :tmp_table_param(0), warning(0),
2969
 
   separator(separator_arg), tree(NULL), unique_filter(NULL), table(0),
 
2967
   separator(separator_arg), tree(0), unique_filter(NULL), table(0),
2970
2968
   order(0), context(context_arg),
2971
2969
   arg_count_order(order_list ? order_list->elements : 0),
2972
2970
   arg_count_field(select_list->elements),
2987
2985
           (for possible order items in temporare tables)
2988
2986
    order - arg_count_order
2989
2987
  */
2990
 
  if (!(args= (Item**) memory::sql_alloc(sizeof(Item*) * arg_count +
2991
 
                                 sizeof(Order*)*arg_count_order)))
 
2988
  if (!(args= (Item**) sql_alloc(sizeof(Item*) * arg_count +
 
2989
                                 sizeof(order_st*)*arg_count_order)))
2992
2990
    return;
2993
2991
 
2994
 
  order= (Order**)(args + arg_count);
 
2992
  order= (order_st**)(args + arg_count);
2995
2993
 
2996
2994
  /* fill args items of show and sort */
2997
2995
  List_iterator_fast<Item> li(*select_list);
3001
2999
 
3002
3000
  if (arg_count_order)
3003
3001
  {
3004
 
    Order **order_ptr= order;
3005
 
    for (Order *order_item= (Order*) order_list->first;
 
3002
    order_st **order_ptr= order;
 
3003
    for (order_st *order_item= (order_st*) order_list->first;
3006
3004
         order_item != NULL;
3007
3005
         order_item= order_item->next)
3008
3006
    {
3048
3046
  if (warning)
3049
3047
  {
3050
3048
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
3051
 
    snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
 
3049
    sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3052
3050
    warning->set_msg(current_session, warn_buff);
3053
3051
    warning= 0;
3054
3052
  }
3064
3062
    if (table)
3065
3063
    {
3066
3064
      Session *session= table->in_use;
 
3065
      table->free_tmp_table(session);
3067
3066
      table= 0;
3068
3067
      if (tree)
3069
3068
      {
3078
3077
      if (warning)
3079
3078
      {
3080
3079
        char warn_buff[DRIZZLE_ERRMSG_SIZE];
3081
 
        snprintf(warn_buff, sizeof(warn_buff), ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
 
3080
        sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3082
3081
        warning->set_msg(session, warn_buff);
3083
3082
        warning= 0;
3084
3083
      }
3115
3114
  if (always_null)
3116
3115
    return 0;
3117
3116
  copy_fields(tmp_table_param);
3118
 
  if (copy_funcs(tmp_table_param->items_to_copy, table->in_use))
3119
 
    return true;
 
3117
  copy_funcs(tmp_table_param->items_to_copy);
3120
3118
 
3121
3119
  for (uint32_t i= 0; i < arg_count_field; i++)
3122
3120
  {
3136
3134
  {
3137
3135
    /* Filter out duplicate rows. */
3138
3136
    uint32_t count= unique_filter->elements_in_tree();
3139
 
    unique_filter->unique_add(table->record[0] + table->getShare()->null_bytes);
 
3137
    unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3140
3138
    if (count == unique_filter->elements_in_tree())
3141
3139
      row_eligible= false;
3142
3140
  }
3143
3141
 
3144
3142
  TREE_ELEMENT *el= 0;                          // Only for safety
3145
3143
  if (row_eligible && tree)
3146
 
    el= tree_insert(tree, table->record[0] + table->getShare()->null_bytes, 0,
 
3144
    el= tree_insert(tree, table->record[0] + table->s->null_bytes, 0,
3147
3145
                    tree->custom_arg);
3148
3146
  /*
3149
3147
    If the row is not a duplicate (el->count == 1)
3152
3150
  */
3153
3151
  if (row_eligible && !warning_for_row &&
3154
3152
      (!tree || (el->count == 1 && distinct && !arg_count_order)))
3155
 
    dump_leaf_key(table->record[0] + table->getShare()->null_bytes, 1, this);
 
3153
    dump_leaf_key(table->record[0] + table->s->null_bytes, 1, this);
3156
3154
 
3157
3155
  return 0;
3158
3156
}
3191
3189
  result.set_charset(collation.collation);
3192
3190
  result_field= 0;
3193
3191
  null_value= 1;
3194
 
  max_length= (size_t)session->variables.group_concat_max_len;
 
3192
  max_length= session->variables.group_concat_max_len;
 
3193
 
 
3194
  uint32_t offset;
 
3195
  if (separator->needs_conversion(separator->length(), separator->charset(),
 
3196
                                  collation.collation, &offset))
 
3197
  {
 
3198
    uint32_t buflen= collation.collation->mbmaxlen * separator->length();
 
3199
    uint32_t errors, conv_length;
 
3200
    char *buf;
 
3201
    String *new_separator;
 
3202
 
 
3203
    if (!(buf= (char*) session->alloc(buflen)) ||
 
3204
        !(new_separator= new(session->mem_root)
 
3205
                           String(buf, buflen, collation.collation)))
 
3206
      return true;
 
3207
 
 
3208
    conv_length= copy_and_convert(buf, buflen, collation.collation,
 
3209
                                  separator->ptr(), separator->length(),
 
3210
                                  separator->charset(), &errors);
 
3211
    new_separator->length(conv_length);
 
3212
    separator= new_separator;
 
3213
  }
3195
3214
 
3196
3215
  if (check_sum_func(session, ref))
3197
3216
    return true;
3204
3223
bool Item_func_group_concat::setup(Session *session)
3205
3224
{
3206
3225
  List<Item> list;
3207
 
  Select_Lex *select_lex= session->lex->current_select;
 
3226
  SELECT_LEX *select_lex= session->lex->current_select;
3208
3227
 
3209
3228
  /*
3210
3229
    Currently setup() can be called twice. Please add
3213
3232
  if (table || tree)
3214
3233
    return(false);
3215
3234
 
3216
 
  if (!(tmp_table_param= new Tmp_Table_Param))
 
3235
  if (!(tmp_table_param= new TMP_TABLE_PARAM))
3217
3236
    return(true);
3218
3237
 
3219
3238
  /* We'll convert all blobs to varchar fields in the temporary table */
3254
3273
  {
3255
3274
    /*
3256
3275
      Currently we have to force conversion of BLOB values to VARCHAR's
3257
 
      if we are to store them in TREE objects used for ORDER BY and
 
3276
      if we are to store them in TREE objects used for order_st BY and
3258
3277
      DISTINCT. This leads to truncation if the BLOB's size exceeds
3259
3278
      Field_varstring::MAX_SIZE.
3260
3279
    */
3266
3285
    We have to create a temporary table to get descriptions of fields
3267
3286
    (types, sizes and so on).
3268
3287
 
3269
 
    Note that in the table, we first have the ORDER BY fields, then the
 
3288
    Note that in the table, we first have the order_st BY fields, then the
3270
3289
    field list.
3271
3290
  */
3272
3291
  if (!(table= create_tmp_table(session, tmp_table_param, all_fields,
3273
 
                                (Order*) 0, 0, true,
 
3292
                                (order_st*) 0, 0, true,
3274
3293
                                (select_lex->options | session->options),
3275
3294
                                HA_POS_ERROR, (char*) "")))
3276
 
  {
3277
3295
    return(true);
3278
 
  }
3279
 
 
3280
 
  table->cursor->extra(HA_EXTRA_NO_ROWS);
 
3296
  table->file->extra(HA_EXTRA_NO_ROWS);
3281
3297
  table->no_rows= 1;
3282
3298
 
3283
3299
  /*
3285
3301
     Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3286
3302
     the row is not added to the result.
3287
3303
  */
3288
 
  uint32_t tree_key_length= table->getShare()->getRecordLength() - table->getShare()->null_bytes;
 
3304
  uint32_t tree_key_length= table->s->reclength - table->s->null_bytes;
3289
3305
 
3290
3306
  if (arg_count_order)
3291
3307
  {
3292
3308
    tree= &tree_base;
3293
3309
    /*
3294
3310
      Create a tree for sorting. The tree is used to sort (according to the
3295
 
      syntax of this function). If there is no ORDER BY clause, we don't
 
3311
      syntax of this function). If there is no order_st BY clause, we don't
3296
3312
      create this tree.
3297
3313
    */
3298
 
    init_tree(tree, (uint32_t) min(session->variables.max_heap_table_size,
3299
 
                                   (uint64_t)(session->variables.sortbuff_size/16)), 
3300
 
              0,
 
3314
    init_tree(tree, (uint) cmin(session->variables.max_heap_table_size,
 
3315
                               session->variables.sortbuff_size/16), 0,
3301
3316
              tree_key_length,
3302
 
              group_concat_key_cmp_with_order , false, NULL, (void*) this);
 
3317
              group_concat_key_cmp_with_order , 0, NULL, (void*) this);
3303
3318
  }
3304
3319
 
3305
3320
  if (distinct)
3306
3321
    unique_filter= new Unique(group_concat_key_cmp_with_distinct,
3307
3322
                              (void*)this,
3308
3323
                              tree_key_length,
3309
 
                              (size_t)session->variables.max_heap_table_size);
 
3324
                              session->variables.max_heap_table_size);
3310
3325
 
3311
3326
  return(false);
3312
3327
}
3323
3338
  tree= 0;
3324
3339
}
3325
3340
 
3326
 
double Item_func_group_concat::val_real()
3327
 
{
3328
 
  String *res;  res=val_str(&str_value);
3329
 
  return res ? internal::my_atof(res->c_ptr()) : 0.0;
3330
 
}
3331
 
 
3332
 
int64_t Item_func_group_concat::val_int()
3333
 
{
3334
 
  String *res;
3335
 
  char *end_ptr;
3336
 
  int error;
3337
 
  if (!(res= val_str(&str_value)))
3338
 
    return (int64_t) 0;
3339
 
  end_ptr= (char*) res->ptr()+ res->length();
3340
 
  return internal::my_strtoll10(res->ptr(), &end_ptr, &error);
3341
 
}
3342
 
 
3343
 
String* Item_func_group_concat::val_str(String* )
 
3341
 
 
3342
String* Item_func_group_concat::val_str(String* str __attribute__((unused)))
3344
3343
{
3345
3344
  assert(fixed == 1);
3346
3345
  if (null_value)
3400
3399
  if (!original && unique_filter)
3401
3400
    delete unique_filter;
3402
3401
}
3403
 
 
3404
 
} /* namespace drizzled */