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
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 */
23
17
/* classes for sum functions */
26
#include "drizzled/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>
35
int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
38
int group_concat_key_cmp_with_order(void* arg, const void* key1,
19
#ifdef USE_PRAGMA_INTERFACE
20
#pragma interface /* gcc class implementation */
45
26
Class Item_sum is the base class used for special expressions that SQL calls
401
val_xxx() functions may be called several times during the execution of a
384
val_xxx() functions may be called several times during the execution of a
402
385
query. Derived classes that require extensive calculation in val_xxx()
403
maintain cache of aggregate value. This variable governs the validity of
386
maintain cache of aggregate value. This variable governs the validity of
406
389
bool is_evaluated;
408
Item_sum_num() :Item_sum(),is_evaluated(false) {}
409
Item_sum_num(Item *item_par)
410
:Item_sum(item_par), is_evaluated(false) {}
411
Item_sum_num(Item *a, Item* b) :Item_sum(a,b),is_evaluated(false) {}
412
Item_sum_num(List<Item> &list)
413
:Item_sum(list), is_evaluated(false) {}
414
Item_sum_num(Session *session, Item_sum_num *item)
415
:Item_sum(session, item),is_evaluated(item->is_evaluated) {}
416
bool fix_fields(Session *, Item **);
391
Item_sum_num() :Item_sum(),is_evaluated(FALSE) {}
392
Item_sum_num(Item *item_par)
393
:Item_sum(item_par), is_evaluated(FALSE) {}
394
Item_sum_num(Item *a, Item* b) :Item_sum(a,b),is_evaluated(FALSE) {}
395
Item_sum_num(List<Item> &list)
396
: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 **);
402
DBUG_ASSERT(fixed == 1);
403
return (longlong) rint(val_real()); /* Real as default */
418
405
String *val_str(String*str);
419
406
my_decimal *val_decimal(my_decimal *);
420
407
void reset_field();
533
520
class Item_sum_avg_distinct: public Item_sum_distinct
536
Item_sum_avg_distinct(Session *session, Item_sum_avg_distinct *original)
537
:Item_sum_distinct(session, original) {}
523
Item_sum_avg_distinct(THD *thd, Item_sum_avg_distinct *original)
524
:Item_sum_distinct(thd, original) {}
539
uint32_t prec_increment;
540
527
Item_sum_avg_distinct(Item *item_arg) : Item_sum_distinct(item_arg) {}
542
529
void fix_length_and_dec();
543
530
virtual void calculate_val_and_count();
544
531
enum Sumfunctype sum_func () const { return AVG_DISTINCT_FUNC; }
545
532
const char *func_name() const { return "avg(distinct "; }
546
Item *copy_or_same(Session* session) { return new Item_sum_avg_distinct(session, this); }
533
Item *copy_or_same(THD* thd) { return new Item_sum_avg_distinct(thd, this); }
550
537
class Item_sum_count :public Item_sum_int
555
542
Item_sum_count(Item *item_par)
556
543
:Item_sum_int(item_par),count(0)
558
Item_sum_count(Session *session, Item_sum_count *item)
559
:Item_sum_int(session, item), count(item->count)
545
Item_sum_count(THD *thd, Item_sum_count *item)
546
:Item_sum_int(thd, item), count(item->count)
561
548
enum Sumfunctype sum_func () const { return COUNT_FUNC; }
563
550
void no_rows_in_result() { count=0; }
565
void make_const_count(int64_t count_arg)
552
void make_const(longlong count_arg)
568
555
Item_sum::make_const();
571
558
void reset_field();
573
560
void update_field();
574
561
const char *func_name() const { return "count("; }
575
Item *copy_or_same(Session* session);
562
Item *copy_or_same(THD* thd);
579
class Tmp_Table_Param;
566
class TMP_TABLE_PARAM;
581
568
class Item_sum_count_distinct :public Item_sum_int
584
uint32_t *field_lengths;
585
Tmp_Table_Param *tmp_table_param;
571
uint32 *field_lengths;
572
TMP_TABLE_PARAM *tmp_table_param;
586
573
bool force_copy_fields;
588
575
If there are no blobs, we can use a tree, which
595
582
will not recalculate on each call. Validitiy of the value is stored in
600
Following is 0 normal object and pointer to original one for copy
587
Following is 0 normal object and pointer to original one for copy
601
588
(to correctly free resources)
603
590
Item_sum_count_distinct *original;
604
uint32_t tree_key_length;
591
uint tree_key_length;
607
594
bool always_null; // Set to 1 if the result is always NULL
610
friend int composite_key_cmp(void* arg, unsigned char* key1, unsigned char* key2);
611
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);
614
601
Item_sum_count_distinct(List<Item> &list)
615
602
:Item_sum_int(list), table(0), field_lengths(0), tmp_table_param(0),
616
603
force_copy_fields(0), tree(0), count(0),
617
original(0), always_null(false)
604
original(0), always_null(FALSE)
618
605
{ quick_group= 0; }
619
Item_sum_count_distinct(Session *session, Item_sum_count_distinct *item)
620
:Item_sum_int(session, item), table(item->table),
606
Item_sum_count_distinct(THD *thd, Item_sum_count_distinct *item)
607
:Item_sum_int(thd, item), table(item->table),
621
608
field_lengths(item->field_lengths),
622
609
tmp_table_param(item->tmp_table_param),
623
force_copy_fields(0), tree(item->tree), count(item->count),
610
force_copy_fields(0), tree(item->tree), count(item->count),
624
611
original(item), tree_key_length(item->tree_key_length),
625
612
always_null(item->always_null)
765
753
Item_result hybrid_type;
767
755
double recurrence_m, recurrence_s; /* Used in recurrence relation. */
769
uint32_t f_precision0, f_scale0;
770
uint32_t f_precision1, f_scale1;
771
uint32_t dec_bin_size0, dec_bin_size1;
773
uint32_t prec_increment;
757
uint f_precision0, f_scale0;
758
uint f_precision1, f_scale1;
759
uint dec_bin_size0, dec_bin_size1;
775
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),
776
764
hybrid_type(REAL_RESULT), count(0), sample(sample_arg)
778
Item_sum_variance(Session *session, Item_sum_variance *item);
766
Item_sum_variance(THD *thd, Item_sum_variance *item);
779
767
enum Sumfunctype sum_func () const { return VARIANCE_FUNC; }
782
770
double val_real();
784
771
my_decimal *val_decimal(my_decimal *);
785
772
void reset_field();
786
773
void update_field();
787
Item *result_item(Field *)
774
Item *result_item(Field *field __attribute__((__unused__)))
788
775
{ return new Item_variance_field(this); }
789
776
void no_rows_in_result() {}
790
777
const char *func_name() const
791
778
{ return sample ? "var_samp(" : "variance("; }
792
Item *copy_or_same(Session* session);
793
Field *create_tmp_field(bool group, Table *table, uint32_t convert_blob_length);
779
Item *copy_or_same(THD* thd);
780
Field *create_tmp_field(bool group, TABLE *table, uint convert_blob_length);
794
781
enum Item_result result_type () const { return REAL_RESULT; }
932
919
class Item_sum_or :public Item_sum_bit
935
Item_sum_or(Item *item_par) :Item_sum_bit(item_par,0) {}
936
Item_sum_or(Session *session, Item_sum_or *item) :Item_sum_bit(session, item) {}
922
Item_sum_or(Item *item_par) :Item_sum_bit(item_par,0LL) {}
923
Item_sum_or(THD *thd, Item_sum_or *item) :Item_sum_bit(thd, item) {}
938
925
const char *func_name() const { return "bit_or("; }
939
Item *copy_or_same(Session* session);
926
Item *copy_or_same(THD* thd);
943
930
class Item_sum_and :public Item_sum_bit
946
Item_sum_and(Item *item_par) :Item_sum_bit(item_par, UINT64_MAX) {}
947
Item_sum_and(Session *session, Item_sum_and *item) :Item_sum_bit(session, item) {}
933
Item_sum_and(Item *item_par) :Item_sum_bit(item_par, ULONGLONG_MAX) {}
934
Item_sum_and(THD *thd, Item_sum_and *item) :Item_sum_bit(thd, item) {}
949
936
const char *func_name() const { return "bit_and("; }
950
Item *copy_or_same(Session* session);
937
Item *copy_or_same(THD* thd);
953
940
class Item_sum_xor :public Item_sum_bit
956
Item_sum_xor(Item *item_par) :Item_sum_bit(item_par,0) {}
957
Item_sum_xor(Session *session, Item_sum_xor *item) :Item_sum_bit(session, item) {}
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) {}
959
946
const char *func_name() const { return "bit_xor("; }
960
Item *copy_or_same(Session* session);
947
Item *copy_or_same(THD* thd);
952
User defined aggregates
957
class Item_udf_sum : public Item_sum
963
Item_udf_sum(udf_func *udf_arg)
964
:Item_sum(), udf(udf_arg)
966
Item_udf_sum(udf_func *udf_arg, List<Item> &list)
967
:Item_sum(list), udf(udf_arg)
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)
975
DBUG_ASSERT(fixed == 0);
977
if (init_sum_func_check(thd))
981
if (udf.fix_fields(thd, this, this->arg_count, this->args))
984
return check_sum_func(thd, ref);
986
enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
987
virtual bool have_field_update(void) const { return 0; }
991
void reset_field() {};
992
void update_field() {};
994
virtual void print(String *str, enum_query_type query_type);
998
class Item_sum_udf_float :public Item_udf_sum
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) {}
1009
DBUG_ASSERT(fixed == 1);
1010
return (longlong) rint(Item_sum_udf_float::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);
1020
class Item_sum_udf_int :public Item_udf_sum
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) {}
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);
1040
class Item_sum_udf_str :public Item_udf_sum
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 *);
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;
1066
if (!(res= val_str(&str_value)))
1067
return 0; /* Null value */
1069
end= (char*) res->ptr()+res->length();
1070
return cs->cset->strtoll10(cs, res->ptr(), &end, &err_not_used);
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);
1079
class Item_sum_udf_decimal :public Item_udf_sum
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 *);
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);
1097
#else /* Dummy functions to get sql_yacc.cc compiled */
1099
class Item_sum_udf_float :public Item_sum_num
1102
Item_sum_udf_float(udf_func *udf_arg)
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; }
1110
bool add() { return 0; }
1111
void update_field() {}
1115
class Item_sum_udf_int :public Item_sum_num
1118
Item_sum_udf_int(udf_func *udf_arg)
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; }
1127
bool add() { return 0; }
1128
void update_field() {}
1132
class Item_sum_udf_decimal :public Item_sum_num
1135
Item_sum_udf_decimal(udf_func *udf_arg)
1137
Item_sum_udf_decimal(udf_func *udf_arg, List<Item> &list)
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; }
1145
bool add() { return 0; }
1146
void update_field() {}
1150
class Item_sum_udf_str :public Item_sum_num
1153
Item_sum_udf_str(udf_func *udf_arg)
1155
Item_sum_udf_str(udf_func *udf_arg, List<Item> &list)
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; }
1167
bool add() { return 0; }
1168
void update_field() {}
1171
#endif /* HAVE_DLOPEN */
967
1175
class Item_func_group_concat : public Item_sum
969
Tmp_Table_Param *tmp_table_param;
970
DRIZZLE_ERROR *warning;
1177
TMP_TABLE_PARAM *tmp_table_param;
1178
MYSQL_ERROR *warning;
972
1180
String *separator;
1022
1231
enum_field_types field_type() const
1024
1233
if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB )
1025
return DRIZZLE_TYPE_BLOB;
1234
return MYSQL_TYPE_BLOB;
1027
return DRIZZLE_TYPE_VARCHAR;
1236
return MYSQL_TYPE_VARCHAR;
1031
void reset_field() { assert(0); } // not used
1032
void update_field() { assert(0); } // not used
1033
bool fix_fields(Session *,Item **);
1034
bool setup(Session *session);
1240
void reset_field() { DBUG_ASSERT(0); } // not used
1241
void update_field() { DBUG_ASSERT(0); } // not used
1242
bool fix_fields(THD *,Item **);
1243
bool setup(THD *thd);
1035
1244
void make_unique();
1247
String *res; res=val_str(&str_value);
1248
return res ? my_atof(res->c_ptr()) : 0.0;
1255
if (!(res= val_str(&str_value)))
1256
return (longlong) 0;
1257
end_ptr= (char*) res->ptr()+ res->length();
1258
return my_strtoll10(res->ptr(), &end_ptr, &error);
1038
1260
my_decimal *val_decimal(my_decimal *decimal_value)
1040
1262
return val_decimal_from_string(decimal_value);
1042
1264
String* val_str(String* str);
1043
Item *copy_or_same(Session* session);
1265
Item *copy_or_same(THD* thd);
1044
1266
void no_rows_in_result() {}
1045
1267
virtual void print(String *str, enum_query_type query_type);
1046
virtual bool change_context_processor(unsigned char *cntx)
1047
{ context= (Name_resolution_context *)cntx; return false; }
1268
virtual bool change_context_processor(uchar *cntx)
1269
{ context= (Name_resolution_context *)cntx; return FALSE; }
1050
} /* namespace drizzled */
1052
#endif /* DRIZZLED_ITEM_SUM_H */