~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/item_sum.h

  • 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:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
 
1
/* Copyright (C) 2000-2006 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
19
15
 
20
16
 
21
17
/* classes for sum functions */
22
18
 
 
19
#ifdef USE_PRAGMA_INTERFACE
 
20
#pragma interface                       /* gcc class implementation */
 
21
#endif
23
22
 
24
 
#include <mysys/my_tree.h>
 
23
#include <my_tree.h>
25
24
 
26
25
/*
27
26
  Class Item_sum is the base class used for special expressions that SQL calls
226
225
  enum Sumfunctype
227
226
  { COUNT_FUNC, COUNT_DISTINCT_FUNC, SUM_FUNC, SUM_DISTINCT_FUNC, AVG_FUNC,
228
227
    AVG_DISTINCT_FUNC, MIN_FUNC, MAX_FUNC, STD_FUNC,
229
 
    VARIANCE_FUNC, SUM_BIT_FUNC, GROUP_CONCAT_FUNC
 
228
    VARIANCE_FUNC, SUM_BIT_FUNC, UDF_SUM_FUNC, GROUP_CONCAT_FUNC
230
229
  };
231
230
 
232
231
  Item **args, *tmp_args[2];
233
232
  Item **ref_by; /* pointer to a ref to the object used to register it */
234
233
  Item_sum *next; /* next in the circular chain of registered objects  */
235
 
  uint32_t arg_count;
 
234
  uint arg_count;
236
235
  Item_sum *in_sum_func;  /* embedding set function if any */ 
237
236
  st_select_lex * aggr_sel; /* select where the function is aggregated       */ 
238
 
  int8_t nest_level;        /* number of the nesting level of the set function */
239
 
  int8_t aggr_level;        /* nesting level of the aggregating subquery       */
240
 
  int8_t max_arg_level;     /* max level of unbound column references          */
241
 
  int8_t max_sum_func_level;/* max level of aggregation for embedded functions */
 
237
  int8 nest_level;        /* number of the nesting level of the set function */
 
238
  int8 aggr_level;        /* nesting level of the aggregating subquery       */
 
239
  int8 max_arg_level;     /* max level of unbound column references          */
 
240
  int8 max_sum_func_level;/* max level of aggregation for embedded functions */
242
241
  bool quick_group;                     /* If incremental update of fields */
243
242
  /*
244
243
    This list is used by the check for mixing non aggregated fields and
255
254
public:  
256
255
 
257
256
  void mark_as_sum_func();
258
 
  Item_sum() :arg_count(0), quick_group(1), forced_const(false)
 
257
  Item_sum() :arg_count(0), quick_group(1), forced_const(FALSE)
259
258
  {
260
259
    mark_as_sum_func();
261
260
  }
262
261
  Item_sum(Item *a) :args(tmp_args), arg_count(1), quick_group(1), 
263
 
    forced_const(false)
 
262
    forced_const(FALSE)
264
263
  {
265
264
    args[0]=a;
266
265
    mark_as_sum_func();
267
266
  }
268
267
  Item_sum( Item *a, Item *b ) :args(tmp_args), arg_count(2), quick_group(1),
269
 
    forced_const(false)
 
268
    forced_const(FALSE)
270
269
  {
271
270
    args[0]=a; args[1]=b;
272
271
    mark_as_sum_func();
341
340
  void cleanup() 
342
341
  { 
343
342
    Item::cleanup();
344
 
    forced_const= false; 
 
343
    forced_const= FALSE; 
345
344
  }
346
345
  bool is_null() { return null_value; }
347
346
  void make_const () 
348
347
  { 
349
348
    used_tables_cache= 0; 
350
 
    forced_const= true; 
 
349
    forced_const= TRUE; 
351
350
  }
352
351
  virtual bool const_item() const { return forced_const; }
353
352
  void make_field(Send_field *field);
364
363
  */
365
364
  void no_rows_in_result() { clear(); }
366
365
 
367
 
  virtual bool setup(THD *thd __attribute__((unused))) {return 0;}
368
 
  virtual void make_unique(void) {}
 
366
  virtual bool setup(THD *thd) {return 0;}
 
367
  virtual void make_unique() {}
369
368
  Item *get_tmp_table_item(THD *thd);
370
 
  virtual Field *create_tmp_field(bool group, Table *table,
371
 
                                  uint32_t convert_blob_length);
372
 
  bool walk(Item_processor processor, bool walk_subquery, unsigned char *argument);
 
369
  virtual Field *create_tmp_field(bool group, TABLE *table,
 
370
                                  uint convert_blob_length);
 
371
  bool walk(Item_processor processor, bool walk_subquery, uchar *argument);
373
372
  bool init_sum_func_check(THD *thd);
374
373
  bool check_sum_func(THD *thd, Item **ref);
