You've already forked mariadb-columnstore-engine
mirror of
https://github.com/mariadb-corporation/mariadb-columnstore-engine.git
synced 2025-07-02 17:22:27 +03:00
MCOL-521 Some more fixes for multi-parm aggregates. Add regr slope
This commit is contained in:
@ -199,7 +199,7 @@ extern "C"
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
long long regr_avgx(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
|
||||
double regr_avgx(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
|
||||
char* is_null, char* error __attribute__((unused)))
|
||||
{
|
||||
struct regr_avgx_data* data = (struct regr_avgx_data*)initid->ptr;
|
||||
@ -283,7 +283,7 @@ extern "C"
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
long long regr_avgy(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
|
||||
double regr_avgy(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
|
||||
char* is_null, char* error __attribute__((unused)))
|
||||
{
|
||||
struct regr_avgy_data* data = (struct regr_avgy_data*)initid->ptr;
|
||||
@ -368,7 +368,225 @@ extern "C"
|
||||
struct regr_count_data* data = (struct regr_count_data*)initid->ptr;
|
||||
return data->cnt;
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
|
||||
/**
|
||||
* regr_slope connector stub
|
||||
*/
|
||||
struct regr_slope_data
|
||||
{
|
||||
int64_t cnt;
|
||||
double sumx;
|
||||
double sumx2; // sum of (x squared)
|
||||
double sumy;
|
||||
double sumxy; // sum of (x*y)
|
||||
};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
my_bool regr_slope_init(UDF_INIT* initid, UDF_ARGS* args, char* message)
|
||||
{
|
||||
struct regr_slope_data* data;
|
||||
if (args->arg_count != 2)
|
||||
{
|
||||
strcpy(message,"regr_slope() requires two arguments");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!(data = (struct regr_slope_data*) malloc(sizeof(struct regr_slope_data))))
|
||||
{
|
||||
strmov(message,"Couldn't allocate memory");
|
||||
return 1;
|
||||
}
|
||||
data->cnt = 0;
|
||||
data->sumx = 0.0;
|
||||
data->sumx2 = 0.0;
|
||||
data->sumy = 0.0;
|
||||
|
||||
initid->ptr = (char*)data;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void regr_slope_deinit(UDF_INIT* initid)
|
||||
{
|
||||
free(initid->ptr);
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void
|
||||
regr_slope_clear(UDF_INIT* initid, char* is_null __attribute__((unused)),
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
struct regr_slope_data* data = (struct regr_slope_data*)initid->ptr;
|
||||
data->cnt = 0;
|
||||
data->sumx = 0.0;
|
||||
data->sumx2 = 0.0;
|
||||
data->sumy = 0.0;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void
|
||||
regr_slope_add(UDF_INIT* initid, UDF_ARGS* args,
|
||||
char* is_null,
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
// Test for NULL in x and y
|
||||
if (args->args[0] == 0 || args->args[1] == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
struct regr_slope_data* data = (struct regr_slope_data*)initid->ptr;
|
||||
double yval = cvtArgToDouble(args->arg_type[0], args->args[0]);
|
||||
double xval = cvtArgToDouble(args->arg_type[1], args->args[1]);
|
||||
data->sumy += yval;
|
||||
data->sumx += xval;
|
||||
data->sumx2 += xval*xval;
|
||||
data->sumxy += xval*yval;
|
||||
++data->cnt;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
double regr_slope(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
|
||||
char* is_null, char* error __attribute__((unused)))
|
||||
{
|
||||
struct regr_slope_data* data = (struct regr_slope_data*)initid->ptr;
|
||||
double N = data->cnt;
|
||||
if (N > 0)
|
||||
{
|
||||
double sumx = data->sumx;
|
||||
double sumy = data->sumy;
|
||||
double sumx2 = data->sumx2;
|
||||
double sumxy = data->sumxy;
|
||||
double variance = (N * sumx2) - (sumx * sumx);
|
||||
if (variance)
|
||||
{
|
||||
return ((N * sumxy) - (sumx * sumy)) / variance;
|
||||
}
|
||||
}
|
||||
*is_null = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
|
||||
/**
|
||||
* regr_intercept connector stub
|
||||
*/
|
||||
struct regr_intercept_data
|
||||
{
|
||||
int64_t cnt;
|
||||
double sumx;
|
||||
double sumx2; // sum of (x squared)
|
||||
double sumy;
|
||||
double sumxy; // sum of (x*y)
|
||||
};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
my_bool regr_intercept_init(UDF_INIT* initid, UDF_ARGS* args, char* message)
|
||||
{
|
||||
struct regr_intercept_data* data;
|
||||
if (args->arg_count != 2)
|
||||
{
|
||||
strcpy(message,"regr_intercept() requires two arguments");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!(data = (struct regr_intercept_data*) malloc(sizeof(struct regr_intercept_data))))
|
||||
{
|
||||
strmov(message,"Couldn't allocate memory");
|
||||
return 1;
|
||||
}
|
||||
data->cnt = 0;
|
||||
data->sumx = 0.0;
|
||||
data->sumx2 = 0.0;
|
||||
data->sumy = 0.0;
|
||||
|
||||
initid->ptr = (char*)data;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void regr_intercept_deinit(UDF_INIT* initid)
|
||||
{
|
||||
free(initid->ptr);
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void
|
||||
regr_intercept_clear(UDF_INIT* initid, char* is_null __attribute__((unused)),
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
struct regr_intercept_data* data = (struct regr_intercept_data*)initid->ptr;
|
||||
data->cnt = 0;
|
||||
data->sumx = 0.0;
|
||||
data->sumx2 = 0.0;
|
||||
data->sumy = 0.0;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void
|
||||
regr_intercept_add(UDF_INIT* initid, UDF_ARGS* args,
|
||||
char* is_null,
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
// Test for NULL in x and y
|
||||
if (args->args[0] == 0 || args->args[1] == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
struct regr_intercept_data* data = (struct regr_intercept_data*)initid->ptr;
|
||||
double yval = cvtArgToDouble(args->arg_type[0], args->args[0]);
|
||||
double xval = cvtArgToDouble(args->arg_type[1], args->args[1]);
|
||||
data->sumy += yval;
|
||||
data->sumx += xval;
|
||||
data->sumx2 += xval*xval;
|
||||
data->sumxy += xval*yval;
|
||||
++data->cnt;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
double regr_intercept(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
|
||||
char* is_null, char* error __attribute__((unused)))
|
||||
{
|
||||
struct regr_intercept_data* data = (struct regr_intercept_data*)initid->ptr;
|
||||
double N = data->cnt;
|
||||
if (N > 0)
|
||||
{
|
||||
double sumx = data->sumx;
|
||||
double sumy = data->sumy;
|
||||
double sumx2 = data->sumx2;
|
||||
double sumxy = data->sumxy;
|
||||
double variance = (N * sumx2) - (sumx * sumx);
|
||||
if (variance)
|
||||
{
|
||||
double slope = ((N * sumxy) - (sumx * sumy)) / variance;
|
||||
return (sumy - (slope * sumx)) / N;
|
||||
}
|
||||
}
|
||||
*is_null = 1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// vim:ts=4 sw=4:
|
||||
|
||||
|
Reference in New Issue
Block a user