Statistics for MySQL  1.1
 All Classes Files Functions Variables Typedefs Macros Pages
median.cc
Go to the documentation of this file.
1 /* median.cc (median) */
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 
21 
31 #include "sqlstat.h"
32 #include <set>
33 
34 using namespace std;
35 
39 typedef multiset<double, less<double> > DoubleSet;
40 
53 my_bool median_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
54  DoubleSet *data;
55 
56  if (args->arg_count != 1) {
57  strcpy(message,"median() requires one argument");
58  return 1;
59  }
60  args->arg_type[0] = REAL_RESULT;
61 
62  data = new DoubleSet();
63  if (data == NULL) {
64  strcpy(message,"Couldn't allocate memory");
65  return 1;
66  }
67 
68  initid->maybe_null = 1;
69  initid->decimals = NOT_FIXED_DEC;
70  initid->max_length = 13 + initid->decimals;
71  initid->ptr = (char *) data;
72  initid->const_item = 0;
73 
74  return 0;
75 }
76 
88 void median_reset(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
89  median_clear(initid, is_null, error);
90  median_add(initid, args, is_null, error);
91 }
92 
102 void median_clear(UDF_INIT *initid, char *is_null, char *error) {
103  DoubleSet *data;
104 
105  data = (DoubleSet *) initid->ptr;
106  data->clear();
107 }
108 
119 void median_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
120  DoubleSet *data;
121  double x;
122 
123  if (!args->args[0]) {
124  return;
125  }
126  data = (DoubleSet *) initid->ptr;
127  x = *((double*) args->args[0]);
128 
129  try {
130  data->insert(x);
131  } catch (std::bad_alloc e) {
132  *error = 1;
133  }
134 }
135 
145 double median(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) {
146  DoubleSet *data;
147  DoubleSet::iterator pos;
148  double ret;
149  size_t n;
150  size_t n2;
151 
152  data = (DoubleSet *) initid->ptr;
153 
154 
155  n = data->size();
156  if (n <= 0 ) {
157  *is_null = 1;
158  return 0;
159  }
160 
161  n2 = ++n / 2;
162 
163  pos = data->begin();
164  for (size_t i = 0; i < n2; i++) {
165  ret = *pos++;
166  }
167 
168  if (1 & n) {
169  ret += *pos;
170  ret /= 2;
171  }
172 
173  return ret;
174 }
175 
183 void median_deinit(UDF_INIT *initid) {
184  DoubleSet *data;
185  if (initid->ptr) {
186  data = (DoubleSet *) initid->ptr;
187  data->~multiset();
188  }
189 }
Definition of functions for UDFs and plugins.
void median_deinit(UDF_INIT *initid)
Called after last access to function.
Definition: median.cc:183
multiset< double, less< double > > DoubleSet
Multiset of doubles.
Definition: median.cc:39
void median_clear(UDF_INIT *initid, char *is_null, char *error)
Called at start of group.
Definition: median.cc:102
my_bool median_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
Called before first usage of function.
Definition: median.cc:53
void median_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Add a member of the group.
Definition: median.cc:119
#define NOT_FIXED_DEC
Maximum number of digits in double As defined in mysql/sql_string.h.
Definition: sqlstat.h:77
ItemSet data
collected data
Definition: gini.cc:59
void median_reset(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Reset function and add first group member Calls clear and add.
Definition: median.cc:88
double median(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
Retrieve median. Called at end of group.
Definition: median.cc:145