Commit c6beb583 authored by bell@sanja.is.com.ua's avatar bell@sanja.is.com.ua
Browse files

fixed functions to be able work with group function as argument

made bisone 1.75 compatible code
parent 710ffb2d
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ Sinisa@sinisa.nasamreza.org
arjen@fred.bitbike.com
bar@bar.mysql.r18.ru
bar@bar.udmsearch.izhnet.ru
bell@sanja.is.com.ua
bk@admin.bk
heikki@donna.mysql.fi
heikki@hundin.mysql.fi
+18 −0
Original line number Diff line number Diff line
@@ -110,3 +110,21 @@ a count(*)
NULL	9
	3
b	1
a	MAX(b)	INTERVAL (MAX(b), 1,3,10,30,39,40,50,60,100,1000)
1	4	2
10	43	6
a	MAX(b)	CASE MAX(b) when 4 then 4 when 43 then 43 else 0 end
1	4	4
10	43	43
a	MAX(b)	FIELD(MAX(b), '43', '4', '5')
1	4	2
10	43	1
a	MAX(b)	CONCAT_WS(MAX(b), '43', '4', '5')
1	4	434445
10	43	43434435
a	MAX(b)	ELT(MAX(b), 'a', 'b', 'c', 'd', 'e', 'f')
1	4	d
10	43	NULL
a	MAX(b)	MAKE_SET(MAX(b), 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
1	4	c
10	43	a,b,d,f
+23 −0
Original line number Diff line number Diff line
@@ -323,3 +323,26 @@ select a,count(*) from t1 group by a;
set option sql_big_tables=1;
select a,count(*) from t1 group by a;
drop table t1;


#
# group function arguments in some functions
#

create table t1 (a int, b int);
insert into t1 values (1, 4);
insert into t1 values (10, 40);
insert into t1 values (1, 4);
insert into t1 values (10, 43);
insert into t1 values (1, 4);
insert into t1 values (10, 41);
insert into t1 values (1, 4);
insert into t1 values (10, 43);
insert into t1 values (1, 4);
select a, MAX(b), INTERVAL (MAX(b), 1,3,10,30,39,40,50,60,100,1000) from t1 group by a;
select a, MAX(b), CASE MAX(b) when 4 then 4 when 43 then 43 else 0 end from t1 group by a;
select a, MAX(b), FIELD(MAX(b), '43', '4', '5') from t1 group by a;
select a, MAX(b), CONCAT_WS(MAX(b), '43', '4', '5') from t1 group by a;
select a, MAX(b), ELT(MAX(b), 'a', 'b', 'c', 'd', 'e', 'f') from t1 group by a;
select a, MAX(b), MAKE_SET(MAX(b), 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h') from t1 group by a;
drop table t1;
+42 −1
Original line number Diff line number Diff line
@@ -287,6 +287,19 @@ void Item_func_interval::fix_length_and_dec()
  }
  maybe_null=0; max_length=2;
  used_tables_cache|=item->used_tables();
  with_sum_func= with_sum_func || item->with_sum_func;
}

void Item_func_interval::split_sum_func(List<Item> &fields)
{
  if (item->with_sum_func && item->type() != SUM_FUNC_ITEM)
    item->split_sum_func(fields);
  else if (item->used_tables() || item->type() == SUM_FUNC_ITEM)
  {
    fields.push_front(item);
    item= new Item_ref((Item**) fields.head_ref(), 0, item->name);
  }
  Item_int_func::split_sum_func(fields);
}

/*
@@ -739,17 +752,45 @@ Item_func_case::fix_fields(THD *thd,TABLE_LIST *tables)
  {
    used_tables_cache|=(first_expr)->used_tables();
    const_item_cache&= (first_expr)->const_item();
    with_sum_func= with_sum_func || (first_expr)->with_sum_func;
  }
  if (else_expr)
  {
    used_tables_cache|=(else_expr)->used_tables();
    const_item_cache&= (else_expr)->const_item();
    with_sum_func= with_sum_func || (else_expr)->with_sum_func;
  }
  if (!else_expr || else_expr->maybe_null)
    maybe_null=1;				// The result may be NULL
  return 0;
}

void Item_func_case::split_sum_func(List<Item> &fields)
{
  if (first_expr)
  {
    if (first_expr->with_sum_func && first_expr->type() != SUM_FUNC_ITEM)
      first_expr->split_sum_func(fields);
    else if (first_expr->used_tables() || first_expr->type() == SUM_FUNC_ITEM)
    {
      fields.push_front(first_expr);
      first_expr= new Item_ref((Item**) fields.head_ref(), 0,
			       first_expr->name);
    }
  }
  if (else_expr)
  {
    if (else_expr->with_sum_func && else_expr->type() != SUM_FUNC_ITEM)
      else_expr->split_sum_func(fields);
    else if (else_expr->used_tables() || else_expr->type() == SUM_FUNC_ITEM)
    {
      fields.push_front(else_expr);
      else_expr= new Item_ref((Item**) fields.head_ref(), 0, else_expr->name);
    }
  }
  Item_func::split_sum_func(fields);
}

void Item_func_case::update_used_tables()
{
  Item_func::update_used_tables();
+2 −0
Original line number Diff line number Diff line
@@ -180,6 +180,7 @@ class Item_func_interval :public Item_int_func
  {
    return (item->fix_fields(thd,tlist) || Item_func::fix_fields(thd,tlist));
  }
  void split_sum_func(List<Item> &fields);
  void fix_length_and_dec();
  ~Item_func_interval() { delete item; }
  const char *func_name() const { return "interval"; }
@@ -259,6 +260,7 @@ class Item_func_case :public Item_func
  const char *func_name() const { return "case"; }
  void print(String *str);
  bool fix_fields(THD *thd,struct st_table_list *tlist);
  void split_sum_func(List<Item> &fields);
  Item *find_item(String *str);
};

Loading