~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 18:10:38 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: monty@inaugust.com-20080705181038-0ih0nnamu5qrut0y
Fixed prototypes. Cleaned define a little bit.

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) {
1579
1594
                             &end_not_used, &err_not_used) : 0.0);
1580
1595
  }
1581
1596
  case INT_RESULT:
 
1597
    if (unsigned_flag)
 
1598
      return ulonglong2double(sum_int);
1582
1599
    return (double) sum_int;
1583
1600
  case DECIMAL_RESULT:
1584
1601
    my_decimal2double(E_DEC_FATAL_ERROR, &sum_dec, &sum);
1588
1605
  case ROW_RESULT:
1589
1606
  default:
1590
1607
    // This case should never be choosen
1591
 
    assert(0);
 
1608
    DBUG_ASSERT(0);
1592
1609
    return 0;
1593
1610
  }
1594
1611
}
1595
1612
 
1596
 
int64_t Item_sum_hybrid::val_int()
 
1613
longlong Item_sum_hybrid::val_int()
1597
1614
{
1598
 
  assert(fixed == 1);
 
1615
  DBUG_ASSERT(fixed == 1);
1599
1616
  if (null_value)
1600
1617
    return 0;
1601
1618
  switch (hybrid_type) {
1603
1620
    return sum_int;
1604
1621
  case DECIMAL_RESULT:
1605
1622
  {
1606
 
    int64_t result;
 
1623
    longlong result;
1607
1624
    my_decimal2int(E_DEC_FATAL_ERROR, &sum_dec, unsigned_flag, &result);
1608
1625
    return sum_int;
1609
1626
  }
1610
1627
  default:
1611
 
    return (int64_t) rint(Item_sum_hybrid::val_real());
 
1628
    return (longlong) rint(Item_sum_hybrid::val_real());
1612
1629
  }
1613
1630
}
1614
1631
 
1615
1632
 
