~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/item_sum.cc

  • Committer: Monty Taylor
  • Date: 2008-07-05 11:20:18 UTC
  • mto: This revision was merged to the branch mainline in revision 62.
  • Revision ID: monty@inaugust.com-20080705112018-fr12kkmgphtu7m29
Changes so that removal of duplicate curr_dir from my_sys.h work.

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.
52
56
  {
53
57
    my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
54
58
               MYF(0));
55
 
    return true;
 
59
    return TRUE;
56
60
  }
57
61
  /* Set a reference to the nesting set function if there is  any */
58
62
  in_sum_func= thd->lex->in_sum_func;
65
69
  max_arg_level= -1;
66
70
  max_sum_func_level= -1;
67
71
  outer_fields.empty();
68
 
  return false;
 
72
  return FALSE;
69
73
}
70
74
 
71
75
/**
119
123
 
120
124
bool Item_sum::check_sum_func(THD *thd, Item **ref)
121
125
{
122
 
  bool invalid= false;
 
126
  bool invalid= FALSE;
123
127
  nesting_map allow_sum_func= thd->lex->allow_sum_func;
124
128
  /*  
125
129
    The value of max_arg_level is updated if an argument of the set function
145
149
      If we fail to find such a subquery report an error.
146
150
    */
147
151
    if (register_sum_func(thd, ref))
148
 
      return true;
 
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)
171
175
  {
172
176
    my_message(ER_INVALID_GROUP_FUNC_USE, ER(ER_INVALID_GROUP_FUNC_USE),
173
177
               MYF(0));
174
 
    return true;
 
178
    return TRUE;
175
179
  }
176
180
 
177
181
  if (in_sum_func)
251
255
      {
252
256
        my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
253
257
                   ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
254
 
        return true;
 
258
        return TRUE;
255
259
      }
256
260
    }
257
261
  }
258
262
  aggr_sel->full_group_by_flag|= SUM_FUNC_USED;
259
263
  update_used_tables();
260
264
  thd->lex->in_sum_func= in_sum_func;
261
 
  return false;
 
265
  return FALSE;
262
266
}
263
267
 
264
268
/**
347
351
      sl->master_unit()->item->with_sum_func= 1;
348
352
  }
349
353
  thd->lex->current_select->mark_as_dependent(aggr_sel);
350
 
  return false;
 
354
  return FALSE;
351
355
}
352
356
 
353
357
 
354
358
Item_sum::Item_sum(List<Item> &list) :arg_count(list.elements), 
355
 
  forced_const(false)
 
359
  forced_const(FALSE)
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, TABLE *table,
 
485
                                  uint convert_blob_length)
483
486
{
484
487
  Field *field;
485
488
  switch (result_type()) {
486
489
  case REAL_RESULT:
487
 
    field= new Field_double(max_length, maybe_null, name, decimals, true);
 
490
    field= new Field_double(max_length, maybe_null, name, decimals, TRUE);
488
491
    break;
489
492
  case INT_RESULT:
490
 
    field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
 
493
    field= new Field_longlong(max_length, maybe_null, name, unsigned_flag);
491
494
    break;
492
495
  case STRING_RESULT:
493
496
    if (max_length/collation.collation->mbmaxlen <= 255 ||
504
507
  case ROW_RESULT:
505
508
  default:
506
509
    // This case should never be choosen
507
 
    assert(0);
 
510
    DBUG_ASSERT(0);
508
511
    return 0;
509
512
  }
510
513
  if (field)
518
521
  if (!forced_const)
519
522
  {
520
523
    used_tables_cache= 0;
521
 
    for (uint32_t i=0 ; i < arg_count ; i++)
 
524
    for (uint i=0 ; i < arg_count ; i++)
522
525
    {
523
526
      args[i]->update_used_tables();
524
527
      used_tables_cache|= args[i]->used_tables();
561
564
bool
562
565
Item_sum_num::fix_fields(THD *thd, Item **ref)
563
566
{
564
 
  assert(fixed == 0);
 
567
  DBUG_ASSERT(fixed == 0);
565
568
 
566
569
  if (init_sum_func_check(thd))
567
 
    return true;
 
570
    return TRUE;
568
571
 
569
572
  decimals=0;
570
573
  maybe_null=0;
571
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
574
  for (uint i=0 ; i < arg_count ; i++)
572
575
  {
573
576
    if (args[i]->fix_fields(thd, args + i) || args[i]->check_cols(1))
574
 
      return true;
 
577
      return TRUE;
575
578
    set_if_bigger(decimals, args[i]->decimals);
576
579
    maybe_null |= args[i]->maybe_null;
577
580
  }
581
584
  fix_length_and_dec();
582
585
 
583
586
  if (check_sum_func(thd, ref))
584
 
    return true;
 
587
    return TRUE;
585
588
 
586
589
  fixed= 1;
587
 
  return false;
 
590
  return FALSE;
588
591
}
589
592
 
590
593
 
612
615
    break;
613
616
  case ROW_RESULT:
614
617
  default:
615
 
    assert(0);
 
618
    DBUG_ASSERT(0);
616
619
  }
617
620
  collation.set(item->collation);
618
621
}
620
623
bool
621
624
Item_sum_hybrid::fix_fields(THD *thd, Item **ref)
622
625
{
623
 
  assert(fixed == 0);
 
626
  DBUG_ASSERT(fixed == 0);
624
627
 
625
628
  Item *item= args[0];
626
629
 
627
630
  if (init_sum_func_check(thd))
628
 
    return true;
 
631
    return TRUE;
629
632
 
630
633
  // 'item' can be changed during fix_fields
631
634
  if ((!item->fixed && item->fix_fields(thd, args)) ||
632
635
      (item= args[0])->check_cols(1))
633
 
    return true;
 
636
    return TRUE;
634
637
  decimals=item->decimals;
635
638
 
636
639
  switch (hybrid_type= item->result_type()) {
651
654
    break;
652
655
  case ROW_RESULT:
653
656
  default:
654
 
    assert(0);
 
657
    DBUG_ASSERT(0);
655
658
  };
656
659
  /* MIN/MAX can return NULL for empty set indepedent of the used column */
657
660
  maybe_null= 1;
667
670
    hybrid_field_type= Item::field_type();
668
671
 
669
672
  if (check_sum_func(thd, ref))
670
 
    return true;
 
673
    return TRUE;
671
674
 
672
675
  fixed= 1;
673
 
  return false;
 
676
  return FALSE;
674
677
}
675
678
 
676
 
Field *Item_sum_hybrid::create_tmp_field(bool group, Table *table,
677
 
                                         uint32_t convert_blob_length)
 
679
Field *Item_sum_hybrid::create_tmp_field(bool group, TABLE *table,
 
680
                                         uint convert_blob_length)
