hi im trying to write sclar function which basicly concatenates ll given columns and return concetanated string.
its working when i give all values as string but i dont know how to convert integer float double or date vaues to string and concat to the string.
following is the point i need to append integer to a string. but Im not sure how to correctly cast this "vint" variable to a string.
std::string myConcat;
// needd to cast the arg_reader.getIntRef(i) inside append but how ??
myConcat.append(arg_reader.getIntRef(i));
basicly how can i convert any c++ vertica variable to string (integer, double, timestamp, date)
second question is
can i call MD5 sql function inside UDF ? i cant see md5 class inthe c++ sdk
full code is
/* Copyright (c)2015 memiiso
*
* UDF function concatenates all given variables and returns concatenated string
*
*/
#include "Vertica.h"
using namespace Vertica;
using namespace std;
class ConcatAll : public Vertica::ScalarFunction
{
public:
virtual void processBlock(Vertica::ServerInterface &srvInterface,
Vertica::BlockReader &arg_reader,
Vertica::BlockWriter &res_writer)
{
// Always catch exceptions to prevent causing the side process or
// Vertica itself from crashing.
try
{
// Find the number of arguments sent.
size_t numCols = arg_reader.getNumCols();
// Make sure at least 1 arguments were supplied
if ((int)numCols < 0)
vt_report_error(0, "Function expects at least 1 parameter");
SizedColumnTypes arg_types = arg_reader.getTypeMetaData();
do
{ // covert to string and concetaneta all into myConcat.
std::string myConcat;
// Loop over all params
for (int i=0; i<(int)numCols; i++) {
const VerticaType &vt = arg_types.getColumnType(i);
if(arg_reader.isNull(i)){
myConcat.append("");
} else if (vt.isStringType() || vt.isLongVarchar() || vt.isChar()) {
myConcat.append(arg_reader.getStringRef(i).str());
// myConcat.append(arg_reader.getStringPtr(i).str());
} else if (vt.isInt()){
myConcat.append(arg_reader.getIntRef(i).str());
}
myConcat.append("^^");
}
res_writer.getStringRef().copy(myConcat);
res_writer.next();
} while (arg_reader.next());
} catch(exception& e) {
// Standard exception. Quit.
vt_report_error(0, "Exception while processing partition: [%s]",
e.what());
}
}
};
// Defines the AddMany function.
class ConcatAllFactory : public Vertica::ScalarFunctionFactory
{
// Return the function object to process the data.
virtual Vertica::ScalarFunction *createScalarFunction(
Vertica::ServerInterface &srvInterface)
{ return vt_createFuncObj(srvInterface.allocator, ConcatAll); }
// Define the number and types of arguments that this function accepts
virtual void getPrototype(Vertica::ServerInterface &srvInterface,
Vertica::ColumnTypes &argTypes,
Vertica::ColumnTypes &returnType)
{
argTypes.addAny(); // Must be only argument type.
returnType.addVarchar();
}
virtual void getReturnType(ServerInterface &srvfloaterface,
const SizedColumnTypes &inputTypes,
SizedColumnTypes &outputTypes)
{
outputTypes.addVarchar(65000);
}
};
RegisterFactory(ConcatAllFactory);
Thank you
Sign up
Already have an account? Login
Welcome to the Rocket Forum!
Please log in or register:
Employee Login | Registration Member Login | RegistrationEnter your E-mail address. We'll send you an e-mail with instructions to reset your password.