1616
1633
my_decimal *Item_sum_hybrid::val_decimal(my_decimal *val)
1617
1634
{
1618
 
  assert(fixed == 1);
 
1635
  DBUG_ASSERT(fixed == 1);
1619
1636
  if (null_value)
1620
1637
    return 0;
1621
1638
  switch (hybrid_type) {
1634
1651
  case ROW_RESULT:
1635
1652
  default:
1636
1653
    // This case should never be choosen
1637
 
    assert(0);
 
1654
    DBUG_ASSERT(0);
1638
1655
    break;
1639
1656
  }
1640
1657
  return val;                                   // Keep compiler happy
1644
1661
String *
1645
1662
Item_sum_hybrid::val_str(String *str)
1646
1663
{
1647
 
  assert(fixed == 1);
 
1664
  DBUG_ASSERT(fixed == 1);
1648
1665
  if (null_value)
1649
1666
    return 0;
1650
1667
  switch (hybrid_type) {
1662
1679
  case ROW_RESULT:
1663
1680
  default:
1664
1681
    // This case should never be choosen
1665
 
    assert(0);
 
1682
    DBUG_ASSERT(0);
1666
1683
    break;
1667
1684
  }
1668
1685
  return str;                                   // Keep compiler happy
1671
1688
 
1672
1689
void Item_sum_hybrid::cleanup()
1673
1690
{
 
1691
  DBUG_ENTER("Item_sum_hybrid::cleanup");
1674
1692
  Item_sum::cleanup();
1675
 
  forced_const= false;
 
1693
  forced_const= FALSE;
1676
1694
 
1677
1695
  /*
1678
1696
    by default it is TRUE to avoid TRUE reporting by
1681
1699
    no_rows_in_result() set it to FALSE if was not results found.
1682
1700
    If some results found it will be left unchanged.
1683
1701
  */
1684
 
  was_values= true;
1685
 
  return;
 
1702
  was_values= TRUE;
 
1703
  DBUG_VOID_RETURN;
1686
1704
}
1687
1705
 
1688
1706
void Item_sum_hybrid::no_rows_in_result()
1689
1707
{
1690
 
  was_values= false;
 
1708
  was_values= FALSE;
1691
1709
  clear();
1692
1710
}
1693
1711
 
1714
1732
  break;
1715
1733
  case INT_RESULT:
1716
1734
  {
1717
 
    int64_t nr=args[0]->val_int();
 
1735
    longlong nr=args[0]->val_int();
1718
1736
    if (!args[0]->null_value && (null_value ||
1719
1737
                                 (unsigned_flag && 
1720
 
                                  (uint64_t) nr < (uint64_t) sum_int) ||
 
1738
                                  (ulonglong) nr < (ulonglong) sum_int) ||
1721
1739
                                 (!unsigned_flag && nr < sum_int)))
1722
1740
    {
1723
1741
      sum_int=nr;
1749
1767
  case ROW_RESULT:
1750
1768
  default:
1751
1769
    // This case should never be choosen
1752
 
    assert(0);
 
1770
    DBUG_ASSERT(0);
1753
1771
    break;
1754
1772
  }
1755
1773
  return 0;
1778
1796
  break;
1779
1797
  case INT_RESULT:
1780
1798
  {
1781
 
    int64_t nr=args[0]->val_int();
 
1799
    longlong nr=args[0]->val_int();
1782
1800
    if (!args[0]->null_value && (null_value ||
1783
1801
                                 (unsigned_flag && 
1784
 
                                  (uint64_t) nr > (uint64_t) sum_int) ||
 
1802
                                  (ulonglong) nr > (ulonglong) sum_int) ||
1785
1803
                                 (!unsigned_flag && nr > sum_int)))
1786
1804
    {
1787
1805
      sum_int=nr;
1813
1831
  case ROW_RESULT:
1814
1832
  default:
1815
1833
    // This case should never be choosen
1816
 
    assert(0);
 
1834
    DBUG_ASSERT(0);
1817
1835
    break;
1818
1836
  }
1819
1837
  return 0;
1822
1840
 
1823
1841
/* bit_or and bit_and */
1824
1842
 
1825
 
int64_t Item_sum_bit::val_int()
 
1843
longlong Item_sum_bit::val_int()
1826
1844
{
1827
 
  assert(fixed == 1);
1828
 
  return (int64_t) bits;
 
1845
  DBUG_ASSERT(fixed == 1);
 
1846
  return (longlong) bits;
1829
1847
}
1830
1848
 
1831
1849
 
1842
1860
 
1843
1861
bool Item_sum_or::add()
1844
1862
{
1845
 
  uint64_t value= (uint64_t) args[0]->val_int();
 
1863
  ulonglong value= (ulonglong) args[0]->val_int();
1846
1864
  if (!args[0]->null_value)
1847
1865
    bits|=value;
1848
1866
  return 0;
1856
1874
 
1857
1875
bool Item_sum_xor::add()
1858
1876
{
1859
 
  uint64_t value= (uint64_t) args[0]->val_int();
 
1877
  ulonglong value= (ulonglong) args[0]->val_int();
1860
1878
  if (!args[0]->null_value)
1861
1879
    bits^=value;
1862
1880
  return 0;
1870
1888
 
1871
1889
bool Item_sum_and::add()
1872
1890
{
1873
 
  uint64_t value= (uint64_t) args[0]->val_int();
 
1891
  ulonglong value= (ulonglong) args[0]->val_int();
1874
1892
  if (!args[0]->null_value)
1875
1893
    bits&=value;
1876
1894
  return 0;
1883
1901
void Item_sum_num::reset_field()
1884
1902
{
1885
1903
  double nr= args[0]->val_real();
1886
 
  unsigned char *res=result_field->ptr;
 
1904
  uchar *res=result_field->ptr;
1887
1905
 
1888
1906
  if (maybe_null)
1889
1907
  {
1922
1940
  }
1923
1941
  case INT_RESULT:
1924
1942
  {
1925
 
    int64_t nr=args[0]->val_int();
 
1943
    longlong nr=args[0]->val_int();
1926
1944
 
1927
1945
    if (maybe_null)
1928
1946
    {
1976
1994
  }
1977
1995
  case ROW_RESULT:
1978
1996
  default:
1979
 
    assert(0);
 
1997
    DBUG_ASSERT(0);
1980
1998
  }
1981
1999
}
1982
2000
 
1992
2010
  }
1993
2011
  else
1994
2012
  {
1995
 
    assert(hybrid_type == REAL_RESULT);
 
2013
    DBUG_ASSERT(hybrid_type == REAL_RESULT);
1996
2014
    double nr= args[0]->val_real();                     // Nulls also return 0
1997
2015
    float8store(result_field->ptr, nr);
1998
2016
  }
2005
2023
 
2006
2024
void Item_sum_count::reset_field()
2007
2025
{
2008
 
  unsigned char *res=result_field->ptr;
2009
 
  int64_t nr=0;
 
2026
  uchar *res=result_field->ptr;
 
2027
  longlong nr=0;
2010
2028
 
2011
2029
  if (!args[0]->maybe_null || !args[0]->is_null())
2012
2030
    nr=1;
2016
2034
 
2017
2035
void Item_sum_avg::reset_field()
2018
2036
{
2019
 
  unsigned char *res=result_field->ptr;
 
2037
  uchar *res=result_field->ptr;
2020
2038
  if (hybrid_type == DECIMAL_RESULT)
2021
2039
  {
2022
 
    int64_t tmp;
 
2040
    longlong tmp;
2023
2041
    my_decimal value, *arg_dec= args[0]->val_decimal(&value);
2024
2042
    if (args[0]->null_value)
2025
2043
    {
2037
2055
    double nr= args[0]->val_real();
2038
2056
 
2039
2057
    if (args[0]->null_value)
2040
 
      memset(res, 0, sizeof(double)+sizeof(int64_t));
 
2058
      bzero(res,sizeof(double)+sizeof(longlong));
2041
2059
    else
2042
2060
    {
2043
 
      int64_t tmp= 1;
 
2061
      longlong tmp= 1;
2044
2062
      float8store(res,nr);
2045
2063
      res+=sizeof(double);
2046
2064
      int8store(res,tmp);
2057
2075
 
2058
2076
void Item_sum_bit::update_field()
2059
2077
{
2060
 
  unsigned char *res=result_field->ptr;
 
2078
  uchar *res=result_field->ptr;
2061
2079
  bits= uint8korr(res);
2062
2080
  add();
2063
2081
  int8store(res, bits);
2092
2110
  else
2093
2111
  {
2094
2112
    double old_nr,nr;
2095
 
    unsigned char *res=result_field->ptr;
 
2113
    uchar *res=result_field->ptr;
2096
2114
 
2097
2115
    float8get(old_nr,res);
2098
2116
    nr= args[0]->val_real();
2108
2126
 
2109
2127
void Item_sum_count::update_field()
2110
2128
{
2111
 
  int64_t nr;
2112
 
  unsigned char *res=result_field->ptr;
 
2129
  longlong nr;
 
2130
  uchar *res=result_field->ptr;
2113
2131
 
2114
2132
  nr=sint8korr(res);
2115
2133
  if (!args[0]->maybe_null || !args[0]->is_null())
2120
2138
 
2121
2139
void Item_sum_avg::update_field()
2122
2140
{
2123
 
  int64_t field_count;
2124
 
  unsigned char *res=result_field->ptr;
 
2141
  longlong field_count;
 
2142
  uchar *res=result_field->ptr;
2125
2143
  if (hybrid_type == DECIMAL_RESULT)
2126
2144
  {
2127
2145
    my_decimal value, *arg_val= args[0]->val_decimal(&value);
2216
2234
void
2217
2235
Item_sum_hybrid::min_max_update_int_field()
2218
2236
{
2219
 
  int64_t nr,old_nr;
 
2237
  longlong nr,old_nr;
2220
2238
 
2221
2239
  old_nr=result_field->val_int();
2222
2240
  nr=args[0]->val_int();
2227
2245
    else
2228
2246
    {
2229
2247
      bool res=(unsigned_flag ?
2230
 
                (uint64_t) old_nr > (uint64_t) nr :
 
2248
                (ulonglong) old_nr > (ulonglong) nr :
2231
2249
                old_nr > nr);
2232
2250
      /* (cmp_sign > 0 && res) || (!(cmp_sign > 0) && !res) */
2233
2251
      if ((cmp_sign > 0) ^ (!res))
2293
2311
{
2294
2312
  // fix_fields() never calls for this Item
2295
2313
  double nr;
2296
 
  int64_t count;
2297
 
  unsigned char *res;
 
2314
  longlong count;
 
2315
  uchar *res;
2298
2316
 
2299
2317
  if (hybrid_type == DECIMAL_RESULT)
2300
2318
    return val_real_from_decimal();
2309
2327
}
2310
2328
 
2311
2329
 
2312
 
int64_t Item_avg_field::val_int()
 
2330
longlong Item_avg_field::val_int()
2313
2331
{
2314
 
  return (int64_t) rint(val_real());
 
2332
  return (longlong) rint(val_real());
2315
2333
}
2316
2334
 
2317
2335
 
2321
2339
  if (hybrid_type == REAL_RESULT)
2322
2340
    return val_decimal_from_real(dec_buf);
2323
2341
 
2324
 
  int64_t count= sint8korr(field->ptr + dec_bin_size);
 
2342
  longlong count= sint8korr(field->ptr + dec_bin_size);
2325
2343
  if ((null_value= !count))
2326
2344
    return 0;
2327
2345
 
2355
2373
  double nr;
2356
2374
  // fix_fields() never calls for this Item
2357
2375
  nr= Item_variance_field::val_real();
2358
 
  assert(nr >= 0.0);
 
2376
  DBUG_ASSERT(nr >= 0.0);
2359
2377
  return sqrt(nr);
2360
2378
}
2361
2379
 
2375
2393
  if (!dec)
2376
2394
    return 0;
2377
2395
  my_decimal2double(E_DEC_FATAL_ERROR, dec, &nr);
2378
 
  assert(nr >= 0.0);
 
2396
  DBUG_ASSERT(nr >= 0.0);
2379
2397
  nr= sqrt(nr);
2380
2398
  double2my_decimal(E_DEC_FATAL_ERROR, nr, &tmp_dec);
2381
 
  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);
2382
2400
  return dec_buf;
2383
2401
}
2384
2402
 
2412
2430
    return val_real_from_decimal();
2413
2431
 
2414
2432
  double recurrence_s;
2415
 
  uint64_t count;
 
2433
  ulonglong count;
2416
2434
  float8get(recurrence_s, (field->ptr + sizeof(double)));
2417
2435
  count=sint8korr(field->ptr+sizeof(double)*2);
2418
2436
 
2427
2445
** COUNT(DISTINCT ...)
2428
2446
****************************************************************************/
2429
2447
 
2430
 
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)
2431
2449
{
2432
2450
  Field *f= (Field*) arg;
2433
2451
  return f->cmp(key1, key2);
2440
2458
  static
2441
2459
*/
2442
2460
 
2443
 
int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2)
 
2461
int composite_key_cmp(void* arg, uchar* key1, uchar* key2)
2444
2462
{
2445
2463
  Item_sum_count_distinct* item = (Item_sum_count_distinct*)arg;
2446
2464
  Field **field    = item->table->field;
2447
2465
  Field **field_end= field + item->table->s->fields;
2448
 
  uint32_t *lengths=item->field_lengths;
 
2466
  uint32 *lengths=item->field_lengths;
2449
2467
  for (; field < field_end; ++field)
2450
2468
  {
2451
2469
    Field* f = *field;
2459
2477
  return 0;
2460
2478
}
2461
2479
 
2462
 
#ifdef __cplusplus
2463
 
extern "C" {
2464
 
#endif
2465
 
 
2466
 
static int count_distinct_walk(void *elem __attribute__((unused)),
2467
 
                               element_count count __attribute__((unused)),
2468
 
                               void *arg)
 
2480
 
 
2481
C_MODE_START
 
2482
 
 
2483
static int count_distinct_walk(void *elem, element_count count, void *arg)
2469
2484
{
2470
 
  (*((uint64_t*)arg))++;
 
2485
  (*((ulonglong*)arg))++;
2471
2486
  return 0;
2472
2487
}
2473
2488
 
2474
 
#ifdef __cplusplus
2475
 
}
2476
 
#endif
2477
 
 
 
2489
C_MODE_END
2478
2490
 
2479
2491
 
2480
2492
void Item_sum_count_distinct::cleanup()
2481
2493
{
 
2494
  DBUG_ENTER("Item_sum_count_distinct::cleanup");
2482
2495
  Item_sum_int::cleanup();
2483
2496
 
2484
2497
  /* Free objects only if we own them. */
2491
2504
    */
2492
2505
    delete tree;
2493
2506
    tree= 0;
2494
 
    is_evaluated= false;
 
2507
    is_evaluated= FALSE;
2495
2508
    if (table)
2496
2509
    {
2497
 
      table->free_tmp_table(table->in_use);
 
2510
      free_tmp_table(table->in_use, table);
2498
2511
      table= 0;
2499
2512
    }
2500
2513
    delete tmp_table_param;
2501
2514
    tmp_table_param= 0;
2502
2515
  }
2503
 
  always_null= false;
2504
 
  return;
 
2516
  always_null= FALSE;
 
2517
  DBUG_VOID_RETURN;
2505
2518
}
2506
2519
 
2507
2520
 
2516
2529
  original= 0;
2517
2530
  force_copy_fields= 1;
2518
2531
  tree= 0;
2519
 
  is_evaluated= false;
 
2532
  is_evaluated= FALSE;
2520
2533
  tmp_table_param= 0;
2521
 
  always_null= false;
 
2534
  always_null= FALSE;
2522
2535
}
2523
2536
 
2524
2537
 
2535
2548
 
2536
2549
  /*
2537
2550
    Setup can be called twice for ROLLUP items. This is a bug.
2538
 
    Please add assert(tree == 0) here when it's fixed.
 
2551
    Please add DBUG_ASSERT(tree == 0) here when it's fixed.
2539
2552
    It's legal to call setup() more than once when in a subquery
2540
2553
  */
2541
2554
  if (tree || table || tmp_table_param)
2542
 
    return false;
 
2555
    return FALSE;
2543
2556
 
2544
2557
  if (!(tmp_table_param= new TMP_TABLE_PARAM))
2545
 
    return true;
 
2558
    return TRUE;
2546
2559
 
2547
2560
  /* Create a table with an unique key over all parameters */
2548
 
  for (uint32_t i=0; i < arg_count ; i++)
 
2561
  for (uint i=0; i < arg_count ; i++)
2549
2562
  {
2550
2563
    Item *item=args[i];
2551
2564
    if (list.push_back(item))
2552
 
      return true;                              // End of memory
 
2565
      return TRUE;                              // End of memory
2553
2566
    if (item->const_item() && item->is_null())
2554
2567
      always_null= 1;
2555
2568
  }
2556
2569
  if (always_null)
2557
 
    return false;
 
2570
    return FALSE;
2558
2571
  count_field_types(select_lex, tmp_table_param, list, 0);
2559
2572
  tmp_table_param->force_copy_fields= force_copy_fields;
2560
 
  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
  }
2561
2590
 
2562
 
  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,
2563
2592
                                0,
2564
2593
                                (select_lex->options | thd->options),
2565
2594
                                HA_POS_ERROR, (char*)"")))
2566
 
    return true;
 
2595
    return TRUE;
2567
2596
  table->file->extra(HA_EXTRA_NO_ROWS);         // Don't update rows
2568
2597
  table->no_rows=1;
2569
2598
 
2577
2606
    void* cmp_arg;
2578
2607
    Field **field= table->field;
2579
2608
    Field **field_end= field + table->s->fields;
2580
 
    bool all_binary= true;
 
2609
    bool all_binary= TRUE;
2581
2610
 
2582
2611
    for (tree_key_length= 0; field < field_end; ++field)
2583
2612
    {
2584
2613
      Field *f= *field;
2585
2614
      enum enum_field_types f_type= f->type();
2586
2615
      tree_key_length+= f->pack_length();
2587
 
      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)))
2588
2617
      {
2589
 
        all_binary= false;
 
2618
        all_binary= FALSE;
2590
2619
        break;
2591
2620
      }
2592
2621
    }
2611
2640
      }
2612
2641
      else
2613
2642
      {
2614
 
        uint32_t *length;
 
2643
        uint32 *length;
2615
2644
        compare_key= (qsort_cmp2) composite_key_cmp;
2616
2645
        cmp_arg= (void*) this;
2617
 
        field_lengths= (uint32_t*) thd->alloc(table->s->fields * sizeof(uint32_t));
 
2646
        field_lengths= (uint32*) thd->alloc(table->s->fields * sizeof(uint32));
2618
2647
        for (tree_key_length= 0, length= field_lengths, field= table->field;
2619
2648
             field < field_end; ++field, ++length)
2620
2649
        {
2623
2652
        }
2624
2653
      }
2625
2654
    }
2626
 
    assert(tree == 0);
 
2655
    DBUG_ASSERT(tree == 0);
2627
2656
    tree= new Unique(compare_key, cmp_arg, tree_key_length,
2628
2657
                     thd->variables.max_heap_table_size);
2629
2658
    /*
2632
2661
      but this has to be handled - otherwise someone can crash
2633
2662
      the server with a DoS attack
2634
2663
    */
2635
 
    is_evaluated= false;
 
2664
    is_evaluated= FALSE;
2636
2665
    if (! tree)
2637
 
      return true;
 
2666
      return TRUE;
2638
2667
  }
2639
 
  return false;
 
2668
  return FALSE;
2640
2669
}
2641
2670
 
