You've already forked mariadb-columnstore-engine
mirror of
https://github.com/mariadb-corporation/mariadb-columnstore-engine.git
synced 2025-08-01 06:46:55 +03:00
Reformat all code to coding standard
This commit is contained in:
78
utils/udfsdk/allnull.cpp
Executable file → Normal file
78
utils/udfsdk/allnull.cpp
Executable file → Normal file
@ -29,59 +29,63 @@ struct allnull_data
|
||||
mcsv1_UDAF::ReturnCode allnull::init(mcsv1Context* context,
|
||||
COL_TYPES& colTypes)
|
||||
{
|
||||
context->setUserDataSize(sizeof(allnull_data));
|
||||
if (colTypes.size() < 1)
|
||||
{
|
||||
// The error message will be prepended with
|
||||
// "The storage engine for the table doesn't support "
|
||||
context->setErrorMessage("allnull() with 0 arguments");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
context->setResultType(CalpontSystemCatalog::TINYINT);
|
||||
context->setUserDataSize(sizeof(allnull_data));
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
if (colTypes.size() < 1)
|
||||
{
|
||||
// The error message will be prepended with
|
||||
// "The storage engine for the table doesn't support "
|
||||
context->setErrorMessage("allnull() with 0 arguments");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
|
||||
context->setResultType(CalpontSystemCatalog::TINYINT);
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode allnull::reset(mcsv1Context* context)
|
||||
{
|
||||
struct allnull_data* data = (struct allnull_data*)context->getUserData()->data;
|
||||
data->totalQuantity = 0;
|
||||
data->totalNulls = 0;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
struct allnull_data* data = (struct allnull_data*)context->getUserData()->data;
|
||||
data->totalQuantity = 0;
|
||||
data->totalNulls = 0;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode allnull::nextValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsIn)
|
||||
mcsv1_UDAF::ReturnCode allnull::nextValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsIn)
|
||||
{
|
||||
struct allnull_data* data = (struct allnull_data*)context->getUserData()->data;
|
||||
|
||||
for (size_t i = 0; i < context->getParameterCount(); i++)
|
||||
{
|
||||
data->totalQuantity++;
|
||||
if (context->isParamNull(0))
|
||||
{
|
||||
data->totalNulls++;
|
||||
}
|
||||
}
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
struct allnull_data* data = (struct allnull_data*)context->getUserData()->data;
|
||||
|
||||
for (size_t i = 0; i < context->getParameterCount(); i++)
|
||||
{
|
||||
data->totalQuantity++;
|
||||
|
||||
if (context->isParamNull(0))
|
||||
{
|
||||
data->totalNulls++;
|
||||
}
|
||||
}
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode allnull::subEvaluate(mcsv1Context* context, const UserData* userDataIn)
|
||||
{
|
||||
struct allnull_data* outData = (struct allnull_data*)context->getUserData()->data;
|
||||
struct allnull_data* inData = (struct allnull_data*)userDataIn->data;
|
||||
outData->totalQuantity += inData->totalQuantity;
|
||||
outData->totalNulls += inData->totalNulls;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
struct allnull_data* outData = (struct allnull_data*)context->getUserData()->data;
|
||||
struct allnull_data* inData = (struct allnull_data*)userDataIn->data;
|
||||
outData->totalQuantity += inData->totalQuantity;
|
||||
outData->totalNulls += inData->totalNulls;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode allnull::evaluate(mcsv1Context* context, static_any::any& valOut)
|
||||
{
|
||||
OUT_TYPE allNull;
|
||||
struct allnull_data* data = (struct allnull_data*)context->getUserData()->data;
|
||||
allNull = data->totalQuantity > 0 && data->totalNulls == data->totalQuantity;
|
||||
valOut = allNull;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
OUT_TYPE allNull;
|
||||
struct allnull_data* data = (struct allnull_data*)context->getUserData()->data;
|
||||
allNull = data->totalQuantity > 0 && data->totalNulls == data->totalQuantity;
|
||||
valOut = allNull;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
|
242
utils/udfsdk/allnull.h
Executable file → Normal file
242
utils/udfsdk/allnull.h
Executable file → Normal file
@ -21,26 +21,26 @@
|
||||
* mcsv1_UDAF.h
|
||||
***********************************************************************/
|
||||
|
||||
/**
|
||||
* Columnstore interface for writing a User Defined Aggregate
|
||||
* Functions (UDAF) and User Defined Analytic Functions (UDAnF)
|
||||
* or a function that can act as either - UDA(n)F
|
||||
*
|
||||
/**
|
||||
* Columnstore interface for writing a User Defined Aggregate
|
||||
* Functions (UDAF) and User Defined Analytic Functions (UDAnF)
|
||||
* or a function that can act as either - UDA(n)F
|
||||
*
|
||||
* The basic steps are:
|
||||
*
|
||||
* 1. Create a the UDA(n)F function interface in some .h file.
|
||||
* 2. Create the UDF function implementation in some .cpp file
|
||||
* 3. Create the connector stub (MariaDB UDAF definition) for
|
||||
* this UDF function.
|
||||
* 4. build the dynamic library using all of the source.
|
||||
* 5 Put the library in $COLUMNSTORE_INSTALL/lib of
|
||||
* all modules
|
||||
* 6. restart the Columnstore system.
|
||||
* 1. Create a the UDA(n)F function interface in some .h file.
|
||||
* 2. Create the UDF function implementation in some .cpp file
|
||||
* 3. Create the connector stub (MariaDB UDAF definition) for
|
||||
* this UDF function.
|
||||
* 4. build the dynamic library using all of the source.
|
||||
* 5 Put the library in $COLUMNSTORE_INSTALL/lib of
|
||||
* all modules
|
||||
* 6. restart the Columnstore system.
|
||||
* 7. notify mysqld about the new functions with commands like:
|
||||
*
|
||||
*
|
||||
* CREATE AGGREGATE FUNCTION allnull returns BOOL soname
|
||||
* 'libudf_mysql.so';
|
||||
*
|
||||
*
|
||||
*/
|
||||
#ifndef HEADER_allnull
|
||||
#define HEADER_allnull
|
||||
@ -69,124 +69,124 @@ using namespace execplan;
|
||||
namespace mcsv1sdk
|
||||
{
|
||||
|
||||
// Override mcsv1_UDAF to build your User Defined Aggregate (UDAF) and/or
|
||||
// Override mcsv1_UDAF to build your User Defined Aggregate (UDAF) and/or
|
||||
// User Defined Analytic Function (UDAnF).
|
||||
// These will be singleton classes, so don't put any instance
|
||||
// specific data in here. All instance data is stored in mcsv1Context
|
||||
// passed to each user function and retrieved by the getUserData() method.
|
||||
//
|
||||
// Each API function returns a ReturnCode. If ERROR is returned at any time,
|
||||
// the query is aborted, getInterrupted() will begin to return true and the
|
||||
// message set in config->setErrorMessage() is returned to MariaDB.
|
||||
//
|
||||
// Each API function returns a ReturnCode. If ERROR is returned at any time,
|
||||
// the query is aborted, getInterrupted() will begin to return true and the
|
||||
// message set in config->setErrorMessage() is returned to MariaDB.
|
||||
class allnull : public mcsv1_UDAF
|
||||
{
|
||||
public:
|
||||
// Defaults OK
|
||||
allnull() : mcsv1_UDAF(){};
|
||||
virtual ~allnull(){};
|
||||
// Defaults OK
|
||||
allnull() : mcsv1_UDAF() {};
|
||||
virtual ~allnull() {};
|
||||
|
||||
/**
|
||||
* init()
|
||||
*
|
||||
* Mandatory. Implement this to initialize flags and instance
|
||||
* data. Called once per SQL statement. You can do any sanity
|
||||
* checks here.
|
||||
*
|
||||
* colTypes (in) - A vector of ColDataType defining the
|
||||
* parameters of the UDA(n)F call. These can be used to decide
|
||||
* to override the default return type. If desired, the new
|
||||
* return type can be set by context->setReturnType() and
|
||||
* decimal precision can be set in context->
|
||||
* setResultDecimalCharacteristics.
|
||||
*
|
||||
* Return mcsv1_UDAF::ERROR on any error, such as non-compatible
|
||||
* colTypes or wrong number of arguments. Else return
|
||||
* mcsv1_UDAF::SUCCESS.
|
||||
*/
|
||||
virtual ReturnCode init(mcsv1Context* context, COL_TYPES& colTypes);
|
||||
/**
|
||||
* init()
|
||||
*
|
||||
* Mandatory. Implement this to initialize flags and instance
|
||||
* data. Called once per SQL statement. You can do any sanity
|
||||
* checks here.
|
||||
*
|
||||
* colTypes (in) - A vector of ColDataType defining the
|
||||
* parameters of the UDA(n)F call. These can be used to decide
|
||||
* to override the default return type. If desired, the new
|
||||
* return type can be set by context->setReturnType() and
|
||||
* decimal precision can be set in context->
|
||||
* setResultDecimalCharacteristics.
|
||||
*
|
||||
* Return mcsv1_UDAF::ERROR on any error, such as non-compatible
|
||||
* colTypes or wrong number of arguments. Else return
|
||||
* mcsv1_UDAF::SUCCESS.
|
||||
*/
|
||||
virtual ReturnCode init(mcsv1Context* context, COL_TYPES& colTypes);
|
||||
|
||||
/**
|
||||
* reset()
|
||||
*
|
||||
* Mandatory. Reset the UDA(n)F for a new group, partition or,
|
||||
* in some cases, new Window Frame. Do not free any memory
|
||||
* allocated by context->setUserDataSize(). The SDK Framework owns
|
||||
* that memory and will handle that. Use this opportunity to
|
||||
* reset any variables in context->getUserData() needed for the
|
||||
* next aggregation. May be called multiple times if running in
|
||||
* a ditributed fashion.
|
||||
*
|
||||
* Use this opportunity to initialize the userData.
|
||||
*/
|
||||
virtual ReturnCode reset(mcsv1Context* context);
|
||||
/**
|
||||
* reset()
|
||||
*
|
||||
* Mandatory. Reset the UDA(n)F for a new group, partition or,
|
||||
* in some cases, new Window Frame. Do not free any memory
|
||||
* allocated by context->setUserDataSize(). The SDK Framework owns
|
||||
* that memory and will handle that. Use this opportunity to
|
||||
* reset any variables in context->getUserData() needed for the
|
||||
* next aggregation. May be called multiple times if running in
|
||||
* a ditributed fashion.
|
||||
*
|
||||
* Use this opportunity to initialize the userData.
|
||||
*/
|
||||
virtual ReturnCode reset(mcsv1Context* context);
|
||||
|
||||
/**
|
||||
* nextValue()
|
||||
*
|
||||
* Mandatory. Handle a single row.
|
||||
*
|
||||
* colsIn - A vector of data structure describing the input
|
||||
* data.
|
||||
*
|
||||
* This function is called once for every row in the filtered
|
||||
* result set (before aggregation). It is very important that
|
||||
* this function is efficient.
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, nextValue
|
||||
* cannot depend on order, as it will only be called for each
|
||||
* row found on the specific PM.
|
||||
*
|
||||
* valsIn (in) - a vector of the parameters from the row.
|
||||
*/
|
||||
virtual ReturnCode nextValue(mcsv1Context* context, std::vector<ColumnDatum>& valsIn);
|
||||
/**
|
||||
* nextValue()
|
||||
*
|
||||
* Mandatory. Handle a single row.
|
||||
*
|
||||
* colsIn - A vector of data structure describing the input
|
||||
* data.
|
||||
*
|
||||
* This function is called once for every row in the filtered
|
||||
* result set (before aggregation). It is very important that
|
||||
* this function is efficient.
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, nextValue
|
||||
* cannot depend on order, as it will only be called for each
|
||||
* row found on the specific PM.
|
||||
*
|
||||
* valsIn (in) - a vector of the parameters from the row.
|
||||
*/
|
||||
virtual ReturnCode nextValue(mcsv1Context* context, std::vector<ColumnDatum>& valsIn);
|
||||
|
||||
/**
|
||||
* subEvaluate()
|
||||
*
|
||||
* Mandatory -- Called if the UDAF is running in a distributed
|
||||
* fashion. Columnstore tries to run all aggregate functions
|
||||
* distributed, depending on context.
|
||||
*
|
||||
* Perform an aggregation on rows partially aggregated by
|
||||
* nextValue. Columnstore calls nextValue for each row on a
|
||||
* given PM for a group (GROUP BY). subEvaluate is called on the
|
||||
* UM to consolodate those values into a single instance of
|
||||
* userData. Keep your aggregated totals in context's userData.
|
||||
* The first time this is called for a group, reset() would have
|
||||
* been called with this version of userData.
|
||||
*
|
||||
* Called for every partial data set in each group in GROUP BY.
|
||||
*
|
||||
* When subEvaluate has been called for all subAggregated data
|
||||
* sets, Evaluate will be called with the same context as here.
|
||||
*
|
||||
* valIn (In) - This is a pointer to a memory block of the size
|
||||
* set in setUserDataSize. It will contain the value of userData
|
||||
* as seen in the last call to NextValue for a given PM.
|
||||
*
|
||||
*/
|
||||
virtual ReturnCode subEvaluate(mcsv1Context* context, const UserData* userDataIn);
|
||||
/**
|
||||
* subEvaluate()
|
||||
*
|
||||
* Mandatory -- Called if the UDAF is running in a distributed
|
||||
* fashion. Columnstore tries to run all aggregate functions
|
||||
* distributed, depending on context.
|
||||
*
|
||||
* Perform an aggregation on rows partially aggregated by
|
||||
* nextValue. Columnstore calls nextValue for each row on a
|
||||
* given PM for a group (GROUP BY). subEvaluate is called on the
|
||||
* UM to consolodate those values into a single instance of
|
||||
* userData. Keep your aggregated totals in context's userData.
|
||||
* The first time this is called for a group, reset() would have
|
||||
* been called with this version of userData.
|
||||
*
|
||||
* Called for every partial data set in each group in GROUP BY.
|
||||
*
|
||||
* When subEvaluate has been called for all subAggregated data
|
||||
* sets, Evaluate will be called with the same context as here.
|
||||
*
|
||||
* valIn (In) - This is a pointer to a memory block of the size
|
||||
* set in setUserDataSize. It will contain the value of userData
|
||||
* as seen in the last call to NextValue for a given PM.
|
||||
*
|
||||
*/
|
||||
virtual ReturnCode subEvaluate(mcsv1Context* context, const UserData* userDataIn);
|
||||
|
||||
/**
|
||||
* evaluate()
|
||||
*
|
||||
* Mandatory. Get the aggregated value.
|
||||
*
|
||||
* Called for every new group if UDAF GROUP BY, UDAnF partition
|
||||
* or, in some cases, new Window Frame.
|
||||
*
|
||||
* Set the aggregated value into valOut. The datatype is assumed
|
||||
* to be the same as that set in the init() function;
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, evaluate is
|
||||
* called after a series of subEvaluate calls.
|
||||
*
|
||||
* valOut (out) - Set the aggregated value here. The datatype is
|
||||
* assumed to be the same as that set in the init() function;
|
||||
*
|
||||
* To return a NULL value, don't assign to valOut.
|
||||
*/
|
||||
virtual ReturnCode evaluate(mcsv1Context* context, static_any::any& valOut);
|
||||
/**
|
||||
* evaluate()
|
||||
*
|
||||
* Mandatory. Get the aggregated value.
|
||||
*
|
||||
* Called for every new group if UDAF GROUP BY, UDAnF partition
|
||||
* or, in some cases, new Window Frame.
|
||||
*
|
||||
* Set the aggregated value into valOut. The datatype is assumed
|
||||
* to be the same as that set in the init() function;
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, evaluate is
|
||||
* called after a series of subEvaluate calls.
|
||||
*
|
||||
* valOut (out) - Set the aggregated value here. The datatype is
|
||||
* assumed to be the same as that set in the init() function;
|
||||
*
|
||||
* To return a NULL value, don't assign to valOut.
|
||||
*/
|
||||
virtual ReturnCode evaluate(mcsv1Context* context, static_any::any& valOut);
|
||||
|
||||
protected:
|
||||
|
||||
|
455
utils/udfsdk/avg_mode.cpp
Executable file → Normal file
455
utils/udfsdk/avg_mode.cpp
Executable file → Normal file
@ -25,269 +25,282 @@
|
||||
using namespace mcsv1sdk;
|
||||
|
||||
mcsv1_UDAF::ReturnCode avg_mode::init(mcsv1Context* context,
|
||||
COL_TYPES& colTypes)
|
||||
COL_TYPES& colTypes)
|
||||
{
|
||||
if (colTypes.size() < 1)
|
||||
{
|
||||
// The error message will be prepended with
|
||||
// "The storage engine for the table doesn't support "
|
||||
context->setErrorMessage("avg_mode() with 0 arguments");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
if (colTypes.size() > 1)
|
||||
{
|
||||
context->setErrorMessage("avg_mode() with more than 1 argument");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
if (colTypes.size() < 1)
|
||||
{
|
||||
// The error message will be prepended with
|
||||
// "The storage engine for the table doesn't support "
|
||||
context->setErrorMessage("avg_mode() with 0 arguments");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
|
||||
if (!(isNumeric(colTypes[0].second)))
|
||||
{
|
||||
// The error message will be prepended with
|
||||
// "The storage engine for the table doesn't support "
|
||||
context->setErrorMessage("avg_mode() with non-numeric argument");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
if (colTypes.size() > 1)
|
||||
{
|
||||
context->setErrorMessage("avg_mode() with more than 1 argument");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
|
||||
context->setResultType(CalpontSystemCatalog::DOUBLE);
|
||||
context->setColWidth(8);
|
||||
context->setScale(context->getScale()*2);
|
||||
context->setPrecision(19);
|
||||
context->setRunFlag(mcsv1sdk::UDAF_IGNORE_NULLS);
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
if (!(isNumeric(colTypes[0].second)))
|
||||
{
|
||||
// The error message will be prepended with
|
||||
// "The storage engine for the table doesn't support "
|
||||
context->setErrorMessage("avg_mode() with non-numeric argument");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
|
||||
context->setResultType(CalpontSystemCatalog::DOUBLE);
|
||||
context->setColWidth(8);
|
||||
context->setScale(context->getScale() * 2);
|
||||
context->setPrecision(19);
|
||||
context->setRunFlag(mcsv1sdk::UDAF_IGNORE_NULLS);
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode avg_mode::reset(mcsv1Context* context)
|
||||
{
|
||||
ModeData* data = static_cast<ModeData*>(context->getUserData());
|
||||
data->mData.clear();
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
ModeData* data = static_cast<ModeData*>(context->getUserData());
|
||||
data->mData.clear();
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode avg_mode::nextValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsIn)
|
||||
mcsv1_UDAF::ReturnCode avg_mode::nextValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsIn)
|
||||
{
|
||||
static_any::any& valIn = valsIn[0].columnData;
|
||||
MODE_DATA& data = static_cast<ModeData*>(context->getUserData())->mData;
|
||||
DATATYPE val = 0.0;
|
||||
static_any::any& valIn = valsIn[0].columnData;
|
||||
MODE_DATA& data = static_cast<ModeData*>(context->getUserData())->mData;
|
||||
DATATYPE val = 0.0;
|
||||
|
||||
if (valIn.empty())
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS; // Ought not happen when UDAF_IGNORE_NULLS is on.
|
||||
}
|
||||
if (valIn.empty())
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS; // Ought not happen when UDAF_IGNORE_NULLS is on.
|
||||
}
|
||||
|
||||
if (valIn.compatible(charTypeId))
|
||||
{
|
||||
val = valIn.cast<char>();
|
||||
}
|
||||
else if (valIn.compatible(scharTypeId))
|
||||
{
|
||||
val = valIn.cast<signed char>();
|
||||
}
|
||||
else if (valIn.compatible(shortTypeId))
|
||||
{
|
||||
val = valIn.cast<short>();
|
||||
}
|
||||
else if (valIn.compatible(intTypeId))
|
||||
{
|
||||
val = valIn.cast<int>();
|
||||
}
|
||||
else if (valIn.compatible(longTypeId))
|
||||
{
|
||||
val = valIn.cast<long>();
|
||||
}
|
||||
else if (valIn.compatible(llTypeId))
|
||||
{
|
||||
val = valIn.cast<long long>();
|
||||
}
|
||||
else if (valIn.compatible(ucharTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned char>();
|
||||
}
|
||||
else if (valIn.compatible(ushortTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned short>();
|
||||
}
|
||||
else if (valIn.compatible(uintTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned int>();
|
||||
}
|
||||
else if (valIn.compatible(ulongTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long>();
|
||||
}
|
||||
else if (valIn.compatible(ullTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long long>();
|
||||
}
|
||||
else if (valIn.compatible(floatTypeId))
|
||||
{
|
||||
val = valIn.cast<float>();
|
||||
}
|
||||
else if (valIn.compatible(doubleTypeId))
|
||||
{
|
||||
val = valIn.cast<double>();
|
||||
}
|
||||
if (valIn.compatible(charTypeId))
|
||||
{
|
||||
val = valIn.cast<char>();
|
||||
}
|
||||
else if (valIn.compatible(scharTypeId))
|
||||
{
|
||||
val = valIn.cast<signed char>();
|
||||
}
|
||||
else if (valIn.compatible(shortTypeId))
|
||||
{
|
||||
val = valIn.cast<short>();
|
||||
}
|
||||
else if (valIn.compatible(intTypeId))
|
||||
{
|
||||
val = valIn.cast<int>();
|
||||
}
|
||||
else if (valIn.compatible(longTypeId))
|
||||
{
|
||||
val = valIn.cast<long>();
|
||||
}
|
||||
else if (valIn.compatible(llTypeId))
|
||||
{
|
||||
val = valIn.cast<long long>();
|
||||
}
|
||||
else if (valIn.compatible(ucharTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned char>();
|
||||
}
|
||||
else if (valIn.compatible(ushortTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned short>();
|
||||
}
|
||||
else if (valIn.compatible(uintTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned int>();
|
||||
}
|
||||
else if (valIn.compatible(ulongTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long>();
|
||||
}
|
||||
else if (valIn.compatible(ullTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long long>();
|
||||
}
|
||||
else if (valIn.compatible(floatTypeId))
|
||||
{
|
||||
val = valIn.cast<float>();
|
||||
}
|
||||
else if (valIn.compatible(doubleTypeId))
|
||||
{
|
||||
val = valIn.cast<double>();
|
||||
}
|
||||
|
||||
// For decimal types, we need to move the decimal point.
|
||||
uint32_t scale = valsIn[0].scale;
|
||||
if (val != 0 && scale > 0)
|
||||
{
|
||||
val /= pow(10.0, (double)scale);
|
||||
}
|
||||
data[val]++;
|
||||
// For decimal types, we need to move the decimal point.
|
||||
uint32_t scale = valsIn[0].scale;
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
if (val != 0 && scale > 0)
|
||||
{
|
||||
val /= pow(10.0, (double)scale);
|
||||
}
|
||||
|
||||
data[val]++;
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode avg_mode::subEvaluate(mcsv1Context* context, const UserData* userDataIn)
|
||||
{
|
||||
if (!userDataIn)
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
MODE_DATA& outData = static_cast<ModeData*>(context->getUserData())->mData;
|
||||
const MODE_DATA& inData = static_cast<const ModeData*>(userDataIn)->mData;
|
||||
MODE_DATA::const_iterator iter = inData.begin();
|
||||
for (; iter != inData.end(); ++iter)
|
||||
{
|
||||
outData[iter->first] += iter->second;
|
||||
}
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
if (!userDataIn)
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
MODE_DATA& outData = static_cast<ModeData*>(context->getUserData())->mData;
|
||||
const MODE_DATA& inData = static_cast<const ModeData*>(userDataIn)->mData;
|
||||
MODE_DATA::const_iterator iter = inData.begin();
|
||||
|
||||
for (; iter != inData.end(); ++iter)
|
||||
{
|
||||
outData[iter->first] += iter->second;
|
||||
}
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode avg_mode::evaluate(mcsv1Context* context, static_any::any& valOut)
|
||||
{
|
||||
uint64_t maxCnt=0;
|
||||
MODE_DATA& data = static_cast<ModeData*>(context->getUserData())->mData;
|
||||
if (data.size() == 0)
|
||||
{
|
||||
valOut = (DATATYPE)0;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
MODE_DATA::iterator iter(data.begin());
|
||||
for (; iter != data.end(); ++iter)
|
||||
{
|
||||
if (iter->second > maxCnt)
|
||||
{
|
||||
valOut = iter->first;
|
||||
maxCnt = iter->second;
|
||||
}
|
||||
}
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
uint64_t maxCnt = 0;
|
||||
MODE_DATA& data = static_cast<ModeData*>(context->getUserData())->mData;
|
||||
|
||||
if (data.size() == 0)
|
||||
{
|
||||
valOut = (DATATYPE)0;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
MODE_DATA::iterator iter(data.begin());
|
||||
|
||||
for (; iter != data.end(); ++iter)
|
||||
{
|
||||
if (iter->second > maxCnt)
|
||||
{
|
||||
valOut = iter->first;
|
||||
maxCnt = iter->second;
|
||||
}
|
||||
}
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode avg_mode::dropValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsDropped)
|
||||
mcsv1_UDAF::ReturnCode avg_mode::dropValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsDropped)
|
||||
{
|
||||
static_any::any& valIn = valsDropped[0].columnData;
|
||||
MODE_DATA& data = static_cast<ModeData*>(context->getUserData())->mData;
|
||||
DATATYPE val = 0.0;
|
||||
static_any::any& valIn = valsDropped[0].columnData;
|
||||
MODE_DATA& data = static_cast<ModeData*>(context->getUserData())->mData;
|
||||
DATATYPE val = 0.0;
|
||||
|
||||
if (valIn.empty())
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS; // Ought not happen when UDAF_IGNORE_NULLS is on.
|
||||
}
|
||||
if (valIn.empty())
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS; // Ought not happen when UDAF_IGNORE_NULLS is on.
|
||||
}
|
||||
|
||||
if (valIn.compatible(charTypeId))
|
||||
{
|
||||
val = valIn.cast<char>();
|
||||
}
|
||||
else if (valIn.compatible(scharTypeId))
|
||||
{
|
||||
val = valIn.cast<signed char>();
|
||||
}
|
||||
else if (valIn.compatible(shortTypeId))
|
||||
{
|
||||
val = valIn.cast<short>();
|
||||
}
|
||||
else if (valIn.compatible(intTypeId))
|
||||
{
|
||||
val = valIn.cast<int>();
|
||||
}
|
||||
else if (valIn.compatible(longTypeId))
|
||||
{
|
||||
val = valIn.cast<long>();
|
||||
}
|
||||
else if (valIn.compatible(llTypeId))
|
||||
{
|
||||
val = valIn.cast<long long>();
|
||||
}
|
||||
else if (valIn.compatible(ucharTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned char>();
|
||||
}
|
||||
else if (valIn.compatible(ushortTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned short>();
|
||||
}
|
||||
else if (valIn.compatible(uintTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned int>();
|
||||
}
|
||||
else if (valIn.compatible(ulongTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long>();
|
||||
}
|
||||
else if (valIn.compatible(ullTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long long>();
|
||||
}
|
||||
else if (valIn.compatible(floatTypeId))
|
||||
{
|
||||
val = valIn.cast<float>();
|
||||
}
|
||||
else if (valIn.compatible(doubleTypeId))
|
||||
{
|
||||
val = valIn.cast<double>();
|
||||
}
|
||||
if (valIn.compatible(charTypeId))
|
||||
{
|
||||
val = valIn.cast<char>();
|
||||
}
|
||||
else if (valIn.compatible(scharTypeId))
|
||||
{
|
||||
val = valIn.cast<signed char>();
|
||||
}
|
||||
else if (valIn.compatible(shortTypeId))
|
||||
{
|
||||
val = valIn.cast<short>();
|
||||
}
|
||||
else if (valIn.compatible(intTypeId))
|
||||
{
|
||||
val = valIn.cast<int>();
|
||||
}
|
||||
else if (valIn.compatible(longTypeId))
|
||||
{
|
||||
val = valIn.cast<long>();
|
||||
}
|
||||
else if (valIn.compatible(llTypeId))
|
||||
{
|
||||
val = valIn.cast<long long>();
|
||||
}
|
||||
else if (valIn.compatible(ucharTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned char>();
|
||||
}
|
||||
else if (valIn.compatible(ushortTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned short>();
|
||||
}
|
||||
else if (valIn.compatible(uintTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned int>();
|
||||
}
|
||||
else if (valIn.compatible(ulongTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long>();
|
||||
}
|
||||
else if (valIn.compatible(ullTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long long>();
|
||||
}
|
||||
else if (valIn.compatible(floatTypeId))
|
||||
{
|
||||
val = valIn.cast<float>();
|
||||
}
|
||||
else if (valIn.compatible(doubleTypeId))
|
||||
{
|
||||
val = valIn.cast<double>();
|
||||
}
|
||||
|
||||
// For decimal types, we need to move the decimal point.
|
||||
uint32_t scale = valsDropped[0].scale;
|
||||
if (val != 0 && scale > 0)
|
||||
{
|
||||
val /= pow(10.0, (double)scale);
|
||||
}
|
||||
// For decimal types, we need to move the decimal point.
|
||||
uint32_t scale = valsDropped[0].scale;
|
||||
|
||||
data[val]--;
|
||||
if (val != 0 && scale > 0)
|
||||
{
|
||||
val /= pow(10.0, (double)scale);
|
||||
}
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
data[val]--;
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode avg_mode::createUserData(UserData*& userData, int32_t& length)
|
||||
{
|
||||
userData = new ModeData;
|
||||
length = sizeof(ModeData);
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
userData = new ModeData;
|
||||
length = sizeof(ModeData);
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
void ModeData::serialize(messageqcpp::ByteStream& bs) const
|
||||
{
|
||||
MODE_DATA::const_iterator iter = mData.begin();
|
||||
DATATYPE num;
|
||||
uint32_t cnt;
|
||||
bs << (int32_t)mData.size();
|
||||
for (; iter != mData.end(); ++iter)
|
||||
{
|
||||
num = iter->first;
|
||||
bs << num;
|
||||
cnt = iter->second;
|
||||
bs << cnt;
|
||||
}
|
||||
MODE_DATA::const_iterator iter = mData.begin();
|
||||
DATATYPE num;
|
||||
uint32_t cnt;
|
||||
bs << (int32_t)mData.size();
|
||||
|
||||
for (; iter != mData.end(); ++iter)
|
||||
{
|
||||
num = iter->first;
|
||||
bs << num;
|
||||
cnt = iter->second;
|
||||
bs << cnt;
|
||||
}
|
||||
}
|
||||
|
||||
void ModeData::unserialize(messageqcpp::ByteStream& bs)
|
||||
{
|
||||
mData.clear();
|
||||
int32_t sz;
|
||||
DATATYPE num;
|
||||
uint32_t cnt;
|
||||
bs >> sz;
|
||||
for (int i = 0; i < sz; ++i)
|
||||
{
|
||||
bs >> num;
|
||||
bs >> cnt;
|
||||
mData[num] = cnt;
|
||||
}
|
||||
mData.clear();
|
||||
int32_t sz;
|
||||
DATATYPE num;
|
||||
uint32_t cnt;
|
||||
bs >> sz;
|
||||
|
||||
for (int i = 0; i < sz; ++i)
|
||||
{
|
||||
bs >> num;
|
||||
bs >> cnt;
|
||||
mData[num] = cnt;
|
||||
}
|
||||
}
|
||||
|
||||
|
380
utils/udfsdk/avg_mode.h
Executable file → Normal file
380
utils/udfsdk/avg_mode.h
Executable file → Normal file
@ -21,34 +21,34 @@
|
||||
* mcsv1_UDAF.h
|
||||
***********************************************************************/
|
||||
|
||||
/**
|
||||
* Columnstore interface for writing a User Defined Aggregate
|
||||
* Functions (UDAF) and User Defined Analytic Functions (UDAnF)
|
||||
* or a function that can act as either - UDA(n)F
|
||||
*
|
||||
/**
|
||||
* Columnstore interface for writing a User Defined Aggregate
|
||||
* Functions (UDAF) and User Defined Analytic Functions (UDAnF)
|
||||
* or a function that can act as either - UDA(n)F
|
||||
*
|
||||
* The basic steps are:
|
||||
*
|
||||
* 1. Create a the UDA(n)F function interface in some .h file.
|
||||
* 2. Create the UDF function implementation in some .cpp file
|
||||
* 3. Create the connector stub (MariaDB UDAF definition) for
|
||||
* this UDF function.
|
||||
* 4. build the dynamic library using all of the source.
|
||||
* 5 Put the library in $COLUMNSTORE_INSTALL/lib of
|
||||
* all modules
|
||||
* 6. restart the Columnstore system.
|
||||
* 1. Create a the UDA(n)F function interface in some .h file.
|
||||
* 2. Create the UDF function implementation in some .cpp file
|
||||
* 3. Create the connector stub (MariaDB UDAF definition) for
|
||||
* this UDF function.
|
||||
* 4. build the dynamic library using all of the source.
|
||||
* 5 Put the library in $COLUMNSTORE_INSTALL/lib of
|
||||
* all modules
|
||||
* 6. restart the Columnstore system.
|
||||
* 7. notify mysqld about the new function:
|
||||
*
|
||||
*
|
||||
* CREATE AGGREGATE FUNCTION avg_mode returns REAL soname
|
||||
* 'libudf_mysql.so';
|
||||
*
|
||||
* The UDAF functions may run distributed in the Columnstore
|
||||
* engine. UDAnF do not run distributed.
|
||||
*
|
||||
* UDAF is User Defined Aggregate Function.
|
||||
* UDAnF is User Defined Analytic Function.
|
||||
* UDA(n)F is an acronym for a function that could be either. It
|
||||
* is also used to describe the interface that is used for
|
||||
* either.
|
||||
*
|
||||
* The UDAF functions may run distributed in the Columnstore
|
||||
* engine. UDAnF do not run distributed.
|
||||
*
|
||||
* UDAF is User Defined Aggregate Function.
|
||||
* UDAnF is User Defined Analytic Function.
|
||||
* UDA(n)F is an acronym for a function that could be either. It
|
||||
* is also used to describe the interface that is used for
|
||||
* either.
|
||||
*/
|
||||
#ifndef HEADER_mode
|
||||
#define HEADER_mode
|
||||
@ -83,196 +83,196 @@ typedef std::tr1::unordered_map<DATATYPE, uint32_t> MODE_DATA;
|
||||
// Override UserData for data storage
|
||||
struct ModeData : public UserData
|
||||
{
|
||||
ModeData() {};
|
||||
ModeData() {};
|
||||
|
||||
virtual ~ModeData(){}
|
||||
virtual ~ModeData() {}
|
||||
|
||||
virtual void serialize(messageqcpp::ByteStream& bs) const;
|
||||
virtual void unserialize(messageqcpp::ByteStream& bs);
|
||||
virtual void serialize(messageqcpp::ByteStream& bs) const;
|
||||
virtual void unserialize(messageqcpp::ByteStream& bs);
|
||||
|
||||
MODE_DATA mData;
|
||||
MODE_DATA mData;
|
||||
private:
|
||||
// For now, copy construction is unwanted
|
||||
ModeData(UserData&);
|
||||
// For now, copy construction is unwanted
|
||||
ModeData(UserData&);
|
||||
};
|
||||
|
||||
// Override mcsv1_UDAF to build your User Defined Aggregate (UDAF) and/or
|
||||
// Override mcsv1_UDAF to build your User Defined Aggregate (UDAF) and/or
|
||||
// User Defined Analytic Function (UDAnF).
|
||||
// These will be singleton classes, so don't put any instance
|
||||
// specific data in here. All instance data is stored in mcsv1Context
|
||||
// passed to each user function and retrieved by the getUserData() method.
|
||||
//
|
||||
// Each API function returns a ReturnCode. If ERROR is returned at any time,
|
||||
// the query is aborted, getInterrupted() will begin to return true and the
|
||||
// message set in config->setErrorMessage() is returned to MariaDB.
|
||||
//
|
||||
// Each API function returns a ReturnCode. If ERROR is returned at any time,
|
||||
// the query is aborted, getInterrupted() will begin to return true and the
|
||||
// message set in config->setErrorMessage() is returned to MariaDB.
|
||||
|
||||
// Return the avg_mode value of the dataset
|
||||
|
||||
class avg_mode : public mcsv1_UDAF
|
||||
{
|
||||
public:
|
||||
// Defaults OK
|
||||
avg_mode() : mcsv1_UDAF(){};
|
||||
virtual ~avg_mode(){};
|
||||
// Defaults OK
|
||||
avg_mode() : mcsv1_UDAF() {};
|
||||
virtual ~avg_mode() {};
|
||||
|
||||
/**
|
||||
* init()
|
||||
*
|
||||
* Mandatory. Implement this to initialize flags and instance
|
||||
* data. Called once per SQL statement. You can do any sanity
|
||||
* checks here.
|
||||
*
|
||||
* colTypes (in) - A vector of ColDataType defining the
|
||||
* parameters of the UDA(n)F call. These can be used to decide
|
||||
* to override the default return type. If desired, the new
|
||||
* return type can be set by context->setReturnType() and
|
||||
* decimal scale and precision can be set by context->setScale
|
||||
* and context->setPrecision respectively.
|
||||
*
|
||||
* Return mcsv1_UDAF::ERROR on any error, such as non-compatible
|
||||
* colTypes or wrong number of arguments. Else return
|
||||
* mcsv1_UDAF::SUCCESS.
|
||||
*/
|
||||
virtual ReturnCode init(mcsv1Context* context,
|
||||
COL_TYPES& colTypes);
|
||||
/**
|
||||
* init()
|
||||
*
|
||||
* Mandatory. Implement this to initialize flags and instance
|
||||
* data. Called once per SQL statement. You can do any sanity
|
||||
* checks here.
|
||||
*
|
||||
* colTypes (in) - A vector of ColDataType defining the
|
||||
* parameters of the UDA(n)F call. These can be used to decide
|
||||
* to override the default return type. If desired, the new
|
||||
* return type can be set by context->setReturnType() and
|
||||
* decimal scale and precision can be set by context->setScale
|
||||
* and context->setPrecision respectively.
|
||||
*
|
||||
* Return mcsv1_UDAF::ERROR on any error, such as non-compatible
|
||||
* colTypes or wrong number of arguments. Else return
|
||||
* mcsv1_UDAF::SUCCESS.
|
||||
*/
|
||||
virtual ReturnCode init(mcsv1Context* context,
|
||||
COL_TYPES& colTypes);
|
||||
|
||||
/**
|
||||
* reset()
|
||||
*
|
||||
* Mandatory. Reset the UDA(n)F for a new group, partition or,
|
||||
* in some cases, new Window Frame. Do not free any memory
|
||||
* allocated by context->setUserDataSize(). The SDK Framework owns
|
||||
* that memory and will handle that. Use this opportunity to
|
||||
* reset any variables in context->getUserData() needed for the
|
||||
* next aggregation. May be called multiple times if running in
|
||||
* a ditributed fashion.
|
||||
*
|
||||
* Use this opportunity to initialize the userData.
|
||||
*/
|
||||
virtual ReturnCode reset(mcsv1Context* context);
|
||||
/**
|
||||
* reset()
|
||||
*
|
||||
* Mandatory. Reset the UDA(n)F for a new group, partition or,
|
||||
* in some cases, new Window Frame. Do not free any memory
|
||||
* allocated by context->setUserDataSize(). The SDK Framework owns
|
||||
* that memory and will handle that. Use this opportunity to
|
||||
* reset any variables in context->getUserData() needed for the
|
||||
* next aggregation. May be called multiple times if running in
|
||||
* a ditributed fashion.
|
||||
*
|
||||
* Use this opportunity to initialize the userData.
|
||||
*/
|
||||
virtual ReturnCode reset(mcsv1Context* context);
|
||||
|
||||
/**
|
||||
* nextValue()
|
||||
*
|
||||
* Mandatory. Handle a single row.
|
||||
*
|
||||
* colsIn - A vector of data structure describing the input
|
||||
* data.
|
||||
*
|
||||
* This function is called once for every row in the filtered
|
||||
* result set (before aggregation). It is very important that
|
||||
* this function is efficient.
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, nextValue
|
||||
* cannot depend on order, as it will only be called for each
|
||||
* row found on the specific PM.
|
||||
*
|
||||
* valsIn (in) - a vector of the parameters from the row.
|
||||
*/
|
||||
virtual ReturnCode nextValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsIn);
|
||||
/**
|
||||
* nextValue()
|
||||
*
|
||||
* Mandatory. Handle a single row.
|
||||
*
|
||||
* colsIn - A vector of data structure describing the input
|
||||
* data.
|
||||
*
|
||||
* This function is called once for every row in the filtered
|
||||
* result set (before aggregation). It is very important that
|
||||
* this function is efficient.
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, nextValue
|
||||
* cannot depend on order, as it will only be called for each
|
||||
* row found on the specific PM.
|
||||
*
|
||||
* valsIn (in) - a vector of the parameters from the row.
|
||||
*/
|
||||
virtual ReturnCode nextValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsIn);
|
||||
|
||||
/**
|
||||
* subEvaluate()
|
||||
*
|
||||
* Mandatory -- Called if the UDAF is running in a distributed
|
||||
* fashion. Columnstore tries to run all aggregate functions
|
||||
* distributed, depending on context.
|
||||
*
|
||||
* Perform an aggregation on rows partially aggregated by
|
||||
* nextValue. Columnstore calls nextValue for each row on a
|
||||
* given PM for a group (GROUP BY). subEvaluate is called on the
|
||||
* UM to consolodate those values into a single instance of
|
||||
* userData. Keep your aggregated totals in context's userData.
|
||||
* The first time this is called for a group, reset() would have
|
||||
* been called with this version of userData.
|
||||
*
|
||||
* Called for every partial data set in each group in GROUP BY.
|
||||
*
|
||||
* When subEvaluate has been called for all subAggregated data
|
||||
* sets, Evaluate will be called with the same context as here.
|
||||
*
|
||||
* valIn (In) - This is a pointer to a memory block of the size
|
||||
* set in setUserDataSize. It will contain the value of userData
|
||||
* as seen in the last call to NextValue for a given PM.
|
||||
*
|
||||
*/
|
||||
virtual ReturnCode subEvaluate(mcsv1Context* context, const UserData* valIn);
|
||||
/**
|
||||
* subEvaluate()
|
||||
*
|
||||
* Mandatory -- Called if the UDAF is running in a distributed
|
||||
* fashion. Columnstore tries to run all aggregate functions
|
||||
* distributed, depending on context.
|
||||
*
|
||||
* Perform an aggregation on rows partially aggregated by
|
||||
* nextValue. Columnstore calls nextValue for each row on a
|
||||
* given PM for a group (GROUP BY). subEvaluate is called on the
|
||||
* UM to consolodate those values into a single instance of
|
||||
* userData. Keep your aggregated totals in context's userData.
|
||||
* The first time this is called for a group, reset() would have
|
||||
* been called with this version of userData.
|
||||
*
|
||||
* Called for every partial data set in each group in GROUP BY.
|
||||
*
|
||||
* When subEvaluate has been called for all subAggregated data
|
||||
* sets, Evaluate will be called with the same context as here.
|
||||
*
|
||||
* valIn (In) - This is a pointer to a memory block of the size
|
||||
* set in setUserDataSize. It will contain the value of userData
|
||||
* as seen in the last call to NextValue for a given PM.
|
||||
*
|
||||
*/
|
||||
virtual ReturnCode subEvaluate(mcsv1Context* context, const UserData* valIn);
|
||||
|
||||
/**
|
||||
* evaluate()
|
||||
*
|
||||
* Mandatory. Get the aggregated value.
|
||||
*
|
||||
* Called for every new group if UDAF GROUP BY, UDAnF partition
|
||||
* or, in some cases, new Window Frame.
|
||||
*
|
||||
* Set the aggregated value into valOut. The datatype is assumed
|
||||
* to be the same as that set in the init() function;
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, evaluate is
|
||||
* called after a series of subEvaluate calls.
|
||||
*
|
||||
* valOut (out) - Set the aggregated value here. The datatype is
|
||||
* assumed to be the same as that set in the init() function;
|
||||
*
|
||||
* To return a NULL value, don't assign to valOut.
|
||||
*/
|
||||
virtual ReturnCode evaluate(mcsv1Context* context, static_any::any& valOut);
|
||||
/**
|
||||
* evaluate()
|
||||
*
|
||||
* Mandatory. Get the aggregated value.
|
||||
*
|
||||
* Called for every new group if UDAF GROUP BY, UDAnF partition
|
||||
* or, in some cases, new Window Frame.
|
||||
*
|
||||
* Set the aggregated value into valOut. The datatype is assumed
|
||||
* to be the same as that set in the init() function;
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, evaluate is
|
||||
* called after a series of subEvaluate calls.
|
||||
*
|
||||
* valOut (out) - Set the aggregated value here. The datatype is
|
||||
* assumed to be the same as that set in the init() function;
|
||||
*
|
||||
* To return a NULL value, don't assign to valOut.
|
||||
*/
|
||||
virtual ReturnCode evaluate(mcsv1Context* context, static_any::any& valOut);
|
||||
|
||||
/**
|
||||
* dropValue()
|
||||
*
|
||||
* Optional -- If defined, the server will call this instead of
|
||||
* reset for UDAnF.
|
||||
*
|
||||
* Don't implement if a UDAnF has one or more of the following:
|
||||
* The UDAnF can't be used with a Window Frame
|
||||
* The UDAnF is not reversable in some way
|
||||
* The UDAnF is not interested in optimal performance
|
||||
*
|
||||
* If not implemented, reset() followed by a series of
|
||||
* nextValue() will be called for each movement of the Window
|
||||
* Frame.
|
||||
*
|
||||
* If implemented, then each movement of the Window Frame will
|
||||
* result in dropValue() being called for each row falling out
|
||||
* of the Frame and nextValue() being called for each new row
|
||||
* coming into the Frame.
|
||||
*
|
||||
* valsDropped (in) - a vector of the parameters from the row
|
||||
* leaving the Frame
|
||||
*
|
||||
* dropValue() will not be called for unbounded/current row type
|
||||
* frames, as those are already optimized.
|
||||
*/
|
||||
virtual ReturnCode dropValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsDropped);
|
||||
/**
|
||||
* dropValue()
|
||||
*
|
||||
* Optional -- If defined, the server will call this instead of
|
||||
* reset for UDAnF.
|
||||
*
|
||||
* Don't implement if a UDAnF has one or more of the following:
|
||||
* The UDAnF can't be used with a Window Frame
|
||||
* The UDAnF is not reversable in some way
|
||||
* The UDAnF is not interested in optimal performance
|
||||
*
|
||||
* If not implemented, reset() followed by a series of
|
||||
* nextValue() will be called for each movement of the Window
|
||||
* Frame.
|
||||
*
|
||||
* If implemented, then each movement of the Window Frame will
|
||||
* result in dropValue() being called for each row falling out
|
||||
* of the Frame and nextValue() being called for each new row
|
||||
* coming into the Frame.
|
||||
*
|
||||
* valsDropped (in) - a vector of the parameters from the row
|
||||
* leaving the Frame
|
||||
*
|
||||
* dropValue() will not be called for unbounded/current row type
|
||||
* frames, as those are already optimized.
|
||||
*/
|
||||
virtual ReturnCode dropValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsDropped);
|
||||
|
||||
/**
|
||||
* createUserData()
|
||||
*
|
||||
* Optional -- If defined, the server will call this instead of
|
||||
* createUserData on context.
|
||||
*
|
||||
* Create your variable length data structure via
|
||||
* data = new <datatype>
|
||||
*
|
||||
* The data structure may contain references to containers or
|
||||
* pointers to other objects. Remember that for distributed
|
||||
* processing, this may be called multiple times for variaous
|
||||
* computing blocks. At the least, it will be called once per PM
|
||||
* that processes the data, and once more for the UM. For UDAnF,
|
||||
* it may only be called once.
|
||||
*
|
||||
* Set length to the length of the data structure you create.
|
||||
*
|
||||
* For each call to createUserData(), there will be a
|
||||
* corresponding deleteUserData() where you must clean up. Any
|
||||
* memory leaks are your fault.
|
||||
*
|
||||
*/
|
||||
virtual ReturnCode createUserData(UserData*& data, int32_t& length);
|
||||
/**
|
||||
* createUserData()
|
||||
*
|
||||
* Optional -- If defined, the server will call this instead of
|
||||
* createUserData on context.
|
||||
*
|
||||
* Create your variable length data structure via
|
||||
* data = new <datatype>
|
||||
*
|
||||
* The data structure may contain references to containers or
|
||||
* pointers to other objects. Remember that for distributed
|
||||
* processing, this may be called multiple times for variaous
|
||||
* computing blocks. At the least, it will be called once per PM
|
||||
* that processes the data, and once more for the UM. For UDAnF,
|
||||
* it may only be called once.
|
||||
*
|
||||
* Set length to the length of the data structure you create.
|
||||
*
|
||||
* For each call to createUserData(), there will be a
|
||||
* corresponding deleteUserData() where you must clean up. Any
|
||||
* memory leaks are your fault.
|
||||
*
|
||||
*/
|
||||
virtual ReturnCode createUserData(UserData*& data, int32_t& length);
|
||||
protected:
|
||||
};
|
||||
|
||||
|
332
utils/udfsdk/mcsv1_udaf.cpp
Executable file → Normal file
332
utils/udfsdk/mcsv1_udaf.cpp
Executable file → Normal file
@ -25,11 +25,11 @@
|
||||
using namespace mcsv1sdk;
|
||||
/**
|
||||
* All UDA(n)F functions must be registered in the function map.
|
||||
* They will be picked up by the Columnstore modules during
|
||||
* startup.
|
||||
*
|
||||
* This is a temporary kludge until we get the library loader
|
||||
* task complete
|
||||
* They will be picked up by the Columnstore modules during
|
||||
* startup.
|
||||
*
|
||||
* This is a temporary kludge until we get the library loader
|
||||
* task complete
|
||||
*/
|
||||
UDAF_MAP UDAFMap::fm;
|
||||
#include "allnull.h"
|
||||
@ -38,204 +38,218 @@ UDAF_MAP UDAFMap::fm;
|
||||
#include "avg_mode.h"
|
||||
UDAF_MAP& UDAFMap::getMap()
|
||||
{
|
||||
if (fm.size() > 0)
|
||||
{
|
||||
return fm;
|
||||
}
|
||||
// first: function name
|
||||
// second: Function pointer
|
||||
// please use lower case for the function name. Because the names might be
|
||||
// case-insensitive in MySQL depending on the setting. In such case,
|
||||
// the function names passed to the interface is always in lower case.
|
||||
fm["allnull"] = new allnull();
|
||||
fm["ssq"] = new ssq();
|
||||
fm["median"] = new median();
|
||||
fm["avg_mode"] = new avg_mode();
|
||||
|
||||
return fm;
|
||||
if (fm.size() > 0)
|
||||
{
|
||||
return fm;
|
||||
}
|
||||
|
||||
// first: function name
|
||||
// second: Function pointer
|
||||
// please use lower case for the function name. Because the names might be
|
||||
// case-insensitive in MySQL depending on the setting. In such case,
|
||||
// the function names passed to the interface is always in lower case.
|
||||
fm["allnull"] = new allnull();
|
||||
fm["ssq"] = new ssq();
|
||||
fm["median"] = new median();
|
||||
fm["avg_mode"] = new avg_mode();
|
||||
|
||||
return fm;
|
||||
}
|
||||
|
||||
int32_t mcsv1Context::getColWidth()
|
||||
{
|
||||
if (fColWidth > 0)
|
||||
{
|
||||
return fColWidth;
|
||||
}
|
||||
// JIT initialization for types that have a defined size.
|
||||
switch (fResultType)
|
||||
{
|
||||
case CalpontSystemCatalog::BIT:
|
||||
case CalpontSystemCatalog::TINYINT:
|
||||
case CalpontSystemCatalog::UTINYINT:
|
||||
case CalpontSystemCatalog::CHAR:
|
||||
fColWidth = 1;
|
||||
break;
|
||||
case CalpontSystemCatalog::SMALLINT:
|
||||
case CalpontSystemCatalog::USMALLINT:
|
||||
fColWidth = 2;
|
||||
break;
|
||||
case CalpontSystemCatalog::MEDINT:
|
||||
case CalpontSystemCatalog::INT:
|
||||
case CalpontSystemCatalog::UMEDINT:
|
||||
case CalpontSystemCatalog::UINT:
|
||||
case CalpontSystemCatalog::FLOAT:
|
||||
case CalpontSystemCatalog::UFLOAT:
|
||||
case CalpontSystemCatalog::DATE:
|
||||
fColWidth = 4;
|
||||
break;
|
||||
case CalpontSystemCatalog::BIGINT:
|
||||
case CalpontSystemCatalog::UBIGINT:
|
||||
case CalpontSystemCatalog::DECIMAL:
|
||||
case CalpontSystemCatalog::UDECIMAL:
|
||||
case CalpontSystemCatalog::DOUBLE:
|
||||
case CalpontSystemCatalog::UDOUBLE:
|
||||
case CalpontSystemCatalog::DATETIME:
|
||||
case CalpontSystemCatalog::STRINT:
|
||||
fColWidth = 8;
|
||||
break;
|
||||
case CalpontSystemCatalog::LONGDOUBLE:
|
||||
fColWidth = sizeof(long double);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return fColWidth;
|
||||
if (fColWidth > 0)
|
||||
{
|
||||
return fColWidth;
|
||||
}
|
||||
|
||||
// JIT initialization for types that have a defined size.
|
||||
switch (fResultType)
|
||||
{
|
||||
case CalpontSystemCatalog::BIT:
|
||||
case CalpontSystemCatalog::TINYINT:
|
||||
case CalpontSystemCatalog::UTINYINT:
|
||||
case CalpontSystemCatalog::CHAR:
|
||||
fColWidth = 1;
|
||||
break;
|
||||
|
||||
case CalpontSystemCatalog::SMALLINT:
|
||||
case CalpontSystemCatalog::USMALLINT:
|
||||
fColWidth = 2;
|
||||
break;
|
||||
|
||||
case CalpontSystemCatalog::MEDINT:
|
||||
case CalpontSystemCatalog::INT:
|
||||
case CalpontSystemCatalog::UMEDINT:
|
||||
case CalpontSystemCatalog::UINT:
|
||||
case CalpontSystemCatalog::FLOAT:
|
||||
case CalpontSystemCatalog::UFLOAT:
|
||||
case CalpontSystemCatalog::DATE:
|
||||
fColWidth = 4;
|
||||
break;
|
||||
|
||||
case CalpontSystemCatalog::BIGINT:
|
||||
case CalpontSystemCatalog::UBIGINT:
|
||||
case CalpontSystemCatalog::DECIMAL:
|
||||
case CalpontSystemCatalog::UDECIMAL:
|
||||
case CalpontSystemCatalog::DOUBLE:
|
||||
case CalpontSystemCatalog::UDOUBLE:
|
||||
case CalpontSystemCatalog::DATETIME:
|
||||
case CalpontSystemCatalog::STRINT:
|
||||
fColWidth = 8;
|
||||
break;
|
||||
|
||||
case CalpontSystemCatalog::LONGDOUBLE:
|
||||
fColWidth = sizeof(long double);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return fColWidth;
|
||||
}
|
||||
|
||||
bool mcsv1Context::operator==(const mcsv1Context& c) const
|
||||
{
|
||||
// We don't test the per row data fields. They don't determine
|
||||
// if it's the same Context.
|
||||
if (getName() != c.getName()
|
||||
|| fRunFlags != c.fRunFlags
|
||||
|| fContextFlags != c.fContextFlags
|
||||
|| fUserDataSize != c.fUserDataSize
|
||||
|| fResultType != c.fResultType
|
||||
|| fResultscale != c.fResultscale
|
||||
|| fResultPrecision != c.fResultPrecision
|
||||
|| fStartFrame != c.fStartFrame
|
||||
|| fEndFrame != c.fEndFrame
|
||||
|| fStartConstant != c.fStartConstant
|
||||
|| fEndConstant != c.fEndConstant)
|
||||
return false;
|
||||
return true;
|
||||
// We don't test the per row data fields. They don't determine
|
||||
// if it's the same Context.
|
||||
if (getName() != c.getName()
|
||||
|| fRunFlags != c.fRunFlags
|
||||
|| fContextFlags != c.fContextFlags
|
||||
|| fUserDataSize != c.fUserDataSize
|
||||
|| fResultType != c.fResultType
|
||||
|| fResultscale != c.fResultscale
|
||||
|| fResultPrecision != c.fResultPrecision
|
||||
|| fStartFrame != c.fStartFrame
|
||||
|| fEndFrame != c.fEndFrame
|
||||
|| fStartConstant != c.fStartConstant
|
||||
|| fEndConstant != c.fEndConstant)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mcsv1Context::operator!=(const mcsv1Context& c) const
|
||||
{
|
||||
return (!(*this == c));
|
||||
return (!(*this == c));
|
||||
}
|
||||
|
||||
const std::string mcsv1Context::toString() const
|
||||
{
|
||||
std::ostringstream output;
|
||||
output << "mcsv1Context: " << getName() << std::endl;
|
||||
output << " RunFlags=" << fRunFlags << " ContextFlags=" << fContextFlags << std::endl;
|
||||
output << " UserDataSize=" << fUserDataSize << " ResultType=" << colDataTypeToString(fResultType) << std::endl;
|
||||
output << " Resultscale=" << fResultscale << " ResultPrecision=" << fResultPrecision << std::endl;
|
||||
output << " ErrorMsg=" << errorMsg << std::endl;
|
||||
output << " bInterrupted=" << bInterrupted << std::endl;
|
||||
output << " StartFrame=" << fStartFrame << " EndFrame=" << fEndFrame << std::endl;
|
||||
output << " StartConstant=" << fStartConstant << " EndConstant=" << fEndConstant << std::endl;
|
||||
return output.str();
|
||||
std::ostringstream output;
|
||||
output << "mcsv1Context: " << getName() << std::endl;
|
||||
output << " RunFlags=" << fRunFlags << " ContextFlags=" << fContextFlags << std::endl;
|
||||
output << " UserDataSize=" << fUserDataSize << " ResultType=" << colDataTypeToString(fResultType) << std::endl;
|
||||
output << " Resultscale=" << fResultscale << " ResultPrecision=" << fResultPrecision << std::endl;
|
||||
output << " ErrorMsg=" << errorMsg << std::endl;
|
||||
output << " bInterrupted=" << bInterrupted << std::endl;
|
||||
output << " StartFrame=" << fStartFrame << " EndFrame=" << fEndFrame << std::endl;
|
||||
output << " StartConstant=" << fStartConstant << " EndConstant=" << fEndConstant << std::endl;
|
||||
return output.str();
|
||||
}
|
||||
|
||||
mcsv1sdk::mcsv1_UDAF* mcsv1Context::getFunction()
|
||||
{
|
||||
if (func)
|
||||
{
|
||||
return func;
|
||||
}
|
||||
if (func)
|
||||
{
|
||||
return func;
|
||||
}
|
||||
|
||||
// Just in time initialization
|
||||
if (functionName.length() == 0)
|
||||
{
|
||||
std::ostringstream errmsg;
|
||||
errmsg << "mcsv1Context::getFunction: " << functionName << " is empty";
|
||||
throw std::logic_error(errmsg.str());
|
||||
}
|
||||
mcsv1sdk::UDAF_MAP::iterator funcIter = mcsv1sdk::UDAFMap::getMap().find(functionName);
|
||||
if (funcIter == mcsv1sdk::UDAFMap::getMap().end())
|
||||
{
|
||||
std::ostringstream errmsg;
|
||||
errmsg << "mcsv1Context::getFunction: " << functionName << " is undefined";
|
||||
throw std::logic_error(errmsg.str());
|
||||
}
|
||||
func = funcIter->second;
|
||||
return func;
|
||||
// Just in time initialization
|
||||
if (functionName.length() == 0)
|
||||
{
|
||||
std::ostringstream errmsg;
|
||||
errmsg << "mcsv1Context::getFunction: " << functionName << " is empty";
|
||||
throw std::logic_error(errmsg.str());
|
||||
}
|
||||
|
||||
mcsv1sdk::UDAF_MAP::iterator funcIter = mcsv1sdk::UDAFMap::getMap().find(functionName);
|
||||
|
||||
if (funcIter == mcsv1sdk::UDAFMap::getMap().end())
|
||||
{
|
||||
std::ostringstream errmsg;
|
||||
errmsg << "mcsv1Context::getFunction: " << functionName << " is undefined";
|
||||
throw std::logic_error(errmsg.str());
|
||||
}
|
||||
|
||||
func = funcIter->second;
|
||||
return func;
|
||||
}
|
||||
|
||||
mcsv1sdk::mcsv1_UDAF* mcsv1Context::getFunction() const
|
||||
{
|
||||
return const_cast<mcsv1Context*>(this)->getFunction();
|
||||
return const_cast<mcsv1Context*>(this)->getFunction();
|
||||
}
|
||||
|
||||
void mcsv1Context::createUserData()
|
||||
void mcsv1Context::createUserData()
|
||||
{
|
||||
// Try the function. If not implemented, create a byte array.
|
||||
UserData* userData = NULL;
|
||||
mcsv1_UDAF::ReturnCode rc = getFunction()->createUserData(userData, fUserDataSize);
|
||||
if (rc == mcsv1_UDAF::ERROR)
|
||||
{
|
||||
std::ostringstream errmsg;
|
||||
errmsg << "mcsv1Context::createUserData: " << functionName << errorMsg.c_str();
|
||||
throw std::logic_error(errmsg.str());
|
||||
}
|
||||
setUserData(userData);
|
||||
// Try the function. If not implemented, create a byte array.
|
||||
UserData* userData = NULL;
|
||||
mcsv1_UDAF::ReturnCode rc = getFunction()->createUserData(userData, fUserDataSize);
|
||||
|
||||
if (rc == mcsv1_UDAF::ERROR)
|
||||
{
|
||||
std::ostringstream errmsg;
|
||||
errmsg << "mcsv1Context::createUserData: " << functionName << errorMsg.c_str();
|
||||
throw std::logic_error(errmsg.str());
|
||||
}
|
||||
|
||||
setUserData(userData);
|
||||
}
|
||||
|
||||
void mcsv1Context::serialize(messageqcpp::ByteStream& b) const
|
||||
{
|
||||
b.needAtLeast(sizeof(mcsv1Context));
|
||||
b << (ObjectReader::id_t) ObjectReader::MCSV1_CONTEXT;
|
||||
b << functionName;
|
||||
b << fRunFlags;
|
||||
// Dont send context flags, These are set for each call
|
||||
b << fUserDataSize;
|
||||
b << (uint32_t)fResultType;
|
||||
b << fResultscale;
|
||||
b << fResultPrecision;
|
||||
b << errorMsg;
|
||||
// Don't send dataflags. These are set for each call
|
||||
// bInterrupted is set internally.
|
||||
b << (uint32_t)fStartFrame;
|
||||
b << (uint32_t)fEndFrame;
|
||||
b << fStartConstant;
|
||||
b << fEndConstant;
|
||||
b.needAtLeast(sizeof(mcsv1Context));
|
||||
b << (ObjectReader::id_t) ObjectReader::MCSV1_CONTEXT;
|
||||
b << functionName;
|
||||
b << fRunFlags;
|
||||
// Dont send context flags, These are set for each call
|
||||
b << fUserDataSize;
|
||||
b << (uint32_t)fResultType;
|
||||
b << fResultscale;
|
||||
b << fResultPrecision;
|
||||
b << errorMsg;
|
||||
// Don't send dataflags. These are set for each call
|
||||
// bInterrupted is set internally.
|
||||
b << (uint32_t)fStartFrame;
|
||||
b << (uint32_t)fEndFrame;
|
||||
b << fStartConstant;
|
||||
b << fEndConstant;
|
||||
}
|
||||
|
||||
void mcsv1Context::unserialize(messageqcpp::ByteStream& b)
|
||||
{
|
||||
ObjectReader::checkType(b, ObjectReader::MCSV1_CONTEXT);
|
||||
b >> functionName;
|
||||
b >> fRunFlags;
|
||||
b >> fUserDataSize;
|
||||
uint32_t iResultType;
|
||||
b >> iResultType;
|
||||
fResultType = (CalpontSystemCatalog::ColDataType)iResultType;
|
||||
b >> fResultscale;
|
||||
b >> fResultPrecision;
|
||||
b >> errorMsg;
|
||||
uint32_t frame;
|
||||
b >> frame;
|
||||
fStartFrame = (WF_FRAME)frame;
|
||||
b >> frame;
|
||||
fEndFrame = (WF_FRAME)frame;
|
||||
b >> fStartConstant;
|
||||
b >> fEndConstant;
|
||||
ObjectReader::checkType(b, ObjectReader::MCSV1_CONTEXT);
|
||||
b >> functionName;
|
||||
b >> fRunFlags;
|
||||
b >> fUserDataSize;
|
||||
uint32_t iResultType;
|
||||
b >> iResultType;
|
||||
fResultType = (CalpontSystemCatalog::ColDataType)iResultType;
|
||||
b >> fResultscale;
|
||||
b >> fResultPrecision;
|
||||
b >> errorMsg;
|
||||
uint32_t frame;
|
||||
b >> frame;
|
||||
fStartFrame = (WF_FRAME)frame;
|
||||
b >> frame;
|
||||
fEndFrame = (WF_FRAME)frame;
|
||||
b >> fStartConstant;
|
||||
b >> fEndConstant;
|
||||
}
|
||||
|
||||
void UserData::serialize(messageqcpp::ByteStream& bs) const
|
||||
{
|
||||
bs << size;
|
||||
bs.append(data, size);
|
||||
bs << size;
|
||||
bs.append(data, size);
|
||||
}
|
||||
|
||||
void UserData::unserialize(messageqcpp::ByteStream& bs)
|
||||
{
|
||||
bs >> size;
|
||||
memcpy(data, bs.buf(), size);
|
||||
bs.advance(size);
|
||||
bs >> size;
|
||||
memcpy(data, bs.buf(), size);
|
||||
bs.advance(size);
|
||||
}
|
||||
|
||||
const std::string typeStr("");
|
||||
|
1155
utils/udfsdk/mcsv1_udaf.h
Executable file → Normal file
1155
utils/udfsdk/mcsv1_udaf.h
Executable file → Normal file
File diff suppressed because it is too large
Load Diff
501
utils/udfsdk/median.cpp
Executable file → Normal file
501
utils/udfsdk/median.cpp
Executable file → Normal file
@ -25,290 +25,307 @@
|
||||
using namespace mcsv1sdk;
|
||||
|
||||
mcsv1_UDAF::ReturnCode median::init(mcsv1Context* context,
|
||||
COL_TYPES& colTypes)
|
||||
COL_TYPES& colTypes)
|
||||
{
|
||||
if (colTypes.size() < 1)
|
||||
{
|
||||
// The error message will be prepended with
|
||||
// "The storage engine for the table doesn't support "
|
||||
context->setErrorMessage("median() with 0 arguments");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
if (colTypes.size() > 1)
|
||||
{
|
||||
context->setErrorMessage("median() with more than 1 argument");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
if (colTypes.size() < 1)
|
||||
{
|
||||
// The error message will be prepended with
|
||||
// "The storage engine for the table doesn't support "
|
||||
context->setErrorMessage("median() with 0 arguments");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
|
||||
if (!(isNumeric(colTypes[0].second)))
|
||||
{
|
||||
// The error message will be prepended with
|
||||
// "The storage engine for the table doesn't support "
|
||||
context->setErrorMessage("median() with non-numeric argument");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
if (colTypes.size() > 1)
|
||||
{
|
||||
context->setErrorMessage("median() with more than 1 argument");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
|
||||
context->setResultType(CalpontSystemCatalog::DOUBLE);
|
||||
context->setColWidth(8);
|
||||
context->setScale(context->getScale()*2);
|
||||
context->setPrecision(19);
|
||||
context->setRunFlag(mcsv1sdk::UDAF_IGNORE_NULLS);
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
if (!(isNumeric(colTypes[0].second)))
|
||||
{
|
||||
// The error message will be prepended with
|
||||
// "The storage engine for the table doesn't support "
|
||||
context->setErrorMessage("median() with non-numeric argument");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
|
||||
context->setResultType(CalpontSystemCatalog::DOUBLE);
|
||||
context->setColWidth(8);
|
||||
context->setScale(context->getScale() * 2);
|
||||
context->setPrecision(19);
|
||||
context->setRunFlag(mcsv1sdk::UDAF_IGNORE_NULLS);
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode median::reset(mcsv1Context* context)
|
||||
{
|
||||
MedianData* data = static_cast<MedianData*>(context->getUserData());
|
||||
data->mData.clear();
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
MedianData* data = static_cast<MedianData*>(context->getUserData());
|
||||
data->mData.clear();
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode median::nextValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsIn)
|
||||
mcsv1_UDAF::ReturnCode median::nextValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsIn)
|
||||
{
|
||||
static_any::any& valIn = valsIn[0].columnData;
|
||||
MEDIAN_DATA& data = static_cast<MedianData*>(context->getUserData())->mData;
|
||||
DATATYPE val = 0.0;
|
||||
static_any::any& valIn = valsIn[0].columnData;
|
||||
MEDIAN_DATA& data = static_cast<MedianData*>(context->getUserData())->mData;
|
||||
DATATYPE val = 0.0;
|
||||
|
||||
if (valIn.empty())
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS; // Ought not happen when UDAF_IGNORE_NULLS is on.
|
||||
}
|
||||
if (valIn.empty())
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS; // Ought not happen when UDAF_IGNORE_NULLS is on.
|
||||
}
|
||||
|
||||
if (valIn.compatible(charTypeId))
|
||||
{
|
||||
val = valIn.cast<char>();
|
||||
}
|
||||
else if (valIn.compatible(scharTypeId))
|
||||
{
|
||||
val = valIn.cast<signed char>();
|
||||
}
|
||||
else if (valIn.compatible(shortTypeId))
|
||||
{
|
||||
val = valIn.cast<short>();
|
||||
}
|
||||
else if (valIn.compatible(intTypeId))
|
||||
{
|
||||
val = valIn.cast<int>();
|
||||
}
|
||||
else if (valIn.compatible(longTypeId))
|
||||
{
|
||||
val = valIn.cast<long>();
|
||||
}
|
||||
else if (valIn.compatible(llTypeId))
|
||||
{
|
||||
val = valIn.cast<long long>();
|
||||
}
|
||||
else if (valIn.compatible(ucharTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned char>();
|
||||
}
|
||||
else if (valIn.compatible(ushortTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned short>();
|
||||
}
|
||||
else if (valIn.compatible(uintTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned int>();
|
||||
}
|
||||
else if (valIn.compatible(ulongTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long>();
|
||||
}
|
||||
else if (valIn.compatible(ullTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long long>();
|
||||
}
|
||||
else if (valIn.compatible(floatTypeId))
|
||||
{
|
||||
val = valIn.cast<float>();
|
||||
}
|
||||
else if (valIn.compatible(doubleTypeId))
|
||||
{
|
||||
val = valIn.cast<double>();
|
||||
}
|
||||
if (valIn.compatible(charTypeId))
|
||||
{
|
||||
val = valIn.cast<char>();
|
||||
}
|
||||
else if (valIn.compatible(scharTypeId))
|
||||
{
|
||||
val = valIn.cast<signed char>();
|
||||
}
|
||||
else if (valIn.compatible(shortTypeId))
|
||||
{
|
||||
val = valIn.cast<short>();
|
||||
}
|
||||
else if (valIn.compatible(intTypeId))
|
||||
{
|
||||
val = valIn.cast<int>();
|
||||
}
|
||||
else if (valIn.compatible(longTypeId))
|
||||
{
|
||||
val = valIn.cast<long>();
|
||||
}
|
||||
else if (valIn.compatible(llTypeId))
|
||||
{
|
||||
val = valIn.cast<long long>();
|
||||
}
|
||||
else if (valIn.compatible(ucharTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned char>();
|
||||
}
|
||||
else if (valIn.compatible(ushortTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned short>();
|
||||
}
|
||||
else if (valIn.compatible(uintTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned int>();
|
||||
}
|
||||
else if (valIn.compatible(ulongTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long>();
|
||||
}
|
||||
else if (valIn.compatible(ullTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long long>();
|
||||
}
|
||||
else if (valIn.compatible(floatTypeId))
|
||||
{
|
||||
val = valIn.cast<float>();
|
||||
}
|
||||
else if (valIn.compatible(doubleTypeId))
|
||||
{
|
||||
val = valIn.cast<double>();
|
||||
}
|
||||
|
||||
// For decimal types, we need to move the decimal point.
|
||||
uint32_t scale = valsIn[0].scale;
|
||||
if (val != 0 && scale > 0)
|
||||
{
|
||||
val /= pow(10.0, (double)scale);
|
||||
}
|
||||
data[val]++;
|
||||
// For decimal types, we need to move the decimal point.
|
||||
uint32_t scale = valsIn[0].scale;
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
if (val != 0 && scale > 0)
|
||||
{
|
||||
val /= pow(10.0, (double)scale);
|
||||
}
|
||||
|
||||
data[val]++;
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode median::subEvaluate(mcsv1Context* context, const UserData* userDataIn)
|
||||
{
|
||||
if (!userDataIn)
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
MEDIAN_DATA& outData = static_cast<MedianData*>(context->getUserData())->mData;
|
||||
const MEDIAN_DATA& inData = static_cast<const MedianData*>(userDataIn)->mData;
|
||||
MEDIAN_DATA::const_iterator iter = inData.begin();
|
||||
for (; iter != inData.end(); ++iter)
|
||||
{
|
||||
outData[iter->first] += iter->second;
|
||||
}
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
if (!userDataIn)
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
MEDIAN_DATA& outData = static_cast<MedianData*>(context->getUserData())->mData;
|
||||
const MEDIAN_DATA& inData = static_cast<const MedianData*>(userDataIn)->mData;
|
||||
MEDIAN_DATA::const_iterator iter = inData.begin();
|
||||
|
||||
for (; iter != inData.end(); ++iter)
|
||||
{
|
||||
outData[iter->first] += iter->second;
|
||||
}
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode median::evaluate(mcsv1Context* context, static_any::any& valOut)
|
||||
{
|
||||
uint64_t cnt1=0, cnt2=0;
|
||||
MEDIAN_DATA& data = static_cast<MedianData*>(context->getUserData())->mData;
|
||||
if (data.size() == 0)
|
||||
{
|
||||
valOut = (DATATYPE)0;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
MEDIAN_DATA::iterator iter(data.begin());
|
||||
MEDIAN_DATA::iterator revfrom(data.end());
|
||||
MEDIAN_DATA::reverse_iterator riter(revfrom);
|
||||
cnt1 += iter->second;
|
||||
cnt2 += riter->second;
|
||||
while (iter->first < riter->first)
|
||||
{
|
||||
while (cnt1 < cnt2 && iter->first < riter->first)
|
||||
{
|
||||
++iter;
|
||||
cnt1 += iter->second;
|
||||
}
|
||||
while (cnt2 < cnt1 &&iter->first < riter->first)
|
||||
{
|
||||
++riter;
|
||||
cnt2 += riter->second;
|
||||
}
|
||||
while (cnt1 == cnt2 && iter->first < riter->first)
|
||||
{
|
||||
++iter;
|
||||
cnt1 += iter->second;
|
||||
if (iter->first > riter->first)
|
||||
{
|
||||
break;
|
||||
}
|
||||
++riter;
|
||||
cnt2 += riter->second;
|
||||
}
|
||||
}
|
||||
valOut = (iter->first + riter->first) / 2;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
uint64_t cnt1 = 0, cnt2 = 0;
|
||||
MEDIAN_DATA& data = static_cast<MedianData*>(context->getUserData())->mData;
|
||||
|
||||
if (data.size() == 0)
|
||||
{
|
||||
valOut = (DATATYPE)0;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
MEDIAN_DATA::iterator iter(data.begin());
|
||||
MEDIAN_DATA::iterator revfrom(data.end());
|
||||
MEDIAN_DATA::reverse_iterator riter(revfrom);
|
||||
cnt1 += iter->second;
|
||||
cnt2 += riter->second;
|
||||
|
||||
while (iter->first < riter->first)
|
||||
{
|
||||
while (cnt1 < cnt2 && iter->first < riter->first)
|
||||
{
|
||||
++iter;
|
||||
cnt1 += iter->second;
|
||||
}
|
||||
|
||||
while (cnt2 < cnt1 && iter->first < riter->first)
|
||||
{
|
||||
++riter;
|
||||
cnt2 += riter->second;
|
||||
}
|
||||
|
||||
while (cnt1 == cnt2 && iter->first < riter->first)
|
||||
{
|
||||
++iter;
|
||||
cnt1 += iter->second;
|
||||
|
||||
if (iter->first > riter->first)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
++riter;
|
||||
cnt2 += riter->second;
|
||||
}
|
||||
}
|
||||
|
||||
valOut = (iter->first + riter->first) / 2;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode median::dropValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsDropped)
|
||||
mcsv1_UDAF::ReturnCode median::dropValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsDropped)
|
||||
{
|
||||
static_any::any& valIn = valsDropped[0].columnData;
|
||||
MEDIAN_DATA& data = static_cast<MedianData*>(context->getUserData())->mData;
|
||||
DATATYPE val = 0.0;
|
||||
static_any::any& valIn = valsDropped[0].columnData;
|
||||
MEDIAN_DATA& data = static_cast<MedianData*>(context->getUserData())->mData;
|
||||
DATATYPE val = 0.0;
|
||||
|
||||
if (valIn.empty())
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS; // Ought not happen when UDAF_IGNORE_NULLS is on.
|
||||
}
|
||||
if (valIn.empty())
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS; // Ought not happen when UDAF_IGNORE_NULLS is on.
|
||||
}
|
||||
|
||||
if (valIn.compatible(charTypeId))
|
||||
{
|
||||
val = valIn.cast<char>();
|
||||
}
|
||||
else if (valIn.compatible(scharTypeId))
|
||||
{
|
||||
val = valIn.cast<signed char>();
|
||||
}
|
||||
else if (valIn.compatible(shortTypeId))
|
||||
{
|
||||
val = valIn.cast<short>();
|
||||
}
|
||||
else if (valIn.compatible(intTypeId))
|
||||
{
|
||||
val = valIn.cast<int>();
|
||||
}
|
||||
else if (valIn.compatible(longTypeId))
|
||||
{
|
||||
val = valIn.cast<long>();
|
||||
}
|
||||
else if (valIn.compatible(llTypeId))
|
||||
{
|
||||
val = valIn.cast<long long>();
|
||||
}
|
||||
else if (valIn.compatible(ucharTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned char>();
|
||||
}
|
||||
else if (valIn.compatible(ushortTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned short>();
|
||||
}
|
||||
else if (valIn.compatible(uintTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned int>();
|
||||
}
|
||||
else if (valIn.compatible(ulongTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long>();
|
||||
}
|
||||
else if (valIn.compatible(ullTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long long>();
|
||||
}
|
||||
else if (valIn.compatible(floatTypeId))
|
||||
{
|
||||
val = valIn.cast<float>();
|
||||
}
|
||||
else if (valIn.compatible(doubleTypeId))
|
||||
{
|
||||
val = valIn.cast<double>();
|
||||
}
|
||||
if (valIn.compatible(charTypeId))
|
||||
{
|
||||
val = valIn.cast<char>();
|
||||
}
|
||||
else if (valIn.compatible(scharTypeId))
|
||||
{
|
||||
val = valIn.cast<signed char>();
|
||||
}
|
||||
else if (valIn.compatible(shortTypeId))
|
||||
{
|
||||
val = valIn.cast<short>();
|
||||
}
|
||||
else if (valIn.compatible(intTypeId))
|
||||
{
|
||||
val = valIn.cast<int>();
|
||||
}
|
||||
else if (valIn.compatible(longTypeId))
|
||||
{
|
||||
val = valIn.cast<long>();
|
||||
}
|
||||
else if (valIn.compatible(llTypeId))
|
||||
{
|
||||
val = valIn.cast<long long>();
|
||||
}
|
||||
else if (valIn.compatible(ucharTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned char>();
|
||||
}
|
||||
else if (valIn.compatible(ushortTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned short>();
|
||||
}
|
||||
else if (valIn.compatible(uintTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned int>();
|
||||
}
|
||||
else if (valIn.compatible(ulongTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long>();
|
||||
}
|
||||
else if (valIn.compatible(ullTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long long>();
|
||||
}
|
||||
else if (valIn.compatible(floatTypeId))
|
||||
{
|
||||
val = valIn.cast<float>();
|
||||
}
|
||||
else if (valIn.compatible(doubleTypeId))
|
||||
{
|
||||
val = valIn.cast<double>();
|
||||
}
|
||||
|
||||
// For decimal types, we need to move the decimal point.
|
||||
uint32_t scale = valsDropped[0].scale;
|
||||
if (val != 0 && scale > 0)
|
||||
{
|
||||
val /= pow(10.0, (double)scale);
|
||||
}
|
||||
// For decimal types, we need to move the decimal point.
|
||||
uint32_t scale = valsDropped[0].scale;
|
||||
|
||||
data[val]--;
|
||||
if (val != 0 && scale > 0)
|
||||
{
|
||||
val /= pow(10.0, (double)scale);
|
||||
}
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
data[val]--;
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode median::createUserData(UserData*& userData, int32_t& length)
|
||||
{
|
||||
userData = new MedianData;
|
||||
length = sizeof(MedianData);
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
userData = new MedianData;
|
||||
length = sizeof(MedianData);
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
void MedianData::serialize(messageqcpp::ByteStream& bs) const
|
||||
{
|
||||
MEDIAN_DATA::const_iterator iter = mData.begin();
|
||||
DATATYPE num;
|
||||
uint32_t cnt;
|
||||
bs << (int32_t)mData.size();
|
||||
for (; iter != mData.end(); ++iter)
|
||||
{
|
||||
num = iter->first;
|
||||
bs << num;
|
||||
cnt = iter->second;
|
||||
bs << cnt;
|
||||
}
|
||||
MEDIAN_DATA::const_iterator iter = mData.begin();
|
||||
DATATYPE num;
|
||||
uint32_t cnt;
|
||||
bs << (int32_t)mData.size();
|
||||
|
||||
for (; iter != mData.end(); ++iter)
|
||||
{
|
||||
num = iter->first;
|
||||
bs << num;
|
||||
cnt = iter->second;
|
||||
bs << cnt;
|
||||
}
|
||||
}
|
||||
|
||||
void MedianData::unserialize(messageqcpp::ByteStream& bs)
|
||||
{
|
||||
mData.clear();
|
||||
int32_t sz;
|
||||
DATATYPE num;
|
||||
uint32_t cnt;
|
||||
bs >> sz;
|
||||
for (int i = 0; i < sz; ++i)
|
||||
{
|
||||
bs >> num;
|
||||
bs >> cnt;
|
||||
mData[num] = cnt;
|
||||
}
|
||||
mData.clear();
|
||||
int32_t sz;
|
||||
DATATYPE num;
|
||||
uint32_t cnt;
|
||||
bs >> sz;
|
||||
|
||||
for (int i = 0; i < sz; ++i)
|
||||
{
|
||||
bs >> num;
|
||||
bs >> cnt;
|
||||
mData[num] = cnt;
|
||||
}
|
||||
}
|
||||
|
||||
|
380
utils/udfsdk/median.h
Executable file → Normal file
380
utils/udfsdk/median.h
Executable file → Normal file
@ -21,34 +21,34 @@
|
||||
* mcsv1_UDAF.h
|
||||
***********************************************************************/
|
||||
|
||||
/**
|
||||
* Columnstore interface for writing a User Defined Aggregate
|
||||
* Functions (UDAF) and User Defined Analytic Functions (UDAnF)
|
||||
* or a function that can act as either - UDA(n)F
|
||||
*
|
||||
/**
|
||||
* Columnstore interface for writing a User Defined Aggregate
|
||||
* Functions (UDAF) and User Defined Analytic Functions (UDAnF)
|
||||
* or a function that can act as either - UDA(n)F
|
||||
*
|
||||
* The basic steps are:
|
||||
*
|
||||
* 1. Create a the UDA(n)F function interface in some .h file.
|
||||
* 2. Create the UDF function implementation in some .cpp file
|
||||
* 3. Create the connector stub (MariaDB UDAF definition) for
|
||||
* this UDF function.
|
||||
* 4. build the dynamic library using all of the source.
|
||||
* 5 Put the library in $COLUMNSTORE_INSTALL/lib of
|
||||
* all modules
|
||||
* 6. restart the Columnstore system.
|
||||
* 1. Create a the UDA(n)F function interface in some .h file.
|
||||
* 2. Create the UDF function implementation in some .cpp file
|
||||
* 3. Create the connector stub (MariaDB UDAF definition) for
|
||||
* this UDF function.
|
||||
* 4. build the dynamic library using all of the source.
|
||||
* 5 Put the library in $COLUMNSTORE_INSTALL/lib of
|
||||
* all modules
|
||||
* 6. restart the Columnstore system.
|
||||
* 7. notify mysqld about the new function:
|
||||
*
|
||||
*
|
||||
* CREATE AGGREGATE FUNCTION median returns REAL soname
|
||||
* 'libudf_mysql.so';
|
||||
*
|
||||
* The UDAF functions may run distributed in the Columnstore
|
||||
* engine. UDAnF do not run distributed.
|
||||
*
|
||||
* UDAF is User Defined Aggregate Function.
|
||||
* UDAnF is User Defined Analytic Function.
|
||||
* UDA(n)F is an acronym for a function that could be either. It
|
||||
* is also used to describe the interface that is used for
|
||||
* either.
|
||||
*
|
||||
* The UDAF functions may run distributed in the Columnstore
|
||||
* engine. UDAnF do not run distributed.
|
||||
*
|
||||
* UDAF is User Defined Aggregate Function.
|
||||
* UDAnF is User Defined Analytic Function.
|
||||
* UDA(n)F is an acronym for a function that could be either. It
|
||||
* is also used to describe the interface that is used for
|
||||
* either.
|
||||
*/
|
||||
#ifndef HEADER_median
|
||||
#define HEADER_median
|
||||
@ -83,196 +83,196 @@ typedef std::map<DATATYPE, uint32_t> MEDIAN_DATA;
|
||||
// Override UserData for data storage
|
||||
struct MedianData : public UserData
|
||||
{
|
||||
MedianData() {};
|
||||
MedianData() {};
|
||||
|
||||
virtual ~MedianData(){}
|
||||
virtual ~MedianData() {}
|
||||
|
||||
virtual void serialize(messageqcpp::ByteStream& bs) const;
|
||||
virtual void unserialize(messageqcpp::ByteStream& bs);
|
||||
virtual void serialize(messageqcpp::ByteStream& bs) const;
|
||||
virtual void unserialize(messageqcpp::ByteStream& bs);
|
||||
|
||||
MEDIAN_DATA mData;
|
||||
MEDIAN_DATA mData;
|
||||
private:
|
||||
// For now, copy construction is unwanted
|
||||
MedianData(UserData&);
|
||||
// For now, copy construction is unwanted
|
||||
MedianData(UserData&);
|
||||
};
|
||||
|
||||
// Override mcsv1_UDAF to build your User Defined Aggregate (UDAF) and/or
|
||||
// Override mcsv1_UDAF to build your User Defined Aggregate (UDAF) and/or
|
||||
// User Defined Analytic Function (UDAnF).
|
||||
// These will be singleton classes, so don't put any instance
|
||||
// specific data in here. All instance data is stored in mcsv1Context
|
||||
// passed to each user function and retrieved by the getUserData() method.
|
||||
//
|
||||
// Each API function returns a ReturnCode. If ERROR is returned at any time,
|
||||
// the query is aborted, getInterrupted() will begin to return true and the
|
||||
// message set in config->setErrorMessage() is returned to MariaDB.
|
||||
//
|
||||
// Each API function returns a ReturnCode. If ERROR is returned at any time,
|
||||
// the query is aborted, getInterrupted() will begin to return true and the
|
||||
// message set in config->setErrorMessage() is returned to MariaDB.
|
||||
|
||||
// Return the median value of the dataset
|
||||
|
||||
class median : public mcsv1_UDAF
|
||||
{
|
||||
public:
|
||||
// Defaults OK
|
||||
median() : mcsv1_UDAF(){};
|
||||
virtual ~median(){};
|
||||
// Defaults OK
|
||||
median() : mcsv1_UDAF() {};
|
||||
virtual ~median() {};
|
||||
|
||||
/**
|
||||
* init()
|
||||
*
|
||||
* Mandatory. Implement this to initialize flags and instance
|
||||
* data. Called once per SQL statement. You can do any sanity
|
||||
* checks here.
|
||||
*
|
||||
* colTypes (in) - A vector of ColDataType defining the
|
||||
* parameters of the UDA(n)F call. These can be used to decide
|
||||
* to override the default return type. If desired, the new
|
||||
* return type can be set by context->setReturnType() and
|
||||
* decimal scale and precision can be set by context->setScale
|
||||
* and context->setPrecision respectively.
|
||||
*
|
||||
* Return mcsv1_UDAF::ERROR on any error, such as non-compatible
|
||||
* colTypes or wrong number of arguments. Else return
|
||||
* mcsv1_UDAF::SUCCESS.
|
||||
*/
|
||||
virtual ReturnCode init(mcsv1Context* context,
|
||||
COL_TYPES& colTypes);
|
||||
/**
|
||||
* init()
|
||||
*
|
||||
* Mandatory. Implement this to initialize flags and instance
|
||||
* data. Called once per SQL statement. You can do any sanity
|
||||
* checks here.
|
||||
*
|
||||
* colTypes (in) - A vector of ColDataType defining the
|
||||
* parameters of the UDA(n)F call. These can be used to decide
|
||||
* to override the default return type. If desired, the new
|
||||
* return type can be set by context->setReturnType() and
|
||||
* decimal scale and precision can be set by context->setScale
|
||||
* and context->setPrecision respectively.
|
||||
*
|
||||
* Return mcsv1_UDAF::ERROR on any error, such as non-compatible
|
||||
* colTypes or wrong number of arguments. Else return
|
||||
* mcsv1_UDAF::SUCCESS.
|
||||
*/
|
||||
virtual ReturnCode init(mcsv1Context* context,
|
||||
COL_TYPES& colTypes);
|
||||
|
||||
/**
|
||||
* reset()
|
||||
*
|
||||
* Mandatory. Reset the UDA(n)F for a new group, partition or,
|
||||
* in some cases, new Window Frame. Do not free any memory
|
||||
* allocated by context->setUserDataSize(). The SDK Framework owns
|
||||
* that memory and will handle that. Use this opportunity to
|
||||
* reset any variables in context->getUserData() needed for the
|
||||
* next aggregation. May be called multiple times if running in
|
||||
* a ditributed fashion.
|
||||
*
|
||||
* Use this opportunity to initialize the userData.
|
||||
*/
|
||||
virtual ReturnCode reset(mcsv1Context* context);
|
||||
/**
|
||||
* reset()
|
||||
*
|
||||
* Mandatory. Reset the UDA(n)F for a new group, partition or,
|
||||
* in some cases, new Window Frame. Do not free any memory
|
||||
* allocated by context->setUserDataSize(). The SDK Framework owns
|
||||
* that memory and will handle that. Use this opportunity to
|
||||
* reset any variables in context->getUserData() needed for the
|
||||
* next aggregation. May be called multiple times if running in
|
||||
* a ditributed fashion.
|
||||
*
|
||||
* Use this opportunity to initialize the userData.
|
||||
*/
|
||||
virtual ReturnCode reset(mcsv1Context* context);
|
||||
|
||||
/**
|
||||
* nextValue()
|
||||
*
|
||||
* Mandatory. Handle a single row.
|
||||
*
|
||||
* colsIn - A vector of data structure describing the input
|
||||
* data.
|
||||
*
|
||||
* This function is called once for every row in the filtered
|
||||
* result set (before aggregation). It is very important that
|
||||
* this function is efficient.
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, nextValue
|
||||
* cannot depend on order, as it will only be called for each
|
||||
* row found on the specific PM.
|
||||
*
|
||||
* valsIn (in) - a vector of the parameters from the row.
|
||||
*/
|
||||
virtual ReturnCode nextValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsIn);
|
||||
/**
|
||||
* nextValue()
|
||||
*
|
||||
* Mandatory. Handle a single row.
|
||||
*
|
||||
* colsIn - A vector of data structure describing the input
|
||||
* data.
|
||||
*
|
||||
* This function is called once for every row in the filtered
|
||||
* result set (before aggregation). It is very important that
|
||||
* this function is efficient.
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, nextValue
|
||||
* cannot depend on order, as it will only be called for each
|
||||
* row found on the specific PM.
|
||||
*
|
||||
* valsIn (in) - a vector of the parameters from the row.
|
||||
*/
|
||||
virtual ReturnCode nextValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsIn);
|
||||
|
||||
/**
|
||||
* subEvaluate()
|
||||
*
|
||||
* Mandatory -- Called if the UDAF is running in a distributed
|
||||
* fashion. Columnstore tries to run all aggregate functions
|
||||
* distributed, depending on context.
|
||||
*
|
||||
* Perform an aggregation on rows partially aggregated by
|
||||
* nextValue. Columnstore calls nextValue for each row on a
|
||||
* given PM for a group (GROUP BY). subEvaluate is called on the
|
||||
* UM to consolodate those values into a single instance of
|
||||
* userData. Keep your aggregated totals in context's userData.
|
||||
* The first time this is called for a group, reset() would have
|
||||
* been called with this version of userData.
|
||||
*
|
||||
* Called for every partial data set in each group in GROUP BY.
|
||||
*
|
||||
* When subEvaluate has been called for all subAggregated data
|
||||
* sets, Evaluate will be called with the same context as here.
|
||||
*
|
||||
* valIn (In) - This is a pointer to a memory block of the size
|
||||
* set in setUserDataSize. It will contain the value of userData
|
||||
* as seen in the last call to NextValue for a given PM.
|
||||
*
|
||||
*/
|
||||
virtual ReturnCode subEvaluate(mcsv1Context* context, const UserData* valIn);
|
||||
/**
|
||||
* subEvaluate()
|
||||
*
|
||||
* Mandatory -- Called if the UDAF is running in a distributed
|
||||
* fashion. Columnstore tries to run all aggregate functions
|
||||
* distributed, depending on context.
|
||||
*
|
||||
* Perform an aggregation on rows partially aggregated by
|
||||
* nextValue. Columnstore calls nextValue for each row on a
|
||||
* given PM for a group (GROUP BY). subEvaluate is called on the
|
||||
* UM to consolodate those values into a single instance of
|
||||
* userData. Keep your aggregated totals in context's userData.
|
||||
* The first time this is called for a group, reset() would have
|
||||
* been called with this version of userData.
|
||||
*
|
||||
* Called for every partial data set in each group in GROUP BY.
|
||||
*
|
||||
* When subEvaluate has been called for all subAggregated data
|
||||
* sets, Evaluate will be called with the same context as here.
|
||||
*
|
||||
* valIn (In) - This is a pointer to a memory block of the size
|
||||
* set in setUserDataSize. It will contain the value of userData
|
||||
* as seen in the last call to NextValue for a given PM.
|
||||
*
|
||||
*/
|
||||
virtual ReturnCode subEvaluate(mcsv1Context* context, const UserData* valIn);
|
||||
|
||||
/**
|
||||
* evaluate()
|
||||
*
|
||||
* Mandatory. Get the aggregated value.
|
||||
*
|
||||
* Called for every new group if UDAF GROUP BY, UDAnF partition
|
||||
* or, in some cases, new Window Frame.
|
||||
*
|
||||
* Set the aggregated value into valOut. The datatype is assumed
|
||||
* to be the same as that set in the init() function;
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, evaluate is
|
||||
* called after a series of subEvaluate calls.
|
||||
*
|
||||
* valOut (out) - Set the aggregated value here. The datatype is
|
||||
* assumed to be the same as that set in the init() function;
|
||||
*
|
||||
* To return a NULL value, don't assign to valOut.
|
||||
*/
|
||||
virtual ReturnCode evaluate(mcsv1Context* context, static_any::any& valOut);
|
||||
/**
|
||||
* evaluate()
|
||||
*
|
||||
* Mandatory. Get the aggregated value.
|
||||
*
|
||||
* Called for every new group if UDAF GROUP BY, UDAnF partition
|
||||
* or, in some cases, new Window Frame.
|
||||
*
|
||||
* Set the aggregated value into valOut. The datatype is assumed
|
||||
* to be the same as that set in the init() function;
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, evaluate is
|
||||
* called after a series of subEvaluate calls.
|
||||
*
|
||||
* valOut (out) - Set the aggregated value here. The datatype is
|
||||
* assumed to be the same as that set in the init() function;
|
||||
*
|
||||
* To return a NULL value, don't assign to valOut.
|
||||
*/
|
||||
virtual ReturnCode evaluate(mcsv1Context* context, static_any::any& valOut);
|
||||
|
||||
/**
|
||||
* dropValue()
|
||||
*
|
||||
* Optional -- If defined, the server will call this instead of
|
||||
* reset for UDAnF.
|
||||
*
|
||||
* Don't implement if a UDAnF has one or more of the following:
|
||||
* The UDAnF can't be used with a Window Frame
|
||||
* The UDAnF is not reversable in some way
|
||||
* The UDAnF is not interested in optimal performance
|
||||
*
|
||||
* If not implemented, reset() followed by a series of
|
||||
* nextValue() will be called for each movement of the Window
|
||||
* Frame.
|
||||
*
|
||||
* If implemented, then each movement of the Window Frame will
|
||||
* result in dropValue() being called for each row falling out
|
||||
* of the Frame and nextValue() being called for each new row
|
||||
* coming into the Frame.
|
||||
*
|
||||
* valsDropped (in) - a vector of the parameters from the row
|
||||
* leaving the Frame
|
||||
*
|
||||
* dropValue() will not be called for unbounded/current row type
|
||||
* frames, as those are already optimized.
|
||||
*/
|
||||
virtual ReturnCode dropValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsDropped);
|
||||
/**
|
||||
* dropValue()
|
||||
*
|
||||
* Optional -- If defined, the server will call this instead of
|
||||
* reset for UDAnF.
|
||||
*
|
||||
* Don't implement if a UDAnF has one or more of the following:
|
||||
* The UDAnF can't be used with a Window Frame
|
||||
* The UDAnF is not reversable in some way
|
||||
* The UDAnF is not interested in optimal performance
|
||||
*
|
||||
* If not implemented, reset() followed by a series of
|
||||
* nextValue() will be called for each movement of the Window
|
||||
* Frame.
|
||||
*
|
||||
* If implemented, then each movement of the Window Frame will
|
||||
* result in dropValue() being called for each row falling out
|
||||
* of the Frame and nextValue() being called for each new row
|
||||
* coming into the Frame.
|
||||
*
|
||||
* valsDropped (in) - a vector of the parameters from the row
|
||||
* leaving the Frame
|
||||
*
|
||||
* dropValue() will not be called for unbounded/current row type
|
||||
* frames, as those are already optimized.
|
||||
*/
|
||||
virtual ReturnCode dropValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsDropped);
|
||||
|
||||
/**
|
||||
* createUserData()
|
||||
*
|
||||
* Optional -- If defined, the server will call this instead of
|
||||
* createUserData on context.
|
||||
*
|
||||
* Create your variable length data structure via
|
||||
* data = new <datatype>
|
||||
*
|
||||
* The data structure may contain references to containers or
|
||||
* pointers to other objects. Remember that for distributed
|
||||
* processing, this may be called multiple times for variaous
|
||||
* computing blocks. At the least, it will be called once per PM
|
||||
* that processes the data, and once more for the UM. For UDAnF,
|
||||
* it may only be called once.
|
||||
*
|
||||
* Set length to the length of the data structure you create.
|
||||
*
|
||||
* For each call to createUserData(), there will be a
|
||||
* corresponding deleteUserData() where you must clean up. Any
|
||||
* memory leaks are your fault.
|
||||
*
|
||||
*/
|
||||
virtual ReturnCode createUserData(UserData*& data, int32_t& length);
|
||||
/**
|
||||
* createUserData()
|
||||
*
|
||||
* Optional -- If defined, the server will call this instead of
|
||||
* createUserData on context.
|
||||
*
|
||||
* Create your variable length data structure via
|
||||
* data = new <datatype>
|
||||
*
|
||||
* The data structure may contain references to containers or
|
||||
* pointers to other objects. Remember that for distributed
|
||||
* processing, this may be called multiple times for variaous
|
||||
* computing blocks. At the least, it will be called once per PM
|
||||
* that processes the data, and once more for the UM. For UDAnF,
|
||||
* it may only be called once.
|
||||
*
|
||||
* Set length to the length of the data structure you create.
|
||||
*
|
||||
* For each call to createUserData(), there will be a
|
||||
* corresponding deleteUserData() where you must clean up. Any
|
||||
* memory leaks are your fault.
|
||||
*
|
||||
*/
|
||||
virtual ReturnCode createUserData(UserData*& data, int32_t& length);
|
||||
protected:
|
||||
};
|
||||
|
||||
|
@ -1,14 +1,14 @@
|
||||
//{{NO_DEPENDENCIES}}
|
||||
// Microsoft Visual C++ generated include file.
|
||||
// Used by libudfsdk-ent.rc
|
||||
|
||||
// Next default values for new objects
|
||||
//
|
||||
#ifdef APSTUDIO_INVOKED
|
||||
#ifndef APSTUDIO_READONLY_SYMBOLS
|
||||
#define _APS_NEXT_RESOURCE_VALUE 101
|
||||
#define _APS_NEXT_COMMAND_VALUE 40001
|
||||
#define _APS_NEXT_CONTROL_VALUE 1001
|
||||
#define _APS_NEXT_SYMED_VALUE 101
|
||||
#endif
|
||||
#endif
|
||||
//{{NO_DEPENDENCIES}}
|
||||
// Microsoft Visual C++ generated include file.
|
||||
// Used by libudfsdk-ent.rc
|
||||
|
||||
// Next default values for new objects
|
||||
//
|
||||
#ifdef APSTUDIO_INVOKED
|
||||
#ifndef APSTUDIO_READONLY_SYMBOLS
|
||||
#define _APS_NEXT_RESOURCE_VALUE 101
|
||||
#define _APS_NEXT_COMMAND_VALUE 40001
|
||||
#define _APS_NEXT_CONTROL_VALUE 1001
|
||||
#define _APS_NEXT_SYMED_VALUE 101
|
||||
#endif
|
||||
#endif
|
||||
|
385
utils/udfsdk/ssq.cpp
Executable file → Normal file
385
utils/udfsdk/ssq.cpp
Executable file → Normal file
@ -27,225 +27,236 @@ using namespace mcsv1sdk;
|
||||
|
||||
struct ssq_data
|
||||
{
|
||||
uint64_t scale;
|
||||
uint64_t scale;
|
||||
DATATYPE sumsq;
|
||||
ssq_data() : scale(0){}
|
||||
ssq_data() : scale(0) {}
|
||||
};
|
||||
|
||||
#define OUT_TYPE int64_t
|
||||
mcsv1_UDAF::ReturnCode ssq::init(mcsv1Context* context,
|
||||
COL_TYPES& colTypes)
|
||||
COL_TYPES& colTypes)
|
||||
{
|
||||
if (colTypes.size() < 1)
|
||||
{
|
||||
// The error message will be prepended with
|
||||
// "The storage engine for the table doesn't support "
|
||||
context->setErrorMessage("ssq() with 0 arguments");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
if (colTypes.size() > 1)
|
||||
{
|
||||
context->setErrorMessage("ssq() with more than 1 argument");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
if (colTypes.size() < 1)
|
||||
{
|
||||
// The error message will be prepended with
|
||||
// "The storage engine for the table doesn't support "
|
||||
context->setErrorMessage("ssq() with 0 arguments");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
|
||||
if (!(isNumeric(colTypes[0].second)))
|
||||
{
|
||||
// The error message will be prepended with
|
||||
// "The storage engine for the table doesn't support "
|
||||
context->setErrorMessage("ssq() with non-numeric argument");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
if (colTypes.size() > 1)
|
||||
{
|
||||
context->setErrorMessage("ssq() with more than 1 argument");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
|
||||
context->setUserDataSize(sizeof(ssq_data));
|
||||
context->setResultType(CalpontSystemCatalog::DOUBLE);
|
||||
context->setColWidth(8);
|
||||
context->setScale(context->getScale()*2);
|
||||
context->setPrecision(19);
|
||||
context->setRunFlag(mcsv1sdk::UDAF_IGNORE_NULLS);
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
if (!(isNumeric(colTypes[0].second)))
|
||||
{
|
||||
// The error message will be prepended with
|
||||
// "The storage engine for the table doesn't support "
|
||||
context->setErrorMessage("ssq() with non-numeric argument");
|
||||
return mcsv1_UDAF::ERROR;
|
||||
}
|
||||
|
||||
context->setUserDataSize(sizeof(ssq_data));
|
||||
context->setResultType(CalpontSystemCatalog::DOUBLE);
|
||||
context->setColWidth(8);
|
||||
context->setScale(context->getScale() * 2);
|
||||
context->setPrecision(19);
|
||||
context->setRunFlag(mcsv1sdk::UDAF_IGNORE_NULLS);
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode ssq::reset(mcsv1Context* context)
|
||||
{
|
||||
struct ssq_data* data = (struct ssq_data*)context->getUserData()->data;
|
||||
if (data)
|
||||
{
|
||||
data->scale = 0;
|
||||
data->sumsq = 0;
|
||||
}
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
struct ssq_data* data = (struct ssq_data*)context->getUserData()->data;
|
||||
|
||||
if (data)
|
||||
{
|
||||
data->scale = 0;
|
||||
data->sumsq = 0;
|
||||
}
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode ssq::nextValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsIn)
|
||||
mcsv1_UDAF::ReturnCode ssq::nextValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsIn)
|
||||
{
|
||||
static_any::any& valIn = valsIn[0].columnData;
|
||||
struct ssq_data* data = (struct ssq_data*)context->getUserData()->data;
|
||||
DATATYPE val = 0.0;
|
||||
static_any::any& valIn = valsIn[0].columnData;
|
||||
struct ssq_data* data = (struct ssq_data*)context->getUserData()->data;
|
||||
DATATYPE val = 0.0;
|
||||
|
||||
if (context->isParamNull(0) || valIn.empty())
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
if (context->isParamNull(0) || valIn.empty())
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
if (valIn.compatible(charTypeId))
|
||||
{
|
||||
val = valIn.cast<char>();
|
||||
}
|
||||
else if (valIn.compatible(scharTypeId))
|
||||
{
|
||||
val = valIn.cast<signed char>();
|
||||
}
|
||||
else if (valIn.compatible(shortTypeId))
|
||||
{
|
||||
val = valIn.cast<short>();
|
||||
}
|
||||
else if (valIn.compatible(intTypeId))
|
||||
{
|
||||
val = valIn.cast<int>();
|
||||
}
|
||||
else if (valIn.compatible(longTypeId))
|
||||
{
|
||||
val = valIn.cast<long>();
|
||||
}
|
||||
else if (valIn.compatible(llTypeId))
|
||||
{
|
||||
val = valIn.cast<long long>();
|
||||
}
|
||||
else if (valIn.compatible(ucharTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned char>();
|
||||
}
|
||||
else if (valIn.compatible(ushortTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned short>();
|
||||
}
|
||||
else if (valIn.compatible(uintTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned int>();
|
||||
}
|
||||
else if (valIn.compatible(ulongTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long>();
|
||||
}
|
||||
else if (valIn.compatible(ullTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long long>();
|
||||
}
|
||||
else if (valIn.compatible(floatTypeId))
|
||||
{
|
||||
val = valIn.cast<float>();
|
||||
}
|
||||
else if (valIn.compatible(doubleTypeId))
|
||||
{
|
||||
val = valIn.cast<double>();
|
||||
}
|
||||
if (valIn.compatible(charTypeId))
|
||||
{
|
||||
val = valIn.cast<char>();
|
||||
}
|
||||
else if (valIn.compatible(scharTypeId))
|
||||
{
|
||||
val = valIn.cast<signed char>();
|
||||
}
|
||||
else if (valIn.compatible(shortTypeId))
|
||||
{
|
||||
val = valIn.cast<short>();
|
||||
}
|
||||
else if (valIn.compatible(intTypeId))
|
||||
{
|
||||
val = valIn.cast<int>();
|
||||
}
|
||||
else if (valIn.compatible(longTypeId))
|
||||
{
|
||||
val = valIn.cast<long>();
|
||||
}
|
||||
else if (valIn.compatible(llTypeId))
|
||||
{
|
||||
val = valIn.cast<long long>();
|
||||
}
|
||||
else if (valIn.compatible(ucharTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned char>();
|
||||
}
|
||||
else if (valIn.compatible(ushortTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned short>();
|
||||
}
|
||||
else if (valIn.compatible(uintTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned int>();
|
||||
}
|
||||
else if (valIn.compatible(ulongTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long>();
|
||||
}
|
||||
else if (valIn.compatible(ullTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long long>();
|
||||
}
|
||||
else if (valIn.compatible(floatTypeId))
|
||||
{
|
||||
val = valIn.cast<float>();
|
||||
}
|
||||
else if (valIn.compatible(doubleTypeId))
|
||||
{
|
||||
val = valIn.cast<double>();
|
||||
}
|
||||
|
||||
// For decimal types, we need to move the decimal point.
|
||||
uint32_t scale = valsIn[0].scale;
|
||||
if (val != 0 && scale > 0)
|
||||
{
|
||||
val /= pow(10.0, (double)scale);
|
||||
}
|
||||
data->sumsq += val*val;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
// For decimal types, we need to move the decimal point.
|
||||
uint32_t scale = valsIn[0].scale;
|
||||
|
||||
if (val != 0 && scale > 0)
|
||||
{
|
||||
val /= pow(10.0, (double)scale);
|
||||
}
|
||||
|
||||
data->sumsq += val * val;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode ssq::subEvaluate(mcsv1Context* context, const UserData* userDataIn)
|
||||
{
|
||||
// If we turn off UDAF_IGNORE_NULLS in init(), then NULLS may be sent here in cases of Joins.
|
||||
// When a NULL value is sent here, userDataIn will be NULL, so check for NULLS.
|
||||
if (context->isParamNull(0))
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
struct ssq_data* outData = (struct ssq_data*)context->getUserData()->data;
|
||||
struct ssq_data* inData = (struct ssq_data*)userDataIn->data;
|
||||
outData->sumsq += inData->sumsq;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
// If we turn off UDAF_IGNORE_NULLS in init(), then NULLS may be sent here in cases of Joins.
|
||||
// When a NULL value is sent here, userDataIn will be NULL, so check for NULLS.
|
||||
if (context->isParamNull(0))
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
struct ssq_data* outData = (struct ssq_data*)context->getUserData()->data;
|
||||
|
||||
struct ssq_data* inData = (struct ssq_data*)userDataIn->data;
|
||||
|
||||
outData->sumsq += inData->sumsq;
|
||||
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode ssq::evaluate(mcsv1Context* context, static_any::any& valOut)
|
||||
{
|
||||
struct ssq_data* data = (struct ssq_data*)context->getUserData()->data;
|
||||
valOut = data->sumsq;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
struct ssq_data* data = (struct ssq_data*)context->getUserData()->data;
|
||||
valOut = data->sumsq;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
mcsv1_UDAF::ReturnCode ssq::dropValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsDropped)
|
||||
mcsv1_UDAF::ReturnCode ssq::dropValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsDropped)
|
||||
{
|
||||
static_any::any& valIn = valsDropped[0].columnData;
|
||||
struct ssq_data* data = (struct ssq_data*)context->getUserData()->data;
|
||||
DATATYPE val = 0.0;
|
||||
static_any::any& valIn = valsDropped[0].columnData;
|
||||
struct ssq_data* data = (struct ssq_data*)context->getUserData()->data;
|
||||
DATATYPE val = 0.0;
|
||||
|
||||
if (valIn.empty())
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS; // Ought not happen when UDAF_IGNORE_NULLS is on.
|
||||
}
|
||||
if (valIn.empty())
|
||||
{
|
||||
return mcsv1_UDAF::SUCCESS; // Ought not happen when UDAF_IGNORE_NULLS is on.
|
||||
}
|
||||
|
||||
if (valIn.compatible(charTypeId))
|
||||
{
|
||||
val = valIn.cast<char>();
|
||||
}
|
||||
else if (valIn.compatible(scharTypeId))
|
||||
{
|
||||
val = valIn.cast<signed char>();
|
||||
}
|
||||
else if (valIn.compatible(shortTypeId))
|
||||
{
|
||||
val = valIn.cast<short>();
|
||||
}
|
||||
else if (valIn.compatible(intTypeId))
|
||||
{
|
||||
val = valIn.cast<int>();
|
||||
}
|
||||
else if (valIn.compatible(longTypeId))
|
||||
{
|
||||
val = valIn.cast<long>();
|
||||
}
|
||||
else if (valIn.compatible(llTypeId))
|
||||
{
|
||||
val = valIn.cast<long long>();
|
||||
}
|
||||
else if (valIn.compatible(ucharTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned char>();
|
||||
}
|
||||
else if (valIn.compatible(ushortTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned short>();
|
||||
}
|
||||
else if (valIn.compatible(uintTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned int>();
|
||||
}
|
||||
else if (valIn.compatible(ulongTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long>();
|
||||
}
|
||||
else if (valIn.compatible(ullTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long long>();
|
||||
}
|
||||
else if (valIn.compatible(floatTypeId))
|
||||
{
|
||||
val = valIn.cast<float>();
|
||||
}
|
||||
else if (valIn.compatible(doubleTypeId))
|
||||
{
|
||||
val = valIn.cast<double>();
|
||||
}
|
||||
if (valIn.compatible(charTypeId))
|
||||
{
|
||||
val = valIn.cast<char>();
|
||||
}
|
||||
else if (valIn.compatible(scharTypeId))
|
||||
{
|
||||
val = valIn.cast<signed char>();
|
||||
}
|
||||
else if (valIn.compatible(shortTypeId))
|
||||
{
|
||||
val = valIn.cast<short>();
|
||||
}
|
||||
else if (valIn.compatible(intTypeId))
|
||||
{
|
||||
val = valIn.cast<int>();
|
||||
}
|
||||
else if (valIn.compatible(longTypeId))
|
||||
{
|
||||
val = valIn.cast<long>();
|
||||
}
|
||||
else if (valIn.compatible(llTypeId))
|
||||
{
|
||||
val = valIn.cast<long long>();
|
||||
}
|
||||
else if (valIn.compatible(ucharTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned char>();
|
||||
}
|
||||
else if (valIn.compatible(ushortTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned short>();
|
||||
}
|
||||
else if (valIn.compatible(uintTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned int>();
|
||||
}
|
||||
else if (valIn.compatible(ulongTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long>();
|
||||
}
|
||||
else if (valIn.compatible(ullTypeId))
|
||||
{
|
||||
val = valIn.cast<unsigned long long>();
|
||||
}
|
||||
else if (valIn.compatible(floatTypeId))
|
||||
{
|
||||
val = valIn.cast<float>();
|
||||
}
|
||||
else if (valIn.compatible(doubleTypeId))
|
||||
{
|
||||
val = valIn.cast<double>();
|
||||
}
|
||||
|
||||
// For decimal types, we need to move the decimal point.
|
||||
uint32_t scale = valsDropped[0].scale;
|
||||
if (val != 0 && scale > 0)
|
||||
{
|
||||
val /= pow(10.0, (double)scale);
|
||||
}
|
||||
data->sumsq -= val*val;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
// For decimal types, we need to move the decimal point.
|
||||
uint32_t scale = valsDropped[0].scale;
|
||||
|
||||
if (val != 0 && scale > 0)
|
||||
{
|
||||
val /= pow(10.0, (double)scale);
|
||||
}
|
||||
|
||||
data->sumsq -= val * val;
|
||||
return mcsv1_UDAF::SUCCESS;
|
||||
}
|
||||
|
||||
|
314
utils/udfsdk/ssq.h
Executable file → Normal file
314
utils/udfsdk/ssq.h
Executable file → Normal file
@ -21,34 +21,34 @@
|
||||
* mcsv1_UDAF.h
|
||||
***********************************************************************/
|
||||
|
||||
/**
|
||||
* Columnstore interface for writing a User Defined Aggregate
|
||||
* Functions (UDAF) and User Defined Analytic Functions (UDAnF)
|
||||
* or a function that can act as either - UDA(n)F
|
||||
*
|
||||
/**
|
||||
* Columnstore interface for writing a User Defined Aggregate
|
||||
* Functions (UDAF) and User Defined Analytic Functions (UDAnF)
|
||||
* or a function that can act as either - UDA(n)F
|
||||
*
|
||||
* The basic steps are:
|
||||
*
|
||||
* 1. Create a the UDA(n)F function interface in some .h file.
|
||||
* 2. Create the UDF function implementation in some .cpp file
|
||||
* 3. Create the connector stub (MariaDB UDAF definition) for
|
||||
* this UDF function.
|
||||
* 4. build the dynamic library using all of the source.
|
||||
* 5 Put the library in $COLUMNSTORE_INSTALL/lib of
|
||||
* all modules
|
||||
* 6. restart the Columnstore system.
|
||||
* 1. Create a the UDA(n)F function interface in some .h file.
|
||||
* 2. Create the UDF function implementation in some .cpp file
|
||||
* 3. Create the connector stub (MariaDB UDAF definition) for
|
||||
* this UDF function.
|
||||
* 4. build the dynamic library using all of the source.
|
||||
* 5 Put the library in $COLUMNSTORE_INSTALL/lib of
|
||||
* all modules
|
||||
* 6. restart the Columnstore system.
|
||||
* 7. notify mysqld about the new function:
|
||||
*
|
||||
*
|
||||
* CREATE AGGREGATE FUNCTION ssq returns REAL soname
|
||||
* 'libudf_mysql.so';
|
||||
*
|
||||
* The UDAF function will run distributed in the Columnstore
|
||||
* engine. UDAnF do not run distributed.
|
||||
*
|
||||
* UDAF is User Defined Aggregate Function.
|
||||
* UDAnF is User Defined Analytic Function.
|
||||
* UDA(n)F is an acronym for a function that could be either. It
|
||||
* is also used to describe the interface that is used for
|
||||
* either.
|
||||
*
|
||||
* The UDAF function will run distributed in the Columnstore
|
||||
* engine. UDAnF do not run distributed.
|
||||
*
|
||||
* UDAF is User Defined Aggregate Function.
|
||||
* UDAnF is User Defined Analytic Function.
|
||||
* UDA(n)F is an acronym for a function that could be either. It
|
||||
* is also used to describe the interface that is used for
|
||||
* either.
|
||||
*/
|
||||
#ifndef HEADER_ssq
|
||||
#define HEADER_ssq
|
||||
@ -77,155 +77,155 @@ using namespace execplan;
|
||||
namespace mcsv1sdk
|
||||
{
|
||||
|
||||
// Override mcsv1_UDAF to build your User Defined Aggregate (UDAF) and/or
|
||||
// Override mcsv1_UDAF to build your User Defined Aggregate (UDAF) and/or
|
||||
// User Defined Analytic Function (UDAnF).
|
||||
// These will be singleton classes, so don't put any instance
|
||||
// specific data in here. All instance data is stored in mcsv1Context
|
||||
// passed to each user function and retrieved by the getUserData() method.
|
||||
//
|
||||
// Each API function returns a ReturnCode. If ERROR is returned at any time,
|
||||
// the query is aborted, getInterrupted() will begin to return true and the
|
||||
// message set in config->setErrorMessage() is returned to MariaDB.
|
||||
//
|
||||
// Each API function returns a ReturnCode. If ERROR is returned at any time,
|
||||
// the query is aborted, getInterrupted() will begin to return true and the
|
||||
// message set in config->setErrorMessage() is returned to MariaDB.
|
||||
|
||||
// A simple aggregate to return the sum of squares
|
||||
class ssq : public mcsv1_UDAF
|
||||
{
|
||||
public:
|
||||
// Defaults OK
|
||||
ssq() : mcsv1_UDAF(){};
|
||||
virtual ~ssq(){};
|
||||
// Defaults OK
|
||||
ssq() : mcsv1_UDAF() {};
|
||||
virtual ~ssq() {};
|
||||
|
||||
/**
|
||||
* init()
|
||||
*
|
||||
* Mandatory. Implement this to initialize flags and instance
|
||||
* data. Called once per SQL statement. You can do any sanity
|
||||
* checks here.
|
||||
*
|
||||
* colTypes (in) - A vector of ColDataType defining the
|
||||
* parameters of the UDA(n)F call. These can be used to decide
|
||||
* to override the default return type. If desired, the new
|
||||
* return type can be set by context->setReturnType() and
|
||||
* decimal scale and precision can be set by context->setScale
|
||||
* and context->setPrecision respectively.
|
||||
*
|
||||
* Return mcsv1_UDAF::ERROR on any error, such as non-compatible
|
||||
* colTypes or wrong number of arguments. Else return
|
||||
* mcsv1_UDAF::SUCCESS.
|
||||
*/
|
||||
virtual ReturnCode init(mcsv1Context* context,
|
||||
COL_TYPES& colTypes);
|
||||
/**
|
||||
* init()
|
||||
*
|
||||
* Mandatory. Implement this to initialize flags and instance
|
||||
* data. Called once per SQL statement. You can do any sanity
|
||||
* checks here.
|
||||
*
|
||||
* colTypes (in) - A vector of ColDataType defining the
|
||||
* parameters of the UDA(n)F call. These can be used to decide
|
||||
* to override the default return type. If desired, the new
|
||||
* return type can be set by context->setReturnType() and
|
||||
* decimal scale and precision can be set by context->setScale
|
||||
* and context->setPrecision respectively.
|
||||
*
|
||||
* Return mcsv1_UDAF::ERROR on any error, such as non-compatible
|
||||
* colTypes or wrong number of arguments. Else return
|
||||
* mcsv1_UDAF::SUCCESS.
|
||||
*/
|
||||
virtual ReturnCode init(mcsv1Context* context,
|
||||
COL_TYPES& colTypes);
|
||||
|
||||
/**
|
||||
* reset()
|
||||
*
|
||||
* Mandatory. Reset the UDA(n)F for a new group, partition or,
|
||||
* in some cases, new Window Frame. Do not free any memory
|
||||
* allocated by context->createUserData(). The SDK Framework
|
||||
* owns that memory and will handle that. Use this opportunity
|
||||
* to reset any variables in context->getUserData() needed for
|
||||
* the next aggregation. May be called multiple times on
|
||||
* different modules.
|
||||
*/
|
||||
virtual ReturnCode reset(mcsv1Context* context);
|
||||
/**
|
||||
* reset()
|
||||
*
|
||||
* Mandatory. Reset the UDA(n)F for a new group, partition or,
|
||||
* in some cases, new Window Frame. Do not free any memory
|
||||
* allocated by context->createUserData(). The SDK Framework
|
||||
* owns that memory and will handle that. Use this opportunity
|
||||
* to reset any variables in context->getUserData() needed for
|
||||
* the next aggregation. May be called multiple times on
|
||||
* different modules.
|
||||
*/
|
||||
virtual ReturnCode reset(mcsv1Context* context);
|
||||
|
||||
/**
|
||||
* nextValue()
|
||||
*
|
||||
* Mandatory. Handle a single row.
|
||||
*
|
||||
* colsIn - A vector of data structure describing the input
|
||||
* data.
|
||||
*
|
||||
* This function is called once for every row in the filtered
|
||||
* result set (before aggregation). It is very important that
|
||||
* this function is efficient.
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, nextValue
|
||||
* cannot depend on order, as it will only be called for each
|
||||
* row found on the specific PM.
|
||||
*
|
||||
* valsIn (in) - a vector of the parameters from the row.
|
||||
*/
|
||||
virtual ReturnCode nextValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsIn);
|
||||
/**
|
||||
* nextValue()
|
||||
*
|
||||
* Mandatory. Handle a single row.
|
||||
*
|
||||
* colsIn - A vector of data structure describing the input
|
||||
* data.
|
||||
*
|
||||
* This function is called once for every row in the filtered
|
||||
* result set (before aggregation). It is very important that
|
||||
* this function is efficient.
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, nextValue
|
||||
* cannot depend on order, as it will only be called for each
|
||||
* row found on the specific PM.
|
||||
*
|
||||
* valsIn (in) - a vector of the parameters from the row.
|
||||
*/
|
||||
virtual ReturnCode nextValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsIn);
|
||||
|
||||
/**
|
||||
* subEvaluate()
|
||||
*
|
||||
* Mandatory -- Called if the UDAF is running in a distributed
|
||||
* fashion. Columnstore tries to run all aggregate functions
|
||||
* distributed, depending on context.
|
||||
*
|
||||
* Perform an aggregation on rows partially aggregated by
|
||||
* nextValue. Columnstore calls nextValue for each row on a
|
||||
* given PM for a group (GROUP BY). subEvaluate is called on the
|
||||
* UM to consolodate those values into a single instance of
|
||||
* userData. Keep your aggregated totals in context's userData.
|
||||
* The first time this is called for a group, reset() would have
|
||||
* been called with this version of userData.
|
||||
*
|
||||
* Called for every partial data set in each group in GROUP BY.
|
||||
*
|
||||
* When subEvaluate has been called for all subAggregated data
|
||||
* sets, Evaluate will be called.
|
||||
*
|
||||
* valIn (In) - This is a pointer to a memory block of the size
|
||||
* set in setUserDataSize. It will contain the value of userData
|
||||
* as seen in the last call to NextValue for a given PM.
|
||||
*
|
||||
*/
|
||||
virtual ReturnCode subEvaluate(mcsv1Context* context, const UserData* userDataIn);
|
||||
/**
|
||||
* subEvaluate()
|
||||
*
|
||||
* Mandatory -- Called if the UDAF is running in a distributed
|
||||
* fashion. Columnstore tries to run all aggregate functions
|
||||
* distributed, depending on context.
|
||||
*
|
||||
* Perform an aggregation on rows partially aggregated by
|
||||
* nextValue. Columnstore calls nextValue for each row on a
|
||||
* given PM for a group (GROUP BY). subEvaluate is called on the
|
||||
* UM to consolodate those values into a single instance of
|
||||
* userData. Keep your aggregated totals in context's userData.
|
||||
* The first time this is called for a group, reset() would have
|
||||
* been called with this version of userData.
|
||||
*
|
||||
* Called for every partial data set in each group in GROUP BY.
|
||||
*
|
||||
* When subEvaluate has been called for all subAggregated data
|
||||
* sets, Evaluate will be called.
|
||||
*
|
||||
* valIn (In) - This is a pointer to a memory block of the size
|
||||
* set in setUserDataSize. It will contain the value of userData
|
||||
* as seen in the last call to NextValue for a given PM.
|
||||
*
|
||||
*/
|
||||
virtual ReturnCode subEvaluate(mcsv1Context* context, const UserData* userDataIn);
|
||||
|
||||
/**
|
||||
* evaluate()
|
||||
*
|
||||
* Mandatory. Get the aggregated value.
|
||||
*
|
||||
* Called for every new group if UDAF GROUP BY, UDAnF partition
|
||||
* or, in some cases, new Window Frame.
|
||||
*
|
||||
* Set the aggregated value into valOut. The datatype is assumed
|
||||
* to be the same as that set in the init() function;
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, evaluate is
|
||||
* called after a series of subEvaluate calls.
|
||||
*
|
||||
* valOut (out) - Set the aggregated value here. The datatype is
|
||||
* assumed to be the same as that set in the init() function;
|
||||
*
|
||||
* To return a NULL value, don't assign to valOut.
|
||||
*/
|
||||
virtual ReturnCode evaluate(mcsv1Context* context, static_any::any& valOut);
|
||||
/**
|
||||
* evaluate()
|
||||
*
|
||||
* Mandatory. Get the aggregated value.
|
||||
*
|
||||
* Called for every new group if UDAF GROUP BY, UDAnF partition
|
||||
* or, in some cases, new Window Frame.
|
||||
*
|
||||
* Set the aggregated value into valOut. The datatype is assumed
|
||||
* to be the same as that set in the init() function;
|
||||
*
|
||||
* If the UDAF is running in a distributed fashion, evaluate is
|
||||
* called after a series of subEvaluate calls.
|
||||
*
|
||||
* valOut (out) - Set the aggregated value here. The datatype is
|
||||
* assumed to be the same as that set in the init() function;
|
||||
*
|
||||
* To return a NULL value, don't assign to valOut.
|
||||
*/
|
||||
virtual ReturnCode evaluate(mcsv1Context* context, static_any::any& valOut);
|
||||
|
||||
/**
|
||||
* dropValue()
|
||||
*
|
||||
* Optional -- If defined, the server will call this instead of
|
||||
* reset for UDAnF.
|
||||
*
|
||||
* Don't implement if a UDAnF has one or more of the following:
|
||||
* The UDAnF can't be used with a Window Frame
|
||||
* The UDAnF is not reversable in some way
|
||||
* The UDAnF is not interested in optimal performance
|
||||
*
|
||||
* If not implemented, reset() followed by a series of
|
||||
* nextValue() will be called for each movement of the Window
|
||||
* Frame.
|
||||
*
|
||||
* If implemented, then each movement of the Window Frame will
|
||||
* result in dropValue() being called for each row falling out
|
||||
* of the Frame and nextValue() being called for each new row
|
||||
* coming into the Frame.
|
||||
*
|
||||
* valsDropped (in) - a vector of the parameters from the row
|
||||
* leaving the Frame
|
||||
*
|
||||
* dropValue() will not be called for unbounded/current row type
|
||||
* frames, as those are already optimized.
|
||||
*/
|
||||
virtual ReturnCode dropValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsDropped);
|
||||
/**
|
||||
* dropValue()
|
||||
*
|
||||
* Optional -- If defined, the server will call this instead of
|
||||
* reset for UDAnF.
|
||||
*
|
||||
* Don't implement if a UDAnF has one or more of the following:
|
||||
* The UDAnF can't be used with a Window Frame
|
||||
* The UDAnF is not reversable in some way
|
||||
* The UDAnF is not interested in optimal performance
|
||||
*
|
||||
* If not implemented, reset() followed by a series of
|
||||
* nextValue() will be called for each movement of the Window
|
||||
* Frame.
|
||||
*
|
||||
* If implemented, then each movement of the Window Frame will
|
||||
* result in dropValue() being called for each row falling out
|
||||
* of the Frame and nextValue() being called for each new row
|
||||
* coming into the Frame.
|
||||
*
|
||||
* valsDropped (in) - a vector of the parameters from the row
|
||||
* leaving the Frame
|
||||
*
|
||||
* dropValue() will not be called for unbounded/current row type
|
||||
* frames, as those are already optimized.
|
||||
*/
|
||||
virtual ReturnCode dropValue(mcsv1Context* context,
|
||||
std::vector<ColumnDatum>& valsDropped);
|
||||
|
||||
protected:
|
||||
};
|
||||
|
682
utils/udfsdk/udfmysql.cpp
Executable file → Normal file
682
utils/udfsdk/udfmysql.cpp
Executable file → Normal file
@ -6,78 +6,94 @@ using namespace std;
|
||||
|
||||
#include "idb_mysql.h"
|
||||
|
||||
namespace {
|
||||
namespace
|
||||
{
|
||||
inline double cvtArgToDouble(int t, const char* v)
|
||||
{
|
||||
double d = 0.0;
|
||||
switch (t)
|
||||
{
|
||||
case INT_RESULT:
|
||||
d = (double)(*((long long*)v));
|
||||
break;
|
||||
case REAL_RESULT:
|
||||
d = *((double*)v);
|
||||
break;
|
||||
case DECIMAL_RESULT:
|
||||
case STRING_RESULT:
|
||||
d = strtod(v, 0);
|
||||
break;
|
||||
case ROW_RESULT:
|
||||
break;
|
||||
}
|
||||
return d;
|
||||
double d = 0.0;
|
||||
|
||||
switch (t)
|
||||
{
|
||||
case INT_RESULT:
|
||||
d = (double)(*((long long*)v));
|
||||
break;
|
||||
|
||||
case REAL_RESULT:
|
||||
d = *((double*)v);
|
||||
break;
|
||||
|
||||
case DECIMAL_RESULT:
|
||||
case STRING_RESULT:
|
||||
d = strtod(v, 0);
|
||||
break;
|
||||
|
||||
case ROW_RESULT:
|
||||
break;
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
inline long long cvtArgToInt(int t, const char* v)
|
||||
{
|
||||
long long ll = 0;
|
||||
switch (t)
|
||||
{
|
||||
case INT_RESULT:
|
||||
ll = *((long long*)v);
|
||||
break;
|
||||
case REAL_RESULT:
|
||||
ll = (long long)(*((double*)v));
|
||||
break;
|
||||
case DECIMAL_RESULT:
|
||||
case STRING_RESULT:
|
||||
ll = strtoll(v, 0, 0);
|
||||
break;
|
||||
case ROW_RESULT:
|
||||
break;
|
||||
}
|
||||
return ll;
|
||||
long long ll = 0;
|
||||
|
||||
switch (t)
|
||||
{
|
||||
case INT_RESULT:
|
||||
ll = *((long long*)v);
|
||||
break;
|
||||
|
||||
case REAL_RESULT:
|
||||
ll = (long long)(*((double*)v));
|
||||
break;
|
||||
|
||||
case DECIMAL_RESULT:
|
||||
case STRING_RESULT:
|
||||
ll = strtoll(v, 0, 0);
|
||||
break;
|
||||
|
||||
case ROW_RESULT:
|
||||
break;
|
||||
}
|
||||
|
||||
return ll;
|
||||
}
|
||||
inline string cvtArgToString(int t, const char* v)
|
||||
{
|
||||
string str;
|
||||
switch (t)
|
||||
{
|
||||
case INT_RESULT:
|
||||
{
|
||||
long long ll;
|
||||
ll = *((long long*)v);
|
||||
ostringstream oss;
|
||||
oss << ll;
|
||||
str = oss.str();
|
||||
break;
|
||||
}
|
||||
case REAL_RESULT:
|
||||
{
|
||||
double d;
|
||||
d = *((double*)v);
|
||||
ostringstream oss;
|
||||
oss << d;
|
||||
str = oss.str();
|
||||
break;
|
||||
}
|
||||
case DECIMAL_RESULT:
|
||||
case STRING_RESULT:
|
||||
str = v;
|
||||
break;
|
||||
case ROW_RESULT:
|
||||
break;
|
||||
}
|
||||
return str;
|
||||
string str;
|
||||
|
||||
switch (t)
|
||||
{
|
||||
case INT_RESULT:
|
||||
{
|
||||
long long ll;
|
||||
ll = *((long long*)v);
|
||||
ostringstream oss;
|
||||
oss << ll;
|
||||
str = oss.str();
|
||||
break;
|
||||
}
|
||||
|
||||
case REAL_RESULT:
|
||||
{
|
||||
double d;
|
||||
d = *((double*)v);
|
||||
ostringstream oss;
|
||||
oss << d;
|
||||
str = oss.str();
|
||||
break;
|
||||
}
|
||||
|
||||
case DECIMAL_RESULT:
|
||||
case STRING_RESULT:
|
||||
str = v;
|
||||
break;
|
||||
|
||||
case ROW_RESULT:
|
||||
break;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,7 +111,7 @@ inline string cvtArgToString(int t, const char* v)
|
||||
* ~/sql/udf_example.cc.
|
||||
*
|
||||
* Please note that the implementation of the function defined on the connector
|
||||
* will only be called when all the input arguments are constant. e.g.,
|
||||
* will only be called when all the input arguments are constant. e.g.,
|
||||
* mcs_add(2,3). That way, the function does not run in a distributed fashion
|
||||
* and could be slow. If there is a need for the UDF function to run with
|
||||
* pure constant input, then one needs to put a implementation in the XXX
|
||||
@ -105,371 +121,375 @@ inline string cvtArgToString(int t, const char* v)
|
||||
*/
|
||||
extern "C"
|
||||
{
|
||||
/**
|
||||
* MCS_ADD connector stub
|
||||
*/
|
||||
/**
|
||||
* MCS_ADD connector stub
|
||||
*/
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
my_bool mcs_add_init(UDF_INIT* initid, UDF_ARGS* args, char* message)
|
||||
{
|
||||
if (args->arg_count != 2)
|
||||
{
|
||||
strcpy(message,"mcs_add() requires two argument");
|
||||
return 1;
|
||||
}
|
||||
my_bool mcs_add_init(UDF_INIT* initid, UDF_ARGS* args, char* message)
|
||||
{
|
||||
if (args->arg_count != 2)
|
||||
{
|
||||
strcpy(message, "mcs_add() requires two argument");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void mcs_add_deinit(UDF_INIT* initid)
|
||||
{
|
||||
}
|
||||
void mcs_add_deinit(UDF_INIT* initid)
|
||||
{
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
double mcs_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
|
||||
{
|
||||
double op1, op2;
|
||||
double mcs_add(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* error)
|
||||
{
|
||||
double op1, op2;
|
||||
|
||||
op1 = cvtArgToDouble(args->arg_type[0], args->args[0]);
|
||||
op2 = cvtArgToDouble(args->arg_type[1], args->args[1]);
|
||||
op1 = cvtArgToDouble(args->arg_type[0], args->args[0]);
|
||||
op2 = cvtArgToDouble(args->arg_type[1], args->args[1]);
|
||||
|
||||
return op1+op2;
|
||||
}
|
||||
return op1 + op2;
|
||||
}
|
||||
|
||||
/**
|
||||
* MCS_ISNULL connector stub
|
||||
*/
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
my_bool mcs_isnull_init(UDF_INIT* initid, UDF_ARGS* args, char* message)
|
||||
{
|
||||
if (args->arg_count != 1)
|
||||
{
|
||||
strcpy(message,"mcs_isnull() requires one argument");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
/**
|
||||
* MCS_ISNULL connector stub
|
||||
*/
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void mcs_isnull_deinit(UDF_INIT* initid)
|
||||
{
|
||||
}
|
||||
my_bool mcs_isnull_init(UDF_INIT* initid, UDF_ARGS* args, char* message)
|
||||
{
|
||||
if (args->arg_count != 1)
|
||||
{
|
||||
strcpy(message, "mcs_isnull() requires one argument");
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
long long mcs_isnull(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
void mcs_isnull_deinit(UDF_INIT* initid)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* ALLNULL connector stub
|
||||
*/
|
||||
struct allnull_data
|
||||
{
|
||||
ulonglong totalQuantity;
|
||||
ulonglong totalNulls;
|
||||
};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
my_bool allnull_init(UDF_INIT* initid, UDF_ARGS* args, char* message)
|
||||
{
|
||||
struct allnull_data* data;
|
||||
long long mcs_isnull(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* error)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* ALLNULL connector stub
|
||||
*/
|
||||
struct allnull_data
|
||||
{
|
||||
ulonglong totalQuantity;
|
||||
ulonglong totalNulls;
|
||||
};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
my_bool allnull_init(UDF_INIT* initid, UDF_ARGS* args, char* message)
|
||||
{
|
||||
struct allnull_data* data;
|
||||
// if (args->arg_count != 1)
|
||||
// {
|
||||
// strcpy(message,"allnull() requires one argument");
|
||||
// return 1;
|
||||
// }
|
||||
|
||||
if (!(data = (struct allnull_data*) malloc(sizeof(struct allnull_data))))
|
||||
{
|
||||
strmov(message,"Couldn't allocate memory");
|
||||
return 1;
|
||||
}
|
||||
data->totalQuantity = 0;
|
||||
data->totalNulls = 0;
|
||||
if (!(data = (struct allnull_data*) malloc(sizeof(struct allnull_data))))
|
||||
{
|
||||
strmov(message, "Couldn't allocate memory");
|
||||
return 1;
|
||||
}
|
||||
|
||||
initid->ptr = (char*)data;
|
||||
data->totalQuantity = 0;
|
||||
data->totalNulls = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
initid->ptr = (char*)data;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void allnull_deinit(UDF_INIT* initid)
|
||||
{
|
||||
free(initid->ptr);
|
||||
}
|
||||
void allnull_deinit(UDF_INIT* initid)
|
||||
{
|
||||
free(initid->ptr);
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
long long allnull(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
|
||||
char* is_null, char* error __attribute__((unused)))
|
||||
{
|
||||
struct allnull_data* data = (struct allnull_data*)initid->ptr;
|
||||
return data->totalQuantity > 0 && data->totalNulls == data->totalQuantity;
|
||||
}
|
||||
long long allnull(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
|
||||
char* is_null, char* error __attribute__((unused)))
|
||||
{
|
||||
struct allnull_data* data = (struct allnull_data*)initid->ptr;
|
||||
return data->totalQuantity > 0 && data->totalNulls == data->totalQuantity;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void
|
||||
allnull_clear(UDF_INIT* initid, char* is_null __attribute__((unused)),
|
||||
void
|
||||
allnull_clear(UDF_INIT* initid, char* is_null __attribute__((unused)),
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
struct allnull_data* data = (struct allnull_data*)initid->ptr;
|
||||
data->totalQuantity = 0;
|
||||
data->totalNulls = 0;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void
|
||||
allnull_add(UDF_INIT* initid, UDF_ARGS* args,
|
||||
char* is_null,
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
struct allnull_data* data = (struct allnull_data*)initid->ptr;
|
||||
const char* word = args->args[0];
|
||||
data->totalQuantity++;
|
||||
|
||||
if (!word)
|
||||
{
|
||||
data->totalNulls++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SSQ connector stub
|
||||
*/
|
||||
struct ssq_data
|
||||
{
|
||||
double sumsq;
|
||||
};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
my_bool ssq_init(UDF_INIT* initid, UDF_ARGS* args, char* message)
|
||||
{
|
||||
struct ssq_data* data;
|
||||
|
||||
if (args->arg_count != 1)
|
||||
{
|
||||
strcpy(message, "ssq() requires one argument");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!(data = (struct ssq_data*) malloc(sizeof(struct ssq_data))))
|
||||
{
|
||||
strmov(message, "Couldn't allocate memory");
|
||||
return 1;
|
||||
}
|
||||
|
||||
data->sumsq = 0;
|
||||
|
||||
initid->ptr = (char*)data;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void ssq_deinit(UDF_INIT* initid)
|
||||
{
|
||||
free(initid->ptr);
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void
|
||||
ssq_clear(UDF_INIT* initid, char* is_null __attribute__((unused)),
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
struct allnull_data* data = (struct allnull_data*)initid->ptr;
|
||||
data->totalQuantity = 0;
|
||||
data->totalNulls = 0;
|
||||
}
|
||||
{
|
||||
struct ssq_data* data = (struct ssq_data*)initid->ptr;
|
||||
data->sumsq = 0;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void
|
||||
allnull_add(UDF_INIT* initid, UDF_ARGS* args,
|
||||
void
|
||||
ssq_add(UDF_INIT* initid, UDF_ARGS* args,
|
||||
char* is_null,
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
struct allnull_data* data = (struct allnull_data*)initid->ptr;
|
||||
const char *word=args->args[0];
|
||||
data->totalQuantity++;
|
||||
if (!word)
|
||||
{
|
||||
data->totalNulls++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SSQ connector stub
|
||||
*/
|
||||
struct ssq_data
|
||||
{
|
||||
double sumsq;
|
||||
};
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
my_bool ssq_init(UDF_INIT* initid, UDF_ARGS* args, char* message)
|
||||
{
|
||||
struct ssq_data* data;
|
||||
if (args->arg_count != 1)
|
||||
{
|
||||
strcpy(message,"ssq() requires one argument");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!(data = (struct ssq_data*) malloc(sizeof(struct ssq_data))))
|
||||
{
|
||||
strmov(message,"Couldn't allocate memory");
|
||||
return 1;
|
||||
}
|
||||
data->sumsq = 0;
|
||||
|
||||
initid->ptr = (char*)data;
|
||||
return 0;
|
||||
}
|
||||
{
|
||||
struct ssq_data* data = (struct ssq_data*)initid->ptr;
|
||||
double val = cvtArgToDouble(args->arg_type[0], args->args[0]);
|
||||
data->sumsq = val * val;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void ssq_deinit(UDF_INIT* initid)
|
||||
{
|
||||
free(initid->ptr);
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void
|
||||
ssq_clear(UDF_INIT* initid, char* is_null __attribute__((unused)),
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
struct ssq_data* data = (struct ssq_data*)initid->ptr;
|
||||
data->sumsq = 0;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void
|
||||
ssq_add(UDF_INIT* initid, UDF_ARGS* args,
|
||||
char* is_null,
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
struct ssq_data* data = (struct ssq_data*)initid->ptr;
|
||||
double val = cvtArgToDouble(args->arg_type[0], args->args[0]);
|
||||
data->sumsq = val*val;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
long long ssq(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
|
||||
char* is_null, char* error __attribute__((unused)))
|
||||
{
|
||||
struct ssq_data* data = (struct ssq_data*)initid->ptr;
|
||||
return data->sumsq;
|
||||
}
|
||||
long long ssq(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
|
||||
char* is_null, char* error __attribute__((unused)))
|
||||
{
|
||||
struct ssq_data* data = (struct ssq_data*)initid->ptr;
|
||||
return data->sumsq;
|
||||
}
|
||||
|
||||
//=======================================================================
|
||||
|
||||
/**
|
||||
* MEDIAN connector stub
|
||||
*/
|
||||
/**
|
||||
* MEDIAN connector stub
|
||||
*/
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
my_bool median_init(UDF_INIT* initid, UDF_ARGS* args, char* message)
|
||||
{
|
||||
if (args->arg_count != 1)
|
||||
{
|
||||
strcpy(message,"median() requires one argument");
|
||||
return 1;
|
||||
}
|
||||
my_bool median_init(UDF_INIT* initid, UDF_ARGS* args, char* message)
|
||||
{
|
||||
if (args->arg_count != 1)
|
||||
{
|
||||
strcpy(message, "median() requires one argument");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
if (!(data = (struct ssq_data*) malloc(sizeof(struct ssq_data))))
|
||||
{
|
||||
strmov(message,"Couldn't allocate memory");
|
||||
return 1;
|
||||
}
|
||||
data->sumsq = 0;
|
||||
/*
|
||||
if (!(data = (struct ssq_data*) malloc(sizeof(struct ssq_data))))
|
||||
{
|
||||
strmov(message,"Couldn't allocate memory");
|
||||
return 1;
|
||||
}
|
||||
data->sumsq = 0;
|
||||
|
||||
initid->ptr = (char*)data;
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
initid->ptr = (char*)data;
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void median_deinit(UDF_INIT* initid)
|
||||
{
|
||||
void median_deinit(UDF_INIT* initid)
|
||||
{
|
||||
// free(initid->ptr);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void
|
||||
median_clear(UDF_INIT* initid, char* is_null __attribute__((unused)),
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
void
|
||||
median_clear(UDF_INIT* initid, char* is_null __attribute__((unused)),
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
// struct ssq_data* data = (struct ssq_data*)initid->ptr;
|
||||
// data->sumsq = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void
|
||||
median_add(UDF_INIT* initid, UDF_ARGS* args,
|
||||
char* is_null,
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
void
|
||||
median_add(UDF_INIT* initid, UDF_ARGS* args,
|
||||
char* is_null,
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
// struct ssq_data* data = (struct ssq_data*)initid->ptr;
|
||||
// double val = cvtArgToDouble(args->arg_type[0], args->args[0]);
|
||||
// data->sumsq = val*val;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
long long median(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
|
||||
char* is_null, char* error __attribute__((unused)))
|
||||
{
|
||||
long long median(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
|
||||
char* is_null, char* error __attribute__((unused)))
|
||||
{
|
||||
// struct ssq_data* data = (struct ssq_data*)initid->ptr;
|
||||
// return data->sumsq;
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* avg_mode connector stub
|
||||
*/
|
||||
/**
|
||||
* avg_mode connector stub
|
||||
*/
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
my_bool avg_mode_init(UDF_INIT* initid, UDF_ARGS* args, char* message)
|
||||
{
|
||||
if (args->arg_count != 1)
|
||||
{
|
||||
strcpy(message,"avg_mode() requires one argument");
|
||||
return 1;
|
||||
}
|
||||
my_bool avg_mode_init(UDF_INIT* initid, UDF_ARGS* args, char* message)
|
||||
{
|
||||
if (args->arg_count != 1)
|
||||
{
|
||||
strcpy(message, "avg_mode() requires one argument");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
if (!(data = (struct ssq_data*) malloc(sizeof(struct ssq_data))))
|
||||
{
|
||||
strmov(message,"Couldn't allocate memory");
|
||||
return 1;
|
||||
}
|
||||
data->sumsq = 0;
|
||||
/*
|
||||
if (!(data = (struct ssq_data*) malloc(sizeof(struct ssq_data))))
|
||||
{
|
||||
strmov(message,"Couldn't allocate memory");
|
||||
return 1;
|
||||
}
|
||||
data->sumsq = 0;
|
||||
|
||||
initid->ptr = (char*)data;
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
initid->ptr = (char*)data;
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void avg_mode_deinit(UDF_INIT* initid)
|
||||
{
|
||||
void avg_mode_deinit(UDF_INIT* initid)
|
||||
{
|
||||
// free(initid->ptr);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void
|
||||
avg_mode_clear(UDF_INIT* initid, char* is_null __attribute__((unused)),
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
void
|
||||
avg_mode_clear(UDF_INIT* initid, char* is_null __attribute__((unused)),
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
// struct ssq_data* data = (struct ssq_data*)initid->ptr;
|
||||
// data->sumsq = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
void
|
||||
avg_mode_add(UDF_INIT* initid, UDF_ARGS* args,
|
||||
char* is_null,
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
void
|
||||
avg_mode_add(UDF_INIT* initid, UDF_ARGS* args,
|
||||
char* is_null,
|
||||
char* message __attribute__((unused)))
|
||||
{
|
||||
// struct ssq_data* data = (struct ssq_data*)initid->ptr;
|
||||
// double val = cvtArgToDouble(args->arg_type[0], args->args[0]);
|
||||
// data->sumsq = val*val;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
__declspec(dllexport)
|
||||
__declspec(dllexport)
|
||||
#endif
|
||||
long long avg_mode(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
|
||||
char* is_null, char* error __attribute__((unused)))
|
||||
{
|
||||
long long avg_mode(UDF_INIT* initid, UDF_ARGS* args __attribute__((unused)),
|
||||
char* is_null, char* error __attribute__((unused)))
|
||||
{
|
||||
// struct ssq_data* data = (struct ssq_data*)initid->ptr;
|
||||
// return data->sumsq;
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
// vim:ts=4 sw=4:
|
||||
|
||||
|
@ -53,70 +53,71 @@ UDFSDK::~UDFSDK()
|
||||
|
||||
/**
|
||||
* All UDF functions should be registered in the function map. They will be
|
||||
* picked up by the MariaDB ColumnStore F&E framework when the servers are started.
|
||||
* picked up by the MariaDB ColumnStore F&E framework when the servers are started.
|
||||
* That will make sure the UDF functions runs distributedly in ColumnStore
|
||||
* engines just like the internal ColumnStore functions.
|
||||
*/
|
||||
FuncMap UDFSDK::UDFMap() const
|
||||
{
|
||||
FuncMap fm;
|
||||
|
||||
// first: function name
|
||||
// second: Function pointer
|
||||
// please use lower case for the function name. Because the names might be
|
||||
// case-insensitive in MariaDB depending on the setting. In such case,
|
||||
// the function names passed to the interface is always in lower case.
|
||||
fm["mcs_add"] = new MCS_add();
|
||||
fm["mcs_isnull"] = new MCS_isnull();
|
||||
|
||||
return fm;
|
||||
FuncMap fm;
|
||||
|
||||
// first: function name
|
||||
// second: Function pointer
|
||||
// please use lower case for the function name. Because the names might be
|
||||
// case-insensitive in MariaDB depending on the setting. In such case,
|
||||
// the function names passed to the interface is always in lower case.
|
||||
fm["mcs_add"] = new MCS_add();
|
||||
fm["mcs_isnull"] = new MCS_isnull();
|
||||
|
||||
return fm;
|
||||
}
|
||||
|
||||
/***************************************************************************
|
||||
* MCS_ADD implementation
|
||||
* MCS_ADD implementation
|
||||
*
|
||||
* OperationType() definition
|
||||
*/
|
||||
CalpontSystemCatalog::ColType MCS_add::operationType (FunctionParm& fp,
|
||||
CalpontSystemCatalog::ColType& resultType)
|
||||
CalpontSystemCatalog::ColType MCS_add::operationType (FunctionParm& fp,
|
||||
CalpontSystemCatalog::ColType& resultType)
|
||||
{
|
||||
// operation type of MCS_add is determined by the argument types
|
||||
assert (fp.size() == 2);
|
||||
CalpontSystemCatalog::ColType rt;
|
||||
if (fp[0]->data()->resultType() == fp[1]->data()->resultType())
|
||||
{
|
||||
rt = fp[0]->data()->resultType();
|
||||
}
|
||||
else if (fp[0]->data()->resultType().colDataType == CalpontSystemCatalog::CHAR ||
|
||||
fp[1]->data()->resultType().colDataType == CalpontSystemCatalog::CHAR ||
|
||||
fp[0]->data()->resultType().colDataType == CalpontSystemCatalog::VARCHAR ||
|
||||
fp[1]->data()->resultType().colDataType == CalpontSystemCatalog::VARCHAR ||
|
||||
fp[0]->data()->resultType().colDataType == CalpontSystemCatalog::DOUBLE ||
|
||||
fp[1]->data()->resultType().colDataType == CalpontSystemCatalog::DOUBLE)
|
||||
{
|
||||
rt.colDataType = CalpontSystemCatalog::DOUBLE;
|
||||
rt.colWidth = 8;
|
||||
}
|
||||
else if (fp[0]->data()->resultType().colDataType == CalpontSystemCatalog::DATE ||
|
||||
fp[1]->data()->resultType().colDataType == CalpontSystemCatalog::DATE ||
|
||||
fp[0]->data()->resultType().colDataType == CalpontSystemCatalog::DATETIME ||
|
||||
fp[1]->data()->resultType().colDataType == CalpontSystemCatalog::DATETIME)
|
||||
{
|
||||
rt.colDataType = CalpontSystemCatalog::BIGINT;
|
||||
rt.colWidth = 8;
|
||||
}
|
||||
else if (fp[0]->data()->resultType().colDataType == CalpontSystemCatalog::DECIMAL ||
|
||||
// operation type of MCS_add is determined by the argument types
|
||||
assert (fp.size() == 2);
|
||||
CalpontSystemCatalog::ColType rt;
|
||||
|
||||
if (fp[0]->data()->resultType() == fp[1]->data()->resultType())
|
||||
{
|
||||
rt = fp[0]->data()->resultType();
|
||||
}
|
||||
else if (fp[0]->data()->resultType().colDataType == CalpontSystemCatalog::CHAR ||
|
||||
fp[1]->data()->resultType().colDataType == CalpontSystemCatalog::CHAR ||
|
||||
fp[0]->data()->resultType().colDataType == CalpontSystemCatalog::VARCHAR ||
|
||||
fp[1]->data()->resultType().colDataType == CalpontSystemCatalog::VARCHAR ||
|
||||
fp[0]->data()->resultType().colDataType == CalpontSystemCatalog::DOUBLE ||
|
||||
fp[1]->data()->resultType().colDataType == CalpontSystemCatalog::DOUBLE)
|
||||
{
|
||||
rt.colDataType = CalpontSystemCatalog::DOUBLE;
|
||||
rt.colWidth = 8;
|
||||
}
|
||||
else if (fp[0]->data()->resultType().colDataType == CalpontSystemCatalog::DATE ||
|
||||
fp[1]->data()->resultType().colDataType == CalpontSystemCatalog::DATE ||
|
||||
fp[0]->data()->resultType().colDataType == CalpontSystemCatalog::DATETIME ||
|
||||
fp[1]->data()->resultType().colDataType == CalpontSystemCatalog::DATETIME)
|
||||
{
|
||||
rt.colDataType = CalpontSystemCatalog::BIGINT;
|
||||
rt.colWidth = 8;
|
||||
}
|
||||
else if (fp[0]->data()->resultType().colDataType == CalpontSystemCatalog::DECIMAL ||
|
||||
fp[0]->data()->resultType().colDataType == CalpontSystemCatalog::UDECIMAL ||
|
||||
fp[1]->data()->resultType().colDataType == CalpontSystemCatalog::DECIMAL ||
|
||||
fp[1]->data()->resultType().colDataType == CalpontSystemCatalog::UDECIMAL)
|
||||
{
|
||||
rt.colDataType = CalpontSystemCatalog::DECIMAL;
|
||||
rt.colWidth = 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
{
|
||||
rt.colDataType = CalpontSystemCatalog::DECIMAL;
|
||||
rt.colWidth = 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isUnsigned(fp[0]->data()->resultType().colDataType) ||
|
||||
isUnsigned(fp[1]->data()->resultType().colDataType))
|
||||
isUnsigned(fp[1]->data()->resultType().colDataType))
|
||||
{
|
||||
rt.colDataType = CalpontSystemCatalog::UBIGINT;
|
||||
rt.colWidth = 8;
|
||||
@ -126,8 +127,9 @@ CalpontSystemCatalog::ColType MCS_add::operationType (FunctionParm& fp,
|
||||
rt.colDataType = CalpontSystemCatalog::BIGINT;
|
||||
rt.colWidth = 8;
|
||||
}
|
||||
}
|
||||
return rt;
|
||||
}
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -136,66 +138,73 @@ CalpontSystemCatalog::ColType MCS_add::operationType (FunctionParm& fp,
|
||||
* This API is called when an double value is needed to return from the UDF function
|
||||
*/
|
||||
double MCS_add::getDoubleVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
switch (op_ct.colDataType)
|
||||
{
|
||||
// The APIs for the evaluation of the function parameters are the same getXXXval()
|
||||
// functions. However, only two arguments are passed in, the current
|
||||
// row reference and the NULL indicator isNull.
|
||||
case CalpontSystemCatalog::BIGINT:
|
||||
case CalpontSystemCatalog::MEDINT:
|
||||
case CalpontSystemCatalog::SMALLINT:
|
||||
case CalpontSystemCatalog::TINYINT:
|
||||
return ( parm[0]->data()->getIntVal(row, isNull) +
|
||||
parm[1]->data()->getIntVal(row, isNull));
|
||||
switch (op_ct.colDataType)
|
||||
{
|
||||
// The APIs for the evaluation of the function parameters are the same getXXXval()
|
||||
// functions. However, only two arguments are passed in, the current
|
||||
// row reference and the NULL indicator isNull.
|
||||
case CalpontSystemCatalog::BIGINT:
|
||||
case CalpontSystemCatalog::MEDINT:
|
||||
case CalpontSystemCatalog::SMALLINT:
|
||||
case CalpontSystemCatalog::TINYINT:
|
||||
return ( parm[0]->data()->getIntVal(row, isNull) +
|
||||
parm[1]->data()->getIntVal(row, isNull));
|
||||
|
||||
case CalpontSystemCatalog::UBIGINT:
|
||||
case CalpontSystemCatalog::UMEDINT:
|
||||
case CalpontSystemCatalog::USMALLINT:
|
||||
case CalpontSystemCatalog::UTINYINT:
|
||||
return ( parm[0]->data()->getUintVal(row, isNull) +
|
||||
parm[1]->data()->getUintVal(row, isNull));
|
||||
case CalpontSystemCatalog::DOUBLE:
|
||||
return ( parm[0]->data()->getDoubleVal(row, isNull) +
|
||||
parm[1]->data()->getDoubleVal(row, isNull));
|
||||
case CalpontSystemCatalog::DECIMAL:
|
||||
|
||||
case CalpontSystemCatalog::DOUBLE:
|
||||
return ( parm[0]->data()->getDoubleVal(row, isNull) +
|
||||
parm[1]->data()->getDoubleVal(row, isNull));
|
||||
|
||||
case CalpontSystemCatalog::DECIMAL:
|
||||
case CalpontSystemCatalog::UDECIMAL:
|
||||
{
|
||||
IDB_Decimal dec;
|
||||
IDB_Decimal op1 = parm[0]->data()->getDecimalVal(row, isNull);
|
||||
IDB_Decimal op2 = parm[1]->data()->getDecimalVal(row, isNull);
|
||||
if (op1.scale == op2.scale)
|
||||
{
|
||||
dec.scale = op1.scale;
|
||||
}
|
||||
else if (op1.scale >= op2.scale)
|
||||
{
|
||||
dec.scale = op2.scale;
|
||||
op1.value *= (int64_t)pow((double)10, op1.scale-op2.scale);
|
||||
}
|
||||
else
|
||||
{
|
||||
dec.scale = op1.scale;
|
||||
op2.value *= (int64_t)pow((double)10, op2.scale-op1.scale);
|
||||
}
|
||||
dec.value = op1.value + op2.value;
|
||||
return (double)(dec.value / pow((double)10, dec.scale));
|
||||
}
|
||||
default:
|
||||
return ( parm[0]->data()->getDoubleVal(row, isNull) +
|
||||
parm[1]->data()->getDoubleVal(row, isNull));
|
||||
}
|
||||
return 0;
|
||||
{
|
||||
IDB_Decimal dec;
|
||||
IDB_Decimal op1 = parm[0]->data()->getDecimalVal(row, isNull);
|
||||
IDB_Decimal op2 = parm[1]->data()->getDecimalVal(row, isNull);
|
||||
|
||||
if (op1.scale == op2.scale)
|
||||
{
|
||||
dec.scale = op1.scale;
|
||||
}
|
||||
else if (op1.scale >= op2.scale)
|
||||
{
|
||||
dec.scale = op2.scale;
|
||||
op1.value *= (int64_t)pow((double)10, op1.scale - op2.scale);
|
||||
}
|
||||
else
|
||||
{
|
||||
dec.scale = op1.scale;
|
||||
op2.value *= (int64_t)pow((double)10, op2.scale - op1.scale);
|
||||
}
|
||||
|
||||
dec.value = op1.value + op2.value;
|
||||
return (double)(dec.value / pow((double)10, dec.scale));
|
||||
}
|
||||
|
||||
default:
|
||||
return ( parm[0]->data()->getDoubleVal(row, isNull) +
|
||||
parm[1]->data()->getDoubleVal(row, isNull));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
float MCS_add::getFloatVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
return (float)getDoubleVal(row, parm, isNull, op_ct);
|
||||
return (float)getDoubleVal(row, parm, isNull, op_ct);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -203,39 +212,39 @@ float MCS_add::getFloatVal(Row& row,
|
||||
*
|
||||
* This API is called when an integer value is needed to return from the UDF function
|
||||
*
|
||||
* Because the result type MCS_add is double(real), all the other API can simply call
|
||||
* Because the result type MCS_add is double(real), all the other API can simply call
|
||||
* getDoubleVal and apply the conversion. This method may not fit for all the UDF
|
||||
* implementation.
|
||||
*/
|
||||
int64_t MCS_add::getIntVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
return (int64_t)getDoubleVal(row, parm, isNull, op_ct);
|
||||
return (int64_t)getDoubleVal(row, parm, isNull, op_ct);
|
||||
}
|
||||
|
||||
string MCS_add::getStrVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
// One will need a more efficient implementation if this API is frequently
|
||||
// called for this UDF function. This code is for demonstration purpose.
|
||||
ostringstream oss;
|
||||
oss << getDoubleVal(row, parm, isNull, op_ct);
|
||||
return oss.str();
|
||||
// One will need a more efficient implementation if this API is frequently
|
||||
// called for this UDF function. This code is for demonstration purpose.
|
||||
ostringstream oss;
|
||||
oss << getDoubleVal(row, parm, isNull, op_ct);
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
IDB_Decimal MCS_add::getDecimalVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
IDB_Decimal dec;
|
||||
dec.value = getIntVal(row, parm, isNull, op_ct);
|
||||
dec.scale = 0;
|
||||
return dec;
|
||||
IDB_Decimal dec;
|
||||
dec.value = getIntVal(row, parm, isNull, op_ct);
|
||||
dec.scale = 0;
|
||||
return dec;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -245,11 +254,11 @@ IDB_Decimal MCS_add::getDecimalVal(Row& row,
|
||||
* or throw a customized exception here.
|
||||
*/
|
||||
int32_t MCS_add::getDateIntVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
throw logic_error("Invalid API called for MCS_ADD");
|
||||
throw logic_error("Invalid API called for MCS_ADD");
|
||||
}
|
||||
|
||||
/**
|
||||
@ -259,32 +268,32 @@ int32_t MCS_add::getDateIntVal(Row& row,
|
||||
* or throw a customized exception here.
|
||||
*/
|
||||
int64_t MCS_add::getDatetimeIntVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
return (int64_t)getDoubleVal(row, parm, isNull, op_ct);
|
||||
return (int64_t)getDoubleVal(row, parm, isNull, op_ct);
|
||||
}
|
||||
|
||||
bool MCS_add::getBoolVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
/***************************************************************************
|
||||
* MCS_ISNULL implementation
|
||||
* MCS_ISNULL implementation
|
||||
*
|
||||
* OperationType() definition
|
||||
*/
|
||||
CalpontSystemCatalog::ColType MCS_isnull::operationType (FunctionParm& fp,
|
||||
CalpontSystemCatalog::ColType& resultType)
|
||||
CalpontSystemCatalog::ColType MCS_isnull::operationType (FunctionParm& fp,
|
||||
CalpontSystemCatalog::ColType& resultType)
|
||||
{
|
||||
// operation type of MCS_isnull should be the same as the argument type
|
||||
assert (fp.size() == 1);
|
||||
return fp[0]->data()->resultType();
|
||||
// operation type of MCS_isnull should be the same as the argument type
|
||||
assert (fp.size() == 1);
|
||||
return fp[0]->data()->resultType();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -293,33 +302,36 @@ CalpontSystemCatalog::ColType MCS_isnull::operationType (FunctionParm& fp,
|
||||
* This would be the most commonly called API for MCS_isnull function
|
||||
*/
|
||||
bool MCS_isnull::getBoolVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
switch (op_ct.colDataType)
|
||||
{
|
||||
// For the purpose of this function, one does not need to get the value of
|
||||
// the argument. One only need to know if the argument is NULL. The passed
|
||||
// in parameter isNull will be set if the parameter is evaluated NULL.
|
||||
// Please note that before this function returns, isNull should be set to
|
||||
// false, otherwise the result of the function would be considered NULL,
|
||||
// which is not possible for MCS_isnull().
|
||||
case CalpontSystemCatalog::DECIMAL:
|
||||
switch (op_ct.colDataType)
|
||||
{
|
||||
// For the purpose of this function, one does not need to get the value of
|
||||
// the argument. One only need to know if the argument is NULL. The passed
|
||||
// in parameter isNull will be set if the parameter is evaluated NULL.
|
||||
// Please note that before this function returns, isNull should be set to
|
||||
// false, otherwise the result of the function would be considered NULL,
|
||||
// which is not possible for MCS_isnull().
|
||||
case CalpontSystemCatalog::DECIMAL:
|
||||
case CalpontSystemCatalog::UDECIMAL:
|
||||
parm[0]->data()->getDecimalVal(row, isNull);
|
||||
break;
|
||||
case CalpontSystemCatalog::CHAR:
|
||||
case CalpontSystemCatalog::VARCHAR:
|
||||
parm[0]->data()->getStrVal(row, isNull);
|
||||
break;
|
||||
default:
|
||||
parm[0]->data()->getIntVal(row, isNull);
|
||||
}
|
||||
bool ret = isNull;
|
||||
// It's important to reset isNull indicator.
|
||||
isNull = false;
|
||||
return ret;
|
||||
parm[0]->data()->getDecimalVal(row, isNull);
|
||||
break;
|
||||
|
||||
case CalpontSystemCatalog::CHAR:
|
||||
case CalpontSystemCatalog::VARCHAR:
|
||||
parm[0]->data()->getStrVal(row, isNull);
|
||||
break;
|
||||
|
||||
default:
|
||||
parm[0]->data()->getIntVal(row, isNull);
|
||||
}
|
||||
|
||||
bool ret = isNull;
|
||||
// It's important to reset isNull indicator.
|
||||
isNull = false;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -328,19 +340,19 @@ bool MCS_isnull::getBoolVal(Row& row,
|
||||
* This API is called when a double value is needed to return from the UDF function
|
||||
*/
|
||||
double MCS_isnull::getDoubleVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
return (getBoolVal(row, parm, isNull, op_ct) ? 1 : 0);
|
||||
return (getBoolVal(row, parm, isNull, op_ct) ? 1 : 0);
|
||||
}
|
||||
|
||||
float MCS_isnull::getFloatVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
return (getBoolVal(row, parm, isNull, op_ct) ? 1 : 0);
|
||||
return (getBoolVal(row, parm, isNull, op_ct) ? 1 : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -348,54 +360,54 @@ float MCS_isnull::getFloatVal(Row& row,
|
||||
*
|
||||
* This API is called when an integer value is needed to return from the UDF function
|
||||
*
|
||||
* Because the result type MCS_add is double(real), all the other API can simply call
|
||||
* Because the result type MCS_add is double(real), all the other API can simply call
|
||||
* getDoubleVal and apply the conversion. This method may not fit for all the UDF
|
||||
* implementations.
|
||||
*/
|
||||
int64_t MCS_isnull::getIntVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
return (getBoolVal(row, parm, isNull, op_ct) ? 1 : 0);
|
||||
return (getBoolVal(row, parm, isNull, op_ct) ? 1 : 0);
|
||||
}
|
||||
|
||||
string MCS_isnull::getStrVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
// This needs to be more efficient if this API will be frequently
|
||||
// called for this UDF function.
|
||||
return (getBoolVal(row, parm, isNull, op_ct) ? "1" : "0");
|
||||
// This needs to be more efficient if this API will be frequently
|
||||
// called for this UDF function.
|
||||
return (getBoolVal(row, parm, isNull, op_ct) ? "1" : "0");
|
||||
|
||||
}
|
||||
|
||||
IDB_Decimal MCS_isnull::getDecimalVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
IDB_Decimal dec;
|
||||
dec.value = (getBoolVal(row, parm, isNull, op_ct) ? 1 : 0);
|
||||
dec.scale = 0;
|
||||
return dec;
|
||||
IDB_Decimal dec;
|
||||
dec.value = (getBoolVal(row, parm, isNull, op_ct) ? 1 : 0);
|
||||
dec.scale = 0;
|
||||
return dec;
|
||||
}
|
||||
|
||||
int32_t MCS_isnull::getDateIntVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
return (getBoolVal(row, parm, isNull, op_ct) ? 1 : 0);
|
||||
return (getBoolVal(row, parm, isNull, op_ct) ? 1 : 0);
|
||||
}
|
||||
|
||||
int64_t MCS_isnull::getDatetimeIntVal(Row& row,
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
FunctionParm& parm,
|
||||
bool& isNull,
|
||||
CalpontSystemCatalog::ColType& op_ct)
|
||||
{
|
||||
return (getBoolVal(row, parm, isNull, op_ct) ? 1 : 0);
|
||||
return (getBoolVal(row, parm, isNull, op_ct) ? 1 : 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -22,7 +22,7 @@
|
||||
*
|
||||
***********************************************************************/
|
||||
|
||||
/**
|
||||
/**
|
||||
* MariaDB ColumnStore interface for writing a user defined function (UDF).
|
||||
*
|
||||
* The basic steps are:
|
||||
@ -62,18 +62,18 @@ namespace udfsdk
|
||||
class UDFSDK
|
||||
{
|
||||
public:
|
||||
EXPORT UDFSDK();
|
||||
EXPORT UDFSDK();
|
||||
|
||||
EXPORT ~UDFSDK();
|
||||
EXPORT ~UDFSDK();
|
||||
|
||||
EXPORT funcexp::FuncMap UDFMap() const;
|
||||
EXPORT funcexp::FuncMap UDFMap() const;
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
//defaults okay
|
||||
//UDFSDK(const UDFSDK& rhs);
|
||||
//UDFSDK& operator=(const UDFSDK& rhs);
|
||||
//defaults okay
|
||||
//UDFSDK(const UDFSDK& rhs);
|
||||
//UDFSDK& operator=(const UDFSDK& rhs);
|
||||
|
||||
};
|
||||
|
||||
@ -85,18 +85,18 @@ private:
|
||||
* The function interface is defined here. All UDF functions are derived from
|
||||
* class funcexp::Func. A set of getXXXval interface APIs are declared in the
|
||||
* parent class Func, which will be called by IDB function and expression (F&E)
|
||||
* framwork when evaluating the function. Which API to be called depends on
|
||||
* the context of the function in the SQL query, i.e., the result type that
|
||||
* framwork when evaluating the function. Which API to be called depends on
|
||||
* the context of the function in the SQL query, i.e., the result type that
|
||||
* the function is expected to return.
|
||||
*
|
||||
* For example, given the following two queries, different APIs will be called
|
||||
* to evaluate the function MCS_add.
|
||||
*
|
||||
* select MCS_add(int1, int2) from t1;
|
||||
* select MCS_add(int1, int2) from t1;
|
||||
* getDoubleVal() is called, because the result type of MCS_add is DOUBLE(real).
|
||||
*
|
||||
* select substr(string1, int1, MCS_add(int1+int2));
|
||||
* getIntVal() will be called, because MCS_add() is passed as the third argument
|
||||
* getIntVal() will be called, because MCS_add() is passed as the third argument
|
||||
* to substr function, and an integer result is expected.
|
||||
*
|
||||
* If one API is not implemented but called for a function, IDB-5001 error will
|
||||
@ -105,126 +105,126 @@ private:
|
||||
class MCS_add : public funcexp::Func
|
||||
{
|
||||
public:
|
||||
/*
|
||||
* Constructor. Pass the function name to the base constructor.
|
||||
*/
|
||||
MCS_add() : Func("mcs_add") {}
|
||||
|
||||
/*
|
||||
* Destructor. MCS_add does not need to do anything here to clean up.
|
||||
*/
|
||||
virtual ~MCS_add() {}
|
||||
|
||||
/**
|
||||
* Decide on the function's operation type
|
||||
*
|
||||
* Operation type decides which API needs to be called for each function
|
||||
* parameter. Sometimes it is obvious. e.g. for function substr (c1, c2, c3),
|
||||
* one knows that getStrVal(), getIntVal() and getIntVal() should be called for
|
||||
* the three parameters in sequence. In that case, a dummy type can be returned
|
||||
* because it won't be used in the function implementation. Sometimes the
|
||||
* operation type is decided by the data type of the function parameters.
|
||||
* e.g., isnull(c1) function, one should call the corresponding getXXXval()
|
||||
* function that in compatible with the result type of c1.
|
||||
*
|
||||
* @parm fp vector of function parameters
|
||||
* Each element is a boost::shared_ptr of execplan::ParseTree. class
|
||||
* ParseTree is defined in ~/dbcon/execplan/parsetree.h
|
||||
* @parm resultType result type of this function
|
||||
* Sometimes it may affect the operation type, but most of the time it
|
||||
* can be ignored. Struct ColType is defined in ~/dbcon/execplan/calpontsystemcatalog.h
|
||||
* @return operation type for this function
|
||||
*
|
||||
* This function is called only one from the connector. Once it's determined, it
|
||||
* will be passed to the getXXXval() APIs during function evaluation.
|
||||
*/
|
||||
execplan::CalpontSystemCatalog::ColType operationType(funcexp::FunctionParm& fp, execplan::CalpontSystemCatalog::ColType& resultType);
|
||||
/*
|
||||
* Constructor. Pass the function name to the base constructor.
|
||||
*/
|
||||
MCS_add() : Func("mcs_add") {}
|
||||
|
||||
/**
|
||||
* Returns an integer result of this function.
|
||||
* All the getXXXvalue APIs take the same arguments. They will be called
|
||||
* for every row in the result set when the function is being evaluated.
|
||||
* So these functions needs to be efficient.
|
||||
*
|
||||
* @parm row reference of the current row
|
||||
* @parm fp function parameters
|
||||
* @parm isNull NULL indicator throughout this function evaluation.
|
||||
* the same reference is passed to all the function argument
|
||||
* evaluations. One always need to know if any argument is NULL
|
||||
* to decide the result of the function. It's explained in detail
|
||||
* in MCS_isnull() function example.
|
||||
* @parm op_ct the operation type that is determined in operationType().
|
||||
*
|
||||
*/
|
||||
virtual int64_t getIntVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a double result of this function.
|
||||
*/
|
||||
/*
|
||||
* Destructor. MCS_add does not need to do anything here to clean up.
|
||||
*/
|
||||
virtual ~MCS_add() {}
|
||||
|
||||
virtual double getDoubleVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
/**
|
||||
* Decide on the function's operation type
|
||||
*
|
||||
* Operation type decides which API needs to be called for each function
|
||||
* parameter. Sometimes it is obvious. e.g. for function substr (c1, c2, c3),
|
||||
* one knows that getStrVal(), getIntVal() and getIntVal() should be called for
|
||||
* the three parameters in sequence. In that case, a dummy type can be returned
|
||||
* because it won't be used in the function implementation. Sometimes the
|
||||
* operation type is decided by the data type of the function parameters.
|
||||
* e.g., isnull(c1) function, one should call the corresponding getXXXval()
|
||||
* function that in compatible with the result type of c1.
|
||||
*
|
||||
* @parm fp vector of function parameters
|
||||
* Each element is a boost::shared_ptr of execplan::ParseTree. class
|
||||
* ParseTree is defined in ~/dbcon/execplan/parsetree.h
|
||||
* @parm resultType result type of this function
|
||||
* Sometimes it may affect the operation type, but most of the time it
|
||||
* can be ignored. Struct ColType is defined in ~/dbcon/execplan/calpontsystemcatalog.h
|
||||
* @return operation type for this function
|
||||
*
|
||||
* This function is called only one from the connector. Once it's determined, it
|
||||
* will be passed to the getXXXval() APIs during function evaluation.
|
||||
*/
|
||||
execplan::CalpontSystemCatalog::ColType operationType(funcexp::FunctionParm& fp, execplan::CalpontSystemCatalog::ColType& resultType);
|
||||
|
||||
/**
|
||||
* Returns a float result of this function.
|
||||
*/
|
||||
virtual float getFloatVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a string result of this function.
|
||||
*/
|
||||
virtual std::string getStrVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a bool result of this function.
|
||||
*/
|
||||
virtual bool getBoolVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a decimal result of this function.
|
||||
*
|
||||
* IDB_Decimal is defined in ~/execplan/treenode.h
|
||||
*/
|
||||
virtual execplan::IDB_Decimal getDecimalVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns an integer representation of a date result of the function.
|
||||
*
|
||||
* Check the date/time functions in ~/utils/funcexp for implementation
|
||||
* example of this API.
|
||||
*/
|
||||
virtual int32_t getDateIntVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns an integer representation of a datetime result of the function.
|
||||
*
|
||||
* Check the date/time functions in ~/utils/funcexp for implementation
|
||||
* example of this API.
|
||||
*/
|
||||
virtual int64_t getDatetimeIntVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
/**
|
||||
* Returns an integer result of this function.
|
||||
* All the getXXXvalue APIs take the same arguments. They will be called
|
||||
* for every row in the result set when the function is being evaluated.
|
||||
* So these functions needs to be efficient.
|
||||
*
|
||||
* @parm row reference of the current row
|
||||
* @parm fp function parameters
|
||||
* @parm isNull NULL indicator throughout this function evaluation.
|
||||
* the same reference is passed to all the function argument
|
||||
* evaluations. One always need to know if any argument is NULL
|
||||
* to decide the result of the function. It's explained in detail
|
||||
* in MCS_isnull() function example.
|
||||
* @parm op_ct the operation type that is determined in operationType().
|
||||
*
|
||||
*/
|
||||
virtual int64_t getIntVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a double result of this function.
|
||||
*/
|
||||
|
||||
virtual double getDoubleVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a float result of this function.
|
||||
*/
|
||||
virtual float getFloatVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a string result of this function.
|
||||
*/
|
||||
virtual std::string getStrVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a bool result of this function.
|
||||
*/
|
||||
virtual bool getBoolVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a decimal result of this function.
|
||||
*
|
||||
* IDB_Decimal is defined in ~/execplan/treenode.h
|
||||
*/
|
||||
virtual execplan::IDB_Decimal getDecimalVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns an integer representation of a date result of the function.
|
||||
*
|
||||
* Check the date/time functions in ~/utils/funcexp for implementation
|
||||
* example of this API.
|
||||
*/
|
||||
virtual int32_t getDateIntVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns an integer representation of a datetime result of the function.
|
||||
*
|
||||
* Check the date/time functions in ~/utils/funcexp for implementation
|
||||
* example of this API.
|
||||
*/
|
||||
virtual int64_t getDatetimeIntVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -235,92 +235,92 @@ public:
|
||||
class MCS_isnull : public funcexp::Func
|
||||
{
|
||||
public:
|
||||
/*
|
||||
* Constructor. Pass the function name to the base constructor.
|
||||
*/
|
||||
MCS_isnull() : Func("mcs_isnull") {}
|
||||
|
||||
/*
|
||||
* Destructor. MCS_add does not need to do anything here to clean up.
|
||||
*/
|
||||
virtual ~MCS_isnull() {}
|
||||
|
||||
/**
|
||||
* Decide on the function's operation type
|
||||
*/
|
||||
execplan::CalpontSystemCatalog::ColType operationType(funcexp::FunctionParm& fp, execplan::CalpontSystemCatalog::ColType& resultType);
|
||||
/*
|
||||
* Constructor. Pass the function name to the base constructor.
|
||||
*/
|
||||
MCS_isnull() : Func("mcs_isnull") {}
|
||||
|
||||
/**
|
||||
* Returns an integer result of this function.
|
||||
*/
|
||||
virtual int64_t getIntVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a double result of this function.
|
||||
*/
|
||||
virtual double getDoubleVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
/*
|
||||
* Destructor. MCS_add does not need to do anything here to clean up.
|
||||
*/
|
||||
virtual ~MCS_isnull() {}
|
||||
|
||||
/**
|
||||
* Returns a float result of this function.
|
||||
*/
|
||||
virtual float getFloatVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a string result of this function.
|
||||
*/
|
||||
virtual std::string getStrVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a bool result of this function.
|
||||
*/
|
||||
virtual bool getBoolVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a decimal result of this function.
|
||||
*
|
||||
* IDB_Decimal is defined in ~/execplan/treenode.h
|
||||
*/
|
||||
virtual execplan::IDB_Decimal getDecimalVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
/**
|
||||
* Decide on the function's operation type
|
||||
*/
|
||||
execplan::CalpontSystemCatalog::ColType operationType(funcexp::FunctionParm& fp, execplan::CalpontSystemCatalog::ColType& resultType);
|
||||
|
||||
/**
|
||||
* Returns an integer representation of a date result of the function.
|
||||
*
|
||||
* Check the date/time functions in ~/utils/funcexp for implementation
|
||||
* example of this API.
|
||||
*/
|
||||
virtual int32_t getDateIntVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns an integer representation of a datetime result of the function.
|
||||
*
|
||||
* Check the date/time functions in ~/utils/funcexp for implementation
|
||||
* example of this API.
|
||||
*/
|
||||
virtual int64_t getDatetimeIntVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
/**
|
||||
* Returns an integer result of this function.
|
||||
*/
|
||||
virtual int64_t getIntVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a double result of this function.
|
||||
*/
|
||||
virtual double getDoubleVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a float result of this function.
|
||||
*/
|
||||
virtual float getFloatVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a string result of this function.
|
||||
*/
|
||||
virtual std::string getStrVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a bool result of this function.
|
||||
*/
|
||||
virtual bool getBoolVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns a decimal result of this function.
|
||||
*
|
||||
* IDB_Decimal is defined in ~/execplan/treenode.h
|
||||
*/
|
||||
virtual execplan::IDB_Decimal getDecimalVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns an integer representation of a date result of the function.
|
||||
*
|
||||
* Check the date/time functions in ~/utils/funcexp for implementation
|
||||
* example of this API.
|
||||
*/
|
||||
virtual int32_t getDateIntVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
|
||||
/**
|
||||
* Returns an integer representation of a datetime result of the function.
|
||||
*
|
||||
* Check the date/time functions in ~/utils/funcexp for implementation
|
||||
* example of this API.
|
||||
*/
|
||||
virtual int64_t getDatetimeIntVal(rowgroup::Row& row,
|
||||
funcexp::FunctionParm& fp,
|
||||
bool& isNull,
|
||||
execplan::CalpontSystemCatalog::ColType& op_ct);
|
||||
};
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user