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
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 */
21
17
/* classes for sum functions */
19
#ifdef USE_PRAGMA_INTERFACE
20
#pragma interface /* gcc class implementation */
24
#include <mysys/my_tree.h>
27
26
Class Item_sum is the base class used for special expressions that SQL calls
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
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 */
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 */
244
243
This list is used by the check for mixing non aggregated fields and
365
364
void no_rows_in_result() { clear(); }
367
virtual bool setup(THD *thd __attribute__((unused))) {return 0;}
366
virtual bool setup(THD *thd __attribute__((__unused__))) {return 0;}
368
367
virtual void make_unique(void) {}
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);
756
755
double recurrence_m, recurrence_s; /* Used in recurrence relation. */
758
uint32_t f_precision0, f_scale0;
759
uint32_t f_precision1, f_scale1;
760
uint32_t dec_bin_size0, dec_bin_size1;
762
uint32_t prec_increment;
757
uint f_precision0, f_scale0;
758
uint f_precision1, f_scale1;
759
uint dec_bin_size0, dec_bin_size1;
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)
767
766
Item_sum_variance(THD *thd, Item_sum_variance *item);
807
806
class Item_sum_std :public Item_sum_variance
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)
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 __attribute__((__unused__)))
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;}
825
824
// This class is a string or number function depending on num_func
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;
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);
955
1097
class Item_func_group_concat : public Item_sum
957
1099
TMP_TABLE_PARAM *tmp_table_param;
958
DRIZZLE_ERROR *warning;
1100
MYSQL_ERROR *warning;
960
1102
String *separator;