375
374
  bool register_sum_func(THD *thd, Item **ref);
389
388
  */
390
389
  bool is_evaluated;
391
390
public:
392
 
  Item_sum_num() :Item_sum(),is_evaluated(false) {}
 
391
  Item_sum_num() :Item_sum(),is_evaluated(FALSE) {}
393
392
  Item_sum_num(Item *item_par) 
394
 
    :Item_sum(item_par), is_evaluated(false) {}
395
 
  Item_sum_num(Item *a, Item* b) :Item_sum(a,b),is_evaluated(false) {}
 
393
    :Item_sum(item_par), is_evaluated(FALSE) {}
 
394
  Item_sum_num(Item *a, Item* b) :Item_sum(a,b),is_evaluated(FALSE) {}
396
395
  Item_sum_num(List<Item> &list) 
397
 
    :Item_sum(list), is_evaluated(false) {}
 
396
    :Item_sum(list), is_evaluated(FALSE) {}
398
397
  Item_sum_num(THD *thd, Item_sum_num *item) 
399
398
    :Item_sum(thd, item),is_evaluated(item->is_evaluated) {}
400
399
  bool fix_fields(THD *, Item **);
401
 
  int64_t val_int()
 
400
  longlong val_int()
402
401
  {
403
 
    assert(fixed == 1);
404
 
    return (int64_t) rint(val_real());             /* Real as default */
 
402
    DBUG_ASSERT(fixed == 1);
 
403
    return (longlong) rint(val_real());             /* Real as default */
405
404
  }
406
405
  String *val_str(String*str);
407
406
  my_decimal *val_decimal(my_decimal *);
415
414
  Item_sum_int(Item *item_par) :Item_sum_num(item_par) {}
416
415
  Item_sum_int(List<Item> &list) :Item_sum_num(list) {}
417
416
  Item_sum_int(THD *thd, Item_sum_int *item) :Item_sum_num(thd, item) {}
418
 
  double val_real() { assert(fixed == 1); return (double) val_int(); }
 
417
  double val_real() { DBUG_ASSERT(fixed == 1); return (double) val_int(); }
419
418
  String *val_str(String*str);
420
419
  my_decimal *val_decimal(my_decimal *);
421
420
  enum Item_result result_type () const { return INT_RESULT; }
430
429
  Item_result hybrid_type;
431
430
  double sum;
432
431
  my_decimal dec_buffs[2];
433
 
  uint32_t curr_dec_buff;
 
432
  uint curr_dec_buff;
434
433
  void fix_length_and_dec();
435
434
 
436
435
public:
440
439
  void clear();
441
440
  bool add();
442
441
  double val_real();
443
 
  int64_t val_int();
 
442
  longlong val_int();
444
443
  String *val_str(String*str);
445
444
  my_decimal *val_decimal(my_decimal *);
446
445
  enum Item_result result_type () const { return hybrid_type; }
