Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ Aggregate Functions

Get the median of a set
```
double median(double n);
double med(double n);

mysql> SELECT median(weight) from t1;
mysql> SELECT med(weight) from t1;
```


Expand Down Expand Up @@ -129,18 +129,18 @@ mysql> SELECT lessavg(double m) from t1;
Calculate continuous percentile. Returns the value at a relative position
specified by the fraction, interpolating between input values if needed.
```
double percentile_cont(double x, double fraction);
double percentile_contin(double x, double fraction);

mysql> SELECT percentile_cont(x, 0.5) from t1;
mysql> SELECT percentile_contin(x, 0.5) from t1;
```


Calculate discrete percentile. Returns the first input value whose relative
position is greater than or equal to the specified fraction.
```
double percentile_disc(double x, double fraction);
double percentile_discr(double x, double fraction);

mysql> SELECT percentile_disc(x, 0.5) from t1;
mysql> SELECT percentile_discr(x, 0.5) from t1;
```


Expand Down
6 changes: 3 additions & 3 deletions load.sql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ if_enable "kurtosis" && create_agg_function "kurtosis" "real"
if_enable "lessavg" && create_agg_function "lessavg" "integer"
if_enable "lesspartpct" && create_agg_function "lesspartpct" "integer"
if_enable "lesspart" && create_agg_function "lesspart" "integer"
if_enable "median" && create_agg_function "median" "real"
if_enable "med" && create_agg_function "med" "real"
if_enable "stats_mode" && create_agg_function "stats_mode" "real"
if_enable "ngram" && create_function "ngram" "string"
if_enable "noverk" && create_function "noverk" "integer"
if_enable "percentile_cont" && create_agg_function "percentile_cont" "real"
if_enable "percentile_disc" && create_agg_function "percentile_disc" "real"
if_enable "percentile_contin" && create_agg_function "percentile_contin" "real"
if_enable "percentile_discr" && create_agg_function "percentile_discr" "real"
if_enable "rotbit" && create_function "rotbit" "integer"
if_enable "rotint" && create_function "rotint" "integer"
if_enable "rsumd" && create_function "rsumd" "real"
Expand Down
12 changes: 6 additions & 6 deletions load.win.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ DROP FUNCTION IF EXISTS kurtosis;
DROP FUNCTION IF EXISTS lessavg;
DROP FUNCTION IF EXISTS lesspartpct;
DROP FUNCTION IF EXISTS lesspart;
DROP FUNCTION IF EXISTS median;
DROP FUNCTION IF EXISTS med;
DROP FUNCTION IF EXISTS stats_mode;
DROP FUNCTION IF EXISTS ngram;
DROP FUNCTION IF EXISTS noverk;
DROP FUNCTION IF EXISTS percentile_cont;
DROP FUNCTION IF EXISTS percentile_disc;
DROP FUNCTION IF EXISTS percentile_contin;
DROP FUNCTION IF EXISTS percentile_discr;
DROP FUNCTION IF EXISTS rotbit;
DROP FUNCTION IF EXISTS rotint;
DROP FUNCTION IF EXISTS rsumd;
Expand All @@ -46,12 +46,12 @@ CREATE AGGREGATE FUNCTION kurtosis RETURNS real SONAME 'udf_infusion.dll';
CREATE AGGREGATE FUNCTION lessavg RETURNS integer SONAME 'udf_infusion.dll';
CREATE AGGREGATE FUNCTION lesspartpct RETURNS integer SONAME 'udf_infusion.dll';
CREATE AGGREGATE FUNCTION lesspart RETURNS integer SONAME 'udf_infusion.dll';
CREATE AGGREGATE FUNCTION median RETURNS real SONAME 'udf_infusion.dll';
CREATE AGGREGATE FUNCTION med RETURNS real SONAME 'udf_infusion.dll';
CREATE AGGREGATE FUNCTION stats_mode RETURNS real SONAME 'udf_infusion.dll';
CREATE FUNCTION ngram RETURNS string SONAME 'udf_infusion.dll';
CREATE FUNCTION noverk RETURNS integer SONAME 'udf_infusion.dll';
CREATE AGGREGATE FUNCTION percentile_cont RETURNS real SONAME 'udf_infusion.dll';
CREATE AGGREGATE FUNCTION percentile_disc RETURNS real SONAME 'udf_infusion.dll';
CREATE AGGREGATE FUNCTION percentile_contin RETURNS real SONAME 'udf_infusion.dll';
CREATE AGGREGATE FUNCTION percentile_discr RETURNS real SONAME 'udf_infusion.dll';
CREATE FUNCTION rotbit RETURNS integer SONAME 'udf_infusion.dll';
CREATE FUNCTION rotint RETURNS integer SONAME 'udf_infusion.dll';
CREATE FUNCTION rsumd RETURNS real SONAME 'udf_infusion.dll';
Expand Down
12 changes: 6 additions & 6 deletions src/median.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ struct Buffer {
struct array values;
};