678
681
{
679
682
  Field *field;
680
683
  if (args[0]->type() == Item::FIELD_ITEM)
692
695
    fields creations separately.
693
696
  */
694
697
  switch (args[0]->field_type()) {
695
 
  case DRIZZLE_TYPE_NEWDATE:
 
698
  case MYSQL_TYPE_DATE:
696
699
    field= new Field_newdate(maybe_null, name, collation.collation);
697
700
    break;
698
 
  case DRIZZLE_TYPE_TIME:
 
701
  case MYSQL_TYPE_TIME:
699
702
    field= new Field_time(maybe_null, name, collation.collation);
700
703
    break;
701
 
  case DRIZZLE_TYPE_TIMESTAMP:
702
 
  case DRIZZLE_TYPE_DATETIME:
 
704
  case MYSQL_TYPE_TIMESTAMP:
 
705
  case MYSQL_TYPE_DATETIME:
703
706
    field= new Field_datetime(maybe_null, name, collation.collation);
704
707
    break;
705
708
  default:
741
744
 
742
745
void Item_sum_sum::clear()
743
746
{
 
747
  DBUG_ENTER("Item_sum_sum::clear");
744
748
  null_value=1;
745
749
  if (hybrid_type == DECIMAL_RESULT)
746
750
  {
749
753
  }
750
754
  else
751
755
    sum= 0.0;
752
 
  return;
 
756
  DBUG_VOID_RETURN;
753
757
}
754
758
 
755
759
 
756
760
void Item_sum_sum::fix_length_and_dec()
757
761
{
 
762
  DBUG_ENTER("Item_sum_sum::fix_length_and_dec");
758
763
  maybe_null=null_value=1;
759
764
  decimals= args[0]->decimals;
760
765
  switch (args[0]->result_type()) {
777
782
  }
778
783
  case ROW_RESULT:
779
784
  default:
780
 
    assert(0);
 
785
    DBUG_ASSERT(0);
781
786
  }
782
 
  return;
 
787
  DBUG_PRINT("info", ("Type: %s (%d, %d)",
 
788
                      (hybrid_type == REAL_RESULT ? "REAL_RESULT" :
 
789
                       hybrid_type == DECIMAL_RESULT ? "DECIMAL_RESULT" :
 
790
                       hybrid_type == INT_RESULT ? "INT_RESULT" :
 
791
                       "--ILLEGAL!!!--"),
 
792
                      max_length,
 
793
                      (int)decimals));
 
794
  DBUG_VOID_RETURN;
783
795
}
784
796
 
785
797
 
786
798
bool Item_sum_sum::add()
787
799
{
 
800
  DBUG_ENTER("Item_sum_sum::add");
788
801
  if (hybrid_type == DECIMAL_RESULT)
789
802
  {
790
803
    my_decimal value, *val= args[0]->val_decimal(&value);
802
815
    if (!args[0]->null_value)
803
816
      null_value= 0;
804
817
  }
805
 
  return(0);
 
818
  DBUG_RETURN(0);
806
819
}
807
820
 
808
821
 
809
 
int64_t Item_sum_sum::val_int()
 
822
longlong Item_sum_sum::val_int()
810
823
{
811
 
  assert(fixed == 1);
 
824
  DBUG_ASSERT(fixed == 1);
812
825
  if (hybrid_type == DECIMAL_RESULT)
813
826
  {
814
 
    int64_t result;
 
827
    longlong result;
815
828
    my_decimal2int(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, unsigned_flag,
816
829
                   &result);
817
830
    return result;
818
831
  }
819
 
  return (int64_t) rint(val_real());
 
832
  return (longlong) rint(val_real());
820
833
}
821
834
 
822
835
 
823
836
double Item_sum_sum::val_real()
824
837
{
825
 
  assert(fixed == 1);
 
838
  DBUG_ASSERT(fixed == 1);
826
839
  if (hybrid_type == DECIMAL_RESULT)
827
840
    my_decimal2double(E_DEC_FATAL_ERROR, dec_buffs + curr_dec_buff, &sum);
828
841
  return sum;
846
859
 
847
860
/***************************************************************************/
848
861
 
849
 
#ifdef __cplusplus
850
 
extern "C" {
851
 
#endif
 
862
C_MODE_START
852
863
 
853
864
/* Declarations for auxilary C-callbacks */
854
865
 
855
866
static int simple_raw_key_cmp(void* arg, const void* key1, const void* key2)
856
867
{
857
 
    return memcmp(key1, key2, *(uint32_t *) arg);
 
868
    return memcmp(key1, key2, *(uint *) arg);
858
869
}
859
870
 
860
871
 
861
 
static int item_sum_distinct_walk(void *element,
862
 
                                  element_count num_of_dups __attribute__((unused)),
 
872
static int item_sum_distinct_walk(void *element, element_count num_of_dups,
863
873
                                  void *item)
864
874
{
865
875
  return ((Item_sum_distinct*) (item))->unique_walk_function(element);
866
876
}
867
877
 
868
 
#ifdef __cplusplus
869
 
}
870
 
#endif
 
878
C_MODE_END
871
879
 
872
880
/* Item_sum_distinct */
873
881
 
906
914
  virtual void fix_length_and_dec(Item *item, Item *arg) const
907
915
  { Hybrid_type_traits_decimal::instance()->fix_length_and_dec(item, arg); }
908
916
 
909
 
  virtual void div(Hybrid_type *val, uint64_t u) const
 
917
  virtual void div(Hybrid_type *val, ulonglong u) const
910
918
  {
911
919
    int2my_decimal(E_DEC_FATAL_ERROR, val->integer, 0, val->dec_buf);
912
920
    val->used_dec_buf_no= 0;
927
935
 
928
936
void Item_sum_distinct::fix_length_and_dec()
929
937
{
930
 
  assert(args[0]->fixed);
 
938
  DBUG_ASSERT(args[0]->fixed);
931
939
 
932
940
  table_field_type= args[0]->field_type();
933
941
 
936
944
  case STRING_RESULT:
937
945
  case REAL_RESULT:
938
946
    val.traits= Hybrid_type_traits::instance();
939
 
    table_field_type= DRIZZLE_TYPE_DOUBLE;
 
947
    if (table_field_type != MYSQL_TYPE_FLOAT)
 
948
      table_field_type= MYSQL_TYPE_DOUBLE;
940
949
    break;
941
950
  case INT_RESULT:
942
951
  /*
943
952
    Preserving int8, int16, int32 field types gives ~10% performance boost
944
953
    as the size of result tree becomes significantly smaller.
945
 
    Another speed up we gain by using int64_t for intermediate
 
954
    Another speed up we gain by using longlong for intermediate
946
955
    calculations. The range of int64 is enough to hold sum 2^32 distinct
947
956
    integers each <= 2^32.
948
957
  */
949
 
  if (table_field_type >= DRIZZLE_TYPE_TINY && table_field_type <= DRIZZLE_TYPE_LONG)
 
958
  if (table_field_type == MYSQL_TYPE_INT24 ||
 
959
      (table_field_type >= MYSQL_TYPE_TINY && table_field_type <= MYSQL_TYPE_LONG))
950
960
  {
951
961
    val.traits= Hybrid_type_traits_fast_decimal::instance();
952
962
    break;
953
963
  }
954
 
  table_field_type= DRIZZLE_TYPE_LONGLONG;
 
964
  table_field_type= MYSQL_TYPE_LONGLONG;
955
965
  /* fallthrough */
956
966
  case DECIMAL_RESULT:
957
967
    val.traits= Hybrid_type_traits_decimal::instance();
958
 
    if (table_field_type != DRIZZLE_TYPE_LONGLONG)
959
 
      table_field_type= DRIZZLE_TYPE_NEWDECIMAL;
 
968
    if (table_field_type != MYSQL_TYPE_LONGLONG)
 
969
      table_field_type= MYSQL_TYPE_NEWDECIMAL;
960
970
    break;
961
971
  case ROW_RESULT:
962
972
  default:
963
 
    assert(0);
 
973
    DBUG_ASSERT(0);
964
974
  }
965
975
  val.traits->fix_length_and_dec(this, args[0]);
966
976
}
974
984
{
975
985
  List<Create_field> field_list;
976
986
  Create_field field_def;                              /* field definition */
 
987
  DBUG_ENTER("Item_sum_distinct::setup");
977
988
  /* It's legal to call setup() more than once when in a subquery */
978
989
  if (tree)
979
 
    return(false);
 
990
    DBUG_RETURN(FALSE);
980
991
 
981
992
  /*
982
993
    Virtual table and the tree are created anew on each re-execution of
984
995
    mem_root.
985
996
  */
986
997
  if (field_list.push_back(&field_def))
987
 
    return(true);
 
998
    DBUG_RETURN(TRUE);
988
999
 
989
1000
  null_value= maybe_null= 1;
990
1001
  quick_group= 0;
991
1002
 
992
 
  assert(args[0]->fixed);
 
1003
  DBUG_ASSERT(args[0]->fixed);
993
1004
 
994
1005
  field_def.init_for_tmp_table(table_field_type, args[0]->max_length,
995
1006
                               args[0]->decimals, args[0]->maybe_null,
996
1007
                               args[0]->unsigned_flag);
997
1008
 
998
1009
  if (! (table= create_virtual_tmp_table(thd, field_list)))
999
 
    return(true);
 
1010
    DBUG_RETURN(TRUE);
1000
1011
 
1001
1012
  /* XXX: check that the case of CHAR(0) works OK */
1002
1013
  tree_key_length= table->s->reclength - table->s->null_bytes;
1010
1021
  tree= new Unique(simple_raw_key_cmp, &tree_key_length, tree_key_length,
1011
1022
                   thd->variables.max_heap_table_size);
1012
1023
 
1013
 
  is_evaluated= false;
1014
 
  return(tree == 0);
 
1024
  is_evaluated= FALSE;
 
1025
  DBUG_RETURN(tree == 0);
1015
1026
}
1016
1027
 
1017
1028
 
1018
1029
bool Item_sum_distinct::add()
1019
1030
{
1020
 
  args[0]->save_in_field(table->field[0], false);
1021
 
  is_evaluated= false;
 
1031
  args[0]->save_in_field(table->field[0], FALSE);
 
1032
  is_evaluated= FALSE;
1022
1033
  if (!table->field[0]->is_null())
1023
1034
  {
1024
 
    assert(tree);
 
1035
    DBUG_ASSERT(tree);
1025
1036
    null_value= 0;
1026
1037
    /*
1027
1038
      '0' values are also stored in the tree. This doesn't matter
1044
1055
 
1045
1056
void Item_sum_distinct::clear()
1046
1057
{
1047
 
  assert(tree != 0);                        /* we always have a tree */
 
1058
  DBUG_ENTER("Item_sum_distinct::clear");
 
1059
  DBUG_ASSERT(tree != 0);                        /* we always have a tree */
1048
1060
  null_value= 1;
1049
1061
  tree->reset();
1050
 
  is_evaluated= false;
1051
 
  return;
 
1062
  is_evaluated= FALSE;
 
1063
  DBUG_VOID_RETURN;
1052
1064
}
1053
1065
 
1054
1066
void Item_sum_distinct::cleanup()
1057
1069
  delete tree;
1058
1070
  tree= 0;
1059
1071
  table= 0;
1060
 
  is_evaluated= false;
 
1072
  is_evaluated= FALSE;
1061
1073
}
1062
1074
 
1063
1075
Item_sum_distinct::~Item_sum_distinct()
1082
1094
      table->field[0]->set_notnull();
1083
1095
      tree->walk(item_sum_distinct_walk, (void*) this);
1084
1096
    }
1085
 
    is_evaluated= true;
 
1097
    is_evaluated= TRUE;
1086
1098
  }
1087
1099
}
1088
1100
 
1103
1115
}
1104
1116
 
1105
1117
 
1106
 
int64_t Item_sum_distinct::val_int()
 
1118
longlong Item_sum_distinct::val_int()
1107
1119
{
1108
1120
  calculate_val_and_count();
1109
1121
  return val.traits->val_int(&val, unsigned_flag);
1131
1143
    AVG() will divide val by count. We need to reserve digits
1132
1144
    after decimal point as the result can be fractional.
1133
1145
  */
1134
 
  decimals= cmin(decimals + prec_increment, (unsigned int)NOT_FIXED_DEC);
 
1146
  decimals= min(decimals + prec_increment, NOT_FIXED_DEC);
1135
1147
}
1136
1148
 
1137
1149
 
1143
1155
    Item_sum_distinct::calculate_val_and_count();
1144
1156
    if (count)
1145
1157
      val.traits->div(&val, count);
1146
 
    is_evaluated= true;
 
1158
    is_evaluated= TRUE;
1147
1159
  }
1148
1160
}
1149
1161
 
1167
1179
  return 0;
1168
1180
}
1169
1181
 
1170
 
int64_t Item_sum_count::val_int()
 
1182
longlong Item_sum_count::val_int()
1171
1183
{
1172
 
  assert(fixed == 1);
1173
 
  return (int64_t) count;
 
1184
  DBUG_ASSERT(fixed == 1);
 
1185
  return (longlong) count;
1174
1186
}
1175
1187
 
1176
1188
 
1177
1189
void Item_sum_count::cleanup()
1178
1190
{
 
1191
  DBUG_ENTER("Item_sum_count::cleanup");
1179
1192
  count= 0;
1180
1193
  Item_sum_int::cleanup();
1181
 
  return;
 
1194
  DBUG_VOID_RETURN;
1182
1195
}
1183
1196
 
1184
1197
 
1193
1206
  if (hybrid_type == DECIMAL_RESULT)
1194
1207
  {
1195
1208
    int precision= args[0]->decimal_precision() + prec_increment;
1196
 
    decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
 
1209
    decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1197
1210
    max_length= my_decimal_precision_to_length(precision, decimals,
1198
1211
                                               unsigned_flag);
1199
 
    f_precision= cmin(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
 
1212
    f_precision= min(precision+DECIMAL_LONGLONG_DIGITS, DECIMAL_MAX_PRECISION);
1200
1213
    f_scale=  args[0]->decimals;
1201
1214
    dec_bin_size= my_decimal_get_binary_size(f_precision, f_scale);
1202
1215
  }
1203
1216
  else {
1204
 
    decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) NOT_FIXED_DEC);
 
