Java Example: Add2Ints

The Add2Ints scalar function adds two integers together, returning a single integer result.

Loading and Using the Example

Use CREATE LIBRARY to load the jar file containing the function, and then use CREATE FUNCTION (UDF) to declare the function as in the following example:

=> CREATE FUNCTION add2ints AS LANGUAGE 'Java' NAME
	'com.mycompany.example.Add2intsFactory' LIBRARY add2intslib;
			

The following example shows how to use this function.

=> SELECT Add2Ints(27,15);
 Add2ints
----------
       42
(1 row)
=> SELECT * FROM MyTable;
  a  | b
-----+----
   7 |  0
  12 |  2
  12 |  6
  18 |  9
   1 |  1
  58 |  4
 450 | 15
(7 rows)
=> SELECT * FROM MyTable WHERE Add2ints(a, b) > 20;
  a  | b
-----+----
  18 |  9
  58 |  4
 450 | 15
(3 rows)

Implementation

The following code example is the full source of the Java Add2ints example. To simplify handling the source code, the ScalarFunction class is defined as an inner class of the ScalarFunctionFactory class.

// You will need to specify the full package when creating functions based on 
// the classes in your library.
package com.mycompany.example;
// Import all of the Vertica SDK 
import com.vertica.sdk.*;
public class Add2intsFactory extends ScalarFunctionFactory
{
    @Override
    public void getPrototype(ServerInterface srvInterface,
                             ColumnTypes argTypes,
                             ColumnTypes returnType)
    {
        argTypes.addInt();
        argTypes.addInt();
        returnType.addInt();
    }

    // This ScalarFunction is defined as an inner class of
    // its ScalarFunctionFactory class. This gets around having
    // to have a separate source file for this public class.
    public class Add2ints extends ScalarFunction
    {
        @Override
        public void processBlock(ServerInterface srvInterface,
                                 BlockReader argReader,
                                 BlockWriter resWriter)
                    throws UdfException, DestroyInvocation
        {
            do {
                // The input and output objects have already loaded
                // the first row, so you can start reading and writing 
                // values immediately.
                
                // Get the two integer arguments from the BlockReader
                long a = argReader.getLong(0);
                long b = argReader.getLong(1);
                
                // Process the arguments and come up with a result. For this
                // example, just add the two arguments together.
                long result = a+b;
                
                // Write the integer output value.
                resWriter.setLong(result);
                
                // Advance the output BlocKWriter to the next row.
                resWriter.next();
            
                // Continue processing input rows until there are no more.
            } while (argReader.next());
        }
    }

    @Override
    public ScalarFunction createScalarFunction(ServerInterface srvInterface)
    {
        return new Add2ints();
    }