1
/* Copyright (C) 2000-2006 MySQL AB
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.
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.
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 */
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
20
#ifndef DRIZZLED_ITEM_SUM_H
21
#define DRIZZLED_ITEM_SUM_H
17
23
/* classes for sum functions */
19
#ifdef USE_PRAGMA_INTERFACE
20
#pragma interface /* gcc class implementation */
23
#include <mysys/my_tree.h>
26
#include "drizzled/my_tree.h"
27
#include <drizzled/hybrid_type.h>
28
#include <drizzled/item.h>
29
#include <drizzled/item/field.h>
30
#include <drizzled/item/bin_string.h>
33
int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
37
int group_concat_key_cmp_with_order(void* arg, const void* key1,
26
44
Class Item_sum is the base class used for special expressions that SQL calls
226
242
{ COUNT_FUNC, COUNT_DISTINCT_FUNC, SUM_FUNC, SUM_DISTINCT_FUNC, AVG_FUNC,
227
243
AVG_DISTINCT_FUNC, MIN_FUNC, MAX_FUNC, STD_FUNC,
228
VARIANCE_FUNC, SUM_BIT_FUNC, UDF_SUM_FUNC, GROUP_CONCAT_FUNC
244
VARIANCE_FUNC, SUM_BIT_FUNC, GROUP_CONCAT_FUNC
231
247
Item **args, *tmp_args[2];
232
248
Item **ref_by; /* pointer to a ref to the object used to register it */
233
249
Item_sum *next; /* next in the circular chain of registered objects */
235
Item_sum *in_sum_func; /* embedding set function if any */
236
st_select_lex * aggr_sel; /* select where the function is aggregated */
251
Item_sum *in_sum_func; /* embedding set function if any */
252
Select_Lex * aggr_sel; /* select where the function is aggregated */
237
253
int8_t nest_level; /* number of the nesting level of the set function */
238
254
int8_t aggr_level; /* nesting level of the aggregating subquery */
239
255
int8_t max_arg_level; /* max level of unbound column references */
364
380
void no_rows_in_result() { clear(); }
366
virtual bool setup(THD *thd __attribute__((unused))) {return 0;}
382
virtual bool setup(Session *) {return 0;}
367
383
virtual void make_unique(void) {}
368
Item *get_tmp_table_item(THD *thd);
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);
372
bool init_sum_func_check(THD *thd);
373
bool check_sum_func(THD *thd, Item **ref);
374
bool register_sum_func(THD *thd, Item **ref);
375
st_select_lex *depended_from()
384
Item *get_tmp_table_item(Session *session);
385
virtual Field *create_tmp_field(bool group, Table *table,
386
uint32_t convert_blob_length);
387
bool walk(Item_processor processor, bool walk_subquery, unsigned char *argument);
388
bool init_sum_func_check(Session *session);
389
bool check_sum_func(Session *session, Item **ref);
390
bool register_sum_func(Session *session, Item **ref);
391
Select_Lex *depended_from()
376
392
{ return (nest_level == aggr_level ? 0 : aggr_sel); }
384
val_xxx() functions may be called several times during the execution of a
400
val_xxx() functions may be called several times during the execution of a
385
401
query. Derived classes that require extensive calculation in val_xxx()
386
maintain cache of aggregate value. This variable governs the validity of
402
maintain cache of aggregate value. This variable governs the validity of
389
405
bool is_evaluated;
391
407
Item_sum_num() :Item_sum(),is_evaluated(false) {}
392
Item_sum_num(Item *item_par)
408
Item_sum_num(Item *item_par)
393
409
:Item_sum(item_par), is_evaluated(false) {}
394
410
Item_sum_num(Item *a, Item* b) :Item_sum(a,b),is_evaluated(false) {}
395
Item_sum_num(List<Item> &list)
411
Item_sum_num(List<Item> &list)
396
412
:Item_sum(list), is_evaluated(false) {}
397
Item_sum_num(THD *thd, Item_sum_num *item)
398
:Item_sum(thd, item),is_evaluated(item->is_evaluated) {}
399
bool fix_fields(THD *, Item **);
403
return (int64_t) rint(val_real()); /* Real as default */
413
Item_sum_num(Session *session, Item_sum_num *item)
414
:Item_sum(session, item),is_evaluated(item->is_evaluated) {}
415
bool fix_fields(Session *, Item **);
405
417
String *val_str(String*str);
406
418
my_decimal *val_decimal(my_decimal *);
407
419
void reset_field();
603
615
force_copy_fields(0), tree(0), count(0),
604
616
original(0), always_null(false)
605
617
{ quick_group= 0; }
606
Item_sum_count_distinct(THD *thd, Item_sum_count_distinct *item)
607
:Item_sum_int(thd, item), table(item->table),
618
Item_sum_count_distinct(Session *session, Item_sum_count_distinct *item)
619
:Item_sum_int(session, item), table(item->table),
608
620
field_lengths(item->field_lengths),
609
621
tmp_table_param(item->tmp_table_param),
610
force_copy_fields(0), tree(item->tree), count(item->count),
622
force_copy_fields(0), tree(item->tree), count(item->count),
611
623
original(item), tree_key_length(item->tree_key_length),
612
624
always_null(item->always_null)
731
742
= sum (ai - avg(a))^2 / count(a) )
732
743
= sum (ai^2 - 2*ai*avg(a) + avg(a)^2) / count(a)
733
= (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) =
734
= (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) =
735
= (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2 )/count(a) =
736
= (sum(ai^2) - 2*sum(a)^2/count(a) + sum(a)^2/count(a) )/count(a) =
744
= (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) =
745
= (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) =
746
= (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2 )/count(a) =
747
= (sum(ai^2) - 2*sum(a)^2/count(a) + sum(a)^2/count(a) )/count(a) =
737
748
= (sum(ai^2) - sum(a)^2/count(a))/count(a)
739
750
But, this falls prey to catastrophic cancellation. Instead, use the recurrence formulas
741
M_{1} = x_{1}, ~ M_{k} = M_{k-1} + (x_{k} - M_{k-1}) / k newline
752
M_{1} = x_{1}, ~ M_{k} = M_{k-1} + (x_{k} - M_{k-1}) / k newline
742
753
S_{1} = 0, ~ S_{k} = S_{k-1} + (x_{k} - M_{k-1}) times (x_{k} - M_{k}) newline
743
754
for 2 <= k <= n newline
744
755
ital variance = S_{n} / (n-1)
755
766
double recurrence_m, recurrence_s; /* Used in recurrence relation. */
757
uint f_precision0, f_scale0;
758
uint f_precision1, f_scale1;
759
uint dec_bin_size0, dec_bin_size1;
768
uint32_t f_precision0, f_scale0;
769
uint32_t f_precision1, f_scale1;
770
uint32_t dec_bin_size0, dec_bin_size1;
772
uint32_t prec_increment;
763
Item_sum_variance(Item *item_par, uint sample_arg) :Item_sum_num(item_par),
774
Item_sum_variance(Item *item_par, uint32_t sample_arg) :Item_sum_num(item_par),
764
775
hybrid_type(REAL_RESULT), count(0), sample(sample_arg)
766
Item_sum_variance(THD *thd, Item_sum_variance *item);
777
Item_sum_variance(Session *session, Item_sum_variance *item);
767
778
enum Sumfunctype sum_func () const { return VARIANCE_FUNC; }
770
781
double val_real();
771
783
my_decimal *val_decimal(my_decimal *);
772
784
void reset_field();
773
785
void update_field();
774
Item *result_item(Field *field __attribute__((unused)))
786
Item *result_item(Field *)
775
787
{ return new Item_variance_field(this); }
776
788
void no_rows_in_result() {}
777
789
const char *func_name() const
778
790
{ return sample ? "var_samp(" : "variance("; }
779
Item *copy_or_same(THD* thd);
780
Field *create_tmp_field(bool group, TABLE *table, uint convert_blob_length);
791
Item *copy_or_same(Session* session);
792
Field *create_tmp_field(bool group, Table *table, uint32_t convert_blob_length);
781
793
enum Item_result result_type () const { return REAL_RESULT; }
806
818
class Item_sum_std :public Item_sum_variance
809
Item_sum_std(Item *item_par, uint sample_arg)
821
Item_sum_std(Item *item_par, uint32_t sample_arg)
810
822
:Item_sum_variance(item_par, sample_arg) {}
811
Item_sum_std(THD *thd, Item_sum_std *item)
812
:Item_sum_variance(thd, item)
823
Item_sum_std(Session *session, Item_sum_std *item)
824
:Item_sum_variance(session, item)
814
826
enum Sumfunctype sum_func () const { return STD_FUNC; }
815
827
double val_real();
816
Item *result_item(Field *field __attribute__((unused)))
828
Item *result_item(Field *)
817
829
{ return new Item_std_field(this); }
818
830
const char *func_name() const { return "std("; }
819
Item *copy_or_same(THD* thd);
831
Item *copy_or_same(Session* session);
820
832
enum Item_result result_type () const { return REAL_RESULT; }
821
833
enum_field_types field_type() const { return DRIZZLE_TYPE_DOUBLE;}
933
945
Item_sum_and(Item *item_par) :Item_sum_bit(item_par, UINT64_MAX) {}
934
Item_sum_and(THD *thd, Item_sum_and *item) :Item_sum_bit(thd, item) {}
946
Item_sum_and(Session *session, Item_sum_and *item) :Item_sum_bit(session, item) {}
936
948
const char *func_name() const { return "bit_and("; }
937
Item *copy_or_same(THD* thd);
949
Item *copy_or_same(Session* session);
940
952
class Item_sum_xor :public Item_sum_bit
943
Item_sum_xor(Item *item_par) :Item_sum_bit(item_par,0LL) {}
944
Item_sum_xor(THD *thd, Item_sum_xor *item) :Item_sum_bit(thd, item) {}
955
Item_sum_xor(Item *item_par) :Item_sum_bit(item_par,0) {}
956
Item_sum_xor(Session *session, Item_sum_xor *item) :Item_sum_bit(session, item) {}
946
958
const char *func_name() const { return "bit_xor("; }
947
Item *copy_or_same(THD* thd);
952
User defined aggregates
955
class Item_udf_sum : public Item_sum
961
Item_udf_sum(udf_func *udf_arg)
962
:Item_sum(), udf(udf_arg)
964
Item_udf_sum(udf_func *udf_arg, List<Item> &list)
965
:Item_sum(list), udf(udf_arg)
967
Item_udf_sum(THD *thd, Item_udf_sum *item)
968
:Item_sum(thd, item), udf(item->udf)
969
{ udf.not_original= true; }
970
const char *func_name() const { return udf.name(); }
971
bool fix_fields(THD *thd, Item **ref)
975
if (init_sum_func_check(thd))
979
if (udf.fix_fields(thd, this, this->arg_count, this->args))
982
return check_sum_func(thd, ref);
984
enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
985
virtual bool have_field_update(void) const { return 0; }
989
void reset_field() {};
990
void update_field() {};
992
virtual void print(String *str, enum_query_type query_type);
996
class Item_sum_udf_float :public Item_udf_sum
999
Item_sum_udf_float(udf_func *udf_arg)
1000
:Item_udf_sum(udf_arg) {}
1001
Item_sum_udf_float(udf_func *udf_arg, List<Item> &list)
1002
:Item_udf_sum(udf_arg, list) {}
1003
Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
1004
:Item_udf_sum(thd, item) {}
1008
return (int64_t) rint(Item_sum_udf_float::val_real());
1011
String *val_str(String*str);
1012
my_decimal *val_decimal(my_decimal *);
1013
void fix_length_and_dec() { fix_num_length_and_dec(); }
1014
Item *copy_or_same(THD* thd);
1018
class Item_sum_udf_int :public Item_udf_sum
1021
Item_sum_udf_int(udf_func *udf_arg)
1022
:Item_udf_sum(udf_arg) {}
1023
Item_sum_udf_int(udf_func *udf_arg, List<Item> &list)
1024
:Item_udf_sum(udf_arg, list) {}
1025
Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
1026
:Item_udf_sum(thd, item) {}
1029
{ assert(fixed == 1); return (double) Item_sum_udf_int::val_int(); }
1030
String *val_str(String*str);
1031
my_decimal *val_decimal(my_decimal *);
1032
enum Item_result result_type () const { return INT_RESULT; }
1033
void fix_length_and_dec() { decimals=0; max_length=21; }
1034
Item *copy_or_same(THD* thd);
1038
class Item_sum_udf_str :public Item_udf_sum
1041
Item_sum_udf_str(udf_func *udf_arg)
1042
:Item_udf_sum(udf_arg) {}
1043
Item_sum_udf_str(udf_func *udf_arg, List<Item> &list)
1044
:Item_udf_sum(udf_arg,list) {}
1045
Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
1046
:Item_udf_sum(thd, item) {}
1047
String *val_str(String *);
1053
res=val_str(&str_value);
1054
return res ? my_strntod(res->charset(),(char*) res->ptr(),res->length(),
1055
&end_not_used, &err_not_used) : 0.0;
1062
const CHARSET_INFO *cs;
1064
if (!(res= val_str(&str_value)))
1065
return 0; /* Null value */
1067
end= (char*) res->ptr()+res->length();
1068
return cs->cset->strtoll10(cs, res->ptr(), &end, &err_not_used);
1070
my_decimal *val_decimal(my_decimal *dec);
1071
enum Item_result result_type () const { return STRING_RESULT; }
1072
void fix_length_and_dec();
1073
Item *copy_or_same(THD* thd);
1077
class Item_sum_udf_decimal :public Item_udf_sum
1080
Item_sum_udf_decimal(udf_func *udf_arg)
1081
:Item_udf_sum(udf_arg) {}
1082
Item_sum_udf_decimal(udf_func *udf_arg, List<Item> &list)
1083
:Item_udf_sum(udf_arg, list) {}
1084
Item_sum_udf_decimal(THD *thd, Item_sum_udf_decimal *item)
1085
:Item_udf_sum(thd, item) {}
1086
String *val_str(String *);
1089
my_decimal *val_decimal(my_decimal *);
1090
enum Item_result result_type () const { return DECIMAL_RESULT; }
1091
void fix_length_and_dec() { fix_num_length_and_dec(); }
1092
Item *copy_or_same(THD* thd);
959
Item *copy_or_same(Session* session);
1095
964
class DRIZZLE_ERROR;
1097
966
class Item_func_group_concat : public Item_sum
1099
TMP_TABLE_PARAM *tmp_table_param;
968
Tmp_Table_Param *tmp_table_param;
1100
969
DRIZZLE_ERROR *warning;
1102
971
String *separator;
1162
1030
void reset_field() { assert(0); } // not used
1163
1031
void update_field() { assert(0); } // not used
1164
bool fix_fields(THD *,Item **);
1165
bool setup(THD *thd);
1032
bool fix_fields(Session *,Item **);
1033
bool setup(Session *session);
1166
1034
void make_unique();
1169
String *res; res=val_str(&str_value);
1170
return res ? my_atof(res->c_ptr()) : 0.0;
1177
if (!(res= val_str(&str_value)))
1179
end_ptr= (char*) res->ptr()+ res->length();
1180
return my_strtoll10(res->ptr(), &end_ptr, &error);
1182
1037
my_decimal *val_decimal(my_decimal *decimal_value)
1184
1039
return val_decimal_from_string(decimal_value);
1186
1041
String* val_str(String* str);
1187
Item *copy_or_same(THD* thd);
1042
Item *copy_or_same(Session* session);
1188
1043
void no_rows_in_result() {}
1189
1044
virtual void print(String *str, enum_query_type query_type);
1190
virtual bool change_context_processor(uchar *cntx)
1045
virtual bool change_context_processor(unsigned char *cntx)
1191
1046
{ context= (Name_resolution_context *)cntx; return false; }
1049
#endif /* DRIZZLED_ITEM_SUM_H */