1217
    decimals= min(args[0]->decimals + prec_increment, NOT_FIXED_DEC);
1205
1218
    max_length= args[0]->max_length + prec_increment;
1206
1219
  }
1207
1220
}
1213
1226
}
1214
1227
 
1215
1228
 
1216
 
Field *Item_sum_avg::create_tmp_field(bool group, Table *table,
1217
 
                                      uint32_t convert_blob_len __attribute__((unused)))
 
1229
Field *Item_sum_avg::create_tmp_field(bool group, TABLE *table,
 
1230
                                      uint convert_blob_len)
1218
1231
{
1219
1232
  Field *field;
1220
1233
  if (group)
1224
1237
      The easiest way is to do this is to store both value in a string
1225
1238
      and unpack on access.
1226
1239
    */
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);
 
1240
    field= new Field_string(((hybrid_type == DECIMAL_RESULT) ?
 
1241
                             dec_bin_size : sizeof(double)) + sizeof(longlong),
 
1242
                            0, name, &my_charset_bin);
1230
1243
  }
1231
1244
  else if (hybrid_type == DECIMAL_RESULT)
1232
1245
    field= new Field_new_decimal(max_length, maybe_null, name,
1233
1246
                                 decimals, unsigned_flag);
1234
1247
  else
1235
 
    field= new Field_double(max_length, maybe_null, name, decimals, true);
 
1248
    field= new Field_double(max_length, maybe_null, name, decimals, TRUE);
1236
1249
  if (field)
1237
1250
    field->init(table);
1238
1251
  return field;
1249
1262
bool Item_sum_avg::add()
1250
1263
{
1251
1264
  if (Item_sum_sum::add())
1252
 
    return true;
 
1265
    return TRUE;
1253
1266
  if (!args[0]->null_value)
1254
1267
    count++;
1255
 
  return false;
 
1268
  return FALSE;
1256
1269
}
1257
1270
 
1258
1271
double Item_sum_avg::val_real()
1259
1272
{
1260
 
  assert(fixed == 1);
 
1273
  DBUG_ASSERT(fixed == 1);
1261
1274
  if (!count)
1262
1275
  {
1263
1276
    null_value=1;
1264
1277
    return 0.0;
1265
1278
  }
1266
 
  return Item_sum_sum::val_real() / uint64_t2double(count);
 
1279
  return Item_sum_sum::val_real() / ulonglong2double(count);
1267
1280
}
1268
1281
 
1269
1282
 