461
460
{
462
461
protected:
463
462
  /* storage for the summation result */
464
 
  uint64_t count;
 
463
  ulonglong count;
465
464
  Hybrid_type val;
466
465
  /* storage for unique elements */
467
466
  Unique *tree;
468
 
  Table *table;
 
467
  TABLE *table;
469
468
  enum enum_field_types table_field_type;
470
 
  uint32_t tree_key_length;
 
469
  uint tree_key_length;
471
470
protected:
472
471
  Item_sum_distinct(THD *thd, Item_sum_distinct *item);
473
472
public:
480
479
  bool add();
481
480
  double val_real();
482
481
  my_decimal *val_decimal(my_decimal *);
483
 
  int64_t val_int();
 
482
  longlong val_int();
484
483
  String *val_str(String *str);
485
484
 
486
485
  /* XXX: does it need make_unique? */
524
523
  Item_sum_avg_distinct(THD *thd, Item_sum_avg_distinct *original)
525
524
    :Item_sum_distinct(thd, original) {}
526
525
public:
527
 
  uint32_t prec_increment;
 
526
  uint prec_increment;
528
527
  Item_sum_avg_distinct(Item *item_arg) : Item_sum_distinct(item_arg) {}
529
528
 
530
529
  void fix_length_and_dec();
537
536
 
538
537
class Item_sum_count :public Item_sum_int
539
538
{
540
 
  int64_t count;
 
539
  longlong count;
541
540
 
542
541
  public:
543
542
  Item_sum_count(Item *item_par)
550
549
  void clear();
551
550
  void no_rows_in_result() { count=0; }
552
551
  bool add();
553
 
  void make_const(int64_t count_arg) 
 
552
  void make_const(longlong count_arg) 
554
553
  { 
555
554
    count=count_arg;
556
555
    Item_sum::make_const();
557
556
  }
558
 
  int64_t val_int();
 
557
  longlong val_int();
559
558
  void reset_field();
560
559
  void cleanup();
561
560
  void update_field();
568
567
 
569
568
class Item_sum_count_distinct :public Item_sum_int
570
569
{
571
 
  Table *table;
572
 
  uint32_t *field_lengths;
 
570
  TABLE *table;
 
571
  uint32 *field_lengths;
573
572
  TMP_TABLE_PARAM *tmp_table_param;
574
573
  bool force_copy_fields;
575
574
  /*
583
582
   will not recalculate on each call. Validitiy of the value is stored in
584
583
   is_evaluated.
585
584
  */
586
 
  int64_t count;
 
585
  longlong count;
587
586
  /*
588
587
    Following is 0 normal object and pointer to original one for copy 
589
588
    (to correctly free resources)
590
589
  */
591
590
  Item_sum_count_distinct *original;
592
 
  uint32_t tree_key_length;
 
591
  uint tree_key_length;
593
592
 
594
593
 
595
594
  bool always_null;             // Set to 1 if the result is always NULL
596
595
 
597
596
 
598
 
  friend int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2);
599
 
  friend int simple_str_key_cmp(void* arg, unsigned char* key1, unsigned char* key2);
 
597
  friend int composite_key_cmp(void* arg, uchar* key1, uchar* key2);
 
598
  friend int simple_str_key_cmp(void* arg, uchar* key1, uchar* key2);
600
599
 
601
600
public:
602
601
  Item_sum_count_distinct(List<Item> &list)
603
602
    :Item_sum_int(list), table(0), field_lengths(0), tmp_table_param(0),
604
603
     force_copy_fields(0), tree(0), count(0),
605
 
     original(0), always_null(false)
 
604
     original(0), always_null(FALSE)
606
605
  { quick_group= 0; }
607
606
  Item_sum_count_distinct(THD *thd, Item_sum_count_distinct *item)
608
607
    :Item_sum_int(thd, item), table(item->table),
619
618
  enum Sumfunctype sum_func () const { return COUNT_DISTINCT_FUNC; }
620
619
  void clear();
621
620
  bool add();
622
 
  int64_t val_int();
 
621
  longlong val_int();
623
622
  void reset_field() { return ;}                // Never called
624
623
  void update_field() { return ; }              // Never called
625
624
  const char *func_name() const { return "count(distinct "; }
639
638
public:
640
639
  Field *field;
641
640
  Item_result hybrid_type;
642
 
  uint32_t f_precision, f_scale, dec_bin_size;
643
 
  uint32_t prec_increment;
 
641
  uint f_precision, f_scale, dec_bin_size;
 
642
  uint prec_increment;
644
643
  Item_avg_field(Item_result res_type, Item_sum_avg *item);
645
644
  enum Type type() const { return FIELD_AVG_ITEM; }
646
645
  double val_real();
647
 
  int64_t val_int();
 
646
  longlong val_int();
648
647
  my_decimal *val_decimal(my_decimal *);
649
648
  bool is_null() { update_null_value(); return null_value; }
650
649
  String *val_str(String*);
651
650
  enum_field_types field_type() const
652
651
  {
653
652
    return hybrid_type == DECIMAL_RESULT ?
654
 
      DRIZZLE_TYPE_NEWDECIMAL : DRIZZLE_TYPE_DOUBLE;
 
653
      MYSQL_TYPE_NEWDECIMAL : MYSQL_TYPE_DOUBLE;
655
654
  }
656
655
  void fix_length_and_dec() {}
657
656
  enum Item_result result_type () const { return hybrid_type; }
661
660
class Item_sum_avg :public Item_sum_sum
662
661
{
663
662
public:
664
 
  uint64_t count;
665
 
  uint32_t prec_increment;
666
 
  uint32_t f_precision, f_scale, dec_bin_size;
 
663
  ulonglong count;
 
664
  uint prec_increment;
 
665
  uint f_precision, f_scale, dec_bin_size;
667
666
 
668
667
  Item_sum_avg(Item *item_par) :Item_sum_sum(item_par), count(0) {}
669
668
  Item_sum_avg(THD *thd, Item_sum_avg *item)
676
675
  bool add();
677
676
  double val_real();
678
677
  // In SPs we might force the "wrong" type with select into a declare variable
679
 
  int64_t val_int() { return (int64_t) rint(val_real()); }
 
678
  longlong val_int() { return (longlong) rint(val_real()); }
680
679
  my_decimal *val_decimal(my_decimal *);
681
680
  String *val_str(String *str);
682
681
  void reset_field();
683
682
  void update_field();
684
 
  Item *result_item(Field *field __attribute__((unused)))
 
683
  Item *result_item(Field *field)
685
684
  { return new Item_avg_field(hybrid_type, this); }
686
685
  void no_rows_in_result() {}
687
686
  const char *func_name() const { return "avg("; }
688
687
  Item *copy_or_same(THD* thd);
689
 
  Field *create_tmp_field(bool group, Table *table, uint32_t convert_blob_length);
 
688
  Field *create_tmp_field(bool group, TABLE *table, uint convert_blob_length);
690
689
  void cleanup()
691
690
  {
692
691
    count= 0;
701
700
public:
702
701
  Field *field;
703
702
  Item_result hybrid_type;
704
 
  uint32_t f_precision0, f_scale0;
705
 
  uint32_t f_precision1, f_scale1;
706
 
  uint32_t dec_bin_size0, dec_bin_size1;
707
 
  uint32_t sample;
708
 
  uint32_t prec_increment;
 
703
  uint f_precision0, f_scale0;
 
704
  uint f_precision1, f_scale1;
 
705
  uint dec_bin_size0, dec_bin_size1;
 
706
  uint sample;
 
707
  uint prec_increment;
709
708
  Item_variance_field(Item_sum_variance *item);
710
709
  enum Type type() const {return FIELD_VARIANCE_ITEM; }
711
710
  double val_real();
712
 
  int64_t val_int()
713
 
  { /* can't be fix_fields()ed */ return (int64_t) rint(val_real()); }
 
711
  longlong val_int()
 
712
  { /* can't be fix_fields()ed */ return (longlong) rint(val_real()); }
714
713
  String *val_str(String *str)
715
714
  { return val_string_from_real(str); }
716
715
  my_decimal *val_decimal(my_decimal *dec_buf)
719
718
  enum_field_types field_type() const
720
719
  {
721
720
    return hybrid_type == DECIMAL_RESULT ?
722
 
      DRIZZLE_TYPE_NEWDECIMAL : DRIZZLE_TYPE_DOUBLE;
 
721
      MYSQL_TYPE_NEWDECIMAL : MYSQL_TYPE_DOUBLE;
723
722
  }
724
723
  void fix_length_and_dec() {}
725
724
  enum Item_result result_type () const { return hybrid_type; }
754
753
  Item_result hybrid_type;
755
754
  int cur_dec;
756
755
  double recurrence_m, recurrence_s;    /* Used in recurrence relation. */
757
 
  uint64_t count;
758
 
  uint32_t f_precision0, f_scale0;
759
 
  uint32_t f_precision1, f_scale1;
760
 
  uint32_t dec_bin_size0, dec_bin_size1;
761
 
  uint32_t sample;
762
 
  uint32_t prec_increment;
 
756
  ulonglong count;
 
757
  uint f_precision0, f_scale0;
 
758
  uint f_precision1, f_scale1;
 
759
  uint dec_bin_size0, dec_bin_size1;
 
760
  uint sample;
 
761
  uint prec_increment;
763
762
 
764
 
  Item_sum_variance(Item *item_par, uint32_t sample_arg) :Item_sum_num(item_par),
 
763
  Item_sum_variance(Item *item_par, uint sample_arg) :Item_sum_num(item_par),
765
764
    hybrid_type(REAL_RESULT), count(0), sample(sample_arg)
766
765
    {}
767
766
  Item_sum_variance(THD *thd, Item_sum_variance *item);
772
771
  my_decimal *val_decimal(my_decimal *);
773
772
  void reset_field();
774
773
  void update_field();
775
 
  Item *result_item(Field *field __attribute__((unused)))
 
774
  Item *result_item(Field *field)
776
775
  { return new Item_variance_field(this); }
777
776
  void no_rows_in_result() {}
778
777
  const char *func_name() const
779
778
    { return sample ? "var_samp(" : "variance("; }
780
779
  Item *copy_or_same(THD* thd);
781
 
  Field *create_tmp_field(bool group, Table *table, uint32_t convert_blob_length);
 
780
  Field *create_tmp_field(bool group, TABLE *table, uint convert_blob_length);
782
781
  enum Item_result result_type () const { return REAL_RESULT; }
783
782
  void cleanup()
784
783
  {
797
796
  double val_real();
798
797
  my_decimal *val_decimal(my_decimal *);
799
798
  enum Item_result result_type () const { return REAL_RESULT; }
800
 
  enum_field_types field_type() const { return DRIZZLE_TYPE_DOUBLE;}
 
799
  enum_field_types field_type() const { return MYSQL_TYPE_DOUBLE;}
801
800
};
802
801
 
803
802
/*
807
806
class Item_sum_std :public Item_sum_variance
808
807
{
809
808
  public:
810
 
  Item_sum_std(Item *item_par, uint32_t sample_arg)
 
809
  Item_sum_std(Item *item_par, uint sample_arg)
811
810
    :Item_sum_variance(item_par, sample_arg) {}
812
811
  Item_sum_std(THD *thd, Item_sum_std *item)
813
812
    :Item_sum_variance(thd, item)
814
813
    {}
815
814
  enum Sumfunctype sum_func () const { return STD_FUNC; }
816
815
  double val_real();
817
 
  Item *result_item(Field *field __attribute__((unused)))
 
816
  Item *result_item(Field *field)
818
817
    { return new Item_std_field(this); }
819
818
  const char *func_name() const { return "std("; }
820
819
  Item *copy_or_same(THD* thd);
821
820
  enum Item_result result_type () const { return REAL_RESULT; }
822
 
  enum_field_types field_type() const { return DRIZZLE_TYPE_DOUBLE;}
 
821
  enum_field_types field_type() const { return MYSQL_TYPE_DOUBLE;}
823
822
};
824
823
 
825
824
// This class is a string or number function depending on num_func
829
828
protected:
830
829
  String value,tmp_value;
831
830
  double sum;
832
 
  int64_t sum_int;
 
831
  longlong sum_int;
833
832
  my_decimal sum_dec;
834
833
  Item_result hybrid_type;
835
834
  enum_field_types hybrid_field_type;
839
838
  public:
840
839
  Item_sum_hybrid(Item *item_par,int sign)
841
840
    :Item_sum(item_par), sum(0.0), sum_int(0),
842
 
    hybrid_type(INT_RESULT), hybrid_field_type(DRIZZLE_TYPE_LONGLONG),
843
 
    cmp_sign(sign), was_values(true)
 
841
    hybrid_type(INT_RESULT), hybrid_field_type(MYSQL_TYPE_LONGLONG),
 
842
    cmp_sign(sign), was_values(TRUE)
844
843
  { collation.set(&my_charset_bin); }
845
844
  Item_sum_hybrid(THD *thd, Item_sum_hybrid *item);
846
845
  bool fix_fields(THD *, Item **);
847
846
  void clear();
848
847
  double val_real();
849
 
  int64_t val_int();
 
848
  longlong val_int();
850
849
  my_decimal *val_decimal(my_decimal *);
851
850
  void reset_field();
852
851
  String *val_str(String *);
861
860
  void cleanup();
862
861
  bool any_value() { return was_values; }
863
862
  void no_rows_in_result();
864
 
  Field *create_tmp_field(bool group, Table *table,
865
 
                          uint32_t convert_blob_length);
 
863
  Field *create_tmp_field(bool group, TABLE *table,
 
864
                          uint convert_blob_length);
866
865
};
867
866
 
868
867
 
895
894
class Item_sum_bit :public Item_sum_int
896
895
{
897
896
protected:
898
 
  uint64_t reset_bits,bits;
 
897
  ulonglong reset_bits,bits;
899
898
 
900
899
public:
901
 
  Item_sum_bit(Item *item_par,uint64_t reset_arg)
 
900
  Item_sum_bit(Item *item_par,ulonglong reset_arg)
902
901
    :Item_sum_int(item_par),reset_bits(reset_arg),bits(reset_arg) {}
903
902
  Item_sum_bit(THD *thd, Item_sum_bit *item):
904
903
    Item_sum_int(thd, item), reset_bits(item->reset_bits), bits(item->bits) {}
905
904
  enum Sumfunctype sum_func () const {return SUM_BIT_FUNC;}
906
905
  void clear();
907
 
  int64_t val_int();
 
906
  longlong val_int();
908
907
  void reset_field();
909
908
  void update_field();
910
909
  void fix_length_and_dec()
920
919
class Item_sum_or :public Item_sum_bit
921
920
{
922
921
public:
923
 
  Item_sum_or(Item *item_par) :Item_sum_bit(item_par,0) {}
 
922
  Item_sum_or(Item *item_par) :Item_sum_bit(item_par,LL(0)) {}
924
923
  Item_sum_or(THD *thd, Item_sum_or *item) :Item_sum_bit(thd, item) {}
925
924
  bool add();
926
925
  const char *func_name() const { return "bit_or("; }
931
930
class Item_sum_and :public Item_sum_bit
932
931
{
933
932
  public:
934
 
  Item_sum_and(Item *item_par) :Item_sum_bit(item_par, UINT64_MAX) {}
 
933
  Item_sum_and(Item *item_par) :Item_sum_bit(item_par, ULONGLONG_MAX) {}
935
934
  Item_sum_and(THD *thd, Item_sum_and *item) :Item_sum_bit(thd, item) {}
936
935
  bool add();
937
936
  const char *func_name() const { return "bit_and("; }
941
940
class Item_sum_xor :public Item_sum_bit
942
941
{
943
942
  public:
944
 
  Item_sum_xor(Item *item_par) :Item_sum_bit(item_par,0) {}
 
943
  Item_sum_xor(Item *item_par) :Item_sum_bit(item_par,LL(0)) {}
945
944
  Item_sum_xor(THD *thd, Item_sum_xor *item) :Item_sum_bit(thd, item) {}
946
945
  bool add();
947
946
  const char *func_name() const { return "bit_xor("; }
949
948
};
950
949
 
951
950
 
952
 
 
953
 
class DRIZZLE_ERROR;
 
951
/*
 
952
  User defined aggregates
 
953
*/
 
954
 
 
955
#ifdef HAVE_DLOPEN
 
956
 
 
957
class Item_udf_sum : public Item_sum
 
958
{
 
959
protected:
 
960
  udf_handler udf;
 
961
 
 
962
public:
 
963
  Item_udf_sum(udf_func *udf_arg)
 
964
    :Item_sum(), udf(udf_arg)
 
965
  { quick_group=0; }
 
966
  Item_udf_sum(udf_func *udf_arg, List<Item> &list)
 
967
    :Item_sum(list), udf(udf_arg)
 
968
  { quick_group=0;}
 
969
  Item_udf_sum(THD *thd, Item_udf_sum *item)
 
970
    :Item_sum(thd, item), udf(item->udf)
 
971
  { udf.not_original= TRUE; }
 
972
  const char *func_name() const { return udf.name(); }
 
973
  bool fix_fields(THD *thd, Item **ref)
 
974
  {
 
975
    DBUG_ASSERT(fixed == 0);
 
976
 
 
977
    if (init_sum_func_check(thd))
 
978
      return TRUE;
 
979
 
 
980
    fixed= 1;
 
981
    if (udf.fix_fields(thd, this, this->arg_count, this->args))
 
982
      return TRUE;
 
983
 
 
984
    return check_sum_func(thd, ref);
 
985
  }
 
986
  enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
 
987
  virtual bool have_field_update(void) const { return 0; }
 
988
 
 
989
  void clear();
 
990
  bool add();
 
991
  void reset_field() {};
 
992
  void update_field() {};
 
993
  void cleanup();
 
994
  virtual void print(String *str, enum_query_type query_type);
 
995
};
 
996
 
 
997
 
 
998
class Item_sum_udf_float :public Item_udf_sum
 
999
{
 
1000
 public:
 
1001
  Item_sum_udf_float(udf_func *udf_arg)
 
1002
    :Item_udf_sum(udf_arg) {}
 
1003
  Item_sum_udf_float(udf_func *udf_arg, List<Item> &list)
 
1004
    :Item_udf_sum(udf_arg, list) {}
 
1005
  Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
 
1006
    :Item_udf_sum(thd, item) {}
 
1007
  longlong val_int()
 
1008
  {
 
1009
    DBUG_ASSERT(fixed == 1);
 
1010
    return (longlong) rint(Item_sum_udf_float::val_real());
 
1011
  }
 
1012
  double val_real();
 
1013
  String *val_str(String*str);
 
1014
  my_decimal *val_decimal(my_decimal *);
 
1015
  void fix_length_and_dec() { fix_num_length_and_dec(); }
 
1016
  Item *copy_or_same(THD* thd);
 
1017
};
 
1018
 
 
1019
 
 
1020
class Item_sum_udf_int :public Item_udf_sum
 
1021
{
 
1022
public:
 
1023
  Item_sum_udf_int(udf_func *udf_arg)
 
1024
    :Item_udf_sum(udf_arg) {}
 
1025
  Item_sum_udf_int(udf_func *udf_arg, List<Item> &list)
 
1026
    :Item_udf_sum(udf_arg, list) {}
 
1027
  Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
 
1028
    :Item_udf_sum(thd, item) {}
 
1029
  longlong val_int();
 
1030
  double val_real()
 
1031
    { DBUG_ASSERT(fixed == 1); return (double) Item_sum_udf_int::val_int(); }
 
1032
  String *val_str(String*str);
 
1033
  my_decimal *val_decimal(my_decimal *);
 
1034
  enum Item_result result_type () const { return INT_RESULT; }
 
1035
  void fix_length_and_dec() { decimals=0; max_length=21; }
 
1036
  Item *copy_or_same(THD* thd);
 
1037
};
 
1038
 
 
1039
 
 
1040
class Item_sum_udf_str :public Item_udf_sum
 
1041
{
 
1042
public:
 
1043
  Item_sum_udf_str(udf_func *udf_arg)
 
1044
    :Item_udf_sum(udf_arg) {}
 
1045
  Item_sum_udf_str(udf_func *udf_arg, List<Item> &list)
 
1046
    :Item_udf_sum(udf_arg,list) {}
 
1047
  Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
 
1048
    :Item_udf_sum(thd, item) {}
 
1049
  String *val_str(String *);
 
1050
  double val_real()
 
1051
  {
 
1052
    int err_not_used;
 
1053
    char *end_not_used;
 
1054
    String *res;
 
1055
    res=val_str(&str_value);
 
1056
    return res ? my_strntod(res->charset(),(char*) res->ptr(),res->length(),
 
1057
                            &end_not_used, &err_not_used) : 0.0;
 
1058
  }
 
1059
  longlong val_int()
 
1060
  {
 
1061
    int err_not_used;
 
1062
    char *end;
 
1063
    String *res;
 
1064
    CHARSET_INFO *cs;
 
1065
 
 
1066
    if (!(res= val_str(&str_value)))
 
1067
      return 0;                                 /* Null value */
 
1068
    cs= res->charset();
 
1069
    end= (char*) res->ptr()+res->length();
 
1070
    return cs->cset->strtoll10(cs, res->ptr(), &end, &err_not_used);
 
1071
  }
 
1072
  my_decimal *val_decimal(my_decimal *dec);
 
1073
  enum Item_result result_type () const { return STRING_RESULT; }
 
1074
  void fix_length_and_dec();
 
1075
  Item *copy_or_same(THD* thd);
 
1076
};
 
1077
 
 
1078
 
 
1079
class Item_sum_udf_decimal :public Item_udf_sum
 
1080
{
 
1081
public:
 
1082
  Item_sum_udf_decimal(udf_func *udf_arg)
 
1083
    :Item_udf_sum(udf_arg) {}
 
1084
  Item_sum_udf_decimal(udf_func *udf_arg, List<Item> &list)
 
1085
    :Item_udf_sum(udf_arg, list) {}
 
1086
  Item_sum_udf_decimal(THD *thd, Item_sum_udf_decimal *item)
 
1087
    :Item_udf_sum(thd, item) {}
 
1088
  String *val_str(String *);
 
1089
  double val_real();
 
1090
  longlong val_int();
 
1091
  my_decimal *val_decimal(my_decimal *);
 
1092
  enum Item_result result_type () const { return DECIMAL_RESULT; }
 
1093
  void fix_length_and_dec() { fix_num_length_and_dec(); }
 
1094
  Item *copy_or_same(THD* thd);
 
1095
};
 
1096
 
 
1097
#else /* Dummy functions to get sql_yacc.cc compiled */
 
1098
 
 
1099
class Item_sum_udf_float :public Item_sum_num
 
1100
{
 
1101
 public:
 
1102
  Item_sum_udf_float(udf_func *udf_arg)
 
1103
    :Item_sum_num() {}
 
1104
  Item_sum_udf_float(udf_func *udf_arg, List<Item> &list) :Item_sum_num() {}
 
1105
  Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
 
1106
    :Item_sum_num(thd, item) {}
 
1107
  enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
 
1108
  double val_real() { DBUG_ASSERT(fixed == 1); return 0.0; }
 
1109
  void clear() {}
 
1110
  bool add() { return 0; }  
 
1111
  void update_field() {}
 
1112
};
 
1113
 
 
1114
 
 
1115
class Item_sum_udf_int :public Item_sum_num
 
1116
{
 
1117
public:
 
1118
  Item_sum_udf_int(udf_func *udf_arg)
 
1119
    :Item_sum_num() {}
 
1120
  Item_sum_udf_int(udf_func *udf_arg, List<Item> &list) :Item_sum_num() {}
 
1121
  Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
 
1122
    :Item_sum_num(thd, item) {}
 
1123
  enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
 
1124
  longlong val_int() { DBUG_ASSERT(fixed == 1); return 0; }
 
1125
  double val_real() { DBUG_ASSERT(fixed == 1); return 0; }
 
1126
  void clear() {}
 
1127
  bool add() { return 0; }  
 
1128
  void update_field() {}
 
1129
};
 
1130
 
 
1131
 
 
1132
class Item_sum_udf_decimal :public Item_sum_num
 
1133
{
 
1134
 public:
 
1135
  Item_sum_udf_decimal(udf_func *udf_arg)
 
1136
    :Item_sum_num() {}
 
1137
  Item_sum_udf_decimal(udf_func *udf_arg, List<Item> &list)
 
1138
    :Item_sum_num() {}
 
1139
  Item_sum_udf_decimal(THD *thd, Item_sum_udf_float *item)
 
1140
    :Item_sum_num(thd, item) {}
 
1141
  enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
 
1142
  double val_real() { DBUG_ASSERT(fixed == 1); return 0.0; }
 
1143
  my_decimal *val_decimal(my_decimal *) { DBUG_ASSERT(fixed == 1); return 0; }
 
1144
  void clear() {}
 
1145
  bool add() { return 0; }
 
1146
  void update_field() {}
 
1147
};
 
1148
 
 
1149
 
 
1150
class Item_sum_udf_str :public Item_sum_num
 
1151
{
 
1152
public:
 
1153
  Item_sum_udf_str(udf_func *udf_arg)
 
1154
    :Item_sum_num() {}
 
1155
  Item_sum_udf_str(udf_func *udf_arg, List<Item> &list)
 
1156
    :Item_sum_num() {}
 
1157
  Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
 
1158
    :Item_sum_num(thd, item) {}
 
1159
  String *val_str(String *)
 
1160
    { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
 
1161
  double val_real() { DBUG_ASSERT(fixed == 1); null_value=1; return 0.0; }
 
1162
  longlong val_int() { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
 
1163
  enum Item_result result_type () const { return STRING_RESULT; }
 
1164
  void fix_length_and_dec() { maybe_null=1; max_length=0; }
 
1165
  enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
 
1166
  void clear() {}
 
1167
  bool add() { return 0; }  
 
1168
  void update_field() {}
 
1169
};
 
1170
 
 
1171
#endif /* HAVE_DLOPEN */
 
1172
 
 
1173
class MYSQL_ERROR;
954
1174
 
955
1175
class Item_func_group_concat : public Item_sum
956
1176
{
957
1177
  TMP_TABLE_PARAM *tmp_table_param;
958
 
  DRIZZLE_ERROR *warning;
 
1178
  MYSQL_ERROR *warning;
959
1179
  String result;
960
1180
  String *separator;
961
1181
  TREE tree_base;
969
1189
     @see Item_func_group_concat::clear
970
1190
   */
971
1191
  Unique *unique_filter;
972
 
  Table *table;
973
 
  order_st **order;
 
1192
  TABLE *table;
 
1193
  ORDER **order;
974
1194
  Name_resolution_context *context;
975
1195
  /** The number of ORDER BY items. */
976
 
  uint32_t arg_count_order;
 
1196
  uint arg_count_order;
977
1197
  /** The number of selected items, aka the expr list. */
978
 
  uint32_t arg_count_field;
979
 
  uint32_t count_cut_values;
 
1198
  uint arg_count_field;
 
1199
  uint count_cut_values;
980
1200
  bool distinct;
981
1201
  bool warning_for_row;
982
1202
  bool always_null;
992
1212
                                                const void* key2);
993
1213
  friend int group_concat_key_cmp_with_order(void* arg, const void* key1,
994
1214
                                             const void* key2);
995
 
  friend int dump_leaf_key(unsigned char* key,
 
1215
  friend int dump_leaf_key(uchar* key,
996
1216
                           element_count count __attribute__((unused)),
997
1217
                           Item_func_group_concat *group_concat_item);
998
1218
 
1011
1231
  enum_field_types field_type() const
1012
1232
  {
1013
1233
    if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB )
1014
 
      return DRIZZLE_TYPE_BLOB;
 
1234
      return MYSQL_TYPE_BLOB;
1015
1235
    else
1016
 
      return DRIZZLE_TYPE_VARCHAR;
 
1236
      return MYSQL_TYPE_VARCHAR;
1017
1237
  }
1018
1238
  void clear();
1019
1239
  bool add();
1020
 
  void reset_field() { assert(0); }        // not used
1021
 
  void update_field() { assert(0); }       // not used
 
1240
  void reset_field() { DBUG_ASSERT(0); }        // not used
 
1241
  void update_field() { DBUG_ASSERT(0); }       // not used
1022
1242
  bool fix_fields(THD *,Item **);
1023
1243
  bool setup(THD *thd);
1024
1244
  void make_unique();
1027
1247
    String *res;  res=val_str(&str_value);
1028
1248
    return res ? my_atof(res->c_ptr()) : 0.0;
1029
1249
  }
1030
 
  int64_t val_int()
 
1250
  longlong val_int()
1031
1251
  {
1032
1252
    String *res;
1033
1253
    char *end_ptr;
1034
1254
    int error;
1035
1255
    if (!(res= val_str(&str_value)))
1036
 
      return (int64_t) 0;
 
1256
      return (longlong) 0;
1037
1257
    end_ptr= (char*) res->ptr()+ res->length();
1038
1258
    return my_strtoll10(res->ptr(), &end_ptr, &error);
1039
1259
  }
1045
1265
  Item *copy_or_same(THD* thd);
1046
1266
  void no_rows_in_result() {}
1047
1267
  virtual void print(String *str, enum_query_type query_type);
1048
 
  virtual bool change_context_processor(unsigned char *cntx)
1049
 
    { context= (Name_resolution_context *)cntx; return false; }
 
1268
  virtual bool change_context_processor(uchar *cntx)
 
1269
    { context= (Name_resolution_context *)cntx; return FALSE; }
1050
1270
};