forked from megastep/mysql-udf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udf_median.cc
208 lines (160 loc) · 4.48 KB
/
udf_median.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
returns the median of the values in a distribution
input parameters:
data (real)
number of decimals in result (int, optional)
output:
median value of the distribution (real)
registering the function:
CREATE AGGREGATE FUNCTION median RETURNS REAL SONAME 'udf_median.so';
getting rid of the function:
DROP FUNCTION median;
04/15/2009
- added percentile, an optional third parameter median function
- default percentile is 50 (for the standard median)
*/
#ifdef STANDARD
#include <stdio.h>
#include <string.h>
#ifdef __WIN__
typedef unsigned __int64 ulonglong;
typedef __int64 longlong;
#else
typedef unsigned long long ulonglong;
typedef long long longlong;
#endif /*__WIN__*/
#else
#include <my_global.h>
#include <my_sys.h>
#endif
#include <mysql.h>
#include <m_ctype.h>
#include <m_string.h>
/*#ifdef HAVE_DLOPEN*/
#define BUFFERSIZE 1024
extern "C" {
my_bool median_init( UDF_INIT* initid, UDF_ARGS* args, char* message );
void median_deinit( UDF_INIT* initid );
void median_clear( UDF_INIT* initid, char* is_null, char *error );
void median_reset( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error );
void median_add( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error );
double median( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error );
}
struct median_data
{
unsigned long count;
unsigned long abscount;
unsigned long pages;
unsigned long percentile;
double *values;
};
my_bool median_init( UDF_INIT* initid, UDF_ARGS* args, char* message )
{
if (args->arg_count < 1 || args->arg_count>3)
{
strcpy(message,"wrong number of arguments: median() requires one or two arguments");
return 1;
}
if (args->arg_type[0]!=REAL_RESULT)
{
/* strcpy(message,"median() requires a real as parameter 1");
return 1; */
args->arg_type[0] = REAL_RESULT;
}
if (args->arg_count>1 && (args->arg_type[1]!=INT_RESULT))
{
strcpy(message,"median() requires an int as parameter 2");
return 1;
}
initid->decimals=2;
if (args->arg_count==2 && (*((ulong*)args->args[1])<=16))
{
initid->decimals=*((ulong*)args->args[1]);
}
/* Default median percentage is 50% */
unsigned long percentile=50;
if (args->arg_count==3 && (*((ulong*)args->args[2])>=0) && (*((ulong*)args->args[2])<=100))
{
percentile=*((ulong*)args->args[2]);
}
median_data *buffer = new median_data;
buffer->count = 0;
buffer->abscount=0;
buffer->pages = 1;
buffer->values = NULL;
buffer->percentile=percentile;
initid->maybe_null = 1;
initid->max_length = 32;
initid->ptr = (char*)buffer;
return 0;
}
void median_deinit( UDF_INIT* initid )
{
median_data *buffer = (median_data*)initid->ptr;
if (buffer->values != NULL)
{
free(buffer->values);
buffer->values=NULL;
}
delete initid->ptr;
}
void median_clear( UDF_INIT* initid, char* is_null, char* is_error )
{
median_data *buffer = (median_data*)initid->ptr;
buffer->count = 0;
buffer->abscount=0;
buffer->pages = 1;
*is_null = 0;
*is_error = 0;
if (buffer->values != NULL)
{
free(buffer->values);
buffer->values=NULL;
}
buffer->values=(double *) malloc(BUFFERSIZE*sizeof(double));
}
void median_reset( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* is_error )
{
median_clear(initid, is_null, is_error);
median_add( initid, args, is_null, is_error );
}
void median_add( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* is_error )
{
if (args->args[0]!=NULL)
{
median_data *buffer = (median_data*)initid->ptr;
if (buffer->count>=BUFFERSIZE)
{
buffer->pages++;
buffer->count=0;
buffer->values=(double *) realloc(buffer->values,BUFFERSIZE*buffer->pages*sizeof(double));
}
buffer->values[buffer->abscount++] = *((double*)args->args[0]);
buffer->count++;
}
}
int
compare_doubles (const void *a, const void *b)
{
const double *da = (const double *) a;
const double *db = (const double *) b;
return (*da > *db) - (*da < *db);
}
double median( UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* is_error )
{
median_data* buffer = (median_data*)initid->ptr;
if (buffer->abscount==0 || *is_error!=0)
{
*is_null = 1;
return 0.0;
}
*is_null=0;
if (buffer->abscount==1)
{
return buffer->values[0];
}
qsort(buffer->values,buffer->abscount,sizeof(double),compare_doubles);
unsigned long bufindex = (ulong) ((((double)buffer->abscount - 1.0) * (double)buffer->percentile) / 100.0);
return buffer->values[bufindex];
}
/* #endif */