Skip to content

Commit

Permalink
At the parsing phase it is impossible to differentiate between an agg…
Browse files Browse the repository at this point in the history
…regate function and a normal function, so during the Item_func_sp::fix_fields we check if the particular function exists or not , if it exists and is an aggregate function we substitute Item_func_sp with Item_sum_sp
  • Loading branch information
varunraiko committed Nov 17, 2017
1 parent 83ec737 commit a9400a9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
22 changes: 4 additions & 18 deletions sql/item_create.cc
Expand Up @@ -3461,25 +3461,11 @@ Create_sp_func::create_with_db(THD *thd, LEX_CSTRING *db, LEX_CSTRING *name,

qname= new (thd->mem_root) sp_name(db, name, use_explicit_name);
sp_add_used_routine(lex, thd, qname, TYPE_ENUM_FUNCTION);
if (db_get_aggregate_value(thd,TYPE_ENUM_FUNCTION,qname,&chistics) == SP_OK)
{
if (chistics.agg_type == GROUP_AGGREGATE)
{
if (arg_count > 0)
func= new (thd->mem_root) Item_sum_sp(thd, lex->current_context(), qname,
if (arg_count > 0)
func= new (thd->mem_root) Item_func_sp(thd, lex->current_context(), qname,
*item_list);
else
func= new (thd->mem_root) Item_sum_sp(thd, lex->current_context(), qname);
}
else
{
if (arg_count > 0)
func= new (thd->mem_root) Item_func_sp(thd, lex->current_context(), qname,
*item_list);
else
func= new (thd->mem_root) Item_func_sp(thd, lex->current_context(), qname);
}
}
else
func= new (thd->mem_root) Item_func_sp(thd, lex->current_context(), qname);

lex->safe_to_cache_query= 0;
return func;
Expand Down
39 changes: 36 additions & 3 deletions sql/item_func.cc
Expand Up @@ -6364,6 +6364,9 @@ Item_func_sp::init_result_field(THD *thd)
DBUG_RETURN(TRUE);
}

if (m_sp->m_chistics->agg_type == GROUP_AGGREGATE)
DBUG_RETURN(FALSE);

/*
A Field need to be attached to a Table.
Below we "create" a dummy table by initializing
Expand Down Expand Up @@ -6654,15 +6657,45 @@ Item_func_sp::fix_fields(THD *thd, Item **ref)
which is called from Item_func::fix_fields().
*/
res= init_result_field(thd);

if (res)
DBUG_RETURN(res);
DBUG_RETURN(TRUE);

if (m_sp->m_chistics->agg_type == GROUP_AGGREGATE)
{
List<Item> list;
list.empty();
for (uint i=0; i < arg_count; i++)
list.push_back(*(args+i));

Item_sum_sp *item_sp;
Query_arena *arena, backup;
arena= thd->activate_stmt_arena_if_needed(&backup);

if (arg_count)
item_sp= new (thd->mem_root) Item_sum_sp(thd, context, m_name, list);
else
item_sp= new (thd->mem_root) Item_sum_sp(thd, context, m_name);

if (arena)
thd->restore_active_arena(arena, &backup);
if (!item_sp)
DBUG_RETURN(TRUE);
*ref= item_sp;
item_sp->name= name;
bool err= item_sp->fix_fields(thd, ref);
if (err)
DBUG_RETURN(TRUE);

list.empty();
DBUG_RETURN(FALSE);
}

res= Item_func::fix_fields(thd, ref);

if (res)
DBUG_RETURN(res);

res= Item_func::fix_fields(thd, ref);

if (thd->lex->is_view_context_analysis())
{
/*
Expand Down

0 comments on commit a9400a9

Please sign in to comment.