UDF isnumeric

Moderator: NorbertKrupa

Post Reply
User avatar
nnani
Master
Master
Posts: 302
Joined: Fri Apr 13, 2012 6:28 am
Contact:

UDF isnumeric

Post by nnani » Tue Feb 25, 2014 2:58 pm

Hello All,

I have written a sample udf for isnumeric fucntion.

To be frank, I am beginner to C++ and this is my first UDx.

I wanted to make a UDx which checks if the input values is numeric.

The code is like this

Code: Select all

using namespace std;
/*
 * ScalarFunction implementation for a UDSF that adds
 * two numbers together.
 */
class isnumeric : public ScalarFunction
{
public:
  /*
   * This function does all of the actual processing for the UDF.
   * In this case, it simply reads two integer values and returns
   * their sum.
   *
   * The inputs are retrieved via arg_reader
   * The outputs are returned via arg_writer
   */


  virtual void processBlock(ServerInterface &srvInterface,
                            BlockReader &arg_reader,
                            BlockWriter &res_writer)
  {
    // While we have input to process
    do
      {
        // Read the two integer input parameters by calling the
        // BlockReader.getIntRef class function
       char  inStr  = arg_reader.getStringRef(0);

        int sizeOfString = strlen(inStr);
        int iteration = 0;
        bool isNumeric = true;

                while(iteration < sizeOfString)
                {
                        if(!isdigit(string[iteration]))
                        {
                                isNumeric = false;
                                break;
                        }

                        iteration++;

                }



        // Call BlockWriter.setInt to store the output value, which is the
        //  two input values added together
        res_writer.setBool(isNumeric);
        // Finish writing the row, and advance to the next output row
        res_writer.next();
        // Continue looping until there are no more input rows
      }
    while (arg_reader.next());
  }
};

/*
* This class provides metadata about the ScalarFunction class, and
* also instantiates a member of that class when needed.
*/
class isnumericFactory : public ScalarFunctionFactory
{
  // return an instance of Add2Ints to perform the actual addition.
  virtual ScalarFunction *createScalarFunction(ServerInterface &interface)
  {
    // Calls the vt_createFuncObj to create the new Add2Ints class instance.
    return vt_createFuncObj(interface.allocator, isnumeric);
  }
  // This function returns the description of the input and outputs of the
  // Add2Ints class's processBlock function.  It stores this information in
  // two ColumnTypes objects, one for the input parameters, and one for
  // the return value.
  virtual void getPrototype(ServerInterface &interface,
                            ColumnTypes &argTypes,
                            ColumnTypes &returnType)
  {
    // Takes two ints as inputs, so add ints to the argTypes object
    argTypes.addVarchar();

    // returns a single int, so add a single int to the returnType object.
    // Note that ScalarFunctions *always* return a single value.
    returnType.addBool();
  }
};

// Register the factory with HP Vertica
RegisterFactory(isnumericFactory);

The code above might have silly mistakes, but I tried rectifying most of them. It still gives me this error

Code: Select all

[nnani]$ g++ -D HAVE_LONG_INT_64 -I /opt/vertica/sdk/include -Wall -shared -Wno-unused-value -fPIC -o /home/dbadmin/navin/isnumeric.so isnumeric.cpp /opt/vertica/sdk/include/Vertica.cpp
isnumeric.cpp: In member function ‘virtual void isnumeric::processBlock(Vertica::ServerInterface&, Vertica::BlockReader&, Vertica::BlockWriter&)’:
isnumeric.cpp:33: error: cannot convert ‘const Vertica::VString’ to ‘char’ in initialization
isnumeric.cpp:35: error: invalid conversion from ‘char’ to ‘const char*’
isnumeric.cpp:35: error:   initializing argument 1 of ‘size_t strlen(const char*)’
isnumeric.cpp:41: error: expected primary-expression before ‘[’ token

Can anybody point me with what is going wrong here.
Also, it would be great to have some info on my mistakes.

Thanks :)
nnani........
Long way to go

You can check out my blogs at vertica-howto

Post Reply

Return to “Vertica User Defined Functions (UDFs)”