=== modified file 'mysql-test/r/udf.result'
--- mysql-test/r/udf.result	2011-12-12 22:58:40 +0000
+++ mysql-test/r/udf.result	2014-12-21 07:47:18 +0000
@@ -53,6 +53,15 @@
 avgcost(sum, price)
 151.4367
 drop table t1;
+create table t1(sum int, price float(24), g int);
+insert into t1 values(100, 50.00, 1), (100, 100.00, 1);
+insert into t1 values(10, 2.00, 2), (100, 100.00, 2);
+select avgcost(sum, price) from t1 group by g with rollup;
+avgcost(sum, price)
+75.0000
+91.0909
+80.7097
+drop table t1;
 select metaphon('hello');
 metaphon('hello')
 HL

=== modified file 'mysql-test/t/udf.test'
--- mysql-test/t/udf.test	2011-10-19 19:45:18 +0000
+++ mysql-test/t/udf.test	2014-12-21 05:44:00 +0000
@@ -74,6 +74,14 @@
 select avgcost(sum, price) from t1;
 drop table t1;
 
+
+#BUG#38297 init not called for rollup function
+create table t1(sum int, price float(24), g int);
+insert into t1 values(100, 50.00, 1), (100, 100.00, 1);
+insert into t1 values(10, 2.00, 2), (100, 100.00, 2);
+select avgcost(sum, price) from t1 group by g with rollup;
+drop table t1;
+
 #------------------------------------------------------------------------
 # BUG#17261 Passing a variable from a stored procedure to UDF crashes mysqld
 #------------------------------------------------------------------------

=== modified file 'sql/item_func.cc'
--- sql/item_func.cc	2014-09-06 06:33:56 +0000
+++ sql/item_func.cc	2014-12-21 08:42:23 +0000
@@ -3468,6 +3468,42 @@
 }
 
 
+bool udf_handler::make_unique()
+{
+  if (u_d->func_init)
+  {
+    char init_msg_buff[MYSQL_ERRMSG_SIZE];
+    // Don't have func any more so assuming these are going to be the same
+    // These may have been overwritten by previous func_init for the non-rollup case
+    // initid.max_length=func->max_length;
+    // initid.maybe_null=func->maybe_null;
+    // initid.decimals=func->decimals;
+    // Should mirror fix_fields below except fields are already initialised
+    initid.const_item=const_item_cache;
+    initid.ptr=0;
+    Udf_func_init init= u_d->func_init;
+    if ((error=(uchar) init(&initid, &f_args, init_msg_buff)))
+    {
+      my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
+               u_d->name.str, init_msg_buff);
+      free_udf(u_d);
+      return FALSE;
+    }
+    const_item_cache=initid.const_item;
+    /* 
+      Keep used_tables_cache in sync with const_item_cache.
+      See the comment in Item_udf_func::update_used tables.
+    */  
+    if (!const_item_cache && !used_tables_cache)
+      used_tables_cache= RAND_TABLE_BIT;
+
+    not_original = FALSE;
+    buffers = NULL;
+  }
+  return TRUE;
+}
+
+
 bool
 udf_handler::fix_fields(THD *thd, Item_result_field *func,
 			uint arg_count, Item **arguments)

=== modified file 'sql/item_sum.cc'
--- sql/item_sum.cc	2014-10-09 08:30:11 +0000
+++ sql/item_sum.cc	2014-12-21 08:33:18 +0000
@@ -3600,13 +3600,15 @@
 
 /* This is used by rollup to create a separate usable copy of the function */
 
-void Item_func_group_concat::make_unique()
+bool Item_func_group_concat::make_unique()
 {
   tmp_table_param= 0;
   table=0;
   original= 0;
   force_copy_fields= 1;
   tree= 0;
+
+  return TRUE;
 }
 
 

=== modified file 'sql/item_sum.h'
--- sql/item_sum.h	2014-03-26 21:25:38 +0000
+++ sql/item_sum.h	2014-12-21 08:34:06 +0000
@@ -476,7 +476,7 @@
                    Aggregator::SIMPLE_AGGREGATOR);
     aggregator_clear();
   }
-  virtual void make_unique() { force_copy_fields= TRUE; }
+  virtual bool make_unique() { force_copy_fields= TRUE; return TRUE; }
   Item *get_tmp_table_item(THD *thd);
   virtual Field *create_tmp_field(bool group, TABLE *table,
                                   uint convert_blob_length);
@@ -1193,6 +1193,8 @@
   enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
   virtual bool have_field_update(void) const { return 0; }
 
+  virtual bool make_unique() { return TRUE; }
+
   void clear();
   bool add();
   void reset_field() {};
@@ -1456,7 +1458,7 @@
   void update_field() { DBUG_ASSERT(0); }       // not used
   bool fix_fields(THD *,Item **);
   bool setup(THD *thd);
-  void make_unique();
+  bool make_unique();
   double val_real()
   {
     int error;

=== modified file 'sql/sql_select.cc'
--- sql/sql_select.cc	2014-12-03 14:49:31 +0000
+++ sql/sql_select.cc	2014-12-21 08:34:43 +0000
@@ -22818,6 +22818,7 @@
   List_iterator_fast<Item> it(fields_arg);
   Item *first_field= sel_fields.head();
   uint level;
+  bool res;
 
   /*
     Create field lists for the different levels
@@ -22885,7 +22886,11 @@
 	  sub selects.
 	*/
 	item= item->copy_or_same(thd);
-	((Item_sum*) item)->make_unique();
+	res = ((Item_sum*) item)->make_unique();
+	if (!res) {
+	  delete item;
+	  return 1;
+	}
 	*(*func)= (Item_sum*) item;
 	(*func)++;
       }

=== modified file 'sql/sql_udf.h'
--- sql/sql_udf.h	2013-09-14 01:09:36 +0000
+++ sql/sql_udf.h	2014-12-21 08:32:30 +0000
@@ -78,6 +78,7 @@
   bool get_arguments();
   bool fix_fields(THD *thd, Item_result_field *item,
 		  uint arg_count, Item **args);
+  bool make_unique();
   void cleanup();
   double val(my_bool *null_value)
   {

