Statistics for MySQL  1.1
 All Classes Files Functions Variables Typedefs Macros Pages
avgw.c
Go to the documentation of this file.
1 /* avgw.c (weighted average) */
2 
3 /***********************************************************************
4 * This code is part of Statistics for MySQL.
5 *
6 * Copyright (C) 2011 Heinrich Schuchardt (xypron.glpk@gmx.de)
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 ***********************************************************************/
20 
32 #include "sqlstat.h"
33 
37 struct avgw_storage {
38  int argc;
39  double weight;
40  double sumX;
41 };
42 
55 my_bool avgw_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
56  struct avgw_storage * data;
57 
58  if (args->arg_count < 1 || args->arg_count > 2) {
59  strcpy(message,"avgw() requires one or two arguments");
60  return 1;
61  }
62  args->arg_type[0] = REAL_RESULT;
63  if (args->arg_count > 1) {
64  args->arg_type[1] = REAL_RESULT;
65  }
66 
67  data = (struct avgw_storage *) malloc( sizeof(struct avgw_storage));
68  if (data == NULL) {
69  strcpy(message,"Couldn't allocate memory");
70  return 1;
71  }
72  data->argc = args->arg_count;
73 
74  initid->maybe_null = 1;
75  initid->decimals = NOT_FIXED_DEC;
76  initid->max_length = 13 + initid->decimals;
77  initid->ptr = (char *) data;
78  initid->const_item = 0;
79 
80  return 0;
81 }
82 
94 void avgw_reset(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
95  avgw_clear(initid, is_null, error);
96  avgw_add(initid, args, is_null, error);
97 }
98 
108 void avgw_clear(UDF_INIT *initid, char *is_null, char *error) {
109  struct avgw_storage *data;
110  data = (struct avgw_storage *) initid->ptr;
111  data->weight = 0;
112  data->sumX = 0;
113 }
114 
125 void avgw_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
126  struct avgw_storage * data;
127  double x;
128  double w;
129 
130  if (!args->args[0]) {
131  return;
132  }
133  data = (struct avgw_storage *) initid->ptr;
134  if (data->argc > 1) {
135  if (!args->args[1]) {
136  return;
137  }
138  w = *((double*) args->args[1]);
139  } else {
140  w = 1.;
141  }
142  x = *((double*) args->args[0]);
143 
144  data->weight += w;
145  data->sumX += w * x;
146 }
147 
157 double avgw(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
158  struct avgw_storage * data;
159 
160  data = (struct avgw_storage *) initid->ptr;
161 
162  if (data->weight <= 0) {
163  *is_null = 1;
164  return 0;
165  }
166 
167  return data->sumX / data->weight;
168 }
169 
177 void avgw_deinit(UDF_INIT *initid) {
178  if (initid->ptr) {
179  free(initid->ptr);
180  }
181 }
Definition of functions for UDFs and plugins.
void avgw_deinit(UDF_INIT *initid)
Called after last access to function.
Definition: avgw.c:177
#define NOT_FIXED_DEC
Maximum number of digits in double As defined in mysql/sql_string.h.
Definition: sqlstat.h:77
void avgw_reset(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Reset function and add first group member Calls clear and add.
Definition: avgw.c:94
int argc
number of arguments
Definition: avgw.c:38
void avgw_clear(UDF_INIT *initid, char *is_null, char *error)
Called at start of group.
Definition: avgw.c:108
my_bool avgw_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
Called before first usage of function.
Definition: avgw.c:55
Storage for weighted average.
Definition: avgw.c:37
double sumX
Sum of x.
Definition: avgw.c:40
double weight
Sum of weight.
Definition: avgw.c:39
double avgw(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Retrieve weighted average. Called at end of group.
Definition: avgw.c:157
void avgw_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Add a member of the group.
Definition: avgw.c:125