Skip to content

Commit

Permalink
Minor reorganization in Item hierarchy, to remove duplicate code.
Browse files Browse the repository at this point in the history
- Adding a new class Item_args, represending regular function or
  aggregate function arguments array.

- Adding a new class Item_func_or_sum,
  a parent class for Item_func and Item_sum

- Moving Item_result_field::name() to Item_func_or_sum(),
  as name() is not needed on Item_result_field level.
  • Loading branch information
Alexander Barkov committed Apr 17, 2015
1 parent 3c4668c commit 99898c6
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 122 deletions.
2 changes: 1 addition & 1 deletion sql/item.cc
Expand Up @@ -7466,7 +7466,7 @@ void Item_cache_wrapper::print(String *str, enum_query_type query_type)
return;
}

str->append(func_name());
str->append("<expr_cache>");
if (expr_cache)
{
init_on_demand();
Expand Down
88 changes: 87 additions & 1 deletion sql/item.h
Expand Up @@ -3263,6 +3263,93 @@ class Item_result_field :public Item /* Item with result field */
}
void cleanup();
bool check_vcol_func_processor(uchar *arg) { return FALSE;}
};


/**
Array of items, e.g. function or aggerate function arguments.
*/
class Item_args
{
protected:
Item **args, *tmp_arg[2];
void set_arguments(List<Item> &list);
public:
uint arg_count;
Item_args(void)
:args(NULL), arg_count(0)
{ }
Item_args(Item *a)
:args(tmp_arg), arg_count(1)
{
args[0]= a;
}
Item_args(Item *a, Item *b)
:args(tmp_arg), arg_count(2)
{
args[0]= a; args[1]= b;
}
Item_args(Item *a, Item *b, Item *c)
{
arg_count= 0;
if ((args= (Item**) sql_alloc(sizeof(Item*) * 3)))
{
arg_count= 3;
args[0]= a; args[1]= b; args[2]= c;
}
}
Item_args(Item *a, Item *b, Item *c, Item *d)
{
arg_count= 0;
if ((args= (Item**) sql_alloc(sizeof(Item*) * 4)))
{
arg_count= 4;
args[0]= a; args[1]= b; args[2]= c; args[3]= d;
}
}
Item_args(Item *a, Item *b, Item *c, Item *d, Item* e)
{
arg_count= 5;
if ((args= (Item**) sql_alloc(sizeof(Item*) * 5)))
{
arg_count= 5;
args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
}
}
Item_args(List<Item> &list)
{
set_arguments(list);
}
Item_args(THD *thd, const Item_args *other);
inline Item **arguments() const { return args; }
inline uint argument_count() const { return arg_count; }
inline void remove_arguments() { arg_count=0; }
};