1271
1284
{
1272
1285
  my_decimal sum_buff, cnt;
1273
1286
  const my_decimal *sum_dec;
1274
 
  assert(fixed == 1);
 
1287
  DBUG_ASSERT(fixed == 1);
1275
1288
  if (!count)
1276
1289
  {
1277
1290
    null_value=1;
1306
1319
 
1307
1320
double Item_sum_std::val_real()
1308
1321
{
1309
 
  assert(fixed == 1);
 
1322
  DBUG_ASSERT(fixed == 1);
1310
1323
  double nr= Item_sum_variance::val_real();
1311
 
  assert(nr >= 0.0);
 
1324
  DBUG_ASSERT(nr >= 0.0);
1312
1325
  return sqrt(nr);
1313
1326
}
1314
1327
 
1335
1348
  variance.  The difference between the two classes is that the first is used
1336
1349
  for a mundane SELECT, while the latter is used in a GROUPing SELECT.
1337
1350
*/
1338
 
static void variance_fp_recurrence_next(double *m, double *s, uint64_t *count, double nr)
 
1351
static void variance_fp_recurrence_next(double *m, double *s, ulonglong *count, double nr)
1339
1352
{
1340
1353
  *count += 1;
1341
1354
 
1353
1366
}
1354
1367
 
1355
1368
 
1356
 
static double variance_fp_recurrence_result(double s, uint64_t count, bool is_sample_variance)
 
1369
static double variance_fp_recurrence_result(double s, ulonglong count, bool is_sample_variance)
1357
1370
{
1358
1371
  if (count == 1)
1359
1372
    return 0.0;
1378
1391
 
1379
1392
void Item_sum_variance::fix_length_and_dec()
1380
1393
{
 
1394
  DBUG_ENTER("Item_sum_variance::fix_length_and_dec");
1381
1395
  maybe_null= null_value= 1;
1382
1396
  prec_increment= current_thd->variables.div_precincrement;
1383
1397
 
1392
1406
  switch (args[0]->result_type()) {
1393
1407
  case REAL_RESULT:
1394
1408
  case STRING_RESULT:
1395
 
    decimals= cmin(args[0]->decimals + 4, NOT_FIXED_DEC);
 
1409
    decimals= min(args[0]->decimals + 4, NOT_FIXED_DEC);
1396
1410
    break;
1397
1411
  case INT_RESULT:
1398
1412
  case DECIMAL_RESULT:
1399
1413
  {
1400
1414
    int precision= args[0]->decimal_precision()*2 + prec_increment;
1401
 
    decimals= cmin(args[0]->decimals + prec_increment, (unsigned int) DECIMAL_MAX_SCALE);
 
1415
    decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1402
1416
    max_length= my_decimal_precision_to_length(precision, decimals,
1403
1417
                                               unsigned_flag);
1404
1418
 
1406
1420
  }
1407
1421
  case ROW_RESULT:
1408
1422
  default:
1409
 
    assert(0);
 
1423
    DBUG_ASSERT(0);
1410
1424
  }
1411
 
  return;
 
1425
  DBUG_PRINT("info", ("Type: REAL_RESULT (%d, %d)", max_length, (int)decimals));
 
1426
  DBUG_VOID_RETURN;
1412
1427
}
1413
1428
 
1414
1429
 
1423
1438
  If we're grouping, then we need some space to serialize variables into, to
1424
1439
  pass around.
1425
1440
*/
1426
 
Field *Item_sum_variance::create_tmp_field(bool group, Table *table,
1427
 
                                           uint32_t convert_blob_len __attribute__((unused)))
 
1441
Field *Item_sum_variance::create_tmp_field(bool group, TABLE *table,
 
1442
                                           uint convert_blob_len)
1428
1443
{
1429
1444
  Field *field;
1430
1445
  if (group)
1434
1449
      The easiest way is to do this is to store both value in a string
1435
1450
      and unpack on access.
1436
1451
    */
1437
 
    field= new Field_varstring(sizeof(double)*2 + sizeof(int64_t), 0, name, table->s, &my_charset_bin);
 
1452
    field= new Field_string(sizeof(double)*2 + sizeof(longlong), 0, name, &my_charset_bin);
1438
1453
  }
1439
1454
  else
1440
 
    field= new Field_double(max_length, maybe_null, name, decimals, true);
 
1455
    field= new Field_double(max_length, maybe_null, name, decimals, TRUE);
1441
1456
 
1442
1457
  if (field != NULL)
1443
1458
    field->init(table);
1466
1481
 
1467
1482
double Item_sum_variance::val_real()
1468
1483
{
1469
 
  assert(fixed == 1);
 
1484
  DBUG_ASSERT(fixed == 1);
1470
1485
 
1471
1486
  /*
1472
1487
    'sample' is a 1/0 boolean value.  If it is 1/true, id est this is a sample
1477
1492
    Another way to read it is that 'sample' is the numerical threshhold, at and
1478
1493
    below which a 'count' number of items is called NULL.
1479
1494
  */
1480
 
  assert((sample == 0) || (sample == 1));
 
1495
  DBUG_ASSERT((sample == 0) || (sample == 1));
1481
1496
  if (count <= sample)
1482
1497
  {
1483
1498
    null_value=1;
1491
1506
 
1492
1507
my_decimal *Item_sum_variance::val_decimal(my_decimal *dec_buf)
1493
1508
{
1494
 
  assert(fixed == 1);
 
1509
  DBUG_ASSERT(fixed == 1);
1495
1510
  return val_decimal_from_real(dec_buf);
1496
1511
}
1497
1512
 
1499
1514
void Item_sum_variance::reset_field()
1500
1515
{
1501
1516
  double nr;
1502
 
  unsigned char *res= result_field->ptr;
 
1517
  uchar *res= result_field->ptr;
1503
1518
 
1504
1519
  nr= args[0]->val_real();              /* sets null_value as side-effect */
1505
1520
 
1506
1521
  if (args[0]->null_value)
1507
 
    memset(res, 0, sizeof(double)*2+sizeof(int64_t));
 
1522
    bzero(res,sizeof(double)*2+sizeof(longlong));
1508
1523
  else
1509
1524
  {
1510
 
    /* Serialize format is (double)m, (double)s, (int64_t)count */
1511
 
    uint64_t tmp_count;
 
1525
    /* Serialize format is (double)m, (double)s, (longlong)count */
 
1526
    ulonglong tmp_count;
1512
1527
    double tmp_s;
1513
1528
    float8store(res, nr);               /* recurrence variable m */
1514
1529
    tmp_s= 0.0;
1521
1536
 
1522
1537
void Item_sum_variance::update_field()
1523
1538
{
1524
 
  uint64_t field_count;
1525
 
  unsigned char *res=result_field->ptr;
 
1539
  ulonglong field_count;
 
1540
  uchar *res=result_field->ptr;
1526
1541
 
1527
1542
  double nr= args[0]->val_real();       /* sets null_value as side-effect */
1528
1543
 
1529
1544
  if (args[0]->null_value)
1530
1545
    return;
1531
1546
 
1532
 
  /* Serialize format is (double)m, (double)s, (int64_t)count */
 
1547
  /* Serialize format is (double)m, (double)s, (longlong)count */
1533
1548
  double field_recurrence_m, field_recurrence_s;
1534
1549
  float8get(field_recurrence_m, res);
1535
1550
  float8get(field_recurrence_s, res + sizeof(double));
1566
1581
 
1567
1582
double Item_sum_hybrid::val_real()
1568
1583
{
1569
 
  assert(fixed == 1);
 
1584
  DBUG_ASSERT(fixed == 1);
1570
1585
  if (null_value)
1571
1586
    return 0.0;
1572
1587
  switch (hybrid_type) {
1580
1595
  }
1581
1596
  case INT_RESULT:
1582
1597
    if (unsigned_flag)
1583
 
      return uint64_t2double(sum_int);
 
1598
      return ulonglong2double(sum_int);
1584
1599
    return (double) sum_int;
1585
1600
  case DECIMAL_RESULT:
1586
1601
    my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1590
1605
  case ROW_RESULT:
1591
1606
  default:
1592
1607
    // This case should never be choosen
1593
 
    assert(0);
 
1608
    DBUG_ASSERT(0);
1594
1609
    return 0;
1595
1610
  }
1596
1611
}
1597
1612
 
1598
 
int64_t Item_sum_hybrid::val_int()
 
1613
longlong Item_sum_hybrid::val_int()
1599
1614
{
1600
 
  assert(fixed == 1);
 
1615
  DBUG_ASSERT(fixed == 1);
1601
1616
  if (null_value)
1602
1617
    return 0;
1603
1618
  switch (hybrid_type) {
1605
1620
    return sum_int;
1606
1621
  case DECIMAL_RESULT:
1607
1622
  {
1608
 
    int64_t result;
 
1623
    longlong result;
1609
1624
    my_decimal2int(E_DEC_FATAL_ERROR, &sum_dec, unsigned_flag, &result);
1610
1625
    return sum_int;
1611
1626
  }
1612
1627
  default:
1613
 
    return (int64_t) rint(Item_sum_hybrid::val_real());
 
1628
    return (longlong) rint(Item_sum_hybrid::val_real());
1614
1629
  }
1615
1630
}
1616
1631
 
1617
1632
 
1618
1633
my_decimal *Item_sum_hybrid::val_decimal(my_decimal *val)
1619
1634
{
1620
 
  assert(fixed == 1);
 
1635
  DBUG_ASSERT(fixed == 1);
1621
1636
  if (null_value)
1622
1637
    return 0;
1623
1638
  switch (hybrid_type) {
1636
1651
  case ROW_RESULT:
1637
1652
  default:
1638
1653
    // This case should never be choosen
1639
 
    assert(0);
 
1654
    DBUG_ASSERT(0);
1640
1655
    break;
1641
1656
  }
1642
1657
  return val;                                   // Keep compiler happy
1646
1661
String *
1647
1662
Item_sum_hybrid::val_str(String *str)
1648
1663
{
1649
 
  assert(fixed == 1);
 
1664
  DBUG_ASSERT(fixed == 1);
1650
1665
  if (null_value)
1651
1666
    return 0;
1652
1667
  switch (hybrid_type) {
1664
1679
  case ROW_RESULT:
1665
1680
  default:
1666
1681
    // This case should never be choosen
1667
 
    assert(0);
 
1682
    DBUG_ASSERT(0);
1668
1683
    break;
1669
1684
  }
1670
1685
  return str;                                   // Keep compiler happy
1673
1688
 
1674
1689
void Item_sum_hybrid::cleanup()
1675
1690
{
 
1691
  DBUG_ENTER("Item_sum_hybrid::cleanup");
1676
1692
  Item_sum::cleanup();
1677
 
  forced_const= false;
 
1693
  forced_const= FALSE;
1678
1694
 
1679
1695
  /*
1680
1696
    by default it is TRUE to avoid TRUE reporting by
1683
1699
    no_rows_in_result() set it to FALSE if was not results found.
1684
1700
    If some results found it will be left unchanged.
1685
1701
  */
1686
 
  was_values= true;
1687
 
  return;
 
1702
  was_values= TRUE;
 
1703
  DBUG_VOID_RETURN;
1688
1704
}
1689
1705
 
1690
1706
void Item_sum_hybrid::no_rows_in_result()
1691
1707
{
1692
 
  was_values= false;
 
1708
  was_values= FALSE;
1693
1709
  clear();
1694
1710
}
1695
1711
 
1716
1732
  break;
1717
1733
  case INT_RESULT:
1718
1734
  {
1719
 
    int64_t nr=args[0]->val_int();
 
1735
    longlong nr=args[0]->val_int();
1720
1736
    if (!args[0]->null_value && (null_value ||
1721
1737
                                 (unsigned_flag && 
1722
 
                                  (uint64_t) nr < (uint64_t) sum_int) ||
 
1738
                                  (ulonglong) nr < (ulonglong) sum_int) ||
1723
1739
                                 (!unsigned_flag && nr < sum_int)))
1724
1740
    {
1725
1741
      sum_int=nr;
1751
1767
  case ROW_RESULT:
1752
1768
  default:
1753
1769
    // This case should never be choosen
1754
 
    assert(0);
 
1770
    DBUG_ASSERT(0);
1755
1771
    break;
1756
1772
  }
1757
1773
  return 0;
1780
1796
  break;
1781
1797
  case INT_RESULT:
1782
1798
  {
1783
 
    int64_t nr=args[0]->val_int();
 
1799
    longlong nr=args[0]->val_int();
1784
1800
    if (!args[0]->null_value && (null_value ||
1785
1801
                                 (unsigned_flag && 
1786
 
                                  (uint64_t) nr > (uint64_t) sum_int) ||
 
1802
                                  (ulonglong) nr > (ulonglong) sum_int) ||
1787
1803
                                 (!unsigned_flag && nr > sum_int)))
1788
1804
    {
1789
1805
      sum_int=nr;
1815
1831
  case ROW_RESULT:
1816
1832
  default:
1817
1833
    // This case should never be choosen
1818
 
    assert(0);
 
1834
    DBUG_ASSERT(0);
1819
1835
    break;
1820
1836
  }
1821
1837
  return 0;
1824
1840
 
1825
1841
/* bit_or and bit_and */
1826
1842
 
1827
 
int64_t Item_sum_bit::val_int()
 
1843
longlong Item_sum_bit::val_int()
1828
1844
{
1829
 
  assert(fixed == 1);
1830
 
  return (int64_t) bits;
 
1845
  DBUG_ASSERT(fixed == 1);
 
1846
  return (longlong) bits;
1831
1847
}
1832
1848
 
1833
1849
 
1844
1860
 
1845
1861
bool Item_sum_or::add()
1846
1862
{
1847
 
  uint64_t value= (uint64_t) args[0]->val_int();
 
1863
  ulonglong value= (ulonglong) args[0]->val_int();
1848
1864
  if (!args[0]->null_value)
1849
1865
    bits|=value;
1850
1866
  return 0;
1858
1874
 
1859
1875
bool Item_sum_xor::add()
1860
1876
{
1861
 
  uint64_t value= (uint64_t) args[0]->val_int();
 
1877
  ulonglong value= (ulonglong) args[0]->val_int();
1862
1878
  if (!args[0]->null_value)
1863
1879
    bits^=value;
1864
1880
  return 0;
1872
1888
 
1873
1889
bool Item_sum_and::add()
1874
1890
{
1875
 
  uint64_t value= (uint64_t) args[0]->val_int();
 
1891
  ulonglong value= (ulonglong) args[0]->val_int();
1876
1892
  if (!args[0]->null_value)
1877
1893
    bits&=value;
1878
1894
  return 0;
1885
1901
void Item_sum_num::reset_field()
1886
1902
{
1887
1903
  double nr= args[0]->val_real();
1888
 
  unsigned char *res=result_field->ptr;
 
1904
  uchar *res=result_field->ptr;
1889
1905
 
1890
1906
  if (maybe_null)
1891
1907
  {
1924
1940
  }
1925
1941
  case INT_RESULT:
1926
1942
  {
1927
 
    int64_t nr=args[0]->val_int();
 
1943
    longlong nr=args[0]->val_int();
1928
1944
 
1929
1945
    if (maybe_null)
1930
1946
    {
1978
1994
  }
1979
1995
  case ROW_RESULT:
1980
1996
  default:
1981
 
    assert(0);
 
1997
    DBUG_ASSERT(0);
1982
1998
  }
1983
1999
}
1984
2000
 
1994
2010
  }
1995
2011
  else
1996
2012
  {
1997
 
    assert(hybrid_type == REAL_RESULT);
 
2013
    DBUG_ASSERT(hybrid_type == REAL_RESULT);
1998
2014
    double nr= args[0]->val_real();                     // Nulls also return 0
1999
2015
    float8store(result_field->ptr, nr);
2000
2016
  }
2007
2023
 
2008
2024
void Item_sum_count::reset_field()
2009
2025
{
2010
 
  unsigned char *res=result_field->ptr;
2011
 
  int64_t nr=0;
 
2026
  uchar *res=result_field->ptr;
 
2027
  longlong nr=0;
2012
2028
 
2013
2029
  if (!args[0]->maybe_null || !args[0]->is_null())
2014
2030
    nr=1;
2018
2034
 
2019
2035
void Item_sum_avg::reset_field()
2020
2036
{
2021
 
  unsigned char *res=result_field->ptr;
 
2037
  uchar *res=result_field->ptr;
2022
2038
  if (hybrid_type == DECIMAL_RESULT)
2023
2039
  {
2024
 
    int64_t tmp;
 
2040
    longlong tmp;
2025
2041
    my_decimal value, *arg_dec= args[0]->val_decimal(&value);
2026
2042
    if (args[0]->null_value)
2027
2043
    {
2039
2055
    double nr= args[0]->val_real();
2040
2056
 
2041
2057
    if (args[0]->null_value)
2042
 
      memset(res, 0, sizeof(double)+sizeof(int64_t));
 
2058
      bzero(res,sizeof(double)+sizeof(longlong));
2043
2059
    else
2044
2060
    {
2045
 
      int64_t tmp= 1;
 
2061
      longlong tmp= 1;
2046
2062
      float8store(res,nr);
2047
2063
      res+=sizeof(double);
2048
2064
      int8store(res,tmp);
2059
2075
 
2060
2076
void Item_sum_bit::update_field()
2061
2077
{
2062
 
  unsigned char *res=result_field->ptr;
 
2078
  uchar *res=result_field->ptr;
2063
2079
  bits= uint8korr(res);
2064
2080
  add();
2065
2081
  int8store(res, bits);
2094
2110
  else
2095
2111
  {
2096
2112
    double old_nr,nr;
2097
 
    unsigned char *res=result_field->ptr;
 
2113
    uchar *res=result_field->ptr;
2098
2114
 
2099
2115
    float8get(old_nr,res);
2100
2116
    nr= args[0]->val_real();
2110
2126
 
2111
2127
void Item_sum_count::update_field()
2112
2128
{
2113
 
  int64_t nr;
2114
 
  unsigned char *res=result_field->ptr;
 
2129
  longlong nr;
 
2130
  uchar *res=result_field->ptr;
2115
2131
 
2116
2132
  nr=sint8korr(res);
2117
2133
  if (!args[0]->maybe_null || !args[0]->is_null())
2122
2138
 
2123
2139
void Item_sum_avg::update_field()
2124
2140
{
2125
 
  int64_t field_count;
2126
 
  unsigned char *res=result_field->ptr;
 
2141
  longlong field_count;
 
2142
  uchar *res=result_field->ptr;
2127
2143
  if (hybrid_type == DECIMAL_RESULT)
2128
2144
  {
2129
2145
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2218
2234
void
2219
2235
Item_sum_hybrid::min_max_update_int_field()
2220
2236
{
2221
 
  int64_t nr,old_nr;
 
2237
  longlong nr,old_nr;
2222
2238
 
2223
2239
  old_nr=result_field->val_int();
2224
2240
  nr=args[0]->val_int();
2229
2245
    else
2230
2246
    {
2231
2247
      bool res=(unsigned_flag ?
2232
 
                (uint64_t) old_nr > (uint64_t) nr :
 
2248
                (ulonglong) old_nr > (ulonglong) nr :
2233
2249
                old_nr > nr);
2234
2250
      /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2235
2251
      if ((cmp_sign > 0) ^ (!res))
2295
2311
{
2296
2312
  // fix_fields() never calls for this Item
2297
2313
  double nr;
2298
 
  int64_t count;
2299
 
  unsigned char *res;
 
2314
  longlong count;
 
2315
  uchar *res;
2300
2316
 
2301
2317
  if (hybrid_type == DECIMAL_RESULT)
2302
2318
    return val_real_from_decimal();
2311
2327
}
2312
2328
 
2313
2329
 
2314
 
int64_t Item_avg_field::val_int()
 
2330
longlong Item_avg_field::val_int()
2315
2331
{
2316
 
  return (int64_t) rint(val_real());
 
2332
  return (longlong) rint(val_real());
2317
2333
}
2318
2334
 
2319
2335
 
2323
2339
  if (hybrid_type == REAL_RESULT)
2324
2340
    return val_decimal_from_real(dec_buf);
2325
2341
 
2326
 
  int64_t count= sint8korr(field->ptr + dec_bin_size);
 
2342
  longlong count= sint8korr(field->ptr + dec_bin_size);
2327
2343
  if ((null_value= !count))
2328
2344
    return 0;
2329
2345
 
2357
2373
  double nr;
2358
2374
  // fix_fields() never calls for this Item
2359
2375
  nr= Item_variance_field::val_real();
2360
 
  assert(nr >= 0.0);
 
2376
  DBUG_ASSERT(nr >= 0.0);
2361
2377
  return sqrt(nr);
2362
2378
}
2363
2379
 
2377
2393
  if (!dec)
2378
2394
    return 0;
2379
2395
  my_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2380
 
  assert(nr >= 0.0);
 
2396
  DBUG_ASSERT(nr >= 0.0);
2381
2397
  nr= sqrt(nr);
2382
2398
  double2my_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
2383
 
  my_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, false, dec_buf);
 
2399
  my_decimal_round(E_DEC_FATAL_ERROR, &tmp_dec, decimals, FALSE, dec_buf);
2384
2400
  return dec_buf;
2385
2401
}
2386
2402
 
2414
2430
    return val_real_from_decimal();
2415
2431
 
2416
2432
  double recurrence_s;
2417
 
  uint64_t count;
 
2433
  ulonglong count;
2418
2434
  float8get(recurrence_s, (field->ptr + sizeof(double)));
2419
2435
  count=sint8korr(field->ptr+sizeof(double)*2);
2420
2436
 
2429
2445
** COUNT(DISTINCT ...)
2430
2446
****************************************************************************/
2431
2447
 
2432
 
int simple_str_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
 
2448
int simple_str_key_cmp(void* arg, uchar* key1, uchar* key2)
2433
2449
{
2434
2450
  Field *f= (Field*) arg;
2435
2451
  return f->cmp(key1, key2);
2442
2458
  static
2443
2459
*/
2444
2460
 
2445
 
int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
 
2461
int composite_key_cmp(void* arg, uchar* key1, uchar* key2)
2446
2462
{
2447
2463
  Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2448
2464
  Field **field    = item->table->field;
2449
2465
  Field **field_end= field + item->table->s->fields;
2450
 
  uint32_t *lengths=item->field_lengths;
 
2466
  uint32 *lengths=item->field_lengths;
2451
2467
  for (; field < field_end; ++field)
2452
2468
  {
2453
2469
    Field* f = *field;
2461
2477
  return 0;
2462
2478
}
2463
2479
 
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)),
2470
 
                               void *arg)
 
2480
 
 
2481
C_MODE_START
 
2482
 
 
2483
static int count_distinct_walk(void *elem, element_count count, void *arg)
2471
2484
{
2472
 
  (*((uint64_t*)arg))++;
 
2485
  (*((ulonglong*)arg))++;
2473
2486
  return 0;
2474
2487
}
2475
2488
 
2476
 
#ifdef __cplusplus
2477
 
}
2478
 
#endif
2479
 
 
 
2489
C_MODE_END
2480
2490
 
2481
2491
 
2482
2492
void Item_sum_count_distinct::cleanup()
2483
2493
{
 
2494
  DBUG_ENTER("Item_sum_count_distinct::cleanup");
2484
2495
  Item_sum_int::cleanup();
2485
2496
 
2486
2497
  /* Free objects only if we own them. */
2493
2504
    */
2494
2505
    delete tree;
2495
2506
    tree= 0;
2496
 
    is_evaluated= false;
 
2507
    is_evaluated= FALSE;
2497
2508
    if (table)
2498
2509
    {
2499
 
      table->free_tmp_table(table->in_use);
 
2510
      free_tmp_table(table->in_use, table);
2500
2511
      table= 0;
2501
2512
    }
2502
2513
    delete tmp_table_param;
2503
2514
    tmp_table_param= 0;
2504
2515
  }
2505
 
  always_null= false;
2506
 
  return;
 
2516
  always_null= FALSE;
 
2517
  DBUG_VOID_RETURN;
2507
2518
}
2508
2519
 
2509
2520
 
2518
2529
  original= 0;
2519
2530
  force_copy_fields= 1;
2520
2531
  tree= 0;
2521
 
  is_evaluated= false;
 
2532
  is_evaluated= FALSE;
2522
2533
  tmp_table_param= 0;
2523
 
  always_null= false;
 
2534
  always_null= FALSE;
2524
2535
}
2525
2536
 
2526
2537
 
2537
2548
 
2538
2549
  /*
2539
2550
    Setup can be called twice for ROLLUP items. This is a bug.
2540
 
    Please add assert(tree == 0) here when it's fixed.
 
2551
    Please add DBUG_ASSERT(tree == 0) here when it's fixed.
2541
2552
    It's legal to call setup() more than once when in a subquery
2542
2553
  */
2543
2554
  if (tree || table || tmp_table_param)
2544
 
    return false;
 
2555
    return FALSE;
2545
2556
 
2546
2557
  if (!(tmp_table_param= new TMP_TABLE_PARAM))
2547
 
    return true;
 
2558
    return TRUE;
2548
2559
 
2549
2560
  /* Create a table with an unique key over all parameters */
2550
 
  for (uint32_t i=0; i < arg_count ; i++)
 
2561
  for (uint i=0; i < arg_count ; i++)
2551
2562
  {
2552
2563
    Item *item=args[i];
2553
2564
    if (list.push_back(item))
2554
 
      return true;                              // End of memory
 
2565
      return TRUE;                              // End of memory
2555
2566
    if (item->const_item() && item->is_null())
2556
2567
      always_null= 1;
2557
2568
  }
2558
2569
  if (always_null)
2559
 
    return false;
 
2570
    return FALSE;
2560
2571
  count_field_types(select_lex, tmp_table_param, list, 0);
2561
2572
  tmp_table_param->force_copy_fields= force_copy_fields;
2562
 
  assert(table == 0);
 
2573
  DBUG_ASSERT(table == 0);
 
2574
  /*
 
2575
    Make create_tmp_table() convert BIT columns to BIGINT.
 
2576
    This is needed because BIT fields store parts of their data in table's
 
2577
    null bits, and we don't have methods to compare two table records, which
 
2578
    is needed by Unique which is used when HEAP table is used.
 
2579
  */
 
2580
  {
 
2581
    List_iterator_fast<Item> li(list);
 
2582
    Item *item;
 
2583
    while ((item= li++))
 
2584
    {
 
2585
      if (item->type() == Item::FIELD_ITEM &&
 
2586
          ((Item_field*)item)->field->type() == FIELD_TYPE_BIT)
 
2587
        item->marker=4;
 
2588
    }
 
2589
  }
2563
2590
 
2564
 
  if (!(table= create_tmp_table(thd, tmp_table_param, list, (order_st*) 0, 1,
 
2591
  if (!(table= create_tmp_table(thd, tmp_table_param, list, (ORDER*) 0, 1,
2565
2592
                                0,
2566
2593
                                (select_lex->options | thd->options),
2567
2594
                                HA_POS_ERROR, (char*)"")))
2568
 
    return true;
 
2595
    return TRUE;
2569
2596
  table->file->extra(HA_EXTRA_NO_ROWS);         // Don't update rows
2570
2597
  table->no_rows=1;
2571
2598
 
2579
2606
    void* cmp_arg;
2580
2607
    Field **field= table->field;
2581
2608
    Field **field_end= field + table->s->fields;
2582
 
    bool all_binary= true;
 
2609
    bool all_binary= TRUE;
2583
2610
 
2584
2611
    for (tree_key_length= 0; field < field_end; ++field)
2585
2612
    {
2586
2613
      Field *f= *field;
2587
2614
      enum enum_field_types f_type= f->type();
2588
2615
      tree_key_length+= f->pack_length();
2589
 
      if (f_type == DRIZZLE_TYPE_VARCHAR)
 
2616
      if ((f_type == MYSQL_TYPE_VARCHAR) || (!f->binary() && (f_type == MYSQL_TYPE_STRING || f_type == MYSQL_TYPE_VAR_STRING)))
2590
2617
      {
2591
 
        all_binary= false;
 
2618
        all_binary= FALSE;
2592
2619
        break;
2593
2620
      }
2594
2621
    }
2613
2640
      }
2614
2641
      else
2615
2642
      {
2616
 
        uint32_t *length;
 
2643
        uint32 *length;
2617
2644
        compare_key= (qsort_cmp2) composite_key_cmp;
2618
2645
        cmp_arg= (void*) this;
2619
 
        field_lengths= (uint32_t*) thd->alloc(table->s->fields * sizeof(uint32_t));
 
2646
        field_lengths= (uint32*) thd->alloc(table->s->fields * sizeof(uint32));
2620
2647
        for (tree_key_length= 0, length= field_lengths, field= table->field;
2621
2648
             field < field_end; ++field, ++length)
2622
2649
        {
2625
2652
        }
2626
2653
      }
2627
2654
    }
2628
 
    assert(tree == 0);
 
2655
    DBUG_ASSERT(tree == 0);
2629
2656
    tree= new Unique(compare_key, cmp_arg, tree_key_length,
2630
2657
                     thd->variables.max_heap_table_size);
2631
2658
    /*
2634
2661
      but this has to be handled - otherwise someone can crash
2635
2662
      the server with a DoS attack
2636
2663
    */
2637
 
    is_evaluated= false;
 
2664
    is_evaluated= FALSE;
2638
2665
    if (! tree)
2639
 
      return true;
 
2666
      return TRUE;
2640
2667
  }
2641
 
  return false;
 
2668
  return FALSE;
2642
2669
}
2643
2670
 
2644
2671
 
2651
2678
void Item_sum_count_distinct::clear()
2652
2679
{
2653
2680
  /* tree and table can be both null only if always_null */
2654
 
  is_evaluated= false;
 
2681
  is_evaluated= FALSE;
2655
2682
  if (tree)
2656
2683
  {
2657
2684
    tree->reset();
2676
2703
    if ((*field)->is_real_null(0))
2677
2704
      return 0;                                 // Don't count NULL
2678
2705
 
2679
 
  is_evaluated= false;
 
2706
  is_evaluated= FALSE;
2680
2707
  if (tree)
2681
2708
  {
2682
2709
    /*
2689
2716
  }
2690
2717
  if ((error= table->file->ha_write_row(table->record[0])) &&
2691
2718
      table->file->is_fatal_error(error, HA_CHECK_DUP))
2692
 
    return true;
2693
 
  return false;
 
2719
    return TRUE;
 
2720
  return FALSE;
2694
2721
}
2695
2722
 
2696
2723
 
2697
 
int64_t Item_sum_count_distinct::val_int()
 
2724
longlong Item_sum_count_distinct::val_int()
2698
2725
{
2699
2726
  int error;
2700
 
  assert(fixed == 1);
 
2727
  DBUG_ASSERT(fixed == 1);
2701
2728
  if (!table)                                   // Empty query
2702
 
    return 0L;
 
2729
    return LL(0);
2703
2730
  if (tree)
2704
2731
  {
2705
2732
    if (is_evaluated)
2706
2733
      return count;
2707
2734
 
2708
2735
    if (tree->elements == 0)
2709
 
      return (int64_t) tree->elements_in_tree(); // everything fits in memory
 
2736
      return (longlong) tree->elements_in_tree(); // everything fits in memory
2710
2737
    count= 0;
2711
2738
    tree->walk(count_distinct_walk, (void*) &count);
2712
 
    is_evaluated= true;
2713
 
    return (int64_t) count;
 
2739
    is_evaluated= TRUE;
 
2740
    return (longlong) count;
2714
2741
  }
2715
2742
 
2716
2743
  error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2723
2750
  return table->file->stats.records;
2724
2751
}
2725
2752
 
 
2753
 
 
2754
/****************************************************************************
 
2755
** Functions to handle dynamic loadable aggregates
 
2756
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
 
2757
** Adapted for UDAs by: Andreas F. Bobak <bobak@relog.ch>.
 
2758
** Rewritten by: Monty.
 
2759
****************************************************************************/
 
2760
 
 
2761
#ifdef HAVE_DLOPEN
 
2762
 
 
2763
void Item_udf_sum::clear()
 
2764
{
 
2765
  DBUG_ENTER("Item_udf_sum::clear");
 
2766
  udf.clear();
 
2767
  DBUG_VOID_RETURN;
 
2768
}
 
2769
 
 
2770
bool Item_udf_sum::add()
 
2771
{
 
2772
  DBUG_ENTER("Item_udf_sum::add");
 
2773
  udf.add(&null_value);
 
2774
  DBUG_RETURN(0);
 
2775
}
 
2776
 
 
2777
void Item_udf_sum::cleanup()
 
2778
{
 
2779
  /*
 
2780
    udf_handler::cleanup() nicely handles case when we have not
 
2781
    original item but one created by copy_or_same() method.
 
2782
  */
 
2783
  udf.cleanup();
 
2784
  Item_sum::cleanup();
 
2785
}
 
2786
 
 
2787
 
 
2788
void Item_udf_sum::print(String *str, enum_query_type query_type)
 
2789
{
 
2790
  str->append(func_name());
 
2791
  str->append('(');
 
2792
  for (uint i=0 ; i < arg_count ; i++)
 
2793
  {
 
2794
    if (i)
 
2795
      str->append(',');
 
2796
    args[i]->print(str, query_type);
 
2797
  }
 
2798
  str->append(')');
 
2799
}
 
2800
 
 
2801
 
 
2802
Item *Item_sum_udf_float::copy_or_same(THD* thd)
 
2803
{
 
2804
  return new (thd->mem_root) Item_sum_udf_float(thd, this);
 
2805
}
 
2806
 
 
2807
double Item_sum_udf_float::val_real()
 
2808
{
 
2809
  DBUG_ASSERT(fixed == 1);
 
2810
  DBUG_ENTER("Item_sum_udf_float::val");
 
2811
  DBUG_PRINT("info",("result_type: %d  arg_count: %d",
 
2812
                     args[0]->result_type(), arg_count));
 
2813
  DBUG_RETURN(udf.val(&null_value));
 
2814
}
 
2815
 
 
2816
 
 
2817
String *Item_sum_udf_float::val_str(String *str)
 
2818
{
 
2819
  return val_string_from_real(str);
 
2820
}
 
2821
 
 
2822
 
 
2823
my_decimal *Item_sum_udf_float::val_decimal(my_decimal *dec)
 
2824
{
 
2825
  return val_decimal_from_real(dec);
 
2826
}
 
2827
 
 
2828
 
 
2829
String *Item_sum_udf_decimal::val_str(String *str)
 
2830
{
 
2831
  return val_string_from_decimal(str);
 
2832
}
 
2833
 
 
2834
 
 
2835
double Item_sum_udf_decimal::val_real()
 
2836
{
 
2837
  return val_real_from_decimal();
 
2838
}
 
2839
 
 
2840
 
 
2841
longlong Item_sum_udf_decimal::val_int()
 
2842
{
 
2843
  return val_int_from_decimal();
 
2844
}
 
2845
 
 
2846
 
 
2847
my_decimal *Item_sum_udf_decimal::val_decimal(my_decimal *dec_buf)
 
2848
{
 
2849
  DBUG_ASSERT(fixed == 1);
 
2850
  DBUG_ENTER("Item_func_udf_decimal::val_decimal");
 
2851
  DBUG_PRINT("info",("result_type: %d  arg_count: %d",
 
2852
                     args[0]->result_type(), arg_count));
 
2853
 
 
2854
  DBUG_RETURN(udf.val_decimal(&null_value, dec_buf));
 
2855
}
 
2856
 
 
2857
 
 
2858
Item *Item_sum_udf_decimal::copy_or_same(THD* thd)
 
2859
{
 
2860
  return new (thd->mem_root) Item_sum_udf_decimal(thd, this);
 
2861
}
 
2862
 
 
2863
 
 
2864
Item *Item_sum_udf_int::copy_or_same(THD* thd)
 
2865
{
 
2866
  return new (thd->mem_root) Item_sum_udf_int(thd, this);
 
2867
}
 
2868
 
 
2869
longlong Item_sum_udf_int::val_int()
 
2870
{
 
2871
  DBUG_ASSERT(fixed == 1);
 
2872
  DBUG_ENTER("Item_sum_udf_int::val_int");
 
2873
  DBUG_PRINT("info",("result_type: %d  arg_count: %d",
 
2874
                     args[0]->result_type(), arg_count));
 
2875
  DBUG_RETURN(udf.val_int(&null_value));
 
2876
}
 
2877
 
 
2878
 
 
2879
String *Item_sum_udf_int::val_str(String *str)
 
2880
{
 
2881
  return val_string_from_int(str);
 
2882
}
 
2883
 
 
2884
my_decimal *Item_sum_udf_int::val_decimal(my_decimal *dec)
 
2885
{
 
2886
  return val_decimal_from_int(dec);
 
2887
}
 
2888
 
 
2889
 
 
2890
/** Default max_length is max argument length. */
 
2891
 
 
2892
void Item_sum_udf_str::fix_length_and_dec()
 
2893
{
 
2894
  DBUG_ENTER("Item_sum_udf_str::fix_length_and_dec");
 
2895
  max_length=0;
 
2896
  for (uint i = 0; i < arg_count; i++)
 
2897
    set_if_bigger(max_length,args[i]->max_length);
 
2898
  DBUG_VOID_RETURN;
 
2899
}
 
2900
 
 
2901
 
 
2902
Item *Item_sum_udf_str::copy_or_same(THD* thd)
 
2903
{
 
2904
  return new (thd->mem_root) Item_sum_udf_str(thd, this);
 
2905
}
 
2906
 
 
2907
 
 
2908
my_decimal *Item_sum_udf_str::val_decimal(my_decimal *dec)
 
2909
{
 
2910
  return val_decimal_from_string(dec);
 
2911
}
 
2912
 
 
2913
String *Item_sum_udf_str::val_str(String *str)
 
2914
{
 
2915
  DBUG_ASSERT(fixed == 1);
 
2916
  DBUG_ENTER("Item_sum_udf_str::str");
 
2917
  String *res=udf.val_str(str,&str_value);
 
2918
  null_value = !res;
 
2919
  DBUG_RETURN(res);
 
2920
}
 
2921
 
 
2922
#endif /* HAVE_DLOPEN */
 
2923
 
 
2924
 
2726
2925
/*****************************************************************************
2727
2926
 GROUP_CONCAT function
2728
2927
 
2729
2928
 SQL SYNTAX:
2730
 
  GROUP_CONCAT([DISTINCT] expr,... [order_st BY col [ASC|DESC],...]
 
2929
  GROUP_CONCAT([DISTINCT] expr,... [ORDER BY col [ASC|DESC],...]
2731
2930
    [SEPARATOR str_const])
2732
2931
 
2733
2932
 concat of values from "group by" operation
2734
2933
 
2735
2934
 BUGS
2736
 
   Blobs doesn't work with DISTINCT or order_st BY
 
2935
   Blobs doesn't work with DISTINCT or ORDER BY
2737
2936
*****************************************************************************/
2738
2937
 
2739
2938
 
2742
2941
  @note
2743
2942
       
2744
2943
     GROUP_CONCAT([DISTINCT] expr [,expr ...]
2745
 
              [order_st BY {unsigned_integer | col_name | expr}
 
2944
              [ORDER BY {unsigned_integer | col_name | expr}
2746
2945
                  [ASC | DESC] [,col_name ...]]
2747
2946
              [SEPARATOR str_val])
2748
2947
 
2756
2955
                                       const void* key2)
2757
2956
{
2758
2957
  Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
2759
 
  Table *table= item_func->table;
 
2958
  TABLE *table= item_func->table;
2760
2959
 
2761
 
  for (uint32_t i= 0; i < item_func->arg_count_field; i++)
 
2960
  for (uint i= 0; i < item_func->arg_count_field; i++)
2762
2961
  {
2763
2962
    Item *item= item_func->args[i];
2764
2963
    /* 
2774
2973
    */
2775
2974
    Field *field= item->get_tmp_table_field();
2776
2975
    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)))
 
2976
    uint offset= field->offset(field->table->record[0])-table->s->null_bytes;
 
2977
    if((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset)))
2779
2978
      return res;
2780
2979
  }
2781
2980
  return 0;
2783
2982
 
2784
2983
 
2785
2984
/**
2786
 
  function of sort for syntax: GROUP_CONCAT(expr,... order_st BY col,... )
 
2985
  function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
2787
2986
*/
2788
2987
 
2789
2988
int group_concat_key_cmp_with_order(void* arg, const void* key1, 
2790
2989
                                    const void* key2)
2791
2990
{
2792
2991
  Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2793
 
  order_st **order_item, **end;
2794
 
  Table *table= grp_item->table;
 
2992
  ORDER **order_item, **end;
 
2993
  TABLE *table= grp_item->table;
2795
2994
 
2796
2995
  for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2797
2996
       order_item < end;
2811
3010
    if (field && !item->const_item())
2812
3011
    {
2813
3012
      int res;
2814
 
      uint32_t offset= (field->offset(field->table->record[0]) -
 
3013
      uint offset= (field->offset(field->table->record[0]) -
2815
3014
                    table->s->null_bytes);
2816
 
      if ((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
 
3015
      if ((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset)))
2817
3016
        return (*order_item)->asc ? res : -res;
2818
3017
    }
2819
3018
  }
2830
3029
  Append data from current leaf to item->result.
2831
3030
*/
2832
3031
 
2833
 
int dump_leaf_key(unsigned char* key, element_count count __attribute__((unused)),
 
3032
int dump_leaf_key(uchar* key, element_count count __attribute__((unused)),
2834
3033
                  Item_func_group_concat *item)
2835
3034
{
2836
 
  Table *table= item->table;
 
3035
  TABLE *table= item->table;
2837
3036
  String tmp((char *)table->record[1], table->s->reclength,
2838
3037
             default_charset_info);
2839
3038
  String tmp2;
2840
3039
  String *result= &item->result;
2841
3040
  Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
2842
 
  uint32_t old_length= result->length();
 
3041
  uint old_length= result->length();
2843
3042
 
2844
3043
  if (item->no_appended)
2845
 
    item->no_appended= false;
 
3044
    item->no_appended= FALSE;
2846
3045
  else
2847
3046
    result->append(*item->separator);
2848
3047
 
2861
3060
        because it contains both order and arg list fields.
2862
3061
      */
2863
3062
      Field *field= (*arg)->get_tmp_table_field();
2864
 
      uint32_t offset= (field->offset(field->table->record[0]) -
 
3063
      uint offset= (field->offset(field->table->record[0]) -
2865
3064
                    table->s->null_bytes);
2866
 
      assert(offset < table->s->reclength);
 
3065
      DBUG_ASSERT(offset < table->s->reclength);
2867
3066
      res= field->val_str(&tmp, key + offset);
2868
3067
    }
2869
3068
    else
2876
3075
  if (result->length() > item->max_length)
2877
3076
  {
2878
3077
    int well_formed_error;
2879
 
    const CHARSET_INFO * const cs= item->collation.collation;
 
3078
    CHARSET_INFO *cs= item->collation.collation;
2880
3079
    const char *ptr= result->ptr();
2881
 
    uint32_t add_length;
 
3080
    uint add_length;
2882
3081
    /*
2883
3082
      It's ok to use item->result.length() as the fourth argument
2884
3083
      as this is never used to limit the length of the data.
2891
3090
                                          &well_formed_error);
2892
3091
    result->length(old_length + add_length);
2893
3092
    item->count_cut_values++;
2894
 
    item->warning_for_row= true;
 
3093
    item->warning_for_row= TRUE;
2895
3094
    return 1;
2896
3095
  }
2897
3096
  return 0;
2918
3117
   arg_count_field(select_list->elements),
2919
3118
   count_cut_values(0),
2920
3119
   distinct(distinct_arg),
2921
 
   warning_for_row(false),
 
3120
   warning_for_row(FALSE),
2922
3121
   force_copy_fields(0), original(0)
2923
3122
{
2924
3123
  Item *item_select;
2925
3124
  Item **arg_ptr;
2926
3125
 
2927
 
  quick_group= false;
 
3126
  quick_group= FALSE;
2928
3127
  arg_count= arg_count_field + arg_count_order;
2929
3128
 
2930
3129
  /*
2934
3133
    order - arg_count_order
2935
3134
  */
2936
3135
  if (!(args= (Item**) sql_alloc(sizeof(Item*) * arg_count +
2937
 
                                 sizeof(order_st*)*arg_count_order)))
 
3136
                                 sizeof(ORDER*)*arg_count_order)))
2938
3137
    return;
2939
3138
 
2940
 
  order= (order_st**)(args + arg_count);
 
3139
  order= (ORDER**)(args + arg_count);
2941
3140
 
2942
3141
  /* fill args items of show and sort */
2943
3142
  List_iterator_fast<Item> li(*select_list);
2947
3146
 
2948
3147
  if (arg_count_order)
2949
3148
  {
2950
 
    order_st **order_ptr= order;
2951
 
    for (order_st *order_item= (order_st*) order_list->first;
 
3149
    ORDER **order_ptr= order;
 
3150
    for (ORDER *order_item= (ORDER*) order_list->first;
2952
3151
         order_item != NULL;
2953
3152
         order_item= order_item->next)
2954
3153
    {
2988
3187
 
2989
3188
void Item_func_group_concat::cleanup()
2990
3189
{
 
3190
  DBUG_ENTER("Item_func_group_concat::cleanup");
2991
3191
  Item_sum::cleanup();
2992
3192
 
2993
3193
  /* Adjust warning message to include total number of cut values */
2994
3194
  if (warning)
2995
3195
  {
2996
 
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
 
3196
    char warn_buff[MYSQL_ERRMSG_SIZE];
2997
3197
    sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
2998
3198
    warning->set_msg(current_thd, warn_buff);
2999
3199
    warning= 0;
3010
3210
    if (table)
3011
3211
    {
3012
3212
      THD *thd= table->in_use;
3013
 
      table->free_tmp_table(thd);
 
3213
      free_tmp_table(thd, table);
3014
3214
      table= 0;
3015
3215
      if (tree)
3016
3216
      {
3024
3224
      }
3025
3225
      if (warning)
3026
3226
      {
3027
 
        char warn_buff[DRIZZLE_ERRMSG_SIZE];
 
3227
        char warn_buff[MYSQL_ERRMSG_SIZE];
3028
3228
        sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3029
3229
        warning->set_msg(thd, warn_buff);
3030
3230
        warning= 0;
3031
3231
      }
3032
3232
    }
3033
 
    assert(tree == 0 && warning == 0);
 
3233
    DBUG_ASSERT(tree == 0 && warning == 0);
3034
3234
  }
3035
 
  return;
 
3235
  DBUG_VOID_RETURN;
3036
3236
}
3037
3237
 
3038
3238
 
3046
3246
{
3047
3247
  result.length(0);
3048
3248
  result.copy();
3049
 
  null_value= true;
3050
 
  warning_for_row= false;
3051
 
  no_appended= true;
 
3249
  null_value= TRUE;
 
3250
  warning_for_row= FALSE;
 
3251
  no_appended= TRUE;
3052
3252
  if (tree)
3053
3253
    reset_tree(tree);
3054
3254
  if (distinct)
3064
3264
  copy_fields(tmp_table_param);
3065
3265
  copy_funcs(tmp_table_param->items_to_copy);
3066
3266
 
3067
 
  for (uint32_t i= 0; i < arg_count_field; i++)
 
3267
  for (uint i= 0; i < arg_count_field; i++)
3068
3268
  {
3069
3269
    Item *show_item= args[i];
3070
3270
    if (!show_item->const_item())
3071
3271
    {
3072
3272
      Field *f= show_item->get_tmp_table_field();
3073
 
      if (f->is_null_in_record((const unsigned char*) table->record[0]))
 
3273
      if (f->is_null_in_record((const uchar*) table->record[0]))
3074
3274
        return 0;                               // Skip row if it contains null
3075
3275
    }
3076
3276
  }
3077
3277
 
3078
 
  null_value= false;
3079
 
  bool row_eligible= true;
 
3278
  null_value= FALSE;
 
3279
  bool row_eligible= TRUE;
3080
3280
 
3081
3281
  if (distinct) 
3082
3282
  {
3083
3283
    /* Filter out duplicate rows. */
3084
 
    uint32_t count= unique_filter->elements_in_tree();
 
3284
    uint count= unique_filter->elements_in_tree();
3085
3285
    unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3086
3286
    if (count == unique_filter->elements_in_tree())
3087
 
      row_eligible= false;
 
3287
      row_eligible= FALSE;
3088
3288
  }
3089
3289
 
3090
3290
  TREE_ELEMENT *el= 0;                          // Only for safety
3107
3307
bool
3108
3308
Item_func_group_concat::fix_fields(THD *thd, Item **ref)
3109
3309
{
3110
 
  uint32_t i;                       /* for loop variable */
3111
 
  assert(fixed == 0);
 
3310
  uint i;                       /* for loop variable */
 
3311
  DBUG_ASSERT(fixed == 0);
3112
3312
 
3113
3313
  if (init_sum_func_check(thd))
3114
 
    return true;
 
3314
    return TRUE;
3115
3315
 
3116
3316
  maybe_null= 1;
3117
3317
 
3118
3318
  /*
3119
 
    Fix fields for select list and order_st clause
 
3319
    Fix fields for select list and ORDER clause
3120
3320
  */
3121
3321
 
3122
3322
  for (i=0 ; i < arg_count ; i++)
3124
3324
    if ((!args[i]->fixed &&
3125
3325
         args[i]->fix_fields(thd, args + i)) ||
3126
3326
        args[i]->check_cols(1))
3127
 
      return true;
 
3327
      return TRUE;
3128
3328
  }
3129
3329
 
3130
3330
  if (agg_item_charsets(collation, func_name(),
3139
3339
  null_value= 1;
3140
3340
  max_length= thd->variables.group_concat_max_len;
3141
3341
 
3142
 
  uint32_t offset;
 
3342
  uint32 offset;
3143
3343
  if (separator->needs_conversion(separator->length(), separator->charset(),
3144
3344
                                  collation.collation, &offset))
3145
3345
  {
3146
 
    uint32_t buflen= collation.collation->mbmaxlen * separator->length();
3147
 
    uint32_t errors, conv_length;
 
3346
    uint32 buflen= collation.collation->mbmaxlen * separator->length();
 
3347
    uint errors, conv_length;
3148
3348
    char *buf;
3149
3349
    String *new_separator;
3150
3350
 
3151
 
    if (!(buf= (char*) thd->alloc(buflen)) ||
3152
 
        !(new_separator= new(thd->mem_root)
 
3351
    if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
 
3352
        !(new_separator= new(thd->stmt_arena->mem_root)
3153
3353
                           String(buf, buflen, collation.collation)))
3154
 
      return true;
 
3354
      return TRUE;
3155
3355
    
3156
3356
    conv_length= copy_and_convert(buf, buflen, collation.collation,
3157
3357
                                  separator->ptr(), separator->length(),
3161
3361
  }
3162
3362
 
3163
3363
  if (check_sum_func(thd, ref))
3164
 
    return true;
 
3364
    return TRUE;
3165
3365
 
3166
3366
  fixed= 1;
3167
 
  return false;
 
3367
  return FALSE;
3168
3368
}
3169
3369
 
3170
3370
 
3172
3372
{
3173
3373
  List<Item> list;
3174
3374
  SELECT_LEX *select_lex= thd->lex->current_select;
 
3375
  DBUG_ENTER("Item_func_group_concat::setup");
3175
3376
 
3176
3377
  /*
3177
3378
    Currently setup() can be called twice. Please add
3178
3379
    assertion here when this is fixed.
3179
3380
  */
3180
3381
  if (table || tree)
3181
 
    return(false);
 
3382
    DBUG_RETURN(FALSE);
3182
3383
 
3183
3384
  if (!(tmp_table_param= new TMP_TABLE_PARAM))
3184
 
    return(true);
 
3385
    DBUG_RETURN(TRUE);
3185
3386
 
3186
3387
  /* We'll convert all blobs to varchar fields in the temporary table */
3187
3388
  tmp_table_param->convert_blob_length= max_length *
3188
3389
                                        collation.collation->mbmaxlen;
3189
3390
  /* Push all not constant fields to the list and create a temp table */
3190
3391
  always_null= 0;
3191
 
  for (uint32_t i= 0; i < arg_count_field; i++)
 
3392
  for (uint i= 0; i < arg_count_field; i++)
3192
3393
  {
3193
3394
    Item *item= args[i];
3194
3395
    if (list.push_back(item))
3195
 
      return(true);
 
3396
      DBUG_RETURN(TRUE);
3196
3397
    if (item->const_item())
3197
3398
    {
3198
3399
      if (item->is_null())
3199
3400
      {
3200
3401
        always_null= 1;
3201
 
        return(false);
 
3402
        DBUG_RETURN(FALSE);
3202
3403
      }
3203
3404
    }
3204
3405
  }
3205
3406
 
3206
3407
  List<Item> all_fields(list);
3207
3408
  /*
3208
 
    Try to find every order_st expression in the list of GROUP_CONCAT
 
3409
    Try to find every ORDER expression in the list of GROUP_CONCAT
3209
3410
    arguments. If an expression is not found, prepend it to
3210
3411
    "all_fields". The resulting field list is used as input to create
3211
3412
    tmp table columns.
3212
3413
  */
3213
3414
  if (arg_count_order &&
3214
3415
      setup_order(thd, args, context->table_list, list, all_fields, *order))
3215
 
    return(true);
 
3416
    DBUG_RETURN(TRUE);
3216
3417
 
3217
3418
  count_field_types(select_lex, tmp_table_param, all_fields, 0);
3218
3419
  tmp_table_param->force_copy_fields= force_copy_fields;
3219
 
  assert(table == 0);
 
3420
  DBUG_ASSERT(table == 0);
3220
3421
  if (arg_count_order > 0 || distinct)
3221
3422
  {
3222
3423
    /*
3223
3424
      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
 
3425
      if we are to store them in TREE objects used for ORDER BY and
3225
3426
      DISTINCT. This leads to truncation if the BLOB's size exceeds
3226
3427
      Field_varstring::MAX_SIZE.
3227
3428
    */
3228
3429
    set_if_smaller(tmp_table_param->convert_blob_length, 
3229
3430
                   Field_varstring::MAX_SIZE);
 
3431
 
 
3432
    /*
 
3433
      Force the create_tmp_table() to convert BIT columns to INT
 
3434
      as we cannot compare two table records containg BIT fields
 
3435
      stored in the the tree used for distinct/order by.
 
3436
      Moreover we don't even save in the tree record null bits 
 
3437
      where BIT fields store parts of their data.
 
3438
    */
 
3439
    List_iterator_fast<Item> li(all_fields);
 
3440
    Item *item;
 
3441
    while ((item= li++))
 
3442
    {
 
3443
      if (item->type() == Item::FIELD_ITEM && 
 
3444
          ((Item_field*) item)->field->type() == FIELD_TYPE_BIT)
 
3445
        item->marker= 4;
 
3446
    }
3230
3447
  }
3231
3448
 
3232
3449
  /*
3233
3450
    We have to create a temporary table to get descriptions of fields
3234
3451
    (types, sizes and so on).
3235
3452
 
3236
 
    Note that in the table, we first have the order_st BY fields, then the
 
3453
    Note that in the table, we first have the ORDER BY fields, then the
3237
3454
    field list.
3238
3455
  */
3239
3456
  if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
3240
 
                                (order_st*) 0, 0, true,
 
3457
                                (ORDER*) 0, 0, TRUE,
3241
3458
                                (select_lex->options | thd->options),
3242
3459
                                HA_POS_ERROR, (char*) "")))
3243
 
    return(true);
 
3460
    DBUG_RETURN(TRUE);
3244
3461
  table->file->extra(HA_EXTRA_NO_ROWS);
3245
3462
  table->no_rows= 1;
3246
3463
 
3249
3466
     Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3250
3467
     the row is not added to the result.
3251
3468
  */
3252
 
  uint32_t tree_key_length= table->s->reclength - table->s->null_bytes;
 
3469
  uint tree_key_length= table->s->reclength - table->s->null_bytes;
3253
3470
 
3254
3471
  if (arg_count_order)
3255
3472
  {
3256
3473
    tree= &tree_base;
3257
3474
    /*
3258
3475
      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
 
3476
      syntax of this function). If there is no ORDER BY clause, we don't
3260
3477
      create this tree.
3261
3478
    */
3262
 
    init_tree(tree, (uint) cmin(thd->variables.max_heap_table_size,
 
3479
    init_tree(tree, (uint) min(thd->variables.max_heap_table_size,
3263
3480
                               thd->variables.sortbuff_size/16), 0,
3264
3481
              tree_key_length, 
3265
3482
              group_concat_key_cmp_with_order , 0, NULL, (void*) this);
3271
3488
                              tree_key_length,
3272
3489
                              thd->variables.max_heap_table_size);
3273
3490
  
3274
 
  return(false);
 
3491
  DBUG_RETURN(FALSE);
3275
3492
}
3276
3493
 
3277
3494
 
3287
3504
}
3288
3505
 
3289
3506
 
3290
 
String* Item_func_group_concat::val_str(String* str __attribute__((unused)))
 
3507
String* Item_func_group_concat::val_str(String* str)
3291
3508
{
3292
 
  assert(fixed == 1);
 
3509
  DBUG_ASSERT(fixed == 1);
3293
3510
  if (null_value)
3294
3511
    return 0;
3295
3512
  if (no_appended && tree)
3302
3519
      ER_CUT_VALUE_GROUP_CONCAT needs an argument, but this gets set in
3303
3520
      Item_func_group_concat::cleanup().
3304
3521
    */
3305
 
    assert(table);
3306
 
    warning= push_warning(table->in_use, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
3522
    DBUG_ASSERT(table);
 
3523
    warning= push_warning(table->in_use, MYSQL_ERROR::WARN_LEVEL_WARN,
3307
3524
                          ER_CUT_VALUE_GROUP_CONCAT,
3308
3525
                          ER(ER_CUT_VALUE_GROUP_CONCAT));
3309
3526
  }
3316
3533
  str->append(STRING_WITH_LEN("group_concat("));
3317
3534
  if (distinct)
3318
3535
    str->append(STRING_WITH_LEN("distinct "));
3319
 
  for (uint32_t i= 0; i < arg_count_field; i++)
 
3536
  for (uint i= 0; i < arg_count_field; i++)
3320
3537
  {
3321
3538
    if (i)
3322
3539
      str->append(',');
3325
3542
  if (arg_count_order)
3326
3543
  {
3327
3544
    str->append(STRING_WITH_LEN(" order by "));
3328
 
    for (uint32_t i= 0 ; i < arg_count_order ; i++)
 
3545
    for (uint i= 0 ; i < arg_count_order ; i++)
3329
3546
    {
3330
3547
      if (i)
3331
3548
        str->append(',');