I am writing a UDF in C++ but facing some errors.
Can anybody please point out my mistakes.
Code: Select all
#include "Vertica.h"
#include <algorithm>
using namespace Vertica;
using namespace std;
class isalpha : public ScalarFunction
{
public:
virtual void processBlock(ServerInterface &srvInterface,
BlockReader &arg_reader,
BlockWriter &res_writer)
{
try {
// Basic error checking
if (arg_reader.getNumCols() != 1)
vt_report_error(0, "Function only accept 1 arguments, but %zu provided",
arg_reader.getNumCols());
// While we have input to process
do
{
std::string inStr = arg_reader.getStringRef(0).str();
std::locale loc;
bool alpha = true;
size_t pos;
while( (pos=inStr.find(' ')) != string::npos || (pos=inStr.find('"')) != string::npos )
inStr.erase(pos, 1);
for (std::string::iterator it=inStr.begin(); it!=inStr.end(); ++it)
{
if (!std::isalpha(*it,loc))
{alpha = false;
break;}
else
{alpha = true;}
}
res_writer.setBool(alpha);
res_writer.next();
}
while (arg_reader.next());
}catch(std::exception& e) {
// Standard exception. Quit.
vt_report_error(0, "Exception while processing block: ", e.what());
}
}
};
class isalphaFactory : public ScalarFunctionFactory
{
virtual ScalarFunction *createScalarFunction(ServerInterface &interface)
{
// Calls the vt_createFuncObj to create the new isalpha class instance.
return vt_createFuncObj(interface.allocator , isalpha);
}
virtual void getPrototype(ServerInterface &interface,
ColumnTypes &argTypes,
ColumnTypes &returnType)
{
argTypes.addVarchar();
returnType.addBool();
}
};
// Register the factory with HP Vertica
RegisterFactory(isalphaFactory);
Code: Select all
dbadmin@eng1vtcfn1 navin]$ g++ -D HAVE_LONG_INT_64 -I /opt/vertica/sdk/include -Wall -shared -Wno-unused-value -fPIC -o home/dbadmin/navin/isalpha.so isalpha.cpp /opt/vertica/sdk/include/Vertica.cpp
isalpha.cpp: In member function ‘virtual Vertica::ScalarFunction* isalphaFactory::createScalarFunction(Vertica::ServerInterface&)’:
isalpha.cpp:65: error: ISO C++ forbids applying ‘sizeof’ to an expression of function type
isalpha.cpp:65: error: expected type-specifier before ‘isalpha’
isalpha.cpp:65: error: expected `)' before ‘isalpha’
isalpha.cpp:65: error: cannot convert ‘int*’ to ‘Vertica::ScalarFunction*’ in return
Thanks in advance