- Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathudf_reg_3_func.cc
309 lines (263 loc) · 10.2 KB
/
udf_reg_3_func.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* Copyright (c) 2017, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/* This test component register 3 UDFsin the init method (install) and
unregister them in deinit (uninstall). */
#include<mysql/components/component_implementation.h>
#include<mysql/components/service_implementation.h>
#include<mysql/components/services/udf_registration.h>
#include<sys/types.h>
#include<cctype>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
REQUIRES_SERVICE_PLACEHOLDER(udf_registration);
REQUIRES_SERVICE_PLACEHOLDER(udf_registration_aggregate);
/***************************************************************************
** UDF double function.
** Arguments:
** initid Structure filled by xxx_init
** args The same structure as to xxx_init. This structure
** contains values for all parameters.
** Note that the functions MUST check and convert all
** to the type it wants! Null values are represented by
** a NULL pointer
** is_null If the result is null, one should store 1 here.
** error If something goes fatally wrong one should store 1 here.
**
** This function should return the result.
***************************************************************************/
boolmyfunc_double_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
unsigned i;
if (!args->arg_count) {
strcpy(message, "myfunc_double must have at least one argument");
returntrue;
}
/*
** As this function wants to have everything as strings, force all arguments
** to strings.
*/
for (i = 0; i < args->arg_count; i++) args->arg_type[i] = STRING_RESULT;
initid->maybe_null = true; /* The result may be null */
initid->decimals = 2; /* We want 2 decimals in the result */
initid->max_length = 6; /* 3 digits + . + 2 decimals */
returnfalse;
}
doublemyfunc_double(UDF_INIT *, UDF_ARGS *args, unsignedchar *is_null,
unsignedchar *) {
unsignedlong val = 0;
unsignedlong v = 0;
unsigned i, j;
for (i = 0; i < args->arg_count; i++) {
if (args->args[i] == nullptr) continue;
val += args->lengths[i];
for (j = args->lengths[i]; j-- > 0;) v += args->args[i][j];
}
if (val) return (double)v / (double)val;
*is_null = 1;
return0.0;
}
/***************************************************************************
** UDF long long function.
** Arguments:
** initid Return value from xxxx_init
** args The same structure as to xxx_init. This structure
** contains values for all parameters.
** Note that the functions MUST check and convert all
** to the type it wants! Null values are represented by
** a NULL pointer
** is_null If the result is null, one should store 1 here.
** error If something goes fatally wrong one should store 1 here.
**
** This function should return the result as a long long
***************************************************************************/
/* This function returns the sum of all arguments */
longlongmyfunc_int(UDF_INIT *, UDF_ARGS *args, unsignedchar *,
unsignedchar *) {
longlong val = 0;
unsigned i;
for (i = 0; i < args->arg_count; i++) {
if (args->args[i] == nullptr) continue;
switch (args->arg_type[i]) {
case STRING_RESULT: /* Add string lengths */
val += args->lengths[i];
break;
case INT_RESULT: /* Add numbers */
val += *((longlong *)args->args[i]);
break;
case REAL_RESULT: /* Add numbers as long long */
val += (longlong)*((double *)args->args[i]);
break;
default:
break;
}
}
return val;
}
boolmyfunc_int_init(UDF_INIT *, UDF_ARGS *, char *) { returnfalse; }
/***************************************************************************
** Syntax for the new aggregate commands are:
** create aggregate function <function_name> returns {string|real|integer}
** soname <name_of_shared_library>
**
** Syntax for avgcost: avgcost( t.quantity, t.price )
** with t.quantity=integer, t.price=double
** (this example is provided by Andreas F. Bobak <bobak@relog.ch>)
****************************************************************************/
structavgcost_data {
unsignedlonglong count;
longlong totalquantity;
double totalprice;
};
/*
** Average Cost Aggregate Function.
*/
boolavgcost_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
structavgcost_data *data;
if (args->arg_count != 2) {
strcpy(message,
"wrong number of arguments: AVGCOST() requires two arguments");
returntrue;
}
if ((args->arg_type[0] != INT_RESULT) || (args->arg_type[1] != REAL_RESULT)) {
strcpy(message,
"wrong argument type: AVGCOST() requires an INT and a REAL");
returntrue;
}
/*
** force arguments to double.
*/
/*args->arg_type[0] = REAL_RESULT;
args->arg_type[1] = REAL_RESULT;*/
initid->maybe_null = false; /* The result may be null */
initid->decimals = 4; /* We want 4 decimals in the result */
initid->max_length = 20; /* 6 digits + . + 10 decimals */
if (!(data = new (std::nothrow) avgcost_data)) {
strcpy(message, "Couldn't allocate memory");
returntrue;
}
data->totalquantity = 0;
data->totalprice = 0.0;
initid->ptr = (char *)data;
returnfalse;
}
voidavgcost_deinit(UDF_INIT *initid) {
void *void_ptr = initid->ptr;
auto *data = static_cast<avgcost_data *>(void_ptr);
delete data;
}
/* This is needed to get things to work in MySQL 4.1.1 and above */
voidavgcost_clear(UDF_INIT *initid, unsignedchar *, unsignedchar *) {
auto *data = (structavgcost_data *)initid->ptr;
data->totalprice = 0.0;
data->totalquantity = 0;
data->count = 0;
}
voidavgcost_add(UDF_INIT *initid, UDF_ARGS *args, unsignedchar *,
unsignedchar *) {
if (args->args[0] && args->args[1]) {
auto *data = (structavgcost_data *)initid->ptr;
longlongconst quantity = *((longlong *)args->args[0]);
longlongconst newquantity = data->totalquantity + quantity;
double price = *((double *)args->args[1]);
data->count++;
if (((data->totalquantity >= 0) && (quantity < 0)) ||
((data->totalquantity < 0) && (quantity > 0))) {
/*
** passing from + to - or from - to +
*/
if (((quantity < 0) && (newquantity < 0)) ||
((quantity > 0) && (newquantity > 0))) {
data->totalprice = price * (double)newquantity;
}
/*
** sub q if totalq > 0
** add q if totalq < 0
*/
else {
price = data->totalprice / (double)data->totalquantity;
data->totalprice = price * (double)newquantity;
}
data->totalquantity = newquantity;
} else {
data->totalquantity += quantity;
data->totalprice += price * (double)quantity;
}
if (data->totalquantity == 0) data->totalprice = 0.0;
}
}
doubleavgcost(UDF_INIT *initid, UDF_ARGS *, unsignedchar *is_null,
unsignedchar *) {
auto *data = (structavgcost_data *)initid->ptr;
if (!data->count || !data->totalquantity) {
*is_null = 1;
return0.0;
}
*is_null = 0;
return data->totalprice / (double)data->totalquantity;
}
/**************************************************************************************/
staticmysql_service_status_tinit() {
bool ret_int = false;
ret_int = mysql_service_udf_registration->udf_register(
"myfunc_int", INT_RESULT, (Udf_func_any)myfunc_int, myfunc_int_init,
nullptr);
// myfunc_double_deinit);
bool ret_double = false;
ret_double = mysql_service_udf_registration->udf_register(
"myfunc_double", REAL_RESULT, (Udf_func_any)myfunc_double,
myfunc_double_init, nullptr);
bool ret_avgcost = false;
ret_avgcost = mysql_service_udf_registration_aggregate->udf_register(
"avgcost", REAL_RESULT, (Udf_func_any)avgcost, avgcost_init,
avgcost_deinit, avgcost_add, avgcost_clear);
return ret_int && ret_double && ret_avgcost;
}
staticmysql_service_status_tdeinit() {
int was_present = 0;
for (int i = 0; i < 10; i++) {
mysql_service_udf_registration->udf_unregister("myfunc_double",
&was_present);
if (was_present != 0) break;
}
was_present = 0;
for (int i = 0; i < 10; i++) {
mysql_service_udf_registration->udf_unregister("myfunc_int", &was_present);
if (was_present != 0) break;
}
was_present = 0;
for (int i = 0; i < 10; i++) {
mysql_service_udf_registration->udf_unregister("avgcost", &was_present);
if (was_present != 0) break;
}
returnfalse;
}
BEGIN_COMPONENT_PROVIDES(test_udf_registration)
END_COMPONENT_PROVIDES();
BEGIN_COMPONENT_REQUIRES(test_udf_registration)
REQUIRES_SERVICE(udf_registration),
REQUIRES_SERVICE(udf_registration_aggregate), END_COMPONENT_REQUIRES();
BEGIN_COMPONENT_METADATA(test_udf_registration)
METADATA("mysql.author", "Oracle Corporation"),
METADATA("mysql.license", "GPL"), METADATA("test_property", "1"),
END_COMPONENT_METADATA();
DECLARE_COMPONENT(test_udf_registration, "mysql:test_udf_registration")
init, deinit END_DECLARE_COMPONENT();
DECLARE_LIBRARY_COMPONENTS &COMPONENT_REF(test_udf_registration)
END_DECLARE_LIBRARY_COMPONENTS