/**
An abstract class representing common features of
regular functions and aggregate functions.
*/
class Item_func_or_sum: public Item_result_field, public Item_args
{
public:
Item_func_or_sum()
:Item_result_field(), Item_args() {}
Item_func_or_sum(Item *a)
:Item_result_field(), Item_args(a) { }
Item_func_or_sum(Item *a, Item *b)
:Item_result_field(), Item_args(a, b) { }
Item_func_or_sum(Item *a, Item *b, Item *c)
:Item_result_field(), Item_args(a, b, c) { }
Item_func_or_sum(Item *a, Item *b, Item *c, Item *d)
:Item_result_field(), Item_args(a, b, c, d) { }
Item_func_or_sum(Item *a, Item *b, Item *c, Item *d, Item *e)
:Item_result_field(), Item_args(a, b, c, d, e) { }
Item_func_or_sum(THD *thd, Item_func_or_sum *item)
:Item_result_field(thd, item), Item_args(thd, item) { }
Item_func_or_sum(List<Item> &list)
:Item_result_field(), Item_args(list) { }
/*
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
Expand Down Expand Up @@ -3559,7 +3646,6 @@ class Item_cache_wrapper :public Item_result_field
Item_cache_wrapper(Item *item_arg);
~Item_cache_wrapper();

const char *func_name() const { return "<expr_cache>"; }
enum Type type() const { return EXPR_CACHE_ITEM; }
enum Type real_type() const { return orig_item->type(); }

Expand Down
6 changes: 3 additions & 3 deletions sql/item_cmpfunc.cc
Expand Up @@ -585,7 +585,7 @@ void Item_bool_func2::fix_length_and_dec()
}


int Arg_comparator::set_compare_func(Item_result_field *item, Item_result type)
int Arg_comparator::set_compare_func(Item_func_or_sum *item, Item_result type)
{
owner= item;
func= comparator_matrix[type]
Expand Down Expand Up @@ -787,7 +787,7 @@ bool Arg_comparator::agg_arg_charsets_for_comparison()
items, holding the cached converted value of the original (constant) item.
*/

int Arg_comparator::set_cmp_func(Item_result_field *owner_arg,
int Arg_comparator::set_cmp_func(Item_func_or_sum *owner_arg,
Item **a1, Item **a2,
Item_result type)
{
Expand Down Expand Up @@ -858,7 +858,7 @@ Item** Arg_comparator::cache_converted_constant(THD *thd_arg, Item **value,
}


void Arg_comparator::set_datetime_cmp_func(Item_result_field *owner_arg,
void Arg_comparator::set_datetime_cmp_func(Item_func_or_sum *owner_arg,
Item **a1, Item **b1)
{
thd= current_thd;
Expand Down
13 changes: 7 additions & 6 deletions sql/item_cmpfunc.h
Expand Up @@ -40,16 +40,16 @@ class Arg_comparator: public Sql_alloc
{
Item **a, **b;
arg_cmp_func func;
Item_result_field *owner;
Item_func_or_sum *owner;
bool set_null; // TRUE <=> set owner->null_value
Arg_comparator *comparators; // used only for compare_row()
double precision;
/* Fields used in DATE/DATETIME comparison. */
THD *thd;
Item *a_cache, *b_cache; // Cached values of a and b items
// when one of arguments is NULL.
int set_compare_func(Item_result_field *owner, Item_result type);
inline int set_compare_func(Item_result_field *owner_arg)
int set_compare_func(Item_func_or_sum *owner, Item_result type);
inline int set_compare_func(Item_func_or_sum *owner_arg)
{
return set_compare_func(owner_arg, item_cmp_type((*a)->result_type(),
(*b)->result_type()));
Expand All @@ -67,11 +67,11 @@ class Arg_comparator: public Sql_alloc
comparators(0), thd(0), a_cache(0), b_cache(0) {};

private:
int set_cmp_func(Item_result_field *owner_arg,
int set_cmp_func(Item_func_or_sum *owner_arg,
Item **a1, Item **a2,
Item_result type);
public:
inline int set_cmp_func(Item_result_field *owner_arg,
inline int set_cmp_func(Item_func_or_sum *owner_arg,
Item **a1, Item **a2, bool set_null_arg)
{
set_null= set_null_arg;
Expand Down Expand Up @@ -104,7 +104,8 @@ class Arg_comparator: public Sql_alloc

Item** cache_converted_constant(THD *thd, Item **value, Item **cache,
Item_result type);
void set_datetime_cmp_func(Item_result_field *owner_arg, Item **a1, Item **b1);
void set_datetime_cmp_func(Item_func_or_sum *owner_arg,
Item **a1, Item **b1);
static arg_cmp_func comparator_matrix [6][2];
inline bool is_owner_equal_func()
{
Expand Down
70 changes: 35 additions & 35 deletions sql/item_func.cc
Expand Up @@ -88,51 +88,51 @@ static inline bool test_if_sum_overflows_ull(ulonglong arg1, ulonglong arg2)
return ULONGLONG_MAX - arg1 < arg2;
}

void Item_func::set_arguments(List<Item> &list)

void Item_args::set_arguments(List<Item> &list)
{
allowed_arg_cols= 1;
arg_count=list.elements;
args= tmp_arg; // If 2 arguments
if (arg_count <= 2 || (args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
arg_count= list.elements;
if (arg_count <= 2)
{
List_iterator_fast<Item> li(list);
Item *item;
Item **save_args= args;

while ((item=li++))
{
*(save_args++)= item;
with_sum_func|=item->with_sum_func;
with_field|= item->with_field;
}
args= tmp_arg;
}
list.empty(); // Fields are used
else if (!(args= (Item**) sql_alloc(sizeof(Item*) * arg_count)))
{
arg_count= 0;
return;
}
uint i= 0;
List_iterator_fast<Item> li(list);
Item *item;
while ((item= li++))
args[i++]= item;
}

Item_func::Item_func(List<Item> &list)
:allowed_arg_cols(1)

Item_args::Item_args(THD *thd, const Item_args *other)
:arg_count(other->arg_count)
{
set_arguments(list);
if (arg_count <= 2)
{
args= tmp_arg;
}
else if (!(args= (Item**) thd->alloc(sizeof(Item*) * arg_count)))
{
arg_count= 0;
return;
}
memcpy(args, other->args, sizeof(Item*) * arg_count);
}

Item_func::Item_func(THD *thd, Item_func *item)
:Item_result_field(thd, item),
allowed_arg_cols(item->allowed_arg_cols),
arg_count(item->arg_count),
used_tables_cache(item->used_tables_cache),
not_null_tables_cache(item->not_null_tables_cache),
const_item_cache(item->const_item_cache)

void Item_func::sync_with_sum_func_and_with_field(List<Item> &list)
{
if (arg_count)
List_iterator_fast<Item> li(list);
Item *item;
while ((item= li++))
{
if (arg_count <=2)
args= tmp_arg;
else
{
if (!(args=(Item**) thd->alloc(sizeof(Item*)*arg_count)))
return;
}
memcpy((char*) args, (char*) item->args, sizeof(Item*)*arg_count);
with_sum_func|= item->with_sum_func;
with_field|= item->with_field;
}
}

Expand Down

0 comments on commit 99898c6

Please sign in to comment.