Example: Overriding Default Parameter Mapping to Handle NULLs - Advanced SQL Engine - Teradata Database

SQL External Routine Programming

Product
Advanced SQL Engine
Teradata Database
Release Number
17.05
17.00
Published
June 2020
Language
English (United States)
Last Update
2021-01-24
dita:mapPath
qwr1571437338192.ditamap
dita:ditavalPath
lze1555437562152.ditaval
dita:id
B035-1147
lifecycle
previous
Product Category
Teradata Vantage™

Here is a code example that shows how to implement a Java method for a UDF that maps an SQL INTEGER type parameter to the java.lang.Integer class. This method can handle NULL as an input argument and return NULL as a return value.

public class UDFExample {

   public static Integer fact( Integer x ) {
      if (x == null)
         return null;
      int x_t = x.intValue();
      if (x_t < 0)
         return new Integer(0);
      int factResult = 1;
      while (x_t > 1) {
         factResult = factResult * x_t;
         x_t = x_t - 1;
      }
      return new Integer(factResult);
   }

   ...

}

If the JAR file for the UDFExample class is called UDFExample.jar, the following statement registers UDFExample.jar and the UDFExample class with the database, and creates an SQL identifier called JarUDF for the JAR file:

CALL SQLJ.INSTALL_JAR('CJ!udfsrc/UDFExample.jar','JarUDF',0);

The corresponding CREATE FUNCTION statement to define the UDF looks like this :

CREATE FUNCTION factorial
  (x INTEGER)
RETURNS INTEGER
LANGUAGE JAVA
NO SQL
PARAMETER STYLE JAVA
CALLED ON NULL INPUT
EXTERNAL NAME 'JarUDF:UDFExample.fact(java.lang.Integer) returns
java.lang.Integer';