C++ Example: Add2Ints

The following example shows a very basic subclass of ScalarFunction called Add2ints. As the name implies it adds two integers together, returning a single integer result. It also demonstrates including the main Vertica SDK header file (Vertica.h) and using the Vertica namespace. While not required, using the namespace saves you from having to prefix every Vertica SDK class reference with Vertica::.

ScalarFunction Implementation

// Include the top-level Vertica SDK file
#include "Vertica.h"
// Using the Vertica namespace means we don't have to prefix all
// class references with Vertica::
using namespace Vertica;
/* 
 * ScalarFunction implementation for a UDSF that adds 
 * two numbers together. 
 */
class Add2Ints : 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
            const vint a = arg_reader.getIntRef(0);
            const vint b = arg_reader.getIntRef(1);
            // Call BlockWriter.setInt to store the output value, which is the
            //  two input values added together
            res_writer.setInt(a+b);
            // 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());
  }
};

ScalarFunctionFactory Implementation

/*
 * This class provides metadata about the ScalarFunction class, and
 * also instantiates a member of that class when needed.
 */
class Add2IntsFactory : 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, Add2Ints);
	}
	// 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.addInt();
		argTypes.addInt();
		// returns a single int, so add a single int to the returnType object.
		// Note that ScalarFunctions *always* return a single value.
		returnType.addInt();
	}
};

The RegisterFactory Macro

Use the RegisterFactory macro to register a ScalarFunctionFactory subclass. This macro instantiates the factory class and makes the metadata it contains available for Vertica to access. To call this macro, pass it the name of your factory class.

RegisterFactory(Add2IntsFactory);