-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_mysqludf_stat.c
728 lines (669 loc) · 14 KB
/
lib_mysqludf_stat.c
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*
lib_mysqludf_stat - a library of mysql udfs with statistical functions
Copyright (C) 2007 Roland Bouman
web: http://www.xcdsql.org/MySQL/UDF/
email: [email protected]
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32)
#define DLLEXP __declspec(dllexport)
#else
#define DLLEXP
#endif
#ifdef STANDARD
#include <string.h>
#include <stdlib.h>
#include <time.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>
#include <stdlib.h>
#include <ctype.h>
#ifdef HAVE_DLOPEN
#define LIBVERSION "lib_mysqludf_stat version 0.0.3"
#ifdef __cplusplus
extern "C" {
#endif
/**
* lib_mysqludf_statistics_info
*/
DLLEXP
my_bool lib_mysqludf_stat_info_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
);
DLLEXP
void lib_mysqludf_stat_info_deinit(
UDF_INIT *initid
);
DLLEXP
char* lib_mysqludf_stat_info(
UDF_INIT *initid
, UDF_ARGS *args
, char* result
, unsigned long* length
, char *is_null
, char *error
);
/**
* ACCUMULATE_INT
*/
DLLEXP
my_bool stat_accum_int_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
);
DLLEXP
void stat_accum_int_deinit(
UDF_INIT *initid
);
DLLEXP
longlong stat_accum_int(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
);
/**
* ACCUMULATE_DOUBLE
*/
DLLEXP
my_bool stat_accum_double_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
);
DLLEXP
void stat_accum_double_deinit(
UDF_INIT *initid
);
DLLEXP
double stat_accum_double(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
);
/**
* PEARSON_SAMPLE
* (http://en.wikipedia.org/wiki/Correlation#Pearson.27s_product-moment_coefficient)
* sum_sq_x = 0
sum_sq_y = 0
sum_coproduct = 0
mean_x = x[1]
mean_y = y[1]
for i in 2 to N:
sweep = (i - 1.0) / i
delta_x = x[i] - mean_x
delta_y = y[i] - mean_y
sum_sq_x += delta_x * delta_x * sweep
sum_sq_y += delta_y * delta_y * sweep
sum_coproduct += delta_x * delta_y * sweep
mean_x += delta_x / i
mean_y += delta_y / i
pop_sd_x = sqrt( sum_sq_x / N )
pop_sd_y = sqrt( sum_sq_y / N )
cov_x_y = sum_coproduct / N
correlation = cov_x_y / (pop_sd_x * pop_sd_y)
*/
DLLEXP
my_bool stat_pmcc_samp_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
);
DLLEXP
void stat_pmcc_samp_deinit(
UDF_INIT *initid
);
DLLEXP
double stat_pmcc_samp(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
);
DLLEXP
char* stat_pmcc_samp_reset(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
);
DLLEXP
char* stat_pmcc_samp_clear(
UDF_INIT *initid
, char *is_null
, char *error
);
DLLEXP
char* stat_pmcc_samp_add(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
);
/**
* POINT_BISERIAL_SAMPLE
*/
DLLEXP
my_bool stat_ptbis_samp_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
);
DLLEXP
void stat_ptbis_samp_deinit(
UDF_INIT *initid
);
DLLEXP
double stat_ptbis_samp(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
);
DLLEXP
char* stat_ptbis_samp_reset(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
);
DLLEXP
char* stat_ptbis_samp_clear(
UDF_INIT *initid
, char *is_null
, char *error
);
DLLEXP
char* stat_ptbis_samp_add(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
);
/**
* int stat_abs_freq(value, series)
*
* calculate absolute frequencies
DLLEXP
my_bool stat_abs_freq_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
);
DLLEXP
void stat_abs_freq_deinit(
UDF_INIT *initid
);
DLLEXP
my_ulonglong stat_abs_freq(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
);
DLLEXP
char* stat_abs_freq_reset(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
);
DLLEXP
char* stat_abs_freq_clear(
UDF_INIT *initid
, char *is_null
, char *error
);
DLLEXP
char* stat_abs_freq_add(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
);
**/
#ifdef __cplusplus
}
#endif
/****************************************
* lib_mysqludf_stat_info
*
* string lib_mysqludf_stat_info()
*
* return version info
****************************************/
my_bool lib_mysqludf_stat_info_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
){
my_bool status;
if(args->arg_count!=0){
strcpy(
message
, "No arguments allowed (udf: lib_mysqludf_stat_info)"
);
status = 1;
} else {
status = 0;
}
return status;
}
void lib_mysqludf_stat_info_deinit(
UDF_INIT *initid
){
}
char* lib_mysqludf_stat_info(
UDF_INIT *initid
, UDF_ARGS *args
, char* result
, unsigned long* length
, char *is_null
, char *error
){
strcpy(result,LIBVERSION);
*length = strlen(LIBVERSION);
return result;
}
/**
* int stat_accum_int(i)
*
* return the i added to stat_accum_int(i-1)
*/
my_bool stat_accum_int_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
){
my_bool status;
if(args->arg_count!=1){
strcpy(
message
, "Expect exactly one argument (udf: accumulate_int)"
);
status = 1;
} else if(!(initid->ptr = malloc(sizeof(longlong)))){
initid->ptr = NULL;
strcpy(
message
, "Could not allocate memory (udf: accumulate_int)"
);
status = 1;
} else {
initid->maybe_null= 1;
args->arg_type[0] = INT_RESULT;
*((longlong *)initid->ptr) = 0;
status = 0;
}
return status;
}
void stat_accum_int_deinit(
UDF_INIT *initid
){
if(initid->ptr!=NULL){
free(initid->ptr);
}
}
longlong stat_accum_int(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
){
if(args->args[0]!=NULL){
*((longlong *)initid->ptr) += *((longlong *)args->args[0]);
}
return *((longlong *)initid->ptr);
}
/**
* double stat_accum_double(double)
*
*/
my_bool stat_accum_double_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
){
my_bool status;
if(args->arg_count!=1){
strcpy(
message
, "Expect exactly one argument (udf: stat_accum_double_init)"
);
status = 1;
} else if(!(initid->ptr = malloc(sizeof(double)))){
initid->ptr = NULL;
strcpy(
message
, "Could not allocate memory (udf: stat_accum_double_init)"
);
status = 1;
} else {
initid->maybe_null= 1;
args->arg_type[0] = REAL_RESULT;
*((double *)initid->ptr) = 0;
status = 0;
}
return status;
}
void stat_accum_double_deinit(
UDF_INIT *initid
){
if(initid->ptr!=NULL){
free(initid->ptr);
}
}
double stat_accum_double(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
){
if(args->args[0]!=NULL){
*((double *)initid->ptr) += *((double *)args->args[0]);
}
return *((double *)initid->ptr);
}
/**
* PEARSON_SAMPLE
*/
typedef struct st_pearson {
int unsigned n;
double sum_sq_x;
double sum_sq_y;
double sum_coproduct;
double mean_x;
double mean_y;
} PEARSON;
my_bool stat_pmcc_samp_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
){
my_bool status;
if(args->arg_count!=2){
strcpy(
message
, "Expect exactly two arguments (udf: pearson_sample)"
);
status = 1;
} else if(!(initid->ptr = malloc(sizeof(PEARSON)))){
initid->ptr = NULL;
strcpy(
message
, "Could not allocate memory (udf: pearson_sample)"
);
status = 1;
} else {
initid->maybe_null= 1;
initid->decimals= 4;
args->arg_type[0] = REAL_RESULT;
args->arg_type[1] = REAL_RESULT;
status = 0;
}
return status;
}
void stat_pmcc_samp_deinit(
UDF_INIT *initid
){
if(initid->ptr!=NULL){
free(initid->ptr);
}
}
char* stat_pmcc_samp_reset(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
){
stat_pmcc_samp_clear(
initid
, is_null
, error
);
stat_pmcc_samp_add(
initid
, args
, is_null
, error
);
return 0; //I hope this is what should be returned...
}
char* stat_pmcc_samp_clear(
UDF_INIT *initid
, char *is_null
, char *error
){
PEARSON* pearson_struct = (PEARSON *)initid->ptr;
pearson_struct->n = 0;
return 0;
}
char* stat_pmcc_samp_add(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
){
PEARSON* pearson_struct;
double x;
double y;
if( args->args[0]!=NULL
&& args->args[1]!=NULL
){
x = *((double *)args->args[0]);
y = *((double *)args->args[1]);
pearson_struct = (PEARSON *)initid->ptr;
if ((++pearson_struct->n)==1){
pearson_struct->sum_sq_x = 0;
pearson_struct->sum_sq_y = 0;
pearson_struct->sum_coproduct = 0;
pearson_struct->mean_x = x;
pearson_struct->mean_y = y;
} else {
double sweep = (pearson_struct->n - 1.0) / pearson_struct->n;
double delta_x = x - pearson_struct->mean_x;
double delta_y = y - pearson_struct->mean_y;
pearson_struct->sum_sq_x += delta_x * delta_x * sweep;
pearson_struct->sum_sq_y += delta_y * delta_y * sweep;
pearson_struct->sum_coproduct += delta_x * delta_y * sweep;
pearson_struct->mean_x += delta_x / pearson_struct->n;
pearson_struct->mean_y += delta_y / pearson_struct->n;
}
}
return 0;
}
double stat_pmcc_samp(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
){
PEARSON* pearson_struct = (PEARSON *)initid->ptr;
double result;
if (pearson_struct->n==0){
*is_null = 1;
} else if (
(result =
sqrt((double)pearson_struct->sum_sq_x / (double)pearson_struct->n)
* sqrt((double)pearson_struct->sum_sq_y / (double)pearson_struct->n)
)==0
){
*is_null = 1;
} else {
result =
((double)pearson_struct->sum_coproduct/(double)pearson_struct->n)
/ result
;
}
return result;
}
/**
* POINT_BISERIAL_SAMPLE
*/
typedef struct st_point_biserial_group{
int unsigned n;
double mean;
} POINT_BISERIAL_GROUP;
typedef struct st_point_biserial{
double sum;
POINT_BISERIAL_GROUP X;
POINT_BISERIAL_GROUP X0;
POINT_BISERIAL_GROUP X1;
} POINT_BISERIAL;
my_bool stat_ptbis_samp_init(
UDF_INIT *initid
, UDF_ARGS *args
, char *message
){
my_bool status;
if(args->arg_count!=2){
strcpy(
message
, "Expect exactly two arguments (udf: point_biserial_sample)"
);
status = 1;
} else if(!(initid->ptr = malloc(sizeof(POINT_BISERIAL)))){
initid->ptr = NULL;
strcpy(
message
, "Could not allocate memory (udf: point_biserial_sample)"
);
status = 1;
} else {
initid->maybe_null= 1;
initid->decimals= 4;
args->arg_type[0] = REAL_RESULT;
args->arg_type[1] = INT_RESULT;
status = 0;
}
return status;
}
void stat_ptbis_samp_deinit(
UDF_INIT *initid
){
if(initid->ptr!=NULL){
free(initid->ptr);
}
}
char* stat_ptbis_samp_reset(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
){
stat_ptbis_samp_clear(
initid
, is_null
, error
);
stat_ptbis_samp_add(
initid
, args
, is_null
, error
);
return 0;
}
char* stat_ptbis_samp_clear(
UDF_INIT *initid
, char *is_null
, char *error
){
POINT_BISERIAL* point_biserial_struct = (POINT_BISERIAL *)initid->ptr;
point_biserial_struct->sum =0;
point_biserial_struct->X.n =0;
point_biserial_struct->X.mean =0;
point_biserial_struct->X0.n = 0;
point_biserial_struct->X0.mean = 0;
point_biserial_struct->X1.n = 0;
point_biserial_struct->X1.mean = 0;
return 0;
}
char* stat_ptbis_samp_add(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
){
POINT_BISERIAL* point_biserial_struct;
POINT_BISERIAL_GROUP* point_biserial_group_struct;
double x;
double delta;
if( args->args[0]!=NULL
&& args->args[1]!=NULL
){
point_biserial_struct = (POINT_BISERIAL *)initid->ptr;
point_biserial_group_struct = (*((longlong*)args->args[1])==0)
? &(point_biserial_struct->X0)
: &(point_biserial_struct->X1)
;
x = *((double *)args->args[0]);
point_biserial_group_struct->n++;
delta = x - point_biserial_group_struct->mean;
point_biserial_group_struct->mean += delta/point_biserial_group_struct->n;
point_biserial_group_struct = &point_biserial_struct->X;
point_biserial_group_struct->n++;
delta = x - point_biserial_group_struct->mean;
point_biserial_group_struct->mean += delta/point_biserial_group_struct->n;
point_biserial_struct->sum += delta*(x - point_biserial_group_struct->mean);
}
return 0;
}
double stat_ptbis_samp(
UDF_INIT *initid
, UDF_ARGS *args
, char *is_null
, char *error
){
POINT_BISERIAL* point_biserial_struct;
POINT_BISERIAL_GROUP* X;
POINT_BISERIAL_GROUP* X0;
POINT_BISERIAL_GROUP* X1;
double s;
double result;
point_biserial_struct = (POINT_BISERIAL *)initid->ptr;
X = &point_biserial_struct->X;
if (X->n==1){
*is_null = 1;
} else if ((s = sqrt((double)point_biserial_struct->sum / (double)(X->n - 1)))==0){
*is_null = 1;
} else {
X0 = &point_biserial_struct->X0;
X1 = &point_biserial_struct->X1;
result =
((X1->mean - X0->mean) / s)
* sqrt(
(double)(X1->n * X0->n)
/ (double)(X->n * (X->n -1))
)
;
}
return result;
}
#endif /* HAVE_DLOPEN */