1 #ifndef ITEM_SUM_INCLUDED 
    2 #define ITEM_SUM_INCLUDED 
   72   enum Aggregator_type { SIMPLE_AGGREGATOR, DISTINCT_AGGREGATOR }; 
 
   73   virtual Aggregator_type Aggrtype() = 0;
 
   80   virtual bool setup(THD *) = 0;
 
   87   virtual void clear() = 0;
 
   93   virtual bool add() = 0;
 
   99   virtual void endup() = 0;
 
  329   bool force_copy_fields;
 
  340   bool has_force_copy_fields()
 const { 
return force_copy_fields; }
 
  341   bool has_with_distinct()
     const { 
return with_distinct; }
 
  344   { COUNT_FUNC, COUNT_DISTINCT_FUNC, SUM_FUNC, SUM_DISTINCT_FUNC, AVG_FUNC,
 
  345     AVG_DISTINCT_FUNC, MIN_FUNC, MAX_FUNC, STD_FUNC,
 
  346     VARIANCE_FUNC, SUM_BIT_FUNC, UDF_SUM_FUNC, GROUP_CONCAT_FUNC
 
  352   st_select_lex * aggr_sel;  
 
  356   int8 max_sum_func_level;
 
  368   Item **args, *tmp_args[2];
 
  374   Item **orig_args, *tmp_orig_args[2];
 
  375   table_map used_tables_cache;
 
  381   void mark_as_sum_func();
 
  382   Item_sum() :next(NULL), quick_group(1), arg_count(0), forced_const(FALSE)
 
  387   Item_sum(
Item *a) :next(NULL), quick_group(1), arg_count(1), args(tmp_args),
 
  388     orig_args(tmp_orig_args), forced_const(FALSE)
 
  394   Item_sum( 
Item *a, 
Item *b ) :next(NULL), quick_group(1), arg_count(2), args(tmp_args),
 
  395     orig_args(tmp_orig_args), forced_const(FALSE)
 
  397     args[0]=a; args[1]=b;
 
  404   enum Type 
type()
 const { 
return SUM_FUNC_ITEM; }
 
  405   virtual enum Sumfunctype sum_func () 
const=0;
 
  425   virtual void reset_field()=0;
 
  431   virtual void update_field()=0;
 
  432   virtual bool keep_field_type(
void)
 const { 
return 0; }
 
  433   virtual void fix_length_and_dec() { maybe_null=1; null_value=1; }
 
  436   table_map used_tables()
 const { 
return used_tables_cache; }
 
  437   void update_used_tables ();
 
  438   bool is_null() { 
return null_value; }
 
  441     used_tables_cache= 0; 
 
  444   virtual bool const_item()
 const { 
return forced_const; }
 
  445   virtual bool const_during_execution()
 const { 
return false; }
 
  446   virtual void print(
String *str, enum_query_type query_type);
 
  447   void fix_num_length_and_dec();
 
  461     set_aggregator(with_distinct ?
 
  462                    Aggregator::DISTINCT_AGGREGATOR :
 
  463                    Aggregator::SIMPLE_AGGREGATOR);
 
  466   virtual void make_unique() { force_copy_fields= TRUE; }
 
  467   Item *get_tmp_table_item(THD *thd);
 
  469   bool walk(Item_processor processor, 
bool walk_subquery, uchar *argument);
 
  474   st_select_lex *depended_from() 
 
  475     { 
return (nest_level == aggr_level ? 0 : aggr_sel); }
 
  477   Item *get_arg(uint 
i) { 
return args[
i]; }
 
  478   Item *set_arg(uint 
i, THD *thd, 
Item *new_val);
 
  479   uint get_arg_count() { 
return arg_count; }
 
  482   void init_aggregator()
 
  485     with_distinct= FALSE;
 
  486     force_copy_fields= FALSE;
 
  508   void set_distinct(
bool distinct)
 
  510     with_distinct= distinct;
 
  511     quick_group= with_distinct ? 0 : 1;
 
  520   int set_aggregator(Aggregator::Aggregator_type aggregator);
 
  522   virtual void clear()= 0;
 
  523   virtual bool add()= 0;
 
  524   virtual bool setup(THD *thd) { 
return false; }
 
  526   virtual void cleanup();
 
  572   uint32 *field_lengths;
 
  578   TMP_TABLE_PARAM *tmp_table_param;
 
  595   uint tree_key_length;
 
  609     always_null(FALSE) {}
 
  611   Aggregator_type Aggrtype() { 
return DISTINCT_AGGREGATOR; }
 
  637   Aggregator_type Aggrtype() { 
return Aggregator::SIMPLE_AGGREGATOR; }
 
  639   bool setup(THD * thd) { 
return item_sum->setup(thd); }
 
  641   bool add() { 
return item_sum->add(); }
 
  662     :
Item_sum(item_par), is_evaluated(FALSE) {}
 
  665     :
Item_sum(list), is_evaluated(FALSE) {}
 
  667     :
Item_sum(thd, item),is_evaluated(item->is_evaluated) {}
 
  668   bool fix_fields(THD *, 
Item **);
 
  671     DBUG_ASSERT(fixed == 1);
 
  672     return (longlong) rint(val_real());             
 
  676   bool get_date(
MYSQL_TIME *ltime, uint fuzzydate)
 
  694   double val_real() { DBUG_ASSERT(fixed == 1); 
return (
double) val_int(); }
 
  697   bool get_date(
MYSQL_TIME *ltime, uint fuzzydate)
 
  705   enum Item_result result_type ()
 const { 
return INT_RESULT; }
 
  706   void fix_length_and_dec()
 
  707   { decimals=0; max_length=21; maybe_null=null_value=0; }
 
  714   Item_result hybrid_type;
 
  718   void fix_length_and_dec();
 
  723     set_distinct(distinct);
 
  726   enum Sumfunctype sum_func ()
 const  
  728     return has_with_distinct() ? SUM_DISTINCT_FUNC : SUM_FUNC; 
 
  736   enum Item_result result_type ()
 const { 
return hybrid_type; }
 
  740   const char *func_name()
 const  
  742     return has_with_distinct() ? 
"sum(distinct " : 
"sum("; 
 
  744   Item *copy_or_same(THD* thd);
 
  779   enum Sumfunctype sum_func ()
 const  
  781     return has_with_distinct() ? COUNT_DISTINCT_FUNC : COUNT_FUNC; 
 
  784   void make_const(longlong count_arg) 
 
  787     Item_sum::make_const();
 
  792   const char *func_name()
 const  
  794     return has_with_distinct() ? 
"count(distinct " : 
"count(";
 
  796   Item *copy_or_same(THD* thd);
 
  813   Item_result hybrid_type;
 
  818     return (longlong) rint(val_real());
 
  820   bool get_date(
MYSQL_TIME *ltime, uint fuzzydate)
 
  828   enum_field_types field_type()
 const 
  830     return hybrid_type == DECIMAL_RESULT ?
 
  831       MYSQL_TYPE_NEWDECIMAL : MYSQL_TYPE_DOUBLE;
 
  833   enum Item_result result_type ()
 const { 
return hybrid_type; }
 
  834   bool is_null() { update_null_value(); 
return null_value; }
 
  841   uint f_precision, f_scale, dec_bin_size;
 
  844   enum Type type()
 const { 
return FIELD_AVG_ITEM; }
 
  848   void fix_length_and_dec() {}
 
  849   const char *func_name()
 const { DBUG_ASSERT(0); 
return "avg_field"; }
 
  858   uint f_precision, f_scale, dec_bin_size;
 
  864     prec_increment(item->prec_increment) {}
 
  866   void fix_length_and_dec();
 
  867   enum Sumfunctype sum_func ()
 const  
  869     return has_with_distinct() ? AVG_DISTINCT_FUNC : AVG_FUNC;
 
  875   longlong val_int() { 
return (longlong) rint(val_real()); }
 
  883   const char *func_name()
 const  
  885     return has_with_distinct() ? 
"avg(distinct " : 
"avg("; 
 
  887   Item *copy_or_same(THD* thd);
 
  892     Item_sum_sum::cleanup();
 
  901   uint f_precision0, f_scale0;
 
  902   uint f_precision1, f_scale1;
 
  903   uint dec_bin_size0, dec_bin_size1;
 
  908   enum Type type()
 const {
return FIELD_VARIANCE_ITEM; }
 
  911   { 
return val_string_from_real(str); }
 
  913   { 
return val_decimal_from_real(dec_buf); }
 
  914   void fix_length_and_dec() {}
 
  915   const char *func_name()
 const { DBUG_ASSERT(0); 
return "variance_field"; }
 
  941   void fix_length_and_dec();
 
  944   Item_result hybrid_type;
 
  946   double recurrence_m, recurrence_s;    
 
  948   uint f_precision0, f_scale0;
 
  949   uint f_precision1, f_scale1;
 
  950   uint dec_bin_size0, dec_bin_size1;
 
  955     hybrid_type(REAL_RESULT), count(0), sample(sample_arg)
 
  958   enum Sumfunctype sum_func ()
 const { 
return VARIANCE_FUNC; }
 
  968   const char *func_name()
 const 
  969     { 
return sample ? 
"var_samp(" : 
"variance("; }
 
  970   Item *copy_or_same(THD* thd);
 
  972   enum Item_result result_type ()
 const { 
return REAL_RESULT; }
 
  976     Item_sum_num::cleanup();
 
  986   enum Type type()
 const { 
return FIELD_STD_ITEM; }
 
  989   enum Item_result result_type ()
 const { 
return REAL_RESULT; }
 
  990   enum_field_types field_type()
 const { 
return MYSQL_TYPE_DOUBLE;}
 
  991   const char *func_name()
 const { DBUG_ASSERT(0); 
return "std_field"; }
 
 1006   enum Sumfunctype sum_func ()
 const { 
return STD_FUNC; }
 
 1010   const char *func_name()
 const { 
return "std("; }
 
 1011   Item *copy_or_same(THD* thd);
 
 1012   enum Item_result result_type ()
 const { 
return REAL_RESULT; }
 
 1013   enum_field_types field_type()
 const { 
return MYSQL_TYPE_DOUBLE;}
 
 1024   Item_result hybrid_type;
 
 1025   enum_field_types hybrid_field_type;
 
 1031     :
Item_sum(item_par), value(0), arg_cache(0), cmp(0),
 
 1032     hybrid_type(INT_RESULT), hybrid_field_type(MYSQL_TYPE_LONGLONG),
 
 1033     cmp_sign(sign), was_values(TRUE)
 
 1034   { collation.set(&my_charset_bin); }
 
 1036     :
Item_sum(thd, item), value(item->value), arg_cache(0),
 
 1037     hybrid_type(item->hybrid_type), hybrid_field_type(item->hybrid_field_type),
 
 1038     cmp_sign(item->cmp_sign), was_values(item->was_values)
 
 1040   bool fix_fields(THD *, 
Item **);
 
 1048   bool get_date(
MYSQL_TIME *ltime, uint fuzzydate);
 
 1052   bool keep_field_type(
void)
 const { 
return 1; }
 
 1053   enum Item_result result_type ()
 const { 
return hybrid_type; }
 
 1054   enum enum_field_types field_type()
 const { 
return hybrid_field_type; }
 
 1055   void update_field();
 
 1056   void min_max_update_str_field();
 
 1057   void min_max_update_temporal_field();
 
 1058   void min_max_update_real_field();
 
 1059   void min_max_update_int_field();
 
 1062   bool any_value() { 
return was_values; }
 
 1073   enum Sumfunctype sum_func ()
 const {
return MIN_FUNC;}
 
 1076   const char *func_name()
 const { 
return "min("; }
 
 1077   Item *copy_or_same(THD* thd);
 
 1086   enum Sumfunctype sum_func ()
 const {
return MAX_FUNC;}
 
 1089   const char *func_name()
 const { 
return "max("; }
 
 1090   Item *copy_or_same(THD* thd);
 
 1097   ulonglong reset_bits,bits;
 
 1101     :
Item_sum_int(item_par),reset_bits(reset_arg),bits(reset_arg) {}
 
 1103     Item_sum_int(thd, item), reset_bits(item->reset_bits), bits(item->bits) {}
 
 1104   enum Sumfunctype sum_func ()
 const {
return SUM_BIT_FUNC;}
 
 1108   void update_field();
 
 1109   void fix_length_and_dec()
 
 1110   { decimals= 0; max_length=21; unsigned_flag= 1; maybe_null= null_value= 0; }
 
 1114     Item_sum_int::cleanup();
 
 1125   const char *func_name()
 const { 
return "bit_or("; }
 
 1126   Item *copy_or_same(THD* thd);
 
 1136   const char *func_name()
 const { 
return "bit_and("; }
 
 1137   Item *copy_or_same(THD* thd);
 
 1146   const char *func_name()
 const { 
return "bit_xor("; }
 
 1147   Item *copy_or_same(THD* thd);
 
 1157 class Item_udf_sum : 
public Item_sum 
 1169   Item_udf_sum(THD *thd, Item_udf_sum *item)
 
 1170     :
Item_sum(thd, item), udf(item->udf)
 
 1171   { udf.not_original= TRUE; }
 
 1172   const char *func_name()
 const { 
return udf.name(); }
 
 1173   bool fix_fields(THD *thd, 
Item **ref)
 
 1175     DBUG_ASSERT(fixed == 0);
 
 1181     if (udf.fix_fields(thd, 
this, this->arg_count, this->args))
 
 1184     memcpy (orig_args, args, 
sizeof (
Item *) * arg_count);
 
 1187   enum Sumfunctype sum_func ()
 const { 
return UDF_SUM_FUNC; }
 
 1188   virtual bool have_field_update(
void)
 const { 
return 0; }
 
 1192   void reset_field() {};
 
 1193   void update_field() {};
 
 1195   virtual void print(
String *str, enum_query_type query_type);
 
 1203     :Item_udf_sum(udf_arg) {}
 
 1205     :Item_udf_sum(udf_arg, list) {}
 
 1207     :Item_udf_sum(thd, item) {}
 
 1210     DBUG_ASSERT(fixed == 1);
 
 1211     return (longlong) rint(Item_sum_udf_float::val_real());
 
 1216   bool get_date(
MYSQL_TIME *ltime, uint fuzzydate)
 
 1224   void fix_length_and_dec() { fix_num_length_and_dec(); }
 
 1225   Item *copy_or_same(THD* thd);
 
 1233     :Item_udf_sum(udf_arg) {}
 
 1235     :Item_udf_sum(udf_arg, list) {}
 
 1237     :Item_udf_sum(thd, item) {}
 
 1240     { DBUG_ASSERT(fixed == 1); 
return (
double) Item_sum_udf_int::val_int(); }
 
 1243   bool get_date(
MYSQL_TIME *ltime, uint fuzzydate)
 
 1251   enum Item_result result_type ()
 const { 
return INT_RESULT; }
 
 1252   void fix_length_and_dec() { decimals=0; max_length=21; }
 
 1253   Item *copy_or_same(THD* thd);
 
 1261     :Item_udf_sum(udf_arg) {}
 
 1263     :Item_udf_sum(udf_arg,list) {}
 
 1265     :Item_udf_sum(thd, item) {}
 
 1272     res=val_str(&str_value);
 
 1273     return res ? my_strntod(res->charset(),(
char*) res->ptr(),res->length(),
 
 1274                             &end_not_used, &err_not_used) : 0.0;
 
 1283     if (!(res= val_str(&str_value)))
 
 1286     end= (
char*) res->ptr()+res->length();
 
 1287     return cs->cset->strtoll10(cs, res->ptr(), &end, &err_not_used);
 
 1290   bool get_date(
MYSQL_TIME *ltime, uint fuzzydate)
 
 1298   enum Item_result result_type ()
 const { 
return STRING_RESULT; }
 
 1299   void fix_length_and_dec();
 
 1300   Item *copy_or_same(THD* thd);
 
 1308     :Item_udf_sum(udf_arg) {}
 
 1310     :Item_udf_sum(udf_arg, list) {}
 
 1312     :Item_udf_sum(thd, item) {}
 
 1317   bool get_date(
MYSQL_TIME *ltime, uint fuzzydate)
 
 1325   enum Item_result result_type ()
 const { 
return DECIMAL_RESULT; }
 
 1326   void fix_length_and_dec() { fix_num_length_and_dec(); }
 
 1327   Item *copy_or_same(THD* thd);
 
 1340   enum Sumfunctype sum_func ()
 const { 
return UDF_SUM_FUNC; }
 
 1341   double val_real() { DBUG_ASSERT(fixed == 1); 
return 0.0; }
 
 1343   bool add() { 
return 0; }  
 
 1344   void update_field() {}
 
 1356   enum Sumfunctype sum_func ()
 const { 
return UDF_SUM_FUNC; }
 
 1357   longlong val_int() { DBUG_ASSERT(fixed == 1); 
return 0; }
 
 1358   double val_real() { DBUG_ASSERT(fixed == 1); 
return 0; }
 
 1360   bool add() { 
return 0; }  
 
 1361   void update_field() {}
 
 1374   enum Sumfunctype sum_func ()
 const { 
return UDF_SUM_FUNC; }
 
 1375   double val_real() { DBUG_ASSERT(fixed == 1); 
return 0.0; }
 
 1378   bool add() { 
return 0; }
 
 1379   void update_field() {}
 
 1393     { DBUG_ASSERT(fixed == 1); null_value=1; 
return 0; }
 
 1394   double val_real() { DBUG_ASSERT(fixed == 1); null_value=1; 
return 0.0; }
 
 1395   longlong val_int() { DBUG_ASSERT(fixed == 1); null_value=1; 
return 0; }
 
 1396   enum Item_result result_type ()
 const { 
return STRING_RESULT; }
 
 1397   void fix_length_and_dec() { maybe_null=1; max_length=0; }
 
 1398   enum Sumfunctype sum_func ()
 const { 
return UDF_SUM_FUNC; }
 
 1400   bool add() { 
return 0; }  
 
 1401   void update_field() {}
 
 1412                   element_count count __attribute__((unused)),
 
 1418   TMP_TABLE_PARAM *tmp_table_param;
 
 1431   Unique *unique_filter;
 
 1436   uint arg_count_order;
 
 1438   uint arg_count_field;
 
 1441   bool warning_for_row;
 
 1443   bool force_copy_fields;
 
 1458                            element_count count __attribute__((unused)),
 
 1470   enum Sumfunctype sum_func ()
 const {
return GROUP_CONCAT_FUNC;}
 
 1471   const char *func_name()
 const { 
return "group_concat"; }
 
 1472   virtual Item_result result_type ()
 const { 
return STRING_RESULT; }
 
 1474   enum_field_types field_type()
 const 
 1476     if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB )
 
 1477       return MYSQL_TYPE_BLOB;
 
 1479       return MYSQL_TYPE_VARCHAR;
 
 1483   void reset_field() { DBUG_ASSERT(0); }        
 
 1484   void update_field() { DBUG_ASSERT(0); }       
 
 1485   bool fix_fields(THD *,
Item **);
 
 1486   bool setup(THD *thd);
 
 1490     String *res;  res=val_str(&str_value);
 
 1491     return res ? my_atof(res->c_ptr()) : 0.0;
 
 1498     if (!(res= val_str(&str_value)))
 
 1499       return (longlong) 0;
 
 1500     end_ptr= (
char*) res->ptr()+ res->length();
 
 1501     return my_strtoll10(res->ptr(), &end_ptr, &error);
 
 1505     return val_decimal_from_string(decimal_value);
 
 1507   bool get_date(
MYSQL_TIME *ltime, uint fuzzydate)
 
 1516   Item *copy_or_same(THD* thd);
 
 1518   virtual void print(
String *str, enum_query_type query_type);
 
 1519   virtual bool change_context_processor(uchar *cntx)