2000-07-31 21:29:14 +02:00
|
|
|
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
|
|
|
|
|
|
/* classes for sum functions */
|
|
|
|
|
2005-05-04 15:05:56 +02:00
|
|
|
#ifdef USE_PRAGMA_INTERFACE
|
2000-07-31 21:29:14 +02:00
|
|
|
#pragma interface /* gcc class implementation */
|
|
|
|
#endif
|
|
|
|
|
2001-05-11 23:07:34 +02:00
|
|
|
#include <my_tree.h>
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
class Item_sum :public Item_result_field
|
|
|
|
{
|
|
|
|
public:
|
2003-06-04 17:28:51 +02:00
|
|
|
enum Sumfunctype
|
2005-09-08 03:07:51 +02:00
|
|
|
{ COUNT_FUNC, COUNT_DISTINCT_FUNC, SUM_FUNC, SUM_DISTINCT_FUNC, AVG_FUNC,
|
|
|
|
AVG_DISTINCT_FUNC, MIN_FUNC, MAX_FUNC, UNIQUE_USERS_FUNC, STD_FUNC,
|
|
|
|
VARIANCE_FUNC, SUM_BIT_FUNC, UDF_SUM_FUNC, GROUP_CONCAT_FUNC
|
2003-06-04 17:28:51 +02:00
|
|
|
};
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2004-04-03 10:13:51 +02:00
|
|
|
Item **args, *tmp_args[2];
|
2000-07-31 21:29:14 +02:00
|
|
|
uint arg_count;
|
|
|
|
bool quick_group; /* If incremental update of fields */
|
|
|
|
|
2002-12-02 16:52:22 +01:00
|
|
|
void mark_as_sum_func();
|
2004-10-09 00:01:19 +02:00
|
|
|
Item_sum() :arg_count(0), quick_group(1)
|
2002-10-31 01:11:59 +01:00
|
|
|
{
|
|
|
|
mark_as_sum_func();
|
|
|
|
}
|
2004-04-03 10:13:51 +02:00
|
|
|
Item_sum(Item *a)
|
2004-10-09 00:01:19 +02:00
|
|
|
:args(tmp_args), arg_count(1), quick_group(1)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
args[0]=a;
|
2002-10-31 01:11:59 +01:00
|
|
|
mark_as_sum_func();
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2004-04-03 10:13:51 +02:00
|
|
|
Item_sum( Item *a, Item *b )
|
2004-10-09 00:01:19 +02:00
|
|
|
:args(tmp_args), arg_count(2), quick_group(1)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
args[0]=a; args[1]=b;
|
2002-10-31 01:11:59 +01:00
|
|
|
mark_as_sum_func();
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
Item_sum(List<Item> &list);
|
2003-01-25 01:25:52 +01:00
|
|
|
//Copy constructor, need to perform subselects with temporary tables
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum(THD *thd, Item_sum *item);
|
2000-07-31 21:29:14 +02:00
|
|
|
enum Type type() const { return SUM_FUNC_ITEM; }
|
|
|
|
virtual enum Sumfunctype sum_func () const=0;
|
2003-08-28 02:10:14 +02:00
|
|
|
inline bool reset() { clear(); return add(); };
|
|
|
|
virtual void clear()= 0;
|
2000-07-31 21:29:14 +02:00
|
|
|
virtual bool add()=0;
|
2003-11-20 22:03:04 +01:00
|
|
|
/*
|
|
|
|
Called when new group is started and results are being saved in
|
|
|
|
a temporary table. Similar to reset(), but must also store value in
|
|
|
|
result_field. Like reset() it is supposed to reset start value to
|
|
|
|
default.
|
2005-09-08 03:07:51 +02:00
|
|
|
This set of methods (reult_field(), reset_field, update_field()) of
|
|
|
|
Item_sum is used only if quick_group is not null. Otherwise
|
|
|
|
copy_or_same() is used to obtain a copy of this item.
|
2003-11-20 22:03:04 +01:00
|
|
|
*/
|
2000-07-31 21:29:14 +02:00
|
|
|
virtual void reset_field()=0;
|
2003-11-20 22:03:04 +01:00
|
|
|
/*
|
|
|
|
Called for each new value in the group, when temporary table is in use.
|
|
|
|
Similar to add(), but uses temporary table field to obtain current value,
|
|
|
|
Updated value is then saved in the field.
|
|
|
|
*/
|
2003-08-20 15:25:44 +02:00
|
|
|
virtual void update_field()=0;
|
2000-07-31 21:29:14 +02:00
|
|
|
virtual bool keep_field_type(void) const { return 0; }
|
|
|
|
virtual void fix_length_and_dec() { maybe_null=1; null_value=1; }
|
2005-09-08 03:07:51 +02:00
|
|
|
/*
|
|
|
|
This method is used for debug purposes to print the name of an
|
|
|
|
item to the debug log. The second use of this method is as
|
|
|
|
a helper function of print(), where it is applicable.
|
|
|
|
To suit both goals it should return a meaningful,
|
|
|
|
distinguishable and sintactically correct string. This method
|
|
|
|
should not be used for runtime type identification, use enum
|
|
|
|
{Sum}Functype and Item_func::functype()/Item_sum::sum_func()
|
|
|
|
instead.
|
|
|
|
|
|
|
|
NOTE: for Items inherited from Item_sum, func_name() return part of
|
|
|
|
function name till first argument (including '(') to make difference in
|
|
|
|
names for functions with 'distinct' clause and without 'distinct' and
|
|
|
|
also to make printing of items inherited from Item_sum uniform.
|
|
|
|
*/
|
|
|
|
virtual const char *func_name() const= 0;
|
2000-07-31 21:29:14 +02:00
|
|
|
virtual Item *result_item(Field *field)
|
2005-09-08 03:07:51 +02:00
|
|
|
{ return new Item_field(field); }
|
2000-07-31 21:29:14 +02:00
|
|
|
table_map used_tables() const { return ~(table_map) 0; } /* Not used */
|
|
|
|
bool const_item() const { return 0; }
|
2002-03-02 08:51:24 +01:00
|
|
|
bool is_null() { return null_value; }
|
2000-07-31 21:29:14 +02:00
|
|
|
void update_used_tables() { }
|
|
|
|
void make_field(Send_field *field);
|
|
|
|
void print(String *str);
|
|
|
|
void fix_num_length_and_dec();
|
2003-02-03 19:20:32 +01:00
|
|
|
void no_rows_in_result() { reset(); }
|
2000-07-31 21:29:14 +02:00
|
|
|
virtual bool setup(THD *thd) {return 0;}
|
2003-06-04 17:28:51 +02:00
|
|
|
virtual void make_unique() {}
|
2003-01-30 17:07:39 +01:00
|
|
|
Item *get_tmp_table_item(THD *thd);
|
2005-09-08 03:07:51 +02:00
|
|
|
virtual Field *create_tmp_field(bool group, TABLE *table,
|
|
|
|
uint convert_blob_length);
|
2003-08-23 12:29:38 +02:00
|
|
|
bool walk (Item_processor processor, byte *argument);
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Item_sum_num :public Item_sum
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Item_sum_num() :Item_sum() {}
|
|
|
|
Item_sum_num(Item *item_par) :Item_sum(item_par) {}
|
|
|
|
Item_sum_num(Item *a, Item* b) :Item_sum(a,b) {}
|
|
|
|
Item_sum_num(List<Item> &list) :Item_sum(list) {}
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum_num(THD *thd, Item_sum_num *item) :Item_sum(thd, item) {}
|
2005-09-08 03:07:51 +02:00
|
|
|
bool fix_fields(THD *, Item **);
|
2004-03-18 14:14:36 +01:00
|
|
|
longlong val_int()
|
2005-09-08 03:07:51 +02:00
|
|
|
{
|
|
|
|
DBUG_ASSERT(fixed == 1);
|
|
|
|
return (longlong) val_real(); /* Real as default */
|
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
String *val_str(String*str);
|
2005-09-08 03:07:51 +02:00
|
|
|
my_decimal *val_decimal(my_decimal *);
|
2000-07-31 21:29:14 +02:00
|
|
|
void reset_field();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Item_sum_int :public Item_sum_num
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Item_sum_int(Item *item_par) :Item_sum_num(item_par) {}
|
|
|
|
Item_sum_int(List<Item> &list) :Item_sum_num(list) {}
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum_int(THD *thd, Item_sum_int *item) :Item_sum_num(thd, item) {}
|
2005-09-08 03:07:51 +02:00
|
|
|
double val_real() { DBUG_ASSERT(fixed == 1); return (double) val_int(); }
|
2000-07-31 21:29:14 +02:00
|
|
|
String *val_str(String*str);
|
2005-09-08 03:07:51 +02:00
|
|
|
my_decimal *val_decimal(my_decimal *);
|
2000-07-31 21:29:14 +02:00
|
|
|
enum Item_result result_type () const { return INT_RESULT; }
|
2003-04-15 21:04:16 +02:00
|
|
|
void fix_length_and_dec()
|
|
|
|
{ decimals=0; max_length=21; maybe_null=null_value=0; }
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Item_sum_sum :public Item_sum_num
|
|
|
|
{
|
2005-09-08 03:07:51 +02:00
|
|
|
protected:
|
|
|
|
Item_result hybrid_type;
|
2000-07-31 21:29:14 +02:00
|
|
|
double sum;
|
2005-09-08 03:07:51 +02:00
|
|
|
my_decimal dec_buffs[2];
|
|
|
|
uint curr_dec_buff;
|
|
|
|
void fix_length_and_dec();
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2005-09-08 03:07:51 +02:00
|
|
|
public:
|
|
|
|
Item_sum_sum(Item *item_par) :Item_sum_num(item_par) {}
|
|
|
|
Item_sum_sum(THD *thd, Item_sum_sum *item);
|
2000-07-31 21:29:14 +02:00
|
|
|
enum Sumfunctype sum_func () const {return SUM_FUNC;}
|
2003-08-28 02:10:14 +02:00
|
|
|
void clear();
|
2000-07-31 21:29:14 +02:00
|
|
|
bool add();
|
2005-09-08 03:07:51 +02:00
|
|
|
double val_real();
|
|
|
|
longlong val_int();
|
|
|
|
String *val_str(String*str);
|
|
|
|
my_decimal *val_decimal(my_decimal *);
|
|
|
|
enum Item_result result_type () const { return hybrid_type; }
|
2000-07-31 21:29:14 +02:00
|
|
|
void reset_field();
|
2003-08-20 15:25:44 +02:00
|
|
|
void update_field();
|
2003-04-15 21:04:16 +02:00
|
|
|
void no_rows_in_result() {}
|
2005-09-08 03:07:51 +02:00
|
|
|
const char *func_name() const { return "sum("; }
|
2003-06-04 17:28:51 +02:00
|
|
|
Item *copy_or_same(THD* thd);
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-09-08 03:07:51 +02:00
|
|
|
|
|
|
|
/* Common class for SUM(DISTINCT), AVG(DISTINCT) */
|
|
|
|
|
|
|
|
class Unique;
|
|
|
|
|
|
|
|
class Item_sum_distinct :public Item_sum_num
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
/* storage for the summation result */
|
|
|
|
ulonglong count;
|
|
|
|
Hybrid_type val;
|
|
|
|
/* storage for unique elements */
|
|
|
|
Unique *tree;
|
|
|
|
TABLE *table;
|
|
|
|
enum enum_field_types table_field_type;
|
|
|
|
uint tree_key_length;
|
|
|
|
protected:
|
|
|
|
Item_sum_distinct(THD *thd, Item_sum_distinct *item);
|
|
|
|
public:
|
|
|
|
Item_sum_distinct(Item *item_par);
|
|
|
|
~Item_sum_distinct();
|
|
|
|
|
|
|
|
bool setup(THD *thd);
|
|
|
|
void clear();
|
|
|
|
void cleanup();
|
|
|
|
bool add();
|
|
|
|
double val_real();
|
|
|
|
my_decimal *val_decimal(my_decimal *);
|
|
|
|
longlong val_int();
|
|
|
|
String *val_str(String *str);
|
|
|
|
|
|
|
|
/* XXX: does it need make_unique? */
|
|
|
|
|
|
|
|
enum Sumfunctype sum_func () const { return SUM_DISTINCT_FUNC; }
|
|
|
|
void reset_field() {} // not used
|
|
|
|
void update_field() {} // not used
|
|
|
|
virtual void no_rows_in_result() {}
|
|
|
|
void fix_length_and_dec();
|
|
|
|
enum Item_result result_type () const { return val.traits->type(); }
|
|
|
|
virtual void calculate_val_and_count();
|
|
|
|
virtual bool unique_walk_function(void *elem);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Item_sum_sum_distinct - implementation of SUM(DISTINCT expr).
|
|
|
|
See also: MySQL manual, chapter 'Adding New Functions To MySQL'
|
|
|
|
and comments in item_sum.cc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Item_sum_sum_distinct :public Item_sum_distinct
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
Item_sum_sum_distinct(THD *thd, Item_sum_sum_distinct *item)
|
|
|
|
:Item_sum_distinct(thd, item) {}
|
|
|
|
public:
|
|
|
|
Item_sum_sum_distinct(Item *item_arg) :Item_sum_distinct(item_arg) {}
|
|
|
|
|
|
|
|
enum Sumfunctype sum_func () const { return SUM_DISTINCT_FUNC; }
|
|
|
|
const char *func_name() const { return "sum(distinct "; }
|
|
|
|
Item *copy_or_same(THD* thd) { return new Item_sum_sum_distinct(thd, this); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Item_sum_avg_distinct - SELECT AVG(DISTINCT expr) FROM ... */
|
|
|
|
|
|
|
|
class Item_sum_avg_distinct: public Item_sum_distinct
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
Item_sum_avg_distinct(THD *thd, Item_sum_avg_distinct *original)
|
|
|
|
:Item_sum_distinct(thd, original) {}
|
|
|
|
public:
|
|
|
|
uint prec_increment;
|
|
|
|
Item_sum_avg_distinct(Item *item_arg) : Item_sum_distinct(item_arg) {}
|
|
|
|
|
|
|
|
void fix_length_and_dec();
|
|
|
|
virtual void calculate_val_and_count();
|
|
|
|
enum Sumfunctype sum_func () const { return AVG_DISTINCT_FUNC; }
|
|
|
|
const char *func_name() const { return "avg(distinct "; }
|
|
|
|
Item *copy_or_same(THD* thd) { return new Item_sum_avg_distinct(thd, this); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
class Item_sum_count :public Item_sum_int
|
|
|
|
{
|
|
|
|
longlong count;
|
|
|
|
table_map used_table_cache;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Item_sum_count(Item *item_par)
|
|
|
|
:Item_sum_int(item_par),count(0),used_table_cache(~(table_map) 0)
|
|
|
|
{}
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum_count(THD *thd, Item_sum_count *item)
|
|
|
|
:Item_sum_int(thd, item), count(item->count),
|
|
|
|
used_table_cache(item->used_table_cache)
|
2003-01-25 01:25:52 +01:00
|
|
|
{}
|
2000-07-31 21:29:14 +02:00
|
|
|
table_map used_tables() const { return used_table_cache; }
|
|
|
|
bool const_item() const { return !used_table_cache; }
|
|
|
|
enum Sumfunctype sum_func () const { return COUNT_FUNC; }
|
2003-08-28 02:10:14 +02:00
|
|
|
void clear();
|
2003-02-03 19:20:32 +01:00
|
|
|
void no_rows_in_result() { count=0; }
|
2000-07-31 21:29:14 +02:00
|
|
|
bool add();
|
|
|
|
void make_const(longlong count_arg) { count=count_arg; used_table_cache=0; }
|
|
|
|
longlong val_int();
|
|
|
|
void reset_field();
|
2004-06-10 09:59:55 +02:00
|
|
|
void cleanup();
|
2003-08-20 15:25:44 +02:00
|
|
|
void update_field();
|
2005-09-08 03:07:51 +02:00
|
|
|
const char *func_name() const { return "count("; }
|
2003-06-04 17:28:51 +02:00
|
|
|
Item *copy_or_same(THD* thd);
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class TMP_TABLE_PARAM;
|
|
|
|
|
|
|
|
class Item_sum_count_distinct :public Item_sum_int
|
|
|
|
{
|
|
|
|
TABLE *table;
|
2001-08-02 05:29:50 +02:00
|
|
|
uint32 *field_lengths;
|
2000-07-31 21:29:14 +02:00
|
|
|
TMP_TABLE_PARAM *tmp_table_param;
|
2005-09-08 03:07:51 +02:00
|
|
|
/*
|
|
|
|
If there are no blobs, we can use a tree, which
|
|
|
|
is faster than heap table. In that case, we still use the table
|
|
|
|
to help get things set up, but we insert nothing in it
|
|
|
|
*/
|
|
|
|
Unique *tree;
|
2003-01-25 01:25:52 +01:00
|
|
|
/*
|
|
|
|
Following is 0 normal object and pointer to original one for copy
|
|
|
|
(to correctly free resources)
|
|
|
|
*/
|
|
|
|
Item_sum_count_distinct *original;
|
2005-09-08 03:07:51 +02:00
|
|
|
uint tree_key_length;
|
2003-01-25 01:25:52 +01:00
|
|
|
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-06-21 11:03:54 +02:00
|
|
|
bool always_null; // Set to 1 if the result is always NULL
|
2001-05-11 23:07:34 +02:00
|
|
|
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2001-05-25 01:06:53 +02:00
|
|
|
friend int composite_key_cmp(void* arg, byte* key1, byte* key2);
|
2002-12-19 13:42:35 +01:00
|
|
|
friend int simple_str_key_cmp(void* arg, byte* key1, byte* key2);
|
2001-05-25 01:06:53 +02:00
|
|
|
|
2005-09-08 03:07:51 +02:00
|
|
|
public:
|
2000-07-31 21:29:14 +02:00
|
|
|
Item_sum_count_distinct(List<Item> &list)
|
2005-09-08 03:07:51 +02:00
|
|
|
:Item_sum_int(list), table(0), field_lengths(0), tmp_table_param(0),
|
|
|
|
tree(0), original(0), always_null(FALSE)
|
2003-01-25 01:25:52 +01:00
|
|
|
{ quick_group= 0; }
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum_count_distinct(THD *thd, Item_sum_count_distinct *item)
|
|
|
|
:Item_sum_int(thd, item), table(item->table),
|
2004-04-03 10:13:51 +02:00
|
|
|
field_lengths(item->field_lengths),
|
|
|
|
tmp_table_param(item->tmp_table_param),
|
2005-09-08 03:07:51 +02:00
|
|
|
tree(item->tree), original(item), tree_key_length(item->tree_key_length),
|
2004-01-19 16:53:25 +01:00
|
|
|
always_null(item->always_null)
|
2003-01-30 17:07:39 +01:00
|
|
|
{}
|
2005-09-08 03:07:51 +02:00
|
|
|
~Item_sum_count_distinct();
|
|
|
|
|
2003-12-30 11:08:19 +01:00
|
|
|
void cleanup();
|
2003-02-10 12:01:47 +01:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
enum Sumfunctype sum_func () const { return COUNT_DISTINCT_FUNC; }
|
2003-08-28 02:10:14 +02:00
|
|
|
void clear();
|
2000-07-31 21:29:14 +02:00
|
|
|
bool add();
|
|
|
|
longlong val_int();
|
|
|
|
void reset_field() { return ;} // Never called
|
2003-08-20 15:25:44 +02:00
|
|
|
void update_field() { return ; } // Never called
|
2005-09-08 03:07:51 +02:00
|
|
|
const char *func_name() const { return "count(distinct "; }
|
2000-07-31 21:29:14 +02:00
|
|
|
bool setup(THD *thd);
|
2003-06-04 17:28:51 +02:00
|
|
|
void make_unique();
|
|
|
|
Item *copy_or_same(THD* thd);
|
2003-02-10 16:03:27 +01:00
|
|
|
void no_rows_in_result() {}
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Item to get the value of a stored sum function */
|
|
|
|
|
2001-01-27 00:20:56 +01:00
|
|
|
class Item_sum_avg;
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
class Item_avg_field :public Item_result_field
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Field *field;
|
2005-09-08 03:07:51 +02:00
|
|
|
Item_result hybrid_type;
|
|
|
|
uint f_precision, f_scale, dec_bin_size;
|
|
|
|
uint prec_increment;
|
|
|
|
Item_avg_field(Item_result res_type, Item_sum_avg *item);
|
2000-07-31 21:29:14 +02:00
|
|
|
enum Type type() const { return FIELD_AVG_ITEM; }
|
2005-09-08 03:07:51 +02:00
|
|
|
double val_real();
|
|
|
|
longlong val_int();
|
|
|
|
my_decimal *val_decimal(my_decimal *);
|
2002-03-02 08:51:24 +01:00
|
|
|
bool is_null() { (void) val_int(); return null_value; }
|
2000-07-31 21:29:14 +02:00
|
|
|
String *val_str(String*);
|
2005-09-08 03:07:51 +02:00
|
|
|
enum_field_types field_type() const
|
|
|
|
{
|
|
|
|
return hybrid_type == DECIMAL_RESULT ?
|
|
|
|
MYSQL_TYPE_NEWDECIMAL : MYSQL_TYPE_DOUBLE;
|
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
void fix_length_and_dec() {}
|
2005-09-08 03:07:51 +02:00
|
|
|
enum Item_result result_type () const { return hybrid_type; }
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-09-08 03:07:51 +02:00
|
|
|
class Item_sum_avg :public Item_sum_sum
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-09-08 03:07:51 +02:00
|
|
|
public:
|
2000-07-31 21:29:14 +02:00
|
|
|
ulonglong count;
|
2005-09-08 03:07:51 +02:00
|
|
|
uint prec_increment;
|
|
|
|
uint f_precision, f_scale, dec_bin_size;
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2005-09-08 03:07:51 +02:00
|
|
|
Item_sum_avg(Item *item_par) :Item_sum_sum(item_par), count(0) {}
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum_avg(THD *thd, Item_sum_avg *item)
|
2005-09-08 03:07:51 +02:00
|
|
|
:Item_sum_sum(thd, item), count(item->count),
|
|
|
|
prec_increment(item->prec_increment) {}
|
|
|
|
|
|
|
|
void fix_length_and_dec();
|
2000-07-31 21:29:14 +02:00
|
|
|
enum Sumfunctype sum_func () const {return AVG_FUNC;}
|
2003-08-28 02:10:14 +02:00
|
|
|
void clear();
|
2000-07-31 21:29:14 +02:00
|
|
|
bool add();
|
2005-09-08 03:07:51 +02:00
|
|
|
double val_real();
|
|
|
|
// In SPs we might force the "wrong" type with select into a declare variable
|
|
|
|
longlong val_int() { return (longlong)val_real(); }
|
|
|
|
my_decimal *val_decimal(my_decimal *);
|
|
|
|
String *val_str(String *str);
|
2000-07-31 21:29:14 +02:00
|
|
|
void reset_field();
|
2003-08-20 15:25:44 +02:00
|
|
|
void update_field();
|
2000-07-31 21:29:14 +02:00
|
|
|
Item *result_item(Field *field)
|
2005-09-08 03:07:51 +02:00
|
|
|
{ return new Item_avg_field(hybrid_type, this); }
|
2004-04-09 16:07:39 +02:00
|
|
|
void no_rows_in_result() {}
|
2005-09-08 03:07:51 +02:00
|
|
|
const char *func_name() const { return "avg("; }
|
2003-06-04 17:28:51 +02:00
|
|
|
Item *copy_or_same(THD* thd);
|
2005-09-08 03:07:51 +02:00
|
|
|
Field *create_tmp_field(bool group, TABLE *table, uint convert_blob_length);
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
2002-12-14 00:36:59 +01:00
|
|
|
class Item_sum_variance;
|
2000-07-31 21:29:14 +02:00
|
|
|
|
2002-12-14 00:36:59 +01:00
|
|
|
class Item_variance_field :public Item_result_field
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Field *field;
|
2005-09-08 03:07:51 +02:00
|
|
|
Item_result hybrid_type;
|
|
|
|
uint f_precision0, f_scale0;
|
|
|
|
uint f_precision1, f_scale1;
|
|
|
|
uint dec_bin_size0, dec_bin_size1;
|
|
|
|
uint sample;
|
|
|
|
uint prec_increment;
|
2002-12-14 00:36:59 +01:00
|
|
|
Item_variance_field(Item_sum_variance *item);
|
|
|
|
enum Type type() const {return FIELD_VARIANCE_ITEM; }
|
2005-09-08 03:07:51 +02:00
|
|
|
double val_real();
|
|
|
|
longlong val_int()
|
|
|
|
{ /* can't be fix_fields()ed */ return (longlong) val_real(); }
|
2000-07-31 21:29:14 +02:00
|
|
|
String *val_str(String*);
|
2005-09-08 03:07:51 +02:00
|
|
|
my_decimal *val_decimal(my_decimal *);
|
2002-03-02 08:51:24 +01:00
|
|
|
bool is_null() { (void) val_int(); return null_value; }
|
2005-09-08 03:07:51 +02:00
|
|
|
enum_field_types field_type() const
|
|
|
|
{
|
|
|
|
return hybrid_type == DECIMAL_RESULT ?
|
|
|
|
MYSQL_TYPE_NEWDECIMAL : MYSQL_TYPE_DOUBLE;
|
|
|
|
}
|
2000-07-31 21:29:14 +02:00
|
|
|
void fix_length_and_dec() {}
|
2005-09-08 03:07:51 +02:00
|
|
|
enum Item_result result_type () const { return hybrid_type; }
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
2002-12-14 00:36:59 +01:00
|
|
|
|
2004-04-05 12:56:05 +02:00
|
|
|
/*
|
|
|
|
variance(a) =
|
|
|
|
|
|
|
|
= sum (ai - avg(a))^2 / count(a) )
|
|
|
|
= sum (ai^2 - 2*ai*avg(a) + avg(a)^2) / count(a)
|
|
|
|
= (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) =
|
|
|
|
= (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) =
|
|
|
|
= (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2 )/count(a) =
|
|
|
|
= (sum(ai^2) - 2*sum(a)^2/count(a) + sum(a)^2/count(a) )/count(a) =
|
|
|
|
= (sum(ai^2) - sum(a)^2/count(a))/count(a)
|
2002-12-14 01:53:28 +01:00
|
|
|
*/
|
2002-12-14 00:36:59 +01:00
|
|
|
|
|
|
|
class Item_sum_variance : public Item_sum_num
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-09-08 03:07:51 +02:00
|
|
|
void fix_length_and_dec();
|
|
|
|
|
|
|
|
public:
|
|
|
|
Item_result hybrid_type;
|
2002-12-14 00:36:59 +01:00
|
|
|
double sum, sum_sqr;
|
2005-09-08 03:07:51 +02:00
|
|
|
my_decimal dec_sum[2], dec_sqr[2];
|
|
|
|
int cur_dec;
|
2000-07-31 21:29:14 +02:00
|
|
|
ulonglong count;
|
2005-09-08 03:07:51 +02:00
|
|
|
uint f_precision0, f_scale0;
|
|
|
|
uint f_precision1, f_scale1;
|
|
|
|
uint dec_bin_size0, dec_bin_size1;
|
|
|
|
uint sample;
|
|
|
|
uint prec_increment;
|
|
|
|
|
|
|
|
Item_sum_variance(Item *item_par, uint sample_arg) :Item_sum_num(item_par),
|
|
|
|
hybrid_type(REAL_RESULT), cur_dec(0), count(0), sample(sample_arg)
|
|
|
|
{}
|
|
|
|
Item_sum_variance(THD *thd, Item_sum_variance *item);
|
2002-12-14 00:36:59 +01:00
|
|
|
enum Sumfunctype sum_func () const { return VARIANCE_FUNC; }
|
2003-08-28 02:10:14 +02:00
|
|
|
void clear();
|
2000-07-31 21:29:14 +02:00
|
|
|
bool add();
|
2005-09-08 03:07:51 +02:00
|
|
|
double val_real();
|
|
|
|
my_decimal *val_decimal(my_decimal *);
|
2000-07-31 21:29:14 +02:00
|
|
|
void reset_field();
|
2003-08-20 15:25:44 +02:00
|
|
|
void update_field();
|
2000-07-31 21:29:14 +02:00
|
|
|
Item *result_item(Field *field)
|
2002-12-14 00:36:59 +01:00
|
|
|
{ return new Item_variance_field(this); }
|
2004-05-05 13:06:01 +02:00
|
|
|
void no_rows_in_result() {}
|
2005-09-08 03:07:51 +02:00
|
|
|
const char *func_name() const
|
|
|
|
{ return sample ? "var_samp(" : "variance("; }
|
2003-06-04 17:28:51 +02:00
|
|
|
Item *copy_or_same(THD* thd);
|
2005-09-08 03:07:51 +02:00
|
|
|
Field *create_tmp_field(bool group, TABLE *table, uint convert_blob_length);
|
|
|
|
enum Item_result result_type () const { return hybrid_type; }
|
2002-12-14 00:36:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class Item_sum_std;
|
|
|
|
|
|
|
|
class Item_std_field :public Item_variance_field
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Item_std_field(Item_sum_std *item);
|
|
|
|
enum Type type() const { return FIELD_STD_ITEM; }
|
2005-09-08 03:07:51 +02:00
|
|
|
double val_real();
|
|
|
|
my_decimal *val_decimal(my_decimal *);
|
|
|
|
enum Item_result result_type () const { return REAL_RESULT; }
|
|
|
|
enum_field_types field_type() const { return MYSQL_TYPE_DOUBLE;}
|
2003-01-26 20:30:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
standard_deviation(a) = sqrt(variance(a))
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Item_sum_std :public Item_sum_variance
|
|
|
|
{
|
|
|
|
public:
|
2005-09-08 03:07:51 +02:00
|
|
|
Item_sum_std(Item *item_par, uint sample_arg)
|
|
|
|
:Item_sum_variance(item_par, sample_arg) {}
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum_std(THD *thd, Item_sum_std *item)
|
2003-06-04 17:28:51 +02:00
|
|
|
:Item_sum_variance(thd, item)
|
|
|
|
{}
|
2003-01-26 20:30:35 +01:00
|
|
|
enum Sumfunctype sum_func () const { return STD_FUNC; }
|
2005-09-08 03:07:51 +02:00
|
|
|
double val_real();
|
2003-01-26 20:30:35 +01:00
|
|
|
Item *result_item(Field *field)
|
|
|
|
{ return new Item_std_field(this); }
|
2005-09-08 03:07:51 +02:00
|
|
|
const char *func_name() const { return "std("; }
|
2003-06-04 17:28:51 +02:00
|
|
|
Item *copy_or_same(THD* thd);
|
2005-09-08 03:07:51 +02:00
|
|
|
enum Item_result result_type () const { return REAL_RESULT; }
|
|
|
|
enum_field_types field_type() const { return MYSQL_TYPE_DOUBLE;}
|
2003-01-26 20:30:35 +01:00
|
|
|
};
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
// This class is a string or number function depending on num_func
|
|
|
|
|
|
|
|
class Item_sum_hybrid :public Item_sum
|
|
|
|
{
|
2005-09-08 03:07:51 +02:00
|
|
|
protected:
|
2000-07-31 21:29:14 +02:00
|
|
|
String value,tmp_value;
|
|
|
|
double sum;
|
2001-09-27 20:45:48 +02:00
|
|
|
longlong sum_int;
|
2005-09-08 03:07:51 +02:00
|
|
|
my_decimal sum_dec;
|
2000-07-31 21:29:14 +02:00
|
|
|
Item_result hybrid_type;
|
2002-12-11 08:17:51 +01:00
|
|
|
enum_field_types hybrid_field_type;
|
2000-07-31 21:29:14 +02:00
|
|
|
int cmp_sign;
|
|
|
|
table_map used_table_cache;
|
2004-12-07 20:18:15 +01:00
|
|
|
bool was_values; // Set if we have found at least one row (for max/min only)
|
2000-07-31 21:29:14 +02:00
|
|
|
|
|
|
|
public:
|
2003-02-12 20:55:37 +01:00
|
|
|
Item_sum_hybrid(Item *item_par,int sign)
|
2004-02-09 12:31:03 +01:00
|
|
|
:Item_sum(item_par), sum(0.0), sum_int(0),
|
|
|
|
hybrid_type(INT_RESULT), hybrid_field_type(FIELD_TYPE_LONGLONG),
|
2005-09-08 03:07:51 +02:00
|
|
|
cmp_sign(sign), used_table_cache(~(table_map) 0),
|
2004-11-18 17:10:07 +01:00
|
|
|
was_values(TRUE)
|
2005-09-08 03:07:51 +02:00
|
|
|
{ collation.set(&my_charset_bin); }
|
|
|
|
Item_sum_hybrid(THD *thd, Item_sum_hybrid *item);
|
|
|
|
bool fix_fields(THD *, Item **);
|
2000-07-31 21:29:14 +02:00
|
|
|
table_map used_tables() const { return used_table_cache; }
|
|
|
|
bool const_item() const { return !used_table_cache; }
|
|
|
|
|
2004-05-07 22:06:11 +02:00
|
|
|
void clear();
|
2005-09-08 03:07:51 +02:00
|
|
|
double val_real();
|
2001-09-27 20:45:48 +02:00
|
|
|
longlong val_int();
|
2005-09-08 03:07:51 +02:00
|
|
|
my_decimal *val_decimal(my_decimal *);
|
2000-07-31 21:29:14 +02:00
|
|
|
void reset_field();
|
|
|
|
String *val_str(String *);
|
|
|
|
void make_const() { used_table_cache=0; }
|
|
|
|
bool keep_field_type(void) const { return 1; }
|
|
|
|
enum Item_result result_type () const { return hybrid_type; }
|
2002-12-11 08:17:51 +01:00
|
|
|
enum enum_field_types field_type() const { return hybrid_field_type; }
|
2003-08-20 15:25:44 +02:00
|
|
|
void update_field();
|
|
|
|
void min_max_update_str_field();
|
|
|
|
void min_max_update_real_field();
|
|
|
|
void min_max_update_int_field();
|
2005-09-08 03:07:51 +02:00
|
|
|
void min_max_update_decimal_field();
|
2004-06-10 09:59:55 +02:00
|
|
|
void cleanup();
|
2004-11-18 17:10:07 +01:00
|
|
|
bool any_value() { return was_values; }
|
|
|
|
void no_rows_in_result();
|
2005-09-08 03:07:51 +02:00
|
|
|
Field *create_tmp_field(bool group, TABLE *table,
|
|
|
|
uint convert_blob_length);
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Item_sum_min :public Item_sum_hybrid
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Item_sum_min(Item *item_par) :Item_sum_hybrid(item_par,1) {}
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum_min(THD *thd, Item_sum_min *item) :Item_sum_hybrid(thd, item) {}
|
2000-07-31 21:29:14 +02:00
|
|
|
enum Sumfunctype sum_func () const {return MIN_FUNC;}
|
|
|
|
|
|
|
|
bool add();
|
2005-09-08 03:07:51 +02:00
|
|
|
const char *func_name() const { return "min("; }
|
2003-06-04 17:28:51 +02:00
|
|
|
Item *copy_or_same(THD* thd);
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Item_sum_max :public Item_sum_hybrid
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Item_sum_max(Item *item_par) :Item_sum_hybrid(item_par,-1) {}
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum_max(THD *thd, Item_sum_max *item) :Item_sum_hybrid(thd, item) {}
|
2000-07-31 21:29:14 +02:00
|
|
|
enum Sumfunctype sum_func () const {return MAX_FUNC;}
|
|
|
|
|
|
|
|
bool add();
|
2005-09-08 03:07:51 +02:00
|
|
|
const char *func_name() const { return "max("; }
|
2003-06-04 17:28:51 +02:00
|
|
|
Item *copy_or_same(THD* thd);
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Item_sum_bit :public Item_sum_int
|
|
|
|
{
|
2003-12-02 17:39:51 +01:00
|
|
|
protected:
|
2000-07-31 21:29:14 +02:00
|
|
|
ulonglong reset_bits,bits;
|
|
|
|
|
2003-12-02 17:39:51 +01:00
|
|
|
public:
|
2000-07-31 21:29:14 +02:00
|
|
|
Item_sum_bit(Item *item_par,ulonglong reset_arg)
|
|
|
|
:Item_sum_int(item_par),reset_bits(reset_arg),bits(reset_arg) {}
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum_bit(THD *thd, Item_sum_bit *item):
|
|
|
|
Item_sum_int(thd, item), reset_bits(item->reset_bits), bits(item->bits) {}
|
2000-07-31 21:29:14 +02:00
|
|
|
enum Sumfunctype sum_func () const {return SUM_BIT_FUNC;}
|
2003-08-28 02:10:14 +02:00
|
|
|
void clear();
|
2000-07-31 21:29:14 +02:00
|
|
|
longlong val_int();
|
|
|
|
void reset_field();
|
2003-12-19 15:25:50 +01:00
|
|
|
void update_field();
|
2003-04-15 21:04:16 +02:00
|
|
|
void fix_length_and_dec()
|
2005-09-08 03:07:51 +02:00
|
|
|
{ decimals= 0; max_length=21; unsigned_flag= 1; maybe_null= null_value= 0; }
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Item_sum_or :public Item_sum_bit
|
|
|
|
{
|
2003-11-20 22:03:04 +01:00
|
|
|
public:
|
2000-07-31 21:29:14 +02:00
|
|
|
Item_sum_or(Item *item_par) :Item_sum_bit(item_par,LL(0)) {}
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum_or(THD *thd, Item_sum_or *item) :Item_sum_bit(thd, item) {}
|
2000-07-31 21:29:14 +02:00
|
|
|
bool add();
|
2005-09-08 03:07:51 +02:00
|
|
|
const char *func_name() const { return "bit_or("; }
|
2003-06-04 17:28:51 +02:00
|
|
|
Item *copy_or_same(THD* thd);
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Item_sum_and :public Item_sum_bit
|
|
|
|
{
|
|
|
|
public:
|
2003-12-02 17:39:51 +01:00
|
|
|
Item_sum_and(Item *item_par) :Item_sum_bit(item_par, ULONGLONG_MAX) {}
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum_and(THD *thd, Item_sum_and *item) :Item_sum_bit(thd, item) {}
|
2000-07-31 21:29:14 +02:00
|
|
|
bool add();
|
2005-09-08 03:07:51 +02:00
|
|
|
const char *func_name() const { return "bit_and("; }
|
2003-06-04 17:28:51 +02:00
|
|
|
Item *copy_or_same(THD* thd);
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
2003-10-15 08:11:03 +02:00
|
|
|
class Item_sum_xor :public Item_sum_bit
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Item_sum_xor(Item *item_par) :Item_sum_bit(item_par,LL(0)) {}
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum_xor(THD *thd, Item_sum_xor *item) :Item_sum_bit(thd, item) {}
|
2003-10-15 08:11:03 +02:00
|
|
|
bool add();
|
2005-09-08 03:07:51 +02:00
|
|
|
const char *func_name() const { return "bit_xor("; }
|
2003-10-15 08:11:03 +02:00
|
|
|
Item *copy_or_same(THD* thd);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
/*
|
2004-04-05 12:56:05 +02:00
|
|
|
User defined aggregates
|
2000-07-31 21:29:14 +02:00
|
|
|
*/
|
2004-04-05 12:56:05 +02:00
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
#ifdef HAVE_DLOPEN
|
|
|
|
|
|
|
|
class Item_udf_sum : public Item_sum
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
udf_handler udf;
|
|
|
|
|
|
|
|
public:
|
2005-09-08 03:07:51 +02:00
|
|
|
Item_udf_sum(udf_func *udf_arg)
|
|
|
|
:Item_sum(), udf(udf_arg)
|
|
|
|
{ quick_group=0; }
|
|
|
|
Item_udf_sum(udf_func *udf_arg, List<Item> &list)
|
|
|
|
:Item_sum(list), udf(udf_arg)
|
2000-07-31 21:29:14 +02:00
|
|
|
{ quick_group=0;}
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_udf_sum(THD *thd, Item_udf_sum *item)
|
2005-09-08 03:07:51 +02:00
|
|
|
:Item_sum(thd, item), udf(item->udf)
|
|
|
|
{ udf.not_original= TRUE; }
|
2000-07-31 21:29:14 +02:00
|
|
|
const char *func_name() const { return udf.name(); }
|
2005-09-08 03:07:51 +02:00
|
|
|
bool fix_fields(THD *thd, Item **ref)
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2004-03-17 13:26:26 +01:00
|
|
|
DBUG_ASSERT(fixed == 0);
|
2002-11-21 10:01:33 +01:00
|
|
|
fixed= 1;
|
2005-09-08 03:07:51 +02:00
|
|
|
return udf.fix_fields(thd, this, this->arg_count, this->args);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
|
|
|
|
virtual bool have_field_update(void) const { return 0; }
|
|
|
|
|
2003-08-28 02:10:14 +02:00
|
|
|
void clear();
|
2000-07-31 21:29:14 +02:00
|
|
|
bool add();
|
|
|
|
void reset_field() {};
|
2003-08-20 15:25:44 +02:00
|
|
|
void update_field() {};
|
2005-04-30 18:23:40 +02:00
|
|
|
void cleanup();
|
2005-09-08 03:07:51 +02:00
|
|
|
void print(String *str);
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Item_sum_udf_float :public Item_udf_sum
|
|
|
|
{
|
|
|
|
public:
|
2005-09-08 03:07:51 +02:00
|
|
|
Item_sum_udf_float(udf_func *udf_arg)
|
|
|
|
:Item_udf_sum(udf_arg) {}
|
2000-07-31 21:29:14 +02:00
|
|
|
Item_sum_udf_float(udf_func *udf_arg, List<Item> &list)
|
2005-09-08 03:07:51 +02:00
|
|
|
:Item_udf_sum(udf_arg, list) {}
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
|
2003-01-30 17:07:39 +01:00
|
|
|
:Item_udf_sum(thd, item) {}
|
2004-03-18 14:14:36 +01:00
|
|
|
longlong val_int()
|
2005-09-08 03:07:51 +02:00
|
|
|
{
|
|
|
|
DBUG_ASSERT(fixed == 1);
|
|
|
|
return (longlong) Item_sum_udf_float::val_real();
|
|
|
|
}
|
|
|
|
double val_real();
|
2000-07-31 21:29:14 +02:00
|
|
|
String *val_str(String*str);
|
2005-09-08 03:07:51 +02:00
|
|
|
my_decimal *val_decimal(my_decimal *);
|
2000-07-31 21:29:14 +02:00
|
|
|
void fix_length_and_dec() { fix_num_length_and_dec(); }
|
2003-06-04 17:28:51 +02:00
|
|
|
Item *copy_or_same(THD* thd);
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Item_sum_udf_int :public Item_udf_sum
|
|
|
|
{
|
|
|
|
public:
|
2005-09-08 03:07:51 +02:00
|
|
|
Item_sum_udf_int(udf_func *udf_arg)
|
|
|
|
:Item_udf_sum(udf_arg) {}
|
2000-07-31 21:29:14 +02:00
|
|
|
Item_sum_udf_int(udf_func *udf_arg, List<Item> &list)
|
2005-09-08 03:07:51 +02:00
|
|
|
:Item_udf_sum(udf_arg, list) {}
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
|
2003-01-30 17:07:39 +01:00
|
|
|
:Item_udf_sum(thd, item) {}
|
2000-07-31 21:29:14 +02:00
|
|
|
longlong val_int();
|
2005-09-08 03:07:51 +02:00
|
|
|
double val_real()
|
2004-03-18 14:14:36 +01:00
|
|
|
{ DBUG_ASSERT(fixed == 1); return (double) Item_sum_udf_int::val_int(); }
|
2000-07-31 21:29:14 +02:00
|
|
|
String *val_str(String*str);
|
2005-09-08 03:07:51 +02:00
|
|
|
my_decimal *val_decimal(my_decimal *);
|
2000-07-31 21:29:14 +02:00
|
|
|
enum Item_result result_type () const { return INT_RESULT; }
|
|
|
|
void fix_length_and_dec() { decimals=0; max_length=21; }
|
2003-06-04 17:28:51 +02:00
|
|
|
Item *copy_or_same(THD* thd);
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Item_sum_udf_str :public Item_udf_sum
|
|
|
|
{
|
|
|
|
public:
|
2005-09-08 03:07:51 +02:00
|
|
|
Item_sum_udf_str(udf_func *udf_arg)
|
|
|
|
:Item_udf_sum(udf_arg) {}
|
2000-07-31 21:29:14 +02:00
|
|
|
Item_sum_udf_str(udf_func *udf_arg, List<Item> &list)
|
|
|
|
:Item_udf_sum(udf_arg,list) {}
|
2004-01-19 16:53:25 +01:00
|
|
|
Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
|
2003-01-30 17:07:39 +01:00
|
|
|
:Item_udf_sum(thd, item) {}
|
2000-07-31 21:29:14 +02:00
|
|
|
String *val_str(String *);
|
2005-09-08 03:07:51 +02:00
|
|
|
double val_real()
|
2000-07-31 21:29:14 +02:00
|
|
|
{
|
2005-09-08 03:07:51 +02:00
|
|
|
int err_not_used;
|
2005-02-22 11:51:23 +01:00
|
|
|
char *end_not_used;
|
|
|
|
String *res;
|
|
|
|
res=val_str(&str_value);
|
2003-01-14 13:28:36 +01:00
|
|
|
return res ? my_strntod(res->charset(),(char*) res->ptr(),res->length(),
|
2005-09-08 03:07:51 +02:00
|
|
|
&end_not_used, &err_not_used) : 0.0;
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
|
|
|
longlong val_int()
|
|
|
|
{
|
2005-09-08 03:07:51 +02:00
|
|
|
int err_not_used;
|
|
|
|
char *end;
|
|
|
|
String *res;
|
|
|
|
CHARSET_INFO *cs;
|
|
|
|
|
|
|
|
if (!(res= val_str(&str_value)))
|
|
|
|
return 0; /* Null value */
|
|
|
|
cs= res->charset();
|
|
|
|
end= (char*) res->ptr()+res->length();
|
|
|
|
return cs->cset->strtoll10(cs, res->ptr(), &end, &err_not_used);
|
2000-07-31 21:29:14 +02:00
|
|
|
}
|
2005-09-08 03:07:51 +02:00
|
|
|
my_decimal *val_decimal(my_decimal *dec);
|
2000-07-31 21:29:14 +02:00
|
|
|
enum Item_result result_type () const { return STRING_RESULT; }
|
|
|
|
void fix_length_and_dec();
|
2003-06-04 17:28:51 +02:00
|
|
|
Item *copy_or_same(THD* thd);
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
2005-09-08 03:07:51 +02:00
|
|
|
|
|
|
|
class Item_sum_udf_decimal :public Item_udf_sum
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Item_sum_udf_decimal(udf_func *udf_arg)
|
|
|
|
:Item_udf_sum(udf_arg) {}
|
|
|
|
Item_sum_udf_decimal(udf_func *udf_arg, List<Item> &list)
|
|
|
|
:Item_udf_sum(udf_arg, list) {}
|
|
|
|
Item_sum_udf_decimal(THD *thd, Item_sum_udf_decimal *item)
|
|
|
|
:Item_udf_sum(thd, item) {}
|
|
|
|
String *val_str(String *);
|
|
|
|
double val_real();
|
|
|
|
longlong val_int();
|
|
|
|
my_decimal *val_decimal(my_decimal *);
|
|
|
|
enum Item_result result_type () const { return DECIMAL_RESULT; }
|
|
|
|
void fix_length_and_dec() { fix_num_length_and_dec(); }
|
|
|
|
Item *copy_or_same(THD* thd);
|
|
|
|
};
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
#else /* Dummy functions to get sql_yacc.cc compiled */
|
|
|
|
|
|
|
|
class Item_sum_udf_float :public Item_sum_num
|
|
|
|
{
|
|
|
|
public:
|
2005-09-08 03:07:51 +02:00
|
|
|
Item_sum_udf_float(udf_func *udf_arg)
|
|
|
|
:Item_sum_num() {}
|
2000-07-31 21:29:14 +02:00
|
|
|
Item_sum_udf_float(udf_func *udf_arg, List<Item> &list) :Item_sum_num() {}
|
2004-01-20 17:55:47 +01:00
|
|
|
Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
|
2003-01-30 17:07:39 +01:00
|
|
|
:Item_sum_num(thd, item) {}
|
2000-07-31 21:29:14 +02:00
|
|
|
enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
|
2005-09-08 03:07:51 +02:00
|
|
|
double val_real() { DBUG_ASSERT(fixed == 1); return 0.0; }
|
2003-08-28 17:31:38 +02:00
|
|
|
void clear() {}
|
2000-07-31 21:29:14 +02:00
|
|
|
bool add() { return 0; }
|
2003-08-20 15:25:44 +02:00
|
|
|
void update_field() {}
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Item_sum_udf_int :public Item_sum_num
|
|
|
|
{
|
|
|
|
public:
|
2005-09-08 03:07:51 +02:00
|
|
|
Item_sum_udf_int(udf_func *udf_arg)
|
|
|
|
:Item_sum_num() {}
|
2000-07-31 21:29:14 +02:00
|
|
|
Item_sum_udf_int(udf_func *udf_arg, List<Item> &list) :Item_sum_num() {}
|
2004-01-20 17:55:47 +01:00
|
|
|
Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
|
2003-01-30 17:07:39 +01:00
|
|
|
:Item_sum_num(thd, item) {}
|
2000-07-31 21:29:14 +02:00
|
|
|
enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
|
2004-03-18 14:14:36 +01:00
|
|
|
longlong val_int() { DBUG_ASSERT(fixed == 1); return 0; }
|
2005-09-08 03:07:51 +02:00
|
|
|
double val_real() { DBUG_ASSERT(fixed == 1); return 0; }
|
2003-08-28 02:10:14 +02:00
|
|
|
void clear() {}
|
2000-07-31 21:29:14 +02:00
|
|
|
bool add() { return 0; }
|
2003-08-20 15:25:44 +02:00
|
|
|
void update_field() {}
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-09-08 03:07:51 +02:00
|
|
|
class Item_sum_udf_decimal :public Item_sum_num
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Item_sum_udf_decimal(udf_func *udf_arg)
|
|
|
|
:Item_sum_num() {}
|
|
|
|
Item_sum_udf_decimal(udf_func *udf_arg, List<Item> &list)
|
|
|
|
:Item_sum_num() {}
|
|
|
|
Item_sum_udf_decimal(THD *thd, Item_sum_udf_float *item)
|
|
|
|
:Item_sum_num(thd, item) {}
|
|
|
|
enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
|
|
|
|
double val_real() { DBUG_ASSERT(fixed == 1); return 0.0; }
|
|
|
|
my_decimal *val_decimal(my_decimal *) { DBUG_ASSERT(fixed == 1); return 0; }
|
|
|
|
void clear() {}
|
|
|
|
bool add() { return 0; }
|
|
|
|
void update_field() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-07-31 21:29:14 +02:00
|
|
|
class Item_sum_udf_str :public Item_sum_num
|
|
|
|
{
|
|
|
|
public:
|
2005-09-08 03:07:51 +02:00
|
|
|
Item_sum_udf_str(udf_func *udf_arg)
|
|
|
|
:Item_sum_num() {}
|
|
|
|
Item_sum_udf_str(udf_func *udf_arg, List<Item> &list)
|
|
|
|
:Item_sum_num() {}
|
2004-01-20 17:55:47 +01:00
|
|
|
Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
|
2003-01-30 17:07:39 +01:00
|
|
|
:Item_sum_num(thd, item) {}
|
2004-03-18 14:14:36 +01:00
|
|
|
String *val_str(String *)
|
|
|
|
{ DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
|
2005-09-08 03:07:51 +02:00
|
|
|
double val_real() { DBUG_ASSERT(fixed == 1); null_value=1; return 0.0; }
|
2004-03-18 14:14:36 +01:00
|
|
|
longlong val_int() { DBUG_ASSERT(fixed == 1); null_value=1; return 0; }
|
2000-07-31 21:29:14 +02:00
|
|
|
enum Item_result result_type () const { return STRING_RESULT; }
|
|
|
|
void fix_length_and_dec() { maybe_null=1; max_length=0; }
|
|
|
|
enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
|
2003-08-28 02:10:14 +02:00
|
|
|
void clear() {}
|
2000-07-31 21:29:14 +02:00
|
|
|
bool add() { return 0; }
|
2003-08-20 15:25:44 +02:00
|
|
|
void update_field() {}
|
2000-07-31 21:29:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* HAVE_DLOPEN */
|
2003-03-18 00:07:40 +01:00
|
|
|
|
2003-04-03 20:19:14 +02:00
|
|
|
class MYSQL_ERROR;
|
|
|
|
|
2003-03-18 00:07:40 +01:00
|
|
|
class Item_func_group_concat : public Item_sum
|
|
|
|
{
|
|
|
|
TMP_TABLE_PARAM *tmp_table_param;
|
2003-04-03 20:19:14 +02:00
|
|
|
MYSQL_ERROR *warning;
|
2005-09-08 03:07:51 +02:00
|
|
|
String result;
|
|
|
|
String *separator;
|
|
|
|
TREE tree_base;
|
|
|
|
TREE *tree;
|
|
|
|
TABLE *table;
|
|
|
|
ORDER **order;
|
|
|
|
Name_resolution_context *context;
|
|
|
|
uint arg_count_order; // total count of ORDER BY items
|
|
|
|
uint arg_count_field; // count of arguments
|
|
|
|
uint count_cut_values;
|
2003-05-31 11:44:19 +02:00
|
|
|
bool distinct;
|
|
|
|
bool warning_for_row;
|
|
|
|
bool always_null;
|
2005-09-08 06:38:27 +02:00
|
|
|
bool no_appended;
|
2005-09-08 03:07:51 +02:00
|
|
|
/*
|
|
|
|
Following is 0 normal object and pointer to original one for copy
|
|
|
|
(to correctly free resources)
|
|
|
|
*/
|
|
|
|
Item_func_group_concat *original;
|
2003-05-31 11:44:19 +02:00
|
|
|
|
|
|
|
friend int group_concat_key_cmp_with_distinct(void* arg, byte* key1,
|
|
|
|
byte* key2);
|
2004-04-05 12:56:05 +02:00
|
|
|
friend int group_concat_key_cmp_with_order(void* arg, byte* key1,
|
|
|
|
byte* key2);
|
2003-05-31 11:44:19 +02:00
|
|
|
friend int group_concat_key_cmp_with_distinct_and_order(void* arg,
|
|
|
|
byte* key1,
|
|
|
|
byte* key2);
|
2005-09-08 03:07:51 +02:00
|
|
|
friend int dump_leaf_key(byte* key,
|
|
|
|
element_count count __attribute__((unused)),
|
2004-04-05 12:56:05 +02:00
|
|
|
Item_func_group_concat *group_concat_item);
|
2003-05-31 11:44:19 +02:00
|
|
|
|
2005-09-08 03:07:51 +02:00
|
|
|
public:
|
|
|
|
Item_func_group_concat(Name_resolution_context *context_arg,
|
|
|
|
bool is_distinct, List<Item> *is_select,
|
|
|
|
SQL_LIST *is_order, String *is_separator);
|
|
|
|
|
2004-04-05 12:56:05 +02:00
|
|
|
Item_func_group_concat(THD *thd, Item_func_group_concat *item);
|
2005-09-08 03:07:51 +02:00
|
|
|
~Item_func_group_concat() {}
|
2003-12-30 11:08:19 +01:00
|
|
|
void cleanup();
|
|
|
|
|
2003-03-18 00:07:40 +01:00
|
|
|
enum Sumfunctype sum_func () const {return GROUP_CONCAT_FUNC;}
|
|
|
|
const char *func_name() const { return "group_concat"; }
|
|
|
|
virtual Item_result result_type () const { return STRING_RESULT; }
|
2003-08-28 02:10:14 +02:00
|
|
|
void clear();
|
2003-03-18 00:07:40 +01:00
|
|
|
bool add();
|
2005-09-08 06:38:27 +02:00
|
|
|
void reset_field() { DBUG_ASSERT(0); } // not used
|
|
|
|
void update_field() { DBUG_ASSERT(0); } // not used
|
2005-09-08 03:07:51 +02:00
|
|
|
bool fix_fields(THD *,Item **);
|
2003-03-18 00:07:40 +01:00
|
|
|
bool setup(THD *thd);
|
2003-06-04 17:28:51 +02:00
|
|
|
void make_unique();
|
2005-09-08 03:07:51 +02:00
|
|
|
double val_real()
|
2003-03-18 00:07:40 +01:00
|
|
|
{
|
|
|
|
String *res; res=val_str(&str_value);
|
2004-02-13 15:27:21 +01:00
|
|
|
return res ? my_atof(res->c_ptr()) : 0.0;
|
2003-03-18 00:07:40 +01:00
|
|
|
}
|
|
|
|
longlong val_int()
|
|
|
|
{
|
2004-05-07 00:43:17 +02:00
|
|
|
String *res;
|
|
|
|
char *end_ptr;
|
|
|
|
int error;
|
2005-01-15 10:05:00 +01:00
|
|
|
if (!(res= val_str(&str_value)))
|
|
|
|
return (longlong) 0;
|
2004-05-07 00:43:17 +02:00
|
|
|
end_ptr= (char*) res->ptr()+ res->length();
|
2005-01-15 10:05:00 +01:00
|
|
|
return my_strtoll10(res->ptr(), &end_ptr, &error);
|
2003-03-18 00:07:40 +01:00
|
|
|
}
|
2005-09-08 03:07:51 +02:00
|
|
|
my_decimal *val_decimal(my_decimal *decimal_value)
|
|
|
|
{
|
|
|
|
return val_decimal_from_string(decimal_value);
|
|
|
|
}
|
2003-03-18 00:07:40 +01:00
|
|
|
String* val_str(String* str);
|
2003-06-04 17:28:51 +02:00
|
|
|
Item *copy_or_same(THD* thd);
|
2003-08-28 22:28:32 +02:00
|
|
|
void no_rows_in_result() {}
|
2003-10-12 16:56:05 +02:00
|
|
|
void print(String *str);
|
2005-09-08 03:07:51 +02:00
|
|
|
virtual bool change_context_processor(byte *cntx)
|
|
|
|
{ context= (Name_resolution_context *)cntx; return FALSE; }
|
2003-03-18 00:07:40 +01:00
|
|
|
};
|