DLLEXPORT my_bool median_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
DLLEXPORT my_bool med_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
struct Buffer *data;

if (1 != args->arg_count) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"median must have exactly one argument");
"med must have exactly one argument");
return 1;
}

Expand All @@ -34,12 +34,12 @@ DLLEXPORT my_bool median_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
return 0;
}

DLLEXPORT void median_clear(UDF_INIT* initid, char* is_null, char *error) {
DLLEXPORT void med_clear(UDF_INIT* initid, char* is_null, char *error) {
struct Buffer *data = (struct Buffer *) initid->ptr;
array_truncate(&data->values);
}

DLLEXPORT void median_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error) {
DLLEXPORT void med_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error) {
struct Buffer *data = (struct Buffer *) initid->ptr;

if (NULL == args->args[0])
Expand All @@ -51,7 +51,7 @@ DLLEXPORT void median_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char
}
}

DLLEXPORT void median_deinit(UDF_INIT *initid) {
DLLEXPORT void med_deinit(UDF_INIT *initid) {
struct Buffer *data = (struct Buffer *) initid->ptr;
if (NULL != data) {
array_free(&data->values);
Expand All @@ -60,7 +60,7 @@ DLLEXPORT void median_deinit(UDF_INIT *initid) {
}
}

DLLEXPORT double median(UDF_INIT *initid, UDF_ARGS *args,
DLLEXPORT double med(UDF_INIT *initid, UDF_ARGS *args,
char *is_null,
char *error __attribute__((unused))) {
struct Buffer *data = (struct Buffer *) initid->ptr;
Expand Down
12 changes: 6 additions & 6 deletions src/percentile_cont.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ struct Buffer {
double percentile;
};

DLLEXPORT my_bool percentile_cont_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
DLLEXPORT my_bool percentile_contin_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
struct Buffer *data;

if (2 != args->arg_count) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"percentile_cont must have exactly two arguments");
"percentile_contin must have exactly two arguments");
return 1;
}

Expand All @@ -36,12 +36,12 @@ DLLEXPORT my_bool percentile_cont_init(UDF_INIT *initid, UDF_ARGS *args, char *m
return 0;
}

DLLEXPORT void percentile_cont_clear(UDF_INIT* initid, char* is_null, char *error) {
DLLEXPORT void percentile_contin_clear(UDF_INIT* initid, char* is_null, char *error) {
struct Buffer *data = (struct Buffer *) initid->ptr;
array_truncate(&data->values);
}

DLLEXPORT void percentile_cont_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error) {
DLLEXPORT void percentile_contin_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error) {
struct Buffer *data = (struct Buffer *) initid->ptr;

double percentile = *((double *) args->args[1]);
Expand All @@ -60,7 +60,7 @@ DLLEXPORT void percentile_cont_add(UDF_INIT* initid, UDF_ARGS* args, char* is_nu
}
}

DLLEXPORT void percentile_cont_deinit(UDF_INIT *initid) {
DLLEXPORT void percentile_contin_deinit(UDF_INIT *initid) {
struct Buffer *data = (struct Buffer *) initid->ptr;
if (NULL != data) {
array_free(&data->values);
Expand All @@ -69,7 +69,7 @@ DLLEXPORT void percentile_cont_deinit(UDF_INIT *initid) {
}
}

DLLEXPORT double percentile_cont(UDF_INIT *initid, UDF_ARGS *args,
DLLEXPORT double percentile_contin(UDF_INIT *initid, UDF_ARGS *args,
char *is_null,
char *error __attribute__((unused))) {
struct Buffer *data = (struct Buffer *) initid->ptr;
Expand Down
12 changes: 6 additions & 6 deletions src/percentile_disc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ struct Buffer {
double percentile;
};

DLLEXPORT my_bool percentile_disc_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
DLLEXPORT my_bool percentile_discr_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
struct Buffer *data;

if (2 != args->arg_count) {
snprintf(message, MYSQL_ERRMSG_SIZE,
"percentile_disc must have exactly two arguments");
"percentile_discr must have exactly two arguments");
return 1;
}

Expand All @@ -36,12 +36,12 @@ DLLEXPORT my_bool percentile_disc_init(UDF_INIT *initid, UDF_ARGS *args, char *m
return 0;
}

DLLEXPORT void percentile_disc_clear(UDF_INIT* initid, char* is_null, char *error) {
DLLEXPORT void percentile_discr_clear(UDF_INIT* initid, char* is_null, char *error) {
struct Buffer *data = (struct Buffer *) initid->ptr;
array_truncate(&data->values);
}

DLLEXPORT void percentile_disc_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error) {
DLLEXPORT void percentile_discr_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error) {
struct Buffer *data = (struct Buffer *) initid->ptr;

double percentile = *((double *) args->args[1]);
Expand All @@ -60,7 +60,7 @@ DLLEXPORT void percentile_disc_add(UDF_INIT* initid, UDF_ARGS* args, char* is_nu
}
}

DLLEXPORT void percentile_disc_deinit(UDF_INIT *initid) {
DLLEXPORT void percentile_discr_deinit(UDF_INIT *initid) {
struct Buffer *data = (struct Buffer *) initid->ptr;
if (NULL != data) {
array_free(&data->values);
Expand All @@ -69,7 +69,7 @@ DLLEXPORT void percentile_disc_deinit(UDF_INIT *initid) {
}
}

DLLEXPORT double percentile_disc(UDF_INIT *initid, UDF_ARGS *args,
DLLEXPORT double percentile_discr(UDF_INIT *initid, UDF_ARGS *args,
char *is_null,
char *error __attribute__((unused))) {
struct Buffer *data = (struct Buffer *) initid->ptr;
Expand Down
44 changes: 22 additions & 22 deletions test/median.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,42 @@ disable_abort_on_error;

use udf_infusion_test;

select median();
# ERROR HY000: Can't initialize function 'median'; median must have exactly one argument
select med();
# ERROR HY000: Can't initialize function 'med'; med must have exactly one argument

select median(1, 2);
# ERROR HY000: Can't initialize function 'median'; median must have exactly one argument
select med(1, 2);
# ERROR HY000: Can't initialize function 'med'; med must have exactly one argument

select median(NULL);
# median(NULL)
select med(NULL);
# med(NULL)
# NULL

select median(x) from empty_table;
# median(x)
select med(x) from empty_table;
# med(x)
# NULL

select median(x) from null_table;
# median(x)
select med(x) from null_table;
# med(x)
# NULL

select median(x) from small_table;
# median(x)
select med(x) from small_table;
# med(x)
# 4.5

select median(x) from (select x from small_table where x is not null order by x limit 7) as t;
# median(x)
select med(x) from (select x from small_table where x is not null order by x limit 7) as t;
# med(x)
# 4

select g, median(x) from small_table group by g;
# g median(x)
select g, med(x) from small_table group by g;
# g med(x)
# 1 1.5
# 2 4
# 3 7

select round(median(x), 12) as `median(x)` from example_table;
# median(x)
# >>> print '%.12f' % ma.median(example_table()[:,0])
select round(med(x), 12) as `med(x)` from example_table;
# med(x)
# >>> print '%.12f' % ma.med(example_table()[:,0])

select round(median(y), 12) as `median(y)` from example_table;
# median(y)
# >>> print '%.12f' % ma.median(example_table()[:,1])
select round(med(y), 12) as `med(y)` from example_table;
# med(y)
# >>> print '%.12f' % ma.med(example_table()[:,1])
56 changes: 28 additions & 28 deletions test/percentile_cont.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,58 @@ disable_abort_on_error;

use udf_infusion_test;

select percentile_cont();
# ERROR HY000: Can't initialize function 'percentile_cont'; percentile_cont must have exactly two arguments
select percentile_contin();
# ERROR HY000: Can't initialize function 'percentile_contin'; percentile_contin must have exactly two arguments

select percentile_cont(1, 2, 3);
# ERROR HY000: Can't initialize function 'percentile_cont'; percentile_cont must have exactly two arguments
select percentile_contin(1, 2, 3);
# ERROR HY000: Can't initialize function 'percentile_contin'; percentile_contin must have exactly two arguments

select percentile_cont(NULL, 0.5);
# percentile_cont(NULL, 0.5)
select percentile_contin(NULL, 0.5);
# percentile_contin(NULL, 0.5)
# NULL

select percentile_cont(x, 0.5) from empty_table;
# percentile_cont(x, 0.5)
select percentile_contin(x, 0.5) from empty_table;
# percentile_contin(x, 0.5)
# NULL

select percentile_cont(x, 0.5) from null_table;
# percentile_cont(x, 0.5)
select percentile_contin(x, 0.5) from null_table;
# percentile_contin(x, 0.5)
# NULL

select percentile_cont(x, 0.5) from small_table;
# percentile_cont(x, 0.5)
select percentile_contin(x, 0.5) from small_table;
# percentile_contin(x, 0.5)
# 4.5

select percentile_cont(x, 0) from small_table;
# percentile_cont(x, 0)
select percentile_contin(x, 0) from small_table;
# percentile_contin(x, 0)
# 1

select percentile_cont(x, 1) from small_table;
# percentile_cont(x, 1)
select percentile_contin(x, 1) from small_table;
# percentile_contin(x, 1)
# 8

select percentile_cont(x, 0.001) from small_table;
# percentile_cont(x, 0.001)
select percentile_contin(x, 0.001) from small_table;
# percentile_contin(x, 0.001)
# 1.007

select percentile_cont(x, 0.999) from small_table;
# percentile_cont(x, 0.999)
select percentile_contin(x, 0.999) from small_table;
# percentile_contin(x, 0.999)
# 7.993

select percentile_cont(x, 0.6) from small_table;
# percentile_cont(x, 0.6)
select percentile_contin(x, 0.6) from small_table;
# percentile_contin(x, 0.6)
# 5.2

select g, percentile_cont(x, 0.7) from small_table group by g;
# g percentile_cont(x, 0.7)
select g, percentile_contin(x, 0.7) from small_table group by g;
# g percentile_contin(x, 0.7)
# 1 1.7
# 2 4.4
# 3 7.4

select round(percentile_cont(x, 0.7), 12) as `percentile_cont(x, 0.7)` from example_table;
# percentile_cont(x, 0.7)
select round(percentile_contin(x, 0.7), 12) as `percentile_contin(x, 0.7)` from example_table;
# percentile_contin(x, 0.7)
# >>> print '%.12f' % np.percentile(example_table()[:,0].compressed(), 70)

select round(percentile_cont(y, 0.7), 12) as `percentile_cont(y, 0.7)` from example_table;
# percentile_cont(y, 0.7)
select round(percentile_contin(y, 0.7), 12) as `percentile_contin(y, 0.7)` from example_table;
# percentile_contin(y, 0.7)
# >>> print '%.12f' % np.percentile(example_table()[:,1].compressed(), 70)
Loading