2642
2671
 
2649
2678
void Item_sum_count_distinct::clear()
2650
2679
{
2651
2680
  /* tree and table can be both null only if always_null */
2652
 
  is_evaluated= false;
 
2681
  is_evaluated= FALSE;
2653
2682
  if (tree)
2654
2683
  {
2655
2684
    tree->reset();
2674
2703
    if ((*field)->is_real_null(0))
2675
2704
      return 0;                                 // Don't count NULL
2676
2705
 
2677
 
  is_evaluated= false;
 
2706
  is_evaluated= FALSE;
2678
2707
  if (tree)
2679
2708
  {
2680
2709
    /*
2687
2716
  }
2688
2717
  if ((error= table->file->ha_write_row(table->record[0])) &&
2689
2718
      table->file->is_fatal_error(error, HA_CHECK_DUP))
2690
 
    return true;
2691
 
  return false;
 
2719
    return TRUE;
 
2720
  return FALSE;
2692
2721
}
2693
2722
 
2694
2723
 
2695
 
int64_t Item_sum_count_distinct::val_int()
 
2724
longlong Item_sum_count_distinct::val_int()
2696
2725
{
2697
2726
  int error;
2698
 
  assert(fixed == 1);
 
2727
  DBUG_ASSERT(fixed == 1);
2699
2728
  if (!table)                                   // Empty query
2700
 
    return 0L;
 
2729
    return LL(0);
2701
2730
  if (tree)
2702
2731
  {
2703
2732
    if (is_evaluated)
2704
2733
      return count;
2705
2734
 
2706
2735
    if (tree->elements == 0)
2707
 
      return (int64_t) tree->elements_in_tree(); // everything fits in memory
 
2736
      return (longlong) tree->elements_in_tree(); // everything fits in memory
2708
2737
    count= 0;
2709
2738
    tree->walk(count_distinct_walk, (void*) &count);
2710
 
    is_evaluated= true;
2711
 
    return (int64_t) count;
 
2739
    is_evaluated= TRUE;
 
2740
    return (longlong) count;
2712
2741
  }
2713
2742
 
2714
2743
  error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
2721
2750
  return table->file->stats.records;
2722
2751
}
2723
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
 
2724
2925
/*****************************************************************************
2725
2926
 GROUP_CONCAT function
2726
2927
 
2727
2928
 SQL SYNTAX:
2728
 
  GROUP_CONCAT([DISTINCT] expr,... [order_st BY col [ASC|DESC],...]
 
2929
  GROUP_CONCAT([DISTINCT] expr,... [ORDER BY col [ASC|DESC],...]
2729
2930
    [SEPARATOR str_const])
2730
2931
 
2731
2932
 concat of values from "group by" operation
2732
2933
 
2733
2934
 BUGS
2734
 
   Blobs doesn't work with DISTINCT or order_st BY
 
2935
   Blobs doesn't work with DISTINCT or ORDER BY
2735
2936
*****************************************************************************/
2736
2937
 
2737
2938
 
2740
2941
  @note
2741
2942
       
2742
2943
     GROUP_CONCAT([DISTINCT] expr [,expr ...]
2743
 
              [order_st BY {unsigned_integer | col_name | expr}
 
2944
              [ORDER BY {unsigned_integer | col_name | expr}
2744
2945
                  [ASC | DESC] [,col_name ...]]
2745
2946
              [SEPARATOR str_val])
2746
2947
 
2754
2955
                                       const void* key2)
2755
2956
{
2756
2957
  Item_func_group_concat *item_func= (Item_func_group_concat*)arg;
2757
 
  Table *table= item_func->table;
 
2958
  TABLE *table= item_func->table;
2758
2959
 
2759
 
  for (uint32_t i= 0; i < item_func->arg_count_field; i++)
 
2960
  for (uint i= 0; i < item_func->arg_count_field; i++)
2760
2961
  {
2761
2962
    Item *item= item_func->args[i];
2762
2963
    /* 
2772
2973
    */
2773
2974
    Field *field= item->get_tmp_table_field();
2774
2975
    int res;
2775
 
    uint32_t offset= field->offset(field->table->record[0])-table->s->null_bytes;
2776
 
    if((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
 
2976
    uint offset= field->offset(field->table->record[0])-table->s->null_bytes;
 
2977
    if((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset)))
2777
2978
      return res;
2778
2979
  }
2779
2980
  return 0;
2781
2982
 
2782
2983
 
2783
2984
/**
2784
 
  function of sort for syntax: GROUP_CONCAT(expr,... order_st BY col,... )
 
2985
  function of sort for syntax: GROUP_CONCAT(expr,... ORDER BY col,... )
2785
2986
*/
2786
2987
 
2787
2988
int group_concat_key_cmp_with_order(void* arg, const void* key1, 
2788
2989
                                    const void* key2)
2789
2990
{
2790
2991
  Item_func_group_concat* grp_item= (Item_func_group_concat*) arg;
2791
 
  order_st **order_item, **end;
2792
 
  Table *table= grp_item->table;
 
2992
  ORDER **order_item, **end;
 
2993
  TABLE *table= grp_item->table;
2793
2994
 
2794
2995
  for (order_item= grp_item->order, end=order_item+ grp_item->arg_count_order;
2795
2996
       order_item < end;
2809
3010
    if (field && !item->const_item())
2810
3011
    {
2811
3012
      int res;
2812
 
      uint32_t offset= (field->offset(field->table->record[0]) -
 
3013
      uint offset= (field->offset(field->table->record[0]) -
2813
3014
                    table->s->null_bytes);
2814
 
      if ((res= field->cmp((unsigned char*)key1 + offset, (unsigned char*)key2 + offset)))
 
3015
      if ((res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset)))
2815
3016
        return (*order_item)->asc ? res : -res;
2816
3017
    }
2817
3018
  }
2828
3029
  Append data from current leaf to item->result.
2829
3030
*/
2830
3031
 
2831
 
int dump_leaf_key(unsigned char* key, element_count count __attribute__((unused)),
 
3032
int dump_leaf_key(uchar* key, element_count count __attribute__((unused)),
2832
3033
                  Item_func_group_concat *item)
2833
3034
{
2834
 
  Table *table= item->table;
 
3035
  TABLE *table= item->table;
2835
3036
  String tmp((char *)table->record[1], table->s->reclength,
2836
3037
             default_charset_info);
2837
3038
  String tmp2;
2838
3039
  String *result= &item->result;
2839
3040
  Item **arg= item->args, **arg_end= item->args + item->arg_count_field;
2840
 
  uint32_t old_length= result->length();
 
3041
  uint old_length= result->length();
2841
3042
 
2842
3043
  if (item->no_appended)
2843
 
    item->no_appended= false;
 
3044
    item->no_appended= FALSE;
2844
3045
  else
2845
3046
    result->append(*item->separator);
2846
3047
 
2859
3060
        because it contains both order and arg list fields.
2860
3061
      */
2861
3062
      Field *field= (*arg)->get_tmp_table_field();
2862
 
      uint32_t offset= (field->offset(field->table->record[0]) -
 
3063
      uint offset= (field->offset(field->table->record[0]) -
2863
3064
                    table->s->null_bytes);
2864
 
      assert(offset < table->s->reclength);
 
3065
      DBUG_ASSERT(offset < table->s->reclength);
2865
3066
      res= field->val_str(&tmp, key + offset);
2866
3067
    }
2867
3068
    else
2874
3075
  if (result->length() > item->max_length)
2875
3076
  {
2876
3077
    int well_formed_error;
2877
 
    const CHARSET_INFO * const cs= item->collation.collation;
 
3078
    CHARSET_INFO *cs= item->collation.collation;
2878
3079
    const char *ptr= result->ptr();
2879
 
    uint32_t add_length;
 
3080
    uint add_length;
2880
3081
    /*
2881
3082
      It's ok to use item->result.length() as the fourth argument
2882
3083
      as this is never used to limit the length of the data.
2889
3090
                                          &well_formed_error);
2890
3091
    result->length(old_length + add_length);
2891
3092
    item->count_cut_values++;
2892
 
    item->warning_for_row= true;
 
3093
    item->warning_for_row= TRUE;
2893
3094
    return 1;
2894
3095
  }
2895
3096
  return 0;
2916
3117
   arg_count_field(select_list->elements),
2917
3118
   count_cut_values(0),
2918
3119
   distinct(distinct_arg),
2919
 
   warning_for_row(false),
 
3120
   warning_for_row(FALSE),
2920
3121
   force_copy_fields(0), original(0)
2921
3122
{
2922
3123
  Item *item_select;
2923
3124
  Item **arg_ptr;
2924
3125
 
2925
 
  quick_group= false;
 
3126
  quick_group= FALSE;
2926
3127
  arg_count= arg_count_field + arg_count_order;
2927
3128
 
2928
3129
  /*
2932
3133
    order - arg_count_order
2933
3134
  */
2934
3135
  if (!(args= (Item**) sql_alloc(sizeof(Item*) * arg_count +
2935
 
                                 sizeof(order_st*)*arg_count_order)))
 
3136
                                 sizeof(ORDER*)*arg_count_order)))
2936
3137
    return;
2937
3138
 
2938
 
  order= (order_st**)(args + arg_count);
 
3139
  order= (ORDER**)(args + arg_count);
2939
3140
 
2940
3141
  /* fill args items of show and sort */
2941
3142
  List_iterator_fast<Item> li(*select_list);
2945
3146
 
2946
3147
  if (arg_count_order)
2947
3148
  {
2948
 
    order_st **order_ptr= order;
2949
 
    for (order_st *order_item= (order_st*) order_list->first;
 
3149
    ORDER **order_ptr= order;
 
3150
    for (ORDER *order_item= (ORDER*) order_list->first;
2950
3151
         order_item != NULL;
2951
3152
         order_item= order_item->next)
2952
3153
    {
2986
3187
 
2987
3188
void Item_func_group_concat::cleanup()
2988
3189
{
 
3190
  DBUG_ENTER("Item_func_group_concat::cleanup");
2989
3191
  Item_sum::cleanup();
2990
3192
 
2991
3193
  /* Adjust warning message to include total number of cut values */
2992
3194
  if (warning)
2993
3195
  {
2994
 
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
 
3196
    char warn_buff[MYSQL_ERRMSG_SIZE];
2995
3197
    sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
2996
3198
    warning->set_msg(current_thd, warn_buff);
2997
3199
    warning= 0;
3008
3210
    if (table)
3009
3211
    {
3010
3212
      THD *thd= table->in_use;
3011
 
      table->free_tmp_table(thd);
 
3213
      free_tmp_table(thd, table);
3012
3214
      table= 0;
3013
3215
      if (tree)
3014
3216
      {
3022
3224
      }
3023
3225
      if (warning)
3024
3226
      {
3025
 
        char warn_buff[DRIZZLE_ERRMSG_SIZE];
 
3227
        char warn_buff[MYSQL_ERRMSG_SIZE];
3026
3228
        sprintf(warn_buff, ER(ER_CUT_VALUE_GROUP_CONCAT), count_cut_values);
3027
3229
        warning->set_msg(thd, warn_buff);
3028
3230
        warning= 0;
3029
3231
      }
3030
3232
    }
3031
 
    assert(tree == 0 && warning == 0);
 
3233
    DBUG_ASSERT(tree == 0 && warning == 0);
3032
3234
  }
3033
 
  return;
 
3235
  DBUG_VOID_RETURN;
3034
3236
}
3035
3237
 
3036
3238
 
3044
3246
{
3045
3247
  result.length(0);
3046
3248
  result.copy();
3047
 
  null_value= true;
3048
 
  warning_for_row= false;
3049
 
  no_appended= true;
 
3249
  null_value= TRUE;
 
3250
  warning_for_row= FALSE;
 
3251
  no_appended= TRUE;
3050
3252
  if (tree)
3051
3253
    reset_tree(tree);
3052
3254
  if (distinct)
3062
3264
  copy_fields(tmp_table_param);
3063
3265
  copy_funcs(tmp_table_param->items_to_copy);
3064
3266
 
3065
 
  for (uint32_t i= 0; i < arg_count_field; i++)
 
3267
  for (uint i= 0; i < arg_count_field; i++)
3066
3268
  {
3067
3269
    Item *show_item= args[i];
3068
3270
    if (!show_item->const_item())
3069
3271
    {
3070
3272
      Field *f= show_item->get_tmp_table_field();
3071
 
      if (f->is_null_in_record((const unsigned char*) table->record[0]))
 
3273
      if (f->is_null_in_record((const uchar*) table->record[0]))
3072
3274
        return 0;                               // Skip row if it contains null
3073
3275
    }
3074
3276
  }
3075
3277
 
3076
 
  null_value= false;
3077
 
  bool row_eligible= true;
 
3278
  null_value= FALSE;
 
3279
  bool row_eligible= TRUE;
3078
3280
 
3079
3281
  if (distinct) 
3080
3282
  {
3081
3283
    /* Filter out duplicate rows. */
3082
 
    uint32_t count= unique_filter->elements_in_tree();
 
3284
    uint count= unique_filter->elements_in_tree();
3083
3285
    unique_filter->unique_add(table->record[0] + table->s->null_bytes);
3084
3286
    if (count == unique_filter->elements_in_tree())
3085
 
      row_eligible= false;
 
3287
      row_eligible= FALSE;
3086
3288
  }
3087
3289
 
3088
3290
  TREE_ELEMENT *el= 0;                          // Only for safety
3105
3307
bool
3106
3308
Item_func_group_concat::fix_fields(THD *thd, Item **ref)
3107
3309
{
3108
 
  uint32_t i;                       /* for loop variable */
3109
 
  assert(fixed == 0);
 
3310
  uint i;                       /* for loop variable */
 
3311
  DBUG_ASSERT(fixed == 0);
3110
3312
 
3111
3313
  if (init_sum_func_check(thd))
3112
 
    return true;
 
3314
    return TRUE;
3113
3315
 
3114
3316
  maybe_null= 1;
3115
3317
 
3116
3318
  /*
3117
 
    Fix fields for select list and order_st clause
 
3319
    Fix fields for select list and ORDER clause
3118
3320
  */
3119
3321
 
3120
3322
  for (i=0 ; i < arg_count ; i++)
3122
3324
    if ((!args[i]->fixed &&
3123
3325
         args[i]->fix_fields(thd, args + i)) ||
3124
3326
        args[i]->check_cols(1))
3125
 
      return true;
 
3327
      return TRUE;
3126
3328
  }
3127
3329
 
3128
3330
  if (agg_item_charsets(collation, func_name(),
3137
3339
  null_value= 1;
3138
3340
  max_length= thd->variables.group_concat_max_len;
3139
3341
 
3140
 
  uint32_t offset;
 
3342
  uint32 offset;
3141
3343
  if (separator->needs_conversion(separator->length(), separator->charset(),
3142
3344
                                  collation.collation, &offset))
3143
3345
  {
3144
 
    uint32_t buflen= collation.collation->mbmaxlen * separator->length();
3145
 
    uint32_t errors, conv_length;
 
3346
    uint32 buflen= collation.collation->mbmaxlen * separator->length();
 
3347
    uint errors, conv_length;
3146
3348
    char *buf;
3147
3349
    String *new_separator;
3148
3350
 
3149
 
    if (!(buf= (char*) thd->alloc(buflen)) ||
3150
 
        !(new_separator= new(thd->mem_root)
 
3351
    if (!(buf= (char*) thd->stmt_arena->alloc(buflen)) ||
 
3352
        !(new_separator= new(thd->stmt_arena->mem_root)
3151
3353
                           String(buf, buflen, collation.collation)))
3152
 
      return true;
 
3354
      return TRUE;
3153
3355
    
3154
3356
    conv_length= copy_and_convert(buf, buflen, collation.collation,
3155
3357
                                  separator->ptr(), separator->length(),
3159
3361
  }
3160
3362
 
3161
3363
  if (check_sum_func(thd, ref))
3162
 
    return true;
 
3364
    return TRUE;
3163
3365
 
3164
3366
  fixed= 1;
3165
 
  return false;
 
3367
  return FALSE;
3166
3368
}
3167
3369
 
3168
3370
 
3170
3372
{
3171
3373
  List<Item> list;
3172
3374
  SELECT_LEX *select_lex= thd->lex->current_select;
 
3375
  DBUG_ENTER("Item_func_group_concat::setup");
3173
3376
 
3174
3377
  /*
3175
3378
    Currently setup() can be called twice. Please add
3176
3379
    assertion here when this is fixed.
3177
3380
  */
3178
3381
  if (table || tree)
3179
 
    return(false);
 
3382
    DBUG_RETURN(FALSE);
3180
3383
 
3181
3384
  if (!(tmp_table_param= new TMP_TABLE_PARAM))
3182
 
    return(true);
 
3385
    DBUG_RETURN(TRUE);
3183
3386
 
3184
3387
  /* We'll convert all blobs to varchar fields in the temporary table */
3185
3388
  tmp_table_param->convert_blob_length= max_length *
3186
3389
                                        collation.collation->mbmaxlen;
3187
3390
  /* Push all not constant fields to the list and create a temp table */
3188
3391
  always_null= 0;
3189
 
  for (uint32_t i= 0; i < arg_count_field; i++)
 
3392
  for (uint i= 0; i < arg_count_field; i++)
3190
3393
  {
3191
3394
    Item *item= args[i];
3192
3395
    if (list.push_back(item))
3193
 
      return(true);
 
3396
      DBUG_RETURN(TRUE);
3194
3397
    if (item->const_item())
3195
3398
    {
3196
3399
      if (item->is_null())
3197
3400
      {
3198
3401
        always_null= 1;
3199
 
        return(false);
 
3402
        DBUG_RETURN(FALSE);
3200
3403
      }
3201
3404
    }
3202
3405
  }
3203
3406
 
3204
3407
  List<Item> all_fields(list);
3205
3408
  /*
3206
 
    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
3207
3410
    arguments. If an expression is not found, prepend it to
3208
3411
    "all_fields". The resulting field list is used as input to create
3209
3412
    tmp table columns.
3210
3413
  */
3211
3414
  if (arg_count_order &&
3212
3415
      setup_order(thd, args, context->table_list, list, all_fields, *order))
3213
 
    return(true);
 
3416
    DBUG_RETURN(TRUE);
3214
3417
 
3215
3418
  count_field_types(select_lex, tmp_table_param, all_fields, 0);
3216
3419
  tmp_table_param->force_copy_fields= force_copy_fields;
3217
 
  assert(table == 0);
 
3420
  DBUG_ASSERT(table == 0);
3218
3421
  if (arg_count_order > 0 || distinct)
3219
3422
  {
3220
3423
    /*
3221
3424
      Currently we have to force conversion of BLOB values to VARCHAR's
3222
 
      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
3223
3426
      DISTINCT. This leads to truncation if the BLOB's size exceeds
3224
3427
      Field_varstring::MAX_SIZE.
3225
3428
    */
3226
3429
    set_if_smaller(tmp_table_param->convert_blob_length, 
3227
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
    }
3228
3447
  }
3229
3448
 
3230
3449
  /*
3231
3450
    We have to create a temporary table to get descriptions of fields
3232
3451
    (types, sizes and so on).
3233
3452
 
3234
 
    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
3235
3454
    field list.
3236
3455
  */
3237
3456
  if (!(table= create_tmp_table(thd, tmp_table_param, all_fields,
3238
 
                                (order_st*) 0, 0, true,
 
3457
                                (ORDER*) 0, 0, TRUE,
3239
3458
                                (select_lex->options | thd->options),
3240
3459
                                HA_POS_ERROR, (char*) "")))
3241
 
    return(true);
 
3460
    DBUG_RETURN(TRUE);
3242
3461
  table->file->extra(HA_EXTRA_NO_ROWS);
3243
3462
  table->no_rows= 1;
3244
3463
 
3247
3466
     Don't reserve space for NULLs: if any of gconcat arguments is NULL,
3248
3467
     the row is not added to the result.
3249
3468
  */
3250
 
  uint32_t tree_key_length= table->s->reclength - table->s->null_bytes;
 
3469
  uint tree_key_length= table->s->reclength - table->s->null_bytes;
3251
3470
 
3252
3471
  if (arg_count_order)
3253
3472
  {
3254
3473
    tree= &tree_base;
3255
3474
    /*
3256
3475
      Create a tree for sorting. The tree is used to sort (according to the
3257
 
      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
3258
3477
      create this tree.
3259
3478
    */
3260
 
    init_tree(tree, (uint) cmin(thd->variables.max_heap_table_size,
 
3479
    init_tree(tree, (uint) min(thd->variables.max_heap_table_size,
3261
3480
                               thd->variables.sortbuff_size/16), 0,
3262
3481
              tree_key_length, 
3263
3482
              group_concat_key_cmp_with_order , 0, NULL, (void*) this);
3269
3488
                              tree_key_length,
3270
3489
                              thd->variables.max_heap_table_size);
3271
3490
  
3272
 
  return(false);
 
3491
  DBUG_RETURN(FALSE);
3273
3492
}
3274
3493
 
3275
3494
 
3285
3504
}
3286
3505
 
3287
3506
 
3288
 
String* Item_func_group_concat::val_str(String* str __attribute__((unused)))
 
3507
String* Item_func_group_concat::val_str(String* str)
3289
3508
{
3290
 
  assert(fixed == 1);
 
3509
  DBUG_ASSERT(fixed == 1);
3291
3510
  if (null_value)
3292
3511
    return 0;
3293
3512
  if (no_appended && tree)
3300
3519
      ER_CUT_VALUE_GROUP_CONCAT needs an argument, but this gets set in
3301
3520
      Item_func_group_concat::cleanup().
3302
3521
    */
3303
 
    assert(table);
3304
 
    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,
3305
3524
                          ER_CUT_VALUE_GROUP_CONCAT,
3306
3525
                          ER(ER_CUT_VALUE_GROUP_CONCAT));
3307
3526
  }
3314
3533
  str->append(STRING_WITH_LEN("group_concat("));
3315
3534
  if (distinct)
3316
3535
    str->append(STRING_WITH_LEN("distinct "));
3317
 
  for (uint32_t i= 0; i < arg_count_field; i++)
 
3536
  for (uint i= 0; i < arg_count_field; i++)
3318
3537
  {
3319
3538
    if (i)
3320
3539
      str->append(',');
3323
3542
  if (arg_count_order)
3324
3543
  {
3325
3544
    str->append(STRING_WITH_LEN(" order by "));
3326
 
    for (uint32_t i= 0 ; i < arg_count_order ; i++)
 
3545
    for (uint i= 0 ; i < arg_count_order ; i++)
3327
3546
    {
3328
3547
      if (i)
3329
3548
        str->append(',');